2 * Block driver for the VMDK format
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "migration/migration.h"
32 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
33 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
34 #define VMDK4_COMPRESSION_DEFLATE 1
35 #define VMDK4_FLAG_NL_DETECT (1 << 0)
36 #define VMDK4_FLAG_RGD (1 << 1)
37 /* Zeroed-grain enable bit */
38 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
39 #define VMDK4_FLAG_COMPRESS (1 << 16)
40 #define VMDK4_FLAG_MARKER (1 << 17)
41 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
43 #define VMDK_GTE_ZEROED 0x1
45 /* VMDK internal error codes */
47 #define VMDK_ERROR (-1)
48 /* Cluster not allocated */
49 #define VMDK_UNALLOC (-2)
50 #define VMDK_ZEROED (-3)
52 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
57 uint32_t disk_sectors;
59 uint32_t l1dir_offset;
61 uint32_t file_sectors;
64 uint32_t sectors_per_track;
65 } QEMU_PACKED VMDK3Header;
74 /* Number of GrainTableEntries per GrainTable */
75 uint32_t num_gtes_per_gt;
78 uint64_t grain_offset;
81 uint16_t compressAlgorithm;
82 } QEMU_PACKED VMDK4Header;
84 #define L2_CACHE_SIZE 16
86 typedef struct VmdkExtent {
87 BlockDriverState *file;
95 int64_t flat_start_offset;
96 int64_t l1_table_offset;
97 int64_t l1_backup_table_offset;
99 uint32_t *l1_backup_table;
100 unsigned int l1_size;
101 uint32_t l1_entry_sectors;
103 unsigned int l2_size;
105 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
106 uint32_t l2_cache_counts[L2_CACHE_SIZE];
108 int64_t cluster_sectors;
112 typedef struct BDRVVmdkState {
114 uint64_t desc_offset;
120 /* Extent array with num_extents entries, ascend ordered by address */
122 Error *migration_blocker;
126 typedef struct VmdkMetaData {
128 unsigned int l1_index;
129 unsigned int l2_index;
130 unsigned int l2_offset;
132 uint32_t *l2_cache_entry;
135 typedef struct VmdkGrainMarker {
139 } QEMU_PACKED VmdkGrainMarker;
142 MARKER_END_OF_STREAM = 0,
143 MARKER_GRAIN_TABLE = 1,
144 MARKER_GRAIN_DIRECTORY = 2,
148 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
155 magic = be32_to_cpu(*(uint32_t *)buf);
156 if (magic == VMDK3_MAGIC ||
157 magic == VMDK4_MAGIC) {
160 const char *p = (const char *)buf;
161 const char *end = p + buf_size;
164 /* skip comment line */
165 while (p < end && *p != '\n') {
172 while (p < end && *p == ' ') {
175 /* skip '\r' if windows line endings used. */
176 if (p < end && *p == '\r') {
179 /* only accept blank lines before 'version=' line */
180 if (p == end || *p != '\n') {
186 if (end - p >= strlen("version=X\n")) {
187 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
188 strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
192 if (end - p >= strlen("version=X\r\n")) {
193 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
194 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
204 #define SECTOR_SIZE 512
205 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
206 #define BUF_SIZE 4096
207 #define HEADER_SIZE 512 /* first sector of 512 bytes */
209 static void vmdk_free_extents(BlockDriverState *bs)
212 BDRVVmdkState *s = bs->opaque;
215 for (i = 0; i < s->num_extents; i++) {
219 g_free(e->l1_backup_table);
221 if (e->file != bs->file) {
228 static void vmdk_free_last_extent(BlockDriverState *bs)
230 BDRVVmdkState *s = bs->opaque;
232 if (s->num_extents == 0) {
236 s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
239 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
241 char desc[DESC_SIZE];
242 uint32_t cid = 0xffffffff;
243 const char *p_name, *cid_str;
245 BDRVVmdkState *s = bs->opaque;
248 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
254 cid_str = "parentCID";
255 cid_str_size = sizeof("parentCID");
258 cid_str_size = sizeof("CID");
261 desc[DESC_SIZE - 1] = '\0';
262 p_name = strstr(desc, cid_str);
263 if (p_name != NULL) {
264 p_name += cid_str_size;
265 sscanf(p_name, "%x", &cid);
271 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
273 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
274 char *p_name, *tmp_str;
275 BDRVVmdkState *s = bs->opaque;
278 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
283 desc[DESC_SIZE - 1] = '\0';
284 tmp_str = strstr(desc, "parentCID");
285 if (tmp_str == NULL) {
289 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
290 p_name = strstr(desc, "CID");
291 if (p_name != NULL) {
292 p_name += sizeof("CID");
293 snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
294 pstrcat(desc, sizeof(desc), tmp_desc);
297 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
305 static int vmdk_is_cid_valid(BlockDriverState *bs)
307 BDRVVmdkState *s = bs->opaque;
308 BlockDriverState *p_bs = bs->backing_hd;
311 if (!s->cid_checked && p_bs) {
312 cur_pcid = vmdk_read_cid(p_bs, 0);
313 if (s->parent_cid != cur_pcid) {
318 s->cid_checked = true;
323 /* Queue extents, if any, for reopen() */
324 static int vmdk_reopen_prepare(BDRVReopenState *state,
325 BlockReopenQueue *queue, Error **errp)
332 assert(state != NULL);
333 assert(state->bs != NULL);
336 error_setg(errp, "No reopen queue for VMDK extents");
340 s = state->bs->opaque;
344 for (i = 0; i < s->num_extents; i++) {
346 if (e->file != state->bs->file) {
347 bdrv_reopen_queue(queue, e->file, state->flags);
356 static int vmdk_parent_open(BlockDriverState *bs)
359 char desc[DESC_SIZE + 1];
360 BDRVVmdkState *s = bs->opaque;
363 desc[DESC_SIZE] = '\0';
364 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
369 p_name = strstr(desc, "parentFileNameHint");
370 if (p_name != NULL) {
373 p_name += sizeof("parentFileNameHint") + 1;
374 end_name = strchr(p_name, '\"');
375 if (end_name == NULL) {
378 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
382 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
388 /* Create and append extent to the extent array. Return the added VmdkExtent
389 * address. return NULL if allocation failed. */
390 static int vmdk_add_extent(BlockDriverState *bs,
391 BlockDriverState *file, bool flat, int64_t sectors,
392 int64_t l1_offset, int64_t l1_backup_offset,
394 int l2_size, uint64_t cluster_sectors,
395 VmdkExtent **new_extent,
399 BDRVVmdkState *s = bs->opaque;
401 if (cluster_sectors > 0x200000) {
402 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
403 error_setg(errp, "Invalid granularity, image may be corrupt");
406 if (l1_size > 512 * 1024 * 1024) {
407 /* Although with big capacity and small l1_entry_sectors, we can get a
408 * big l1_size, we don't want unbounded value to allocate the table.
409 * Limit it to 512M, which is 16PB for default cluster and L2 table
411 error_setg(errp, "L1 size too big");
415 s->extents = g_realloc(s->extents,
416 (s->num_extents + 1) * sizeof(VmdkExtent));
417 extent = &s->extents[s->num_extents];
420 memset(extent, 0, sizeof(VmdkExtent));
423 extent->sectors = sectors;
424 extent->l1_table_offset = l1_offset;
425 extent->l1_backup_table_offset = l1_backup_offset;
426 extent->l1_size = l1_size;
427 extent->l1_entry_sectors = l2_size * cluster_sectors;
428 extent->l2_size = l2_size;
429 extent->cluster_sectors = flat ? sectors : cluster_sectors;
432 bs->bl.write_zeroes_alignment =
433 MAX(bs->bl.write_zeroes_alignment, cluster_sectors);
435 if (s->num_extents > 1) {
436 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
438 extent->end_sector = extent->sectors;
440 bs->total_sectors = extent->end_sector;
442 *new_extent = extent;
447 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
453 /* read the L1 table */
454 l1_size = extent->l1_size * sizeof(uint32_t);
455 extent->l1_table = g_malloc(l1_size);
456 ret = bdrv_pread(extent->file,
457 extent->l1_table_offset,
461 error_setg_errno(errp, -ret,
462 "Could not read l1 table from extent '%s'",
463 extent->file->filename);
466 for (i = 0; i < extent->l1_size; i++) {
467 le32_to_cpus(&extent->l1_table[i]);
470 if (extent->l1_backup_table_offset) {
471 extent->l1_backup_table = g_malloc(l1_size);
472 ret = bdrv_pread(extent->file,
473 extent->l1_backup_table_offset,
474 extent->l1_backup_table,
477 error_setg_errno(errp, -ret,
478 "Could not read l1 backup table from extent '%s'",
479 extent->file->filename);
482 for (i = 0; i < extent->l1_size; i++) {
483 le32_to_cpus(&extent->l1_backup_table[i]);
488 g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
491 g_free(extent->l1_backup_table);
493 g_free(extent->l1_table);
497 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
498 BlockDriverState *file,
499 int flags, Error **errp)
506 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
508 error_setg_errno(errp, -ret,
509 "Could not read header from file '%s'",
513 ret = vmdk_add_extent(bs, file, false,
514 le32_to_cpu(header.disk_sectors),
515 le32_to_cpu(header.l1dir_offset) << 9,
517 le32_to_cpu(header.l1dir_size),
519 le32_to_cpu(header.granularity),
525 ret = vmdk_init_tables(bs, extent, errp);
527 /* free extent allocated by vmdk_add_extent */
528 vmdk_free_last_extent(bs);
533 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
534 uint64_t desc_offset, Error **errp);
536 static int vmdk_open_vmdk4(BlockDriverState *bs,
537 BlockDriverState *file,
538 int flags, Error **errp)
542 uint32_t l1_size, l1_entry_sectors;
545 BDRVVmdkState *s = bs->opaque;
546 int64_t l1_backup_offset = 0;
548 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
550 error_setg_errno(errp, -ret,
551 "Could not read header from file '%s'",
554 if (header.capacity == 0) {
555 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
557 return vmdk_open_desc_file(bs, flags, desc_offset << 9, errp);
561 if (!s->create_type) {
562 s->create_type = g_strdup("monolithicSparse");
565 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
567 * The footer takes precedence over the header, so read it in. The
568 * footer starts at offset -1024 from the end: One sector for the
569 * footer, and another one for the end-of-stream marker.
576 uint8_t pad[512 - 16];
577 } QEMU_PACKED footer_marker;
581 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
587 uint8_t pad[512 - 16];
588 } QEMU_PACKED eos_marker;
589 } QEMU_PACKED footer;
591 ret = bdrv_pread(file,
592 bs->file->total_sectors * 512 - 1536,
593 &footer, sizeof(footer));
598 /* Some sanity checks for the footer */
599 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
600 le32_to_cpu(footer.footer_marker.size) != 0 ||
601 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
602 le64_to_cpu(footer.eos_marker.val) != 0 ||
603 le32_to_cpu(footer.eos_marker.size) != 0 ||
604 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
609 header = footer.header;
612 if (le32_to_cpu(header.version) > 3) {
614 snprintf(buf, sizeof(buf), "VMDK version %d",
615 le32_to_cpu(header.version));
616 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
617 bs->device_name, "vmdk", buf);
619 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
620 /* VMware KB 2064959 explains that version 3 added support for
621 * persistent changed block tracking (CBT), and backup software can
622 * read it as version=1 if it doesn't care about the changed area
623 * information. So we are safe to enable read only. */
624 error_setg(errp, "VMDK version 3 must be read only");
628 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
629 error_report("L2 table size too big");
633 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
634 * le64_to_cpu(header.granularity);
635 if (l1_entry_sectors == 0) {
638 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
640 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
641 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
643 ret = vmdk_add_extent(bs, file, false,
644 le64_to_cpu(header.capacity),
645 le64_to_cpu(header.gd_offset) << 9,
648 le32_to_cpu(header.num_gtes_per_gt),
649 le64_to_cpu(header.granularity),
656 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
657 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
658 extent->version = le32_to_cpu(header.version);
659 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
660 ret = vmdk_init_tables(bs, extent, errp);
662 /* free extent allocated by vmdk_add_extent */
663 vmdk_free_last_extent(bs);
668 /* find an option value out of descriptor file */
669 static int vmdk_parse_description(const char *desc, const char *opt_name,
670 char *buf, int buf_size)
672 char *opt_pos, *opt_end;
673 const char *end = desc + strlen(desc);
675 opt_pos = strstr(desc, opt_name);
679 /* Skip "=\"" following opt_name */
680 opt_pos += strlen(opt_name) + 2;
681 if (opt_pos >= end) {
685 while (opt_end < end && *opt_end != '"') {
688 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
691 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
695 /* Open an extent file and append to bs array */
696 static int vmdk_open_sparse(BlockDriverState *bs,
697 BlockDriverState *file,
698 int flags, Error **errp)
702 if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
706 magic = be32_to_cpu(magic);
709 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
712 return vmdk_open_vmdk4(bs, file, flags, errp);
720 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
721 const char *desc_file_path, Error **errp)
727 const char *p = desc;
730 char extent_path[PATH_MAX];
731 BlockDriverState *extent_file;
732 BDRVVmdkState *s = bs->opaque;
736 /* parse extent line:
737 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
739 * RW [size in sectors] SPARSE "file-name.vmdk"
742 ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
743 access, §ors, type, fname, &flat_offset);
744 if (ret < 4 || strcmp(access, "RW")) {
746 } else if (!strcmp(type, "FLAT")) {
747 if (ret != 5 || flat_offset < 0) {
748 error_setg(errp, "Invalid extent lines: \n%s", p);
751 } else if (!strcmp(type, "VMFS")) {
753 } else if (ret != 4) {
754 error_setg(errp, "Invalid extent lines: \n%s", p);
759 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
760 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
761 (strcmp(access, "RW"))) {
765 path_combine(extent_path, sizeof(extent_path),
766 desc_file_path, fname);
767 ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
773 /* save to extents array */
774 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
777 ret = vmdk_add_extent(bs, extent_file, true, sectors,
778 0, 0, 0, 0, 0, &extent, errp);
782 extent->flat_start_offset = flat_offset << 9;
783 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
784 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
785 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, errp);
787 bdrv_unref(extent_file);
790 extent = &s->extents[s->num_extents - 1];
792 error_setg(errp, "Unsupported extent type '%s'", type);
795 extent->type = g_strdup(type);
797 /* move to next line */
809 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
810 uint64_t desc_offset, Error **errp)
815 BDRVVmdkState *s = bs->opaque;
818 size = bdrv_getlength(bs->file);
823 size = MIN(size, 1 << 20); /* avoid unbounded allocation */
824 buf = g_malloc0(size + 1);
826 ret = bdrv_pread(bs->file, desc_offset, buf, size);
830 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
834 if (strcmp(ct, "monolithicFlat") &&
835 strcmp(ct, "vmfs") &&
836 strcmp(ct, "vmfsSparse") &&
837 strcmp(ct, "twoGbMaxExtentSparse") &&
838 strcmp(ct, "twoGbMaxExtentFlat")) {
839 error_setg(errp, "Unsupported image type '%s'", ct);
843 s->create_type = g_strdup(ct);
845 ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
851 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
855 BDRVVmdkState *s = bs->opaque;
857 if (vmdk_open_sparse(bs, bs->file, flags, errp) == 0) {
858 s->desc_offset = 0x200;
860 ret = vmdk_open_desc_file(bs, flags, 0, errp);
865 /* try to open parent images, if exist */
866 ret = vmdk_parent_open(bs);
870 s->cid = vmdk_read_cid(bs, 0);
871 s->parent_cid = vmdk_read_cid(bs, 1);
872 qemu_co_mutex_init(&s->lock);
874 /* Disable migration when VMDK images are used */
875 error_set(&s->migration_blocker,
876 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
877 "vmdk", bs->device_name, "live migration");
878 migrate_add_blocker(s->migration_blocker);
883 g_free(s->create_type);
884 s->create_type = NULL;
885 vmdk_free_extents(bs);
889 static int get_whole_cluster(BlockDriverState *bs,
891 uint64_t cluster_offset,
896 uint8_t *whole_grain = NULL;
898 /* we will be here if it's first write on non-exist grain(cluster).
899 * try to read from parent image, if exist */
900 if (bs->backing_hd) {
902 qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS);
903 if (!vmdk_is_cid_valid(bs)) {
908 /* floor offset to cluster */
909 offset -= offset % (extent->cluster_sectors * 512);
910 ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
911 extent->cluster_sectors);
917 /* Write grain only into the active image */
918 ret = bdrv_write(extent->file, cluster_offset, whole_grain,
919 extent->cluster_sectors);
926 qemu_vfree(whole_grain);
930 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
933 QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
934 offset = cpu_to_le32(m_data->offset);
935 /* update L2 table */
936 if (bdrv_pwrite_sync(
938 ((int64_t)m_data->l2_offset * 512)
939 + (m_data->l2_index * sizeof(m_data->offset)),
940 &offset, sizeof(offset)) < 0) {
943 /* update backup L2 table */
944 if (extent->l1_backup_table_offset != 0) {
945 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
946 if (bdrv_pwrite_sync(
948 ((int64_t)m_data->l2_offset * 512)
949 + (m_data->l2_index * sizeof(m_data->offset)),
950 &offset, sizeof(offset)) < 0) {
954 if (m_data->l2_cache_entry) {
955 *m_data->l2_cache_entry = offset;
961 static int get_cluster_offset(BlockDriverState *bs,
963 VmdkMetaData *m_data,
966 uint64_t *cluster_offset)
968 unsigned int l1_index, l2_offset, l2_index;
970 uint32_t min_count, *l2_table;
977 *cluster_offset = extent->flat_start_offset;
981 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
982 l1_index = (offset >> 9) / extent->l1_entry_sectors;
983 if (l1_index >= extent->l1_size) {
986 l2_offset = extent->l1_table[l1_index];
990 for (i = 0; i < L2_CACHE_SIZE; i++) {
991 if (l2_offset == extent->l2_cache_offsets[i]) {
992 /* increment the hit count */
993 if (++extent->l2_cache_counts[i] == 0xffffffff) {
994 for (j = 0; j < L2_CACHE_SIZE; j++) {
995 extent->l2_cache_counts[j] >>= 1;
998 l2_table = extent->l2_cache + (i * extent->l2_size);
1002 /* not found: load a new entry in the least used one */
1004 min_count = 0xffffffff;
1005 for (i = 0; i < L2_CACHE_SIZE; i++) {
1006 if (extent->l2_cache_counts[i] < min_count) {
1007 min_count = extent->l2_cache_counts[i];
1011 l2_table = extent->l2_cache + (min_index * extent->l2_size);
1014 (int64_t)l2_offset * 512,
1016 extent->l2_size * sizeof(uint32_t)
1017 ) != extent->l2_size * sizeof(uint32_t)) {
1021 extent->l2_cache_offsets[min_index] = l2_offset;
1022 extent->l2_cache_counts[min_index] = 1;
1024 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
1025 *cluster_offset = le32_to_cpu(l2_table[l2_index]);
1029 m_data->l1_index = l1_index;
1030 m_data->l2_index = l2_index;
1031 m_data->offset = *cluster_offset;
1032 m_data->l2_offset = l2_offset;
1033 m_data->l2_cache_entry = &l2_table[l2_index];
1035 if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
1039 if (!*cluster_offset || zeroed) {
1041 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1044 /* Avoid the L2 tables update for the images that have snapshots. */
1045 *cluster_offset = bdrv_getlength(extent->file);
1046 if (!extent->compressed) {
1049 *cluster_offset + (extent->cluster_sectors << 9)
1053 *cluster_offset >>= 9;
1054 l2_table[l2_index] = cpu_to_le32(*cluster_offset);
1056 /* First of all we write grain itself, to avoid race condition
1057 * that may to corrupt the image.
1058 * This problem may occur because of insufficient space on host disk
1059 * or inappropriate VM shutdown.
1061 if (get_whole_cluster(
1062 bs, extent, *cluster_offset, offset, allocate) == -1) {
1067 m_data->offset = *cluster_offset;
1070 *cluster_offset <<= 9;
1074 static VmdkExtent *find_extent(BDRVVmdkState *s,
1075 int64_t sector_num, VmdkExtent *start_hint)
1077 VmdkExtent *extent = start_hint;
1080 extent = &s->extents[0];
1082 while (extent < &s->extents[s->num_extents]) {
1083 if (sector_num < extent->end_sector) {
1091 static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
1092 int64_t sector_num, int nb_sectors, int *pnum)
1094 BDRVVmdkState *s = bs->opaque;
1095 int64_t index_in_cluster, n, ret;
1099 extent = find_extent(s, sector_num, NULL);
1103 qemu_co_mutex_lock(&s->lock);
1104 ret = get_cluster_offset(bs, extent, NULL,
1105 sector_num * 512, 0, &offset);
1106 qemu_co_mutex_unlock(&s->lock);
1116 ret = BDRV_BLOCK_ZERO;
1119 ret = BDRV_BLOCK_DATA;
1120 if (extent->file == bs->file) {
1121 ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1127 index_in_cluster = sector_num % extent->cluster_sectors;
1128 n = extent->cluster_sectors - index_in_cluster;
1129 if (n > nb_sectors) {
1136 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1137 int64_t offset_in_cluster, const uint8_t *buf,
1138 int nb_sectors, int64_t sector_num)
1141 VmdkGrainMarker *data = NULL;
1143 const uint8_t *write_buf = buf;
1144 int write_len = nb_sectors * 512;
1146 if (extent->compressed) {
1147 if (!extent->has_marker) {
1151 buf_len = (extent->cluster_sectors << 9) * 2;
1152 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1153 if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1158 data->lba = sector_num;
1159 data->size = buf_len;
1160 write_buf = (uint8_t *)data;
1161 write_len = buf_len + sizeof(VmdkGrainMarker);
1163 ret = bdrv_pwrite(extent->file,
1164 cluster_offset + offset_in_cluster,
1167 if (ret != write_len) {
1168 ret = ret < 0 ? ret : -EIO;
1177 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1178 int64_t offset_in_cluster, uint8_t *buf,
1182 int cluster_bytes, buf_bytes;
1183 uint8_t *cluster_buf, *compressed_data;
1184 uint8_t *uncomp_buf;
1186 VmdkGrainMarker *marker;
1190 if (!extent->compressed) {
1191 ret = bdrv_pread(extent->file,
1192 cluster_offset + offset_in_cluster,
1193 buf, nb_sectors * 512);
1194 if (ret == nb_sectors * 512) {
1200 cluster_bytes = extent->cluster_sectors * 512;
1201 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1202 buf_bytes = cluster_bytes * 2;
1203 cluster_buf = g_malloc(buf_bytes);
1204 uncomp_buf = g_malloc(cluster_bytes);
1205 ret = bdrv_pread(extent->file,
1207 cluster_buf, buf_bytes);
1211 compressed_data = cluster_buf;
1212 buf_len = cluster_bytes;
1213 data_len = cluster_bytes;
1214 if (extent->has_marker) {
1215 marker = (VmdkGrainMarker *)cluster_buf;
1216 compressed_data = marker->data;
1217 data_len = le32_to_cpu(marker->size);
1219 if (!data_len || data_len > buf_bytes) {
1223 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1229 if (offset_in_cluster < 0 ||
1230 offset_in_cluster + nb_sectors * 512 > buf_len) {
1234 memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1239 g_free(cluster_buf);
1243 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
1244 uint8_t *buf, int nb_sectors)
1246 BDRVVmdkState *s = bs->opaque;
1248 uint64_t n, index_in_cluster;
1249 uint64_t extent_begin_sector, extent_relative_sector_num;
1250 VmdkExtent *extent = NULL;
1251 uint64_t cluster_offset;
1253 while (nb_sectors > 0) {
1254 extent = find_extent(s, sector_num, extent);
1258 ret = get_cluster_offset(
1260 sector_num << 9, 0, &cluster_offset);
1261 extent_begin_sector = extent->end_sector - extent->sectors;
1262 extent_relative_sector_num = sector_num - extent_begin_sector;
1263 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1264 n = extent->cluster_sectors - index_in_cluster;
1265 if (n > nb_sectors) {
1268 if (ret != VMDK_OK) {
1269 /* if not allocated, try to read from parent image, if exist */
1270 if (bs->backing_hd && ret != VMDK_ZEROED) {
1271 if (!vmdk_is_cid_valid(bs)) {
1274 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
1279 memset(buf, 0, 512 * n);
1282 ret = vmdk_read_extent(extent,
1283 cluster_offset, index_in_cluster * 512,
1296 static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1297 uint8_t *buf, int nb_sectors)
1300 BDRVVmdkState *s = bs->opaque;
1301 qemu_co_mutex_lock(&s->lock);
1302 ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1303 qemu_co_mutex_unlock(&s->lock);
1309 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1310 * if possible, otherwise return -ENOTSUP.
1311 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1312 * with each cluster. By dry run we can find if the zero write
1313 * is possible without modifying image data.
1315 * Returns: error code with 0 for success.
1317 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
1318 const uint8_t *buf, int nb_sectors,
1319 bool zeroed, bool zero_dry_run)
1321 BDRVVmdkState *s = bs->opaque;
1322 VmdkExtent *extent = NULL;
1324 int64_t index_in_cluster;
1325 uint64_t extent_begin_sector, extent_relative_sector_num;
1326 uint64_t cluster_offset;
1327 VmdkMetaData m_data;
1329 if (sector_num > bs->total_sectors) {
1330 error_report("Wrong offset: sector_num=0x%" PRIx64
1331 " total_sectors=0x%" PRIx64 "\n",
1332 sector_num, bs->total_sectors);
1336 while (nb_sectors > 0) {
1337 extent = find_extent(s, sector_num, extent);
1341 ret = get_cluster_offset(
1345 sector_num << 9, !extent->compressed,
1347 if (extent->compressed) {
1348 if (ret == VMDK_OK) {
1349 /* Refuse write to allocated cluster for streamOptimized */
1350 error_report("Could not write to allocated cluster"
1351 " for streamOptimized");
1355 ret = get_cluster_offset(
1363 if (ret == VMDK_ERROR) {
1366 extent_begin_sector = extent->end_sector - extent->sectors;
1367 extent_relative_sector_num = sector_num - extent_begin_sector;
1368 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1369 n = extent->cluster_sectors - index_in_cluster;
1370 if (n > nb_sectors) {
1374 /* Do zeroed write, buf is ignored */
1375 if (extent->has_zero_grain &&
1376 index_in_cluster == 0 &&
1377 n >= extent->cluster_sectors) {
1378 n = extent->cluster_sectors;
1379 if (!zero_dry_run) {
1380 m_data.offset = VMDK_GTE_ZEROED;
1381 /* update L2 tables */
1382 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1390 ret = vmdk_write_extent(extent,
1391 cluster_offset, index_in_cluster * 512,
1392 buf, n, sector_num);
1397 /* update L2 tables */
1398 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1407 /* update CID on the first write every time the virtual disk is
1409 if (!s->cid_updated) {
1410 ret = vmdk_write_cid(bs, time(NULL));
1414 s->cid_updated = true;
1420 static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1421 const uint8_t *buf, int nb_sectors)
1424 BDRVVmdkState *s = bs->opaque;
1425 qemu_co_mutex_lock(&s->lock);
1426 ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1427 qemu_co_mutex_unlock(&s->lock);
1431 static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1434 BdrvRequestFlags flags)
1437 BDRVVmdkState *s = bs->opaque;
1438 qemu_co_mutex_lock(&s->lock);
1439 /* write zeroes could fail if sectors not aligned to cluster, test it with
1440 * dry_run == true before really updating image */
1441 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1443 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1445 qemu_co_mutex_unlock(&s->lock);
1449 static int vmdk_create_extent(const char *filename, int64_t filesize,
1450 bool flat, bool compress, bool zeroed_grain)
1455 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
1457 fd = qemu_open(filename,
1458 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1464 ret = ftruncate(fd, filesize);
1470 magic = cpu_to_be32(VMDK4_MAGIC);
1471 memset(&header, 0, sizeof(header));
1472 header.version = zeroed_grain ? 2 : 1;
1473 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1474 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1475 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1476 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1477 header.capacity = filesize / 512;
1478 header.granularity = 128;
1479 header.num_gtes_per_gt = 512;
1481 grains = (filesize / 512 + header.granularity - 1) / header.granularity;
1482 gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9;
1484 (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
1485 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
1487 header.desc_offset = 1;
1488 header.desc_size = 20;
1489 header.rgd_offset = header.desc_offset + header.desc_size;
1490 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
1491 header.grain_offset =
1492 ((header.gd_offset + gd_size + (gt_size * gt_count) +
1493 header.granularity - 1) / header.granularity) *
1495 /* swap endianness for all header fields */
1496 header.version = cpu_to_le32(header.version);
1497 header.flags = cpu_to_le32(header.flags);
1498 header.capacity = cpu_to_le64(header.capacity);
1499 header.granularity = cpu_to_le64(header.granularity);
1500 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1501 header.desc_offset = cpu_to_le64(header.desc_offset);
1502 header.desc_size = cpu_to_le64(header.desc_size);
1503 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1504 header.gd_offset = cpu_to_le64(header.gd_offset);
1505 header.grain_offset = cpu_to_le64(header.grain_offset);
1506 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1508 header.check_bytes[0] = 0xa;
1509 header.check_bytes[1] = 0x20;
1510 header.check_bytes[2] = 0xd;
1511 header.check_bytes[3] = 0xa;
1513 /* write all the data */
1514 ret = qemu_write_full(fd, &magic, sizeof(magic));
1515 if (ret != sizeof(magic)) {
1519 ret = qemu_write_full(fd, &header, sizeof(header));
1520 if (ret != sizeof(header)) {
1525 ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
1531 /* write grain directory */
1532 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
1533 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
1534 i < gt_count; i++, tmp += gt_size) {
1535 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1536 if (ret != sizeof(tmp)) {
1542 /* write backup grain directory */
1543 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
1544 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
1545 i < gt_count; i++, tmp += gt_size) {
1546 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1547 if (ret != sizeof(tmp)) {
1559 static int filename_decompose(const char *filename, char *path, char *prefix,
1560 char *postfix, size_t buf_len, Error **errp)
1564 if (filename == NULL || !strlen(filename)) {
1565 error_setg(errp, "No filename provided");
1568 p = strrchr(filename, '/');
1570 p = strrchr(filename, '\\');
1573 p = strrchr(filename, ':');
1577 if (p - filename >= buf_len) {
1580 pstrcpy(path, p - filename + 1, filename);
1585 q = strrchr(p, '.');
1587 pstrcpy(prefix, buf_len, p);
1590 if (q - p >= buf_len) {
1593 pstrcpy(prefix, q - p + 1, p);
1594 pstrcpy(postfix, buf_len, q);
1599 static int vmdk_create(const char *filename, QEMUOptionParameter *options,
1604 int64_t total_size = 0, filesize;
1605 const char *adapter_type = NULL;
1606 const char *backing_file = NULL;
1607 const char *fmt = NULL;
1610 bool flat, split, compress;
1611 GString *ext_desc_lines;
1612 char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1613 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1614 const char *desc_extent_line;
1615 char parent_desc_line[BUF_SIZE] = "";
1616 uint32_t parent_cid = 0xffffffff;
1617 uint32_t number_heads = 16;
1618 bool zeroed_grain = false;
1619 const char desc_template[] =
1620 "# Disk DescriptorFile\n"
1624 "createType=\"%s\"\n"
1627 "# Extent description\n"
1630 "# The Disk Data Base\n"
1633 "ddb.virtualHWVersion = \"%d\"\n"
1634 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
1635 "ddb.geometry.heads = \"%d\"\n"
1636 "ddb.geometry.sectors = \"63\"\n"
1637 "ddb.adapterType = \"%s\"\n";
1639 ext_desc_lines = g_string_new(NULL);
1641 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
1645 /* Read out options */
1646 while (options && options->name) {
1647 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1648 total_size = options->value.n;
1649 } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {
1650 adapter_type = options->value.s;
1651 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1652 backing_file = options->value.s;
1653 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1654 flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1655 } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1656 fmt = options->value.s;
1657 } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {
1658 zeroed_grain |= options->value.n;
1662 if (!adapter_type) {
1663 adapter_type = "ide";
1664 } else if (strcmp(adapter_type, "ide") &&
1665 strcmp(adapter_type, "buslogic") &&
1666 strcmp(adapter_type, "lsilogic") &&
1667 strcmp(adapter_type, "legacyESX")) {
1668 error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
1672 if (strcmp(adapter_type, "ide") != 0) {
1673 /* that's the number of heads with which vmware operates when
1674 creating, exporting, etc. vmdk files with a non-ide adapter type */
1678 /* Default format to monolithicSparse */
1679 fmt = "monolithicSparse";
1680 } else if (strcmp(fmt, "monolithicFlat") &&
1681 strcmp(fmt, "monolithicSparse") &&
1682 strcmp(fmt, "twoGbMaxExtentSparse") &&
1683 strcmp(fmt, "twoGbMaxExtentFlat") &&
1684 strcmp(fmt, "streamOptimized")) {
1685 error_setg(errp, "Unknown subformat: '%s'", fmt);
1689 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1690 strcmp(fmt, "twoGbMaxExtentSparse"));
1691 flat = !(strcmp(fmt, "monolithicFlat") &&
1692 strcmp(fmt, "twoGbMaxExtentFlat"));
1693 compress = !strcmp(fmt, "streamOptimized");
1695 desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1697 desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1699 if (flat && backing_file) {
1700 error_setg(errp, "Flat image can't have backing file");
1704 if (flat && zeroed_grain) {
1705 error_setg(errp, "Flat image can't enable zeroed grain");
1710 BlockDriverState *bs = bdrv_new("");
1711 ret = bdrv_open(bs, backing_file, NULL, BDRV_O_NO_BACKING, NULL, errp);
1716 if (strcmp(bs->drv->format_name, "vmdk")) {
1721 parent_cid = vmdk_read_cid(bs, 0);
1723 snprintf(parent_desc_line, sizeof(parent_desc_line),
1724 "parentFileNameHint=\"%s\"", backing_file);
1727 /* Create extents */
1728 filesize = total_size;
1729 while (filesize > 0) {
1730 char desc_line[BUF_SIZE];
1731 char ext_filename[PATH_MAX];
1732 char desc_filename[PATH_MAX];
1733 int64_t size = filesize;
1735 if (split && size > split_size) {
1739 snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1740 prefix, flat ? 'f' : 's', ++idx, postfix);
1742 snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1745 snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1748 snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1749 path, desc_filename);
1751 if (vmdk_create_extent(ext_filename, size,
1752 flat, compress, zeroed_grain)) {
1758 /* Format description line */
1759 snprintf(desc_line, sizeof(desc_line),
1760 desc_extent_line, size / 512, desc_filename);
1761 g_string_append(ext_desc_lines, desc_line);
1763 /* generate descriptor file */
1764 desc = g_strdup_printf(desc_template,
1765 (unsigned int)time(NULL),
1769 ext_desc_lines->str,
1770 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1771 total_size / (int64_t)(63 * number_heads * 512),
1774 if (split || flat) {
1775 fd = qemu_open(filename,
1776 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1779 fd = qemu_open(filename,
1780 O_WRONLY | O_BINARY | O_LARGEFILE,
1787 /* the descriptor offset = 0x200 */
1788 if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
1792 ret = qemu_write_full(fd, desc, strlen(desc));
1793 if (ret != strlen(desc)) {
1802 g_string_free(ext_desc_lines, true);
1806 static void vmdk_close(BlockDriverState *bs)
1808 BDRVVmdkState *s = bs->opaque;
1810 vmdk_free_extents(bs);
1811 g_free(s->create_type);
1813 migrate_del_blocker(s->migration_blocker);
1814 error_free(s->migration_blocker);
1817 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
1819 BDRVVmdkState *s = bs->opaque;
1823 for (i = 0; i < s->num_extents; i++) {
1824 err = bdrv_co_flush(s->extents[i].file);
1832 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1837 BDRVVmdkState *s = bs->opaque;
1839 ret = bdrv_get_allocated_file_size(bs->file);
1843 for (i = 0; i < s->num_extents; i++) {
1844 if (s->extents[i].file == bs->file) {
1847 r = bdrv_get_allocated_file_size(s->extents[i].file);
1856 static int vmdk_has_zero_init(BlockDriverState *bs)
1859 BDRVVmdkState *s = bs->opaque;
1861 /* If has a flat extent and its underlying storage doesn't have zero init,
1863 for (i = 0; i < s->num_extents; i++) {
1864 if (s->extents[i].flat) {
1865 if (!bdrv_has_zero_init(s->extents[i].file)) {
1873 static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
1875 ImageInfo *info = g_new0(ImageInfo, 1);
1877 *info = (ImageInfo){
1878 .filename = g_strdup(extent->file->filename),
1879 .format = g_strdup(extent->type),
1880 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
1881 .compressed = extent->compressed,
1882 .has_compressed = extent->compressed,
1883 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
1884 .has_cluster_size = !extent->flat,
1890 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
1893 BDRVVmdkState *s = bs->opaque;
1894 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
1895 ImageInfoList **next;
1897 *spec_info = (ImageInfoSpecific){
1898 .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
1900 .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
1904 *spec_info->vmdk = (ImageInfoSpecificVmdk) {
1905 .create_type = g_strdup(s->create_type),
1907 .parent_cid = s->parent_cid,
1910 next = &spec_info->vmdk->extents;
1911 for (i = 0; i < s->num_extents; i++) {
1912 *next = g_new0(ImageInfoList, 1);
1913 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
1914 (*next)->next = NULL;
1915 next = &(*next)->next;
1921 static QEMUOptionParameter vmdk_create_options[] = {
1923 .name = BLOCK_OPT_SIZE,
1925 .help = "Virtual disk size"
1928 .name = BLOCK_OPT_ADAPTER_TYPE,
1930 .help = "Virtual adapter type, can be one of "
1931 "ide (default), lsilogic, buslogic or legacyESX"
1934 .name = BLOCK_OPT_BACKING_FILE,
1936 .help = "File name of a base image"
1939 .name = BLOCK_OPT_COMPAT6,
1941 .help = "VMDK version 6 image"
1944 .name = BLOCK_OPT_SUBFMT,
1947 "VMDK flat extent format, can be one of "
1948 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1951 .name = BLOCK_OPT_ZEROED_GRAIN,
1953 .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
1958 static BlockDriver bdrv_vmdk = {
1959 .format_name = "vmdk",
1960 .instance_size = sizeof(BDRVVmdkState),
1961 .bdrv_probe = vmdk_probe,
1962 .bdrv_open = vmdk_open,
1963 .bdrv_reopen_prepare = vmdk_reopen_prepare,
1964 .bdrv_read = vmdk_co_read,
1965 .bdrv_write = vmdk_co_write,
1966 .bdrv_co_write_zeroes = vmdk_co_write_zeroes,
1967 .bdrv_close = vmdk_close,
1968 .bdrv_create = vmdk_create,
1969 .bdrv_co_flush_to_disk = vmdk_co_flush,
1970 .bdrv_co_get_block_status = vmdk_co_get_block_status,
1971 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
1972 .bdrv_has_zero_init = vmdk_has_zero_init,
1973 .bdrv_get_specific_info = vmdk_get_specific_info,
1975 .create_options = vmdk_create_options,
1978 static void bdrv_vmdk_init(void)
1980 bdrv_register(&bdrv_vmdk);
1983 block_init(bdrv_vmdk_init);