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, 2018 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.
71 * It is also worth keeping in mind that once add_disk() is called, the zvol is
72 * announced to the world, and zvol_open()/zvol_release() can be called at any
73 * time. Incidentally, add_disk() itself calls zvol_open()->zvol_first_open()
74 * and zvol_release()->zvol_last_close() directly as well.
77 #include <sys/dataset_kstats.h>
79 #include <sys/dmu_traverse.h>
80 #include <sys/dsl_dataset.h>
81 #include <sys/dsl_prop.h>
82 #include <sys/dsl_dir.h>
84 #include <sys/zfeature.h>
85 #include <sys/zil_impl.h>
86 #include <sys/dmu_tx.h>
88 #include <sys/zfs_rlock.h>
89 #include <sys/zfs_znode.h>
90 #include <sys/spa_impl.h>
93 #include <linux/blkdev_compat.h>
94 #include <linux/task_io_accounting_ops.h>
96 unsigned int zvol_inhibit_dev
= 0;
97 unsigned int zvol_major
= ZVOL_MAJOR
;
98 unsigned int zvol_threads
= 32;
99 unsigned int zvol_request_sync
= 0;
100 unsigned int zvol_prefetch_bytes
= (128 * 1024);
101 unsigned long zvol_max_discard_blocks
= 16384;
102 unsigned int zvol_volmode
= ZFS_VOLMODE_GEOM
;
104 static taskq_t
*zvol_taskq
;
105 static krwlock_t zvol_state_lock
;
106 static list_t zvol_state_list
;
108 #define ZVOL_HT_SIZE 1024
109 static struct hlist_head
*zvol_htable
;
110 #define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
112 static struct ida zvol_ida
;
115 * The in-core state of each volume.
118 char zv_name
[MAXNAMELEN
]; /* name */
119 uint64_t zv_volsize
; /* advertised space */
120 uint64_t zv_volblocksize
; /* volume block size */
121 objset_t
*zv_objset
; /* objset handle */
122 uint32_t zv_flags
; /* ZVOL_* flags */
123 uint32_t zv_open_count
; /* open counts */
124 uint32_t zv_changed
; /* disk changed */
125 zilog_t
*zv_zilog
; /* ZIL handle */
126 zfs_rlock_t zv_range_lock
; /* range lock */
127 dnode_t
*zv_dn
; /* dnode hold */
128 dev_t zv_dev
; /* device id */
129 struct gendisk
*zv_disk
; /* generic disk */
130 struct request_queue
*zv_queue
; /* request queue */
131 dataset_kstats_t zv_kstat
; /* zvol kstats */
132 list_node_t zv_next
; /* next zvol_state_t linkage */
133 uint64_t zv_hash
; /* name hash */
134 struct hlist_node zv_hlink
; /* hash link */
135 kmutex_t zv_state_lock
; /* protects zvol_state_t */
136 atomic_t zv_suspend_ref
; /* refcount for suspend */
137 krwlock_t zv_suspend_lock
; /* suspend lock */
141 ZVOL_ASYNC_CREATE_MINORS
,
142 ZVOL_ASYNC_REMOVE_MINORS
,
143 ZVOL_ASYNC_RENAME_MINORS
,
144 ZVOL_ASYNC_SET_SNAPDEV
,
145 ZVOL_ASYNC_SET_VOLMODE
,
151 char pool
[MAXNAMELEN
];
152 char name1
[MAXNAMELEN
];
153 char name2
[MAXNAMELEN
];
154 zprop_source_t source
;
158 #define ZVOL_RDONLY 0x1
161 zvol_name_hash(const char *name
)
164 uint64_t crc
= -1ULL;
165 uint8_t *p
= (uint8_t *)name
;
166 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
167 for (i
= 0; i
< MAXNAMELEN
- 1 && *p
; i
++, p
++) {
168 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
174 * Find a zvol_state_t given the full major+minor dev_t. If found,
175 * return with zv_state_lock taken, otherwise, return (NULL) without
176 * taking zv_state_lock.
178 static zvol_state_t
*
179 zvol_find_by_dev(dev_t dev
)
183 rw_enter(&zvol_state_lock
, RW_READER
);
184 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
185 zv
= list_next(&zvol_state_list
, zv
)) {
186 mutex_enter(&zv
->zv_state_lock
);
187 if (zv
->zv_dev
== dev
) {
188 rw_exit(&zvol_state_lock
);
191 mutex_exit(&zv
->zv_state_lock
);
193 rw_exit(&zvol_state_lock
);
199 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
200 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
201 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
202 * before zv_state_lock. The mode argument indicates the mode (including none)
203 * for zv_suspend_lock to be taken.
205 static zvol_state_t
*
206 zvol_find_by_name_hash(const char *name
, uint64_t hash
, int mode
)
209 struct hlist_node
*p
= NULL
;
211 rw_enter(&zvol_state_lock
, RW_READER
);
212 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
213 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
214 mutex_enter(&zv
->zv_state_lock
);
215 if (zv
->zv_hash
== hash
&&
216 strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0) {
218 * this is the right zvol, take the locks in the
221 if (mode
!= RW_NONE
&&
222 !rw_tryenter(&zv
->zv_suspend_lock
, mode
)) {
223 mutex_exit(&zv
->zv_state_lock
);
224 rw_enter(&zv
->zv_suspend_lock
, mode
);
225 mutex_enter(&zv
->zv_state_lock
);
227 * zvol cannot be renamed as we continue
228 * to hold zvol_state_lock
230 ASSERT(zv
->zv_hash
== hash
&&
231 strncmp(zv
->zv_name
, name
, MAXNAMELEN
)
234 rw_exit(&zvol_state_lock
);
237 mutex_exit(&zv
->zv_state_lock
);
239 rw_exit(&zvol_state_lock
);
245 * Find a zvol_state_t given the name.
246 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
247 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
248 * before zv_state_lock. The mode argument indicates the mode (including none)
249 * for zv_suspend_lock to be taken.
251 static zvol_state_t
*
252 zvol_find_by_name(const char *name
, int mode
)
254 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
), mode
));
259 * Given a path, return TRUE if path is a ZVOL.
262 zvol_is_zvol(const char *device
)
264 struct block_device
*bdev
;
267 bdev
= vdev_lookup_bdev(device
);
271 major
= MAJOR(bdev
->bd_dev
);
274 if (major
== zvol_major
)
281 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
284 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
286 zfs_creat_t
*zct
= arg
;
287 nvlist_t
*nvprops
= zct
->zct_props
;
289 uint64_t volblocksize
, volsize
;
291 VERIFY(nvlist_lookup_uint64(nvprops
,
292 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
293 if (nvlist_lookup_uint64(nvprops
,
294 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
295 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
298 * These properties must be removed from the list so the generic
299 * property setting step won't apply to them.
301 VERIFY(nvlist_remove_all(nvprops
,
302 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
303 (void) nvlist_remove_all(nvprops
,
304 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
306 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
310 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
314 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
319 * ZFS_IOC_OBJSET_STATS entry point.
322 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
325 dmu_object_info_t
*doi
;
328 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
330 return (SET_ERROR(error
));
332 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
333 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
334 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
337 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
338 doi
->doi_data_block_size
);
341 kmem_free(doi
, sizeof (dmu_object_info_t
));
343 return (SET_ERROR(error
));
347 * Sanity check volume size.
350 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
353 return (SET_ERROR(EINVAL
));
355 if (volsize
% blocksize
!= 0)
356 return (SET_ERROR(EINVAL
));
359 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
360 return (SET_ERROR(EOVERFLOW
));
366 * Ensure the zap is flushed then inform the VFS of the capacity change.
369 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
375 tx
= dmu_tx_create(os
);
376 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
377 dmu_tx_mark_netfree(tx
);
378 error
= dmu_tx_assign(tx
, TXG_WAIT
);
381 return (SET_ERROR(error
));
383 txg
= dmu_tx_get_txg(tx
);
385 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
389 txg_wait_synced(dmu_objset_pool(os
), txg
);
392 error
= dmu_free_long_range(os
,
393 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
399 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume
400 * size will result in a udev "change" event being generated.
403 zvol_set_volsize(const char *name
, uint64_t volsize
)
406 struct gendisk
*disk
= NULL
;
409 boolean_t owned
= B_FALSE
;
411 error
= dsl_prop_get_integer(name
,
412 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
414 return (SET_ERROR(error
));
416 return (SET_ERROR(EROFS
));
418 zvol_state_t
*zv
= zvol_find_by_name(name
, RW_READER
);
420 ASSERT(zv
== NULL
|| (MUTEX_HELD(&zv
->zv_state_lock
) &&
421 RW_READ_HELD(&zv
->zv_suspend_lock
)));
423 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
425 rw_exit(&zv
->zv_suspend_lock
);
426 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
, B_TRUE
,
429 mutex_exit(&zv
->zv_state_lock
);
430 return (SET_ERROR(error
));
439 dmu_object_info_t
*doi
= kmem_alloc(sizeof (*doi
), KM_SLEEP
);
441 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
442 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
445 error
= zvol_update_volsize(volsize
, os
);
446 if (error
== 0 && zv
!= NULL
) {
447 zv
->zv_volsize
= volsize
;
452 kmem_free(doi
, sizeof (dmu_object_info_t
));
455 dmu_objset_disown(os
, B_TRUE
, FTAG
);
457 zv
->zv_objset
= NULL
;
459 rw_exit(&zv
->zv_suspend_lock
);
463 mutex_exit(&zv
->zv_state_lock
);
466 revalidate_disk(disk
);
468 return (SET_ERROR(error
));
472 * Sanity check volume block size.
475 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
477 /* Record sizes above 128k need the feature to be enabled */
478 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
482 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
485 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
486 spa_close(spa
, FTAG
);
487 return (SET_ERROR(ENOTSUP
));
491 * We don't allow setting the property above 1MB,
492 * unless the tunable has been changed.
494 if (volblocksize
> zfs_max_recordsize
)
495 return (SET_ERROR(EDOM
));
497 spa_close(spa
, FTAG
);
500 if (volblocksize
< SPA_MINBLOCKSIZE
||
501 volblocksize
> SPA_MAXBLOCKSIZE
||
503 return (SET_ERROR(EDOM
));
509 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
512 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
518 zv
= zvol_find_by_name(name
, RW_READER
);
521 return (SET_ERROR(ENXIO
));
523 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
524 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
526 if (zv
->zv_flags
& ZVOL_RDONLY
) {
527 mutex_exit(&zv
->zv_state_lock
);
528 rw_exit(&zv
->zv_suspend_lock
);
529 return (SET_ERROR(EROFS
));
532 tx
= dmu_tx_create(zv
->zv_objset
);
533 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
534 error
= dmu_tx_assign(tx
, TXG_WAIT
);
538 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
539 volblocksize
, 0, tx
);
540 if (error
== ENOTSUP
)
541 error
= SET_ERROR(EBUSY
);
544 zv
->zv_volblocksize
= volblocksize
;
547 mutex_exit(&zv
->zv_state_lock
);
548 rw_exit(&zv
->zv_suspend_lock
);
550 return (SET_ERROR(error
));
554 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
555 * implement DKIOCFREE/free-long-range.
558 zvol_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
560 zvol_state_t
*zv
= arg1
;
561 lr_truncate_t
*lr
= arg2
;
562 uint64_t offset
, length
;
565 byteswap_uint64_array(lr
, sizeof (*lr
));
567 offset
= lr
->lr_offset
;
568 length
= lr
->lr_length
;
570 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
574 * Replay a TX_WRITE ZIL transaction that didn't get committed
575 * after a system failure
578 zvol_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
580 zvol_state_t
*zv
= arg1
;
581 lr_write_t
*lr
= arg2
;
582 objset_t
*os
= zv
->zv_objset
;
583 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
584 uint64_t offset
, length
;
589 byteswap_uint64_array(lr
, sizeof (*lr
));
591 offset
= lr
->lr_offset
;
592 length
= lr
->lr_length
;
594 /* If it's a dmu_sync() block, write the whole block */
595 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
596 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
597 if (length
< blocksize
) {
598 offset
-= offset
% blocksize
;
603 tx
= dmu_tx_create(os
);
604 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
605 error
= dmu_tx_assign(tx
, TXG_WAIT
);
609 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
617 zvol_replay_err(void *arg1
, void *arg2
, boolean_t byteswap
)
619 return (SET_ERROR(ENOTSUP
));
623 * Callback vectors for replaying records.
624 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
626 zil_replay_func_t
*zvol_replay_vector
[TX_MAX_TYPE
] = {
627 zvol_replay_err
, /* no such transaction type */
628 zvol_replay_err
, /* TX_CREATE */
629 zvol_replay_err
, /* TX_MKDIR */
630 zvol_replay_err
, /* TX_MKXATTR */
631 zvol_replay_err
, /* TX_SYMLINK */
632 zvol_replay_err
, /* TX_REMOVE */
633 zvol_replay_err
, /* TX_RMDIR */
634 zvol_replay_err
, /* TX_LINK */
635 zvol_replay_err
, /* TX_RENAME */
636 zvol_replay_write
, /* TX_WRITE */
637 zvol_replay_truncate
, /* TX_TRUNCATE */
638 zvol_replay_err
, /* TX_SETATTR */
639 zvol_replay_err
, /* TX_ACL */
640 zvol_replay_err
, /* TX_CREATE_ATTR */
641 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
642 zvol_replay_err
, /* TX_MKDIR_ACL */
643 zvol_replay_err
, /* TX_MKDIR_ATTR */
644 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
645 zvol_replay_err
, /* TX_WRITE2 */
649 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
651 * We store data in the log buffers if it's small enough.
652 * Otherwise we will later flush the data out via dmu_sync().
654 ssize_t zvol_immediate_write_sz
= 32768;
657 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
658 uint64_t size
, int sync
)
660 uint32_t blocksize
= zv
->zv_volblocksize
;
661 zilog_t
*zilog
= zv
->zv_zilog
;
662 itx_wr_state_t write_state
;
664 if (zil_replaying(zilog
, tx
))
667 if (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
668 write_state
= WR_INDIRECT
;
669 else if (!spa_has_slogs(zilog
->zl_spa
) &&
670 size
>= blocksize
&& blocksize
> zvol_immediate_write_sz
)
671 write_state
= WR_INDIRECT
;
673 write_state
= WR_COPIED
;
675 write_state
= WR_NEED_COPY
;
680 itx_wr_state_t wr_state
= write_state
;
683 if (wr_state
== WR_COPIED
&& size
> ZIL_MAX_COPIED_DATA
)
684 wr_state
= WR_NEED_COPY
;
685 else if (wr_state
== WR_INDIRECT
)
686 len
= MIN(blocksize
- P2PHASE(offset
, blocksize
), size
);
688 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
689 (wr_state
== WR_COPIED
? len
: 0));
690 lr
= (lr_write_t
*)&itx
->itx_lr
;
691 if (wr_state
== WR_COPIED
&& dmu_read_by_dnode(zv
->zv_dn
,
692 offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
693 zil_itx_destroy(itx
);
694 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
695 lr
= (lr_write_t
*)&itx
->itx_lr
;
696 wr_state
= WR_NEED_COPY
;
699 itx
->itx_wr_state
= wr_state
;
700 lr
->lr_foid
= ZVOL_OBJ
;
701 lr
->lr_offset
= offset
;
704 BP_ZERO(&lr
->lr_blkptr
);
706 itx
->itx_private
= zv
;
707 itx
->itx_sync
= sync
;
709 (void) zil_itx_assign(zilog
, itx
, tx
);
716 typedef struct zv_request
{
723 uio_from_bio(uio_t
*uio
, struct bio
*bio
)
725 uio
->uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
726 uio
->uio_skip
= BIO_BI_SKIP(bio
);
727 uio
->uio_resid
= BIO_BI_SIZE(bio
);
728 uio
->uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
729 uio
->uio_loffset
= BIO_BI_SECTOR(bio
) << 9;
730 uio
->uio_limit
= MAXOFFSET_T
;
731 uio
->uio_segflg
= UIO_BVEC
;
735 zvol_write(void *arg
)
739 zv_request_t
*zvr
= arg
;
740 struct bio
*bio
= zvr
->bio
;
742 uio_from_bio(&uio
, bio
);
744 zvol_state_t
*zv
= zvr
->zv
;
745 ASSERT(zv
&& zv
->zv_open_count
> 0);
747 ssize_t start_resid
= uio
.uio_resid
;
748 unsigned long start_jif
= jiffies
;
749 blk_generic_start_io_acct(zv
->zv_queue
, WRITE
, bio_sectors(bio
),
750 &zv
->zv_disk
->part0
);
753 bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
755 uint64_t volsize
= zv
->zv_volsize
;
756 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
757 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
758 uint64_t off
= uio
.uio_loffset
;
759 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
761 if (bytes
> volsize
- off
) /* don't write past the end */
762 bytes
= volsize
- off
;
764 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
766 /* This will only fail for ENOSPC */
767 error
= dmu_tx_assign(tx
, TXG_WAIT
);
772 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
774 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
781 zfs_range_unlock(zvr
->rl
);
783 int64_t nwritten
= start_resid
- uio
.uio_resid
;
784 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
785 task_io_account_write(nwritten
);
788 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
790 rw_exit(&zv
->zv_suspend_lock
);
791 blk_generic_end_io_acct(zv
->zv_queue
, WRITE
, &zv
->zv_disk
->part0
,
793 BIO_END_IO(bio
, -error
);
794 kmem_free(zvr
, sizeof (zv_request_t
));
798 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
801 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
806 zilog_t
*zilog
= zv
->zv_zilog
;
808 if (zil_replaying(zilog
, tx
))
811 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
812 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
813 lr
->lr_foid
= ZVOL_OBJ
;
817 itx
->itx_sync
= sync
;
818 zil_itx_assign(zilog
, itx
, tx
);
822 zvol_discard(void *arg
)
824 zv_request_t
*zvr
= arg
;
825 struct bio
*bio
= zvr
->bio
;
826 zvol_state_t
*zv
= zvr
->zv
;
827 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
828 uint64_t size
= BIO_BI_SIZE(bio
);
829 uint64_t end
= start
+ size
;
833 unsigned long start_jif
;
835 ASSERT(zv
&& zv
->zv_open_count
> 0);
838 blk_generic_start_io_acct(zv
->zv_queue
, WRITE
, bio_sectors(bio
),
839 &zv
->zv_disk
->part0
);
841 sync
= bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
843 if (end
> zv
->zv_volsize
) {
844 error
= SET_ERROR(EIO
);
849 * Align the request to volume block boundaries when a secure erase is
850 * not required. This will prevent dnode_free_range() from zeroing out
851 * the unaligned parts which is slow (read-modify-write) and useless
852 * since we are not freeing any space by doing so.
854 if (!bio_is_secure_erase(bio
)) {
855 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
856 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
863 tx
= dmu_tx_create(zv
->zv_objset
);
864 dmu_tx_mark_netfree(tx
);
865 error
= dmu_tx_assign(tx
, TXG_WAIT
);
869 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
871 error
= dmu_free_long_range(zv
->zv_objset
,
872 ZVOL_OBJ
, start
, size
);
875 zfs_range_unlock(zvr
->rl
);
876 if (error
== 0 && sync
)
877 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
879 rw_exit(&zv
->zv_suspend_lock
);
880 blk_generic_end_io_acct(zv
->zv_queue
, WRITE
, &zv
->zv_disk
->part0
,
882 BIO_END_IO(bio
, -error
);
883 kmem_free(zvr
, sizeof (zv_request_t
));
891 zv_request_t
*zvr
= arg
;
892 struct bio
*bio
= zvr
->bio
;
894 uio_from_bio(&uio
, bio
);
896 zvol_state_t
*zv
= zvr
->zv
;
897 ASSERT(zv
&& zv
->zv_open_count
> 0);
899 ssize_t start_resid
= uio
.uio_resid
;
900 unsigned long start_jif
= jiffies
;
901 blk_generic_start_io_acct(zv
->zv_queue
, READ
, bio_sectors(bio
),
902 &zv
->zv_disk
->part0
);
904 uint64_t volsize
= zv
->zv_volsize
;
905 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
906 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
908 /* don't read past the end */
909 if (bytes
> volsize
- uio
.uio_loffset
)
910 bytes
= volsize
- uio
.uio_loffset
;
912 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
914 /* convert checksum errors into IO errors */
916 error
= SET_ERROR(EIO
);
920 zfs_range_unlock(zvr
->rl
);
922 int64_t nread
= start_resid
- uio
.uio_resid
;
923 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
924 task_io_account_read(nread
);
926 rw_exit(&zv
->zv_suspend_lock
);
927 blk_generic_end_io_acct(zv
->zv_queue
, READ
, &zv
->zv_disk
->part0
,
929 BIO_END_IO(bio
, -error
);
930 kmem_free(zvr
, sizeof (zv_request_t
));
933 static MAKE_REQUEST_FN_RET
934 zvol_request(struct request_queue
*q
, struct bio
*bio
)
936 zvol_state_t
*zv
= q
->queuedata
;
937 fstrans_cookie_t cookie
= spl_fstrans_mark();
938 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
939 uint64_t size
= BIO_BI_SIZE(bio
);
940 int rw
= bio_data_dir(bio
);
943 if (bio_has_data(bio
) && offset
+ size
> zv
->zv_volsize
) {
945 "%s: bad access: offset=%llu, size=%lu\n",
946 zv
->zv_disk
->disk_name
,
947 (long long unsigned)offset
,
948 (long unsigned)size
);
950 BIO_END_IO(bio
, -SET_ERROR(EIO
));
955 boolean_t need_sync
= B_FALSE
;
957 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
958 BIO_END_IO(bio
, -SET_ERROR(EROFS
));
963 * To be released in the I/O function. See the comment on
964 * zfs_range_lock below.
966 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
968 /* bio marked as FLUSH need to flush before write */
969 if (bio_is_flush(bio
))
970 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
972 /* Some requests are just for flush and nothing else. */
974 rw_exit(&zv
->zv_suspend_lock
);
979 zvr
= kmem_alloc(sizeof (zv_request_t
), KM_SLEEP
);
984 * To be released in the I/O function. Since the I/O functions
985 * are asynchronous, we take it here synchronously to make
986 * sure overlapped I/Os are properly ordered.
988 zvr
->rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
991 * Sync writes and discards execute zil_commit() which may need
992 * to take a RL_READER lock on the whole block being modified
993 * via its zillog->zl_get_data(): to avoid circular dependency
994 * issues with taskq threads execute these requests
995 * synchronously here in zvol_request().
997 need_sync
= bio_is_fua(bio
) ||
998 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
999 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
1000 if (zvol_request_sync
|| need_sync
||
1001 taskq_dispatch(zvol_taskq
, zvol_discard
, zvr
,
1002 TQ_SLEEP
) == TASKQID_INVALID
)
1005 if (zvol_request_sync
|| need_sync
||
1006 taskq_dispatch(zvol_taskq
, zvol_write
, zvr
,
1007 TQ_SLEEP
) == TASKQID_INVALID
)
1011 zvr
= kmem_alloc(sizeof (zv_request_t
), KM_SLEEP
);
1015 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1017 zvr
->rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
1019 if (zvol_request_sync
|| taskq_dispatch(zvol_taskq
,
1020 zvol_read
, zvr
, TQ_SLEEP
) == TASKQID_INVALID
)
1025 spl_fstrans_unmark(cookie
);
1026 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
1028 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
1029 return (BLK_QC_T_NONE
);
1034 zvol_get_done(zgd_t
*zgd
, int error
)
1037 dmu_buf_rele(zgd
->zgd_db
, zgd
);
1039 zfs_range_unlock(zgd
->zgd_rl
);
1041 if (error
== 0 && zgd
->zgd_bp
)
1042 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
1044 kmem_free(zgd
, sizeof (zgd_t
));
1048 * Get data to generate a TX_WRITE intent log record.
1051 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
, zio_t
*zio
)
1053 zvol_state_t
*zv
= arg
;
1054 uint64_t offset
= lr
->lr_offset
;
1055 uint64_t size
= lr
->lr_length
;
1060 ASSERT3P(lwb
, !=, NULL
);
1061 ASSERT3P(zio
, !=, NULL
);
1062 ASSERT3U(size
, !=, 0);
1064 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1068 * Write records come in two flavors: immediate and indirect.
1069 * For small writes it's cheaper to store the data with the
1070 * log record (immediate); for large writes it's cheaper to
1071 * sync the data and get a pointer to it (indirect) so that
1072 * we don't have to write the data twice.
1074 if (buf
!= NULL
) { /* immediate write */
1075 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
1077 error
= dmu_read_by_dnode(zv
->zv_dn
, offset
, size
, buf
,
1078 DMU_READ_NO_PREFETCH
);
1079 } else { /* indirect write */
1081 * Have to lock the whole block to ensure when it's written out
1082 * and its checksum is being calculated that no one can change
1083 * the data. Contrarily to zfs_get_data we need not re-check
1084 * blocksize after we get the lock because it cannot be changed.
1086 size
= zv
->zv_volblocksize
;
1087 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
1088 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
1090 error
= dmu_buf_hold_by_dnode(zv
->zv_dn
, offset
, zgd
, &db
,
1091 DMU_READ_NO_PREFETCH
);
1093 blkptr_t
*bp
= &lr
->lr_blkptr
;
1099 ASSERT(db
->db_offset
== offset
);
1100 ASSERT(db
->db_size
== size
);
1102 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1103 zvol_get_done
, zgd
);
1110 zvol_get_done(zgd
, error
);
1112 return (SET_ERROR(error
));
1116 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
1119 zvol_insert(zvol_state_t
*zv
)
1121 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
1122 ASSERT3U(MINOR(zv
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
1123 list_insert_head(&zvol_state_list
, zv
);
1124 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1128 * Simply remove the zvol from to list of zvols.
1131 zvol_remove(zvol_state_t
*zv
)
1133 ASSERT(RW_WRITE_HELD(&zvol_state_lock
));
1134 list_remove(&zvol_state_list
, zv
);
1135 hlist_del(&zv
->zv_hlink
);
1139 * Setup zv after we just own the zv->objset
1142 zvol_setup_zv(zvol_state_t
*zv
)
1147 objset_t
*os
= zv
->zv_objset
;
1149 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1150 ASSERT(RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1152 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
1154 return (SET_ERROR(error
));
1156 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1158 return (SET_ERROR(error
));
1160 error
= dnode_hold(os
, ZVOL_OBJ
, FTAG
, &zv
->zv_dn
);
1162 return (SET_ERROR(error
));
1164 set_capacity(zv
->zv_disk
, volsize
>> 9);
1165 zv
->zv_volsize
= volsize
;
1166 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
1168 if (ro
|| dmu_objset_is_snapshot(os
) ||
1169 !spa_writeable(dmu_objset_spa(os
))) {
1170 set_disk_ro(zv
->zv_disk
, 1);
1171 zv
->zv_flags
|= ZVOL_RDONLY
;
1173 set_disk_ro(zv
->zv_disk
, 0);
1174 zv
->zv_flags
&= ~ZVOL_RDONLY
;
1180 * Shutdown every zv_objset related stuff except zv_objset itself.
1181 * The is the reverse of zvol_setup_zv.
1184 zvol_shutdown_zv(zvol_state_t
*zv
)
1186 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
) &&
1187 RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1189 zil_close(zv
->zv_zilog
);
1190 zv
->zv_zilog
= NULL
;
1192 dnode_rele(zv
->zv_dn
, FTAG
);
1196 * Evict cached data. We must write out any dirty data before
1197 * disowning the dataset.
1199 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
1200 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1201 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
1205 * return the proper tag for rollback and recv
1208 zvol_tag(zvol_state_t
*zv
)
1210 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
1211 return (zv
->zv_open_count
> 0 ? zv
: NULL
);
1215 * Suspend the zvol for recv and rollback.
1218 zvol_suspend(const char *name
)
1222 zv
= zvol_find_by_name(name
, RW_WRITER
);
1227 /* block all I/O, release in zvol_resume. */
1228 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1229 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
1231 atomic_inc(&zv
->zv_suspend_ref
);
1233 if (zv
->zv_open_count
> 0)
1234 zvol_shutdown_zv(zv
);
1237 * do not hold zv_state_lock across suspend/resume to
1238 * avoid locking up zvol lookups
1240 mutex_exit(&zv
->zv_state_lock
);
1242 /* zv_suspend_lock is released in zvol_resume() */
1247 zvol_resume(zvol_state_t
*zv
)
1251 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
1253 mutex_enter(&zv
->zv_state_lock
);
1255 if (zv
->zv_open_count
> 0) {
1256 VERIFY0(dmu_objset_hold(zv
->zv_name
, zv
, &zv
->zv_objset
));
1257 VERIFY3P(zv
->zv_objset
->os_dsl_dataset
->ds_owner
, ==, zv
);
1258 VERIFY(dsl_dataset_long_held(zv
->zv_objset
->os_dsl_dataset
));
1259 dmu_objset_rele(zv
->zv_objset
, zv
);
1261 error
= zvol_setup_zv(zv
);
1264 mutex_exit(&zv
->zv_state_lock
);
1266 rw_exit(&zv
->zv_suspend_lock
);
1268 * We need this because we don't hold zvol_state_lock while releasing
1269 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
1270 * zv_suspend_lock to determine it is safe to free because rwlock is
1271 * not inherent atomic.
1273 atomic_dec(&zv
->zv_suspend_ref
);
1275 return (SET_ERROR(error
));
1279 zvol_first_open(zvol_state_t
*zv
, boolean_t readonly
)
1282 int error
, locked
= 0;
1285 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
1286 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1289 * In all other cases the spa_namespace_lock is taken before the
1290 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
1291 * function calls fops->open() with the bdev->bd_mutex lock held.
1292 * This deadlock can be easily observed with zvols used as vdevs.
1294 * To avoid a potential lock inversion deadlock we preemptively
1295 * try to take the spa_namespace_lock(). Normally it will not
1296 * be contended and this is safe because spa_open_common() handles
1297 * the case where the caller already holds the spa_namespace_lock.
1299 * When it is contended we risk a lock inversion if we were to
1300 * block waiting for the lock. Luckily, the __blkdev_get()
1301 * function allows us to return -ERESTARTSYS which will result in
1302 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
1303 * called again. This process can be repeated safely until both
1304 * locks are acquired.
1306 if (!mutex_owned(&spa_namespace_lock
)) {
1307 locked
= mutex_tryenter(&spa_namespace_lock
);
1309 return (-SET_ERROR(ERESTARTSYS
));
1312 ro
= (readonly
|| (strchr(zv
->zv_name
, '@') != NULL
));
1313 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, ro
, B_TRUE
, zv
, &os
);
1319 error
= zvol_setup_zv(zv
);
1322 dmu_objset_disown(os
, 1, zv
);
1323 zv
->zv_objset
= NULL
;
1328 mutex_exit(&spa_namespace_lock
);
1329 return (SET_ERROR(-error
));
1333 zvol_last_close(zvol_state_t
*zv
)
1335 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
1336 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1338 zvol_shutdown_zv(zv
);
1340 dmu_objset_disown(zv
->zv_objset
, 1, zv
);
1341 zv
->zv_objset
= NULL
;
1345 zvol_open(struct block_device
*bdev
, fmode_t flag
)
1349 boolean_t drop_suspend
= B_TRUE
;
1351 rw_enter(&zvol_state_lock
, RW_READER
);
1353 * Obtain a copy of private_data under the zvol_state_lock to make
1354 * sure that either the result of zvol free code path setting
1355 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1356 * is not called on this zv because of the positive zv_open_count.
1358 zv
= bdev
->bd_disk
->private_data
;
1360 rw_exit(&zvol_state_lock
);
1361 return (SET_ERROR(-ENXIO
));
1364 mutex_enter(&zv
->zv_state_lock
);
1366 * make sure zvol is not suspended during first open
1367 * (hold zv_suspend_lock) and respect proper lock acquisition
1368 * ordering - zv_suspend_lock before zv_state_lock
1370 if (zv
->zv_open_count
== 0) {
1371 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
1372 mutex_exit(&zv
->zv_state_lock
);
1373 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1374 mutex_enter(&zv
->zv_state_lock
);
1375 /* check to see if zv_suspend_lock is needed */
1376 if (zv
->zv_open_count
!= 0) {
1377 rw_exit(&zv
->zv_suspend_lock
);
1378 drop_suspend
= B_FALSE
;
1382 drop_suspend
= B_FALSE
;
1384 rw_exit(&zvol_state_lock
);
1386 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1387 ASSERT(zv
->zv_open_count
!= 0 || RW_READ_HELD(&zv
->zv_suspend_lock
));
1389 if (zv
->zv_open_count
== 0) {
1390 error
= zvol_first_open(zv
, !(flag
& FMODE_WRITE
));
1395 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1397 goto out_open_count
;
1400 zv
->zv_open_count
++;
1402 mutex_exit(&zv
->zv_state_lock
);
1404 rw_exit(&zv
->zv_suspend_lock
);
1406 check_disk_change(bdev
);
1411 if (zv
->zv_open_count
== 0)
1412 zvol_last_close(zv
);
1415 mutex_exit(&zv
->zv_state_lock
);
1417 rw_exit(&zv
->zv_suspend_lock
);
1418 if (error
== -ERESTARTSYS
)
1421 return (SET_ERROR(error
));
1424 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1429 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1432 boolean_t drop_suspend
= B_TRUE
;
1434 rw_enter(&zvol_state_lock
, RW_READER
);
1435 zv
= disk
->private_data
;
1437 mutex_enter(&zv
->zv_state_lock
);
1438 ASSERT(zv
->zv_open_count
> 0);
1440 * make sure zvol is not suspended during last close
1441 * (hold zv_suspend_lock) and respect proper lock acquisition
1442 * ordering - zv_suspend_lock before zv_state_lock
1444 if (zv
->zv_open_count
== 1) {
1445 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
1446 mutex_exit(&zv
->zv_state_lock
);
1447 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1448 mutex_enter(&zv
->zv_state_lock
);
1449 /* check to see if zv_suspend_lock is needed */
1450 if (zv
->zv_open_count
!= 1) {
1451 rw_exit(&zv
->zv_suspend_lock
);
1452 drop_suspend
= B_FALSE
;
1456 drop_suspend
= B_FALSE
;
1458 rw_exit(&zvol_state_lock
);
1460 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1461 ASSERT(zv
->zv_open_count
!= 1 || RW_READ_HELD(&zv
->zv_suspend_lock
));
1463 zv
->zv_open_count
--;
1464 if (zv
->zv_open_count
== 0)
1465 zvol_last_close(zv
);
1467 mutex_exit(&zv
->zv_state_lock
);
1470 rw_exit(&zv
->zv_suspend_lock
);
1472 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1478 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1479 unsigned int cmd
, unsigned long arg
)
1481 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1484 ASSERT3U(zv
->zv_open_count
, >, 0);
1489 invalidate_bdev(bdev
);
1490 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1492 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
1493 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1495 rw_exit(&zv
->zv_suspend_lock
);
1499 mutex_enter(&zv
->zv_state_lock
);
1500 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1501 mutex_exit(&zv
->zv_state_lock
);
1509 return (SET_ERROR(error
));
1512 #ifdef CONFIG_COMPAT
1514 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1515 unsigned cmd
, unsigned long arg
)
1517 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1520 #define zvol_compat_ioctl NULL
1524 * Linux 2.6.38 preferred interface.
1526 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_CHECK_EVENTS
1528 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
1530 unsigned int mask
= 0;
1532 rw_enter(&zvol_state_lock
, RW_READER
);
1534 zvol_state_t
*zv
= disk
->private_data
;
1536 mutex_enter(&zv
->zv_state_lock
);
1537 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
1539 mutex_exit(&zv
->zv_state_lock
);
1542 rw_exit(&zvol_state_lock
);
1547 static int zvol_media_changed(struct gendisk
*disk
)
1551 rw_enter(&zvol_state_lock
, RW_READER
);
1553 zvol_state_t
*zv
= disk
->private_data
;
1555 mutex_enter(&zv
->zv_state_lock
);
1556 changed
= zv
->zv_changed
;
1558 mutex_exit(&zv
->zv_state_lock
);
1561 rw_exit(&zvol_state_lock
);
1567 static int zvol_revalidate_disk(struct gendisk
*disk
)
1569 rw_enter(&zvol_state_lock
, RW_READER
);
1571 zvol_state_t
*zv
= disk
->private_data
;
1573 mutex_enter(&zv
->zv_state_lock
);
1574 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> SECTOR_BITS
);
1575 mutex_exit(&zv
->zv_state_lock
);
1578 rw_exit(&zvol_state_lock
);
1584 * Provide a simple virtual geometry for legacy compatibility. For devices
1585 * smaller than 1 MiB a small head and sector count is used to allow very
1586 * tiny devices. For devices over 1 Mib a standard head and sector count
1587 * is used to keep the cylinders count reasonable.
1590 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1592 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1595 ASSERT3U(zv
->zv_open_count
, >, 0);
1597 sectors
= get_capacity(zv
->zv_disk
);
1599 if (sectors
> 2048) {
1608 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1613 static struct kobject
*
1614 zvol_probe(dev_t dev
, int *part
, void *arg
)
1617 struct kobject
*kobj
;
1619 zv
= zvol_find_by_dev(dev
);
1620 kobj
= zv
? get_disk_and_module(zv
->zv_disk
) : NULL
;
1621 ASSERT(zv
== NULL
|| MUTEX_HELD(&zv
->zv_state_lock
));
1623 mutex_exit(&zv
->zv_state_lock
);
1628 static struct block_device_operations zvol_ops
= {
1630 .release
= zvol_release
,
1631 .ioctl
= zvol_ioctl
,
1632 .compat_ioctl
= zvol_compat_ioctl
,
1633 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_CHECK_EVENTS
1634 .check_events
= zvol_check_events
,
1636 .media_changed
= zvol_media_changed
,
1638 .revalidate_disk
= zvol_revalidate_disk
,
1639 .getgeo
= zvol_getgeo
,
1640 .owner
= THIS_MODULE
,
1644 * Allocate memory for a new zvol_state_t and setup the required
1645 * request queue and generic disk structures for the block device.
1647 static zvol_state_t
*
1648 zvol_alloc(dev_t dev
, const char *name
)
1653 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
1656 if (volmode
== ZFS_VOLMODE_DEFAULT
)
1657 volmode
= zvol_volmode
;
1659 if (volmode
== ZFS_VOLMODE_NONE
)
1662 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1664 list_link_init(&zv
->zv_next
);
1666 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1668 zv
->zv_queue
= blk_alloc_queue(GFP_ATOMIC
);
1669 if (zv
->zv_queue
== NULL
)
1672 blk_queue_make_request(zv
->zv_queue
, zvol_request
);
1673 blk_queue_set_write_cache(zv
->zv_queue
, B_TRUE
, B_TRUE
);
1675 /* Limit read-ahead to a single page to prevent over-prefetching. */
1676 blk_queue_set_read_ahead(zv
->zv_queue
, 1);
1678 /* Disable write merging in favor of the ZIO pipeline. */
1679 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zv
->zv_queue
);
1681 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1682 if (zv
->zv_disk
== NULL
)
1685 zv
->zv_queue
->queuedata
= zv
;
1687 zv
->zv_open_count
= 0;
1688 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1690 zfs_rlock_init(&zv
->zv_range_lock
);
1691 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
1693 zv
->zv_disk
->major
= zvol_major
;
1694 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_CHECK_EVENTS
1695 zv
->zv_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
1698 if (volmode
== ZFS_VOLMODE_DEV
) {
1700 * ZFS_VOLMODE_DEV disable partitioning on ZVOL devices: set
1701 * gendisk->minors = 1 as noted in include/linux/genhd.h.
1702 * Also disable extended partition numbers (GENHD_FL_EXT_DEVT)
1703 * and suppresses partition scanning (GENHD_FL_NO_PART_SCAN)
1704 * setting gendisk->flags accordingly.
1706 zv
->zv_disk
->minors
= 1;
1707 #if defined(GENHD_FL_EXT_DEVT)
1708 zv
->zv_disk
->flags
&= ~GENHD_FL_EXT_DEVT
;
1710 #if defined(GENHD_FL_NO_PART_SCAN)
1711 zv
->zv_disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
1714 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1715 zv
->zv_disk
->fops
= &zvol_ops
;
1716 zv
->zv_disk
->private_data
= zv
;
1717 zv
->zv_disk
->queue
= zv
->zv_queue
;
1718 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1719 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1724 blk_cleanup_queue(zv
->zv_queue
);
1726 kmem_free(zv
, sizeof (zvol_state_t
));
1732 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1733 * At this time, the structure is not opened by anyone, is taken off
1734 * the zvol_state_list, and has its private data set to NULL.
1735 * The zvol_state_lock is dropped.
1738 zvol_free(void *arg
)
1740 zvol_state_t
*zv
= arg
;
1742 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1743 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
1744 ASSERT(zv
->zv_open_count
== 0);
1745 ASSERT(zv
->zv_disk
->private_data
== NULL
);
1747 rw_destroy(&zv
->zv_suspend_lock
);
1748 zfs_rlock_destroy(&zv
->zv_range_lock
);
1750 del_gendisk(zv
->zv_disk
);
1751 blk_cleanup_queue(zv
->zv_queue
);
1752 put_disk(zv
->zv_disk
);
1754 ida_simple_remove(&zvol_ida
, MINOR(zv
->zv_dev
) >> ZVOL_MINOR_BITS
);
1756 mutex_destroy(&zv
->zv_state_lock
);
1757 dataset_kstats_destroy(&zv
->zv_kstat
);
1759 kmem_free(zv
, sizeof (zvol_state_t
));
1763 * Create a block device minor node and setup the linkage between it
1764 * and the specified volume. Once this function returns the block
1765 * device is live and ready for use.
1768 zvol_create_minor_impl(const char *name
)
1772 dmu_object_info_t
*doi
;
1778 uint64_t hash
= zvol_name_hash(name
);
1780 if (zvol_inhibit_dev
)
1783 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1785 return (SET_ERROR(-idx
));
1786 minor
= idx
<< ZVOL_MINOR_BITS
;
1788 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
1790 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1791 mutex_exit(&zv
->zv_state_lock
);
1792 ida_simple_remove(&zvol_ida
, idx
);
1793 return (SET_ERROR(EEXIST
));
1796 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1798 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
1802 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1804 goto out_dmu_objset_disown
;
1806 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1808 goto out_dmu_objset_disown
;
1810 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1812 error
= SET_ERROR(EAGAIN
);
1813 goto out_dmu_objset_disown
;
1817 if (dmu_objset_is_snapshot(os
))
1818 zv
->zv_flags
|= ZVOL_RDONLY
;
1820 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1821 zv
->zv_volsize
= volsize
;
1824 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1826 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1827 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1828 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1829 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1830 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1831 blk_queue_max_discard_sectors(zv
->zv_queue
,
1832 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1833 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1834 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1835 #ifdef QUEUE_FLAG_NONROT
1836 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1838 #ifdef QUEUE_FLAG_ADD_RANDOM
1839 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_queue
);
1842 if (spa_writeable(dmu_objset_spa(os
))) {
1843 if (zil_replay_disable
)
1844 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1846 zil_replay(os
, zv
, zvol_replay_vector
);
1848 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
1849 dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
1852 * When udev detects the addition of the device it will immediately
1853 * invoke blkid(8) to determine the type of content on the device.
1854 * Prefetching the blocks commonly scanned by blkid(8) will speed
1857 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1859 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1860 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1861 ZIO_PRIORITY_SYNC_READ
);
1864 zv
->zv_objset
= NULL
;
1865 out_dmu_objset_disown
:
1866 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1868 kmem_free(doi
, sizeof (dmu_object_info_t
));
1871 rw_enter(&zvol_state_lock
, RW_WRITER
);
1873 rw_exit(&zvol_state_lock
);
1874 add_disk(zv
->zv_disk
);
1876 ida_simple_remove(&zvol_ida
, idx
);
1879 return (SET_ERROR(error
));
1883 * Rename a block device minor mode for the specified volume.
1886 zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1888 int readonly
= get_disk_ro(zv
->zv_disk
);
1890 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1891 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1893 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1895 /* move to new hashtable entry */
1896 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1897 hlist_del(&zv
->zv_hlink
);
1898 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1901 * The block device's read-only state is briefly changed causing
1902 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1903 * the name change and fixes the symlinks. This does not change
1904 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1905 * changes. This would normally be done using kobject_uevent() but
1906 * that is a GPL-only symbol which is why we need this workaround.
1908 set_disk_ro(zv
->zv_disk
, !readonly
);
1909 set_disk_ro(zv
->zv_disk
, readonly
);
1912 typedef struct minors_job
{
1922 * Prefetch zvol dnodes for the minors_job
1925 zvol_prefetch_minors_impl(void *arg
)
1927 minors_job_t
*job
= arg
;
1928 char *dsname
= job
->name
;
1929 objset_t
*os
= NULL
;
1931 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
,
1933 if (job
->error
== 0) {
1934 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, 0, ZIO_PRIORITY_SYNC_READ
);
1935 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1940 * Mask errors to continue dmu_objset_find() traversal
1943 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
1945 minors_job_t
*j
= arg
;
1946 list_t
*minors_list
= j
->list
;
1947 const char *name
= j
->name
;
1949 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1951 /* skip the designated dataset */
1952 if (name
&& strcmp(dsname
, name
) == 0)
1955 /* at this point, the dsname should name a snapshot */
1956 if (strchr(dsname
, '@') == 0) {
1957 dprintf("zvol_create_snap_minor_cb(): "
1958 "%s is not a shapshot name\n", dsname
);
1961 char *n
= strdup(dsname
);
1965 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1967 job
->list
= minors_list
;
1969 list_insert_tail(minors_list
, job
);
1970 /* don't care if dispatch fails, because job->error is 0 */
1971 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1979 * Mask errors to continue dmu_objset_find() traversal
1982 zvol_create_minors_cb(const char *dsname
, void *arg
)
1986 list_t
*minors_list
= arg
;
1988 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1990 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1995 * Given the name and the 'snapdev' property, create device minor nodes
1996 * with the linkages to zvols/snapshots as needed.
1997 * If the name represents a zvol, create a minor node for the zvol, then
1998 * check if its snapshots are 'visible', and if so, iterate over the
1999 * snapshots and create device minor nodes for those.
2001 if (strchr(dsname
, '@') == 0) {
2003 char *n
= strdup(dsname
);
2007 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
2009 job
->list
= minors_list
;
2011 list_insert_tail(minors_list
, job
);
2012 /* don't care if dispatch fails, because job->error is 0 */
2013 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
2016 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
2018 * traverse snapshots only, do not traverse children,
2019 * and skip the 'dsname'
2021 error
= dmu_objset_find((char *)dsname
,
2022 zvol_create_snap_minor_cb
, (void *)job
,
2026 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
2034 * Create minors for the specified dataset, including children and snapshots.
2035 * Pay attention to the 'snapdev' property and iterate over the snapshots
2036 * only if they are 'visible'. This approach allows one to assure that the
2037 * snapshot metadata is read from disk only if it is needed.
2039 * The name can represent a dataset to be recursively scanned for zvols and
2040 * their snapshots, or a single zvol snapshot. If the name represents a
2041 * dataset, the scan is performed in two nested stages:
2042 * - scan the dataset for zvols, and
2043 * - for each zvol, create a minor node, then check if the zvol's snapshots
2044 * are 'visible', and only then iterate over the snapshots if needed
2046 * If the name represents a snapshot, a check is performed if the snapshot is
2047 * 'visible' (which also verifies that the parent is a zvol), and if so,
2048 * a minor node for that snapshot is created.
2051 zvol_create_minors_impl(const char *name
)
2054 fstrans_cookie_t cookie
;
2059 if (zvol_inhibit_dev
)
2063 * This is the list for prefetch jobs. Whenever we found a match
2064 * during dmu_objset_find, we insert a minors_job to the list and do
2065 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
2066 * any lock because all list operation is done on the current thread.
2068 * We will use this list to do zvol_create_minor_impl after prefetch
2069 * so we don't have to traverse using dmu_objset_find again.
2071 list_create(&minors_list
, sizeof (minors_job_t
),
2072 offsetof(minors_job_t
, link
));
2074 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
2075 (void) strlcpy(parent
, name
, MAXPATHLEN
);
2077 if ((atp
= strrchr(parent
, '@')) != NULL
) {
2081 error
= dsl_prop_get_integer(parent
, "snapdev",
2084 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
2085 error
= zvol_create_minor_impl(name
);
2087 cookie
= spl_fstrans_mark();
2088 error
= dmu_objset_find(parent
, zvol_create_minors_cb
,
2089 &minors_list
, DS_FIND_CHILDREN
);
2090 spl_fstrans_unmark(cookie
);
2093 kmem_free(parent
, MAXPATHLEN
);
2094 taskq_wait_outstanding(system_taskq
, 0);
2097 * Prefetch is completed, we can do zvol_create_minor_impl
2100 while ((job
= list_head(&minors_list
)) != NULL
) {
2101 list_remove(&minors_list
, job
);
2103 zvol_create_minor_impl(job
->name
);
2105 kmem_free(job
, sizeof (minors_job_t
));
2108 list_destroy(&minors_list
);
2110 return (SET_ERROR(error
));
2114 * Remove minors for specified dataset including children and snapshots.
2117 zvol_remove_minors_impl(const char *name
)
2119 zvol_state_t
*zv
, *zv_next
;
2120 int namelen
= ((name
) ? strlen(name
) : 0);
2121 taskqid_t t
, tid
= TASKQID_INVALID
;
2124 if (zvol_inhibit_dev
)
2127 list_create(&free_list
, sizeof (zvol_state_t
),
2128 offsetof(zvol_state_t
, zv_next
));
2130 rw_enter(&zvol_state_lock
, RW_WRITER
);
2132 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
2133 zv_next
= list_next(&zvol_state_list
, zv
);
2135 mutex_enter(&zv
->zv_state_lock
);
2136 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
2137 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
2138 (zv
->zv_name
[namelen
] == '/' ||
2139 zv
->zv_name
[namelen
] == '@'))) {
2141 * By holding zv_state_lock here, we guarantee that no
2142 * one is currently using this zv
2145 /* If in use, leave alone */
2146 if (zv
->zv_open_count
> 0 ||
2147 atomic_read(&zv
->zv_suspend_ref
)) {
2148 mutex_exit(&zv
->zv_state_lock
);
2155 * Cleared while holding zvol_state_lock as a writer
2156 * which will prevent zvol_open() from opening it.
2158 zv
->zv_disk
->private_data
= NULL
;
2160 /* Drop zv_state_lock before zvol_free() */
2161 mutex_exit(&zv
->zv_state_lock
);
2163 /* Try parallel zv_free, if failed do it in place */
2164 t
= taskq_dispatch(system_taskq
, zvol_free
, zv
,
2166 if (t
== TASKQID_INVALID
)
2167 list_insert_head(&free_list
, zv
);
2171 mutex_exit(&zv
->zv_state_lock
);
2174 rw_exit(&zvol_state_lock
);
2176 /* Drop zvol_state_lock before calling zvol_free() */
2177 while ((zv
= list_head(&free_list
)) != NULL
) {
2178 list_remove(&free_list
, zv
);
2182 if (tid
!= TASKQID_INVALID
)
2183 taskq_wait_outstanding(system_taskq
, tid
);
2186 /* Remove minor for this specific volume only */
2188 zvol_remove_minor_impl(const char *name
)
2190 zvol_state_t
*zv
= NULL
, *zv_next
;
2192 if (zvol_inhibit_dev
)
2195 rw_enter(&zvol_state_lock
, RW_WRITER
);
2197 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
2198 zv_next
= list_next(&zvol_state_list
, zv
);
2200 mutex_enter(&zv
->zv_state_lock
);
2201 if (strcmp(zv
->zv_name
, name
) == 0) {
2203 * By holding zv_state_lock here, we guarantee that no
2204 * one is currently using this zv
2207 /* If in use, leave alone */
2208 if (zv
->zv_open_count
> 0 ||
2209 atomic_read(&zv
->zv_suspend_ref
)) {
2210 mutex_exit(&zv
->zv_state_lock
);
2216 * Cleared while holding zvol_state_lock as a writer
2217 * which will prevent zvol_open() from opening it.
2219 zv
->zv_disk
->private_data
= NULL
;
2221 mutex_exit(&zv
->zv_state_lock
);
2224 mutex_exit(&zv
->zv_state_lock
);
2228 /* Drop zvol_state_lock before calling zvol_free() */
2229 rw_exit(&zvol_state_lock
);
2236 * Rename minors for specified dataset including children and snapshots.
2239 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
2241 zvol_state_t
*zv
, *zv_next
;
2242 int oldnamelen
, newnamelen
;
2244 if (zvol_inhibit_dev
)
2247 oldnamelen
= strlen(oldname
);
2248 newnamelen
= strlen(newname
);
2250 rw_enter(&zvol_state_lock
, RW_READER
);
2252 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
2253 zv_next
= list_next(&zvol_state_list
, zv
);
2255 mutex_enter(&zv
->zv_state_lock
);
2257 /* If in use, leave alone */
2258 if (zv
->zv_open_count
> 0) {
2259 mutex_exit(&zv
->zv_state_lock
);
2263 if (strcmp(zv
->zv_name
, oldname
) == 0) {
2264 zvol_rename_minor(zv
, newname
);
2265 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
2266 (zv
->zv_name
[oldnamelen
] == '/' ||
2267 zv
->zv_name
[oldnamelen
] == '@')) {
2268 char *name
= kmem_asprintf("%s%c%s", newname
,
2269 zv
->zv_name
[oldnamelen
],
2270 zv
->zv_name
+ oldnamelen
+ 1);
2271 zvol_rename_minor(zv
, name
);
2272 kmem_free(name
, strlen(name
+ 1));
2275 mutex_exit(&zv
->zv_state_lock
);
2278 rw_exit(&zvol_state_lock
);
2281 typedef struct zvol_snapdev_cb_arg
{
2283 } zvol_snapdev_cb_arg_t
;
2286 zvol_set_snapdev_cb(const char *dsname
, void *param
)
2288 zvol_snapdev_cb_arg_t
*arg
= param
;
2290 if (strchr(dsname
, '@') == NULL
)
2293 switch (arg
->snapdev
) {
2294 case ZFS_SNAPDEV_VISIBLE
:
2295 (void) zvol_create_minor_impl(dsname
);
2297 case ZFS_SNAPDEV_HIDDEN
:
2298 (void) zvol_remove_minor_impl(dsname
);
2306 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
2308 zvol_snapdev_cb_arg_t arg
= {snapdev
};
2309 fstrans_cookie_t cookie
= spl_fstrans_mark();
2311 * The zvol_set_snapdev_sync() sets snapdev appropriately
2312 * in the dataset hierarchy. Here, we only scan snapshots.
2314 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
2315 spl_fstrans_unmark(cookie
);
2318 typedef struct zvol_volmode_cb_arg
{
2320 } zvol_volmode_cb_arg_t
;
2323 zvol_set_volmode_impl(char *name
, uint64_t volmode
)
2325 fstrans_cookie_t cookie
= spl_fstrans_mark();
2327 if (strchr(name
, '@') != NULL
)
2331 * It's unfortunate we need to remove minors before we create new ones:
2332 * this is necessary because our backing gendisk (zvol_state->zv_disk)
2333 * coule be different when we set, for instance, volmode from "geom"
2334 * to "dev" (or vice versa).
2335 * A possible optimization is to modify our consumers so we don't get
2336 * called when "volmode" does not change.
2339 case ZFS_VOLMODE_NONE
:
2340 (void) zvol_remove_minor_impl(name
);
2342 case ZFS_VOLMODE_GEOM
:
2343 case ZFS_VOLMODE_DEV
:
2344 (void) zvol_remove_minor_impl(name
);
2345 (void) zvol_create_minor_impl(name
);
2347 case ZFS_VOLMODE_DEFAULT
:
2348 (void) zvol_remove_minor_impl(name
);
2349 if (zvol_volmode
== ZFS_VOLMODE_NONE
)
2351 else /* if zvol_volmode is invalid defaults to "geom" */
2352 (void) zvol_create_minor_impl(name
);
2356 spl_fstrans_unmark(cookie
);
2359 static zvol_task_t
*
2360 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
2366 /* Never allow tasks on hidden names. */
2367 if (name1
[0] == '$')
2370 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
2372 task
->value
= value
;
2373 delim
= strchr(name1
, '/');
2374 strlcpy(task
->pool
, name1
, delim
? (delim
- name1
+ 1) : MAXNAMELEN
);
2376 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
2378 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
2384 zvol_task_free(zvol_task_t
*task
)
2386 kmem_free(task
, sizeof (zvol_task_t
));
2390 * The worker thread function performed asynchronously.
2393 zvol_task_cb(void *param
)
2395 zvol_task_t
*task
= (zvol_task_t
*)param
;
2398 case ZVOL_ASYNC_CREATE_MINORS
:
2399 (void) zvol_create_minors_impl(task
->name1
);
2401 case ZVOL_ASYNC_REMOVE_MINORS
:
2402 zvol_remove_minors_impl(task
->name1
);
2404 case ZVOL_ASYNC_RENAME_MINORS
:
2405 zvol_rename_minors_impl(task
->name1
, task
->name2
);
2407 case ZVOL_ASYNC_SET_SNAPDEV
:
2408 zvol_set_snapdev_impl(task
->name1
, task
->value
);
2410 case ZVOL_ASYNC_SET_VOLMODE
:
2411 zvol_set_volmode_impl(task
->name1
, task
->value
);
2418 zvol_task_free(task
);
2421 typedef struct zvol_set_prop_int_arg
{
2422 const char *zsda_name
;
2423 uint64_t zsda_value
;
2424 zprop_source_t zsda_source
;
2426 } zvol_set_prop_int_arg_t
;
2429 * Sanity check the dataset for safe use by the sync task. No additional
2430 * conditions are imposed.
2433 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
2435 zvol_set_prop_int_arg_t
*zsda
= arg
;
2436 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2440 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
2444 dsl_dir_rele(dd
, FTAG
);
2451 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2453 char dsname
[MAXNAMELEN
];
2457 dsl_dataset_name(ds
, dsname
);
2458 if (dsl_prop_get_int_ds(ds
, "snapdev", &snapdev
) != 0)
2460 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
, NULL
, snapdev
);
2464 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
2470 * Traverse all child datasets and apply snapdev appropriately.
2471 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
2472 * dataset and read the effective "snapdev" on every child in the callback
2473 * function: this is because the value is not guaranteed to be the same in the
2474 * whole dataset hierarchy.
2477 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
2479 zvol_set_prop_int_arg_t
*zsda
= arg
;
2480 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2485 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
2488 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
2490 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
2491 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
2492 &zsda
->zsda_value
, zsda
->zsda_tx
);
2493 dsl_dataset_rele(ds
, FTAG
);
2495 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
2496 zsda
, DS_FIND_CHILDREN
);
2498 dsl_dir_rele(dd
, FTAG
);
2502 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
2504 zvol_set_prop_int_arg_t zsda
;
2506 zsda
.zsda_name
= ddname
;
2507 zsda
.zsda_source
= source
;
2508 zsda
.zsda_value
= snapdev
;
2510 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
2511 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
2515 * Sanity check the dataset for safe use by the sync task. No additional
2516 * conditions are imposed.
2519 zvol_set_volmode_check(void *arg
, dmu_tx_t
*tx
)
2521 zvol_set_prop_int_arg_t
*zsda
= arg
;
2522 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2526 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
2530 dsl_dir_rele(dd
, FTAG
);
2537 zvol_set_volmode_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2539 char dsname
[MAXNAMELEN
];
2543 dsl_dataset_name(ds
, dsname
);
2544 if (dsl_prop_get_int_ds(ds
, "volmode", &volmode
) != 0)
2546 task
= zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE
, dsname
, NULL
, volmode
);
2550 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
2556 * Traverse all child datasets and apply volmode appropriately.
2557 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
2558 * dataset and read the effective "volmode" on every child in the callback
2559 * function: this is because the value is not guaranteed to be the same in the
2560 * whole dataset hierarchy.
2563 zvol_set_volmode_sync(void *arg
, dmu_tx_t
*tx
)
2565 zvol_set_prop_int_arg_t
*zsda
= arg
;
2566 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2571 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
2574 error
= dsl_dataset_hold(dp
, zsda
->zsda_name
, FTAG
, &ds
);
2576 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_VOLMODE
),
2577 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
2578 &zsda
->zsda_value
, zsda
->zsda_tx
);
2579 dsl_dataset_rele(ds
, FTAG
);
2582 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_volmode_sync_cb
,
2583 zsda
, DS_FIND_CHILDREN
);
2585 dsl_dir_rele(dd
, FTAG
);
2589 zvol_set_volmode(const char *ddname
, zprop_source_t source
, uint64_t volmode
)
2591 zvol_set_prop_int_arg_t zsda
;
2593 zsda
.zsda_name
= ddname
;
2594 zsda
.zsda_source
= source
;
2595 zsda
.zsda_value
= volmode
;
2597 return (dsl_sync_task(ddname
, zvol_set_volmode_check
,
2598 zvol_set_volmode_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
2602 zvol_create_minors(spa_t
*spa
, const char *name
, boolean_t async
)
2607 task
= zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS
, name
, NULL
, ~0ULL);
2611 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2612 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2613 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2617 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
2622 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
2626 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2627 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2628 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2632 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
2638 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
2642 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2643 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2644 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2650 int threads
= MIN(MAX(zvol_threads
, 1), 1024);
2653 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
2654 offsetof(zvol_state_t
, zv_next
));
2655 rw_init(&zvol_state_lock
, NULL
, RW_DEFAULT
, NULL
);
2656 ida_init(&zvol_ida
);
2658 zvol_taskq
= taskq_create(ZVOL_DRIVER
, threads
, maxclsyspri
,
2659 threads
* 2, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
2660 if (zvol_taskq
== NULL
) {
2661 printk(KERN_INFO
"ZFS: taskq_create() failed\n");
2666 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
2672 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
2673 INIT_HLIST_HEAD(&zvol_htable
[i
]);
2675 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
2677 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
2681 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
2682 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
2687 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2689 taskq_destroy(zvol_taskq
);
2691 ida_destroy(&zvol_ida
);
2692 rw_destroy(&zvol_state_lock
);
2693 list_destroy(&zvol_state_list
);
2695 return (SET_ERROR(error
));
2701 zvol_remove_minors_impl(NULL
);
2703 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
2704 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
2705 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2707 taskq_destroy(zvol_taskq
);
2708 list_destroy(&zvol_state_list
);
2709 rw_destroy(&zvol_state_lock
);
2711 ida_destroy(&zvol_ida
);
2715 module_param(zvol_inhibit_dev
, uint
, 0644);
2716 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
2718 module_param(zvol_major
, uint
, 0444);
2719 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
2721 module_param(zvol_threads
, uint
, 0444);
2722 MODULE_PARM_DESC(zvol_threads
, "Max number of threads to handle I/O requests");
2724 module_param(zvol_request_sync
, uint
, 0644);
2725 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
2727 module_param(zvol_max_discard_blocks
, ulong
, 0444);
2728 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
2730 module_param(zvol_prefetch_bytes
, uint
, 0644);
2731 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
2733 module_param(zvol_volmode
, uint
, 0644);
2734 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");