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 "qemu/memalign.h"
37 #include "migration/blocker.h"
38 #include "qemu/cutils.h"
41 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
42 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
43 #define VMDK4_COMPRESSION_DEFLATE 1
44 #define VMDK4_FLAG_NL_DETECT (1 << 0)
45 #define VMDK4_FLAG_RGD (1 << 1)
46 /* Zeroed-grain enable bit */
47 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
48 #define VMDK4_FLAG_COMPRESS (1 << 16)
49 #define VMDK4_FLAG_MARKER (1 << 17)
50 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
52 #define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
54 #define VMDK_GTE_ZEROED 0x1
56 /* VMDK internal error codes */
58 #define VMDK_ERROR (-1)
59 /* Cluster not allocated */
60 #define VMDK_UNALLOC (-2)
61 #define VMDK_ZEROED (-3)
63 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
64 #define BLOCK_OPT_TOOLSVERSION "toolsversion"
69 uint32_t disk_sectors
;
71 uint32_t l1dir_offset
;
73 uint32_t file_sectors
;
76 uint32_t sectors_per_track
;
77 } QEMU_PACKED VMDK3Header
;
86 /* Number of GrainTableEntries per GrainTable */
87 uint32_t num_gtes_per_gt
;
90 uint64_t grain_offset
;
93 uint16_t compressAlgorithm
;
94 } QEMU_PACKED VMDK4Header
;
96 typedef struct VMDKSESparseConstHeader
{
101 uint64_t grain_table_size
;
107 uint64_t volatile_header_offset
;
108 uint64_t volatile_header_size
;
109 uint64_t journal_header_offset
;
110 uint64_t journal_header_size
;
111 uint64_t journal_offset
;
112 uint64_t journal_size
;
113 uint64_t grain_dir_offset
;
114 uint64_t grain_dir_size
;
115 uint64_t grain_tables_offset
;
116 uint64_t grain_tables_size
;
117 uint64_t free_bitmap_offset
;
118 uint64_t free_bitmap_size
;
119 uint64_t backmap_offset
;
120 uint64_t backmap_size
;
121 uint64_t grains_offset
;
122 uint64_t grains_size
;
124 } QEMU_PACKED VMDKSESparseConstHeader
;
126 typedef struct VMDKSESparseVolatileHeader
{
128 uint64_t free_gt_number
;
129 uint64_t next_txn_seq_number
;
130 uint64_t replay_journal
;
132 } QEMU_PACKED VMDKSESparseVolatileHeader
;
134 #define L2_CACHE_SIZE 16
136 typedef struct VmdkExtent
{
143 uint64_t sesparse_l2_tables_offset
;
144 uint64_t sesparse_clusters_offset
;
149 int64_t flat_start_offset
;
150 int64_t l1_table_offset
;
151 int64_t l1_backup_table_offset
;
153 uint32_t *l1_backup_table
;
154 unsigned int l1_size
;
155 uint32_t l1_entry_sectors
;
157 unsigned int l2_size
;
159 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
160 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
162 int64_t cluster_sectors
;
163 int64_t next_cluster_sector
;
167 typedef struct BDRVVmdkState
{
169 uint64_t desc_offset
;
175 /* Extent array with num_extents entries, ascend ordered by address */
177 Error
*migration_blocker
;
181 typedef struct BDRVVmdkReopenState
{
182 bool *extents_using_bs_file
;
183 } BDRVVmdkReopenState
;
185 typedef struct VmdkMetaData
{
186 unsigned int l1_index
;
187 unsigned int l2_index
;
188 unsigned int l2_offset
;
190 uint32_t *l2_cache_entry
;
193 typedef struct VmdkGrainMarker
{
197 } QEMU_PACKED VmdkGrainMarker
;
200 MARKER_END_OF_STREAM
= 0,
201 MARKER_GRAIN_TABLE
= 1,
202 MARKER_GRAIN_DIRECTORY
= 2,
206 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
213 magic
= be32_to_cpu(*(uint32_t *)buf
);
214 if (magic
== VMDK3_MAGIC
||
215 magic
== VMDK4_MAGIC
) {
218 const char *p
= (const char *)buf
;
219 const char *end
= p
+ buf_size
;
222 /* skip comment line */
223 while (p
< end
&& *p
!= '\n') {
230 while (p
< end
&& *p
== ' ') {
233 /* skip '\r' if windows line endings used. */
234 if (p
< end
&& *p
== '\r') {
237 /* only accept blank lines before 'version=' line */
238 if (p
== end
|| *p
!= '\n') {
244 if (end
- p
>= strlen("version=X\n")) {
245 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
246 strncmp("version=2\n", p
, strlen("version=2\n")) == 0 ||
247 strncmp("version=3\n", p
, strlen("version=3\n")) == 0) {
251 if (end
- p
>= strlen("version=X\r\n")) {
252 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
253 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0 ||
254 strncmp("version=3\r\n", p
, strlen("version=3\r\n")) == 0) {
264 #define SECTOR_SIZE 512
265 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
266 #define BUF_SIZE 4096
267 #define HEADER_SIZE 512 /* first sector of 512 bytes */
269 static void vmdk_free_extents(BlockDriverState
*bs
)
272 BDRVVmdkState
*s
= bs
->opaque
;
275 bdrv_graph_wrlock(NULL
);
276 for (i
= 0; i
< s
->num_extents
; i
++) {
280 g_free(e
->l1_backup_table
);
282 if (e
->file
!= bs
->file
) {
283 bdrv_unref_child(bs
, e
->file
);
286 bdrv_graph_wrunlock();
291 static void vmdk_free_last_extent(BlockDriverState
*bs
)
293 BDRVVmdkState
*s
= bs
->opaque
;
295 if (s
->num_extents
== 0) {
299 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
);
302 /* Return -ve errno, or 0 on success and write CID into *pcid. */
303 static int 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 desc
= g_malloc0(DESC_SIZE
);
354 tmp_desc
= g_malloc0(DESC_SIZE
);
355 ret
= bdrv_co_pread(bs
->file
, s
->desc_offset
, DESC_SIZE
, desc
, 0);
360 desc
[DESC_SIZE
- 1] = '\0';
361 tmp_str
= strstr(desc
, "parentCID");
362 if (tmp_str
== NULL
) {
367 pstrcpy(tmp_desc
, DESC_SIZE
, tmp_str
);
368 p_name
= strstr(desc
, "CID");
369 if (p_name
!= NULL
) {
370 p_name
+= sizeof("CID");
371 snprintf(p_name
, DESC_SIZE
- (p_name
- desc
), "%" PRIx32
"\n", cid
);
372 pstrcat(desc
, DESC_SIZE
, tmp_desc
);
375 ret
= bdrv_co_pwrite_sync(bs
->file
, s
->desc_offset
, DESC_SIZE
, desc
, 0);
383 static int coroutine_fn
vmdk_is_cid_valid(BlockDriverState
*bs
)
385 BDRVVmdkState
*s
= bs
->opaque
;
388 if (!s
->cid_checked
&& bs
->backing
) {
389 BlockDriverState
*p_bs
= bs
->backing
->bs
;
391 if (strcmp(p_bs
->drv
->format_name
, "vmdk")) {
392 /* Backing file is not in vmdk format, so it does not have
393 * a CID, which makes the overlay's parent CID invalid */
397 if (vmdk_read_cid(p_bs
, 0, &cur_pcid
) != 0) {
398 /* read failure: report as not valid */
401 if (s
->parent_cid
!= cur_pcid
) {
406 s
->cid_checked
= true;
411 static int vmdk_reopen_prepare(BDRVReopenState
*state
,
412 BlockReopenQueue
*queue
, Error
**errp
)
415 BDRVVmdkReopenState
*rs
;
418 assert(state
!= NULL
);
419 assert(state
->bs
!= NULL
);
420 assert(state
->opaque
== NULL
);
422 s
= state
->bs
->opaque
;
424 rs
= g_new0(BDRVVmdkReopenState
, 1);
428 * Check whether there are any extents stored in bs->file; if bs->file
429 * changes, we will need to update their .file pointers to follow suit
431 rs
->extents_using_bs_file
= g_new(bool, s
->num_extents
);
432 for (i
= 0; i
< s
->num_extents
; i
++) {
433 rs
->extents_using_bs_file
[i
] = s
->extents
[i
].file
== state
->bs
->file
;
439 static void vmdk_reopen_clean(BDRVReopenState
*state
)
441 BDRVVmdkReopenState
*rs
= state
->opaque
;
443 g_free(rs
->extents_using_bs_file
);
445 state
->opaque
= NULL
;
448 static void vmdk_reopen_commit(BDRVReopenState
*state
)
450 BDRVVmdkState
*s
= state
->bs
->opaque
;
451 BDRVVmdkReopenState
*rs
= state
->opaque
;
454 for (i
= 0; i
< s
->num_extents
; i
++) {
455 if (rs
->extents_using_bs_file
[i
]) {
456 s
->extents
[i
].file
= state
->bs
->file
;
460 vmdk_reopen_clean(state
);
463 static void vmdk_reopen_abort(BDRVReopenState
*state
)
465 vmdk_reopen_clean(state
);
468 static int vmdk_parent_open(BlockDriverState
*bs
)
472 BDRVVmdkState
*s
= bs
->opaque
;
475 desc
= g_malloc0(DESC_SIZE
+ 1);
476 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, DESC_SIZE
, desc
, 0);
481 p_name
= strstr(desc
, "parentFileNameHint");
482 if (p_name
!= NULL
) {
485 p_name
+= sizeof("parentFileNameHint") + 1;
486 end_name
= strchr(p_name
, '\"');
487 if (end_name
== NULL
) {
491 if ((end_name
- p_name
) > sizeof(bs
->auto_backing_file
) - 1) {
496 pstrcpy(bs
->auto_backing_file
, end_name
- p_name
+ 1, p_name
);
497 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
),
498 bs
->auto_backing_file
);
499 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
),
508 /* Create and append extent to the extent array. Return the added VmdkExtent
509 * address. return NULL if allocation failed. */
510 static int vmdk_add_extent(BlockDriverState
*bs
,
511 BdrvChild
*file
, bool flat
, int64_t sectors
,
512 int64_t l1_offset
, int64_t l1_backup_offset
,
514 int l2_size
, uint64_t cluster_sectors
,
515 VmdkExtent
**new_extent
,
519 BDRVVmdkState
*s
= bs
->opaque
;
522 if (cluster_sectors
> 0x200000) {
523 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
524 error_setg(errp
, "Invalid granularity, image may be corrupt");
527 if (l1_size
> 32 * 1024 * 1024) {
529 * Although with big capacity and small l1_entry_sectors, we can get a
530 * big l1_size, we don't want unbounded value to allocate the table.
531 * Limit it to 32M, which is enough to store:
532 * 8TB - for both VMDK3 & VMDK4 with
533 * minimal cluster size: 512B
534 * minimal L2 table size: 512 entries
535 * 8 TB is still more than the maximal value supported for
536 * VMDK3 & VMDK4 which is 2TB.
537 * 64TB - for "ESXi seSparse Extent"
538 * minimal cluster size: 512B (default is 4KB)
539 * L2 table size: 4096 entries (const).
540 * 64TB is more than the maximal value supported for
541 * seSparse VMDKs (which is slightly less than 64TB)
543 error_setg(errp
, "L1 size too big");
547 nb_sectors
= bdrv_nb_sectors(file
->bs
);
548 if (nb_sectors
< 0) {
552 s
->extents
= g_renew(VmdkExtent
, s
->extents
, s
->num_extents
+ 1);
553 extent
= &s
->extents
[s
->num_extents
];
556 memset(extent
, 0, sizeof(VmdkExtent
));
559 extent
->sectors
= sectors
;
560 extent
->l1_table_offset
= l1_offset
;
561 extent
->l1_backup_table_offset
= l1_backup_offset
;
562 extent
->l1_size
= l1_size
;
563 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
564 extent
->l2_size
= l2_size
;
565 extent
->cluster_sectors
= flat
? sectors
: cluster_sectors
;
566 extent
->next_cluster_sector
= ROUND_UP(nb_sectors
, cluster_sectors
);
567 extent
->entry_size
= sizeof(uint32_t);
569 if (s
->num_extents
> 1) {
570 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
572 extent
->end_sector
= extent
->sectors
;
574 bs
->total_sectors
= extent
->end_sector
;
576 *new_extent
= extent
;
581 static int GRAPH_RDLOCK
582 vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
, Error
**errp
)
588 /* read the L1 table */
589 l1_size
= extent
->l1_size
* extent
->entry_size
;
590 extent
->l1_table
= g_try_malloc(l1_size
);
591 if (l1_size
&& extent
->l1_table
== NULL
) {
595 ret
= bdrv_pread(extent
->file
, extent
->l1_table_offset
, l1_size
,
596 extent
->l1_table
, 0);
598 bdrv_refresh_filename(extent
->file
->bs
);
599 error_setg_errno(errp
, -ret
,
600 "Could not read l1 table from extent '%s'",
601 extent
->file
->bs
->filename
);
604 for (i
= 0; i
< extent
->l1_size
; i
++) {
605 if (extent
->entry_size
== sizeof(uint64_t)) {
606 le64_to_cpus((uint64_t *)extent
->l1_table
+ i
);
608 assert(extent
->entry_size
== sizeof(uint32_t));
609 le32_to_cpus((uint32_t *)extent
->l1_table
+ i
);
613 if (extent
->l1_backup_table_offset
) {
614 assert(!extent
->sesparse
);
615 extent
->l1_backup_table
= g_try_malloc(l1_size
);
616 if (l1_size
&& extent
->l1_backup_table
== NULL
) {
620 ret
= bdrv_pread(extent
->file
, extent
->l1_backup_table_offset
,
621 l1_size
, extent
->l1_backup_table
, 0);
623 bdrv_refresh_filename(extent
->file
->bs
);
624 error_setg_errno(errp
, -ret
,
625 "Could not read l1 backup table from extent '%s'",
626 extent
->file
->bs
->filename
);
629 for (i
= 0; i
< extent
->l1_size
; i
++) {
630 le32_to_cpus(&extent
->l1_backup_table
[i
]);
635 g_malloc(extent
->entry_size
* extent
->l2_size
* L2_CACHE_SIZE
);
638 g_free(extent
->l1_backup_table
);
640 g_free(extent
->l1_table
);
644 static int GRAPH_RDLOCK
645 vmdk_open_vmfs_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
651 VmdkExtent
*extent
= NULL
;
653 ret
= bdrv_pread(file
, sizeof(magic
), sizeof(header
), &header
, 0);
655 bdrv_refresh_filename(file
->bs
);
656 error_setg_errno(errp
, -ret
,
657 "Could not read header from file '%s'",
661 ret
= vmdk_add_extent(bs
, file
, false,
662 le32_to_cpu(header
.disk_sectors
),
663 (int64_t)le32_to_cpu(header
.l1dir_offset
) << 9,
665 le32_to_cpu(header
.l1dir_size
),
667 le32_to_cpu(header
.granularity
),
673 ret
= vmdk_init_tables(bs
, extent
, errp
);
675 /* free extent allocated by vmdk_add_extent */
676 vmdk_free_last_extent(bs
);
681 #define SESPARSE_CONST_HEADER_MAGIC UINT64_C(0x00000000cafebabe)
682 #define SESPARSE_VOLATILE_HEADER_MAGIC UINT64_C(0x00000000cafecafe)
684 /* Strict checks - format not officially documented */
685 static int check_se_sparse_const_header(VMDKSESparseConstHeader
*header
,
688 header
->magic
= le64_to_cpu(header
->magic
);
689 header
->version
= le64_to_cpu(header
->version
);
690 header
->grain_size
= le64_to_cpu(header
->grain_size
);
691 header
->grain_table_size
= le64_to_cpu(header
->grain_table_size
);
692 header
->flags
= le64_to_cpu(header
->flags
);
693 header
->reserved1
= le64_to_cpu(header
->reserved1
);
694 header
->reserved2
= le64_to_cpu(header
->reserved2
);
695 header
->reserved3
= le64_to_cpu(header
->reserved3
);
696 header
->reserved4
= le64_to_cpu(header
->reserved4
);
698 header
->volatile_header_offset
=
699 le64_to_cpu(header
->volatile_header_offset
);
700 header
->volatile_header_size
= le64_to_cpu(header
->volatile_header_size
);
702 header
->journal_header_offset
= le64_to_cpu(header
->journal_header_offset
);
703 header
->journal_header_size
= le64_to_cpu(header
->journal_header_size
);
705 header
->journal_offset
= le64_to_cpu(header
->journal_offset
);
706 header
->journal_size
= le64_to_cpu(header
->journal_size
);
708 header
->grain_dir_offset
= le64_to_cpu(header
->grain_dir_offset
);
709 header
->grain_dir_size
= le64_to_cpu(header
->grain_dir_size
);
711 header
->grain_tables_offset
= le64_to_cpu(header
->grain_tables_offset
);
712 header
->grain_tables_size
= le64_to_cpu(header
->grain_tables_size
);
714 header
->free_bitmap_offset
= le64_to_cpu(header
->free_bitmap_offset
);
715 header
->free_bitmap_size
= le64_to_cpu(header
->free_bitmap_size
);
717 header
->backmap_offset
= le64_to_cpu(header
->backmap_offset
);
718 header
->backmap_size
= le64_to_cpu(header
->backmap_size
);
720 header
->grains_offset
= le64_to_cpu(header
->grains_offset
);
721 header
->grains_size
= le64_to_cpu(header
->grains_size
);
723 if (header
->magic
!= SESPARSE_CONST_HEADER_MAGIC
) {
724 error_setg(errp
, "Bad const header magic: 0x%016" PRIx64
,
729 if (header
->version
!= 0x0000000200000001) {
730 error_setg(errp
, "Unsupported version: 0x%016" PRIx64
,
735 if (header
->grain_size
!= 8) {
736 error_setg(errp
, "Unsupported grain size: %" PRIu64
,
741 if (header
->grain_table_size
!= 64) {
742 error_setg(errp
, "Unsupported grain table size: %" PRIu64
,
743 header
->grain_table_size
);
747 if (header
->flags
!= 0) {
748 error_setg(errp
, "Unsupported flags: 0x%016" PRIx64
,
753 if (header
->reserved1
!= 0 || header
->reserved2
!= 0 ||
754 header
->reserved3
!= 0 || header
->reserved4
!= 0) {
755 error_setg(errp
, "Unsupported reserved bits:"
756 " 0x%016" PRIx64
" 0x%016" PRIx64
757 " 0x%016" PRIx64
" 0x%016" PRIx64
,
758 header
->reserved1
, header
->reserved2
,
759 header
->reserved3
, header
->reserved4
);
763 /* check that padding is 0 */
764 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
765 error_setg(errp
, "Unsupported non-zero const header padding");
772 static int check_se_sparse_volatile_header(VMDKSESparseVolatileHeader
*header
,
775 header
->magic
= le64_to_cpu(header
->magic
);
776 header
->free_gt_number
= le64_to_cpu(header
->free_gt_number
);
777 header
->next_txn_seq_number
= le64_to_cpu(header
->next_txn_seq_number
);
778 header
->replay_journal
= le64_to_cpu(header
->replay_journal
);
780 if (header
->magic
!= SESPARSE_VOLATILE_HEADER_MAGIC
) {
781 error_setg(errp
, "Bad volatile header magic: 0x%016" PRIx64
,
786 if (header
->replay_journal
) {
787 error_setg(errp
, "Image is dirty, Replaying journal not supported");
791 /* check that padding is 0 */
792 if (!buffer_is_zero(header
->pad
, sizeof(header
->pad
))) {
793 error_setg(errp
, "Unsupported non-zero volatile header padding");
800 static int GRAPH_RDLOCK
801 vmdk_open_se_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
805 VMDKSESparseConstHeader const_header
;
806 VMDKSESparseVolatileHeader volatile_header
;
807 VmdkExtent
*extent
= NULL
;
809 ret
= bdrv_apply_auto_read_only(bs
,
810 "No write support for seSparse images available", errp
);
815 assert(sizeof(const_header
) == SECTOR_SIZE
);
817 ret
= bdrv_pread(file
, 0, sizeof(const_header
), &const_header
, 0);
819 bdrv_refresh_filename(file
->bs
);
820 error_setg_errno(errp
, -ret
,
821 "Could not read const header from file '%s'",
826 /* check const header */
827 ret
= check_se_sparse_const_header(&const_header
, errp
);
832 assert(sizeof(volatile_header
) == SECTOR_SIZE
);
834 ret
= bdrv_pread(file
, const_header
.volatile_header_offset
* SECTOR_SIZE
,
835 sizeof(volatile_header
), &volatile_header
, 0);
837 bdrv_refresh_filename(file
->bs
);
838 error_setg_errno(errp
, -ret
,
839 "Could not read volatile header from file '%s'",
844 /* check volatile header */
845 ret
= check_se_sparse_volatile_header(&volatile_header
, errp
);
850 ret
= vmdk_add_extent(bs
, file
, false,
851 const_header
.capacity
,
852 const_header
.grain_dir_offset
* SECTOR_SIZE
,
854 const_header
.grain_dir_size
*
855 SECTOR_SIZE
/ sizeof(uint64_t),
856 const_header
.grain_table_size
*
857 SECTOR_SIZE
/ sizeof(uint64_t),
858 const_header
.grain_size
,
865 extent
->sesparse
= true;
866 extent
->sesparse_l2_tables_offset
= const_header
.grain_tables_offset
;
867 extent
->sesparse_clusters_offset
= const_header
.grains_offset
;
868 extent
->entry_size
= sizeof(uint64_t);
870 ret
= vmdk_init_tables(bs
, extent
, errp
);
872 /* free extent allocated by vmdk_add_extent */
873 vmdk_free_last_extent(bs
);
879 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
,
880 QDict
*options
, Error
**errp
);
882 static char *vmdk_read_desc(BdrvChild
*file
, uint64_t desc_offset
, Error
**errp
)
888 size
= bdrv_getlength(file
->bs
);
890 error_setg_errno(errp
, -size
, "Could not access file");
895 /* Both descriptor file and sparse image must be much larger than 4
896 * bytes, also callers of vmdk_read_desc want to compare the first 4
897 * bytes with VMDK4_MAGIC, let's error out if less is read. */
898 error_setg(errp
, "File is too small, not a valid image");
902 size
= MIN(size
, (1 << 20) - 1); /* avoid unbounded allocation */
903 buf
= g_malloc(size
+ 1);
905 ret
= bdrv_pread(file
, desc_offset
, size
, buf
, 0);
907 error_setg_errno(errp
, -ret
, "Could not read from file");
916 static int GRAPH_RDLOCK
917 vmdk_open_vmdk4(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
918 QDict
*options
, Error
**errp
)
922 uint32_t l1_size
, l1_entry_sectors
;
924 VmdkExtent
*extent
= NULL
;
925 BDRVVmdkState
*s
= bs
->opaque
;
926 int64_t l1_backup_offset
= 0;
929 ret
= bdrv_pread(file
, sizeof(magic
), sizeof(header
), &header
, 0);
931 bdrv_refresh_filename(file
->bs
);
932 error_setg_errno(errp
, -ret
,
933 "Could not read header from file '%s'",
937 if (header
.capacity
== 0) {
938 uint64_t desc_offset
= le64_to_cpu(header
.desc_offset
);
940 char *buf
= vmdk_read_desc(file
, desc_offset
<< 9, errp
);
944 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
950 if (!s
->create_type
) {
951 s
->create_type
= g_strdup("monolithicSparse");
954 if (le64_to_cpu(header
.gd_offset
) == VMDK4_GD_AT_END
) {
956 * The footer takes precedence over the header, so read it in. The
957 * footer starts at offset -1024 from the end: One sector for the
958 * footer, and another one for the end-of-stream marker.
965 uint8_t pad
[512 - 16];
966 } QEMU_PACKED footer_marker
;
970 uint8_t pad
[512 - 4 - sizeof(VMDK4Header
)];
976 uint8_t pad
[512 - 16];
977 } QEMU_PACKED eos_marker
;
978 } QEMU_PACKED footer
;
980 ret
= bdrv_pread(file
, bs
->file
->bs
->total_sectors
* 512 - 1536,
981 sizeof(footer
), &footer
, 0);
983 error_setg_errno(errp
, -ret
, "Failed to read footer");
987 /* Some sanity checks for the footer */
988 if (be32_to_cpu(footer
.magic
) != VMDK4_MAGIC
||
989 le32_to_cpu(footer
.footer_marker
.size
) != 0 ||
990 le32_to_cpu(footer
.footer_marker
.type
) != MARKER_FOOTER
||
991 le64_to_cpu(footer
.eos_marker
.val
) != 0 ||
992 le32_to_cpu(footer
.eos_marker
.size
) != 0 ||
993 le32_to_cpu(footer
.eos_marker
.type
) != MARKER_END_OF_STREAM
)
995 error_setg(errp
, "Invalid footer");
999 header
= footer
.header
;
1003 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1004 if (le32_to_cpu(header
.version
) > 3) {
1005 error_setg(errp
, "Unsupported VMDK version %" PRIu32
,
1006 le32_to_cpu(header
.version
));
1008 } else if (le32_to_cpu(header
.version
) == 3 && (flags
& BDRV_O_RDWR
) &&
1010 /* VMware KB 2064959 explains that version 3 added support for
1011 * persistent changed block tracking (CBT), and backup software can
1012 * read it as version=1 if it doesn't care about the changed area
1013 * information. So we are safe to enable read only. */
1014 error_setg(errp
, "VMDK version 3 must be read only");
1018 if (le32_to_cpu(header
.num_gtes_per_gt
) > 512) {
1019 error_setg(errp
, "L2 table size too big");
1023 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gt
)
1024 * le64_to_cpu(header
.granularity
);
1025 if (l1_entry_sectors
== 0) {
1026 error_setg(errp
, "L1 entry size is invalid");
1029 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
1031 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
1032 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
1034 if (bdrv_nb_sectors(file
->bs
) < le64_to_cpu(header
.grain_offset
)) {
1035 error_setg(errp
, "File truncated, expecting at least %" PRId64
" bytes",
1036 (int64_t)(le64_to_cpu(header
.grain_offset
)
1037 * BDRV_SECTOR_SIZE
));
1041 ret
= vmdk_add_extent(bs
, file
, false,
1042 le64_to_cpu(header
.capacity
),
1043 le64_to_cpu(header
.gd_offset
) << 9,
1046 le32_to_cpu(header
.num_gtes_per_gt
),
1047 le64_to_cpu(header
.granularity
),
1053 extent
->compressed
=
1054 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
1055 if (extent
->compressed
) {
1056 g_free(s
->create_type
);
1057 s
->create_type
= g_strdup("streamOptimized");
1059 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
1060 extent
->version
= le32_to_cpu(header
.version
);
1061 extent
->has_zero_grain
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_ZERO_GRAIN
;
1062 ret
= vmdk_init_tables(bs
, extent
, errp
);
1064 /* free extent allocated by vmdk_add_extent */
1065 vmdk_free_last_extent(bs
);
1070 /* find an option value out of descriptor file */
1071 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
1072 char *buf
, int buf_size
)
1074 char *opt_pos
, *opt_end
;
1075 const char *end
= desc
+ strlen(desc
);
1077 opt_pos
= strstr(desc
, opt_name
);
1081 /* Skip "=\"" following opt_name */
1082 opt_pos
+= strlen(opt_name
) + 2;
1083 if (opt_pos
>= end
) {
1087 while (opt_end
< end
&& *opt_end
!= '"') {
1090 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
1093 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
1097 /* Open an extent file and append to bs array */
1098 static int GRAPH_RDLOCK
1099 vmdk_open_sparse(BlockDriverState
*bs
, BdrvChild
*file
, int flags
,
1100 char *buf
, QDict
*options
, Error
**errp
)
1104 magic
= ldl_be_p(buf
);
1107 return vmdk_open_vmfs_sparse(bs
, file
, flags
, errp
);
1109 return vmdk_open_vmdk4(bs
, file
, flags
, options
, errp
);
1111 error_setg(errp
, "Image not in VMDK format");
1116 static const char *next_line(const char *s
)
1127 static int GRAPH_RDLOCK
1128 vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
, QDict
*options
,
1137 int64_t sectors
= 0;
1138 int64_t flat_offset
;
1139 char *desc_file_dir
= NULL
;
1141 BdrvChild
*extent_file
;
1142 BdrvChildRole extent_role
;
1143 BDRVVmdkState
*s
= bs
->opaque
;
1144 VmdkExtent
*extent
= NULL
;
1145 char extent_opt_prefix
[32];
1146 Error
*local_err
= NULL
;
1148 GLOBAL_STATE_CODE();
1150 for (p
= desc
; *p
; p
= next_line(p
)) {
1151 /* parse extent line in one of below formats:
1153 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
1154 * RW [size in sectors] SPARSE "file-name.vmdk"
1155 * RW [size in sectors] VMFS "file-name.vmdk"
1156 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
1157 * RW [size in sectors] SESPARSE "file-name.vmdk"
1160 matches
= sscanf(p
, "%10s %" SCNd64
" %10s \"%511[^\n\r\"]\" %" SCNd64
,
1161 access
, §ors
, type
, fname
, &flat_offset
);
1162 if (matches
< 4 || strcmp(access
, "RW")) {
1164 } else if (!strcmp(type
, "FLAT")) {
1165 if (matches
!= 5 || flat_offset
< 0) {
1168 } else if (!strcmp(type
, "VMFS")) {
1174 } else if (matches
!= 4) {
1179 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE") &&
1180 strcmp(type
, "VMFS") && strcmp(type
, "VMFSSPARSE") &&
1181 strcmp(type
, "SESPARSE")) ||
1182 (strcmp(access
, "RW"))) {
1186 if (path_is_absolute(fname
)) {
1187 extent_path
= g_strdup(fname
);
1189 if (!desc_file_dir
) {
1190 desc_file_dir
= bdrv_dirname(bs
->file
->bs
, errp
);
1191 if (!desc_file_dir
) {
1192 bdrv_refresh_filename(bs
->file
->bs
);
1193 error_prepend(errp
, "Cannot use relative paths with VMDK "
1194 "descriptor file '%s': ",
1195 bs
->file
->bs
->filename
);
1201 extent_path
= g_strconcat(desc_file_dir
, fname
, NULL
);
1204 ret
= snprintf(extent_opt_prefix
, 32, "extents.%d", s
->num_extents
);
1207 extent_role
= BDRV_CHILD_DATA
;
1208 if (strcmp(type
, "FLAT") != 0 && strcmp(type
, "VMFS") != 0) {
1209 /* non-flat extents have metadata */
1210 extent_role
|= BDRV_CHILD_METADATA
;
1213 extent_file
= bdrv_open_child(extent_path
, options
, extent_opt_prefix
,
1214 bs
, &child_of_bds
, extent_role
, false,
1216 g_free(extent_path
);
1218 error_propagate(errp
, local_err
);
1223 /* save to extents array */
1224 if (!strcmp(type
, "FLAT") || !strcmp(type
, "VMFS")) {
1227 ret
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
1228 0, 0, 0, 0, 0, &extent
, errp
);
1230 bdrv_graph_rdunlock_main_loop();
1231 bdrv_graph_wrlock(NULL
);
1232 bdrv_unref_child(bs
, extent_file
);
1233 bdrv_graph_wrunlock();
1234 bdrv_graph_rdlock_main_loop();
1237 extent
->flat_start_offset
= flat_offset
<< 9;
1238 } else if (!strcmp(type
, "SPARSE") || !strcmp(type
, "VMFSSPARSE")) {
1239 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
1240 char *buf
= vmdk_read_desc(extent_file
, 0, errp
);
1244 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
, buf
,
1249 bdrv_graph_rdunlock_main_loop();
1250 bdrv_graph_wrlock(NULL
);
1251 bdrv_unref_child(bs
, extent_file
);
1252 bdrv_graph_wrunlock();
1253 bdrv_graph_rdlock_main_loop();
1256 extent
= &s
->extents
[s
->num_extents
- 1];
1257 } else if (!strcmp(type
, "SESPARSE")) {
1258 ret
= vmdk_open_se_sparse(bs
, extent_file
, bs
->open_flags
, errp
);
1260 bdrv_graph_rdunlock_main_loop();
1261 bdrv_graph_wrlock(NULL
);
1262 bdrv_unref_child(bs
, extent_file
);
1263 bdrv_graph_wrunlock();
1264 bdrv_graph_rdlock_main_loop();
1267 extent
= &s
->extents
[s
->num_extents
- 1];
1269 error_setg(errp
, "Unsupported extent type '%s'", type
);
1270 bdrv_graph_rdunlock_main_loop();
1271 bdrv_graph_wrlock(NULL
);
1272 bdrv_unref_child(bs
, extent_file
);
1273 bdrv_graph_wrunlock();
1274 bdrv_graph_rdlock_main_loop();
1278 extent
->type
= g_strdup(type
);
1287 if (np
[-1] == '\n') {
1290 error_setg(errp
, "Invalid extent line: %.*s", (int)(np
- p
), p
);
1294 g_free(desc_file_dir
);
1298 static int GRAPH_RDLOCK
1299 vmdk_open_desc_file(BlockDriverState
*bs
, int flags
, char *buf
, QDict
*options
,
1304 BDRVVmdkState
*s
= bs
->opaque
;
1306 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
1307 error_setg(errp
, "invalid VMDK image descriptor");
1311 if (strcmp(ct
, "monolithicFlat") &&
1312 strcmp(ct
, "vmfs") &&
1313 strcmp(ct
, "vmfsSparse") &&
1314 strcmp(ct
, "seSparse") &&
1315 strcmp(ct
, "twoGbMaxExtentSparse") &&
1316 strcmp(ct
, "twoGbMaxExtentFlat")) {
1317 error_setg(errp
, "Unsupported image type '%s'", ct
);
1321 s
->create_type
= g_strdup(ct
);
1323 ret
= vmdk_parse_extents(buf
, bs
, options
, errp
);
1328 static int vmdk_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1333 BDRVVmdkState
*s
= bs
->opaque
;
1336 GRAPH_RDLOCK_GUARD_MAINLOOP();
1338 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
1343 buf
= vmdk_read_desc(bs
->file
, 0, errp
);
1348 magic
= ldl_be_p(buf
);
1352 ret
= vmdk_open_sparse(bs
, bs
->file
, flags
, buf
, options
,
1354 s
->desc_offset
= 0x200;
1357 /* No data in the descriptor file */
1358 bs
->file
->role
&= ~BDRV_CHILD_DATA
;
1360 /* Must succeed because we have given up permissions if anything */
1361 bdrv_child_refresh_perms(bs
, bs
->file
, &error_abort
);
1363 ret
= vmdk_open_desc_file(bs
, flags
, buf
, options
, errp
);
1370 /* try to open parent images, if exist */
1371 ret
= vmdk_parent_open(bs
);
1375 ret
= vmdk_read_cid(bs
, 0, &s
->cid
);
1379 ret
= vmdk_read_cid(bs
, 1, &s
->parent_cid
);
1383 qemu_co_mutex_init(&s
->lock
);
1385 /* Disable migration when VMDK images are used */
1386 error_setg(&s
->migration_blocker
, "The vmdk format used by node '%s' "
1387 "does not support live migration",
1388 bdrv_get_device_or_node_name(bs
));
1389 ret
= migrate_add_blocker(s
->migration_blocker
, errp
);
1391 error_free(s
->migration_blocker
);
1400 g_free(s
->create_type
);
1401 s
->create_type
= NULL
;
1402 vmdk_free_extents(bs
);
1407 static void vmdk_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1409 BDRVVmdkState
*s
= bs
->opaque
;
1412 for (i
= 0; i
< s
->num_extents
; i
++) {
1413 if (!s
->extents
[i
].flat
) {
1414 bs
->bl
.pwrite_zeroes_alignment
=
1415 MAX(bs
->bl
.pwrite_zeroes_alignment
,
1416 s
->extents
[i
].cluster_sectors
<< BDRV_SECTOR_BITS
);
1424 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1425 * to the cluster at @cluster_sector_num. If @zeroed is true, we're overwriting
1426 * a zeroed cluster in the current layer and must not copy data from the
1429 * If @skip_start_sector < @skip_end_sector, the relative range
1430 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1431 * it for call to write user data in the request.
1433 static int coroutine_fn GRAPH_RDLOCK
1434 get_whole_cluster(BlockDriverState
*bs
, VmdkExtent
*extent
,
1435 uint64_t cluster_offset
, uint64_t offset
,
1436 uint64_t skip_start_bytes
, uint64_t skip_end_bytes
,
1440 int64_t cluster_bytes
;
1441 uint8_t *whole_grain
;
1442 bool copy_from_backing
;
1444 /* For COW, align request sector_num to cluster start */
1445 cluster_bytes
= extent
->cluster_sectors
<< BDRV_SECTOR_BITS
;
1446 offset
= QEMU_ALIGN_DOWN(offset
, cluster_bytes
);
1447 whole_grain
= qemu_blockalign(bs
, cluster_bytes
);
1448 copy_from_backing
= bs
->backing
&& !zeroed
;
1450 if (!copy_from_backing
) {
1451 memset(whole_grain
, 0, skip_start_bytes
);
1452 memset(whole_grain
+ skip_end_bytes
, 0, cluster_bytes
- skip_end_bytes
);
1455 assert(skip_end_bytes
<= cluster_bytes
);
1456 /* we will be here if it's first write on non-exist grain(cluster).
1457 * try to read from parent image, if exist */
1458 if (bs
->backing
&& !vmdk_is_cid_valid(bs
)) {
1463 /* Read backing data before skip range */
1464 if (skip_start_bytes
> 0) {
1465 if (copy_from_backing
) {
1466 /* qcow2 emits this on bs->file instead of bs->backing */
1467 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_READ
);
1468 ret
= bdrv_co_pread(bs
->backing
, offset
, skip_start_bytes
,
1475 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1476 ret
= bdrv_co_pwrite(extent
->file
, cluster_offset
, skip_start_bytes
,
1483 /* Read backing data after skip range */
1484 if (skip_end_bytes
< cluster_bytes
) {
1485 if (copy_from_backing
) {
1486 /* qcow2 emits this on bs->file instead of bs->backing */
1487 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_READ
);
1488 ret
= bdrv_co_pread(bs
->backing
, offset
+ skip_end_bytes
,
1489 cluster_bytes
- skip_end_bytes
,
1490 whole_grain
+ skip_end_bytes
, 0);
1496 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_COW_WRITE
);
1497 ret
= bdrv_co_pwrite(extent
->file
, cluster_offset
+ skip_end_bytes
,
1498 cluster_bytes
- skip_end_bytes
,
1499 whole_grain
+ skip_end_bytes
, 0);
1508 qemu_vfree(whole_grain
);
1512 static int coroutine_fn GRAPH_RDLOCK
1513 vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
, uint32_t offset
)
1515 offset
= cpu_to_le32(offset
);
1516 /* update L2 table */
1517 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_L2_UPDATE
);
1518 if (bdrv_co_pwrite(extent
->file
,
1519 ((int64_t)m_data
->l2_offset
* 512)
1520 + (m_data
->l2_index
* sizeof(offset
)),
1521 sizeof(offset
), &offset
, 0) < 0) {
1524 /* update backup L2 table */
1525 if (extent
->l1_backup_table_offset
!= 0) {
1526 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
1527 if (bdrv_co_pwrite(extent
->file
,
1528 ((int64_t)m_data
->l2_offset
* 512)
1529 + (m_data
->l2_index
* sizeof(offset
)),
1530 sizeof(offset
), &offset
, 0) < 0) {
1534 if (bdrv_co_flush(extent
->file
->bs
) < 0) {
1537 if (m_data
->l2_cache_entry
) {
1538 *m_data
->l2_cache_entry
= offset
;
1545 * get_cluster_offset
1547 * Look up cluster offset in extent file by sector number, and store in
1550 * For flat extents, the start offset as parsed from the description file is
1553 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1554 * offset for a new cluster and update L2 cache. If there is a backing file,
1555 * COW is done before returning; otherwise, zeroes are written to the allocated
1556 * cluster. Both COW and zero writing skips the sector range
1557 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1558 * has new data to write there.
1560 * Returns: VMDK_OK if cluster exists and mapped in the image.
1561 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1562 * VMDK_ERROR if failed.
1564 static int coroutine_fn GRAPH_RDLOCK
1565 get_cluster_offset(BlockDriverState
*bs
, VmdkExtent
*extent
,
1566 VmdkMetaData
*m_data
, uint64_t offset
, bool allocate
,
1567 uint64_t *cluster_offset
, uint64_t skip_start_bytes
,
1568 uint64_t skip_end_bytes
)
1570 unsigned int l1_index
, l2_offset
, l2_index
;
1571 int min_index
, i
, j
;
1574 bool zeroed
= false;
1576 int64_t cluster_sector
;
1577 unsigned int l2_size_bytes
= extent
->l2_size
* extent
->entry_size
;
1580 m_data
->new_allocation
= false;
1583 *cluster_offset
= extent
->flat_start_offset
;
1587 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
1588 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
1589 if (l1_index
>= extent
->l1_size
) {
1592 if (extent
->sesparse
) {
1593 uint64_t l2_offset_u64
;
1595 assert(extent
->entry_size
== sizeof(uint64_t));
1597 l2_offset_u64
= ((uint64_t *)extent
->l1_table
)[l1_index
];
1598 if (l2_offset_u64
== 0) {
1600 } else if ((l2_offset_u64
& 0xffffffff00000000) != 0x1000000000000000) {
1602 * Top most nibble is 0x1 if grain table is allocated.
1603 * strict check - top most 4 bytes must be 0x10000000 since max
1604 * supported size is 64TB for disk - so no more than 64TB / 16MB
1605 * grain directories which is smaller than uint32,
1606 * where 16MB is the only supported default grain table coverage.
1610 l2_offset_u64
= l2_offset_u64
& 0x00000000ffffffff;
1611 l2_offset_u64
= extent
->sesparse_l2_tables_offset
+
1612 l2_offset_u64
* l2_size_bytes
/ SECTOR_SIZE
;
1613 if (l2_offset_u64
> 0x00000000ffffffff) {
1616 l2_offset
= (unsigned int)(l2_offset_u64
);
1619 assert(extent
->entry_size
== sizeof(uint32_t));
1620 l2_offset
= ((uint32_t *)extent
->l1_table
)[l1_index
];
1623 return VMDK_UNALLOC
;
1625 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1626 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
1627 /* increment the hit count */
1628 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
1629 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
1630 extent
->l2_cache_counts
[j
] >>= 1;
1633 l2_table
= (char *)extent
->l2_cache
+ (i
* l2_size_bytes
);
1637 /* not found: load a new entry in the least used one */
1639 min_count
= 0xffffffff;
1640 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
1641 if (extent
->l2_cache_counts
[i
] < min_count
) {
1642 min_count
= extent
->l2_cache_counts
[i
];
1646 l2_table
= (char *)extent
->l2_cache
+ (min_index
* l2_size_bytes
);
1647 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_L2_LOAD
);
1648 if (bdrv_co_pread(extent
->file
,
1649 (int64_t)l2_offset
* 512,
1656 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
1657 extent
->l2_cache_counts
[min_index
] = 1;
1659 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
1661 m_data
->l1_index
= l1_index
;
1662 m_data
->l2_index
= l2_index
;
1663 m_data
->l2_offset
= l2_offset
;
1664 m_data
->l2_cache_entry
= ((uint32_t *)l2_table
) + l2_index
;
1667 if (extent
->sesparse
) {
1668 cluster_sector
= le64_to_cpu(((uint64_t *)l2_table
)[l2_index
]);
1669 switch (cluster_sector
& 0xf000000000000000) {
1670 case 0x0000000000000000:
1671 /* unallocated grain */
1672 if (cluster_sector
!= 0) {
1676 case 0x1000000000000000:
1677 /* scsi-unmapped grain - fallthrough */
1678 case 0x2000000000000000:
1682 case 0x3000000000000000:
1683 /* allocated grain */
1684 cluster_sector
= (((cluster_sector
& 0x0fff000000000000) >> 48) |
1685 ((cluster_sector
& 0x0000ffffffffffff) << 12));
1686 cluster_sector
= extent
->sesparse_clusters_offset
+
1687 cluster_sector
* extent
->cluster_sectors
;
1693 cluster_sector
= le32_to_cpu(((uint32_t *)l2_table
)[l2_index
]);
1695 if (extent
->has_zero_grain
&& cluster_sector
== VMDK_GTE_ZEROED
) {
1700 if (!cluster_sector
|| zeroed
) {
1702 return zeroed
? VMDK_ZEROED
: VMDK_UNALLOC
;
1704 assert(!extent
->sesparse
);
1706 if (extent
->next_cluster_sector
>= VMDK_EXTENT_MAX_SECTORS
) {
1710 cluster_sector
= extent
->next_cluster_sector
;
1711 extent
->next_cluster_sector
+= extent
->cluster_sectors
;
1713 /* First of all we write grain itself, to avoid race condition
1714 * that may to corrupt the image.
1715 * This problem may occur because of insufficient space on host disk
1716 * or inappropriate VM shutdown.
1718 ret
= get_whole_cluster(bs
, extent
, cluster_sector
* BDRV_SECTOR_SIZE
,
1719 offset
, skip_start_bytes
, skip_end_bytes
,
1725 m_data
->new_allocation
= true;
1728 *cluster_offset
= cluster_sector
<< BDRV_SECTOR_BITS
;
1732 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
1733 int64_t sector_num
, VmdkExtent
*start_hint
)
1735 VmdkExtent
*extent
= start_hint
;
1738 extent
= &s
->extents
[0];
1740 while (extent
< &s
->extents
[s
->num_extents
]) {
1741 if (sector_num
< extent
->end_sector
) {
1749 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent
*extent
,
1752 uint64_t extent_begin_offset
, extent_relative_offset
;
1753 uint64_t cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1755 extent_begin_offset
=
1756 (extent
->end_sector
- extent
->sectors
) * BDRV_SECTOR_SIZE
;
1757 extent_relative_offset
= offset
- extent_begin_offset
;
1758 return extent_relative_offset
% cluster_size
;
1761 static int coroutine_fn GRAPH_RDLOCK
1762 vmdk_co_block_status(BlockDriverState
*bs
, bool want_zero
,
1763 int64_t offset
, int64_t bytes
, int64_t *pnum
,
1764 int64_t *map
, BlockDriverState
**file
)
1766 BDRVVmdkState
*s
= bs
->opaque
;
1767 int64_t index_in_cluster
, n
, ret
;
1768 uint64_t cluster_offset
;
1771 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, NULL
);
1775 qemu_co_mutex_lock(&s
->lock
);
1776 ret
= get_cluster_offset(bs
, extent
, NULL
, offset
, false, &cluster_offset
,
1778 qemu_co_mutex_unlock(&s
->lock
);
1780 index_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1789 ret
= BDRV_BLOCK_ZERO
;
1792 ret
= BDRV_BLOCK_DATA
;
1793 if (!extent
->compressed
) {
1794 ret
|= BDRV_BLOCK_OFFSET_VALID
;
1795 *map
= cluster_offset
+ index_in_cluster
;
1797 ret
|= BDRV_BLOCK_RECURSE
;
1800 ret
|= BDRV_BLOCK_COMPRESSED
;
1802 *file
= extent
->file
->bs
;
1806 n
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
- index_in_cluster
;
1807 *pnum
= MIN(n
, bytes
);
1811 static int coroutine_fn GRAPH_RDLOCK
1812 vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1813 int64_t offset_in_cluster
, QEMUIOVector
*qiov
,
1814 uint64_t qiov_offset
, uint64_t n_bytes
,
1818 VmdkGrainMarker
*data
= NULL
;
1820 QEMUIOVector local_qiov
;
1821 int64_t write_offset
;
1822 int64_t write_end_sector
;
1824 if (extent
->compressed
) {
1825 void *compressed_data
;
1827 /* Only whole clusters */
1828 if (offset_in_cluster
||
1829 n_bytes
> (extent
->cluster_sectors
* SECTOR_SIZE
) ||
1830 (n_bytes
< (extent
->cluster_sectors
* SECTOR_SIZE
) &&
1831 offset
+ n_bytes
!= extent
->end_sector
* SECTOR_SIZE
))
1837 if (!extent
->has_marker
) {
1841 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
1842 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
1844 compressed_data
= g_malloc(n_bytes
);
1845 qemu_iovec_to_buf(qiov
, qiov_offset
, compressed_data
, n_bytes
);
1846 ret
= compress(data
->data
, &buf_len
, compressed_data
, n_bytes
);
1847 g_free(compressed_data
);
1849 if (ret
!= Z_OK
|| buf_len
== 0) {
1854 data
->lba
= cpu_to_le64(offset
>> BDRV_SECTOR_BITS
);
1855 data
->size
= cpu_to_le32(buf_len
);
1857 n_bytes
= buf_len
+ sizeof(VmdkGrainMarker
);
1858 qemu_iovec_init_buf(&local_qiov
, data
, n_bytes
);
1860 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_WRITE_COMPRESSED
);
1862 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1863 qemu_iovec_concat(&local_qiov
, qiov
, qiov_offset
, n_bytes
);
1865 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_WRITE_AIO
);
1868 write_offset
= cluster_offset
+ offset_in_cluster
;
1869 ret
= bdrv_co_pwritev(extent
->file
, write_offset
, n_bytes
,
1872 write_end_sector
= DIV_ROUND_UP(write_offset
+ n_bytes
, BDRV_SECTOR_SIZE
);
1874 if (extent
->compressed
) {
1875 extent
->next_cluster_sector
= write_end_sector
;
1877 extent
->next_cluster_sector
= MAX(extent
->next_cluster_sector
,
1887 if (!extent
->compressed
) {
1888 qemu_iovec_destroy(&local_qiov
);
1893 static int coroutine_fn GRAPH_RDLOCK
1894 vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
1895 int64_t offset_in_cluster
, QEMUIOVector
*qiov
, int bytes
)
1898 int cluster_bytes
, buf_bytes
;
1899 uint8_t *cluster_buf
, *compressed_data
;
1900 uint8_t *uncomp_buf
;
1902 VmdkGrainMarker
*marker
;
1906 if (!extent
->compressed
) {
1907 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_READ_AIO
);
1908 ret
= bdrv_co_preadv(extent
->file
,
1909 cluster_offset
+ offset_in_cluster
, bytes
,
1916 cluster_bytes
= extent
->cluster_sectors
* 512;
1917 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1918 buf_bytes
= cluster_bytes
* 2;
1919 cluster_buf
= g_malloc(buf_bytes
);
1920 uncomp_buf
= g_malloc(cluster_bytes
);
1921 BLKDBG_CO_EVENT(extent
->file
, BLKDBG_READ_COMPRESSED
);
1922 ret
= bdrv_co_pread(extent
->file
, cluster_offset
, buf_bytes
, cluster_buf
,
1927 compressed_data
= cluster_buf
;
1928 buf_len
= cluster_bytes
;
1929 data_len
= cluster_bytes
;
1930 if (extent
->has_marker
) {
1931 marker
= (VmdkGrainMarker
*)cluster_buf
;
1932 compressed_data
= marker
->data
;
1933 data_len
= le32_to_cpu(marker
->size
);
1935 if (!data_len
|| data_len
> buf_bytes
) {
1939 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
1945 if (offset_in_cluster
< 0 ||
1946 offset_in_cluster
+ bytes
> buf_len
) {
1950 qemu_iovec_from_buf(qiov
, 0, uncomp_buf
+ offset_in_cluster
, bytes
);
1955 g_free(cluster_buf
);
1959 static int coroutine_fn GRAPH_RDLOCK
1960 vmdk_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
1961 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
1963 BDRVVmdkState
*s
= bs
->opaque
;
1965 uint64_t n_bytes
, offset_in_cluster
;
1966 VmdkExtent
*extent
= NULL
;
1967 QEMUIOVector local_qiov
;
1968 uint64_t cluster_offset
;
1969 uint64_t bytes_done
= 0;
1971 qemu_iovec_init(&local_qiov
, qiov
->niov
);
1972 qemu_co_mutex_lock(&s
->lock
);
1975 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
1980 ret
= get_cluster_offset(bs
, extent
, NULL
,
1981 offset
, false, &cluster_offset
, 0, 0);
1982 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
1984 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
1985 - offset_in_cluster
);
1987 if (ret
!= VMDK_OK
) {
1988 /* if not allocated, try to read from parent image, if exist */
1989 if (bs
->backing
&& ret
!= VMDK_ZEROED
) {
1990 if (!vmdk_is_cid_valid(bs
)) {
1995 qemu_iovec_reset(&local_qiov
);
1996 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
1998 /* qcow2 emits this on bs->file instead of bs->backing */
1999 BLKDBG_CO_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
2000 ret
= bdrv_co_preadv(bs
->backing
, offset
, n_bytes
,
2006 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
2009 qemu_iovec_reset(&local_qiov
);
2010 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
2012 ret
= vmdk_read_extent(extent
, cluster_offset
, offset_in_cluster
,
2013 &local_qiov
, n_bytes
);
2020 bytes_done
+= n_bytes
;
2025 qemu_co_mutex_unlock(&s
->lock
);
2026 qemu_iovec_destroy(&local_qiov
);
2033 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
2034 * if possible, otherwise return -ENOTSUP.
2035 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
2036 * with each cluster. By dry run we can find if the zero write
2037 * is possible without modifying image data.
2039 * Returns: error code with 0 for success.
2041 static int coroutine_fn GRAPH_RDLOCK
2042 vmdk_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
2043 QEMUIOVector
*qiov
, bool zeroed
, bool zero_dry_run
)
2045 BDRVVmdkState
*s
= bs
->opaque
;
2046 VmdkExtent
*extent
= NULL
;
2048 int64_t offset_in_cluster
, n_bytes
;
2049 uint64_t cluster_offset
;
2050 uint64_t bytes_done
= 0;
2051 VmdkMetaData m_data
;
2053 if (DIV_ROUND_UP(offset
, BDRV_SECTOR_SIZE
) > bs
->total_sectors
) {
2054 error_report("Wrong offset: offset=0x%" PRIx64
2055 " total_sectors=0x%" PRIx64
,
2056 offset
, bs
->total_sectors
);
2061 extent
= find_extent(s
, offset
>> BDRV_SECTOR_BITS
, extent
);
2065 if (extent
->sesparse
) {
2068 offset_in_cluster
= vmdk_find_offset_in_cluster(extent
, offset
);
2069 n_bytes
= MIN(bytes
, extent
->cluster_sectors
* BDRV_SECTOR_SIZE
2070 - offset_in_cluster
);
2072 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2073 !(extent
->compressed
|| zeroed
),
2074 &cluster_offset
, offset_in_cluster
,
2075 offset_in_cluster
+ n_bytes
);
2076 if (extent
->compressed
) {
2077 if (ret
== VMDK_OK
) {
2078 /* Refuse write to allocated cluster for streamOptimized */
2079 error_report("Could not write to allocated cluster"
2080 " for streamOptimized");
2082 } else if (!zeroed
) {
2084 ret
= get_cluster_offset(bs
, extent
, &m_data
, offset
,
2085 true, &cluster_offset
, 0, 0);
2088 if (ret
== VMDK_ERROR
) {
2092 /* Do zeroed write, buf is ignored */
2093 if (extent
->has_zero_grain
&&
2094 offset_in_cluster
== 0 &&
2095 n_bytes
>= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
) {
2096 n_bytes
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
;
2097 if (!zero_dry_run
&& ret
!= VMDK_ZEROED
) {
2098 /* update L2 tables */
2099 if (vmdk_L2update(extent
, &m_data
, VMDK_GTE_ZEROED
)
2108 ret
= vmdk_write_extent(extent
, cluster_offset
, offset_in_cluster
,
2109 qiov
, bytes_done
, n_bytes
, offset
);
2113 if (m_data
.new_allocation
) {
2114 /* update L2 tables */
2115 if (vmdk_L2update(extent
, &m_data
,
2116 cluster_offset
>> BDRV_SECTOR_BITS
)
2124 bytes_done
+= n_bytes
;
2126 /* update CID on the first write every time the virtual disk is
2128 if (!s
->cid_updated
) {
2129 ret
= vmdk_write_cid(bs
, g_random_int());
2133 s
->cid_updated
= true;
2139 static int coroutine_fn GRAPH_RDLOCK
2140 vmdk_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2141 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
2144 BDRVVmdkState
*s
= bs
->opaque
;
2145 qemu_co_mutex_lock(&s
->lock
);
2146 ret
= vmdk_pwritev(bs
, offset
, bytes
, qiov
, false, false);
2147 qemu_co_mutex_unlock(&s
->lock
);
2151 static int coroutine_fn GRAPH_RDLOCK
2152 vmdk_co_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2156 /* The caller will write bytes 0 to signal EOF.
2157 * When receive it, we align EOF to a sector boundary. */
2158 BDRVVmdkState
*s
= bs
->opaque
;
2162 for (i
= 0; i
< s
->num_extents
; i
++) {
2163 length
= bdrv_co_getlength(s
->extents
[i
].file
->bs
);
2167 length
= QEMU_ALIGN_UP(length
, BDRV_SECTOR_SIZE
);
2168 ret
= bdrv_co_truncate(s
->extents
[i
].file
, length
, false,
2169 PREALLOC_MODE_OFF
, 0, NULL
);
2176 return vmdk_co_pwritev(bs
, offset
, bytes
, qiov
, 0);
2179 static int coroutine_fn GRAPH_RDLOCK
2180 vmdk_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
2181 BdrvRequestFlags flags
)
2184 BDRVVmdkState
*s
= bs
->opaque
;
2186 qemu_co_mutex_lock(&s
->lock
);
2187 /* write zeroes could fail if sectors not aligned to cluster, test it with
2188 * dry_run == true before really updating image */
2189 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, true);
2191 ret
= vmdk_pwritev(bs
, offset
, bytes
, NULL
, true, false);
2193 qemu_co_mutex_unlock(&s
->lock
);
2197 static int coroutine_fn GRAPH_UNLOCKED
2198 vmdk_init_extent(BlockBackend
*blk
, int64_t filesize
, bool flat
, bool compress
,
2199 bool zeroed_grain
, Error
**errp
)
2203 uint32_t tmp
, magic
, grains
, gd_sectors
, gt_size
, gt_count
;
2204 uint32_t *gd_buf
= NULL
;
2208 ret
= blk_co_truncate(blk
, filesize
, false, PREALLOC_MODE_OFF
, 0, errp
);
2211 magic
= cpu_to_be32(VMDK4_MAGIC
);
2212 memset(&header
, 0, sizeof(header
));
2215 } else if (zeroed_grain
) {
2220 header
.flags
= VMDK4_FLAG_RGD
| VMDK4_FLAG_NL_DETECT
2221 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0)
2222 | (zeroed_grain
? VMDK4_FLAG_ZERO_GRAIN
: 0);
2223 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
2224 header
.capacity
= filesize
/ BDRV_SECTOR_SIZE
;
2225 header
.granularity
= 128;
2226 header
.num_gtes_per_gt
= BDRV_SECTOR_SIZE
;
2228 grains
= DIV_ROUND_UP(filesize
/ BDRV_SECTOR_SIZE
, header
.granularity
);
2229 gt_size
= DIV_ROUND_UP(header
.num_gtes_per_gt
* sizeof(uint32_t),
2231 gt_count
= DIV_ROUND_UP(grains
, header
.num_gtes_per_gt
);
2232 gd_sectors
= DIV_ROUND_UP(gt_count
* sizeof(uint32_t), BDRV_SECTOR_SIZE
);
2234 header
.desc_offset
= 1;
2235 header
.desc_size
= 20;
2236 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
2237 header
.gd_offset
= header
.rgd_offset
+ gd_sectors
+ (gt_size
* gt_count
);
2238 header
.grain_offset
=
2239 ROUND_UP(header
.gd_offset
+ gd_sectors
+ (gt_size
* gt_count
),
2240 header
.granularity
);
2241 /* swap endianness for all header fields */
2242 header
.version
= cpu_to_le32(header
.version
);
2243 header
.flags
= cpu_to_le32(header
.flags
);
2244 header
.capacity
= cpu_to_le64(header
.capacity
);
2245 header
.granularity
= cpu_to_le64(header
.granularity
);
2246 header
.num_gtes_per_gt
= cpu_to_le32(header
.num_gtes_per_gt
);
2247 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
2248 header
.desc_size
= cpu_to_le64(header
.desc_size
);
2249 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
2250 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
2251 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
2252 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
2254 header
.check_bytes
[0] = 0xa;
2255 header
.check_bytes
[1] = 0x20;
2256 header
.check_bytes
[2] = 0xd;
2257 header
.check_bytes
[3] = 0xa;
2259 /* write all the data */
2260 ret
= blk_co_pwrite(blk
, 0, sizeof(magic
), &magic
, 0);
2262 error_setg(errp
, QERR_IO_ERROR
);
2265 ret
= blk_co_pwrite(blk
, sizeof(magic
), sizeof(header
), &header
, 0);
2267 error_setg(errp
, QERR_IO_ERROR
);
2271 ret
= blk_co_truncate(blk
, le64_to_cpu(header
.grain_offset
) << 9, false,
2272 PREALLOC_MODE_OFF
, 0, errp
);
2277 /* write grain directory */
2278 gd_buf_size
= gd_sectors
* BDRV_SECTOR_SIZE
;
2279 gd_buf
= g_malloc0(gd_buf_size
);
2280 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_sectors
;
2281 i
< gt_count
; i
++, tmp
+= gt_size
) {
2282 gd_buf
[i
] = cpu_to_le32(tmp
);
2284 ret
= blk_co_pwrite(blk
, le64_to_cpu(header
.rgd_offset
) * BDRV_SECTOR_SIZE
,
2285 gd_buf_size
, gd_buf
, 0);
2287 error_setg(errp
, QERR_IO_ERROR
);
2291 /* write backup grain directory */
2292 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_sectors
;
2293 i
< gt_count
; i
++, tmp
+= gt_size
) {
2294 gd_buf
[i
] = cpu_to_le32(tmp
);
2296 ret
= blk_co_pwrite(blk
, le64_to_cpu(header
.gd_offset
) * BDRV_SECTOR_SIZE
,
2297 gd_buf_size
, gd_buf
, 0);
2299 error_setg(errp
, QERR_IO_ERROR
);
2308 static int coroutine_fn GRAPH_UNLOCKED
2309 vmdk_create_extent(const char *filename
, int64_t filesize
, bool flat
,
2310 bool compress
, bool zeroed_grain
, BlockBackend
**pbb
,
2311 QemuOpts
*opts
, Error
**errp
)
2314 BlockBackend
*blk
= NULL
;
2316 ret
= bdrv_co_create_file(filename
, opts
, errp
);
2321 blk
= blk_co_new_open(filename
, NULL
, NULL
,
2322 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
2329 blk_set_allow_write_beyond_eof(blk
, true);
2331 ret
= vmdk_init_extent(blk
, filesize
, flat
, compress
, zeroed_grain
, errp
);
2344 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
2345 char *postfix
, size_t buf_len
, Error
**errp
)
2349 if (filename
== NULL
|| !strlen(filename
)) {
2350 error_setg(errp
, "No filename provided");
2353 p
= strrchr(filename
, '/');
2355 p
= strrchr(filename
, '\\');
2358 p
= strrchr(filename
, ':');
2362 if (p
- filename
>= buf_len
) {
2365 pstrcpy(path
, p
- filename
+ 1, filename
);
2370 q
= strrchr(p
, '.');
2372 pstrcpy(prefix
, buf_len
, p
);
2375 if (q
- p
>= buf_len
) {
2378 pstrcpy(prefix
, q
- p
+ 1, p
);
2379 pstrcpy(postfix
, buf_len
, q
);
2385 * idx == 0: get or create the descriptor file (also the image file if in a
2387 * idx >= 1: get the n-th extent if in a split subformat
2389 typedef BlockBackend
* coroutine_fn GRAPH_UNLOCKED_PTR
2390 (*vmdk_create_extent_fn
)(int64_t size
, int idx
, bool flat
, bool split
,
2391 bool compress
, bool zeroed_grain
, void *opaque
,
2394 static void vmdk_desc_add_extent(GString
*desc
,
2395 const char *extent_line_fmt
,
2396 int64_t size
, const char *filename
)
2398 char *basename
= g_path_get_basename(filename
);
2400 g_string_append_printf(desc
, extent_line_fmt
,
2401 DIV_ROUND_UP(size
, BDRV_SECTOR_SIZE
), basename
);
2405 static int coroutine_fn GRAPH_UNLOCKED
2406 vmdk_co_do_create(int64_t size
,
2407 BlockdevVmdkSubformat subformat
,
2408 BlockdevVmdkAdapterType adapter_type
,
2409 const char *backing_file
,
2410 const char *hw_version
,
2411 const char *toolsversion
,
2414 vmdk_create_extent_fn extent_fn
,
2419 BlockBackend
*blk
= NULL
;
2420 BlockBackend
*extent_blk
;
2421 Error
*local_err
= NULL
;
2424 bool flat
, split
, compress
;
2425 GString
*ext_desc_lines
;
2426 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
2427 int64_t extent_size
;
2428 int64_t created_size
= 0;
2429 const char *extent_line_fmt
;
2430 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2431 uint32_t parent_cid
= 0xffffffff;
2432 uint32_t number_heads
= 16;
2433 uint32_t desc_offset
= 0, desc_len
;
2434 const char desc_template
[] =
2435 "# Disk DescriptorFile\n"
2438 "parentCID=%" PRIx32
"\n"
2439 "createType=\"%s\"\n"
2442 "# Extent description\n"
2445 "# The Disk Data Base\n"
2448 "ddb.virtualHWVersion = \"%s\"\n"
2449 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
2450 "ddb.geometry.heads = \"%" PRIu32
"\"\n"
2451 "ddb.geometry.sectors = \"63\"\n"
2452 "ddb.adapterType = \"%s\"\n"
2453 "ddb.toolsVersion = \"%s\"\n";
2455 ext_desc_lines
= g_string_new(NULL
);
2457 /* Read out options */
2461 "compat6 cannot be enabled with hwversion set");
2470 if (!toolsversion
) {
2471 toolsversion
= "2147483647";
2474 if (adapter_type
!= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
) {
2475 /* that's the number of heads with which vmware operates when
2476 creating, exporting, etc. vmdk files with a non-ide adapter type */
2479 split
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
) ||
2480 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE
);
2481 flat
= (subformat
== BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT
) ||
2482 (subformat
== BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT
);
2483 compress
= subformat
== BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED
;
2486 extent_line_fmt
= "RW %" PRId64
" FLAT \"%s\" 0\n";
2488 extent_line_fmt
= "RW %" PRId64
" SPARSE \"%s\"\n";
2490 if (flat
&& backing_file
) {
2491 error_setg(errp
, "Flat image can't have backing file");
2495 if (flat
&& zeroed_grain
) {
2496 error_setg(errp
, "Flat image can't enable zeroed grain");
2501 /* Create extents */
2503 extent_size
= split_size
;
2507 if (!split
&& !flat
) {
2508 created_size
= extent_size
;
2512 /* Get the descriptor file BDS */
2513 blk
= extent_fn(created_size
, 0, flat
, split
, compress
, zeroed_grain
,
2519 if (!split
&& !flat
) {
2520 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, created_size
,
2521 blk_bs(blk
)->filename
);
2525 BlockBackend
*backing
;
2526 char *full_backing
=
2527 bdrv_get_full_backing_filename_from_filename(blk_bs(blk
)->filename
,
2531 error_propagate(errp
, local_err
);
2535 assert(full_backing
);
2537 backing
= blk_co_new_open(full_backing
, NULL
, NULL
,
2538 BDRV_O_NO_BACKING
, errp
);
2539 g_free(full_backing
);
2540 if (backing
== NULL
) {
2544 if (strcmp(blk_bs(backing
)->drv
->format_name
, "vmdk")) {
2545 error_setg(errp
, "Invalid backing file format: %s. Must be vmdk",
2546 blk_bs(backing
)->drv
->format_name
);
2547 blk_co_unref(backing
);
2551 ret
= vmdk_read_cid(blk_bs(backing
), 0, &parent_cid
);
2552 blk_co_unref(backing
);
2554 error_setg(errp
, "Failed to read parent CID");
2557 snprintf(parent_desc_line
, BUF_SIZE
,
2558 "parentFileNameHint=\"%s\"", backing_file
);
2561 while (created_size
< size
) {
2562 int64_t cur_size
= MIN(size
- created_size
, extent_size
);
2563 extent_blk
= extent_fn(cur_size
, extent_idx
, flat
, split
, compress
,
2564 zeroed_grain
, opaque
, errp
);
2569 vmdk_desc_add_extent(ext_desc_lines
, extent_line_fmt
, cur_size
,
2570 blk_bs(extent_blk
)->filename
);
2571 created_size
+= cur_size
;
2573 blk_co_unref(extent_blk
);
2576 /* Check whether we got excess extents */
2577 extent_blk
= extent_fn(-1, extent_idx
, flat
, split
, compress
, zeroed_grain
,
2580 blk_co_unref(extent_blk
);
2581 error_setg(errp
, "List of extents contains unused extents");
2586 /* generate descriptor file */
2587 desc
= g_strdup_printf(desc_template
,
2590 BlockdevVmdkSubformat_str(subformat
),
2592 ext_desc_lines
->str
,
2595 (int64_t)(63 * number_heads
* BDRV_SECTOR_SIZE
),
2597 BlockdevVmdkAdapterType_str(adapter_type
),
2599 desc_len
= strlen(desc
);
2600 /* the descriptor offset = 0x200 */
2601 if (!split
&& !flat
) {
2602 desc_offset
= 0x200;
2605 ret
= blk_co_pwrite(blk
, desc_offset
, desc_len
, desc
, 0);
2607 error_setg_errno(errp
, -ret
, "Could not write description");
2610 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2611 * for description file */
2612 if (desc_offset
== 0) {
2613 ret
= blk_co_truncate(blk
, desc_len
, false, PREALLOC_MODE_OFF
, 0, errp
);
2624 g_free(parent_desc_line
);
2625 g_string_free(ext_desc_lines
, true);
2634 } VMDKCreateOptsData
;
2636 static BlockBackend
* coroutine_fn GRAPH_UNLOCKED
2637 vmdk_co_create_opts_cb(int64_t size
, int idx
, bool flat
, bool split
,
2638 bool compress
, bool zeroed_grain
, void *opaque
,
2641 BlockBackend
*blk
= NULL
;
2642 BlockDriverState
*bs
= NULL
;
2643 VMDKCreateOptsData
*data
= opaque
;
2644 char *ext_filename
= NULL
;
2645 char *rel_filename
= NULL
;
2647 /* We're done, don't create excess extents. */
2649 assert(errp
== NULL
);
2654 rel_filename
= g_strdup_printf("%s%s", data
->prefix
, data
->postfix
);
2656 rel_filename
= g_strdup_printf("%s-%c%03d%s",
2658 flat
? 'f' : 's', idx
, data
->postfix
);
2661 rel_filename
= g_strdup_printf("%s-flat%s", data
->prefix
, data
->postfix
);
2664 ext_filename
= g_strdup_printf("%s%s", data
->path
, rel_filename
);
2665 g_free(rel_filename
);
2667 if (vmdk_create_extent(ext_filename
, size
,
2668 flat
, compress
, zeroed_grain
, &blk
, data
->opts
,
2674 g_free(ext_filename
);
2678 static int coroutine_fn GRAPH_UNLOCKED
2679 vmdk_co_create_opts(BlockDriver
*drv
, const char *filename
,
2680 QemuOpts
*opts
, Error
**errp
)
2682 Error
*local_err
= NULL
;
2684 int64_t total_size
= 0;
2685 char *adapter_type
= NULL
;
2686 BlockdevVmdkAdapterType adapter_type_enum
;
2687 char *backing_file
= NULL
;
2688 char *hw_version
= NULL
;
2689 char *toolsversion
= NULL
;
2691 BlockdevVmdkSubformat subformat
;
2693 char *path
= g_malloc0(PATH_MAX
);
2694 char *prefix
= g_malloc0(PATH_MAX
);
2695 char *postfix
= g_malloc0(PATH_MAX
);
2696 char *desc_line
= g_malloc0(BUF_SIZE
);
2697 char *ext_filename
= g_malloc0(PATH_MAX
);
2698 char *desc_filename
= g_malloc0(PATH_MAX
);
2699 char *parent_desc_line
= g_malloc0(BUF_SIZE
);
2702 VMDKCreateOptsData data
;
2703 char *backing_fmt
= NULL
;
2705 backing_fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FMT
);
2706 if (backing_fmt
&& strcmp(backing_fmt
, "vmdk") != 0) {
2707 error_setg(errp
, "backing_file must be a vmdk image");
2712 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
, errp
)) {
2716 /* Read out options */
2717 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2719 adapter_type
= qemu_opt_get_del(opts
, BLOCK_OPT_ADAPTER_TYPE
);
2720 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
2721 hw_version
= qemu_opt_get_del(opts
, BLOCK_OPT_HWVERSION
);
2722 toolsversion
= qemu_opt_get_del(opts
, BLOCK_OPT_TOOLSVERSION
);
2723 compat6
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_COMPAT6
, false);
2724 if (strcmp(hw_version
, "undefined") == 0) {
2728 fmt
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
2729 zeroed_grain
= qemu_opt_get_bool_del(opts
, BLOCK_OPT_ZEROED_GRAIN
, false);
2732 adapter_type_enum
= qapi_enum_parse(&BlockdevVmdkAdapterType_lookup
,
2734 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
,
2737 error_propagate(errp
, local_err
);
2742 adapter_type_enum
= BLOCKDEV_VMDK_ADAPTER_TYPE_IDE
;
2746 /* Default format to monolithicSparse */
2747 subformat
= BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
;
2749 subformat
= qapi_enum_parse(&BlockdevVmdkSubformat_lookup
,
2751 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE
,
2754 error_propagate(errp
, local_err
);
2759 data
= (VMDKCreateOptsData
){
2765 ret
= vmdk_co_do_create(total_size
, subformat
, adapter_type_enum
,
2766 backing_file
, hw_version
, toolsversion
, compat6
,
2767 zeroed_grain
, vmdk_co_create_opts_cb
, &data
, errp
);
2770 g_free(backing_fmt
);
2771 g_free(adapter_type
);
2772 g_free(backing_file
);
2774 g_free(toolsversion
);
2781 g_free(ext_filename
);
2782 g_free(desc_filename
);
2783 g_free(parent_desc_line
);
2787 static BlockBackend
* coroutine_fn GRAPH_UNLOCKED
2788 vmdk_co_create_cb(int64_t size
, int idx
, bool flat
, bool split
, bool compress
,
2789 bool zeroed_grain
, void *opaque
, Error
**errp
)
2792 BlockDriverState
*bs
;
2794 BlockdevCreateOptionsVmdk
*opts
= opaque
;
2797 bs
= bdrv_co_open_blockdev_ref(opts
->file
, errp
);
2800 BlockdevRefList
*list
= opts
->extents
;
2801 for (i
= 1; i
< idx
; i
++) {
2802 if (!list
|| !list
->next
) {
2803 error_setg(errp
, "Extent [%d] not specified", i
);
2809 error_setg(errp
, "Extent [%d] not specified", idx
- 1);
2812 bs
= bdrv_co_open_blockdev_ref(list
->value
, errp
);
2817 blk
= blk_co_new_with_bs(bs
,
2818 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
|
2825 blk_set_allow_write_beyond_eof(blk
, true);
2829 ret
= vmdk_init_extent(blk
, size
, flat
, compress
, zeroed_grain
, errp
);
2838 static int coroutine_fn GRAPH_UNLOCKED
2839 vmdk_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
2841 BlockdevCreateOptionsVmdk
*opts
;
2843 opts
= &create_options
->u
.vmdk
;
2845 /* Validate options */
2846 if (!QEMU_IS_ALIGNED(opts
->size
, BDRV_SECTOR_SIZE
)) {
2847 error_setg(errp
, "Image size must be a multiple of 512 bytes");
2851 return vmdk_co_do_create(opts
->size
,
2863 static void vmdk_close(BlockDriverState
*bs
)
2865 BDRVVmdkState
*s
= bs
->opaque
;
2867 vmdk_free_extents(bs
);
2868 g_free(s
->create_type
);
2870 migrate_del_blocker(s
->migration_blocker
);
2871 error_free(s
->migration_blocker
);
2874 static int64_t coroutine_fn GRAPH_RDLOCK
2875 vmdk_co_get_allocated_file_size(BlockDriverState
*bs
)
2880 BDRVVmdkState
*s
= bs
->opaque
;
2882 ret
= bdrv_co_get_allocated_file_size(bs
->file
->bs
);
2886 for (i
= 0; i
< s
->num_extents
; i
++) {
2887 if (s
->extents
[i
].file
== bs
->file
) {
2890 r
= bdrv_co_get_allocated_file_size(s
->extents
[i
].file
->bs
);
2899 static int vmdk_has_zero_init(BlockDriverState
*bs
)
2902 BDRVVmdkState
*s
= bs
->opaque
;
2904 /* If has a flat extent and its underlying storage doesn't have zero init,
2906 for (i
= 0; i
< s
->num_extents
; i
++) {
2907 if (s
->extents
[i
].flat
) {
2908 if (!bdrv_has_zero_init(s
->extents
[i
].file
->bs
)) {
2916 static VmdkExtentInfo
* GRAPH_RDLOCK
vmdk_get_extent_info(VmdkExtent
*extent
)
2918 VmdkExtentInfo
*info
= g_new0(VmdkExtentInfo
, 1);
2920 bdrv_refresh_filename(extent
->file
->bs
);
2921 *info
= (VmdkExtentInfo
){
2922 .filename
= g_strdup(extent
->file
->bs
->filename
),
2923 .format
= g_strdup(extent
->type
),
2924 .virtual_size
= extent
->sectors
* BDRV_SECTOR_SIZE
,
2925 .compressed
= extent
->compressed
,
2926 .has_compressed
= extent
->compressed
,
2927 .cluster_size
= extent
->cluster_sectors
* BDRV_SECTOR_SIZE
,
2928 .has_cluster_size
= !extent
->flat
,
2934 static int coroutine_fn GRAPH_RDLOCK
2935 vmdk_co_check(BlockDriverState
*bs
, BdrvCheckResult
*result
, BdrvCheckMode fix
)
2937 BDRVVmdkState
*s
= bs
->opaque
;
2938 VmdkExtent
*extent
= NULL
;
2939 int64_t sector_num
= 0;
2940 int64_t total_sectors
= bdrv_co_nb_sectors(bs
);
2942 uint64_t cluster_offset
;
2949 if (sector_num
>= total_sectors
) {
2952 extent
= find_extent(s
, sector_num
, extent
);
2955 "ERROR: could not find extent for sector %" PRId64
"\n",
2960 ret
= get_cluster_offset(bs
, extent
, NULL
,
2961 sector_num
<< BDRV_SECTOR_BITS
,
2962 false, &cluster_offset
, 0, 0);
2963 if (ret
== VMDK_ERROR
) {
2965 "ERROR: could not get cluster_offset for sector %"
2966 PRId64
"\n", sector_num
);
2969 if (ret
== VMDK_OK
) {
2970 int64_t extent_len
= bdrv_co_getlength(extent
->file
->bs
);
2971 if (extent_len
< 0) {
2973 "ERROR: could not get extent file length for sector %"
2974 PRId64
"\n", sector_num
);
2978 if (cluster_offset
>= extent_len
) {
2980 "ERROR: cluster offset for sector %"
2981 PRId64
" points after EOF\n", sector_num
);
2986 sector_num
+= extent
->cluster_sectors
;
2989 result
->corruptions
++;
2993 static ImageInfoSpecific
* GRAPH_RDLOCK
2994 vmdk_get_specific_info(BlockDriverState
*bs
, Error
**errp
)
2997 BDRVVmdkState
*s
= bs
->opaque
;
2998 ImageInfoSpecific
*spec_info
= g_new0(ImageInfoSpecific
, 1);
2999 VmdkExtentInfoList
**tail
;
3001 *spec_info
= (ImageInfoSpecific
){
3002 .type
= IMAGE_INFO_SPECIFIC_KIND_VMDK
,
3004 .vmdk
.data
= g_new0(ImageInfoSpecificVmdk
, 1),
3008 *spec_info
->u
.vmdk
.data
= (ImageInfoSpecificVmdk
) {
3009 .create_type
= g_strdup(s
->create_type
),
3011 .parent_cid
= s
->parent_cid
,
3014 tail
= &spec_info
->u
.vmdk
.data
->extents
;
3015 for (i
= 0; i
< s
->num_extents
; i
++) {
3016 QAPI_LIST_APPEND(tail
, vmdk_get_extent_info(&s
->extents
[i
]));
3022 static bool vmdk_extents_type_eq(const VmdkExtent
*a
, const VmdkExtent
*b
)
3024 return a
->flat
== b
->flat
&&
3025 a
->compressed
== b
->compressed
&&
3026 (a
->flat
|| a
->cluster_sectors
== b
->cluster_sectors
);
3029 static int coroutine_fn
3030 vmdk_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
3033 BDRVVmdkState
*s
= bs
->opaque
;
3034 assert(s
->num_extents
);
3036 /* See if we have multiple extents but they have different cases */
3037 for (i
= 1; i
< s
->num_extents
; i
++) {
3038 if (!vmdk_extents_type_eq(&s
->extents
[0], &s
->extents
[i
])) {
3042 bdi
->needs_compressed_writes
= s
->extents
[0].compressed
;
3043 if (!s
->extents
[0].flat
) {
3044 bdi
->cluster_size
= s
->extents
[0].cluster_sectors
<< BDRV_SECTOR_BITS
;
3049 static void vmdk_gather_child_options(BlockDriverState
*bs
, QDict
*target
,
3050 bool backing_overridden
)
3052 /* No children but file and backing can be explicitly specified (TODO) */
3053 qdict_put(target
, "file",
3054 qobject_ref(bs
->file
->bs
->full_open_options
));
3056 if (backing_overridden
) {
3058 qdict_put(target
, "backing",
3059 qobject_ref(bs
->backing
->bs
->full_open_options
));
3061 qdict_put_null(target
, "backing");
3066 static QemuOptsList vmdk_create_opts
= {
3067 .name
= "vmdk-create-opts",
3068 .head
= QTAILQ_HEAD_INITIALIZER(vmdk_create_opts
.head
),
3071 .name
= BLOCK_OPT_SIZE
,
3072 .type
= QEMU_OPT_SIZE
,
3073 .help
= "Virtual disk size"
3076 .name
= BLOCK_OPT_ADAPTER_TYPE
,
3077 .type
= QEMU_OPT_STRING
,
3078 .help
= "Virtual adapter type, can be one of "
3079 "ide (default), lsilogic, buslogic or legacyESX"
3082 .name
= BLOCK_OPT_BACKING_FILE
,
3083 .type
= QEMU_OPT_STRING
,
3084 .help
= "File name of a base image"
3087 .name
= BLOCK_OPT_BACKING_FMT
,
3088 .type
= QEMU_OPT_STRING
,
3089 .help
= "Must be 'vmdk' if present",
3092 .name
= BLOCK_OPT_COMPAT6
,
3093 .type
= QEMU_OPT_BOOL
,
3094 .help
= "VMDK version 6 image",
3095 .def_value_str
= "off"
3098 .name
= BLOCK_OPT_HWVERSION
,
3099 .type
= QEMU_OPT_STRING
,
3100 .help
= "VMDK hardware version",
3101 .def_value_str
= "undefined"
3104 .name
= BLOCK_OPT_TOOLSVERSION
,
3105 .type
= QEMU_OPT_STRING
,
3106 .help
= "VMware guest tools version",
3109 .name
= BLOCK_OPT_SUBFMT
,
3110 .type
= QEMU_OPT_STRING
,
3112 "VMDK flat extent format, can be one of "
3113 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
3116 .name
= BLOCK_OPT_ZEROED_GRAIN
,
3117 .type
= QEMU_OPT_BOOL
,
3118 .help
= "Enable efficient zero writes "
3119 "using the zeroed-grain GTE feature"
3121 { /* end of list */ }
3125 static BlockDriver bdrv_vmdk
= {
3126 .format_name
= "vmdk",
3127 .instance_size
= sizeof(BDRVVmdkState
),
3128 .bdrv_probe
= vmdk_probe
,
3129 .bdrv_open
= vmdk_open
,
3130 .bdrv_co_check
= vmdk_co_check
,
3131 .bdrv_reopen_prepare
= vmdk_reopen_prepare
,
3132 .bdrv_reopen_commit
= vmdk_reopen_commit
,
3133 .bdrv_reopen_abort
= vmdk_reopen_abort
,
3134 .bdrv_child_perm
= bdrv_default_perms
,
3135 .bdrv_co_preadv
= vmdk_co_preadv
,
3136 .bdrv_co_pwritev
= vmdk_co_pwritev
,
3137 .bdrv_co_pwritev_compressed
= vmdk_co_pwritev_compressed
,
3138 .bdrv_co_pwrite_zeroes
= vmdk_co_pwrite_zeroes
,
3139 .bdrv_close
= vmdk_close
,
3140 .bdrv_co_create_opts
= vmdk_co_create_opts
,
3141 .bdrv_co_create
= vmdk_co_create
,
3142 .bdrv_co_block_status
= vmdk_co_block_status
,
3143 .bdrv_co_get_allocated_file_size
= vmdk_co_get_allocated_file_size
,
3144 .bdrv_has_zero_init
= vmdk_has_zero_init
,
3145 .bdrv_get_specific_info
= vmdk_get_specific_info
,
3146 .bdrv_refresh_limits
= vmdk_refresh_limits
,
3147 .bdrv_co_get_info
= vmdk_co_get_info
,
3148 .bdrv_gather_child_options
= vmdk_gather_child_options
,
3151 .supports_backing
= true,
3152 .create_opts
= &vmdk_create_opts
,
3155 static void bdrv_vmdk_init(void)
3157 bdrv_register(&bdrv_vmdk
);
3160 block_init(bdrv_vmdk_init
);