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"
63 #define BLOCK_OPT_TOOLSVERSION "toolsversion"
68 uint32_t disk_sectors
;
70 uint32_t l1dir_offset
;
72 uint32_t file_sectors
;
75 uint32_t sectors_per_track
;
76 } QEMU_PACKED VMDK3Header
;
85 /* Number of GrainTableEntries per GrainTable */
86 uint32_t num_gtes_per_gt
;
89 uint64_t grain_offset
;
92 uint16_t compressAlgorithm
;
93 } QEMU_PACKED VMDK4Header
;
95 typedef struct VMDKSESparseConstHeader
{
100 uint64_t grain_table_size
;
106 uint64_t volatile_header_offset
;
107 uint64_t volatile_header_size
;
108 uint64_t journal_header_offset
;
109 uint64_t journal_header_size
;
110 uint64_t journal_offset
;
111 uint64_t journal_size
;
112 uint64_t grain_dir_offset
;
113 uint64_t grain_dir_size
;
114 uint64_t grain_tables_offset
;
115 uint64_t grain_tables_size
;
116 uint64_t free_bitmap_offset
;
117 uint64_t free_bitmap_size
;
118 uint64_t backmap_offset
;
119 uint64_t backmap_size
;
120 uint64_t grains_offset
;
121 uint64_t grains_size
;
123 } QEMU_PACKED VMDKSESparseConstHeader
;
125 typedef struct VMDKSESparseVolatileHeader
{
127 uint64_t free_gt_number
;
128 uint64_t next_txn_seq_number
;
129 uint64_t replay_journal
;
131 } QEMU_PACKED VMDKSESparseVolatileHeader
;
133 #define L2_CACHE_SIZE 16
135 typedef struct VmdkExtent
{
142 uint64_t sesparse_l2_tables_offset
;
143 uint64_t sesparse_clusters_offset
;
148 int64_t flat_start_offset
;
149 int64_t l1_table_offset
;
150 int64_t l1_backup_table_offset
;
152 uint32_t *l1_backup_table
;
153 unsigned int l1_size
;
154 uint32_t l1_entry_sectors
;
156 unsigned int l2_size
;
158 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
159 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
161 int64_t cluster_sectors
;
162 int64_t next_cluster_sector
;
166 typedef struct BDRVVmdkState
{
168 uint64_t desc_offset
;
174 /* Extent array with num_extents entries, ascend ordered by address */
176 Error
*migration_blocker
;
180 typedef struct VmdkMetaData
{
181 unsigned int l1_index
;
182 unsigned int l2_index
;
183 unsigned int l2_offset
;
185 uint32_t *l2_cache_entry
;
188 typedef struct VmdkGrainMarker
{
192 } QEMU_PACKED VmdkGrainMarker
;
195 MARKER_END_OF_STREAM
= 0,
196 MARKER_GRAIN_TABLE
= 1,
197 MARKER_GRAIN_DIRECTORY
= 2,
201 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
208 magic
= be32_to_cpu(*(uint32_t *)buf
);
209 if (magic
== VMDK3_MAGIC
||
210 magic
== VMDK4_MAGIC
) {
213 const char *p
= (const char *)buf
;
214 const char *end
= p
+ buf_size
;
217 /* skip comment line */
218 while (p
< end
&& *p
!= '\n') {
225 while (p
< end
&& *p
== ' ') {
228 /* skip '\r' if windows line endings used. */
229 if (p
< end
&& *p
== '\r') {
232 /* only accept blank lines before 'version=' line */
233 if (p
== end
|| *p
!= '\n') {
239 if (end
- p
>= strlen("version=X\n")) {
240 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
241 strncmp("version=2\n", p
, strlen("version=2\n")) == 0 ||
242 strncmp("version=3\n", p
, strlen("version=3\n")) == 0) {
246 if (end
- p
>= strlen("version=X\r\n")) {
247 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
248 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0 ||
249 strncmp("version=3\r\n", p
, strlen("version=3\r\n")) == 0) {
259 #define SECTOR_SIZE 512
260 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
261 #define BUF_SIZE 4096
262 #define HEADER_SIZE 512 /* first sector of 512 bytes */
264 static void vmdk_free_extents(BlockDriverState
*bs
)
267 BDRVVmdkState
*s
= bs
->opaque
;
270 for (i
= 0; i
< s
->num_extents
; i
++) {
274 g_free(e
->l1_backup_table
);
276 if (e
->file
!= bs
->file
) {
277 bdrv_unref_child(bs
, e
->file
);
283 static void vmdk_free_last_extent(BlockDriverState
*bs
)
285 BDRVVmdkState
*s
= bs
->opaque
;
287 if (s
->num_extents
== 0) {
291 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
294 /* Return -ve errno, or 0 on success and write CID into *pcid. */
295 static int vmdk_read_cid(BlockDriverState
*bs
, int parent
, uint32_t *pcid
)
299 const char *p_name
, *cid_str
;
301 BDRVVmdkState
*s
= bs
->opaque
;
304 desc
= g_malloc0(DESC_SIZE
);
305 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
311 cid_str
= "parentCID";
312 cid_str_size
= sizeof("parentCID");
315 cid_str_size
= sizeof("CID");
318 desc
[DESC_SIZE
- 1] = '\0';
319 p_name
= strstr(desc
, cid_str
);
320 if (p_name
== NULL
) {
324 p_name
+= cid_str_size
;
325 if (sscanf(p_name
, "%" SCNx32
, &cid
) != 1) {
337 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
339 char *desc
, *tmp_desc
;
340 char *p_name
, *tmp_str
;
341 BDRVVmdkState
*s
= bs
->opaque
;
344 desc
= g_malloc0(DESC_SIZE
);
345 tmp_desc
= g_malloc0(DESC_SIZE
);
346 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
351 desc
[DESC_SIZE
- 1] = '\0';
352 tmp_str
= strstr(desc
, "parentCID");
353 if (tmp_str
== NULL
) {
358 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
359 p_name
= strstr(desc
, "CID");
360 if (p_name
!= NULL
) {
361 p_name
+= sizeof("CID");
362 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
363 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
366 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
374 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
376 BDRVVmdkState
*s
= bs
->opaque
;
379 if (!s
->cid_checked
&& bs
->backing
) {
380 BlockDriverState
*p_bs
= bs
->backing
->bs
;
382 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
383 /* Backing file is not in vmdk format, so it does not have
384 * a CID, which makes the overlay's parent CID invalid */
388 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
389 /* read failure: report as not valid */
392 if (s
->parent_cid
!= cur_pcid
) {
397 s
->cid_checked
= true;
402 /* We have nothing to do for VMDK reopen, stubs just return success */
403 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
404 BlockReopenQueue
*queue
, Error
**errp
)
406 assert(state
!= NULL
);
407 assert(state
->bs
!= NULL
);
411 static int vmdk_parent_open(BlockDriverState
*bs
)
415 BDRVVmdkState
*s
= bs
->opaque
;
418 desc
= g_malloc0(DESC_SIZE
+ 1);
419 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
425 p_name
= strstr(desc
, "parentFileNameHint");
426 if (p_name
!= NULL
) {
429 p_name
+= sizeof("parentFileNameHint") + 1;
430 end_name
= strchr(p_name
, '\"');
431 if (end_name
== NULL
) {
435 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
440 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
441 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
442 bs
->auto_backing_file
);
443 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
),
452 /* Create and append extent to the extent array. Return the added VmdkExtent
453 * address. return NULL if allocation failed. */
454 static int vmdk_add_extent(BlockDriverState
*bs
,
455 BdrvChild
*file
, bool flat
, int64_t sectors
,
456 int64_t l1_offset
, int64_t l1_backup_offset
,
458 int l2_size
, uint64_t cluster_sectors
,
459 VmdkExtent
**new_extent
,
463 BDRVVmdkState
*s
= bs
->opaque
;
466 if (cluster_sectors
> 0x200000) {
467 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
468 error_setg(errp
, "Invalid granularity, image may be corrupt");
471 if (l1_size
> 32 * 1024 * 1024) {
473 * Although with big capacity and small l1_entry_sectors, we can get a
474 * big l1_size, we don't want unbounded value to allocate the table.
475 * Limit it to 32M, which is enough to store:
476 * 8TB - for both VMDK3 & VMDK4 with
477 * minimal cluster size: 512B
478 * minimal L2 table size: 512 entries
479 * 8 TB is still more than the maximal value supported for
480 * VMDK3 & VMDK4 which is 2TB.
481 * 64TB - for "ESXi seSparse Extent"
482 * minimal cluster size: 512B (default is 4KB)
483 * L2 table size: 4096 entries (const).
484 * 64TB is more than the maximal value supported for
485 * seSparse VMDKs (which is slightly less than 64TB)
487 error_setg(errp
, "L1 size too big");
491 nb_sectors
= bdrv_nb_sectors(file
->bs
);
492 if (nb_sectors
< 0) {
496 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
497 extent
= &s
->extents
[s
->num_extents
];
500 memset(extent
, 0, sizeof(VmdkExtent
));
503 extent
->sectors
= sectors
;
504 extent
->l1_table_offset
= l1_offset
;
505 extent
->l1_backup_table_offset
= l1_backup_offset
;
506 extent
->l1_size
= l1_size
;
507 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
508 extent
->l2_size
= l2_size
;
509 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
510 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
511 extent
->entry_size
= sizeof(uint32_t);
513 if (s
->num_extents
> 1) {
514 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
516 extent
->end_sector
= extent
->sectors
;
518 bs
->total_sectors
= extent
->end_sector
;
520 *new_extent
= extent
;
525 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
,
532 /* read the L1 table */
533 l1_size
= extent
->l1_size
* extent
->entry_size
;
534 extent
->l1_table
= g_try_malloc(l1_size
);
535 if (l1_size
&& extent
->l1_table
== NULL
) {
539 ret
= bdrv_pread(extent
->file
,
540 extent
->l1_table_offset
,
544 bdrv_refresh_filename(extent
->file
->bs
);
545 error_setg_errno(errp
, -ret
,
546 "Could not read l1 table from extent '%s'",
547 extent
->file
->bs
->filename
);
550 for (i
= 0; i
< extent
->l1_size
; i
++) {
551 if (extent
->entry_size
== sizeof(uint64_t)) {
552 le64_to_cpus((uint64_t *)extent
->l1_table
+ i
);
554 assert(extent
->entry_size
== sizeof(uint32_t));
555 le32_to_cpus((uint32_t *)extent
->l1_table
+ i
);
559 if (extent
->l1_backup_table_offset
) {
560 assert(!extent
->sesparse
);
561 extent
->l1_backup_table
= g_try_malloc(l1_size
);
562 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
566 ret
= bdrv_pread(extent
->file
,
567 extent
->l1_backup_table_offset
,
568 extent
->l1_backup_table
,
571 bdrv_refresh_filename(extent
->file
->bs
);
572 error_setg_errno(errp
, -ret
,
573 "Could not read l1 backup table from extent '%s'",
574 extent
->file
->bs
->filename
);
577 for (i
= 0; i
< extent
->l1_size
; i
++) {
578 le32_to_cpus(&extent
->l1_backup_table
[i
]);
583 g_malloc(extent
->entry_size
* extent
->l2_size
* L2_CACHE_SIZE
);
586 g_free(extent
->l1_backup_table
);
588 g_free(extent
->l1_table
);
592 static int vmdk_open_vmfs_sparse(BlockDriverState
*bs
,
594 int flags
, Error
**errp
)
599 VmdkExtent
*extent
= NULL
;
601 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
603 bdrv_refresh_filename(file
->bs
);
604 error_setg_errno(errp
, -ret
,
605 "Could not read header from file '%s'",
609 ret
= vmdk_add_extent(bs
, file
, false,
610 le32_to_cpu(header
.disk_sectors
),
611 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
613 le32_to_cpu(header
.l1dir_size
),
615 le32_to_cpu(header
.granularity
),
621 ret
= vmdk_init_tables(bs
, extent
, errp
);
623 /* free extent allocated by vmdk_add_extent */
624 vmdk_free_last_extent(bs
);
629 #define SESPARSE_CONST_HEADER_MAGIC UINT64_C(0x00000000cafebabe)
630 #define SESPARSE_VOLATILE_HEADER_MAGIC UINT64_C(0x00000000cafecafe)
632 /* Strict checks - format not officially documented */
633 static int check_se_sparse_const_header(VMDKSESparseConstHeader
*header
,
636 header
->magic
= le64_to_cpu(header
->magic
);
637 header
->version
= le64_to_cpu(header
->version
);
638 header
->grain_size
= le64_to_cpu(header
->grain_size
);
639 header
->grain_table_size
= le64_to_cpu(header
->grain_table_size
);
640 header
->flags
= le64_to_cpu(header
->flags
);
641 header
->reserved1
= le64_to_cpu(header
->reserved1
);
642 header
->reserved2
= le64_to_cpu(header
->reserved2
);
643 header
->reserved3
= le64_to_cpu(header
->reserved3
);
644 header
->reserved4
= le64_to_cpu(header
->reserved4
);
646 header
->volatile_header_offset
=
647 le64_to_cpu(header
->volatile_header_offset
);
648 header
->volatile_header_size
= le64_to_cpu(header
->volatile_header_size
);
650 header
->journal_header_offset
= le64_to_cpu(header
->journal_header_offset
);
651 header
->journal_header_size
= le64_to_cpu(header
->journal_header_size
);
653 header
->journal_offset
= le64_to_cpu(header
->journal_offset
);
654 header
->journal_size
= le64_to_cpu(header
->journal_size
);
656 header
->grain_dir_offset
= le64_to_cpu(header
->grain_dir_offset
);
657 header
->grain_dir_size
= le64_to_cpu(header
->grain_dir_size
);
659 header
->grain_tables_offset
= le64_to_cpu(header
->grain_tables_offset
);
660 header
->grain_tables_size
= le64_to_cpu(header
->grain_tables_size
);
662 header
->free_bitmap_offset
= le64_to_cpu(header
->free_bitmap_offset
);
663 header
->free_bitmap_size
= le64_to_cpu(header
->free_bitmap_size
);
665 header
->backmap_offset
= le64_to_cpu(header
->backmap_offset
);
666 header
->backmap_size
= le64_to_cpu(header
->backmap_size
);
668 header
->grains_offset
= le64_to_cpu(header
->grains_offset
);
669 header
->grains_size
= le64_to_cpu(header
->grains_size
);
671 if (header
->magic
!= SESPARSE_CONST_HEADER_MAGIC
) {
672 error_setg(errp
, "Bad const header magic: 0x%016" PRIx64
,
677 if (header
->version
!= 0x0000000200000001) {
678 error_setg(errp
, "Unsupported version: 0x%016" PRIx64
,
683 if (header
->grain_size
!= 8) {
684 error_setg(errp
, "Unsupported grain size: %" PRIu64
,
689 if (header
->grain_table_size
!= 64) {
690 error_setg(errp
, "Unsupported grain table size: %" PRIu64
,
691 header
->grain_table_size
);
695 if (header
->flags
!= 0) {
696 error_setg(errp
, "Unsupported flags: 0x%016" PRIx64
,
701 if (header
->reserved1
!= 0 || header
->reserved2
!= 0 ||
702 header
->reserved3
!= 0 || header
->reserved4
!= 0) {
703 error_setg(errp
, "Unsupported reserved bits:"
704 " 0x%016" PRIx64
" 0x%016" PRIx64
705 " 0x%016" PRIx64
" 0x%016" PRIx64
,
706 header
->reserved1
, header
->reserved2
,
707 header
->reserved3
, header
->reserved4
);
711 /* check that padding is 0 */
712 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
713 error_setg(errp
, "Unsupported non-zero const header padding");
720 static int check_se_sparse_volatile_header(VMDKSESparseVolatileHeader
*header
,
723 header
->magic
= le64_to_cpu(header
->magic
);
724 header
->free_gt_number
= le64_to_cpu(header
->free_gt_number
);
725 header
->next_txn_seq_number
= le64_to_cpu(header
->next_txn_seq_number
);
726 header
->replay_journal
= le64_to_cpu(header
->replay_journal
);
728 if (header
->magic
!= SESPARSE_VOLATILE_HEADER_MAGIC
) {
729 error_setg(errp
, "Bad volatile header magic: 0x%016" PRIx64
,
734 if (header
->replay_journal
) {
735 error_setg(errp
, "Image is dirty, Replaying journal not supported");
739 /* check that padding is 0 */
740 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
741 error_setg(errp
, "Unsupported non-zero volatile header padding");
748 static int vmdk_open_se_sparse(BlockDriverState
*bs
,
750 int flags
, Error
**errp
)
753 VMDKSESparseConstHeader const_header
;
754 VMDKSESparseVolatileHeader volatile_header
;
755 VmdkExtent
*extent
= NULL
;
757 ret
= bdrv_apply_auto_read_only(bs
,
758 "No write support for seSparse images available", errp
);
763 assert(sizeof(const_header
) == SECTOR_SIZE
);
765 ret
= bdrv_pread(file
, 0, &const_header
, sizeof(const_header
));
767 bdrv_refresh_filename(file
->bs
);
768 error_setg_errno(errp
, -ret
,
769 "Could not read const header from file '%s'",
774 /* check const header */
775 ret
= check_se_sparse_const_header(&const_header
, errp
);
780 assert(sizeof(volatile_header
) == SECTOR_SIZE
);
782 ret
= bdrv_pread(file
,
783 const_header
.volatile_header_offset
* SECTOR_SIZE
,
784 &volatile_header
, sizeof(volatile_header
));
786 bdrv_refresh_filename(file
->bs
);
787 error_setg_errno(errp
, -ret
,
788 "Could not read volatile header from file '%s'",
793 /* check volatile header */
794 ret
= check_se_sparse_volatile_header(&volatile_header
, errp
);
799 ret
= vmdk_add_extent(bs
, file
, false,
800 const_header
.capacity
,
801 const_header
.grain_dir_offset
* SECTOR_SIZE
,
803 const_header
.grain_dir_size
*
804 SECTOR_SIZE
/ sizeof(uint64_t),
805 const_header
.grain_table_size
*
806 SECTOR_SIZE
/ sizeof(uint64_t),
807 const_header
.grain_size
,
814 extent
->sesparse
= true;
815 extent
->sesparse_l2_tables_offset
= const_header
.grain_tables_offset
;
816 extent
->sesparse_clusters_offset
= const_header
.grains_offset
;
817 extent
->entry_size
= sizeof(uint64_t);
819 ret
= vmdk_init_tables(bs
, extent
, errp
);
821 /* free extent allocated by vmdk_add_extent */
822 vmdk_free_last_extent(bs
);
828 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
829 QDict
*options
, Error
**errp
);
831 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
837 size
= bdrv_getlength(file
->bs
);
839 error_setg_errno(errp
, -size
, "Could not access file");
844 /* Both descriptor file and sparse image must be much larger than 4
845 * bytes, also callers of vmdk_read_desc want to compare the first 4
846 * bytes with VMDK4_MAGIC, let's error out if less is read. */
847 error_setg(errp
, "File is too small, not a valid image");
851 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
852 buf
= g_malloc(size
+ 1);
854 ret
= bdrv_pread(file
, desc_offset
, buf
, size
);
856 error_setg_errno(errp
, -ret
, "Could not read from file");
865 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
867 int flags
, QDict
*options
, Error
**errp
)
871 uint32_t l1_size
, l1_entry_sectors
;
873 VmdkExtent
*extent
= NULL
;
874 BDRVVmdkState
*s
= bs
->opaque
;
875 int64_t l1_backup_offset
= 0;
878 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
880 bdrv_refresh_filename(file
->bs
);
881 error_setg_errno(errp
, -ret
,
882 "Could not read header from file '%s'",
886 if (header
.capacity
== 0) {
887 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
889 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
893 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
899 if (!s
->create_type
) {
900 s
->create_type
= g_strdup("monolithicSparse");
903 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
905 * The footer takes precedence over the header, so read it in. The
906 * footer starts at offset -1024 from the end: One sector for the
907 * footer, and another one for the end-of-stream marker.
914 uint8_t pad
[512 - 16];
915 } QEMU_PACKED footer_marker
;
919 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
925 uint8_t pad
[512 - 16];
926 } QEMU_PACKED eos_marker
;
927 } QEMU_PACKED footer
;
929 ret
= bdrv_pread(file
,
930 bs
->file
->bs
->total_sectors
* 512 - 1536,
931 &footer
, sizeof(footer
));
933 error_setg_errno(errp
, -ret
, "Failed to read footer");
937 /* Some sanity checks for the footer */
938 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
939 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
940 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
941 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
942 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
943 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
945 error_setg(errp
, "Invalid footer");
949 header
= footer
.header
;
953 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
954 if (le32_to_cpu(header
.version
) > 3) {
955 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
956 le32_to_cpu(header
.version
));
958 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
960 /* VMware KB 2064959 explains that version 3 added support for
961 * persistent changed block tracking (CBT), and backup software can
962 * read it as version=1 if it doesn't care about the changed area
963 * information. So we are safe to enable read only. */
964 error_setg(errp
, "VMDK version 3 must be read only");
968 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
969 error_setg(errp
, "L2 table size too big");
973 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
974 * le64_to_cpu(header
.granularity
);
975 if (l1_entry_sectors
== 0) {
976 error_setg(errp
, "L1 entry size is invalid");
979 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
981 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
982 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
984 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
985 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
986 (int64_t)(le64_to_cpu(header
.grain_offset
)
987 * BDRV_SECTOR_SIZE
));
991 ret
= vmdk_add_extent(bs
, file
, false,
992 le64_to_cpu(header
.capacity
),
993 le64_to_cpu(header
.gd_offset
) << 9,
996 le32_to_cpu(header
.num_gtes_per_gt
),
997 le64_to_cpu(header
.granularity
),
1003 extent
->compressed
=
1004 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1005 if (extent
->compressed
) {
1006 g_free(s
->create_type
);
1007 s
->create_type
= g_strdup("streamOptimized");
1009 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
1010 extent
->version
= le32_to_cpu(header
.version
);
1011 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
1012 ret
= vmdk_init_tables(bs
, extent
, errp
);
1014 /* free extent allocated by vmdk_add_extent */
1015 vmdk_free_last_extent(bs
);
1020 /* find an option value out of descriptor file */
1021 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
1022 char *buf
, int buf_size
)
1024 char *opt_pos
, *opt_end
;
1025 const char *end
= desc
+ strlen(desc
);
1027 opt_pos
= strstr(desc
, opt_name
);
1031 /* Skip "=\"" following opt_name */
1032 opt_pos
+= strlen(opt_name
) + 2;
1033 if (opt_pos
>= end
) {
1037 while (opt_end
< end
&& *opt_end
!= '"') {
1040 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
1043 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
1047 /* Open an extent file and append to bs array */
1048 static int vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
1049 char *buf
, QDict
*options
, Error
**errp
)
1053 magic
= ldl_be_p(buf
);
1056 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
1058 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
1060 error_setg(errp
, "Image not in VMDK format");
1065 static const char *next_line(const char *s
)
1076 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
1077 QDict
*options
, Error
**errp
)
1085 int64_t sectors
= 0;
1086 int64_t flat_offset
;
1087 char *desc_file_dir
= NULL
;
1089 BdrvChild
*extent_file
;
1090 BdrvChildRole extent_role
;
1091 BDRVVmdkState
*s
= bs
->opaque
;
1092 VmdkExtent
*extent
= NULL
;
1093 char extent_opt_prefix
[32];
1094 Error
*local_err
= NULL
;
1096 for (p
= desc
; *p
; p
= next_line(p
)) {
1097 /* parse extent line in one of below formats:
1099 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
1100 * RW [size in sectors] SPARSE "file-name.vmdk"
1101 * RW [size in sectors] VMFS "file-name.vmdk"
1102 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
1103 * RW [size in sectors] SESPARSE "file-name.vmdk"
1106 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
1107 access
, §ors
, type
, fname
, &flat_offset
);
1108 if (matches
< 4 || strcmp(access
, "RW")) {
1110 } else if (!strcmp(type
, "FLAT")) {
1111 if (matches
!= 5 || flat_offset
< 0) {
1114 } else if (!strcmp(type
, "VMFS")) {
1120 } else if (matches
!= 4) {
1125 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
1126 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE") &&
1127 strcmp(type
, "SESPARSE")) ||
1128 (strcmp(access
, "RW"))) {
1132 if (path_is_absolute(fname
)) {
1133 extent_path
= g_strdup(fname
);
1135 if (!desc_file_dir
) {
1136 desc_file_dir
= bdrv_dirname(bs
->file
->bs
, errp
);
1137 if (!desc_file_dir
) {
1138 bdrv_refresh_filename(bs
->file
->bs
);
1139 error_prepend(errp
, "Cannot use relative paths with VMDK "
1140 "descriptor file '%s': ",
1141 bs
->file
->bs
->filename
);
1147 extent_path
= g_strconcat(desc_file_dir
, fname
, NULL
);
1150 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
1153 extent_role
= BDRV_CHILD_DATA
;
1154 if (strcmp(type
, "FLAT") != 0 && strcmp(type
, "VMFS") != 0) {
1155 /* non-flat extents have metadata */
1156 extent_role
|= BDRV_CHILD_METADATA
;
1159 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
1160 bs
, &child_of_bds
, extent_role
, false,
1162 g_free(extent_path
);
1164 error_propagate(errp
, local_err
);
1169 /* save to extents array */
1170 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
1173 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
1174 0, 0, 0, 0, 0, &extent
, errp
);
1176 bdrv_unref_child(bs
, extent_file
);
1179 extent
->flat_start_offset
= flat_offset
<< 9;
1180 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
1181 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
1182 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
1186 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
1191 bdrv_unref_child(bs
, extent_file
);
1194 extent
= &s
->extents
[s
->num_extents
- 1];
1195 } else if (!strcmp(type
, "SESPARSE")) {
1196 ret
= vmdk_open_se_sparse(bs
, extent_file
, bs
->open_flags
, errp
);
1198 bdrv_unref_child(bs
, extent_file
);
1201 extent
= &s
->extents
[s
->num_extents
- 1];
1203 error_setg(errp
, "Unsupported extent type '%s'", type
);
1204 bdrv_unref_child(bs
, extent_file
);
1208 extent
->type
= g_strdup(type
);
1217 if (np
[-1] == '\n') {
1220 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
1224 g_free(desc_file_dir
);
1228 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
1229 QDict
*options
, Error
**errp
)
1233 BDRVVmdkState
*s
= bs
->opaque
;
1235 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
1236 error_setg(errp
, "invalid VMDK image descriptor");
1240 if (strcmp(ct
, "monolithicFlat") &&
1241 strcmp(ct
, "vmfs") &&
1242 strcmp(ct
, "vmfsSparse") &&
1243 strcmp(ct
, "seSparse") &&
1244 strcmp(ct
, "twoGbMaxExtentSparse") &&
1245 strcmp(ct
, "twoGbMaxExtentFlat")) {
1246 error_setg(errp
, "Unsupported image type '%s'", ct
);
1250 s
->create_type
= g_strdup(ct
);
1252 ret
= vmdk_parse_extents(buf
, bs
, options
, errp
);
1257 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1262 BDRVVmdkState
*s
= bs
->opaque
;
1265 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
1266 BDRV_CHILD_IMAGE
, false, errp
);
1271 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
1276 magic
= ldl_be_p(buf
);
1280 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
1282 s
->desc_offset
= 0x200;
1285 /* No data in the descriptor file */
1286 bs
->file
->role
&= ~BDRV_CHILD_DATA
;
1288 /* Must succeed because we have given up permissions if anything */
1289 bdrv_child_refresh_perms(bs
, bs
->file
, &error_abort
);
1291 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1298 /* try to open parent images, if exist */
1299 ret
= vmdk_parent_open(bs
);
1303 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1307 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1311 qemu_co_mutex_init(&s
->lock
);
1313 /* Disable migration when VMDK images are used */
1314 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1315 "does not support live migration",
1316 bdrv_get_device_or_node_name(bs
));
1317 ret
= migrate_add_blocker(s
->migration_blocker
, errp
);
1319 error_free(s
->migration_blocker
);
1328 g_free(s
->create_type
);
1329 s
->create_type
= NULL
;
1330 vmdk_free_extents(bs
);
1335 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1337 BDRVVmdkState
*s
= bs
->opaque
;
1340 for (i
= 0; i
< s
->num_extents
; i
++) {
1341 if (!s
->extents
[i
].flat
) {
1342 bs
->bl
.pwrite_zeroes_alignment
=
1343 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1344 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1352 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1353 * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting
1354 * a zeroed cluster in the current layer and must not copy data from the
1357 * If @skip_start_sector < @skip_end_sector, the relative range
1358 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1359 * it for call to write user data in the request.
1361 static int get_whole_cluster(BlockDriverState
*bs
,
1363 uint64_t cluster_offset
,
1365 uint64_t skip_start_bytes
,
1366 uint64_t skip_end_bytes
,
1370 int64_t cluster_bytes
;
1371 uint8_t *whole_grain
;
1372 bool copy_from_backing
;
1374 /* For COW, align request sector_num to cluster start */
1375 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1376 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1377 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1378 copy_from_backing
= bs
->backing
&& !zeroed
;
1380 if (!copy_from_backing
) {
1381 memset(whole_grain
, 0, skip_start_bytes
);
1382 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1385 assert(skip_end_bytes
<= cluster_bytes
);
1386 /* we will be here if it's first write on non-exist grain(cluster).
1387 * try to read from parent image, if exist */
1388 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1393 /* Read backing data before skip range */
1394 if (skip_start_bytes
> 0) {
1395 if (copy_from_backing
) {
1396 /* qcow2 emits this on bs->file instead of bs->backing */
1397 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1398 ret
= bdrv_pread(bs
->backing
, offset
, whole_grain
,
1405 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1406 ret
= bdrv_pwrite(extent
->file
, cluster_offset
, whole_grain
,
1413 /* Read backing data after skip range */
1414 if (skip_end_bytes
< cluster_bytes
) {
1415 if (copy_from_backing
) {
1416 /* qcow2 emits this on bs->file instead of bs->backing */
1417 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_READ
);
1418 ret
= bdrv_pread(bs
->backing
, offset
+ skip_end_bytes
,
1419 whole_grain
+ skip_end_bytes
,
1420 cluster_bytes
- skip_end_bytes
);
1426 BLKDBG_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1427 ret
= bdrv_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1428 whole_grain
+ skip_end_bytes
,
1429 cluster_bytes
- skip_end_bytes
);
1438 qemu_vfree(whole_grain
);
1442 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
,
1445 offset
= cpu_to_le32(offset
);
1446 /* update L2 table */
1447 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1448 if (bdrv_pwrite(extent
->file
,
1449 ((int64_t)m_data
->l2_offset
* 512)
1450 + (m_data
->l2_index
* sizeof(offset
)),
1451 &offset
, sizeof(offset
)) < 0) {
1454 /* update backup L2 table */
1455 if (extent
->l1_backup_table_offset
!= 0) {
1456 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1457 if (bdrv_pwrite(extent
->file
,
1458 ((int64_t)m_data
->l2_offset
* 512)
1459 + (m_data
->l2_index
* sizeof(offset
)),
1460 &offset
, sizeof(offset
)) < 0) {
1464 if (bdrv_flush(extent
->file
->bs
) < 0) {
1467 if (m_data
->l2_cache_entry
) {
1468 *m_data
->l2_cache_entry
= offset
;
1475 * get_cluster_offset
1477 * Look up cluster offset in extent file by sector number, and store in
1480 * For flat extents, the start offset as parsed from the description file is
1483 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1484 * offset for a new cluster and update L2 cache. If there is a backing file,
1485 * COW is done before returning; otherwise, zeroes are written to the allocated
1486 * cluster. Both COW and zero writing skips the sector range
1487 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1488 * has new data to write there.
1490 * Returns: VMDK_OK if cluster exists and mapped in the image.
1491 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1492 * VMDK_ERROR if failed.
1494 static int get_cluster_offset(BlockDriverState
*bs
,
1496 VmdkMetaData
*m_data
,
1499 uint64_t *cluster_offset
,
1500 uint64_t skip_start_bytes
,
1501 uint64_t skip_end_bytes
)
1503 unsigned int l1_index
, l2_offset
, l2_index
;
1504 int min_index
, i
, j
;
1507 bool zeroed
= false;
1509 int64_t cluster_sector
;
1510 unsigned int l2_size_bytes
= extent
->l2_size
* extent
->entry_size
;
1513 m_data
->new_allocation
= false;
1516 *cluster_offset
= extent
->flat_start_offset
;
1520 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1521 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1522 if (l1_index
>= extent
->l1_size
) {
1525 if (extent
->sesparse
) {
1526 uint64_t l2_offset_u64
;
1528 assert(extent
->entry_size
== sizeof(uint64_t));
1530 l2_offset_u64
= ((uint64_t *)extent
->l1_table
)[l1_index
];
1531 if (l2_offset_u64
== 0) {
1533 } else if ((l2_offset_u64
& 0xffffffff00000000) != 0x1000000000000000) {
1535 * Top most nibble is 0x1 if grain table is allocated.
1536 * strict check - top most 4 bytes must be 0x10000000 since max
1537 * supported size is 64TB for disk - so no more than 64TB / 16MB
1538 * grain directories which is smaller than uint32,
1539 * where 16MB is the only supported default grain table coverage.
1543 l2_offset_u64
= l2_offset_u64
& 0x00000000ffffffff;
1544 l2_offset_u64
= extent
->sesparse_l2_tables_offset
+
1545 l2_offset_u64
* l2_size_bytes
/ SECTOR_SIZE
;
1546 if (l2_offset_u64
> 0x00000000ffffffff) {
1549 l2_offset
= (unsigned int)(l2_offset_u64
);
1552 assert(extent
->entry_size
== sizeof(uint32_t));
1553 l2_offset
= ((uint32_t *)extent
->l1_table
)[l1_index
];
1556 return VMDK_UNALLOC
;
1558 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1559 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1560 /* increment the hit count */
1561 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1562 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1563 extent
->l2_cache_counts
[j
] >>= 1;
1566 l2_table
= (char *)extent
->l2_cache
+ (i
* l2_size_bytes
);
1570 /* not found: load a new entry in the least used one */
1572 min_count
= 0xffffffff;
1573 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1574 if (extent
->l2_cache_counts
[i
] < min_count
) {
1575 min_count
= extent
->l2_cache_counts
[i
];
1579 l2_table
= (char *)extent
->l2_cache
+ (min_index
* l2_size_bytes
);
1580 BLKDBG_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1581 if (bdrv_pread(extent
->file
,
1582 (int64_t)l2_offset
* 512,
1585 ) != l2_size_bytes
) {
1589 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1590 extent
->l2_cache_counts
[min_index
] = 1;
1592 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1594 m_data
->l1_index
= l1_index
;
1595 m_data
->l2_index
= l2_index
;
1596 m_data
->l2_offset
= l2_offset
;
1597 m_data
->l2_cache_entry
= ((uint32_t *)l2_table
) + l2_index
;
1600 if (extent
->sesparse
) {
1601 cluster_sector
= le64_to_cpu(((uint64_t *)l2_table
)[l2_index
]);
1602 switch (cluster_sector
& 0xf000000000000000) {
1603 case 0x0000000000000000:
1604 /* unallocated grain */
1605 if (cluster_sector
!= 0) {
1609 case 0x1000000000000000:
1610 /* scsi-unmapped grain - fallthrough */
1611 case 0x2000000000000000:
1615 case 0x3000000000000000:
1616 /* allocated grain */
1617 cluster_sector
= (((cluster_sector
& 0x0fff000000000000) >> 48) |
1618 ((cluster_sector
& 0x0000ffffffffffff) << 12));
1619 cluster_sector
= extent
->sesparse_clusters_offset
+
1620 cluster_sector
* extent
->cluster_sectors
;
1626 cluster_sector
= le32_to_cpu(((uint32_t *)l2_table
)[l2_index
]);
1628 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1633 if (!cluster_sector
|| zeroed
) {
1635 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1637 assert(!extent
->sesparse
);
1639 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1643 cluster_sector
= extent
->next_cluster_sector
;
1644 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1646 /* First of all we write grain itself, to avoid race condition
1647 * that may to corrupt the image.
1648 * This problem may occur because of insufficient space on host disk
1649 * or inappropriate VM shutdown.
1651 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1652 offset
, skip_start_bytes
, skip_end_bytes
,
1658 m_data
->new_allocation
= true;
1661 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1665 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1666 int64_t sector_num
, VmdkExtent
*start_hint
)
1668 VmdkExtent
*extent
= start_hint
;
1671 extent
= &s
->extents
[0];
1673 while (extent
< &s
->extents
[s
->num_extents
]) {
1674 if (sector_num
< extent
->end_sector
) {
1682 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1685 uint64_t extent_begin_offset
, extent_relative_offset
;
1686 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1688 extent_begin_offset
=
1689 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1690 extent_relative_offset
= offset
- extent_begin_offset
;
1691 return extent_relative_offset
% cluster_size
;
1694 static int coroutine_fn
vmdk_co_block_status(BlockDriverState
*bs
,
1696 int64_t offset
, int64_t bytes
,
1697 int64_t *pnum
, int64_t *map
,
1698 BlockDriverState
**file
)
1700 BDRVVmdkState
*s
= bs
->opaque
;
1701 int64_t index_in_cluster
, n
, ret
;
1702 uint64_t cluster_offset
;
1705 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1709 qemu_co_mutex_lock(&s
->lock
);
1710 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1712 qemu_co_mutex_unlock(&s
->lock
);
1714 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1723 ret
= BDRV_BLOCK_ZERO
;
1726 ret
= BDRV_BLOCK_DATA
;
1727 if (!extent
->compressed
) {
1728 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1729 *map
= cluster_offset
+ index_in_cluster
;
1731 ret
|= BDRV_BLOCK_RECURSE
;
1734 *file
= extent
->file
->bs
;
1738 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1739 *pnum
= MIN(n
, bytes
);
1743 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1744 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1745 uint64_t qiov_offset
, uint64_t n_bytes
,
1749 VmdkGrainMarker
*data
= NULL
;
1751 QEMUIOVector local_qiov
;
1752 int64_t write_offset
;
1753 int64_t write_end_sector
;
1755 if (extent
->compressed
) {
1756 void *compressed_data
;
1758 /* Only whole clusters */
1759 if (offset_in_cluster
||
1760 n_bytes
> (extent
->cluster_sectors
* SECTOR_SIZE
) ||
1761 (n_bytes
< (extent
->cluster_sectors
* SECTOR_SIZE
) &&
1762 offset
+ n_bytes
!= extent
->end_sector
* SECTOR_SIZE
))
1768 if (!extent
->has_marker
) {
1772 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1773 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1775 compressed_data
= g_malloc(n_bytes
);
1776 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1777 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1778 g_free(compressed_data
);
1780 if (ret
!= Z_OK
|| buf_len
== 0) {
1785 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1786 data
->size
= cpu_to_le32(buf_len
);
1788 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1789 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1791 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1793 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1794 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1796 BLKDBG_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1799 write_offset
= cluster_offset
+ offset_in_cluster
;
1800 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1803 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1805 if (extent
->compressed
) {
1806 extent
->next_cluster_sector
= write_end_sector
;
1808 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1818 if (!extent
->compressed
) {
1819 qemu_iovec_destroy(&local_qiov
);
1824 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1825 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1829 int cluster_bytes
, buf_bytes
;
1830 uint8_t *cluster_buf
, *compressed_data
;
1831 uint8_t *uncomp_buf
;
1833 VmdkGrainMarker
*marker
;
1837 if (!extent
->compressed
) {
1838 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1839 ret
= bdrv_co_preadv(extent
->file
,
1840 cluster_offset
+ offset_in_cluster
, bytes
,
1847 cluster_bytes
= extent
->cluster_sectors
* 512;
1848 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1849 buf_bytes
= cluster_bytes
* 2;
1850 cluster_buf
= g_malloc(buf_bytes
);
1851 uncomp_buf
= g_malloc(cluster_bytes
);
1852 BLKDBG_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1853 ret
= bdrv_pread(extent
->file
,
1855 cluster_buf
, buf_bytes
);
1859 compressed_data
= cluster_buf
;
1860 buf_len
= cluster_bytes
;
1861 data_len
= cluster_bytes
;
1862 if (extent
->has_marker
) {
1863 marker
= (VmdkGrainMarker
*)cluster_buf
;
1864 compressed_data
= marker
->data
;
1865 data_len
= le32_to_cpu(marker
->size
);
1867 if (!data_len
|| data_len
> buf_bytes
) {
1871 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1877 if (offset_in_cluster
< 0 ||
1878 offset_in_cluster
+ bytes
> buf_len
) {
1882 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1887 g_free(cluster_buf
);
1891 static int coroutine_fn
1892 vmdk_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1893 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1895 BDRVVmdkState
*s
= bs
->opaque
;
1897 uint64_t n_bytes
, offset_in_cluster
;
1898 VmdkExtent
*extent
= NULL
;
1899 QEMUIOVector local_qiov
;
1900 uint64_t cluster_offset
;
1901 uint64_t bytes_done
= 0;
1903 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1904 qemu_co_mutex_lock(&s
->lock
);
1907 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1912 ret
= get_cluster_offset(bs
, extent
, NULL
,
1913 offset
, false, &cluster_offset
, 0, 0);
1914 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1916 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1917 - offset_in_cluster
);
1919 if (ret
!= VMDK_OK
) {
1920 /* if not allocated, try to read from parent image, if exist */
1921 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1922 if (!vmdk_is_cid_valid(bs
)) {
1927 qemu_iovec_reset(&local_qiov
);
1928 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1930 /* qcow2 emits this on bs->file instead of bs->backing */
1931 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
1932 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
1938 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
1941 qemu_iovec_reset(&local_qiov
);
1942 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1944 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
1945 &local_qiov
, n_bytes
);
1952 bytes_done
+= n_bytes
;
1957 qemu_co_mutex_unlock(&s
->lock
);
1958 qemu_iovec_destroy(&local_qiov
);
1965 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1966 * if possible, otherwise return -ENOTSUP.
1967 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1968 * with each cluster. By dry run we can find if the zero write
1969 * is possible without modifying image data.
1971 * Returns: error code with 0 for success.
1973 static int vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1974 uint64_t bytes
, QEMUIOVector
*qiov
,
1975 bool zeroed
, bool zero_dry_run
)
1977 BDRVVmdkState
*s
= bs
->opaque
;
1978 VmdkExtent
*extent
= NULL
;
1980 int64_t offset_in_cluster
, n_bytes
;
1981 uint64_t cluster_offset
;
1982 uint64_t bytes_done
= 0;
1983 VmdkMetaData m_data
;
1985 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
1986 error_report("Wrong offset: offset=0x%" PRIx64
1987 " total_sectors=0x%" PRIx64
,
1988 offset
, bs
->total_sectors
);
1993 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1997 if (extent
->sesparse
) {
2000 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
2001 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
2002 - offset_in_cluster
);
2004 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2005 !(extent
->compressed
|| zeroed
),
2006 &cluster_offset
, offset_in_cluster
,
2007 offset_in_cluster
+ n_bytes
);
2008 if (extent
->compressed
) {
2009 if (ret
== VMDK_OK
) {
2010 /* Refuse write to allocated cluster for streamOptimized */
2011 error_report("Could not write to allocated cluster"
2012 " for streamOptimized");
2014 } else if (!zeroed
) {
2016 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2017 true, &cluster_offset
, 0, 0);
2020 if (ret
== VMDK_ERROR
) {
2024 /* Do zeroed write, buf is ignored */
2025 if (extent
->has_zero_grain
&&
2026 offset_in_cluster
== 0 &&
2027 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
2028 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
2029 if (!zero_dry_run
&& ret
!= VMDK_ZEROED
) {
2030 /* update L2 tables */
2031 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
2040 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
2041 qiov
, bytes_done
, n_bytes
, offset
);
2045 if (m_data
.new_allocation
) {
2046 /* update L2 tables */
2047 if (vmdk_L2update(extent
, &m_data
,
2048 cluster_offset
>> BDRV_SECTOR_BITS
)
2056 bytes_done
+= n_bytes
;
2058 /* update CID on the first write every time the virtual disk is
2060 if (!s
->cid_updated
) {
2061 ret
= vmdk_write_cid(bs
, g_random_int());
2065 s
->cid_updated
= true;
2071 static int coroutine_fn
2072 vmdk_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2073 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
2076 BDRVVmdkState
*s
= bs
->opaque
;
2077 qemu_co_mutex_lock(&s
->lock
);
2078 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
2079 qemu_co_mutex_unlock(&s
->lock
);
2083 static int coroutine_fn
2084 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2088 /* The caller will write bytes 0 to signal EOF.
2089 * When receive it, we align EOF to a sector boundary. */
2090 BDRVVmdkState
*s
= bs
->opaque
;
2094 for (i
= 0; i
< s
->num_extents
; i
++) {
2095 length
= bdrv_getlength(s
->extents
[i
].file
->bs
);
2099 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
2100 ret
= bdrv_truncate(s
->extents
[i
].file
, length
, false,
2101 PREALLOC_MODE_OFF
, 0, NULL
);
2108 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
2111 static int coroutine_fn
vmdk_co_pwrite_zeroes(BlockDriverState
*bs
,
2114 BdrvRequestFlags flags
)
2117 BDRVVmdkState
*s
= bs
->opaque
;
2119 qemu_co_mutex_lock(&s
->lock
);
2120 /* write zeroes could fail if sectors not aligned to cluster, test it with
2121 * dry_run == true before really updating image */
2122 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
2124 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
2126 qemu_co_mutex_unlock(&s
->lock
);
2130 static int vmdk_init_extent(BlockBackend
*blk
,
2131 int64_t filesize
, bool flat
,
2132 bool compress
, bool zeroed_grain
,
2137 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
2138 uint32_t *gd_buf
= NULL
;
2142 ret
= blk_truncate(blk
, filesize
, false, PREALLOC_MODE_OFF
, 0, errp
);
2145 magic
= cpu_to_be32(VMDK4_MAGIC
);
2146 memset(&header
, 0, sizeof(header
));
2149 } else if (zeroed_grain
) {
2154 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
2155 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
2156 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
2157 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
2158 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
2159 header
.granularity
= 128;
2160 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
2162 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
2163 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
2165 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
2166 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
2168 header
.desc_offset
= 1;
2169 header
.desc_size
= 20;
2170 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
2171 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
2172 header
.grain_offset
=
2173 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
2174 header
.granularity
);
2175 /* swap endianness for all header fields */
2176 header
.version
= cpu_to_le32(header
.version
);
2177 header
.flags
= cpu_to_le32(header
.flags
);
2178 header
.capacity
= cpu_to_le64(header
.capacity
);
2179 header
.granularity
= cpu_to_le64(header
.granularity
);
2180 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
2181 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
2182 header
.desc_size
= cpu_to_le64(header
.desc_size
);
2183 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
2184 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
2185 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
2186 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
2188 header
.check_bytes
[0] = 0xa;
2189 header
.check_bytes
[1] = 0x20;
2190 header
.check_bytes
[2] = 0xd;
2191 header
.check_bytes
[3] = 0xa;
2193 /* write all the data */
2194 ret
= blk_pwrite(blk
, 0, &magic
, sizeof(magic
), 0);
2196 error_setg(errp
, QERR_IO_ERROR
);
2199 ret
= blk_pwrite(blk
, sizeof(magic
), &header
, sizeof(header
), 0);
2201 error_setg(errp
, QERR_IO_ERROR
);
2205 ret
= blk_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9, false,
2206 PREALLOC_MODE_OFF
, 0, errp
);
2211 /* write grain directory */
2212 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
2213 gd_buf
= g_malloc0(gd_buf_size
);
2214 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
2215 i
< gt_count
; i
++, tmp
+= gt_size
) {
2216 gd_buf
[i
] = cpu_to_le32(tmp
);
2218 ret
= blk_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
2219 gd_buf
, gd_buf_size
, 0);
2221 error_setg(errp
, QERR_IO_ERROR
);
2225 /* write backup grain directory */
2226 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
2227 i
< gt_count
; i
++, tmp
+= gt_size
) {
2228 gd_buf
[i
] = cpu_to_le32(tmp
);
2230 ret
= blk_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
2231 gd_buf
, gd_buf_size
, 0);
2233 error_setg(errp
, QERR_IO_ERROR
);
2242 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
2243 bool flat
, bool compress
, bool zeroed_grain
,
2245 QemuOpts
*opts
, Error
**errp
)
2248 BlockBackend
*blk
= NULL
;
2250 ret
= bdrv_create_file(filename
, opts
, errp
);
2255 blk
= blk_new_open(filename
, NULL
, NULL
,
2256 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
2263 blk_set_allow_write_beyond_eof(blk
, true);
2265 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
2278 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
2279 char *postfix
, size_t buf_len
, Error
**errp
)
2283 if (filename
== NULL
|| !strlen(filename
)) {
2284 error_setg(errp
, "No filename provided");
2287 p
= strrchr(filename
, '/');
2289 p
= strrchr(filename
, '\\');
2292 p
= strrchr(filename
, ':');
2296 if (p
- filename
>= buf_len
) {
2299 pstrcpy(path
, p
- filename
+ 1, filename
);
2304 q
= strrchr(p
, '.');
2306 pstrcpy(prefix
, buf_len
, p
);
2309 if (q
- p
>= buf_len
) {
2312 pstrcpy(prefix
, q
- p
+ 1, p
);
2313 pstrcpy(postfix
, buf_len
, q
);
2319 * idx == 0: get or create the descriptor file (also the image file if in a
2321 * idx >= 1: get the n-th extent if in a split subformat
2323 typedef BlockBackend
*(*vmdk_create_extent_fn
)(int64_t size
,
2332 static void vmdk_desc_add_extent(GString
*desc
,
2333 const char *extent_line_fmt
,
2334 int64_t size
, const char *filename
)
2336 char *basename
= g_path_get_basename(filename
);
2338 g_string_append_printf(desc
, extent_line_fmt
,
2339 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
2343 static int coroutine_fn
vmdk_co_do_create(int64_t size
,
2344 BlockdevVmdkSubformat subformat
,
2345 BlockdevVmdkAdapterType adapter_type
,
2346 const char *backing_file
,
2347 const char *hw_version
,
2348 const char *toolsversion
,
2351 vmdk_create_extent_fn extent_fn
,
2356 BlockBackend
*blk
= NULL
;
2357 BlockBackend
*extent_blk
;
2358 Error
*local_err
= NULL
;
2361 bool flat
, split
, compress
;
2362 GString
*ext_desc_lines
;
2363 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
2364 int64_t extent_size
;
2365 int64_t created_size
= 0;
2366 const char *extent_line_fmt
;
2367 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2368 uint32_t parent_cid
= 0xffffffff;
2369 uint32_t number_heads
= 16;
2370 uint32_t desc_offset
= 0, desc_len
;
2371 const char desc_template
[] =
2372 "# Disk DescriptorFile\n"
2375 "parentCID=%" PRIx32
"\n"
2376 "createType=\"%s\"\n"
2379 "# Extent description\n"
2382 "# The Disk Data Base\n"
2385 "ddb.virtualHWVersion = \"%s\"\n"
2386 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2387 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2388 "ddb.geometry.sectors = \"63\"\n"
2389 "ddb.adapterType = \"%s\"\n"
2390 "ddb.toolsVersion = \"%s\"\n";
2392 ext_desc_lines
= g_string_new(NULL
);
2394 /* Read out options */
2398 "compat6 cannot be enabled with hwversion set");
2407 if (!toolsversion
) {
2408 toolsversion
= "2147483647";
2411 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2412 /* that's the number of heads with which vmware operates when
2413 creating, exporting, etc. vmdk files with a non-ide adapter type */
2416 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2417 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2418 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2419 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2420 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2423 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2425 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2427 if (flat
&& backing_file
) {
2428 error_setg(errp
, "Flat image can't have backing file");
2432 if (flat
&& zeroed_grain
) {
2433 error_setg(errp
, "Flat image can't enable zeroed grain");
2438 /* Create extents */
2440 extent_size
= split_size
;
2444 if (!split
&& !flat
) {
2445 created_size
= extent_size
;
2449 /* Get the descriptor file BDS */
2450 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2456 if (!split
&& !flat
) {
2457 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2458 blk_bs(blk
)->filename
);
2462 BlockBackend
*backing
;
2463 char *full_backing
=
2464 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2468 error_propagate(errp
, local_err
);
2472 assert(full_backing
);
2474 backing
= blk_new_open(full_backing
, NULL
, NULL
,
2475 BDRV_O_NO_BACKING
, errp
);
2476 g_free(full_backing
);
2477 if (backing
== NULL
) {
2481 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2482 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2483 blk_bs(backing
)->drv
->format_name
);
2488 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2491 error_setg(errp
, "Failed to read parent CID");
2494 snprintf(parent_desc_line
, BUF_SIZE
,
2495 "parentFileNameHint=\"%s\"", backing_file
);
2498 while (created_size
< size
) {
2499 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2500 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2501 zeroed_grain
, opaque
, errp
);
2506 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2507 blk_bs(extent_blk
)->filename
);
2508 created_size
+= cur_size
;
2510 blk_unref(extent_blk
);
2513 /* Check whether we got excess extents */
2514 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2517 blk_unref(extent_blk
);
2518 error_setg(errp
, "List of extents contains unused extents");
2523 /* generate descriptor file */
2524 desc
= g_strdup_printf(desc_template
,
2527 BlockdevVmdkSubformat_str(subformat
),
2529 ext_desc_lines
->str
,
2532 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2534 BlockdevVmdkAdapterType_str(adapter_type
),
2536 desc_len
= strlen(desc
);
2537 /* the descriptor offset = 0x200 */
2538 if (!split
&& !flat
) {
2539 desc_offset
= 0x200;
2542 ret
= blk_pwrite(blk
, desc_offset
, desc
, desc_len
, 0);
2544 error_setg_errno(errp
, -ret
, "Could not write description");
2547 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2548 * for description file */
2549 if (desc_offset
== 0) {
2550 ret
= blk_truncate(blk
, desc_len
, false, PREALLOC_MODE_OFF
, 0, errp
);
2561 g_free(parent_desc_line
);
2562 g_string_free(ext_desc_lines
, true);
2571 } VMDKCreateOptsData
;
2573 static BlockBackend
*vmdk_co_create_opts_cb(int64_t size
, int idx
,
2574 bool flat
, bool split
, bool compress
,
2575 bool zeroed_grain
, void *opaque
,
2578 BlockBackend
*blk
= NULL
;
2579 BlockDriverState
*bs
= NULL
;
2580 VMDKCreateOptsData
*data
= opaque
;
2581 char *ext_filename
= NULL
;
2582 char *rel_filename
= NULL
;
2584 /* We're done, don't create excess extents. */
2586 assert(errp
== NULL
);
2591 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2593 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2595 flat
? 'f' : 's', idx
, data
->postfix
);
2598 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2601 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2602 g_free(rel_filename
);
2604 if (vmdk_create_extent(ext_filename
, size
,
2605 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2611 g_free(ext_filename
);
2615 static int coroutine_fn
vmdk_co_create_opts(BlockDriver
*drv
,
2616 const char *filename
,
2620 Error
*local_err
= NULL
;
2622 int64_t total_size
= 0;
2623 char *adapter_type
= NULL
;
2624 BlockdevVmdkAdapterType adapter_type_enum
;
2625 char *backing_file
= NULL
;
2626 char *hw_version
= NULL
;
2627 char *toolsversion
= NULL
;
2629 BlockdevVmdkSubformat subformat
;
2631 char *path
= g_malloc0(PATH_MAX
);
2632 char *prefix
= g_malloc0(PATH_MAX
);
2633 char *postfix
= g_malloc0(PATH_MAX
);
2634 char *desc_line
= g_malloc0(BUF_SIZE
);
2635 char *ext_filename
= g_malloc0(PATH_MAX
);
2636 char *desc_filename
= g_malloc0(PATH_MAX
);
2637 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2640 VMDKCreateOptsData data
;
2641 char *backing_fmt
= NULL
;
2643 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2644 if (backing_fmt
&& strcmp(backing_fmt
, "vmdk") != 0) {
2645 error_setg(errp
, "backing_file must be a vmdk image");
2650 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2654 /* Read out options */
2655 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2657 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2658 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2659 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2660 toolsversion
= qemu_opt_get_del(opts
, BLOCK_OPT_TOOLSVERSION
);
2661 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2662 if (strcmp(hw_version
, "undefined") == 0) {
2666 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2667 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2670 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2672 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2675 error_propagate(errp
, local_err
);
2680 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2684 /* Default format to monolithicSparse */
2685 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2687 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2689 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2692 error_propagate(errp
, local_err
);
2697 data
= (VMDKCreateOptsData
){
2703 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2704 backing_file
, hw_version
, toolsversion
, compat6
,
2705 zeroed_grain
, vmdk_co_create_opts_cb
, &data
, errp
);
2708 g_free(backing_fmt
);
2709 g_free(adapter_type
);
2710 g_free(backing_file
);
2712 g_free(toolsversion
);
2719 g_free(ext_filename
);
2720 g_free(desc_filename
);
2721 g_free(parent_desc_line
);
2725 static BlockBackend
*vmdk_co_create_cb(int64_t size
, int idx
,
2726 bool flat
, bool split
, bool compress
,
2727 bool zeroed_grain
, void *opaque
,
2731 BlockDriverState
*bs
;
2733 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2736 bs
= bdrv_open_blockdev_ref(opts
->file
, errp
);
2739 BlockdevRefList
*list
= opts
->extents
;
2740 for (i
= 1; i
< idx
; i
++) {
2741 if (!list
|| !list
->next
) {
2742 error_setg(errp
, "Extent [%d] not specified", i
);
2748 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2751 bs
= bdrv_open_blockdev_ref(list
->value
, errp
);
2756 blk
= blk_new_with_bs(bs
,
2757 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
2758 BLK_PERM_ALL
, errp
);
2762 blk_set_allow_write_beyond_eof(blk
, true);
2766 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2775 static int coroutine_fn
vmdk_co_create(BlockdevCreateOptions
*create_options
,
2779 BlockdevCreateOptionsVmdk
*opts
;
2781 opts
= &create_options
->u
.vmdk
;
2783 /* Validate options */
2784 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2785 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2790 ret
= vmdk_co_do_create(opts
->size
,
2806 static void vmdk_close(BlockDriverState
*bs
)
2808 BDRVVmdkState
*s
= bs
->opaque
;
2810 vmdk_free_extents(bs
);
2811 g_free(s
->create_type
);
2813 migrate_del_blocker(s
->migration_blocker
);
2814 error_free(s
->migration_blocker
);
2817 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
2822 BDRVVmdkState
*s
= bs
->opaque
;
2824 ret
= bdrv_get_allocated_file_size(bs
->file
->bs
);
2828 for (i
= 0; i
< s
->num_extents
; i
++) {
2829 if (s
->extents
[i
].file
== bs
->file
) {
2832 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2841 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2844 BDRVVmdkState
*s
= bs
->opaque
;
2846 /* If has a flat extent and its underlying storage doesn't have zero init,
2848 for (i
= 0; i
< s
->num_extents
; i
++) {
2849 if (s
->extents
[i
].flat
) {
2850 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2858 static ImageInfo
*vmdk_get_extent_info(VmdkExtent
*extent
)
2860 ImageInfo
*info
= g_new0(ImageInfo
, 1);
2862 bdrv_refresh_filename(extent
->file
->bs
);
2863 *info
= (ImageInfo
){
2864 .filename
= g_strdup(extent
->file
->bs
->filename
),
2865 .format
= g_strdup(extent
->type
),
2866 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2867 .compressed
= extent
->compressed
,
2868 .has_compressed
= extent
->compressed
,
2869 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2870 .has_cluster_size
= !extent
->flat
,
2876 static int coroutine_fn
vmdk_co_check(BlockDriverState
*bs
,
2877 BdrvCheckResult
*result
,
2880 BDRVVmdkState
*s
= bs
->opaque
;
2881 VmdkExtent
*extent
= NULL
;
2882 int64_t sector_num
= 0;
2883 int64_t total_sectors
= bdrv_nb_sectors(bs
);
2885 uint64_t cluster_offset
;
2892 if (sector_num
>= total_sectors
) {
2895 extent
= find_extent(s
, sector_num
, extent
);
2898 "ERROR: could not find extent for sector %" PRId64
"\n",
2903 ret
= get_cluster_offset(bs
, extent
, NULL
,
2904 sector_num
<< BDRV_SECTOR_BITS
,
2905 false, &cluster_offset
, 0, 0);
2906 if (ret
== VMDK_ERROR
) {
2908 "ERROR: could not get cluster_offset for sector %"
2909 PRId64
"\n", sector_num
);
2912 if (ret
== VMDK_OK
) {
2913 int64_t extent_len
= bdrv_getlength(extent
->file
->bs
);
2914 if (extent_len
< 0) {
2916 "ERROR: could not get extent file length for sector %"
2917 PRId64
"\n", sector_num
);
2921 if (cluster_offset
>= extent_len
) {
2923 "ERROR: cluster offset for sector %"
2924 PRId64
" points after EOF\n", sector_num
);
2929 sector_num
+= extent
->cluster_sectors
;
2932 result
->corruptions
++;
2936 static ImageInfoSpecific
*vmdk_get_specific_info(BlockDriverState
*bs
,
2940 BDRVVmdkState
*s
= bs
->opaque
;
2941 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2942 ImageInfoList
**tail
;
2944 *spec_info
= (ImageInfoSpecific
){
2945 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
2947 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
2951 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
2952 .create_type
= g_strdup(s
->create_type
),
2954 .parent_cid
= s
->parent_cid
,
2957 tail
= &spec_info
->u
.vmdk
.data
->extents
;
2958 for (i
= 0; i
< s
->num_extents
; i
++) {
2959 QAPI_LIST_APPEND(tail
, vmdk_get_extent_info(&s
->extents
[i
]));
2965 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
2967 return a
->flat
== b
->flat
&&
2968 a
->compressed
== b
->compressed
&&
2969 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
2972 static int vmdk_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2975 BDRVVmdkState
*s
= bs
->opaque
;
2976 assert(s
->num_extents
);
2978 /* See if we have multiple extents but they have different cases */
2979 for (i
= 1; i
< s
->num_extents
; i
++) {
2980 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
2984 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
2985 if (!s
->extents
[0].flat
) {
2986 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
2991 static void vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
2992 bool backing_overridden
)
2994 /* No children but file and backing can be explicitly specified (TODO) */
2995 qdict_put(target
, "file",
2996 qobject_ref(bs
->file
->bs
->full_open_options
));
2998 if (backing_overridden
) {
3000 qdict_put(target
, "backing",
3001 qobject_ref(bs
->backing
->bs
->full_open_options
));
3003 qdict_put_null(target
, "backing");
3008 static QemuOptsList vmdk_create_opts
= {
3009 .name
= "vmdk-create-opts",
3010 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
3013 .name
= BLOCK_OPT_SIZE
,
3014 .type
= QEMU_OPT_SIZE
,
3015 .help
= "Virtual disk size"
3018 .name
= BLOCK_OPT_ADAPTER_TYPE
,
3019 .type
= QEMU_OPT_STRING
,
3020 .help
= "Virtual adapter type, can be one of "
3021 "ide (default), lsilogic, buslogic or legacyESX"
3024 .name
= BLOCK_OPT_BACKING_FILE
,
3025 .type
= QEMU_OPT_STRING
,
3026 .help
= "File name of a base image"
3029 .name
= BLOCK_OPT_BACKING_FMT
,
3030 .type
= QEMU_OPT_STRING
,
3031 .help
= "Must be 'vmdk' if present",
3034 .name
= BLOCK_OPT_COMPAT6
,
3035 .type
= QEMU_OPT_BOOL
,
3036 .help
= "VMDK version 6 image",
3037 .def_value_str
= "off"
3040 .name
= BLOCK_OPT_HWVERSION
,
3041 .type
= QEMU_OPT_STRING
,
3042 .help
= "VMDK hardware version",
3043 .def_value_str
= "undefined"
3046 .name
= BLOCK_OPT_TOOLSVERSION
,
3047 .type
= QEMU_OPT_STRING
,
3048 .help
= "VMware guest tools version",
3051 .name
= BLOCK_OPT_SUBFMT
,
3052 .type
= QEMU_OPT_STRING
,
3054 "VMDK flat extent format, can be one of "
3055 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
3058 .name
= BLOCK_OPT_ZEROED_GRAIN
,
3059 .type
= QEMU_OPT_BOOL
,
3060 .help
= "Enable efficient zero writes "
3061 "using the zeroed-grain GTE feature"
3063 { /* end of list */ }
3067 static BlockDriver bdrv_vmdk
= {
3068 .format_name
= "vmdk",
3069 .instance_size
= sizeof(BDRVVmdkState
),
3070 .bdrv_probe
= vmdk_probe
,
3071 .bdrv_open
= vmdk_open
,
3072 .bdrv_co_check
= vmdk_co_check
,
3073 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
3074 .bdrv_child_perm
= bdrv_default_perms
,
3075 .bdrv_co_preadv
= vmdk_co_preadv
,
3076 .bdrv_co_pwritev
= vmdk_co_pwritev
,
3077 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
3078 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
3079 .bdrv_close
= vmdk_close
,
3080 .bdrv_co_create_opts
= vmdk_co_create_opts
,
3081 .bdrv_co_create
= vmdk_co_create
,
3082 .bdrv_co_block_status
= vmdk_co_block_status
,
3083 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
3084 .bdrv_has_zero_init
= vmdk_has_zero_init
,
3085 .bdrv_get_specific_info
= vmdk_get_specific_info
,
3086 .bdrv_refresh_limits
= vmdk_refresh_limits
,
3087 .bdrv_get_info
= vmdk_get_info
,
3088 .bdrv_gather_child_options
= vmdk_gather_child_options
,
3091 .supports_backing
= true,
3092 .create_opts
= &vmdk_create_opts
,
3095 static void bdrv_vmdk_init(void)
3097 bdrv_register(&bdrv_vmdk
);
3100 block_init(bdrv_vmdk_init
);