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.
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_prop.h>
43 #include <sys/zfeature.h>
44 #include <sys/zil_impl.h>
46 #include <sys/zfs_rlock.h>
47 #include <sys/zfs_znode.h>
49 #include <linux/blkdev_compat.h>
51 unsigned int zvol_inhibit_dev
= 0;
52 unsigned int zvol_major
= ZVOL_MAJOR
;
53 unsigned int zvol_threads
= 32;
54 unsigned long zvol_max_discard_blocks
= 16384;
56 static taskq_t
*zvol_taskq
;
57 static kmutex_t zvol_state_lock
;
58 static list_t zvol_state_list
;
59 static char *zvol_tag
= "zvol_tag";
62 * The in-core state of each volume.
64 typedef struct zvol_state
{
65 char zv_name
[MAXNAMELEN
]; /* name */
66 uint64_t zv_volsize
; /* advertised space */
67 uint64_t zv_volblocksize
; /* volume block size */
68 objset_t
*zv_objset
; /* objset handle */
69 uint32_t zv_flags
; /* ZVOL_* flags */
70 uint32_t zv_open_count
; /* open counts */
71 uint32_t zv_changed
; /* disk changed */
72 zilog_t
*zv_zilog
; /* ZIL handle */
73 znode_t zv_znode
; /* for range locking */
74 dmu_buf_t
*zv_dbuf
; /* bonus handle */
75 dev_t zv_dev
; /* device id */
76 struct gendisk
*zv_disk
; /* generic disk */
77 struct request_queue
*zv_queue
; /* request queue */
78 spinlock_t zv_lock
; /* request queue lock */
79 list_node_t zv_next
; /* next zvol_state_t linkage */
82 #define ZVOL_RDONLY 0x1
85 * Find the next available range of ZVOL_MINORS minor numbers. The
86 * zvol_state_list is kept in ascending minor order so we simply need
87 * to scan the list for the first gap in the sequence. This allows us
88 * to recycle minor number as devices are created and removed.
91 zvol_find_minor(unsigned *minor
)
96 ASSERT(MUTEX_HELD(&zvol_state_lock
));
97 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
98 zv
= list_next(&zvol_state_list
, zv
), *minor
+= ZVOL_MINORS
) {
99 if (MINOR(zv
->zv_dev
) != MINOR(*minor
))
103 /* All minors are in use */
104 if (*minor
>= (1 << MINORBITS
))
105 return (SET_ERROR(ENXIO
));
111 * Find a zvol_state_t given the full major+minor dev_t.
113 static zvol_state_t
*
114 zvol_find_by_dev(dev_t dev
)
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
)) {
121 if (zv
->zv_dev
== dev
)
129 * Find a zvol_state_t given the name provided at zvol_alloc() time.
131 static zvol_state_t
*
132 zvol_find_by_name(const char *name
)
136 ASSERT(MUTEX_HELD(&zvol_state_lock
));
137 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
138 zv
= list_next(&zvol_state_list
, zv
)) {
139 if (strncmp(zv
->zv_name
, name
, MAXNAMELEN
) == 0)
148 * Given a path, return TRUE if path is a ZVOL.
151 zvol_is_zvol(const char *device
)
153 struct block_device
*bdev
;
156 bdev
= lookup_bdev(device
);
160 major
= MAJOR(bdev
->bd_dev
);
163 if (major
== zvol_major
)
170 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
173 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
175 zfs_creat_t
*zct
= arg
;
176 nvlist_t
*nvprops
= zct
->zct_props
;
178 uint64_t volblocksize
, volsize
;
180 VERIFY(nvlist_lookup_uint64(nvprops
,
181 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
182 if (nvlist_lookup_uint64(nvprops
,
183 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
184 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
187 * These properties must be removed from the list so the generic
188 * property setting step won't apply to them.
190 VERIFY(nvlist_remove_all(nvprops
,
191 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
192 (void) nvlist_remove_all(nvprops
,
193 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
195 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
199 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
203 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
208 * ZFS_IOC_OBJSET_STATS entry point.
211 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
214 dmu_object_info_t
*doi
;
217 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
219 return (SET_ERROR(error
));
221 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
222 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
223 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
226 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
227 doi
->doi_data_block_size
);
230 kmem_free(doi
, sizeof (dmu_object_info_t
));
232 return (SET_ERROR(error
));
236 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
238 struct block_device
*bdev
;
240 bdev
= bdget_disk(zv
->zv_disk
, 0);
245 * Added check_disk_size_change() helper function.
247 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
248 set_capacity(zv
->zv_disk
, volsize
>> 9);
249 zv
->zv_volsize
= volsize
;
250 check_disk_size_change(zv
->zv_disk
, bdev
);
252 zv
->zv_volsize
= volsize
;
254 (void) check_disk_change(bdev
);
255 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
261 * Sanity check volume size.
264 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
267 return (SET_ERROR(EINVAL
));
269 if (volsize
% blocksize
!= 0)
270 return (SET_ERROR(EINVAL
));
273 if (volsize
- 1 > MAXOFFSET_T
)
274 return (SET_ERROR(EOVERFLOW
));
280 * Ensure the zap is flushed then inform the VFS of the capacity change.
283 zvol_update_volsize(uint64_t volsize
, objset_t
*os
)
288 ASSERT(MUTEX_HELD(&zvol_state_lock
));
290 tx
= dmu_tx_create(os
);
291 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
292 error
= dmu_tx_assign(tx
, TXG_WAIT
);
295 return (SET_ERROR(error
));
298 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
303 error
= dmu_free_long_range(os
,
304 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
310 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
312 zvol_size_changed(zv
, volsize
);
315 * We should post a event here describing the expansion. However,
316 * the zfs_ereport_post() interface doesn't nicely support posting
317 * events for zvols, it assumes events relate to vdevs or zios.
324 * Set ZFS_PROP_VOLSIZE set entry point.
327 zvol_set_volsize(const char *name
, uint64_t volsize
)
329 zvol_state_t
*zv
= NULL
;
332 dmu_object_info_t
*doi
;
334 boolean_t owned
= B_FALSE
;
336 error
= dsl_prop_get_integer(name
,
337 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
339 return (SET_ERROR(error
));
341 return (SET_ERROR(EROFS
));
343 mutex_enter(&zvol_state_lock
);
344 zv
= zvol_find_by_name(name
);
346 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
347 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
349 mutex_exit(&zvol_state_lock
);
350 return (SET_ERROR(error
));
359 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
361 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, doi
)) ||
362 (error
= zvol_check_volsize(volsize
, doi
->doi_data_block_size
)))
365 error
= zvol_update_volsize(volsize
, os
);
366 kmem_free(doi
, sizeof (dmu_object_info_t
));
368 if (error
== 0 && zv
!= NULL
)
369 error
= zvol_update_live_volsize(zv
, volsize
);
372 dmu_objset_disown(os
, FTAG
);
374 zv
->zv_objset
= NULL
;
376 mutex_exit(&zvol_state_lock
);
381 * Sanity check volume block size.
384 zvol_check_volblocksize(const char *name
, uint64_t volblocksize
)
386 /* Record sizes above 128k need the feature to be enabled */
387 if (volblocksize
> SPA_OLD_MAXBLOCKSIZE
) {
391 if ((error
= spa_open(name
, &spa
, FTAG
)) != 0)
394 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
395 spa_close(spa
, FTAG
);
396 return (SET_ERROR(ENOTSUP
));
400 * We don't allow setting the property above 1MB,
401 * unless the tunable has been changed.
403 if (volblocksize
> zfs_max_recordsize
)
404 return (SET_ERROR(EDOM
));
406 spa_close(spa
, FTAG
);
409 if (volblocksize
< SPA_MINBLOCKSIZE
||
410 volblocksize
> SPA_MAXBLOCKSIZE
||
412 return (SET_ERROR(EDOM
));
418 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
421 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
427 mutex_enter(&zvol_state_lock
);
429 zv
= zvol_find_by_name(name
);
431 error
= SET_ERROR(ENXIO
);
435 if (zv
->zv_flags
& ZVOL_RDONLY
) {
436 error
= SET_ERROR(EROFS
);
440 tx
= dmu_tx_create(zv
->zv_objset
);
441 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
442 error
= dmu_tx_assign(tx
, TXG_WAIT
);
446 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
447 volblocksize
, 0, tx
);
448 if (error
== ENOTSUP
)
449 error
= SET_ERROR(EBUSY
);
452 zv
->zv_volblocksize
= volblocksize
;
455 mutex_exit(&zvol_state_lock
);
457 return (SET_ERROR(error
));
461 * Replay a TX_WRITE ZIL transaction that didn't get committed
462 * after a system failure
465 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
467 objset_t
*os
= zv
->zv_objset
;
468 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
469 uint64_t off
= lr
->lr_offset
;
470 uint64_t len
= lr
->lr_length
;
475 byteswap_uint64_array(lr
, sizeof (*lr
));
477 tx
= dmu_tx_create(os
);
478 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
479 error
= dmu_tx_assign(tx
, TXG_WAIT
);
483 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
487 return (SET_ERROR(error
));
491 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
493 return (SET_ERROR(ENOTSUP
));
497 * Callback vectors for replaying records.
498 * Only TX_WRITE is needed for zvol.
500 zil_replay_func_t zvol_replay_vector
[TX_MAX_TYPE
] = {
501 (zil_replay_func_t
)zvol_replay_err
, /* no such transaction type */
502 (zil_replay_func_t
)zvol_replay_err
, /* TX_CREATE */
503 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKDIR */
504 (zil_replay_func_t
)zvol_replay_err
, /* TX_MKXATTR */
505 (zil_replay_func_t
)zvol_replay_err
, /* TX_SYMLINK */
506 (zil_replay_func_t
)zvol_replay_err
, /* TX_REMOVE */
507 (zil_replay_func_t
)zvol_replay_err
, /* TX_RMDIR */
508 (zil_replay_func_t
)zvol_replay_err
, /* TX_LINK */
509 (zil_replay_func_t
)zvol_replay_err
, /* TX_RENAME */
510 (zil_replay_func_t
)zvol_replay_write
, /* TX_WRITE */
511 (zil_replay_func_t
)zvol_replay_err
, /* TX_TRUNCATE */
512 (zil_replay_func_t
)zvol_replay_err
, /* TX_SETATTR */
513 (zil_replay_func_t
)zvol_replay_err
, /* TX_ACL */
517 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
519 * We store data in the log buffers if it's small enough.
520 * Otherwise we will later flush the data out via dmu_sync().
522 ssize_t zvol_immediate_write_sz
= 32768;
525 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t offset
,
526 uint64_t size
, int sync
)
528 uint32_t blocksize
= zv
->zv_volblocksize
;
529 zilog_t
*zilog
= zv
->zv_zilog
;
531 ssize_t immediate_write_sz
;
533 if (zil_replaying(zilog
, tx
))
536 immediate_write_sz
= (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
537 ? 0 : zvol_immediate_write_sz
;
538 slogging
= spa_has_slogs(zilog
->zl_spa
) &&
539 (zilog
->zl_logbias
== ZFS_LOGBIAS_LATENCY
);
545 itx_wr_state_t write_state
;
548 * Unlike zfs_log_write() we can be called with
549 * up to DMU_MAX_ACCESS/2 (5MB) writes.
551 if (blocksize
> immediate_write_sz
&& !slogging
&&
552 size
>= blocksize
&& offset
% blocksize
== 0) {
553 write_state
= WR_INDIRECT
; /* uses dmu_sync */
556 write_state
= WR_COPIED
;
557 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
559 write_state
= WR_NEED_COPY
;
560 len
= MIN(ZIL_MAX_LOG_DATA
, size
);
563 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
564 (write_state
== WR_COPIED
? len
: 0));
565 lr
= (lr_write_t
*)&itx
->itx_lr
;
566 if (write_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
567 ZVOL_OBJ
, offset
, len
, lr
+1, DMU_READ_NO_PREFETCH
) != 0) {
568 zil_itx_destroy(itx
);
569 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
570 lr
= (lr_write_t
*)&itx
->itx_lr
;
571 write_state
= WR_NEED_COPY
;
574 itx
->itx_wr_state
= write_state
;
575 if (write_state
== WR_NEED_COPY
)
577 lr
->lr_foid
= ZVOL_OBJ
;
578 lr
->lr_offset
= offset
;
581 BP_ZERO(&lr
->lr_blkptr
);
583 itx
->itx_private
= zv
;
584 itx
->itx_sync
= sync
;
586 (void) zil_itx_assign(zilog
, itx
, tx
);
594 * Common write path running under the zvol taskq context. This function
595 * is responsible for copying the request structure data in to the DMU and
596 * signaling the request queue with the result of the copy.
599 zvol_write(void *arg
)
601 struct request
*req
= (struct request
*)arg
;
602 struct request_queue
*q
= req
->q
;
603 zvol_state_t
*zv
= q
->queuedata
;
604 fstrans_cookie_t cookie
= spl_fstrans_mark();
605 uint64_t offset
= blk_rq_pos(req
) << 9;
606 uint64_t size
= blk_rq_bytes(req
);
611 if (req
->cmd_flags
& VDEV_REQ_FLUSH
)
612 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
615 * Some requests are just for flush and nothing else.
622 rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_WRITER
);
624 tx
= dmu_tx_create(zv
->zv_objset
);
625 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, size
);
627 /* This will only fail for ENOSPC */
628 error
= dmu_tx_assign(tx
, TXG_WAIT
);
631 zfs_range_unlock(rl
);
635 error
= dmu_write_req(zv
->zv_objset
, ZVOL_OBJ
, req
, tx
);
637 zvol_log_write(zv
, tx
, offset
, size
,
638 req
->cmd_flags
& VDEV_REQ_FUA
);
641 zfs_range_unlock(rl
);
643 if ((req
->cmd_flags
& VDEV_REQ_FUA
) ||
644 zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
)
645 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
648 blk_end_request(req
, -error
, size
);
649 spl_fstrans_unmark(cookie
);
652 #ifdef HAVE_BLK_QUEUE_DISCARD
654 zvol_discard(void *arg
)
656 struct request
*req
= (struct request
*)arg
;
657 struct request_queue
*q
= req
->q
;
658 zvol_state_t
*zv
= q
->queuedata
;
659 fstrans_cookie_t cookie
= spl_fstrans_mark();
660 uint64_t start
= blk_rq_pos(req
) << 9;
661 uint64_t end
= start
+ blk_rq_bytes(req
);
665 if (end
> zv
->zv_volsize
) {
671 * Align the request to volume block boundaries. If we don't,
672 * then this will force dnode_free_range() to zero out the
673 * unaligned parts, which is slow (read-modify-write) and
674 * useless since we are not freeing any space by doing so.
676 start
= P2ROUNDUP(start
, zv
->zv_volblocksize
);
677 end
= P2ALIGN(end
, zv
->zv_volblocksize
);
684 rl
= zfs_range_lock(&zv
->zv_znode
, start
, end
- start
, RL_WRITER
);
686 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, start
, end
-start
);
689 * TODO: maybe we should add the operation to the log.
692 zfs_range_unlock(rl
);
694 blk_end_request(req
, -error
, blk_rq_bytes(req
));
695 spl_fstrans_unmark(cookie
);
697 #endif /* HAVE_BLK_QUEUE_DISCARD */
700 * Common read path running under the zvol taskq context. This function
701 * is responsible for copying the requested data out of the DMU and in to
702 * a linux request structure. It then must signal the request queue with
703 * an error code describing the result of the copy.
708 struct request
*req
= (struct request
*)arg
;
709 struct request_queue
*q
= req
->q
;
710 zvol_state_t
*zv
= q
->queuedata
;
711 fstrans_cookie_t cookie
= spl_fstrans_mark();
712 uint64_t offset
= blk_rq_pos(req
) << 9;
713 uint64_t size
= blk_rq_bytes(req
);
722 rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
724 error
= dmu_read_req(zv
->zv_objset
, ZVOL_OBJ
, req
);
726 zfs_range_unlock(rl
);
728 /* convert checksum errors into IO errors */
730 error
= SET_ERROR(EIO
);
733 blk_end_request(req
, -error
, size
);
734 spl_fstrans_unmark(cookie
);
738 * Request will be added back to the request queue and retried if
739 * it cannot be immediately dispatched to the taskq for handling
742 zvol_dispatch(task_func_t func
, struct request
*req
)
744 if (!taskq_dispatch(zvol_taskq
, func
, (void *)req
, TQ_NOSLEEP
))
745 blk_requeue_request(req
->q
, req
);
749 * Common request path. Rather than registering a custom make_request()
750 * function we use the generic Linux version. This is done because it allows
751 * us to easily merge read requests which would otherwise we performed
752 * synchronously by the DMU. This is less critical in write case where the
753 * DMU will perform the correct merging within a transaction group. Using
754 * the generic make_request() also let's use leverage the fact that the
755 * elevator with ensure correct ordering in regards to barrior IOs. On
756 * the downside it means that in the write case we end up doing request
757 * merging twice once in the elevator and once in the DMU.
759 * The request handler is called under a spin lock so all the real work
760 * is handed off to be done in the context of the zvol taskq. This function
761 * simply performs basic request sanity checking and hands off the request.
764 zvol_request(struct request_queue
*q
)
766 zvol_state_t
*zv
= q
->queuedata
;
770 while ((req
= blk_fetch_request(q
)) != NULL
) {
771 size
= blk_rq_bytes(req
);
773 if (size
!= 0 && blk_rq_pos(req
) + blk_rq_sectors(req
) >
774 get_capacity(zv
->zv_disk
)) {
776 "%s: bad access: block=%llu, count=%lu\n",
777 req
->rq_disk
->disk_name
,
778 (long long unsigned)blk_rq_pos(req
),
779 (long unsigned)blk_rq_sectors(req
));
780 __blk_end_request(req
, -EIO
, size
);
784 if (!blk_fs_request(req
)) {
785 printk(KERN_INFO
"%s: non-fs cmd\n",
786 req
->rq_disk
->disk_name
);
787 __blk_end_request(req
, -EIO
, size
);
791 switch ((int)rq_data_dir(req
)) {
793 zvol_dispatch(zvol_read
, req
);
796 if (unlikely(zv
->zv_flags
& ZVOL_RDONLY
)) {
797 __blk_end_request(req
, -EROFS
, size
);
801 #ifdef HAVE_BLK_QUEUE_DISCARD
802 if (req
->cmd_flags
& VDEV_REQ_DISCARD
) {
803 zvol_dispatch(zvol_discard
, req
);
806 #endif /* HAVE_BLK_QUEUE_DISCARD */
808 zvol_dispatch(zvol_write
, req
);
811 printk(KERN_INFO
"%s: unknown cmd: %d\n",
812 req
->rq_disk
->disk_name
, (int)rq_data_dir(req
));
813 __blk_end_request(req
, -EIO
, size
);
820 zvol_get_done(zgd_t
*zgd
, int error
)
823 dmu_buf_rele(zgd
->zgd_db
, zgd
);
825 zfs_range_unlock(zgd
->zgd_rl
);
827 if (error
== 0 && zgd
->zgd_bp
)
828 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
830 kmem_free(zgd
, sizeof (zgd_t
));
834 * Get data to generate a TX_WRITE intent log record.
837 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
839 zvol_state_t
*zv
= arg
;
840 objset_t
*os
= zv
->zv_objset
;
841 uint64_t object
= ZVOL_OBJ
;
842 uint64_t offset
= lr
->lr_offset
;
843 uint64_t size
= lr
->lr_length
;
844 blkptr_t
*bp
= &lr
->lr_blkptr
;
852 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
853 zgd
->zgd_zilog
= zv
->zv_zilog
;
854 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
857 * Write records come in two flavors: immediate and indirect.
858 * For small writes it's cheaper to store the data with the
859 * log record (immediate); for large writes it's cheaper to
860 * sync the data and get a pointer to it (indirect) so that
861 * we don't have to write the data twice.
863 if (buf
!= NULL
) { /* immediate write */
864 error
= dmu_read(os
, object
, offset
, size
, buf
,
865 DMU_READ_NO_PREFETCH
);
867 size
= zv
->zv_volblocksize
;
868 offset
= P2ALIGN_TYPED(offset
, size
, uint64_t);
869 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
870 DMU_READ_NO_PREFETCH
);
872 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
874 ASSERT(BP_IS_HOLE(bp
));
879 zgd
->zgd_bp
= &lr
->lr_blkptr
;
882 ASSERT(db
->db_offset
== offset
);
883 ASSERT(db
->db_size
== size
);
885 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
893 zvol_get_done(zgd
, error
);
895 return (SET_ERROR(error
));
899 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
902 zvol_insert(zvol_state_t
*zv_insert
)
904 zvol_state_t
*zv
= NULL
;
906 ASSERT(MUTEX_HELD(&zvol_state_lock
));
907 ASSERT3U(MINOR(zv_insert
->zv_dev
) & ZVOL_MINOR_MASK
, ==, 0);
908 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
;
909 zv
= list_next(&zvol_state_list
, zv
)) {
910 if (MINOR(zv
->zv_dev
) > MINOR(zv_insert
->zv_dev
))
914 list_insert_before(&zvol_state_list
, zv
, zv_insert
);
918 * Simply remove the zvol from to list of zvols.
921 zvol_remove(zvol_state_t
*zv_remove
)
923 ASSERT(MUTEX_HELD(&zvol_state_lock
));
924 list_remove(&zvol_state_list
, zv_remove
);
928 zvol_first_open(zvol_state_t
*zv
)
937 * In all other cases the spa_namespace_lock is taken before the
938 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
939 * function calls fops->open() with the bdev->bd_mutex lock held.
941 * To avoid a potential lock inversion deadlock we preemptively
942 * try to take the spa_namespace_lock(). Normally it will not
943 * be contended and this is safe because spa_open_common() handles
944 * the case where the caller already holds the spa_namespace_lock.
946 * When it is contended we risk a lock inversion if we were to
947 * block waiting for the lock. Luckily, the __blkdev_get()
948 * function allows us to return -ERESTARTSYS which will result in
949 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
950 * called again. This process can be repeated safely until both
951 * locks are acquired.
953 if (!mutex_owned(&spa_namespace_lock
)) {
954 locked
= mutex_tryenter(&spa_namespace_lock
);
956 return (-SET_ERROR(ERESTARTSYS
));
959 error
= dsl_prop_get_integer(zv
->zv_name
, "readonly", &ro
, NULL
);
963 /* lie and say we're read-only */
964 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, 1, zvol_tag
, &os
);
968 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
970 dmu_objset_disown(os
, zvol_tag
);
975 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
977 dmu_objset_disown(os
, zvol_tag
);
981 set_capacity(zv
->zv_disk
, volsize
>> 9);
982 zv
->zv_volsize
= volsize
;
983 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
985 if (ro
|| dmu_objset_is_snapshot(os
) ||
986 !spa_writeable(dmu_objset_spa(os
))) {
987 set_disk_ro(zv
->zv_disk
, 1);
988 zv
->zv_flags
|= ZVOL_RDONLY
;
990 set_disk_ro(zv
->zv_disk
, 0);
991 zv
->zv_flags
&= ~ZVOL_RDONLY
;
996 mutex_exit(&spa_namespace_lock
);
998 return (SET_ERROR(-error
));
1002 zvol_last_close(zvol_state_t
*zv
)
1004 zil_close(zv
->zv_zilog
);
1005 zv
->zv_zilog
= NULL
;
1007 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
1013 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
1014 !(zv
->zv_flags
& ZVOL_RDONLY
))
1015 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1016 (void) dmu_objset_evict_dbufs(zv
->zv_objset
);
1018 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
1019 zv
->zv_objset
= NULL
;
1023 zvol_open(struct block_device
*bdev
, fmode_t flag
)
1025 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1026 int error
= 0, drop_mutex
= 0;
1029 * If the caller is already holding the mutex do not take it
1030 * again, this will happen as part of zvol_create_minor().
1031 * Once add_disk() is called the device is live and the kernel
1032 * will attempt to open it to read the partition information.
1034 if (!mutex_owned(&zvol_state_lock
)) {
1035 mutex_enter(&zvol_state_lock
);
1039 ASSERT3P(zv
, !=, NULL
);
1041 if (zv
->zv_open_count
== 0) {
1042 error
= zvol_first_open(zv
);
1047 if ((flag
& FMODE_WRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1049 goto out_open_count
;
1052 zv
->zv_open_count
++;
1055 if (zv
->zv_open_count
== 0)
1056 zvol_last_close(zv
);
1060 mutex_exit(&zvol_state_lock
);
1062 check_disk_change(bdev
);
1064 return (SET_ERROR(error
));
1067 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1072 zvol_release(struct gendisk
*disk
, fmode_t mode
)
1074 zvol_state_t
*zv
= disk
->private_data
;
1077 if (!mutex_owned(&zvol_state_lock
)) {
1078 mutex_enter(&zvol_state_lock
);
1082 if (zv
->zv_open_count
> 0) {
1083 zv
->zv_open_count
--;
1084 if (zv
->zv_open_count
== 0)
1085 zvol_last_close(zv
);
1089 mutex_exit(&zvol_state_lock
);
1091 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1097 zvol_ioctl(struct block_device
*bdev
, fmode_t mode
,
1098 unsigned int cmd
, unsigned long arg
)
1100 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1104 return (SET_ERROR(-ENXIO
));
1108 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1111 error
= copy_to_user((void *)arg
, zv
->zv_name
, MAXNAMELEN
);
1120 return (SET_ERROR(error
));
1123 #ifdef CONFIG_COMPAT
1125 zvol_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1126 unsigned cmd
, unsigned long arg
)
1128 return (zvol_ioctl(bdev
, mode
, cmd
, arg
));
1131 #define zvol_compat_ioctl NULL
1134 static int zvol_media_changed(struct gendisk
*disk
)
1136 zvol_state_t
*zv
= disk
->private_data
;
1138 return (zv
->zv_changed
);
1141 static int zvol_revalidate_disk(struct gendisk
*disk
)
1143 zvol_state_t
*zv
= disk
->private_data
;
1146 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1152 * Provide a simple virtual geometry for legacy compatibility. For devices
1153 * smaller than 1 MiB a small head and sector count is used to allow very
1154 * tiny devices. For devices over 1 Mib a standard head and sector count
1155 * is used to keep the cylinders count reasonable.
1158 zvol_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1160 zvol_state_t
*zv
= bdev
->bd_disk
->private_data
;
1161 sector_t sectors
= get_capacity(zv
->zv_disk
);
1163 if (sectors
> 2048) {
1172 geo
->cylinders
= sectors
/ (geo
->heads
* geo
->sectors
);
1177 static struct kobject
*
1178 zvol_probe(dev_t dev
, int *part
, void *arg
)
1181 struct kobject
*kobj
;
1183 mutex_enter(&zvol_state_lock
);
1184 zv
= zvol_find_by_dev(dev
);
1185 kobj
= zv
? get_disk(zv
->zv_disk
) : NULL
;
1186 mutex_exit(&zvol_state_lock
);
1191 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1192 static struct block_device_operations zvol_ops
= {
1194 .release
= zvol_release
,
1195 .ioctl
= zvol_ioctl
,
1196 .compat_ioctl
= zvol_compat_ioctl
,
1197 .media_changed
= zvol_media_changed
,
1198 .revalidate_disk
= zvol_revalidate_disk
,
1199 .getgeo
= zvol_getgeo
,
1200 .owner
= THIS_MODULE
,
1203 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1206 zvol_open_by_inode(struct inode
*inode
, struct file
*file
)
1208 return (zvol_open(inode
->i_bdev
, file
->f_mode
));
1212 zvol_release_by_inode(struct inode
*inode
, struct file
*file
)
1214 return (zvol_release(inode
->i_bdev
->bd_disk
, file
->f_mode
));
1218 zvol_ioctl_by_inode(struct inode
*inode
, struct file
*file
,
1219 unsigned int cmd
, unsigned long arg
)
1221 if (file
== NULL
|| inode
== NULL
)
1222 return (SET_ERROR(-EINVAL
));
1224 return (zvol_ioctl(inode
->i_bdev
, file
->f_mode
, cmd
, arg
));
1227 #ifdef CONFIG_COMPAT
1229 zvol_compat_ioctl_by_inode(struct file
*file
,
1230 unsigned int cmd
, unsigned long arg
)
1233 return (SET_ERROR(-EINVAL
));
1235 return (zvol_compat_ioctl(file
->f_dentry
->d_inode
->i_bdev
,
1236 file
->f_mode
, cmd
, arg
));
1239 #define zvol_compat_ioctl_by_inode NULL
1242 static struct block_device_operations zvol_ops
= {
1243 .open
= zvol_open_by_inode
,
1244 .release
= zvol_release_by_inode
,
1245 .ioctl
= zvol_ioctl_by_inode
,
1246 .compat_ioctl
= zvol_compat_ioctl_by_inode
,
1247 .media_changed
= zvol_media_changed
,
1248 .revalidate_disk
= zvol_revalidate_disk
,
1249 .getgeo
= zvol_getgeo
,
1250 .owner
= THIS_MODULE
,
1252 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1255 * Allocate memory for a new zvol_state_t and setup the required
1256 * request queue and generic disk structures for the block device.
1258 static zvol_state_t
*
1259 zvol_alloc(dev_t dev
, const char *name
)
1264 zv
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
1266 spin_lock_init(&zv
->zv_lock
);
1267 list_link_init(&zv
->zv_next
);
1269 zv
->zv_queue
= blk_init_queue(zvol_request
, &zv
->zv_lock
);
1270 if (zv
->zv_queue
== NULL
)
1273 #ifdef HAVE_ELEVATOR_CHANGE
1274 error
= elevator_change(zv
->zv_queue
, "noop");
1275 #endif /* HAVE_ELEVATOR_CHANGE */
1277 printk("ZFS: Unable to set \"%s\" scheduler for zvol %s: %d\n",
1278 "noop", name
, error
);
1282 #ifdef HAVE_BLK_QUEUE_FLUSH
1283 blk_queue_flush(zv
->zv_queue
, VDEV_REQ_FLUSH
| VDEV_REQ_FUA
);
1285 blk_queue_ordered(zv
->zv_queue
, QUEUE_ORDERED_DRAIN
, NULL
);
1286 #endif /* HAVE_BLK_QUEUE_FLUSH */
1288 zv
->zv_disk
= alloc_disk(ZVOL_MINORS
);
1289 if (zv
->zv_disk
== NULL
)
1292 zv
->zv_queue
->queuedata
= zv
;
1294 zv
->zv_open_count
= 0;
1295 strlcpy(zv
->zv_name
, name
, MAXNAMELEN
);
1297 mutex_init(&zv
->zv_znode
.z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1298 avl_create(&zv
->zv_znode
.z_range_avl
, zfs_range_compare
,
1299 sizeof (rl_t
), offsetof(rl_t
, r_node
));
1300 zv
->zv_znode
.z_is_zvol
= TRUE
;
1302 zv
->zv_disk
->major
= zvol_major
;
1303 zv
->zv_disk
->first_minor
= (dev
& MINORMASK
);
1304 zv
->zv_disk
->fops
= &zvol_ops
;
1305 zv
->zv_disk
->private_data
= zv
;
1306 zv
->zv_disk
->queue
= zv
->zv_queue
;
1307 snprintf(zv
->zv_disk
->disk_name
, DISK_NAME_LEN
, "%s%d",
1308 ZVOL_DEV_NAME
, (dev
& MINORMASK
));
1313 blk_cleanup_queue(zv
->zv_queue
);
1315 kmem_free(zv
, sizeof (zvol_state_t
));
1321 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1324 zvol_free(zvol_state_t
*zv
)
1326 avl_destroy(&zv
->zv_znode
.z_range_avl
);
1327 mutex_destroy(&zv
->zv_znode
.z_range_lock
);
1329 del_gendisk(zv
->zv_disk
);
1330 blk_cleanup_queue(zv
->zv_queue
);
1331 put_disk(zv
->zv_disk
);
1333 kmem_free(zv
, sizeof (zvol_state_t
));
1337 __zvol_snapdev_hidden(const char *name
)
1344 parent
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1345 (void) strlcpy(parent
, name
, MAXPATHLEN
);
1347 if ((atp
= strrchr(parent
, '@')) != NULL
) {
1349 error
= dsl_prop_get_integer(parent
, "snapdev", &snapdev
, NULL
);
1350 if ((error
== 0) && (snapdev
== ZFS_SNAPDEV_HIDDEN
))
1351 error
= SET_ERROR(ENODEV
);
1354 kmem_free(parent
, MAXPATHLEN
);
1356 return (SET_ERROR(error
));
1360 __zvol_create_minor(const char *name
, boolean_t ignore_snapdev
)
1364 dmu_object_info_t
*doi
;
1369 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1371 zv
= zvol_find_by_name(name
);
1373 error
= SET_ERROR(EEXIST
);
1377 if (ignore_snapdev
== B_FALSE
) {
1378 error
= __zvol_snapdev_hidden(name
);
1383 doi
= kmem_alloc(sizeof (dmu_object_info_t
), KM_SLEEP
);
1385 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, zvol_tag
, &os
);
1389 error
= dmu_object_info(os
, ZVOL_OBJ
, doi
);
1391 goto out_dmu_objset_disown
;
1393 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
1395 goto out_dmu_objset_disown
;
1397 error
= zvol_find_minor(&minor
);
1399 goto out_dmu_objset_disown
;
1401 zv
= zvol_alloc(MKDEV(zvol_major
, minor
), name
);
1403 error
= SET_ERROR(EAGAIN
);
1404 goto out_dmu_objset_disown
;
1407 if (dmu_objset_is_snapshot(os
))
1408 zv
->zv_flags
|= ZVOL_RDONLY
;
1410 zv
->zv_volblocksize
= doi
->doi_data_block_size
;
1411 zv
->zv_volsize
= volsize
;
1414 set_capacity(zv
->zv_disk
, zv
->zv_volsize
>> 9);
1416 blk_queue_max_hw_sectors(zv
->zv_queue
, (DMU_MAX_ACCESS
/ 4) >> 9);
1417 blk_queue_max_segments(zv
->zv_queue
, UINT16_MAX
);
1418 blk_queue_max_segment_size(zv
->zv_queue
, UINT_MAX
);
1419 blk_queue_physical_block_size(zv
->zv_queue
, zv
->zv_volblocksize
);
1420 blk_queue_io_opt(zv
->zv_queue
, zv
->zv_volblocksize
);
1421 #ifdef HAVE_BLK_QUEUE_DISCARD
1422 blk_queue_max_discard_sectors(zv
->zv_queue
,
1423 (zvol_max_discard_blocks
* zv
->zv_volblocksize
) >> 9);
1424 blk_queue_discard_granularity(zv
->zv_queue
, zv
->zv_volblocksize
);
1425 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, zv
->zv_queue
);
1427 #ifdef HAVE_BLK_QUEUE_NONROT
1428 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, zv
->zv_queue
);
1431 if (spa_writeable(dmu_objset_spa(os
))) {
1432 if (zil_replay_disable
)
1433 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
1435 zil_replay(os
, zv
, zvol_replay_vector
);
1438 zv
->zv_objset
= NULL
;
1439 out_dmu_objset_disown
:
1440 dmu_objset_disown(os
, zvol_tag
);
1442 kmem_free(doi
, sizeof (dmu_object_info_t
));
1447 add_disk(zv
->zv_disk
);
1450 return (SET_ERROR(error
));
1454 * Create a block device minor node and setup the linkage between it
1455 * and the specified volume. Once this function returns the block
1456 * device is live and ready for use.
1459 zvol_create_minor(const char *name
)
1463 mutex_enter(&zvol_state_lock
);
1464 error
= __zvol_create_minor(name
, B_FALSE
);
1465 mutex_exit(&zvol_state_lock
);
1467 return (SET_ERROR(error
));
1471 __zvol_remove_minor(const char *name
)
1475 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1477 zv
= zvol_find_by_name(name
);
1479 return (SET_ERROR(ENXIO
));
1481 if (zv
->zv_open_count
> 0)
1482 return (SET_ERROR(EBUSY
));
1491 * Remove a block device minor node for the specified volume.
1494 zvol_remove_minor(const char *name
)
1498 mutex_enter(&zvol_state_lock
);
1499 error
= __zvol_remove_minor(name
);
1500 mutex_exit(&zvol_state_lock
);
1502 return (SET_ERROR(error
));
1506 * Rename a block device minor mode for the specified volume.
1509 __zvol_rename_minor(zvol_state_t
*zv
, const char *newname
)
1511 int readonly
= get_disk_ro(zv
->zv_disk
);
1513 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1515 strlcpy(zv
->zv_name
, newname
, sizeof (zv
->zv_name
));
1518 * The block device's read-only state is briefly changed causing
1519 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1520 * the name change and fixes the symlinks. This does not change
1521 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1522 * changes. This would normally be done using kobject_uevent() but
1523 * that is a GPL-only symbol which is why we need this workaround.
1525 set_disk_ro(zv
->zv_disk
, !readonly
);
1526 set_disk_ro(zv
->zv_disk
, readonly
);
1530 zvol_create_minors_cb(const char *dsname
, void *arg
)
1532 (void) zvol_create_minor(dsname
);
1538 * Create minors for specified dataset including children and snapshots.
1541 zvol_create_minors(const char *name
)
1545 if (!zvol_inhibit_dev
)
1546 error
= dmu_objset_find((char *)name
, zvol_create_minors_cb
,
1547 NULL
, DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
1549 return (SET_ERROR(error
));
1553 * Remove minors for specified dataset including children and snapshots.
1556 zvol_remove_minors(const char *name
)
1558 zvol_state_t
*zv
, *zv_next
;
1559 int namelen
= ((name
) ? strlen(name
) : 0);
1561 if (zvol_inhibit_dev
)
1564 mutex_enter(&zvol_state_lock
);
1566 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1567 zv_next
= list_next(&zvol_state_list
, zv
);
1569 if (name
== NULL
|| strcmp(zv
->zv_name
, name
) == 0 ||
1570 (strncmp(zv
->zv_name
, name
, namelen
) == 0 &&
1571 zv
->zv_name
[namelen
] == '/')) {
1577 mutex_exit(&zvol_state_lock
);
1581 * Rename minors for specified dataset including children and snapshots.
1584 zvol_rename_minors(const char *oldname
, const char *newname
)
1586 zvol_state_t
*zv
, *zv_next
;
1587 int oldnamelen
, newnamelen
;
1590 if (zvol_inhibit_dev
)
1593 oldnamelen
= strlen(oldname
);
1594 newnamelen
= strlen(newname
);
1595 name
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
1597 mutex_enter(&zvol_state_lock
);
1599 for (zv
= list_head(&zvol_state_list
); zv
!= NULL
; zv
= zv_next
) {
1600 zv_next
= list_next(&zvol_state_list
, zv
);
1602 if (strcmp(zv
->zv_name
, oldname
) == 0) {
1603 __zvol_rename_minor(zv
, newname
);
1604 } else if (strncmp(zv
->zv_name
, oldname
, oldnamelen
) == 0 &&
1605 (zv
->zv_name
[oldnamelen
] == '/' ||
1606 zv
->zv_name
[oldnamelen
] == '@')) {
1607 snprintf(name
, MAXNAMELEN
, "%s%c%s", newname
,
1608 zv
->zv_name
[oldnamelen
],
1609 zv
->zv_name
+ oldnamelen
+ 1);
1610 __zvol_rename_minor(zv
, name
);
1614 mutex_exit(&zvol_state_lock
);
1616 kmem_free(name
, MAXNAMELEN
);
1620 snapdev_snapshot_changed_cb(const char *dsname
, void *arg
) {
1621 uint64_t snapdev
= *(uint64_t *) arg
;
1623 if (strchr(dsname
, '@') == NULL
)
1627 case ZFS_SNAPDEV_VISIBLE
:
1628 mutex_enter(&zvol_state_lock
);
1629 (void) __zvol_create_minor(dsname
, B_TRUE
);
1630 mutex_exit(&zvol_state_lock
);
1632 case ZFS_SNAPDEV_HIDDEN
:
1633 (void) zvol_remove_minor(dsname
);
1641 zvol_set_snapdev(const char *dsname
, uint64_t snapdev
) {
1642 (void) dmu_objset_find((char *) dsname
, snapdev_snapshot_changed_cb
,
1643 &snapdev
, DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
1644 /* caller should continue to modify snapdev property */
1651 int threads
= MIN(MAX(zvol_threads
, 1), 1024);
1654 list_create(&zvol_state_list
, sizeof (zvol_state_t
),
1655 offsetof(zvol_state_t
, zv_next
));
1657 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1659 zvol_taskq
= taskq_create(ZVOL_DRIVER
, threads
, maxclsyspri
,
1660 threads
* 2, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
1661 if (zvol_taskq
== NULL
) {
1662 printk(KERN_INFO
"ZFS: taskq_create() failed\n");
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 taskq_destroy(zvol_taskq
);
1681 mutex_destroy(&zvol_state_lock
);
1682 list_destroy(&zvol_state_list
);
1684 return (SET_ERROR(error
));
1690 zvol_remove_minors(NULL
);
1691 blk_unregister_region(MKDEV(zvol_major
, 0), 1UL << MINORBITS
);
1692 unregister_blkdev(zvol_major
, ZVOL_DRIVER
);
1693 taskq_destroy(zvol_taskq
);
1694 mutex_destroy(&zvol_state_lock
);
1695 list_destroy(&zvol_state_list
);
1698 module_param(zvol_inhibit_dev
, uint
, 0644);
1699 MODULE_PARM_DESC(zvol_inhibit_dev
, "Do not create zvol device nodes");
1701 module_param(zvol_major
, uint
, 0444);
1702 MODULE_PARM_DESC(zvol_major
, "Major number for zvol device");
1704 module_param(zvol_threads
, uint
, 0444);
1705 MODULE_PARM_DESC(zvol_threads
, "Max number of threads to handle I/O requests");
1707 module_param(zvol_max_discard_blocks
, ulong
, 0444);
1708 MODULE_PARM_DESC(zvol_max_discard_blocks
, "Max number of blocks to discard");