]> git.proxmox.com Git - mirror_qemu.git/blame - block/vmdk.c
block/iscsi: set bdi->cluster_size
[mirror_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"
737e150e 27#include "block/block_int.h"
1de7afc9 28#include "qemu/module.h"
caf71f86 29#include "migration/migration.h"
2923d34f 30#include <zlib.h>
ea2384d3 31
ea2384d3
FB
32#define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
33#define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
432bb170 34#define VMDK4_COMPRESSION_DEFLATE 1
95b0aa42 35#define VMDK4_FLAG_NL_DETECT (1 << 0)
bb45ded9 36#define VMDK4_FLAG_RGD (1 << 1)
14ead646
FZ
37/* Zeroed-grain enable bit */
38#define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
432bb170
FZ
39#define VMDK4_FLAG_COMPRESS (1 << 16)
40#define VMDK4_FLAG_MARKER (1 << 17)
65bd155c 41#define VMDK4_GD_AT_END 0xffffffffffffffffULL
ea2384d3 42
14ead646 43#define VMDK_GTE_ZEROED 0x1
65f74725
FZ
44
45/* VMDK internal error codes */
46#define VMDK_OK 0
47#define VMDK_ERROR (-1)
48/* Cluster not allocated */
49#define VMDK_UNALLOC (-2)
50#define VMDK_ZEROED (-3)
51
69e0b6df
FZ
52#define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
53
ea2384d3
FB
54typedef struct {
55 uint32_t version;
56 uint32_t flags;
57 uint32_t disk_sectors;
58 uint32_t granularity;
59 uint32_t l1dir_offset;
60 uint32_t l1dir_size;
61 uint32_t file_sectors;
62 uint32_t cylinders;
63 uint32_t heads;
64 uint32_t sectors_per_track;
5d8caa54 65} QEMU_PACKED VMDK3Header;
ea2384d3
FB
66
67typedef struct {
68 uint32_t version;
69 uint32_t flags;
e98768d4
FZ
70 uint64_t capacity;
71 uint64_t granularity;
72 uint64_t desc_offset;
73 uint64_t desc_size;
ca8804ce
FZ
74 /* Number of GrainTableEntries per GrainTable */
75 uint32_t num_gtes_per_gt;
e98768d4
FZ
76 uint64_t rgd_offset;
77 uint64_t gd_offset;
78 uint64_t grain_offset;
ea2384d3
FB
79 char filler[1];
80 char check_bytes[4];
432bb170 81 uint16_t compressAlgorithm;
541dc0d4 82} QEMU_PACKED VMDK4Header;
ea2384d3
FB
83
84#define L2_CACHE_SIZE 16
85
b3976d3c
FZ
86typedef struct VmdkExtent {
87 BlockDriverState *file;
88 bool flat;
432bb170
FZ
89 bool compressed;
90 bool has_marker;
14ead646
FZ
91 bool has_zero_grain;
92 int version;
b3976d3c
FZ
93 int64_t sectors;
94 int64_t end_sector;
7fa60fa3 95 int64_t flat_start_offset;
ea2384d3 96 int64_t l1_table_offset;
ff1afc72 97 int64_t l1_backup_table_offset;
ea2384d3 98 uint32_t *l1_table;
ff1afc72 99 uint32_t *l1_backup_table;
ea2384d3
FB
100 unsigned int l1_size;
101 uint32_t l1_entry_sectors;
102
103 unsigned int l2_size;
104 uint32_t *l2_cache;
105 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
106 uint32_t l2_cache_counts[L2_CACHE_SIZE];
107
301c7d38 108 int64_t cluster_sectors;
f4c129a3 109 char *type;
b3976d3c
FZ
110} VmdkExtent;
111
112typedef struct BDRVVmdkState {
848c66e8 113 CoMutex lock;
e98768d4 114 uint64_t desc_offset;
69b4d86d 115 bool cid_updated;
c338b6ad 116 bool cid_checked;
f4c129a3 117 uint32_t cid;
5f4da8c0 118 uint32_t parent_cid;
b3976d3c
FZ
119 int num_extents;
120 /* Extent array with num_extents entries, ascend ordered by address */
121 VmdkExtent *extents;
2bc3166c 122 Error *migration_blocker;
f4c129a3 123 char *create_type;
ea2384d3
FB
124} BDRVVmdkState;
125
630530a6
TS
126typedef struct VmdkMetaData {
127 uint32_t offset;
128 unsigned int l1_index;
129 unsigned int l2_index;
130 unsigned int l2_offset;
131 int valid;
cdeaf1f1 132 uint32_t *l2_cache_entry;
630530a6
TS
133} VmdkMetaData;
134
432bb170
FZ
135typedef struct VmdkGrainMarker {
136 uint64_t lba;
137 uint32_t size;
138 uint8_t data[0];
5d8caa54 139} QEMU_PACKED VmdkGrainMarker;
432bb170 140
65bd155c
KW
141enum {
142 MARKER_END_OF_STREAM = 0,
143 MARKER_GRAIN_TABLE = 1,
144 MARKER_GRAIN_DIRECTORY = 2,
145 MARKER_FOOTER = 3,
146};
147
ea2384d3
FB
148static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
149{
150 uint32_t magic;
151
ae261c86 152 if (buf_size < 4) {
ea2384d3 153 return 0;
ae261c86 154 }
ea2384d3
FB
155 magic = be32_to_cpu(*(uint32_t *)buf);
156 if (magic == VMDK3_MAGIC ||
01fc99d6 157 magic == VMDK4_MAGIC) {
ea2384d3 158 return 100;
01fc99d6
FZ
159 } else {
160 const char *p = (const char *)buf;
161 const char *end = p + buf_size;
162 while (p < end) {
163 if (*p == '#') {
164 /* skip comment line */
165 while (p < end && *p != '\n') {
166 p++;
167 }
168 p++;
169 continue;
170 }
171 if (*p == ' ') {
172 while (p < end && *p == ' ') {
173 p++;
174 }
175 /* skip '\r' if windows line endings used. */
176 if (p < end && *p == '\r') {
177 p++;
178 }
179 /* only accept blank lines before 'version=' line */
180 if (p == end || *p != '\n') {
181 return 0;
182 }
183 p++;
184 continue;
185 }
186 if (end - p >= strlen("version=X\n")) {
187 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
188 strncmp("version=2\n", p, strlen("version=2\n")) == 0) {
189 return 100;
190 }
191 }
192 if (end - p >= strlen("version=X\r\n")) {
193 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
194 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0) {
195 return 100;
196 }
197 }
198 return 0;
199 }
ea2384d3 200 return 0;
01fc99d6 201 }
ea2384d3
FB
202}
203
3b46e624 204#define SECTOR_SIZE 512
f66fd6c3
FZ
205#define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
206#define BUF_SIZE 4096
207#define HEADER_SIZE 512 /* first sector of 512 bytes */
5f4da8c0 208
b3976d3c
FZ
209static void vmdk_free_extents(BlockDriverState *bs)
210{
211 int i;
212 BDRVVmdkState *s = bs->opaque;
b3c0bfb6 213 VmdkExtent *e;
b3976d3c
FZ
214
215 for (i = 0; i < s->num_extents; i++) {
b3c0bfb6
FZ
216 e = &s->extents[i];
217 g_free(e->l1_table);
218 g_free(e->l2_cache);
219 g_free(e->l1_backup_table);
f4c129a3 220 g_free(e->type);
b3c0bfb6 221 if (e->file != bs->file) {
4f6fd349 222 bdrv_unref(e->file);
b3c0bfb6 223 }
b3976d3c 224 }
7267c094 225 g_free(s->extents);
b3976d3c
FZ
226}
227
86c6b429
FZ
228static void vmdk_free_last_extent(BlockDriverState *bs)
229{
230 BDRVVmdkState *s = bs->opaque;
231
232 if (s->num_extents == 0) {
233 return;
234 }
235 s->num_extents--;
236 s->extents = g_realloc(s->extents, s->num_extents * sizeof(VmdkExtent));
237}
238
5f4da8c0 239static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
ea2384d3 240{
5f4da8c0 241 char desc[DESC_SIZE];
8379e46d 242 uint32_t cid = 0xffffffff;
7ccfb2eb 243 const char *p_name, *cid_str;
5f4da8c0 244 size_t cid_str_size;
e1da9b24 245 BDRVVmdkState *s = bs->opaque;
99f1835d 246 int ret;
5f4da8c0 247
99f1835d
KW
248 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
249 if (ret < 0) {
5f4da8c0 250 return 0;
e1da9b24 251 }
5f4da8c0
TS
252
253 if (parent) {
254 cid_str = "parentCID";
255 cid_str_size = sizeof("parentCID");
256 } else {
257 cid_str = "CID";
258 cid_str_size = sizeof("CID");
259 }
260
93897b9f 261 desc[DESC_SIZE - 1] = '\0';
ae261c86
FZ
262 p_name = strstr(desc, cid_str);
263 if (p_name != NULL) {
5f4da8c0 264 p_name += cid_str_size;
ae261c86 265 sscanf(p_name, "%x", &cid);
5f4da8c0
TS
266 }
267
268 return cid;
269}
270
271static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
272{
5f4da8c0
TS
273 char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
274 char *p_name, *tmp_str;
e1da9b24 275 BDRVVmdkState *s = bs->opaque;
99f1835d 276 int ret;
5f4da8c0 277
99f1835d
KW
278 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
279 if (ret < 0) {
280 return ret;
e1da9b24 281 }
5f4da8c0 282
93897b9f 283 desc[DESC_SIZE - 1] = '\0';
ae261c86 284 tmp_str = strstr(desc, "parentCID");
93897b9f
KW
285 if (tmp_str == NULL) {
286 return -EINVAL;
287 }
288
363a37d5 289 pstrcpy(tmp_desc, sizeof(tmp_desc), tmp_str);
ae261c86
FZ
290 p_name = strstr(desc, "CID");
291 if (p_name != NULL) {
5f4da8c0 292 p_name += sizeof("CID");
363a37d5
BS
293 snprintf(p_name, sizeof(desc) - (p_name - desc), "%x\n", cid);
294 pstrcat(desc, sizeof(desc), tmp_desc);
5f4da8c0
TS
295 }
296
99f1835d
KW
297 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
298 if (ret < 0) {
299 return ret;
e1da9b24 300 }
99f1835d 301
5f4da8c0
TS
302 return 0;
303}
304
305static int vmdk_is_cid_valid(BlockDriverState *bs)
306{
5f4da8c0 307 BDRVVmdkState *s = bs->opaque;
b171271a 308 BlockDriverState *p_bs = bs->backing_hd;
5f4da8c0
TS
309 uint32_t cur_pcid;
310
c338b6ad 311 if (!s->cid_checked && p_bs) {
ae261c86
FZ
312 cur_pcid = vmdk_read_cid(p_bs, 0);
313 if (s->parent_cid != cur_pcid) {
314 /* CID not valid */
5f4da8c0 315 return 0;
ae261c86 316 }
5f4da8c0 317 }
c338b6ad 318 s->cid_checked = true;
ae261c86 319 /* CID valid */
5f4da8c0
TS
320 return 1;
321}
322
3897575f
JC
323/* Queue extents, if any, for reopen() */
324static int vmdk_reopen_prepare(BDRVReopenState *state,
325 BlockReopenQueue *queue, Error **errp)
326{
327 BDRVVmdkState *s;
328 int ret = -1;
329 int i;
330 VmdkExtent *e;
331
332 assert(state != NULL);
333 assert(state->bs != NULL);
334
335 if (queue == NULL) {
4823970b 336 error_setg(errp, "No reopen queue for VMDK extents");
3897575f
JC
337 goto exit;
338 }
339
340 s = state->bs->opaque;
341
342 assert(s != NULL);
343
344 for (i = 0; i < s->num_extents; i++) {
345 e = &s->extents[i];
346 if (e->file != state->bs->file) {
347 bdrv_reopen_queue(queue, e->file, state->flags);
348 }
349 }
350 ret = 0;
351
352exit:
353 return ret;
354}
355
9949f97e 356static int vmdk_parent_open(BlockDriverState *bs)
5f4da8c0 357{
5fafdf24 358 char *p_name;
7fa60fa3 359 char desc[DESC_SIZE + 1];
e1da9b24 360 BDRVVmdkState *s = bs->opaque;
588b65a3 361 int ret;
5f4da8c0 362
7fa60fa3 363 desc[DESC_SIZE] = '\0';
588b65a3
PB
364 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
365 if (ret < 0) {
366 return ret;
e1da9b24 367 }
5f4da8c0 368
ae261c86
FZ
369 p_name = strstr(desc, "parentFileNameHint");
370 if (p_name != NULL) {
5f4da8c0 371 char *end_name;
5f4da8c0
TS
372
373 p_name += sizeof("parentFileNameHint") + 1;
ae261c86
FZ
374 end_name = strchr(p_name, '\"');
375 if (end_name == NULL) {
588b65a3 376 return -EINVAL;
ae261c86
FZ
377 }
378 if ((end_name - p_name) > sizeof(bs->backing_file) - 1) {
588b65a3 379 return -EINVAL;
ae261c86 380 }
3b46e624 381
b171271a 382 pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
ff1afc72 383 }
5f4da8c0
TS
384
385 return 0;
386}
387
b3976d3c
FZ
388/* Create and append extent to the extent array. Return the added VmdkExtent
389 * address. return NULL if allocation failed. */
8aa1331c 390static int vmdk_add_extent(BlockDriverState *bs,
b3976d3c
FZ
391 BlockDriverState *file, bool flat, int64_t sectors,
392 int64_t l1_offset, int64_t l1_backup_offset,
393 uint32_t l1_size,
8aa1331c 394 int l2_size, uint64_t cluster_sectors,
4823970b
FZ
395 VmdkExtent **new_extent,
396 Error **errp)
b3976d3c
FZ
397{
398 VmdkExtent *extent;
399 BDRVVmdkState *s = bs->opaque;
400
8aa1331c
FZ
401 if (cluster_sectors > 0x200000) {
402 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
4823970b
FZ
403 error_setg(errp, "Invalid granularity, image may be corrupt");
404 return -EFBIG;
8aa1331c 405 }
b0651b8c
FZ
406 if (l1_size > 512 * 1024 * 1024) {
407 /* Although with big capacity and small l1_entry_sectors, we can get a
408 * big l1_size, we don't want unbounded value to allocate the table.
409 * Limit it to 512M, which is 16PB for default cluster and L2 table
410 * size */
4823970b 411 error_setg(errp, "L1 size too big");
b0651b8c
FZ
412 return -EFBIG;
413 }
8aa1331c 414
7267c094 415 s->extents = g_realloc(s->extents,
b3976d3c
FZ
416 (s->num_extents + 1) * sizeof(VmdkExtent));
417 extent = &s->extents[s->num_extents];
418 s->num_extents++;
419
420 memset(extent, 0, sizeof(VmdkExtent));
421 extent->file = file;
422 extent->flat = flat;
423 extent->sectors = sectors;
424 extent->l1_table_offset = l1_offset;
425 extent->l1_backup_table_offset = l1_backup_offset;
426 extent->l1_size = l1_size;
427 extent->l1_entry_sectors = l2_size * cluster_sectors;
428 extent->l2_size = l2_size;
301c7d38 429 extent->cluster_sectors = flat ? sectors : cluster_sectors;
b3976d3c 430
cffb1ec6
PB
431 if (!flat) {
432 bs->bl.write_zeroes_alignment =
433 MAX(bs->bl.write_zeroes_alignment, cluster_sectors);
434 }
b3976d3c
FZ
435 if (s->num_extents > 1) {
436 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
437 } else {
438 extent->end_sector = extent->sectors;
439 }
440 bs->total_sectors = extent->end_sector;
8aa1331c
FZ
441 if (new_extent) {
442 *new_extent = extent;
443 }
444 return 0;
b3976d3c
FZ
445}
446
4823970b
FZ
447static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
448 Error **errp)
5f4da8c0 449{
b4b3ab14
FZ
450 int ret;
451 int l1_size, i;
5f4da8c0 452
ea2384d3 453 /* read the L1 table */
b3976d3c 454 l1_size = extent->l1_size * sizeof(uint32_t);
7267c094 455 extent->l1_table = g_malloc(l1_size);
b4b3ab14 456 ret = bdrv_pread(extent->file,
4823970b
FZ
457 extent->l1_table_offset,
458 extent->l1_table,
459 l1_size);
b4b3ab14 460 if (ret < 0) {
4823970b
FZ
461 error_setg_errno(errp, -ret,
462 "Could not read l1 table from extent '%s'",
463 extent->file->filename);
b4b3ab14 464 goto fail_l1;
b3976d3c
FZ
465 }
466 for (i = 0; i < extent->l1_size; i++) {
467 le32_to_cpus(&extent->l1_table[i]);
ea2384d3
FB
468 }
469
b3976d3c 470 if (extent->l1_backup_table_offset) {
7267c094 471 extent->l1_backup_table = g_malloc(l1_size);
b4b3ab14 472 ret = bdrv_pread(extent->file,
4823970b
FZ
473 extent->l1_backup_table_offset,
474 extent->l1_backup_table,
475 l1_size);
b4b3ab14 476 if (ret < 0) {
4823970b
FZ
477 error_setg_errno(errp, -ret,
478 "Could not read l1 backup table from extent '%s'",
479 extent->file->filename);
b4b3ab14 480 goto fail_l1b;
b3976d3c
FZ
481 }
482 for (i = 0; i < extent->l1_size; i++) {
483 le32_to_cpus(&extent->l1_backup_table[i]);
ff1afc72
FB
484 }
485 }
486
b3976d3c 487 extent->l2_cache =
7267c094 488 g_malloc(extent->l2_size * L2_CACHE_SIZE * sizeof(uint32_t));
ea2384d3 489 return 0;
b4b3ab14 490 fail_l1b:
7267c094 491 g_free(extent->l1_backup_table);
b4b3ab14 492 fail_l1:
7267c094 493 g_free(extent->l1_table);
b4b3ab14
FZ
494 return ret;
495}
496
daac8fdc
FZ
497static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
498 BlockDriverState *file,
4823970b 499 int flags, Error **errp)
b4b3ab14
FZ
500{
501 int ret;
502 uint32_t magic;
503 VMDK3Header header;
504 VmdkExtent *extent;
505
86c6b429 506 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
b4b3ab14 507 if (ret < 0) {
4823970b
FZ
508 error_setg_errno(errp, -ret,
509 "Could not read header from file '%s'",
510 file->filename);
86c6b429 511 return ret;
b4b3ab14 512 }
f6b61e54
FZ
513 ret = vmdk_add_extent(bs, file, false,
514 le32_to_cpu(header.disk_sectors),
515 le32_to_cpu(header.l1dir_offset) << 9,
516 0,
517 le32_to_cpu(header.l1dir_size),
518 4096,
519 le32_to_cpu(header.granularity),
4823970b
FZ
520 &extent,
521 errp);
8aa1331c
FZ
522 if (ret < 0) {
523 return ret;
524 }
4823970b 525 ret = vmdk_init_tables(bs, extent, errp);
b4b3ab14 526 if (ret) {
86c6b429
FZ
527 /* free extent allocated by vmdk_add_extent */
528 vmdk_free_last_extent(bs);
b4b3ab14 529 }
b4b3ab14
FZ
530 return ret;
531}
532
f16f509d 533static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
4823970b 534 uint64_t desc_offset, Error **errp);
f16f509d 535
86c6b429
FZ
536static int vmdk_open_vmdk4(BlockDriverState *bs,
537 BlockDriverState *file,
4823970b 538 int flags, Error **errp)
b4b3ab14
FZ
539{
540 int ret;
541 uint32_t magic;
542 uint32_t l1_size, l1_entry_sectors;
543 VMDK4Header header;
b4b3ab14 544 VmdkExtent *extent;
f4c129a3 545 BDRVVmdkState *s = bs->opaque;
bb45ded9 546 int64_t l1_backup_offset = 0;
b4b3ab14 547
86c6b429 548 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
b4b3ab14 549 if (ret < 0) {
4823970b
FZ
550 error_setg_errno(errp, -ret,
551 "Could not read header from file '%s'",
552 file->filename);
b4b3ab14 553 }
5a394b9e 554 if (header.capacity == 0) {
e98768d4 555 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
5a394b9e 556 if (desc_offset) {
4823970b 557 return vmdk_open_desc_file(bs, flags, desc_offset << 9, errp);
5a394b9e 558 }
f16f509d 559 }
65bd155c 560
f4c129a3
FZ
561 if (!s->create_type) {
562 s->create_type = g_strdup("monolithicSparse");
563 }
564
65bd155c
KW
565 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
566 /*
567 * The footer takes precedence over the header, so read it in. The
568 * footer starts at offset -1024 from the end: One sector for the
569 * footer, and another one for the end-of-stream marker.
570 */
571 struct {
572 struct {
573 uint64_t val;
574 uint32_t size;
575 uint32_t type;
576 uint8_t pad[512 - 16];
577 } QEMU_PACKED footer_marker;
578
579 uint32_t magic;
580 VMDK4Header header;
581 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
582
583 struct {
584 uint64_t val;
585 uint32_t size;
586 uint32_t type;
587 uint8_t pad[512 - 16];
588 } QEMU_PACKED eos_marker;
589 } QEMU_PACKED footer;
590
591 ret = bdrv_pread(file,
592 bs->file->total_sectors * 512 - 1536,
593 &footer, sizeof(footer));
594 if (ret < 0) {
595 return ret;
596 }
597
598 /* Some sanity checks for the footer */
599 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
600 le32_to_cpu(footer.footer_marker.size) != 0 ||
601 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
602 le64_to_cpu(footer.eos_marker.val) != 0 ||
603 le32_to_cpu(footer.eos_marker.size) != 0 ||
604 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
605 {
606 return -EINVAL;
607 }
608
609 header = footer.header;
610 }
611
509d39aa 612 if (le32_to_cpu(header.version) > 3) {
96c51eb5
FZ
613 char buf[64];
614 snprintf(buf, sizeof(buf), "VMDK version %d",
615 le32_to_cpu(header.version));
616 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
617 bs->device_name, "vmdk", buf);
618 return -ENOTSUP;
509d39aa
FZ
619 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
620 /* VMware KB 2064959 explains that version 3 added support for
621 * persistent changed block tracking (CBT), and backup software can
622 * read it as version=1 if it doesn't care about the changed area
623 * information. So we are safe to enable read only. */
624 error_setg(errp, "VMDK version 3 must be read only");
625 return -EINVAL;
96c51eb5
FZ
626 }
627
ca8804ce 628 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
f8ce0403
FZ
629 error_report("L2 table size too big");
630 return -EINVAL;
631 }
632
ca8804ce 633 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
b4b3ab14 634 * le64_to_cpu(header.granularity);
75d12341 635 if (l1_entry_sectors == 0) {
86c6b429
FZ
636 return -EINVAL;
637 }
b4b3ab14
FZ
638 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
639 / l1_entry_sectors;
bb45ded9
FZ
640 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
641 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
642 }
8aa1331c 643 ret = vmdk_add_extent(bs, file, false,
b4b3ab14
FZ
644 le64_to_cpu(header.capacity),
645 le64_to_cpu(header.gd_offset) << 9,
bb45ded9 646 l1_backup_offset,
b4b3ab14 647 l1_size,
ca8804ce 648 le32_to_cpu(header.num_gtes_per_gt),
8aa1331c 649 le64_to_cpu(header.granularity),
4823970b
FZ
650 &extent,
651 errp);
8aa1331c
FZ
652 if (ret < 0) {
653 return ret;
654 }
432bb170
FZ
655 extent->compressed =
656 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
657 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
14ead646
FZ
658 extent->version = le32_to_cpu(header.version);
659 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
4823970b 660 ret = vmdk_init_tables(bs, extent, errp);
b4b3ab14 661 if (ret) {
86c6b429
FZ
662 /* free extent allocated by vmdk_add_extent */
663 vmdk_free_last_extent(bs);
b4b3ab14 664 }
b4b3ab14
FZ
665 return ret;
666}
667
7fa60fa3
FZ
668/* find an option value out of descriptor file */
669static int vmdk_parse_description(const char *desc, const char *opt_name,
670 char *buf, int buf_size)
671{
672 char *opt_pos, *opt_end;
673 const char *end = desc + strlen(desc);
674
675 opt_pos = strstr(desc, opt_name);
676 if (!opt_pos) {
65f74725 677 return VMDK_ERROR;
7fa60fa3
FZ
678 }
679 /* Skip "=\"" following opt_name */
680 opt_pos += strlen(opt_name) + 2;
681 if (opt_pos >= end) {
65f74725 682 return VMDK_ERROR;
7fa60fa3
FZ
683 }
684 opt_end = opt_pos;
685 while (opt_end < end && *opt_end != '"') {
686 opt_end++;
687 }
688 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
65f74725 689 return VMDK_ERROR;
7fa60fa3
FZ
690 }
691 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
65f74725 692 return VMDK_OK;
7fa60fa3
FZ
693}
694
86c6b429
FZ
695/* Open an extent file and append to bs array */
696static int vmdk_open_sparse(BlockDriverState *bs,
697 BlockDriverState *file,
4823970b 698 int flags, Error **errp)
86c6b429
FZ
699{
700 uint32_t magic;
701
702 if (bdrv_pread(file, 0, &magic, sizeof(magic)) != sizeof(magic)) {
703 return -EIO;
704 }
705
706 magic = be32_to_cpu(magic);
707 switch (magic) {
708 case VMDK3_MAGIC:
4823970b 709 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
86c6b429
FZ
710 break;
711 case VMDK4_MAGIC:
4823970b 712 return vmdk_open_vmdk4(bs, file, flags, errp);
86c6b429
FZ
713 break;
714 default:
15bac0d5 715 return -EMEDIUMTYPE;
86c6b429
FZ
716 break;
717 }
718}
719
7fa60fa3 720static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
4823970b 721 const char *desc_file_path, Error **errp)
7fa60fa3
FZ
722{
723 int ret;
724 char access[11];
725 char type[11];
726 char fname[512];
727 const char *p = desc;
728 int64_t sectors = 0;
729 int64_t flat_offset;
86c6b429
FZ
730 char extent_path[PATH_MAX];
731 BlockDriverState *extent_file;
f4c129a3
FZ
732 BDRVVmdkState *s = bs->opaque;
733 VmdkExtent *extent;
7fa60fa3
FZ
734
735 while (*p) {
736 /* parse extent line:
737 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
738 * or
739 * RW [size in sectors] SPARSE "file-name.vmdk"
740 */
741 flat_offset = -1;
cd923475 742 ret = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
7fa60fa3
FZ
743 access, &sectors, type, fname, &flat_offset);
744 if (ret < 4 || strcmp(access, "RW")) {
745 goto next_line;
746 } else if (!strcmp(type, "FLAT")) {
747 if (ret != 5 || flat_offset < 0) {
4823970b 748 error_setg(errp, "Invalid extent lines: \n%s", p);
7fa60fa3
FZ
749 return -EINVAL;
750 }
dbbcaa8d
FZ
751 } else if (!strcmp(type, "VMFS")) {
752 flat_offset = 0;
7fa60fa3 753 } else if (ret != 4) {
4823970b 754 error_setg(errp, "Invalid extent lines: \n%s", p);
7fa60fa3
FZ
755 return -EINVAL;
756 }
757
7fa60fa3 758 if (sectors <= 0 ||
daac8fdc 759 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
04d542c8 760 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
7fa60fa3
FZ
761 (strcmp(access, "RW"))) {
762 goto next_line;
763 }
764
86c6b429
FZ
765 path_combine(extent_path, sizeof(extent_path),
766 desc_file_path, fname);
34b5d2c6 767 ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
4823970b 768 errp);
86c6b429
FZ
769 if (ret) {
770 return ret;
771 }
772
7fa60fa3 773 /* save to extents array */
04d542c8 774 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
7fa60fa3 775 /* FLAT extent */
7fa60fa3 776
8aa1331c 777 ret = vmdk_add_extent(bs, extent_file, true, sectors,
4823970b 778 0, 0, 0, 0, 0, &extent, errp);
8aa1331c
FZ
779 if (ret < 0) {
780 return ret;
781 }
f16f509d 782 extent->flat_start_offset = flat_offset << 9;
daac8fdc
FZ
783 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
784 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
4823970b 785 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, errp);
86c6b429 786 if (ret) {
4f6fd349 787 bdrv_unref(extent_file);
86c6b429
FZ
788 return ret;
789 }
f4c129a3 790 extent = &s->extents[s->num_extents - 1];
7fa60fa3 791 } else {
4823970b 792 error_setg(errp, "Unsupported extent type '%s'", type);
7fa60fa3
FZ
793 return -ENOTSUP;
794 }
f4c129a3 795 extent->type = g_strdup(type);
7fa60fa3
FZ
796next_line:
797 /* move to next line */
899f1ae2
FZ
798 while (*p) {
799 if (*p == '\n') {
800 p++;
801 break;
802 }
7fa60fa3
FZ
803 p++;
804 }
7fa60fa3
FZ
805 }
806 return 0;
807}
808
f16f509d 809static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
4823970b 810 uint64_t desc_offset, Error **errp)
7fa60fa3
FZ
811{
812 int ret;
0bed087d 813 char *buf = NULL;
7fa60fa3
FZ
814 char ct[128];
815 BDRVVmdkState *s = bs->opaque;
0bed087d 816 int64_t size;
7fa60fa3 817
0bed087d
EB
818 size = bdrv_getlength(bs->file);
819 if (size < 0) {
820 return -EINVAL;
821 }
822
823 size = MIN(size, 1 << 20); /* avoid unbounded allocation */
824 buf = g_malloc0(size + 1);
825
826 ret = bdrv_pread(bs->file, desc_offset, buf, size);
7fa60fa3 827 if (ret < 0) {
0bed087d 828 goto exit;
7fa60fa3 829 }
7fa60fa3 830 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
0bed087d
EB
831 ret = -EMEDIUMTYPE;
832 goto exit;
7fa60fa3 833 }
6398de51 834 if (strcmp(ct, "monolithicFlat") &&
04d542c8 835 strcmp(ct, "vmfs") &&
daac8fdc 836 strcmp(ct, "vmfsSparse") &&
86c6b429 837 strcmp(ct, "twoGbMaxExtentSparse") &&
6398de51 838 strcmp(ct, "twoGbMaxExtentFlat")) {
4823970b 839 error_setg(errp, "Unsupported image type '%s'", ct);
0bed087d
EB
840 ret = -ENOTSUP;
841 goto exit;
7fa60fa3 842 }
f4c129a3 843 s->create_type = g_strdup(ct);
7fa60fa3 844 s->desc_offset = 0;
4823970b 845 ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
0bed087d
EB
846exit:
847 g_free(buf);
848 return ret;
7fa60fa3
FZ
849}
850
015a1036
HR
851static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
852 Error **errp)
b4b3ab14 853{
86c6b429
FZ
854 int ret;
855 BDRVVmdkState *s = bs->opaque;
b4b3ab14 856
4823970b 857 if (vmdk_open_sparse(bs, bs->file, flags, errp) == 0) {
86c6b429 858 s->desc_offset = 0x200;
bae0a0cc 859 } else {
4823970b 860 ret = vmdk_open_desc_file(bs, flags, 0, errp);
86c6b429 861 if (ret) {
bae0a0cc 862 goto fail;
86c6b429 863 }
b4b3ab14 864 }
bae0a0cc
PB
865 /* try to open parent images, if exist */
866 ret = vmdk_parent_open(bs);
867 if (ret) {
868 goto fail;
869 }
f4c129a3 870 s->cid = vmdk_read_cid(bs, 0);
bae0a0cc 871 s->parent_cid = vmdk_read_cid(bs, 1);
848c66e8 872 qemu_co_mutex_init(&s->lock);
2bc3166c
KW
873
874 /* Disable migration when VMDK images are used */
875 error_set(&s->migration_blocker,
876 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
877 "vmdk", bs->device_name, "live migration");
878 migrate_add_blocker(s->migration_blocker);
879
880 return 0;
bae0a0cc
PB
881
882fail:
f4c129a3
FZ
883 g_free(s->create_type);
884 s->create_type = NULL;
bae0a0cc
PB
885 vmdk_free_extents(bs);
886 return ret;
ea2384d3
FB
887}
888
b3976d3c
FZ
889static int get_whole_cluster(BlockDriverState *bs,
890 VmdkExtent *extent,
891 uint64_t cluster_offset,
892 uint64_t offset,
893 bool allocate)
5f4da8c0 894{
bf81507d
FZ
895 int ret = VMDK_OK;
896 uint8_t *whole_grain = NULL;
5f4da8c0 897
0e69c543
FZ
898 /* we will be here if it's first write on non-exist grain(cluster).
899 * try to read from parent image, if exist */
b171271a 900 if (bs->backing_hd) {
bf81507d
FZ
901 whole_grain =
902 qemu_blockalign(bs, extent->cluster_sectors << BDRV_SECTOR_BITS);
ae261c86 903 if (!vmdk_is_cid_valid(bs)) {
bf81507d
FZ
904 ret = VMDK_ERROR;
905 goto exit;
ae261c86 906 }
5f4da8c0 907
0e69c543
FZ
908 /* floor offset to cluster */
909 offset -= offset % (extent->cluster_sectors * 512);
c336500d 910 ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
b3976d3c 911 extent->cluster_sectors);
c336500d 912 if (ret < 0) {
bf81507d
FZ
913 ret = VMDK_ERROR;
914 goto exit;
c336500d 915 }
630530a6 916
0e69c543 917 /* Write grain only into the active image */
b3976d3c
FZ
918 ret = bdrv_write(extent->file, cluster_offset, whole_grain,
919 extent->cluster_sectors);
c336500d 920 if (ret < 0) {
bf81507d
FZ
921 ret = VMDK_ERROR;
922 goto exit;
630530a6
TS
923 }
924 }
bf81507d
FZ
925exit:
926 qemu_vfree(whole_grain);
927 return ret;
630530a6
TS
928}
929
b3976d3c 930static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
630530a6 931{
e304e8e5
FZ
932 uint32_t offset;
933 QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
934 offset = cpu_to_le32(m_data->offset);
630530a6 935 /* update L2 table */
b3976d3c
FZ
936 if (bdrv_pwrite_sync(
937 extent->file,
938 ((int64_t)m_data->l2_offset * 512)
939 + (m_data->l2_index * sizeof(m_data->offset)),
e304e8e5 940 &offset, sizeof(offset)) < 0) {
65f74725 941 return VMDK_ERROR;
b3976d3c 942 }
630530a6 943 /* update backup L2 table */
b3976d3c
FZ
944 if (extent->l1_backup_table_offset != 0) {
945 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
946 if (bdrv_pwrite_sync(
947 extent->file,
948 ((int64_t)m_data->l2_offset * 512)
949 + (m_data->l2_index * sizeof(m_data->offset)),
e304e8e5 950 &offset, sizeof(offset)) < 0) {
65f74725 951 return VMDK_ERROR;
b3976d3c 952 }
5f4da8c0 953 }
cdeaf1f1
FZ
954 if (m_data->l2_cache_entry) {
955 *m_data->l2_cache_entry = offset;
956 }
630530a6 957
65f74725 958 return VMDK_OK;
5f4da8c0
TS
959}
960
91b85bd3 961static int get_cluster_offset(BlockDriverState *bs,
b3976d3c
FZ
962 VmdkExtent *extent,
963 VmdkMetaData *m_data,
91b85bd3
FZ
964 uint64_t offset,
965 int allocate,
966 uint64_t *cluster_offset)
ea2384d3 967{
ea2384d3
FB
968 unsigned int l1_index, l2_offset, l2_index;
969 int min_index, i, j;
e304e8e5 970 uint32_t min_count, *l2_table;
14ead646 971 bool zeroed = false;
630530a6 972
ae261c86 973 if (m_data) {
630530a6 974 m_data->valid = 0;
ae261c86 975 }
91b85bd3 976 if (extent->flat) {
7fa60fa3 977 *cluster_offset = extent->flat_start_offset;
65f74725 978 return VMDK_OK;
91b85bd3 979 }
630530a6 980
6398de51 981 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
b3976d3c
FZ
982 l1_index = (offset >> 9) / extent->l1_entry_sectors;
983 if (l1_index >= extent->l1_size) {
65f74725 984 return VMDK_ERROR;
b3976d3c
FZ
985 }
986 l2_offset = extent->l1_table[l1_index];
987 if (!l2_offset) {
65f74725 988 return VMDK_UNALLOC;
b3976d3c 989 }
b4b3ab14 990 for (i = 0; i < L2_CACHE_SIZE; i++) {
b3976d3c 991 if (l2_offset == extent->l2_cache_offsets[i]) {
ea2384d3 992 /* increment the hit count */
b3976d3c 993 if (++extent->l2_cache_counts[i] == 0xffffffff) {
b4b3ab14 994 for (j = 0; j < L2_CACHE_SIZE; j++) {
b3976d3c 995 extent->l2_cache_counts[j] >>= 1;
ea2384d3
FB
996 }
997 }
b3976d3c 998 l2_table = extent->l2_cache + (i * extent->l2_size);
ea2384d3
FB
999 goto found;
1000 }
1001 }
1002 /* not found: load a new entry in the least used one */
1003 min_index = 0;
1004 min_count = 0xffffffff;
b4b3ab14 1005 for (i = 0; i < L2_CACHE_SIZE; i++) {
b3976d3c
FZ
1006 if (extent->l2_cache_counts[i] < min_count) {
1007 min_count = extent->l2_cache_counts[i];
ea2384d3
FB
1008 min_index = i;
1009 }
1010 }
b3976d3c
FZ
1011 l2_table = extent->l2_cache + (min_index * extent->l2_size);
1012 if (bdrv_pread(
1013 extent->file,
1014 (int64_t)l2_offset * 512,
1015 l2_table,
1016 extent->l2_size * sizeof(uint32_t)
1017 ) != extent->l2_size * sizeof(uint32_t)) {
65f74725 1018 return VMDK_ERROR;
b3976d3c 1019 }
5f4da8c0 1020
b3976d3c
FZ
1021 extent->l2_cache_offsets[min_index] = l2_offset;
1022 extent->l2_cache_counts[min_index] = 1;
ea2384d3 1023 found:
b3976d3c 1024 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
91b85bd3 1025 *cluster_offset = le32_to_cpu(l2_table[l2_index]);
630530a6 1026
cdeaf1f1
FZ
1027 if (m_data) {
1028 m_data->valid = 1;
1029 m_data->l1_index = l1_index;
1030 m_data->l2_index = l2_index;
1031 m_data->offset = *cluster_offset;
1032 m_data->l2_offset = l2_offset;
1033 m_data->l2_cache_entry = &l2_table[l2_index];
1034 }
14ead646
FZ
1035 if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
1036 zeroed = true;
1037 }
1038
1039 if (!*cluster_offset || zeroed) {
91b85bd3 1040 if (!allocate) {
14ead646 1041 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
91b85bd3 1042 }
9949f97e 1043
ae261c86 1044 /* Avoid the L2 tables update for the images that have snapshots. */
91b85bd3 1045 *cluster_offset = bdrv_getlength(extent->file);
2b2c8c5d
FZ
1046 if (!extent->compressed) {
1047 bdrv_truncate(
1048 extent->file,
1049 *cluster_offset + (extent->cluster_sectors << 9)
1050 );
1051 }
9949f97e 1052
91b85bd3 1053 *cluster_offset >>= 9;
e304e8e5 1054 l2_table[l2_index] = cpu_to_le32(*cluster_offset);
630530a6 1055
630530a6
TS
1056 /* First of all we write grain itself, to avoid race condition
1057 * that may to corrupt the image.
1058 * This problem may occur because of insufficient space on host disk
1059 * or inappropriate VM shutdown.
1060 */
b3976d3c 1061 if (get_whole_cluster(
ae261c86 1062 bs, extent, *cluster_offset, offset, allocate) == -1) {
65f74725 1063 return VMDK_ERROR;
ae261c86 1064 }
630530a6
TS
1065
1066 if (m_data) {
e304e8e5 1067 m_data->offset = *cluster_offset;
630530a6 1068 }
ff1afc72 1069 }
91b85bd3 1070 *cluster_offset <<= 9;
65f74725 1071 return VMDK_OK;
ea2384d3
FB
1072}
1073
b3976d3c
FZ
1074static VmdkExtent *find_extent(BDRVVmdkState *s,
1075 int64_t sector_num, VmdkExtent *start_hint)
1076{
1077 VmdkExtent *extent = start_hint;
1078
1079 if (!extent) {
1080 extent = &s->extents[0];
1081 }
1082 while (extent < &s->extents[s->num_extents]) {
1083 if (sector_num < extent->end_sector) {
1084 return extent;
1085 }
1086 extent++;
1087 }
1088 return NULL;
1089}
1090
b6b8a333 1091static int64_t coroutine_fn vmdk_co_get_block_status(BlockDriverState *bs,
f8a2e5e3 1092 int64_t sector_num, int nb_sectors, int *pnum)
ea2384d3
FB
1093{
1094 BDRVVmdkState *s = bs->opaque;
b3976d3c
FZ
1095 int64_t index_in_cluster, n, ret;
1096 uint64_t offset;
1097 VmdkExtent *extent;
1098
1099 extent = find_extent(s, sector_num, NULL);
1100 if (!extent) {
1101 return 0;
1102 }
f8a2e5e3 1103 qemu_co_mutex_lock(&s->lock);
91b85bd3
FZ
1104 ret = get_cluster_offset(bs, extent, NULL,
1105 sector_num * 512, 0, &offset);
f8a2e5e3 1106 qemu_co_mutex_unlock(&s->lock);
14ead646 1107
4bc74be9
PB
1108 switch (ret) {
1109 case VMDK_ERROR:
1110 ret = -EIO;
1111 break;
1112 case VMDK_UNALLOC:
1113 ret = 0;
1114 break;
1115 case VMDK_ZEROED:
1116 ret = BDRV_BLOCK_ZERO;
1117 break;
1118 case VMDK_OK:
1119 ret = BDRV_BLOCK_DATA;
1120 if (extent->file == bs->file) {
1121 ret |= BDRV_BLOCK_OFFSET_VALID | offset;
1122 }
1123
1124 break;
1125 }
91b85bd3
FZ
1126
1127 index_in_cluster = sector_num % extent->cluster_sectors;
1128 n = extent->cluster_sectors - index_in_cluster;
ae261c86 1129 if (n > nb_sectors) {
ea2384d3 1130 n = nb_sectors;
ae261c86 1131 }
ea2384d3 1132 *pnum = n;
b3976d3c 1133 return ret;
ea2384d3
FB
1134}
1135
dd3f6ee2
FZ
1136static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1137 int64_t offset_in_cluster, const uint8_t *buf,
1138 int nb_sectors, int64_t sector_num)
1139{
1140 int ret;
2b2c8c5d
FZ
1141 VmdkGrainMarker *data = NULL;
1142 uLongf buf_len;
dd3f6ee2
FZ
1143 const uint8_t *write_buf = buf;
1144 int write_len = nb_sectors * 512;
1145
2b2c8c5d
FZ
1146 if (extent->compressed) {
1147 if (!extent->has_marker) {
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151 buf_len = (extent->cluster_sectors << 9) * 2;
1152 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1153 if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||
1154 buf_len == 0) {
1155 ret = -EINVAL;
1156 goto out;
1157 }
1158 data->lba = sector_num;
1159 data->size = buf_len;
1160 write_buf = (uint8_t *)data;
1161 write_len = buf_len + sizeof(VmdkGrainMarker);
1162 }
dd3f6ee2
FZ
1163 ret = bdrv_pwrite(extent->file,
1164 cluster_offset + offset_in_cluster,
1165 write_buf,
1166 write_len);
1167 if (ret != write_len) {
1168 ret = ret < 0 ? ret : -EIO;
1169 goto out;
1170 }
1171 ret = 0;
1172 out:
2b2c8c5d 1173 g_free(data);
dd3f6ee2
FZ
1174 return ret;
1175}
1176
1177static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1178 int64_t offset_in_cluster, uint8_t *buf,
1179 int nb_sectors)
1180{
1181 int ret;
2b2c8c5d
FZ
1182 int cluster_bytes, buf_bytes;
1183 uint8_t *cluster_buf, *compressed_data;
1184 uint8_t *uncomp_buf;
1185 uint32_t data_len;
1186 VmdkGrainMarker *marker;
1187 uLongf buf_len;
1188
dd3f6ee2 1189
2b2c8c5d
FZ
1190 if (!extent->compressed) {
1191 ret = bdrv_pread(extent->file,
1192 cluster_offset + offset_in_cluster,
1193 buf, nb_sectors * 512);
1194 if (ret == nb_sectors * 512) {
1195 return 0;
1196 } else {
1197 return -EIO;
1198 }
1199 }
1200 cluster_bytes = extent->cluster_sectors * 512;
1201 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1202 buf_bytes = cluster_bytes * 2;
1203 cluster_buf = g_malloc(buf_bytes);
1204 uncomp_buf = g_malloc(cluster_bytes);
dd3f6ee2 1205 ret = bdrv_pread(extent->file,
2b2c8c5d
FZ
1206 cluster_offset,
1207 cluster_buf, buf_bytes);
1208 if (ret < 0) {
1209 goto out;
1210 }
1211 compressed_data = cluster_buf;
1212 buf_len = cluster_bytes;
1213 data_len = cluster_bytes;
1214 if (extent->has_marker) {
1215 marker = (VmdkGrainMarker *)cluster_buf;
1216 compressed_data = marker->data;
1217 data_len = le32_to_cpu(marker->size);
1218 }
1219 if (!data_len || data_len > buf_bytes) {
1220 ret = -EINVAL;
1221 goto out;
1222 }
1223 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1224 if (ret != Z_OK) {
1225 ret = -EINVAL;
1226 goto out;
1227
1228 }
1229 if (offset_in_cluster < 0 ||
1230 offset_in_cluster + nb_sectors * 512 > buf_len) {
1231 ret = -EINVAL;
1232 goto out;
dd3f6ee2 1233 }
2b2c8c5d
FZ
1234 memcpy(buf, uncomp_buf + offset_in_cluster, nb_sectors * 512);
1235 ret = 0;
1236
1237 out:
1238 g_free(uncomp_buf);
1239 g_free(cluster_buf);
1240 return ret;
dd3f6ee2
FZ
1241}
1242
5fafdf24 1243static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
ea2384d3
FB
1244 uint8_t *buf, int nb_sectors)
1245{
1246 BDRVVmdkState *s = bs->opaque;
b3976d3c
FZ
1247 int ret;
1248 uint64_t n, index_in_cluster;
b1649fae 1249 uint64_t extent_begin_sector, extent_relative_sector_num;
b3976d3c 1250 VmdkExtent *extent = NULL;
ea2384d3 1251 uint64_t cluster_offset;
5f4da8c0 1252
ea2384d3 1253 while (nb_sectors > 0) {
b3976d3c
FZ
1254 extent = find_extent(s, sector_num, extent);
1255 if (!extent) {
1256 return -EIO;
1257 }
91b85bd3
FZ
1258 ret = get_cluster_offset(
1259 bs, extent, NULL,
1260 sector_num << 9, 0, &cluster_offset);
b1649fae
GW
1261 extent_begin_sector = extent->end_sector - extent->sectors;
1262 extent_relative_sector_num = sector_num - extent_begin_sector;
1263 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
b3976d3c 1264 n = extent->cluster_sectors - index_in_cluster;
ae261c86 1265 if (n > nb_sectors) {
ea2384d3 1266 n = nb_sectors;
ae261c86 1267 }
14ead646 1268 if (ret != VMDK_OK) {
91b85bd3 1269 /* if not allocated, try to read from parent image, if exist */
14ead646 1270 if (bs->backing_hd && ret != VMDK_ZEROED) {
ae261c86 1271 if (!vmdk_is_cid_valid(bs)) {
7fa60fa3 1272 return -EINVAL;
ae261c86 1273 }
b171271a 1274 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
ae261c86 1275 if (ret < 0) {
7fa60fa3 1276 return ret;
ae261c86 1277 }
5f4da8c0
TS
1278 } else {
1279 memset(buf, 0, 512 * n);
1280 }
ea2384d3 1281 } else {
dd3f6ee2
FZ
1282 ret = vmdk_read_extent(extent,
1283 cluster_offset, index_in_cluster * 512,
1284 buf, n);
1285 if (ret) {
7fa60fa3
FZ
1286 return ret;
1287 }
ea2384d3
FB
1288 }
1289 nb_sectors -= n;
1290 sector_num += n;
1291 buf += n * 512;
1292 }
1293 return 0;
1294}
1295
2914caa0
PB
1296static coroutine_fn int vmdk_co_read(BlockDriverState *bs, int64_t sector_num,
1297 uint8_t *buf, int nb_sectors)
1298{
1299 int ret;
1300 BDRVVmdkState *s = bs->opaque;
1301 qemu_co_mutex_lock(&s->lock);
1302 ret = vmdk_read(bs, sector_num, buf, nb_sectors);
1303 qemu_co_mutex_unlock(&s->lock);
1304 return ret;
1305}
1306
cdeaf1f1
FZ
1307/**
1308 * vmdk_write:
1309 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
8e507243
FZ
1310 * if possible, otherwise return -ENOTSUP.
1311 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1312 * with each cluster. By dry run we can find if the zero write
1313 * is possible without modifying image data.
cdeaf1f1
FZ
1314 *
1315 * Returns: error code with 0 for success.
1316 */
5fafdf24 1317static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
cdeaf1f1
FZ
1318 const uint8_t *buf, int nb_sectors,
1319 bool zeroed, bool zero_dry_run)
ea2384d3 1320{
ff1afc72 1321 BDRVVmdkState *s = bs->opaque;
b3976d3c 1322 VmdkExtent *extent = NULL;
91b85bd3 1323 int n, ret;
b3976d3c 1324 int64_t index_in_cluster;
b1649fae 1325 uint64_t extent_begin_sector, extent_relative_sector_num;
ff1afc72 1326 uint64_t cluster_offset;
b3976d3c 1327 VmdkMetaData m_data;
ff1afc72 1328
630530a6 1329 if (sector_num > bs->total_sectors) {
4823970b 1330 error_report("Wrong offset: sector_num=0x%" PRIx64
92868412 1331 " total_sectors=0x%" PRIx64 "\n",
630530a6 1332 sector_num, bs->total_sectors);
7fa60fa3 1333 return -EIO;
630530a6
TS
1334 }
1335
ff1afc72 1336 while (nb_sectors > 0) {
b3976d3c
FZ
1337 extent = find_extent(s, sector_num, extent);
1338 if (!extent) {
1339 return -EIO;
1340 }
91b85bd3 1341 ret = get_cluster_offset(
b3976d3c
FZ
1342 bs,
1343 extent,
1344 &m_data,
2b2c8c5d 1345 sector_num << 9, !extent->compressed,
91b85bd3 1346 &cluster_offset);
2b2c8c5d 1347 if (extent->compressed) {
65f74725 1348 if (ret == VMDK_OK) {
2b2c8c5d 1349 /* Refuse write to allocated cluster for streamOptimized */
4823970b
FZ
1350 error_report("Could not write to allocated cluster"
1351 " for streamOptimized");
2b2c8c5d
FZ
1352 return -EIO;
1353 } else {
1354 /* allocate */
1355 ret = get_cluster_offset(
1356 bs,
1357 extent,
1358 &m_data,
1359 sector_num << 9, 1,
1360 &cluster_offset);
1361 }
1362 }
cdeaf1f1 1363 if (ret == VMDK_ERROR) {
91b85bd3 1364 return -EINVAL;
b3976d3c 1365 }
b1649fae
GW
1366 extent_begin_sector = extent->end_sector - extent->sectors;
1367 extent_relative_sector_num = sector_num - extent_begin_sector;
1368 index_in_cluster = extent_relative_sector_num % extent->cluster_sectors;
b3976d3c
FZ
1369 n = extent->cluster_sectors - index_in_cluster;
1370 if (n > nb_sectors) {
1371 n = nb_sectors;
1372 }
cdeaf1f1
FZ
1373 if (zeroed) {
1374 /* Do zeroed write, buf is ignored */
1375 if (extent->has_zero_grain &&
1376 index_in_cluster == 0 &&
1377 n >= extent->cluster_sectors) {
1378 n = extent->cluster_sectors;
1379 if (!zero_dry_run) {
1380 m_data.offset = VMDK_GTE_ZEROED;
1381 /* update L2 tables */
1382 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1383 return -EIO;
1384 }
1385 }
1386 } else {
1387 return -ENOTSUP;
1388 }
1389 } else {
1390 ret = vmdk_write_extent(extent,
1391 cluster_offset, index_in_cluster * 512,
1392 buf, n, sector_num);
1393 if (ret) {
1394 return ret;
1395 }
1396 if (m_data.valid) {
1397 /* update L2 tables */
1398 if (vmdk_L2update(extent, &m_data) != VMDK_OK) {
1399 return -EIO;
1400 }
b3976d3c 1401 }
630530a6 1402 }
ff1afc72
FB
1403 nb_sectors -= n;
1404 sector_num += n;
1405 buf += n * 512;
5f4da8c0 1406
ae261c86
FZ
1407 /* update CID on the first write every time the virtual disk is
1408 * opened */
69b4d86d 1409 if (!s->cid_updated) {
99f1835d
KW
1410 ret = vmdk_write_cid(bs, time(NULL));
1411 if (ret < 0) {
1412 return ret;
1413 }
69b4d86d 1414 s->cid_updated = true;
5f4da8c0 1415 }
ff1afc72
FB
1416 }
1417 return 0;
ea2384d3
FB
1418}
1419
e183ef75
PB
1420static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
1421 const uint8_t *buf, int nb_sectors)
1422{
1423 int ret;
1424 BDRVVmdkState *s = bs->opaque;
1425 qemu_co_mutex_lock(&s->lock);
cdeaf1f1
FZ
1426 ret = vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
1427 qemu_co_mutex_unlock(&s->lock);
1428 return ret;
1429}
1430
1431static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
1432 int64_t sector_num,
aa7bfbff
PL
1433 int nb_sectors,
1434 BdrvRequestFlags flags)
cdeaf1f1
FZ
1435{
1436 int ret;
1437 BDRVVmdkState *s = bs->opaque;
1438 qemu_co_mutex_lock(&s->lock);
8e507243
FZ
1439 /* write zeroes could fail if sectors not aligned to cluster, test it with
1440 * dry_run == true before really updating image */
cdeaf1f1
FZ
1441 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, true);
1442 if (!ret) {
1443 ret = vmdk_write(bs, sector_num, NULL, nb_sectors, true, false);
1444 }
e183ef75
PB
1445 qemu_co_mutex_unlock(&s->lock);
1446 return ret;
1447}
1448
6c031aac 1449static int vmdk_create_extent(const char *filename, int64_t filesize,
69e0b6df 1450 bool flat, bool compress, bool zeroed_grain)
8979b227 1451{
f66fd6c3
FZ
1452 int ret, i;
1453 int fd = 0;
8979b227
FB
1454 VMDK4Header header;
1455 uint32_t tmp, magic, grains, gd_size, gt_size, gt_count;
0e7e1989 1456
6165f4d8
CB
1457 fd = qemu_open(filename,
1458 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1459 0644);
f66fd6c3
FZ
1460 if (fd < 0) {
1461 return -errno;
0e7e1989 1462 }
f66fd6c3
FZ
1463 if (flat) {
1464 ret = ftruncate(fd, filesize);
1465 if (ret < 0) {
1466 ret = -errno;
1467 }
1468 goto exit;
5f4da8c0 1469 }
8979b227
FB
1470 magic = cpu_to_be32(VMDK4_MAGIC);
1471 memset(&header, 0, sizeof(header));
69e0b6df 1472 header.version = zeroed_grain ? 2 : 1;
95b0aa42 1473 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
69e0b6df
FZ
1474 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1475 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
6c031aac 1476 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
f66fd6c3 1477 header.capacity = filesize / 512;
16372ff0 1478 header.granularity = 128;
ca8804ce 1479 header.num_gtes_per_gt = 512;
8979b227 1480
f66fd6c3 1481 grains = (filesize / 512 + header.granularity - 1) / header.granularity;
ca8804ce 1482 gt_size = ((header.num_gtes_per_gt * sizeof(uint32_t)) + 511) >> 9;
f66fd6c3 1483 gt_count =
ca8804ce 1484 (grains + header.num_gtes_per_gt - 1) / header.num_gtes_per_gt;
8979b227
FB
1485 gd_size = (gt_count * sizeof(uint32_t) + 511) >> 9;
1486
1487 header.desc_offset = 1;
1488 header.desc_size = 20;
1489 header.rgd_offset = header.desc_offset + header.desc_size;
1490 header.gd_offset = header.rgd_offset + gd_size + (gt_size * gt_count);
1491 header.grain_offset =
1492 ((header.gd_offset + gd_size + (gt_size * gt_count) +
1493 header.granularity - 1) / header.granularity) *
1494 header.granularity;
16372ff0
AG
1495 /* swap endianness for all header fields */
1496 header.version = cpu_to_le32(header.version);
1497 header.flags = cpu_to_le32(header.flags);
1498 header.capacity = cpu_to_le64(header.capacity);
1499 header.granularity = cpu_to_le64(header.granularity);
ca8804ce 1500 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
8979b227
FB
1501 header.desc_offset = cpu_to_le64(header.desc_offset);
1502 header.desc_size = cpu_to_le64(header.desc_size);
1503 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1504 header.gd_offset = cpu_to_le64(header.gd_offset);
1505 header.grain_offset = cpu_to_le64(header.grain_offset);
6c031aac 1506 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
8979b227
FB
1507
1508 header.check_bytes[0] = 0xa;
1509 header.check_bytes[1] = 0x20;
1510 header.check_bytes[2] = 0xd;
1511 header.check_bytes[3] = 0xa;
3b46e624
TS
1512
1513 /* write all the data */
1640366c
KS
1514 ret = qemu_write_full(fd, &magic, sizeof(magic));
1515 if (ret != sizeof(magic)) {
b781cce5 1516 ret = -errno;
1640366c
KS
1517 goto exit;
1518 }
1519 ret = qemu_write_full(fd, &header, sizeof(header));
1520 if (ret != sizeof(header)) {
b781cce5 1521 ret = -errno;
1640366c
KS
1522 goto exit;
1523 }
8979b227 1524
16372ff0 1525 ret = ftruncate(fd, le64_to_cpu(header.grain_offset) << 9);
1640366c 1526 if (ret < 0) {
b781cce5 1527 ret = -errno;
1640366c
KS
1528 goto exit;
1529 }
8979b227
FB
1530
1531 /* write grain directory */
1532 lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
16372ff0 1533 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_size;
1640366c
KS
1534 i < gt_count; i++, tmp += gt_size) {
1535 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1536 if (ret != sizeof(tmp)) {
b781cce5 1537 ret = -errno;
1640366c
KS
1538 goto exit;
1539 }
1540 }
3b46e624 1541
8979b227
FB
1542 /* write backup grain directory */
1543 lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
16372ff0 1544 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_size;
1640366c
KS
1545 i < gt_count; i++, tmp += gt_size) {
1546 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1547 if (ret != sizeof(tmp)) {
b781cce5 1548 ret = -errno;
1640366c
KS
1549 goto exit;
1550 }
1551 }
8979b227 1552
f66fd6c3
FZ
1553 ret = 0;
1554 exit:
2e1e79da 1555 qemu_close(fd);
f66fd6c3
FZ
1556 return ret;
1557}
1558
1559static int filename_decompose(const char *filename, char *path, char *prefix,
4823970b 1560 char *postfix, size_t buf_len, Error **errp)
f66fd6c3
FZ
1561{
1562 const char *p, *q;
1563
1564 if (filename == NULL || !strlen(filename)) {
4823970b 1565 error_setg(errp, "No filename provided");
65f74725 1566 return VMDK_ERROR;
f66fd6c3
FZ
1567 }
1568 p = strrchr(filename, '/');
1569 if (p == NULL) {
1570 p = strrchr(filename, '\\');
1571 }
1572 if (p == NULL) {
1573 p = strrchr(filename, ':');
1574 }
1575 if (p != NULL) {
1576 p++;
1577 if (p - filename >= buf_len) {
65f74725 1578 return VMDK_ERROR;
f66fd6c3
FZ
1579 }
1580 pstrcpy(path, p - filename + 1, filename);
1581 } else {
1582 p = filename;
1583 path[0] = '\0';
1584 }
1585 q = strrchr(p, '.');
1586 if (q == NULL) {
1587 pstrcpy(prefix, buf_len, p);
1588 postfix[0] = '\0';
1589 } else {
1590 if (q - p >= buf_len) {
65f74725 1591 return VMDK_ERROR;
f66fd6c3
FZ
1592 }
1593 pstrcpy(prefix, q - p + 1, p);
1594 pstrcpy(postfix, buf_len, q);
1595 }
65f74725 1596 return VMDK_OK;
f66fd6c3
FZ
1597}
1598
d5124c00
HR
1599static int vmdk_create(const char *filename, QEMUOptionParameter *options,
1600 Error **errp)
f66fd6c3
FZ
1601{
1602 int fd, idx = 0;
af057fe7 1603 char *desc = NULL;
f66fd6c3 1604 int64_t total_size = 0, filesize;
7f2039f6 1605 const char *adapter_type = NULL;
f66fd6c3
FZ
1606 const char *backing_file = NULL;
1607 const char *fmt = NULL;
1608 int flags = 0;
1609 int ret = 0;
6c031aac 1610 bool flat, split, compress;
af057fe7 1611 GString *ext_desc_lines;
f66fd6c3
FZ
1612 char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];
1613 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1614 const char *desc_extent_line;
1615 char parent_desc_line[BUF_SIZE] = "";
1616 uint32_t parent_cid = 0xffffffff;
7f2039f6 1617 uint32_t number_heads = 16;
69e0b6df 1618 bool zeroed_grain = false;
f66fd6c3
FZ
1619 const char desc_template[] =
1620 "# Disk DescriptorFile\n"
1621 "version=1\n"
1622 "CID=%x\n"
1623 "parentCID=%x\n"
1624 "createType=\"%s\"\n"
1625 "%s"
1626 "\n"
1627 "# Extent description\n"
1628 "%s"
1629 "\n"
1630 "# The Disk Data Base\n"
1631 "#DDB\n"
1632 "\n"
1633 "ddb.virtualHWVersion = \"%d\"\n"
1634 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
7f2039f6 1635 "ddb.geometry.heads = \"%d\"\n"
f66fd6c3 1636 "ddb.geometry.sectors = \"63\"\n"
7f2039f6 1637 "ddb.adapterType = \"%s\"\n";
f66fd6c3 1638
af057fe7
FZ
1639 ext_desc_lines = g_string_new(NULL);
1640
4823970b 1641 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
af057fe7
FZ
1642 ret = -EINVAL;
1643 goto exit;
f66fd6c3
FZ
1644 }
1645 /* Read out options */
1646 while (options && options->name) {
1647 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1648 total_size = options->value.n;
7f2039f6
OP
1649 } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {
1650 adapter_type = options->value.s;
f66fd6c3
FZ
1651 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1652 backing_file = options->value.s;
1653 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
1654 flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;
1655 } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {
1656 fmt = options->value.s;
69e0b6df
FZ
1657 } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {
1658 zeroed_grain |= options->value.n;
f66fd6c3
FZ
1659 }
1660 options++;
1661 }
7f2039f6
OP
1662 if (!adapter_type) {
1663 adapter_type = "ide";
1664 } else if (strcmp(adapter_type, "ide") &&
1665 strcmp(adapter_type, "buslogic") &&
1666 strcmp(adapter_type, "lsilogic") &&
1667 strcmp(adapter_type, "legacyESX")) {
4823970b 1668 error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
af057fe7
FZ
1669 ret = -EINVAL;
1670 goto exit;
7f2039f6
OP
1671 }
1672 if (strcmp(adapter_type, "ide") != 0) {
1673 /* that's the number of heads with which vmware operates when
1674 creating, exporting, etc. vmdk files with a non-ide adapter type */
1675 number_heads = 255;
1676 }
f66fd6c3
FZ
1677 if (!fmt) {
1678 /* Default format to monolithicSparse */
1679 fmt = "monolithicSparse";
1680 } else if (strcmp(fmt, "monolithicFlat") &&
1681 strcmp(fmt, "monolithicSparse") &&
1682 strcmp(fmt, "twoGbMaxExtentSparse") &&
6c031aac
FZ
1683 strcmp(fmt, "twoGbMaxExtentFlat") &&
1684 strcmp(fmt, "streamOptimized")) {
4823970b 1685 error_setg(errp, "Unknown subformat: '%s'", fmt);
af057fe7
FZ
1686 ret = -EINVAL;
1687 goto exit;
f66fd6c3
FZ
1688 }
1689 split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
1690 strcmp(fmt, "twoGbMaxExtentSparse"));
1691 flat = !(strcmp(fmt, "monolithicFlat") &&
1692 strcmp(fmt, "twoGbMaxExtentFlat"));
6c031aac 1693 compress = !strcmp(fmt, "streamOptimized");
f66fd6c3
FZ
1694 if (flat) {
1695 desc_extent_line = "RW %lld FLAT \"%s\" 0\n";
1696 } else {
1697 desc_extent_line = "RW %lld SPARSE \"%s\"\n";
1698 }
1699 if (flat && backing_file) {
4823970b 1700 error_setg(errp, "Flat image can't have backing file");
af057fe7
FZ
1701 ret = -ENOTSUP;
1702 goto exit;
f66fd6c3 1703 }
52c8d629
FZ
1704 if (flat && zeroed_grain) {
1705 error_setg(errp, "Flat image can't enable zeroed grain");
af057fe7
FZ
1706 ret = -ENOTSUP;
1707 goto exit;
52c8d629 1708 }
f66fd6c3 1709 if (backing_file) {
f66fd6c3 1710 BlockDriverState *bs = bdrv_new("");
c9fbb99d 1711 ret = bdrv_open(bs, backing_file, NULL, BDRV_O_NO_BACKING, NULL, errp);
f66fd6c3 1712 if (ret != 0) {
4f6fd349 1713 bdrv_unref(bs);
af057fe7 1714 goto exit;
f66fd6c3
FZ
1715 }
1716 if (strcmp(bs->drv->format_name, "vmdk")) {
4f6fd349 1717 bdrv_unref(bs);
af057fe7
FZ
1718 ret = -EINVAL;
1719 goto exit;
f66fd6c3 1720 }
f66fd6c3 1721 parent_cid = vmdk_read_cid(bs, 0);
4f6fd349 1722 bdrv_unref(bs);
f66fd6c3 1723 snprintf(parent_desc_line, sizeof(parent_desc_line),
8ed610a1 1724 "parentFileNameHint=\"%s\"", backing_file);
f66fd6c3
FZ
1725 }
1726
1727 /* Create extents */
1728 filesize = total_size;
1729 while (filesize > 0) {
1730 char desc_line[BUF_SIZE];
1731 char ext_filename[PATH_MAX];
1732 char desc_filename[PATH_MAX];
1733 int64_t size = filesize;
1734
1735 if (split && size > split_size) {
1736 size = split_size;
1737 }
1738 if (split) {
1739 snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",
1740 prefix, flat ? 'f' : 's', ++idx, postfix);
1741 } else if (flat) {
1742 snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",
1743 prefix, postfix);
1744 } else {
1745 snprintf(desc_filename, sizeof(desc_filename), "%s%s",
1746 prefix, postfix);
1747 }
1748 snprintf(ext_filename, sizeof(ext_filename), "%s%s",
1749 path, desc_filename);
1750
69e0b6df
FZ
1751 if (vmdk_create_extent(ext_filename, size,
1752 flat, compress, zeroed_grain)) {
af057fe7
FZ
1753 ret = -EINVAL;
1754 goto exit;
f66fd6c3
FZ
1755 }
1756 filesize -= size;
1757
1758 /* Format description line */
1759 snprintf(desc_line, sizeof(desc_line),
1760 desc_extent_line, size / 512, desc_filename);
af057fe7 1761 g_string_append(ext_desc_lines, desc_line);
f66fd6c3
FZ
1762 }
1763 /* generate descriptor file */
af057fe7
FZ
1764 desc = g_strdup_printf(desc_template,
1765 (unsigned int)time(NULL),
1766 parent_cid,
1767 fmt,
1768 parent_desc_line,
1769 ext_desc_lines->str,
1770 (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
1771 total_size / (int64_t)(63 * number_heads * 512),
1772 number_heads,
1773 adapter_type);
f66fd6c3 1774 if (split || flat) {
6165f4d8
CB
1775 fd = qemu_open(filename,
1776 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
1777 0644);
f66fd6c3 1778 } else {
6165f4d8
CB
1779 fd = qemu_open(filename,
1780 O_WRONLY | O_BINARY | O_LARGEFILE,
1781 0644);
f66fd6c3
FZ
1782 }
1783 if (fd < 0) {
af057fe7
FZ
1784 ret = -errno;
1785 goto exit;
f66fd6c3
FZ
1786 }
1787 /* the descriptor offset = 0x200 */
1788 if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {
1789 ret = -errno;
af057fe7 1790 goto close_exit;
f66fd6c3 1791 }
1640366c
KS
1792 ret = qemu_write_full(fd, desc, strlen(desc));
1793 if (ret != strlen(desc)) {
b781cce5 1794 ret = -errno;
af057fe7 1795 goto close_exit;
1640366c 1796 }
1640366c 1797 ret = 0;
af057fe7 1798close_exit:
2e1e79da 1799 qemu_close(fd);
af057fe7
FZ
1800exit:
1801 g_free(desc);
1802 g_string_free(ext_desc_lines, true);
1640366c 1803 return ret;
8979b227
FB
1804}
1805
e2731add 1806static void vmdk_close(BlockDriverState *bs)
ea2384d3 1807{
2bc3166c
KW
1808 BDRVVmdkState *s = bs->opaque;
1809
b3976d3c 1810 vmdk_free_extents(bs);
f4c129a3 1811 g_free(s->create_type);
2bc3166c
KW
1812
1813 migrate_del_blocker(s->migration_blocker);
1814 error_free(s->migration_blocker);
ea2384d3
FB
1815}
1816
8b94ff85 1817static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
7a6cba61 1818{
333c574d 1819 BDRVVmdkState *s = bs->opaque;
29cdb251
PB
1820 int i, err;
1821 int ret = 0;
333c574d 1822
333c574d 1823 for (i = 0; i < s->num_extents; i++) {
8b94ff85 1824 err = bdrv_co_flush(s->extents[i].file);
333c574d
FZ
1825 if (err < 0) {
1826 ret = err;
1827 }
1828 }
1829 return ret;
7a6cba61
PB
1830}
1831
4a1d5e1f
FZ
1832static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
1833{
1834 int i;
1835 int64_t ret = 0;
1836 int64_t r;
1837 BDRVVmdkState *s = bs->opaque;
1838
1839 ret = bdrv_get_allocated_file_size(bs->file);
1840 if (ret < 0) {
1841 return ret;
1842 }
1843 for (i = 0; i < s->num_extents; i++) {
1844 if (s->extents[i].file == bs->file) {
1845 continue;
1846 }
1847 r = bdrv_get_allocated_file_size(s->extents[i].file);
1848 if (r < 0) {
1849 return r;
1850 }
1851 ret += r;
1852 }
1853 return ret;
1854}
0e7e1989 1855
da7a50f9
FZ
1856static int vmdk_has_zero_init(BlockDriverState *bs)
1857{
1858 int i;
1859 BDRVVmdkState *s = bs->opaque;
1860
1861 /* If has a flat extent and its underlying storage doesn't have zero init,
1862 * return 0. */
1863 for (i = 0; i < s->num_extents; i++) {
1864 if (s->extents[i].flat) {
1865 if (!bdrv_has_zero_init(s->extents[i].file)) {
1866 return 0;
1867 }
1868 }
1869 }
1870 return 1;
1871}
1872
f4c129a3
FZ
1873static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
1874{
1875 ImageInfo *info = g_new0(ImageInfo, 1);
1876
1877 *info = (ImageInfo){
1878 .filename = g_strdup(extent->file->filename),
1879 .format = g_strdup(extent->type),
1880 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
1881 .compressed = extent->compressed,
1882 .has_compressed = extent->compressed,
1883 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
1884 .has_cluster_size = !extent->flat,
1885 };
1886
1887 return info;
1888}
1889
1890static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
1891{
1892 int i;
1893 BDRVVmdkState *s = bs->opaque;
1894 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
1895 ImageInfoList **next;
1896
1897 *spec_info = (ImageInfoSpecific){
1898 .kind = IMAGE_INFO_SPECIFIC_KIND_VMDK,
1899 {
1900 .vmdk = g_new0(ImageInfoSpecificVmdk, 1),
1901 },
1902 };
1903
1904 *spec_info->vmdk = (ImageInfoSpecificVmdk) {
1905 .create_type = g_strdup(s->create_type),
1906 .cid = s->cid,
1907 .parent_cid = s->parent_cid,
1908 };
1909
1910 next = &spec_info->vmdk->extents;
1911 for (i = 0; i < s->num_extents; i++) {
1912 *next = g_new0(ImageInfoList, 1);
1913 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
1914 (*next)->next = NULL;
1915 next = &(*next)->next;
1916 }
1917
1918 return spec_info;
1919}
1920
0e7e1989 1921static QEMUOptionParameter vmdk_create_options[] = {
db08adf5
KW
1922 {
1923 .name = BLOCK_OPT_SIZE,
1924 .type = OPT_SIZE,
1925 .help = "Virtual disk size"
1926 },
7f2039f6
OP
1927 {
1928 .name = BLOCK_OPT_ADAPTER_TYPE,
1929 .type = OPT_STRING,
1930 .help = "Virtual adapter type, can be one of "
1931 "ide (default), lsilogic, buslogic or legacyESX"
1932 },
db08adf5
KW
1933 {
1934 .name = BLOCK_OPT_BACKING_FILE,
1935 .type = OPT_STRING,
1936 .help = "File name of a base image"
1937 },
1938 {
1939 .name = BLOCK_OPT_COMPAT6,
1940 .type = OPT_FLAG,
1941 .help = "VMDK version 6 image"
1942 },
f66fd6c3
FZ
1943 {
1944 .name = BLOCK_OPT_SUBFMT,
1945 .type = OPT_STRING,
1946 .help =
1947 "VMDK flat extent format, can be one of "
6c031aac 1948 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
f66fd6c3 1949 },
69e0b6df
FZ
1950 {
1951 .name = BLOCK_OPT_ZEROED_GRAIN,
1952 .type = OPT_FLAG,
1953 .help = "Enable efficient zero writes using the zeroed-grain GTE feature"
1954 },
0e7e1989
KW
1955 { NULL }
1956};
1957
5efa9d5a 1958static BlockDriver bdrv_vmdk = {
da7a50f9
FZ
1959 .format_name = "vmdk",
1960 .instance_size = sizeof(BDRVVmdkState),
1961 .bdrv_probe = vmdk_probe,
1962 .bdrv_open = vmdk_open,
1963 .bdrv_reopen_prepare = vmdk_reopen_prepare,
1964 .bdrv_read = vmdk_co_read,
1965 .bdrv_write = vmdk_co_write,
1966 .bdrv_co_write_zeroes = vmdk_co_write_zeroes,
1967 .bdrv_close = vmdk_close,
1968 .bdrv_create = vmdk_create,
1969 .bdrv_co_flush_to_disk = vmdk_co_flush,
b6b8a333 1970 .bdrv_co_get_block_status = vmdk_co_get_block_status,
da7a50f9
FZ
1971 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
1972 .bdrv_has_zero_init = vmdk_has_zero_init,
f4c129a3 1973 .bdrv_get_specific_info = vmdk_get_specific_info,
da7a50f9
FZ
1974
1975 .create_options = vmdk_create_options,
ea2384d3 1976};
5efa9d5a
AL
1977
1978static void bdrv_vmdk_init(void)
1979{
1980 bdrv_register(&bdrv_vmdk);
1981}
1982
1983block_init(bdrv_vmdk_init);