]> git.proxmox.com Git - qemu.git/blame - block/vmdk.c
Create qemu-option.h
[qemu.git] / block / vmdk.c
CommitLineData
ea2384d3
FB
1/*
2 * Block driver for the VMDK format
5fafdf24 3 *
ea2384d3 4 * Copyright (c) 2004 Fabrice Bellard
ff1afc72 5 * Copyright (c) 2005 Filip Navara
5fafdf24 6 *
ea2384d3
FB
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
5f4da8c0 25
faf07963 26#include "qemu-common.h"
ea2384d3 27#include "block_int.h"
5efa9d5a 28#include "module.h"
ea2384d3 29
ea2384d3
FB
30#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
31#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
32
33typedef 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;
45
46typedef 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];
ff1afc72 59} __attribute__((packed)) VMDK4Header;
ea2384d3
FB
60
61#define L2_CACHE_SIZE 16
62
63typedef struct BDRVVmdkState {
5f4da8c0 64 BlockDriverState *hd;
ea2384d3 65 int64_t l1_table_offset;
ff1afc72 66 int64_t l1_backup_table_offset;
ea2384d3 67 uint32_t *l1_table;
ff1afc72 68 uint32_t *l1_backup_table;
ea2384d3
FB
69 unsigned int l1_size;
70 uint32_t l1_entry_sectors;
71
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];
76
77 unsigned int cluster_sectors;
5f4da8c0 78 uint32_t parent_cid;
630530a6 79 int is_parent;
ea2384d3
FB
80} BDRVVmdkState;
81
630530a6
TS
82typedef 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;
89
90typedef struct ActiveBDRVState{
91 BlockDriverState *hd; // active image handler
92 uint64_t cluster_offset; // current write offset
93}ActiveBDRVState;
94
95static ActiveBDRVState activeBDRV;
96
97
ea2384d3
FB
98static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
99{
100 uint32_t magic;
101
102 if (buf_size < 4)
103 return 0;
104 magic = be32_to_cpu(*(uint32_t *)buf);
105 if (magic == VMDK3_MAGIC ||
106 magic == VMDK4_MAGIC)
107 return 100;
108 else
109 return 0;
110}
111
5f4da8c0
TS
112#define CHECK_CID 1
113
3b46e624 114#define SECTOR_SIZE 512
5f4da8c0 115#define DESC_SIZE 20*SECTOR_SIZE // 20 sectors of 512 bytes each
5fafdf24 116#define HEADER_SIZE 512 // first sector of 512 bytes
5f4da8c0
TS
117
118static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
ea2384d3
FB
119{
120 BDRVVmdkState *s = bs->opaque;
5f4da8c0
TS
121 char desc[DESC_SIZE];
122 uint32_t cid;
7ccfb2eb 123 const char *p_name, *cid_str;
5f4da8c0
TS
124 size_t cid_str_size;
125
126 /* the descriptor offset = 0x200 */
127 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
128 return 0;
129
130 if (parent) {
131 cid_str = "parentCID";
132 cid_str_size = sizeof("parentCID");
133 } else {
134 cid_str = "CID";
135 cid_str_size = sizeof("CID");
136 }
137
511d2b14 138 if ((p_name = strstr(desc,cid_str)) != NULL) {
5f4da8c0
TS
139 p_name += cid_str_size;
140 sscanf(p_name,"%x",&cid);
141 }
142
143 return cid;
144}
145
146static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
147{
148 BDRVVmdkState *s = bs->opaque;
149 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
150 char *p_name, *tmp_str;
151
152 /* the descriptor offset = 0x200 */
153 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
154 return -1;
155
156 tmp_str = strstr(desc,"parentCID");
363a37d5 157 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
511d2b14 158 if ((p_name = strstr(desc,"CID")) != NULL) {
5f4da8c0 159 p_name += sizeof("CID");
363a37d5
BS
160 snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
161 pstrcat(desc, sizeof(desc), tmp_desc);
5f4da8c0
TS
162 }
163
164 if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
165 return -1;
166 return 0;
167}
168
169static int vmdk_is_cid_valid(BlockDriverState *bs)
170{
171#ifdef CHECK_CID
172 BDRVVmdkState *s = bs->opaque;
173 BlockDriverState *p_bs = s->hd->backing_hd;
174 uint32_t cur_pcid;
175
176 if (p_bs) {
177 cur_pcid = vmdk_read_cid(p_bs,0);
178 if (s->parent_cid != cur_pcid)
179 // CID not valid
180 return 0;
181 }
182#endif
183 // CID valid
184 return 1;
185}
186
187static int vmdk_snapshot_create(const char *filename, const char *backing_file)
188{
189 int snp_fd, p_fd;
190 uint32_t p_cid;
5fafdf24 191 char *p_name, *gd_buf, *rgd_buf;
5f4da8c0
TS
192 const char *real_filename, *temp_str;
193 VMDK4Header header;
194 uint32_t gde_entries, gd_size;
195 int64_t gd_offset, rgd_offset, capacity, gt_size;
196 char p_desc[DESC_SIZE], s_desc[DESC_SIZE], hdr[HEADER_SIZE];
7ccfb2eb 197 static const char desc_template[] =
5f4da8c0
TS
198 "# Disk DescriptorFile\n"
199 "version=1\n"
200 "CID=%x\n"
201 "parentCID=%x\n"
202 "createType=\"monolithicSparse\"\n"
203 "parentFileNameHint=\"%s\"\n"
204 "\n"
205 "# Extent description\n"
7ccfb2eb 206 "RW %u SPARSE \"%s\"\n"
5f4da8c0
TS
207 "\n"
208 "# The Disk Data Base \n"
209 "#DDB\n"
210 "\n";
211
212 snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
213 if (snp_fd < 0)
214 return -1;
215 p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
216 if (p_fd < 0) {
217 close(snp_fd);
218 return -1;
219 }
220
221 /* read the header */
222 if (lseek(p_fd, 0x0, SEEK_SET) == -1)
223 goto fail;
224 if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
225 goto fail;
226
227 /* write the header */
228 if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
229 goto fail;
230 if (write(snp_fd, hdr, HEADER_SIZE) == -1)
231 goto fail;
232
233 memset(&header, 0, sizeof(header));
234 memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
235
236 ftruncate(snp_fd, header.grain_offset << 9);
237 /* the descriptor offset = 0x200 */
238 if (lseek(p_fd, 0x200, SEEK_SET) == -1)
239 goto fail;
240 if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
241 goto fail;
242
511d2b14 243 if ((p_name = strstr(p_desc,"CID")) != NULL) {
5f4da8c0
TS
244 p_name += sizeof("CID");
245 sscanf(p_name,"%x",&p_cid);
246 }
247
248 real_filename = filename;
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;
253 if ((temp_str = strrchr(real_filename, ':')) != NULL)
254 real_filename = temp_str + 1;
255
363a37d5
BS
256 snprintf(s_desc, sizeof(s_desc), desc_template, p_cid, p_cid, backing_file,
257 (uint32_t)header.capacity, real_filename);
5f4da8c0
TS
258
259 /* write the descriptor */
260 if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
261 goto fail;
262 if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
263 goto fail;
ea2384d3 264
5f4da8c0
TS
265 gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
266 rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
267 capacity = header.capacity * SECTOR_SIZE; // Extent size
268 /*
269 * Each GDE span 32M disk, means:
270 * 512 GTE per GT, each GTE points to grain
271 */
272 gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
273 if (!gt_size)
274 goto fail;
5fafdf24 275 gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
5f4da8c0
TS
276 gd_size = gde_entries * sizeof(uint32_t);
277
278 /* write RGD */
279 rgd_buf = qemu_malloc(gd_size);
5f4da8c0
TS
280 if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
281 goto fail_rgd;
282 if (read(p_fd, rgd_buf, gd_size) != gd_size)
283 goto fail_rgd;
284 if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
285 goto fail_rgd;
286 if (write(snp_fd, rgd_buf, gd_size) == -1)
287 goto fail_rgd;
288 qemu_free(rgd_buf);
289
290 /* write GD */
291 gd_buf = qemu_malloc(gd_size);
5f4da8c0
TS
292 if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
293 goto fail_gd;
294 if (read(p_fd, gd_buf, gd_size) != gd_size)
295 goto fail_gd;
296 if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
297 goto fail_gd;
298 if (write(snp_fd, gd_buf, gd_size) == -1)
299 goto fail_gd;
300 qemu_free(gd_buf);
301
302 close(p_fd);
303 close(snp_fd);
304 return 0;
305
306 fail_gd:
307 qemu_free(gd_buf);
3b46e624 308 fail_rgd:
5f4da8c0
TS
309 qemu_free(rgd_buf);
310 fail:
311 close(p_fd);
312 close(snp_fd);
313 return -1;
314}
315
316static void vmdk_parent_close(BlockDriverState *bs)
317{
318 if (bs->backing_hd)
319 bdrv_close(bs->backing_hd);
320}
321
b1d8e52e 322static int parent_open = 0;
5f4da8c0
TS
323static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
324{
325 BDRVVmdkState *s = bs->opaque;
5fafdf24 326 char *p_name;
5f4da8c0
TS
327 char desc[DESC_SIZE];
328 char parent_img_name[1024];
329
330 /* the descriptor offset = 0x200 */
331 if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
332 return -1;
333
511d2b14 334 if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) {
5f4da8c0
TS
335 char *end_name;
336 struct stat file_buf;
337
338 p_name += sizeof("parentFileNameHint") + 1;
511d2b14 339 if ((end_name = strchr(p_name,'\"')) == NULL)
5f4da8c0 340 return -1;
b34d259a
AZ
341 if ((end_name - p_name) > sizeof (s->hd->backing_file) - 1)
342 return -1;
3b46e624 343
9b8114a8 344 pstrcpy(s->hd->backing_file, end_name - p_name + 1, p_name);
5f4da8c0
TS
345 if (stat(s->hd->backing_file, &file_buf) != 0) {
346 path_combine(parent_img_name, sizeof(parent_img_name),
347 filename, s->hd->backing_file);
348 } else {
363a37d5
BS
349 pstrcpy(parent_img_name, sizeof(parent_img_name),
350 s->hd->backing_file);
5f4da8c0
TS
351 }
352
353 s->hd->backing_hd = bdrv_new("");
354 if (!s->hd->backing_hd) {
355 failure:
356 bdrv_close(s->hd);
ff1afc72 357 return -1;
5f4da8c0 358 }
630530a6
TS
359 parent_open = 1;
360 if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
5f4da8c0 361 goto failure;
630530a6 362 parent_open = 0;
ff1afc72 363 }
5f4da8c0
TS
364
365 return 0;
366}
367
368static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
369{
370 BDRVVmdkState *s = bs->opaque;
371 uint32_t magic;
372 int l1_size, i, ret;
373
630530a6
TS
374 if (parent_open)
375 // Parent must be opened as RO.
376 flags = BDRV_O_RDONLY;
630530a6 377
b5eff355 378 ret = bdrv_file_open(&s->hd, filename, flags);
5f4da8c0
TS
379 if (ret < 0)
380 return ret;
381 if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
ea2384d3 382 goto fail;
5f4da8c0 383
7143c62c 384 magic = be32_to_cpu(magic);
ea2384d3
FB
385 if (magic == VMDK3_MAGIC) {
386 VMDK3Header header;
5f4da8c0
TS
387
388 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
ea2384d3
FB
389 goto fail;
390 s->cluster_sectors = le32_to_cpu(header.granularity);
391 s->l2_size = 1 << 9;
392 s->l1_size = 1 << 6;
393 bs->total_sectors = le32_to_cpu(header.disk_sectors);
ff1afc72
FB
394 s->l1_table_offset = le32_to_cpu(header.l1dir_offset) << 9;
395 s->l1_backup_table_offset = 0;
ea2384d3
FB
396 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
397 } else if (magic == VMDK4_MAGIC) {
398 VMDK4Header header;
5f4da8c0
TS
399
400 if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
ea2384d3 401 goto fail;
bd6ea3c8
FB
402 bs->total_sectors = le64_to_cpu(header.capacity);
403 s->cluster_sectors = le64_to_cpu(header.granularity);
ea2384d3
FB
404 s->l2_size = le32_to_cpu(header.num_gtes_per_gte);
405 s->l1_entry_sectors = s->l2_size * s->cluster_sectors;
406 if (s->l1_entry_sectors <= 0)
407 goto fail;
5fafdf24 408 s->l1_size = (bs->total_sectors + s->l1_entry_sectors - 1)
ea2384d3 409 / s->l1_entry_sectors;
ff1afc72
FB
410 s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
411 s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
5f4da8c0 412
630530a6
TS
413 if (parent_open)
414 s->is_parent = 1;
415 else
416 s->is_parent = 0;
417
5f4da8c0
TS
418 // try to open parent images, if exist
419 if (vmdk_parent_open(bs, filename) != 0)
420 goto fail;
421 // write the CID once after the image creation
422 s->parent_cid = vmdk_read_cid(bs,1);
ea2384d3
FB
423 } else {
424 goto fail;
425 }
5f4da8c0 426
ea2384d3
FB
427 /* read the L1 table */
428 l1_size = s->l1_size * sizeof(uint32_t);
429 s->l1_table = qemu_malloc(l1_size);
5f4da8c0 430 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
ea2384d3
FB
431 goto fail;
432 for(i = 0; i < s->l1_size; i++) {
433 le32_to_cpus(&s->l1_table[i]);
434 }
435
ff1afc72
FB
436 if (s->l1_backup_table_offset) {
437 s->l1_backup_table = qemu_malloc(l1_size);
5f4da8c0 438 if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
ff1afc72
FB
439 goto fail;
440 for(i = 0; i < s->l1_size; i++) {
441 le32_to_cpus(&s->l1_backup_table[i]);
442 }
443 }
444
ea2384d3 445 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
ea2384d3
FB
446 return 0;
447 fail:
ff1afc72 448 qemu_free(s->l1_backup_table);
ea2384d3
FB
449 qemu_free(s->l1_table);
450 qemu_free(s->l2_cache);
5f4da8c0 451 bdrv_delete(s->hd);
ea2384d3
FB
452 return -1;
453}
454
630530a6
TS
455static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
456 uint64_t offset, int allocate);
5f4da8c0
TS
457
458static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
459 uint64_t offset, int allocate)
460{
461 uint64_t parent_cluster_offset;
462 BDRVVmdkState *s = bs->opaque;
463 uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
464
465 // we will be here if it's first write on non-exist grain(cluster).
466 // try to read from parent image, if exist
467 if (s->hd->backing_hd) {
468 BDRVVmdkState *ps = s->hd->backing_hd->opaque;
469
470 if (!vmdk_is_cid_valid(bs))
471 return -1;
5f4da8c0 472
630530a6
TS
473 parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
474
475 if (parent_cluster_offset) {
476 BDRVVmdkState *act_s = activeBDRV.hd->opaque;
477
478 if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512)
479 return -1;
480
481 //Write grain only into the active image
482 if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain))
483 return -1;
484 }
485 }
486 return 0;
487}
488
489static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
490{
491 BDRVVmdkState *s = bs->opaque;
492
493 /* update L2 table */
494 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
495 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
496 return -1;
497 /* update backup L2 table */
498 if (s->l1_backup_table_offset != 0) {
499 m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
500 if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
501 &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
5f4da8c0
TS
502 return -1;
503 }
630530a6 504
5f4da8c0
TS
505 return 0;
506}
507
630530a6 508static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
ff1afc72 509 uint64_t offset, int allocate)
ea2384d3
FB
510{
511 BDRVVmdkState *s = bs->opaque;
512 unsigned int l1_index, l2_offset, l2_index;
513 int min_index, i, j;
630530a6 514 uint32_t min_count, *l2_table, tmp = 0;
ea2384d3 515 uint64_t cluster_offset;
630530a6
TS
516
517 if (m_data)
518 m_data->valid = 0;
519
ea2384d3
FB
520 l1_index = (offset >> 9) / s->l1_entry_sectors;
521 if (l1_index >= s->l1_size)
522 return 0;
523 l2_offset = s->l1_table[l1_index];
524 if (!l2_offset)
525 return 0;
ea2384d3
FB
526 for(i = 0; i < L2_CACHE_SIZE; i++) {
527 if (l2_offset == s->l2_cache_offsets[i]) {
528 /* increment the hit count */
529 if (++s->l2_cache_counts[i] == 0xffffffff) {
530 for(j = 0; j < L2_CACHE_SIZE; j++) {
531 s->l2_cache_counts[j] >>= 1;
532 }
533 }
534 l2_table = s->l2_cache + (i * s->l2_size);
535 goto found;
536 }
537 }
538 /* not found: load a new entry in the least used one */
539 min_index = 0;
540 min_count = 0xffffffff;
541 for(i = 0; i < L2_CACHE_SIZE; i++) {
542 if (s->l2_cache_counts[i] < min_count) {
543 min_count = s->l2_cache_counts[i];
544 min_index = i;
545 }
546 }
547 l2_table = s->l2_cache + (min_index * s->l2_size);
5fafdf24 548 if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
5f4da8c0 549 s->l2_size * sizeof(uint32_t))
ea2384d3 550 return 0;
5f4da8c0 551
ea2384d3
FB
552 s->l2_cache_offsets[min_index] = l2_offset;
553 s->l2_cache_counts[min_index] = 1;
554 found:
555 l2_index = ((offset >> 9) / s->cluster_sectors) % s->l2_size;
556 cluster_offset = le32_to_cpu(l2_table[l2_index]);
630530a6 557
ff1afc72
FB
558 if (!cluster_offset) {
559 if (!allocate)
560 return 0;
630530a6
TS
561 // Avoid the L2 tables update for the images that have snapshots.
562 if (!s->is_parent) {
49140a77 563 cluster_offset = bdrv_getlength(s->hd);
630530a6
TS
564 bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
565
566 cluster_offset >>= 9;
567 tmp = cpu_to_le32(cluster_offset);
568 l2_table[l2_index] = tmp;
569 // Save the active image state
570 activeBDRV.cluster_offset = cluster_offset;
571 activeBDRV.hd = bs;
ff1afc72 572 }
630530a6
TS
573 /* First of all we write grain itself, to avoid race condition
574 * that may to corrupt the image.
575 * This problem may occur because of insufficient space on host disk
576 * or inappropriate VM shutdown.
577 */
5f4da8c0
TS
578 if (get_whole_cluster(bs, cluster_offset, offset, allocate) == -1)
579 return 0;
630530a6
TS
580
581 if (m_data) {
582 m_data->offset = tmp;
583 m_data->l1_index = l1_index;
584 m_data->l2_index = l2_index;
585 m_data->l2_offset = l2_offset;
586 m_data->valid = 1;
587 }
ff1afc72 588 }
ea2384d3
FB
589 cluster_offset <<= 9;
590 return cluster_offset;
591}
592
5fafdf24 593static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,
ea2384d3
FB
594 int nb_sectors, int *pnum)
595{
596 BDRVVmdkState *s = bs->opaque;
597 int index_in_cluster, n;
598 uint64_t cluster_offset;
599
630530a6 600 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
ea2384d3
FB
601 index_in_cluster = sector_num % s->cluster_sectors;
602 n = s->cluster_sectors - index_in_cluster;
603 if (n > nb_sectors)
604 n = nb_sectors;
605 *pnum = n;
606 return (cluster_offset != 0);
607}
608
5fafdf24 609static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
ea2384d3
FB
610 uint8_t *buf, int nb_sectors)
611{
612 BDRVVmdkState *s = bs->opaque;
5f4da8c0 613 int index_in_cluster, n, ret;
ea2384d3 614 uint64_t cluster_offset;
5f4da8c0 615
ea2384d3 616 while (nb_sectors > 0) {
630530a6 617 cluster_offset = get_cluster_offset(bs, NULL, sector_num << 9, 0);
ea2384d3
FB
618 index_in_cluster = sector_num % s->cluster_sectors;
619 n = s->cluster_sectors - index_in_cluster;
620 if (n > nb_sectors)
621 n = nb_sectors;
622 if (!cluster_offset) {
5f4da8c0
TS
623 // try to read from parent image, if exist
624 if (s->hd->backing_hd) {
625 if (!vmdk_is_cid_valid(bs))
626 return -1;
627 ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
628 if (ret < 0)
629 return -1;
630 } else {
631 memset(buf, 0, 512 * n);
632 }
ea2384d3 633 } else {
5f4da8c0 634 if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
ea2384d3
FB
635 return -1;
636 }
637 nb_sectors -= n;
638 sector_num += n;
639 buf += n * 512;
640 }
641 return 0;
642}
643
5fafdf24 644static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
ea2384d3
FB
645 const uint8_t *buf, int nb_sectors)
646{
ff1afc72 647 BDRVVmdkState *s = bs->opaque;
630530a6 648 VmdkMetaData m_data;
5f4da8c0 649 int index_in_cluster, n;
ff1afc72 650 uint64_t cluster_offset;
5f4da8c0 651 static int cid_update = 0;
ff1afc72 652
630530a6
TS
653 if (sector_num > bs->total_sectors) {
654 fprintf(stderr,
92868412
JM
655 "(VMDK) Wrong offset: sector_num=0x%" PRIx64
656 " total_sectors=0x%" PRIx64 "\n",
630530a6
TS
657 sector_num, bs->total_sectors);
658 return -1;
659 }
660
ff1afc72
FB
661 while (nb_sectors > 0) {
662 index_in_cluster = sector_num & (s->cluster_sectors - 1);
663 n = s->cluster_sectors - index_in_cluster;
664 if (n > nb_sectors)
665 n = nb_sectors;
630530a6 666 cluster_offset = get_cluster_offset(bs, &m_data, sector_num << 9, 1);
ff1afc72
FB
667 if (!cluster_offset)
668 return -1;
630530a6 669
5f4da8c0 670 if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
ff1afc72 671 return -1;
630530a6
TS
672 if (m_data.valid) {
673 /* update L2 tables */
674 if (vmdk_L2update(bs, &m_data) == -1)
675 return -1;
676 }
ff1afc72
FB
677 nb_sectors -= n;
678 sector_num += n;
679 buf += n * 512;
5f4da8c0
TS
680
681 // update CID on the first write every time the virtual disk is opened
682 if (!cid_update) {
683 vmdk_write_cid(bs, time(NULL));
684 cid_update++;
685 }
ff1afc72
FB
686 }
687 return 0;
ea2384d3
FB
688}
689
8979b227
FB
690static int vmdk_create(const char *filename, int64_t total_size,
691 const char *backing_file, int flags)
692{
693 int fd, i;
694 VMDK4Header header;
695 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
7ccfb2eb 696 static const char desc_template[] =
8979b227
FB
697 "# Disk DescriptorFile\n"
698 "version=1\n"
699 "CID=%x\n"
700 "parentCID=ffffffff\n"
701 "createType=\"monolithicSparse\"\n"
702 "\n"
703 "# Extent description\n"
7fd6d9fc 704 "RW %" PRId64 " SPARSE \"%s\"\n"
8979b227
FB
705 "\n"
706 "# The Disk Data Base \n"
707 "#DDB\n"
708 "\n"
ec36ba14 709 "ddb.virtualHWVersion = \"%d\"\n"
7fd6d9fc 710 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
8979b227
FB
711 "ddb.geometry.heads = \"16\"\n"
712 "ddb.geometry.sectors = \"63\"\n"
713 "ddb.adapterType = \"ide\"\n";
714 char desc[1024];
715 const char *real_filename, *temp_str;
716
717 /* XXX: add support for backing file */
5f4da8c0
TS
718 if (backing_file) {
719 return vmdk_snapshot_create(filename, backing_file);
720 }
8979b227
FB
721
722 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
723 0644);
724 if (fd < 0)
725 return -1;
726 magic = cpu_to_be32(VMDK4_MAGIC);
727 memset(&header, 0, sizeof(header));
728 header.version = cpu_to_le32(1);
729 header.flags = cpu_to_le32(3); /* ?? */
730 header.capacity = cpu_to_le64(total_size);
731 header.granularity = cpu_to_le64(128);
732 header.num_gtes_per_gte = cpu_to_le32(512);
733
734 grains = (total_size + header.granularity - 1) / header.granularity;
735 gt_size = ((header.num_gtes_per_gte * sizeof(uint32_t)) + 511) >> 9;
736 gt_count = (grains + header.num_gtes_per_gte - 1) / header.num_gtes_per_gte;
737 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
738
739 header.desc_offset = 1;
740 header.desc_size = 20;
741 header.rgd_offset = header.desc_offset + header.desc_size;
742 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
743 header.grain_offset =
744 ((header.gd_offset + gd_size + (gt_size * gt_count) +
745 header.granularity - 1) / header.granularity) *
746 header.granularity;
747
748 header.desc_offset = cpu_to_le64(header.desc_offset);
749 header.desc_size = cpu_to_le64(header.desc_size);
750 header.rgd_offset = cpu_to_le64(header.rgd_offset);
751 header.gd_offset = cpu_to_le64(header.gd_offset);
752 header.grain_offset = cpu_to_le64(header.grain_offset);
753
754 header.check_bytes[0] = 0xa;
755 header.check_bytes[1] = 0x20;
756 header.check_bytes[2] = 0xd;
757 header.check_bytes[3] = 0xa;
3b46e624
TS
758
759 /* write all the data */
8979b227
FB
760 write(fd, &magic, sizeof(magic));
761 write(fd, &header, sizeof(header));
762
763 ftruncate(fd, header.grain_offset << 9);
764
765 /* write grain directory */
766 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
767 for (i = 0, tmp = header.rgd_offset + gd_size;
768 i < gt_count; i++, tmp += gt_size)
769 write(fd, &tmp, sizeof(tmp));
3b46e624 770
8979b227
FB
771 /* write backup grain directory */
772 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
773 for (i = 0, tmp = header.gd_offset + gd_size;
774 i < gt_count; i++, tmp += gt_size)
775 write(fd, &tmp, sizeof(tmp));
776
777 /* compose the descriptor */
778 real_filename = filename;
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 if ((temp_str = strrchr(real_filename, ':')) != NULL)
784 real_filename = temp_str + 1;
7ccfb2eb 785 snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL),
7fd6d9fc
BS
786 total_size, real_filename,
787 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
788 total_size / (int64_t)(63 * 16));
8979b227
FB
789
790 /* write the descriptor */
791 lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
792 write(fd, desc, strlen(desc));
793
794 close(fd);
795 return 0;
796}
797
e2731add 798static void vmdk_close(BlockDriverState *bs)
ea2384d3
FB
799{
800 BDRVVmdkState *s = bs->opaque;
5f4da8c0 801
ea2384d3
FB
802 qemu_free(s->l1_table);
803 qemu_free(s->l2_cache);
5f4da8c0
TS
804 // try to close parent image, if exist
805 vmdk_parent_close(s->hd);
5cbdd273 806 bdrv_delete(s->hd);
ea2384d3
FB
807}
808
7a6cba61
PB
809static void vmdk_flush(BlockDriverState *bs)
810{
811 BDRVVmdkState *s = bs->opaque;
5f4da8c0 812 bdrv_flush(s->hd);
7a6cba61
PB
813}
814
5efa9d5a 815static BlockDriver bdrv_vmdk = {
e60f469c
AJ
816 .format_name = "vmdk",
817 .instance_size = sizeof(BDRVVmdkState),
818 .bdrv_probe = vmdk_probe,
819 .bdrv_open = vmdk_open,
820 .bdrv_read = vmdk_read,
821 .bdrv_write = vmdk_write,
822 .bdrv_close = vmdk_close,
823 .bdrv_create = vmdk_create,
824 .bdrv_flush = vmdk_flush,
825 .bdrv_is_allocated = vmdk_is_allocated,
ea2384d3 826};
5efa9d5a
AL
827
828static void bdrv_vmdk_init(void)
829{
830 bdrv_register(&bdrv_vmdk);
831}
832
833block_init(bdrv_vmdk_init);