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