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>
41 #include <linux/blkdev_compat.h>
42 #include <linux/task_io_accounting_ops.h>
45 #include <linux/blk-mq.h>
48 static void zvol_request_impl(zvol_state_t
*zv
, struct bio
*bio
,
49 struct request
*rq
, boolean_t force_sync
);
51 static unsigned int zvol_major
= ZVOL_MAJOR
;
52 static unsigned int zvol_request_sync
= 0;
53 static unsigned int zvol_prefetch_bytes
= (128 * 1024);
54 static unsigned long zvol_max_discard_blocks
= 16384;
56 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
57 static unsigned int zvol_open_timeout_ms
= 1000;
60 static unsigned int zvol_threads
= 0;
62 static unsigned int zvol_blk_mq_threads
= 0;
63 static unsigned int zvol_blk_mq_actual_threads
;
64 static boolean_t zvol_use_blk_mq
= B_FALSE
;
67 * The maximum number of volblocksize blocks to process per thread. Typically,
68 * write heavy workloads preform better with higher values here, and read
69 * heavy workloads preform better with lower values, but that's not a hard
70 * and fast rule. It's basically a knob to tune between "less overhead with
71 * less parallelism" and "more overhead, but more parallelism".
73 * '8' was chosen as a reasonable, balanced, default based off of sequential
74 * read and write tests to a zvol in an NVMe pool (with 16 CPUs).
76 static unsigned int zvol_blk_mq_blocks_per_thread
= 8;
79 #ifndef BLKDEV_DEFAULT_RQ
80 /* BLKDEV_MAX_RQ was renamed to BLKDEV_DEFAULT_RQ in the 5.16 kernel */
81 #define BLKDEV_DEFAULT_RQ BLKDEV_MAX_RQ
85 * Finalize our BIO or request.
88 #define END_IO(zv, bio, rq, error) do { \
90 BIO_END_IO(bio, error); \
92 blk_mq_end_request(rq, errno_to_bi_status(error)); \
96 #define END_IO(zv, bio, rq, error) BIO_END_IO(bio, error)
100 static unsigned int zvol_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
101 static unsigned int zvol_actual_blk_mq_queue_depth
;
104 struct zvol_state_os
{
105 struct gendisk
*zvo_disk
; /* generic disk */
106 struct request_queue
*zvo_queue
; /* request queue */
107 dev_t zvo_dev
; /* device id */
110 struct blk_mq_tag_set tag_set
;
113 /* Set from the global 'zvol_use_blk_mq' at zvol load */
114 boolean_t use_blk_mq
;
117 static taskq_t
*zvol_taskq
;
118 static struct ida zvol_ida
;
120 typedef struct zv_request_stack
{
126 typedef struct zv_work
{
128 struct work_struct work
;
131 typedef struct zv_request_task
{
136 static zv_request_task_t
*
137 zv_request_task_create(zv_request_t zvr
)
139 zv_request_task_t
*task
;
140 task
= kmem_alloc(sizeof (zv_request_task_t
), KM_SLEEP
);
141 taskq_init_ent(&task
->ent
);
147 zv_request_task_free(zv_request_task_t
*task
)
149 kmem_free(task
, sizeof (*task
));
155 * This is called when a new block multiqueue request comes in. A request
156 * contains one or more BIOs.
158 static blk_status_t
zvol_mq_queue_rq(struct blk_mq_hw_ctx
*hctx
,
159 const struct blk_mq_queue_data
*bd
)
161 struct request
*rq
= bd
->rq
;
162 zvol_state_t
*zv
= rq
->q
->queuedata
;
164 /* Tell the kernel that we are starting to process this request */
165 blk_mq_start_request(rq
);
167 if (blk_rq_is_passthrough(rq
)) {
168 /* Skip non filesystem request */
169 blk_mq_end_request(rq
, BLK_STS_IOERR
);
170 return (BLK_STS_IOERR
);
173 zvol_request_impl(zv
, NULL
, rq
, 0);
175 /* Acknowledge to the kernel that we got this request */
179 static struct blk_mq_ops zvol_blk_mq_queue_ops
= {
180 .queue_rq
= zvol_mq_queue_rq
,
183 /* Initialize our blk-mq struct */
184 static int zvol_blk_mq_alloc_tag_set(zvol_state_t
*zv
)
186 struct zvol_state_os
*zso
= zv
->zv_zso
;
188 memset(&zso
->tag_set
, 0, sizeof (zso
->tag_set
));
190 /* Initialize tag set. */
191 zso
->tag_set
.ops
= &zvol_blk_mq_queue_ops
;
192 zso
->tag_set
.nr_hw_queues
= zvol_blk_mq_actual_threads
;
193 zso
->tag_set
.queue_depth
= zvol_actual_blk_mq_queue_depth
;
194 zso
->tag_set
.numa_node
= NUMA_NO_NODE
;
195 zso
->tag_set
.cmd_size
= 0;
198 * We need BLK_MQ_F_BLOCKING here since we do blocking calls in
199 * zvol_request_impl()
201 zso
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_BLOCKING
;
202 zso
->tag_set
.driver_data
= zv
;
204 return (blk_mq_alloc_tag_set(&zso
->tag_set
));
206 #endif /* HAVE_BLK_MQ */
209 * Given a path, return TRUE if path is a ZVOL.
212 zvol_os_is_zvol(const char *path
)
216 if (vdev_lookup_bdev(path
, &dev
) != 0)
219 if (MAJOR(dev
) == zvol_major
)
226 zvol_write(zv_request_t
*zvr
)
228 struct bio
*bio
= zvr
->bio
;
229 struct request
*rq
= zvr
->rq
;
232 zvol_state_t
*zv
= zvr
->zv
;
233 struct request_queue
*q
;
234 struct gendisk
*disk
;
235 unsigned long start_time
= 0;
236 boolean_t acct
= B_FALSE
;
238 ASSERT3P(zv
, !=, NULL
);
239 ASSERT3U(zv
->zv_open_count
, >, 0);
240 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
242 q
= zv
->zv_zso
->zvo_queue
;
243 disk
= zv
->zv_zso
->zvo_disk
;
245 /* bio marked as FLUSH need to flush before write */
246 if (io_is_flush(bio
, rq
))
247 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
249 /* Some requests are just for flush and nothing else. */
250 if (io_size(bio
, rq
) == 0) {
251 rw_exit(&zv
->zv_suspend_lock
);
252 END_IO(zv
, bio
, rq
, 0);
256 zfs_uio_bvec_init(&uio
, bio
, rq
);
258 ssize_t start_resid
= uio
.uio_resid
;
261 * With use_blk_mq, accounting is done by blk_mq_start_request()
262 * and blk_mq_end_request(), so we can skip it here.
265 acct
= blk_queue_io_stat(q
);
267 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
273 io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
275 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
276 uio
.uio_loffset
, uio
.uio_resid
, RL_WRITER
);
278 uint64_t volsize
= zv
->zv_volsize
;
279 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
280 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
281 uint64_t off
= uio
.uio_loffset
;
282 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
284 if (bytes
> volsize
- off
) /* don't write past the end */
285 bytes
= volsize
- off
;
287 dmu_tx_hold_write_by_dnode(tx
, zv
->zv_dn
, off
, bytes
);
289 /* This will only fail for ENOSPC */
290 error
= dmu_tx_assign(tx
, TXG_WAIT
);
295 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
297 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
304 zfs_rangelock_exit(lr
);
306 int64_t nwritten
= start_resid
- uio
.uio_resid
;
307 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
308 task_io_account_write(nwritten
);
311 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
313 rw_exit(&zv
->zv_suspend_lock
);
316 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
, start_time
);
319 END_IO(zv
, bio
, rq
, -error
);
323 zvol_write_task(void *arg
)
325 zv_request_task_t
*task
= arg
;
326 zvol_write(&task
->zvr
);
327 zv_request_task_free(task
);
331 zvol_discard(zv_request_t
*zvr
)
333 struct bio
*bio
= zvr
->bio
;
334 struct request
*rq
= zvr
->rq
;
335 zvol_state_t
*zv
= zvr
->zv
;
336 uint64_t start
= io_offset(bio
, rq
);
337 uint64_t size
= io_size(bio
, rq
);
338 uint64_t end
= start
+ size
;
342 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
343 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
344 unsigned long start_time
= 0;
345 boolean_t acct
= B_FALSE
;
347 ASSERT3P(zv
, !=, NULL
);
348 ASSERT3U(zv
->zv_open_count
, >, 0);
349 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
352 acct
= blk_queue_io_stat(q
);
354 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
,
359 sync
= io_is_fua(bio
, rq
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
361 if (end
> zv
->zv_volsize
) {
362 error
= SET_ERROR(EIO
);
367 * Align the request to volume block boundaries when a secure erase is
368 * not required. This will prevent dnode_free_range() from zeroing out
369 * the unaligned parts which is slow (read-modify-write) and useless
370 * since we are not freeing any space by doing so.
372 if (!io_is_secure_erase(bio
, rq
)) {
373 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
374 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
381 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
382 start
, size
, RL_WRITER
);
384 tx
= dmu_tx_create(zv
->zv_objset
);
385 dmu_tx_mark_netfree(tx
);
386 error
= dmu_tx_assign(tx
, TXG_WAIT
);
390 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
392 error
= dmu_free_long_range(zv
->zv_objset
,
393 ZVOL_OBJ
, start
, size
);
395 zfs_rangelock_exit(lr
);
397 if (error
== 0 && sync
)
398 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
401 rw_exit(&zv
->zv_suspend_lock
);
404 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
,
408 END_IO(zv
, bio
, rq
, -error
);
412 zvol_discard_task(void *arg
)
414 zv_request_task_t
*task
= arg
;
415 zvol_discard(&task
->zvr
);
416 zv_request_task_free(task
);
420 zvol_read(zv_request_t
*zvr
)
422 struct bio
*bio
= zvr
->bio
;
423 struct request
*rq
= zvr
->rq
;
426 boolean_t acct
= B_FALSE
;
427 zvol_state_t
*zv
= zvr
->zv
;
428 struct request_queue
*q
;
429 struct gendisk
*disk
;
430 unsigned long start_time
= 0;
432 ASSERT3P(zv
, !=, NULL
);
433 ASSERT3U(zv
->zv_open_count
, >, 0);
435 zfs_uio_bvec_init(&uio
, bio
, rq
);
437 q
= zv
->zv_zso
->zvo_queue
;
438 disk
= zv
->zv_zso
->zvo_disk
;
440 ssize_t start_resid
= uio
.uio_resid
;
443 * When blk-mq is being used, accounting is done by
444 * blk_mq_start_request() and blk_mq_end_request().
447 acct
= blk_queue_io_stat(q
);
449 start_time
= blk_generic_start_io_acct(q
, disk
, READ
,
453 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
454 uio
.uio_loffset
, uio
.uio_resid
, RL_READER
);
456 uint64_t volsize
= zv
->zv_volsize
;
458 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
459 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
461 /* don't read past the end */
462 if (bytes
> volsize
- uio
.uio_loffset
)
463 bytes
= volsize
- uio
.uio_loffset
;
465 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
467 /* convert checksum errors into IO errors */
469 error
= SET_ERROR(EIO
);
473 zfs_rangelock_exit(lr
);
475 int64_t nread
= start_resid
- uio
.uio_resid
;
476 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
477 task_io_account_read(nread
);
479 rw_exit(&zv
->zv_suspend_lock
);
482 blk_generic_end_io_acct(q
, disk
, READ
, bio
, start_time
);
485 END_IO(zv
, bio
, rq
, -error
);
489 zvol_read_task(void *arg
)
491 zv_request_task_t
*task
= arg
;
492 zvol_read(&task
->zvr
);
493 zv_request_task_free(task
);
498 * Process a BIO or request
500 * Either 'bio' or 'rq' should be set depending on if we are processing a
501 * bio or a request (both should not be set).
503 * force_sync: Set to 0 to defer processing to a background taskq
504 * Set to 1 to process data synchronously
507 zvol_request_impl(zvol_state_t
*zv
, struct bio
*bio
, struct request
*rq
,
508 boolean_t force_sync
)
510 fstrans_cookie_t cookie
= spl_fstrans_mark();
511 uint64_t offset
= io_offset(bio
, rq
);
512 uint64_t size
= io_size(bio
, rq
);
513 int rw
= io_data_dir(bio
, rq
);
515 if (zvol_request_sync
)
524 if (io_has_data(bio
, rq
) && offset
+ size
> zv
->zv_volsize
) {
525 printk(KERN_INFO
"%s: bad access: offset=%llu, size=%lu\n",
526 zv
->zv_zso
->zvo_disk
->disk_name
,
527 (long long unsigned)offset
,
528 (long unsigned)size
);
530 END_IO(zv
, bio
, rq
, -SET_ERROR(EIO
));
534 zv_request_task_t
*task
;
537 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
538 END_IO(zv
, bio
, rq
, -SET_ERROR(EROFS
));
543 * Prevents the zvol from being suspended, or the ZIL being
544 * concurrently opened. Will be released after the i/o
547 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
550 * Open a ZIL if this is the first time we have written to this
551 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
552 * than zv_state_lock so that we don't need to acquire an
553 * additional lock in this path.
555 if (zv
->zv_zilog
== NULL
) {
556 rw_exit(&zv
->zv_suspend_lock
);
557 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
558 if (zv
->zv_zilog
== NULL
) {
559 zv
->zv_zilog
= zil_open(zv
->zv_objset
,
560 zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
561 zv
->zv_flags
|= ZVOL_WRITTEN_TO
;
562 /* replay / destroy done in zvol_create_minor */
563 VERIFY0((zv
->zv_zilog
->zl_header
->zh_flags
&
566 rw_downgrade(&zv
->zv_suspend_lock
);
570 * We don't want this thread to be blocked waiting for i/o to
571 * complete, so we instead wait from a taskq callback. The
572 * i/o may be a ZIL write (via zil_commit()), or a read of an
573 * indirect block, or a read of a data block (if this is a
574 * partial-block write). We will indicate that the i/o is
575 * complete by calling END_IO() from the taskq callback.
577 * This design allows the calling thread to continue and
578 * initiate more concurrent operations by calling
579 * zvol_request() again. There are typically only a small
580 * number of threads available to call zvol_request() (e.g.
581 * one per iSCSI target), so keeping the latency of
582 * zvol_request() low is important for performance.
584 * The zvol_request_sync module parameter allows this
585 * behavior to be altered, for performance evaluation
586 * purposes. If the callback blocks, setting
587 * zvol_request_sync=1 will result in much worse performance.
589 * We can have up to zvol_threads concurrent i/o's being
590 * processed for all zvols on the system. This is typically
591 * a vast improvement over the zvol_request_sync=1 behavior
592 * of one i/o at a time per zvol. However, an even better
593 * design would be for zvol_request() to initiate the zio
594 * directly, and then be notified by the zio_done callback,
595 * which would call END_IO(). Unfortunately, the DMU/ZIL
596 * interfaces lack this functionality (they block waiting for
597 * the i/o to complete).
599 if (io_is_discard(bio
, rq
) || io_is_secure_erase(bio
, rq
)) {
603 task
= zv_request_task_create(zvr
);
604 taskq_dispatch_ent(zvol_taskq
,
605 zvol_discard_task
, task
, 0, &task
->ent
);
611 task
= zv_request_task_create(zvr
);
612 taskq_dispatch_ent(zvol_taskq
,
613 zvol_write_task
, task
, 0, &task
->ent
);
618 * The SCST driver, and possibly others, may issue READ I/Os
619 * with a length of zero bytes. These empty I/Os contain no
620 * data and require no additional handling.
623 END_IO(zv
, bio
, rq
, 0);
627 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
629 /* See comment in WRITE case above. */
633 task
= zv_request_task_create(zvr
);
634 taskq_dispatch_ent(zvol_taskq
,
635 zvol_read_task
, task
, 0, &task
->ent
);
640 spl_fstrans_unmark(cookie
);
643 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
644 #ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
646 zvol_submit_bio(struct bio
*bio
)
649 zvol_submit_bio(struct bio
*bio
)
652 static MAKE_REQUEST_FN_RET
653 zvol_request(struct request_queue
*q
, struct bio
*bio
)
656 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
657 #if defined(HAVE_BIO_BDEV_DISK)
658 struct request_queue
*q
= bio
->bi_bdev
->bd_disk
->queue
;
660 struct request_queue
*q
= bio
->bi_disk
->queue
;
663 zvol_state_t
*zv
= q
->queuedata
;
665 zvol_request_impl(zv
, bio
, NULL
, 0);
666 #if defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
667 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
668 !defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
669 return (BLK_QC_T_NONE
);
674 #ifdef HAVE_BLK_MODE_T
675 zvol_open(struct gendisk
*disk
, blk_mode_t flag
)
677 zvol_open(struct block_device
*bdev
, fmode_t flag
)
682 boolean_t drop_suspend
= B_FALSE
;
683 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
684 hrtime_t timeout
= MSEC2NSEC(zvol_open_timeout_ms
);
685 hrtime_t start
= gethrtime();
689 rw_enter(&zvol_state_lock
, RW_READER
);
691 * Obtain a copy of private_data under the zvol_state_lock to make
692 * sure that either the result of zvol free code path setting
693 * disk->private_data to NULL is observed, or zvol_os_free()
694 * is not called on this zv because of the positive zv_open_count.
696 #ifdef HAVE_BLK_MODE_T
697 zv
= disk
->private_data
;
699 zv
= bdev
->bd_disk
->private_data
;
702 rw_exit(&zvol_state_lock
);
703 return (SET_ERROR(-ENXIO
));
706 mutex_enter(&zv
->zv_state_lock
);
708 * Make sure zvol is not suspended during first open
709 * (hold zv_suspend_lock) and respect proper lock acquisition
710 * ordering - zv_suspend_lock before zv_state_lock
712 if (zv
->zv_open_count
== 0) {
713 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
714 mutex_exit(&zv
->zv_state_lock
);
715 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
716 mutex_enter(&zv
->zv_state_lock
);
717 /* check to see if zv_suspend_lock is needed */
718 if (zv
->zv_open_count
!= 0) {
719 rw_exit(&zv
->zv_suspend_lock
);
721 drop_suspend
= B_TRUE
;
724 drop_suspend
= B_TRUE
;
727 rw_exit(&zvol_state_lock
);
729 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
731 if (zv
->zv_open_count
== 0) {
732 boolean_t drop_namespace
= B_FALSE
;
734 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
737 * In all other call paths the spa_namespace_lock is taken
738 * before the bdev->bd_mutex lock. However, on open(2)
739 * the __blkdev_get() function calls fops->open() with the
740 * bdev->bd_mutex lock held. This can result in a deadlock
741 * when zvols from one pool are used as vdevs in another.
743 * To prevent a lock inversion deadlock we preemptively
744 * take the spa_namespace_lock. Normally the lock will not
745 * be contended and this is safe because spa_open_common()
746 * handles the case where the caller already holds the
747 * spa_namespace_lock.
749 * When the lock cannot be aquired after multiple retries
750 * this must be the vdev on zvol deadlock case and we have
751 * no choice but to return an error. For 5.12 and older
752 * kernels returning -ERESTARTSYS will result in the
753 * bdev->bd_mutex being dropped, then reacquired, and
754 * fops->open() being called again. This process can be
755 * repeated safely until both locks are acquired. For 5.13
756 * and newer the -ERESTARTSYS retry logic was removed from
757 * the kernel so the only option is to return the error for
758 * the caller to handle it.
760 if (!mutex_owned(&spa_namespace_lock
)) {
761 if (!mutex_tryenter(&spa_namespace_lock
)) {
762 mutex_exit(&zv
->zv_state_lock
);
763 rw_exit(&zv
->zv_suspend_lock
);
765 #ifdef HAVE_BLKDEV_GET_ERESTARTSYS
767 return (SET_ERROR(-ERESTARTSYS
));
769 if ((gethrtime() - start
) > timeout
)
770 return (SET_ERROR(-ERESTARTSYS
));
772 schedule_timeout(MSEC_TO_TICK(10));
776 drop_namespace
= B_TRUE
;
780 error
= -zvol_first_open(zv
, !(blk_mode_is_open_write(flag
)));
783 mutex_exit(&spa_namespace_lock
);
787 if ((blk_mode_is_open_write(flag
)) &&
788 (zv
->zv_flags
& ZVOL_RDONLY
)) {
789 if (zv
->zv_open_count
== 0)
792 error
= SET_ERROR(-EROFS
);
798 mutex_exit(&zv
->zv_state_lock
);
800 rw_exit(&zv
->zv_suspend_lock
);
803 #ifdef HAVE_BLK_MODE_T
804 disk_check_media_change(disk
);
806 zfs_check_media_change(bdev
);
813 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG
814 zvol_release(struct gendisk
*disk
)
816 zvol_release(struct gendisk
*disk
, fmode_t unused
)
819 #if !defined(HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG)
823 boolean_t drop_suspend
= B_TRUE
;
825 rw_enter(&zvol_state_lock
, RW_READER
);
826 zv
= disk
->private_data
;
828 mutex_enter(&zv
->zv_state_lock
);
829 ASSERT3U(zv
->zv_open_count
, >, 0);
831 * make sure zvol is not suspended during last close
832 * (hold zv_suspend_lock) and respect proper lock acquisition
833 * ordering - zv_suspend_lock before zv_state_lock
835 if (zv
->zv_open_count
== 1) {
836 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
837 mutex_exit(&zv
->zv_state_lock
);
838 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
839 mutex_enter(&zv
->zv_state_lock
);
840 /* check to see if zv_suspend_lock is needed */
841 if (zv
->zv_open_count
!= 1) {
842 rw_exit(&zv
->zv_suspend_lock
);
843 drop_suspend
= B_FALSE
;
847 drop_suspend
= B_FALSE
;
849 rw_exit(&zvol_state_lock
);
851 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
854 if (zv
->zv_open_count
== 0) {
855 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
859 mutex_exit(&zv
->zv_state_lock
);
862 rw_exit(&zv
->zv_suspend_lock
);
866 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
867 unsigned int cmd
, unsigned long arg
)
869 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
872 ASSERT3U(zv
->zv_open_count
, >, 0);
876 #ifdef HAVE_FSYNC_BDEV
878 #elif defined(HAVE_SYNC_BLOCKDEV)
881 #error "Neither fsync_bdev() nor sync_blockdev() found"
883 invalidate_bdev(bdev
);
884 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
886 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
887 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
889 rw_exit(&zv
->zv_suspend_lock
);
893 mutex_enter(&zv
->zv_state_lock
);
894 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
895 mutex_exit(&zv
->zv_state_lock
);
903 return (SET_ERROR(error
));
908 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
909 unsigned cmd
, unsigned long arg
)
911 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
914 #define zvol_compat_ioctl NULL
918 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
920 unsigned int mask
= 0;
922 rw_enter(&zvol_state_lock
, RW_READER
);
924 zvol_state_t
*zv
= disk
->private_data
;
926 mutex_enter(&zv
->zv_state_lock
);
927 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
929 mutex_exit(&zv
->zv_state_lock
);
932 rw_exit(&zvol_state_lock
);
938 zvol_revalidate_disk(struct gendisk
*disk
)
940 rw_enter(&zvol_state_lock
, RW_READER
);
942 zvol_state_t
*zv
= disk
->private_data
;
944 mutex_enter(&zv
->zv_state_lock
);
945 set_capacity(zv
->zv_zso
->zvo_disk
,
946 zv
->zv_volsize
>> SECTOR_BITS
);
947 mutex_exit(&zv
->zv_state_lock
);
950 rw_exit(&zvol_state_lock
);
956 zvol_os_update_volsize(zvol_state_t
*zv
, uint64_t volsize
)
958 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
960 #if defined(HAVE_REVALIDATE_DISK_SIZE)
961 revalidate_disk_size(disk
, zvol_revalidate_disk(disk
) == 0);
962 #elif defined(HAVE_REVALIDATE_DISK)
963 revalidate_disk(disk
);
965 zvol_revalidate_disk(disk
);
971 zvol_os_clear_private(zvol_state_t
*zv
)
974 * Cleared while holding zvol_state_lock as a writer
975 * which will prevent zvol_open() from opening it.
977 zv
->zv_zso
->zvo_disk
->private_data
= NULL
;
981 * Provide a simple virtual geometry for legacy compatibility. For devices
982 * smaller than 1 MiB a small head and sector count is used to allow very
983 * tiny devices. For devices over 1 Mib a standard head and sector count
984 * is used to keep the cylinders count reasonable.
987 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
989 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
992 ASSERT3U(zv
->zv_open_count
, >, 0);
994 sectors
= get_capacity(zv
->zv_zso
->zvo_disk
);
996 if (sectors
> 2048) {
1005 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1011 * Why have two separate block_device_operations structs?
1013 * Normally we'd just have one, and assign 'submit_bio' as needed. However,
1014 * it's possible the user's kernel is built with CONSTIFY_PLUGIN, meaning we
1015 * can't just change submit_bio dynamically at runtime. So just create two
1016 * separate structs to get around this.
1018 static const struct block_device_operations zvol_ops_blk_mq
= {
1020 .release
= zvol_release
,
1021 .ioctl
= zvol_ioctl
,
1022 .compat_ioctl
= zvol_compat_ioctl
,
1023 .check_events
= zvol_check_events
,
1024 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1025 .revalidate_disk
= zvol_revalidate_disk
,
1027 .getgeo
= zvol_getgeo
,
1028 .owner
= THIS_MODULE
,
1031 static const struct block_device_operations zvol_ops
= {
1033 .release
= zvol_release
,
1034 .ioctl
= zvol_ioctl
,
1035 .compat_ioctl
= zvol_compat_ioctl
,
1036 .check_events
= zvol_check_events
,
1037 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1038 .revalidate_disk
= zvol_revalidate_disk
,
1040 .getgeo
= zvol_getgeo
,
1041 .owner
= THIS_MODULE
,
1042 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1043 .submit_bio
= zvol_submit_bio
,
1048 zvol_alloc_non_blk_mq(struct zvol_state_os
*zso
)
1050 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)
1051 #if defined(HAVE_BLK_ALLOC_DISK)
1052 zso
->zvo_disk
= blk_alloc_disk(NUMA_NO_NODE
);
1053 if (zso
->zvo_disk
== NULL
)
1056 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1057 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1059 zso
->zvo_queue
= blk_alloc_queue(NUMA_NO_NODE
);
1060 if (zso
->zvo_queue
== NULL
)
1063 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1064 if (zso
->zvo_disk
== NULL
) {
1065 blk_cleanup_queue(zso
->zvo_queue
);
1069 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1070 #endif /* HAVE_BLK_ALLOC_DISK */
1072 zso
->zvo_queue
= blk_generic_alloc_queue(zvol_request
, NUMA_NO_NODE
);
1073 if (zso
->zvo_queue
== NULL
)
1076 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1077 if (zso
->zvo_disk
== NULL
) {
1078 blk_cleanup_queue(zso
->zvo_queue
);
1082 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1083 #endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
1089 zvol_alloc_blk_mq(zvol_state_t
*zv
)
1092 struct zvol_state_os
*zso
= zv
->zv_zso
;
1094 /* Allocate our blk-mq tag_set */
1095 if (zvol_blk_mq_alloc_tag_set(zv
) != 0)
1098 #if defined(HAVE_BLK_ALLOC_DISK)
1099 zso
->zvo_disk
= blk_mq_alloc_disk(&zso
->tag_set
, zv
);
1100 if (zso
->zvo_disk
== NULL
) {
1101 blk_mq_free_tag_set(&zso
->tag_set
);
1104 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
1105 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
1107 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
1108 if (zso
->zvo_disk
== NULL
) {
1109 blk_cleanup_queue(zso
->zvo_queue
);
1110 blk_mq_free_tag_set(&zso
->tag_set
);
1113 /* Allocate queue */
1114 zso
->zvo_queue
= blk_mq_init_queue(&zso
->tag_set
);
1115 if (IS_ERR(zso
->zvo_queue
)) {
1116 blk_mq_free_tag_set(&zso
->tag_set
);
1120 /* Our queue is now created, assign it to our disk */
1121 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
1129 * Allocate memory for a new zvol_state_t and setup the required
1130 * request queue and generic disk structures for the block device.
1132 static zvol_state_t
*
1133 zvol_alloc(dev_t dev
, const char *name
)
1136 struct zvol_state_os
*zso
;
1140 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
1143 if (volmode
== ZFS_VOLMODE_DEFAULT
)
1144 volmode
= zvol_volmode
;
1146 if (volmode
== ZFS_VOLMODE_NONE
)
1149 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1150 zso
= kmem_zalloc(sizeof (struct zvol_state_os
), KM_SLEEP
);
1152 zv
->zv_volmode
= volmode
;
1154 list_link_init(&zv
->zv_next
);
1155 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1158 zv
->zv_zso
->use_blk_mq
= zvol_use_blk_mq
;
1162 * The block layer has 3 interfaces for getting BIOs:
1164 * 1. blk-mq request queues (new)
1165 * 2. submit_bio() (oldest)
1166 * 3. regular request queues (old).
1168 * Each of those interfaces has two permutations:
1170 * a) We have blk_alloc_disk()/blk_mq_alloc_disk(), which allocates
1171 * both the disk and its queue (5.14 kernel or newer)
1173 * b) We don't have blk_*alloc_disk(), and have to allocate the
1174 * disk and the queue separately. (5.13 kernel or older)
1176 if (zv
->zv_zso
->use_blk_mq
) {
1177 ret
= zvol_alloc_blk_mq(zv
);
1178 zso
->zvo_disk
->fops
= &zvol_ops_blk_mq
;
1180 ret
= zvol_alloc_non_blk_mq(zso
);
1181 zso
->zvo_disk
->fops
= &zvol_ops
;
1186 blk_queue_set_write_cache(zso
->zvo_queue
, B_TRUE
, B_TRUE
);
1188 /* Limit read-ahead to a single page to prevent over-prefetching. */
1189 blk_queue_set_read_ahead(zso
->zvo_queue
, 1);
1191 if (!zv
->zv_zso
->use_blk_mq
) {
1192 /* Disable write merging in favor of the ZIO pipeline. */
1193 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zso
->zvo_queue
);
1196 /* Enable /proc/diskstats */
1197 blk_queue_flag_set(QUEUE_FLAG_IO_STAT
, zso
->zvo_queue
);
1199 zso
->zvo_queue
->queuedata
= zv
;
1201 zv
->zv_open_count
= 0;
1202 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1204 zfs_rangelock_init(&zv
->zv_rangelock
, NULL
, NULL
);
1205 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
1207 zso
->zvo_disk
->major
= zvol_major
;
1208 zso
->zvo_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
1211 * Setting ZFS_VOLMODE_DEV disables partitioning on ZVOL devices.
1212 * This is accomplished by limiting the number of minors for the
1213 * device to one and explicitly disabling partition scanning.
1215 if (volmode
== ZFS_VOLMODE_DEV
) {
1216 zso
->zvo_disk
->minors
= 1;
1217 zso
->zvo_disk
->flags
&= ~ZFS_GENHD_FL_EXT_DEVT
;
1218 zso
->zvo_disk
->flags
|= ZFS_GENHD_FL_NO_PART
;
1221 zso
->zvo_disk
->first_minor
= (dev
& MINORMASK
);
1222 zso
->zvo_disk
->private_data
= zv
;
1223 snprintf(zso
->zvo_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1224 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1229 kmem_free(zso
, sizeof (struct zvol_state_os
));
1230 kmem_free(zv
, sizeof (zvol_state_t
));
1235 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1236 * At this time, the structure is not opened by anyone, is taken off
1237 * the zvol_state_list, and has its private data set to NULL.
1238 * The zvol_state_lock is dropped.
1240 * This function may take many milliseconds to complete (e.g. we've seen
1241 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
1242 * "del_gendisk". Thus, consumers need to be careful to account for this
1243 * latency when calling this function.
1246 zvol_os_free(zvol_state_t
*zv
)
1249 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
1250 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
1251 ASSERT0(zv
->zv_open_count
);
1252 ASSERT3P(zv
->zv_zso
->zvo_disk
->private_data
, ==, NULL
);
1254 rw_destroy(&zv
->zv_suspend_lock
);
1255 zfs_rangelock_fini(&zv
->zv_rangelock
);
1257 del_gendisk(zv
->zv_zso
->zvo_disk
);
1258 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
1259 defined(HAVE_BLK_ALLOC_DISK)
1260 #if defined(HAVE_BLK_CLEANUP_DISK)
1261 blk_cleanup_disk(zv
->zv_zso
->zvo_disk
);
1263 put_disk(zv
->zv_zso
->zvo_disk
);
1266 blk_cleanup_queue(zv
->zv_zso
->zvo_queue
);
1267 put_disk(zv
->zv_zso
->zvo_disk
);
1271 if (zv
->zv_zso
->use_blk_mq
)
1272 blk_mq_free_tag_set(&zv
->zv_zso
->tag_set
);
1275 ida_simple_remove(&zvol_ida
,
1276 MINOR(zv
->zv_zso
->zvo_dev
) >> ZVOL_MINOR_BITS
);
1278 mutex_destroy(&zv
->zv_state_lock
);
1279 dataset_kstats_destroy(&zv
->zv_kstat
);
1281 kmem_free(zv
->zv_zso
, sizeof (struct zvol_state_os
));
1282 kmem_free(zv
, sizeof (zvol_state_t
));
1286 zvol_wait_close(zvol_state_t
*zv
)
1291 * Create a block device minor node and setup the linkage between it
1292 * and the specified volume. Once this function returns the block
1293 * device is live and ready for use.
1296 zvol_os_create_minor(const char *name
)
1300 dmu_object_info_t
*doi
;
1306 uint64_t hash
= zvol_name_hash(name
);
1307 bool replayed_zil
= B_FALSE
;
1309 if (zvol_inhibit_dev
)
1312 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1314 return (SET_ERROR(-idx
));
1315 minor
= idx
<< ZVOL_MINOR_BITS
;
1317 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
1319 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1320 mutex_exit(&zv
->zv_state_lock
);
1321 ida_simple_remove(&zvol_ida
, idx
);
1322 return (SET_ERROR(EEXIST
));
1325 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1327 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
1331 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1333 goto out_dmu_objset_disown
;
1335 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1337 goto out_dmu_objset_disown
;
1339 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1341 error
= SET_ERROR(EAGAIN
);
1342 goto out_dmu_objset_disown
;
1346 if (dmu_objset_is_snapshot(os
))
1347 zv
->zv_flags
|= ZVOL_RDONLY
;
1349 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1350 zv
->zv_volsize
= volsize
;
1353 set_capacity(zv
->zv_zso
->zvo_disk
, zv
->zv_volsize
>> 9);
1355 blk_queue_max_hw_sectors(zv
->zv_zso
->zvo_queue
,
1356 (DMU_MAX_ACCESS
/ 4) >> 9);
1358 if (zv
->zv_zso
->use_blk_mq
) {
1360 * IO requests can be really big (1MB). When an IO request
1361 * comes in, it is passed off to zvol_read() or zvol_write()
1362 * in a new thread, where it is chunked up into 'volblocksize'
1363 * sized pieces and processed. So for example, if the request
1364 * is a 1MB write and your volblocksize is 128k, one zvol_write
1365 * thread will take that request and sequentially do ten 128k
1366 * IOs. This is due to the fact that the thread needs to lock
1367 * each volblocksize sized block. So you might be wondering:
1368 * "instead of passing the whole 1MB request to one thread,
1369 * why not pass ten individual 128k chunks to ten threads and
1370 * process the whole write in parallel?" The short answer is
1371 * that there's a sweet spot number of chunks that balances
1372 * the greater parallelism with the added overhead of more
1373 * threads. The sweet spot can be different depending on if you
1374 * have a read or write heavy workload. Writes typically want
1375 * high chunk counts while reads typically want lower ones. On
1376 * a test pool with 6 NVMe drives in a 3x 2-disk mirror
1377 * configuration, with volblocksize=8k, the sweet spot for good
1378 * sequential reads and writes was at 8 chunks.
1382 * Below we tell the kernel how big we want our requests
1383 * to be. You would think that blk_queue_io_opt() would be
1384 * used to do this since it is used to "set optimal request
1385 * size for the queue", but that doesn't seem to do
1386 * anything - the kernel still gives you huge requests
1387 * with tons of little PAGE_SIZE segments contained within it.
1389 * Knowing that the kernel will just give you PAGE_SIZE segments
1390 * no matter what, you can say "ok, I want PAGE_SIZE byte
1391 * segments, and I want 'N' of them per request", where N is
1392 * the correct number of segments for the volblocksize and
1393 * number of chunks you want.
1396 if (zvol_blk_mq_blocks_per_thread
!= 0) {
1397 unsigned int chunks
;
1398 chunks
= MIN(zvol_blk_mq_blocks_per_thread
, UINT16_MAX
);
1400 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
,
1402 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
,
1403 (zv
->zv_volblocksize
* chunks
) / PAGE_SIZE
);
1406 * Special case: zvol_blk_mq_blocks_per_thread = 0
1407 * Max everything out.
1409 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
,
1411 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
,
1416 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
, UINT16_MAX
);
1417 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
, UINT_MAX
);
1420 blk_queue_physical_block_size(zv
->zv_zso
->zvo_queue
,
1421 zv
->zv_volblocksize
);
1422 blk_queue_io_opt(zv
->zv_zso
->zvo_queue
, zv
->zv_volblocksize
);
1423 blk_queue_max_discard_sectors(zv
->zv_zso
->zvo_queue
,
1424 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1425 blk_queue_discard_granularity(zv
->zv_zso
->zvo_queue
,
1426 zv
->zv_volblocksize
);
1427 #ifdef QUEUE_FLAG_DISCARD
1428 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_zso
->zvo_queue
);
1430 #ifdef QUEUE_FLAG_NONROT
1431 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_zso
->zvo_queue
);
1433 #ifdef QUEUE_FLAG_ADD_RANDOM
1434 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_zso
->zvo_queue
);
1436 /* This flag was introduced in kernel version 4.12. */
1437 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1438 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH
, zv
->zv_zso
->zvo_queue
);
1441 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
1442 error
= dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
1444 goto out_dmu_objset_disown
;
1445 ASSERT3P(zv
->zv_zilog
, ==, NULL
);
1446 zv
->zv_zilog
= zil_open(os
, zvol_get_data
, &zv
->zv_kstat
.dk_zil_sums
);
1447 if (spa_writeable(dmu_objset_spa(os
))) {
1448 if (zil_replay_disable
)
1449 replayed_zil
= zil_destroy(zv
->zv_zilog
, B_FALSE
);
1451 replayed_zil
= zil_replay(os
, zv
, zvol_replay_vector
);
1454 zil_close(zv
->zv_zilog
);
1455 zv
->zv_zilog
= NULL
;
1458 * When udev detects the addition of the device it will immediately
1459 * invoke blkid(8) to determine the type of content on the device.
1460 * Prefetching the blocks commonly scanned by blkid(8) will speed
1463 len
= MIN(zvol_prefetch_bytes
, SPA_MAXBLOCKSIZE
);
1465 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1466 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1467 ZIO_PRIORITY_SYNC_READ
);
1470 zv
->zv_objset
= NULL
;
1471 out_dmu_objset_disown
:
1472 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1474 kmem_free(doi
, sizeof (dmu_object_info_t
));
1477 * Keep in mind that once add_disk() is called, the zvol is
1478 * announced to the world, and zvol_open()/zvol_release() can
1479 * be called at any time. Incidentally, add_disk() itself calls
1480 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1484 rw_enter(&zvol_state_lock
, RW_WRITER
);
1486 rw_exit(&zvol_state_lock
);
1487 #ifdef HAVE_ADD_DISK_RET
1488 error
= add_disk(zv
->zv_zso
->zvo_disk
);
1490 add_disk(zv
->zv_zso
->zvo_disk
);
1493 ida_simple_remove(&zvol_ida
, idx
);
1500 zvol_os_rename_minor(zvol_state_t
*zv
, const char *newname
)
1502 int readonly
= get_disk_ro(zv
->zv_zso
->zvo_disk
);
1504 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1505 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1507 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1509 /* move to new hashtable entry */
1510 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1511 hlist_del(&zv
->zv_hlink
);
1512 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1515 * The block device's read-only state is briefly changed causing
1516 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1517 * the name change and fixes the symlinks. This does not change
1518 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1519 * changes. This would normally be done using kobject_uevent() but
1520 * that is a GPL-only symbol which is why we need this workaround.
1522 set_disk_ro(zv
->zv_zso
->zvo_disk
, !readonly
);
1523 set_disk_ro(zv
->zv_zso
->zvo_disk
, readonly
);
1527 zvol_os_set_disk_ro(zvol_state_t
*zv
, int flags
)
1530 set_disk_ro(zv
->zv_zso
->zvo_disk
, flags
);
1534 zvol_os_set_capacity(zvol_state_t
*zv
, uint64_t capacity
)
1537 set_capacity(zv
->zv_zso
->zvo_disk
, capacity
);
1546 * zvol_threads is the module param the user passes in.
1548 * zvol_actual_threads is what we use internally, since the user can
1549 * pass zvol_thread = 0 to mean "use all the CPUs" (the default).
1551 static unsigned int zvol_actual_threads
;
1553 if (zvol_threads
== 0) {
1555 * See dde9380a1 for why 32 was chosen here. This should
1556 * probably be refined to be some multiple of the number
1559 zvol_actual_threads
= MAX(num_online_cpus(), 32);
1561 zvol_actual_threads
= MIN(MAX(zvol_threads
, 1), 1024);
1564 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1566 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1571 if (zvol_blk_mq_queue_depth
== 0) {
1572 zvol_actual_blk_mq_queue_depth
= BLKDEV_DEFAULT_RQ
;
1574 zvol_actual_blk_mq_queue_depth
=
1575 MAX(zvol_blk_mq_queue_depth
, BLKDEV_MIN_RQ
);
1578 if (zvol_blk_mq_threads
== 0) {
1579 zvol_blk_mq_actual_threads
= num_online_cpus();
1581 zvol_blk_mq_actual_threads
= MIN(MAX(zvol_blk_mq_threads
, 1),
1585 zvol_taskq
= taskq_create(ZVOL_DRIVER
, zvol_actual_threads
, maxclsyspri
,
1586 zvol_actual_threads
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1587 if (zvol_taskq
== NULL
) {
1588 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1593 ida_init(&zvol_ida
);
1601 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1602 taskq_destroy(zvol_taskq
);
1603 ida_destroy(&zvol_ida
);
1607 module_param(zvol_inhibit_dev
, uint
, 0644);
1608 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1610 module_param(zvol_major
, uint
, 0444);
1611 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1613 module_param(zvol_threads
, uint
, 0444);
1614 MODULE_PARM_DESC(zvol_threads
, "Number of threads to handle I/O requests. Set"
1615 "to 0 to use all active CPUs");
1617 module_param(zvol_request_sync
, uint
, 0644);
1618 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
1620 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1621 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1623 module_param(zvol_prefetch_bytes
, uint
, 0644);
1624 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
1626 module_param(zvol_volmode
, uint
, 0644);
1627 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");
1630 module_param(zvol_blk_mq_queue_depth
, uint
, 0644);
1631 MODULE_PARM_DESC(zvol_blk_mq_queue_depth
, "Default blk-mq queue depth");
1633 module_param(zvol_use_blk_mq
, uint
, 0644);
1634 MODULE_PARM_DESC(zvol_use_blk_mq
, "Use the blk-mq API for zvols");
1636 module_param(zvol_blk_mq_blocks_per_thread
, uint
, 0644);
1637 MODULE_PARM_DESC(zvol_blk_mq_blocks_per_thread
,
1638 "Process volblocksize blocks per thread");
1641 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
1642 module_param(zvol_open_timeout_ms
, uint
, 0644);
1643 MODULE_PARM_DESC(zvol_open_timeout_ms
, "Timeout for ZVOL open retries");