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