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