18
#include "qemu/osdep.h"
19
#include "qapi/error.h"
20
#include "block/block_int.h"
21
#include "block/qdict.h"
22
#include "sysemu/block-backend.h"
23
#include "qemu/module.h"
24
#include "qemu/option.h"
25
#include "qemu/crc32c.h"
26
#include "qemu/bswap.h"
27
#include "qemu/error-report.h"
28
#include "qemu/memalign.h"
30
#include "migration/blocker.h"
32
#include "qapi/qmp/qdict.h"
33
#include "qapi/qobject-input-visitor.h"
34
#include "qapi/qapi-visit-block-core.h"
38
#define VHDX_BLOCK_OPT_LOG_SIZE "log_size"
39
#define VHDX_BLOCK_OPT_BLOCK_SIZE "block_size"
40
#define VHDX_BLOCK_OPT_ZERO "block_state_zero"
42
typedef enum VHDXImageType {
43
VHDX_TYPE_DYNAMIC = 0,
45
VHDX_TYPE_DIFFERENCING,
48
static QemuOptsList vhdx_create_opts;
55
static const MSGUID bat_guid = { .data1 = 0x2dc27766,
58
.data4 = { 0x9d, 0x64, 0x11, 0x5e,
59
0x9b, 0xfd, 0x4a, 0x08} };
61
static const MSGUID metadata_guid = { .data1 = 0x8b7ca206,
64
.data4 = { 0xb8, 0xfe, 0x57, 0x5f,
65
0x05, 0x0f, 0x88, 0x6e} };
70
static const MSGUID file_param_guid = { .data1 = 0xcaa16737,
73
.data4 = { 0xb3, 0xb6, 0x33, 0xf0,
74
0xaa, 0x44, 0xe7, 0x6b} };
76
static const MSGUID virtual_size_guid = { .data1 = 0x2FA54224,
79
.data4 = { 0xb2, 0x11, 0x5d, 0xbe,
80
0xd8, 0x3b, 0xf4, 0xb8} };
82
static const MSGUID page83_guid = { .data1 = 0xbeca12ab,
85
.data4 = { 0x93, 0xef, 0xc3, 0x09,
86
0xe0, 0x00, 0xc7, 0x46} };
89
static const MSGUID phys_sector_guid = { .data1 = 0xcda348c7,
92
.data4 = { 0x9c, 0xc9, 0xe9, 0x88,
93
0x52, 0x51, 0xc5, 0x56} };
95
static const MSGUID parent_locator_guid = { .data1 = 0xa8d35f2d,
98
.data4 = { 0xab, 0xf7, 0xd3,
102
static const MSGUID logical_sector_guid = { .data1 = 0x8141bf1d,
105
.data4 = { 0xba, 0x47, 0xf2,
112
static const MSGUID parent_vhdx_guid __attribute__((unused))
113
= { .data1 = 0xb04aefb7,
116
.data4 = { 0xb7, 0x89, 0x25, 0xb8,
117
0xe9, 0x44, 0x59, 0x13} };
120
#define META_FILE_PARAMETER_PRESENT 0x01
121
#define META_VIRTUAL_DISK_SIZE_PRESENT 0x02
122
#define META_PAGE_83_PRESENT 0x04
123
#define META_LOGICAL_SECTOR_SIZE_PRESENT 0x08
124
#define META_PHYS_SECTOR_SIZE_PRESENT 0x10
125
#define META_PARENT_LOCATOR_PRESENT 0x20
127
#define META_ALL_PRESENT \
128
(META_FILE_PARAMETER_PRESENT | META_VIRTUAL_DISK_SIZE_PRESENT | \
129
META_PAGE_83_PRESENT | META_LOGICAL_SECTOR_SIZE_PRESENT | \
130
META_PHYS_SECTOR_SIZE_PRESENT)
133
typedef struct VHDXSectorInfo {
135
uint32_t sectors_avail;
137
uint32_t bytes_avail;
138
uint64_t file_offset;
139
uint64_t block_offset;
152
uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset)
157
assert(size > (crc_offset + sizeof(crc)));
159
memset(buf + crc_offset, 0, sizeof(crc));
160
crc = crc32c(0xffffffff, buf, size);
161
crc = cpu_to_le32(crc);
162
memcpy(buf + crc_offset, &crc, sizeof(crc));
167
uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
174
if (crc_offset > 0) {
175
memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
176
memset(buf + crc_offset, 0, sizeof(crc_orig));
179
crc_new = crc32c(crc, buf, size);
180
if (crc_offset > 0) {
181
memcpy(buf + crc_offset, &crc_orig, sizeof(crc_orig));
200
bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset)
206
assert(size > (crc_offset + 4));
208
memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
209
crc_orig = le32_to_cpu(crc_orig);
211
crc = vhdx_checksum_calc(0xffffffff, buf, size, crc_offset);
213
return crc == crc_orig;
221
void vhdx_guid_generate(MSGUID *guid)
224
assert(guid != NULL);
226
qemu_uuid_generate(&uuid);
227
memcpy(guid, &uuid, sizeof(MSGUID));
231
static int vhdx_region_check(BDRVVHDXState *s, uint64_t start, uint64_t length)
237
end = start + length;
238
QLIST_FOREACH(r, &s->regions, entries) {
239
if (!((start >= r->end) || (end <= r->start))) {
240
error_report("VHDX region %" PRIu64 "-%" PRIu64 " overlaps with "
241
"region %" PRIu64 "-%." PRIu64, start, end, r->start,
253
static void vhdx_region_register(BDRVVHDXState *s,
254
uint64_t start, uint64_t length)
258
r = g_malloc0(sizeof(*r));
261
r->end = start + length;
263
QLIST_INSERT_HEAD(&s->regions, r, entries);
267
static void vhdx_region_unregister_all(BDRVVHDXState *s)
269
VHDXRegionEntry *r, *r_next;
271
QLIST_FOREACH_SAFE(r, &s->regions, entries, r_next) {
272
QLIST_REMOVE(r, entries);
277
static void vhdx_set_shift_bits(BDRVVHDXState *s)
279
s->logical_sector_size_bits = ctz32(s->logical_sector_size);
280
s->sectors_per_block_bits = ctz32(s->sectors_per_block);
281
s->chunk_ratio_bits = ctz64(s->chunk_ratio);
282
s->block_size_bits = ctz32(s->block_size);
296
static int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename)
298
if (buf_size >= 8 && !memcmp(buf, "vhdxfile", 8)) {
311
static int vhdx_write_header(BdrvChild *file, VHDXHeader *hdr,
312
uint64_t offset, bool read)
314
BlockDriverState *bs_file = file->bs;
315
uint8_t *buffer = NULL;
317
VHDXHeader *header_le;
319
assert(bs_file != NULL);
326
buffer = qemu_blockalign(bs_file, VHDX_HEADER_SIZE);
329
ret = bdrv_pread(file, offset, VHDX_HEADER_SIZE, buffer, 0);
334
memset(buffer, 0, VHDX_HEADER_SIZE);
338
header_le = (VHDXHeader *)buffer;
339
memcpy(header_le, hdr, sizeof(VHDXHeader));
340
vhdx_header_le_export(hdr, header_le);
341
vhdx_update_checksum(buffer, VHDX_HEADER_SIZE,
342
offsetof(VHDXHeader, checksum));
343
ret = bdrv_pwrite_sync(file, offset, sizeof(VHDXHeader), header_le, 0);
356
static int GRAPH_RDLOCK
357
vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
358
bool generate_data_write_guid, MSGUID *log_guid)
362
uint64_t header_offset = VHDX_HEADER1_OFFSET;
364
VHDXHeader *active_header;
365
VHDXHeader *inactive_header;
368
if (s->curr_header == 0) {
370
header_offset = VHDX_HEADER2_OFFSET;
373
active_header = s->headers[s->curr_header];
374
inactive_header = s->headers[hdr_idx];
376
inactive_header->sequence_number = active_header->sequence_number + 1;
380
inactive_header->file_write_guid = s->session_guid;
384
if (generate_data_write_guid) {
385
vhdx_guid_generate(&inactive_header->data_write_guid);
390
inactive_header->log_guid = *log_guid;
393
ret = vhdx_write_header(bs->file, inactive_header, header_offset, true);
397
s->curr_header = hdr_idx;
407
int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s,
408
bool generate_data_write_guid, MSGUID *log_guid)
412
ret = vhdx_update_header(bs, s, generate_data_write_guid, log_guid);
416
return vhdx_update_header(bs, s, generate_data_write_guid, log_guid);
420
static void GRAPH_RDLOCK
421
vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s, Error **errp)
426
bool h1_valid = false;
427
bool h2_valid = false;
433
header1 = qemu_blockalign(bs, sizeof(VHDXHeader));
434
header2 = qemu_blockalign(bs, sizeof(VHDXHeader));
436
buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE);
438
s->headers[0] = header1;
439
s->headers[1] = header2;
444
ret = bdrv_pread(bs->file, VHDX_HEADER1_OFFSET, VHDX_HEADER_SIZE, buffer,
450
memcpy(header1, buffer, sizeof(VHDXHeader));
452
if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) {
453
vhdx_header_le_import(header1);
454
if (header1->signature == VHDX_HEADER_SIGNATURE &&
455
header1->version == 1) {
456
h1_seq = header1->sequence_number;
461
ret = bdrv_pread(bs->file, VHDX_HEADER2_OFFSET, VHDX_HEADER_SIZE, buffer,
467
memcpy(header2, buffer, sizeof(VHDXHeader));
469
if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) {
470
vhdx_header_le_import(header2);
471
if (header2->signature == VHDX_HEADER_SIGNATURE &&
472
header2->version == 1) {
473
h2_seq = header2->sequence_number;
480
if (h1_valid && !h2_valid) {
482
} else if (!h1_valid && h2_valid) {
484
} else if (!h1_valid && !h2_valid) {
490
if (h1_seq > h2_seq) {
492
} else if (h2_seq > h1_seq) {
498
if (!memcmp(header1, header2, sizeof(VHDXHeader))) {
506
vhdx_region_register(s, s->headers[s->curr_header]->log_offset,
507
s->headers[s->curr_header]->log_length);
511
error_setg_errno(errp, -ret, "No valid VHDX header found");
514
s->headers[0] = NULL;
515
s->headers[1] = NULL;
521
static int GRAPH_RDLOCK
522
vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
527
VHDXRegionTableEntry rt_entry;
529
bool bat_rt_found = false;
530
bool metadata_rt_found = false;
534
buffer = qemu_blockalign(bs, VHDX_HEADER_BLOCK_SIZE);
536
ret = bdrv_pread(bs->file, VHDX_REGION_TABLE_OFFSET,
537
VHDX_HEADER_BLOCK_SIZE, buffer, 0);
541
memcpy(&s->rt, buffer, sizeof(s->rt));
542
offset += sizeof(s->rt);
544
if (!vhdx_checksum_is_valid(buffer, VHDX_HEADER_BLOCK_SIZE, 4)) {
549
vhdx_region_header_le_import(&s->rt);
551
if (s->rt.signature != VHDX_REGION_SIGNATURE) {
558
if (s->rt.entry_count > 2047) {
563
for (i = 0; i < s->rt.entry_count; i++) {
564
memcpy(&rt_entry, buffer + offset, sizeof(rt_entry));
565
offset += sizeof(rt_entry);
567
vhdx_region_entry_le_import(&rt_entry);
571
ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);
576
vhdx_region_register(s, rt_entry.file_offset, rt_entry.length);
579
if (guid_eq(rt_entry.guid, bat_guid)) {
586
s->bat_rt = rt_entry;
590
if (guid_eq(rt_entry.guid, metadata_guid)) {
592
if (metadata_rt_found) {
596
metadata_rt_found = true;
597
s->metadata_rt = rt_entry;
601
if (rt_entry.data_bits & VHDX_REGION_ENTRY_REQUIRED) {
609
if (!bat_rt_found || !metadata_rt_found) {
639
static int GRAPH_RDLOCK
640
vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s)
646
VHDXMetadataTableEntry md_entry;
648
buffer = qemu_blockalign(bs, VHDX_METADATA_TABLE_MAX_SIZE);
650
ret = bdrv_pread(bs->file, s->metadata_rt.file_offset,
651
VHDX_METADATA_TABLE_MAX_SIZE, buffer, 0);
655
memcpy(&s->metadata_hdr, buffer, sizeof(s->metadata_hdr));
656
offset += sizeof(s->metadata_hdr);
658
vhdx_metadata_header_le_import(&s->metadata_hdr);
660
if (s->metadata_hdr.signature != VHDX_METADATA_SIGNATURE) {
665
s->metadata_entries.present = 0;
667
if ((s->metadata_hdr.entry_count * sizeof(md_entry)) >
668
(VHDX_METADATA_TABLE_MAX_SIZE - offset)) {
673
for (i = 0; i < s->metadata_hdr.entry_count; i++) {
674
memcpy(&md_entry, buffer + offset, sizeof(md_entry));
675
offset += sizeof(md_entry);
677
vhdx_metadata_entry_le_import(&md_entry);
679
if (guid_eq(md_entry.item_id, file_param_guid)) {
680
if (s->metadata_entries.present & META_FILE_PARAMETER_PRESENT) {
684
s->metadata_entries.file_parameters_entry = md_entry;
685
s->metadata_entries.present |= META_FILE_PARAMETER_PRESENT;
689
if (guid_eq(md_entry.item_id, virtual_size_guid)) {
690
if (s->metadata_entries.present & META_VIRTUAL_DISK_SIZE_PRESENT) {
694
s->metadata_entries.virtual_disk_size_entry = md_entry;
695
s->metadata_entries.present |= META_VIRTUAL_DISK_SIZE_PRESENT;
699
if (guid_eq(md_entry.item_id, page83_guid)) {
700
if (s->metadata_entries.present & META_PAGE_83_PRESENT) {
704
s->metadata_entries.page83_data_entry = md_entry;
705
s->metadata_entries.present |= META_PAGE_83_PRESENT;
709
if (guid_eq(md_entry.item_id, logical_sector_guid)) {
710
if (s->metadata_entries.present &
711
META_LOGICAL_SECTOR_SIZE_PRESENT) {
715
s->metadata_entries.logical_sector_size_entry = md_entry;
716
s->metadata_entries.present |= META_LOGICAL_SECTOR_SIZE_PRESENT;
720
if (guid_eq(md_entry.item_id, phys_sector_guid)) {
721
if (s->metadata_entries.present & META_PHYS_SECTOR_SIZE_PRESENT) {
725
s->metadata_entries.phys_sector_size_entry = md_entry;
726
s->metadata_entries.present |= META_PHYS_SECTOR_SIZE_PRESENT;
730
if (guid_eq(md_entry.item_id, parent_locator_guid)) {
731
if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
735
s->metadata_entries.parent_locator_entry = md_entry;
736
s->metadata_entries.present |= META_PARENT_LOCATOR_PRESENT;
740
if (md_entry.data_bits & VHDX_META_FLAGS_IS_REQUIRED) {
748
if (s->metadata_entries.present != META_ALL_PRESENT) {
753
ret = bdrv_pread(bs->file,
754
s->metadata_entries.file_parameters_entry.offset
755
+ s->metadata_rt.file_offset,
764
s->params.block_size = le32_to_cpu(s->params.block_size);
765
s->params.data_bits = le32_to_cpu(s->params.data_bits);
773
if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
774
if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
789
ret = bdrv_pread(bs->file,
790
s->metadata_entries.virtual_disk_size_entry.offset
791
+ s->metadata_rt.file_offset,
793
&s->virtual_disk_size,
798
ret = bdrv_pread(bs->file,
799
s->metadata_entries.logical_sector_size_entry.offset
800
+ s->metadata_rt.file_offset,
802
&s->logical_sector_size,
807
ret = bdrv_pread(bs->file,
808
s->metadata_entries.phys_sector_size_entry.offset
809
+ s->metadata_rt.file_offset,
811
&s->physical_sector_size,
817
s->virtual_disk_size = le64_to_cpu(s->virtual_disk_size);
818
s->logical_sector_size = le32_to_cpu(s->logical_sector_size);
819
s->physical_sector_size = le32_to_cpu(s->physical_sector_size);
821
if (s->params.block_size < VHDX_BLOCK_SIZE_MIN ||
822
s->params.block_size > VHDX_BLOCK_SIZE_MAX) {
828
if (s->logical_sector_size != 512) {
835
s->sectors_per_block = s->params.block_size / s->logical_sector_size;
836
s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) *
837
(uint64_t)s->logical_sector_size /
838
(uint64_t)s->params.block_size;
844
if (s->logical_sector_size & (s->logical_sector_size - 1)) {
848
if (s->sectors_per_block & (s->sectors_per_block - 1)) {
852
if (s->chunk_ratio & (s->chunk_ratio - 1)) {
856
s->block_size = s->params.block_size;
857
if (s->block_size & (s->block_size - 1)) {
862
vhdx_set_shift_bits(s);
875
static void vhdx_calc_bat_entries(BDRVVHDXState *s)
877
uint32_t data_blocks_cnt, bitmap_blocks_cnt;
879
data_blocks_cnt = DIV_ROUND_UP(s->virtual_disk_size, s->block_size);
880
bitmap_blocks_cnt = DIV_ROUND_UP(data_blocks_cnt, s->chunk_ratio);
882
if (s->parent_entries) {
883
s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1);
885
s->bat_entries = data_blocks_cnt +
886
((data_blocks_cnt - 1) >> s->chunk_ratio_bits);
891
static int coroutine_mixed_fn GRAPH_RDLOCK
892
vhdx_check_bat_entries(BlockDriverState *bs, int *errcnt)
894
BDRVVHDXState *s = bs->opaque;
895
int64_t image_file_size = bdrv_getlength(bs->file->bs);
896
uint64_t payblocks = s->chunk_ratio;
900
if (image_file_size < 0) {
901
error_report("Could not determinate VHDX image file size.");
902
return image_file_size;
905
for (i = 0; i < s->bat_entries; i++) {
906
if ((s->bat[i] & VHDX_BAT_STATE_BIT_MASK) ==
907
PAYLOAD_BLOCK_FULLY_PRESENT) {
908
uint64_t offset = s->bat[i] & VHDX_BAT_FILE_OFF_MASK;
914
uint32_t block_length = MIN(s->block_size,
915
bs->total_sectors * BDRV_SECTOR_SIZE - i * s->block_size);
919
if (offset > INT64_MAX - s->block_size) {
920
error_report("VHDX BAT entry %" PRIu64 " offset overflow.", i);
931
if (offset >= image_file_size) {
932
error_report("VHDX BAT entry %" PRIu64 " start offset %" PRIu64
933
" points after end of file (%" PRIi64 "). Image"
934
" has probably been truncated.",
935
i, offset, image_file_size);
941
} else if (offset + block_length > image_file_size) {
942
error_report("VHDX BAT entry %" PRIu64 " end offset %" PRIu64
943
" points after end of file (%" PRIi64 "). Image"
944
" has probably been truncated.",
945
i, offset + block_length - 1, image_file_size);
960
ret2 = vhdx_region_check(s, offset, s->block_size);
969
payblocks = s->chunk_ratio;
981
static void vhdx_close(BlockDriverState *bs)
983
BDRVVHDXState *s = bs->opaque;
984
qemu_vfree(s->headers[0]);
985
s->headers[0] = NULL;
986
qemu_vfree(s->headers[1]);
987
s->headers[1] = NULL;
990
qemu_vfree(s->parent_entries);
991
s->parent_entries = NULL;
992
migrate_del_blocker(&s->migration_blocker);
993
qemu_vfree(s->log.hdr);
995
vhdx_region_unregister_all(s);
998
static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
1001
BDRVVHDXState *s = bs->opaque;
1005
Error *local_err = NULL;
1007
GLOBAL_STATE_CODE();
1009
ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
1014
GRAPH_RDLOCK_GUARD_MAINLOOP();
1017
s->first_visible_write = true;
1019
qemu_co_mutex_init(&s->lock);
1020
QLIST_INIT(&s->regions);
1023
ret = bdrv_pread(bs->file, 0, sizeof(uint64_t), &signature, 0);
1027
if (memcmp(&signature, "vhdxfile", 8)) {
1035
vhdx_guid_generate(&s->session_guid);
1037
vhdx_parse_header(bs, s, &local_err);
1038
if (local_err != NULL) {
1039
error_propagate(errp, local_err);
1044
ret = vhdx_parse_log(bs, s, &s->log_replayed_on_open, errp);
1049
ret = vhdx_open_region_tables(bs, s);
1054
ret = vhdx_parse_metadata(bs, s);
1059
s->block_size = s->params.block_size;
1063
bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits;
1065
vhdx_calc_bat_entries(s);
1067
s->bat_offset = s->bat_rt.file_offset;
1069
if (s->bat_entries > s->bat_rt.length / sizeof(VHDXBatEntry)) {
1076
s->bat = qemu_try_blockalign(bs->file->bs, s->bat_rt.length);
1077
if (s->bat == NULL) {
1082
ret = bdrv_pread(bs->file, s->bat_offset, s->bat_rt.length, s->bat, 0);
1088
for (i = 0; i < s->bat_entries; i++) {
1089
s->bat[i] = le64_to_cpu(s->bat[i]);
1092
if (!(flags & BDRV_O_CHECK)) {
1093
ret = vhdx_check_bat_entries(bs, NULL);
1100
error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
1101
"does not support live migration",
1102
bdrv_get_device_or_node_name(bs));
1103
ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
1116
static int vhdx_reopen_prepare(BDRVReopenState *state,
1117
BlockReopenQueue *queue, Error **errp)
1127
static void vhdx_block_translate(BDRVVHDXState *s, int64_t sector_num,
1128
int nb_sectors, VHDXSectorInfo *sinfo)
1130
uint32_t block_offset;
1132
sinfo->bat_idx = sector_num >> s->sectors_per_block_bits;
1135
block_offset = sector_num - (sinfo->bat_idx << s->sectors_per_block_bits);
1139
sinfo->bat_idx += sinfo->bat_idx >> s->chunk_ratio_bits;
1142
sinfo->sectors_avail = s->sectors_per_block - block_offset;
1144
sinfo->bytes_left = sinfo->sectors_avail << s->logical_sector_size_bits;
1146
if (sinfo->sectors_avail > nb_sectors) {
1147
sinfo->sectors_avail = nb_sectors;
1150
sinfo->bytes_avail = sinfo->sectors_avail << s->logical_sector_size_bits;
1152
sinfo->file_offset = s->bat[sinfo->bat_idx] & VHDX_BAT_FILE_OFF_MASK;
1154
sinfo->block_offset = block_offset << s->logical_sector_size_bits;
1157
if (sinfo->file_offset == 0) {
1166
sinfo->file_offset += sinfo->block_offset;
1170
static int coroutine_fn
1171
vhdx_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1173
BDRVVHDXState *s = bs->opaque;
1175
bdi->cluster_size = s->block_size;
1181
static int coroutine_fn GRAPH_RDLOCK
1182
vhdx_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1185
BDRVVHDXState *s = bs->opaque;
1187
VHDXSectorInfo sinfo;
1188
uint64_t bytes_done = 0;
1189
QEMUIOVector hd_qiov;
1191
qemu_iovec_init(&hd_qiov, qiov->niov);
1193
qemu_co_mutex_lock(&s->lock);
1195
while (nb_sectors > 0) {
1198
if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
1203
vhdx_block_translate(s, sector_num, nb_sectors, &sinfo);
1205
qemu_iovec_reset(&hd_qiov);
1206
qemu_iovec_concat(&hd_qiov, qiov, bytes_done, sinfo.bytes_avail);
1209
switch (s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK) {
1210
case PAYLOAD_BLOCK_NOT_PRESENT:
1211
case PAYLOAD_BLOCK_UNDEFINED:
1212
case PAYLOAD_BLOCK_UNMAPPED:
1213
case PAYLOAD_BLOCK_UNMAPPED_v095:
1214
case PAYLOAD_BLOCK_ZERO:
1216
qemu_iovec_memset(&hd_qiov, 0, 0, sinfo.bytes_avail);
1218
case PAYLOAD_BLOCK_FULLY_PRESENT:
1219
qemu_co_mutex_unlock(&s->lock);
1220
ret = bdrv_co_preadv(bs->file, sinfo.file_offset,
1221
sinfo.sectors_avail * BDRV_SECTOR_SIZE,
1223
qemu_co_mutex_lock(&s->lock);
1228
case PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1236
nb_sectors -= sinfo.sectors_avail;
1237
sector_num += sinfo.sectors_avail;
1238
bytes_done += sinfo.bytes_avail;
1243
qemu_co_mutex_unlock(&s->lock);
1244
qemu_iovec_destroy(&hd_qiov);
1259
static int coroutine_fn GRAPH_RDLOCK
1260
vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
1261
uint64_t *new_offset, bool *need_zero)
1263
int64_t current_len;
1265
current_len = bdrv_co_getlength(bs->file->bs);
1266
if (current_len < 0) {
1270
*new_offset = current_len;
1273
*new_offset = ROUND_UP(*new_offset, 1 * MiB);
1274
if (*new_offset > INT64_MAX) {
1281
ret = bdrv_co_truncate(bs->file, *new_offset + s->block_size, false,
1282
PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE, NULL);
1283
if (ret != -ENOTSUP) {
1289
return bdrv_co_truncate(bs->file, *new_offset + s->block_size, false,
1290
PREALLOC_MODE_OFF, 0, NULL);
1296
static void vhdx_update_bat_table_entry(BlockDriverState *bs, BDRVVHDXState *s,
1297
VHDXSectorInfo *sinfo,
1298
uint64_t *bat_entry_le,
1299
uint64_t *bat_offset, int state)
1303
if ((state == PAYLOAD_BLOCK_ZERO) ||
1304
(state == PAYLOAD_BLOCK_UNDEFINED) ||
1305
(state == PAYLOAD_BLOCK_NOT_PRESENT) ||
1306
(state == PAYLOAD_BLOCK_UNMAPPED)) {
1307
s->bat[sinfo->bat_idx] = 0;
1313
s->bat[sinfo->bat_idx] = sinfo->file_offset;
1316
s->bat[sinfo->bat_idx] |= state & VHDX_BAT_STATE_BIT_MASK;
1318
*bat_entry_le = cpu_to_le64(s->bat[sinfo->bat_idx]);
1319
*bat_offset = s->bat_offset + sinfo->bat_idx * sizeof(VHDXBatEntry);
1325
int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s)
1328
if (s->first_visible_write) {
1329
s->first_visible_write = false;
1330
ret = vhdx_update_headers(bs, s, true, NULL);
1335
static int coroutine_fn GRAPH_RDLOCK
1336
vhdx_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1337
QEMUIOVector *qiov, int flags)
1340
BDRVVHDXState *s = bs->opaque;
1341
VHDXSectorInfo sinfo;
1342
uint64_t bytes_done = 0;
1343
uint64_t bat_entry = 0;
1344
uint64_t bat_entry_offset = 0;
1345
QEMUIOVector hd_qiov;
1346
struct iovec iov1 = { 0 };
1347
struct iovec iov2 = { 0 };
1348
int sectors_to_write;
1350
uint64_t bat_prior_offset = 0;
1351
bool bat_update = false;
1353
qemu_iovec_init(&hd_qiov, qiov->niov);
1355
qemu_co_mutex_lock(&s->lock);
1357
ret = vhdx_user_visible_write(bs, s);
1362
while (nb_sectors > 0) {
1363
bool use_zero_buffers = false;
1365
if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
1370
vhdx_block_translate(s, sector_num, nb_sectors, &sinfo);
1371
sectors_to_write = sinfo.sectors_avail;
1373
qemu_iovec_reset(&hd_qiov);
1375
bat_state = s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK;
1376
switch (bat_state) {
1377
case PAYLOAD_BLOCK_ZERO:
1381
use_zero_buffers = true;
1383
case PAYLOAD_BLOCK_NOT_PRESENT:
1384
case PAYLOAD_BLOCK_UNMAPPED:
1385
case PAYLOAD_BLOCK_UNMAPPED_v095:
1386
case PAYLOAD_BLOCK_UNDEFINED:
1387
bat_prior_offset = sinfo.file_offset;
1388
ret = vhdx_allocate_block(bs, s, &sinfo.file_offset,
1398
vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry,
1400
PAYLOAD_BLOCK_FULLY_PRESENT);
1410
if (!use_zero_buffers) {
1411
sinfo.file_offset += sinfo.block_offset;
1414
if (sinfo.block_offset) {
1415
iov1.iov_len = sinfo.block_offset;
1416
iov1.iov_base = qemu_blockalign(bs, iov1.iov_len);
1417
memset(iov1.iov_base, 0, iov1.iov_len);
1418
qemu_iovec_concat_iov(&hd_qiov, &iov1, 1, 0,
1420
sectors_to_write += iov1.iov_len >> BDRV_SECTOR_BITS;
1424
qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1428
if ((sinfo.bytes_avail - sinfo.block_offset) <
1430
iov2.iov_len = s->block_size -
1431
(sinfo.bytes_avail + sinfo.block_offset);
1432
iov2.iov_base = qemu_blockalign(bs, iov2.iov_len);
1433
memset(iov2.iov_base, 0, iov2.iov_len);
1434
qemu_iovec_concat_iov(&hd_qiov, &iov2, 1, 0,
1436
sectors_to_write += iov2.iov_len >> BDRV_SECTOR_BITS;
1441
case PAYLOAD_BLOCK_FULLY_PRESENT:
1444
if (sinfo.file_offset < (1 * MiB)) {
1446
goto error_bat_restore;
1449
if (!use_zero_buffers) {
1450
qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1454
qemu_co_mutex_unlock(&s->lock);
1455
ret = bdrv_co_pwritev(bs->file, sinfo.file_offset,
1456
sectors_to_write * BDRV_SECTOR_SIZE,
1458
qemu_co_mutex_lock(&s->lock);
1460
goto error_bat_restore;
1463
case PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1475
ret = vhdx_log_write_and_flush(bs, s, &bat_entry,
1476
sizeof(VHDXBatEntry),
1483
nb_sectors -= sinfo.sectors_avail;
1484
sector_num += sinfo.sectors_avail;
1485
bytes_done += sinfo.bytes_avail;
1496
sinfo.file_offset = bat_prior_offset;
1497
vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry,
1498
&bat_entry_offset, bat_state);
1501
qemu_vfree(iov1.iov_base);
1502
qemu_vfree(iov2.iov_base);
1503
qemu_co_mutex_unlock(&s->lock);
1504
qemu_iovec_destroy(&hd_qiov);
1516
static int coroutine_fn GRAPH_UNLOCKED
1517
vhdx_create_new_headers(BlockBackend *blk, uint64_t image_size,
1520
BlockDriverState *bs = blk_bs(blk);
1523
VHDXHeader *hdr = NULL;
1525
GRAPH_RDLOCK_GUARD();
1527
hdr = g_new0(VHDXHeader, 1);
1529
hdr->signature = VHDX_HEADER_SIGNATURE;
1530
hdr->sequence_number = g_random_int();
1531
hdr->log_version = 0;
1533
hdr->log_length = log_size;
1534
hdr->log_offset = VHDX_HEADER_SECTION_END;
1535
vhdx_guid_generate(&hdr->file_write_guid);
1536
vhdx_guid_generate(&hdr->data_write_guid);
1541
child = QLIST_FIRST(&bs->parents);
1542
assert(!QLIST_NEXT(child, next_parent));
1544
ret = vhdx_write_header(child, hdr, VHDX_HEADER1_OFFSET, false);
1548
hdr->sequence_number++;
1549
ret = vhdx_write_header(child, hdr, VHDX_HEADER2_OFFSET, false);
1559
#define VHDX_METADATA_ENTRY_BUFFER_SIZE \
1560
(sizeof(VHDXFileParameters) +\
1561
sizeof(VHDXVirtualDiskSize) +\
1562
sizeof(VHDXPage83Data) +\
1563
sizeof(VHDXVirtualDiskLogicalSectorSize) +\
1564
sizeof(VHDXVirtualDiskPhysicalSectorSize))
1582
static int coroutine_fn
1583
vhdx_create_new_metadata(BlockBackend *blk, uint64_t image_size,
1584
uint32_t block_size, uint32_t sector_size,
1585
uint64_t metadata_offset, VHDXImageType type)
1588
uint32_t offset = 0;
1589
void *buffer = NULL;
1591
VHDXMetadataTableHeader *md_table;
1592
VHDXMetadataTableEntry *md_table_entry;
1595
VHDXFileParameters *mt_file_params;
1596
VHDXVirtualDiskSize *mt_virtual_size;
1597
VHDXPage83Data *mt_page83;
1598
VHDXVirtualDiskLogicalSectorSize *mt_log_sector_size;
1599
VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size;
1601
entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE);
1603
mt_file_params = entry_buffer;
1604
offset += sizeof(VHDXFileParameters);
1605
mt_virtual_size = entry_buffer + offset;
1606
offset += sizeof(VHDXVirtualDiskSize);
1607
mt_page83 = entry_buffer + offset;
1608
offset += sizeof(VHDXPage83Data);
1609
mt_log_sector_size = entry_buffer + offset;
1610
offset += sizeof(VHDXVirtualDiskLogicalSectorSize);
1611
mt_phys_sector_size = entry_buffer + offset;
1613
mt_file_params->block_size = cpu_to_le32(block_size);
1614
if (type == VHDX_TYPE_FIXED) {
1615
mt_file_params->data_bits |= VHDX_PARAMS_LEAVE_BLOCKS_ALLOCED;
1616
mt_file_params->data_bits = cpu_to_le32(mt_file_params->data_bits);
1619
vhdx_guid_generate(&mt_page83->page_83_data);
1620
cpu_to_leguids(&mt_page83->page_83_data);
1621
mt_virtual_size->virtual_disk_size = cpu_to_le64(image_size);
1622
mt_log_sector_size->logical_sector_size = cpu_to_le32(sector_size);
1623
mt_phys_sector_size->physical_sector_size = cpu_to_le32(sector_size);
1625
buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE);
1628
md_table->signature = VHDX_METADATA_SIGNATURE;
1629
md_table->entry_count = 5;
1630
vhdx_metadata_header_le_export(md_table);
1636
md_table_entry = buffer + sizeof(VHDXMetadataTableHeader);
1638
md_table_entry[0].item_id = file_param_guid;
1639
md_table_entry[0].offset = offset;
1640
md_table_entry[0].length = sizeof(VHDXFileParameters);
1641
md_table_entry[0].data_bits |= VHDX_META_FLAGS_IS_REQUIRED;
1642
offset += md_table_entry[0].length;
1643
vhdx_metadata_entry_le_export(&md_table_entry[0]);
1645
md_table_entry[1].item_id = virtual_size_guid;
1646
md_table_entry[1].offset = offset;
1647
md_table_entry[1].length = sizeof(VHDXVirtualDiskSize);
1648
md_table_entry[1].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1649
VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1650
offset += md_table_entry[1].length;
1651
vhdx_metadata_entry_le_export(&md_table_entry[1]);
1653
md_table_entry[2].item_id = page83_guid;
1654
md_table_entry[2].offset = offset;
1655
md_table_entry[2].length = sizeof(VHDXPage83Data);
1656
md_table_entry[2].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1657
VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1658
offset += md_table_entry[2].length;
1659
vhdx_metadata_entry_le_export(&md_table_entry[2]);
1661
md_table_entry[3].item_id = logical_sector_guid;
1662
md_table_entry[3].offset = offset;
1663
md_table_entry[3].length = sizeof(VHDXVirtualDiskLogicalSectorSize);
1664
md_table_entry[3].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1665
VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1666
offset += md_table_entry[3].length;
1667
vhdx_metadata_entry_le_export(&md_table_entry[3]);
1669
md_table_entry[4].item_id = phys_sector_guid;
1670
md_table_entry[4].offset = offset;
1671
md_table_entry[4].length = sizeof(VHDXVirtualDiskPhysicalSectorSize);
1672
md_table_entry[4].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1673
VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1674
vhdx_metadata_entry_le_export(&md_table_entry[4]);
1676
ret = blk_co_pwrite(blk, metadata_offset, VHDX_HEADER_BLOCK_SIZE, buffer, 0);
1681
ret = blk_co_pwrite(blk, metadata_offset + (64 * KiB),
1682
VHDX_METADATA_ENTRY_BUFFER_SIZE, entry_buffer, 0);
1690
g_free(entry_buffer);
1702
static int coroutine_fn GRAPH_UNLOCKED
1703
vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
1704
uint64_t image_size, VHDXImageType type,
1705
bool use_zero_blocks, uint64_t file_offset,
1706
uint32_t length, Error **errp)
1709
uint64_t data_file_offset;
1710
uint64_t total_sectors = 0;
1711
uint64_t sector_num = 0;
1714
VHDXSectorInfo sinfo;
1717
assert(s->bat == NULL);
1722
data_file_offset = file_offset + length + 5 * MiB;
1723
total_sectors = image_size >> s->logical_sector_size_bits;
1725
if (type == VHDX_TYPE_DYNAMIC) {
1728
ret = blk_co_truncate(blk, data_file_offset, false, PREALLOC_MODE_OFF,
1733
} else if (type == VHDX_TYPE_FIXED) {
1734
ret = blk_co_truncate(blk, data_file_offset + image_size, false,
1735
PREALLOC_MODE_OFF, 0, errp);
1740
error_setg(errp, "Unsupported image type");
1745
bdrv_graph_co_rdlock();
1746
has_zero_init = bdrv_has_zero_init(blk_bs(blk));
1747
bdrv_graph_co_rdunlock();
1749
if (type == VHDX_TYPE_FIXED ||
1751
has_zero_init == 0) {
1753
s->bat = g_try_malloc0(length);
1754
if (length && s->bat == NULL) {
1755
error_setg(errp, "Failed to allocate memory for the BAT");
1759
block_state = type == VHDX_TYPE_FIXED ? PAYLOAD_BLOCK_FULLY_PRESENT :
1760
PAYLOAD_BLOCK_NOT_PRESENT;
1761
block_state = use_zero_blocks ? PAYLOAD_BLOCK_ZERO : block_state;
1763
while (sector_num < total_sectors) {
1764
vhdx_block_translate(s, sector_num, s->sectors_per_block, &sinfo);
1765
sinfo.file_offset = data_file_offset +
1766
(sector_num << s->logical_sector_size_bits);
1767
sinfo.file_offset = ROUND_UP(sinfo.file_offset, MiB);
1768
vhdx_update_bat_table_entry(blk_bs(blk), s, &sinfo, &unused, &unused,
1770
s->bat[sinfo.bat_idx] = cpu_to_le64(s->bat[sinfo.bat_idx]);
1771
sector_num += s->sectors_per_block;
1773
ret = blk_co_pwrite(blk, file_offset, length, s->bat, 0);
1775
error_setg_errno(errp, -ret, "Failed to write the BAT");
1794
static int coroutine_fn GRAPH_UNLOCKED
1795
vhdx_create_new_region_table(BlockBackend *blk, uint64_t image_size,
1796
uint32_t block_size, uint32_t sector_size,
1797
uint32_t log_size, bool use_zero_blocks,
1798
VHDXImageType type, uint64_t *metadata_offset,
1802
uint32_t offset = 0;
1803
void *buffer = NULL;
1804
uint64_t bat_file_offset;
1805
uint32_t bat_length;
1806
BDRVVHDXState *s = NULL;
1807
VHDXRegionTableHeader *region_table;
1808
VHDXRegionTableEntry *rt_bat;
1809
VHDXRegionTableEntry *rt_metadata;
1811
assert(metadata_offset != NULL);
1815
s = g_new0(BDRVVHDXState, 1);
1817
s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) *
1818
(uint64_t) sector_size / (uint64_t) block_size;
1820
s->sectors_per_block = block_size / sector_size;
1821
s->virtual_disk_size = image_size;
1822
s->block_size = block_size;
1823
s->logical_sector_size = sector_size;
1825
vhdx_set_shift_bits(s);
1827
vhdx_calc_bat_entries(s);
1833
buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE);
1834
region_table = buffer;
1835
offset += sizeof(VHDXRegionTableHeader);
1836
rt_bat = buffer + offset;
1837
offset += sizeof(VHDXRegionTableEntry);
1838
rt_metadata = buffer + offset;
1840
region_table->signature = VHDX_REGION_SIGNATURE;
1841
region_table->entry_count = 2;
1843
rt_bat->guid = bat_guid;
1844
rt_bat->length = ROUND_UP(s->bat_entries * sizeof(VHDXBatEntry), MiB);
1845
rt_bat->file_offset = ROUND_UP(VHDX_HEADER_SECTION_END + log_size, MiB);
1846
s->bat_offset = rt_bat->file_offset;
1848
rt_metadata->guid = metadata_guid;
1849
rt_metadata->file_offset = ROUND_UP(rt_bat->file_offset + rt_bat->length,
1851
rt_metadata->length = 1 * MiB;
1852
*metadata_offset = rt_metadata->file_offset;
1854
bat_file_offset = rt_bat->file_offset;
1855
bat_length = rt_bat->length;
1857
vhdx_region_header_le_export(region_table);
1858
vhdx_region_entry_le_export(rt_bat);
1859
vhdx_region_entry_le_export(rt_metadata);
1861
vhdx_update_checksum(buffer, VHDX_HEADER_BLOCK_SIZE,
1862
offsetof(VHDXRegionTableHeader, checksum));
1867
ret = vhdx_create_bat(blk, s, image_size, type, use_zero_blocks,
1868
bat_file_offset, bat_length, errp);
1874
ret = blk_co_pwrite(blk, VHDX_REGION_TABLE_OFFSET, VHDX_HEADER_BLOCK_SIZE,
1877
error_setg_errno(errp, -ret, "Failed to write first region table");
1881
ret = blk_co_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, VHDX_HEADER_BLOCK_SIZE,
1884
error_setg_errno(errp, -ret, "Failed to write second region table");
1911
static int coroutine_fn GRAPH_UNLOCKED
1912
vhdx_co_create(BlockdevCreateOptions *opts, Error **errp)
1914
BlockdevCreateOptionsVhdx *vhdx_opts;
1915
BlockBackend *blk = NULL;
1916
BlockDriverState *bs = NULL;
1919
uint64_t image_size;
1921
uint32_t block_size;
1923
uint64_t metadata_offset;
1924
bool use_zero_blocks = false;
1926
gunichar2 *creator = NULL;
1927
glong creator_items;
1928
VHDXImageType image_type;
1930
assert(opts->driver == BLOCKDEV_DRIVER_VHDX);
1931
vhdx_opts = &opts->u.vhdx;
1934
image_size = vhdx_opts->size;
1935
if (image_size > VHDX_MAX_IMAGE_SIZE) {
1936
error_setg(errp, "Image size too large; max of 64TB");
1940
if (!vhdx_opts->has_log_size) {
1941
log_size = DEFAULT_LOG_SIZE;
1943
if (vhdx_opts->log_size > UINT32_MAX) {
1944
error_setg(errp, "Log size must be smaller than 4 GB");
1947
log_size = vhdx_opts->log_size;
1949
if (log_size < MiB || (log_size % MiB) != 0) {
1950
error_setg(errp, "Log size must be a multiple of 1 MB");
1954
if (!vhdx_opts->has_block_state_zero) {
1955
use_zero_blocks = true;
1957
use_zero_blocks = vhdx_opts->block_state_zero;
1960
if (!vhdx_opts->has_subformat) {
1961
vhdx_opts->subformat = BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC;
1964
switch (vhdx_opts->subformat) {
1965
case BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC:
1966
image_type = VHDX_TYPE_DYNAMIC;
1968
case BLOCKDEV_VHDX_SUBFORMAT_FIXED:
1969
image_type = VHDX_TYPE_FIXED;
1972
g_assert_not_reached();
1977
if (vhdx_opts->has_block_size) {
1978
block_size = vhdx_opts->block_size;
1980
if (image_size > 32 * TiB) {
1981
block_size = 64 * MiB;
1982
} else if (image_size > (uint64_t) 100 * GiB) {
1983
block_size = 32 * MiB;
1984
} else if (image_size > 1 * GiB) {
1985
block_size = 16 * MiB;
1987
block_size = 8 * MiB;
1991
if (block_size < MiB || (block_size % MiB) != 0) {
1992
error_setg(errp, "Block size must be a multiple of 1 MB");
1995
if (!is_power_of_2(block_size)) {
1996
error_setg(errp, "Block size must be a power of two");
1999
if (block_size > VHDX_BLOCK_SIZE_MAX) {
2000
error_setg(errp, "Block size must not exceed %" PRId64,
2001
VHDX_BLOCK_SIZE_MAX);
2006
bs = bdrv_co_open_blockdev_ref(vhdx_opts->file, errp);
2011
blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
2015
goto delete_and_exit;
2017
blk_set_allow_write_beyond_eof(blk, true);
2023
creator = g_utf8_to_utf16("QEMU v" QEMU_VERSION, -1, NULL,
2024
&creator_items, NULL);
2025
signature = cpu_to_le64(VHDX_FILE_SIGNATURE);
2026
ret = blk_co_pwrite(blk, VHDX_FILE_ID_OFFSET, sizeof(signature), &signature,
2029
error_setg_errno(errp, -ret, "Failed to write file signature");
2030
goto delete_and_exit;
2033
ret = blk_co_pwrite(blk, VHDX_FILE_ID_OFFSET + sizeof(signature),
2034
creator_items * sizeof(gunichar2), creator, 0);
2036
error_setg_errno(errp, -ret, "Failed to write creator field");
2037
goto delete_and_exit;
2043
ret = vhdx_create_new_headers(blk, image_size, log_size);
2045
error_setg_errno(errp, -ret, "Failed to write image headers");
2046
goto delete_and_exit;
2050
ret = vhdx_create_new_region_table(blk, image_size, block_size, 512,
2051
log_size, use_zero_blocks, image_type,
2052
&metadata_offset, errp);
2054
goto delete_and_exit;
2058
ret = vhdx_create_new_metadata(blk, image_size, block_size, 512,
2059
metadata_offset, image_type);
2061
error_setg_errno(errp, -ret, "Failed to initialize metadata");
2062
goto delete_and_exit;
2073
static int coroutine_fn GRAPH_UNLOCKED
2074
vhdx_co_create_opts(BlockDriver *drv, const char *filename,
2075
QemuOpts *opts, Error **errp)
2077
BlockdevCreateOptions *create_options = NULL;
2080
BlockDriverState *bs = NULL;
2083
static const QDictRenames opt_renames[] = {
2084
{ VHDX_BLOCK_OPT_LOG_SIZE, "log-size" },
2085
{ VHDX_BLOCK_OPT_BLOCK_SIZE, "block-size" },
2086
{ VHDX_BLOCK_OPT_ZERO, "block-state-zero" },
2091
qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vhdx_create_opts, true);
2093
if (!qdict_rename_keys(qdict, opt_renames, errp)) {
2099
ret = bdrv_co_create_file(filename, opts, errp);
2104
bs = bdrv_co_open(filename, NULL, NULL,
2105
BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
2112
qdict_put_str(qdict, "driver", "vhdx");
2113
qdict_put_str(qdict, "file", bs->node_name);
2115
v = qobject_input_visitor_new_flat_confused(qdict, errp);
2121
visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
2123
if (!create_options) {
2133
assert(create_options->driver == BLOCKDEV_DRIVER_VHDX);
2134
create_options->u.vhdx.size =
2135
ROUND_UP(create_options->u.vhdx.size, BDRV_SECTOR_SIZE);
2137
if (create_options->u.vhdx.has_log_size) {
2138
create_options->u.vhdx.log_size =
2139
ROUND_UP(create_options->u.vhdx.log_size, MiB);
2141
if (create_options->u.vhdx.has_block_size) {
2142
create_options->u.vhdx.block_size =
2143
ROUND_UP(create_options->u.vhdx.block_size, MiB);
2145
if (create_options->u.vhdx.block_size == 0) {
2146
create_options->u.vhdx.has_block_size = false;
2148
if (create_options->u.vhdx.block_size > VHDX_BLOCK_SIZE_MAX) {
2149
create_options->u.vhdx.block_size = VHDX_BLOCK_SIZE_MAX;
2154
ret = vhdx_co_create(create_options, errp);
2157
qobject_unref(qdict);
2159
qapi_free_BlockdevCreateOptions(create_options);
2170
static int coroutine_fn GRAPH_RDLOCK
2171
vhdx_co_check(BlockDriverState *bs, BdrvCheckResult *result,
2174
BDRVVHDXState *s = bs->opaque;
2176
if (s->log_replayed_on_open) {
2177
result->corruptions_fixed++;
2180
vhdx_check_bat_entries(bs, &result->corruptions);
2185
static int GRAPH_RDLOCK vhdx_has_zero_init(BlockDriverState *bs)
2187
BDRVVHDXState *s = bs->opaque;
2195
if (!s->bat_entries) {
2199
state = s->bat[0] & VHDX_BAT_STATE_BIT_MASK;
2200
if (state == PAYLOAD_BLOCK_FULLY_PRESENT) {
2202
return bdrv_has_zero_init(bs->file->bs);
2209
static QemuOptsList vhdx_create_opts = {
2210
.name = "vhdx-create-opts",
2211
.head = QTAILQ_HEAD_INITIALIZER(vhdx_create_opts.head),
2214
.name = BLOCK_OPT_SIZE,
2215
.type = QEMU_OPT_SIZE,
2216
.help = "Virtual disk size; max of 64TB."
2219
.name = VHDX_BLOCK_OPT_LOG_SIZE,
2220
.type = QEMU_OPT_SIZE,
2221
.def_value_str = stringify(DEFAULT_LOG_SIZE),
2222
.help = "Log size; min 1MB."
2225
.name = VHDX_BLOCK_OPT_BLOCK_SIZE,
2226
.type = QEMU_OPT_SIZE,
2227
.def_value_str = stringify(0),
2228
.help = "Block Size; min 1MB, max 256MB. "
2229
"0 means auto-calculate based on image size."
2232
.name = BLOCK_OPT_SUBFMT,
2233
.type = QEMU_OPT_STRING,
2234
.help = "VHDX format type, can be either 'dynamic' or 'fixed'. "
2235
"Default is 'dynamic'."
2238
.name = VHDX_BLOCK_OPT_ZERO,
2239
.type = QEMU_OPT_BOOL,
2240
.help = "Force use of payload blocks of type 'ZERO'. "
2241
"Non-standard, but default. Do not set to 'off' when "
2242
"using 'qemu-img convert' with subformat=dynamic."
2248
static BlockDriver bdrv_vhdx = {
2249
.format_name = "vhdx",
2250
.instance_size = sizeof(BDRVVHDXState),
2251
.bdrv_probe = vhdx_probe,
2252
.bdrv_open = vhdx_open,
2253
.bdrv_close = vhdx_close,
2254
.bdrv_reopen_prepare = vhdx_reopen_prepare,
2255
.bdrv_child_perm = bdrv_default_perms,
2256
.bdrv_co_readv = vhdx_co_readv,
2257
.bdrv_co_writev = vhdx_co_writev,
2258
.bdrv_co_create = vhdx_co_create,
2259
.bdrv_co_create_opts = vhdx_co_create_opts,
2260
.bdrv_co_get_info = vhdx_co_get_info,
2261
.bdrv_co_check = vhdx_co_check,
2262
.bdrv_has_zero_init = vhdx_has_zero_init,
2265
.create_opts = &vhdx_create_opts,
2268
static void bdrv_vhdx_init(void)
2270
bdrv_register(&bdrv_vhdx);
2273
block_init(bdrv_vhdx_init);