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>
88 #include <sys/zvol_impl.h>
91 unsigned int zvol_inhibit_dev
= 0;
92 unsigned int zvol_volmode
= ZFS_VOLMODE_GEOM
;
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
;
100 ZVOL_ASYNC_CREATE_MINORS
,
101 ZVOL_ASYNC_REMOVE_MINORS
,
102 ZVOL_ASYNC_RENAME_MINORS
,
103 ZVOL_ASYNC_SET_SNAPDEV
,
104 ZVOL_ASYNC_SET_VOLMODE
,
110 char pool
[MAXNAMELEN
];
111 char name1
[MAXNAMELEN
];
112 char name2
[MAXNAMELEN
];
113 zprop_source_t source
;
118 zvol_name_hash(const char *name
)
121 uint64_t crc
= -1ULL;
122 const uint8_t *p
= (const uint8_t *)name
;
123 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
124 for (i
= 0; i
< MAXNAMELEN
- 1 && *p
; i
++, p
++) {
125 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
131 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
132 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
133 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
134 * before zv_state_lock. The mode argument indicates the mode (including none)
135 * for zv_suspend_lock to be taken.
138 zvol_find_by_name_hash(const char *name
, uint64_t hash
, int mode
)
141 struct hlist_node
*p
= NULL
;
143 rw_enter(&zvol_state_lock
, RW_READER
);
144 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
145 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
146 mutex_enter(&zv
->zv_state_lock
);
147 if (zv
->zv_hash
== hash
&&
148 strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0) {
150 * this is the right zvol, take the locks in the
153 if (mode
!= RW_NONE
&&
154 !rw_tryenter(&zv
->zv_suspend_lock
, mode
)) {
155 mutex_exit(&zv
->zv_state_lock
);
156 rw_enter(&zv
->zv_suspend_lock
, mode
);
157 mutex_enter(&zv
->zv_state_lock
);
159 * zvol cannot be renamed as we continue
160 * to hold zvol_state_lock
162 ASSERT(zv
->zv_hash
== hash
&&
163 strncmp(zv
->zv_name
, name
, MAXNAMELEN
)
166 rw_exit(&zvol_state_lock
);
169 mutex_exit(&zv
->zv_state_lock
);
171 rw_exit(&zvol_state_lock
);
177 * Find a zvol_state_t given the name.
178 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
179 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
180 * before zv_state_lock. The mode argument indicates the mode (including none)
181 * for zv_suspend_lock to be taken.
183 static zvol_state_t
*
184 zvol_find_by_name(const char *name
, int mode
)
186 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
), mode
));
190 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
193 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
195 zfs_creat_t
*zct
= arg
;
196 nvlist_t
*nvprops
= zct
->zct_props
;
198 uint64_t volblocksize
, volsize
;
200 VERIFY(nvlist_lookup_uint64(nvprops
,
201 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
202 if (nvlist_lookup_uint64(nvprops
,
203 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
204 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
207 * These properties must be removed from the list so the generic
208 * property setting step won't apply to them.
210 VERIFY(nvlist_remove_all(nvprops
,
211 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
212 (void) nvlist_remove_all(nvprops
,
213 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
215 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
219 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
223 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
228 * ZFS_IOC_OBJSET_STATS entry point.
231 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
234 dmu_object_info_t
*doi
;
237 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
239 return (SET_ERROR(error
));
241 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
242 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
243 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
246 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
247 doi
->doi_data_block_size
);
250 kmem_free(doi
, sizeof (dmu_object_info_t
));
252 return (SET_ERROR(error
));
256 * Sanity check volume size.
259 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
262 return (SET_ERROR(EINVAL
));
264 if (volsize
% blocksize
!= 0)
265 return (SET_ERROR(EINVAL
));
268 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
269 return (SET_ERROR(EOVERFLOW
));
275 * Ensure the zap is flushed then inform the VFS of the capacity change.
278 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
284 tx
= dmu_tx_create(os
);
285 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
286 dmu_tx_mark_netfree(tx
);
287 error
= dmu_tx_assign(tx
, TXG_WAIT
);
290 return (SET_ERROR(error
));
292 txg
= dmu_tx_get_txg(tx
);
294 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
298 txg_wait_synced(dmu_objset_pool(os
), txg
);
301 error
= dmu_free_long_range(os
,
302 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
308 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume
309 * size will result in a udev "change" event being generated.
312 zvol_set_volsize(const char *name
, uint64_t volsize
)
317 boolean_t owned
= B_FALSE
;
319 error
= dsl_prop_get_integer(name
,
320 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
322 return (SET_ERROR(error
));
324 return (SET_ERROR(EROFS
));
326 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_READER
);
328 ASSERT(zv
== NULL
|| (MUTEX_HELD(&zv
->zv_state_lock
) &&
329 RW_READ_HELD(&zv
->zv_suspend_lock
)));
331 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
333 rw_exit(&zv
->zv_suspend_lock
);
334 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
, B_TRUE
,
337 mutex_exit(&zv
->zv_state_lock
);
338 return (SET_ERROR(error
));
347 dmu_object_info_t
*doi
= kmem_alloc(sizeof (*doi
), KM_SLEEP
);
349 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
350 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
353 error
= zvol_update_volsize(volsize
, os
);
354 if (error
== 0 && zv
!= NULL
) {
355 zv
->zv_volsize
= volsize
;
359 kmem_free(doi
, sizeof (dmu_object_info_t
));
362 dmu_objset_disown(os
, B_TRUE
, FTAG
);
364 zv
->zv_objset
= NULL
;
366 rw_exit(&zv
->zv_suspend_lock
);
370 mutex_exit(&zv
->zv_state_lock
);
372 if (error
== 0 && zv
!= NULL
)
373 ops
->zv_update_volsize(zv
, volsize
);
375 return (SET_ERROR(error
));
379 * Sanity check volume block size.
382 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
384 /* Record sizes above 128k need the feature to be enabled */
385 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
389 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
392 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
393 spa_close(spa
, FTAG
);
394 return (SET_ERROR(ENOTSUP
));
398 * We don't allow setting the property above 1MB,
399 * unless the tunable has been changed.
401 if (volblocksize
> zfs_max_recordsize
)
402 return (SET_ERROR(EDOM
));
404 spa_close(spa
, FTAG
);
407 if (volblocksize
< SPA_MINBLOCKSIZE
||
408 volblocksize
> SPA_MAXBLOCKSIZE
||
410 return (SET_ERROR(EDOM
));
416 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
419 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
425 zv
= zvol_find_by_name(name
, RW_READER
);
428 return (SET_ERROR(ENXIO
));
430 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
431 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
433 if (zv
->zv_flags
& ZVOL_RDONLY
) {
434 mutex_exit(&zv
->zv_state_lock
);
435 rw_exit(&zv
->zv_suspend_lock
);
436 return (SET_ERROR(EROFS
));
439 tx
= dmu_tx_create(zv
->zv_objset
);
440 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
441 error
= dmu_tx_assign(tx
, TXG_WAIT
);
445 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
446 volblocksize
, 0, tx
);
447 if (error
== ENOTSUP
)
448 error
= SET_ERROR(EBUSY
);
451 zv
->zv_volblocksize
= volblocksize
;
454 mutex_exit(&zv
->zv_state_lock
);
455 rw_exit(&zv
->zv_suspend_lock
);
457 return (SET_ERROR(error
));
461 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
462 * implement DKIOCFREE/free-long-range.
465 zvol_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
467 zvol_state_t
*zv
= arg1
;
468 lr_truncate_t
*lr
= arg2
;
469 uint64_t offset
, length
;
472 byteswap_uint64_array(lr
, sizeof (*lr
));
474 offset
= lr
->lr_offset
;
475 length
= lr
->lr_length
;
477 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
481 * Replay a TX_WRITE ZIL transaction that didn't get committed
482 * after a system failure
485 zvol_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
487 zvol_state_t
*zv
= arg1
;
488 lr_write_t
*lr
= arg2
;
489 objset_t
*os
= zv
->zv_objset
;
490 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
491 uint64_t offset
, length
;
496 byteswap_uint64_array(lr
, sizeof (*lr
));
498 offset
= lr
->lr_offset
;
499 length
= lr
->lr_length
;
501 /* If it's a dmu_sync() block, write the whole block */
502 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
503 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
504 if (length
< blocksize
) {
505 offset
-= offset
% blocksize
;
510 tx
= dmu_tx_create(os
);
511 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
512 error
= dmu_tx_assign(tx
, TXG_WAIT
);
516 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
524 zvol_replay_err(void *arg1
, void *arg2
, boolean_t byteswap
)
526 return (SET_ERROR(ENOTSUP
));
530 * Callback vectors for replaying records.
531 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
533 zil_replay_func_t
*zvol_replay_vector
[TX_MAX_TYPE
] = {
534 zvol_replay_err
, /* no such transaction type */
535 zvol_replay_err
, /* TX_CREATE */
536 zvol_replay_err
, /* TX_MKDIR */
537 zvol_replay_err
, /* TX_MKXATTR */
538 zvol_replay_err
, /* TX_SYMLINK */
539 zvol_replay_err
, /* TX_REMOVE */
540 zvol_replay_err
, /* TX_RMDIR */
541 zvol_replay_err
, /* TX_LINK */
542 zvol_replay_err
, /* TX_RENAME */
543 zvol_replay_write
, /* TX_WRITE */
544 zvol_replay_truncate
, /* TX_TRUNCATE */
545 zvol_replay_err
, /* TX_SETATTR */
546 zvol_replay_err
, /* TX_ACL */
547 zvol_replay_err
, /* TX_CREATE_ATTR */
548 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
549 zvol_replay_err
, /* TX_MKDIR_ACL */
550 zvol_replay_err
, /* TX_MKDIR_ATTR */
551 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
552 zvol_replay_err
, /* TX_WRITE2 */
556 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
558 * We store data in the log buffers if it's small enough.
559 * Otherwise we will later flush the data out via dmu_sync().
561 ssize_t zvol_immediate_write_sz
= 32768;
564 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
565 uint64_t size
, int sync
)
567 uint32_t blocksize
= zv
->zv_volblocksize
;
568 zilog_t
*zilog
= zv
->zv_zilog
;
569 itx_wr_state_t write_state
;
571 if (zil_replaying(zilog
, tx
))
574 if (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
575 write_state
= WR_INDIRECT
;
576 else if (!spa_has_slogs(zilog
->zl_spa
) &&
577 size
>= blocksize
&& blocksize
> zvol_immediate_write_sz
)
578 write_state
= WR_INDIRECT
;
580 write_state
= WR_COPIED
;
582 write_state
= WR_NEED_COPY
;
587 itx_wr_state_t wr_state
= write_state
;
590 if (wr_state
== WR_COPIED
&& size
> zil_max_copied_data(zilog
))
591 wr_state
= WR_NEED_COPY
;
592 else if (wr_state
== WR_INDIRECT
)
593 len
= MIN(blocksize
- P2PHASE(offset
, blocksize
), size
);
595 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
596 (wr_state
== WR_COPIED
? len
: 0));
597 lr
= (lr_write_t
*)&itx
->itx_lr
;
598 if (wr_state
== WR_COPIED
&& dmu_read_by_dnode(zv
->zv_dn
,
599 offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
600 zil_itx_destroy(itx
);
601 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
602 lr
= (lr_write_t
*)&itx
->itx_lr
;
603 wr_state
= WR_NEED_COPY
;
606 itx
->itx_wr_state
= wr_state
;
607 lr
->lr_foid
= ZVOL_OBJ
;
608 lr
->lr_offset
= offset
;
611 BP_ZERO(&lr
->lr_blkptr
);
613 itx
->itx_private
= zv
;
614 itx
->itx_sync
= sync
;
616 (void) zil_itx_assign(zilog
, itx
, tx
);
624 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
627 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
632 zilog_t
*zilog
= zv
->zv_zilog
;
634 if (zil_replaying(zilog
, tx
))
637 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
638 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
639 lr
->lr_foid
= ZVOL_OBJ
;
643 itx
->itx_sync
= sync
;
644 zil_itx_assign(zilog
, itx
, tx
);
650 zvol_get_done(zgd_t
*zgd
, int error
)
653 dmu_buf_rele(zgd
->zgd_db
, zgd
);
655 rangelock_exit(zgd
->zgd_lr
);
657 kmem_free(zgd
, sizeof (zgd_t
));
661 * Get data to generate a TX_WRITE intent log record.
664 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
666 zvol_state_t
*zv
= arg
;
667 uint64_t offset
= lr
->lr_offset
;
668 uint64_t size
= lr
->lr_length
;
673 ASSERT3P(lwb
, !=, NULL
);
674 ASSERT3P(zio
, !=, NULL
);
675 ASSERT3U(size
, !=, 0);
677 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
681 * Write records come in two flavors: immediate and indirect.
682 * For small writes it's cheaper to store the data with the
683 * log record (immediate); for large writes it's cheaper to
684 * sync the data and get a pointer to it (indirect) so that
685 * we don't have to write the data twice.
687 if (buf
!= NULL
) { /* immediate write */
688 zgd
->zgd_lr
= rangelock_enter(&zv
->zv_rangelock
, offset
, size
,
690 error
= dmu_read_by_dnode(zv
->zv_dn
, offset
, size
, buf
,
691 DMU_READ_NO_PREFETCH
);
692 } else { /* indirect write */
694 * Have to lock the whole block to ensure when it's written out
695 * and its checksum is being calculated that no one can change
696 * the data. Contrarily to zfs_get_data we need not re-check
697 * blocksize after we get the lock because it cannot be changed.
699 size
= zv
->zv_volblocksize
;
700 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
701 zgd
->zgd_lr
= rangelock_enter(&zv
->zv_rangelock
, offset
, size
,
703 error
= dmu_buf_hold_by_dnode(zv
->zv_dn
, offset
, zgd
, &db
,
704 DMU_READ_NO_PREFETCH
);
706 blkptr_t
*bp
= &lr
->lr_blkptr
;
712 ASSERT(db
->db_offset
== offset
);
713 ASSERT(db
->db_size
== size
);
715 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
723 zvol_get_done(zgd
, error
);
725 return (SET_ERROR(error
));
729 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
733 zvol_insert(zvol_state_t
*zv
)
735 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
736 list_insert_head(&zvol_state_list
, zv
);
737 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
741 * Simply remove the zvol from to list of zvols.
744 zvol_remove(zvol_state_t
*zv
)
746 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
747 list_remove(&zvol_state_list
, zv
);
748 hlist_del(&zv
->zv_hlink
);
752 * Setup zv after we just own the zv->objset
755 zvol_setup_zv(zvol_state_t
*zv
)
760 objset_t
*os
= zv
->zv_objset
;
762 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
763 ASSERT(RW_LOCK_HELD(&zv
->zv_suspend_lock
));
766 zv
->zv_flags
&= ~ZVOL_WRITTEN_TO
;
768 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
770 return (SET_ERROR(error
));
772 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
774 return (SET_ERROR(error
));
776 error
= dnode_hold(os
, ZVOL_OBJ
, FTAG
, &zv
->zv_dn
);
778 return (SET_ERROR(error
));
780 ops
->zv_set_capacity(zv
, volsize
>> 9);
781 zv
->zv_volsize
= volsize
;
783 if (ro
|| dmu_objset_is_snapshot(os
) ||
784 !spa_writeable(dmu_objset_spa(os
))) {
785 ops
->zv_set_disk_ro(zv
, 1);
786 zv
->zv_flags
|= ZVOL_RDONLY
;
788 ops
->zv_set_disk_ro(zv
, 0);
789 zv
->zv_flags
&= ~ZVOL_RDONLY
;
795 * Shutdown every zv_objset related stuff except zv_objset itself.
796 * The is the reverse of zvol_setup_zv.
799 zvol_shutdown_zv(zvol_state_t
*zv
)
801 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
) &&
802 RW_LOCK_HELD(&zv
->zv_suspend_lock
));
804 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
) {
805 ASSERT(zv
->zv_zilog
!= NULL
);
806 zil_close(zv
->zv_zilog
);
811 dnode_rele(zv
->zv_dn
, FTAG
);
815 * Evict cached data. We must write out any dirty data before
816 * disowning the dataset.
818 if (zv
->zv_flags
& ZVOL_WRITTEN_TO
)
819 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
820 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
824 * return the proper tag for rollback and recv
827 zvol_tag(zvol_state_t
*zv
)
829 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
830 return (zv
->zv_open_count
> 0 ? zv
: NULL
);
834 * Suspend the zvol for recv and rollback.
837 zvol_suspend(const char *name
)
841 zv
= zvol_find_by_name(name
, RW_WRITER
);
846 /* block all I/O, release in zvol_resume. */
847 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
848 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
850 atomic_inc(&zv
->zv_suspend_ref
);
852 if (zv
->zv_open_count
> 0)
853 zvol_shutdown_zv(zv
);
856 * do not hold zv_state_lock across suspend/resume to
857 * avoid locking up zvol lookups
859 mutex_exit(&zv
->zv_state_lock
);
861 /* zv_suspend_lock is released in zvol_resume() */
866 zvol_resume(zvol_state_t
*zv
)
870 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
872 mutex_enter(&zv
->zv_state_lock
);
874 if (zv
->zv_open_count
> 0) {
875 VERIFY0(dmu_objset_hold(zv
->zv_name
, zv
, &zv
->zv_objset
));
876 VERIFY3P(zv
->zv_objset
->os_dsl_dataset
->ds_owner
, ==, zv
);
877 VERIFY(dsl_dataset_long_held(zv
->zv_objset
->os_dsl_dataset
));
878 dmu_objset_rele(zv
->zv_objset
, zv
);
880 error
= zvol_setup_zv(zv
);
883 mutex_exit(&zv
->zv_state_lock
);
885 rw_exit(&zv
->zv_suspend_lock
);
887 * We need this because we don't hold zvol_state_lock while releasing
888 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
889 * zv_suspend_lock to determine it is safe to free because rwlock is
890 * not inherent atomic.
892 atomic_dec(&zv
->zv_suspend_ref
);
894 return (SET_ERROR(error
));
898 zvol_first_open(zvol_state_t
*zv
, boolean_t readonly
)
901 int error
, locked
= 0;
904 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
905 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
908 * In all other cases the spa_namespace_lock is taken before the
909 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
910 * function calls fops->open() with the bdev->bd_mutex lock held.
911 * This deadlock can be easily observed with zvols used as vdevs.
913 * To avoid a potential lock inversion deadlock we preemptively
914 * try to take the spa_namespace_lock(). Normally it will not
915 * be contended and this is safe because spa_open_common() handles
916 * the case where the caller already holds the spa_namespace_lock.
918 * When it is contended we risk a lock inversion if we were to
919 * block waiting for the lock. Luckily, the __blkdev_get()
920 * function allows us to return -ERESTARTSYS which will result in
921 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
922 * called again. This process can be repeated safely until both
923 * locks are acquired.
925 if (!mutex_owned(&spa_namespace_lock
)) {
926 locked
= mutex_tryenter(&spa_namespace_lock
);
928 return (SET_ERROR(EINTR
));
931 ro
= (readonly
|| (strchr(zv
->zv_name
, '@') != NULL
));
932 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, ro
, B_TRUE
, zv
, &os
);
938 error
= zvol_setup_zv(zv
);
941 dmu_objset_disown(os
, 1, zv
);
942 zv
->zv_objset
= NULL
;
947 mutex_exit(&spa_namespace_lock
);
948 return (SET_ERROR(error
));
952 zvol_last_close(zvol_state_t
*zv
)
954 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
955 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
957 zvol_shutdown_zv(zv
);
959 dmu_objset_disown(zv
->zv_objset
, 1, zv
);
960 zv
->zv_objset
= NULL
;
963 typedef struct minors_job
{
973 * Prefetch zvol dnodes for the minors_job
976 zvol_prefetch_minors_impl(void *arg
)
978 minors_job_t
*job
= arg
;
979 char *dsname
= job
->name
;
982 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
,
984 if (job
->error
== 0) {
985 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, 0, ZIO_PRIORITY_SYNC_READ
);
986 dmu_objset_disown(os
, B_TRUE
, FTAG
);
991 * Mask errors to continue dmu_objset_find() traversal
994 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
996 minors_job_t
*j
= arg
;
997 list_t
*minors_list
= j
->list
;
998 const char *name
= j
->name
;
1000 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1002 /* skip the designated dataset */
1003 if (name
&& strcmp(dsname
, name
) == 0)
1006 /* at this point, the dsname should name a snapshot */
1007 if (strchr(dsname
, '@') == 0) {
1008 dprintf("zvol_create_snap_minor_cb(): "
1009 "%s is not a snapshot name\n", dsname
);
1012 char *n
= strdup(dsname
);
1016 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1018 job
->list
= minors_list
;
1020 list_insert_tail(minors_list
, job
);
1021 /* don't care if dispatch fails, because job->error is 0 */
1022 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1030 * Mask errors to continue dmu_objset_find() traversal
1033 zvol_create_minors_cb(const char *dsname
, void *arg
)
1037 list_t
*minors_list
= arg
;
1039 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1041 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1046 * Given the name and the 'snapdev' property, create device minor nodes
1047 * with the linkages to zvols/snapshots as needed.
1048 * If the name represents a zvol, create a minor node for the zvol, then
1049 * check if its snapshots are 'visible', and if so, iterate over the
1050 * snapshots and create device minor nodes for those.
1052 if (strchr(dsname
, '@') == 0) {
1054 char *n
= strdup(dsname
);
1058 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1060 job
->list
= minors_list
;
1062 list_insert_tail(minors_list
, job
);
1063 /* don't care if dispatch fails, because job->error is 0 */
1064 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1067 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
1069 * traverse snapshots only, do not traverse children,
1070 * and skip the 'dsname'
1072 error
= dmu_objset_find(dsname
,
1073 zvol_create_snap_minor_cb
, (void *)job
,
1077 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1085 * Create minors for the specified dataset, including children and snapshots.
1086 * Pay attention to the 'snapdev' property and iterate over the snapshots
1087 * only if they are 'visible'. This approach allows one to assure that the
1088 * snapshot metadata is read from disk only if it is needed.
1090 * The name can represent a dataset to be recursively scanned for zvols and
1091 * their snapshots, or a single zvol snapshot. If the name represents a
1092 * dataset, the scan is performed in two nested stages:
1093 * - scan the dataset for zvols, and
1094 * - for each zvol, create a minor node, then check if the zvol's snapshots
1095 * are 'visible', and only then iterate over the snapshots if needed
1097 * If the name represents a snapshot, a check is performed if the snapshot is
1098 * 'visible' (which also verifies that the parent is a zvol), and if so,
1099 * a minor node for that snapshot is created.
1102 zvol_create_minors_impl(const char *name
)
1105 fstrans_cookie_t cookie
;
1110 if (zvol_inhibit_dev
)
1114 * This is the list for prefetch jobs. Whenever we found a match
1115 * during dmu_objset_find, we insert a minors_job to the list and do
1116 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1117 * any lock because all list operation is done on the current thread.
1119 * We will use this list to do zvol_create_minor_impl after prefetch
1120 * so we don't have to traverse using dmu_objset_find again.
1122 list_create(&minors_list
, sizeof (minors_job_t
),
1123 offsetof(minors_job_t
, link
));
1125 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1126 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1128 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1132 error
= dsl_prop_get_integer(parent
, "snapdev",
1135 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1136 error
= ops
->zv_create_minor(name
);
1138 cookie
= spl_fstrans_mark();
1139 error
= dmu_objset_find(parent
, zvol_create_minors_cb
,
1140 &minors_list
, DS_FIND_CHILDREN
);
1141 spl_fstrans_unmark(cookie
);
1144 kmem_free(parent
, MAXPATHLEN
);
1145 taskq_wait_outstanding(system_taskq
, 0);
1148 * Prefetch is completed, we can do zvol_create_minor_impl
1151 while ((job
= list_head(&minors_list
)) != NULL
) {
1152 list_remove(&minors_list
, job
);
1154 ops
->zv_create_minor(job
->name
);
1156 kmem_free(job
, sizeof (minors_job_t
));
1159 list_destroy(&minors_list
);
1161 return (SET_ERROR(error
));
1165 * Remove minors for specified dataset including children and snapshots.
1169 zvol_remove_minors_impl(const char *name
)
1171 zvol_state_t
*zv
, *zv_next
;
1172 int namelen
= ((name
) ? strlen(name
) : 0);
1173 taskqid_t t
, tid
= TASKQID_INVALID
;
1176 if (zvol_inhibit_dev
)
1179 list_create(&free_list
, sizeof (zvol_state_t
),
1180 offsetof(zvol_state_t
, zv_next
));
1182 rw_enter(&zvol_state_lock
, RW_WRITER
);
1184 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1185 zv_next
= list_next(&zvol_state_list
, zv
);
1187 mutex_enter(&zv
->zv_state_lock
);
1188 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1189 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1190 (zv
->zv_name
[namelen
] == '/' ||
1191 zv
->zv_name
[namelen
] == '@'))) {
1193 * By holding zv_state_lock here, we guarantee that no
1194 * one is currently using this zv
1197 /* If in use, leave alone */
1198 if (zv
->zv_open_count
> 0 ||
1199 atomic_read(&zv
->zv_suspend_ref
)) {
1200 mutex_exit(&zv
->zv_state_lock
);
1207 * Cleared while holding zvol_state_lock as a writer
1208 * which will prevent zvol_open() from opening it.
1210 ops
->zv_clear_private(zv
);
1212 /* Drop zv_state_lock before zvol_free() */
1213 mutex_exit(&zv
->zv_state_lock
);
1215 /* Try parallel zv_free, if failed do it in place */
1216 t
= taskq_dispatch(system_taskq
,
1217 (task_func_t
*)ops
->zv_free
, zv
, TQ_SLEEP
);
1218 if (t
== TASKQID_INVALID
)
1219 list_insert_head(&free_list
, zv
);
1223 mutex_exit(&zv
->zv_state_lock
);
1226 rw_exit(&zvol_state_lock
);
1228 /* Drop zvol_state_lock before calling zvol_free() */
1229 while ((zv
= list_head(&free_list
)) != NULL
) {
1230 list_remove(&free_list
, zv
);
1234 if (tid
!= TASKQID_INVALID
)
1235 taskq_wait_outstanding(system_taskq
, tid
);
1238 /* Remove minor for this specific volume only */
1240 zvol_remove_minor_impl(const char *name
)
1242 zvol_state_t
*zv
= NULL
, *zv_next
;
1244 if (zvol_inhibit_dev
)
1247 rw_enter(&zvol_state_lock
, RW_WRITER
);
1249 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1250 zv_next
= list_next(&zvol_state_list
, zv
);
1252 mutex_enter(&zv
->zv_state_lock
);
1253 if (strcmp(zv
->zv_name
, name
) == 0) {
1255 * By holding zv_state_lock here, we guarantee that no
1256 * one is currently using this zv
1259 /* If in use, leave alone */
1260 if (zv
->zv_open_count
> 0 ||
1261 atomic_read(&zv
->zv_suspend_ref
)) {
1262 mutex_exit(&zv
->zv_state_lock
);
1267 ops
->zv_clear_private(zv
);
1268 mutex_exit(&zv
->zv_state_lock
);
1271 mutex_exit(&zv
->zv_state_lock
);
1275 /* Drop zvol_state_lock before calling zvol_free() */
1276 rw_exit(&zvol_state_lock
);
1283 * Rename minors for specified dataset including children and snapshots.
1286 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1288 zvol_state_t
*zv
, *zv_next
;
1289 int oldnamelen
, newnamelen
;
1291 if (zvol_inhibit_dev
)
1294 oldnamelen
= strlen(oldname
);
1295 newnamelen
= strlen(newname
);
1297 rw_enter(&zvol_state_lock
, RW_READER
);
1299 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1300 zv_next
= list_next(&zvol_state_list
, zv
);
1302 mutex_enter(&zv
->zv_state_lock
);
1304 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1305 ops
->zv_rename_minor(zv
, newname
);
1306 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1307 (zv
->zv_name
[oldnamelen
] == '/' ||
1308 zv
->zv_name
[oldnamelen
] == '@')) {
1309 char *name
= kmem_asprintf("%s%c%s", newname
,
1310 zv
->zv_name
[oldnamelen
],
1311 zv
->zv_name
+ oldnamelen
+ 1);
1312 ops
->zv_rename_minor(zv
, name
);
1316 mutex_exit(&zv
->zv_state_lock
);
1319 rw_exit(&zvol_state_lock
);
1322 typedef struct zvol_snapdev_cb_arg
{
1324 } zvol_snapdev_cb_arg_t
;
1327 zvol_set_snapdev_cb(const char *dsname
, void *param
)
1329 zvol_snapdev_cb_arg_t
*arg
= param
;
1331 if (strchr(dsname
, '@') == NULL
)
1334 switch (arg
->snapdev
) {
1335 case ZFS_SNAPDEV_VISIBLE
:
1336 (void) ops
->zv_create_minor(dsname
);
1338 case ZFS_SNAPDEV_HIDDEN
:
1339 (void) zvol_remove_minor_impl(dsname
);
1347 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1349 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1350 fstrans_cookie_t cookie
= spl_fstrans_mark();
1352 * The zvol_set_snapdev_sync() sets snapdev appropriately
1353 * in the dataset hierarchy. Here, we only scan snapshots.
1355 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1356 spl_fstrans_unmark(cookie
);
1359 typedef struct zvol_volmode_cb_arg
{
1361 } zvol_volmode_cb_arg_t
;
1364 zvol_set_volmode_impl(char *name
, uint64_t volmode
)
1366 fstrans_cookie_t cookie
= spl_fstrans_mark();
1368 if (strchr(name
, '@') != NULL
)
1372 * It's unfortunate we need to remove minors before we create new ones:
1373 * this is necessary because our backing gendisk (zvol_state->zv_disk)
1374 * coule be different when we set, for instance, volmode from "geom"
1375 * to "dev" (or vice versa).
1376 * A possible optimization is to modify our consumers so we don't get
1377 * called when "volmode" does not change.
1380 case ZFS_VOLMODE_NONE
:
1381 (void) zvol_remove_minor_impl(name
);
1383 case ZFS_VOLMODE_GEOM
:
1384 case ZFS_VOLMODE_DEV
:
1385 (void) zvol_remove_minor_impl(name
);
1386 (void) ops
->zv_create_minor(name
);
1388 case ZFS_VOLMODE_DEFAULT
:
1389 (void) zvol_remove_minor_impl(name
);
1390 if (zvol_volmode
== ZFS_VOLMODE_NONE
)
1392 else /* if zvol_volmode is invalid defaults to "geom" */
1393 (void) ops
->zv_create_minor(name
);
1397 spl_fstrans_unmark(cookie
);
1400 static zvol_task_t
*
1401 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1407 /* Never allow tasks on hidden names. */
1408 if (name1
[0] == '$')
1411 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1413 task
->value
= value
;
1414 delim
= strchr(name1
, '/');
1415 strlcpy(task
->pool
, name1
, delim
? (delim
- name1
+ 1) : MAXNAMELEN
);
1417 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
1419 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
1425 zvol_task_free(zvol_task_t
*task
)
1427 kmem_free(task
, sizeof (zvol_task_t
));
1431 * The worker thread function performed asynchronously.
1434 zvol_task_cb(void *param
)
1436 zvol_task_t
*task
= (zvol_task_t
*)param
;
1439 case ZVOL_ASYNC_CREATE_MINORS
:
1440 (void) zvol_create_minors_impl(task
->name1
);
1442 case ZVOL_ASYNC_REMOVE_MINORS
:
1443 zvol_remove_minors_impl(task
->name1
);
1445 case ZVOL_ASYNC_RENAME_MINORS
:
1446 zvol_rename_minors_impl(task
->name1
, task
->name2
);
1448 case ZVOL_ASYNC_SET_SNAPDEV
:
1449 zvol_set_snapdev_impl(task
->name1
, task
->value
);
1451 case ZVOL_ASYNC_SET_VOLMODE
:
1452 zvol_set_volmode_impl(task
->name1
, task
->value
);
1459 zvol_task_free(task
);
1462 typedef struct zvol_set_prop_int_arg
{
1463 const char *zsda_name
;
1464 uint64_t zsda_value
;
1465 zprop_source_t zsda_source
;
1467 } zvol_set_prop_int_arg_t
;
1470 * Sanity check the dataset for safe use by the sync task. No additional
1471 * conditions are imposed.
1474 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
1476 zvol_set_prop_int_arg_t
*zsda
= arg
;
1477 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1481 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1485 dsl_dir_rele(dd
, FTAG
);
1492 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1494 char dsname
[MAXNAMELEN
];
1498 dsl_dataset_name(ds
, dsname
);
1499 if (dsl_prop_get_int_ds(ds
, "snapdev", &snapdev
) != 0)
1501 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
, NULL
, snapdev
);
1505 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1511 * Traverse all child datasets and apply snapdev appropriately.
1512 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1513 * dataset and read the effective "snapdev" on every child in the callback
1514 * function: this is because the value is not guaranteed to be the same in the
1515 * whole dataset hierarchy.
1518 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
1520 zvol_set_prop_int_arg_t
*zsda
= arg
;
1521 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1526 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1529 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
1531 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
1532 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1533 &zsda
->zsda_value
, zsda
->zsda_tx
);
1534 dsl_dataset_rele(ds
, FTAG
);
1536 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
1537 zsda
, DS_FIND_CHILDREN
);
1539 dsl_dir_rele(dd
, FTAG
);
1543 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
1545 zvol_set_prop_int_arg_t zsda
;
1547 zsda
.zsda_name
= ddname
;
1548 zsda
.zsda_source
= source
;
1549 zsda
.zsda_value
= snapdev
;
1551 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
1552 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1556 * Sanity check the dataset for safe use by the sync task. No additional
1557 * conditions are imposed.
1560 zvol_set_volmode_check(void *arg
, dmu_tx_t
*tx
)
1562 zvol_set_prop_int_arg_t
*zsda
= arg
;
1563 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1567 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1571 dsl_dir_rele(dd
, FTAG
);
1578 zvol_set_volmode_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1580 char dsname
[MAXNAMELEN
];
1584 dsl_dataset_name(ds
, dsname
);
1585 if (dsl_prop_get_int_ds(ds
, "volmode", &volmode
) != 0)
1587 task
= zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE
, dsname
, NULL
, volmode
);
1591 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1597 * Traverse all child datasets and apply volmode appropriately.
1598 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1599 * dataset and read the effective "volmode" on every child in the callback
1600 * function: this is because the value is not guaranteed to be the same in the
1601 * whole dataset hierarchy.
1604 zvol_set_volmode_sync(void *arg
, dmu_tx_t
*tx
)
1606 zvol_set_prop_int_arg_t
*zsda
= arg
;
1607 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1612 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1615 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
1617 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_VOLMODE
),
1618 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1619 &zsda
->zsda_value
, zsda
->zsda_tx
);
1620 dsl_dataset_rele(ds
, FTAG
);
1623 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_volmode_sync_cb
,
1624 zsda
, DS_FIND_CHILDREN
);
1626 dsl_dir_rele(dd
, FTAG
);
1630 zvol_set_volmode(const char *ddname
, zprop_source_t source
, uint64_t volmode
)
1632 zvol_set_prop_int_arg_t zsda
;
1634 zsda
.zsda_name
= ddname
;
1635 zsda
.zsda_source
= source
;
1636 zsda
.zsda_value
= volmode
;
1638 return (dsl_sync_task(ddname
, zvol_set_volmode_check
,
1639 zvol_set_volmode_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1643 zvol_create_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1648 task
= zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS
, name
, NULL
, ~0ULL);
1652 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1653 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1654 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1658 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1663 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
1667 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1668 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1669 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1673 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
1679 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
1683 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1684 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1685 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1689 zvol_is_zvol(const char *name
)
1692 return (ops
->zv_is_zvol(name
));
1696 zvol_register_ops(const zvol_platform_ops_t
*zvol_ops
)
1702 zvol_init_impl(void)
1706 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1707 offsetof(zvol_state_t
, zv_next
));
1708 rw_init(&zvol_state_lock
, NULL
, RW_DEFAULT
, NULL
);
1710 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
1712 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
1713 INIT_HLIST_HEAD(&zvol_htable
[i
]);
1719 zvol_fini_impl(void)
1721 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
1722 list_destroy(&zvol_state_list
);
1723 rw_destroy(&zvol_state_lock
);