Remove libuuid dependency
[qemu-kvm/fedora.git] / block-vmdk.c
blobbe75e0dd88f68efd78de3ac1789bc62706ac85e2
1 /*
2 * Block driver for the VMDK format
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2005 Filip Navara
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "vl.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')
32 typedef struct {
33 uint32_t version;
34 uint32_t flags;
35 uint32_t disk_sectors;
36 uint32_t granularity;
37 uint32_t l1dir_offset;
38 uint32_t l1dir_size;
39 uint32_t file_sectors;
40 uint32_t cylinders;
41 uint32_t heads;
42 uint32_t sectors_per_track;
43 } VMDK3Header;
45 typedef struct {
46 uint32_t version;
47 uint32_t flags;
48 int64_t capacity;
49 int64_t granularity;
50 int64_t desc_offset;
51 int64_t desc_size;
52 int32_t num_gtes_per_gte;
53 int64_t rgd_offset;
54 int64_t gd_offset;
55 int64_t grain_offset;
56 char filler[1];
57 char check_bytes[4];
58 } __attribute__((packed)) VMDK4Header;
60 #define L2_CACHE_SIZE 16
62 typedef struct BDRVVmdkState {
63 BlockDriverState *hd;
64 int64_t l1_table_offset;
65 int64_t l1_backup_table_offset;
66 uint32_t *l1_table;
67 uint32_t *l1_backup_table;
68 unsigned int l1_size;
69 uint32_t l1_entry_sectors;
71 unsigned int l2_size;
72 uint32_t *l2_cache;
73 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
74 uint32_t l2_cache_counts[L2_CACHE_SIZE];
76 unsigned int cluster_sectors;
77 uint32_t parent_cid;
78 DiskIOStatistics io;
79 } BDRVVmdkState;
81 DiskIOStatistics vmdk_io_statistics(BlockDriverState *bs)
83 BDRVVmdkState *s = bs->opaque;
84 // return disk I/O counters
85 return s->io;
88 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
90 uint32_t magic;
92 if (buf_size < 4)
93 return 0;
94 magic = be32_to_cpu(*(uint32_t *)buf);
95 if (magic == VMDK3_MAGIC ||
96 magic == VMDK4_MAGIC)
97 return 100;
98 else
99 return 0;
102 #define CHECK_CID 1
104 #define SECTOR_SIZE 512
105 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
106 #define HEADER_SIZE 512 // first sector of 512 bytes
108 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
110 BDRVVmdkState *s = bs->opaque;
111 char desc[DESC_SIZE];
112 uint32_t cid;
113 char *p_name, *cid_str;
114 size_t cid_str_size;
116 /* the descriptor offset = 0x200 */
117 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
118 return 0;
120 if (parent) {
121 cid_str = "parentCID";
122 cid_str_size = sizeof("parentCID");
123 } else {
124 cid_str = "CID";
125 cid_str_size = sizeof("CID");
128 if ((p_name = strstr(desc,cid_str)) != 0) {
129 p_name += cid_str_size;
130 sscanf(p_name,"%x",&cid);
133 return cid;
136 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
138 BDRVVmdkState *s = bs->opaque;
139 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
140 char *p_name, *tmp_str;
142 /* the descriptor offset = 0x200 */
143 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
144 return -1;
146 tmp_str = strstr(desc,"parentCID");
147 strcpy(tmp_desc, tmp_str);
148 if ((p_name = strstr(desc,"CID")) != 0) {
149 p_name += sizeof("CID");
150 sprintf(p_name,"%x\n",cid);
151 strcat(desc,tmp_desc);
154 if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
155 return -1;
156 return 0;
159 static int vmdk_is_cid_valid(BlockDriverState *bs)
161 #ifdef CHECK_CID
162 BDRVVmdkState *s = bs->opaque;
163 BlockDriverState *p_bs = s->hd->backing_hd;
164 uint32_t cur_pcid;
166 if (p_bs) {
167 cur_pcid = vmdk_read_cid(p_bs,0);
168 if (s->parent_cid != cur_pcid)
169 // CID not valid
170 return 0;
172 #endif
173 // CID valid
174 return 1;
177 static int vmdk_snapshot_create(const char *filename, const char *backing_file)
179 int snp_fd, p_fd;
180 uint32_t p_cid;
181 char *p_name, *gd_buf, *rgd_buf;
182 const char *real_filename, *temp_str;
183 VMDK4Header header;
184 uint32_t gde_entries, gd_size;
185 int64_t gd_offset, rgd_offset, capacity, gt_size;
186 char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
187 char *desc_template =
188 "# Disk DescriptorFile\n"
189 "version=1\n"
190 "CID=%x\n"
191 "parentCID=%x\n"
192 "createType=\"monolithicSparse\"\n"
193 "parentFileNameHint=\"%s\"\n"
194 "\n"
195 "# Extent description\n"
196 "RW %lu SPARSE \"%s\"\n"
197 "\n"
198 "# The Disk Data Base \n"
199 "#DDB\n"
200 "\n";
202 snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
203 if (snp_fd < 0)
204 return -1;
205 p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
206 if (p_fd < 0) {
207 close(snp_fd);
208 return -1;
211 /* read the header */
212 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
213 goto fail;
214 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
215 goto fail;
217 /* write the header */
218 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
219 goto fail;
220 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
221 goto fail;
223 memset(&header, 0, sizeof(header));
224 memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
226 ftruncate(snp_fd, header.grain_offset << 9);
227 /* the descriptor offset = 0x200 */
228 if (lseek(p_fd, 0x200, SEEK_SET) == -1)
229 goto fail;
230 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
231 goto fail;
233 if ((p_name = strstr(p_desc,"CID")) != 0) {
234 p_name += sizeof("CID");
235 sscanf(p_name,"%x",&p_cid);
238 real_filename = filename;
239 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
240 real_filename = temp_str + 1;
241 if ((temp_str = strrchr(real_filename, '/')) != NULL)
242 real_filename = temp_str + 1;
243 if ((temp_str = strrchr(real_filename, ':')) != NULL)
244 real_filename = temp_str + 1;
246 sprintf(s_desc, desc_template, p_cid, p_cid, backing_file
247 , (uint32_t)header.capacity, real_filename);
249 /* write the descriptor */
250 if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
251 goto fail;
252 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
253 goto fail;
255 gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
256 rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
257 capacity = header.capacity * SECTOR_SIZE; // Extent size
259 * Each GDE span 32M disk, means:
260 * 512 GTE per GT, each GTE points to grain
262 gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
263 if (!gt_size)
264 goto fail;
265 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
266 gd_size = gde_entries * sizeof(uint32_t);
268 /* write RGD */
269 rgd_buf = qemu_malloc(gd_size);
270 if (!rgd_buf)
271 goto fail;
272 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
273 goto fail_rgd;
274 if (read(p_fd, rgd_buf, gd_size) != gd_size)
275 goto fail_rgd;
276 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
277 goto fail_rgd;
278 if (write(snp_fd, rgd_buf, gd_size) == -1)
279 goto fail_rgd;
280 qemu_free(rgd_buf);
282 /* write GD */
283 gd_buf = qemu_malloc(gd_size);
284 if (!gd_buf)
285 goto fail_rgd;
286 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
287 goto fail_gd;
288 if (read(p_fd, gd_buf, gd_size) != gd_size)
289 goto fail_gd;
290 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
291 goto fail_gd;
292 if (write(snp_fd, gd_buf, gd_size) == -1)
293 goto fail_gd;
294 qemu_free(gd_buf);
296 close(p_fd);
297 close(snp_fd);
298 return 0;
300 fail_gd:
301 qemu_free(gd_buf);
302 fail_rgd:
303 qemu_free(rgd_buf);
304 fail:
305 close(p_fd);
306 close(snp_fd);
307 return -1;
310 static void vmdk_parent_close(BlockDriverState *bs)
312 if (bs->backing_hd)
313 bdrv_close(bs->backing_hd);
317 static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
319 BDRVVmdkState *s = bs->opaque;
320 char *p_name;
321 char desc[DESC_SIZE];
322 char parent_img_name[1024];
324 /* the descriptor offset = 0x200 */
325 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
326 return -1;
328 if ((p_name = strstr(desc,"parentFileNameHint")) != 0) {
329 char *end_name;
330 struct stat file_buf;
332 p_name += sizeof("parentFileNameHint") + 1;
333 if ((end_name = strchr(p_name,'\"')) == 0)
334 return -1;
336 strncpy(s->hd->backing_file, p_name, end_name - p_name);
337 if (stat(s->hd->backing_file, &file_buf) != 0) {
338 path_combine(parent_img_name, sizeof(parent_img_name),
339 filename, s->hd->backing_file);
340 } else {
341 strcpy(parent_img_name, s->hd->backing_file);
344 s->hd->backing_hd = bdrv_new("");
345 if (!s->hd->backing_hd) {
346 failure:
347 bdrv_close(s->hd);
348 return -1;
350 if (bdrv_open(s->hd->backing_hd, parent_img_name, 0) < 0)
351 goto failure;
354 return 0;
357 static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
359 BDRVVmdkState *s = bs->opaque;
360 uint32_t magic;
361 int l1_size, i, ret;
363 ret = bdrv_file_open(&s->hd, filename, flags);
364 if (ret < 0)
365 return ret;
366 if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
367 goto fail;
369 magic = be32_to_cpu(magic);
370 if (magic == VMDK3_MAGIC) {
371 VMDK3Header header;
373 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
374 goto fail;
375 s->cluster_sectors = le32_to_cpu(header.granularity);
376 s->l2_size = 1 << 9;
377 s->l1_size = 1 << 6;
378 bs->total_sectors = le32_to_cpu(header.disk_sectors);
379 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
380 s->l1_backup_table_offset = 0;
381 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
382 } else if (magic == VMDK4_MAGIC) {
383 VMDK4Header header;
385 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
386 goto fail;
387 bs->total_sectors = le64_to_cpu(header.capacity);
388 s->cluster_sectors = le64_to_cpu(header.granularity);
389 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
390 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
391 if (s->l1_entry_sectors <= 0)
392 goto fail;
393 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
394 / s->l1_entry_sectors;
395 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
396 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
398 // try to open parent images, if exist
399 if (vmdk_parent_open(bs, filename) != 0)
400 goto fail;
401 // write the CID once after the image creation
402 s->parent_cid = vmdk_read_cid(bs,1);
403 } else {
404 goto fail;
407 /* read the L1 table */
408 l1_size = s->l1_size * sizeof(uint32_t);
409 s->l1_table = qemu_malloc(l1_size);
410 if (!s->l1_table)
411 goto fail;
412 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
413 goto fail;
414 for(i = 0; i < s->l1_size; i++) {
415 le32_to_cpus(&s->l1_table[i]);
418 if (s->l1_backup_table_offset) {
419 s->l1_backup_table = qemu_malloc(l1_size);
420 if (!s->l1_backup_table)
421 goto fail;
422 if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
423 goto fail;
424 for(i = 0; i < s->l1_size; i++) {
425 le32_to_cpus(&s->l1_backup_table[i]);
429 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
430 if (!s->l2_cache)
431 goto fail;
432 return 0;
433 fail:
434 qemu_free(s->l1_backup_table);
435 qemu_free(s->l1_table);
436 qemu_free(s->l2_cache);
437 bdrv_delete(s->hd);
438 return -1;
441 static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate);
443 static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
444 uint64_t offset, int allocate)
446 uint64_t parent_cluster_offset;
447 BDRVVmdkState *s = bs->opaque;
448 uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
450 // we will be here if it's first write on non-exist grain(cluster).
451 // try to read from parent image, if exist
452 if (s->hd->backing_hd) {
453 BDRVVmdkState *ps = s->hd->backing_hd->opaque;
455 if (!vmdk_is_cid_valid(bs))
456 return -1;
457 parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, offset, allocate);
458 if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) !=
459 ps->cluster_sectors*512)
460 return -1;
462 if (bdrv_pwrite(s->hd, cluster_offset << 9, whole_grain, sizeof(whole_grain)) !=
463 sizeof(whole_grain))
464 return -1;
466 return 0;
469 static uint64_t get_cluster_offset(BlockDriverState *bs,
470 uint64_t offset, int allocate)
472 BDRVVmdkState *s = bs->opaque;
473 unsigned int l1_index, l2_offset, l2_index;
474 int min_index, i, j;
475 uint32_t min_count, *l2_table, tmp;
476 uint64_t cluster_offset;
478 l1_index = (offset >> 9) / s->l1_entry_sectors;
479 if (l1_index >= s->l1_size)
480 return 0;
481 l2_offset = s->l1_table[l1_index];
482 if (!l2_offset)
483 return 0;
484 for(i = 0; i < L2_CACHE_SIZE; i++) {
485 if (l2_offset == s->l2_cache_offsets[i]) {
486 /* increment the hit count */
487 if (++s->l2_cache_counts[i] == 0xffffffff) {
488 for(j = 0; j < L2_CACHE_SIZE; j++) {
489 s->l2_cache_counts[j] >>= 1;
492 l2_table = s->l2_cache + (i * s->l2_size);
493 goto found;
496 /* not found: load a new entry in the least used one */
497 min_index = 0;
498 min_count = 0xffffffff;
499 for(i = 0; i < L2_CACHE_SIZE; i++) {
500 if (s->l2_cache_counts[i] < min_count) {
501 min_count = s->l2_cache_counts[i];
502 min_index = i;
505 l2_table = s->l2_cache + (min_index * s->l2_size);
506 if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
507 s->l2_size * sizeof(uint32_t))
508 return 0;
510 s->l2_cache_offsets[min_index] = l2_offset;
511 s->l2_cache_counts[min_index] = 1;
512 found:
513 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
514 cluster_offset = le32_to_cpu(l2_table[l2_index]);
515 if (!cluster_offset) {
516 struct stat file_buf;
518 if (!allocate)
519 return 0;
520 stat(s->hd->filename, &file_buf);
521 cluster_offset = file_buf.st_size;
522 bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
524 cluster_offset >>= 9;
525 /* update L2 table */
526 tmp = cpu_to_le32(cluster_offset);
527 l2_table[l2_index] = tmp;
528 if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)),
529 &tmp, sizeof(tmp)) != sizeof(tmp))
530 return 0;
531 /* update backup L2 table */
532 if (s->l1_backup_table_offset != 0) {
533 l2_offset = s->l1_backup_table[l1_index];
534 if (bdrv_pwrite(s->hd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)),
535 &tmp, sizeof(tmp)) != sizeof(tmp))
536 return 0;
539 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
540 return 0;
542 cluster_offset <<= 9;
543 return cluster_offset;
546 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
547 int nb_sectors, int *pnum)
549 BDRVVmdkState *s = bs->opaque;
550 int index_in_cluster, n;
551 uint64_t cluster_offset;
553 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
554 index_in_cluster = sector_num % s->cluster_sectors;
555 n = s->cluster_sectors - index_in_cluster;
556 if (n > nb_sectors)
557 n = nb_sectors;
558 *pnum = n;
559 return (cluster_offset != 0);
562 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
563 uint8_t *buf, int nb_sectors)
565 BDRVVmdkState *s = bs->opaque;
566 int index_in_cluster, n, ret;
567 uint64_t cluster_offset;
569 while (nb_sectors > 0) {
570 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
571 index_in_cluster = sector_num % s->cluster_sectors;
572 n = s->cluster_sectors - index_in_cluster;
573 if (n > nb_sectors)
574 n = nb_sectors;
575 if (!cluster_offset) {
576 // try to read from parent image, if exist
577 if (s->hd->backing_hd) {
578 if (!vmdk_is_cid_valid(bs))
579 return -1;
580 ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
581 if (ret < 0)
582 return -1;
583 } else {
584 memset(buf, 0, 512 * n);
586 } else {
587 if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
588 return -1;
590 nb_sectors -= n;
591 sector_num += n;
592 buf += n * 512;
593 s->io.read_byte_counter += (uint64_t)(n*512);
595 return 0;
598 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
599 const uint8_t *buf, int nb_sectors)
601 BDRVVmdkState *s = bs->opaque;
602 int index_in_cluster, n;
603 uint64_t cluster_offset;
604 static int cid_update = 0;
606 while (nb_sectors > 0) {
607 index_in_cluster = sector_num & (s->cluster_sectors - 1);
608 n = s->cluster_sectors - index_in_cluster;
609 if (n > nb_sectors)
610 n = nb_sectors;
611 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1);
612 if (!cluster_offset)
613 return -1;
614 if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
615 return -1;
616 nb_sectors -= n;
617 sector_num += n;
618 buf += n * 512;
619 s->io.write_byte_counter += (uint64_t)(n*512);
621 // update CID on the first write every time the virtual disk is opened
622 if (!cid_update) {
623 vmdk_write_cid(bs, time(NULL));
624 cid_update++;
627 return 0;
630 static int vmdk_create(const char *filename, int64_t total_size,
631 const char *backing_file, int flags)
633 int fd, i;
634 VMDK4Header header;
635 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
636 char *desc_template =
637 "# Disk DescriptorFile\n"
638 "version=1\n"
639 "CID=%x\n"
640 "parentCID=ffffffff\n"
641 "createType=\"monolithicSparse\"\n"
642 "\n"
643 "# Extent description\n"
644 "RW %lu SPARSE \"%s\"\n"
645 "\n"
646 "# The Disk Data Base \n"
647 "#DDB\n"
648 "\n"
649 "ddb.virtualHWVersion = \"4\"\n"
650 "ddb.geometry.cylinders = \"%lu\"\n"
651 "ddb.geometry.heads = \"16\"\n"
652 "ddb.geometry.sectors = \"63\"\n"
653 "ddb.adapterType = \"ide\"\n";
654 char desc[1024];
655 const char *real_filename, *temp_str;
657 /* XXX: add support for backing file */
658 if (backing_file) {
659 return vmdk_snapshot_create(filename, backing_file);
662 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
663 0644);
664 if (fd < 0)
665 return -1;
666 magic = cpu_to_be32(VMDK4_MAGIC);
667 memset(&header, 0, sizeof(header));
668 header.version = cpu_to_le32(1);
669 header.flags = cpu_to_le32(3); /* ?? */
670 header.capacity = cpu_to_le64(total_size);
671 header.granularity = cpu_to_le64(128);
672 header.num_gtes_per_gte = cpu_to_le32(512);
674 grains = (total_size + header.granularity - 1) / header.granularity;
675 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
676 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
677 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
679 header.desc_offset = 1;
680 header.desc_size = 20;
681 header.rgd_offset = header.desc_offset + header.desc_size;
682 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
683 header.grain_offset =
684 ((header.gd_offset + gd_size + (gt_size * gt_count) +
685 header.granularity - 1) / header.granularity) *
686 header.granularity;
688 header.desc_offset = cpu_to_le64(header.desc_offset);
689 header.desc_size = cpu_to_le64(header.desc_size);
690 header.rgd_offset = cpu_to_le64(header.rgd_offset);
691 header.gd_offset = cpu_to_le64(header.gd_offset);
692 header.grain_offset = cpu_to_le64(header.grain_offset);
694 header.check_bytes[0] = 0xa;
695 header.check_bytes[1] = 0x20;
696 header.check_bytes[2] = 0xd;
697 header.check_bytes[3] = 0xa;
699 /* write all the data */
700 write(fd, &magic, sizeof(magic));
701 write(fd, &header, sizeof(header));
703 ftruncate(fd, header.grain_offset << 9);
705 /* write grain directory */
706 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
707 for (i = 0, tmp = header.rgd_offset + gd_size;
708 i < gt_count; i++, tmp += gt_size)
709 write(fd, &tmp, sizeof(tmp));
711 /* write backup grain directory */
712 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
713 for (i = 0, tmp = header.gd_offset + gd_size;
714 i < gt_count; i++, tmp += gt_size)
715 write(fd, &tmp, sizeof(tmp));
717 /* compose the descriptor */
718 real_filename = filename;
719 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
720 real_filename = temp_str + 1;
721 if ((temp_str = strrchr(real_filename, '/')) != NULL)
722 real_filename = temp_str + 1;
723 if ((temp_str = strrchr(real_filename, ':')) != NULL)
724 real_filename = temp_str + 1;
725 sprintf(desc, desc_template, time(NULL), (unsigned long)total_size,
726 real_filename, total_size / (63 * 16));
728 /* write the descriptor */
729 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
730 write(fd, desc, strlen(desc));
732 close(fd);
733 return 0;
736 static void vmdk_close(BlockDriverState *bs)
738 BDRVVmdkState *s = bs->opaque;
740 qemu_free(s->l1_table);
741 qemu_free(s->l2_cache);
742 bdrv_delete(s->hd);
743 // try to close parent image, if exist
744 vmdk_parent_close(s->hd);
747 static void vmdk_flush(BlockDriverState *bs)
749 BDRVVmdkState *s = bs->opaque;
750 bdrv_flush(s->hd);
753 BlockDriver bdrv_vmdk = {
754 "vmdk",
755 sizeof(BDRVVmdkState),
756 vmdk_probe,
757 vmdk_open,
758 vmdk_read,
759 vmdk_write,
760 vmdk_close,
761 vmdk_create,
762 vmdk_flush,
763 vmdk_is_allocated,