]> Git Repo - qemu.git/blob - block/vmdk.c
vmdk: Fix big flat extent IO
[qemu.git] / block / vmdk.c
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-common.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "migration/migration.h"
30 #include <zlib.h>
31
32 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
33 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
34 #define VMDK4_COMPRESSION_DEFLATE 1
35 #define VMDK4_FLAG_NL_DETECT (1 << 0)
36 #define VMDK4_FLAG_RGD (1 << 1)
37 /* Zeroed-grain enable bit */
38 #define VMDK4_FLAG_ZERO_GRAIN   (1 << 2)
39 #define VMDK4_FLAG_COMPRESS (1 << 16)
40 #define VMDK4_FLAG_MARKER (1 << 17)
41 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
42
43 #define VMDK_GTE_ZEROED 0x1
44
45 /* VMDK internal error codes */
46 #define VMDK_OK      0
47 #define VMDK_ERROR   (-1)
48 /* Cluster not allocated */
49 #define VMDK_UNALLOC (-2)
50 #define VMDK_ZEROED  (-3)
51
52 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
53
54 typedef struct {
55     uint32_t version;
56     uint32_t flags;
57     uint32_t disk_sectors;
58     uint32_t granularity;
59     uint32_t l1dir_offset;
60     uint32_t l1dir_size;
61     uint32_t file_sectors;
62     uint32_t cylinders;
63     uint32_t heads;
64     uint32_t sectors_per_track;
65 } QEMU_PACKED VMDK3Header;
66
67 typedef struct {
68     uint32_t version;
69     uint32_t flags;
70     uint64_t capacity;
71     uint64_t granularity;
72     uint64_t desc_offset;
73     uint64_t desc_size;
74     /* Number of GrainTableEntries per GrainTable */
75     uint32_t num_gtes_per_gt;
76     uint64_t rgd_offset;
77     uint64_t gd_offset;
78     uint64_t grain_offset;
79     char filler[1];
80     char check_bytes[4];
81     uint16_t compressAlgorithm;
82 } QEMU_PACKED VMDK4Header;
83
84 #define L2_CACHE_SIZE 16
85
86 typedef struct VmdkExtent {
87     BlockDriverState *file;
88     bool flat;
89     bool compressed;
90     bool has_marker;
91     bool has_zero_grain;
92     int version;
93     int64_t sectors;
94     int64_t end_sector;
95     int64_t flat_start_offset;
96     int64_t l1_table_offset;
97     int64_t l1_backup_table_offset;
98     uint32_t *l1_table;
99     uint32_t *l1_backup_table;
100     unsigned int l1_size;
101     uint32_t l1_entry_sectors;
102
103     unsigned int l2_size;
104     uint32_t *l2_cache;
105     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
106     uint32_t l2_cache_counts[L2_CACHE_SIZE];
107
108     int64_t cluster_sectors;
109     char *type;
110 } VmdkExtent;
111
112 typedef struct BDRVVmdkState {
113     CoMutex lock;
114     uint64_t desc_offset;
115     bool cid_updated;
116     bool cid_checked;
117     uint32_t cid;
118     uint32_t parent_cid;
119     int num_extents;
120     /* Extent array with num_extents entries, ascend ordered by address */
121     VmdkExtent *extents;
122     Error *migration_blocker;
123     char *create_type;
124 } BDRVVmdkState;
125
126 typedef struct VmdkMetaData {
127     uint32_t offset;
128     unsigned int l1_index;
129     unsigned int l2_index;
130     unsigned int l2_offset;
131     int valid;
132     uint32_t *l2_cache_entry;
133 } VmdkMetaData;
134
135 typedef struct VmdkGrainMarker {
136     uint64_t lba;
137     uint32_t size;
138     uint8_t  data[0];
139 } QEMU_PACKED VmdkGrainMarker;
140
141 enum {
142     MARKER_END_OF_STREAM    = 0,
143     MARKER_GRAIN_TABLE      = 1,
144     MARKER_GRAIN_DIRECTORY  = 2,
145     MARKER_FOOTER           = 3,
146 };
147
148 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
149 {
150     uint32_t magic;
151
152     if (buf_size < 4) {
153         return 0;
154     }
155     magic = be32_to_cpu(*(uint32_t *)buf);
156     if (magic == VMDK3_MAGIC ||
157         magic == VMDK4_MAGIC) {
158         return 100;
159     } else {
160         const char *p = (const char *)buf;
161         const char *end = p + buf_size;
162         while (p < end) {
163             if (*p == '#') {
164                 /* skip comment line */
165                 while (p < end && *p != '\n') {
166                     p++;
167                 }
168                 p++;
169                 continue;
170             }
171             if (*p == ' ') {
172                 while (p < end && *p == ' ') {
173                     p++;
174                 }
175                 /* skip '\r' if windows line endings used. */
176                 if (p < end && *p == '\r') {
177                     p++;
178                 }
179                 /* only accept blank lines before 'version=' line */
180                 if (p == end || *p != '\n') {
181                     return 0;
182                 }
183                 p++;
184                 continue;
185             }
186             if (end - p >= strlen("version=X\n")) {
187                 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
188                     strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
189                     return 100;
190                 }
191             }
192             if (end - p >= strlen("version=X\r\n")) {
193                 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
194                     strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
195                     return 100;
196                 }
197             }
198             return 0;
199         }
200         return 0;
201     }
202 }
203
204 #define SECTOR_SIZE 512
205 #define DESC_SIZE (20 * SECTOR_SIZE)    /* 20 sectors of 512 bytes each */
206 #define BUF_SIZE 4096
207 #define HEADER_SIZE 512                 /* first sector of 512 bytes */
208
209 static void vmdk_free_extents(BlockDriverState *bs)
210 {
211     int i;
212     BDRVVmdkState *s = bs->opaque;
213     VmdkExtent *e;
214
215     for (i = 0; i < s->num_extents; i++) {
216         e = &s->extents[i];
217         g_free(e->l1_table);
218         g_free(e->l2_cache);
219         g_free(e->l1_backup_table);
220         g_free(e->type);
221         if (e->file != bs->file) {
222             bdrv_unref(e->file);
223         }
224     }
225     g_free(s->extents);
226 }
227
228 static void vmdk_free_last_extent(BlockDriverState *bs)
229 {
230     BDRVVmdkState *s = bs->opaque;
231
232     if (s->num_extents == 0) {
233         return;
234     }
235     s->num_extents--;
236     s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
237 }
238
239 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
240 {
241     char desc[DESC_SIZE];
242     uint32_t cid = 0xffffffff;
243     const char *p_name, *cid_str;
244     size_t cid_str_size;
245     BDRVVmdkState *s = bs->opaque;
246     int ret;
247
248     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
249     if (ret < 0) {
250         return 0;
251     }
252
253     if (parent) {
254         cid_str = "parentCID";
255         cid_str_size = sizeof("parentCID");
256     } else {
257         cid_str = "CID";
258         cid_str_size = sizeof("CID");
259     }
260
261     desc[DESC_SIZE - 1] = '\0';
262     p_name = strstr(desc, cid_str);
263     if (p_name != NULL) {
264         p_name += cid_str_size;
265         sscanf(p_name, "%x", &cid);
266     }
267
268     return cid;
269 }
270
271 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
272 {
273     char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
274     char *p_name, *tmp_str;
275     BDRVVmdkState *s = bs->opaque;
276     int ret;
277
278     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
279     if (ret < 0) {
280         return ret;
281     }
282
283     desc[DESC_SIZE - 1] = '\0';
284     tmp_str = strstr(desc, "parentCID");
285     if (tmp_str == NULL) {
286         return -EINVAL;
287     }
288
289     pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
290     p_name = strstr(desc, "CID");
291     if (p_name != NULL) {
292         p_name += sizeof("CID");
293         snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
294         pstrcat(desc, sizeof(desc), tmp_desc);
295     }
296
297     ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
298     if (ret < 0) {
299         return ret;
300     }
301
302     return 0;
303 }
304
305 static int vmdk_is_cid_valid(BlockDriverState *bs)
306 {
307     BDRVVmdkState *s = bs->opaque;
308     BlockDriverState *p_bs = bs->backing_hd;
309     uint32_t cur_pcid;
310
311     if (!s->cid_checked && p_bs) {
312         cur_pcid = vmdk_read_cid(p_bs, 0);
313         if (s->parent_cid != cur_pcid) {
314             /* CID not valid */
315             return 0;
316         }
317     }
318     s->cid_checked = true;
319     /* CID valid */
320     return 1;
321 }
322
323 /* Queue extents, if any, for reopen() */
324 static int vmdk_reopen_prepare(BDRVReopenState *state,
325                                BlockReopenQueue *queue, Error **errp)
326 {
327     BDRVVmdkState *s;
328     int ret = -1;
329     int i;
330     VmdkExtent *e;
331
332     assert(state != NULL);
333     assert(state->bs != NULL);
334
335     if (queue == NULL) {
336         error_setg(errp, "No reopen queue for VMDK extents");
337         goto exit;
338     }
339
340     s = state->bs->opaque;
341
342     assert(s != NULL);
343
344     for (i = 0; i < s->num_extents; i++) {
345         e = &s->extents[i];
346         if (e->file != state->bs->file) {
347             bdrv_reopen_queue(queue, e->file, state->flags);
348         }
349     }
350     ret = 0;
351
352 exit:
353     return ret;
354 }
355
356 static int vmdk_parent_open(BlockDriverState *bs)
357 {
358     char *p_name;
359     char desc[DESC_SIZE + 1];
360     BDRVVmdkState *s = bs->opaque;
361     int ret;
362
363     desc[DESC_SIZE] = '\0';
364     ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
365     if (ret < 0) {
366         return ret;
367     }
368
369     p_name = strstr(desc, "parentFileNameHint");
370     if (p_name != NULL) {
371         char *end_name;
372
373         p_name += sizeof("parentFileNameHint") + 1;
374         end_name = strchr(p_name, '\"');
375         if (end_name == NULL) {
376             return -EINVAL;
377         }
378         if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
379             return -EINVAL;
380         }
381
382         pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
383     }
384
385     return 0;
386 }
387
388 /* Create and append extent to the extent array. Return the added VmdkExtent
389  * address. return NULL if allocation failed. */
390 static int vmdk_add_extent(BlockDriverState *bs,
391                            BlockDriverState *file, bool flat, int64_t sectors,
392                            int64_t l1_offset, int64_t l1_backup_offset,
393                            uint32_t l1_size,
394                            int l2_size, uint64_t cluster_sectors,
395                            VmdkExtent **new_extent,
396                            Error **errp)
397 {
398     VmdkExtent *extent;
399     BDRVVmdkState *s = bs->opaque;
400
401     if (cluster_sectors > 0x200000) {
402         /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
403         error_setg(errp, "Invalid granularity, image may be corrupt");
404         return -EFBIG;
405     }
406     if (l1_size > 512 * 1024 * 1024) {
407         /* Although with big capacity and small l1_entry_sectors, we can get a
408          * big l1_size, we don't want unbounded value to allocate the table.
409          * Limit it to 512M, which is 16PB for default cluster and L2 table
410          * size */
411         error_setg(errp, "L1 size too big");
412         return -EFBIG;
413     }
414
415     s->extents = g_realloc(s->extents,
416                               (s->num_extents + 1) * sizeof(VmdkExtent));
417     extent = &s->extents[s->num_extents];
418     s->num_extents++;
419
420     memset(extent, 0, sizeof(VmdkExtent));
421     extent->file = file;
422     extent->flat = flat;
423     extent->sectors = sectors;
424     extent->l1_table_offset = l1_offset;
425     extent->l1_backup_table_offset = l1_backup_offset;
426     extent->l1_size = l1_size;
427     extent->l1_entry_sectors = l2_size * cluster_sectors;
428     extent->l2_size = l2_size;
429     extent->cluster_sectors = flat ? sectors : cluster_sectors;
430
431     if (!flat) {
432         bs->bl.write_zeroes_alignment =
433             MAX(bs->bl.write_zeroes_alignment, cluster_sectors);
434     }
435     if (s->num_extents > 1) {
436         extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
437     } else {
438         extent->end_sector = extent->sectors;
439     }
440     bs->total_sectors = extent->end_sector;
441     if (new_extent) {
442         *new_extent = extent;
443     }
444     return 0;
445 }
446
447 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
448                             Error **errp)
449 {
450     int ret;
451     int l1_size, i;
452
453     /* read the L1 table */
454     l1_size = extent->l1_size * sizeof(uint32_t);
455     extent->l1_table = g_malloc(l1_size);
456     ret = bdrv_pread(extent->file,
457                      extent->l1_table_offset,
458                      extent->l1_table,
459                      l1_size);
460     if (ret < 0) {
461         error_setg_errno(errp, -ret,
462                          "Could not read l1 table from extent '%s'",
463                          extent->file->filename);
464         goto fail_l1;
465     }
466     for (i = 0; i < extent->l1_size; i++) {
467         le32_to_cpus(&extent->l1_table[i]);
468     }
469
470     if (extent->l1_backup_table_offset) {
471         extent->l1_backup_table = g_malloc(l1_size);
472         ret = bdrv_pread(extent->file,
473                          extent->l1_backup_table_offset,
474                          extent->l1_backup_table,
475                          l1_size);
476         if (ret < 0) {
477             error_setg_errno(errp, -ret,
478                              "Could not read l1 backup table from extent '%s'",
479                              extent->file->filename);
480             goto fail_l1b;
481         }
482         for (i = 0; i < extent->l1_size; i++) {
483             le32_to_cpus(&extent->l1_backup_table[i]);
484         }
485     }
486
487     extent->l2_cache =
488         g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
489     return 0;
490  fail_l1b:
491     g_free(extent->l1_backup_table);
492  fail_l1:
493     g_free(extent->l1_table);
494     return ret;
495 }
496
497 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
498                                  BlockDriverState *file,
499                                  int flags, Error **errp)
500 {
501     int ret;
502     uint32_t magic;
503     VMDK3Header header;
504     VmdkExtent *extent;
505
506     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
507     if (ret < 0) {
508         error_setg_errno(errp, -ret,
509                          "Could not read header from file '%s'",
510                          file->filename);
511         return ret;
512     }
513     ret = vmdk_add_extent(bs, file, false,
514                           le32_to_cpu(header.disk_sectors),
515                           le32_to_cpu(header.l1dir_offset) << 9,
516                           0,
517                           le32_to_cpu(header.l1dir_size),
518                           4096,
519                           le32_to_cpu(header.granularity),
520                           &extent,
521                           errp);
522     if (ret < 0) {
523         return ret;
524     }
525     ret = vmdk_init_tables(bs, extent, errp);
526     if (ret) {
527         /* free extent allocated by vmdk_add_extent */
528         vmdk_free_last_extent(bs);
529     }
530     return ret;
531 }
532
533 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
534                                uint64_t desc_offset, Error **errp);
535
536 static int vmdk_open_vmdk4(BlockDriverState *bs,
537                            BlockDriverState *file,
538                            int flags, Error **errp)
539 {
540     int ret;
541     uint32_t magic;
542     uint32_t l1_size, l1_entry_sectors;
543     VMDK4Header header;
544     VmdkExtent *extent;
545     BDRVVmdkState *s = bs->opaque;
546     int64_t l1_backup_offset = 0;
547
548     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
549     if (ret < 0) {
550         error_setg_errno(errp, -ret,
551                          "Could not read header from file '%s'",
552                          file->filename);
553     }
554     if (header.capacity == 0) {
555         uint64_t desc_offset = le64_to_cpu(header.desc_offset);
556         if (desc_offset) {
557             return vmdk_open_desc_file(bs, flags, desc_offset << 9, errp);
558         }
559     }
560
561     if (!s->create_type) {
562         s->create_type = g_strdup("monolithicSparse");
563     }
564
565     if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
566         /*
567          * The footer takes precedence over the header, so read it in. The
568          * footer starts at offset -1024 from the end: One sector for the
569          * footer, and another one for the end-of-stream marker.
570          */
571         struct {
572             struct {
573                 uint64_t val;
574                 uint32_t size;
575                 uint32_t type;
576                 uint8_t pad[512 - 16];
577             } QEMU_PACKED footer_marker;
578
579             uint32_t magic;
580             VMDK4Header header;
581             uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
582
583             struct {
584                 uint64_t val;
585                 uint32_t size;
586                 uint32_t type;
587                 uint8_t pad[512 - 16];
588             } QEMU_PACKED eos_marker;
589         } QEMU_PACKED footer;
590
591         ret = bdrv_pread(file,
592             bs->file->total_sectors * 512 - 1536,
593             &footer, sizeof(footer));
594         if (ret < 0) {
595             return ret;
596         }
597
598         /* Some sanity checks for the footer */
599         if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
600             le32_to_cpu(footer.footer_marker.size) != 0  ||
601             le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
602             le64_to_cpu(footer.eos_marker.val) != 0  ||
603             le32_to_cpu(footer.eos_marker.size) != 0  ||
604             le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
605         {
606             return -EINVAL;
607         }
608
609         header = footer.header;
610     }
611
612     if (le32_to_cpu(header.version) > 3) {
613         char buf[64];
614         snprintf(buf, sizeof(buf), "VMDK version %d",
615                  le32_to_cpu(header.version));
616         qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
617                 bs->device_name, "vmdk", buf);
618         return -ENOTSUP;
619     } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
620         /* VMware KB 2064959 explains that version 3 added support for
621          * persistent changed block tracking (CBT), and backup software can
622          * read it as version=1 if it doesn't care about the changed area
623          * information. So we are safe to enable read only. */
624         error_setg(errp, "VMDK version 3 must be read only");
625         return -EINVAL;
626     }
627
628     if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
629         error_report("L2 table size too big");
630         return -EINVAL;
631     }
632
633     l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
634                         * le64_to_cpu(header.granularity);
635     if (l1_entry_sectors == 0) {
636         return -EINVAL;
637     }
638     l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
639                 / l1_entry_sectors;
640     if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
641         l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
642     }
643     ret = vmdk_add_extent(bs, file, false,
644                           le64_to_cpu(header.capacity),
645                           le64_to_cpu(header.gd_offset) << 9,
646                           l1_backup_offset,
647                           l1_size,
648                           le32_to_cpu(header.num_gtes_per_gt),
649                           le64_to_cpu(header.granularity),
650                           &extent,
651                           errp);
652     if (ret < 0) {
653         return ret;
654     }
655     extent->compressed =
656         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
657     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
658     extent->version = le32_to_cpu(header.version);
659     extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
660     ret = vmdk_init_tables(bs, extent, errp);
661     if (ret) {
662         /* free extent allocated by vmdk_add_extent */
663         vmdk_free_last_extent(bs);
664     }
665     return ret;
666 }
667
668 /* find an option value out of descriptor file */
669 static int vmdk_parse_description(const char *desc, const char *opt_name,
670         char *buf, int buf_size)
671 {
672     char *opt_pos, *opt_end;
673     const char *end = desc + strlen(desc);
674
675     opt_pos = strstr(desc, opt_name);
676     if (!opt_pos) {
677         return VMDK_ERROR;
678     }
679     /* Skip "=\"" following opt_name */
680     opt_pos += strlen(opt_name) + 2;
681     if (opt_pos >= end) {
682         return VMDK_ERROR;
683     }
684     opt_end = opt_pos;
685     while (opt_end < end && *opt_end != '"') {
686         opt_end++;
687     }
688     if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
689         return VMDK_ERROR;
690     }
691     pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
692     return VMDK_OK;
693 }
694
695 /* Open an extent file and append to bs array */
696 static int vmdk_open_sparse(BlockDriverState *bs,
697                             BlockDriverState *file,
698                             int flags, Error **errp)
699 {
700     uint32_t magic;
701
702     if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
703         return -EIO;
704     }
705
706     magic = be32_to_cpu(magic);
707     switch (magic) {
708         case VMDK3_MAGIC:
709             return vmdk_open_vmfs_sparse(bs, file, flags, errp);
710             break;
711         case VMDK4_MAGIC:
712             return vmdk_open_vmdk4(bs, file, flags, errp);
713             break;
714         default:
715             return -EMEDIUMTYPE;
716             break;
717     }
718 }
719
720 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
721                               const char *desc_file_path, Error **errp)
722 {
723     int ret;
724     char access[11];
725     char type[11];
726     char fname[512];
727     const char *p = desc;
728     int64_t sectors = 0;
729     int64_t flat_offset;
730     char extent_path[PATH_MAX];
731     BlockDriverState *extent_file;
732     BDRVVmdkState *s = bs->opaque;
733     VmdkExtent *extent;
734
735     while (*p) {
736         /* parse extent line:
737          * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
738          * or
739          * RW [size in sectors] SPARSE "file-name.vmdk"
740          */
741         flat_offset = -1;
742         ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
743                 access, &sectors, type, fname, &flat_offset);
744         if (ret < 4 || strcmp(access, "RW")) {
745             goto next_line;
746         } else if (!strcmp(type, "FLAT")) {
747             if (ret != 5 || flat_offset < 0) {
748                 error_setg(errp, "Invalid extent lines: \n%s", p);
749                 return -EINVAL;
750             }
751         } else if (!strcmp(type, "VMFS")) {
752             if (ret == 4) {
753                 flat_offset = 0;
754             } else {
755                 error_setg(errp, "Invalid extent lines:\n%s", p);
756                 return -EINVAL;
757             }
758         } else if (ret != 4) {
759             error_setg(errp, "Invalid extent lines:\n%s", p);
760             return -EINVAL;
761         }
762
763         if (sectors <= 0 ||
764             (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
765              strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
766             (strcmp(access, "RW"))) {
767             goto next_line;
768         }
769
770         path_combine(extent_path, sizeof(extent_path),
771                 desc_file_path, fname);
772         ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
773                              errp);
774         if (ret) {
775             return ret;
776         }
777
778         /* save to extents array */
779         if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
780             /* FLAT extent */
781
782             ret = vmdk_add_extent(bs, extent_file, true, sectors,
783                             0, 0, 0, 0, 0, &extent, errp);
784             if (ret < 0) {
785                 return ret;
786             }
787             extent->flat_start_offset = flat_offset << 9;
788         } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
789             /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
790             ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, errp);
791             if (ret) {
792                 bdrv_unref(extent_file);
793                 return ret;
794             }
795             extent = &s->extents[s->num_extents - 1];
796         } else {
797             error_setg(errp, "Unsupported extent type '%s'", type);
798             return -ENOTSUP;
799         }
800         extent->type = g_strdup(type);
801 next_line:
802         /* move to next line */
803         while (*p) {
804             if (*p == '\n') {
805                 p++;
806                 break;
807             }
808             p++;
809         }
810     }
811     return 0;
812 }
813
814 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
815                                uint64_t desc_offset, Error **errp)
816 {
817     int ret;
818     char *buf = NULL;
819     char ct[128];
820     BDRVVmdkState *s = bs->opaque;
821     int64_t size;
822
823     size = bdrv_getlength(bs->file);
824     if (size < 0) {
825         return -EINVAL;
826     }
827
828     size = MIN(size, 1 << 20);  /* avoid unbounded allocation */
829     buf = g_malloc0(size + 1);
830
831     ret = bdrv_pread(bs->file, desc_offset, buf, size);
832     if (ret < 0) {
833         goto exit;
834     }
835     if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
836         ret = -EMEDIUMTYPE;
837         goto exit;
838     }
839     if (strcmp(ct, "monolithicFlat") &&
840         strcmp(ct, "vmfs") &&
841         strcmp(ct, "vmfsSparse") &&
842         strcmp(ct, "twoGbMaxExtentSparse") &&
843         strcmp(ct, "twoGbMaxExtentFlat")) {
844         error_setg(errp, "Unsupported image type '%s'", ct);
845         ret = -ENOTSUP;
846         goto exit;
847     }
848     s->create_type = g_strdup(ct);
849     s->desc_offset = 0;
850     ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
851 exit:
852     g_free(buf);
853     return ret;
854 }
855
856 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
857                      Error **errp)
858 {
859     int ret;
860     BDRVVmdkState *s = bs->opaque;
861
862     if (vmdk_open_sparse(bs, bs->file, flags, errp) == 0) {
863         s->desc_offset = 0x200;
864     } else {
865         ret = vmdk_open_desc_file(bs, flags, 0, errp);
866         if (ret) {
867             goto fail;
868         }
869     }
870     /* try to open parent images, if exist */
871     ret = vmdk_parent_open(bs);
872     if (ret) {
873         goto fail;
874     }
875     s->cid = vmdk_read_cid(bs, 0);
876     s->parent_cid = vmdk_read_cid(bs, 1);
877     qemu_co_mutex_init(&s->lock);
878
879     /* Disable migration when VMDK images are used */
880     error_set(&s->migration_blocker,
881               QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
882               "vmdk", bs->device_name, "live migration");
883     migrate_add_blocker(s->migration_blocker);
884
885     return 0;
886
887 fail:
888     g_free(s->create_type);
889     s->create_type = NULL;
890     vmdk_free_extents(bs);
891     return ret;
892 }
893
894 static int get_whole_cluster(BlockDriverState *bs,
895                 VmdkExtent *extent,
896                 uint64_t cluster_offset,
897                 uint64_t offset,
898                 bool allocate)
899 {
900     int ret = VMDK_OK;
901     uint8_t *whole_grain = NULL;
902
903     /* we will be here if it's first write on non-exist grain(cluster).
904      * try to read from parent image, if exist */
905     if (bs->backing_hd) {
906         whole_grain =
907             qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS);
908         if (!vmdk_is_cid_valid(bs)) {
909             ret = VMDK_ERROR;
910             goto exit;
911         }
912
913         /* floor offset to cluster */
914         offset -= offset % (extent->cluster_sectors * 512);
915         ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
916                 extent->cluster_sectors);
917         if (ret < 0) {
918             ret = VMDK_ERROR;
919             goto exit;
920         }
921
922         /* Write grain only into the active image */
923         ret = bdrv_write(extent->file, cluster_offset, whole_grain,
924                 extent->cluster_sectors);
925         if (ret < 0) {
926             ret = VMDK_ERROR;
927             goto exit;
928         }
929     }
930 exit:
931     qemu_vfree(whole_grain);
932     return ret;
933 }
934
935 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
936 {
937     uint32_t offset;
938     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
939     offset = cpu_to_le32(m_data->offset);
940     /* update L2 table */
941     if (bdrv_pwrite_sync(
942                 extent->file,
943                 ((int64_t)m_data->l2_offset * 512)
944                     + (m_data->l2_index * sizeof(m_data->offset)),
945                 &offset, sizeof(offset)) < 0) {
946         return VMDK_ERROR;
947     }
948     /* update backup L2 table */
949     if (extent->l1_backup_table_offset != 0) {
950         m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
951         if (bdrv_pwrite_sync(
952                     extent->file,
953                     ((int64_t)m_data->l2_offset * 512)
954                         + (m_data->l2_index * sizeof(m_data->offset)),
955                     &offset, sizeof(offset)) < 0) {
956             return VMDK_ERROR;
957         }
958     }
959     if (m_data->l2_cache_entry) {
960         *m_data->l2_cache_entry = offset;
961     }
962
963     return VMDK_OK;
964 }
965
966 static int get_cluster_offset(BlockDriverState *bs,
967                                     VmdkExtent *extent,
968                                     VmdkMetaData *m_data,
969                                     uint64_t offset,
970                                     int allocate,
971                                     uint64_t *cluster_offset)
972 {
973     unsigned int l1_index, l2_offset, l2_index;
974     int min_index, i, j;
975     uint32_t min_count, *l2_table;
976     bool zeroed = false;
977
978     if (m_data) {
979         m_data->valid = 0;
980     }
981     if (extent->flat) {
982         *cluster_offset = extent->flat_start_offset;
983         return VMDK_OK;
984     }
985
986     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
987     l1_index = (offset >> 9) / extent->l1_entry_sectors;
988     if (l1_index >= extent->l1_size) {
989         return VMDK_ERROR;
990     }
991     l2_offset = extent->l1_table[l1_index];
992     if (!l2_offset) {
993         return VMDK_UNALLOC;
994     }
995     for (i = 0; i < L2_CACHE_SIZE; i++) {
996         if (l2_offset == extent->l2_cache_offsets[i]) {
997             /* increment the hit count */
998             if (++extent->l2_cache_counts[i] == 0xffffffff) {
999                 for (j = 0; j < L2_CACHE_SIZE; j++) {
1000                     extent->l2_cache_counts[j] >>= 1;
1001                 }
1002             }
1003             l2_table = extent->l2_cache + (i * extent->l2_size);
1004             goto found;
1005         }
1006     }
1007     /* not found: load a new entry in the least used one */
1008     min_index = 0;
1009     min_count = 0xffffffff;
1010     for (i = 0; i < L2_CACHE_SIZE; i++) {
1011         if (extent->l2_cache_counts[i] < min_count) {
1012             min_count = extent->l2_cache_counts[i];
1013             min_index = i;
1014         }
1015     }
1016     l2_table = extent->l2_cache + (min_index * extent->l2_size);
1017     if (bdrv_pread(
1018                 extent->file,
1019                 (int64_t)l2_offset * 512,
1020                 l2_table,
1021                 extent->l2_size * sizeof(uint32_t)
1022             ) != extent->l2_size * sizeof(uint32_t)) {
1023         return VMDK_ERROR;
1024     }
1025
1026     extent->l2_cache_offsets[min_index] = l2_offset;
1027     extent->l2_cache_counts[min_index] = 1;
1028  found:
1029     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
1030     *cluster_offset = le32_to_cpu(l2_table[l2_index]);
1031
1032     if (m_data) {
1033         m_data->valid = 1;
1034         m_data->l1_index = l1_index;
1035         m_data->l2_index = l2_index;
1036         m_data->offset = *cluster_offset;
1037         m_data->l2_offset = l2_offset;
1038         m_data->l2_cache_entry = &l2_table[l2_index];
1039     }
1040     if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
1041         zeroed = true;
1042     }
1043
1044     if (!*cluster_offset || zeroed) {
1045         if (!allocate) {
1046             return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1047         }
1048
1049         /* Avoid the L2 tables update for the images that have snapshots. */
1050         *cluster_offset = bdrv_getlength(extent->file);
1051         if (!extent->compressed) {
1052             bdrv_truncate(
1053                 extent->file,
1054                 *cluster_offset + (extent->cluster_sectors << 9)
1055             );
1056         }
1057
1058         *cluster_offset >>= 9;
1059         l2_table[l2_index] = cpu_to_le32(*cluster_offset);
1060
1061         /* First of all we write grain itself, to avoid race condition
1062          * that may to corrupt the image.
1063          * This problem may occur because of insufficient space on host disk
1064          * or inappropriate VM shutdown.
1065          */
1066         if (get_whole_cluster(
1067                 bs, extent, *cluster_offset, offset, allocate) == -1) {
1068             return VMDK_ERROR;
1069         }
1070
1071         if (m_data) {
1072             m_data->offset = *cluster_offset;
1073         }
1074     }
1075     *cluster_offset <<= 9;
1076     return VMDK_OK;
1077 }
1078
1079 static VmdkExtent *find_extent(BDRVVmdkState *s,
1080                                 int64_t sector_num, VmdkExtent *start_hint)
1081 {
1082     VmdkExtent *extent = start_hint;
1083
1084     if (!extent) {
1085         extent = &s->extents[0];
1086     }
1087     while (extent < &s->extents[s->num_extents]) {
1088         if (sector_num < extent->end_sector) {
1089             return extent;
1090         }
1091         extent++;
1092     }
1093     return NULL;
1094 }
1095
1096 static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
1097         int64_t sector_num, int nb_sectors, int *pnum)
1098 {
1099     BDRVVmdkState *s = bs->opaque;
1100     int64_t index_in_cluster, n, ret;
1101     uint64_t offset;
1102     VmdkExtent *extent;
1103
1104     extent = find_extent(s, sector_num, NULL);
1105     if (!extent) {
1106         return 0;
1107     }
1108     qemu_co_mutex_lock(&s->lock);
1109     ret = get_cluster_offset(bs, extent, NULL,
1110                             sector_num * 512, 0, &offset);
1111     qemu_co_mutex_unlock(&s->lock);
1112
1113     switch (ret) {
1114     case VMDK_ERROR:
1115         ret = -EIO;
1116         break;
1117     case VMDK_UNALLOC:
1118         ret = 0;
1119         break;
1120     case VMDK_ZEROED:
1121         ret = BDRV_BLOCK_ZERO;
1122         break;
1123     case VMDK_OK:
1124         ret = BDRV_BLOCK_DATA;
1125         if (extent->file == bs->file) {
1126             ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1127         }
1128
1129         break;
1130     }
1131
1132     index_in_cluster = sector_num % extent->cluster_sectors;
1133     n = extent->cluster_sectors - index_in_cluster;
1134     if (n > nb_sectors) {
1135         n = nb_sectors;
1136     }
1137     *pnum = n;
1138     return ret;
1139 }
1140
1141 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1142                             int64_t offset_in_cluster, const uint8_t *buf,
1143                             int nb_sectors, int64_t sector_num)
1144 {
1145     int ret;
1146     VmdkGrainMarker *data = NULL;
1147     uLongf buf_len;
1148     const uint8_t *write_buf = buf;
1149     int write_len = nb_sectors * 512;
1150
1151     if (extent->compressed) {
1152         if (!extent->has_marker) {
1153             ret = -EINVAL;
1154             goto out;
1155         }
1156         buf_len = (extent->cluster_sectors << 9) * 2;
1157         data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1158         if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1159                 buf_len == 0) {
1160             ret = -EINVAL;
1161             goto out;
1162         }
1163         data->lba = sector_num;
1164         data->size = buf_len;
1165         write_buf = (uint8_t *)data;
1166         write_len = buf_len + sizeof(VmdkGrainMarker);
1167     }
1168     ret = bdrv_pwrite(extent->file,
1169                         cluster_offset + offset_in_cluster,
1170                         write_buf,
1171                         write_len);
1172     if (ret != write_len) {
1173         ret = ret < 0 ? ret : -EIO;
1174         goto out;
1175     }
1176     ret = 0;
1177  out:
1178     g_free(data);
1179     return ret;
1180 }
1181
1182 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1183                             int64_t offset_in_cluster, uint8_t *buf,
1184                             int nb_sectors)
1185 {
1186     int ret;
1187     int cluster_bytes, buf_bytes;
1188     uint8_t *cluster_buf, *compressed_data;
1189     uint8_t *uncomp_buf;
1190     uint32_t data_len;
1191     VmdkGrainMarker *marker;
1192     uLongf buf_len;
1193
1194
1195     if (!extent->compressed) {
1196         ret = bdrv_pread(extent->file,
1197                           cluster_offset + offset_in_cluster,
1198                           buf, nb_sectors * 512);
1199         if (ret == nb_sectors * 512) {
1200             return 0;
1201         } else {
1202             return -EIO;
1203         }
1204     }
1205     cluster_bytes = extent->cluster_sectors * 512;
1206     /* Read two clusters in case GrainMarker + compressed data > one cluster */
1207     buf_bytes = cluster_bytes * 2;
1208     cluster_buf = g_malloc(buf_bytes);
1209     uncomp_buf = g_malloc(cluster_bytes);
1210     ret = bdrv_pread(extent->file,
1211                 cluster_offset,
1212                 cluster_buf, buf_bytes);
1213     if (ret < 0) {
1214         goto out;
1215     }
1216     compressed_data = cluster_buf;
1217     buf_len = cluster_bytes;
1218     data_len = cluster_bytes;
1219     if (extent->has_marker) {
1220         marker = (VmdkGrainMarker *)cluster_buf;
1221         compressed_data = marker->data;
1222         data_len = le32_to_cpu(marker->size);
1223     }
1224     if (!data_len || data_len > buf_bytes) {
1225         ret = -EINVAL;
1226         goto out;
1227     }
1228     ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1229     if (ret != Z_OK) {
1230         ret = -EINVAL;
1231         goto out;
1232
1233     }
1234     if (offset_in_cluster < 0 ||
1235             offset_in_cluster + nb_sectors * 512 > buf_len) {
1236         ret = -EINVAL;
1237         goto out;
1238     }
1239     memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1240     ret = 0;
1241
1242  out:
1243     g_free(uncomp_buf);
1244     g_free(cluster_buf);
1245     return ret;
1246 }
1247
1248 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
1249                     uint8_t *buf, int nb_sectors)
1250 {
1251     BDRVVmdkState *s = bs->opaque;
1252     int ret;
1253     uint64_t n, index_in_cluster;
1254     uint64_t extent_begin_sector, extent_relative_sector_num;
1255     VmdkExtent *extent = NULL;
1256     uint64_t cluster_offset;
1257
1258     while (nb_sectors > 0) {
1259         extent = find_extent(s, sector_num, extent);
1260         if (!extent) {
1261             return -EIO;
1262         }
1263         ret = get_cluster_offset(
1264                             bs, extent, NULL,
1265                             sector_num << 9, 0, &cluster_offset);
1266         extent_begin_sector = extent->end_sector - extent->sectors;
1267         extent_relative_sector_num = sector_num - extent_begin_sector;
1268         index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1269         n = extent->cluster_sectors - index_in_cluster;
1270         if (n > nb_sectors) {
1271             n = nb_sectors;
1272         }
1273         if (ret != VMDK_OK) {
1274             /* if not allocated, try to read from parent image, if exist */
1275             if (bs->backing_hd && ret != VMDK_ZEROED) {
1276                 if (!vmdk_is_cid_valid(bs)) {
1277                     return -EINVAL;
1278                 }
1279                 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
1280                 if (ret < 0) {
1281                     return ret;
1282                 }
1283             } else {
1284                 memset(buf, 0, 512 * n);
1285             }
1286         } else {
1287             ret = vmdk_read_extent(extent,
1288                             cluster_offset, index_in_cluster * 512,
1289                             buf, n);
1290             if (ret) {
1291                 return ret;
1292             }
1293         }
1294         nb_sectors -= n;
1295         sector_num += n;
1296         buf += n * 512;
1297     }
1298     return 0;
1299 }
1300
1301 static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1302                                      uint8_t *buf, int nb_sectors)
1303 {
1304     int ret;
1305     BDRVVmdkState *s = bs->opaque;
1306     qemu_co_mutex_lock(&s->lock);
1307     ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1308     qemu_co_mutex_unlock(&s->lock);
1309     return ret;
1310 }
1311
1312 /**
1313  * vmdk_write:
1314  * @zeroed:       buf is ignored (data is zero), use zeroed_grain GTE feature
1315  *                if possible, otherwise return -ENOTSUP.
1316  * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1317  *                with each cluster. By dry run we can find if the zero write
1318  *                is possible without modifying image data.
1319  *
1320  * Returns: error code with 0 for success.
1321  */
1322 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
1323                       const uint8_t *buf, int nb_sectors,
1324                       bool zeroed, bool zero_dry_run)
1325 {
1326     BDRVVmdkState *s = bs->opaque;
1327     VmdkExtent *extent = NULL;
1328     int ret;
1329     int64_t index_in_cluster, n;
1330     uint64_t extent_begin_sector, extent_relative_sector_num;
1331     uint64_t cluster_offset;
1332     VmdkMetaData m_data;
1333
1334     if (sector_num > bs->total_sectors) {
1335         error_report("Wrong offset: sector_num=0x%" PRIx64
1336                 " total_sectors=0x%" PRIx64 "\n",
1337                 sector_num, bs->total_sectors);
1338         return -EIO;
1339     }
1340
1341     while (nb_sectors > 0) {
1342         extent = find_extent(s, sector_num, extent);
1343         if (!extent) {
1344             return -EIO;
1345         }
1346         ret = get_cluster_offset(
1347                                 bs,
1348                                 extent,
1349                                 &m_data,
1350                                 sector_num << 9, !extent->compressed,
1351                                 &cluster_offset);
1352         if (extent->compressed) {
1353             if (ret == VMDK_OK) {
1354                 /* Refuse write to allocated cluster for streamOptimized */
1355                 error_report("Could not write to allocated cluster"
1356                               " for streamOptimized");
1357                 return -EIO;
1358             } else {
1359                 /* allocate */
1360                 ret = get_cluster_offset(
1361                                         bs,
1362                                         extent,
1363                                         &m_data,
1364                                         sector_num << 9, 1,
1365                                         &cluster_offset);
1366             }
1367         }
1368         if (ret == VMDK_ERROR) {
1369             return -EINVAL;
1370         }
1371         extent_begin_sector = extent->end_sector - extent->sectors;
1372         extent_relative_sector_num = sector_num - extent_begin_sector;
1373         index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1374         n = extent->cluster_sectors - index_in_cluster;
1375         if (n > nb_sectors) {
1376             n = nb_sectors;
1377         }
1378         if (zeroed) {
1379             /* Do zeroed write, buf is ignored */
1380             if (extent->has_zero_grain &&
1381                     index_in_cluster == 0 &&
1382                     n >= extent->cluster_sectors) {
1383                 n = extent->cluster_sectors;
1384                 if (!zero_dry_run) {
1385                     m_data.offset = VMDK_GTE_ZEROED;
1386                     /* update L2 tables */
1387                     if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1388                         return -EIO;
1389                     }
1390                 }
1391             } else {
1392                 return -ENOTSUP;
1393             }
1394         } else {
1395             ret = vmdk_write_extent(extent,
1396                             cluster_offset, index_in_cluster * 512,
1397                             buf, n, sector_num);
1398             if (ret) {
1399                 return ret;
1400             }
1401             if (m_data.valid) {
1402                 /* update L2 tables */
1403                 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1404                     return -EIO;
1405                 }
1406             }
1407         }
1408         nb_sectors -= n;
1409         sector_num += n;
1410         buf += n * 512;
1411
1412         /* update CID on the first write every time the virtual disk is
1413          * opened */
1414         if (!s->cid_updated) {
1415             ret = vmdk_write_cid(bs, time(NULL));
1416             if (ret < 0) {
1417                 return ret;
1418             }
1419             s->cid_updated = true;
1420         }
1421     }
1422     return 0;
1423 }
1424
1425 static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1426                                       const uint8_t *buf, int nb_sectors)
1427 {
1428     int ret;
1429     BDRVVmdkState *s = bs->opaque;
1430     qemu_co_mutex_lock(&s->lock);
1431     ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1432     qemu_co_mutex_unlock(&s->lock);
1433     return ret;
1434 }
1435
1436 static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1437                                              int64_t sector_num,
1438                                              int nb_sectors,
1439                                              BdrvRequestFlags flags)
1440 {
1441     int ret;
1442     BDRVVmdkState *s = bs->opaque;
1443     qemu_co_mutex_lock(&s->lock);
1444     /* write zeroes could fail if sectors not aligned to cluster, test it with
1445      * dry_run == true before really updating image */
1446     ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1447     if (!ret) {
1448         ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1449     }
1450     qemu_co_mutex_unlock(&s->lock);
1451     return ret;
1452 }
1453
1454 static int vmdk_create_extent(const char *filename, int64_t filesize,
1455                               bool flat, bool compress, bool zeroed_grain,
1456                               Error **errp)
1457 {
1458     int ret, i;
1459     BlockDriverState *bs = NULL;
1460     VMDK4Header header;
1461     Error *local_err;
1462     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1463     uint32_t *gd_buf = NULL;
1464     int gd_buf_size;
1465
1466     ret = bdrv_create_file(filename, NULL, &local_err);
1467     if (ret < 0) {
1468         error_propagate(errp, local_err);
1469         goto exit;
1470     }
1471
1472     ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
1473     if (ret < 0) {
1474         error_propagate(errp, local_err);
1475         goto exit;
1476     }
1477
1478     if (flat) {
1479         ret = bdrv_truncate(bs, filesize);
1480         if (ret < 0) {
1481             error_setg(errp, "Could not truncate file");
1482         }
1483         goto exit;
1484     }
1485     magic = cpu_to_be32(VMDK4_MAGIC);
1486     memset(&header, 0, sizeof(header));
1487     header.version = zeroed_grain ? 2 : 1;
1488     header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1489                    | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1490                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1491     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1492     header.capacity = filesize / BDRV_SECTOR_SIZE;
1493     header.granularity = 128;
1494     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
1495
1496     grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1497     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1498                            BDRV_SECTOR_SIZE);
1499     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1500     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
1501
1502     header.desc_offset = 1;
1503     header.desc_size = 20;
1504     header.rgd_offset = header.desc_offset + header.desc_size;
1505     header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
1506     header.grain_offset =
1507         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1508                  header.granularity);
1509     /* swap endianness for all header fields */
1510     header.version = cpu_to_le32(header.version);
1511     header.flags = cpu_to_le32(header.flags);
1512     header.capacity = cpu_to_le64(header.capacity);
1513     header.granularity = cpu_to_le64(header.granularity);
1514     header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1515     header.desc_offset = cpu_to_le64(header.desc_offset);
1516     header.desc_size = cpu_to_le64(header.desc_size);
1517     header.rgd_offset = cpu_to_le64(header.rgd_offset);
1518     header.gd_offset = cpu_to_le64(header.gd_offset);
1519     header.grain_offset = cpu_to_le64(header.grain_offset);
1520     header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1521
1522     header.check_bytes[0] = 0xa;
1523     header.check_bytes[1] = 0x20;
1524     header.check_bytes[2] = 0xd;
1525     header.check_bytes[3] = 0xa;
1526
1527     /* write all the data */
1528     ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
1529     if (ret < 0) {
1530         error_set(errp, QERR_IO_ERROR);
1531         goto exit;
1532     }
1533     ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
1534     if (ret < 0) {
1535         error_set(errp, QERR_IO_ERROR);
1536         goto exit;
1537     }
1538
1539     ret = bdrv_truncate(bs, le64_to_cpu(header.grain_offset) << 9);
1540     if (ret < 0) {
1541         error_setg(errp, "Could not truncate file");
1542         goto exit;
1543     }
1544
1545     /* write grain directory */
1546     gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1547     gd_buf = g_malloc0(gd_buf_size);
1548     for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
1549          i < gt_count; i++, tmp += gt_size) {
1550         gd_buf[i] = cpu_to_le32(tmp);
1551     }
1552     ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
1553                       gd_buf, gd_buf_size);
1554     if (ret < 0) {
1555         error_set(errp, QERR_IO_ERROR);
1556         goto exit;
1557     }
1558
1559     /* write backup grain directory */
1560     for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
1561          i < gt_count; i++, tmp += gt_size) {
1562         gd_buf[i] = cpu_to_le32(tmp);
1563     }
1564     ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
1565                       gd_buf, gd_buf_size);
1566     if (ret < 0) {
1567         error_set(errp, QERR_IO_ERROR);
1568         goto exit;
1569     }
1570
1571     ret = 0;
1572 exit:
1573     if (bs) {
1574         bdrv_unref(bs);
1575     }
1576     g_free(gd_buf);
1577     return ret;
1578 }
1579
1580 static int filename_decompose(const char *filename, char *path, char *prefix,
1581                               char *postfix, size_t buf_len, Error **errp)
1582 {
1583     const char *p, *q;
1584
1585     if (filename == NULL || !strlen(filename)) {
1586         error_setg(errp, "No filename provided");
1587         return VMDK_ERROR;
1588     }
1589     p = strrchr(filename, '/');
1590     if (p == NULL) {
1591         p = strrchr(filename, '\\');
1592     }
1593     if (p == NULL) {
1594         p = strrchr(filename, ':');
1595     }
1596     if (p != NULL) {
1597         p++;
1598         if (p - filename >= buf_len) {
1599             return VMDK_ERROR;
1600         }
1601         pstrcpy(path, p - filename + 1, filename);
1602     } else {
1603         p = filename;
1604         path[0] = '\0';
1605     }
1606     q = strrchr(p, '.');
1607     if (q == NULL) {
1608         pstrcpy(prefix, buf_len, p);
1609         postfix[0] = '\0';
1610     } else {
1611         if (q - p >= buf_len) {
1612             return VMDK_ERROR;
1613         }
1614         pstrcpy(prefix, q - p + 1, p);
1615         pstrcpy(postfix, buf_len, q);
1616     }
1617     return VMDK_OK;
1618 }
1619
1620 static int vmdk_create(const char *filename, QEMUOptionParameter *options,
1621                        Error **errp)
1622 {
1623     int idx = 0;
1624     BlockDriverState *new_bs = NULL;
1625     Error *local_err;
1626     char *desc = NULL;
1627     int64_t total_size = 0, filesize;
1628     const char *adapter_type = NULL;
1629     const char *backing_file = NULL;
1630     const char *fmt = NULL;
1631     int flags = 0;
1632     int ret = 0;
1633     bool flat, split, compress;
1634     GString *ext_desc_lines;
1635     char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1636     const int64_t split_size = 0x80000000;  /* VMDK has constant split size */
1637     const char *desc_extent_line;
1638     char parent_desc_line[BUF_SIZE] = "";
1639     uint32_t parent_cid = 0xffffffff;
1640     uint32_t number_heads = 16;
1641     bool zeroed_grain = false;
1642     uint32_t desc_offset = 0, desc_len;
1643     const char desc_template[] =
1644         "# Disk DescriptorFile\n"
1645         "version=1\n"
1646         "CID=%x\n"
1647         "parentCID=%x\n"
1648         "createType=\"%s\"\n"
1649         "%s"
1650         "\n"
1651         "# Extent description\n"
1652         "%s"
1653         "\n"
1654         "# The Disk Data Base\n"
1655         "#DDB\n"
1656         "\n"
1657         "ddb.virtualHWVersion = \"%d\"\n"
1658         "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
1659         "ddb.geometry.heads = \"%d\"\n"
1660         "ddb.geometry.sectors = \"63\"\n"
1661         "ddb.adapterType = \"%s\"\n";
1662
1663     ext_desc_lines = g_string_new(NULL);
1664
1665     if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
1666         ret = -EINVAL;
1667         goto exit;
1668     }
1669     /* Read out options */
1670     while (options && options->name) {
1671         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1672             total_size = options->value.n;
1673         } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {
1674             adapter_type = options->value.s;
1675         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1676             backing_file = options->value.s;
1677         } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1678             flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1679         } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1680             fmt = options->value.s;
1681         } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {
1682             zeroed_grain |= options->value.n;
1683         }
1684         options++;
1685     }
1686     if (!adapter_type) {
1687         adapter_type = "ide";
1688     } else if (strcmp(adapter_type, "ide") &&
1689                strcmp(adapter_type, "buslogic") &&
1690                strcmp(adapter_type, "lsilogic") &&
1691                strcmp(adapter_type, "legacyESX")) {
1692         error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
1693         ret = -EINVAL;
1694         goto exit;
1695     }
1696     if (strcmp(adapter_type, "ide") != 0) {
1697         /* that's the number of heads with which vmware operates when
1698            creating, exporting, etc. vmdk files with a non-ide adapter type */
1699         number_heads = 255;
1700     }
1701     if (!fmt) {
1702         /* Default format to monolithicSparse */
1703         fmt = "monolithicSparse";
1704     } else if (strcmp(fmt, "monolithicFlat") &&
1705                strcmp(fmt, "monolithicSparse") &&
1706                strcmp(fmt, "twoGbMaxExtentSparse") &&
1707                strcmp(fmt, "twoGbMaxExtentFlat") &&
1708                strcmp(fmt, "streamOptimized")) {
1709         error_setg(errp, "Unknown subformat: '%s'", fmt);
1710         ret = -EINVAL;
1711         goto exit;
1712     }
1713     split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1714               strcmp(fmt, "twoGbMaxExtentSparse"));
1715     flat = !(strcmp(fmt, "monolithicFlat") &&
1716              strcmp(fmt, "twoGbMaxExtentFlat"));
1717     compress = !strcmp(fmt, "streamOptimized");
1718     if (flat) {
1719         desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1720     } else {
1721         desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1722     }
1723     if (flat && backing_file) {
1724         error_setg(errp, "Flat image can't have backing file");
1725         ret = -ENOTSUP;
1726         goto exit;
1727     }
1728     if (flat && zeroed_grain) {
1729         error_setg(errp, "Flat image can't enable zeroed grain");
1730         ret = -ENOTSUP;
1731         goto exit;
1732     }
1733     if (backing_file) {
1734         BlockDriverState *bs = bdrv_new("");
1735         ret = bdrv_open(bs, backing_file, NULL, BDRV_O_NO_BACKING, NULL, errp);
1736         if (ret != 0) {
1737             bdrv_unref(bs);
1738             goto exit;
1739         }
1740         if (strcmp(bs->drv->format_name, "vmdk")) {
1741             bdrv_unref(bs);
1742             ret = -EINVAL;
1743             goto exit;
1744         }
1745         parent_cid = vmdk_read_cid(bs, 0);
1746         bdrv_unref(bs);
1747         snprintf(parent_desc_line, sizeof(parent_desc_line),
1748                 "parentFileNameHint=\"%s\"", backing_file);
1749     }
1750
1751     /* Create extents */
1752     filesize = total_size;
1753     while (filesize > 0) {
1754         char desc_line[BUF_SIZE];
1755         char ext_filename[PATH_MAX];
1756         char desc_filename[PATH_MAX];
1757         int64_t size = filesize;
1758
1759         if (split && size > split_size) {
1760             size = split_size;
1761         }
1762         if (split) {
1763             snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1764                     prefix, flat ? 'f' : 's', ++idx, postfix);
1765         } else if (flat) {
1766             snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1767                     prefix, postfix);
1768         } else {
1769             snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1770                     prefix, postfix);
1771         }
1772         snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1773                 path, desc_filename);
1774
1775         if (vmdk_create_extent(ext_filename, size,
1776                                flat, compress, zeroed_grain, errp)) {
1777             ret = -EINVAL;
1778             goto exit;
1779         }
1780         filesize -= size;
1781
1782         /* Format description line */
1783         snprintf(desc_line, sizeof(desc_line),
1784                     desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
1785         g_string_append(ext_desc_lines, desc_line);
1786     }
1787     /* generate descriptor file */
1788     desc = g_strdup_printf(desc_template,
1789                            (unsigned int)time(NULL),
1790                            parent_cid,
1791                            fmt,
1792                            parent_desc_line,
1793                            ext_desc_lines->str,
1794                            (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1795                            total_size /
1796                                (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
1797                            number_heads,
1798                            adapter_type);
1799     desc_len = strlen(desc);
1800     /* the descriptor offset = 0x200 */
1801     if (!split && !flat) {
1802         desc_offset = 0x200;
1803     } else {
1804         ret = bdrv_create_file(filename, options, &local_err);
1805         if (ret < 0) {
1806             error_setg_errno(errp, -ret, "Could not create image file");
1807             goto exit;
1808         }
1809     }
1810     ret = bdrv_file_open(&new_bs, filename, NULL, BDRV_O_RDWR, &local_err);
1811     if (ret < 0) {
1812         error_setg_errno(errp, -ret, "Could not write description");
1813         goto exit;
1814     }
1815     ret = bdrv_pwrite(new_bs, desc_offset, desc, desc_len);
1816     if (ret < 0) {
1817         error_setg_errno(errp, -ret, "Could not write description");
1818         goto exit;
1819     }
1820     /* bdrv_pwrite write padding zeros to align to sector, we don't need that
1821      * for description file */
1822     if (desc_offset == 0) {
1823         ret = bdrv_truncate(new_bs, desc_len);
1824         if (ret < 0) {
1825             error_setg(errp, "Could not truncate file");
1826         }
1827     }
1828 exit:
1829     if (new_bs) {
1830         bdrv_unref(new_bs);
1831     }
1832     g_free(desc);
1833     g_string_free(ext_desc_lines, true);
1834     return ret;
1835 }
1836
1837 static void vmdk_close(BlockDriverState *bs)
1838 {
1839     BDRVVmdkState *s = bs->opaque;
1840
1841     vmdk_free_extents(bs);
1842     g_free(s->create_type);
1843
1844     migrate_del_blocker(s->migration_blocker);
1845     error_free(s->migration_blocker);
1846 }
1847
1848 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
1849 {
1850     BDRVVmdkState *s = bs->opaque;
1851     int i, err;
1852     int ret = 0;
1853
1854     for (i = 0; i < s->num_extents; i++) {
1855         err = bdrv_co_flush(s->extents[i].file);
1856         if (err < 0) {
1857             ret = err;
1858         }
1859     }
1860     return ret;
1861 }
1862
1863 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1864 {
1865     int i;
1866     int64_t ret = 0;
1867     int64_t r;
1868     BDRVVmdkState *s = bs->opaque;
1869
1870     ret = bdrv_get_allocated_file_size(bs->file);
1871     if (ret < 0) {
1872         return ret;
1873     }
1874     for (i = 0; i < s->num_extents; i++) {
1875         if (s->extents[i].file == bs->file) {
1876             continue;
1877         }
1878         r = bdrv_get_allocated_file_size(s->extents[i].file);
1879         if (r < 0) {
1880             return r;
1881         }
1882         ret += r;
1883     }
1884     return ret;
1885 }
1886
1887 static int vmdk_has_zero_init(BlockDriverState *bs)
1888 {
1889     int i;
1890     BDRVVmdkState *s = bs->opaque;
1891
1892     /* If has a flat extent and its underlying storage doesn't have zero init,
1893      * return 0. */
1894     for (i = 0; i < s->num_extents; i++) {
1895         if (s->extents[i].flat) {
1896             if (!bdrv_has_zero_init(s->extents[i].file)) {
1897                 return 0;
1898             }
1899         }
1900     }
1901     return 1;
1902 }
1903
1904 static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
1905 {
1906     ImageInfo *info = g_new0(ImageInfo, 1);
1907
1908     *info = (ImageInfo){
1909         .filename         = g_strdup(extent->file->filename),
1910         .format           = g_strdup(extent->type),
1911         .virtual_size     = extent->sectors * BDRV_SECTOR_SIZE,
1912         .compressed       = extent->compressed,
1913         .has_compressed   = extent->compressed,
1914         .cluster_size     = extent->cluster_sectors * BDRV_SECTOR_SIZE,
1915         .has_cluster_size = !extent->flat,
1916     };
1917
1918     return info;
1919 }
1920
1921 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
1922 {
1923     int i;
1924     BDRVVmdkState *s = bs->opaque;
1925     ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
1926     ImageInfoList **next;
1927
1928     *spec_info = (ImageInfoSpecific){
1929         .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
1930         {
1931             .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
1932         },
1933     };
1934
1935     *spec_info->vmdk = (ImageInfoSpecificVmdk) {
1936         .create_type = g_strdup(s->create_type),
1937         .cid = s->cid,
1938         .parent_cid = s->parent_cid,
1939     };
1940
1941     next = &spec_info->vmdk->extents;
1942     for (i = 0; i < s->num_extents; i++) {
1943         *next = g_new0(ImageInfoList, 1);
1944         (*next)->value = vmdk_get_extent_info(&s->extents[i]);
1945         (*next)->next = NULL;
1946         next = &(*next)->next;
1947     }
1948
1949     return spec_info;
1950 }
1951
1952 static QEMUOptionParameter vmdk_create_options[] = {
1953     {
1954         .name = BLOCK_OPT_SIZE,
1955         .type = OPT_SIZE,
1956         .help = "Virtual disk size"
1957     },
1958     {
1959         .name = BLOCK_OPT_ADAPTER_TYPE,
1960         .type = OPT_STRING,
1961         .help = "Virtual adapter type, can be one of "
1962                 "ide (default), lsilogic, buslogic or legacyESX"
1963     },
1964     {
1965         .name = BLOCK_OPT_BACKING_FILE,
1966         .type = OPT_STRING,
1967         .help = "File name of a base image"
1968     },
1969     {
1970         .name = BLOCK_OPT_COMPAT6,
1971         .type = OPT_FLAG,
1972         .help = "VMDK version 6 image"
1973     },
1974     {
1975         .name = BLOCK_OPT_SUBFMT,
1976         .type = OPT_STRING,
1977         .help =
1978             "VMDK flat extent format, can be one of "
1979             "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1980     },
1981     {
1982         .name = BLOCK_OPT_ZEROED_GRAIN,
1983         .type = OPT_FLAG,
1984         .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
1985     },
1986     { NULL }
1987 };
1988
1989 static BlockDriver bdrv_vmdk = {
1990     .format_name                  = "vmdk",
1991     .instance_size                = sizeof(BDRVVmdkState),
1992     .bdrv_probe                   = vmdk_probe,
1993     .bdrv_open                    = vmdk_open,
1994     .bdrv_reopen_prepare          = vmdk_reopen_prepare,
1995     .bdrv_read                    = vmdk_co_read,
1996     .bdrv_write                   = vmdk_co_write,
1997     .bdrv_co_write_zeroes         = vmdk_co_write_zeroes,
1998     .bdrv_close                   = vmdk_close,
1999     .bdrv_create                  = vmdk_create,
2000     .bdrv_co_flush_to_disk        = vmdk_co_flush,
2001     .bdrv_co_get_block_status     = vmdk_co_get_block_status,
2002     .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2003     .bdrv_has_zero_init           = vmdk_has_zero_init,
2004     .bdrv_get_specific_info       = vmdk_get_specific_info,
2005
2006     .create_options               = vmdk_create_options,
2007 };
2008
2009 static void bdrv_vmdk_init(void)
2010 {
2011     bdrv_register(&bdrv_vmdk);
2012 }
2013
2014 block_init(bdrv_vmdk_init);
This page took 0.134332 seconds and 4 git commands to generate.