vmdk: Reduce the max bound for L1 table size
[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/osdep.h"
27 #include "qapi/error.h"
28 #include "block/block_int.h"
29 #include "sysemu/block-backend.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qemu/error-report.h"
33 #include "qemu/module.h"
34 #include "qemu/option.h"
35 #include "qemu/bswap.h"
36 #include "migration/blocker.h"
37 #include "qemu/cutils.h"
38 #include <zlib.h>
39
40 #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D')
41 #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V')
42 #define VMDK4_COMPRESSION_DEFLATE 1
43 #define VMDK4_FLAG_NL_DETECT (1 << 0)
44 #define VMDK4_FLAG_RGD (1 << 1)
45 /* Zeroed-grain enable bit */
46 #define VMDK4_FLAG_ZERO_GRAIN (1 << 2)
47 #define VMDK4_FLAG_COMPRESS (1 << 16)
48 #define VMDK4_FLAG_MARKER (1 << 17)
49 #define VMDK4_GD_AT_END 0xffffffffffffffffULL
50
51 #define VMDK_EXTENT_MAX_SECTORS (1ULL << 32)
52
53 #define VMDK_GTE_ZEROED 0x1
54
55 /* VMDK internal error codes */
56 #define VMDK_OK 0
57 #define VMDK_ERROR (-1)
58 /* Cluster not allocated */
59 #define VMDK_UNALLOC (-2)
60 #define VMDK_ZEROED (-3)
61
62 #define BLOCK_OPT_ZEROED_GRAIN "zeroed_grain"
63
64 typedef struct {
65 uint32_t version;
66 uint32_t flags;
67 uint32_t disk_sectors;
68 uint32_t granularity;
69 uint32_t l1dir_offset;
70 uint32_t l1dir_size;
71 uint32_t file_sectors;
72 uint32_t cylinders;
73 uint32_t heads;
74 uint32_t sectors_per_track;
75 } QEMU_PACKED VMDK3Header;
76
77 typedef struct {
78 uint32_t version;
79 uint32_t flags;
80 uint64_t capacity;
81 uint64_t granularity;
82 uint64_t desc_offset;
83 uint64_t desc_size;
84 /* Number of GrainTableEntries per GrainTable */
85 uint32_t num_gtes_per_gt;
86 uint64_t rgd_offset;
87 uint64_t gd_offset;
88 uint64_t grain_offset;
89 char filler[1];
90 char check_bytes[4];
91 uint16_t compressAlgorithm;
92 } QEMU_PACKED VMDK4Header;
93
94 #define L2_CACHE_SIZE 16
95
96 typedef struct VmdkExtent {
97 BdrvChild *file;
98 bool flat;
99 bool compressed;
100 bool has_marker;
101 bool has_zero_grain;
102 int version;
103 int64_t sectors;
104 int64_t end_sector;
105 int64_t flat_start_offset;
106 int64_t l1_table_offset;
107 int64_t l1_backup_table_offset;
108 uint32_t *l1_table;
109 uint32_t *l1_backup_table;
110 unsigned int l1_size;
111 uint32_t l1_entry_sectors;
112
113 unsigned int l2_size;
114 uint32_t *l2_cache;
115 uint32_t l2_cache_offsets[L2_CACHE_SIZE];
116 uint32_t l2_cache_counts[L2_CACHE_SIZE];
117
118 int64_t cluster_sectors;
119 int64_t next_cluster_sector;
120 char *type;
121 } VmdkExtent;
122
123 typedef struct BDRVVmdkState {
124 CoMutex lock;
125 uint64_t desc_offset;
126 bool cid_updated;
127 bool cid_checked;
128 uint32_t cid;
129 uint32_t parent_cid;
130 int num_extents;
131 /* Extent array with num_extents entries, ascend ordered by address */
132 VmdkExtent *extents;
133 Error *migration_blocker;
134 char *create_type;
135 } BDRVVmdkState;
136
137 typedef struct VmdkMetaData {
138 unsigned int l1_index;
139 unsigned int l2_index;
140 unsigned int l2_offset;
141 int valid;
142 uint32_t *l2_cache_entry;
143 } VmdkMetaData;
144
145 typedef struct VmdkGrainMarker {
146 uint64_t lba;
147 uint32_t size;
148 uint8_t data[0];
149 } QEMU_PACKED VmdkGrainMarker;
150
151 enum {
152 MARKER_END_OF_STREAM = 0,
153 MARKER_GRAIN_TABLE = 1,
154 MARKER_GRAIN_DIRECTORY = 2,
155 MARKER_FOOTER = 3,
156 };
157
158 static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
159 {
160 uint32_t magic;
161
162 if (buf_size < 4) {
163 return 0;
164 }
165 magic = be32_to_cpu(*(uint32_t *)buf);
166 if (magic == VMDK3_MAGIC ||
167 magic == VMDK4_MAGIC) {
168 return 100;
169 } else {
170 const char *p = (const char *)buf;
171 const char *end = p + buf_size;
172 while (p < end) {
173 if (*p == '#') {
174 /* skip comment line */
175 while (p < end && *p != '\n') {
176 p++;
177 }
178 p++;
179 continue;
180 }
181 if (*p == ' ') {
182 while (p < end && *p == ' ') {
183 p++;
184 }
185 /* skip '\r' if windows line endings used. */
186 if (p < end && *p == '\r') {
187 p++;
188 }
189 /* only accept blank lines before 'version=' line */
190 if (p == end || *p != '\n') {
191 return 0;
192 }
193 p++;
194 continue;
195 }
196 if (end - p >= strlen("version=X\n")) {
197 if (strncmp("version=1\n", p, strlen("version=1\n")) == 0 ||
198 strncmp("version=2\n", p, strlen("version=2\n")) == 0 ||
199 strncmp("version=3\n", p, strlen("version=3\n")) == 0) {
200 return 100;
201 }
202 }
203 if (end - p >= strlen("version=X\r\n")) {
204 if (strncmp("version=1\r\n", p, strlen("version=1\r\n")) == 0 ||
205 strncmp("version=2\r\n", p, strlen("version=2\r\n")) == 0 ||
206 strncmp("version=3\r\n", p, strlen("version=3\r\n")) == 0) {
207 return 100;
208 }
209 }
210 return 0;
211 }
212 return 0;
213 }
214 }
215
216 #define SECTOR_SIZE 512
217 #define DESC_SIZE (20 * SECTOR_SIZE) /* 20 sectors of 512 bytes each */
218 #define BUF_SIZE 4096
219 #define HEADER_SIZE 512 /* first sector of 512 bytes */
220
221 static void vmdk_free_extents(BlockDriverState *bs)
222 {
223 int i;
224 BDRVVmdkState *s = bs->opaque;
225 VmdkExtent *e;
226
227 for (i = 0; i < s->num_extents; i++) {
228 e = &s->extents[i];
229 g_free(e->l1_table);
230 g_free(e->l2_cache);
231 g_free(e->l1_backup_table);
232 g_free(e->type);
233 if (e->file != bs->file) {
234 bdrv_unref_child(bs, e->file);
235 }
236 }
237 g_free(s->extents);
238 }
239
240 static void vmdk_free_last_extent(BlockDriverState *bs)
241 {
242 BDRVVmdkState *s = bs->opaque;
243
244 if (s->num_extents == 0) {
245 return;
246 }
247 s->num_extents--;
248 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents);
249 }
250
251 /* Return -ve errno, or 0 on success and write CID into *pcid. */
252 static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid)
253 {
254 char *desc;
255 uint32_t cid;
256 const char *p_name, *cid_str;
257 size_t cid_str_size;
258 BDRVVmdkState *s = bs->opaque;
259 int ret;
260
261 desc = g_malloc0(DESC_SIZE);
262 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
263 if (ret < 0) {
264 goto out;
265 }
266
267 if (parent) {
268 cid_str = "parentCID";
269 cid_str_size = sizeof("parentCID");
270 } else {
271 cid_str = "CID";
272 cid_str_size = sizeof("CID");
273 }
274
275 desc[DESC_SIZE - 1] = '\0';
276 p_name = strstr(desc, cid_str);
277 if (p_name == NULL) {
278 ret = -EINVAL;
279 goto out;
280 }
281 p_name += cid_str_size;
282 if (sscanf(p_name, "%" SCNx32, &cid) != 1) {
283 ret = -EINVAL;
284 goto out;
285 }
286 *pcid = cid;
287 ret = 0;
288
289 out:
290 g_free(desc);
291 return ret;
292 }
293
294 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
295 {
296 char *desc, *tmp_desc;
297 char *p_name, *tmp_str;
298 BDRVVmdkState *s = bs->opaque;
299 int ret = 0;
300
301 desc = g_malloc0(DESC_SIZE);
302 tmp_desc = g_malloc0(DESC_SIZE);
303 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
304 if (ret < 0) {
305 goto out;
306 }
307
308 desc[DESC_SIZE - 1] = '\0';
309 tmp_str = strstr(desc, "parentCID");
310 if (tmp_str == NULL) {
311 ret = -EINVAL;
312 goto out;
313 }
314
315 pstrcpy(tmp_desc, DESC_SIZE, tmp_str);
316 p_name = strstr(desc, "CID");
317 if (p_name != NULL) {
318 p_name += sizeof("CID");
319 snprintf(p_name, DESC_SIZE - (p_name - desc), "%" PRIx32 "\n", cid);
320 pstrcat(desc, DESC_SIZE, tmp_desc);
321 }
322
323 ret = bdrv_pwrite_sync(bs->file, s->desc_offset, desc, DESC_SIZE);
324
325 out:
326 g_free(desc);
327 g_free(tmp_desc);
328 return ret;
329 }
330
331 static int vmdk_is_cid_valid(BlockDriverState *bs)
332 {
333 BDRVVmdkState *s = bs->opaque;
334 uint32_t cur_pcid;
335
336 if (!s->cid_checked && bs->backing) {
337 BlockDriverState *p_bs = bs->backing->bs;
338
339 if (strcmp(p_bs->drv->format_name, "vmdk")) {
340 /* Backing file is not in vmdk format, so it does not have
341 * a CID, which makes the overlay's parent CID invalid */
342 return 0;
343 }
344
345 if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) {
346 /* read failure: report as not valid */
347 return 0;
348 }
349 if (s->parent_cid != cur_pcid) {
350 /* CID not valid */
351 return 0;
352 }
353 }
354 s->cid_checked = true;
355 /* CID valid */
356 return 1;
357 }
358
359 /* We have nothing to do for VMDK reopen, stubs just return success */
360 static int vmdk_reopen_prepare(BDRVReopenState *state,
361 BlockReopenQueue *queue, Error **errp)
362 {
363 assert(state != NULL);
364 assert(state->bs != NULL);
365 return 0;
366 }
367
368 static int vmdk_parent_open(BlockDriverState *bs)
369 {
370 char *p_name;
371 char *desc;
372 BDRVVmdkState *s = bs->opaque;
373 int ret;
374
375 desc = g_malloc0(DESC_SIZE + 1);
376 ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE);
377 if (ret < 0) {
378 goto out;
379 }
380 ret = 0;
381
382 p_name = strstr(desc, "parentFileNameHint");
383 if (p_name != NULL) {
384 char *end_name;
385
386 p_name += sizeof("parentFileNameHint") + 1;
387 end_name = strchr(p_name, '\"');
388 if (end_name == NULL) {
389 ret = -EINVAL;
390 goto out;
391 }
392 if ((end_name - p_name) > sizeof(bs->auto_backing_file) - 1) {
393 ret = -EINVAL;
394 goto out;
395 }
396
397 pstrcpy(bs->auto_backing_file, end_name - p_name + 1, p_name);
398 pstrcpy(bs->backing_file, sizeof(bs->backing_file),
399 bs->auto_backing_file);
400 pstrcpy(bs->backing_format, sizeof(bs->backing_format),
401 "vmdk");
402 }
403
404 out:
405 g_free(desc);
406 return ret;
407 }
408
409 /* Create and append extent to the extent array. Return the added VmdkExtent
410 * address. return NULL if allocation failed. */
411 static int vmdk_add_extent(BlockDriverState *bs,
412 BdrvChild *file, bool flat, int64_t sectors,
413 int64_t l1_offset, int64_t l1_backup_offset,
414 uint32_t l1_size,
415 int l2_size, uint64_t cluster_sectors,
416 VmdkExtent **new_extent,
417 Error **errp)
418 {
419 VmdkExtent *extent;
420 BDRVVmdkState *s = bs->opaque;
421 int64_t nb_sectors;
422
423 if (cluster_sectors > 0x200000) {
424 /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
425 error_setg(errp, "Invalid granularity, image may be corrupt");
426 return -EFBIG;
427 }
428 if (l1_size > 32 * 1024 * 1024) {
429 /*
430 * Although with big capacity and small l1_entry_sectors, we can get a
431 * big l1_size, we don't want unbounded value to allocate the table.
432 * Limit it to 32M, which is enough to store:
433 * 8TB - for both VMDK3 & VMDK4 with
434 * minimal cluster size: 512B
435 * minimal L2 table size: 512 entries
436 * 8 TB is still more than the maximal value supported for
437 * VMDK3 & VMDK4 which is 2TB.
438 */
439 error_setg(errp, "L1 size too big");
440 return -EFBIG;
441 }
442
443 nb_sectors = bdrv_nb_sectors(file->bs);
444 if (nb_sectors < 0) {
445 return nb_sectors;
446 }
447
448 s->extents = g_renew(VmdkExtent, s->extents, s->num_extents + 1);
449 extent = &s->extents[s->num_extents];
450 s->num_extents++;
451
452 memset(extent, 0, sizeof(VmdkExtent));
453 extent->file = file;
454 extent->flat = flat;
455 extent->sectors = sectors;
456 extent->l1_table_offset = l1_offset;
457 extent->l1_backup_table_offset = l1_backup_offset;
458 extent->l1_size = l1_size;
459 extent->l1_entry_sectors = l2_size * cluster_sectors;
460 extent->l2_size = l2_size;
461 extent->cluster_sectors = flat ? sectors : cluster_sectors;
462 extent->next_cluster_sector = ROUND_UP(nb_sectors, cluster_sectors);
463
464 if (s->num_extents > 1) {
465 extent->end_sector = (*(extent - 1)).end_sector + extent->sectors;
466 } else {
467 extent->end_sector = extent->sectors;
468 }
469 bs->total_sectors = extent->end_sector;
470 if (new_extent) {
471 *new_extent = extent;
472 }
473 return 0;
474 }
475
476 static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
477 Error **errp)
478 {
479 int ret;
480 size_t l1_size;
481 int i;
482
483 /* read the L1 table */
484 l1_size = extent->l1_size * sizeof(uint32_t);
485 extent->l1_table = g_try_malloc(l1_size);
486 if (l1_size && extent->l1_table == NULL) {
487 return -ENOMEM;
488 }
489
490 ret = bdrv_pread(extent->file,
491 extent->l1_table_offset,
492 extent->l1_table,
493 l1_size);
494 if (ret < 0) {
495 bdrv_refresh_filename(extent->file->bs);
496 error_setg_errno(errp, -ret,
497 "Could not read l1 table from extent '%s'",
498 extent->file->bs->filename);
499 goto fail_l1;
500 }
501 for (i = 0; i < extent->l1_size; i++) {
502 le32_to_cpus(&extent->l1_table[i]);
503 }
504
505 if (extent->l1_backup_table_offset) {
506 extent->l1_backup_table = g_try_malloc(l1_size);
507 if (l1_size && extent->l1_backup_table == NULL) {
508 ret = -ENOMEM;
509 goto fail_l1;
510 }
511 ret = bdrv_pread(extent->file,
512 extent->l1_backup_table_offset,
513 extent->l1_backup_table,
514 l1_size);
515 if (ret < 0) {
516 bdrv_refresh_filename(extent->file->bs);
517 error_setg_errno(errp, -ret,
518 "Could not read l1 backup table from extent '%s'",
519 extent->file->bs->filename);
520 goto fail_l1b;
521 }
522 for (i = 0; i < extent->l1_size; i++) {
523 le32_to_cpus(&extent->l1_backup_table[i]);
524 }
525 }
526
527 extent->l2_cache =
528 g_new(uint32_t, extent->l2_size * L2_CACHE_SIZE);
529 return 0;
530 fail_l1b:
531 g_free(extent->l1_backup_table);
532 fail_l1:
533 g_free(extent->l1_table);
534 return ret;
535 }
536
537 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
538 BdrvChild *file,
539 int flags, Error **errp)
540 {
541 int ret;
542 uint32_t magic;
543 VMDK3Header header;
544 VmdkExtent *extent;
545
546 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
547 if (ret < 0) {
548 bdrv_refresh_filename(file->bs);
549 error_setg_errno(errp, -ret,
550 "Could not read header from file '%s'",
551 file->bs->filename);
552 return ret;
553 }
554 ret = vmdk_add_extent(bs, file, false,
555 le32_to_cpu(header.disk_sectors),
556 (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
557 0,
558 le32_to_cpu(header.l1dir_size),
559 4096,
560 le32_to_cpu(header.granularity),
561 &extent,
562 errp);
563 if (ret < 0) {
564 return ret;
565 }
566 ret = vmdk_init_tables(bs, extent, errp);
567 if (ret) {
568 /* free extent allocated by vmdk_add_extent */
569 vmdk_free_last_extent(bs);
570 }
571 return ret;
572 }
573
574 static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
575 QDict *options, Error **errp);
576
577 static char *vmdk_read_desc(BdrvChild *file, uint64_t desc_offset, Error **errp)
578 {
579 int64_t size;
580 char *buf;
581 int ret;
582
583 size = bdrv_getlength(file->bs);
584 if (size < 0) {
585 error_setg_errno(errp, -size, "Could not access file");
586 return NULL;
587 }
588
589 if (size < 4) {
590 /* Both descriptor file and sparse image must be much larger than 4
591 * bytes, also callers of vmdk_read_desc want to compare the first 4
592 * bytes with VMDK4_MAGIC, let's error out if less is read. */
593 error_setg(errp, "File is too small, not a valid image");
594 return NULL;
595 }
596
597 size = MIN(size, (1 << 20) - 1); /* avoid unbounded allocation */
598 buf = g_malloc(size + 1);
599
600 ret = bdrv_pread(file, desc_offset, buf, size);
601 if (ret < 0) {
602 error_setg_errno(errp, -ret, "Could not read from file");
603 g_free(buf);
604 return NULL;
605 }
606 buf[ret] = 0;
607
608 return buf;
609 }
610
611 static int vmdk_open_vmdk4(BlockDriverState *bs,
612 BdrvChild *file,
613 int flags, QDict *options, Error **errp)
614 {
615 int ret;
616 uint32_t magic;
617 uint32_t l1_size, l1_entry_sectors;
618 VMDK4Header header;
619 VmdkExtent *extent;
620 BDRVVmdkState *s = bs->opaque;
621 int64_t l1_backup_offset = 0;
622 bool compressed;
623
624 ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
625 if (ret < 0) {
626 bdrv_refresh_filename(file->bs);
627 error_setg_errno(errp, -ret,
628 "Could not read header from file '%s'",
629 file->bs->filename);
630 return -EINVAL;
631 }
632 if (header.capacity == 0) {
633 uint64_t desc_offset = le64_to_cpu(header.desc_offset);
634 if (desc_offset) {
635 char *buf = vmdk_read_desc(file, desc_offset << 9, errp);
636 if (!buf) {
637 return -EINVAL;
638 }
639 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
640 g_free(buf);
641 return ret;
642 }
643 }
644
645 if (!s->create_type) {
646 s->create_type = g_strdup("monolithicSparse");
647 }
648
649 if (le64_to_cpu(header.gd_offset) == VMDK4_GD_AT_END) {
650 /*
651 * The footer takes precedence over the header, so read it in. The
652 * footer starts at offset -1024 from the end: One sector for the
653 * footer, and another one for the end-of-stream marker.
654 */
655 struct {
656 struct {
657 uint64_t val;
658 uint32_t size;
659 uint32_t type;
660 uint8_t pad[512 - 16];
661 } QEMU_PACKED footer_marker;
662
663 uint32_t magic;
664 VMDK4Header header;
665 uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
666
667 struct {
668 uint64_t val;
669 uint32_t size;
670 uint32_t type;
671 uint8_t pad[512 - 16];
672 } QEMU_PACKED eos_marker;
673 } QEMU_PACKED footer;
674
675 ret = bdrv_pread(file,
676 bs->file->bs->total_sectors * 512 - 1536,
677 &footer, sizeof(footer));
678 if (ret < 0) {
679 error_setg_errno(errp, -ret, "Failed to read footer");
680 return ret;
681 }
682
683 /* Some sanity checks for the footer */
684 if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
685 le32_to_cpu(footer.footer_marker.size) != 0 ||
686 le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
687 le64_to_cpu(footer.eos_marker.val) != 0 ||
688 le32_to_cpu(footer.eos_marker.size) != 0 ||
689 le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
690 {
691 error_setg(errp, "Invalid footer");
692 return -EINVAL;
693 }
694
695 header = footer.header;
696 }
697
698 compressed =
699 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
700 if (le32_to_cpu(header.version) > 3) {
701 error_setg(errp, "Unsupported VMDK version %" PRIu32,
702 le32_to_cpu(header.version));
703 return -ENOTSUP;
704 } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR) &&
705 !compressed) {
706 /* VMware KB 2064959 explains that version 3 added support for
707 * persistent changed block tracking (CBT), and backup software can
708 * read it as version=1 if it doesn't care about the changed area
709 * information. So we are safe to enable read only. */
710 error_setg(errp, "VMDK version 3 must be read only");
711 return -EINVAL;
712 }
713
714 if (le32_to_cpu(header.num_gtes_per_gt) > 512) {
715 error_setg(errp, "L2 table size too big");
716 return -EINVAL;
717 }
718
719 l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gt)
720 * le64_to_cpu(header.granularity);
721 if (l1_entry_sectors == 0) {
722 error_setg(errp, "L1 entry size is invalid");
723 return -EINVAL;
724 }
725 l1_size = (le64_to_cpu(header.capacity) + l1_entry_sectors - 1)
726 / l1_entry_sectors;
727 if (le32_to_cpu(header.flags) & VMDK4_FLAG_RGD) {
728 l1_backup_offset = le64_to_cpu(header.rgd_offset) << 9;
729 }
730 if (bdrv_nb_sectors(file->bs) < le64_to_cpu(header.grain_offset)) {
731 error_setg(errp, "File truncated, expecting at least %" PRId64 " bytes",
732 (int64_t)(le64_to_cpu(header.grain_offset)
733 * BDRV_SECTOR_SIZE));
734 return -EINVAL;
735 }
736
737 ret = vmdk_add_extent(bs, file, false,
738 le64_to_cpu(header.capacity),
739 le64_to_cpu(header.gd_offset) << 9,
740 l1_backup_offset,
741 l1_size,
742 le32_to_cpu(header.num_gtes_per_gt),
743 le64_to_cpu(header.granularity),
744 &extent,
745 errp);
746 if (ret < 0) {
747 return ret;
748 }
749 extent->compressed =
750 le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
751 if (extent->compressed) {
752 g_free(s->create_type);
753 s->create_type = g_strdup("streamOptimized");
754 }
755 extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
756 extent->version = le32_to_cpu(header.version);
757 extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
758 ret = vmdk_init_tables(bs, extent, errp);
759 if (ret) {
760 /* free extent allocated by vmdk_add_extent */
761 vmdk_free_last_extent(bs);
762 }
763 return ret;
764 }
765
766 /* find an option value out of descriptor file */
767 static int vmdk_parse_description(const char *desc, const char *opt_name,
768 char *buf, int buf_size)
769 {
770 char *opt_pos, *opt_end;
771 const char *end = desc + strlen(desc);
772
773 opt_pos = strstr(desc, opt_name);
774 if (!opt_pos) {
775 return VMDK_ERROR;
776 }
777 /* Skip "=\"" following opt_name */
778 opt_pos += strlen(opt_name) + 2;
779 if (opt_pos >= end) {
780 return VMDK_ERROR;
781 }
782 opt_end = opt_pos;
783 while (opt_end < end && *opt_end != '"') {
784 opt_end++;
785 }
786 if (opt_end == end || buf_size < opt_end - opt_pos + 1) {
787 return VMDK_ERROR;
788 }
789 pstrcpy(buf, opt_end - opt_pos + 1, opt_pos);
790 return VMDK_OK;
791 }
792
793 /* Open an extent file and append to bs array */
794 static int vmdk_open_sparse(BlockDriverState *bs, BdrvChild *file, int flags,
795 char *buf, QDict *options, Error **errp)
796 {
797 uint32_t magic;
798
799 magic = ldl_be_p(buf);
800 switch (magic) {
801 case VMDK3_MAGIC:
802 return vmdk_open_vmfs_sparse(bs, file, flags, errp);
803 break;
804 case VMDK4_MAGIC:
805 return vmdk_open_vmdk4(bs, file, flags, options, errp);
806 break;
807 default:
808 error_setg(errp, "Image not in VMDK format");
809 return -EINVAL;
810 break;
811 }
812 }
813
814 static const char *next_line(const char *s)
815 {
816 while (*s) {
817 if (*s == '\n') {
818 return s + 1;
819 }
820 s++;
821 }
822 return s;
823 }
824
825 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
826 const char *desc_file_path, QDict *options,
827 Error **errp)
828 {
829 int ret;
830 int matches;
831 char access[11];
832 char type[11];
833 char fname[512];
834 const char *p, *np;
835 int64_t sectors = 0;
836 int64_t flat_offset;
837 char *extent_path;
838 BdrvChild *extent_file;
839 BDRVVmdkState *s = bs->opaque;
840 VmdkExtent *extent;
841 char extent_opt_prefix[32];
842 Error *local_err = NULL;
843
844 for (p = desc; *p; p = next_line(p)) {
845 /* parse extent line in one of below formats:
846 *
847 * RW [size in sectors] FLAT "file-name.vmdk" OFFSET
848 * RW [size in sectors] SPARSE "file-name.vmdk"
849 * RW [size in sectors] VMFS "file-name.vmdk"
850 * RW [size in sectors] VMFSSPARSE "file-name.vmdk"
851 */
852 flat_offset = -1;
853 matches = sscanf(p, "%10s %" SCNd64 " %10s \"%511[^\n\r\"]\" %" SCNd64,
854 access, &sectors, type, fname, &flat_offset);
855 if (matches < 4 || strcmp(access, "RW")) {
856 continue;
857 } else if (!strcmp(type, "FLAT")) {
858 if (matches != 5 || flat_offset < 0) {
859 goto invalid;
860 }
861 } else if (!strcmp(type, "VMFS")) {
862 if (matches == 4) {
863 flat_offset = 0;
864 } else {
865 goto invalid;
866 }
867 } else if (matches != 4) {
868 goto invalid;
869 }
870
871 if (sectors <= 0 ||
872 (strcmp(type, "FLAT") && strcmp(type, "SPARSE") &&
873 strcmp(type, "VMFS") && strcmp(type, "VMFSSPARSE")) ||
874 (strcmp(access, "RW"))) {
875 continue;
876 }
877
878 if (!path_is_absolute(fname) && !path_has_protocol(fname) &&
879 !desc_file_path[0])
880 {
881 bdrv_refresh_filename(bs->file->bs);
882 error_setg(errp, "Cannot use relative extent paths with VMDK "
883 "descriptor file '%s'", bs->file->bs->filename);
884 return -EINVAL;
885 }
886
887 extent_path = path_combine(desc_file_path, fname);
888
889 ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
890 assert(ret < 32);
891
892 extent_file = bdrv_open_child(extent_path, options, extent_opt_prefix,
893 bs, &child_file, false, &local_err);
894 g_free(extent_path);
895 if (local_err) {
896 error_propagate(errp, local_err);
897 return -EINVAL;
898 }
899
900 /* save to extents array */
901 if (!strcmp(type, "FLAT") || !strcmp(type, "VMFS")) {
902 /* FLAT extent */
903
904 ret = vmdk_add_extent(bs, extent_file, true, sectors,
905 0, 0, 0, 0, 0, &extent, errp);
906 if (ret < 0) {
907 bdrv_unref_child(bs, extent_file);
908 return ret;
909 }
910 extent->flat_start_offset = flat_offset << 9;
911 } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
912 /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
913 char *buf = vmdk_read_desc(extent_file, 0, errp);
914 if (!buf) {
915 ret = -EINVAL;
916 } else {
917 ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
918 options, errp);
919 }
920 g_free(buf);
921 if (ret) {
922 bdrv_unref_child(bs, extent_file);
923 return ret;
924 }
925 extent = &s->extents[s->num_extents - 1];
926 } else {
927 error_setg(errp, "Unsupported extent type '%s'", type);
928 bdrv_unref_child(bs, extent_file);
929 return -ENOTSUP;
930 }
931 extent->type = g_strdup(type);
932 }
933 return 0;
934
935 invalid:
936 np = next_line(p);
937 assert(np != p);
938 if (np[-1] == '\n') {
939 np--;
940 }
941 error_setg(errp, "Invalid extent line: %.*s", (int)(np - p), p);
942 return -EINVAL;
943 }
944
945 static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
946 QDict *options, Error **errp)
947 {
948 int ret;
949 char ct[128];
950 BDRVVmdkState *s = bs->opaque;
951
952 if (vmdk_parse_description(buf, "createType", ct, sizeof(ct))) {
953 error_setg(errp, "invalid VMDK image descriptor");
954 ret = -EINVAL;
955 goto exit;
956 }
957 if (strcmp(ct, "monolithicFlat") &&
958 strcmp(ct, "vmfs") &&
959 strcmp(ct, "vmfsSparse") &&
960 strcmp(ct, "twoGbMaxExtentSparse") &&
961 strcmp(ct, "twoGbMaxExtentFlat")) {
962 error_setg(errp, "Unsupported image type '%s'", ct);
963 ret = -ENOTSUP;
964 goto exit;
965 }
966 s->create_type = g_strdup(ct);
967 s->desc_offset = 0;
968 ret = vmdk_parse_extents(buf, bs, bs->file->bs->exact_filename, options,
969 errp);
970 exit:
971 return ret;
972 }
973
974 static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
975 Error **errp)
976 {
977 char *buf;
978 int ret;
979 BDRVVmdkState *s = bs->opaque;
980 uint32_t magic;
981 Error *local_err = NULL;
982
983 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
984 false, errp);
985 if (!bs->file) {
986 return -EINVAL;
987 }
988
989 buf = vmdk_read_desc(bs->file, 0, errp);
990 if (!buf) {
991 return -EINVAL;
992 }
993
994 magic = ldl_be_p(buf);
995 switch (magic) {
996 case VMDK3_MAGIC:
997 case VMDK4_MAGIC:
998 ret = vmdk_open_sparse(bs, bs->file, flags, buf, options,
999 errp);
1000 s->desc_offset = 0x200;
1001 break;
1002 default:
1003 ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
1004 break;
1005 }
1006 if (ret) {
1007 goto fail;
1008 }
1009
1010 /* try to open parent images, if exist */
1011 ret = vmdk_parent_open(bs);
1012 if (ret) {
1013 goto fail;
1014 }
1015 ret = vmdk_read_cid(bs, 0, &s->cid);
1016 if (ret) {
1017 goto fail;
1018 }
1019 ret = vmdk_read_cid(bs, 1, &s->parent_cid);
1020 if (ret) {
1021 goto fail;
1022 }
1023 qemu_co_mutex_init(&s->lock);
1024
1025 /* Disable migration when VMDK images are used */
1026 error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
1027 "does not support live migration",
1028 bdrv_get_device_or_node_name(bs));
1029 ret = migrate_add_blocker(s->migration_blocker, &local_err);
1030 if (local_err) {
1031 error_propagate(errp, local_err);
1032 error_free(s->migration_blocker);
1033 goto fail;
1034 }
1035
1036 g_free(buf);
1037 return 0;
1038
1039 fail:
1040 g_free(buf);
1041 g_free(s->create_type);
1042 s->create_type = NULL;
1043 vmdk_free_extents(bs);
1044 return ret;
1045 }
1046
1047
1048 static void vmdk_refresh_limits(BlockDriverState *bs, Error **errp)
1049 {
1050 BDRVVmdkState *s = bs->opaque;
1051 int i;
1052
1053 for (i = 0; i < s->num_extents; i++) {
1054 if (!s->extents[i].flat) {
1055 bs->bl.pwrite_zeroes_alignment =
1056 MAX(bs->bl.pwrite_zeroes_alignment,
1057 s->extents[i].cluster_sectors << BDRV_SECTOR_BITS);
1058 }
1059 }
1060 }
1061
1062 /**
1063 * get_whole_cluster
1064 *
1065 * Copy backing file's cluster that covers @sector_num, otherwise write zero,
1066 * to the cluster at @cluster_sector_num.
1067 *
1068 * If @skip_start_sector < @skip_end_sector, the relative range
1069 * [@skip_start_sector, @skip_end_sector) is not copied or written, and leave
1070 * it for call to write user data in the request.
1071 */
1072 static int get_whole_cluster(BlockDriverState *bs,
1073 VmdkExtent *extent,
1074 uint64_t cluster_offset,
1075 uint64_t offset,
1076 uint64_t skip_start_bytes,
1077 uint64_t skip_end_bytes)
1078 {
1079 int ret = VMDK_OK;
1080 int64_t cluster_bytes;
1081 uint8_t *whole_grain;
1082
1083 /* For COW, align request sector_num to cluster start */
1084 cluster_bytes = extent->cluster_sectors << BDRV_SECTOR_BITS;
1085 offset = QEMU_ALIGN_DOWN(offset, cluster_bytes);
1086 whole_grain = qemu_blockalign(bs, cluster_bytes);
1087
1088 if (!bs->backing) {
1089 memset(whole_grain, 0, skip_start_bytes);
1090 memset(whole_grain + skip_end_bytes, 0, cluster_bytes - skip_end_bytes);
1091 }
1092
1093 assert(skip_end_bytes <= cluster_bytes);
1094 /* we will be here if it's first write on non-exist grain(cluster).
1095 * try to read from parent image, if exist */
1096 if (bs->backing && !vmdk_is_cid_valid(bs)) {
1097 ret = VMDK_ERROR;
1098 goto exit;
1099 }
1100
1101 /* Read backing data before skip range */
1102 if (skip_start_bytes > 0) {
1103 if (bs->backing) {
1104 /* qcow2 emits this on bs->file instead of bs->backing */
1105 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
1106 ret = bdrv_pread(bs->backing, offset, whole_grain,
1107 skip_start_bytes);
1108 if (ret < 0) {
1109 ret = VMDK_ERROR;
1110 goto exit;
1111 }
1112 }
1113 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
1114 ret = bdrv_pwrite(extent->file, cluster_offset, whole_grain,
1115 skip_start_bytes);
1116 if (ret < 0) {
1117 ret = VMDK_ERROR;
1118 goto exit;
1119 }
1120 }
1121 /* Read backing data after skip range */
1122 if (skip_end_bytes < cluster_bytes) {
1123 if (bs->backing) {
1124 /* qcow2 emits this on bs->file instead of bs->backing */
1125 BLKDBG_EVENT(extent->file, BLKDBG_COW_READ);
1126 ret = bdrv_pread(bs->backing, offset + skip_end_bytes,
1127 whole_grain + skip_end_bytes,
1128 cluster_bytes - skip_end_bytes);
1129 if (ret < 0) {
1130 ret = VMDK_ERROR;
1131 goto exit;
1132 }
1133 }
1134 BLKDBG_EVENT(extent->file, BLKDBG_COW_WRITE);
1135 ret = bdrv_pwrite(extent->file, cluster_offset + skip_end_bytes,
1136 whole_grain + skip_end_bytes,
1137 cluster_bytes - skip_end_bytes);
1138 if (ret < 0) {
1139 ret = VMDK_ERROR;
1140 goto exit;
1141 }
1142 }
1143
1144 ret = VMDK_OK;
1145 exit:
1146 qemu_vfree(whole_grain);
1147 return ret;
1148 }
1149
1150 static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data,
1151 uint32_t offset)
1152 {
1153 offset = cpu_to_le32(offset);
1154 /* update L2 table */
1155 BLKDBG_EVENT(extent->file, BLKDBG_L2_UPDATE);
1156 if (bdrv_pwrite_sync(extent->file,
1157 ((int64_t)m_data->l2_offset * 512)
1158 + (m_data->l2_index * sizeof(offset)),
1159 &offset, sizeof(offset)) < 0) {
1160 return VMDK_ERROR;
1161 }
1162 /* update backup L2 table */
1163 if (extent->l1_backup_table_offset != 0) {
1164 m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
1165 if (bdrv_pwrite_sync(extent->file,
1166 ((int64_t)m_data->l2_offset * 512)
1167 + (m_data->l2_index * sizeof(offset)),
1168 &offset, sizeof(offset)) < 0) {
1169 return VMDK_ERROR;
1170 }
1171 }
1172 if (m_data->l2_cache_entry) {
1173 *m_data->l2_cache_entry = offset;
1174 }
1175
1176 return VMDK_OK;
1177 }
1178
1179 /**
1180 * get_cluster_offset
1181 *
1182 * Look up cluster offset in extent file by sector number, and store in
1183 * @cluster_offset.
1184 *
1185 * For flat extents, the start offset as parsed from the description file is
1186 * returned.
1187 *
1188 * For sparse extents, look up in L1, L2 table. If allocate is true, return an
1189 * offset for a new cluster and update L2 cache. If there is a backing file,
1190 * COW is done before returning; otherwise, zeroes are written to the allocated
1191 * cluster. Both COW and zero writing skips the sector range
1192 * [@skip_start_sector, @skip_end_sector) passed in by caller, because caller
1193 * has new data to write there.
1194 *
1195 * Returns: VMDK_OK if cluster exists and mapped in the image.
1196 * VMDK_UNALLOC if cluster is not mapped and @allocate is false.
1197 * VMDK_ERROR if failed.
1198 */
1199 static int get_cluster_offset(BlockDriverState *bs,
1200 VmdkExtent *extent,
1201 VmdkMetaData *m_data,
1202 uint64_t offset,
1203 bool allocate,
1204 uint64_t *cluster_offset,
1205 uint64_t skip_start_bytes,
1206 uint64_t skip_end_bytes)
1207 {
1208 unsigned int l1_index, l2_offset, l2_index;
1209 int min_index, i, j;
1210 uint32_t min_count, *l2_table;
1211 bool zeroed = false;
1212 int64_t ret;
1213 int64_t cluster_sector;
1214
1215 if (m_data) {
1216 m_data->valid = 0;
1217 }
1218 if (extent->flat) {
1219 *cluster_offset = extent->flat_start_offset;
1220 return VMDK_OK;
1221 }
1222
1223 offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
1224 l1_index = (offset >> 9) / extent->l1_entry_sectors;
1225 if (l1_index >= extent->l1_size) {
1226 return VMDK_ERROR;
1227 }
1228 l2_offset = extent->l1_table[l1_index];
1229 if (!l2_offset) {
1230 return VMDK_UNALLOC;
1231 }
1232 for (i = 0; i < L2_CACHE_SIZE; i++) {
1233 if (l2_offset == extent->l2_cache_offsets[i]) {
1234 /* increment the hit count */
1235 if (++extent->l2_cache_counts[i] == 0xffffffff) {
1236 for (j = 0; j < L2_CACHE_SIZE; j++) {
1237 extent->l2_cache_counts[j] >>= 1;
1238 }
1239 }
1240 l2_table = extent->l2_cache + (i * extent->l2_size);
1241 goto found;
1242 }
1243 }
1244 /* not found: load a new entry in the least used one */
1245 min_index = 0;
1246 min_count = 0xffffffff;
1247 for (i = 0; i < L2_CACHE_SIZE; i++) {
1248 if (extent->l2_cache_counts[i] < min_count) {
1249 min_count = extent->l2_cache_counts[i];
1250 min_index = i;
1251 }
1252 }
1253 l2_table = extent->l2_cache + (min_index * extent->l2_size);
1254 BLKDBG_EVENT(extent->file, BLKDBG_L2_LOAD);
1255 if (bdrv_pread(extent->file,
1256 (int64_t)l2_offset * 512,
1257 l2_table,
1258 extent->l2_size * sizeof(uint32_t)
1259 ) != extent->l2_size * sizeof(uint32_t)) {
1260 return VMDK_ERROR;
1261 }
1262
1263 extent->l2_cache_offsets[min_index] = l2_offset;
1264 extent->l2_cache_counts[min_index] = 1;
1265 found:
1266 l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
1267 cluster_sector = le32_to_cpu(l2_table[l2_index]);
1268
1269 if (extent->has_zero_grain && cluster_sector == VMDK_GTE_ZEROED) {
1270 zeroed = true;
1271 }
1272
1273 if (!cluster_sector || zeroed) {
1274 if (!allocate) {
1275 return zeroed ? VMDK_ZEROED : VMDK_UNALLOC;
1276 }
1277
1278 if (extent->next_cluster_sector >= VMDK_EXTENT_MAX_SECTORS) {
1279 return VMDK_ERROR;
1280 }
1281
1282 cluster_sector = extent->next_cluster_sector;
1283 extent->next_cluster_sector += extent->cluster_sectors;
1284
1285 /* First of all we write grain itself, to avoid race condition
1286 * that may to corrupt the image.
1287 * This problem may occur because of insufficient space on host disk
1288 * or inappropriate VM shutdown.
1289 */
1290 ret = get_whole_cluster(bs, extent, cluster_sector * BDRV_SECTOR_SIZE,
1291 offset, skip_start_bytes, skip_end_bytes);
1292 if (ret) {
1293 return ret;
1294 }
1295 if (m_data) {
1296 m_data->valid = 1;
1297 m_data->l1_index = l1_index;
1298 m_data->l2_index = l2_index;
1299 m_data->l2_offset = l2_offset;
1300 m_data->l2_cache_entry = &l2_table[l2_index];
1301 }
1302 }
1303 *cluster_offset = cluster_sector << BDRV_SECTOR_BITS;
1304 return VMDK_OK;
1305 }
1306
1307 static VmdkExtent *find_extent(BDRVVmdkState *s,
1308 int64_t sector_num, VmdkExtent *start_hint)
1309 {
1310 VmdkExtent *extent = start_hint;
1311
1312 if (!extent) {
1313 extent = &s->extents[0];
1314 }
1315 while (extent < &s->extents[s->num_extents]) {
1316 if (sector_num < extent->end_sector) {
1317 return extent;
1318 }
1319 extent++;
1320 }
1321 return NULL;
1322 }
1323
1324 static inline uint64_t vmdk_find_offset_in_cluster(VmdkExtent *extent,
1325 int64_t offset)
1326 {
1327 uint64_t extent_begin_offset, extent_relative_offset;
1328 uint64_t cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE;
1329
1330 extent_begin_offset =
1331 (extent->end_sector - extent->sectors) * BDRV_SECTOR_SIZE;
1332 extent_relative_offset = offset - extent_begin_offset;
1333 return extent_relative_offset % cluster_size;
1334 }
1335
1336 static int coroutine_fn vmdk_co_block_status(BlockDriverState *bs,
1337 bool want_zero,
1338 int64_t offset, int64_t bytes,
1339 int64_t *pnum, int64_t *map,
1340 BlockDriverState **file)
1341 {
1342 BDRVVmdkState *s = bs->opaque;
1343 int64_t index_in_cluster, n, ret;
1344 uint64_t cluster_offset;
1345 VmdkExtent *extent;
1346
1347 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, NULL);
1348 if (!extent) {
1349 return -EIO;
1350 }
1351 qemu_co_mutex_lock(&s->lock);
1352 ret = get_cluster_offset(bs, extent, NULL, offset, false, &cluster_offset,
1353 0, 0);
1354 qemu_co_mutex_unlock(&s->lock);
1355
1356 index_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1357 switch (ret) {
1358 case VMDK_ERROR:
1359 ret = -EIO;
1360 break;
1361 case VMDK_UNALLOC:
1362 ret = 0;
1363 break;
1364 case VMDK_ZEROED:
1365 ret = BDRV_BLOCK_ZERO;
1366 break;
1367 case VMDK_OK:
1368 ret = BDRV_BLOCK_DATA;
1369 if (!extent->compressed) {
1370 ret |= BDRV_BLOCK_OFFSET_VALID;
1371 *map = cluster_offset + index_in_cluster;
1372 }
1373 *file = extent->file->bs;
1374 break;
1375 }
1376
1377 n = extent->cluster_sectors * BDRV_SECTOR_SIZE - index_in_cluster;
1378 *pnum = MIN(n, bytes);
1379 return ret;
1380 }
1381
1382 static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
1383 int64_t offset_in_cluster, QEMUIOVector *qiov,
1384 uint64_t qiov_offset, uint64_t n_bytes,
1385 uint64_t offset)
1386 {
1387 int ret;
1388 VmdkGrainMarker *data = NULL;
1389 uLongf buf_len;
1390 QEMUIOVector local_qiov;
1391 int64_t write_offset;
1392 int64_t write_end_sector;
1393
1394 if (extent->compressed) {
1395 void *compressed_data;
1396
1397 if (!extent->has_marker) {
1398 ret = -EINVAL;
1399 goto out;
1400 }
1401 buf_len = (extent->cluster_sectors << 9) * 2;
1402 data = g_malloc(buf_len + sizeof(VmdkGrainMarker));
1403
1404 compressed_data = g_malloc(n_bytes);
1405 qemu_iovec_to_buf(qiov, qiov_offset, compressed_data, n_bytes);
1406 ret = compress(data->data, &buf_len, compressed_data, n_bytes);
1407 g_free(compressed_data);
1408
1409 if (ret != Z_OK || buf_len == 0) {
1410 ret = -EINVAL;
1411 goto out;
1412 }
1413
1414 data->lba = cpu_to_le64(offset >> BDRV_SECTOR_BITS);
1415 data->size = cpu_to_le32(buf_len);
1416
1417 n_bytes = buf_len + sizeof(VmdkGrainMarker);
1418 qemu_iovec_init_buf(&local_qiov, data, n_bytes);
1419
1420 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_COMPRESSED);
1421 } else {
1422 qemu_iovec_init(&local_qiov, qiov->niov);
1423 qemu_iovec_concat(&local_qiov, qiov, qiov_offset, n_bytes);
1424
1425 BLKDBG_EVENT(extent->file, BLKDBG_WRITE_AIO);
1426 }
1427
1428 write_offset = cluster_offset + offset_in_cluster;
1429 ret = bdrv_co_pwritev(extent->file, write_offset, n_bytes,
1430 &local_qiov, 0);
1431
1432 write_end_sector = DIV_ROUND_UP(write_offset + n_bytes, BDRV_SECTOR_SIZE);
1433
1434 if (extent->compressed) {
1435 extent->next_cluster_sector = write_end_sector;
1436 } else {
1437 extent->next_cluster_sector = MAX(extent->next_cluster_sector,
1438 write_end_sector);
1439 }
1440
1441 if (ret < 0) {
1442 goto out;
1443 }
1444 ret = 0;
1445 out:
1446 g_free(data);
1447 if (!extent->compressed) {
1448 qemu_iovec_destroy(&local_qiov);
1449 }
1450 return ret;
1451 }
1452
1453 static int vmdk_read_extent(VmdkExtent *extent, int64_t cluster_offset,
1454 int64_t offset_in_cluster, QEMUIOVector *qiov,
1455 int bytes)
1456 {
1457 int ret;
1458 int cluster_bytes, buf_bytes;
1459 uint8_t *cluster_buf, *compressed_data;
1460 uint8_t *uncomp_buf;
1461 uint32_t data_len;
1462 VmdkGrainMarker *marker;
1463 uLongf buf_len;
1464
1465
1466 if (!extent->compressed) {
1467 BLKDBG_EVENT(extent->file, BLKDBG_READ_AIO);
1468 ret = bdrv_co_preadv(extent->file,
1469 cluster_offset + offset_in_cluster, bytes,
1470 qiov, 0);
1471 if (ret < 0) {
1472 return ret;
1473 }
1474 return 0;
1475 }
1476 cluster_bytes = extent->cluster_sectors * 512;
1477 /* Read two clusters in case GrainMarker + compressed data > one cluster */
1478 buf_bytes = cluster_bytes * 2;
1479 cluster_buf = g_malloc(buf_bytes);
1480 uncomp_buf = g_malloc(cluster_bytes);
1481 BLKDBG_EVENT(extent->file, BLKDBG_READ_COMPRESSED);
1482 ret = bdrv_pread(extent->file,
1483 cluster_offset,
1484 cluster_buf, buf_bytes);
1485 if (ret < 0) {
1486 goto out;
1487 }
1488 compressed_data = cluster_buf;
1489 buf_len = cluster_bytes;
1490 data_len = cluster_bytes;
1491 if (extent->has_marker) {
1492 marker = (VmdkGrainMarker *)cluster_buf;
1493 compressed_data = marker->data;
1494 data_len = le32_to_cpu(marker->size);
1495 }
1496 if (!data_len || data_len > buf_bytes) {
1497 ret = -EINVAL;
1498 goto out;
1499 }
1500 ret = uncompress(uncomp_buf, &buf_len, compressed_data, data_len);
1501 if (ret != Z_OK) {
1502 ret = -EINVAL;
1503 goto out;
1504
1505 }
1506 if (offset_in_cluster < 0 ||
1507 offset_in_cluster + bytes > buf_len) {
1508 ret = -EINVAL;
1509 goto out;
1510 }
1511 qemu_iovec_from_buf(qiov, 0, uncomp_buf + offset_in_cluster, bytes);
1512 ret = 0;
1513
1514 out:
1515 g_free(uncomp_buf);
1516 g_free(cluster_buf);
1517 return ret;
1518 }
1519
1520 static int coroutine_fn
1521 vmdk_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1522 QEMUIOVector *qiov, int flags)
1523 {
1524 BDRVVmdkState *s = bs->opaque;
1525 int ret;
1526 uint64_t n_bytes, offset_in_cluster;
1527 VmdkExtent *extent = NULL;
1528 QEMUIOVector local_qiov;
1529 uint64_t cluster_offset;
1530 uint64_t bytes_done = 0;
1531
1532 qemu_iovec_init(&local_qiov, qiov->niov);
1533 qemu_co_mutex_lock(&s->lock);
1534
1535 while (bytes > 0) {
1536 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
1537 if (!extent) {
1538 ret = -EIO;
1539 goto fail;
1540 }
1541 ret = get_cluster_offset(bs, extent, NULL,
1542 offset, false, &cluster_offset, 0, 0);
1543 offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1544
1545 n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1546 - offset_in_cluster);
1547
1548 if (ret != VMDK_OK) {
1549 /* if not allocated, try to read from parent image, if exist */
1550 if (bs->backing && ret != VMDK_ZEROED) {
1551 if (!vmdk_is_cid_valid(bs)) {
1552 ret = -EINVAL;
1553 goto fail;
1554 }
1555
1556 qemu_iovec_reset(&local_qiov);
1557 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1558
1559 /* qcow2 emits this on bs->file instead of bs->backing */
1560 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1561 ret = bdrv_co_preadv(bs->backing, offset, n_bytes,
1562 &local_qiov, 0);
1563 if (ret < 0) {
1564 goto fail;
1565 }
1566 } else {
1567 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
1568 }
1569 } else {
1570 qemu_iovec_reset(&local_qiov);
1571 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
1572
1573 ret = vmdk_read_extent(extent, cluster_offset, offset_in_cluster,
1574 &local_qiov, n_bytes);
1575 if (ret) {
1576 goto fail;
1577 }
1578 }
1579 bytes -= n_bytes;
1580 offset += n_bytes;
1581 bytes_done += n_bytes;
1582 }
1583
1584 ret = 0;
1585 fail:
1586 qemu_co_mutex_unlock(&s->lock);
1587 qemu_iovec_destroy(&local_qiov);
1588
1589 return ret;
1590 }
1591
1592 /**
1593 * vmdk_write:
1594 * @zeroed: buf is ignored (data is zero), use zeroed_grain GTE feature
1595 * if possible, otherwise return -ENOTSUP.
1596 * @zero_dry_run: used for zeroed == true only, don't update L2 table, just try
1597 * with each cluster. By dry run we can find if the zero write
1598 * is possible without modifying image data.
1599 *
1600 * Returns: error code with 0 for success.
1601 */
1602 static int vmdk_pwritev(BlockDriverState *bs, uint64_t offset,
1603 uint64_t bytes, QEMUIOVector *qiov,
1604 bool zeroed, bool zero_dry_run)
1605 {
1606 BDRVVmdkState *s = bs->opaque;
1607 VmdkExtent *extent = NULL;
1608 int ret;
1609 int64_t offset_in_cluster, n_bytes;
1610 uint64_t cluster_offset;
1611 uint64_t bytes_done = 0;
1612 VmdkMetaData m_data;
1613
1614 if (DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE) > bs->total_sectors) {
1615 error_report("Wrong offset: offset=0x%" PRIx64
1616 " total_sectors=0x%" PRIx64,
1617 offset, bs->total_sectors);
1618 return -EIO;
1619 }
1620
1621 while (bytes > 0) {
1622 extent = find_extent(s, offset >> BDRV_SECTOR_BITS, extent);
1623 if (!extent) {
1624 return -EIO;
1625 }
1626 offset_in_cluster = vmdk_find_offset_in_cluster(extent, offset);
1627 n_bytes = MIN(bytes, extent->cluster_sectors * BDRV_SECTOR_SIZE
1628 - offset_in_cluster);
1629
1630 ret = get_cluster_offset(bs, extent, &m_data, offset,
1631 !(extent->compressed || zeroed),
1632 &cluster_offset, offset_in_cluster,
1633 offset_in_cluster + n_bytes);
1634 if (extent->compressed) {
1635 if (ret == VMDK_OK) {
1636 /* Refuse write to allocated cluster for streamOptimized */
1637 error_report("Could not write to allocated cluster"
1638 " for streamOptimized");
1639 return -EIO;
1640 } else {
1641 /* allocate */
1642 ret = get_cluster_offset(bs, extent, &m_data, offset,
1643 true, &cluster_offset, 0, 0);
1644 }
1645 }
1646 if (ret == VMDK_ERROR) {
1647 return -EINVAL;
1648 }
1649 if (zeroed) {
1650 /* Do zeroed write, buf is ignored */
1651 if (extent->has_zero_grain &&
1652 offset_in_cluster == 0 &&
1653 n_bytes >= extent->cluster_sectors * BDRV_SECTOR_SIZE) {
1654 n_bytes = extent->cluster_sectors * BDRV_SECTOR_SIZE;
1655 if (!zero_dry_run) {
1656 /* update L2 tables */
1657 if (vmdk_L2update(extent, &m_data, VMDK_GTE_ZEROED)
1658 != VMDK_OK) {
1659 return -EIO;
1660 }
1661 }
1662 } else {
1663 return -ENOTSUP;
1664 }
1665 } else {
1666 ret = vmdk_write_extent(extent, cluster_offset, offset_in_cluster,
1667 qiov, bytes_done, n_bytes, offset);
1668 if (ret) {
1669 return ret;
1670 }
1671 if (m_data.valid) {
1672 /* update L2 tables */
1673 if (vmdk_L2update(extent, &m_data,
1674 cluster_offset >> BDRV_SECTOR_BITS)
1675 != VMDK_OK) {
1676 return -EIO;
1677 }
1678 }
1679 }
1680 bytes -= n_bytes;
1681 offset += n_bytes;
1682 bytes_done += n_bytes;
1683
1684 /* update CID on the first write every time the virtual disk is
1685 * opened */
1686 if (!s->cid_updated) {
1687 ret = vmdk_write_cid(bs, g_random_int());
1688 if (ret < 0) {
1689 return ret;
1690 }
1691 s->cid_updated = true;
1692 }
1693 }
1694 return 0;
1695 }
1696
1697 static int coroutine_fn
1698 vmdk_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1699 QEMUIOVector *qiov, int flags)
1700 {
1701 int ret;
1702 BDRVVmdkState *s = bs->opaque;
1703 qemu_co_mutex_lock(&s->lock);
1704 ret = vmdk_pwritev(bs, offset, bytes, qiov, false, false);
1705 qemu_co_mutex_unlock(&s->lock);
1706 return ret;
1707 }
1708
1709 static int coroutine_fn
1710 vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
1711 uint64_t bytes, QEMUIOVector *qiov)
1712 {
1713 if (bytes == 0) {
1714 /* The caller will write bytes 0 to signal EOF.
1715 * When receive it, we align EOF to a sector boundary. */
1716 BDRVVmdkState *s = bs->opaque;
1717 int i, ret;
1718 int64_t length;
1719
1720 for (i = 0; i < s->num_extents; i++) {
1721 length = bdrv_getlength(s->extents[i].file->bs);
1722 if (length < 0) {
1723 return length;
1724 }
1725 length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);
1726 ret = bdrv_truncate(s->extents[i].file, length,
1727 PREALLOC_MODE_OFF, NULL);
1728 if (ret < 0) {
1729 return ret;
1730 }
1731 }
1732 return 0;
1733 }
1734 return vmdk_co_pwritev(bs, offset, bytes, qiov, 0);
1735 }
1736
1737 static int coroutine_fn vmdk_co_pwrite_zeroes(BlockDriverState *bs,
1738 int64_t offset,
1739 int bytes,
1740 BdrvRequestFlags flags)
1741 {
1742 int ret;
1743 BDRVVmdkState *s = bs->opaque;
1744
1745 qemu_co_mutex_lock(&s->lock);
1746 /* write zeroes could fail if sectors not aligned to cluster, test it with
1747 * dry_run == true before really updating image */
1748 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, true);
1749 if (!ret) {
1750 ret = vmdk_pwritev(bs, offset, bytes, NULL, true, false);
1751 }
1752 qemu_co_mutex_unlock(&s->lock);
1753 return ret;
1754 }
1755
1756 static int vmdk_init_extent(BlockBackend *blk,
1757 int64_t filesize, bool flat,
1758 bool compress, bool zeroed_grain,
1759 Error **errp)
1760 {
1761 int ret, i;
1762 VMDK4Header header;
1763 uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
1764 uint32_t *gd_buf = NULL;
1765 int gd_buf_size;
1766
1767 if (flat) {
1768 ret = blk_truncate(blk, filesize, PREALLOC_MODE_OFF, errp);
1769 goto exit;
1770 }
1771 magic = cpu_to_be32(VMDK4_MAGIC);
1772 memset(&header, 0, sizeof(header));
1773 if (compress) {
1774 header.version = 3;
1775 } else if (zeroed_grain) {
1776 header.version = 2;
1777 } else {
1778 header.version = 1;
1779 }
1780 header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
1781 | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
1782 | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
1783 header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
1784 header.capacity = filesize / BDRV_SECTOR_SIZE;
1785 header.granularity = 128;
1786 header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
1787
1788 grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
1789 gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
1790 BDRV_SECTOR_SIZE);
1791 gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
1792 gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
1793
1794 header.desc_offset = 1;
1795 header.desc_size = 20;
1796 header.rgd_offset = header.desc_offset + header.desc_size;
1797 header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
1798 header.grain_offset =
1799 ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
1800 header.granularity);
1801 /* swap endianness for all header fields */
1802 header.version = cpu_to_le32(header.version);
1803 header.flags = cpu_to_le32(header.flags);
1804 header.capacity = cpu_to_le64(header.capacity);
1805 header.granularity = cpu_to_le64(header.granularity);
1806 header.num_gtes_per_gt = cpu_to_le32(header.num_gtes_per_gt);
1807 header.desc_offset = cpu_to_le64(header.desc_offset);
1808 header.desc_size = cpu_to_le64(header.desc_size);
1809 header.rgd_offset = cpu_to_le64(header.rgd_offset);
1810 header.gd_offset = cpu_to_le64(header.gd_offset);
1811 header.grain_offset = cpu_to_le64(header.grain_offset);
1812 header.compressAlgorithm = cpu_to_le16(header.compressAlgorithm);
1813
1814 header.check_bytes[0] = 0xa;
1815 header.check_bytes[1] = 0x20;
1816 header.check_bytes[2] = 0xd;
1817 header.check_bytes[3] = 0xa;
1818
1819 /* write all the data */
1820 ret = blk_pwrite(blk, 0, &magic, sizeof(magic), 0);
1821 if (ret < 0) {
1822 error_setg(errp, QERR_IO_ERROR);
1823 goto exit;
1824 }
1825 ret = blk_pwrite(blk, sizeof(magic), &header, sizeof(header), 0);
1826 if (ret < 0) {
1827 error_setg(errp, QERR_IO_ERROR);
1828 goto exit;
1829 }
1830
1831 ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9,
1832 PREALLOC_MODE_OFF, errp);
1833 if (ret < 0) {
1834 goto exit;
1835 }
1836
1837 /* write grain directory */
1838 gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
1839 gd_buf = g_malloc0(gd_buf_size);
1840 for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
1841 i < gt_count; i++, tmp += gt_size) {
1842 gd_buf[i] = cpu_to_le32(tmp);
1843 }
1844 ret = blk_pwrite(blk, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
1845 gd_buf, gd_buf_size, 0);
1846 if (ret < 0) {
1847 error_setg(errp, QERR_IO_ERROR);
1848 goto exit;
1849 }
1850
1851 /* write backup grain directory */
1852 for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
1853 i < gt_count; i++, tmp += gt_size) {
1854 gd_buf[i] = cpu_to_le32(tmp);
1855 }
1856 ret = blk_pwrite(blk, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
1857 gd_buf, gd_buf_size, 0);
1858 if (ret < 0) {
1859 error_setg(errp, QERR_IO_ERROR);
1860 }
1861
1862 ret = 0;
1863 exit:
1864 g_free(gd_buf);
1865 return ret;
1866 }
1867
1868 static int vmdk_create_extent(const char *filename, int64_t filesize,
1869 bool flat, bool compress, bool zeroed_grain,
1870 BlockBackend **pbb,
1871 QemuOpts *opts, Error **errp)
1872 {
1873 int ret;
1874 BlockBackend *blk = NULL;
1875 Error *local_err = NULL;
1876
1877 ret = bdrv_create_file(filename, opts, &local_err);
1878 if (ret < 0) {
1879 error_propagate(errp, local_err);
1880 goto exit;
1881 }
1882
1883 blk = blk_new_open(filename, NULL, NULL,
1884 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
1885 &local_err);
1886 if (blk == NULL) {
1887 error_propagate(errp, local_err);
1888 ret = -EIO;
1889 goto exit;
1890 }
1891
1892 blk_set_allow_write_beyond_eof(blk, true);
1893
1894 ret = vmdk_init_extent(blk, filesize, flat, compress, zeroed_grain, errp);
1895 exit:
1896 if (blk) {
1897 if (pbb) {
1898 *pbb = blk;
1899 } else {
1900 blk_unref(blk);
1901 blk = NULL;
1902 }
1903 }
1904 return ret;
1905 }
1906
1907 static int filename_decompose(const char *filename, char *path, char *prefix,
1908 char *postfix, size_t buf_len, Error **errp)
1909 {
1910 const char *p, *q;
1911
1912 if (filename == NULL || !strlen(filename)) {
1913 error_setg(errp, "No filename provided");
1914 return VMDK_ERROR;
1915 }
1916 p = strrchr(filename, '/');
1917 if (p == NULL) {
1918 p = strrchr(filename, '\\');
1919 }
1920 if (p == NULL) {
1921 p = strrchr(filename, ':');
1922 }
1923 if (p != NULL) {
1924 p++;
1925 if (p - filename >= buf_len) {
1926 return VMDK_ERROR;
1927 }
1928 pstrcpy(path, p - filename + 1, filename);
1929 } else {
1930 p = filename;
1931 path[0] = '\0';
1932 }
1933 q = strrchr(p, '.');
1934 if (q == NULL) {
1935 pstrcpy(prefix, buf_len, p);
1936 postfix[0] = '\0';
1937 } else {
1938 if (q - p >= buf_len) {
1939 return VMDK_ERROR;
1940 }
1941 pstrcpy(prefix, q - p + 1, p);
1942 pstrcpy(postfix, buf_len, q);
1943 }
1944 return VMDK_OK;
1945 }
1946
1947 /*
1948 * idx == 0: get or create the descriptor file (also the image file if in a
1949 * non-split format.
1950 * idx >= 1: get the n-th extent if in a split subformat
1951 */
1952 typedef BlockBackend *(*vmdk_create_extent_fn)(int64_t size,
1953 int idx,
1954 bool flat,
1955 bool split,
1956 bool compress,
1957 bool zeroed_grain,
1958 void *opaque,
1959 Error **errp);
1960
1961 static void vmdk_desc_add_extent(GString *desc,
1962 const char *extent_line_fmt,
1963 int64_t size, const char *filename)
1964 {
1965 char *basename = g_path_get_basename(filename);
1966
1967 g_string_append_printf(desc, extent_line_fmt,
1968 DIV_ROUND_UP(size, BDRV_SECTOR_SIZE), basename);
1969 g_free(basename);
1970 }
1971
1972 static int coroutine_fn vmdk_co_do_create(int64_t size,
1973 BlockdevVmdkSubformat subformat,
1974 BlockdevVmdkAdapterType adapter_type,
1975 const char *backing_file,
1976 const char *hw_version,
1977 bool compat6,
1978 bool zeroed_grain,
1979 vmdk_create_extent_fn extent_fn,
1980 void *opaque,
1981 Error **errp)
1982 {
1983 int extent_idx;
1984 BlockBackend *blk = NULL;
1985 BlockBackend *extent_blk;
1986 Error *local_err = NULL;
1987 char *desc = NULL;
1988 int ret = 0;
1989 bool flat, split, compress;
1990 GString *ext_desc_lines;
1991 const int64_t split_size = 0x80000000; /* VMDK has constant split size */
1992 int64_t extent_size;
1993 int64_t created_size = 0;
1994 const char *extent_line_fmt;
1995 char *parent_desc_line = g_malloc0(BUF_SIZE);
1996 uint32_t parent_cid = 0xffffffff;
1997 uint32_t number_heads = 16;
1998 uint32_t desc_offset = 0, desc_len;
1999 const char desc_template[] =
2000 "# Disk DescriptorFile\n"
2001 "version=1\n"
2002 "CID=%" PRIx32 "\n"
2003 "parentCID=%" PRIx32 "\n"
2004 "createType=\"%s\"\n"
2005 "%s"
2006 "\n"
2007 "# Extent description\n"
2008 "%s"
2009 "\n"
2010 "# The Disk Data Base\n"
2011 "#DDB\n"
2012 "\n"
2013 "ddb.virtualHWVersion = \"%s\"\n"
2014 "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
2015 "ddb.geometry.heads = \"%" PRIu32 "\"\n"
2016 "ddb.geometry.sectors = \"63\"\n"
2017 "ddb.adapterType = \"%s\"\n";
2018
2019 ext_desc_lines = g_string_new(NULL);
2020
2021 /* Read out options */
2022 if (compat6) {
2023 if (hw_version) {
2024 error_setg(errp,
2025 "compat6 cannot be enabled with hwversion set");
2026 ret = -EINVAL;
2027 goto exit;
2028 }
2029 hw_version = "6";
2030 }
2031 if (!hw_version) {
2032 hw_version = "4";
2033 }
2034
2035 if (adapter_type != BLOCKDEV_VMDK_ADAPTER_TYPE_IDE) {
2036 /* that's the number of heads with which vmware operates when
2037 creating, exporting, etc. vmdk files with a non-ide adapter type */
2038 number_heads = 255;
2039 }
2040 split = (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT) ||
2041 (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTSPARSE);
2042 flat = (subformat == BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICFLAT) ||
2043 (subformat == BLOCKDEV_VMDK_SUBFORMAT_TWOGBMAXEXTENTFLAT);
2044 compress = subformat == BLOCKDEV_VMDK_SUBFORMAT_STREAMOPTIMIZED;
2045
2046 if (flat) {
2047 extent_line_fmt = "RW %" PRId64 " FLAT \"%s\" 0\n";
2048 } else {
2049 extent_line_fmt = "RW %" PRId64 " SPARSE \"%s\"\n";
2050 }
2051 if (flat && backing_file) {
2052 error_setg(errp, "Flat image can't have backing file");
2053 ret = -ENOTSUP;
2054 goto exit;
2055 }
2056 if (flat && zeroed_grain) {
2057 error_setg(errp, "Flat image can't enable zeroed grain");
2058 ret = -ENOTSUP;
2059 goto exit;
2060 }
2061
2062 /* Create extents */
2063 if (split) {
2064 extent_size = split_size;
2065 } else {
2066 extent_size = size;
2067 }
2068 if (!split && !flat) {
2069 created_size = extent_size;
2070 } else {
2071 created_size = 0;
2072 }
2073 /* Get the descriptor file BDS */
2074 blk = extent_fn(created_size, 0, flat, split, compress, zeroed_grain,
2075 opaque, errp);
2076 if (!blk) {
2077 ret = -EIO;
2078 goto exit;
2079 }
2080 if (!split && !flat) {
2081 vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, created_size,
2082 blk_bs(blk)->filename);
2083 }
2084
2085 if (backing_file) {
2086 BlockBackend *backing;
2087 char *full_backing =
2088 bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename,
2089 backing_file,
2090 &local_err);
2091 if (local_err) {
2092 error_propagate(errp, local_err);
2093 ret = -ENOENT;
2094 goto exit;
2095 }
2096 assert(full_backing);
2097
2098 backing = blk_new_open(full_backing, NULL, NULL,
2099 BDRV_O_NO_BACKING, errp);
2100 g_free(full_backing);
2101 if (backing == NULL) {
2102 ret = -EIO;
2103 goto exit;
2104 }
2105 if (strcmp(blk_bs(backing)->drv->format_name, "vmdk")) {
2106 error_setg(errp, "Invalid backing file format: %s. Must be vmdk",
2107 blk_bs(backing)->drv->format_name);
2108 blk_unref(backing);
2109 ret = -EINVAL;
2110 goto exit;
2111 }
2112 ret = vmdk_read_cid(blk_bs(backing), 0, &parent_cid);
2113 blk_unref(backing);
2114 if (ret) {
2115 error_setg(errp, "Failed to read parent CID");
2116 goto exit;
2117 }
2118 snprintf(parent_desc_line, BUF_SIZE,
2119 "parentFileNameHint=\"%s\"", backing_file);
2120 }
2121 extent_idx = 1;
2122 while (created_size < size) {
2123 int64_t cur_size = MIN(size - created_size, extent_size);
2124 extent_blk = extent_fn(cur_size, extent_idx, flat, split, compress,
2125 zeroed_grain, opaque, errp);
2126 if (!extent_blk) {
2127 ret = -EINVAL;
2128 goto exit;
2129 }
2130 vmdk_desc_add_extent(ext_desc_lines, extent_line_fmt, cur_size,
2131 blk_bs(extent_blk)->filename);
2132 created_size += cur_size;
2133 extent_idx++;
2134 blk_unref(extent_blk);
2135 }
2136
2137 /* Check whether we got excess extents */
2138 extent_blk = extent_fn(-1, extent_idx, flat, split, compress, zeroed_grain,
2139 opaque, NULL);
2140 if (extent_blk) {
2141 blk_unref(extent_blk);
2142 error_setg(errp, "List of extents contains unused extents");
2143 ret = -EINVAL;
2144 goto exit;
2145 }
2146
2147 /* generate descriptor file */
2148 desc = g_strdup_printf(desc_template,
2149 g_random_int(),
2150 parent_cid,
2151 BlockdevVmdkSubformat_str(subformat),
2152 parent_desc_line,
2153 ext_desc_lines->str,
2154 hw_version,
2155 size /
2156 (int64_t)(63 * number_heads * BDRV_SECTOR_SIZE),
2157 number_heads,
2158 BlockdevVmdkAdapterType_str(adapter_type));
2159 desc_len = strlen(desc);
2160 /* the descriptor offset = 0x200 */
2161 if (!split && !flat) {
2162 desc_offset = 0x200;
2163 }
2164
2165 ret = blk_pwrite(blk, desc_offset, desc, desc_len, 0);
2166 if (ret < 0) {
2167 error_setg_errno(errp, -ret, "Could not write description");
2168 goto exit;
2169 }
2170 /* bdrv_pwrite write padding zeros to align to sector, we don't need that
2171 * for description file */
2172 if (desc_offset == 0) {
2173 ret = blk_truncate(blk, desc_len, PREALLOC_MODE_OFF, errp);
2174 if (ret < 0) {
2175 goto exit;
2176 }
2177 }
2178 ret = 0;
2179 exit:
2180 if (blk) {
2181 blk_unref(blk);
2182 }
2183 g_free(desc);
2184 g_free(parent_desc_line);
2185 g_string_free(ext_desc_lines, true);
2186 return ret;
2187 }
2188
2189 typedef struct {
2190 char *path;
2191 char *prefix;
2192 char *postfix;
2193 QemuOpts *opts;
2194 } VMDKCreateOptsData;
2195
2196 static BlockBackend *vmdk_co_create_opts_cb(int64_t size, int idx,
2197 bool flat, bool split, bool compress,
2198 bool zeroed_grain, void *opaque,
2199 Error **errp)
2200 {
2201 BlockBackend *blk = NULL;
2202 BlockDriverState *bs = NULL;
2203 VMDKCreateOptsData *data = opaque;
2204 char *ext_filename = NULL;
2205 char *rel_filename = NULL;
2206
2207 /* We're done, don't create excess extents. */
2208 if (size == -1) {
2209 assert(errp == NULL);
2210 return NULL;
2211 }
2212
2213 if (idx == 0) {
2214 rel_filename = g_strdup_printf("%s%s", data->prefix, data->postfix);
2215 } else if (split) {
2216 rel_filename = g_strdup_printf("%s-%c%03d%s",
2217 data->prefix,
2218 flat ? 'f' : 's', idx, data->postfix);
2219 } else {
2220 assert(idx == 1);
2221 rel_filename = g_strdup_printf("%s-flat%s", data->prefix, data->postfix);
2222 }
2223
2224 ext_filename = g_strdup_printf("%s%s", data->path, rel_filename);
2225 g_free(rel_filename);
2226
2227 if (vmdk_create_extent(ext_filename, size,
2228 flat, compress, zeroed_grain, &blk, data->opts,
2229 errp)) {
2230 goto exit;
2231 }
2232 bdrv_unref(bs);
2233 exit:
2234 g_free(ext_filename);
2235 return blk;
2236 }
2237
2238 static int coroutine_fn vmdk_co_create_opts(const char *filename, QemuOpts *opts,
2239 Error **errp)
2240 {
2241 Error *local_err = NULL;
2242 char *desc = NULL;
2243 int64_t total_size = 0;
2244 char *adapter_type = NULL;
2245 BlockdevVmdkAdapterType adapter_type_enum;
2246 char *backing_file = NULL;
2247 char *hw_version = NULL;
2248 char *fmt = NULL;
2249 BlockdevVmdkSubformat subformat;
2250 int ret = 0;
2251 char *path = g_malloc0(PATH_MAX);
2252 char *prefix = g_malloc0(PATH_MAX);
2253 char *postfix = g_malloc0(PATH_MAX);
2254 char *desc_line = g_malloc0(BUF_SIZE);
2255 char *ext_filename = g_malloc0(PATH_MAX);
2256 char *desc_filename = g_malloc0(PATH_MAX);
2257 char *parent_desc_line = g_malloc0(BUF_SIZE);
2258 bool zeroed_grain;
2259 bool compat6;
2260 VMDKCreateOptsData data;
2261
2262 if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
2263 ret = -EINVAL;
2264 goto exit;
2265 }
2266 /* Read out options */
2267 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2268 BDRV_SECTOR_SIZE);
2269 adapter_type = qemu_opt_get_del(opts, BLOCK_OPT_ADAPTER_TYPE);
2270 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2271 hw_version = qemu_opt_get_del(opts, BLOCK_OPT_HWVERSION);
2272 compat6 = qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false);
2273 if (strcmp(hw_version, "undefined") == 0) {
2274 g_free(hw_version);
2275 hw_version = NULL;
2276 }
2277 fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
2278 zeroed_grain = qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false);
2279
2280 if (adapter_type) {
2281 adapter_type_enum = qapi_enum_parse(&BlockdevVmdkAdapterType_lookup,
2282 adapter_type,
2283 BLOCKDEV_VMDK_ADAPTER_TYPE_IDE,
2284 &local_err);
2285 if (local_err) {
2286 error_propagate(errp, local_err);
2287 ret = -EINVAL;
2288 goto exit;
2289 }
2290 } else {
2291 adapter_type_enum = BLOCKDEV_VMDK_ADAPTER_TYPE_IDE;
2292 }
2293
2294 if (!fmt) {
2295 /* Default format to monolithicSparse */
2296 subformat = BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE;
2297 } else {
2298 subformat = qapi_enum_parse(&BlockdevVmdkSubformat_lookup,
2299 fmt,
2300 BLOCKDEV_VMDK_SUBFORMAT_MONOLITHICSPARSE,
2301 &local_err);
2302 if (local_err) {
2303 error_propagate(errp, local_err);
2304 ret = -EINVAL;
2305 goto exit;
2306 }
2307 }
2308 data = (VMDKCreateOptsData){
2309 .prefix = prefix,
2310 .postfix = postfix,
2311 .path = path,
2312 .opts = opts,
2313 };
2314 ret = vmdk_co_do_create(total_size, subformat, adapter_type_enum,
2315 backing_file, hw_version, compat6, zeroed_grain,
2316 vmdk_co_create_opts_cb, &data, errp);
2317
2318 exit:
2319 g_free(adapter_type);
2320 g_free(backing_file);
2321 g_free(hw_version);
2322 g_free(fmt);
2323 g_free(desc);
2324 g_free(path);
2325 g_free(prefix);
2326 g_free(postfix);
2327 g_free(desc_line);
2328 g_free(ext_filename);
2329 g_free(desc_filename);
2330 g_free(parent_desc_line);
2331 return ret;
2332 }
2333
2334 static BlockBackend *vmdk_co_create_cb(int64_t size, int idx,
2335 bool flat, bool split, bool compress,
2336 bool zeroed_grain, void *opaque,
2337 Error **errp)
2338 {
2339 int ret;
2340 BlockDriverState *bs;
2341 BlockBackend *blk;
2342 BlockdevCreateOptionsVmdk *opts = opaque;
2343
2344 if (idx == 0) {
2345 bs = bdrv_open_blockdev_ref(opts->file, errp);
2346 } else {
2347 int i;
2348 BlockdevRefList *list = opts->extents;
2349 for (i = 1; i < idx; i++) {
2350 if (!list || !list->next) {
2351 error_setg(errp, "Extent [%d] not specified", i);
2352 return NULL;
2353 }
2354 list = list->next;
2355 }
2356 if (!list) {
2357 error_setg(errp, "Extent [%d] not specified", idx - 1);
2358 return NULL;
2359 }
2360 bs = bdrv_open_blockdev_ref(list->value, errp);
2361 }
2362 if (!bs) {
2363 return NULL;
2364 }
2365 blk = blk_new(bdrv_get_aio_context(bs),
2366 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
2367 BLK_PERM_ALL);
2368 if (blk_insert_bs(blk, bs, errp)) {
2369 bdrv_unref(bs);
2370 return NULL;
2371 }
2372 blk_set_allow_write_beyond_eof(blk, true);
2373 bdrv_unref(bs);
2374
2375 if (size != -1) {
2376 ret = vmdk_init_extent(blk, size, flat, compress, zeroed_grain, errp);
2377 if (ret) {
2378 blk_unref(blk);
2379 blk = NULL;
2380 }
2381 }
2382 return blk;
2383 }
2384
2385 static int coroutine_fn vmdk_co_create(BlockdevCreateOptions *create_options,
2386 Error **errp)
2387 {
2388 int ret;
2389 BlockdevCreateOptionsVmdk *opts;
2390
2391 opts = &create_options->u.vmdk;
2392
2393 /* Validate options */
2394 if (!QEMU_IS_ALIGNED(opts->size, BDRV_SECTOR_SIZE)) {
2395 error_setg(errp, "Image size must be a multiple of 512 bytes");
2396 ret = -EINVAL;
2397 goto out;
2398 }
2399
2400 ret = vmdk_co_do_create(opts->size,
2401 opts->subformat,
2402 opts->adapter_type,
2403 opts->backing_file,
2404 opts->hwversion,
2405 false,
2406 opts->zeroed_grain,
2407 vmdk_co_create_cb,
2408 opts, errp);
2409 return ret;
2410
2411 out:
2412 return ret;
2413 }
2414
2415 static void vmdk_close(BlockDriverState *bs)
2416 {
2417 BDRVVmdkState *s = bs->opaque;
2418
2419 vmdk_free_extents(bs);
2420 g_free(s->create_type);
2421
2422 migrate_del_blocker(s->migration_blocker);
2423 error_free(s->migration_blocker);
2424 }
2425
2426 static coroutine_fn int vmdk_co_flush(BlockDriverState *bs)
2427 {
2428 BDRVVmdkState *s = bs->opaque;
2429 int i, err;
2430 int ret = 0;
2431
2432 for (i = 0; i < s->num_extents; i++) {
2433 err = bdrv_co_flush(s->extents[i].file->bs);
2434 if (err < 0) {
2435 ret = err;
2436 }
2437 }
2438 return ret;
2439 }
2440
2441 static int64_t vmdk_get_allocated_file_size(BlockDriverState *bs)
2442 {
2443 int i;
2444 int64_t ret = 0;
2445 int64_t r;
2446 BDRVVmdkState *s = bs->opaque;
2447
2448 ret = bdrv_get_allocated_file_size(bs->file->bs);
2449 if (ret < 0) {
2450 return ret;
2451 }
2452 for (i = 0; i < s->num_extents; i++) {
2453 if (s->extents[i].file == bs->file) {
2454 continue;
2455 }
2456 r = bdrv_get_allocated_file_size(s->extents[i].file->bs);
2457 if (r < 0) {
2458 return r;
2459 }
2460 ret += r;
2461 }
2462 return ret;
2463 }
2464
2465 static int vmdk_has_zero_init(BlockDriverState *bs)
2466 {
2467 int i;
2468 BDRVVmdkState *s = bs->opaque;
2469
2470 /* If has a flat extent and its underlying storage doesn't have zero init,
2471 * return 0. */
2472 for (i = 0; i < s->num_extents; i++) {
2473 if (s->extents[i].flat) {
2474 if (!bdrv_has_zero_init(s->extents[i].file->bs)) {
2475 return 0;
2476 }
2477 }
2478 }
2479 return 1;
2480 }
2481
2482 static ImageInfo *vmdk_get_extent_info(VmdkExtent *extent)
2483 {
2484 ImageInfo *info = g_new0(ImageInfo, 1);
2485
2486 bdrv_refresh_filename(extent->file->bs);
2487 *info = (ImageInfo){
2488 .filename = g_strdup(extent->file->bs->filename),
2489 .format = g_strdup(extent->type),
2490 .virtual_size = extent->sectors * BDRV_SECTOR_SIZE,
2491 .compressed = extent->compressed,
2492 .has_compressed = extent->compressed,
2493 .cluster_size = extent->cluster_sectors * BDRV_SECTOR_SIZE,
2494 .has_cluster_size = !extent->flat,
2495 };
2496
2497 return info;
2498 }
2499
2500 static int coroutine_fn vmdk_co_check(BlockDriverState *bs,
2501 BdrvCheckResult *result,
2502 BdrvCheckMode fix)
2503 {
2504 BDRVVmdkState *s = bs->opaque;
2505 VmdkExtent *extent = NULL;
2506 int64_t sector_num = 0;
2507 int64_t total_sectors = bdrv_nb_sectors(bs);
2508 int ret;
2509 uint64_t cluster_offset;
2510
2511 if (fix) {
2512 return -ENOTSUP;
2513 }
2514
2515 for (;;) {
2516 if (sector_num >= total_sectors) {
2517 return 0;
2518 }
2519 extent = find_extent(s, sector_num, extent);
2520 if (!extent) {
2521 fprintf(stderr,
2522 "ERROR: could not find extent for sector %" PRId64 "\n",
2523 sector_num);
2524 ret = -EINVAL;
2525 break;
2526 }
2527 ret = get_cluster_offset(bs, extent, NULL,
2528 sector_num << BDRV_SECTOR_BITS,
2529 false, &cluster_offset, 0, 0);
2530 if (ret == VMDK_ERROR) {
2531 fprintf(stderr,
2532 "ERROR: could not get cluster_offset for sector %"
2533 PRId64 "\n", sector_num);
2534 break;
2535 }
2536 if (ret == VMDK_OK) {
2537 int64_t extent_len = bdrv_getlength(extent->file->bs);
2538 if (extent_len < 0) {
2539 fprintf(stderr,
2540 "ERROR: could not get extent file length for sector %"
2541 PRId64 "\n", sector_num);
2542 ret = extent_len;
2543 break;
2544 }
2545 if (cluster_offset >= extent_len) {
2546 fprintf(stderr,
2547 "ERROR: cluster offset for sector %"
2548 PRId64 " points after EOF\n", sector_num);
2549 ret = -EINVAL;
2550 break;
2551 }
2552 }
2553 sector_num += extent->cluster_sectors;
2554 }
2555
2556 result->corruptions++;
2557 return ret;
2558 }
2559
2560 static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs,
2561 Error **errp)
2562 {
2563 int i;
2564 BDRVVmdkState *s = bs->opaque;
2565 ImageInfoSpecific *spec_info = g_new0(ImageInfoSpecific, 1);
2566 ImageInfoList **next;
2567
2568 *spec_info = (ImageInfoSpecific){
2569 .type = IMAGE_INFO_SPECIFIC_KIND_VMDK,
2570 .u = {
2571 .vmdk.data = g_new0(ImageInfoSpecificVmdk, 1),
2572 },
2573 };
2574
2575 *spec_info->u.vmdk.data = (ImageInfoSpecificVmdk) {
2576 .create_type = g_strdup(s->create_type),
2577 .cid = s->cid,
2578 .parent_cid = s->parent_cid,
2579 };
2580
2581 next = &spec_info->u.vmdk.data->extents;
2582 for (i = 0; i < s->num_extents; i++) {
2583 *next = g_new0(ImageInfoList, 1);
2584 (*next)->value = vmdk_get_extent_info(&s->extents[i]);
2585 (*next)->next = NULL;
2586 next = &(*next)->next;
2587 }
2588
2589 return spec_info;
2590 }
2591
2592 static bool vmdk_extents_type_eq(const VmdkExtent *a, const VmdkExtent *b)
2593 {
2594 return a->flat == b->flat &&
2595 a->compressed == b->compressed &&
2596 (a->flat || a->cluster_sectors == b->cluster_sectors);
2597 }
2598
2599 static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2600 {
2601 int i;
2602 BDRVVmdkState *s = bs->opaque;
2603 assert(s->num_extents);
2604
2605 /* See if we have multiple extents but they have different cases */
2606 for (i = 1; i < s->num_extents; i++) {
2607 if (!vmdk_extents_type_eq(&s->extents[0], &s->extents[i])) {
2608 return -ENOTSUP;
2609 }
2610 }
2611 bdi->needs_compressed_writes = s->extents[0].compressed;
2612 if (!s->extents[0].flat) {
2613 bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS;
2614 }
2615 return 0;
2616 }
2617
2618 static void vmdk_gather_child_options(BlockDriverState *bs, QDict *target,
2619 bool backing_overridden)
2620 {
2621 /* No children but file and backing can be explicitly specified (TODO) */
2622 qdict_put(target, "file",
2623 qobject_ref(bs->file->bs->full_open_options));
2624
2625 if (backing_overridden) {
2626 if (bs->backing) {
2627 qdict_put(target, "backing",
2628 qobject_ref(bs->backing->bs->full_open_options));
2629 } else {
2630 qdict_put_null(target, "backing");
2631 }
2632 }
2633 }
2634
2635 static QemuOptsList vmdk_create_opts = {
2636 .name = "vmdk-create-opts",
2637 .head = QTAILQ_HEAD_INITIALIZER(vmdk_create_opts.head),
2638 .desc = {
2639 {
2640 .name = BLOCK_OPT_SIZE,
2641 .type = QEMU_OPT_SIZE,
2642 .help = "Virtual disk size"
2643 },
2644 {
2645 .name = BLOCK_OPT_ADAPTER_TYPE,
2646 .type = QEMU_OPT_STRING,
2647 .help = "Virtual adapter type, can be one of "
2648 "ide (default), lsilogic, buslogic or legacyESX"
2649 },
2650 {
2651 .name = BLOCK_OPT_BACKING_FILE,
2652 .type = QEMU_OPT_STRING,
2653 .help = "File name of a base image"
2654 },
2655 {
2656 .name = BLOCK_OPT_COMPAT6,
2657 .type = QEMU_OPT_BOOL,
2658 .help = "VMDK version 6 image",
2659 .def_value_str = "off"
2660 },
2661 {
2662 .name = BLOCK_OPT_HWVERSION,
2663 .type = QEMU_OPT_STRING,
2664 .help = "VMDK hardware version",
2665 .def_value_str = "undefined"
2666 },
2667 {
2668 .name = BLOCK_OPT_SUBFMT,
2669 .type = QEMU_OPT_STRING,
2670 .help =
2671 "VMDK flat extent format, can be one of "
2672 "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} "
2673 },
2674 {
2675 .name = BLOCK_OPT_ZEROED_GRAIN,
2676 .type = QEMU_OPT_BOOL,
2677 .help = "Enable efficient zero writes "
2678 "using the zeroed-grain GTE feature"
2679 },
2680 { /* end of list */ }
2681 }
2682 };
2683
2684 static BlockDriver bdrv_vmdk = {
2685 .format_name = "vmdk",
2686 .instance_size = sizeof(BDRVVmdkState),
2687 .bdrv_probe = vmdk_probe,
2688 .bdrv_open = vmdk_open,
2689 .bdrv_co_check = vmdk_co_check,
2690 .bdrv_reopen_prepare = vmdk_reopen_prepare,
2691 .bdrv_child_perm = bdrv_format_default_perms,
2692 .bdrv_co_preadv = vmdk_co_preadv,
2693 .bdrv_co_pwritev = vmdk_co_pwritev,
2694 .bdrv_co_pwritev_compressed = vmdk_co_pwritev_compressed,
2695 .bdrv_co_pwrite_zeroes = vmdk_co_pwrite_zeroes,
2696 .bdrv_close = vmdk_close,
2697 .bdrv_co_create_opts = vmdk_co_create_opts,
2698 .bdrv_co_create = vmdk_co_create,
2699 .bdrv_co_flush_to_disk = vmdk_co_flush,
2700 .bdrv_co_block_status = vmdk_co_block_status,
2701 .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
2702 .bdrv_has_zero_init = vmdk_has_zero_init,
2703 .bdrv_get_specific_info = vmdk_get_specific_info,
2704 .bdrv_refresh_limits = vmdk_refresh_limits,
2705 .bdrv_get_info = vmdk_get_info,
2706 .bdrv_gather_child_options = vmdk_gather_child_options,
2707
2708 .supports_backing = true,
2709 .create_opts = &vmdk_create_opts,
2710 };
2711
2712 static void bdrv_vmdk_init(void)
2713 {
2714 bdrv_register(&bdrv_vmdk);
2715 }
2716
2717 block_init(bdrv_vmdk_init);