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"
29 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
30 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
35 uint32_t disk_sectors
;
37 uint32_t l1dir_offset
;
39 uint32_t file_sectors
;
42 uint32_t sectors_per_track
;
52 int32_t num_gtes_per_gte
;
58 } __attribute__((packed
)) VMDK4Header
;
60 #define L2_CACHE_SIZE 16
62 typedef struct BDRVVmdkState
{
64 int64_t l1_table_offset
;
65 int64_t l1_backup_table_offset
;
67 uint32_t *l1_backup_table
;
69 uint32_t l1_entry_sectors
;
73 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
74 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
76 unsigned int cluster_sectors
;
81 typedef struct VmdkMetaData
{
83 unsigned int l1_index
;
84 unsigned int l2_index
;
85 unsigned int l2_offset
;
89 typedef struct ActiveBDRVState
{
90 BlockDriverState
*hd
; // active image handler
91 uint64_t cluster_offset
; // current write offset
94 static ActiveBDRVState activeBDRV
;
96 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
102 magic
= be32_to_cpu(*(uint32_t *)buf
);
103 if (magic
== VMDK3_MAGIC
||
104 magic
== VMDK4_MAGIC
)
112 #define SECTOR_SIZE 512
113 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
114 #define HEADER_SIZE 512 // first sector of 512 bytes
116 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
118 BDRVVmdkState
*s
= bs
->opaque
;
119 char desc
[DESC_SIZE
];
121 char *p_name
, *cid_str
;
124 /* the descriptor offset = 0x200 */
125 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
129 cid_str
= "parentCID";
130 cid_str_size
= sizeof("parentCID");
133 cid_str_size
= sizeof("CID");
136 if ((p_name
= strstr(desc
,cid_str
)) != 0) {
137 p_name
+= cid_str_size
;
138 sscanf(p_name
,"%x",&cid
);
144 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
146 BDRVVmdkState
*s
= bs
->opaque
;
147 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
148 char *p_name
, *tmp_str
;
150 /* the descriptor offset = 0x200 */
151 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
154 tmp_str
= strstr(desc
,"parentCID");
155 strcpy(tmp_desc
, tmp_str
);
156 if ((p_name
= strstr(desc
,"CID")) != 0) {
157 p_name
+= sizeof("CID");
158 sprintf(p_name
,"%x\n",cid
);
159 strcat(desc
,tmp_desc
);
162 if (bdrv_pwrite(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
167 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
170 BDRVVmdkState
*s
= bs
->opaque
;
171 BlockDriverState
*p_bs
= s
->hd
->backing_hd
;
175 cur_pcid
= vmdk_read_cid(p_bs
,0);
176 if (s
->parent_cid
!= cur_pcid
)
185 static int vmdk_snapshot_create(const char *filename
, const char *backing_file
)
189 char *p_name
, *gd_buf
, *rgd_buf
;
190 const char *real_filename
, *temp_str
;
192 uint32_t gde_entries
, gd_size
;
193 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
194 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
195 char *desc_template
=
196 "# Disk DescriptorFile\n"
200 "createType=\"monolithicSparse\"\n"
201 "parentFileNameHint=\"%s\"\n"
203 "# Extent description\n"
204 "RW %lu SPARSE \"%s\"\n"
206 "# The Disk Data Base \n"
210 snp_fd
= open(filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
213 p_fd
= open(backing_file
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
219 /* read the header */
220 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1)
222 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
)
225 /* write the header */
226 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1)
228 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1)
231 memset(&header
, 0, sizeof(header
));
232 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
234 ftruncate(snp_fd
, header
.grain_offset
<< 9);
235 /* the descriptor offset = 0x200 */
236 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1)
238 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
)
241 if ((p_name
= strstr(p_desc
,"CID")) != 0) {
242 p_name
+= sizeof("CID");
243 sscanf(p_name
,"%x",&p_cid
);
246 real_filename
= filename
;
247 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
248 real_filename
= temp_str
+ 1;
249 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
250 real_filename
= temp_str
+ 1;
251 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
252 real_filename
= temp_str
+ 1;
254 sprintf(s_desc
, desc_template
, p_cid
, p_cid
, backing_file
255 , (uint32_t)header
.capacity
, real_filename
);
257 /* write the descriptor */
258 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1)
260 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1)
263 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
264 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
265 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
267 * Each GDE span 32M disk, means:
268 * 512 GTE per GT, each GTE points to grain
270 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
273 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
274 gd_size
= gde_entries
* sizeof(uint32_t);
277 rgd_buf
= qemu_malloc(gd_size
);
280 if (lseek(p_fd
, rgd_offset
, SEEK_SET
) == -1)
282 if (read(p_fd
, rgd_buf
, gd_size
) != gd_size
)
284 if (lseek(snp_fd
, rgd_offset
, SEEK_SET
) == -1)
286 if (write(snp_fd
, rgd_buf
, gd_size
) == -1)
291 gd_buf
= qemu_malloc(gd_size
);
294 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1)
296 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
)
298 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1)
300 if (write(snp_fd
, gd_buf
, gd_size
) == -1)
318 static void vmdk_parent_close(BlockDriverState
*bs
)
321 bdrv_close(bs
->backing_hd
);
325 static int vmdk_parent_open(BlockDriverState
*bs
, const char * filename
)
327 BDRVVmdkState
*s
= bs
->opaque
;
329 char desc
[DESC_SIZE
];
330 char parent_img_name
[1024];
332 /* the descriptor offset = 0x200 */
333 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
336 if ((p_name
= strstr(desc
,"parentFileNameHint")) != 0) {
338 struct stat file_buf
;
340 p_name
+= sizeof("parentFileNameHint") + 1;
341 if ((end_name
= strchr(p_name
,'\"')) == 0)
344 strncpy(s
->hd
->backing_file
, p_name
, end_name
- p_name
);
345 if (stat(s
->hd
->backing_file
, &file_buf
) != 0) {
346 path_combine(parent_img_name
, sizeof(parent_img_name
),
347 filename
, s
->hd
->backing_file
);
349 strcpy(parent_img_name
, s
->hd
->backing_file
);
352 s
->hd
->backing_hd
= bdrv_new("");
353 if (!s
->hd
->backing_hd
) {
359 if (bdrv_open(s
->hd
->backing_hd
, parent_img_name
, BDRV_O_RDONLY
) < 0)
367 static int vmdk_open(BlockDriverState
*bs
, const char *filename
, int flags
)
369 BDRVVmdkState
*s
= bs
->opaque
;
374 // Parent must be opened as RO.
375 flags
= BDRV_O_RDONLY
;
376 fprintf(stderr
, "(VMDK) image open: flags=0x%x filename=%s\n", flags
, bs
->filename
);
378 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
381 if (bdrv_pread(s
->hd
, 0, &magic
, sizeof(magic
)) != sizeof(magic
))
384 magic
= be32_to_cpu(magic
);
385 if (magic
== VMDK3_MAGIC
) {
388 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
390 s
->cluster_sectors
= le32_to_cpu(header
.granularity
);
393 bs
->total_sectors
= le32_to_cpu(header
.disk_sectors
);
394 s
->l1_table_offset
= le32_to_cpu(header
.l1dir_offset
) << 9;
395 s
->l1_backup_table_offset
= 0;
396 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
397 } else if (magic
== VMDK4_MAGIC
) {
400 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
402 bs
->total_sectors
= le64_to_cpu(header
.capacity
);
403 s
->cluster_sectors
= le64_to_cpu(header
.granularity
);
404 s
->l2_size
= le32_to_cpu(header
.num_gtes_per_gte
);
405 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
406 if (s
->l1_entry_sectors
<= 0)
408 s
->l1_size
= (bs
->total_sectors
+ s
->l1_entry_sectors
- 1)
409 / s
->l1_entry_sectors
;
410 s
->l1_table_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
411 s
->l1_backup_table_offset
= le64_to_cpu(header
.gd_offset
) << 9;
418 // try to open parent images, if exist
419 if (vmdk_parent_open(bs
, filename
) != 0)
421 // write the CID once after the image creation
422 s
->parent_cid
= vmdk_read_cid(bs
,1);
427 /* read the L1 table */
428 l1_size
= s
->l1_size
* sizeof(uint32_t);
429 s
->l1_table
= qemu_malloc(l1_size
);
432 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_size
) != l1_size
)
434 for(i
= 0; i
< s
->l1_size
; i
++) {
435 le32_to_cpus(&s
->l1_table
[i
]);
438 if (s
->l1_backup_table_offset
) {
439 s
->l1_backup_table
= qemu_malloc(l1_size
);
440 if (!s
->l1_backup_table
)
442 if (bdrv_pread(s
->hd
, s
->l1_backup_table_offset
, s
->l1_backup_table
, l1_size
) != l1_size
)
444 for(i
= 0; i
< s
->l1_size
; i
++) {
445 le32_to_cpus(&s
->l1_backup_table
[i
]);
449 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
454 qemu_free(s
->l1_backup_table
);
455 qemu_free(s
->l1_table
);
456 qemu_free(s
->l2_cache
);
461 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
462 uint64_t offset
, int allocate
);
464 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
465 uint64_t offset
, int allocate
)
467 uint64_t parent_cluster_offset
;
468 BDRVVmdkState
*s
= bs
->opaque
;
469 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
471 // we will be here if it's first write on non-exist grain(cluster).
472 // try to read from parent image, if exist
473 if (s
->hd
->backing_hd
) {
474 BDRVVmdkState
*ps
= s
->hd
->backing_hd
->opaque
;
476 if (!vmdk_is_cid_valid(bs
))
479 parent_cluster_offset
= get_cluster_offset(s
->hd
->backing_hd
, NULL
, offset
, allocate
);
481 if (parent_cluster_offset
) {
482 BDRVVmdkState
*act_s
= activeBDRV
.hd
->opaque
;
484 if (bdrv_pread(ps
->hd
, parent_cluster_offset
, whole_grain
, ps
->cluster_sectors
*512) != ps
->cluster_sectors
*512)
487 //Write grain only into the active image
488 if (bdrv_pwrite(act_s
->hd
, activeBDRV
.cluster_offset
<< 9, whole_grain
, sizeof(whole_grain
)) != sizeof(whole_grain
))
495 static int vmdk_L2update(BlockDriverState
*bs
, VmdkMetaData
*m_data
)
497 BDRVVmdkState
*s
= bs
->opaque
;
499 /* update L2 table */
500 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
501 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
503 /* update backup L2 table */
504 if (s
->l1_backup_table_offset
!= 0) {
505 m_data
->l2_offset
= s
->l1_backup_table
[m_data
->l1_index
];
506 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
507 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
514 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
515 uint64_t offset
, int allocate
)
517 BDRVVmdkState
*s
= bs
->opaque
;
518 unsigned int l1_index
, l2_offset
, l2_index
;
520 uint32_t min_count
, *l2_table
, tmp
= 0;
521 uint64_t cluster_offset
;
526 l1_index
= (offset
>> 9) / s
->l1_entry_sectors
;
527 if (l1_index
>= s
->l1_size
)
529 l2_offset
= s
->l1_table
[l1_index
];
532 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
533 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
534 /* increment the hit count */
535 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
536 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
537 s
->l2_cache_counts
[j
] >>= 1;
540 l2_table
= s
->l2_cache
+ (i
* s
->l2_size
);
544 /* not found: load a new entry in the least used one */
546 min_count
= 0xffffffff;
547 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
548 if (s
->l2_cache_counts
[i
] < min_count
) {
549 min_count
= s
->l2_cache_counts
[i
];
553 l2_table
= s
->l2_cache
+ (min_index
* s
->l2_size
);
554 if (bdrv_pread(s
->hd
, (int64_t)l2_offset
* 512, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
555 s
->l2_size
* sizeof(uint32_t))
558 s
->l2_cache_offsets
[min_index
] = l2_offset
;
559 s
->l2_cache_counts
[min_index
] = 1;
561 l2_index
= ((offset
>> 9) / s
->cluster_sectors
) % s
->l2_size
;
562 cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
564 if (!cluster_offset
) {
567 // Avoid the L2 tables update for the images that have snapshots.
569 cluster_offset
= bdrv_getlength(s
->hd
);
570 bdrv_truncate(s
->hd
, cluster_offset
+ (s
->cluster_sectors
<< 9));
572 cluster_offset
>>= 9;
573 tmp
= cpu_to_le32(cluster_offset
);
574 l2_table
[l2_index
] = tmp
;
575 // Save the active image state
576 activeBDRV
.cluster_offset
= cluster_offset
;
579 /* First of all we write grain itself, to avoid race condition
580 * that may to corrupt the image.
581 * This problem may occur because of insufficient space on host disk
582 * or inappropriate VM shutdown.
584 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
588 m_data
->offset
= tmp
;
589 m_data
->l1_index
= l1_index
;
590 m_data
->l2_index
= l2_index
;
591 m_data
->l2_offset
= l2_offset
;
595 cluster_offset
<<= 9;
596 return cluster_offset
;
599 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
600 int nb_sectors
, int *pnum
)
602 BDRVVmdkState
*s
= bs
->opaque
;
603 int index_in_cluster
, n
;
604 uint64_t cluster_offset
;
606 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
607 index_in_cluster
= sector_num
% s
->cluster_sectors
;
608 n
= s
->cluster_sectors
- index_in_cluster
;
612 return (cluster_offset
!= 0);
615 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
616 uint8_t *buf
, int nb_sectors
)
618 BDRVVmdkState
*s
= bs
->opaque
;
619 int index_in_cluster
, n
, ret
;
620 uint64_t cluster_offset
;
622 while (nb_sectors
> 0) {
623 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
624 index_in_cluster
= sector_num
% s
->cluster_sectors
;
625 n
= s
->cluster_sectors
- index_in_cluster
;
628 if (!cluster_offset
) {
629 // try to read from parent image, if exist
630 if (s
->hd
->backing_hd
) {
631 if (!vmdk_is_cid_valid(bs
))
633 ret
= bdrv_read(s
->hd
->backing_hd
, sector_num
, buf
, n
);
637 memset(buf
, 0, 512 * n
);
640 if(bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
650 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
651 const uint8_t *buf
, int nb_sectors
)
653 BDRVVmdkState
*s
= bs
->opaque
;
655 int index_in_cluster
, n
;
656 uint64_t cluster_offset
;
657 static int cid_update
= 0;
659 if (sector_num
> bs
->total_sectors
) {
661 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
662 " total_sectors=0x%" PRIx64
"\n",
663 sector_num
, bs
->total_sectors
);
667 while (nb_sectors
> 0) {
668 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
669 n
= s
->cluster_sectors
- index_in_cluster
;
672 cluster_offset
= get_cluster_offset(bs
, &m_data
, sector_num
<< 9, 1);
676 if (bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
679 /* update L2 tables */
680 if (vmdk_L2update(bs
, &m_data
) == -1)
687 // update CID on the first write every time the virtual disk is opened
689 vmdk_write_cid(bs
, time(NULL
));
696 static int vmdk_create(const char *filename
, int64_t total_size
,
697 const char *backing_file
, int flags
)
701 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
702 char *desc_template
=
703 "# Disk DescriptorFile\n"
706 "parentCID=ffffffff\n"
707 "createType=\"monolithicSparse\"\n"
709 "# Extent description\n"
710 "RW %lu SPARSE \"%s\"\n"
712 "# The Disk Data Base \n"
715 "ddb.virtualHWVersion = \"%d\"\n"
716 "ddb.geometry.cylinders = \"%lu\"\n"
717 "ddb.geometry.heads = \"16\"\n"
718 "ddb.geometry.sectors = \"63\"\n"
719 "ddb.adapterType = \"ide\"\n";
721 const char *real_filename
, *temp_str
;
723 /* XXX: add support for backing file */
725 return vmdk_snapshot_create(filename
, backing_file
);
728 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
732 magic
= cpu_to_be32(VMDK4_MAGIC
);
733 memset(&header
, 0, sizeof(header
));
734 header
.version
= cpu_to_le32(1);
735 header
.flags
= cpu_to_le32(3); /* ?? */
736 header
.capacity
= cpu_to_le64(total_size
);
737 header
.granularity
= cpu_to_le64(128);
738 header
.num_gtes_per_gte
= cpu_to_le32(512);
740 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
741 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
742 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
743 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
745 header
.desc_offset
= 1;
746 header
.desc_size
= 20;
747 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
748 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
749 header
.grain_offset
=
750 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
751 header
.granularity
- 1) / header
.granularity
) *
754 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
755 header
.desc_size
= cpu_to_le64(header
.desc_size
);
756 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
757 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
758 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
760 header
.check_bytes
[0] = 0xa;
761 header
.check_bytes
[1] = 0x20;
762 header
.check_bytes
[2] = 0xd;
763 header
.check_bytes
[3] = 0xa;
765 /* write all the data */
766 write(fd
, &magic
, sizeof(magic
));
767 write(fd
, &header
, sizeof(header
));
769 ftruncate(fd
, header
.grain_offset
<< 9);
771 /* write grain directory */
772 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
773 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
774 i
< gt_count
; i
++, tmp
+= gt_size
)
775 write(fd
, &tmp
, sizeof(tmp
));
777 /* write backup grain directory */
778 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
779 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
780 i
< gt_count
; i
++, tmp
+= gt_size
)
781 write(fd
, &tmp
, sizeof(tmp
));
783 /* compose the descriptor */
784 real_filename
= filename
;
785 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
786 real_filename
= temp_str
+ 1;
787 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
788 real_filename
= temp_str
+ 1;
789 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
790 real_filename
= temp_str
+ 1;
791 sprintf(desc
, desc_template
, time(NULL
), (unsigned long)total_size
,
792 real_filename
, (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4), total_size
/ (63 * 16));
794 /* write the descriptor */
795 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
796 write(fd
, desc
, strlen(desc
));
802 static void vmdk_close(BlockDriverState
*bs
)
804 BDRVVmdkState
*s
= bs
->opaque
;
806 qemu_free(s
->l1_table
);
807 qemu_free(s
->l2_cache
);
809 // try to close parent image, if exist
810 vmdk_parent_close(s
->hd
);
813 static void vmdk_flush(BlockDriverState
*bs
)
815 BDRVVmdkState
*s
= bs
->opaque
;
819 BlockDriver bdrv_vmdk
= {
821 sizeof(BDRVVmdkState
),