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
;
97 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
103 magic
= be32_to_cpu(*(uint32_t *)buf
);
104 if (magic
== VMDK3_MAGIC
||
105 magic
== VMDK4_MAGIC
)
113 #define SECTOR_SIZE 512
114 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
115 #define HEADER_SIZE 512 // first sector of 512 bytes
117 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
119 BDRVVmdkState
*s
= bs
->opaque
;
120 char desc
[DESC_SIZE
];
122 const char *p_name
, *cid_str
;
125 /* the descriptor offset = 0x200 */
126 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
130 cid_str
= "parentCID";
131 cid_str_size
= sizeof("parentCID");
134 cid_str_size
= sizeof("CID");
137 if ((p_name
= strstr(desc
,cid_str
)) != 0) {
138 p_name
+= cid_str_size
;
139 sscanf(p_name
,"%x",&cid
);
145 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
147 BDRVVmdkState
*s
= bs
->opaque
;
148 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
149 char *p_name
, *tmp_str
;
151 /* the descriptor offset = 0x200 */
152 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
155 tmp_str
= strstr(desc
,"parentCID");
156 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
157 if ((p_name
= strstr(desc
,"CID")) != 0) {
158 p_name
+= sizeof("CID");
159 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
160 pstrcat(desc
, sizeof(desc
), tmp_desc
);
163 if (bdrv_pwrite(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
168 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
171 BDRVVmdkState
*s
= bs
->opaque
;
172 BlockDriverState
*p_bs
= s
->hd
->backing_hd
;
176 cur_pcid
= vmdk_read_cid(p_bs
,0);
177 if (s
->parent_cid
!= cur_pcid
)
186 static int vmdk_snapshot_create(const char *filename
, const char *backing_file
)
190 char *p_name
, *gd_buf
, *rgd_buf
;
191 const char *real_filename
, *temp_str
;
193 uint32_t gde_entries
, gd_size
;
194 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
195 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
196 static const char desc_template
[] =
197 "# Disk DescriptorFile\n"
201 "createType=\"monolithicSparse\"\n"
202 "parentFileNameHint=\"%s\"\n"
204 "# Extent description\n"
205 "RW %u SPARSE \"%s\"\n"
207 "# The Disk Data Base \n"
211 snp_fd
= open(filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
214 p_fd
= open(backing_file
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
220 /* read the header */
221 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1)
223 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
)
226 /* write the header */
227 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1)
229 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1)
232 memset(&header
, 0, sizeof(header
));
233 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
235 ftruncate(snp_fd
, header
.grain_offset
<< 9);
236 /* the descriptor offset = 0x200 */
237 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1)
239 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
)
242 if ((p_name
= strstr(p_desc
,"CID")) != 0) {
243 p_name
+= sizeof("CID");
244 sscanf(p_name
,"%x",&p_cid
);
247 real_filename
= filename
;
248 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
249 real_filename
= temp_str
+ 1;
250 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
251 real_filename
= temp_str
+ 1;
252 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
253 real_filename
= temp_str
+ 1;
255 snprintf(s_desc
, sizeof(s_desc
), desc_template
, p_cid
, p_cid
, backing_file
,
256 (uint32_t)header
.capacity
, real_filename
);
258 /* write the descriptor */
259 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1)
261 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1)
264 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
265 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
266 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
268 * Each GDE span 32M disk, means:
269 * 512 GTE per GT, each GTE points to grain
271 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
274 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
275 gd_size
= gde_entries
* sizeof(uint32_t);
278 rgd_buf
= qemu_malloc(gd_size
);
279 if (lseek(p_fd
, rgd_offset
, SEEK_SET
) == -1)
281 if (read(p_fd
, rgd_buf
, gd_size
) != gd_size
)
283 if (lseek(snp_fd
, rgd_offset
, SEEK_SET
) == -1)
285 if (write(snp_fd
, rgd_buf
, gd_size
) == -1)
290 gd_buf
= qemu_malloc(gd_size
);
291 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1)
293 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
)
295 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1)
297 if (write(snp_fd
, gd_buf
, gd_size
) == -1)
315 static void vmdk_parent_close(BlockDriverState
*bs
)
318 bdrv_close(bs
->backing_hd
);
321 static int parent_open
= 0;
322 static int vmdk_parent_open(BlockDriverState
*bs
, const char * filename
)
324 BDRVVmdkState
*s
= bs
->opaque
;
326 char desc
[DESC_SIZE
];
327 char parent_img_name
[1024];
329 /* the descriptor offset = 0x200 */
330 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
333 if ((p_name
= strstr(desc
,"parentFileNameHint")) != 0) {
335 struct stat file_buf
;
337 p_name
+= sizeof("parentFileNameHint") + 1;
338 if ((end_name
= strchr(p_name
,'\"')) == 0)
340 if ((end_name
- p_name
) > sizeof (s
->hd
->backing_file
) - 1)
343 pstrcpy(s
->hd
->backing_file
, end_name
- p_name
+ 1, p_name
);
344 if (stat(s
->hd
->backing_file
, &file_buf
) != 0) {
345 path_combine(parent_img_name
, sizeof(parent_img_name
),
346 filename
, s
->hd
->backing_file
);
348 pstrcpy(parent_img_name
, sizeof(parent_img_name
),
349 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
;
377 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
380 if (bdrv_pread(s
->hd
, 0, &magic
, sizeof(magic
)) != sizeof(magic
))
383 magic
= be32_to_cpu(magic
);
384 if (magic
== VMDK3_MAGIC
) {
387 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
389 s
->cluster_sectors
= le32_to_cpu(header
.granularity
);
392 bs
->total_sectors
= le32_to_cpu(header
.disk_sectors
);
393 s
->l1_table_offset
= le32_to_cpu(header
.l1dir_offset
) << 9;
394 s
->l1_backup_table_offset
= 0;
395 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
396 } else if (magic
== VMDK4_MAGIC
) {
399 if (bdrv_pread(s
->hd
, sizeof(magic
), &header
, sizeof(header
)) != sizeof(header
))
401 bs
->total_sectors
= le64_to_cpu(header
.capacity
);
402 s
->cluster_sectors
= le64_to_cpu(header
.granularity
);
403 s
->l2_size
= le32_to_cpu(header
.num_gtes_per_gte
);
404 s
->l1_entry_sectors
= s
->l2_size
* s
->cluster_sectors
;
405 if (s
->l1_entry_sectors
<= 0)
407 s
->l1_size
= (bs
->total_sectors
+ s
->l1_entry_sectors
- 1)
408 / s
->l1_entry_sectors
;
409 s
->l1_table_offset
= le64_to_cpu(header
.rgd_offset
) << 9;
410 s
->l1_backup_table_offset
= le64_to_cpu(header
.gd_offset
) << 9;
417 // try to open parent images, if exist
418 if (vmdk_parent_open(bs
, filename
) != 0)
420 // write the CID once after the image creation
421 s
->parent_cid
= vmdk_read_cid(bs
,1);
426 /* read the L1 table */
427 l1_size
= s
->l1_size
* sizeof(uint32_t);
428 s
->l1_table
= qemu_malloc(l1_size
);
429 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_size
) != l1_size
)
431 for(i
= 0; i
< s
->l1_size
; i
++) {
432 le32_to_cpus(&s
->l1_table
[i
]);
435 if (s
->l1_backup_table_offset
) {
436 s
->l1_backup_table
= qemu_malloc(l1_size
);
437 if (bdrv_pread(s
->hd
, s
->l1_backup_table_offset
, s
->l1_backup_table
, l1_size
) != l1_size
)
439 for(i
= 0; i
< s
->l1_size
; i
++) {
440 le32_to_cpus(&s
->l1_backup_table
[i
]);
444 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
447 qemu_free(s
->l1_backup_table
);
448 qemu_free(s
->l1_table
);
449 qemu_free(s
->l2_cache
);
454 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
455 uint64_t offset
, int allocate
);
457 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
458 uint64_t offset
, int allocate
)
460 uint64_t parent_cluster_offset
;
461 BDRVVmdkState
*s
= bs
->opaque
;
462 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
464 // we will be here if it's first write on non-exist grain(cluster).
465 // try to read from parent image, if exist
466 if (s
->hd
->backing_hd
) {
467 BDRVVmdkState
*ps
= s
->hd
->backing_hd
->opaque
;
469 if (!vmdk_is_cid_valid(bs
))
472 parent_cluster_offset
= get_cluster_offset(s
->hd
->backing_hd
, NULL
, offset
, allocate
);
474 if (parent_cluster_offset
) {
475 BDRVVmdkState
*act_s
= activeBDRV
.hd
->opaque
;
477 if (bdrv_pread(ps
->hd
, parent_cluster_offset
, whole_grain
, ps
->cluster_sectors
*512) != ps
->cluster_sectors
*512)
480 //Write grain only into the active image
481 if (bdrv_pwrite(act_s
->hd
, activeBDRV
.cluster_offset
<< 9, whole_grain
, sizeof(whole_grain
)) != sizeof(whole_grain
))
488 static int vmdk_L2update(BlockDriverState
*bs
, VmdkMetaData
*m_data
)
490 BDRVVmdkState
*s
= bs
->opaque
;
492 /* update L2 table */
493 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
494 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
496 /* update backup L2 table */
497 if (s
->l1_backup_table_offset
!= 0) {
498 m_data
->l2_offset
= s
->l1_backup_table
[m_data
->l1_index
];
499 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
500 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
507 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
508 uint64_t offset
, int allocate
)
510 BDRVVmdkState
*s
= bs
->opaque
;
511 unsigned int l1_index
, l2_offset
, l2_index
;
513 uint32_t min_count
, *l2_table
, tmp
= 0;
514 uint64_t cluster_offset
;
519 l1_index
= (offset
>> 9) / s
->l1_entry_sectors
;
520 if (l1_index
>= s
->l1_size
)
522 l2_offset
= s
->l1_table
[l1_index
];
525 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
526 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
527 /* increment the hit count */
528 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
529 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
530 s
->l2_cache_counts
[j
] >>= 1;
533 l2_table
= s
->l2_cache
+ (i
* s
->l2_size
);
537 /* not found: load a new entry in the least used one */
539 min_count
= 0xffffffff;
540 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
541 if (s
->l2_cache_counts
[i
] < min_count
) {
542 min_count
= s
->l2_cache_counts
[i
];
546 l2_table
= s
->l2_cache
+ (min_index
* s
->l2_size
);
547 if (bdrv_pread(s
->hd
, (int64_t)l2_offset
* 512, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
548 s
->l2_size
* sizeof(uint32_t))
551 s
->l2_cache_offsets
[min_index
] = l2_offset
;
552 s
->l2_cache_counts
[min_index
] = 1;
554 l2_index
= ((offset
>> 9) / s
->cluster_sectors
) % s
->l2_size
;
555 cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
557 if (!cluster_offset
) {
560 // Avoid the L2 tables update for the images that have snapshots.
562 cluster_offset
= bdrv_getlength(s
->hd
);
563 bdrv_truncate(s
->hd
, cluster_offset
+ (s
->cluster_sectors
<< 9));
565 cluster_offset
>>= 9;
566 tmp
= cpu_to_le32(cluster_offset
);
567 l2_table
[l2_index
] = tmp
;
568 // Save the active image state
569 activeBDRV
.cluster_offset
= cluster_offset
;
572 /* First of all we write grain itself, to avoid race condition
573 * that may to corrupt the image.
574 * This problem may occur because of insufficient space on host disk
575 * or inappropriate VM shutdown.
577 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
581 m_data
->offset
= tmp
;
582 m_data
->l1_index
= l1_index
;
583 m_data
->l2_index
= l2_index
;
584 m_data
->l2_offset
= l2_offset
;
588 cluster_offset
<<= 9;
589 return cluster_offset
;
592 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
593 int nb_sectors
, int *pnum
)
595 BDRVVmdkState
*s
= bs
->opaque
;
596 int index_in_cluster
, n
;
597 uint64_t cluster_offset
;
599 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
600 index_in_cluster
= sector_num
% s
->cluster_sectors
;
601 n
= s
->cluster_sectors
- index_in_cluster
;
605 return (cluster_offset
!= 0);
608 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
609 uint8_t *buf
, int nb_sectors
)
611 BDRVVmdkState
*s
= bs
->opaque
;
612 int index_in_cluster
, n
, ret
;
613 uint64_t cluster_offset
;
615 while (nb_sectors
> 0) {
616 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
617 index_in_cluster
= sector_num
% s
->cluster_sectors
;
618 n
= s
->cluster_sectors
- index_in_cluster
;
621 if (!cluster_offset
) {
622 // try to read from parent image, if exist
623 if (s
->hd
->backing_hd
) {
624 if (!vmdk_is_cid_valid(bs
))
626 ret
= bdrv_read(s
->hd
->backing_hd
, sector_num
, buf
, n
);
630 memset(buf
, 0, 512 * n
);
633 if(bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
643 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
644 const uint8_t *buf
, int nb_sectors
)
646 BDRVVmdkState
*s
= bs
->opaque
;
648 int index_in_cluster
, n
;
649 uint64_t cluster_offset
;
650 static int cid_update
= 0;
652 if (sector_num
> bs
->total_sectors
) {
654 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
655 " total_sectors=0x%" PRIx64
"\n",
656 sector_num
, bs
->total_sectors
);
660 while (nb_sectors
> 0) {
661 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
662 n
= s
->cluster_sectors
- index_in_cluster
;
665 cluster_offset
= get_cluster_offset(bs
, &m_data
, sector_num
<< 9, 1);
669 if (bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
672 /* update L2 tables */
673 if (vmdk_L2update(bs
, &m_data
) == -1)
680 // update CID on the first write every time the virtual disk is opened
682 vmdk_write_cid(bs
, time(NULL
));
689 static int vmdk_create(const char *filename
, int64_t total_size
,
690 const char *backing_file
, int flags
)
694 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
695 static const char desc_template
[] =
696 "# Disk DescriptorFile\n"
699 "parentCID=ffffffff\n"
700 "createType=\"monolithicSparse\"\n"
702 "# Extent description\n"
703 "RW %" PRId64
" SPARSE \"%s\"\n"
705 "# The Disk Data Base \n"
708 "ddb.virtualHWVersion = \"%d\"\n"
709 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
710 "ddb.geometry.heads = \"16\"\n"
711 "ddb.geometry.sectors = \"63\"\n"
712 "ddb.adapterType = \"ide\"\n";
714 const char *real_filename
, *temp_str
;
716 /* XXX: add support for backing file */
718 return vmdk_snapshot_create(filename
, backing_file
);
721 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
725 magic
= cpu_to_be32(VMDK4_MAGIC
);
726 memset(&header
, 0, sizeof(header
));
727 header
.version
= cpu_to_le32(1);
728 header
.flags
= cpu_to_le32(3); /* ?? */
729 header
.capacity
= cpu_to_le64(total_size
);
730 header
.granularity
= cpu_to_le64(128);
731 header
.num_gtes_per_gte
= cpu_to_le32(512);
733 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
734 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
735 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
736 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
738 header
.desc_offset
= 1;
739 header
.desc_size
= 20;
740 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
741 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
742 header
.grain_offset
=
743 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
744 header
.granularity
- 1) / header
.granularity
) *
747 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
748 header
.desc_size
= cpu_to_le64(header
.desc_size
);
749 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
750 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
751 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
753 header
.check_bytes
[0] = 0xa;
754 header
.check_bytes
[1] = 0x20;
755 header
.check_bytes
[2] = 0xd;
756 header
.check_bytes
[3] = 0xa;
758 /* write all the data */
759 write(fd
, &magic
, sizeof(magic
));
760 write(fd
, &header
, sizeof(header
));
762 ftruncate(fd
, header
.grain_offset
<< 9);
764 /* write grain directory */
765 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
766 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
767 i
< gt_count
; i
++, tmp
+= gt_size
)
768 write(fd
, &tmp
, sizeof(tmp
));
770 /* write backup grain directory */
771 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
772 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
773 i
< gt_count
; i
++, tmp
+= gt_size
)
774 write(fd
, &tmp
, sizeof(tmp
));
776 /* compose the descriptor */
777 real_filename
= filename
;
778 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
779 real_filename
= temp_str
+ 1;
780 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
781 real_filename
= temp_str
+ 1;
782 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
783 real_filename
= temp_str
+ 1;
784 snprintf(desc
, sizeof(desc
), desc_template
, (unsigned int)time(NULL
),
785 total_size
, real_filename
,
786 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
787 total_size
/ (int64_t)(63 * 16));
789 /* write the descriptor */
790 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
791 write(fd
, desc
, strlen(desc
));
797 static void vmdk_close(BlockDriverState
*bs
)
799 BDRVVmdkState
*s
= bs
->opaque
;
801 qemu_free(s
->l1_table
);
802 qemu_free(s
->l2_cache
);
803 // try to close parent image, if exist
804 vmdk_parent_close(s
->hd
);
808 static void vmdk_flush(BlockDriverState
*bs
)
810 BDRVVmdkState
*s
= bs
->opaque
;
814 BlockDriver bdrv_vmdk
= {
816 sizeof(BDRVVmdkState
),