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.
25 #include <sys/dataset_kstats.h>
27 #include <sys/dmu_traverse.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
32 #include <sys/zfeature.h>
33 #include <sys/zil_impl.h>
34 #include <sys/dmu_tx.h>
36 #include <sys/zfs_rlock.h>
37 #include <sys/spa_impl.h>
39 #include <sys/zvol_impl.h>
42 #include <linux/blkdev_compat.h>
43 #include <linux/task_io_accounting_ops.h>
46 #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;
69 static unsigned int zvol_blk_mq_threads
= 0;
70 static unsigned int zvol_blk_mq_actual_threads
;
71 static boolean_t zvol_use_blk_mq
= B_FALSE
;
74 * The maximum number of volblocksize blocks to process per thread. Typically,
75 * write heavy workloads preform better with higher values here, and read
76 * heavy workloads preform better with lower values, but that's not a hard
77 * and fast rule. It's basically a knob to tune between "less overhead with
78 * less parallelism" and "more overhead, but more parallelism".
80 * '8' was chosen as a reasonable, balanced, default based off of sequential
81 * read and write tests to a zvol in an NVMe pool (with 16 CPUs).
83 static unsigned int zvol_blk_mq_blocks_per_thread
= 8;
86 static unsigned int zvol_num_taskqs
= 0;
88 #ifndef BLKDEV_DEFAULT_RQ
89 /* BLKDEV_MAX_RQ was renamed to BLKDEV_DEFAULT_RQ in the 5.16 kernel */
90 #define BLKDEV_DEFAULT_RQ BLKDEV_MAX_RQ
94 * Finalize our BIO or request.
97 #define END_IO(zv, bio, rq, error) do { \
99 BIO_END_IO(bio, error); \
101 blk_mq_end_request(rq, errno_to_bi_status(error)); \
105 #define END_IO(zv, bio, rq, error) BIO_END_IO(bio, error)
109 static unsigned int zvol_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
110 static unsigned int zvol_actual_blk_mq_queue_depth
;
113 struct zvol_state_os
{
114 struct gendisk
*zvo_disk
; /* generic disk */
115 struct request_queue
*zvo_queue
; /* request queue */
116 dev_t zvo_dev
; /* device id */
119 struct blk_mq_tag_set tag_set
;
122 /* Set from the global 'zvol_use_blk_mq' at zvol load */
123 boolean_t use_blk_mq
;
126 typedef struct zv_taskq
{
130 static zv_taskq_t zvol_taskqs
;
131 static struct ida zvol_ida
;
133 typedef struct zv_request_stack
{
139 typedef struct zv_work
{
141 struct work_struct work
;
144 typedef struct zv_request_task
{
149 static zv_request_task_t
*
150 zv_request_task_create(zv_request_t zvr
)
152 zv_request_task_t
*task
;
153 task
= kmem_alloc(sizeof (zv_request_task_t
), KM_SLEEP
);
154 taskq_init_ent(&task
->ent
);
160 zv_request_task_free(zv_request_task_t
*task
)
162 kmem_free(task
, sizeof (*task
));
168 * This is called when a new block multiqueue request comes in. A request
169 * contains one or more BIOs.
171 static blk_status_t
zvol_mq_queue_rq(struct blk_mq_hw_ctx
*hctx
,
172 const struct blk_mq_queue_data
*bd
)
174 struct request
*rq
= bd
->rq
;
175 zvol_state_t
*zv
= rq
->q
->queuedata
;
177 /* Tell the kernel that we are starting to process this request */
178 blk_mq_start_request(rq
);
180 if (blk_rq_is_passthrough(rq
)) {
181 /* Skip non filesystem request */
182 blk_mq_end_request(rq
, BLK_STS_IOERR
);
183 return (BLK_STS_IOERR
);
186 zvol_request_impl(zv
, NULL
, rq
, 0);
188 /* Acknowledge to the kernel that we got this request */
192 static struct blk_mq_ops zvol_blk_mq_queue_ops
= {
193 .queue_rq
= zvol_mq_queue_rq
,
196 /* Initialize our blk-mq struct */
197 static int zvol_blk_mq_alloc_tag_set(zvol_state_t
*zv
)
199 struct zvol_state_os
*zso
= zv
->zv_zso
;
201 memset(&zso
->tag_set
, 0, sizeof (zso
->tag_set
));
203 /* Initialize tag set. */
204 zso
->tag_set
.ops
= &zvol_blk_mq_queue_ops
;
205 zso
->tag_set
.nr_hw_queues
= zvol_blk_mq_actual_threads
;
206 zso
->tag_set
.queue_depth
= zvol_actual_blk_mq_queue_depth
;
207 zso
->tag_set
.numa_node
= NUMA_NO_NODE
;
208 zso
->tag_set
.cmd_size
= 0;
211 * We need BLK_MQ_F_BLOCKING here since we do blocking calls in
212 * zvol_request_impl()
214 zso
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_BLOCKING
;
215 zso
->tag_set
.driver_data
= zv
;
217 return (blk_mq_alloc_tag_set(&zso
->tag_set
));
219 #endif /* HAVE_BLK_MQ */
222 * Given a path, return TRUE if path is a ZVOL.
225 zvol_os_is_zvol(const char *path
)
229 if (vdev_lookup_bdev(path
, &dev
) != 0)
232 if (MAJOR(dev
) == zvol_major
)
239 zvol_write(zv_request_t
*zvr
)
241 struct bio
*bio
= zvr
->bio
;
242 struct request
*rq
= zvr
->rq
;
245 zvol_state_t
*zv
= zvr
->zv
;
246 struct request_queue
*q
;
247 struct gendisk
*disk
;
248 unsigned long start_time
= 0;
249 boolean_t acct
= B_FALSE
;
251 ASSERT3P(zv
, !=, NULL
);
252 ASSERT3U(zv
->zv_open_count
, >, 0);
253 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
255 q
= zv
->zv_zso
->zvo_queue
;
256 disk
= zv
->zv_zso
->zvo_disk
;
258 /* bio marked as FLUSH need to flush before write */
259 if (io_is_flush(bio
, rq
))
260 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
262 /* Some requests are just for flush and nothing else. */
263 if (io_size(bio
, rq
) == 0) {
264 rw_exit(&zv
->zv_suspend_lock
);
265 END_IO(zv
, bio
, rq
, 0);
269 zfs_uio_bvec_init(&uio
, bio
, rq
);
271 ssize_t start_resid
= uio
.uio_resid
;
274 * With use_blk_mq, accounting is done by blk_mq_start_request()
275 * and blk_mq_end_request(), so we can skip it here.
278 acct
= blk_queue_io_stat(q
);
280 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
286 io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
288 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
289 uio
.uio_loffset
, uio
.uio_resid
, RL_WRITER
);
291 uint64_t volsize
= zv
->zv_volsize
;
292 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
293 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
294 uint64_t off
= uio
.uio_loffset
;
295 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
297 if (bytes
> volsize
- off
) /* don't write past the end */
298 bytes
= volsize
- off
;
300 dmu_tx_hold_write_by_dnode(tx
, zv
->zv_dn
, off
, bytes
);
302 /* This will only fail for ENOSPC */
303 error
= dmu_tx_assign(tx
, TXG_WAIT
);
308 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
310 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
317 zfs_rangelock_exit(lr
);
319 int64_t nwritten
= start_resid
- uio
.uio_resid
;
320 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
321 task_io_account_write(nwritten
);
324 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
326 rw_exit(&zv
->zv_suspend_lock
);
329 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
, start_time
);
332 END_IO(zv
, bio
, rq
, -error
);
336 zvol_write_task(void *arg
)
338 zv_request_task_t
*task
= arg
;
339 zvol_write(&task
->zvr
);
340 zv_request_task_free(task
);
344 zvol_discard(zv_request_t
*zvr
)
346 struct bio
*bio
= zvr
->bio
;
347 struct request
*rq
= zvr
->rq
;
348 zvol_state_t
*zv
= zvr
->zv
;
349 uint64_t start
= io_offset(bio
, rq
);
350 uint64_t size
= io_size(bio
, rq
);
351 uint64_t end
= start
+ size
;
355 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
356 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
357 unsigned long start_time
= 0;
358 boolean_t acct
= B_FALSE
;
360 ASSERT3P(zv
, !=, NULL
);
361 ASSERT3U(zv
->zv_open_count
, >, 0);
362 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
365 acct
= blk_queue_io_stat(q
);
367 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
372 sync
= io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
374 if (end
> zv
->zv_volsize
) {
375 error
= SET_ERROR(EIO
);
380 * Align the request to volume block boundaries when a secure erase is
381 * not required. This will prevent dnode_free_range() from zeroing out
382 * the unaligned parts which is slow (read-modify-write) and useless
383 * since we are not freeing any space by doing so.
385 if (!io_is_secure_erase(bio
, rq
)) {
386 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
387 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
394 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
395 start
, size
, RL_WRITER
);
397 tx
= dmu_tx_create(zv
->zv_objset
);
398 dmu_tx_mark_netfree(tx
);
399 error
= dmu_tx_assign(tx
, TXG_WAIT
);
403 zvol_log_truncate(zv
, tx
, start
, size
);
405 error
= dmu_free_long_range(zv
->zv_objset
,
406 ZVOL_OBJ
, start
, size
);
408 zfs_rangelock_exit(lr
);
410 if (error
== 0 && sync
)
411 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
414 rw_exit(&zv
->zv_suspend_lock
);
417 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
,
421 END_IO(zv
, bio
, rq
, -error
);
425 zvol_discard_task(void *arg
)
427 zv_request_task_t
*task
= arg
;
428 zvol_discard(&task
->zvr
);
429 zv_request_task_free(task
);
433 zvol_read(zv_request_t
*zvr
)
435 struct bio
*bio
= zvr
->bio
;
436 struct request
*rq
= zvr
->rq
;
439 boolean_t acct
= B_FALSE
;
440 zvol_state_t
*zv
= zvr
->zv
;
441 struct request_queue
*q
;
442 struct gendisk
*disk
;
443 unsigned long start_time
= 0;
445 ASSERT3P(zv
, !=, NULL
);
446 ASSERT3U(zv
->zv_open_count
, >, 0);
448 zfs_uio_bvec_init(&uio
, bio
, rq
);
450 q
= zv
->zv_zso
->zvo_queue
;
451 disk
= zv
->zv_zso
->zvo_disk
;
453 ssize_t start_resid
= uio
.uio_resid
;
456 * When blk-mq is being used, accounting is done by
457 * blk_mq_start_request() and blk_mq_end_request().
460 acct
= blk_queue_io_stat(q
);
462 start_time
= blk_generic_start_io_acct(q
, disk
, READ
,
466 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
467 uio
.uio_loffset
, uio
.uio_resid
, RL_READER
);
469 uint64_t volsize
= zv
->zv_volsize
;
471 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
472 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
474 /* don't read past the end */
475 if (bytes
> volsize
- uio
.uio_loffset
)
476 bytes
= volsize
- uio
.uio_loffset
;
478 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
480 /* convert checksum errors into IO errors */
482 error
= SET_ERROR(EIO
);
486 zfs_rangelock_exit(lr
);
488 int64_t nread
= start_resid
- uio
.uio_resid
;
489 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
490 task_io_account_read(nread
);
492 rw_exit(&zv
->zv_suspend_lock
);
495 blk_generic_end_io_acct(q
, disk
, READ
, bio
, start_time
);
498 END_IO(zv
, bio
, rq
, -error
);
502 zvol_read_task(void *arg
)
504 zv_request_task_t
*task
= arg
;
505 zvol_read(&task
->zvr
);
506 zv_request_task_free(task
);
511 * Process a BIO or request
513 * Either 'bio' or 'rq' should be set depending on if we are processing a
514 * bio or a request (both should not be set).
516 * force_sync: Set to 0 to defer processing to a background taskq
517 * Set to 1 to process data synchronously
520 zvol_request_impl(zvol_state_t
*zv
, struct bio
*bio
, struct request
*rq
,
521 boolean_t force_sync
)
523 fstrans_cookie_t cookie
= spl_fstrans_mark();
524 uint64_t offset
= io_offset(bio
, rq
);
525 uint64_t size
= io_size(bio
, rq
);
526 int rw
= io_data_dir(bio
, rq
);
528 if (zvol_request_sync
|| zv
->zv_threading
== B_FALSE
)
537 if (io_has_data(bio
, rq
) && offset
+ size
> zv
->zv_volsize
) {
538 printk(KERN_INFO
"%s: bad access: offset=%llu, size=%lu\n",
539 zv
->zv_zso
->zvo_disk
->disk_name
,
540 (long long unsigned)offset
,
541 (long unsigned)size
);
543 END_IO(zv
, bio
, rq
, -SET_ERROR(EIO
));
547 zv_request_task_t
*task
;
548 zv_taskq_t
*ztqs
= &zvol_taskqs
;
549 uint_t blk_mq_hw_queue
= 0;
554 #ifdef HAVE_BLK_MQ_RQ_HCTX
555 blk_mq_hw_queue
= rq
->mq_hctx
->queue_num
;
558 rq
->q
->queue_hw_ctx
[rq
->q
->mq_map
[rq
->cpu
]]->queue_num
;
561 taskq_hash
= cityhash4((uintptr_t)zv
, offset
>> ZVOL_TASKQ_OFFSET_SHIFT
,
563 tq_idx
= taskq_hash
% ztqs
->tqs_cnt
;
566 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
567 END_IO(zv
, bio
, rq
, -SET_ERROR(EROFS
));
572 * Prevents the zvol from being suspended, or the ZIL being
573 * concurrently opened. Will be released after the i/o
576 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
579 * Open a ZIL if this is the first time we have written to this
580 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
581 * than zv_state_lock so that we don't need to acquire an
582 * additional lock in this path.
584 if (zv
->zv_zilog
== NULL
) {
585 rw_exit(&zv
->zv_suspend_lock
);
586 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
587 if (zv
->zv_zilog
== NULL
) {
588 zv
->zv_zilog
= zil_open(zv
->zv_objset
,
589 zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
590 zv
->zv_flags
|= ZVOL_WRITTEN_TO
;
591 /* replay / destroy done in zvol_create_minor */
592 VERIFY0((zv
->zv_zilog
->zl_header
->zh_flags
&
595 rw_downgrade(&zv
->zv_suspend_lock
);
599 * We don't want this thread to be blocked waiting for i/o to
600 * complete, so we instead wait from a taskq callback. The
601 * i/o may be a ZIL write (via zil_commit()), or a read of an
602 * indirect block, or a read of a data block (if this is a
603 * partial-block write). We will indicate that the i/o is
604 * complete by calling END_IO() from the taskq callback.
606 * This design allows the calling thread to continue and
607 * initiate more concurrent operations by calling
608 * zvol_request() again. There are typically only a small
609 * number of threads available to call zvol_request() (e.g.
610 * one per iSCSI target), so keeping the latency of
611 * zvol_request() low is important for performance.
613 * The zvol_request_sync module parameter allows this
614 * behavior to be altered, for performance evaluation
615 * purposes. If the callback blocks, setting
616 * zvol_request_sync=1 will result in much worse performance.
618 * We can have up to zvol_threads concurrent i/o's being
619 * processed for all zvols on the system. This is typically
620 * a vast improvement over the zvol_request_sync=1 behavior
621 * of one i/o at a time per zvol. However, an even better
622 * design would be for zvol_request() to initiate the zio
623 * directly, and then be notified by the zio_done callback,
624 * which would call END_IO(). Unfortunately, the DMU/ZIL
625 * interfaces lack this functionality (they block waiting for
626 * the i/o to complete).
628 if (io_is_discard(bio
, rq
) || io_is_secure_erase(bio
, rq
)) {
632 task
= zv_request_task_create(zvr
);
633 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
634 zvol_discard_task
, task
, 0, &task
->ent
);
640 task
= zv_request_task_create(zvr
);
641 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
642 zvol_write_task
, task
, 0, &task
->ent
);
647 * The SCST driver, and possibly others, may issue READ I/Os
648 * with a length of zero bytes. These empty I/Os contain no
649 * data and require no additional handling.
652 END_IO(zv
, bio
, rq
, 0);
656 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
658 /* See comment in WRITE case above. */
662 task
= zv_request_task_create(zvr
);
663 taskq_dispatch_ent(ztqs
->tqs_taskq
[tq_idx
],
664 zvol_read_task
, task
, 0, &task
->ent
);
669 spl_fstrans_unmark(cookie
);
672 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
673 #ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
675 zvol_submit_bio(struct bio
*bio
)
678 zvol_submit_bio(struct bio
*bio
)
681 static MAKE_REQUEST_FN_RET
682 zvol_request(struct request_queue
*q
, struct bio
*bio
)
685 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
686 #if defined(HAVE_BIO_BDEV_DISK)
687 struct request_queue
*q
= bio
->bi_bdev
->bd_disk
->queue
;
689 struct request_queue
*q
= bio
->bi_disk
->queue
;
692 zvol_state_t
*zv
= q
->queuedata
;
694 zvol_request_impl(zv
, bio
, NULL
, 0);
695 #if defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
696 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
697 !defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
698 return (BLK_QC_T_NONE
);
703 #ifdef HAVE_BLK_MODE_T
704 zvol_open(struct gendisk
*disk
, blk_mode_t flag
)
706 zvol_open(struct block_device
*bdev
, fmode_t flag
)
711 boolean_t drop_suspend
= B_FALSE
;
712 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
713 hrtime_t timeout
= MSEC2NSEC(zvol_open_timeout_ms
);
714 hrtime_t start
= gethrtime();
718 rw_enter(&zvol_state_lock
, RW_READER
);
720 * Obtain a copy of private_data under the zvol_state_lock to make
721 * sure that either the result of zvol free code path setting
722 * disk->private_data to NULL is observed, or zvol_os_free()
723 * is not called on this zv because of the positive zv_open_count.
725 #ifdef HAVE_BLK_MODE_T
726 zv
= disk
->private_data
;
728 zv
= bdev
->bd_disk
->private_data
;
731 rw_exit(&zvol_state_lock
);
732 return (SET_ERROR(-ENXIO
));
735 mutex_enter(&zv
->zv_state_lock
);
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
);
794 #ifdef HAVE_BLKDEV_GET_ERESTARTSYS
796 return (SET_ERROR(-ERESTARTSYS
));
798 if ((gethrtime() - start
) > timeout
)
799 return (SET_ERROR(-ERESTARTSYS
));
801 schedule_timeout(MSEC_TO_TICK(10));
805 drop_namespace
= B_TRUE
;
809 error
= -zvol_first_open(zv
, !(blk_mode_is_open_write(flag
)));
812 mutex_exit(&spa_namespace_lock
);
816 if ((blk_mode_is_open_write(flag
)) &&
817 (zv
->zv_flags
& ZVOL_RDONLY
)) {
818 if (zv
->zv_open_count
== 0)
821 error
= SET_ERROR(-EROFS
);
827 mutex_exit(&zv
->zv_state_lock
);
829 rw_exit(&zv
->zv_suspend_lock
);
832 #ifdef HAVE_BLK_MODE_T
833 disk_check_media_change(disk
);
835 zfs_check_media_change(bdev
);
842 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG
843 zvol_release(struct gendisk
*disk
)
845 zvol_release(struct gendisk
*disk
, fmode_t unused
)
848 #if !defined(HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG)
852 boolean_t drop_suspend
= B_TRUE
;
854 rw_enter(&zvol_state_lock
, RW_READER
);
855 zv
= disk
->private_data
;
857 mutex_enter(&zv
->zv_state_lock
);
858 ASSERT3U(zv
->zv_open_count
, >, 0);
860 * make sure zvol is not suspended during last close
861 * (hold zv_suspend_lock) and respect proper lock acquisition
862 * ordering - zv_suspend_lock before zv_state_lock
864 if (zv
->zv_open_count
== 1) {
865 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
866 mutex_exit(&zv
->zv_state_lock
);
867 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
868 mutex_enter(&zv
->zv_state_lock
);
869 /* check to see if zv_suspend_lock is needed */
870 if (zv
->zv_open_count
!= 1) {
871 rw_exit(&zv
->zv_suspend_lock
);
872 drop_suspend
= B_FALSE
;
876 drop_suspend
= B_FALSE
;
878 rw_exit(&zvol_state_lock
);
880 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
883 if (zv
->zv_open_count
== 0) {
884 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
888 mutex_exit(&zv
->zv_state_lock
);
891 rw_exit(&zv
->zv_suspend_lock
);
895 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
896 unsigned int cmd
, unsigned long arg
)
898 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
901 ASSERT3U(zv
->zv_open_count
, >, 0);
905 #ifdef HAVE_FSYNC_BDEV
907 #elif defined(HAVE_SYNC_BLOCKDEV)
910 #error "Neither fsync_bdev() nor sync_blockdev() found"
912 invalidate_bdev(bdev
);
913 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
915 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
916 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
918 rw_exit(&zv
->zv_suspend_lock
);
922 mutex_enter(&zv
->zv_state_lock
);
923 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
924 mutex_exit(&zv
->zv_state_lock
);
932 return (SET_ERROR(error
));
937 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
938 unsigned cmd
, unsigned long arg
)
940 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
943 #define zvol_compat_ioctl NULL
947 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
949 unsigned int mask
= 0;
951 rw_enter(&zvol_state_lock
, RW_READER
);
953 zvol_state_t
*zv
= disk
->private_data
;
955 mutex_enter(&zv
->zv_state_lock
);
956 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
958 mutex_exit(&zv
->zv_state_lock
);
961 rw_exit(&zvol_state_lock
);
967 zvol_revalidate_disk(struct gendisk
*disk
)
969 rw_enter(&zvol_state_lock
, RW_READER
);
971 zvol_state_t
*zv
= disk
->private_data
;
973 mutex_enter(&zv
->zv_state_lock
);
974 set_capacity(zv
->zv_zso
->zvo_disk
,
975 zv
->zv_volsize
>> SECTOR_BITS
);
976 mutex_exit(&zv
->zv_state_lock
);
979 rw_exit(&zvol_state_lock
);
985 zvol_os_update_volsize(zvol_state_t
*zv
, uint64_t volsize
)
987 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
989 #if defined(HAVE_REVALIDATE_DISK_SIZE)
990 revalidate_disk_size(disk
, zvol_revalidate_disk(disk
) == 0);
991 #elif defined(HAVE_REVALIDATE_DISK)
992 revalidate_disk(disk
);
994 zvol_revalidate_disk(disk
);
1000 zvol_os_clear_private(zvol_state_t
*zv
)
1003 * Cleared while holding zvol_state_lock as a writer
1004 * which will prevent zvol_open() from opening it.
1006 zv
->zv_zso
->zvo_disk
->private_data
= NULL
;
1010 * Provide a simple virtual geometry for legacy compatibility. For devices
1011 * smaller than 1 MiB a small head and sector count is used to allow very
1012 * tiny devices. For devices over 1 Mib a standard head and sector count
1013 * is used to keep the cylinders count reasonable.
1016 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1018 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1021 ASSERT3U(zv
->zv_open_count
, >, 0);
1023 sectors
= get_capacity(zv
->zv_zso
->zvo_disk
);
1025 if (sectors
> 2048) {
1034 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1040 * Why have two separate block_device_operations structs?
1042 * Normally we'd just have one, and assign 'submit_bio' as needed. However,
1043 * it's possible the user's kernel is built with CONSTIFY_PLUGIN, meaning we
1044 * can't just change submit_bio dynamically at runtime. So just create two
1045 * separate structs to get around this.
1047 static const struct block_device_operations zvol_ops_blk_mq
= {
1049 .release
= zvol_release
,
1050 .ioctl
= zvol_ioctl
,
1051 .compat_ioctl
= zvol_compat_ioctl
,
1052 .check_events
= zvol_check_events
,
1053 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1054 .revalidate_disk
= zvol_revalidate_disk
,
1056 .getgeo
= zvol_getgeo
,
1057 .owner
= THIS_MODULE
,
1060 static const struct block_device_operations zvol_ops
= {
1062 .release
= zvol_release
,
1063 .ioctl
= zvol_ioctl
,
1064 .compat_ioctl
= zvol_compat_ioctl
,
1065 .check_events
= zvol_check_events
,
1066 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1067 .revalidate_disk
= zvol_revalidate_disk
,
1069 .getgeo
= zvol_getgeo
,
1070 .owner
= THIS_MODULE
,
1071 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1072 .submit_bio
= zvol_submit_bio
,
1077 zvol_alloc_non_blk_mq(struct zvol_state_os
*zso
)
1079 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)
1080 #if defined(HAVE_BLK_ALLOC_DISK)
1081 zso
->zvo_disk
= blk_alloc_disk(NUMA_NO_NODE
);
1082 if (zso
->zvo_disk
== NULL
)
1085 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1086 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1087 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1088 struct gendisk
*disk
= blk_alloc_disk(NULL
, NUMA_NO_NODE
);
1090 zso
->zvo_disk
= NULL
;
1094 zso
->zvo_disk
= disk
;
1095 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1096 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1098 zso
->zvo_queue
= blk_alloc_queue(NUMA_NO_NODE
);
1099 if (zso
->zvo_queue
== NULL
)
1102 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1103 if (zso
->zvo_disk
== NULL
) {
1104 blk_cleanup_queue(zso
->zvo_queue
);
1108 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1109 #endif /* HAVE_BLK_ALLOC_DISK */
1111 zso
->zvo_queue
= blk_generic_alloc_queue(zvol_request
, NUMA_NO_NODE
);
1112 if (zso
->zvo_queue
== NULL
)
1115 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1116 if (zso
->zvo_disk
== NULL
) {
1117 blk_cleanup_queue(zso
->zvo_queue
);
1121 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1122 #endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
1128 zvol_alloc_blk_mq(zvol_state_t
*zv
)
1131 struct zvol_state_os
*zso
= zv
->zv_zso
;
1133 /* Allocate our blk-mq tag_set */
1134 if (zvol_blk_mq_alloc_tag_set(zv
) != 0)
1137 #if defined(HAVE_BLK_ALLOC_DISK)
1138 zso
->zvo_disk
= blk_mq_alloc_disk(&zso
->tag_set
, zv
);
1139 if (zso
->zvo_disk
== NULL
) {
1140 blk_mq_free_tag_set(&zso
->tag_set
);
1143 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1144 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1145 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1146 struct gendisk
*disk
= blk_mq_alloc_disk(&zso
->tag_set
, NULL
, zv
);
1148 zso
->zvo_disk
= NULL
;
1149 blk_mq_free_tag_set(&zso
->tag_set
);
1153 zso
->zvo_disk
= disk
;
1154 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1155 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1157 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1158 if (zso
->zvo_disk
== NULL
) {
1159 blk_cleanup_queue(zso
->zvo_queue
);
1160 blk_mq_free_tag_set(&zso
->tag_set
);
1163 /* Allocate queue */
1164 zso
->zvo_queue
= blk_mq_init_queue(&zso
->tag_set
);
1165 if (IS_ERR(zso
->zvo_queue
)) {
1166 blk_mq_free_tag_set(&zso
->tag_set
);
1170 /* Our queue is now created, assign it to our disk */
1171 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1179 * Allocate memory for a new zvol_state_t and setup the required
1180 * request queue and generic disk structures for the block device.
1182 static zvol_state_t
*
1183 zvol_alloc(dev_t dev
, const char *name
)
1186 struct zvol_state_os
*zso
;
1190 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
1193 if (volmode
== ZFS_VOLMODE_DEFAULT
)
1194 volmode
= zvol_volmode
;
1196 if (volmode
== ZFS_VOLMODE_NONE
)
1199 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1200 zso
= kmem_zalloc(sizeof (struct zvol_state_os
), KM_SLEEP
);
1202 zv
->zv_volmode
= volmode
;
1204 list_link_init(&zv
->zv_next
);
1205 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1208 zv
->zv_zso
->use_blk_mq
= zvol_use_blk_mq
;
1212 * The block layer has 3 interfaces for getting BIOs:
1214 * 1. blk-mq request queues (new)
1215 * 2. submit_bio() (oldest)
1216 * 3. regular request queues (old).
1218 * Each of those interfaces has two permutations:
1220 * a) We have blk_alloc_disk()/blk_mq_alloc_disk(), which allocates
1221 * both the disk and its queue (5.14 kernel or newer)
1223 * b) We don't have blk_*alloc_disk(), and have to allocate the
1224 * disk and the queue separately. (5.13 kernel or older)
1226 if (zv
->zv_zso
->use_blk_mq
) {
1227 ret
= zvol_alloc_blk_mq(zv
);
1228 zso
->zvo_disk
->fops
= &zvol_ops_blk_mq
;
1230 ret
= zvol_alloc_non_blk_mq(zso
);
1231 zso
->zvo_disk
->fops
= &zvol_ops
;
1236 blk_queue_set_write_cache(zso
->zvo_queue
, B_TRUE
, B_TRUE
);
1238 /* Limit read-ahead to a single page to prevent over-prefetching. */
1239 blk_queue_set_read_ahead(zso
->zvo_queue
, 1);
1241 if (!zv
->zv_zso
->use_blk_mq
) {
1242 /* Disable write merging in favor of the ZIO pipeline. */
1243 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zso
->zvo_queue
);
1246 /* Enable /proc/diskstats */
1247 blk_queue_flag_set(QUEUE_FLAG_IO_STAT
, zso
->zvo_queue
);
1249 zso
->zvo_queue
->queuedata
= zv
;
1251 zv
->zv_open_count
= 0;
1252 strlcpy(zv
->zv_name
, name
, sizeof (zv
->zv_name
));
1254 zfs_rangelock_init(&zv
->zv_rangelock
, NULL
, NULL
);
1255 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
1257 zso
->zvo_disk
->major
= zvol_major
;
1258 zso
->zvo_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
1261 * Setting ZFS_VOLMODE_DEV disables partitioning on ZVOL devices.
1262 * This is accomplished by limiting the number of minors for the
1263 * device to one and explicitly disabling partition scanning.
1265 if (volmode
== ZFS_VOLMODE_DEV
) {
1266 zso
->zvo_disk
->minors
= 1;
1267 zso
->zvo_disk
->flags
&= ~ZFS_GENHD_FL_EXT_DEVT
;
1268 zso
->zvo_disk
->flags
|= ZFS_GENHD_FL_NO_PART
;
1271 zso
->zvo_disk
->first_minor
= (dev
& MINORMASK
);
1272 zso
->zvo_disk
->private_data
= zv
;
1273 snprintf(zso
->zvo_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1274 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1279 kmem_free(zso
, sizeof (struct zvol_state_os
));
1280 kmem_free(zv
, sizeof (zvol_state_t
));
1285 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1286 * At this time, the structure is not opened by anyone, is taken off
1287 * the zvol_state_list, and has its private data set to NULL.
1288 * The zvol_state_lock is dropped.
1290 * This function may take many milliseconds to complete (e.g. we've seen
1291 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
1292 * "del_gendisk". Thus, consumers need to be careful to account for this
1293 * latency when calling this function.
1296 zvol_os_free(zvol_state_t
*zv
)
1299 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1300 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
1301 ASSERT0(zv
->zv_open_count
);
1302 ASSERT3P(zv
->zv_zso
->zvo_disk
->private_data
, ==, NULL
);
1304 rw_destroy(&zv
->zv_suspend_lock
);
1305 zfs_rangelock_fini(&zv
->zv_rangelock
);
1307 del_gendisk(zv
->zv_zso
->zvo_disk
);
1308 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
1309 (defined(HAVE_BLK_ALLOC_DISK) || defined(HAVE_BLK_ALLOC_DISK_2ARG))
1310 #if defined(HAVE_BLK_CLEANUP_DISK)
1311 blk_cleanup_disk(zv
->zv_zso
->zvo_disk
);
1313 put_disk(zv
->zv_zso
->zvo_disk
);
1316 blk_cleanup_queue(zv
->zv_zso
->zvo_queue
);
1317 put_disk(zv
->zv_zso
->zvo_disk
);
1321 if (zv
->zv_zso
->use_blk_mq
)
1322 blk_mq_free_tag_set(&zv
->zv_zso
->tag_set
);
1325 ida_simple_remove(&zvol_ida
,
1326 MINOR(zv
->zv_zso
->zvo_dev
) >> ZVOL_MINOR_BITS
);
1328 mutex_destroy(&zv
->zv_state_lock
);
1329 dataset_kstats_destroy(&zv
->zv_kstat
);
1331 kmem_free(zv
->zv_zso
, sizeof (struct zvol_state_os
));
1332 kmem_free(zv
, sizeof (zvol_state_t
));
1336 zvol_wait_close(zvol_state_t
*zv
)
1341 * Create a block device minor node and setup the linkage between it
1342 * and the specified volume. Once this function returns the block
1343 * device is live and ready for use.
1346 zvol_os_create_minor(const char *name
)
1350 dmu_object_info_t
*doi
;
1356 uint64_t hash
= zvol_name_hash(name
);
1357 uint64_t volthreading
;
1358 bool replayed_zil
= B_FALSE
;
1360 if (zvol_inhibit_dev
)
1363 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1365 return (SET_ERROR(-idx
));
1366 minor
= idx
<< ZVOL_MINOR_BITS
;
1367 if (MINOR(minor
) != minor
) {
1368 /* too many partitions can cause an overflow */
1369 zfs_dbgmsg("zvol: create minor overflow: %s, minor %u/%u",
1370 name
, minor
, MINOR(minor
));
1371 ida_simple_remove(&zvol_ida
, idx
);
1372 return (SET_ERROR(EINVAL
));
1375 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
1377 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1378 mutex_exit(&zv
->zv_state_lock
);
1379 ida_simple_remove(&zvol_ida
, idx
);
1380 return (SET_ERROR(EEXIST
));
1383 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1385 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
1389 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1391 goto out_dmu_objset_disown
;
1393 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1395 goto out_dmu_objset_disown
;
1397 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1399 error
= SET_ERROR(EAGAIN
);
1400 goto out_dmu_objset_disown
;
1404 if (dmu_objset_is_snapshot(os
))
1405 zv
->zv_flags
|= ZVOL_RDONLY
;
1407 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1408 zv
->zv_volsize
= volsize
;
1412 zv
->zv_threading
= B_TRUE
;
1413 if (dsl_prop_get_integer(name
, "volthreading", &volthreading
, NULL
)
1415 zv
->zv_threading
= volthreading
;
1417 set_capacity(zv
->zv_zso
->zvo_disk
, zv
->zv_volsize
>> 9);
1419 blk_queue_max_hw_sectors(zv
->zv_zso
->zvo_queue
,
1420 (DMU_MAX_ACCESS
/ 4) >> 9);
1422 if (zv
->zv_zso
->use_blk_mq
) {
1424 * IO requests can be really big (1MB). When an IO request
1425 * comes in, it is passed off to zvol_read() or zvol_write()
1426 * in a new thread, where it is chunked up into 'volblocksize'
1427 * sized pieces and processed. So for example, if the request
1428 * is a 1MB write and your volblocksize is 128k, one zvol_write
1429 * thread will take that request and sequentially do ten 128k
1430 * IOs. This is due to the fact that the thread needs to lock
1431 * each volblocksize sized block. So you might be wondering:
1432 * "instead of passing the whole 1MB request to one thread,
1433 * why not pass ten individual 128k chunks to ten threads and
1434 * process the whole write in parallel?" The short answer is
1435 * that there's a sweet spot number of chunks that balances
1436 * the greater parallelism with the added overhead of more
1437 * threads. The sweet spot can be different depending on if you
1438 * have a read or write heavy workload. Writes typically want
1439 * high chunk counts while reads typically want lower ones. On
1440 * a test pool with 6 NVMe drives in a 3x 2-disk mirror
1441 * configuration, with volblocksize=8k, the sweet spot for good
1442 * sequential reads and writes was at 8 chunks.
1446 * Below we tell the kernel how big we want our requests
1447 * to be. You would think that blk_queue_io_opt() would be
1448 * used to do this since it is used to "set optimal request
1449 * size for the queue", but that doesn't seem to do
1450 * anything - the kernel still gives you huge requests
1451 * with tons of little PAGE_SIZE segments contained within it.
1453 * Knowing that the kernel will just give you PAGE_SIZE segments
1454 * no matter what, you can say "ok, I want PAGE_SIZE byte
1455 * segments, and I want 'N' of them per request", where N is
1456 * the correct number of segments for the volblocksize and
1457 * number of chunks you want.
1460 if (zvol_blk_mq_blocks_per_thread
!= 0) {
1461 unsigned int chunks
;
1462 chunks
= MIN(zvol_blk_mq_blocks_per_thread
, UINT16_MAX
);
1464 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
,
1466 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
,
1467 (zv
->zv_volblocksize
* chunks
) / PAGE_SIZE
);
1470 * Special case: zvol_blk_mq_blocks_per_thread = 0
1471 * Max everything out.
1473 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
,
1475 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
,
1480 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
, UINT16_MAX
);
1481 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
, UINT_MAX
);
1484 blk_queue_physical_block_size(zv
->zv_zso
->zvo_queue
,
1485 zv
->zv_volblocksize
);
1486 blk_queue_io_opt(zv
->zv_zso
->zvo_queue
, zv
->zv_volblocksize
);
1487 blk_queue_max_discard_sectors(zv
->zv_zso
->zvo_queue
,
1488 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1489 blk_queue_discard_granularity(zv
->zv_zso
->zvo_queue
,
1490 zv
->zv_volblocksize
);
1491 #ifdef QUEUE_FLAG_DISCARD
1492 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_zso
->zvo_queue
);
1494 #ifdef QUEUE_FLAG_NONROT
1495 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_zso
->zvo_queue
);
1497 #ifdef QUEUE_FLAG_ADD_RANDOM
1498 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_zso
->zvo_queue
);
1500 /* This flag was introduced in kernel version 4.12. */
1501 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1502 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH
, zv
->zv_zso
->zvo_queue
);
1505 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
1506 error
= dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
1508 goto out_dmu_objset_disown
;
1509 ASSERT3P(zv
->zv_zilog
, ==, NULL
);
1510 zv
->zv_zilog
= zil_open(os
, zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
1511 if (spa_writeable(dmu_objset_spa(os
))) {
1512 if (zil_replay_disable
)
1513 replayed_zil
= zil_destroy(zv
->zv_zilog
, B_FALSE
);
1515 replayed_zil
= zil_replay(os
, zv
, zvol_replay_vector
);
1518 zil_close(zv
->zv_zilog
);
1519 zv
->zv_zilog
= NULL
;
1522 * When udev detects the addition of the device it will immediately
1523 * invoke blkid(8) to determine the type of content on the device.
1524 * Prefetching the blocks commonly scanned by blkid(8) will speed
1527 len
= MIN(zvol_prefetch_bytes
, SPA_MAXBLOCKSIZE
);
1529 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1530 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1531 ZIO_PRIORITY_SYNC_READ
);
1534 zv
->zv_objset
= NULL
;
1535 out_dmu_objset_disown
:
1536 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1538 kmem_free(doi
, sizeof (dmu_object_info_t
));
1541 * Keep in mind that once add_disk() is called, the zvol is
1542 * announced to the world, and zvol_open()/zvol_release() can
1543 * be called at any time. Incidentally, add_disk() itself calls
1544 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1548 rw_enter(&zvol_state_lock
, RW_WRITER
);
1550 rw_exit(&zvol_state_lock
);
1551 #ifdef HAVE_ADD_DISK_RET
1552 error
= add_disk(zv
->zv_zso
->zvo_disk
);
1554 add_disk(zv
->zv_zso
->zvo_disk
);
1557 ida_simple_remove(&zvol_ida
, idx
);
1564 zvol_os_rename_minor(zvol_state_t
*zv
, const char *newname
)
1566 int readonly
= get_disk_ro(zv
->zv_zso
->zvo_disk
);
1568 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1569 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1571 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1573 /* move to new hashtable entry */
1574 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1575 hlist_del(&zv
->zv_hlink
);
1576 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1579 * The block device's read-only state is briefly changed causing
1580 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1581 * the name change and fixes the symlinks. This does not change
1582 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1583 * changes. This would normally be done using kobject_uevent() but
1584 * that is a GPL-only symbol which is why we need this workaround.
1586 set_disk_ro(zv
->zv_zso
->zvo_disk
, !readonly
);
1587 set_disk_ro(zv
->zv_zso
->zvo_disk
, readonly
);
1589 dataset_kstats_rename(&zv
->zv_kstat
, newname
);
1593 zvol_os_set_disk_ro(zvol_state_t
*zv
, int flags
)
1596 set_disk_ro(zv
->zv_zso
->zvo_disk
, flags
);
1600 zvol_os_set_capacity(zvol_state_t
*zv
, uint64_t capacity
)
1603 set_capacity(zv
->zv_zso
->zvo_disk
, capacity
);
1612 * zvol_threads is the module param the user passes in.
1614 * zvol_actual_threads is what we use internally, since the user can
1615 * pass zvol_thread = 0 to mean "use all the CPUs" (the default).
1617 static unsigned int zvol_actual_threads
;
1619 if (zvol_threads
== 0) {
1621 * See dde9380a1 for why 32 was chosen here. This should
1622 * probably be refined to be some multiple of the number
1625 zvol_actual_threads
= MAX(num_online_cpus(), 32);
1627 zvol_actual_threads
= MIN(MAX(zvol_threads
, 1), 1024);
1631 * Use atleast 32 zvol_threads but for many core system,
1632 * prefer 6 threads per taskq, but no more taskqs
1633 * than threads in them on large systems.
1636 * cpus taskqs threads threads
1637 * ------- ------- ------- -------
1648 zv_taskq_t
*ztqs
= &zvol_taskqs
;
1649 uint_t num_tqs
= MIN(num_online_cpus(), zvol_num_taskqs
);
1651 num_tqs
= 1 + num_online_cpus() / 6;
1652 while (num_tqs
* num_tqs
> zvol_actual_threads
)
1655 uint_t per_tq_thread
= zvol_actual_threads
/ num_tqs
;
1656 if (per_tq_thread
* num_tqs
< zvol_actual_threads
)
1658 ztqs
->tqs_cnt
= num_tqs
;
1659 ztqs
->tqs_taskq
= kmem_alloc(num_tqs
* sizeof (taskq_t
*), KM_SLEEP
);
1660 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1662 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
* sizeof (taskq_t
*));
1663 ztqs
->tqs_taskq
= NULL
;
1664 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1669 if (zvol_blk_mq_queue_depth
== 0) {
1670 zvol_actual_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
1672 zvol_actual_blk_mq_queue_depth
=
1673 MAX(zvol_blk_mq_queue_depth
, BLKDEV_MIN_RQ
);
1676 if (zvol_blk_mq_threads
== 0) {
1677 zvol_blk_mq_actual_threads
= num_online_cpus();
1679 zvol_blk_mq_actual_threads
= MIN(MAX(zvol_blk_mq_threads
, 1),
1683 for (uint_t i
= 0; i
< num_tqs
; i
++) {
1685 (void) snprintf(name
, sizeof (name
), "%s_tq-%u",
1687 ztqs
->tqs_taskq
[i
] = taskq_create(name
, per_tq_thread
,
1688 maxclsyspri
, per_tq_thread
, INT_MAX
,
1689 TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1690 if (ztqs
->tqs_taskq
[i
] == NULL
) {
1691 for (int j
= i
- 1; j
>= 0; j
--)
1692 taskq_destroy(ztqs
->tqs_taskq
[j
]);
1693 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1694 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
*
1695 sizeof (taskq_t
*));
1696 ztqs
->tqs_taskq
= NULL
;
1702 ida_init(&zvol_ida
);
1709 zv_taskq_t
*ztqs
= &zvol_taskqs
;
1711 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1713 if (ztqs
->tqs_taskq
== NULL
) {
1714 ASSERT3U(ztqs
->tqs_cnt
, ==, 0);
1716 for (uint_t i
= 0; i
< ztqs
->tqs_cnt
; i
++) {
1717 ASSERT3P(ztqs
->tqs_taskq
[i
], !=, NULL
);
1718 taskq_destroy(ztqs
->tqs_taskq
[i
]);
1720 kmem_free(ztqs
->tqs_taskq
, ztqs
->tqs_cnt
*
1721 sizeof (taskq_t
*));
1722 ztqs
->tqs_taskq
= NULL
;
1725 ida_destroy(&zvol_ida
);
1729 module_param(zvol_inhibit_dev
, uint
, 0644);
1730 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1732 module_param(zvol_major
, uint
, 0444);
1733 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1735 module_param(zvol_threads
, uint
, 0444);
1736 MODULE_PARM_DESC(zvol_threads
, "Number of threads to handle I/O requests. Set"
1737 "to 0 to use all active CPUs");
1739 module_param(zvol_request_sync
, uint
, 0644);
1740 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
1742 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1743 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1745 module_param(zvol_num_taskqs
, uint
, 0444);
1746 MODULE_PARM_DESC(zvol_num_taskqs
, "Number of zvol taskqs");
1748 module_param(zvol_prefetch_bytes
, uint
, 0644);
1749 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
1751 module_param(zvol_volmode
, uint
, 0644);
1752 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");
1755 module_param(zvol_blk_mq_queue_depth
, uint
, 0644);
1756 MODULE_PARM_DESC(zvol_blk_mq_queue_depth
, "Default blk-mq queue depth");
1758 module_param(zvol_use_blk_mq
, uint
, 0644);
1759 MODULE_PARM_DESC(zvol_use_blk_mq
, "Use the blk-mq API for zvols");
1761 module_param(zvol_blk_mq_blocks_per_thread
, uint
, 0644);
1762 MODULE_PARM_DESC(zvol_blk_mq_blocks_per_thread
,
1763 "Process volblocksize blocks per thread");
1766 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
1767 module_param(zvol_open_timeout_ms
, uint
, 0644);
1768 MODULE_PARM_DESC(zvol_open_timeout_ms
, "Timeout for ZVOL open retries");