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 (c) 2016 Actifio, Inc. All rights reserved.
41 #include <sys/dmu_traverse.h>
42 #include <sys/dsl_dataset.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_dir.h>
46 #include <sys/zfeature.h>
47 #include <sys/zil_impl.h>
48 #include <sys/dmu_tx.h>
50 #include <sys/zfs_rlock.h>
51 #include <sys/zfs_znode.h>
52 #include <sys/spa_impl.h>
54 #include <linux/blkdev_compat.h>
55 #include <linux/version.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
;
64 static char *zvol_tag
= "zvol_tag";
67 * The in-core state of each volume.
69 typedef struct zvol_state
{
70 char zv_name
[MAXNAMELEN
]; /* name */
71 uint64_t zv_volsize
; /* advertised space */
72 uint64_t zv_volblocksize
; /* volume block size */
73 objset_t
*zv_objset
; /* objset handle */
74 uint32_t zv_flags
; /* ZVOL_* flags */
75 uint32_t zv_open_count
; /* open counts */
76 uint32_t zv_changed
; /* disk changed */
77 zilog_t
*zv_zilog
; /* ZIL handle */
78 zfs_rlock_t zv_range_lock
; /* range lock */
79 dmu_buf_t
*zv_dbuf
; /* bonus handle */
80 dev_t zv_dev
; /* device id */
81 struct gendisk
*zv_disk
; /* generic disk */
82 struct request_queue
*zv_queue
; /* request queue */
83 spinlock_t zv_lock
; /* request queue lock */
84 list_node_t zv_next
; /* next zvol_state_t linkage */
88 ZVOL_ASYNC_CREATE_MINORS
,
89 ZVOL_ASYNC_REMOVE_MINORS
,
90 ZVOL_ASYNC_RENAME_MINORS
,
91 ZVOL_ASYNC_SET_SNAPDEV
,
97 char pool
[MAXNAMELEN
];
98 char name1
[MAXNAMELEN
];
99 char name2
[MAXNAMELEN
];
100 zprop_source_t source
;
104 #define ZVOL_RDONLY 0x1
107 * Find the next available range of ZVOL_MINORS minor numbers. The
108 * zvol_state_list is kept in ascending minor order so we simply need
109 * to scan the list for the first gap in the sequence. This allows us
110 * to recycle minor number as devices are created and removed.
113 zvol_find_minor(unsigned *minor
)
118 ASSERT(MUTEX_HELD(&zvol_state_lock
));
119 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
120 zv
= list_next(&zvol_state_list
, zv
), *minor
+= ZVOL_MINORS
) {
121 if (MINOR(zv
->zv_dev
) != MINOR(*minor
))
125 /* All minors are in use */
126 if (*minor
>= (1 << MINORBITS
))
127 return (SET_ERROR(ENXIO
));
133 * Find a zvol_state_t given the full major+minor dev_t.
135 static zvol_state_t
*
136 zvol_find_by_dev(dev_t dev
)
140 ASSERT(MUTEX_HELD(&zvol_state_lock
));
141 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
142 zv
= list_next(&zvol_state_list
, zv
)) {
143 if (zv
->zv_dev
== dev
)
151 * Find a zvol_state_t given the name provided at zvol_alloc() time.
153 static zvol_state_t
*
154 zvol_find_by_name(const char *name
)
158 ASSERT(MUTEX_HELD(&zvol_state_lock
));
159 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
160 zv
= list_next(&zvol_state_list
, zv
)) {
161 if (strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0)
170 * Given a path, return TRUE if path is a ZVOL.
173 zvol_is_zvol(const char *device
)
175 struct block_device
*bdev
;
178 bdev
= vdev_lookup_bdev(device
);
182 major
= MAJOR(bdev
->bd_dev
);
185 if (major
== zvol_major
)
192 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
195 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
197 zfs_creat_t
*zct
= arg
;
198 nvlist_t
*nvprops
= zct
->zct_props
;
200 uint64_t volblocksize
, volsize
;
202 VERIFY(nvlist_lookup_uint64(nvprops
,
203 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
204 if (nvlist_lookup_uint64(nvprops
,
205 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
206 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
209 * These properties must be removed from the list so the generic
210 * property setting step won't apply to them.
212 VERIFY(nvlist_remove_all(nvprops
,
213 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
214 (void) nvlist_remove_all(nvprops
,
215 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
217 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
221 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
225 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
230 * ZFS_IOC_OBJSET_STATS entry point.
233 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
236 dmu_object_info_t
*doi
;
239 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
241 return (SET_ERROR(error
));
243 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
244 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
245 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
248 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
249 doi
->doi_data_block_size
);
252 kmem_free(doi
, sizeof (dmu_object_info_t
));
254 return (SET_ERROR(error
));
258 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
260 struct block_device
*bdev
;
262 bdev
= bdget_disk(zv
->zv_disk
, 0);
267 * Added check_disk_size_change() helper function.
269 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
270 set_capacity(zv
->zv_disk
, volsize
>> 9);
271 zv
->zv_volsize
= volsize
;
272 check_disk_size_change(zv
->zv_disk
, bdev
);
274 zv
->zv_volsize
= volsize
;
276 (void) check_disk_change(bdev
);
277 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
283 * Sanity check volume size.
286 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
289 return (SET_ERROR(EINVAL
));
291 if (volsize
% blocksize
!= 0)
292 return (SET_ERROR(EINVAL
));
295 if (volsize
- 1 > MAXOFFSET_T
)
296 return (SET_ERROR(EOVERFLOW
));
302 * Ensure the zap is flushed then inform the VFS of the capacity change.
305 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
310 ASSERT(MUTEX_HELD(&zvol_state_lock
));
312 tx
= dmu_tx_create(os
);
313 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
314 error
= dmu_tx_assign(tx
, TXG_WAIT
);
317 return (SET_ERROR(error
));
320 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
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
));
381 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
383 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
384 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
387 error
= zvol_update_volsize(volsize
, os
);
388 kmem_free(doi
, sizeof (dmu_object_info_t
));
390 if (error
== 0 && zv
!= NULL
)
391 error
= zvol_update_live_volsize(zv
, volsize
);
394 dmu_objset_disown(os
, FTAG
);
396 zv
->zv_objset
= NULL
;
398 mutex_exit(&zvol_state_lock
);
403 * Sanity check volume block size.
406 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
408 /* Record sizes above 128k need the feature to be enabled */
409 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
413 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
416 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
417 spa_close(spa
, FTAG
);
418 return (SET_ERROR(ENOTSUP
));
422 * We don't allow setting the property above 1MB,
423 * unless the tunable has been changed.
425 if (volblocksize
> zfs_max_recordsize
)
426 return (SET_ERROR(EDOM
));
428 spa_close(spa
, FTAG
);
431 if (volblocksize
< SPA_MINBLOCKSIZE
||
432 volblocksize
> SPA_MAXBLOCKSIZE
||
434 return (SET_ERROR(EDOM
));
440 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
443 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
449 mutex_enter(&zvol_state_lock
);
451 zv
= zvol_find_by_name(name
);
453 error
= SET_ERROR(ENXIO
);
457 if (zv
->zv_flags
& ZVOL_RDONLY
) {
458 error
= SET_ERROR(EROFS
);
462 tx
= dmu_tx_create(zv
->zv_objset
);
463 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
464 error
= dmu_tx_assign(tx
, TXG_WAIT
);
468 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
469 volblocksize
, 0, tx
);
470 if (error
== ENOTSUP
)
471 error
= SET_ERROR(EBUSY
);
474 zv
->zv_volblocksize
= volblocksize
;
477 mutex_exit(&zvol_state_lock
);
479 return (SET_ERROR(error
));
483 * Replay a TX_WRITE ZIL transaction that didn't get committed
484 * after a system failure
487 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
489 objset_t
*os
= zv
->zv_objset
;
490 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
491 uint64_t off
= lr
->lr_offset
;
492 uint64_t len
= lr
->lr_length
;
497 byteswap_uint64_array(lr
, sizeof (*lr
));
499 tx
= dmu_tx_create(os
);
500 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
501 error
= dmu_tx_assign(tx
, TXG_WAIT
);
505 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
509 return (SET_ERROR(error
));
513 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
515 return (SET_ERROR(ENOTSUP
));
519 * Callback vectors for replaying records.
520 * Only TX_WRITE is needed for zvol.
522 zil_replay_func_t zvol_replay_vector
[TX_MAX_TYPE
] = {
523 (zil_replay_func_t
)zvol_replay_err
, /* no such transaction type */
524 (zil_replay_func_t
)zvol_replay_err
, /* TX_CREATE */
525 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKDIR */
526 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKXATTR */
527 (zil_replay_func_t
)zvol_replay_err
, /* TX_SYMLINK */
528 (zil_replay_func_t
)zvol_replay_err
, /* TX_REMOVE */
529 (zil_replay_func_t
)zvol_replay_err
, /* TX_RMDIR */
530 (zil_replay_func_t
)zvol_replay_err
, /* TX_LINK */
531 (zil_replay_func_t
)zvol_replay_err
, /* TX_RENAME */
532 (zil_replay_func_t
)zvol_replay_write
, /* TX_WRITE */
533 (zil_replay_func_t
)zvol_replay_err
, /* TX_TRUNCATE */
534 (zil_replay_func_t
)zvol_replay_err
, /* TX_SETATTR */
535 (zil_replay_func_t
)zvol_replay_err
, /* TX_ACL */
539 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
541 * We store data in the log buffers if it's small enough.
542 * Otherwise we will later flush the data out via dmu_sync().
544 ssize_t zvol_immediate_write_sz
= 32768;
547 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
548 uint64_t size
, int sync
)
550 uint32_t blocksize
= zv
->zv_volblocksize
;
551 zilog_t
*zilog
= zv
->zv_zilog
;
553 ssize_t immediate_write_sz
;
555 if (zil_replaying(zilog
, tx
))
558 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
559 ? 0 : zvol_immediate_write_sz
;
560 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
561 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
567 itx_wr_state_t write_state
;
570 * Unlike zfs_log_write() we can be called with
571 * up to DMU_MAX_ACCESS/2 (5MB) writes.
573 if (blocksize
> immediate_write_sz
&& !slogging
&&
574 size
>= blocksize
&& offset
% blocksize
== 0) {
575 write_state
= WR_INDIRECT
; /* uses dmu_sync */
578 write_state
= WR_COPIED
;
579 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
581 write_state
= WR_NEED_COPY
;
582 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
585 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
586 (write_state
== WR_COPIED
? len
: 0));
587 lr
= (lr_write_t
*)&itx
->itx_lr
;
588 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
589 ZVOL_OBJ
, offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
590 zil_itx_destroy(itx
);
591 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
592 lr
= (lr_write_t
*)&itx
->itx_lr
;
593 write_state
= WR_NEED_COPY
;
596 itx
->itx_wr_state
= write_state
;
597 if (write_state
== WR_NEED_COPY
)
599 lr
->lr_foid
= ZVOL_OBJ
;
600 lr
->lr_offset
= offset
;
603 BP_ZERO(&lr
->lr_blkptr
);
605 itx
->itx_private
= zv
;
606 itx
->itx_sync
= sync
;
608 (void) zil_itx_assign(zilog
, itx
, tx
);
616 zvol_write(struct bio
*bio
)
618 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
619 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
621 zvol_state_t
*zv
= bio
->bi_disk
->private_data
;
623 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
624 uint64_t size
= BIO_BI_SIZE(bio
);
629 ASSERT(zv
&& zv
->zv_open_count
> 0);
631 if (bio_is_flush(bio
))
632 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
635 * Some requests are just for flush and nothing else.
640 rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
, RL_WRITER
);
642 tx
= dmu_tx_create(zv
->zv_objset
);
643 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, size
);
645 /* This will only fail for ENOSPC */
646 error
= dmu_tx_assign(tx
, TXG_WAIT
);
649 zfs_range_unlock(rl
);
653 error
= dmu_write_bio(zv
->zv_objset
, ZVOL_OBJ
, bio
, tx
);
655 zvol_log_write(zv
, tx
, offset
, size
,
656 !!(bio_is_fua(bio
)));
659 zfs_range_unlock(rl
);
661 if ((bio_is_fua(bio
)) ||
662 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
)
663 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
670 zvol_discard(struct bio
*bio
)
672 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
673 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
675 zvol_state_t
*zv
= bio
->bi_disk
->private_data
;
677 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
678 uint64_t size
= BIO_BI_SIZE(bio
);
679 uint64_t end
= start
+ size
;
683 ASSERT(zv
&& zv
->zv_open_count
> 0);
685 if (end
> zv
->zv_volsize
)
686 return (SET_ERROR(EIO
));
689 * Align the request to volume block boundaries when a secure erase is
690 * not required. This will prevent dnode_free_range() from zeroing out
691 * the unaligned parts which is slow (read-modify-write) and useless
692 * since we are not freeing any space by doing so.
694 if (!bio_is_secure_erase(bio
)) {
695 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
696 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
703 rl
= zfs_range_lock(&zv
->zv_range_lock
, start
, size
, RL_WRITER
);
705 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, start
, size
);
708 * TODO: maybe we should add the operation to the log.
710 zfs_range_unlock(rl
);
716 zvol_read(struct bio
*bio
)
718 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
719 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
721 zvol_state_t
*zv
= bio
->bi_disk
->private_data
;
723 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
724 uint64_t len
= BIO_BI_SIZE(bio
);
728 ASSERT(zv
&& zv
->zv_open_count
> 0);
733 rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, len
, RL_READER
);
735 error
= dmu_read_bio(zv
->zv_objset
, ZVOL_OBJ
, bio
);
737 zfs_range_unlock(rl
);
739 /* convert checksum errors into IO errors */
741 error
= SET_ERROR(EIO
);
746 static MAKE_REQUEST_FN_RET
747 zvol_request(struct request_queue
*q
, struct bio
*bio
)
749 zvol_state_t
*zv
= q
->queuedata
;
750 fstrans_cookie_t cookie
= spl_fstrans_mark();
751 uint64_t offset
= BIO_BI_SECTOR(bio
);
752 unsigned int sectors
= bio_sectors(bio
);
753 int rw
= bio_data_dir(bio
);
754 #ifdef HAVE_GENERIC_IO_ACCT
755 unsigned long start
= jiffies
;
759 if (bio_has_data(bio
) && offset
+ sectors
>
760 get_capacity(zv
->zv_disk
)) {
762 "%s: bad access: block=%llu, count=%lu\n",
763 zv
->zv_disk
->disk_name
,
764 (long long unsigned)offset
,
765 (long unsigned)sectors
);
766 error
= SET_ERROR(EIO
);
770 generic_start_io_acct(rw
, sectors
, &zv
->zv_disk
->part0
);
773 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
774 error
= SET_ERROR(EROFS
);
778 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
779 error
= zvol_discard(bio
);
783 error
= zvol_write(bio
);
785 error
= zvol_read(bio
);
788 generic_end_io_acct(rw
, &zv
->zv_disk
->part0
, start
);
790 BIO_END_IO(bio
, -error
);
791 spl_fstrans_unmark(cookie
);
792 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
794 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
795 return (BLK_QC_T_NONE
);
800 zvol_get_done(zgd_t
*zgd
, int error
)
803 dmu_buf_rele(zgd
->zgd_db
, zgd
);
805 zfs_range_unlock(zgd
->zgd_rl
);
807 if (error
== 0 && zgd
->zgd_bp
)
808 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
810 kmem_free(zgd
, sizeof (zgd_t
));
814 * Get data to generate a TX_WRITE intent log record.
817 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
819 zvol_state_t
*zv
= arg
;
820 objset_t
*os
= zv
->zv_objset
;
821 uint64_t object
= ZVOL_OBJ
;
822 uint64_t offset
= lr
->lr_offset
;
823 uint64_t size
= lr
->lr_length
;
824 blkptr_t
*bp
= &lr
->lr_blkptr
;
832 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
833 zgd
->zgd_zilog
= zv
->zv_zilog
;
834 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
838 * Write records come in two flavors: immediate and indirect.
839 * For small writes it's cheaper to store the data with the
840 * log record (immediate); for large writes it's cheaper to
841 * sync the data and get a pointer to it (indirect) so that
842 * we don't have to write the data twice.
844 if (buf
!= NULL
) { /* immediate write */
845 error
= dmu_read(os
, object
, offset
, size
, buf
,
846 DMU_READ_NO_PREFETCH
);
848 size
= zv
->zv_volblocksize
;
849 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
850 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
851 DMU_READ_NO_PREFETCH
);
853 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
855 ASSERT(BP_IS_HOLE(bp
));
860 zgd
->zgd_bp
= &lr
->lr_blkptr
;
863 ASSERT(db
->db_offset
== offset
);
864 ASSERT(db
->db_size
== size
);
866 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
874 zvol_get_done(zgd
, error
);
876 return (SET_ERROR(error
));
880 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
883 zvol_insert(zvol_state_t
*zv_insert
)
885 zvol_state_t
*zv
= NULL
;
887 ASSERT(MUTEX_HELD(&zvol_state_lock
));
888 ASSERT3U(MINOR(zv_insert
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
889 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
890 zv
= list_next(&zvol_state_list
, zv
)) {
891 if (MINOR(zv
->zv_dev
) > MINOR(zv_insert
->zv_dev
))
895 list_insert_before(&zvol_state_list
, zv
, zv_insert
);
899 * Simply remove the zvol from to list of zvols.
902 zvol_remove(zvol_state_t
*zv_remove
)
904 ASSERT(MUTEX_HELD(&zvol_state_lock
));
905 list_remove(&zvol_state_list
, zv_remove
);
909 zvol_first_open(zvol_state_t
*zv
)
916 /* lie and say we're read-only */
917 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, 1, zvol_tag
, &os
);
919 return (SET_ERROR(-error
));
923 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
927 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
931 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
935 set_capacity(zv
->zv_disk
, volsize
>> 9);
936 zv
->zv_volsize
= volsize
;
937 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
939 if (ro
|| dmu_objset_is_snapshot(os
) ||
940 !spa_writeable(dmu_objset_spa(os
))) {
941 set_disk_ro(zv
->zv_disk
, 1);
942 zv
->zv_flags
|= ZVOL_RDONLY
;
944 set_disk_ro(zv
->zv_disk
, 0);
945 zv
->zv_flags
&= ~ZVOL_RDONLY
;
950 dmu_objset_disown(os
, zvol_tag
);
951 zv
->zv_objset
= NULL
;
954 return (SET_ERROR(-error
));
958 zvol_last_close(zvol_state_t
*zv
)
960 zil_close(zv
->zv_zilog
);
963 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
969 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
970 !(zv
->zv_flags
& ZVOL_RDONLY
))
971 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
972 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
974 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
975 zv
->zv_objset
= NULL
;
979 zvol_open(struct block_device
*bdev
, fmode_t flag
)
982 int error
= 0, drop_mutex
= 0;
985 * If the caller is already holding the mutex do not take it
986 * again, this will happen as part of zvol_create_minor_impl().
987 * Once add_disk() is called the device is live and the kernel
988 * will attempt to open it to read the partition information.
990 if (!mutex_owned(&zvol_state_lock
)) {
991 mutex_enter(&zvol_state_lock
);
996 * Obtain a copy of private_data under the lock to make sure
997 * that either the result of zvol_freeg() setting
998 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
999 * is not called on this zv because of the positive zv_open_count.
1001 zv
= bdev
->bd_disk
->private_data
;
1007 if (zv
->zv_open_count
== 0) {
1008 error
= zvol_first_open(zv
);
1013 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1015 goto out_open_count
;
1018 zv
->zv_open_count
++;
1020 check_disk_change(bdev
);
1023 if (zv
->zv_open_count
== 0)
1024 zvol_last_close(zv
);
1028 mutex_exit(&zvol_state_lock
);
1030 return (SET_ERROR(error
));
1033 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1038 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1040 zvol_state_t
*zv
= disk
->private_data
;
1043 ASSERT(zv
&& zv
->zv_open_count
> 0);
1045 if (!mutex_owned(&zvol_state_lock
)) {
1046 mutex_enter(&zvol_state_lock
);
1050 zv
->zv_open_count
--;
1051 if (zv
->zv_open_count
== 0)
1052 zvol_last_close(zv
);
1055 mutex_exit(&zvol_state_lock
);
1057 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1063 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1064 unsigned int cmd
, unsigned long arg
)
1066 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1069 ASSERT(zv
&& zv
->zv_open_count
> 0);
1073 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1076 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1085 return (SET_ERROR(error
));
1088 #ifdef CONFIG_COMPAT
1090 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1091 unsigned cmd
, unsigned long arg
)
1093 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1096 #define zvol_compat_ioctl NULL
1099 static int zvol_media_changed(struct gendisk
*disk
)
1101 zvol_state_t
*zv
= disk
->private_data
;
1103 ASSERT(zv
&& zv
->zv_open_count
> 0);
1105 return (zv
->zv_changed
);
1108 static int zvol_revalidate_disk(struct gendisk
*disk
)
1110 zvol_state_t
*zv
= disk
->private_data
;
1112 ASSERT(zv
&& zv
->zv_open_count
> 0);
1115 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1121 * Provide a simple virtual geometry for legacy compatibility. For devices
1122 * smaller than 1 MiB a small head and sector count is used to allow very
1123 * tiny devices. For devices over 1 Mib a standard head and sector count
1124 * is used to keep the cylinders count reasonable.
1127 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1129 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1132 ASSERT(zv
&& zv
->zv_open_count
> 0);
1134 sectors
= get_capacity(zv
->zv_disk
);
1136 if (sectors
> 2048) {
1145 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1150 static struct kobject
*
1151 zvol_probe(dev_t dev
, int *part
, void *arg
)
1154 struct kobject
*kobj
;
1156 mutex_enter(&zvol_state_lock
);
1157 zv
= zvol_find_by_dev(dev
);
1158 kobj
= zv
? get_disk(zv
->zv_disk
) : NULL
;
1159 mutex_exit(&zvol_state_lock
);
1164 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1165 static struct block_device_operations zvol_ops
= {
1167 .release
= zvol_release
,
1168 .ioctl
= zvol_ioctl
,
1169 .compat_ioctl
= zvol_compat_ioctl
,
1170 .media_changed
= zvol_media_changed
,
1171 .revalidate_disk
= zvol_revalidate_disk
,
1172 .getgeo
= zvol_getgeo
,
1173 .owner
= THIS_MODULE
,
1176 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1179 zvol_open_by_inode(struct inode
*inode
, struct file
*file
)
1181 return (zvol_open(inode
->i_bdev
, file
->f_mode
));
1185 zvol_release_by_inode(struct inode
*inode
, struct file
*file
)
1187 return (zvol_release(inode
->i_bdev
->bd_disk
, file
->f_mode
));
1191 zvol_ioctl_by_inode(struct inode
*inode
, struct file
*file
,
1192 unsigned int cmd
, unsigned long arg
)
1194 if (file
== NULL
|| inode
== NULL
)
1195 return (SET_ERROR(-EINVAL
));
1197 return (zvol_ioctl(inode
->i_bdev
, file
->f_mode
, cmd
, arg
));
1200 #ifdef CONFIG_COMPAT
1202 zvol_compat_ioctl_by_inode(struct file
*file
,
1203 unsigned int cmd
, unsigned long arg
)
1206 return (SET_ERROR(-EINVAL
));
1208 return (zvol_compat_ioctl(file
->f_dentry
->d_inode
->i_bdev
,
1209 file
->f_mode
, cmd
, arg
));
1212 #define zvol_compat_ioctl_by_inode NULL
1215 static struct block_device_operations zvol_ops
= {
1216 .open
= zvol_open_by_inode
,
1217 .release
= zvol_release_by_inode
,
1218 .ioctl
= zvol_ioctl_by_inode
,
1219 .compat_ioctl
= zvol_compat_ioctl_by_inode
,
1220 .media_changed
= zvol_media_changed
,
1221 .revalidate_disk
= zvol_revalidate_disk
,
1222 .getgeo
= zvol_getgeo
,
1223 .owner
= THIS_MODULE
,
1225 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1228 * Allocate memory for a new zvol_state_t and setup the required
1229 * request queue and generic disk structures for the block device.
1231 static zvol_state_t
*
1232 zvol_alloc(dev_t dev
, const char *name
)
1236 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1238 spin_lock_init(&zv
->zv_lock
);
1239 list_link_init(&zv
->zv_next
);
1241 zv
->zv_queue
= blk_alloc_queue(GFP_ATOMIC
);
1242 if (zv
->zv_queue
== NULL
)
1245 blk_queue_make_request(zv
->zv_queue
, zvol_request
);
1246 blk_queue_set_write_cache(zv
->zv_queue
, B_TRUE
, B_TRUE
);
1248 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1249 if (zv
->zv_disk
== NULL
)
1252 zv
->zv_queue
->queuedata
= zv
;
1254 zv
->zv_open_count
= 0;
1255 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1257 zfs_rlock_init(&zv
->zv_range_lock
);
1259 zv
->zv_disk
->major
= zvol_major
;
1260 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1261 zv
->zv_disk
->fops
= &zvol_ops
;
1262 zv
->zv_disk
->private_data
= zv
;
1263 zv
->zv_disk
->queue
= zv
->zv_queue
;
1264 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1265 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1270 blk_cleanup_queue(zv
->zv_queue
);
1272 kmem_free(zv
, sizeof (zvol_state_t
));
1278 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1281 zvol_free(zvol_state_t
*zv
)
1283 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1284 ASSERT(zv
->zv_open_count
== 0);
1286 zfs_rlock_destroy(&zv
->zv_range_lock
);
1288 zv
->zv_disk
->private_data
= NULL
;
1290 del_gendisk(zv
->zv_disk
);
1291 blk_cleanup_queue(zv
->zv_queue
);
1292 put_disk(zv
->zv_disk
);
1294 kmem_free(zv
, sizeof (zvol_state_t
));
1298 * Create a block device minor node and setup the linkage between it
1299 * and the specified volume. Once this function returns the block
1300 * device is live and ready for use.
1303 zvol_create_minor_impl(const char *name
)
1307 dmu_object_info_t
*doi
;
1313 mutex_enter(&zvol_state_lock
);
1315 zv
= zvol_find_by_name(name
);
1317 error
= SET_ERROR(EEXIST
);
1321 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1323 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, zvol_tag
, &os
);
1327 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1329 goto out_dmu_objset_disown
;
1331 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1333 goto out_dmu_objset_disown
;
1335 error
= zvol_find_minor(&minor
);
1337 goto out_dmu_objset_disown
;
1339 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1341 error
= SET_ERROR(EAGAIN
);
1342 goto out_dmu_objset_disown
;
1345 if (dmu_objset_is_snapshot(os
))
1346 zv
->zv_flags
|= ZVOL_RDONLY
;
1348 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1349 zv
->zv_volsize
= volsize
;
1352 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1354 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1355 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1356 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1357 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1358 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1359 blk_queue_max_discard_sectors(zv
->zv_queue
,
1360 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1361 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1362 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1363 #ifdef QUEUE_FLAG_NONROT
1364 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1366 #ifdef QUEUE_FLAG_ADD_RANDOM
1367 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_queue
);
1370 if (spa_writeable(dmu_objset_spa(os
))) {
1371 if (zil_replay_disable
)
1372 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1374 zil_replay(os
, zv
, zvol_replay_vector
);
1378 * When udev detects the addition of the device it will immediately
1379 * invoke blkid(8) to determine the type of content on the device.
1380 * Prefetching the blocks commonly scanned by blkid(8) will speed
1383 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1385 dmu_prefetch(os
, ZVOL_OBJ
, 0, len
);
1386 dmu_prefetch(os
, ZVOL_OBJ
, volsize
- len
, len
);
1389 zv
->zv_objset
= NULL
;
1390 out_dmu_objset_disown
:
1391 dmu_objset_disown(os
, zvol_tag
);
1393 kmem_free(doi
, sizeof (dmu_object_info_t
));
1399 * Drop the lock to prevent deadlock with sys_open() ->
1400 * zvol_open(), which first takes bd_disk->bd_mutex and then
1401 * takes zvol_state_lock, whereas this code path first takes
1402 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1404 mutex_exit(&zvol_state_lock
);
1405 add_disk(zv
->zv_disk
);
1407 mutex_exit(&zvol_state_lock
);
1410 return (SET_ERROR(error
));
1414 * Rename a block device minor mode for the specified volume.
1417 zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1419 int readonly
= get_disk_ro(zv
->zv_disk
);
1421 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1423 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1426 * The block device's read-only state is briefly changed causing
1427 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1428 * the name change and fixes the symlinks. This does not change
1429 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1430 * changes. This would normally be done using kobject_uevent() but
1431 * that is a GPL-only symbol which is why we need this workaround.
1433 set_disk_ro(zv
->zv_disk
, !readonly
);
1434 set_disk_ro(zv
->zv_disk
, readonly
);
1439 * Mask errors to continue dmu_objset_find() traversal
1442 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
1444 const char *name
= (const char *)arg
;
1446 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1448 /* skip the designated dataset */
1449 if (name
&& strcmp(dsname
, name
) == 0)
1452 /* at this point, the dsname should name a snapshot */
1453 if (strchr(dsname
, '@') == 0) {
1454 dprintf("zvol_create_snap_minor_cb(): "
1455 "%s is not a shapshot name\n", dsname
);
1457 (void) zvol_create_minor_impl(dsname
);
1464 * Mask errors to continue dmu_objset_find() traversal
1467 zvol_create_minors_cb(const char *dsname
, void *arg
)
1472 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1474 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1479 * Given the name and the 'snapdev' property, create device minor nodes
1480 * with the linkages to zvols/snapshots as needed.
1481 * If the name represents a zvol, create a minor node for the zvol, then
1482 * check if its snapshots are 'visible', and if so, iterate over the
1483 * snapshots and create device minor nodes for those.
1485 if (strchr(dsname
, '@') == 0) {
1486 /* create minor for the 'dsname' explicitly */
1487 error
= zvol_create_minor_impl(dsname
);
1488 if ((error
== 0 || error
== EEXIST
) &&
1489 (snapdev
== ZFS_SNAPDEV_VISIBLE
)) {
1490 fstrans_cookie_t cookie
= spl_fstrans_mark();
1492 * traverse snapshots only, do not traverse children,
1493 * and skip the 'dsname'
1495 error
= dmu_objset_find((char *)dsname
,
1496 zvol_create_snap_minor_cb
, (void *)dsname
,
1498 spl_fstrans_unmark(cookie
);
1501 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1509 * Create minors for the specified dataset, including children and snapshots.
1510 * Pay attention to the 'snapdev' property and iterate over the snapshots
1511 * only if they are 'visible'. This approach allows one to assure that the
1512 * snapshot metadata is read from disk only if it is needed.
1514 * The name can represent a dataset to be recursively scanned for zvols and
1515 * their snapshots, or a single zvol snapshot. If the name represents a
1516 * dataset, the scan is performed in two nested stages:
1517 * - scan the dataset for zvols, and
1518 * - for each zvol, create a minor node, then check if the zvol's snapshots
1519 * are 'visible', and only then iterate over the snapshots if needed
1521 * If the name represents a snapshot, a check is perfromed if the snapshot is
1522 * 'visible' (which also verifies that the parent is a zvol), and if so,
1523 * a minor node for that snapshot is created.
1526 zvol_create_minors_impl(const char *name
)
1529 fstrans_cookie_t cookie
;
1532 if (zvol_inhibit_dev
)
1535 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1536 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1538 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1542 error
= dsl_prop_get_integer(parent
, "snapdev",
1545 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1546 error
= zvol_create_minor_impl(name
);
1548 cookie
= spl_fstrans_mark();
1549 error
= dmu_objset_find(parent
, zvol_create_minors_cb
,
1550 NULL
, DS_FIND_CHILDREN
);
1551 spl_fstrans_unmark(cookie
);
1554 kmem_free(parent
, MAXPATHLEN
);
1556 return (SET_ERROR(error
));
1560 * Remove minors for specified dataset including children and snapshots.
1563 zvol_remove_minors_impl(const char *name
)
1565 zvol_state_t
*zv
, *zv_next
;
1566 int namelen
= ((name
) ? strlen(name
) : 0);
1568 if (zvol_inhibit_dev
)
1571 mutex_enter(&zvol_state_lock
);
1573 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1574 zv_next
= list_next(&zvol_state_list
, zv
);
1576 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1577 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1578 (zv
->zv_name
[namelen
] == '/' ||
1579 zv
->zv_name
[namelen
] == '@'))) {
1581 /* If in use, leave alone */
1582 if (zv
->zv_open_count
> 0)
1590 mutex_exit(&zvol_state_lock
);
1593 /* Remove minor for this specific snapshot only */
1595 zvol_remove_minor_impl(const char *name
)
1597 zvol_state_t
*zv
, *zv_next
;
1599 if (zvol_inhibit_dev
)
1602 if (strchr(name
, '@') == NULL
)
1605 mutex_enter(&zvol_state_lock
);
1607 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1608 zv_next
= list_next(&zvol_state_list
, zv
);
1610 if (strcmp(zv
->zv_name
, name
) == 0) {
1611 /* If in use, leave alone */
1612 if (zv
->zv_open_count
> 0)
1620 mutex_exit(&zvol_state_lock
);
1624 * Rename minors for specified dataset including children and snapshots.
1627 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1629 zvol_state_t
*zv
, *zv_next
;
1630 int oldnamelen
, newnamelen
;
1632 if (zvol_inhibit_dev
)
1635 oldnamelen
= strlen(oldname
);
1636 newnamelen
= strlen(newname
);
1638 mutex_enter(&zvol_state_lock
);
1640 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1641 zv_next
= list_next(&zvol_state_list
, zv
);
1643 /* If in use, leave alone */
1644 if (zv
->zv_open_count
> 0)
1647 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1648 zvol_rename_minor(zv
, newname
);
1649 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1650 (zv
->zv_name
[oldnamelen
] == '/' ||
1651 zv
->zv_name
[oldnamelen
] == '@')) {
1652 char *name
= kmem_asprintf("%s%c%s", newname
,
1653 zv
->zv_name
[oldnamelen
],
1654 zv
->zv_name
+ oldnamelen
+ 1);
1655 zvol_rename_minor(zv
, name
);
1656 kmem_free(name
, strlen(name
+ 1));
1660 mutex_exit(&zvol_state_lock
);
1663 typedef struct zvol_snapdev_cb_arg
{
1665 } zvol_snapdev_cb_arg_t
;
1668 zvol_set_snapdev_cb(const char *dsname
, void *param
) {
1669 zvol_snapdev_cb_arg_t
*arg
= param
;
1671 if (strchr(dsname
, '@') == NULL
)
1674 switch (arg
->snapdev
) {
1675 case ZFS_SNAPDEV_VISIBLE
:
1676 (void) zvol_create_minor_impl(dsname
);
1678 case ZFS_SNAPDEV_HIDDEN
:
1679 (void) zvol_remove_minor_impl(dsname
);
1687 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1689 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1690 fstrans_cookie_t cookie
= spl_fstrans_mark();
1692 * The zvol_set_snapdev_sync() sets snapdev appropriately
1693 * in the dataset hierarchy. Here, we only scan snapshots.
1695 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1696 spl_fstrans_unmark(cookie
);
1699 static zvol_task_t
*
1700 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1706 /* Never allow tasks on hidden names. */
1707 if (name1
[0] == '$')
1710 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1712 task
->snapdev
= snapdev
;
1713 delim
= strchr(name1
, '/');
1714 strlcpy(task
->pool
, name1
, delim
? (delim
- name1
+ 1) : MAXNAMELEN
);
1716 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
1718 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
1724 zvol_task_free(zvol_task_t
*task
)
1726 kmem_free(task
, sizeof (zvol_task_t
));
1730 * The worker thread function performed asynchronously.
1733 zvol_task_cb(void *param
)
1735 zvol_task_t
*task
= (zvol_task_t
*)param
;
1738 case ZVOL_ASYNC_CREATE_MINORS
:
1739 (void) zvol_create_minors_impl(task
->name1
);
1741 case ZVOL_ASYNC_REMOVE_MINORS
:
1742 zvol_remove_minors_impl(task
->name1
);
1744 case ZVOL_ASYNC_RENAME_MINORS
:
1745 zvol_rename_minors_impl(task
->name1
, task
->name2
);
1747 case ZVOL_ASYNC_SET_SNAPDEV
:
1748 zvol_set_snapdev_impl(task
->name1
, task
->snapdev
);
1755 zvol_task_free(task
);
1758 typedef struct zvol_set_snapdev_arg
{
1759 const char *zsda_name
;
1760 uint64_t zsda_value
;
1761 zprop_source_t zsda_source
;
1763 } zvol_set_snapdev_arg_t
;
1766 * Sanity check the dataset for safe use by the sync task. No additional
1767 * conditions are imposed.
1770 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
1772 zvol_set_snapdev_arg_t
*zsda
= arg
;
1773 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1777 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1781 dsl_dir_rele(dd
, FTAG
);
1787 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1789 zvol_set_snapdev_arg_t
*zsda
= arg
;
1790 char dsname
[MAXNAMELEN
];
1793 dsl_dataset_name(ds
, dsname
);
1794 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
1795 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1796 &zsda
->zsda_value
, zsda
->zsda_tx
);
1798 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
,
1799 NULL
, zsda
->zsda_value
);
1803 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1809 * Traverse all child snapshot datasets and apply snapdev appropriately.
1812 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
1814 zvol_set_snapdev_arg_t
*zsda
= arg
;
1815 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1818 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1821 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
1822 zsda
, DS_FIND_CHILDREN
);
1824 dsl_dir_rele(dd
, FTAG
);
1828 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
1830 zvol_set_snapdev_arg_t zsda
;
1832 zsda
.zsda_name
= ddname
;
1833 zsda
.zsda_source
= source
;
1834 zsda
.zsda_value
= snapdev
;
1836 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
1837 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1841 zvol_create_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1846 task
= zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS
, name
, NULL
, ~0ULL);
1850 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1851 if ((async
== B_FALSE
) && (id
!= 0))
1852 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1856 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1861 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
1865 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1866 if ((async
== B_FALSE
) && (id
!= 0))
1867 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1871 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
1877 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
1881 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1882 if ((async
== B_FALSE
) && (id
!= 0))
1883 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1891 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1892 offsetof(zvol_state_t
, zv_next
));
1893 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1895 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1897 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1901 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
1902 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
1907 mutex_destroy(&zvol_state_lock
);
1908 list_destroy(&zvol_state_list
);
1910 return (SET_ERROR(error
));
1916 zvol_remove_minors_impl(NULL
);
1918 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
1919 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1921 list_destroy(&zvol_state_list
);
1922 mutex_destroy(&zvol_state_lock
);
1925 module_param(zvol_inhibit_dev
, uint
, 0644);
1926 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1928 module_param(zvol_major
, uint
, 0444);
1929 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1931 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1932 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1934 module_param(zvol_prefetch_bytes
, uint
, 0644);
1935 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");