kvm: host suspend/resume support
[qemu-kvm/fedora.git] / block-vmdk.c
blobc2cb161fe8d198ca7e608f0d8f86ba07c76b80be
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.
25 #include <uuid/uuid.h>
26 #include <libgen.h>
27 #include "vl.h"
28 #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')
33 typedef struct {
34 uint32_t version;
35 uint32_t flags;
36 uint32_t disk_sectors;
37 uint32_t granularity;
38 uint32_t l1dir_offset;
39 uint32_t l1dir_size;
40 uint32_t file_sectors;
41 uint32_t cylinders;
42 uint32_t heads;
43 uint32_t sectors_per_track;
44 } VMDK3Header;
46 typedef struct {
47 uint32_t version;
48 uint32_t flags;
49 int64_t capacity;
50 int64_t granularity;
51 int64_t desc_offset;
52 int64_t desc_size;
53 int32_t num_gtes_per_gte;
54 int64_t rgd_offset;
55 int64_t gd_offset;
56 int64_t grain_offset;
57 char filler[1];
58 char check_bytes[4];
59 } __attribute__((packed)) VMDK4Header;
61 #define L2_CACHE_SIZE 16
63 typedef struct BDRVVmdkState {
64 int fd;
65 int64_t l1_table_offset;
66 int64_t l1_backup_table_offset;
67 uint32_t *l1_table;
68 uint32_t *l1_backup_table;
69 unsigned int l1_size;
70 uint32_t l1_entry_sectors;
72 unsigned int l2_size;
73 uint32_t *l2_cache;
74 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
75 uint32_t l2_cache_counts[L2_CACHE_SIZE];
77 unsigned int cluster_sectors;
78 } BDRVVmdkState;
80 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
82 uint32_t magic;
84 if (buf_size < 4)
85 return 0;
86 magic = be32_to_cpu(*(uint32_t *)buf);
87 if (magic == VMDK3_MAGIC ||
88 magic == VMDK4_MAGIC)
89 return 100;
90 else
91 return 0;
94 #define CHECK_CID 1
96 #define SECTOR_SIZE 512
97 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
98 #define HEADER_SIZE 512 // first sector of 512 bytes
100 static uint32_t vmdk_read_cid(int fd, int parent)
102 char desc[DESC_SIZE];
103 uint32_t cid;
104 char *p_name, *cid_str;
105 size_t cid_str_size;
107 if (fd) {
108 /* the descriptor offset = 0x200 */
109 if (lseek(fd, 0x200, SEEK_SET) == -1)
110 return 0;
111 if (read(fd, desc, DESC_SIZE) != DESC_SIZE)
112 return 0;
114 if (parent) {
115 cid_str = "parentCID";
116 cid_str_size = sizeof("parentCID");
117 } else {
118 cid_str = "CID";
119 cid_str_size = sizeof("CID");
122 if ((p_name = strstr(desc,cid_str)) != 0) {
123 p_name += cid_str_size;
124 sscanf(p_name,"%x",&cid);
127 return cid;
129 return 0;
132 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
134 BDRVVmdkState *s = bs->opaque;
135 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
136 char *p_name, *tmp_str;
138 if (s->fd) {
139 /* the descriptor offset = 0x200 */
140 if (lseek(s->fd, 0x200, SEEK_SET) == -1)
141 return -1;
142 if (read(s->fd, desc, DESC_SIZE) != DESC_SIZE)
143 return 0;
145 tmp_str = strstr(desc,"parentCID");
146 strcpy(tmp_desc, tmp_str);
147 if ((p_name = strstr(desc,"CID")) != 0) {
148 p_name += sizeof("CID");
149 sprintf(p_name,"%x\n",cid);
150 strcat(desc,tmp_desc);
153 if (lseek(s->fd, 0x200, SEEK_SET) == -1)
154 return -1;
155 if (write(s->fd, &desc, DESC_SIZE) != DESC_SIZE)
156 return -1;
158 bs->cid = cid;
159 return 0;
161 return -1;
164 static int vmdk_is_cid_valid(BlockDriverState *bs)
166 #ifdef CHECK_CID
167 BlockDriverState *p_bs = bs->bs_par_table;
168 uint32_t cur_pcid;
170 if (p_bs) {
171 BDRVVmdkState *s = p_bs->opaque;
173 cur_pcid = vmdk_read_cid(s->fd,0);
174 if (bs->parent_cid != cur_pcid)
175 // CID not valid
176 return 0;
178 #endif
179 // CID valid
180 return 1;
183 int vmdk_snapshot_create(BlockDriverState *bs)
185 int snp_fd, p_fd;
186 uint32_t p_cid;
187 char *p_name, *gd_buf, *rgd_buf;
188 VMDK4Header header;
189 uint32_t gde_entries, gd_size;
190 int64_t gd_offset, rgd_offset, capacity, gt_size;
191 char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
192 char parent_filename[1024];
193 char snapshot_filename[1024];
194 uuid_t name;
195 char *desc_template =
196 "# Disk DescriptorFile\n"
197 "version=1\n"
198 "CID=%x\n"
199 "parentCID=%x\n"
200 "createType=\"monolithicSparse\"\n"
201 "parentFileNameHint=\"%s\"\n"
202 "\n"
203 "# Extent description\n"
204 "RW %lu SPARSE \"%s\"\n"
205 "\n"
206 "# The Disk Data Base \n"
207 "#DDB\n"
208 "\n";
210 strcpy(parent_filename, bs->filename);
211 uuid_generate(name); // it should be unique filename
212 uuid_unparse(name, snapshot_filename);
213 strcat(snapshot_filename,".vmdk");
215 snp_fd = open(snapshot_filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
216 if (snp_fd < 0)
217 return -1;
218 p_fd = open(parent_filename, O_RDONLY | O_BINARY | O_LARGEFILE);
219 if (p_fd < 0) {
220 close(snp_fd);
221 return -1;
224 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
225 goto fail;
226 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
227 goto fail;
229 /* write the header */
230 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
231 goto fail;
232 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
233 goto fail;
235 memset(&header, 0, sizeof(header));
236 memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
238 ftruncate(snp_fd, header.grain_offset << 9);
239 /* the descriptor offset = 0x200 */
240 if (lseek(p_fd, 0x200, SEEK_SET) == -1)
241 goto fail;
242 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
243 goto fail;
245 if ((p_name = strstr(p_desc,"CID")) != 0) {
246 p_name += sizeof("CID");
247 sscanf(p_name,"%x",&p_cid);
249 sprintf(s_desc, desc_template, p_cid, p_cid, parent_filename
250 , (uint32_t)header.capacity, snapshot_filename);
252 bs->parent_cid = p_cid;
254 /* write the descriptor */
255 if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
256 goto fail;
257 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
258 goto fail;
260 gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
261 rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
262 capacity = header.capacity * SECTOR_SIZE; // Extent size
264 * Each GDE span 32M disk, means:
265 * 512 GTE per GT, each GTE points to grain
267 gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
268 if (!gt_size)
269 goto fail;
270 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
271 gd_size = gde_entries * sizeof(uint32_t);
273 /* write RGD */
274 rgd_buf = qemu_malloc(gd_size);
275 if (!rgd_buf)
276 goto fail;
277 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
278 goto fail_rgd;
279 if (read(p_fd, rgd_buf, gd_size) != gd_size)
280 goto fail_rgd;
281 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
282 goto fail_rgd;
283 if (write(snp_fd, rgd_buf, gd_size) == -1)
284 goto fail_rgd;
285 qemu_free(rgd_buf);
287 /* write GD */
288 gd_buf = qemu_malloc(gd_size);
289 if (!gd_buf)
290 goto fail_rgd;
291 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
292 goto fail_gd;
293 if (read(p_fd, gd_buf, gd_size) != gd_size)
294 goto fail_gd;
295 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
296 goto fail_gd;
297 if (write(snp_fd, gd_buf, gd_size) == -1)
298 goto fail_gd;
299 qemu_free(gd_buf);
301 close(p_fd);
302 close(snp_fd);
303 return 0;
305 fail_gd:
306 qemu_free(gd_buf);
307 fail_rgd:
308 qemu_free(rgd_buf);
309 fail:
310 close(p_fd);
311 close(snp_fd);
312 return -1;
315 static void vmdk_parent_close(BlockDriverState *bs)
317 if (bs->bs_par_table)
318 bdrv_close(bs->bs_par_table);
322 static int vmdk_parent_open(BlockDriverState *bs, int fd, char * dir_name)
324 char *p_name;
325 char desc[DESC_SIZE];
326 static int idx=0;
327 char parent_img_name[1024];
329 /* the descriptor offset = 0x200 */
330 if (lseek(fd, 0x200, SEEK_SET) == -1)
331 return -1;
332 if (read(fd, desc, DESC_SIZE) != DESC_SIZE)
333 return -1;
335 if ((p_name = strstr(desc,"parentFileNameHint")) != 0) {
336 char *end_name, *tmp_name;
337 char name[256], buf[128];
338 int name_size;
339 struct stat file_buf;
341 p_name += sizeof("parentFileNameHint") + 1;
342 if ((end_name = strchr(p_name,'\"')) == 0)
343 return -1;
345 bs->parent_img_name = qemu_mallocz(end_name - p_name + 2);
346 strncpy(bs->parent_img_name, p_name, end_name - p_name);
347 if (stat(bs->parent_img_name, &file_buf) != 0) {
348 strcpy(parent_img_name, dir_name);
349 strcat(parent_img_name, basename(bs->parent_img_name));
350 } else {
351 strcpy(parent_img_name, bs->parent_img_name);
354 tmp_name = strstr(bs->device_name,"_QEMU");
355 name_size = tmp_name ? (tmp_name - bs->device_name) : sizeof(bs->device_name);
356 strncpy(name,bs->device_name,name_size);
357 sprintf(buf, "_QEMU_%d", ++idx);
359 bs->bs_par_table = bdrv_new(strcat(name, buf));
360 if (!bs->bs_par_table) {
361 failure:
362 bdrv_close(bs);
363 return -1;
366 if (bdrv_open(bs->bs_par_table, parent_img_name, 0) < 0)
367 goto failure;
370 return 0;
373 static int vmdk_open(BlockDriverState *bs, const char *filename)
375 BDRVVmdkState *s = bs->opaque;
376 int fd, i;
377 uint32_t magic;
378 int l1_size;
380 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
381 if (fd < 0) {
382 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
383 if (fd < 0)
384 return -1;
385 bs->read_only = 1;
387 if (read(fd, &magic, sizeof(magic)) != sizeof(magic))
388 goto fail;
389 magic = be32_to_cpu(magic);
390 if (magic == VMDK3_MAGIC) {
391 VMDK3Header header;
392 if (read(fd, &header, sizeof(header)) !=
393 sizeof(header))
394 goto fail;
395 s->cluster_sectors = le32_to_cpu(header.granularity);
396 s->l2_size = 1 << 9;
397 s->l1_size = 1 << 6;
398 bs->total_sectors = le32_to_cpu(header.disk_sectors);
399 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
400 s->l1_backup_table_offset = 0;
401 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
402 } else if (magic == VMDK4_MAGIC) {
403 char dir_name[1024];
404 VMDK4Header header;
406 if (read(fd, &header, sizeof(header)) != sizeof(header))
407 goto fail;
408 bs->total_sectors = le64_to_cpu(header.capacity);
409 s->cluster_sectors = le64_to_cpu(header.granularity);
410 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
411 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
412 if (s->l1_entry_sectors <= 0)
413 goto fail;
414 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
415 / s->l1_entry_sectors;
416 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
417 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
419 // try to open parent images, if exist
420 strcpy(dir_name, dirname(filename));
421 strcat(dir_name,"/");
422 if (vmdk_parent_open(bs, fd, dir_name) != 0)
423 goto fail;
424 // write the CID once after the image creation
425 bs->cid = vmdk_read_cid(fd,0);
426 bs->parent_cid = vmdk_read_cid(fd,1);
427 } else {
428 goto fail;
430 /* read the L1 table */
431 l1_size = s->l1_size * sizeof(uint32_t);
432 s->l1_table = qemu_malloc(l1_size);
433 if (!s->l1_table)
434 goto fail;
435 if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1)
436 goto fail;
437 if (read(fd, s->l1_table, l1_size) != l1_size)
438 goto fail;
439 for(i = 0; i < s->l1_size; i++) {
440 le32_to_cpus(&s->l1_table[i]);
443 if (s->l1_backup_table_offset) {
444 s->l1_backup_table = qemu_malloc(l1_size);
445 if (!s->l1_backup_table)
446 goto fail;
447 if (lseek(fd, s->l1_backup_table_offset, SEEK_SET) == -1)
448 goto fail;
449 if (read(fd, s->l1_backup_table, l1_size) != l1_size)
450 goto fail;
451 for(i = 0; i < s->l1_size; i++) {
452 le32_to_cpus(&s->l1_backup_table[i]);
456 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
457 if (!s->l2_cache)
458 goto fail;
459 s->fd = fd;
460 return 0;
461 fail:
462 qemu_free(s->l1_backup_table);
463 qemu_free(s->l1_table);
464 qemu_free(s->l2_cache);
465 close(fd);
466 return -1;
469 static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate);
471 static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
472 uint64_t offset, int allocate)
474 int ret;
475 uint64_t parent_cluster_offset;
476 BDRVVmdkState *s = bs->opaque;
477 uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
479 // we will be here if it's first write on non-exist grain(cluster).
480 // try to read from parent image, if exist
481 if (bs->bs_par_table) {
482 BDRVVmdkState *ps = bs->bs_par_table->opaque;
484 if (!vmdk_is_cid_valid(bs))
485 return -1;
487 parent_cluster_offset = get_cluster_offset(bs->bs_par_table, offset, allocate);
489 lseek(ps->fd, parent_cluster_offset, SEEK_SET);
490 ret = read(ps->fd, whole_grain, ps->cluster_sectors*512);
491 if (ret != ps->cluster_sectors*512)
492 return -1;
494 lseek(s->fd, cluster_offset << 9, SEEK_SET);
495 ret = write(s->fd, whole_grain, sizeof(whole_grain));
496 if (ret != sizeof(whole_grain))
497 return -1;
499 return 0;
502 static uint64_t get_cluster_offset(BlockDriverState *bs,
503 uint64_t offset, int allocate)
505 BDRVVmdkState *s = bs->opaque;
506 unsigned int l1_index, l2_offset, l2_index;
507 int min_index, i, j;
508 uint32_t min_count, *l2_table, tmp;
509 uint64_t cluster_offset;
511 l1_index = (offset >> 9) / s->l1_entry_sectors;
512 if (l1_index >= s->l1_size)
513 return 0;
514 l2_offset = s->l1_table[l1_index];
515 if (!l2_offset)
516 return 0;
517 for(i = 0; i < L2_CACHE_SIZE; i++) {
518 if (l2_offset == s->l2_cache_offsets[i]) {
519 /* increment the hit count */
520 if (++s->l2_cache_counts[i] == 0xffffffff) {
521 for(j = 0; j < L2_CACHE_SIZE; j++) {
522 s->l2_cache_counts[j] >>= 1;
525 l2_table = s->l2_cache + (i * s->l2_size);
526 goto found;
529 /* not found: load a new entry in the least used one */
530 min_index = 0;
531 min_count = 0xffffffff;
532 for(i = 0; i < L2_CACHE_SIZE; i++) {
533 if (s->l2_cache_counts[i] < min_count) {
534 min_count = s->l2_cache_counts[i];
535 min_index = i;
538 l2_table = s->l2_cache + (min_index * s->l2_size);
539 lseek(s->fd, (int64_t)l2_offset * 512, SEEK_SET);
540 if (read(s->fd, l2_table, s->l2_size * sizeof(uint32_t)) !=
541 s->l2_size * sizeof(uint32_t))
542 return 0;
543 s->l2_cache_offsets[min_index] = l2_offset;
544 s->l2_cache_counts[min_index] = 1;
545 found:
546 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
547 cluster_offset = le32_to_cpu(l2_table[l2_index]);
548 if (!cluster_offset) {
549 if (!allocate)
550 return 0;
551 cluster_offset = lseek(s->fd, 0, SEEK_END);
552 ftruncate(s->fd, cluster_offset + (s->cluster_sectors << 9));
553 cluster_offset >>= 9;
554 /* update L2 table */
555 tmp = cpu_to_le32(cluster_offset);
556 l2_table[l2_index] = tmp;
557 lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET);
558 if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
559 return 0;
560 /* update backup L2 table */
561 if (s->l1_backup_table_offset != 0) {
562 l2_offset = s->l1_backup_table[l1_index];
563 lseek(s->fd, ((int64_t)l2_offset * 512) + (l2_index * sizeof(tmp)), SEEK_SET);
564 if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
565 return 0;
568 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
569 return 0;
571 cluster_offset <<= 9;
572 return cluster_offset;
575 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
576 int nb_sectors, int *pnum)
578 BDRVVmdkState *s = bs->opaque;
579 int index_in_cluster, n;
580 uint64_t cluster_offset;
582 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
583 index_in_cluster = sector_num % s->cluster_sectors;
584 n = s->cluster_sectors - index_in_cluster;
585 if (n > nb_sectors)
586 n = nb_sectors;
587 *pnum = n;
588 return (cluster_offset != 0);
591 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
592 uint8_t *buf, int nb_sectors)
594 BDRVVmdkState *s = bs->opaque;
595 int ret, index_in_cluster, n;
596 uint64_t cluster_offset;
598 while (nb_sectors > 0) {
599 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0);
600 index_in_cluster = sector_num % s->cluster_sectors;
601 n = s->cluster_sectors - index_in_cluster;
602 if (n > nb_sectors)
603 n = nb_sectors;
604 if (!cluster_offset) {
605 // try to read from parent image, if exist
606 if (bs->bs_par_table) {
607 if (!vmdk_is_cid_valid(bs))
608 return -1;
609 if (vmdk_read(bs->bs_par_table, sector_num, buf, nb_sectors) == -1)
610 return -1;
611 } else {
612 memset(buf, 0, 512 * n);
614 } else {
615 lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
616 ret = read(s->fd, buf, n * 512);
617 if (ret != n * 512)
618 return -1;
620 nb_sectors -= n;
621 sector_num += n;
622 buf += n * 512;
624 return 0;
627 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
628 const uint8_t *buf, int nb_sectors)
630 BDRVVmdkState *s = bs->opaque;
631 int ret, index_in_cluster, n;
632 uint64_t cluster_offset;
633 static int cid_update = 0;
635 while (nb_sectors > 0) {
636 index_in_cluster = sector_num & (s->cluster_sectors - 1);
637 n = s->cluster_sectors - index_in_cluster;
638 if (n > nb_sectors)
639 n = nb_sectors;
640 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1);
641 if (!cluster_offset)
642 return -1;
643 lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
644 ret = write(s->fd, buf, n * 512);
645 if (ret != n * 512)
646 return -1;
647 nb_sectors -= n;
648 sector_num += n;
649 buf += n * 512;
651 // update CID on the first write every time the virtual disk is opened
652 if (!cid_update) {
653 vmdk_write_cid(bs, time(NULL));
654 cid_update++;
657 return 0;
660 static int vmdk_create(const char *filename, int64_t total_size,
661 const char *backing_file, int flags)
663 int fd, i;
664 VMDK4Header header;
665 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
666 char *desc_template =
667 "# Disk DescriptorFile\n"
668 "version=1\n"
669 "CID=%x\n"
670 "parentCID=ffffffff\n"
671 "createType=\"monolithicSparse\"\n"
672 "\n"
673 "# Extent description\n"
674 "RW %lu SPARSE \"%s\"\n"
675 "\n"
676 "# The Disk Data Base \n"
677 "#DDB\n"
678 "\n"
679 "ddb.virtualHWVersion = \"4\"\n"
680 "ddb.geometry.cylinders = \"%lu\"\n"
681 "ddb.geometry.heads = \"16\"\n"
682 "ddb.geometry.sectors = \"63\"\n"
683 "ddb.adapterType = \"ide\"\n";
684 char desc[1024];
685 const char *real_filename, *temp_str;
687 /* XXX: add support for backing file */
689 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
690 0644);
691 if (fd < 0)
692 return -1;
693 magic = cpu_to_be32(VMDK4_MAGIC);
694 memset(&header, 0, sizeof(header));
695 header.version = cpu_to_le32(1);
696 header.flags = cpu_to_le32(3); /* ?? */
697 header.capacity = cpu_to_le64(total_size);
698 header.granularity = cpu_to_le64(128);
699 header.num_gtes_per_gte = cpu_to_le32(512);
701 grains = (total_size + header.granularity - 1) / header.granularity;
702 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
703 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
704 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
706 header.desc_offset = 1;
707 header.desc_size = 20;
708 header.rgd_offset = header.desc_offset + header.desc_size;
709 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
710 header.grain_offset =
711 ((header.gd_offset + gd_size + (gt_size * gt_count) +
712 header.granularity - 1) / header.granularity) *
713 header.granularity;
715 header.desc_offset = cpu_to_le64(header.desc_offset);
716 header.desc_size = cpu_to_le64(header.desc_size);
717 header.rgd_offset = cpu_to_le64(header.rgd_offset);
718 header.gd_offset = cpu_to_le64(header.gd_offset);
719 header.grain_offset = cpu_to_le64(header.grain_offset);
721 header.check_bytes[0] = 0xa;
722 header.check_bytes[1] = 0x20;
723 header.check_bytes[2] = 0xd;
724 header.check_bytes[3] = 0xa;
726 /* write all the data */
727 write(fd, &magic, sizeof(magic));
728 write(fd, &header, sizeof(header));
730 ftruncate(fd, header.grain_offset << 9);
732 /* write grain directory */
733 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
734 for (i = 0, tmp = header.rgd_offset + gd_size;
735 i < gt_count; i++, tmp += gt_size)
736 write(fd, &tmp, sizeof(tmp));
738 /* write backup grain directory */
739 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
740 for (i = 0, tmp = header.gd_offset + gd_size;
741 i < gt_count; i++, tmp += gt_size)
742 write(fd, &tmp, sizeof(tmp));
744 /* compose the descriptor */
745 real_filename = filename;
746 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
747 real_filename = temp_str + 1;
748 if ((temp_str = strrchr(real_filename, '/')) != NULL)
749 real_filename = temp_str + 1;
750 if ((temp_str = strrchr(real_filename, ':')) != NULL)
751 real_filename = temp_str + 1;
752 sprintf(desc, desc_template, time(NULL), (unsigned long)total_size,
753 real_filename, total_size / (63 * 16));
755 /* write the descriptor */
756 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
757 write(fd, desc, strlen(desc));
759 close(fd);
760 return 0;
763 static void vmdk_close(BlockDriverState *bs)
765 BDRVVmdkState *s = bs->opaque;
767 qemu_free(s->l1_table);
768 qemu_free(s->l2_cache);
769 close(s->fd);
770 // try to close parent image, if exist
771 vmdk_parent_close(bs);
774 static void vmdk_flush(BlockDriverState *bs)
776 BDRVVmdkState *s = bs->opaque;
777 fsync(s->fd);
780 BlockDriver bdrv_vmdk = {
781 "vmdk",
782 sizeof(BDRVVmdkState),
783 vmdk_probe,
784 vmdk_open,
785 vmdk_read,
786 vmdk_write,
787 vmdk_close,
788 vmdk_create,
789 vmdk_flush,
790 vmdk_is_allocated,