qemu/block/qcow2.c
<<
>>
Prefs
   1/*
   2 * Block driver for the QCOW version 2 format
   3 *
   4 * Copyright (c) 2004-2006 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26
  27#include "block/qdict.h"
  28#include "sysemu/block-backend.h"
  29#include "qemu/module.h"
  30#include "qcow2.h"
  31#include "qemu/error-report.h"
  32#include "qapi/error.h"
  33#include "qapi/qapi-events-block-core.h"
  34#include "qapi/qmp/qdict.h"
  35#include "qapi/qmp/qstring.h"
  36#include "trace.h"
  37#include "qemu/option_int.h"
  38#include "qemu/cutils.h"
  39#include "qemu/bswap.h"
  40#include "qapi/qobject-input-visitor.h"
  41#include "qapi/qapi-visit-block-core.h"
  42#include "crypto.h"
  43
  44/*
  45  Differences with QCOW:
  46
  47  - Support for multiple incremental snapshots.
  48  - Memory management by reference counts.
  49  - Clusters which have a reference count of one have the bit
  50    QCOW_OFLAG_COPIED to optimize write performance.
  51  - Size of compressed clusters is stored in sectors to reduce bit usage
  52    in the cluster offsets.
  53  - Support for storing additional data (such as the VM state) in the
  54    snapshots.
  55  - If a backing store is used, the cluster size is not constrained
  56    (could be backported to QCOW).
  57  - L2 tables have always a size of one cluster.
  58*/
  59
  60
  61typedef struct {
  62    uint32_t magic;
  63    uint32_t len;
  64} QEMU_PACKED QCowExtension;
  65
  66#define  QCOW2_EXT_MAGIC_END 0
  67#define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
  68#define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
  69#define  QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
  70#define  QCOW2_EXT_MAGIC_BITMAPS 0x23852875
  71#define  QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
  72
  73static int coroutine_fn
  74qcow2_co_preadv_compressed(BlockDriverState *bs,
  75                           uint64_t file_cluster_offset,
  76                           uint64_t offset,
  77                           uint64_t bytes,
  78                           QEMUIOVector *qiov);
  79
  80static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
  81{
  82    const QCowHeader *cow_header = (const void *)buf;
  83
  84    if (buf_size >= sizeof(QCowHeader) &&
  85        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
  86        be32_to_cpu(cow_header->version) >= 2)
  87        return 100;
  88    else
  89        return 0;
  90}
  91
  92
  93static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
  94                                          uint8_t *buf, size_t buflen,
  95                                          void *opaque, Error **errp)
  96{
  97    BlockDriverState *bs = opaque;
  98    BDRVQcow2State *s = bs->opaque;
  99    ssize_t ret;
 100
 101    if ((offset + buflen) > s->crypto_header.length) {
 102        error_setg(errp, "Request for data outside of extension header");
 103        return -1;
 104    }
 105
 106    ret = bdrv_pread(bs->file,
 107                     s->crypto_header.offset + offset, buf, buflen);
 108    if (ret < 0) {
 109        error_setg_errno(errp, -ret, "Could not read encryption header");
 110        return -1;
 111    }
 112    return ret;
 113}
 114
 115
 116static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
 117                                          void *opaque, Error **errp)
 118{
 119    BlockDriverState *bs = opaque;
 120    BDRVQcow2State *s = bs->opaque;
 121    int64_t ret;
 122    int64_t clusterlen;
 123
 124    ret = qcow2_alloc_clusters(bs, headerlen);
 125    if (ret < 0) {
 126        error_setg_errno(errp, -ret,
 127                         "Cannot allocate cluster for LUKS header size %zu",
 128                         headerlen);
 129        return -1;
 130    }
 131
 132    s->crypto_header.length = headerlen;
 133    s->crypto_header.offset = ret;
 134
 135    /* Zero fill remaining space in cluster so it has predictable
 136     * content in case of future spec changes */
 137    clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
 138    assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen, false) == 0);
 139    ret = bdrv_pwrite_zeroes(bs->file,
 140                             ret + headerlen,
 141                             clusterlen - headerlen, 0);
 142    if (ret < 0) {
 143        error_setg_errno(errp, -ret, "Could not zero fill encryption header");
 144        return -1;
 145    }
 146
 147    return ret;
 148}
 149
 150
 151static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
 152                                           const uint8_t *buf, size_t buflen,
 153                                           void *opaque, Error **errp)
 154{
 155    BlockDriverState *bs = opaque;
 156    BDRVQcow2State *s = bs->opaque;
 157    ssize_t ret;
 158
 159    if ((offset + buflen) > s->crypto_header.length) {
 160        error_setg(errp, "Request for data outside of extension header");
 161        return -1;
 162    }
 163
 164    ret = bdrv_pwrite(bs->file,
 165                      s->crypto_header.offset + offset, buf, buflen);
 166    if (ret < 0) {
 167        error_setg_errno(errp, -ret, "Could not read encryption header");
 168        return -1;
 169    }
 170    return ret;
 171}
 172
 173
 174/* 
 175 * read qcow2 extension and fill bs
 176 * start reading from start_offset
 177 * finish reading upon magic of value 0 or when end_offset reached
 178 * unknown magic is skipped (future extension this version knows nothing about)
 179 * return 0 upon success, non-0 otherwise
 180 */
 181static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
 182                                 uint64_t end_offset, void **p_feature_table,
 183                                 int flags, bool *need_update_header,
 184                                 Error **errp)
 185{
 186    BDRVQcow2State *s = bs->opaque;
 187    QCowExtension ext;
 188    uint64_t offset;
 189    int ret;
 190    Qcow2BitmapHeaderExt bitmaps_ext;
 191
 192    if (need_update_header != NULL) {
 193        *need_update_header = false;
 194    }
 195
 196#ifdef DEBUG_EXT
 197    printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
 198#endif
 199    offset = start_offset;
 200    while (offset < end_offset) {
 201
 202#ifdef DEBUG_EXT
 203        /* Sanity check */
 204        if (offset > s->cluster_size)
 205            printf("qcow2_read_extension: suspicious offset %lu\n", offset);
 206
 207        printf("attempting to read extended header in offset %lu\n", offset);
 208#endif
 209
 210        ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
 211        if (ret < 0) {
 212            error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
 213                             "pread fail from offset %" PRIu64, offset);
 214            return 1;
 215        }
 216        ext.magic = be32_to_cpu(ext.magic);
 217        ext.len = be32_to_cpu(ext.len);
 218        offset += sizeof(ext);
 219#ifdef DEBUG_EXT
 220        printf("ext.magic = 0x%x\n", ext.magic);
 221#endif
 222        if (offset > end_offset || ext.len > end_offset - offset) {
 223            error_setg(errp, "Header extension too large");
 224            return -EINVAL;
 225        }
 226
 227        switch (ext.magic) {
 228        case QCOW2_EXT_MAGIC_END:
 229            return 0;
 230
 231        case QCOW2_EXT_MAGIC_BACKING_FORMAT:
 232            if (ext.len >= sizeof(bs->backing_format)) {
 233                error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
 234                           " too large (>=%zu)", ext.len,
 235                           sizeof(bs->backing_format));
 236                return 2;
 237            }
 238            ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
 239            if (ret < 0) {
 240                error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
 241                                 "Could not read format name");
 242                return 3;
 243            }
 244            bs->backing_format[ext.len] = '\0';
 245            s->image_backing_format = g_strdup(bs->backing_format);
 246#ifdef DEBUG_EXT
 247            printf("Qcow2: Got format extension %s\n", bs->backing_format);
 248#endif
 249            break;
 250
 251        case QCOW2_EXT_MAGIC_FEATURE_TABLE:
 252            if (p_feature_table != NULL) {
 253                void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
 254                ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
 255                if (ret < 0) {
 256                    error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
 257                                     "Could not read table");
 258                    return ret;
 259                }
 260
 261                *p_feature_table = feature_table;
 262            }
 263            break;
 264
 265        case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
 266            unsigned int cflags = 0;
 267            if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
 268                error_setg(errp, "CRYPTO header extension only "
 269                           "expected with LUKS encryption method");
 270                return -EINVAL;
 271            }
 272            if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
 273                error_setg(errp, "CRYPTO header extension size %u, "
 274                           "but expected size %zu", ext.len,
 275                           sizeof(Qcow2CryptoHeaderExtension));
 276                return -EINVAL;
 277            }
 278
 279            ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
 280            if (ret < 0) {
 281                error_setg_errno(errp, -ret,
 282                                 "Unable to read CRYPTO header extension");
 283                return ret;
 284            }
 285            s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
 286            s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
 287
 288            if ((s->crypto_header.offset % s->cluster_size) != 0) {
 289                error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
 290                           "not a multiple of cluster size '%u'",
 291                           s->crypto_header.offset, s->cluster_size);
 292                return -EINVAL;
 293            }
 294
 295            if (flags & BDRV_O_NO_IO) {
 296                cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
 297            }
 298            s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
 299                                           qcow2_crypto_hdr_read_func,
 300                                           bs, cflags, QCOW2_MAX_THREADS, errp);
 301            if (!s->crypto) {
 302                return -EINVAL;
 303            }
 304        }   break;
 305
 306        case QCOW2_EXT_MAGIC_BITMAPS:
 307            if (ext.len != sizeof(bitmaps_ext)) {
 308                error_setg_errno(errp, -ret, "bitmaps_ext: "
 309                                 "Invalid extension length");
 310                return -EINVAL;
 311            }
 312
 313            if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
 314                if (s->qcow_version < 3) {
 315                    /* Let's be a bit more specific */
 316                    warn_report("This qcow2 v2 image contains bitmaps, but "
 317                                "they may have been modified by a program "
 318                                "without persistent bitmap support; so now "
 319                                "they must all be considered inconsistent");
 320                } else {
 321                    warn_report("a program lacking bitmap support "
 322                                "modified this file, so all bitmaps are now "
 323                                "considered inconsistent");
 324                }
 325                error_printf("Some clusters may be leaked, "
 326                             "run 'qemu-img check -r' on the image "
 327                             "file to fix.");
 328                if (need_update_header != NULL) {
 329                    /* Updating is needed to drop invalid bitmap extension. */
 330                    *need_update_header = true;
 331                }
 332                break;
 333            }
 334
 335            ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
 336            if (ret < 0) {
 337                error_setg_errno(errp, -ret, "bitmaps_ext: "
 338                                 "Could not read ext header");
 339                return ret;
 340            }
 341
 342            if (bitmaps_ext.reserved32 != 0) {
 343                error_setg_errno(errp, -ret, "bitmaps_ext: "
 344                                 "Reserved field is not zero");
 345                return -EINVAL;
 346            }
 347
 348            bitmaps_ext.nb_bitmaps = be32_to_cpu(bitmaps_ext.nb_bitmaps);
 349            bitmaps_ext.bitmap_directory_size =
 350                be64_to_cpu(bitmaps_ext.bitmap_directory_size);
 351            bitmaps_ext.bitmap_directory_offset =
 352                be64_to_cpu(bitmaps_ext.bitmap_directory_offset);
 353
 354            if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
 355                error_setg(errp,
 356                           "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
 357                           "exceeding the QEMU supported maximum of %d",
 358                           bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
 359                return -EINVAL;
 360            }
 361
 362            if (bitmaps_ext.nb_bitmaps == 0) {
 363                error_setg(errp, "found bitmaps extension with zero bitmaps");
 364                return -EINVAL;
 365            }
 366
 367            if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
 368                error_setg(errp, "bitmaps_ext: "
 369                                 "invalid bitmap directory offset");
 370                return -EINVAL;
 371            }
 372
 373            if (bitmaps_ext.bitmap_directory_size >
 374                QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
 375                error_setg(errp, "bitmaps_ext: "
 376                                 "bitmap directory size (%" PRIu64 ") exceeds "
 377                                 "the maximum supported size (%d)",
 378                                 bitmaps_ext.bitmap_directory_size,
 379                                 QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
 380                return -EINVAL;
 381            }
 382
 383            s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
 384            s->bitmap_directory_offset =
 385                    bitmaps_ext.bitmap_directory_offset;
 386            s->bitmap_directory_size =
 387                    bitmaps_ext.bitmap_directory_size;
 388
 389#ifdef DEBUG_EXT
 390            printf("Qcow2: Got bitmaps extension: "
 391                   "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
 392                   s->bitmap_directory_offset, s->nb_bitmaps);
 393#endif
 394            break;
 395
 396        case QCOW2_EXT_MAGIC_DATA_FILE:
 397        {
 398            s->image_data_file = g_malloc0(ext.len + 1);
 399            ret = bdrv_pread(bs->file, offset, s->image_data_file, ext.len);
 400            if (ret < 0) {
 401                error_setg_errno(errp, -ret,
 402                                 "ERROR: Could not read data file name");
 403                return ret;
 404            }
 405#ifdef DEBUG_EXT
 406            printf("Qcow2: Got external data file %s\n", s->image_data_file);
 407#endif
 408            break;
 409        }
 410
 411        default:
 412            /* unknown magic - save it in case we need to rewrite the header */
 413            /* If you add a new feature, make sure to also update the fast
 414             * path of qcow2_make_empty() to deal with it. */
 415            {
 416                Qcow2UnknownHeaderExtension *uext;
 417
 418                uext = g_malloc0(sizeof(*uext)  + ext.len);
 419                uext->magic = ext.magic;
 420                uext->len = ext.len;
 421                QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
 422
 423                ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
 424                if (ret < 0) {
 425                    error_setg_errno(errp, -ret, "ERROR: unknown extension: "
 426                                     "Could not read data");
 427                    return ret;
 428                }
 429            }
 430            break;
 431        }
 432
 433        offset += ((ext.len + 7) & ~7);
 434    }
 435
 436    return 0;
 437}
 438
 439static void cleanup_unknown_header_ext(BlockDriverState *bs)
 440{
 441    BDRVQcow2State *s = bs->opaque;
 442    Qcow2UnknownHeaderExtension *uext, *next;
 443
 444    QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
 445        QLIST_REMOVE(uext, next);
 446        g_free(uext);
 447    }
 448}
 449
 450static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
 451                                       uint64_t mask)
 452{
 453    char *features = g_strdup("");
 454    char *old;
 455
 456    while (table && table->name[0] != '\0') {
 457        if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
 458            if (mask & (1ULL << table->bit)) {
 459                old = features;
 460                features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
 461                                           table->name);
 462                g_free(old);
 463                mask &= ~(1ULL << table->bit);
 464            }
 465        }
 466        table++;
 467    }
 468
 469    if (mask) {
 470        old = features;
 471        features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
 472                                   old, *old ? ", " : "", mask);
 473        g_free(old);
 474    }
 475
 476    error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
 477    g_free(features);
 478}
 479
 480/*
 481 * Sets the dirty bit and flushes afterwards if necessary.
 482 *
 483 * The incompatible_features bit is only set if the image file header was
 484 * updated successfully.  Therefore it is not required to check the return
 485 * value of this function.
 486 */
 487int qcow2_mark_dirty(BlockDriverState *bs)
 488{
 489    BDRVQcow2State *s = bs->opaque;
 490    uint64_t val;
 491    int ret;
 492
 493    assert(s->qcow_version >= 3);
 494
 495    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
 496        return 0; /* already dirty */
 497    }
 498
 499    val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
 500    ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
 501                      &val, sizeof(val));
 502    if (ret < 0) {
 503        return ret;
 504    }
 505    ret = bdrv_flush(bs->file->bs);
 506    if (ret < 0) {
 507        return ret;
 508    }
 509
 510    /* Only treat image as dirty if the header was updated successfully */
 511    s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
 512    return 0;
 513}
 514
 515/*
 516 * Clears the dirty bit and flushes before if necessary.  Only call this
 517 * function when there are no pending requests, it does not guard against
 518 * concurrent requests dirtying the image.
 519 */
 520static int qcow2_mark_clean(BlockDriverState *bs)
 521{
 522    BDRVQcow2State *s = bs->opaque;
 523
 524    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
 525        int ret;
 526
 527        s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
 528
 529        ret = qcow2_flush_caches(bs);
 530        if (ret < 0) {
 531            return ret;
 532        }
 533
 534        return qcow2_update_header(bs);
 535    }
 536    return 0;
 537}
 538
 539/*
 540 * Marks the image as corrupt.
 541 */
 542int qcow2_mark_corrupt(BlockDriverState *bs)
 543{
 544    BDRVQcow2State *s = bs->opaque;
 545
 546    s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
 547    return qcow2_update_header(bs);
 548}
 549
 550/*
 551 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
 552 * before if necessary.
 553 */
 554int qcow2_mark_consistent(BlockDriverState *bs)
 555{
 556    BDRVQcow2State *s = bs->opaque;
 557
 558    if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
 559        int ret = qcow2_flush_caches(bs);
 560        if (ret < 0) {
 561            return ret;
 562        }
 563
 564        s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
 565        return qcow2_update_header(bs);
 566    }
 567    return 0;
 568}
 569
 570static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs,
 571                                              BdrvCheckResult *result,
 572                                              BdrvCheckMode fix)
 573{
 574    int ret = qcow2_check_refcounts(bs, result, fix);
 575    if (ret < 0) {
 576        return ret;
 577    }
 578
 579    if (fix && result->check_errors == 0 && result->corruptions == 0) {
 580        ret = qcow2_mark_clean(bs);
 581        if (ret < 0) {
 582            return ret;
 583        }
 584        return qcow2_mark_consistent(bs);
 585    }
 586    return ret;
 587}
 588
 589static int coroutine_fn qcow2_co_check(BlockDriverState *bs,
 590                                       BdrvCheckResult *result,
 591                                       BdrvCheckMode fix)
 592{
 593    BDRVQcow2State *s = bs->opaque;
 594    int ret;
 595
 596    qemu_co_mutex_lock(&s->lock);
 597    ret = qcow2_co_check_locked(bs, result, fix);
 598    qemu_co_mutex_unlock(&s->lock);
 599    return ret;
 600}
 601
 602int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
 603                         uint64_t entries, size_t entry_len,
 604                         int64_t max_size_bytes, const char *table_name,
 605                         Error **errp)
 606{
 607    BDRVQcow2State *s = bs->opaque;
 608
 609    if (entries > max_size_bytes / entry_len) {
 610        error_setg(errp, "%s too large", table_name);
 611        return -EFBIG;
 612    }
 613
 614    /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
 615     * because values will be passed to qemu functions taking int64_t. */
 616    if ((INT64_MAX - entries * entry_len < offset) ||
 617        (offset_into_cluster(s, offset) != 0)) {
 618        error_setg(errp, "%s offset invalid", table_name);
 619        return -EINVAL;
 620    }
 621
 622    return 0;
 623}
 624
 625static const char *const mutable_opts[] = {
 626    QCOW2_OPT_LAZY_REFCOUNTS,
 627    QCOW2_OPT_DISCARD_REQUEST,
 628    QCOW2_OPT_DISCARD_SNAPSHOT,
 629    QCOW2_OPT_DISCARD_OTHER,
 630    QCOW2_OPT_OVERLAP,
 631    QCOW2_OPT_OVERLAP_TEMPLATE,
 632    QCOW2_OPT_OVERLAP_MAIN_HEADER,
 633    QCOW2_OPT_OVERLAP_ACTIVE_L1,
 634    QCOW2_OPT_OVERLAP_ACTIVE_L2,
 635    QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
 636    QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
 637    QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
 638    QCOW2_OPT_OVERLAP_INACTIVE_L1,
 639    QCOW2_OPT_OVERLAP_INACTIVE_L2,
 640    QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 641    QCOW2_OPT_CACHE_SIZE,
 642    QCOW2_OPT_L2_CACHE_SIZE,
 643    QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
 644    QCOW2_OPT_REFCOUNT_CACHE_SIZE,
 645    QCOW2_OPT_CACHE_CLEAN_INTERVAL,
 646    NULL
 647};
 648
 649static QemuOptsList qcow2_runtime_opts = {
 650    .name = "qcow2",
 651    .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
 652    .desc = {
 653        {
 654            .name = QCOW2_OPT_LAZY_REFCOUNTS,
 655            .type = QEMU_OPT_BOOL,
 656            .help = "Postpone refcount updates",
 657        },
 658        {
 659            .name = QCOW2_OPT_DISCARD_REQUEST,
 660            .type = QEMU_OPT_BOOL,
 661            .help = "Pass guest discard requests to the layer below",
 662        },
 663        {
 664            .name = QCOW2_OPT_DISCARD_SNAPSHOT,
 665            .type = QEMU_OPT_BOOL,
 666            .help = "Generate discard requests when snapshot related space "
 667                    "is freed",
 668        },
 669        {
 670            .name = QCOW2_OPT_DISCARD_OTHER,
 671            .type = QEMU_OPT_BOOL,
 672            .help = "Generate discard requests when other clusters are freed",
 673        },
 674        {
 675            .name = QCOW2_OPT_OVERLAP,
 676            .type = QEMU_OPT_STRING,
 677            .help = "Selects which overlap checks to perform from a range of "
 678                    "templates (none, constant, cached, all)",
 679        },
 680        {
 681            .name = QCOW2_OPT_OVERLAP_TEMPLATE,
 682            .type = QEMU_OPT_STRING,
 683            .help = "Selects which overlap checks to perform from a range of "
 684                    "templates (none, constant, cached, all)",
 685        },
 686        {
 687            .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
 688            .type = QEMU_OPT_BOOL,
 689            .help = "Check for unintended writes into the main qcow2 header",
 690        },
 691        {
 692            .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
 693            .type = QEMU_OPT_BOOL,
 694            .help = "Check for unintended writes into the active L1 table",
 695        },
 696        {
 697            .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
 698            .type = QEMU_OPT_BOOL,
 699            .help = "Check for unintended writes into an active L2 table",
 700        },
 701        {
 702            .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
 703            .type = QEMU_OPT_BOOL,
 704            .help = "Check for unintended writes into the refcount table",
 705        },
 706        {
 707            .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
 708            .type = QEMU_OPT_BOOL,
 709            .help = "Check for unintended writes into a refcount block",
 710        },
 711        {
 712            .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
 713            .type = QEMU_OPT_BOOL,
 714            .help = "Check for unintended writes into the snapshot table",
 715        },
 716        {
 717            .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
 718            .type = QEMU_OPT_BOOL,
 719            .help = "Check for unintended writes into an inactive L1 table",
 720        },
 721        {
 722            .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
 723            .type = QEMU_OPT_BOOL,
 724            .help = "Check for unintended writes into an inactive L2 table",
 725        },
 726        {
 727            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 728            .type = QEMU_OPT_BOOL,
 729            .help = "Check for unintended writes into the bitmap directory",
 730        },
 731        {
 732            .name = QCOW2_OPT_CACHE_SIZE,
 733            .type = QEMU_OPT_SIZE,
 734            .help = "Maximum combined metadata (L2 tables and refcount blocks) "
 735                    "cache size",
 736        },
 737        {
 738            .name = QCOW2_OPT_L2_CACHE_SIZE,
 739            .type = QEMU_OPT_SIZE,
 740            .help = "Maximum L2 table cache size",
 741        },
 742        {
 743            .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
 744            .type = QEMU_OPT_SIZE,
 745            .help = "Size of each entry in the L2 cache",
 746        },
 747        {
 748            .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
 749            .type = QEMU_OPT_SIZE,
 750            .help = "Maximum refcount block cache size",
 751        },
 752        {
 753            .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
 754            .type = QEMU_OPT_NUMBER,
 755            .help = "Clean unused cache entries after this time (in seconds)",
 756        },
 757        BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
 758            "ID of secret providing qcow2 AES key or LUKS passphrase"),
 759        { /* end of list */ }
 760    },
 761};
 762
 763static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
 764    [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,
 765    [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
 766    [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
 767    [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
 768    [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
 769    [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
 770    [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
 771    [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
 772    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 773};
 774
 775static void cache_clean_timer_cb(void *opaque)
 776{
 777    BlockDriverState *bs = opaque;
 778    BDRVQcow2State *s = bs->opaque;
 779    qcow2_cache_clean_unused(s->l2_table_cache);
 780    qcow2_cache_clean_unused(s->refcount_block_cache);
 781    timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
 782              (int64_t) s->cache_clean_interval * 1000);
 783}
 784
 785static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
 786{
 787    BDRVQcow2State *s = bs->opaque;
 788    if (s->cache_clean_interval > 0) {
 789        s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
 790                                             SCALE_MS, cache_clean_timer_cb,
 791                                             bs);
 792        timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
 793                  (int64_t) s->cache_clean_interval * 1000);
 794    }
 795}
 796
 797static void cache_clean_timer_del(BlockDriverState *bs)
 798{
 799    BDRVQcow2State *s = bs->opaque;
 800    if (s->cache_clean_timer) {
 801        timer_del(s->cache_clean_timer);
 802        timer_free(s->cache_clean_timer);
 803        s->cache_clean_timer = NULL;
 804    }
 805}
 806
 807static void qcow2_detach_aio_context(BlockDriverState *bs)
 808{
 809    cache_clean_timer_del(bs);
 810}
 811
 812static void qcow2_attach_aio_context(BlockDriverState *bs,
 813                                     AioContext *new_context)
 814{
 815    cache_clean_timer_init(bs, new_context);
 816}
 817
 818static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
 819                             uint64_t *l2_cache_size,
 820                             uint64_t *l2_cache_entry_size,
 821                             uint64_t *refcount_cache_size, Error **errp)
 822{
 823    BDRVQcow2State *s = bs->opaque;
 824    uint64_t combined_cache_size, l2_cache_max_setting;
 825    bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
 826    bool l2_cache_entry_size_set;
 827    int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
 828    uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
 829    uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
 830
 831    combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
 832    l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
 833    refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
 834    l2_cache_entry_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE);
 835
 836    combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
 837    l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE,
 838                                             DEFAULT_L2_CACHE_MAX_SIZE);
 839    *refcount_cache_size = qemu_opt_get_size(opts,
 840                                             QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
 841
 842    *l2_cache_entry_size = qemu_opt_get_size(
 843        opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
 844
 845    *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting);
 846
 847    if (combined_cache_size_set) {
 848        if (l2_cache_size_set && refcount_cache_size_set) {
 849            error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
 850                       " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
 851                       "at the same time");
 852            return;
 853        } else if (l2_cache_size_set &&
 854                   (l2_cache_max_setting > combined_cache_size)) {
 855            error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
 856                       QCOW2_OPT_CACHE_SIZE);
 857            return;
 858        } else if (*refcount_cache_size > combined_cache_size) {
 859            error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
 860                       QCOW2_OPT_CACHE_SIZE);
 861            return;
 862        }
 863
 864        if (l2_cache_size_set) {
 865            *refcount_cache_size = combined_cache_size - *l2_cache_size;
 866        } else if (refcount_cache_size_set) {
 867            *l2_cache_size = combined_cache_size - *refcount_cache_size;
 868        } else {
 869            /* Assign as much memory as possible to the L2 cache, and
 870             * use the remainder for the refcount cache */
 871            if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
 872                *l2_cache_size = max_l2_cache;
 873                *refcount_cache_size = combined_cache_size - *l2_cache_size;
 874            } else {
 875                *refcount_cache_size =
 876                    MIN(combined_cache_size, min_refcount_cache);
 877                *l2_cache_size = combined_cache_size - *refcount_cache_size;
 878            }
 879        }
 880    }
 881
 882    /*
 883     * If the L2 cache is not enough to cover the whole disk then
 884     * default to 4KB entries. Smaller entries reduce the cost of
 885     * loads and evictions and increase I/O performance.
 886     */
 887    if (*l2_cache_size < max_l2_cache && !l2_cache_entry_size_set) {
 888        *l2_cache_entry_size = MIN(s->cluster_size, 4096);
 889    }
 890
 891    /* l2_cache_size and refcount_cache_size are ensured to have at least
 892     * their minimum values in qcow2_update_options_prepare() */
 893
 894    if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
 895        *l2_cache_entry_size > s->cluster_size ||
 896        !is_power_of_2(*l2_cache_entry_size)) {
 897        error_setg(errp, "L2 cache entry size must be a power of two "
 898                   "between %d and the cluster size (%d)",
 899                   1 << MIN_CLUSTER_BITS, s->cluster_size);
 900        return;
 901    }
 902}
 903
 904typedef struct Qcow2ReopenState {
 905    Qcow2Cache *l2_table_cache;
 906    Qcow2Cache *refcount_block_cache;
 907    int l2_slice_size; /* Number of entries in a slice of the L2 table */
 908    bool use_lazy_refcounts;
 909    int overlap_check;
 910    bool discard_passthrough[QCOW2_DISCARD_MAX];
 911    uint64_t cache_clean_interval;
 912    QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
 913} Qcow2ReopenState;
 914
 915static int qcow2_update_options_prepare(BlockDriverState *bs,
 916                                        Qcow2ReopenState *r,
 917                                        QDict *options, int flags,
 918                                        Error **errp)
 919{
 920    BDRVQcow2State *s = bs->opaque;
 921    QemuOpts *opts = NULL;
 922    const char *opt_overlap_check, *opt_overlap_check_template;
 923    int overlap_check_template = 0;
 924    uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
 925    int i;
 926    const char *encryptfmt;
 927    QDict *encryptopts = NULL;
 928    Error *local_err = NULL;
 929    int ret;
 930
 931    qdict_extract_subqdict(options, &encryptopts, "encrypt.");
 932    encryptfmt = qdict_get_try_str(encryptopts, "format");
 933
 934    opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
 935    qemu_opts_absorb_qdict(opts, options, &local_err);
 936    if (local_err) {
 937        error_propagate(errp, local_err);
 938        ret = -EINVAL;
 939        goto fail;
 940    }
 941
 942    /* get L2 table/refcount block cache size from command line options */
 943    read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
 944                     &refcount_cache_size, &local_err);
 945    if (local_err) {
 946        error_propagate(errp, local_err);
 947        ret = -EINVAL;
 948        goto fail;
 949    }
 950
 951    l2_cache_size /= l2_cache_entry_size;
 952    if (l2_cache_size < MIN_L2_CACHE_SIZE) {
 953        l2_cache_size = MIN_L2_CACHE_SIZE;
 954    }
 955    if (l2_cache_size > INT_MAX) {
 956        error_setg(errp, "L2 cache size too big");
 957        ret = -EINVAL;
 958        goto fail;
 959    }
 960
 961    refcount_cache_size /= s->cluster_size;
 962    if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
 963        refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
 964    }
 965    if (refcount_cache_size > INT_MAX) {
 966        error_setg(errp, "Refcount cache size too big");
 967        ret = -EINVAL;
 968        goto fail;
 969    }
 970
 971    /* alloc new L2 table/refcount block cache, flush old one */
 972    if (s->l2_table_cache) {
 973        ret = qcow2_cache_flush(bs, s->l2_table_cache);
 974        if (ret) {
 975            error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
 976            goto fail;
 977        }
 978    }
 979
 980    if (s->refcount_block_cache) {
 981        ret = qcow2_cache_flush(bs, s->refcount_block_cache);
 982        if (ret) {
 983            error_setg_errno(errp, -ret,
 984                             "Failed to flush the refcount block cache");
 985            goto fail;
 986        }
 987    }
 988
 989    r->l2_slice_size = l2_cache_entry_size / sizeof(uint64_t);
 990    r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
 991                                           l2_cache_entry_size);
 992    r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
 993                                                 s->cluster_size);
 994    if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
 995        error_setg(errp, "Could not allocate metadata caches");
 996        ret = -ENOMEM;
 997        goto fail;
 998    }
 999
