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
;
64 void *zvol_tag
= "zvol_tag";
66 #define ZVOL_HT_SIZE 1024
67 static struct hlist_head
*zvol_htable
;
68 #define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
69 static DEFINE_IDA(zvol_ida
);
72 * The in-core state of each volume.
74 typedef struct zvol_state
{
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 */
94 ZVOL_ASYNC_CREATE_MINORS
,
95 ZVOL_ASYNC_REMOVE_MINORS
,
96 ZVOL_ASYNC_RENAME_MINORS
,
97 ZVOL_ASYNC_SET_SNAPDEV
,
103 char pool
[MAXNAMELEN
];
104 char name1
[MAXNAMELEN
];
105 char name2
[MAXNAMELEN
];
106 zprop_source_t source
;
110 #define ZVOL_RDONLY 0x1
113 zvol_name_hash(const char *name
)
116 uint64_t crc
= -1ULL;
117 uint8_t *p
= (uint8_t *)name
;
118 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
119 for (i
= 0; i
< MAXNAMELEN
- 1 && *p
; i
++, p
++) {
120 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (*p
)) & 0xFF];
126 * Find a zvol_state_t given the full major+minor dev_t.
128 static zvol_state_t
*
129 zvol_find_by_dev(dev_t dev
)
133 ASSERT(MUTEX_HELD(&zvol_state_lock
));
134 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
135 zv
= list_next(&zvol_state_list
, zv
)) {
136 if (zv
->zv_dev
== dev
)
144 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
146 static zvol_state_t
*
147 zvol_find_by_name_hash(const char *name
, uint64_t hash
)
150 struct hlist_node
*p
;
152 ASSERT(MUTEX_HELD(&zvol_state_lock
));
153 hlist_for_each(p
, ZVOL_HT_HEAD(hash
)) {
154 zv
= hlist_entry(p
, zvol_state_t
, zv_hlink
);
155 if (zv
->zv_hash
== hash
&&
156 strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0)
163 * Find a zvol_state_t given the name provided at zvol_alloc() time.
165 static zvol_state_t
*
166 zvol_find_by_name(const char *name
)
168 return (zvol_find_by_name_hash(name
, zvol_name_hash(name
)));
173 * Given a path, return TRUE if path is a ZVOL.
176 zvol_is_zvol(const char *device
)
178 struct block_device
*bdev
;
181 bdev
= vdev_lookup_bdev(device
);
185 major
= MAJOR(bdev
->bd_dev
);
188 if (major
== zvol_major
)
195 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
198 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
200 zfs_creat_t
*zct
= arg
;
201 nvlist_t
*nvprops
= zct
->zct_props
;
203 uint64_t volblocksize
, volsize
;
205 VERIFY(nvlist_lookup_uint64(nvprops
,
206 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
207 if (nvlist_lookup_uint64(nvprops
,
208 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
209 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
212 * These properties must be removed from the list so the generic
213 * property setting step won't apply to them.
215 VERIFY(nvlist_remove_all(nvprops
,
216 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
217 (void) nvlist_remove_all(nvprops
,
218 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
220 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
224 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
228 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
233 * ZFS_IOC_OBJSET_STATS entry point.
236 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
239 dmu_object_info_t
*doi
;
242 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
244 return (SET_ERROR(error
));
246 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
247 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
248 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
251 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
252 doi
->doi_data_block_size
);
255 kmem_free(doi
, sizeof (dmu_object_info_t
));
257 return (SET_ERROR(error
));
261 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
263 struct block_device
*bdev
;
265 bdev
= bdget_disk(zv
->zv_disk
, 0);
268 set_capacity(zv
->zv_disk
, volsize
>> 9);
269 zv
->zv_volsize
= volsize
;
270 check_disk_size_change(zv
->zv_disk
, bdev
);
276 * Sanity check volume size.
279 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
282 return (SET_ERROR(EINVAL
));
284 if (volsize
% blocksize
!= 0)
285 return (SET_ERROR(EINVAL
));
288 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
289 return (SET_ERROR(EOVERFLOW
));
295 * Ensure the zap is flushed then inform the VFS of the capacity change.
298 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
304 ASSERT(MUTEX_HELD(&zvol_state_lock
));
306 tx
= dmu_tx_create(os
);
307 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
308 dmu_tx_mark_netfree(tx
);
309 error
= dmu_tx_assign(tx
, TXG_WAIT
);
312 return (SET_ERROR(error
));
314 txg
= dmu_tx_get_txg(tx
);
316 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
320 txg_wait_synced(dmu_objset_pool(os
), txg
);
323 error
= dmu_free_long_range(os
,
324 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
330 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
332 zvol_size_changed(zv
, volsize
);
335 * We should post a event here describing the expansion. However,
336 * the zfs_ereport_post() interface doesn't nicely support posting
337 * events for zvols, it assumes events relate to vdevs or zios.
344 * Set ZFS_PROP_VOLSIZE set entry point.
347 zvol_set_volsize(const char *name
, uint64_t volsize
)
349 zvol_state_t
*zv
= NULL
;
352 dmu_object_info_t
*doi
;
354 boolean_t owned
= B_FALSE
;
356 error
= dsl_prop_get_integer(name
,
357 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
359 return (SET_ERROR(error
));
361 return (SET_ERROR(EROFS
));
363 mutex_enter(&zvol_state_lock
);
364 zv
= zvol_find_by_name(name
);
366 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
367 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
369 mutex_exit(&zvol_state_lock
);
370 return (SET_ERROR(error
));
379 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
381 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
382 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
385 error
= zvol_update_volsize(volsize
, os
);
386 kmem_free(doi
, sizeof (dmu_object_info_t
));
388 if (error
== 0 && zv
!= NULL
)
389 error
= zvol_update_live_volsize(zv
, volsize
);
392 dmu_objset_disown(os
, FTAG
);
394 zv
->zv_objset
= NULL
;
396 mutex_exit(&zvol_state_lock
);
401 * Sanity check volume block size.
404 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
406 /* Record sizes above 128k need the feature to be enabled */
407 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
411 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
414 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
415 spa_close(spa
, FTAG
);
416 return (SET_ERROR(ENOTSUP
));
420 * We don't allow setting the property above 1MB,
421 * unless the tunable has been changed.
423 if (volblocksize
> zfs_max_recordsize
)
424 return (SET_ERROR(EDOM
));
426 spa_close(spa
, FTAG
);
429 if (volblocksize
< SPA_MINBLOCKSIZE
||
430 volblocksize
> SPA_MAXBLOCKSIZE
||
432 return (SET_ERROR(EDOM
));
438 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
441 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
447 mutex_enter(&zvol_state_lock
);
449 zv
= zvol_find_by_name(name
);
451 error
= SET_ERROR(ENXIO
);
455 if (zv
->zv_flags
& ZVOL_RDONLY
) {
456 error
= SET_ERROR(EROFS
);
460 tx
= dmu_tx_create(zv
->zv_objset
);
461 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
462 error
= dmu_tx_assign(tx
, TXG_WAIT
);
466 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
467 volblocksize
, 0, tx
);
468 if (error
== ENOTSUP
)
469 error
= SET_ERROR(EBUSY
);
472 zv
->zv_volblocksize
= volblocksize
;
475 mutex_exit(&zvol_state_lock
);
477 return (SET_ERROR(error
));
481 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
482 * implement DKIOCFREE/free-long-range.
485 zvol_replay_truncate(zvol_state_t
*zv
, lr_truncate_t
*lr
, boolean_t byteswap
)
487 uint64_t offset
, length
;
490 byteswap_uint64_array(lr
, sizeof (*lr
));
492 offset
= lr
->lr_offset
;
493 length
= lr
->lr_length
;
495 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
499 * Replay a TX_WRITE ZIL transaction that didn't get committed
500 * after a system failure
503 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
505 objset_t
*os
= zv
->zv_objset
;
506 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
507 uint64_t off
= lr
->lr_offset
;
508 uint64_t len
= lr
->lr_length
;
513 byteswap_uint64_array(lr
, sizeof (*lr
));
515 tx
= dmu_tx_create(os
);
516 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
517 error
= dmu_tx_assign(tx
, TXG_WAIT
);
521 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
525 return (SET_ERROR(error
));
529 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
531 return (SET_ERROR(ENOTSUP
));
535 * Callback vectors for replaying records.
536 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
538 zil_replay_func_t zvol_replay_vector
[TX_MAX_TYPE
] = {
539 (zil_replay_func_t
)zvol_replay_err
, /* no such transaction type */
540 (zil_replay_func_t
)zvol_replay_err
, /* TX_CREATE */
541 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKDIR */
542 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKXATTR */
543 (zil_replay_func_t
)zvol_replay_err
, /* TX_SYMLINK */
544 (zil_replay_func_t
)zvol_replay_err
, /* TX_REMOVE */
545 (zil_replay_func_t
)zvol_replay_err
, /* TX_RMDIR */
546 (zil_replay_func_t
)zvol_replay_err
, /* TX_LINK */
547 (zil_replay_func_t
)zvol_replay_err
, /* TX_RENAME */
548 (zil_replay_func_t
)zvol_replay_write
, /* TX_WRITE */
549 (zil_replay_func_t
)zvol_replay_truncate
, /* TX_TRUNCATE */
550 (zil_replay_func_t
)zvol_replay_err
, /* TX_SETATTR */
551 (zil_replay_func_t
)zvol_replay_err
, /* TX_ACL */
555 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
557 * We store data in the log buffers if it's small enough.
558 * Otherwise we will later flush the data out via dmu_sync().
560 ssize_t zvol_immediate_write_sz
= 32768;
563 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
564 uint64_t size
, int sync
)
566 uint32_t blocksize
= zv
->zv_volblocksize
;
567 zilog_t
*zilog
= zv
->zv_zilog
;
569 ssize_t immediate_write_sz
;
571 if (zil_replaying(zilog
, tx
))
574 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
575 ? 0 : zvol_immediate_write_sz
;
576 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
577 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
583 itx_wr_state_t write_state
;
586 * Unlike zfs_log_write() we can be called with
587 * up to DMU_MAX_ACCESS/2 (5MB) writes.
589 if (blocksize
> immediate_write_sz
&& !slogging
&&
590 size
>= blocksize
&& offset
% blocksize
== 0) {
591 write_state
= WR_INDIRECT
; /* uses dmu_sync */
594 write_state
= WR_COPIED
;
595 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
597 write_state
= WR_NEED_COPY
;
598 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
601 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
602 (write_state
== WR_COPIED
? len
: 0));
603 lr
= (lr_write_t
*)&itx
->itx_lr
;
604 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
605 ZVOL_OBJ
, offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
606 zil_itx_destroy(itx
);
607 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
608 lr
= (lr_write_t
*)&itx
->itx_lr
;
609 write_state
= WR_NEED_COPY
;
612 itx
->itx_wr_state
= write_state
;
613 if (write_state
== WR_NEED_COPY
)
615 lr
->lr_foid
= ZVOL_OBJ
;
616 lr
->lr_offset
= offset
;
619 BP_ZERO(&lr
->lr_blkptr
);
621 itx
->itx_private
= zv
;
622 itx
->itx_sync
= sync
;
624 (void) zil_itx_assign(zilog
, itx
, tx
);
632 zvol_write(zvol_state_t
*zv
, uio_t
*uio
, boolean_t sync
)
634 uint64_t volsize
= zv
->zv_volsize
;
638 ASSERT(zv
&& zv
->zv_open_count
> 0);
640 rl
= zfs_range_lock(&zv
->zv_range_lock
, uio
->uio_loffset
,
641 uio
->uio_resid
, RL_WRITER
);
643 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
644 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
645 uint64_t off
= uio
->uio_loffset
;
646 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
648 if (bytes
> volsize
- off
) /* don't write past the end */
649 bytes
= volsize
- off
;
651 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
653 /* This will only fail for ENOSPC */
654 error
= dmu_tx_assign(tx
, TXG_WAIT
);
659 error
= dmu_write_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
, tx
);
661 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
667 zfs_range_unlock(rl
);
669 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
674 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
677 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
682 zilog_t
*zilog
= zv
->zv_zilog
;
684 if (zil_replaying(zilog
, tx
))
687 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
688 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
689 lr
->lr_foid
= ZVOL_OBJ
;
693 itx
->itx_sync
= sync
;
694 zil_itx_assign(zilog
, itx
, tx
);
698 zvol_discard(struct bio
*bio
)
700 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
701 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
702 uint64_t size
= BIO_BI_SIZE(bio
);
703 uint64_t end
= start
+ size
;
708 ASSERT(zv
&& zv
->zv_open_count
> 0);
710 if (end
> zv
->zv_volsize
)
711 return (SET_ERROR(EIO
));
714 * Align the request to volume block boundaries when a secure erase is
715 * not required. This will prevent dnode_free_range() from zeroing out
716 * the unaligned parts which is slow (read-modify-write) and useless
717 * since we are not freeing any space by doing so.
719 if (!bio_is_secure_erase(bio
)) {
720 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
721 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
728 rl
= zfs_range_lock(&zv
->zv_range_lock
, start
, size
, RL_WRITER
);
729 tx
= dmu_tx_create(zv
->zv_objset
);
730 dmu_tx_mark_netfree(tx
);
731 error
= dmu_tx_assign(tx
, TXG_WAIT
);
735 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
737 error
= dmu_free_long_range(zv
->zv_objset
,
738 ZVOL_OBJ
, start
, size
);
741 zfs_range_unlock(rl
);
747 zvol_read(zvol_state_t
*zv
, uio_t
*uio
)
749 uint64_t volsize
= zv
->zv_volsize
;
753 ASSERT(zv
&& zv
->zv_open_count
> 0);
755 rl
= zfs_range_lock(&zv
->zv_range_lock
, uio
->uio_loffset
,
756 uio
->uio_resid
, RL_READER
);
757 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
758 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
760 /* don't read past the end */
761 if (bytes
> volsize
- uio
->uio_loffset
)
762 bytes
= volsize
- uio
->uio_loffset
;
764 error
= dmu_read_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
);
766 /* convert checksum errors into IO errors */
768 error
= SET_ERROR(EIO
);
772 zfs_range_unlock(rl
);
776 static MAKE_REQUEST_FN_RET
777 zvol_request(struct request_queue
*q
, struct bio
*bio
)
780 zvol_state_t
*zv
= q
->queuedata
;
781 fstrans_cookie_t cookie
= spl_fstrans_mark();
782 int rw
= bio_data_dir(bio
);
783 #ifdef HAVE_GENERIC_IO_ACCT
784 unsigned long start
= jiffies
;
788 uio
.uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
789 uio
.uio_skip
= BIO_BI_SKIP(bio
);
790 uio
.uio_resid
= BIO_BI_SIZE(bio
);
791 uio
.uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
792 uio
.uio_loffset
= BIO_BI_SECTOR(bio
) << 9;
793 uio
.uio_limit
= MAXOFFSET_T
;
794 uio
.uio_segflg
= UIO_BVEC
;
796 if (bio_has_data(bio
) && uio
.uio_loffset
+ uio
.uio_resid
>
799 "%s: bad access: offset=%llu, size=%lu\n",
800 zv
->zv_disk
->disk_name
,
801 (long long unsigned)uio
.uio_loffset
,
802 (long unsigned)uio
.uio_resid
);
803 error
= SET_ERROR(EIO
);
807 generic_start_io_acct(rw
, bio_sectors(bio
), &zv
->zv_disk
->part0
);
810 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
811 error
= SET_ERROR(EROFS
);
815 if (bio_is_discard(bio
) || bio_is_secure_erase(bio
)) {
816 error
= zvol_discard(bio
);
821 * Some requests are just for flush and nothing else.
823 if (uio
.uio_resid
== 0) {
824 if (bio_is_flush(bio
))
825 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
829 error
= zvol_write(zv
, &uio
,
830 bio_is_flush(bio
) || bio_is_fua(bio
) ||
831 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
);
833 error
= zvol_read(zv
, &uio
);
836 generic_end_io_acct(rw
, &zv
->zv_disk
->part0
, start
);
838 BIO_END_IO(bio
, -error
);
839 spl_fstrans_unmark(cookie
);
840 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
842 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
843 return (BLK_QC_T_NONE
);
848 zvol_get_done(zgd_t
*zgd
, int error
)
851 dmu_buf_rele(zgd
->zgd_db
, zgd
);
853 zfs_range_unlock(zgd
->zgd_rl
);
855 if (error
== 0 && zgd
->zgd_bp
)
856 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
858 kmem_free(zgd
, sizeof (zgd_t
));
862 * Get data to generate a TX_WRITE intent log record.
865 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
867 zvol_state_t
*zv
= arg
;
868 objset_t
*os
= zv
->zv_objset
;
869 uint64_t object
= ZVOL_OBJ
;
870 uint64_t offset
= lr
->lr_offset
;
871 uint64_t size
= lr
->lr_length
;
872 blkptr_t
*bp
= &lr
->lr_blkptr
;
880 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
881 zgd
->zgd_zilog
= zv
->zv_zilog
;
882 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_range_lock
, offset
, size
,
886 * Write records come in two flavors: immediate and indirect.
887 * For small writes it's cheaper to store the data with the
888 * log record (immediate); for large writes it's cheaper to
889 * sync the data and get a pointer to it (indirect) so that
890 * we don't have to write the data twice.
892 if (buf
!= NULL
) { /* immediate write */
893 error
= dmu_read(os
, object
, offset
, size
, buf
,
894 DMU_READ_NO_PREFETCH
);
896 size
= zv
->zv_volblocksize
;
897 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
898 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
899 DMU_READ_NO_PREFETCH
);
901 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
903 ASSERT(BP_IS_HOLE(bp
));
908 zgd
->zgd_bp
= &lr
->lr_blkptr
;
911 ASSERT(db
->db_offset
== offset
);
912 ASSERT(db
->db_size
== size
);
914 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
922 zvol_get_done(zgd
, error
);
924 return (SET_ERROR(error
));
928 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
931 zvol_insert(zvol_state_t
*zv
)
933 ASSERT(MUTEX_HELD(&zvol_state_lock
));
934 ASSERT3U(MINOR(zv
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
935 list_insert_head(&zvol_state_list
, zv
);
936 hlist_add_head(&zv
->zv_hlink
, ZVOL_HT_HEAD(zv
->zv_hash
));
940 * Simply remove the zvol from to list of zvols.
943 zvol_remove(zvol_state_t
*zv
)
945 ASSERT(MUTEX_HELD(&zvol_state_lock
));
946 list_remove(&zvol_state_list
, zv
);
947 hlist_del(&zv
->zv_hlink
);
951 zvol_first_open(zvol_state_t
*zv
)
958 /* lie and say we're read-only */
959 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, 1, zvol_tag
, &os
);
961 return (SET_ERROR(-error
));
965 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
969 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
973 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
977 set_capacity(zv
->zv_disk
, volsize
>> 9);
978 zv
->zv_volsize
= volsize
;
979 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
981 if (ro
|| dmu_objset_is_snapshot(os
) ||
982 !spa_writeable(dmu_objset_spa(os
))) {
983 set_disk_ro(zv
->zv_disk
, 1);
984 zv
->zv_flags
|= ZVOL_RDONLY
;
986 set_disk_ro(zv
->zv_disk
, 0);
987 zv
->zv_flags
&= ~ZVOL_RDONLY
;
992 dmu_objset_disown(os
, zvol_tag
);
993 zv
->zv_objset
= NULL
;
996 return (SET_ERROR(-error
));
1000 zvol_last_close(zvol_state_t
*zv
)
1002 zil_close(zv
->zv_zilog
);
1003 zv
->zv_zilog
= NULL
;
1005 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
1011 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
1012 !(zv
->zv_flags
& ZVOL_RDONLY
))
1013 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1014 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
1016 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
1017 zv
->zv_objset
= NULL
;
1021 zvol_open(struct block_device
*bdev
, fmode_t flag
)
1024 int error
= 0, drop_mutex
= 0;
1027 * If the caller is already holding the mutex do not take it
1028 * again, this will happen as part of zvol_create_minor_impl().
1029 * Once add_disk() is called the device is live and the kernel
1030 * will attempt to open it to read the partition information.
1032 if (!mutex_owned(&zvol_state_lock
)) {
1033 mutex_enter(&zvol_state_lock
);
1038 * Obtain a copy of private_data under the lock to make sure
1039 * that either the result of zvol_freeg() setting
1040 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1041 * is not called on this zv because of the positive zv_open_count.
1043 zv
= bdev
->bd_disk
->private_data
;
1049 if (zv
->zv_open_count
== 0) {
1050 error
= zvol_first_open(zv
);
1055 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1057 goto out_open_count
;
1060 zv
->zv_open_count
++;
1062 check_disk_change(bdev
);
1065 if (zv
->zv_open_count
== 0)
1066 zvol_last_close(zv
);
1070 mutex_exit(&zvol_state_lock
);
1072 return (SET_ERROR(error
));
1075 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1080 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1082 zvol_state_t
*zv
= disk
->private_data
;
1085 ASSERT(zv
&& zv
->zv_open_count
> 0);
1087 if (!mutex_owned(&zvol_state_lock
)) {
1088 mutex_enter(&zvol_state_lock
);
1092 zv
->zv_open_count
--;
1093 if (zv
->zv_open_count
== 0)
1094 zvol_last_close(zv
);
1097 mutex_exit(&zvol_state_lock
);
1099 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1105 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1106 unsigned int cmd
, unsigned long arg
)
1108 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1111 ASSERT(zv
&& zv
->zv_open_count
> 0);
1115 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1118 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1127 return (SET_ERROR(error
));
1130 #ifdef CONFIG_COMPAT
1132 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1133 unsigned cmd
, unsigned long arg
)
1135 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1138 #define zvol_compat_ioctl NULL
1141 static int zvol_media_changed(struct gendisk
*disk
)
1143 zvol_state_t
*zv
= disk
->private_data
;
1145 ASSERT(zv
&& zv
->zv_open_count
> 0);
1147 return (zv
->zv_changed
);
1150 static int zvol_revalidate_disk(struct gendisk
*disk
)
1152 zvol_state_t
*zv
= disk
->private_data
;
1154 ASSERT(zv
&& zv
->zv_open_count
> 0);
1157 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1163 * Provide a simple virtual geometry for legacy compatibility. For devices
1164 * smaller than 1 MiB a small head and sector count is used to allow very
1165 * tiny devices. For devices over 1 Mib a standard head and sector count
1166 * is used to keep the cylinders count reasonable.
1169 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1171 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1174 ASSERT(zv
&& zv
->zv_open_count
> 0);
1176 sectors
= get_capacity(zv
->zv_disk
);
1178 if (sectors
> 2048) {
1187 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1192 static struct kobject
*
1193 zvol_probe(dev_t dev
, int *part
, void *arg
)
1196 struct kobject
*kobj
;
1198 mutex_enter(&zvol_state_lock
);
1199 zv
= zvol_find_by_dev(dev
);
1200 kobj
= zv
? get_disk(zv
->zv_disk
) : NULL
;
1201 mutex_exit(&zvol_state_lock
);
1206 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1207 static struct block_device_operations zvol_ops
= {
1209 .release
= zvol_release
,
1210 .ioctl
= zvol_ioctl
,
1211 .compat_ioctl
= zvol_compat_ioctl
,
1212 .media_changed
= zvol_media_changed
,
1213 .revalidate_disk
= zvol_revalidate_disk
,
1214 .getgeo
= zvol_getgeo
,
1215 .owner
= THIS_MODULE
,
1218 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1221 zvol_open_by_inode(struct inode
*inode
, struct file
*file
)
1223 return (zvol_open(inode
->i_bdev
, file
->f_mode
));
1227 zvol_release_by_inode(struct inode
*inode
, struct file
*file
)
1229 return (zvol_release(inode
->i_bdev
->bd_disk
, file
->f_mode
));
1233 zvol_ioctl_by_inode(struct inode
*inode
, struct file
*file
,
1234 unsigned int cmd
, unsigned long arg
)
1236 if (file
== NULL
|| inode
== NULL
)
1237 return (SET_ERROR(-EINVAL
));
1239 return (zvol_ioctl(inode
->i_bdev
, file
->f_mode
, cmd
, arg
));
1242 #ifdef CONFIG_COMPAT
1244 zvol_compat_ioctl_by_inode(struct file
*file
,
1245 unsigned int cmd
, unsigned long arg
)
1248 return (SET_ERROR(-EINVAL
));
1250 return (zvol_compat_ioctl(file
->f_dentry
->d_inode
->i_bdev
,
1251 file
->f_mode
, cmd
, arg
));
1254 #define zvol_compat_ioctl_by_inode NULL
1257 static struct block_device_operations zvol_ops
= {
1258 .open
= zvol_open_by_inode
,
1259 .release
= zvol_release_by_inode
,
1260 .ioctl
= zvol_ioctl_by_inode
,
1261 .compat_ioctl
= zvol_compat_ioctl_by_inode
,
1262 .media_changed
= zvol_media_changed
,
1263 .revalidate_disk
= zvol_revalidate_disk
,
1264 .getgeo
= zvol_getgeo
,
1265 .owner
= THIS_MODULE
,
1267 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1270 * Allocate memory for a new zvol_state_t and setup the required
1271 * request queue and generic disk structures for the block device.
1273 static zvol_state_t
*
1274 zvol_alloc(dev_t dev
, const char *name
)
1278 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1280 list_link_init(&zv
->zv_next
);
1282 zv
->zv_queue
= blk_alloc_queue(GFP_ATOMIC
);
1283 if (zv
->zv_queue
== NULL
)
1286 blk_queue_make_request(zv
->zv_queue
, zvol_request
);
1287 blk_queue_set_write_cache(zv
->zv_queue
, B_TRUE
, B_TRUE
);
1289 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1290 if (zv
->zv_disk
== NULL
)
1293 zv
->zv_queue
->queuedata
= zv
;
1295 zv
->zv_open_count
= 0;
1296 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1298 zfs_rlock_init(&zv
->zv_range_lock
);
1300 zv
->zv_disk
->major
= zvol_major
;
1301 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1302 zv
->zv_disk
->fops
= &zvol_ops
;
1303 zv
->zv_disk
->private_data
= zv
;
1304 zv
->zv_disk
->queue
= zv
->zv_queue
;
1305 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1306 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1311 blk_cleanup_queue(zv
->zv_queue
);
1313 kmem_free(zv
, sizeof (zvol_state_t
));
1319 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1322 zvol_free(zvol_state_t
*zv
)
1324 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1325 ASSERT(zv
->zv_open_count
== 0);
1327 zfs_rlock_destroy(&zv
->zv_range_lock
);
1329 zv
->zv_disk
->private_data
= NULL
;
1331 del_gendisk(zv
->zv_disk
);
1332 blk_cleanup_queue(zv
->zv_queue
);
1333 put_disk(zv
->zv_disk
);
1335 ida_simple_remove(&zvol_ida
, MINOR(zv
->zv_dev
) >> ZVOL_MINOR_BITS
);
1336 kmem_free(zv
, sizeof (zvol_state_t
));
1340 * Create a block device minor node and setup the linkage between it
1341 * and the specified volume. Once this function returns the block
1342 * device is live and ready for use.
1345 zvol_create_minor_impl(const char *name
)
1349 dmu_object_info_t
*doi
;
1355 uint64_t hash
= zvol_name_hash(name
);
1357 idx
= ida_simple_get(&zvol_ida
, 0, 0, kmem_flags_convert(KM_SLEEP
));
1359 return (SET_ERROR(-idx
));
1360 minor
= idx
<< ZVOL_MINOR_BITS
;
1362 mutex_enter(&zvol_state_lock
);
1364 zv
= zvol_find_by_name_hash(name
, hash
);
1366 error
= SET_ERROR(EEXIST
);
1370 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1372 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, zvol_tag
, &os
);
1376 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1378 goto out_dmu_objset_disown
;
1380 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1382 goto out_dmu_objset_disown
;
1384 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1386 error
= SET_ERROR(EAGAIN
);
1387 goto out_dmu_objset_disown
;
1391 if (dmu_objset_is_snapshot(os
))
1392 zv
->zv_flags
|= ZVOL_RDONLY
;
1394 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1395 zv
->zv_volsize
= volsize
;
1398 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1400 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1401 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1402 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1403 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1404 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1405 blk_queue_max_discard_sectors(zv
->zv_queue
,
1406 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1407 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1408 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1409 #ifdef QUEUE_FLAG_NONROT
1410 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1412 #ifdef QUEUE_FLAG_ADD_RANDOM
1413 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_queue
);
1416 if (spa_writeable(dmu_objset_spa(os
))) {
1417 if (zil_replay_disable
)
1418 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1420 zil_replay(os
, zv
, zvol_replay_vector
);
1424 * When udev detects the addition of the device it will immediately
1425 * invoke blkid(8) to determine the type of content on the device.
1426 * Prefetching the blocks commonly scanned by blkid(8) will speed
1429 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1431 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1432 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1433 ZIO_PRIORITY_SYNC_READ
);
1436 zv
->zv_objset
= NULL
;
1437 out_dmu_objset_disown
:
1438 dmu_objset_disown(os
, zvol_tag
);
1440 kmem_free(doi
, sizeof (dmu_object_info_t
));
1446 * Drop the lock to prevent deadlock with sys_open() ->
1447 * zvol_open(), which first takes bd_disk->bd_mutex and then
1448 * takes zvol_state_lock, whereas this code path first takes
1449 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1451 mutex_exit(&zvol_state_lock
);
1452 add_disk(zv
->zv_disk
);
1454 mutex_exit(&zvol_state_lock
);
1455 ida_simple_remove(&zvol_ida
, idx
);
1458 return (SET_ERROR(error
));
1462 * Rename a block device minor mode for the specified volume.
1465 zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1467 int readonly
= get_disk_ro(zv
->zv_disk
);
1469 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1471 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1474 * The block device's read-only state is briefly changed causing
1475 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1476 * the name change and fixes the symlinks. This does not change
1477 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1478 * changes. This would normally be done using kobject_uevent() but
1479 * that is a GPL-only symbol which is why we need this workaround.
1481 set_disk_ro(zv
->zv_disk
, !readonly
);
1482 set_disk_ro(zv
->zv_disk
, readonly
);
1485 typedef struct minors_job
{
1495 * Prefetch zvol dnodes for the minors_job
1498 zvol_prefetch_minors_impl(void *arg
)
1500 minors_job_t
*job
= arg
;
1501 char *dsname
= job
->name
;
1502 objset_t
*os
= NULL
;
1504 job
->error
= dmu_objset_own(dsname
, DMU_OST_ZVOL
, B_TRUE
, zvol_tag
,
1506 if (job
->error
== 0) {
1507 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, 0, ZIO_PRIORITY_SYNC_READ
);
1508 dmu_objset_disown(os
, zvol_tag
);
1513 * Mask errors to continue dmu_objset_find() traversal
1516 zvol_create_snap_minor_cb(const char *dsname
, void *arg
)
1518 minors_job_t
*j
= arg
;
1519 list_t
*minors_list
= j
->list
;
1520 const char *name
= j
->name
;
1522 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1524 /* skip the designated dataset */
1525 if (name
&& strcmp(dsname
, name
) == 0)
1528 /* at this point, the dsname should name a snapshot */
1529 if (strchr(dsname
, '@') == 0) {
1530 dprintf("zvol_create_snap_minor_cb(): "
1531 "%s is not a shapshot name\n", dsname
);
1534 char *n
= strdup(dsname
);
1538 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1540 job
->list
= minors_list
;
1542 list_insert_tail(minors_list
, job
);
1543 /* don't care if dispatch fails, because job->error is 0 */
1544 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1552 * Mask errors to continue dmu_objset_find() traversal
1555 zvol_create_minors_cb(const char *dsname
, void *arg
)
1559 list_t
*minors_list
= arg
;
1561 ASSERT0(MUTEX_HELD(&spa_namespace_lock
));
1563 error
= dsl_prop_get_integer(dsname
, "snapdev", &snapdev
, NULL
);
1568 * Given the name and the 'snapdev' property, create device minor nodes
1569 * with the linkages to zvols/snapshots as needed.
1570 * If the name represents a zvol, create a minor node for the zvol, then
1571 * check if its snapshots are 'visible', and if so, iterate over the
1572 * snapshots and create device minor nodes for those.
1574 if (strchr(dsname
, '@') == 0) {
1576 char *n
= strdup(dsname
);
1580 job
= kmem_alloc(sizeof (minors_job_t
), KM_SLEEP
);
1582 job
->list
= minors_list
;
1584 list_insert_tail(minors_list
, job
);
1585 /* don't care if dispatch fails, because job->error is 0 */
1586 taskq_dispatch(system_taskq
, zvol_prefetch_minors_impl
, job
,
1589 if (snapdev
== ZFS_SNAPDEV_VISIBLE
) {
1591 * traverse snapshots only, do not traverse children,
1592 * and skip the 'dsname'
1594 error
= dmu_objset_find((char *)dsname
,
1595 zvol_create_snap_minor_cb
, (void *)job
,
1599 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1607 * Create minors for the specified dataset, including children and snapshots.
1608 * Pay attention to the 'snapdev' property and iterate over the snapshots
1609 * only if they are 'visible'. This approach allows one to assure that the
1610 * snapshot metadata is read from disk only if it is needed.
1612 * The name can represent a dataset to be recursively scanned for zvols and
1613 * their snapshots, or a single zvol snapshot. If the name represents a
1614 * dataset, the scan is performed in two nested stages:
1615 * - scan the dataset for zvols, and
1616 * - for each zvol, create a minor node, then check if the zvol's snapshots
1617 * are 'visible', and only then iterate over the snapshots if needed
1619 * If the name represents a snapshot, a check is perfromed if the snapshot is
1620 * 'visible' (which also verifies that the parent is a zvol), and if so,
1621 * a minor node for that snapshot is created.
1624 zvol_create_minors_impl(const char *name
)
1627 fstrans_cookie_t cookie
;
1632 if (zvol_inhibit_dev
)
1636 * This is the list for prefetch jobs. Whenever we found a match
1637 * during dmu_objset_find, we insert a minors_job to the list and do
1638 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1639 * any lock because all list operation is done on the current thread.
1641 * We will use this list to do zvol_create_minor_impl after prefetch
1642 * so we don't have to traverse using dmu_objset_find again.
1644 list_create(&minors_list
, sizeof (minors_job_t
),
1645 offsetof(minors_job_t
, link
));
1647 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1648 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1650 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1654 error
= dsl_prop_get_integer(parent
, "snapdev",
1657 if (error
== 0 && snapdev
== ZFS_SNAPDEV_VISIBLE
)
1658 error
= zvol_create_minor_impl(name
);
1660 cookie
= spl_fstrans_mark();
1661 error
= dmu_objset_find(parent
, zvol_create_minors_cb
,
1662 &minors_list
, DS_FIND_CHILDREN
);
1663 spl_fstrans_unmark(cookie
);
1666 kmem_free(parent
, MAXPATHLEN
);
1667 taskq_wait_outstanding(system_taskq
, 0);
1670 * Prefetch is completed, we can do zvol_create_minor_impl
1673 while ((job
= list_head(&minors_list
)) != NULL
) {
1674 list_remove(&minors_list
, job
);
1676 zvol_create_minor_impl(job
->name
);
1678 kmem_free(job
, sizeof (minors_job_t
));
1681 list_destroy(&minors_list
);
1683 return (SET_ERROR(error
));
1687 * Remove minors for specified dataset including children and snapshots.
1690 zvol_remove_minors_impl(const char *name
)
1692 zvol_state_t
*zv
, *zv_next
;
1693 int namelen
= ((name
) ? strlen(name
) : 0);
1695 if (zvol_inhibit_dev
)
1698 mutex_enter(&zvol_state_lock
);
1700 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1701 zv_next
= list_next(&zvol_state_list
, zv
);
1703 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1704 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1705 (zv
->zv_name
[namelen
] == '/' ||
1706 zv
->zv_name
[namelen
] == '@'))) {
1708 /* If in use, leave alone */
1709 if (zv
->zv_open_count
> 0)
1717 mutex_exit(&zvol_state_lock
);
1720 /* Remove minor for this specific snapshot only */
1722 zvol_remove_minor_impl(const char *name
)
1724 zvol_state_t
*zv
, *zv_next
;
1726 if (zvol_inhibit_dev
)
1729 if (strchr(name
, '@') == NULL
)
1732 mutex_enter(&zvol_state_lock
);
1734 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1735 zv_next
= list_next(&zvol_state_list
, zv
);
1737 if (strcmp(zv
->zv_name
, name
) == 0) {
1738 /* If in use, leave alone */
1739 if (zv
->zv_open_count
> 0)
1747 mutex_exit(&zvol_state_lock
);
1751 * Rename minors for specified dataset including children and snapshots.
1754 zvol_rename_minors_impl(const char *oldname
, const char *newname
)
1756 zvol_state_t
*zv
, *zv_next
;
1757 int oldnamelen
, newnamelen
;
1760 if (zvol_inhibit_dev
)
1763 oldnamelen
= strlen(oldname
);
1764 newnamelen
= strlen(newname
);
1765 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1767 mutex_enter(&zvol_state_lock
);
1769 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1770 zv_next
= list_next(&zvol_state_list
, zv
);
1772 /* If in use, leave alone */
1773 if (zv
->zv_open_count
> 0)
1776 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1777 zvol_rename_minor(zv
, newname
);
1778 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1779 (zv
->zv_name
[oldnamelen
] == '/' ||
1780 zv
->zv_name
[oldnamelen
] == '@')) {
1781 snprintf(name
, MAXNAMELEN
, "%s%c%s", newname
,
1782 zv
->zv_name
[oldnamelen
],
1783 zv
->zv_name
+ oldnamelen
+ 1);
1784 zvol_rename_minor(zv
, name
);
1788 mutex_exit(&zvol_state_lock
);
1790 kmem_free(name
, MAXNAMELEN
);
1793 typedef struct zvol_snapdev_cb_arg
{
1795 } zvol_snapdev_cb_arg_t
;
1798 zvol_set_snapdev_cb(const char *dsname
, void *param
) {
1799 zvol_snapdev_cb_arg_t
*arg
= param
;
1801 if (strchr(dsname
, '@') == NULL
)
1804 switch (arg
->snapdev
) {
1805 case ZFS_SNAPDEV_VISIBLE
:
1806 (void) zvol_create_minor_impl(dsname
);
1808 case ZFS_SNAPDEV_HIDDEN
:
1809 (void) zvol_remove_minor_impl(dsname
);
1817 zvol_set_snapdev_impl(char *name
, uint64_t snapdev
)
1819 zvol_snapdev_cb_arg_t arg
= {snapdev
};
1820 fstrans_cookie_t cookie
= spl_fstrans_mark();
1822 * The zvol_set_snapdev_sync() sets snapdev appropriately
1823 * in the dataset hierarchy. Here, we only scan snapshots.
1825 dmu_objset_find(name
, zvol_set_snapdev_cb
, &arg
, DS_FIND_SNAPSHOTS
);
1826 spl_fstrans_unmark(cookie
);
1829 static zvol_task_t
*
1830 zvol_task_alloc(zvol_async_op_t op
, const char *name1
, const char *name2
,
1836 /* Never allow tasks on hidden names. */
1837 if (name1
[0] == '$')
1840 task
= kmem_zalloc(sizeof (zvol_task_t
), KM_SLEEP
);
1842 task
->snapdev
= snapdev
;
1843 delim
= strchr(name1
, '/');
1844 strlcpy(task
->pool
, name1
, delim
? (delim
- name1
+ 1) : MAXNAMELEN
);
1846 strlcpy(task
->name1
, name1
, MAXNAMELEN
);
1848 strlcpy(task
->name2
, name2
, MAXNAMELEN
);
1854 zvol_task_free(zvol_task_t
*task
)
1856 kmem_free(task
, sizeof (zvol_task_t
));
1860 * The worker thread function performed asynchronously.
1863 zvol_task_cb(void *param
)
1865 zvol_task_t
*task
= (zvol_task_t
*)param
;
1868 case ZVOL_ASYNC_CREATE_MINORS
:
1869 (void) zvol_create_minors_impl(task
->name1
);
1871 case ZVOL_ASYNC_REMOVE_MINORS
:
1872 zvol_remove_minors_impl(task
->name1
);
1874 case ZVOL_ASYNC_RENAME_MINORS
:
1875 zvol_rename_minors_impl(task
->name1
, task
->name2
);
1877 case ZVOL_ASYNC_SET_SNAPDEV
:
1878 zvol_set_snapdev_impl(task
->name1
, task
->snapdev
);
1885 zvol_task_free(task
);
1888 typedef struct zvol_set_snapdev_arg
{
1889 const char *zsda_name
;
1890 uint64_t zsda_value
;
1891 zprop_source_t zsda_source
;
1893 } zvol_set_snapdev_arg_t
;
1896 * Sanity check the dataset for safe use by the sync task. No additional
1897 * conditions are imposed.
1900 zvol_set_snapdev_check(void *arg
, dmu_tx_t
*tx
)
1902 zvol_set_snapdev_arg_t
*zsda
= arg
;
1903 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1907 error
= dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
);
1911 dsl_dir_rele(dd
, FTAG
);
1917 zvol_set_snapdev_sync_cb(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1919 zvol_set_snapdev_arg_t
*zsda
= arg
;
1920 char dsname
[MAXNAMELEN
];
1923 dsl_dataset_name(ds
, dsname
);
1924 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDEV
),
1925 zsda
->zsda_source
, sizeof (zsda
->zsda_value
), 1,
1926 &zsda
->zsda_value
, zsda
->zsda_tx
);
1928 task
= zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV
, dsname
,
1929 NULL
, zsda
->zsda_value
);
1933 (void) taskq_dispatch(dp
->dp_spa
->spa_zvol_taskq
, zvol_task_cb
,
1939 * Traverse all child snapshot datasets and apply snapdev appropriately.
1942 zvol_set_snapdev_sync(void *arg
, dmu_tx_t
*tx
)
1944 zvol_set_snapdev_arg_t
*zsda
= arg
;
1945 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1948 VERIFY0(dsl_dir_hold(dp
, zsda
->zsda_name
, FTAG
, &dd
, NULL
));
1951 dmu_objset_find_dp(dp
, dd
->dd_object
, zvol_set_snapdev_sync_cb
,
1952 zsda
, DS_FIND_CHILDREN
);
1954 dsl_dir_rele(dd
, FTAG
);
1958 zvol_set_snapdev(const char *ddname
, zprop_source_t source
, uint64_t snapdev
)
1960 zvol_set_snapdev_arg_t zsda
;
1962 zsda
.zsda_name
= ddname
;
1963 zsda
.zsda_source
= source
;
1964 zsda
.zsda_value
= snapdev
;
1966 return (dsl_sync_task(ddname
, zvol_set_snapdev_check
,
1967 zvol_set_snapdev_sync
, &zsda
, 0, ZFS_SPACE_CHECK_NONE
));
1971 zvol_create_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1976 task
= zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS
, name
, NULL
, ~0ULL);
1980 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1981 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1982 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
1986 zvol_remove_minors(spa_t
*spa
, const char *name
, boolean_t async
)
1991 task
= zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS
, name
, NULL
, ~0ULL);
1995 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
1996 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
1997 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2001 zvol_rename_minors(spa_t
*spa
, const char *name1
, const char *name2
,
2007 task
= zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS
, name1
, name2
, ~0ULL);
2011 id
= taskq_dispatch(spa
->spa_zvol_taskq
, zvol_task_cb
, task
, TQ_SLEEP
);
2012 if ((async
== B_FALSE
) && (id
!= TASKQID_INVALID
))
2013 taskq_wait_id(spa
->spa_zvol_taskq
, id
);
2021 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
2022 offsetof(zvol_state_t
, zv_next
));
2023 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2025 zvol_htable
= kmem_alloc(ZVOL_HT_SIZE
* sizeof (struct hlist_head
),
2031 for (i
= 0; i
< ZVOL_HT_SIZE
; i
++)
2032 INIT_HLIST_HEAD(&zvol_htable
[i
]);
2034 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
2036 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
2040 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
2041 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
2046 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2048 mutex_destroy(&zvol_state_lock
);
2049 list_destroy(&zvol_state_list
);
2051 return (SET_ERROR(error
));
2057 zvol_remove_minors_impl(NULL
);
2059 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
2060 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
2061 kmem_free(zvol_htable
, ZVOL_HT_SIZE
* sizeof (struct hlist_head
));
2063 list_destroy(&zvol_state_list
);
2064 mutex_destroy(&zvol_state_lock
);
2067 module_param(zvol_inhibit_dev
, uint
, 0644);
2068 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
2070 module_param(zvol_major
, uint
, 0444);
2071 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
2073 module_param(zvol_max_discard_blocks
, ulong
, 0444);
2074 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
2076 module_param(zvol_prefetch_bytes
, uint
, 0644);
2077 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");