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.
41 #include <sys/dmu_traverse.h>
42 #include <sys/dsl_dataset.h>
43 #include <sys/dsl_prop.h>
45 #include <sys/zfeature.h>
46 #include <sys/zil_impl.h>
47 #include <sys/dmu_tx.h>
49 #include <sys/zfs_rlock.h>
50 #include <sys/zfs_znode.h>
52 #include <linux/blkdev_compat.h>
54 unsigned int zvol_inhibit_dev
= 0;
55 unsigned int zvol_major
= ZVOL_MAJOR
;
56 unsigned int zvol_prefetch_bytes
= (128 * 1024);
57 unsigned long zvol_max_discard_blocks
= 16384;
59 static kmutex_t zvol_state_lock
;
60 static list_t zvol_state_list
;
61 static char *zvol_tag
= "zvol_tag";
64 * The in-core state of each volume.
66 typedef struct zvol_state
{
67 char zv_name
[MAXNAMELEN
]; /* name */
68 uint64_t zv_volsize
; /* advertised space */
69 uint64_t zv_volblocksize
; /* volume block size */
70 objset_t
*zv_objset
; /* objset handle */
71 uint32_t zv_flags
; /* ZVOL_* flags */
72 uint32_t zv_open_count
; /* open counts */
73 uint32_t zv_changed
; /* disk changed */
74 zilog_t
*zv_zilog
; /* ZIL handle */
75 znode_t zv_znode
; /* for range locking */
76 dmu_buf_t
*zv_dbuf
; /* bonus handle */
77 dev_t zv_dev
; /* device id */
78 struct gendisk
*zv_disk
; /* generic disk */
79 struct request_queue
*zv_queue
; /* request queue */
80 list_node_t zv_next
; /* next zvol_state_t linkage */
83 #define ZVOL_RDONLY 0x1
86 * Find the next available range of ZVOL_MINORS minor numbers. The
87 * zvol_state_list is kept in ascending minor order so we simply need
88 * to scan the list for the first gap in the sequence. This allows us
89 * to recycle minor number as devices are created and removed.
92 zvol_find_minor(unsigned *minor
)
97 ASSERT(MUTEX_HELD(&zvol_state_lock
));
98 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
99 zv
= list_next(&zvol_state_list
, zv
), *minor
+= ZVOL_MINORS
) {
100 if (MINOR(zv
->zv_dev
) != MINOR(*minor
))
104 /* All minors are in use */
105 if (*minor
>= (1 << MINORBITS
))
106 return (SET_ERROR(ENXIO
));
112 * Find a zvol_state_t given the full major+minor dev_t.
114 static zvol_state_t
*
115 zvol_find_by_dev(dev_t dev
)
119 ASSERT(MUTEX_HELD(&zvol_state_lock
));
120 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
121 zv
= list_next(&zvol_state_list
, zv
)) {
122 if (zv
->zv_dev
== dev
)
130 * Find a zvol_state_t given the name provided at zvol_alloc() time.
132 static zvol_state_t
*
133 zvol_find_by_name(const char *name
)
137 ASSERT(MUTEX_HELD(&zvol_state_lock
));
138 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
139 zv
= list_next(&zvol_state_list
, zv
)) {
140 if (strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0)
149 * Given a path, return TRUE if path is a ZVOL.
152 zvol_is_zvol(const char *device
)
154 struct block_device
*bdev
;
157 bdev
= lookup_bdev(device
);
161 major
= MAJOR(bdev
->bd_dev
);
164 if (major
== zvol_major
)
171 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
174 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
176 zfs_creat_t
*zct
= arg
;
177 nvlist_t
*nvprops
= zct
->zct_props
;
179 uint64_t volblocksize
, volsize
;
181 VERIFY(nvlist_lookup_uint64(nvprops
,
182 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
183 if (nvlist_lookup_uint64(nvprops
,
184 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
185 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
188 * These properties must be removed from the list so the generic
189 * property setting step won't apply to them.
191 VERIFY(nvlist_remove_all(nvprops
,
192 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
193 (void) nvlist_remove_all(nvprops
,
194 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
196 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
200 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
204 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
209 * ZFS_IOC_OBJSET_STATS entry point.
212 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
215 dmu_object_info_t
*doi
;
218 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
220 return (SET_ERROR(error
));
222 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
223 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
224 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
227 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
228 doi
->doi_data_block_size
);
231 kmem_free(doi
, sizeof (dmu_object_info_t
));
233 return (SET_ERROR(error
));
237 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
239 struct block_device
*bdev
;
241 bdev
= bdget_disk(zv
->zv_disk
, 0);
244 set_capacity(zv
->zv_disk
, volsize
>> 9);
245 zv
->zv_volsize
= volsize
;
246 check_disk_size_change(zv
->zv_disk
, bdev
);
252 * Sanity check volume size.
255 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
258 return (SET_ERROR(EINVAL
));
260 if (volsize
% blocksize
!= 0)
261 return (SET_ERROR(EINVAL
));
264 if (volsize
- 1 > MAXOFFSET_T
)
265 return (SET_ERROR(EOVERFLOW
));
271 * Ensure the zap is flushed then inform the VFS of the capacity change.
274 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
279 ASSERT(MUTEX_HELD(&zvol_state_lock
));
281 tx
= dmu_tx_create(os
);
282 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
283 dmu_tx_mark_netfree(tx
);
284 error
= dmu_tx_assign(tx
, TXG_WAIT
);
287 return (SET_ERROR(error
));
290 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
295 error
= dmu_free_long_range(os
,
296 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
302 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
304 zvol_size_changed(zv
, volsize
);
307 * We should post a event here describing the expansion. However,
308 * the zfs_ereport_post() interface doesn't nicely support posting
309 * events for zvols, it assumes events relate to vdevs or zios.
316 * Set ZFS_PROP_VOLSIZE set entry point.
319 zvol_set_volsize(const char *name
, uint64_t volsize
)
321 zvol_state_t
*zv
= NULL
;
324 dmu_object_info_t
*doi
;
326 boolean_t owned
= B_FALSE
;
328 error
= dsl_prop_get_integer(name
,
329 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
331 return (SET_ERROR(error
));
333 return (SET_ERROR(EROFS
));
335 mutex_enter(&zvol_state_lock
);
336 zv
= zvol_find_by_name(name
);
338 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
339 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
341 mutex_exit(&zvol_state_lock
);
342 return (SET_ERROR(error
));
351 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
353 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
354 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
357 error
= zvol_update_volsize(volsize
, os
);
358 kmem_free(doi
, sizeof (dmu_object_info_t
));
360 if (error
== 0 && zv
!= NULL
)
361 error
= zvol_update_live_volsize(zv
, volsize
);
364 dmu_objset_disown(os
, FTAG
);
366 zv
->zv_objset
= NULL
;
368 mutex_exit(&zvol_state_lock
);
373 * Sanity check volume block size.
376 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
378 /* Record sizes above 128k need the feature to be enabled */
379 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
383 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
386 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
387 spa_close(spa
, FTAG
);
388 return (SET_ERROR(ENOTSUP
));
392 * We don't allow setting the property above 1MB,
393 * unless the tunable has been changed.
395 if (volblocksize
> zfs_max_recordsize
)
396 return (SET_ERROR(EDOM
));
398 spa_close(spa
, FTAG
);
401 if (volblocksize
< SPA_MINBLOCKSIZE
||
402 volblocksize
> SPA_MAXBLOCKSIZE
||
404 return (SET_ERROR(EDOM
));
410 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
413 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
419 mutex_enter(&zvol_state_lock
);
421 zv
= zvol_find_by_name(name
);
423 error
= SET_ERROR(ENXIO
);
427 if (zv
->zv_flags
& ZVOL_RDONLY
) {
428 error
= SET_ERROR(EROFS
);
432 tx
= dmu_tx_create(zv
->zv_objset
);
433 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
434 error
= dmu_tx_assign(tx
, TXG_WAIT
);
438 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
439 volblocksize
, 0, tx
);
440 if (error
== ENOTSUP
)
441 error
= SET_ERROR(EBUSY
);
444 zv
->zv_volblocksize
= volblocksize
;
447 mutex_exit(&zvol_state_lock
);
449 return (SET_ERROR(error
));
453 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
454 * implement DKIOCFREE/free-long-range.
457 zvol_replay_truncate(zvol_state_t
*zv
, lr_truncate_t
*lr
, boolean_t byteswap
)
459 uint64_t offset
, length
;
462 byteswap_uint64_array(lr
, sizeof (*lr
));
464 offset
= lr
->lr_offset
;
465 length
= lr
->lr_length
;
467 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
471 * Replay a TX_WRITE ZIL transaction that didn't get committed
472 * after a system failure
475 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
477 objset_t
*os
= zv
->zv_objset
;
478 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
479 uint64_t off
= lr
->lr_offset
;
480 uint64_t len
= lr
->lr_length
;
485 byteswap_uint64_array(lr
, sizeof (*lr
));
487 tx
= dmu_tx_create(os
);
488 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
489 error
= dmu_tx_assign(tx
, TXG_WAIT
);
493 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
497 return (SET_ERROR(error
));
501 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
503 return (SET_ERROR(ENOTSUP
));
507 * Callback vectors for replaying records.
508 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
510 zil_replay_func_t zvol_replay_vector
[TX_MAX_TYPE
] = {
511 (zil_replay_func_t
)zvol_replay_err
, /* no such transaction type */
512 (zil_replay_func_t
)zvol_replay_err
, /* TX_CREATE */
513 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKDIR */
514 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKXATTR */
515 (zil_replay_func_t
)zvol_replay_err
, /* TX_SYMLINK */
516 (zil_replay_func_t
)zvol_replay_err
, /* TX_REMOVE */
517 (zil_replay_func_t
)zvol_replay_err
, /* TX_RMDIR */
518 (zil_replay_func_t
)zvol_replay_err
, /* TX_LINK */
519 (zil_replay_func_t
)zvol_replay_err
, /* TX_RENAME */
520 (zil_replay_func_t
)zvol_replay_write
, /* TX_WRITE */
521 (zil_replay_func_t
)zvol_replay_truncate
, /* TX_TRUNCATE */
522 (zil_replay_func_t
)zvol_replay_err
, /* TX_SETATTR */
523 (zil_replay_func_t
)zvol_replay_err
, /* TX_ACL */
527 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
529 * We store data in the log buffers if it's small enough.
530 * Otherwise we will later flush the data out via dmu_sync().
532 ssize_t zvol_immediate_write_sz
= 32768;
535 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
536 uint64_t size
, int sync
)
538 uint32_t blocksize
= zv
->zv_volblocksize
;
539 zilog_t
*zilog
= zv
->zv_zilog
;
541 ssize_t immediate_write_sz
;
543 if (zil_replaying(zilog
, tx
))
546 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
547 ? 0 : zvol_immediate_write_sz
;
548 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
549 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
555 itx_wr_state_t write_state
;
558 * Unlike zfs_log_write() we can be called with
559 * up to DMU_MAX_ACCESS/2 (5MB) writes.
561 if (blocksize
> immediate_write_sz
&& !slogging
&&
562 size
>= blocksize
&& offset
% blocksize
== 0) {
563 write_state
= WR_INDIRECT
; /* uses dmu_sync */
566 write_state
= WR_COPIED
;
567 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
569 write_state
= WR_NEED_COPY
;
570 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
573 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
574 (write_state
== WR_COPIED
? len
: 0));
575 lr
= (lr_write_t
*)&itx
->itx_lr
;
576 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
577 ZVOL_OBJ
, offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
578 zil_itx_destroy(itx
);
579 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
580 lr
= (lr_write_t
*)&itx
->itx_lr
;
581 write_state
= WR_NEED_COPY
;
584 itx
->itx_wr_state
= write_state
;
585 if (write_state
== WR_NEED_COPY
)
587 lr
->lr_foid
= ZVOL_OBJ
;
588 lr
->lr_offset
= offset
;
591 BP_ZERO(&lr
->lr_blkptr
);
593 itx
->itx_private
= zv
;
594 itx
->itx_sync
= sync
;
596 (void) zil_itx_assign(zilog
, itx
, tx
);
604 zvol_write(struct bio
*bio
)
606 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
607 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
608 uint64_t size
= BIO_BI_SIZE(bio
);
614 if (bio
->bi_rw
& VDEV_REQ_FLUSH
)
615 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
618 * Some requests are just for flush and nothing else.
623 uio
.uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
624 uio
.uio_skip
= BIO_BI_SKIP(bio
);
625 uio
.uio_resid
= size
;
626 uio
.uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
627 uio
.uio_loffset
= offset
;
628 uio
.uio_limit
= MAXOFFSET_T
;
629 uio
.uio_segflg
= UIO_BVEC
;
631 rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_WRITER
);
633 tx
= dmu_tx_create(zv
->zv_objset
);
634 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, size
);
636 /* This will only fail for ENOSPC */
637 error
= dmu_tx_assign(tx
, TXG_WAIT
);
640 zfs_range_unlock(rl
);
644 error
= dmu_write_uio(zv
->zv_objset
, ZVOL_OBJ
, &uio
, size
, tx
);
646 zvol_log_write(zv
, tx
, offset
, size
,
647 !!(bio
->bi_rw
& VDEV_REQ_FUA
));
650 zfs_range_unlock(rl
);
652 if ((bio
->bi_rw
& VDEV_REQ_FUA
) ||
653 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
)
654 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
661 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
664 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
669 zilog_t
*zilog
= zv
->zv_zilog
;
671 if (zil_replaying(zilog
, tx
))
674 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
675 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
676 lr
->lr_foid
= ZVOL_OBJ
;
680 itx
->itx_sync
= sync
;
681 zil_itx_assign(zilog
, itx
, tx
);
685 zvol_discard(struct bio
*bio
)
687 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
688 uint64_t start
= BIO_BI_SECTOR(bio
) << 9;
689 uint64_t size
= BIO_BI_SIZE(bio
);
690 uint64_t end
= start
+ size
;
695 if (end
> zv
->zv_volsize
)
696 return (SET_ERROR(EIO
));
699 * Align the request to volume block boundaries when REQ_SECURE is
700 * available, but not requested. If we don't, then this will force
701 * dnode_free_range() to zero out the unaligned parts, which is slow
702 * (read-modify-write) and useless since we are not freeing any space
703 * by doing so. Kernels that do not support REQ_SECURE (2.6.32 through
704 * 2.6.35) will not receive this optimization.
707 if (!(bio
->bi_rw
& REQ_SECURE
)) {
708 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
709 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
717 rl
= zfs_range_lock(&zv
->zv_znode
, start
, size
, RL_WRITER
);
718 tx
= dmu_tx_create(zv
->zv_objset
);
719 dmu_tx_mark_netfree(tx
);
720 error
= dmu_tx_assign(tx
, TXG_WAIT
);
724 zvol_log_truncate(zv
, tx
, start
, size
, B_TRUE
);
726 error
= dmu_free_long_range(zv
->zv_objset
,
727 ZVOL_OBJ
, start
, size
);
730 zfs_range_unlock(rl
);
736 zvol_read(struct bio
*bio
)
738 zvol_state_t
*zv
= bio
->bi_bdev
->bd_disk
->private_data
;
739 uint64_t offset
= BIO_BI_SECTOR(bio
) << 9;
740 uint64_t size
= BIO_BI_SIZE(bio
);
748 uio
.uio_bvec
= &bio
->bi_io_vec
[BIO_BI_IDX(bio
)];
749 uio
.uio_skip
= BIO_BI_SKIP(bio
);
750 uio
.uio_resid
= size
;
751 uio
.uio_iovcnt
= bio
->bi_vcnt
- BIO_BI_IDX(bio
);
752 uio
.uio_loffset
= offset
;
753 uio
.uio_limit
= MAXOFFSET_T
;
754 uio
.uio_segflg
= UIO_BVEC
;
756 rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
758 error
= dmu_read_uio(zv
->zv_objset
, ZVOL_OBJ
, &uio
, size
);
760 zfs_range_unlock(rl
);
762 /* convert checksum errors into IO errors */
764 error
= SET_ERROR(EIO
);
769 static MAKE_REQUEST_FN_RET
770 zvol_request(struct request_queue
*q
, struct bio
*bio
)
772 zvol_state_t
*zv
= q
->queuedata
;
773 fstrans_cookie_t cookie
= spl_fstrans_mark();
774 uint64_t offset
= BIO_BI_SECTOR(bio
);
775 unsigned int sectors
= bio_sectors(bio
);
776 int rw
= bio_data_dir(bio
);
777 #ifdef HAVE_GENERIC_IO_ACCT
778 unsigned long start
= jiffies
;
782 if (bio_has_data(bio
) && offset
+ sectors
>
783 get_capacity(zv
->zv_disk
)) {
785 "%s: bad access: block=%llu, count=%lu\n",
786 zv
->zv_disk
->disk_name
,
787 (long long unsigned)offset
,
788 (long unsigned)sectors
);
789 error
= SET_ERROR(EIO
);
793 generic_start_io_acct(rw
, sectors
, &zv
->zv_disk
->part0
);
796 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
797 error
= SET_ERROR(EROFS
);
801 if (bio
->bi_rw
& VDEV_REQ_DISCARD
) {
802 error
= zvol_discard(bio
);
806 error
= zvol_write(bio
);
808 error
= zvol_read(bio
);
811 generic_end_io_acct(rw
, &zv
->zv_disk
->part0
, start
);
813 BIO_END_IO(bio
, -error
);
814 spl_fstrans_unmark(cookie
);
815 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
817 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
818 return (BLK_QC_T_NONE
);
823 zvol_get_done(zgd_t
*zgd
, int error
)
826 dmu_buf_rele(zgd
->zgd_db
, zgd
);
828 zfs_range_unlock(zgd
->zgd_rl
);
830 if (error
== 0 && zgd
->zgd_bp
)
831 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
833 kmem_free(zgd
, sizeof (zgd_t
));
837 * Get data to generate a TX_WRITE intent log record.
840 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
842 zvol_state_t
*zv
= arg
;
843 objset_t
*os
= zv
->zv_objset
;
844 uint64_t object
= ZVOL_OBJ
;
845 uint64_t offset
= lr
->lr_offset
;
846 uint64_t size
= lr
->lr_length
;
847 blkptr_t
*bp
= &lr
->lr_blkptr
;
855 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
856 zgd
->zgd_zilog
= zv
->zv_zilog
;
857 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
860 * Write records come in two flavors: immediate and indirect.
861 * For small writes it's cheaper to store the data with the
862 * log record (immediate); for large writes it's cheaper to
863 * sync the data and get a pointer to it (indirect) so that
864 * we don't have to write the data twice.
866 if (buf
!= NULL
) { /* immediate write */
867 error
= dmu_read(os
, object
, offset
, size
, buf
,
868 DMU_READ_NO_PREFETCH
);
870 size
= zv
->zv_volblocksize
;
871 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
872 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
873 DMU_READ_NO_PREFETCH
);
875 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
877 ASSERT(BP_IS_HOLE(bp
));
882 zgd
->zgd_bp
= &lr
->lr_blkptr
;
885 ASSERT(db
->db_offset
== offset
);
886 ASSERT(db
->db_size
== size
);
888 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
896 zvol_get_done(zgd
, error
);
898 return (SET_ERROR(error
));
902 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
905 zvol_insert(zvol_state_t
*zv_insert
)
907 zvol_state_t
*zv
= NULL
;
909 ASSERT(MUTEX_HELD(&zvol_state_lock
));
910 ASSERT3U(MINOR(zv_insert
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
911 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
912 zv
= list_next(&zvol_state_list
, zv
)) {
913 if (MINOR(zv
->zv_dev
) > MINOR(zv_insert
->zv_dev
))
917 list_insert_before(&zvol_state_list
, zv
, zv_insert
);
921 * Simply remove the zvol from to list of zvols.
924 zvol_remove(zvol_state_t
*zv_remove
)
926 ASSERT(MUTEX_HELD(&zvol_state_lock
));
927 list_remove(&zvol_state_list
, zv_remove
);
931 zvol_first_open(zvol_state_t
*zv
)
940 * In all other cases the spa_namespace_lock is taken before the
941 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
942 * function calls fops->open() with the bdev->bd_mutex lock held.
944 * To avoid a potential lock inversion deadlock we preemptively
945 * try to take the spa_namespace_lock(). Normally it will not
946 * be contended and this is safe because spa_open_common() handles
947 * the case where the caller already holds the spa_namespace_lock.
949 * When it is contended we risk a lock inversion if we were to
950 * block waiting for the lock. Luckily, the __blkdev_get()
951 * function allows us to return -ERESTARTSYS which will result in
952 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
953 * called again. This process can be repeated safely until both
954 * locks are acquired.
956 if (!mutex_owned(&spa_namespace_lock
)) {
957 locked
= mutex_tryenter(&spa_namespace_lock
);
959 return (-SET_ERROR(ERESTARTSYS
));
962 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
966 /* lie and say we're read-only */
967 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, 1, zvol_tag
, &os
);
971 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
973 dmu_objset_disown(os
, zvol_tag
);
978 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
980 dmu_objset_disown(os
, zvol_tag
);
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
;
999 mutex_exit(&spa_namespace_lock
);
1001 return (SET_ERROR(-error
));
1005 zvol_last_close(zvol_state_t
*zv
)
1007 zil_close(zv
->zv_zilog
);
1008 zv
->zv_zilog
= NULL
;
1010 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
1016 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
1017 !(zv
->zv_flags
& ZVOL_RDONLY
))
1018 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1019 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
1021 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
1022 zv
->zv_objset
= NULL
;
1026 zvol_open(struct block_device
*bdev
, fmode_t flag
)
1028 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1029 int error
= 0, drop_mutex
= 0;
1032 * If the caller is already holding the mutex do not take it
1033 * again, this will happen as part of zvol_create_minor().
1034 * Once add_disk() is called the device is live and the kernel
1035 * will attempt to open it to read the partition information.
1037 if (!mutex_owned(&zvol_state_lock
)) {
1038 mutex_enter(&zvol_state_lock
);
1042 ASSERT3P(zv
, !=, NULL
);
1044 if (zv
->zv_open_count
== 0) {
1045 error
= zvol_first_open(zv
);
1050 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1052 goto out_open_count
;
1055 zv
->zv_open_count
++;
1058 if (zv
->zv_open_count
== 0)
1059 zvol_last_close(zv
);
1063 mutex_exit(&zvol_state_lock
);
1065 check_disk_change(bdev
);
1067 return (SET_ERROR(error
));
1070 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1075 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1077 zvol_state_t
*zv
= disk
->private_data
;
1080 if (!mutex_owned(&zvol_state_lock
)) {
1081 mutex_enter(&zvol_state_lock
);
1085 if (zv
->zv_open_count
> 0) {
1086 zv
->zv_open_count
--;
1087 if (zv
->zv_open_count
== 0)
1088 zvol_last_close(zv
);
1092 mutex_exit(&zvol_state_lock
);
1094 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1100 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1101 unsigned int cmd
, unsigned long arg
)
1103 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1107 return (SET_ERROR(-ENXIO
));
1111 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1114 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1123 return (SET_ERROR(error
));
1126 #ifdef CONFIG_COMPAT
1128 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1129 unsigned cmd
, unsigned long arg
)
1131 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1134 #define zvol_compat_ioctl NULL
1137 static int zvol_media_changed(struct gendisk
*disk
)
1139 zvol_state_t
*zv
= disk
->private_data
;
1141 return (zv
->zv_changed
);
1144 static int zvol_revalidate_disk(struct gendisk
*disk
)
1146 zvol_state_t
*zv
= disk
->private_data
;
1149 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1155 * Provide a simple virtual geometry for legacy compatibility. For devices
1156 * smaller than 1 MiB a small head and sector count is used to allow very
1157 * tiny devices. For devices over 1 Mib a standard head and sector count
1158 * is used to keep the cylinders count reasonable.
1161 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1163 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1164 sector_t sectors
= get_capacity(zv
->zv_disk
);
1166 if (sectors
> 2048) {
1175 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1180 static struct kobject
*
1181 zvol_probe(dev_t dev
, int *part
, void *arg
)
1184 struct kobject
*kobj
;
1186 mutex_enter(&zvol_state_lock
);
1187 zv
= zvol_find_by_dev(dev
);
1188 kobj
= zv
? get_disk(zv
->zv_disk
) : NULL
;
1189 mutex_exit(&zvol_state_lock
);
1194 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1195 static struct block_device_operations zvol_ops
= {
1197 .release
= zvol_release
,
1198 .ioctl
= zvol_ioctl
,
1199 .compat_ioctl
= zvol_compat_ioctl
,
1200 .media_changed
= zvol_media_changed
,
1201 .revalidate_disk
= zvol_revalidate_disk
,
1202 .getgeo
= zvol_getgeo
,
1203 .owner
= THIS_MODULE
,
1206 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1209 zvol_open_by_inode(struct inode
*inode
, struct file
*file
)
1211 return (zvol_open(inode
->i_bdev
, file
->f_mode
));
1215 zvol_release_by_inode(struct inode
*inode
, struct file
*file
)
1217 return (zvol_release(inode
->i_bdev
->bd_disk
, file
->f_mode
));
1221 zvol_ioctl_by_inode(struct inode
*inode
, struct file
*file
,
1222 unsigned int cmd
, unsigned long arg
)
1224 if (file
== NULL
|| inode
== NULL
)
1225 return (SET_ERROR(-EINVAL
));
1227 return (zvol_ioctl(inode
->i_bdev
, file
->f_mode
, cmd
, arg
));
1230 #ifdef CONFIG_COMPAT
1232 zvol_compat_ioctl_by_inode(struct file
*file
,
1233 unsigned int cmd
, unsigned long arg
)
1236 return (SET_ERROR(-EINVAL
));
1238 return (zvol_compat_ioctl(file
->f_dentry
->d_inode
->i_bdev
,
1239 file
->f_mode
, cmd
, arg
));
1242 #define zvol_compat_ioctl_by_inode NULL
1245 static struct block_device_operations zvol_ops
= {
1246 .open
= zvol_open_by_inode
,
1247 .release
= zvol_release_by_inode
,
1248 .ioctl
= zvol_ioctl_by_inode
,
1249 .compat_ioctl
= zvol_compat_ioctl_by_inode
,
1250 .media_changed
= zvol_media_changed
,
1251 .revalidate_disk
= zvol_revalidate_disk
,
1252 .getgeo
= zvol_getgeo
,
1253 .owner
= THIS_MODULE
,
1255 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1258 * Allocate memory for a new zvol_state_t and setup the required
1259 * request queue and generic disk structures for the block device.
1261 static zvol_state_t
*
1262 zvol_alloc(dev_t dev
, const char *name
)
1266 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1268 list_link_init(&zv
->zv_next
);
1270 zv
->zv_queue
= blk_alloc_queue(GFP_ATOMIC
);
1271 if (zv
->zv_queue
== NULL
)
1274 blk_queue_make_request(zv
->zv_queue
, zvol_request
);
1276 #ifdef HAVE_BLK_QUEUE_FLUSH
1277 blk_queue_flush(zv
->zv_queue
, VDEV_REQ_FLUSH
| VDEV_REQ_FUA
);
1279 blk_queue_ordered(zv
->zv_queue
, QUEUE_ORDERED_DRAIN
, NULL
);
1280 #endif /* HAVE_BLK_QUEUE_FLUSH */
1282 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1283 if (zv
->zv_disk
== NULL
)
1286 zv
->zv_queue
->queuedata
= zv
;
1288 zv
->zv_open_count
= 0;
1289 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1291 mutex_init(&zv
->zv_znode
.z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1292 avl_create(&zv
->zv_znode
.z_range_avl
, zfs_range_compare
,
1293 sizeof (rl_t
), offsetof(rl_t
, r_node
));
1294 zv
->zv_znode
.z_is_zvol
= TRUE
;
1296 zv
->zv_disk
->major
= zvol_major
;
1297 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1298 zv
->zv_disk
->fops
= &zvol_ops
;
1299 zv
->zv_disk
->private_data
= zv
;
1300 zv
->zv_disk
->queue
= zv
->zv_queue
;
1301 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1302 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1307 blk_cleanup_queue(zv
->zv_queue
);
1309 kmem_free(zv
, sizeof (zvol_state_t
));
1315 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1318 zvol_free(zvol_state_t
*zv
)
1320 avl_destroy(&zv
->zv_znode
.z_range_avl
);
1321 mutex_destroy(&zv
->zv_znode
.z_range_lock
);
1323 del_gendisk(zv
->zv_disk
);
1324 blk_cleanup_queue(zv
->zv_queue
);
1325 put_disk(zv
->zv_disk
);
1327 kmem_free(zv
, sizeof (zvol_state_t
));
1331 __zvol_snapdev_hidden(const char *name
)
1338 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1339 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1341 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1343 error
= dsl_prop_get_integer(parent
, "snapdev", &snapdev
, NULL
);
1344 if ((error
== 0) && (snapdev
== ZFS_SNAPDEV_HIDDEN
))
1345 error
= SET_ERROR(ENODEV
);
1348 kmem_free(parent
, MAXPATHLEN
);
1350 return (SET_ERROR(error
));
1354 __zvol_create_minor(const char *name
, boolean_t ignore_snapdev
)
1358 dmu_object_info_t
*doi
;
1364 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1366 zv
= zvol_find_by_name(name
);
1368 error
= SET_ERROR(EEXIST
);
1372 if (ignore_snapdev
== B_FALSE
) {
1373 error
= __zvol_snapdev_hidden(name
);
1378 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1380 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, zvol_tag
, &os
);
1384 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1386 goto out_dmu_objset_disown
;
1388 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1390 goto out_dmu_objset_disown
;
1392 error
= zvol_find_minor(&minor
);
1394 goto out_dmu_objset_disown
;
1396 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1398 error
= SET_ERROR(EAGAIN
);
1399 goto out_dmu_objset_disown
;
1402 if (dmu_objset_is_snapshot(os
))
1403 zv
->zv_flags
|= ZVOL_RDONLY
;
1405 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1406 zv
->zv_volsize
= volsize
;
1409 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1411 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1412 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1413 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1414 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1415 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1416 blk_queue_max_discard_sectors(zv
->zv_queue
,
1417 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1418 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1419 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1420 #ifdef QUEUE_FLAG_NONROT
1421 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1423 #ifdef QUEUE_FLAG_ADD_RANDOM
1424 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, zv
->zv_queue
);
1427 if (spa_writeable(dmu_objset_spa(os
))) {
1428 if (zil_replay_disable
)
1429 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1431 zil_replay(os
, zv
, zvol_replay_vector
);
1435 * When udev detects the addition of the device it will immediately
1436 * invoke blkid(8) to determine the type of content on the device.
1437 * Prefetching the blocks commonly scanned by blkid(8) will speed
1440 len
= MIN(MAX(zvol_prefetch_bytes
, 0), SPA_MAXBLOCKSIZE
);
1442 dmu_prefetch(os
, ZVOL_OBJ
, 0, 0, len
, ZIO_PRIORITY_SYNC_READ
);
1443 dmu_prefetch(os
, ZVOL_OBJ
, 0, volsize
- len
, len
,
1444 ZIO_PRIORITY_SYNC_READ
);
1447 zv
->zv_objset
= NULL
;
1448 out_dmu_objset_disown
:
1449 dmu_objset_disown(os
, zvol_tag
);
1451 kmem_free(doi
, sizeof (dmu_object_info_t
));
1456 add_disk(zv
->zv_disk
);
1459 return (SET_ERROR(error
));
1463 * Create a block device minor node and setup the linkage between it
1464 * and the specified volume. Once this function returns the block
1465 * device is live and ready for use.
1468 zvol_create_minor(const char *name
)
1472 mutex_enter(&zvol_state_lock
);
1473 error
= __zvol_create_minor(name
, B_FALSE
);
1474 mutex_exit(&zvol_state_lock
);
1476 return (SET_ERROR(error
));
1480 __zvol_remove_minor(const char *name
)
1484 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1486 zv
= zvol_find_by_name(name
);
1488 return (SET_ERROR(ENXIO
));
1490 if (zv
->zv_open_count
> 0)
1491 return (SET_ERROR(EBUSY
));
1500 * Remove a block device minor node for the specified volume.
1503 zvol_remove_minor(const char *name
)
1507 mutex_enter(&zvol_state_lock
);
1508 error
= __zvol_remove_minor(name
);
1509 mutex_exit(&zvol_state_lock
);
1511 return (SET_ERROR(error
));
1515 * Rename a block device minor mode for the specified volume.
1518 __zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1520 int readonly
= get_disk_ro(zv
->zv_disk
);
1522 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1524 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1527 * The block device's read-only state is briefly changed causing
1528 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1529 * the name change and fixes the symlinks. This does not change
1530 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1531 * changes. This would normally be done using kobject_uevent() but
1532 * that is a GPL-only symbol which is why we need this workaround.
1534 set_disk_ro(zv
->zv_disk
, !readonly
);
1535 set_disk_ro(zv
->zv_disk
, readonly
);
1539 zvol_create_minors_cb(const char *dsname
, void *arg
)
1541 (void) zvol_create_minor(dsname
);
1547 * Create minors for specified dataset including children and snapshots.
1550 zvol_create_minors(const char *name
)
1554 if (!zvol_inhibit_dev
)
1555 error
= dmu_objset_find((char *)name
, zvol_create_minors_cb
,
1556 NULL
, DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
1558 return (SET_ERROR(error
));
1562 * Remove minors for specified dataset including children and snapshots.
1565 zvol_remove_minors(const char *name
)
1567 zvol_state_t
*zv
, *zv_next
;
1568 int namelen
= ((name
) ? strlen(name
) : 0);
1570 if (zvol_inhibit_dev
)
1573 mutex_enter(&zvol_state_lock
);
1575 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1576 zv_next
= list_next(&zvol_state_list
, zv
);
1578 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1579 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1580 zv
->zv_name
[namelen
] == '/')) {
1586 mutex_exit(&zvol_state_lock
);
1590 * Rename minors for specified dataset including children and snapshots.
1593 zvol_rename_minors(const char *oldname
, const char *newname
)
1595 zvol_state_t
*zv
, *zv_next
;
1596 int oldnamelen
, newnamelen
;
1599 if (zvol_inhibit_dev
)
1602 oldnamelen
= strlen(oldname
);
1603 newnamelen
= strlen(newname
);
1604 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1606 mutex_enter(&zvol_state_lock
);
1608 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1609 zv_next
= list_next(&zvol_state_list
, zv
);
1611 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1612 __zvol_rename_minor(zv
, newname
);
1613 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1614 (zv
->zv_name
[oldnamelen
] == '/' ||
1615 zv
->zv_name
[oldnamelen
] == '@')) {
1616 snprintf(name
, MAXNAMELEN
, "%s%c%s", newname
,
1617 zv
->zv_name
[oldnamelen
],
1618 zv
->zv_name
+ oldnamelen
+ 1);
1619 __zvol_rename_minor(zv
, name
);
1623 mutex_exit(&zvol_state_lock
);
1625 kmem_free(name
, MAXNAMELEN
);
1629 snapdev_snapshot_changed_cb(const char *dsname
, void *arg
) {
1630 uint64_t snapdev
= *(uint64_t *) arg
;
1632 if (strchr(dsname
, '@') == NULL
)
1636 case ZFS_SNAPDEV_VISIBLE
:
1637 mutex_enter(&zvol_state_lock
);
1638 (void) __zvol_create_minor(dsname
, B_TRUE
);
1639 mutex_exit(&zvol_state_lock
);
1641 case ZFS_SNAPDEV_HIDDEN
:
1642 (void) zvol_remove_minor(dsname
);
1650 zvol_set_snapdev(const char *dsname
, uint64_t snapdev
) {
1651 (void) dmu_objset_find((char *) dsname
, snapdev_snapshot_changed_cb
,
1652 &snapdev
, DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
1653 /* caller should continue to modify snapdev property */
1662 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1663 offsetof(zvol_state_t
, zv_next
));
1665 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1667 error
= register_blkdev(zvol_major
, ZVOL_DRIVER
);
1669 printk(KERN_INFO
"ZFS: register_blkdev() failed %d\n", error
);
1673 blk_register_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
,
1674 THIS_MODULE
, zvol_probe
, NULL
, NULL
);
1679 mutex_destroy(&zvol_state_lock
);
1680 list_destroy(&zvol_state_list
);
1682 return (SET_ERROR(error
));
1688 zvol_remove_minors(NULL
);
1689 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
1690 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1691 mutex_destroy(&zvol_state_lock
);
1692 list_destroy(&zvol_state_list
);
1695 module_param(zvol_inhibit_dev
, uint
, 0644);
1696 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1698 module_param(zvol_major
, uint
, 0444);
1699 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1701 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1702 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");
1704 module_param(zvol_prefetch_bytes
, uint
, 0644);
1705 MODULE_PARM_DESC(zvol_prefetch_bytes
, "Prefetch N bytes at zvol start+end");