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 "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "qemu/option.h"
34 #include "qemu/bswap.h"
35 #include "qemu/memalign.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 BDRVVmdkReopenState
{
181 bool *extents_using_bs_file
;
182 } BDRVVmdkReopenState
;
184 typedef struct VmdkMetaData
{
185 unsigned int l1_index
;
186 unsigned int l2_index
;
187 unsigned int l2_offset
;
189 uint32_t *l2_cache_entry
;
192 typedef struct VmdkGrainMarker
{
196 } QEMU_PACKED VmdkGrainMarker
;
199 MARKER_END_OF_STREAM
= 0,
200 MARKER_GRAIN_TABLE
= 1,
201 MARKER_GRAIN_DIRECTORY
= 2,
205 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
212 magic
= be32_to_cpu(*(uint32_t *)buf
);
213 if (magic
== VMDK3_MAGIC
||
214 magic
== VMDK4_MAGIC
) {
217 const char *p
= (const char *)buf
;
218 const char *end
= p
+ buf_size
;
221 /* skip comment line */
222 while (p
< end
&& *p
!= '\n') {
229 while (p
< end
&& *p
== ' ') {
232 /* skip '\r' if windows line endings used. */
233 if (p
< end
&& *p
== '\r') {
236 /* only accept blank lines before 'version=' line */
237 if (p
== end
|| *p
!= '\n') {
243 if (end
- p
>= strlen("version=X\n")) {
244 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
245 strncmp("version=2\n", p
, strlen("version=2\n")) == 0 ||
246 strncmp("version=3\n", p
, strlen("version=3\n")) == 0) {
250 if (end
- p
>= strlen("version=X\r\n")) {
251 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
252 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0 ||
253 strncmp("version=3\r\n", p
, strlen("version=3\r\n")) == 0) {
263 #define SECTOR_SIZE 512
264 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
265 #define BUF_SIZE 4096
266 #define HEADER_SIZE 512 /* first sector of 512 bytes */
268 static void vmdk_free_extents(BlockDriverState
*bs
)
271 BDRVVmdkState
*s
= bs
->opaque
;
275 for (i
= 0; i
< s
->num_extents
; i
++) {
279 g_free(e
->l1_backup_table
);
281 if (e
->file
!= bs
->file
) {
282 bdrv_unref_child(bs
, e
->file
);
285 bdrv_graph_wrunlock();
290 static void vmdk_free_last_extent(BlockDriverState
*bs
)
292 BDRVVmdkState
*s
= bs
->opaque
;
294 if (s
->num_extents
== 0) {
298 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
301 /* Return -ve errno, or 0 on success and write CID into *pcid. */
302 static int GRAPH_RDLOCK
303 vmdk_read_cid(BlockDriverState
*bs
, int parent
, uint32_t *pcid
)
307 const char *p_name
, *cid_str
;
309 BDRVVmdkState
*s
= bs
->opaque
;
312 desc
= g_malloc0(DESC_SIZE
);
313 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, DESC_SIZE
, desc
, 0);
319 cid_str
= "parentCID";
320 cid_str_size
= sizeof("parentCID");
323 cid_str_size
= sizeof("CID");
326 desc
[DESC_SIZE
- 1] = '\0';
327 p_name
= strstr(desc
, cid_str
);
328 if (p_name
== NULL
) {
332 p_name
+= cid_str_size
;
333 if (sscanf(p_name
, "%" SCNx32
, &cid
) != 1) {
345 static int coroutine_fn GRAPH_RDLOCK
346 vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
348 char *desc
, *tmp_desc
;
349 char *p_name
, *tmp_str
;
350 BDRVVmdkState
*s
= bs
->opaque
;
353 size_t desc_buf_size
;
355 if (s
->desc_offset
== 0) {
356 desc_buf_size
= bdrv_getlength(bs
->file
->bs
);
357 if (desc_buf_size
> 16ULL << 20) {
358 error_report("VMDK description file too big");
362 desc_buf_size
= DESC_SIZE
;
365 desc
= g_malloc0(desc_buf_size
);
366 tmp_desc
= g_malloc0(desc_buf_size
);
367 ret
= bdrv_co_pread(bs
->file
, s
->desc_offset
, desc_buf_size
, desc
, 0);
372 desc
[desc_buf_size
- 1] = '\0';
373 tmp_str
= strstr(desc
, "parentCID");
374 if (tmp_str
== NULL
) {
379 pstrcpy(tmp_desc
, desc_buf_size
, tmp_str
);
380 p_name
= strstr(desc
, "CID");
381 if (p_name
!= NULL
) {
382 p_name
+= sizeof("CID");
383 snprintf(p_name
, desc_buf_size
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
384 pstrcat(desc
, desc_buf_size
, tmp_desc
);
387 ret
= bdrv_co_pwrite_sync(bs
->file
, s
->desc_offset
, desc_buf_size
, desc
, 0);
395 static int coroutine_fn GRAPH_RDLOCK
vmdk_is_cid_valid(BlockDriverState
*bs
)
397 BDRVVmdkState
*s
= bs
->opaque
;
400 if (!s
->cid_checked
&& bs
->backing
) {
401 BlockDriverState
*p_bs
= bs
->backing
->bs
;
403 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
404 /* Backing file is not in vmdk format, so it does not have
405 * a CID, which makes the overlay's parent CID invalid */
409 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
410 /* read failure: report as not valid */
413 if (s
->parent_cid
!= cur_pcid
) {
418 s
->cid_checked
= true;
423 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
424 BlockReopenQueue
*queue
, Error
**errp
)
427 BDRVVmdkReopenState
*rs
;
431 GRAPH_RDLOCK_GUARD_MAINLOOP();
433 assert(state
!= NULL
);
434 assert(state
->bs
!= NULL
);
435 assert(state
->opaque
== NULL
);
437 s
= state
->bs
->opaque
;
439 rs
= g_new0(BDRVVmdkReopenState
, 1);
443 * Check whether there are any extents stored in bs->file; if bs->file
444 * changes, we will need to update their .file pointers to follow suit
446 rs
->extents_using_bs_file
= g_new(bool, s
->num_extents
);
447 for (i
= 0; i
< s
->num_extents
; i
++) {
448 rs
->extents_using_bs_file
[i
] = s
->extents
[i
].file
== state
->bs
->file
;
454 static void vmdk_reopen_clean(BDRVReopenState
*state
)
456 BDRVVmdkReopenState
*rs
= state
->opaque
;
458 g_free(rs
->extents_using_bs_file
);
460 state
->opaque
= NULL
;
463 static void vmdk_reopen_commit(BDRVReopenState
*state
)
465 BDRVVmdkState
*s
= state
->bs
->opaque
;
466 BDRVVmdkReopenState
*rs
= state
->opaque
;
470 GRAPH_RDLOCK_GUARD_MAINLOOP();
472 for (i
= 0; i
< s
->num_extents
; i
++) {
473 if (rs
->extents_using_bs_file
[i
]) {
474 s
->extents
[i
].file
= state
->bs
->file
;
478 vmdk_reopen_clean(state
);
481 static void vmdk_reopen_abort(BDRVReopenState
*state
)
483 vmdk_reopen_clean(state
);
486 static int GRAPH_RDLOCK
vmdk_parent_open(BlockDriverState
*bs
)
490 BDRVVmdkState
*s
= bs
->opaque
;
493 desc
= g_malloc0(DESC_SIZE
+ 1);
494 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, DESC_SIZE
, desc
, 0);
499 p_name
= strstr(desc
, "parentFileNameHint");
500 if (p_name
!= NULL
) {
503 p_name
+= sizeof("parentFileNameHint") + 1;
504 end_name
= strchr(p_name
, '\"');
505 if (end_name
== NULL
) {
509 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
514 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
515 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
516 bs
->auto_backing_file
);
517 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
),
526 /* Create and append extent to the extent array. Return the added VmdkExtent
527 * address. return NULL if allocation failed. */
528 static int vmdk_add_extent(BlockDriverState
*bs
,
529 BdrvChild
*file
, bool flat
, int64_t sectors
,
530 int64_t l1_offset
, int64_t l1_backup_offset
,
532 int l2_size
, uint64_t cluster_sectors
,
533 VmdkExtent
**new_extent
,
537 BDRVVmdkState
*s
= bs
->opaque
;
540 if (cluster_sectors
> 0x200000) {
541 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
542 error_setg(errp
, "Invalid granularity, image may be corrupt");
545 if (l1_size
> 32 * 1024 * 1024) {
547 * Although with big capacity and small l1_entry_sectors, we can get a
548 * big l1_size, we don't want unbounded value to allocate the table.
549 * Limit it to 32M, which is enough to store:
550 * 8TB - for both VMDK3 & VMDK4 with
551 * minimal cluster size: 512B
552 * minimal L2 table size: 512 entries
553 * 8 TB is still more than the maximal value supported for
554 * VMDK3 & VMDK4 which is 2TB.
555 * 64TB - for "ESXi seSparse Extent"
556 * minimal cluster size: 512B (default is 4KB)
557 * L2 table size: 4096 entries (const).
558 * 64TB is more than the maximal value supported for
559 * seSparse VMDKs (which is slightly less than 64TB)
561 error_setg(errp
, "L1 size too big");
565 nb_sectors
= bdrv_nb_sectors(file
->bs
);
566 if (nb_sectors
< 0) {
570 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
571 extent
= &s
->extents
[s
->num_extents
];
574 memset(extent
, 0, sizeof(VmdkExtent
));
577 extent
->sectors
= sectors
;
578 extent
->l1_table_offset
= l1_offset
;
579 extent
->l1_backup_table_offset
= l1_backup_offset
;
580 extent
->l1_size
= l1_size
;
581 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
582 extent
->l2_size
= l2_size
;
583 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
584 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
585 extent
->entry_size
= sizeof(uint32_t);
587 if (s
->num_extents
> 1) {
588 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
590 extent
->end_sector
= extent
->sectors
;
592 bs
->total_sectors
= extent
->end_sector
;
594 *new_extent
= extent
;
599 static int GRAPH_RDLOCK
600 vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
, Error
**errp
)
606 /* read the L1 table */
607 l1_size
= extent
->l1_size
* extent
->entry_size
;
608 extent
->l1_table
= g_try_malloc(l1_size
);
609 if (l1_size
&& extent
->l1_table
== NULL
) {
613 ret
= bdrv_pread(extent
->file
, extent
->l1_table_offset
, l1_size
,
614 extent
->l1_table
, 0);
616 bdrv_refresh_filename(extent
->file
->bs
);
617 error_setg_errno(errp
, -ret
,
618 "Could not read l1 table from extent '%s'",
619 extent
->file
->bs
->filename
);
622 for (i
= 0; i
< extent
->l1_size
; i
++) {
623 if (extent
->entry_size
== sizeof(uint64_t)) {
624 le64_to_cpus((uint64_t *)extent
->l1_table
+ i
);
626 assert(extent
->entry_size
== sizeof(uint32_t));
627 le32_to_cpus((uint32_t *)extent
->l1_table
+ i
);
631 if (extent
->l1_backup_table_offset
) {
632 assert(!extent
->sesparse
);
633 extent
->l1_backup_table
= g_try_malloc(l1_size
);
634 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
638 ret
= bdrv_pread(extent
->file
, extent
->l1_backup_table_offset
,
639 l1_size
, extent
->l1_backup_table
, 0);
641 bdrv_refresh_filename(extent
->file
->bs
);
642 error_setg_errno(errp
, -ret
,
643 "Could not read l1 backup table from extent '%s'",
644 extent
->file
->bs
->filename
);
647 for (i
= 0; i
< extent
->l1_size
; i
++) {
648 le32_to_cpus(&extent
->l1_backup_table
[i
]);
653 g_malloc(extent
->entry_size
* extent
->l2_size
* L2_CACHE_SIZE
);
656 g_free(extent
->l1_backup_table
);
658 g_free(extent
->l1_table
);
662 static int GRAPH_RDLOCK
663 vmdk_open_vmfs_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
669 VmdkExtent
*extent
= NULL
;
671 ret
= bdrv_pread(file
, sizeof(magic
), sizeof(header
), &header
, 0);
673 bdrv_refresh_filename(file
->bs
);
674 error_setg_errno(errp
, -ret
,
675 "Could not read header from file '%s'",
679 ret
= vmdk_add_extent(bs
, file
, false,
680 le32_to_cpu(header
.disk_sectors
),
681 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
683 le32_to_cpu(header
.l1dir_size
),
685 le32_to_cpu(header
.granularity
),
691 ret
= vmdk_init_tables(bs
, extent
, errp
);
693 /* free extent allocated by vmdk_add_extent */
694 vmdk_free_last_extent(bs
);
699 #define SESPARSE_CONST_HEADER_MAGIC UINT64_C(0x00000000cafebabe)
700 #define SESPARSE_VOLATILE_HEADER_MAGIC UINT64_C(0x00000000cafecafe)
702 /* Strict checks - format not officially documented */
703 static int check_se_sparse_const_header(VMDKSESparseConstHeader
*header
,
706 header
->magic
= le64_to_cpu(header
->magic
);
707 header
->version
= le64_to_cpu(header
->version
);
708 header
->grain_size
= le64_to_cpu(header
->grain_size
);
709 header
->grain_table_size
= le64_to_cpu(header
->grain_table_size
);
710 header
->flags
= le64_to_cpu(header
->flags
);
711 header
->reserved1
= le64_to_cpu(header
->reserved1
);
712 header
->reserved2
= le64_to_cpu(header
->reserved2
);
713 header
->reserved3
= le64_to_cpu(header
->reserved3
);
714 header
->reserved4
= le64_to_cpu(header
->reserved4
);
716 header
->volatile_header_offset
=
717 le64_to_cpu(header
->volatile_header_offset
);
718 header
->volatile_header_size
= le64_to_cpu(header
->volatile_header_size
);
720 header
->journal_header_offset
= le64_to_cpu(header
->journal_header_offset
);
721 header
->journal_header_size
= le64_to_cpu(header
->journal_header_size
);
723 header
->journal_offset
= le64_to_cpu(header
->journal_offset
);
724 header
->journal_size
= le64_to_cpu(header
->journal_size
);
726 header
->grain_dir_offset
= le64_to_cpu(header
->grain_dir_offset
);
727 header
->grain_dir_size
= le64_to_cpu(header
->grain_dir_size
);
729 header
->grain_tables_offset
= le64_to_cpu(header
->grain_tables_offset
);
730 header
->grain_tables_size
= le64_to_cpu(header
->grain_tables_size
);
732 header
->free_bitmap_offset
= le64_to_cpu(header
->free_bitmap_offset
);
733 header
->free_bitmap_size
= le64_to_cpu(header
->free_bitmap_size
);
735 header
->backmap_offset
= le64_to_cpu(header
->backmap_offset
);
736 header
->backmap_size
= le64_to_cpu(header
->backmap_size
);
738 header
->grains_offset
= le64_to_cpu(header
->grains_offset
);
739 header
->grains_size
= le64_to_cpu(header
->grains_size
);
741 if (header
->magic
!= SESPARSE_CONST_HEADER_MAGIC
) {
742 error_setg(errp
, "Bad const header magic: 0x%016" PRIx64
,
747 if (header
->version
!= 0x0000000200000001) {
748 error_setg(errp
, "Unsupported version: 0x%016" PRIx64
,
753 if (header
->grain_size
!= 8) {
754 error_setg(errp
, "Unsupported grain size: %" PRIu64
,
759 if (header
->grain_table_size
!= 64) {
760 error_setg(errp
, "Unsupported grain table size: %" PRIu64
,
761 header
->grain_table_size
);
765 if (header
->flags
!= 0) {
766 error_setg(errp
, "Unsupported flags: 0x%016" PRIx64
,
771 if (header
->reserved1
!= 0 || header
->reserved2
!= 0 ||
772 header
->reserved3
!= 0 || header
->reserved4
!= 0) {
773 error_setg(errp
, "Unsupported reserved bits:"
774 " 0x%016" PRIx64
" 0x%016" PRIx64
775 " 0x%016" PRIx64
" 0x%016" PRIx64
,
776 header
->reserved1
, header
->reserved2
,
777 header
->reserved3
, header
->reserved4
);
781 /* check that padding is 0 */
782 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
783 error_setg(errp
, "Unsupported non-zero const header padding");
790 static int check_se_sparse_volatile_header(VMDKSESparseVolatileHeader
*header
,
793 header
->magic
= le64_to_cpu(header
->magic
);
794 header
->free_gt_number
= le64_to_cpu(header
->free_gt_number
);
795 header
->next_txn_seq_number
= le64_to_cpu(header
->next_txn_seq_number
);
796 header
->replay_journal
= le64_to_cpu(header
->replay_journal
);
798 if (header
->magic
!= SESPARSE_VOLATILE_HEADER_MAGIC
) {
799 error_setg(errp
, "Bad volatile header magic: 0x%016" PRIx64
,
804 if (header
->replay_journal
) {
805 error_setg(errp
, "Image is dirty, Replaying journal not supported");
809 /* check that padding is 0 */
810 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
811 error_setg(errp
, "Unsupported non-zero volatile header padding");
818 static int GRAPH_RDLOCK
819 vmdk_open_se_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
823 VMDKSESparseConstHeader const_header
;
824 VMDKSESparseVolatileHeader volatile_header
;
825 VmdkExtent
*extent
= NULL
;
827 ret
= bdrv_apply_auto_read_only(bs
,
828 "No write support for seSparse images available", errp
);
833 assert(sizeof(const_header
) == SECTOR_SIZE
);
835 ret
= bdrv_pread(file
, 0, sizeof(const_header
), &const_header
, 0);
837 bdrv_refresh_filename(file
->bs
);
838 error_setg_errno(errp
, -ret
,
839 "Could not read const header from file '%s'",
844 /* check const header */
845 ret
= check_se_sparse_const_header(&const_header
, errp
);
850 assert(sizeof(volatile_header
) == SECTOR_SIZE
);
852 ret
= bdrv_pread(file
, const_header
.volatile_header_offset
* SECTOR_SIZE
,
853 sizeof(volatile_header
), &volatile_header
, 0);
855 bdrv_refresh_filename(file
->bs
);
856 error_setg_errno(errp
, -ret
,
857 "Could not read volatile header from file '%s'",
862 /* check volatile header */
863 ret
= check_se_sparse_volatile_header(&volatile_header
, errp
);
868 ret
= vmdk_add_extent(bs
, file
, false,
869 const_header
.capacity
,
870 const_header
.grain_dir_offset
* SECTOR_SIZE
,
872 const_header
.grain_dir_size
*
873 SECTOR_SIZE
/ sizeof(uint64_t),
874 const_header
.grain_table_size
*
875 SECTOR_SIZE
/ sizeof(uint64_t),
876 const_header
.grain_size
,
883 extent
->sesparse
= true;
884 extent
->sesparse_l2_tables_offset
= const_header
.grain_tables_offset
;
885 extent
->sesparse_clusters_offset
= const_header
.grains_offset
;
886 extent
->entry_size
= sizeof(uint64_t);
888 ret
= vmdk_init_tables(bs
, extent
, errp
);
890 /* free extent allocated by vmdk_add_extent */
891 vmdk_free_last_extent(bs
);
897 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
898 QDict
*options
, Error
**errp
);
900 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
906 size
= bdrv_getlength(file
->bs
);
908 error_setg_errno(errp
, -size
, "Could not access file");
913 /* Both descriptor file and sparse image must be much larger than 4
914 * bytes, also callers of vmdk_read_desc want to compare the first 4
915 * bytes with VMDK4_MAGIC, let's error out if less is read. */
916 error_setg(errp
, "File is too small, not a valid image");
920 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
921 buf
= g_malloc(size
+ 1);
923 ret
= bdrv_pread(file
, desc_offset
, size
, buf
, 0);
925 error_setg_errno(errp
, -ret
, "Could not read from file");
934 static int GRAPH_RDLOCK
935 vmdk_open_vmdk4(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
936 QDict
*options
, Error
**errp
)
940 uint32_t l1_size
, l1_entry_sectors
;
942 VmdkExtent
*extent
= NULL
;
943 BDRVVmdkState
*s
= bs
->opaque
;
944 int64_t l1_backup_offset
= 0;
947 ret
= bdrv_pread(file
, sizeof(magic
), sizeof(header
), &header
, 0);
949 bdrv_refresh_filename(file
->bs
);
950 error_setg_errno(errp
, -ret
,
951 "Could not read header from file '%s'",
955 if (header
.capacity
== 0) {
956 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
958 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
962 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
968 if (!s
->create_type
) {
969 s
->create_type
= g_strdup("monolithicSparse");
972 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
974 * The footer takes precedence over the header, so read it in. The
975 * footer starts at offset -1024 from the end: One sector for the
976 * footer, and another one for the end-of-stream marker.
983 uint8_t pad
[512 - 16];
984 } QEMU_PACKED footer_marker
;
988 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
994 uint8_t pad
[512 - 16];
995 } QEMU_PACKED eos_marker
;
996 } QEMU_PACKED footer
;
998 ret
= bdrv_pread(file
, bs
->file
->bs
->total_sectors
* 512 - 1536,
999 sizeof(footer
), &footer
, 0);
1001 error_setg_errno(errp
, -ret
, "Failed to read footer");
1005 /* Some sanity checks for the footer */
1006 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
1007 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
1008 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
1009 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
1010 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
1011 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
1013 error_setg(errp
, "Invalid footer");
1017 header
= footer
.header
;
1021 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1022 if (le32_to_cpu(header
.version
) > 3) {
1023 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
1024 le32_to_cpu(header
.version
));
1026 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
1028 /* VMware KB 2064959 explains that version 3 added support for
1029 * persistent changed block tracking (CBT), and backup software can
1030 * read it as version=1 if it doesn't care about the changed area
1031 * information. So we are safe to enable read only. */
1032 error_setg(errp
, "VMDK version 3 must be read only");
1036 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
1037 error_setg(errp
, "L2 table size too big");
1041 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
1042 * le64_to_cpu(header
.granularity
);
1043 if (l1_entry_sectors
== 0) {
1044 error_setg(errp
, "L1 entry size is invalid");
1047 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
1049 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
1050 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
1052 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
1053 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
1054 (int64_t)(le64_to_cpu(header
.grain_offset
)
1055 * BDRV_SECTOR_SIZE
));
1059 ret
= vmdk_add_extent(bs
, file
, false,
1060 le64_to_cpu(header
.capacity
),
1061 le64_to_cpu(header
.gd_offset
) << 9,
1064 le32_to_cpu(header
.num_gtes_per_gt
),
1065 le64_to_cpu(header
.granularity
),
1071 extent
->compressed
=
1072 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1073 if (extent
->compressed
) {
1074 g_free(s
->create_type
);
1075 s
->create_type
= g_strdup("streamOptimized");
1077 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
1078 extent
->version
= le32_to_cpu(header
.version
);
1079 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
1080 ret
= vmdk_init_tables(bs
, extent
, errp
);
1082 /* free extent allocated by vmdk_add_extent */
1083 vmdk_free_last_extent(bs
);
1088 /* find an option value out of descriptor file */
1089 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
1090 char *buf
, int buf_size
)
1092 char *opt_pos
, *opt_end
;
1093 const char *end
= desc
+ strlen(desc
);
1095 opt_pos
= strstr(desc
, opt_name
);
1099 /* Skip "=\"" following opt_name */
1100 opt_pos
+= strlen(opt_name
) + 2;
1101 if (opt_pos
>= end
) {
1105 while (opt_end
< end
&& *opt_end
!= '"') {
1108 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
1111 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
1115 /* Open an extent file and append to bs array */
1116 static int GRAPH_RDLOCK
1117 vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
1118 char *buf
, QDict
*options
, Error
**errp
)
1122 magic
= ldl_be_p(buf
);
1125 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
1127 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
1129 error_setg(errp
, "Image not in VMDK format");
1134 static const char *next_line(const char *s
)
1145 static int GRAPH_RDLOCK
1146 vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
, QDict
*options
,
1156 int64_t sectors
= 0;
1157 int64_t flat_offset
;
1158 char *desc_file_dir
= NULL
;
1160 BdrvChild
*extent_file
;
1161 BdrvChildRole extent_role
;
1162 BDRVVmdkState
*s
= bs
->opaque
;
1163 VmdkExtent
*extent
= NULL
;
1164 char extent_opt_prefix
[32];
1165 Error
*local_err
= NULL
;
1167 GLOBAL_STATE_CODE();
1169 for (p
= desc
; *p
; p
= next_line(p
)) {
1170 /* parse extent line in one of below formats:
1172 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
1173 * RW [size in sectors] SPARSE "file-name.vmdk"
1174 * RW [size in sectors] VMFS "file-name.vmdk"
1175 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
1176 * RW [size in sectors] SESPARSE "file-name.vmdk"
1179 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
1180 access
, §ors
, type
, fname
, &flat_offset
);
1181 if (matches
< 4 || strcmp(access
, "RW")) {
1183 } else if (!strcmp(type
, "FLAT")) {
1184 if (matches
!= 5 || flat_offset
< 0) {
1187 } else if (!strcmp(type
, "VMFS")) {
1193 } else if (matches
!= 4) {
1198 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
1199 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE") &&
1200 strcmp(type
, "SESPARSE")) ||
1201 (strcmp(access
, "RW"))) {
1205 if (path_is_absolute(fname
)) {
1206 extent_path
= g_strdup(fname
);
1208 if (!desc_file_dir
) {
1209 desc_file_dir
= bdrv_dirname(bs
->file
->bs
, errp
);
1210 if (!desc_file_dir
) {
1211 bdrv_refresh_filename(bs
->file
->bs
);
1212 error_prepend(errp
, "Cannot use relative paths with VMDK "
1213 "descriptor file '%s': ",
1214 bs
->file
->bs
->filename
);
1220 extent_path
= g_strconcat(desc_file_dir
, fname
, NULL
);
1223 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
1226 extent_role
= BDRV_CHILD_DATA
;
1227 if (strcmp(type
, "FLAT") != 0 && strcmp(type
, "VMFS") != 0) {
1228 /* non-flat extents have metadata */
1229 extent_role
|= BDRV_CHILD_METADATA
;
1232 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
1233 bs
, &child_of_bds
, extent_role
, false,
1235 g_free(extent_path
);
1237 error_propagate(errp
, local_err
);
1242 /* save to extents array */
1243 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
1246 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
1247 0, 0, 0, 0, 0, &extent
, errp
);
1249 bdrv_graph_rdunlock_main_loop();
1250 bdrv_graph_wrlock();
1251 bdrv_unref_child(bs
, extent_file
);
1252 bdrv_graph_wrunlock();
1253 bdrv_graph_rdlock_main_loop();
1256 extent
->flat_start_offset
= flat_offset
<< 9;
1257 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
1258 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
1259 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
1263 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
1268 bdrv_graph_rdunlock_main_loop();
1269 bdrv_graph_wrlock();
1270 bdrv_unref_child(bs
, extent_file
);
1271 bdrv_graph_wrunlock();
1272 bdrv_graph_rdlock_main_loop();
1275 extent
= &s
->extents
[s
->num_extents
- 1];
1276 } else if (!strcmp(type
, "SESPARSE")) {
1277 ret
= vmdk_open_se_sparse(bs
, extent_file
, bs
->open_flags
, errp
);
1279 bdrv_graph_rdunlock_main_loop();
1280 bdrv_graph_wrlock();
1281 bdrv_unref_child(bs
, extent_file
);
1282 bdrv_graph_wrunlock();
1283 bdrv_graph_rdlock_main_loop();
1286 extent
= &s
->extents
[s
->num_extents
- 1];
1288 error_setg(errp
, "Unsupported extent type '%s'", type
);
1289 bdrv_graph_rdunlock_main_loop();
1290 bdrv_graph_wrlock();
1291 bdrv_unref_child(bs
, extent_file
);
1292 bdrv_graph_wrunlock();
1293 bdrv_graph_rdlock_main_loop();
1297 extent
->type
= g_strdup(type
);
1306 if (np
[-1] == '\n') {
1309 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
1313 g_free(desc_file_dir
);
1317 static int GRAPH_RDLOCK
1318 vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
, QDict
*options
,
1323 BDRVVmdkState
*s
= bs
->opaque
;
1325 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
1326 error_setg(errp
, "invalid VMDK image descriptor");
1330 if (strcmp(ct
, "monolithicFlat") &&
1331 strcmp(ct
, "vmfs") &&
1332 strcmp(ct
, "vmfsSparse") &&
1333 strcmp(ct
, "seSparse") &&
1334 strcmp(ct
, "twoGbMaxExtentSparse") &&
1335 strcmp(ct
, "twoGbMaxExtentFlat")) {
1336 error_setg(errp
, "Unsupported image type '%s'", ct
);
1340 s
->create_type
= g_strdup(ct
);
1342 ret
= vmdk_parse_extents(buf
, bs
, options
, errp
);
1347 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1352 BDRVVmdkState
*s
= bs
->opaque
;
1355 GRAPH_RDLOCK_GUARD_MAINLOOP();
1357 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
1362 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
1367 magic
= ldl_be_p(buf
);
1371 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
1373 s
->desc_offset
= 0x200;
1376 /* No data in the descriptor file */
1377 bs
->file
->role
&= ~BDRV_CHILD_DATA
;
1379 /* Must succeed because we have given up permissions if anything */
1380 bdrv_child_refresh_perms(bs
, bs
->file
, &error_abort
);
1382 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1389 /* try to open parent images, if exist */
1390 ret
= vmdk_parent_open(bs
);
1394 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1398 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1402 qemu_co_mutex_init(&s
->lock
);
1404 /* Disable migration when VMDK images are used */
1405 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1406 "does not support live migration",
1407 bdrv_get_device_or_node_name(bs
));
1408 ret
= migrate_add_blocker_normal(&s
->migration_blocker
, errp
);
1418 g_free(s
->create_type
);
1419 s
->create_type
= NULL
;
1420 vmdk_free_extents(bs
);
1425 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1427 BDRVVmdkState
*s
= bs
->opaque
;
1430 for (i
= 0; i
< s
->num_extents
; i
++) {
1431 if (!s
->extents
[i
].flat
) {
1432 bs
->bl
.pwrite_zeroes_alignment
=
1433 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1434 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1442 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1443 * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting
1444 * a zeroed cluster in the current layer and must not copy data from the
1447 * If @skip_start_sector < @skip_end_sector, the relative range
1448 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1449 * it for call to write user data in the request.
1451 static int coroutine_fn GRAPH_RDLOCK
1452 get_whole_cluster(BlockDriverState
*bs
, VmdkExtent
*extent
,
1453 uint64_t cluster_offset
, uint64_t offset
,
1454 uint64_t skip_start_bytes
, uint64_t skip_end_bytes
,
1458 int64_t cluster_bytes
;
1459 uint8_t *whole_grain
;
1460 bool copy_from_backing
;
1462 /* For COW, align request sector_num to cluster start */
1463 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1464 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1465 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1466 copy_from_backing
= bs
->backing
&& !zeroed
;
1468 if (!copy_from_backing
) {
1469 memset(whole_grain
, 0, skip_start_bytes
);
1470 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1473 assert(skip_end_bytes
<= cluster_bytes
);
1474 /* we will be here if it's first write on non-exist grain(cluster).
1475 * try to read from parent image, if exist */
1476 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1481 /* Read backing data before skip range */
1482 if (skip_start_bytes
> 0) {
1483 if (copy_from_backing
) {
1484 /* qcow2 emits this on bs->file instead of bs->backing */
1485 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_READ
);
1486 ret
= bdrv_co_pread(bs
->backing
, offset
, skip_start_bytes
,
1493 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1494 ret
= bdrv_co_pwrite(extent
->file
, cluster_offset
, skip_start_bytes
,
1501 /* Read backing data after skip range */
1502 if (skip_end_bytes
< cluster_bytes
) {
1503 if (copy_from_backing
) {
1504 /* qcow2 emits this on bs->file instead of bs->backing */
1505 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_READ
);
1506 ret
= bdrv_co_pread(bs
->backing
, offset
+ skip_end_bytes
,
1507 cluster_bytes
- skip_end_bytes
,
1508 whole_grain
+ skip_end_bytes
, 0);
1514 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1515 ret
= bdrv_co_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1516 cluster_bytes
- skip_end_bytes
,
1517 whole_grain
+ skip_end_bytes
, 0);
1526 qemu_vfree(whole_grain
);
1530 static int coroutine_fn GRAPH_RDLOCK
1531 vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
, uint32_t offset
)
1533 offset
= cpu_to_le32(offset
);
1534 /* update L2 table */
1535 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1536 if (bdrv_co_pwrite(extent
->file
,
1537 ((int64_t)m_data
->l2_offset
* 512)
1538 + (m_data
->l2_index
* sizeof(offset
)),
1539 sizeof(offset
), &offset
, 0) < 0) {
1542 /* update backup L2 table */
1543 if (extent
->l1_backup_table_offset
!= 0) {
1544 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1545 if (bdrv_co_pwrite(extent
->file
,
1546 ((int64_t)m_data
->l2_offset
* 512)
1547 + (m_data
->l2_index
* sizeof(offset
)),
1548 sizeof(offset
), &offset
, 0) < 0) {
1552 if (bdrv_co_flush(extent
->file
->bs
) < 0) {
1555 if (m_data
->l2_cache_entry
) {
1556 *m_data
->l2_cache_entry
= offset
;
1563 * get_cluster_offset
1565 * Look up cluster offset in extent file by sector number, and store in
1568 * For flat extents, the start offset as parsed from the description file is
1571 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1572 * offset for a new cluster and update L2 cache. If there is a backing file,
1573 * COW is done before returning; otherwise, zeroes are written to the allocated
1574 * cluster. Both COW and zero writing skips the sector range
1575 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1576 * has new data to write there.
1578 * Returns: VMDK_OK if cluster exists and mapped in the image.
1579 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1580 * VMDK_ERROR if failed.
1582 static int coroutine_fn GRAPH_RDLOCK
1583 get_cluster_offset(BlockDriverState
*bs
, VmdkExtent
*extent
,
1584 VmdkMetaData
*m_data
, uint64_t offset
, bool allocate
,
1585 uint64_t *cluster_offset
, uint64_t skip_start_bytes
,
1586 uint64_t skip_end_bytes
)
1588 unsigned int l1_index
, l2_offset
, l2_index
;
1589 int min_index
, i
, j
;
1592 bool zeroed
= false;
1594 int64_t cluster_sector
;
1595 unsigned int l2_size_bytes
= extent
->l2_size
* extent
->entry_size
;
1598 m_data
->new_allocation
= false;
1601 *cluster_offset
= extent
->flat_start_offset
;
1605 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1606 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1607 if (l1_index
>= extent
->l1_size
) {
1610 if (extent
->sesparse
) {
1611 uint64_t l2_offset_u64
;
1613 assert(extent
->entry_size
== sizeof(uint64_t));
1615 l2_offset_u64
= ((uint64_t *)extent
->l1_table
)[l1_index
];
1616 if (l2_offset_u64
== 0) {
1618 } else if ((l2_offset_u64
& 0xffffffff00000000) != 0x1000000000000000) {
1620 * Top most nibble is 0x1 if grain table is allocated.
1621 * strict check - top most 4 bytes must be 0x10000000 since max
1622 * supported size is 64TB for disk - so no more than 64TB / 16MB
1623 * grain directories which is smaller than uint32,
1624 * where 16MB is the only supported default grain table coverage.
1628 l2_offset_u64
= l2_offset_u64
& 0x00000000ffffffff;
1629 l2_offset_u64
= extent
->sesparse_l2_tables_offset
+
1630 l2_offset_u64
* l2_size_bytes
/ SECTOR_SIZE
;
1631 if (l2_offset_u64
> 0x00000000ffffffff) {
1634 l2_offset
= (unsigned int)(l2_offset_u64
);
1637 assert(extent
->entry_size
== sizeof(uint32_t));
1638 l2_offset
= ((uint32_t *)extent
->l1_table
)[l1_index
];
1641 return VMDK_UNALLOC
;
1643 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1644 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1645 /* increment the hit count */
1646 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1647 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1648 extent
->l2_cache_counts
[j
] >>= 1;
1651 l2_table
= (char *)extent
->l2_cache
+ (i
* l2_size_bytes
);
1655 /* not found: load a new entry in the least used one */
1657 min_count
= 0xffffffff;
1658 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1659 if (extent
->l2_cache_counts
[i
] < min_count
) {
1660 min_count
= extent
->l2_cache_counts
[i
];
1664 l2_table
= (char *)extent
->l2_cache
+ (min_index
* l2_size_bytes
);
1665 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1666 if (bdrv_co_pread(extent
->file
,
1667 (int64_t)l2_offset
* 512,
1674 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1675 extent
->l2_cache_counts
[min_index
] = 1;
1677 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1679 m_data
->l1_index
= l1_index
;
1680 m_data
->l2_index
= l2_index
;
1681 m_data
->l2_offset
= l2_offset
;
1682 m_data
->l2_cache_entry
= ((uint32_t *)l2_table
) + l2_index
;
1685 if (extent
->sesparse
) {
1686 cluster_sector
= le64_to_cpu(((uint64_t *)l2_table
)[l2_index
]);
1687 switch (cluster_sector
& 0xf000000000000000) {
1688 case 0x0000000000000000:
1689 /* unallocated grain */
1690 if (cluster_sector
!= 0) {
1694 case 0x1000000000000000:
1695 /* scsi-unmapped grain - fallthrough */
1696 case 0x2000000000000000:
1700 case 0x3000000000000000:
1701 /* allocated grain */
1702 cluster_sector
= (((cluster_sector
& 0x0fff000000000000) >> 48) |
1703 ((cluster_sector
& 0x0000ffffffffffff) << 12));
1704 cluster_sector
= extent
->sesparse_clusters_offset
+
1705 cluster_sector
* extent
->cluster_sectors
;
1711 cluster_sector
= le32_to_cpu(((uint32_t *)l2_table
)[l2_index
]);
1713 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1718 if (!cluster_sector
|| zeroed
) {
1720 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1722 assert(!extent
->sesparse
);
1724 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1728 cluster_sector
= extent
->next_cluster_sector
;
1729 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1731 /* First of all we write grain itself, to avoid race condition
1732 * that may to corrupt the image.
1733 * This problem may occur because of insufficient space on host disk
1734 * or inappropriate VM shutdown.
1736 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1737 offset
, skip_start_bytes
, skip_end_bytes
,
1743 m_data
->new_allocation
= true;
1746 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1750 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1751 int64_t sector_num
, VmdkExtent
*start_hint
)
1753 VmdkExtent
*extent
= start_hint
;
1756 extent
= &s
->extents
[0];
1758 while (extent
< &s
->extents
[s
->num_extents
]) {
1759 if (sector_num
< extent
->end_sector
) {
1767 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1770 uint64_t extent_begin_offset
, extent_relative_offset
;
1771 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1773 extent_begin_offset
=
1774 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1775 extent_relative_offset
= offset
- extent_begin_offset
;
1776 return extent_relative_offset
% cluster_size
;
1779 static int coroutine_fn GRAPH_RDLOCK
1780 vmdk_co_block_status(BlockDriverState
*bs
, bool want_zero
,
1781 int64_t offset
, int64_t bytes
, int64_t *pnum
,
1782 int64_t *map
, BlockDriverState
**file
)
1784 BDRVVmdkState
*s
= bs
->opaque
;
1785 int64_t index_in_cluster
, n
, ret
;
1786 uint64_t cluster_offset
;
1789 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1793 qemu_co_mutex_lock(&s
->lock
);
1794 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1796 qemu_co_mutex_unlock(&s
->lock
);
1798 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1807 ret
= BDRV_BLOCK_ZERO
;
1810 ret
= BDRV_BLOCK_DATA
;
1811 if (!extent
->compressed
) {
1812 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1813 *map
= cluster_offset
+ index_in_cluster
;
1815 ret
|= BDRV_BLOCK_RECURSE
;
1818 ret
|= BDRV_BLOCK_COMPRESSED
;
1820 *file
= extent
->file
->bs
;
1824 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1825 *pnum
= MIN(n
, bytes
);
1829 static int coroutine_fn GRAPH_RDLOCK
1830 vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1831 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1832 uint64_t qiov_offset
, uint64_t n_bytes
,
1836 VmdkGrainMarker
*data
= NULL
;
1838 QEMUIOVector local_qiov
;
1839 int64_t write_offset
;
1840 int64_t write_end_sector
;
1842 if (extent
->compressed
) {
1843 void *compressed_data
;
1845 /* Only whole clusters */
1846 if (offset_in_cluster
||
1847 n_bytes
> (extent
->cluster_sectors
* SECTOR_SIZE
) ||
1848 (n_bytes
< (extent
->cluster_sectors
* SECTOR_SIZE
) &&
1849 offset
+ n_bytes
!= extent
->end_sector
* SECTOR_SIZE
))
1855 if (!extent
->has_marker
) {
1859 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1860 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1862 compressed_data
= g_malloc(n_bytes
);
1863 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1864 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1865 g_free(compressed_data
);
1867 if (ret
!= Z_OK
|| buf_len
== 0) {
1872 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1873 data
->size
= cpu_to_le32(buf_len
);
1875 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1876 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1878 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1880 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1881 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1883 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1886 write_offset
= cluster_offset
+ offset_in_cluster
;
1887 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1890 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1892 if (extent
->compressed
) {
1893 extent
->next_cluster_sector
= write_end_sector
;
1895 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1905 if (!extent
->compressed
) {
1906 qemu_iovec_destroy(&local_qiov
);
1911 static int coroutine_fn GRAPH_RDLOCK
1912 vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1913 int64_t offset_in_cluster
, QEMUIOVector
*qiov
, int bytes
)
1916 int cluster_bytes
, buf_bytes
;
1917 uint8_t *cluster_buf
, *compressed_data
;
1918 uint8_t *uncomp_buf
;
1920 VmdkGrainMarker
*marker
;
1924 if (!extent
->compressed
) {
1925 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1926 ret
= bdrv_co_preadv(extent
->file
,
1927 cluster_offset
+ offset_in_cluster
, bytes
,
1934 cluster_bytes
= extent
->cluster_sectors
* 512;
1935 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1936 buf_bytes
= cluster_bytes
* 2;
1937 cluster_buf
= g_malloc(buf_bytes
);
1938 uncomp_buf
= g_malloc(cluster_bytes
);
1939 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1940 ret
= bdrv_co_pread(extent
->file
, cluster_offset
, buf_bytes
, cluster_buf
,
1945 compressed_data
= cluster_buf
;
1946 buf_len
= cluster_bytes
;
1947 data_len
= cluster_bytes
;
1948 if (extent
->has_marker
) {
1949 marker
= (VmdkGrainMarker
*)cluster_buf
;
1950 compressed_data
= marker
->data
;
1951 data_len
= le32_to_cpu(marker
->size
);
1953 if (!data_len
|| data_len
> buf_bytes
) {
1957 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1963 if (offset_in_cluster
< 0 ||
1964 offset_in_cluster
+ bytes
> buf_len
) {
1968 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1973 g_free(cluster_buf
);
1977 static int coroutine_fn GRAPH_RDLOCK
1978 vmdk_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1979 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1981 BDRVVmdkState
*s
= bs
->opaque
;
1983 uint64_t n_bytes
, offset_in_cluster
;
1984 VmdkExtent
*extent
= NULL
;
1985 QEMUIOVector local_qiov
;
1986 uint64_t cluster_offset
;
1987 uint64_t bytes_done
= 0;
1989 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1990 qemu_co_mutex_lock(&s
->lock
);
1993 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1998 ret
= get_cluster_offset(bs
, extent
, NULL
,
1999 offset
, false, &cluster_offset
, 0, 0);
2000 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
2002 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
2003 - offset_in_cluster
);
2005 if (ret
!= VMDK_OK
) {
2006 /* if not allocated, try to read from parent image, if exist */
2007 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
2008 if (!vmdk_is_cid_valid(bs
)) {
2013 qemu_iovec_reset(&local_qiov
);
2014 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
2016 /* qcow2 emits this on bs->file instead of bs->backing */
2017 BLKDBG_CO_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
2018 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
2024 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
2027 qemu_iovec_reset(&local_qiov
);
2028 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
2030 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
2031 &local_qiov
, n_bytes
);
2038 bytes_done
+= n_bytes
;
2043 qemu_co_mutex_unlock(&s
->lock
);
2044 qemu_iovec_destroy(&local_qiov
);
2051 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
2052 * if possible, otherwise return -ENOTSUP.
2053 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
2054 * with each cluster. By dry run we can find if the zero write
2055 * is possible without modifying image data.
2057 * Returns: error code with 0 for success.
2059 static int coroutine_fn GRAPH_RDLOCK
2060 vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
2061 QEMUIOVector
*qiov
, bool zeroed
, bool zero_dry_run
)
2063 BDRVVmdkState
*s
= bs
->opaque
;
2064 VmdkExtent
*extent
= NULL
;
2066 int64_t offset_in_cluster
, n_bytes
;
2067 uint64_t cluster_offset
;
2068 uint64_t bytes_done
= 0;
2069 VmdkMetaData m_data
;
2071 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
2072 error_report("Wrong offset: offset=0x%" PRIx64
2073 " total_sectors=0x%" PRIx64
,
2074 offset
, bs
->total_sectors
);
2079 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
2083 if (extent
->sesparse
) {
2086 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
2087 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
2088 - offset_in_cluster
);
2090 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2091 !(extent
->compressed
|| zeroed
),
2092 &cluster_offset
, offset_in_cluster
,
2093 offset_in_cluster
+ n_bytes
);
2094 if (extent
->compressed
) {
2095 if (ret
== VMDK_OK
) {
2096 /* Refuse write to allocated cluster for streamOptimized */
2097 error_report("Could not write to allocated cluster"
2098 " for streamOptimized");
2100 } else if (!zeroed
) {
2102 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2103 true, &cluster_offset
, 0, 0);
2106 if (ret
== VMDK_ERROR
) {
2110 /* Do zeroed write, buf is ignored */
2111 if (extent
->has_zero_grain
&&
2112 offset_in_cluster
== 0 &&
2113 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
2114 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
2115 if (!zero_dry_run
&& ret
!= VMDK_ZEROED
) {
2116 /* update L2 tables */
2117 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
2126 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
2127 qiov
, bytes_done
, n_bytes
, offset
);
2131 if (m_data
.new_allocation
) {
2132 /* update L2 tables */
2133 if (vmdk_L2update(extent
, &m_data
,
2134 cluster_offset
>> BDRV_SECTOR_BITS
)
2142 bytes_done
+= n_bytes
;
2144 /* update CID on the first write every time the virtual disk is
2146 if (!s
->cid_updated
) {
2147 ret
= vmdk_write_cid(bs
, g_random_int());
2151 s
->cid_updated
= true;
2157 static int coroutine_fn GRAPH_RDLOCK
2158 vmdk_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2159 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
2162 BDRVVmdkState
*s
= bs
->opaque
;
2163 qemu_co_mutex_lock(&s
->lock
);
2164 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
2165 qemu_co_mutex_unlock(&s
->lock
);
2169 static int coroutine_fn GRAPH_RDLOCK
2170 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2174 /* The caller will write bytes 0 to signal EOF.
2175 * When receive it, we align EOF to a sector boundary. */
2176 BDRVVmdkState
*s
= bs
->opaque
;
2180 for (i
= 0; i
< s
->num_extents
; i
++) {
2181 length
= bdrv_co_getlength(s
->extents
[i
].file
->bs
);
2185 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
2186 ret
= bdrv_co_truncate(s
->extents
[i
].file
, length
, false,
2187 PREALLOC_MODE_OFF
, 0, NULL
);
2194 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
2197 static int coroutine_fn GRAPH_RDLOCK
2198 vmdk_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2199 BdrvRequestFlags flags
)
2202 BDRVVmdkState
*s
= bs
->opaque
;
2204 qemu_co_mutex_lock(&s
->lock
);
2205 /* write zeroes could fail if sectors not aligned to cluster, test it with
2206 * dry_run == true before really updating image */
2207 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
2209 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
2211 qemu_co_mutex_unlock(&s
->lock
);
2215 static int coroutine_fn GRAPH_UNLOCKED
2216 vmdk_init_extent(BlockBackend
*blk
, int64_t filesize
, bool flat
, bool compress
,
2217 bool zeroed_grain
, Error
**errp
)
2221 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
2222 uint32_t *gd_buf
= NULL
;
2226 ret
= blk_co_truncate(blk
, filesize
, false, PREALLOC_MODE_OFF
, 0, errp
);
2229 magic
= cpu_to_be32(VMDK4_MAGIC
);
2230 memset(&header
, 0, sizeof(header
));
2233 } else if (zeroed_grain
) {
2238 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
2239 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
2240 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
2241 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
2242 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
2243 header
.granularity
= 128;
2244 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
2246 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
2247 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
2249 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
2250 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
2252 header
.desc_offset
= 1;
2253 header
.desc_size
= 20;
2254 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
2255 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
2256 header
.grain_offset
=
2257 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
2258 header
.granularity
);
2259 /* swap endianness for all header fields */
2260 header
.version
= cpu_to_le32(header
.version
);
2261 header
.flags
= cpu_to_le32(header
.flags
);
2262 header
.capacity
= cpu_to_le64(header
.capacity
);
2263 header
.granularity
= cpu_to_le64(header
.granularity
);
2264 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
2265 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
2266 header
.desc_size
= cpu_to_le64(header
.desc_size
);
2267 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
2268 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
2269 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
2270 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
2272 header
.check_bytes
[0] = 0xa;
2273 header
.check_bytes
[1] = 0x20;
2274 header
.check_bytes
[2] = 0xd;
2275 header
.check_bytes
[3] = 0xa;
2277 /* write all the data */
2278 ret
= blk_co_pwrite(blk
, 0, sizeof(magic
), &magic
, 0);
2280 error_setg_errno(errp
, -ret
, "failed to write VMDK magic");
2283 ret
= blk_co_pwrite(blk
, sizeof(magic
), sizeof(header
), &header
, 0);
2285 error_setg_errno(errp
, -ret
, "failed to write VMDK header");
2289 ret
= blk_co_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9, false,
2290 PREALLOC_MODE_OFF
, 0, errp
);
2295 /* write grain directory */
2296 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
2297 gd_buf
= g_malloc0(gd_buf_size
);
2298 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
2299 i
< gt_count
; i
++, tmp
+= gt_size
) {
2300 gd_buf
[i
] = cpu_to_le32(tmp
);
2302 ret
= blk_co_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
2303 gd_buf_size
, gd_buf
, 0);
2305 error_setg_errno(errp
, -ret
, "failed to write VMDK grain directory");
2309 /* write backup grain directory */
2310 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
2311 i
< gt_count
; i
++, tmp
+= gt_size
) {
2312 gd_buf
[i
] = cpu_to_le32(tmp
);
2314 ret
= blk_co_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
2315 gd_buf_size
, gd_buf
, 0);
2317 error_setg_errno(errp
, -ret
,
2318 "failed to write VMDK backup grain directory");
2327 static int coroutine_fn GRAPH_UNLOCKED
2328 vmdk_create_extent(const char *filename
, int64_t filesize
, bool flat
,
2329 bool compress
, bool zeroed_grain
, BlockBackend
**pbb
,
2330 QemuOpts
*opts
, Error
**errp
)
2333 BlockBackend
*blk
= NULL
;
2335 ret
= bdrv_co_create_file(filename
, opts
, errp
);
2340 blk
= blk_co_new_open(filename
, NULL
, NULL
,
2341 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
2348 blk_set_allow_write_beyond_eof(blk
, true);
2350 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
2363 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
2364 char *postfix
, size_t buf_len
, Error
**errp
)
2368 if (filename
== NULL
|| !strlen(filename
)) {
2369 error_setg(errp
, "No filename provided");
2372 p
= strrchr(filename
, '/');
2374 p
= strrchr(filename
, '\\');
2377 p
= strrchr(filename
, ':');
2381 if (p
- filename
>= buf_len
) {
2384 pstrcpy(path
, p
- filename
+ 1, filename
);
2389 q
= strrchr(p
, '.');
2391 pstrcpy(prefix
, buf_len
, p
);
2394 if (q
- p
>= buf_len
) {
2397 pstrcpy(prefix
, q
- p
+ 1, p
);
2398 pstrcpy(postfix
, buf_len
, q
);
2404 * idx == 0: get or create the descriptor file (also the image file if in a
2406 * idx >= 1: get the n-th extent if in a split subformat
2408 typedef BlockBackend
* coroutine_fn GRAPH_UNLOCKED_PTR
2409 (*vmdk_create_extent_fn
)(int64_t size
, int idx
, bool flat
, bool split
,
2410 bool compress
, bool zeroed_grain
, void *opaque
,
2413 static void vmdk_desc_add_extent(GString
*desc
,
2414 const char *extent_line_fmt
,
2415 int64_t size
, const char *filename
)
2417 char *basename
= g_path_get_basename(filename
);
2419 g_string_append_printf(desc
, extent_line_fmt
,
2420 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
2424 static int coroutine_fn GRAPH_UNLOCKED
2425 vmdk_co_do_create(int64_t size
,
2426 BlockdevVmdkSubformat subformat
,
2427 BlockdevVmdkAdapterType adapter_type
,
2428 const char *backing_file
,
2429 const char *hw_version
,
2430 const char *toolsversion
,
2433 vmdk_create_extent_fn extent_fn
,
2438 BlockBackend
*blk
= NULL
;
2439 BlockBackend
*extent_blk
;
2440 Error
*local_err
= NULL
;
2443 bool flat
, split
, compress
;
2444 GString
*ext_desc_lines
;
2445 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
2446 int64_t extent_size
;
2447 int64_t created_size
= 0;
2448 const char *extent_line_fmt
;
2449 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2450 uint32_t parent_cid
= 0xffffffff;
2451 uint32_t number_heads
= 16;
2452 uint32_t desc_offset
= 0, desc_len
;
2453 const char desc_template
[] =
2454 "# Disk DescriptorFile\n"
2457 "parentCID=%" PRIx32
"\n"
2458 "createType=\"%s\"\n"
2461 "# Extent description\n"
2464 "# The Disk Data Base\n"
2467 "ddb.virtualHWVersion = \"%s\"\n"
2468 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2469 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2470 "ddb.geometry.sectors = \"63\"\n"
2471 "ddb.adapterType = \"%s\"\n"
2472 "ddb.toolsVersion = \"%s\"\n";
2474 ext_desc_lines
= g_string_new(NULL
);
2476 /* Read out options */
2480 "compat6 cannot be enabled with hwversion set");
2489 if (!toolsversion
) {
2490 toolsversion
= "2147483647";
2493 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2494 /* that's the number of heads with which vmware operates when
2495 creating, exporting, etc. vmdk files with a non-ide adapter type */
2498 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2499 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2500 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2501 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2502 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2505 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2507 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2509 if (flat
&& backing_file
) {
2510 error_setg(errp
, "Flat image can't have backing file");
2514 if (flat
&& zeroed_grain
) {
2515 error_setg(errp
, "Flat image can't enable zeroed grain");
2520 /* Create extents */
2522 extent_size
= split_size
;
2526 if (!split
&& !flat
) {
2527 created_size
= extent_size
;
2531 /* Get the descriptor file BDS */
2532 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2538 if (!split
&& !flat
) {
2539 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2540 blk_bs(blk
)->filename
);
2544 BlockBackend
*backing
;
2545 char *full_backing
=
2546 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2550 error_propagate(errp
, local_err
);
2554 assert(full_backing
);
2556 backing
= blk_co_new_open(full_backing
, NULL
, NULL
,
2557 BDRV_O_NO_BACKING
, errp
);
2558 g_free(full_backing
);
2559 if (backing
== NULL
) {
2563 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2564 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2565 blk_bs(backing
)->drv
->format_name
);
2566 blk_co_unref(backing
);
2571 bdrv_graph_co_rdlock();
2572 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2573 bdrv_graph_co_rdunlock();
2574 blk_co_unref(backing
);
2576 error_setg(errp
, "Failed to read parent CID");
2579 snprintf(parent_desc_line
, BUF_SIZE
,
2580 "parentFileNameHint=\"%s\"", backing_file
);
2583 while (created_size
< size
) {
2584 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2585 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2586 zeroed_grain
, opaque
, errp
);
2591 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2592 blk_bs(extent_blk
)->filename
);
2593 created_size
+= cur_size
;
2595 blk_co_unref(extent_blk
);
2598 /* Check whether we got excess extents */
2599 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2602 blk_co_unref(extent_blk
);
2603 error_setg(errp
, "List of extents contains unused extents");
2608 /* generate descriptor file */
2609 desc
= g_strdup_printf(desc_template
,
2612 BlockdevVmdkSubformat_str(subformat
),
2614 ext_desc_lines
->str
,
2617 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2619 BlockdevVmdkAdapterType_str(adapter_type
),
2621 desc_len
= strlen(desc
);
2622 /* the descriptor offset = 0x200 */
2623 if (!split
&& !flat
) {
2624 desc_offset
= 0x200;
2627 ret
= blk_co_pwrite(blk
, desc_offset
, desc_len
, desc
, 0);
2629 error_setg_errno(errp
, -ret
, "Could not write description");
2632 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2633 * for description file */
2634 if (desc_offset
== 0) {
2635 ret
= blk_co_truncate(blk
, desc_len
, false, PREALLOC_MODE_OFF
, 0, errp
);
2646 g_free(parent_desc_line
);
2647 g_string_free(ext_desc_lines
, true);
2656 } VMDKCreateOptsData
;
2658 static BlockBackend
* coroutine_fn GRAPH_UNLOCKED
2659 vmdk_co_create_opts_cb(int64_t size
, int idx
, bool flat
, bool split
,
2660 bool compress
, bool zeroed_grain
, void *opaque
,
2663 BlockBackend
*blk
= NULL
;
2664 BlockDriverState
*bs
= NULL
;
2665 VMDKCreateOptsData
*data
= opaque
;
2666 char *ext_filename
= NULL
;
2667 char *rel_filename
= NULL
;
2669 /* We're done, don't create excess extents. */
2671 assert(errp
== NULL
);
2676 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2678 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2680 flat
? 'f' : 's', idx
, data
->postfix
);
2683 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2686 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2687 g_free(rel_filename
);
2689 if (vmdk_create_extent(ext_filename
, size
,
2690 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2696 g_free(ext_filename
);
2700 static int coroutine_fn GRAPH_UNLOCKED
2701 vmdk_co_create_opts(BlockDriver
*drv
, const char *filename
,
2702 QemuOpts
*opts
, Error
**errp
)
2704 Error
*local_err
= NULL
;
2706 int64_t total_size
= 0;
2707 char *adapter_type
= NULL
;
2708 BlockdevVmdkAdapterType adapter_type_enum
;
2709 char *backing_file
= NULL
;
2710 char *hw_version
= NULL
;
2711 char *toolsversion
= NULL
;
2713 BlockdevVmdkSubformat subformat
;
2715 char *path
= g_malloc0(PATH_MAX
);
2716 char *prefix
= g_malloc0(PATH_MAX
);
2717 char *postfix
= g_malloc0(PATH_MAX
);
2718 char *desc_line
= g_malloc0(BUF_SIZE
);
2719 char *ext_filename
= g_malloc0(PATH_MAX
);
2720 char *desc_filename
= g_malloc0(PATH_MAX
);
2721 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2724 VMDKCreateOptsData data
;
2725 char *backing_fmt
= NULL
;
2727 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2728 if (backing_fmt
&& strcmp(backing_fmt
, "vmdk") != 0) {
2729 error_setg(errp
, "backing_file must be a vmdk image");
2734 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2738 /* Read out options */
2739 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2741 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2742 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2743 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2744 toolsversion
= qemu_opt_get_del(opts
, BLOCK_OPT_TOOLSVERSION
);
2745 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2746 if (strcmp(hw_version
, "undefined") == 0) {
2750 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2751 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2754 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2756 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2759 error_propagate(errp
, local_err
);
2764 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2768 /* Default format to monolithicSparse */
2769 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2771 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2773 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2776 error_propagate(errp
, local_err
);
2781 data
= (VMDKCreateOptsData
){
2787 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2788 backing_file
, hw_version
, toolsversion
, compat6
,
2789 zeroed_grain
, vmdk_co_create_opts_cb
, &data
, errp
);
2792 g_free(backing_fmt
);
2793 g_free(adapter_type
);
2794 g_free(backing_file
);
2796 g_free(toolsversion
);
2803 g_free(ext_filename
);
2804 g_free(desc_filename
);
2805 g_free(parent_desc_line
);
2809 static BlockBackend
* coroutine_fn GRAPH_UNLOCKED
2810 vmdk_co_create_cb(int64_t size
, int idx
, bool flat
, bool split
, bool compress
,
2811 bool zeroed_grain
, void *opaque
, Error
**errp
)
2814 BlockDriverState
*bs
;
2816 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2819 bs
= bdrv_co_open_blockdev_ref(opts
->file
, errp
);
2822 BlockdevRefList
*list
= opts
->extents
;
2823 for (i
= 1; i
< idx
; i
++) {
2824 if (!list
|| !list
->next
) {
2825 error_setg(errp
, "Extent [%d] not specified", i
);
2831 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2834 bs
= bdrv_co_open_blockdev_ref(list
->value
, errp
);
2839 blk
= blk_co_new_with_bs(bs
,
2840 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
|
2847 blk_set_allow_write_beyond_eof(blk
, true);
2851 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2860 static int coroutine_fn GRAPH_UNLOCKED
2861 vmdk_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
2863 BlockdevCreateOptionsVmdk
*opts
;
2865 opts
= &create_options
->u
.vmdk
;
2867 /* Validate options */
2868 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2869 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2873 return vmdk_co_do_create(opts
->size
,
2885 static void vmdk_close(BlockDriverState
*bs
)
2887 BDRVVmdkState
*s
= bs
->opaque
;
2889 vmdk_free_extents(bs
);
2890 g_free(s
->create_type
);
2892 migrate_del_blocker(&s
->migration_blocker
);
2895 static int64_t coroutine_fn GRAPH_RDLOCK
2896 vmdk_co_get_allocated_file_size(BlockDriverState
*bs
)
2901 BDRVVmdkState
*s
= bs
->opaque
;
2903 ret
= bdrv_co_get_allocated_file_size(bs
->file
->bs
);
2907 for (i
= 0; i
< s
->num_extents
; i
++) {
2908 if (s
->extents
[i
].file
== bs
->file
) {
2911 r
= bdrv_co_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2920 static int GRAPH_RDLOCK
vmdk_has_zero_init(BlockDriverState
*bs
)
2923 BDRVVmdkState
*s
= bs
->opaque
;
2925 /* If has a flat extent and its underlying storage doesn't have zero init,
2927 for (i
= 0; i
< s
->num_extents
; i
++) {
2928 if (s
->extents
[i
].flat
) {
2929 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2937 static VmdkExtentInfo
* GRAPH_RDLOCK
vmdk_get_extent_info(VmdkExtent
*extent
)
2939 VmdkExtentInfo
*info
= g_new0(VmdkExtentInfo
, 1);
2941 bdrv_refresh_filename(extent
->file
->bs
);
2942 *info
= (VmdkExtentInfo
){
2943 .filename
= g_strdup(extent
->file
->bs
->filename
),
2944 .format
= g_strdup(extent
->type
),
2945 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2946 .compressed
= extent
->compressed
,
2947 .has_compressed
= extent
->compressed
,
2948 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2949 .has_cluster_size
= !extent
->flat
,
2955 static int coroutine_fn GRAPH_RDLOCK
2956 vmdk_co_check(BlockDriverState
*bs
, BdrvCheckResult
*result
, BdrvCheckMode fix
)
2958 BDRVVmdkState
*s
= bs
->opaque
;
2959 VmdkExtent
*extent
= NULL
;
2960 int64_t sector_num
= 0;
2961 int64_t total_sectors
= bdrv_co_nb_sectors(bs
);
2963 uint64_t cluster_offset
;
2970 if (sector_num
>= total_sectors
) {
2973 extent
= find_extent(s
, sector_num
, extent
);
2976 "ERROR: could not find extent for sector %" PRId64
"\n",
2981 ret
= get_cluster_offset(bs
, extent
, NULL
,
2982 sector_num
<< BDRV_SECTOR_BITS
,
2983 false, &cluster_offset
, 0, 0);
2984 if (ret
== VMDK_ERROR
) {
2986 "ERROR: could not get cluster_offset for sector %"
2987 PRId64
"\n", sector_num
);
2990 if (ret
== VMDK_OK
) {
2991 int64_t extent_len
= bdrv_co_getlength(extent
->file
->bs
);
2992 if (extent_len
< 0) {
2994 "ERROR: could not get extent file length for sector %"
2995 PRId64
"\n", sector_num
);
2999 if (cluster_offset
>= extent_len
) {
3001 "ERROR: cluster offset for sector %"
3002 PRId64
" points after EOF\n", sector_num
);
3007 sector_num
+= extent
->cluster_sectors
;
3010 result
->corruptions
++;
3014 static ImageInfoSpecific
* GRAPH_RDLOCK
3015 vmdk_get_specific_info(BlockDriverState
*bs
, Error
**errp
)
3018 BDRVVmdkState
*s
= bs
->opaque
;
3019 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
3020 VmdkExtentInfoList
**tail
;
3022 *spec_info
= (ImageInfoSpecific
){
3023 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
3025 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
3029 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
3030 .create_type
= g_strdup(s
->create_type
),
3032 .parent_cid
= s
->parent_cid
,
3035 tail
= &spec_info
->u
.vmdk
.data
->extents
;
3036 for (i
= 0; i
< s
->num_extents
; i
++) {
3037 QAPI_LIST_APPEND(tail
, vmdk_get_extent_info(&s
->extents
[i
]));
3043 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
3045 return a
->flat
== b
->flat
&&
3046 a
->compressed
== b
->compressed
&&
3047 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
3050 static int coroutine_fn
3051 vmdk_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
3054 BDRVVmdkState
*s
= bs
->opaque
;
3055 assert(s
->num_extents
);
3057 /* See if we have multiple extents but they have different cases */
3058 for (i
= 1; i
< s
->num_extents
; i
++) {
3059 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
3063 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
3064 if (!s
->extents
[0].flat
) {
3065 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
3070 static void GRAPH_RDLOCK
3071 vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
3072 bool backing_overridden
)
3074 /* No children but file and backing can be explicitly specified (TODO) */
3075 qdict_put(target
, "file",
3076 qobject_ref(bs
->file
->bs
->full_open_options
));
3078 if (backing_overridden
) {
3080 qdict_put(target
, "backing",
3081 qobject_ref(bs
->backing
->bs
->full_open_options
));
3083 qdict_put_null(target
, "backing");
3088 static QemuOptsList vmdk_create_opts
= {
3089 .name
= "vmdk-create-opts",
3090 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
3093 .name
= BLOCK_OPT_SIZE
,
3094 .type
= QEMU_OPT_SIZE
,
3095 .help
= "Virtual disk size"
3098 .name
= BLOCK_OPT_ADAPTER_TYPE
,
3099 .type
= QEMU_OPT_STRING
,
3100 .help
= "Virtual adapter type, can be one of "
3101 "ide (default), lsilogic, buslogic or legacyESX"
3104 .name
= BLOCK_OPT_BACKING_FILE
,
3105 .type
= QEMU_OPT_STRING
,
3106 .help
= "File name of a base image"
3109 .name
= BLOCK_OPT_BACKING_FMT
,
3110 .type
= QEMU_OPT_STRING
,
3111 .help
= "Must be 'vmdk' if present",
3114 .name
= BLOCK_OPT_COMPAT6
,
3115 .type
= QEMU_OPT_BOOL
,
3116 .help
= "VMDK version 6 image",
3117 .def_value_str
= "off"
3120 .name
= BLOCK_OPT_HWVERSION
,
3121 .type
= QEMU_OPT_STRING
,
3122 .help
= "VMDK hardware version",
3123 .def_value_str
= "undefined"
3126 .name
= BLOCK_OPT_TOOLSVERSION
,
3127 .type
= QEMU_OPT_STRING
,
3128 .help
= "VMware guest tools version",
3131 .name
= BLOCK_OPT_SUBFMT
,
3132 .type
= QEMU_OPT_STRING
,
3134 "VMDK flat extent format, can be one of "
3135 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
3138 .name
= BLOCK_OPT_ZEROED_GRAIN
,
3139 .type
= QEMU_OPT_BOOL
,
3140 .help
= "Enable efficient zero writes "
3141 "using the zeroed-grain GTE feature"
3143 { /* end of list */ }
3147 static BlockDriver bdrv_vmdk
= {
3148 .format_name
= "vmdk",
3149 .instance_size
= sizeof(BDRVVmdkState
),
3150 .bdrv_probe
= vmdk_probe
,
3151 .bdrv_open
= vmdk_open
,
3152 .bdrv_co_check
= vmdk_co_check
,
3153 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
3154 .bdrv_reopen_commit
= vmdk_reopen_commit
,
3155 .bdrv_reopen_abort
= vmdk_reopen_abort
,
3156 .bdrv_child_perm
= bdrv_default_perms
,
3157 .bdrv_co_preadv
= vmdk_co_preadv
,
3158 .bdrv_co_pwritev
= vmdk_co_pwritev
,
3159 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
3160 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
3161 .bdrv_close
= vmdk_close
,
3162 .bdrv_co_create_opts
= vmdk_co_create_opts
,
3163 .bdrv_co_create
= vmdk_co_create
,
3164 .bdrv_co_block_status
= vmdk_co_block_status
,
3165 .bdrv_co_get_allocated_file_size
= vmdk_co_get_allocated_file_size
,
3166 .bdrv_has_zero_init
= vmdk_has_zero_init
,
3167 .bdrv_get_specific_info
= vmdk_get_specific_info
,
3168 .bdrv_refresh_limits
= vmdk_refresh_limits
,
3169 .bdrv_co_get_info
= vmdk_co_get_info
,
3170 .bdrv_gather_child_options
= vmdk_gather_child_options
,
3173 .supports_backing
= true,
3174 .create_opts
= &vmdk_create_opts
,
3177 static void bdrv_vmdk_init(void)
3179 bdrv_register(&bdrv_vmdk
);
3182 block_init(bdrv_vmdk_init
);