1000    /* New interval for cache cleanup timer */
1001    r->cache_clean_interval =
1002        qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
1003                            DEFAULT_CACHE_CLEAN_INTERVAL);
1004#ifndef CONFIG_LINUX
1005    if (r->cache_clean_interval != 0) {
1006        error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
1007                   " not supported on this host");
1008        ret = -EINVAL;
1009        goto fail;
1010    }
1011#endif
1012    if (r->cache_clean_interval > UINT_MAX) {
1013        error_setg(errp, "Cache clean interval too big");
1014        ret = -EINVAL;
1015        goto fail;
1016    }
1017
1018    /* lazy-refcounts; flush if going from enabled to disabled */
1019    r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
1020        (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
1021    if (r->use_lazy_refcounts && s->qcow_version < 3) {
1022        error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
1023                   "qemu 1.1 compatibility level");
1024        ret = -EINVAL;
1025        goto fail;
1026    }
1027
1028    if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
1029        ret = qcow2_mark_clean(bs);
1030        if (ret < 0) {
1031            error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
1032            goto fail;
1033        }
1034    }
1035
1036    /* Overlap check options */
1037    opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
1038    opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
1039    if (opt_overlap_check_template && opt_overlap_check &&
1040        strcmp(opt_overlap_check_template, opt_overlap_check))
1041    {
1042        error_setg(errp, "Conflicting values for qcow2 options '"
1043                   QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1044                   "' ('%s')", opt_overlap_check, opt_overlap_check_template);
1045        ret = -EINVAL;
1046        goto fail;
1047    }
1048    if (!opt_overlap_check) {
1049        opt_overlap_check = opt_overlap_check_template ?: "cached";
1050    }
1051
1052    if (!strcmp(opt_overlap_check, "none")) {
1053        overlap_check_template = 0;
1054    } else if (!strcmp(opt_overlap_check, "constant")) {
1055        overlap_check_template = QCOW2_OL_CONSTANT;
1056    } else if (!strcmp(opt_overlap_check, "cached")) {
1057        overlap_check_template = QCOW2_OL_CACHED;
1058    } else if (!strcmp(opt_overlap_check, "all")) {
1059        overlap_check_template = QCOW2_OL_ALL;
1060    } else {
1061        error_setg(errp, "Unsupported value '%s' for qcow2 option "
1062                   "'overlap-check'. Allowed are any of the following: "
1063                   "none, constant, cached, all", opt_overlap_check);
1064        ret = -EINVAL;
1065        goto fail;
1066    }
1067
1068    r->overlap_check = 0;
1069    for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
1070        /* overlap-check defines a template bitmask, but every flag may be
1071         * overwritten through the associated boolean option */
1072        r->overlap_check |=
1073            qemu_opt_get_bool(opts, overlap_bool_option_names[i],
1074                              overlap_check_template & (1 << i)) << i;
1075    }
1076
1077    r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1078    r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1079    r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1080        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1081                          flags & BDRV_O_UNMAP);
1082    r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1083        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1084    r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1085        qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1086
1087    switch (s->crypt_method_header) {
1088    case QCOW_CRYPT_NONE:
1089        if (encryptfmt) {
1090            error_setg(errp, "No encryption in image header, but options "
1091                       "specified format '%s'", encryptfmt);
1092            ret = -EINVAL;
1093            goto fail;
1094        }
1095        break;
1096
1097    case QCOW_CRYPT_AES:
1098        if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1099            error_setg(errp,
1100                       "Header reported 'aes' encryption format but "
1101                       "options specify '%s'", encryptfmt);
1102            ret = -EINVAL;
1103            goto fail;
1104        }
1105        qdict_put_str(encryptopts, "format", "qcow");
1106        r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1107        break;
1108
1109    case QCOW_CRYPT_LUKS:
1110        if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
1111            error_setg(errp,
1112                       "Header reported 'luks' encryption format but "
1113                       "options specify '%s'", encryptfmt);
1114            ret = -EINVAL;
1115            goto fail;
1116        }
1117        qdict_put_str(encryptopts, "format", "luks");
1118        r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1119        break;
1120
1121    default:
1122        error_setg(errp, "Unsupported encryption method %d",
1123                   s->crypt_method_header);
1124        break;
1125    }
1126    if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1127        ret = -EINVAL;
1128        goto fail;
1129    }
1130
1131    ret = 0;
1132fail:
1133    qobject_unref(encryptopts);
1134    qemu_opts_del(opts);
1135    opts = NULL;
1136    return ret;
1137}
1138
1139static void qcow2_update_options_commit(BlockDriverState *bs,
1140                                        Qcow2ReopenState *r)
1141{
1142    BDRVQcow2State *s = bs->opaque;
1143    int i;
1144
1145    if (s->l2_table_cache) {
1146        qcow2_cache_destroy(s->l2_table_cache);
1147    }
1148    if (s->refcount_block_cache) {
1149        qcow2_cache_destroy(s->refcount_block_cache);
1150    }
1151    s->l2_table_cache = r->l2_table_cache;
1152    s->refcount_block_cache = r->refcount_block_cache;
1153    s->l2_slice_size = r->l2_slice_size;
1154
1155    s->overlap_check = r->overlap_check;
1156    s->use_lazy_refcounts = r->use_lazy_refcounts;
1157
1158    for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1159        s->discard_passthrough[i] = r->discard_passthrough[i];
1160    }
1161
1162    if (s->cache_clean_interval != r->cache_clean_interval) {
1163        cache_clean_timer_del(bs);
1164        s->cache_clean_interval = r->cache_clean_interval;
1165        cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1166    }
1167
1168    qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1169    s->crypto_opts = r->crypto_opts;
1170}
1171
1172static void qcow2_update_options_abort(BlockDriverState *bs,
1173                                       Qcow2ReopenState *r)
1174{
1175    if (r->l2_table_cache) {
1176        qcow2_cache_destroy(r->l2_table_cache);
1177    }
1178    if (r->refcount_block_cache) {
1179        qcow2_cache_destroy(r->refcount_block_cache);
1180    }
1181    qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1182}
1183
1184static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1185                                int flags, Error **errp)
1186{
1187    Qcow2ReopenState r = {};
1188    int ret;
1189
1190    ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1191    if (ret >= 0) {
1192        qcow2_update_options_commit(bs, &r);
1193    } else {
1194        qcow2_update_options_abort(bs, &r);
1195    }
1196
1197    return ret;
1198}
1199
1200/* Called with s->lock held.  */
1201static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
1202                                      int flags, Error **errp)
1203{
1204    BDRVQcow2State *s = bs->opaque;
1205    unsigned int len, i;
1206    int ret = 0;
1207    QCowHeader header;
1208    Error *local_err = NULL;
1209    uint64_t ext_end;
1210    uint64_t l1_vm_state_index;
1211    bool update_header = false;
1212
1213    ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
1214    if (ret < 0) {
1215        error_setg_errno(errp, -ret, "Could not read qcow2 header");
1216        goto fail;
1217    }
1218    header.magic = be32_to_cpu(header.magic);
1219    header.version = be32_to_cpu(header.version);
1220    header.backing_file_offset = be64_to_cpu(header.backing_file_offset);
1221    header.backing_file_size = be32_to_cpu(header.backing_file_size);
1222    header.size = be64_to_cpu(header.size);
1223    header.cluster_bits = be32_to_cpu(header.cluster_bits);
1224    header.crypt_method = be32_to_cpu(header.crypt_method);
1225    header.l1_table_offset = be64_to_cpu(header.l1_table_offset);
1226    header.l1_size = be32_to_cpu(header.l1_size);
1227    header.refcount_table_offset = be64_to_cpu(header.refcount_table_offset);
1228    header.refcount_table_clusters =
1229        be32_to_cpu(header.refcount_table_clusters);
1230    header.snapshots_offset = be64_to_cpu(header.snapshots_offset);
1231    header.nb_snapshots = be32_to_cpu(header.nb_snapshots);
1232
1233    if (header.magic != QCOW_MAGIC) {
1234        error_setg(errp, "Image is not in qcow2 format");
1235        ret = -EINVAL;
1236        goto fail;
1237    }
1238    if (header.version < 2 || header.version > 3) {
1239        error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1240        ret = -ENOTSUP;
1241        goto fail;
1242    }
1243
1244    s->qcow_version = header.version;
1245
1246    /* Initialise cluster size */
1247    if (header.cluster_bits < MIN_CLUSTER_BITS ||
1248        header.cluster_bits > MAX_CLUSTER_BITS) {
1249        error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1250                   header.cluster_bits);
1251        ret = -EINVAL;
1252        goto fail;
1253    }
1254
1255    s->cluster_bits = header.cluster_bits;
1256    s->cluster_size = 1 << s->cluster_bits;
1257
1258    /* Initialise version 3 header fields */
1259    if (header.version == 2) {
1260        header.incompatible_features    = 0;
1261        header.compatible_features      = 0;
1262        header.autoclear_features       = 0;
1263        header.refcount_order           = 4;
1264        header.header_length            = 72;
1265    } else {
1266        header.incompatible_features =
1267            be64_to_cpu(header.incompatible_features);
1268        header.compatible_features = be64_to_cpu(header.compatible_features);
1269        header.autoclear_features = be64_to_cpu(header.autoclear_features);
1270        header.refcount_order = be32_to_cpu(header.refcount_order);
1271        header.header_length = be32_to_cpu(header.header_length);
1272
1273        if (header.header_length < 104) {
1274            error_setg(errp, "qcow2 header too short");
1275            ret = -EINVAL;
1276            goto fail;
1277        }
1278    }
1279
1280    if (header.header_length > s->cluster_size) {
1281        error_setg(errp, "qcow2 header exceeds cluster size");
1282        ret = -EINVAL;
1283        goto fail;
1284    }
1285
1286    if (header.header_length > sizeof(header)) {
1287        s->unknown_header_fields_size = header.header_length - sizeof(header);
1288        s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1289        ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
1290                         s->unknown_header_fields_size);
1291        if (ret < 0) {
1292            error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
1293                             "fields");
1294            goto fail;
1295        }
1296    }
1297
1298    if (header.backing_file_offset > s->cluster_size) {
1299        error_setg(errp, "Invalid backing file offset");
1300        ret = -EINVAL;
1301        goto fail;
1302    }
1303
1304    if (header.backing_file_offset) {
1305        ext_end = header.backing_file_offset;
1306    } else {
1307        ext_end = 1 << header.cluster_bits;
1308    }
1309
1310    /* Handle feature bits */
1311    s->incompatible_features    = header.incompatible_features;
1312    s->compatible_features      = header.compatible_features;
1313    s->autoclear_features       = header.autoclear_features;
1314
1315    if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1316        void *feature_table = NULL;
1317        qcow2_read_extensions(bs, header.header_length, ext_end,
1318                              &feature_table, flags, NULL, NULL);
1319        report_unsupported_feature(errp, feature_table,
1320                                   s->incompatible_features &
1321                                   ~QCOW2_INCOMPAT_MASK);
1322        ret = -ENOTSUP;
1323        g_free(feature_table);
1324        goto fail;
1325    }
1326
1327    if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
1328        /* Corrupt images may not be written to unless they are being repaired
1329         */
1330        if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
1331            error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
1332                       "read/write");
1333            ret = -EACCES;
1334            goto fail;
1335        }
1336    }
1337
1338    /* Check support for various header values */
1339    if (header.refcount_order > 6) {
1340        error_setg(errp, "Reference count entry width too large; may not "
1341                   "exceed 64 bits");
1342        ret = -EINVAL;
1343        goto fail;
1344    }
1345    s->refcount_order = header.refcount_order;
1346    s->refcount_bits = 1 << s->refcount_order;
1347    s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1348    s->refcount_max += s->refcount_max - 1;
1349
1350    s->crypt_method_header = header.crypt_method;
1351    if (s->crypt_method_header) {
1352        if (bdrv_uses_whitelist() &&
1353            s->crypt_method_header == QCOW_CRYPT_AES) {
1354            error_setg(errp,
1355                       "Use of AES-CBC encrypted qcow2 images is no longer "
1356                       "supported in system emulators");
1357            error_append_hint(errp,
1358                              "You can use 'qemu-img convert' to convert your "
1359                              "image to an alternative supported format, such "
1360                              "as unencrypted qcow2, or raw with the LUKS "
1361                              "format instead.\n");
1362            ret = -ENOSYS;
1363            goto fail;
1364        }
1365
1366        if (s->crypt_method_header == QCOW_CRYPT_AES) {
1367            s->crypt_physical_offset = false;
1368        } else {
1369            /* Assuming LUKS and any future crypt methods we
1370             * add will all use physical offsets, due to the
1371             * fact that the alternative is insecure...  */
1372            s->crypt_physical_offset = true;
1373        }
1374
1375        bs->encrypted = true;
1376    }
1377
1378    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
1379    s->l2_size = 1 << s->l2_bits;
1380    /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1381    s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
1382    s->refcount_block_size = 1 << s->refcount_block_bits;
1383    bs->total_sectors = header.size / BDRV_SECTOR_SIZE;
1384    s->csize_shift = (62 - (s->cluster_bits - 8));
1385    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1386    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
1387
1388    s->refcount_table_offset = header.refcount_table_offset;
1389    s->refcount_table_size =
1390        header.refcount_table_clusters << (s->cluster_bits - 3);
1391
1392    if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1393        error_setg(errp, "Image does not contain a reference count table");
1394        ret = -EINVAL;
1395        goto fail;
1396    }
1397
1398    ret = qcow2_validate_table(bs, s->refcount_table_offset,
1399                               header.refcount_table_clusters,
1400                               s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
1401                               "Reference count table", errp);
1402    if (ret < 0) {
1403        goto fail;
1404    }
1405
1406    /* The total size in bytes of the snapshot table is checked in
1407     * qcow2_read_snapshots() because the size of each snapshot is
1408     * variable and we don't know it yet.
1409     * Here we only check the offset and number of snapshots. */
1410    ret = qcow2_validate_table(bs, header.snapshots_offset,
1411                               header.nb_snapshots,
1412                               sizeof(QCowSnapshotHeader),
1413                               sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
1414                               "Snapshot table", errp);
1415    if (ret < 0) {
1416        goto fail;
1417    }
1418
1419    /* read the level 1 table */
1420    ret = qcow2_validate_table(bs, header.l1_table_offset,
1421                               header.l1_size, sizeof(uint64_t),
1422                               QCOW_MAX_L1_SIZE, "Active L1 table", errp);
1423    if (ret < 0) {
1424        goto fail;
1425    }
1426    s->l1_size = header.l1_size;
1427    s->l1_table_offset = header.l1_table_offset;
1428
1429    l1_vm_state_index = size_to_l1(s, header.size);
1430    if (l1_vm_state_index > INT_MAX) {
1431        error_setg(errp, "Image is too big");
1432        ret = -EFBIG;
1433        goto fail;
1434    }
1435    s->l1_vm_state_index = l1_vm_state_index;
1436
1437    /* the L1 table must contain at least enough entries to put
1438       header.size bytes */
1439    if (s->l1_size < s->l1_vm_state_index) {
1440        error_setg(errp, "L1 table is too small");
1441        ret = -EINVAL;
1442        goto fail;
1443    }
1444
1445    if (s->l1_size > 0) {
1446        s->l1_table = qemu_try_blockalign(bs->file->bs,
1447            ROUND_UP(s->l1_size * sizeof(uint64_t), 512));
1448        if (s->l1_table == NULL) {
1449            error_setg(errp, "Could not allocate L1 table");
1450            ret = -ENOMEM;
1451            goto fail;
1452        }
1453        ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
1454                         s->l1_size * sizeof(uint64_t));
1455        if (ret < 0) {
1456            error_setg_errno(errp, -ret, "Could not read L1 table");
1457            goto fail;
1458        }
1459        for(i = 0;i < s->l1_size; i++) {
1460            s->l1_table[i] = be64_to_cpu(s->l1_table[i]);
1461        }
1462    }
1463
1464    /* Parse driver-specific options */
1465    ret = qcow2_update_options(bs, options, flags, errp);
1466    if (ret < 0) {
1467        goto fail;
1468    }
1469
1470    s->flags = flags;
1471
1472    ret = qcow2_refcount_init(bs);
1473    if (ret != 0) {
1474        error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1475        goto fail;
1476    }
1477
1478    QLIST_INIT(&s->cluster_allocs);
1479    QTAILQ_INIT(&s->discards);
1480
1481    /* read qcow2 extensions */
1482    if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1483                              flags, &update_header, &local_err)) {
1484        error_propagate(errp, local_err);
1485        ret = -EINVAL;
1486        goto fail;
1487    }
1488
1489    /* Open external data file */
1490    s->data_file = bdrv_open_child(NULL, options, "data-file", bs, &child_file,
1491                                   true, &local_err);
1492    if (local_err) {
1493        error_propagate(errp, local_err);
1494        ret = -EINVAL;
1495        goto fail;
1496    }
1497
1498    if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) {
1499        if (!s->data_file && s->image_data_file) {
1500            s->data_file = bdrv_open_child(s->image_data_file, options,
1501                                           "data-file", bs, &child_file,
1502                                           false, errp);
1503            if (!s->data_file) {
1504                ret = -EINVAL;
1505                goto fail;
1506            }
1507        }
1508        if (!s->data_file) {
1509            error_setg(errp, "'data-file' is required for this image");
1510            ret = -EINVAL;
1511            goto fail;
1512        }
1513    } else {
1514        if (s->data_file) {
1515            error_setg(errp, "'data-file' can only be set for images with an "
1516                             "external data file");
1517            ret = -EINVAL;
1518            goto fail;
1519        }
1520
1521        s->data_file = bs->file;
1522
1523        if (data_file_is_raw(bs)) {
1524            error_setg(errp, "data-file-raw requires a data file");
1525            ret = -EINVAL;
1526            goto fail;
1527        }
1528    }
1529
1530    /* qcow2_read_extension may have set up the crypto context
1531     * if the crypt method needs a header region, some methods
1532     * don't need header extensions, so must check here
1533     */
1534    if (s->crypt_method_header && !s->crypto) {
1535        if (s->crypt_method_header == QCOW_CRYPT_AES) {
1536            unsigned int cflags = 0;
1537            if (flags & BDRV_O_NO_IO) {
1538                cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1539            }
1540            s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
1541                                           NULL, NULL, cflags,
1542                                           QCOW2_MAX_THREADS, errp);
1543            if (!s->crypto) {
1544                ret = -EINVAL;
1545                goto fail;
1546            }
1547        } else if (!(flags & BDRV_O_NO_IO)) {
1548            error_setg(errp, "Missing CRYPTO header for crypt method %d",
1549                       s->crypt_method_header);
1550            ret = -EINVAL;
1551            goto fail;
1552        }
1553    }
1554
1555    /* read the backing file name */
1556    if (header.backing_file_offset != 0) {
1557        len = header.backing_file_size;
1558        if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1559            len >= sizeof(bs->backing_file)) {
1560            error_setg(errp, "Backing file name too long");
1561            ret = -EINVAL;
1562            goto fail;
1563        }
1564        ret = bdrv_pread(bs->file, header.backing_file_offset,
1565                         bs->auto_backing_file, len);
1566        if (ret < 0) {
1567            error_setg_errno(errp, -ret, "Could not read backing file name");
1568            goto fail;
1569        }
1570        bs->auto_backing_file[len] = '\0';
1571        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
1572                bs->auto_backing_file);
1573        s->image_backing_file = g_strdup(bs->auto_backing_file);
1574    }
1575
1576    /* Internal snapshots */
1577    s->snapshots_offset = header.snapshots_offset;
1578    s->nb_snapshots = header.nb_snapshots;
1579
1580    ret = qcow2_read_snapshots(bs);
1581    if (ret < 0) {
1582        error_setg_errno(errp, -ret, "Could not read snapshots");
1583        goto fail;
1584    }
1585
1586    /* Clear unknown autoclear feature bits */
1587    update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1588    update_header =
1589        update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1590    if (update_header) {
1591        s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1592    }
1593
1594    /* == Handle persistent dirty bitmaps ==
1595     *
1596     * We want load dirty bitmaps in three cases:
1597     *
1598     * 1. Normal open of the disk in active mode, not related to invalidation
1599     *    after migration.
1600     *
1601     * 2. Invalidation of the target vm after pre-copy phase of migration, if
1602     *    bitmaps are _not_ migrating through migration channel, i.e.
1603     *    'dirty-bitmaps' capability is disabled.
1604     *
1605     * 3. Invalidation of source vm after failed or canceled migration.
1606     *    This is a very interesting case. There are two possible types of
1607     *    bitmaps:
1608     *
1609     *    A. Stored on inactivation and removed. They should be loaded from the
1610     *       image.
1611     *
1612     *    B. Not stored: not-persistent bitmaps and bitmaps, migrated through
1613     *       the migration channel (with dirty-bitmaps capability).
1614     *
1615     *    On the other hand, there are two possible sub-cases:
1616     *
1617     *    3.1 disk was changed by somebody else while were inactive. In this
1618     *        case all in-RAM dirty bitmaps (both persistent and not) are
1619     *        definitely invalid. And we don't have any method to determine
1620     *        this.
1621     *
1622     *        Simple and safe thing is to just drop all the bitmaps of type B on
1623     *        inactivation. But in this case we lose bitmaps in valid 4.2 case.
1624     *
1625     *        On the other hand, resuming source vm, if disk was already changed
1626     *        is a bad thing anyway: not only bitmaps, the whole vm state is
1627     *        out of sync with disk.
1628     *
1629     *        This means, that user or management tool, who for some reason
1630     *        decided to resume source vm, after disk was already changed by
1631     *        target vm, should at least drop all dirty bitmaps by hand.
1632     *
1633     *        So, we can ignore this case for now, but TODO: "generation"
1634     *        extension for qcow2, to determine, that image was changed after
1635     *        last inactivation. And if it is changed, we will drop (or at least
1636     *        mark as 'invalid' all the bitmaps of type B, both persistent
1637     *        and not).
1638     *
1639     *    3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
1640     *        to disk ('dirty-bitmaps' capability disabled), or not saved
1641     *        ('dirty-bitmaps' capability enabled), but we don't need to care
1642     *        of: let's load bitmaps as always: stored bitmaps will be loaded,
1643     *        and not stored has flag IN_USE=1 in the image and will be skipped
1644     *        on loading.
1645     *
1646     * One remaining possible case when we don't want load bitmaps:
1647     *
1648     * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
1649     *    will be loaded on invalidation, no needs try loading them before)
1650     */
1651
1652    if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) {
1653        /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
1654        bool header_updated = qcow2_load_dirty_bitmaps(bs, &local_err);
1655
1656        update_header = update_header && !header_updated;
1657    }
1658    if (local_err != NULL) {
1659        error_propagate(errp, local_err);
1660        ret = -EINVAL;
1661        goto fail;
1662    }
1663
1664    if (update_header) {
1665        ret = qcow2_update_header(bs);
1666        if (ret < 0) {
1667            error_setg_errno(errp, -ret, "Could not update qcow2 header");
1668            goto fail;
1669        }
1670    }
1671
1672    bs->supported_zero_flags = header.version >= 3 ? BDRV_REQ_MAY_UNMAP : 0;
1673
1674    /* Repair image if dirty */
1675    if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1676        (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1677        BdrvCheckResult result = {0};
1678
1679        ret = qcow2_co_check_locked(bs, &result,
1680                                    BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1681        if (ret < 0 || result.check_errors) {
1682            if (ret >= 0) {
1683                ret = -EIO;
1684            }
1685            error_setg_errno(errp, -ret, "Could not repair dirty image");
1686            goto fail;
1687        }
1688    }
1689
1690#ifdef DEBUG_ALLOC
1691    {
1692        BdrvCheckResult result = {0};
1693        qcow2_check_refcounts(bs, &result, 0);
1694    }
1695#endif
1696
1697    qemu_co_queue_init(&s->thread_task_queue);
1698
1699    return ret;
1700
1701 fail:
1702    g_free(s->image_data_file);
1703    if (has_data_file(bs)) {
1704        bdrv_unref_child(bs, s->data_file);
1705    }
1706    g_free(s->unknown_header_fields);
1707    cleanup_unknown_header_ext(bs);
1708    qcow2_free_snapshots(bs);
1709    qcow2_refcount_close(bs);
1710    qemu_vfree(s->l1_table);
1711    /* else pre-write overlap checks in cache_destroy may crash */
1712    s->l1_table = NULL;
1713    cache_clean_timer_del(bs);
1714    if (s->l2_table_cache) {
1715        qcow2_cache_destroy(s->l2_table_cache);
1716    }
1717    if (s->refcount_block_cache) {
1718        qcow2_cache_destroy(s->refcount_block_cache);
1719    }
1720    qcrypto_block_free(s->crypto);
1721    qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1722    return ret;
1723}
1724
1725typedef struct QCow2OpenCo {
1726    BlockDriverState *bs;
1727    QDict *options;
1728    int flags;
1729    Error **errp;
1730    int ret;
1731} QCow2OpenCo;
1732
1733static void coroutine_fn qcow2_open_entry(void *opaque)
1734{
1735    QCow2OpenCo *qoc = opaque;
1736    BDRVQcow2State *s = qoc->bs->opaque;
1737
1738    qemu_co_mutex_lock(&s->lock);
1739    qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
1740    qemu_co_mutex_unlock(&s->lock);
1741}
1742
1743static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
1744                      Error **errp)
1745{
1746    BDRVQcow2State *s = bs->opaque;
1747    QCow2OpenCo qoc = {
1748        .bs = bs,
1749        .options = options,
1750        .flags = flags,
1751        .errp = errp,
1752        .ret = -EINPROGRESS
1753    };
1754
1755    bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
1756                               false, errp);
1757    if (!bs->file) {
1758        return -EINVAL;
1759    }
1760
1761    /* Initialise locks */
1762    qemu_co_mutex_init(&s->lock);
1763
1764    if (qemu_in_coroutine()) {
1765        /* From bdrv_co_create.  */
1766        qcow2_open_entry(&qoc);
1767    } else {
1768        assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1769        qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc));
1770        BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
1771    }
1772    return qoc.ret;
1773}
1774
1775static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1776{
1777    BDRVQcow2State *s = bs->opaque;
1778
1779    if (bs->encrypted) {
1780        /* Encryption works on a sector granularity */
1781        bs->bl.request_alignment = qcrypto_block_get_sector_size(s->crypto);
1782    }
1783    bs->bl.pwrite_zeroes_alignment = s->cluster_size;
1784    bs->bl.pdiscard_alignment = s->cluster_size;
1785}
1786
1787static int qcow2_reopen_prepare(BDRVReopenState *state,
1788                                BlockReopenQueue *queue, Error **errp)
1789{
1790    Qcow2ReopenState *r;
1791    int ret;
1792
1793    r = g_new0(Qcow2ReopenState, 1);
1794    state->opaque = r;
1795
1796    ret = qcow2_update_options_prepare(state->bs, r, state->options,
1797                                       state->flags, errp);
1798    if (ret < 0) {
1799        goto fail;
1800    }
1801
1802    /* We need to write out any unwritten data if we reopen read-only. */
1803    if ((state->flags & BDRV_O_RDWR) == 0) {
1804        ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1805        if (ret < 0) {
1806            goto fail;
1807        }
1808
1809        ret = bdrv_flush(state->bs);
1810        if (ret < 0) {
1811            goto fail;
1812        }
1813
1814        ret = qcow2_mark_clean(state->bs);
1815        if (ret < 0) {
1816            goto fail;
1817        }
1818    }
1819
1820    return 0;
1821
1822fail:
1823    qcow2_update_options_abort(state->bs, r);
1824    g_free(r);
1825    return ret;
1826}
1827
1828static void qcow2_reopen_commit(BDRVReopenState *state)
1829{
1830    qcow2_update_options_commit(state->bs, state->opaque);
1831    g_free(state->opaque);
1832}
1833
1834static void qcow2_reopen_abort(BDRVReopenState *state)
1835{
1836    qcow2_update_options_abort(state->bs, state->opaque);
1837    g_free(state->opaque);
1838}
1839
1840static void qcow2_join_options(QDict *options, QDict *old_options)
1841{
1842    bool has_new_overlap_template =
1843        qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
1844        qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
1845    bool has_new_total_cache_size =
1846        qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
1847    bool has_all_cache_options;
1848
1849    /* New overlap template overrides all old overlap options */
1850    if (has_new_overlap_template) {
1851        qdict_del(old_options, QCOW2_OPT_OVERLAP);
1852        qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
1853        qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
1854        qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
1855        qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
1856        qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
1857        qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
1858        qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
1859        qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
1860        qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
1861    }
1862
1863    /* New total cache size overrides all old options */
1864    if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
1865        qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
1866        qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1867    }
1868
1869    qdict_join(options, old_options, false);
1870
1871    /*
1872     * If after merging all cache size options are set, an old total size is
1873     * overwritten. Do keep all options, however, if all three are new. The
1874     * resulting error message is what we want to happen.
1875     */
1876    has_all_cache_options =
1877        qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
1878        qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
1879        qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1880
1881    if (has_all_cache_options && !has_new_total_cache_size) {
1882        qdict_del(options, QCOW2_OPT_CACHE_SIZE);
1883    }
1884}
1885
1886static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
1887                                              bool want_zero,
1888                                              int64_t offset, int64_t count,
1889                                              int64_t *pnum, int64_t *map,
1890                                              BlockDriverState **file)
1891{
1892    BDRVQcow2State *s = bs->opaque;
1893    uint64_t cluster_offset;
1894    int index_in_cluster, ret;
1895    unsigned int bytes;
1896    int status = 0;
1897
1898    if (!s->metadata_preallocation_checked) {
1899        ret = qcow2_detect_metadata_preallocation(bs);
1900        s->metadata_preallocation = (ret == 1);
1901        s->metadata_preallocation_checked = true;
1902    }
1903
1904    bytes = MIN(INT_MAX, count);
1905    qemu_co_mutex_lock(&s->lock);
1906    ret = qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset);
1907    qemu_co_mutex_unlock(&s->lock);
1908    if (ret < 0) {
1909        return ret;
1910    }
1911
1912    *pnum = bytes;
1913
1914    if ((ret == QCOW2_CLUSTER_NORMAL || ret == QCOW2_CLUSTER_ZERO_ALLOC) &&
1915        !s->crypto) {
1916        index_in_cluster = offset & (s->cluster_size - 1);
1917        *map = cluster_offset | index_in_cluster;
1918        *file = s->data_file->bs;
1919        status |= BDRV_BLOCK_OFFSET_VALID;
1920    }
1921    if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
1922        status |= BDRV_BLOCK_ZERO;
1923    } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1924        status |= BDRV_BLOCK_DATA;
1925    }
1926    if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) &&
1927        (status & BDRV_BLOCK_OFFSET_VALID))
1928    {
1929        status |= BDRV_BLOCK_RECURSE;
1930    }
1931    return status;
1932}
1933
1934static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
1935                                            QCowL2Meta **pl2meta,
1936                                            bool link_l2)
1937{
1938    int ret = 0;
1939    QCowL2Meta *l2meta = *pl2meta;
1940
1941    while (l2meta != NULL) {
1942        QCowL2Meta *next;
1943
1944        if (link_l2) {
1945            ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1946            if (ret) {
1947                goto out;
1948            }
1949        } else {
1950            qcow2_alloc_cluster_abort(bs, l2meta);
1951        }
1952
1953        /* Take the request off the list of running requests */
1954        if (l2meta->nb_clusters != 0) {
1955            QLIST_REMOVE(l2meta, next_in_flight);
1956        }
1957
1958        qemu_co_queue_restart_all(&l2meta->dependent_requests);
1959
1960        next = l2meta->next;
1961        g_free(l2meta);
1962        l2meta = next;
1963    }
1964out:
1965    *pl2meta = l2meta;
1966    return ret;
1967}
1968
1969static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
1970                                        uint64_t bytes, QEMUIOVector *qiov,
1971                                        int flags)
1972{
1973    BDRVQcow2State *s = bs->opaque;
1974    int offset_in_cluster;
1975    int ret;
1976    unsigned int cur_bytes; /* number of bytes in current iteration */
1977    uint64_t cluster_offset = 0;
1978    uint64_t bytes_done = 0;
1979    QEMUIOVector hd_qiov;
1980    uint8_t *cluster_data = NULL;
1981
1982    qemu_iovec_init(&hd_qiov, qiov->niov);
1983
1984    while (bytes != 0) {
1985
1986        /* prepare next request */
1987        cur_bytes = MIN(bytes, INT_MAX);
1988        if (s->crypto) {
1989            cur_bytes = MIN(cur_bytes,
1990                            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1991        }
1992
1993        qemu_co_mutex_lock(&s->lock);
1994        ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
1995        qemu_co_mutex_unlock(&s->lock);
1996        if (ret < 0) {
1997            goto fail;
1998        }
1999
2000        offset_in_cluster = offset_into_cluster(s, offset);
2001
2002        qemu_iovec_reset(&hd_qiov);
2003        qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
2004
2005        switch (ret) {
2006        case QCOW2_CLUSTER_UNALLOCATED:
2007
2008            if (bs->backing) {
2009                BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
2010                ret = bdrv_co_preadv(bs->backing, offset, cur_bytes,
2011                                     &hd_qiov, 0);
2012                if (ret < 0) {
2013                    goto fail;
2014                }
2015            } else {
2016                /* Note: in this case, no need to wait */
2017                qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
2018            }
2019            break;
2020
2021        case QCOW2_CLUSTER_ZERO_PLAIN:
2022        case QCOW2_CLUSTER_ZERO_ALLOC:
2023            qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
2024            break;
2025
2026        case QCOW2_CLUSTER_COMPRESSED:
2027            ret = qcow2_co_preadv_compressed(bs, cluster_offset,
2028                                             offset, cur_bytes,
2029                                             &hd_qiov);
2030            if (ret < 0) {
2031                goto fail;
2032            }
2033
2034            break;
2035
2036        case QCOW2_CLUSTER_NORMAL:
2037            if ((cluster_offset & 511) != 0) {
2038                ret = -EIO;
2039                goto fail;
2040            }
2041
2042            if (bs->encrypted) {
2043                assert(s->crypto);
2044
2045                /*
2046                 * For encrypted images, read everything into a temporary
2047                 * contiguous buffer on which the AES functions can work.
2048                 */
2049                if (!cluster_data) {
2050                    cluster_data =
2051                        qemu_try_blockalign(s->data_file->bs,
2052                                            QCOW_MAX_CRYPT_CLUSTERS
2053                                            * s->cluster_size);
2054                    if (cluster_data == NULL) {
2055                        ret = -ENOMEM;
2056                        goto fail;
2057                    }
2058                }
2059
2060                assert(cur_bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2061                qemu_iovec_reset(&hd_qiov);
2062                qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
2063            }
2064
2065            BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
2066            ret = bdrv_co_preadv(s->data_file,
2067                                 cluster_offset + offset_in_cluster,
2068                                 cur_bytes, &hd_qiov, 0);
2069            if (ret < 0) {
2070                goto fail;
2071            }
2072            if (bs->encrypted) {
2073                assert(s->crypto);
2074                assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
2075                assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
2076                if (qcow2_co_decrypt(bs, cluster_offset, offset,
2077                                     cluster_data, cur_bytes) < 0) {
2078                    ret = -EIO;
2079                    goto fail;
2080                }
2081                qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_bytes);
2082            }
2083            break;
2084
2085        default:
2086            g_assert_not_reached();
2087            ret = -EIO;
2088            goto fail;
2089        }
2090
2091        bytes -= cur_bytes;
2092        offset += cur_bytes;
2093        bytes_done += cur_bytes;
2094    }
2095    ret = 0;
2096
2097fail:
2098    qemu_iovec_destroy(&hd_qiov);
2099    qemu_vfree(cluster_data);
2100
2101    return ret;
2102}
2103
2104/* Check if it's possible to merge a write request with the writing of
2105 * the data from the COW regions */
2106static bool merge_cow(uint64_t offset, unsigned bytes,
2107                      QEMUIOVector *hd_qiov, QCowL2Meta *l2meta)
2108{
2109    QCowL2Meta *m;
2110
2111    for (m = l2meta; m != NULL; m = m->next) {
2112        /* If both COW regions are empty then there's nothing to merge */
2113        if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
2114            continue;
2115        }
2116
2117        /* If COW regions are handled already, skip this too */
2118        if (m->skip_cow) {
2119            continue;
2120        }
2121
2122        /* The data (middle) region must be immediately after the
2123         * start region */
2124        if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
2125            continue;
2126        }
2127
2128        /* The end region must be immediately after the data (middle)
2129         * region */
2130        if (m->offset + m->cow_end.offset != offset + bytes) {
2131            continue;
2132        }
2133
2134        /* Make sure that adding both COW regions to the QEMUIOVector
2135         * does not exceed IOV_MAX */
2136        if (hd_qiov->niov > IOV_MAX - 2) {
2137            continue;
2138        }
2139
2140        m->data_qiov = hd_qiov;
2141        return true;
2142    }
2143
2144    return false;
2145}
2146
2147static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes)
2148{
2149    int64_t nr;
2150    return !bytes ||
2151        (!bdrv_is_allocated_above(bs, NULL, false, offset, bytes, &nr) &&
2152         nr == bytes);
2153}
2154
2155static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
2156{
2157    /*
2158     * This check is designed for optimization shortcut so it must be
2159     * efficient.
2160     * Instead of is_zero(), use is_unallocated() as it is faster (but not
2161     * as accurate and can result in false negatives).
2162     */
2163    return is_unallocated(bs, m->offset + m->cow_start.offset,
2164                          m->cow_start.nb_bytes) &&
2165           is_unallocated(bs, m->offset + m->cow_end.offset,
2166                          m->cow_end.nb_bytes);
2167}
2168
2169static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)
2170{
2171    BDRVQcow2State *s = bs->opaque;
2172    QCowL2Meta *m;
2173
2174    if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) {
2175        return 0;
2176    }
2177
2178    if (bs->encrypted) {
2179        return 0;
2180    }
2181
2182    for (m = l2meta; m != NULL; m = m->next) {
2183        int ret;
2184
2185        if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) {
2186            continue;
2187        }
2188
2189        if (!is_zero_cow(bs, m)) {
2190            continue;
2191        }
2192
2193        /*
2194         * instead of writing zero COW buffers,
2195         * efficiently zero out the whole clusters
2196         */
2197
2198        ret = qcow2_pre_write_overlap_check(bs, 0, m->alloc_offset,
2199                                            m->nb_clusters * s->cluster_size,
2200                                            true);
2201        if (ret < 0) {
2202            return ret;
2203        }
2204
2205        BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
2206        ret = bdrv_co_pwrite_zeroes(s->data_file, m->alloc_offset,
2207                                    m->nb_clusters * s->cluster_size,
2208                                    BDRV_REQ_NO_FALLBACK);
2209        if (ret < 0) {
2210            if (ret != -ENOTSUP && ret != -EAGAIN) {
2211                return ret;
2212            }
2213            continue;
2214        }
2215
2216        trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters);
2217        m->skip_cow = true;
2218    }
2219    return 0;
2220}
2221
2222static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
2223                                         uint64_t bytes, QEMUIOVector *qiov,
2224                                         int flags)
2225{
2226    BDRVQcow2State *s = bs->opaque;
2227    int offset_in_cluster;
2228    int ret;
2229    unsigned int cur_bytes; /* number of sectors in current iteration */
2230    uint64_t cluster_offset;
2231    QEMUIOVector hd_qiov;
2232    uint64_t bytes_done = 0;
2233    uint8_t *cluster_data = NULL;
2234    QCowL2Meta *l2meta = NULL;
2235
2236    trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
2237
2238    qemu_iovec_init(&hd_qiov, qiov->niov);
2239
2240    qemu_co_mutex_lock(&s->lock);
2241
2242    while (bytes != 0) {
2243
2244        l2meta = NULL;
2245
2246        trace_qcow2_writev_start_part(qemu_coroutine_self());
2247        offset_in_cluster = offset_into_cluster(s, offset);
2248        cur_bytes = MIN(bytes, INT_MAX);
2249        if (bs->encrypted) {
2250            cur_bytes = MIN(cur_bytes,
2251                            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2252                            - offset_in_cluster);
2253        }
2254
2255        ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2256                                         &cluster_offset, &l2meta);
2257        if (ret < 0) {
2258            goto out_locked;
2259        }
2260
2261        assert((cluster_offset & 511) == 0);
2262
2263        ret = qcow2_pre_write_overlap_check(bs, 0,
2264                                            cluster_offset + offset_in_cluster,
2265                                            cur_bytes, true);
2266        if (ret < 0) {
2267            goto out_locked;
2268        }
2269
2270        qemu_co_mutex_unlock(&s->lock);
2271
2272        qemu_iovec_reset(&hd_qiov);
2273        qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
2274
2275        if (bs->encrypted) {
2276            assert(s->crypto);
2277            if (!cluster_data) {
2278                cluster_data = qemu_try_blockalign(bs->file->bs,
2279                                                   QCOW_MAX_CRYPT_CLUSTERS
2280                                                   * s->cluster_size);
2281                if (cluster_data == NULL) {
2282                    ret = -ENOMEM;
2283                    goto out_unlocked;
2284                }
2285            }
2286
2287            assert(hd_qiov.size <=
2288                   QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2289            qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
2290
2291            if (qcow2_co_encrypt(bs, cluster_offset, offset,
2292                                 cluster_data, cur_bytes) < 0) {
2293                ret = -EIO;
2294                goto out_unlocked;
2295            }
2296
2297            qemu_iovec_reset(&hd_qiov);
2298            qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
2299        }
2300
2301        /* Try to efficiently initialize the physical space with zeroes */
2302        ret = handle_alloc_space(bs, l2meta);
2303        if (ret < 0) {
2304            goto out_unlocked;
2305        }
2306
2307        /* If we need to do COW, check if it's possible to merge the
2308         * writing of the guest data together with that of the COW regions.
2309         * If it's not possible (or not necessary) then write the
2310         * guest data now. */
2311        if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
2312            BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
2313            trace_qcow2_writev_data(qemu_coroutine_self(),
2314                                    cluster_offset + offset_in_cluster);
2315            ret = bdrv_co_pwritev(s->data_file,
2316                                  cluster_offset + offset_in_cluster,
2317                                  cur_bytes, &hd_qiov, 0);
2318            if (ret < 0) {
2319                goto out_unlocked;
2320            }
2321        }
2322
2323        qemu_co_mutex_lock(&s->lock);
2324
2325        ret = qcow2_handle_l2meta(bs, &l2meta, true);
2326        if (ret) {
2327            goto out_locked;
2328        }
2329
2330        bytes -= cur_bytes;
2331        offset += cur_bytes;
2332        bytes_done += cur_bytes;
2333        trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
2334    }
2335    ret = 0;
2336    goto out_locked;
2337
2338out_unlocked:
2339    qemu_co_mutex_lock(&s->lock);
2340
2341out_locked:
2342    qcow2_handle_l2meta(bs, &l2meta, false);
2343
2344    qemu_co_mutex_unlock(&s->lock);
2345
2346    qemu_iovec_destroy(&hd_qiov);
2347    qemu_vfree(cluster_data);
2348    trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
2349
2350    return ret;
2351}
2352
2353static int qcow2_inactivate(BlockDriverState *bs)
2354{
2355    BDRVQcow2State *s = bs->opaque;
2356    int ret, result = 0;
2357    Error *local_err = NULL;
2358
2359    qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
2360    if (local_err != NULL) {
2361        result = -EINVAL;
2362        error_reportf_err(local_err, "Lost persistent bitmaps during "
2363                          "inactivation of node '%s': ",
2364                          bdrv_get_device_or_node_name(bs));
2365    }
2366
2367    ret = qcow2_cache_flush(bs, s->l2_table_cache);
2368    if (ret) {
2369        result = ret;
2370        error_report("Failed to flush the L2 table cache: %s",
2371                     strerror(-ret));
2372    }
2373
2374    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2375    if (ret) {
2376        result = ret;
2377        error_report("Failed to flush the refcount block cache: %s",
2378                     strerror(-ret));
2379    }
2380
2381    if (result == 0) {
2382        qcow2_mark_clean(bs);
2383    }
2384
2385    return result;
2386}
2387
2388static void qcow2_close(BlockDriverState *bs)
2389{
2390    BDRVQcow2State *s = bs->opaque;
2391    qemu_vfree(s->l1_table);
2392    /* else pre-write overlap checks in cache_destroy may crash */
2393    s->l1_table = NULL;
2394
2395    if (!(s->flags & BDRV_O_INACTIVE)) {
2396        qcow2_inactivate(bs);
2397    }
2398
2399    cache_clean_timer_del(bs);
2400    qcow2_cache_destroy(s->l2_table_cache);
2401    qcow2_cache_destroy(s->refcount_block_cache);
2402
2403    qcrypto_block_free(s->crypto);
2404    s->crypto = NULL;
2405
2406    g_free(s->unknown_header_fields);
2407    cleanup_unknown_header_ext(bs);
2408
2409    g_free(s->image_data_file);
2410    g_free(s->image_backing_file);
2411    g_free(s->image_backing_format);
2412
2413    if (has_data_file(bs)) {
2414        bdrv_unref_child(bs, s->data_file);
2415    }
2416
2417    qcow2_refcount_close(bs);
2418    qcow2_free_snapshots(bs);
2419}
2420
2421static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
2422                                                   Error **errp)
2423{
2424    BDRVQcow2State *s = bs->opaque;
2425    int flags = s->flags;
2426    QCryptoBlock *crypto = NULL;
2427    QDict *options;
2428    Error *local_err = NULL;
2429    int ret;
2430
2431    /*
2432     * Backing files are read-only which makes all of their metadata immutable,
2433     * that means we don't have to worry about reopening them here.
2434     */
2435
2436    crypto = s->crypto;
2437    s->crypto = NULL;
2438
2439    qcow2_close(bs);
2440
2441    memset(s, 0, sizeof(BDRVQcow2State));
2442    options = qdict_clone_shallow(bs->options);
2443
2444    flags &= ~BDRV_O_INACTIVE;
2445    qemu_co_mutex_lock(&s->lock);
2446    ret = qcow2_do_open(bs, options, flags, &local_err);
2447    qemu_co_mutex_unlock(&s->lock);
2448    qobject_unref(options);
2449    if (local_err) {
2450        error_propagate_prepend(errp, local_err,
2451                                "Could not reopen qcow2 layer: ");
2452        bs->drv = NULL;
2453        return;
2454    } else if (ret < 0) {
2455        error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
2456        bs->drv = NULL;
2457        return;
2458    }
2459
2460    s->crypto = crypto;
2461}
2462
2463static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2464    size_t len, size_t buflen)
2465{
2466    QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2467    size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2468
2469    if (buflen < ext_len) {
2470        return -ENOSPC;
2471    }
2472
2473    *ext_backing_fmt = (QCowExtension) {
2474        .magic  = cpu_to_be32(magic),
2475        .len    = cpu_to_be32(len),
2476    };
2477
2478    if (len) {
2479        memcpy(buf + sizeof(QCowExtension), s, len);
2480    }
2481
2482    return ext_len;
2483}
2484
2485/*
2486 * Updates the qcow2 header, including the variable length parts of it, i.e.
2487 * the backing file name and all extensions. qcow2 was not designed to allow
2488 * such changes, so if we run out of space (we can only use the first cluster)
2489 * this function may fail.
2490 *
2491 * Returns 0 on success, -errno in error cases.
2492 */
2493int qcow2_update_header(BlockDriverState *bs)
2494{
2495    BDRVQcow2State *s = bs->opaque;
2496    QCowHeader *header;
2497    char *buf;
2498    size_t buflen = s->cluster_size;
2499    int ret;
2500    uint64_t total_size;
2501    uint32_t refcount_table_clusters;
2502    size_t header_length;
2503    Qcow2UnknownHeaderExtension *uext;
2504
2505    buf = qemu_blockalign(bs, buflen);
2506
2507    /* Header structure */
2508    header = (QCowHeader*) buf;
2509
2510    if (buflen < sizeof(*header)) {
2511        ret = -ENOSPC;
2512        goto fail;
2513    }
2514
2515    header_length = sizeof(*header) + s->unknown_header_fields_size;
2516    total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2517    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2518
2519    *header = (QCowHeader) {
2520        /* Version 2 fields */
2521        .magic                  = cpu_to_be32(QCOW_MAGIC),
2522        .version                = cpu_to_be32(s->qcow_version),
2523        .backing_file_offset    = 0,
2524        .backing_file_size      = 0,
2525        .cluster_bits           = cpu_to_be32(s->cluster_bits),
2526        .size                   = cpu_to_be64(total_size),
2527        .crypt_method           = cpu_to_be32(s->crypt_method_header),
2528        .l1_size                = cpu_to_be32(s->l1_size),
2529        .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
2530        .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
2531        .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2532        .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
2533        .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
2534
2535        /* Version 3 fields */
2536        .incompatible_features  = cpu_to_be64(s->incompatible_features),
2537        .compatible_features    = cpu_to_be64(s->compatible_features),
2538        .autoclear_features     = cpu_to_be64(s->autoclear_features),
2539        .refcount_order         = cpu_to_be32(s->refcount_order),
2540        .header_length          = cpu_to_be32(header_length),
2541    };
2542
2543    /* For older versions, write a shorter header */
2544    switch (s->qcow_version) {
2545    case 2:
2546        ret = offsetof(QCowHeader, incompatible_features);
2547        break;
2548    case 3:
2549        ret = sizeof(*header);
2550        break;
2551    default:
2552        ret = -EINVAL;
2553        goto fail;
2554    }
2555
2556    buf += ret;
2557    buflen -= ret;
2558    memset(buf, 0, buflen);
2559
2560    /* Preserve any unknown field in the header */
2561    if (s->unknown_header_fields_size) {
2562        if (buflen < s->unknown_header_fields_size) {
2563            ret = -ENOSPC;
2564            goto fail;
2565        }
2566
2567        memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
2568        buf += s->unknown_header_fields_size;
2569        buflen -= s->unknown_header_fields_size;
2570    }
2571
2572    /* Backing file format header extension */
2573    if (s->image_backing_format) {
2574        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
2575                             s->image_backing_format,
2576                             strlen(s->image_backing_format),
2577                             buflen);
2578        if (ret < 0) {
2579            goto fail;
2580        }
2581
2582        buf += ret;
2583        buflen -= ret;
2584    }
2585
2586    /* External data file header extension */
2587    if (has_data_file(bs) && s->image_data_file) {
2588        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE,
2589                             s->image_data_file, strlen(s->image_data_file),
2590                             buflen);
2591        if (ret < 0) {
2592            goto fail;
2593        }
2594
2595        buf += ret;
2596        buflen -= ret;
2597    }
2598
2599    /* Full disk encryption header pointer extension */
2600    if (s->crypto_header.offset != 0) {
2601        s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset);
2602        s->crypto_header.length = cpu_to_be64(s->crypto_header.length);
2603        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
2604                             &s->crypto_header, sizeof(s->crypto_header),
2605                             buflen);
2606        s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
2607        s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
2608        if (ret < 0) {
2609            goto fail;
2610        }
2611        buf += ret;
2612        buflen -= ret;
2613    }
2614
2615    /* Feature table */
2616    if (s->qcow_version >= 3) {
2617        Qcow2Feature features[] = {
2618            {
2619                .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2620                .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
2621                .name = "dirty bit",
2622            },
2623            {
2624                .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2625                .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
2626                .name = "corrupt bit",
2627            },
2628            {
2629                .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2630                .bit  = QCOW2_INCOMPAT_DATA_FILE_BITNR,
2631                .name = "external data file",
2632            },
2633            {
2634                .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2635                .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2636                .name = "lazy refcounts",
2637            },
2638        };
2639
2640        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2641                             features, sizeof(features), buflen);
2642        if (ret < 0) {
2643            goto fail;
2644        }
2645        buf += ret;
2646        buflen -= ret;
2647    }
2648
2649    /* Bitmap extension */
2650    if (s->nb_bitmaps > 0) {
2651        Qcow2BitmapHeaderExt bitmaps_header = {
2652            .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
2653            .bitmap_directory_size =
2654                    cpu_to_be64(s->bitmap_directory_size),
2655            .bitmap_directory_offset =
2656                    cpu_to_be64(s->bitmap_directory_offset)
2657        };
2658        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
2659                             &bitmaps_header, sizeof(bitmaps_header),
2660                             buflen);
2661        if (ret < 0) {
2662            goto fail;
2663        }
2664        buf += ret;
2665        buflen -= ret;
2666    }
2667
2668    /* Keep unknown header extensions */
2669    QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
2670        ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
2671        if (ret < 0) {
2672            goto fail;
2673        }
2674
2675        buf += ret;
2676        buflen -= ret;
2677    }
2678
2679    /* End of header extensions */
2680    ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
2681    if (ret < 0) {
2682        goto fail;
2683    }
2684
2685    buf += ret;
2686    buflen -= ret;
2687
2688    /* Backing file name */
2689    if (s->image_backing_file) {
2690        size_t backing_file_len = strlen(s->image_backing_file);
2691
2692        if (buflen < backing_file_len) {
2693            ret = -ENOSPC;
2694            goto fail;
2695        }
2696
2697        /* Using strncpy is ok here, since buf is not NUL-terminated. */
2698        strncpy(buf, s->image_backing_file, buflen);
2699
2700        header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
2701        header->backing_file_size   = cpu_to_be32(backing_file_len);
2702    }
2703
2704    /* Write the new header */
2705    ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
2706    if (ret < 0) {
2707        goto fail;
2708    }
2709
2710    ret = 0;
2711fail:
2712    qemu_vfree(header);
2713    return ret;
2714}
2715
2716static int qcow2_change_backing_file(BlockDriverState *bs,
2717    const char *backing_file, const char *backing_fmt)
2718{
2719    BDRVQcow2State *s = bs->opaque;
2720
2721    /* Adding a backing file means that the external data file alone won't be
2722     * enough to make sense of the content */
2723    if (backing_file && data_file_is_raw(bs)) {
2724        return -EINVAL;
2725    }
2726
2727    if (backing_file && strlen(backing_file) > 1023) {
2728        return -EINVAL;
2729    }
2730
2731    pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
2732            backing_file ?: "");
2733    pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2734    pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2735
2736    g_free(s->image_backing_file);
2737    g_free(s->image_backing_format);
2738
2739    s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2740    s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2741
2742    return qcow2_update_header(bs);
2743}
2744
2745static int qcow2_crypt_method_from_format(const char *encryptfmt)
2746{
2747    if (g_str_equal(encryptfmt, "luks")) {
2748        return QCOW_CRYPT_LUKS;
2749    } else if (g_str_equal(encryptfmt, "aes")) {
2750        return QCOW_CRYPT_AES;
2751    } else {
2752        return -EINVAL;
2753    }
2754}
2755
2756static int qcow2_set_up_encryption(BlockDriverState *bs,
2757                                   QCryptoBlockCreateOptions *cryptoopts,
2758                                   Error **errp)
2759{
2760    BDRVQcow2State *s = bs->opaque;
2761    QCryptoBlock *crypto = NULL;
2762    int fmt, ret;
2763
2764    switch (cryptoopts->format) {
2765    case Q_CRYPTO_BLOCK_FORMAT_LUKS:
2766        fmt = QCOW_CRYPT_LUKS;
2767        break;
2768    case Q_CRYPTO_BLOCK_FORMAT_QCOW:
2769        fmt = QCOW_CRYPT_AES;
2770        break;
2771    default:
2772        error_setg(errp, "Crypto format not supported in qcow2");
2773        return -EINVAL;
2774    }
2775
2776    s->crypt_method_header = fmt;
2777
2778    crypto = qcrypto_block_create(cryptoopts, "encrypt.",
2779                                  qcow2_crypto_hdr_init_func,
2780                                  qcow2_crypto_hdr_write_func,
2781                                  bs, errp);
2782    if (!crypto) {
2783        return -EINVAL;
2784    }
2785
2786    ret = qcow2_update_header(bs);
2787    if (ret < 0) {
2788        error_setg_errno(errp, -ret, "Could not write encryption header");
2789        goto out;
2790    }
2791
2792    ret = 0;
2793 out:
2794    qcrypto_block_free(crypto);
2795    return ret;
2796}
2797
2798/**
2799 * Preallocates metadata structures for data clusters between @offset (in the
2800 * guest disk) and @new_length (which is thus generally the new guest disk
2801 * size).
2802 *
2803 * Returns: 0 on success, -errno on failure.
2804 */
2805static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
2806                                       uint64_t new_length, PreallocMode mode,
2807                                       Error **errp)
2808{
2809    BDRVQcow2State *s = bs->opaque;
2810    uint64_t bytes;
2811    uint64_t host_offset = 0;
2812    int64_t file_length;
2813    unsigned int cur_bytes;
2814    int ret;
2815    QCowL2Meta *meta;
2816
2817    assert(offset <= new_length);
2818    bytes = new_length - offset;
2819
2820    while (bytes) {
2821        cur_bytes = MIN(bytes, QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size));
2822        ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2823                                         &host_offset, &meta);
2824        if (ret < 0) {
2825            error_setg_errno(errp, -ret, "Allocating clusters failed");
2826            return ret;
2827        }
2828
2829        while (meta) {
2830            QCowL2Meta *next = meta->next;
2831
2832            ret = qcow2_alloc_cluster_link_l2(bs, meta);
2833            if (ret < 0) {
2834                error_setg_errno(errp, -ret, "Mapping clusters failed");
2835                qcow2_free_any_clusters(bs, meta->alloc_offset,
2836                                        meta->nb_clusters, QCOW2_DISCARD_NEVER);
2837                return ret;
2838            }
2839
2840            /* There are no dependent requests, but we need to remove our
2841             * request from the list of in-flight requests */
2842            QLIST_REMOVE(meta, next_in_flight);
2843
2844            g_free(meta);
2845            meta = next;
2846        }
2847
2848        /* TODO Preallocate data if requested */
2849
2850        bytes -= cur_bytes;
2851        offset += cur_bytes;
2852    }
2853
2854    /*
2855     * It is expected that the image file is large enough to actually contain
2856     * all of the allocated clusters (otherwise we get failing reads after
2857     * EOF). Extend the image to the last allocated sector.
2858     */
2859    file_length = bdrv_getlength(s->data_file->bs);
2860    if (file_length < 0) {
2861        error_setg_errno(errp, -file_length, "Could not get file size");
2862        return file_length;
2863    }
2864
2865    if (host_offset + cur_bytes > file_length) {
2866        if (mode == PREALLOC_MODE_METADATA) {
2867            mode = PREALLOC_MODE_OFF;
2868        }
2869        ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, mode,
2870                               errp);
2871        if (ret < 0) {
2872            return ret;
2873        }
2874    }
2875
2876    return 0;
2877}
2878
2879/* qcow2_refcount_metadata_size:
2880 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
2881 * @cluster_size: size of a cluster, in bytes
2882 * @refcount_order: refcount bits power-of-2 exponent
2883 * @generous_increase: allow for the refcount table to be 1.5x as large as it
2884 *                     needs to be
2885 *
2886 * Returns: Number of bytes required for refcount blocks and table metadata.
2887 */
2888int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
2889                                     int refcount_order, bool generous_increase,
2890                                     uint64_t *refblock_count)
2891{
2892    /*
2893     * Every host cluster is reference-counted, including metadata (even
2894     * refcount metadata is recursively included).
2895     *
2896     * An accurate formula for the size of refcount metadata size is difficult
2897     * to derive.  An easier method of calculation is finding the fixed point
2898     * where no further refcount blocks or table clusters are required to
2899     * reference count every cluster.
2900     */
2901    int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
2902    int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
2903    int64_t table = 0;  /* number of refcount table clusters */
2904    int64_t blocks = 0; /* number of refcount block clusters */
2905    int64_t last;
2906    int64_t n = 0;
2907
2908    do {
2909        last = n;
2910        blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
2911        table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
2912        n = clusters + blocks + table;
2913
2914        if (n == last && generous_increase) {
2915            clusters += DIV_ROUND_UP(table, 2);
2916            n = 0; /* force another loop */
2917            generous_increase = false;
2918        }
2919    } while (n != last);
2920
2921    if (refblock_count) {
2922        *refblock_count = blocks;
2923    }
2924
2925    return (blocks + table) * cluster_size;
2926}
2927
2928/**
2929 * qcow2_calc_prealloc_size:
2930 * @total_size: virtual disk size in bytes
2931 * @cluster_size: cluster size in bytes
2932 * @refcount_order: refcount bits power-of-2 exponent
2933 *
2934 * Returns: Total number of bytes required for the fully allocated image
2935 * (including metadata).
2936 */
2937static int64_t qcow2_calc_prealloc_size(int64_t total_size,
2938                                        size_t cluster_size,
2939                                        int refcount_order)
2940{
2941    int64_t meta_size = 0;
2942    uint64_t nl1e, nl2e;
2943    int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
2944
2945    /* header: 1 cluster */
2946    meta_size += cluster_size;
2947
2948    /* total size of L2 tables */
2949    nl2e = aligned_total_size / cluster_size;
2950    nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t));
2951    meta_size += nl2e * sizeof(uint64_t);
2952
2953    /* total size of L1 tables */
2954    nl1e = nl2e * sizeof(uint64_t) / cluster_size;
2955    nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t));
2956    meta_size += nl1e * sizeof(uint64_t);
2957
2958    /* total size of refcount table and blocks */
2959    meta_size += qcow2_refcount_metadata_size(
2960            (meta_size + aligned_total_size) / cluster_size,
2961            cluster_size, refcount_order, false, NULL);
2962
2963    return meta_size + aligned_total_size;
2964}
2965
2966static bool validate_cluster_size(size_t cluster_size, Error **errp)
2967{
2968    int cluster_bits = ctz32(cluster_size);
2969    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
2970        (1 << cluster_bits) != cluster_size)
2971    {
2972        error_setg(errp, "Cluster size must be a power of two between %d and "
2973                   "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
2974        return false;
2975    }
2976    return true;
2977}
2978
2979static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
2980{
2981    size_t cluster_size;
2982
2983    cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2984                                         DEFAULT_CLUSTER_SIZE);
2985    if (!validate_cluster_size(cluster_size, errp)) {
2986        return 0;
2987    }
2988    return cluster_size;
2989}
2990
2991static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
2992{
2993    char *buf;
2994    int ret;
2995
2996    buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2997    if (!buf) {
2998        ret = 3; /* default */
2999    } else if (!strcmp(buf, "0.10")) {
3000        ret = 2;
3001    } else if (!strcmp(buf, "1.1")) {
3002        ret = 3;
3003    } else {
3004        error_setg(errp, "Invalid compatibility level: '%s'", buf);
3005        ret = -EINVAL;
3006    }
3007    g_free(buf);
3008    return ret;
3009}
3010
3011static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
3012                                                Error **errp)
3013{
3014    uint64_t refcount_bits;
3015
3016    refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
3017    if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
3018        error_setg(errp, "Refcount width must be a power of two and may not "
3019                   "exceed 64 bits");
3020        return 0;
3021    }
3022
3023    if (version < 3 && refcount_bits != 16) {
3024        error_setg(errp, "Different refcount widths than 16 bits require "
3025                   "compatibility level 1.1 or above (use compat=1.1 or "
3026                   "greater)");
3027        return 0;
3028    }
3029
3030    return refcount_bits;
3031}
3032
3033static int coroutine_fn
3034qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
3035{
3036    BlockdevCreateOptionsQcow2 *qcow2_opts;
3037    QDict *options;
3038
3039    /*
3040     * Open the image file and write a minimal qcow2 header.
3041     *
3042     * We keep things simple and start with a zero-sized image. We also
3043     * do without refcount blocks or a L1 table for now. We'll fix the
3044     * inconsistency later.
3045     *
3046     * We do need a refcount table because growing the refcount table means
3047     * allocating two new refcount blocks - the seconds of which would be at
3048     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
3049     * size for any qcow2 image.
3050     */
3051    BlockBackend *blk = NULL;
3052    BlockDriverState *bs = NULL;
3053    BlockDriverState *data_bs = NULL;
3054    QCowHeader *header;
3055    size_t cluster_size;
3056    int version;
3057    int refcount_order;
3058    uint64_t* refcount_table;
3059    Error *local_err = NULL;
3060    int ret;
3061
3062    assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
3063    qcow2_opts = &create_options->u.qcow2;
3064
3065    bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp);
3066    if (bs == NULL) {
3067        return -EIO;
3068    }
3069
3070    /* Validate options and set default values */
3071    if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
3072        error_setg(errp, "Image size must be a multiple of 512 bytes");
3073        ret = -EINVAL;
3074        goto out;
3075    }
3076
3077    if (qcow2_opts->has_version) {
3078        switch (qcow2_opts->version) {
3079        case BLOCKDEV_QCOW2_VERSION_V2:
3080            version = 2;
3081            break;
3082        case BLOCKDEV_QCOW2_VERSION_V3:
3083            version = 3;
3084            break;
3085        default:
3086            g_assert_not_reached();
3087        }
3088    } else {
3089        version = 3;
3090    }
3091
3092    if (qcow2_opts->has_cluster_size) {
3093        cluster_size = qcow2_opts->cluster_size;
3094    } else {
3095        cluster_size = DEFAULT_CLUSTER_SIZE;
3096    }
3097
3098    if (!validate_cluster_size(cluster_size, errp)) {
3099        ret = -EINVAL;
3100        goto out;
3101    }
3102
3103    if (!qcow2_opts->has_preallocation) {
3104        qcow2_opts->preallocation = PREALLOC_MODE_OFF;
3105    }
3106    if (qcow2_opts->has_backing_file &&
3107        qcow2_opts->preallocation != PREALLOC_MODE_OFF)
3108    {
3109        error_setg(errp, "Backing file and preallocation cannot be used at "
3110                   "the same time");
3111        ret = -EINVAL;
3112        goto out;
3113    }
3114    if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) {
3115        error_setg(errp, "Backing format cannot be used without backing file");
3116        ret = -EINVAL;
3117        goto out;
3118    }
3119
3120    if (!qcow2_opts->has_lazy_refcounts) {
3121        qcow2_opts->lazy_refcounts = false;
3122    }
3123    if (version < 3 && qcow2_opts->lazy_refcounts) {
3124        error_setg(errp, "Lazy refcounts only supported with compatibility "
3125                   "level 1.1 and above (use version=v3 or greater)");
3126        ret = -EINVAL;
3127        goto out;
3128    }
3129
3130    if (!qcow2_opts->has_refcount_bits) {
3131        qcow2_opts->refcount_bits = 16;
3132    }
3133    if (qcow2_opts->refcount_bits > 64 ||
3134        !is_power_of_2(qcow2_opts->refcount_bits))
3135    {
3136        error_setg(errp, "Refcount width must be a power of two and may not "
3137                   "exceed 64 bits");
3138        ret = -EINVAL;
3139        goto out;
3140    }
3141    if (version < 3 && qcow2_opts->refcount_bits != 16) {
3142        error_setg(errp, "Different refcount widths than 16 bits require "
3143                   "compatibility level 1.1 or above (use version=v3 or "
3144                   "greater)");
3145        ret = -EINVAL;
3146        goto out;
3147    }
3148    refcount_order = ctz32(qcow2_opts->refcount_bits);
3149
3150    if (qcow2_opts->data_file_raw && !qcow2_opts->data_file) {
3151        error_setg(errp, "data-file-raw requires data-file");
3152        ret = -EINVAL;
3153        goto out;
3154    }
3155    if (qcow2_opts->data_file_raw && qcow2_opts->has_backing_file) {
3156        error_setg(errp, "Backing file and data-file-raw cannot be used at "
3157                   "the same time");
3158        ret = -EINVAL;
3159        goto out;
3160    }
3161
3162    if (qcow2_opts->data_file) {
3163        if (version < 3) {
3164            error_setg(errp, "External data files are only supported with "
3165                       "compatibility level 1.1 and above (use version=v3 or "
3166                       "greater)");
3167            ret = -EINVAL;
3168            goto out;
3169        }
3170        data_bs = bdrv_open_blockdev_ref(qcow2_opts->data_file, errp);
3171        if (data_bs == NULL) {
3172            ret = -EIO;
3173            goto out;
3174        }
3175    }
3176
3177    /* Create BlockBackend to write to the image */
3178    blk = blk_new(bdrv_get_aio_context(bs),
3179                  BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
3180    ret = blk_insert_bs(blk, bs, errp);
3181    if (ret < 0) {
3182        goto out;
3183    }
3184    blk_set_allow_write_beyond_eof(blk, true);
3185
3186    /* Clear the protocol layer and preallocate it if necessary */
3187    ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp);
3188    if (ret < 0) {
3189        goto out;
3190    }
3191
3192    /* Write the header */
3193    QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
3194    header = g_malloc0(cluster_size);
3195    *header = (QCowHeader) {
3196        .magic                      = cpu_to_be32(QCOW_MAGIC),
3197        .version                    = cpu_to_be32(version),
3198        .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
3199        .size                       = cpu_to_be64(0),
3200        .l1_table_offset            = cpu_to_be64(0),
3201        .l1_size                    = cpu_to_be32(0),
3202        .refcount_table_offset      = cpu_to_be64(cluster_size),
3203        .refcount_table_clusters    = cpu_to_be32(1),
3204        .refcount_order             = cpu_to_be32(refcount_order),
3205        .header_length              = cpu_to_be32(sizeof(*header)),
3206    };
3207
3208    /* We'll update this to correct value later */
3209    header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
3210
3211    if (qcow2_opts->lazy_refcounts) {
3212        header->compatible_features |=
3213            cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
3214    }
3215    if (data_bs) {
3216        header->incompatible_features |=
3217            cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE);
3218    }
3219    if (qcow2_opts->data_file_raw) {
3220        header->autoclear_features |=
3221            cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW);
3222    }
3223
3224    ret = blk_pwrite(blk, 0, header, cluster_size, 0);
3225    g_free(header);
3226    if (ret < 0) {
3227        error_setg_errno(errp, -ret, "Could not write qcow2 header");
3228        goto out;
3229    }
3230
3231    /* Write a refcount table with one refcount block */
3232    refcount_table = g_malloc0(2 * cluster_size);
3233    refcount_table[0] = cpu_to_be64(2 * cluster_size);
3234    ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
3235    g_free(refcount_table);
3236
3237    if (ret < 0) {
3238        error_setg_errno(errp, -ret, "Could not write refcount table");
3239        goto out;
3240    }
3241
3242    blk_unref(blk);
3243    blk = NULL;
3244
3245    /*
3246     * And now open the image and make it consistent first (i.e. increase the
3247     * refcount of the cluster that is occupied by the header and the refcount
3248     * table)
3249     */
3250    options = qdict_new();
3251    qdict_put_str(options, "driver", "qcow2");
3252    qdict_put_str(options, "file", bs->node_name);
3253    if (data_bs) {
3254        qdict_put_str(options, "data-file", data_bs->node_name);
3255    }
3256    blk = blk_new_open(NULL, NULL, options,
3257                       BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
3258                       &local_err);
3259    if (blk == NULL) {
3260        error_propagate(errp, local_err);
3261        ret = -EIO;
3262        goto out;
3263    }
3264
3265    ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
3266    if (ret < 0) {
3267        error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
3268                         "header and refcount table");
3269        goto out;
3270
3271    } else if (ret != 0) {
3272        error_report("Huh, first cluster in empty image is already in use?");
3273        abort();
3274    }
3275
3276    /* Set the external data file if necessary */
3277    if (data_bs) {
3278        BDRVQcow2State *s = blk_bs(blk)->opaque;
3279        s->image_data_file = g_strdup(data_bs->filename);
3280    }
3281
3282    /* Create a full header (including things like feature table) */
3283    ret = qcow2_update_header(blk_bs(blk));
3284    if (ret < 0) {
3285        error_setg_errno(errp, -ret, "Could not update qcow2 header");
3286        goto out;
3287    }
3288
3289    /* Okay, now that we have a valid image, let's give it the right size */
3290    ret = blk_truncate(blk, qcow2_opts->size, qcow2_opts->preallocation, errp);
3291    if (ret < 0) {
3292        error_prepend(errp, "Could not resize image: ");
3293        goto out;
3294    }
3295
3296    /* Want a backing file? There you go.*/
3297    if (qcow2_opts->has_backing_file) {
3298        const char *backing_format = NULL;
3299
3300        if (qcow2_opts->has_backing_fmt) {
3301            backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
3302        }
3303
3304        ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
3305                                       backing_format);
3306        if (ret < 0) {
3307            error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
3308                             "with format '%s'", qcow2_opts->backing_file,
3309                             backing_format);
3310            goto out;
3311        }
3312    }
3313
3314    /* Want encryption? There you go. */
3315    if (qcow2_opts->has_encrypt) {
3316        ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
3317        if (ret < 0) {
3318            goto out;
3319        }
3320    }
3321
3322    blk_unref(blk);
3323    blk = NULL;
3324
3325    /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3326     * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3327     * have to setup decryption context. We're not doing any I/O on the top
3328     * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3329     * not have effect.
3330     */
3331    options = qdict_new();
3332    qdict_put_str(options, "driver", "qcow2");
3333    qdict_put_str(options, "file", bs->node_name);
3334    if (data_bs) {
3335        qdict_put_str(options, "data-file", data_bs->node_name);
3336    }
3337    blk = blk_new_open(NULL, NULL, options,
3338                       BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3339                       &local_err);
3340    if (blk == NULL) {
3341        error_propagate(errp, local_err);
3342        ret = -EIO;
3343        goto out;
3344    }
3345
3346    ret = 0;
3347out:
3348    blk_unref(blk);
3349    bdrv_unref(bs);
3350    bdrv_unref(data_bs);
3351    return ret;
3352}
3353
3354static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opts,
3355                                             Error **errp)
3356{
3357    BlockdevCreateOptions *create_options = NULL;
3358    QDict *qdict;
3359    Visitor *v;
3360    BlockDriverState *bs = NULL;
3361    BlockDriverState *data_bs = NULL;
3362    Error *local_err = NULL;
3363    const char *val;
3364    int ret;
3365
3366    /* Only the keyval visitor supports the dotted syntax needed for
3367     * encryption, so go through a QDict before getting a QAPI type. Ignore
3368     * options meant for the protocol layer so that the visitor doesn't
3369     * complain. */
3370    qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3371                                        true);
3372
3373    /* Handle encryption options */
3374    val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3375    if (val && !strcmp(val, "on")) {
3376        qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3377    } else if (val && !strcmp(val, "off")) {
3378        qdict_del(qdict, BLOCK_OPT_ENCRYPT);
3379    }
3380
3381    val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3382    if (val && !strcmp(val, "aes")) {
3383        qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
3384    }
3385
3386    /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3387     * version=v2/v3 below. */
3388    val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3389    if (val && !strcmp(val, "0.10")) {
3390        qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3391    } else if (val && !strcmp(val, "1.1")) {
3392        qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3393    }
3394
3395    /* Change legacy command line options into QMP ones */
3396    static const QDictRenames opt_renames[] = {
3397        { BLOCK_OPT_BACKING_FILE,       "backing-file" },
3398        { BLOCK_OPT_BACKING_FMT,        "backing-fmt" },
3399        { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
3400        { BLOCK_OPT_LAZY_REFCOUNTS,     "lazy-refcounts" },
3401        { BLOCK_OPT_REFCOUNT_BITS,      "refcount-bits" },
3402        { BLOCK_OPT_ENCRYPT,            BLOCK_OPT_ENCRYPT_FORMAT },
3403        { BLOCK_OPT_COMPAT_LEVEL,       "version" },
3404        { BLOCK_OPT_DATA_FILE_RAW,      "data-file-raw" },
3405        { NULL, NULL },
3406    };
3407
3408    if (!qdict_rename_keys(qdict, opt_renames, errp)) {
3409        ret = -EINVAL;
3410        goto finish;
3411    }
3412
3413    /* Create and open the file (protocol layer) */
3414    ret = bdrv_create_file(filename, opts, errp);
3415    if (ret < 0) {
3416        goto finish;
3417    }
3418
3419    bs = bdrv_open(filename, NULL, NULL,
3420                   BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3421    if (bs == NULL) {
3422        ret = -EIO;
3423        goto finish;
3424    }
3425
3426    /* Create and open an external data file (protocol layer) */
3427    val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE);
3428    if (val) {
3429        ret = bdrv_create_file(val, opts, errp);
3430        if (ret < 0) {
3431            goto finish;
3432        }
3433
3434        data_bs = bdrv_open(val, NULL, NULL,
3435                            BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
3436                            errp);
3437        if (data_bs == NULL) {
3438            ret = -EIO;
3439            goto finish;
3440        }
3441
3442        qdict_del(qdict, BLOCK_OPT_DATA_FILE);
3443        qdict_put_str(qdict, "data-file", data_bs->node_name);
3444    }
3445
3446    /* Set 'driver' and 'node' options */
3447    qdict_put_str(qdict, "driver", "qcow2");
3448    qdict_put_str(qdict, "file", bs->node_name);
3449
3450    /* Now get the QAPI type BlockdevCreateOptions */
3451    v = qobject_input_visitor_new_flat_confused(qdict, errp);
3452    if (!v) {
3453        ret = -EINVAL;
3454        goto finish;
3455    }
3456
3457    visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
3458    visit_free(v);
3459
3460    if (local_err) {
3461        error_propagate(errp, local_err);
3462        ret = -EINVAL;
3463        goto finish;
3464    }
3465
3466    /* Silently round up size */
3467    create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
3468                                            BDRV_SECTOR_SIZE);
3469
3470    /* Create the qcow2 image (format layer) */
3471    ret = qcow2_co_create(create_options, errp);
3472    if (ret < 0) {
3473        goto finish;
3474    }
3475
3476    ret = 0;
3477finish:
3478    qobject_unref(qdict);
3479    bdrv_unref(bs);
3480    bdrv_unref(data_bs);
3481    qapi_free_BlockdevCreateOptions(create_options);
3482    return ret;
3483}
3484
3485
3486static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
3487{
3488    int64_t nr;
3489    int res;
3490
3491    /* Clamp to image length, before checking status of underlying sectors */
3492    if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
3493        bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
3494    }
3495
3496    if (!bytes) {
3497        return true;
3498    }
3499    res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
3500    return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
3501}
3502
3503static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
3504    int64_t offset, int bytes, BdrvRequestFlags flags)
3505{
3506    int ret;
3507    BDRVQcow2State *s = bs->opaque;
3508
3509    uint32_t head = offset % s->cluster_size;
3510    uint32_t tail = (offset + bytes) % s->cluster_size;
3511
3512    trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3513    if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
3514        tail = 0;
3515    }
3516
3517    if (head || tail) {
3518        uint64_t off;
3519        unsigned int nr;
3520
3521        assert(head + bytes <= s->cluster_size);
3522
3523        /* check whether remainder of cluster already reads as zero */
3524        if (!(is_zero(bs, offset - head, head) &&
3525              is_zero(bs, offset + bytes,
3526                      tail ? s->cluster_size - tail : 0))) {
3527            return -ENOTSUP;
3528        }
3529
3530        qemu_co_mutex_lock(&s->lock);
3531        /* We can have new write after previous check */
3532        offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
3533        bytes = s->cluster_size;
3534        nr = s->cluster_size;
3535        ret = qcow2_get_cluster_offset(bs, offset, &nr, &off);
3536        if (ret != QCOW2_CLUSTER_UNALLOCATED &&
3537            ret != QCOW2_CLUSTER_ZERO_PLAIN &&
3538            ret != QCOW2_CLUSTER_ZERO_ALLOC) {
3539            qemu_co_mutex_unlock(&s->lock);
3540            return -ENOTSUP;
3541        }
3542    } else {
3543        qemu_co_mutex_lock(&s->lock);
3544    }
3545
3546    trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
3547
3548    /* Whatever is left can use real zero clusters */
3549    ret = qcow2_cluster_zeroize(bs, offset, bytes, flags);
3550    qemu_co_mutex_unlock(&s->lock);
3551
3552    return ret;
3553}
3554
3555static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
3556                                          int64_t offset, int bytes)
3557{
3558    int ret;
3559    BDRVQcow2State *s = bs->opaque;
3560
3561    if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3562        assert(bytes < s->cluster_size);
3563        /* Ignore partial clusters, except for the special case of the
3564         * complete partial cluster at the end of an unaligned file */
3565        if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
3566            offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
3567            return -ENOTSUP;
3568        }
3569    }
3570
3571    qemu_co_mutex_lock(&s->lock);
3572    ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
3573                                false);
3574    qemu_co_mutex_unlock(&s->lock);
3575    return ret;
3576}
3577
3578static int coroutine_fn
3579qcow2_co_copy_range_from(BlockDriverState *bs,
3580                         BdrvChild *src, uint64_t src_offset,
3581                         BdrvChild *dst, uint64_t dst_offset,
3582                         uint64_t bytes, BdrvRequestFlags read_flags,
3583                         BdrvRequestFlags write_flags)
3584{
3585    BDRVQcow2State *s = bs->opaque;
3586    int ret;
3587    unsigned int cur_bytes; /* number of bytes in current iteration */
3588    BdrvChild *child = NULL;
3589    BdrvRequestFlags cur_write_flags;
3590
3591    assert(!bs->encrypted);
3592    qemu_co_mutex_lock(&s->lock);
3593
3594    while (bytes != 0) {
3595        uint64_t copy_offset = 0;
3596        /* prepare next request */
3597        cur_bytes = MIN(bytes, INT_MAX);
3598        cur_write_flags = write_flags;
3599
3600        ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, &copy_offset);
3601        if (ret < 0) {
3602            goto out;
3603        }
3604
3605        switch (ret) {
3606        case QCOW2_CLUSTER_UNALLOCATED:
3607            if (bs->backing && bs->backing->bs) {
3608                int64_t backing_length = bdrv_getlength(bs->backing->bs);
3609                if (src_offset >= backing_length) {
3610                    cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3611                } else {
3612                    child = bs->backing;
3613                    cur_bytes = MIN(cur_bytes, backing_length - src_offset);
3614                    copy_offset = src_offset;
3615                }
3616            } else {
3617                cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3618            }
3619            break;
3620
3621        case QCOW2_CLUSTER_ZERO_PLAIN:
3622        case QCOW2_CLUSTER_ZERO_ALLOC:
3623            cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3624            break;
3625
3626        case QCOW2_CLUSTER_COMPRESSED:
3627            ret = -ENOTSUP;
3628            goto out;
3629
3630        case QCOW2_CLUSTER_NORMAL:
3631            child = s->data_file;
3632            copy_offset += offset_into_cluster(s, src_offset);
3633            if ((copy_offset & 511) != 0) {
3634                ret = -EIO;
3635                goto out;
3636            }
3637            break;
3638
3639        default:
3640            abort();
3641        }
3642        qemu_co_mutex_unlock(&s->lock);
3643        ret = bdrv_co_copy_range_from(child,
3644                                      copy_offset,
3645                                      dst, dst_offset,
3646                                      cur_bytes, read_flags, cur_write_flags);
3647        qemu_co_mutex_lock(&s->lock);
3648        if (ret < 0) {
3649            goto out;
3650        }
3651
3652        bytes -= cur_bytes;
3653        src_offset += cur_bytes;
3654        dst_offset += cur_bytes;
3655    }
3656    ret = 0;
3657
3658out:
3659    qemu_co_mutex_unlock(&s->lock);
3660    return ret;
3661}
3662
3663static int coroutine_fn
3664qcow2_co_copy_range_to(BlockDriverState *bs,
3665                       BdrvChild *src, uint64_t src_offset,
3666                       BdrvChild *dst, uint64_t dst_offset,
3667                       uint64_t bytes, BdrvRequestFlags read_flags,
3668                       BdrvRequestFlags write_flags)
3669{
3670    BDRVQcow2State *s = bs->opaque;
3671    int offset_in_cluster;
3672    int ret;
3673    unsigned int cur_bytes; /* number of sectors in current iteration */
3674    uint64_t cluster_offset;
3675    QCowL2Meta *l2meta = NULL;
3676
3677    assert(!bs->encrypted);
3678
3679    qemu_co_mutex_lock(&s->lock);
3680
3681    while (bytes != 0) {
3682
3683        l2meta = NULL;
3684
3685        offset_in_cluster = offset_into_cluster(s, dst_offset);
3686        cur_bytes = MIN(bytes, INT_MAX);
3687
3688        /* TODO:
3689         * If src->bs == dst->bs, we could simply copy by incrementing
3690         * the refcnt, without copying user data.
3691         * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3692        ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
3693                                         &cluster_offset, &l2meta);
3694        if (ret < 0) {
3695            goto fail;
3696        }
3697
3698        assert((cluster_offset & 511) == 0);
3699
3700        ret = qcow2_pre_write_overlap_check(bs, 0,
3701                cluster_offset + offset_in_cluster, cur_bytes, true);
3702        if (ret < 0) {
3703            goto fail;
3704        }
3705
3706        qemu_co_mutex_unlock(&s->lock);
3707        ret = bdrv_co_copy_range_to(src, src_offset,
3708                                    s->data_file,
3709                                    cluster_offset + offset_in_cluster,
3710                                    cur_bytes, read_flags, write_flags);
3711        qemu_co_mutex_lock(&s->lock);
3712        if (ret < 0) {
3713            goto fail;
3714        }
3715
3716        ret = qcow2_handle_l2meta(bs, &l2meta, true);
3717        if (ret) {
3718            goto fail;
3719        }
3720
3721        bytes -= cur_bytes;
3722        src_offset += cur_bytes;
3723        dst_offset += cur_bytes;
3724    }
3725    ret = 0;
3726
3727fail:
3728    qcow2_handle_l2meta(bs, &l2meta, false);
3729
3730    qemu_co_mutex_unlock(&s->lock);
3731
3732    trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
3733
3734    return ret;
3735}
3736
3737static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
3738                                          PreallocMode prealloc, Error **errp)
3739{
3740    BDRVQcow2State *s = bs->opaque;
3741    uint64_t old_length;
3742    int64_t new_l1_size;
3743    int ret;
3744    QDict *options;
3745
3746    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
3747        prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
3748    {
3749        error_setg(errp, "Unsupported preallocation mode '%s'",
3750                   PreallocMode_str(prealloc));
3751        return -ENOTSUP;
3752    }
3753
3754    if (offset & 511) {
3755        error_setg(errp, "The new size must be a multiple of 512");
3756        return -EINVAL;
3757    }
3758
3759    qemu_co_mutex_lock(&s->lock);
3760
3761    /* cannot proceed if image has snapshots */
3762    if (s->nb_snapshots) {
3763        error_setg(errp, "Can't resize an image which has snapshots");
3764        ret = -ENOTSUP;
3765        goto fail;
3766    }
3767
3768    /* cannot proceed if image has bitmaps */
3769    if (qcow2_truncate_bitmaps_check(bs, errp)) {
3770        ret = -ENOTSUP;
3771        goto fail;
3772    }
3773
3774    old_length = bs->total_sectors * BDRV_SECTOR_SIZE;
3775    new_l1_size = size_to_l1(s, offset);
3776
3777    if (offset < old_length) {
3778        int64_t last_cluster, old_file_size;
3779        if (prealloc != PREALLOC_MODE_OFF) {
3780            error_setg(errp,
3781                       "Preallocation can't be used for shrinking an image");
3782            ret = -EINVAL;
3783            goto fail;
3784        }
3785
3786        ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
3787                                    old_length - ROUND_UP(offset,
3788                                                          s->cluster_size),
3789                                    QCOW2_DISCARD_ALWAYS, true);
3790        if (ret < 0) {
3791            error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
3792            goto fail;
3793        }
3794
3795        ret = qcow2_shrink_l1_table(bs, new_l1_size);
3796        if (ret < 0) {
3797            error_setg_errno(errp, -ret,
3798                             "Failed to reduce the number of L2 tables");
3799            goto fail;
3800        }
3801
3802        ret = qcow2_shrink_reftable(bs);
3803        if (ret < 0) {
3804            error_setg_errno(errp, -ret,
3805                             "Failed to discard unused refblocks");
3806            goto fail;
3807        }
3808
3809        old_file_size = bdrv_getlength(bs->file->bs);
3810        if (old_file_size < 0) {
3811            error_setg_errno(errp, -old_file_size,
3812                             "Failed to inquire current file length");
3813            ret = old_file_size;
3814            goto fail;
3815        }
3816        last_cluster = qcow2_get_last_cluster(bs, old_file_size);
3817        if (last_cluster < 0) {
3818            error_setg_errno(errp, -last_cluster,
3819                             "Failed to find the last cluster");
3820            ret = last_cluster;
3821            goto fail;
3822        }
3823        if ((last_cluster + 1) * s->cluster_size < old_file_size) {
3824            Error *local_err = NULL;
3825
3826            bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
3827                             PREALLOC_MODE_OFF, &local_err);
3828            if (local_err) {
3829                warn_reportf_err(local_err,
3830                                 "Failed to truncate the tail of the image: ");
3831            }
3832        }
3833    } else {
3834        ret = qcow2_grow_l1_table(bs, new_l1_size, true);
3835        if (ret < 0) {
3836            error_setg_errno(errp, -ret, "Failed to grow the L1 table");
3837            goto fail;
3838        }
3839    }
3840
3841    switch (prealloc) {
3842    case PREALLOC_MODE_OFF:
3843        if (has_data_file(bs)) {
3844            ret = bdrv_co_truncate(s->data_file, offset, prealloc, errp);
3845            if (ret < 0) {
3846                goto fail;
3847            }
3848        }
3849        break;
3850
3851    case PREALLOC_MODE_METADATA:
3852        ret = preallocate_co(bs, old_length, offset, prealloc, errp);
3853        if (ret < 0) {
3854            goto fail;
3855        }
3856        break;
3857
3858    case PREALLOC_MODE_FALLOC:
3859    case PREALLOC_MODE_FULL:
3860    {
3861        int64_t allocation_start, host_offset, guest_offset;
3862        int64_t clusters_allocated;
3863        int64_t old_file_size, new_file_size;
3864        uint64_t nb_new_data_clusters, nb_new_l2_tables;
3865
3866        /* With a data file, preallocation means just allocating the metadata
3867         * and forwarding the truncate request to the data file */
3868        if (has_data_file(bs)) {
3869            ret = preallocate_co(bs, old_length, offset, prealloc, errp);
3870            if (ret < 0) {
3871                goto fail;
3872            }
3873            break;
3874        }
3875
3876        old_file_size = bdrv_getlength(bs->file->bs);
3877        if (old_file_size < 0) {
3878            error_setg_errno(errp, -old_file_size,
3879                             "Failed to inquire current file length");
3880            ret = old_file_size;
3881            goto fail;
3882        }
3883        old_file_size = ROUND_UP(old_file_size, s->cluster_size);
3884
3885        nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
3886                                            s->cluster_size);
3887
3888        /* This is an overestimation; we will not actually allocate space for
3889         * these in the file but just make sure the new refcount structures are
3890         * able to cover them so we will not have to allocate new refblocks
3891         * while entering the data blocks in the potentially new L2 tables.
3892         * (We do not actually care where the L2 tables are placed. Maybe they
3893         *  are already allocated or they can be placed somewhere before
3894         *  @old_file_size. It does not matter because they will be fully
3895         *  allocated automatically, so they do not need to be covered by the
3896         *  preallocation. All that matters is that we will not have to allocate
3897         *  new refcount structures for them.) */
3898        nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
3899                                        s->cluster_size / sizeof(uint64_t));
3900        /* The cluster range may not be aligned to L2 boundaries, so add one L2
3901         * table for a potential head/tail */
3902        nb_new_l2_tables++;
3903
3904        allocation_start = qcow2_refcount_area(bs, old_file_size,
3905                                               nb_new_data_clusters +
3906                                               nb_new_l2_tables,
3907                                               true, 0, 0);
3908        if (allocation_start < 0) {
3909            error_setg_errno(errp, -allocation_start,
3910                             "Failed to resize refcount structures");
3911            ret = allocation_start;
3912            goto fail;
3913        }
3914
3915        clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
3916                                                     nb_new_data_clusters);
3917        if (clusters_allocated < 0) {
3918            error_setg_errno(errp, -clusters_allocated,
3919                             "Failed to allocate data clusters");
3920            ret = clusters_allocated;
3921            goto fail;
3922        }
3923
3924        assert(clusters_allocated == nb_new_data_clusters);
3925
3926        /* Allocate the data area */
3927        new_file_size = allocation_start +
3928                        nb_new_data_clusters * s->cluster_size;
3929        ret = bdrv_co_truncate(bs->file, new_file_size, prealloc, errp);
3930        if (ret < 0) {
3931            error_prepend(errp, "Failed to resize underlying file: ");
3932            qcow2_free_clusters(bs, allocation_start,
3933                                nb_new_data_clusters * s->cluster_size,
3934                                QCOW2_DISCARD_OTHER);
3935            goto fail;
3936        }
3937
3938        /* Create the necessary L2 entries */
3939        host_offset = allocation_start;
3940        guest_offset = old_length;
3941        while (nb_new_data_clusters) {
3942            int64_t nb_clusters = MIN(
3943                nb_new_data_clusters,
3944                s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
3945            QCowL2Meta allocation = {
3946                .offset       = guest_offset,
3947                .alloc_offset = host_offset,
3948                .nb_clusters  = nb_clusters,
3949            };
3950            qemu_co_queue_init(&allocation.dependent_requests);
3951
3952            ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
3953            if (ret < 0) {
3954                error_setg_errno(errp, -ret, "Failed to update L2 tables");
3955                qcow2_free_clusters(bs, host_offset,
3956                                    nb_new_data_clusters * s->cluster_size,
3957                                    QCOW2_DISCARD_OTHER);
3958                goto fail;
3959            }
3960
3961            guest_offset += nb_clusters * s->cluster_size;
3962            host_offset += nb_clusters * s->cluster_size;
3963            nb_new_data_clusters -= nb_clusters;
3964        }
3965        break;
3966    }
3967
3968    default:
3969        g_assert_not_reached();
3970    }
3971
3972    if (prealloc != PREALLOC_MODE_OFF) {
3973        /* Flush metadata before actually changing the image size */
3974        ret = qcow2_write_caches(bs);
3975        if (ret < 0) {
3976            error_setg_errno(errp, -ret,
3977                             "Failed to flush the preallocated area to disk");
3978            goto fail;
3979        }
3980    }
3981
3982    bs->total_sectors = offset / BDRV_SECTOR_SIZE;
3983
3984    /* write updated header.size */
3985    offset = cpu_to_be64(offset);
3986    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
3987                           &offset, sizeof(uint64_t));
3988    if (ret < 0) {
3989        error_setg_errno(errp, -ret, "Failed to update the image size");
3990        goto fail;
3991    }
3992
3993    s->l1_vm_state_index = new_l1_size;
3994
3995    /* Update cache sizes */
3996    options = qdict_clone_shallow(bs->options);
3997    ret = qcow2_update_options(bs, options, s->flags, errp);
3998    qobject_unref(options);
3999    if (ret < 0) {
4000        goto fail;
4001    }
4002    ret = 0;
4003fail:
4004    qemu_co_mutex_unlock(&s->lock);
4005    return ret;
4006}
4007
4008/* XXX: put compressed sectors first, then all the cluster aligned
4009   tables to avoid losing bytes in alignment */
4010static coroutine_fn int
4011qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
4012                            uint64_t bytes, QEMUIOVector *qiov)
4013{
4014    BDRVQcow2State *s = bs->opaque;
4015    int ret;
4016    ssize_t out_len;
4017    uint8_t *buf, *out_buf;
4018    uint64_t cluster_offset;
4019
4020    if (has_data_file(bs)) {
4021        return -ENOTSUP;
4022    }
4023
4024    if (bytes == 0) {
4025        /* align end of file to a sector boundary to ease reading with
4026           sector based I/Os */
4027        int64_t len = bdrv_getlength(bs->file->bs);
4028        if (len < 0) {
4029            return len;
4030        }
4031        return bdrv_co_truncate(bs->file, len, PREALLOC_MODE_OFF, NULL);
4032    }
4033
4034    if (offset_into_cluster(s, offset)) {
4035        return -EINVAL;
4036    }
4037
4038    buf = qemu_blockalign(bs, s->cluster_size);
4039    if (bytes != s->cluster_size) {
4040        if (bytes > s->cluster_size ||
4041            offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
4042        {
4043            qemu_vfree(buf);
4044            return -EINVAL;
4045        }
4046        /* Zero-pad last write if image size is not cluster aligned */
4047        memset(buf + bytes, 0, s->cluster_size - bytes);
4048    }
4049    qemu_iovec_to_buf(qiov, 0, buf, bytes);
4050
4051    out_buf = g_malloc(s->cluster_size);
4052
4053    out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1,
4054                                buf, s->cluster_size);
4055    if (out_len == -ENOMEM) {
4056        /* could not compress: write normal cluster */
4057        ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
4058        if (ret < 0) {
4059            goto fail;
4060        }
4061        goto success;
4062    } else if (out_len < 0) {
4063        ret = -EINVAL;
4064        goto fail;
4065    }
4066
4067    qemu_co_mutex_lock(&s->lock);
4068    ret = qcow2_alloc_compressed_cluster_offset(bs, offset, out_len,
4069                                                &cluster_offset);
4070    if (ret < 0) {
4071        qemu_co_mutex_unlock(&s->lock);
4072        goto fail;
4073    }
4074
4075    ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len, true);
4076    qemu_co_mutex_unlock(&s->lock);
4077    if (ret < 0) {
4078        goto fail;
4079    }
4080
4081    BLKDBG_EVENT(s->data_file, BLKDBG_WRITE_COMPRESSED);
4082    ret = bdrv_co_pwrite(s->data_file, cluster_offset, out_len, out_buf, 0);
4083    if (ret < 0) {
4084        goto fail;
4085    }
4086success:
4087    ret = 0;
4088fail:
4089    qemu_vfree(buf);
4090    g_free(out_buf);
4091    return ret;
4092}
4093
4094static int coroutine_fn
4095qcow2_co_preadv_compressed(BlockDriverState *bs,
4096                           uint64_t file_cluster_offset,
4097                           uint64_t offset,
4098                           uint64_t bytes,
4099                           QEMUIOVector *qiov)
4100{
4101    BDRVQcow2State *s = bs->opaque;
4102    int ret = 0, csize, nb_csectors;
4103    uint64_t coffset;
4104    uint8_t *buf, *out_buf;
4105    int offset_in_cluster = offset_into_cluster(s, offset);
4106
4107    coffset = file_cluster_offset & s->cluster_offset_mask;
4108    nb_csectors = ((file_cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
4109    csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
4110        (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK);
4111
4112    buf = g_try_malloc(csize);
4113    if (!buf) {
4114        return -ENOMEM;
4115    }
4116
4117    out_buf = qemu_blockalign(bs, s->cluster_size);
4118
4119    BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
4120    ret = bdrv_co_pread(bs->file, coffset, csize, buf, 0);
4121    if (ret < 0) {
4122        goto fail;
4123    }
4124
4125    if (qcow2_co_decompress(bs, out_buf, s->cluster_size, buf, csize) < 0) {
4126        ret = -EIO;
4127        goto fail;
4128    }
4129
4130    qemu_iovec_from_buf(qiov, 0, out_buf + offset_in_cluster, bytes);
4131
4132fail:
4133    qemu_vfree(out_buf);
4134    g_free(buf);
4135
4136    return ret;
4137}
4138
4139static int make_completely_empty(BlockDriverState *bs)
4140{
4141    BDRVQcow2State *s = bs->opaque;
4142    Error *local_err = NULL;
4143    int ret, l1_clusters;
4144    int64_t offset;
4145    uint64_t *new_reftable = NULL;
4146    uint64_t rt_entry, l1_size2;
4147    struct {
4148        uint64_t l1_offset;
4149        uint64_t reftable_offset;
4150        uint32_t reftable_clusters;
4151    } QEMU_PACKED l1_ofs_rt_ofs_cls;
4152
4153    ret = qcow2_cache_empty(bs, s->l2_table_cache);
4154    if (ret < 0) {
4155        goto fail;
4156    }
4157
4158    ret = qcow2_cache_empty(bs, s->refcount_block_cache);
4159    if (ret < 0) {
4160        goto fail;
4161    }
4162
4163    /* Refcounts will be broken utterly */
4164    ret = qcow2_mark_dirty(bs);
4165    if (ret < 0) {
4166        goto fail;
4167    }
4168
4169    BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
4170
4171    l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
4172    l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
4173
4174    /* After this call, neither the in-memory nor the on-disk refcount
4175     * information accurately describe the actual references */
4176
4177    ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
4178                             l1_clusters * s->cluster_size, 0);
4179    if (ret < 0) {
4180        goto fail_broken_refcounts;
4181    }
4182    memset(s->l1_table, 0, l1_size2);
4183
4184    BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
4185
4186    /* Overwrite enough clusters at the beginning of the sectors to place
4187     * the refcount table, a refcount block and the L1 table in; this may
4188     * overwrite parts of the existing refcount and L1 table, which is not
4189     * an issue because the dirty flag is set, complete data loss is in fact
4190     * desired and partial data loss is consequently fine as well */
4191    ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
4192                             (2 + l1_clusters) * s->cluster_size, 0);
4193    /* This call (even if it failed overall) may have overwritten on-disk
4194     * refcount structures; in that case, the in-memory refcount information
4195     * will probably differ from the on-disk information which makes the BDS
4196     * unusable */
4197    if (ret < 0) {
4198        goto fail_broken_refcounts;
4199    }
4200
4201    BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
4202    BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
4203
4204    /* "Create" an empty reftable (one cluster) directly after the image
4205     * header and an empty L1 table three clusters after the image header;
4206     * the cluster between those two will be used as the first refblock */
4207    l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
4208    l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
4209    l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
4210    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
4211                           &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
4212    if (ret < 0) {
4213        goto fail_broken_refcounts;
4214    }
4215
4216    s->l1_table_offset = 3 * s->cluster_size;
4217
4218    new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
4219    if (!new_reftable) {
4220        ret = -ENOMEM;
4221        goto fail_broken_refcounts;
4222    }
4223
4224    s->refcount_table_offset = s->cluster_size;
4225    s->refcount_table_size   = s->cluster_size / sizeof(uint64_t);
4226    s->max_refcount_table_index = 0;
4227
4228    g_free(s->refcount_table);
4229    s->refcount_table = new_reftable;
4230    new_reftable = NULL;
4231
4232    /* Now the in-memory refcount information again corresponds to the on-disk
4233     * information (reftable is empty and no refblocks (the refblock cache is
4234     * empty)); however, this means some clusters (e.g. the image header) are
4235     * referenced, but not refcounted, but the normal qcow2 code assumes that
4236     * the in-memory information is always correct */
4237
4238    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
4239
4240    /* Enter the first refblock into the reftable */
4241    rt_entry = cpu_to_be64(2 * s->cluster_size);
4242    ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
4243                           &rt_entry, sizeof(rt_entry));
4244    if (ret < 0) {
4245        goto fail_broken_refcounts;
4246    }
4247    s->refcount_table[0] = 2 * s->cluster_size;
4248
4249    s->free_cluster_index = 0;
4250    assert(3 + l1_clusters <= s->refcount_block_size);
4251    offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
4252    if (offset < 0) {
4253        ret = offset;
4254        goto fail_broken_refcounts;
4255    } else if (offset > 0) {
4256        error_report("First cluster in emptied image is in use");
4257        abort();
4258    }
4259
4260    /* Now finally the in-memory information corresponds to the on-disk
4261     * structures and is correct */
4262    ret = qcow2_mark_clean(bs);
4263    if (ret < 0) {
4264        goto fail;
4265    }
4266
4267    ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
4268                        PREALLOC_MODE_OFF, &local_err);
4269    if (ret < 0) {
4270        error_report_err(local_err);
4271        goto fail;
4272    }
4273
4274    return 0;
4275
4276fail_broken_refcounts:
4277    /* The BDS is unusable at this point. If we wanted to make it usable, we
4278     * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
4279     * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
4280     * again. However, because the functions which could have caused this error
4281     * path to be taken are used by those functions as well, it's very likely
4282     * that that sequence will fail as well. Therefore, just eject the BDS. */
4283    bs->drv = NULL;
4284
4285fail:
4286    g_free(new_reftable);
4287    return ret;
4288}
4289
4290static int qcow2_make_empty(BlockDriverState *bs)
4291{
4292    BDRVQcow2State *s = bs->opaque;
4293    uint64_t offset, end_offset;
4294    int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
4295    int l1_clusters, ret = 0;
4296
4297    l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
4298
4299    if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
4300        3 + l1_clusters <= s->refcount_block_size &&
4301        s->crypt_method_header != QCOW_CRYPT_LUKS &&
4302        !has_data_file(bs)) {
4303        /* The following function only works for qcow2 v3 images (it
4304         * requires the dirty flag) and only as long as there are no
4305         * features that reserve extra clusters (such as snapshots,
4306         * LUKS header, or persistent bitmaps), because it completely
4307         * empties the image.  Furthermore, the L1 table and three
4308         * additional clusters (image header, refcount table, one
4309         * refcount block) have to fit inside one refcount block. It
4310         * only resets the image file, i.e. does not work with an
4311         * external data file. */
4312        return make_completely_empty(bs);
4313    }
4314
4315    /* This fallback code simply discards every active cluster; this is slow,
4316     * but works in all cases */
4317    end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
4318    for (offset = 0; offset < end_offset; offset += step) {
4319        /* As this function is generally used after committing an external
4320         * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4321         * default action for this kind of discard is to pass the discard,
4322         * which will ideally result in an actually smaller image file, as
4323         * is probably desired. */
4324        ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
4325                                    QCOW2_DISCARD_SNAPSHOT, true);
4326        if (ret < 0) {
4327            break;
4328        }
4329    }
4330
4331    return ret;
4332}
4333
4334static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
4335{
4336    BDRVQcow2State *s = bs->opaque;
4337    int ret;
4338
4339    qemu_co_mutex_lock(&s->lock);
4340    ret = qcow2_write_caches(bs);
4341    qemu_co_mutex_unlock(&s->lock);
4342
4343    return ret;
4344}
4345
4346static ssize_t qcow2_measure_crypto_hdr_init_func(QCryptoBlock *block,
4347        size_t headerlen, void *opaque, Error **errp)
4348{
4349    size_t *headerlenp = opaque;
4350
4351    /* Stash away the payload size */
4352    *headerlenp = headerlen;
4353    return 0;
4354}
4355
4356static ssize_t qcow2_measure_crypto_hdr_write_func(QCryptoBlock *block,
4357        size_t offset, const uint8_t *buf, size_t buflen,
4358        void *opaque, Error **errp)
4359{
4360    /* Discard the bytes, we're not actually writing to an image */
4361    return buflen;
4362}
4363
4364/* Determine the number of bytes for the LUKS payload */
4365static bool qcow2_measure_luks_headerlen(QemuOpts *opts, size_t *len,
4366                                         Error **errp)
4367{
4368    QDict *opts_qdict;
4369    QDict *cryptoopts_qdict;
4370    QCryptoBlockCreateOptions *cryptoopts;
4371    QCryptoBlock *crypto;
4372
4373    /* Extract "encrypt." options into a qdict */
4374    opts_qdict = qemu_opts_to_qdict(opts, NULL);
4375    qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt.");
4376    qobject_unref(opts_qdict);
4377
4378    /* Build QCryptoBlockCreateOptions object from qdict */
4379    qdict_put_str(cryptoopts_qdict, "format", "luks");
4380    cryptoopts = block_crypto_create_opts_init(cryptoopts_qdict, errp);
4381    qobject_unref(cryptoopts_qdict);
4382    if (!cryptoopts) {
4383        return false;
4384    }
4385
4386    /* Fake LUKS creation in order to determine the payload size */
4387    crypto = qcrypto_block_create(cryptoopts, "encrypt.",
4388                                  qcow2_measure_crypto_hdr_init_func,
4389                                  qcow2_measure_crypto_hdr_write_func,
4390                                  len, errp);
4391    qapi_free_QCryptoBlockCreateOptions(cryptoopts);
4392    if (!crypto) {
4393        return false;
4394    }
4395
4396    qcrypto_block_free(crypto);
4397    return true;
4398}
4399
4400static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
4401                                       Error **errp)
4402{
4403    Error *local_err = NULL;
4404    BlockMeasureInfo *info;
4405    uint64_t required = 0; /* bytes that contribute to required size */
4406    uint64_t virtual_size; /* disk size as seen by guest */
4407    uint64_t refcount_bits;
4408    uint64_t l2_tables;
4409    uint64_t luks_payload_size = 0;
4410    size_t cluster_size;
4411    int version;
4412    char *optstr;
4413    PreallocMode prealloc;
4414    bool has_backing_file;
4415    bool has_luks;
4416
4417    /* Parse image creation options */
4418    cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
4419    if (local_err) {
4420        goto err;
4421    }
4422
4423    version = qcow2_opt_get_version_del(opts, &local_err);
4424    if (local_err) {
4425        goto err;
4426    }
4427
4428    refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
4429    if (local_err) {
4430        goto err;
4431    }
4432
4433    optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
4434    prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
4435                               PREALLOC_MODE_OFF, &local_err);
4436    g_free(optstr);
4437    if (local_err) {
4438        goto err;
4439    }
4440
4441    optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
4442    has_backing_file = !!optstr;
4443    g_free(optstr);
4444
4445    optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
4446    has_luks = optstr && strcmp(optstr, "luks") == 0;
4447    g_free(optstr);
4448
4449    if (has_luks) {
4450        size_t headerlen;
4451
4452        if (!qcow2_measure_luks_headerlen(opts, &headerlen, &local_err)) {
4453            goto err;
4454        }
4455
4456        luks_payload_size = ROUND_UP(headerlen, cluster_size);
4457    }
4458
4459    virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
4460    virtual_size = ROUND_UP(virtual_size, cluster_size);
4461
4462    /* Check that virtual disk size is valid */
4463    l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
4464                             cluster_size / sizeof(uint64_t));
4465    if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
4466        error_setg(&local_err, "The image size is too large "
4467                               "(try using a larger cluster size)");
4468        goto err;
4469    }
4470
4471    /* Account for input image */
4472    if (in_bs) {
4473        int64_t ssize = bdrv_getlength(in_bs);
4474        if (ssize < 0) {
4475            error_setg_errno(&local_err, -ssize,
4476                             "Unable to get image virtual_size");
4477            goto err;
4478        }
4479
4480        virtual_size = ROUND_UP(ssize, cluster_size);
4481
4482        if (has_backing_file) {
4483            /* We don't how much of the backing chain is shared by the input
4484             * image and the new image file.  In the worst case the new image's
4485             * backing file has nothing in common with the input image.  Be
4486             * conservative and assume all clusters need to be written.
4487             */
4488            required = virtual_size;
4489        } else {
4490            int64_t offset;
4491            int64_t pnum = 0;
4492
4493            for (offset = 0; offset < ssize; offset += pnum) {
4494                int ret;
4495
4496                ret = bdrv_block_status_above(in_bs, NULL, offset,
4497                                              ssize - offset, &pnum, NULL,
4498                                              NULL);
4499                if (ret < 0) {
4500                    error_setg_errno(&local_err, -ret,
4501                                     "Unable to get block status");
4502                    goto err;
4503                }
4504
4505                if (ret & BDRV_BLOCK_ZERO) {
4506                    /* Skip zero regions (safe with no backing file) */
4507                } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
4508                           (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
4509                    /* Extend pnum to end of cluster for next iteration */
4510                    pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
4511
4512                    /* Count clusters we've seen */
4513                    required += offset % cluster_size + pnum;
4514                }
4515            }
4516        }
4517    }
4518
4519    /* Take into account preallocation.  Nothing special is needed for
4520     * PREALLOC_MODE_METADATA since metadata is always counted.
4521     */
4522    if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
4523        required = virtual_size;
4524    }
4525
4526    info = g_new(BlockMeasureInfo, 1);
4527    info->fully_allocated =
4528        qcow2_calc_prealloc_size(virtual_size, cluster_size,
4529                                 ctz32(refcount_bits)) + luks_payload_size;
4530
4531    /* Remove data clusters that are not required.  This overestimates the
4532     * required size because metadata needed for the fully allocated file is
4533     * still counted.
4534     */
4535    info->required = info->fully_allocated - virtual_size + required;
4536    return info;
4537
4538err:
4539    error_propagate(errp, local_err);
4540    return NULL;
4541}
4542
4543static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
4544{
4545    BDRVQcow2State *s = bs->opaque;
4546    bdi->unallocated_blocks_are_zero = true;
4547    bdi->cluster_size = s->cluster_size;
4548    bdi->vm_state_offset = qcow2_vm_state_offset(s);
4549    return 0;
4550}
4551
4552static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs,
4553                                                  Error **errp)
4554{
4555    BDRVQcow2State *s = bs->opaque;
4556    ImageInfoSpecific *spec_info;
4557    QCryptoBlockInfo *encrypt_info = NULL;
4558    Error *local_err = NULL;
4559
4560    if (s->crypto != NULL) {
4561        encrypt_info = qcrypto_block_get_info(s->crypto, &local_err);
4562        if (local_err) {
4563            error_propagate(errp, local_err);
4564            return NULL;
4565        }
4566    }
4567
4568    spec_info = g_new(ImageInfoSpecific, 1);
4569    *spec_info = (ImageInfoSpecific){
4570        .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
4571        .u.qcow2.data = g_new0(ImageInfoSpecificQCow2, 1),
4572    };
4573    if (s->qcow_version == 2) {
4574        *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4575            .compat             = g_strdup("0.10"),
4576            .refcount_bits      = s->refcount_bits,
4577        };
4578    } else if (s->qcow_version == 3) {
4579        Qcow2BitmapInfoList *bitmaps;
4580        bitmaps = qcow2_get_bitmap_info_list(bs, &local_err);
4581        if (local_err) {
4582            error_propagate(errp, local_err);
4583            qapi_free_ImageInfoSpecific(spec_info);
4584            return NULL;
4585        }
4586        *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4587            .compat             = g_strdup("1.1"),
4588            .lazy_refcounts     = s->compatible_features &
4589                                  QCOW2_COMPAT_LAZY_REFCOUNTS,
4590            .has_lazy_refcounts = true,
4591            .corrupt            = s->incompatible_features &
4592                                  QCOW2_INCOMPAT_CORRUPT,
4593            .has_corrupt        = true,
4594            .refcount_bits      = s->refcount_bits,
4595            .has_bitmaps        = !!bitmaps,
4596            .bitmaps            = bitmaps,
4597            .has_data_file      = !!s->image_data_file,
4598            .data_file          = g_strdup(s->image_data_file),
4599            .has_data_file_raw  = has_data_file(bs),
4600            .data_file_raw      = data_file_is_raw(bs),
4601        };
4602    } else {
4603        /* if this assertion fails, this probably means a new version was
4604         * added without having it covered here */
4605        assert(false);
4606    }
4607
4608    if (encrypt_info) {
4609        ImageInfoSpecificQCow2Encryption *qencrypt =
4610            g_new(ImageInfoSpecificQCow2Encryption, 1);
4611        switch (encrypt_info->format) {
4612        case Q_CRYPTO_BLOCK_FORMAT_QCOW:
4613            qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
4614            break;
4615        case Q_CRYPTO_BLOCK_FORMAT_LUKS:
4616            qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
4617            qencrypt->u.luks = encrypt_info->u.luks;
4618            break;
4619        default:
4620            abort();
4621        }
4622        /* Since we did shallow copy above, erase any pointers
4623         * in the original info */
4624        memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
4625        qapi_free_QCryptoBlockInfo(encrypt_info);
4626
4627        spec_info->u.qcow2.data->has_encrypt = true;
4628        spec_info->u.qcow2.data->encrypt = qencrypt;
4629    }
4630
4631    return spec_info;
4632}
4633
4634static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4635                              int64_t pos)
4636{
4637    BDRVQcow2State *s = bs->opaque;
4638
4639    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
4640    return bs->drv->bdrv_co_pwritev(bs, qcow2_vm_state_offset(s) + pos,
4641                                    qiov->size, qiov, 0);
4642}
4643
4644static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4645                              int64_t pos)
4646{
4647    BDRVQcow2State *s = bs->opaque;
4648
4649    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
4650    return bs->drv->bdrv_co_preadv(bs, qcow2_vm_state_offset(s) + pos,
4651                                   qiov->size, qiov, 0);
4652}
4653
4654/*
4655 * Downgrades an image's version. To achieve this, any incompatible features
4656 * have to be removed.
4657 */
4658static int qcow2_downgrade(BlockDriverState *bs, int target_version,
4659                           BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
4660                           Error **errp)
4661{
4662    BDRVQcow2State *s = bs->opaque;
4663    int current_version = s->qcow_version;
4664    int ret;
4665
4666    /* This is qcow2_downgrade(), not qcow2_upgrade() */
4667    assert(target_version < current_version);
4668
4669    /* There are no other versions (now) that you can downgrade to */
4670    assert(target_version == 2);
4671
4672    if (s->refcount_order != 4) {
4673        error_setg(errp, "compat=0.10 requires refcount_bits=16");
4674        return -ENOTSUP;
4675    }
4676
4677    if (has_data_file(bs)) {
4678        error_setg(errp, "Cannot downgrade an image with a data file");
4679        return -ENOTSUP;
4680    }
4681
4682    /* clear incompatible features */
4683    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
4684        ret = qcow2_mark_clean(bs);
4685        if (ret < 0) {
4686            error_setg_errno(errp, -ret, "Failed to make the image clean");
4687            return ret;
4688        }
4689    }
4690
4691    /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
4692     * the first place; if that happens nonetheless, returning -ENOTSUP is the
4693     * best thing to do anyway */
4694
4695    if (s->incompatible_features) {
4696        error_setg(errp, "Cannot downgrade an image with incompatible features "
4697                   "%#" PRIx64 " set", s->incompatible_features);
4698        return -ENOTSUP;
4699    }
4700
4701    /* since we can ignore compatible features, we can set them to 0 as well */
4702    s->compatible_features = 0;
4703    /* if lazy refcounts have been used, they have already been fixed through
4704     * clearing the dirty flag */
4705
4706    /* clearing autoclear features is trivial */
4707    s->autoclear_features = 0;
4708
4709    ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
4710    if (ret < 0) {
4711        error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
4712        return ret;
4713    }
4714
4715    s->qcow_version = target_version;
4716    ret = qcow2_update_header(bs);
4717    if (ret < 0) {
4718        s->qcow_version = current_version;
4719        error_setg_errno(errp, -ret, "Failed to update the image header");
4720        return ret;
4721    }
4722    return 0;
4723}
4724
4725typedef enum Qcow2AmendOperation {
4726    /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
4727     * statically initialized to so that the helper CB can discern the first
4728     * invocation from an operation change */
4729    QCOW2_NO_OPERATION = 0,
4730
4731    QCOW2_CHANGING_REFCOUNT_ORDER,
4732    QCOW2_DOWNGRADING,
4733} Qcow2AmendOperation;
4734
4735typedef struct Qcow2AmendHelperCBInfo {
4736    /* The code coordinating the amend operations should only modify
4737     * these four fields; the rest will be managed by the CB */
4738    BlockDriverAmendStatusCB *original_status_cb;
4739    void *original_cb_opaque;
4740
4741    Qcow2AmendOperation current_operation;
4742
4743    /* Total number of operations to perform (only set once) */
4744    int total_operations;
4745
4746    /* The following fields are managed by the CB */
4747
4748    /* Number of operations completed */
4749    int operations_completed;
4750
4751    /* Cumulative offset of all completed operations */
4752    int64_t offset_completed;
4753
4754    Qcow2AmendOperation last_operation;
4755    int64_t last_work_size;
4756} Qcow2AmendHelperCBInfo;
4757
4758static void qcow2_amend_helper_cb(BlockDriverState *bs,
4759                                  int64_t operation_offset,
4760                                  int64_t operation_work_size, void *opaque)
4761{
4762    Qcow2AmendHelperCBInfo *info = opaque;
4763    int64_t current_work_size;
4764    int64_t projected_work_size;
4765
4766    if (info->current_operation != info->last_operation) {
4767        if (info->last_operation != QCOW2_NO_OPERATION) {
4768            info->offset_completed += info->last_work_size;
4769            info->operations_completed++;
4770        }
4771
4772        info->last_operation = info->current_operation;
4773    }
4774
4775    assert(info->total_operations > 0);
4776    assert(info->operations_completed < info->total_operations);
4777
4778    info->last_work_size = operation_work_size;
4779
4780    current_work_size = info->offset_completed + operation_work_size;
4781
4782    /* current_work_size is the total work size for (operations_completed + 1)
4783     * operations (which includes this one), so multiply it by the number of
4784     * operations not covered and divide it by the number of operations
4785     * covered to get a projection for the operations not covered */
4786    projected_work_size = current_work_size * (info->total_operations -
4787                                               info->operations_completed - 1)
4788                                            / (info->operations_completed + 1);
4789
4790    info->original_status_cb(bs, info->offset_completed + operation_offset,
4791                             current_work_size + projected_work_size,
4792                             info->original_cb_opaque);
4793}
4794
4795static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
4796                               BlockDriverAmendStatusCB *status_cb,
4797                               void *cb_opaque,
4798                               Error **errp)
4799{
4800    BDRVQcow2State *s = bs->opaque;
4801    int old_version = s->qcow_version, new_version = old_version;
4802    uint64_t new_size = 0;
4803    const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL;
4804    bool lazy_refcounts = s->use_lazy_refcounts;
4805    bool data_file_raw = data_file_is_raw(bs);
4806    const char *compat = NULL;
4807    uint64_t cluster_size = s->cluster_size;
4808    bool encrypt;
4809    int encformat;
4810    int refcount_bits = s->refcount_bits;
4811    int ret;
4812    QemuOptDesc *desc = opts->list->desc;
4813    Qcow2AmendHelperCBInfo helper_cb_info;
4814
4815    while (desc && desc->name) {
4816        if (!qemu_opt_find(opts, desc->name)) {
4817            /* only change explicitly defined options */
4818            desc++;
4819            continue;
4820        }
4821
4822        if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
4823            compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
4824            if (!compat) {
4825                /* preserve default */
4826            } else if (!strcmp(compat, "0.10") || !strcmp(compat, "v2")) {
4827                new_version = 2;
4828            } else if (!strcmp(compat, "1.1") || !strcmp(compat, "v3")) {
4829                new_version = 3;
4830            } else {
4831                error_setg(errp, "Unknown compatibility level %s", compat);
4832                return -EINVAL;
4833            }
4834        } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
4835            error_setg(errp, "Cannot change preallocation mode");
4836            return -ENOTSUP;
4837        } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
4838            new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
4839        } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
4840            backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
4841        } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
4842            backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
4843        } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
4844            encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
4845                                        !!s->crypto);
4846
4847            if (encrypt != !!s->crypto) {
4848                error_setg(errp,
4849                           "Changing the encryption flag is not supported");
4850                return -ENOTSUP;
4851            }
4852        } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) {
4853            encformat = qcow2_crypt_method_from_format(
4854                qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT));
4855
4856            if (encformat != s->crypt_method_header) {
4857                error_setg(errp,
4858                           "Changing the encryption format is not supported");
4859                return -ENOTSUP;
4860            }
4861        } else if (g_str_has_prefix(desc->name, "encrypt.")) {
4862            error_setg(errp,
4863                       "Changing the encryption parameters is not supported");
4864            return -ENOTSUP;
4865        } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
4866            cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
4867                                             cluster_size);
4868            if (cluster_size != s->cluster_size) {
4869                error_setg(errp, "Changing the cluster size is not supported");
4870                return -ENOTSUP;
4871            }
4872        } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
4873            lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
4874                                               lazy_refcounts);
4875        } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
4876            refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
4877                                                refcount_bits);
4878
4879            if (refcount_bits <= 0 || refcount_bits > 64 ||
4880                !is_power_of_2(refcount_bits))
4881            {
4882                error_setg(errp, "Refcount width must be a power of two and "
4883                           "may not exceed 64 bits");
4884                return -EINVAL;
4885            }
4886        } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) {
4887            data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE);
4888            if (data_file && !has_data_file(bs)) {
4889                error_setg(errp, "data-file can only be set for images that "
4890                                 "use an external data file");
4891                return -EINVAL;
4892            }
4893        } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE_RAW)) {
4894            data_file_raw = qemu_opt_get_bool(opts, BLOCK_OPT_DATA_FILE_RAW,
4895                                              data_file_raw);
4896            if (data_file_raw && !data_file_is_raw(bs)) {
4897                error_setg(errp, "data-file-raw cannot be set on existing "
4898                                 "images");
4899                return -EINVAL;
4900            }
4901        } else {
4902            /* if this point is reached, this probably means a new option was
4903             * added without having it covered here */
4904            abort();
4905        }
4906
4907        desc++;
4908    }
4909
4910    helper_cb_info = (Qcow2AmendHelperCBInfo){
4911        .original_status_cb = status_cb,
4912        .original_cb_opaque = cb_opaque,
4913        .total_operations = (new_version < old_version)
4914                          + (s->refcount_bits != refcount_bits)
4915    };
4916
4917    /* Upgrade first (some features may require compat=1.1) */
4918    if (new_version > old_version) {
4919        s->qcow_version = new_version;
4920        ret = qcow2_update_header(bs);
4921        if (ret < 0) {
4922            s->qcow_version = old_version;
4923            error_setg_errno(errp, -ret, "Failed to update the image header");
4924            return ret;
4925        }
4926    }
4927
4928    if (s->refcount_bits != refcount_bits) {
4929        int refcount_order = ctz32(refcount_bits);
4930
4931        if (new_version < 3 && refcount_bits != 16) {
4932            error_setg(errp, "Refcount widths other than 16 bits require "
4933                       "compatibility level 1.1 or above (use compat=1.1 or "
4934                       "greater)");
4935            return -EINVAL;
4936        }
4937
4938        helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
4939        ret = qcow2_change_refcount_order(bs, refcount_order,
4940                                          &qcow2_amend_helper_cb,
4941                                          &helper_cb_info, errp);
4942        if (ret < 0) {
4943            return ret;
4944        }
4945    }
4946
4947    /* data-file-raw blocks backing files, so clear it first if requested */
4948    if (data_file_raw) {
4949        s->autoclear_features |= QCOW2_AUTOCLEAR_DATA_FILE_RAW;
4950    } else {
4951        s->autoclear_features &= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW;
4952    }
4953
4954    if (data_file) {
4955        g_free(s->image_data_file);
4956        s->image_data_file = *data_file ? g_strdup(data_file) : NULL;
4957    }
4958
4959    ret = qcow2_update_header(bs);
4960    if (ret < 0) {
4961        error_setg_errno(errp, -ret, "Failed to update the image header");
4962        return ret;
4963    }
4964
4965    if (backing_file || backing_format) {
4966        ret = qcow2_change_backing_file(bs,
4967                    backing_file ?: s->image_backing_file,
4968                    backing_format ?: s->image_backing_format);
4969        if (ret < 0) {
4970            error_setg_errno(errp, -ret, "Failed to change the backing file");
4971            return ret;
4972        }
4973    }
4974
4975    if (s->use_lazy_refcounts != lazy_refcounts) {
4976        if (lazy_refcounts) {
4977            if (new_version < 3) {
4978                error_setg(errp, "Lazy refcounts only supported with "
4979                           "compatibility level 1.1 and above (use compat=1.1 "
4980                           "or greater)");
4981                return -EINVAL;
4982            }
4983            s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4984            ret = qcow2_update_header(bs);
4985            if (ret < 0) {
4986                s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4987                error_setg_errno(errp, -ret, "Failed to update the image header");
4988                return ret;
4989            }
4990            s->use_lazy_refcounts = true;
4991        } else {
4992            /* make image clean first */
4993            ret = qcow2_mark_clean(bs);
4994            if (ret < 0) {
4995                error_setg_errno(errp, -ret, "Failed to make the image clean");
4996                return ret;
4997            }
4998            /* now disallow lazy refcounts */
4999            s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
5000            ret = qcow2_update_header(bs);
5001            if (ret < 0) {
5002                s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
5003                error_setg_errno(errp, -ret, "Failed to update the image header");
5004                return ret;
5005            }
5006            s->use_lazy_refcounts = false;
5007        }
5008    }
5009
5010    if (new_size) {
5011        BlockBackend *blk = blk_new(bdrv_get_aio_context(bs),
5012                                    BLK_PERM_RESIZE, BLK_PERM_ALL);
5013        ret = blk_insert_bs(blk, bs, errp);
5014        if (ret < 0) {
5015            blk_unref(blk);
5016            return ret;
5017        }
5018
5019        ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, errp);
5020        blk_unref(blk);
5021        if (ret < 0) {
5022            return ret;
5023        }
5024    }
5025
5026    /* Downgrade last (so unsupported features can be removed before) */
5027    if (new_version < old_version) {
5028        helper_cb_info.current_operation = QCOW2_DOWNGRADING;
5029        ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
5030                              &helper_cb_info, errp);
5031        if (ret < 0) {
5032            return ret;
5033        }
5034    }
5035
5036    return 0;
5037}
5038
5039/*
5040 * If offset or size are negative, respectively, they will not be included in
5041 * the BLOCK_IMAGE_CORRUPTED event emitted.
5042 * fatal will be ignored for read-only BDS; corruptions found there will always
5043 * be considered non-fatal.
5044 */
5045void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
5046                             int64_t size, const char *message_format, ...)
5047{
5048    BDRVQcow2State *s = bs->opaque;
5049    const char *node_name;
5050    char *message;
5051    va_list ap;
5052
5053    fatal = fatal && bdrv_is_writable(bs);
5054
5055    if (s->signaled_corruption &&
5056        (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
5057    {
5058        return;
5059    }
5060
5061    va_start(ap, message_format);
5062    message = g_strdup_vprintf(message_format, ap);
5063    va_end(ap);
5064
5065    if (fatal) {
5066        fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
5067                "corruption events will be suppressed\n", message);
5068    } else {
5069        fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
5070                "corruption events will be suppressed\n", message);
5071    }
5072
5073    node_name = bdrv_get_node_name(bs);
5074    qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
5075                                          *node_name != '\0', node_name,
5076                                          message, offset >= 0, offset,
5077                                          size >= 0, size,
5078                                          fatal);
5079    g_free(message);
5080
5081    if (fatal) {
5082        qcow2_mark_corrupt(bs);
5083        bs->drv = NULL; /* make BDS unusable */
5084    }
5085
5086    s->signaled_corruption = true;
5087}
5088
5089static QemuOptsList qcow2_create_opts = {
5090    .name = "qcow2-create-opts",
5091    .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
5092    .desc = {
5093        {
5094            .name = BLOCK_OPT_SIZE,
5095            .type = QEMU_OPT_SIZE,
5096            .help = "Virtual disk size"
5097        },
5098        {
5099            .name = BLOCK_OPT_COMPAT_LEVEL,
5100            .type = QEMU_OPT_STRING,
5101            .help = "Compatibility level (v2 [0.10] or v3 [1.1])"
5102        },
5103        {
5104            .name = BLOCK_OPT_BACKING_FILE,
5105            .type = QEMU_OPT_STRING,
5106            .help = "File name of a base image"
5107        },
5108        {
5109            .name = BLOCK_OPT_BACKING_FMT,
5110            .type = QEMU_OPT_STRING,
5111            .help = "Image format of the base image"
5112        },
5113        {
5114            .name = BLOCK_OPT_DATA_FILE,
5115            .type = QEMU_OPT_STRING,
5116            .help = "File name of an external data file"
5117        },
5118        {
5119            .name = BLOCK_OPT_DATA_FILE_RAW,
5120            .type = QEMU_OPT_BOOL,
5121            .help = "The external data file must stay valid as a raw image"
5122        },
5123        {
5124            .name = BLOCK_OPT_ENCRYPT,
5125            .type = QEMU_OPT_BOOL,
5126            .help = "Encrypt the image with format 'aes'. (Deprecated "
5127                    "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
5128        },
5129        {
5130            .name = BLOCK_OPT_ENCRYPT_FORMAT,
5131            .type = QEMU_OPT_STRING,
5132            .help = "Encrypt the image, format choices: 'aes', 'luks'",
5133        },
5134        BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
5135            "ID of secret providing qcow AES key or LUKS passphrase"),
5136        BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
5137        BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
5138        BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
5139        BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
5140        BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
5141        BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
5142        {
5143            .name = BLOCK_OPT_CLUSTER_SIZE,
5144            .type = QEMU_OPT_SIZE,
5145            .help = "qcow2 cluster size",
5146            .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
5147        },
5148        {
5149            .name = BLOCK_OPT_PREALLOC,
5150            .type = QEMU_OPT_STRING,
5151            .help = "Preallocation mode (allowed values: off, metadata, "
5152                    "falloc, full)"
5153        },
5154        {
5155            .name = BLOCK_OPT_LAZY_REFCOUNTS,
5156            .type = QEMU_OPT_BOOL,
5157            .help = "Postpone refcount updates",
5158            .def_value_str = "off"
5159        },
5160        {
5161            .name = BLOCK_OPT_REFCOUNT_BITS,
5162            .type = QEMU_OPT_NUMBER,
5163            .help = "Width of a reference count entry in bits",
5164            .def_value_str = "16"
5165        },
5166        { /* end of list */ }
5167    }
5168};
5169
5170static const char *const qcow2_strong_runtime_opts[] = {
5171    "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET,
5172
5173    NULL
5174};
5175
5176BlockDriver bdrv_qcow2 = {
5177    .format_name        = "qcow2",
5178    .instance_size      = sizeof(BDRVQcow2State),
5179    .bdrv_probe         = qcow2_probe,
5180    .bdrv_open          = qcow2_open,
5181    .bdrv_close         = qcow2_close,
5182    .bdrv_reopen_prepare  = qcow2_reopen_prepare,
5183    .bdrv_reopen_commit   = qcow2_reopen_commit,
5184    .bdrv_reopen_abort    = qcow2_reopen_abort,
5185    .bdrv_join_options    = qcow2_join_options,
5186    .bdrv_child_perm      = bdrv_format_default_perms,
5187    .bdrv_co_create_opts  = qcow2_co_create_opts,
5188    .bdrv_co_create       = qcow2_co_create,
5189    .bdrv_has_zero_init = bdrv_has_zero_init_1,
5190    .bdrv_co_block_status = qcow2_co_block_status,
5191
5192    .bdrv_co_preadv         = qcow2_co_preadv,
5193    .bdrv_co_pwritev        = qcow2_co_pwritev,
5194    .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
5195
5196    .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
5197    .bdrv_co_pdiscard       = qcow2_co_pdiscard,
5198    .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
5199    .bdrv_co_copy_range_to  = qcow2_co_copy_range_to,
5200    .bdrv_co_truncate       = qcow2_co_truncate,
5201    .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
5202    .bdrv_make_empty        = qcow2_make_empty,
5203
5204    .bdrv_snapshot_create   = qcow2_snapshot_create,
5205    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
5206    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
5207    .bdrv_snapshot_list     = qcow2_snapshot_list,
5208    .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
5209    .bdrv_measure           = qcow2_measure,
5210    .bdrv_get_info          = qcow2_get_info,
5211    .bdrv_get_specific_info = qcow2_get_specific_info,
5212
5213    .bdrv_save_vmstate    = qcow2_save_vmstate,
5214    .bdrv_load_vmstate    = qcow2_load_vmstate,
5215
5216    .supports_backing           = true,
5217    .bdrv_change_backing_file   = qcow2_change_backing_file,
5218
5219    .bdrv_refresh_limits        = qcow2_refresh_limits,
5220    .bdrv_co_invalidate_cache   = qcow2_co_invalidate_cache,
5221    .bdrv_inactivate            = qcow2_inactivate,
5222
5223    .create_opts         = &qcow2_create_opts,
5224    .strong_runtime_opts = qcow2_strong_runtime_opts,
5225    .mutable_opts        = mutable_opts,
5226    .bdrv_co_check       = qcow2_co_check,
5227    .bdrv_amend_options  = qcow2_amend_options,
5228
5229    .bdrv_detach_aio_context  = qcow2_detach_aio_context,
5230    .bdrv_attach_aio_context  = qcow2_attach_aio_context,
5231
5232    .bdrv_reopen_bitmaps_rw = qcow2_reopen_bitmaps_rw,
5233    .bdrv_can_store_new_dirty_bitmap = qcow2_can_store_new_dirty_bitmap,
5234    .bdrv_remove_persistent_dirty_bitmap = qcow2_remove_persistent_dirty_bitmap,
5235};
5236
5237static void bdrv_qcow2_init(void)
5238{
5239    bdrv_register(&bdrv_qcow2);
5240}
5241
5242block_init(bdrv_qcow2_init);
5243