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"
30 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
31 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
36 uint32_t disk_sectors
;
38 uint32_t l1dir_offset
;
40 uint32_t file_sectors
;
43 uint32_t sectors_per_track
;
53 int32_t num_gtes_per_gte
;
59 } __attribute__((packed
)) VMDK4Header
;
61 #define L2_CACHE_SIZE 16
63 typedef struct BDRVVmdkState
{
65 int64_t l1_table_offset
;
66 int64_t l1_backup_table_offset
;
68 uint32_t *l1_backup_table
;
70 uint32_t l1_entry_sectors
;
74 uint32_t l2_cache_offsets
[L2_CACHE_SIZE
];
75 uint32_t l2_cache_counts
[L2_CACHE_SIZE
];
77 unsigned int cluster_sectors
;
82 typedef struct VmdkMetaData
{
84 unsigned int l1_index
;
85 unsigned int l2_index
;
86 unsigned int l2_offset
;
90 typedef struct ActiveBDRVState
{
91 BlockDriverState
*hd
; // active image handler
92 uint64_t cluster_offset
; // current write offset
95 static ActiveBDRVState activeBDRV
;
98 static int vmdk_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
104 magic
= be32_to_cpu(*(uint32_t *)buf
);
105 if (magic
== VMDK3_MAGIC
||
106 magic
== VMDK4_MAGIC
)
114 #define SECTOR_SIZE 512
115 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
116 #define HEADER_SIZE 512 // first sector of 512 bytes
118 static uint32_t vmdk_read_cid(BlockDriverState
*bs
, int parent
)
120 BDRVVmdkState
*s
= bs
->opaque
;
121 char desc
[DESC_SIZE
];
123 const char *p_name
, *cid_str
;
126 /* the descriptor offset = 0x200 */
127 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
131 cid_str
= "parentCID";
132 cid_str_size
= sizeof("parentCID");
135 cid_str_size
= sizeof("CID");
138 if ((p_name
= strstr(desc
,cid_str
)) != NULL
) {
139 p_name
+= cid_str_size
;
140 sscanf(p_name
,"%x",&cid
);
146 static int vmdk_write_cid(BlockDriverState
*bs
, uint32_t cid
)
148 BDRVVmdkState
*s
= bs
->opaque
;
149 char desc
[DESC_SIZE
], tmp_desc
[DESC_SIZE
];
150 char *p_name
, *tmp_str
;
152 /* the descriptor offset = 0x200 */
153 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
156 tmp_str
= strstr(desc
,"parentCID");
157 pstrcpy(tmp_desc
, sizeof(tmp_desc
), tmp_str
);
158 if ((p_name
= strstr(desc
,"CID")) != NULL
) {
159 p_name
+= sizeof("CID");
160 snprintf(p_name
, sizeof(desc
) - (p_name
- desc
), "%x\n", cid
);
161 pstrcat(desc
, sizeof(desc
), tmp_desc
);
164 if (bdrv_pwrite(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
169 static int vmdk_is_cid_valid(BlockDriverState
*bs
)
172 BDRVVmdkState
*s
= bs
->opaque
;
173 BlockDriverState
*p_bs
= s
->hd
->backing_hd
;
177 cur_pcid
= vmdk_read_cid(p_bs
,0);
178 if (s
->parent_cid
!= cur_pcid
)
187 static int vmdk_snapshot_create(const char *filename
, const char *backing_file
)
191 char *p_name
, *gd_buf
, *rgd_buf
;
192 const char *real_filename
, *temp_str
;
194 uint32_t gde_entries
, gd_size
;
195 int64_t gd_offset
, rgd_offset
, capacity
, gt_size
;
196 char p_desc
[DESC_SIZE
], s_desc
[DESC_SIZE
], hdr
[HEADER_SIZE
];
197 static const char desc_template
[] =
198 "# Disk DescriptorFile\n"
202 "createType=\"monolithicSparse\"\n"
203 "parentFileNameHint=\"%s\"\n"
205 "# Extent description\n"
206 "RW %u SPARSE \"%s\"\n"
208 "# The Disk Data Base \n"
212 snp_fd
= open(filename
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
, 0644);
215 p_fd
= open(backing_file
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
221 /* read the header */
222 if (lseek(p_fd
, 0x0, SEEK_SET
) == -1)
224 if (read(p_fd
, hdr
, HEADER_SIZE
) != HEADER_SIZE
)
227 /* write the header */
228 if (lseek(snp_fd
, 0x0, SEEK_SET
) == -1)
230 if (write(snp_fd
, hdr
, HEADER_SIZE
) == -1)
233 memset(&header
, 0, sizeof(header
));
234 memcpy(&header
,&hdr
[4], sizeof(header
)); // skip the VMDK4_MAGIC
236 ftruncate(snp_fd
, header
.grain_offset
<< 9);
237 /* the descriptor offset = 0x200 */
238 if (lseek(p_fd
, 0x200, SEEK_SET
) == -1)
240 if (read(p_fd
, p_desc
, DESC_SIZE
) != DESC_SIZE
)
243 if ((p_name
= strstr(p_desc
,"CID")) != NULL
) {
244 p_name
+= sizeof("CID");
245 sscanf(p_name
,"%x",&p_cid
);
248 real_filename
= filename
;
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;
253 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
254 real_filename
= temp_str
+ 1;
256 snprintf(s_desc
, sizeof(s_desc
), desc_template
, p_cid
, p_cid
, backing_file
,
257 (uint32_t)header
.capacity
, real_filename
);
259 /* write the descriptor */
260 if (lseek(snp_fd
, 0x200, SEEK_SET
) == -1)
262 if (write(snp_fd
, s_desc
, strlen(s_desc
)) == -1)
265 gd_offset
= header
.gd_offset
* SECTOR_SIZE
; // offset of GD table
266 rgd_offset
= header
.rgd_offset
* SECTOR_SIZE
; // offset of RGD table
267 capacity
= header
.capacity
* SECTOR_SIZE
; // Extent size
269 * Each GDE span 32M disk, means:
270 * 512 GTE per GT, each GTE points to grain
272 gt_size
= (int64_t)header
.num_gtes_per_gte
* header
.granularity
* SECTOR_SIZE
;
275 gde_entries
= (uint32_t)(capacity
/ gt_size
); // number of gde/rgde
276 gd_size
= gde_entries
* sizeof(uint32_t);
279 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
);
292 if (lseek(p_fd
, gd_offset
, SEEK_SET
) == -1)
294 if (read(p_fd
, gd_buf
, gd_size
) != gd_size
)
296 if (lseek(snp_fd
, gd_offset
, SEEK_SET
) == -1)
298 if (write(snp_fd
, gd_buf
, gd_size
) == -1)
316 static void vmdk_parent_close(BlockDriverState
*bs
)
319 bdrv_close(bs
->backing_hd
);
322 static int parent_open
= 0;
323 static int vmdk_parent_open(BlockDriverState
*bs
, const char * filename
)
325 BDRVVmdkState
*s
= bs
->opaque
;
327 char desc
[DESC_SIZE
];
328 char parent_img_name
[1024];
330 /* the descriptor offset = 0x200 */
331 if (bdrv_pread(s
->hd
, 0x200, desc
, DESC_SIZE
) != DESC_SIZE
)
334 if ((p_name
= strstr(desc
,"parentFileNameHint")) != NULL
) {
336 struct stat file_buf
;
338 p_name
+= sizeof("parentFileNameHint") + 1;
339 if ((end_name
= strchr(p_name
,'\"')) == NULL
)
341 if ((end_name
- p_name
) > sizeof (s
->hd
->backing_file
) - 1)
344 pstrcpy(s
->hd
->backing_file
, end_name
- p_name
+ 1, 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 pstrcpy(parent_img_name
, sizeof(parent_img_name
),
350 s
->hd
->backing_file
);
353 s
->hd
->backing_hd
= bdrv_new("");
354 if (!s
->hd
->backing_hd
) {
360 if (bdrv_open(s
->hd
->backing_hd
, parent_img_name
, BDRV_O_RDONLY
) < 0)
368 static int vmdk_open(BlockDriverState
*bs
, const char *filename
, int flags
)
370 BDRVVmdkState
*s
= bs
->opaque
;
375 // Parent must be opened as RO.
376 flags
= BDRV_O_RDONLY
;
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
);
430 if (bdrv_pread(s
->hd
, s
->l1_table_offset
, s
->l1_table
, l1_size
) != l1_size
)
432 for(i
= 0; i
< s
->l1_size
; i
++) {
433 le32_to_cpus(&s
->l1_table
[i
]);
436 if (s
->l1_backup_table_offset
) {
437 s
->l1_backup_table
= qemu_malloc(l1_size
);
438 if (bdrv_pread(s
->hd
, s
->l1_backup_table_offset
, s
->l1_backup_table
, l1_size
) != l1_size
)
440 for(i
= 0; i
< s
->l1_size
; i
++) {
441 le32_to_cpus(&s
->l1_backup_table
[i
]);
445 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint32_t));
448 qemu_free(s
->l1_backup_table
);
449 qemu_free(s
->l1_table
);
450 qemu_free(s
->l2_cache
);
455 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
456 uint64_t offset
, int allocate
);
458 static int get_whole_cluster(BlockDriverState
*bs
, uint64_t cluster_offset
,
459 uint64_t offset
, int allocate
)
461 uint64_t parent_cluster_offset
;
462 BDRVVmdkState
*s
= bs
->opaque
;
463 uint8_t whole_grain
[s
->cluster_sectors
*512]; // 128 sectors * 512 bytes each = grain size 64KB
465 // we will be here if it's first write on non-exist grain(cluster).
466 // try to read from parent image, if exist
467 if (s
->hd
->backing_hd
) {
468 BDRVVmdkState
*ps
= s
->hd
->backing_hd
->opaque
;
470 if (!vmdk_is_cid_valid(bs
))
473 parent_cluster_offset
= get_cluster_offset(s
->hd
->backing_hd
, NULL
, offset
, allocate
);
475 if (parent_cluster_offset
) {
476 BDRVVmdkState
*act_s
= activeBDRV
.hd
->opaque
;
478 if (bdrv_pread(ps
->hd
, parent_cluster_offset
, whole_grain
, ps
->cluster_sectors
*512) != ps
->cluster_sectors
*512)
481 //Write grain only into the active image
482 if (bdrv_pwrite(act_s
->hd
, activeBDRV
.cluster_offset
<< 9, whole_grain
, sizeof(whole_grain
)) != sizeof(whole_grain
))
489 static int vmdk_L2update(BlockDriverState
*bs
, VmdkMetaData
*m_data
)
491 BDRVVmdkState
*s
= bs
->opaque
;
493 /* update L2 table */
494 if (bdrv_pwrite(s
->hd
, ((int64_t)m_data
->l2_offset
* 512) + (m_data
->l2_index
* sizeof(m_data
->offset
)),
495 &(m_data
->offset
), sizeof(m_data
->offset
)) != sizeof(m_data
->offset
))
497 /* update backup L2 table */
498 if (s
->l1_backup_table_offset
!= 0) {
499 m_data
->l2_offset
= s
->l1_backup_table
[m_data
->l1_index
];
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
))
508 static uint64_t get_cluster_offset(BlockDriverState
*bs
, VmdkMetaData
*m_data
,
509 uint64_t offset
, int allocate
)
511 BDRVVmdkState
*s
= bs
->opaque
;
512 unsigned int l1_index
, l2_offset
, l2_index
;
514 uint32_t min_count
, *l2_table
, tmp
= 0;
515 uint64_t cluster_offset
;
520 l1_index
= (offset
>> 9) / s
->l1_entry_sectors
;
521 if (l1_index
>= s
->l1_size
)
523 l2_offset
= s
->l1_table
[l1_index
];
526 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
527 if (l2_offset
== s
->l2_cache_offsets
[i
]) {
528 /* increment the hit count */
529 if (++s
->l2_cache_counts
[i
] == 0xffffffff) {
530 for(j
= 0; j
< L2_CACHE_SIZE
; j
++) {
531 s
->l2_cache_counts
[j
] >>= 1;
534 l2_table
= s
->l2_cache
+ (i
* s
->l2_size
);
538 /* not found: load a new entry in the least used one */
540 min_count
= 0xffffffff;
541 for(i
= 0; i
< L2_CACHE_SIZE
; i
++) {
542 if (s
->l2_cache_counts
[i
] < min_count
) {
543 min_count
= s
->l2_cache_counts
[i
];
547 l2_table
= s
->l2_cache
+ (min_index
* s
->l2_size
);
548 if (bdrv_pread(s
->hd
, (int64_t)l2_offset
* 512, l2_table
, s
->l2_size
* sizeof(uint32_t)) !=
549 s
->l2_size
* sizeof(uint32_t))
552 s
->l2_cache_offsets
[min_index
] = l2_offset
;
553 s
->l2_cache_counts
[min_index
] = 1;
555 l2_index
= ((offset
>> 9) / s
->cluster_sectors
) % s
->l2_size
;
556 cluster_offset
= le32_to_cpu(l2_table
[l2_index
]);
558 if (!cluster_offset
) {
561 // Avoid the L2 tables update for the images that have snapshots.
563 cluster_offset
= bdrv_getlength(s
->hd
);
564 bdrv_truncate(s
->hd
, cluster_offset
+ (s
->cluster_sectors
<< 9));
566 cluster_offset
>>= 9;
567 tmp
= cpu_to_le32(cluster_offset
);
568 l2_table
[l2_index
] = tmp
;
569 // Save the active image state
570 activeBDRV
.cluster_offset
= cluster_offset
;
573 /* First of all we write grain itself, to avoid race condition
574 * that may to corrupt the image.
575 * This problem may occur because of insufficient space on host disk
576 * or inappropriate VM shutdown.
578 if (get_whole_cluster(bs
, cluster_offset
, offset
, allocate
) == -1)
582 m_data
->offset
= tmp
;
583 m_data
->l1_index
= l1_index
;
584 m_data
->l2_index
= l2_index
;
585 m_data
->l2_offset
= l2_offset
;
589 cluster_offset
<<= 9;
590 return cluster_offset
;
593 static int vmdk_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
594 int nb_sectors
, int *pnum
)
596 BDRVVmdkState
*s
= bs
->opaque
;
597 int index_in_cluster
, n
;
598 uint64_t cluster_offset
;
600 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
601 index_in_cluster
= sector_num
% s
->cluster_sectors
;
602 n
= s
->cluster_sectors
- index_in_cluster
;
606 return (cluster_offset
!= 0);
609 static int vmdk_read(BlockDriverState
*bs
, int64_t sector_num
,
610 uint8_t *buf
, int nb_sectors
)
612 BDRVVmdkState
*s
= bs
->opaque
;
613 int index_in_cluster
, n
, ret
;
614 uint64_t cluster_offset
;
616 while (nb_sectors
> 0) {
617 cluster_offset
= get_cluster_offset(bs
, NULL
, sector_num
<< 9, 0);
618 index_in_cluster
= sector_num
% s
->cluster_sectors
;
619 n
= s
->cluster_sectors
- index_in_cluster
;
622 if (!cluster_offset
) {
623 // try to read from parent image, if exist
624 if (s
->hd
->backing_hd
) {
625 if (!vmdk_is_cid_valid(bs
))
627 ret
= bdrv_read(s
->hd
->backing_hd
, sector_num
, buf
, n
);
631 memset(buf
, 0, 512 * n
);
634 if(bdrv_pread(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
644 static int vmdk_write(BlockDriverState
*bs
, int64_t sector_num
,
645 const uint8_t *buf
, int nb_sectors
)
647 BDRVVmdkState
*s
= bs
->opaque
;
649 int index_in_cluster
, n
;
650 uint64_t cluster_offset
;
651 static int cid_update
= 0;
653 if (sector_num
> bs
->total_sectors
) {
655 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
656 " total_sectors=0x%" PRIx64
"\n",
657 sector_num
, bs
->total_sectors
);
661 while (nb_sectors
> 0) {
662 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
663 n
= s
->cluster_sectors
- index_in_cluster
;
666 cluster_offset
= get_cluster_offset(bs
, &m_data
, sector_num
<< 9, 1);
670 if (bdrv_pwrite(s
->hd
, cluster_offset
+ index_in_cluster
* 512, buf
, n
* 512) != n
* 512)
673 /* update L2 tables */
674 if (vmdk_L2update(bs
, &m_data
) == -1)
681 // update CID on the first write every time the virtual disk is opened
683 vmdk_write_cid(bs
, time(NULL
));
690 static int vmdk_create(const char *filename
, int64_t total_size
,
691 const char *backing_file
, int flags
)
695 uint32_t tmp
, magic
, grains
, gd_size
, gt_size
, gt_count
;
696 static const char desc_template
[] =
697 "# Disk DescriptorFile\n"
700 "parentCID=ffffffff\n"
701 "createType=\"monolithicSparse\"\n"
703 "# Extent description\n"
704 "RW %" PRId64
" SPARSE \"%s\"\n"
706 "# The Disk Data Base \n"
709 "ddb.virtualHWVersion = \"%d\"\n"
710 "ddb.geometry.cylinders = \"%" PRId64
"\"\n"
711 "ddb.geometry.heads = \"16\"\n"
712 "ddb.geometry.sectors = \"63\"\n"
713 "ddb.adapterType = \"ide\"\n";
715 const char *real_filename
, *temp_str
;
717 /* XXX: add support for backing file */
719 return vmdk_snapshot_create(filename
, backing_file
);
722 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
726 magic
= cpu_to_be32(VMDK4_MAGIC
);
727 memset(&header
, 0, sizeof(header
));
728 header
.version
= cpu_to_le32(1);
729 header
.flags
= cpu_to_le32(3); /* ?? */
730 header
.capacity
= cpu_to_le64(total_size
);
731 header
.granularity
= cpu_to_le64(128);
732 header
.num_gtes_per_gte
= cpu_to_le32(512);
734 grains
= (total_size
+ header
.granularity
- 1) / header
.granularity
;
735 gt_size
= ((header
.num_gtes_per_gte
* sizeof(uint32_t)) + 511) >> 9;
736 gt_count
= (grains
+ header
.num_gtes_per_gte
- 1) / header
.num_gtes_per_gte
;
737 gd_size
= (gt_count
* sizeof(uint32_t) + 511) >> 9;
739 header
.desc_offset
= 1;
740 header
.desc_size
= 20;
741 header
.rgd_offset
= header
.desc_offset
+ header
.desc_size
;
742 header
.gd_offset
= header
.rgd_offset
+ gd_size
+ (gt_size
* gt_count
);
743 header
.grain_offset
=
744 ((header
.gd_offset
+ gd_size
+ (gt_size
* gt_count
) +
745 header
.granularity
- 1) / header
.granularity
) *
748 header
.desc_offset
= cpu_to_le64(header
.desc_offset
);
749 header
.desc_size
= cpu_to_le64(header
.desc_size
);
750 header
.rgd_offset
= cpu_to_le64(header
.rgd_offset
);
751 header
.gd_offset
= cpu_to_le64(header
.gd_offset
);
752 header
.grain_offset
= cpu_to_le64(header
.grain_offset
);
754 header
.check_bytes
[0] = 0xa;
755 header
.check_bytes
[1] = 0x20;
756 header
.check_bytes
[2] = 0xd;
757 header
.check_bytes
[3] = 0xa;
759 /* write all the data */
760 write(fd
, &magic
, sizeof(magic
));
761 write(fd
, &header
, sizeof(header
));
763 ftruncate(fd
, header
.grain_offset
<< 9);
765 /* write grain directory */
766 lseek(fd
, le64_to_cpu(header
.rgd_offset
) << 9, SEEK_SET
);
767 for (i
= 0, tmp
= header
.rgd_offset
+ gd_size
;
768 i
< gt_count
; i
++, tmp
+= gt_size
)
769 write(fd
, &tmp
, sizeof(tmp
));
771 /* write backup grain directory */
772 lseek(fd
, le64_to_cpu(header
.gd_offset
) << 9, SEEK_SET
);
773 for (i
= 0, tmp
= header
.gd_offset
+ gd_size
;
774 i
< gt_count
; i
++, tmp
+= gt_size
)
775 write(fd
, &tmp
, sizeof(tmp
));
777 /* compose the descriptor */
778 real_filename
= filename
;
779 if ((temp_str
= strrchr(real_filename
, '\\')) != NULL
)
780 real_filename
= temp_str
+ 1;
781 if ((temp_str
= strrchr(real_filename
, '/')) != NULL
)
782 real_filename
= temp_str
+ 1;
783 if ((temp_str
= strrchr(real_filename
, ':')) != NULL
)
784 real_filename
= temp_str
+ 1;
785 snprintf(desc
, sizeof(desc
), desc_template
, (unsigned int)time(NULL
),
786 total_size
, real_filename
,
787 (flags
& BLOCK_FLAG_COMPAT6
? 6 : 4),
788 total_size
/ (int64_t)(63 * 16));
790 /* write the descriptor */
791 lseek(fd
, le64_to_cpu(header
.desc_offset
) << 9, SEEK_SET
);
792 write(fd
, desc
, strlen(desc
));
798 static void vmdk_close(BlockDriverState
*bs
)
800 BDRVVmdkState
*s
= bs
->opaque
;
802 qemu_free(s
->l1_table
);
803 qemu_free(s
->l2_cache
);
804 // try to close parent image, if exist
805 vmdk_parent_close(s
->hd
);
809 static void vmdk_flush(BlockDriverState
*bs
)
811 BDRVVmdkState
*s
= bs
->opaque
;
815 static BlockDriver bdrv_vmdk
= {
816 .format_name
= "vmdk",
817 .instance_size
= sizeof(BDRVVmdkState
),
818 .bdrv_probe
= vmdk_probe
,
819 .bdrv_open
= vmdk_open
,
820 .bdrv_read
= vmdk_read
,
821 .bdrv_write
= vmdk_write
,
822 .bdrv_close
= vmdk_close
,
823 .bdrv_create
= vmdk_create
,
824 .bdrv_flush
= vmdk_flush
,
825 .bdrv_is_allocated
= vmdk_is_allocated
,
828 static void bdrv_vmdk_init(void)
830 bdrv_register(&bdrv_vmdk
);
833 block_init(bdrv_vmdk_init
);