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
, "%" SCNx32
, &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
), "%" PRIx32
"\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
, char *buf
,
532 static char *vmdk_read_desc(BlockDriverState
*file
, uint64_t desc_offset
,
539 size
= bdrv_getlength(file
);
541 error_setg_errno(errp
, -size
, "Could not access file");
545 size
= MIN(size
, 1 << 20); /* avoid unbounded allocation */
546 buf
= g_malloc0(size
+ 1);
548 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
550 error_setg_errno(errp
, -ret
, "Could not read from file");
558 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
559 BlockDriverState
*file
,
560 int flags
, Error
**errp
)
564 uint32_t l1_size
, l1_entry_sectors
;
567 BDRVVmdkState
*s
= bs
->opaque
;
568 int64_t l1_backup_offset
= 0;
570 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
572 error_setg_errno(errp
, -ret
,
573 "Could not read header from file '%s'",
577 if (header
.capacity
== 0) {
578 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
580 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
584 ret
= vmdk_open_desc_file(bs
, flags
, buf
, errp
);
590 if (!s
->create_type
) {
591 s
->create_type
= g_strdup("monolithicSparse");
594 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
596 * The footer takes precedence over the header, so read it in. The
597 * footer starts at offset -1024 from the end: One sector for the
598 * footer, and another one for the end-of-stream marker.
605 uint8_t pad
[512 - 16];
606 } QEMU_PACKED footer_marker
;
610 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
616 uint8_t pad
[512 - 16];
617 } QEMU_PACKED eos_marker
;
618 } QEMU_PACKED footer
;
620 ret
= bdrv_pread(file
,
621 bs
->file
->total_sectors
* 512 - 1536,
622 &footer
, sizeof(footer
));
627 /* Some sanity checks for the footer */
628 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
629 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
630 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
631 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
632 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
633 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
638 header
= footer
.header
;
641 if (le32_to_cpu(header
.version
) > 3) {
643 snprintf(buf
, sizeof(buf
), "VMDK version %" PRId32
,
644 le32_to_cpu(header
.version
));
645 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
646 bs
->device_name
, "vmdk", buf
);
648 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
)) {
649 /* VMware KB 2064959 explains that version 3 added support for
650 * persistent changed block tracking (CBT), and backup software can
651 * read it as version=1 if it doesn't care about the changed area
652 * information. So we are safe to enable read only. */
653 error_setg(errp
, "VMDK version 3 must be read only");
657 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
658 error_setg(errp
, "L2 table size too big");
662 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
663 * le64_to_cpu(header
.granularity
);
664 if (l1_entry_sectors
== 0) {
667 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
669 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
670 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
672 if (bdrv_getlength(file
) <
673 le64_to_cpu(header
.grain_offset
) * BDRV_SECTOR_SIZE
) {
674 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
675 (int64_t)(le64_to_cpu(header
.grain_offset
)
676 * BDRV_SECTOR_SIZE
));
680 ret
= vmdk_add_extent(bs
, file
, false,
681 le64_to_cpu(header
.capacity
),
682 le64_to_cpu(header
.gd_offset
) << 9,
685 le32_to_cpu(header
.num_gtes_per_gt
),
686 le64_to_cpu(header
.granularity
),
693 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
694 if (extent
->compressed
) {
695 g_free(s
->create_type
);
696 s
->create_type
= g_strdup("streamOptimized");
698 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
699 extent
->version
= le32_to_cpu(header
.version
);
700 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
701 ret
= vmdk_init_tables(bs
, extent
, errp
);
703 /* free extent allocated by vmdk_add_extent */
704 vmdk_free_last_extent(bs
);
709 /* find an option value out of descriptor file */
710 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
711 char *buf
, int buf_size
)
713 char *opt_pos
, *opt_end
;
714 const char *end
= desc
+ strlen(desc
);
716 opt_pos
= strstr(desc
, opt_name
);
720 /* Skip "=\"" following opt_name */
721 opt_pos
+= strlen(opt_name
) + 2;
722 if (opt_pos
>= end
) {
726 while (opt_end
< end
&& *opt_end
!= '"') {
729 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
732 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
736 /* Open an extent file and append to bs array */
737 static int vmdk_open_sparse(BlockDriverState
*bs
,
738 BlockDriverState
*file
, int flags
,
739 char *buf
, Error
**errp
)
743 magic
= ldl_be_p(buf
);
746 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
749 return vmdk_open_vmdk4(bs
, file
, flags
, errp
);
752 error_setg(errp
, "Image not in VMDK format");
758 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
759 const char *desc_file_path
, Error
**errp
)
765 const char *p
= desc
;
768 char extent_path
[PATH_MAX
];
769 BlockDriverState
*extent_file
;
770 BDRVVmdkState
*s
= bs
->opaque
;
774 /* parse extent line:
775 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
777 * RW [size in sectors] SPARSE "file-name.vmdk"
780 ret
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
781 access
, §ors
, type
, fname
, &flat_offset
);
782 if (ret
< 4 || strcmp(access
, "RW")) {
784 } else if (!strcmp(type
, "FLAT")) {
785 if (ret
!= 5 || flat_offset
< 0) {
786 error_setg(errp
, "Invalid extent lines: \n%s", p
);
789 } else if (!strcmp(type
, "VMFS")) {
793 error_setg(errp
, "Invalid extent lines:\n%s", p
);
796 } else if (ret
!= 4) {
797 error_setg(errp
, "Invalid extent lines:\n%s", p
);
802 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
803 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
804 (strcmp(access
, "RW"))) {
808 path_combine(extent_path
, sizeof(extent_path
),
809 desc_file_path
, fname
);
811 ret
= bdrv_open(&extent_file
, extent_path
, NULL
, NULL
,
812 bs
->open_flags
| BDRV_O_PROTOCOL
, NULL
, errp
);
817 /* save to extents array */
818 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
821 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
822 0, 0, 0, 0, 0, &extent
, errp
);
826 extent
->flat_start_offset
= flat_offset
<< 9;
827 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
828 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
829 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
833 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
, errp
);
837 bdrv_unref(extent_file
);
840 extent
= &s
->extents
[s
->num_extents
- 1];
842 error_setg(errp
, "Unsupported extent type '%s'", type
);
845 extent
->type
= g_strdup(type
);
847 /* move to next line */
859 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
864 BDRVVmdkState
*s
= bs
->opaque
;
866 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
867 error_setg(errp
, "invalid VMDK image descriptor");
871 if (strcmp(ct
, "monolithicFlat") &&
872 strcmp(ct
, "vmfs") &&
873 strcmp(ct
, "vmfsSparse") &&
874 strcmp(ct
, "twoGbMaxExtentSparse") &&
875 strcmp(ct
, "twoGbMaxExtentFlat")) {
876 error_setg(errp
, "Unsupported image type '%s'", ct
);
880 s
->create_type
= g_strdup(ct
);
882 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->filename
, errp
);
887 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
892 BDRVVmdkState
*s
= bs
->opaque
;
895 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
900 magic
= ldl_be_p(buf
);
904 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, errp
);
905 s
->desc_offset
= 0x200;
908 ret
= vmdk_open_desc_file(bs
, flags
, buf
, errp
);
915 /* try to open parent images, if exist */
916 ret
= vmdk_parent_open(bs
);
920 s
->cid
= vmdk_read_cid(bs
, 0);
921 s
->parent_cid
= vmdk_read_cid(bs
, 1);
922 qemu_co_mutex_init(&s
->lock
);
924 /* Disable migration when VMDK images are used */
925 error_set(&s
->migration_blocker
,
926 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
927 "vmdk", bs
->device_name
, "live migration");
928 migrate_add_blocker(s
->migration_blocker
);
934 g_free(s
->create_type
);
935 s
->create_type
= NULL
;
936 vmdk_free_extents(bs
);
941 static int vmdk_refresh_limits(BlockDriverState
*bs
)
943 BDRVVmdkState
*s
= bs
->opaque
;
946 for (i
= 0; i
< s
->num_extents
; i
++) {
947 if (!s
->extents
[i
].flat
) {
948 bs
->bl
.write_zeroes_alignment
=
949 MAX(bs
->bl
.write_zeroes_alignment
,
950 s
->extents
[i
].cluster_sectors
);
957 static int get_whole_cluster(BlockDriverState
*bs
,
959 uint64_t cluster_offset
,
964 uint8_t *whole_grain
= NULL
;
966 /* we will be here if it's first write on non-exist grain(cluster).
967 * try to read from parent image, if exist */
968 if (bs
->backing_hd
) {
970 qemu_blockalign(bs
, extent
->cluster_sectors
<< BDRV_SECTOR_BITS
);
971 if (!vmdk_is_cid_valid(bs
)) {
976 /* floor offset to cluster */
977 offset
-= offset
% (extent
->cluster_sectors
* 512);
978 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
979 extent
->cluster_sectors
);
985 /* Write grain only into the active image */
986 ret
= bdrv_write(extent
->file
, cluster_offset
, whole_grain
,
987 extent
->cluster_sectors
);
994 qemu_vfree(whole_grain
);
998 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
)
1001 QEMU_BUILD_BUG_ON(sizeof(offset
) != sizeof(m_data
->offset
));
1002 offset
= cpu_to_le32(m_data
->offset
);
1003 /* update L2 table */
1004 if (bdrv_pwrite_sync(
1006 ((int64_t)m_data
->l2_offset
* 512)
1007 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
1008 &offset
, sizeof(offset
)) < 0) {
1011 /* update backup L2 table */
1012 if (extent
->l1_backup_table_offset
!= 0) {
1013 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1014 if (bdrv_pwrite_sync(
1016 ((int64_t)m_data
->l2_offset
* 512)
1017 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
1018 &offset
, sizeof(offset
)) < 0) {
1022 if (m_data
->l2_cache_entry
) {
1023 *m_data
->l2_cache_entry
= offset
;
1029 static int get_cluster_offset(BlockDriverState
*bs
,
1031 VmdkMetaData
*m_data
,
1034 uint64_t *cluster_offset
)
1036 unsigned int l1_index
, l2_offset
, l2_index
;
1037 int min_index
, i
, j
;
1038 uint32_t min_count
, *l2_table
;
1039 bool zeroed
= false;
1045 *cluster_offset
= extent
->flat_start_offset
;
1049 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1050 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1051 if (l1_index
>= extent
->l1_size
) {
1054 l2_offset
= extent
->l1_table
[l1_index
];
1056 return VMDK_UNALLOC
;
1058 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1059 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1060 /* increment the hit count */
1061 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1062 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1063 extent
->l2_cache_counts
[j
] >>= 1;
1066 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
1070 /* not found: load a new entry in the least used one */
1072 min_count
= 0xffffffff;
1073 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1074 if (extent
->l2_cache_counts
[i
] < min_count
) {
1075 min_count
= extent
->l2_cache_counts
[i
];
1079 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1082 (int64_t)l2_offset
* 512,
1084 extent
->l2_size
* sizeof(uint32_t)
1085 ) != extent
->l2_size
* sizeof(uint32_t)) {
1089 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1090 extent
->l2_cache_counts
[min_index
] = 1;
1092 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1093 *cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
1097 m_data
->l1_index
= l1_index
;
1098 m_data
->l2_index
= l2_index
;
1099 m_data
->offset
= *cluster_offset
;
1100 m_data
->l2_offset
= l2_offset
;
1101 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1103 if (extent
->has_zero_grain
&& *cluster_offset
== VMDK_GTE_ZEROED
) {
1107 if (!*cluster_offset
|| zeroed
) {
1109 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1112 /* Avoid the L2 tables update for the images that have snapshots. */
1113 *cluster_offset
= bdrv_getlength(extent
->file
);
1114 if (!extent
->compressed
) {
1117 *cluster_offset
+ (extent
->cluster_sectors
<< 9)
1121 *cluster_offset
>>= 9;
1122 l2_table
[l2_index
] = cpu_to_le32(*cluster_offset
);
1124 /* First of all we write grain itself, to avoid race condition
1125 * that may to corrupt the image.
1126 * This problem may occur because of insufficient space on host disk
1127 * or inappropriate VM shutdown.
1129 if (get_whole_cluster(
1130 bs
, extent
, *cluster_offset
, offset
, allocate
) == -1) {
1135 m_data
->offset
= *cluster_offset
;
1138 *cluster_offset
<<= 9;
1142 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1143 int64_t sector_num
, VmdkExtent
*start_hint
)
1145 VmdkExtent
*extent
= start_hint
;
1148 extent
= &s
->extents
[0];
1150 while (extent
< &s
->extents
[s
->num_extents
]) {
1151 if (sector_num
< extent
->end_sector
) {
1159 static int64_t coroutine_fn
vmdk_co_get_block_status(BlockDriverState
*bs
,
1160 int64_t sector_num
, int nb_sectors
, int *pnum
)
1162 BDRVVmdkState
*s
= bs
->opaque
;
1163 int64_t index_in_cluster
, n
, ret
;
1167 extent
= find_extent(s
, sector_num
, NULL
);
1171 qemu_co_mutex_lock(&s
->lock
);
1172 ret
= get_cluster_offset(bs
, extent
, NULL
,
1173 sector_num
* 512, 0, &offset
);
1174 qemu_co_mutex_unlock(&s
->lock
);
1184 ret
= BDRV_BLOCK_ZERO
;
1187 ret
= BDRV_BLOCK_DATA
;
1188 if (extent
->file
== bs
->file
&& !extent
->compressed
) {
1189 ret
|= BDRV_BLOCK_OFFSET_VALID
| offset
;
1195 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1196 n
= extent
->cluster_sectors
- index_in_cluster
;
1197 if (n
> nb_sectors
) {
1204 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1205 int64_t offset_in_cluster
, const uint8_t *buf
,
1206 int nb_sectors
, int64_t sector_num
)
1209 VmdkGrainMarker
*data
= NULL
;
1211 const uint8_t *write_buf
= buf
;
1212 int write_len
= nb_sectors
* 512;
1214 if (extent
->compressed
) {
1215 if (!extent
->has_marker
) {
1219 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1220 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1221 if (compress(data
->data
, &buf_len
, buf
, nb_sectors
<< 9) != Z_OK
||
1226 data
->lba
= sector_num
;
1227 data
->size
= buf_len
;
1228 write_buf
= (uint8_t *)data
;
1229 write_len
= buf_len
+ sizeof(VmdkGrainMarker
);
1231 ret
= bdrv_pwrite(extent
->file
,
1232 cluster_offset
+ offset_in_cluster
,
1235 if (ret
!= write_len
) {
1236 ret
= ret
< 0 ? ret
: -EIO
;
1245 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1246 int64_t offset_in_cluster
, uint8_t *buf
,
1250 int cluster_bytes
, buf_bytes
;
1251 uint8_t *cluster_buf
, *compressed_data
;
1252 uint8_t *uncomp_buf
;
1254 VmdkGrainMarker
*marker
;
1258 if (!extent
->compressed
) {
1259 ret
= bdrv_pread(extent
->file
,
1260 cluster_offset
+ offset_in_cluster
,
1261 buf
, nb_sectors
* 512);
1262 if (ret
== nb_sectors
* 512) {
1268 cluster_bytes
= extent
->cluster_sectors
* 512;
1269 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1270 buf_bytes
= cluster_bytes
* 2;
1271 cluster_buf
= g_malloc(buf_bytes
);
1272 uncomp_buf
= g_malloc(cluster_bytes
);
1273 ret
= bdrv_pread(extent
->file
,
1275 cluster_buf
, buf_bytes
);
1279 compressed_data
= cluster_buf
;
1280 buf_len
= cluster_bytes
;
1281 data_len
= cluster_bytes
;
1282 if (extent
->has_marker
) {
1283 marker
= (VmdkGrainMarker
*)cluster_buf
;
1284 compressed_data
= marker
->data
;
1285 data_len
= le32_to_cpu(marker
->size
);
1287 if (!data_len
|| data_len
> buf_bytes
) {
1291 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1297 if (offset_in_cluster
< 0 ||
1298 offset_in_cluster
+ nb_sectors
* 512 > buf_len
) {
1302 memcpy(buf
, uncomp_buf
+ offset_in_cluster
, nb_sectors
* 512);
1307 g_free(cluster_buf
);
1311 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
1312 uint8_t *buf
, int nb_sectors
)
1314 BDRVVmdkState
*s
= bs
->opaque
;
1316 uint64_t n
, index_in_cluster
;
1317 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1318 VmdkExtent
*extent
= NULL
;
1319 uint64_t cluster_offset
;
1321 while (nb_sectors
> 0) {
1322 extent
= find_extent(s
, sector_num
, extent
);
1326 ret
= get_cluster_offset(
1328 sector_num
<< 9, 0, &cluster_offset
);
1329 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1330 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1331 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1332 n
= extent
->cluster_sectors
- index_in_cluster
;
1333 if (n
> nb_sectors
) {
1336 if (ret
!= VMDK_OK
) {
1337 /* if not allocated, try to read from parent image, if exist */
1338 if (bs
->backing_hd
&& ret
!= VMDK_ZEROED
) {
1339 if (!vmdk_is_cid_valid(bs
)) {
1342 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
1347 memset(buf
, 0, 512 * n
);
1350 ret
= vmdk_read_extent(extent
,
1351 cluster_offset
, index_in_cluster
* 512,
1364 static coroutine_fn
int vmdk_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1365 uint8_t *buf
, int nb_sectors
)
1368 BDRVVmdkState
*s
= bs
->opaque
;
1369 qemu_co_mutex_lock(&s
->lock
);
1370 ret
= vmdk_read(bs
, sector_num
, buf
, nb_sectors
);
1371 qemu_co_mutex_unlock(&s
->lock
);
1377 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1378 * if possible, otherwise return -ENOTSUP.
1379 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1380 * with each cluster. By dry run we can find if the zero write
1381 * is possible without modifying image data.
1383 * Returns: error code with 0 for success.
1385 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
1386 const uint8_t *buf
, int nb_sectors
,
1387 bool zeroed
, bool zero_dry_run
)
1389 BDRVVmdkState
*s
= bs
->opaque
;
1390 VmdkExtent
*extent
= NULL
;
1392 int64_t index_in_cluster
, n
;
1393 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1394 uint64_t cluster_offset
;
1395 VmdkMetaData m_data
;
1397 if (sector_num
> bs
->total_sectors
) {
1398 error_report("Wrong offset: sector_num=0x%" PRIx64
1399 " total_sectors=0x%" PRIx64
"\n",
1400 sector_num
, bs
->total_sectors
);
1404 while (nb_sectors
> 0) {
1405 extent
= find_extent(s
, sector_num
, extent
);
1409 ret
= get_cluster_offset(
1413 sector_num
<< 9, !extent
->compressed
,
1415 if (extent
->compressed
) {
1416 if (ret
== VMDK_OK
) {
1417 /* Refuse write to allocated cluster for streamOptimized */
1418 error_report("Could not write to allocated cluster"
1419 " for streamOptimized");
1423 ret
= get_cluster_offset(
1431 if (ret
== VMDK_ERROR
) {
1434 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1435 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1436 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1437 n
= extent
->cluster_sectors
- index_in_cluster
;
1438 if (n
> nb_sectors
) {
1442 /* Do zeroed write, buf is ignored */
1443 if (extent
->has_zero_grain
&&
1444 index_in_cluster
== 0 &&
1445 n
>= extent
->cluster_sectors
) {
1446 n
= extent
->cluster_sectors
;
1447 if (!zero_dry_run
) {
1448 m_data
.offset
= VMDK_GTE_ZEROED
;
1449 /* update L2 tables */
1450 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1458 ret
= vmdk_write_extent(extent
,
1459 cluster_offset
, index_in_cluster
* 512,
1460 buf
, n
, sector_num
);
1465 /* update L2 tables */
1466 if (vmdk_L2update(extent
, &m_data
) != VMDK_OK
) {
1475 /* update CID on the first write every time the virtual disk is
1477 if (!s
->cid_updated
) {
1478 ret
= vmdk_write_cid(bs
, time(NULL
));
1482 s
->cid_updated
= true;
1488 static coroutine_fn
int vmdk_co_write(BlockDriverState
*bs
, int64_t sector_num
,
1489 const uint8_t *buf
, int nb_sectors
)
1492 BDRVVmdkState
*s
= bs
->opaque
;
1493 qemu_co_mutex_lock(&s
->lock
);
1494 ret
= vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1495 qemu_co_mutex_unlock(&s
->lock
);
1499 static int vmdk_write_compressed(BlockDriverState
*bs
,
1504 BDRVVmdkState
*s
= bs
->opaque
;
1505 if (s
->num_extents
== 1 && s
->extents
[0].compressed
) {
1506 return vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1512 static int coroutine_fn
vmdk_co_write_zeroes(BlockDriverState
*bs
,
1515 BdrvRequestFlags flags
)
1518 BDRVVmdkState
*s
= bs
->opaque
;
1519 qemu_co_mutex_lock(&s
->lock
);
1520 /* write zeroes could fail if sectors not aligned to cluster, test it with
1521 * dry_run == true before really updating image */
1522 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, true);
1524 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, false);
1526 qemu_co_mutex_unlock(&s
->lock
);
1530 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1531 bool flat
, bool compress
, bool zeroed_grain
,
1535 BlockDriverState
*bs
= NULL
;
1537 Error
*local_err
= NULL
;
1538 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
1539 uint32_t *gd_buf
= NULL
;
1542 ret
= bdrv_create_file(filename
, NULL
, &local_err
);
1544 error_propagate(errp
, local_err
);
1549 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1552 error_propagate(errp
, local_err
);
1557 ret
= bdrv_truncate(bs
, filesize
);
1559 error_setg_errno(errp
, -ret
, "Could not truncate file");
1563 magic
= cpu_to_be32(VMDK4_MAGIC
);
1564 memset(&header
, 0, sizeof(header
));
1565 header
.version
= zeroed_grain
? 2 : 1;
1566 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1567 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1568 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1569 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1570 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
1571 header
.granularity
= 128;
1572 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
1574 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
1575 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
1577 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
1578 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
1580 header
.desc_offset
= 1;
1581 header
.desc_size
= 20;
1582 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1583 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
1584 header
.grain_offset
=
1585 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
1586 header
.granularity
);
1587 /* swap endianness for all header fields */
1588 header
.version
= cpu_to_le32(header
.version
);
1589 header
.flags
= cpu_to_le32(header
.flags
);
1590 header
.capacity
= cpu_to_le64(header
.capacity
);
1591 header
.granularity
= cpu_to_le64(header
.granularity
);
1592 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1593 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1594 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1595 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1596 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1597 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1598 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1600 header
.check_bytes
[0] = 0xa;
1601 header
.check_bytes
[1] = 0x20;
1602 header
.check_bytes
[2] = 0xd;
1603 header
.check_bytes
[3] = 0xa;
1605 /* write all the data */
1606 ret
= bdrv_pwrite(bs
, 0, &magic
, sizeof(magic
));
1608 error_set(errp
, QERR_IO_ERROR
);
1611 ret
= bdrv_pwrite(bs
, sizeof(magic
), &header
, sizeof(header
));
1613 error_set(errp
, QERR_IO_ERROR
);
1617 ret
= bdrv_truncate(bs
, le64_to_cpu(header
.grain_offset
) << 9);
1619 error_setg_errno(errp
, -ret
, "Could not truncate file");
1623 /* write grain directory */
1624 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
1625 gd_buf
= g_malloc0(gd_buf_size
);
1626 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
1627 i
< gt_count
; i
++, tmp
+= gt_size
) {
1628 gd_buf
[i
] = cpu_to_le32(tmp
);
1630 ret
= bdrv_pwrite(bs
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
1631 gd_buf
, gd_buf_size
);
1633 error_set(errp
, QERR_IO_ERROR
);
1637 /* write backup grain directory */
1638 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
1639 i
< gt_count
; i
++, tmp
+= gt_size
) {
1640 gd_buf
[i
] = cpu_to_le32(tmp
);
1642 ret
= bdrv_pwrite(bs
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
1643 gd_buf
, gd_buf_size
);
1645 error_set(errp
, QERR_IO_ERROR
);
1658 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1659 char *postfix
, size_t buf_len
, Error
**errp
)
1663 if (filename
== NULL
|| !strlen(filename
)) {
1664 error_setg(errp
, "No filename provided");
1667 p
= strrchr(filename
, '/');
1669 p
= strrchr(filename
, '\\');
1672 p
= strrchr(filename
, ':');
1676 if (p
- filename
>= buf_len
) {
1679 pstrcpy(path
, p
- filename
+ 1, filename
);
1684 q
= strrchr(p
, '.');
1686 pstrcpy(prefix
, buf_len
, p
);
1689 if (q
- p
>= buf_len
) {
1692 pstrcpy(prefix
, q
- p
+ 1, p
);
1693 pstrcpy(postfix
, buf_len
, q
);
1698 static int vmdk_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1701 BlockDriverState
*new_bs
= NULL
;
1702 Error
*local_err
= NULL
;
1704 int64_t total_size
= 0, filesize
;
1705 char *adapter_type
= NULL
;
1706 char *backing_file
= NULL
;
1710 bool flat
, split
, compress
;
1711 GString
*ext_desc_lines
;
1712 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1713 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1714 const char *desc_extent_line
;
1715 char parent_desc_line
[BUF_SIZE
] = "";
1716 uint32_t parent_cid
= 0xffffffff;
1717 uint32_t number_heads
= 16;
1718 bool zeroed_grain
= false;
1719 uint32_t desc_offset
= 0, desc_len
;
1720 const char desc_template
[] =
1721 "# Disk DescriptorFile\n"
1724 "parentCID=%" PRIx32
"\n"
1725 "createType=\"%s\"\n"
1728 "# Extent description\n"
1731 "# The Disk Data Base\n"
1734 "ddb.virtualHWVersion = \"%d\"\n"
1735 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1736 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
1737 "ddb.geometry.sectors = \"63\"\n"
1738 "ddb.adapterType = \"%s\"\n";
1740 ext_desc_lines
= g_string_new(NULL
);
1742 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
1746 /* Read out options */
1747 total_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
1748 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
1749 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1750 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false)) {
1751 flags
|= BLOCK_FLAG_COMPAT6
;
1753 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
1754 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false)) {
1755 zeroed_grain
= true;
1758 if (!adapter_type
) {
1759 adapter_type
= g_strdup("ide");
1760 } else if (strcmp(adapter_type
, "ide") &&
1761 strcmp(adapter_type
, "buslogic") &&
1762 strcmp(adapter_type
, "lsilogic") &&
1763 strcmp(adapter_type
, "legacyESX")) {
1764 error_setg(errp
, "Unknown adapter type: '%s'", adapter_type
);
1768 if (strcmp(adapter_type
, "ide") != 0) {
1769 /* that's the number of heads with which vmware operates when
1770 creating, exporting, etc. vmdk files with a non-ide adapter type */
1774 /* Default format to monolithicSparse */
1775 fmt
= g_strdup("monolithicSparse");
1776 } else if (strcmp(fmt
, "monolithicFlat") &&
1777 strcmp(fmt
, "monolithicSparse") &&
1778 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1779 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1780 strcmp(fmt
, "streamOptimized")) {
1781 error_setg(errp
, "Unknown subformat: '%s'", fmt
);
1785 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1786 strcmp(fmt
, "twoGbMaxExtentSparse"));
1787 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1788 strcmp(fmt
, "twoGbMaxExtentFlat"));
1789 compress
= !strcmp(fmt
, "streamOptimized");
1791 desc_extent_line
= "RW %" PRId64
" FLAT \"%s\" 0\n";
1793 desc_extent_line
= "RW %" PRId64
" SPARSE \"%s\"\n";
1795 if (flat
&& backing_file
) {
1796 error_setg(errp
, "Flat image can't have backing file");
1800 if (flat
&& zeroed_grain
) {
1801 error_setg(errp
, "Flat image can't enable zeroed grain");
1806 BlockDriverState
*bs
= NULL
;
1807 ret
= bdrv_open(&bs
, backing_file
, NULL
, NULL
, BDRV_O_NO_BACKING
, NULL
,
1812 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1817 parent_cid
= vmdk_read_cid(bs
, 0);
1819 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1820 "parentFileNameHint=\"%s\"", backing_file
);
1823 /* Create extents */
1824 filesize
= total_size
;
1825 while (filesize
> 0) {
1826 char desc_line
[BUF_SIZE
];
1827 char ext_filename
[PATH_MAX
];
1828 char desc_filename
[PATH_MAX
];
1829 int64_t size
= filesize
;
1831 if (split
&& size
> split_size
) {
1835 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1836 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1838 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1841 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1844 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1845 path
, desc_filename
);
1847 if (vmdk_create_extent(ext_filename
, size
,
1848 flat
, compress
, zeroed_grain
, errp
)) {
1854 /* Format description line */
1855 snprintf(desc_line
, sizeof(desc_line
),
1856 desc_extent_line
, size
/ BDRV_SECTOR_SIZE
, desc_filename
);
1857 g_string_append(ext_desc_lines
, desc_line
);
1859 /* generate descriptor file */
1860 desc
= g_strdup_printf(desc_template
,
1861 (uint32_t)time(NULL
),
1865 ext_desc_lines
->str
,
1866 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1868 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
1871 desc_len
= strlen(desc
);
1872 /* the descriptor offset = 0x200 */
1873 if (!split
&& !flat
) {
1874 desc_offset
= 0x200;
1876 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1878 error_propagate(errp
, local_err
);
1882 assert(new_bs
== NULL
);
1883 ret
= bdrv_open(&new_bs
, filename
, NULL
, NULL
,
1884 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
1886 error_propagate(errp
, local_err
);
1889 ret
= bdrv_pwrite(new_bs
, desc_offset
, desc
, desc_len
);
1891 error_setg_errno(errp
, -ret
, "Could not write description");
1894 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
1895 * for description file */
1896 if (desc_offset
== 0) {
1897 ret
= bdrv_truncate(new_bs
, desc_len
);
1899 error_setg_errno(errp
, -ret
, "Could not truncate file");
1906 g_free(adapter_type
);
1907 g_free(backing_file
);
1910 g_string_free(ext_desc_lines
, true);
1914 static void vmdk_close(BlockDriverState
*bs
)
1916 BDRVVmdkState
*s
= bs
->opaque
;
1918 vmdk_free_extents(bs
);
1919 g_free(s
->create_type
);
1921 migrate_del_blocker(s
->migration_blocker
);
1922 error_free(s
->migration_blocker
);
1925 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
1927 BDRVVmdkState
*s
= bs
->opaque
;
1931 for (i
= 0; i
< s
->num_extents
; i
++) {
1932 err
= bdrv_co_flush(s
->extents
[i
].file
);
1940 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
1945 BDRVVmdkState
*s
= bs
->opaque
;
1947 ret
= bdrv_get_allocated_file_size(bs
->file
);
1951 for (i
= 0; i
< s
->num_extents
; i
++) {
1952 if (s
->extents
[i
].file
== bs
->file
) {
1955 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
1964 static int vmdk_has_zero_init(BlockDriverState
*bs
)
1967 BDRVVmdkState
*s
= bs
->opaque
;
1969 /* If has a flat extent and its underlying storage doesn't have zero init,
1971 for (i
= 0; i
< s
->num_extents
; i
++) {
1972 if (s
->extents
[i
].flat
) {
1973 if (!bdrv_has_zero_init(s
->extents
[i
].file
)) {
1981 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
1983 ImageInfo
*info
= g_new0(ImageInfo
, 1);
1985 *info
= (ImageInfo
){
1986 .filename
= g_strdup(extent
->file
->filename
),
1987 .format
= g_strdup(extent
->type
),
1988 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
1989 .compressed
= extent
->compressed
,
1990 .has_compressed
= extent
->compressed
,
1991 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
1992 .has_cluster_size
= !extent
->flat
,
1998 static int vmdk_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
2001 BDRVVmdkState
*s
= bs
->opaque
;
2002 VmdkExtent
*extent
= NULL
;
2003 int64_t sector_num
= 0;
2004 int64_t total_sectors
= bdrv_getlength(bs
) / BDRV_SECTOR_SIZE
;
2006 uint64_t cluster_offset
;
2013 if (sector_num
>= total_sectors
) {
2016 extent
= find_extent(s
, sector_num
, extent
);
2019 "ERROR: could not find extent for sector %" PRId64
"\n",
2023 ret
= get_cluster_offset(bs
, extent
, NULL
,
2024 sector_num
<< BDRV_SECTOR_BITS
,
2025 0, &cluster_offset
);
2026 if (ret
== VMDK_ERROR
) {
2028 "ERROR: could not get cluster_offset for sector %"
2029 PRId64
"\n", sector_num
);
2032 if (ret
== VMDK_OK
&& cluster_offset
>= bdrv_getlength(extent
->file
)) {
2034 "ERROR: cluster offset for sector %"
2035 PRId64
" points after EOF\n", sector_num
);
2038 sector_num
+= extent
->cluster_sectors
;
2041 result
->corruptions
++;
2045 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
)
2048 BDRVVmdkState
*s
= bs
->opaque
;
2049 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2050 ImageInfoList
**next
;
2052 *spec_info
= (ImageInfoSpecific
){
2053 .kind
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2055 .vmdk
= g_new0(ImageInfoSpecificVmdk
, 1),
2059 *spec_info
->vmdk
= (ImageInfoSpecificVmdk
) {
2060 .create_type
= g_strdup(s
->create_type
),
2062 .parent_cid
= s
->parent_cid
,
2065 next
= &spec_info
->vmdk
->extents
;
2066 for (i
= 0; i
< s
->num_extents
; i
++) {
2067 *next
= g_new0(ImageInfoList
, 1);
2068 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2069 (*next
)->next
= NULL
;
2070 next
= &(*next
)->next
;
2076 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2079 BDRVVmdkState
*s
= bs
->opaque
;
2080 assert(s
->num_extents
);
2081 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2082 if (!s
->extents
[0].flat
) {
2083 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2085 /* See if we have multiple extents but they have different cases */
2086 for (i
= 1; i
< s
->num_extents
; i
++) {
2087 if (bdi
->needs_compressed_writes
!= s
->extents
[i
].compressed
||
2088 (bdi
->cluster_size
&& bdi
->cluster_size
!=
2089 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
)) {
2096 static void vmdk_detach_aio_context(BlockDriverState
*bs
)
2098 BDRVVmdkState
*s
= bs
->opaque
;
2101 for (i
= 0; i
< s
->num_extents
; i
++) {
2102 bdrv_detach_aio_context(s
->extents
[i
].file
);
2106 static void vmdk_attach_aio_context(BlockDriverState
*bs
,
2107 AioContext
*new_context
)
2109 BDRVVmdkState
*s
= bs
->opaque
;
2112 for (i
= 0; i
< s
->num_extents
; i
++) {
2113 bdrv_attach_aio_context(s
->extents
[i
].file
, new_context
);
2117 static QemuOptsList vmdk_create_opts
= {
2118 .name
= "vmdk-create-opts",
2119 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
2122 .name
= BLOCK_OPT_SIZE
,
2123 .type
= QEMU_OPT_SIZE
,
2124 .help
= "Virtual disk size"
2127 .name
= BLOCK_OPT_ADAPTER_TYPE
,
2128 .type
= QEMU_OPT_STRING
,
2129 .help
= "Virtual adapter type, can be one of "
2130 "ide (default), lsilogic, buslogic or legacyESX"
2133 .name
= BLOCK_OPT_BACKING_FILE
,
2134 .type
= QEMU_OPT_STRING
,
2135 .help
= "File name of a base image"
2138 .name
= BLOCK_OPT_COMPAT6
,
2139 .type
= QEMU_OPT_BOOL
,
2140 .help
= "VMDK version 6 image",
2141 .def_value_str
= "off"
2144 .name
= BLOCK_OPT_SUBFMT
,
2145 .type
= QEMU_OPT_STRING
,
2147 "VMDK flat extent format, can be one of "
2148 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2151 .name
= BLOCK_OPT_ZEROED_GRAIN
,
2152 .type
= QEMU_OPT_BOOL
,
2153 .help
= "Enable efficient zero writes "
2154 "using the zeroed-grain GTE feature"
2156 { /* end of list */ }
2160 static BlockDriver bdrv_vmdk
= {
2161 .format_name
= "vmdk",
2162 .instance_size
= sizeof(BDRVVmdkState
),
2163 .bdrv_probe
= vmdk_probe
,
2164 .bdrv_open
= vmdk_open
,
2165 .bdrv_check
= vmdk_check
,
2166 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
2167 .bdrv_read
= vmdk_co_read
,
2168 .bdrv_write
= vmdk_co_write
,
2169 .bdrv_write_compressed
= vmdk_write_compressed
,
2170 .bdrv_co_write_zeroes
= vmdk_co_write_zeroes
,
2171 .bdrv_close
= vmdk_close
,
2172 .bdrv_create
= vmdk_create
,
2173 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
2174 .bdrv_co_get_block_status
= vmdk_co_get_block_status
,
2175 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
2176 .bdrv_has_zero_init
= vmdk_has_zero_init
,
2177 .bdrv_get_specific_info
= vmdk_get_specific_info
,
2178 .bdrv_refresh_limits
= vmdk_refresh_limits
,
2179 .bdrv_get_info
= vmdk_get_info
,
2180 .bdrv_detach_aio_context
= vmdk_detach_aio_context
,
2181 .bdrv_attach_aio_context
= vmdk_attach_aio_context
,
2183 .supports_backing
= true,
2184 .create_opts
= &vmdk_create_opts
,
2187 static void bdrv_vmdk_init(void)
2189 bdrv_register(&bdrv_vmdk
);
2192 block_init(bdrv_vmdk_init
);