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 unsigned int cluster_sectors
;
111 typedef struct BDRVVmdkState
{
113 uint64_t desc_offset
;
117 /* Extent array with num_extents entries, ascend ordered by address */
119 Error
*migration_blocker
;
122 typedef struct VmdkMetaData
{
124 unsigned int l1_index
;
125 unsigned int l2_index
;
126 unsigned int l2_offset
;
128 uint32_t *l2_cache_entry
;
131 typedef struct VmdkGrainMarker
{
135 } QEMU_PACKED VmdkGrainMarker
;
138 MARKER_END_OF_STREAM
= 0,
139 MARKER_GRAIN_TABLE
= 1,
140 MARKER_GRAIN_DIRECTORY
= 2,
144 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
151 magic
= be32_to_cpu(*(uint32_t *)buf
);
152 if (magic
== VMDK3_MAGIC
||
153 magic
== VMDK4_MAGIC
) {
156 const char *p
= (const char *)buf
;
157 const char *end
= p
+ buf_size
;
160 /* skip comment line */
161 while (p
< end
&& *p
!= '\n') {
168 while (p
< end
&& *p
== ' ') {
171 /* skip '\r' if windows line endings used. */
172 if (p
< end
&& *p
== '\r') {
175 /* only accept blank lines before 'version=' line */
176 if (p
== end
|| *p
!= '\n') {
182 if (end
- p
>= strlen("version=X\n")) {
183 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
184 strncmp("version=2\n", p
, strlen("version=2\n")) == 0) {
188 if (end
- p
>= strlen("version=X\r\n")) {
189 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
190 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
202 #define SECTOR_SIZE 512
203 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
204 #define BUF_SIZE 4096
205 #define HEADER_SIZE 512 /* first sector of 512 bytes */
207 static void vmdk_free_extents(BlockDriverState
*bs
)
210 BDRVVmdkState
*s
= bs
->opaque
;
213 for (i
= 0; i
< s
->num_extents
; i
++) {
217 g_free(e
->l1_backup_table
);
218 if (e
->file
!= bs
->file
) {
225 static void vmdk_free_last_extent(BlockDriverState
*bs
)
227 BDRVVmdkState
*s
= bs
->opaque
;
229 if (s
->num_extents
== 0) {
233 s
->extents
= g_realloc(s
->extents
, s
->num_extents
* sizeof(VmdkExtent
));
236 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
238 char desc
[DESC_SIZE
];
239 uint32_t cid
= 0xffffffff;
240 const char *p_name
, *cid_str
;
242 BDRVVmdkState
*s
= bs
->opaque
;
245 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
251 cid_str
= "parentCID";
252 cid_str_size
= sizeof("parentCID");
255 cid_str_size
= sizeof("CID");
258 desc
[DESC_SIZE
- 1] = '\0';
259 p_name
= strstr(desc
, cid_str
);
260 if (p_name
!= NULL
) {
261 p_name
+= cid_str_size
;
262 sscanf(p_name
, "%x", &cid
);
268 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
270 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
271 char *p_name
, *tmp_str
;
272 BDRVVmdkState
*s
= bs
->opaque
;
275 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
280 desc
[DESC_SIZE
- 1] = '\0';
281 tmp_str
= strstr(desc
, "parentCID");
282 if (tmp_str
== NULL
) {
286 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
287 p_name
= strstr(desc
, "CID");
288 if (p_name
!= NULL
) {
289 p_name
+= sizeof("CID");
290 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
291 pstrcat(desc
, sizeof(desc
), tmp_desc
);
294 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
302 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
305 BDRVVmdkState
*s
= bs
->opaque
;
306 BlockDriverState
*p_bs
= bs
->backing_hd
;
310 cur_pcid
= vmdk_read_cid(p_bs
, 0);
311 if (s
->parent_cid
!= cur_pcid
) {
321 /* Queue extents, if any, for reopen() */
322 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
323 BlockReopenQueue
*queue
, Error
**errp
)
330 assert(state
!= NULL
);
331 assert(state
->bs
!= NULL
);
334 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
335 "No reopen queue for VMDK extents");
339 s
= state
->bs
->opaque
;
343 for (i
= 0; i
< s
->num_extents
; i
++) {
345 if (e
->file
!= state
->bs
->file
) {
346 bdrv_reopen_queue(queue
, e
->file
, state
->flags
);
355 static int vmdk_parent_open(BlockDriverState
*bs
)
358 char desc
[DESC_SIZE
+ 1];
359 BDRVVmdkState
*s
= bs
->opaque
;
362 desc
[DESC_SIZE
] = '\0';
363 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
368 p_name
= strstr(desc
, "parentFileNameHint");
369 if (p_name
!= NULL
) {
372 p_name
+= sizeof("parentFileNameHint") + 1;
373 end_name
= strchr(p_name
, '\"');
374 if (end_name
== NULL
) {
377 if ((end_name
- p_name
) > sizeof(bs
->backing_file
) - 1) {
381 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
387 /* Create and append extent to the extent array. Return the added VmdkExtent
388 * address. return NULL if allocation failed. */
389 static int vmdk_add_extent(BlockDriverState
*bs
,
390 BlockDriverState
*file
, bool flat
, int64_t sectors
,
391 int64_t l1_offset
, int64_t l1_backup_offset
,
393 int l2_size
, uint64_t cluster_sectors
,
394 VmdkExtent
**new_extent
)
397 BDRVVmdkState
*s
= bs
->opaque
;
399 if (cluster_sectors
> 0x200000) {
400 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
401 error_report("invalid granularity, image may be corrupt");
404 if (l1_size
> 512 * 1024 * 1024) {
405 /* Although with big capacity and small l1_entry_sectors, we can get a
406 * big l1_size, we don't want unbounded value to allocate the table.
407 * Limit it to 512M, which is 16PB for default cluster and L2 table
409 error_report("L1 size too big");
413 s
->extents
= g_realloc(s
->extents
,
414 (s
->num_extents
+ 1) * sizeof(VmdkExtent
));
415 extent
= &s
->extents
[s
->num_extents
];
418 memset(extent
, 0, sizeof(VmdkExtent
));
421 extent
->sectors
= sectors
;
422 extent
->l1_table_offset
= l1_offset
;
423 extent
->l1_backup_table_offset
= l1_backup_offset
;
424 extent
->l1_size
= l1_size
;
425 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
426 extent
->l2_size
= l2_size
;
427 extent
->cluster_sectors
= cluster_sectors
;
429 if (s
->num_extents
> 1) {
430 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
432 extent
->end_sector
= extent
->sectors
;
434 bs
->total_sectors
= extent
->end_sector
;
436 *new_extent
= extent
;
441 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
)
446 /* read the L1 table */
447 l1_size
= extent
->l1_size
* sizeof(uint32_t);
448 extent
->l1_table
= g_malloc(l1_size
);
449 ret
= bdrv_pread(extent
->file
,
450 extent
->l1_table_offset
,
456 for (i
= 0; i
< extent
->l1_size
; i
++) {
457 le32_to_cpus(&extent
->l1_table
[i
]);
460 if (extent
->l1_backup_table_offset
) {
461 extent
->l1_backup_table
= g_malloc(l1_size
);
462 ret
= bdrv_pread(extent
->file
,
463 extent
->l1_backup_table_offset
,
464 extent
->l1_backup_table
,
469 for (i
= 0; i
< extent
->l1_size
; i
++) {
470 le32_to_cpus(&extent
->l1_backup_table
[i
]);
475 g_malloc(extent
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
478 g_free(extent
->l1_backup_table
);
480 g_free(extent
->l1_table
);
484 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
485 BlockDriverState
*file
,
493 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
497 ret
= vmdk_add_extent(bs
, file
, false,
498 le32_to_cpu(header
.disk_sectors
),
499 le32_to_cpu(header
.l1dir_offset
) << 9,
501 le32_to_cpu(header
.l1dir_size
),
503 le32_to_cpu(header
.granularity
),
508 ret
= vmdk_init_tables(bs
, extent
);
510 /* free extent allocated by vmdk_add_extent */
511 vmdk_free_last_extent(bs
);
516 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
517 uint64_t desc_offset
);
519 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
520 BlockDriverState
*file
,
525 uint32_t l1_size
, l1_entry_sectors
;
528 int64_t l1_backup_offset
= 0;
530 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
534 if (header
.capacity
== 0) {
535 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
537 return vmdk_open_desc_file(bs
, flags
, desc_offset
<< 9);
541 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
543 * The footer takes precedence over the header, so read it in. The
544 * footer starts at offset -1024 from the end: One sector for the
545 * footer, and another one for the end-of-stream marker.
552 uint8_t pad
[512 - 16];
553 } QEMU_PACKED footer_marker
;
557 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
563 uint8_t pad
[512 - 16];
564 } QEMU_PACKED eos_marker
;
565 } QEMU_PACKED footer
;
567 ret
= bdrv_pread(file
,
568 bs
->file
->total_sectors
* 512 - 1536,
569 &footer
, sizeof(footer
));
574 /* Some sanity checks for the footer */
575 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
576 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
577 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
578 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
579 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
580 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
585 header
= footer
.header
;
588 if (le32_to_cpu(header
.version
) >= 3) {
590 snprintf(buf
, sizeof(buf
), "VMDK version %d",
591 le32_to_cpu(header
.version
));
592 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
593 bs
->device_name
, "vmdk", buf
);
597 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
598 error_report("L2 table size too big");
602 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
603 * le64_to_cpu(header
.granularity
);
604 if (l1_entry_sectors
== 0) {
607 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
609 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
610 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
612 ret
= vmdk_add_extent(bs
, file
, false,
613 le64_to_cpu(header
.capacity
),
614 le64_to_cpu(header
.gd_offset
) << 9,
617 le32_to_cpu(header
.num_gtes_per_gt
),
618 le64_to_cpu(header
.granularity
),
624 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
625 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
626 extent
->version
= le32_to_cpu(header
.version
);
627 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
628 ret
= vmdk_init_tables(bs
, extent
);
630 /* free extent allocated by vmdk_add_extent */
631 vmdk_free_last_extent(bs
);
636 /* find an option value out of descriptor file */
637 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
638 char *buf
, int buf_size
)
640 char *opt_pos
, *opt_end
;
641 const char *end
= desc
+ strlen(desc
);
643 opt_pos
= strstr(desc
, opt_name
);
647 /* Skip "=\"" following opt_name */
648 opt_pos
+= strlen(opt_name
) + 2;
649 if (opt_pos
>= end
) {
653 while (opt_end
< end
&& *opt_end
!= '"') {
656 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
659 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
663 /* Open an extent file and append to bs array */
664 static int vmdk_open_sparse(BlockDriverState
*bs
,
665 BlockDriverState
*file
,
670 if (bdrv_pread(file
, 0, &magic
, sizeof(magic
)) != sizeof(magic
)) {
674 magic
= be32_to_cpu(magic
);
677 return vmdk_open_vmfs_sparse(bs
, file
, flags
);
680 return vmdk_open_vmdk4(bs
, file
, flags
);
688 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
689 const char *desc_file_path
)
695 const char *p
= desc
;
698 char extent_path
[PATH_MAX
];
699 BlockDriverState
*extent_file
;
702 /* parse extent line:
703 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
705 * RW [size in sectors] SPARSE "file-name.vmdk"
708 ret
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
709 access
, §ors
, type
, fname
, &flat_offset
);
710 if (ret
< 4 || strcmp(access
, "RW")) {
712 } else if (!strcmp(type
, "FLAT")) {
713 if (ret
!= 5 || flat_offset
< 0) {
716 } else if (ret
!= 4) {
721 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
722 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
723 (strcmp(access
, "RW"))) {
727 path_combine(extent_path
, sizeof(extent_path
),
728 desc_file_path
, fname
);
729 ret
= bdrv_file_open(&extent_file
, extent_path
, NULL
, bs
->open_flags
);
734 /* save to extents array */
735 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
739 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
740 0, 0, 0, 0, sectors
, &extent
);
744 extent
->flat_start_offset
= flat_offset
<< 9;
745 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
746 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
747 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
);
749 bdrv_unref(extent_file
);
754 "VMDK: Not supported extent type \"%s\""".\n", type
);
758 /* move to next line */
759 while (*p
&& *p
!= '\n') {
767 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
768 uint64_t desc_offset
)
773 BDRVVmdkState
*s
= bs
->opaque
;
776 size
= bdrv_getlength(bs
->file
);
781 size
= MIN(size
, 1 << 20); /* avoid unbounded allocation */
782 buf
= g_malloc0(size
+ 1);
784 ret
= bdrv_pread(bs
->file
, desc_offset
, buf
, size
);
788 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
792 if (strcmp(ct
, "monolithicFlat") &&
793 strcmp(ct
, "vmfs") &&
794 strcmp(ct
, "vmfsSparse") &&
795 strcmp(ct
, "twoGbMaxExtentSparse") &&
796 strcmp(ct
, "twoGbMaxExtentFlat")) {
798 "VMDK: Not supported image type \"%s\""".\n", ct
);
803 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->filename
);
809 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
813 BDRVVmdkState
*s
= bs
->opaque
;
815 if (vmdk_open_sparse(bs
, bs
->file
, flags
) == 0) {
816 s
->desc_offset
= 0x200;
818 ret
= vmdk_open_desc_file(bs
, flags
, 0);
823 /* try to open parent images, if exist */
824 ret
= vmdk_parent_open(bs
);
828 s
->parent_cid
= vmdk_read_cid(bs
, 1);
829 qemu_co_mutex_init(&s
->lock
);
831 /* Disable migration when VMDK images are used */
832 error_set(&s
->migration_blocker
,
833 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
834 "vmdk", bs
->device_name
, "live migration");
835 migrate_add_blocker(s
->migration_blocker
);
840 vmdk_free_extents(bs
);
844 static int get_whole_cluster(BlockDriverState
*bs
,
846 uint64_t cluster_offset
,
851 uint8_t *whole_grain
= NULL
;
853 /* we will be here if it's first write on non-exist grain(cluster).
854 * try to read from parent image, if exist */
855 if (bs
->backing_hd
) {
857 qemu_blockalign(bs
, extent
->cluster_sectors
<< BDRV_SECTOR_BITS
);
858 if (!vmdk_is_cid_valid(bs
)) {
863 /* floor offset to cluster */
864 offset
-= offset
% (extent
->cluster_sectors
* 512);
865 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
866 extent
->cluster_sectors
);
872 /* Write grain only into the active image */
873 ret
= bdrv_write(extent
->file
, cluster_offset
, whole_grain
,
874 extent
->cluster_sectors
);
881 qemu_vfree(whole_grain
);
885 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
)
888 QEMU_BUILD_BUG_ON(sizeof(offset
) != sizeof(m_data
->offset
));
889 offset
= cpu_to_le32(m_data
->offset
);
890 /* update L2 table */
891 if (bdrv_pwrite_sync(
893 ((int64_t)m_data
->l2_offset
* 512)
894 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
895 &offset
, sizeof(offset
)) < 0) {
898 /* update backup L2 table */
899 if (extent
->l1_backup_table_offset
!= 0) {
900 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
901 if (bdrv_pwrite_sync(
903 ((int64_t)m_data
->l2_offset
* 512)
904 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
905 &offset
, sizeof(offset
)) < 0) {
909 if (m_data
->l2_cache_entry
) {
910 *m_data
->l2_cache_entry
= offset
;
916 static int get_cluster_offset(BlockDriverState
*bs
,
918 VmdkMetaData
*m_data
,
921 uint64_t *cluster_offset
)
923 unsigned int l1_index
, l2_offset
, l2_index
;
925 uint32_t min_count
, *l2_table
;
932 *cluster_offset
= extent
->flat_start_offset
;
936 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
937 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
938 if (l1_index
>= extent
->l1_size
) {
941 l2_offset
= extent
->l1_table
[l1_index
];
945 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
946 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
947 /* increment the hit count */
948 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
949 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
950 extent
->l2_cache_counts
[j
] >>= 1;
953 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
957 /* not found: load a new entry in the least used one */
959 min_count
= 0xffffffff;
960 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
961 if (extent
->l2_cache_counts
[i
] < min_count
) {
962 min_count
= extent
->l2_cache_counts
[i
];
966 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
969 (int64_t)l2_offset
* 512,
971 extent
->l2_size
* sizeof(uint32_t)
972 ) != extent
->l2_size
* sizeof(uint32_t)) {
976 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
977 extent
->l2_cache_counts
[min_index
] = 1;
979 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
980 *cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
984 m_data
->l1_index
= l1_index
;
985 m_data
->l2_index
= l2_index
;
986 m_data
->offset
= *cluster_offset
;
987 m_data
->l2_offset
= l2_offset
;
988 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
990 if (extent
->has_zero_grain
&& *cluster_offset
== VMDK_GTE_ZEROED
) {
994 if (!*cluster_offset
|| zeroed
) {
996 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
999 /* Avoid the L2 tables update for the images that have snapshots. */
1000 *cluster_offset
= bdrv_getlength(extent
->file
);
1001 if (!extent
->compressed
) {
1004 *cluster_offset
+ (extent
->cluster_sectors
<< 9)
1008 *cluster_offset
>>= 9;
1009 l2_table
[l2_index
] = cpu_to_le32(*cluster_offset
);
1011 /* First of all we write grain itself, to avoid race condition
1012 * that may to corrupt the image.
1013 * This problem may occur because of insufficient space on host disk
1014 * or inappropriate VM shutdown.
1016 if (get_whole_cluster(
1017 bs
, extent
, *cluster_offset
, offset
, allocate
) == -1) {
1022 m_data
->offset
= *cluster_offset
;
1025 *cluster_offset
<<= 9;
1029 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1030 int64_t sector_num
, VmdkExtent
*start_hint
)
1032 VmdkExtent
*extent
= start_hint
;
1035 extent
= &s
->extents
[0];
1037 while (extent
< &s
->extents
[s
->num_extents
]) {
1038 if (sector_num
< extent
->end_sector
) {
1046 static int64_t coroutine_fn
vmdk_co_get_block_status(BlockDriverState
*bs
,
1047 int64_t sector_num
, int nb_sectors
, int *pnum
)
1049 BDRVVmdkState
*s
= bs
->opaque
;
1050 int64_t index_in_cluster
, n
, ret
;
1054 extent
= find_extent(s
, sector_num
, NULL
);
1058 qemu_co_mutex_lock(&s
->lock
);
1059 ret
= get_cluster_offset(bs
, extent
, NULL
,
1060 sector_num
* 512, 0, &offset
);
1061 qemu_co_mutex_unlock(&s
->lock
);
1071 ret
= BDRV_BLOCK_ZERO
;
1074 ret
= BDRV_BLOCK_DATA
;
1075 if (extent
->file
== bs
->file
) {
1076 ret
|= BDRV_BLOCK_OFFSET_VALID
| offset
;
1082 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1083 n
= extent
->cluster_sectors
- index_in_cluster
;
1084 if (n
> nb_sectors
) {
1091 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1092 int64_t offset_in_cluster
, const uint8_t *buf
,
1093 int nb_sectors
, int64_t sector_num
)
1096 VmdkGrainMarker
*data
= NULL
;
1098 const uint8_t *write_buf
= buf
;
1099 int write_len
= nb_sectors
* 512;
1101 if (extent
->compressed
) {
1102 if (!extent
->has_marker
) {
1106 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1107 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1108 if (compress(data
->data
, &buf_len
, buf
, nb_sectors
<< 9) != Z_OK
||
1113 data
->lba
= sector_num
;
1114 data
->size
= buf_len
;
1115 write_buf
= (uint8_t *)data
;
1116 write_len
= buf_len
+ sizeof(VmdkGrainMarker
);
1118 ret
= bdrv_pwrite(extent
->file
,
1119 cluster_offset
+ offset_in_cluster
,
1122 if (ret
!= write_len
) {
1123 ret
= ret
< 0 ? ret
: -EIO
;
1132 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1133 int64_t offset_in_cluster
, uint8_t *buf
,
1137 int cluster_bytes
, buf_bytes
;
1138 uint8_t *cluster_buf
, *compressed_data
;
1139 uint8_t *uncomp_buf
;
1141 VmdkGrainMarker
*marker
;
1145 if (!extent
->compressed
) {
1146 ret
= bdrv_pread(extent
->file
,
1147 cluster_offset
+ offset_in_cluster
,
1148 buf
, nb_sectors
* 512);
1149 if (ret
== nb_sectors
* 512) {
1155 cluster_bytes
= extent
->cluster_sectors
* 512;
1156 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1157 buf_bytes
= cluster_bytes
* 2;
1158 cluster_buf
= g_malloc(buf_bytes
);
1159 uncomp_buf
= g_malloc(cluster_bytes
);
1160 ret
= bdrv_pread(extent
->file
,
1162 cluster_buf
, buf_bytes
);
1166 compressed_data
= cluster_buf
;
1167 buf_len
= cluster_bytes
;
1168 data_len
= cluster_bytes
;
1169 if (extent
->has_marker
) {
1170 marker
= (VmdkGrainMarker
*)cluster_buf
;
1171 compressed_data
= marker
->data
;
1172 data_len
= le32_to_cpu(marker
->size
);
1174 if (!data_len
|| data_len
> buf_bytes
) {
1178 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1184 if (offset_in_cluster
< 0 ||
1185 offset_in_cluster
+ nb_sectors
* 512 > buf_len
) {
1189 memcpy(buf
, uncomp_buf
+ offset_in_cluster
, nb_sectors
* 512);
1194 g_free(cluster_buf
);
1198 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
1199 uint8_t *buf
, int nb_sectors
)
1201 BDRVVmdkState
*s
= bs
->opaque
;
1203 uint64_t n
, index_in_cluster
;
1204 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1205 VmdkExtent
*extent
= NULL
;
1206 uint64_t cluster_offset
;
1208 while (nb_sectors
> 0) {
1209 extent
= find_extent(s
, sector_num
, extent
);
1213 ret
= get_cluster_offset(
1215 sector_num
<< 9, 0, &cluster_offset
);
1216 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1217 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1218 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1219 n
= extent
->cluster_sectors
- index_in_cluster
;
1220 if (n
> nb_sectors
) {
1223 if (ret
!= VMDK_OK
) {
1224 /* if not allocated, try to read from parent image, if exist */
1225 if (bs
->backing_hd
&& ret
!= VMDK_ZEROED
) {
1226 if (!vmdk_is_cid_valid(bs
)) {
1229 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
1234 memset(buf
, 0, 512 * n
);
1237 ret
= vmdk_read_extent(extent
,
1238 cluster_offset
, index_in_cluster
* 512,
1251 static coroutine_fn
int vmdk_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1252 uint8_t *buf
, int nb_sectors
)
1255 BDRVVmdkState
*s
= bs
->opaque
;
1256 qemu_co_mutex_lock(&s
->lock
);
1257 ret
= vmdk_read(bs
, sector_num
, buf
, nb_sectors
);
1258 qemu_co_mutex_unlock(&s
->lock
);
1264 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1265 * if possible, otherwise return -ENOTSUP.
1266 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1267 * with each cluster. By dry run we can find if the zero write
1268 * is possible without modifying image data.
1270 * Returns: error code with 0 for success.
1272 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
1273 const uint8_t *buf
, int nb_sectors
,
1274 bool zeroed
, bool zero_dry_run
)
1276 BDRVVmdkState
*s
= bs
->opaque
;
1277 VmdkExtent
*extent
= NULL
;
1279 int64_t index_in_cluster
;
1280 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1281 uint64_t cluster_offset
;
1282 VmdkMetaData m_data
;
1284 if (sector_num
> bs
->total_sectors
) {
1286 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
1287 " total_sectors=0x%" PRIx64
"\n",
1288 sector_num
, bs
->total_sectors
);
1292 while (nb_sectors
> 0) {
1293 extent
= find_extent(s
, sector_num
, extent
);
1297 ret
= get_cluster_offset(
1301 sector_num
<< 9, !extent
->compressed
,
1303 if (extent
->compressed
) {
1304 if (ret
== VMDK_OK
) {
1305 /* Refuse write to allocated cluster for streamOptimized */
1307 "VMDK: can't write to allocated cluster"
1308 " for streamOptimized\n");
1312 ret
= get_cluster_offset(
1320 if (ret
== VMDK_ERROR
) {
1323 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1324 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1325 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1326 n
= extent
->cluster_sectors
- index_in_cluster
;
1327 if (n
> nb_sectors
) {
1331 /* Do zeroed write, buf is ignored */
1332 if (extent
->has_zero_grain
&&
1333 index_in_cluster
== 0 &&
1334 n
>= extent
->cluster_sectors
) {
1335 n
= extent
->cluster_sectors
;
1336 if (!zero_dry_run
) {
1337 m_data
.offset
= VMDK_GTE_ZEROED
;
1338 /* update L2 tables */
1339 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1347 ret
= vmdk_write_extent(extent
,
1348 cluster_offset
, index_in_cluster
* 512,
1349 buf
, n
, sector_num
);
1354 /* update L2 tables */
1355 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1364 /* update CID on the first write every time the virtual disk is
1366 if (!s
->cid_updated
) {
1367 ret
= vmdk_write_cid(bs
, time(NULL
));
1371 s
->cid_updated
= true;
1377 static coroutine_fn
int vmdk_co_write(BlockDriverState
*bs
, int64_t sector_num
,
1378 const uint8_t *buf
, int nb_sectors
)
1381 BDRVVmdkState
*s
= bs
->opaque
;
1382 qemu_co_mutex_lock(&s
->lock
);
1383 ret
= vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1384 qemu_co_mutex_unlock(&s
->lock
);
1388 static int coroutine_fn
vmdk_co_write_zeroes(BlockDriverState
*bs
,
1393 BDRVVmdkState
*s
= bs
->opaque
;
1394 qemu_co_mutex_lock(&s
->lock
);
1395 /* write zeroes could fail if sectors not aligned to cluster, test it with
1396 * dry_run == true before really updating image */
1397 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, true);
1399 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, false);
1401 qemu_co_mutex_unlock(&s
->lock
);
1405 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1406 bool flat
, bool compress
, bool zeroed_grain
)
1411 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
1413 fd
= qemu_open(filename
,
1414 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1420 ret
= ftruncate(fd
, filesize
);
1426 magic
= cpu_to_be32(VMDK4_MAGIC
);
1427 memset(&header
, 0, sizeof(header
));
1428 header
.version
= zeroed_grain
? 2 : 1;
1429 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1430 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1431 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1432 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1433 header
.capacity
= filesize
/ 512;
1434 header
.granularity
= 128;
1435 header
.num_gtes_per_gt
= 512;
1437 grains
= (filesize
/ 512 + header
.granularity
- 1) / header
.granularity
;
1438 gt_size
= ((header
.num_gtes_per_gt
* sizeof(uint32_t)) + 511) >> 9;
1440 (grains
+ header
.num_gtes_per_gt
- 1) / header
.num_gtes_per_gt
;
1441 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
1443 header
.desc_offset
= 1;
1444 header
.desc_size
= 20;
1445 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1446 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
1447 header
.grain_offset
=
1448 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
1449 header
.granularity
- 1) / header
.granularity
) *
1451 /* swap endianness for all header fields */
1452 header
.version
= cpu_to_le32(header
.version
);
1453 header
.flags
= cpu_to_le32(header
.flags
);
1454 header
.capacity
= cpu_to_le64(header
.capacity
);
1455 header
.granularity
= cpu_to_le64(header
.granularity
);
1456 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1457 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1458 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1459 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1460 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1461 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1462 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1464 header
.check_bytes
[0] = 0xa;
1465 header
.check_bytes
[1] = 0x20;
1466 header
.check_bytes
[2] = 0xd;
1467 header
.check_bytes
[3] = 0xa;
1469 /* write all the data */
1470 ret
= qemu_write_full(fd
, &magic
, sizeof(magic
));
1471 if (ret
!= sizeof(magic
)) {
1475 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
1476 if (ret
!= sizeof(header
)) {
1481 ret
= ftruncate(fd
, le64_to_cpu(header
.grain_offset
) << 9);
1487 /* write grain directory */
1488 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
1489 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_size
;
1490 i
< gt_count
; i
++, tmp
+= gt_size
) {
1491 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1492 if (ret
!= sizeof(tmp
)) {
1498 /* write backup grain directory */
1499 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
1500 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_size
;
1501 i
< gt_count
; i
++, tmp
+= gt_size
) {
1502 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1503 if (ret
!= sizeof(tmp
)) {
1515 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1516 char *postfix
, size_t buf_len
)
1520 if (filename
== NULL
|| !strlen(filename
)) {
1521 fprintf(stderr
, "Vmdk: no filename provided.\n");
1524 p
= strrchr(filename
, '/');
1526 p
= strrchr(filename
, '\\');
1529 p
= strrchr(filename
, ':');
1533 if (p
- filename
>= buf_len
) {
1536 pstrcpy(path
, p
- filename
+ 1, filename
);
1541 q
= strrchr(p
, '.');
1543 pstrcpy(prefix
, buf_len
, p
);
1546 if (q
- p
>= buf_len
) {
1549 pstrcpy(prefix
, q
- p
+ 1, p
);
1550 pstrcpy(postfix
, buf_len
, q
);
1555 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
,
1559 char desc
[BUF_SIZE
];
1560 int64_t total_size
= 0, filesize
;
1561 const char *adapter_type
= NULL
;
1562 const char *backing_file
= NULL
;
1563 const char *fmt
= NULL
;
1566 bool flat
, split
, compress
;
1567 char ext_desc_lines
[BUF_SIZE
] = "";
1568 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1569 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1570 const char *desc_extent_line
;
1571 char parent_desc_line
[BUF_SIZE
] = "";
1572 uint32_t parent_cid
= 0xffffffff;
1573 uint32_t number_heads
= 16;
1574 bool zeroed_grain
= false;
1575 const char desc_template
[] =
1576 "# Disk DescriptorFile\n"
1580 "createType=\"%s\"\n"
1583 "# Extent description\n"
1586 "# The Disk Data Base\n"
1589 "ddb.virtualHWVersion = \"%d\"\n"
1590 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1591 "ddb.geometry.heads = \"%d\"\n"
1592 "ddb.geometry.sectors = \"63\"\n"
1593 "ddb.adapterType = \"%s\"\n";
1595 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
)) {
1598 /* Read out options */
1599 while (options
&& options
->name
) {
1600 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1601 total_size
= options
->value
.n
;
1602 } else if (!strcmp(options
->name
, BLOCK_OPT_ADAPTER_TYPE
)) {
1603 adapter_type
= options
->value
.s
;
1604 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1605 backing_file
= options
->value
.s
;
1606 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
1607 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
1608 } else if (!strcmp(options
->name
, BLOCK_OPT_SUBFMT
)) {
1609 fmt
= options
->value
.s
;
1610 } else if (!strcmp(options
->name
, BLOCK_OPT_ZEROED_GRAIN
)) {
1611 zeroed_grain
|= options
->value
.n
;
1615 if (!adapter_type
) {
1616 adapter_type
= "ide";
1617 } else if (strcmp(adapter_type
, "ide") &&
1618 strcmp(adapter_type
, "buslogic") &&
1619 strcmp(adapter_type
, "lsilogic") &&
1620 strcmp(adapter_type
, "legacyESX")) {
1621 fprintf(stderr
, "VMDK: Unknown adapter type: '%s'.\n", adapter_type
);
1624 if (strcmp(adapter_type
, "ide") != 0) {
1625 /* that's the number of heads with which vmware operates when
1626 creating, exporting, etc. vmdk files with a non-ide adapter type */
1630 /* Default format to monolithicSparse */
1631 fmt
= "monolithicSparse";
1632 } else if (strcmp(fmt
, "monolithicFlat") &&
1633 strcmp(fmt
, "monolithicSparse") &&
1634 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1635 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1636 strcmp(fmt
, "streamOptimized")) {
1637 fprintf(stderr
, "VMDK: Unknown subformat: %s\n", fmt
);
1640 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1641 strcmp(fmt
, "twoGbMaxExtentSparse"));
1642 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1643 strcmp(fmt
, "twoGbMaxExtentFlat"));
1644 compress
= !strcmp(fmt
, "streamOptimized");
1646 desc_extent_line
= "RW %lld FLAT \"%s\" 0\n";
1648 desc_extent_line
= "RW %lld SPARSE \"%s\"\n";
1650 if (flat
&& backing_file
) {
1651 /* not supporting backing file for flat image */
1655 BlockDriverState
*bs
= bdrv_new("");
1656 ret
= bdrv_open(bs
, backing_file
, NULL
, 0, NULL
);
1661 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1665 parent_cid
= vmdk_read_cid(bs
, 0);
1667 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1668 "parentFileNameHint=\"%s\"", backing_file
);
1671 /* Create extents */
1672 filesize
= total_size
;
1673 while (filesize
> 0) {
1674 char desc_line
[BUF_SIZE
];
1675 char ext_filename
[PATH_MAX
];
1676 char desc_filename
[PATH_MAX
];
1677 int64_t size
= filesize
;
1679 if (split
&& size
> split_size
) {
1683 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1684 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1686 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1689 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1692 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1693 path
, desc_filename
);
1695 if (vmdk_create_extent(ext_filename
, size
,
1696 flat
, compress
, zeroed_grain
)) {
1701 /* Format description line */
1702 snprintf(desc_line
, sizeof(desc_line
),
1703 desc_extent_line
, size
/ 512, desc_filename
);
1704 pstrcat(ext_desc_lines
, sizeof(ext_desc_lines
), desc_line
);
1706 /* generate descriptor file */
1707 snprintf(desc
, sizeof(desc
), desc_template
,
1708 (unsigned int)time(NULL
),
1713 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1714 total_size
/ (int64_t)(63 * number_heads
* 512), number_heads
,
1716 if (split
|| flat
) {
1717 fd
= qemu_open(filename
,
1718 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1721 fd
= qemu_open(filename
,
1722 O_WRONLY
| O_BINARY
| O_LARGEFILE
,
1728 /* the descriptor offset = 0x200 */
1729 if (!split
&& !flat
&& 0x200 != lseek(fd
, 0x200, SEEK_SET
)) {
1733 ret
= qemu_write_full(fd
, desc
, strlen(desc
));
1734 if (ret
!= strlen(desc
)) {
1744 static void vmdk_close(BlockDriverState
*bs
)
1746 BDRVVmdkState
*s
= bs
->opaque
;
1748 vmdk_free_extents(bs
);
1750 migrate_del_blocker(s
->migration_blocker
);
1751 error_free(s
->migration_blocker
);
1754 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
1756 BDRVVmdkState
*s
= bs
->opaque
;
1760 for (i
= 0; i
< s
->num_extents
; i
++) {
1761 err
= bdrv_co_flush(s
->extents
[i
].file
);
1769 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
1774 BDRVVmdkState
*s
= bs
->opaque
;
1776 ret
= bdrv_get_allocated_file_size(bs
->file
);
1780 for (i
= 0; i
< s
->num_extents
; i
++) {
1781 if (s
->extents
[i
].file
== bs
->file
) {
1784 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
1793 static int vmdk_has_zero_init(BlockDriverState
*bs
)
1796 BDRVVmdkState
*s
= bs
->opaque
;
1798 /* If has a flat extent and its underlying storage doesn't have zero init,
1800 for (i
= 0; i
< s
->num_extents
; i
++) {
1801 if (s
->extents
[i
].flat
) {
1802 if (!bdrv_has_zero_init(s
->extents
[i
].file
)) {
1810 static QEMUOptionParameter vmdk_create_options
[] = {
1812 .name
= BLOCK_OPT_SIZE
,
1814 .help
= "Virtual disk size"
1817 .name
= BLOCK_OPT_ADAPTER_TYPE
,
1819 .help
= "Virtual adapter type, can be one of "
1820 "ide (default), lsilogic, buslogic or legacyESX"
1823 .name
= BLOCK_OPT_BACKING_FILE
,
1825 .help
= "File name of a base image"
1828 .name
= BLOCK_OPT_COMPAT6
,
1830 .help
= "VMDK version 6 image"
1833 .name
= BLOCK_OPT_SUBFMT
,
1836 "VMDK flat extent format, can be one of "
1837 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1840 .name
= BLOCK_OPT_ZEROED_GRAIN
,
1842 .help
= "Enable efficient zero writes using the zeroed-grain GTE feature"
1847 static BlockDriver bdrv_vmdk
= {
1848 .format_name
= "vmdk",
1849 .instance_size
= sizeof(BDRVVmdkState
),
1850 .bdrv_probe
= vmdk_probe
,
1851 .bdrv_open
= vmdk_open
,
1852 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
1853 .bdrv_read
= vmdk_co_read
,
1854 .bdrv_write
= vmdk_co_write
,
1855 .bdrv_co_write_zeroes
= vmdk_co_write_zeroes
,
1856 .bdrv_close
= vmdk_close
,
1857 .bdrv_create
= vmdk_create
,
1858 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
1859 .bdrv_co_get_block_status
= vmdk_co_get_block_status
,
1860 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
1861 .bdrv_has_zero_init
= vmdk_has_zero_init
,
1863 .create_options
= vmdk_create_options
,
1866 static void bdrv_vmdk_init(void)
1868 bdrv_register(&bdrv_vmdk
);
1871 block_init(bdrv_vmdk_init
);