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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2010 Robert Milkowski
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
33 * ZFS volume emulation driver.
35 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
36 * Volumes are accessed through the symbolic links named:
38 * /dev/zvol/dsk/<pool_name>/<dataset_name>
39 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
41 * These links are created by the /dev filesystem (sdev_zvolops.c).
42 * Volumes are persistent through reboot. No user command needs to be
43 * run before opening and using a device.
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/errno.h>
51 #include <sys/modctl.h>
55 #include <sys/cmn_err.h>
59 #include <sys/spa_impl.h>
61 #include <sys/dmu_traverse.h>
62 #include <sys/dnode.h>
63 #include <sys/dsl_dataset.h>
64 #include <sys/dsl_prop.h>
66 #include <sys/efi_partition.h>
67 #include <sys/byteorder.h>
68 #include <sys/pathname.h>
70 #include <sys/sunddi.h>
71 #include <sys/crc32.h>
72 #include <sys/dirent.h>
73 #include <sys/policy.h>
74 #include <sys/fs/zfs.h>
75 #include <sys/zfs_ioctl.h>
76 #include <sys/mkdev.h>
78 #include <sys/refcount.h>
79 #include <sys/zfs_znode.h>
80 #include <sys/zfs_rlock.h>
81 #include <sys/vdev_disk.h>
82 #include <sys/vdev_impl.h>
83 #include <sys/vdev_raidz.h>
85 #include <sys/dumphdr.h>
86 #include <sys/zil_impl.h>
88 #include <sys/dmu_tx.h>
89 #include <sys/zfeature.h>
90 #include <sys/zio_checksum.h>
92 #include "zfs_namecheck.h"
95 static char *zvol_tag
= "zvol_tag";
97 #define ZVOL_DUMPSIZE "dumpsize"
100 * This lock protects the zfsdev_state structure from being modified
101 * while it's being used, e.g. an open that comes in before a create
102 * finishes. It also protects temporary opens of the dataset so that,
103 * e.g., an open doesn't get a spurious EBUSY.
105 kmutex_t zfsdev_state_lock
;
106 static uint32_t zvol_minors
;
108 typedef struct zvol_extent
{
110 dva_t ze_dva
; /* dva associated with this extent */
111 uint64_t ze_nblks
; /* number of blocks in extent */
115 * The in-core state of each volume.
117 typedef struct zvol_state
{
118 char zv_name
[MAXPATHLEN
]; /* pool/dd name */
119 uint64_t zv_volsize
; /* amount of space we advertise */
120 uint64_t zv_volblocksize
; /* volume block size */
121 minor_t zv_minor
; /* minor number */
122 uint8_t zv_min_bs
; /* minimum addressable block shift */
123 uint8_t zv_flags
; /* readonly, dumpified, etc. */
124 objset_t
*zv_objset
; /* objset handle */
125 uint32_t zv_open_count
[OTYPCNT
]; /* open counts */
126 uint32_t zv_total_opens
; /* total open count */
127 zilog_t
*zv_zilog
; /* ZIL handle */
128 list_t zv_extents
; /* List of extents for dump */
129 znode_t zv_znode
; /* for range locking */
130 dmu_buf_t
*zv_dbuf
; /* bonus handle */
134 * zvol specific flags
136 #define ZVOL_RDONLY 0x1
137 #define ZVOL_DUMPIFIED 0x2
138 #define ZVOL_EXCL 0x4
142 * zvol maximum transfer in one DMU tx.
144 int zvol_maxphys
= DMU_MAX_ACCESS
/2;
147 * Toggle unmap functionality.
149 boolean_t zvol_unmap_enabled
= B_TRUE
;
152 * If true, unmaps requested as synchronous are executed synchronously,
153 * otherwise all unmaps are asynchronous.
155 boolean_t zvol_unmap_sync_enabled
= B_FALSE
;
157 extern int zfs_set_prop_nvlist(const char *, zprop_source_t
,
158 nvlist_t
*, nvlist_t
*);
159 static int zvol_remove_zv(zvol_state_t
*);
160 static int zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
);
161 static int zvol_dumpify(zvol_state_t
*zv
);
162 static int zvol_dump_fini(zvol_state_t
*zv
);
163 static int zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
);
166 zvol_size_changed(zvol_state_t
*zv
, uint64_t volsize
)
168 dev_t dev
= makedevice(ddi_driver_major(zfs_dip
), zv
->zv_minor
);
170 zv
->zv_volsize
= volsize
;
171 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
172 "Size", volsize
) == DDI_SUCCESS
);
173 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
174 "Nblocks", lbtodb(volsize
)) == DDI_SUCCESS
);
176 /* Notify specfs to invalidate the cached size */
177 spec_size_invalidate(dev
, VBLK
);
178 spec_size_invalidate(dev
, VCHR
);
182 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
185 return (SET_ERROR(EINVAL
));
187 if (volsize
% blocksize
!= 0)
188 return (SET_ERROR(EINVAL
));
191 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
192 return (SET_ERROR(EOVERFLOW
));
198 zvol_check_volblocksize(uint64_t volblocksize
)
200 if (volblocksize
< SPA_MINBLOCKSIZE
||
201 volblocksize
> SPA_OLD_MAXBLOCKSIZE
||
203 return (SET_ERROR(EDOM
));
209 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
212 dmu_object_info_t doi
;
215 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
219 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
221 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
224 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
225 doi
.doi_data_block_size
);
231 static zvol_state_t
*
232 zvol_minor_lookup(const char *name
)
237 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
239 for (minor
= 1; minor
<= ZFSDEV_MAX_MINOR
; minor
++) {
240 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
243 if (strcmp(zv
->zv_name
, name
) == 0)
250 /* extent mapping arg */
258 zvol_map_block(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
259 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
261 struct maparg
*ma
= arg
;
263 int bs
= ma
->ma_zv
->zv_volblocksize
;
265 if (bp
== NULL
|| BP_IS_HOLE(bp
) ||
266 zb
->zb_object
!= ZVOL_OBJ
|| zb
->zb_level
!= 0)
269 VERIFY(!BP_IS_EMBEDDED(bp
));
271 VERIFY3U(ma
->ma_blks
, ==, zb
->zb_blkid
);
274 /* Abort immediately if we have encountered gang blocks */
276 return (SET_ERROR(EFRAGS
));
279 * See if the block is at the end of the previous extent.
281 ze
= list_tail(&ma
->ma_zv
->zv_extents
);
283 DVA_GET_VDEV(BP_IDENTITY(bp
)) == DVA_GET_VDEV(&ze
->ze_dva
) &&
284 DVA_GET_OFFSET(BP_IDENTITY(bp
)) ==
285 DVA_GET_OFFSET(&ze
->ze_dva
) + ze
->ze_nblks
* bs
) {
290 dprintf_bp(bp
, "%s", "next blkptr:");
292 /* start a new extent */
293 ze
= kmem_zalloc(sizeof (zvol_extent_t
), KM_SLEEP
);
294 ze
->ze_dva
= bp
->blk_dva
[0]; /* structure assignment */
296 list_insert_tail(&ma
->ma_zv
->zv_extents
, ze
);
301 zvol_free_extents(zvol_state_t
*zv
)
305 while (ze
= list_head(&zv
->zv_extents
)) {
306 list_remove(&zv
->zv_extents
, ze
);
307 kmem_free(ze
, sizeof (zvol_extent_t
));
312 zvol_get_lbas(zvol_state_t
*zv
)
314 objset_t
*os
= zv
->zv_objset
;
320 zvol_free_extents(zv
);
322 /* commit any in-flight changes before traversing the dataset */
323 txg_wait_synced(dmu_objset_pool(os
), 0);
324 err
= traverse_dataset(dmu_objset_ds(os
), 0,
325 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
, zvol_map_block
, &ma
);
326 if (err
|| ma
.ma_blks
!= (zv
->zv_volsize
/ zv
->zv_volblocksize
)) {
327 zvol_free_extents(zv
);
328 return (err
? err
: EIO
);
336 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
338 zfs_creat_t
*zct
= arg
;
339 nvlist_t
*nvprops
= zct
->zct_props
;
341 uint64_t volblocksize
, volsize
;
343 VERIFY(nvlist_lookup_uint64(nvprops
,
344 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
345 if (nvlist_lookup_uint64(nvprops
,
346 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
347 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
350 * These properties must be removed from the list so the generic
351 * property setting step won't apply to them.
353 VERIFY(nvlist_remove_all(nvprops
,
354 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
355 (void) nvlist_remove_all(nvprops
,
356 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
358 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
362 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
366 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
371 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
372 * implement DKIOCFREE/free-long-range.
375 zvol_replay_truncate(zvol_state_t
*zv
, lr_truncate_t
*lr
, boolean_t byteswap
)
377 uint64_t offset
, length
;
380 byteswap_uint64_array(lr
, sizeof (*lr
));
382 offset
= lr
->lr_offset
;
383 length
= lr
->lr_length
;
385 return (dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, offset
, length
));
389 * Replay a TX_WRITE ZIL transaction that didn't get committed
390 * after a system failure
393 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
395 objset_t
*os
= zv
->zv_objset
;
396 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
397 uint64_t offset
, length
;
402 byteswap_uint64_array(lr
, sizeof (*lr
));
404 offset
= lr
->lr_offset
;
405 length
= lr
->lr_length
;
407 /* If it's a dmu_sync() block, write the whole block */
408 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
409 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
410 if (length
< blocksize
) {
411 offset
-= offset
% blocksize
;
416 tx
= dmu_tx_create(os
);
417 dmu_tx_hold_write(tx
, ZVOL_OBJ
, offset
, length
);
418 error
= dmu_tx_assign(tx
, TXG_WAIT
);
422 dmu_write(os
, ZVOL_OBJ
, offset
, length
, data
, tx
);
431 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
433 return (SET_ERROR(ENOTSUP
));
437 * Callback vectors for replaying records.
438 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
440 zil_replay_func_t
*zvol_replay_vector
[TX_MAX_TYPE
] = {
441 zvol_replay_err
, /* 0 no such transaction type */
442 zvol_replay_err
, /* TX_CREATE */
443 zvol_replay_err
, /* TX_MKDIR */
444 zvol_replay_err
, /* TX_MKXATTR */
445 zvol_replay_err
, /* TX_SYMLINK */
446 zvol_replay_err
, /* TX_REMOVE */
447 zvol_replay_err
, /* TX_RMDIR */
448 zvol_replay_err
, /* TX_LINK */
449 zvol_replay_err
, /* TX_RENAME */
450 zvol_replay_write
, /* TX_WRITE */
451 zvol_replay_truncate
, /* TX_TRUNCATE */
452 zvol_replay_err
, /* TX_SETATTR */
453 zvol_replay_err
, /* TX_ACL */
454 zvol_replay_err
, /* TX_CREATE_ACL */
455 zvol_replay_err
, /* TX_CREATE_ATTR */
456 zvol_replay_err
, /* TX_CREATE_ACL_ATTR */
457 zvol_replay_err
, /* TX_MKDIR_ACL */
458 zvol_replay_err
, /* TX_MKDIR_ATTR */
459 zvol_replay_err
, /* TX_MKDIR_ACL_ATTR */
460 zvol_replay_err
, /* TX_WRITE2 */
464 zvol_name2minor(const char *name
, minor_t
*minor
)
468 mutex_enter(&zfsdev_state_lock
);
469 zv
= zvol_minor_lookup(name
);
471 *minor
= zv
->zv_minor
;
472 mutex_exit(&zfsdev_state_lock
);
473 return (zv
? 0 : -1);
477 * Create a minor node (plus a whole lot more) for the specified volume.
480 zvol_create_minor(const char *name
)
482 zfs_soft_state_t
*zs
;
485 dmu_object_info_t doi
;
487 char chrbuf
[30], blkbuf
[30];
490 mutex_enter(&zfsdev_state_lock
);
492 if (zvol_minor_lookup(name
) != NULL
) {
493 mutex_exit(&zfsdev_state_lock
);
494 return (SET_ERROR(EEXIST
));
497 /* lie and say we're read-only */
498 error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_TRUE
, FTAG
, &os
);
501 mutex_exit(&zfsdev_state_lock
);
505 if ((minor
= zfsdev_minor_alloc()) == 0) {
506 dmu_objset_disown(os
, FTAG
);
507 mutex_exit(&zfsdev_state_lock
);
508 return (SET_ERROR(ENXIO
));
511 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
) {
512 dmu_objset_disown(os
, FTAG
);
513 mutex_exit(&zfsdev_state_lock
);
514 return (SET_ERROR(EAGAIN
));
516 (void) ddi_prop_update_string(minor
, zfs_dip
, ZVOL_PROP_NAME
,
519 (void) snprintf(chrbuf
, sizeof (chrbuf
), "%u,raw", minor
);
521 if (ddi_create_minor_node(zfs_dip
, chrbuf
, S_IFCHR
,
522 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
523 ddi_soft_state_free(zfsdev_state
, minor
);
524 dmu_objset_disown(os
, FTAG
);
525 mutex_exit(&zfsdev_state_lock
);
526 return (SET_ERROR(EAGAIN
));
529 (void) snprintf(blkbuf
, sizeof (blkbuf
), "%u", minor
);
531 if (ddi_create_minor_node(zfs_dip
, blkbuf
, S_IFBLK
,
532 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
533 ddi_remove_minor_node(zfs_dip
, chrbuf
);
534 ddi_soft_state_free(zfsdev_state
, minor
);
535 dmu_objset_disown(os
, FTAG
);
536 mutex_exit(&zfsdev_state_lock
);
537 return (SET_ERROR(EAGAIN
));
540 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
541 zs
->zss_type
= ZSST_ZVOL
;
542 zv
= zs
->zss_data
= kmem_zalloc(sizeof (zvol_state_t
), KM_SLEEP
);
543 (void) strlcpy(zv
->zv_name
, name
, MAXPATHLEN
);
544 zv
->zv_min_bs
= DEV_BSHIFT
;
545 zv
->zv_minor
= minor
;
547 if (dmu_objset_is_snapshot(os
) || !spa_writeable(dmu_objset_spa(os
)))
548 zv
->zv_flags
|= ZVOL_RDONLY
;
549 mutex_init(&zv
->zv_znode
.z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
550 avl_create(&zv
->zv_znode
.z_range_avl
, zfs_range_compare
,
551 sizeof (rl_t
), offsetof(rl_t
, r_node
));
552 list_create(&zv
->zv_extents
, sizeof (zvol_extent_t
),
553 offsetof(zvol_extent_t
, ze_node
));
554 /* get and cache the blocksize */
555 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
557 zv
->zv_volblocksize
= doi
.doi_data_block_size
;
559 if (spa_writeable(dmu_objset_spa(os
))) {
560 if (zil_replay_disable
)
561 zil_destroy(dmu_objset_zil(os
), B_FALSE
);
563 zil_replay(os
, zv
, zvol_replay_vector
);
565 dmu_objset_disown(os
, FTAG
);
566 zv
->zv_objset
= NULL
;
570 mutex_exit(&zfsdev_state_lock
);
576 * Remove minor node for the specified volume.
579 zvol_remove_zv(zvol_state_t
*zv
)
582 minor_t minor
= zv
->zv_minor
;
584 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
585 if (zv
->zv_total_opens
!= 0)
586 return (SET_ERROR(EBUSY
));
588 (void) snprintf(nmbuf
, sizeof (nmbuf
), "%u,raw", minor
);
589 ddi_remove_minor_node(zfs_dip
, nmbuf
);
591 (void) snprintf(nmbuf
, sizeof (nmbuf
), "%u", minor
);
592 ddi_remove_minor_node(zfs_dip
, nmbuf
);
594 avl_destroy(&zv
->zv_znode
.z_range_avl
);
595 mutex_destroy(&zv
->zv_znode
.z_range_lock
);
597 kmem_free(zv
, sizeof (zvol_state_t
));
599 ddi_soft_state_free(zfsdev_state
, minor
);
606 zvol_remove_minor(const char *name
)
611 mutex_enter(&zfsdev_state_lock
);
612 if ((zv
= zvol_minor_lookup(name
)) == NULL
) {
613 mutex_exit(&zfsdev_state_lock
);
614 return (SET_ERROR(ENXIO
));
616 rc
= zvol_remove_zv(zv
);
617 mutex_exit(&zfsdev_state_lock
);
622 zvol_first_open(zvol_state_t
*zv
)
629 /* lie and say we're read-only */
630 error
= dmu_objset_own(zv
->zv_name
, DMU_OST_ZVOL
, B_TRUE
,
636 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
639 dmu_objset_disown(os
, zvol_tag
);
643 error
= dmu_bonus_hold(os
, ZVOL_OBJ
, zvol_tag
, &zv
->zv_dbuf
);
645 dmu_objset_disown(os
, zvol_tag
);
649 zvol_size_changed(zv
, volsize
);
650 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
652 VERIFY(dsl_prop_get_integer(zv
->zv_name
, "readonly", &readonly
,
654 if (readonly
|| dmu_objset_is_snapshot(os
) ||
655 !spa_writeable(dmu_objset_spa(os
)))
656 zv
->zv_flags
|= ZVOL_RDONLY
;
658 zv
->zv_flags
&= ~ZVOL_RDONLY
;
663 zvol_last_close(zvol_state_t
*zv
)
665 zil_close(zv
->zv_zilog
);
668 dmu_buf_rele(zv
->zv_dbuf
, zvol_tag
);
674 if (dsl_dataset_is_dirty(dmu_objset_ds(zv
->zv_objset
)) &&
675 !(zv
->zv_flags
& ZVOL_RDONLY
))
676 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
677 dmu_objset_evict_dbufs(zv
->zv_objset
);
679 dmu_objset_disown(zv
->zv_objset
, zvol_tag
);
680 zv
->zv_objset
= NULL
;
684 zvol_prealloc(zvol_state_t
*zv
)
686 objset_t
*os
= zv
->zv_objset
;
688 uint64_t refd
, avail
, usedobjs
, availobjs
;
689 uint64_t resid
= zv
->zv_volsize
;
692 /* Check the space usage before attempting to allocate the space */
693 dmu_objset_space(os
, &refd
, &avail
, &usedobjs
, &availobjs
);
694 if (avail
< zv
->zv_volsize
)
695 return (SET_ERROR(ENOSPC
));
697 /* Free old extents if they exist */
698 zvol_free_extents(zv
);
702 uint64_t bytes
= MIN(resid
, SPA_OLD_MAXBLOCKSIZE
);
704 tx
= dmu_tx_create(os
);
705 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
706 error
= dmu_tx_assign(tx
, TXG_WAIT
);
709 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, off
);
712 dmu_prealloc(os
, ZVOL_OBJ
, off
, bytes
, tx
);
717 txg_wait_synced(dmu_objset_pool(os
), 0);
723 zvol_update_volsize(objset_t
*os
, uint64_t volsize
)
728 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
730 tx
= dmu_tx_create(os
);
731 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
732 dmu_tx_mark_netfree(tx
);
733 error
= dmu_tx_assign(tx
, TXG_WAIT
);
739 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1,
744 error
= dmu_free_long_range(os
,
745 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
750 zvol_remove_minors(const char *name
)
756 namebuf
= kmem_zalloc(strlen(name
) + 2, KM_SLEEP
);
757 (void) strncpy(namebuf
, name
, strlen(name
));
758 (void) strcat(namebuf
, "/");
759 mutex_enter(&zfsdev_state_lock
);
760 for (minor
= 1; minor
<= ZFSDEV_MAX_MINOR
; minor
++) {
762 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
765 if (strncmp(namebuf
, zv
->zv_name
, strlen(namebuf
)) == 0)
766 (void) zvol_remove_zv(zv
);
768 kmem_free(namebuf
, strlen(name
) + 2);
770 mutex_exit(&zfsdev_state_lock
);
774 zvol_update_live_volsize(zvol_state_t
*zv
, uint64_t volsize
)
776 uint64_t old_volsize
= 0ULL;
779 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
782 * Reinitialize the dump area to the new size. If we
783 * failed to resize the dump area then restore it back to
784 * its original size. We must set the new volsize prior
785 * to calling dumpvp_resize() to ensure that the devices'
786 * size(9P) is not visible by the dump subsystem.
788 old_volsize
= zv
->zv_volsize
;
789 zvol_size_changed(zv
, volsize
);
791 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
792 if ((error
= zvol_dumpify(zv
)) != 0 ||
793 (error
= dumpvp_resize()) != 0) {
796 (void) zvol_update_volsize(zv
->zv_objset
, old_volsize
);
797 zvol_size_changed(zv
, old_volsize
);
798 dumpify_error
= zvol_dumpify(zv
);
799 error
= dumpify_error
? dumpify_error
: error
;
804 * Generate a LUN expansion event.
809 char *physpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
811 (void) snprintf(physpath
, MAXPATHLEN
, "%s%u", ZVOL_PSEUDO_DEV
,
814 VERIFY(nvlist_alloc(&attr
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
815 VERIFY(nvlist_add_string(attr
, DEV_PHYS_PATH
, physpath
) == 0);
817 (void) ddi_log_sysevent(zfs_dip
, SUNW_VENDOR
, EC_DEV_STATUS
,
818 ESC_DEV_DLE
, attr
, &eid
, DDI_SLEEP
);
821 kmem_free(physpath
, MAXPATHLEN
);
827 zvol_set_volsize(const char *name
, uint64_t volsize
)
829 zvol_state_t
*zv
= NULL
;
832 dmu_object_info_t doi
;
834 boolean_t owned
= B_FALSE
;
836 error
= dsl_prop_get_integer(name
,
837 zfs_prop_to_name(ZFS_PROP_READONLY
), &readonly
, NULL
);
841 return (SET_ERROR(EROFS
));
843 mutex_enter(&zfsdev_state_lock
);
844 zv
= zvol_minor_lookup(name
);
846 if (zv
== NULL
|| zv
->zv_objset
== NULL
) {
847 if ((error
= dmu_objset_own(name
, DMU_OST_ZVOL
, B_FALSE
,
849 mutex_exit(&zfsdev_state_lock
);
859 if ((error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
)) != 0 ||
860 (error
= zvol_check_volsize(volsize
, doi
.doi_data_block_size
)) != 0)
863 error
= zvol_update_volsize(os
, volsize
);
865 if (error
== 0 && zv
!= NULL
)
866 error
= zvol_update_live_volsize(zv
, volsize
);
869 dmu_objset_disown(os
, FTAG
);
871 zv
->zv_objset
= NULL
;
873 mutex_exit(&zfsdev_state_lock
);
879 zvol_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
884 mutex_enter(&zfsdev_state_lock
);
886 zv
= zfsdev_get_soft_state(getminor(*devp
), ZSST_ZVOL
);
888 mutex_exit(&zfsdev_state_lock
);
889 return (SET_ERROR(ENXIO
));
892 if (zv
->zv_total_opens
== 0)
893 err
= zvol_first_open(zv
);
895 mutex_exit(&zfsdev_state_lock
);
898 if ((flag
& FWRITE
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
899 err
= SET_ERROR(EROFS
);
902 if (zv
->zv_flags
& ZVOL_EXCL
) {
903 err
= SET_ERROR(EBUSY
);
907 if (zv
->zv_total_opens
!= 0) {
908 err
= SET_ERROR(EBUSY
);
911 zv
->zv_flags
|= ZVOL_EXCL
;
914 if (zv
->zv_open_count
[otyp
] == 0 || otyp
== OTYP_LYR
) {
915 zv
->zv_open_count
[otyp
]++;
916 zv
->zv_total_opens
++;
918 mutex_exit(&zfsdev_state_lock
);
922 if (zv
->zv_total_opens
== 0)
924 mutex_exit(&zfsdev_state_lock
);
930 zvol_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
932 minor_t minor
= getminor(dev
);
936 mutex_enter(&zfsdev_state_lock
);
938 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
940 mutex_exit(&zfsdev_state_lock
);
941 return (SET_ERROR(ENXIO
));
944 if (zv
->zv_flags
& ZVOL_EXCL
) {
945 ASSERT(zv
->zv_total_opens
== 1);
946 zv
->zv_flags
&= ~ZVOL_EXCL
;
950 * If the open count is zero, this is a spurious close.
951 * That indicates a bug in the kernel / DDI framework.
953 ASSERT(zv
->zv_open_count
[otyp
] != 0);
954 ASSERT(zv
->zv_total_opens
!= 0);
957 * You may get multiple opens, but only one close.
959 zv
->zv_open_count
[otyp
]--;
960 zv
->zv_total_opens
--;
962 if (zv
->zv_total_opens
== 0)
965 mutex_exit(&zfsdev_state_lock
);
970 zvol_get_done(zgd_t
*zgd
, int error
)
973 dmu_buf_rele(zgd
->zgd_db
, zgd
);
975 zfs_range_unlock(zgd
->zgd_rl
);
977 if (error
== 0 && zgd
->zgd_bp
)
978 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
980 kmem_free(zgd
, sizeof (zgd_t
));
984 * Get data to generate a TX_WRITE intent log record.
987 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
989 zvol_state_t
*zv
= arg
;
990 objset_t
*os
= zv
->zv_objset
;
991 uint64_t object
= ZVOL_OBJ
;
992 uint64_t offset
= lr
->lr_offset
;
993 uint64_t size
= lr
->lr_length
; /* length of user data */
1001 zgd
= kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
1002 zgd
->zgd_zilog
= zv
->zv_zilog
;
1003 zgd
->zgd_rl
= zfs_range_lock(&zv
->zv_znode
, offset
, size
, RL_READER
);
1006 * Write records come in two flavors: immediate and indirect.
1007 * For small writes it's cheaper to store the data with the
1008 * log record (immediate); for large writes it's cheaper to
1009 * sync the data and get a pointer to it (indirect) so that
1010 * we don't have to write the data twice.
1012 if (buf
!= NULL
) { /* immediate write */
1013 error
= dmu_read(os
, object
, offset
, size
, buf
,
1014 DMU_READ_NO_PREFETCH
);
1016 size
= zv
->zv_volblocksize
;
1017 offset
= P2ALIGN(offset
, size
);
1018 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1019 DMU_READ_NO_PREFETCH
);
1021 blkptr_t
*bp
= &lr
->lr_blkptr
;
1026 ASSERT(db
->db_offset
== offset
);
1027 ASSERT(db
->db_size
== size
);
1029 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1030 zvol_get_done
, zgd
);
1037 zvol_get_done(zgd
, error
);
1043 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1045 * We store data in the log buffers if it's small enough.
1046 * Otherwise we will later flush the data out via dmu_sync().
1048 ssize_t zvol_immediate_write_sz
= 32768;
1051 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, offset_t off
, ssize_t resid
,
1054 uint32_t blocksize
= zv
->zv_volblocksize
;
1055 zilog_t
*zilog
= zv
->zv_zilog
;
1056 itx_wr_state_t write_state
;
1058 if (zil_replaying(zilog
, tx
))
1061 if (zilog
->zl_logbias
== ZFS_LOGBIAS_THROUGHPUT
)
1062 write_state
= WR_INDIRECT
;
1063 else if (!spa_has_slogs(zilog
->zl_spa
) &&
1064 resid
>= blocksize
&& blocksize
> zvol_immediate_write_sz
)
1065 write_state
= WR_INDIRECT
;
1067 write_state
= WR_COPIED
;
1069 write_state
= WR_NEED_COPY
;
1074 itx_wr_state_t wr_state
= write_state
;
1075 ssize_t len
= resid
;
1077 if (wr_state
== WR_COPIED
&& resid
> ZIL_MAX_COPIED_DATA
)
1078 wr_state
= WR_NEED_COPY
;
1079 else if (wr_state
== WR_INDIRECT
)
1080 len
= MIN(blocksize
- P2PHASE(off
, blocksize
), resid
);
1082 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
) +
1083 (wr_state
== WR_COPIED
? len
: 0));
1084 lr
= (lr_write_t
*)&itx
->itx_lr
;
1085 if (wr_state
== WR_COPIED
&& dmu_read(zv
->zv_objset
,
1086 ZVOL_OBJ
, off
, len
, lr
+ 1, DMU_READ_NO_PREFETCH
) != 0) {
1087 zil_itx_destroy(itx
);
1088 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1089 lr
= (lr_write_t
*)&itx
->itx_lr
;
1090 wr_state
= WR_NEED_COPY
;
1093 itx
->itx_wr_state
= wr_state
;
1094 lr
->lr_foid
= ZVOL_OBJ
;
1095 lr
->lr_offset
= off
;
1096 lr
->lr_length
= len
;
1098 BP_ZERO(&lr
->lr_blkptr
);
1100 itx
->itx_private
= zv
;
1101 itx
->itx_sync
= sync
;
1103 zil_itx_assign(zilog
, itx
, tx
);
1111 zvol_dumpio_vdev(vdev_t
*vd
, void *addr
, uint64_t offset
, uint64_t origoffset
,
1112 uint64_t size
, boolean_t doread
, boolean_t isdump
)
1118 if (vd
->vdev_ops
== &vdev_mirror_ops
||
1119 vd
->vdev_ops
== &vdev_replacing_ops
||
1120 vd
->vdev_ops
== &vdev_spare_ops
) {
1121 for (c
= 0; c
< vd
->vdev_children
; c
++) {
1122 int err
= zvol_dumpio_vdev(vd
->vdev_child
[c
],
1123 addr
, offset
, origoffset
, size
, doread
, isdump
);
1126 } else if (doread
) {
1132 if (!vd
->vdev_ops
->vdev_op_leaf
&& vd
->vdev_ops
!= &vdev_raidz_ops
)
1133 return (numerrors
< vd
->vdev_children
? 0 : EIO
);
1135 if (doread
&& !vdev_readable(vd
))
1136 return (SET_ERROR(EIO
));
1137 else if (!doread
&& !vdev_writeable(vd
))
1138 return (SET_ERROR(EIO
));
1140 if (vd
->vdev_ops
== &vdev_raidz_ops
) {
1141 return (vdev_raidz_physio(vd
,
1142 addr
, size
, offset
, origoffset
, doread
, isdump
));
1145 offset
+= VDEV_LABEL_START_SIZE
;
1147 if (ddi_in_panic() || isdump
) {
1150 return (SET_ERROR(EIO
));
1152 ASSERT3P(dvd
, !=, NULL
);
1153 return (ldi_dump(dvd
->vd_lh
, addr
, lbtodb(offset
),
1157 ASSERT3P(dvd
, !=, NULL
);
1158 return (vdev_disk_ldi_physio(dvd
->vd_lh
, addr
, size
,
1159 offset
, doread
? B_READ
: B_WRITE
));
1164 zvol_dumpio(zvol_state_t
*zv
, void *addr
, uint64_t offset
, uint64_t size
,
1165 boolean_t doread
, boolean_t isdump
)
1170 spa_t
*spa
= dmu_objset_spa(zv
->zv_objset
);
1172 /* Must be sector aligned, and not stradle a block boundary. */
1173 if (P2PHASE(offset
, DEV_BSIZE
) || P2PHASE(size
, DEV_BSIZE
) ||
1174 P2BOUNDARY(offset
, size
, zv
->zv_volblocksize
)) {
1175 return (SET_ERROR(EINVAL
));
1177 ASSERT(size
<= zv
->zv_volblocksize
);
1179 /* Locate the extent this belongs to */
1180 ze
= list_head(&zv
->zv_extents
);
1181 while (offset
>= ze
->ze_nblks
* zv
->zv_volblocksize
) {
1182 offset
-= ze
->ze_nblks
* zv
->zv_volblocksize
;
1183 ze
= list_next(&zv
->zv_extents
, ze
);
1187 return (SET_ERROR(EINVAL
));
1189 if (!ddi_in_panic())
1190 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
1192 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&ze
->ze_dva
));
1193 offset
+= DVA_GET_OFFSET(&ze
->ze_dva
);
1194 error
= zvol_dumpio_vdev(vd
, addr
, offset
, DVA_GET_OFFSET(&ze
->ze_dva
),
1195 size
, doread
, isdump
);
1197 if (!ddi_in_panic())
1198 spa_config_exit(spa
, SCL_STATE
, FTAG
);
1204 zvol_strategy(buf_t
*bp
)
1206 zfs_soft_state_t
*zs
= NULL
;
1208 uint64_t off
, volsize
;
1214 boolean_t doread
= bp
->b_flags
& B_READ
;
1215 boolean_t is_dumpified
;
1218 if (getminor(bp
->b_edev
) == 0) {
1219 error
= SET_ERROR(EINVAL
);
1221 zs
= ddi_get_soft_state(zfsdev_state
, getminor(bp
->b_edev
));
1223 error
= SET_ERROR(ENXIO
);
1224 else if (zs
->zss_type
!= ZSST_ZVOL
)
1225 error
= SET_ERROR(EINVAL
);
1229 bioerror(bp
, error
);
1236 if (!(bp
->b_flags
& B_READ
) && (zv
->zv_flags
& ZVOL_RDONLY
)) {
1237 bioerror(bp
, EROFS
);
1242 off
= ldbtob(bp
->b_blkno
);
1243 volsize
= zv
->zv_volsize
;
1249 addr
= bp
->b_un
.b_addr
;
1250 resid
= bp
->b_bcount
;
1252 if (resid
> 0 && (off
< 0 || off
>= volsize
)) {
1258 is_dumpified
= zv
->zv_flags
& ZVOL_DUMPIFIED
;
1259 sync
= ((!(bp
->b_flags
& B_ASYNC
) &&
1260 !(zv
->zv_flags
& ZVOL_WCE
)) ||
1261 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
)) &&
1262 !doread
&& !is_dumpified
;
1265 * There must be no buffer changes when doing a dmu_sync() because
1266 * we can't change the data whilst calculating the checksum.
1268 rl
= zfs_range_lock(&zv
->zv_znode
, off
, resid
,
1269 doread
? RL_READER
: RL_WRITER
);
1271 while (resid
!= 0 && off
< volsize
) {
1272 size_t size
= MIN(resid
, zvol_maxphys
);
1274 size
= MIN(size
, P2END(off
, zv
->zv_volblocksize
) - off
);
1275 error
= zvol_dumpio(zv
, addr
, off
, size
,
1277 } else if (doread
) {
1278 error
= dmu_read(os
, ZVOL_OBJ
, off
, size
, addr
,
1281 dmu_tx_t
*tx
= dmu_tx_create(os
);
1282 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, size
);
1283 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1287 dmu_write(os
, ZVOL_OBJ
, off
, size
, addr
, tx
);
1288 zvol_log_write(zv
, tx
, off
, size
, sync
);
1293 /* convert checksum errors into IO errors */
1294 if (error
== ECKSUM
)
1295 error
= SET_ERROR(EIO
);
1302 zfs_range_unlock(rl
);
1304 if ((bp
->b_resid
= resid
) == bp
->b_bcount
)
1305 bioerror(bp
, off
> volsize
? EINVAL
: error
);
1308 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1315 * Set the buffer count to the zvol maximum transfer.
1316 * Using our own routine instead of the default minphys()
1317 * means that for larger writes we write bigger buffers on X86
1318 * (128K instead of 56K) and flush the disk write cache less often
1319 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1320 * 56K on X86 and 128K on sparc).
1323 zvol_minphys(struct buf
*bp
)
1325 if (bp
->b_bcount
> zvol_maxphys
)
1326 bp
->b_bcount
= zvol_maxphys
;
1330 zvol_dump(dev_t dev
, caddr_t addr
, daddr_t blkno
, int nblocks
)
1332 minor_t minor
= getminor(dev
);
1339 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1341 return (SET_ERROR(ENXIO
));
1343 if ((zv
->zv_flags
& ZVOL_DUMPIFIED
) == 0)
1344 return (SET_ERROR(EINVAL
));
1346 boff
= ldbtob(blkno
);
1347 resid
= ldbtob(nblocks
);
1349 VERIFY3U(boff
+ resid
, <=, zv
->zv_volsize
);
1352 size
= MIN(resid
, P2END(boff
, zv
->zv_volblocksize
) - boff
);
1353 error
= zvol_dumpio(zv
, addr
, boff
, size
, B_FALSE
, B_TRUE
);
1366 zvol_read(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1368 minor_t minor
= getminor(dev
);
1374 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1376 return (SET_ERROR(ENXIO
));
1378 volsize
= zv
->zv_volsize
;
1379 if (uio
->uio_resid
> 0 &&
1380 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1381 return (SET_ERROR(EIO
));
1383 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1384 error
= physio(zvol_strategy
, NULL
, dev
, B_READ
,
1389 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1391 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1392 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1394 /* don't read past the end */
1395 if (bytes
> volsize
- uio
->uio_loffset
)
1396 bytes
= volsize
- uio
->uio_loffset
;
1398 error
= dmu_read_uio(zv
->zv_objset
, ZVOL_OBJ
, uio
, bytes
);
1400 /* convert checksum errors into IO errors */
1401 if (error
== ECKSUM
)
1402 error
= SET_ERROR(EIO
);
1406 zfs_range_unlock(rl
);
1412 zvol_write(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1414 minor_t minor
= getminor(dev
);
1421 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1423 return (SET_ERROR(ENXIO
));
1425 volsize
= zv
->zv_volsize
;
1426 if (uio
->uio_resid
> 0 &&
1427 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1428 return (SET_ERROR(EIO
));
1430 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1431 error
= physio(zvol_strategy
, NULL
, dev
, B_WRITE
,
1436 sync
= !(zv
->zv_flags
& ZVOL_WCE
) ||
1437 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
);
1439 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1441 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1442 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1443 uint64_t off
= uio
->uio_loffset
;
1444 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
1446 if (bytes
> volsize
- off
) /* don't write past the end */
1447 bytes
= volsize
- off
;
1449 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
1450 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1455 error
= dmu_write_uio_dbuf(zv
->zv_dbuf
, uio
, bytes
, tx
);
1457 zvol_log_write(zv
, tx
, off
, bytes
, sync
);
1463 zfs_range_unlock(rl
);
1465 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1470 zvol_getefi(void *arg
, int flag
, uint64_t vs
, uint8_t bs
)
1472 struct uuid uuid
= EFI_RESERVED
;
1473 efi_gpe_t gpe
= { 0 };
1479 if (ddi_copyin(arg
, &efi
, sizeof (dk_efi_t
), flag
))
1480 return (SET_ERROR(EFAULT
));
1481 ptr
= (char *)(uintptr_t)efi
.dki_data_64
;
1482 length
= efi
.dki_length
;
1484 * Some clients may attempt to request a PMBR for the
1485 * zvol. Currently this interface will return EINVAL to
1486 * such requests. These requests could be supported by
1487 * adding a check for lba == 0 and consing up an appropriate
1490 if (efi
.dki_lba
< 1 || efi
.dki_lba
> 2 || length
<= 0)
1491 return (SET_ERROR(EINVAL
));
1493 gpe
.efi_gpe_StartingLBA
= LE_64(34ULL);
1494 gpe
.efi_gpe_EndingLBA
= LE_64((vs
>> bs
) - 1);
1495 UUID_LE_CONVERT(gpe
.efi_gpe_PartitionTypeGUID
, uuid
);
1497 if (efi
.dki_lba
== 1) {
1498 efi_gpt_t gpt
= { 0 };
1500 gpt
.efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1501 gpt
.efi_gpt_Revision
= LE_32(EFI_VERSION_CURRENT
);
1502 gpt
.efi_gpt_HeaderSize
= LE_32(sizeof (gpt
));
1503 gpt
.efi_gpt_MyLBA
= LE_64(1ULL);
1504 gpt
.efi_gpt_FirstUsableLBA
= LE_64(34ULL);
1505 gpt
.efi_gpt_LastUsableLBA
= LE_64((vs
>> bs
) - 1);
1506 gpt
.efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1507 gpt
.efi_gpt_NumberOfPartitionEntries
= LE_32(1);
1508 gpt
.efi_gpt_SizeOfPartitionEntry
=
1509 LE_32(sizeof (efi_gpe_t
));
1510 CRC32(crc
, &gpe
, sizeof (gpe
), -1U, crc32_table
);
1511 gpt
.efi_gpt_PartitionEntryArrayCRC32
= LE_32(~crc
);
1512 CRC32(crc
, &gpt
, sizeof (gpt
), -1U, crc32_table
);
1513 gpt
.efi_gpt_HeaderCRC32
= LE_32(~crc
);
1514 if (ddi_copyout(&gpt
, ptr
, MIN(sizeof (gpt
), length
),
1516 return (SET_ERROR(EFAULT
));
1517 ptr
+= sizeof (gpt
);
1518 length
-= sizeof (gpt
);
1520 if (length
> 0 && ddi_copyout(&gpe
, ptr
, MIN(sizeof (gpe
),
1522 return (SET_ERROR(EFAULT
));
1527 * BEGIN entry points to allow external callers access to the volume.
1530 * Return the volume parameters needed for access from an external caller.
1531 * These values are invariant as long as the volume is held open.
1534 zvol_get_volume_params(minor_t minor
, uint64_t *blksize
,
1535 uint64_t *max_xfer_len
, void **minor_hdl
, void **objset_hdl
, void **zil_hdl
,
1536 void **rl_hdl
, void **bonus_hdl
)
1540 zv
= zfsdev_get_soft_state(minor
, ZSST_ZVOL
);
1542 return (SET_ERROR(ENXIO
));
1543 if (zv
->zv_flags
& ZVOL_DUMPIFIED
)
1544 return (SET_ERROR(ENXIO
));
1546 ASSERT(blksize
&& max_xfer_len
&& minor_hdl
&&
1547 objset_hdl
&& zil_hdl
&& rl_hdl
&& bonus_hdl
);
1549 *blksize
= zv
->zv_volblocksize
;
1550 *max_xfer_len
= (uint64_t)zvol_maxphys
;
1552 *objset_hdl
= zv
->zv_objset
;
1553 *zil_hdl
= zv
->zv_zilog
;
1554 *rl_hdl
= &zv
->zv_znode
;
1555 *bonus_hdl
= zv
->zv_dbuf
;
1560 * Return the current volume size to an external caller.
1561 * The size can change while the volume is open.
1564 zvol_get_volume_size(void *minor_hdl
)
1566 zvol_state_t
*zv
= minor_hdl
;
1568 return (zv
->zv_volsize
);
1572 * Return the current WCE setting to an external caller.
1573 * The WCE setting can change while the volume is open.
1576 zvol_get_volume_wce(void *minor_hdl
)
1578 zvol_state_t
*zv
= minor_hdl
;
1580 return ((zv
->zv_flags
& ZVOL_WCE
) ? 1 : 0);
1584 * Entry point for external callers to zvol_log_write
1587 zvol_log_write_minor(void *minor_hdl
, dmu_tx_t
*tx
, offset_t off
, ssize_t resid
,
1590 zvol_state_t
*zv
= minor_hdl
;
1592 zvol_log_write(zv
, tx
, off
, resid
, sync
);
1595 * END entry points to allow external callers access to the volume.
1599 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1602 zvol_log_truncate(zvol_state_t
*zv
, dmu_tx_t
*tx
, uint64_t off
, uint64_t len
,
1607 zilog_t
*zilog
= zv
->zv_zilog
;
1609 if (zil_replaying(zilog
, tx
))
1612 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1613 lr
= (lr_truncate_t
*)&itx
->itx_lr
;
1614 lr
->lr_foid
= ZVOL_OBJ
;
1615 lr
->lr_offset
= off
;
1616 lr
->lr_length
= len
;
1618 itx
->itx_sync
= sync
;
1619 zil_itx_assign(zilog
, itx
, tx
);
1623 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1624 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1628 zvol_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
1631 struct dk_callback
*dkc
;
1635 mutex_enter(&zfsdev_state_lock
);
1637 zv
= zfsdev_get_soft_state(getminor(dev
), ZSST_ZVOL
);
1640 mutex_exit(&zfsdev_state_lock
);
1641 return (SET_ERROR(ENXIO
));
1643 ASSERT(zv
->zv_total_opens
> 0);
1649 struct dk_cinfo dki
;
1651 bzero(&dki
, sizeof (dki
));
1652 (void) strcpy(dki
.dki_cname
, "zvol");
1653 (void) strcpy(dki
.dki_dname
, "zvol");
1654 dki
.dki_ctype
= DKC_UNKNOWN
;
1655 dki
.dki_unit
= getminor(dev
);
1656 dki
.dki_maxtransfer
=
1657 1 << (SPA_OLD_MAXBLOCKSHIFT
- zv
->zv_min_bs
);
1658 mutex_exit(&zfsdev_state_lock
);
1659 if (ddi_copyout(&dki
, (void *)arg
, sizeof (dki
), flag
))
1660 error
= SET_ERROR(EFAULT
);
1664 case DKIOCGMEDIAINFO
:
1666 struct dk_minfo dkm
;
1668 bzero(&dkm
, sizeof (dkm
));
1669 dkm
.dki_lbsize
= 1U << zv
->zv_min_bs
;
1670 dkm
.dki_capacity
= zv
->zv_volsize
>> zv
->zv_min_bs
;
1671 dkm
.dki_media_type
= DK_UNKNOWN
;
1672 mutex_exit(&zfsdev_state_lock
);
1673 if (ddi_copyout(&dkm
, (void *)arg
, sizeof (dkm
), flag
))
1674 error
= SET_ERROR(EFAULT
);
1678 case DKIOCGMEDIAINFOEXT
:
1680 struct dk_minfo_ext dkmext
;
1682 bzero(&dkmext
, sizeof (dkmext
));
1683 dkmext
.dki_lbsize
= 1U << zv
->zv_min_bs
;
1684 dkmext
.dki_pbsize
= zv
->zv_volblocksize
;
1685 dkmext
.dki_capacity
= zv
->zv_volsize
>> zv
->zv_min_bs
;
1686 dkmext
.dki_media_type
= DK_UNKNOWN
;
1687 mutex_exit(&zfsdev_state_lock
);
1688 if (ddi_copyout(&dkmext
, (void *)arg
, sizeof (dkmext
), flag
))
1689 error
= SET_ERROR(EFAULT
);
1695 uint64_t vs
= zv
->zv_volsize
;
1696 uint8_t bs
= zv
->zv_min_bs
;
1698 mutex_exit(&zfsdev_state_lock
);
1699 error
= zvol_getefi((void *)arg
, flag
, vs
, bs
);
1703 case DKIOCFLUSHWRITECACHE
:
1704 dkc
= (struct dk_callback
*)arg
;
1705 mutex_exit(&zfsdev_state_lock
);
1706 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1707 if ((flag
& FKIOCTL
) && dkc
!= NULL
&& dkc
->dkc_callback
) {
1708 (*dkc
->dkc_callback
)(dkc
->dkc_cookie
, error
);
1715 int wce
= (zv
->zv_flags
& ZVOL_WCE
) ? 1 : 0;
1716 if (ddi_copyout(&wce
, (void *)arg
, sizeof (int),
1718 error
= SET_ERROR(EFAULT
);
1724 if (ddi_copyin((void *)arg
, &wce
, sizeof (int),
1726 error
= SET_ERROR(EFAULT
);
1730 zv
->zv_flags
|= ZVOL_WCE
;
1731 mutex_exit(&zfsdev_state_lock
);
1733 zv
->zv_flags
&= ~ZVOL_WCE
;
1734 mutex_exit(&zfsdev_state_lock
);
1735 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1743 * commands using these (like prtvtoc) expect ENOTSUP
1744 * since we're emulating an EFI label
1746 error
= SET_ERROR(ENOTSUP
);
1750 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1752 error
= zvol_dumpify(zv
);
1753 zfs_range_unlock(rl
);
1757 if (!(zv
->zv_flags
& ZVOL_DUMPIFIED
))
1759 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1761 error
= zvol_dump_fini(zv
);
1762 zfs_range_unlock(rl
);
1770 if (!zvol_unmap_enabled
)
1773 if (ddi_copyin((void *)arg
, &df
, sizeof (df
), flag
)) {
1774 error
= SET_ERROR(EFAULT
);
1779 * Apply Postel's Law to length-checking. If they overshoot,
1780 * just blank out until the end, if there's a need to blank
1783 if (df
.df_start
>= zv
->zv_volsize
)
1784 break; /* No need to do anything... */
1786 mutex_exit(&zfsdev_state_lock
);
1788 rl
= zfs_range_lock(&zv
->zv_znode
, df
.df_start
, df
.df_length
,
1790 tx
= dmu_tx_create(zv
->zv_objset
);
1791 dmu_tx_mark_netfree(tx
);
1792 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1796 zvol_log_truncate(zv
, tx
, df
.df_start
,
1797 df
.df_length
, B_TRUE
);
1799 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
,
1800 df
.df_start
, df
.df_length
);
1803 zfs_range_unlock(rl
);
1806 * If the write-cache is disabled, 'sync' property
1807 * is set to 'always', or if the caller is asking for
1808 * a synchronous free, commit this operation to the zil.
1809 * This will sync any previous uncommitted writes to the
1811 * Can be overridden by the zvol_unmap_sync_enabled tunable.
1813 if ((error
== 0) && zvol_unmap_sync_enabled
&&
1814 (!(zv
->zv_flags
& ZVOL_WCE
) ||
1815 (zv
->zv_objset
->os_sync
== ZFS_SYNC_ALWAYS
) ||
1816 (df
.df_flags
& DF_WAIT_SYNC
))) {
1817 zil_commit(zv
->zv_zilog
, ZVOL_OBJ
);
1824 error
= SET_ERROR(ENOTTY
);
1828 mutex_exit(&zfsdev_state_lock
);
1835 return (zvol_minors
!= 0);
1841 VERIFY(ddi_soft_state_init(&zfsdev_state
, sizeof (zfs_soft_state_t
),
1843 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1849 mutex_destroy(&zfsdev_state_lock
);
1850 ddi_soft_state_fini(&zfsdev_state
);
1855 zfs_mvdev_dump_feature_check(void *arg
, dmu_tx_t
*tx
)
1857 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1859 if (spa_feature_is_active(spa
, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
))
1866 zfs_mvdev_dump_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
1868 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1870 spa_feature_incr(spa
, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
, tx
);
1874 zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
)
1878 objset_t
*os
= zv
->zv_objset
;
1879 spa_t
*spa
= dmu_objset_spa(os
);
1880 vdev_t
*vd
= spa
->spa_root_vdev
;
1881 nvlist_t
*nv
= NULL
;
1882 uint64_t version
= spa_version(spa
);
1883 uint64_t checksum
, compress
, refresrv
, vbs
, dedup
;
1885 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
1886 ASSERT(vd
->vdev_ops
== &vdev_root_ops
);
1888 error
= dmu_free_long_range(zv
->zv_objset
, ZVOL_OBJ
, 0,
1892 /* wait for dmu_free_long_range to actually free the blocks */
1893 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
1896 * If the pool on which the dump device is being initialized has more
1897 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
1898 * enabled. If so, bump that feature's counter to indicate that the
1899 * feature is active. We also check the vdev type to handle the
1901 * # zpool create test raidz disk1 disk2 disk3
1902 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
1903 * the raidz vdev itself has 3 children.
1905 if (vd
->vdev_children
> 1 || vd
->vdev_ops
== &vdev_raidz_ops
) {
1906 if (!spa_feature_is_enabled(spa
,
1907 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
))
1908 return (SET_ERROR(ENOTSUP
));
1909 (void) dsl_sync_task(spa_name(spa
),
1910 zfs_mvdev_dump_feature_check
,
1911 zfs_mvdev_dump_activate_feature_sync
, NULL
,
1912 2, ZFS_SPACE_CHECK_RESERVED
);
1916 error
= dsl_prop_get_integer(zv
->zv_name
,
1917 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), &compress
, NULL
);
1919 error
= dsl_prop_get_integer(zv
->zv_name
,
1920 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), &checksum
,
1924 error
= dsl_prop_get_integer(zv
->zv_name
,
1925 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
1929 error
= dsl_prop_get_integer(zv
->zv_name
,
1930 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &vbs
,
1933 if (version
>= SPA_VERSION_DEDUP
&& error
== 0) {
1934 error
= dsl_prop_get_integer(zv
->zv_name
,
1935 zfs_prop_to_name(ZFS_PROP_DEDUP
), &dedup
, NULL
);
1941 tx
= dmu_tx_create(os
);
1942 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
1943 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
1944 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1951 * If we are resizing the dump device then we only need to
1952 * update the refreservation to match the newly updated
1953 * zvolsize. Otherwise, we save off the original state of the
1954 * zvol so that we can restore them if the zvol is ever undumpified.
1957 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1958 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1959 &zv
->zv_volsize
, tx
);
1961 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1962 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1,
1965 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1966 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1,
1970 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1971 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1975 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1976 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1,
1980 error
= dmu_object_set_blocksize(
1981 os
, ZVOL_OBJ
, SPA_OLD_MAXBLOCKSIZE
, 0, tx
);
1983 if (version
>= SPA_VERSION_DEDUP
&& error
== 0) {
1984 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1985 zfs_prop_to_name(ZFS_PROP_DEDUP
), 8, 1,
1989 zv
->zv_volblocksize
= SPA_OLD_MAXBLOCKSIZE
;
1994 * We only need update the zvol's property if we are initializing
1995 * the dump area for the first time.
1997 if (error
== 0 && !resize
) {
1999 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2000 * function. Otherwise, use the old default -- OFF.
2002 checksum
= spa_feature_is_active(spa
,
2003 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP
) ? ZIO_CHECKSUM_NOPARITY
:
2006 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2007 VERIFY(nvlist_add_uint64(nv
,
2008 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 0) == 0);
2009 VERIFY(nvlist_add_uint64(nv
,
2010 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
2011 ZIO_COMPRESS_OFF
) == 0);
2012 VERIFY(nvlist_add_uint64(nv
,
2013 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
2015 if (version
>= SPA_VERSION_DEDUP
) {
2016 VERIFY(nvlist_add_uint64(nv
,
2017 zfs_prop_to_name(ZFS_PROP_DEDUP
),
2018 ZIO_CHECKSUM_OFF
) == 0);
2021 error
= zfs_set_prop_nvlist(zv
->zv_name
, ZPROP_SRC_LOCAL
,
2026 /* Allocate the space for the dump */
2028 error
= zvol_prealloc(zv
);
2033 zvol_dumpify(zvol_state_t
*zv
)
2036 uint64_t dumpsize
= 0;
2038 objset_t
*os
= zv
->zv_objset
;
2040 if (zv
->zv_flags
& ZVOL_RDONLY
)
2041 return (SET_ERROR(EROFS
));
2043 if (zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
,
2044 8, 1, &dumpsize
) != 0 || dumpsize
!= zv
->zv_volsize
) {
2045 boolean_t resize
= (dumpsize
> 0);
2047 if ((error
= zvol_dump_init(zv
, resize
)) != 0) {
2048 (void) zvol_dump_fini(zv
);
2054 * Build up our lba mapping.
2056 error
= zvol_get_lbas(zv
);
2058 (void) zvol_dump_fini(zv
);
2062 tx
= dmu_tx_create(os
);
2063 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
2064 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2067 (void) zvol_dump_fini(zv
);
2071 zv
->zv_flags
|= ZVOL_DUMPIFIED
;
2072 error
= zap_update(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, 8, 1,
2073 &zv
->zv_volsize
, tx
);
2077 (void) zvol_dump_fini(zv
);
2081 txg_wait_synced(dmu_objset_pool(os
), 0);
2086 zvol_dump_fini(zvol_state_t
*zv
)
2089 objset_t
*os
= zv
->zv_objset
;
2092 uint64_t checksum
, compress
, refresrv
, vbs
, dedup
;
2093 uint64_t version
= spa_version(dmu_objset_spa(zv
->zv_objset
));
2096 * Attempt to restore the zvol back to its pre-dumpified state.
2097 * This is a best-effort attempt as it's possible that not all
2098 * of these properties were initialized during the dumpify process
2099 * (i.e. error during zvol_dump_init).
2102 tx
= dmu_tx_create(os
);
2103 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
2104 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2109 (void) zap_remove(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, tx
);
2112 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2113 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1, &checksum
);
2114 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2115 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1, &compress
);
2116 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2117 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1, &refresrv
);
2118 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2119 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1, &vbs
);
2121 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2122 (void) nvlist_add_uint64(nv
,
2123 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), checksum
);
2124 (void) nvlist_add_uint64(nv
,
2125 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), compress
);
2126 (void) nvlist_add_uint64(nv
,
2127 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), refresrv
);
2128 if (version
>= SPA_VERSION_DEDUP
&&
2129 zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
2130 zfs_prop_to_name(ZFS_PROP_DEDUP
), 8, 1, &dedup
) == 0) {
2131 (void) nvlist_add_uint64(nv
,
2132 zfs_prop_to_name(ZFS_PROP_DEDUP
), dedup
);
2134 (void) zfs_set_prop_nvlist(zv
->zv_name
, ZPROP_SRC_LOCAL
,
2138 zvol_free_extents(zv
);
2139 zv
->zv_flags
&= ~ZVOL_DUMPIFIED
;
2140 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, DMU_OBJECT_END
);
2141 /* wait for dmu_free_long_range to actually free the blocks */
2142 txg_wait_synced(dmu_objset_pool(zv
->zv_objset
), 0);
2143 tx
= dmu_tx_create(os
);
2144 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
2145 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2150 if (dmu_object_set_blocksize(os
, ZVOL_OBJ
, vbs
, 0, tx
) == 0)
2151 zv
->zv_volblocksize
= vbs
;