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