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 typedef struct VMDKSESparseConstHeader
{
99 uint64_t grain_table_size
;
105 uint64_t volatile_header_offset
;
106 uint64_t volatile_header_size
;
107 uint64_t journal_header_offset
;
108 uint64_t journal_header_size
;
109 uint64_t journal_offset
;
110 uint64_t journal_size
;
111 uint64_t grain_dir_offset
;
112 uint64_t grain_dir_size
;
113 uint64_t grain_tables_offset
;
114 uint64_t grain_tables_size
;
115 uint64_t free_bitmap_offset
;
116 uint64_t free_bitmap_size
;
117 uint64_t backmap_offset
;
118 uint64_t backmap_size
;
119 uint64_t grains_offset
;
120 uint64_t grains_size
;
122 } QEMU_PACKED VMDKSESparseConstHeader
;
124 typedef struct VMDKSESparseVolatileHeader
{
126 uint64_t free_gt_number
;
127 uint64_t next_txn_seq_number
;
128 uint64_t replay_journal
;
130 } QEMU_PACKED VMDKSESparseVolatileHeader
;
132 #define L2_CACHE_SIZE 16
134 typedef struct VmdkExtent
{
141 uint64_t sesparse_l2_tables_offset
;
142 uint64_t sesparse_clusters_offset
;
147 int64_t flat_start_offset
;
148 int64_t l1_table_offset
;
149 int64_t l1_backup_table_offset
;
151 uint32_t *l1_backup_table
;
152 unsigned int l1_size
;
153 uint32_t l1_entry_sectors
;
155 unsigned int l2_size
;
157 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
158 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
160 int64_t cluster_sectors
;
161 int64_t next_cluster_sector
;
165 typedef struct BDRVVmdkState
{
167 uint64_t desc_offset
;
173 /* Extent array with num_extents entries, ascend ordered by address */
175 Error
*migration_blocker
;
179 typedef struct VmdkMetaData
{
180 unsigned int l1_index
;
181 unsigned int l2_index
;
182 unsigned int l2_offset
;
184 uint32_t *l2_cache_entry
;
187 typedef struct VmdkGrainMarker
{
191 } QEMU_PACKED VmdkGrainMarker
;
194 MARKER_END_OF_STREAM
= 0,
195 MARKER_GRAIN_TABLE
= 1,
196 MARKER_GRAIN_DIRECTORY
= 2,
200 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
207 magic
= be32_to_cpu(*(uint32_t *)buf
);
208 if (magic
== VMDK3_MAGIC
||
209 magic
== VMDK4_MAGIC
) {
212 const char *p
= (const char *)buf
;
213 const char *end
= p
+ buf_size
;
216 /* skip comment line */
217 while (p
< end
&& *p
!= '\n') {
224 while (p
< end
&& *p
== ' ') {
227 /* skip '\r' if windows line endings used. */
228 if (p
< end
&& *p
== '\r') {
231 /* only accept blank lines before 'version=' line */
232 if (p
== end
|| *p
!= '\n') {
238 if (end
- p
>= strlen("version=X\n")) {
239 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
240 strncmp("version=2\n", p
, strlen("version=2\n")) == 0 ||
241 strncmp("version=3\n", p
, strlen("version=3\n")) == 0) {
245 if (end
- p
>= strlen("version=X\r\n")) {
246 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
247 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0 ||
248 strncmp("version=3\r\n", p
, strlen("version=3\r\n")) == 0) {
258 #define SECTOR_SIZE 512
259 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
260 #define BUF_SIZE 4096
261 #define HEADER_SIZE 512 /* first sector of 512 bytes */
263 static void vmdk_free_extents(BlockDriverState
*bs
)
266 BDRVVmdkState
*s
= bs
->opaque
;
269 for (i
= 0; i
< s
->num_extents
; i
++) {
273 g_free(e
->l1_backup_table
);
275 if (e
->file
!= bs
->file
) {
276 bdrv_unref_child(bs
, e
->file
);
282 static void vmdk_free_last_extent(BlockDriverState
*bs
)
284 BDRVVmdkState
*s
= bs
->opaque
;
286 if (s
->num_extents
== 0) {
290 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
293 /* Return -ve errno, or 0 on success and write CID into *pcid. */
294 static int vmdk_read_cid(BlockDriverState
*bs
, int parent
, uint32_t *pcid
)
298 const char *p_name
, *cid_str
;
300 BDRVVmdkState
*s
= bs
->opaque
;
303 desc
= g_malloc0(DESC_SIZE
);
304 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
310 cid_str
= "parentCID";
311 cid_str_size
= sizeof("parentCID");
314 cid_str_size
= sizeof("CID");
317 desc
[DESC_SIZE
- 1] = '\0';
318 p_name
= strstr(desc
, cid_str
);
319 if (p_name
== NULL
) {
323 p_name
+= cid_str_size
;
324 if (sscanf(p_name
, "%" SCNx32
, &cid
) != 1) {
336 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
338 char *desc
, *tmp_desc
;
339 char *p_name
, *tmp_str
;
340 BDRVVmdkState
*s
= bs
->opaque
;
343 desc
= g_malloc0(DESC_SIZE
);
344 tmp_desc
= g_malloc0(DESC_SIZE
);
345 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
350 desc
[DESC_SIZE
- 1] = '\0';
351 tmp_str
= strstr(desc
, "parentCID");
352 if (tmp_str
== NULL
) {
357 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
358 p_name
= strstr(desc
, "CID");
359 if (p_name
!= NULL
) {
360 p_name
+= sizeof("CID");
361 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
362 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
365 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
373 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
375 BDRVVmdkState
*s
= bs
->opaque
;
378 if (!s
->cid_checked
&& bs
->backing
) {
379 BlockDriverState
*p_bs
= bs
->backing
->bs
;
381 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
382 /* Backing file is not in vmdk format, so it does not have
383 * a CID, which makes the overlay's parent CID invalid */
387 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
388 /* read failure: report as not valid */
391 if (s
->parent_cid
!= cur_pcid
) {
396 s
->cid_checked
= true;
401 /* We have nothing to do for VMDK reopen, stubs just return success */
402 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
403 BlockReopenQueue
*queue
, Error
**errp
)
405 assert(state
!= NULL
);
406 assert(state
->bs
!= NULL
);
410 static int vmdk_parent_open(BlockDriverState
*bs
)
414 BDRVVmdkState
*s
= bs
->opaque
;
417 desc
= g_malloc0(DESC_SIZE
+ 1);
418 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
424 p_name
= strstr(desc
, "parentFileNameHint");
425 if (p_name
!= NULL
) {
428 p_name
+= sizeof("parentFileNameHint") + 1;
429 end_name
= strchr(p_name
, '\"');
430 if (end_name
== NULL
) {
434 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
439 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
440 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
441 bs
->auto_backing_file
);
442 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
),
451 /* Create and append extent to the extent array. Return the added VmdkExtent
452 * address. return NULL if allocation failed. */
453 static int vmdk_add_extent(BlockDriverState
*bs
,
454 BdrvChild
*file
, bool flat
, int64_t sectors
,
455 int64_t l1_offset
, int64_t l1_backup_offset
,
457 int l2_size
, uint64_t cluster_sectors
,
458 VmdkExtent
**new_extent
,
462 BDRVVmdkState
*s
= bs
->opaque
;
465 if (cluster_sectors
> 0x200000) {
466 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
467 error_setg(errp
, "Invalid granularity, image may be corrupt");
470 if (l1_size
> 32 * 1024 * 1024) {
472 * Although with big capacity and small l1_entry_sectors, we can get a
473 * big l1_size, we don't want unbounded value to allocate the table.
474 * Limit it to 32M, which is enough to store:
475 * 8TB - for both VMDK3 & VMDK4 with
476 * minimal cluster size: 512B
477 * minimal L2 table size: 512 entries
478 * 8 TB is still more than the maximal value supported for
479 * VMDK3 & VMDK4 which is 2TB.
480 * 64TB - for "ESXi seSparse Extent"
481 * minimal cluster size: 512B (default is 4KB)
482 * L2 table size: 4096 entries (const).
483 * 64TB is more than the maximal value supported for
484 * seSparse VMDKs (which is slightly less than 64TB)
486 error_setg(errp
, "L1 size too big");
490 nb_sectors
= bdrv_nb_sectors(file
->bs
);
491 if (nb_sectors
< 0) {
495 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
496 extent
= &s
->extents
[s
->num_extents
];
499 memset(extent
, 0, sizeof(VmdkExtent
));
502 extent
->sectors
= sectors
;
503 extent
->l1_table_offset
= l1_offset
;
504 extent
->l1_backup_table_offset
= l1_backup_offset
;
505 extent
->l1_size
= l1_size
;
506 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
507 extent
->l2_size
= l2_size
;
508 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
509 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
510 extent
->entry_size
= sizeof(uint32_t);
512 if (s
->num_extents
> 1) {
513 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
515 extent
->end_sector
= extent
->sectors
;
517 bs
->total_sectors
= extent
->end_sector
;
519 *new_extent
= extent
;
524 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
531 /* read the L1 table */
532 l1_size
= extent
->l1_size
* extent
->entry_size
;
533 extent
->l1_table
= g_try_malloc(l1_size
);
534 if (l1_size
&& extent
->l1_table
== NULL
) {
538 ret
= bdrv_pread(extent
->file
,
539 extent
->l1_table_offset
,
543 bdrv_refresh_filename(extent
->file
->bs
);
544 error_setg_errno(errp
, -ret
,
545 "Could not read l1 table from extent '%s'",
546 extent
->file
->bs
->filename
);
549 for (i
= 0; i
< extent
->l1_size
; i
++) {
550 if (extent
->entry_size
== sizeof(uint64_t)) {
551 le64_to_cpus((uint64_t *)extent
->l1_table
+ i
);
553 assert(extent
->entry_size
== sizeof(uint32_t));
554 le32_to_cpus((uint32_t *)extent
->l1_table
+ i
);
558 if (extent
->l1_backup_table_offset
) {
559 assert(!extent
->sesparse
);
560 extent
->l1_backup_table
= g_try_malloc(l1_size
);
561 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
565 ret
= bdrv_pread(extent
->file
,
566 extent
->l1_backup_table_offset
,
567 extent
->l1_backup_table
,
570 bdrv_refresh_filename(extent
->file
->bs
);
571 error_setg_errno(errp
, -ret
,
572 "Could not read l1 backup table from extent '%s'",
573 extent
->file
->bs
->filename
);
576 for (i
= 0; i
< extent
->l1_size
; i
++) {
577 le32_to_cpus(&extent
->l1_backup_table
[i
]);
582 g_malloc(extent
->entry_size
* extent
->l2_size
* L2_CACHE_SIZE
);
585 g_free(extent
->l1_backup_table
);
587 g_free(extent
->l1_table
);
591 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
593 int flags
, Error
**errp
)
598 VmdkExtent
*extent
= NULL
;
600 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
602 bdrv_refresh_filename(file
->bs
);
603 error_setg_errno(errp
, -ret
,
604 "Could not read header from file '%s'",
608 ret
= vmdk_add_extent(bs
, file
, false,
609 le32_to_cpu(header
.disk_sectors
),
610 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
612 le32_to_cpu(header
.l1dir_size
),
614 le32_to_cpu(header
.granularity
),
620 ret
= vmdk_init_tables(bs
, extent
, errp
);
622 /* free extent allocated by vmdk_add_extent */
623 vmdk_free_last_extent(bs
);
628 #define SESPARSE_CONST_HEADER_MAGIC UINT64_C(0x00000000cafebabe)
629 #define SESPARSE_VOLATILE_HEADER_MAGIC UINT64_C(0x00000000cafecafe)
631 /* Strict checks - format not officially documented */
632 static int check_se_sparse_const_header(VMDKSESparseConstHeader
*header
,
635 header
->magic
= le64_to_cpu(header
->magic
);
636 header
->version
= le64_to_cpu(header
->version
);
637 header
->grain_size
= le64_to_cpu(header
->grain_size
);
638 header
->grain_table_size
= le64_to_cpu(header
->grain_table_size
);
639 header
->flags
= le64_to_cpu(header
->flags
);
640 header
->reserved1
= le64_to_cpu(header
->reserved1
);
641 header
->reserved2
= le64_to_cpu(header
->reserved2
);
642 header
->reserved3
= le64_to_cpu(header
->reserved3
);
643 header
->reserved4
= le64_to_cpu(header
->reserved4
);
645 header
->volatile_header_offset
=
646 le64_to_cpu(header
->volatile_header_offset
);
647 header
->volatile_header_size
= le64_to_cpu(header
->volatile_header_size
);
649 header
->journal_header_offset
= le64_to_cpu(header
->journal_header_offset
);
650 header
->journal_header_size
= le64_to_cpu(header
->journal_header_size
);
652 header
->journal_offset
= le64_to_cpu(header
->journal_offset
);
653 header
->journal_size
= le64_to_cpu(header
->journal_size
);
655 header
->grain_dir_offset
= le64_to_cpu(header
->grain_dir_offset
);
656 header
->grain_dir_size
= le64_to_cpu(header
->grain_dir_size
);
658 header
->grain_tables_offset
= le64_to_cpu(header
->grain_tables_offset
);
659 header
->grain_tables_size
= le64_to_cpu(header
->grain_tables_size
);
661 header
->free_bitmap_offset
= le64_to_cpu(header
->free_bitmap_offset
);
662 header
->free_bitmap_size
= le64_to_cpu(header
->free_bitmap_size
);
664 header
->backmap_offset
= le64_to_cpu(header
->backmap_offset
);
665 header
->backmap_size
= le64_to_cpu(header
->backmap_size
);
667 header
->grains_offset
= le64_to_cpu(header
->grains_offset
);
668 header
->grains_size
= le64_to_cpu(header
->grains_size
);
670 if (header
->magic
!= SESPARSE_CONST_HEADER_MAGIC
) {
671 error_setg(errp
, "Bad const header magic: 0x%016" PRIx64
,
676 if (header
->version
!= 0x0000000200000001) {
677 error_setg(errp
, "Unsupported version: 0x%016" PRIx64
,
682 if (header
->grain_size
!= 8) {
683 error_setg(errp
, "Unsupported grain size: %" PRIu64
,
688 if (header
->grain_table_size
!= 64) {
689 error_setg(errp
, "Unsupported grain table size: %" PRIu64
,
690 header
->grain_table_size
);
694 if (header
->flags
!= 0) {
695 error_setg(errp
, "Unsupported flags: 0x%016" PRIx64
,
700 if (header
->reserved1
!= 0 || header
->reserved2
!= 0 ||
701 header
->reserved3
!= 0 || header
->reserved4
!= 0) {
702 error_setg(errp
, "Unsupported reserved bits:"
703 " 0x%016" PRIx64
" 0x%016" PRIx64
704 " 0x%016" PRIx64
" 0x%016" PRIx64
,
705 header
->reserved1
, header
->reserved2
,
706 header
->reserved3
, header
->reserved4
);
710 /* check that padding is 0 */
711 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
712 error_setg(errp
, "Unsupported non-zero const header padding");
719 static int check_se_sparse_volatile_header(VMDKSESparseVolatileHeader
*header
,
722 header
->magic
= le64_to_cpu(header
->magic
);
723 header
->free_gt_number
= le64_to_cpu(header
->free_gt_number
);
724 header
->next_txn_seq_number
= le64_to_cpu(header
->next_txn_seq_number
);
725 header
->replay_journal
= le64_to_cpu(header
->replay_journal
);
727 if (header
->magic
!= SESPARSE_VOLATILE_HEADER_MAGIC
) {
728 error_setg(errp
, "Bad volatile header magic: 0x%016" PRIx64
,
733 if (header
->replay_journal
) {
734 error_setg(errp
, "Image is dirty, Replaying journal not supported");
738 /* check that padding is 0 */
739 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
740 error_setg(errp
, "Unsupported non-zero volatile header padding");
747 static int vmdk_open_se_sparse(BlockDriverState
*bs
,
749 int flags
, Error
**errp
)
752 VMDKSESparseConstHeader const_header
;
753 VMDKSESparseVolatileHeader volatile_header
;
754 VmdkExtent
*extent
= NULL
;
756 ret
= bdrv_apply_auto_read_only(bs
,
757 "No write support for seSparse images available", errp
);
762 assert(sizeof(const_header
) == SECTOR_SIZE
);
764 ret
= bdrv_pread(file
, 0, &const_header
, sizeof(const_header
));
766 bdrv_refresh_filename(file
->bs
);
767 error_setg_errno(errp
, -ret
,
768 "Could not read const header from file '%s'",
773 /* check const header */
774 ret
= check_se_sparse_const_header(&const_header
, errp
);
779 assert(sizeof(volatile_header
) == SECTOR_SIZE
);
781 ret
= bdrv_pread(file
,
782 const_header
.volatile_header_offset
* SECTOR_SIZE
,
783 &volatile_header
, sizeof(volatile_header
));
785 bdrv_refresh_filename(file
->bs
);
786 error_setg_errno(errp
, -ret
,
787 "Could not read volatile header from file '%s'",
792 /* check volatile header */
793 ret
= check_se_sparse_volatile_header(&volatile_header
, errp
);
798 ret
= vmdk_add_extent(bs
, file
, false,
799 const_header
.capacity
,
800 const_header
.grain_dir_offset
* SECTOR_SIZE
,
802 const_header
.grain_dir_size
*
803 SECTOR_SIZE
/ sizeof(uint64_t),
804 const_header
.grain_table_size
*
805 SECTOR_SIZE
/ sizeof(uint64_t),
806 const_header
.grain_size
,
813 extent
->sesparse
= true;
814 extent
->sesparse_l2_tables_offset
= const_header
.grain_tables_offset
;
815 extent
->sesparse_clusters_offset
= const_header
.grains_offset
;
816 extent
->entry_size
= sizeof(uint64_t);
818 ret
= vmdk_init_tables(bs
, extent
, errp
);
820 /* free extent allocated by vmdk_add_extent */
821 vmdk_free_last_extent(bs
);
827 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
828 QDict
*options
, Error
**errp
);
830 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
836 size
= bdrv_getlength(file
->bs
);
838 error_setg_errno(errp
, -size
, "Could not access file");
843 /* Both descriptor file and sparse image must be much larger than 4
844 * bytes, also callers of vmdk_read_desc want to compare the first 4
845 * bytes with VMDK4_MAGIC, let's error out if less is read. */
846 error_setg(errp
, "File is too small, not a valid image");
850 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
851 buf
= g_malloc(size
+ 1);
853 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
855 error_setg_errno(errp
, -ret
, "Could not read from file");
864 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
866 int flags
, QDict
*options
, Error
**errp
)
870 uint32_t l1_size
, l1_entry_sectors
;
872 VmdkExtent
*extent
= NULL
;
873 BDRVVmdkState
*s
= bs
->opaque
;
874 int64_t l1_backup_offset
= 0;
877 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
879 bdrv_refresh_filename(file
->bs
);
880 error_setg_errno(errp
, -ret
,
881 "Could not read header from file '%s'",
885 if (header
.capacity
== 0) {
886 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
888 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
892 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
898 if (!s
->create_type
) {
899 s
->create_type
= g_strdup("monolithicSparse");
902 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
904 * The footer takes precedence over the header, so read it in. The
905 * footer starts at offset -1024 from the end: One sector for the
906 * footer, and another one for the end-of-stream marker.
913 uint8_t pad
[512 - 16];
914 } QEMU_PACKED footer_marker
;
918 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
924 uint8_t pad
[512 - 16];
925 } QEMU_PACKED eos_marker
;
926 } QEMU_PACKED footer
;
928 ret
= bdrv_pread(file
,
929 bs
->file
->bs
->total_sectors
* 512 - 1536,
930 &footer
, sizeof(footer
));
932 error_setg_errno(errp
, -ret
, "Failed to read footer");
936 /* Some sanity checks for the footer */
937 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
938 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
939 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
940 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
941 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
942 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
944 error_setg(errp
, "Invalid footer");
948 header
= footer
.header
;
952 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
953 if (le32_to_cpu(header
.version
) > 3) {
954 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
955 le32_to_cpu(header
.version
));
957 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
959 /* VMware KB 2064959 explains that version 3 added support for
960 * persistent changed block tracking (CBT), and backup software can
961 * read it as version=1 if it doesn't care about the changed area
962 * information. So we are safe to enable read only. */
963 error_setg(errp
, "VMDK version 3 must be read only");
967 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
968 error_setg(errp
, "L2 table size too big");
972 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
973 * le64_to_cpu(header
.granularity
);
974 if (l1_entry_sectors
== 0) {
975 error_setg(errp
, "L1 entry size is invalid");
978 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
980 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
981 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
983 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
984 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
985 (int64_t)(le64_to_cpu(header
.grain_offset
)
986 * BDRV_SECTOR_SIZE
));
990 ret
= vmdk_add_extent(bs
, file
, false,
991 le64_to_cpu(header
.capacity
),
992 le64_to_cpu(header
.gd_offset
) << 9,
995 le32_to_cpu(header
.num_gtes_per_gt
),
996 le64_to_cpu(header
.granularity
),
1002 extent
->compressed
=
1003 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1004 if (extent
->compressed
) {
1005 g_free(s
->create_type
);
1006 s
->create_type
= g_strdup("streamOptimized");
1008 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
1009 extent
->version
= le32_to_cpu(header
.version
);
1010 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
1011 ret
= vmdk_init_tables(bs
, extent
, errp
);
1013 /* free extent allocated by vmdk_add_extent */
1014 vmdk_free_last_extent(bs
);
1019 /* find an option value out of descriptor file */
1020 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
1021 char *buf
, int buf_size
)
1023 char *opt_pos
, *opt_end
;
1024 const char *end
= desc
+ strlen(desc
);
1026 opt_pos
= strstr(desc
, opt_name
);
1030 /* Skip "=\"" following opt_name */
1031 opt_pos
+= strlen(opt_name
) + 2;
1032 if (opt_pos
>= end
) {
1036 while (opt_end
< end
&& *opt_end
!= '"') {
1039 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
1042 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
1046 /* Open an extent file and append to bs array */
1047 static int vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
1048 char *buf
, QDict
*options
, Error
**errp
)
1052 magic
= ldl_be_p(buf
);
1055 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
1057 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
1059 error_setg(errp
, "Image not in VMDK format");
1064 static const char *next_line(const char *s
)
1075 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
1076 QDict
*options
, Error
**errp
)
1084 int64_t sectors
= 0;
1085 int64_t flat_offset
;
1086 char *desc_file_dir
= NULL
;
1088 BdrvChild
*extent_file
;
1089 BdrvChildRole extent_role
;
1090 BDRVVmdkState
*s
= bs
->opaque
;
1091 VmdkExtent
*extent
= NULL
;
1092 char extent_opt_prefix
[32];
1093 Error
*local_err
= NULL
;
1095 for (p
= desc
; *p
; p
= next_line(p
)) {
1096 /* parse extent line in one of below formats:
1098 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
1099 * RW [size in sectors] SPARSE "file-name.vmdk"
1100 * RW [size in sectors] VMFS "file-name.vmdk"
1101 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
1102 * RW [size in sectors] SESPARSE "file-name.vmdk"
1105 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
1106 access
, §ors
, type
, fname
, &flat_offset
);
1107 if (matches
< 4 || strcmp(access
, "RW")) {
1109 } else if (!strcmp(type
, "FLAT")) {
1110 if (matches
!= 5 || flat_offset
< 0) {
1113 } else if (!strcmp(type
, "VMFS")) {
1119 } else if (matches
!= 4) {
1124 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
1125 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE") &&
1126 strcmp(type
, "SESPARSE")) ||
1127 (strcmp(access
, "RW"))) {
1131 if (path_is_absolute(fname
)) {
1132 extent_path
= g_strdup(fname
);
1134 if (!desc_file_dir
) {
1135 desc_file_dir
= bdrv_dirname(bs
->file
->bs
, errp
);
1136 if (!desc_file_dir
) {
1137 bdrv_refresh_filename(bs
->file
->bs
);
1138 error_prepend(errp
, "Cannot use relative paths with VMDK "
1139 "descriptor file '%s': ",
1140 bs
->file
->bs
->filename
);
1146 extent_path
= g_strconcat(desc_file_dir
, fname
, NULL
);
1149 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
1152 extent_role
= BDRV_CHILD_DATA
;
1153 if (strcmp(type
, "FLAT") != 0 && strcmp(type
, "VMFS") != 0) {
1154 /* non-flat extents have metadata */
1155 extent_role
|= BDRV_CHILD_METADATA
;
1158 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
1159 bs
, &child_of_bds
, extent_role
, false,
1161 g_free(extent_path
);
1163 error_propagate(errp
, local_err
);
1168 /* save to extents array */
1169 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
1172 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
1173 0, 0, 0, 0, 0, &extent
, errp
);
1175 bdrv_unref_child(bs
, extent_file
);
1178 extent
->flat_start_offset
= flat_offset
<< 9;
1179 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
1180 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
1181 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
1185 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
1190 bdrv_unref_child(bs
, extent_file
);
1193 extent
= &s
->extents
[s
->num_extents
- 1];
1194 } else if (!strcmp(type
, "SESPARSE")) {
1195 ret
= vmdk_open_se_sparse(bs
, extent_file
, bs
->open_flags
, errp
);
1197 bdrv_unref_child(bs
, extent_file
);
1200 extent
= &s
->extents
[s
->num_extents
- 1];
1202 error_setg(errp
, "Unsupported extent type '%s'", type
);
1203 bdrv_unref_child(bs
, extent_file
);
1207 extent
->type
= g_strdup(type
);
1216 if (np
[-1] == '\n') {
1219 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
1223 g_free(desc_file_dir
);
1227 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
1228 QDict
*options
, Error
**errp
)
1232 BDRVVmdkState
*s
= bs
->opaque
;
1234 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
1235 error_setg(errp
, "invalid VMDK image descriptor");
1239 if (strcmp(ct
, "monolithicFlat") &&
1240 strcmp(ct
, "vmfs") &&
1241 strcmp(ct
, "vmfsSparse") &&
1242 strcmp(ct
, "seSparse") &&
1243 strcmp(ct
, "twoGbMaxExtentSparse") &&
1244 strcmp(ct
, "twoGbMaxExtentFlat")) {
1245 error_setg(errp
, "Unsupported image type '%s'", ct
);
1249 s
->create_type
= g_strdup(ct
);
1251 ret
= vmdk_parse_extents(buf
, bs
, options
, errp
);
1256 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1261 BDRVVmdkState
*s
= bs
->opaque
;
1264 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
1265 BDRV_CHILD_IMAGE
, false, errp
);
1270 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
1275 magic
= ldl_be_p(buf
);
1279 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
1281 s
->desc_offset
= 0x200;
1284 /* No data in the descriptor file */
1285 bs
->file
->role
&= ~BDRV_CHILD_DATA
;
1287 /* Must succeed because we have given up permissions if anything */
1288 bdrv_child_refresh_perms(bs
, bs
->file
, &error_abort
);
1290 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1297 /* try to open parent images, if exist */
1298 ret
= vmdk_parent_open(bs
);
1302 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1306 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1310 qemu_co_mutex_init(&s
->lock
);
1312 /* Disable migration when VMDK images are used */
1313 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1314 "does not support live migration",
1315 bdrv_get_device_or_node_name(bs
));
1316 ret
= migrate_add_blocker(s
->migration_blocker
, errp
);
1318 error_free(s
->migration_blocker
);
1327 g_free(s
->create_type
);
1328 s
->create_type
= NULL
;
1329 vmdk_free_extents(bs
);
1334 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1336 BDRVVmdkState
*s
= bs
->opaque
;
1339 for (i
= 0; i
< s
->num_extents
; i
++) {
1340 if (!s
->extents
[i
].flat
) {
1341 bs
->bl
.pwrite_zeroes_alignment
=
1342 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1343 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1351 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1352 * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting
1353 * a zeroed cluster in the current layer and must not copy data from the
1356 * If @skip_start_sector < @skip_end_sector, the relative range
1357 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1358 * it for call to write user data in the request.
1360 static int get_whole_cluster(BlockDriverState
*bs
,
1362 uint64_t cluster_offset
,
1364 uint64_t skip_start_bytes
,
1365 uint64_t skip_end_bytes
,
1369 int64_t cluster_bytes
;
1370 uint8_t *whole_grain
;
1371 bool copy_from_backing
;
1373 /* For COW, align request sector_num to cluster start */
1374 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1375 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1376 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1377 copy_from_backing
= bs
->backing
&& !zeroed
;
1379 if (!copy_from_backing
) {
1380 memset(whole_grain
, 0, skip_start_bytes
);
1381 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1384 assert(skip_end_bytes
<= cluster_bytes
);
1385 /* we will be here if it's first write on non-exist grain(cluster).
1386 * try to read from parent image, if exist */
1387 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1392 /* Read backing data before skip range */
1393 if (skip_start_bytes
> 0) {
1394 if (copy_from_backing
) {
1395 /* qcow2 emits this on bs->file instead of bs->backing */
1396 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1397 ret
= bdrv_pread(bs
->backing
, offset
, whole_grain
,
1404 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1405 ret
= bdrv_pwrite(extent
->file
, cluster_offset
, whole_grain
,
1412 /* Read backing data after skip range */
1413 if (skip_end_bytes
< cluster_bytes
) {
1414 if (copy_from_backing
) {
1415 /* qcow2 emits this on bs->file instead of bs->backing */
1416 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1417 ret
= bdrv_pread(bs
->backing
, offset
+ skip_end_bytes
,
1418 whole_grain
+ skip_end_bytes
,
1419 cluster_bytes
- skip_end_bytes
);
1425 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1426 ret
= bdrv_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1427 whole_grain
+ skip_end_bytes
,
1428 cluster_bytes
- skip_end_bytes
);
1437 qemu_vfree(whole_grain
);
1441 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1444 offset
= cpu_to_le32(offset
);
1445 /* update L2 table */
1446 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1447 if (bdrv_pwrite(extent
->file
,
1448 ((int64_t)m_data
->l2_offset
* 512)
1449 + (m_data
->l2_index
* sizeof(offset
)),
1450 &offset
, sizeof(offset
)) < 0) {
1453 /* update backup L2 table */
1454 if (extent
->l1_backup_table_offset
!= 0) {
1455 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1456 if (bdrv_pwrite(extent
->file
,
1457 ((int64_t)m_data
->l2_offset
* 512)
1458 + (m_data
->l2_index
* sizeof(offset
)),
1459 &offset
, sizeof(offset
)) < 0) {
1463 if (bdrv_flush(extent
->file
->bs
) < 0) {
1466 if (m_data
->l2_cache_entry
) {
1467 *m_data
->l2_cache_entry
= offset
;
1474 * get_cluster_offset
1476 * Look up cluster offset in extent file by sector number, and store in
1479 * For flat extents, the start offset as parsed from the description file is
1482 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1483 * offset for a new cluster and update L2 cache. If there is a backing file,
1484 * COW is done before returning; otherwise, zeroes are written to the allocated
1485 * cluster. Both COW and zero writing skips the sector range
1486 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1487 * has new data to write there.
1489 * Returns: VMDK_OK if cluster exists and mapped in the image.
1490 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1491 * VMDK_ERROR if failed.
1493 static int get_cluster_offset(BlockDriverState
*bs
,
1495 VmdkMetaData
*m_data
,
1498 uint64_t *cluster_offset
,
1499 uint64_t skip_start_bytes
,
1500 uint64_t skip_end_bytes
)
1502 unsigned int l1_index
, l2_offset
, l2_index
;
1503 int min_index
, i
, j
;
1506 bool zeroed
= false;
1508 int64_t cluster_sector
;
1509 unsigned int l2_size_bytes
= extent
->l2_size
* extent
->entry_size
;
1512 m_data
->new_allocation
= false;
1515 *cluster_offset
= extent
->flat_start_offset
;
1519 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1520 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1521 if (l1_index
>= extent
->l1_size
) {
1524 if (extent
->sesparse
) {
1525 uint64_t l2_offset_u64
;
1527 assert(extent
->entry_size
== sizeof(uint64_t));
1529 l2_offset_u64
= ((uint64_t *)extent
->l1_table
)[l1_index
];
1530 if (l2_offset_u64
== 0) {
1532 } else if ((l2_offset_u64
& 0xffffffff00000000) != 0x1000000000000000) {
1534 * Top most nibble is 0x1 if grain table is allocated.
1535 * strict check - top most 4 bytes must be 0x10000000 since max
1536 * supported size is 64TB for disk - so no more than 64TB / 16MB
1537 * grain directories which is smaller than uint32,
1538 * where 16MB is the only supported default grain table coverage.
1542 l2_offset_u64
= l2_offset_u64
& 0x00000000ffffffff;
1543 l2_offset_u64
= extent
->sesparse_l2_tables_offset
+
1544 l2_offset_u64
* l2_size_bytes
/ SECTOR_SIZE
;
1545 if (l2_offset_u64
> 0x00000000ffffffff) {
1548 l2_offset
= (unsigned int)(l2_offset_u64
);
1551 assert(extent
->entry_size
== sizeof(uint32_t));
1552 l2_offset
= ((uint32_t *)extent
->l1_table
)[l1_index
];
1555 return VMDK_UNALLOC
;
1557 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1558 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1559 /* increment the hit count */
1560 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1561 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1562 extent
->l2_cache_counts
[j
] >>= 1;
1565 l2_table
= (char *)extent
->l2_cache
+ (i
* l2_size_bytes
);
1569 /* not found: load a new entry in the least used one */
1571 min_count
= 0xffffffff;
1572 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1573 if (extent
->l2_cache_counts
[i
] < min_count
) {
1574 min_count
= extent
->l2_cache_counts
[i
];
1578 l2_table
= (char *)extent
->l2_cache
+ (min_index
* l2_size_bytes
);
1579 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1580 if (bdrv_pread(extent
->file
,
1581 (int64_t)l2_offset
* 512,
1584 ) != l2_size_bytes
) {
1588 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1589 extent
->l2_cache_counts
[min_index
] = 1;
1591 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1593 m_data
->l1_index
= l1_index
;
1594 m_data
->l2_index
= l2_index
;
1595 m_data
->l2_offset
= l2_offset
;
1596 m_data
->l2_cache_entry
= ((uint32_t *)l2_table
) + l2_index
;
1599 if (extent
->sesparse
) {
1600 cluster_sector
= le64_to_cpu(((uint64_t *)l2_table
)[l2_index
]);
1601 switch (cluster_sector
& 0xf000000000000000) {
1602 case 0x0000000000000000:
1603 /* unallocated grain */
1604 if (cluster_sector
!= 0) {
1608 case 0x1000000000000000:
1609 /* scsi-unmapped grain - fallthrough */
1610 case 0x2000000000000000:
1614 case 0x3000000000000000:
1615 /* allocated grain */
1616 cluster_sector
= (((cluster_sector
& 0x0fff000000000000) >> 48) |
1617 ((cluster_sector
& 0x0000ffffffffffff) << 12));
1618 cluster_sector
= extent
->sesparse_clusters_offset
+
1619 cluster_sector
* extent
->cluster_sectors
;
1625 cluster_sector
= le32_to_cpu(((uint32_t *)l2_table
)[l2_index
]);
1627 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1632 if (!cluster_sector
|| zeroed
) {
1634 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1636 assert(!extent
->sesparse
);
1638 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1642 cluster_sector
= extent
->next_cluster_sector
;
1643 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1645 /* First of all we write grain itself, to avoid race condition
1646 * that may to corrupt the image.
1647 * This problem may occur because of insufficient space on host disk
1648 * or inappropriate VM shutdown.
1650 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1651 offset
, skip_start_bytes
, skip_end_bytes
,
1657 m_data
->new_allocation
= true;
1660 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1664 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1665 int64_t sector_num
, VmdkExtent
*start_hint
)
1667 VmdkExtent
*extent
= start_hint
;
1670 extent
= &s
->extents
[0];
1672 while (extent
< &s
->extents
[s
->num_extents
]) {
1673 if (sector_num
< extent
->end_sector
) {
1681 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1684 uint64_t extent_begin_offset
, extent_relative_offset
;
1685 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1687 extent_begin_offset
=
1688 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1689 extent_relative_offset
= offset
- extent_begin_offset
;
1690 return extent_relative_offset
% cluster_size
;
1693 static int coroutine_fn
vmdk_co_block_status(BlockDriverState
*bs
,
1695 int64_t offset
, int64_t bytes
,
1696 int64_t *pnum
, int64_t *map
,
1697 BlockDriverState
**file
)
1699 BDRVVmdkState
*s
= bs
->opaque
;
1700 int64_t index_in_cluster
, n
, ret
;
1701 uint64_t cluster_offset
;
1704 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1708 qemu_co_mutex_lock(&s
->lock
);
1709 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1711 qemu_co_mutex_unlock(&s
->lock
);
1713 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1722 ret
= BDRV_BLOCK_ZERO
;
1725 ret
= BDRV_BLOCK_DATA
;
1726 if (!extent
->compressed
) {
1727 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1728 *map
= cluster_offset
+ index_in_cluster
;
1730 ret
|= BDRV_BLOCK_RECURSE
;
1733 *file
= extent
->file
->bs
;
1737 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1738 *pnum
= MIN(n
, bytes
);
1742 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1743 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1744 uint64_t qiov_offset
, uint64_t n_bytes
,
1748 VmdkGrainMarker
*data
= NULL
;
1750 QEMUIOVector local_qiov
;
1751 int64_t write_offset
;
1752 int64_t write_end_sector
;
1754 if (extent
->compressed
) {
1755 void *compressed_data
;
1757 /* Only whole clusters */
1758 if (offset_in_cluster
||
1759 n_bytes
> (extent
->cluster_sectors
* SECTOR_SIZE
) ||
1760 (n_bytes
< (extent
->cluster_sectors
* SECTOR_SIZE
) &&
1761 offset
+ n_bytes
!= extent
->end_sector
* SECTOR_SIZE
))
1767 if (!extent
->has_marker
) {
1771 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1772 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1774 compressed_data
= g_malloc(n_bytes
);
1775 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1776 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1777 g_free(compressed_data
);
1779 if (ret
!= Z_OK
|| buf_len
== 0) {
1784 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1785 data
->size
= cpu_to_le32(buf_len
);
1787 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1788 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1790 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1792 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1793 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1795 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1798 write_offset
= cluster_offset
+ offset_in_cluster
;
1799 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1802 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1804 if (extent
->compressed
) {
1805 extent
->next_cluster_sector
= write_end_sector
;
1807 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1817 if (!extent
->compressed
) {
1818 qemu_iovec_destroy(&local_qiov
);
1823 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1824 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1828 int cluster_bytes
, buf_bytes
;
1829 uint8_t *cluster_buf
, *compressed_data
;
1830 uint8_t *uncomp_buf
;
1832 VmdkGrainMarker
*marker
;
1836 if (!extent
->compressed
) {
1837 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1838 ret
= bdrv_co_preadv(extent
->file
,
1839 cluster_offset
+ offset_in_cluster
, bytes
,
1846 cluster_bytes
= extent
->cluster_sectors
* 512;
1847 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1848 buf_bytes
= cluster_bytes
* 2;
1849 cluster_buf
= g_malloc(buf_bytes
);
1850 uncomp_buf
= g_malloc(cluster_bytes
);
1851 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1852 ret
= bdrv_pread(extent
->file
,
1854 cluster_buf
, buf_bytes
);
1858 compressed_data
= cluster_buf
;
1859 buf_len
= cluster_bytes
;
1860 data_len
= cluster_bytes
;
1861 if (extent
->has_marker
) {
1862 marker
= (VmdkGrainMarker
*)cluster_buf
;
1863 compressed_data
= marker
->data
;
1864 data_len
= le32_to_cpu(marker
->size
);
1866 if (!data_len
|| data_len
> buf_bytes
) {
1870 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1876 if (offset_in_cluster
< 0 ||
1877 offset_in_cluster
+ bytes
> buf_len
) {
1881 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1886 g_free(cluster_buf
);
1890 static int coroutine_fn
1891 vmdk_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1892 QEMUIOVector
*qiov
, int flags
)
1894 BDRVVmdkState
*s
= bs
->opaque
;
1896 uint64_t n_bytes
, offset_in_cluster
;
1897 VmdkExtent
*extent
= NULL
;
1898 QEMUIOVector local_qiov
;
1899 uint64_t cluster_offset
;
1900 uint64_t bytes_done
= 0;
1902 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1903 qemu_co_mutex_lock(&s
->lock
);
1906 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1911 ret
= get_cluster_offset(bs
, extent
, NULL
,
1912 offset
, false, &cluster_offset
, 0, 0);
1913 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1915 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1916 - offset_in_cluster
);
1918 if (ret
!= VMDK_OK
) {
1919 /* if not allocated, try to read from parent image, if exist */
1920 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1921 if (!vmdk_is_cid_valid(bs
)) {
1926 qemu_iovec_reset(&local_qiov
);
1927 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1929 /* qcow2 emits this on bs->file instead of bs->backing */
1930 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1931 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
1937 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
1940 qemu_iovec_reset(&local_qiov
);
1941 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1943 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
1944 &local_qiov
, n_bytes
);
1951 bytes_done
+= n_bytes
;
1956 qemu_co_mutex_unlock(&s
->lock
);
1957 qemu_iovec_destroy(&local_qiov
);
1964 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1965 * if possible, otherwise return -ENOTSUP.
1966 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1967 * with each cluster. By dry run we can find if the zero write
1968 * is possible without modifying image data.
1970 * Returns: error code with 0 for success.
1972 static int vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1973 uint64_t bytes
, QEMUIOVector
*qiov
,
1974 bool zeroed
, bool zero_dry_run
)
1976 BDRVVmdkState
*s
= bs
->opaque
;
1977 VmdkExtent
*extent
= NULL
;
1979 int64_t offset_in_cluster
, n_bytes
;
1980 uint64_t cluster_offset
;
1981 uint64_t bytes_done
= 0;
1982 VmdkMetaData m_data
;
1984 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
1985 error_report("Wrong offset: offset=0x%" PRIx64
1986 " total_sectors=0x%" PRIx64
,
1987 offset
, bs
->total_sectors
);
1992 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1996 if (extent
->sesparse
) {
1999 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
2000 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
2001 - offset_in_cluster
);
2003 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2004 !(extent
->compressed
|| zeroed
),
2005 &cluster_offset
, offset_in_cluster
,
2006 offset_in_cluster
+ n_bytes
);
2007 if (extent
->compressed
) {
2008 if (ret
== VMDK_OK
) {
2009 /* Refuse write to allocated cluster for streamOptimized */
2010 error_report("Could not write to allocated cluster"
2011 " for streamOptimized");
2013 } else if (!zeroed
) {
2015 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2016 true, &cluster_offset
, 0, 0);
2019 if (ret
== VMDK_ERROR
) {
2023 /* Do zeroed write, buf is ignored */
2024 if (extent
->has_zero_grain
&&
2025 offset_in_cluster
== 0 &&
2026 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
2027 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
2028 if (!zero_dry_run
&& ret
!= VMDK_ZEROED
) {
2029 /* update L2 tables */
2030 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
2039 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
2040 qiov
, bytes_done
, n_bytes
, offset
);
2044 if (m_data
.new_allocation
) {
2045 /* update L2 tables */
2046 if (vmdk_L2update(extent
, &m_data
,
2047 cluster_offset
>> BDRV_SECTOR_BITS
)
2055 bytes_done
+= n_bytes
;
2057 /* update CID on the first write every time the virtual disk is
2059 if (!s
->cid_updated
) {
2060 ret
= vmdk_write_cid(bs
, g_random_int());
2064 s
->cid_updated
= true;
2070 static int coroutine_fn
2071 vmdk_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
2072 QEMUIOVector
*qiov
, int flags
)
2075 BDRVVmdkState
*s
= bs
->opaque
;
2076 qemu_co_mutex_lock(&s
->lock
);
2077 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
2078 qemu_co_mutex_unlock(&s
->lock
);
2082 static int coroutine_fn
2083 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, uint64_t offset
,
2084 uint64_t bytes
, QEMUIOVector
*qiov
)
2087 /* The caller will write bytes 0 to signal EOF.
2088 * When receive it, we align EOF to a sector boundary. */
2089 BDRVVmdkState
*s
= bs
->opaque
;
2093 for (i
= 0; i
< s
->num_extents
; i
++) {
2094 length
= bdrv_getlength(s
->extents
[i
].file
->bs
);
2098 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
2099 ret
= bdrv_truncate(s
->extents
[i
].file
, length
, false,
2100 PREALLOC_MODE_OFF
, 0, NULL
);
2107 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
2110 static int coroutine_fn
vmdk_co_pwrite_zeroes(BlockDriverState
*bs
,
2113 BdrvRequestFlags flags
)
2116 BDRVVmdkState
*s
= bs
->opaque
;
2118 qemu_co_mutex_lock(&s
->lock
);
2119 /* write zeroes could fail if sectors not aligned to cluster, test it with
2120 * dry_run == true before really updating image */
2121 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
2123 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
2125 qemu_co_mutex_unlock(&s
->lock
);
2129 static int vmdk_init_extent(BlockBackend
*blk
,
2130 int64_t filesize
, bool flat
,
2131 bool compress
, bool zeroed_grain
,
2136 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
2137 uint32_t *gd_buf
= NULL
;
2141 ret
= blk_truncate(blk
, filesize
, false, PREALLOC_MODE_OFF
, 0, errp
);
2144 magic
= cpu_to_be32(VMDK4_MAGIC
);
2145 memset(&header
, 0, sizeof(header
));
2148 } else if (zeroed_grain
) {
2153 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
2154 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
2155 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
2156 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
2157 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
2158 header
.granularity
= 128;
2159 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
2161 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
2162 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
2164 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
2165 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
2167 header
.desc_offset
= 1;
2168 header
.desc_size
= 20;
2169 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
2170 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
2171 header
.grain_offset
=
2172 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
2173 header
.granularity
);
2174 /* swap endianness for all header fields */
2175 header
.version
= cpu_to_le32(header
.version
);
2176 header
.flags
= cpu_to_le32(header
.flags
);
2177 header
.capacity
= cpu_to_le64(header
.capacity
);
2178 header
.granularity
= cpu_to_le64(header
.granularity
);
2179 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
2180 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
2181 header
.desc_size
= cpu_to_le64(header
.desc_size
);
2182 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
2183 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
2184 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
2185 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
2187 header
.check_bytes
[0] = 0xa;
2188 header
.check_bytes
[1] = 0x20;
2189 header
.check_bytes
[2] = 0xd;
2190 header
.check_bytes
[3] = 0xa;
2192 /* write all the data */
2193 ret
= blk_pwrite(blk
, 0, &magic
, sizeof(magic
), 0);
2195 error_setg(errp
, QERR_IO_ERROR
);
2198 ret
= blk_pwrite(blk
, sizeof(magic
), &header
, sizeof(header
), 0);
2200 error_setg(errp
, QERR_IO_ERROR
);
2204 ret
= blk_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9, false,
2205 PREALLOC_MODE_OFF
, 0, errp
);
2210 /* write grain directory */
2211 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
2212 gd_buf
= g_malloc0(gd_buf_size
);
2213 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
2214 i
< gt_count
; i
++, tmp
+= gt_size
) {
2215 gd_buf
[i
] = cpu_to_le32(tmp
);
2217 ret
= blk_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
2218 gd_buf
, gd_buf_size
, 0);
2220 error_setg(errp
, QERR_IO_ERROR
);
2224 /* write backup grain directory */
2225 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
2226 i
< gt_count
; i
++, tmp
+= gt_size
) {
2227 gd_buf
[i
] = cpu_to_le32(tmp
);
2229 ret
= blk_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
2230 gd_buf
, gd_buf_size
, 0);
2232 error_setg(errp
, QERR_IO_ERROR
);
2241 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
2242 bool flat
, bool compress
, bool zeroed_grain
,
2244 QemuOpts
*opts
, Error
**errp
)
2247 BlockBackend
*blk
= NULL
;
2249 ret
= bdrv_create_file(filename
, opts
, errp
);
2254 blk
= blk_new_open(filename
, NULL
, NULL
,
2255 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
2262 blk_set_allow_write_beyond_eof(blk
, true);
2264 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
2277 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
2278 char *postfix
, size_t buf_len
, Error
**errp
)
2282 if (filename
== NULL
|| !strlen(filename
)) {
2283 error_setg(errp
, "No filename provided");
2286 p
= strrchr(filename
, '/');
2288 p
= strrchr(filename
, '\\');
2291 p
= strrchr(filename
, ':');
2295 if (p
- filename
>= buf_len
) {
2298 pstrcpy(path
, p
- filename
+ 1, filename
);
2303 q
= strrchr(p
, '.');
2305 pstrcpy(prefix
, buf_len
, p
);
2308 if (q
- p
>= buf_len
) {
2311 pstrcpy(prefix
, q
- p
+ 1, p
);
2312 pstrcpy(postfix
, buf_len
, q
);
2318 * idx == 0: get or create the descriptor file (also the image file if in a
2320 * idx >= 1: get the n-th extent if in a split subformat
2322 typedef BlockBackend
*(*vmdk_create_extent_fn
)(int64_t size
,
2331 static void vmdk_desc_add_extent(GString
*desc
,
2332 const char *extent_line_fmt
,
2333 int64_t size
, const char *filename
)
2335 char *basename
= g_path_get_basename(filename
);
2337 g_string_append_printf(desc
, extent_line_fmt
,
2338 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
2342 static int coroutine_fn
vmdk_co_do_create(int64_t size
,
2343 BlockdevVmdkSubformat subformat
,
2344 BlockdevVmdkAdapterType adapter_type
,
2345 const char *backing_file
,
2346 const char *hw_version
,
2349 vmdk_create_extent_fn extent_fn
,
2354 BlockBackend
*blk
= NULL
;
2355 BlockBackend
*extent_blk
;
2356 Error
*local_err
= NULL
;
2359 bool flat
, split
, compress
;
2360 GString
*ext_desc_lines
;
2361 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
2362 int64_t extent_size
;
2363 int64_t created_size
= 0;
2364 const char *extent_line_fmt
;
2365 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2366 uint32_t parent_cid
= 0xffffffff;
2367 uint32_t number_heads
= 16;
2368 uint32_t desc_offset
= 0, desc_len
;
2369 const char desc_template
[] =
2370 "# Disk DescriptorFile\n"
2373 "parentCID=%" PRIx32
"\n"
2374 "createType=\"%s\"\n"
2377 "# Extent description\n"
2380 "# The Disk Data Base\n"
2383 "ddb.virtualHWVersion = \"%s\"\n"
2384 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2385 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2386 "ddb.geometry.sectors = \"63\"\n"
2387 "ddb.adapterType = \"%s\"\n";
2389 ext_desc_lines
= g_string_new(NULL
);
2391 /* Read out options */
2395 "compat6 cannot be enabled with hwversion set");
2405 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2406 /* that's the number of heads with which vmware operates when
2407 creating, exporting, etc. vmdk files with a non-ide adapter type */
2410 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2411 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2412 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2413 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2414 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2417 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2419 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2421 if (flat
&& backing_file
) {
2422 error_setg(errp
, "Flat image can't have backing file");
2426 if (flat
&& zeroed_grain
) {
2427 error_setg(errp
, "Flat image can't enable zeroed grain");
2432 /* Create extents */
2434 extent_size
= split_size
;
2438 if (!split
&& !flat
) {
2439 created_size
= extent_size
;
2443 /* Get the descriptor file BDS */
2444 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2450 if (!split
&& !flat
) {
2451 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2452 blk_bs(blk
)->filename
);
2456 BlockBackend
*backing
;
2457 char *full_backing
=
2458 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2462 error_propagate(errp
, local_err
);
2466 assert(full_backing
);
2468 backing
= blk_new_open(full_backing
, NULL
, NULL
,
2469 BDRV_O_NO_BACKING
, errp
);
2470 g_free(full_backing
);
2471 if (backing
== NULL
) {
2475 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2476 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2477 blk_bs(backing
)->drv
->format_name
);
2482 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2485 error_setg(errp
, "Failed to read parent CID");
2488 snprintf(parent_desc_line
, BUF_SIZE
,
2489 "parentFileNameHint=\"%s\"", backing_file
);
2492 while (created_size
< size
) {
2493 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2494 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2495 zeroed_grain
, opaque
, errp
);
2500 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2501 blk_bs(extent_blk
)->filename
);
2502 created_size
+= cur_size
;
2504 blk_unref(extent_blk
);
2507 /* Check whether we got excess extents */
2508 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2511 blk_unref(extent_blk
);
2512 error_setg(errp
, "List of extents contains unused extents");
2517 /* generate descriptor file */
2518 desc
= g_strdup_printf(desc_template
,
2521 BlockdevVmdkSubformat_str(subformat
),
2523 ext_desc_lines
->str
,
2526 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2528 BlockdevVmdkAdapterType_str(adapter_type
));
2529 desc_len
= strlen(desc
);
2530 /* the descriptor offset = 0x200 */
2531 if (!split
&& !flat
) {
2532 desc_offset
= 0x200;
2535 ret
= blk_pwrite(blk
, desc_offset
, desc
, desc_len
, 0);
2537 error_setg_errno(errp
, -ret
, "Could not write description");
2540 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2541 * for description file */
2542 if (desc_offset
== 0) {
2543 ret
= blk_truncate(blk
, desc_len
, false, PREALLOC_MODE_OFF
, 0, errp
);
2554 g_free(parent_desc_line
);
2555 g_string_free(ext_desc_lines
, true);
2564 } VMDKCreateOptsData
;
2566 static BlockBackend
*vmdk_co_create_opts_cb(int64_t size
, int idx
,
2567 bool flat
, bool split
, bool compress
,
2568 bool zeroed_grain
, void *opaque
,
2571 BlockBackend
*blk
= NULL
;
2572 BlockDriverState
*bs
= NULL
;
2573 VMDKCreateOptsData
*data
= opaque
;
2574 char *ext_filename
= NULL
;
2575 char *rel_filename
= NULL
;
2577 /* We're done, don't create excess extents. */
2579 assert(errp
== NULL
);
2584 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2586 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2588 flat
? 'f' : 's', idx
, data
->postfix
);
2591 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2594 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2595 g_free(rel_filename
);
2597 if (vmdk_create_extent(ext_filename
, size
,
2598 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2604 g_free(ext_filename
);
2608 static int coroutine_fn
vmdk_co_create_opts(BlockDriver
*drv
,
2609 const char *filename
,
2613 Error
*local_err
= NULL
;
2615 int64_t total_size
= 0;
2616 char *adapter_type
= NULL
;
2617 BlockdevVmdkAdapterType adapter_type_enum
;
2618 char *backing_file
= NULL
;
2619 char *hw_version
= NULL
;
2621 BlockdevVmdkSubformat subformat
;
2623 char *path
= g_malloc0(PATH_MAX
);
2624 char *prefix
= g_malloc0(PATH_MAX
);
2625 char *postfix
= g_malloc0(PATH_MAX
);
2626 char *desc_line
= g_malloc0(BUF_SIZE
);
2627 char *ext_filename
= g_malloc0(PATH_MAX
);
2628 char *desc_filename
= g_malloc0(PATH_MAX
);
2629 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2632 VMDKCreateOptsData data
;
2633 char *backing_fmt
= NULL
;
2635 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2636 if (backing_fmt
&& strcmp(backing_fmt
, "vmdk") != 0) {
2637 error_setg(errp
, "backing_file must be a vmdk image");
2642 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2646 /* Read out options */
2647 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2649 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2650 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2651 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2652 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2653 if (strcmp(hw_version
, "undefined") == 0) {
2657 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2658 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2661 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2663 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2666 error_propagate(errp
, local_err
);
2671 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2675 /* Default format to monolithicSparse */
2676 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2678 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2680 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2683 error_propagate(errp
, local_err
);
2688 data
= (VMDKCreateOptsData
){
2694 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2695 backing_file
, hw_version
, compat6
, zeroed_grain
,
2696 vmdk_co_create_opts_cb
, &data
, errp
);
2699 g_free(backing_fmt
);
2700 g_free(adapter_type
);
2701 g_free(backing_file
);
2709 g_free(ext_filename
);
2710 g_free(desc_filename
);
2711 g_free(parent_desc_line
);
2715 static BlockBackend
*vmdk_co_create_cb(int64_t size
, int idx
,
2716 bool flat
, bool split
, bool compress
,
2717 bool zeroed_grain
, void *opaque
,
2721 BlockDriverState
*bs
;
2723 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2726 bs
= bdrv_open_blockdev_ref(opts
->file
, errp
);
2729 BlockdevRefList
*list
= opts
->extents
;
2730 for (i
= 1; i
< idx
; i
++) {
2731 if (!list
|| !list
->next
) {
2732 error_setg(errp
, "Extent [%d] not specified", i
);
2738 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2741 bs
= bdrv_open_blockdev_ref(list
->value
, errp
);
2746 blk
= blk_new_with_bs(bs
,
2747 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
2748 BLK_PERM_ALL
, errp
);
2752 blk_set_allow_write_beyond_eof(blk
, true);
2756 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2765 static int coroutine_fn
vmdk_co_create(BlockdevCreateOptions
*create_options
,
2769 BlockdevCreateOptionsVmdk
*opts
;
2771 opts
= &create_options
->u
.vmdk
;
2773 /* Validate options */
2774 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2775 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2780 ret
= vmdk_co_do_create(opts
->size
,
2795 static void vmdk_close(BlockDriverState
*bs
)
2797 BDRVVmdkState
*s
= bs
->opaque
;
2799 vmdk_free_extents(bs
);
2800 g_free(s
->create_type
);
2802 migrate_del_blocker(s
->migration_blocker
);
2803 error_free(s
->migration_blocker
);
2806 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2811 BDRVVmdkState
*s
= bs
->opaque
;
2813 ret
= bdrv_get_allocated_file_size(bs
->file
->bs
);
2817 for (i
= 0; i
< s
->num_extents
; i
++) {
2818 if (s
->extents
[i
].file
== bs
->file
) {
2821 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2830 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2833 BDRVVmdkState
*s
= bs
->opaque
;
2835 /* If has a flat extent and its underlying storage doesn't have zero init,
2837 for (i
= 0; i
< s
->num_extents
; i
++) {
2838 if (s
->extents
[i
].flat
) {
2839 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2847 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2849 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2851 bdrv_refresh_filename(extent
->file
->bs
);
2852 *info
= (ImageInfo
){
2853 .filename
= g_strdup(extent
->file
->bs
->filename
),
2854 .format
= g_strdup(extent
->type
),
2855 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2856 .compressed
= extent
->compressed
,
2857 .has_compressed
= extent
->compressed
,
2858 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2859 .has_cluster_size
= !extent
->flat
,
2865 static int coroutine_fn
vmdk_co_check(BlockDriverState
*bs
,
2866 BdrvCheckResult
*result
,
2869 BDRVVmdkState
*s
= bs
->opaque
;
2870 VmdkExtent
*extent
= NULL
;
2871 int64_t sector_num
= 0;
2872 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2874 uint64_t cluster_offset
;
2881 if (sector_num
>= total_sectors
) {
2884 extent
= find_extent(s
, sector_num
, extent
);
2887 "ERROR: could not find extent for sector %" PRId64
"\n",
2892 ret
= get_cluster_offset(bs
, extent
, NULL
,
2893 sector_num
<< BDRV_SECTOR_BITS
,
2894 false, &cluster_offset
, 0, 0);
2895 if (ret
== VMDK_ERROR
) {
2897 "ERROR: could not get cluster_offset for sector %"
2898 PRId64
"\n", sector_num
);
2901 if (ret
== VMDK_OK
) {
2902 int64_t extent_len
= bdrv_getlength(extent
->file
->bs
);
2903 if (extent_len
< 0) {
2905 "ERROR: could not get extent file length for sector %"
2906 PRId64
"\n", sector_num
);
2910 if (cluster_offset
>= extent_len
) {
2912 "ERROR: cluster offset for sector %"
2913 PRId64
" points after EOF\n", sector_num
);
2918 sector_num
+= extent
->cluster_sectors
;
2921 result
->corruptions
++;
2925 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
,
2929 BDRVVmdkState
*s
= bs
->opaque
;
2930 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2931 ImageInfoList
**next
;
2933 *spec_info
= (ImageInfoSpecific
){
2934 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2936 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
2940 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
2941 .create_type
= g_strdup(s
->create_type
),
2943 .parent_cid
= s
->parent_cid
,
2946 next
= &spec_info
->u
.vmdk
.data
->extents
;
2947 for (i
= 0; i
< s
->num_extents
; i
++) {
2948 *next
= g_new0(ImageInfoList
, 1);
2949 (*next
)->value
= vmdk_get_extent_info(&s
->extents
[i
]);
2950 (*next
)->next
= NULL
;
2951 next
= &(*next
)->next
;
2957 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
2959 return a
->flat
== b
->flat
&&
2960 a
->compressed
== b
->compressed
&&
2961 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
2964 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2967 BDRVVmdkState
*s
= bs
->opaque
;
2968 assert(s
->num_extents
);
2970 /* See if we have multiple extents but they have different cases */
2971 for (i
= 1; i
< s
->num_extents
; i
++) {
2972 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
2976 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2977 if (!s
->extents
[0].flat
) {
2978 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2983 static void vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
2984 bool backing_overridden
)
2986 /* No children but file and backing can be explicitly specified (TODO) */
2987 qdict_put(target
, "file",
2988 qobject_ref(bs
->file
->bs
->full_open_options
));
2990 if (backing_overridden
) {
2992 qdict_put(target
, "backing",
2993 qobject_ref(bs
->backing
->bs
->full_open_options
));
2995 qdict_put_null(target
, "backing");
3000 static QemuOptsList vmdk_create_opts
= {
3001 .name
= "vmdk-create-opts",
3002 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
3005 .name
= BLOCK_OPT_SIZE
,
3006 .type
= QEMU_OPT_SIZE
,
3007 .help
= "Virtual disk size"
3010 .name
= BLOCK_OPT_ADAPTER_TYPE
,
3011 .type
= QEMU_OPT_STRING
,
3012 .help
= "Virtual adapter type, can be one of "
3013 "ide (default), lsilogic, buslogic or legacyESX"
3016 .name
= BLOCK_OPT_BACKING_FILE
,
3017 .type
= QEMU_OPT_STRING
,
3018 .help
= "File name of a base image"
3021 .name
= BLOCK_OPT_BACKING_FMT
,
3022 .type
= QEMU_OPT_STRING
,
3023 .help
= "Must be 'vmdk' if present",
3026 .name
= BLOCK_OPT_COMPAT6
,
3027 .type
= QEMU_OPT_BOOL
,
3028 .help
= "VMDK version 6 image",
3029 .def_value_str
= "off"
3032 .name
= BLOCK_OPT_HWVERSION
,
3033 .type
= QEMU_OPT_STRING
,
3034 .help
= "VMDK hardware version",
3035 .def_value_str
= "undefined"
3038 .name
= BLOCK_OPT_SUBFMT
,
3039 .type
= QEMU_OPT_STRING
,
3041 "VMDK flat extent format, can be one of "
3042 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
3045 .name
= BLOCK_OPT_ZEROED_GRAIN
,
3046 .type
= QEMU_OPT_BOOL
,
3047 .help
= "Enable efficient zero writes "
3048 "using the zeroed-grain GTE feature"
3050 { /* end of list */ }
3054 static BlockDriver bdrv_vmdk
= {
3055 .format_name
= "vmdk",
3056 .instance_size
= sizeof(BDRVVmdkState
),
3057 .bdrv_probe
= vmdk_probe
,
3058 .bdrv_open
= vmdk_open
,
3059 .bdrv_co_check
= vmdk_co_check
,
3060 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
3061 .bdrv_child_perm
= bdrv_default_perms
,
3062 .bdrv_co_preadv
= vmdk_co_preadv
,
3063 .bdrv_co_pwritev
= vmdk_co_pwritev
,
3064 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
3065 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
3066 .bdrv_close
= vmdk_close
,
3067 .bdrv_co_create_opts
= vmdk_co_create_opts
,
3068 .bdrv_co_create
= vmdk_co_create
,
3069 .bdrv_co_block_status
= vmdk_co_block_status
,
3070 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
3071 .bdrv_has_zero_init
= vmdk_has_zero_init
,
3072 .bdrv_get_specific_info
= vmdk_get_specific_info
,
3073 .bdrv_refresh_limits
= vmdk_refresh_limits
,
3074 .bdrv_get_info
= vmdk_get_info
,
3075 .bdrv_gather_child_options
= vmdk_gather_child_options
,
3078 .supports_backing
= true,
3079 .create_opts
= &vmdk_create_opts
,
3082 static void bdrv_vmdk_init(void)
3084 bdrv_register(&bdrv_vmdk
);
3087 block_init(bdrv_vmdk_init
);