xref: /openbmc/qemu/block/vmdk.c (revision 79e42085)
1 /*
2  * Block driver for the VMDK format
3  *
4  * Copyright (c) 2004 Fabrice Bellard
5  * Copyright (c) 2005 Filip Navara
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "block/block_int.h"
29 #include "sysemu/block-backend.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qemu/error-report.h"
33 #include "qemu/module.h"
34 #include "qemu/option.h"
35 #include "qemu/bswap.h"
36 #include "migration/blocker.h"
37 #include "qemu/cutils.h"
38 #include <zlib.h>
39 
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
50 
51 #define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
52 
53 #define VMDK_GTE_ZEROED 0x1
54 
55 /* VMDK internal error codes */
56 #define VMDK_OK      0
57 #define VMDK_ERROR   (-1)
58 /* Cluster not allocated */
59 #define VMDK_UNALLOC (-2)
60 #define VMDK_ZEROED  (-3)
61 
62 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
63 
64 typedef struct {
65     uint32_t version;
66     uint32_t flags;
67     uint32_t disk_sectors;
68     uint32_t granularity;
69     uint32_t l1dir_offset;
70     uint32_t l1dir_size;
71     uint32_t file_sectors;
72     uint32_t cylinders;
73     uint32_t heads;
74     uint32_t sectors_per_track;
75 } QEMU_PACKED VMDK3Header;
76 
77 typedef struct {
78     uint32_t version;
79     uint32_t flags;
80     uint64_t capacity;
81     uint64_t granularity;
82     uint64_t desc_offset;
83     uint64_t desc_size;
84     /* Number of GrainTableEntries per GrainTable */
85     uint32_t num_gtes_per_gt;
86     uint64_t rgd_offset;
87     uint64_t gd_offset;
88     uint64_t grain_offset;
89     char filler[1];
90     char check_bytes[4];
91     uint16_t compressAlgorithm;
92 } QEMU_PACKED VMDK4Header;
93 
94 #define L2_CACHE_SIZE 16
95 
96 typedef struct VmdkExtent {
97     BdrvChild *file;
98     bool flat;
99     bool compressed;
100     bool has_marker;
101     bool has_zero_grain;
102     int version;
103     int64_t sectors;
104     int64_t end_sector;
105     int64_t flat_start_offset;
106     int64_t l1_table_offset;
107     int64_t l1_backup_table_offset;
108     uint32_t *l1_table;
109     uint32_t *l1_backup_table;
110     unsigned int l1_size;
111     uint32_t l1_entry_sectors;
112 
113     unsigned int l2_size;
114     uint32_t *l2_cache;
115     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
116     uint32_t l2_cache_counts[L2_CACHE_SIZE];
117 
118     int64_t cluster_sectors;
119     int64_t next_cluster_sector;
120     char *type;
121 } VmdkExtent;
122 
123 typedef struct BDRVVmdkState {
124     CoMutex lock;
125     uint64_t desc_offset;
126     bool cid_updated;
127     bool cid_checked;
128     uint32_t cid;
129     uint32_t parent_cid;
130     int num_extents;
131     /* Extent array with num_extents entries, ascend ordered by address */
132     VmdkExtent *extents;
133     Error *migration_blocker;
134     char *create_type;
135 } BDRVVmdkState;
136 
137 typedef struct VmdkMetaData {
138     unsigned int l1_index;
139     unsigned int l2_index;
140     unsigned int l2_offset;
141     int valid;
142     uint32_t *l2_cache_entry;
143 } VmdkMetaData;
144 
145 typedef struct VmdkGrainMarker {
146     uint64_t lba;
147     uint32_t size;
148     uint8_t  data[0];
149 } QEMU_PACKED VmdkGrainMarker;
150 
151 enum {
152     MARKER_END_OF_STREAM    = 0,
153     MARKER_GRAIN_TABLE      = 1,
154     MARKER_GRAIN_DIRECTORY  = 2,
155     MARKER_FOOTER           = 3,
156 };
157 
158 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
159 {
160     uint32_t magic;
161 
162     if (buf_size < 4) {
163         return 0;
164     }
165     magic = be32_to_cpu(*(uint32_t *)buf);
166     if (magic == VMDK3_MAGIC ||
167         magic == VMDK4_MAGIC) {
168         return 100;
169     } else {
170         const char *p = (const char *)buf;
171         const char *end = p + buf_size;
172         while (p < end) {
173             if (*p == '#') {
174                 /* skip comment line */
175                 while (p < end && *p != '\n') {
176                     p++;
177                 }
178                 p++;
179                 continue;
180             }
181             if (*p == ' ') {
182                 while (p < end && *p == ' ') {
183                     p++;
184                 }
185                 /* skip '\r' if windows line endings used. */
186                 if (p < end && *p == '\r') {
187                     p++;
188                 }
189                 /* only accept blank lines before 'version=' line */
190                 if (p == end || *p != '\n') {
191                     return 0;
192                 }
193                 p++;
194                 continue;
195             }
196             if (end - p >= strlen("version=X\n")) {
197                 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
198                     strncmp("version=2\n", p, strlen("version=2\n")) == 0 ||
199                     strncmp("version=3\n", p, strlen("version=3\n")) == 0) {
200                     return 100;
201                 }
202             }
203             if (end - p >= strlen("version=X\r\n")) {
204                 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
205                     strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0 ||
206                     strncmp("version=3\r\n", p, strlen("version=3\r\n")) == 0) {
207                     return 100;
208                 }
209             }
210             return 0;
211         }
212         return 0;
213     }
214 }
215 
216 #define SECTOR_SIZE 512
217 #define DESC_SIZE (20 * SECTOR_SIZE)    /* 20 sectors of 512 bytes each */
218 #define BUF_SIZE 4096
219 #define HEADER_SIZE 512                 /* first sector of 512 bytes */
220 
221 static void vmdk_free_extents(BlockDriverState *bs)
222 {
223     int i;
224     BDRVVmdkState *s = bs->opaque;
225     VmdkExtent *e;
226 
227     for (i = 0; i < s->num_extents; i++) {
228         e = &s->extents[i];
229         g_free(e->l1_table);
230         g_free(e->l2_cache);
231         g_free(e->l1_backup_table);
232         g_free(e->type);
233         if (e->file != bs->file) {
234             bdrv_unref_child(bs, e->file);
235         }
236     }
237     g_free(s->extents);
238 }
239 
240 static void vmdk_free_last_extent(BlockDriverState *bs)
241 {
242     BDRVVmdkState *s = bs->opaque;
243 
244     if (s->num_extents == 0) {
245         return;
246     }
247     s->num_extents--;
248     s->extents = g_renew(VmdkExtent, s->extents, s->num_extents);
249 }
250 
251 /* Return -ve errno, or 0 on success and write CID into *pcid. */
252 static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid)
253 {
254     char *desc;
255     uint32_t cid;
256     const char *p_name, *cid_str;
257     size_t cid_str_size;
258     BDRVVmdkState *s = bs->opaque;
259     int ret;
260 
261     desc = g_malloc0(DESC_SIZE);
262     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
263     if (ret < 0) {
264         goto out;
265     }
266 
267     if (parent) {
268         cid_str = "parentCID";
269         cid_str_size = sizeof("parentCID");
270     } else {
271         cid_str = "CID";
272         cid_str_size = sizeof("CID");
273     }
274 
275     desc[DESC_SIZE - 1] = '\0';
276     p_name = strstr(desc, cid_str);
277     if (p_name == NULL) {
278         ret = -EINVAL;
279         goto out;
280     }
281     p_name += cid_str_size;
282     if (sscanf(p_name, "%" SCNx32, &cid) != 1) {
283         ret = -EINVAL;
284         goto out;
285     }
286     *pcid = cid;
287     ret = 0;
288 
289 out:
290     g_free(desc);
291     return ret;
292 }
293 
294 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
295 {
296     char *desc, *tmp_desc;
297     char *p_name, *tmp_str;
298     BDRVVmdkState *s = bs->opaque;
299     int ret = 0;
300 
301     desc = g_malloc0(DESC_SIZE);
302     tmp_desc = g_malloc0(DESC_SIZE);
303     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
304     if (ret < 0) {
305         goto out;
306     }
307 
308     desc[DESC_SIZE - 1] = '\0';
309     tmp_str = strstr(desc, "parentCID");
310     if (tmp_str == NULL) {
311         ret = -EINVAL;
312         goto out;
313     }
314 
315     pstrcpy(tmp_desc, DESC_SIZE, tmp_str);
316     p_name = strstr(desc, "CID");
317     if (p_name != NULL) {
318         p_name += sizeof("CID");
319         snprintf(p_name, DESC_SIZE - (p_name - desc), "%" PRIx32 "\n", cid);
320         pstrcat(desc, DESC_SIZE, tmp_desc);
321     }
322 
323     ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
324 
325 out:
326     g_free(desc);
327     g_free(tmp_desc);
328     return ret;
329 }
330 
331 static int vmdk_is_cid_valid(BlockDriverState *bs)
332 {
333     BDRVVmdkState *s = bs->opaque;
334     uint32_t cur_pcid;
335 
336     if (!s->cid_checked && bs->backing) {
337         BlockDriverState *p_bs = bs->backing->bs;
338 
339         if (strcmp(p_bs->drv->format_name, "vmdk")) {
340             /* Backing file is not in vmdk format, so it does not have
341              * a CID, which makes the overlay's parent CID invalid */
342             return 0;
343         }
344 
345         if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) {
346             /* read failure: report as not valid */
347             return 0;
348         }
349         if (s->parent_cid != cur_pcid) {
350             /* CID not valid */
351             return 0;
352         }
353     }
354     s->cid_checked = true;
355     /* CID valid */
356     return 1;
357 }
358 
359 /* We have nothing to do for VMDK reopen, stubs just return success */
360 static int vmdk_reopen_prepare(BDRVReopenState *state,
361                                BlockReopenQueue *queue, Error **errp)
362 {
363     assert(state != NULL);
364     assert(state->bs != NULL);
365     return 0;
366 }
367 
368 static int vmdk_parent_open(BlockDriverState *bs)
369 {
370     char *p_name;
371     char *desc;
372     BDRVVmdkState *s = bs->opaque;
373     int ret;
374 
375     desc = g_malloc0(DESC_SIZE + 1);
376     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
377     if (ret < 0) {
378         goto out;
379     }
380     ret = 0;
381 
382     p_name = strstr(desc, "parentFileNameHint");
383     if (p_name != NULL) {
384         char *end_name;
385 
386         p_name += sizeof("parentFileNameHint") + 1;
387         end_name = strchr(p_name, '\"');
388         if (end_name == NULL) {
389             ret = -EINVAL;
390             goto out;
391         }
392         if ((end_name - p_name) > sizeof(bs->auto_backing_file) - 1) {
393             ret = -EINVAL;
394             goto out;
395         }
396 
397         pstrcpy(bs->auto_backing_file, end_name - p_name + 1, p_name);
398         pstrcpy(bs->backing_file, sizeof(bs->backing_file),
399                 bs->auto_backing_file);
400         pstrcpy(bs->backing_format, sizeof(bs->backing_format),
401                 "vmdk");
402     }
403 
404 out:
405     g_free(desc);
406     return ret;
407 }
408 
409 /* Create and append extent to the extent array. Return the added VmdkExtent
410  * address. return NULL if allocation failed. */
411 static int vmdk_add_extent(BlockDriverState *bs,
412                            BdrvChild *file, bool flat, int64_t sectors,
413                            int64_t l1_offset, int64_t l1_backup_offset,
414                            uint32_t l1_size,
415                            int l2_size, uint64_t cluster_sectors,
416                            VmdkExtent **new_extent,
417                            Error **errp)
418 {
419     VmdkExtent *extent;
420     BDRVVmdkState *s = bs->opaque;
421     int64_t nb_sectors;
422 
423     if (cluster_sectors > 0x200000) {
424         /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
425         error_setg(errp, "Invalid granularity, image may be corrupt");
426         return -EFBIG;
427     }
428     if (l1_size > 512 * 1024 * 1024) {
429         /* Although with big capacity and small l1_entry_sectors, we can get a
430          * big l1_size, we don't want unbounded value to allocate the table.
431          * Limit it to 512M, which is 16PB for default cluster and L2 table
432          * size */
433         error_setg(errp, "L1 size too big");
434         return -EFBIG;
435     }
436 
437     nb_sectors = bdrv_nb_sectors(file->bs);
438     if (nb_sectors < 0) {
439         return nb_sectors;
440     }
441 
442     s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1);
443     extent = &s->extents[s->num_extents];
444     s->num_extents++;
445 
446     memset(extent, 0, sizeof(VmdkExtent));
447     extent->file = file;
448     extent->flat = flat;
449     extent->sectors = sectors;
450     extent->l1_table_offset = l1_offset;
451     extent->l1_backup_table_offset = l1_backup_offset;
452     extent->l1_size = l1_size;
453     extent->l1_entry_sectors = l2_size * cluster_sectors;
454     extent->l2_size = l2_size;
455     extent->cluster_sectors = flat ? sectors : cluster_sectors;
456     extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors);
457 
458     if (s->num_extents > 1) {
459         extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
460     } else {
461         extent->end_sector = extent->sectors;
462     }
463     bs->total_sectors = extent->end_sector;
464     if (new_extent) {
465         *new_extent = extent;
466     }
467     return 0;
468 }
469 
470 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
471                             Error **errp)
472 {
473     int ret;
474     size_t l1_size;
475     int i;
476 
477     /* read the L1 table */
478     l1_size = extent->l1_size * sizeof(uint32_t);
479     extent->l1_table = g_try_malloc(l1_size);
480     if (l1_size && extent->l1_table == NULL) {
481         return -ENOMEM;
482     }
483 
484     ret = bdrv_pread(extent->file,
485                      extent->l1_table_offset,
486                      extent->l1_table,
487                      l1_size);
488     if (ret < 0) {
489         bdrv_refresh_filename(extent->file->bs);
490         error_setg_errno(errp, -ret,
491                          "Could not read l1 table from extent '%s'",
492                          extent->file->bs->filename);
493         goto fail_l1;
494     }
495     for (i = 0; i < extent->l1_size; i++) {
496         le32_to_cpus(&extent->l1_table[i]);
497     }
498 
499     if (extent->l1_backup_table_offset) {
500         extent->l1_backup_table = g_try_malloc(l1_size);
501         if (l1_size && extent->l1_backup_table == NULL) {
502             ret = -ENOMEM;
503             goto fail_l1;
504         }
505         ret = bdrv_pread(extent->file,
506                          extent->l1_backup_table_offset,
507                          extent->l1_backup_table,
508                          l1_size);
509         if (ret < 0) {
510             bdrv_refresh_filename(extent->file->bs);
511             error_setg_errno(errp, -ret,
512                              "Could not read l1 backup table from extent '%s'",
513                              extent->file->bs->filename);
514             goto fail_l1b;
515         }
516         for (i = 0; i < extent->l1_size; i++) {
517             le32_to_cpus(&extent->l1_backup_table[i]);
518         }
519     }
520 
521     extent->l2_cache =
522         g_new(uint32_t, extent->l2_size * L2_CACHE_SIZE);
523     return 0;
524  fail_l1b:
525     g_free(extent->l1_backup_table);
526  fail_l1:
527     g_free(extent->l1_table);
528     return ret;
529 }
530 
531 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
532                                  BdrvChild *file,
533                                  int flags, Error **errp)
534 {
535     int ret;
536     uint32_t magic;
537     VMDK3Header header;
538     VmdkExtent *extent;
539 
540     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
541     if (ret < 0) {
542         bdrv_refresh_filename(file->bs);
543         error_setg_errno(errp, -ret,
544                          "Could not read header from file '%s'",
545                          file->bs->filename);
546         return ret;
547     }
548     ret = vmdk_add_extent(bs, file, false,
549                           le32_to_cpu(header.disk_sectors),
550                           (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
551                           0,
552                           le32_to_cpu(header.l1dir_size),
553                           4096,
554                           le32_to_cpu(header.granularity),
555                           &extent,
556                           errp);
557     if (ret < 0) {
558         return ret;
559     }
560     ret = vmdk_init_tables(bs, extent, errp);
561     if (ret) {
562         /* free extent allocated by vmdk_add_extent */
563         vmdk_free_last_extent(bs);
564     }
565     return ret;
566 }
567 
568 static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
569                                QDict *options, Error **errp);
570 
571 static char *vmdk_read_desc(BdrvChild *file, uint64_t desc_offset, Error **errp)
572 {
573     int64_t size;
574     char *buf;
575     int ret;
576 
577     size = bdrv_getlength(file->bs);
578     if (size < 0) {
579         error_setg_errno(errp, -size, "Could not access file");
580         return NULL;
581     }
582 
583     if (size < 4) {
584         /* Both descriptor file and sparse image must be much larger than 4
585          * bytes, also callers of vmdk_read_desc want to compare the first 4
586          * bytes with VMDK4_MAGIC, let's error out if less is read. */
587         error_setg(errp, "File is too small, not a valid image");
588         return NULL;
589     }
590 
591     size = MIN(size, (1 << 20) - 1);  /* avoid unbounded allocation */
592     buf = g_malloc(size + 1);
593 
594     ret = bdrv_pread(file, desc_offset, buf, size);
595     if (ret < 0) {
596         error_setg_errno(errp, -ret, "Could not read from file");
597         g_free(buf);
598         return NULL;
599     }
600     buf[ret] = 0;
601 
602     return buf;
603 }
604 
605 static int vmdk_open_vmdk4(BlockDriverState *bs,
606                            BdrvChild *file,
607                            int flags, QDict *options, Error **errp)
608 {
609     int ret;
610     uint32_t magic;
611     uint32_t l1_size, l1_entry_sectors;
612     VMDK4Header header;
613     VmdkExtent *extent;
614     BDRVVmdkState *s = bs->opaque;
615     int64_t l1_backup_offset = 0;
616     bool compressed;
617 
618     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
619     if (ret < 0) {
620         bdrv_refresh_filename(file->bs);
621         error_setg_errno(errp, -ret,
622                          "Could not read header from file '%s'",
623                          file->bs->filename);
624         return -EINVAL;
625     }
626     if (header.capacity == 0) {
627         uint64_t desc_offset = le64_to_cpu(header.desc_offset);
628         if (desc_offset) {
629             char *buf = vmdk_read_desc(file, desc_offset << 9, errp);
630             if (!buf) {
631                 return -EINVAL;
632             }
633             ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
634             g_free(buf);
635             return ret;
636         }
637     }
638 
639     if (!s->create_type) {
640         s->create_type = g_strdup("monolithicSparse");
641     }
642 
643     if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
644         /*
645          * The footer takes precedence over the header, so read it in. The
646          * footer starts at offset -1024 from the end: One sector for the
647          * footer, and another one for the end-of-stream marker.
648          */
649         struct {
650             struct {
651                 uint64_t val;
652                 uint32_t size;
653                 uint32_t type;
654                 uint8_t pad[512 - 16];
655             } QEMU_PACKED footer_marker;
656 
657             uint32_t magic;
658             VMDK4Header header;
659             uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
660 
661             struct {
662                 uint64_t val;
663                 uint32_t size;
664                 uint32_t type;
665                 uint8_t pad[512 - 16];
666             } QEMU_PACKED eos_marker;
667         } QEMU_PACKED footer;
668 
669         ret = bdrv_pread(file,
670             bs->file->bs->total_sectors * 512 - 1536,
671             &footer, sizeof(footer));
672         if (ret < 0) {
673             error_setg_errno(errp, -ret, "Failed to read footer");
674             return ret;
675         }
676 
677         /* Some sanity checks for the footer */
678         if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
679             le32_to_cpu(footer.footer_marker.size) != 0  ||
680             le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
681             le64_to_cpu(footer.eos_marker.val) != 0  ||
682             le32_to_cpu(footer.eos_marker.size) != 0  ||
683             le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
684         {
685             error_setg(errp, "Invalid footer");
686             return -EINVAL;
687         }
688 
689         header = footer.header;
690     }
691 
692     compressed =
693         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
694     if (le32_to_cpu(header.version) > 3) {
695         error_setg(errp, "Unsupported VMDK version %" PRIu32,
696                    le32_to_cpu(header.version));
697         return -ENOTSUP;
698     } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR) &&
699                !compressed) {
700         /* VMware KB 2064959 explains that version 3 added support for
701          * persistent changed block tracking (CBT), and backup software can
702          * read it as version=1 if it doesn't care about the changed area
703          * information. So we are safe to enable read only. */
704         error_setg(errp, "VMDK version 3 must be read only");
705         return -EINVAL;
706     }
707 
708     if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
709         error_setg(errp, "L2 table size too big");
710         return -EINVAL;
711     }
712 
713     l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
714                         * le64_to_cpu(header.granularity);
715     if (l1_entry_sectors == 0) {
716         error_setg(errp, "L1 entry size is invalid");
717         return -EINVAL;
718     }
719     l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
720                 / l1_entry_sectors;
721     if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
722         l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
723     }
724     if (bdrv_nb_sectors(file->bs) < le64_to_cpu(header.grain_offset)) {
725         error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",
726                    (int64_t)(le64_to_cpu(header.grain_offset)
727                              * BDRV_SECTOR_SIZE));
728         return -EINVAL;
729     }
730 
731     ret = vmdk_add_extent(bs, file, false,
732                           le64_to_cpu(header.capacity),
733                           le64_to_cpu(header.gd_offset) << 9,
734                           l1_backup_offset,
735                           l1_size,
736                           le32_to_cpu(header.num_gtes_per_gt),
737                           le64_to_cpu(header.granularity),
738                           &extent,
739                           errp);
740     if (ret < 0) {
741         return ret;
742     }
743     extent->compressed =
744         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
745     if (extent->compressed) {
746         g_free(s->create_type);
747         s->create_type = g_strdup("streamOptimized");
748     }
749     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
750     extent->version = le32_to_cpu(header.version);
751     extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
752     ret = vmdk_init_tables(bs, extent, errp);
753     if (ret) {
754         /* free extent allocated by vmdk_add_extent */
755         vmdk_free_last_extent(bs);
756     }
757     return ret;
758 }
759 
760 /* find an option value out of descriptor file */
761 static int vmdk_parse_description(const char *desc, const char *opt_name,
762         char *buf, int buf_size)
763 {
764     char *opt_pos, *opt_end;
765     const char *end = desc + strlen(desc);
766 
767     opt_pos = strstr(desc, opt_name);
768     if (!opt_pos) {
769         return VMDK_ERROR;
770     }
771     /* Skip "=\"" following opt_name */
772     opt_pos += strlen(opt_name) + 2;
773     if (opt_pos >= end) {
774         return VMDK_ERROR;
775     }
776     opt_end = opt_pos;
777     while (opt_end < end && *opt_end != '"') {
778         opt_end++;
779     }
780     if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
781         return VMDK_ERROR;
782     }
783     pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
784     return VMDK_OK;
785 }
786 
787 /* Open an extent file and append to bs array */
788 static int vmdk_open_sparse(BlockDriverState *bs, BdrvChild *file, int flags,
789                             char *buf, QDict *options, Error **errp)
790 {
791     uint32_t magic;
792 
793     magic = ldl_be_p(buf);
794     switch (magic) {
795         case VMDK3_MAGIC:
796             return vmdk_open_vmfs_sparse(bs, file, flags, errp);
797             break;
798         case VMDK4_MAGIC:
799             return vmdk_open_vmdk4(bs, file, flags, options, errp);
800             break;
801         default:
802             error_setg(errp, "Image not in VMDK format");
803             return -EINVAL;
804             break;
805     }
806 }
807 
808 static const char *next_line(const char *s)
809 {
810     while (*s) {
811         if (*s == '\n') {
812             return s + 1;
813         }
814         s++;
815     }
816     return s;
817 }
818 
819 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
820                               const char *desc_file_path, QDict *options,
821                               Error **errp)
822 {
823     int ret;
824     int matches;
825     char access[11];
826     char type[11];
827     char fname[512];
828     const char *p, *np;
829     int64_t sectors = 0;
830     int64_t flat_offset;
831     char *extent_path;
832     BdrvChild *extent_file;
833     BDRVVmdkState *s = bs->opaque;
834     VmdkExtent *extent;
835     char extent_opt_prefix[32];
836     Error *local_err = NULL;
837 
838     for (p = desc; *p; p = next_line(p)) {
839         /* parse extent line in one of below formats:
840          *
841          * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
842          * RW [size in sectors] SPARSE "file-name.vmdk"
843          * RW [size in sectors] VMFS "file-name.vmdk"
844          * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
845          */
846         flat_offset = -1;
847         matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
848                          access, &sectors, type, fname, &flat_offset);
849         if (matches < 4 || strcmp(access, "RW")) {
850             continue;
851         } else if (!strcmp(type, "FLAT")) {
852             if (matches != 5 || flat_offset < 0) {
853                 goto invalid;
854             }
855         } else if (!strcmp(type, "VMFS")) {
856             if (matches == 4) {
857                 flat_offset = 0;
858             } else {
859                 goto invalid;
860             }
861         } else if (matches != 4) {
862             goto invalid;
863         }
864 
865         if (sectors <= 0 ||
866             (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
867              strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
868             (strcmp(access, "RW"))) {
869             continue;
870         }
871 
872         if (!path_is_absolute(fname) && !path_has_protocol(fname) &&
873             !desc_file_path[0])
874         {
875             bdrv_refresh_filename(bs->file->bs);
876             error_setg(errp, "Cannot use relative extent paths with VMDK "
877                        "descriptor file '%s'", bs->file->bs->filename);
878             return -EINVAL;
879         }
880 
881         extent_path = path_combine(desc_file_path, fname);
882 
883         ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
884         assert(ret < 32);
885 
886         extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix,
887                                       bs, &child_file, false, &local_err);
888         g_free(extent_path);
889         if (local_err) {
890             error_propagate(errp, local_err);
891             return -EINVAL;
892         }
893 
894         /* save to extents array */
895         if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
896             /* FLAT extent */
897 
898             ret = vmdk_add_extent(bs, extent_file, true, sectors,
899                             0, 0, 0, 0, 0, &extent, errp);
900             if (ret < 0) {
901                 bdrv_unref_child(bs, extent_file);
902                 return ret;
903             }
904             extent->flat_start_offset = flat_offset << 9;
905         } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
906             /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
907             char *buf = vmdk_read_desc(extent_file, 0, errp);
908             if (!buf) {
909                 ret = -EINVAL;
910             } else {
911                 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
912                                        options, errp);
913             }
914             g_free(buf);
915             if (ret) {
916                 bdrv_unref_child(bs, extent_file);
917                 return ret;
918             }
919             extent = &s->extents[s->num_extents - 1];
920         } else {
921             error_setg(errp, "Unsupported extent type '%s'", type);
922             bdrv_unref_child(bs, extent_file);
923             return -ENOTSUP;
924         }
925         extent->type = g_strdup(type);
926     }
927     return 0;
928 
929 invalid:
930     np = next_line(p);
931     assert(np != p);
932     if (np[-1] == '\n') {
933         np--;
934     }
935     error_setg(errp, "Invalid extent line: %.*s", (int)(np - p), p);
936     return -EINVAL;
937 }
938 
939 static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
940                                QDict *options, Error **errp)
941 {
942     int ret;
943     char ct[128];
944     BDRVVmdkState *s = bs->opaque;
945 
946     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
947         error_setg(errp, "invalid VMDK image descriptor");
948         ret = -EINVAL;
949         goto exit;
950     }
951     if (strcmp(ct, "monolithicFlat") &&
952         strcmp(ct, "vmfs") &&
953         strcmp(ct, "vmfsSparse") &&
954         strcmp(ct, "twoGbMaxExtentSparse") &&
955         strcmp(ct, "twoGbMaxExtentFlat")) {
956         error_setg(errp, "Unsupported image type '%s'", ct);
957         ret = -ENOTSUP;
958         goto exit;
959     }
960     s->create_type = g_strdup(ct);
961     s->desc_offset = 0;
962     ret = vmdk_parse_extents(buf, bs, bs->file->bs->exact_filename, options,
963                              errp);
964 exit:
965     return ret;
966 }
967 
968 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
969                      Error **errp)
970 {
971     char *buf;
972     int ret;
973     BDRVVmdkState *s = bs->opaque;
974     uint32_t magic;
975     Error *local_err = NULL;
976 
977     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
978                                false, errp);
979     if (!bs->file) {
980         return -EINVAL;
981     }
982 
983     buf = vmdk_read_desc(bs->file, 0, errp);
984     if (!buf) {
985         return -EINVAL;
986     }
987 
988     magic = ldl_be_p(buf);
989     switch (magic) {
990         case VMDK3_MAGIC:
991         case VMDK4_MAGIC:
992             ret = vmdk_open_sparse(bs, bs->file, flags, buf, options,
993                                    errp);
994             s->desc_offset = 0x200;
995             break;
996         default:
997             ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
998             break;
999     }
1000     if (ret) {
1001         goto fail;
1002     }
1003 
1004     /* try to open parent images, if exist */
1005     ret = vmdk_parent_open(bs);
1006     if (ret) {
1007         goto fail;
1008     }
1009     ret = vmdk_read_cid(bs, 0, &s->cid);
1010     if (ret) {
1011         goto fail;
1012     }
1013     ret = vmdk_read_cid(bs, 1, &s->parent_cid);
1014     if (ret) {
1015         goto fail;
1016     }
1017     qemu_co_mutex_init(&s->lock);
1018 
1019     /* Disable migration when VMDK images are used */
1020     error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
1021                "does not support live migration",
1022                bdrv_get_device_or_node_name(bs));
1023     ret = migrate_add_blocker(s->migration_blocker, &local_err);
1024     if (local_err) {
1025         error_propagate(errp, local_err);
1026         error_free(s->migration_blocker);
1027         goto fail;
1028     }
1029 
1030     g_free(buf);
1031     return 0;
1032 
1033 fail:
1034     g_free(buf);
1035     g_free(s->create_type);
1036     s->create_type = NULL;
1037     vmdk_free_extents(bs);
1038     return ret;
1039 }
1040 
1041 
1042 static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp)
1043 {
1044     BDRVVmdkState *s = bs->opaque;
1045     int i;
1046 
1047     for (i = 0; i < s->num_extents; i++) {
1048         if (!s->extents[i].flat) {
1049             bs->bl.pwrite_zeroes_alignment =
1050                 MAX(bs->bl.pwrite_zeroes_alignment,
1051                     s->extents[i].cluster_sectors << BDRV_SECTOR_BITS);
1052         }
1053     }
1054 }
1055 
1056 /**
1057  * get_whole_cluster
1058  *
1059  * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1060  * to the cluster at @cluster_sector_num.
1061  *
1062  * If @skip_start_sector < @skip_end_sector, the relative range
1063  * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1064  * it for call to write user data in the request.
1065  */
1066 static int get_whole_cluster(BlockDriverState *bs,
1067                              VmdkExtent *extent,
1068                              uint64_t cluster_offset,
1069                              uint64_t offset,
1070                              uint64_t skip_start_bytes,
1071                              uint64_t skip_end_bytes)
1072 {
1073     int ret = VMDK_OK;
1074     int64_t cluster_bytes;
1075     uint8_t *whole_grain;
1076 
1077     /* For COW, align request sector_num to cluster start */
1078     cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
1079     offset = QEMU_ALIGN_DOWN(offset, cluster_bytes);
1080     whole_grain = qemu_blockalign(bs, cluster_bytes);
1081 
1082     if (!bs->backing) {
1083         memset(whole_grain, 0, skip_start_bytes);
1084         memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes);
1085     }
1086 
1087     assert(skip_end_bytes <= cluster_bytes);
1088     /* we will be here if it's first write on non-exist grain(cluster).
1089      * try to read from parent image, if exist */
1090     if (bs->backing && !vmdk_is_cid_valid(bs)) {
1091         ret = VMDK_ERROR;
1092         goto exit;
1093     }
1094 
1095     /* Read backing data before skip range */
1096     if (skip_start_bytes > 0) {
1097         if (bs->backing) {
1098             /* qcow2 emits this on bs->file instead of bs->backing */
1099             BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
1100             ret = bdrv_pread(bs->backing, offset, whole_grain,
1101                              skip_start_bytes);
1102             if (ret < 0) {
1103                 ret = VMDK_ERROR;
1104                 goto exit;
1105             }
1106         }
1107         BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
1108         ret = bdrv_pwrite(extent->file, cluster_offset, whole_grain,
1109                           skip_start_bytes);
1110         if (ret < 0) {
1111             ret = VMDK_ERROR;
1112             goto exit;
1113         }
1114     }
1115     /* Read backing data after skip range */
1116     if (skip_end_bytes < cluster_bytes) {
1117         if (bs->backing) {
1118             /* qcow2 emits this on bs->file instead of bs->backing */
1119             BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
1120             ret = bdrv_pread(bs->backing, offset + skip_end_bytes,
1121                              whole_grain + skip_end_bytes,
1122                              cluster_bytes - skip_end_bytes);
1123             if (ret < 0) {
1124                 ret = VMDK_ERROR;
1125                 goto exit;
1126             }
1127         }
1128         BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
1129         ret = bdrv_pwrite(extent->file, cluster_offset + skip_end_bytes,
1130                           whole_grain + skip_end_bytes,
1131                           cluster_bytes - skip_end_bytes);
1132         if (ret < 0) {
1133             ret = VMDK_ERROR;
1134             goto exit;
1135         }
1136     }
1137 
1138     ret = VMDK_OK;
1139 exit:
1140     qemu_vfree(whole_grain);
1141     return ret;
1142 }
1143 
1144 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data,
1145                          uint32_t offset)
1146 {
1147     offset = cpu_to_le32(offset);
1148     /* update L2 table */
1149     BLKDBG_EVENT(extent->file, BLKDBG_L2_UPDATE);
1150     if (bdrv_pwrite_sync(extent->file,
1151                 ((int64_t)m_data->l2_offset * 512)
1152                     + (m_data->l2_index * sizeof(offset)),
1153                 &offset, sizeof(offset)) < 0) {
1154         return VMDK_ERROR;
1155     }
1156     /* update backup L2 table */
1157     if (extent->l1_backup_table_offset != 0) {
1158         m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
1159         if (bdrv_pwrite_sync(extent->file,
1160                     ((int64_t)m_data->l2_offset * 512)
1161                         + (m_data->l2_index * sizeof(offset)),
1162                     &offset, sizeof(offset)) < 0) {
1163             return VMDK_ERROR;
1164         }
1165     }
1166     if (m_data->l2_cache_entry) {
1167         *m_data->l2_cache_entry = offset;
1168     }
1169 
1170     return VMDK_OK;
1171 }
1172 
1173 /**
1174  * get_cluster_offset
1175  *
1176  * Look up cluster offset in extent file by sector number, and store in
1177  * @cluster_offset.
1178  *
1179  * For flat extents, the start offset as parsed from the description file is
1180  * returned.
1181  *
1182  * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1183  * offset for a new cluster and update L2 cache. If there is a backing file,
1184  * COW is done before returning; otherwise, zeroes are written to the allocated
1185  * cluster. Both COW and zero writing skips the sector range
1186  * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1187  * has new data to write there.
1188  *
1189  * Returns: VMDK_OK if cluster exists and mapped in the image.
1190  *          VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1191  *          VMDK_ERROR if failed.
1192  */
1193 static int get_cluster_offset(BlockDriverState *bs,
1194                               VmdkExtent *extent,
1195                               VmdkMetaData *m_data,
1196                               uint64_t offset,
1197                               bool allocate,
1198                               uint64_t *cluster_offset,
1199                               uint64_t skip_start_bytes,
1200                               uint64_t skip_end_bytes)
1201 {
1202     unsigned int l1_index, l2_offset, l2_index;
1203     int min_index, i, j;
1204     uint32_t min_count, *l2_table;
1205     bool zeroed = false;
1206     int64_t ret;
1207     int64_t cluster_sector;
1208 
1209     if (m_data) {
1210         m_data->valid = 0;
1211     }
1212     if (extent->flat) {
1213         *cluster_offset = extent->flat_start_offset;
1214         return VMDK_OK;
1215     }
1216 
1217     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
1218     l1_index = (offset >> 9) / extent->l1_entry_sectors;
1219     if (l1_index >= extent->l1_size) {
1220         return VMDK_ERROR;
1221     }
1222     l2_offset = extent->l1_table[l1_index];
1223     if (!l2_offset) {
1224         return VMDK_UNALLOC;
1225     }
1226     for (i = 0; i < L2_CACHE_SIZE; i++) {
1227         if (l2_offset == extent->l2_cache_offsets[i]) {
1228             /* increment the hit count */
1229             if (++extent->l2_cache_counts[i] == 0xffffffff) {
1230                 for (j = 0; j < L2_CACHE_SIZE; j++) {
1231                     extent->l2_cache_counts[j] >>= 1;
1232                 }
1233             }
1234             l2_table = extent->l2_cache + (i * extent->l2_size);
1235             goto found;
1236         }
1237     }
1238     /* not found: load a new entry in the least used one */
1239     min_index = 0;
1240     min_count = 0xffffffff;
1241     for (i = 0; i < L2_CACHE_SIZE; i++) {
1242         if (extent->l2_cache_counts[i] < min_count) {
1243             min_count = extent->l2_cache_counts[i];
1244             min_index = i;
1245         }
1246     }
1247     l2_table = extent->l2_cache + (min_index * extent->l2_size);
1248     BLKDBG_EVENT(extent->file, BLKDBG_L2_LOAD);
1249     if (bdrv_pread(extent->file,
1250                 (int64_t)l2_offset * 512,
1251                 l2_table,
1252                 extent->l2_size * sizeof(uint32_t)
1253             ) != extent->l2_size * sizeof(uint32_t)) {
1254         return VMDK_ERROR;
1255     }
1256 
1257     extent->l2_cache_offsets[min_index] = l2_offset;
1258     extent->l2_cache_counts[min_index] = 1;
1259  found:
1260     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
1261     cluster_sector = le32_to_cpu(l2_table[l2_index]);
1262 
1263     if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) {
1264         zeroed = true;
1265     }
1266 
1267     if (!cluster_sector || zeroed) {
1268         if (!allocate) {
1269             return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1270         }
1271 
1272         if (extent->next_cluster_sector >= VMDK_EXTENT_MAX_SECTORS) {
1273             return VMDK_ERROR;
1274         }
1275 
1276         cluster_sector = extent->next_cluster_sector;
1277         extent->next_cluster_sector += extent->cluster_sectors;
1278 
1279         /* First of all we write grain itself, to avoid race condition
1280          * that may to corrupt the image.
1281          * This problem may occur because of insufficient space on host disk
1282          * or inappropriate VM shutdown.
1283          */
1284         ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE,
1285                                 offset, skip_start_bytes, skip_end_bytes);
1286         if (ret) {
1287             return ret;
1288         }
1289         if (m_data) {
1290             m_data->valid = 1;
1291             m_data->l1_index = l1_index;
1292             m_data->l2_index = l2_index;
1293             m_data->l2_offset = l2_offset;
1294             m_data->l2_cache_entry = &l2_table[l2_index];
1295         }
1296     }
1297     *cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
1298     return VMDK_OK;
1299 }
1300 
1301 static VmdkExtent *find_extent(BDRVVmdkState *s,
1302                                 int64_t sector_num, VmdkExtent *start_hint)
1303 {
1304     VmdkExtent *extent = start_hint;
1305 
1306     if (!extent) {
1307         extent = &s->extents[0];
1308     }
1309     while (extent < &s->extents[s->num_extents]) {
1310         if (sector_num < extent->end_sector) {
1311             return extent;
1312         }
1313         extent++;
1314     }
1315     return NULL;
1316 }
1317 
1318 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent,
1319                                                    int64_t offset)
1320 {
1321     uint64_t extent_begin_offset, extent_relative_offset;
1322     uint64_t cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE;
1323 
1324     extent_begin_offset =
1325         (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE;
1326     extent_relative_offset = offset - extent_begin_offset;
1327     return extent_relative_offset % cluster_size;
1328 }
1329 
1330 static int coroutine_fn vmdk_co_block_status(BlockDriverState *bs,
1331                                              bool want_zero,
1332                                              int64_t offset, int64_t bytes,
1333                                              int64_t *pnum, int64_t *map,
1334                                              BlockDriverState **file)
1335 {
1336     BDRVVmdkState *s = bs->opaque;
1337     int64_t index_in_cluster, n, ret;
1338     uint64_t cluster_offset;
1339     VmdkExtent *extent;
1340 
1341     extent = find_extent(s, offset >> BDRV_SECTOR_BITS, NULL);
1342     if (!extent) {
1343         return -EIO;
1344     }
1345     qemu_co_mutex_lock(&s->lock);
1346     ret = get_cluster_offset(bs, extent, NULL, offset, false, &cluster_offset,
1347                              0, 0);
1348     qemu_co_mutex_unlock(&s->lock);
1349 
1350     index_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1351     switch (ret) {
1352     case VMDK_ERROR:
1353         ret = -EIO;
1354         break;
1355     case VMDK_UNALLOC:
1356         ret = 0;
1357         break;
1358     case VMDK_ZEROED:
1359         ret = BDRV_BLOCK_ZERO;
1360         break;
1361     case VMDK_OK:
1362         ret = BDRV_BLOCK_DATA;
1363         if (!extent->compressed) {
1364             ret |= BDRV_BLOCK_OFFSET_VALID;
1365             *map = cluster_offset + index_in_cluster;
1366         }
1367         *file = extent->file->bs;
1368         break;
1369     }
1370 
1371     n = extent->cluster_sectors * BDRV_SECTOR_SIZE - index_in_cluster;
1372     *pnum = MIN(n, bytes);
1373     return ret;
1374 }
1375 
1376 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1377                             int64_t offset_in_cluster, QEMUIOVector *qiov,
1378                             uint64_t qiov_offset, uint64_t n_bytes,
1379                             uint64_t offset)
1380 {
1381     int ret;
1382     VmdkGrainMarker *data = NULL;
1383     uLongf buf_len;
1384     QEMUIOVector local_qiov;
1385     int64_t write_offset;
1386     int64_t write_end_sector;
1387 
1388     if (extent->compressed) {
1389         void *compressed_data;
1390 
1391         if (!extent->has_marker) {
1392             ret = -EINVAL;
1393             goto out;
1394         }
1395         buf_len = (extent->cluster_sectors << 9) * 2;
1396         data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1397 
1398         compressed_data = g_malloc(n_bytes);
1399         qemu_iovec_to_buf(qiov, qiov_offset, compressed_data, n_bytes);
1400         ret = compress(data->data, &buf_len, compressed_data, n_bytes);
1401         g_free(compressed_data);
1402 
1403         if (ret != Z_OK || buf_len == 0) {
1404             ret = -EINVAL;
1405             goto out;
1406         }
1407 
1408         data->lba = cpu_to_le64(offset >> BDRV_SECTOR_BITS);
1409         data->size = cpu_to_le32(buf_len);
1410 
1411         n_bytes = buf_len + sizeof(VmdkGrainMarker);
1412         qemu_iovec_init_buf(&local_qiov, data, n_bytes);
1413 
1414         BLKDBG_EVENT(extent->file, BLKDBG_WRITE_COMPRESSED);
1415     } else {
1416         qemu_iovec_init(&local_qiov, qiov->niov);
1417         qemu_iovec_concat(&local_qiov, qiov, qiov_offset, n_bytes);
1418 
1419         BLKDBG_EVENT(extent->file, BLKDBG_WRITE_AIO);
1420     }
1421 
1422     write_offset = cluster_offset + offset_in_cluster;
1423     ret = bdrv_co_pwritev(extent->file, write_offset, n_bytes,
1424                           &local_qiov, 0);
1425 
1426     write_end_sector = DIV_ROUND_UP(write_offset + n_bytes, BDRV_SECTOR_SIZE);
1427 
1428     if (extent->compressed) {
1429         extent->next_cluster_sector = write_end_sector;
1430     } else {
1431         extent->next_cluster_sector = MAX(extent->next_cluster_sector,
1432                                           write_end_sector);
1433     }
1434 
1435     if (ret < 0) {
1436         goto out;
1437     }
1438     ret = 0;
1439  out:
1440     g_free(data);
1441     if (!extent->compressed) {
1442         qemu_iovec_destroy(&local_qiov);
1443     }
1444     return ret;
1445 }
1446 
1447 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1448                             int64_t offset_in_cluster, QEMUIOVector *qiov,
1449                             int bytes)
1450 {
1451     int ret;
1452     int cluster_bytes, buf_bytes;
1453     uint8_t *cluster_buf, *compressed_data;
1454     uint8_t *uncomp_buf;
1455     uint32_t data_len;
1456     VmdkGrainMarker *marker;
1457     uLongf buf_len;
1458 
1459 
1460     if (!extent->compressed) {
1461         BLKDBG_EVENT(extent->file, BLKDBG_READ_AIO);
1462         ret = bdrv_co_preadv(extent->file,
1463                              cluster_offset + offset_in_cluster, bytes,
1464                              qiov, 0);
1465         if (ret < 0) {
1466             return ret;
1467         }
1468         return 0;
1469     }
1470     cluster_bytes = extent->cluster_sectors * 512;
1471     /* Read two clusters in case GrainMarker + compressed data > one cluster */
1472     buf_bytes = cluster_bytes * 2;
1473     cluster_buf = g_malloc(buf_bytes);
1474     uncomp_buf = g_malloc(cluster_bytes);
1475     BLKDBG_EVENT(extent->file, BLKDBG_READ_COMPRESSED);
1476     ret = bdrv_pread(extent->file,
1477                 cluster_offset,
1478                 cluster_buf, buf_bytes);
1479     if (ret < 0) {
1480         goto out;
1481     }
1482     compressed_data = cluster_buf;
1483     buf_len = cluster_bytes;
1484     data_len = cluster_bytes;
1485     if (extent->has_marker) {
1486         marker = (VmdkGrainMarker *)cluster_buf;
1487         compressed_data = marker->data;
1488         data_len = le32_to_cpu(marker->size);
1489     }
1490     if (!data_len || data_len > buf_bytes) {
1491         ret = -EINVAL;
1492         goto out;
1493     }
1494     ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1495     if (ret != Z_OK) {
1496         ret = -EINVAL;
1497         goto out;
1498 
1499     }
1500     if (offset_in_cluster < 0 ||
1501             offset_in_cluster + bytes > buf_len) {
1502         ret = -EINVAL;
1503         goto out;
1504     }
1505     qemu_iovec_from_buf(qiov, 0, uncomp_buf + offset_in_cluster, bytes);
1506     ret = 0;
1507 
1508  out:
1509     g_free(uncomp_buf);
1510     g_free(cluster_buf);
1511     return ret;
1512 }
1513 
1514 static int coroutine_fn
1515 vmdk_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1516                QEMUIOVector *qiov, int flags)
1517 {
1518     BDRVVmdkState *s = bs->opaque;
1519     int ret;
1520     uint64_t n_bytes, offset_in_cluster;
1521     VmdkExtent *extent = NULL;
1522     QEMUIOVector local_qiov;
1523     uint64_t cluster_offset;
1524     uint64_t bytes_done = 0;
1525 
1526     qemu_iovec_init(&local_qiov, qiov->niov);
1527     qemu_co_mutex_lock(&s->lock);
1528 
1529     while (bytes > 0) {
1530         extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
1531         if (!extent) {
1532             ret = -EIO;
1533             goto fail;
1534         }
1535         ret = get_cluster_offset(bs, extent, NULL,
1536                                  offset, false, &cluster_offset, 0, 0);
1537         offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1538 
1539         n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1540                              - offset_in_cluster);
1541 
1542         if (ret != VMDK_OK) {
1543             /* if not allocated, try to read from parent image, if exist */
1544             if (bs->backing && ret != VMDK_ZEROED) {
1545                 if (!vmdk_is_cid_valid(bs)) {
1546                     ret = -EINVAL;
1547                     goto fail;
1548                 }
1549 
1550                 qemu_iovec_reset(&local_qiov);
1551                 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1552 
1553                 /* qcow2 emits this on bs->file instead of bs->backing */
1554                 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1555                 ret = bdrv_co_preadv(bs->backing, offset, n_bytes,
1556                                      &local_qiov, 0);
1557                 if (ret < 0) {
1558                     goto fail;
1559                 }
1560             } else {
1561                 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
1562             }
1563         } else {
1564             qemu_iovec_reset(&local_qiov);
1565             qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1566 
1567             ret = vmdk_read_extent(extent, cluster_offset, offset_in_cluster,
1568                                    &local_qiov, n_bytes);
1569             if (ret) {
1570                 goto fail;
1571             }
1572         }
1573         bytes -= n_bytes;
1574         offset += n_bytes;
1575         bytes_done += n_bytes;
1576     }
1577 
1578     ret = 0;
1579 fail:
1580     qemu_co_mutex_unlock(&s->lock);
1581     qemu_iovec_destroy(&local_qiov);
1582 
1583     return ret;
1584 }
1585 
1586 /**
1587  * vmdk_write:
1588  * @zeroed:       buf is ignored (data is zero), use zeroed_grain GTE feature
1589  *                if possible, otherwise return -ENOTSUP.
1590  * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1591  *                with each cluster. By dry run we can find if the zero write
1592  *                is possible without modifying image data.
1593  *
1594  * Returns: error code with 0 for success.
1595  */
1596 static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset,
1597                        uint64_t bytes, QEMUIOVector *qiov,
1598                        bool zeroed, bool zero_dry_run)
1599 {
1600     BDRVVmdkState *s = bs->opaque;
1601     VmdkExtent *extent = NULL;
1602     int ret;
1603     int64_t offset_in_cluster, n_bytes;
1604     uint64_t cluster_offset;
1605     uint64_t bytes_done = 0;
1606     VmdkMetaData m_data;
1607 
1608     if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) {
1609         error_report("Wrong offset: offset=0x%" PRIx64
1610                      " total_sectors=0x%" PRIx64,
1611                      offset, bs->total_sectors);
1612         return -EIO;
1613     }
1614 
1615     while (bytes > 0) {
1616         extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
1617         if (!extent) {
1618             return -EIO;
1619         }
1620         offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1621         n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1622                              - offset_in_cluster);
1623 
1624         ret = get_cluster_offset(bs, extent, &m_data, offset,
1625                                  !(extent->compressed || zeroed),
1626                                  &cluster_offset, offset_in_cluster,
1627                                  offset_in_cluster + n_bytes);
1628         if (extent->compressed) {
1629             if (ret == VMDK_OK) {
1630                 /* Refuse write to allocated cluster for streamOptimized */
1631                 error_report("Could not write to allocated cluster"
1632                               " for streamOptimized");
1633                 return -EIO;
1634             } else {
1635                 /* allocate */
1636                 ret = get_cluster_offset(bs, extent, &m_data, offset,
1637                                          true, &cluster_offset, 0, 0);
1638             }
1639         }
1640         if (ret == VMDK_ERROR) {
1641             return -EINVAL;
1642         }
1643         if (zeroed) {
1644             /* Do zeroed write, buf is ignored */
1645             if (extent->has_zero_grain &&
1646                     offset_in_cluster == 0 &&
1647                     n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) {
1648                 n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE;
1649                 if (!zero_dry_run) {
1650                     /* update L2 tables */
1651                     if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
1652                             != VMDK_OK) {
1653                         return -EIO;
1654                     }
1655                 }
1656             } else {
1657                 return -ENOTSUP;
1658             }
1659         } else {
1660             ret = vmdk_write_extent(extent, cluster_offset, offset_in_cluster,
1661                                     qiov, bytes_done, n_bytes, offset);
1662             if (ret) {
1663                 return ret;
1664             }
1665             if (m_data.valid) {
1666                 /* update L2 tables */
1667                 if (vmdk_L2update(extent, &m_data,
1668                                   cluster_offset >> BDRV_SECTOR_BITS)
1669                         != VMDK_OK) {
1670                     return -EIO;
1671                 }
1672             }
1673         }
1674         bytes -= n_bytes;
1675         offset += n_bytes;
1676         bytes_done += n_bytes;
1677 
1678         /* update CID on the first write every time the virtual disk is
1679          * opened */
1680         if (!s->cid_updated) {
1681             ret = vmdk_write_cid(bs, g_random_int());
1682             if (ret < 0) {
1683                 return ret;
1684             }
1685             s->cid_updated = true;
1686         }
1687     }
1688     return 0;
1689 }
1690 
1691 static int coroutine_fn
1692 vmdk_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1693                 QEMUIOVector *qiov, int flags)
1694 {
1695     int ret;
1696     BDRVVmdkState *s = bs->opaque;
1697     qemu_co_mutex_lock(&s->lock);
1698     ret = vmdk_pwritev(bs, offset, bytes, qiov, false, false);
1699     qemu_co_mutex_unlock(&s->lock);
1700     return ret;
1701 }
1702 
1703 static int coroutine_fn
1704 vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
1705                            uint64_t bytes, QEMUIOVector *qiov)
1706 {
1707     if (bytes == 0) {
1708         /* The caller will write bytes 0 to signal EOF.
1709          * When receive it, we align EOF to a sector boundary. */
1710         BDRVVmdkState *s = bs->opaque;
1711         int i, ret;
1712         int64_t length;
1713 
1714         for (i = 0; i < s->num_extents; i++) {
1715             length = bdrv_getlength(s->extents[i].file->bs);
1716             if (length < 0) {
1717                 return length;
1718             }
1719             length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
1720             ret = bdrv_truncate(s->extents[i].file, length,
1721                                 PREALLOC_MODE_OFF, NULL);
1722             if (ret < 0) {
1723                 return ret;
1724             }
1725         }
1726         return 0;
1727     }
1728     return vmdk_co_pwritev(bs, offset, bytes, qiov, 0);
1729 }
1730 
1731 static int coroutine_fn vmdk_co_pwrite_zeroes(BlockDriverState *bs,
1732                                               int64_t offset,
1733                                               int bytes,
1734                                               BdrvRequestFlags flags)
1735 {
1736     int ret;
1737     BDRVVmdkState *s = bs->opaque;
1738 
1739     qemu_co_mutex_lock(&s->lock);
1740     /* write zeroes could fail if sectors not aligned to cluster, test it with
1741      * dry_run == true before really updating image */
1742     ret = vmdk_pwritev(bs, offset, bytes, NULL, true, true);
1743     if (!ret) {
1744         ret = vmdk_pwritev(bs, offset, bytes, NULL, true, false);
1745     }
1746     qemu_co_mutex_unlock(&s->lock);
1747     return ret;
1748 }
1749 
1750 static int vmdk_init_extent(BlockBackend *blk,
1751                             int64_t filesize, bool flat,
1752                             bool compress, bool zeroed_grain,
1753                             Error **errp)
1754 {
1755     int ret, i;
1756     VMDK4Header header;
1757     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1758     uint32_t *gd_buf = NULL;
1759     int gd_buf_size;
1760 
1761     if (flat) {
1762         ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp);
1763         goto exit;
1764     }
1765     magic = cpu_to_be32(VMDK4_MAGIC);
1766     memset(&header, 0, sizeof(header));
1767     if (compress) {
1768         header.version = 3;
1769     } else if (zeroed_grain) {
1770         header.version = 2;
1771     } else {
1772         header.version = 1;
1773     }
1774     header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1775                    | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1776                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1777     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1778     header.capacity = filesize / BDRV_SECTOR_SIZE;
1779     header.granularity = 128;
1780     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
1781 
1782     grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1783     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1784                            BDRV_SECTOR_SIZE);
1785     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1786     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
1787 
1788     header.desc_offset = 1;
1789     header.desc_size = 20;
1790     header.rgd_offset = header.desc_offset + header.desc_size;
1791     header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
1792     header.grain_offset =
1793         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1794                  header.granularity);
1795     /* swap endianness for all header fields */
1796     header.version = cpu_to_le32(header.version);
1797     header.flags = cpu_to_le32(header.flags);
1798     header.capacity = cpu_to_le64(header.capacity);
1799     header.granularity = cpu_to_le64(header.granularity);
1800     header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1801     header.desc_offset = cpu_to_le64(header.desc_offset);
1802     header.desc_size = cpu_to_le64(header.desc_size);
1803     header.rgd_offset = cpu_to_le64(header.rgd_offset);
1804     header.gd_offset = cpu_to_le64(header.gd_offset);
1805     header.grain_offset = cpu_to_le64(header.grain_offset);
1806     header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1807 
1808     header.check_bytes[0] = 0xa;
1809     header.check_bytes[1] = 0x20;
1810     header.check_bytes[2] = 0xd;
1811     header.check_bytes[3] = 0xa;
1812 
1813     /* write all the data */
1814     ret = blk_pwrite(blk, 0, &magic, sizeof(magic), 0);
1815     if (ret < 0) {
1816         error_setg(errp, QERR_IO_ERROR);
1817         goto exit;
1818     }
1819     ret = blk_pwrite(blk, sizeof(magic), &header, sizeof(header), 0);
1820     if (ret < 0) {
1821         error_setg(errp, QERR_IO_ERROR);
1822         goto exit;
1823     }
1824 
1825     ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9,
1826                        PREALLOC_MODE_OFF, errp);
1827     if (ret < 0) {
1828         goto exit;
1829     }
1830 
1831     /* write grain directory */
1832     gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1833     gd_buf = g_malloc0(gd_buf_size);
1834     for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
1835          i < gt_count; i++, tmp += gt_size) {
1836         gd_buf[i] = cpu_to_le32(tmp);
1837     }
1838     ret = blk_pwrite(blk, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
1839                      gd_buf, gd_buf_size, 0);
1840     if (ret < 0) {
1841         error_setg(errp, QERR_IO_ERROR);
1842         goto exit;
1843     }
1844 
1845     /* write backup grain directory */
1846     for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
1847          i < gt_count; i++, tmp += gt_size) {
1848         gd_buf[i] = cpu_to_le32(tmp);
1849     }
1850     ret = blk_pwrite(blk, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
1851                      gd_buf, gd_buf_size, 0);
1852     if (ret < 0) {
1853         error_setg(errp, QERR_IO_ERROR);
1854     }
1855 
1856     ret = 0;
1857 exit:
1858     g_free(gd_buf);
1859     return ret;
1860 }
1861 
1862 static int vmdk_create_extent(const char *filename, int64_t filesize,
1863                               bool flat, bool compress, bool zeroed_grain,
1864                               BlockBackend **pbb,
1865                               QemuOpts *opts, Error **errp)
1866 {
1867     int ret;
1868     BlockBackend *blk = NULL;
1869     Error *local_err = NULL;
1870 
1871     ret = bdrv_create_file(filename, opts, &local_err);
1872     if (ret < 0) {
1873         error_propagate(errp, local_err);
1874         goto exit;
1875     }
1876 
1877     blk = blk_new_open(filename, NULL, NULL,
1878                        BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
1879                        &local_err);
1880     if (blk == NULL) {
1881         error_propagate(errp, local_err);
1882         ret = -EIO;
1883         goto exit;
1884     }
1885 
1886     blk_set_allow_write_beyond_eof(blk, true);
1887 
1888     ret = vmdk_init_extent(blk, filesize, flat, compress, zeroed_grain, errp);
1889 exit:
1890     if (blk) {
1891         if (pbb) {
1892             *pbb = blk;
1893         } else {
1894             blk_unref(blk);
1895             blk = NULL;
1896         }
1897     }
1898     return ret;
1899 }
1900 
1901 static int filename_decompose(const char *filename, char *path, char *prefix,
1902                               char *postfix, size_t buf_len, Error **errp)
1903 {
1904     const char *p, *q;
1905 
1906     if (filename == NULL || !strlen(filename)) {
1907         error_setg(errp, "No filename provided");
1908         return VMDK_ERROR;
1909     }
1910     p = strrchr(filename, '/');
1911     if (p == NULL) {
1912         p = strrchr(filename, '\\');
1913     }
1914     if (p == NULL) {
1915         p = strrchr(filename, ':');
1916     }
1917     if (p != NULL) {
1918         p++;
1919         if (p - filename >= buf_len) {
1920             return VMDK_ERROR;
1921         }
1922         pstrcpy(path, p - filename + 1, filename);
1923     } else {
1924         p = filename;
1925         path[0] = '\0';
1926     }
1927     q = strrchr(p, '.');
1928     if (q == NULL) {
1929         pstrcpy(prefix, buf_len, p);
1930         postfix[0] = '\0';
1931     } else {
1932         if (q - p >= buf_len) {
1933             return VMDK_ERROR;
1934         }
1935         pstrcpy(prefix, q - p + 1, p);
1936         pstrcpy(postfix, buf_len, q);
1937     }
1938     return VMDK_OK;
1939 }
1940 
1941 /*
1942  * idx == 0: get or create the descriptor file (also the image file if in a
1943  *           non-split format.
1944  * idx >= 1: get the n-th extent if in a split subformat
1945  */
1946 typedef BlockBackend *(*vmdk_create_extent_fn)(int64_t size,
1947                                                int idx,
1948                                                bool flat,
1949                                                bool split,
1950                                                bool compress,
1951                                                bool zeroed_grain,
1952                                                void *opaque,
1953                                                Error **errp);
1954 
1955 static void vmdk_desc_add_extent(GString *desc,
1956                                  const char *extent_line_fmt,
1957                                  int64_t size, const char *filename)
1958 {
1959     char *basename = g_path_get_basename(filename);
1960 
1961     g_string_append_printf(desc, extent_line_fmt,
1962                            DIV_ROUND_UP(size, BDRV_SECTOR_SIZE), basename);
1963     g_free(basename);
1964 }
1965 
1966 static int coroutine_fn vmdk_co_do_create(int64_t size,
1967                                           BlockdevVmdkSubformat subformat,
1968                                           BlockdevVmdkAdapterType adapter_type,
1969                                           const char *backing_file,
1970                                           const char *hw_version,
1971                                           bool compat6,
1972                                           bool zeroed_grain,
1973                                           vmdk_create_extent_fn extent_fn,
1974                                           void *opaque,
1975                                           Error **errp)
1976 {
1977     int extent_idx;
1978     BlockBackend *blk = NULL;
1979     BlockBackend *extent_blk;
1980     Error *local_err = NULL;
1981     char *desc = NULL;
1982     int ret = 0;
1983     bool flat, split, compress;
1984     GString *ext_desc_lines;
1985     const int64_t split_size = 0x80000000;  /* VMDK has constant split size */
1986     int64_t extent_size;
1987     int64_t created_size = 0;
1988     const char *extent_line_fmt;
1989     char *parent_desc_line = g_malloc0(BUF_SIZE);
1990     uint32_t parent_cid = 0xffffffff;
1991     uint32_t number_heads = 16;
1992     uint32_t desc_offset = 0, desc_len;
1993     const char desc_template[] =
1994         "# Disk DescriptorFile\n"
1995         "version=1\n"
1996         "CID=%" PRIx32 "\n"
1997         "parentCID=%" PRIx32 "\n"
1998         "createType=\"%s\"\n"
1999         "%s"
2000         "\n"
2001         "# Extent description\n"
2002         "%s"
2003         "\n"
2004         "# The Disk Data Base\n"
2005         "#DDB\n"
2006         "\n"
2007         "ddb.virtualHWVersion = \"%s\"\n"
2008         "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
2009         "ddb.geometry.heads = \"%" PRIu32 "\"\n"
2010         "ddb.geometry.sectors = \"63\"\n"
2011         "ddb.adapterType = \"%s\"\n";
2012 
2013     ext_desc_lines = g_string_new(NULL);
2014 
2015     /* Read out options */
2016     if (compat6) {
2017         if (hw_version) {
2018             error_setg(errp,
2019                        "compat6 cannot be enabled with hwversion set");
2020             ret = -EINVAL;
2021             goto exit;
2022         }
2023         hw_version = "6";
2024     }
2025     if (!hw_version) {
2026         hw_version = "4";
2027     }
2028 
2029     if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) {
2030         /* that's the number of heads with which vmware operates when
2031            creating, exporting, etc. vmdk files with a non-ide adapter type */
2032         number_heads = 255;
2033     }
2034     split = (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT) ||
2035             (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE);
2036     flat = (subformat == BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT) ||
2037            (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT);
2038     compress = subformat == BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED;
2039 
2040     if (flat) {
2041         extent_line_fmt = "RW %" PRId64 " FLAT \"%s\" 0\n";
2042     } else {
2043         extent_line_fmt = "RW %" PRId64 " SPARSE \"%s\"\n";
2044     }
2045     if (flat && backing_file) {
2046         error_setg(errp, "Flat image can't have backing file");
2047         ret = -ENOTSUP;
2048         goto exit;
2049     }
2050     if (flat && zeroed_grain) {
2051         error_setg(errp, "Flat image can't enable zeroed grain");
2052         ret = -ENOTSUP;
2053         goto exit;
2054     }
2055 
2056     /* Create extents */
2057     if (split) {
2058         extent_size = split_size;
2059     } else {
2060         extent_size = size;
2061     }
2062     if (!split && !flat) {
2063         created_size = extent_size;
2064     } else {
2065         created_size = 0;
2066     }
2067     /* Get the descriptor file BDS */
2068     blk = extent_fn(created_size, 0, flat, split, compress, zeroed_grain,
2069                     opaque, errp);
2070     if (!blk) {
2071         ret = -EIO;
2072         goto exit;
2073     }
2074     if (!split && !flat) {
2075         vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, created_size,
2076                              blk_bs(blk)->filename);
2077     }
2078 
2079     if (backing_file) {
2080         BlockBackend *backing;
2081         char *full_backing =
2082             bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename,
2083                                                          backing_file,
2084                                                          &local_err);
2085         if (local_err) {
2086             error_propagate(errp, local_err);
2087             ret = -ENOENT;
2088             goto exit;
2089         }
2090         assert(full_backing);
2091 
2092         backing = blk_new_open(full_backing, NULL, NULL,
2093                                BDRV_O_NO_BACKING, errp);
2094         g_free(full_backing);
2095         if (backing == NULL) {
2096             ret = -EIO;
2097             goto exit;
2098         }
2099         if (strcmp(blk_bs(backing)->drv->format_name, "vmdk")) {
2100             error_setg(errp, "Invalid backing file format: %s. Must be vmdk",
2101                        blk_bs(backing)->drv->format_name);
2102             blk_unref(backing);
2103             ret = -EINVAL;
2104             goto exit;
2105         }
2106         ret = vmdk_read_cid(blk_bs(backing), 0, &parent_cid);
2107         blk_unref(backing);
2108         if (ret) {
2109             error_setg(errp, "Failed to read parent CID");
2110             goto exit;
2111         }
2112         snprintf(parent_desc_line, BUF_SIZE,
2113                 "parentFileNameHint=\"%s\"", backing_file);
2114     }
2115     extent_idx = 1;
2116     while (created_size < size) {
2117         int64_t cur_size = MIN(size - created_size, extent_size);
2118         extent_blk = extent_fn(cur_size, extent_idx, flat, split, compress,
2119                                zeroed_grain, opaque, errp);
2120         if (!extent_blk) {
2121             ret = -EINVAL;
2122             goto exit;
2123         }
2124         vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, cur_size,
2125                              blk_bs(extent_blk)->filename);
2126         created_size += cur_size;
2127         extent_idx++;
2128         blk_unref(extent_blk);
2129     }
2130 
2131     /* Check whether we got excess extents */
2132     extent_blk = extent_fn(-1, extent_idx, flat, split, compress, zeroed_grain,
2133                            opaque, NULL);
2134     if (extent_blk) {
2135         blk_unref(extent_blk);
2136         error_setg(errp, "List of extents contains unused extents");
2137         ret = -EINVAL;
2138         goto exit;
2139     }
2140 
2141     /* generate descriptor file */
2142     desc = g_strdup_printf(desc_template,
2143                            g_random_int(),
2144                            parent_cid,
2145                            BlockdevVmdkSubformat_str(subformat),
2146                            parent_desc_line,
2147                            ext_desc_lines->str,
2148                            hw_version,
2149                            size /
2150                                (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
2151                            number_heads,
2152                            BlockdevVmdkAdapterType_str(adapter_type));
2153     desc_len = strlen(desc);
2154     /* the descriptor offset = 0x200 */
2155     if (!split && !flat) {
2156         desc_offset = 0x200;
2157     }
2158 
2159     ret = blk_pwrite(blk, desc_offset, desc, desc_len, 0);
2160     if (ret < 0) {
2161         error_setg_errno(errp, -ret, "Could not write description");
2162         goto exit;
2163     }
2164     /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2165      * for description file */
2166     if (desc_offset == 0) {
2167         ret = blk_truncate(blk, desc_len, PREALLOC_MODE_OFF, errp);
2168         if (ret < 0) {
2169             goto exit;
2170         }
2171     }
2172     ret = 0;
2173 exit:
2174     if (blk) {
2175         blk_unref(blk);
2176     }
2177     g_free(desc);
2178     g_free(parent_desc_line);
2179     g_string_free(ext_desc_lines, true);
2180     return ret;
2181 }
2182 
2183 typedef struct {
2184     char *path;
2185     char *prefix;
2186     char *postfix;
2187     QemuOpts *opts;
2188 } VMDKCreateOptsData;
2189 
2190 static BlockBackend *vmdk_co_create_opts_cb(int64_t size, int idx,
2191                                             bool flat, bool split, bool compress,
2192                                             bool zeroed_grain, void *opaque,
2193                                             Error **errp)
2194 {
2195     BlockBackend *blk = NULL;
2196     BlockDriverState *bs = NULL;
2197     VMDKCreateOptsData *data = opaque;
2198     char *ext_filename = NULL;
2199     char *rel_filename = NULL;
2200 
2201     /* We're done, don't create excess extents. */
2202     if (size == -1) {
2203         assert(errp == NULL);
2204         return NULL;
2205     }
2206 
2207     if (idx == 0) {
2208         rel_filename = g_strdup_printf("%s%s", data->prefix, data->postfix);
2209     } else if (split) {
2210         rel_filename = g_strdup_printf("%s-%c%03d%s",
2211                                        data->prefix,
2212                                        flat ? 'f' : 's', idx, data->postfix);
2213     } else {
2214         assert(idx == 1);
2215         rel_filename = g_strdup_printf("%s-flat%s", data->prefix, data->postfix);
2216     }
2217 
2218     ext_filename = g_strdup_printf("%s%s", data->path, rel_filename);
2219     g_free(rel_filename);
2220 
2221     if (vmdk_create_extent(ext_filename, size,
2222                            flat, compress, zeroed_grain, &blk, data->opts,
2223                            errp)) {
2224         goto exit;
2225     }
2226     bdrv_unref(bs);
2227 exit:
2228     g_free(ext_filename);
2229     return blk;
2230 }
2231 
2232 static int coroutine_fn vmdk_co_create_opts(const char *filename, QemuOpts *opts,
2233                                             Error **errp)
2234 {
2235     Error *local_err = NULL;
2236     char *desc = NULL;
2237     int64_t total_size = 0;
2238     char *adapter_type = NULL;
2239     BlockdevVmdkAdapterType adapter_type_enum;
2240     char *backing_file = NULL;
2241     char *hw_version = NULL;
2242     char *fmt = NULL;
2243     BlockdevVmdkSubformat subformat;
2244     int ret = 0;
2245     char *path = g_malloc0(PATH_MAX);
2246     char *prefix = g_malloc0(PATH_MAX);
2247     char *postfix = g_malloc0(PATH_MAX);
2248     char *desc_line = g_malloc0(BUF_SIZE);
2249     char *ext_filename = g_malloc0(PATH_MAX);
2250     char *desc_filename = g_malloc0(PATH_MAX);
2251     char *parent_desc_line = g_malloc0(BUF_SIZE);
2252     bool zeroed_grain;
2253     bool compat6;
2254     VMDKCreateOptsData data;
2255 
2256     if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
2257         ret = -EINVAL;
2258         goto exit;
2259     }
2260     /* Read out options */
2261     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2262                           BDRV_SECTOR_SIZE);
2263     adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
2264     backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2265     hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION);
2266     compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false);
2267     if (strcmp(hw_version, "undefined") == 0) {
2268         g_free(hw_version);
2269         hw_version = NULL;
2270     }
2271     fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
2272     zeroed_grain = qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false);
2273 
2274     if (adapter_type) {
2275         adapter_type_enum = qapi_enum_parse(&BlockdevVmdkAdapterType_lookup,
2276                                             adapter_type,
2277                                             BLOCKDEV_VMDK_ADAPTER_TYPE_IDE,
2278                                             &local_err);
2279         if (local_err) {
2280             error_propagate(errp, local_err);
2281             ret = -EINVAL;
2282             goto exit;
2283         }
2284     } else {
2285         adapter_type_enum = BLOCKDEV_VMDK_ADAPTER_TYPE_IDE;
2286     }
2287 
2288     if (!fmt) {
2289         /* Default format to monolithicSparse */
2290         subformat = BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE;
2291     } else {
2292         subformat = qapi_enum_parse(&BlockdevVmdkSubformat_lookup,
2293                                     fmt,
2294                                     BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE,
2295                                     &local_err);
2296         if (local_err) {
2297             error_propagate(errp, local_err);
2298             ret = -EINVAL;
2299             goto exit;
2300         }
2301     }
2302     data = (VMDKCreateOptsData){
2303         .prefix = prefix,
2304         .postfix = postfix,
2305         .path = path,
2306         .opts = opts,
2307     };
2308     ret = vmdk_co_do_create(total_size, subformat, adapter_type_enum,
2309                             backing_file, hw_version, compat6, zeroed_grain,
2310                             vmdk_co_create_opts_cb, &data, errp);
2311 
2312 exit:
2313     g_free(adapter_type);
2314     g_free(backing_file);
2315     g_free(hw_version);
2316     g_free(fmt);
2317     g_free(desc);
2318     g_free(path);
2319     g_free(prefix);
2320     g_free(postfix);
2321     g_free(desc_line);
2322     g_free(ext_filename);
2323     g_free(desc_filename);
2324     g_free(parent_desc_line);
2325     return ret;
2326 }
2327 
2328 static BlockBackend *vmdk_co_create_cb(int64_t size, int idx,
2329                                        bool flat, bool split, bool compress,
2330                                        bool zeroed_grain, void *opaque,
2331                                        Error **errp)
2332 {
2333     int ret;
2334     BlockDriverState *bs;
2335     BlockBackend *blk;
2336     BlockdevCreateOptionsVmdk *opts = opaque;
2337 
2338     if (idx == 0) {
2339         bs = bdrv_open_blockdev_ref(opts->file, errp);
2340     } else {
2341         int i;
2342         BlockdevRefList *list = opts->extents;
2343         for (i = 1; i < idx; i++) {
2344             if (!list || !list->next) {
2345                 error_setg(errp, "Extent [%d] not specified", i);
2346                 return NULL;
2347             }
2348             list = list->next;
2349         }
2350         if (!list) {
2351             error_setg(errp, "Extent [%d] not specified", idx - 1);
2352             return NULL;
2353         }
2354         bs = bdrv_open_blockdev_ref(list->value, errp);
2355     }
2356     if (!bs) {
2357         return NULL;
2358     }
2359     blk = blk_new(bdrv_get_aio_context(bs),
2360                   BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
2361                   BLK_PERM_ALL);
2362     if (blk_insert_bs(blk, bs, errp)) {
2363         bdrv_unref(bs);
2364         return NULL;
2365     }
2366     blk_set_allow_write_beyond_eof(blk, true);
2367     bdrv_unref(bs);
2368 
2369     if (size != -1) {
2370         ret = vmdk_init_extent(blk, size, flat, compress, zeroed_grain, errp);
2371         if (ret) {
2372             blk_unref(blk);
2373             blk = NULL;
2374         }
2375     }
2376     return blk;
2377 }
2378 
2379 static int coroutine_fn vmdk_co_create(BlockdevCreateOptions *create_options,
2380                                        Error **errp)
2381 {
2382     int ret;
2383     BlockdevCreateOptionsVmdk *opts;
2384 
2385     opts = &create_options->u.vmdk;
2386 
2387     /* Validate options */
2388     if (!QEMU_IS_ALIGNED(opts->size, BDRV_SECTOR_SIZE)) {
2389         error_setg(errp, "Image size must be a multiple of 512 bytes");
2390         ret = -EINVAL;
2391         goto out;
2392     }
2393 
2394     ret = vmdk_co_do_create(opts->size,
2395                             opts->subformat,
2396                             opts->adapter_type,
2397                             opts->backing_file,
2398                             opts->hwversion,
2399                             false,
2400                             opts->zeroed_grain,
2401                             vmdk_co_create_cb,
2402                             opts, errp);
2403     return ret;
2404 
2405 out:
2406     return ret;
2407 }
2408 
2409 static void vmdk_close(BlockDriverState *bs)
2410 {
2411     BDRVVmdkState *s = bs->opaque;
2412 
2413     vmdk_free_extents(bs);
2414     g_free(s->create_type);
2415 
2416     migrate_del_blocker(s->migration_blocker);
2417     error_free(s->migration_blocker);
2418 }
2419 
2420 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
2421 {
2422     BDRVVmdkState *s = bs->opaque;
2423     int i, err;
2424     int ret = 0;
2425 
2426     for (i = 0; i < s->num_extents; i++) {
2427         err = bdrv_co_flush(s->extents[i].file->bs);
2428         if (err < 0) {
2429             ret = err;
2430         }
2431     }
2432     return ret;
2433 }
2434 
2435 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
2436 {
2437     int i;
2438     int64_t ret = 0;
2439     int64_t r;
2440     BDRVVmdkState *s = bs->opaque;
2441 
2442     ret = bdrv_get_allocated_file_size(bs->file->bs);
2443     if (ret < 0) {
2444         return ret;
2445     }
2446     for (i = 0; i < s->num_extents; i++) {
2447         if (s->extents[i].file == bs->file) {
2448             continue;
2449         }
2450         r = bdrv_get_allocated_file_size(s->extents[i].file->bs);
2451         if (r < 0) {
2452             return r;
2453         }
2454         ret += r;
2455     }
2456     return ret;
2457 }
2458 
2459 static int vmdk_has_zero_init(BlockDriverState *bs)
2460 {
2461     int i;
2462     BDRVVmdkState *s = bs->opaque;
2463 
2464     /* If has a flat extent and its underlying storage doesn't have zero init,
2465      * return 0. */
2466     for (i = 0; i < s->num_extents; i++) {
2467         if (s->extents[i].flat) {
2468             if (!bdrv_has_zero_init(s->extents[i].file->bs)) {
2469                 return 0;
2470             }
2471         }
2472     }
2473     return 1;
2474 }
2475 
2476 static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
2477 {
2478     ImageInfo *info = g_new0(ImageInfo, 1);
2479 
2480     bdrv_refresh_filename(extent->file->bs);
2481     *info = (ImageInfo){
2482         .filename         = g_strdup(extent->file->bs->filename),
2483         .format           = g_strdup(extent->type),
2484         .virtual_size     = extent->sectors * BDRV_SECTOR_SIZE,
2485         .compressed       = extent->compressed,
2486         .has_compressed   = extent->compressed,
2487         .cluster_size     = extent->cluster_sectors * BDRV_SECTOR_SIZE,
2488         .has_cluster_size = !extent->flat,
2489     };
2490 
2491     return info;
2492 }
2493 
2494 static int coroutine_fn vmdk_co_check(BlockDriverState *bs,
2495                                       BdrvCheckResult *result,
2496                                       BdrvCheckMode fix)
2497 {
2498     BDRVVmdkState *s = bs->opaque;
2499     VmdkExtent *extent = NULL;
2500     int64_t sector_num = 0;
2501     int64_t total_sectors = bdrv_nb_sectors(bs);
2502     int ret;
2503     uint64_t cluster_offset;
2504 
2505     if (fix) {
2506         return -ENOTSUP;
2507     }
2508 
2509     for (;;) {
2510         if (sector_num >= total_sectors) {
2511             return 0;
2512         }
2513         extent = find_extent(s, sector_num, extent);
2514         if (!extent) {
2515             fprintf(stderr,
2516                     "ERROR: could not find extent for sector %" PRId64 "\n",
2517                     sector_num);
2518             ret = -EINVAL;
2519             break;
2520         }
2521         ret = get_cluster_offset(bs, extent, NULL,
2522                                  sector_num << BDRV_SECTOR_BITS,
2523                                  false, &cluster_offset, 0, 0);
2524         if (ret == VMDK_ERROR) {
2525             fprintf(stderr,
2526                     "ERROR: could not get cluster_offset for sector %"
2527                     PRId64 "\n", sector_num);
2528             break;
2529         }
2530         if (ret == VMDK_OK) {
2531             int64_t extent_len = bdrv_getlength(extent->file->bs);
2532             if (extent_len < 0) {
2533                 fprintf(stderr,
2534                         "ERROR: could not get extent file length for sector %"
2535                         PRId64 "\n", sector_num);
2536                 ret = extent_len;
2537                 break;
2538             }
2539             if (cluster_offset >= extent_len) {
2540                 fprintf(stderr,
2541                         "ERROR: cluster offset for sector %"
2542                         PRId64 " points after EOF\n", sector_num);
2543                 ret = -EINVAL;
2544                 break;
2545             }
2546         }
2547         sector_num += extent->cluster_sectors;
2548     }
2549 
2550     result->corruptions++;
2551     return ret;
2552 }
2553 
2554 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
2555                                                  Error **errp)
2556 {
2557     int i;
2558     BDRVVmdkState *s = bs->opaque;
2559     ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
2560     ImageInfoList **next;
2561 
2562     *spec_info = (ImageInfoSpecific){
2563         .type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
2564         .u = {
2565             .vmdk.data = g_new0(ImageInfoSpecificVmdk, 1),
2566         },
2567     };
2568 
2569     *spec_info->u.vmdk.data = (ImageInfoSpecificVmdk) {
2570         .create_type = g_strdup(s->create_type),
2571         .cid = s->cid,
2572         .parent_cid = s->parent_cid,
2573     };
2574 
2575     next = &spec_info->u.vmdk.data->extents;
2576     for (i = 0; i < s->num_extents; i++) {
2577         *next = g_new0(ImageInfoList, 1);
2578         (*next)->value = vmdk_get_extent_info(&s->extents[i]);
2579         (*next)->next = NULL;
2580         next = &(*next)->next;
2581     }
2582 
2583     return spec_info;
2584 }
2585 
2586 static bool vmdk_extents_type_eq(const VmdkExtent *a, const VmdkExtent *b)
2587 {
2588     return a->flat == b->flat &&
2589            a->compressed == b->compressed &&
2590            (a->flat || a->cluster_sectors == b->cluster_sectors);
2591 }
2592 
2593 static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2594 {
2595     int i;
2596     BDRVVmdkState *s = bs->opaque;
2597     assert(s->num_extents);
2598 
2599     /* See if we have multiple extents but they have different cases */
2600     for (i = 1; i < s->num_extents; i++) {
2601         if (!vmdk_extents_type_eq(&s->extents[0], &s->extents[i])) {
2602             return -ENOTSUP;
2603         }
2604     }
2605     bdi->needs_compressed_writes = s->extents[0].compressed;
2606     if (!s->extents[0].flat) {
2607         bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS;
2608     }
2609     return 0;
2610 }
2611 
2612 static void vmdk_gather_child_options(BlockDriverState *bs, QDict *target,
2613                                       bool backing_overridden)
2614 {
2615     /* No children but file and backing can be explicitly specified (TODO) */
2616     qdict_put(target, "file",
2617               qobject_ref(bs->file->bs->full_open_options));
2618 
2619     if (backing_overridden) {
2620         if (bs->backing) {
2621             qdict_put(target, "backing",
2622                       qobject_ref(bs->backing->bs->full_open_options));
2623         } else {
2624             qdict_put_null(target, "backing");
2625         }
2626     }
2627 }
2628 
2629 static QemuOptsList vmdk_create_opts = {
2630     .name = "vmdk-create-opts",
2631     .head = QTAILQ_HEAD_INITIALIZER(vmdk_create_opts.head),
2632     .desc = {
2633         {
2634             .name = BLOCK_OPT_SIZE,
2635             .type = QEMU_OPT_SIZE,
2636             .help = "Virtual disk size"
2637         },
2638         {
2639             .name = BLOCK_OPT_ADAPTER_TYPE,
2640             .type = QEMU_OPT_STRING,
2641             .help = "Virtual adapter type, can be one of "
2642                     "ide (default), lsilogic, buslogic or legacyESX"
2643         },
2644         {
2645             .name = BLOCK_OPT_BACKING_FILE,
2646             .type = QEMU_OPT_STRING,
2647             .help = "File name of a base image"
2648         },
2649         {
2650             .name = BLOCK_OPT_COMPAT6,
2651             .type = QEMU_OPT_BOOL,
2652             .help = "VMDK version 6 image",
2653             .def_value_str = "off"
2654         },
2655         {
2656             .name = BLOCK_OPT_HWVERSION,
2657             .type = QEMU_OPT_STRING,
2658             .help = "VMDK hardware version",
2659             .def_value_str = "undefined"
2660         },
2661         {
2662             .name = BLOCK_OPT_SUBFMT,
2663             .type = QEMU_OPT_STRING,
2664             .help =
2665                 "VMDK flat extent format, can be one of "
2666                 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2667         },
2668         {
2669             .name = BLOCK_OPT_ZEROED_GRAIN,
2670             .type = QEMU_OPT_BOOL,
2671             .help = "Enable efficient zero writes "
2672                     "using the zeroed-grain GTE feature"
2673         },
2674         { /* end of list */ }
2675     }
2676 };
2677 
2678 static BlockDriver bdrv_vmdk = {
2679     .format_name                  = "vmdk",
2680     .instance_size                = sizeof(BDRVVmdkState),
2681     .bdrv_probe                   = vmdk_probe,
2682     .bdrv_open                    = vmdk_open,
2683     .bdrv_co_check                = vmdk_co_check,
2684     .bdrv_reopen_prepare          = vmdk_reopen_prepare,
2685     .bdrv_child_perm              = bdrv_format_default_perms,
2686     .bdrv_co_preadv               = vmdk_co_preadv,
2687     .bdrv_co_pwritev              = vmdk_co_pwritev,
2688     .bdrv_co_pwritev_compressed   = vmdk_co_pwritev_compressed,
2689     .bdrv_co_pwrite_zeroes        = vmdk_co_pwrite_zeroes,
2690     .bdrv_close                   = vmdk_close,
2691     .bdrv_co_create_opts          = vmdk_co_create_opts,
2692     .bdrv_co_create               = vmdk_co_create,
2693     .bdrv_co_flush_to_disk        = vmdk_co_flush,
2694     .bdrv_co_block_status         = vmdk_co_block_status,
2695     .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2696     .bdrv_has_zero_init           = vmdk_has_zero_init,
2697     .bdrv_get_specific_info       = vmdk_get_specific_info,
2698     .bdrv_refresh_limits          = vmdk_refresh_limits,
2699     .bdrv_get_info                = vmdk_get_info,
2700     .bdrv_gather_child_options    = vmdk_gather_child_options,
2701 
2702     .supports_backing             = true,
2703     .create_opts                  = &vmdk_create_opts,
2704 };
2705 
2706 static void bdrv_vmdk_init(void)
2707 {
2708     bdrv_register(&bdrv_vmdk);
2709 }
2710 
2711 block_init(bdrv_vmdk_init);
2712