2 * Block driver for the VMDK format
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "migration/migration.h"
32 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
33 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
34 #define VMDK4_COMPRESSION_DEFLATE 1
35 #define VMDK4_FLAG_NL_DETECT (1 << 0)
36 #define VMDK4_FLAG_RGD (1 << 1)
37 /* Zeroed-grain enable bit */
38 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
39 #define VMDK4_FLAG_COMPRESS (1 << 16)
40 #define VMDK4_FLAG_MARKER (1 << 17)
41 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
43 #define VMDK_GTE_ZEROED 0x1
45 /* VMDK internal error codes */
47 #define VMDK_ERROR (-1)
48 /* Cluster not allocated */
49 #define VMDK_UNALLOC (-2)
50 #define VMDK_ZEROED (-3)
52 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
57 uint32_t disk_sectors
;
59 uint32_t l1dir_offset
;
61 uint32_t file_sectors
;
64 uint32_t sectors_per_track
;
65 } QEMU_PACKED VMDK3Header
;
74 /* Number of GrainTableEntries per GrainTable */
75 uint32_t num_gtes_per_gt
;
78 uint64_t grain_offset
;
81 uint16_t compressAlgorithm
;
82 } QEMU_PACKED VMDK4Header
;
84 #define L2_CACHE_SIZE 16
86 typedef struct VmdkExtent
{
87 BlockDriverState
*file
;
95 int64_t flat_start_offset
;
96 int64_t l1_table_offset
;
97 int64_t l1_backup_table_offset
;
99 uint32_t *l1_backup_table
;
100 unsigned int l1_size
;
101 uint32_t l1_entry_sectors
;
103 unsigned int l2_size
;
105 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
106 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
108 int64_t cluster_sectors
;
109 int64_t next_cluster_sector
;
113 typedef struct BDRVVmdkState
{
115 uint64_t desc_offset
;
121 /* Extent array with num_extents entries, ascend ordered by address */
123 Error
*migration_blocker
;
127 typedef struct VmdkMetaData
{
128 unsigned int l1_index
;
129 unsigned int l2_index
;
130 unsigned int l2_offset
;
132 uint32_t *l2_cache_entry
;
135 typedef struct VmdkGrainMarker
{
139 } QEMU_PACKED VmdkGrainMarker
;
142 MARKER_END_OF_STREAM
= 0,
143 MARKER_GRAIN_TABLE
= 1,
144 MARKER_GRAIN_DIRECTORY
= 2,
148 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
155 magic
= be32_to_cpu(*(uint32_t *)buf
);
156 if (magic
== VMDK3_MAGIC
||
157 magic
== VMDK4_MAGIC
) {
160 const char *p
= (const char *)buf
;
161 const char *end
= p
+ buf_size
;
164 /* skip comment line */
165 while (p
< end
&& *p
!= '\n') {
172 while (p
< end
&& *p
== ' ') {
175 /* skip '\r' if windows line endings used. */
176 if (p
< end
&& *p
== '\r') {
179 /* only accept blank lines before 'version=' line */
180 if (p
== end
|| *p
!= '\n') {
186 if (end
- p
>= strlen("version=X\n")) {
187 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
188 strncmp("version=2\n", p
, strlen("version=2\n")) == 0) {
192 if (end
- p
>= strlen("version=X\r\n")) {
193 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
194 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
204 #define SECTOR_SIZE 512
205 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
206 #define BUF_SIZE 4096
207 #define HEADER_SIZE 512 /* first sector of 512 bytes */
209 static void vmdk_free_extents(BlockDriverState
*bs
)
212 BDRVVmdkState
*s
= bs
->opaque
;
215 for (i
= 0; i
< s
->num_extents
; i
++) {
219 g_free(e
->l1_backup_table
);
221 if (e
->file
!= bs
->file
) {
228 static void vmdk_free_last_extent(BlockDriverState
*bs
)
230 BDRVVmdkState
*s
= bs
->opaque
;
232 if (s
->num_extents
== 0) {
236 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
239 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
241 char desc
[DESC_SIZE
];
242 uint32_t cid
= 0xffffffff;
243 const char *p_name
, *cid_str
;
245 BDRVVmdkState
*s
= bs
->opaque
;
248 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
254 cid_str
= "parentCID";
255 cid_str_size
= sizeof("parentCID");
258 cid_str_size
= sizeof("CID");
261 desc
[DESC_SIZE
- 1] = '\0';
262 p_name
= strstr(desc
, cid_str
);
263 if (p_name
!= NULL
) {
264 p_name
+= cid_str_size
;
265 sscanf(p_name
, "%" SCNx32
, &cid
);
271 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
273 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
274 char *p_name
, *tmp_str
;
275 BDRVVmdkState
*s
= bs
->opaque
;
278 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
283 desc
[DESC_SIZE
- 1] = '\0';
284 tmp_str
= strstr(desc
, "parentCID");
285 if (tmp_str
== NULL
) {
289 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
290 p_name
= strstr(desc
, "CID");
291 if (p_name
!= NULL
) {
292 p_name
+= sizeof("CID");
293 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%" PRIx32
"\n", cid
);
294 pstrcat(desc
, sizeof(desc
), tmp_desc
);
297 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
305 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
307 BDRVVmdkState
*s
= bs
->opaque
;
308 BlockDriverState
*p_bs
= bs
->backing_hd
;
311 if (!s
->cid_checked
&& p_bs
) {
312 cur_pcid
= vmdk_read_cid(p_bs
, 0);
313 if (s
->parent_cid
!= cur_pcid
) {
318 s
->cid_checked
= true;
323 /* Queue extents, if any, for reopen() */
324 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
325 BlockReopenQueue
*queue
, Error
**errp
)
332 assert(state
!= NULL
);
333 assert(state
->bs
!= NULL
);
336 error_setg(errp
, "No reopen queue for VMDK extents");
340 s
= state
->bs
->opaque
;
344 for (i
= 0; i
< s
->num_extents
; i
++) {
346 if (e
->file
!= state
->bs
->file
) {
347 bdrv_reopen_queue(queue
, e
->file
, state
->flags
);
356 static int vmdk_parent_open(BlockDriverState
*bs
)
359 char desc
[DESC_SIZE
+ 1];
360 BDRVVmdkState
*s
= bs
->opaque
;
363 desc
[DESC_SIZE
] = '\0';
364 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
369 p_name
= strstr(desc
, "parentFileNameHint");
370 if (p_name
!= NULL
) {
373 p_name
+= sizeof("parentFileNameHint") + 1;
374 end_name
= strchr(p_name
, '\"');
375 if (end_name
== NULL
) {
378 if ((end_name
- p_name
) > sizeof(bs
->backing_file
) - 1) {
382 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
388 /* Create and append extent to the extent array. Return the added VmdkExtent
389 * address. return NULL if allocation failed. */
390 static int vmdk_add_extent(BlockDriverState
*bs
,
391 BlockDriverState
*file
, bool flat
, int64_t sectors
,
392 int64_t l1_offset
, int64_t l1_backup_offset
,
394 int l2_size
, uint64_t cluster_sectors
,
395 VmdkExtent
**new_extent
,
399 BDRVVmdkState
*s
= bs
->opaque
;
402 if (cluster_sectors
> 0x200000) {
403 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
404 error_setg(errp
, "Invalid granularity, image may be corrupt");
407 if (l1_size
> 512 * 1024 * 1024) {
408 /* Although with big capacity and small l1_entry_sectors, we can get a
409 * big l1_size, we don't want unbounded value to allocate the table.
410 * Limit it to 512M, which is 16PB for default cluster and L2 table
412 error_setg(errp
, "L1 size too big");
416 length
= bdrv_getlength(file
);
421 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
422 extent
= &s
->extents
[s
->num_extents
];
425 memset(extent
, 0, sizeof(VmdkExtent
));
428 extent
->sectors
= sectors
;
429 extent
->l1_table_offset
= l1_offset
;
430 extent
->l1_backup_table_offset
= l1_backup_offset
;
431 extent
->l1_size
= l1_size
;
432 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
433 extent
->l2_size
= l2_size
;
434 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
435 extent
->next_cluster_sector
=
436 ROUND_UP(DIV_ROUND_UP(length
, BDRV_SECTOR_SIZE
), cluster_sectors
);
438 if (s
->num_extents
> 1) {
439 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
441 extent
->end_sector
= extent
->sectors
;
443 bs
->total_sectors
= extent
->end_sector
;
445 *new_extent
= extent
;
450 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
456 /* read the L1 table */
457 l1_size
= extent
->l1_size
* sizeof(uint32_t);
458 extent
->l1_table
= g_try_malloc(l1_size
);
459 if (l1_size
&& extent
->l1_table
== NULL
) {
463 ret
= bdrv_pread(extent
->file
,
464 extent
->l1_table_offset
,
468 error_setg_errno(errp
, -ret
,
469 "Could not read l1 table from extent '%s'",
470 extent
->file
->filename
);
473 for (i
= 0; i
< extent
->l1_size
; i
++) {
474 le32_to_cpus(&extent
->l1_table
[i
]);
477 if (extent
->l1_backup_table_offset
) {
478 extent
->l1_backup_table
= g_try_malloc(l1_size
);
479 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
483 ret
= bdrv_pread(extent
->file
,
484 extent
->l1_backup_table_offset
,
485 extent
->l1_backup_table
,
488 error_setg_errno(errp
, -ret
,
489 "Could not read l1 backup table from extent '%s'",
490 extent
->file
->filename
);
493 for (i
= 0; i
< extent
->l1_size
; i
++) {
494 le32_to_cpus(&extent
->l1_backup_table
[i
]);
499 g_new(uint32_t, extent
->l2_size
* L2_CACHE_SIZE
);
502 g_free(extent
->l1_backup_table
);
504 g_free(extent
->l1_table
);
508 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
509 BlockDriverState
*file
,
510 int flags
, Error
**errp
)
517 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
519 error_setg_errno(errp
, -ret
,
520 "Could not read header from file '%s'",
524 ret
= vmdk_add_extent(bs
, file
, false,
525 le32_to_cpu(header
.disk_sectors
),
526 le32_to_cpu(header
.l1dir_offset
) << 9,
528 le32_to_cpu(header
.l1dir_size
),
530 le32_to_cpu(header
.granularity
),
536 ret
= vmdk_init_tables(bs
, extent
, errp
);
538 /* free extent allocated by vmdk_add_extent */
539 vmdk_free_last_extent(bs
);
544 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
547 static char *vmdk_read_desc(BlockDriverState
*file
, uint64_t desc_offset
,
554 size
= bdrv_getlength(file
);
556 error_setg_errno(errp
, -size
, "Could not access file");
560 size
= MIN(size
, 1 << 20); /* avoid unbounded allocation */
561 buf
= g_malloc0(size
+ 1);
563 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
565 error_setg_errno(errp
, -ret
, "Could not read from file");
573 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
574 BlockDriverState
*file
,
575 int flags
, Error
**errp
)
579 uint32_t l1_size
, l1_entry_sectors
;
582 BDRVVmdkState
*s
= bs
->opaque
;
583 int64_t l1_backup_offset
= 0;
585 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
587 error_setg_errno(errp
, -ret
,
588 "Could not read header from file '%s'",
592 if (header
.capacity
== 0) {
593 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
595 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
599 ret
= vmdk_open_desc_file(bs
, flags
, buf
, errp
);
605 if (!s
->create_type
) {
606 s
->create_type
= g_strdup("monolithicSparse");
609 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
611 * The footer takes precedence over the header, so read it in. The
612 * footer starts at offset -1024 from the end: One sector for the
613 * footer, and another one for the end-of-stream marker.
620 uint8_t pad
[512 - 16];
621 } QEMU_PACKED footer_marker
;
625 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
631 uint8_t pad
[512 - 16];
632 } QEMU_PACKED eos_marker
;
633 } QEMU_PACKED footer
;
635 ret
= bdrv_pread(file
,
636 bs
->file
->total_sectors
* 512 - 1536,
637 &footer
, sizeof(footer
));
642 /* Some sanity checks for the footer */
643 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
644 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
645 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
646 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
647 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
648 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
653 header
= footer
.header
;
656 if (le32_to_cpu(header
.version
) > 3) {
658 snprintf(buf
, sizeof(buf
), "VMDK version %" PRId32
,
659 le32_to_cpu(header
.version
));
660 error_set(errp
, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
661 bs
->device_name
, "vmdk", buf
);
663 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
)) {
664 /* VMware KB 2064959 explains that version 3 added support for
665 * persistent changed block tracking (CBT), and backup software can
666 * read it as version=1 if it doesn't care about the changed area
667 * information. So we are safe to enable read only. */
668 error_setg(errp
, "VMDK version 3 must be read only");
672 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
673 error_setg(errp
, "L2 table size too big");
677 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
678 * le64_to_cpu(header
.granularity
);
679 if (l1_entry_sectors
== 0) {
682 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
684 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
685 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
687 if (bdrv_nb_sectors(file
) < le64_to_cpu(header
.grain_offset
)) {
688 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
689 (int64_t)(le64_to_cpu(header
.grain_offset
)
690 * BDRV_SECTOR_SIZE
));
694 ret
= vmdk_add_extent(bs
, file
, false,
695 le64_to_cpu(header
.capacity
),
696 le64_to_cpu(header
.gd_offset
) << 9,
699 le32_to_cpu(header
.num_gtes_per_gt
),
700 le64_to_cpu(header
.granularity
),
707 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
708 if (extent
->compressed
) {
709 g_free(s
->create_type
);
710 s
->create_type
= g_strdup("streamOptimized");
712 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
713 extent
->version
= le32_to_cpu(header
.version
);
714 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
715 ret
= vmdk_init_tables(bs
, extent
, errp
);
717 /* free extent allocated by vmdk_add_extent */
718 vmdk_free_last_extent(bs
);
723 /* find an option value out of descriptor file */
724 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
725 char *buf
, int buf_size
)
727 char *opt_pos
, *opt_end
;
728 const char *end
= desc
+ strlen(desc
);
730 opt_pos
= strstr(desc
, opt_name
);
734 /* Skip "=\"" following opt_name */
735 opt_pos
+= strlen(opt_name
) + 2;
736 if (opt_pos
>= end
) {
740 while (opt_end
< end
&& *opt_end
!= '"') {
743 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
746 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
750 /* Open an extent file and append to bs array */
751 static int vmdk_open_sparse(BlockDriverState
*bs
,
752 BlockDriverState
*file
, int flags
,
753 char *buf
, Error
**errp
)
757 magic
= ldl_be_p(buf
);
760 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
763 return vmdk_open_vmdk4(bs
, file
, flags
, errp
);
766 error_setg(errp
, "Image not in VMDK format");
772 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
773 const char *desc_file_path
, Error
**errp
)
779 const char *p
= desc
;
782 char extent_path
[PATH_MAX
];
783 BlockDriverState
*extent_file
;
784 BDRVVmdkState
*s
= bs
->opaque
;
788 /* parse extent line:
789 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
791 * RW [size in sectors] SPARSE "file-name.vmdk"
794 ret
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
795 access
, §ors
, type
, fname
, &flat_offset
);
796 if (ret
< 4 || strcmp(access
, "RW")) {
798 } else if (!strcmp(type
, "FLAT")) {
799 if (ret
!= 5 || flat_offset
< 0) {
800 error_setg(errp
, "Invalid extent lines: \n%s", p
);
803 } else if (!strcmp(type
, "VMFS")) {
807 error_setg(errp
, "Invalid extent lines:\n%s", p
);
810 } else if (ret
!= 4) {
811 error_setg(errp
, "Invalid extent lines:\n%s", p
);
816 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
817 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE")) ||
818 (strcmp(access
, "RW"))) {
822 path_combine(extent_path
, sizeof(extent_path
),
823 desc_file_path
, fname
);
825 ret
= bdrv_open(&extent_file
, extent_path
, NULL
, NULL
,
826 bs
->open_flags
| BDRV_O_PROTOCOL
, NULL
, errp
);
831 /* save to extents array */
832 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
835 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
836 0, 0, 0, 0, 0, &extent
, errp
);
840 extent
->flat_start_offset
= flat_offset
<< 9;
841 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
842 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
843 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
847 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
, errp
);
851 bdrv_unref(extent_file
);
854 extent
= &s
->extents
[s
->num_extents
- 1];
856 error_setg(errp
, "Unsupported extent type '%s'", type
);
859 extent
->type
= g_strdup(type
);
861 /* move to next line */
873 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
878 BDRVVmdkState
*s
= bs
->opaque
;
880 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
881 error_setg(errp
, "invalid VMDK image descriptor");
885 if (strcmp(ct
, "monolithicFlat") &&
886 strcmp(ct
, "vmfs") &&
887 strcmp(ct
, "vmfsSparse") &&
888 strcmp(ct
, "twoGbMaxExtentSparse") &&
889 strcmp(ct
, "twoGbMaxExtentFlat")) {
890 error_setg(errp
, "Unsupported image type '%s'", ct
);
894 s
->create_type
= g_strdup(ct
);
896 ret
= vmdk_parse_extents(buf
, bs
, bs
->file
->filename
, errp
);
901 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
906 BDRVVmdkState
*s
= bs
->opaque
;
909 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
914 magic
= ldl_be_p(buf
);
918 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, errp
);
919 s
->desc_offset
= 0x200;
922 ret
= vmdk_open_desc_file(bs
, flags
, buf
, errp
);
929 /* try to open parent images, if exist */
930 ret
= vmdk_parent_open(bs
);
934 s
->cid
= vmdk_read_cid(bs
, 0);
935 s
->parent_cid
= vmdk_read_cid(bs
, 1);
936 qemu_co_mutex_init(&s
->lock
);
938 /* Disable migration when VMDK images are used */
939 error_set(&s
->migration_blocker
,
940 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
941 "vmdk", bs
->device_name
, "live migration");
942 migrate_add_blocker(s
->migration_blocker
);
948 g_free(s
->create_type
);
949 s
->create_type
= NULL
;
950 vmdk_free_extents(bs
);
955 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
957 BDRVVmdkState
*s
= bs
->opaque
;
960 for (i
= 0; i
< s
->num_extents
; i
++) {
961 if (!s
->extents
[i
].flat
) {
962 bs
->bl
.write_zeroes_alignment
=
963 MAX(bs
->bl
.write_zeroes_alignment
,
964 s
->extents
[i
].cluster_sectors
);
972 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
973 * to the cluster at @cluster_sector_num.
975 * If @skip_start_sector < @skip_end_sector, the relative range
976 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
977 * it for call to write user data in the request.
979 static int get_whole_cluster(BlockDriverState
*bs
,
981 uint64_t cluster_sector_num
,
983 uint64_t skip_start_sector
,
984 uint64_t skip_end_sector
)
987 int64_t cluster_bytes
;
988 uint8_t *whole_grain
;
990 /* For COW, align request sector_num to cluster start */
991 sector_num
= QEMU_ALIGN_DOWN(sector_num
, extent
->cluster_sectors
);
992 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
993 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
995 if (!bs
->backing_hd
) {
996 memset(whole_grain
, 0, skip_start_sector
<< BDRV_SECTOR_BITS
);
997 memset(whole_grain
+ (skip_end_sector
<< BDRV_SECTOR_BITS
), 0,
998 cluster_bytes
- (skip_end_sector
<< BDRV_SECTOR_BITS
));
1001 assert(skip_end_sector
<= extent
->cluster_sectors
);
1002 /* we will be here if it's first write on non-exist grain(cluster).
1003 * try to read from parent image, if exist */
1004 if (bs
->backing_hd
&& !vmdk_is_cid_valid(bs
)) {
1009 /* Read backing data before skip range */
1010 if (skip_start_sector
> 0) {
1011 if (bs
->backing_hd
) {
1012 ret
= bdrv_read(bs
->backing_hd
, sector_num
,
1013 whole_grain
, skip_start_sector
);
1019 ret
= bdrv_write(extent
->file
, cluster_sector_num
, whole_grain
,
1026 /* Read backing data after skip range */
1027 if (skip_end_sector
< extent
->cluster_sectors
) {
1028 if (bs
->backing_hd
) {
1029 ret
= bdrv_read(bs
->backing_hd
, sector_num
+ skip_end_sector
,
1030 whole_grain
+ (skip_end_sector
<< BDRV_SECTOR_BITS
),
1031 extent
->cluster_sectors
- skip_end_sector
);
1037 ret
= bdrv_write(extent
->file
, cluster_sector_num
+ skip_end_sector
,
1038 whole_grain
+ (skip_end_sector
<< BDRV_SECTOR_BITS
),
1039 extent
->cluster_sectors
- skip_end_sector
);
1047 qemu_vfree(whole_grain
);
1051 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1054 offset
= cpu_to_le32(offset
);
1055 /* update L2 table */
1056 if (bdrv_pwrite_sync(
1058 ((int64_t)m_data
->l2_offset
* 512)
1059 + (m_data
->l2_index
* sizeof(offset
)),
1060 &offset
, sizeof(offset
)) < 0) {
1063 /* update backup L2 table */
1064 if (extent
->l1_backup_table_offset
!= 0) {
1065 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1066 if (bdrv_pwrite_sync(
1068 ((int64_t)m_data
->l2_offset
* 512)
1069 + (m_data
->l2_index
* sizeof(offset
)),
1070 &offset
, sizeof(offset
)) < 0) {
1074 if (m_data
->l2_cache_entry
) {
1075 *m_data
->l2_cache_entry
= offset
;
1082 * get_cluster_offset
1084 * Look up cluster offset in extent file by sector number, and store in
1087 * For flat extents, the start offset as parsed from the description file is
1090 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1091 * offset for a new cluster and update L2 cache. If there is a backing file,
1092 * COW is done before returning; otherwise, zeroes are written to the allocated
1093 * cluster. Both COW and zero writing skips the sector range
1094 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1095 * has new data to write there.
1097 * Returns: VMDK_OK if cluster exists and mapped in the image.
1098 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1099 * VMDK_ERROR if failed.
1101 static int get_cluster_offset(BlockDriverState
*bs
,
1103 VmdkMetaData
*m_data
,
1106 uint64_t *cluster_offset
,
1107 uint64_t skip_start_sector
,
1108 uint64_t skip_end_sector
)
1110 unsigned int l1_index
, l2_offset
, l2_index
;
1111 int min_index
, i
, j
;
1112 uint32_t min_count
, *l2_table
;
1113 bool zeroed
= false;
1115 int32_t cluster_sector
;
1121 *cluster_offset
= extent
->flat_start_offset
;
1125 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1126 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1127 if (l1_index
>= extent
->l1_size
) {
1130 l2_offset
= extent
->l1_table
[l1_index
];
1132 return VMDK_UNALLOC
;
1134 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1135 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1136 /* increment the hit count */
1137 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1138 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1139 extent
->l2_cache_counts
[j
] >>= 1;
1142 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
1146 /* not found: load a new entry in the least used one */
1148 min_count
= 0xffffffff;
1149 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1150 if (extent
->l2_cache_counts
[i
] < min_count
) {
1151 min_count
= extent
->l2_cache_counts
[i
];
1155 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
1158 (int64_t)l2_offset
* 512,
1160 extent
->l2_size
* sizeof(uint32_t)
1161 ) != extent
->l2_size
* sizeof(uint32_t)) {
1165 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1166 extent
->l2_cache_counts
[min_index
] = 1;
1168 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1169 cluster_sector
= le32_to_cpu(l2_table
[l2_index
]);
1173 m_data
->l1_index
= l1_index
;
1174 m_data
->l2_index
= l2_index
;
1175 m_data
->l2_offset
= l2_offset
;
1176 m_data
->l2_cache_entry
= &l2_table
[l2_index
];
1178 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1182 if (!cluster_sector
|| zeroed
) {
1184 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1187 cluster_sector
= extent
->next_cluster_sector
;
1188 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1190 /* First of all we write grain itself, to avoid race condition
1191 * that may to corrupt the image.
1192 * This problem may occur because of insufficient space on host disk
1193 * or inappropriate VM shutdown.
1195 ret
= get_whole_cluster(bs
, extent
,
1197 offset
>> BDRV_SECTOR_BITS
,
1198 skip_start_sector
, skip_end_sector
);
1203 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1207 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1208 int64_t sector_num
, VmdkExtent
*start_hint
)
1210 VmdkExtent
*extent
= start_hint
;
1213 extent
= &s
->extents
[0];
1215 while (extent
< &s
->extents
[s
->num_extents
]) {
1216 if (sector_num
< extent
->end_sector
) {
1224 static int64_t coroutine_fn
vmdk_co_get_block_status(BlockDriverState
*bs
,
1225 int64_t sector_num
, int nb_sectors
, int *pnum
)
1227 BDRVVmdkState
*s
= bs
->opaque
;
1228 int64_t index_in_cluster
, n
, ret
;
1232 extent
= find_extent(s
, sector_num
, NULL
);
1236 qemu_co_mutex_lock(&s
->lock
);
1237 ret
= get_cluster_offset(bs
, extent
, NULL
,
1238 sector_num
* 512, false, &offset
,
1240 qemu_co_mutex_unlock(&s
->lock
);
1250 ret
= BDRV_BLOCK_ZERO
;
1253 ret
= BDRV_BLOCK_DATA
;
1254 if (extent
->file
== bs
->file
&& !extent
->compressed
) {
1255 ret
|= BDRV_BLOCK_OFFSET_VALID
| offset
;
1261 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1262 n
= extent
->cluster_sectors
- index_in_cluster
;
1263 if (n
> nb_sectors
) {
1270 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1271 int64_t offset_in_cluster
, const uint8_t *buf
,
1272 int nb_sectors
, int64_t sector_num
)
1275 VmdkGrainMarker
*data
= NULL
;
1277 const uint8_t *write_buf
= buf
;
1278 int write_len
= nb_sectors
* 512;
1280 if (extent
->compressed
) {
1281 if (!extent
->has_marker
) {
1285 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1286 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1287 if (compress(data
->data
, &buf_len
, buf
, nb_sectors
<< 9) != Z_OK
||
1292 data
->lba
= sector_num
;
1293 data
->size
= buf_len
;
1294 write_buf
= (uint8_t *)data
;
1295 write_len
= buf_len
+ sizeof(VmdkGrainMarker
);
1297 ret
= bdrv_pwrite(extent
->file
,
1298 cluster_offset
+ offset_in_cluster
,
1301 if (ret
!= write_len
) {
1302 ret
= ret
< 0 ? ret
: -EIO
;
1311 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1312 int64_t offset_in_cluster
, uint8_t *buf
,
1316 int cluster_bytes
, buf_bytes
;
1317 uint8_t *cluster_buf
, *compressed_data
;
1318 uint8_t *uncomp_buf
;
1320 VmdkGrainMarker
*marker
;
1324 if (!extent
->compressed
) {
1325 ret
= bdrv_pread(extent
->file
,
1326 cluster_offset
+ offset_in_cluster
,
1327 buf
, nb_sectors
* 512);
1328 if (ret
== nb_sectors
* 512) {
1334 cluster_bytes
= extent
->cluster_sectors
* 512;
1335 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1336 buf_bytes
= cluster_bytes
* 2;
1337 cluster_buf
= g_malloc(buf_bytes
);
1338 uncomp_buf
= g_malloc(cluster_bytes
);
1339 ret
= bdrv_pread(extent
->file
,
1341 cluster_buf
, buf_bytes
);
1345 compressed_data
= cluster_buf
;
1346 buf_len
= cluster_bytes
;
1347 data_len
= cluster_bytes
;
1348 if (extent
->has_marker
) {
1349 marker
= (VmdkGrainMarker
*)cluster_buf
;
1350 compressed_data
= marker
->data
;
1351 data_len
= le32_to_cpu(marker
->size
);
1353 if (!data_len
|| data_len
> buf_bytes
) {
1357 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1363 if (offset_in_cluster
< 0 ||
1364 offset_in_cluster
+ nb_sectors
* 512 > buf_len
) {
1368 memcpy(buf
, uncomp_buf
+ offset_in_cluster
, nb_sectors
* 512);
1373 g_free(cluster_buf
);
1377 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
1378 uint8_t *buf
, int nb_sectors
)
1380 BDRVVmdkState
*s
= bs
->opaque
;
1382 uint64_t n
, index_in_cluster
;
1383 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1384 VmdkExtent
*extent
= NULL
;
1385 uint64_t cluster_offset
;
1387 while (nb_sectors
> 0) {
1388 extent
= find_extent(s
, sector_num
, extent
);
1392 ret
= get_cluster_offset(bs
, extent
, NULL
,
1393 sector_num
<< 9, false, &cluster_offset
,
1395 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1396 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1397 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1398 n
= extent
->cluster_sectors
- index_in_cluster
;
1399 if (n
> nb_sectors
) {
1402 if (ret
!= VMDK_OK
) {
1403 /* if not allocated, try to read from parent image, if exist */
1404 if (bs
->backing_hd
&& ret
!= VMDK_ZEROED
) {
1405 if (!vmdk_is_cid_valid(bs
)) {
1408 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
1413 memset(buf
, 0, 512 * n
);
1416 ret
= vmdk_read_extent(extent
,
1417 cluster_offset
, index_in_cluster
* 512,
1430 static coroutine_fn
int vmdk_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1431 uint8_t *buf
, int nb_sectors
)
1434 BDRVVmdkState
*s
= bs
->opaque
;
1435 qemu_co_mutex_lock(&s
->lock
);
1436 ret
= vmdk_read(bs
, sector_num
, buf
, nb_sectors
);
1437 qemu_co_mutex_unlock(&s
->lock
);
1443 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1444 * if possible, otherwise return -ENOTSUP.
1445 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1446 * with each cluster. By dry run we can find if the zero write
1447 * is possible without modifying image data.
1449 * Returns: error code with 0 for success.
1451 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
1452 const uint8_t *buf
, int nb_sectors
,
1453 bool zeroed
, bool zero_dry_run
)
1455 BDRVVmdkState
*s
= bs
->opaque
;
1456 VmdkExtent
*extent
= NULL
;
1458 int64_t index_in_cluster
, n
;
1459 uint64_t extent_begin_sector
, extent_relative_sector_num
;
1460 uint64_t cluster_offset
;
1461 VmdkMetaData m_data
;
1463 if (sector_num
> bs
->total_sectors
) {
1464 error_report("Wrong offset: sector_num=0x%" PRIx64
1465 " total_sectors=0x%" PRIx64
"\n",
1466 sector_num
, bs
->total_sectors
);
1470 while (nb_sectors
> 0) {
1471 extent
= find_extent(s
, sector_num
, extent
);
1475 extent_begin_sector
= extent
->end_sector
- extent
->sectors
;
1476 extent_relative_sector_num
= sector_num
- extent_begin_sector
;
1477 index_in_cluster
= extent_relative_sector_num
% extent
->cluster_sectors
;
1478 n
= extent
->cluster_sectors
- index_in_cluster
;
1479 if (n
> nb_sectors
) {
1482 ret
= get_cluster_offset(bs
, extent
, &m_data
, sector_num
<< 9,
1483 !(extent
->compressed
|| zeroed
),
1485 index_in_cluster
, index_in_cluster
+ n
);
1486 if (extent
->compressed
) {
1487 if (ret
== VMDK_OK
) {
1488 /* Refuse write to allocated cluster for streamOptimized */
1489 error_report("Could not write to allocated cluster"
1490 " for streamOptimized");
1494 ret
= get_cluster_offset(bs
, extent
, &m_data
, sector_num
<< 9,
1495 true, &cluster_offset
, 0, 0);
1498 if (ret
== VMDK_ERROR
) {
1502 /* Do zeroed write, buf is ignored */
1503 if (extent
->has_zero_grain
&&
1504 index_in_cluster
== 0 &&
1505 n
>= extent
->cluster_sectors
) {
1506 n
= extent
->cluster_sectors
;
1507 if (!zero_dry_run
) {
1508 /* update L2 tables */
1509 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
1518 ret
= vmdk_write_extent(extent
,
1519 cluster_offset
, index_in_cluster
* 512,
1520 buf
, n
, sector_num
);
1525 /* update L2 tables */
1526 if (vmdk_L2update(extent
, &m_data
,
1527 cluster_offset
>> BDRV_SECTOR_BITS
)
1537 /* update CID on the first write every time the virtual disk is
1539 if (!s
->cid_updated
) {
1540 ret
= vmdk_write_cid(bs
, time(NULL
));
1544 s
->cid_updated
= true;
1550 static coroutine_fn
int vmdk_co_write(BlockDriverState
*bs
, int64_t sector_num
,
1551 const uint8_t *buf
, int nb_sectors
)
1554 BDRVVmdkState
*s
= bs
->opaque
;
1555 qemu_co_mutex_lock(&s
->lock
);
1556 ret
= vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1557 qemu_co_mutex_unlock(&s
->lock
);
1561 static int vmdk_write_compressed(BlockDriverState
*bs
,
1566 BDRVVmdkState
*s
= bs
->opaque
;
1567 if (s
->num_extents
== 1 && s
->extents
[0].compressed
) {
1568 return vmdk_write(bs
, sector_num
, buf
, nb_sectors
, false, false);
1574 static int coroutine_fn
vmdk_co_write_zeroes(BlockDriverState
*bs
,
1577 BdrvRequestFlags flags
)
1580 BDRVVmdkState
*s
= bs
->opaque
;
1581 qemu_co_mutex_lock(&s
->lock
);
1582 /* write zeroes could fail if sectors not aligned to cluster, test it with
1583 * dry_run == true before really updating image */
1584 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, true);
1586 ret
= vmdk_write(bs
, sector_num
, NULL
, nb_sectors
, true, false);
1588 qemu_co_mutex_unlock(&s
->lock
);
1592 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1593 bool flat
, bool compress
, bool zeroed_grain
,
1594 QemuOpts
*opts
, Error
**errp
)
1597 BlockDriverState
*bs
= NULL
;
1599 Error
*local_err
= NULL
;
1600 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
1601 uint32_t *gd_buf
= NULL
;
1604 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1606 error_propagate(errp
, local_err
);
1611 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1614 error_propagate(errp
, local_err
);
1619 ret
= bdrv_truncate(bs
, filesize
);
1621 error_setg_errno(errp
, -ret
, "Could not truncate file");
1625 magic
= cpu_to_be32(VMDK4_MAGIC
);
1626 memset(&header
, 0, sizeof(header
));
1627 header
.version
= zeroed_grain
? 2 : 1;
1628 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
1629 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
1630 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
1631 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1632 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
1633 header
.granularity
= 128;
1634 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
1636 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
1637 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
1639 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
1640 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
1642 header
.desc_offset
= 1;
1643 header
.desc_size
= 20;
1644 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1645 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
1646 header
.grain_offset
=
1647 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
1648 header
.granularity
);
1649 /* swap endianness for all header fields */
1650 header
.version
= cpu_to_le32(header
.version
);
1651 header
.flags
= cpu_to_le32(header
.flags
);
1652 header
.capacity
= cpu_to_le64(header
.capacity
);
1653 header
.granularity
= cpu_to_le64(header
.granularity
);
1654 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
1655 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1656 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1657 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1658 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1659 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1660 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1662 header
.check_bytes
[0] = 0xa;
1663 header
.check_bytes
[1] = 0x20;
1664 header
.check_bytes
[2] = 0xd;
1665 header
.check_bytes
[3] = 0xa;
1667 /* write all the data */
1668 ret
= bdrv_pwrite(bs
, 0, &magic
, sizeof(magic
));
1670 error_set(errp
, QERR_IO_ERROR
);
1673 ret
= bdrv_pwrite(bs
, sizeof(magic
), &header
, sizeof(header
));
1675 error_set(errp
, QERR_IO_ERROR
);
1679 ret
= bdrv_truncate(bs
, le64_to_cpu(header
.grain_offset
) << 9);
1681 error_setg_errno(errp
, -ret
, "Could not truncate file");
1685 /* write grain directory */
1686 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
1687 gd_buf
= g_malloc0(gd_buf_size
);
1688 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
1689 i
< gt_count
; i
++, tmp
+= gt_size
) {
1690 gd_buf
[i
] = cpu_to_le32(tmp
);
1692 ret
= bdrv_pwrite(bs
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
1693 gd_buf
, gd_buf_size
);
1695 error_set(errp
, QERR_IO_ERROR
);
1699 /* write backup grain directory */
1700 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
1701 i
< gt_count
; i
++, tmp
+= gt_size
) {
1702 gd_buf
[i
] = cpu_to_le32(tmp
);
1704 ret
= bdrv_pwrite(bs
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
1705 gd_buf
, gd_buf_size
);
1707 error_set(errp
, QERR_IO_ERROR
);
1720 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1721 char *postfix
, size_t buf_len
, Error
**errp
)
1725 if (filename
== NULL
|| !strlen(filename
)) {
1726 error_setg(errp
, "No filename provided");
1729 p
= strrchr(filename
, '/');
1731 p
= strrchr(filename
, '\\');
1734 p
= strrchr(filename
, ':');
1738 if (p
- filename
>= buf_len
) {
1741 pstrcpy(path
, p
- filename
+ 1, filename
);
1746 q
= strrchr(p
, '.');
1748 pstrcpy(prefix
, buf_len
, p
);
1751 if (q
- p
>= buf_len
) {
1754 pstrcpy(prefix
, q
- p
+ 1, p
);
1755 pstrcpy(postfix
, buf_len
, q
);
1760 static int vmdk_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1763 BlockDriverState
*new_bs
= NULL
;
1764 Error
*local_err
= NULL
;
1766 int64_t total_size
= 0, filesize
;
1767 char *adapter_type
= NULL
;
1768 char *backing_file
= NULL
;
1772 bool flat
, split
, compress
;
1773 GString
*ext_desc_lines
;
1774 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1775 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1776 const char *desc_extent_line
;
1777 char parent_desc_line
[BUF_SIZE
] = "";
1778 uint32_t parent_cid
= 0xffffffff;
1779 uint32_t number_heads
= 16;
1780 bool zeroed_grain
= false;
1781 uint32_t desc_offset
= 0, desc_len
;
1782 const char desc_template
[] =
1783 "# Disk DescriptorFile\n"
1786 "parentCID=%" PRIx32
"\n"
1787 "createType=\"%s\"\n"
1790 "# Extent description\n"
1793 "# The Disk Data Base\n"
1796 "ddb.virtualHWVersion = \"%d\"\n"
1797 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1798 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
1799 "ddb.geometry.sectors = \"63\"\n"
1800 "ddb.adapterType = \"%s\"\n";
1802 ext_desc_lines
= g_string_new(NULL
);
1804 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
1808 /* Read out options */
1809 total_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
1810 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
1811 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1812 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false)) {
1813 flags
|= BLOCK_FLAG_COMPAT6
;
1815 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
1816 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false)) {
1817 zeroed_grain
= true;
1820 if (!adapter_type
) {
1821 adapter_type
= g_strdup("ide");
1822 } else if (strcmp(adapter_type
, "ide") &&
1823 strcmp(adapter_type
, "buslogic") &&
1824 strcmp(adapter_type
, "lsilogic") &&
1825 strcmp(adapter_type
, "legacyESX")) {
1826 error_setg(errp
, "Unknown adapter type: '%s'", adapter_type
);
1830 if (strcmp(adapter_type
, "ide") != 0) {
1831 /* that's the number of heads with which vmware operates when
1832 creating, exporting, etc. vmdk files with a non-ide adapter type */
1836 /* Default format to monolithicSparse */
1837 fmt
= g_strdup("monolithicSparse");
1838 } else if (strcmp(fmt
, "monolithicFlat") &&
1839 strcmp(fmt
, "monolithicSparse") &&
1840 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1841 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1842 strcmp(fmt
, "streamOptimized")) {
1843 error_setg(errp
, "Unknown subformat: '%s'", fmt
);
1847 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1848 strcmp(fmt
, "twoGbMaxExtentSparse"));
1849 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1850 strcmp(fmt
, "twoGbMaxExtentFlat"));
1851 compress
= !strcmp(fmt
, "streamOptimized");
1853 desc_extent_line
= "RW %" PRId64
" FLAT \"%s\" 0\n";
1855 desc_extent_line
= "RW %" PRId64
" SPARSE \"%s\"\n";
1857 if (flat
&& backing_file
) {
1858 error_setg(errp
, "Flat image can't have backing file");
1862 if (flat
&& zeroed_grain
) {
1863 error_setg(errp
, "Flat image can't enable zeroed grain");
1868 BlockDriverState
*bs
= NULL
;
1869 ret
= bdrv_open(&bs
, backing_file
, NULL
, NULL
, BDRV_O_NO_BACKING
, NULL
,
1874 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1879 parent_cid
= vmdk_read_cid(bs
, 0);
1881 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1882 "parentFileNameHint=\"%s\"", backing_file
);
1885 /* Create extents */
1886 filesize
= total_size
;
1887 while (filesize
> 0) {
1888 char desc_line
[BUF_SIZE
];
1889 char ext_filename
[PATH_MAX
];
1890 char desc_filename
[PATH_MAX
];
1891 int64_t size
= filesize
;
1893 if (split
&& size
> split_size
) {
1897 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1898 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1900 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1903 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1906 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1907 path
, desc_filename
);
1909 if (vmdk_create_extent(ext_filename
, size
,
1910 flat
, compress
, zeroed_grain
, opts
, errp
)) {
1916 /* Format description line */
1917 snprintf(desc_line
, sizeof(desc_line
),
1918 desc_extent_line
, size
/ BDRV_SECTOR_SIZE
, desc_filename
);
1919 g_string_append(ext_desc_lines
, desc_line
);
1921 /* generate descriptor file */
1922 desc
= g_strdup_printf(desc_template
,
1923 (uint32_t)time(NULL
),
1927 ext_desc_lines
->str
,
1928 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1930 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
1933 desc_len
= strlen(desc
);
1934 /* the descriptor offset = 0x200 */
1935 if (!split
&& !flat
) {
1936 desc_offset
= 0x200;
1938 ret
= bdrv_create_file(filename
, opts
, &local_err
);
1940 error_propagate(errp
, local_err
);
1944 assert(new_bs
== NULL
);
1945 ret
= bdrv_open(&new_bs
, filename
, NULL
, NULL
,
1946 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, NULL
, &local_err
);
1948 error_propagate(errp
, local_err
);
1951 ret
= bdrv_pwrite(new_bs
, desc_offset
, desc
, desc_len
);
1953 error_setg_errno(errp
, -ret
, "Could not write description");
1956 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
1957 * for description file */
1958 if (desc_offset
== 0) {
1959 ret
= bdrv_truncate(new_bs
, desc_len
);
1961 error_setg_errno(errp
, -ret
, "Could not truncate file");
1968 g_free(adapter_type
);
1969 g_free(backing_file
);
1972 g_string_free(ext_desc_lines
, true);
1976 static void vmdk_close(BlockDriverState
*bs
)
1978 BDRVVmdkState
*s
= bs
->opaque
;
1980 vmdk_free_extents(bs
);
1981 g_free(s
->create_type
);
1983 migrate_del_blocker(s
->migration_blocker
);
1984 error_free(s
->migration_blocker
);
1987 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
1989 BDRVVmdkState
*s
= bs
->opaque
;
1993 for (i
= 0; i
< s
->num_extents
; i
++) {
1994 err
= bdrv_co_flush(s
->extents
[i
].file
);
2002 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2007 BDRVVmdkState
*s
= bs
->opaque
;
2009 ret
= bdrv_get_allocated_file_size(bs
->file
);
2013 for (i
= 0; i
< s
->num_extents
; i
++) {
2014 if (s
->extents
[i
].file
== bs
->file
) {
2017 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
2026 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2029 BDRVVmdkState
*s
= bs
->opaque
;
2031 /* If has a flat extent and its underlying storage doesn't have zero init,
2033 for (i
= 0; i
< s
->num_extents
; i
++) {
2034 if (s
->extents
[i
].flat
) {
2035 if (!bdrv_has_zero_init(s
->extents
[i
].file
)) {
2043 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2045 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2047 *info
= (ImageInfo
){
2048 .filename
= g_strdup(extent
->file
->filename
),
2049 .format
= g_strdup(extent
->type
),
2050 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2051 .compressed
= extent
->compressed
,
2052 .has_compressed
= extent
->compressed
,
2053 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2054 .has_cluster_size
= !extent
->flat
,
2060 static int vmdk_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
2063 BDRVVmdkState
*s
= bs
->opaque
;
2064 VmdkExtent
*extent
= NULL
;
2065 int64_t sector_num
= 0;
2066 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2068 uint64_t cluster_offset
;
2075 if (sector_num
>= total_sectors
) {
2078 extent
= find_extent(s
, sector_num
, extent
);
2081 "ERROR: could not find extent for sector %" PRId64
"\n",
2085 ret
= get_cluster_offset(bs
, extent
, NULL
,
2086 sector_num
<< BDRV_SECTOR_BITS
,
2087 false, &cluster_offset
, 0, 0);
2088 if (ret
== VMDK_ERROR
) {
2090 "ERROR: could not get cluster_offset for sector %"
2091 PRId64
"\n", sector_num
);
2094 if (ret
== VMDK_OK
&& cluster_offset
>= bdrv_getlength(extent
->file
)) {
2096 "ERROR: cluster offset for sector %"
2097 PRId64
" points after EOF\n", sector_num
);
2100 sector_num
+= extent
->cluster_sectors
;
2103 result
->corruptions
++;
2107 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
)
2110 BDRVVmdkState
*s
= bs
->opaque
;
2111 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2112 ImageInfoList
**next
;
2114 *spec_info
= (ImageInfoSpecific
){
2115 .kind
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2117 .vmdk
= g_new0(ImageInfoSpecificVmdk
, 1),
2121 *spec_info
->vmdk
= (ImageInfoSpecificVmdk
) {
2122 .create_type
= g_strdup(s
->create_type
),
2124 .parent_cid
= s
->parent_cid
,
2127 next
= &spec_info
->vmdk
->extents
;
2128 for (i
= 0; i
< s
->num_extents
; i
++) {
2129 *next
= g_new0(ImageInfoList
, 1);
2130 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2131 (*next
)->next
= NULL
;
2132 next
= &(*next
)->next
;
2138 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2141 BDRVVmdkState
*s
= bs
->opaque
;
2142 assert(s
->num_extents
);
2143 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2144 if (!s
->extents
[0].flat
) {
2145 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2147 /* See if we have multiple extents but they have different cases */
2148 for (i
= 1; i
< s
->num_extents
; i
++) {
2149 if (bdi
->needs_compressed_writes
!= s
->extents
[i
].compressed
||
2150 (bdi
->cluster_size
&& bdi
->cluster_size
!=
2151 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
)) {
2158 static void vmdk_detach_aio_context(BlockDriverState
*bs
)
2160 BDRVVmdkState
*s
= bs
->opaque
;
2163 for (i
= 0; i
< s
->num_extents
; i
++) {
2164 bdrv_detach_aio_context(s
->extents
[i
].file
);
2168 static void vmdk_attach_aio_context(BlockDriverState
*bs
,
2169 AioContext
*new_context
)
2171 BDRVVmdkState
*s
= bs
->opaque
;
2174 for (i
= 0; i
< s
->num_extents
; i
++) {
2175 bdrv_attach_aio_context(s
->extents
[i
].file
, new_context
);
2179 static QemuOptsList vmdk_create_opts
= {
2180 .name
= "vmdk-create-opts",
2181 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
2184 .name
= BLOCK_OPT_SIZE
,
2185 .type
= QEMU_OPT_SIZE
,
2186 .help
= "Virtual disk size"
2189 .name
= BLOCK_OPT_ADAPTER_TYPE
,
2190 .type
= QEMU_OPT_STRING
,
2191 .help
= "Virtual adapter type, can be one of "
2192 "ide (default), lsilogic, buslogic or legacyESX"
2195 .name
= BLOCK_OPT_BACKING_FILE
,
2196 .type
= QEMU_OPT_STRING
,
2197 .help
= "File name of a base image"
2200 .name
= BLOCK_OPT_COMPAT6
,
2201 .type
= QEMU_OPT_BOOL
,
2202 .help
= "VMDK version 6 image",
2203 .def_value_str
= "off"
2206 .name
= BLOCK_OPT_SUBFMT
,
2207 .type
= QEMU_OPT_STRING
,
2209 "VMDK flat extent format, can be one of "
2210 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2213 .name
= BLOCK_OPT_ZEROED_GRAIN
,
2214 .type
= QEMU_OPT_BOOL
,
2215 .help
= "Enable efficient zero writes "
2216 "using the zeroed-grain GTE feature"
2218 { /* end of list */ }
2222 static BlockDriver bdrv_vmdk
= {
2223 .format_name
= "vmdk",
2224 .instance_size
= sizeof(BDRVVmdkState
),
2225 .bdrv_probe
= vmdk_probe
,
2226 .bdrv_open
= vmdk_open
,
2227 .bdrv_check
= vmdk_check
,
2228 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
2229 .bdrv_read
= vmdk_co_read
,
2230 .bdrv_write
= vmdk_co_write
,
2231 .bdrv_write_compressed
= vmdk_write_compressed
,
2232 .bdrv_co_write_zeroes
= vmdk_co_write_zeroes
,
2233 .bdrv_close
= vmdk_close
,
2234 .bdrv_create
= vmdk_create
,
2235 .bdrv_co_flush_to_disk
= vmdk_co_flush
,
2236 .bdrv_co_get_block_status
= vmdk_co_get_block_status
,
2237 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
2238 .bdrv_has_zero_init
= vmdk_has_zero_init
,
2239 .bdrv_get_specific_info
= vmdk_get_specific_info
,
2240 .bdrv_refresh_limits
= vmdk_refresh_limits
,
2241 .bdrv_get_info
= vmdk_get_info
,
2242 .bdrv_detach_aio_context
= vmdk_detach_aio_context
,
2243 .bdrv_attach_aio_context
= vmdk_attach_aio_context
,
2245 .supports_backing
= true,
2246 .create_opts
= &vmdk_create_opts
,
2249 static void bdrv_vmdk_init(void)
2251 bdrv_register(&bdrv_vmdk
);
2254 block_init(bdrv_vmdk_init
);