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