Merge branch 'upstream-merge'
[qemu-kvm.git] / block / vmdk.c
blob1a80e5a247f53b7b86aad7fce8d1c5f05330e5b4
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_int.h"
28 #include "module.h"
29 #include "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_RGD (1 << 1)
36 #define VMDK4_FLAG_COMPRESS (1 << 16)
37 #define VMDK4_FLAG_MARKER (1 << 17)
38 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
40 typedef struct {
41 uint32_t version;
42 uint32_t flags;
43 uint32_t disk_sectors;
44 uint32_t granularity;
45 uint32_t l1dir_offset;
46 uint32_t l1dir_size;
47 uint32_t file_sectors;
48 uint32_t cylinders;
49 uint32_t heads;
50 uint32_t sectors_per_track;
51 } VMDK3Header;
53 typedef struct {
54 uint32_t version;
55 uint32_t flags;
56 int64_t capacity;
57 int64_t granularity;
58 int64_t desc_offset;
59 int64_t desc_size;
60 int32_t num_gtes_per_gte;
61 int64_t rgd_offset;
62 int64_t gd_offset;
63 int64_t grain_offset;
64 char filler[1];
65 char check_bytes[4];
66 uint16_t compressAlgorithm;
67 } QEMU_PACKED VMDK4Header;
69 #define L2_CACHE_SIZE 16
71 typedef struct VmdkExtent {
72 BlockDriverState *file;
73 bool flat;
74 bool compressed;
75 bool has_marker;
76 int64_t sectors;
77 int64_t end_sector;
78 int64_t flat_start_offset;
79 int64_t l1_table_offset;
80 int64_t l1_backup_table_offset;
81 uint32_t *l1_table;
82 uint32_t *l1_backup_table;
83 unsigned int l1_size;
84 uint32_t l1_entry_sectors;
86 unsigned int l2_size;
87 uint32_t *l2_cache;
88 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
89 uint32_t l2_cache_counts[L2_CACHE_SIZE];
91 unsigned int cluster_sectors;
92 } VmdkExtent;
94 typedef struct BDRVVmdkState {
95 CoMutex lock;
96 int desc_offset;
97 bool cid_updated;
98 uint32_t parent_cid;
99 int num_extents;
100 /* Extent array with num_extents entries, ascend ordered by address */
101 VmdkExtent *extents;
102 Error *migration_blocker;
103 } BDRVVmdkState;
105 typedef struct VmdkMetaData {
106 uint32_t offset;
107 unsigned int l1_index;
108 unsigned int l2_index;
109 unsigned int l2_offset;
110 int valid;
111 } VmdkMetaData;
113 typedef struct VmdkGrainMarker {
114 uint64_t lba;
115 uint32_t size;
116 uint8_t data[0];
117 } VmdkGrainMarker;
119 enum {
120 MARKER_END_OF_STREAM = 0,
121 MARKER_GRAIN_TABLE = 1,
122 MARKER_GRAIN_DIRECTORY = 2,
123 MARKER_FOOTER = 3,
126 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
128 uint32_t magic;
130 if (buf_size < 4) {
131 return 0;
133 magic = be32_to_cpu(*(uint32_t *)buf);
134 if (magic == VMDK3_MAGIC ||
135 magic == VMDK4_MAGIC) {
136 return 100;
137 } else {
138 const char *p = (const char *)buf;
139 const char *end = p + buf_size;
140 while (p < end) {
141 if (*p == '#') {
142 /* skip comment line */
143 while (p < end && *p != '\n') {
144 p++;
146 p++;
147 continue;
149 if (*p == ' ') {
150 while (p < end && *p == ' ') {
151 p++;
153 /* skip '\r' if windows line endings used. */
154 if (p < end && *p == '\r') {
155 p++;
157 /* only accept blank lines before 'version=' line */
158 if (p == end || *p != '\n') {
159 return 0;
161 p++;
162 continue;
164 if (end - p >= strlen("version=X\n")) {
165 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
166 strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
167 return 100;
170 if (end - p >= strlen("version=X\r\n")) {
171 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
172 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
173 return 100;
176 return 0;
178 return 0;
182 #define CHECK_CID 1
184 #define SECTOR_SIZE 512
185 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
186 #define BUF_SIZE 4096
187 #define HEADER_SIZE 512 /* first sector of 512 bytes */
189 static void vmdk_free_extents(BlockDriverState *bs)
191 int i;
192 BDRVVmdkState *s = bs->opaque;
193 VmdkExtent *e;
195 for (i = 0; i < s->num_extents; i++) {
196 e = &s->extents[i];
197 g_free(e->l1_table);
198 g_free(e->l2_cache);
199 g_free(e->l1_backup_table);
200 if (e->file != bs->file) {
201 bdrv_delete(e->file);
204 g_free(s->extents);
207 static void vmdk_free_last_extent(BlockDriverState *bs)
209 BDRVVmdkState *s = bs->opaque;
211 if (s->num_extents == 0) {
212 return;
214 s->num_extents--;
215 s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
218 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
220 char desc[DESC_SIZE];
221 uint32_t cid = 0xffffffff;
222 const char *p_name, *cid_str;
223 size_t cid_str_size;
224 BDRVVmdkState *s = bs->opaque;
225 int ret;
227 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
228 if (ret < 0) {
229 return 0;
232 if (parent) {
233 cid_str = "parentCID";
234 cid_str_size = sizeof("parentCID");
235 } else {
236 cid_str = "CID";
237 cid_str_size = sizeof("CID");
240 desc[DESC_SIZE - 1] = '\0';
241 p_name = strstr(desc, cid_str);
242 if (p_name != NULL) {
243 p_name += cid_str_size;
244 sscanf(p_name, "%x", &cid);
247 return cid;
250 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
252 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
253 char *p_name, *tmp_str;
254 BDRVVmdkState *s = bs->opaque;
255 int ret;
257 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
258 if (ret < 0) {
259 return ret;
262 desc[DESC_SIZE - 1] = '\0';
263 tmp_str = strstr(desc, "parentCID");
264 if (tmp_str == NULL) {
265 return -EINVAL;
268 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
269 p_name = strstr(desc, "CID");
270 if (p_name != NULL) {
271 p_name += sizeof("CID");
272 snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
273 pstrcat(desc, sizeof(desc), tmp_desc);
276 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
277 if (ret < 0) {
278 return ret;
281 return 0;
284 static int vmdk_is_cid_valid(BlockDriverState *bs)
286 #ifdef CHECK_CID
287 BDRVVmdkState *s = bs->opaque;
288 BlockDriverState *p_bs = bs->backing_hd;
289 uint32_t cur_pcid;
291 if (p_bs) {
292 cur_pcid = vmdk_read_cid(p_bs, 0);
293 if (s->parent_cid != cur_pcid) {
294 /* CID not valid */
295 return 0;
298 #endif
299 /* CID valid */
300 return 1;
303 /* Queue extents, if any, for reopen() */
304 static int vmdk_reopen_prepare(BDRVReopenState *state,
305 BlockReopenQueue *queue, Error **errp)
307 BDRVVmdkState *s;
308 int ret = -1;
309 int i;
310 VmdkExtent *e;
312 assert(state != NULL);
313 assert(state->bs != NULL);
315 if (queue == NULL) {
316 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
317 "No reopen queue for VMDK extents");
318 goto exit;
321 s = state->bs->opaque;
323 assert(s != NULL);
325 for (i = 0; i < s->num_extents; i++) {
326 e = &s->extents[i];
327 if (e->file != state->bs->file) {
328 bdrv_reopen_queue(queue, e->file, state->flags);
331 ret = 0;
333 exit:
334 return ret;
337 static int vmdk_parent_open(BlockDriverState *bs)
339 char *p_name;
340 char desc[DESC_SIZE + 1];
341 BDRVVmdkState *s = bs->opaque;
342 int ret;
344 desc[DESC_SIZE] = '\0';
345 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
346 if (ret < 0) {
347 return ret;
350 p_name = strstr(desc, "parentFileNameHint");
351 if (p_name != NULL) {
352 char *end_name;
354 p_name += sizeof("parentFileNameHint") + 1;
355 end_name = strchr(p_name, '\"');
356 if (end_name == NULL) {
357 return -EINVAL;
359 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
360 return -EINVAL;
363 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
366 return 0;
369 /* Create and append extent to the extent array. Return the added VmdkExtent
370 * address. return NULL if allocation failed. */
371 static VmdkExtent *vmdk_add_extent(BlockDriverState *bs,
372 BlockDriverState *file, bool flat, int64_t sectors,
373 int64_t l1_offset, int64_t l1_backup_offset,
374 uint32_t l1_size,
375 int l2_size, unsigned int cluster_sectors)
377 VmdkExtent *extent;
378 BDRVVmdkState *s = bs->opaque;
380 s->extents = g_realloc(s->extents,
381 (s->num_extents + 1) * sizeof(VmdkExtent));
382 extent = &s->extents[s->num_extents];
383 s->num_extents++;
385 memset(extent, 0, sizeof(VmdkExtent));
386 extent->file = file;
387 extent->flat = flat;
388 extent->sectors = sectors;
389 extent->l1_table_offset = l1_offset;
390 extent->l1_backup_table_offset = l1_backup_offset;
391 extent->l1_size = l1_size;
392 extent->l1_entry_sectors = l2_size * cluster_sectors;
393 extent->l2_size = l2_size;
394 extent->cluster_sectors = cluster_sectors;
396 if (s->num_extents > 1) {
397 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
398 } else {
399 extent->end_sector = extent->sectors;
401 bs->total_sectors = extent->end_sector;
402 return extent;
405 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
407 int ret;
408 int l1_size, i;
410 /* read the L1 table */
411 l1_size = extent->l1_size * sizeof(uint32_t);
412 extent->l1_table = g_malloc(l1_size);
413 ret = bdrv_pread(extent->file,
414 extent->l1_table_offset,
415 extent->l1_table,
416 l1_size);
417 if (ret < 0) {
418 goto fail_l1;
420 for (i = 0; i < extent->l1_size; i++) {
421 le32_to_cpus(&extent->l1_table[i]);
424 if (extent->l1_backup_table_offset) {
425 extent->l1_backup_table = g_malloc(l1_size);
426 ret = bdrv_pread(extent->file,
427 extent->l1_backup_table_offset,
428 extent->l1_backup_table,
429 l1_size);
430 if (ret < 0) {
431 goto fail_l1b;
433 for (i = 0; i < extent->l1_size; i++) {
434 le32_to_cpus(&extent->l1_backup_table[i]);
438 extent->l2_cache =
439 g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
440 return 0;
441 fail_l1b:
442 g_free(extent->l1_backup_table);
443 fail_l1:
444 g_free(extent->l1_table);
445 return ret;
448 static int vmdk_open_vmdk3(BlockDriverState *bs,
449 BlockDriverState *file,
450 int flags)
452 int ret;
453 uint32_t magic;
454 VMDK3Header header;
455 VmdkExtent *extent;
457 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
458 if (ret < 0) {
459 return ret;
461 extent = vmdk_add_extent(bs,
462 bs->file, false,
463 le32_to_cpu(header.disk_sectors),
464 le32_to_cpu(header.l1dir_offset) << 9,
465 0, 1 << 6, 1 << 9,
466 le32_to_cpu(header.granularity));
467 ret = vmdk_init_tables(bs, extent);
468 if (ret) {
469 /* free extent allocated by vmdk_add_extent */
470 vmdk_free_last_extent(bs);
472 return ret;
475 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
476 int64_t desc_offset);
478 static int vmdk_open_vmdk4(BlockDriverState *bs,
479 BlockDriverState *file,
480 int flags)
482 int ret;
483 uint32_t magic;
484 uint32_t l1_size, l1_entry_sectors;
485 VMDK4Header header;
486 VmdkExtent *extent;
487 int64_t l1_backup_offset = 0;
489 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
490 if (ret < 0) {
491 return ret;
493 if (header.capacity == 0 && header.desc_offset) {
494 return vmdk_open_desc_file(bs, flags, header.desc_offset << 9);
497 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
499 * The footer takes precedence over the header, so read it in. The
500 * footer starts at offset -1024 from the end: One sector for the
501 * footer, and another one for the end-of-stream marker.
503 struct {
504 struct {
505 uint64_t val;
506 uint32_t size;
507 uint32_t type;
508 uint8_t pad[512 - 16];
509 } QEMU_PACKED footer_marker;
511 uint32_t magic;
512 VMDK4Header header;
513 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
515 struct {
516 uint64_t val;
517 uint32_t size;
518 uint32_t type;
519 uint8_t pad[512 - 16];
520 } QEMU_PACKED eos_marker;
521 } QEMU_PACKED footer;
523 ret = bdrv_pread(file,
524 bs->file->total_sectors * 512 - 1536,
525 &footer, sizeof(footer));
526 if (ret < 0) {
527 return ret;
530 /* Some sanity checks for the footer */
531 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
532 le32_to_cpu(footer.footer_marker.size) != 0 ||
533 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
534 le64_to_cpu(footer.eos_marker.val) != 0 ||
535 le32_to_cpu(footer.eos_marker.size) != 0 ||
536 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
538 return -EINVAL;
541 header = footer.header;
544 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gte)
545 * le64_to_cpu(header.granularity);
546 if (l1_entry_sectors == 0) {
547 return -EINVAL;
549 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
550 / l1_entry_sectors;
551 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
552 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
554 extent = vmdk_add_extent(bs, file, false,
555 le64_to_cpu(header.capacity),
556 le64_to_cpu(header.gd_offset) << 9,
557 l1_backup_offset,
558 l1_size,
559 le32_to_cpu(header.num_gtes_per_gte),
560 le64_to_cpu(header.granularity));
561 extent->compressed =
562 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
563 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
564 ret = vmdk_init_tables(bs, extent);
565 if (ret) {
566 /* free extent allocated by vmdk_add_extent */
567 vmdk_free_last_extent(bs);
569 return ret;
572 /* find an option value out of descriptor file */
573 static int vmdk_parse_description(const char *desc, const char *opt_name,
574 char *buf, int buf_size)
576 char *opt_pos, *opt_end;
577 const char *end = desc + strlen(desc);
579 opt_pos = strstr(desc, opt_name);
580 if (!opt_pos) {
581 return -1;
583 /* Skip "=\"" following opt_name */
584 opt_pos += strlen(opt_name) + 2;
585 if (opt_pos >= end) {
586 return -1;
588 opt_end = opt_pos;
589 while (opt_end < end && *opt_end != '"') {
590 opt_end++;
592 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
593 return -1;
595 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
596 return 0;
599 /* Open an extent file and append to bs array */
600 static int vmdk_open_sparse(BlockDriverState *bs,
601 BlockDriverState *file,
602 int flags)
604 uint32_t magic;
606 if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
607 return -EIO;
610 magic = be32_to_cpu(magic);
611 switch (magic) {
612 case VMDK3_MAGIC:
613 return vmdk_open_vmdk3(bs, file, flags);
614 break;
615 case VMDK4_MAGIC:
616 return vmdk_open_vmdk4(bs, file, flags);
617 break;
618 default:
619 return -EINVAL;
620 break;
624 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
625 const char *desc_file_path)
627 int ret;
628 char access[11];
629 char type[11];
630 char fname[512];
631 const char *p = desc;
632 int64_t sectors = 0;
633 int64_t flat_offset;
634 char extent_path[PATH_MAX];
635 BlockDriverState *extent_file;
637 while (*p) {
638 /* parse extent line:
639 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
640 * or
641 * RW [size in sectors] SPARSE "file-name.vmdk"
643 flat_offset = -1;
644 ret = sscanf(p, "%10s %" SCNd64 " %10s %511s %" SCNd64,
645 access, &sectors, type, fname, &flat_offset);
646 if (ret < 4 || strcmp(access, "RW")) {
647 goto next_line;
648 } else if (!strcmp(type, "FLAT")) {
649 if (ret != 5 || flat_offset < 0) {
650 return -EINVAL;
652 } else if (ret != 4) {
653 return -EINVAL;
656 /* trim the quotation marks around */
657 if (fname[0] == '"') {
658 memmove(fname, fname + 1, strlen(fname));
659 if (strlen(fname) <= 1 || fname[strlen(fname) - 1] != '"') {
660 return -EINVAL;
662 fname[strlen(fname) - 1] = '\0';
664 if (sectors <= 0 ||
665 (strcmp(type, "FLAT") && strcmp(type, "SPARSE")) ||
666 (strcmp(access, "RW"))) {
667 goto next_line;
670 path_combine(extent_path, sizeof(extent_path),
671 desc_file_path, fname);
672 ret = bdrv_file_open(&extent_file, extent_path, bs->open_flags);
673 if (ret) {
674 return ret;
677 /* save to extents array */
678 if (!strcmp(type, "FLAT")) {
679 /* FLAT extent */
680 VmdkExtent *extent;
682 extent = vmdk_add_extent(bs, extent_file, true, sectors,
683 0, 0, 0, 0, sectors);
684 extent->flat_start_offset = flat_offset << 9;
685 } else if (!strcmp(type, "SPARSE")) {
686 /* SPARSE extent */
687 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags);
688 if (ret) {
689 bdrv_delete(extent_file);
690 return ret;
692 } else {
693 fprintf(stderr,
694 "VMDK: Not supported extent type \"%s\""".\n", type);
695 return -ENOTSUP;
697 next_line:
698 /* move to next line */
699 while (*p && *p != '\n') {
700 p++;
702 p++;
704 return 0;
707 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
708 int64_t desc_offset)
710 int ret;
711 char buf[2048];
712 char ct[128];
713 BDRVVmdkState *s = bs->opaque;
715 ret = bdrv_pread(bs->file, desc_offset, buf, sizeof(buf));
716 if (ret < 0) {
717 return ret;
719 buf[2047] = '\0';
720 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
721 return -EINVAL;
723 if (strcmp(ct, "monolithicFlat") &&
724 strcmp(ct, "twoGbMaxExtentSparse") &&
725 strcmp(ct, "twoGbMaxExtentFlat")) {
726 fprintf(stderr,
727 "VMDK: Not supported image type \"%s\""".\n", ct);
728 return -ENOTSUP;
730 s->desc_offset = 0;
731 return vmdk_parse_extents(buf, bs, bs->file->filename);
734 static int vmdk_open(BlockDriverState *bs, int flags)
736 int ret;
737 BDRVVmdkState *s = bs->opaque;
739 if (vmdk_open_sparse(bs, bs->file, flags) == 0) {
740 s->desc_offset = 0x200;
741 } else {
742 ret = vmdk_open_desc_file(bs, flags, 0);
743 if (ret) {
744 goto fail;
747 /* try to open parent images, if exist */
748 ret = vmdk_parent_open(bs);
749 if (ret) {
750 goto fail;
752 s->parent_cid = vmdk_read_cid(bs, 1);
753 qemu_co_mutex_init(&s->lock);
755 /* Disable migration when VMDK images are used */
756 error_set(&s->migration_blocker,
757 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
758 "vmdk", bs->device_name, "live migration");
759 migrate_add_blocker(s->migration_blocker);
761 return 0;
763 fail:
764 vmdk_free_extents(bs);
765 return ret;
768 static int get_whole_cluster(BlockDriverState *bs,
769 VmdkExtent *extent,
770 uint64_t cluster_offset,
771 uint64_t offset,
772 bool allocate)
774 /* 128 sectors * 512 bytes each = grain size 64KB */
775 uint8_t whole_grain[extent->cluster_sectors * 512];
777 /* we will be here if it's first write on non-exist grain(cluster).
778 * try to read from parent image, if exist */
779 if (bs->backing_hd) {
780 int ret;
782 if (!vmdk_is_cid_valid(bs)) {
783 return -1;
786 /* floor offset to cluster */
787 offset -= offset % (extent->cluster_sectors * 512);
788 ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
789 extent->cluster_sectors);
790 if (ret < 0) {
791 return -1;
794 /* Write grain only into the active image */
795 ret = bdrv_write(extent->file, cluster_offset, whole_grain,
796 extent->cluster_sectors);
797 if (ret < 0) {
798 return -1;
801 return 0;
804 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
806 /* update L2 table */
807 if (bdrv_pwrite_sync(
808 extent->file,
809 ((int64_t)m_data->l2_offset * 512)
810 + (m_data->l2_index * sizeof(m_data->offset)),
811 &(m_data->offset),
812 sizeof(m_data->offset)
813 ) < 0) {
814 return -1;
816 /* update backup L2 table */
817 if (extent->l1_backup_table_offset != 0) {
818 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
819 if (bdrv_pwrite_sync(
820 extent->file,
821 ((int64_t)m_data->l2_offset * 512)
822 + (m_data->l2_index * sizeof(m_data->offset)),
823 &(m_data->offset), sizeof(m_data->offset)
824 ) < 0) {
825 return -1;
829 return 0;
832 static int get_cluster_offset(BlockDriverState *bs,
833 VmdkExtent *extent,
834 VmdkMetaData *m_data,
835 uint64_t offset,
836 int allocate,
837 uint64_t *cluster_offset)
839 unsigned int l1_index, l2_offset, l2_index;
840 int min_index, i, j;
841 uint32_t min_count, *l2_table, tmp = 0;
843 if (m_data) {
844 m_data->valid = 0;
846 if (extent->flat) {
847 *cluster_offset = extent->flat_start_offset;
848 return 0;
851 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
852 l1_index = (offset >> 9) / extent->l1_entry_sectors;
853 if (l1_index >= extent->l1_size) {
854 return -1;
856 l2_offset = extent->l1_table[l1_index];
857 if (!l2_offset) {
858 return -1;
860 for (i = 0; i < L2_CACHE_SIZE; i++) {
861 if (l2_offset == extent->l2_cache_offsets[i]) {
862 /* increment the hit count */
863 if (++extent->l2_cache_counts[i] == 0xffffffff) {
864 for (j = 0; j < L2_CACHE_SIZE; j++) {
865 extent->l2_cache_counts[j] >>= 1;
868 l2_table = extent->l2_cache + (i * extent->l2_size);
869 goto found;
872 /* not found: load a new entry in the least used one */
873 min_index = 0;
874 min_count = 0xffffffff;
875 for (i = 0; i < L2_CACHE_SIZE; i++) {
876 if (extent->l2_cache_counts[i] < min_count) {
877 min_count = extent->l2_cache_counts[i];
878 min_index = i;
881 l2_table = extent->l2_cache + (min_index * extent->l2_size);
882 if (bdrv_pread(
883 extent->file,
884 (int64_t)l2_offset * 512,
885 l2_table,
886 extent->l2_size * sizeof(uint32_t)
887 ) != extent->l2_size * sizeof(uint32_t)) {
888 return -1;
891 extent->l2_cache_offsets[min_index] = l2_offset;
892 extent->l2_cache_counts[min_index] = 1;
893 found:
894 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
895 *cluster_offset = le32_to_cpu(l2_table[l2_index]);
897 if (!*cluster_offset) {
898 if (!allocate) {
899 return -1;
902 /* Avoid the L2 tables update for the images that have snapshots. */
903 *cluster_offset = bdrv_getlength(extent->file);
904 if (!extent->compressed) {
905 bdrv_truncate(
906 extent->file,
907 *cluster_offset + (extent->cluster_sectors << 9)
911 *cluster_offset >>= 9;
912 tmp = cpu_to_le32(*cluster_offset);
913 l2_table[l2_index] = tmp;
915 /* First of all we write grain itself, to avoid race condition
916 * that may to corrupt the image.
917 * This problem may occur because of insufficient space on host disk
918 * or inappropriate VM shutdown.
920 if (get_whole_cluster(
921 bs, extent, *cluster_offset, offset, allocate) == -1) {
922 return -1;
925 if (m_data) {
926 m_data->offset = tmp;
927 m_data->l1_index = l1_index;
928 m_data->l2_index = l2_index;
929 m_data->l2_offset = l2_offset;
930 m_data->valid = 1;
933 *cluster_offset <<= 9;
934 return 0;
937 static VmdkExtent *find_extent(BDRVVmdkState *s,
938 int64_t sector_num, VmdkExtent *start_hint)
940 VmdkExtent *extent = start_hint;
942 if (!extent) {
943 extent = &s->extents[0];
945 while (extent < &s->extents[s->num_extents]) {
946 if (sector_num < extent->end_sector) {
947 return extent;
949 extent++;
951 return NULL;
954 static int coroutine_fn vmdk_co_is_allocated(BlockDriverState *bs,
955 int64_t sector_num, int nb_sectors, int *pnum)
957 BDRVVmdkState *s = bs->opaque;
958 int64_t index_in_cluster, n, ret;
959 uint64_t offset;
960 VmdkExtent *extent;
962 extent = find_extent(s, sector_num, NULL);
963 if (!extent) {
964 return 0;
966 qemu_co_mutex_lock(&s->lock);
967 ret = get_cluster_offset(bs, extent, NULL,
968 sector_num * 512, 0, &offset);
969 qemu_co_mutex_unlock(&s->lock);
970 /* get_cluster_offset returning 0 means success */
971 ret = !ret;
973 index_in_cluster = sector_num % extent->cluster_sectors;
974 n = extent->cluster_sectors - index_in_cluster;
975 if (n > nb_sectors) {
976 n = nb_sectors;
978 *pnum = n;
979 return ret;
982 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
983 int64_t offset_in_cluster, const uint8_t *buf,
984 int nb_sectors, int64_t sector_num)
986 int ret;
987 VmdkGrainMarker *data = NULL;
988 uLongf buf_len;
989 const uint8_t *write_buf = buf;
990 int write_len = nb_sectors * 512;
992 if (extent->compressed) {
993 if (!extent->has_marker) {
994 ret = -EINVAL;
995 goto out;
997 buf_len = (extent->cluster_sectors << 9) * 2;
998 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
999 if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1000 buf_len == 0) {
1001 ret = -EINVAL;
1002 goto out;
1004 data->lba = sector_num;
1005 data->size = buf_len;
1006 write_buf = (uint8_t *)data;
1007 write_len = buf_len + sizeof(VmdkGrainMarker);
1009 ret = bdrv_pwrite(extent->file,
1010 cluster_offset + offset_in_cluster,
1011 write_buf,
1012 write_len);
1013 if (ret != write_len) {
1014 ret = ret < 0 ? ret : -EIO;
1015 goto out;
1017 ret = 0;
1018 out:
1019 g_free(data);
1020 return ret;
1023 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1024 int64_t offset_in_cluster, uint8_t *buf,
1025 int nb_sectors)
1027 int ret;
1028 int cluster_bytes, buf_bytes;
1029 uint8_t *cluster_buf, *compressed_data;
1030 uint8_t *uncomp_buf;
1031 uint32_t data_len;
1032 VmdkGrainMarker *marker;
1033 uLongf buf_len;
1036 if (!extent->compressed) {
1037 ret = bdrv_pread(extent->file,
1038 cluster_offset + offset_in_cluster,
1039 buf, nb_sectors * 512);
1040 if (ret == nb_sectors * 512) {
1041 return 0;
1042 } else {
1043 return -EIO;
1046 cluster_bytes = extent->cluster_sectors * 512;
1047 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1048 buf_bytes = cluster_bytes * 2;
1049 cluster_buf = g_malloc(buf_bytes);
1050 uncomp_buf = g_malloc(cluster_bytes);
1051 ret = bdrv_pread(extent->file,
1052 cluster_offset,
1053 cluster_buf, buf_bytes);
1054 if (ret < 0) {
1055 goto out;
1057 compressed_data = cluster_buf;
1058 buf_len = cluster_bytes;
1059 data_len = cluster_bytes;
1060 if (extent->has_marker) {
1061 marker = (VmdkGrainMarker *)cluster_buf;
1062 compressed_data = marker->data;
1063 data_len = le32_to_cpu(marker->size);
1065 if (!data_len || data_len > buf_bytes) {
1066 ret = -EINVAL;
1067 goto out;
1069 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1070 if (ret != Z_OK) {
1071 ret = -EINVAL;
1072 goto out;
1075 if (offset_in_cluster < 0 ||
1076 offset_in_cluster + nb_sectors * 512 > buf_len) {
1077 ret = -EINVAL;
1078 goto out;
1080 memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1081 ret = 0;
1083 out:
1084 g_free(uncomp_buf);
1085 g_free(cluster_buf);
1086 return ret;
1089 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
1090 uint8_t *buf, int nb_sectors)
1092 BDRVVmdkState *s = bs->opaque;
1093 int ret;
1094 uint64_t n, index_in_cluster;
1095 VmdkExtent *extent = NULL;
1096 uint64_t cluster_offset;
1098 while (nb_sectors > 0) {
1099 extent = find_extent(s, sector_num, extent);
1100 if (!extent) {
1101 return -EIO;
1103 ret = get_cluster_offset(
1104 bs, extent, NULL,
1105 sector_num << 9, 0, &cluster_offset);
1106 index_in_cluster = sector_num % extent->cluster_sectors;
1107 n = extent->cluster_sectors - index_in_cluster;
1108 if (n > nb_sectors) {
1109 n = nb_sectors;
1111 if (ret) {
1112 /* if not allocated, try to read from parent image, if exist */
1113 if (bs->backing_hd) {
1114 if (!vmdk_is_cid_valid(bs)) {
1115 return -EINVAL;
1117 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
1118 if (ret < 0) {
1119 return ret;
1121 } else {
1122 memset(buf, 0, 512 * n);
1124 } else {
1125 ret = vmdk_read_extent(extent,
1126 cluster_offset, index_in_cluster * 512,
1127 buf, n);
1128 if (ret) {
1129 return ret;
1132 nb_sectors -= n;
1133 sector_num += n;
1134 buf += n * 512;
1136 return 0;
1139 static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1140 uint8_t *buf, int nb_sectors)
1142 int ret;
1143 BDRVVmdkState *s = bs->opaque;
1144 qemu_co_mutex_lock(&s->lock);
1145 ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1146 qemu_co_mutex_unlock(&s->lock);
1147 return ret;
1150 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
1151 const uint8_t *buf, int nb_sectors)
1153 BDRVVmdkState *s = bs->opaque;
1154 VmdkExtent *extent = NULL;
1155 int n, ret;
1156 int64_t index_in_cluster;
1157 uint64_t cluster_offset;
1158 VmdkMetaData m_data;
1160 if (sector_num > bs->total_sectors) {
1161 fprintf(stderr,
1162 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
1163 " total_sectors=0x%" PRIx64 "\n",
1164 sector_num, bs->total_sectors);
1165 return -EIO;
1168 while (nb_sectors > 0) {
1169 extent = find_extent(s, sector_num, extent);
1170 if (!extent) {
1171 return -EIO;
1173 ret = get_cluster_offset(
1175 extent,
1176 &m_data,
1177 sector_num << 9, !extent->compressed,
1178 &cluster_offset);
1179 if (extent->compressed) {
1180 if (ret == 0) {
1181 /* Refuse write to allocated cluster for streamOptimized */
1182 fprintf(stderr,
1183 "VMDK: can't write to allocated cluster"
1184 " for streamOptimized\n");
1185 return -EIO;
1186 } else {
1187 /* allocate */
1188 ret = get_cluster_offset(
1190 extent,
1191 &m_data,
1192 sector_num << 9, 1,
1193 &cluster_offset);
1196 if (ret) {
1197 return -EINVAL;
1199 index_in_cluster = sector_num % extent->cluster_sectors;
1200 n = extent->cluster_sectors - index_in_cluster;
1201 if (n > nb_sectors) {
1202 n = nb_sectors;
1205 ret = vmdk_write_extent(extent,
1206 cluster_offset, index_in_cluster * 512,
1207 buf, n, sector_num);
1208 if (ret) {
1209 return ret;
1211 if (m_data.valid) {
1212 /* update L2 tables */
1213 if (vmdk_L2update(extent, &m_data) == -1) {
1214 return -EIO;
1217 nb_sectors -= n;
1218 sector_num += n;
1219 buf += n * 512;
1221 /* update CID on the first write every time the virtual disk is
1222 * opened */
1223 if (!s->cid_updated) {
1224 ret = vmdk_write_cid(bs, time(NULL));
1225 if (ret < 0) {
1226 return ret;
1228 s->cid_updated = true;
1231 return 0;
1234 static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1235 const uint8_t *buf, int nb_sectors)
1237 int ret;
1238 BDRVVmdkState *s = bs->opaque;
1239 qemu_co_mutex_lock(&s->lock);
1240 ret = vmdk_write(bs, sector_num, buf, nb_sectors);
1241 qemu_co_mutex_unlock(&s->lock);
1242 return ret;
1246 static int vmdk_create_extent(const char *filename, int64_t filesize,
1247 bool flat, bool compress)
1249 int ret, i;
1250 int fd = 0;
1251 VMDK4Header header;
1252 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
1254 fd = qemu_open(filename,
1255 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1256 0644);
1257 if (fd < 0) {
1258 return -errno;
1260 if (flat) {
1261 ret = ftruncate(fd, filesize);
1262 if (ret < 0) {
1263 ret = -errno;
1265 goto exit;
1267 magic = cpu_to_be32(VMDK4_MAGIC);
1268 memset(&header, 0, sizeof(header));
1269 header.version = 1;
1270 header.flags =
1271 3 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0);
1272 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1273 header.capacity = filesize / 512;
1274 header.granularity = 128;
1275 header.num_gtes_per_gte = 512;
1277 grains = (filesize / 512 + header.granularity - 1) / header.granularity;
1278 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
1279 gt_count =
1280 (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
1281 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
1283 header.desc_offset = 1;
1284 header.desc_size = 20;
1285 header.rgd_offset = header.desc_offset + header.desc_size;
1286 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
1287 header.grain_offset =
1288 ((header.gd_offset + gd_size + (gt_size * gt_count) +
1289 header.granularity - 1) / header.granularity) *
1290 header.granularity;
1291 /* swap endianness for all header fields */
1292 header.version = cpu_to_le32(header.version);
1293 header.flags = cpu_to_le32(header.flags);
1294 header.capacity = cpu_to_le64(header.capacity);
1295 header.granularity = cpu_to_le64(header.granularity);
1296 header.num_gtes_per_gte = cpu_to_le32(header.num_gtes_per_gte);
1297 header.desc_offset = cpu_to_le64(header.desc_offset);
1298 header.desc_size = cpu_to_le64(header.desc_size);
1299 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1300 header.gd_offset = cpu_to_le64(header.gd_offset);
1301 header.grain_offset = cpu_to_le64(header.grain_offset);
1302 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1304 header.check_bytes[0] = 0xa;
1305 header.check_bytes[1] = 0x20;
1306 header.check_bytes[2] = 0xd;
1307 header.check_bytes[3] = 0xa;
1309 /* write all the data */
1310 ret = qemu_write_full(fd, &magic, sizeof(magic));
1311 if (ret != sizeof(magic)) {
1312 ret = -errno;
1313 goto exit;
1315 ret = qemu_write_full(fd, &header, sizeof(header));
1316 if (ret != sizeof(header)) {
1317 ret = -errno;
1318 goto exit;
1321 ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
1322 if (ret < 0) {
1323 ret = -errno;
1324 goto exit;
1327 /* write grain directory */
1328 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
1329 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
1330 i < gt_count; i++, tmp += gt_size) {
1331 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1332 if (ret != sizeof(tmp)) {
1333 ret = -errno;
1334 goto exit;
1338 /* write backup grain directory */
1339 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
1340 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
1341 i < gt_count; i++, tmp += gt_size) {
1342 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1343 if (ret != sizeof(tmp)) {
1344 ret = -errno;
1345 goto exit;
1349 ret = 0;
1350 exit:
1351 qemu_close(fd);
1352 return ret;
1355 static int filename_decompose(const char *filename, char *path, char *prefix,
1356 char *postfix, size_t buf_len)
1358 const char *p, *q;
1360 if (filename == NULL || !strlen(filename)) {
1361 fprintf(stderr, "Vmdk: no filename provided.\n");
1362 return -1;
1364 p = strrchr(filename, '/');
1365 if (p == NULL) {
1366 p = strrchr(filename, '\\');
1368 if (p == NULL) {
1369 p = strrchr(filename, ':');
1371 if (p != NULL) {
1372 p++;
1373 if (p - filename >= buf_len) {
1374 return -1;
1376 pstrcpy(path, p - filename + 1, filename);
1377 } else {
1378 p = filename;
1379 path[0] = '\0';
1381 q = strrchr(p, '.');
1382 if (q == NULL) {
1383 pstrcpy(prefix, buf_len, p);
1384 postfix[0] = '\0';
1385 } else {
1386 if (q - p >= buf_len) {
1387 return -1;
1389 pstrcpy(prefix, q - p + 1, p);
1390 pstrcpy(postfix, buf_len, q);
1392 return 0;
1395 static int relative_path(char *dest, int dest_size,
1396 const char *base, const char *target)
1398 int i = 0;
1399 int n = 0;
1400 const char *p, *q;
1401 #ifdef _WIN32
1402 const char *sep = "\\";
1403 #else
1404 const char *sep = "/";
1405 #endif
1407 if (!(dest && base && target)) {
1408 return -1;
1410 if (path_is_absolute(target)) {
1411 pstrcpy(dest, dest_size, target);
1412 return 0;
1414 while (base[i] == target[i]) {
1415 i++;
1417 p = &base[i];
1418 q = &target[i];
1419 while (*p) {
1420 if (*p == *sep) {
1421 n++;
1423 p++;
1425 dest[0] = '\0';
1426 for (; n; n--) {
1427 pstrcat(dest, dest_size, "..");
1428 pstrcat(dest, dest_size, sep);
1430 pstrcat(dest, dest_size, q);
1431 return 0;
1434 static int vmdk_create(const char *filename, QEMUOptionParameter *options)
1436 int fd, idx = 0;
1437 char desc[BUF_SIZE];
1438 int64_t total_size = 0, filesize;
1439 const char *backing_file = NULL;
1440 const char *fmt = NULL;
1441 int flags = 0;
1442 int ret = 0;
1443 bool flat, split, compress;
1444 char ext_desc_lines[BUF_SIZE] = "";
1445 char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1446 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1447 const char *desc_extent_line;
1448 char parent_desc_line[BUF_SIZE] = "";
1449 uint32_t parent_cid = 0xffffffff;
1450 const char desc_template[] =
1451 "# Disk DescriptorFile\n"
1452 "version=1\n"
1453 "CID=%x\n"
1454 "parentCID=%x\n"
1455 "createType=\"%s\"\n"
1456 "%s"
1457 "\n"
1458 "# Extent description\n"
1459 "%s"
1460 "\n"
1461 "# The Disk Data Base\n"
1462 "#DDB\n"
1463 "\n"
1464 "ddb.virtualHWVersion = \"%d\"\n"
1465 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
1466 "ddb.geometry.heads = \"16\"\n"
1467 "ddb.geometry.sectors = \"63\"\n"
1468 "ddb.adapterType = \"ide\"\n";
1470 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
1471 return -EINVAL;
1473 /* Read out options */
1474 while (options && options->name) {
1475 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1476 total_size = options->value.n;
1477 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1478 backing_file = options->value.s;
1479 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1480 flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1481 } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1482 fmt = options->value.s;
1484 options++;
1486 if (!fmt) {
1487 /* Default format to monolithicSparse */
1488 fmt = "monolithicSparse";
1489 } else if (strcmp(fmt, "monolithicFlat") &&
1490 strcmp(fmt, "monolithicSparse") &&
1491 strcmp(fmt, "twoGbMaxExtentSparse") &&
1492 strcmp(fmt, "twoGbMaxExtentFlat") &&
1493 strcmp(fmt, "streamOptimized")) {
1494 fprintf(stderr, "VMDK: Unknown subformat: %s\n", fmt);
1495 return -EINVAL;
1497 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1498 strcmp(fmt, "twoGbMaxExtentSparse"));
1499 flat = !(strcmp(fmt, "monolithicFlat") &&
1500 strcmp(fmt, "twoGbMaxExtentFlat"));
1501 compress = !strcmp(fmt, "streamOptimized");
1502 if (flat) {
1503 desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1504 } else {
1505 desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1507 if (flat && backing_file) {
1508 /* not supporting backing file for flat image */
1509 return -ENOTSUP;
1511 if (backing_file) {
1512 char parent_filename[PATH_MAX];
1513 BlockDriverState *bs = bdrv_new("");
1514 ret = bdrv_open(bs, backing_file, 0, NULL);
1515 if (ret != 0) {
1516 bdrv_delete(bs);
1517 return ret;
1519 if (strcmp(bs->drv->format_name, "vmdk")) {
1520 bdrv_delete(bs);
1521 return -EINVAL;
1523 parent_cid = vmdk_read_cid(bs, 0);
1524 bdrv_delete(bs);
1525 relative_path(parent_filename, sizeof(parent_filename),
1526 filename, backing_file);
1527 snprintf(parent_desc_line, sizeof(parent_desc_line),
1528 "parentFileNameHint=\"%s\"", parent_filename);
1531 /* Create extents */
1532 filesize = total_size;
1533 while (filesize > 0) {
1534 char desc_line[BUF_SIZE];
1535 char ext_filename[PATH_MAX];
1536 char desc_filename[PATH_MAX];
1537 int64_t size = filesize;
1539 if (split && size > split_size) {
1540 size = split_size;
1542 if (split) {
1543 snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1544 prefix, flat ? 'f' : 's', ++idx, postfix);
1545 } else if (flat) {
1546 snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1547 prefix, postfix);
1548 } else {
1549 snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1550 prefix, postfix);
1552 snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1553 path, desc_filename);
1555 if (vmdk_create_extent(ext_filename, size, flat, compress)) {
1556 return -EINVAL;
1558 filesize -= size;
1560 /* Format description line */
1561 snprintf(desc_line, sizeof(desc_line),
1562 desc_extent_line, size / 512, desc_filename);
1563 pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);
1565 /* generate descriptor file */
1566 snprintf(desc, sizeof(desc), desc_template,
1567 (unsigned int)time(NULL),
1568 parent_cid,
1569 fmt,
1570 parent_desc_line,
1571 ext_desc_lines,
1572 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1573 total_size / (int64_t)(63 * 16 * 512));
1574 if (split || flat) {
1575 fd = qemu_open(filename,
1576 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1577 0644);
1578 } else {
1579 fd = qemu_open(filename,
1580 O_WRONLY | O_BINARY | O_LARGEFILE,
1581 0644);
1583 if (fd < 0) {
1584 return -errno;
1586 /* the descriptor offset = 0x200 */
1587 if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
1588 ret = -errno;
1589 goto exit;
1591 ret = qemu_write_full(fd, desc, strlen(desc));
1592 if (ret != strlen(desc)) {
1593 ret = -errno;
1594 goto exit;
1596 ret = 0;
1597 exit:
1598 qemu_close(fd);
1599 return ret;
1602 static void vmdk_close(BlockDriverState *bs)
1604 BDRVVmdkState *s = bs->opaque;
1606 vmdk_free_extents(bs);
1608 migrate_del_blocker(s->migration_blocker);
1609 error_free(s->migration_blocker);
1612 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
1614 BDRVVmdkState *s = bs->opaque;
1615 int i, err;
1616 int ret = 0;
1618 for (i = 0; i < s->num_extents; i++) {
1619 err = bdrv_co_flush(s->extents[i].file);
1620 if (err < 0) {
1621 ret = err;
1624 return ret;
1627 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1629 int i;
1630 int64_t ret = 0;
1631 int64_t r;
1632 BDRVVmdkState *s = bs->opaque;
1634 ret = bdrv_get_allocated_file_size(bs->file);
1635 if (ret < 0) {
1636 return ret;
1638 for (i = 0; i < s->num_extents; i++) {
1639 if (s->extents[i].file == bs->file) {
1640 continue;
1642 r = bdrv_get_allocated_file_size(s->extents[i].file);
1643 if (r < 0) {
1644 return r;
1646 ret += r;
1648 return ret;
1651 static QEMUOptionParameter vmdk_create_options[] = {
1653 .name = BLOCK_OPT_SIZE,
1654 .type = OPT_SIZE,
1655 .help = "Virtual disk size"
1658 .name = BLOCK_OPT_BACKING_FILE,
1659 .type = OPT_STRING,
1660 .help = "File name of a base image"
1663 .name = BLOCK_OPT_COMPAT6,
1664 .type = OPT_FLAG,
1665 .help = "VMDK version 6 image"
1668 .name = BLOCK_OPT_SUBFMT,
1669 .type = OPT_STRING,
1670 .help =
1671 "VMDK flat extent format, can be one of "
1672 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1674 { NULL }
1677 static BlockDriver bdrv_vmdk = {
1678 .format_name = "vmdk",
1679 .instance_size = sizeof(BDRVVmdkState),
1680 .bdrv_probe = vmdk_probe,
1681 .bdrv_open = vmdk_open,
1682 .bdrv_reopen_prepare = vmdk_reopen_prepare,
1683 .bdrv_read = vmdk_co_read,
1684 .bdrv_write = vmdk_co_write,
1685 .bdrv_close = vmdk_close,
1686 .bdrv_create = vmdk_create,
1687 .bdrv_co_flush_to_disk = vmdk_co_flush,
1688 .bdrv_co_is_allocated = vmdk_co_is_allocated,
1689 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
1691 .create_options = vmdk_create_options,
1694 static void bdrv_vmdk_init(void)
1696 bdrv_register(&bdrv_vmdk);
1699 block_init(bdrv_vmdk_init);