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
;
431 if (s
->num_extents
> 1) {
432 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
434 extent
->end_sector
= extent
->sectors
;
436 bs
->total_sectors
= extent
->end_sector
;
438 *new_extent
= extent
;
443 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
449 /* read the L1 table */
450 l1_size
= extent
->l1_size
* sizeof(uint32_t);
451 extent
->l1_table
= g_malloc(l1_size
);
452 ret
= bdrv_pread(extent
->file
,
453 extent
->l1_table_offset
,
457 error_setg_errno(errp
, -ret
,
458 "Could not read l1 table from extent '%s'",
459 extent
->file
->filename
);
462 for (i
= 0; i
< extent
->l1_size
; i
++) {
463 le32_to_cpus(&extent
->l1_table
[i
]);
466 if (extent
->l1_backup_table_offset
) {
467 extent
->l1_backup_table
= g_malloc(l1_size
);
468 ret
= bdrv_pread(extent
->file
,
469 extent
->l1_backup_table_offset
,
470 extent
->l1_backup_table
,
473 error_setg_errno(errp
, -ret
,
474 "Could not read l1 backup table from extent '%s'",
475 extent
->file
->filename
);
478 for (i
= 0; i
< extent
->l1_size
; i
++) {
479 le32_to_cpus(&extent
->l1_backup_table
[i
]);
484 g_malloc(extent
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
487 g_free(extent
->l1_backup_table
);
489 g_free(extent
->l1_table
);
493 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
494 BlockDriverState
*file
,
495 int flags
, Error
**errp
)
502 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
504 error_setg_errno(errp
, -ret
,
505 "Could not read header from file '%s'",
509 ret
= vmdk_add_extent(bs
, file
, false,
510 le32_to_cpu(header
.disk_sectors
),
511 le32_to_cpu(header
.l1dir_offset
) << 9,
513 le32_to_cpu(header
.l1dir_size
),
515 le32_to_cpu(header
.granularity
),
521 ret
= vmdk_init_tables(bs
, extent
, errp
);
523 /* free extent allocated by vmdk_add_extent */
524 vmdk_free_last_extent(bs
);
529 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
530 uint64_t desc_offset
, Error
**errp
);
532 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
533 BlockDriverState
*file
,
534 int flags
, Error
**errp
)
538 uint32_t l1_size
, l1_entry_sectors
;
541 BDRVVmdkState
*s
= bs
->opaque
;
542 int64_t l1_backup_offset
= 0;
544 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
546 error_setg_errno(errp
, -ret
,
547 "Could not read header from file '%s'",
550 if (header
.capacity
== 0) {
551 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
553 return vmdk_open_desc_file(bs
, flags
, desc_offset
<< 9, errp
);
557 if (!s
->create_type
) {
558 s
->create_type
= g_strdup("monolithicSparse");
561 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
563 * The footer takes precedence over the header, so read it in. The
564 * footer starts at offset -1024 from the end: One sector for the
565 * footer, and another one for the end-of-stream marker.
572 uint8_t pad
[512 - 16];
573 } QEMU_PACKED footer_marker
;
577 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
583 uint8_t pad
[512 - 16];
584 } QEMU_PACKED eos_marker
;
585 } QEMU_PACKED footer
;
587 ret
= bdrv_pread(file
,
588 bs
->file
->total_sectors
* 512 - 1536,
589 &footer
, sizeof(footer
));
594 /* Some sanity checks for the footer */
595 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
596 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
597 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
598 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
599 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
600 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
605 header
= footer
.header
;
608 if (le32_to_cpu(header
.version
) >= 3) {
610 snprintf(buf
, sizeof(buf
), "VMDK version %d",
611 le32_to_cpu(header
.version
));
612 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
613 bs
->device_name
, "vmdk", buf
);
617 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
618 error_report("L2 table size too big");
622 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
623 * le64_to_cpu(header
.granularity
);
624 if (l1_entry_sectors
== 0) {
627 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
629 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
630 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
632 ret
= vmdk_add_extent(bs
, file
, false,
633 le64_to_cpu(header
.capacity
),
634 le64_to_cpu(header
.gd_offset
) << 9,
637 le32_to_cpu(header
.num_gtes_per_gt
),
638 le64_to_cpu(header
.granularity
),
645 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
646 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
647 extent
->version
= le32_to_cpu(header
.version
);
648 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
649 ret
= vmdk_init_tables(bs
, extent
, errp
);
651 /* free extent allocated by vmdk_add_extent */
652 vmdk_free_last_extent(bs
);
657 /* find an option value out of descriptor file */
658 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
659 char *buf
, int buf_size
)
661 char *opt_pos
, *opt_end
;
662 const char *end
= desc
+ strlen(desc
);
664 opt_pos
= strstr(desc
, opt_name
);
668 /* Skip "=\"" following opt_name */
669 opt_pos
+= strlen(opt_name
) + 2;
670 if (opt_pos
>= end
) {
674 while (opt_end
< end
&& *opt_end
!= '"') {
677 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
680 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
684 /* Open an extent file and append to bs array */
685 static int vmdk_open_sparse(BlockDriverState
*bs
,
686 BlockDriverState
*file
,
687 int flags
, Error
**errp
)
691 if (bdrv_pread(file
, 0, &magic
, sizeof(magic
)) != sizeof(magic
)) {
695 magic
= be32_to_cpu(magic
);
698 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
701 return vmdk_open_vmdk4(bs
, file
, flags
, errp
);
709 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
710 const char *desc_file_path
, Error
**errp
)
716 const char *p
= desc
;
719 char extent_path
[PATH_MAX
];
720 BlockDriverState
*extent_file
;
721 BDRVVmdkState
*s
= bs
->opaque
;
725 /* parse extent line:
726 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
728 * RW [size in sectors] SPARSE "file-name.vmdk"
731 ret
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
732 access
, §ors
, type
, fname
, &flat_offset
);
733 if (ret
< 4 || strcmp(access
, "RW")) {
735 } else if (!strcmp(type
, "FLAT")) {
736 if (ret
!= 5 || flat_offset
< 0) {
737 error_setg(errp
, "Invalid extent lines: \n%s", p
);
740 } else if (!strcmp(type
, "VMFS")) {
742 } else if (ret
!= 4) {
743 error_setg(errp
, "Invalid extent lines: \n%s", p
);
748 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
749 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
750 (strcmp(access
, "RW"))) {
754 path_combine(extent_path
, sizeof(extent_path
),
755 desc_file_path
, fname
);
756 ret
= bdrv_file_open(&extent_file
, extent_path
, NULL
, bs
->open_flags
,
762 /* save to extents array */
763 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
766 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
767 0, 0, 0, 0, 0, &extent
, errp
);
771 extent
->flat_start_offset
= flat_offset
<< 9;
772 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
773 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
774 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, errp
);
776 bdrv_unref(extent_file
);
779 extent
= &s
->extents
[s
->num_extents
- 1];
781 error_setg(errp
, "Unsupported extent type '%s'", type
);
784 extent
->type
= g_strdup(type
);
786 /* move to next line */
798 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
799 uint64_t desc_offset
, Error
**errp
)
804 BDRVVmdkState
*s
= bs
->opaque
;
807 size
= bdrv_getlength(bs
->file
);
812 size
= MIN(size
, 1 << 20); /* avoid unbounded allocation */
813 buf
= g_malloc0(size
+ 1);
815 ret
= bdrv_pread(bs
->file
, desc_offset
, buf
, size
);
819 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
823 if (strcmp(ct
, "monolithicFlat") &&
824 strcmp(ct
, "vmfs") &&
825 strcmp(ct
, "vmfsSparse") &&
826 strcmp(ct
, "twoGbMaxExtentSparse") &&
827 strcmp(ct
, "twoGbMaxExtentFlat")) {
828 error_setg(errp
, "Unsupported image type '%s'", ct
);
832 s
->create_type
= g_strdup(ct
);
834 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->filename
, errp
);
840 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
844 BDRVVmdkState
*s
= bs
->opaque
;
846 if (vmdk_open_sparse(bs
, bs
->file
, flags
, errp
) == 0) {
847 s
->desc_offset
= 0x200;
849 ret
= vmdk_open_desc_file(bs
, flags
, 0, errp
);
854 /* try to open parent images, if exist */
855 ret
= vmdk_parent_open(bs
);
859 s
->cid
= vmdk_read_cid(bs
, 0);
860 s
->parent_cid
= vmdk_read_cid(bs
, 1);
861 qemu_co_mutex_init(&s
->lock
);
863 /* Disable migration when VMDK images are used */
864 error_set(&s
->migration_blocker
,
865 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
866 "vmdk", bs
->device_name
, "live migration");
867 migrate_add_blocker(s
->migration_blocker
);
872 g_free(s
->create_type
);
873 s
->create_type
= NULL
;
874 vmdk_free_extents(bs
);
878 static int get_whole_cluster(BlockDriverState
*bs
,
880 uint64_t cluster_offset
,
885 uint8_t *whole_grain
= NULL
;
887 /* we will be here if it's first write on non-exist grain(cluster).
888 * try to read from parent image, if exist */
889 if (bs
->backing_hd
) {
891 qemu_blockalign(bs
, extent
->cluster_sectors
<< BDRV_SECTOR_BITS
);
892 if (!vmdk_is_cid_valid(bs
)) {
897 /* floor offset to cluster */
898 offset
-= offset
% (extent
->cluster_sectors
* 512);
899 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
900 extent
->cluster_sectors
);
906 /* Write grain only into the active image */
907 ret
= bdrv_write(extent
->file
, cluster_offset
, whole_grain
,
908 extent
->cluster_sectors
);
915 qemu_vfree(whole_grain
);
919 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
)
922 QEMU_BUILD_BUG_ON(sizeof(offset
) != sizeof(m_data
->offset
));
923 offset
= cpu_to_le32(m_data
->offset
);
924 /* update L2 table */
925 if (bdrv_pwrite_sync(
927 ((int64_t)m_data
->l2_offset
* 512)
928 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
929 &offset
, sizeof(offset
)) < 0) {
932 /* update backup L2 table */
933 if (extent
->l1_backup_table_offset
!= 0) {
934 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
935 if (bdrv_pwrite_sync(
937 ((int64_t)m_data
->l2_offset
* 512)
938 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
939 &offset
, sizeof(offset
)) < 0) {
943 if (m_data
->l2_cache_entry
) {
944 *m_data
->l2_cache_entry
= offset
;
950 static int get_cluster_offset(BlockDriverState
*bs
,
952 VmdkMetaData
*m_data
,
955 uint64_t *cluster_offset
)
957 unsigned int l1_index
, l2_offset
, l2_index
;
959 uint32_t min_count
, *l2_table
;
966 *cluster_offset
= extent
->flat_start_offset
;
970 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
971 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
972 if (l1_index
>= extent
->l1_size
) {
975 l2_offset
= extent
->l1_table
[l1_index
];
979 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
980 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
981 /* increment the hit count */
982 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
983 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
984 extent
->l2_cache_counts
[j
] >>= 1;
987 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
991 /* not found: load a new entry in the least used one */
993 min_count
= 0xffffffff;
994 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
995 if (extent
->l2_cache_counts
[i
] < min_count
) {
996 min_count
= extent
->l2_cache_counts
[i
];
1000 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1003 (int64_t)l2_offset
* 512,
1005 extent
->l2_size
* sizeof(uint32_t)
1006 ) != extent
->l2_size
* sizeof(uint32_t)) {
1010 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1011 extent
->l2_cache_counts
[min_index
] = 1;
1013 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1014 *cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
1018 m_data
->l1_index
= l1_index
;
1019 m_data
->l2_index
= l2_index
;
1020 m_data
->offset
= *cluster_offset
;
1021 m_data
->l2_offset
= l2_offset
;
1022 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1024 if (extent
->has_zero_grain
&& *cluster_offset
== VMDK_GTE_ZEROED
) {
1028 if (!*cluster_offset
|| zeroed
) {
1030 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1033 /* Avoid the L2 tables update for the images that have snapshots. */
1034 *cluster_offset
= bdrv_getlength(extent
->file
);
1035 if (!extent
->compressed
) {
1038 *cluster_offset
+ (extent
->cluster_sectors
<< 9)
1042 *cluster_offset
>>= 9;
1043 l2_table
[l2_index
] = cpu_to_le32(*cluster_offset
);
1045 /* First of all we write grain itself, to avoid race condition
1046 * that may to corrupt the image.
1047 * This problem may occur because of insufficient space on host disk
1048 * or inappropriate VM shutdown.
1050 if (get_whole_cluster(
1051 bs
, extent
, *cluster_offset
, offset
, allocate
) == -1) {
1056 m_data
->offset
= *cluster_offset
;
1059 *cluster_offset
<<= 9;
1063 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1064 int64_t sector_num
, VmdkExtent
*start_hint
)
1066 VmdkExtent
*extent
= start_hint
;
1069 extent
= &s
->extents
[0];
1071 while (extent
< &s
->extents
[s
->num_extents
]) {
1072 if (sector_num
< extent
->end_sector
) {
1080 static int64_t coroutine_fn
vmdk_co_get_block_status(BlockDriverState
*bs
,
1081 int64_t sector_num
, int nb_sectors
, int *pnum
)
1083 BDRVVmdkState
*s
= bs
->opaque
;
1084 int64_t index_in_cluster
, n
, ret
;
1088 extent
= find_extent(s
, sector_num
, NULL
);
1092 qemu_co_mutex_lock(&s
->lock
);
1093 ret
= get_cluster_offset(bs
, extent
, NULL
,
1094 sector_num
* 512, 0, &offset
);
1095 qemu_co_mutex_unlock(&s
->lock
);
1105 ret
= BDRV_BLOCK_ZERO
;
1108 ret
= BDRV_BLOCK_DATA
;
1109 if (extent
->file
== bs
->file
) {
1110 ret
|= BDRV_BLOCK_OFFSET_VALID
| offset
;
1116 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1117 n
= extent
->cluster_sectors
- index_in_cluster
;
1118 if (n
> nb_sectors
) {
1125 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1126 int64_t offset_in_cluster
, const uint8_t *buf
,
1127 int nb_sectors
, int64_t sector_num
)
1130 VmdkGrainMarker
*data
= NULL
;
1132 const uint8_t *write_buf
= buf
;
1133 int write_len
= nb_sectors
* 512;
1135 if (extent
->compressed
) {
1136 if (!extent
->has_marker
) {
1140 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1141 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1142 if (compress(data
->data
, &buf_len
, buf
, nb_sectors
<< 9) != Z_OK
||
1147 data
->lba
= sector_num
;
1148 data
->size
= buf_len
;
1149 write_buf
= (uint8_t *)data
;
1150 write_len
= buf_len
+ sizeof(VmdkGrainMarker
);
1152 ret
= bdrv_pwrite(extent
->file
,
1153 cluster_offset
+ offset_in_cluster
,
1156 if (ret
!= write_len
) {
1157 ret
= ret
< 0 ? ret
: -EIO
;
1166 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1167 int64_t offset_in_cluster
, uint8_t *buf
,
1171 int cluster_bytes
, buf_bytes
;
1172 uint8_t *cluster_buf
, *compressed_data
;
1173 uint8_t *uncomp_buf
;
1175 VmdkGrainMarker
*marker
;
1179 if (!extent
->compressed
) {
1180 ret
= bdrv_pread(extent
->file
,
1181 cluster_offset
+ offset_in_cluster
,
1182 buf
, nb_sectors
* 512);
1183 if (ret
== nb_sectors
* 512) {
1189 cluster_bytes
= extent
->cluster_sectors
* 512;
1190 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1191 buf_bytes
= cluster_bytes
* 2;
1192 cluster_buf
= g_malloc(buf_bytes
);
1193 uncomp_buf
= g_malloc(cluster_bytes
);
1194 ret
= bdrv_pread(extent
->file
,
1196 cluster_buf
, buf_bytes
);
1200 compressed_data
= cluster_buf
;
1201 buf_len
= cluster_bytes
;
1202 data_len
= cluster_bytes
;
1203 if (extent
->has_marker
) {
1204 marker
= (VmdkGrainMarker
*)cluster_buf
;
1205 compressed_data
= marker
->data
;
1206 data_len
= le32_to_cpu(marker
->size
);
1208 if (!data_len
|| data_len
> buf_bytes
) {
1212 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1218 if (offset_in_cluster
< 0 ||
1219 offset_in_cluster
+ nb_sectors
* 512 > buf_len
) {
1223 memcpy(buf
, uncomp_buf
+ offset_in_cluster
, nb_sectors
* 512);
1228 g_free(cluster_buf
);
1232 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
1233 uint8_t *buf
, int nb_sectors
)
1235 BDRVVmdkState
*s
= bs
->opaque
;
1237 uint64_t n
, index_in_cluster
;
1238 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1239 VmdkExtent
*extent
= NULL
;
1240 uint64_t cluster_offset
;
1242 while (nb_sectors
> 0) {
1243 extent
= find_extent(s
, sector_num
, extent
);
1247 ret
= get_cluster_offset(
1249 sector_num
<< 9, 0, &cluster_offset
);
1250 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1251 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1252 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1253 n
= extent
->cluster_sectors
- index_in_cluster
;
1254 if (n
> nb_sectors
) {
1257 if (ret
!= VMDK_OK
) {
1258 /* if not allocated, try to read from parent image, if exist */
1259 if (bs
->backing_hd
&& ret
!= VMDK_ZEROED
) {
1260 if (!vmdk_is_cid_valid(bs
)) {
1263 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
1268 memset(buf
, 0, 512 * n
);
1271 ret
= vmdk_read_extent(extent
,
1272 cluster_offset
, index_in_cluster
* 512,
1285 static coroutine_fn
int vmdk_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1286 uint8_t *buf
, int nb_sectors
)
1289 BDRVVmdkState
*s
= bs
->opaque
;
1290 qemu_co_mutex_lock(&s
->lock
);
1291 ret
= vmdk_read(bs
, sector_num
, buf
, nb_sectors
);
1292 qemu_co_mutex_unlock(&s
->lock
);
1298 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1299 * if possible, otherwise return -ENOTSUP.
1300 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1301 * with each cluster. By dry run we can find if the zero write
1302 * is possible without modifying image data.
1304 * Returns: error code with 0 for success.
1306 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
1307 const uint8_t *buf
, int nb_sectors
,
1308 bool zeroed
, bool zero_dry_run
)
1310 BDRVVmdkState
*s
= bs
->opaque
;
1311 VmdkExtent
*extent
= NULL
;
1313 int64_t index_in_cluster
;
1314 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1315 uint64_t cluster_offset
;
1316 VmdkMetaData m_data
;
1318 if (sector_num
> bs
->total_sectors
) {
1319 error_report("Wrong offset: sector_num=0x%" PRIx64
1320 " total_sectors=0x%" PRIx64
"\n",
1321 sector_num
, bs
->total_sectors
);
1325 while (nb_sectors
> 0) {
1326 extent
= find_extent(s
, sector_num
, extent
);
1330 ret
= get_cluster_offset(
1334 sector_num
<< 9, !extent
->compressed
,
1336 if (extent
->compressed
) {
1337 if (ret
== VMDK_OK
) {
1338 /* Refuse write to allocated cluster for streamOptimized */
1339 error_report("Could not write to allocated cluster"
1340 " for streamOptimized");
1344 ret
= get_cluster_offset(
1352 if (ret
== VMDK_ERROR
) {
1355 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1356 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1357 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1358 n
= extent
->cluster_sectors
- index_in_cluster
;
1359 if (n
> nb_sectors
) {
1363 /* Do zeroed write, buf is ignored */
1364 if (extent
->has_zero_grain
&&
1365 index_in_cluster
== 0 &&
1366 n
>= extent
->cluster_sectors
) {
1367 n
= extent
->cluster_sectors
;
1368 if (!zero_dry_run
) {
1369 m_data
.offset
= VMDK_GTE_ZEROED
;
1370 /* update L2 tables */
1371 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1379 ret
= vmdk_write_extent(extent
,
1380 cluster_offset
, index_in_cluster
* 512,
1381 buf
, n
, sector_num
);
1386 /* update L2 tables */
1387 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1396 /* update CID on the first write every time the virtual disk is
1398 if (!s
->cid_updated
) {
1399 ret
= vmdk_write_cid(bs
, time(NULL
));
1403 s
->cid_updated
= true;
1409 static coroutine_fn
int vmdk_co_write(BlockDriverState
*bs
, int64_t sector_num
,
1410 const uint8_t *buf
, int nb_sectors
)
1413 BDRVVmdkState
*s
= bs
->opaque
;
1414 qemu_co_mutex_lock(&s
->lock
);
1415 ret
= vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1416 qemu_co_mutex_unlock(&s
->lock
);
1420 static int coroutine_fn
vmdk_co_write_zeroes(BlockDriverState
*bs
,
1423 BdrvRequestFlags flags
)
1426 BDRVVmdkState
*s
= bs
->opaque
;
1427 qemu_co_mutex_lock(&s
->lock
);
1428 /* write zeroes could fail if sectors not aligned to cluster, test it with
1429 * dry_run == true before really updating image */
1430 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, true);
1432 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, false);
1434 qemu_co_mutex_unlock(&s
->lock
);
1438 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1439 bool flat
, bool compress
, bool zeroed_grain
)
1444 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
1446 fd
= qemu_open(filename
,
1447 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1453 ret
= ftruncate(fd
, filesize
);
1459 magic
= cpu_to_be32(VMDK4_MAGIC
);
1460 memset(&header
, 0, sizeof(header
));
1461 header
.version
= zeroed_grain
? 2 : 1;
1462 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1463 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1464 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1465 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1466 header
.capacity
= filesize
/ 512;
1467 header
.granularity
= 128;
1468 header
.num_gtes_per_gt
= 512;
1470 grains
= (filesize
/ 512 + header
.granularity
- 1) / header
.granularity
;
1471 gt_size
= ((header
.num_gtes_per_gt
* sizeof(uint32_t)) + 511) >> 9;
1473 (grains
+ header
.num_gtes_per_gt
- 1) / header
.num_gtes_per_gt
;
1474 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
1476 header
.desc_offset
= 1;
1477 header
.desc_size
= 20;
1478 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1479 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
1480 header
.grain_offset
=
1481 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
1482 header
.granularity
- 1) / header
.granularity
) *
1484 /* swap endianness for all header fields */
1485 header
.version
= cpu_to_le32(header
.version
);
1486 header
.flags
= cpu_to_le32(header
.flags
);
1487 header
.capacity
= cpu_to_le64(header
.capacity
);
1488 header
.granularity
= cpu_to_le64(header
.granularity
);
1489 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1490 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1491 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1492 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1493 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1494 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1495 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1497 header
.check_bytes
[0] = 0xa;
1498 header
.check_bytes
[1] = 0x20;
1499 header
.check_bytes
[2] = 0xd;
1500 header
.check_bytes
[3] = 0xa;
1502 /* write all the data */
1503 ret
= qemu_write_full(fd
, &magic
, sizeof(magic
));
1504 if (ret
!= sizeof(magic
)) {
1508 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
1509 if (ret
!= sizeof(header
)) {
1514 ret
= ftruncate(fd
, le64_to_cpu(header
.grain_offset
) << 9);
1520 /* write grain directory */
1521 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
1522 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_size
;
1523 i
< gt_count
; i
++, tmp
+= gt_size
) {
1524 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1525 if (ret
!= sizeof(tmp
)) {
1531 /* write backup grain directory */
1532 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
1533 for (i
= 0, tmp
= le64_to_cpu(header
.gd_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
)) {
1548 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1549 char *postfix
, size_t buf_len
, Error
**errp
)
1553 if (filename
== NULL
|| !strlen(filename
)) {
1554 error_setg(errp
, "No filename provided");
1557 p
= strrchr(filename
, '/');
1559 p
= strrchr(filename
, '\\');
1562 p
= strrchr(filename
, ':');
1566 if (p
- filename
>= buf_len
) {
1569 pstrcpy(path
, p
- filename
+ 1, filename
);
1574 q
= strrchr(p
, '.');
1576 pstrcpy(prefix
, buf_len
, p
);
1579 if (q
- p
>= buf_len
) {
1582 pstrcpy(prefix
, q
- p
+ 1, p
);
1583 pstrcpy(postfix
, buf_len
, q
);
1588 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
,
1592 char desc
[BUF_SIZE
];
1593 int64_t total_size
= 0, filesize
;
1594 const char *adapter_type
= NULL
;
1595 const char *backing_file
= NULL
;
1596 const char *fmt
= NULL
;
1599 bool flat
, split
, compress
;
1600 char ext_desc_lines
[BUF_SIZE
] = "";
1601 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1602 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1603 const char *desc_extent_line
;
1604 char parent_desc_line
[BUF_SIZE
] = "";
1605 uint32_t parent_cid
= 0xffffffff;
1606 uint32_t number_heads
= 16;
1607 bool zeroed_grain
= false;
1608 const char desc_template
[] =
1609 "# Disk DescriptorFile\n"
1613 "createType=\"%s\"\n"
1616 "# Extent description\n"
1619 "# The Disk Data Base\n"
1622 "ddb.virtualHWVersion = \"%d\"\n"
1623 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1624 "ddb.geometry.heads = \"%d\"\n"
1625 "ddb.geometry.sectors = \"63\"\n"
1626 "ddb.adapterType = \"%s\"\n";
1628 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
1631 /* Read out options */
1632 while (options
&& options
->name
) {
1633 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1634 total_size
= options
->value
.n
;
1635 } else if (!strcmp(options
->name
, BLOCK_OPT_ADAPTER_TYPE
)) {
1636 adapter_type
= options
->value
.s
;
1637 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1638 backing_file
= options
->value
.s
;
1639 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
1640 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
1641 } else if (!strcmp(options
->name
, BLOCK_OPT_SUBFMT
)) {
1642 fmt
= options
->value
.s
;
1643 } else if (!strcmp(options
->name
, BLOCK_OPT_ZEROED_GRAIN
)) {
1644 zeroed_grain
|= options
->value
.n
;
1648 if (!adapter_type
) {
1649 adapter_type
= "ide";
1650 } else if (strcmp(adapter_type
, "ide") &&
1651 strcmp(adapter_type
, "buslogic") &&
1652 strcmp(adapter_type
, "lsilogic") &&
1653 strcmp(adapter_type
, "legacyESX")) {
1654 error_setg(errp
, "Unknown adapter type: '%s'", adapter_type
);
1657 if (strcmp(adapter_type
, "ide") != 0) {
1658 /* that's the number of heads with which vmware operates when
1659 creating, exporting, etc. vmdk files with a non-ide adapter type */
1663 /* Default format to monolithicSparse */
1664 fmt
= "monolithicSparse";
1665 } else if (strcmp(fmt
, "monolithicFlat") &&
1666 strcmp(fmt
, "monolithicSparse") &&
1667 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1668 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1669 strcmp(fmt
, "streamOptimized")) {
1670 error_setg(errp
, "Unknown subformat: '%s'", fmt
);
1673 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1674 strcmp(fmt
, "twoGbMaxExtentSparse"));
1675 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1676 strcmp(fmt
, "twoGbMaxExtentFlat"));
1677 compress
= !strcmp(fmt
, "streamOptimized");
1679 desc_extent_line
= "RW %lld FLAT \"%s\" 0\n";
1681 desc_extent_line
= "RW %lld SPARSE \"%s\"\n";
1683 if (flat
&& backing_file
) {
1684 error_setg(errp
, "Flat image can't have backing file");
1687 if (flat
&& zeroed_grain
) {
1688 error_setg(errp
, "Flat image can't enable zeroed grain");
1692 BlockDriverState
*bs
= bdrv_new("");
1693 ret
= bdrv_open(bs
, backing_file
, NULL
, 0, NULL
, errp
);
1698 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1702 parent_cid
= vmdk_read_cid(bs
, 0);
1704 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1705 "parentFileNameHint=\"%s\"", backing_file
);
1708 /* Create extents */
1709 filesize
= total_size
;
1710 while (filesize
> 0) {
1711 char desc_line
[BUF_SIZE
];
1712 char ext_filename
[PATH_MAX
];
1713 char desc_filename
[PATH_MAX
];
1714 int64_t size
= filesize
;
1716 if (split
&& size
> split_size
) {
1720 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1721 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1723 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1726 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1729 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1730 path
, desc_filename
);
1732 if (vmdk_create_extent(ext_filename
, size
,
1733 flat
, compress
, zeroed_grain
)) {
1738 /* Format description line */
1739 snprintf(desc_line
, sizeof(desc_line
),
1740 desc_extent_line
, size
/ 512, desc_filename
);
1741 pstrcat(ext_desc_lines
, sizeof(ext_desc_lines
), desc_line
);
1743 /* generate descriptor file */
1744 snprintf(desc
, sizeof(desc
), desc_template
,
1745 (unsigned int)time(NULL
),
1750 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1751 total_size
/ (int64_t)(63 * number_heads
* 512), number_heads
,
1753 if (split
|| flat
) {
1754 fd
= qemu_open(filename
,
1755 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1758 fd
= qemu_open(filename
,
1759 O_WRONLY
| O_BINARY
| O_LARGEFILE
,
1765 /* the descriptor offset = 0x200 */
1766 if (!split
&& !flat
&& 0x200 != lseek(fd
, 0x200, SEEK_SET
)) {
1770 ret
= qemu_write_full(fd
, desc
, strlen(desc
));
1771 if (ret
!= strlen(desc
)) {
1781 static void vmdk_close(BlockDriverState
*bs
)
1783 BDRVVmdkState
*s
= bs
->opaque
;
1785 vmdk_free_extents(bs
);
1786 g_free(s
->create_type
);
1788 migrate_del_blocker(s
->migration_blocker
);
1789 error_free(s
->migration_blocker
);
1792 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
1794 BDRVVmdkState
*s
= bs
->opaque
;
1798 for (i
= 0; i
< s
->num_extents
; i
++) {
1799 err
= bdrv_co_flush(s
->extents
[i
].file
);
1807 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
1812 BDRVVmdkState
*s
= bs
->opaque
;
1814 ret
= bdrv_get_allocated_file_size(bs
->file
);
1818 for (i
= 0; i
< s
->num_extents
; i
++) {
1819 if (s
->extents
[i
].file
== bs
->file
) {
1822 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
1831 static int vmdk_has_zero_init(BlockDriverState
*bs
)
1834 BDRVVmdkState
*s
= bs
->opaque
;
1836 /* If has a flat extent and its underlying storage doesn't have zero init,
1838 for (i
= 0; i
< s
->num_extents
; i
++) {
1839 if (s
->extents
[i
].flat
) {
1840 if (!bdrv_has_zero_init(s
->extents
[i
].file
)) {
1848 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
1850 ImageInfo
*info
= g_new0(ImageInfo
, 1);
1852 *info
= (ImageInfo
){
1853 .filename
= g_strdup(extent
->file
->filename
),
1854 .format
= g_strdup(extent
->type
),
1855 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
1856 .compressed
= extent
->compressed
,
1857 .has_compressed
= extent
->compressed
,
1858 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
1859 .has_cluster_size
= !extent
->flat
,
1865 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
)
1868 BDRVVmdkState
*s
= bs
->opaque
;
1869 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
1870 ImageInfoList
**next
;
1872 *spec_info
= (ImageInfoSpecific
){
1873 .kind
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
1875 .vmdk
= g_new0(ImageInfoSpecificVmdk
, 1),
1879 *spec_info
->vmdk
= (ImageInfoSpecificVmdk
) {
1880 .create_type
= g_strdup(s
->create_type
),
1882 .parent_cid
= s
->parent_cid
,
1885 next
= &spec_info
->vmdk
->extents
;
1886 for (i
= 0; i
< s
->num_extents
; i
++) {
1887 *next
= g_new0(ImageInfoList
, 1);
1888 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
1889 (*next
)->next
= NULL
;
1890 next
= &(*next
)->next
;
1896 static QEMUOptionParameter vmdk_create_options
[] = {
1898 .name
= BLOCK_OPT_SIZE
,
1900 .help
= "Virtual disk size"
1903 .name
= BLOCK_OPT_ADAPTER_TYPE
,
1905 .help
= "Virtual adapter type, can be one of "
1906 "ide (default), lsilogic, buslogic or legacyESX"
1909 .name
= BLOCK_OPT_BACKING_FILE
,
1911 .help
= "File name of a base image"
1914 .name
= BLOCK_OPT_COMPAT6
,
1916 .help
= "VMDK version 6 image"
1919 .name
= BLOCK_OPT_SUBFMT
,
1922 "VMDK flat extent format, can be one of "
1923 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1926 .name
= BLOCK_OPT_ZEROED_GRAIN
,
1928 .help
= "Enable efficient zero writes using the zeroed-grain GTE feature"
1933 static BlockDriver bdrv_vmdk
= {
1934 .format_name
= "vmdk",
1935 .instance_size
= sizeof(BDRVVmdkState
),
1936 .bdrv_probe
= vmdk_probe
,
1937 .bdrv_open
= vmdk_open
,
1938 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
1939 .bdrv_read
= vmdk_co_read
,
1940 .bdrv_write
= vmdk_co_write
,
1941 .bdrv_co_write_zeroes
= vmdk_co_write_zeroes
,
1942 .bdrv_close
= vmdk_close
,
1943 .bdrv_create
= vmdk_create
,
1944 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
1945 .bdrv_co_get_block_status
= vmdk_co_get_block_status
,
1946 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
1947 .bdrv_has_zero_init
= vmdk_has_zero_init
,
1948 .bdrv_get_specific_info
= vmdk_get_specific_info
,
1950 .create_options
= vmdk_create_options
,
1953 static void bdrv_vmdk_init(void)
1955 bdrv_register(&bdrv_vmdk
);
1958 block_init(bdrv_vmdk_init
);