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 unsigned int zvol_major
= ZVOL_MAJOR
;
45 unsigned int zvol_request_sync
= 0;
46 unsigned int zvol_prefetch_bytes
= (128 * 1024);
47 unsigned long zvol_max_discard_blocks
= 16384;
48 unsigned int zvol_threads
= 32;
50 struct zvol_state_os
{
51 struct gendisk
*zvo_disk
; /* generic disk */
52 struct request_queue
*zvo_queue
; /* request queue */
53 dev_t zvo_dev
; /* device id */
57 static struct ida zvol_ida
;
59 typedef struct zv_request
{
66 * Given a path, return TRUE if path is a ZVOL.
69 zvol_is_zvol_impl(const char *device
)
71 struct block_device
*bdev
;
74 bdev
= vdev_lookup_bdev(device
);
78 major
= MAJOR(bdev
->bd_dev
);
81 if (major
== zvol_major
)
88 uio_from_bio(uio_t
*uio
, struct bio
*bio
)
90 uio
->uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
91 uio
->uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
92 uio
->uio_loffset
= BIO_BI_SECTOR(bio
) << 9;
93 uio
->uio_segflg
= UIO_BVEC
;
94 uio
->uio_resid
= BIO_BI_SIZE(bio
);
95 uio
->uio_skip
= BIO_BI_SKIP(bio
);
103 zv_request_t
*zvr
= arg
;
104 struct bio
*bio
= zvr
->bio
;
105 uio_t uio
= { { 0 }, 0 };
106 uio_from_bio(&uio
, bio
);
108 zvol_state_t
*zv
= zvr
->zv
;
109 ASSERT3P(zv
, !=, NULL
);
110 ASSERT3U(zv
->zv_open_count
, >, 0);
111 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
113 /* bio marked as FLUSH need to flush before write */
114 if (bio_is_flush(bio
))
115 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
117 /* Some requests are just for flush and nothing else. */
118 if (uio
.uio_resid
== 0) {
119 rw_exit(&zv
->zv_suspend_lock
);
121 kmem_free(zvr
, sizeof (zv_request_t
));
125 ssize_t start_resid
= uio
.uio_resid
;
126 unsigned long start_jif
= jiffies
;
127 blk_generic_start_io_acct(zv
->zv_zso
->zvo_queue
, WRITE
,
128 bio_sectors(bio
), &zv
->zv_zso
->zvo_disk
->part0
);
131 bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
133 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
134 uio
.uio_loffset
, uio
.uio_resid
, RL_WRITER
);
136 uint64_t volsize
= zv
->zv_volsize
;
137 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
138 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
139 uint64_t off
= uio
.uio_loffset
;
140 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
142 if (bytes
> volsize
- off
) /* don't write past the end */
143 bytes
= volsize
- off
;
145 dmu_tx_hold_write_by_dnode(tx
, zv
->zv_dn
, off
, bytes
);
147 /* This will only fail for ENOSPC */
148 error
= dmu_tx_assign(tx
, TXG_WAIT
);
153 error
= dmu_write_uio_dnode(zv
->zv_dn
, &uio
, bytes
, tx
);
155 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
162 zfs_rangelock_exit(lr
);
164 int64_t nwritten
= start_resid
- uio
.uio_resid
;
165 dataset_kstats_update_write_kstats(&zv
->zv_kstat
, nwritten
);
166 task_io_account_write(nwritten
);
169 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
171 rw_exit(&zv
->zv_suspend_lock
);
172 blk_generic_end_io_acct(zv
->zv_zso
->zvo_queue
,
173 WRITE
, &zv
->zv_zso
->zvo_disk
->part0
, start_jif
);
174 BIO_END_IO(bio
, -error
);
175 kmem_free(zvr
, sizeof (zv_request_t
));
179 zvol_discard(void *arg
)
181 zv_request_t
*zvr
= arg
;
182 struct bio
*bio
= zvr
->bio
;
183 zvol_state_t
*zv
= zvr
->zv
;
184 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
185 uint64_t size
= BIO_BI_SIZE(bio
);
186 uint64_t end
= start
+ size
;
190 unsigned long start_jif
;
192 ASSERT3P(zv
, !=, NULL
);
193 ASSERT3U(zv
->zv_open_count
, >, 0);
194 ASSERT3P(zv
->zv_zilog
, !=, NULL
);
197 blk_generic_start_io_acct(zv
->zv_zso
->zvo_queue
, WRITE
,
198 bio_sectors(bio
), &zv
->zv_zso
->zvo_disk
->part0
);
200 sync
= bio_is_fua(bio
) || zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
;
202 if (end
> zv
->zv_volsize
) {
203 error
= SET_ERROR(EIO
);
208 * Align the request to volume block boundaries when a secure erase is
209 * not required. This will prevent dnode_free_range() from zeroing out
210 * the unaligned parts which is slow (read-modify-write) and useless
211 * since we are not freeing any space by doing so.
213 if (!bio_is_secure_erase(bio
)) {
214 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
215 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
222 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
223 start
, size
, RL_WRITER
);
225 tx
= dmu_tx_create(zv
->zv_objset
);
226 dmu_tx_mark_netfree(tx
);
227 error
= dmu_tx_assign(tx
, TXG_WAIT
);
231 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
233 error
= dmu_free_long_range(zv
->zv_objset
,
234 ZVOL_OBJ
, start
, size
);
236 zfs_rangelock_exit(lr
);
238 if (error
== 0 && sync
)
239 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
242 rw_exit(&zv
->zv_suspend_lock
);
243 blk_generic_end_io_acct(zv
->zv_zso
->zvo_queue
, WRITE
,
244 &zv
->zv_zso
->zvo_disk
->part0
, start_jif
);
245 BIO_END_IO(bio
, -error
);
246 kmem_free(zvr
, sizeof (zv_request_t
));
254 zv_request_t
*zvr
= arg
;
255 struct bio
*bio
= zvr
->bio
;
256 uio_t uio
= { { 0 }, 0 };
257 uio_from_bio(&uio
, bio
);
259 zvol_state_t
*zv
= zvr
->zv
;
260 ASSERT3P(zv
, !=, NULL
);
261 ASSERT3U(zv
->zv_open_count
, >, 0);
263 ssize_t start_resid
= uio
.uio_resid
;
264 unsigned long start_jif
= jiffies
;
265 blk_generic_start_io_acct(zv
->zv_zso
->zvo_queue
, READ
, bio_sectors(bio
),
266 &zv
->zv_zso
->zvo_disk
->part0
);
268 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zv
->zv_rangelock
,
269 uio
.uio_loffset
, uio
.uio_resid
, RL_READER
);
271 uint64_t volsize
= zv
->zv_volsize
;
272 while (uio
.uio_resid
> 0 && uio
.uio_loffset
< volsize
) {
273 uint64_t bytes
= MIN(uio
.uio_resid
, DMU_MAX_ACCESS
>> 1);
275 /* don't read past the end */
276 if (bytes
> volsize
- uio
.uio_loffset
)
277 bytes
= volsize
- uio
.uio_loffset
;
279 error
= dmu_read_uio_dnode(zv
->zv_dn
, &uio
, bytes
);
281 /* convert checksum errors into IO errors */
283 error
= SET_ERROR(EIO
);
287 zfs_rangelock_exit(lr
);
289 int64_t nread
= start_resid
- uio
.uio_resid
;
290 dataset_kstats_update_read_kstats(&zv
->zv_kstat
, nread
);
291 task_io_account_read(nread
);
293 rw_exit(&zv
->zv_suspend_lock
);
294 blk_generic_end_io_acct(zv
->zv_zso
->zvo_queue
, READ
,
295 &zv
->zv_zso
->zvo_disk
->part0
, start_jif
);
296 BIO_END_IO(bio
, -error
);
297 kmem_free(zvr
, sizeof (zv_request_t
));
300 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
302 zvol_submit_bio(struct bio
*bio
)
304 static MAKE_REQUEST_FN_RET
305 zvol_request(struct request_queue
*q
, struct bio
*bio
)
308 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
309 struct request_queue
*q
= bio
->bi_disk
->queue
;
311 zvol_state_t
*zv
= q
->queuedata
;
312 fstrans_cookie_t cookie
= spl_fstrans_mark();
313 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
314 uint64_t size
= BIO_BI_SIZE(bio
);
315 int rw
= bio_data_dir(bio
);
318 if (bio_has_data(bio
) && offset
+ size
> zv
->zv_volsize
) {
320 "%s: bad access: offset=%llu, size=%lu\n",
321 zv
->zv_zso
->zvo_disk
->disk_name
,
322 (long long unsigned)offset
,
323 (long unsigned)size
);
325 BIO_END_IO(bio
, -SET_ERROR(EIO
));
330 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
331 BIO_END_IO(bio
, -SET_ERROR(EROFS
));
336 * Prevents the zvol from being suspended, or the ZIL being
337 * concurrently opened. Will be released after the i/o
340 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
343 * Open a ZIL if this is the first time we have written to this
344 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
345 * than zv_state_lock so that we don't need to acquire an
346 * additional lock in this path.
348 if (zv
->zv_zilog
== NULL
) {
349 rw_exit(&zv
->zv_suspend_lock
);
350 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
351 if (zv
->zv_zilog
== NULL
) {
352 zv
->zv_zilog
= zil_open(zv
->zv_objset
,
354 zv
->zv_flags
|= ZVOL_WRITTEN_TO
;
356 rw_downgrade(&zv
->zv_suspend_lock
);
359 zvr
= kmem_alloc(sizeof (zv_request_t
), KM_SLEEP
);
362 taskq_init_ent(&zvr
->ent
);
365 * We don't want this thread to be blocked waiting for i/o to
366 * complete, so we instead wait from a taskq callback. The
367 * i/o may be a ZIL write (via zil_commit()), or a read of an
368 * indirect block, or a read of a data block (if this is a
369 * partial-block write). We will indicate that the i/o is
370 * complete by calling BIO_END_IO() from the taskq callback.
372 * This design allows the calling thread to continue and
373 * initiate more concurrent operations by calling
374 * zvol_request() again. There are typically only a small
375 * number of threads available to call zvol_request() (e.g.
376 * one per iSCSI target), so keeping the latency of
377 * zvol_request() low is important for performance.
379 * The zvol_request_sync module parameter allows this
380 * behavior to be altered, for performance evaluation
381 * purposes. If the callback blocks, setting
382 * zvol_request_sync=1 will result in much worse performance.
384 * We can have up to zvol_threads concurrent i/o's being
385 * processed for all zvols on the system. This is typically
386 * a vast improvement over the zvol_request_sync=1 behavior
387 * of one i/o at a time per zvol. However, an even better
388 * design would be for zvol_request() to initiate the zio
389 * directly, and then be notified by the zio_done callback,
390 * which would call BIO_END_IO(). Unfortunately, the DMU/ZIL
391 * interfaces lack this functionality (they block waiting for
392 * the i/o to complete).
394 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
395 if (zvol_request_sync
) {
398 taskq_dispatch_ent(zvol_taskq
,
399 zvol_discard
, zvr
, 0, &zvr
->ent
);
402 if (zvol_request_sync
) {
405 taskq_dispatch_ent(zvol_taskq
,
406 zvol_write
, zvr
, 0, &zvr
->ent
);
411 * The SCST driver, and possibly others, may issue READ I/Os
412 * with a length of zero bytes. These empty I/Os contain no
413 * data and require no additional handling.
420 zvr
= kmem_alloc(sizeof (zv_request_t
), KM_SLEEP
);
423 taskq_init_ent(&zvr
->ent
);
425 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
427 /* See comment in WRITE case above. */
428 if (zvol_request_sync
) {
431 taskq_dispatch_ent(zvol_taskq
,
432 zvol_read
, zvr
, 0, &zvr
->ent
);
437 spl_fstrans_unmark(cookie
);
438 #if defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
439 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)
440 return (BLK_QC_T_NONE
);
445 zvol_open(struct block_device
*bdev
, fmode_t flag
)
449 boolean_t drop_suspend
= B_TRUE
;
451 rw_enter(&zvol_state_lock
, RW_READER
);
453 * Obtain a copy of private_data under the zvol_state_lock to make
454 * sure that either the result of zvol free code path setting
455 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
456 * is not called on this zv because of the positive zv_open_count.
458 zv
= bdev
->bd_disk
->private_data
;
460 rw_exit(&zvol_state_lock
);
461 return (SET_ERROR(-ENXIO
));
464 mutex_enter(&zv
->zv_state_lock
);
466 * make sure zvol is not suspended during first open
467 * (hold zv_suspend_lock) and respect proper lock acquisition
468 * ordering - zv_suspend_lock before zv_state_lock
470 if (zv
->zv_open_count
== 0) {
471 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
472 mutex_exit(&zv
->zv_state_lock
);
473 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
474 mutex_enter(&zv
->zv_state_lock
);
475 /* check to see if zv_suspend_lock is needed */
476 if (zv
->zv_open_count
!= 0) {
477 rw_exit(&zv
->zv_suspend_lock
);
478 drop_suspend
= B_FALSE
;
482 drop_suspend
= B_FALSE
;
484 rw_exit(&zvol_state_lock
);
486 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
488 if (zv
->zv_open_count
== 0) {
489 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
490 error
= -zvol_first_open(zv
, !(flag
& FMODE_WRITE
));
495 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
502 mutex_exit(&zv
->zv_state_lock
);
504 rw_exit(&zv
->zv_suspend_lock
);
506 zfs_check_media_change(bdev
);
511 if (zv
->zv_open_count
== 0)
515 mutex_exit(&zv
->zv_state_lock
);
517 rw_exit(&zv
->zv_suspend_lock
);
518 if (error
== -EINTR
) {
519 error
= -ERESTARTSYS
;
522 return (SET_ERROR(error
));
526 zvol_release(struct gendisk
*disk
, fmode_t mode
)
529 boolean_t drop_suspend
= B_TRUE
;
531 rw_enter(&zvol_state_lock
, RW_READER
);
532 zv
= disk
->private_data
;
534 mutex_enter(&zv
->zv_state_lock
);
535 ASSERT3U(zv
->zv_open_count
, >, 0);
537 * make sure zvol is not suspended during last close
538 * (hold zv_suspend_lock) and respect proper lock acquisition
539 * ordering - zv_suspend_lock before zv_state_lock
541 if (zv
->zv_open_count
== 1) {
542 if (!rw_tryenter(&zv
->zv_suspend_lock
, RW_READER
)) {
543 mutex_exit(&zv
->zv_state_lock
);
544 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
545 mutex_enter(&zv
->zv_state_lock
);
546 /* check to see if zv_suspend_lock is needed */
547 if (zv
->zv_open_count
!= 1) {
548 rw_exit(&zv
->zv_suspend_lock
);
549 drop_suspend
= B_FALSE
;
553 drop_suspend
= B_FALSE
;
555 rw_exit(&zvol_state_lock
);
557 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
560 if (zv
->zv_open_count
== 0) {
561 ASSERT(RW_READ_HELD(&zv
->zv_suspend_lock
));
565 mutex_exit(&zv
->zv_state_lock
);
568 rw_exit(&zv
->zv_suspend_lock
);
572 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
573 unsigned int cmd
, unsigned long arg
)
575 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
578 ASSERT3U(zv
->zv_open_count
, >, 0);
583 invalidate_bdev(bdev
);
584 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
586 if (!(zv
->zv_flags
& ZVOL_RDONLY
))
587 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
589 rw_exit(&zv
->zv_suspend_lock
);
593 mutex_enter(&zv
->zv_state_lock
);
594 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
595 mutex_exit(&zv
->zv_state_lock
);
603 return (SET_ERROR(error
));
608 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
609 unsigned cmd
, unsigned long arg
)
611 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
614 #define zvol_compat_ioctl NULL
618 zvol_check_events(struct gendisk
*disk
, unsigned int clearing
)
620 unsigned int mask
= 0;
622 rw_enter(&zvol_state_lock
, RW_READER
);
624 zvol_state_t
*zv
= disk
->private_data
;
626 mutex_enter(&zv
->zv_state_lock
);
627 mask
= zv
->zv_changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
629 mutex_exit(&zv
->zv_state_lock
);
632 rw_exit(&zvol_state_lock
);
638 zvol_revalidate_disk(struct gendisk
*disk
)
640 rw_enter(&zvol_state_lock
, RW_READER
);
642 zvol_state_t
*zv
= disk
->private_data
;
644 mutex_enter(&zv
->zv_state_lock
);
645 set_capacity(zv
->zv_zso
->zvo_disk
,
646 zv
->zv_volsize
>> SECTOR_BITS
);
647 mutex_exit(&zv
->zv_state_lock
);
650 rw_exit(&zvol_state_lock
);
656 zvol_update_volsize(zvol_state_t
*zv
, uint64_t volsize
)
659 #ifdef HAVE_REVALIDATE_DISK_SIZE
660 revalidate_disk_size(zv
->zv_zso
->zvo_disk
, false);
662 revalidate_disk(zv
->zv_zso
->zvo_disk
);
668 zvol_clear_private(zvol_state_t
*zv
)
671 * Cleared while holding zvol_state_lock as a writer
672 * which will prevent zvol_open() from opening it.
674 zv
->zv_zso
->zvo_disk
->private_data
= NULL
;
678 * Provide a simple virtual geometry for legacy compatibility. For devices
679 * smaller than 1 MiB a small head and sector count is used to allow very
680 * tiny devices. For devices over 1 Mib a standard head and sector count
681 * is used to keep the cylinders count reasonable.
684 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
686 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
689 ASSERT3U(zv
->zv_open_count
, >, 0);
691 sectors
= get_capacity(zv
->zv_zso
->zvo_disk
);
693 if (sectors
> 2048) {
702 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
708 * Find a zvol_state_t given the full major+minor dev_t. If found,
709 * return with zv_state_lock taken, otherwise, return (NULL) without
710 * taking zv_state_lock.
712 static zvol_state_t
*
713 zvol_find_by_dev(dev_t dev
)
717 rw_enter(&zvol_state_lock
, RW_READER
);
718 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
719 zv
= list_next(&zvol_state_list
, zv
)) {
720 mutex_enter(&zv
->zv_state_lock
);
721 if (zv
->zv_zso
->zvo_dev
== dev
) {
722 rw_exit(&zvol_state_lock
);
725 mutex_exit(&zv
->zv_state_lock
);
727 rw_exit(&zvol_state_lock
);
732 static struct kobject
*
733 zvol_probe(dev_t dev
, int *part
, void *arg
)
736 struct kobject
*kobj
;
738 zv
= zvol_find_by_dev(dev
);
739 kobj
= zv
? get_disk_and_module(zv
->zv_zso
->zvo_disk
) : NULL
;
740 ASSERT(zv
== NULL
|| MUTEX_HELD(&zv
->zv_state_lock
));
742 mutex_exit(&zv
->zv_state_lock
);
747 static struct block_device_operations zvol_ops
= {
749 .release
= zvol_release
,
751 .compat_ioctl
= zvol_compat_ioctl
,
752 .check_events
= zvol_check_events
,
753 .revalidate_disk
= zvol_revalidate_disk
,
754 .getgeo
= zvol_getgeo
,
755 .owner
= THIS_MODULE
,
756 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
757 .submit_bio
= zvol_submit_bio
,
762 * Allocate memory for a new zvol_state_t and setup the required
763 * request queue and generic disk structures for the block device.
765 static zvol_state_t
*
766 zvol_alloc(dev_t dev
, const char *name
)
769 struct zvol_state_os
*zso
;
772 if (dsl_prop_get_integer(name
, "volmode", &volmode
, NULL
) != 0)
775 if (volmode
== ZFS_VOLMODE_DEFAULT
)
776 volmode
= zvol_volmode
;
778 if (volmode
== ZFS_VOLMODE_NONE
)
781 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
782 zso
= kmem_zalloc(sizeof (struct zvol_state_os
), KM_SLEEP
);
784 zv
->zv_volmode
= volmode
;
786 list_link_init(&zv
->zv_next
);
787 mutex_init(&zv
->zv_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
789 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
790 zso
->zvo_queue
= blk_alloc_queue(NUMA_NO_NODE
);
792 zso
->zvo_queue
= blk_generic_alloc_queue(zvol_request
, NUMA_NO_NODE
);
794 if (zso
->zvo_queue
== NULL
)
797 blk_queue_set_write_cache(zso
->zvo_queue
, B_TRUE
, B_TRUE
);
799 /* Limit read-ahead to a single page to prevent over-prefetching. */
800 blk_queue_set_read_ahead(zso
->zvo_queue
, 1);
802 /* Disable write merging in favor of the ZIO pipeline. */
803 blk_queue_flag_set(QUEUE_FLAG_NOMERGES
, zso
->zvo_queue
);
805 zso
->zvo_disk
= alloc_disk(ZVOL_MINORS
);
806 if (zso
->zvo_disk
== NULL
)
809 zso
->zvo_queue
->queuedata
= zv
;
811 zv
->zv_open_count
= 0;
812 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
814 zfs_rangelock_init(&zv
->zv_rangelock
, NULL
, NULL
);
815 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
817 zso
->zvo_disk
->major
= zvol_major
;
818 zso
->zvo_disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
820 if (volmode
== ZFS_VOLMODE_DEV
) {
822 * ZFS_VOLMODE_DEV disable partitioning on ZVOL devices: set
823 * gendisk->minors = 1 as noted in include/linux/genhd.h.
824 * Also disable extended partition numbers (GENHD_FL_EXT_DEVT)
825 * and suppresses partition scanning (GENHD_FL_NO_PART_SCAN)
826 * setting gendisk->flags accordingly.
828 zso
->zvo_disk
->minors
= 1;
829 #if defined(GENHD_FL_EXT_DEVT)
830 zso
->zvo_disk
->flags
&= ~GENHD_FL_EXT_DEVT
;
832 #if defined(GENHD_FL_NO_PART_SCAN)
833 zso
->zvo_disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
836 zso
->zvo_disk
->first_minor
= (dev
& MINORMASK
);
837 zso
->zvo_disk
->fops
= &zvol_ops
;
838 zso
->zvo_disk
->private_data
= zv
;
839 zso
->zvo_disk
->queue
= zso
->zvo_queue
;
840 snprintf(zso
->zvo_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
841 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
846 blk_cleanup_queue(zso
->zvo_queue
);
848 kmem_free(zso
, sizeof (struct zvol_state_os
));
849 kmem_free(zv
, sizeof (zvol_state_t
));
854 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
855 * At this time, the structure is not opened by anyone, is taken off
856 * the zvol_state_list, and has its private data set to NULL.
857 * The zvol_state_lock is dropped.
859 * This function may take many milliseconds to complete (e.g. we've seen
860 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
861 * "del_gendisk". Thus, consumers need to be careful to account for this
862 * latency when calling this function.
865 zvol_free(zvol_state_t
*zv
)
868 ASSERT(!RW_LOCK_HELD(&zv
->zv_suspend_lock
));
869 ASSERT(!MUTEX_HELD(&zv
->zv_state_lock
));
870 ASSERT0(zv
->zv_open_count
);
871 ASSERT3P(zv
->zv_zso
->zvo_disk
->private_data
, ==, NULL
);
873 rw_destroy(&zv
->zv_suspend_lock
);
874 zfs_rangelock_fini(&zv
->zv_rangelock
);
876 del_gendisk(zv
->zv_zso
->zvo_disk
);
877 blk_cleanup_queue(zv
->zv_zso
->zvo_queue
);
878 put_disk(zv
->zv_zso
->zvo_disk
);
880 ida_simple_remove(&zvol_ida
,
881 MINOR(zv
->zv_zso
->zvo_dev
) >> ZVOL_MINOR_BITS
);
883 mutex_destroy(&zv
->zv_state_lock
);
884 dataset_kstats_destroy(&zv
->zv_kstat
);
886 kmem_free(zv
->zv_zso
, sizeof (struct zvol_state_os
));
887 kmem_free(zv
, sizeof (zvol_state_t
));
891 zvol_wait_close(zvol_state_t
*zv
)
896 * Create a block device minor node and setup the linkage between it
897 * and the specified volume. Once this function returns the block
898 * device is live and ready for use.
901 zvol_os_create_minor(const char *name
)
905 dmu_object_info_t
*doi
;
911 uint64_t hash
= zvol_name_hash(name
);
913 if (zvol_inhibit_dev
)
916 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
918 return (SET_ERROR(-idx
));
919 minor
= idx
<< ZVOL_MINOR_BITS
;
921 zv
= zvol_find_by_name_hash(name
, hash
, RW_NONE
);
923 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
924 mutex_exit(&zv
->zv_state_lock
);
925 ida_simple_remove(&zvol_ida
, idx
);
926 return (SET_ERROR(EEXIST
));
929 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
931 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, B_TRUE
, FTAG
, &os
);
935 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
937 goto out_dmu_objset_disown
;
939 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
941 goto out_dmu_objset_disown
;
943 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
945 error
= SET_ERROR(EAGAIN
);
946 goto out_dmu_objset_disown
;
950 if (dmu_objset_is_snapshot(os
))
951 zv
->zv_flags
|= ZVOL_RDONLY
;
953 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
954 zv
->zv_volsize
= volsize
;
957 set_capacity(zv
->zv_zso
->zvo_disk
, zv
->zv_volsize
>> 9);
959 blk_queue_max_hw_sectors(zv
->zv_zso
->zvo_queue
,
960 (DMU_MAX_ACCESS
/ 4) >> 9);
961 blk_queue_max_segments(zv
->zv_zso
->zvo_queue
, UINT16_MAX
);
962 blk_queue_max_segment_size(zv
->zv_zso
->zvo_queue
, UINT_MAX
);
963 blk_queue_physical_block_size(zv
->zv_zso
->zvo_queue
,
964 zv
->zv_volblocksize
);
965 blk_queue_io_opt(zv
->zv_zso
->zvo_queue
, zv
->zv_volblocksize
);
966 blk_queue_max_discard_sectors(zv
->zv_zso
->zvo_queue
,
967 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
968 blk_queue_discard_granularity(zv
->zv_zso
->zvo_queue
,
969 zv
->zv_volblocksize
);
970 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, zv
->zv_zso
->zvo_queue
);
971 #ifdef QUEUE_FLAG_NONROT
972 blk_queue_flag_set(QUEUE_FLAG_NONROT
, zv
->zv_zso
->zvo_queue
);
974 #ifdef QUEUE_FLAG_ADD_RANDOM
975 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_zso
->zvo_queue
);
977 /* This flag was introduced in kernel version 4.12. */
978 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
979 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH
, zv
->zv_zso
->zvo_queue
);
982 if (spa_writeable(dmu_objset_spa(os
))) {
983 if (zil_replay_disable
)
984 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
986 zil_replay(os
, zv
, zvol_replay_vector
);
988 ASSERT3P(zv
->zv_kstat
.dk_kstats
, ==, NULL
);
989 dataset_kstats_create(&zv
->zv_kstat
, zv
->zv_objset
);
992 * When udev detects the addition of the device it will immediately
993 * invoke blkid(8) to determine the type of content on the device.
994 * Prefetching the blocks commonly scanned by blkid(8) will speed
997 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
999 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1000 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1001 ZIO_PRIORITY_SYNC_READ
);
1004 zv
->zv_objset
= NULL
;
1005 out_dmu_objset_disown
:
1006 dmu_objset_disown(os
, B_TRUE
, FTAG
);
1008 kmem_free(doi
, sizeof (dmu_object_info_t
));
1011 * Keep in mind that once add_disk() is called, the zvol is
1012 * announced to the world, and zvol_open()/zvol_release() can
1013 * be called at any time. Incidentally, add_disk() itself calls
1014 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1018 rw_enter(&zvol_state_lock
, RW_WRITER
);
1020 rw_exit(&zvol_state_lock
);
1021 add_disk(zv
->zv_zso
->zvo_disk
);
1023 ida_simple_remove(&zvol_ida
, idx
);
1030 zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1032 int readonly
= get_disk_ro(zv
->zv_zso
->zvo_disk
);
1034 ASSERT(RW_LOCK_HELD(&zvol_state_lock
));
1035 ASSERT(MUTEX_HELD(&zv
->zv_state_lock
));
1037 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1039 /* move to new hashtable entry */
1040 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1041 hlist_del(&zv
->zv_hlink
);
1042 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1045 * The block device's read-only state is briefly changed causing
1046 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1047 * the name change and fixes the symlinks. This does not change
1048 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1049 * changes. This would normally be done using kobject_uevent() but
1050 * that is a GPL-only symbol which is why we need this workaround.
1052 set_disk_ro(zv
->zv_zso
->zvo_disk
, !readonly
);
1053 set_disk_ro(zv
->zv_zso
->zvo_disk
, readonly
);
1057 zvol_set_disk_ro_impl(zvol_state_t
*zv
, int flags
)
1060 set_disk_ro(zv
->zv_zso
->zvo_disk
, flags
);
1064 zvol_set_capacity_impl(zvol_state_t
*zv
, uint64_t capacity
)
1067 set_capacity(zv
->zv_zso
->zvo_disk
, capacity
);
1070 const static zvol_platform_ops_t zvol_linux_ops
= {
1071 .zv_free
= zvol_free
,
1072 .zv_rename_minor
= zvol_rename_minor
,
1073 .zv_create_minor
= zvol_os_create_minor
,
1074 .zv_update_volsize
= zvol_update_volsize
,
1075 .zv_clear_private
= zvol_clear_private
,
1076 .zv_is_zvol
= zvol_is_zvol_impl
,
1077 .zv_set_disk_ro
= zvol_set_disk_ro_impl
,
1078 .zv_set_capacity
= zvol_set_capacity_impl
,
1085 int threads
= MIN(MAX(zvol_threads
, 1), 1024);
1087 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1089 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1092 zvol_taskq
= taskq_create(ZVOL_DRIVER
, threads
, maxclsyspri
,
1093 threads
* 2, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1094 if (zvol_taskq
== NULL
) {
1095 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1099 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
1100 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
1102 ida_init(&zvol_ida
);
1103 zvol_register_ops(&zvol_linux_ops
);
1111 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
1112 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1113 taskq_destroy(zvol_taskq
);
1114 ida_destroy(&zvol_ida
);
1118 module_param(zvol_inhibit_dev
, uint
, 0644);
1119 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1121 module_param(zvol_major
, uint
, 0444);
1122 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1124 module_param(zvol_threads
, uint
, 0444);
1125 MODULE_PARM_DESC(zvol_threads
, "Max number of threads to handle I/O requests");
1127 module_param(zvol_request_sync
, uint
, 0644);
1128 MODULE_PARM_DESC(zvol_request_sync
, "Synchronously handle bio requests");
1130 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1131 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1133 module_param(zvol_prefetch_bytes
, uint
, 0644);
1134 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");
1136 module_param(zvol_volmode
, uint
, 0644);
1137 MODULE_PARM_DESC(zvol_volmode
, "Default volmode property value");