Fix configurations with more than 4GB of memory
[qemu-kvm/fedora.git] / block-vmdk.c
blob1f3709ced55b8fedb77bb02e043f96f9b7fd3b81
1 /*
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
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 int is_parent;
79 DiskIOStatistics io;
80 } BDRVVmdkState;
82 typedef struct VmdkMetaData {
83 uint32_t offset;
84 unsigned int l1_index;
85 unsigned int l2_index;
86 unsigned int l2_offset;
87 int valid;
88 } VmdkMetaData;
90 typedef struct ActiveBDRVState{
91 BlockDriverState *hd; // active image handler
92 uint64_t cluster_offset; // current write offset
93 }ActiveBDRVState;
95 static ActiveBDRVState activeBDRV;
97 DiskIOStatistics vmdk_io_statistics(BlockDriverState *bs)
99 BDRVVmdkState *s = bs->opaque;
100 // return disk I/O counters
101 return s->io;
104 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
106 uint32_t magic;
108 if (buf_size < 4)
109 return 0;
110 magic = be32_to_cpu(*(uint32_t *)buf);
111 if (magic == VMDK3_MAGIC ||
112 magic == VMDK4_MAGIC)
113 return 100;
114 else
115 return 0;
118 #define CHECK_CID 1
120 #define SECTOR_SIZE 512
121 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
122 #define HEADER_SIZE 512 // first sector of 512 bytes
124 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
126 BDRVVmdkState *s = bs->opaque;
127 char desc[DESC_SIZE];
128 uint32_t cid;
129 char *p_name, *cid_str;
130 size_t cid_str_size;
132 /* the descriptor offset = 0x200 */
133 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
134 return 0;
136 if (parent) {
137 cid_str = "parentCID";
138 cid_str_size = sizeof("parentCID");
139 } else {
140 cid_str = "CID";
141 cid_str_size = sizeof("CID");
144 if ((p_name = strstr(desc,cid_str)) != 0) {
145 p_name += cid_str_size;
146 sscanf(p_name,"%x",&cid);
149 return cid;
152 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
154 BDRVVmdkState *s = bs->opaque;
155 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
156 char *p_name, *tmp_str;
158 /* the descriptor offset = 0x200 */
159 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
160 return -1;
162 tmp_str = strstr(desc,"parentCID");
163 strcpy(tmp_desc, tmp_str);
164 if ((p_name = strstr(desc,"CID")) != 0) {
165 p_name += sizeof("CID");
166 sprintf(p_name,"%x\n",cid);
167 strcat(desc,tmp_desc);
170 if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
171 return -1;
172 return 0;
175 static int vmdk_is_cid_valid(BlockDriverState *bs)
177 #ifdef CHECK_CID
178 BDRVVmdkState *s = bs->opaque;
179 BlockDriverState *p_bs = s->hd->backing_hd;
180 uint32_t cur_pcid;
182 if (p_bs) {
183 cur_pcid = vmdk_read_cid(p_bs,0);
184 if (s->parent_cid != cur_pcid)
185 // CID not valid
186 return 0;
188 #endif
189 // CID valid
190 return 1;
193 static int vmdk_snapshot_create(const char *filename, const char *backing_file)
195 int snp_fd, p_fd;
196 uint32_t p_cid;
197 char *p_name, *gd_buf, *rgd_buf;
198 const char *real_filename, *temp_str;
199 VMDK4Header header;
200 uint32_t gde_entries, gd_size;
201 int64_t gd_offset, rgd_offset, capacity, gt_size;
202 char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
203 char *desc_template =
204 "# Disk DescriptorFile\n"
205 "version=1\n"
206 "CID=%x\n"
207 "parentCID=%x\n"
208 "createType=\"monolithicSparse\"\n"
209 "parentFileNameHint=\"%s\"\n"
210 "\n"
211 "# Extent description\n"
212 "RW %lu SPARSE \"%s\"\n"
213 "\n"
214 "# The Disk Data Base \n"
215 "#DDB\n"
216 "\n";
218 snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
219 if (snp_fd < 0)
220 return -1;
221 p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
222 if (p_fd < 0) {
223 close(snp_fd);
224 return -1;
227 /* read the header */
228 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
229 goto fail;
230 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
231 goto fail;
233 /* write the header */
234 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
235 goto fail;
236 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
237 goto fail;
239 memset(&header, 0, sizeof(header));
240 memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
242 ftruncate(snp_fd, header.grain_offset << 9);
243 /* the descriptor offset = 0x200 */
244 if (lseek(p_fd, 0x200, SEEK_SET) == -1)
245 goto fail;
246 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
247 goto fail;
249 if ((p_name = strstr(p_desc,"CID")) != 0) {
250 p_name += sizeof("CID");
251 sscanf(p_name,"%x",&p_cid);
254 real_filename = filename;
255 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
256 real_filename = temp_str + 1;
257 if ((temp_str = strrchr(real_filename, '/')) != NULL)
258 real_filename = temp_str + 1;
259 if ((temp_str = strrchr(real_filename, ':')) != NULL)
260 real_filename = temp_str + 1;
262 sprintf(s_desc, desc_template, p_cid, p_cid, backing_file
263 , (uint32_t)header.capacity, real_filename);
265 /* write the descriptor */
266 if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
267 goto fail;
268 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
269 goto fail;
271 gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
272 rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
273 capacity = header.capacity * SECTOR_SIZE; // Extent size
275 * Each GDE span 32M disk, means:
276 * 512 GTE per GT, each GTE points to grain
278 gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
279 if (!gt_size)
280 goto fail;
281 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
282 gd_size = gde_entries * sizeof(uint32_t);
284 /* write RGD */
285 rgd_buf = qemu_malloc(gd_size);
286 if (!rgd_buf)
287 goto fail;
288 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
289 goto fail_rgd;
290 if (read(p_fd, rgd_buf, gd_size) != gd_size)
291 goto fail_rgd;
292 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
293 goto fail_rgd;
294 if (write(snp_fd, rgd_buf, gd_size) == -1)
295 goto fail_rgd;
296 qemu_free(rgd_buf);
298 /* write GD */
299 gd_buf = qemu_malloc(gd_size);
300 if (!gd_buf)
301 goto fail_rgd;
302 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
303 goto fail_gd;
304 if (read(p_fd, gd_buf, gd_size) != gd_size)
305 goto fail_gd;
306 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
307 goto fail_gd;
308 if (write(snp_fd, gd_buf, gd_size) == -1)
309 goto fail_gd;
310 qemu_free(gd_buf);
312 close(p_fd);
313 close(snp_fd);
314 return 0;
316 fail_gd:
317 qemu_free(gd_buf);
318 fail_rgd:
319 qemu_free(rgd_buf);
320 fail:
321 close(p_fd);
322 close(snp_fd);
323 return -1;
326 static void vmdk_parent_close(BlockDriverState *bs)
328 if (bs->backing_hd)
329 bdrv_close(bs->backing_hd);
332 int parent_open = 0;
333 static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
335 BDRVVmdkState *s = bs->opaque;
336 char *p_name;
337 char desc[DESC_SIZE];
338 char parent_img_name[1024];
340 /* the descriptor offset = 0x200 */
341 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
342 return -1;
344 if ((p_name = strstr(desc,"parentFileNameHint")) != 0) {
345 char *end_name;
346 struct stat file_buf;
348 p_name += sizeof("parentFileNameHint") + 1;
349 if ((end_name = strchr(p_name,'\"')) == 0)
350 return -1;
352 strncpy(s->hd->backing_file, p_name, end_name - p_name);
353 if (stat(s->hd->backing_file, &file_buf) != 0) {
354 path_combine(parent_img_name, sizeof(parent_img_name),
355 filename, s->hd->backing_file);
356 } else {
357 strcpy(parent_img_name, s->hd->backing_file);
360 s->hd->backing_hd = bdrv_new("");
361 if (!s->hd->backing_hd) {
362 failure:
363 bdrv_close(s->hd);
364 return -1;
366 parent_open = 1;
367 if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
368 goto failure;
369 parent_open = 0;
372 return 0;
375 static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
377 BDRVVmdkState *s = bs->opaque;
378 uint32_t magic;
379 int l1_size, i, ret;
381 if (parent_open)
382 // Parent must be opened as RO.
383 flags = BDRV_O_RDONLY;
384 fprintf(stderr, "(VMDK) image open: flags=0x%x filename=%s\n", flags, bs->filename);
386 ret = bdrv_file_open(&s->hd, filename, flags);
387 if (ret < 0)
388 return ret;
389 if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
390 goto fail;
392 magic = be32_to_cpu(magic);
393 if (magic == VMDK3_MAGIC) {
394 VMDK3Header header;
396 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
397 goto fail;
398 s->cluster_sectors = le32_to_cpu(header.granularity);
399 s->l2_size = 1 << 9;
400 s->l1_size = 1 << 6;
401 bs->total_sectors = le32_to_cpu(header.disk_sectors);
402 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
403 s->l1_backup_table_offset = 0;
404 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
405 } else if (magic == VMDK4_MAGIC) {
406 VMDK4Header header;
408 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
409 goto fail;
410 bs->total_sectors = le64_to_cpu(header.capacity);
411 s->cluster_sectors = le64_to_cpu(header.granularity);
412 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
413 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
414 if (s->l1_entry_sectors <= 0)
415 goto fail;
416 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
417 / s->l1_entry_sectors;
418 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
419 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
421 if (parent_open)
422 s->is_parent = 1;
423 else
424 s->is_parent = 0;
426 // try to open parent images, if exist
427 if (vmdk_parent_open(bs, filename) != 0)
428 goto fail;
429 // write the CID once after the image creation
430 s->parent_cid = vmdk_read_cid(bs,1);
431 } else {
432 goto fail;
435 /* read the L1 table */
436 l1_size = s->l1_size * sizeof(uint32_t);
437 s->l1_table = qemu_malloc(l1_size);
438 if (!s->l1_table)
439 goto fail;
440 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
441 goto fail;
442 for(i = 0; i < s->l1_size; i++) {
443 le32_to_cpus(&s->l1_table[i]);
446 if (s->l1_backup_table_offset) {
447 s->l1_backup_table = qemu_malloc(l1_size);
448 if (!s->l1_backup_table)
449 goto fail;
450 if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
451 goto fail;
452 for(i = 0; i < s->l1_size; i++) {
453 le32_to_cpus(&s->l1_backup_table[i]);
457 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
458 if (!s->l2_cache)
459 goto fail;
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 bdrv_delete(s->hd);
466 return -1;
469 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
470 uint64_t offset, int allocate);
472 static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
473 uint64_t offset, int allocate)
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 (s->hd->backing_hd) {
482 BDRVVmdkState *ps = s->hd->backing_hd->opaque;
484 if (!vmdk_is_cid_valid(bs))
485 return -1;
487 parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
489 if (parent_cluster_offset) {
490 BDRVVmdkState *act_s = activeBDRV.hd->opaque;
492 if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512)
493 return -1;
495 //Write grain only into the active image
496 if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain))
497 return -1;
500 return 0;
503 static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
505 BDRVVmdkState *s = bs->opaque;
507 /* update L2 table */
508 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
509 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
510 return -1;
511 /* update backup L2 table */
512 if (s->l1_backup_table_offset != 0) {
513 m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
514 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
515 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
516 return -1;
519 return 0;
522 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
523 uint64_t offset, int allocate)
525 BDRVVmdkState *s = bs->opaque;
526 unsigned int l1_index, l2_offset, l2_index;
527 int min_index, i, j;
528 uint32_t min_count, *l2_table, tmp = 0;
529 uint64_t cluster_offset;
531 if (m_data)
532 m_data->valid = 0;
534 l1_index = (offset >> 9) / s->l1_entry_sectors;
535 if (l1_index >= s->l1_size)
536 return 0;
537 l2_offset = s->l1_table[l1_index];
538 if (!l2_offset)
539 return 0;
540 for(i = 0; i < L2_CACHE_SIZE; i++) {
541 if (l2_offset == s->l2_cache_offsets[i]) {
542 /* increment the hit count */
543 if (++s->l2_cache_counts[i] == 0xffffffff) {
544 for(j = 0; j < L2_CACHE_SIZE; j++) {
545 s->l2_cache_counts[j] >>= 1;
548 l2_table = s->l2_cache + (i * s->l2_size);
549 goto found;
552 /* not found: load a new entry in the least used one */
553 min_index = 0;
554 min_count = 0xffffffff;
555 for(i = 0; i < L2_CACHE_SIZE; i++) {
556 if (s->l2_cache_counts[i] < min_count) {
557 min_count = s->l2_cache_counts[i];
558 min_index = i;
561 l2_table = s->l2_cache + (min_index * s->l2_size);
562 if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
563 s->l2_size * sizeof(uint32_t))
564 return 0;
566 s->l2_cache_offsets[min_index] = l2_offset;
567 s->l2_cache_counts[min_index] = 1;
568 found:
569 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
570 cluster_offset = le32_to_cpu(l2_table[l2_index]);
572 if (!cluster_offset) {
573 if (!allocate)
574 return 0;
575 // Avoid the L2 tables update for the images that have snapshots.
576 if (!s->is_parent) {
577 cluster_offset = bdrv_getlength(s->hd);
578 bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
580 cluster_offset >>= 9;
581 tmp = cpu_to_le32(cluster_offset);
582 l2_table[l2_index] = tmp;
583 // Save the active image state
584 activeBDRV.cluster_offset = cluster_offset;
585 activeBDRV.hd = bs;
587 /* First of all we write grain itself, to avoid race condition
588 * that may to corrupt the image.
589 * This problem may occur because of insufficient space on host disk
590 * or inappropriate VM shutdown.
592 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
593 return 0;
595 if (m_data) {
596 m_data->offset = tmp;
597 m_data->l1_index = l1_index;
598 m_data->l2_index = l2_index;
599 m_data->l2_offset = l2_offset;
600 m_data->valid = 1;
603 cluster_offset <<= 9;
604 return cluster_offset;
607 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
608 int nb_sectors, int *pnum)
610 BDRVVmdkState *s = bs->opaque;
611 int index_in_cluster, n;
612 uint64_t cluster_offset;
614 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
615 index_in_cluster = sector_num % s->cluster_sectors;
616 n = s->cluster_sectors - index_in_cluster;
617 if (n > nb_sectors)
618 n = nb_sectors;
619 *pnum = n;
620 return (cluster_offset != 0);
623 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
624 uint8_t *buf, int nb_sectors)
626 BDRVVmdkState *s = bs->opaque;
627 int index_in_cluster, n, ret;
628 uint64_t cluster_offset;
630 while (nb_sectors > 0) {
631 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
632 index_in_cluster = sector_num % s->cluster_sectors;
633 n = s->cluster_sectors - index_in_cluster;
634 if (n > nb_sectors)
635 n = nb_sectors;
636 if (!cluster_offset) {
637 // try to read from parent image, if exist
638 if (s->hd->backing_hd) {
639 if (!vmdk_is_cid_valid(bs))
640 return -1;
641 ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
642 if (ret < 0)
643 return -1;
644 } else {
645 memset(buf, 0, 512 * n);
647 } else {
648 if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
649 return -1;
651 nb_sectors -= n;
652 sector_num += n;
653 buf += n * 512;
654 s->io.read_byte_counter += (uint64_t)(n*512);
656 return 0;
659 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
660 const uint8_t *buf, int nb_sectors)
662 BDRVVmdkState *s = bs->opaque;
663 VmdkMetaData m_data;
664 int index_in_cluster, n;
665 uint64_t cluster_offset;
666 static int cid_update = 0;
668 if (sector_num > bs->total_sectors) {
669 fprintf(stderr,
670 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
671 " total_sectors=0x%" PRIx64 "\n",
672 sector_num, bs->total_sectors);
673 return -1;
676 while (nb_sectors > 0) {
677 index_in_cluster = sector_num & (s->cluster_sectors - 1);
678 n = s->cluster_sectors - index_in_cluster;
679 if (n > nb_sectors)
680 n = nb_sectors;
681 cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1);
682 if (!cluster_offset)
683 return -1;
685 if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
686 return -1;
687 if (m_data.valid) {
688 /* update L2 tables */
689 if (vmdk_L2update(bs, &m_data) == -1)
690 return -1;
692 nb_sectors -= n;
693 sector_num += n;
694 buf += n * 512;
695 s->io.write_byte_counter += (uint64_t)(n*512);
697 // update CID on the first write every time the virtual disk is opened
698 if (!cid_update) {
699 vmdk_write_cid(bs, time(NULL));
700 cid_update++;
703 return 0;
706 static int vmdk_create(const char *filename, int64_t total_size,
707 const char *backing_file, int flags)
709 int fd, i;
710 VMDK4Header header;
711 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
712 char *desc_template =
713 "# Disk DescriptorFile\n"
714 "version=1\n"
715 "CID=%x\n"
716 "parentCID=ffffffff\n"
717 "createType=\"monolithicSparse\"\n"
718 "\n"
719 "# Extent description\n"
720 "RW %lu SPARSE \"%s\"\n"
721 "\n"
722 "# The Disk Data Base \n"
723 "#DDB\n"
724 "\n"
725 "ddb.virtualHWVersion = \"%d\"\n"
726 "ddb.geometry.cylinders = \"%lu\"\n"
727 "ddb.geometry.heads = \"16\"\n"
728 "ddb.geometry.sectors = \"63\"\n"
729 "ddb.adapterType = \"ide\"\n";
730 char desc[1024];
731 const char *real_filename, *temp_str;
733 /* XXX: add support for backing file */
734 if (backing_file) {
735 return vmdk_snapshot_create(filename, backing_file);
738 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
739 0644);
740 if (fd < 0)
741 return -1;
742 magic = cpu_to_be32(VMDK4_MAGIC);
743 memset(&header, 0, sizeof(header));
744 header.version = cpu_to_le32(1);
745 header.flags = cpu_to_le32(3); /* ?? */
746 header.capacity = cpu_to_le64(total_size);
747 header.granularity = cpu_to_le64(128);
748 header.num_gtes_per_gte = cpu_to_le32(512);
750 grains = (total_size + header.granularity - 1) / header.granularity;
751 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
752 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
753 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
755 header.desc_offset = 1;
756 header.desc_size = 20;
757 header.rgd_offset = header.desc_offset + header.desc_size;
758 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
759 header.grain_offset =
760 ((header.gd_offset + gd_size + (gt_size * gt_count) +
761 header.granularity - 1) / header.granularity) *
762 header.granularity;
764 header.desc_offset = cpu_to_le64(header.desc_offset);
765 header.desc_size = cpu_to_le64(header.desc_size);
766 header.rgd_offset = cpu_to_le64(header.rgd_offset);
767 header.gd_offset = cpu_to_le64(header.gd_offset);
768 header.grain_offset = cpu_to_le64(header.grain_offset);
770 header.check_bytes[0] = 0xa;
771 header.check_bytes[1] = 0x20;
772 header.check_bytes[2] = 0xd;
773 header.check_bytes[3] = 0xa;
775 /* write all the data */
776 write(fd, &magic, sizeof(magic));
777 write(fd, &header, sizeof(header));
779 ftruncate(fd, header.grain_offset << 9);
781 /* write grain directory */
782 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
783 for (i = 0, tmp = header.rgd_offset + gd_size;
784 i < gt_count; i++, tmp += gt_size)
785 write(fd, &tmp, sizeof(tmp));
787 /* write backup grain directory */
788 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
789 for (i = 0, tmp = header.gd_offset + gd_size;
790 i < gt_count; i++, tmp += gt_size)
791 write(fd, &tmp, sizeof(tmp));
793 /* compose the descriptor */
794 real_filename = filename;
795 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
796 real_filename = temp_str + 1;
797 if ((temp_str = strrchr(real_filename, '/')) != NULL)
798 real_filename = temp_str + 1;
799 if ((temp_str = strrchr(real_filename, ':')) != NULL)
800 real_filename = temp_str + 1;
801 sprintf(desc, desc_template, time(NULL), (unsigned long)total_size,
802 real_filename, (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4), total_size / (63 * 16));
804 /* write the descriptor */
805 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
806 write(fd, desc, strlen(desc));
808 close(fd);
809 return 0;
812 static void vmdk_close(BlockDriverState *bs)
814 BDRVVmdkState *s = bs->opaque;
816 qemu_free(s->l1_table);
817 qemu_free(s->l2_cache);
818 bdrv_delete(s->hd);
819 // try to close parent image, if exist
820 vmdk_parent_close(s->hd);
823 static void vmdk_flush(BlockDriverState *bs)
825 BDRVVmdkState *s = bs->opaque;
826 bdrv_flush(s->hd);
829 BlockDriver bdrv_vmdk = {
830 "vmdk",
831 sizeof(BDRVVmdkState),
832 vmdk_probe,
833 vmdk_open,
834 vmdk_read,
835 vmdk_write,
836 vmdk_close,
837 vmdk_create,
838 vmdk_flush,
839 vmdk_is_allocated,