Fix msr_mask.
[qemu/mini2440.git] / block-vmdk.c
blobaf979a1e8e4a9aea4ef6f8b5eb84d424d3e0417d
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 "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')
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 } BDRVVmdkState;
81 typedef struct VmdkMetaData {
82 uint32_t offset;
83 unsigned int l1_index;
84 unsigned int l2_index;
85 unsigned int l2_offset;
86 int valid;
87 } VmdkMetaData;
89 typedef struct ActiveBDRVState{
90 BlockDriverState *hd; // active image handler
91 uint64_t cluster_offset; // current write offset
92 }ActiveBDRVState;
94 static ActiveBDRVState activeBDRV;
97 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
99 uint32_t magic;
101 if (buf_size < 4)
102 return 0;
103 magic = be32_to_cpu(*(uint32_t *)buf);
104 if (magic == VMDK3_MAGIC ||
105 magic == VMDK4_MAGIC)
106 return 100;
107 else
108 return 0;
111 #define CHECK_CID 1
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];
121 uint32_t cid;
122 char *p_name, *cid_str;
123 size_t cid_str_size;
125 /* the descriptor offset = 0x200 */
126 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
127 return 0;
129 if (parent) {
130 cid_str = "parentCID";
131 cid_str_size = sizeof("parentCID");
132 } else {
133 cid_str = "CID";
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);
142 return 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)
153 return -1;
155 tmp_str = strstr(desc,"parentCID");
156 strcpy(tmp_desc, tmp_str);
157 if ((p_name = strstr(desc,"CID")) != 0) {
158 p_name += sizeof("CID");
159 sprintf(p_name,"%x\n",cid);
160 strcat(desc,tmp_desc);
163 if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
164 return -1;
165 return 0;
168 static int vmdk_is_cid_valid(BlockDriverState *bs)
170 #ifdef CHECK_CID
171 BDRVVmdkState *s = bs->opaque;
172 BlockDriverState *p_bs = s->hd->backing_hd;
173 uint32_t cur_pcid;
175 if (p_bs) {
176 cur_pcid = vmdk_read_cid(p_bs,0);
177 if (s->parent_cid != cur_pcid)
178 // CID not valid
179 return 0;
181 #endif
182 // CID valid
183 return 1;
186 static int vmdk_snapshot_create(const char *filename, const char *backing_file)
188 int snp_fd, p_fd;
189 uint32_t p_cid;
190 char *p_name, *gd_buf, *rgd_buf;
191 const char *real_filename, *temp_str;
192 VMDK4Header header;
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 char *desc_template =
197 "# Disk DescriptorFile\n"
198 "version=1\n"
199 "CID=%x\n"
200 "parentCID=%x\n"
201 "createType=\"monolithicSparse\"\n"
202 "parentFileNameHint=\"%s\"\n"
203 "\n"
204 "# Extent description\n"
205 "RW %lu SPARSE \"%s\"\n"
206 "\n"
207 "# The Disk Data Base \n"
208 "#DDB\n"
209 "\n";
211 snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
212 if (snp_fd < 0)
213 return -1;
214 p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
215 if (p_fd < 0) {
216 close(snp_fd);
217 return -1;
220 /* read the header */
221 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
222 goto fail;
223 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
224 goto fail;
226 /* write the header */
227 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
228 goto fail;
229 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
230 goto fail;
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)
238 goto fail;
239 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
240 goto fail;
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 sprintf(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)
260 goto fail;
261 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
262 goto fail;
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;
272 if (!gt_size)
273 goto fail;
274 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
275 gd_size = gde_entries * sizeof(uint32_t);
277 /* write RGD */
278 rgd_buf = qemu_malloc(gd_size);
279 if (!rgd_buf)
280 goto fail;
281 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
282 goto fail_rgd;
283 if (read(p_fd, rgd_buf, gd_size) != gd_size)
284 goto fail_rgd;
285 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
286 goto fail_rgd;
287 if (write(snp_fd, rgd_buf, gd_size) == -1)
288 goto fail_rgd;
289 qemu_free(rgd_buf);
291 /* write GD */
292 gd_buf = qemu_malloc(gd_size);
293 if (!gd_buf)
294 goto fail_rgd;
295 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
296 goto fail_gd;
297 if (read(p_fd, gd_buf, gd_size) != gd_size)
298 goto fail_gd;
299 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
300 goto fail_gd;
301 if (write(snp_fd, gd_buf, gd_size) == -1)
302 goto fail_gd;
303 qemu_free(gd_buf);
305 close(p_fd);
306 close(snp_fd);
307 return 0;
309 fail_gd:
310 qemu_free(gd_buf);
311 fail_rgd:
312 qemu_free(rgd_buf);
313 fail:
314 close(p_fd);
315 close(snp_fd);
316 return -1;
319 static void vmdk_parent_close(BlockDriverState *bs)
321 if (bs->backing_hd)
322 bdrv_close(bs->backing_hd);
325 int parent_open = 0;
326 static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
328 BDRVVmdkState *s = bs->opaque;
329 char *p_name;
330 char desc[DESC_SIZE];
331 char parent_img_name[1024];
333 /* the descriptor offset = 0x200 */
334 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
335 return -1;
337 if ((p_name = strstr(desc,"parentFileNameHint")) != 0) {
338 char *end_name;
339 struct stat file_buf;
341 p_name += sizeof("parentFileNameHint") + 1;
342 if ((end_name = strchr(p_name,'\"')) == 0)
343 return -1;
345 strncpy(s->hd->backing_file, p_name, end_name - p_name);
346 if (stat(s->hd->backing_file, &file_buf) != 0) {
347 path_combine(parent_img_name, sizeof(parent_img_name),
348 filename, s->hd->backing_file);
349 } else {
350 strcpy(parent_img_name, s->hd->backing_file);
353 s->hd->backing_hd = bdrv_new("");
354 if (!s->hd->backing_hd) {
355 failure:
356 bdrv_close(s->hd);
357 return -1;
359 parent_open = 1;
360 if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
361 goto failure;
362 parent_open = 0;
365 return 0;
368 static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
370 BDRVVmdkState *s = bs->opaque;
371 uint32_t magic;
372 int l1_size, i, ret;
374 if (parent_open)
375 // Parent must be opened as RO.
376 flags = BDRV_O_RDONLY;
377 fprintf(stderr, "(VMDK) image open: flags=0x%x filename=%s\n", flags, bs->filename);
379 ret = bdrv_file_open(&s->hd, filename, flags);
380 if (ret < 0)
381 return ret;
382 if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
383 goto fail;
385 magic = be32_to_cpu(magic);
386 if (magic == VMDK3_MAGIC) {
387 VMDK3Header header;
389 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
390 goto fail;
391 s->cluster_sectors = le32_to_cpu(header.granularity);
392 s->l2_size = 1 << 9;
393 s->l1_size = 1 << 6;
394 bs->total_sectors = le32_to_cpu(header.disk_sectors);
395 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
396 s->l1_backup_table_offset = 0;
397 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
398 } else if (magic == VMDK4_MAGIC) {
399 VMDK4Header header;
401 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
402 goto fail;
403 bs->total_sectors = le64_to_cpu(header.capacity);
404 s->cluster_sectors = le64_to_cpu(header.granularity);
405 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
406 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
407 if (s->l1_entry_sectors <= 0)
408 goto fail;
409 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
410 / s->l1_entry_sectors;
411 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
412 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
414 if (parent_open)
415 s->is_parent = 1;
416 else
417 s->is_parent = 0;
419 // try to open parent images, if exist
420 if (vmdk_parent_open(bs, filename) != 0)
421 goto fail;
422 // write the CID once after the image creation
423 s->parent_cid = vmdk_read_cid(bs,1);
424 } else {
425 goto fail;
428 /* read the L1 table */
429 l1_size = s->l1_size * sizeof(uint32_t);
430 s->l1_table = qemu_malloc(l1_size);
431 if (!s->l1_table)
432 goto fail;
433 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
434 goto fail;
435 for(i = 0; i < s->l1_size; i++) {
436 le32_to_cpus(&s->l1_table[i]);
439 if (s->l1_backup_table_offset) {
440 s->l1_backup_table = qemu_malloc(l1_size);
441 if (!s->l1_backup_table)
442 goto fail;
443 if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
444 goto fail;
445 for(i = 0; i < s->l1_size; i++) {
446 le32_to_cpus(&s->l1_backup_table[i]);
450 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
451 if (!s->l2_cache)
452 goto fail;
453 return 0;
454 fail:
455 qemu_free(s->l1_backup_table);
456 qemu_free(s->l1_table);
457 qemu_free(s->l2_cache);
458 bdrv_delete(s->hd);
459 return -1;
462 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
463 uint64_t offset, int allocate);
465 static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
466 uint64_t offset, int allocate)
468 uint64_t parent_cluster_offset;
469 BDRVVmdkState *s = bs->opaque;
470 uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
472 // we will be here if it's first write on non-exist grain(cluster).
473 // try to read from parent image, if exist
474 if (s->hd->backing_hd) {
475 BDRVVmdkState *ps = s->hd->backing_hd->opaque;
477 if (!vmdk_is_cid_valid(bs))
478 return -1;
480 parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
482 if (parent_cluster_offset) {
483 BDRVVmdkState *act_s = activeBDRV.hd->opaque;
485 if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512)
486 return -1;
488 //Write grain only into the active image
489 if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain))
490 return -1;
493 return 0;
496 static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
498 BDRVVmdkState *s = bs->opaque;
500 /* update L2 table */
501 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
502 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
503 return -1;
504 /* update backup L2 table */
505 if (s->l1_backup_table_offset != 0) {
506 m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
507 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
508 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
509 return -1;
512 return 0;
515 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
516 uint64_t offset, int allocate)
518 BDRVVmdkState *s = bs->opaque;
519 unsigned int l1_index, l2_offset, l2_index;
520 int min_index, i, j;
521 uint32_t min_count, *l2_table, tmp = 0;
522 uint64_t cluster_offset;
524 if (m_data)
525 m_data->valid = 0;
527 l1_index = (offset >> 9) / s->l1_entry_sectors;
528 if (l1_index >= s->l1_size)
529 return 0;
530 l2_offset = s->l1_table[l1_index];
531 if (!l2_offset)
532 return 0;
533 for(i = 0; i < L2_CACHE_SIZE; i++) {
534 if (l2_offset == s->l2_cache_offsets[i]) {
535 /* increment the hit count */
536 if (++s->l2_cache_counts[i] == 0xffffffff) {
537 for(j = 0; j < L2_CACHE_SIZE; j++) {
538 s->l2_cache_counts[j] >>= 1;
541 l2_table = s->l2_cache + (i * s->l2_size);
542 goto found;
545 /* not found: load a new entry in the least used one */
546 min_index = 0;
547 min_count = 0xffffffff;
548 for(i = 0; i < L2_CACHE_SIZE; i++) {
549 if (s->l2_cache_counts[i] < min_count) {
550 min_count = s->l2_cache_counts[i];
551 min_index = i;
554 l2_table = s->l2_cache + (min_index * s->l2_size);
555 if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
556 s->l2_size * sizeof(uint32_t))
557 return 0;
559 s->l2_cache_offsets[min_index] = l2_offset;
560 s->l2_cache_counts[min_index] = 1;
561 found:
562 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
563 cluster_offset = le32_to_cpu(l2_table[l2_index]);
565 if (!cluster_offset) {
566 if (!allocate)
567 return 0;
568 // Avoid the L2 tables update for the images that have snapshots.
569 if (!s->is_parent) {
570 cluster_offset = bdrv_getlength(s->hd);
571 bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
573 cluster_offset >>= 9;
574 tmp = cpu_to_le32(cluster_offset);
575 l2_table[l2_index] = tmp;
576 // Save the active image state
577 activeBDRV.cluster_offset = cluster_offset;
578 activeBDRV.hd = bs;
580 /* First of all we write grain itself, to avoid race condition
581 * that may to corrupt the image.
582 * This problem may occur because of insufficient space on host disk
583 * or inappropriate VM shutdown.
585 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
586 return 0;
588 if (m_data) {
589 m_data->offset = tmp;
590 m_data->l1_index = l1_index;
591 m_data->l2_index = l2_index;
592 m_data->l2_offset = l2_offset;
593 m_data->valid = 1;
596 cluster_offset <<= 9;
597 return cluster_offset;
600 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
601 int nb_sectors, int *pnum)
603 BDRVVmdkState *s = bs->opaque;
604 int index_in_cluster, n;
605 uint64_t cluster_offset;
607 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
608 index_in_cluster = sector_num % s->cluster_sectors;
609 n = s->cluster_sectors - index_in_cluster;
610 if (n > nb_sectors)
611 n = nb_sectors;
612 *pnum = n;
613 return (cluster_offset != 0);
616 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
617 uint8_t *buf, int nb_sectors)
619 BDRVVmdkState *s = bs->opaque;
620 int index_in_cluster, n, ret;
621 uint64_t cluster_offset;
623 while (nb_sectors > 0) {
624 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
625 index_in_cluster = sector_num % s->cluster_sectors;
626 n = s->cluster_sectors - index_in_cluster;
627 if (n > nb_sectors)
628 n = nb_sectors;
629 if (!cluster_offset) {
630 // try to read from parent image, if exist
631 if (s->hd->backing_hd) {
632 if (!vmdk_is_cid_valid(bs))
633 return -1;
634 ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
635 if (ret < 0)
636 return -1;
637 } else {
638 memset(buf, 0, 512 * n);
640 } else {
641 if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
642 return -1;
644 nb_sectors -= n;
645 sector_num += n;
646 buf += n * 512;
648 return 0;
651 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
652 const uint8_t *buf, int nb_sectors)
654 BDRVVmdkState *s = bs->opaque;
655 VmdkMetaData m_data;
656 int index_in_cluster, n;
657 uint64_t cluster_offset;
658 static int cid_update = 0;
660 if (sector_num > bs->total_sectors) {
661 fprintf(stderr,
662 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
663 " total_sectors=0x%" PRIx64 "\n",
664 sector_num, bs->total_sectors);
665 return -1;
668 while (nb_sectors > 0) {
669 index_in_cluster = sector_num & (s->cluster_sectors - 1);
670 n = s->cluster_sectors - index_in_cluster;
671 if (n > nb_sectors)
672 n = nb_sectors;
673 cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1);
674 if (!cluster_offset)
675 return -1;
677 if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
678 return -1;
679 if (m_data.valid) {
680 /* update L2 tables */
681 if (vmdk_L2update(bs, &m_data) == -1)
682 return -1;
684 nb_sectors -= n;
685 sector_num += n;
686 buf += n * 512;
688 // update CID on the first write every time the virtual disk is opened
689 if (!cid_update) {
690 vmdk_write_cid(bs, time(NULL));
691 cid_update++;
694 return 0;
697 static int vmdk_create(const char *filename, int64_t total_size,
698 const char *backing_file, int flags)
700 int fd, i;
701 VMDK4Header header;
702 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
703 char *desc_template =
704 "# Disk DescriptorFile\n"
705 "version=1\n"
706 "CID=%x\n"
707 "parentCID=ffffffff\n"
708 "createType=\"monolithicSparse\"\n"
709 "\n"
710 "# Extent description\n"
711 "RW %lu SPARSE \"%s\"\n"
712 "\n"
713 "# The Disk Data Base \n"
714 "#DDB\n"
715 "\n"
716 "ddb.virtualHWVersion = \"%d\"\n"
717 "ddb.geometry.cylinders = \"%lu\"\n"
718 "ddb.geometry.heads = \"16\"\n"
719 "ddb.geometry.sectors = \"63\"\n"
720 "ddb.adapterType = \"ide\"\n";
721 char desc[1024];
722 const char *real_filename, *temp_str;
724 /* XXX: add support for backing file */
725 if (backing_file) {
726 return vmdk_snapshot_create(filename, backing_file);
729 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
730 0644);
731 if (fd < 0)
732 return -1;
733 magic = cpu_to_be32(VMDK4_MAGIC);
734 memset(&header, 0, sizeof(header));
735 header.version = cpu_to_le32(1);
736 header.flags = cpu_to_le32(3); /* ?? */
737 header.capacity = cpu_to_le64(total_size);
738 header.granularity = cpu_to_le64(128);
739 header.num_gtes_per_gte = cpu_to_le32(512);
741 grains = (total_size + header.granularity - 1) / header.granularity;
742 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
743 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
744 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
746 header.desc_offset = 1;
747 header.desc_size = 20;
748 header.rgd_offset = header.desc_offset + header.desc_size;
749 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
750 header.grain_offset =
751 ((header.gd_offset + gd_size + (gt_size * gt_count) +
752 header.granularity - 1) / header.granularity) *
753 header.granularity;
755 header.desc_offset = cpu_to_le64(header.desc_offset);
756 header.desc_size = cpu_to_le64(header.desc_size);
757 header.rgd_offset = cpu_to_le64(header.rgd_offset);
758 header.gd_offset = cpu_to_le64(header.gd_offset);
759 header.grain_offset = cpu_to_le64(header.grain_offset);
761 header.check_bytes[0] = 0xa;
762 header.check_bytes[1] = 0x20;
763 header.check_bytes[2] = 0xd;
764 header.check_bytes[3] = 0xa;
766 /* write all the data */
767 write(fd, &magic, sizeof(magic));
768 write(fd, &header, sizeof(header));
770 ftruncate(fd, header.grain_offset << 9);
772 /* write grain directory */
773 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
774 for (i = 0, tmp = header.rgd_offset + gd_size;
775 i < gt_count; i++, tmp += gt_size)
776 write(fd, &tmp, sizeof(tmp));
778 /* write backup grain directory */
779 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
780 for (i = 0, tmp = header.gd_offset + gd_size;
781 i < gt_count; i++, tmp += gt_size)
782 write(fd, &tmp, sizeof(tmp));
784 /* compose the descriptor */
785 real_filename = filename;
786 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
787 real_filename = temp_str + 1;
788 if ((temp_str = strrchr(real_filename, '/')) != NULL)
789 real_filename = temp_str + 1;
790 if ((temp_str = strrchr(real_filename, ':')) != NULL)
791 real_filename = temp_str + 1;
792 sprintf(desc, desc_template, time(NULL), (unsigned long)total_size,
793 real_filename, (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4), total_size / (63 * 16));
795 /* write the descriptor */
796 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
797 write(fd, desc, strlen(desc));
799 close(fd);
800 return 0;
803 static void vmdk_close(BlockDriverState *bs)
805 BDRVVmdkState *s = bs->opaque;
807 qemu_free(s->l1_table);
808 qemu_free(s->l2_cache);
809 bdrv_delete(s->hd);
810 // try to close parent image, if exist
811 vmdk_parent_close(s->hd);
814 static void vmdk_flush(BlockDriverState *bs)
816 BDRVVmdkState *s = bs->opaque;
817 bdrv_flush(s->hd);
820 BlockDriver bdrv_vmdk = {
821 "vmdk",
822 sizeof(BDRVVmdkState),
823 vmdk_probe,
824 vmdk_open,
825 vmdk_read,
826 vmdk_write,
827 vmdk_close,
828 vmdk_create,
829 vmdk_flush,
830 vmdk_is_allocated,