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) {
202 if (end
- p
>= strlen("version=X\r\n")) {
203 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
204 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
214 #define SECTOR_SIZE 512
215 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
216 #define BUF_SIZE 4096
217 #define HEADER_SIZE 512 /* first sector of 512 bytes */
219 static void vmdk_free_extents(BlockDriverState
*bs
)
222 BDRVVmdkState
*s
= bs
->opaque
;
225 for (i
= 0; i
< s
->num_extents
; i
++) {
229 g_free(e
->l1_backup_table
);
231 if (e
->file
!= bs
->file
) {
232 bdrv_unref_child(bs
, e
->file
);
238 static void vmdk_free_last_extent(BlockDriverState
*bs
)
240 BDRVVmdkState
*s
= bs
->opaque
;
242 if (s
->num_extents
== 0) {
246 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
249 /* Return -ve errno, or 0 on success and write CID into *pcid. */
250 static int vmdk_read_cid(BlockDriverState
*bs
, int parent
, uint32_t *pcid
)
254 const char *p_name
, *cid_str
;
256 BDRVVmdkState
*s
= bs
->opaque
;
259 desc
= g_malloc0(DESC_SIZE
);
260 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
266 cid_str
= "parentCID";
267 cid_str_size
= sizeof("parentCID");
270 cid_str_size
= sizeof("CID");
273 desc
[DESC_SIZE
- 1] = '\0';
274 p_name
= strstr(desc
, cid_str
);
275 if (p_name
== NULL
) {
279 p_name
+= cid_str_size
;
280 if (sscanf(p_name
, "%" SCNx32
, &cid
) != 1) {
292 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
294 char *desc
, *tmp_desc
;
295 char *p_name
, *tmp_str
;
296 BDRVVmdkState
*s
= bs
->opaque
;
299 desc
= g_malloc0(DESC_SIZE
);
300 tmp_desc
= g_malloc0(DESC_SIZE
);
301 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
306 desc
[DESC_SIZE
- 1] = '\0';
307 tmp_str
= strstr(desc
, "parentCID");
308 if (tmp_str
== NULL
) {
313 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
314 p_name
= strstr(desc
, "CID");
315 if (p_name
!= NULL
) {
316 p_name
+= sizeof("CID");
317 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
318 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
321 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
329 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
331 BDRVVmdkState
*s
= bs
->opaque
;
334 if (!s
->cid_checked
&& bs
->backing
) {
335 BlockDriverState
*p_bs
= bs
->backing
->bs
;
337 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
338 /* Backing file is not in vmdk format, so it does not have
339 * a CID, which makes the overlay's parent CID invalid */
343 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
344 /* read failure: report as not valid */
347 if (s
->parent_cid
!= cur_pcid
) {
352 s
->cid_checked
= true;
357 /* We have nothing to do for VMDK reopen, stubs just return success */
358 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
359 BlockReopenQueue
*queue
, Error
**errp
)
361 assert(state
!= NULL
);
362 assert(state
->bs
!= NULL
);
366 static int vmdk_parent_open(BlockDriverState
*bs
)
370 BDRVVmdkState
*s
= bs
->opaque
;
373 desc
= g_malloc0(DESC_SIZE
+ 1);
374 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
380 p_name
= strstr(desc
, "parentFileNameHint");
381 if (p_name
!= NULL
) {
384 p_name
+= sizeof("parentFileNameHint") + 1;
385 end_name
= strchr(p_name
, '\"');
386 if (end_name
== NULL
) {
390 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
395 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
396 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
397 bs
->auto_backing_file
);
405 /* Create and append extent to the extent array. Return the added VmdkExtent
406 * address. return NULL if allocation failed. */
407 static int vmdk_add_extent(BlockDriverState
*bs
,
408 BdrvChild
*file
, bool flat
, int64_t sectors
,
409 int64_t l1_offset
, int64_t l1_backup_offset
,
411 int l2_size
, uint64_t cluster_sectors
,
412 VmdkExtent
**new_extent
,
416 BDRVVmdkState
*s
= bs
->opaque
;
419 if (cluster_sectors
> 0x200000) {
420 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
421 error_setg(errp
, "Invalid granularity, image may be corrupt");
424 if (l1_size
> 512 * 1024 * 1024) {
425 /* Although with big capacity and small l1_entry_sectors, we can get a
426 * big l1_size, we don't want unbounded value to allocate the table.
427 * Limit it to 512M, which is 16PB for default cluster and L2 table
429 error_setg(errp
, "L1 size too big");
433 nb_sectors
= bdrv_nb_sectors(file
->bs
);
434 if (nb_sectors
< 0) {
438 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
439 extent
= &s
->extents
[s
->num_extents
];
442 memset(extent
, 0, sizeof(VmdkExtent
));
445 extent
->sectors
= sectors
;
446 extent
->l1_table_offset
= l1_offset
;
447 extent
->l1_backup_table_offset
= l1_backup_offset
;
448 extent
->l1_size
= l1_size
;
449 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
450 extent
->l2_size
= l2_size
;
451 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
452 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
454 if (s
->num_extents
> 1) {
455 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
457 extent
->end_sector
= extent
->sectors
;
459 bs
->total_sectors
= extent
->end_sector
;
461 *new_extent
= extent
;
466 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
473 /* read the L1 table */
474 l1_size
= extent
->l1_size
* sizeof(uint32_t);
475 extent
->l1_table
= g_try_malloc(l1_size
);
476 if (l1_size
&& extent
->l1_table
== NULL
) {
480 ret
= bdrv_pread(extent
->file
,
481 extent
->l1_table_offset
,
485 bdrv_refresh_filename(extent
->file
->bs
);
486 error_setg_errno(errp
, -ret
,
487 "Could not read l1 table from extent '%s'",
488 extent
->file
->bs
->filename
);
491 for (i
= 0; i
< extent
->l1_size
; i
++) {
492 le32_to_cpus(&extent
->l1_table
[i
]);
495 if (extent
->l1_backup_table_offset
) {
496 extent
->l1_backup_table
= g_try_malloc(l1_size
);
497 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
501 ret
= bdrv_pread(extent
->file
,
502 extent
->l1_backup_table_offset
,
503 extent
->l1_backup_table
,
506 bdrv_refresh_filename(extent
->file
->bs
);
507 error_setg_errno(errp
, -ret
,
508 "Could not read l1 backup table from extent '%s'",
509 extent
->file
->bs
->filename
);
512 for (i
= 0; i
< extent
->l1_size
; i
++) {
513 le32_to_cpus(&extent
->l1_backup_table
[i
]);
518 g_new(uint32_t, extent
->l2_size
* L2_CACHE_SIZE
);
521 g_free(extent
->l1_backup_table
);
523 g_free(extent
->l1_table
);
527 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
529 int flags
, Error
**errp
)
536 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
538 bdrv_refresh_filename(file
->bs
);
539 error_setg_errno(errp
, -ret
,
540 "Could not read header from file '%s'",
544 ret
= vmdk_add_extent(bs
, file
, false,
545 le32_to_cpu(header
.disk_sectors
),
546 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
548 le32_to_cpu(header
.l1dir_size
),
550 le32_to_cpu(header
.granularity
),
556 ret
= vmdk_init_tables(bs
, extent
, errp
);
558 /* free extent allocated by vmdk_add_extent */
559 vmdk_free_last_extent(bs
);
564 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
565 QDict
*options
, Error
**errp
);
567 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
573 size
= bdrv_getlength(file
->bs
);
575 error_setg_errno(errp
, -size
, "Could not access file");
580 /* Both descriptor file and sparse image must be much larger than 4
581 * bytes, also callers of vmdk_read_desc want to compare the first 4
582 * bytes with VMDK4_MAGIC, let's error out if less is read. */
583 error_setg(errp
, "File is too small, not a valid image");
587 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
588 buf
= g_malloc(size
+ 1);
590 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
592 error_setg_errno(errp
, -ret
, "Could not read from file");
601 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
603 int flags
, QDict
*options
, Error
**errp
)
607 uint32_t l1_size
, l1_entry_sectors
;
610 BDRVVmdkState
*s
= bs
->opaque
;
611 int64_t l1_backup_offset
= 0;
614 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
616 bdrv_refresh_filename(file
->bs
);
617 error_setg_errno(errp
, -ret
,
618 "Could not read header from file '%s'",
622 if (header
.capacity
== 0) {
623 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
625 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
629 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
635 if (!s
->create_type
) {
636 s
->create_type
= g_strdup("monolithicSparse");
639 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
641 * The footer takes precedence over the header, so read it in. The
642 * footer starts at offset -1024 from the end: One sector for the
643 * footer, and another one for the end-of-stream marker.
650 uint8_t pad
[512 - 16];
651 } QEMU_PACKED footer_marker
;
655 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
661 uint8_t pad
[512 - 16];
662 } QEMU_PACKED eos_marker
;
663 } QEMU_PACKED footer
;
665 ret
= bdrv_pread(file
,
666 bs
->file
->bs
->total_sectors
* 512 - 1536,
667 &footer
, sizeof(footer
));
669 error_setg_errno(errp
, -ret
, "Failed to read footer");
673 /* Some sanity checks for the footer */
674 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
675 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
676 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
677 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
678 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
679 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
681 error_setg(errp
, "Invalid footer");
685 header
= footer
.header
;
689 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
690 if (le32_to_cpu(header
.version
) > 3) {
691 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
692 le32_to_cpu(header
.version
));
694 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
696 /* VMware KB 2064959 explains that version 3 added support for
697 * persistent changed block tracking (CBT), and backup software can
698 * read it as version=1 if it doesn't care about the changed area
699 * information. So we are safe to enable read only. */
700 error_setg(errp
, "VMDK version 3 must be read only");
704 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
705 error_setg(errp
, "L2 table size too big");
709 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
710 * le64_to_cpu(header
.granularity
);
711 if (l1_entry_sectors
== 0) {
712 error_setg(errp
, "L1 entry size is invalid");
715 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
717 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
718 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
720 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
721 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
722 (int64_t)(le64_to_cpu(header
.grain_offset
)
723 * BDRV_SECTOR_SIZE
));
727 ret
= vmdk_add_extent(bs
, file
, false,
728 le64_to_cpu(header
.capacity
),
729 le64_to_cpu(header
.gd_offset
) << 9,
732 le32_to_cpu(header
.num_gtes_per_gt
),
733 le64_to_cpu(header
.granularity
),
740 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
741 if (extent
->compressed
) {
742 g_free(s
->create_type
);
743 s
->create_type
= g_strdup("streamOptimized");
745 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
746 extent
->version
= le32_to_cpu(header
.version
);
747 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
748 ret
= vmdk_init_tables(bs
, extent
, errp
);
750 /* free extent allocated by vmdk_add_extent */
751 vmdk_free_last_extent(bs
);
756 /* find an option value out of descriptor file */
757 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
758 char *buf
, int buf_size
)
760 char *opt_pos
, *opt_end
;
761 const char *end
= desc
+ strlen(desc
);
763 opt_pos
= strstr(desc
, opt_name
);
767 /* Skip "=\"" following opt_name */
768 opt_pos
+= strlen(opt_name
) + 2;
769 if (opt_pos
>= end
) {
773 while (opt_end
< end
&& *opt_end
!= '"') {
776 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
779 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
783 /* Open an extent file and append to bs array */
784 static int vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
785 char *buf
, QDict
*options
, Error
**errp
)
789 magic
= ldl_be_p(buf
);
792 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
795 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
798 error_setg(errp
, "Image not in VMDK format");
804 static const char *next_line(const char *s
)
815 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
816 const char *desc_file_path
, QDict
*options
,
828 BdrvChild
*extent_file
;
829 BDRVVmdkState
*s
= bs
->opaque
;
831 char extent_opt_prefix
[32];
832 Error
*local_err
= NULL
;
834 for (p
= desc
; *p
; p
= next_line(p
)) {
835 /* parse extent line in one of below formats:
837 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
838 * RW [size in sectors] SPARSE "file-name.vmdk"
839 * RW [size in sectors] VMFS "file-name.vmdk"
840 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
843 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
844 access
, §ors
, type
, fname
, &flat_offset
);
845 if (matches
< 4 || strcmp(access
, "RW")) {
847 } else if (!strcmp(type
, "FLAT")) {
848 if (matches
!= 5 || flat_offset
< 0) {
851 } else if (!strcmp(type
, "VMFS")) {
857 } else if (matches
!= 4) {
862 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
863 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
864 (strcmp(access
, "RW"))) {
868 if (!path_is_absolute(fname
) && !path_has_protocol(fname
) &&
871 bdrv_refresh_filename(bs
->file
->bs
);
872 error_setg(errp
, "Cannot use relative extent paths with VMDK "
873 "descriptor file '%s'", bs
->file
->bs
->filename
);
877 extent_path
= path_combine(desc_file_path
, fname
);
879 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
882 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
883 bs
, &child_file
, false, &local_err
);
886 error_propagate(errp
, local_err
);
890 /* save to extents array */
891 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
894 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
895 0, 0, 0, 0, 0, &extent
, errp
);
897 bdrv_unref_child(bs
, extent_file
);
900 extent
->flat_start_offset
= flat_offset
<< 9;
901 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
902 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
903 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
907 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
912 bdrv_unref_child(bs
, extent_file
);
915 extent
= &s
->extents
[s
->num_extents
- 1];
917 error_setg(errp
, "Unsupported extent type '%s'", type
);
918 bdrv_unref_child(bs
, extent_file
);
921 extent
->type
= g_strdup(type
);
928 if (np
[-1] == '\n') {
931 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
935 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
936 QDict
*options
, Error
**errp
)
940 BDRVVmdkState
*s
= bs
->opaque
;
942 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
943 error_setg(errp
, "invalid VMDK image descriptor");
947 if (strcmp(ct
, "monolithicFlat") &&
948 strcmp(ct
, "vmfs") &&
949 strcmp(ct
, "vmfsSparse") &&
950 strcmp(ct
, "twoGbMaxExtentSparse") &&
951 strcmp(ct
, "twoGbMaxExtentFlat")) {
952 error_setg(errp
, "Unsupported image type '%s'", ct
);
956 s
->create_type
= g_strdup(ct
);
958 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->bs
->exact_filename
, options
,
964 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
969 BDRVVmdkState
*s
= bs
->opaque
;
971 Error
*local_err
= NULL
;
973 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
979 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
984 magic
= ldl_be_p(buf
);
988 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
990 s
->desc_offset
= 0x200;
993 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1000 /* try to open parent images, if exist */
1001 ret
= vmdk_parent_open(bs
);
1005 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1009 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1013 qemu_co_mutex_init(&s
->lock
);
1015 /* Disable migration when VMDK images are used */
1016 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1017 "does not support live migration",
1018 bdrv_get_device_or_node_name(bs
));
1019 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1021 error_propagate(errp
, local_err
);
1022 error_free(s
->migration_blocker
);
1031 g_free(s
->create_type
);
1032 s
->create_type
= NULL
;
1033 vmdk_free_extents(bs
);
1038 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1040 BDRVVmdkState
*s
= bs
->opaque
;
1043 for (i
= 0; i
< s
->num_extents
; i
++) {
1044 if (!s
->extents
[i
].flat
) {
1045 bs
->bl
.pwrite_zeroes_alignment
=
1046 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1047 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1055 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1056 * to the cluster at @cluster_sector_num.
1058 * If @skip_start_sector < @skip_end_sector, the relative range
1059 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1060 * it for call to write user data in the request.
1062 static int get_whole_cluster(BlockDriverState
*bs
,
1064 uint64_t cluster_offset
,
1066 uint64_t skip_start_bytes
,
1067 uint64_t skip_end_bytes
)
1070 int64_t cluster_bytes
;
1071 uint8_t *whole_grain
;
1073 /* For COW, align request sector_num to cluster start */
1074 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1075 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1076 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1079 memset(whole_grain
, 0, skip_start_bytes
);
1080 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1083 assert(skip_end_bytes
<= cluster_bytes
);
1084 /* we will be here if it's first write on non-exist grain(cluster).
1085 * try to read from parent image, if exist */
1086 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1091 /* Read backing data before skip range */
1092 if (skip_start_bytes
> 0) {
1094 /* qcow2 emits this on bs->file instead of bs->backing */
1095 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1096 ret
= bdrv_pread(bs
->backing
, offset
, whole_grain
,
1103 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1104 ret
= bdrv_pwrite(extent
->file
, cluster_offset
, whole_grain
,
1111 /* Read backing data after skip range */
1112 if (skip_end_bytes
< cluster_bytes
) {
1114 /* qcow2 emits this on bs->file instead of bs->backing */
1115 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1116 ret
= bdrv_pread(bs
->backing
, offset
+ skip_end_bytes
,
1117 whole_grain
+ skip_end_bytes
,
1118 cluster_bytes
- skip_end_bytes
);
1124 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1125 ret
= bdrv_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1126 whole_grain
+ skip_end_bytes
,
1127 cluster_bytes
- skip_end_bytes
);
1136 qemu_vfree(whole_grain
);
1140 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1143 offset
= cpu_to_le32(offset
);
1144 /* update L2 table */
1145 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1146 if (bdrv_pwrite_sync(extent
->file
,
1147 ((int64_t)m_data
->l2_offset
* 512)
1148 + (m_data
->l2_index
* sizeof(offset
)),
1149 &offset
, sizeof(offset
)) < 0) {
1152 /* update backup L2 table */
1153 if (extent
->l1_backup_table_offset
!= 0) {
1154 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1155 if (bdrv_pwrite_sync(extent
->file
,
1156 ((int64_t)m_data
->l2_offset
* 512)
1157 + (m_data
->l2_index
* sizeof(offset
)),
1158 &offset
, sizeof(offset
)) < 0) {
1162 if (m_data
->l2_cache_entry
) {
1163 *m_data
->l2_cache_entry
= offset
;
1170 * get_cluster_offset
1172 * Look up cluster offset in extent file by sector number, and store in
1175 * For flat extents, the start offset as parsed from the description file is
1178 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1179 * offset for a new cluster and update L2 cache. If there is a backing file,
1180 * COW is done before returning; otherwise, zeroes are written to the allocated
1181 * cluster. Both COW and zero writing skips the sector range
1182 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1183 * has new data to write there.
1185 * Returns: VMDK_OK if cluster exists and mapped in the image.
1186 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1187 * VMDK_ERROR if failed.
1189 static int get_cluster_offset(BlockDriverState
*bs
,
1191 VmdkMetaData
*m_data
,
1194 uint64_t *cluster_offset
,
1195 uint64_t skip_start_bytes
,
1196 uint64_t skip_end_bytes
)
1198 unsigned int l1_index
, l2_offset
, l2_index
;
1199 int min_index
, i
, j
;
1200 uint32_t min_count
, *l2_table
;
1201 bool zeroed
= false;
1203 int64_t cluster_sector
;
1209 *cluster_offset
= extent
->flat_start_offset
;
1213 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1214 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1215 if (l1_index
>= extent
->l1_size
) {
1218 l2_offset
= extent
->l1_table
[l1_index
];
1220 return VMDK_UNALLOC
;
1222 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1223 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1224 /* increment the hit count */
1225 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1226 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1227 extent
->l2_cache_counts
[j
] >>= 1;
1230 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
1234 /* not found: load a new entry in the least used one */
1236 min_count
= 0xffffffff;
1237 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1238 if (extent
->l2_cache_counts
[i
] < min_count
) {
1239 min_count
= extent
->l2_cache_counts
[i
];
1243 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1244 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1245 if (bdrv_pread(extent
->file
,
1246 (int64_t)l2_offset
* 512,
1248 extent
->l2_size
* sizeof(uint32_t)
1249 ) != extent
->l2_size
* sizeof(uint32_t)) {
1253 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1254 extent
->l2_cache_counts
[min_index
] = 1;
1256 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1257 cluster_sector
= le32_to_cpu(l2_table
[l2_index
]);
1259 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1263 if (!cluster_sector
|| zeroed
) {
1265 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1268 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1272 cluster_sector
= extent
->next_cluster_sector
;
1273 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1275 /* First of all we write grain itself, to avoid race condition
1276 * that may to corrupt the image.
1277 * This problem may occur because of insufficient space on host disk
1278 * or inappropriate VM shutdown.
1280 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1281 offset
, skip_start_bytes
, skip_end_bytes
);
1287 m_data
->l1_index
= l1_index
;
1288 m_data
->l2_index
= l2_index
;
1289 m_data
->l2_offset
= l2_offset
;
1290 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1293 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1297 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1298 int64_t sector_num
, VmdkExtent
*start_hint
)
1300 VmdkExtent
*extent
= start_hint
;
1303 extent
= &s
->extents
[0];
1305 while (extent
< &s
->extents
[s
->num_extents
]) {
1306 if (sector_num
< extent
->end_sector
) {
1314 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1317 uint64_t extent_begin_offset
, extent_relative_offset
;
1318 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1320 extent_begin_offset
=
1321 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1322 extent_relative_offset
= offset
- extent_begin_offset
;
1323 return extent_relative_offset
% cluster_size
;
1326 static int coroutine_fn
vmdk_co_block_status(BlockDriverState
*bs
,
1328 int64_t offset
, int64_t bytes
,
1329 int64_t *pnum
, int64_t *map
,
1330 BlockDriverState
**file
)
1332 BDRVVmdkState
*s
= bs
->opaque
;
1333 int64_t index_in_cluster
, n
, ret
;
1334 uint64_t cluster_offset
;
1337 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1341 qemu_co_mutex_lock(&s
->lock
);
1342 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1344 qemu_co_mutex_unlock(&s
->lock
);
1346 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1355 ret
= BDRV_BLOCK_ZERO
;
1358 ret
= BDRV_BLOCK_DATA
;
1359 if (!extent
->compressed
) {
1360 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1361 *map
= cluster_offset
+ index_in_cluster
;
1363 *file
= extent
->file
->bs
;
1367 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1368 *pnum
= MIN(n
, bytes
);
1372 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1373 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1374 uint64_t qiov_offset
, uint64_t n_bytes
,
1378 VmdkGrainMarker
*data
= NULL
;
1380 QEMUIOVector local_qiov
;
1381 int64_t write_offset
;
1382 int64_t write_end_sector
;
1384 if (extent
->compressed
) {
1385 void *compressed_data
;
1387 if (!extent
->has_marker
) {
1391 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1392 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1394 compressed_data
= g_malloc(n_bytes
);
1395 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1396 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1397 g_free(compressed_data
);
1399 if (ret
!= Z_OK
|| buf_len
== 0) {
1404 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1405 data
->size
= cpu_to_le32(buf_len
);
1407 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1408 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1410 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1412 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1413 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1415 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1418 write_offset
= cluster_offset
+ offset_in_cluster
;
1419 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1422 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1424 if (extent
->compressed
) {
1425 extent
->next_cluster_sector
= write_end_sector
;
1427 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1437 if (!extent
->compressed
) {
1438 qemu_iovec_destroy(&local_qiov
);
1443 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1444 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1448 int cluster_bytes
, buf_bytes
;
1449 uint8_t *cluster_buf
, *compressed_data
;
1450 uint8_t *uncomp_buf
;
1452 VmdkGrainMarker
*marker
;
1456 if (!extent
->compressed
) {
1457 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1458 ret
= bdrv_co_preadv(extent
->file
,
1459 cluster_offset
+ offset_in_cluster
, bytes
,
1466 cluster_bytes
= extent
->cluster_sectors
* 512;
1467 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1468 buf_bytes
= cluster_bytes
* 2;
1469 cluster_buf
= g_malloc(buf_bytes
);
1470 uncomp_buf
= g_malloc(cluster_bytes
);
1471 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1472 ret
= bdrv_pread(extent
->file
,
1474 cluster_buf
, buf_bytes
);
1478 compressed_data
= cluster_buf
;
1479 buf_len
= cluster_bytes
;
1480 data_len
= cluster_bytes
;
1481 if (extent
->has_marker
) {
1482 marker
= (VmdkGrainMarker
*)cluster_buf
;
1483 compressed_data
= marker
->data
;
1484 data_len
= le32_to_cpu(marker
->size
);
1486 if (!data_len
|| data_len
> buf_bytes
) {
1490 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1496 if (offset_in_cluster
< 0 ||
1497 offset_in_cluster
+ bytes
> buf_len
) {
1501 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1506 g_free(cluster_buf
);
1510 static int coroutine_fn
1511 vmdk_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1512 QEMUIOVector
*qiov
, int flags
)
1514 BDRVVmdkState
*s
= bs
->opaque
;
1516 uint64_t n_bytes
, offset_in_cluster
;
1517 VmdkExtent
*extent
= NULL
;
1518 QEMUIOVector local_qiov
;
1519 uint64_t cluster_offset
;
1520 uint64_t bytes_done
= 0;
1522 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1523 qemu_co_mutex_lock(&s
->lock
);
1526 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1531 ret
= get_cluster_offset(bs
, extent
, NULL
,
1532 offset
, false, &cluster_offset
, 0, 0);
1533 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1535 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1536 - offset_in_cluster
);
1538 if (ret
!= VMDK_OK
) {
1539 /* if not allocated, try to read from parent image, if exist */
1540 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1541 if (!vmdk_is_cid_valid(bs
)) {
1546 qemu_iovec_reset(&local_qiov
);
1547 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1549 /* qcow2 emits this on bs->file instead of bs->backing */
1550 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1551 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
1557 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
1560 qemu_iovec_reset(&local_qiov
);
1561 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1563 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
1564 &local_qiov
, n_bytes
);
1571 bytes_done
+= n_bytes
;
1576 qemu_co_mutex_unlock(&s
->lock
);
1577 qemu_iovec_destroy(&local_qiov
);
1584 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1585 * if possible, otherwise return -ENOTSUP.
1586 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1587 * with each cluster. By dry run we can find if the zero write
1588 * is possible without modifying image data.
1590 * Returns: error code with 0 for success.
1592 static int vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1593 uint64_t bytes
, QEMUIOVector
*qiov
,
1594 bool zeroed
, bool zero_dry_run
)
1596 BDRVVmdkState
*s
= bs
->opaque
;
1597 VmdkExtent
*extent
= NULL
;
1599 int64_t offset_in_cluster
, n_bytes
;
1600 uint64_t cluster_offset
;
1601 uint64_t bytes_done
= 0;
1602 VmdkMetaData m_data
;
1604 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
1605 error_report("Wrong offset: offset=0x%" PRIx64
1606 " total_sectors=0x%" PRIx64
,
1607 offset
, bs
->total_sectors
);
1612 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1616 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1617 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1618 - offset_in_cluster
);
1620 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1621 !(extent
->compressed
|| zeroed
),
1622 &cluster_offset
, offset_in_cluster
,
1623 offset_in_cluster
+ n_bytes
);
1624 if (extent
->compressed
) {
1625 if (ret
== VMDK_OK
) {
1626 /* Refuse write to allocated cluster for streamOptimized */
1627 error_report("Could not write to allocated cluster"
1628 " for streamOptimized");
1632 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
1633 true, &cluster_offset
, 0, 0);
1636 if (ret
== VMDK_ERROR
) {
1640 /* Do zeroed write, buf is ignored */
1641 if (extent
->has_zero_grain
&&
1642 offset_in_cluster
== 0 &&
1643 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
1644 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1645 if (!zero_dry_run
) {
1646 /* update L2 tables */
1647 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
1656 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
1657 qiov
, bytes_done
, n_bytes
, offset
);
1662 /* update L2 tables */
1663 if (vmdk_L2update(extent
, &m_data
,
1664 cluster_offset
>> BDRV_SECTOR_BITS
)
1672 bytes_done
+= n_bytes
;
1674 /* update CID on the first write every time the virtual disk is
1676 if (!s
->cid_updated
) {
1677 ret
= vmdk_write_cid(bs
, g_random_int());
1681 s
->cid_updated
= true;
1687 static int coroutine_fn
1688 vmdk_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1689 QEMUIOVector
*qiov
, int flags
)
1692 BDRVVmdkState
*s
= bs
->opaque
;
1693 qemu_co_mutex_lock(&s
->lock
);
1694 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
1695 qemu_co_mutex_unlock(&s
->lock
);
1699 static int coroutine_fn
1700 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
1701 uint64_t bytes
, QEMUIOVector
*qiov
)
1704 /* The caller will write bytes 0 to signal EOF.
1705 * When receive it, we align EOF to a sector boundary. */
1706 BDRVVmdkState
*s
= bs
->opaque
;
1710 for (i
= 0; i
< s
->num_extents
; i
++) {
1711 length
= bdrv_getlength(s
->extents
[i
].file
->bs
);
1715 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
1716 ret
= bdrv_truncate(s
->extents
[i
].file
, length
,
1717 PREALLOC_MODE_OFF
, NULL
);
1724 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
1727 static int coroutine_fn
vmdk_co_pwrite_zeroes(BlockDriverState
*bs
,
1730 BdrvRequestFlags flags
)
1733 BDRVVmdkState
*s
= bs
->opaque
;
1735 qemu_co_mutex_lock(&s
->lock
);
1736 /* write zeroes could fail if sectors not aligned to cluster, test it with
1737 * dry_run == true before really updating image */
1738 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
1740 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
1742 qemu_co_mutex_unlock(&s
->lock
);
1746 static int vmdk_init_extent(BlockBackend
*blk
,
1747 int64_t filesize
, bool flat
,
1748 bool compress
, bool zeroed_grain
,
1753 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
1754 uint32_t *gd_buf
= NULL
;
1758 ret
= blk_truncate(blk
, filesize
, PREALLOC_MODE_OFF
, errp
);
1761 magic
= cpu_to_be32(VMDK4_MAGIC
);
1762 memset(&header
, 0, sizeof(header
));
1765 } else if (zeroed_grain
) {
1770 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1771 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1772 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1773 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1774 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
1775 header
.granularity
= 128;
1776 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
1778 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
1779 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
1781 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
1782 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
1784 header
.desc_offset
= 1;
1785 header
.desc_size
= 20;
1786 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1787 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
1788 header
.grain_offset
=
1789 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
1790 header
.granularity
);
1791 /* swap endianness for all header fields */
1792 header
.version
= cpu_to_le32(header
.version
);
1793 header
.flags
= cpu_to_le32(header
.flags
);
1794 header
.capacity
= cpu_to_le64(header
.capacity
);
1795 header
.granularity
= cpu_to_le64(header
.granularity
);
1796 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1797 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1798 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1799 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1800 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1801 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1802 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1804 header
.check_bytes
[0] = 0xa;
1805 header
.check_bytes
[1] = 0x20;
1806 header
.check_bytes
[2] = 0xd;
1807 header
.check_bytes
[3] = 0xa;
1809 /* write all the data */
1810 ret
= blk_pwrite(blk
, 0, &magic
, sizeof(magic
), 0);
1812 error_setg(errp
, QERR_IO_ERROR
);
1815 ret
= blk_pwrite(blk
, sizeof(magic
), &header
, sizeof(header
), 0);
1817 error_setg(errp
, QERR_IO_ERROR
);
1821 ret
= blk_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9,
1822 PREALLOC_MODE_OFF
, errp
);
1827 /* write grain directory */
1828 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
1829 gd_buf
= g_malloc0(gd_buf_size
);
1830 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
1831 i
< gt_count
; i
++, tmp
+= gt_size
) {
1832 gd_buf
[i
] = cpu_to_le32(tmp
);
1834 ret
= blk_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
1835 gd_buf
, gd_buf_size
, 0);
1837 error_setg(errp
, QERR_IO_ERROR
);
1841 /* write backup grain directory */
1842 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
1843 i
< gt_count
; i
++, tmp
+= gt_size
) {
1844 gd_buf
[i
] = cpu_to_le32(tmp
);
1846 ret
= blk_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
1847 gd_buf
, gd_buf_size
, 0);
1849 error_setg(errp
, QERR_IO_ERROR
);
1858 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1859 bool flat
, bool compress
, bool zeroed_grain
,
1861 QemuOpts
*opts
, Error
**errp
)
1864 BlockBackend
*blk
= NULL
;
1865 Error
*local_err
= NULL
;
1867 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1869 error_propagate(errp
, local_err
);
1873 blk
= blk_new_open(filename
, NULL
, NULL
,
1874 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
1877 error_propagate(errp
, local_err
);
1882 blk_set_allow_write_beyond_eof(blk
, true);
1884 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
1897 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1898 char *postfix
, size_t buf_len
, Error
**errp
)
1902 if (filename
== NULL
|| !strlen(filename
)) {
1903 error_setg(errp
, "No filename provided");
1906 p
= strrchr(filename
, '/');
1908 p
= strrchr(filename
, '\\');
1911 p
= strrchr(filename
, ':');
1915 if (p
- filename
>= buf_len
) {
1918 pstrcpy(path
, p
- filename
+ 1, filename
);
1923 q
= strrchr(p
, '.');
1925 pstrcpy(prefix
, buf_len
, p
);
1928 if (q
- p
>= buf_len
) {
1931 pstrcpy(prefix
, q
- p
+ 1, p
);
1932 pstrcpy(postfix
, buf_len
, q
);
1938 * idx == 0: get or create the descriptor file (also the image file if in a
1940 * idx >= 1: get the n-th extent if in a split subformat
1942 typedef BlockBackend
*(*vmdk_create_extent_fn
)(int64_t size
,
1951 static void vmdk_desc_add_extent(GString
*desc
,
1952 const char *extent_line_fmt
,
1953 int64_t size
, const char *filename
)
1955 char *basename
= g_path_get_basename(filename
);
1957 g_string_append_printf(desc
, extent_line_fmt
,
1958 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
1962 static int coroutine_fn
vmdk_co_do_create(int64_t size
,
1963 BlockdevVmdkSubformat subformat
,
1964 BlockdevVmdkAdapterType adapter_type
,
1965 const char *backing_file
,
1966 const char *hw_version
,
1969 vmdk_create_extent_fn extent_fn
,
1974 BlockBackend
*blk
= NULL
;
1975 BlockBackend
*extent_blk
;
1976 Error
*local_err
= NULL
;
1979 bool flat
, split
, compress
;
1980 GString
*ext_desc_lines
;
1981 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1982 int64_t extent_size
;
1983 int64_t created_size
= 0;
1984 const char *extent_line_fmt
;
1985 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
1986 uint32_t parent_cid
= 0xffffffff;
1987 uint32_t number_heads
= 16;
1988 uint32_t desc_offset
= 0, desc_len
;
1989 const char desc_template
[] =
1990 "# Disk DescriptorFile\n"
1993 "parentCID=%" PRIx32
"\n"
1994 "createType=\"%s\"\n"
1997 "# Extent description\n"
2000 "# The Disk Data Base\n"
2003 "ddb.virtualHWVersion = \"%s\"\n"
2004 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2005 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2006 "ddb.geometry.sectors = \"63\"\n"
2007 "ddb.adapterType = \"%s\"\n";
2009 ext_desc_lines
= g_string_new(NULL
);
2011 /* Read out options */
2015 "compat6 cannot be enabled with hwversion set");
2025 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2026 /* that's the number of heads with which vmware operates when
2027 creating, exporting, etc. vmdk files with a non-ide adapter type */
2030 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2031 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2032 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2033 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2034 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2037 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2039 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2041 if (flat
&& backing_file
) {
2042 error_setg(errp
, "Flat image can't have backing file");
2046 if (flat
&& zeroed_grain
) {
2047 error_setg(errp
, "Flat image can't enable zeroed grain");
2052 /* Create extents */
2054 extent_size
= split_size
;
2058 if (!split
&& !flat
) {
2059 created_size
= extent_size
;
2063 /* Get the descriptor file BDS */
2064 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2070 if (!split
&& !flat
) {
2071 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2072 blk_bs(blk
)->filename
);
2076 BlockBackend
*backing
;
2077 char *full_backing
=
2078 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2082 error_propagate(errp
, local_err
);
2086 assert(full_backing
);
2088 backing
= blk_new_open(full_backing
, NULL
, NULL
,
2089 BDRV_O_NO_BACKING
, errp
);
2090 g_free(full_backing
);
2091 if (backing
== NULL
) {
2095 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2096 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2097 blk_bs(backing
)->drv
->format_name
);
2102 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2105 error_setg(errp
, "Failed to read parent CID");
2108 snprintf(parent_desc_line
, BUF_SIZE
,
2109 "parentFileNameHint=\"%s\"", backing_file
);
2112 while (created_size
< size
) {
2113 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2114 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2115 zeroed_grain
, opaque
, errp
);
2120 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2121 blk_bs(extent_blk
)->filename
);
2122 created_size
+= cur_size
;
2124 blk_unref(extent_blk
);
2127 /* Check whether we got excess extents */
2128 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2131 blk_unref(extent_blk
);
2132 error_setg(errp
, "List of extents contains unused extents");
2137 /* generate descriptor file */
2138 desc
= g_strdup_printf(desc_template
,
2141 BlockdevVmdkSubformat_str(subformat
),
2143 ext_desc_lines
->str
,
2146 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2148 BlockdevVmdkAdapterType_str(adapter_type
));
2149 desc_len
= strlen(desc
);
2150 /* the descriptor offset = 0x200 */
2151 if (!split
&& !flat
) {
2152 desc_offset
= 0x200;
2155 ret
= blk_pwrite(blk
, desc_offset
, desc
, desc_len
, 0);
2157 error_setg_errno(errp
, -ret
, "Could not write description");
2160 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2161 * for description file */
2162 if (desc_offset
== 0) {
2163 ret
= blk_truncate(blk
, desc_len
, PREALLOC_MODE_OFF
, errp
);
2174 g_free(parent_desc_line
);
2175 g_string_free(ext_desc_lines
, true);
2184 } VMDKCreateOptsData
;
2186 static BlockBackend
*vmdk_co_create_opts_cb(int64_t size
, int idx
,
2187 bool flat
, bool split
, bool compress
,
2188 bool zeroed_grain
, void *opaque
,
2191 BlockBackend
*blk
= NULL
;
2192 BlockDriverState
*bs
= NULL
;
2193 VMDKCreateOptsData
*data
= opaque
;
2194 char *ext_filename
= NULL
;
2195 char *rel_filename
= NULL
;
2197 /* We're done, don't create excess extents. */
2199 assert(errp
== NULL
);
2204 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2206 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2208 flat
? 'f' : 's', idx
, data
->postfix
);
2211 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2214 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2215 g_free(rel_filename
);
2217 if (vmdk_create_extent(ext_filename
, size
,
2218 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2224 g_free(ext_filename
);
2228 static int coroutine_fn
vmdk_co_create_opts(const char *filename
, QemuOpts
*opts
,
2231 Error
*local_err
= NULL
;
2233 int64_t total_size
= 0;
2234 char *adapter_type
= NULL
;
2235 BlockdevVmdkAdapterType adapter_type_enum
;
2236 char *backing_file
= NULL
;
2237 char *hw_version
= NULL
;
2239 BlockdevVmdkSubformat subformat
;
2241 char *path
= g_malloc0(PATH_MAX
);
2242 char *prefix
= g_malloc0(PATH_MAX
);
2243 char *postfix
= g_malloc0(PATH_MAX
);
2244 char *desc_line
= g_malloc0(BUF_SIZE
);
2245 char *ext_filename
= g_malloc0(PATH_MAX
);
2246 char *desc_filename
= g_malloc0(PATH_MAX
);
2247 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2250 VMDKCreateOptsData data
;
2252 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2256 /* Read out options */
2257 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2259 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2260 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2261 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2262 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2263 if (strcmp(hw_version
, "undefined") == 0) {
2267 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2268 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2271 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2273 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2276 error_propagate(errp
, local_err
);
2281 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2285 /* Default format to monolithicSparse */
2286 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2288 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2290 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2293 error_propagate(errp
, local_err
);
2298 data
= (VMDKCreateOptsData
){
2304 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2305 backing_file
, hw_version
, compat6
, zeroed_grain
,
2306 vmdk_co_create_opts_cb
, &data
, errp
);
2309 g_free(adapter_type
);
2310 g_free(backing_file
);
2318 g_free(ext_filename
);
2319 g_free(desc_filename
);
2320 g_free(parent_desc_line
);
2324 static BlockBackend
*vmdk_co_create_cb(int64_t size
, int idx
,
2325 bool flat
, bool split
, bool compress
,
2326 bool zeroed_grain
, void *opaque
,
2330 BlockDriverState
*bs
;
2332 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2335 bs
= bdrv_open_blockdev_ref(opts
->file
, errp
);
2338 BlockdevRefList
*list
= opts
->extents
;
2339 for (i
= 1; i
< idx
; i
++) {
2340 if (!list
|| !list
->next
) {
2341 error_setg(errp
, "Extent [%d] not specified", i
);
2347 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2350 bs
= bdrv_open_blockdev_ref(list
->value
, errp
);
2355 blk
= blk_new(BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
2357 if (blk_insert_bs(blk
, bs
, errp
)) {
2361 blk_set_allow_write_beyond_eof(blk
, true);
2365 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2374 static int coroutine_fn
vmdk_co_create(BlockdevCreateOptions
*create_options
,
2378 BlockdevCreateOptionsVmdk
*opts
;
2380 opts
= &create_options
->u
.vmdk
;
2382 /* Validate options */
2383 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2384 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2389 ret
= vmdk_co_do_create(opts
->size
,
2404 static void vmdk_close(BlockDriverState
*bs
)
2406 BDRVVmdkState
*s
= bs
->opaque
;
2408 vmdk_free_extents(bs
);
2409 g_free(s
->create_type
);
2411 migrate_del_blocker(s
->migration_blocker
);
2412 error_free(s
->migration_blocker
);
2415 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
2417 BDRVVmdkState
*s
= bs
->opaque
;
2421 for (i
= 0; i
< s
->num_extents
; i
++) {
2422 err
= bdrv_co_flush(s
->extents
[i
].file
->bs
);
2430 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2435 BDRVVmdkState
*s
= bs
->opaque
;
2437 ret
= bdrv_get_allocated_file_size(bs
->file
->bs
);
2441 for (i
= 0; i
< s
->num_extents
; i
++) {
2442 if (s
->extents
[i
].file
== bs
->file
) {
2445 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2454 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2457 BDRVVmdkState
*s
= bs
->opaque
;
2459 /* If has a flat extent and its underlying storage doesn't have zero init,
2461 for (i
= 0; i
< s
->num_extents
; i
++) {
2462 if (s
->extents
[i
].flat
) {
2463 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2471 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2473 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2475 bdrv_refresh_filename(extent
->file
->bs
);
2476 *info
= (ImageInfo
){
2477 .filename
= g_strdup(extent
->file
->bs
->filename
),
2478 .format
= g_strdup(extent
->type
),
2479 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2480 .compressed
= extent
->compressed
,
2481 .has_compressed
= extent
->compressed
,
2482 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2483 .has_cluster_size
= !extent
->flat
,
2489 static int coroutine_fn
vmdk_co_check(BlockDriverState
*bs
,
2490 BdrvCheckResult
*result
,
2493 BDRVVmdkState
*s
= bs
->opaque
;
2494 VmdkExtent
*extent
= NULL
;
2495 int64_t sector_num
= 0;
2496 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2498 uint64_t cluster_offset
;
2505 if (sector_num
>= total_sectors
) {
2508 extent
= find_extent(s
, sector_num
, extent
);
2511 "ERROR: could not find extent for sector %" PRId64
"\n",
2516 ret
= get_cluster_offset(bs
, extent
, NULL
,
2517 sector_num
<< BDRV_SECTOR_BITS
,
2518 false, &cluster_offset
, 0, 0);
2519 if (ret
== VMDK_ERROR
) {
2521 "ERROR: could not get cluster_offset for sector %"
2522 PRId64
"\n", sector_num
);
2525 if (ret
== VMDK_OK
) {
2526 int64_t extent_len
= bdrv_getlength(extent
->file
->bs
);
2527 if (extent_len
< 0) {
2529 "ERROR: could not get extent file length for sector %"
2530 PRId64
"\n", sector_num
);
2534 if (cluster_offset
>= extent_len
) {
2536 "ERROR: cluster offset for sector %"
2537 PRId64
" points after EOF\n", sector_num
);
2542 sector_num
+= extent
->cluster_sectors
;
2545 result
->corruptions
++;
2549 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
,
2553 BDRVVmdkState
*s
= bs
->opaque
;
2554 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2555 ImageInfoList
**next
;
2557 *spec_info
= (ImageInfoSpecific
){
2558 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2560 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
2564 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
2565 .create_type
= g_strdup(s
->create_type
),
2567 .parent_cid
= s
->parent_cid
,
2570 next
= &spec_info
->u
.vmdk
.data
->extents
;
2571 for (i
= 0; i
< s
->num_extents
; i
++) {
2572 *next
= g_new0(ImageInfoList
, 1);
2573 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2574 (*next
)->next
= NULL
;
2575 next
= &(*next
)->next
;
2581 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
2583 return a
->flat
== b
->flat
&&
2584 a
->compressed
== b
->compressed
&&
2585 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
2588 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2591 BDRVVmdkState
*s
= bs
->opaque
;
2592 assert(s
->num_extents
);
2594 /* See if we have multiple extents but they have different cases */
2595 for (i
= 1; i
< s
->num_extents
; i
++) {
2596 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
2600 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2601 if (!s
->extents
[0].flat
) {
2602 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2607 static void vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
2608 bool backing_overridden
)
2610 /* No children but file and backing can be explicitly specified (TODO) */
2611 qdict_put(target
, "file",
2612 qobject_ref(bs
->file
->bs
->full_open_options
));
2614 if (backing_overridden
) {
2616 qdict_put(target
, "backing",
2617 qobject_ref(bs
->backing
->bs
->full_open_options
));
2619 qdict_put_null(target
, "backing");
2624 static QemuOptsList vmdk_create_opts
= {
2625 .name
= "vmdk-create-opts",
2626 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
2629 .name
= BLOCK_OPT_SIZE
,
2630 .type
= QEMU_OPT_SIZE
,
2631 .help
= "Virtual disk size"
2634 .name
= BLOCK_OPT_ADAPTER_TYPE
,
2635 .type
= QEMU_OPT_STRING
,
2636 .help
= "Virtual adapter type, can be one of "
2637 "ide (default), lsilogic, buslogic or legacyESX"
2640 .name
= BLOCK_OPT_BACKING_FILE
,
2641 .type
= QEMU_OPT_STRING
,
2642 .help
= "File name of a base image"
2645 .name
= BLOCK_OPT_COMPAT6
,
2646 .type
= QEMU_OPT_BOOL
,
2647 .help
= "VMDK version 6 image",
2648 .def_value_str
= "off"
2651 .name
= BLOCK_OPT_HWVERSION
,
2652 .type
= QEMU_OPT_STRING
,
2653 .help
= "VMDK hardware version",
2654 .def_value_str
= "undefined"
2657 .name
= BLOCK_OPT_SUBFMT
,
2658 .type
= QEMU_OPT_STRING
,
2660 "VMDK flat extent format, can be one of "
2661 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2664 .name
= BLOCK_OPT_ZEROED_GRAIN
,
2665 .type
= QEMU_OPT_BOOL
,
2666 .help
= "Enable efficient zero writes "
2667 "using the zeroed-grain GTE feature"
2669 { /* end of list */ }
2673 static BlockDriver bdrv_vmdk
= {
2674 .format_name
= "vmdk",
2675 .instance_size
= sizeof(BDRVVmdkState
),
2676 .bdrv_probe
= vmdk_probe
,
2677 .bdrv_open
= vmdk_open
,
2678 .bdrv_co_check
= vmdk_co_check
,
2679 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
2680 .bdrv_child_perm
= bdrv_format_default_perms
,
2681 .bdrv_co_preadv
= vmdk_co_preadv
,
2682 .bdrv_co_pwritev
= vmdk_co_pwritev
,
2683 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
2684 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
2685 .bdrv_close
= vmdk_close
,
2686 .bdrv_co_create_opts
= vmdk_co_create_opts
,
2687 .bdrv_co_create
= vmdk_co_create
,
2688 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
2689 .bdrv_co_block_status
= vmdk_co_block_status
,
2690 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
2691 .bdrv_has_zero_init
= vmdk_has_zero_init
,
2692 .bdrv_get_specific_info
= vmdk_get_specific_info
,
2693 .bdrv_refresh_limits
= vmdk_refresh_limits
,
2694 .bdrv_get_info
= vmdk_get_info
,
2695 .bdrv_gather_child_options
= vmdk_gather_child_options
,
2697 .supports_backing
= true,
2698 .create_opts
= &vmdk_create_opts
,
2701 static void bdrv_vmdk_init(void)
2703 bdrv_register(&bdrv_vmdk
);
2706 block_init(bdrv_vmdk_init
);