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