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/osdep.h"
27 #include "qapi/error.h"
28 #include "block/block_int.h"
29 #include "sysemu/block-backend.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "qemu/bswap.h"
34 #include "migration/migration.h"
35 #include "qemu/cutils.h"
38 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
39 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
40 #define VMDK4_COMPRESSION_DEFLATE 1
41 #define VMDK4_FLAG_NL_DETECT (1 << 0)
42 #define VMDK4_FLAG_RGD (1 << 1)
43 /* Zeroed-grain enable bit */
44 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
45 #define VMDK4_FLAG_COMPRESS (1 << 16)
46 #define VMDK4_FLAG_MARKER (1 << 17)
47 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
49 #define VMDK_GTE_ZEROED 0x1
51 /* VMDK internal error codes */
53 #define VMDK_ERROR (-1)
54 /* Cluster not allocated */
55 #define VMDK_UNALLOC (-2)
56 #define VMDK_ZEROED (-3)
58 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
63 uint32_t disk_sectors
;
65 uint32_t l1dir_offset
;
67 uint32_t file_sectors
;
70 uint32_t sectors_per_track
;
71 } QEMU_PACKED VMDK3Header
;
80 /* Number of GrainTableEntries per GrainTable */
81 uint32_t num_gtes_per_gt
;
84 uint64_t grain_offset
;
87 uint16_t compressAlgorithm
;
88 } QEMU_PACKED VMDK4Header
;
90 #define L2_CACHE_SIZE 16
92 typedef struct VmdkExtent
{
101 int64_t flat_start_offset
;
102 int64_t l1_table_offset
;
103 int64_t l1_backup_table_offset
;
105 uint32_t *l1_backup_table
;
106 unsigned int l1_size
;
107 uint32_t l1_entry_sectors
;
109 unsigned int l2_size
;
111 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
112 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
114 int64_t cluster_sectors
;
115 int64_t next_cluster_sector
;
119 typedef struct BDRVVmdkState
{
121 uint64_t desc_offset
;
127 /* Extent array with num_extents entries, ascend ordered by address */
129 Error
*migration_blocker
;
133 typedef struct VmdkMetaData
{
134 unsigned int l1_index
;
135 unsigned int l2_index
;
136 unsigned int l2_offset
;
138 uint32_t *l2_cache_entry
;
141 typedef struct VmdkGrainMarker
{
145 } QEMU_PACKED VmdkGrainMarker
;
148 MARKER_END_OF_STREAM
= 0,
149 MARKER_GRAIN_TABLE
= 1,
150 MARKER_GRAIN_DIRECTORY
= 2,
154 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
161 magic
= be32_to_cpu(*(uint32_t *)buf
);
162 if (magic
== VMDK3_MAGIC
||
163 magic
== VMDK4_MAGIC
) {
166 const char *p
= (const char *)buf
;
167 const char *end
= p
+ buf_size
;
170 /* skip comment line */
171 while (p
< end
&& *p
!= '\n') {
178 while (p
< end
&& *p
== ' ') {
181 /* skip '\r' if windows line endings used. */
182 if (p
< end
&& *p
== '\r') {
185 /* only accept blank lines before 'version=' line */
186 if (p
== end
|| *p
!= '\n') {
192 if (end
- p
>= strlen("version=X\n")) {
193 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
194 strncmp("version=2\n", p
, strlen("version=2\n")) == 0) {
198 if (end
- p
>= strlen("version=X\r\n")) {
199 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
200 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
210 #define SECTOR_SIZE 512
211 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
212 #define BUF_SIZE 4096
213 #define HEADER_SIZE 512 /* first sector of 512 bytes */
215 static void vmdk_free_extents(BlockDriverState
*bs
)
218 BDRVVmdkState
*s
= bs
->opaque
;
221 for (i
= 0; i
< s
->num_extents
; i
++) {
225 g_free(e
->l1_backup_table
);
227 if (e
->file
!= bs
->file
) {
228 bdrv_unref_child(bs
, e
->file
);
234 static void vmdk_free_last_extent(BlockDriverState
*bs
)
236 BDRVVmdkState
*s
= bs
->opaque
;
238 if (s
->num_extents
== 0) {
242 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
245 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
248 uint32_t cid
= 0xffffffff;
249 const char *p_name
, *cid_str
;
251 BDRVVmdkState
*s
= bs
->opaque
;
254 desc
= g_malloc0(DESC_SIZE
);
255 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
262 cid_str
= "parentCID";
263 cid_str_size
= sizeof("parentCID");
266 cid_str_size
= sizeof("CID");
269 desc
[DESC_SIZE
- 1] = '\0';
270 p_name
= strstr(desc
, cid_str
);
271 if (p_name
!= NULL
) {
272 p_name
+= cid_str_size
;
273 sscanf(p_name
, "%" SCNx32
, &cid
);
280 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
282 char *desc
, *tmp_desc
;
283 char *p_name
, *tmp_str
;
284 BDRVVmdkState
*s
= bs
->opaque
;
287 desc
= g_malloc0(DESC_SIZE
);
288 tmp_desc
= g_malloc0(DESC_SIZE
);
289 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
294 desc
[DESC_SIZE
- 1] = '\0';
295 tmp_str
= strstr(desc
, "parentCID");
296 if (tmp_str
== NULL
) {
301 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
302 p_name
= strstr(desc
, "CID");
303 if (p_name
!= NULL
) {
304 p_name
+= sizeof("CID");
305 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
306 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
309 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
317 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
319 BDRVVmdkState
*s
= bs
->opaque
;
322 if (!s
->cid_checked
&& bs
->backing
) {
323 BlockDriverState
*p_bs
= bs
->backing
->bs
;
325 cur_pcid
= vmdk_read_cid(p_bs
, 0);
326 if (s
->parent_cid
!= cur_pcid
) {
331 s
->cid_checked
= true;
336 /* We have nothing to do for VMDK reopen, stubs just return success */
337 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
338 BlockReopenQueue
*queue
, Error
**errp
)
340 assert(state
!= NULL
);
341 assert(state
->bs
!= NULL
);
345 static int vmdk_parent_open(BlockDriverState
*bs
)
349 BDRVVmdkState
*s
= bs
->opaque
;
352 desc
= g_malloc0(DESC_SIZE
+ 1);
353 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
359 p_name
= strstr(desc
, "parentFileNameHint");
360 if (p_name
!= NULL
) {
363 p_name
+= sizeof("parentFileNameHint") + 1;
364 end_name
= strchr(p_name
, '\"');
365 if (end_name
== NULL
) {
369 if ((end_name
- p_name
) > sizeof(bs
->backing_file
) - 1) {
374 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
382 /* Create and append extent to the extent array. Return the added VmdkExtent
383 * address. return NULL if allocation failed. */
384 static int vmdk_add_extent(BlockDriverState
*bs
,
385 BdrvChild
*file
, bool flat
, int64_t sectors
,
386 int64_t l1_offset
, int64_t l1_backup_offset
,
388 int l2_size
, uint64_t cluster_sectors
,
389 VmdkExtent
**new_extent
,
393 BDRVVmdkState
*s
= bs
->opaque
;
396 if (cluster_sectors
> 0x200000) {
397 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
398 error_setg(errp
, "Invalid granularity, image may be corrupt");
401 if (l1_size
> 512 * 1024 * 1024) {
402 /* Although with big capacity and small l1_entry_sectors, we can get a
403 * big l1_size, we don't want unbounded value to allocate the table.
404 * Limit it to 512M, which is 16PB for default cluster and L2 table
406 error_setg(errp
, "L1 size too big");
410 nb_sectors
= bdrv_nb_sectors(file
->bs
);
411 if (nb_sectors
< 0) {
415 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
416 extent
= &s
->extents
[s
->num_extents
];
419 memset(extent
, 0, sizeof(VmdkExtent
));
422 extent
->sectors
= sectors
;
423 extent
->l1_table_offset
= l1_offset
;
424 extent
->l1_backup_table_offset
= l1_backup_offset
;
425 extent
->l1_size
= l1_size
;
426 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
427 extent
->l2_size
= l2_size
;
428 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
429 extent
->next_cluster_sector
= ROUND_UP(nb_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
,
450 /* read the L1 table */
451 l1_size
= extent
->l1_size
* sizeof(uint32_t);
452 extent
->l1_table
= g_try_malloc(l1_size
);
453 if (l1_size
&& extent
->l1_table
== NULL
) {
457 ret
= bdrv_pread(extent
->file
,
458 extent
->l1_table_offset
,
462 error_setg_errno(errp
, -ret
,
463 "Could not read l1 table from extent '%s'",
464 extent
->file
->bs
->filename
);
467 for (i
= 0; i
< extent
->l1_size
; i
++) {
468 le32_to_cpus(&extent
->l1_table
[i
]);
471 if (extent
->l1_backup_table_offset
) {
472 extent
->l1_backup_table
= g_try_malloc(l1_size
);
473 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
477 ret
= bdrv_pread(extent
->file
,
478 extent
->l1_backup_table_offset
,
479 extent
->l1_backup_table
,
482 error_setg_errno(errp
, -ret
,
483 "Could not read l1 backup table from extent '%s'",
484 extent
->file
->bs
->filename
);
487 for (i
= 0; i
< extent
->l1_size
; i
++) {
488 le32_to_cpus(&extent
->l1_backup_table
[i
]);
493 g_new(uint32_t, extent
->l2_size
* L2_CACHE_SIZE
);
496 g_free(extent
->l1_backup_table
);
498 g_free(extent
->l1_table
);
502 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
504 int flags
, Error
**errp
)
511 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
513 error_setg_errno(errp
, -ret
,
514 "Could not read header from file '%s'",
518 ret
= vmdk_add_extent(bs
, file
, false,
519 le32_to_cpu(header
.disk_sectors
),
520 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
522 le32_to_cpu(header
.l1dir_size
),
524 le32_to_cpu(header
.granularity
),
530 ret
= vmdk_init_tables(bs
, extent
, errp
);
532 /* free extent allocated by vmdk_add_extent */
533 vmdk_free_last_extent(bs
);
538 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
539 QDict
*options
, Error
**errp
);
541 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
547 size
= bdrv_getlength(file
->bs
);
549 error_setg_errno(errp
, -size
, "Could not access file");
554 /* Both descriptor file and sparse image must be much larger than 4
555 * bytes, also callers of vmdk_read_desc want to compare the first 4
556 * bytes with VMDK4_MAGIC, let's error out if less is read. */
557 error_setg(errp
, "File is too small, not a valid image");
561 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
562 buf
= g_malloc(size
+ 1);
564 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
566 error_setg_errno(errp
, -ret
, "Could not read from file");
575 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
577 int flags
, QDict
*options
, Error
**errp
)
581 uint32_t l1_size
, l1_entry_sectors
;
584 BDRVVmdkState
*s
= bs
->opaque
;
585 int64_t l1_backup_offset
= 0;
588 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
590 error_setg_errno(errp
, -ret
,
591 "Could not read header from file '%s'",
595 if (header
.capacity
== 0) {
596 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
598 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
602 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
608 if (!s
->create_type
) {
609 s
->create_type
= g_strdup("monolithicSparse");
612 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
614 * The footer takes precedence over the header, so read it in. The
615 * footer starts at offset -1024 from the end: One sector for the
616 * footer, and another one for the end-of-stream marker.
623 uint8_t pad
[512 - 16];
624 } QEMU_PACKED footer_marker
;
628 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
634 uint8_t pad
[512 - 16];
635 } QEMU_PACKED eos_marker
;
636 } QEMU_PACKED footer
;
638 ret
= bdrv_pread(file
,
639 bs
->file
->bs
->total_sectors
* 512 - 1536,
640 &footer
, sizeof(footer
));
642 error_setg_errno(errp
, -ret
, "Failed to read footer");
646 /* Some sanity checks for the footer */
647 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
648 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
649 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
650 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
651 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
652 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
654 error_setg(errp
, "Invalid footer");
658 header
= footer
.header
;
662 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
663 if (le32_to_cpu(header
.version
) > 3) {
664 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
665 le32_to_cpu(header
.version
));
667 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
669 /* VMware KB 2064959 explains that version 3 added support for
670 * persistent changed block tracking (CBT), and backup software can
671 * read it as version=1 if it doesn't care about the changed area
672 * information. So we are safe to enable read only. */
673 error_setg(errp
, "VMDK version 3 must be read only");
677 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
678 error_setg(errp
, "L2 table size too big");
682 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
683 * le64_to_cpu(header
.granularity
);
684 if (l1_entry_sectors
== 0) {
685 error_setg(errp
, "L1 entry size is invalid");
688 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
690 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
691 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
693 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
694 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
695 (int64_t)(le64_to_cpu(header
.grain_offset
)
696 * BDRV_SECTOR_SIZE
));
700 ret
= vmdk_add_extent(bs
, file
, false,
701 le64_to_cpu(header
.capacity
),
702 le64_to_cpu(header
.gd_offset
) << 9,
705 le32_to_cpu(header
.num_gtes_per_gt
),
706 le64_to_cpu(header
.granularity
),
713 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
714 if (extent
->compressed
) {
715 g_free(s
->create_type
);
716 s
->create_type
= g_strdup("streamOptimized");
718 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
719 extent
->version
= le32_to_cpu(header
.version
);
720 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
721 ret
= vmdk_init_tables(bs
, extent
, errp
);
723 /* free extent allocated by vmdk_add_extent */
724 vmdk_free_last_extent(bs
);
729 /* find an option value out of descriptor file */
730 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
731 char *buf
, int buf_size
)
733 char *opt_pos
, *opt_end
;
734 const char *end
= desc
+ strlen(desc
);
736 opt_pos
= strstr(desc
, opt_name
);
740 /* Skip "=\"" following opt_name */
741 opt_pos
+= strlen(opt_name
) + 2;
742 if (opt_pos
>= end
) {
746 while (opt_end
< end
&& *opt_end
!= '"') {
749 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
752 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
756 /* Open an extent file and append to bs array */
757 static int vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
758 char *buf
, QDict
*options
, Error
**errp
)
762 magic
= ldl_be_p(buf
);
765 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
768 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
771 error_setg(errp
, "Image not in VMDK format");
777 static const char *next_line(const char *s
)
788 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
789 const char *desc_file_path
, QDict
*options
,
801 BdrvChild
*extent_file
;
802 BDRVVmdkState
*s
= bs
->opaque
;
804 char extent_opt_prefix
[32];
805 Error
*local_err
= NULL
;
807 for (p
= desc
; *p
; p
= next_line(p
)) {
808 /* parse extent line in one of below formats:
810 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
811 * RW [size in sectors] SPARSE "file-name.vmdk"
812 * RW [size in sectors] VMFS "file-name.vmdk"
813 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
816 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
817 access
, §ors
, type
, fname
, &flat_offset
);
818 if (matches
< 4 || strcmp(access
, "RW")) {
820 } else if (!strcmp(type
, "FLAT")) {
821 if (matches
!= 5 || flat_offset
< 0) {
824 } else if (!strcmp(type
, "VMFS")) {
830 } else if (matches
!= 4) {
835 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
836 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
837 (strcmp(access
, "RW"))) {
841 if (!path_is_absolute(fname
) && !path_has_protocol(fname
) &&
844 error_setg(errp
, "Cannot use relative extent paths with VMDK "
845 "descriptor file '%s'", bs
->file
->bs
->filename
);
849 extent_path
= g_malloc0(PATH_MAX
);
850 path_combine(extent_path
, PATH_MAX
, desc_file_path
, fname
);
852 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
855 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
856 bs
, &child_file
, false, &local_err
);
859 error_propagate(errp
, local_err
);
863 /* save to extents array */
864 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
867 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
868 0, 0, 0, 0, 0, &extent
, errp
);
870 bdrv_unref_child(bs
, extent_file
);
873 extent
->flat_start_offset
= flat_offset
<< 9;
874 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
875 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
876 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
880 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
885 bdrv_unref_child(bs
, extent_file
);
888 extent
= &s
->extents
[s
->num_extents
- 1];
890 error_setg(errp
, "Unsupported extent type '%s'", type
);
891 bdrv_unref_child(bs
, extent_file
);
894 extent
->type
= g_strdup(type
);
901 if (np
[-1] == '\n') {
904 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
908 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
909 QDict
*options
, Error
**errp
)
913 BDRVVmdkState
*s
= bs
->opaque
;
915 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
916 error_setg(errp
, "invalid VMDK image descriptor");
920 if (strcmp(ct
, "monolithicFlat") &&
921 strcmp(ct
, "vmfs") &&
922 strcmp(ct
, "vmfsSparse") &&
923 strcmp(ct
, "twoGbMaxExtentSparse") &&
924 strcmp(ct
, "twoGbMaxExtentFlat")) {
925 error_setg(errp
, "Unsupported image type '%s'", ct
);
929 s
->create_type
= g_strdup(ct
);
931 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->bs
->exact_filename
, options
,
937 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
942 BDRVVmdkState
*s
= bs
->opaque
;
945 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
950 magic
= ldl_be_p(buf
);
954 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
956 s
->desc_offset
= 0x200;
959 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
966 /* try to open parent images, if exist */
967 ret
= vmdk_parent_open(bs
);
971 s
->cid
= vmdk_read_cid(bs
, 0);
972 s
->parent_cid
= vmdk_read_cid(bs
, 1);
973 qemu_co_mutex_init(&s
->lock
);
975 /* Disable migration when VMDK images are used */
976 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
977 "does not support live migration",
978 bdrv_get_device_or_node_name(bs
));
979 migrate_add_blocker(s
->migration_blocker
);
985 g_free(s
->create_type
);
986 s
->create_type
= NULL
;
987 vmdk_free_extents(bs
);
992 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
994 BDRVVmdkState
*s
= bs
->opaque
;
997 for (i
= 0; i
< s
->num_extents
; i
++) {
998 if (!s
->extents
[i
].flat
) {
999 bs
->bl
.pwrite_zeroes_alignment
=
1000 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1001 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1009 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1010 * to the cluster at @cluster_sector_num.
1012 * If @skip_start_sector < @skip_end_sector, the relative range
1013 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1014 * it for call to write user data in the request.
1016 static int get_whole_cluster(BlockDriverState
*bs
,
1018 uint64_t cluster_offset
,
1020 uint64_t skip_start_bytes
,
1021 uint64_t skip_end_bytes
)
1024 int64_t cluster_bytes
;
1025 uint8_t *whole_grain
;
1027 /* For COW, align request sector_num to cluster start */
1028 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1029 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1030 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1033 memset(whole_grain
, 0, skip_start_bytes
);
1034 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1037 assert(skip_end_bytes
<= cluster_bytes
);
1038 /* we will be here if it's first write on non-exist grain(cluster).
1039 * try to read from parent image, if exist */
1040 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1045 /* Read backing data before skip range */
1046 if (skip_start_bytes
> 0) {
1048 ret
= bdrv_pread(bs
->backing
, offset
, whole_grain
,
1055 ret
= bdrv_pwrite(extent
->file
, cluster_offset
, whole_grain
,
1062 /* Read backing data after skip range */
1063 if (skip_end_bytes
< cluster_bytes
) {
1065 ret
= bdrv_pread(bs
->backing
, offset
+ skip_end_bytes
,
1066 whole_grain
+ skip_end_bytes
,
1067 cluster_bytes
- skip_end_bytes
);
1073 ret
= bdrv_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1074 whole_grain
+ skip_end_bytes
,
1075 cluster_bytes
- skip_end_bytes
);
1084 qemu_vfree(whole_grain
);
1088 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1091 offset
= cpu_to_le32(offset
);
1092 /* update L2 table */
1093 if (bdrv_pwrite_sync(extent
->file
,
1094 ((int64_t)m_data
->l2_offset
* 512)
1095 + (m_data
->l2_index
* sizeof(offset
)),
1096 &offset
, sizeof(offset
)) < 0) {
1099 /* update backup L2 table */
1100 if (extent
->l1_backup_table_offset
!= 0) {
1101 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1102 if (bdrv_pwrite_sync(extent
->file
,
1103 ((int64_t)m_data
->l2_offset
* 512)
1104 + (m_data
->l2_index
* sizeof(offset
)),
1105 &offset
, sizeof(offset
)) < 0) {
1109 if (m_data
->l2_cache_entry
) {
1110 *m_data
->l2_cache_entry
= offset
;
1117 * get_cluster_offset
1119 * Look up cluster offset in extent file by sector number, and store in
1122 * For flat extents, the start offset as parsed from the description file is
1125 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1126 * offset for a new cluster and update L2 cache. If there is a backing file,
1127 * COW is done before returning; otherwise, zeroes are written to the allocated
1128 * cluster. Both COW and zero writing skips the sector range
1129 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1130 * has new data to write there.
1132 * Returns: VMDK_OK if cluster exists and mapped in the image.
1133 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1134 * VMDK_ERROR if failed.
1136 static int get_cluster_offset(BlockDriverState
*bs
,
1138 VmdkMetaData
*m_data
,
1141 uint64_t *cluster_offset
,
1142 uint64_t skip_start_bytes
,
1143 uint64_t skip_end_bytes
)
1145 unsigned int l1_index
, l2_offset
, l2_index
;
1146 int min_index
, i
, j
;
1147 uint32_t min_count
, *l2_table
;
1148 bool zeroed
= false;
1150 int64_t cluster_sector
;
1156 *cluster_offset
= extent
->flat_start_offset
;
1160 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1161 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1162 if (l1_index
>= extent
->l1_size
) {
1165 l2_offset
= extent
->l1_table
[l1_index
];
1167 return VMDK_UNALLOC
;
1169 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1170 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1171 /* increment the hit count */
1172 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1173 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1174 extent
->l2_cache_counts
[j
] >>= 1;
1177 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
1181 /* not found: load a new entry in the least used one */
1183 min_count
= 0xffffffff;
1184 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1185 if (extent
->l2_cache_counts
[i
] < min_count
) {
1186 min_count
= extent
->l2_cache_counts
[i
];
1190 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1191 if (bdrv_pread(extent
->file
,
1192 (int64_t)l2_offset
* 512,
1194 extent
->l2_size
* sizeof(uint32_t)
1195 ) != extent
->l2_size
* sizeof(uint32_t)) {
1199 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1200 extent
->l2_cache_counts
[min_index
] = 1;
1202 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1203 cluster_sector
= le32_to_cpu(l2_table
[l2_index
]);
1205 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1209 if (!cluster_sector
|| zeroed
) {
1211 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1214 cluster_sector
= extent
->next_cluster_sector
;
1215 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1217 /* First of all we write grain itself, to avoid race condition
1218 * that may to corrupt the image.
1219 * This problem may occur because of insufficient space on host disk
1220 * or inappropriate VM shutdown.
1222 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1223 offset
, skip_start_bytes
, skip_end_bytes
);
1229 m_data
->l1_index
= l1_index
;
1230 m_data
->l2_index
= l2_index
;
1231 m_data
->l2_offset
= l2_offset
;
1232 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1235 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1239 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1240 int64_t sector_num
, VmdkExtent
*start_hint
)
1242 VmdkExtent
*extent
= start_hint
;
1245 extent
= &s
->extents
[0];
1247 while (extent
< &s
->extents
[s
->num_extents
]) {
1248 if (sector_num
< extent
->end_sector
) {
1256 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1259 uint64_t extent_begin_offset
, extent_relative_offset
;
1260 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1262 extent_begin_offset
=
1263 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1264 extent_relative_offset
= offset
- extent_begin_offset
;
1265 return extent_relative_offset
% cluster_size
;
1268 static inline uint64_t vmdk_find_index_in_cluster(VmdkExtent
*extent
,
1272 offset
= vmdk_find_offset_in_cluster(extent
, sector_num
* BDRV_SECTOR_SIZE
);
1273 return offset
/ BDRV_SECTOR_SIZE
;
1276 static int64_t coroutine_fn
vmdk_co_get_block_status(BlockDriverState
*bs
,
1277 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
1279 BDRVVmdkState
*s
= bs
->opaque
;
1280 int64_t index_in_cluster
, n
, ret
;
1284 extent
= find_extent(s
, sector_num
, NULL
);
1288 qemu_co_mutex_lock(&s
->lock
);
1289 ret
= get_cluster_offset(bs
, extent
, NULL
,
1290 sector_num
* 512, false, &offset
,
1292 qemu_co_mutex_unlock(&s
->lock
);
1294 index_in_cluster
= vmdk_find_index_in_cluster(extent
, sector_num
);
1303 ret
= BDRV_BLOCK_ZERO
;
1306 ret
= BDRV_BLOCK_DATA
;
1307 if (!extent
->compressed
) {
1308 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1309 ret
|= (offset
+ (index_in_cluster
<< BDRV_SECTOR_BITS
))
1310 & BDRV_BLOCK_OFFSET_MASK
;
1312 *file
= extent
->file
->bs
;
1316 n
= extent
->cluster_sectors
- index_in_cluster
;
1317 if (n
> nb_sectors
) {
1324 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1325 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1326 uint64_t qiov_offset
, uint64_t n_bytes
,
1330 VmdkGrainMarker
*data
= NULL
;
1332 QEMUIOVector local_qiov
;
1334 int64_t write_offset
;
1335 int64_t write_end_sector
;
1337 if (extent
->compressed
) {
1338 void *compressed_data
;
1340 if (!extent
->has_marker
) {
1344 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1345 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1347 compressed_data
= g_malloc(n_bytes
);
1348 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1349 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1350 g_free(compressed_data
);
1352 if (ret
!= Z_OK
|| buf_len
== 0) {
1357 data
->lba
= offset
>> BDRV_SECTOR_BITS
;
1358 data
->size
= buf_len
;
1360 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1361 iov
= (struct iovec
) {
1365 qemu_iovec_init_external(&local_qiov
, &iov
, 1);
1367 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1368 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1371 write_offset
= cluster_offset
+ offset_in_cluster
,
1372 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1375 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1377 if (extent
->compressed
) {
1378 extent
->next_cluster_sector
= write_end_sector
;
1380 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1390 if (!extent
->compressed
) {
1391 qemu_iovec_destroy(&local_qiov
);
1396 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1397 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1401 int cluster_bytes
, buf_bytes
;
1402 uint8_t *cluster_buf
, *compressed_data
;
1403 uint8_t *uncomp_buf
;
1405 VmdkGrainMarker
*marker
;
1409 if (!extent
->compressed
) {
1410 ret
= bdrv_co_preadv(extent
->file
,
1411 cluster_offset
+ offset_in_cluster
, bytes
,
1418 cluster_bytes
= extent
->cluster_sectors
* 512;
1419 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1420 buf_bytes
= cluster_bytes
* 2;
1421 cluster_buf
= g_malloc(buf_bytes
);
1422 uncomp_buf
= g_malloc(cluster_bytes
);
1423 ret
= bdrv_pread(extent
->file
,
1425 cluster_buf
, buf_bytes
);
1429 compressed_data
= cluster_buf
;
1430 buf_len
= cluster_bytes
;
1431 data_len
= cluster_bytes
;
1432 if (extent
->has_marker
) {
1433 marker
= (VmdkGrainMarker
*)cluster_buf
;
1434 compressed_data
= marker
->data
;
1435 data_len
= le32_to_cpu(marker
->size
);
1437 if (!data_len
|| data_len
> buf_bytes
) {
1441 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1447 if (offset_in_cluster
< 0 ||
1448 offset_in_cluster
+ bytes
> buf_len
) {
1452 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1457 g_free(cluster_buf
);
1461 static int coroutine_fn
1462 vmdk_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1463 QEMUIOVector
*qiov
, int flags
)
1465 BDRVVmdkState
*s
= bs
->opaque
;
1467 uint64_t n_bytes
, offset_in_cluster
;
1468 VmdkExtent
*extent
= NULL
;
1469 QEMUIOVector local_qiov
;
1470 uint64_t cluster_offset
;
1471 uint64_t bytes_done
= 0;
1473 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1474 qemu_co_mutex_lock(&s
->lock
);
1477 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1482 ret
= get_cluster_offset(bs
, extent
, NULL
,
1483 offset
, false, &cluster_offset
, 0, 0);
1484 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1486 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1487 - offset_in_cluster
);
1489 if (ret
!= VMDK_OK
) {
1490 /* if not allocated, try to read from parent image, if exist */
1491 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1492 if (!vmdk_is_cid_valid(bs
)) {
1497 qemu_iovec_reset(&local_qiov
);
1498 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1500 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
1506 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
1509 qemu_iovec_reset(&local_qiov
);
1510 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1512 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
1513 &local_qiov
, n_bytes
);
1520 bytes_done
+= n_bytes
;
1525 qemu_co_mutex_unlock(&s
->lock
);
1526 qemu_iovec_destroy(&local_qiov
);
1533 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1534 * if possible, otherwise return -ENOTSUP.
1535 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1536 * with each cluster. By dry run we can find if the zero write
1537 * is possible without modifying image data.
1539 * Returns: error code with 0 for success.
1541 static int vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1542 uint64_t bytes
, QEMUIOVector
*qiov
,
1543 bool zeroed
, bool zero_dry_run
)
1545 BDRVVmdkState
*s
= bs
->opaque
;
1546 VmdkExtent
*extent
= NULL
;
1548 int64_t offset_in_cluster
, n_bytes
;
1549 uint64_t cluster_offset
;
1550 uint64_t bytes_done
= 0;
1551 VmdkMetaData m_data
;
1553 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
1554 error_report("Wrong offset: offset=0x%" PRIx64
1555 " total_sectors=0x%" PRIx64
,
1556 offset
, bs
->total_sectors
);
1561 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1565 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1566 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1567 - offset_in_cluster
);
1569 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1570 !(extent
->compressed
|| zeroed
),
1571 &cluster_offset
, offset_in_cluster
,
1572 offset_in_cluster
+ n_bytes
);
1573 if (extent
->compressed
) {
1574 if (ret
== VMDK_OK
) {
1575 /* Refuse write to allocated cluster for streamOptimized */
1576 error_report("Could not write to allocated cluster"
1577 " for streamOptimized");
1581 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1582 true, &cluster_offset
, 0, 0);
1585 if (ret
== VMDK_ERROR
) {
1589 /* Do zeroed write, buf is ignored */
1590 if (extent
->has_zero_grain
&&
1591 offset_in_cluster
== 0 &&
1592 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
1593 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1594 if (!zero_dry_run
) {
1595 /* update L2 tables */
1596 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
1605 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
1606 qiov
, bytes_done
, n_bytes
, offset
);
1611 /* update L2 tables */
1612 if (vmdk_L2update(extent
, &m_data
,
1613 cluster_offset
>> BDRV_SECTOR_BITS
)
1621 bytes_done
+= n_bytes
;
1623 /* update CID on the first write every time the virtual disk is
1625 if (!s
->cid_updated
) {
1626 ret
= vmdk_write_cid(bs
, g_random_int());
1630 s
->cid_updated
= true;
1636 static int coroutine_fn
1637 vmdk_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1638 QEMUIOVector
*qiov
, int flags
)
1641 BDRVVmdkState
*s
= bs
->opaque
;
1642 qemu_co_mutex_lock(&s
->lock
);
1643 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
1644 qemu_co_mutex_unlock(&s
->lock
);
1648 static int coroutine_fn
1649 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
1650 uint64_t bytes
, QEMUIOVector
*qiov
)
1652 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
1655 static int coroutine_fn
vmdk_co_pwrite_zeroes(BlockDriverState
*bs
,
1658 BdrvRequestFlags flags
)
1661 BDRVVmdkState
*s
= bs
->opaque
;
1663 qemu_co_mutex_lock(&s
->lock
);
1664 /* write zeroes could fail if sectors not aligned to cluster, test it with
1665 * dry_run == true before really updating image */
1666 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
1668 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
1670 qemu_co_mutex_unlock(&s
->lock
);
1674 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1675 bool flat
, bool compress
, bool zeroed_grain
,
1676 QemuOpts
*opts
, Error
**errp
)
1679 BlockBackend
*blk
= NULL
;
1681 Error
*local_err
= NULL
;
1682 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
1683 uint32_t *gd_buf
= NULL
;
1686 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1688 error_propagate(errp
, local_err
);
1692 blk
= blk_new_open(filename
, NULL
, NULL
,
1693 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
1695 error_propagate(errp
, local_err
);
1700 blk_set_allow_write_beyond_eof(blk
, true);
1703 ret
= blk_truncate(blk
, filesize
);
1705 error_setg_errno(errp
, -ret
, "Could not truncate file");
1709 magic
= cpu_to_be32(VMDK4_MAGIC
);
1710 memset(&header
, 0, sizeof(header
));
1713 } else if (zeroed_grain
) {
1718 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1719 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1720 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1721 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1722 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
1723 header
.granularity
= 128;
1724 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
1726 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
1727 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
1729 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
1730 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
1732 header
.desc_offset
= 1;
1733 header
.desc_size
= 20;
1734 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1735 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
1736 header
.grain_offset
=
1737 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
1738 header
.granularity
);
1739 /* swap endianness for all header fields */
1740 header
.version
= cpu_to_le32(header
.version
);
1741 header
.flags
= cpu_to_le32(header
.flags
);
1742 header
.capacity
= cpu_to_le64(header
.capacity
);
1743 header
.granularity
= cpu_to_le64(header
.granularity
);
1744 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1745 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1746 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1747 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1748 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1749 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1750 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1752 header
.check_bytes
[0] = 0xa;
1753 header
.check_bytes
[1] = 0x20;
1754 header
.check_bytes
[2] = 0xd;
1755 header
.check_bytes
[3] = 0xa;
1757 /* write all the data */
1758 ret
= blk_pwrite(blk
, 0, &magic
, sizeof(magic
), 0);
1760 error_setg(errp
, QERR_IO_ERROR
);
1763 ret
= blk_pwrite(blk
, sizeof(magic
), &header
, sizeof(header
), 0);
1765 error_setg(errp
, QERR_IO_ERROR
);
1769 ret
= blk_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9);
1771 error_setg_errno(errp
, -ret
, "Could not truncate file");
1775 /* write grain directory */
1776 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
1777 gd_buf
= g_malloc0(gd_buf_size
);
1778 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
1779 i
< gt_count
; i
++, tmp
+= gt_size
) {
1780 gd_buf
[i
] = cpu_to_le32(tmp
);
1782 ret
= blk_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
1783 gd_buf
, gd_buf_size
, 0);
1785 error_setg(errp
, QERR_IO_ERROR
);
1789 /* write backup grain directory */
1790 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
1791 i
< gt_count
; i
++, tmp
+= gt_size
) {
1792 gd_buf
[i
] = cpu_to_le32(tmp
);
1794 ret
= blk_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
1795 gd_buf
, gd_buf_size
, 0);
1797 error_setg(errp
, QERR_IO_ERROR
);
1810 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1811 char *postfix
, size_t buf_len
, Error
**errp
)
1815 if (filename
== NULL
|| !strlen(filename
)) {
1816 error_setg(errp
, "No filename provided");
1819 p
= strrchr(filename
, '/');
1821 p
= strrchr(filename
, '\\');
1824 p
= strrchr(filename
, ':');
1828 if (p
- filename
>= buf_len
) {
1831 pstrcpy(path
, p
- filename
+ 1, filename
);
1836 q
= strrchr(p
, '.');
1838 pstrcpy(prefix
, buf_len
, p
);
1841 if (q
- p
>= buf_len
) {
1844 pstrcpy(prefix
, q
- p
+ 1, p
);
1845 pstrcpy(postfix
, buf_len
, q
);
1850 static int vmdk_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1853 BlockBackend
*new_blk
= NULL
;
1854 Error
*local_err
= NULL
;
1856 int64_t total_size
= 0, filesize
;
1857 char *adapter_type
= NULL
;
1858 char *backing_file
= NULL
;
1859 char *hw_version
= NULL
;
1862 bool flat
, split
, compress
;
1863 GString
*ext_desc_lines
;
1864 char *path
= g_malloc0(PATH_MAX
);
1865 char *prefix
= g_malloc0(PATH_MAX
);
1866 char *postfix
= g_malloc0(PATH_MAX
);
1867 char *desc_line
= g_malloc0(BUF_SIZE
);
1868 char *ext_filename
= g_malloc0(PATH_MAX
);
1869 char *desc_filename
= g_malloc0(PATH_MAX
);
1870 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1871 const char *desc_extent_line
;
1872 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
1873 uint32_t parent_cid
= 0xffffffff;
1874 uint32_t number_heads
= 16;
1875 bool zeroed_grain
= false;
1876 uint32_t desc_offset
= 0, desc_len
;
1877 const char desc_template
[] =
1878 "# Disk DescriptorFile\n"
1881 "parentCID=%" PRIx32
"\n"
1882 "createType=\"%s\"\n"
1885 "# Extent description\n"
1888 "# The Disk Data Base\n"
1891 "ddb.virtualHWVersion = \"%s\"\n"
1892 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1893 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
1894 "ddb.geometry.sectors = \"63\"\n"
1895 "ddb.adapterType = \"%s\"\n";
1897 ext_desc_lines
= g_string_new(NULL
);
1899 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
1903 /* Read out options */
1904 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1906 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
1907 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1908 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
1909 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false)) {
1910 if (strcmp(hw_version
, "undefined")) {
1912 "compat6 cannot be enabled with hwversion set");
1917 hw_version
= g_strdup("6");
1919 if (strcmp(hw_version
, "undefined") == 0) {
1921 hw_version
= g_strdup("4");
1923 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
1924 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false)) {
1925 zeroed_grain
= true;
1928 if (!adapter_type
) {
1929 adapter_type
= g_strdup("ide");
1930 } else if (strcmp(adapter_type
, "ide") &&
1931 strcmp(adapter_type
, "buslogic") &&
1932 strcmp(adapter_type
, "lsilogic") &&
1933 strcmp(adapter_type
, "legacyESX")) {
1934 error_setg(errp
, "Unknown adapter type: '%s'", adapter_type
);
1938 if (strcmp(adapter_type
, "ide") != 0) {
1939 /* that's the number of heads with which vmware operates when
1940 creating, exporting, etc. vmdk files with a non-ide adapter type */
1944 /* Default format to monolithicSparse */
1945 fmt
= g_strdup("monolithicSparse");
1946 } else if (strcmp(fmt
, "monolithicFlat") &&
1947 strcmp(fmt
, "monolithicSparse") &&
1948 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1949 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1950 strcmp(fmt
, "streamOptimized")) {
1951 error_setg(errp
, "Unknown subformat: '%s'", fmt
);
1955 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1956 strcmp(fmt
, "twoGbMaxExtentSparse"));
1957 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1958 strcmp(fmt
, "twoGbMaxExtentFlat"));
1959 compress
= !strcmp(fmt
, "streamOptimized");
1961 desc_extent_line
= "RW %" PRId64
" FLAT \"%s\" 0\n";
1963 desc_extent_line
= "RW %" PRId64
" SPARSE \"%s\"\n";
1965 if (flat
&& backing_file
) {
1966 error_setg(errp
, "Flat image can't have backing file");
1970 if (flat
&& zeroed_grain
) {
1971 error_setg(errp
, "Flat image can't enable zeroed grain");
1977 char *full_backing
= g_new0(char, PATH_MAX
);
1978 bdrv_get_full_backing_filename_from_filename(filename
, backing_file
,
1979 full_backing
, PATH_MAX
,
1982 g_free(full_backing
);
1983 error_propagate(errp
, local_err
);
1988 blk
= blk_new_open(full_backing
, NULL
, NULL
,
1989 BDRV_O_NO_BACKING
, errp
);
1990 g_free(full_backing
);
1995 if (strcmp(blk_bs(blk
)->drv
->format_name
, "vmdk")) {
2000 parent_cid
= vmdk_read_cid(blk_bs(blk
), 0);
2002 snprintf(parent_desc_line
, BUF_SIZE
,
2003 "parentFileNameHint=\"%s\"", backing_file
);
2006 /* Create extents */
2007 filesize
= total_size
;
2008 while (filesize
> 0) {
2009 int64_t size
= filesize
;
2011 if (split
&& size
> split_size
) {
2015 snprintf(desc_filename
, PATH_MAX
, "%s-%c%03d%s",
2016 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
2018 snprintf(desc_filename
, PATH_MAX
, "%s-flat%s", prefix
, postfix
);
2020 snprintf(desc_filename
, PATH_MAX
, "%s%s", prefix
, postfix
);
2022 snprintf(ext_filename
, PATH_MAX
, "%s%s", path
, desc_filename
);
2024 if (vmdk_create_extent(ext_filename
, size
,
2025 flat
, compress
, zeroed_grain
, opts
, errp
)) {
2031 /* Format description line */
2032 snprintf(desc_line
, BUF_SIZE
,
2033 desc_extent_line
, size
/ BDRV_SECTOR_SIZE
, desc_filename
);
2034 g_string_append(ext_desc_lines
, desc_line
);
2036 /* generate descriptor file */
2037 desc
= g_strdup_printf(desc_template
,
2042 ext_desc_lines
->str
,
2045 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2048 desc_len
= strlen(desc
);
2049 /* the descriptor offset = 0x200 */
2050 if (!split
&& !flat
) {
2051 desc_offset
= 0x200;
2053 ret
= bdrv_create_file(filename
, opts
, &local_err
);
2055 error_propagate(errp
, local_err
);
2060 new_blk
= blk_new_open(filename
, NULL
, NULL
,
2061 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, &local_err
);
2062 if (new_blk
== NULL
) {
2063 error_propagate(errp
, local_err
);
2068 blk_set_allow_write_beyond_eof(new_blk
, true);
2070 ret
= blk_pwrite(new_blk
, desc_offset
, desc
, desc_len
, 0);
2072 error_setg_errno(errp
, -ret
, "Could not write description");
2075 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2076 * for description file */
2077 if (desc_offset
== 0) {
2078 ret
= blk_truncate(new_blk
, desc_len
);
2080 error_setg_errno(errp
, -ret
, "Could not truncate file");
2087 g_free(adapter_type
);
2088 g_free(backing_file
);
2096 g_free(ext_filename
);
2097 g_free(desc_filename
);
2098 g_free(parent_desc_line
);
2099 g_string_free(ext_desc_lines
, true);
2103 static void vmdk_close(BlockDriverState
*bs
)
2105 BDRVVmdkState
*s
= bs
->opaque
;
2107 vmdk_free_extents(bs
);
2108 g_free(s
->create_type
);
2110 migrate_del_blocker(s
->migration_blocker
);
2111 error_free(s
->migration_blocker
);
2114 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
2116 BDRVVmdkState
*s
= bs
->opaque
;
2120 for (i
= 0; i
< s
->num_extents
; i
++) {
2121 err
= bdrv_co_flush(s
->extents
[i
].file
->bs
);
2129 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2134 BDRVVmdkState
*s
= bs
->opaque
;
2136 ret
= bdrv_get_allocated_file_size(bs
->file
->bs
);
2140 for (i
= 0; i
< s
->num_extents
; i
++) {
2141 if (s
->extents
[i
].file
== bs
->file
) {
2144 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2153 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2156 BDRVVmdkState
*s
= bs
->opaque
;
2158 /* If has a flat extent and its underlying storage doesn't have zero init,
2160 for (i
= 0; i
< s
->num_extents
; i
++) {
2161 if (s
->extents
[i
].flat
) {
2162 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2170 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2172 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2174 *info
= (ImageInfo
){
2175 .filename
= g_strdup(extent
->file
->bs
->filename
),
2176 .format
= g_strdup(extent
->type
),
2177 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2178 .compressed
= extent
->compressed
,
2179 .has_compressed
= extent
->compressed
,
2180 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2181 .has_cluster_size
= !extent
->flat
,
2187 static int vmdk_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
2190 BDRVVmdkState
*s
= bs
->opaque
;
2191 VmdkExtent
*extent
= NULL
;
2192 int64_t sector_num
= 0;
2193 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2195 uint64_t cluster_offset
;
2202 if (sector_num
>= total_sectors
) {
2205 extent
= find_extent(s
, sector_num
, extent
);
2208 "ERROR: could not find extent for sector %" PRId64
"\n",
2212 ret
= get_cluster_offset(bs
, extent
, NULL
,
2213 sector_num
<< BDRV_SECTOR_BITS
,
2214 false, &cluster_offset
, 0, 0);
2215 if (ret
== VMDK_ERROR
) {
2217 "ERROR: could not get cluster_offset for sector %"
2218 PRId64
"\n", sector_num
);
2221 if (ret
== VMDK_OK
&&
2222 cluster_offset
>= bdrv_getlength(extent
->file
->bs
))
2225 "ERROR: cluster offset for sector %"
2226 PRId64
" points after EOF\n", sector_num
);
2229 sector_num
+= extent
->cluster_sectors
;
2232 result
->corruptions
++;
2236 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
)
2239 BDRVVmdkState
*s
= bs
->opaque
;
2240 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2241 ImageInfoList
**next
;
2243 *spec_info
= (ImageInfoSpecific
){
2244 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2246 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
2250 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
2251 .create_type
= g_strdup(s
->create_type
),
2253 .parent_cid
= s
->parent_cid
,
2256 next
= &spec_info
->u
.vmdk
.data
->extents
;
2257 for (i
= 0; i
< s
->num_extents
; i
++) {
2258 *next
= g_new0(ImageInfoList
, 1);
2259 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2260 (*next
)->next
= NULL
;
2261 next
= &(*next
)->next
;
2267 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
2269 return a
->flat
== b
->flat
&&
2270 a
->compressed
== b
->compressed
&&
2271 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
2274 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2277 BDRVVmdkState
*s
= bs
->opaque
;
2278 assert(s
->num_extents
);
2280 /* See if we have multiple extents but they have different cases */
2281 for (i
= 1; i
< s
->num_extents
; i
++) {
2282 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
2286 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2287 if (!s
->extents
[0].flat
) {
2288 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2293 static QemuOptsList vmdk_create_opts
= {
2294 .name
= "vmdk-create-opts",
2295 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
2298 .name
= BLOCK_OPT_SIZE
,
2299 .type
= QEMU_OPT_SIZE
,
2300 .help
= "Virtual disk size"
2303 .name
= BLOCK_OPT_ADAPTER_TYPE
,
2304 .type
= QEMU_OPT_STRING
,
2305 .help
= "Virtual adapter type, can be one of "
2306 "ide (default), lsilogic, buslogic or legacyESX"
2309 .name
= BLOCK_OPT_BACKING_FILE
,
2310 .type
= QEMU_OPT_STRING
,
2311 .help
= "File name of a base image"
2314 .name
= BLOCK_OPT_COMPAT6
,
2315 .type
= QEMU_OPT_BOOL
,
2316 .help
= "VMDK version 6 image",
2317 .def_value_str
= "off"
2320 .name
= BLOCK_OPT_HWVERSION
,
2321 .type
= QEMU_OPT_STRING
,
2322 .help
= "VMDK hardware version",
2323 .def_value_str
= "undefined"
2326 .name
= BLOCK_OPT_SUBFMT
,
2327 .type
= QEMU_OPT_STRING
,
2329 "VMDK flat extent format, can be one of "
2330 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2333 .name
= BLOCK_OPT_ZEROED_GRAIN
,
2334 .type
= QEMU_OPT_BOOL
,
2335 .help
= "Enable efficient zero writes "
2336 "using the zeroed-grain GTE feature"
2338 { /* end of list */ }
2342 static BlockDriver bdrv_vmdk
= {
2343 .format_name
= "vmdk",
2344 .instance_size
= sizeof(BDRVVmdkState
),
2345 .bdrv_probe
= vmdk_probe
,
2346 .bdrv_open
= vmdk_open
,
2347 .bdrv_check
= vmdk_check
,
2348 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
2349 .bdrv_co_preadv
= vmdk_co_preadv
,
2350 .bdrv_co_pwritev
= vmdk_co_pwritev
,
2351 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
2352 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
2353 .bdrv_close
= vmdk_close
,
2354 .bdrv_create
= vmdk_create
,
2355 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
2356 .bdrv_co_get_block_status
= vmdk_co_get_block_status
,
2357 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
2358 .bdrv_has_zero_init
= vmdk_has_zero_init
,
2359 .bdrv_get_specific_info
= vmdk_get_specific_info
,
2360 .bdrv_refresh_limits
= vmdk_refresh_limits
,
2361 .bdrv_get_info
= vmdk_get_info
,
2363 .supports_backing
= true,
2364 .create_opts
= &vmdk_create_opts
,
2367 static void bdrv_vmdk_init(void)
2369 bdrv_register(&bdrv_vmdk
);
2372 block_init(bdrv_vmdk_init
);