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) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
27 * ZFS volume emulation driver.
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
32 * /dev/<pool_name>/<dataset_name>
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
38 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
42 #include <sys/dmu_traverse.h>
43 #include <sys/dsl_dataset.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_dir.h>
47 #include <sys/zfeature.h>
48 #include <sys/zil_impl.h>
49 #include <sys/dmu_tx.h>
51 #include <sys/zfs_rlock.h>
52 #include <sys/zfs_znode.h>
53 #include <sys/spa_impl.h>
55 #include <linux/blkdev_compat.h>
57 unsigned int zvol_inhibit_dev
= 0;
58 unsigned int zvol_major
= ZVOL_MAJOR
;
59 unsigned int zvol_prefetch_bytes
= (128 * 1024);
60 unsigned long zvol_max_discard_blocks
= 16384;
62 static kmutex_t zvol_state_lock
;
63 static list_t zvol_state_list
;
65 #define ZVOL_HT_SIZE 1024
66 static struct hlist_head
*zvol_htable
;
67 #define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
69 static struct ida zvol_ida
;
72 * The in-core state of each volume.
75 char zv_name
[MAXNAMELEN
]; /* name */
76 uint64_t zv_volsize
; /* advertised space */
77 uint64_t zv_volblocksize
; /* volume block size */
78 objset_t
*zv_objset
; /* objset handle */
79 uint32_t zv_flags
; /* ZVOL_* flags */
80 uint32_t zv_open_count
; /* open counts */
81 uint32_t zv_changed
; /* disk changed */
82 zilog_t
*zv_zilog
; /* ZIL handle */
83 zfs_rlock_t zv_range_lock
; /* range lock */
84 dmu_buf_t
*zv_dbuf
; /* bonus handle */
85 dev_t zv_dev
; /* device id */
86 struct gendisk
*zv_disk
; /* generic disk */
87 struct request_queue
*zv_queue
; /* request queue */
88 list_node_t zv_next
; /* next zvol_state_t linkage */
89 uint64_t zv_hash
; /* name hash */
90 struct hlist_node zv_hlink
; /* hash link */
91 atomic_t zv_suspend_ref
; /* refcount for suspend */
92 krwlock_t zv_suspend_lock
; /* suspend lock */
96 ZVOL_ASYNC_CREATE_MINORS
,
97 ZVOL_ASYNC_REMOVE_MINORS
,
98 ZVOL_ASYNC_RENAME_MINORS
,
99 ZVOL_ASYNC_SET_SNAPDEV
,
105 char pool
[MAXNAMELEN
];
106 char name1
[MAXNAMELEN
];
107 char name2
[MAXNAMELEN
];
108 zprop_source_t source
;
112 #define ZVOL_RDONLY 0x1
115 zvol_name_hash(const char *name
)
118 uint64_t crc
= -1ULL;
119 uint8_t *p
= (uint8_t *)name
;
120 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
121 for (i
= 0; i
< MAXNAMELEN
- 1 && *p
; i
++, p
++) {
122 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
128 * Find a zvol_state_t given the full major+minor dev_t.
130 static zvol_state_t
*
131 zvol_find_by_dev(dev_t dev
)
135 ASSERT(MUTEX_HELD(&zvol_state_lock
));
136 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
137 zv
= list_next(&zvol_state_list
, zv
)) {
138 if (zv
->zv_dev
== dev
)
146 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
148 static zvol_state_t
*
149 zvol_find_by_name_hash(const char *name
, uint64_t hash
)
152 struct hlist_node
*p
;
154 ASSERT(MUTEX_HELD(&zvol_state_lock
));
155 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
156 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
157 if (zv
->zv_hash
== hash
&&
158 strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0)
165 * Find a zvol_state_t given the name provided at zvol_alloc() time.
167 static zvol_state_t
*
168 zvol_find_by_name(const char *name
)
170 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
)));
175 * Given a path, return TRUE if path is a ZVOL.
178 zvol_is_zvol(const char *device
)
180 struct block_device
*bdev
;
183 bdev
= vdev_lookup_bdev(device
);
187 major
= MAJOR(bdev
->bd_dev
);
190 if (major
== zvol_major
)
197 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
200 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
202 zfs_creat_t
*zct
= arg
;
203 nvlist_t
*nvprops
= zct
->zct_props
;
205 uint64_t volblocksize
, volsize
;
207 VERIFY(nvlist_lookup_uint64(nvprops
,
208 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
209 if (nvlist_lookup_uint64(nvprops
,
210 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
211 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
214 * These properties must be removed from the list so the generic
215 * property setting step won't apply to them.
217 VERIFY(nvlist_remove_all(nvprops
,
218 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
219 (void) nvlist_remove_all(nvprops
,
220 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
222 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
226 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
230 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
235 * ZFS_IOC_OBJSET_STATS entry point.
238 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
241 dmu_object_info_t
*doi
;
244 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
246 return (SET_ERROR(error
));
248 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
249 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
250 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
253 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
254 doi
->doi_data_block_size
);
257 kmem_free(doi
, sizeof (dmu_object_info_t
));
259 return (SET_ERROR(error
));
263 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
265 struct block_device
*bdev
;
267 bdev
= bdget_disk(zv
->zv_disk
, 0);
270 set_capacity(zv
->zv_disk
, volsize
>> 9);
271 zv
->zv_volsize
= volsize
;
272 check_disk_size_change(zv
->zv_disk
, bdev
);
278 * Sanity check volume size.
281 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
284 return (SET_ERROR(EINVAL
));
286 if (volsize
% blocksize
!= 0)
287 return (SET_ERROR(EINVAL
));
290 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
291 return (SET_ERROR(EOVERFLOW
));
297 * Ensure the zap is flushed then inform the VFS of the capacity change.
300 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
306 ASSERT(MUTEX_HELD(&zvol_state_lock
));
308 tx
= dmu_tx_create(os
);
309 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
310 dmu_tx_mark_netfree(tx
);
311 error
= dmu_tx_assign(tx
, TXG_WAIT
);
314 return (SET_ERROR(error
));
316 txg
= dmu_tx_get_txg(tx
);
318 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
322 txg_wait_synced(dmu_objset_pool(os
), txg
);
325 error
= dmu_free_long_range(os
,
326 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
332 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
334 zvol_size_changed(zv
, volsize
);
337 * We should post a event here describing the expansion. However,
338 * the zfs_ereport_post() interface doesn't nicely support posting
339 * events for zvols, it assumes events relate to vdevs or zios.
346 * Set ZFS_PROP_VOLSIZE set entry point.
349 zvol_set_volsize(const char *name
, uint64_t volsize
)
351 zvol_state_t
*zv
= NULL
;
354 dmu_object_info_t
*doi
;
356 boolean_t owned
= B_FALSE
;
358 error
= dsl_prop_get_integer(name
,
359 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
361 return (SET_ERROR(error
));
363 return (SET_ERROR(EROFS
));
365 mutex_enter(&zvol_state_lock
);
366 zv
= zvol_find_by_name(name
);
368 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
369 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
371 mutex_exit(&zvol_state_lock
);
372 return (SET_ERROR(error
));
378 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
382 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
384 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
385 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
388 error
= zvol_update_volsize(volsize
, os
);
389 kmem_free(doi
, sizeof (dmu_object_info_t
));
391 if (error
== 0 && zv
!= NULL
)
392 error
= zvol_update_live_volsize(zv
, volsize
);
395 dmu_objset_disown(os
, FTAG
);
397 zv
->zv_objset
= NULL
;
399 rw_exit(&zv
->zv_suspend_lock
);
401 mutex_exit(&zvol_state_lock
);
406 * Sanity check volume block size.
409 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
411 /* Record sizes above 128k need the feature to be enabled */
412 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
416 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
419 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
420 spa_close(spa
, FTAG
);
421 return (SET_ERROR(ENOTSUP
));
425 * We don't allow setting the property above 1MB,
426 * unless the tunable has been changed.
428 if (volblocksize
> zfs_max_recordsize
)
429 return (SET_ERROR(EDOM
));
431 spa_close(spa
, FTAG
);
434 if (volblocksize
< SPA_MINBLOCKSIZE
||
435 volblocksize
> SPA_MAXBLOCKSIZE
||
437 return (SET_ERROR(EDOM
));
443 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
446 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
452 mutex_enter(&zvol_state_lock
);
454 zv
= zvol_find_by_name(name
);
456 error
= SET_ERROR(ENXIO
);
460 if (zv
->zv_flags
& ZVOL_RDONLY
) {
461 error
= SET_ERROR(EROFS
);
465 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
467 tx
= dmu_tx_create(zv
->zv_objset
);
468 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
469 error
= dmu_tx_assign(tx
, TXG_WAIT
);
473 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
474 volblocksize
, 0, tx
);
475 if (error
== ENOTSUP
)
476 error
= SET_ERROR(EBUSY
);
479 zv
->zv_volblocksize
= volblocksize
;
481 rw_exit(&zv
->zv_suspend_lock
);
483 mutex_exit(&zvol_state_lock
);
485 return (SET_ERROR(error
));
489 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
490 * implement DKIOCFREE/free-long-range.
493 zvol_replay_truncate(zvol_state_t
*zv
, lr_truncate_t
*lr
, boolean_t byteswap
)
495 uint64_t offset
, length
;
498 byteswap_uint64_array(lr
, sizeof (*lr
));
500 offset
= lr
->lr_offset
;
501 length
= lr
->lr_length
;
503 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
507 * Replay a TX_WRITE ZIL transaction that didn't get committed
508 * after a system failure
511 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
513 objset_t
*os
= zv
->zv_objset
;
514 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
515 uint64_t off
= lr
->lr_offset
;
516 uint64_t len
= lr
->lr_length
;
521 byteswap_uint64_array(lr
, sizeof (*lr
));
523 tx
= dmu_tx_create(os
);
524 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
525 error
= dmu_tx_assign(tx
, TXG_WAIT
);
529 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
533 return (SET_ERROR(error
));
537 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
539 return (SET_ERROR(ENOTSUP
));
543 * Callback vectors for replaying records.
544 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
546 zil_replay_func_t zvol_replay_vector
[TX_MAX_TYPE
] = {
547 (zil_replay_func_t
)zvol_replay_err
, /* no such transaction type */
548 (zil_replay_func_t
)zvol_replay_err
, /* TX_CREATE */
549 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKDIR */
550 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKXATTR */
551 (zil_replay_func_t
)zvol_replay_err
, /* TX_SYMLINK */
552 (zil_replay_func_t
)zvol_replay_err
, /* TX_REMOVE */
553 (zil_replay_func_t
)zvol_replay_err
, /* TX_RMDIR */
554 (zil_replay_func_t
)zvol_replay_err
, /* TX_LINK */
555 (zil_replay_func_t
)zvol_replay_err
, /* TX_RENAME */
556 (zil_replay_func_t
)zvol_replay_write
, /* TX_WRITE */
557 (zil_replay_func_t
)zvol_replay_truncate
, /* TX_TRUNCATE */
558 (zil_replay_func_t
)zvol_replay_err
, /* TX_SETATTR */
559 (zil_replay_func_t
)zvol_replay_err
, /* TX_ACL */
563 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
565 * We store data in the log buffers if it's small enough.
566 * Otherwise we will later flush the data out via dmu_sync().
568 ssize_t zvol_immediate_write_sz
= 32768;
571 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
572 uint64_t size
, int sync
)
574 uint32_t blocksize
= zv
->zv_volblocksize
;
575 zilog_t
*zilog
= zv
->zv_zilog
;
577 ssize_t immediate_write_sz
;
579 if (zil_replaying(zilog
, tx
))
582 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
583 ? 0 : zvol_immediate_write_sz
;
584 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
585 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
591 itx_wr_state_t write_state
;
594 * Unlike zfs_log_write() we can be called with
595 * up to DMU_MAX_ACCESS/2 (5MB) writes.
597 if (blocksize
> immediate_write_sz
&& !slogging
&&
598 size
>= blocksize
&& offset
% blocksize
== 0) {
599 write_state
= WR_INDIRECT
; /* uses dmu_sync */
602 write_state
= WR_COPIED
;
603 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
605 write_state
= WR_NEED_COPY
;
606 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
609 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
610 (write_state
== WR_COPIED
? len
: 0));
611 lr
= (lr_write_t
*)&itx
->itx_lr
;
612 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
613 ZVOL_OBJ
, offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
614 zil_itx_destroy(itx
);
615 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
616 lr
= (lr_write_t
*)&itx
->itx_lr
;
617 write_state
= WR_NEED_COPY
;
620 itx
->itx_wr_state
= write_state
;
621 if (write_state
== WR_NEED_COPY
)
623 lr
->lr_foid
= ZVOL_OBJ
;
624 lr
->lr_offset
= offset
;
627 BP_ZERO(&lr
->lr_blkptr
);
629 itx
->itx_private
= zv
;
630 itx
->itx_sync
= sync
;
632 (void) zil_itx_assign(zilog
, itx
, tx
);
640 zvol_write(zvol_state_t
*zv
, uio_t
*uio
, boolean_t sync
)
642 uint64_t volsize
= zv
->zv_volsize
;
646 ASSERT(zv
&& zv
->zv_open_count
> 0);
648 rl
= zfs_range_lock(&zv
->zv_range_lock
, uio
->uio_loffset
,
649 uio
->uio_resid
, RL_WRITER
);
651 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
652 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
653 uint64_t off
= uio
->uio_loffset
;
654 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
656 if (bytes
> volsize
- off
) /* don't write past the end */
657 bytes
= volsize
- off
;
659 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
661 /* This will only fail for ENOSPC */
662 error
= dmu_tx_assign(tx
, TXG_WAIT
);
667 error
= dmu_write_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
, tx
);
669 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
675 zfs_range_unlock(rl
);
677 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
682 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
685 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
690 zilog_t
*zilog
= zv
->zv_zilog
;
692 if (zil_replaying(zilog
, tx
))
695 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
696 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
697 lr
->lr_foid
= ZVOL_OBJ
;
701 itx
->itx_sync
= sync
;
702 zil_itx_assign(zilog
, itx
, tx
);
706 zvol_discard(struct bio
*bio
)
708 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
709 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
710 uint64_t size
= BIO_BI_SIZE(bio
);
711 uint64_t end
= start
+ size
;
716 ASSERT(zv
&& zv
->zv_open_count
> 0);
718 if (end
> zv
->zv_volsize
)
719 return (SET_ERROR(EIO
));
722 * Align the request to volume block boundaries when a secure erase is
723 * not required. This will prevent dnode_free_range() from zeroing out
724 * the unaligned parts which is slow (read-modify-write) and useless
725 * since we are not freeing any space by doing so.
727 if (!bio_is_secure_erase(bio
)) {
728 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
729 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
736 rl
= zfs_range_lock(&zv
->zv_range_lock
, start
, size
, RL_WRITER
);
737 tx
= dmu_tx_create(zv
->zv_objset
);
738 dmu_tx_mark_netfree(tx
);
739 error
= dmu_tx_assign(tx
, TXG_WAIT
);
743 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
745 error
= dmu_free_long_range(zv
->zv_objset
,
746 ZVOL_OBJ
, start
, size
);
749 zfs_range_unlock(rl
);
755 zvol_read(zvol_state_t
*zv
, uio_t
*uio
)
757 uint64_t volsize
= zv
->zv_volsize
;
761 ASSERT(zv
&& zv
->zv_open_count
> 0);
763 rl
= zfs_range_lock(&zv
->zv_range_lock
, uio
->uio_loffset
,
764 uio
->uio_resid
, RL_READER
);
765 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
766 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
768 /* don't read past the end */
769 if (bytes
> volsize
- uio
->uio_loffset
)
770 bytes
= volsize
- uio
->uio_loffset
;
772 error
= dmu_read_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
);
774 /* convert checksum errors into IO errors */
776 error
= SET_ERROR(EIO
);
780 zfs_range_unlock(rl
);
784 static MAKE_REQUEST_FN_RET
785 zvol_request(struct request_queue
*q
, struct bio
*bio
)
788 zvol_state_t
*zv
= q
->queuedata
;
789 fstrans_cookie_t cookie
= spl_fstrans_mark();
790 int rw
= bio_data_dir(bio
);
791 #ifdef HAVE_GENERIC_IO_ACCT
792 unsigned long start
= jiffies
;
796 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
798 uio
.uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
799 uio
.uio_skip
= BIO_BI_SKIP(bio
);
800 uio
.uio_resid
= BIO_BI_SIZE(bio
);
801 uio
.uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
802 uio
.uio_loffset
= BIO_BI_SECTOR(bio
) << 9;
803 uio
.uio_limit
= MAXOFFSET_T
;
804 uio
.uio_segflg
= UIO_BVEC
;
806 if (bio_has_data(bio
) && uio
.uio_loffset
+ uio
.uio_resid
>
809 "%s: bad access: offset=%llu, size=%lu\n",
810 zv
->zv_disk
->disk_name
,
811 (long long unsigned)uio
.uio_loffset
,
812 (long unsigned)uio
.uio_resid
);
813 error
= SET_ERROR(EIO
);
817 generic_start_io_acct(rw
, bio_sectors(bio
), &zv
->zv_disk
->part0
);
820 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
821 error
= SET_ERROR(EROFS
);
825 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
826 error
= zvol_discard(bio
);
831 * Some requests are just for flush and nothing else.
833 if (uio
.uio_resid
== 0) {
834 if (bio_is_flush(bio
))
835 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
839 error
= zvol_write(zv
, &uio
,
840 bio_is_flush(bio
) || bio_is_fua(bio
) ||
841 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
);
843 error
= zvol_read(zv
, &uio
);
846 generic_end_io_acct(rw
, &zv
->zv_disk
->part0
, start
);
848 BIO_END_IO(bio
, -error
);
849 rw_exit(&zv
->zv_suspend_lock
);
850 spl_fstrans_unmark(cookie
);
851 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
853 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
854 return (BLK_QC_T_NONE
);
859 zvol_get_done(zgd_t
*zgd
, int error
)
862 dmu_buf_rele(zgd
->zgd_db
, zgd
);
864 zfs_range_unlock(zgd
->zgd_rl
);
866 if (error
== 0 && zgd
->zgd_bp
)
867 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
869 kmem_free(zgd
, sizeof (zgd_t
));
873 * Get data to generate a TX_WRITE intent log record.
876 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
878 zvol_state_t
*zv
= arg
;
879 objset_t
*os
= zv
->zv_objset
;
880 uint64_t object
= ZVOL_OBJ
;
881 uint64_t offset
= lr
->lr_offset
;
882 uint64_t size
= lr
->lr_length
;
883 blkptr_t
*bp
= &lr
->lr_blkptr
;
891 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
892 zgd
->zgd_zilog
= zv
->zv_zilog
;
893 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
897 * Write records come in two flavors: immediate and indirect.
898 * For small writes it's cheaper to store the data with the
899 * log record (immediate); for large writes it's cheaper to
900 * sync the data and get a pointer to it (indirect) so that
901 * we don't have to write the data twice.
903 if (buf
!= NULL
) { /* immediate write */
904 error
= dmu_read(os
, object
, offset
, size
, buf
,
905 DMU_READ_NO_PREFETCH
);
907 size
= zv
->zv_volblocksize
;
908 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
909 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
910 DMU_READ_NO_PREFETCH
);
912 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
914 ASSERT(BP_IS_HOLE(bp
));
919 zgd
->zgd_bp
= &lr
->lr_blkptr
;
922 ASSERT(db
->db_offset
== offset
);
923 ASSERT(db
->db_size
== size
);
925 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
933 zvol_get_done(zgd
, error
);
935 return (SET_ERROR(error
));
939 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
942 zvol_insert(zvol_state_t
*zv
)
944 ASSERT(MUTEX_HELD(&zvol_state_lock
));
945 ASSERT3U(MINOR(zv
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
946 list_insert_head(&zvol_state_list
, zv
);
947 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
951 * Simply remove the zvol from to list of zvols.
954 zvol_remove(zvol_state_t
*zv
)
956 ASSERT(MUTEX_HELD(&zvol_state_lock
));
957 list_remove(&zvol_state_list
, zv
);
958 hlist_del(&zv
->zv_hlink
);
962 * Setup zv after we just own the zv->objset
965 zvol_setup_zv(zvol_state_t
*zv
)
970 objset_t
*os
= zv
->zv_objset
;
972 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
974 return (SET_ERROR(error
));
976 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
978 return (SET_ERROR(error
));
980 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zv
, &zv
->zv_dbuf
);
982 return (SET_ERROR(error
));
984 set_capacity(zv
->zv_disk
, volsize
>> 9);
985 zv
->zv_volsize
= volsize
;
986 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
988 if (ro
|| dmu_objset_is_snapshot(os
) ||
989 !spa_writeable(dmu_objset_spa(os
))) {
990 set_disk_ro(zv
->zv_disk
, 1);
991 zv
->zv_flags
|= ZVOL_RDONLY
;
993 set_disk_ro(zv
->zv_disk
, 0);
994 zv
->zv_flags
&= ~ZVOL_RDONLY
;
1000 * Shutdown every zv_objset related stuff except zv_objset itself.
1001 * The is the reverse of zvol_setup_zv.
1004 zvol_shutdown_zv(zvol_state_t
*zv
)
1006 zil_close(zv
->zv_zilog
);
1007 zv
->zv_zilog
= NULL
;
1009 dmu_buf_rele(zv
->zv_dbuf
, zv
);
1015 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
1016 !(zv
->zv_flags
& ZVOL_RDONLY
))
1017 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1018 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
1022 * return the proper tag for rollback and recv
1025 zvol_tag(zvol_state_t
*zv
)
1027 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
1028 return (zv
->zv_open_count
> 0 ? zv
: NULL
);
1032 * Suspend the zvol for recv and rollback.
1035 zvol_suspend(const char *name
)
1039 mutex_enter(&zvol_state_lock
);
1040 zv
= zvol_find_by_name(name
);
1044 /* block all I/O, release in zvol_resume. */
1045 rw_enter(&zv
->zv_suspend_lock
, RW_WRITER
);
1047 atomic_inc(&zv
->zv_suspend_ref
);
1049 if (zv
->zv_open_count
> 0)
1050 zvol_shutdown_zv(zv
);
1052 mutex_exit(&zvol_state_lock
);
1057 zvol_resume(zvol_state_t
*zv
)
1061 ASSERT(RW_WRITE_HELD(&zv
->zv_suspend_lock
));
1062 if (zv
->zv_open_count
> 0) {
1063 VERIFY0(dmu_objset_hold(zv
->zv_name
, zv
, &zv
->zv_objset
));
1064 VERIFY3P(zv
->zv_objset
->os_dsl_dataset
->ds_owner
, ==, zv
);
1065 VERIFY(dsl_dataset_long_held(zv
->zv_objset
->os_dsl_dataset
));
1066 dmu_objset_rele(zv
->zv_objset
, zv
);
1068 error
= zvol_setup_zv(zv
);
1070 rw_exit(&zv
->zv_suspend_lock
);
1072 * We need this because we don't hold zvol_state_lock while releasing
1073 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
1074 * zv_suspend_lock to determine it is safe to free because rwlock is
1075 * not inherent atomic.
1077 atomic_dec(&zv
->zv_suspend_ref
);
1079 return (SET_ERROR(error
));
1083 zvol_first_open(zvol_state_t
*zv
)
1088 /* lie and say we're read-only */
1089 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, 1, zv
, &os
);
1091 return (SET_ERROR(-error
));
1095 error
= zvol_setup_zv(zv
);
1098 dmu_objset_disown(os
, zv
);
1099 zv
->zv_objset
= NULL
;
1102 return (SET_ERROR(-error
));
1106 zvol_last_close(zvol_state_t
*zv
)
1108 zvol_shutdown_zv(zv
);
1110 dmu_objset_disown(zv
->zv_objset
, zv
);
1111 zv
->zv_objset
= NULL
;
1115 zvol_open(struct block_device
*bdev
, fmode_t flag
)
1118 int error
= 0, drop_mutex
= 0, drop_suspend
= 0;
1121 * If the caller is already holding the mutex do not take it
1122 * again, this will happen as part of zvol_create_minor_impl().
1123 * Once add_disk() is called the device is live and the kernel
1124 * will attempt to open it to read the partition information.
1126 if (!mutex_owned(&zvol_state_lock
)) {
1127 mutex_enter(&zvol_state_lock
);
1132 * Obtain a copy of private_data under the lock to make sure
1133 * that either the result of zvol_free() setting
1134 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1135 * is not called on this zv because of the positive zv_open_count.
1137 zv
= bdev
->bd_disk
->private_data
;
1143 if (zv
->zv_open_count
== 0) {
1144 /* make sure zvol is not suspended when first open */
1145 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1148 error
= zvol_first_open(zv
);
1153 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1155 goto out_open_count
;
1158 zv
->zv_open_count
++;
1160 check_disk_change(bdev
);
1163 if (zv
->zv_open_count
== 0)
1164 zvol_last_close(zv
);
1167 rw_exit(&zv
->zv_suspend_lock
);
1169 mutex_exit(&zvol_state_lock
);
1171 return (SET_ERROR(error
));
1174 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1179 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1181 zvol_state_t
*zv
= disk
->private_data
;
1184 ASSERT(zv
&& zv
->zv_open_count
> 0);
1186 if (!mutex_owned(&zvol_state_lock
)) {
1187 mutex_enter(&zvol_state_lock
);
1191 /* make sure zvol is not suspended when last close */
1192 if (zv
->zv_open_count
== 1)
1193 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1195 zv
->zv_open_count
--;
1196 if (zv
->zv_open_count
== 0) {
1197 zvol_last_close(zv
);
1198 rw_exit(&zv
->zv_suspend_lock
);
1202 mutex_exit(&zvol_state_lock
);
1204 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1210 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1211 unsigned int cmd
, unsigned long arg
)
1213 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1216 ASSERT(zv
&& zv
->zv_open_count
> 0);
1218 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1221 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1224 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1231 rw_exit(&zv
->zv_suspend_lock
);
1233 return (SET_ERROR(error
));
1236 #ifdef CONFIG_COMPAT
1238 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1239 unsigned cmd
, unsigned long arg
)
1241 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1244 #define zvol_compat_ioctl NULL
1247 static int zvol_media_changed(struct gendisk
*disk
)
1249 zvol_state_t
*zv
= disk
->private_data
;
1251 ASSERT(zv
&& zv
->zv_open_count
> 0);
1253 return (zv
->zv_changed
);
1256 static int zvol_revalidate_disk(struct gendisk
*disk
)
1258 zvol_state_t
*zv
= disk
->private_data
;
1260 ASSERT(zv
&& zv
->zv_open_count
> 0);
1263 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1269 * Provide a simple virtual geometry for legacy compatibility. For devices
1270 * smaller than 1 MiB a small head and sector count is used to allow very
1271 * tiny devices. For devices over 1 Mib a standard head and sector count
1272 * is used to keep the cylinders count reasonable.
1275 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1277 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1280 ASSERT(zv
&& zv
->zv_open_count
> 0);
1282 sectors
= get_capacity(zv
->zv_disk
);
1284 if (sectors
> 2048) {
1293 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1298 static struct kobject
*
1299 zvol_probe(dev_t dev
, int *part
, void *arg
)
1302 struct kobject
*kobj
;
1304 mutex_enter(&zvol_state_lock
);
1305 zv
= zvol_find_by_dev(dev
);
1306 kobj
= zv
? get_disk(zv
->zv_disk
) : NULL
;
1307 mutex_exit(&zvol_state_lock
);
1312 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1313 static struct block_device_operations zvol_ops
= {
1315 .release
= zvol_release
,
1316 .ioctl
= zvol_ioctl
,
1317 .compat_ioctl
= zvol_compat_ioctl
,
1318 .media_changed
= zvol_media_changed
,
1319 .revalidate_disk
= zvol_revalidate_disk
,
1320 .getgeo
= zvol_getgeo
,
1321 .owner
= THIS_MODULE
,
1324 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1327 zvol_open_by_inode(struct inode
*inode
, struct file
*file
)
1329 return (zvol_open(inode
->i_bdev
, file
->f_mode
));
1333 zvol_release_by_inode(struct inode
*inode
, struct file
*file
)
1335 return (zvol_release(inode
->i_bdev
->bd_disk
, file
->f_mode
));
1339 zvol_ioctl_by_inode(struct inode
*inode
, struct file
*file
,
1340 unsigned int cmd
, unsigned long arg
)
1342 if (file
== NULL
|| inode
== NULL
)
1343 return (SET_ERROR(-EINVAL
));
1345 return (zvol_ioctl(inode
->i_bdev
, file
->f_mode
, cmd
, arg
));
1348 #ifdef CONFIG_COMPAT
1350 zvol_compat_ioctl_by_inode(struct file
*file
,
1351 unsigned int cmd
, unsigned long arg
)
1354 return (SET_ERROR(-EINVAL
));
1356 return (zvol_compat_ioctl(file
->f_dentry
->d_inode
->i_bdev
,
1357 file
->f_mode
, cmd
, arg
));
1360 #define zvol_compat_ioctl_by_inode NULL
1363 static struct block_device_operations zvol_ops
= {
1364 .open
= zvol_open_by_inode
,
1365 .release
= zvol_release_by_inode
,
1366 .ioctl
= zvol_ioctl_by_inode
,
1367 .compat_ioctl
= zvol_compat_ioctl_by_inode
,
1368 .media_changed
= zvol_media_changed
,
1369 .revalidate_disk
= zvol_revalidate_disk
,
1370 .getgeo
= zvol_getgeo
,
1371 .owner
= THIS_MODULE
,
1373 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1376 * Allocate memory for a new zvol_state_t and setup the required
1377 * request queue and generic disk structures for the block device.
1379 static zvol_state_t
*
1380 zvol_alloc(dev_t dev
, const char *name
)
1384 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1386 list_link_init(&zv
->zv_next
);
1388 zv
->zv_queue
= blk_alloc_queue(GFP_ATOMIC
);
1389 if (zv
->zv_queue
== NULL
)
1392 blk_queue_make_request(zv
->zv_queue
, zvol_request
);
1393 blk_queue_set_write_cache(zv
->zv_queue
, B_TRUE
, B_TRUE
);
1395 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1396 if (zv
->zv_disk
== NULL
)
1399 zv
->zv_queue
->queuedata
= zv
;
1401 zv
->zv_open_count
= 0;
1402 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1404 zfs_rlock_init(&zv
->zv_range_lock
);
1405 rw_init(&zv
->zv_suspend_lock
, NULL
, RW_DEFAULT
, NULL
);
1407 zv
->zv_disk
->major
= zvol_major
;
1408 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1409 zv
->zv_disk
->fops
= &zvol_ops
;
1410 zv
->zv_disk
->private_data
= zv
;
1411 zv
->zv_disk
->queue
= zv
->zv_queue
;
1412 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1413 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1418 blk_cleanup_queue(zv
->zv_queue
);
1420 kmem_free(zv
, sizeof (zvol_state_t
));
1426 * Used for taskq, if used out side zvol_state_lock, you need to clear
1427 * zv_disk->private_data inside lock first.
1430 zvol_free_impl(void *arg
)
1432 zvol_state_t
*zv
= arg
;
1433 ASSERT(zv
->zv_open_count
== 0);
1435 rw_destroy(&zv
->zv_suspend_lock
);
1436 zfs_rlock_destroy(&zv
->zv_range_lock
);
1438 zv
->zv_disk
->private_data
= NULL
;
1440 del_gendisk(zv
->zv_disk
);
1441 blk_cleanup_queue(zv
->zv_queue
);
1442 put_disk(zv
->zv_disk
);
1444 ida_simple_remove(&zvol_ida
, MINOR(zv
->zv_dev
) >> ZVOL_MINOR_BITS
);
1445 kmem_free(zv
, sizeof (zvol_state_t
));
1449 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1452 zvol_free(zvol_state_t
*zv
)
1454 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1459 * Create a block device minor node and setup the linkage between it
1460 * and the specified volume. Once this function returns the block
1461 * device is live and ready for use.
1464 zvol_create_minor_impl(const char *name
)
1468 dmu_object_info_t
*doi
;
1474 uint64_t hash
= zvol_name_hash(name
);
1476 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1478 return (SET_ERROR(-idx
));
1479 minor
= idx
<< ZVOL_MINOR_BITS
;
1481 mutex_enter(&zvol_state_lock
);
1483 zv
= zvol_find_by_name_hash(name
, hash
);
1485 error
= SET_ERROR(EEXIST
);
1489 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1491 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, FTAG
, &os
);
1495 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1497 goto out_dmu_objset_disown
;
1499 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1501 goto out_dmu_objset_disown
;
1503 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1505 error
= SET_ERROR(EAGAIN
);
1506 goto out_dmu_objset_disown
;
1510 if (dmu_objset_is_snapshot(os
))
1511 zv
->zv_flags
|= ZVOL_RDONLY
;
1513 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1514 zv
->zv_volsize
= volsize
;
1517 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1519 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1520 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1521 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1522 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1523 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1524 blk_queue_max_discard_sectors(zv
->zv_queue
,
1525 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1526 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1527 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1528 #ifdef QUEUE_FLAG_NONROT
1529 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1531 #ifdef QUEUE_FLAG_ADD_RANDOM
1532 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_queue
);
1535 if (spa_writeable(dmu_objset_spa(os
))) {
1536 if (zil_replay_disable
)
1537 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1539 zil_replay(os
, zv
, zvol_replay_vector
);
1543 * When udev detects the addition of the device it will immediately
1544 * invoke blkid(8) to determine the type of content on the device.
1545 * Prefetching the blocks commonly scanned by blkid(8) will speed
1548 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1550 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1551 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1552 ZIO_PRIORITY_SYNC_READ
);
1555 zv
->zv_objset
= NULL
;
1556 out_dmu_objset_disown
:
1557 dmu_objset_disown(os
, FTAG
);
1559 kmem_free(doi
, sizeof (dmu_object_info_t
));
1565 * Drop the lock to prevent deadlock with sys_open() ->
1566 * zvol_open(), which first takes bd_disk->bd_mutex and then
1567 * takes zvol_state_lock, whereas this code path first takes
1568 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1570 mutex_exit(&zvol_state_lock
);
1571 add_disk(zv
->zv_disk
);
1573 mutex_exit(&zvol_state_lock
);
1574 ida_simple_remove(&zvol_ida
, idx
);
1577 return (SET_ERROR(error
));
1581 * Rename a block device minor mode for the specified volume.
1584 zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1586 int readonly
= get_disk_ro(zv
->zv_disk
);
1588 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1590 rw_enter(&zv
->zv_suspend_lock
, RW_READER
);
1591 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1592 rw_exit(&zv
->zv_suspend_lock
);
1594 /* move to new hashtable entry */
1595 zv
->zv_hash
= zvol_name_hash(zv
->zv_name
);
1596 hlist_del(&zv
->zv_hlink
);
1597 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
1600 * The block device's read-only state is briefly changed causing
1601 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1602 * the name change and fixes the symlinks. This does not change
1603 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1604 * changes. This would normally be done using kobject_uevent() but
1605 * that is a GPL-only symbol which is why we need this workaround.
1607 set_disk_ro(zv
->zv_disk
, !readonly
);
1608 set_disk_ro(zv
->zv_disk
, readonly
);
1611 typedef struct minors_job
{
1621 * Prefetch zvol dnodes for the minors_job
1624 zvol_prefetch_minors_impl(void *arg
)
1626 minors_job_t
*job
= arg
;
1627 char *dsname
= job
->name
;
1628 objset_t
*os
= NULL
;
1630 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, FTAG
,
1632 if (job
->error
== 0) {
1633 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, 0, ZIO_PRIORITY_SYNC_READ
);
1634 dmu_objset_disown(os
, FTAG
);
1639 * Mask errors to continue dmu_objset_find() traversal
1642 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
1644 minors_job_t
*j
= arg
;
1645 list_t
*minors_list
= j
->list
;
1646 const char *name
= j
->name
;
1648 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1650 /* skip the designated dataset */
1651 if (name
&& strcmp(dsname
, name
) == 0)
1654 /* at this point, the dsname should name a snapshot */
1655 if (strchr(dsname
, '@') == 0) {
1656 dprintf("zvol_create_snap_minor_cb(): "
1657 "%s is not a shapshot name\n", dsname
);
1660 char *n
= strdup(dsname
);
1664 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1666 job
->list
= minors_list
;
1668 list_insert_tail(minors_list
, job
);
1669 /* don't care if dispatch fails, because job->error is 0 */
1670 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1678 * Mask errors to continue dmu_objset_find() traversal
1681 zvol_create_minors_cb(const char *dsname
, void *arg
)
1685 list_t
*minors_list
= arg
;
1687 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1689 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1694 * Given the name and the 'snapdev' property, create device minor nodes
1695 * with the linkages to zvols/snapshots as needed.
1696 * If the name represents a zvol, create a minor node for the zvol, then
1697 * check if its snapshots are 'visible', and if so, iterate over the
1698 * snapshots and create device minor nodes for those.
1700 if (strchr(dsname
, '@') == 0) {
1702 char *n
= strdup(dsname
);
1706 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1708 job
->list
= minors_list
;
1710 list_insert_tail(minors_list
, job
);
1711 /* don't care if dispatch fails, because job->error is 0 */
1712 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1715 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
1717 * traverse snapshots only, do not traverse children,
1718 * and skip the 'dsname'
1720 error
= dmu_objset_find((char *)dsname
,
1721 zvol_create_snap_minor_cb
, (void *)job
,
1725 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1733 * Create minors for the specified dataset, including children and snapshots.
1734 * Pay attention to the 'snapdev' property and iterate over the snapshots
1735 * only if they are 'visible'. This approach allows one to assure that the
1736 * snapshot metadata is read from disk only if it is needed.
1738 * The name can represent a dataset to be recursively scanned for zvols and
1739 * their snapshots, or a single zvol snapshot. If the name represents a
1740 * dataset, the scan is performed in two nested stages:
1741 * - scan the dataset for zvols, and
1742 * - for each zvol, create a minor node, then check if the zvol's snapshots
1743 * are 'visible', and only then iterate over the snapshots if needed
1745 * If the name represents a snapshot, a check is performed if the snapshot is
1746 * 'visible' (which also verifies that the parent is a zvol), and if so,
1747 * a minor node for that snapshot is created.
1750 zvol_create_minors_impl(const char *name
)
1753 fstrans_cookie_t cookie
;
1758 if (zvol_inhibit_dev
)
1762 * This is the list for prefetch jobs. Whenever we found a match
1763 * during dmu_objset_find, we insert a minors_job to the list and do
1764 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1765 * any lock because all list operation is done on the current thread.
1767 * We will use this list to do zvol_create_minor_impl after prefetch
1768 * so we don't have to traverse using dmu_objset_find again.
1770 list_create(&minors_list
, sizeof (minors_job_t
),
1771 offsetof(minors_job_t
, link
));
1773 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1774 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1776 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1780 error
= dsl_prop_get_integer(parent
, "snapdev",
1783 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1784 error
= zvol_create_minor_impl(name
);
1786 cookie
= spl_fstrans_mark();
1787 error
= dmu_objset_find(parent
, zvol_create_minors_cb
,
1788 &minors_list
, DS_FIND_CHILDREN
);
1789 spl_fstrans_unmark(cookie
);
1792 kmem_free(parent
, MAXPATHLEN
);
1793 taskq_wait_outstanding(system_taskq
, 0);
1796 * Prefetch is completed, we can do zvol_create_minor_impl
1799 while ((job
= list_head(&minors_list
)) != NULL
) {
1800 list_remove(&minors_list
, job
);
1802 zvol_create_minor_impl(job
->name
);
1804 kmem_free(job
, sizeof (minors_job_t
));
1807 list_destroy(&minors_list
);
1809 return (SET_ERROR(error
));
1813 * Remove minors for specified dataset including children and snapshots.
1816 zvol_remove_minors_impl(const char *name
)
1818 zvol_state_t
*zv
, *zv_next
;
1819 int namelen
= ((name
) ? strlen(name
) : 0);
1820 taskqid_t t
, tid
= TASKQID_INVALID
;
1822 if (zvol_inhibit_dev
)
1825 mutex_enter(&zvol_state_lock
);
1827 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1828 zv_next
= list_next(&zvol_state_list
, zv
);
1830 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1831 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1832 (zv
->zv_name
[namelen
] == '/' ||
1833 zv
->zv_name
[namelen
] == '@'))) {
1835 /* If in use, leave alone */
1836 if (zv
->zv_open_count
> 0 ||
1837 atomic_read(&zv
->zv_suspend_ref
))
1842 /* clear this so zvol_open won't open it */
1843 zv
->zv_disk
->private_data
= NULL
;
1845 /* try parallel zv_free, if failed do it in place */
1846 t
= taskq_dispatch(system_taskq
, zvol_free_impl
, zv
,
1848 if (t
== TASKQID_INVALID
)
1854 mutex_exit(&zvol_state_lock
);
1855 if (tid
!= TASKQID_INVALID
)
1856 taskq_wait_outstanding(system_taskq
, tid
);
1859 /* Remove minor for this specific snapshot only */
1861 zvol_remove_minor_impl(const char *name
)
1863 zvol_state_t
*zv
, *zv_next
;
1865 if (zvol_inhibit_dev
)
1868 if (strchr(name
, '@') == NULL
)
1871 mutex_enter(&zvol_state_lock
);
1873 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1874 zv_next
= list_next(&zvol_state_list
, zv
);
1876 if (strcmp(zv
->zv_name
, name
) == 0) {
1877 /* If in use, leave alone */
1878 if (zv
->zv_open_count
> 0 ||
1879 atomic_read(&zv
->zv_suspend_ref
))
1887 mutex_exit(&zvol_state_lock
);
1891 * Rename minors for specified dataset including children and snapshots.
1894 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1896 zvol_state_t
*zv
, *zv_next
;
1897 int oldnamelen
, newnamelen
;
1900 if (zvol_inhibit_dev
)
1903 oldnamelen
= strlen(oldname
);
1904 newnamelen
= strlen(newname
);
1905 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1907 mutex_enter(&zvol_state_lock
);
1909 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1910 zv_next
= list_next(&zvol_state_list
, zv
);
1912 /* If in use, leave alone */
1913 if (zv
->zv_open_count
> 0)
1916 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1917 zvol_rename_minor(zv
, newname
);
1918 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1919 (zv
->zv_name
[oldnamelen
] == '/' ||
1920 zv
->zv_name
[oldnamelen
] == '@')) {
1921 snprintf(name
, MAXNAMELEN
, "%s%c%s", newname
,
1922 zv
->zv_name
[oldnamelen
],
1923 zv
->zv_name
+ oldnamelen
+ 1);
1924 zvol_rename_minor(zv
, name
);
1928 mutex_exit(&zvol_state_lock
);
1930 kmem_free(name
, MAXNAMELEN
);
1933 typedef struct zvol_snapdev_cb_arg
{
1935 } zvol_snapdev_cb_arg_t
;
1938 zvol_set_snapdev_cb(const char *dsname
, void *param
)
1940 zvol_snapdev_cb_arg_t
*arg
= param
;
1942 if (strchr(dsname
, '@') == NULL
)
1945 switch (arg
->snapdev
) {
1946 case ZFS_SNAPDEV_VISIBLE
:
1947 (void) zvol_create_minor_impl(dsname
);
1949 case ZFS_SNAPDEV_HIDDEN
:
1950 (void) zvol_remove_minor_impl(dsname
);
1958 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1960 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1961 fstrans_cookie_t cookie
= spl_fstrans_mark();
1963 * The zvol_set_snapdev_sync() sets snapdev appropriately
1964 * in the dataset hierarchy. Here, we only scan snapshots.
1966 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1967 spl_fstrans_unmark(cookie
);
1970 static zvol_task_t
*
1971 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1977 /* Never allow tasks on hidden names. */
1978 if (name1
[0] == '$')
1981 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1983 task
->snapdev
= snapdev
;
1984 delim
= strchr(name1
, '/');
1985 strlcpy(task
->pool
, name1
, delim
? (delim
- name1
+ 1) : MAXNAMELEN
);
1987 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
1989 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
1995 zvol_task_free(zvol_task_t
*task
)
1997 kmem_free(task
, sizeof (zvol_task_t
));
2001 * The worker thread function performed asynchronously.
2004 zvol_task_cb(void *param
)
2006 zvol_task_t
*task
= (zvol_task_t
*)param
;
2009 case ZVOL_ASYNC_CREATE_MINORS
:
2010 (void) zvol_create_minors_impl(task
->name1
);
2012 case ZVOL_ASYNC_REMOVE_MINORS
:
2013 zvol_remove_minors_impl(task
->name1
);
2015 case ZVOL_ASYNC_RENAME_MINORS
:
2016 zvol_rename_minors_impl(task
->name1
, task
->name2
);
2018 case ZVOL_ASYNC_SET_SNAPDEV
:
2019 zvol_set_snapdev_impl(task
->name1
, task
->snapdev
);
2026 zvol_task_free(task
);
2029 typedef struct zvol_set_snapdev_arg
{
2030 const char *zsda_name
;
2031 uint64_t zsda_value
;
2032 zprop_source_t zsda_source
;
2034 } zvol_set_snapdev_arg_t
;
2037 * Sanity check the dataset for safe use by the sync task. No additional
2038 * conditions are imposed.
2041 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
2043 zvol_set_snapdev_arg_t
*zsda
= arg
;
2044 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2048 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
2052 dsl_dir_rele(dd
, FTAG
);
2058 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2060 zvol_set_snapdev_arg_t
*zsda
= arg
;
2061 char dsname
[MAXNAMELEN
];
2064 dsl_dataset_name(ds
, dsname
);
2065 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
2066 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
2067 &zsda
->zsda_value
, zsda
->zsda_tx
);
2069 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
,
2070 NULL
, zsda
->zsda_value
);
2074 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
2080 * Traverse all child snapshot datasets and apply snapdev appropriately.
2083 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
2085 zvol_set_snapdev_arg_t
*zsda
= arg
;
2086 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2089 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
2092 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
2093 zsda
, DS_FIND_CHILDREN
);
2095 dsl_dir_rele(dd
, FTAG
);
2099 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
2101 zvol_set_snapdev_arg_t zsda
;
2103 zsda
.zsda_name
= ddname
;
2104 zsda
.zsda_source
= source
;
2105 zsda
.zsda_value
= snapdev
;
2107 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
2108 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
2112 zvol_create_minors(spa_t
*spa
, const char *name
, boolean_t async
)
2117 task
= zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS
, name
, NULL
, ~0ULL);
2121 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2122 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2123 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2127 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
2132 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
2136 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2137 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2138 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2142 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
2148 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
2152 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2153 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2154 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2162 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
2163 offsetof(zvol_state_t
, zv_next
));
2164 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2165 ida_init(&zvol_ida
);
2167 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
2173 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
2174 INIT_HLIST_HEAD(&zvol_htable
[i
]);
2176 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
2178 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
2182 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
2183 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
2188 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2190 mutex_destroy(&zvol_state_lock
);
2191 list_destroy(&zvol_state_list
);
2193 return (SET_ERROR(error
));
2199 zvol_remove_minors_impl(NULL
);
2201 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
2202 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
2203 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2205 list_destroy(&zvol_state_list
);
2206 mutex_destroy(&zvol_state_lock
);
2208 ida_destroy(&zvol_ida
);
2212 module_param(zvol_inhibit_dev
, uint
, 0644);
2213 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
2215 module_param(zvol_major
, uint
, 0444);
2216 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
2218 module_param(zvol_max_discard_blocks
, ulong
, 0444);
2219 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
2221 module_param(zvol_prefetch_bytes
, uint
, 0644);
2222 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");