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