target-i386: kvm_cpu_fill_host(): No need to check xlevel2
[qemu/kevin.git] / block / vmdk.c
blob99ca60fdb973078a55bc7550dd0b33fcf5697668
1 /*
2 * Block driver for the VMDK format
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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>
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
43 #define VMDK_GTE_ZEROED 0x1
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)
52 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
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;
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;
84 #define L2_CACHE_SIZE 16
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;
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];
108 int64_t cluster_sectors;
109 char *type;
110 } VmdkExtent;
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;
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;
135 typedef struct VmdkGrainMarker {
136 uint64_t lba;
137 uint32_t size;
138 uint8_t data[0];
139 } QEMU_PACKED VmdkGrainMarker;
141 enum {
142 MARKER_END_OF_STREAM = 0,
143 MARKER_GRAIN_TABLE = 1,
144 MARKER_GRAIN_DIRECTORY = 2,
145 MARKER_FOOTER = 3,
148 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
150 uint32_t magic;
152 if (buf_size < 4) {
153 return 0;
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++;
168 p++;
169 continue;
171 if (*p == ' ') {
172 while (p < end && *p == ' ') {
173 p++;
175 /* skip '\r' if windows line endings used. */
176 if (p < end && *p == '\r') {
177 p++;
179 /* only accept blank lines before 'version=' line */
180 if (p == end || *p != '\n') {
181 return 0;
183 p++;
184 continue;
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;
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;
198 return 0;
200 return 0;
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 */
209 static void vmdk_free_extents(BlockDriverState *bs)
211 int i;
212 BDRVVmdkState *s = bs->opaque;
213 VmdkExtent *e;
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);
225 g_free(s->extents);
228 static void vmdk_free_last_extent(BlockDriverState *bs)
230 BDRVVmdkState *s = bs->opaque;
232 if (s->num_extents == 0) {
233 return;
235 s->num_extents--;
236 s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
239 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
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;
248 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
249 if (ret < 0) {
250 return 0;
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");
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);
268 return cid;
271 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
273 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
274 char *p_name, *tmp_str;
275 BDRVVmdkState *s = bs->opaque;
276 int ret;
278 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
279 if (ret < 0) {
280 return ret;
283 desc[DESC_SIZE - 1] = '\0';
284 tmp_str = strstr(desc, "parentCID");
285 if (tmp_str == NULL) {
286 return -EINVAL;
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);
297 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
298 if (ret < 0) {
299 return ret;
302 return 0;
305 static int vmdk_is_cid_valid(BlockDriverState *bs)
307 BDRVVmdkState *s = bs->opaque;
308 BlockDriverState *p_bs = bs->backing_hd;
309 uint32_t cur_pcid;
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;
318 s->cid_checked = true;
319 /* CID valid */
320 return 1;
323 /* Queue extents, if any, for reopen() */
324 static int vmdk_reopen_prepare(BDRVReopenState *state,
325 BlockReopenQueue *queue, Error **errp)
327 BDRVVmdkState *s;
328 int ret = -1;
329 int i;
330 VmdkExtent *e;
332 assert(state != NULL);
333 assert(state->bs != NULL);
335 if (queue == NULL) {
336 error_setg(errp, "No reopen queue for VMDK extents");
337 goto exit;
340 s = state->bs->opaque;
342 assert(s != NULL);
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);
350 ret = 0;
352 exit:
353 return ret;
356 static int vmdk_parent_open(BlockDriverState *bs)
358 char *p_name;
359 char desc[DESC_SIZE + 1];
360 BDRVVmdkState *s = bs->opaque;
361 int ret;
363 desc[DESC_SIZE] = '\0';
364 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
365 if (ret < 0) {
366 return ret;
369 p_name = strstr(desc, "parentFileNameHint");
370 if (p_name != NULL) {
371 char *end_name;
373 p_name += sizeof("parentFileNameHint") + 1;
374 end_name = strchr(p_name, '\"');
375 if (end_name == NULL) {
376 return -EINVAL;
378 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
379 return -EINVAL;
382 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
385 return 0;
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)
398 VmdkExtent *extent;
399 BDRVVmdkState *s = bs->opaque;
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;
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;
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++;
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;
431 if (s->num_extents > 1) {
432 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
433 } else {
434 extent->end_sector = extent->sectors;
436 bs->total_sectors = extent->end_sector;
437 if (new_extent) {
438 *new_extent = extent;
440 return 0;
443 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
444 Error **errp)
446 int ret;
447 int l1_size, i;
449 /* read the L1 table */
450 l1_size = extent->l1_size * sizeof(uint32_t);
451 extent->l1_table = g_malloc(l1_size);
452 ret = bdrv_pread(extent->file,
453 extent->l1_table_offset,
454 extent->l1_table,
455 l1_size);
456 if (ret < 0) {
457 error_setg_errno(errp, -ret,
458 "Could not read l1 table from extent '%s'",
459 extent->file->filename);
460 goto fail_l1;
462 for (i = 0; i < extent->l1_size; i++) {
463 le32_to_cpus(&extent->l1_table[i]);
466 if (extent->l1_backup_table_offset) {
467 extent->l1_backup_table = g_malloc(l1_size);
468 ret = bdrv_pread(extent->file,
469 extent->l1_backup_table_offset,
470 extent->l1_backup_table,
471 l1_size);
472 if (ret < 0) {
473 error_setg_errno(errp, -ret,
474 "Could not read l1 backup table from extent '%s'",
475 extent->file->filename);
476 goto fail_l1b;
478 for (i = 0; i < extent->l1_size; i++) {
479 le32_to_cpus(&extent->l1_backup_table[i]);
483 extent->l2_cache =
484 g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
485 return 0;
486 fail_l1b:
487 g_free(extent->l1_backup_table);
488 fail_l1:
489 g_free(extent->l1_table);
490 return ret;
493 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
494 BlockDriverState *file,
495 int flags, Error **errp)
497 int ret;
498 uint32_t magic;
499 VMDK3Header header;
500 VmdkExtent *extent;
502 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
503 if (ret < 0) {
504 error_setg_errno(errp, -ret,
505 "Could not read header from file '%s'",
506 file->filename);
507 return ret;
509 ret = vmdk_add_extent(bs, file, false,
510 le32_to_cpu(header.disk_sectors),
511 le32_to_cpu(header.l1dir_offset) << 9,
513 le32_to_cpu(header.l1dir_size),
514 4096,
515 le32_to_cpu(header.granularity),
516 &extent,
517 errp);
518 if (ret < 0) {
519 return ret;
521 ret = vmdk_init_tables(bs, extent, errp);
522 if (ret) {
523 /* free extent allocated by vmdk_add_extent */
524 vmdk_free_last_extent(bs);
526 return ret;
529 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
530 uint64_t desc_offset, Error **errp);
532 static int vmdk_open_vmdk4(BlockDriverState *bs,
533 BlockDriverState *file,
534 int flags, Error **errp)
536 int ret;
537 uint32_t magic;
538 uint32_t l1_size, l1_entry_sectors;
539 VMDK4Header header;
540 VmdkExtent *extent;
541 BDRVVmdkState *s = bs->opaque;
542 int64_t l1_backup_offset = 0;
544 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
545 if (ret < 0) {
546 error_setg_errno(errp, -ret,
547 "Could not read header from file '%s'",
548 file->filename);
550 if (header.capacity == 0) {
551 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
552 if (desc_offset) {
553 return vmdk_open_desc_file(bs, flags, desc_offset << 9, errp);
557 if (!s->create_type) {
558 s->create_type = g_strdup("monolithicSparse");
561 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
563 * The footer takes precedence over the header, so read it in. The
564 * footer starts at offset -1024 from the end: One sector for the
565 * footer, and another one for the end-of-stream marker.
567 struct {
568 struct {
569 uint64_t val;
570 uint32_t size;
571 uint32_t type;
572 uint8_t pad[512 - 16];
573 } QEMU_PACKED footer_marker;
575 uint32_t magic;
576 VMDK4Header header;
577 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
579 struct {
580 uint64_t val;
581 uint32_t size;
582 uint32_t type;
583 uint8_t pad[512 - 16];
584 } QEMU_PACKED eos_marker;
585 } QEMU_PACKED footer;
587 ret = bdrv_pread(file,
588 bs->file->total_sectors * 512 - 1536,
589 &footer, sizeof(footer));
590 if (ret < 0) {
591 return ret;
594 /* Some sanity checks for the footer */
595 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
596 le32_to_cpu(footer.footer_marker.size) != 0 ||
597 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
598 le64_to_cpu(footer.eos_marker.val) != 0 ||
599 le32_to_cpu(footer.eos_marker.size) != 0 ||
600 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
602 return -EINVAL;
605 header = footer.header;
608 if (le32_to_cpu(header.version) > 3) {
609 char buf[64];
610 snprintf(buf, sizeof(buf), "VMDK version %d",
611 le32_to_cpu(header.version));
612 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
613 bs->device_name, "vmdk", buf);
614 return -ENOTSUP;
615 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
616 /* VMware KB 2064959 explains that version 3 added support for
617 * persistent changed block tracking (CBT), and backup software can
618 * read it as version=1 if it doesn't care about the changed area
619 * information. So we are safe to enable read only. */
620 error_setg(errp, "VMDK version 3 must be read only");
621 return -EINVAL;
624 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
625 error_report("L2 table size too big");
626 return -EINVAL;
629 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
630 * le64_to_cpu(header.granularity);
631 if (l1_entry_sectors == 0) {
632 return -EINVAL;
634 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
635 / l1_entry_sectors;
636 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
637 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
639 if (bdrv_getlength(file) <
640 le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE) {
641 error_report("File truncated, expecting at least %lld bytes",
642 le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
643 return -EINVAL;
646 ret = vmdk_add_extent(bs, file, false,
647 le64_to_cpu(header.capacity),
648 le64_to_cpu(header.gd_offset) << 9,
649 l1_backup_offset,
650 l1_size,
651 le32_to_cpu(header.num_gtes_per_gt),
652 le64_to_cpu(header.granularity),
653 &extent,
654 errp);
655 if (ret < 0) {
656 return ret;
658 extent->compressed =
659 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
660 if (extent->compressed) {
661 g_free(s->create_type);
662 s->create_type = g_strdup("streamOptimized");
664 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
665 extent->version = le32_to_cpu(header.version);
666 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
667 ret = vmdk_init_tables(bs, extent, errp);
668 if (ret) {
669 /* free extent allocated by vmdk_add_extent */
670 vmdk_free_last_extent(bs);
672 return ret;
675 /* find an option value out of descriptor file */
676 static int vmdk_parse_description(const char *desc, const char *opt_name,
677 char *buf, int buf_size)
679 char *opt_pos, *opt_end;
680 const char *end = desc + strlen(desc);
682 opt_pos = strstr(desc, opt_name);
683 if (!opt_pos) {
684 return VMDK_ERROR;
686 /* Skip "=\"" following opt_name */
687 opt_pos += strlen(opt_name) + 2;
688 if (opt_pos >= end) {
689 return VMDK_ERROR;
691 opt_end = opt_pos;
692 while (opt_end < end && *opt_end != '"') {
693 opt_end++;
695 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
696 return VMDK_ERROR;
698 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
699 return VMDK_OK;
702 /* Open an extent file and append to bs array */
703 static int vmdk_open_sparse(BlockDriverState *bs,
704 BlockDriverState *file,
705 int flags, Error **errp)
707 uint32_t magic;
709 if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
710 return -EIO;
713 magic = be32_to_cpu(magic);
714 switch (magic) {
715 case VMDK3_MAGIC:
716 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
717 break;
718 case VMDK4_MAGIC:
719 return vmdk_open_vmdk4(bs, file, flags, errp);
720 break;
721 default:
722 return -EMEDIUMTYPE;
723 break;
727 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
728 const char *desc_file_path, Error **errp)
730 int ret;
731 char access[11];
732 char type[11];
733 char fname[512];
734 const char *p = desc;
735 int64_t sectors = 0;
736 int64_t flat_offset;
737 char extent_path[PATH_MAX];
738 BlockDriverState *extent_file;
739 BDRVVmdkState *s = bs->opaque;
740 VmdkExtent *extent;
742 while (*p) {
743 /* parse extent line:
744 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
745 * or
746 * RW [size in sectors] SPARSE "file-name.vmdk"
748 flat_offset = -1;
749 ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
750 access, &sectors, type, fname, &flat_offset);
751 if (ret < 4 || strcmp(access, "RW")) {
752 goto next_line;
753 } else if (!strcmp(type, "FLAT")) {
754 if (ret != 5 || flat_offset < 0) {
755 error_setg(errp, "Invalid extent lines: \n%s", p);
756 return -EINVAL;
758 } else if (!strcmp(type, "VMFS")) {
759 if (ret == 4) {
760 flat_offset = 0;
761 } else {
762 error_setg(errp, "Invalid extent lines:\n%s", p);
763 return -EINVAL;
765 } else if (ret != 4) {
766 error_setg(errp, "Invalid extent lines:\n%s", p);
767 return -EINVAL;
770 if (sectors <= 0 ||
771 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
772 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
773 (strcmp(access, "RW"))) {
774 goto next_line;
777 path_combine(extent_path, sizeof(extent_path),
778 desc_file_path, fname);
779 ret = bdrv_file_open(&extent_file, extent_path, NULL, NULL,
780 bs->open_flags, errp);
781 if (ret) {
782 return ret;
785 /* save to extents array */
786 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
787 /* FLAT extent */
789 ret = vmdk_add_extent(bs, extent_file, true, sectors,
790 0, 0, 0, 0, 0, &extent, errp);
791 if (ret < 0) {
792 return ret;
794 extent->flat_start_offset = flat_offset << 9;
795 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
796 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
797 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, errp);
798 if (ret) {
799 bdrv_unref(extent_file);
800 return ret;
802 extent = &s->extents[s->num_extents - 1];
803 } else {
804 error_setg(errp, "Unsupported extent type '%s'", type);
805 return -ENOTSUP;
807 extent->type = g_strdup(type);
808 next_line:
809 /* move to next line */
810 while (*p) {
811 if (*p == '\n') {
812 p++;
813 break;
815 p++;
818 return 0;
821 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
822 uint64_t desc_offset, Error **errp)
824 int ret;
825 char *buf = NULL;
826 char ct[128];
827 BDRVVmdkState *s = bs->opaque;
828 int64_t size;
830 size = bdrv_getlength(bs->file);
831 if (size < 0) {
832 return -EINVAL;
835 size = MIN(size, 1 << 20); /* avoid unbounded allocation */
836 buf = g_malloc0(size + 1);
838 ret = bdrv_pread(bs->file, desc_offset, buf, size);
839 if (ret < 0) {
840 goto exit;
842 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
843 ret = -EMEDIUMTYPE;
844 goto exit;
846 if (strcmp(ct, "monolithicFlat") &&
847 strcmp(ct, "vmfs") &&
848 strcmp(ct, "vmfsSparse") &&
849 strcmp(ct, "twoGbMaxExtentSparse") &&
850 strcmp(ct, "twoGbMaxExtentFlat")) {
851 error_setg(errp, "Unsupported image type '%s'", ct);
852 ret = -ENOTSUP;
853 goto exit;
855 s->create_type = g_strdup(ct);
856 s->desc_offset = 0;
857 ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
858 exit:
859 g_free(buf);
860 return ret;
863 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
864 Error **errp)
866 int ret;
867 BDRVVmdkState *s = bs->opaque;
869 if (vmdk_open_sparse(bs, bs->file, flags, errp) == 0) {
870 s->desc_offset = 0x200;
871 } else {
872 ret = vmdk_open_desc_file(bs, flags, 0, errp);
873 if (ret) {
874 goto fail;
877 /* try to open parent images, if exist */
878 ret = vmdk_parent_open(bs);
879 if (ret) {
880 goto fail;
882 s->cid = vmdk_read_cid(bs, 0);
883 s->parent_cid = vmdk_read_cid(bs, 1);
884 qemu_co_mutex_init(&s->lock);
886 /* Disable migration when VMDK images are used */
887 error_set(&s->migration_blocker,
888 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
889 "vmdk", bs->device_name, "live migration");
890 migrate_add_blocker(s->migration_blocker);
892 return 0;
894 fail:
895 g_free(s->create_type);
896 s->create_type = NULL;
897 vmdk_free_extents(bs);
898 return ret;
902 static int vmdk_refresh_limits(BlockDriverState *bs)
904 BDRVVmdkState *s = bs->opaque;
905 int i;
907 for (i = 0; i < s->num_extents; i++) {
908 if (!s->extents[i].flat) {
909 bs->bl.write_zeroes_alignment =
910 MAX(bs->bl.write_zeroes_alignment,
911 s->extents[i].cluster_sectors);
915 return 0;
918 static int get_whole_cluster(BlockDriverState *bs,
919 VmdkExtent *extent,
920 uint64_t cluster_offset,
921 uint64_t offset,
922 bool allocate)
924 int ret = VMDK_OK;
925 uint8_t *whole_grain = NULL;
927 /* we will be here if it's first write on non-exist grain(cluster).
928 * try to read from parent image, if exist */
929 if (bs->backing_hd) {
930 whole_grain =
931 qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS);
932 if (!vmdk_is_cid_valid(bs)) {
933 ret = VMDK_ERROR;
934 goto exit;
937 /* floor offset to cluster */
938 offset -= offset % (extent->cluster_sectors * 512);
939 ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
940 extent->cluster_sectors);
941 if (ret < 0) {
942 ret = VMDK_ERROR;
943 goto exit;
946 /* Write grain only into the active image */
947 ret = bdrv_write(extent->file, cluster_offset, whole_grain,
948 extent->cluster_sectors);
949 if (ret < 0) {
950 ret = VMDK_ERROR;
951 goto exit;
954 exit:
955 qemu_vfree(whole_grain);
956 return ret;
959 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
961 uint32_t offset;
962 QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
963 offset = cpu_to_le32(m_data->offset);
964 /* update L2 table */
965 if (bdrv_pwrite_sync(
966 extent->file,
967 ((int64_t)m_data->l2_offset * 512)
968 + (m_data->l2_index * sizeof(m_data->offset)),
969 &offset, sizeof(offset)) < 0) {
970 return VMDK_ERROR;
972 /* update backup L2 table */
973 if (extent->l1_backup_table_offset != 0) {
974 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
975 if (bdrv_pwrite_sync(
976 extent->file,
977 ((int64_t)m_data->l2_offset * 512)
978 + (m_data->l2_index * sizeof(m_data->offset)),
979 &offset, sizeof(offset)) < 0) {
980 return VMDK_ERROR;
983 if (m_data->l2_cache_entry) {
984 *m_data->l2_cache_entry = offset;
987 return VMDK_OK;
990 static int get_cluster_offset(BlockDriverState *bs,
991 VmdkExtent *extent,
992 VmdkMetaData *m_data,
993 uint64_t offset,
994 int allocate,
995 uint64_t *cluster_offset)
997 unsigned int l1_index, l2_offset, l2_index;
998 int min_index, i, j;
999 uint32_t min_count, *l2_table;
1000 bool zeroed = false;
1002 if (m_data) {
1003 m_data->valid = 0;
1005 if (extent->flat) {
1006 *cluster_offset = extent->flat_start_offset;
1007 return VMDK_OK;
1010 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
1011 l1_index = (offset >> 9) / extent->l1_entry_sectors;
1012 if (l1_index >= extent->l1_size) {
1013 return VMDK_ERROR;
1015 l2_offset = extent->l1_table[l1_index];
1016 if (!l2_offset) {
1017 return VMDK_UNALLOC;
1019 for (i = 0; i < L2_CACHE_SIZE; i++) {
1020 if (l2_offset == extent->l2_cache_offsets[i]) {
1021 /* increment the hit count */
1022 if (++extent->l2_cache_counts[i] == 0xffffffff) {
1023 for (j = 0; j < L2_CACHE_SIZE; j++) {
1024 extent->l2_cache_counts[j] >>= 1;
1027 l2_table = extent->l2_cache + (i * extent->l2_size);
1028 goto found;
1031 /* not found: load a new entry in the least used one */
1032 min_index = 0;
1033 min_count = 0xffffffff;
1034 for (i = 0; i < L2_CACHE_SIZE; i++) {
1035 if (extent->l2_cache_counts[i] < min_count) {
1036 min_count = extent->l2_cache_counts[i];
1037 min_index = i;
1040 l2_table = extent->l2_cache + (min_index * extent->l2_size);
1041 if (bdrv_pread(
1042 extent->file,
1043 (int64_t)l2_offset * 512,
1044 l2_table,
1045 extent->l2_size * sizeof(uint32_t)
1046 ) != extent->l2_size * sizeof(uint32_t)) {
1047 return VMDK_ERROR;
1050 extent->l2_cache_offsets[min_index] = l2_offset;
1051 extent->l2_cache_counts[min_index] = 1;
1052 found:
1053 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
1054 *cluster_offset = le32_to_cpu(l2_table[l2_index]);
1056 if (m_data) {
1057 m_data->valid = 1;
1058 m_data->l1_index = l1_index;
1059 m_data->l2_index = l2_index;
1060 m_data->offset = *cluster_offset;
1061 m_data->l2_offset = l2_offset;
1062 m_data->l2_cache_entry = &l2_table[l2_index];
1064 if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
1065 zeroed = true;
1068 if (!*cluster_offset || zeroed) {
1069 if (!allocate) {
1070 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1073 /* Avoid the L2 tables update for the images that have snapshots. */
1074 *cluster_offset = bdrv_getlength(extent->file);
1075 if (!extent->compressed) {
1076 bdrv_truncate(
1077 extent->file,
1078 *cluster_offset + (extent->cluster_sectors << 9)
1082 *cluster_offset >>= 9;
1083 l2_table[l2_index] = cpu_to_le32(*cluster_offset);
1085 /* First of all we write grain itself, to avoid race condition
1086 * that may to corrupt the image.
1087 * This problem may occur because of insufficient space on host disk
1088 * or inappropriate VM shutdown.
1090 if (get_whole_cluster(
1091 bs, extent, *cluster_offset, offset, allocate) == -1) {
1092 return VMDK_ERROR;
1095 if (m_data) {
1096 m_data->offset = *cluster_offset;
1099 *cluster_offset <<= 9;
1100 return VMDK_OK;
1103 static VmdkExtent *find_extent(BDRVVmdkState *s,
1104 int64_t sector_num, VmdkExtent *start_hint)
1106 VmdkExtent *extent = start_hint;
1108 if (!extent) {
1109 extent = &s->extents[0];
1111 while (extent < &s->extents[s->num_extents]) {
1112 if (sector_num < extent->end_sector) {
1113 return extent;
1115 extent++;
1117 return NULL;
1120 static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
1121 int64_t sector_num, int nb_sectors, int *pnum)
1123 BDRVVmdkState *s = bs->opaque;
1124 int64_t index_in_cluster, n, ret;
1125 uint64_t offset;
1126 VmdkExtent *extent;
1128 extent = find_extent(s, sector_num, NULL);
1129 if (!extent) {
1130 return 0;
1132 qemu_co_mutex_lock(&s->lock);
1133 ret = get_cluster_offset(bs, extent, NULL,
1134 sector_num * 512, 0, &offset);
1135 qemu_co_mutex_unlock(&s->lock);
1137 switch (ret) {
1138 case VMDK_ERROR:
1139 ret = -EIO;
1140 break;
1141 case VMDK_UNALLOC:
1142 ret = 0;
1143 break;
1144 case VMDK_ZEROED:
1145 ret = BDRV_BLOCK_ZERO;
1146 break;
1147 case VMDK_OK:
1148 ret = BDRV_BLOCK_DATA;
1149 if (extent->file == bs->file) {
1150 ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1153 break;
1156 index_in_cluster = sector_num % extent->cluster_sectors;
1157 n = extent->cluster_sectors - index_in_cluster;
1158 if (n > nb_sectors) {
1159 n = nb_sectors;
1161 *pnum = n;
1162 return ret;
1165 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1166 int64_t offset_in_cluster, const uint8_t *buf,
1167 int nb_sectors, int64_t sector_num)
1169 int ret;
1170 VmdkGrainMarker *data = NULL;
1171 uLongf buf_len;
1172 const uint8_t *write_buf = buf;
1173 int write_len = nb_sectors * 512;
1175 if (extent->compressed) {
1176 if (!extent->has_marker) {
1177 ret = -EINVAL;
1178 goto out;
1180 buf_len = (extent->cluster_sectors << 9) * 2;
1181 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1182 if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1183 buf_len == 0) {
1184 ret = -EINVAL;
1185 goto out;
1187 data->lba = sector_num;
1188 data->size = buf_len;
1189 write_buf = (uint8_t *)data;
1190 write_len = buf_len + sizeof(VmdkGrainMarker);
1192 ret = bdrv_pwrite(extent->file,
1193 cluster_offset + offset_in_cluster,
1194 write_buf,
1195 write_len);
1196 if (ret != write_len) {
1197 ret = ret < 0 ? ret : -EIO;
1198 goto out;
1200 ret = 0;
1201 out:
1202 g_free(data);
1203 return ret;
1206 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1207 int64_t offset_in_cluster, uint8_t *buf,
1208 int nb_sectors)
1210 int ret;
1211 int cluster_bytes, buf_bytes;
1212 uint8_t *cluster_buf, *compressed_data;
1213 uint8_t *uncomp_buf;
1214 uint32_t data_len;
1215 VmdkGrainMarker *marker;
1216 uLongf buf_len;
1219 if (!extent->compressed) {
1220 ret = bdrv_pread(extent->file,
1221 cluster_offset + offset_in_cluster,
1222 buf, nb_sectors * 512);
1223 if (ret == nb_sectors * 512) {
1224 return 0;
1225 } else {
1226 return -EIO;
1229 cluster_bytes = extent->cluster_sectors * 512;
1230 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1231 buf_bytes = cluster_bytes * 2;
1232 cluster_buf = g_malloc(buf_bytes);
1233 uncomp_buf = g_malloc(cluster_bytes);
1234 ret = bdrv_pread(extent->file,
1235 cluster_offset,
1236 cluster_buf, buf_bytes);
1237 if (ret < 0) {
1238 goto out;
1240 compressed_data = cluster_buf;
1241 buf_len = cluster_bytes;
1242 data_len = cluster_bytes;
1243 if (extent->has_marker) {
1244 marker = (VmdkGrainMarker *)cluster_buf;
1245 compressed_data = marker->data;
1246 data_len = le32_to_cpu(marker->size);
1248 if (!data_len || data_len > buf_bytes) {
1249 ret = -EINVAL;
1250 goto out;
1252 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1253 if (ret != Z_OK) {
1254 ret = -EINVAL;
1255 goto out;
1258 if (offset_in_cluster < 0 ||
1259 offset_in_cluster + nb_sectors * 512 > buf_len) {
1260 ret = -EINVAL;
1261 goto out;
1263 memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1264 ret = 0;
1266 out:
1267 g_free(uncomp_buf);
1268 g_free(cluster_buf);
1269 return ret;
1272 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
1273 uint8_t *buf, int nb_sectors)
1275 BDRVVmdkState *s = bs->opaque;
1276 int ret;
1277 uint64_t n, index_in_cluster;
1278 uint64_t extent_begin_sector, extent_relative_sector_num;
1279 VmdkExtent *extent = NULL;
1280 uint64_t cluster_offset;
1282 while (nb_sectors > 0) {
1283 extent = find_extent(s, sector_num, extent);
1284 if (!extent) {
1285 return -EIO;
1287 ret = get_cluster_offset(
1288 bs, extent, NULL,
1289 sector_num << 9, 0, &cluster_offset);
1290 extent_begin_sector = extent->end_sector - extent->sectors;
1291 extent_relative_sector_num = sector_num - extent_begin_sector;
1292 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1293 n = extent->cluster_sectors - index_in_cluster;
1294 if (n > nb_sectors) {
1295 n = nb_sectors;
1297 if (ret != VMDK_OK) {
1298 /* if not allocated, try to read from parent image, if exist */
1299 if (bs->backing_hd && ret != VMDK_ZEROED) {
1300 if (!vmdk_is_cid_valid(bs)) {
1301 return -EINVAL;
1303 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
1304 if (ret < 0) {
1305 return ret;
1307 } else {
1308 memset(buf, 0, 512 * n);
1310 } else {
1311 ret = vmdk_read_extent(extent,
1312 cluster_offset, index_in_cluster * 512,
1313 buf, n);
1314 if (ret) {
1315 return ret;
1318 nb_sectors -= n;
1319 sector_num += n;
1320 buf += n * 512;
1322 return 0;
1325 static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1326 uint8_t *buf, int nb_sectors)
1328 int ret;
1329 BDRVVmdkState *s = bs->opaque;
1330 qemu_co_mutex_lock(&s->lock);
1331 ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1332 qemu_co_mutex_unlock(&s->lock);
1333 return ret;
1337 * vmdk_write:
1338 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1339 * if possible, otherwise return -ENOTSUP.
1340 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1341 * with each cluster. By dry run we can find if the zero write
1342 * is possible without modifying image data.
1344 * Returns: error code with 0 for success.
1346 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
1347 const uint8_t *buf, int nb_sectors,
1348 bool zeroed, bool zero_dry_run)
1350 BDRVVmdkState *s = bs->opaque;
1351 VmdkExtent *extent = NULL;
1352 int ret;
1353 int64_t index_in_cluster, n;
1354 uint64_t extent_begin_sector, extent_relative_sector_num;
1355 uint64_t cluster_offset;
1356 VmdkMetaData m_data;
1358 if (sector_num > bs->total_sectors) {
1359 error_report("Wrong offset: sector_num=0x%" PRIx64
1360 " total_sectors=0x%" PRIx64 "\n",
1361 sector_num, bs->total_sectors);
1362 return -EIO;
1365 while (nb_sectors > 0) {
1366 extent = find_extent(s, sector_num, extent);
1367 if (!extent) {
1368 return -EIO;
1370 ret = get_cluster_offset(
1372 extent,
1373 &m_data,
1374 sector_num << 9, !extent->compressed,
1375 &cluster_offset);
1376 if (extent->compressed) {
1377 if (ret == VMDK_OK) {
1378 /* Refuse write to allocated cluster for streamOptimized */
1379 error_report("Could not write to allocated cluster"
1380 " for streamOptimized");
1381 return -EIO;
1382 } else {
1383 /* allocate */
1384 ret = get_cluster_offset(
1386 extent,
1387 &m_data,
1388 sector_num << 9, 1,
1389 &cluster_offset);
1392 if (ret == VMDK_ERROR) {
1393 return -EINVAL;
1395 extent_begin_sector = extent->end_sector - extent->sectors;
1396 extent_relative_sector_num = sector_num - extent_begin_sector;
1397 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
1398 n = extent->cluster_sectors - index_in_cluster;
1399 if (n > nb_sectors) {
1400 n = nb_sectors;
1402 if (zeroed) {
1403 /* Do zeroed write, buf is ignored */
1404 if (extent->has_zero_grain &&
1405 index_in_cluster == 0 &&
1406 n >= extent->cluster_sectors) {
1407 n = extent->cluster_sectors;
1408 if (!zero_dry_run) {
1409 m_data.offset = VMDK_GTE_ZEROED;
1410 /* update L2 tables */
1411 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1412 return -EIO;
1415 } else {
1416 return -ENOTSUP;
1418 } else {
1419 ret = vmdk_write_extent(extent,
1420 cluster_offset, index_in_cluster * 512,
1421 buf, n, sector_num);
1422 if (ret) {
1423 return ret;
1425 if (m_data.valid) {
1426 /* update L2 tables */
1427 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1428 return -EIO;
1432 nb_sectors -= n;
1433 sector_num += n;
1434 buf += n * 512;
1436 /* update CID on the first write every time the virtual disk is
1437 * opened */
1438 if (!s->cid_updated) {
1439 ret = vmdk_write_cid(bs, time(NULL));
1440 if (ret < 0) {
1441 return ret;
1443 s->cid_updated = true;
1446 return 0;
1449 static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1450 const uint8_t *buf, int nb_sectors)
1452 int ret;
1453 BDRVVmdkState *s = bs->opaque;
1454 qemu_co_mutex_lock(&s->lock);
1455 ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1456 qemu_co_mutex_unlock(&s->lock);
1457 return ret;
1460 static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1461 int64_t sector_num,
1462 int nb_sectors,
1463 BdrvRequestFlags flags)
1465 int ret;
1466 BDRVVmdkState *s = bs->opaque;
1467 qemu_co_mutex_lock(&s->lock);
1468 /* write zeroes could fail if sectors not aligned to cluster, test it with
1469 * dry_run == true before really updating image */
1470 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1471 if (!ret) {
1472 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1474 qemu_co_mutex_unlock(&s->lock);
1475 return ret;
1478 static int vmdk_create_extent(const char *filename, int64_t filesize,
1479 bool flat, bool compress, bool zeroed_grain,
1480 Error **errp)
1482 int ret, i;
1483 BlockDriverState *bs = NULL;
1484 VMDK4Header header;
1485 Error *local_err;
1486 uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1487 uint32_t *gd_buf = NULL;
1488 int gd_buf_size;
1490 ret = bdrv_create_file(filename, NULL, &local_err);
1491 if (ret < 0) {
1492 error_propagate(errp, local_err);
1493 goto exit;
1496 ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
1497 if (ret < 0) {
1498 error_propagate(errp, local_err);
1499 goto exit;
1502 if (flat) {
1503 ret = bdrv_truncate(bs, filesize);
1504 if (ret < 0) {
1505 error_setg(errp, "Could not truncate file");
1507 goto exit;
1509 magic = cpu_to_be32(VMDK4_MAGIC);
1510 memset(&header, 0, sizeof(header));
1511 header.version = zeroed_grain ? 2 : 1;
1512 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1513 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1514 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1515 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1516 header.capacity = filesize / BDRV_SECTOR_SIZE;
1517 header.granularity = 128;
1518 header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
1520 grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1521 gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1522 BDRV_SECTOR_SIZE);
1523 gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1524 gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
1526 header.desc_offset = 1;
1527 header.desc_size = 20;
1528 header.rgd_offset = header.desc_offset + header.desc_size;
1529 header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
1530 header.grain_offset =
1531 ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1532 header.granularity);
1533 /* swap endianness for all header fields */
1534 header.version = cpu_to_le32(header.version);
1535 header.flags = cpu_to_le32(header.flags);
1536 header.capacity = cpu_to_le64(header.capacity);
1537 header.granularity = cpu_to_le64(header.granularity);
1538 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1539 header.desc_offset = cpu_to_le64(header.desc_offset);
1540 header.desc_size = cpu_to_le64(header.desc_size);
1541 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1542 header.gd_offset = cpu_to_le64(header.gd_offset);
1543 header.grain_offset = cpu_to_le64(header.grain_offset);
1544 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1546 header.check_bytes[0] = 0xa;
1547 header.check_bytes[1] = 0x20;
1548 header.check_bytes[2] = 0xd;
1549 header.check_bytes[3] = 0xa;
1551 /* write all the data */
1552 ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
1553 if (ret < 0) {
1554 error_set(errp, QERR_IO_ERROR);
1555 goto exit;
1557 ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
1558 if (ret < 0) {
1559 error_set(errp, QERR_IO_ERROR);
1560 goto exit;
1563 ret = bdrv_truncate(bs, le64_to_cpu(header.grain_offset) << 9);
1564 if (ret < 0) {
1565 error_setg(errp, "Could not truncate file");
1566 goto exit;
1569 /* write grain directory */
1570 gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1571 gd_buf = g_malloc0(gd_buf_size);
1572 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
1573 i < gt_count; i++, tmp += gt_size) {
1574 gd_buf[i] = cpu_to_le32(tmp);
1576 ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
1577 gd_buf, gd_buf_size);
1578 if (ret < 0) {
1579 error_set(errp, QERR_IO_ERROR);
1580 goto exit;
1583 /* write backup grain directory */
1584 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
1585 i < gt_count; i++, tmp += gt_size) {
1586 gd_buf[i] = cpu_to_le32(tmp);
1588 ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
1589 gd_buf, gd_buf_size);
1590 if (ret < 0) {
1591 error_set(errp, QERR_IO_ERROR);
1592 goto exit;
1595 ret = 0;
1596 exit:
1597 if (bs) {
1598 bdrv_unref(bs);
1600 g_free(gd_buf);
1601 return ret;
1604 static int filename_decompose(const char *filename, char *path, char *prefix,
1605 char *postfix, size_t buf_len, Error **errp)
1607 const char *p, *q;
1609 if (filename == NULL || !strlen(filename)) {
1610 error_setg(errp, "No filename provided");
1611 return VMDK_ERROR;
1613 p = strrchr(filename, '/');
1614 if (p == NULL) {
1615 p = strrchr(filename, '\\');
1617 if (p == NULL) {
1618 p = strrchr(filename, ':');
1620 if (p != NULL) {
1621 p++;
1622 if (p - filename >= buf_len) {
1623 return VMDK_ERROR;
1625 pstrcpy(path, p - filename + 1, filename);
1626 } else {
1627 p = filename;
1628 path[0] = '\0';
1630 q = strrchr(p, '.');
1631 if (q == NULL) {
1632 pstrcpy(prefix, buf_len, p);
1633 postfix[0] = '\0';
1634 } else {
1635 if (q - p >= buf_len) {
1636 return VMDK_ERROR;
1638 pstrcpy(prefix, q - p + 1, p);
1639 pstrcpy(postfix, buf_len, q);
1641 return VMDK_OK;
1644 static int vmdk_create(const char *filename, QEMUOptionParameter *options,
1645 Error **errp)
1647 int idx = 0;
1648 BlockDriverState *new_bs = NULL;
1649 Error *local_err;
1650 char *desc = NULL;
1651 int64_t total_size = 0, filesize;
1652 const char *adapter_type = NULL;
1653 const char *backing_file = NULL;
1654 const char *fmt = NULL;
1655 int flags = 0;
1656 int ret = 0;
1657 bool flat, split, compress;
1658 GString *ext_desc_lines;
1659 char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1660 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1661 const char *desc_extent_line;
1662 char parent_desc_line[BUF_SIZE] = "";
1663 uint32_t parent_cid = 0xffffffff;
1664 uint32_t number_heads = 16;
1665 bool zeroed_grain = false;
1666 uint32_t desc_offset = 0, desc_len;
1667 const char desc_template[] =
1668 "# Disk DescriptorFile\n"
1669 "version=1\n"
1670 "CID=%x\n"
1671 "parentCID=%x\n"
1672 "createType=\"%s\"\n"
1673 "%s"
1674 "\n"
1675 "# Extent description\n"
1676 "%s"
1677 "\n"
1678 "# The Disk Data Base\n"
1679 "#DDB\n"
1680 "\n"
1681 "ddb.virtualHWVersion = \"%d\"\n"
1682 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
1683 "ddb.geometry.heads = \"%d\"\n"
1684 "ddb.geometry.sectors = \"63\"\n"
1685 "ddb.adapterType = \"%s\"\n";
1687 ext_desc_lines = g_string_new(NULL);
1689 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
1690 ret = -EINVAL;
1691 goto exit;
1693 /* Read out options */
1694 while (options && options->name) {
1695 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1696 total_size = options->value.n;
1697 } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {
1698 adapter_type = options->value.s;
1699 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1700 backing_file = options->value.s;
1701 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1702 flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1703 } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1704 fmt = options->value.s;
1705 } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {
1706 zeroed_grain |= options->value.n;
1708 options++;
1710 if (!adapter_type) {
1711 adapter_type = "ide";
1712 } else if (strcmp(adapter_type, "ide") &&
1713 strcmp(adapter_type, "buslogic") &&
1714 strcmp(adapter_type, "lsilogic") &&
1715 strcmp(adapter_type, "legacyESX")) {
1716 error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
1717 ret = -EINVAL;
1718 goto exit;
1720 if (strcmp(adapter_type, "ide") != 0) {
1721 /* that's the number of heads with which vmware operates when
1722 creating, exporting, etc. vmdk files with a non-ide adapter type */
1723 number_heads = 255;
1725 if (!fmt) {
1726 /* Default format to monolithicSparse */
1727 fmt = "monolithicSparse";
1728 } else if (strcmp(fmt, "monolithicFlat") &&
1729 strcmp(fmt, "monolithicSparse") &&
1730 strcmp(fmt, "twoGbMaxExtentSparse") &&
1731 strcmp(fmt, "twoGbMaxExtentFlat") &&
1732 strcmp(fmt, "streamOptimized")) {
1733 error_setg(errp, "Unknown subformat: '%s'", fmt);
1734 ret = -EINVAL;
1735 goto exit;
1737 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1738 strcmp(fmt, "twoGbMaxExtentSparse"));
1739 flat = !(strcmp(fmt, "monolithicFlat") &&
1740 strcmp(fmt, "twoGbMaxExtentFlat"));
1741 compress = !strcmp(fmt, "streamOptimized");
1742 if (flat) {
1743 desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1744 } else {
1745 desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1747 if (flat && backing_file) {
1748 error_setg(errp, "Flat image can't have backing file");
1749 ret = -ENOTSUP;
1750 goto exit;
1752 if (flat && zeroed_grain) {
1753 error_setg(errp, "Flat image can't enable zeroed grain");
1754 ret = -ENOTSUP;
1755 goto exit;
1757 if (backing_file) {
1758 BlockDriverState *bs = bdrv_new("");
1759 ret = bdrv_open(bs, backing_file, NULL, BDRV_O_NO_BACKING, NULL, errp);
1760 if (ret != 0) {
1761 bdrv_unref(bs);
1762 goto exit;
1764 if (strcmp(bs->drv->format_name, "vmdk")) {
1765 bdrv_unref(bs);
1766 ret = -EINVAL;
1767 goto exit;
1769 parent_cid = vmdk_read_cid(bs, 0);
1770 bdrv_unref(bs);
1771 snprintf(parent_desc_line, sizeof(parent_desc_line),
1772 "parentFileNameHint=\"%s\"", backing_file);
1775 /* Create extents */
1776 filesize = total_size;
1777 while (filesize > 0) {
1778 char desc_line[BUF_SIZE];
1779 char ext_filename[PATH_MAX];
1780 char desc_filename[PATH_MAX];
1781 int64_t size = filesize;
1783 if (split && size > split_size) {
1784 size = split_size;
1786 if (split) {
1787 snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1788 prefix, flat ? 'f' : 's', ++idx, postfix);
1789 } else if (flat) {
1790 snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1791 prefix, postfix);
1792 } else {
1793 snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1794 prefix, postfix);
1796 snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1797 path, desc_filename);
1799 if (vmdk_create_extent(ext_filename, size,
1800 flat, compress, zeroed_grain, errp)) {
1801 ret = -EINVAL;
1802 goto exit;
1804 filesize -= size;
1806 /* Format description line */
1807 snprintf(desc_line, sizeof(desc_line),
1808 desc_extent_line, size / BDRV_SECTOR_SIZE, desc_filename);
1809 g_string_append(ext_desc_lines, desc_line);
1811 /* generate descriptor file */
1812 desc = g_strdup_printf(desc_template,
1813 (unsigned int)time(NULL),
1814 parent_cid,
1815 fmt,
1816 parent_desc_line,
1817 ext_desc_lines->str,
1818 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1819 total_size /
1820 (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
1821 number_heads,
1822 adapter_type);
1823 desc_len = strlen(desc);
1824 /* the descriptor offset = 0x200 */
1825 if (!split && !flat) {
1826 desc_offset = 0x200;
1827 } else {
1828 ret = bdrv_create_file(filename, options, &local_err);
1829 if (ret < 0) {
1830 error_setg_errno(errp, -ret, "Could not create image file");
1831 goto exit;
1834 ret = bdrv_file_open(&new_bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err);
1835 if (ret < 0) {
1836 error_setg_errno(errp, -ret, "Could not write description");
1837 goto exit;
1839 ret = bdrv_pwrite(new_bs, desc_offset, desc, desc_len);
1840 if (ret < 0) {
1841 error_setg_errno(errp, -ret, "Could not write description");
1842 goto exit;
1844 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
1845 * for description file */
1846 if (desc_offset == 0) {
1847 ret = bdrv_truncate(new_bs, desc_len);
1848 if (ret < 0) {
1849 error_setg(errp, "Could not truncate file");
1852 exit:
1853 if (new_bs) {
1854 bdrv_unref(new_bs);
1856 g_free(desc);
1857 g_string_free(ext_desc_lines, true);
1858 return ret;
1861 static void vmdk_close(BlockDriverState *bs)
1863 BDRVVmdkState *s = bs->opaque;
1865 vmdk_free_extents(bs);
1866 g_free(s->create_type);
1868 migrate_del_blocker(s->migration_blocker);
1869 error_free(s->migration_blocker);
1872 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
1874 BDRVVmdkState *s = bs->opaque;
1875 int i, err;
1876 int ret = 0;
1878 for (i = 0; i < s->num_extents; i++) {
1879 err = bdrv_co_flush(s->extents[i].file);
1880 if (err < 0) {
1881 ret = err;
1884 return ret;
1887 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1889 int i;
1890 int64_t ret = 0;
1891 int64_t r;
1892 BDRVVmdkState *s = bs->opaque;
1894 ret = bdrv_get_allocated_file_size(bs->file);
1895 if (ret < 0) {
1896 return ret;
1898 for (i = 0; i < s->num_extents; i++) {
1899 if (s->extents[i].file == bs->file) {
1900 continue;
1902 r = bdrv_get_allocated_file_size(s->extents[i].file);
1903 if (r < 0) {
1904 return r;
1906 ret += r;
1908 return ret;
1911 static int vmdk_has_zero_init(BlockDriverState *bs)
1913 int i;
1914 BDRVVmdkState *s = bs->opaque;
1916 /* If has a flat extent and its underlying storage doesn't have zero init,
1917 * return 0. */
1918 for (i = 0; i < s->num_extents; i++) {
1919 if (s->extents[i].flat) {
1920 if (!bdrv_has_zero_init(s->extents[i].file)) {
1921 return 0;
1925 return 1;
1928 static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
1930 ImageInfo *info = g_new0(ImageInfo, 1);
1932 *info = (ImageInfo){
1933 .filename = g_strdup(extent->file->filename),
1934 .format = g_strdup(extent->type),
1935 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
1936 .compressed = extent->compressed,
1937 .has_compressed = extent->compressed,
1938 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
1939 .has_cluster_size = !extent->flat,
1942 return info;
1945 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
1947 int i;
1948 BDRVVmdkState *s = bs->opaque;
1949 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
1950 ImageInfoList **next;
1952 *spec_info = (ImageInfoSpecific){
1953 .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
1955 .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
1959 *spec_info->vmdk = (ImageInfoSpecificVmdk) {
1960 .create_type = g_strdup(s->create_type),
1961 .cid = s->cid,
1962 .parent_cid = s->parent_cid,
1965 next = &spec_info->vmdk->extents;
1966 for (i = 0; i < s->num_extents; i++) {
1967 *next = g_new0(ImageInfoList, 1);
1968 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
1969 (*next)->next = NULL;
1970 next = &(*next)->next;
1973 return spec_info;
1976 static QEMUOptionParameter vmdk_create_options[] = {
1978 .name = BLOCK_OPT_SIZE,
1979 .type = OPT_SIZE,
1980 .help = "Virtual disk size"
1983 .name = BLOCK_OPT_ADAPTER_TYPE,
1984 .type = OPT_STRING,
1985 .help = "Virtual adapter type, can be one of "
1986 "ide (default), lsilogic, buslogic or legacyESX"
1989 .name = BLOCK_OPT_BACKING_FILE,
1990 .type = OPT_STRING,
1991 .help = "File name of a base image"
1994 .name = BLOCK_OPT_COMPAT6,
1995 .type = OPT_FLAG,
1996 .help = "VMDK version 6 image"
1999 .name = BLOCK_OPT_SUBFMT,
2000 .type = OPT_STRING,
2001 .help =
2002 "VMDK flat extent format, can be one of "
2003 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2006 .name = BLOCK_OPT_ZEROED_GRAIN,
2007 .type = OPT_FLAG,
2008 .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
2010 { NULL }
2013 static BlockDriver bdrv_vmdk = {
2014 .format_name = "vmdk",
2015 .instance_size = sizeof(BDRVVmdkState),
2016 .bdrv_probe = vmdk_probe,
2017 .bdrv_open = vmdk_open,
2018 .bdrv_reopen_prepare = vmdk_reopen_prepare,
2019 .bdrv_read = vmdk_co_read,
2020 .bdrv_write = vmdk_co_write,
2021 .bdrv_co_write_zeroes = vmdk_co_write_zeroes,
2022 .bdrv_close = vmdk_close,
2023 .bdrv_create = vmdk_create,
2024 .bdrv_co_flush_to_disk = vmdk_co_flush,
2025 .bdrv_co_get_block_status = vmdk_co_get_block_status,
2026 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2027 .bdrv_has_zero_init = vmdk_has_zero_init,
2028 .bdrv_get_specific_info = vmdk_get_specific_info,
2029 .bdrv_refresh_limits = vmdk_refresh_limits,
2031 .create_options = vmdk_create_options,
2034 static void bdrv_vmdk_init(void)
2036 bdrv_register(&bdrv_vmdk);
2039 block_init(bdrv_vmdk_init);