use KVM_UPSTREAM for ppc.
[qemu-kvm/fedora.git] / block-vmdk.c
blobf33484393161cb5c93dd9ff13bcb31e23939e1d9
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;
96 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
98 uint32_t magic;
100 if (buf_size < 4)
101 return 0;
102 magic = be32_to_cpu(*(uint32_t *)buf);
103 if (magic == VMDK3_MAGIC ||
104 magic == VMDK4_MAGIC)
105 return 100;
106 else
107 return 0;
110 #define CHECK_CID 1
112 #define SECTOR_SIZE 512
113 #define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
114 #define HEADER_SIZE 512 // first sector of 512 bytes
116 static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
118 BDRVVmdkState *s = bs->opaque;
119 char desc[DESC_SIZE];
120 uint32_t cid;
121 const char *p_name, *cid_str;
122 size_t cid_str_size;
124 /* the descriptor offset = 0x200 */
125 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
126 return 0;
128 if (parent) {
129 cid_str = "parentCID";
130 cid_str_size = sizeof("parentCID");
131 } else {
132 cid_str = "CID";
133 cid_str_size = sizeof("CID");
136 if ((p_name = strstr(desc,cid_str)) != 0) {
137 p_name += cid_str_size;
138 sscanf(p_name,"%x",&cid);
141 return cid;
144 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
146 BDRVVmdkState *s = bs->opaque;
147 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
148 char *p_name, *tmp_str;
150 /* the descriptor offset = 0x200 */
151 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
152 return -1;
154 tmp_str = strstr(desc,"parentCID");
155 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
156 if ((p_name = strstr(desc,"CID")) != 0) {
157 p_name += sizeof("CID");
158 snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
159 pstrcat(desc, sizeof(desc), tmp_desc);
162 if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
163 return -1;
164 return 0;
167 static int vmdk_is_cid_valid(BlockDriverState *bs)
169 #ifdef CHECK_CID
170 BDRVVmdkState *s = bs->opaque;
171 BlockDriverState *p_bs = s->hd->backing_hd;
172 uint32_t cur_pcid;
174 if (p_bs) {
175 cur_pcid = vmdk_read_cid(p_bs,0);
176 if (s->parent_cid != cur_pcid)
177 // CID not valid
178 return 0;
180 #endif
181 // CID valid
182 return 1;
185 static int vmdk_snapshot_create(const char *filename, const char *backing_file)
187 int snp_fd, p_fd;
188 uint32_t p_cid;
189 char *p_name, *gd_buf, *rgd_buf;
190 const char *real_filename, *temp_str;
191 VMDK4Header header;
192 uint32_t gde_entries, gd_size;
193 int64_t gd_offset, rgd_offset, capacity, gt_size;
194 char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
195 static const 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 %u SPARSE \"%s\"\n"
205 "\n"
206 "# The Disk Data Base \n"
207 "#DDB\n"
208 "\n";
210 snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
211 if (snp_fd < 0)
212 return -1;
213 p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
214 if (p_fd < 0) {
215 close(snp_fd);
216 return -1;
219 /* read the header */
220 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
221 goto fail;
222 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
223 goto fail;
225 /* write the header */
226 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
227 goto fail;
228 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
229 goto fail;
231 memset(&header, 0, sizeof(header));
232 memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
234 ftruncate(snp_fd, header.grain_offset << 9);
235 /* the descriptor offset = 0x200 */
236 if (lseek(p_fd, 0x200, SEEK_SET) == -1)
237 goto fail;
238 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
239 goto fail;
241 if ((p_name = strstr(p_desc,"CID")) != 0) {
242 p_name += sizeof("CID");
243 sscanf(p_name,"%x",&p_cid);
246 real_filename = filename;
247 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
248 real_filename = temp_str + 1;
249 if ((temp_str = strrchr(real_filename, '/')) != NULL)
250 real_filename = temp_str + 1;
251 if ((temp_str = strrchr(real_filename, ':')) != NULL)
252 real_filename = temp_str + 1;
254 snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
255 (uint32_t)header.capacity, real_filename);
257 /* write the descriptor */
258 if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
259 goto fail;
260 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
261 goto fail;
263 gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
264 rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
265 capacity = header.capacity * SECTOR_SIZE; // Extent size
267 * Each GDE span 32M disk, means:
268 * 512 GTE per GT, each GTE points to grain
270 gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
271 if (!gt_size)
272 goto fail;
273 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
274 gd_size = gde_entries * sizeof(uint32_t);
276 /* write RGD */
277 rgd_buf = qemu_malloc(gd_size);
278 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
279 goto fail_rgd;
280 if (read(p_fd, rgd_buf, gd_size) != gd_size)
281 goto fail_rgd;
282 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
283 goto fail_rgd;
284 if (write(snp_fd, rgd_buf, gd_size) == -1)
285 goto fail_rgd;
286 qemu_free(rgd_buf);
288 /* write GD */
289 gd_buf = qemu_malloc(gd_size);
290 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
291 goto fail_gd;
292 if (read(p_fd, gd_buf, gd_size) != gd_size)
293 goto fail_gd;
294 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
295 goto fail_gd;
296 if (write(snp_fd, gd_buf, gd_size) == -1)
297 goto fail_gd;
298 qemu_free(gd_buf);
300 close(p_fd);
301 close(snp_fd);
302 return 0;
304 fail_gd:
305 qemu_free(gd_buf);
306 fail_rgd:
307 qemu_free(rgd_buf);
308 fail:
309 close(p_fd);
310 close(snp_fd);
311 return -1;
314 static void vmdk_parent_close(BlockDriverState *bs)
316 if (bs->backing_hd)
317 bdrv_close(bs->backing_hd);
320 static int parent_open = 0;
321 static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
323 BDRVVmdkState *s = bs->opaque;
324 char *p_name;
325 char desc[DESC_SIZE];
326 char parent_img_name[1024];
328 /* the descriptor offset = 0x200 */
329 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
330 return -1;
332 if ((p_name = strstr(desc,"parentFileNameHint")) != 0) {
333 char *end_name;
334 struct stat file_buf;
336 p_name += sizeof("parentFileNameHint") + 1;
337 if ((end_name = strchr(p_name,'\"')) == 0)
338 return -1;
339 if ((end_name - p_name) > sizeof (s->hd->backing_file) - 1)
340 return -1;
342 pstrcpy(s->hd->backing_file, end_name - p_name + 1, p_name);
343 if (stat(s->hd->backing_file, &file_buf) != 0) {
344 path_combine(parent_img_name, sizeof(parent_img_name),
345 filename, s->hd->backing_file);
346 } else {
347 pstrcpy(parent_img_name, sizeof(parent_img_name),
348 s->hd->backing_file);
351 s->hd->backing_hd = bdrv_new("");
352 if (!s->hd->backing_hd) {
353 failure:
354 bdrv_close(s->hd);
355 return -1;
357 parent_open = 1;
358 if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
359 goto failure;
360 parent_open = 0;
363 return 0;
366 static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
368 BDRVVmdkState *s = bs->opaque;
369 uint32_t magic;
370 int l1_size, i, ret;
372 if (parent_open)
373 // Parent must be opened as RO.
374 flags = BDRV_O_RDONLY;
376 ret = bdrv_file_open(&s->hd, filename, flags);
377 if (ret < 0)
378 return ret;
379 if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
380 goto fail;
382 magic = be32_to_cpu(magic);
383 if (magic == VMDK3_MAGIC) {
384 VMDK3Header header;
386 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
387 goto fail;
388 s->cluster_sectors = le32_to_cpu(header.granularity);
389 s->l2_size = 1 << 9;
390 s->l1_size = 1 << 6;
391 bs->total_sectors = le32_to_cpu(header.disk_sectors);
392 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
393 s->l1_backup_table_offset = 0;
394 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
395 } else if (magic == VMDK4_MAGIC) {
396 VMDK4Header header;
398 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
399 goto fail;
400 bs->total_sectors = le64_to_cpu(header.capacity);
401 s->cluster_sectors = le64_to_cpu(header.granularity);
402 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
403 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
404 if (s->l1_entry_sectors <= 0)
405 goto fail;
406 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
407 / s->l1_entry_sectors;
408 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
409 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
411 if (parent_open)
412 s->is_parent = 1;
413 else
414 s->is_parent = 0;
416 // try to open parent images, if exist
417 if (vmdk_parent_open(bs, filename) != 0)
418 goto fail;
419 // write the CID once after the image creation
420 s->parent_cid = vmdk_read_cid(bs,1);
421 } else {
422 goto fail;
425 /* read the L1 table */
426 l1_size = s->l1_size * sizeof(uint32_t);
427 s->l1_table = qemu_malloc(l1_size);
428 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
429 goto fail;
430 for(i = 0; i < s->l1_size; i++) {
431 le32_to_cpus(&s->l1_table[i]);
434 if (s->l1_backup_table_offset) {
435 s->l1_backup_table = qemu_malloc(l1_size);
436 if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
437 goto fail;
438 for(i = 0; i < s->l1_size; i++) {
439 le32_to_cpus(&s->l1_backup_table[i]);
443 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
444 return 0;
445 fail:
446 qemu_free(s->l1_backup_table);
447 qemu_free(s->l1_table);
448 qemu_free(s->l2_cache);
449 bdrv_delete(s->hd);
450 return -1;
453 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
454 uint64_t offset, int allocate);
456 static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
457 uint64_t offset, int allocate)
459 uint64_t parent_cluster_offset;
460 BDRVVmdkState *s = bs->opaque;
461 uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
463 // we will be here if it's first write on non-exist grain(cluster).
464 // try to read from parent image, if exist
465 if (s->hd->backing_hd) {
466 BDRVVmdkState *ps = s->hd->backing_hd->opaque;
468 if (!vmdk_is_cid_valid(bs))
469 return -1;
471 parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
473 if (parent_cluster_offset) {
474 BDRVVmdkState *act_s = activeBDRV.hd->opaque;
476 if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512)
477 return -1;
479 //Write grain only into the active image
480 if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain))
481 return -1;
484 return 0;
487 static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
489 BDRVVmdkState *s = bs->opaque;
491 /* update L2 table */
492 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
493 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
494 return -1;
495 /* update backup L2 table */
496 if (s->l1_backup_table_offset != 0) {
497 m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
498 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
499 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
500 return -1;
503 return 0;
506 static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
507 uint64_t offset, int allocate)
509 BDRVVmdkState *s = bs->opaque;
510 unsigned int l1_index, l2_offset, l2_index;
511 int min_index, i, j;
512 uint32_t min_count, *l2_table, tmp = 0;
513 uint64_t cluster_offset;
515 if (m_data)
516 m_data->valid = 0;
518 l1_index = (offset >> 9) / s->l1_entry_sectors;
519 if (l1_index >= s->l1_size)
520 return 0;
521 l2_offset = s->l1_table[l1_index];
522 if (!l2_offset)
523 return 0;
524 for(i = 0; i < L2_CACHE_SIZE; i++) {
525 if (l2_offset == s->l2_cache_offsets[i]) {
526 /* increment the hit count */
527 if (++s->l2_cache_counts[i] == 0xffffffff) {
528 for(j = 0; j < L2_CACHE_SIZE; j++) {
529 s->l2_cache_counts[j] >>= 1;
532 l2_table = s->l2_cache + (i * s->l2_size);
533 goto found;
536 /* not found: load a new entry in the least used one */
537 min_index = 0;
538 min_count = 0xffffffff;
539 for(i = 0; i < L2_CACHE_SIZE; i++) {
540 if (s->l2_cache_counts[i] < min_count) {
541 min_count = s->l2_cache_counts[i];
542 min_index = i;
545 l2_table = s->l2_cache + (min_index * s->l2_size);
546 if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
547 s->l2_size * sizeof(uint32_t))
548 return 0;
550 s->l2_cache_offsets[min_index] = l2_offset;
551 s->l2_cache_counts[min_index] = 1;
552 found:
553 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
554 cluster_offset = le32_to_cpu(l2_table[l2_index]);
556 if (!cluster_offset) {
557 if (!allocate)
558 return 0;
559 // Avoid the L2 tables update for the images that have snapshots.
560 if (!s->is_parent) {
561 cluster_offset = bdrv_getlength(s->hd);
562 bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
564 cluster_offset >>= 9;
565 tmp = cpu_to_le32(cluster_offset);
566 l2_table[l2_index] = tmp;
567 // Save the active image state
568 activeBDRV.cluster_offset = cluster_offset;
569 activeBDRV.hd = bs;
571 /* First of all we write grain itself, to avoid race condition
572 * that may to corrupt the image.
573 * This problem may occur because of insufficient space on host disk
574 * or inappropriate VM shutdown.
576 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
577 return 0;
579 if (m_data) {
580 m_data->offset = tmp;
581 m_data->l1_index = l1_index;
582 m_data->l2_index = l2_index;
583 m_data->l2_offset = l2_offset;
584 m_data->valid = 1;
587 cluster_offset <<= 9;
588 return cluster_offset;
591 static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
592 int nb_sectors, int *pnum)
594 BDRVVmdkState *s = bs->opaque;
595 int index_in_cluster, n;
596 uint64_t cluster_offset;
598 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
599 index_in_cluster = sector_num % s->cluster_sectors;
600 n = s->cluster_sectors - index_in_cluster;
601 if (n > nb_sectors)
602 n = nb_sectors;
603 *pnum = n;
604 return (cluster_offset != 0);
607 static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
608 uint8_t *buf, int nb_sectors)
610 BDRVVmdkState *s = bs->opaque;
611 int index_in_cluster, n, ret;
612 uint64_t cluster_offset;
614 while (nb_sectors > 0) {
615 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
616 index_in_cluster = sector_num % s->cluster_sectors;
617 n = s->cluster_sectors - index_in_cluster;
618 if (n > nb_sectors)
619 n = nb_sectors;
620 if (!cluster_offset) {
621 // try to read from parent image, if exist
622 if (s->hd->backing_hd) {
623 if (!vmdk_is_cid_valid(bs))
624 return -1;
625 ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
626 if (ret < 0)
627 return -1;
628 } else {
629 memset(buf, 0, 512 * n);
631 } else {
632 if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
633 return -1;
635 nb_sectors -= n;
636 sector_num += n;
637 buf += n * 512;
639 return 0;
642 static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
643 const uint8_t *buf, int nb_sectors)
645 BDRVVmdkState *s = bs->opaque;
646 VmdkMetaData m_data;
647 int index_in_cluster, n;
648 uint64_t cluster_offset;
649 static int cid_update = 0;
651 if (sector_num > bs->total_sectors) {
652 fprintf(stderr,
653 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
654 " total_sectors=0x%" PRIx64 "\n",
655 sector_num, bs->total_sectors);
656 return -1;
659 while (nb_sectors > 0) {
660 index_in_cluster = sector_num & (s->cluster_sectors - 1);
661 n = s->cluster_sectors - index_in_cluster;
662 if (n > nb_sectors)
663 n = nb_sectors;
664 cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1);
665 if (!cluster_offset)
666 return -1;
668 if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
669 return -1;
670 if (m_data.valid) {
671 /* update L2 tables */
672 if (vmdk_L2update(bs, &m_data) == -1)
673 return -1;
675 nb_sectors -= n;
676 sector_num += n;
677 buf += n * 512;
679 // update CID on the first write every time the virtual disk is opened
680 if (!cid_update) {
681 vmdk_write_cid(bs, time(NULL));
682 cid_update++;
685 return 0;
688 static int vmdk_create(const char *filename, int64_t total_size,
689 const char *backing_file, int flags)
691 int fd, i;
692 VMDK4Header header;
693 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
694 static const char desc_template[] =
695 "# Disk DescriptorFile\n"
696 "version=1\n"
697 "CID=%x\n"
698 "parentCID=ffffffff\n"
699 "createType=\"monolithicSparse\"\n"
700 "\n"
701 "# Extent description\n"
702 "RW %" PRId64 " SPARSE \"%s\"\n"
703 "\n"
704 "# The Disk Data Base \n"
705 "#DDB\n"
706 "\n"
707 "ddb.virtualHWVersion = \"%d\"\n"
708 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
709 "ddb.geometry.heads = \"16\"\n"
710 "ddb.geometry.sectors = \"63\"\n"
711 "ddb.adapterType = \"ide\"\n";
712 char desc[1024];
713 const char *real_filename, *temp_str;
715 /* XXX: add support for backing file */
716 if (backing_file) {
717 return vmdk_snapshot_create(filename, backing_file);
720 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
721 0644);
722 if (fd < 0)
723 return -1;
724 magic = cpu_to_be32(VMDK4_MAGIC);
725 memset(&header, 0, sizeof(header));
726 header.version = cpu_to_le32(1);
727 header.flags = cpu_to_le32(3); /* ?? */
728 header.capacity = cpu_to_le64(total_size);
729 header.granularity = cpu_to_le64(128);
730 header.num_gtes_per_gte = cpu_to_le32(512);
732 grains = (total_size + header.granularity - 1) / header.granularity;
733 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
734 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
735 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
737 header.desc_offset = 1;
738 header.desc_size = 20;
739 header.rgd_offset = header.desc_offset + header.desc_size;
740 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
741 header.grain_offset =
742 ((header.gd_offset + gd_size + (gt_size * gt_count) +
743 header.granularity - 1) / header.granularity) *
744 header.granularity;
746 header.desc_offset = cpu_to_le64(header.desc_offset);
747 header.desc_size = cpu_to_le64(header.desc_size);
748 header.rgd_offset = cpu_to_le64(header.rgd_offset);
749 header.gd_offset = cpu_to_le64(header.gd_offset);
750 header.grain_offset = cpu_to_le64(header.grain_offset);
752 header.check_bytes[0] = 0xa;
753 header.check_bytes[1] = 0x20;
754 header.check_bytes[2] = 0xd;
755 header.check_bytes[3] = 0xa;
757 /* write all the data */
758 write(fd, &magic, sizeof(magic));
759 write(fd, &header, sizeof(header));
761 ftruncate(fd, header.grain_offset << 9);
763 /* write grain directory */
764 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
765 for (i = 0, tmp = header.rgd_offset + gd_size;
766 i < gt_count; i++, tmp += gt_size)
767 write(fd, &tmp, sizeof(tmp));
769 /* write backup grain directory */
770 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
771 for (i = 0, tmp = header.gd_offset + gd_size;
772 i < gt_count; i++, tmp += gt_size)
773 write(fd, &tmp, sizeof(tmp));
775 /* compose the descriptor */
776 real_filename = filename;
777 if ((temp_str = strrchr(real_filename, '\\')) != NULL)
778 real_filename = temp_str + 1;
779 if ((temp_str = strrchr(real_filename, '/')) != NULL)
780 real_filename = temp_str + 1;
781 if ((temp_str = strrchr(real_filename, ':')) != NULL)
782 real_filename = temp_str + 1;
783 snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL),
784 total_size, real_filename,
785 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
786 total_size / (int64_t)(63 * 16));
788 /* write the descriptor */
789 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
790 write(fd, desc, strlen(desc));
792 close(fd);
793 return 0;
796 static void vmdk_close(BlockDriverState *bs)
798 BDRVVmdkState *s = bs->opaque;
800 qemu_free(s->l1_table);
801 qemu_free(s->l2_cache);
802 // try to close parent image, if exist
803 vmdk_parent_close(s->hd);
804 bdrv_delete(s->hd);
807 static void vmdk_flush(BlockDriverState *bs)
809 BDRVVmdkState *s = bs->opaque;
810 bdrv_flush(s->hd);
813 BlockDriver bdrv_vmdk = {
814 "vmdk",
815 sizeof(BDRVVmdkState),
816 vmdk_probe,
817 vmdk_open,
818 vmdk_read,
819 vmdk_write,
820 vmdk_close,
821 vmdk_create,
822 vmdk_flush,
823 vmdk_is_allocated,