4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
23 * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
24 * Copyright (c) 2024, Klara, Inc.
27 #include <sys/dataset_kstats.h>
29 #include <sys/dmu_traverse.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_dir.h>
34 #include <sys/zfeature.h>
35 #include <sys/zil_impl.h>
36 #include <sys/dmu_tx.h>
38 #include <sys/zfs_rlock.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zvol_impl.h>
44 #include <linux/blkdev_compat.h>
45 #include <linux/task_io_accounting_ops.h>
46 #include <linux/workqueue.h>
47 #include <linux/blk-mq.h>
49 static void zvol_request_impl(zvol_state_t
*zv
, struct bio
*bio
,
50 struct request
*rq
, boolean_t force_sync
);
52 static unsigned int zvol_major
= ZVOL_MAJOR
;
53 static unsigned int zvol_request_sync
= 0;
54 static unsigned int zvol_prefetch_bytes
= (128 * 1024);
55 static unsigned long zvol_max_discard_blocks
= 16384;
58 * Switch taskq at multiple of 512 MB offset. This can be set to a lower value
59 * to utilize more threads for small files but may affect prefetch hits.
61 #define ZVOL_TASKQ_OFFSET_SHIFT 29
63 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
64 static unsigned int zvol_open_timeout_ms
= 1000;
67 static unsigned int zvol_threads
= 0;
68 static unsigned int zvol_blk_mq_threads
= 0;
69 static unsigned int zvol_blk_mq_actual_threads
;
70 static boolean_t zvol_use_blk_mq
= B_FALSE
;
73 * The maximum number of volblocksize blocks to process per thread. Typically,
74 * write heavy workloads preform better with higher values here, and read
75 * heavy workloads preform better with lower values, but that's not a hard
76 * and fast rule. It's basically a knob to tune between "less overhead with
77 * less parallelism" and "more overhead, but more parallelism".
79 * '8' was chosen as a reasonable, balanced, default based off of sequential
80 * read and write tests to a zvol in an NVMe pool (with 16 CPUs).
82 static unsigned int zvol_blk_mq_blocks_per_thread
= 8;
84 static unsigned int zvol_num_taskqs
= 0;
86 #ifndef BLKDEV_DEFAULT_RQ
87 /* BLKDEV_MAX_RQ was renamed to BLKDEV_DEFAULT_RQ in the 5.16 kernel */
88 #define BLKDEV_DEFAULT_RQ BLKDEV_MAX_RQ
92 * Finalize our BIO or request.
95 zvol_end_io(struct bio
*bio
, struct request
*rq
, int error
)
98 bio
->bi_status
= errno_to_bi_status(-error
);
101 blk_mq_end_request(rq
, errno_to_bi_status(error
));
105 static unsigned int zvol_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
106 static unsigned int zvol_actual_blk_mq_queue_depth
;
108 struct zvol_state_os
{
109 struct gendisk
*zvo_disk
; /* generic disk */
110 struct request_queue
*zvo_queue
; /* request queue */
111 dev_t zvo_dev
; /* device id */
113 struct blk_mq_tag_set tag_set
;
115 /* Set from the global 'zvol_use_blk_mq' at zvol load */
116 boolean_t use_blk_mq
;
119 typedef struct zv_taskq
{
123 static zv_taskq_t zvol_taskqs
;
124 static struct ida zvol_ida
;
126 typedef struct zv_request_stack
{
132 typedef struct zv_work
{
134 struct work_struct work
;
137 typedef struct zv_request_task
{
142 static zv_request_task_t
*
143 zv_request_task_create(zv_request_t zvr
)
145 zv_request_task_t
*task
;
146 task
= kmem_alloc(sizeof (zv_request_task_t
), KM_SLEEP
);
147 taskq_init_ent(&task
->ent
);
153 zv_request_task_free(zv_request_task_t
*task
)
155 kmem_free(task
, sizeof (*task
));
159 * This is called when a new block multiqueue request comes in. A request
160 * contains one or more BIOs.
162 static blk_status_t
zvol_mq_queue_rq(struct blk_mq_hw_ctx
*hctx
,
163 const struct blk_mq_queue_data
*bd
)
165 struct request
*rq
= bd
->rq
;
166 zvol_state_t
*zv
= rq
->q
->queuedata
;
168 /* Tell the kernel that we are starting to process this request */
169 blk_mq_start_request(rq
);
171 if (blk_rq_is_passthrough(rq
)) {
172 /* Skip non filesystem request */
173 blk_mq_end_request(rq
, BLK_STS_IOERR
);
174 return (BLK_STS_IOERR
);
177 zvol_request_impl(zv
, NULL
, rq
, 0);
179 /* Acknowledge to the kernel that we got this request */
183 static struct blk_mq_ops zvol_blk_mq_queue_ops
= {
184 .queue_rq
= zvol_mq_queue_rq
,
187 /* Initialize our blk-mq struct */
188 static int zvol_blk_mq_alloc_tag_set(zvol_state_t
*zv
)
190 struct zvol_state_os
*zso
= zv
->zv_zso
;
192 memset(&zso
->tag_set
, 0, sizeof (zso
->tag_set
));
194 /* Initialize tag set. */
195 zso
->tag_set
.ops
= &zvol_blk_mq_queue_ops
;
196 zso
->tag_set
.nr_hw_queues
= zvol_blk_mq_actual_threads
;
197 zso
->tag_set
.queue_depth
= zvol_actual_blk_mq_queue_depth
;
198 zso
->tag_set
.numa_node
= NUMA_NO_NODE
;
199 zso
->tag_set
.cmd_size
= 0;
202 * We need BLK_MQ_F_BLOCKING here since we do blocking calls in
203 * zvol_request_impl()
205 zso
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_BLOCKING
;
206 zso
->tag_set
.driver_data
= zv
;
208 return (blk_mq_alloc_tag_set(&zso
->tag_set
));
212 * Given a path, return TRUE if path is a ZVOL.
215 zvol_os_is_zvol(const char *path
)
219 if (vdev_lookup_bdev(path
, &dev
) != 0)
222 if (MAJOR(dev
) == zvol_major
)
229 zvol_write(zv_request_t
*zvr
)
231 struct bio
*bio
= zvr
->bio
;
232 struct request
*rq
= zvr
->rq
;
235 zvol_state_t
*zv
= zvr
->zv
;
236 struct request_queue
*q
;
237 struct gendisk
*disk
;
238 unsigned long start_time
= 0;
239 boolean_t acct
= B_FALSE
;
241 ASSERT3P(zv
, !=, NULL
);
242 ASSERT3U(zv
->zv_open_count
, >, 0);
243 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
245 q
= zv
->zv_zso
->zvo_queue
;
246 disk
= zv
->zv_zso
->zvo_disk
;
248 /* bio marked as FLUSH need to flush before write */
249 if (io_is_flush(bio
, rq
))
250 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
252 /* Some requests are just for flush and nothing else. */
253 if (io_size(bio
, rq
) == 0) {
254 rw_exit(&zv
->zv_suspend_lock
);
255 zvol_end_io(bio
, rq
, 0);
259 zfs_uio_bvec_init(&uio
, bio
, rq
);
261 ssize_t start_resid
= uio
.uio_resid
;
264 * With use_blk_mq, accounting is done by blk_mq_start_request()
265 * and blk_mq_end_request(), so we can skip it here.
268 acct
= blk_queue_io_stat(q
);
270 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
276 io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
278 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
279 uio
.uio_loffset
, uio
.uio_resid
, RL_WRITER
);
281 uint64_t volsize
= zv
->zv_volsize
;
282 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
283 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
284 uint64_t off
= uio
.uio_loffset
;
285 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
287 if (bytes
> volsize
- off
) /* don't write past the end */
288 bytes
= volsize
- off
;
290 dmu_tx_hold_write_by_dnode(tx
, zv
->zv_dn
, off
, bytes
);
292 /* This will only fail for ENOSPC */
293 error
= dmu_tx_assign(tx
, TXG_WAIT
);
298 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
300 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
307 zfs_rangelock_exit(lr
);
309 int64_t nwritten
= start_resid
- uio
.uio_resid
;
310 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
311 task_io_account_write(nwritten
);
314 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
316 rw_exit(&zv
->zv_suspend_lock
);
319 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
, start_time
);
322 zvol_end_io(bio
, rq
, -error
);
326 zvol_write_task(void *arg
)
328 zv_request_task_t
*task
= arg
;
329 zvol_write(&task
->zvr
);
330 zv_request_task_free(task
);
334 zvol_discard(zv_request_t
*zvr
)
336 struct bio
*bio
= zvr
->bio
;
337 struct request
*rq
= zvr
->rq
;
338 zvol_state_t
*zv
= zvr
->zv
;
339 uint64_t start
= io_offset(bio
, rq
);
340 uint64_t size
= io_size(bio
, rq
);
341 uint64_t end
= start
+ size
;
345 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
346 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
347 unsigned long start_time
= 0;
348 boolean_t acct
= B_FALSE
;
350 ASSERT3P(zv
, !=, NULL
);
351 ASSERT3U(zv
->zv_open_count
, >, 0);
352 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
355 acct
= blk_queue_io_stat(q
);
357 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
362 sync
= io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
364 if (end
> zv
->zv_volsize
) {
365 error
= SET_ERROR(EIO
);
370 * Align the request to volume block boundaries when a secure erase is
371 * not required. This will prevent dnode_free_range() from zeroing out
372 * the unaligned parts which is slow (read-modify-write) and useless
373 * since we are not freeing any space by doing so.
375 if (!io_is_secure_erase(bio
, rq
)) {
376 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
377 end
= P2ALIGN_TYPED(end
, zv
->zv_volblocksize
, uint64_t);
384 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
385 start
, size
, RL_WRITER
);
387 tx
= dmu_tx_create(zv
->zv_objset
);
388 dmu_tx_mark_netfree(tx
);
389 error
= dmu_tx_assign(tx
, TXG_WAIT
);
393 zvol_log_truncate(zv
, tx
, start
, size
);
395 error
= dmu_free_long_range(zv
->zv_objset
,
396 ZVOL_OBJ
, start
, size
);
398 zfs_rangelock_exit(lr
);
400 if (error
== 0 && sync
)
401 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
404 rw_exit(&zv
->zv_suspend_lock
);
407 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
,
411 zvol_end_io(bio
, rq
, -error
);
415 zvol_discard_task(void *arg
)
417 zv_request_task_t
*task
= arg
;
418 zvol_discard(&task
->zvr
);
419 zv_request_task_free(task
);
423 zvol_read(zv_request_t
*zvr
)
425 struct bio
*bio
= zvr
->bio
;
426 struct request
*rq
= zvr
->rq
;
429 boolean_t acct
= B_FALSE
;
430 zvol_state_t
*zv
= zvr
->zv
;
431 struct request_queue
*q
;
432 struct gendisk
*disk
;
433 unsigned long start_time
= 0;
435 ASSERT3P(zv
, !=, NULL
);
436 ASSERT3U(zv
->zv_open_count
, >, 0);
438 zfs_uio_bvec_init(&uio
, bio
, rq
);
440 q
= zv
->zv_zso
->zvo_queue
;
441 disk
= zv
->zv_zso
->zvo_disk
;
443 ssize_t start_resid
= uio
.uio_resid
;
446 * When blk-mq is being used, accounting is done by
447 * blk_mq_start_request() and blk_mq_end_request().
450 acct
= blk_queue_io_stat(q
);
452 start_time
= blk_generic_start_io_acct(q
, disk
, READ
,
456 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
457 uio
.uio_loffset
, uio
.uio_resid
, RL_READER
);
459 uint64_t volsize
= zv
->zv_volsize
;
461 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
462 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
464 /* don't read past the end */
465 if (bytes
> volsize
- uio
.uio_loffset
)
466 bytes
= volsize
- uio
.uio_loffset
;
468 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
470 /* convert checksum errors into IO errors */
472 error
= SET_ERROR(EIO
);
476 zfs_rangelock_exit(lr
);
478 int64_t nread
= start_resid
- uio
.uio_resid
;
479 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
480 task_io_account_read(nread
);
482 rw_exit(&zv
->zv_suspend_lock
);
485 blk_generic_end_io_acct(q
, disk
, READ
, bio
, start_time
);
488 zvol_end_io(bio
, rq
, -error
);
492 zvol_read_task(void *arg
)
494 zv_request_task_t
*task
= arg
;
495 zvol_read(&task
->zvr
);
496 zv_request_task_free(task
);
501 * Process a BIO or request
503 * Either 'bio' or 'rq' should be set depending on if we are processing a
504 * bio or a request (both should not be set).
506 * force_sync: Set to 0 to defer processing to a background taskq
507 * Set to 1 to process data synchronously
510 zvol_request_impl(zvol_state_t
*zv
, struct bio
*bio
, struct request
*rq
,
511 boolean_t force_sync
)
513 fstrans_cookie_t cookie
= spl_fstrans_mark();
514 uint64_t offset
= io_offset(bio
, rq
);
515 uint64_t size
= io_size(bio
, rq
);
516 int rw
= io_data_dir(bio
, rq
);
518 if (unlikely(zv
->zv_flags
& ZVOL_REMOVING
)) {
519 zvol_end_io(bio
, rq
, -SET_ERROR(ENXIO
));
523 if (zvol_request_sync
|| zv
->zv_threading
== B_FALSE
)
532 if (io_has_data(bio
, rq
) && offset
+ size
> zv
->zv_volsize
) {
533 printk(KERN_INFO
"%s: bad access: offset=%llu, size=%lu\n",
534 zv
->zv_zso
->zvo_disk
->disk_name
,
535 (long long unsigned)offset
,
536 (long unsigned)size
);
538 zvol_end_io(bio
, rq
, -SET_ERROR(EIO
));
542 zv_request_task_t
*task
;
543 zv_taskq_t
*ztqs
= &zvol_taskqs
;
544 uint_t blk_mq_hw_queue
= 0;
548 #ifdef HAVE_BLK_MQ_RQ_HCTX
549 blk_mq_hw_queue
= rq
->mq_hctx
->queue_num
;
552 rq
->q
->queue_hw_ctx
[rq
->q
->mq_map
[rq
->cpu
]]->queue_num
;
554 taskq_hash
= cityhash3((uintptr_t)zv
, offset
>> ZVOL_TASKQ_OFFSET_SHIFT
,
556 tq_idx
= taskq_hash
% ztqs
->tqs_cnt
;
559 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
560 zvol_end_io(bio
, rq
, -SET_ERROR(EROFS
));
565 * Prevents the zvol from being suspended, or the ZIL being
566 * concurrently opened. Will be released after the i/o
569 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
572 * Open a ZIL if this is the first time we have written to this
573 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
574 * than zv_state_lock so that we don't need to acquire an
575 * additional lock in this path.
577 if (zv
->zv_zilog
== NULL
) {
578 rw_exit(&zv
->zv_suspend_lock
);
579 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
580 if (zv
->zv_zilog
== NULL
) {
581 zv
->zv_zilog
= zil_open(zv
->zv_objset
,
582 zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
583 zv
->zv_flags
|= ZVOL_WRITTEN_TO
;
584 /* replay / destroy done in zvol_create_minor */
585 VERIFY0((zv
->zv_zilog
->zl_header
->zh_flags
&
588 rw_downgrade(&zv
->zv_suspend_lock
);
592 * We don't want this thread to be blocked waiting for i/o to
593 * complete, so we instead wait from a taskq callback. The
594 * i/o may be a ZIL write (via zil_commit()), or a read of an
595 * indirect block, or a read of a data block (if this is a
596 * partial-block write). We will indicate that the i/o is
597 * complete by calling END_IO() from the taskq callback.
599 * This design allows the calling thread to continue and
600 * initiate more concurrent operations by calling
601 * zvol_request() again. There are typically only a small
602 * number of threads available to call zvol_request() (e.g.
603 * one per iSCSI target), so keeping the latency of
604 * zvol_request() low is important for performance.
606 * The zvol_request_sync module parameter allows this
607 * behavior to be altered, for performance evaluation
608 * purposes. If the callback blocks, setting
609 * zvol_request_sync=1 will result in much worse performance.
611 * We can have up to zvol_threads concurrent i/o's being
612 * processed for all zvols on the system. This is typically
613 * a vast improvement over the zvol_request_sync=1 behavior
614 * of one i/o at a time per zvol. However, an even better
615 * design would be for zvol_request() to initiate the zio
616 * directly, and then be notified by the zio_done callback,
617 * which would call END_IO(). Unfortunately, the DMU/ZIL
618 * interfaces lack this functionality (they block waiting for
619 * the i/o to complete).
621 if (io_is_discard(bio
, rq
) || io_is_secure_erase(bio
, rq
)) {
625 task
= zv_request_task_create(zvr
);
626 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
627 zvol_discard_task
, task
, 0, &task
->ent
);
633 task
= zv_request_task_create(zvr
);
634 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
635 zvol_write_task
, task
, 0, &task
->ent
);
640 * The SCST driver, and possibly others, may issue READ I/Os
641 * with a length of zero bytes. These empty I/Os contain no
642 * data and require no additional handling.
645 zvol_end_io(bio
, rq
, 0);
649 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
651 /* See comment in WRITE case above. */
655 task
= zv_request_task_create(zvr
);
656 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
657 zvol_read_task
, task
, 0, &task
->ent
);
662 spl_fstrans_unmark(cookie
);
665 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
666 #ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
668 zvol_submit_bio(struct bio
*bio
)
671 zvol_submit_bio(struct bio
*bio
)
674 static MAKE_REQUEST_FN_RET
675 zvol_request(struct request_queue
*q
, struct bio
*bio
)
678 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
679 #if defined(HAVE_BIO_BDEV_DISK)
680 struct request_queue
*q
= bio
->bi_bdev
->bd_disk
->queue
;
682 struct request_queue
*q
= bio
->bi_disk
->queue
;
685 zvol_state_t
*zv
= q
->queuedata
;
687 zvol_request_impl(zv
, bio
, NULL
, 0);
688 #if defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
689 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
690 !defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
691 return (BLK_QC_T_NONE
);
696 #ifdef HAVE_BLK_MODE_T
697 zvol_open(struct gendisk
*disk
, blk_mode_t flag
)
699 zvol_open(struct block_device
*bdev
, fmode_t flag
)
704 boolean_t drop_suspend
= B_FALSE
;
705 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
706 hrtime_t timeout
= MSEC2NSEC(zvol_open_timeout_ms
);
707 hrtime_t start
= gethrtime();
711 rw_enter(&zvol_state_lock
, RW_READER
);
713 * Obtain a copy of private_data under the zvol_state_lock to make
714 * sure that either the result of zvol free code path setting
715 * disk->private_data to NULL is observed, or zvol_os_free()
716 * is not called on this zv because of the positive zv_open_count.
718 #ifdef HAVE_BLK_MODE_T
719 zv
= disk
->private_data
;
721 zv
= bdev
->bd_disk
->private_data
;
724 rw_exit(&zvol_state_lock
);
725 return (-SET_ERROR(ENXIO
));
728 mutex_enter(&zv
->zv_state_lock
);
730 if (unlikely(zv
->zv_flags
& ZVOL_REMOVING
)) {
731 mutex_exit(&zv
->zv_state_lock
);
732 rw_exit(&zvol_state_lock
);
733 return (-SET_ERROR(ENXIO
));
737 * Make sure zvol is not suspended during first open
738 * (hold zv_suspend_lock) and respect proper lock acquisition
739 * ordering - zv_suspend_lock before zv_state_lock
741 if (zv
->zv_open_count
== 0) {
742 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
743 mutex_exit(&zv
->zv_state_lock
);
744 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
745 mutex_enter(&zv
->zv_state_lock
);
746 /* check to see if zv_suspend_lock is needed */
747 if (zv
->zv_open_count
!= 0) {
748 rw_exit(&zv
->zv_suspend_lock
);
750 drop_suspend
= B_TRUE
;
753 drop_suspend
= B_TRUE
;
756 rw_exit(&zvol_state_lock
);
758 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
760 if (zv
->zv_open_count
== 0) {
761 boolean_t drop_namespace
= B_FALSE
;
763 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
766 * In all other call paths the spa_namespace_lock is taken
767 * before the bdev->bd_mutex lock. However, on open(2)
768 * the __blkdev_get() function calls fops->open() with the
769 * bdev->bd_mutex lock held. This can result in a deadlock
770 * when zvols from one pool are used as vdevs in another.
772 * To prevent a lock inversion deadlock we preemptively
773 * take the spa_namespace_lock. Normally the lock will not
774 * be contended and this is safe because spa_open_common()
775 * handles the case where the caller already holds the
776 * spa_namespace_lock.
778 * When the lock cannot be aquired after multiple retries
779 * this must be the vdev on zvol deadlock case and we have
780 * no choice but to return an error. For 5.12 and older
781 * kernels returning -ERESTARTSYS will result in the
782 * bdev->bd_mutex being dropped, then reacquired, and
783 * fops->open() being called again. This process can be
784 * repeated safely until both locks are acquired. For 5.13
785 * and newer the -ERESTARTSYS retry logic was removed from
786 * the kernel so the only option is to return the error for
787 * the caller to handle it.
789 if (!mutex_owned(&spa_namespace_lock
)) {
790 if (!mutex_tryenter(&spa_namespace_lock
)) {
791 mutex_exit(&zv
->zv_state_lock
);
792 rw_exit(&zv
->zv_suspend_lock
);
793 drop_suspend
= B_FALSE
;
795 #ifdef HAVE_BLKDEV_GET_ERESTARTSYS
797 return (-SET_ERROR(ERESTARTSYS
));
799 if ((gethrtime() - start
) > timeout
)
800 return (-SET_ERROR(ERESTARTSYS
));
802 schedule_timeout_interruptible(
807 drop_namespace
= B_TRUE
;
811 error
= -zvol_first_open(zv
, !(blk_mode_is_open_write(flag
)));
814 mutex_exit(&spa_namespace_lock
);
818 if ((blk_mode_is_open_write(flag
)) &&
819 (zv
->zv_flags
& ZVOL_RDONLY
)) {
820 if (zv
->zv_open_count
== 0)
823 error
= -SET_ERROR(EROFS
);
829 mutex_exit(&zv
->zv_state_lock
);
831 rw_exit(&zv
->zv_suspend_lock
);
834 #ifdef HAVE_BLK_MODE_T
835 disk_check_media_change(disk
);
837 zfs_check_media_change(bdev
);
844 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG
845 zvol_release(struct gendisk
*disk
)
847 zvol_release(struct gendisk
*disk
, fmode_t unused
)
850 #if !defined(HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG)
854 boolean_t drop_suspend
= B_TRUE
;
856 rw_enter(&zvol_state_lock
, RW_READER
);
857 zv
= disk
->private_data
;
859 mutex_enter(&zv
->zv_state_lock
);
860 ASSERT3U(zv
->zv_open_count
, >, 0);
862 * make sure zvol is not suspended during last close
863 * (hold zv_suspend_lock) and respect proper lock acquisition
864 * ordering - zv_suspend_lock before zv_state_lock
866 if (zv
->zv_open_count
== 1) {
867 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
868 mutex_exit(&zv
->zv_state_lock
);
869 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
870 mutex_enter(&zv
->zv_state_lock
);
871 /* check to see if zv_suspend_lock is needed */
872 if (zv
->zv_open_count
!= 1) {
873 rw_exit(&zv
->zv_suspend_lock
);
874 drop_suspend
= B_FALSE
;
878 drop_suspend
= B_FALSE
;
880 rw_exit(&zvol_state_lock
);
882 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
885 if (zv
->zv_open_count
== 0) {
886 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
890 mutex_exit(&zv
->zv_state_lock
);
893 rw_exit(&zv
->zv_suspend_lock
);
897 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
898 unsigned int cmd
, unsigned long arg
)
900 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
903 ASSERT3U(zv
->zv_open_count
, >, 0);
907 #ifdef HAVE_FSYNC_BDEV
909 #elif defined(HAVE_SYNC_BLOCKDEV)
912 #error "Neither fsync_bdev() nor sync_blockdev() found"
914 invalidate_bdev(bdev
);
915 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
917 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
918 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
920 rw_exit(&zv
->zv_suspend_lock
);
924 mutex_enter(&zv
->zv_state_lock
);
925 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
926 mutex_exit(&zv
->zv_state_lock
);
934 return (SET_ERROR(error
));
939 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
940 unsigned cmd
, unsigned long arg
)
942 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
945 #define zvol_compat_ioctl NULL
949 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
951 unsigned int mask
= 0;
953 rw_enter(&zvol_state_lock
, RW_READER
);
955 zvol_state_t
*zv
= disk
->private_data
;
957 mutex_enter(&zv
->zv_state_lock
);
958 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
960 mutex_exit(&zv
->zv_state_lock
);
963 rw_exit(&zvol_state_lock
);
969 zvol_revalidate_disk(struct gendisk
*disk
)
971 rw_enter(&zvol_state_lock
, RW_READER
);
973 zvol_state_t
*zv
= disk
->private_data
;
975 mutex_enter(&zv
->zv_state_lock
);
976 set_capacity(zv
->zv_zso
->zvo_disk
,
977 zv
->zv_volsize
>> SECTOR_BITS
);
978 mutex_exit(&zv
->zv_state_lock
);
981 rw_exit(&zvol_state_lock
);
987 zvol_os_update_volsize(zvol_state_t
*zv
, uint64_t volsize
)
989 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
991 #if defined(HAVE_REVALIDATE_DISK_SIZE)
992 revalidate_disk_size(disk
, zvol_revalidate_disk(disk
) == 0);
993 #elif defined(HAVE_REVALIDATE_DISK)
994 revalidate_disk(disk
);
996 zvol_revalidate_disk(disk
);
1002 zvol_os_clear_private(zvol_state_t
*zv
)
1005 * Cleared while holding zvol_state_lock as a writer
1006 * which will prevent zvol_open() from opening it.
1008 zv
->zv_zso
->zvo_disk
->private_data
= NULL
;
1012 * Provide a simple virtual geometry for legacy compatibility. For devices
1013 * smaller than 1 MiB a small head and sector count is used to allow very
1014 * tiny devices. For devices over 1 Mib a standard head and sector count
1015 * is used to keep the cylinders count reasonable.
1018 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1020 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1023 ASSERT3U(zv
->zv_open_count
, >, 0);
1025 sectors
= get_capacity(zv
->zv_zso
->zvo_disk
);
1027 if (sectors
> 2048) {
1036 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1042 * Why have two separate block_device_operations structs?
1044 * Normally we'd just have one, and assign 'submit_bio' as needed. However,
1045 * it's possible the user's kernel is built with CONSTIFY_PLUGIN, meaning we
1046 * can't just change submit_bio dynamically at runtime. So just create two
1047 * separate structs to get around this.
1049 static const struct block_device_operations zvol_ops_blk_mq
= {
1051 .release
= zvol_release
,
1052 .ioctl
= zvol_ioctl
,
1053 .compat_ioctl
= zvol_compat_ioctl
,
1054 .check_events
= zvol_check_events
,
1055 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1056 .revalidate_disk
= zvol_revalidate_disk
,
1058 .getgeo
= zvol_getgeo
,
1059 .owner
= THIS_MODULE
,
1062 static const struct block_device_operations zvol_ops
= {
1064 .release
= zvol_release
,
1065 .ioctl
= zvol_ioctl
,
1066 .compat_ioctl
= zvol_compat_ioctl
,
1067 .check_events
= zvol_check_events
,
1068 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1069 .revalidate_disk
= zvol_revalidate_disk
,
1071 .getgeo
= zvol_getgeo
,
1072 .owner
= THIS_MODULE
,
1073 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1074 .submit_bio
= zvol_submit_bio
,
1079 * Since 6.9, Linux has been removing queue limit setters in favour of an
1080 * initial queue_limits struct applied when the device is open. Since 6.11,
1081 * queue_limits is being extended to allow more things to be applied when the
1082 * device is open. Setters are also being removed for this.
1084 * For OpenZFS, this means that depending on kernel version, some options may
1085 * be set up before the device is open, and some applied to an open device
1086 * (queue) after the fact.
1088 * We manage this complexity by having our own limits struct,
1089 * zvol_queue_limits_t, in which we carry any queue config that we're
1090 * interested in setting. This structure is the same on all kernels.
1092 * These limits are then applied to the queue at device open time by the most
1093 * appropriate method for the kernel.
1095 * zvol_queue_limits_convert() is used on 6.9+ (where the two-arg form of
1096 * blk_alloc_disk() exists). This converts our limits struct to a proper Linux
1097 * struct queue_limits, and passes it in. Any fields added in later kernels are
1098 * (obviously) not set up here.
1100 * zvol_queue_limits_apply() is called on all kernel versions after the queue
1101 * is created, and applies any remaining config. Before 6.9 that will be
1102 * everything, via setter methods. After 6.9 that will be whatever couldn't be
1103 * put into struct queue_limits. (This implies that zvol_queue_limits_apply()
1104 * will always be a no-op on the latest kernel we support).
1106 typedef struct zvol_queue_limits
{
1107 unsigned int zql_max_hw_sectors
;
1108 unsigned short zql_max_segments
;
1109 unsigned int zql_max_segment_size
;
1110 unsigned int zql_io_opt
;
1111 unsigned int zql_physical_block_size
;
1112 unsigned int zql_max_discard_sectors
;
1113 unsigned int zql_discard_granularity
;
1114 } zvol_queue_limits_t
;
1117 zvol_queue_limits_init(zvol_queue_limits_t
*limits
, zvol_state_t
*zv
,
1118 boolean_t use_blk_mq
)
1120 limits
->zql_max_hw_sectors
= (DMU_MAX_ACCESS
/ 4) >> 9;
1124 * IO requests can be really big (1MB). When an IO request
1125 * comes in, it is passed off to zvol_read() or zvol_write()
1126 * in a new thread, where it is chunked up into 'volblocksize'
1127 * sized pieces and processed. So for example, if the request
1128 * is a 1MB write and your volblocksize is 128k, one zvol_write
1129 * thread will take that request and sequentially do ten 128k
1130 * IOs. This is due to the fact that the thread needs to lock
1131 * each volblocksize sized block. So you might be wondering:
1132 * "instead of passing the whole 1MB request to one thread,
1133 * why not pass ten individual 128k chunks to ten threads and
1134 * process the whole write in parallel?" The short answer is
1135 * that there's a sweet spot number of chunks that balances
1136 * the greater parallelism with the added overhead of more
1137 * threads. The sweet spot can be different depending on if you
1138 * have a read or write heavy workload. Writes typically want
1139 * high chunk counts while reads typically want lower ones. On
1140 * a test pool with 6 NVMe drives in a 3x 2-disk mirror
1141 * configuration, with volblocksize=8k, the sweet spot for good
1142 * sequential reads and writes was at 8 chunks.
1146 * Below we tell the kernel how big we want our requests
1147 * to be. You would think that blk_queue_io_opt() would be
1148 * used to do this since it is used to "set optimal request
1149 * size for the queue", but that doesn't seem to do
1150 * anything - the kernel still gives you huge requests
1151 * with tons of little PAGE_SIZE segments contained within it.
1153 * Knowing that the kernel will just give you PAGE_SIZE segments
1154 * no matter what, you can say "ok, I want PAGE_SIZE byte
1155 * segments, and I want 'N' of them per request", where N is
1156 * the correct number of segments for the volblocksize and
1157 * number of chunks you want.
1159 if (zvol_blk_mq_blocks_per_thread
!= 0) {
1160 unsigned int chunks
;
1161 chunks
= MIN(zvol_blk_mq_blocks_per_thread
, UINT16_MAX
);
1163 limits
->zql_max_segment_size
= PAGE_SIZE
;
1164 limits
->zql_max_segments
=
1165 (zv
->zv_volblocksize
* chunks
) / PAGE_SIZE
;
1168 * Special case: zvol_blk_mq_blocks_per_thread = 0
1169 * Max everything out.
1171 limits
->zql_max_segments
= UINT16_MAX
;
1172 limits
->zql_max_segment_size
= UINT_MAX
;
1175 limits
->zql_max_segments
= UINT16_MAX
;
1176 limits
->zql_max_segment_size
= UINT_MAX
;
1179 limits
->zql_io_opt
= DMU_MAX_ACCESS
/ 2;
1181 limits
->zql_physical_block_size
= zv
->zv_volblocksize
;
1182 limits
->zql_max_discard_sectors
=
1183 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9;
1184 limits
->zql_discard_granularity
= zv
->zv_volblocksize
;
1187 #ifdef HAVE_BLK_ALLOC_DISK_2ARG
1189 zvol_queue_limits_convert(zvol_queue_limits_t
*limits
,
1190 struct queue_limits
*qlimits
)
1192 memset(qlimits
, 0, sizeof (struct queue_limits
));
1193 qlimits
->max_hw_sectors
= limits
->zql_max_hw_sectors
;
1194 qlimits
->max_segments
= limits
->zql_max_segments
;
1195 qlimits
->max_segment_size
= limits
->zql_max_segment_size
;
1196 qlimits
->io_opt
= limits
->zql_io_opt
;
1197 qlimits
->physical_block_size
= limits
->zql_physical_block_size
;
1198 qlimits
->max_discard_sectors
= limits
->zql_max_discard_sectors
;
1199 qlimits
->max_hw_discard_sectors
= limits
->zql_max_discard_sectors
;
1200 qlimits
->discard_granularity
= limits
->zql_discard_granularity
;
1201 #ifdef HAVE_BLKDEV_QUEUE_LIMITS_FEATURES
1203 BLK_FEAT_WRITE_CACHE
| BLK_FEAT_FUA
| BLK_FEAT_IO_STAT
;
1209 zvol_queue_limits_apply(zvol_queue_limits_t
*limits
,
1210 struct request_queue
*queue
)
1212 #ifndef HAVE_BLK_ALLOC_DISK_2ARG
1213 blk_queue_max_hw_sectors(queue
, limits
->zql_max_hw_sectors
);
1214 blk_queue_max_segments(queue
, limits
->zql_max_segments
);
1215 blk_queue_max_segment_size(queue
, limits
->zql_max_segment_size
);
1216 blk_queue_io_opt(queue
, limits
->zql_io_opt
);
1217 blk_queue_physical_block_size(queue
, limits
->zql_physical_block_size
);
1218 blk_queue_max_discard_sectors(queue
, limits
->zql_max_discard_sectors
);
1219 blk_queue_discard_granularity(queue
, limits
->zql_discard_granularity
);
1221 #ifndef HAVE_BLKDEV_QUEUE_LIMITS_FEATURES
1222 blk_queue_set_write_cache(queue
, B_TRUE
);
1223 blk_queue_flag_set(QUEUE_FLAG_IO_STAT
, queue
);
1228 zvol_alloc_non_blk_mq(struct zvol_state_os
*zso
, zvol_queue_limits_t
*limits
)
1230 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)
1231 #if defined(HAVE_BLK_ALLOC_DISK)
1232 zso
->zvo_disk
= blk_alloc_disk(NUMA_NO_NODE
);
1233 if (zso
->zvo_disk
== NULL
)
1236 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1237 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1238 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1239 struct queue_limits qlimits
;
1240 zvol_queue_limits_convert(limits
, &qlimits
);
1241 struct gendisk
*disk
= blk_alloc_disk(&qlimits
, NUMA_NO_NODE
);
1243 zso
->zvo_disk
= NULL
;
1247 zso
->zvo_disk
= disk
;
1248 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1249 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1252 zso
->zvo_queue
= blk_alloc_queue(NUMA_NO_NODE
);
1253 if (zso
->zvo_queue
== NULL
)
1256 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1257 if (zso
->zvo_disk
== NULL
) {
1258 blk_cleanup_queue(zso
->zvo_queue
);
1262 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1263 #endif /* HAVE_BLK_ALLOC_DISK */
1265 zso
->zvo_queue
= blk_generic_alloc_queue(zvol_request
, NUMA_NO_NODE
);
1266 if (zso
->zvo_queue
== NULL
)
1269 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1270 if (zso
->zvo_disk
== NULL
) {
1271 blk_cleanup_queue(zso
->zvo_queue
);
1275 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1276 #endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
1278 zvol_queue_limits_apply(limits
, zso
->zvo_queue
);
1285 zvol_alloc_blk_mq(zvol_state_t
*zv
, zvol_queue_limits_t
*limits
)
1287 struct zvol_state_os
*zso
= zv
->zv_zso
;
1289 /* Allocate our blk-mq tag_set */
1290 if (zvol_blk_mq_alloc_tag_set(zv
) != 0)
1293 #if defined(HAVE_BLK_ALLOC_DISK)
1294 zso
->zvo_disk
= blk_mq_alloc_disk(&zso
->tag_set
, zv
);
1295 if (zso
->zvo_disk
== NULL
) {
1296 blk_mq_free_tag_set(&zso
->tag_set
);
1299 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1300 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1301 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1302 struct queue_limits qlimits
;
1303 zvol_queue_limits_convert(limits
, &qlimits
);
1304 struct gendisk
*disk
= blk_mq_alloc_disk(&zso
->tag_set
, &qlimits
, zv
);
1306 zso
->zvo_disk
= NULL
;
1307 blk_mq_free_tag_set(&zso
->tag_set
);
1311 zso
->zvo_disk
= disk
;
1312 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1313 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1315 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1316 if (zso
->zvo_disk
== NULL
) {
1317 blk_cleanup_queue(zso
->zvo_queue
);
1318 blk_mq_free_tag_set(&zso
->tag_set
);
1321 /* Allocate queue */
1322 zso
->zvo_queue
= blk_mq_init_queue(&zso
->tag_set
);
1323 if (IS_ERR(zso
->zvo_queue
)) {
1324 blk_mq_free_tag_set(&zso
->tag_set
);
1328 /* Our queue is now created, assign it to our disk */
1329 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1332 zvol_queue_limits_apply(limits
, zso
->zvo_queue
);
1338 * Allocate memory for a new zvol_state_t and setup the required
1339 * request queue and generic disk structures for the block device.
1341 static zvol_state_t
*
1342 zvol_alloc(dev_t dev
, const char *name
, uint64_t volblocksize
)
1345 struct zvol_state_os
*zso
;
1349 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
1352 if (volmode
== ZFS_VOLMODE_DEFAULT
)
1353 volmode
= zvol_volmode
;
1355 if (volmode
== ZFS_VOLMODE_NONE
)
1358 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1359 zso
= kmem_zalloc(sizeof (struct zvol_state_os
), KM_SLEEP
);
1361 zv
->zv_volmode
= volmode
;
1362 zv
->zv_volblocksize
= volblocksize
;
1364 list_link_init(&zv
->zv_next
);
1365 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1366 cv_init(&zv
->zv_removing_cv
, NULL
, CV_DEFAULT
, NULL
);
1368 zv
->zv_zso
->use_blk_mq
= zvol_use_blk_mq
;
1370 zvol_queue_limits_t limits
;
1371 zvol_queue_limits_init(&limits
, zv
, zv
->zv_zso
->use_blk_mq
);
1374 * The block layer has 3 interfaces for getting BIOs:
1376 * 1. blk-mq request queues (new)
1377 * 2. submit_bio() (oldest)
1378 * 3. regular request queues (old).
1380 * Each of those interfaces has two permutations:
1382 * a) We have blk_alloc_disk()/blk_mq_alloc_disk(), which allocates
1383 * both the disk and its queue (5.14 kernel or newer)
1385 * b) We don't have blk_*alloc_disk(), and have to allocate the
1386 * disk and the queue separately. (5.13 kernel or older)
1388 if (zv
->zv_zso
->use_blk_mq
) {
1389 ret
= zvol_alloc_blk_mq(zv
, &limits
);
1390 zso
->zvo_disk
->fops
= &zvol_ops_blk_mq
;
1392 ret
= zvol_alloc_non_blk_mq(zso
, &limits
);
1393 zso
->zvo_disk
->fops
= &zvol_ops
;
1398 /* Limit read-ahead to a single page to prevent over-prefetching. */
1399 blk_queue_set_read_ahead(zso
->zvo_queue
, 1);
1401 if (!zv
->zv_zso
->use_blk_mq
) {
1402 /* Disable write merging in favor of the ZIO pipeline. */
1403 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zso
->zvo_queue
);
1406 zso
->zvo_queue
->queuedata
= zv
;
1408 zv
->zv_open_count
= 0;
1409 strlcpy(zv
->zv_name
, name
, sizeof (zv
->zv_name
));
1411 zfs_rangelock_init(&zv
->zv_rangelock
, NULL
, NULL
);
1412 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
1414 zso
->zvo_disk
->major
= zvol_major
;
1415 zso
->zvo_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
1418 * Setting ZFS_VOLMODE_DEV disables partitioning on ZVOL devices.
1419 * This is accomplished by limiting the number of minors for the
1420 * device to one and explicitly disabling partition scanning.
1422 if (volmode
== ZFS_VOLMODE_DEV
) {
1423 zso
->zvo_disk
->minors
= 1;
1424 zso
->zvo_disk
->flags
&= ~GENHD_FL_EXT_DEVT
;
1425 zso
->zvo_disk
->flags
|= GENHD_FL_NO_PART
;
1428 zso
->zvo_disk
->first_minor
= (dev
& MINORMASK
);
1429 zso
->zvo_disk
->private_data
= zv
;
1430 snprintf(zso
->zvo_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1431 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1436 kmem_free(zso
, sizeof (struct zvol_state_os
));
1437 kmem_free(zv
, sizeof (zvol_state_t
));
1442 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1443 * At this time, the structure is not opened by anyone, is taken off
1444 * the zvol_state_list, and has its private data set to NULL.
1445 * The zvol_state_lock is dropped.
1447 * This function may take many milliseconds to complete (e.g. we've seen
1448 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
1449 * "del_gendisk". Thus, consumers need to be careful to account for this
1450 * latency when calling this function.
1453 zvol_os_free(zvol_state_t
*zv
)
1456 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1457 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
1458 ASSERT0(zv
->zv_open_count
);
1459 ASSERT3P(zv
->zv_zso
->zvo_disk
->private_data
, ==, NULL
);
1461 rw_destroy(&zv
->zv_suspend_lock
);
1462 zfs_rangelock_fini(&zv
->zv_rangelock
);
1464 del_gendisk(zv
->zv_zso
->zvo_disk
);
1465 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
1466 (defined(HAVE_BLK_ALLOC_DISK) || defined(HAVE_BLK_ALLOC_DISK_2ARG))
1467 #if defined(HAVE_BLK_CLEANUP_DISK)
1468 blk_cleanup_disk(zv
->zv_zso
->zvo_disk
);
1470 put_disk(zv
->zv_zso
->zvo_disk
);
1473 blk_cleanup_queue(zv
->zv_zso
->zvo_queue
);
1474 put_disk(zv
->zv_zso
->zvo_disk
);
1477 if (zv
->zv_zso
->use_blk_mq
)
1478 blk_mq_free_tag_set(&zv
->zv_zso
->tag_set
);
1480 ida_simple_remove(&zvol_ida
,
1481 MINOR(zv
->zv_zso
->zvo_dev
) >> ZVOL_MINOR_BITS
);
1483 cv_destroy(&zv
->zv_removing_cv
);
1484 mutex_destroy(&zv
->zv_state_lock
);
1485 dataset_kstats_destroy(&zv
->zv_kstat
);
1487 kmem_free(zv
->zv_zso
, sizeof (struct zvol_state_os
));
1488 kmem_free(zv
, sizeof (zvol_state_t
));
1492 zvol_wait_close(zvol_state_t
*zv
)
1496 struct add_disk_work
{
1497 struct delayed_work work
;
1498 struct gendisk
*disk
;
1503 __zvol_os_add_disk(struct gendisk
*disk
)
1506 #ifdef HAVE_ADD_DISK_RET
1507 error
= add_disk(disk
);
1514 #if defined(HAVE_BDEV_FILE_OPEN_BY_PATH)
1516 zvol_os_add_disk_work(struct work_struct
*work
)
1518 struct add_disk_work
*add_disk_work
;
1519 add_disk_work
= container_of(work
, struct add_disk_work
, work
.work
);
1520 add_disk_work
->error
= __zvol_os_add_disk(add_disk_work
->disk
);
1527 * This function basically calls add_disk() from a workqueue. You may be
1528 * thinking: why not just call add_disk() directly?
1530 * When you call add_disk(), the zvol appears to the world. When this happens,
1531 * the kernel calls disk_scan_partitions() on the zvol, which behaves
1532 * differently on the 6.9+ kernels:
1534 * - 6.8 and older kernels -
1535 * disk_scan_partitions()
1536 * handle = bdev_open_by_dev(
1538 * bdev_release(handle);
1543 * disk_scan_partitions()
1544 * file = bdev_file_open_by_dev()
1547 * < wait for return to userspace >
1550 * The difference is that the bdev_release() from the 6.8 kernel is synchronous
1551 * while the fput() from the 6.9 kernel is async. Or more specifically it's
1552 * async that has to wait until we return to userspace (since it adds the fput
1553 * into the caller's work queue with the TWA_RESUME flag set). This is not the
1554 * behavior we want, since we want do things like create+destroy a zvol within
1555 * a single ZFS_IOC_CREATE ioctl, and the "create" part needs to release the
1556 * reference to the zvol while we're in the IOCTL, which can't wait until we
1557 * return to userspace.
1559 * We can get around this since fput() has a special codepath for when it's
1560 * running in a kernel thread or interrupt. In those cases, it just puts the
1561 * fput into the system workqueue, which we can force to run with
1562 * __flush_workqueue(). That is why we call add_disk() from a workqueue - so it
1563 * run from a kernel thread and "tricks" the fput() codepaths.
1565 * Note that __flush_workqueue() is slowly getting deprecated. This may be ok
1566 * though, since our IOCTL will spin on EBUSY waiting for the zvol release (via
1567 * fput) to happen, which it eventually, naturally, will from the system_wq
1568 * without us explicitly calling __flush_workqueue().
1571 zvol_os_add_disk(struct gendisk
*disk
)
1573 #if defined(HAVE_BDEV_FILE_OPEN_BY_PATH) /* 6.9+ kernel */
1574 struct add_disk_work add_disk_work
;
1576 INIT_DELAYED_WORK(&add_disk_work
.work
, zvol_os_add_disk_work
);
1577 add_disk_work
.disk
= disk
;
1578 add_disk_work
.error
= 0;
1580 /* Use *_delayed_work functions since they're not GPL'd */
1581 schedule_delayed_work(&add_disk_work
.work
, 0);
1582 flush_delayed_work(&add_disk_work
.work
);
1584 __flush_workqueue(system_wq
);
1585 return (add_disk_work
.error
);
1586 #else /* <= 6.8 kernel */
1587 return (__zvol_os_add_disk(disk
));
1592 * Create a block device minor node and setup the linkage between it
1593 * and the specified volume. Once this function returns the block
1594 * device is live and ready for use.
1597 zvol_os_create_minor(const char *name
)
1601 dmu_object_info_t
*doi
;
1607 uint64_t hash
= zvol_name_hash(name
);
1608 uint64_t volthreading
;
1609 bool replayed_zil
= B_FALSE
;
1611 if (zvol_inhibit_dev
)
1614 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1616 return (SET_ERROR(-idx
));
1617 minor
= idx
<< ZVOL_MINOR_BITS
;
1618 if (MINOR(minor
) != minor
) {
1619 /* too many partitions can cause an overflow */
1620 zfs_dbgmsg("zvol: create minor overflow: %s, minor %u/%u",
1621 name
, minor
, MINOR(minor
));
1622 ida_simple_remove(&zvol_ida
, idx
);
1623 return (SET_ERROR(EINVAL
));
1626 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
1628 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1629 mutex_exit(&zv
->zv_state_lock
);
1630 ida_simple_remove(&zvol_ida
, idx
);
1631 return (SET_ERROR(EEXIST
));
1634 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1636 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
1640 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1642 goto out_dmu_objset_disown
;
1644 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1646 goto out_dmu_objset_disown
;
1648 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
,
1649 doi
->doi_data_block_size
);
1651 error
= SET_ERROR(EAGAIN
);
1652 goto out_dmu_objset_disown
;
1656 if (dmu_objset_is_snapshot(os
))
1657 zv
->zv_flags
|= ZVOL_RDONLY
;
1659 zv
->zv_volsize
= volsize
;
1663 zv
->zv_threading
= B_TRUE
;
1664 if (dsl_prop_get_integer(name
, "volthreading", &volthreading
, NULL
)
1666 zv
->zv_threading
= volthreading
;
1668 set_capacity(zv
->zv_zso
->zvo_disk
, zv
->zv_volsize
>> 9);
1670 #ifdef QUEUE_FLAG_DISCARD
1671 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_zso
->zvo_queue
);
1673 #ifdef QUEUE_FLAG_NONROT
1674 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_zso
->zvo_queue
);
1676 #ifdef QUEUE_FLAG_ADD_RANDOM
1677 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_zso
->zvo_queue
);
1679 /* This flag was introduced in kernel version 4.12. */
1680 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1681 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH
, zv
->zv_zso
->zvo_queue
);
1684 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
1685 error
= dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
1687 goto out_dmu_objset_disown
;
1688 ASSERT3P(zv
->zv_zilog
, ==, NULL
);
1689 zv
->zv_zilog
= zil_open(os
, zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
1690 if (spa_writeable(dmu_objset_spa(os
))) {
1691 if (zil_replay_disable
)
1692 replayed_zil
= zil_destroy(zv
->zv_zilog
, B_FALSE
);
1694 replayed_zil
= zil_replay(os
, zv
, zvol_replay_vector
);
1697 zil_close(zv
->zv_zilog
);
1698 zv
->zv_zilog
= NULL
;
1701 * When udev detects the addition of the device it will immediately
1702 * invoke blkid(8) to determine the type of content on the device.
1703 * Prefetching the blocks commonly scanned by blkid(8) will speed
1706 len
= MIN(zvol_prefetch_bytes
, SPA_MAXBLOCKSIZE
);
1708 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1709 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1710 ZIO_PRIORITY_SYNC_READ
);
1713 zv
->zv_objset
= NULL
;
1714 out_dmu_objset_disown
:
1715 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1717 kmem_free(doi
, sizeof (dmu_object_info_t
));
1720 * Keep in mind that once add_disk() is called, the zvol is
1721 * announced to the world, and zvol_open()/zvol_release() can
1722 * be called at any time. Incidentally, add_disk() itself calls
1723 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1727 rw_enter(&zvol_state_lock
, RW_WRITER
);
1729 rw_exit(&zvol_state_lock
);
1730 error
= zvol_os_add_disk(zv
->zv_zso
->zvo_disk
);
1732 ida_simple_remove(&zvol_ida
, idx
);
1739 zvol_os_rename_minor(zvol_state_t
*zv
, const char *newname
)
1741 int readonly
= get_disk_ro(zv
->zv_zso
->zvo_disk
);
1743 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1744 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1746 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1748 /* move to new hashtable entry */
1749 zv
->zv_hash
= zvol_name_hash(newname
);
1750 hlist_del(&zv
->zv_hlink
);
1751 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1754 * The block device's read-only state is briefly changed causing
1755 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1756 * the name change and fixes the symlinks. This does not change
1757 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1758 * changes. This would normally be done using kobject_uevent() but
1759 * that is a GPL-only symbol which is why we need this workaround.
1761 set_disk_ro(zv
->zv_zso
->zvo_disk
, !readonly
);
1762 set_disk_ro(zv
->zv_zso
->zvo_disk
, readonly
);
1764 dataset_kstats_rename(&zv
->zv_kstat
, newname
);
1768 zvol_os_set_disk_ro(zvol_state_t
*zv
, int flags
)
1771 set_disk_ro(zv
->zv_zso
->zvo_disk
, flags
);
1775 zvol_os_set_capacity(zvol_state_t
*zv
, uint64_t capacity
)
1778 set_capacity(zv
->zv_zso
->zvo_disk
, capacity
);
1787 * zvol_threads is the module param the user passes in.
1789 * zvol_actual_threads is what we use internally, since the user can
1790 * pass zvol_thread = 0 to mean "use all the CPUs" (the default).
1792 static unsigned int zvol_actual_threads
;
1794 if (zvol_threads
== 0) {
1796 * See dde9380a1 for why 32 was chosen here. This should
1797 * probably be refined to be some multiple of the number
1800 zvol_actual_threads
= MAX(num_online_cpus(), 32);
1802 zvol_actual_threads
= MIN(MAX(zvol_threads
, 1), 1024);
1806 * Use atleast 32 zvol_threads but for many core system,
1807 * prefer 6 threads per taskq, but no more taskqs
1808 * than threads in them on large systems.
1811 * cpus taskqs threads threads
1812 * ------- ------- ------- -------
1823 zv_taskq_t
*ztqs
= &zvol_taskqs
;
1824 uint_t num_tqs
= MIN(num_online_cpus(), zvol_num_taskqs
);
1826 num_tqs
= 1 + num_online_cpus() / 6;
1827 while (num_tqs
* num_tqs
> zvol_actual_threads
)
1830 uint_t per_tq_thread
= zvol_actual_threads
/ num_tqs
;
1831 if (per_tq_thread
* num_tqs
< zvol_actual_threads
)
1833 ztqs
->tqs_cnt
= num_tqs
;
1834 ztqs
->tqs_taskq
= kmem_alloc(num_tqs
* sizeof (taskq_t
*), KM_SLEEP
);
1835 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1837 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
* sizeof (taskq_t
*));
1838 ztqs
->tqs_taskq
= NULL
;
1839 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1843 if (zvol_blk_mq_queue_depth
== 0) {
1844 zvol_actual_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
1846 zvol_actual_blk_mq_queue_depth
=
1847 MAX(zvol_blk_mq_queue_depth
, BLKDEV_MIN_RQ
);
1850 if (zvol_blk_mq_threads
== 0) {
1851 zvol_blk_mq_actual_threads
= num_online_cpus();
1853 zvol_blk_mq_actual_threads
= MIN(MAX(zvol_blk_mq_threads
, 1),
1857 for (uint_t i
= 0; i
< num_tqs
; i
++) {
1859 (void) snprintf(name
, sizeof (name
), "%s_tq-%u",
1861 ztqs
->tqs_taskq
[i
] = taskq_create(name
, per_tq_thread
,
1862 maxclsyspri
, per_tq_thread
, INT_MAX
,
1863 TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1864 if (ztqs
->tqs_taskq
[i
] == NULL
) {
1865 for (int j
= i
- 1; j
>= 0; j
--)
1866 taskq_destroy(ztqs
->tqs_taskq
[j
]);
1867 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1868 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
*
1869 sizeof (taskq_t
*));
1870 ztqs
->tqs_taskq
= NULL
;
1876 ida_init(&zvol_ida
);
1883 zv_taskq_t
*ztqs
= &zvol_taskqs
;
1885 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1887 if (ztqs
->tqs_taskq
== NULL
) {
1888 ASSERT3U(ztqs
->tqs_cnt
, ==, 0);
1890 for (uint_t i
= 0; i
< ztqs
->tqs_cnt
; i
++) {
1891 ASSERT3P(ztqs
->tqs_taskq
[i
], !=, NULL
);
1892 taskq_destroy(ztqs
->tqs_taskq
[i
]);
1894 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
*
1895 sizeof (taskq_t
*));
1896 ztqs
->tqs_taskq
= NULL
;
1899 ida_destroy(&zvol_ida
);
1903 module_param(zvol_inhibit_dev
, uint
, 0644);
1904 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1906 module_param(zvol_major
, uint
, 0444);
1907 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1909 module_param(zvol_threads
, uint
, 0444);
1910 MODULE_PARM_DESC(zvol_threads
, "Number of threads to handle I/O requests. Set"
1911 "to 0 to use all active CPUs");
1913 module_param(zvol_request_sync
, uint
, 0644);
1914 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
1916 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1917 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1919 module_param(zvol_num_taskqs
, uint
, 0444);
1920 MODULE_PARM_DESC(zvol_num_taskqs
, "Number of zvol taskqs");
1922 module_param(zvol_prefetch_bytes
, uint
, 0644);
1923 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
1925 module_param(zvol_volmode
, uint
, 0644);
1926 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");
1928 module_param(zvol_blk_mq_queue_depth
, uint
, 0644);
1929 MODULE_PARM_DESC(zvol_blk_mq_queue_depth
, "Default blk-mq queue depth");
1931 module_param(zvol_use_blk_mq
, uint
, 0644);
1932 MODULE_PARM_DESC(zvol_use_blk_mq
, "Use the blk-mq API for zvols");
1934 module_param(zvol_blk_mq_blocks_per_thread
, uint
, 0644);
1935 MODULE_PARM_DESC(zvol_blk_mq_blocks_per_thread
,
1936 "Process volblocksize blocks per thread");
1938 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
1939 module_param(zvol_open_timeout_ms
, uint
, 0644);
1940 MODULE_PARM_DESC(zvol_open_timeout_ms
, "Timeout for ZVOL open retries");