4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 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>
44 static unsigned int zvol_major
= ZVOL_MAJOR
;
45 static unsigned int zvol_request_sync
= 0;
46 static unsigned int zvol_prefetch_bytes
= (128 * 1024);
47 static unsigned long zvol_max_discard_blocks
= 16384;
48 static unsigned int zvol_threads
= 32;
50 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
51 static const unsigned int zvol_open_timeout_ms
= 1000;
54 struct zvol_state_os
{
55 struct gendisk
*zvo_disk
; /* generic disk */
56 struct request_queue
*zvo_queue
; /* request queue */
57 dev_t zvo_dev
; /* device id */
61 static struct ida zvol_ida
;
63 typedef struct zv_request_stack
{
68 typedef struct zv_request_task
{
73 static zv_request_task_t
*
74 zv_request_task_create(zv_request_t zvr
)
76 zv_request_task_t
*task
;
77 task
= kmem_alloc(sizeof (zv_request_task_t
), KM_SLEEP
);
78 taskq_init_ent(&task
->ent
);
84 zv_request_task_free(zv_request_task_t
*task
)
86 kmem_free(task
, sizeof (*task
));
90 * Given a path, return TRUE if path is a ZVOL.
93 zvol_os_is_zvol(const char *path
)
97 if (vdev_lookup_bdev(path
, &dev
) != 0)
100 if (MAJOR(dev
) == zvol_major
)
107 zvol_write(zv_request_t
*zvr
)
109 struct bio
*bio
= zvr
->bio
;
113 zfs_uio_bvec_init(&uio
, bio
);
115 zvol_state_t
*zv
= zvr
->zv
;
116 ASSERT3P(zv
, !=, NULL
);
117 ASSERT3U(zv
->zv_open_count
, >, 0);
118 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
120 /* bio marked as FLUSH need to flush before write */
121 if (bio_is_flush(bio
))
122 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
124 /* Some requests are just for flush and nothing else. */
125 if (uio
.uio_resid
== 0) {
126 rw_exit(&zv
->zv_suspend_lock
);
131 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
132 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
133 ssize_t start_resid
= uio
.uio_resid
;
134 unsigned long start_time
;
136 boolean_t acct
= blk_queue_io_stat(q
);
138 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
, bio
);
141 bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
143 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
144 uio
.uio_loffset
, uio
.uio_resid
, RL_WRITER
);
146 uint64_t volsize
= zv
->zv_volsize
;
147 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
148 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
149 uint64_t off
= uio
.uio_loffset
;
150 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
152 if (bytes
> volsize
- off
) /* don't write past the end */
153 bytes
= volsize
- off
;
155 dmu_tx_hold_write_by_dnode(tx
, zv
->zv_dn
, off
, bytes
);
157 /* This will only fail for ENOSPC */
158 error
= dmu_tx_assign(tx
, TXG_WAIT
);
163 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
165 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
172 zfs_rangelock_exit(lr
);
174 int64_t nwritten
= start_resid
- uio
.uio_resid
;
175 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
176 task_io_account_write(nwritten
);
179 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
181 rw_exit(&zv
->zv_suspend_lock
);
184 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
, start_time
);
186 BIO_END_IO(bio
, -error
);
190 zvol_write_task(void *arg
)
192 zv_request_task_t
*task
= arg
;
193 zvol_write(&task
->zvr
);
194 zv_request_task_free(task
);
198 zvol_discard(zv_request_t
*zvr
)
200 struct bio
*bio
= zvr
->bio
;
201 zvol_state_t
*zv
= zvr
->zv
;
202 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
203 uint64_t size
= BIO_BI_SIZE(bio
);
204 uint64_t end
= start
+ size
;
209 ASSERT3P(zv
, !=, NULL
);
210 ASSERT3U(zv
->zv_open_count
, >, 0);
211 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
213 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
214 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
215 unsigned long start_time
;
217 boolean_t acct
= blk_queue_io_stat(q
);
219 start_time
= blk_generic_start_io_acct(q
, disk
, WRITE
, bio
);
221 sync
= bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
223 if (end
> zv
->zv_volsize
) {
224 error
= SET_ERROR(EIO
);
229 * Align the request to volume block boundaries when a secure erase is
230 * not required. This will prevent dnode_free_range() from zeroing out
231 * the unaligned parts which is slow (read-modify-write) and useless
232 * since we are not freeing any space by doing so.
234 if (!bio_is_secure_erase(bio
)) {
235 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
236 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
243 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
244 start
, size
, RL_WRITER
);
246 tx
= dmu_tx_create(zv
->zv_objset
);
247 dmu_tx_mark_netfree(tx
);
248 error
= dmu_tx_assign(tx
, TXG_WAIT
);
252 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
254 error
= dmu_free_long_range(zv
->zv_objset
,
255 ZVOL_OBJ
, start
, size
);
257 zfs_rangelock_exit(lr
);
259 if (error
== 0 && sync
)
260 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
263 rw_exit(&zv
->zv_suspend_lock
);
266 blk_generic_end_io_acct(q
, disk
, WRITE
, bio
, start_time
);
268 BIO_END_IO(bio
, -error
);
272 zvol_discard_task(void *arg
)
274 zv_request_task_t
*task
= arg
;
275 zvol_discard(&task
->zvr
);
276 zv_request_task_free(task
);
280 zvol_read(zv_request_t
*zvr
)
282 struct bio
*bio
= zvr
->bio
;
286 zfs_uio_bvec_init(&uio
, bio
);
288 zvol_state_t
*zv
= zvr
->zv
;
289 ASSERT3P(zv
, !=, NULL
);
290 ASSERT3U(zv
->zv_open_count
, >, 0);
292 struct request_queue
*q
= zv
->zv_zso
->zvo_queue
;
293 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
294 ssize_t start_resid
= uio
.uio_resid
;
295 unsigned long start_time
;
297 boolean_t acct
= blk_queue_io_stat(q
);
299 start_time
= blk_generic_start_io_acct(q
, disk
, READ
, bio
);
301 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
302 uio
.uio_loffset
, uio
.uio_resid
, RL_READER
);
304 uint64_t volsize
= zv
->zv_volsize
;
305 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
306 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
308 /* don't read past the end */
309 if (bytes
> volsize
- uio
.uio_loffset
)
310 bytes
= volsize
- uio
.uio_loffset
;
312 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
314 /* convert checksum errors into IO errors */
316 error
= SET_ERROR(EIO
);
320 zfs_rangelock_exit(lr
);
322 int64_t nread
= start_resid
- uio
.uio_resid
;
323 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
324 task_io_account_read(nread
);
326 rw_exit(&zv
->zv_suspend_lock
);
329 blk_generic_end_io_acct(q
, disk
, READ
, bio
, start_time
);
331 BIO_END_IO(bio
, -error
);
335 zvol_read_task(void *arg
)
337 zv_request_task_t
*task
= arg
;
338 zvol_read(&task
->zvr
);
339 zv_request_task_free(task
);
342 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
343 #ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
345 zvol_submit_bio(struct bio
*bio
)
348 zvol_submit_bio(struct bio
*bio
)
351 static MAKE_REQUEST_FN_RET
352 zvol_request(struct request_queue
*q
, struct bio
*bio
)
355 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
356 #if defined(HAVE_BIO_BDEV_DISK)
357 struct request_queue
*q
= bio
->bi_bdev
->bd_disk
->queue
;
359 struct request_queue
*q
= bio
->bi_disk
->queue
;
362 zvol_state_t
*zv
= q
->queuedata
;
363 fstrans_cookie_t cookie
= spl_fstrans_mark();
364 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
365 uint64_t size
= BIO_BI_SIZE(bio
);
366 int rw
= bio_data_dir(bio
);
368 if (bio_has_data(bio
) && offset
+ size
> zv
->zv_volsize
) {
370 "%s: bad access: offset=%llu, size=%lu\n",
371 zv
->zv_zso
->zvo_disk
->disk_name
,
372 (long long unsigned)offset
,
373 (long unsigned)size
);
375 BIO_END_IO(bio
, -SET_ERROR(EIO
));
383 zv_request_task_t
*task
;
386 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
387 BIO_END_IO(bio
, -SET_ERROR(EROFS
));
392 * Prevents the zvol from being suspended, or the ZIL being
393 * concurrently opened. Will be released after the i/o
396 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
399 * Open a ZIL if this is the first time we have written to this
400 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
401 * than zv_state_lock so that we don't need to acquire an
402 * additional lock in this path.
404 if (zv
->zv_zilog
== NULL
) {
405 rw_exit(&zv
->zv_suspend_lock
);
406 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
407 if (zv
->zv_zilog
== NULL
) {
408 zv
->zv_zilog
= zil_open(zv
->zv_objset
,
410 zv
->zv_flags
|= ZVOL_WRITTEN_TO
;
411 /* replay / destroy done in zvol_create_minor */
412 VERIFY0((zv
->zv_zilog
->zl_header
->zh_flags
&
415 rw_downgrade(&zv
->zv_suspend_lock
);
419 * We don't want this thread to be blocked waiting for i/o to
420 * complete, so we instead wait from a taskq callback. The
421 * i/o may be a ZIL write (via zil_commit()), or a read of an
422 * indirect block, or a read of a data block (if this is a
423 * partial-block write). We will indicate that the i/o is
424 * complete by calling BIO_END_IO() from the taskq callback.
426 * This design allows the calling thread to continue and
427 * initiate more concurrent operations by calling
428 * zvol_request() again. There are typically only a small
429 * number of threads available to call zvol_request() (e.g.
430 * one per iSCSI target), so keeping the latency of
431 * zvol_request() low is important for performance.
433 * The zvol_request_sync module parameter allows this
434 * behavior to be altered, for performance evaluation
435 * purposes. If the callback blocks, setting
436 * zvol_request_sync=1 will result in much worse performance.
438 * We can have up to zvol_threads concurrent i/o's being
439 * processed for all zvols on the system. This is typically
440 * a vast improvement over the zvol_request_sync=1 behavior
441 * of one i/o at a time per zvol. However, an even better
442 * design would be for zvol_request() to initiate the zio
443 * directly, and then be notified by the zio_done callback,
444 * which would call BIO_END_IO(). Unfortunately, the DMU/ZIL
445 * interfaces lack this functionality (they block waiting for
446 * the i/o to complete).
448 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
449 if (zvol_request_sync
) {
452 task
= zv_request_task_create(zvr
);
453 taskq_dispatch_ent(zvol_taskq
,
454 zvol_discard_task
, task
, 0, &task
->ent
);
457 if (zvol_request_sync
) {
460 task
= zv_request_task_create(zvr
);
461 taskq_dispatch_ent(zvol_taskq
,
462 zvol_write_task
, task
, 0, &task
->ent
);
467 * The SCST driver, and possibly others, may issue READ I/Os
468 * with a length of zero bytes. These empty I/Os contain no
469 * data and require no additional handling.
476 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
478 /* See comment in WRITE case above. */
479 if (zvol_request_sync
) {
482 task
= zv_request_task_create(zvr
);
483 taskq_dispatch_ent(zvol_taskq
,
484 zvol_read_task
, task
, 0, &task
->ent
);
489 spl_fstrans_unmark(cookie
);
490 #if (defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
491 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)) && \
492 !defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
493 return (BLK_QC_T_NONE
);
498 zvol_open(struct block_device
*bdev
, fmode_t flag
)
502 boolean_t drop_suspend
= B_FALSE
;
503 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
504 hrtime_t timeout
= MSEC2NSEC(zvol_open_timeout_ms
);
505 hrtime_t start
= gethrtime();
509 rw_enter(&zvol_state_lock
, RW_READER
);
511 * Obtain a copy of private_data under the zvol_state_lock to make
512 * sure that either the result of zvol free code path setting
513 * bdev->bd_disk->private_data to NULL is observed, or zvol_os_free()
514 * is not called on this zv because of the positive zv_open_count.
516 zv
= bdev
->bd_disk
->private_data
;
518 rw_exit(&zvol_state_lock
);
519 return (SET_ERROR(-ENXIO
));
522 mutex_enter(&zv
->zv_state_lock
);
524 * Make sure zvol is not suspended during first open
525 * (hold zv_suspend_lock) and respect proper lock acquisition
526 * ordering - zv_suspend_lock before zv_state_lock
528 if (zv
->zv_open_count
== 0) {
529 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
530 mutex_exit(&zv
->zv_state_lock
);
531 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
532 mutex_enter(&zv
->zv_state_lock
);
533 /* check to see if zv_suspend_lock is needed */
534 if (zv
->zv_open_count
!= 0) {
535 rw_exit(&zv
->zv_suspend_lock
);
537 drop_suspend
= B_TRUE
;
540 drop_suspend
= B_TRUE
;
543 rw_exit(&zvol_state_lock
);
545 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
547 if (zv
->zv_open_count
== 0) {
548 boolean_t drop_namespace
= B_FALSE
;
550 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
553 * In all other call paths the spa_namespace_lock is taken
554 * before the bdev->bd_mutex lock. However, on open(2)
555 * the __blkdev_get() function calls fops->open() with the
556 * bdev->bd_mutex lock held. This can result in a deadlock
557 * when zvols from one pool are used as vdevs in another.
559 * To prevent a lock inversion deadlock we preemptively
560 * take the spa_namespace_lock. Normally the lock will not
561 * be contended and this is safe because spa_open_common()
562 * handles the case where the caller already holds the
563 * spa_namespace_lock.
565 * When the lock cannot be aquired after multiple retries
566 * this must be the vdev on zvol deadlock case and we have
567 * no choice but to return an error. For 5.12 and older
568 * kernels returning -ERESTARTSYS will result in the
569 * bdev->bd_mutex being dropped, then reacquired, and
570 * fops->open() being called again. This process can be
571 * repeated safely until both locks are acquired. For 5.13
572 * and newer the -ERESTARTSYS retry logic was removed from
573 * the kernel so the only option is to return the error for
574 * the caller to handle it.
576 if (!mutex_owned(&spa_namespace_lock
)) {
577 if (!mutex_tryenter(&spa_namespace_lock
)) {
578 mutex_exit(&zv
->zv_state_lock
);
579 rw_exit(&zv
->zv_suspend_lock
);
581 #ifdef HAVE_BLKDEV_GET_ERESTARTSYS
583 return (SET_ERROR(-ERESTARTSYS
));
585 if ((gethrtime() - start
) > timeout
)
586 return (SET_ERROR(-ERESTARTSYS
));
588 schedule_timeout(MSEC_TO_TICK(10));
592 drop_namespace
= B_TRUE
;
596 error
= -zvol_first_open(zv
, !(flag
& FMODE_WRITE
));
599 mutex_exit(&spa_namespace_lock
);
603 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
604 if (zv
->zv_open_count
== 0)
607 error
= SET_ERROR(-EROFS
);
613 mutex_exit(&zv
->zv_state_lock
);
615 rw_exit(&zv
->zv_suspend_lock
);
618 zfs_check_media_change(bdev
);
624 zvol_release(struct gendisk
*disk
, fmode_t mode
)
627 boolean_t drop_suspend
= B_TRUE
;
629 rw_enter(&zvol_state_lock
, RW_READER
);
630 zv
= disk
->private_data
;
632 mutex_enter(&zv
->zv_state_lock
);
633 ASSERT3U(zv
->zv_open_count
, >, 0);
635 * make sure zvol is not suspended during last close
636 * (hold zv_suspend_lock) and respect proper lock acquisition
637 * ordering - zv_suspend_lock before zv_state_lock
639 if (zv
->zv_open_count
== 1) {
640 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
641 mutex_exit(&zv
->zv_state_lock
);
642 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
643 mutex_enter(&zv
->zv_state_lock
);
644 /* check to see if zv_suspend_lock is needed */
645 if (zv
->zv_open_count
!= 1) {
646 rw_exit(&zv
->zv_suspend_lock
);
647 drop_suspend
= B_FALSE
;
651 drop_suspend
= B_FALSE
;
653 rw_exit(&zvol_state_lock
);
655 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
658 if (zv
->zv_open_count
== 0) {
659 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
663 mutex_exit(&zv
->zv_state_lock
);
666 rw_exit(&zv
->zv_suspend_lock
);
670 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
671 unsigned int cmd
, unsigned long arg
)
673 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
676 ASSERT3U(zv
->zv_open_count
, >, 0);
681 invalidate_bdev(bdev
);
682 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
684 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
685 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
687 rw_exit(&zv
->zv_suspend_lock
);
691 mutex_enter(&zv
->zv_state_lock
);
692 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
693 mutex_exit(&zv
->zv_state_lock
);
701 return (SET_ERROR(error
));
706 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
707 unsigned cmd
, unsigned long arg
)
709 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
712 #define zvol_compat_ioctl NULL
716 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
718 unsigned int mask
= 0;
720 rw_enter(&zvol_state_lock
, RW_READER
);
722 zvol_state_t
*zv
= disk
->private_data
;
724 mutex_enter(&zv
->zv_state_lock
);
725 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
727 mutex_exit(&zv
->zv_state_lock
);
730 rw_exit(&zvol_state_lock
);
736 zvol_revalidate_disk(struct gendisk
*disk
)
738 rw_enter(&zvol_state_lock
, RW_READER
);
740 zvol_state_t
*zv
= disk
->private_data
;
742 mutex_enter(&zv
->zv_state_lock
);
743 set_capacity(zv
->zv_zso
->zvo_disk
,
744 zv
->zv_volsize
>> SECTOR_BITS
);
745 mutex_exit(&zv
->zv_state_lock
);
748 rw_exit(&zvol_state_lock
);
754 zvol_os_update_volsize(zvol_state_t
*zv
, uint64_t volsize
)
756 struct gendisk
*disk
= zv
->zv_zso
->zvo_disk
;
758 #if defined(HAVE_REVALIDATE_DISK_SIZE)
759 revalidate_disk_size(disk
, zvol_revalidate_disk(disk
) == 0);
760 #elif defined(HAVE_REVALIDATE_DISK)
761 revalidate_disk(disk
);
763 zvol_revalidate_disk(disk
);
769 zvol_os_clear_private(zvol_state_t
*zv
)
772 * Cleared while holding zvol_state_lock as a writer
773 * which will prevent zvol_open() from opening it.
775 zv
->zv_zso
->zvo_disk
->private_data
= NULL
;
779 * Provide a simple virtual geometry for legacy compatibility. For devices
780 * smaller than 1 MiB a small head and sector count is used to allow very
781 * tiny devices. For devices over 1 Mib a standard head and sector count
782 * is used to keep the cylinders count reasonable.
785 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
787 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
790 ASSERT3U(zv
->zv_open_count
, >, 0);
792 sectors
= get_capacity(zv
->zv_zso
->zvo_disk
);
794 if (sectors
> 2048) {
803 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
808 static const struct block_device_operations zvol_ops
= {
810 .release
= zvol_release
,
812 .compat_ioctl
= zvol_compat_ioctl
,
813 .check_events
= zvol_check_events
,
814 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
815 .revalidate_disk
= zvol_revalidate_disk
,
817 .getgeo
= zvol_getgeo
,
818 .owner
= THIS_MODULE
,
819 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
820 .submit_bio
= zvol_submit_bio
,
825 * Allocate memory for a new zvol_state_t and setup the required
826 * request queue and generic disk structures for the block device.
828 static zvol_state_t
*
829 zvol_alloc(dev_t dev
, const char *name
)
832 struct zvol_state_os
*zso
;
835 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
838 if (volmode
== ZFS_VOLMODE_DEFAULT
)
839 volmode
= zvol_volmode
;
841 if (volmode
== ZFS_VOLMODE_NONE
)
844 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
845 zso
= kmem_zalloc(sizeof (struct zvol_state_os
), KM_SLEEP
);
847 zv
->zv_volmode
= volmode
;
849 list_link_init(&zv
->zv_next
);
850 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
852 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
853 #ifdef HAVE_BLK_ALLOC_DISK
854 zso
->zvo_disk
= blk_alloc_disk(NUMA_NO_NODE
);
855 if (zso
->zvo_disk
== NULL
)
858 zso
->zvo_disk
->minors
= ZVOL_MINORS
;
859 zso
->zvo_queue
= zso
->zvo_disk
->queue
;
861 zso
->zvo_queue
= blk_alloc_queue(NUMA_NO_NODE
);
862 if (zso
->zvo_queue
== NULL
)
865 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
866 if (zso
->zvo_disk
== NULL
) {
867 blk_cleanup_queue(zso
->zvo_queue
);
871 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
872 #endif /* HAVE_BLK_ALLOC_DISK */
874 zso
->zvo_queue
= blk_generic_alloc_queue(zvol_request
, NUMA_NO_NODE
);
875 if (zso
->zvo_queue
== NULL
)
878 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
879 if (zso
->zvo_disk
== NULL
) {
880 blk_cleanup_queue(zso
->zvo_queue
);
884 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
885 #endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
887 blk_queue_set_write_cache(zso
->zvo_queue
, B_TRUE
, B_TRUE
);
889 /* Limit read-ahead to a single page to prevent over-prefetching. */
890 blk_queue_set_read_ahead(zso
->zvo_queue
, 1);
892 /* Disable write merging in favor of the ZIO pipeline. */
893 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zso
->zvo_queue
);
895 /* Enable /proc/diskstats */
896 blk_queue_flag_set(QUEUE_FLAG_IO_STAT
, zso
->zvo_queue
);
898 zso
->zvo_queue
->queuedata
= zv
;
900 zv
->zv_open_count
= 0;
901 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
903 zfs_rangelock_init(&zv
->zv_rangelock
, NULL
, NULL
);
904 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
906 zso
->zvo_disk
->major
= zvol_major
;
907 zso
->zvo_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
910 * Setting ZFS_VOLMODE_DEV disables partitioning on ZVOL devices.
911 * This is accomplished by limiting the number of minors for the
912 * device to one and explicitly disabling partition scanning.
914 if (volmode
== ZFS_VOLMODE_DEV
) {
915 zso
->zvo_disk
->minors
= 1;
916 zso
->zvo_disk
->flags
&= ~ZFS_GENHD_FL_EXT_DEVT
;
917 zso
->zvo_disk
->flags
|= ZFS_GENHD_FL_NO_PART
;
920 zso
->zvo_disk
->first_minor
= (dev
& MINORMASK
);
921 zso
->zvo_disk
->fops
= &zvol_ops
;
922 zso
->zvo_disk
->private_data
= zv
;
923 snprintf(zso
->zvo_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
924 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
929 kmem_free(zso
, sizeof (struct zvol_state_os
));
930 kmem_free(zv
, sizeof (zvol_state_t
));
935 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
936 * At this time, the structure is not opened by anyone, is taken off
937 * the zvol_state_list, and has its private data set to NULL.
938 * The zvol_state_lock is dropped.
940 * This function may take many milliseconds to complete (e.g. we've seen
941 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
942 * "del_gendisk". Thus, consumers need to be careful to account for this
943 * latency when calling this function.
946 zvol_os_free(zvol_state_t
*zv
)
949 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
950 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
951 ASSERT0(zv
->zv_open_count
);
952 ASSERT3P(zv
->zv_zso
->zvo_disk
->private_data
, ==, NULL
);
954 rw_destroy(&zv
->zv_suspend_lock
);
955 zfs_rangelock_fini(&zv
->zv_rangelock
);
957 del_gendisk(zv
->zv_zso
->zvo_disk
);
958 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
959 defined(HAVE_BLK_ALLOC_DISK)
960 blk_cleanup_disk(zv
->zv_zso
->zvo_disk
);
962 blk_cleanup_queue(zv
->zv_zso
->zvo_queue
);
963 put_disk(zv
->zv_zso
->zvo_disk
);
966 ida_simple_remove(&zvol_ida
,
967 MINOR(zv
->zv_zso
->zvo_dev
) >> ZVOL_MINOR_BITS
);
969 mutex_destroy(&zv
->zv_state_lock
);
970 dataset_kstats_destroy(&zv
->zv_kstat
);
972 kmem_free(zv
->zv_zso
, sizeof (struct zvol_state_os
));
973 kmem_free(zv
, sizeof (zvol_state_t
));
977 zvol_wait_close(zvol_state_t
*zv
)
982 * Create a block device minor node and setup the linkage between it
983 * and the specified volume. Once this function returns the block
984 * device is live and ready for use.
987 zvol_os_create_minor(const char *name
)
991 dmu_object_info_t
*doi
;
997 uint64_t hash
= zvol_name_hash(name
);
999 if (zvol_inhibit_dev
)
1002 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1004 return (SET_ERROR(-idx
));
1005 minor
= idx
<< ZVOL_MINOR_BITS
;
1007 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
1009 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1010 mutex_exit(&zv
->zv_state_lock
);
1011 ida_simple_remove(&zvol_ida
, idx
);
1012 return (SET_ERROR(EEXIST
));
1015 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1017 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
1021 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1023 goto out_dmu_objset_disown
;
1025 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1027 goto out_dmu_objset_disown
;
1029 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1031 error
= SET_ERROR(EAGAIN
);
1032 goto out_dmu_objset_disown
;
1036 if (dmu_objset_is_snapshot(os
))
1037 zv
->zv_flags
|= ZVOL_RDONLY
;
1039 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1040 zv
->zv_volsize
= volsize
;
1043 set_capacity(zv
->zv_zso
->zvo_disk
, zv
->zv_volsize
>> 9);
1045 blk_queue_max_hw_sectors(zv
->zv_zso
->zvo_queue
,
1046 (DMU_MAX_ACCESS
/ 4) >> 9);
1047 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
, UINT16_MAX
);
1048 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
, UINT_MAX
);
1049 blk_queue_physical_block_size(zv
->zv_zso
->zvo_queue
,
1050 zv
->zv_volblocksize
);
1051 blk_queue_io_opt(zv
->zv_zso
->zvo_queue
, zv
->zv_volblocksize
);
1052 blk_queue_max_discard_sectors(zv
->zv_zso
->zvo_queue
,
1053 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1054 blk_queue_discard_granularity(zv
->zv_zso
->zvo_queue
,
1055 zv
->zv_volblocksize
);
1056 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_zso
->zvo_queue
);
1057 #ifdef QUEUE_FLAG_NONROT
1058 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_zso
->zvo_queue
);
1060 #ifdef QUEUE_FLAG_ADD_RANDOM
1061 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_zso
->zvo_queue
);
1063 /* This flag was introduced in kernel version 4.12. */
1064 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1065 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH
, zv
->zv_zso
->zvo_queue
);
1068 ASSERT3P(zv
->zv_zilog
, ==, NULL
);
1069 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
1070 if (spa_writeable(dmu_objset_spa(os
))) {
1071 if (zil_replay_disable
)
1072 zil_destroy(zv
->zv_zilog
, B_FALSE
);
1074 zil_replay(os
, zv
, zvol_replay_vector
);
1076 zil_close(zv
->zv_zilog
);
1077 zv
->zv_zilog
= NULL
;
1078 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
1079 dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
1082 * When udev detects the addition of the device it will immediately
1083 * invoke blkid(8) to determine the type of content on the device.
1084 * Prefetching the blocks commonly scanned by blkid(8) will speed
1087 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1089 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1090 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1091 ZIO_PRIORITY_SYNC_READ
);
1094 zv
->zv_objset
= NULL
;
1095 out_dmu_objset_disown
:
1096 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1098 kmem_free(doi
, sizeof (dmu_object_info_t
));
1101 * Keep in mind that once add_disk() is called, the zvol is
1102 * announced to the world, and zvol_open()/zvol_release() can
1103 * be called at any time. Incidentally, add_disk() itself calls
1104 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1108 rw_enter(&zvol_state_lock
, RW_WRITER
);
1110 rw_exit(&zvol_state_lock
);
1111 #ifdef HAVE_ADD_DISK_RET
1112 error
= add_disk(zv
->zv_zso
->zvo_disk
);
1114 add_disk(zv
->zv_zso
->zvo_disk
);
1117 ida_simple_remove(&zvol_ida
, idx
);
1124 zvol_os_rename_minor(zvol_state_t
*zv
, const char *newname
)
1126 int readonly
= get_disk_ro(zv
->zv_zso
->zvo_disk
);
1128 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1129 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1131 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1133 /* move to new hashtable entry */
1134 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1135 hlist_del(&zv
->zv_hlink
);
1136 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1139 * The block device's read-only state is briefly changed causing
1140 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1141 * the name change and fixes the symlinks. This does not change
1142 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1143 * changes. This would normally be done using kobject_uevent() but
1144 * that is a GPL-only symbol which is why we need this workaround.
1146 set_disk_ro(zv
->zv_zso
->zvo_disk
, !readonly
);
1147 set_disk_ro(zv
->zv_zso
->zvo_disk
, readonly
);
1151 zvol_os_set_disk_ro(zvol_state_t
*zv
, int flags
)
1154 set_disk_ro(zv
->zv_zso
->zvo_disk
, flags
);
1158 zvol_os_set_capacity(zvol_state_t
*zv
, uint64_t capacity
)
1161 set_capacity(zv
->zv_zso
->zvo_disk
, capacity
);
1168 int threads
= MIN(MAX(zvol_threads
, 1), 1024);
1170 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1172 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1175 zvol_taskq
= taskq_create(ZVOL_DRIVER
, threads
, maxclsyspri
,
1176 threads
* 2, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1177 if (zvol_taskq
== NULL
) {
1178 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1182 ida_init(&zvol_ida
);
1190 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1191 taskq_destroy(zvol_taskq
);
1192 ida_destroy(&zvol_ida
);
1196 module_param(zvol_inhibit_dev
, uint
, 0644);
1197 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1199 module_param(zvol_major
, uint
, 0444);
1200 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1202 module_param(zvol_threads
, uint
, 0444);
1203 MODULE_PARM_DESC(zvol_threads
, "Max number of threads to handle I/O requests");
1205 module_param(zvol_request_sync
, uint
, 0644);
1206 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
1208 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1209 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1211 module_param(zvol_prefetch_bytes
, uint
, 0644);
1212 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
1214 module_param(zvol_volmode
, uint
, 0644);
1215 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");