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