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