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
26 #include "qemu-common.h"
27 #include "block_int.h"
31 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
32 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
33 #define VMDK4_COMPRESSION_DEFLATE 1
34 #define VMDK4_FLAG_RGD (1 << 1)
35 #define VMDK4_FLAG_COMPRESS (1 << 16)
36 #define VMDK4_FLAG_MARKER (1 << 17)
41 uint32_t disk_sectors
;
43 uint32_t l1dir_offset
;
45 uint32_t file_sectors
;
48 uint32_t sectors_per_track
;
58 int32_t num_gtes_per_gte
;
64 uint16_t compressAlgorithm
;
65 } QEMU_PACKED VMDK4Header
;
67 #define L2_CACHE_SIZE 16
69 typedef struct VmdkExtent
{
70 BlockDriverState
*file
;
76 int64_t flat_start_offset
;
77 int64_t l1_table_offset
;
78 int64_t l1_backup_table_offset
;
80 uint32_t *l1_backup_table
;
82 uint32_t l1_entry_sectors
;
86 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
87 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
89 unsigned int cluster_sectors
;
92 typedef struct BDRVVmdkState
{
98 /* Extent array with num_extents entries, ascend ordered by address */
102 typedef struct VmdkMetaData
{
104 unsigned int l1_index
;
105 unsigned int l2_index
;
106 unsigned int l2_offset
;
110 typedef struct VmdkGrainMarker
{
116 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
123 magic
= be32_to_cpu(*(uint32_t *)buf
);
124 if (magic
== VMDK3_MAGIC
||
125 magic
== VMDK4_MAGIC
) {
128 const char *p
= (const char *)buf
;
129 const char *end
= p
+ buf_size
;
132 /* skip comment line */
133 while (p
< end
&& *p
!= '\n') {
140 while (p
< end
&& *p
== ' ') {
143 /* skip '\r' if windows line endings used. */
144 if (p
< end
&& *p
== '\r') {
147 /* only accept blank lines before 'version=' line */
148 if (p
== end
|| *p
!= '\n') {
154 if (end
- p
>= strlen("version=X\n")) {
155 if (strncmp("version=1\n", p
, strlen("version=1\n")) == 0 ||
156 strncmp("version=2\n", p
, strlen("version=2\n")) == 0) {
160 if (end
- p
>= strlen("version=X\r\n")) {
161 if (strncmp("version=1\r\n", p
, strlen("version=1\r\n")) == 0 ||
162 strncmp("version=2\r\n", p
, strlen("version=2\r\n")) == 0) {
174 #define SECTOR_SIZE 512
175 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
176 #define BUF_SIZE 4096
177 #define HEADER_SIZE 512 /* first sector of 512 bytes */
179 static void vmdk_free_extents(BlockDriverState
*bs
)
182 BDRVVmdkState
*s
= bs
->opaque
;
185 for (i
= 0; i
< s
->num_extents
; i
++) {
189 g_free(e
->l1_backup_table
);
190 if (e
->file
!= bs
->file
) {
191 bdrv_delete(e
->file
);
197 static void vmdk_free_last_extent(BlockDriverState
*bs
)
199 BDRVVmdkState
*s
= bs
->opaque
;
201 if (s
->num_extents
== 0) {
205 s
->extents
= g_realloc(s
->extents
, s
->num_extents
* sizeof(VmdkExtent
));
208 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
210 char desc
[DESC_SIZE
];
211 uint32_t cid
= 0xffffffff;
212 const char *p_name
, *cid_str
;
214 BDRVVmdkState
*s
= bs
->opaque
;
217 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
223 cid_str
= "parentCID";
224 cid_str_size
= sizeof("parentCID");
227 cid_str_size
= sizeof("CID");
230 desc
[DESC_SIZE
- 1] = '\0';
231 p_name
= strstr(desc
, cid_str
);
232 if (p_name
!= NULL
) {
233 p_name
+= cid_str_size
;
234 sscanf(p_name
, "%x", &cid
);
240 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
242 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
243 char *p_name
, *tmp_str
;
244 BDRVVmdkState
*s
= bs
->opaque
;
247 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
252 desc
[DESC_SIZE
- 1] = '\0';
253 tmp_str
= strstr(desc
, "parentCID");
254 if (tmp_str
== NULL
) {
258 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
259 p_name
= strstr(desc
, "CID");
260 if (p_name
!= NULL
) {
261 p_name
+= sizeof("CID");
262 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
263 pstrcat(desc
, sizeof(desc
), tmp_desc
);
266 ret
= bdrv_pwrite_sync(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
274 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
277 BDRVVmdkState
*s
= bs
->opaque
;
278 BlockDriverState
*p_bs
= bs
->backing_hd
;
282 cur_pcid
= vmdk_read_cid(p_bs
, 0);
283 if (s
->parent_cid
!= cur_pcid
) {
293 static int vmdk_parent_open(BlockDriverState
*bs
)
296 char desc
[DESC_SIZE
+ 1];
297 BDRVVmdkState
*s
= bs
->opaque
;
300 desc
[DESC_SIZE
] = '\0';
301 ret
= bdrv_pread(bs
->file
, s
->desc_offset
, desc
, DESC_SIZE
);
306 p_name
= strstr(desc
, "parentFileNameHint");
307 if (p_name
!= NULL
) {
310 p_name
+= sizeof("parentFileNameHint") + 1;
311 end_name
= strchr(p_name
, '\"');
312 if (end_name
== NULL
) {
315 if ((end_name
- p_name
) > sizeof(bs
->backing_file
) - 1) {
319 pstrcpy(bs
->backing_file
, end_name
- p_name
+ 1, p_name
);
325 /* Create and append extent to the extent array. Return the added VmdkExtent
326 * address. return NULL if allocation failed. */
327 static VmdkExtent
*vmdk_add_extent(BlockDriverState
*bs
,
328 BlockDriverState
*file
, bool flat
, int64_t sectors
,
329 int64_t l1_offset
, int64_t l1_backup_offset
,
331 int l2_size
, unsigned int cluster_sectors
)
334 BDRVVmdkState
*s
= bs
->opaque
;
336 s
->extents
= g_realloc(s
->extents
,
337 (s
->num_extents
+ 1) * sizeof(VmdkExtent
));
338 extent
= &s
->extents
[s
->num_extents
];
341 memset(extent
, 0, sizeof(VmdkExtent
));
344 extent
->sectors
= sectors
;
345 extent
->l1_table_offset
= l1_offset
;
346 extent
->l1_backup_table_offset
= l1_backup_offset
;
347 extent
->l1_size
= l1_size
;
348 extent
->l1_entry_sectors
= l2_size
* cluster_sectors
;
349 extent
->l2_size
= l2_size
;
350 extent
->cluster_sectors
= cluster_sectors
;
352 if (s
->num_extents
> 1) {
353 extent
->end_sector
= (*(extent
- 1)).end_sector
+ extent
->sectors
;
355 extent
->end_sector
= extent
->sectors
;
357 bs
->total_sectors
= extent
->end_sector
;
361 static int vmdk_init_tables(BlockDriverState
*bs
, VmdkExtent
*extent
)
366 /* read the L1 table */
367 l1_size
= extent
->l1_size
* sizeof(uint32_t);
368 extent
->l1_table
= g_malloc(l1_size
);
369 ret
= bdrv_pread(extent
->file
,
370 extent
->l1_table_offset
,
376 for (i
= 0; i
< extent
->l1_size
; i
++) {
377 le32_to_cpus(&extent
->l1_table
[i
]);
380 if (extent
->l1_backup_table_offset
) {
381 extent
->l1_backup_table
= g_malloc(l1_size
);
382 ret
= bdrv_pread(extent
->file
,
383 extent
->l1_backup_table_offset
,
384 extent
->l1_backup_table
,
389 for (i
= 0; i
< extent
->l1_size
; i
++) {
390 le32_to_cpus(&extent
->l1_backup_table
[i
]);
395 g_malloc(extent
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
398 g_free(extent
->l1_backup_table
);
400 g_free(extent
->l1_table
);
404 static int vmdk_open_vmdk3(BlockDriverState
*bs
,
405 BlockDriverState
*file
,
413 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
417 extent
= vmdk_add_extent(bs
,
419 le32_to_cpu(header
.disk_sectors
),
420 le32_to_cpu(header
.l1dir_offset
) << 9,
422 le32_to_cpu(header
.granularity
));
423 ret
= vmdk_init_tables(bs
, extent
);
425 /* free extent allocated by vmdk_add_extent */
426 vmdk_free_last_extent(bs
);
431 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
432 int64_t desc_offset
);
434 static int vmdk_open_vmdk4(BlockDriverState
*bs
,
435 BlockDriverState
*file
,
440 uint32_t l1_size
, l1_entry_sectors
;
443 int64_t l1_backup_offset
= 0;
445 ret
= bdrv_pread(file
, sizeof(magic
), &header
, sizeof(header
));
449 if (header
.capacity
== 0 && header
.desc_offset
) {
450 return vmdk_open_desc_file(bs
, flags
, header
.desc_offset
<< 9);
452 l1_entry_sectors
= le32_to_cpu(header
.num_gtes_per_gte
)
453 * le64_to_cpu(header
.granularity
);
454 if (l1_entry_sectors
<= 0) {
457 l1_size
= (le64_to_cpu(header
.capacity
) + l1_entry_sectors
- 1)
459 if (le32_to_cpu(header
.flags
) & VMDK4_FLAG_RGD
) {
460 l1_backup_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
462 extent
= vmdk_add_extent(bs
, file
, false,
463 le64_to_cpu(header
.capacity
),
464 le64_to_cpu(header
.gd_offset
) << 9,
467 le32_to_cpu(header
.num_gtes_per_gte
),
468 le64_to_cpu(header
.granularity
));
470 le16_to_cpu(header
.compressAlgorithm
) == VMDK4_COMPRESSION_DEFLATE
;
471 extent
->has_marker
= le32_to_cpu(header
.flags
) & VMDK4_FLAG_MARKER
;
472 ret
= vmdk_init_tables(bs
, extent
);
474 /* free extent allocated by vmdk_add_extent */
475 vmdk_free_last_extent(bs
);
480 /* find an option value out of descriptor file */
481 static int vmdk_parse_description(const char *desc
, const char *opt_name
,
482 char *buf
, int buf_size
)
484 char *opt_pos
, *opt_end
;
485 const char *end
= desc
+ strlen(desc
);
487 opt_pos
= strstr(desc
, opt_name
);
491 /* Skip "=\"" following opt_name */
492 opt_pos
+= strlen(opt_name
) + 2;
493 if (opt_pos
>= end
) {
497 while (opt_end
< end
&& *opt_end
!= '"') {
500 if (opt_end
== end
|| buf_size
< opt_end
- opt_pos
+ 1) {
503 pstrcpy(buf
, opt_end
- opt_pos
+ 1, opt_pos
);
507 /* Open an extent file and append to bs array */
508 static int vmdk_open_sparse(BlockDriverState
*bs
,
509 BlockDriverState
*file
,
514 if (bdrv_pread(file
, 0, &magic
, sizeof(magic
)) != sizeof(magic
)) {
518 magic
= be32_to_cpu(magic
);
521 return vmdk_open_vmdk3(bs
, file
, flags
);
524 return vmdk_open_vmdk4(bs
, file
, flags
);
532 static int vmdk_parse_extents(const char *desc
, BlockDriverState
*bs
,
533 const char *desc_file_path
)
539 const char *p
= desc
;
542 char extent_path
[PATH_MAX
];
543 BlockDriverState
*extent_file
;
546 /* parse extent line:
547 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
549 * RW [size in sectors] SPARSE "file-name.vmdk"
552 ret
= sscanf(p
, "%10s %" SCNd64
" %10s %511s %" SCNd64
,
553 access
, §ors
, type
, fname
, &flat_offset
);
554 if (ret
< 4 || strcmp(access
, "RW")) {
556 } else if (!strcmp(type
, "FLAT")) {
557 if (ret
!= 5 || flat_offset
< 0) {
560 } else if (ret
!= 4) {
564 /* trim the quotation marks around */
565 if (fname
[0] == '"') {
566 memmove(fname
, fname
+ 1, strlen(fname
));
567 if (strlen(fname
) <= 1 || fname
[strlen(fname
) - 1] != '"') {
570 fname
[strlen(fname
) - 1] = '\0';
573 (strcmp(type
, "FLAT") && strcmp(type
, "SPARSE")) ||
574 (strcmp(access
, "RW"))) {
578 path_combine(extent_path
, sizeof(extent_path
),
579 desc_file_path
, fname
);
580 ret
= bdrv_file_open(&extent_file
, extent_path
, bs
->open_flags
);
585 /* save to extents array */
586 if (!strcmp(type
, "FLAT")) {
590 extent
= vmdk_add_extent(bs
, extent_file
, true, sectors
,
591 0, 0, 0, 0, sectors
);
592 extent
->flat_start_offset
= flat_offset
<< 9;
593 } else if (!strcmp(type
, "SPARSE")) {
595 ret
= vmdk_open_sparse(bs
, extent_file
, bs
->open_flags
);
597 bdrv_delete(extent_file
);
602 "VMDK: Not supported extent type \"%s\""".\n", type
);
606 /* move to next line */
607 while (*p
&& *p
!= '\n') {
615 static int vmdk_open_desc_file(BlockDriverState
*bs
, int flags
,
621 BDRVVmdkState
*s
= bs
->opaque
;
623 ret
= bdrv_pread(bs
->file
, desc_offset
, buf
, sizeof(buf
));
628 if (vmdk_parse_description(buf
, "createType", ct
, sizeof(ct
))) {
631 if (strcmp(ct
, "monolithicFlat") &&
632 strcmp(ct
, "twoGbMaxExtentSparse") &&
633 strcmp(ct
, "twoGbMaxExtentFlat")) {
635 "VMDK: Not supported image type \"%s\""".\n", ct
);
639 return vmdk_parse_extents(buf
, bs
, bs
->file
->filename
);
642 static int vmdk_open(BlockDriverState
*bs
, int flags
)
645 BDRVVmdkState
*s
= bs
->opaque
;
647 if (vmdk_open_sparse(bs
, bs
->file
, flags
) == 0) {
648 s
->desc_offset
= 0x200;
650 ret
= vmdk_open_desc_file(bs
, flags
, 0);
655 /* try to open parent images, if exist */
656 ret
= vmdk_parent_open(bs
);
660 s
->parent_cid
= vmdk_read_cid(bs
, 1);
661 qemu_co_mutex_init(&s
->lock
);
665 vmdk_free_extents(bs
);
669 static int get_whole_cluster(BlockDriverState
*bs
,
671 uint64_t cluster_offset
,
675 /* 128 sectors * 512 bytes each = grain size 64KB */
676 uint8_t whole_grain
[extent
->cluster_sectors
* 512];
678 /* we will be here if it's first write on non-exist grain(cluster).
679 * try to read from parent image, if exist */
680 if (bs
->backing_hd
) {
683 if (!vmdk_is_cid_valid(bs
)) {
687 /* floor offset to cluster */
688 offset
-= offset
% (extent
->cluster_sectors
* 512);
689 ret
= bdrv_read(bs
->backing_hd
, offset
>> 9, whole_grain
,
690 extent
->cluster_sectors
);
695 /* Write grain only into the active image */
696 ret
= bdrv_write(extent
->file
, cluster_offset
, whole_grain
,
697 extent
->cluster_sectors
);
705 static int vmdk_L2update(VmdkExtent
*extent
, VmdkMetaData
*m_data
)
707 /* update L2 table */
708 if (bdrv_pwrite_sync(
710 ((int64_t)m_data
->l2_offset
* 512)
711 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
713 sizeof(m_data
->offset
)
717 /* update backup L2 table */
718 if (extent
->l1_backup_table_offset
!= 0) {
719 m_data
->l2_offset
= extent
->l1_backup_table
[m_data
->l1_index
];
720 if (bdrv_pwrite_sync(
722 ((int64_t)m_data
->l2_offset
* 512)
723 + (m_data
->l2_index
* sizeof(m_data
->offset
)),
724 &(m_data
->offset
), sizeof(m_data
->offset
)
733 static int get_cluster_offset(BlockDriverState
*bs
,
735 VmdkMetaData
*m_data
,
738 uint64_t *cluster_offset
)
740 unsigned int l1_index
, l2_offset
, l2_index
;
742 uint32_t min_count
, *l2_table
, tmp
= 0;
748 *cluster_offset
= extent
->flat_start_offset
;
752 offset
-= (extent
->end_sector
- extent
->sectors
) * SECTOR_SIZE
;
753 l1_index
= (offset
>> 9) / extent
->l1_entry_sectors
;
754 if (l1_index
>= extent
->l1_size
) {
757 l2_offset
= extent
->l1_table
[l1_index
];
761 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
762 if (l2_offset
== extent
->l2_cache_offsets
[i
]) {
763 /* increment the hit count */
764 if (++extent
->l2_cache_counts
[i
] == 0xffffffff) {
765 for (j
= 0; j
< L2_CACHE_SIZE
; j
++) {
766 extent
->l2_cache_counts
[j
] >>= 1;
769 l2_table
= extent
->l2_cache
+ (i
* extent
->l2_size
);
773 /* not found: load a new entry in the least used one */
775 min_count
= 0xffffffff;
776 for (i
= 0; i
< L2_CACHE_SIZE
; i
++) {
777 if (extent
->l2_cache_counts
[i
] < min_count
) {
778 min_count
= extent
->l2_cache_counts
[i
];
782 l2_table
= extent
->l2_cache
+ (min_index
* extent
->l2_size
);
785 (int64_t)l2_offset
* 512,
787 extent
->l2_size
* sizeof(uint32_t)
788 ) != extent
->l2_size
* sizeof(uint32_t)) {
792 extent
->l2_cache_offsets
[min_index
] = l2_offset
;
793 extent
->l2_cache_counts
[min_index
] = 1;
795 l2_index
= ((offset
>> 9) / extent
->cluster_sectors
) % extent
->l2_size
;
796 *cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
798 if (!*cluster_offset
) {
803 /* Avoid the L2 tables update for the images that have snapshots. */
804 *cluster_offset
= bdrv_getlength(extent
->file
);
805 if (!extent
->compressed
) {
808 *cluster_offset
+ (extent
->cluster_sectors
<< 9)
812 *cluster_offset
>>= 9;
813 tmp
= cpu_to_le32(*cluster_offset
);
814 l2_table
[l2_index
] = tmp
;
816 /* First of all we write grain itself, to avoid race condition
817 * that may to corrupt the image.
818 * This problem may occur because of insufficient space on host disk
819 * or inappropriate VM shutdown.
821 if (get_whole_cluster(
822 bs
, extent
, *cluster_offset
, offset
, allocate
) == -1) {
827 m_data
->offset
= tmp
;
828 m_data
->l1_index
= l1_index
;
829 m_data
->l2_index
= l2_index
;
830 m_data
->l2_offset
= l2_offset
;
834 *cluster_offset
<<= 9;
838 static VmdkExtent
*find_extent(BDRVVmdkState
*s
,
839 int64_t sector_num
, VmdkExtent
*start_hint
)
841 VmdkExtent
*extent
= start_hint
;
844 extent
= &s
->extents
[0];
846 while (extent
< &s
->extents
[s
->num_extents
]) {
847 if (sector_num
< extent
->end_sector
) {
855 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
856 int nb_sectors
, int *pnum
)
858 BDRVVmdkState
*s
= bs
->opaque
;
859 int64_t index_in_cluster
, n
, ret
;
863 extent
= find_extent(s
, sector_num
, NULL
);
867 ret
= get_cluster_offset(bs
, extent
, NULL
,
868 sector_num
* 512, 0, &offset
);
869 /* get_cluster_offset returning 0 means success */
872 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
873 n
= extent
->cluster_sectors
- index_in_cluster
;
874 if (n
> nb_sectors
) {
881 static int vmdk_write_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
882 int64_t offset_in_cluster
, const uint8_t *buf
,
883 int nb_sectors
, int64_t sector_num
)
886 VmdkGrainMarker
*data
= NULL
;
888 const uint8_t *write_buf
= buf
;
889 int write_len
= nb_sectors
* 512;
891 if (extent
->compressed
) {
892 if (!extent
->has_marker
) {
896 buf_len
= (extent
->cluster_sectors
<< 9) * 2;
897 data
= g_malloc(buf_len
+ sizeof(VmdkGrainMarker
));
898 if (compress(data
->data
, &buf_len
, buf
, nb_sectors
<< 9) != Z_OK
||
903 data
->lba
= sector_num
;
904 data
->size
= buf_len
;
905 write_buf
= (uint8_t *)data
;
906 write_len
= buf_len
+ sizeof(VmdkGrainMarker
);
908 ret
= bdrv_pwrite(extent
->file
,
909 cluster_offset
+ offset_in_cluster
,
912 if (ret
!= write_len
) {
913 ret
= ret
< 0 ? ret
: -EIO
;
922 static int vmdk_read_extent(VmdkExtent
*extent
, int64_t cluster_offset
,
923 int64_t offset_in_cluster
, uint8_t *buf
,
927 int cluster_bytes
, buf_bytes
;
928 uint8_t *cluster_buf
, *compressed_data
;
931 VmdkGrainMarker
*marker
;
935 if (!extent
->compressed
) {
936 ret
= bdrv_pread(extent
->file
,
937 cluster_offset
+ offset_in_cluster
,
938 buf
, nb_sectors
* 512);
939 if (ret
== nb_sectors
* 512) {
945 cluster_bytes
= extent
->cluster_sectors
* 512;
946 /* Read two clusters in case GrainMarker + compressed data > one cluster */
947 buf_bytes
= cluster_bytes
* 2;
948 cluster_buf
= g_malloc(buf_bytes
);
949 uncomp_buf
= g_malloc(cluster_bytes
);
950 ret
= bdrv_pread(extent
->file
,
952 cluster_buf
, buf_bytes
);
956 compressed_data
= cluster_buf
;
957 buf_len
= cluster_bytes
;
958 data_len
= cluster_bytes
;
959 if (extent
->has_marker
) {
960 marker
= (VmdkGrainMarker
*)cluster_buf
;
961 compressed_data
= marker
->data
;
962 data_len
= le32_to_cpu(marker
->size
);
964 if (!data_len
|| data_len
> buf_bytes
) {
968 ret
= uncompress(uncomp_buf
, &buf_len
, compressed_data
, data_len
);
974 if (offset_in_cluster
< 0 ||
975 offset_in_cluster
+ nb_sectors
* 512 > buf_len
) {
979 memcpy(buf
, uncomp_buf
+ offset_in_cluster
, nb_sectors
* 512);
988 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
989 uint8_t *buf
, int nb_sectors
)
991 BDRVVmdkState
*s
= bs
->opaque
;
993 uint64_t n
, index_in_cluster
;
994 VmdkExtent
*extent
= NULL
;
995 uint64_t cluster_offset
;
997 while (nb_sectors
> 0) {
998 extent
= find_extent(s
, sector_num
, extent
);
1002 ret
= get_cluster_offset(
1004 sector_num
<< 9, 0, &cluster_offset
);
1005 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1006 n
= extent
->cluster_sectors
- index_in_cluster
;
1007 if (n
> nb_sectors
) {
1011 /* if not allocated, try to read from parent image, if exist */
1012 if (bs
->backing_hd
) {
1013 if (!vmdk_is_cid_valid(bs
)) {
1016 ret
= bdrv_read(bs
->backing_hd
, sector_num
, buf
, n
);
1021 memset(buf
, 0, 512 * n
);
1024 ret
= vmdk_read_extent(extent
,
1025 cluster_offset
, index_in_cluster
* 512,
1038 static coroutine_fn
int vmdk_co_read(BlockDriverState
*bs
, int64_t sector_num
,
1039 uint8_t *buf
, int nb_sectors
)
1042 BDRVVmdkState
*s
= bs
->opaque
;
1043 qemu_co_mutex_lock(&s
->lock
);
1044 ret
= vmdk_read(bs
, sector_num
, buf
, nb_sectors
);
1045 qemu_co_mutex_unlock(&s
->lock
);
1049 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
1050 const uint8_t *buf
, int nb_sectors
)
1052 BDRVVmdkState
*s
= bs
->opaque
;
1053 VmdkExtent
*extent
= NULL
;
1055 int64_t index_in_cluster
;
1056 uint64_t cluster_offset
;
1057 VmdkMetaData m_data
;
1059 if (sector_num
> bs
->total_sectors
) {
1061 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
1062 " total_sectors=0x%" PRIx64
"\n",
1063 sector_num
, bs
->total_sectors
);
1067 while (nb_sectors
> 0) {
1068 extent
= find_extent(s
, sector_num
, extent
);
1072 ret
= get_cluster_offset(
1076 sector_num
<< 9, !extent
->compressed
,
1078 if (extent
->compressed
) {
1080 /* Refuse write to allocated cluster for streamOptimized */
1082 "VMDK: can't write to allocated cluster"
1083 " for streamOptimized\n");
1087 ret
= get_cluster_offset(
1098 index_in_cluster
= sector_num
% extent
->cluster_sectors
;
1099 n
= extent
->cluster_sectors
- index_in_cluster
;
1100 if (n
> nb_sectors
) {
1104 ret
= vmdk_write_extent(extent
,
1105 cluster_offset
, index_in_cluster
* 512,
1106 buf
, n
, sector_num
);
1111 /* update L2 tables */
1112 if (vmdk_L2update(extent
, &m_data
) == -1) {
1120 /* update CID on the first write every time the virtual disk is
1122 if (!s
->cid_updated
) {
1123 ret
= vmdk_write_cid(bs
, time(NULL
));
1127 s
->cid_updated
= true;
1133 static coroutine_fn
int vmdk_co_write(BlockDriverState
*bs
, int64_t sector_num
,
1134 const uint8_t *buf
, int nb_sectors
)
1137 BDRVVmdkState
*s
= bs
->opaque
;
1138 qemu_co_mutex_lock(&s
->lock
);
1139 ret
= vmdk_write(bs
, sector_num
, buf
, nb_sectors
);
1140 qemu_co_mutex_unlock(&s
->lock
);
1145 static int vmdk_create_extent(const char *filename
, int64_t filesize
,
1146 bool flat
, bool compress
)
1151 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
1155 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1161 ret
= ftruncate(fd
, filesize
);
1167 magic
= cpu_to_be32(VMDK4_MAGIC
);
1168 memset(&header
, 0, sizeof(header
));
1171 3 | (compress
? VMDK4_FLAG_COMPRESS
| VMDK4_FLAG_MARKER
: 0);
1172 header
.compressAlgorithm
= compress
? VMDK4_COMPRESSION_DEFLATE
: 0;
1173 header
.capacity
= filesize
/ 512;
1174 header
.granularity
= 128;
1175 header
.num_gtes_per_gte
= 512;
1177 grains
= (filesize
/ 512 + header
.granularity
- 1) / header
.granularity
;
1178 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
1180 (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
1181 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
1183 header
.desc_offset
= 1;
1184 header
.desc_size
= 20;
1185 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
1186 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
1187 header
.grain_offset
=
1188 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
1189 header
.granularity
- 1) / header
.granularity
) *
1191 /* swap endianness for all header fields */
1192 header
.version
= cpu_to_le32(header
.version
);
1193 header
.flags
= cpu_to_le32(header
.flags
);
1194 header
.capacity
= cpu_to_le64(header
.capacity
);
1195 header
.granularity
= cpu_to_le64(header
.granularity
);
1196 header
.num_gtes_per_gte
= cpu_to_le32(header
.num_gtes_per_gte
);
1197 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
1198 header
.desc_size
= cpu_to_le64(header
.desc_size
);
1199 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
1200 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
1201 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
1202 header
.compressAlgorithm
= cpu_to_le16(header
.compressAlgorithm
);
1204 header
.check_bytes
[0] = 0xa;
1205 header
.check_bytes
[1] = 0x20;
1206 header
.check_bytes
[2] = 0xd;
1207 header
.check_bytes
[3] = 0xa;
1209 /* write all the data */
1210 ret
= qemu_write_full(fd
, &magic
, sizeof(magic
));
1211 if (ret
!= sizeof(magic
)) {
1215 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
1216 if (ret
!= sizeof(header
)) {
1221 ret
= ftruncate(fd
, le64_to_cpu(header
.grain_offset
) << 9);
1227 /* write grain directory */
1228 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
1229 for (i
= 0, tmp
= le64_to_cpu(header
.rgd_offset
) + gd_size
;
1230 i
< gt_count
; i
++, tmp
+= gt_size
) {
1231 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1232 if (ret
!= sizeof(tmp
)) {
1238 /* write backup grain directory */
1239 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
1240 for (i
= 0, tmp
= le64_to_cpu(header
.gd_offset
) + gd_size
;
1241 i
< gt_count
; i
++, tmp
+= gt_size
) {
1242 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1243 if (ret
!= sizeof(tmp
)) {
1255 static int filename_decompose(const char *filename
, char *path
, char *prefix
,
1256 char *postfix
, size_t buf_len
)
1260 if (filename
== NULL
|| !strlen(filename
)) {
1261 fprintf(stderr
, "Vmdk: no filename provided.\n");
1264 p
= strrchr(filename
, '/');
1266 p
= strrchr(filename
, '\\');
1269 p
= strrchr(filename
, ':');
1273 if (p
- filename
>= buf_len
) {
1276 pstrcpy(path
, p
- filename
+ 1, filename
);
1281 q
= strrchr(p
, '.');
1283 pstrcpy(prefix
, buf_len
, p
);
1286 if (q
- p
>= buf_len
) {
1289 pstrcpy(prefix
, q
- p
+ 1, p
);
1290 pstrcpy(postfix
, buf_len
, q
);
1295 static int relative_path(char *dest
, int dest_size
,
1296 const char *base
, const char *target
)
1302 const char *sep
= "\\";
1304 const char *sep
= "/";
1307 if (!(dest
&& base
&& target
)) {
1310 if (path_is_absolute(target
)) {
1311 dest
[dest_size
- 1] = '\0';
1312 strncpy(dest
, target
, dest_size
- 1);
1315 while (base
[i
] == target
[i
]) {
1328 pstrcat(dest
, dest_size
, "..");
1329 pstrcat(dest
, dest_size
, sep
);
1331 pstrcat(dest
, dest_size
, q
);
1335 static int vmdk_create(const char *filename
, QEMUOptionParameter
*options
)
1338 char desc
[BUF_SIZE
];
1339 int64_t total_size
= 0, filesize
;
1340 const char *backing_file
= NULL
;
1341 const char *fmt
= NULL
;
1344 bool flat
, split
, compress
;
1345 char ext_desc_lines
[BUF_SIZE
] = "";
1346 char path
[PATH_MAX
], prefix
[PATH_MAX
], postfix
[PATH_MAX
];
1347 const int64_t split_size
= 0x80000000; /* VMDK has constant split size */
1348 const char *desc_extent_line
;
1349 char parent_desc_line
[BUF_SIZE
] = "";
1350 uint32_t parent_cid
= 0xffffffff;
1351 const char desc_template
[] =
1352 "# Disk DescriptorFile\n"
1356 "createType=\"%s\"\n"
1359 "# Extent description\n"
1362 "# The Disk Data Base\n"
1365 "ddb.virtualHWVersion = \"%d\"\n"
1366 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
1367 "ddb.geometry.heads = \"16\"\n"
1368 "ddb.geometry.sectors = \"63\"\n"
1369 "ddb.adapterType = \"ide\"\n";
1371 if (filename_decompose(filename
, path
, prefix
, postfix
, PATH_MAX
)) {
1374 /* Read out options */
1375 while (options
&& options
->name
) {
1376 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1377 total_size
= options
->value
.n
;
1378 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1379 backing_file
= options
->value
.s
;
1380 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT6
)) {
1381 flags
|= options
->value
.n
? BLOCK_FLAG_COMPAT6
: 0;
1382 } else if (!strcmp(options
->name
, BLOCK_OPT_SUBFMT
)) {
1383 fmt
= options
->value
.s
;
1388 /* Default format to monolithicSparse */
1389 fmt
= "monolithicSparse";
1390 } else if (strcmp(fmt
, "monolithicFlat") &&
1391 strcmp(fmt
, "monolithicSparse") &&
1392 strcmp(fmt
, "twoGbMaxExtentSparse") &&
1393 strcmp(fmt
, "twoGbMaxExtentFlat") &&
1394 strcmp(fmt
, "streamOptimized")) {
1395 fprintf(stderr
, "VMDK: Unknown subformat: %s\n", fmt
);
1398 split
= !(strcmp(fmt
, "twoGbMaxExtentFlat") &&
1399 strcmp(fmt
, "twoGbMaxExtentSparse"));
1400 flat
= !(strcmp(fmt
, "monolithicFlat") &&
1401 strcmp(fmt
, "twoGbMaxExtentFlat"));
1402 compress
= !strcmp(fmt
, "streamOptimized");
1404 desc_extent_line
= "RW %lld FLAT \"%s\" 0\n";
1406 desc_extent_line
= "RW %lld SPARSE \"%s\"\n";
1408 if (flat
&& backing_file
) {
1409 /* not supporting backing file for flat image */
1413 char parent_filename
[PATH_MAX
];
1414 BlockDriverState
*bs
= bdrv_new("");
1415 ret
= bdrv_open(bs
, backing_file
, 0, NULL
);
1420 if (strcmp(bs
->drv
->format_name
, "vmdk")) {
1424 parent_cid
= vmdk_read_cid(bs
, 0);
1426 relative_path(parent_filename
, sizeof(parent_filename
),
1427 filename
, backing_file
);
1428 snprintf(parent_desc_line
, sizeof(parent_desc_line
),
1429 "parentFileNameHint=\"%s\"", parent_filename
);
1432 /* Create extents */
1433 filesize
= total_size
;
1434 while (filesize
> 0) {
1435 char desc_line
[BUF_SIZE
];
1436 char ext_filename
[PATH_MAX
];
1437 char desc_filename
[PATH_MAX
];
1438 int64_t size
= filesize
;
1440 if (split
&& size
> split_size
) {
1444 snprintf(desc_filename
, sizeof(desc_filename
), "%s-%c%03d%s",
1445 prefix
, flat
? 'f' : 's', ++idx
, postfix
);
1447 snprintf(desc_filename
, sizeof(desc_filename
), "%s-flat%s",
1450 snprintf(desc_filename
, sizeof(desc_filename
), "%s%s",
1453 snprintf(ext_filename
, sizeof(ext_filename
), "%s%s",
1454 path
, desc_filename
);
1456 if (vmdk_create_extent(ext_filename
, size
, flat
, compress
)) {
1461 /* Format description line */
1462 snprintf(desc_line
, sizeof(desc_line
),
1463 desc_extent_line
, size
/ 512, desc_filename
);
1464 pstrcat(ext_desc_lines
, sizeof(ext_desc_lines
), desc_line
);
1466 /* generate descriptor file */
1467 snprintf(desc
, sizeof(desc
), desc_template
,
1468 (unsigned int)time(NULL
),
1473 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
1474 total_size
/ (int64_t)(63 * 16 * 512));
1475 if (split
|| flat
) {
1478 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
1483 O_WRONLY
| O_BINARY
| O_LARGEFILE
,
1489 /* the descriptor offset = 0x200 */
1490 if (!split
&& !flat
&& 0x200 != lseek(fd
, 0x200, SEEK_SET
)) {
1494 ret
= qemu_write_full(fd
, desc
, strlen(desc
));
1495 if (ret
!= strlen(desc
)) {
1505 static void vmdk_close(BlockDriverState
*bs
)
1507 vmdk_free_extents(bs
);
1510 static coroutine_fn
int vmdk_co_flush(BlockDriverState
*bs
)
1513 BDRVVmdkState
*s
= bs
->opaque
;
1515 ret
= bdrv_co_flush(bs
->file
);
1516 for (i
= 0; i
< s
->num_extents
; i
++) {
1517 err
= bdrv_co_flush(s
->extents
[i
].file
);
1525 static int64_t vmdk_get_allocated_file_size(BlockDriverState
*bs
)
1530 BDRVVmdkState
*s
= bs
->opaque
;
1532 ret
= bdrv_get_allocated_file_size(bs
->file
);
1536 for (i
= 0; i
< s
->num_extents
; i
++) {
1537 if (s
->extents
[i
].file
== bs
->file
) {
1540 r
= bdrv_get_allocated_file_size(s
->extents
[i
].file
);
1549 static QEMUOptionParameter vmdk_create_options
[] = {
1551 .name
= BLOCK_OPT_SIZE
,
1553 .help
= "Virtual disk size"
1556 .name
= BLOCK_OPT_BACKING_FILE
,
1558 .help
= "File name of a base image"
1561 .name
= BLOCK_OPT_COMPAT6
,
1563 .help
= "VMDK version 6 image"
1566 .name
= BLOCK_OPT_SUBFMT
,
1569 "VMDK flat extent format, can be one of "
1570 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
1575 static BlockDriver bdrv_vmdk
= {
1576 .format_name
= "vmdk",
1577 .instance_size
= sizeof(BDRVVmdkState
),
1578 .bdrv_probe
= vmdk_probe
,
1579 .bdrv_open
= vmdk_open
,
1580 .bdrv_read
= vmdk_co_read
,
1581 .bdrv_write
= vmdk_co_write
,
1582 .bdrv_close
= vmdk_close
,
1583 .bdrv_create
= vmdk_create
,
1584 .bdrv_co_flush
= vmdk_co_flush
,
1585 .bdrv_is_allocated
= vmdk_is_allocated
,
1586 .bdrv_get_allocated_file_size
= vmdk_get_allocated_file_size
,
1588 .create_options
= vmdk_create_options
,
1591 static void bdrv_vmdk_init(void)
1593 bdrv_register(&bdrv_vmdk
);
1596 block_init(bdrv_vmdk_init
);