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