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/qdict.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qemu/error-report.h"
33 #include "qemu/module.h"
34 #include "qemu/option.h"
35 #include "qemu/bswap.h"
36 #include "migration/blocker.h"
37 #include "qemu/cutils.h"
40 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
41 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
42 #define VMDK4_COMPRESSION_DEFLATE 1
43 #define VMDK4_FLAG_NL_DETECT (1 << 0)
44 #define VMDK4_FLAG_RGD (1 << 1)
45 /* Zeroed-grain enable bit */
46 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
47 #define VMDK4_FLAG_COMPRESS (1 << 16)
48 #define VMDK4_FLAG_MARKER (1 << 17)
49 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
51 #define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
53 #define VMDK_GTE_ZEROED 0x1
55 /* VMDK internal error codes */
57 #define VMDK_ERROR (-1)
58 /* Cluster not allocated */
59 #define VMDK_UNALLOC (-2)
60 #define VMDK_ZEROED (-3)
62 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
67 uint32_t disk_sectors
;
69 uint32_t l1dir_offset
;
71 uint32_t file_sectors
;
74 uint32_t sectors_per_track
;
75 } QEMU_PACKED VMDK3Header
;
84 /* Number of GrainTableEntries per GrainTable */
85 uint32_t num_gtes_per_gt
;
88 uint64_t grain_offset
;
91 uint16_t compressAlgorithm
;
92 } QEMU_PACKED VMDK4Header
;
94 #define L2_CACHE_SIZE 16
96 typedef struct VmdkExtent
{
105 int64_t flat_start_offset
;
106 int64_t l1_table_offset
;
107 int64_t l1_backup_table_offset
;
109 uint32_t *l1_backup_table
;
110 unsigned int l1_size
;
111 uint32_t l1_entry_sectors
;
113 unsigned int l2_size
;
115 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
116 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
118 int64_t cluster_sectors
;
119 int64_t next_cluster_sector
;
123 typedef struct BDRVVmdkState
{
125 uint64_t desc_offset
;
131 /* Extent array with num_extents entries, ascend ordered by address */
133 Error
*migration_blocker
;
137 typedef struct VmdkMetaData
{
138 unsigned int l1_index
;
139 unsigned int l2_index
;
140 unsigned int l2_offset
;
142 uint32_t *l2_cache_entry
;
145 typedef struct VmdkGrainMarker
{
149 } QEMU_PACKED VmdkGrainMarker
;
152 MARKER_END_OF_STREAM
= 0,
153 MARKER_GRAIN_TABLE
= 1,
154 MARKER_GRAIN_DIRECTORY
= 2,
158 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
165 magic
= be32_to_cpu(*(uint32_t *)buf
);
166 if (magic
== VMDK3_MAGIC
||
167 magic
== VMDK4_MAGIC
) {
170 const char *p
= (const char *)buf
;
171 const char *end
= p
+ buf_size
;
174 /* skip comment line */
175 while (p
< end
&& *p
!= '\n') {
182 while (p
< end
&& *p
== ' ') {
185 /* skip '\r' if windows line endings used. */
186 if (p
< end
&& *p
== '\r') {
189 /* only accept blank lines before 'version=' line */
190 if (p
== end
|| *p
!= '\n') {
196 if (end
- p
>= strlen("version=X\n")) {
197 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
198 strncmp("version=2\n", p
, strlen("version=2\n")) == 0 ||
199 strncmp("version=3\n", p
, strlen("version=3\n")) == 0) {
203 if (end
- p
>= strlen("version=X\r\n")) {
204 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
205 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0 ||
206 strncmp("version=3\r\n", p
, strlen("version=3\r\n")) == 0) {
216 #define SECTOR_SIZE 512
217 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
218 #define BUF_SIZE 4096
219 #define HEADER_SIZE 512 /* first sector of 512 bytes */
221 static void vmdk_free_extents(BlockDriverState
*bs
)
224 BDRVVmdkState
*s
= bs
->opaque
;
227 for (i
= 0; i
< s
->num_extents
; i
++) {
231 g_free(e
->l1_backup_table
);
233 if (e
->file
!= bs
->file
) {
234 bdrv_unref_child(bs
, e
->file
);
240 static void vmdk_free_last_extent(BlockDriverState
*bs
)
242 BDRVVmdkState
*s
= bs
->opaque
;
244 if (s
->num_extents
== 0) {
248 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
251 /* Return -ve errno, or 0 on success and write CID into *pcid. */
252 static int vmdk_read_cid(BlockDriverState
*bs
, int parent
, uint32_t *pcid
)
256 const char *p_name
, *cid_str
;
258 BDRVVmdkState
*s
= bs
->opaque
;
261 desc
= g_malloc0(DESC_SIZE
);
262 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
268 cid_str
= "parentCID";
269 cid_str_size
= sizeof("parentCID");
272 cid_str_size
= sizeof("CID");
275 desc
[DESC_SIZE
- 1] = '\0';
276 p_name
= strstr(desc
, cid_str
);
277 if (p_name
== NULL
) {
281 p_name
+= cid_str_size
;
282 if (sscanf(p_name
, "%" SCNx32
, &cid
) != 1) {
294 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
296 char *desc
, *tmp_desc
;
297 char *p_name
, *tmp_str
;
298 BDRVVmdkState
*s
= bs
->opaque
;
301 desc
= g_malloc0(DESC_SIZE
);
302 tmp_desc
= g_malloc0(DESC_SIZE
);
303 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
308 desc
[DESC_SIZE
- 1] = '\0';
309 tmp_str
= strstr(desc
, "parentCID");
310 if (tmp_str
== NULL
) {
315 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
316 p_name
= strstr(desc
, "CID");
317 if (p_name
!= NULL
) {
318 p_name
+= sizeof("CID");
319 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
320 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
323 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
331 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
333 BDRVVmdkState
*s
= bs
->opaque
;
336 if (!s
->cid_checked
&& bs
->backing
) {
337 BlockDriverState
*p_bs
= bs
->backing
->bs
;
339 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
340 /* Backing file is not in vmdk format, so it does not have
341 * a CID, which makes the overlay's parent CID invalid */
345 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
346 /* read failure: report as not valid */
349 if (s
->parent_cid
!= cur_pcid
) {
354 s
->cid_checked
= true;
359 /* We have nothing to do for VMDK reopen, stubs just return success */
360 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
361 BlockReopenQueue
*queue
, Error
**errp
)
363 assert(state
!= NULL
);
364 assert(state
->bs
!= NULL
);
368 static int vmdk_parent_open(BlockDriverState
*bs
)
372 BDRVVmdkState
*s
= bs
->opaque
;
375 desc
= g_malloc0(DESC_SIZE
+ 1);
376 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
382 p_name
= strstr(desc
, "parentFileNameHint");
383 if (p_name
!= NULL
) {
386 p_name
+= sizeof("parentFileNameHint") + 1;
387 end_name
= strchr(p_name
, '\"');
388 if (end_name
== NULL
) {
392 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
397 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
398 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
399 bs
->auto_backing_file
);
407 /* Create and append extent to the extent array. Return the added VmdkExtent
408 * address. return NULL if allocation failed. */
409 static int vmdk_add_extent(BlockDriverState
*bs
,
410 BdrvChild
*file
, bool flat
, int64_t sectors
,
411 int64_t l1_offset
, int64_t l1_backup_offset
,
413 int l2_size
, uint64_t cluster_sectors
,
414 VmdkExtent
**new_extent
,
418 BDRVVmdkState
*s
= bs
->opaque
;
421 if (cluster_sectors
> 0x200000) {
422 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
423 error_setg(errp
, "Invalid granularity, image may be corrupt");
426 if (l1_size
> 512 * 1024 * 1024) {
427 /* Although with big capacity and small l1_entry_sectors, we can get a
428 * big l1_size, we don't want unbounded value to allocate the table.
429 * Limit it to 512M, which is 16PB for default cluster and L2 table
431 error_setg(errp
, "L1 size too big");
435 nb_sectors
= bdrv_nb_sectors(file
->bs
);
436 if (nb_sectors
< 0) {
440 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
441 extent
= &s
->extents
[s
->num_extents
];
444 memset(extent
, 0, sizeof(VmdkExtent
));
447 extent
->sectors
= sectors
;
448 extent
->l1_table_offset
= l1_offset
;
449 extent
->l1_backup_table_offset
= l1_backup_offset
;
450 extent
->l1_size
= l1_size
;
451 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
452 extent
->l2_size
= l2_size
;
453 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
454 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
456 if (s
->num_extents
> 1) {
457 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
459 extent
->end_sector
= extent
->sectors
;
461 bs
->total_sectors
= extent
->end_sector
;
463 *new_extent
= extent
;
468 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
475 /* read the L1 table */
476 l1_size
= extent
->l1_size
* sizeof(uint32_t);
477 extent
->l1_table
= g_try_malloc(l1_size
);
478 if (l1_size
&& extent
->l1_table
== NULL
) {
482 ret
= bdrv_pread(extent
->file
,
483 extent
->l1_table_offset
,
487 bdrv_refresh_filename(extent
->file
->bs
);
488 error_setg_errno(errp
, -ret
,
489 "Could not read l1 table from extent '%s'",
490 extent
->file
->bs
->filename
);
493 for (i
= 0; i
< extent
->l1_size
; i
++) {
494 le32_to_cpus(&extent
->l1_table
[i
]);
497 if (extent
->l1_backup_table_offset
) {
498 extent
->l1_backup_table
= g_try_malloc(l1_size
);
499 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
503 ret
= bdrv_pread(extent
->file
,
504 extent
->l1_backup_table_offset
,
505 extent
->l1_backup_table
,
508 bdrv_refresh_filename(extent
->file
->bs
);
509 error_setg_errno(errp
, -ret
,
510 "Could not read l1 backup table from extent '%s'",
511 extent
->file
->bs
->filename
);
514 for (i
= 0; i
< extent
->l1_size
; i
++) {
515 le32_to_cpus(&extent
->l1_backup_table
[i
]);
520 g_new(uint32_t, extent
->l2_size
* L2_CACHE_SIZE
);
523 g_free(extent
->l1_backup_table
);
525 g_free(extent
->l1_table
);
529 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
531 int flags
, Error
**errp
)
538 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
540 bdrv_refresh_filename(file
->bs
);
541 error_setg_errno(errp
, -ret
,
542 "Could not read header from file '%s'",
546 ret
= vmdk_add_extent(bs
, file
, false,
547 le32_to_cpu(header
.disk_sectors
),
548 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
550 le32_to_cpu(header
.l1dir_size
),
552 le32_to_cpu(header
.granularity
),
558 ret
= vmdk_init_tables(bs
, extent
, errp
);
560 /* free extent allocated by vmdk_add_extent */
561 vmdk_free_last_extent(bs
);
566 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
567 QDict
*options
, Error
**errp
);
569 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
575 size
= bdrv_getlength(file
->bs
);
577 error_setg_errno(errp
, -size
, "Could not access file");
582 /* Both descriptor file and sparse image must be much larger than 4
583 * bytes, also callers of vmdk_read_desc want to compare the first 4
584 * bytes with VMDK4_MAGIC, let's error out if less is read. */
585 error_setg(errp
, "File is too small, not a valid image");
589 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
590 buf
= g_malloc(size
+ 1);
592 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
594 error_setg_errno(errp
, -ret
, "Could not read from file");
603 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
605 int flags
, QDict
*options
, Error
**errp
)
609 uint32_t l1_size
, l1_entry_sectors
;
612 BDRVVmdkState
*s
= bs
->opaque
;
613 int64_t l1_backup_offset
= 0;
616 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
618 bdrv_refresh_filename(file
->bs
);
619 error_setg_errno(errp
, -ret
,
620 "Could not read header from file '%s'",
624 if (header
.capacity
== 0) {
625 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
627 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
631 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
637 if (!s
->create_type
) {
638 s
->create_type
= g_strdup("monolithicSparse");
641 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
643 * The footer takes precedence over the header, so read it in. The
644 * footer starts at offset -1024 from the end: One sector for the
645 * footer, and another one for the end-of-stream marker.
652 uint8_t pad
[512 - 16];
653 } QEMU_PACKED footer_marker
;
657 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
663 uint8_t pad
[512 - 16];
664 } QEMU_PACKED eos_marker
;
665 } QEMU_PACKED footer
;
667 ret
= bdrv_pread(file
,
668 bs
->file
->bs
->total_sectors
* 512 - 1536,
669 &footer
, sizeof(footer
));
671 error_setg_errno(errp
, -ret
, "Failed to read footer");
675 /* Some sanity checks for the footer */
676 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
677 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
678 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
679 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
680 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
681 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
683 error_setg(errp
, "Invalid footer");
687 header
= footer
.header
;
691 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
692 if (le32_to_cpu(header
.version
) > 3) {
693 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
694 le32_to_cpu(header
.version
));
696 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
698 /* VMware KB 2064959 explains that version 3 added support for
699 * persistent changed block tracking (CBT), and backup software can
700 * read it as version=1 if it doesn't care about the changed area
701 * information. So we are safe to enable read only. */
702 error_setg(errp
, "VMDK version 3 must be read only");
706 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
707 error_setg(errp
, "L2 table size too big");
711 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
712 * le64_to_cpu(header
.granularity
);
713 if (l1_entry_sectors
== 0) {
714 error_setg(errp
, "L1 entry size is invalid");
717 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
719 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
720 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
722 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
723 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
724 (int64_t)(le64_to_cpu(header
.grain_offset
)
725 * BDRV_SECTOR_SIZE
));
729 ret
= vmdk_add_extent(bs
, file
, false,
730 le64_to_cpu(header
.capacity
),
731 le64_to_cpu(header
.gd_offset
) << 9,
734 le32_to_cpu(header
.num_gtes_per_gt
),
735 le64_to_cpu(header
.granularity
),
742 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
743 if (extent
->compressed
) {
744 g_free(s
->create_type
);
745 s
->create_type
= g_strdup("streamOptimized");
747 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
748 extent
->version
= le32_to_cpu(header
.version
);
749 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
750 ret
= vmdk_init_tables(bs
, extent
, errp
);
752 /* free extent allocated by vmdk_add_extent */
753 vmdk_free_last_extent(bs
);
758 /* find an option value out of descriptor file */
759 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
760 char *buf
, int buf_size
)
762 char *opt_pos
, *opt_end
;
763 const char *end
= desc
+ strlen(desc
);
765 opt_pos
= strstr(desc
, opt_name
);
769 /* Skip "=\"" following opt_name */
770 opt_pos
+= strlen(opt_name
) + 2;
771 if (opt_pos
>= end
) {
775 while (opt_end
< end
&& *opt_end
!= '"') {
778 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
781 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
785 /* Open an extent file and append to bs array */
786 static int vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
787 char *buf
, QDict
*options
, Error
**errp
)
791 magic
= ldl_be_p(buf
);
794 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
797 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
800 error_setg(errp
, "Image not in VMDK format");
806 static const char *next_line(const char *s
)
817 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
818 const char *desc_file_path
, QDict
*options
,
830 BdrvChild
*extent_file
;
831 BDRVVmdkState
*s
= bs
->opaque
;
833 char extent_opt_prefix
[32];
834 Error
*local_err
= NULL
;
836 for (p
= desc
; *p
; p
= next_line(p
)) {
837 /* parse extent line in one of below formats:
839 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
840 * RW [size in sectors] SPARSE "file-name.vmdk"
841 * RW [size in sectors] VMFS "file-name.vmdk"
842 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
845 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
846 access
, §ors
, type
, fname
, &flat_offset
);
847 if (matches
< 4 || strcmp(access
, "RW")) {
849 } else if (!strcmp(type
, "FLAT")) {
850 if (matches
!= 5 || flat_offset
< 0) {
853 } else if (!strcmp(type
, "VMFS")) {
859 } else if (matches
!= 4) {
864 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
865 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
866 (strcmp(access
, "RW"))) {
870 if (!path_is_absolute(fname
) && !path_has_protocol(fname
) &&
873 bdrv_refresh_filename(bs
->file
->bs
);
874 error_setg(errp
, "Cannot use relative extent paths with VMDK "
875 "descriptor file '%s'", bs
->file
->bs
->filename
);
879 extent_path
= path_combine(desc_file_path
, fname
);
881 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
884 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
885 bs
, &child_file
, false, &local_err
);
888 error_propagate(errp
, local_err
);
892 /* save to extents array */
893 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
896 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
897 0, 0, 0, 0, 0, &extent
, errp
);
899 bdrv_unref_child(bs
, extent_file
);
902 extent
->flat_start_offset
= flat_offset
<< 9;
903 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
904 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
905 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
909 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
914 bdrv_unref_child(bs
, extent_file
);
917 extent
= &s
->extents
[s
->num_extents
- 1];
919 error_setg(errp
, "Unsupported extent type '%s'", type
);
920 bdrv_unref_child(bs
, extent_file
);
923 extent
->type
= g_strdup(type
);
930 if (np
[-1] == '\n') {
933 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
937 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
938 QDict
*options
, Error
**errp
)
942 BDRVVmdkState
*s
= bs
->opaque
;
944 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
945 error_setg(errp
, "invalid VMDK image descriptor");
949 if (strcmp(ct
, "monolithicFlat") &&
950 strcmp(ct
, "vmfs") &&
951 strcmp(ct
, "vmfsSparse") &&
952 strcmp(ct
, "twoGbMaxExtentSparse") &&
953 strcmp(ct
, "twoGbMaxExtentFlat")) {
954 error_setg(errp
, "Unsupported image type '%s'", ct
);
958 s
->create_type
= g_strdup(ct
);
960 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->bs
->exact_filename
, options
,
966 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
971 BDRVVmdkState
*s
= bs
->opaque
;
973 Error
*local_err
= NULL
;
975 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
981 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
986 magic
= ldl_be_p(buf
);
990 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
992 s
->desc_offset
= 0x200;
995 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1002 /* try to open parent images, if exist */
1003 ret
= vmdk_parent_open(bs
);
1007 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1011 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1015 qemu_co_mutex_init(&s
->lock
);
1017 /* Disable migration when VMDK images are used */
1018 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1019 "does not support live migration",
1020 bdrv_get_device_or_node_name(bs
));
1021 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1023 error_propagate(errp
, local_err
);
1024 error_free(s
->migration_blocker
);
1033 g_free(s
->create_type
);
1034 s
->create_type
= NULL
;
1035 vmdk_free_extents(bs
);
1040 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1042 BDRVVmdkState
*s
= bs
->opaque
;
1045 for (i
= 0; i
< s
->num_extents
; i
++) {
1046 if (!s
->extents
[i
].flat
) {
1047 bs
->bl
.pwrite_zeroes_alignment
=
1048 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1049 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1057 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1058 * to the cluster at @cluster_sector_num.
1060 * If @skip_start_sector < @skip_end_sector, the relative range
1061 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1062 * it for call to write user data in the request.
1064 static int get_whole_cluster(BlockDriverState
*bs
,
1066 uint64_t cluster_offset
,
1068 uint64_t skip_start_bytes
,
1069 uint64_t skip_end_bytes
)
1072 int64_t cluster_bytes
;
1073 uint8_t *whole_grain
;
1075 /* For COW, align request sector_num to cluster start */
1076 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1077 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1078 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1081 memset(whole_grain
, 0, skip_start_bytes
);
1082 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1085 assert(skip_end_bytes
<= cluster_bytes
);
1086 /* we will be here if it's first write on non-exist grain(cluster).
1087 * try to read from parent image, if exist */
1088 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1093 /* Read backing data before skip range */
1094 if (skip_start_bytes
> 0) {
1096 /* qcow2 emits this on bs->file instead of bs->backing */
1097 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1098 ret
= bdrv_pread(bs
->backing
, offset
, whole_grain
,
1105 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1106 ret
= bdrv_pwrite(extent
->file
, cluster_offset
, whole_grain
,
1113 /* Read backing data after skip range */
1114 if (skip_end_bytes
< cluster_bytes
) {
1116 /* qcow2 emits this on bs->file instead of bs->backing */
1117 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1118 ret
= bdrv_pread(bs
->backing
, offset
+ skip_end_bytes
,
1119 whole_grain
+ skip_end_bytes
,
1120 cluster_bytes
- skip_end_bytes
);
1126 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1127 ret
= bdrv_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1128 whole_grain
+ skip_end_bytes
,
1129 cluster_bytes
- skip_end_bytes
);
1138 qemu_vfree(whole_grain
);
1142 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1145 offset
= cpu_to_le32(offset
);
1146 /* update L2 table */
1147 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1148 if (bdrv_pwrite_sync(extent
->file
,
1149 ((int64_t)m_data
->l2_offset
* 512)
1150 + (m_data
->l2_index
* sizeof(offset
)),
1151 &offset
, sizeof(offset
)) < 0) {
1154 /* update backup L2 table */
1155 if (extent
->l1_backup_table_offset
!= 0) {
1156 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1157 if (bdrv_pwrite_sync(extent
->file
,
1158 ((int64_t)m_data
->l2_offset
* 512)
1159 + (m_data
->l2_index
* sizeof(offset
)),
1160 &offset
, sizeof(offset
)) < 0) {
1164 if (m_data
->l2_cache_entry
) {
1165 *m_data
->l2_cache_entry
= offset
;
1172 * get_cluster_offset
1174 * Look up cluster offset in extent file by sector number, and store in
1177 * For flat extents, the start offset as parsed from the description file is
1180 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1181 * offset for a new cluster and update L2 cache. If there is a backing file,
1182 * COW is done before returning; otherwise, zeroes are written to the allocated
1183 * cluster. Both COW and zero writing skips the sector range
1184 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1185 * has new data to write there.
1187 * Returns: VMDK_OK if cluster exists and mapped in the image.
1188 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1189 * VMDK_ERROR if failed.
1191 static int get_cluster_offset(BlockDriverState
*bs
,
1193 VmdkMetaData
*m_data
,
1196 uint64_t *cluster_offset
,
1197 uint64_t skip_start_bytes
,
1198 uint64_t skip_end_bytes
)
1200 unsigned int l1_index
, l2_offset
, l2_index
;
1201 int min_index
, i
, j
;
1202 uint32_t min_count
, *l2_table
;
1203 bool zeroed
= false;
1205 int64_t cluster_sector
;
1211 *cluster_offset
= extent
->flat_start_offset
;
1215 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1216 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1217 if (l1_index
>= extent
->l1_size
) {
1220 l2_offset
= extent
->l1_table
[l1_index
];
1222 return VMDK_UNALLOC
;
1224 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1225 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1226 /* increment the hit count */
1227 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1228 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1229 extent
->l2_cache_counts
[j
] >>= 1;
1232 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
1236 /* not found: load a new entry in the least used one */
1238 min_count
= 0xffffffff;
1239 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1240 if (extent
->l2_cache_counts
[i
] < min_count
) {
1241 min_count
= extent
->l2_cache_counts
[i
];
1245 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1246 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1247 if (bdrv_pread(extent
->file
,
1248 (int64_t)l2_offset
* 512,
1250 extent
->l2_size
* sizeof(uint32_t)
1251 ) != extent
->l2_size
* sizeof(uint32_t)) {
1255 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1256 extent
->l2_cache_counts
[min_index
] = 1;
1258 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1259 cluster_sector
= le32_to_cpu(l2_table
[l2_index
]);
1261 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1265 if (!cluster_sector
|| zeroed
) {
1267 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1270 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1274 cluster_sector
= extent
->next_cluster_sector
;
1275 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1277 /* First of all we write grain itself, to avoid race condition
1278 * that may to corrupt the image.
1279 * This problem may occur because of insufficient space on host disk
1280 * or inappropriate VM shutdown.
1282 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1283 offset
, skip_start_bytes
, skip_end_bytes
);
1289 m_data
->l1_index
= l1_index
;
1290 m_data
->l2_index
= l2_index
;
1291 m_data
->l2_offset
= l2_offset
;
1292 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1295 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1299 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1300 int64_t sector_num
, VmdkExtent
*start_hint
)
1302 VmdkExtent
*extent
= start_hint
;
1305 extent
= &s
->extents
[0];
1307 while (extent
< &s
->extents
[s
->num_extents
]) {
1308 if (sector_num
< extent
->end_sector
) {
1316 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1319 uint64_t extent_begin_offset
, extent_relative_offset
;
1320 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1322 extent_begin_offset
=
1323 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1324 extent_relative_offset
= offset
- extent_begin_offset
;
1325 return extent_relative_offset
% cluster_size
;
1328 static int coroutine_fn
vmdk_co_block_status(BlockDriverState
*bs
,
1330 int64_t offset
, int64_t bytes
,
1331 int64_t *pnum
, int64_t *map
,
1332 BlockDriverState
**file
)
1334 BDRVVmdkState
*s
= bs
->opaque
;
1335 int64_t index_in_cluster
, n
, ret
;
1336 uint64_t cluster_offset
;
1339 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1343 qemu_co_mutex_lock(&s
->lock
);
1344 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1346 qemu_co_mutex_unlock(&s
->lock
);
1348 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1357 ret
= BDRV_BLOCK_ZERO
;
1360 ret
= BDRV_BLOCK_DATA
;
1361 if (!extent
->compressed
) {
1362 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1363 *map
= cluster_offset
+ index_in_cluster
;
1365 *file
= extent
->file
->bs
;
1369 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1370 *pnum
= MIN(n
, bytes
);
1374 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1375 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1376 uint64_t qiov_offset
, uint64_t n_bytes
,
1380 VmdkGrainMarker
*data
= NULL
;
1382 QEMUIOVector local_qiov
;
1383 int64_t write_offset
;
1384 int64_t write_end_sector
;
1386 if (extent
->compressed
) {
1387 void *compressed_data
;
1389 if (!extent
->has_marker
) {
1393 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1394 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1396 compressed_data
= g_malloc(n_bytes
);
1397 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1398 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1399 g_free(compressed_data
);
1401 if (ret
!= Z_OK
|| buf_len
== 0) {
1406 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1407 data
->size
= cpu_to_le32(buf_len
);
1409 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1410 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1412 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1414 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1415 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1417 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1420 write_offset
= cluster_offset
+ offset_in_cluster
;
1421 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1424 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1426 if (extent
->compressed
) {
1427 extent
->next_cluster_sector
= write_end_sector
;
1429 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1439 if (!extent
->compressed
) {
1440 qemu_iovec_destroy(&local_qiov
);
1445 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1446 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1450 int cluster_bytes
, buf_bytes
;
1451 uint8_t *cluster_buf
, *compressed_data
;
1452 uint8_t *uncomp_buf
;
1454 VmdkGrainMarker
*marker
;
1458 if (!extent
->compressed
) {
1459 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1460 ret
= bdrv_co_preadv(extent
->file
,
1461 cluster_offset
+ offset_in_cluster
, bytes
,
1468 cluster_bytes
= extent
->cluster_sectors
* 512;
1469 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1470 buf_bytes
= cluster_bytes
* 2;
1471 cluster_buf
= g_malloc(buf_bytes
);
1472 uncomp_buf
= g_malloc(cluster_bytes
);
1473 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1474 ret
= bdrv_pread(extent
->file
,
1476 cluster_buf
, buf_bytes
);
1480 compressed_data
= cluster_buf
;
1481 buf_len
= cluster_bytes
;
1482 data_len
= cluster_bytes
;
1483 if (extent
->has_marker
) {
1484 marker
= (VmdkGrainMarker
*)cluster_buf
;
1485 compressed_data
= marker
->data
;
1486 data_len
= le32_to_cpu(marker
->size
);
1488 if (!data_len
|| data_len
> buf_bytes
) {
1492 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1498 if (offset_in_cluster
< 0 ||
1499 offset_in_cluster
+ bytes
> buf_len
) {
1503 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1508 g_free(cluster_buf
);
1512 static int coroutine_fn
1513 vmdk_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1514 QEMUIOVector
*qiov
, int flags
)
1516 BDRVVmdkState
*s
= bs
->opaque
;
1518 uint64_t n_bytes
, offset_in_cluster
;
1519 VmdkExtent
*extent
= NULL
;
1520 QEMUIOVector local_qiov
;
1521 uint64_t cluster_offset
;
1522 uint64_t bytes_done
= 0;
1524 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1525 qemu_co_mutex_lock(&s
->lock
);
1528 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1533 ret
= get_cluster_offset(bs
, extent
, NULL
,
1534 offset
, false, &cluster_offset
, 0, 0);
1535 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1537 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1538 - offset_in_cluster
);
1540 if (ret
!= VMDK_OK
) {
1541 /* if not allocated, try to read from parent image, if exist */
1542 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1543 if (!vmdk_is_cid_valid(bs
)) {
1548 qemu_iovec_reset(&local_qiov
);
1549 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1551 /* qcow2 emits this on bs->file instead of bs->backing */
1552 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1553 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
1559 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
1562 qemu_iovec_reset(&local_qiov
);
1563 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1565 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
1566 &local_qiov
, n_bytes
);
1573 bytes_done
+= n_bytes
;
1578 qemu_co_mutex_unlock(&s
->lock
);
1579 qemu_iovec_destroy(&local_qiov
);
1586 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1587 * if possible, otherwise return -ENOTSUP.
1588 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1589 * with each cluster. By dry run we can find if the zero write
1590 * is possible without modifying image data.
1592 * Returns: error code with 0 for success.
1594 static int vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1595 uint64_t bytes
, QEMUIOVector
*qiov
,
1596 bool zeroed
, bool zero_dry_run
)
1598 BDRVVmdkState
*s
= bs
->opaque
;
1599 VmdkExtent
*extent
= NULL
;
1601 int64_t offset_in_cluster
, n_bytes
;
1602 uint64_t cluster_offset
;
1603 uint64_t bytes_done
= 0;
1604 VmdkMetaData m_data
;
1606 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
1607 error_report("Wrong offset: offset=0x%" PRIx64
1608 " total_sectors=0x%" PRIx64
,
1609 offset
, bs
->total_sectors
);
1614 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1618 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1619 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1620 - offset_in_cluster
);
1622 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1623 !(extent
->compressed
|| zeroed
),
1624 &cluster_offset
, offset_in_cluster
,
1625 offset_in_cluster
+ n_bytes
);
1626 if (extent
->compressed
) {
1627 if (ret
== VMDK_OK
) {
1628 /* Refuse write to allocated cluster for streamOptimized */
1629 error_report("Could not write to allocated cluster"
1630 " for streamOptimized");
1634 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1635 true, &cluster_offset
, 0, 0);
1638 if (ret
== VMDK_ERROR
) {
1642 /* Do zeroed write, buf is ignored */
1643 if (extent
->has_zero_grain
&&
1644 offset_in_cluster
== 0 &&
1645 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
1646 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1647 if (!zero_dry_run
) {
1648 /* update L2 tables */
1649 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
1658 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
1659 qiov
, bytes_done
, n_bytes
, offset
);
1664 /* update L2 tables */
1665 if (vmdk_L2update(extent
, &m_data
,
1666 cluster_offset
>> BDRV_SECTOR_BITS
)
1674 bytes_done
+= n_bytes
;
1676 /* update CID on the first write every time the virtual disk is
1678 if (!s
->cid_updated
) {
1679 ret
= vmdk_write_cid(bs
, g_random_int());
1683 s
->cid_updated
= true;
1689 static int coroutine_fn
1690 vmdk_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1691 QEMUIOVector
*qiov
, int flags
)
1694 BDRVVmdkState
*s
= bs
->opaque
;
1695 qemu_co_mutex_lock(&s
->lock
);
1696 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
1697 qemu_co_mutex_unlock(&s
->lock
);
1701 static int coroutine_fn
1702 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
1703 uint64_t bytes
, QEMUIOVector
*qiov
)
1706 /* The caller will write bytes 0 to signal EOF.
1707 * When receive it, we align EOF to a sector boundary. */
1708 BDRVVmdkState
*s
= bs
->opaque
;
1712 for (i
= 0; i
< s
->num_extents
; i
++) {
1713 length
= bdrv_getlength(s
->extents
[i
].file
->bs
);
1717 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
1718 ret
= bdrv_truncate(s
->extents
[i
].file
, length
,
1719 PREALLOC_MODE_OFF
, NULL
);
1726 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
1729 static int coroutine_fn
vmdk_co_pwrite_zeroes(BlockDriverState
*bs
,
1732 BdrvRequestFlags flags
)
1735 BDRVVmdkState
*s
= bs
->opaque
;
1737 qemu_co_mutex_lock(&s
->lock
);
1738 /* write zeroes could fail if sectors not aligned to cluster, test it with
1739 * dry_run == true before really updating image */
1740 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
1742 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
1744 qemu_co_mutex_unlock(&s
->lock
);
1748 static int vmdk_init_extent(BlockBackend
*blk
,
1749 int64_t filesize
, bool flat
,
1750 bool compress
, bool zeroed_grain
,
1755 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
1756 uint32_t *gd_buf
= NULL
;
1760 ret
= blk_truncate(blk
, filesize
, PREALLOC_MODE_OFF
, errp
);
1763 magic
= cpu_to_be32(VMDK4_MAGIC
);
1764 memset(&header
, 0, sizeof(header
));
1767 } else if (zeroed_grain
) {
1772 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1773 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1774 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1775 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1776 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
1777 header
.granularity
= 128;
1778 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
1780 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
1781 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
1783 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
1784 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
1786 header
.desc_offset
= 1;
1787 header
.desc_size
= 20;
1788 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1789 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
1790 header
.grain_offset
=
1791 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
1792 header
.granularity
);
1793 /* swap endianness for all header fields */
1794 header
.version
= cpu_to_le32(header
.version
);
1795 header
.flags
= cpu_to_le32(header
.flags
);
1796 header
.capacity
= cpu_to_le64(header
.capacity
);
1797 header
.granularity
= cpu_to_le64(header
.granularity
);
1798 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1799 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1800 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1801 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1802 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1803 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1804 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1806 header
.check_bytes
[0] = 0xa;
1807 header
.check_bytes
[1] = 0x20;
1808 header
.check_bytes
[2] = 0xd;
1809 header
.check_bytes
[3] = 0xa;
1811 /* write all the data */
1812 ret
= blk_pwrite(blk
, 0, &magic
, sizeof(magic
), 0);
1814 error_setg(errp
, QERR_IO_ERROR
);
1817 ret
= blk_pwrite(blk
, sizeof(magic
), &header
, sizeof(header
), 0);
1819 error_setg(errp
, QERR_IO_ERROR
);
1823 ret
= blk_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9,
1824 PREALLOC_MODE_OFF
, errp
);
1829 /* write grain directory */
1830 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
1831 gd_buf
= g_malloc0(gd_buf_size
);
1832 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
1833 i
< gt_count
; i
++, tmp
+= gt_size
) {
1834 gd_buf
[i
] = cpu_to_le32(tmp
);
1836 ret
= blk_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
1837 gd_buf
, gd_buf_size
, 0);
1839 error_setg(errp
, QERR_IO_ERROR
);
1843 /* write backup grain directory */
1844 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
1845 i
< gt_count
; i
++, tmp
+= gt_size
) {
1846 gd_buf
[i
] = cpu_to_le32(tmp
);
1848 ret
= blk_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
1849 gd_buf
, gd_buf_size
, 0);
1851 error_setg(errp
, QERR_IO_ERROR
);
1860 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1861 bool flat
, bool compress
, bool zeroed_grain
,
1863 QemuOpts
*opts
, Error
**errp
)
1866 BlockBackend
*blk
= NULL
;
1867 Error
*local_err
= NULL
;
1869 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1871 error_propagate(errp
, local_err
);
1875 blk
= blk_new_open(filename
, NULL
, NULL
,
1876 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
1879 error_propagate(errp
, local_err
);
1884 blk_set_allow_write_beyond_eof(blk
, true);
1886 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
1899 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1900 char *postfix
, size_t buf_len
, Error
**errp
)
1904 if (filename
== NULL
|| !strlen(filename
)) {
1905 error_setg(errp
, "No filename provided");
1908 p
= strrchr(filename
, '/');
1910 p
= strrchr(filename
, '\\');
1913 p
= strrchr(filename
, ':');
1917 if (p
- filename
>= buf_len
) {
1920 pstrcpy(path
, p
- filename
+ 1, filename
);
1925 q
= strrchr(p
, '.');
1927 pstrcpy(prefix
, buf_len
, p
);
1930 if (q
- p
>= buf_len
) {
1933 pstrcpy(prefix
, q
- p
+ 1, p
);
1934 pstrcpy(postfix
, buf_len
, q
);
1940 * idx == 0: get or create the descriptor file (also the image file if in a
1942 * idx >= 1: get the n-th extent if in a split subformat
1944 typedef BlockBackend
*(*vmdk_create_extent_fn
)(int64_t size
,
1953 static void vmdk_desc_add_extent(GString
*desc
,
1954 const char *extent_line_fmt
,
1955 int64_t size
, const char *filename
)
1957 char *basename
= g_path_get_basename(filename
);
1959 g_string_append_printf(desc
, extent_line_fmt
,
1960 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
1964 static int coroutine_fn
vmdk_co_do_create(int64_t size
,
1965 BlockdevVmdkSubformat subformat
,
1966 BlockdevVmdkAdapterType adapter_type
,
1967 const char *backing_file
,
1968 const char *hw_version
,
1971 vmdk_create_extent_fn extent_fn
,
1976 BlockBackend
*blk
= NULL
;
1977 BlockBackend
*extent_blk
;
1978 Error
*local_err
= NULL
;
1981 bool flat
, split
, compress
;
1982 GString
*ext_desc_lines
;
1983 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1984 int64_t extent_size
;
1985 int64_t created_size
= 0;
1986 const char *extent_line_fmt
;
1987 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
1988 uint32_t parent_cid
= 0xffffffff;
1989 uint32_t number_heads
= 16;
1990 uint32_t desc_offset
= 0, desc_len
;
1991 const char desc_template
[] =
1992 "# Disk DescriptorFile\n"
1995 "parentCID=%" PRIx32
"\n"
1996 "createType=\"%s\"\n"
1999 "# Extent description\n"
2002 "# The Disk Data Base\n"
2005 "ddb.virtualHWVersion = \"%s\"\n"
2006 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2007 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2008 "ddb.geometry.sectors = \"63\"\n"
2009 "ddb.adapterType = \"%s\"\n";
2011 ext_desc_lines
= g_string_new(NULL
);
2013 /* Read out options */
2017 "compat6 cannot be enabled with hwversion set");
2027 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2028 /* that's the number of heads with which vmware operates when
2029 creating, exporting, etc. vmdk files with a non-ide adapter type */
2032 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2033 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2034 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2035 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2036 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2039 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2041 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2043 if (flat
&& backing_file
) {
2044 error_setg(errp
, "Flat image can't have backing file");
2048 if (flat
&& zeroed_grain
) {
2049 error_setg(errp
, "Flat image can't enable zeroed grain");
2054 /* Create extents */
2056 extent_size
= split_size
;
2060 if (!split
&& !flat
) {
2061 created_size
= extent_size
;
2065 /* Get the descriptor file BDS */
2066 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2072 if (!split
&& !flat
) {
2073 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2074 blk_bs(blk
)->filename
);
2078 BlockBackend
*backing
;
2079 char *full_backing
=
2080 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2084 error_propagate(errp
, local_err
);
2088 assert(full_backing
);
2090 backing
= blk_new_open(full_backing
, NULL
, NULL
,
2091 BDRV_O_NO_BACKING
, errp
);
2092 g_free(full_backing
);
2093 if (backing
== NULL
) {
2097 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2098 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2099 blk_bs(backing
)->drv
->format_name
);
2104 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2107 error_setg(errp
, "Failed to read parent CID");
2110 snprintf(parent_desc_line
, BUF_SIZE
,
2111 "parentFileNameHint=\"%s\"", backing_file
);
2114 while (created_size
< size
) {
2115 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2116 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2117 zeroed_grain
, opaque
, errp
);
2122 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2123 blk_bs(extent_blk
)->filename
);
2124 created_size
+= cur_size
;
2126 blk_unref(extent_blk
);
2129 /* Check whether we got excess extents */
2130 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2133 blk_unref(extent_blk
);
2134 error_setg(errp
, "List of extents contains unused extents");
2139 /* generate descriptor file */
2140 desc
= g_strdup_printf(desc_template
,
2143 BlockdevVmdkSubformat_str(subformat
),
2145 ext_desc_lines
->str
,
2148 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2150 BlockdevVmdkAdapterType_str(adapter_type
));
2151 desc_len
= strlen(desc
);
2152 /* the descriptor offset = 0x200 */
2153 if (!split
&& !flat
) {
2154 desc_offset
= 0x200;
2157 ret
= blk_pwrite(blk
, desc_offset
, desc
, desc_len
, 0);
2159 error_setg_errno(errp
, -ret
, "Could not write description");
2162 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2163 * for description file */
2164 if (desc_offset
== 0) {
2165 ret
= blk_truncate(blk
, desc_len
, PREALLOC_MODE_OFF
, errp
);
2176 g_free(parent_desc_line
);
2177 g_string_free(ext_desc_lines
, true);
2186 } VMDKCreateOptsData
;
2188 static BlockBackend
*vmdk_co_create_opts_cb(int64_t size
, int idx
,
2189 bool flat
, bool split
, bool compress
,
2190 bool zeroed_grain
, void *opaque
,
2193 BlockBackend
*blk
= NULL
;
2194 BlockDriverState
*bs
= NULL
;
2195 VMDKCreateOptsData
*data
= opaque
;
2196 char *ext_filename
= NULL
;
2197 char *rel_filename
= NULL
;
2199 /* We're done, don't create excess extents. */
2201 assert(errp
== NULL
);
2206 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2208 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2210 flat
? 'f' : 's', idx
, data
->postfix
);
2213 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2216 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2217 g_free(rel_filename
);
2219 if (vmdk_create_extent(ext_filename
, size
,
2220 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2226 g_free(ext_filename
);
2230 static int coroutine_fn
vmdk_co_create_opts(const char *filename
, QemuOpts
*opts
,
2233 Error
*local_err
= NULL
;
2235 int64_t total_size
= 0;
2236 char *adapter_type
= NULL
;
2237 BlockdevVmdkAdapterType adapter_type_enum
;
2238 char *backing_file
= NULL
;
2239 char *hw_version
= NULL
;
2241 BlockdevVmdkSubformat subformat
;
2243 char *path
= g_malloc0(PATH_MAX
);
2244 char *prefix
= g_malloc0(PATH_MAX
);
2245 char *postfix
= g_malloc0(PATH_MAX
);
2246 char *desc_line
= g_malloc0(BUF_SIZE
);
2247 char *ext_filename
= g_malloc0(PATH_MAX
);
2248 char *desc_filename
= g_malloc0(PATH_MAX
);
2249 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2252 VMDKCreateOptsData data
;
2254 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2258 /* Read out options */
2259 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2261 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2262 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2263 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2264 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2265 if (strcmp(hw_version
, "undefined") == 0) {
2269 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2270 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2273 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2275 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2278 error_propagate(errp
, local_err
);
2283 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2287 /* Default format to monolithicSparse */
2288 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2290 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2292 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2295 error_propagate(errp
, local_err
);
2300 data
= (VMDKCreateOptsData
){
2306 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2307 backing_file
, hw_version
, compat6
, zeroed_grain
,
2308 vmdk_co_create_opts_cb
, &data
, errp
);
2311 g_free(adapter_type
);
2312 g_free(backing_file
);
2320 g_free(ext_filename
);
2321 g_free(desc_filename
);
2322 g_free(parent_desc_line
);
2326 static BlockBackend
*vmdk_co_create_cb(int64_t size
, int idx
,
2327 bool flat
, bool split
, bool compress
,
2328 bool zeroed_grain
, void *opaque
,
2332 BlockDriverState
*bs
;
2334 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2337 bs
= bdrv_open_blockdev_ref(opts
->file
, errp
);
2340 BlockdevRefList
*list
= opts
->extents
;
2341 for (i
= 1; i
< idx
; i
++) {
2342 if (!list
|| !list
->next
) {
2343 error_setg(errp
, "Extent [%d] not specified", i
);
2349 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2352 bs
= bdrv_open_blockdev_ref(list
->value
, errp
);
2357 blk
= blk_new(BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
2359 if (blk_insert_bs(blk
, bs
, errp
)) {
2363 blk_set_allow_write_beyond_eof(blk
, true);
2367 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2376 static int coroutine_fn
vmdk_co_create(BlockdevCreateOptions
*create_options
,
2380 BlockdevCreateOptionsVmdk
*opts
;
2382 opts
= &create_options
->u
.vmdk
;
2384 /* Validate options */
2385 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2386 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2391 ret
= vmdk_co_do_create(opts
->size
,
2406 static void vmdk_close(BlockDriverState
*bs
)
2408 BDRVVmdkState
*s
= bs
->opaque
;
2410 vmdk_free_extents(bs
);
2411 g_free(s
->create_type
);
2413 migrate_del_blocker(s
->migration_blocker
);
2414 error_free(s
->migration_blocker
);
2417 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
2419 BDRVVmdkState
*s
= bs
->opaque
;
2423 for (i
= 0; i
< s
->num_extents
; i
++) {
2424 err
= bdrv_co_flush(s
->extents
[i
].file
->bs
);
2432 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2437 BDRVVmdkState
*s
= bs
->opaque
;
2439 ret
= bdrv_get_allocated_file_size(bs
->file
->bs
);
2443 for (i
= 0; i
< s
->num_extents
; i
++) {
2444 if (s
->extents
[i
].file
== bs
->file
) {
2447 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2456 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2459 BDRVVmdkState
*s
= bs
->opaque
;
2461 /* If has a flat extent and its underlying storage doesn't have zero init,
2463 for (i
= 0; i
< s
->num_extents
; i
++) {
2464 if (s
->extents
[i
].flat
) {
2465 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2473 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2475 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2477 bdrv_refresh_filename(extent
->file
->bs
);
2478 *info
= (ImageInfo
){
2479 .filename
= g_strdup(extent
->file
->bs
->filename
),
2480 .format
= g_strdup(extent
->type
),
2481 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2482 .compressed
= extent
->compressed
,
2483 .has_compressed
= extent
->compressed
,
2484 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2485 .has_cluster_size
= !extent
->flat
,
2491 static int coroutine_fn
vmdk_co_check(BlockDriverState
*bs
,
2492 BdrvCheckResult
*result
,
2495 BDRVVmdkState
*s
= bs
->opaque
;
2496 VmdkExtent
*extent
= NULL
;
2497 int64_t sector_num
= 0;
2498 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2500 uint64_t cluster_offset
;
2507 if (sector_num
>= total_sectors
) {
2510 extent
= find_extent(s
, sector_num
, extent
);
2513 "ERROR: could not find extent for sector %" PRId64
"\n",
2518 ret
= get_cluster_offset(bs
, extent
, NULL
,
2519 sector_num
<< BDRV_SECTOR_BITS
,
2520 false, &cluster_offset
, 0, 0);
2521 if (ret
== VMDK_ERROR
) {
2523 "ERROR: could not get cluster_offset for sector %"
2524 PRId64
"\n", sector_num
);
2527 if (ret
== VMDK_OK
) {
2528 int64_t extent_len
= bdrv_getlength(extent
->file
->bs
);
2529 if (extent_len
< 0) {
2531 "ERROR: could not get extent file length for sector %"
2532 PRId64
"\n", sector_num
);
2536 if (cluster_offset
>= extent_len
) {
2538 "ERROR: cluster offset for sector %"
2539 PRId64
" points after EOF\n", sector_num
);
2544 sector_num
+= extent
->cluster_sectors
;
2547 result
->corruptions
++;
2551 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
,
2555 BDRVVmdkState
*s
= bs
->opaque
;
2556 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2557 ImageInfoList
**next
;
2559 *spec_info
= (ImageInfoSpecific
){
2560 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2562 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
2566 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
2567 .create_type
= g_strdup(s
->create_type
),
2569 .parent_cid
= s
->parent_cid
,
2572 next
= &spec_info
->u
.vmdk
.data
->extents
;
2573 for (i
= 0; i
< s
->num_extents
; i
++) {
2574 *next
= g_new0(ImageInfoList
, 1);
2575 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2576 (*next
)->next
= NULL
;
2577 next
= &(*next
)->next
;
2583 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
2585 return a
->flat
== b
->flat
&&
2586 a
->compressed
== b
->compressed
&&
2587 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
2590 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2593 BDRVVmdkState
*s
= bs
->opaque
;
2594 assert(s
->num_extents
);
2596 /* See if we have multiple extents but they have different cases */
2597 for (i
= 1; i
< s
->num_extents
; i
++) {
2598 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
2602 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2603 if (!s
->extents
[0].flat
) {
2604 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2609 static void vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
2610 bool backing_overridden
)
2612 /* No children but file and backing can be explicitly specified (TODO) */
2613 qdict_put(target
, "file",
2614 qobject_ref(bs
->file
->bs
->full_open_options
));
2616 if (backing_overridden
) {
2618 qdict_put(target
, "backing",
2619 qobject_ref(bs
->backing
->bs
->full_open_options
));
2621 qdict_put_null(target
, "backing");
2626 static QemuOptsList vmdk_create_opts
= {
2627 .name
= "vmdk-create-opts",
2628 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
2631 .name
= BLOCK_OPT_SIZE
,
2632 .type
= QEMU_OPT_SIZE
,
2633 .help
= "Virtual disk size"
2636 .name
= BLOCK_OPT_ADAPTER_TYPE
,
2637 .type
= QEMU_OPT_STRING
,
2638 .help
= "Virtual adapter type, can be one of "
2639 "ide (default), lsilogic, buslogic or legacyESX"
2642 .name
= BLOCK_OPT_BACKING_FILE
,
2643 .type
= QEMU_OPT_STRING
,
2644 .help
= "File name of a base image"
2647 .name
= BLOCK_OPT_COMPAT6
,
2648 .type
= QEMU_OPT_BOOL
,
2649 .help
= "VMDK version 6 image",
2650 .def_value_str
= "off"
2653 .name
= BLOCK_OPT_HWVERSION
,
2654 .type
= QEMU_OPT_STRING
,
2655 .help
= "VMDK hardware version",
2656 .def_value_str
= "undefined"
2659 .name
= BLOCK_OPT_SUBFMT
,
2660 .type
= QEMU_OPT_STRING
,
2662 "VMDK flat extent format, can be one of "
2663 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2666 .name
= BLOCK_OPT_ZEROED_GRAIN
,
2667 .type
= QEMU_OPT_BOOL
,
2668 .help
= "Enable efficient zero writes "
2669 "using the zeroed-grain GTE feature"
2671 { /* end of list */ }
2675 static BlockDriver bdrv_vmdk
= {
2676 .format_name
= "vmdk",
2677 .instance_size
= sizeof(BDRVVmdkState
),
2678 .bdrv_probe
= vmdk_probe
,
2679 .bdrv_open
= vmdk_open
,
2680 .bdrv_co_check
= vmdk_co_check
,
2681 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
2682 .bdrv_child_perm
= bdrv_format_default_perms
,
2683 .bdrv_co_preadv
= vmdk_co_preadv
,
2684 .bdrv_co_pwritev
= vmdk_co_pwritev
,
2685 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
2686 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
2687 .bdrv_close
= vmdk_close
,
2688 .bdrv_co_create_opts
= vmdk_co_create_opts
,
2689 .bdrv_co_create
= vmdk_co_create
,
2690 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
2691 .bdrv_co_block_status
= vmdk_co_block_status
,
2692 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
2693 .bdrv_has_zero_init
= vmdk_has_zero_init
,
2694 .bdrv_get_specific_info
= vmdk_get_specific_info
,
2695 .bdrv_refresh_limits
= vmdk_refresh_limits
,
2696 .bdrv_get_info
= vmdk_get_info
,
2697 .bdrv_gather_child_options
= vmdk_gather_child_options
,
2699 .supports_backing
= true,
2700 .create_opts
= &vmdk_create_opts
,
2703 static void bdrv_vmdk_init(void)
2705 bdrv_register(&bdrv_vmdk
);
2708 block_init(bdrv_vmdk_init
);