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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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/zvol/dsk/<pool_name>/<dataset_name>
33 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
35 * These links are created by the ZFS-specific devfsadm link generator.
36 * Volumes are persistent through reboot. No user command needs to be
37 * run before opening and using a device.
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/errno.h>
45 #include <sys/modctl.h>
49 #include <sys/cmn_err.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/dnode.h>
56 #include <sys/dsl_dataset.h>
57 #include <sys/dsl_prop.h>
59 #include <sys/efi_partition.h>
60 #include <sys/byteorder.h>
61 #include <sys/pathname.h>
63 #include <sys/sunddi.h>
64 #include <sys/crc32.h>
65 #include <sys/dirent.h>
66 #include <sys/policy.h>
67 #include <sys/fs/zfs.h>
68 #include <sys/zfs_ioctl.h>
69 #include <sys/mkdev.h>
71 #include <sys/refcount.h>
72 #include <sys/zfs_znode.h>
73 #include <sys/zfs_rlock.h>
74 #include <sys/vdev_disk.h>
75 #include <sys/vdev_impl.h>
77 #include <sys/dumphdr.h>
79 #include "zfs_namecheck.h"
81 static void *zvol_state
;
83 #define ZVOL_DUMPSIZE "dumpsize"
86 * This lock protects the zvol_state structure from being modified
87 * while it's being used, e.g. an open that comes in before a create
88 * finishes. It also protects temporary opens of the dataset so that,
89 * e.g., an open doesn't get a spurious EBUSY.
91 static kmutex_t zvol_state_lock
;
92 static uint32_t zvol_minors
;
94 typedef struct zvol_extent
{
96 dva_t ze_dva
; /* dva associated with this extent */
97 uint64_t ze_nblks
; /* number of blocks in extent */
101 * The in-core state of each volume.
103 typedef struct zvol_state
{
104 char zv_name
[MAXPATHLEN
]; /* pool/dd name */
105 uint64_t zv_volsize
; /* amount of space we advertise */
106 uint64_t zv_volblocksize
; /* volume block size */
107 minor_t zv_minor
; /* minor number */
108 uint8_t zv_min_bs
; /* minimum addressable block shift */
109 uint8_t zv_flags
; /* readonly; dumpified */
110 objset_t
*zv_objset
; /* objset handle */
111 uint32_t zv_mode
; /* DS_MODE_* flags at open time */
112 uint32_t zv_open_count
[OTYPCNT
]; /* open counts */
113 uint32_t zv_total_opens
; /* total open count */
114 zilog_t
*zv_zilog
; /* ZIL handle */
115 list_t zv_extents
; /* List of extents for dump */
116 uint64_t zv_txg_assign
; /* txg to assign during ZIL replay */
117 znode_t zv_znode
; /* for range locking */
121 * zvol specific flags
123 #define ZVOL_RDONLY 0x1
124 #define ZVOL_DUMPIFIED 0x2
125 #define ZVOL_EXCL 0x4
128 * zvol maximum transfer in one DMU tx.
130 int zvol_maxphys
= DMU_MAX_ACCESS
/2;
132 extern int zfs_set_prop_nvlist(const char *, nvlist_t
*);
133 static int zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
);
134 static int zvol_dumpify(zvol_state_t
*zv
);
135 static int zvol_dump_fini(zvol_state_t
*zv
);
136 static int zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
);
139 zvol_size_changed(zvol_state_t
*zv
, major_t maj
)
141 dev_t dev
= makedevice(maj
, zv
->zv_minor
);
143 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
144 "Size", zv
->zv_volsize
) == DDI_SUCCESS
);
145 VERIFY(ddi_prop_update_int64(dev
, zfs_dip
,
146 "Nblocks", lbtodb(zv
->zv_volsize
)) == DDI_SUCCESS
);
148 /* Notify specfs to invalidate the cached size */
149 spec_size_invalidate(dev
, VBLK
);
150 spec_size_invalidate(dev
, VCHR
);
154 zvol_check_volsize(uint64_t volsize
, uint64_t blocksize
)
159 if (volsize
% blocksize
!= 0)
163 if (volsize
- 1 > SPEC_MAXOFFSET_T
)
170 zvol_check_volblocksize(uint64_t volblocksize
)
172 if (volblocksize
< SPA_MINBLOCKSIZE
||
173 volblocksize
> SPA_MAXBLOCKSIZE
||
181 zvol_readonly_changed_cb(void *arg
, uint64_t newval
)
183 zvol_state_t
*zv
= arg
;
186 zv
->zv_flags
|= ZVOL_RDONLY
;
188 zv
->zv_flags
&= ~ZVOL_RDONLY
;
192 zvol_get_stats(objset_t
*os
, nvlist_t
*nv
)
195 dmu_object_info_t doi
;
199 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &val
);
203 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLSIZE
, val
);
205 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
208 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_VOLBLOCKSIZE
,
209 doi
.doi_data_block_size
);
216 * Find a free minor number.
219 zvol_minor_alloc(void)
223 ASSERT(MUTEX_HELD(&zvol_state_lock
));
225 for (minor
= 1; minor
<= ZVOL_MAX_MINOR
; minor
++)
226 if (ddi_get_soft_state(zvol_state
, minor
) == NULL
)
232 static zvol_state_t
*
233 zvol_minor_lookup(const char *name
)
238 ASSERT(MUTEX_HELD(&zvol_state_lock
));
240 for (minor
= 1; minor
<= ZVOL_MAX_MINOR
; minor
++) {
241 zv
= ddi_get_soft_state(zvol_state
, minor
);
244 if (strcmp(zv
->zv_name
, name
) == 0)
251 /* extent mapping arg */
259 zvol_map_block(spa_t
*spa
, blkptr_t
*bp
, const zbookmark_t
*zb
,
260 const dnode_phys_t
*dnp
, void *arg
)
262 struct maparg
*ma
= arg
;
264 int bs
= ma
->ma_zv
->zv_volblocksize
;
266 if (bp
== NULL
|| zb
->zb_object
!= ZVOL_OBJ
|| zb
->zb_level
!= 0)
269 VERIFY3U(ma
->ma_blks
, ==, zb
->zb_blkid
);
272 /* Abort immediately if we have encountered gang blocks */
277 * See if the block is at the end of the previous extent.
279 ze
= list_tail(&ma
->ma_zv
->zv_extents
);
281 DVA_GET_VDEV(BP_IDENTITY(bp
)) == DVA_GET_VDEV(&ze
->ze_dva
) &&
282 DVA_GET_OFFSET(BP_IDENTITY(bp
)) ==
283 DVA_GET_OFFSET(&ze
->ze_dva
) + ze
->ze_nblks
* bs
) {
288 dprintf_bp(bp
, "%s", "next blkptr:");
290 /* start a new extent */
291 ze
= kmem_zalloc(sizeof (zvol_extent_t
), KM_SLEEP
);
292 ze
->ze_dva
= bp
->blk_dva
[0]; /* structure assignment */
294 list_insert_tail(&ma
->ma_zv
->zv_extents
, ze
);
299 zvol_free_extents(zvol_state_t
*zv
)
303 while (ze
= list_head(&zv
->zv_extents
)) {
304 list_remove(&zv
->zv_extents
, ze
);
305 kmem_free(ze
, sizeof (zvol_extent_t
));
310 zvol_get_lbas(zvol_state_t
*zv
)
317 zvol_free_extents(zv
);
319 err
= traverse_dataset(dmu_objset_ds(zv
->zv_objset
), 0,
320 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
, zvol_map_block
, &ma
);
321 if (err
|| ma
.ma_blks
!= (zv
->zv_volsize
/ zv
->zv_volblocksize
)) {
322 zvol_free_extents(zv
);
323 return (err
? err
: EIO
);
331 zvol_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
333 zfs_creat_t
*zct
= arg
;
334 nvlist_t
*nvprops
= zct
->zct_props
;
336 uint64_t volblocksize
, volsize
;
338 VERIFY(nvlist_lookup_uint64(nvprops
,
339 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) == 0);
340 if (nvlist_lookup_uint64(nvprops
,
341 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &volblocksize
) != 0)
342 volblocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
345 * These properties must be removed from the list so the generic
346 * property setting step won't apply to them.
348 VERIFY(nvlist_remove_all(nvprops
,
349 zfs_prop_to_name(ZFS_PROP_VOLSIZE
)) == 0);
350 (void) nvlist_remove_all(nvprops
,
351 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
));
353 error
= dmu_object_claim(os
, ZVOL_OBJ
, DMU_OT_ZVOL
, volblocksize
,
357 error
= zap_create_claim(os
, ZVOL_ZAP_OBJ
, DMU_OT_ZVOL_PROP
,
361 error
= zap_update(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
, tx
);
366 * Replay a TX_WRITE ZIL transaction that didn't get committed
367 * after a system failure
370 zvol_replay_write(zvol_state_t
*zv
, lr_write_t
*lr
, boolean_t byteswap
)
372 objset_t
*os
= zv
->zv_objset
;
373 char *data
= (char *)(lr
+ 1); /* data follows lr_write_t */
374 uint64_t off
= lr
->lr_offset
;
375 uint64_t len
= lr
->lr_length
;
380 byteswap_uint64_array(lr
, sizeof (*lr
));
382 tx
= dmu_tx_create(os
);
383 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, len
);
384 error
= dmu_tx_assign(tx
, zv
->zv_txg_assign
);
388 dmu_write(os
, ZVOL_OBJ
, off
, len
, data
, tx
);
397 zvol_replay_err(zvol_state_t
*zv
, lr_t
*lr
, boolean_t byteswap
)
403 * Callback vectors for replaying records.
404 * Only TX_WRITE is needed for zvol.
406 zil_replay_func_t
*zvol_replay_vector
[TX_MAX_TYPE
] = {
407 zvol_replay_err
, /* 0 no such transaction type */
408 zvol_replay_err
, /* TX_CREATE */
409 zvol_replay_err
, /* TX_MKDIR */
410 zvol_replay_err
, /* TX_MKXATTR */
411 zvol_replay_err
, /* TX_SYMLINK */
412 zvol_replay_err
, /* TX_REMOVE */
413 zvol_replay_err
, /* TX_RMDIR */
414 zvol_replay_err
, /* TX_LINK */
415 zvol_replay_err
, /* TX_RENAME */
416 zvol_replay_write
, /* TX_WRITE */
417 zvol_replay_err
, /* TX_TRUNCATE */
418 zvol_replay_err
, /* TX_SETATTR */
419 zvol_replay_err
, /* TX_ACL */
423 * Create a minor node (plus a whole lot more) for the specified volume.
426 zvol_create_minor(const char *name
, major_t maj
)
430 dmu_object_info_t doi
;
433 struct pathname linkpath
;
434 int ds_mode
= DS_MODE_OWNER
;
437 size_t devpathlen
= strlen(ZVOL_FULL_DEV_DIR
) + strlen(name
) + 1;
438 char chrbuf
[30], blkbuf
[30];
441 mutex_enter(&zvol_state_lock
);
443 if ((zv
= zvol_minor_lookup(name
)) != NULL
) {
444 mutex_exit(&zvol_state_lock
);
448 if (strchr(name
, '@') != 0)
449 ds_mode
|= DS_MODE_READONLY
;
451 error
= dmu_objset_open(name
, DMU_OST_ZVOL
, ds_mode
, &os
);
454 mutex_exit(&zvol_state_lock
);
458 error
= zap_lookup(os
, ZVOL_ZAP_OBJ
, "size", 8, 1, &volsize
);
461 dmu_objset_close(os
);
462 mutex_exit(&zvol_state_lock
);
467 * If there's an existing /dev/zvol symlink, try to use the
468 * same minor number we used last time.
470 devpath
= kmem_alloc(devpathlen
, KM_SLEEP
);
472 (void) sprintf(devpath
, "%s%s", ZVOL_FULL_DEV_DIR
, name
);
474 error
= lookupname(devpath
, UIO_SYSSPACE
, NO_FOLLOW
, NULL
, &vp
);
476 kmem_free(devpath
, devpathlen
);
478 if (error
== 0 && vp
->v_type
!= VLNK
)
483 error
= pn_getsymlink(vp
, &linkpath
, kcred
);
485 char *ms
= strstr(linkpath
.pn_path
, ZVOL_PSEUDO_DEV
);
487 ms
+= strlen(ZVOL_PSEUDO_DEV
);
498 * If we found a minor but it's already in use, we must pick a new one.
500 if (minor
!= 0 && ddi_get_soft_state(zvol_state
, minor
) != NULL
)
504 minor
= zvol_minor_alloc();
507 dmu_objset_close(os
);
508 mutex_exit(&zvol_state_lock
);
512 if (ddi_soft_state_zalloc(zvol_state
, minor
) != DDI_SUCCESS
) {
513 dmu_objset_close(os
);
514 mutex_exit(&zvol_state_lock
);
518 (void) ddi_prop_update_string(minor
, zfs_dip
, ZVOL_PROP_NAME
,
521 (void) sprintf(chrbuf
, "%uc,raw", minor
);
523 if (ddi_create_minor_node(zfs_dip
, chrbuf
, S_IFCHR
,
524 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
525 ddi_soft_state_free(zvol_state
, minor
);
526 dmu_objset_close(os
);
527 mutex_exit(&zvol_state_lock
);
531 (void) sprintf(blkbuf
, "%uc", minor
);
533 if (ddi_create_minor_node(zfs_dip
, blkbuf
, S_IFBLK
,
534 minor
, DDI_PSEUDO
, 0) == DDI_FAILURE
) {
535 ddi_remove_minor_node(zfs_dip
, chrbuf
);
536 ddi_soft_state_free(zvol_state
, minor
);
537 dmu_objset_close(os
);
538 mutex_exit(&zvol_state_lock
);
542 zv
= ddi_get_soft_state(zvol_state
, minor
);
544 (void) strcpy(zv
->zv_name
, name
);
545 zv
->zv_min_bs
= DEV_BSHIFT
;
546 zv
->zv_minor
= minor
;
547 zv
->zv_volsize
= volsize
;
549 zv
->zv_mode
= ds_mode
;
550 zv
->zv_zilog
= zil_open(os
, zvol_get_data
);
551 mutex_init(&zv
->zv_znode
.z_range_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
552 avl_create(&zv
->zv_znode
.z_range_avl
, zfs_range_compare
,
553 sizeof (rl_t
), offsetof(rl_t
, r_node
));
554 list_create(&zv
->zv_extents
, sizeof (zvol_extent_t
),
555 offsetof(zvol_extent_t
, ze_node
));
556 /* get and cache the blocksize */
557 error
= dmu_object_info(os
, ZVOL_OBJ
, &doi
);
559 zv
->zv_volblocksize
= doi
.doi_data_block_size
;
561 zil_replay(os
, zv
, &zv
->zv_txg_assign
, zvol_replay_vector
, NULL
);
562 zvol_size_changed(zv
, maj
);
564 /* XXX this should handle the possible i/o error */
565 VERIFY(dsl_prop_register(dmu_objset_ds(zv
->zv_objset
),
566 "readonly", zvol_readonly_changed_cb
, zv
) == 0);
570 mutex_exit(&zvol_state_lock
);
576 * Remove minor node for the specified volume.
579 zvol_remove_minor(const char *name
)
584 mutex_enter(&zvol_state_lock
);
586 if ((zv
= zvol_minor_lookup(name
)) == NULL
) {
587 mutex_exit(&zvol_state_lock
);
591 if (zv
->zv_total_opens
!= 0) {
592 mutex_exit(&zvol_state_lock
);
596 (void) sprintf(namebuf
, "%uc,raw", zv
->zv_minor
);
597 ddi_remove_minor_node(zfs_dip
, namebuf
);
599 (void) sprintf(namebuf
, "%uc", zv
->zv_minor
);
600 ddi_remove_minor_node(zfs_dip
, namebuf
);
602 VERIFY(dsl_prop_unregister(dmu_objset_ds(zv
->zv_objset
),
603 "readonly", zvol_readonly_changed_cb
, zv
) == 0);
605 zil_close(zv
->zv_zilog
);
607 dmu_objset_close(zv
->zv_objset
);
608 zv
->zv_objset
= NULL
;
609 avl_destroy(&zv
->zv_znode
.z_range_avl
);
610 mutex_destroy(&zv
->zv_znode
.z_range_lock
);
612 ddi_soft_state_free(zvol_state
, zv
->zv_minor
);
616 mutex_exit(&zvol_state_lock
);
622 zvol_prealloc(zvol_state_t
*zv
)
624 objset_t
*os
= zv
->zv_objset
;
626 uint64_t refd
, avail
, usedobjs
, availobjs
;
627 uint64_t resid
= zv
->zv_volsize
;
630 /* Check the space usage before attempting to allocate the space */
631 dmu_objset_space(os
, &refd
, &avail
, &usedobjs
, &availobjs
);
632 if (avail
< zv
->zv_volsize
)
635 /* Free old extents if they exist */
636 zvol_free_extents(zv
);
640 uint64_t bytes
= MIN(resid
, SPA_MAXBLOCKSIZE
);
642 tx
= dmu_tx_create(os
);
643 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
644 error
= dmu_tx_assign(tx
, TXG_WAIT
);
647 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, off
);
650 dmu_prealloc(os
, ZVOL_OBJ
, off
, bytes
, tx
);
655 txg_wait_synced(dmu_objset_pool(os
), 0);
661 zvol_update_volsize(zvol_state_t
*zv
, major_t maj
, uint64_t volsize
)
666 ASSERT(MUTEX_HELD(&zvol_state_lock
));
668 tx
= dmu_tx_create(zv
->zv_objset
);
669 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
670 error
= dmu_tx_assign(tx
, TXG_WAIT
);
676 error
= zap_update(zv
->zv_objset
, ZVOL_ZAP_OBJ
, "size", 8, 1,
681 error
= dmu_free_long_range(zv
->zv_objset
,
682 ZVOL_OBJ
, volsize
, DMU_OBJECT_END
);
685 * If we are using a faked-up state (zv_minor == 0) then don't
686 * try to update the in-core zvol state.
688 if (error
== 0 && zv
->zv_minor
) {
689 zv
->zv_volsize
= volsize
;
690 zvol_size_changed(zv
, maj
);
696 zvol_set_volsize(const char *name
, major_t maj
, uint64_t volsize
)
700 dmu_object_info_t doi
;
701 uint64_t old_volsize
= 0ULL;
702 zvol_state_t state
= { 0 };
704 mutex_enter(&zvol_state_lock
);
706 if ((zv
= zvol_minor_lookup(name
)) == NULL
) {
708 * If we are doing a "zfs clone -o volsize=", then the
709 * minor node won't exist yet.
711 error
= dmu_objset_open(name
, DMU_OST_ZVOL
, DS_MODE_OWNER
,
717 old_volsize
= zv
->zv_volsize
;
719 if ((error
= dmu_object_info(zv
->zv_objset
, ZVOL_OBJ
, &doi
)) != 0 ||
720 (error
= zvol_check_volsize(volsize
,
721 doi
.doi_data_block_size
)) != 0)
724 if (zv
->zv_flags
& ZVOL_RDONLY
|| (zv
->zv_mode
& DS_MODE_READONLY
)) {
729 error
= zvol_update_volsize(zv
, maj
, volsize
);
732 * Reinitialize the dump area to the new size. If we
733 * failed to resize the dump area then restore the it back to
734 * it's original size.
736 if (error
== 0 && zv
->zv_flags
& ZVOL_DUMPIFIED
) {
737 if ((error
= zvol_dumpify(zv
)) != 0 ||
738 (error
= dumpvp_resize()) != 0) {
739 (void) zvol_update_volsize(zv
, maj
, old_volsize
);
740 error
= zvol_dumpify(zv
);
746 dmu_objset_close(state
.zv_objset
);
748 mutex_exit(&zvol_state_lock
);
754 zvol_set_volblocksize(const char *name
, uint64_t volblocksize
)
762 * The lock may already be held if we are being called from
765 needlock
= !MUTEX_HELD(&zvol_state_lock
);
767 mutex_enter(&zvol_state_lock
);
769 if ((zv
= zvol_minor_lookup(name
)) == NULL
) {
771 mutex_exit(&zvol_state_lock
);
774 if (zv
->zv_flags
& ZVOL_RDONLY
|| (zv
->zv_mode
& DS_MODE_READONLY
)) {
776 mutex_exit(&zvol_state_lock
);
780 tx
= dmu_tx_create(zv
->zv_objset
);
781 dmu_tx_hold_bonus(tx
, ZVOL_OBJ
);
782 error
= dmu_tx_assign(tx
, TXG_WAIT
);
786 error
= dmu_object_set_blocksize(zv
->zv_objset
, ZVOL_OBJ
,
787 volblocksize
, 0, tx
);
788 if (error
== ENOTSUP
)
792 zv
->zv_volblocksize
= volblocksize
;
796 mutex_exit(&zvol_state_lock
);
803 zvol_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
805 minor_t minor
= getminor(*devp
);
808 if (minor
== 0) /* This is the control device */
811 mutex_enter(&zvol_state_lock
);
813 zv
= ddi_get_soft_state(zvol_state
, minor
);
815 mutex_exit(&zvol_state_lock
);
819 ASSERT(zv
->zv_objset
!= NULL
);
821 if ((flag
& FWRITE
) &&
822 (zv
->zv_flags
& ZVOL_RDONLY
|| (zv
->zv_mode
& DS_MODE_READONLY
))) {
823 mutex_exit(&zvol_state_lock
);
826 if (zv
->zv_flags
& ZVOL_EXCL
) {
827 mutex_exit(&zvol_state_lock
);
831 if (zv
->zv_total_opens
!= 0) {
832 mutex_exit(&zvol_state_lock
);
835 zv
->zv_flags
|= ZVOL_EXCL
;
838 if (zv
->zv_open_count
[otyp
] == 0 || otyp
== OTYP_LYR
) {
839 zv
->zv_open_count
[otyp
]++;
840 zv
->zv_total_opens
++;
843 mutex_exit(&zvol_state_lock
);
850 zvol_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
852 minor_t minor
= getminor(dev
);
855 if (minor
== 0) /* This is the control device */
858 mutex_enter(&zvol_state_lock
);
860 zv
= ddi_get_soft_state(zvol_state
, minor
);
862 mutex_exit(&zvol_state_lock
);
866 if (zv
->zv_flags
& ZVOL_EXCL
) {
867 ASSERT(zv
->zv_total_opens
== 1);
868 zv
->zv_flags
&= ~ZVOL_EXCL
;
872 * If the open count is zero, this is a spurious close.
873 * That indicates a bug in the kernel / DDI framework.
875 ASSERT(zv
->zv_open_count
[otyp
] != 0);
876 ASSERT(zv
->zv_total_opens
!= 0);
879 * You may get multiple opens, but only one close.
881 zv
->zv_open_count
[otyp
]--;
882 zv
->zv_total_opens
--;
884 mutex_exit(&zvol_state_lock
);
890 zvol_get_done(dmu_buf_t
*db
, void *vzgd
)
892 zgd_t
*zgd
= (zgd_t
*)vzgd
;
893 rl_t
*rl
= zgd
->zgd_rl
;
895 dmu_buf_rele(db
, vzgd
);
896 zfs_range_unlock(rl
);
897 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
898 kmem_free(zgd
, sizeof (zgd_t
));
902 * Get data to generate a TX_WRITE intent log record.
905 zvol_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
907 zvol_state_t
*zv
= arg
;
908 objset_t
*os
= zv
->zv_objset
;
912 uint64_t boff
; /* block starting offset */
913 int dlen
= lr
->lr_length
; /* length of user data */
920 * Write records come in two flavors: immediate and indirect.
921 * For small writes it's cheaper to store the data with the
922 * log record (immediate); for large writes it's cheaper to
923 * sync the data and get a pointer to it (indirect) so that
924 * we don't have to write the data twice.
926 if (buf
!= NULL
) /* immediate write */
927 return (dmu_read(os
, ZVOL_OBJ
, lr
->lr_offset
, dlen
, buf
));
929 zgd
= (zgd_t
*)kmem_alloc(sizeof (zgd_t
), KM_SLEEP
);
930 zgd
->zgd_zilog
= zv
->zv_zilog
;
931 zgd
->zgd_bp
= &lr
->lr_blkptr
;
934 * Lock the range of the block to ensure that when the data is
935 * written out and its checksum is being calculated that no other
936 * thread can change the block.
938 boff
= P2ALIGN_TYPED(lr
->lr_offset
, zv
->zv_volblocksize
, uint64_t);
939 rl
= zfs_range_lock(&zv
->zv_znode
, boff
, zv
->zv_volblocksize
,
943 VERIFY(0 == dmu_buf_hold(os
, ZVOL_OBJ
, lr
->lr_offset
, zgd
, &db
));
944 error
= dmu_sync(zio
, db
, &lr
->lr_blkptr
,
945 lr
->lr_common
.lrc_txg
, zvol_get_done
, zgd
);
947 zil_add_block(zv
->zv_zilog
, &lr
->lr_blkptr
);
949 * If we get EINPROGRESS, then we need to wait for a
950 * write IO initiated by dmu_sync() to complete before
951 * we can release this dbuf. We will finish everything
952 * up in the zvol_get_done() callback.
954 if (error
== EINPROGRESS
)
956 dmu_buf_rele(db
, zgd
);
957 zfs_range_unlock(rl
);
958 kmem_free(zgd
, sizeof (zgd_t
));
963 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
965 * We store data in the log buffers if it's small enough.
966 * Otherwise we will later flush the data out via dmu_sync().
968 ssize_t zvol_immediate_write_sz
= 32768;
971 zvol_log_write(zvol_state_t
*zv
, dmu_tx_t
*tx
, offset_t off
, ssize_t len
)
973 uint32_t blocksize
= zv
->zv_volblocksize
;
977 ssize_t nbytes
= MIN(len
, blocksize
- P2PHASE(off
, blocksize
));
978 itx_t
*itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
981 len
> zvol_immediate_write_sz
? WR_INDIRECT
: WR_NEED_COPY
;
982 itx
->itx_private
= zv
;
983 lr
= (lr_write_t
*)&itx
->itx_lr
;
984 lr
->lr_foid
= ZVOL_OBJ
;
986 lr
->lr_length
= nbytes
;
987 lr
->lr_blkoff
= off
- P2ALIGN_TYPED(off
, blocksize
, uint64_t);
988 BP_ZERO(&lr
->lr_blkptr
);
990 (void) zil_itx_assign(zv
->zv_zilog
, itx
, tx
);
997 zvol_dumpio_vdev(vdev_t
*vd
, void *addr
, uint64_t offset
, uint64_t size
,
998 boolean_t doread
, boolean_t isdump
)
1004 for (c
= 0; c
< vd
->vdev_children
; c
++) {
1005 ASSERT(vd
->vdev_ops
== &vdev_mirror_ops
);
1006 int err
= zvol_dumpio_vdev(vd
->vdev_child
[c
],
1007 addr
, offset
, size
, doread
, isdump
);
1010 } else if (doread
) {
1015 if (!vd
->vdev_ops
->vdev_op_leaf
)
1016 return (numerrors
< vd
->vdev_children
? 0 : EIO
);
1018 if (doread
&& !vdev_readable(vd
))
1020 else if (!doread
&& !vdev_writeable(vd
))
1024 ASSERT3P(dvd
, !=, NULL
);
1025 offset
+= VDEV_LABEL_START_SIZE
;
1027 if (ddi_in_panic() || isdump
) {
1031 return (ldi_dump(dvd
->vd_lh
, addr
, lbtodb(offset
),
1034 return (vdev_disk_physio(dvd
->vd_lh
, addr
, size
, offset
,
1035 doread
? B_READ
: B_WRITE
));
1040 zvol_dumpio(zvol_state_t
*zv
, void *addr
, uint64_t offset
, uint64_t size
,
1041 boolean_t doread
, boolean_t isdump
)
1046 spa_t
*spa
= dmu_objset_spa(zv
->zv_objset
);
1048 /* Must be sector aligned, and not stradle a block boundary. */
1049 if (P2PHASE(offset
, DEV_BSIZE
) || P2PHASE(size
, DEV_BSIZE
) ||
1050 P2BOUNDARY(offset
, size
, zv
->zv_volblocksize
)) {
1053 ASSERT(size
<= zv
->zv_volblocksize
);
1055 /* Locate the extent this belongs to */
1056 ze
= list_head(&zv
->zv_extents
);
1057 while (offset
>= ze
->ze_nblks
* zv
->zv_volblocksize
) {
1058 offset
-= ze
->ze_nblks
* zv
->zv_volblocksize
;
1059 ze
= list_next(&zv
->zv_extents
, ze
);
1061 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
1062 vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&ze
->ze_dva
));
1063 offset
+= DVA_GET_OFFSET(&ze
->ze_dva
);
1064 error
= zvol_dumpio_vdev(vd
, addr
, offset
, size
, doread
, isdump
);
1065 spa_config_exit(spa
, SCL_STATE
, FTAG
);
1070 zvol_strategy(buf_t
*bp
)
1072 zvol_state_t
*zv
= ddi_get_soft_state(zvol_state
, getminor(bp
->b_edev
));
1073 uint64_t off
, volsize
;
1079 boolean_t doread
= bp
->b_flags
& B_READ
;
1080 boolean_t is_dump
= zv
->zv_flags
& ZVOL_DUMPIFIED
;
1083 bioerror(bp
, ENXIO
);
1088 if (getminor(bp
->b_edev
) == 0) {
1089 bioerror(bp
, EINVAL
);
1094 if (!(bp
->b_flags
& B_READ
) &&
1095 (zv
->zv_flags
& ZVOL_RDONLY
||
1096 zv
->zv_mode
& DS_MODE_READONLY
)) {
1097 bioerror(bp
, EROFS
);
1102 off
= ldbtob(bp
->b_blkno
);
1103 volsize
= zv
->zv_volsize
;
1109 addr
= bp
->b_un
.b_addr
;
1110 resid
= bp
->b_bcount
;
1112 if (resid
> 0 && (off
< 0 || off
>= volsize
)) {
1119 * There must be no buffer changes when doing a dmu_sync() because
1120 * we can't change the data whilst calculating the checksum.
1122 rl
= zfs_range_lock(&zv
->zv_znode
, off
, resid
,
1123 doread
? RL_READER
: RL_WRITER
);
1125 while (resid
!= 0 && off
< volsize
) {
1126 size_t size
= MIN(resid
, zvol_maxphys
);
1128 size
= MIN(size
, P2END(off
, zv
->zv_volblocksize
) - off
);
1129 error
= zvol_dumpio(zv
, addr
, off
, size
,
1131 } else if (doread
) {
1132 error
= dmu_read(os
, ZVOL_OBJ
, off
, size
, addr
);
1134 dmu_tx_t
*tx
= dmu_tx_create(os
);
1135 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, size
);
1136 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1140 dmu_write(os
, ZVOL_OBJ
, off
, size
, addr
, tx
);
1141 zvol_log_write(zv
, tx
, off
, size
);
1146 /* convert checksum errors into IO errors */
1147 if (error
== ECKSUM
)
1155 zfs_range_unlock(rl
);
1157 if ((bp
->b_resid
= resid
) == bp
->b_bcount
)
1158 bioerror(bp
, off
> volsize
? EINVAL
: error
);
1160 if (!(bp
->b_flags
& B_ASYNC
) && !doread
&& !zil_disable
&& !is_dump
)
1161 zil_commit(zv
->zv_zilog
, UINT64_MAX
, ZVOL_OBJ
);
1168 * Set the buffer count to the zvol maximum transfer.
1169 * Using our own routine instead of the default minphys()
1170 * means that for larger writes we write bigger buffers on X86
1171 * (128K instead of 56K) and flush the disk write cache less often
1172 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1173 * 56K on X86 and 128K on sparc).
1176 zvol_minphys(struct buf
*bp
)
1178 if (bp
->b_bcount
> zvol_maxphys
)
1179 bp
->b_bcount
= zvol_maxphys
;
1183 zvol_dump(dev_t dev
, caddr_t addr
, daddr_t blkno
, int nblocks
)
1185 minor_t minor
= getminor(dev
);
1192 if (minor
== 0) /* This is the control device */
1195 zv
= ddi_get_soft_state(zvol_state
, minor
);
1199 boff
= ldbtob(blkno
);
1200 resid
= ldbtob(nblocks
);
1202 VERIFY3U(boff
+ resid
, <=, zv
->zv_volsize
);
1205 size
= MIN(resid
, P2END(boff
, zv
->zv_volblocksize
) - boff
);
1206 error
= zvol_dumpio(zv
, addr
, boff
, size
, B_FALSE
, B_TRUE
);
1219 zvol_read(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1221 minor_t minor
= getminor(dev
);
1227 if (minor
== 0) /* This is the control device */
1230 zv
= ddi_get_soft_state(zvol_state
, minor
);
1234 volsize
= zv
->zv_volsize
;
1235 if (uio
->uio_resid
> 0 &&
1236 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1239 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1240 error
= physio(zvol_strategy
, NULL
, dev
, B_READ
,
1245 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1247 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1248 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1250 /* don't read past the end */
1251 if (bytes
> volsize
- uio
->uio_loffset
)
1252 bytes
= volsize
- uio
->uio_loffset
;
1254 error
= dmu_read_uio(zv
->zv_objset
, ZVOL_OBJ
, uio
, bytes
);
1256 /* convert checksum errors into IO errors */
1257 if (error
== ECKSUM
)
1262 zfs_range_unlock(rl
);
1268 zvol_write(dev_t dev
, uio_t
*uio
, cred_t
*cr
)
1270 minor_t minor
= getminor(dev
);
1276 if (minor
== 0) /* This is the control device */
1279 zv
= ddi_get_soft_state(zvol_state
, minor
);
1283 volsize
= zv
->zv_volsize
;
1284 if (uio
->uio_resid
> 0 &&
1285 (uio
->uio_loffset
< 0 || uio
->uio_loffset
>= volsize
))
1288 if (zv
->zv_flags
& ZVOL_DUMPIFIED
) {
1289 error
= physio(zvol_strategy
, NULL
, dev
, B_WRITE
,
1294 rl
= zfs_range_lock(&zv
->zv_znode
, uio
->uio_loffset
, uio
->uio_resid
,
1296 while (uio
->uio_resid
> 0 && uio
->uio_loffset
< volsize
) {
1297 uint64_t bytes
= MIN(uio
->uio_resid
, DMU_MAX_ACCESS
>> 1);
1298 uint64_t off
= uio
->uio_loffset
;
1299 dmu_tx_t
*tx
= dmu_tx_create(zv
->zv_objset
);
1301 if (bytes
> volsize
- off
) /* don't write past the end */
1302 bytes
= volsize
- off
;
1304 dmu_tx_hold_write(tx
, ZVOL_OBJ
, off
, bytes
);
1305 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1310 error
= dmu_write_uio(zv
->zv_objset
, ZVOL_OBJ
, uio
, bytes
, tx
);
1312 zvol_log_write(zv
, tx
, off
, bytes
);
1318 zfs_range_unlock(rl
);
1323 zvol_getefi(void *arg
, int flag
, uint64_t vs
, uint8_t bs
)
1325 struct uuid uuid
= EFI_RESERVED
;
1326 efi_gpe_t gpe
= { 0 };
1332 if (ddi_copyin(arg
, &efi
, sizeof (dk_efi_t
), flag
))
1334 ptr
= (char *)(uintptr_t)efi
.dki_data_64
;
1335 length
= efi
.dki_length
;
1337 * Some clients may attempt to request a PMBR for the
1338 * zvol. Currently this interface will return EINVAL to
1339 * such requests. These requests could be supported by
1340 * adding a check for lba == 0 and consing up an appropriate
1343 if (efi
.dki_lba
< 1 || efi
.dki_lba
> 2 || length
<= 0)
1346 gpe
.efi_gpe_StartingLBA
= LE_64(34ULL);
1347 gpe
.efi_gpe_EndingLBA
= LE_64((vs
>> bs
) - 1);
1348 UUID_LE_CONVERT(gpe
.efi_gpe_PartitionTypeGUID
, uuid
);
1350 if (efi
.dki_lba
== 1) {
1351 efi_gpt_t gpt
= { 0 };
1353 gpt
.efi_gpt_Signature
= LE_64(EFI_SIGNATURE
);
1354 gpt
.efi_gpt_Revision
= LE_32(EFI_VERSION_CURRENT
);
1355 gpt
.efi_gpt_HeaderSize
= LE_32(sizeof (gpt
));
1356 gpt
.efi_gpt_MyLBA
= LE_64(1ULL);
1357 gpt
.efi_gpt_FirstUsableLBA
= LE_64(34ULL);
1358 gpt
.efi_gpt_LastUsableLBA
= LE_64((vs
>> bs
) - 1);
1359 gpt
.efi_gpt_PartitionEntryLBA
= LE_64(2ULL);
1360 gpt
.efi_gpt_NumberOfPartitionEntries
= LE_32(1);
1361 gpt
.efi_gpt_SizeOfPartitionEntry
=
1362 LE_32(sizeof (efi_gpe_t
));
1363 CRC32(crc
, &gpe
, sizeof (gpe
), -1U, crc32_table
);
1364 gpt
.efi_gpt_PartitionEntryArrayCRC32
= LE_32(~crc
);
1365 CRC32(crc
, &gpt
, sizeof (gpt
), -1U, crc32_table
);
1366 gpt
.efi_gpt_HeaderCRC32
= LE_32(~crc
);
1367 if (ddi_copyout(&gpt
, ptr
, MIN(sizeof (gpt
), length
),
1370 ptr
+= sizeof (gpt
);
1371 length
-= sizeof (gpt
);
1373 if (length
> 0 && ddi_copyout(&gpe
, ptr
, MIN(sizeof (gpe
),
1380 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
1384 zvol_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
1387 struct dk_cinfo dki
;
1388 struct dk_minfo dkm
;
1389 struct dk_callback
*dkc
;
1393 mutex_enter(&zvol_state_lock
);
1395 zv
= ddi_get_soft_state(zvol_state
, getminor(dev
));
1398 mutex_exit(&zvol_state_lock
);
1405 bzero(&dki
, sizeof (dki
));
1406 (void) strcpy(dki
.dki_cname
, "zvol");
1407 (void) strcpy(dki
.dki_dname
, "zvol");
1408 dki
.dki_ctype
= DKC_UNKNOWN
;
1409 dki
.dki_maxtransfer
= 1 << (SPA_MAXBLOCKSHIFT
- zv
->zv_min_bs
);
1410 mutex_exit(&zvol_state_lock
);
1411 if (ddi_copyout(&dki
, (void *)arg
, sizeof (dki
), flag
))
1415 case DKIOCGMEDIAINFO
:
1416 bzero(&dkm
, sizeof (dkm
));
1417 dkm
.dki_lbsize
= 1U << zv
->zv_min_bs
;
1418 dkm
.dki_capacity
= zv
->zv_volsize
>> zv
->zv_min_bs
;
1419 dkm
.dki_media_type
= DK_UNKNOWN
;
1420 mutex_exit(&zvol_state_lock
);
1421 if (ddi_copyout(&dkm
, (void *)arg
, sizeof (dkm
), flag
))
1427 uint64_t vs
= zv
->zv_volsize
;
1428 uint8_t bs
= zv
->zv_min_bs
;
1430 mutex_exit(&zvol_state_lock
);
1431 error
= zvol_getefi((void *)arg
, flag
, vs
, bs
);
1435 case DKIOCFLUSHWRITECACHE
:
1436 dkc
= (struct dk_callback
*)arg
;
1437 zil_commit(zv
->zv_zilog
, UINT64_MAX
, ZVOL_OBJ
);
1438 if ((flag
& FKIOCTL
) && dkc
!= NULL
&& dkc
->dkc_callback
) {
1439 (*dkc
->dkc_callback
)(dkc
->dkc_cookie
, error
);
1447 * commands using these (like prtvtoc) expect ENOTSUP
1448 * since we're emulating an EFI label
1454 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1456 error
= zvol_dumpify(zv
);
1457 zfs_range_unlock(rl
);
1461 rl
= zfs_range_lock(&zv
->zv_znode
, 0, zv
->zv_volsize
,
1463 error
= zvol_dump_fini(zv
);
1464 zfs_range_unlock(rl
);
1472 mutex_exit(&zvol_state_lock
);
1479 return (zvol_minors
!= 0);
1485 VERIFY(ddi_soft_state_init(&zvol_state
, sizeof (zvol_state_t
), 1) == 0);
1486 mutex_init(&zvol_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1492 mutex_destroy(&zvol_state_lock
);
1493 ddi_soft_state_fini(&zvol_state
);
1497 zvol_is_swap(zvol_state_t
*zv
)
1500 boolean_t ret
= B_FALSE
;
1505 devpathlen
= strlen(ZVOL_FULL_DEV_DIR
) + strlen(zv
->zv_name
) + 1;
1506 devpath
= kmem_alloc(devpathlen
, KM_SLEEP
);
1507 (void) sprintf(devpath
, "%s%s", ZVOL_FULL_DEV_DIR
, zv
->zv_name
);
1508 error
= lookupname(devpath
, UIO_SYSSPACE
, FOLLOW
, NULLVPP
, &vp
);
1509 kmem_free(devpath
, devpathlen
);
1511 ret
= !error
&& IS_SWAPVP(common_specvp(vp
));
1520 zvol_dump_init(zvol_state_t
*zv
, boolean_t resize
)
1524 objset_t
*os
= zv
->zv_objset
;
1525 nvlist_t
*nv
= NULL
;
1527 ASSERT(MUTEX_HELD(&zvol_state_lock
));
1529 tx
= dmu_tx_create(os
);
1530 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
1531 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1538 * If we are resizing the dump device then we only need to
1539 * update the refreservation to match the newly updated
1540 * zvolsize. Otherwise, we save off the original state of the
1541 * zvol so that we can restore them if the zvol is ever undumpified.
1544 error
= zap_update(os
, ZVOL_ZAP_OBJ
,
1545 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1546 &zv
->zv_volsize
, tx
);
1548 uint64_t checksum
, compress
, refresrv
, vbs
;
1550 error
= dsl_prop_get_integer(zv
->zv_name
,
1551 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), &compress
, NULL
);
1552 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1553 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), &checksum
, NULL
);
1554 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1555 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &refresrv
, NULL
);
1556 error
= error
? error
: dsl_prop_get_integer(zv
->zv_name
,
1557 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), &vbs
, NULL
);
1559 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1560 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1,
1562 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1563 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1, &checksum
, tx
);
1564 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1565 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1,
1567 error
= error
? error
: zap_update(os
, ZVOL_ZAP_OBJ
,
1568 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1,
1573 /* Truncate the file */
1575 error
= dmu_free_long_range(zv
->zv_objset
,
1576 ZVOL_OBJ
, 0, DMU_OBJECT_END
);
1582 * We only need update the zvol's property if we are initializing
1583 * the dump area for the first time.
1586 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1587 VERIFY(nvlist_add_uint64(nv
,
1588 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 0) == 0);
1589 VERIFY(nvlist_add_uint64(nv
,
1590 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
1591 ZIO_COMPRESS_OFF
) == 0);
1592 VERIFY(nvlist_add_uint64(nv
,
1593 zfs_prop_to_name(ZFS_PROP_CHECKSUM
),
1594 ZIO_CHECKSUM_OFF
) == 0);
1595 VERIFY(nvlist_add_uint64(nv
,
1596 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1597 SPA_MAXBLOCKSIZE
) == 0);
1599 error
= zfs_set_prop_nvlist(zv
->zv_name
, nv
);
1606 /* Allocate the space for the dump */
1607 error
= zvol_prealloc(zv
);
1612 zvol_dumpify(zvol_state_t
*zv
)
1615 uint64_t dumpsize
= 0;
1617 objset_t
*os
= zv
->zv_objset
;
1619 if (zv
->zv_flags
& ZVOL_RDONLY
|| (zv
->zv_mode
& DS_MODE_READONLY
))
1623 * We do not support swap devices acting as dump devices.
1625 if (zvol_is_swap(zv
))
1628 if (zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
,
1629 8, 1, &dumpsize
) != 0 || dumpsize
!= zv
->zv_volsize
) {
1630 boolean_t resize
= (dumpsize
> 0) ? B_TRUE
: B_FALSE
;
1632 if ((error
= zvol_dump_init(zv
, resize
)) != 0) {
1633 (void) zvol_dump_fini(zv
);
1639 * Build up our lba mapping.
1641 error
= zvol_get_lbas(zv
);
1643 (void) zvol_dump_fini(zv
);
1647 tx
= dmu_tx_create(os
);
1648 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
1649 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1652 (void) zvol_dump_fini(zv
);
1656 zv
->zv_flags
|= ZVOL_DUMPIFIED
;
1657 error
= zap_update(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, 8, 1,
1658 &zv
->zv_volsize
, tx
);
1662 (void) zvol_dump_fini(zv
);
1666 txg_wait_synced(dmu_objset_pool(os
), 0);
1671 zvol_dump_fini(zvol_state_t
*zv
)
1674 objset_t
*os
= zv
->zv_objset
;
1677 uint64_t checksum
, compress
, refresrv
, vbs
;
1680 * Attempt to restore the zvol back to its pre-dumpified state.
1681 * This is a best-effort attempt as it's possible that not all
1682 * of these properties were initialized during the dumpify process
1683 * (i.e. error during zvol_dump_init).
1686 tx
= dmu_tx_create(os
);
1687 dmu_tx_hold_zap(tx
, ZVOL_ZAP_OBJ
, TRUE
, NULL
);
1688 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1693 (void) zap_remove(os
, ZVOL_ZAP_OBJ
, ZVOL_DUMPSIZE
, tx
);
1696 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
1697 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), 8, 1, &checksum
);
1698 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
1699 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), 8, 1, &compress
);
1700 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
1701 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), 8, 1, &refresrv
);
1702 (void) zap_lookup(zv
->zv_objset
, ZVOL_ZAP_OBJ
,
1703 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), 8, 1, &vbs
);
1705 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1706 (void) nvlist_add_uint64(nv
,
1707 zfs_prop_to_name(ZFS_PROP_CHECKSUM
), checksum
);
1708 (void) nvlist_add_uint64(nv
,
1709 zfs_prop_to_name(ZFS_PROP_COMPRESSION
), compress
);
1710 (void) nvlist_add_uint64(nv
,
1711 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), refresrv
);
1712 (void) nvlist_add_uint64(nv
,
1713 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
), vbs
);
1714 (void) zfs_set_prop_nvlist(zv
->zv_name
, nv
);
1717 zvol_free_extents(zv
);
1718 zv
->zv_flags
&= ~ZVOL_DUMPIFIED
;
1719 (void) dmu_free_long_range(os
, ZVOL_OBJ
, 0, DMU_OBJECT_END
);