xref: /qemu/block/qcow2.c (revision a6841a2de66fa44fe52ed996b70f9fb9f7bd6ca7)
1585f8587Sbellard /*
2585f8587Sbellard  * Block driver for the QCOW version 2 format
3585f8587Sbellard  *
4585f8587Sbellard  * Copyright (c) 2004-2006 Fabrice Bellard
5585f8587Sbellard  *
6585f8587Sbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
7585f8587Sbellard  * of this software and associated documentation files (the "Software"), to deal
8585f8587Sbellard  * in the Software without restriction, including without limitation the rights
9585f8587Sbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10585f8587Sbellard  * copies of the Software, and to permit persons to whom the Software is
11585f8587Sbellard  * furnished to do so, subject to the following conditions:
12585f8587Sbellard  *
13585f8587Sbellard  * The above copyright notice and this permission notice shall be included in
14585f8587Sbellard  * all copies or substantial portions of the Software.
15585f8587Sbellard  *
16585f8587Sbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17585f8587Sbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18585f8587Sbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19585f8587Sbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20585f8587Sbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21585f8587Sbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22585f8587Sbellard  * THE SOFTWARE.
23585f8587Sbellard  */
24e688df6bSMarkus Armbruster 
2580c71a24SPeter Maydell #include "qemu/osdep.h"
262714f13dSVladimir Sementsov-Ogievskiy 
27609f45eaSMax Reitz #include "block/qdict.h"
2823588797SKevin Wolf #include "sysemu/block-backend.h"
29db725815SMarkus Armbruster #include "qemu/main-loop.h"
301de7afc9SPaolo Bonzini #include "qemu/module.h"
310d8c41daSMichael S. Tsirkin #include "qcow2.h"
321de7afc9SPaolo Bonzini #include "qemu/error-report.h"
33e688df6bSMarkus Armbruster #include "qapi/error.h"
349af23989SMarkus Armbruster #include "qapi/qapi-events-block-core.h"
356b673957SMarkus Armbruster #include "qapi/qmp/qdict.h"
366b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
373cce16f4SKevin Wolf #include "trace.h"
381bd0e2d1SChunyan Liu #include "qemu/option_int.h"
39f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
4058369e22SPaolo Bonzini #include "qemu/bswap.h"
41b76b4f60SKevin Wolf #include "qapi/qobject-input-visitor.h"
42b76b4f60SKevin Wolf #include "qapi/qapi-visit-block-core.h"
430d8c41daSMichael S. Tsirkin #include "crypto.h"
44d710cf57SVladimir Sementsov-Ogievskiy #include "block/aio_task.h"
45585f8587Sbellard 
46585f8587Sbellard /*
47585f8587Sbellard   Differences with QCOW:
48585f8587Sbellard 
49585f8587Sbellard   - Support for multiple incremental snapshots.
50585f8587Sbellard   - Memory management by reference counts.
51585f8587Sbellard   - Clusters which have a reference count of one have the bit
52585f8587Sbellard     QCOW_OFLAG_COPIED to optimize write performance.
53585f8587Sbellard   - Size of compressed clusters is stored in sectors to reduce bit usage
54585f8587Sbellard     in the cluster offsets.
55585f8587Sbellard   - Support for storing additional data (such as the VM state) in the
56585f8587Sbellard     snapshots.
57585f8587Sbellard   - If a backing store is used, the cluster size is not constrained
58585f8587Sbellard     (could be backported to QCOW).
59585f8587Sbellard   - L2 tables have always a size of one cluster.
60585f8587Sbellard */
61585f8587Sbellard 
629b80ddf3Saliguori 
639b80ddf3Saliguori typedef struct {
649b80ddf3Saliguori     uint32_t magic;
659b80ddf3Saliguori     uint32_t len;
66c4217f64SJeff Cody } QEMU_PACKED QCowExtension;
6721d82ac9SJeff Cody 
687c80ab3fSJes Sorensen #define  QCOW2_EXT_MAGIC_END 0
698098969cSAndrey Shinkevich #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xe2792aca
70cfcc4c62SKevin Wolf #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
714652b8f3SDaniel P. Berrange #define  QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
7288ddffaeSVladimir Sementsov-Ogievskiy #define  QCOW2_EXT_MAGIC_BITMAPS 0x23852875
7393c24936SKevin Wolf #define  QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
749b80ddf3Saliguori 
75c3c10f72SVladimir Sementsov-Ogievskiy static int coroutine_fn
76c3c10f72SVladimir Sementsov-Ogievskiy qcow2_co_preadv_compressed(BlockDriverState *bs,
779c4269d5SAlberto Garcia                            uint64_t cluster_descriptor,
78c3c10f72SVladimir Sementsov-Ogievskiy                            uint64_t offset,
79c3c10f72SVladimir Sementsov-Ogievskiy                            uint64_t bytes,
80df893d25SVladimir Sementsov-Ogievskiy                            QEMUIOVector *qiov,
81df893d25SVladimir Sementsov-Ogievskiy                            size_t qiov_offset);
82c3c10f72SVladimir Sementsov-Ogievskiy 
837c80ab3fSJes Sorensen static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
84585f8587Sbellard {
85585f8587Sbellard     const QCowHeader *cow_header = (const void *)buf;
86585f8587Sbellard 
87585f8587Sbellard     if (buf_size >= sizeof(QCowHeader) &&
88585f8587Sbellard         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
896744cbabSKevin Wolf         be32_to_cpu(cow_header->version) >= 2)
90585f8587Sbellard         return 100;
91585f8587Sbellard     else
92585f8587Sbellard         return 0;
93585f8587Sbellard }
94585f8587Sbellard 
959b80ddf3Saliguori 
964652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
974652b8f3SDaniel P. Berrange                                           uint8_t *buf, size_t buflen,
984652b8f3SDaniel P. Berrange                                           void *opaque, Error **errp)
994652b8f3SDaniel P. Berrange {
1004652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
1014652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
1024652b8f3SDaniel P. Berrange     ssize_t ret;
1034652b8f3SDaniel P. Berrange 
1044652b8f3SDaniel P. Berrange     if ((offset + buflen) > s->crypto_header.length) {
1054652b8f3SDaniel P. Berrange         error_setg(errp, "Request for data outside of extension header");
1064652b8f3SDaniel P. Berrange         return -1;
1074652b8f3SDaniel P. Berrange     }
1084652b8f3SDaniel P. Berrange 
1094652b8f3SDaniel P. Berrange     ret = bdrv_pread(bs->file,
1104652b8f3SDaniel P. Berrange                      s->crypto_header.offset + offset, buf, buflen);
1114652b8f3SDaniel P. Berrange     if (ret < 0) {
1124652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not read encryption header");
1134652b8f3SDaniel P. Berrange         return -1;
1144652b8f3SDaniel P. Berrange     }
1154652b8f3SDaniel P. Berrange     return ret;
1164652b8f3SDaniel P. Berrange }
1174652b8f3SDaniel P. Berrange 
1184652b8f3SDaniel P. Berrange 
1194652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
1204652b8f3SDaniel P. Berrange                                           void *opaque, Error **errp)
1214652b8f3SDaniel P. Berrange {
1224652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
1234652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
1244652b8f3SDaniel P. Berrange     int64_t ret;
1254652b8f3SDaniel P. Berrange     int64_t clusterlen;
1264652b8f3SDaniel P. Berrange 
1274652b8f3SDaniel P. Berrange     ret = qcow2_alloc_clusters(bs, headerlen);
1284652b8f3SDaniel P. Berrange     if (ret < 0) {
1294652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret,
1304652b8f3SDaniel P. Berrange                          "Cannot allocate cluster for LUKS header size %zu",
1314652b8f3SDaniel P. Berrange                          headerlen);
1324652b8f3SDaniel P. Berrange         return -1;
1334652b8f3SDaniel P. Berrange     }
1344652b8f3SDaniel P. Berrange 
1354652b8f3SDaniel P. Berrange     s->crypto_header.length = headerlen;
1364652b8f3SDaniel P. Berrange     s->crypto_header.offset = ret;
1374652b8f3SDaniel P. Berrange 
138087ab8e7SDaniel P. Berrangé     /*
139087ab8e7SDaniel P. Berrangé      * Zero fill all space in cluster so it has predictable
140087ab8e7SDaniel P. Berrangé      * content, as we may not initialize some regions of the
141087ab8e7SDaniel P. Berrangé      * header (eg only 1 out of 8 key slots will be initialized)
142087ab8e7SDaniel P. Berrangé      */
1434652b8f3SDaniel P. Berrange     clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
144966b000fSKevin Wolf     assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen, false) == 0);
1454652b8f3SDaniel P. Berrange     ret = bdrv_pwrite_zeroes(bs->file,
146087ab8e7SDaniel P. Berrangé                              ret,
147087ab8e7SDaniel P. Berrangé                              clusterlen, 0);
1484652b8f3SDaniel P. Berrange     if (ret < 0) {
1494652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not zero fill encryption header");
1504652b8f3SDaniel P. Berrange         return -1;
1514652b8f3SDaniel P. Berrange     }
1524652b8f3SDaniel P. Berrange 
1534652b8f3SDaniel P. Berrange     return ret;
1544652b8f3SDaniel P. Berrange }
1554652b8f3SDaniel P. Berrange 
1564652b8f3SDaniel P. Berrange 
1574652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
1584652b8f3SDaniel P. Berrange                                            const uint8_t *buf, size_t buflen,
1594652b8f3SDaniel P. Berrange                                            void *opaque, Error **errp)
1604652b8f3SDaniel P. Berrange {
1614652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
1624652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
1634652b8f3SDaniel P. Berrange     ssize_t ret;
1644652b8f3SDaniel P. Berrange 
1654652b8f3SDaniel P. Berrange     if ((offset + buflen) > s->crypto_header.length) {
1664652b8f3SDaniel P. Berrange         error_setg(errp, "Request for data outside of extension header");
1674652b8f3SDaniel P. Berrange         return -1;
1684652b8f3SDaniel P. Berrange     }
1694652b8f3SDaniel P. Berrange 
1704652b8f3SDaniel P. Berrange     ret = bdrv_pwrite(bs->file,
1714652b8f3SDaniel P. Berrange                       s->crypto_header.offset + offset, buf, buflen);
1724652b8f3SDaniel P. Berrange     if (ret < 0) {
1734652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not read encryption header");
1744652b8f3SDaniel P. Berrange         return -1;
1754652b8f3SDaniel P. Berrange     }
1764652b8f3SDaniel P. Berrange     return ret;
1774652b8f3SDaniel P. Berrange }
1784652b8f3SDaniel P. Berrange 
17990766d9dSMaxim Levitsky static QDict*
18090766d9dSMaxim Levitsky qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp)
18190766d9dSMaxim Levitsky {
18290766d9dSMaxim Levitsky     QDict *cryptoopts_qdict;
18390766d9dSMaxim Levitsky     QDict *opts_qdict;
18490766d9dSMaxim Levitsky 
18590766d9dSMaxim Levitsky     /* Extract "encrypt." options into a qdict */
18690766d9dSMaxim Levitsky     opts_qdict = qemu_opts_to_qdict(opts, NULL);
18790766d9dSMaxim Levitsky     qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt.");
18890766d9dSMaxim Levitsky     qobject_unref(opts_qdict);
18990766d9dSMaxim Levitsky     qdict_put_str(cryptoopts_qdict, "format", fmt);
19090766d9dSMaxim Levitsky     return cryptoopts_qdict;
19190766d9dSMaxim Levitsky }
1924652b8f3SDaniel P. Berrange 
1939b80ddf3Saliguori /*
1949b80ddf3Saliguori  * read qcow2 extension and fill bs
1959b80ddf3Saliguori  * start reading from start_offset
1969b80ddf3Saliguori  * finish reading upon magic of value 0 or when end_offset reached
1979b80ddf3Saliguori  * unknown magic is skipped (future extension this version knows nothing about)
1989b80ddf3Saliguori  * return 0 upon success, non-0 otherwise
1999b80ddf3Saliguori  */
2007c80ab3fSJes Sorensen static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
2013ef6c40aSMax Reitz                                  uint64_t end_offset, void **p_feature_table,
20288ddffaeSVladimir Sementsov-Ogievskiy                                  int flags, bool *need_update_header,
20388ddffaeSVladimir Sementsov-Ogievskiy                                  Error **errp)
2049b80ddf3Saliguori {
205ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2069b80ddf3Saliguori     QCowExtension ext;
2079b80ddf3Saliguori     uint64_t offset;
20875bab85cSKevin Wolf     int ret;
20988ddffaeSVladimir Sementsov-Ogievskiy     Qcow2BitmapHeaderExt bitmaps_ext;
21088ddffaeSVladimir Sementsov-Ogievskiy 
21188ddffaeSVladimir Sementsov-Ogievskiy     if (need_update_header != NULL) {
21288ddffaeSVladimir Sementsov-Ogievskiy         *need_update_header = false;
21388ddffaeSVladimir Sementsov-Ogievskiy     }
2149b80ddf3Saliguori 
2159b80ddf3Saliguori #ifdef DEBUG_EXT
2167c80ab3fSJes Sorensen     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
2179b80ddf3Saliguori #endif
2189b80ddf3Saliguori     offset = start_offset;
2199b80ddf3Saliguori     while (offset < end_offset) {
2209b80ddf3Saliguori 
2219b80ddf3Saliguori #ifdef DEBUG_EXT
2229b80ddf3Saliguori         /* Sanity check */
2239b80ddf3Saliguori         if (offset > s->cluster_size)
2247c80ab3fSJes Sorensen             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
2259b80ddf3Saliguori 
2269b2260cbSDong Xu Wang         printf("attempting to read extended header in offset %lu\n", offset);
2279b80ddf3Saliguori #endif
2289b80ddf3Saliguori 
229cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
2303ef6c40aSMax Reitz         if (ret < 0) {
2313ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
2323ef6c40aSMax Reitz                              "pread fail from offset %" PRIu64, offset);
2339b80ddf3Saliguori             return 1;
2349b80ddf3Saliguori         }
2353b698f52SPeter Maydell         ext.magic = be32_to_cpu(ext.magic);
2363b698f52SPeter Maydell         ext.len = be32_to_cpu(ext.len);
2379b80ddf3Saliguori         offset += sizeof(ext);
2389b80ddf3Saliguori #ifdef DEBUG_EXT
2399b80ddf3Saliguori         printf("ext.magic = 0x%x\n", ext.magic);
2409b80ddf3Saliguori #endif
2412ebafc85SKevin Wolf         if (offset > end_offset || ext.len > end_offset - offset) {
2423ef6c40aSMax Reitz             error_setg(errp, "Header extension too large");
24364ca6aeeSKevin Wolf             return -EINVAL;
24464ca6aeeSKevin Wolf         }
24564ca6aeeSKevin Wolf 
2469b80ddf3Saliguori         switch (ext.magic) {
2477c80ab3fSJes Sorensen         case QCOW2_EXT_MAGIC_END:
2489b80ddf3Saliguori             return 0;
249f965509cSaliguori 
2507c80ab3fSJes Sorensen         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
251f965509cSaliguori             if (ext.len >= sizeof(bs->backing_format)) {
252521b2b5dSMax Reitz                 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
253521b2b5dSMax Reitz                            " too large (>=%zu)", ext.len,
254521b2b5dSMax Reitz                            sizeof(bs->backing_format));
255f965509cSaliguori                 return 2;
256f965509cSaliguori             }
257cf2ab8fcSKevin Wolf             ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
2583ef6c40aSMax Reitz             if (ret < 0) {
2593ef6c40aSMax Reitz                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
2603ef6c40aSMax Reitz                                  "Could not read format name");
261f965509cSaliguori                 return 3;
2623ef6c40aSMax Reitz             }
263f965509cSaliguori             bs->backing_format[ext.len] = '\0';
264e4603fe1SKevin Wolf             s->image_backing_format = g_strdup(bs->backing_format);
265f965509cSaliguori #ifdef DEBUG_EXT
266f965509cSaliguori             printf("Qcow2: Got format extension %s\n", bs->backing_format);
267f965509cSaliguori #endif
268f965509cSaliguori             break;
269f965509cSaliguori 
270cfcc4c62SKevin Wolf         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
271cfcc4c62SKevin Wolf             if (p_feature_table != NULL) {
272cfcc4c62SKevin Wolf                 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
273cf2ab8fcSKevin Wolf                 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
274cfcc4c62SKevin Wolf                 if (ret < 0) {
2753ef6c40aSMax Reitz                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
2763ef6c40aSMax Reitz                                      "Could not read table");
277cfcc4c62SKevin Wolf                     return ret;
278cfcc4c62SKevin Wolf                 }
279cfcc4c62SKevin Wolf 
280cfcc4c62SKevin Wolf                 *p_feature_table = feature_table;
281cfcc4c62SKevin Wolf             }
282cfcc4c62SKevin Wolf             break;
283cfcc4c62SKevin Wolf 
2844652b8f3SDaniel P. Berrange         case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
2854652b8f3SDaniel P. Berrange             unsigned int cflags = 0;
2864652b8f3SDaniel P. Berrange             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
2874652b8f3SDaniel P. Berrange                 error_setg(errp, "CRYPTO header extension only "
2884652b8f3SDaniel P. Berrange                            "expected with LUKS encryption method");
2894652b8f3SDaniel P. Berrange                 return -EINVAL;
2904652b8f3SDaniel P. Berrange             }
2914652b8f3SDaniel P. Berrange             if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
2924652b8f3SDaniel P. Berrange                 error_setg(errp, "CRYPTO header extension size %u, "
2934652b8f3SDaniel P. Berrange                            "but expected size %zu", ext.len,
2944652b8f3SDaniel P. Berrange                            sizeof(Qcow2CryptoHeaderExtension));
2954652b8f3SDaniel P. Berrange                 return -EINVAL;
2964652b8f3SDaniel P. Berrange             }
2974652b8f3SDaniel P. Berrange 
2984652b8f3SDaniel P. Berrange             ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
2994652b8f3SDaniel P. Berrange             if (ret < 0) {
3004652b8f3SDaniel P. Berrange                 error_setg_errno(errp, -ret,
3014652b8f3SDaniel P. Berrange                                  "Unable to read CRYPTO header extension");
3024652b8f3SDaniel P. Berrange                 return ret;
3034652b8f3SDaniel P. Berrange             }
3043b698f52SPeter Maydell             s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
3053b698f52SPeter Maydell             s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
3064652b8f3SDaniel P. Berrange 
3074652b8f3SDaniel P. Berrange             if ((s->crypto_header.offset % s->cluster_size) != 0) {
3084652b8f3SDaniel P. Berrange                 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
3094652b8f3SDaniel P. Berrange                            "not a multiple of cluster size '%u'",
3104652b8f3SDaniel P. Berrange                            s->crypto_header.offset, s->cluster_size);
3114652b8f3SDaniel P. Berrange                 return -EINVAL;
3124652b8f3SDaniel P. Berrange             }
3134652b8f3SDaniel P. Berrange 
3144652b8f3SDaniel P. Berrange             if (flags & BDRV_O_NO_IO) {
3154652b8f3SDaniel P. Berrange                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
3164652b8f3SDaniel P. Berrange             }
3171cd9a787SDaniel P. Berrange             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
3184652b8f3SDaniel P. Berrange                                            qcow2_crypto_hdr_read_func,
3198ac0f15fSVladimir Sementsov-Ogievskiy                                            bs, cflags, QCOW2_MAX_THREADS, errp);
3204652b8f3SDaniel P. Berrange             if (!s->crypto) {
3214652b8f3SDaniel P. Berrange                 return -EINVAL;
3224652b8f3SDaniel P. Berrange             }
3234652b8f3SDaniel P. Berrange         }   break;
3244652b8f3SDaniel P. Berrange 
32588ddffaeSVladimir Sementsov-Ogievskiy         case QCOW2_EXT_MAGIC_BITMAPS:
32688ddffaeSVladimir Sementsov-Ogievskiy             if (ext.len != sizeof(bitmaps_ext)) {
32788ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
32888ddffaeSVladimir Sementsov-Ogievskiy                                  "Invalid extension length");
32988ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
33088ddffaeSVladimir Sementsov-Ogievskiy             }
33188ddffaeSVladimir Sementsov-Ogievskiy 
33288ddffaeSVladimir Sementsov-Ogievskiy             if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
333c9ceb3ecSMax Reitz                 if (s->qcow_version < 3) {
334c9ceb3ecSMax Reitz                     /* Let's be a bit more specific */
335c9ceb3ecSMax Reitz                     warn_report("This qcow2 v2 image contains bitmaps, but "
336c9ceb3ecSMax Reitz                                 "they may have been modified by a program "
337c9ceb3ecSMax Reitz                                 "without persistent bitmap support; so now "
338c9ceb3ecSMax Reitz                                 "they must all be considered inconsistent");
339c9ceb3ecSMax Reitz                 } else {
34055d527a9SAlistair Francis                     warn_report("a program lacking bitmap support "
34188ddffaeSVladimir Sementsov-Ogievskiy                                 "modified this file, so all bitmaps are now "
34255d527a9SAlistair Francis                                 "considered inconsistent");
343c9ceb3ecSMax Reitz                 }
34455d527a9SAlistair Francis                 error_printf("Some clusters may be leaked, "
34555d527a9SAlistair Francis                              "run 'qemu-img check -r' on the image "
34688ddffaeSVladimir Sementsov-Ogievskiy                              "file to fix.");
34788ddffaeSVladimir Sementsov-Ogievskiy                 if (need_update_header != NULL) {
34888ddffaeSVladimir Sementsov-Ogievskiy                     /* Updating is needed to drop invalid bitmap extension. */
34988ddffaeSVladimir Sementsov-Ogievskiy                     *need_update_header = true;
35088ddffaeSVladimir Sementsov-Ogievskiy                 }
35188ddffaeSVladimir Sementsov-Ogievskiy                 break;
35288ddffaeSVladimir Sementsov-Ogievskiy             }
35388ddffaeSVladimir Sementsov-Ogievskiy 
35488ddffaeSVladimir Sementsov-Ogievskiy             ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
35588ddffaeSVladimir Sementsov-Ogievskiy             if (ret < 0) {
35688ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
35788ddffaeSVladimir Sementsov-Ogievskiy                                  "Could not read ext header");
35888ddffaeSVladimir Sementsov-Ogievskiy                 return ret;
35988ddffaeSVladimir Sementsov-Ogievskiy             }
36088ddffaeSVladimir Sementsov-Ogievskiy 
36188ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.reserved32 != 0) {
36288ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
36388ddffaeSVladimir Sementsov-Ogievskiy                                  "Reserved field is not zero");
36488ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
36588ddffaeSVladimir Sementsov-Ogievskiy             }
36688ddffaeSVladimir Sementsov-Ogievskiy 
3673b698f52SPeter Maydell             bitmaps_ext.nb_bitmaps = be32_to_cpu(bitmaps_ext.nb_bitmaps);
3683b698f52SPeter Maydell             bitmaps_ext.bitmap_directory_size =
3693b698f52SPeter Maydell                 be64_to_cpu(bitmaps_ext.bitmap_directory_size);
3703b698f52SPeter Maydell             bitmaps_ext.bitmap_directory_offset =
3713b698f52SPeter Maydell                 be64_to_cpu(bitmaps_ext.bitmap_directory_offset);
37288ddffaeSVladimir Sementsov-Ogievskiy 
37388ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
37488ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp,
37588ddffaeSVladimir Sementsov-Ogievskiy                            "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
37688ddffaeSVladimir Sementsov-Ogievskiy                            "exceeding the QEMU supported maximum of %d",
37788ddffaeSVladimir Sementsov-Ogievskiy                            bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
37888ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
37988ddffaeSVladimir Sementsov-Ogievskiy             }
38088ddffaeSVladimir Sementsov-Ogievskiy 
38188ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.nb_bitmaps == 0) {
38288ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "found bitmaps extension with zero bitmaps");
38388ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
38488ddffaeSVladimir Sementsov-Ogievskiy             }
38588ddffaeSVladimir Sementsov-Ogievskiy 
38674e60fb5SAlberto Garcia             if (offset_into_cluster(s, bitmaps_ext.bitmap_directory_offset)) {
38788ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "bitmaps_ext: "
38888ddffaeSVladimir Sementsov-Ogievskiy                                  "invalid bitmap directory offset");
38988ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
39088ddffaeSVladimir Sementsov-Ogievskiy             }
39188ddffaeSVladimir Sementsov-Ogievskiy 
39288ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.bitmap_directory_size >
39388ddffaeSVladimir Sementsov-Ogievskiy                 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
39488ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "bitmaps_ext: "
39588ddffaeSVladimir Sementsov-Ogievskiy                                  "bitmap directory size (%" PRIu64 ") exceeds "
39688ddffaeSVladimir Sementsov-Ogievskiy                                  "the maximum supported size (%d)",
39788ddffaeSVladimir Sementsov-Ogievskiy                                  bitmaps_ext.bitmap_directory_size,
39888ddffaeSVladimir Sementsov-Ogievskiy                                  QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
39988ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
40088ddffaeSVladimir Sementsov-Ogievskiy             }
40188ddffaeSVladimir Sementsov-Ogievskiy 
40288ddffaeSVladimir Sementsov-Ogievskiy             s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
40388ddffaeSVladimir Sementsov-Ogievskiy             s->bitmap_directory_offset =
40488ddffaeSVladimir Sementsov-Ogievskiy                     bitmaps_ext.bitmap_directory_offset;
40588ddffaeSVladimir Sementsov-Ogievskiy             s->bitmap_directory_size =
40688ddffaeSVladimir Sementsov-Ogievskiy                     bitmaps_ext.bitmap_directory_size;
40788ddffaeSVladimir Sementsov-Ogievskiy 
40888ddffaeSVladimir Sementsov-Ogievskiy #ifdef DEBUG_EXT
40988ddffaeSVladimir Sementsov-Ogievskiy             printf("Qcow2: Got bitmaps extension: "
41088ddffaeSVladimir Sementsov-Ogievskiy                    "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
41188ddffaeSVladimir Sementsov-Ogievskiy                    s->bitmap_directory_offset, s->nb_bitmaps);
41288ddffaeSVladimir Sementsov-Ogievskiy #endif
41388ddffaeSVladimir Sementsov-Ogievskiy             break;
41488ddffaeSVladimir Sementsov-Ogievskiy 
4159b890bdcSKevin Wolf         case QCOW2_EXT_MAGIC_DATA_FILE:
4169b890bdcSKevin Wolf         {
4179b890bdcSKevin Wolf             s->image_data_file = g_malloc0(ext.len + 1);
4189b890bdcSKevin Wolf             ret = bdrv_pread(bs->file, offset, s->image_data_file, ext.len);
4199b890bdcSKevin Wolf             if (ret < 0) {
4209b890bdcSKevin Wolf                 error_setg_errno(errp, -ret,
4219b890bdcSKevin Wolf                                  "ERROR: Could not read data file name");
4229b890bdcSKevin Wolf                 return ret;
4239b890bdcSKevin Wolf             }
4249b890bdcSKevin Wolf #ifdef DEBUG_EXT
4259b890bdcSKevin Wolf             printf("Qcow2: Got external data file %s\n", s->image_data_file);
4269b890bdcSKevin Wolf #endif
4279b890bdcSKevin Wolf             break;
4289b890bdcSKevin Wolf         }
4299b890bdcSKevin Wolf 
4309b80ddf3Saliguori         default:
43175bab85cSKevin Wolf             /* unknown magic - save it in case we need to rewrite the header */
4324096974eSEric Blake             /* If you add a new feature, make sure to also update the fast
4334096974eSEric Blake              * path of qcow2_make_empty() to deal with it. */
43475bab85cSKevin Wolf             {
43575bab85cSKevin Wolf                 Qcow2UnknownHeaderExtension *uext;
43675bab85cSKevin Wolf 
43775bab85cSKevin Wolf                 uext = g_malloc0(sizeof(*uext)  + ext.len);
43875bab85cSKevin Wolf                 uext->magic = ext.magic;
43975bab85cSKevin Wolf                 uext->len = ext.len;
44075bab85cSKevin Wolf                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
44175bab85cSKevin Wolf 
442cf2ab8fcSKevin Wolf                 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
44375bab85cSKevin Wolf                 if (ret < 0) {
4443ef6c40aSMax Reitz                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
4453ef6c40aSMax Reitz                                      "Could not read data");
44675bab85cSKevin Wolf                     return ret;
44775bab85cSKevin Wolf                 }
44875bab85cSKevin Wolf             }
4499b80ddf3Saliguori             break;
4509b80ddf3Saliguori         }
451fd29b4bbSKevin Wolf 
452fd29b4bbSKevin Wolf         offset += ((ext.len + 7) & ~7);
4539b80ddf3Saliguori     }
4549b80ddf3Saliguori 
4559b80ddf3Saliguori     return 0;
4569b80ddf3Saliguori }
4579b80ddf3Saliguori 
45875bab85cSKevin Wolf static void cleanup_unknown_header_ext(BlockDriverState *bs)
45975bab85cSKevin Wolf {
460ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
46175bab85cSKevin Wolf     Qcow2UnknownHeaderExtension *uext, *next;
46275bab85cSKevin Wolf 
46375bab85cSKevin Wolf     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
46475bab85cSKevin Wolf         QLIST_REMOVE(uext, next);
46575bab85cSKevin Wolf         g_free(uext);
46675bab85cSKevin Wolf     }
46775bab85cSKevin Wolf }
4689b80ddf3Saliguori 
469a55448b3SMax Reitz static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
470a55448b3SMax Reitz                                        uint64_t mask)
471cfcc4c62SKevin Wolf {
4727cdca2e2SAlberto Garcia     g_autoptr(GString) features = g_string_sized_new(60);
47312ac6d3dSKevin Wolf 
474cfcc4c62SKevin Wolf     while (table && table->name[0] != '\0') {
475cfcc4c62SKevin Wolf         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
47612ac6d3dSKevin Wolf             if (mask & (1ULL << table->bit)) {
4777cdca2e2SAlberto Garcia                 if (features->len > 0) {
4787cdca2e2SAlberto Garcia                     g_string_append(features, ", ");
4797cdca2e2SAlberto Garcia                 }
4807cdca2e2SAlberto Garcia                 g_string_append_printf(features, "%.46s", table->name);
48112ac6d3dSKevin Wolf                 mask &= ~(1ULL << table->bit);
482cfcc4c62SKevin Wolf             }
483cfcc4c62SKevin Wolf         }
484cfcc4c62SKevin Wolf         table++;
485cfcc4c62SKevin Wolf     }
486cfcc4c62SKevin Wolf 
487cfcc4c62SKevin Wolf     if (mask) {
4887cdca2e2SAlberto Garcia         if (features->len > 0) {
4897cdca2e2SAlberto Garcia             g_string_append(features, ", ");
4907cdca2e2SAlberto Garcia         }
4917cdca2e2SAlberto Garcia         g_string_append_printf(features,
4927cdca2e2SAlberto Garcia                                "Unknown incompatible feature: %" PRIx64, mask);
493cfcc4c62SKevin Wolf     }
49412ac6d3dSKevin Wolf 
4957cdca2e2SAlberto Garcia     error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str);
496cfcc4c62SKevin Wolf }
497cfcc4c62SKevin Wolf 
498c61d0004SStefan Hajnoczi /*
499bfe8043eSStefan Hajnoczi  * Sets the dirty bit and flushes afterwards if necessary.
500bfe8043eSStefan Hajnoczi  *
501bfe8043eSStefan Hajnoczi  * The incompatible_features bit is only set if the image file header was
502bfe8043eSStefan Hajnoczi  * updated successfully.  Therefore it is not required to check the return
503bfe8043eSStefan Hajnoczi  * value of this function.
504bfe8043eSStefan Hajnoczi  */
505280d3735SKevin Wolf int qcow2_mark_dirty(BlockDriverState *bs)
506bfe8043eSStefan Hajnoczi {
507ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
508bfe8043eSStefan Hajnoczi     uint64_t val;
509bfe8043eSStefan Hajnoczi     int ret;
510bfe8043eSStefan Hajnoczi 
511bfe8043eSStefan Hajnoczi     assert(s->qcow_version >= 3);
512bfe8043eSStefan Hajnoczi 
513bfe8043eSStefan Hajnoczi     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
514bfe8043eSStefan Hajnoczi         return 0; /* already dirty */
515bfe8043eSStefan Hajnoczi     }
516bfe8043eSStefan Hajnoczi 
517bfe8043eSStefan Hajnoczi     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
518d9ca2ea2SKevin Wolf     ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
519bfe8043eSStefan Hajnoczi                       &val, sizeof(val));
520bfe8043eSStefan Hajnoczi     if (ret < 0) {
521bfe8043eSStefan Hajnoczi         return ret;
522bfe8043eSStefan Hajnoczi     }
5239a4f4c31SKevin Wolf     ret = bdrv_flush(bs->file->bs);
524bfe8043eSStefan Hajnoczi     if (ret < 0) {
525bfe8043eSStefan Hajnoczi         return ret;
526bfe8043eSStefan Hajnoczi     }
527bfe8043eSStefan Hajnoczi 
528bfe8043eSStefan Hajnoczi     /* Only treat image as dirty if the header was updated successfully */
529bfe8043eSStefan Hajnoczi     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
530bfe8043eSStefan Hajnoczi     return 0;
531bfe8043eSStefan Hajnoczi }
532bfe8043eSStefan Hajnoczi 
533bfe8043eSStefan Hajnoczi /*
534c61d0004SStefan Hajnoczi  * Clears the dirty bit and flushes before if necessary.  Only call this
535c61d0004SStefan Hajnoczi  * function when there are no pending requests, it does not guard against
536c61d0004SStefan Hajnoczi  * concurrent requests dirtying the image.
537c61d0004SStefan Hajnoczi  */
538c61d0004SStefan Hajnoczi static int qcow2_mark_clean(BlockDriverState *bs)
539c61d0004SStefan Hajnoczi {
540ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
541c61d0004SStefan Hajnoczi 
542c61d0004SStefan Hajnoczi     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
5434c2e5f8fSKevin Wolf         int ret;
5444c2e5f8fSKevin Wolf 
5454c2e5f8fSKevin Wolf         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
5464c2e5f8fSKevin Wolf 
5478b220eb7SPaolo Bonzini         ret = qcow2_flush_caches(bs);
548c61d0004SStefan Hajnoczi         if (ret < 0) {
549c61d0004SStefan Hajnoczi             return ret;
550c61d0004SStefan Hajnoczi         }
551c61d0004SStefan Hajnoczi 
552c61d0004SStefan Hajnoczi         return qcow2_update_header(bs);
553c61d0004SStefan Hajnoczi     }
554c61d0004SStefan Hajnoczi     return 0;
555c61d0004SStefan Hajnoczi }
556c61d0004SStefan Hajnoczi 
55769c98726SMax Reitz /*
55869c98726SMax Reitz  * Marks the image as corrupt.
55969c98726SMax Reitz  */
56069c98726SMax Reitz int qcow2_mark_corrupt(BlockDriverState *bs)
56169c98726SMax Reitz {
562ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
56369c98726SMax Reitz 
56469c98726SMax Reitz     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
56569c98726SMax Reitz     return qcow2_update_header(bs);
56669c98726SMax Reitz }
56769c98726SMax Reitz 
56869c98726SMax Reitz /*
56969c98726SMax Reitz  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
57069c98726SMax Reitz  * before if necessary.
57169c98726SMax Reitz  */
57269c98726SMax Reitz int qcow2_mark_consistent(BlockDriverState *bs)
57369c98726SMax Reitz {
574ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
57569c98726SMax Reitz 
57669c98726SMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
5778b220eb7SPaolo Bonzini         int ret = qcow2_flush_caches(bs);
57869c98726SMax Reitz         if (ret < 0) {
57969c98726SMax Reitz             return ret;
58069c98726SMax Reitz         }
58169c98726SMax Reitz 
58269c98726SMax Reitz         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
58369c98726SMax Reitz         return qcow2_update_header(bs);
58469c98726SMax Reitz     }
58569c98726SMax Reitz     return 0;
58669c98726SMax Reitz }
58769c98726SMax Reitz 
5888bc584feSMax Reitz static void qcow2_add_check_result(BdrvCheckResult *out,
5898bc584feSMax Reitz                                    const BdrvCheckResult *src,
5908bc584feSMax Reitz                                    bool set_allocation_info)
5918bc584feSMax Reitz {
5928bc584feSMax Reitz     out->corruptions += src->corruptions;
5938bc584feSMax Reitz     out->leaks += src->leaks;
5948bc584feSMax Reitz     out->check_errors += src->check_errors;
5958bc584feSMax Reitz     out->corruptions_fixed += src->corruptions_fixed;
5968bc584feSMax Reitz     out->leaks_fixed += src->leaks_fixed;
5978bc584feSMax Reitz 
5988bc584feSMax Reitz     if (set_allocation_info) {
5998bc584feSMax Reitz         out->image_end_offset = src->image_end_offset;
6008bc584feSMax Reitz         out->bfi = src->bfi;
6018bc584feSMax Reitz     }
6028bc584feSMax Reitz }
6038bc584feSMax Reitz 
6042fd61638SPaolo Bonzini static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs,
6052fd61638SPaolo Bonzini                                               BdrvCheckResult *result,
606acbe5982SStefan Hajnoczi                                               BdrvCheckMode fix)
607acbe5982SStefan Hajnoczi {
6088bc584feSMax Reitz     BdrvCheckResult snapshot_res = {};
6098bc584feSMax Reitz     BdrvCheckResult refcount_res = {};
6108bc584feSMax Reitz     int ret;
6118bc584feSMax Reitz 
6128bc584feSMax Reitz     memset(result, 0, sizeof(*result));
6138bc584feSMax Reitz 
6148bc584feSMax Reitz     ret = qcow2_check_read_snapshot_table(bs, &snapshot_res, fix);
6158bc584feSMax Reitz     if (ret < 0) {
616fe446b5dSMax Reitz         qcow2_add_check_result(result, &snapshot_res, false);
6178bc584feSMax Reitz         return ret;
6188bc584feSMax Reitz     }
6198bc584feSMax Reitz 
6208bc584feSMax Reitz     ret = qcow2_check_refcounts(bs, &refcount_res, fix);
6218bc584feSMax Reitz     qcow2_add_check_result(result, &refcount_res, true);
622acbe5982SStefan Hajnoczi     if (ret < 0) {
623fe446b5dSMax Reitz         qcow2_add_check_result(result, &snapshot_res, false);
624fe446b5dSMax Reitz         return ret;
625fe446b5dSMax Reitz     }
626fe446b5dSMax Reitz 
627fe446b5dSMax Reitz     ret = qcow2_check_fix_snapshot_table(bs, &snapshot_res, fix);
628fe446b5dSMax Reitz     qcow2_add_check_result(result, &snapshot_res, false);
629fe446b5dSMax Reitz     if (ret < 0) {
630acbe5982SStefan Hajnoczi         return ret;
631acbe5982SStefan Hajnoczi     }
632acbe5982SStefan Hajnoczi 
633acbe5982SStefan Hajnoczi     if (fix && result->check_errors == 0 && result->corruptions == 0) {
63424530f3eSMax Reitz         ret = qcow2_mark_clean(bs);
63524530f3eSMax Reitz         if (ret < 0) {
63624530f3eSMax Reitz             return ret;
63724530f3eSMax Reitz         }
63824530f3eSMax Reitz         return qcow2_mark_consistent(bs);
639acbe5982SStefan Hajnoczi     }
640acbe5982SStefan Hajnoczi     return ret;
641acbe5982SStefan Hajnoczi }
642acbe5982SStefan Hajnoczi 
6432fd61638SPaolo Bonzini static int coroutine_fn qcow2_co_check(BlockDriverState *bs,
6442fd61638SPaolo Bonzini                                        BdrvCheckResult *result,
6452fd61638SPaolo Bonzini                                        BdrvCheckMode fix)
6462fd61638SPaolo Bonzini {
6472fd61638SPaolo Bonzini     BDRVQcow2State *s = bs->opaque;
6482fd61638SPaolo Bonzini     int ret;
6492fd61638SPaolo Bonzini 
6502fd61638SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
6512fd61638SPaolo Bonzini     ret = qcow2_co_check_locked(bs, result, fix);
6522fd61638SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
6532fd61638SPaolo Bonzini     return ret;
6542fd61638SPaolo Bonzini }
6552fd61638SPaolo Bonzini 
6560cf0e598SAlberto Garcia int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
6570cf0e598SAlberto Garcia                          uint64_t entries, size_t entry_len,
6580cf0e598SAlberto Garcia                          int64_t max_size_bytes, const char *table_name,
6590cf0e598SAlberto Garcia                          Error **errp)
6608c7de283SKevin Wolf {
661ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
6620cf0e598SAlberto Garcia 
6630cf0e598SAlberto Garcia     if (entries > max_size_bytes / entry_len) {
6640cf0e598SAlberto Garcia         error_setg(errp, "%s too large", table_name);
6650cf0e598SAlberto Garcia         return -EFBIG;
6660cf0e598SAlberto Garcia     }
6678c7de283SKevin Wolf 
6688c7de283SKevin Wolf     /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
6698c7de283SKevin Wolf      * because values will be passed to qemu functions taking int64_t. */
6700cf0e598SAlberto Garcia     if ((INT64_MAX - entries * entry_len < offset) ||
6710cf0e598SAlberto Garcia         (offset_into_cluster(s, offset) != 0)) {
6720cf0e598SAlberto Garcia         error_setg(errp, "%s offset invalid", table_name);
6738c7de283SKevin Wolf         return -EINVAL;
6748c7de283SKevin Wolf     }
6758c7de283SKevin Wolf 
6768c7de283SKevin Wolf     return 0;
6778c7de283SKevin Wolf }
6788c7de283SKevin Wolf 
6798a2ce0bcSAlberto Garcia static const char *const mutable_opts[] = {
6808a2ce0bcSAlberto Garcia     QCOW2_OPT_LAZY_REFCOUNTS,
6818a2ce0bcSAlberto Garcia     QCOW2_OPT_DISCARD_REQUEST,
6828a2ce0bcSAlberto Garcia     QCOW2_OPT_DISCARD_SNAPSHOT,
6838a2ce0bcSAlberto Garcia     QCOW2_OPT_DISCARD_OTHER,
6848a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP,
6858a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_TEMPLATE,
6868a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_MAIN_HEADER,
6878a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_ACTIVE_L1,
6888a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_ACTIVE_L2,
6898a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
6908a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
6918a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
6928a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_INACTIVE_L1,
6938a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_INACTIVE_L2,
6948a2ce0bcSAlberto Garcia     QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
6958a2ce0bcSAlberto Garcia     QCOW2_OPT_CACHE_SIZE,
6968a2ce0bcSAlberto Garcia     QCOW2_OPT_L2_CACHE_SIZE,
6978a2ce0bcSAlberto Garcia     QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
6988a2ce0bcSAlberto Garcia     QCOW2_OPT_REFCOUNT_CACHE_SIZE,
6998a2ce0bcSAlberto Garcia     QCOW2_OPT_CACHE_CLEAN_INTERVAL,
7008a2ce0bcSAlberto Garcia     NULL
7018a2ce0bcSAlberto Garcia };
7028a2ce0bcSAlberto Garcia 
70374c4510aSKevin Wolf static QemuOptsList qcow2_runtime_opts = {
70474c4510aSKevin Wolf     .name = "qcow2",
70574c4510aSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
70674c4510aSKevin Wolf     .desc = {
70774c4510aSKevin Wolf         {
70864aa99d3SKevin Wolf             .name = QCOW2_OPT_LAZY_REFCOUNTS,
70974c4510aSKevin Wolf             .type = QEMU_OPT_BOOL,
71074c4510aSKevin Wolf             .help = "Postpone refcount updates",
71174c4510aSKevin Wolf         },
71267af674eSKevin Wolf         {
71367af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_REQUEST,
71467af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
71567af674eSKevin Wolf             .help = "Pass guest discard requests to the layer below",
71667af674eSKevin Wolf         },
71767af674eSKevin Wolf         {
71867af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
71967af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
72067af674eSKevin Wolf             .help = "Generate discard requests when snapshot related space "
72167af674eSKevin Wolf                     "is freed",
72267af674eSKevin Wolf         },
72367af674eSKevin Wolf         {
72467af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_OTHER,
72567af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
72667af674eSKevin Wolf             .help = "Generate discard requests when other clusters are freed",
72767af674eSKevin Wolf         },
72805de7e86SMax Reitz         {
72905de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP,
73005de7e86SMax Reitz             .type = QEMU_OPT_STRING,
73105de7e86SMax Reitz             .help = "Selects which overlap checks to perform from a range of "
73205de7e86SMax Reitz                     "templates (none, constant, cached, all)",
73305de7e86SMax Reitz         },
73405de7e86SMax Reitz         {
735ee42b5ceSMax Reitz             .name = QCOW2_OPT_OVERLAP_TEMPLATE,
736ee42b5ceSMax Reitz             .type = QEMU_OPT_STRING,
737ee42b5ceSMax Reitz             .help = "Selects which overlap checks to perform from a range of "
738ee42b5ceSMax Reitz                     "templates (none, constant, cached, all)",
739ee42b5ceSMax Reitz         },
740ee42b5ceSMax Reitz         {
74105de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
74205de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
74305de7e86SMax Reitz             .help = "Check for unintended writes into the main qcow2 header",
74405de7e86SMax Reitz         },
74505de7e86SMax Reitz         {
74605de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
74705de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
74805de7e86SMax Reitz             .help = "Check for unintended writes into the active L1 table",
74905de7e86SMax Reitz         },
75005de7e86SMax Reitz         {
75105de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
75205de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
75305de7e86SMax Reitz             .help = "Check for unintended writes into an active L2 table",
75405de7e86SMax Reitz         },
75505de7e86SMax Reitz         {
75605de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
75705de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
75805de7e86SMax Reitz             .help = "Check for unintended writes into the refcount table",
75905de7e86SMax Reitz         },
76005de7e86SMax Reitz         {
76105de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
76205de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
76305de7e86SMax Reitz             .help = "Check for unintended writes into a refcount block",
76405de7e86SMax Reitz         },
76505de7e86SMax Reitz         {
76605de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
76705de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
76805de7e86SMax Reitz             .help = "Check for unintended writes into the snapshot table",
76905de7e86SMax Reitz         },
77005de7e86SMax Reitz         {
77105de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
77205de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
77305de7e86SMax Reitz             .help = "Check for unintended writes into an inactive L1 table",
77405de7e86SMax Reitz         },
77505de7e86SMax Reitz         {
77605de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
77705de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
77805de7e86SMax Reitz             .help = "Check for unintended writes into an inactive L2 table",
77905de7e86SMax Reitz         },
7806c1c8d5dSMax Reitz         {
7810e4e4318SVladimir Sementsov-Ogievskiy             .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
7820e4e4318SVladimir Sementsov-Ogievskiy             .type = QEMU_OPT_BOOL,
7830e4e4318SVladimir Sementsov-Ogievskiy             .help = "Check for unintended writes into the bitmap directory",
7840e4e4318SVladimir Sementsov-Ogievskiy         },
7850e4e4318SVladimir Sementsov-Ogievskiy         {
7866c1c8d5dSMax Reitz             .name = QCOW2_OPT_CACHE_SIZE,
7876c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
7886c1c8d5dSMax Reitz             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
7896c1c8d5dSMax Reitz                     "cache size",
7906c1c8d5dSMax Reitz         },
7916c1c8d5dSMax Reitz         {
7926c1c8d5dSMax Reitz             .name = QCOW2_OPT_L2_CACHE_SIZE,
7936c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
7946c1c8d5dSMax Reitz             .help = "Maximum L2 table cache size",
7956c1c8d5dSMax Reitz         },
7966c1c8d5dSMax Reitz         {
7971221fe6fSAlberto Garcia             .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
7981221fe6fSAlberto Garcia             .type = QEMU_OPT_SIZE,
7991221fe6fSAlberto Garcia             .help = "Size of each entry in the L2 cache",
8001221fe6fSAlberto Garcia         },
8011221fe6fSAlberto Garcia         {
8026c1c8d5dSMax Reitz             .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
8036c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
8046c1c8d5dSMax Reitz             .help = "Maximum refcount block cache size",
8056c1c8d5dSMax Reitz         },
806279621c0SAlberto Garcia         {
807279621c0SAlberto Garcia             .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
808279621c0SAlberto Garcia             .type = QEMU_OPT_NUMBER,
809279621c0SAlberto Garcia             .help = "Clean unused cache entries after this time (in seconds)",
810279621c0SAlberto Garcia         },
8114652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
8124652b8f3SDaniel P. Berrange             "ID of secret providing qcow2 AES key or LUKS passphrase"),
81374c4510aSKevin Wolf         { /* end of list */ }
81474c4510aSKevin Wolf     },
81574c4510aSKevin Wolf };
81674c4510aSKevin Wolf 
8174092e99dSMax Reitz static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
8184092e99dSMax Reitz     [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,
8194092e99dSMax Reitz     [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
8204092e99dSMax Reitz     [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
8214092e99dSMax Reitz     [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
8224092e99dSMax Reitz     [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
8234092e99dSMax Reitz     [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
8244092e99dSMax Reitz     [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
8254092e99dSMax Reitz     [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
8260e4e4318SVladimir Sementsov-Ogievskiy     [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
8274092e99dSMax Reitz };
8284092e99dSMax Reitz 
829279621c0SAlberto Garcia static void cache_clean_timer_cb(void *opaque)
830279621c0SAlberto Garcia {
831279621c0SAlberto Garcia     BlockDriverState *bs = opaque;
832ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
833b2f68bffSAlberto Garcia     qcow2_cache_clean_unused(s->l2_table_cache);
834b2f68bffSAlberto Garcia     qcow2_cache_clean_unused(s->refcount_block_cache);
835279621c0SAlberto Garcia     timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
836279621c0SAlberto Garcia               (int64_t) s->cache_clean_interval * 1000);
837279621c0SAlberto Garcia }
838279621c0SAlberto Garcia 
839279621c0SAlberto Garcia static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
840279621c0SAlberto Garcia {
841ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
842279621c0SAlberto Garcia     if (s->cache_clean_interval > 0) {
843279621c0SAlberto Garcia         s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
844279621c0SAlberto Garcia                                              SCALE_MS, cache_clean_timer_cb,
845279621c0SAlberto Garcia                                              bs);
846279621c0SAlberto Garcia         timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
847279621c0SAlberto Garcia                   (int64_t) s->cache_clean_interval * 1000);
848279621c0SAlberto Garcia     }
849279621c0SAlberto Garcia }
850279621c0SAlberto Garcia 
851279621c0SAlberto Garcia static void cache_clean_timer_del(BlockDriverState *bs)
852279621c0SAlberto Garcia {
853ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
854279621c0SAlberto Garcia     if (s->cache_clean_timer) {
855279621c0SAlberto Garcia         timer_del(s->cache_clean_timer);
856279621c0SAlberto Garcia         timer_free(s->cache_clean_timer);
857279621c0SAlberto Garcia         s->cache_clean_timer = NULL;
858279621c0SAlberto Garcia     }
859279621c0SAlberto Garcia }
860279621c0SAlberto Garcia 
861279621c0SAlberto Garcia static void qcow2_detach_aio_context(BlockDriverState *bs)
862279621c0SAlberto Garcia {
863279621c0SAlberto Garcia     cache_clean_timer_del(bs);
864279621c0SAlberto Garcia }
865279621c0SAlberto Garcia 
866279621c0SAlberto Garcia static void qcow2_attach_aio_context(BlockDriverState *bs,
867279621c0SAlberto Garcia                                      AioContext *new_context)
868279621c0SAlberto Garcia {
869279621c0SAlberto Garcia     cache_clean_timer_init(bs, new_context);
870279621c0SAlberto Garcia }
871279621c0SAlberto Garcia 
872bc85ef26SMax Reitz static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
873bc85ef26SMax Reitz                              uint64_t *l2_cache_size,
8741221fe6fSAlberto Garcia                              uint64_t *l2_cache_entry_size,
8756c1c8d5dSMax Reitz                              uint64_t *refcount_cache_size, Error **errp)
8766c1c8d5dSMax Reitz {
877ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
878b749562dSLeonid Bloch     uint64_t combined_cache_size, l2_cache_max_setting;
8796c1c8d5dSMax Reitz     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
880af39bd0dSAlberto Garcia     bool l2_cache_entry_size_set;
8817af5eea9SAlberto Garcia     int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
882b749562dSLeonid Bloch     uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
883b70d0820SAlberto Garcia     uint64_t max_l2_entries = DIV_ROUND_UP(virtual_disk_size, s->cluster_size);
884b70d0820SAlberto Garcia     /* An L2 table is always one cluster in size so the max cache size
885b70d0820SAlberto Garcia      * should be a multiple of the cluster size. */
886c8fd8554SAlberto Garcia     uint64_t max_l2_cache = ROUND_UP(max_l2_entries * l2_entry_size(s),
887b70d0820SAlberto Garcia                                      s->cluster_size);
8886c1c8d5dSMax Reitz 
8896c1c8d5dSMax Reitz     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
8906c1c8d5dSMax Reitz     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
8916c1c8d5dSMax Reitz     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
892af39bd0dSAlberto Garcia     l2_cache_entry_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE);
8936c1c8d5dSMax Reitz 
8946c1c8d5dSMax Reitz     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
895b749562dSLeonid Bloch     l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE,
896b749562dSLeonid Bloch                                              DEFAULT_L2_CACHE_MAX_SIZE);
8976c1c8d5dSMax Reitz     *refcount_cache_size = qemu_opt_get_size(opts,
8986c1c8d5dSMax Reitz                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
8996c1c8d5dSMax Reitz 
9001221fe6fSAlberto Garcia     *l2_cache_entry_size = qemu_opt_get_size(
9011221fe6fSAlberto Garcia         opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
9021221fe6fSAlberto Garcia 
903b749562dSLeonid Bloch     *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting);
904b749562dSLeonid Bloch 
9056c1c8d5dSMax Reitz     if (combined_cache_size_set) {
9066c1c8d5dSMax Reitz         if (l2_cache_size_set && refcount_cache_size_set) {
9076c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
9086c1c8d5dSMax Reitz                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
909308999e9SLeonid Bloch                        "at the same time");
9106c1c8d5dSMax Reitz             return;
911b749562dSLeonid Bloch         } else if (l2_cache_size_set &&
912b749562dSLeonid Bloch                    (l2_cache_max_setting > combined_cache_size)) {
9136c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
9146c1c8d5dSMax Reitz                        QCOW2_OPT_CACHE_SIZE);
9156c1c8d5dSMax Reitz             return;
9166c1c8d5dSMax Reitz         } else if (*refcount_cache_size > combined_cache_size) {
9176c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
9186c1c8d5dSMax Reitz                        QCOW2_OPT_CACHE_SIZE);
9196c1c8d5dSMax Reitz             return;
9206c1c8d5dSMax Reitz         }
9216c1c8d5dSMax Reitz 
9226c1c8d5dSMax Reitz         if (l2_cache_size_set) {
9236c1c8d5dSMax Reitz             *refcount_cache_size = combined_cache_size - *l2_cache_size;
9246c1c8d5dSMax Reitz         } else if (refcount_cache_size_set) {
9256c1c8d5dSMax Reitz             *l2_cache_size = combined_cache_size - *refcount_cache_size;
9266c1c8d5dSMax Reitz         } else {
92752253998SAlberto Garcia             /* Assign as much memory as possible to the L2 cache, and
92852253998SAlberto Garcia              * use the remainder for the refcount cache */
92952253998SAlberto Garcia             if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
93052253998SAlberto Garcia                 *l2_cache_size = max_l2_cache;
93152253998SAlberto Garcia                 *refcount_cache_size = combined_cache_size - *l2_cache_size;
93252253998SAlberto Garcia             } else {
93352253998SAlberto Garcia                 *refcount_cache_size =
93452253998SAlberto Garcia                     MIN(combined_cache_size, min_refcount_cache);
9356c1c8d5dSMax Reitz                 *l2_cache_size = combined_cache_size - *refcount_cache_size;
9366c1c8d5dSMax Reitz             }
93752253998SAlberto Garcia         }
9386c1c8d5dSMax Reitz     }
939af39bd0dSAlberto Garcia 
940af39bd0dSAlberto Garcia     /*
941af39bd0dSAlberto Garcia      * If the L2 cache is not enough to cover the whole disk then
942af39bd0dSAlberto Garcia      * default to 4KB entries. Smaller entries reduce the cost of
943af39bd0dSAlberto Garcia      * loads and evictions and increase I/O performance.
944af39bd0dSAlberto Garcia      */
945af39bd0dSAlberto Garcia     if (*l2_cache_size < max_l2_cache && !l2_cache_entry_size_set) {
946af39bd0dSAlberto Garcia         *l2_cache_entry_size = MIN(s->cluster_size, 4096);
947af39bd0dSAlberto Garcia     }
948af39bd0dSAlberto Garcia 
949657ada52SLeonid Bloch     /* l2_cache_size and refcount_cache_size are ensured to have at least
950657ada52SLeonid Bloch      * their minimum values in qcow2_update_options_prepare() */
9511221fe6fSAlberto Garcia 
9521221fe6fSAlberto Garcia     if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
9531221fe6fSAlberto Garcia         *l2_cache_entry_size > s->cluster_size ||
9541221fe6fSAlberto Garcia         !is_power_of_2(*l2_cache_entry_size)) {
9551221fe6fSAlberto Garcia         error_setg(errp, "L2 cache entry size must be a power of two "
9561221fe6fSAlberto Garcia                    "between %d and the cluster size (%d)",
9571221fe6fSAlberto Garcia                    1 << MIN_CLUSTER_BITS, s->cluster_size);
9581221fe6fSAlberto Garcia         return;
9591221fe6fSAlberto Garcia     }
9606c1c8d5dSMax Reitz }
9616c1c8d5dSMax Reitz 
962ee55b173SKevin Wolf typedef struct Qcow2ReopenState {
963ee55b173SKevin Wolf     Qcow2Cache *l2_table_cache;
964ee55b173SKevin Wolf     Qcow2Cache *refcount_block_cache;
9653c2e511aSAlberto Garcia     int l2_slice_size; /* Number of entries in a slice of the L2 table */
966ee55b173SKevin Wolf     bool use_lazy_refcounts;
967ee55b173SKevin Wolf     int overlap_check;
968ee55b173SKevin Wolf     bool discard_passthrough[QCOW2_DISCARD_MAX];
969ee55b173SKevin Wolf     uint64_t cache_clean_interval;
970b25b387fSDaniel P. Berrange     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
971ee55b173SKevin Wolf } Qcow2ReopenState;
972ee55b173SKevin Wolf 
973ee55b173SKevin Wolf static int qcow2_update_options_prepare(BlockDriverState *bs,
974ee55b173SKevin Wolf                                         Qcow2ReopenState *r,
975ee55b173SKevin Wolf                                         QDict *options, int flags,
976ee55b173SKevin Wolf                                         Error **errp)
9774c75d1a1SKevin Wolf {
9784c75d1a1SKevin Wolf     BDRVQcow2State *s = bs->opaque;
97994edf3fbSKevin Wolf     QemuOpts *opts = NULL;
9804c75d1a1SKevin Wolf     const char *opt_overlap_check, *opt_overlap_check_template;
9814c75d1a1SKevin Wolf     int overlap_check_template = 0;
9821221fe6fSAlberto Garcia     uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
9834c75d1a1SKevin Wolf     int i;
984b25b387fSDaniel P. Berrange     const char *encryptfmt;
985b25b387fSDaniel P. Berrange     QDict *encryptopts = NULL;
98694edf3fbSKevin Wolf     Error *local_err = NULL;
9874c75d1a1SKevin Wolf     int ret;
9884c75d1a1SKevin Wolf 
989b25b387fSDaniel P. Berrange     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
990b25b387fSDaniel P. Berrange     encryptfmt = qdict_get_try_str(encryptopts, "format");
991b25b387fSDaniel P. Berrange 
99294edf3fbSKevin Wolf     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
993af175e85SMarkus Armbruster     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
99494edf3fbSKevin Wolf         ret = -EINVAL;
99594edf3fbSKevin Wolf         goto fail;
99694edf3fbSKevin Wolf     }
99794edf3fbSKevin Wolf 
99894edf3fbSKevin Wolf     /* get L2 table/refcount block cache size from command line options */
9991221fe6fSAlberto Garcia     read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
10001221fe6fSAlberto Garcia                      &refcount_cache_size, &local_err);
100194edf3fbSKevin Wolf     if (local_err) {
100294edf3fbSKevin Wolf         error_propagate(errp, local_err);
100394edf3fbSKevin Wolf         ret = -EINVAL;
100494edf3fbSKevin Wolf         goto fail;
100594edf3fbSKevin Wolf     }
100694edf3fbSKevin Wolf 
10071221fe6fSAlberto Garcia     l2_cache_size /= l2_cache_entry_size;
100894edf3fbSKevin Wolf     if (l2_cache_size < MIN_L2_CACHE_SIZE) {
100994edf3fbSKevin Wolf         l2_cache_size = MIN_L2_CACHE_SIZE;
101094edf3fbSKevin Wolf     }
101194edf3fbSKevin Wolf     if (l2_cache_size > INT_MAX) {
101294edf3fbSKevin Wolf         error_setg(errp, "L2 cache size too big");
101394edf3fbSKevin Wolf         ret = -EINVAL;
101494edf3fbSKevin Wolf         goto fail;
101594edf3fbSKevin Wolf     }
101694edf3fbSKevin Wolf 
101794edf3fbSKevin Wolf     refcount_cache_size /= s->cluster_size;
101894edf3fbSKevin Wolf     if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
101994edf3fbSKevin Wolf         refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
102094edf3fbSKevin Wolf     }
102194edf3fbSKevin Wolf     if (refcount_cache_size > INT_MAX) {
102294edf3fbSKevin Wolf         error_setg(errp, "Refcount cache size too big");
102394edf3fbSKevin Wolf         ret = -EINVAL;
102494edf3fbSKevin Wolf         goto fail;
102594edf3fbSKevin Wolf     }
102694edf3fbSKevin Wolf 
10275b0959a7SKevin Wolf     /* alloc new L2 table/refcount block cache, flush old one */
10285b0959a7SKevin Wolf     if (s->l2_table_cache) {
10295b0959a7SKevin Wolf         ret = qcow2_cache_flush(bs, s->l2_table_cache);
10305b0959a7SKevin Wolf         if (ret) {
10315b0959a7SKevin Wolf             error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
10325b0959a7SKevin Wolf             goto fail;
10335b0959a7SKevin Wolf         }
10345b0959a7SKevin Wolf     }
10355b0959a7SKevin Wolf 
10365b0959a7SKevin Wolf     if (s->refcount_block_cache) {
10375b0959a7SKevin Wolf         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
10385b0959a7SKevin Wolf         if (ret) {
10395b0959a7SKevin Wolf             error_setg_errno(errp, -ret,
10405b0959a7SKevin Wolf                              "Failed to flush the refcount block cache");
10415b0959a7SKevin Wolf             goto fail;
10425b0959a7SKevin Wolf         }
10435b0959a7SKevin Wolf     }
10445b0959a7SKevin Wolf 
1045c8fd8554SAlberto Garcia     r->l2_slice_size = l2_cache_entry_size / l2_entry_size(s);
10461221fe6fSAlberto Garcia     r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
10471221fe6fSAlberto Garcia                                            l2_cache_entry_size);
10481221fe6fSAlberto Garcia     r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
10491221fe6fSAlberto Garcia                                                  s->cluster_size);
1050ee55b173SKevin Wolf     if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
105194edf3fbSKevin Wolf         error_setg(errp, "Could not allocate metadata caches");
105294edf3fbSKevin Wolf         ret = -ENOMEM;
105394edf3fbSKevin Wolf         goto fail;
105494edf3fbSKevin Wolf     }
105594edf3fbSKevin Wolf 
105694edf3fbSKevin Wolf     /* New interval for cache cleanup timer */
1057ee55b173SKevin Wolf     r->cache_clean_interval =
10585b0959a7SKevin Wolf         qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
1059e957b50bSLeonid Bloch                             DEFAULT_CACHE_CLEAN_INTERVAL);
106091203f08SAlberto Garcia #ifndef CONFIG_LINUX
106191203f08SAlberto Garcia     if (r->cache_clean_interval != 0) {
106291203f08SAlberto Garcia         error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
106391203f08SAlberto Garcia                    " not supported on this host");
106491203f08SAlberto Garcia         ret = -EINVAL;
106591203f08SAlberto Garcia         goto fail;
106691203f08SAlberto Garcia     }
106791203f08SAlberto Garcia #endif
1068ee55b173SKevin Wolf     if (r->cache_clean_interval > UINT_MAX) {
106994edf3fbSKevin Wolf         error_setg(errp, "Cache clean interval too big");
107094edf3fbSKevin Wolf         ret = -EINVAL;
107194edf3fbSKevin Wolf         goto fail;
107294edf3fbSKevin Wolf     }
107394edf3fbSKevin Wolf 
10745b0959a7SKevin Wolf     /* lazy-refcounts; flush if going from enabled to disabled */
1075ee55b173SKevin Wolf     r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
10764c75d1a1SKevin Wolf         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
1077ee55b173SKevin Wolf     if (r->use_lazy_refcounts && s->qcow_version < 3) {
1078007dbc39SKevin Wolf         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
1079007dbc39SKevin Wolf                    "qemu 1.1 compatibility level");
1080007dbc39SKevin Wolf         ret = -EINVAL;
1081007dbc39SKevin Wolf         goto fail;
1082007dbc39SKevin Wolf     }
10834c75d1a1SKevin Wolf 
10845b0959a7SKevin Wolf     if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
10855b0959a7SKevin Wolf         ret = qcow2_mark_clean(bs);
10865b0959a7SKevin Wolf         if (ret < 0) {
10875b0959a7SKevin Wolf             error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
10885b0959a7SKevin Wolf             goto fail;
10895b0959a7SKevin Wolf         }
10905b0959a7SKevin Wolf     }
10915b0959a7SKevin Wolf 
1092007dbc39SKevin Wolf     /* Overlap check options */
10934c75d1a1SKevin Wolf     opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
10944c75d1a1SKevin Wolf     opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
10954c75d1a1SKevin Wolf     if (opt_overlap_check_template && opt_overlap_check &&
10964c75d1a1SKevin Wolf         strcmp(opt_overlap_check_template, opt_overlap_check))
10974c75d1a1SKevin Wolf     {
10984c75d1a1SKevin Wolf         error_setg(errp, "Conflicting values for qcow2 options '"
10994c75d1a1SKevin Wolf                    QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
11004c75d1a1SKevin Wolf                    "' ('%s')", opt_overlap_check, opt_overlap_check_template);
11014c75d1a1SKevin Wolf         ret = -EINVAL;
11024c75d1a1SKevin Wolf         goto fail;
11034c75d1a1SKevin Wolf     }
11044c75d1a1SKevin Wolf     if (!opt_overlap_check) {
11054c75d1a1SKevin Wolf         opt_overlap_check = opt_overlap_check_template ?: "cached";
11064c75d1a1SKevin Wolf     }
11074c75d1a1SKevin Wolf 
11084c75d1a1SKevin Wolf     if (!strcmp(opt_overlap_check, "none")) {
11094c75d1a1SKevin Wolf         overlap_check_template = 0;
11104c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "constant")) {
11114c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_CONSTANT;
11124c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "cached")) {
11134c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_CACHED;
11144c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "all")) {
11154c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_ALL;
11164c75d1a1SKevin Wolf     } else {
11174c75d1a1SKevin Wolf         error_setg(errp, "Unsupported value '%s' for qcow2 option "
11184c75d1a1SKevin Wolf                    "'overlap-check'. Allowed are any of the following: "
11194c75d1a1SKevin Wolf                    "none, constant, cached, all", opt_overlap_check);
11204c75d1a1SKevin Wolf         ret = -EINVAL;
11214c75d1a1SKevin Wolf         goto fail;
11224c75d1a1SKevin Wolf     }
11234c75d1a1SKevin Wolf 
1124ee55b173SKevin Wolf     r->overlap_check = 0;
11254c75d1a1SKevin Wolf     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
11264c75d1a1SKevin Wolf         /* overlap-check defines a template bitmask, but every flag may be
11274c75d1a1SKevin Wolf          * overwritten through the associated boolean option */
1128ee55b173SKevin Wolf         r->overlap_check |=
11294c75d1a1SKevin Wolf             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
11304c75d1a1SKevin Wolf                               overlap_check_template & (1 << i)) << i;
11314c75d1a1SKevin Wolf     }
11324c75d1a1SKevin Wolf 
1133ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1134ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1135ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1136007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1137007dbc39SKevin Wolf                           flags & BDRV_O_UNMAP);
1138ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1139007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1140ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1141007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1142007dbc39SKevin Wolf 
1143b25b387fSDaniel P. Berrange     switch (s->crypt_method_header) {
1144b25b387fSDaniel P. Berrange     case QCOW_CRYPT_NONE:
1145b25b387fSDaniel P. Berrange         if (encryptfmt) {
1146b25b387fSDaniel P. Berrange             error_setg(errp, "No encryption in image header, but options "
1147b25b387fSDaniel P. Berrange                        "specified format '%s'", encryptfmt);
1148b25b387fSDaniel P. Berrange             ret = -EINVAL;
1149b25b387fSDaniel P. Berrange             goto fail;
1150b25b387fSDaniel P. Berrange         }
1151b25b387fSDaniel P. Berrange         break;
1152b25b387fSDaniel P. Berrange 
1153b25b387fSDaniel P. Berrange     case QCOW_CRYPT_AES:
1154b25b387fSDaniel P. Berrange         if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1155b25b387fSDaniel P. Berrange             error_setg(errp,
1156b25b387fSDaniel P. Berrange                        "Header reported 'aes' encryption format but "
1157b25b387fSDaniel P. Berrange                        "options specify '%s'", encryptfmt);
1158b25b387fSDaniel P. Berrange             ret = -EINVAL;
1159b25b387fSDaniel P. Berrange             goto fail;
1160b25b387fSDaniel P. Berrange         }
1161796d3239SMarkus Armbruster         qdict_put_str(encryptopts, "format", "qcow");
1162796d3239SMarkus Armbruster         r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1163b25b387fSDaniel P. Berrange         break;
1164b25b387fSDaniel P. Berrange 
11654652b8f3SDaniel P. Berrange     case QCOW_CRYPT_LUKS:
11664652b8f3SDaniel P. Berrange         if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
11674652b8f3SDaniel P. Berrange             error_setg(errp,
11684652b8f3SDaniel P. Berrange                        "Header reported 'luks' encryption format but "
11694652b8f3SDaniel P. Berrange                        "options specify '%s'", encryptfmt);
11704652b8f3SDaniel P. Berrange             ret = -EINVAL;
11714652b8f3SDaniel P. Berrange             goto fail;
11724652b8f3SDaniel P. Berrange         }
1173796d3239SMarkus Armbruster         qdict_put_str(encryptopts, "format", "luks");
1174796d3239SMarkus Armbruster         r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
11754652b8f3SDaniel P. Berrange         break;
11764652b8f3SDaniel P. Berrange 
1177b25b387fSDaniel P. Berrange     default:
1178b25b387fSDaniel P. Berrange         error_setg(errp, "Unsupported encryption method %d",
1179b25b387fSDaniel P. Berrange                    s->crypt_method_header);
1180b25b387fSDaniel P. Berrange         break;
1181b25b387fSDaniel P. Berrange     }
1182b25b387fSDaniel P. Berrange     if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1183b25b387fSDaniel P. Berrange         ret = -EINVAL;
1184b25b387fSDaniel P. Berrange         goto fail;
1185b25b387fSDaniel P. Berrange     }
1186b25b387fSDaniel P. Berrange 
11874c75d1a1SKevin Wolf     ret = 0;
11884c75d1a1SKevin Wolf fail:
1189cb3e7f08SMarc-André Lureau     qobject_unref(encryptopts);
119094edf3fbSKevin Wolf     qemu_opts_del(opts);
119194edf3fbSKevin Wolf     opts = NULL;
1192ee55b173SKevin Wolf     return ret;
1193ee55b173SKevin Wolf }
1194ee55b173SKevin Wolf 
1195ee55b173SKevin Wolf static void qcow2_update_options_commit(BlockDriverState *bs,
1196ee55b173SKevin Wolf                                         Qcow2ReopenState *r)
1197ee55b173SKevin Wolf {
1198ee55b173SKevin Wolf     BDRVQcow2State *s = bs->opaque;
1199ee55b173SKevin Wolf     int i;
1200ee55b173SKevin Wolf 
12015b0959a7SKevin Wolf     if (s->l2_table_cache) {
1202e64d4072SAlberto Garcia         qcow2_cache_destroy(s->l2_table_cache);
12035b0959a7SKevin Wolf     }
12045b0959a7SKevin Wolf     if (s->refcount_block_cache) {
1205e64d4072SAlberto Garcia         qcow2_cache_destroy(s->refcount_block_cache);
12065b0959a7SKevin Wolf     }
1207ee55b173SKevin Wolf     s->l2_table_cache = r->l2_table_cache;
1208ee55b173SKevin Wolf     s->refcount_block_cache = r->refcount_block_cache;
12093c2e511aSAlberto Garcia     s->l2_slice_size = r->l2_slice_size;
1210ee55b173SKevin Wolf 
1211ee55b173SKevin Wolf     s->overlap_check = r->overlap_check;
1212ee55b173SKevin Wolf     s->use_lazy_refcounts = r->use_lazy_refcounts;
1213ee55b173SKevin Wolf 
1214ee55b173SKevin Wolf     for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1215ee55b173SKevin Wolf         s->discard_passthrough[i] = r->discard_passthrough[i];
1216ee55b173SKevin Wolf     }
1217ee55b173SKevin Wolf 
12185b0959a7SKevin Wolf     if (s->cache_clean_interval != r->cache_clean_interval) {
12195b0959a7SKevin Wolf         cache_clean_timer_del(bs);
1220ee55b173SKevin Wolf         s->cache_clean_interval = r->cache_clean_interval;
1221ee55b173SKevin Wolf         cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1222ee55b173SKevin Wolf     }
1223b25b387fSDaniel P. Berrange 
1224b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1225b25b387fSDaniel P. Berrange     s->crypto_opts = r->crypto_opts;
12265b0959a7SKevin Wolf }
1227ee55b173SKevin Wolf 
1228ee55b173SKevin Wolf static void qcow2_update_options_abort(BlockDriverState *bs,
1229ee55b173SKevin Wolf                                        Qcow2ReopenState *r)
1230ee55b173SKevin Wolf {
1231ee55b173SKevin Wolf     if (r->l2_table_cache) {
1232e64d4072SAlberto Garcia         qcow2_cache_destroy(r->l2_table_cache);
1233ee55b173SKevin Wolf     }
1234ee55b173SKevin Wolf     if (r->refcount_block_cache) {
1235e64d4072SAlberto Garcia         qcow2_cache_destroy(r->refcount_block_cache);
1236ee55b173SKevin Wolf     }
1237b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1238ee55b173SKevin Wolf }
1239ee55b173SKevin Wolf 
1240ee55b173SKevin Wolf static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1241ee55b173SKevin Wolf                                 int flags, Error **errp)
1242ee55b173SKevin Wolf {
1243ee55b173SKevin Wolf     Qcow2ReopenState r = {};
1244ee55b173SKevin Wolf     int ret;
1245ee55b173SKevin Wolf 
1246ee55b173SKevin Wolf     ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1247ee55b173SKevin Wolf     if (ret >= 0) {
1248ee55b173SKevin Wolf         qcow2_update_options_commit(bs, &r);
1249ee55b173SKevin Wolf     } else {
1250ee55b173SKevin Wolf         qcow2_update_options_abort(bs, &r);
1251ee55b173SKevin Wolf     }
125294edf3fbSKevin Wolf 
12534c75d1a1SKevin Wolf     return ret;
12544c75d1a1SKevin Wolf }
12554c75d1a1SKevin Wolf 
1256572ad978SDenis Plotnikov static int validate_compression_type(BDRVQcow2State *s, Error **errp)
1257572ad978SDenis Plotnikov {
1258572ad978SDenis Plotnikov     switch (s->compression_type) {
1259572ad978SDenis Plotnikov     case QCOW2_COMPRESSION_TYPE_ZLIB:
1260d298ac10SDenis Plotnikov #ifdef CONFIG_ZSTD
1261d298ac10SDenis Plotnikov     case QCOW2_COMPRESSION_TYPE_ZSTD:
1262d298ac10SDenis Plotnikov #endif
1263572ad978SDenis Plotnikov         break;
1264572ad978SDenis Plotnikov 
1265572ad978SDenis Plotnikov     default:
1266572ad978SDenis Plotnikov         error_setg(errp, "qcow2: unknown compression type: %u",
1267572ad978SDenis Plotnikov                    s->compression_type);
1268572ad978SDenis Plotnikov         return -ENOTSUP;
1269572ad978SDenis Plotnikov     }
1270572ad978SDenis Plotnikov 
1271572ad978SDenis Plotnikov     /*
1272572ad978SDenis Plotnikov      * if the compression type differs from QCOW2_COMPRESSION_TYPE_ZLIB
1273572ad978SDenis Plotnikov      * the incompatible feature flag must be set
1274572ad978SDenis Plotnikov      */
1275572ad978SDenis Plotnikov     if (s->compression_type == QCOW2_COMPRESSION_TYPE_ZLIB) {
1276572ad978SDenis Plotnikov         if (s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION) {
1277572ad978SDenis Plotnikov             error_setg(errp, "qcow2: Compression type incompatible feature "
1278572ad978SDenis Plotnikov                              "bit must not be set");
1279572ad978SDenis Plotnikov             return -EINVAL;
1280572ad978SDenis Plotnikov         }
1281572ad978SDenis Plotnikov     } else {
1282572ad978SDenis Plotnikov         if (!(s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION)) {
1283572ad978SDenis Plotnikov             error_setg(errp, "qcow2: Compression type incompatible feature "
1284572ad978SDenis Plotnikov                              "bit must be set");
1285572ad978SDenis Plotnikov             return -EINVAL;
1286572ad978SDenis Plotnikov         }
1287572ad978SDenis Plotnikov     }
1288572ad978SDenis Plotnikov 
1289572ad978SDenis Plotnikov     return 0;
1290572ad978SDenis Plotnikov }
1291572ad978SDenis Plotnikov 
12921fafcd93SPaolo Bonzini /* Called with s->lock held.  */
12931fafcd93SPaolo Bonzini static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
12941fafcd93SPaolo Bonzini                                       int flags, Error **errp)
1295585f8587Sbellard {
1296ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
12976d33e8e7SKevin Wolf     unsigned int len, i;
12986d33e8e7SKevin Wolf     int ret = 0;
1299585f8587Sbellard     QCowHeader header;
130074c4510aSKevin Wolf     Error *local_err = NULL;
13019b80ddf3Saliguori     uint64_t ext_end;
13022cf7cfa1SKevin Wolf     uint64_t l1_vm_state_index;
130388ddffaeSVladimir Sementsov-Ogievskiy     bool update_header = false;
1304585f8587Sbellard 
1305cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
13066d85a57eSJes Sorensen     if (ret < 0) {
13073ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not read qcow2 header");
1308585f8587Sbellard         goto fail;
13096d85a57eSJes Sorensen     }
13103b698f52SPeter Maydell     header.magic = be32_to_cpu(header.magic);
13113b698f52SPeter Maydell     header.version = be32_to_cpu(header.version);
13123b698f52SPeter Maydell     header.backing_file_offset = be64_to_cpu(header.backing_file_offset);
13133b698f52SPeter Maydell     header.backing_file_size = be32_to_cpu(header.backing_file_size);
13143b698f52SPeter Maydell     header.size = be64_to_cpu(header.size);
13153b698f52SPeter Maydell     header.cluster_bits = be32_to_cpu(header.cluster_bits);
13163b698f52SPeter Maydell     header.crypt_method = be32_to_cpu(header.crypt_method);
13173b698f52SPeter Maydell     header.l1_table_offset = be64_to_cpu(header.l1_table_offset);
13183b698f52SPeter Maydell     header.l1_size = be32_to_cpu(header.l1_size);
13193b698f52SPeter Maydell     header.refcount_table_offset = be64_to_cpu(header.refcount_table_offset);
13203b698f52SPeter Maydell     header.refcount_table_clusters =
13213b698f52SPeter Maydell         be32_to_cpu(header.refcount_table_clusters);
13223b698f52SPeter Maydell     header.snapshots_offset = be64_to_cpu(header.snapshots_offset);
13233b698f52SPeter Maydell     header.nb_snapshots = be32_to_cpu(header.nb_snapshots);
1324585f8587Sbellard 
1325e8cdcec1SKevin Wolf     if (header.magic != QCOW_MAGIC) {
13263ef6c40aSMax Reitz         error_setg(errp, "Image is not in qcow2 format");
132776abe407SPaolo Bonzini         ret = -EINVAL;
1328585f8587Sbellard         goto fail;
13296d85a57eSJes Sorensen     }
13306744cbabSKevin Wolf     if (header.version < 2 || header.version > 3) {
1331a55448b3SMax Reitz         error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1332e8cdcec1SKevin Wolf         ret = -ENOTSUP;
1333e8cdcec1SKevin Wolf         goto fail;
1334e8cdcec1SKevin Wolf     }
13356744cbabSKevin Wolf 
13366744cbabSKevin Wolf     s->qcow_version = header.version;
13376744cbabSKevin Wolf 
133824342f2cSKevin Wolf     /* Initialise cluster size */
133924342f2cSKevin Wolf     if (header.cluster_bits < MIN_CLUSTER_BITS ||
134024342f2cSKevin Wolf         header.cluster_bits > MAX_CLUSTER_BITS) {
1341521b2b5dSMax Reitz         error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1342521b2b5dSMax Reitz                    header.cluster_bits);
134324342f2cSKevin Wolf         ret = -EINVAL;
134424342f2cSKevin Wolf         goto fail;
134524342f2cSKevin Wolf     }
134624342f2cSKevin Wolf 
134724342f2cSKevin Wolf     s->cluster_bits = header.cluster_bits;
134824342f2cSKevin Wolf     s->cluster_size = 1 << s->cluster_bits;
134924342f2cSKevin Wolf 
13506744cbabSKevin Wolf     /* Initialise version 3 header fields */
13516744cbabSKevin Wolf     if (header.version == 2) {
13526744cbabSKevin Wolf         header.incompatible_features    = 0;
13536744cbabSKevin Wolf         header.compatible_features      = 0;
13546744cbabSKevin Wolf         header.autoclear_features       = 0;
13556744cbabSKevin Wolf         header.refcount_order           = 4;
13566744cbabSKevin Wolf         header.header_length            = 72;
13576744cbabSKevin Wolf     } else {
13583b698f52SPeter Maydell         header.incompatible_features =
13593b698f52SPeter Maydell             be64_to_cpu(header.incompatible_features);
13603b698f52SPeter Maydell         header.compatible_features = be64_to_cpu(header.compatible_features);
13613b698f52SPeter Maydell         header.autoclear_features = be64_to_cpu(header.autoclear_features);
13623b698f52SPeter Maydell         header.refcount_order = be32_to_cpu(header.refcount_order);
13633b698f52SPeter Maydell         header.header_length = be32_to_cpu(header.header_length);
136424342f2cSKevin Wolf 
136524342f2cSKevin Wolf         if (header.header_length < 104) {
136624342f2cSKevin Wolf             error_setg(errp, "qcow2 header too short");
136724342f2cSKevin Wolf             ret = -EINVAL;
136824342f2cSKevin Wolf             goto fail;
136924342f2cSKevin Wolf         }
137024342f2cSKevin Wolf     }
137124342f2cSKevin Wolf 
137224342f2cSKevin Wolf     if (header.header_length > s->cluster_size) {
137324342f2cSKevin Wolf         error_setg(errp, "qcow2 header exceeds cluster size");
137424342f2cSKevin Wolf         ret = -EINVAL;
137524342f2cSKevin Wolf         goto fail;
13766744cbabSKevin Wolf     }
13776744cbabSKevin Wolf 
13786744cbabSKevin Wolf     if (header.header_length > sizeof(header)) {
13796744cbabSKevin Wolf         s->unknown_header_fields_size = header.header_length - sizeof(header);
13806744cbabSKevin Wolf         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1381cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
13826744cbabSKevin Wolf                          s->unknown_header_fields_size);
13836744cbabSKevin Wolf         if (ret < 0) {
13843ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
13853ef6c40aSMax Reitz                              "fields");
13866744cbabSKevin Wolf             goto fail;
13876744cbabSKevin Wolf         }
13886744cbabSKevin Wolf     }
13896744cbabSKevin Wolf 
1390a1b3955cSKevin Wolf     if (header.backing_file_offset > s->cluster_size) {
1391a1b3955cSKevin Wolf         error_setg(errp, "Invalid backing file offset");
1392a1b3955cSKevin Wolf         ret = -EINVAL;
1393a1b3955cSKevin Wolf         goto fail;
1394a1b3955cSKevin Wolf     }
1395a1b3955cSKevin Wolf 
1396cfcc4c62SKevin Wolf     if (header.backing_file_offset) {
1397cfcc4c62SKevin Wolf         ext_end = header.backing_file_offset;
1398cfcc4c62SKevin Wolf     } else {
1399cfcc4c62SKevin Wolf         ext_end = 1 << header.cluster_bits;
1400cfcc4c62SKevin Wolf     }
1401cfcc4c62SKevin Wolf 
14026744cbabSKevin Wolf     /* Handle feature bits */
14036744cbabSKevin Wolf     s->incompatible_features    = header.incompatible_features;
14046744cbabSKevin Wolf     s->compatible_features      = header.compatible_features;
14056744cbabSKevin Wolf     s->autoclear_features       = header.autoclear_features;
14066744cbabSKevin Wolf 
1407572ad978SDenis Plotnikov     /*
1408572ad978SDenis Plotnikov      * Handle compression type
1409572ad978SDenis Plotnikov      * Older qcow2 images don't contain the compression type header.
1410572ad978SDenis Plotnikov      * Distinguish them by the header length and use
1411572ad978SDenis Plotnikov      * the only valid (default) compression type in that case
1412572ad978SDenis Plotnikov      */
1413572ad978SDenis Plotnikov     if (header.header_length > offsetof(QCowHeader, compression_type)) {
1414572ad978SDenis Plotnikov         s->compression_type = header.compression_type;
1415572ad978SDenis Plotnikov     } else {
1416572ad978SDenis Plotnikov         s->compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
1417572ad978SDenis Plotnikov     }
1418572ad978SDenis Plotnikov 
1419572ad978SDenis Plotnikov     ret = validate_compression_type(s, errp);
1420572ad978SDenis Plotnikov     if (ret) {
1421572ad978SDenis Plotnikov         goto fail;
1422572ad978SDenis Plotnikov     }
1423572ad978SDenis Plotnikov 
1424c61d0004SStefan Hajnoczi     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1425cfcc4c62SKevin Wolf         void *feature_table = NULL;
1426cfcc4c62SKevin Wolf         qcow2_read_extensions(bs, header.header_length, ext_end,
142788ddffaeSVladimir Sementsov-Ogievskiy                               &feature_table, flags, NULL, NULL);
1428a55448b3SMax Reitz         report_unsupported_feature(errp, feature_table,
1429c61d0004SStefan Hajnoczi                                    s->incompatible_features &
1430c61d0004SStefan Hajnoczi                                    ~QCOW2_INCOMPAT_MASK);
14316744cbabSKevin Wolf         ret = -ENOTSUP;
1432c5a33ee9SPrasad Joshi         g_free(feature_table);
14336744cbabSKevin Wolf         goto fail;
14346744cbabSKevin Wolf     }
14356744cbabSKevin Wolf 
143669c98726SMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
143769c98726SMax Reitz         /* Corrupt images may not be written to unless they are being repaired
143869c98726SMax Reitz          */
143969c98726SMax Reitz         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
14403ef6c40aSMax Reitz             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
14413ef6c40aSMax Reitz                        "read/write");
144269c98726SMax Reitz             ret = -EACCES;
144369c98726SMax Reitz             goto fail;
144469c98726SMax Reitz         }
144569c98726SMax Reitz     }
144669c98726SMax Reitz 
1447d0346b55SAlberto Garcia     s->subclusters_per_cluster =
1448d0346b55SAlberto Garcia         has_subclusters(s) ? QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER : 1;
1449d0346b55SAlberto Garcia     s->subcluster_size = s->cluster_size / s->subclusters_per_cluster;
1450d0346b55SAlberto Garcia     s->subcluster_bits = ctz32(s->subcluster_size);
1451d0346b55SAlberto Garcia 
14526744cbabSKevin Wolf     /* Check support for various header values */
1453b72faf9fSMax Reitz     if (header.refcount_order > 6) {
1454b72faf9fSMax Reitz         error_setg(errp, "Reference count entry width too large; may not "
1455b72faf9fSMax Reitz                    "exceed 64 bits");
1456b72faf9fSMax Reitz         ret = -EINVAL;
14576744cbabSKevin Wolf         goto fail;
14586744cbabSKevin Wolf     }
1459b6481f37SMax Reitz     s->refcount_order = header.refcount_order;
1460346a53dfSMax Reitz     s->refcount_bits = 1 << s->refcount_order;
1461346a53dfSMax Reitz     s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1462346a53dfSMax Reitz     s->refcount_max += s->refcount_max - 1;
14636744cbabSKevin Wolf 
1464585f8587Sbellard     s->crypt_method_header = header.crypt_method;
14656d85a57eSJes Sorensen     if (s->crypt_method_header) {
1466e6ff69bfSDaniel P. Berrange         if (bdrv_uses_whitelist() &&
1467e6ff69bfSDaniel P. Berrange             s->crypt_method_header == QCOW_CRYPT_AES) {
14688c0dcbc4SDaniel P. Berrange             error_setg(errp,
14698c0dcbc4SDaniel P. Berrange                        "Use of AES-CBC encrypted qcow2 images is no longer "
14708c0dcbc4SDaniel P. Berrange                        "supported in system emulators");
14718c0dcbc4SDaniel P. Berrange             error_append_hint(errp,
14728c0dcbc4SDaniel P. Berrange                               "You can use 'qemu-img convert' to convert your "
14738c0dcbc4SDaniel P. Berrange                               "image to an alternative supported format, such "
14748c0dcbc4SDaniel P. Berrange                               "as unencrypted qcow2, or raw with the LUKS "
14758c0dcbc4SDaniel P. Berrange                               "format instead.\n");
14768c0dcbc4SDaniel P. Berrange             ret = -ENOSYS;
14778c0dcbc4SDaniel P. Berrange             goto fail;
1478e6ff69bfSDaniel P. Berrange         }
1479e6ff69bfSDaniel P. Berrange 
14804652b8f3SDaniel P. Berrange         if (s->crypt_method_header == QCOW_CRYPT_AES) {
14814652b8f3SDaniel P. Berrange             s->crypt_physical_offset = false;
14824652b8f3SDaniel P. Berrange         } else {
14834652b8f3SDaniel P. Berrange             /* Assuming LUKS and any future crypt methods we
14844652b8f3SDaniel P. Berrange              * add will all use physical offsets, due to the
14854652b8f3SDaniel P. Berrange              * fact that the alternative is insecure...  */
14864652b8f3SDaniel P. Berrange             s->crypt_physical_offset = true;
14874652b8f3SDaniel P. Berrange         }
14884652b8f3SDaniel P. Berrange 
148954115412SEric Blake         bs->encrypted = true;
14906d85a57eSJes Sorensen     }
149124342f2cSKevin Wolf 
1492c8fd8554SAlberto Garcia     s->l2_bits = s->cluster_bits - ctz32(l2_entry_size(s));
1493585f8587Sbellard     s->l2_size = 1 << s->l2_bits;
14941d13d654SMax Reitz     /* 2^(s->refcount_order - 3) is the refcount width in bytes */
14951d13d654SMax Reitz     s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
14961d13d654SMax Reitz     s->refcount_block_size = 1 << s->refcount_block_bits;
1497bd016b91SLeonid Bloch     bs->total_sectors = header.size / BDRV_SECTOR_SIZE;
1498585f8587Sbellard     s->csize_shift = (62 - (s->cluster_bits - 8));
1499585f8587Sbellard     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1500585f8587Sbellard     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
15015dab2fadSKevin Wolf 
1502585f8587Sbellard     s->refcount_table_offset = header.refcount_table_offset;
1503585f8587Sbellard     s->refcount_table_size =
1504585f8587Sbellard         header.refcount_table_clusters << (s->cluster_bits - 3);
1505585f8587Sbellard 
1506951053a9SAlberto Garcia     if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1507951053a9SAlberto Garcia         error_setg(errp, "Image does not contain a reference count table");
1508951053a9SAlberto Garcia         ret = -EINVAL;
1509951053a9SAlberto Garcia         goto fail;
1510951053a9SAlberto Garcia     }
1511951053a9SAlberto Garcia 
15120cf0e598SAlberto Garcia     ret = qcow2_validate_table(bs, s->refcount_table_offset,
15130cf0e598SAlberto Garcia                                header.refcount_table_clusters,
15140cf0e598SAlberto Garcia                                s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
15150cf0e598SAlberto Garcia                                "Reference count table", errp);
15168c7de283SKevin Wolf     if (ret < 0) {
15178c7de283SKevin Wolf         goto fail;
15188c7de283SKevin Wolf     }
15198c7de283SKevin Wolf 
15208bc584feSMax Reitz     if (!(flags & BDRV_O_CHECK)) {
15218bc584feSMax Reitz         /*
15228bc584feSMax Reitz          * The total size in bytes of the snapshot table is checked in
15230cf0e598SAlberto Garcia          * qcow2_read_snapshots() because the size of each snapshot is
15240cf0e598SAlberto Garcia          * variable and we don't know it yet.
15258bc584feSMax Reitz          * Here we only check the offset and number of snapshots.
15268bc584feSMax Reitz          */
15270cf0e598SAlberto Garcia         ret = qcow2_validate_table(bs, header.snapshots_offset,
1528ce48f2f4SKevin Wolf                                    header.nb_snapshots,
15290cf0e598SAlberto Garcia                                    sizeof(QCowSnapshotHeader),
15308bc584feSMax Reitz                                    sizeof(QCowSnapshotHeader) *
15318bc584feSMax Reitz                                        QCOW_MAX_SNAPSHOTS,
15320cf0e598SAlberto Garcia                                    "Snapshot table", errp);
1533ce48f2f4SKevin Wolf         if (ret < 0) {
1534ce48f2f4SKevin Wolf             goto fail;
1535ce48f2f4SKevin Wolf         }
15368bc584feSMax Reitz     }
1537ce48f2f4SKevin Wolf 
1538585f8587Sbellard     /* read the level 1 table */
15390cf0e598SAlberto Garcia     ret = qcow2_validate_table(bs, header.l1_table_offset,
15400cf0e598SAlberto Garcia                                header.l1_size, sizeof(uint64_t),
15410cf0e598SAlberto Garcia                                QCOW_MAX_L1_SIZE, "Active L1 table", errp);
15420cf0e598SAlberto Garcia     if (ret < 0) {
15432d51c32cSKevin Wolf         goto fail;
15442d51c32cSKevin Wolf     }
1545585f8587Sbellard     s->l1_size = header.l1_size;
15460cf0e598SAlberto Garcia     s->l1_table_offset = header.l1_table_offset;
15472cf7cfa1SKevin Wolf 
15482cf7cfa1SKevin Wolf     l1_vm_state_index = size_to_l1(s, header.size);
15492cf7cfa1SKevin Wolf     if (l1_vm_state_index > INT_MAX) {
15503ef6c40aSMax Reitz         error_setg(errp, "Image is too big");
15512cf7cfa1SKevin Wolf         ret = -EFBIG;
15522cf7cfa1SKevin Wolf         goto fail;
15532cf7cfa1SKevin Wolf     }
15542cf7cfa1SKevin Wolf     s->l1_vm_state_index = l1_vm_state_index;
15552cf7cfa1SKevin Wolf 
1556585f8587Sbellard     /* the L1 table must contain at least enough entries to put
1557585f8587Sbellard        header.size bytes */
15586d85a57eSJes Sorensen     if (s->l1_size < s->l1_vm_state_index) {
15593ef6c40aSMax Reitz         error_setg(errp, "L1 table is too small");
15606d85a57eSJes Sorensen         ret = -EINVAL;
1561585f8587Sbellard         goto fail;
15626d85a57eSJes Sorensen     }
15632d51c32cSKevin Wolf 
1564d191d12dSStefan Weil     if (s->l1_size > 0) {
15659a4f4c31SKevin Wolf         s->l1_table = qemu_try_blockalign(bs->file->bs,
1566ef97d608SAlberto Garcia                                           s->l1_size * sizeof(uint64_t));
1567de82815dSKevin Wolf         if (s->l1_table == NULL) {
1568de82815dSKevin Wolf             error_setg(errp, "Could not allocate L1 table");
1569de82815dSKevin Wolf             ret = -ENOMEM;
1570de82815dSKevin Wolf             goto fail;
1571de82815dSKevin Wolf         }
1572cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
15736d85a57eSJes Sorensen                          s->l1_size * sizeof(uint64_t));
15746d85a57eSJes Sorensen         if (ret < 0) {
15753ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read L1 table");
1576585f8587Sbellard             goto fail;
15776d85a57eSJes Sorensen         }
1578585f8587Sbellard         for(i = 0;i < s->l1_size; i++) {
15793b698f52SPeter Maydell             s->l1_table[i] = be64_to_cpu(s->l1_table[i]);
1580585f8587Sbellard         }
1581d191d12dSStefan Weil     }
158229c1a730SKevin Wolf 
158394edf3fbSKevin Wolf     /* Parse driver-specific options */
158494edf3fbSKevin Wolf     ret = qcow2_update_options(bs, options, flags, errp);
158590efa0eaSKevin Wolf     if (ret < 0) {
158690efa0eaSKevin Wolf         goto fail;
158790efa0eaSKevin Wolf     }
158890efa0eaSKevin Wolf 
158906d9260fSAnthony Liguori     s->flags = flags;
1590585f8587Sbellard 
15916d85a57eSJes Sorensen     ret = qcow2_refcount_init(bs);
15926d85a57eSJes Sorensen     if (ret != 0) {
15933ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1594585f8587Sbellard         goto fail;
15956d85a57eSJes Sorensen     }
1596585f8587Sbellard 
159772cf2d4fSBlue Swirl     QLIST_INIT(&s->cluster_allocs);
15980b919faeSKevin Wolf     QTAILQ_INIT(&s->discards);
1599f214978aSKevin Wolf 
16009b80ddf3Saliguori     /* read qcow2 extensions */
16013ef6c40aSMax Reitz     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1602af175e85SMarkus Armbruster                               flags, &update_header, errp)) {
16036d85a57eSJes Sorensen         ret = -EINVAL;
16049b80ddf3Saliguori         goto fail;
16056d85a57eSJes Sorensen     }
16069b80ddf3Saliguori 
16070e8c08beSKevin Wolf     /* Open external data file */
16088b1869daSMax Reitz     s->data_file = bdrv_open_child(NULL, options, "data-file", bs,
16098b1869daSMax Reitz                                    &child_of_bds, BDRV_CHILD_DATA,
16108b1869daSMax Reitz                                    true, &local_err);
16110e8c08beSKevin Wolf     if (local_err) {
16120e8c08beSKevin Wolf         error_propagate(errp, local_err);
16130e8c08beSKevin Wolf         ret = -EINVAL;
16140e8c08beSKevin Wolf         goto fail;
16150e8c08beSKevin Wolf     }
16160e8c08beSKevin Wolf 
16170e8c08beSKevin Wolf     if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) {
16189b890bdcSKevin Wolf         if (!s->data_file && s->image_data_file) {
16199b890bdcSKevin Wolf             s->data_file = bdrv_open_child(s->image_data_file, options,
16208b1869daSMax Reitz                                            "data-file", bs, &child_of_bds,
16218b1869daSMax Reitz                                            BDRV_CHILD_DATA, false, errp);
16229b890bdcSKevin Wolf             if (!s->data_file) {
16239b890bdcSKevin Wolf                 ret = -EINVAL;
16249b890bdcSKevin Wolf                 goto fail;
16259b890bdcSKevin Wolf             }
16269b890bdcSKevin Wolf         }
16270e8c08beSKevin Wolf         if (!s->data_file) {
16280e8c08beSKevin Wolf             error_setg(errp, "'data-file' is required for this image");
16290e8c08beSKevin Wolf             ret = -EINVAL;
16300e8c08beSKevin Wolf             goto fail;
16310e8c08beSKevin Wolf         }
16328b1869daSMax Reitz 
16338b1869daSMax Reitz         /* No data here */
16348b1869daSMax Reitz         bs->file->role &= ~BDRV_CHILD_DATA;
16358b1869daSMax Reitz 
16368b1869daSMax Reitz         /* Must succeed because we have given up permissions if anything */
16378b1869daSMax Reitz         bdrv_child_refresh_perms(bs, bs->file, &error_abort);
16380e8c08beSKevin Wolf     } else {
16390e8c08beSKevin Wolf         if (s->data_file) {
16400e8c08beSKevin Wolf             error_setg(errp, "'data-file' can only be set for images with an "
16410e8c08beSKevin Wolf                              "external data file");
16420e8c08beSKevin Wolf             ret = -EINVAL;
16430e8c08beSKevin Wolf             goto fail;
16446c3944dcSKevin Wolf         }
16456c3944dcSKevin Wolf 
164693c24936SKevin Wolf         s->data_file = bs->file;
16476c3944dcSKevin Wolf 
16486c3944dcSKevin Wolf         if (data_file_is_raw(bs)) {
16496c3944dcSKevin Wolf             error_setg(errp, "data-file-raw requires a data file");
16506c3944dcSKevin Wolf             ret = -EINVAL;
16516c3944dcSKevin Wolf             goto fail;
16520e8c08beSKevin Wolf         }
16530e8c08beSKevin Wolf     }
165493c24936SKevin Wolf 
16554652b8f3SDaniel P. Berrange     /* qcow2_read_extension may have set up the crypto context
16564652b8f3SDaniel P. Berrange      * if the crypt method needs a header region, some methods
16574652b8f3SDaniel P. Berrange      * don't need header extensions, so must check here
16584652b8f3SDaniel P. Berrange      */
16594652b8f3SDaniel P. Berrange     if (s->crypt_method_header && !s->crypto) {
1660b25b387fSDaniel P. Berrange         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1661b25b387fSDaniel P. Berrange             unsigned int cflags = 0;
1662b25b387fSDaniel P. Berrange             if (flags & BDRV_O_NO_IO) {
1663b25b387fSDaniel P. Berrange                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1664b25b387fSDaniel P. Berrange             }
16651cd9a787SDaniel P. Berrange             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
16668ac0f15fSVladimir Sementsov-Ogievskiy                                            NULL, NULL, cflags,
16678ac0f15fSVladimir Sementsov-Ogievskiy                                            QCOW2_MAX_THREADS, errp);
1668b25b387fSDaniel P. Berrange             if (!s->crypto) {
1669b25b387fSDaniel P. Berrange                 ret = -EINVAL;
1670b25b387fSDaniel P. Berrange                 goto fail;
1671b25b387fSDaniel P. Berrange             }
16724652b8f3SDaniel P. Berrange         } else if (!(flags & BDRV_O_NO_IO)) {
16734652b8f3SDaniel P. Berrange             error_setg(errp, "Missing CRYPTO header for crypt method %d",
16744652b8f3SDaniel P. Berrange                        s->crypt_method_header);
16754652b8f3SDaniel P. Berrange             ret = -EINVAL;
16764652b8f3SDaniel P. Berrange             goto fail;
16774652b8f3SDaniel P. Berrange         }
1678b25b387fSDaniel P. Berrange     }
1679b25b387fSDaniel P. Berrange 
1680585f8587Sbellard     /* read the backing file name */
1681585f8587Sbellard     if (header.backing_file_offset != 0) {
1682585f8587Sbellard         len = header.backing_file_size;
16839a29e18fSJeff Cody         if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1684e729fa6aSJeff Cody             len >= sizeof(bs->backing_file)) {
16856d33e8e7SKevin Wolf             error_setg(errp, "Backing file name too long");
16866d33e8e7SKevin Wolf             ret = -EINVAL;
16876d33e8e7SKevin Wolf             goto fail;
16886d85a57eSJes Sorensen         }
1689cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, header.backing_file_offset,
1690998c2019SMax Reitz                          bs->auto_backing_file, len);
16916d85a57eSJes Sorensen         if (ret < 0) {
16923ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read backing file name");
1693585f8587Sbellard             goto fail;
16946d85a57eSJes Sorensen         }
1695998c2019SMax Reitz         bs->auto_backing_file[len] = '\0';
1696998c2019SMax Reitz         pstrcpy(bs->backing_file, sizeof(bs->backing_file),
1697998c2019SMax Reitz                 bs->auto_backing_file);
1698998c2019SMax Reitz         s->image_backing_file = g_strdup(bs->auto_backing_file);
1699585f8587Sbellard     }
170042deb29fSKevin Wolf 
17018bc584feSMax Reitz     /*
17028bc584feSMax Reitz      * Internal snapshots; skip reading them in check mode, because
17038bc584feSMax Reitz      * we do not need them then, and we do not want to abort because
17048bc584feSMax Reitz      * of a broken table.
17058bc584feSMax Reitz      */
17068bc584feSMax Reitz     if (!(flags & BDRV_O_CHECK)) {
170711b128f4SKevin Wolf         s->snapshots_offset = header.snapshots_offset;
170811b128f4SKevin Wolf         s->nb_snapshots = header.nb_snapshots;
170911b128f4SKevin Wolf 
1710ecf6c7c0SMax Reitz         ret = qcow2_read_snapshots(bs, errp);
171142deb29fSKevin Wolf         if (ret < 0) {
1712585f8587Sbellard             goto fail;
17136d85a57eSJes Sorensen         }
17148bc584feSMax Reitz     }
1715585f8587Sbellard 
1716af7b708dSStefan Hajnoczi     /* Clear unknown autoclear feature bits */
171788ddffaeSVladimir Sementsov-Ogievskiy     update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1718d1258dd0SVladimir Sementsov-Ogievskiy     update_header =
1719d1258dd0SVladimir Sementsov-Ogievskiy         update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1720d1258dd0SVladimir Sementsov-Ogievskiy     if (update_header) {
172188ddffaeSVladimir Sementsov-Ogievskiy         s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1722d1258dd0SVladimir Sementsov-Ogievskiy     }
1723d1258dd0SVladimir Sementsov-Ogievskiy 
17249c98f145SVladimir Sementsov-Ogievskiy     /* == Handle persistent dirty bitmaps ==
17259c98f145SVladimir Sementsov-Ogievskiy      *
17269c98f145SVladimir Sementsov-Ogievskiy      * We want load dirty bitmaps in three cases:
17279c98f145SVladimir Sementsov-Ogievskiy      *
17289c98f145SVladimir Sementsov-Ogievskiy      * 1. Normal open of the disk in active mode, not related to invalidation
17299c98f145SVladimir Sementsov-Ogievskiy      *    after migration.
17309c98f145SVladimir Sementsov-Ogievskiy      *
17319c98f145SVladimir Sementsov-Ogievskiy      * 2. Invalidation of the target vm after pre-copy phase of migration, if
17329c98f145SVladimir Sementsov-Ogievskiy      *    bitmaps are _not_ migrating through migration channel, i.e.
17339c98f145SVladimir Sementsov-Ogievskiy      *    'dirty-bitmaps' capability is disabled.
17349c98f145SVladimir Sementsov-Ogievskiy      *
17359c98f145SVladimir Sementsov-Ogievskiy      * 3. Invalidation of source vm after failed or canceled migration.
17369c98f145SVladimir Sementsov-Ogievskiy      *    This is a very interesting case. There are two possible types of
17379c98f145SVladimir Sementsov-Ogievskiy      *    bitmaps:
17389c98f145SVladimir Sementsov-Ogievskiy      *
17399c98f145SVladimir Sementsov-Ogievskiy      *    A. Stored on inactivation and removed. They should be loaded from the
17409c98f145SVladimir Sementsov-Ogievskiy      *       image.
17419c98f145SVladimir Sementsov-Ogievskiy      *
17429c98f145SVladimir Sementsov-Ogievskiy      *    B. Not stored: not-persistent bitmaps and bitmaps, migrated through
17439c98f145SVladimir Sementsov-Ogievskiy      *       the migration channel (with dirty-bitmaps capability).
17449c98f145SVladimir Sementsov-Ogievskiy      *
17459c98f145SVladimir Sementsov-Ogievskiy      *    On the other hand, there are two possible sub-cases:
17469c98f145SVladimir Sementsov-Ogievskiy      *
17479c98f145SVladimir Sementsov-Ogievskiy      *    3.1 disk was changed by somebody else while were inactive. In this
17489c98f145SVladimir Sementsov-Ogievskiy      *        case all in-RAM dirty bitmaps (both persistent and not) are
17499c98f145SVladimir Sementsov-Ogievskiy      *        definitely invalid. And we don't have any method to determine
17509c98f145SVladimir Sementsov-Ogievskiy      *        this.
17519c98f145SVladimir Sementsov-Ogievskiy      *
17529c98f145SVladimir Sementsov-Ogievskiy      *        Simple and safe thing is to just drop all the bitmaps of type B on
17539c98f145SVladimir Sementsov-Ogievskiy      *        inactivation. But in this case we lose bitmaps in valid 4.2 case.
17549c98f145SVladimir Sementsov-Ogievskiy      *
17559c98f145SVladimir Sementsov-Ogievskiy      *        On the other hand, resuming source vm, if disk was already changed
17569c98f145SVladimir Sementsov-Ogievskiy      *        is a bad thing anyway: not only bitmaps, the whole vm state is
17579c98f145SVladimir Sementsov-Ogievskiy      *        out of sync with disk.
17589c98f145SVladimir Sementsov-Ogievskiy      *
17599c98f145SVladimir Sementsov-Ogievskiy      *        This means, that user or management tool, who for some reason
17609c98f145SVladimir Sementsov-Ogievskiy      *        decided to resume source vm, after disk was already changed by
17619c98f145SVladimir Sementsov-Ogievskiy      *        target vm, should at least drop all dirty bitmaps by hand.
17629c98f145SVladimir Sementsov-Ogievskiy      *
17639c98f145SVladimir Sementsov-Ogievskiy      *        So, we can ignore this case for now, but TODO: "generation"
17649c98f145SVladimir Sementsov-Ogievskiy      *        extension for qcow2, to determine, that image was changed after
17659c98f145SVladimir Sementsov-Ogievskiy      *        last inactivation. And if it is changed, we will drop (or at least
17669c98f145SVladimir Sementsov-Ogievskiy      *        mark as 'invalid' all the bitmaps of type B, both persistent
17679c98f145SVladimir Sementsov-Ogievskiy      *        and not).
17689c98f145SVladimir Sementsov-Ogievskiy      *
17699c98f145SVladimir Sementsov-Ogievskiy      *    3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
17709c98f145SVladimir Sementsov-Ogievskiy      *        to disk ('dirty-bitmaps' capability disabled), or not saved
17719c98f145SVladimir Sementsov-Ogievskiy      *        ('dirty-bitmaps' capability enabled), but we don't need to care
17729c98f145SVladimir Sementsov-Ogievskiy      *        of: let's load bitmaps as always: stored bitmaps will be loaded,
17739c98f145SVladimir Sementsov-Ogievskiy      *        and not stored has flag IN_USE=1 in the image and will be skipped
17749c98f145SVladimir Sementsov-Ogievskiy      *        on loading.
17759c98f145SVladimir Sementsov-Ogievskiy      *
17769c98f145SVladimir Sementsov-Ogievskiy      * One remaining possible case when we don't want load bitmaps:
17779c98f145SVladimir Sementsov-Ogievskiy      *
17789c98f145SVladimir Sementsov-Ogievskiy      * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
17799c98f145SVladimir Sementsov-Ogievskiy      *    will be loaded on invalidation, no needs try loading them before)
17809c98f145SVladimir Sementsov-Ogievskiy      */
17819c98f145SVladimir Sementsov-Ogievskiy 
17829c98f145SVladimir Sementsov-Ogievskiy     if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) {
17839c98f145SVladimir Sementsov-Ogievskiy         /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
17849c98f145SVladimir Sementsov-Ogievskiy         bool header_updated = qcow2_load_dirty_bitmaps(bs, &local_err);
1785d1258dd0SVladimir Sementsov-Ogievskiy         if (local_err != NULL) {
1786d1258dd0SVladimir Sementsov-Ogievskiy             error_propagate(errp, local_err);
1787d1258dd0SVladimir Sementsov-Ogievskiy             ret = -EINVAL;
1788d1258dd0SVladimir Sementsov-Ogievskiy             goto fail;
1789d1258dd0SVladimir Sementsov-Ogievskiy         }
1790d1258dd0SVladimir Sementsov-Ogievskiy 
179166be5c3eSTuguoyi         update_header = update_header && !header_updated;
179266be5c3eSTuguoyi     }
179366be5c3eSTuguoyi 
1794d1258dd0SVladimir Sementsov-Ogievskiy     if (update_header) {
1795af7b708dSStefan Hajnoczi         ret = qcow2_update_header(bs);
1796af7b708dSStefan Hajnoczi         if (ret < 0) {
17973ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not update qcow2 header");
1798af7b708dSStefan Hajnoczi             goto fail;
1799af7b708dSStefan Hajnoczi         }
1800af7b708dSStefan Hajnoczi     }
1801af7b708dSStefan Hajnoczi 
18023b650816SKevin Wolf     bs->supported_zero_flags = header.version >= 3 ?
18033b650816SKevin Wolf                                BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK : 0;
1804f01643fbSKevin Wolf     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
180568d100e9SKevin Wolf 
1806c61d0004SStefan Hajnoczi     /* Repair image if dirty */
180704c01a5cSKevin Wolf     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1808058f8f16SStefan Hajnoczi         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1809c61d0004SStefan Hajnoczi         BdrvCheckResult result = {0};
1810c61d0004SStefan Hajnoczi 
18112fd61638SPaolo Bonzini         ret = qcow2_co_check_locked(bs, &result,
18122fd61638SPaolo Bonzini                                     BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1813791fff50SMax Reitz         if (ret < 0 || result.check_errors) {
1814791fff50SMax Reitz             if (ret >= 0) {
1815791fff50SMax Reitz                 ret = -EIO;
1816791fff50SMax Reitz             }
18173ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not repair dirty image");
1818c61d0004SStefan Hajnoczi             goto fail;
1819c61d0004SStefan Hajnoczi         }
1820c61d0004SStefan Hajnoczi     }
1821c61d0004SStefan Hajnoczi 
1822585f8587Sbellard #ifdef DEBUG_ALLOC
18236cbc3031SPhilipp Hahn     {
18246cbc3031SPhilipp Hahn         BdrvCheckResult result = {0};
1825b35278f7SStefan Hajnoczi         qcow2_check_refcounts(bs, &result, 0);
18266cbc3031SPhilipp Hahn     }
1827585f8587Sbellard #endif
1828ceb029cdSVladimir Sementsov-Ogievskiy 
18296f13a316SVladimir Sementsov-Ogievskiy     qemu_co_queue_init(&s->thread_task_queue);
1830ceb029cdSVladimir Sementsov-Ogievskiy 
18316d85a57eSJes Sorensen     return ret;
1832585f8587Sbellard 
1833585f8587Sbellard  fail:
18349b890bdcSKevin Wolf     g_free(s->image_data_file);
18350e8c08beSKevin Wolf     if (has_data_file(bs)) {
18360e8c08beSKevin Wolf         bdrv_unref_child(bs, s->data_file);
1837808cf3cbSVladimir Sementsov-Ogievskiy         s->data_file = NULL;
18380e8c08beSKevin Wolf     }
18396744cbabSKevin Wolf     g_free(s->unknown_header_fields);
184075bab85cSKevin Wolf     cleanup_unknown_header_ext(bs);
1841ed6ccf0fSKevin Wolf     qcow2_free_snapshots(bs);
1842ed6ccf0fSKevin Wolf     qcow2_refcount_close(bs);
1843de82815dSKevin Wolf     qemu_vfree(s->l1_table);
1844cf93980eSMax Reitz     /* else pre-write overlap checks in cache_destroy may crash */
1845cf93980eSMax Reitz     s->l1_table = NULL;
1846279621c0SAlberto Garcia     cache_clean_timer_del(bs);
184729c1a730SKevin Wolf     if (s->l2_table_cache) {
1848e64d4072SAlberto Garcia         qcow2_cache_destroy(s->l2_table_cache);
184929c1a730SKevin Wolf     }
1850c5a33ee9SPrasad Joshi     if (s->refcount_block_cache) {
1851e64d4072SAlberto Garcia         qcow2_cache_destroy(s->refcount_block_cache);
1852c5a33ee9SPrasad Joshi     }
1853b25b387fSDaniel P. Berrange     qcrypto_block_free(s->crypto);
1854b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
18556d85a57eSJes Sorensen     return ret;
1856585f8587Sbellard }
1857585f8587Sbellard 
18581fafcd93SPaolo Bonzini typedef struct QCow2OpenCo {
18591fafcd93SPaolo Bonzini     BlockDriverState *bs;
18601fafcd93SPaolo Bonzini     QDict *options;
18611fafcd93SPaolo Bonzini     int flags;
18621fafcd93SPaolo Bonzini     Error **errp;
18631fafcd93SPaolo Bonzini     int ret;
18641fafcd93SPaolo Bonzini } QCow2OpenCo;
18651fafcd93SPaolo Bonzini 
18661fafcd93SPaolo Bonzini static void coroutine_fn qcow2_open_entry(void *opaque)
18671fafcd93SPaolo Bonzini {
18681fafcd93SPaolo Bonzini     QCow2OpenCo *qoc = opaque;
18691fafcd93SPaolo Bonzini     BDRVQcow2State *s = qoc->bs->opaque;
18701fafcd93SPaolo Bonzini 
18711fafcd93SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
18721fafcd93SPaolo Bonzini     qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
18731fafcd93SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
18741fafcd93SPaolo Bonzini }
18751fafcd93SPaolo Bonzini 
18764e4bf5c4SKevin Wolf static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
18774e4bf5c4SKevin Wolf                       Error **errp)
18784e4bf5c4SKevin Wolf {
18791fafcd93SPaolo Bonzini     BDRVQcow2State *s = bs->opaque;
18801fafcd93SPaolo Bonzini     QCow2OpenCo qoc = {
18811fafcd93SPaolo Bonzini         .bs = bs,
18821fafcd93SPaolo Bonzini         .options = options,
18831fafcd93SPaolo Bonzini         .flags = flags,
18841fafcd93SPaolo Bonzini         .errp = errp,
18851fafcd93SPaolo Bonzini         .ret = -EINPROGRESS
18861fafcd93SPaolo Bonzini     };
18871fafcd93SPaolo Bonzini 
18888b1869daSMax Reitz     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
18898b1869daSMax Reitz                                BDRV_CHILD_IMAGE, false, errp);
18904e4bf5c4SKevin Wolf     if (!bs->file) {
18914e4bf5c4SKevin Wolf         return -EINVAL;
18924e4bf5c4SKevin Wolf     }
18934e4bf5c4SKevin Wolf 
18941fafcd93SPaolo Bonzini     /* Initialise locks */
18951fafcd93SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
18961fafcd93SPaolo Bonzini 
18971fafcd93SPaolo Bonzini     if (qemu_in_coroutine()) {
18981fafcd93SPaolo Bonzini         /* From bdrv_co_create.  */
18991fafcd93SPaolo Bonzini         qcow2_open_entry(&qoc);
19001fafcd93SPaolo Bonzini     } else {
19014720cbeeSKevin Wolf         assert(qemu_get_current_aio_context() == qemu_get_aio_context());
19021fafcd93SPaolo Bonzini         qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc));
19031fafcd93SPaolo Bonzini         BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
19041fafcd93SPaolo Bonzini     }
19051fafcd93SPaolo Bonzini     return qoc.ret;
19064e4bf5c4SKevin Wolf }
19074e4bf5c4SKevin Wolf 
19083baca891SKevin Wolf static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1909d34682cdSKevin Wolf {
1910ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1911d34682cdSKevin Wolf 
1912a84178ccSEric Blake     if (bs->encrypted) {
1913a84178ccSEric Blake         /* Encryption works on a sector granularity */
19146f8f015cSAlberto Garcia         bs->bl.request_alignment = qcrypto_block_get_sector_size(s->crypto);
1915a84178ccSEric Blake     }
1916*a6841a2dSAlberto Garcia     bs->bl.pwrite_zeroes_alignment = s->subcluster_size;
1917ecdbead6SEric Blake     bs->bl.pdiscard_alignment = s->cluster_size;
1918d34682cdSKevin Wolf }
1919d34682cdSKevin Wolf 
192021d82ac9SJeff Cody static int qcow2_reopen_prepare(BDRVReopenState *state,
192121d82ac9SJeff Cody                                 BlockReopenQueue *queue, Error **errp)
192221d82ac9SJeff Cody {
19235b0959a7SKevin Wolf     Qcow2ReopenState *r;
19244c2e5f8fSKevin Wolf     int ret;
19254c2e5f8fSKevin Wolf 
19265b0959a7SKevin Wolf     r = g_new0(Qcow2ReopenState, 1);
19275b0959a7SKevin Wolf     state->opaque = r;
19285b0959a7SKevin Wolf 
19295b0959a7SKevin Wolf     ret = qcow2_update_options_prepare(state->bs, r, state->options,
19305b0959a7SKevin Wolf                                        state->flags, errp);
19315b0959a7SKevin Wolf     if (ret < 0) {
19325b0959a7SKevin Wolf         goto fail;
19335b0959a7SKevin Wolf     }
19345b0959a7SKevin Wolf 
19355b0959a7SKevin Wolf     /* We need to write out any unwritten data if we reopen read-only. */
19364c2e5f8fSKevin Wolf     if ((state->flags & BDRV_O_RDWR) == 0) {
1937169b8793SVladimir Sementsov-Ogievskiy         ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1938169b8793SVladimir Sementsov-Ogievskiy         if (ret < 0) {
1939169b8793SVladimir Sementsov-Ogievskiy             goto fail;
1940169b8793SVladimir Sementsov-Ogievskiy         }
1941169b8793SVladimir Sementsov-Ogievskiy 
19424c2e5f8fSKevin Wolf         ret = bdrv_flush(state->bs);
19434c2e5f8fSKevin Wolf         if (ret < 0) {
19445b0959a7SKevin Wolf             goto fail;
19454c2e5f8fSKevin Wolf         }
19464c2e5f8fSKevin Wolf 
19474c2e5f8fSKevin Wolf         ret = qcow2_mark_clean(state->bs);
19484c2e5f8fSKevin Wolf         if (ret < 0) {
19495b0959a7SKevin Wolf             goto fail;
19504c2e5f8fSKevin Wolf         }
19514c2e5f8fSKevin Wolf     }
19524c2e5f8fSKevin Wolf 
195321d82ac9SJeff Cody     return 0;
19545b0959a7SKevin Wolf 
19555b0959a7SKevin Wolf fail:
19565b0959a7SKevin Wolf     qcow2_update_options_abort(state->bs, r);
19575b0959a7SKevin Wolf     g_free(r);
19585b0959a7SKevin Wolf     return ret;
19595b0959a7SKevin Wolf }
19605b0959a7SKevin Wolf 
19615b0959a7SKevin Wolf static void qcow2_reopen_commit(BDRVReopenState *state)
19625b0959a7SKevin Wolf {
19635b0959a7SKevin Wolf     qcow2_update_options_commit(state->bs, state->opaque);
196465eb7c85SPeter Krempa     g_free(state->opaque);
196565eb7c85SPeter Krempa }
196665eb7c85SPeter Krempa 
196765eb7c85SPeter Krempa static void qcow2_reopen_commit_post(BDRVReopenState *state)
196865eb7c85SPeter Krempa {
19694dd09f62SVladimir Sementsov-Ogievskiy     if (state->flags & BDRV_O_RDWR) {
19704dd09f62SVladimir Sementsov-Ogievskiy         Error *local_err = NULL;
19714dd09f62SVladimir Sementsov-Ogievskiy 
19724dd09f62SVladimir Sementsov-Ogievskiy         if (qcow2_reopen_bitmaps_rw(state->bs, &local_err) < 0) {
19734dd09f62SVladimir Sementsov-Ogievskiy             /*
19744dd09f62SVladimir Sementsov-Ogievskiy              * This is not fatal, bitmaps just left read-only, so all following
19754dd09f62SVladimir Sementsov-Ogievskiy              * writes will fail. User can remove read-only bitmaps to unblock
19764dd09f62SVladimir Sementsov-Ogievskiy              * writes or retry reopen.
19774dd09f62SVladimir Sementsov-Ogievskiy              */
19784dd09f62SVladimir Sementsov-Ogievskiy             error_reportf_err(local_err,
19794dd09f62SVladimir Sementsov-Ogievskiy                               "%s: Failed to make dirty bitmaps writable: ",
19804dd09f62SVladimir Sementsov-Ogievskiy                               bdrv_get_node_name(state->bs));
19814dd09f62SVladimir Sementsov-Ogievskiy         }
19824dd09f62SVladimir Sementsov-Ogievskiy     }
19835b0959a7SKevin Wolf }
19845b0959a7SKevin Wolf 
19855b0959a7SKevin Wolf static void qcow2_reopen_abort(BDRVReopenState *state)
19865b0959a7SKevin Wolf {
19875b0959a7SKevin Wolf     qcow2_update_options_abort(state->bs, state->opaque);
19885b0959a7SKevin Wolf     g_free(state->opaque);
198921d82ac9SJeff Cody }
199021d82ac9SJeff Cody 
19915365f44dSKevin Wolf static void qcow2_join_options(QDict *options, QDict *old_options)
19925365f44dSKevin Wolf {
19935365f44dSKevin Wolf     bool has_new_overlap_template =
19945365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
19955365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
19965365f44dSKevin Wolf     bool has_new_total_cache_size =
19975365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
19985365f44dSKevin Wolf     bool has_all_cache_options;
19995365f44dSKevin Wolf 
20005365f44dSKevin Wolf     /* New overlap template overrides all old overlap options */
20015365f44dSKevin Wolf     if (has_new_overlap_template) {
20025365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP);
20035365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
20045365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
20055365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
20065365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
20075365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
20085365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
20095365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
20105365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
20115365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
20125365f44dSKevin Wolf     }
20135365f44dSKevin Wolf 
20145365f44dSKevin Wolf     /* New total cache size overrides all old options */
20155365f44dSKevin Wolf     if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
20165365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
20175365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
20185365f44dSKevin Wolf     }
20195365f44dSKevin Wolf 
20205365f44dSKevin Wolf     qdict_join(options, old_options, false);
20215365f44dSKevin Wolf 
20225365f44dSKevin Wolf     /*
20235365f44dSKevin Wolf      * If after merging all cache size options are set, an old total size is
20245365f44dSKevin Wolf      * overwritten. Do keep all options, however, if all three are new. The
20255365f44dSKevin Wolf      * resulting error message is what we want to happen.
20265365f44dSKevin Wolf      */
20275365f44dSKevin Wolf     has_all_cache_options =
20285365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
20295365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
20305365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
20315365f44dSKevin Wolf 
20325365f44dSKevin Wolf     if (has_all_cache_options && !has_new_total_cache_size) {
20335365f44dSKevin Wolf         qdict_del(options, QCOW2_OPT_CACHE_SIZE);
20345365f44dSKevin Wolf     }
20355365f44dSKevin Wolf }
20365365f44dSKevin Wolf 
2037a320fb04SEric Blake static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
2038a320fb04SEric Blake                                               bool want_zero,
2039a320fb04SEric Blake                                               int64_t offset, int64_t count,
2040a320fb04SEric Blake                                               int64_t *pnum, int64_t *map,
2041a320fb04SEric Blake                                               BlockDriverState **file)
2042585f8587Sbellard {
2043ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2044388e5816SAlberto Garcia     uint64_t host_offset;
2045ecfe1863SKevin Wolf     unsigned int bytes;
204610dabdc5SAlberto Garcia     QCow2SubclusterType type;
204774e60fb5SAlberto Garcia     int ret, status = 0;
2048585f8587Sbellard 
20495e978550SKevin Wolf     qemu_co_mutex_lock(&s->lock);
20505e978550SKevin Wolf 
205169f47505SVladimir Sementsov-Ogievskiy     if (!s->metadata_preallocation_checked) {
205269f47505SVladimir Sementsov-Ogievskiy         ret = qcow2_detect_metadata_preallocation(bs);
205369f47505SVladimir Sementsov-Ogievskiy         s->metadata_preallocation = (ret == 1);
205469f47505SVladimir Sementsov-Ogievskiy         s->metadata_preallocation_checked = true;
205569f47505SVladimir Sementsov-Ogievskiy     }
205669f47505SVladimir Sementsov-Ogievskiy 
2057a320fb04SEric Blake     bytes = MIN(INT_MAX, count);
2058ca4a0bb8SAlberto Garcia     ret = qcow2_get_host_offset(bs, offset, &bytes, &host_offset, &type);
2059f8a2e5e3SStefan Hajnoczi     qemu_co_mutex_unlock(&s->lock);
20601c46efaaSKevin Wolf     if (ret < 0) {
2061d663640cSPaolo Bonzini         return ret;
20621c46efaaSKevin Wolf     }
2063095a9c58Saliguori 
2064a320fb04SEric Blake     *pnum = bytes;
2065ecfe1863SKevin Wolf 
206610dabdc5SAlberto Garcia     if ((type == QCOW2_SUBCLUSTER_NORMAL ||
206797490a14SAlberto Garcia          type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
206897490a14SAlberto Garcia          type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) && !s->crypto) {
2069388e5816SAlberto Garcia         *map = host_offset;
207037be1403SKevin Wolf         *file = s->data_file->bs;
2071a320fb04SEric Blake         status |= BDRV_BLOCK_OFFSET_VALID;
20724bc74be9SPaolo Bonzini     }
207310dabdc5SAlberto Garcia     if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
207410dabdc5SAlberto Garcia         type == QCOW2_SUBCLUSTER_ZERO_ALLOC) {
20754bc74be9SPaolo Bonzini         status |= BDRV_BLOCK_ZERO;
207697490a14SAlberto Garcia     } else if (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN &&
207797490a14SAlberto Garcia                type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) {
20784bc74be9SPaolo Bonzini         status |= BDRV_BLOCK_DATA;
20794bc74be9SPaolo Bonzini     }
208069f47505SVladimir Sementsov-Ogievskiy     if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) &&
208169f47505SVladimir Sementsov-Ogievskiy         (status & BDRV_BLOCK_OFFSET_VALID))
208269f47505SVladimir Sementsov-Ogievskiy     {
208369f47505SVladimir Sementsov-Ogievskiy         status |= BDRV_BLOCK_RECURSE;
208469f47505SVladimir Sementsov-Ogievskiy     }
20854bc74be9SPaolo Bonzini     return status;
2086585f8587Sbellard }
2087585f8587Sbellard 
2088fd9fcd37SFam Zheng static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
2089fd9fcd37SFam Zheng                                             QCowL2Meta **pl2meta,
2090fd9fcd37SFam Zheng                                             bool link_l2)
2091fd9fcd37SFam Zheng {
2092fd9fcd37SFam Zheng     int ret = 0;
2093fd9fcd37SFam Zheng     QCowL2Meta *l2meta = *pl2meta;
2094fd9fcd37SFam Zheng 
2095fd9fcd37SFam Zheng     while (l2meta != NULL) {
2096fd9fcd37SFam Zheng         QCowL2Meta *next;
2097fd9fcd37SFam Zheng 
2098354d930dSFam Zheng         if (link_l2) {
2099fd9fcd37SFam Zheng             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
2100fd9fcd37SFam Zheng             if (ret) {
2101fd9fcd37SFam Zheng                 goto out;
2102fd9fcd37SFam Zheng             }
21038b24cd14SKevin Wolf         } else {
21048b24cd14SKevin Wolf             qcow2_alloc_cluster_abort(bs, l2meta);
2105fd9fcd37SFam Zheng         }
2106fd9fcd37SFam Zheng 
2107fd9fcd37SFam Zheng         /* Take the request off the list of running requests */
2108fd9fcd37SFam Zheng         if (l2meta->nb_clusters != 0) {
2109fd9fcd37SFam Zheng             QLIST_REMOVE(l2meta, next_in_flight);
2110fd9fcd37SFam Zheng         }
2111fd9fcd37SFam Zheng 
2112fd9fcd37SFam Zheng         qemu_co_queue_restart_all(&l2meta->dependent_requests);
2113fd9fcd37SFam Zheng 
2114fd9fcd37SFam Zheng         next = l2meta->next;
2115fd9fcd37SFam Zheng         g_free(l2meta);
2116fd9fcd37SFam Zheng         l2meta = next;
2117fd9fcd37SFam Zheng     }
2118fd9fcd37SFam Zheng out:
2119fd9fcd37SFam Zheng     *pl2meta = l2meta;
2120fd9fcd37SFam Zheng     return ret;
2121fd9fcd37SFam Zheng }
2122fd9fcd37SFam Zheng 
212388f468e5SVladimir Sementsov-Ogievskiy static coroutine_fn int
212488f468e5SVladimir Sementsov-Ogievskiy qcow2_co_preadv_encrypted(BlockDriverState *bs,
21259c4269d5SAlberto Garcia                            uint64_t host_offset,
212688f468e5SVladimir Sementsov-Ogievskiy                            uint64_t offset,
212788f468e5SVladimir Sementsov-Ogievskiy                            uint64_t bytes,
212888f468e5SVladimir Sementsov-Ogievskiy                            QEMUIOVector *qiov,
212988f468e5SVladimir Sementsov-Ogievskiy                            uint64_t qiov_offset)
213088f468e5SVladimir Sementsov-Ogievskiy {
213188f468e5SVladimir Sementsov-Ogievskiy     int ret;
213288f468e5SVladimir Sementsov-Ogievskiy     BDRVQcow2State *s = bs->opaque;
213388f468e5SVladimir Sementsov-Ogievskiy     uint8_t *buf;
213488f468e5SVladimir Sementsov-Ogievskiy 
213588f468e5SVladimir Sementsov-Ogievskiy     assert(bs->encrypted && s->crypto);
213688f468e5SVladimir Sementsov-Ogievskiy     assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
213788f468e5SVladimir Sementsov-Ogievskiy 
213888f468e5SVladimir Sementsov-Ogievskiy     /*
213988f468e5SVladimir Sementsov-Ogievskiy      * For encrypted images, read everything into a temporary
214088f468e5SVladimir Sementsov-Ogievskiy      * contiguous buffer on which the AES functions can work.
214188f468e5SVladimir Sementsov-Ogievskiy      * Also, decryption in a separate buffer is better as it
214288f468e5SVladimir Sementsov-Ogievskiy      * prevents the guest from learning information about the
214388f468e5SVladimir Sementsov-Ogievskiy      * encrypted nature of the virtual disk.
214488f468e5SVladimir Sementsov-Ogievskiy      */
214588f468e5SVladimir Sementsov-Ogievskiy 
214688f468e5SVladimir Sementsov-Ogievskiy     buf = qemu_try_blockalign(s->data_file->bs, bytes);
214788f468e5SVladimir Sementsov-Ogievskiy     if (buf == NULL) {
214888f468e5SVladimir Sementsov-Ogievskiy         return -ENOMEM;
214988f468e5SVladimir Sementsov-Ogievskiy     }
215088f468e5SVladimir Sementsov-Ogievskiy 
215188f468e5SVladimir Sementsov-Ogievskiy     BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
21529c4269d5SAlberto Garcia     ret = bdrv_co_pread(s->data_file, host_offset, bytes, buf, 0);
215388f468e5SVladimir Sementsov-Ogievskiy     if (ret < 0) {
215488f468e5SVladimir Sementsov-Ogievskiy         goto fail;
215588f468e5SVladimir Sementsov-Ogievskiy     }
215688f468e5SVladimir Sementsov-Ogievskiy 
21579c4269d5SAlberto Garcia     if (qcow2_co_decrypt(bs, host_offset, offset, buf, bytes) < 0)
215888f468e5SVladimir Sementsov-Ogievskiy     {
215988f468e5SVladimir Sementsov-Ogievskiy         ret = -EIO;
216088f468e5SVladimir Sementsov-Ogievskiy         goto fail;
216188f468e5SVladimir Sementsov-Ogievskiy     }
216288f468e5SVladimir Sementsov-Ogievskiy     qemu_iovec_from_buf(qiov, qiov_offset, buf, bytes);
216388f468e5SVladimir Sementsov-Ogievskiy 
216488f468e5SVladimir Sementsov-Ogievskiy fail:
216588f468e5SVladimir Sementsov-Ogievskiy     qemu_vfree(buf);
216688f468e5SVladimir Sementsov-Ogievskiy 
216788f468e5SVladimir Sementsov-Ogievskiy     return ret;
216888f468e5SVladimir Sementsov-Ogievskiy }
216988f468e5SVladimir Sementsov-Ogievskiy 
2170d710cf57SVladimir Sementsov-Ogievskiy typedef struct Qcow2AioTask {
2171d710cf57SVladimir Sementsov-Ogievskiy     AioTask task;
2172d710cf57SVladimir Sementsov-Ogievskiy 
2173d710cf57SVladimir Sementsov-Ogievskiy     BlockDriverState *bs;
217410dabdc5SAlberto Garcia     QCow2SubclusterType subcluster_type; /* only for read */
21759c4269d5SAlberto Garcia     uint64_t host_offset; /* or full descriptor in compressed clusters */
2176d710cf57SVladimir Sementsov-Ogievskiy     uint64_t offset;
2177d710cf57SVladimir Sementsov-Ogievskiy     uint64_t bytes;
2178d710cf57SVladimir Sementsov-Ogievskiy     QEMUIOVector *qiov;
2179d710cf57SVladimir Sementsov-Ogievskiy     uint64_t qiov_offset;
2180d710cf57SVladimir Sementsov-Ogievskiy     QCowL2Meta *l2meta; /* only for write */
2181d710cf57SVladimir Sementsov-Ogievskiy } Qcow2AioTask;
2182d710cf57SVladimir Sementsov-Ogievskiy 
2183d710cf57SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task);
2184d710cf57SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_add_task(BlockDriverState *bs,
2185d710cf57SVladimir Sementsov-Ogievskiy                                        AioTaskPool *pool,
2186d710cf57SVladimir Sementsov-Ogievskiy                                        AioTaskFunc func,
218710dabdc5SAlberto Garcia                                        QCow2SubclusterType subcluster_type,
21889c4269d5SAlberto Garcia                                        uint64_t host_offset,
2189d710cf57SVladimir Sementsov-Ogievskiy                                        uint64_t offset,
2190d710cf57SVladimir Sementsov-Ogievskiy                                        uint64_t bytes,
2191d710cf57SVladimir Sementsov-Ogievskiy                                        QEMUIOVector *qiov,
2192d710cf57SVladimir Sementsov-Ogievskiy                                        size_t qiov_offset,
2193d710cf57SVladimir Sementsov-Ogievskiy                                        QCowL2Meta *l2meta)
2194d710cf57SVladimir Sementsov-Ogievskiy {
2195d710cf57SVladimir Sementsov-Ogievskiy     Qcow2AioTask local_task;
2196d710cf57SVladimir Sementsov-Ogievskiy     Qcow2AioTask *task = pool ? g_new(Qcow2AioTask, 1) : &local_task;
2197d710cf57SVladimir Sementsov-Ogievskiy 
2198d710cf57SVladimir Sementsov-Ogievskiy     *task = (Qcow2AioTask) {
2199d710cf57SVladimir Sementsov-Ogievskiy         .task.func = func,
2200d710cf57SVladimir Sementsov-Ogievskiy         .bs = bs,
220110dabdc5SAlberto Garcia         .subcluster_type = subcluster_type,
2202d710cf57SVladimir Sementsov-Ogievskiy         .qiov = qiov,
22039c4269d5SAlberto Garcia         .host_offset = host_offset,
2204d710cf57SVladimir Sementsov-Ogievskiy         .offset = offset,
2205d710cf57SVladimir Sementsov-Ogievskiy         .bytes = bytes,
2206d710cf57SVladimir Sementsov-Ogievskiy         .qiov_offset = qiov_offset,
2207d710cf57SVladimir Sementsov-Ogievskiy         .l2meta = l2meta,
2208d710cf57SVladimir Sementsov-Ogievskiy     };
2209d710cf57SVladimir Sementsov-Ogievskiy 
2210d710cf57SVladimir Sementsov-Ogievskiy     trace_qcow2_add_task(qemu_coroutine_self(), bs, pool,
2211d710cf57SVladimir Sementsov-Ogievskiy                          func == qcow2_co_preadv_task_entry ? "read" : "write",
221210dabdc5SAlberto Garcia                          subcluster_type, host_offset, offset, bytes,
2213d710cf57SVladimir Sementsov-Ogievskiy                          qiov, qiov_offset);
2214d710cf57SVladimir Sementsov-Ogievskiy 
2215d710cf57SVladimir Sementsov-Ogievskiy     if (!pool) {
2216d710cf57SVladimir Sementsov-Ogievskiy         return func(&task->task);
2217d710cf57SVladimir Sementsov-Ogievskiy     }
2218d710cf57SVladimir Sementsov-Ogievskiy 
2219d710cf57SVladimir Sementsov-Ogievskiy     aio_task_pool_start_task(pool, &task->task);
2220d710cf57SVladimir Sementsov-Ogievskiy 
2221d710cf57SVladimir Sementsov-Ogievskiy     return 0;
2222d710cf57SVladimir Sementsov-Ogievskiy }
2223d710cf57SVladimir Sementsov-Ogievskiy 
222488f468e5SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
222510dabdc5SAlberto Garcia                                              QCow2SubclusterType subc_type,
22269c4269d5SAlberto Garcia                                              uint64_t host_offset,
222788f468e5SVladimir Sementsov-Ogievskiy                                              uint64_t offset, uint64_t bytes,
222888f468e5SVladimir Sementsov-Ogievskiy                                              QEMUIOVector *qiov,
222988f468e5SVladimir Sementsov-Ogievskiy                                              size_t qiov_offset)
223088f468e5SVladimir Sementsov-Ogievskiy {
223188f468e5SVladimir Sementsov-Ogievskiy     BDRVQcow2State *s = bs->opaque;
223288f468e5SVladimir Sementsov-Ogievskiy 
223310dabdc5SAlberto Garcia     switch (subc_type) {
223410dabdc5SAlberto Garcia     case QCOW2_SUBCLUSTER_ZERO_PLAIN:
223510dabdc5SAlberto Garcia     case QCOW2_SUBCLUSTER_ZERO_ALLOC:
223688f468e5SVladimir Sementsov-Ogievskiy         /* Both zero types are handled in qcow2_co_preadv_part */
223788f468e5SVladimir Sementsov-Ogievskiy         g_assert_not_reached();
223888f468e5SVladimir Sementsov-Ogievskiy 
223910dabdc5SAlberto Garcia     case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
224097490a14SAlberto Garcia     case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
224188f468e5SVladimir Sementsov-Ogievskiy         assert(bs->backing); /* otherwise handled in qcow2_co_preadv_part */
224288f468e5SVladimir Sementsov-Ogievskiy 
224388f468e5SVladimir Sementsov-Ogievskiy         BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
224488f468e5SVladimir Sementsov-Ogievskiy         return bdrv_co_preadv_part(bs->backing, offset, bytes,
224588f468e5SVladimir Sementsov-Ogievskiy                                    qiov, qiov_offset, 0);
224688f468e5SVladimir Sementsov-Ogievskiy 
224710dabdc5SAlberto Garcia     case QCOW2_SUBCLUSTER_COMPRESSED:
22489c4269d5SAlberto Garcia         return qcow2_co_preadv_compressed(bs, host_offset,
224988f468e5SVladimir Sementsov-Ogievskiy                                           offset, bytes, qiov, qiov_offset);
225088f468e5SVladimir Sementsov-Ogievskiy 
225110dabdc5SAlberto Garcia     case QCOW2_SUBCLUSTER_NORMAL:
225288f468e5SVladimir Sementsov-Ogievskiy         if (bs->encrypted) {
22539c4269d5SAlberto Garcia             return qcow2_co_preadv_encrypted(bs, host_offset,
225488f468e5SVladimir Sementsov-Ogievskiy                                              offset, bytes, qiov, qiov_offset);
225588f468e5SVladimir Sementsov-Ogievskiy         }
225688f468e5SVladimir Sementsov-Ogievskiy 
225788f468e5SVladimir Sementsov-Ogievskiy         BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
22589c4269d5SAlberto Garcia         return bdrv_co_preadv_part(s->data_file, host_offset,
225988f468e5SVladimir Sementsov-Ogievskiy                                    bytes, qiov, qiov_offset, 0);
226088f468e5SVladimir Sementsov-Ogievskiy 
226188f468e5SVladimir Sementsov-Ogievskiy     default:
226288f468e5SVladimir Sementsov-Ogievskiy         g_assert_not_reached();
226388f468e5SVladimir Sementsov-Ogievskiy     }
226488f468e5SVladimir Sementsov-Ogievskiy 
226588f468e5SVladimir Sementsov-Ogievskiy     g_assert_not_reached();
226688f468e5SVladimir Sementsov-Ogievskiy }
226788f468e5SVladimir Sementsov-Ogievskiy 
2268d710cf57SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task)
2269d710cf57SVladimir Sementsov-Ogievskiy {
2270d710cf57SVladimir Sementsov-Ogievskiy     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
2271d710cf57SVladimir Sementsov-Ogievskiy 
2272d710cf57SVladimir Sementsov-Ogievskiy     assert(!t->l2meta);
2273d710cf57SVladimir Sementsov-Ogievskiy 
227410dabdc5SAlberto Garcia     return qcow2_co_preadv_task(t->bs, t->subcluster_type,
227510dabdc5SAlberto Garcia                                 t->host_offset, t->offset, t->bytes,
227610dabdc5SAlberto Garcia                                 t->qiov, t->qiov_offset);
2277d710cf57SVladimir Sementsov-Ogievskiy }
2278d710cf57SVladimir Sementsov-Ogievskiy 
2279df893d25SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs,
2280df893d25SVladimir Sementsov-Ogievskiy                                              uint64_t offset, uint64_t bytes,
2281df893d25SVladimir Sementsov-Ogievskiy                                              QEMUIOVector *qiov,
2282df893d25SVladimir Sementsov-Ogievskiy                                              size_t qiov_offset, int flags)
22831490791fSaliguori {
2284ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2285d710cf57SVladimir Sementsov-Ogievskiy     int ret = 0;
2286ecfe1863SKevin Wolf     unsigned int cur_bytes; /* number of bytes in current iteration */
2287388e5816SAlberto Garcia     uint64_t host_offset = 0;
228810dabdc5SAlberto Garcia     QCow2SubclusterType type;
2289d710cf57SVladimir Sementsov-Ogievskiy     AioTaskPool *aio = NULL;
2290585f8587Sbellard 
2291d710cf57SVladimir Sementsov-Ogievskiy     while (bytes != 0 && aio_task_pool_status(aio) == 0) {
2292faf575c1SFrediano Ziglio         /* prepare next request */
2293ecfe1863SKevin Wolf         cur_bytes = MIN(bytes, INT_MAX);
2294b25b387fSDaniel P. Berrange         if (s->crypto) {
2295ecfe1863SKevin Wolf             cur_bytes = MIN(cur_bytes,
2296ecfe1863SKevin Wolf                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2297bd28f835SKevin Wolf         }
2298bd28f835SKevin Wolf 
2299f24196d3SVladimir Sementsov-Ogievskiy         qemu_co_mutex_lock(&s->lock);
2300ca4a0bb8SAlberto Garcia         ret = qcow2_get_host_offset(bs, offset, &cur_bytes,
2301ca4a0bb8SAlberto Garcia                                     &host_offset, &type);
2302f24196d3SVladimir Sementsov-Ogievskiy         qemu_co_mutex_unlock(&s->lock);
23031c46efaaSKevin Wolf         if (ret < 0) {
2304d710cf57SVladimir Sementsov-Ogievskiy             goto out;
23051c46efaaSKevin Wolf         }
23061c46efaaSKevin Wolf 
230710dabdc5SAlberto Garcia         if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
230810dabdc5SAlberto Garcia             type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
230997490a14SAlberto Garcia             (type == QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && !bs->backing) ||
231097490a14SAlberto Garcia             (type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC && !bs->backing))
231188f468e5SVladimir Sementsov-Ogievskiy         {
231288f468e5SVladimir Sementsov-Ogievskiy             qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes);
2313a9465922Sbellard         } else {
2314d710cf57SVladimir Sementsov-Ogievskiy             if (!aio && cur_bytes != bytes) {
2315d710cf57SVladimir Sementsov-Ogievskiy                 aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
2316d710cf57SVladimir Sementsov-Ogievskiy             }
2317ca4a0bb8SAlberto Garcia             ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, type,
23189c4269d5SAlberto Garcia                                  host_offset, offset, cur_bytes,
2319d710cf57SVladimir Sementsov-Ogievskiy                                  qiov, qiov_offset, NULL);
23208af36488SKevin Wolf             if (ret < 0) {
2321d710cf57SVladimir Sementsov-Ogievskiy                 goto out;
23228af36488SKevin Wolf             }
2323faf575c1SFrediano Ziglio         }
2324faf575c1SFrediano Ziglio 
2325ecfe1863SKevin Wolf         bytes -= cur_bytes;
2326ecfe1863SKevin Wolf         offset += cur_bytes;
2327df893d25SVladimir Sementsov-Ogievskiy         qiov_offset += cur_bytes;
23285ebaa27eSFrediano Ziglio     }
2329f141eafeSaliguori 
2330d710cf57SVladimir Sementsov-Ogievskiy out:
2331d710cf57SVladimir Sementsov-Ogievskiy     if (aio) {
2332d710cf57SVladimir Sementsov-Ogievskiy         aio_task_pool_wait_all(aio);
2333d710cf57SVladimir Sementsov-Ogievskiy         if (ret == 0) {
2334d710cf57SVladimir Sementsov-Ogievskiy             ret = aio_task_pool_status(aio);
2335d710cf57SVladimir Sementsov-Ogievskiy         }
2336d710cf57SVladimir Sementsov-Ogievskiy         g_free(aio);
2337d710cf57SVladimir Sementsov-Ogievskiy     }
2338d710cf57SVladimir Sementsov-Ogievskiy 
2339d710cf57SVladimir Sementsov-Ogievskiy     return ret;
2340585f8587Sbellard }
2341585f8587Sbellard 
2342ee22a9d8SAlberto Garcia /* Check if it's possible to merge a write request with the writing of
2343ee22a9d8SAlberto Garcia  * the data from the COW regions */
2344ee22a9d8SAlberto Garcia static bool merge_cow(uint64_t offset, unsigned bytes,
23455396234bSVladimir Sementsov-Ogievskiy                       QEMUIOVector *qiov, size_t qiov_offset,
23465396234bSVladimir Sementsov-Ogievskiy                       QCowL2Meta *l2meta)
2347ee22a9d8SAlberto Garcia {
2348ee22a9d8SAlberto Garcia     QCowL2Meta *m;
2349ee22a9d8SAlberto Garcia 
2350ee22a9d8SAlberto Garcia     for (m = l2meta; m != NULL; m = m->next) {
2351ee22a9d8SAlberto Garcia         /* If both COW regions are empty then there's nothing to merge */
2352ee22a9d8SAlberto Garcia         if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
2353ee22a9d8SAlberto Garcia             continue;
2354ee22a9d8SAlberto Garcia         }
2355ee22a9d8SAlberto Garcia 
2356c8bb23cbSAnton Nefedov         /* If COW regions are handled already, skip this too */
2357c8bb23cbSAnton Nefedov         if (m->skip_cow) {
2358c8bb23cbSAnton Nefedov             continue;
2359c8bb23cbSAnton Nefedov         }
2360c8bb23cbSAnton Nefedov 
2361ee22a9d8SAlberto Garcia         /* The data (middle) region must be immediately after the
2362ee22a9d8SAlberto Garcia          * start region */
2363ee22a9d8SAlberto Garcia         if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
2364ee22a9d8SAlberto Garcia             continue;
2365ee22a9d8SAlberto Garcia         }
2366ee22a9d8SAlberto Garcia 
2367ee22a9d8SAlberto Garcia         /* The end region must be immediately after the data (middle)
2368ee22a9d8SAlberto Garcia          * region */
2369ee22a9d8SAlberto Garcia         if (m->offset + m->cow_end.offset != offset + bytes) {
2370ee22a9d8SAlberto Garcia             continue;
2371ee22a9d8SAlberto Garcia         }
2372ee22a9d8SAlberto Garcia 
2373ee22a9d8SAlberto Garcia         /* Make sure that adding both COW regions to the QEMUIOVector
2374ee22a9d8SAlberto Garcia          * does not exceed IOV_MAX */
23755396234bSVladimir Sementsov-Ogievskiy         if (qemu_iovec_subvec_niov(qiov, qiov_offset, bytes) > IOV_MAX - 2) {
2376ee22a9d8SAlberto Garcia             continue;
2377ee22a9d8SAlberto Garcia         }
2378ee22a9d8SAlberto Garcia 
23795396234bSVladimir Sementsov-Ogievskiy         m->data_qiov = qiov;
23805396234bSVladimir Sementsov-Ogievskiy         m->data_qiov_offset = qiov_offset;
2381ee22a9d8SAlberto Garcia         return true;
2382ee22a9d8SAlberto Garcia     }
2383ee22a9d8SAlberto Garcia 
2384ee22a9d8SAlberto Garcia     return false;
2385ee22a9d8SAlberto Garcia }
2386ee22a9d8SAlberto Garcia 
2387c8bb23cbSAnton Nefedov static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes)
2388c8bb23cbSAnton Nefedov {
2389c8bb23cbSAnton Nefedov     int64_t nr;
2390c8bb23cbSAnton Nefedov     return !bytes ||
2391170d3bd3SAndrey Shinkevich         (!bdrv_is_allocated_above(bs, NULL, false, offset, bytes, &nr) &&
2392170d3bd3SAndrey Shinkevich          nr == bytes);
2393c8bb23cbSAnton Nefedov }
2394c8bb23cbSAnton Nefedov 
2395c8bb23cbSAnton Nefedov static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
2396c8bb23cbSAnton Nefedov {
2397c8bb23cbSAnton Nefedov     /*
2398c8bb23cbSAnton Nefedov      * This check is designed for optimization shortcut so it must be
2399c8bb23cbSAnton Nefedov      * efficient.
2400c8bb23cbSAnton Nefedov      * Instead of is_zero(), use is_unallocated() as it is faster (but not
2401c8bb23cbSAnton Nefedov      * as accurate and can result in false negatives).
2402c8bb23cbSAnton Nefedov      */
2403c8bb23cbSAnton Nefedov     return is_unallocated(bs, m->offset + m->cow_start.offset,
2404c8bb23cbSAnton Nefedov                           m->cow_start.nb_bytes) &&
2405c8bb23cbSAnton Nefedov            is_unallocated(bs, m->offset + m->cow_end.offset,
2406c8bb23cbSAnton Nefedov                           m->cow_end.nb_bytes);
2407c8bb23cbSAnton Nefedov }
2408c8bb23cbSAnton Nefedov 
2409c8bb23cbSAnton Nefedov static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)
2410c8bb23cbSAnton Nefedov {
2411c8bb23cbSAnton Nefedov     BDRVQcow2State *s = bs->opaque;
2412c8bb23cbSAnton Nefedov     QCowL2Meta *m;
2413c8bb23cbSAnton Nefedov 
2414c8bb23cbSAnton Nefedov     if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) {
2415c8bb23cbSAnton Nefedov         return 0;
2416c8bb23cbSAnton Nefedov     }
2417c8bb23cbSAnton Nefedov 
2418c8bb23cbSAnton Nefedov     if (bs->encrypted) {
2419c8bb23cbSAnton Nefedov         return 0;
2420c8bb23cbSAnton Nefedov     }
2421c8bb23cbSAnton Nefedov 
2422c8bb23cbSAnton Nefedov     for (m = l2meta; m != NULL; m = m->next) {
2423c8bb23cbSAnton Nefedov         int ret;
2424bf4a66eeSAlberto Garcia         uint64_t start_offset = m->alloc_offset + m->cow_start.offset;
2425bf4a66eeSAlberto Garcia         unsigned nb_bytes = m->cow_end.offset + m->cow_end.nb_bytes -
2426bf4a66eeSAlberto Garcia             m->cow_start.offset;
2427c8bb23cbSAnton Nefedov 
2428c8bb23cbSAnton Nefedov         if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) {
2429c8bb23cbSAnton Nefedov             continue;
2430c8bb23cbSAnton Nefedov         }
2431c8bb23cbSAnton Nefedov 
2432c8bb23cbSAnton Nefedov         if (!is_zero_cow(bs, m)) {
2433c8bb23cbSAnton Nefedov             continue;
2434c8bb23cbSAnton Nefedov         }
2435c8bb23cbSAnton Nefedov 
2436c8bb23cbSAnton Nefedov         /*
2437c8bb23cbSAnton Nefedov          * instead of writing zero COW buffers,
2438c8bb23cbSAnton Nefedov          * efficiently zero out the whole clusters
2439c8bb23cbSAnton Nefedov          */
2440c8bb23cbSAnton Nefedov 
2441bf4a66eeSAlberto Garcia         ret = qcow2_pre_write_overlap_check(bs, 0, start_offset, nb_bytes,
2442c8bb23cbSAnton Nefedov                                             true);
2443c8bb23cbSAnton Nefedov         if (ret < 0) {
2444c8bb23cbSAnton Nefedov             return ret;
2445c8bb23cbSAnton Nefedov         }
2446c8bb23cbSAnton Nefedov 
2447c8bb23cbSAnton Nefedov         BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
2448bf4a66eeSAlberto Garcia         ret = bdrv_co_pwrite_zeroes(s->data_file, start_offset, nb_bytes,
2449c8bb23cbSAnton Nefedov                                     BDRV_REQ_NO_FALLBACK);
2450c8bb23cbSAnton Nefedov         if (ret < 0) {
2451c8bb23cbSAnton Nefedov             if (ret != -ENOTSUP && ret != -EAGAIN) {
2452c8bb23cbSAnton Nefedov                 return ret;
2453c8bb23cbSAnton Nefedov             }
2454c8bb23cbSAnton Nefedov             continue;
2455c8bb23cbSAnton Nefedov         }
2456c8bb23cbSAnton Nefedov 
2457c8bb23cbSAnton Nefedov         trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters);
2458c8bb23cbSAnton Nefedov         m->skip_cow = true;
2459c8bb23cbSAnton Nefedov     }
2460c8bb23cbSAnton Nefedov     return 0;
2461c8bb23cbSAnton Nefedov }
2462c8bb23cbSAnton Nefedov 
24636aa7a263SVladimir Sementsov-Ogievskiy /*
24646aa7a263SVladimir Sementsov-Ogievskiy  * qcow2_co_pwritev_task
24656aa7a263SVladimir Sementsov-Ogievskiy  * Called with s->lock unlocked
24666aa7a263SVladimir Sementsov-Ogievskiy  * l2meta  - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must
24676aa7a263SVladimir Sementsov-Ogievskiy  *           not use it somehow after qcow2_co_pwritev_task() call
24686aa7a263SVladimir Sementsov-Ogievskiy  */
24696aa7a263SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_pwritev_task(BlockDriverState *bs,
24709c4269d5SAlberto Garcia                                               uint64_t host_offset,
24716aa7a263SVladimir Sementsov-Ogievskiy                                               uint64_t offset, uint64_t bytes,
24726aa7a263SVladimir Sementsov-Ogievskiy                                               QEMUIOVector *qiov,
24736aa7a263SVladimir Sementsov-Ogievskiy                                               uint64_t qiov_offset,
24746aa7a263SVladimir Sementsov-Ogievskiy                                               QCowL2Meta *l2meta)
24756aa7a263SVladimir Sementsov-Ogievskiy {
24766aa7a263SVladimir Sementsov-Ogievskiy     int ret;
24776aa7a263SVladimir Sementsov-Ogievskiy     BDRVQcow2State *s = bs->opaque;
24786aa7a263SVladimir Sementsov-Ogievskiy     void *crypt_buf = NULL;
24796aa7a263SVladimir Sementsov-Ogievskiy     QEMUIOVector encrypted_qiov;
24806aa7a263SVladimir Sementsov-Ogievskiy 
24816aa7a263SVladimir Sementsov-Ogievskiy     if (bs->encrypted) {
24826aa7a263SVladimir Sementsov-Ogievskiy         assert(s->crypto);
24836aa7a263SVladimir Sementsov-Ogievskiy         assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
24846aa7a263SVladimir Sementsov-Ogievskiy         crypt_buf = qemu_try_blockalign(bs->file->bs, bytes);
24856aa7a263SVladimir Sementsov-Ogievskiy         if (crypt_buf == NULL) {
24866aa7a263SVladimir Sementsov-Ogievskiy             ret = -ENOMEM;
24876aa7a263SVladimir Sementsov-Ogievskiy             goto out_unlocked;
24886aa7a263SVladimir Sementsov-Ogievskiy         }
24896aa7a263SVladimir Sementsov-Ogievskiy         qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes);
24906aa7a263SVladimir Sementsov-Ogievskiy 
24919c4269d5SAlberto Garcia         if (qcow2_co_encrypt(bs, host_offset, offset, crypt_buf, bytes) < 0) {
24926aa7a263SVladimir Sementsov-Ogievskiy             ret = -EIO;
24936aa7a263SVladimir Sementsov-Ogievskiy             goto out_unlocked;
24946aa7a263SVladimir Sementsov-Ogievskiy         }
24956aa7a263SVladimir Sementsov-Ogievskiy 
24966aa7a263SVladimir Sementsov-Ogievskiy         qemu_iovec_init_buf(&encrypted_qiov, crypt_buf, bytes);
24976aa7a263SVladimir Sementsov-Ogievskiy         qiov = &encrypted_qiov;
24986aa7a263SVladimir Sementsov-Ogievskiy         qiov_offset = 0;
24996aa7a263SVladimir Sementsov-Ogievskiy     }
25006aa7a263SVladimir Sementsov-Ogievskiy 
25016aa7a263SVladimir Sementsov-Ogievskiy     /* Try to efficiently initialize the physical space with zeroes */
25026aa7a263SVladimir Sementsov-Ogievskiy     ret = handle_alloc_space(bs, l2meta);
25036aa7a263SVladimir Sementsov-Ogievskiy     if (ret < 0) {
25046aa7a263SVladimir Sementsov-Ogievskiy         goto out_unlocked;
25056aa7a263SVladimir Sementsov-Ogievskiy     }
25066aa7a263SVladimir Sementsov-Ogievskiy 
25076aa7a263SVladimir Sementsov-Ogievskiy     /*
25086aa7a263SVladimir Sementsov-Ogievskiy      * If we need to do COW, check if it's possible to merge the
25096aa7a263SVladimir Sementsov-Ogievskiy      * writing of the guest data together with that of the COW regions.
25106aa7a263SVladimir Sementsov-Ogievskiy      * If it's not possible (or not necessary) then write the
25116aa7a263SVladimir Sementsov-Ogievskiy      * guest data now.
25126aa7a263SVladimir Sementsov-Ogievskiy      */
25136aa7a263SVladimir Sementsov-Ogievskiy     if (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) {
25146aa7a263SVladimir Sementsov-Ogievskiy         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
25159c4269d5SAlberto Garcia         trace_qcow2_writev_data(qemu_coroutine_self(), host_offset);
25169c4269d5SAlberto Garcia         ret = bdrv_co_pwritev_part(s->data_file, host_offset,
25176aa7a263SVladimir Sementsov-Ogievskiy                                    bytes, qiov, qiov_offset, 0);
25186aa7a263SVladimir Sementsov-Ogievskiy         if (ret < 0) {
25196aa7a263SVladimir Sementsov-Ogievskiy             goto out_unlocked;
25206aa7a263SVladimir Sementsov-Ogievskiy         }
25216aa7a263SVladimir Sementsov-Ogievskiy     }
25226aa7a263SVladimir Sementsov-Ogievskiy 
25236aa7a263SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->lock);
25246aa7a263SVladimir Sementsov-Ogievskiy 
25256aa7a263SVladimir Sementsov-Ogievskiy     ret = qcow2_handle_l2meta(bs, &l2meta, true);
25266aa7a263SVladimir Sementsov-Ogievskiy     goto out_locked;
25276aa7a263SVladimir Sementsov-Ogievskiy 
25286aa7a263SVladimir Sementsov-Ogievskiy out_unlocked:
25296aa7a263SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->lock);
25306aa7a263SVladimir Sementsov-Ogievskiy 
25316aa7a263SVladimir Sementsov-Ogievskiy out_locked:
25326aa7a263SVladimir Sementsov-Ogievskiy     qcow2_handle_l2meta(bs, &l2meta, false);
25336aa7a263SVladimir Sementsov-Ogievskiy     qemu_co_mutex_unlock(&s->lock);
25346aa7a263SVladimir Sementsov-Ogievskiy 
25356aa7a263SVladimir Sementsov-Ogievskiy     qemu_vfree(crypt_buf);
25366aa7a263SVladimir Sementsov-Ogievskiy 
25376aa7a263SVladimir Sementsov-Ogievskiy     return ret;
25386aa7a263SVladimir Sementsov-Ogievskiy }
25396aa7a263SVladimir Sementsov-Ogievskiy 
2540d710cf57SVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_pwritev_task_entry(AioTask *task)
2541d710cf57SVladimir Sementsov-Ogievskiy {
2542d710cf57SVladimir Sementsov-Ogievskiy     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
2543d710cf57SVladimir Sementsov-Ogievskiy 
254410dabdc5SAlberto Garcia     assert(!t->subcluster_type);
2545d710cf57SVladimir Sementsov-Ogievskiy 
25469c4269d5SAlberto Garcia     return qcow2_co_pwritev_task(t->bs, t->host_offset,
2547d710cf57SVladimir Sementsov-Ogievskiy                                  t->offset, t->bytes, t->qiov, t->qiov_offset,
2548d710cf57SVladimir Sementsov-Ogievskiy                                  t->l2meta);
2549d710cf57SVladimir Sementsov-Ogievskiy }
2550d710cf57SVladimir Sementsov-Ogievskiy 
25515396234bSVladimir Sementsov-Ogievskiy static coroutine_fn int qcow2_co_pwritev_part(
25525396234bSVladimir Sementsov-Ogievskiy         BlockDriverState *bs, uint64_t offset, uint64_t bytes,
25535396234bSVladimir Sementsov-Ogievskiy         QEMUIOVector *qiov, size_t qiov_offset, int flags)
2554585f8587Sbellard {
2555ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2556d46a0bb2SKevin Wolf     int offset_in_cluster;
255768d100e9SKevin Wolf     int ret;
2558d46a0bb2SKevin Wolf     unsigned int cur_bytes; /* number of sectors in current iteration */
2559c2bdd990SFrediano Ziglio     uint64_t cluster_offset;
25608d2497c3SKevin Wolf     QCowL2Meta *l2meta = NULL;
2561d710cf57SVladimir Sementsov-Ogievskiy     AioTaskPool *aio = NULL;
2562c2271403SFrediano Ziglio 
2563d46a0bb2SKevin Wolf     trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
25643cce16f4SKevin Wolf 
2565d710cf57SVladimir Sementsov-Ogievskiy     while (bytes != 0 && aio_task_pool_status(aio) == 0) {
25663fc48d09SFrediano Ziglio 
2567f50f88b9SKevin Wolf         l2meta = NULL;
2568cf5c1a23SKevin Wolf 
25693cce16f4SKevin Wolf         trace_qcow2_writev_start_part(qemu_coroutine_self());
2570d46a0bb2SKevin Wolf         offset_in_cluster = offset_into_cluster(s, offset);
2571d46a0bb2SKevin Wolf         cur_bytes = MIN(bytes, INT_MAX);
2572d46a0bb2SKevin Wolf         if (bs->encrypted) {
2573d46a0bb2SKevin Wolf             cur_bytes = MIN(cur_bytes,
2574d46a0bb2SKevin Wolf                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2575d46a0bb2SKevin Wolf                             - offset_in_cluster);
25765ebaa27eSFrediano Ziglio         }
2577095a9c58Saliguori 
25786aa7a263SVladimir Sementsov-Ogievskiy         qemu_co_mutex_lock(&s->lock);
25796aa7a263SVladimir Sementsov-Ogievskiy 
2580d46a0bb2SKevin Wolf         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2581d46a0bb2SKevin Wolf                                          &cluster_offset, &l2meta);
2582148da7eaSKevin Wolf         if (ret < 0) {
25835447c3a0SVladimir Sementsov-Ogievskiy             goto out_locked;
2584148da7eaSKevin Wolf         }
2585148da7eaSKevin Wolf 
2586344ffea9SAlberto Garcia         assert(offset_into_cluster(s, cluster_offset) == 0);
2587148da7eaSKevin Wolf 
25885447c3a0SVladimir Sementsov-Ogievskiy         ret = qcow2_pre_write_overlap_check(bs, 0,
25895447c3a0SVladimir Sementsov-Ogievskiy                                             cluster_offset + offset_in_cluster,
25905447c3a0SVladimir Sementsov-Ogievskiy                                             cur_bytes, true);
25915447c3a0SVladimir Sementsov-Ogievskiy         if (ret < 0) {
25925447c3a0SVladimir Sementsov-Ogievskiy             goto out_locked;
25935447c3a0SVladimir Sementsov-Ogievskiy         }
25945447c3a0SVladimir Sementsov-Ogievskiy 
25955447c3a0SVladimir Sementsov-Ogievskiy         qemu_co_mutex_unlock(&s->lock);
25965447c3a0SVladimir Sementsov-Ogievskiy 
2597d710cf57SVladimir Sementsov-Ogievskiy         if (!aio && cur_bytes != bytes) {
2598d710cf57SVladimir Sementsov-Ogievskiy             aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
2599d710cf57SVladimir Sementsov-Ogievskiy         }
2600d710cf57SVladimir Sementsov-Ogievskiy         ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0,
26019c4269d5SAlberto Garcia                              cluster_offset + offset_in_cluster, offset,
26029c4269d5SAlberto Garcia                              cur_bytes, qiov, qiov_offset, l2meta);
26036aa7a263SVladimir Sementsov-Ogievskiy         l2meta = NULL; /* l2meta is consumed by qcow2_co_pwritev_task() */
2604c8bb23cbSAnton Nefedov         if (ret < 0) {
26056aa7a263SVladimir Sementsov-Ogievskiy             goto fail_nometa;
2606faf575c1SFrediano Ziglio         }
2607faf575c1SFrediano Ziglio 
2608d46a0bb2SKevin Wolf         bytes -= cur_bytes;
2609d46a0bb2SKevin Wolf         offset += cur_bytes;
26106aa7a263SVladimir Sementsov-Ogievskiy         qiov_offset += cur_bytes;
2611d46a0bb2SKevin Wolf         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
26125ebaa27eSFrediano Ziglio     }
26133fc48d09SFrediano Ziglio     ret = 0;
2614faf575c1SFrediano Ziglio 
26155447c3a0SVladimir Sementsov-Ogievskiy     qemu_co_mutex_lock(&s->lock);
26165447c3a0SVladimir Sementsov-Ogievskiy 
26175447c3a0SVladimir Sementsov-Ogievskiy out_locked:
2618fd9fcd37SFam Zheng     qcow2_handle_l2meta(bs, &l2meta, false);
26190fa9131aSKevin Wolf 
2620a8c57408SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2621a8c57408SPaolo Bonzini 
26226aa7a263SVladimir Sementsov-Ogievskiy fail_nometa:
2623d710cf57SVladimir Sementsov-Ogievskiy     if (aio) {
2624d710cf57SVladimir Sementsov-Ogievskiy         aio_task_pool_wait_all(aio);
2625d710cf57SVladimir Sementsov-Ogievskiy         if (ret == 0) {
2626d710cf57SVladimir Sementsov-Ogievskiy             ret = aio_task_pool_status(aio);
2627d710cf57SVladimir Sementsov-Ogievskiy         }
2628d710cf57SVladimir Sementsov-Ogievskiy         g_free(aio);
2629d710cf57SVladimir Sementsov-Ogievskiy     }
2630d710cf57SVladimir Sementsov-Ogievskiy 
26313cce16f4SKevin Wolf     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
263242496d62SKevin Wolf 
263368d100e9SKevin Wolf     return ret;
2634585f8587Sbellard }
2635585f8587Sbellard 
2636ec6d8912SKevin Wolf static int qcow2_inactivate(BlockDriverState *bs)
2637ec6d8912SKevin Wolf {
2638ec6d8912SKevin Wolf     BDRVQcow2State *s = bs->opaque;
2639ec6d8912SKevin Wolf     int ret, result = 0;
26405f72826eSVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
2641ec6d8912SKevin Wolf 
2642644ddbb7SVladimir Sementsov-Ogievskiy     qcow2_store_persistent_dirty_bitmaps(bs, true, &local_err);
264383a8c775SPavel Butsykin     if (local_err != NULL) {
264483a8c775SPavel Butsykin         result = -EINVAL;
2645132adb68SVladimir Sementsov-Ogievskiy         error_reportf_err(local_err, "Lost persistent bitmaps during "
2646132adb68SVladimir Sementsov-Ogievskiy                           "inactivation of node '%s': ",
264783a8c775SPavel Butsykin                           bdrv_get_device_or_node_name(bs));
264883a8c775SPavel Butsykin     }
264983a8c775SPavel Butsykin 
2650ec6d8912SKevin Wolf     ret = qcow2_cache_flush(bs, s->l2_table_cache);
2651ec6d8912SKevin Wolf     if (ret) {
2652ec6d8912SKevin Wolf         result = ret;
2653ec6d8912SKevin Wolf         error_report("Failed to flush the L2 table cache: %s",
2654ec6d8912SKevin Wolf                      strerror(-ret));
2655ec6d8912SKevin Wolf     }
2656ec6d8912SKevin Wolf 
2657ec6d8912SKevin Wolf     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2658ec6d8912SKevin Wolf     if (ret) {
2659ec6d8912SKevin Wolf         result = ret;
2660ec6d8912SKevin Wolf         error_report("Failed to flush the refcount block cache: %s",
2661ec6d8912SKevin Wolf                      strerror(-ret));
2662ec6d8912SKevin Wolf     }
2663ec6d8912SKevin Wolf 
2664ec6d8912SKevin Wolf     if (result == 0) {
2665ec6d8912SKevin Wolf         qcow2_mark_clean(bs);
2666ec6d8912SKevin Wolf     }
2667ec6d8912SKevin Wolf 
2668ec6d8912SKevin Wolf     return result;
2669ec6d8912SKevin Wolf }
2670ec6d8912SKevin Wolf 
26717c80ab3fSJes Sorensen static void qcow2_close(BlockDriverState *bs)
2672585f8587Sbellard {
2673ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2674de82815dSKevin Wolf     qemu_vfree(s->l1_table);
2675cf93980eSMax Reitz     /* else pre-write overlap checks in cache_destroy may crash */
2676cf93980eSMax Reitz     s->l1_table = NULL;
267729c1a730SKevin Wolf 
2678140fd5a6SKevin Wolf     if (!(s->flags & BDRV_O_INACTIVE)) {
2679ec6d8912SKevin Wolf         qcow2_inactivate(bs);
26803b5e14c7SMax Reitz     }
2681c61d0004SStefan Hajnoczi 
2682279621c0SAlberto Garcia     cache_clean_timer_del(bs);
2683e64d4072SAlberto Garcia     qcow2_cache_destroy(s->l2_table_cache);
2684e64d4072SAlberto Garcia     qcow2_cache_destroy(s->refcount_block_cache);
268529c1a730SKevin Wolf 
2686b25b387fSDaniel P. Berrange     qcrypto_block_free(s->crypto);
2687b25b387fSDaniel P. Berrange     s->crypto = NULL;
26884aebf0f0SPan Nengyuan     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
2689f6fa64f6SDaniel P. Berrange 
26906744cbabSKevin Wolf     g_free(s->unknown_header_fields);
269175bab85cSKevin Wolf     cleanup_unknown_header_ext(bs);
26926744cbabSKevin Wolf 
26939b890bdcSKevin Wolf     g_free(s->image_data_file);
2694e4603fe1SKevin Wolf     g_free(s->image_backing_file);
2695e4603fe1SKevin Wolf     g_free(s->image_backing_format);
2696e4603fe1SKevin Wolf 
26970e8c08beSKevin Wolf     if (has_data_file(bs)) {
26980e8c08beSKevin Wolf         bdrv_unref_child(bs, s->data_file);
2699808cf3cbSVladimir Sementsov-Ogievskiy         s->data_file = NULL;
27000e8c08beSKevin Wolf     }
27010e8c08beSKevin Wolf 
2702ed6ccf0fSKevin Wolf     qcow2_refcount_close(bs);
270328c1202bSLi Zhi Hui     qcow2_free_snapshots(bs);
2704585f8587Sbellard }
2705585f8587Sbellard 
27062b148f39SPaolo Bonzini static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
27072b148f39SPaolo Bonzini                                                    Error **errp)
270806d9260fSAnthony Liguori {
2709ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
271006d9260fSAnthony Liguori     int flags = s->flags;
2711b25b387fSDaniel P. Berrange     QCryptoBlock *crypto = NULL;
2712acdfb480SKevin Wolf     QDict *options;
27135a8a30dbSKevin Wolf     Error *local_err = NULL;
27145a8a30dbSKevin Wolf     int ret;
271506d9260fSAnthony Liguori 
271606d9260fSAnthony Liguori     /*
271706d9260fSAnthony Liguori      * Backing files are read-only which makes all of their metadata immutable,
271806d9260fSAnthony Liguori      * that means we don't have to worry about reopening them here.
271906d9260fSAnthony Liguori      */
272006d9260fSAnthony Liguori 
2721b25b387fSDaniel P. Berrange     crypto = s->crypto;
2722b25b387fSDaniel P. Berrange     s->crypto = NULL;
272306d9260fSAnthony Liguori 
272406d9260fSAnthony Liguori     qcow2_close(bs);
272506d9260fSAnthony Liguori 
2726ff99129aSKevin Wolf     memset(s, 0, sizeof(BDRVQcow2State));
2727d475e5acSKevin Wolf     options = qdict_clone_shallow(bs->options);
27285a8a30dbSKevin Wolf 
2729140fd5a6SKevin Wolf     flags &= ~BDRV_O_INACTIVE;
27302b148f39SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
27314e4bf5c4SKevin Wolf     ret = qcow2_do_open(bs, options, flags, &local_err);
27322b148f39SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2733cb3e7f08SMarc-André Lureau     qobject_unref(options);
27345a8a30dbSKevin Wolf     if (local_err) {
27354b576648SMarkus Armbruster         error_propagate_prepend(errp, local_err,
27364b576648SMarkus Armbruster                                 "Could not reopen qcow2 layer: ");
2737191fb11bSKevin Wolf         bs->drv = NULL;
27385a8a30dbSKevin Wolf         return;
27395a8a30dbSKevin Wolf     } else if (ret < 0) {
27405a8a30dbSKevin Wolf         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
2741191fb11bSKevin Wolf         bs->drv = NULL;
27425a8a30dbSKevin Wolf         return;
27435a8a30dbSKevin Wolf     }
2744acdfb480SKevin Wolf 
2745b25b387fSDaniel P. Berrange     s->crypto = crypto;
274606d9260fSAnthony Liguori }
274706d9260fSAnthony Liguori 
2748e24e49e6SKevin Wolf static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2749e24e49e6SKevin Wolf     size_t len, size_t buflen)
2750756e6736SKevin Wolf {
2751e24e49e6SKevin Wolf     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2752e24e49e6SKevin Wolf     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2753756e6736SKevin Wolf 
2754e24e49e6SKevin Wolf     if (buflen < ext_len) {
2755756e6736SKevin Wolf         return -ENOSPC;
2756756e6736SKevin Wolf     }
2757756e6736SKevin Wolf 
2758e24e49e6SKevin Wolf     *ext_backing_fmt = (QCowExtension) {
2759e24e49e6SKevin Wolf         .magic  = cpu_to_be32(magic),
2760e24e49e6SKevin Wolf         .len    = cpu_to_be32(len),
2761e24e49e6SKevin Wolf     };
27620647d47cSStefan Hajnoczi 
27630647d47cSStefan Hajnoczi     if (len) {
2764e24e49e6SKevin Wolf         memcpy(buf + sizeof(QCowExtension), s, len);
27650647d47cSStefan Hajnoczi     }
2766756e6736SKevin Wolf 
2767e24e49e6SKevin Wolf     return ext_len;
2768756e6736SKevin Wolf }
2769756e6736SKevin Wolf 
2770e24e49e6SKevin Wolf /*
2771e24e49e6SKevin Wolf  * Updates the qcow2 header, including the variable length parts of it, i.e.
2772e24e49e6SKevin Wolf  * the backing file name and all extensions. qcow2 was not designed to allow
2773e24e49e6SKevin Wolf  * such changes, so if we run out of space (we can only use the first cluster)
2774e24e49e6SKevin Wolf  * this function may fail.
2775e24e49e6SKevin Wolf  *
2776e24e49e6SKevin Wolf  * Returns 0 on success, -errno in error cases.
2777e24e49e6SKevin Wolf  */
2778e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs)
2779e24e49e6SKevin Wolf {
2780ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2781e24e49e6SKevin Wolf     QCowHeader *header;
2782e24e49e6SKevin Wolf     char *buf;
2783e24e49e6SKevin Wolf     size_t buflen = s->cluster_size;
2784e24e49e6SKevin Wolf     int ret;
2785e24e49e6SKevin Wolf     uint64_t total_size;
2786e24e49e6SKevin Wolf     uint32_t refcount_table_clusters;
27876744cbabSKevin Wolf     size_t header_length;
278875bab85cSKevin Wolf     Qcow2UnknownHeaderExtension *uext;
2789e24e49e6SKevin Wolf 
2790e24e49e6SKevin Wolf     buf = qemu_blockalign(bs, buflen);
2791e24e49e6SKevin Wolf 
2792e24e49e6SKevin Wolf     /* Header structure */
2793e24e49e6SKevin Wolf     header = (QCowHeader*) buf;
2794e24e49e6SKevin Wolf 
2795e24e49e6SKevin Wolf     if (buflen < sizeof(*header)) {
2796e24e49e6SKevin Wolf         ret = -ENOSPC;
2797e24e49e6SKevin Wolf         goto fail;
2798756e6736SKevin Wolf     }
2799756e6736SKevin Wolf 
28006744cbabSKevin Wolf     header_length = sizeof(*header) + s->unknown_header_fields_size;
2801e24e49e6SKevin Wolf     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2802e24e49e6SKevin Wolf     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2803e24e49e6SKevin Wolf 
2804572ad978SDenis Plotnikov     ret = validate_compression_type(s, NULL);
2805572ad978SDenis Plotnikov     if (ret) {
2806572ad978SDenis Plotnikov         goto fail;
2807572ad978SDenis Plotnikov     }
2808572ad978SDenis Plotnikov 
2809e24e49e6SKevin Wolf     *header = (QCowHeader) {
28106744cbabSKevin Wolf         /* Version 2 fields */
2811e24e49e6SKevin Wolf         .magic                  = cpu_to_be32(QCOW_MAGIC),
28126744cbabSKevin Wolf         .version                = cpu_to_be32(s->qcow_version),
2813e24e49e6SKevin Wolf         .backing_file_offset    = 0,
2814e24e49e6SKevin Wolf         .backing_file_size      = 0,
2815e24e49e6SKevin Wolf         .cluster_bits           = cpu_to_be32(s->cluster_bits),
2816e24e49e6SKevin Wolf         .size                   = cpu_to_be64(total_size),
2817e24e49e6SKevin Wolf         .crypt_method           = cpu_to_be32(s->crypt_method_header),
2818e24e49e6SKevin Wolf         .l1_size                = cpu_to_be32(s->l1_size),
2819e24e49e6SKevin Wolf         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
2820e24e49e6SKevin Wolf         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
2821e24e49e6SKevin Wolf         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2822e24e49e6SKevin Wolf         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
2823e24e49e6SKevin Wolf         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
28246744cbabSKevin Wolf 
28256744cbabSKevin Wolf         /* Version 3 fields */
28266744cbabSKevin Wolf         .incompatible_features  = cpu_to_be64(s->incompatible_features),
28276744cbabSKevin Wolf         .compatible_features    = cpu_to_be64(s->compatible_features),
28286744cbabSKevin Wolf         .autoclear_features     = cpu_to_be64(s->autoclear_features),
2829b6481f37SMax Reitz         .refcount_order         = cpu_to_be32(s->refcount_order),
28306744cbabSKevin Wolf         .header_length          = cpu_to_be32(header_length),
2831572ad978SDenis Plotnikov         .compression_type       = s->compression_type,
2832e24e49e6SKevin Wolf     };
2833e24e49e6SKevin Wolf 
28346744cbabSKevin Wolf     /* For older versions, write a shorter header */
28356744cbabSKevin Wolf     switch (s->qcow_version) {
28366744cbabSKevin Wolf     case 2:
28376744cbabSKevin Wolf         ret = offsetof(QCowHeader, incompatible_features);
28386744cbabSKevin Wolf         break;
28396744cbabSKevin Wolf     case 3:
28406744cbabSKevin Wolf         ret = sizeof(*header);
28416744cbabSKevin Wolf         break;
28426744cbabSKevin Wolf     default:
2843b6c14762SJim Meyering         ret = -EINVAL;
2844b6c14762SJim Meyering         goto fail;
28456744cbabSKevin Wolf     }
28466744cbabSKevin Wolf 
28476744cbabSKevin Wolf     buf += ret;
28486744cbabSKevin Wolf     buflen -= ret;
28496744cbabSKevin Wolf     memset(buf, 0, buflen);
28506744cbabSKevin Wolf 
28516744cbabSKevin Wolf     /* Preserve any unknown field in the header */
28526744cbabSKevin Wolf     if (s->unknown_header_fields_size) {
28536744cbabSKevin Wolf         if (buflen < s->unknown_header_fields_size) {
28546744cbabSKevin Wolf             ret = -ENOSPC;
28556744cbabSKevin Wolf             goto fail;
28566744cbabSKevin Wolf         }
28576744cbabSKevin Wolf 
28586744cbabSKevin Wolf         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
28596744cbabSKevin Wolf         buf += s->unknown_header_fields_size;
28606744cbabSKevin Wolf         buflen -= s->unknown_header_fields_size;
28616744cbabSKevin Wolf     }
2862e24e49e6SKevin Wolf 
2863e24e49e6SKevin Wolf     /* Backing file format header extension */
2864e4603fe1SKevin Wolf     if (s->image_backing_format) {
2865e24e49e6SKevin Wolf         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
2866e4603fe1SKevin Wolf                              s->image_backing_format,
2867e4603fe1SKevin Wolf                              strlen(s->image_backing_format),
2868e24e49e6SKevin Wolf                              buflen);
2869756e6736SKevin Wolf         if (ret < 0) {
2870756e6736SKevin Wolf             goto fail;
2871756e6736SKevin Wolf         }
2872756e6736SKevin Wolf 
2873e24e49e6SKevin Wolf         buf += ret;
2874e24e49e6SKevin Wolf         buflen -= ret;
2875e24e49e6SKevin Wolf     }
2876756e6736SKevin Wolf 
28779b890bdcSKevin Wolf     /* External data file header extension */
28789b890bdcSKevin Wolf     if (has_data_file(bs) && s->image_data_file) {
28799b890bdcSKevin Wolf         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE,
28809b890bdcSKevin Wolf                              s->image_data_file, strlen(s->image_data_file),
28819b890bdcSKevin Wolf                              buflen);
28829b890bdcSKevin Wolf         if (ret < 0) {
28839b890bdcSKevin Wolf             goto fail;
28849b890bdcSKevin Wolf         }
28859b890bdcSKevin Wolf 
28869b890bdcSKevin Wolf         buf += ret;
28879b890bdcSKevin Wolf         buflen -= ret;
28889b890bdcSKevin Wolf     }
28899b890bdcSKevin Wolf 
28904652b8f3SDaniel P. Berrange     /* Full disk encryption header pointer extension */
28914652b8f3SDaniel P. Berrange     if (s->crypto_header.offset != 0) {
28923b698f52SPeter Maydell         s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset);
28933b698f52SPeter Maydell         s->crypto_header.length = cpu_to_be64(s->crypto_header.length);
28944652b8f3SDaniel P. Berrange         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
28954652b8f3SDaniel P. Berrange                              &s->crypto_header, sizeof(s->crypto_header),
28964652b8f3SDaniel P. Berrange                              buflen);
28973b698f52SPeter Maydell         s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
28983b698f52SPeter Maydell         s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
28994652b8f3SDaniel P. Berrange         if (ret < 0) {
29004652b8f3SDaniel P. Berrange             goto fail;
29014652b8f3SDaniel P. Berrange         }
29024652b8f3SDaniel P. Berrange         buf += ret;
29034652b8f3SDaniel P. Berrange         buflen -= ret;
29044652b8f3SDaniel P. Berrange     }
29054652b8f3SDaniel P. Berrange 
2906e7be13adSEric Blake     /*
2907e7be13adSEric Blake      * Feature table.  A mere 8 feature names occupies 392 bytes, and
2908e7be13adSEric Blake      * when coupled with the v3 minimum header of 104 bytes plus the
2909e7be13adSEric Blake      * 8-byte end-of-extension marker, that would leave only 8 bytes
2910e7be13adSEric Blake      * for a backing file name in an image with 512-byte clusters.
2911e7be13adSEric Blake      * Thus, we choose to omit this header for cluster sizes 4k and
2912e7be13adSEric Blake      * smaller.
2913e7be13adSEric Blake      */
2914e7be13adSEric Blake     if (s->qcow_version >= 3 && s->cluster_size > 4096) {
2915bb40ebceSEric Blake         static const Qcow2Feature features[] = {
2916c61d0004SStefan Hajnoczi             {
2917c61d0004SStefan Hajnoczi                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2918c61d0004SStefan Hajnoczi                 .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
2919c61d0004SStefan Hajnoczi                 .name = "dirty bit",
2920c61d0004SStefan Hajnoczi             },
2921bfe8043eSStefan Hajnoczi             {
292269c98726SMax Reitz                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
292369c98726SMax Reitz                 .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
292469c98726SMax Reitz                 .name = "corrupt bit",
292569c98726SMax Reitz             },
292669c98726SMax Reitz             {
292793c24936SKevin Wolf                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
292893c24936SKevin Wolf                 .bit  = QCOW2_INCOMPAT_DATA_FILE_BITNR,
292993c24936SKevin Wolf                 .name = "external data file",
293093c24936SKevin Wolf             },
293193c24936SKevin Wolf             {
2932572ad978SDenis Plotnikov                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2933572ad978SDenis Plotnikov                 .bit  = QCOW2_INCOMPAT_COMPRESSION_BITNR,
2934572ad978SDenis Plotnikov                 .name = "compression type",
2935572ad978SDenis Plotnikov             },
2936572ad978SDenis Plotnikov             {
2937bfe8043eSStefan Hajnoczi                 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2938bfe8043eSStefan Hajnoczi                 .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2939bfe8043eSStefan Hajnoczi                 .name = "lazy refcounts",
2940bfe8043eSStefan Hajnoczi             },
2941bb40ebceSEric Blake             {
2942bb40ebceSEric Blake                 .type = QCOW2_FEAT_TYPE_AUTOCLEAR,
2943bb40ebceSEric Blake                 .bit  = QCOW2_AUTOCLEAR_BITMAPS_BITNR,
2944bb40ebceSEric Blake                 .name = "bitmaps",
2945bb40ebceSEric Blake             },
2946bb40ebceSEric Blake             {
2947bb40ebceSEric Blake                 .type = QCOW2_FEAT_TYPE_AUTOCLEAR,
2948bb40ebceSEric Blake                 .bit  = QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
2949bb40ebceSEric Blake                 .name = "raw external data",
2950bb40ebceSEric Blake             },
2951cfcc4c62SKevin Wolf         };
2952cfcc4c62SKevin Wolf 
2953cfcc4c62SKevin Wolf         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2954cfcc4c62SKevin Wolf                              features, sizeof(features), buflen);
2955cfcc4c62SKevin Wolf         if (ret < 0) {
2956cfcc4c62SKevin Wolf             goto fail;
2957cfcc4c62SKevin Wolf         }
2958cfcc4c62SKevin Wolf         buf += ret;
2959cfcc4c62SKevin Wolf         buflen -= ret;
29601a4828c7SKevin Wolf     }
2961cfcc4c62SKevin Wolf 
296288ddffaeSVladimir Sementsov-Ogievskiy     /* Bitmap extension */
296388ddffaeSVladimir Sementsov-Ogievskiy     if (s->nb_bitmaps > 0) {
296488ddffaeSVladimir Sementsov-Ogievskiy         Qcow2BitmapHeaderExt bitmaps_header = {
296588ddffaeSVladimir Sementsov-Ogievskiy             .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
296688ddffaeSVladimir Sementsov-Ogievskiy             .bitmap_directory_size =
296788ddffaeSVladimir Sementsov-Ogievskiy                     cpu_to_be64(s->bitmap_directory_size),
296888ddffaeSVladimir Sementsov-Ogievskiy             .bitmap_directory_offset =
296988ddffaeSVladimir Sementsov-Ogievskiy                     cpu_to_be64(s->bitmap_directory_offset)
297088ddffaeSVladimir Sementsov-Ogievskiy         };
297188ddffaeSVladimir Sementsov-Ogievskiy         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
297288ddffaeSVladimir Sementsov-Ogievskiy                              &bitmaps_header, sizeof(bitmaps_header),
297388ddffaeSVladimir Sementsov-Ogievskiy                              buflen);
297488ddffaeSVladimir Sementsov-Ogievskiy         if (ret < 0) {
297588ddffaeSVladimir Sementsov-Ogievskiy             goto fail;
297688ddffaeSVladimir Sementsov-Ogievskiy         }
297788ddffaeSVladimir Sementsov-Ogievskiy         buf += ret;
297888ddffaeSVladimir Sementsov-Ogievskiy         buflen -= ret;
297988ddffaeSVladimir Sementsov-Ogievskiy     }
298088ddffaeSVladimir Sementsov-Ogievskiy 
298175bab85cSKevin Wolf     /* Keep unknown header extensions */
298275bab85cSKevin Wolf     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
298375bab85cSKevin Wolf         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
298475bab85cSKevin Wolf         if (ret < 0) {
298575bab85cSKevin Wolf             goto fail;
298675bab85cSKevin Wolf         }
298775bab85cSKevin Wolf 
298875bab85cSKevin Wolf         buf += ret;
298975bab85cSKevin Wolf         buflen -= ret;
299075bab85cSKevin Wolf     }
299175bab85cSKevin Wolf 
2992e24e49e6SKevin Wolf     /* End of header extensions */
2993e24e49e6SKevin Wolf     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
2994756e6736SKevin Wolf     if (ret < 0) {
2995756e6736SKevin Wolf         goto fail;
2996756e6736SKevin Wolf     }
2997756e6736SKevin Wolf 
2998e24e49e6SKevin Wolf     buf += ret;
2999e24e49e6SKevin Wolf     buflen -= ret;
3000e24e49e6SKevin Wolf 
3001e24e49e6SKevin Wolf     /* Backing file name */
3002e4603fe1SKevin Wolf     if (s->image_backing_file) {
3003e4603fe1SKevin Wolf         size_t backing_file_len = strlen(s->image_backing_file);
3004e24e49e6SKevin Wolf 
3005e24e49e6SKevin Wolf         if (buflen < backing_file_len) {
3006e24e49e6SKevin Wolf             ret = -ENOSPC;
3007e24e49e6SKevin Wolf             goto fail;
3008e24e49e6SKevin Wolf         }
3009e24e49e6SKevin Wolf 
301000ea1881SJim Meyering         /* Using strncpy is ok here, since buf is not NUL-terminated. */
3011e4603fe1SKevin Wolf         strncpy(buf, s->image_backing_file, buflen);
3012e24e49e6SKevin Wolf 
3013e24e49e6SKevin Wolf         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
3014e24e49e6SKevin Wolf         header->backing_file_size   = cpu_to_be32(backing_file_len);
3015e24e49e6SKevin Wolf     }
3016e24e49e6SKevin Wolf 
3017e24e49e6SKevin Wolf     /* Write the new header */
3018d9ca2ea2SKevin Wolf     ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
3019756e6736SKevin Wolf     if (ret < 0) {
3020756e6736SKevin Wolf         goto fail;
3021756e6736SKevin Wolf     }
3022756e6736SKevin Wolf 
3023756e6736SKevin Wolf     ret = 0;
3024756e6736SKevin Wolf fail:
3025e24e49e6SKevin Wolf     qemu_vfree(header);
3026756e6736SKevin Wolf     return ret;
3027756e6736SKevin Wolf }
3028756e6736SKevin Wolf 
3029756e6736SKevin Wolf static int qcow2_change_backing_file(BlockDriverState *bs,
3030756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
3031756e6736SKevin Wolf {
3032ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3033e4603fe1SKevin Wolf 
30346c3944dcSKevin Wolf     /* Adding a backing file means that the external data file alone won't be
30356c3944dcSKevin Wolf      * enough to make sense of the content */
30366c3944dcSKevin Wolf     if (backing_file && data_file_is_raw(bs)) {
30376c3944dcSKevin Wolf         return -EINVAL;
30386c3944dcSKevin Wolf     }
30396c3944dcSKevin Wolf 
30404e876bcfSMax Reitz     if (backing_file && strlen(backing_file) > 1023) {
30414e876bcfSMax Reitz         return -EINVAL;
30424e876bcfSMax Reitz     }
30434e876bcfSMax Reitz 
3044998c2019SMax Reitz     pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3045998c2019SMax Reitz             backing_file ?: "");
3046e24e49e6SKevin Wolf     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
3047e24e49e6SKevin Wolf     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
3048e24e49e6SKevin Wolf 
3049e4603fe1SKevin Wolf     g_free(s->image_backing_file);
3050e4603fe1SKevin Wolf     g_free(s->image_backing_format);
3051e4603fe1SKevin Wolf 
3052e4603fe1SKevin Wolf     s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
3053e4603fe1SKevin Wolf     s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
3054e4603fe1SKevin Wolf 
3055e24e49e6SKevin Wolf     return qcow2_update_header(bs);
3056756e6736SKevin Wolf }
3057756e6736SKevin Wolf 
305860900b7bSKevin Wolf static int qcow2_set_up_encryption(BlockDriverState *bs,
305960900b7bSKevin Wolf                                    QCryptoBlockCreateOptions *cryptoopts,
306060900b7bSKevin Wolf                                    Error **errp)
306160900b7bSKevin Wolf {
306260900b7bSKevin Wolf     BDRVQcow2State *s = bs->opaque;
306360900b7bSKevin Wolf     QCryptoBlock *crypto = NULL;
306460900b7bSKevin Wolf     int fmt, ret;
306560900b7bSKevin Wolf 
306660900b7bSKevin Wolf     switch (cryptoopts->format) {
306760900b7bSKevin Wolf     case Q_CRYPTO_BLOCK_FORMAT_LUKS:
306860900b7bSKevin Wolf         fmt = QCOW_CRYPT_LUKS;
306960900b7bSKevin Wolf         break;
307060900b7bSKevin Wolf     case Q_CRYPTO_BLOCK_FORMAT_QCOW:
307160900b7bSKevin Wolf         fmt = QCOW_CRYPT_AES;
307260900b7bSKevin Wolf         break;
307360900b7bSKevin Wolf     default:
307460900b7bSKevin Wolf         error_setg(errp, "Crypto format not supported in qcow2");
307560900b7bSKevin Wolf         return -EINVAL;
307660900b7bSKevin Wolf     }
307760900b7bSKevin Wolf 
30784652b8f3SDaniel P. Berrange     s->crypt_method_header = fmt;
3079b25b387fSDaniel P. Berrange 
30801cd9a787SDaniel P. Berrange     crypto = qcrypto_block_create(cryptoopts, "encrypt.",
30814652b8f3SDaniel P. Berrange                                   qcow2_crypto_hdr_init_func,
30824652b8f3SDaniel P. Berrange                                   qcow2_crypto_hdr_write_func,
3083b25b387fSDaniel P. Berrange                                   bs, errp);
3084b25b387fSDaniel P. Berrange     if (!crypto) {
308560900b7bSKevin Wolf         return -EINVAL;
3086b25b387fSDaniel P. Berrange     }
3087b25b387fSDaniel P. Berrange 
3088b25b387fSDaniel P. Berrange     ret = qcow2_update_header(bs);
3089b25b387fSDaniel P. Berrange     if (ret < 0) {
3090b25b387fSDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not write encryption header");
3091b25b387fSDaniel P. Berrange         goto out;
3092b25b387fSDaniel P. Berrange     }
3093b25b387fSDaniel P. Berrange 
309460900b7bSKevin Wolf     ret = 0;
3095b25b387fSDaniel P. Berrange  out:
3096b25b387fSDaniel P. Berrange     qcrypto_block_free(crypto);
3097b25b387fSDaniel P. Berrange     return ret;
3098b25b387fSDaniel P. Berrange }
3099b25b387fSDaniel P. Berrange 
31007bc45dc1SMax Reitz /**
31017bc45dc1SMax Reitz  * Preallocates metadata structures for data clusters between @offset (in the
31027bc45dc1SMax Reitz  * guest disk) and @new_length (which is thus generally the new guest disk
31037bc45dc1SMax Reitz  * size).
31047bc45dc1SMax Reitz  *
31057bc45dc1SMax Reitz  * Returns: 0 on success, -errno on failure.
31067bc45dc1SMax Reitz  */
310747e86b86SKevin Wolf static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
3108718c0fceSKevin Wolf                                        uint64_t new_length, PreallocMode mode,
3109718c0fceSKevin Wolf                                        Error **errp)
3110a35e1c17SKevin Wolf {
311193e32b3eSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3112d46a0bb2SKevin Wolf     uint64_t bytes;
3113060bee89SKevin Wolf     uint64_t host_offset = 0;
3114718c0fceSKevin Wolf     int64_t file_length;
3115d46a0bb2SKevin Wolf     unsigned int cur_bytes;
3116148da7eaSKevin Wolf     int ret;
3117f50f88b9SKevin Wolf     QCowL2Meta *meta;
3118a35e1c17SKevin Wolf 
31197bc45dc1SMax Reitz     assert(offset <= new_length);
31207bc45dc1SMax Reitz     bytes = new_length - offset;
3121a35e1c17SKevin Wolf 
3122d46a0bb2SKevin Wolf     while (bytes) {
3123f29fbf7cSKevin Wolf         cur_bytes = MIN(bytes, QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size));
3124d46a0bb2SKevin Wolf         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
3125060bee89SKevin Wolf                                          &host_offset, &meta);
3126148da7eaSKevin Wolf         if (ret < 0) {
3127360bd074SKevin Wolf             error_setg_errno(errp, -ret, "Allocating clusters failed");
312847e86b86SKevin Wolf             return ret;
3129a35e1c17SKevin Wolf         }
3130a35e1c17SKevin Wolf 
3131c792707fSStefan Hajnoczi         while (meta) {
3132c792707fSStefan Hajnoczi             QCowL2Meta *next = meta->next;
3133c792707fSStefan Hajnoczi 
3134f50f88b9SKevin Wolf             ret = qcow2_alloc_cluster_link_l2(bs, meta);
313519dbcbf7SKevin Wolf             if (ret < 0) {
3136360bd074SKevin Wolf                 error_setg_errno(errp, -ret, "Mapping clusters failed");
31377c2bbf4aSHu Tao                 qcow2_free_any_clusters(bs, meta->alloc_offset,
31387c2bbf4aSHu Tao                                         meta->nb_clusters, QCOW2_DISCARD_NEVER);
313947e86b86SKevin Wolf                 return ret;
3140a35e1c17SKevin Wolf             }
3141a35e1c17SKevin Wolf 
31427c2bbf4aSHu Tao             /* There are no dependent requests, but we need to remove our
31437c2bbf4aSHu Tao              * request from the list of in-flight requests */
31444e95314eSKevin Wolf             QLIST_REMOVE(meta, next_in_flight);
3145c792707fSStefan Hajnoczi 
3146c792707fSStefan Hajnoczi             g_free(meta);
3147c792707fSStefan Hajnoczi             meta = next;
3148f50f88b9SKevin Wolf         }
3149f214978aSKevin Wolf 
3150a35e1c17SKevin Wolf         /* TODO Preallocate data if requested */
3151a35e1c17SKevin Wolf 
3152d46a0bb2SKevin Wolf         bytes -= cur_bytes;
3153d46a0bb2SKevin Wolf         offset += cur_bytes;
3154a35e1c17SKevin Wolf     }
3155a35e1c17SKevin Wolf 
3156a35e1c17SKevin Wolf     /*
3157a35e1c17SKevin Wolf      * It is expected that the image file is large enough to actually contain
3158a35e1c17SKevin Wolf      * all of the allocated clusters (otherwise we get failing reads after
3159a35e1c17SKevin Wolf      * EOF). Extend the image to the last allocated sector.
3160a35e1c17SKevin Wolf      */
3161718c0fceSKevin Wolf     file_length = bdrv_getlength(s->data_file->bs);
3162718c0fceSKevin Wolf     if (file_length < 0) {
3163718c0fceSKevin Wolf         error_setg_errno(errp, -file_length, "Could not get file size");
3164718c0fceSKevin Wolf         return file_length;
3165718c0fceSKevin Wolf     }
3166718c0fceSKevin Wolf 
3167718c0fceSKevin Wolf     if (host_offset + cur_bytes > file_length) {
3168718c0fceSKevin Wolf         if (mode == PREALLOC_MODE_METADATA) {
3169718c0fceSKevin Wolf             mode = PREALLOC_MODE_OFF;
3170718c0fceSKevin Wolf         }
3171c80d8b06SMax Reitz         ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
31727b8e4857SKevin Wolf                                mode, 0, errp);
317319dbcbf7SKevin Wolf         if (ret < 0) {
317447e86b86SKevin Wolf             return ret;
317519dbcbf7SKevin Wolf         }
3176a35e1c17SKevin Wolf     }
3177a35e1c17SKevin Wolf 
317847e86b86SKevin Wolf     return 0;
3179a35e1c17SKevin Wolf }
3180a35e1c17SKevin Wolf 
31817c5bcc42SStefan Hajnoczi /* qcow2_refcount_metadata_size:
31827c5bcc42SStefan Hajnoczi  * @clusters: number of clusters to refcount (including data and L1/L2 tables)
31837c5bcc42SStefan Hajnoczi  * @cluster_size: size of a cluster, in bytes
31847c5bcc42SStefan Hajnoczi  * @refcount_order: refcount bits power-of-2 exponent
318512cc30a8SMax Reitz  * @generous_increase: allow for the refcount table to be 1.5x as large as it
318612cc30a8SMax Reitz  *                     needs to be
31877c5bcc42SStefan Hajnoczi  *
31887c5bcc42SStefan Hajnoczi  * Returns: Number of bytes required for refcount blocks and table metadata.
31897c5bcc42SStefan Hajnoczi  */
319012cc30a8SMax Reitz int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
319112cc30a8SMax Reitz                                      int refcount_order, bool generous_increase,
319212cc30a8SMax Reitz                                      uint64_t *refblock_count)
31937c5bcc42SStefan Hajnoczi {
31947c5bcc42SStefan Hajnoczi     /*
31957c5bcc42SStefan Hajnoczi      * Every host cluster is reference-counted, including metadata (even
31967c5bcc42SStefan Hajnoczi      * refcount metadata is recursively included).
31977c5bcc42SStefan Hajnoczi      *
31987c5bcc42SStefan Hajnoczi      * An accurate formula for the size of refcount metadata size is difficult
31997c5bcc42SStefan Hajnoczi      * to derive.  An easier method of calculation is finding the fixed point
32007c5bcc42SStefan Hajnoczi      * where no further refcount blocks or table clusters are required to
32017c5bcc42SStefan Hajnoczi      * reference count every cluster.
32027c5bcc42SStefan Hajnoczi      */
32037c5bcc42SStefan Hajnoczi     int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
32047c5bcc42SStefan Hajnoczi     int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
32057c5bcc42SStefan Hajnoczi     int64_t table = 0;  /* number of refcount table clusters */
32067c5bcc42SStefan Hajnoczi     int64_t blocks = 0; /* number of refcount block clusters */
32077c5bcc42SStefan Hajnoczi     int64_t last;
32087c5bcc42SStefan Hajnoczi     int64_t n = 0;
32097c5bcc42SStefan Hajnoczi 
32107c5bcc42SStefan Hajnoczi     do {
32117c5bcc42SStefan Hajnoczi         last = n;
32127c5bcc42SStefan Hajnoczi         blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
32137c5bcc42SStefan Hajnoczi         table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
32147c5bcc42SStefan Hajnoczi         n = clusters + blocks + table;
321512cc30a8SMax Reitz 
321612cc30a8SMax Reitz         if (n == last && generous_increase) {
321712cc30a8SMax Reitz             clusters += DIV_ROUND_UP(table, 2);
321812cc30a8SMax Reitz             n = 0; /* force another loop */
321912cc30a8SMax Reitz             generous_increase = false;
322012cc30a8SMax Reitz         }
32217c5bcc42SStefan Hajnoczi     } while (n != last);
32227c5bcc42SStefan Hajnoczi 
322312cc30a8SMax Reitz     if (refblock_count) {
322412cc30a8SMax Reitz         *refblock_count = blocks;
322512cc30a8SMax Reitz     }
322612cc30a8SMax Reitz 
32277c5bcc42SStefan Hajnoczi     return (blocks + table) * cluster_size;
32287c5bcc42SStefan Hajnoczi }
32297c5bcc42SStefan Hajnoczi 
323095c67e3bSStefan Hajnoczi /**
323195c67e3bSStefan Hajnoczi  * qcow2_calc_prealloc_size:
323295c67e3bSStefan Hajnoczi  * @total_size: virtual disk size in bytes
323395c67e3bSStefan Hajnoczi  * @cluster_size: cluster size in bytes
323495c67e3bSStefan Hajnoczi  * @refcount_order: refcount bits power-of-2 exponent
3235a9420734SKevin Wolf  *
323695c67e3bSStefan Hajnoczi  * Returns: Total number of bytes required for the fully allocated image
323795c67e3bSStefan Hajnoczi  * (including metadata).
3238a9420734SKevin Wolf  */
323995c67e3bSStefan Hajnoczi static int64_t qcow2_calc_prealloc_size(int64_t total_size,
324095c67e3bSStefan Hajnoczi                                         size_t cluster_size,
324195c67e3bSStefan Hajnoczi                                         int refcount_order)
324295c67e3bSStefan Hajnoczi {
32430e4271b7SHu Tao     int64_t meta_size = 0;
32447c5bcc42SStefan Hajnoczi     uint64_t nl1e, nl2e;
32459e029689SAlberto Garcia     int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
32460e4271b7SHu Tao 
32470e4271b7SHu Tao     /* header: 1 cluster */
32480e4271b7SHu Tao     meta_size += cluster_size;
32490e4271b7SHu Tao 
32500e4271b7SHu Tao     /* total size of L2 tables */
32510e4271b7SHu Tao     nl2e = aligned_total_size / cluster_size;
32529e029689SAlberto Garcia     nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t));
32530e4271b7SHu Tao     meta_size += nl2e * sizeof(uint64_t);
32540e4271b7SHu Tao 
32550e4271b7SHu Tao     /* total size of L1 tables */
32560e4271b7SHu Tao     nl1e = nl2e * sizeof(uint64_t) / cluster_size;
32579e029689SAlberto Garcia     nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t));
32580e4271b7SHu Tao     meta_size += nl1e * sizeof(uint64_t);
32590e4271b7SHu Tao 
32607c5bcc42SStefan Hajnoczi     /* total size of refcount table and blocks */
32617c5bcc42SStefan Hajnoczi     meta_size += qcow2_refcount_metadata_size(
32627c5bcc42SStefan Hajnoczi             (meta_size + aligned_total_size) / cluster_size,
326312cc30a8SMax Reitz             cluster_size, refcount_order, false, NULL);
32640e4271b7SHu Tao 
326595c67e3bSStefan Hajnoczi     return meta_size + aligned_total_size;
326695c67e3bSStefan Hajnoczi }
326795c67e3bSStefan Hajnoczi 
326829ca9e45SKevin Wolf static bool validate_cluster_size(size_t cluster_size, Error **errp)
326995c67e3bSStefan Hajnoczi {
327029ca9e45SKevin Wolf     int cluster_bits = ctz32(cluster_size);
327195c67e3bSStefan Hajnoczi     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
327295c67e3bSStefan Hajnoczi         (1 << cluster_bits) != cluster_size)
327395c67e3bSStefan Hajnoczi     {
327495c67e3bSStefan Hajnoczi         error_setg(errp, "Cluster size must be a power of two between %d and "
327595c67e3bSStefan Hajnoczi                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
327629ca9e45SKevin Wolf         return false;
327729ca9e45SKevin Wolf     }
327829ca9e45SKevin Wolf     return true;
327929ca9e45SKevin Wolf }
328029ca9e45SKevin Wolf 
328129ca9e45SKevin Wolf static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
328229ca9e45SKevin Wolf {
328329ca9e45SKevin Wolf     size_t cluster_size;
328429ca9e45SKevin Wolf 
328529ca9e45SKevin Wolf     cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
328629ca9e45SKevin Wolf                                          DEFAULT_CLUSTER_SIZE);
328729ca9e45SKevin Wolf     if (!validate_cluster_size(cluster_size, errp)) {
32880eb4a8c1SStefan Hajnoczi         return 0;
328995c67e3bSStefan Hajnoczi     }
32900eb4a8c1SStefan Hajnoczi     return cluster_size;
32910eb4a8c1SStefan Hajnoczi }
32920eb4a8c1SStefan Hajnoczi 
32930eb4a8c1SStefan Hajnoczi static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
32940eb4a8c1SStefan Hajnoczi {
32950eb4a8c1SStefan Hajnoczi     char *buf;
32960eb4a8c1SStefan Hajnoczi     int ret;
32970eb4a8c1SStefan Hajnoczi 
32980eb4a8c1SStefan Hajnoczi     buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
32990eb4a8c1SStefan Hajnoczi     if (!buf) {
33000eb4a8c1SStefan Hajnoczi         ret = 3; /* default */
33010eb4a8c1SStefan Hajnoczi     } else if (!strcmp(buf, "0.10")) {
33020eb4a8c1SStefan Hajnoczi         ret = 2;
33030eb4a8c1SStefan Hajnoczi     } else if (!strcmp(buf, "1.1")) {
33040eb4a8c1SStefan Hajnoczi         ret = 3;
33050eb4a8c1SStefan Hajnoczi     } else {
33060eb4a8c1SStefan Hajnoczi         error_setg(errp, "Invalid compatibility level: '%s'", buf);
33070eb4a8c1SStefan Hajnoczi         ret = -EINVAL;
33080eb4a8c1SStefan Hajnoczi     }
33090eb4a8c1SStefan Hajnoczi     g_free(buf);
33100eb4a8c1SStefan Hajnoczi     return ret;
33110eb4a8c1SStefan Hajnoczi }
33120eb4a8c1SStefan Hajnoczi 
33130eb4a8c1SStefan Hajnoczi static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
33140eb4a8c1SStefan Hajnoczi                                                 Error **errp)
33150eb4a8c1SStefan Hajnoczi {
33160eb4a8c1SStefan Hajnoczi     uint64_t refcount_bits;
33170eb4a8c1SStefan Hajnoczi 
33180eb4a8c1SStefan Hajnoczi     refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
33190eb4a8c1SStefan Hajnoczi     if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
33200eb4a8c1SStefan Hajnoczi         error_setg(errp, "Refcount width must be a power of two and may not "
33210eb4a8c1SStefan Hajnoczi                    "exceed 64 bits");
33220eb4a8c1SStefan Hajnoczi         return 0;
33230eb4a8c1SStefan Hajnoczi     }
33240eb4a8c1SStefan Hajnoczi 
33250eb4a8c1SStefan Hajnoczi     if (version < 3 && refcount_bits != 16) {
33260eb4a8c1SStefan Hajnoczi         error_setg(errp, "Different refcount widths than 16 bits require "
33270eb4a8c1SStefan Hajnoczi                    "compatibility level 1.1 or above (use compat=1.1 or "
33280eb4a8c1SStefan Hajnoczi                    "greater)");
33290eb4a8c1SStefan Hajnoczi         return 0;
33300eb4a8c1SStefan Hajnoczi     }
33310eb4a8c1SStefan Hajnoczi 
33320eb4a8c1SStefan Hajnoczi     return refcount_bits;
33330eb4a8c1SStefan Hajnoczi }
33340eb4a8c1SStefan Hajnoczi 
3335c274393aSStefan Hajnoczi static int coroutine_fn
333660900b7bSKevin Wolf qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
33370eb4a8c1SStefan Hajnoczi {
333829ca9e45SKevin Wolf     BlockdevCreateOptionsQcow2 *qcow2_opts;
33390eb4a8c1SStefan Hajnoczi     QDict *options;
334095c67e3bSStefan Hajnoczi 
334195c67e3bSStefan Hajnoczi     /*
334295c67e3bSStefan Hajnoczi      * Open the image file and write a minimal qcow2 header.
334395c67e3bSStefan Hajnoczi      *
334495c67e3bSStefan Hajnoczi      * We keep things simple and start with a zero-sized image. We also
334595c67e3bSStefan Hajnoczi      * do without refcount blocks or a L1 table for now. We'll fix the
334695c67e3bSStefan Hajnoczi      * inconsistency later.
334795c67e3bSStefan Hajnoczi      *
334895c67e3bSStefan Hajnoczi      * We do need a refcount table because growing the refcount table means
3349a951a631SEric Blake      * allocating two new refcount blocks - the second of which would be at
335095c67e3bSStefan Hajnoczi      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
335195c67e3bSStefan Hajnoczi      * size for any qcow2 image.
335295c67e3bSStefan Hajnoczi      */
3353e1d74bc6SKevin Wolf     BlockBackend *blk = NULL;
3354e1d74bc6SKevin Wolf     BlockDriverState *bs = NULL;
3355dcc98687SKevin Wolf     BlockDriverState *data_bs = NULL;
335695c67e3bSStefan Hajnoczi     QCowHeader *header;
335729ca9e45SKevin Wolf     size_t cluster_size;
335829ca9e45SKevin Wolf     int version;
335929ca9e45SKevin Wolf     int refcount_order;
336095c67e3bSStefan Hajnoczi     uint64_t* refcount_table;
336195c67e3bSStefan Hajnoczi     int ret;
3362572ad978SDenis Plotnikov     uint8_t compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
336395c67e3bSStefan Hajnoczi 
336429ca9e45SKevin Wolf     assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
336529ca9e45SKevin Wolf     qcow2_opts = &create_options->u.qcow2;
336629ca9e45SKevin Wolf 
3367e1d74bc6SKevin Wolf     bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp);
3368e1d74bc6SKevin Wolf     if (bs == NULL) {
3369e1d74bc6SKevin Wolf         return -EIO;
3370e1d74bc6SKevin Wolf     }
3371e1d74bc6SKevin Wolf 
3372e1d74bc6SKevin Wolf     /* Validate options and set default values */
337329ca9e45SKevin Wolf     if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
33743afea402SAlberto Garcia         error_setg(errp, "Image size must be a multiple of %u bytes",
33753afea402SAlberto Garcia                    (unsigned) BDRV_SECTOR_SIZE);
337629ca9e45SKevin Wolf         ret = -EINVAL;
337729ca9e45SKevin Wolf         goto out;
337829ca9e45SKevin Wolf     }
337929ca9e45SKevin Wolf 
338029ca9e45SKevin Wolf     if (qcow2_opts->has_version) {
338129ca9e45SKevin Wolf         switch (qcow2_opts->version) {
338229ca9e45SKevin Wolf         case BLOCKDEV_QCOW2_VERSION_V2:
338329ca9e45SKevin Wolf             version = 2;
338429ca9e45SKevin Wolf             break;
338529ca9e45SKevin Wolf         case BLOCKDEV_QCOW2_VERSION_V3:
338629ca9e45SKevin Wolf             version = 3;
338729ca9e45SKevin Wolf             break;
338829ca9e45SKevin Wolf         default:
338929ca9e45SKevin Wolf             g_assert_not_reached();
339029ca9e45SKevin Wolf         }
339129ca9e45SKevin Wolf     } else {
339229ca9e45SKevin Wolf         version = 3;
339329ca9e45SKevin Wolf     }
339429ca9e45SKevin Wolf 
339529ca9e45SKevin Wolf     if (qcow2_opts->has_cluster_size) {
339629ca9e45SKevin Wolf         cluster_size = qcow2_opts->cluster_size;
339729ca9e45SKevin Wolf     } else {
339829ca9e45SKevin Wolf         cluster_size = DEFAULT_CLUSTER_SIZE;
339929ca9e45SKevin Wolf     }
340029ca9e45SKevin Wolf 
340129ca9e45SKevin Wolf     if (!validate_cluster_size(cluster_size, errp)) {
3402e1d74bc6SKevin Wolf         ret = -EINVAL;
3403e1d74bc6SKevin Wolf         goto out;
340429ca9e45SKevin Wolf     }
340529ca9e45SKevin Wolf 
340629ca9e45SKevin Wolf     if (!qcow2_opts->has_preallocation) {
340729ca9e45SKevin Wolf         qcow2_opts->preallocation = PREALLOC_MODE_OFF;
340829ca9e45SKevin Wolf     }
340929ca9e45SKevin Wolf     if (qcow2_opts->has_backing_file &&
341029ca9e45SKevin Wolf         qcow2_opts->preallocation != PREALLOC_MODE_OFF)
341129ca9e45SKevin Wolf     {
341229ca9e45SKevin Wolf         error_setg(errp, "Backing file and preallocation cannot be used at "
341329ca9e45SKevin Wolf                    "the same time");
3414e1d74bc6SKevin Wolf         ret = -EINVAL;
3415e1d74bc6SKevin Wolf         goto out;
341629ca9e45SKevin Wolf     }
341729ca9e45SKevin Wolf     if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) {
341829ca9e45SKevin Wolf         error_setg(errp, "Backing format cannot be used without backing file");
3419e1d74bc6SKevin Wolf         ret = -EINVAL;
3420e1d74bc6SKevin Wolf         goto out;
342129ca9e45SKevin Wolf     }
342229ca9e45SKevin Wolf 
342329ca9e45SKevin Wolf     if (!qcow2_opts->has_lazy_refcounts) {
342429ca9e45SKevin Wolf         qcow2_opts->lazy_refcounts = false;
342529ca9e45SKevin Wolf     }
342629ca9e45SKevin Wolf     if (version < 3 && qcow2_opts->lazy_refcounts) {
342729ca9e45SKevin Wolf         error_setg(errp, "Lazy refcounts only supported with compatibility "
3428b76b4f60SKevin Wolf                    "level 1.1 and above (use version=v3 or greater)");
3429e1d74bc6SKevin Wolf         ret = -EINVAL;
3430e1d74bc6SKevin Wolf         goto out;
343129ca9e45SKevin Wolf     }
343229ca9e45SKevin Wolf 
343329ca9e45SKevin Wolf     if (!qcow2_opts->has_refcount_bits) {
343429ca9e45SKevin Wolf         qcow2_opts->refcount_bits = 16;
343529ca9e45SKevin Wolf     }
343629ca9e45SKevin Wolf     if (qcow2_opts->refcount_bits > 64 ||
343729ca9e45SKevin Wolf         !is_power_of_2(qcow2_opts->refcount_bits))
343829ca9e45SKevin Wolf     {
343929ca9e45SKevin Wolf         error_setg(errp, "Refcount width must be a power of two and may not "
344029ca9e45SKevin Wolf                    "exceed 64 bits");
3441e1d74bc6SKevin Wolf         ret = -EINVAL;
3442e1d74bc6SKevin Wolf         goto out;
344329ca9e45SKevin Wolf     }
344429ca9e45SKevin Wolf     if (version < 3 && qcow2_opts->refcount_bits != 16) {
344529ca9e45SKevin Wolf         error_setg(errp, "Different refcount widths than 16 bits require "
3446b76b4f60SKevin Wolf                    "compatibility level 1.1 or above (use version=v3 or "
344729ca9e45SKevin Wolf                    "greater)");
3448e1d74bc6SKevin Wolf         ret = -EINVAL;
3449e1d74bc6SKevin Wolf         goto out;
345029ca9e45SKevin Wolf     }
345129ca9e45SKevin Wolf     refcount_order = ctz32(qcow2_opts->refcount_bits);
345229ca9e45SKevin Wolf 
34536c3944dcSKevin Wolf     if (qcow2_opts->data_file_raw && !qcow2_opts->data_file) {
34546c3944dcSKevin Wolf         error_setg(errp, "data-file-raw requires data-file");
34556c3944dcSKevin Wolf         ret = -EINVAL;
34566c3944dcSKevin Wolf         goto out;
34576c3944dcSKevin Wolf     }
34586c3944dcSKevin Wolf     if (qcow2_opts->data_file_raw && qcow2_opts->has_backing_file) {
34596c3944dcSKevin Wolf         error_setg(errp, "Backing file and data-file-raw cannot be used at "
34606c3944dcSKevin Wolf                    "the same time");
34616c3944dcSKevin Wolf         ret = -EINVAL;
34626c3944dcSKevin Wolf         goto out;
34636c3944dcSKevin Wolf     }
34646c3944dcSKevin Wolf 
3465dcc98687SKevin Wolf     if (qcow2_opts->data_file) {
3466dcc98687SKevin Wolf         if (version < 3) {
3467dcc98687SKevin Wolf             error_setg(errp, "External data files are only supported with "
3468dcc98687SKevin Wolf                        "compatibility level 1.1 and above (use version=v3 or "
3469dcc98687SKevin Wolf                        "greater)");
3470dcc98687SKevin Wolf             ret = -EINVAL;
3471dcc98687SKevin Wolf             goto out;
3472dcc98687SKevin Wolf         }
3473dcc98687SKevin Wolf         data_bs = bdrv_open_blockdev_ref(qcow2_opts->data_file, errp);
3474a0cf8363SKevin Wolf         if (data_bs == NULL) {
3475dcc98687SKevin Wolf             ret = -EIO;
3476dcc98687SKevin Wolf             goto out;
3477dcc98687SKevin Wolf         }
3478dcc98687SKevin Wolf     }
347929ca9e45SKevin Wolf 
3480572ad978SDenis Plotnikov     if (qcow2_opts->has_compression_type &&
3481572ad978SDenis Plotnikov         qcow2_opts->compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) {
3482572ad978SDenis Plotnikov 
3483572ad978SDenis Plotnikov         ret = -EINVAL;
3484572ad978SDenis Plotnikov 
3485572ad978SDenis Plotnikov         if (version < 3) {
3486572ad978SDenis Plotnikov             error_setg(errp, "Non-zlib compression type is only supported with "
3487572ad978SDenis Plotnikov                        "compatibility level 1.1 and above (use version=v3 or "
3488572ad978SDenis Plotnikov                        "greater)");
3489572ad978SDenis Plotnikov             goto out;
3490572ad978SDenis Plotnikov         }
3491572ad978SDenis Plotnikov 
3492572ad978SDenis Plotnikov         switch (qcow2_opts->compression_type) {
3493d298ac10SDenis Plotnikov #ifdef CONFIG_ZSTD
3494d298ac10SDenis Plotnikov         case QCOW2_COMPRESSION_TYPE_ZSTD:
3495d298ac10SDenis Plotnikov             break;
3496d298ac10SDenis Plotnikov #endif
3497572ad978SDenis Plotnikov         default:
3498572ad978SDenis Plotnikov             error_setg(errp, "Unknown compression type");
3499572ad978SDenis Plotnikov             goto out;
3500572ad978SDenis Plotnikov         }
3501572ad978SDenis Plotnikov 
3502572ad978SDenis Plotnikov         compression_type = qcow2_opts->compression_type;
3503572ad978SDenis Plotnikov     }
3504572ad978SDenis Plotnikov 
350529ca9e45SKevin Wolf     /* Create BlockBackend to write to the image */
3506a3aeeab5SEric Blake     blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
3507a3aeeab5SEric Blake                           errp);
3508a3aeeab5SEric Blake     if (!blk) {
3509a3aeeab5SEric Blake         ret = -EPERM;
3510cbf2b7c4SKevin Wolf         goto out;
3511a9420734SKevin Wolf     }
351223588797SKevin Wolf     blk_set_allow_write_beyond_eof(blk, true);
351323588797SKevin Wolf 
3514a9420734SKevin Wolf     /* Write the header */
3515f8413b3cSKevin Wolf     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
3516f8413b3cSKevin Wolf     header = g_malloc0(cluster_size);
3517f8413b3cSKevin Wolf     *header = (QCowHeader) {
3518f8413b3cSKevin Wolf         .magic                      = cpu_to_be32(QCOW_MAGIC),
3519f8413b3cSKevin Wolf         .version                    = cpu_to_be32(version),
35200eb4a8c1SStefan Hajnoczi         .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
3521f8413b3cSKevin Wolf         .size                       = cpu_to_be64(0),
3522f8413b3cSKevin Wolf         .l1_table_offset            = cpu_to_be64(0),
3523f8413b3cSKevin Wolf         .l1_size                    = cpu_to_be32(0),
3524f8413b3cSKevin Wolf         .refcount_table_offset      = cpu_to_be64(cluster_size),
3525f8413b3cSKevin Wolf         .refcount_table_clusters    = cpu_to_be32(1),
3526bd4b167fSMax Reitz         .refcount_order             = cpu_to_be32(refcount_order),
3527572ad978SDenis Plotnikov         /* don't deal with endianness since compression_type is 1 byte long */
3528572ad978SDenis Plotnikov         .compression_type           = compression_type,
3529f8413b3cSKevin Wolf         .header_length              = cpu_to_be32(sizeof(*header)),
3530f8413b3cSKevin Wolf     };
3531a9420734SKevin Wolf 
3532b25b387fSDaniel P. Berrange     /* We'll update this to correct value later */
3533f8413b3cSKevin Wolf     header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
3534a9420734SKevin Wolf 
353529ca9e45SKevin Wolf     if (qcow2_opts->lazy_refcounts) {
3536f8413b3cSKevin Wolf         header->compatible_features |=
3537bfe8043eSStefan Hajnoczi             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
3538bfe8043eSStefan Hajnoczi     }
3539dcc98687SKevin Wolf     if (data_bs) {
3540dcc98687SKevin Wolf         header->incompatible_features |=
3541dcc98687SKevin Wolf             cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE);
3542dcc98687SKevin Wolf     }
35436c3944dcSKevin Wolf     if (qcow2_opts->data_file_raw) {
35446c3944dcSKevin Wolf         header->autoclear_features |=
35456c3944dcSKevin Wolf             cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW);
35466c3944dcSKevin Wolf     }
3547572ad978SDenis Plotnikov     if (compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) {
3548572ad978SDenis Plotnikov         header->incompatible_features |=
3549572ad978SDenis Plotnikov             cpu_to_be64(QCOW2_INCOMPAT_COMPRESSION);
3550572ad978SDenis Plotnikov     }
3551bfe8043eSStefan Hajnoczi 
35528341f00dSEric Blake     ret = blk_pwrite(blk, 0, header, cluster_size, 0);
3553f8413b3cSKevin Wolf     g_free(header);
3554a9420734SKevin Wolf     if (ret < 0) {
35553ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not write qcow2 header");
3556a9420734SKevin Wolf         goto out;
3557a9420734SKevin Wolf     }
3558a9420734SKevin Wolf 
3559b106ad91SKevin Wolf     /* Write a refcount table with one refcount block */
3560b106ad91SKevin Wolf     refcount_table = g_malloc0(2 * cluster_size);
3561b106ad91SKevin Wolf     refcount_table[0] = cpu_to_be64(2 * cluster_size);
35628341f00dSEric Blake     ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
35637267c094SAnthony Liguori     g_free(refcount_table);
3564a9420734SKevin Wolf 
3565a9420734SKevin Wolf     if (ret < 0) {
35663ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not write refcount table");
3567a9420734SKevin Wolf         goto out;
3568a9420734SKevin Wolf     }
3569a9420734SKevin Wolf 
357023588797SKevin Wolf     blk_unref(blk);
357123588797SKevin Wolf     blk = NULL;
3572a9420734SKevin Wolf 
3573a9420734SKevin Wolf     /*
3574a9420734SKevin Wolf      * And now open the image and make it consistent first (i.e. increase the
3575a9420734SKevin Wolf      * refcount of the cluster that is occupied by the header and the refcount
3576a9420734SKevin Wolf      * table)
3577a9420734SKevin Wolf      */
3578e6641719SMax Reitz     options = qdict_new();
357946f5ac20SEric Blake     qdict_put_str(options, "driver", "qcow2");
3580cbf2b7c4SKevin Wolf     qdict_put_str(options, "file", bs->node_name);
3581dcc98687SKevin Wolf     if (data_bs) {
3582dcc98687SKevin Wolf         qdict_put_str(options, "data-file", data_bs->node_name);
3583dcc98687SKevin Wolf     }
3584cbf2b7c4SKevin Wolf     blk = blk_new_open(NULL, NULL, options,
358555880601SKevin Wolf                        BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
3586af175e85SMarkus Armbruster                        errp);
358723588797SKevin Wolf     if (blk == NULL) {
358823588797SKevin Wolf         ret = -EIO;
3589a9420734SKevin Wolf         goto out;
3590a9420734SKevin Wolf     }
3591a9420734SKevin Wolf 
359223588797SKevin Wolf     ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
3593a9420734SKevin Wolf     if (ret < 0) {
35943ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
35953ef6c40aSMax Reitz                          "header and refcount table");
3596a9420734SKevin Wolf         goto out;
3597a9420734SKevin Wolf 
3598a9420734SKevin Wolf     } else if (ret != 0) {
3599a9420734SKevin Wolf         error_report("Huh, first cluster in empty image is already in use?");
3600a9420734SKevin Wolf         abort();
3601a9420734SKevin Wolf     }
3602a9420734SKevin Wolf 
36039b890bdcSKevin Wolf     /* Set the external data file if necessary */
36049b890bdcSKevin Wolf     if (data_bs) {
36059b890bdcSKevin Wolf         BDRVQcow2State *s = blk_bs(blk)->opaque;
36069b890bdcSKevin Wolf         s->image_data_file = g_strdup(data_bs->filename);
36079b890bdcSKevin Wolf     }
36089b890bdcSKevin Wolf 
3609b527c9b3SKevin Wolf     /* Create a full header (including things like feature table) */
361023588797SKevin Wolf     ret = qcow2_update_header(blk_bs(blk));
3611b527c9b3SKevin Wolf     if (ret < 0) {
3612b527c9b3SKevin Wolf         error_setg_errno(errp, -ret, "Could not update qcow2 header");
3613b527c9b3SKevin Wolf         goto out;
3614b527c9b3SKevin Wolf     }
3615b527c9b3SKevin Wolf 
3616a9420734SKevin Wolf     /* Okay, now that we have a valid image, let's give it the right size */
3617c80d8b06SMax Reitz     ret = blk_truncate(blk, qcow2_opts->size, false, qcow2_opts->preallocation,
36188c6242b6SKevin Wolf                        0, errp);
3619a9420734SKevin Wolf     if (ret < 0) {
3620ed3d2ec9SMax Reitz         error_prepend(errp, "Could not resize image: ");
3621a9420734SKevin Wolf         goto out;
3622a9420734SKevin Wolf     }
3623a9420734SKevin Wolf 
3624a9420734SKevin Wolf     /* Want a backing file? There you go. */
362529ca9e45SKevin Wolf     if (qcow2_opts->has_backing_file) {
362629ca9e45SKevin Wolf         const char *backing_format = NULL;
362729ca9e45SKevin Wolf 
362829ca9e45SKevin Wolf         if (qcow2_opts->has_backing_fmt) {
362929ca9e45SKevin Wolf             backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
363029ca9e45SKevin Wolf         }
363129ca9e45SKevin Wolf 
363229ca9e45SKevin Wolf         ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
3633e54ee1b3SEric Blake                                        backing_format, false);
3634a9420734SKevin Wolf         if (ret < 0) {
36353ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
363629ca9e45SKevin Wolf                              "with format '%s'", qcow2_opts->backing_file,
363729ca9e45SKevin Wolf                              backing_format);
3638a9420734SKevin Wolf             goto out;
3639a9420734SKevin Wolf         }
3640a9420734SKevin Wolf     }
3641a9420734SKevin Wolf 
3642b25b387fSDaniel P. Berrange     /* Want encryption? There you go. */
364360900b7bSKevin Wolf     if (qcow2_opts->has_encrypt) {
364460900b7bSKevin Wolf         ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
3645b25b387fSDaniel P. Berrange         if (ret < 0) {
3646b25b387fSDaniel P. Berrange             goto out;
3647b25b387fSDaniel P. Berrange         }
3648b25b387fSDaniel P. Berrange     }
3649b25b387fSDaniel P. Berrange 
365023588797SKevin Wolf     blk_unref(blk);
365123588797SKevin Wolf     blk = NULL;
3652ba2ab2f2SMax Reitz 
3653b25b387fSDaniel P. Berrange     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3654b25b387fSDaniel P. Berrange      * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3655b25b387fSDaniel P. Berrange      * have to setup decryption context. We're not doing any I/O on the top
3656b25b387fSDaniel P. Berrange      * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3657b25b387fSDaniel P. Berrange      * not have effect.
3658b25b387fSDaniel P. Berrange      */
3659e6641719SMax Reitz     options = qdict_new();
366046f5ac20SEric Blake     qdict_put_str(options, "driver", "qcow2");
3661cbf2b7c4SKevin Wolf     qdict_put_str(options, "file", bs->node_name);
3662dcc98687SKevin Wolf     if (data_bs) {
3663dcc98687SKevin Wolf         qdict_put_str(options, "data-file", data_bs->node_name);
3664dcc98687SKevin Wolf     }
3665cbf2b7c4SKevin Wolf     blk = blk_new_open(NULL, NULL, options,
3666b25b387fSDaniel P. Berrange                        BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3667af175e85SMarkus Armbruster                        errp);
366823588797SKevin Wolf     if (blk == NULL) {
366923588797SKevin Wolf         ret = -EIO;
3670ba2ab2f2SMax Reitz         goto out;
3671ba2ab2f2SMax Reitz     }
3672ba2ab2f2SMax Reitz 
3673a9420734SKevin Wolf     ret = 0;
3674a9420734SKevin Wolf out:
367523588797SKevin Wolf     blk_unref(blk);
3676e1d74bc6SKevin Wolf     bdrv_unref(bs);
3677dcc98687SKevin Wolf     bdrv_unref(data_bs);
3678a9420734SKevin Wolf     return ret;
3679a9420734SKevin Wolf }
3680de5f3f40SKevin Wolf 
3681b92902dfSMaxim Levitsky static int coroutine_fn qcow2_co_create_opts(BlockDriver *drv,
3682b92902dfSMaxim Levitsky                                              const char *filename,
3683b92902dfSMaxim Levitsky                                              QemuOpts *opts,
3684efc75e2aSStefan Hajnoczi                                              Error **errp)
3685de5f3f40SKevin Wolf {
3686b76b4f60SKevin Wolf     BlockdevCreateOptions *create_options = NULL;
368792adf9dbSMarkus Armbruster     QDict *qdict;
3688b76b4f60SKevin Wolf     Visitor *v;
3689cbf2b7c4SKevin Wolf     BlockDriverState *bs = NULL;
36909b890bdcSKevin Wolf     BlockDriverState *data_bs = NULL;
3691b76b4f60SKevin Wolf     const char *val;
36923ef6c40aSMax Reitz     int ret;
3693de5f3f40SKevin Wolf 
3694b76b4f60SKevin Wolf     /* Only the keyval visitor supports the dotted syntax needed for
3695b76b4f60SKevin Wolf      * encryption, so go through a QDict before getting a QAPI type. Ignore
3696b76b4f60SKevin Wolf      * options meant for the protocol layer so that the visitor doesn't
3697b76b4f60SKevin Wolf      * complain. */
3698b76b4f60SKevin Wolf     qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3699b76b4f60SKevin Wolf                                         true);
3700b76b4f60SKevin Wolf 
3701b76b4f60SKevin Wolf     /* Handle encryption options */
3702b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3703b76b4f60SKevin Wolf     if (val && !strcmp(val, "on")) {
3704b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3705b76b4f60SKevin Wolf     } else if (val && !strcmp(val, "off")) {
3706b76b4f60SKevin Wolf         qdict_del(qdict, BLOCK_OPT_ENCRYPT);
370729ca9e45SKevin Wolf     }
370860900b7bSKevin Wolf 
3709b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3710b76b4f60SKevin Wolf     if (val && !strcmp(val, "aes")) {
3711b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
371260900b7bSKevin Wolf     }
371360900b7bSKevin Wolf 
3714b76b4f60SKevin Wolf     /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3715b76b4f60SKevin Wolf      * version=v2/v3 below. */
3716b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3717b76b4f60SKevin Wolf     if (val && !strcmp(val, "0.10")) {
3718b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3719b76b4f60SKevin Wolf     } else if (val && !strcmp(val, "1.1")) {
3720b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3721b76b4f60SKevin Wolf     }
3722b76b4f60SKevin Wolf 
3723b76b4f60SKevin Wolf     /* Change legacy command line options into QMP ones */
3724b76b4f60SKevin Wolf     static const QDictRenames opt_renames[] = {
3725b76b4f60SKevin Wolf         { BLOCK_OPT_BACKING_FILE,       "backing-file" },
3726b76b4f60SKevin Wolf         { BLOCK_OPT_BACKING_FMT,        "backing-fmt" },
3727b76b4f60SKevin Wolf         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
3728b76b4f60SKevin Wolf         { BLOCK_OPT_LAZY_REFCOUNTS,     "lazy-refcounts" },
3729b76b4f60SKevin Wolf         { BLOCK_OPT_REFCOUNT_BITS,      "refcount-bits" },
3730b76b4f60SKevin Wolf         { BLOCK_OPT_ENCRYPT,            BLOCK_OPT_ENCRYPT_FORMAT },
3731b76b4f60SKevin Wolf         { BLOCK_OPT_COMPAT_LEVEL,       "version" },
37326c3944dcSKevin Wolf         { BLOCK_OPT_DATA_FILE_RAW,      "data-file-raw" },
3733572ad978SDenis Plotnikov         { BLOCK_OPT_COMPRESSION_TYPE,   "compression-type" },
3734b76b4f60SKevin Wolf         { NULL, NULL },
3735b76b4f60SKevin Wolf     };
3736b76b4f60SKevin Wolf 
3737b76b4f60SKevin Wolf     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
37380eb4a8c1SStefan Hajnoczi         ret = -EINVAL;
37390eb4a8c1SStefan Hajnoczi         goto finish;
37400eb4a8c1SStefan Hajnoczi     }
3741bd4b167fSMax Reitz 
3742cbf2b7c4SKevin Wolf     /* Create and open the file (protocol layer) */
3743cbf2b7c4SKevin Wolf     ret = bdrv_create_file(filename, opts, errp);
3744cbf2b7c4SKevin Wolf     if (ret < 0) {
3745cbf2b7c4SKevin Wolf         goto finish;
3746cbf2b7c4SKevin Wolf     }
3747cbf2b7c4SKevin Wolf 
3748cbf2b7c4SKevin Wolf     bs = bdrv_open(filename, NULL, NULL,
3749cbf2b7c4SKevin Wolf                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3750cbf2b7c4SKevin Wolf     if (bs == NULL) {
3751cbf2b7c4SKevin Wolf         ret = -EIO;
3752cbf2b7c4SKevin Wolf         goto finish;
3753cbf2b7c4SKevin Wolf     }
3754cbf2b7c4SKevin Wolf 
37559b890bdcSKevin Wolf     /* Create and open an external data file (protocol layer) */
37569b890bdcSKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE);
37579b890bdcSKevin Wolf     if (val) {
37589b890bdcSKevin Wolf         ret = bdrv_create_file(val, opts, errp);
37599b890bdcSKevin Wolf         if (ret < 0) {
37609b890bdcSKevin Wolf             goto finish;
37619b890bdcSKevin Wolf         }
37629b890bdcSKevin Wolf 
37639b890bdcSKevin Wolf         data_bs = bdrv_open(val, NULL, NULL,
37649b890bdcSKevin Wolf                             BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
37659b890bdcSKevin Wolf                             errp);
37669b890bdcSKevin Wolf         if (data_bs == NULL) {
37679b890bdcSKevin Wolf             ret = -EIO;
37689b890bdcSKevin Wolf             goto finish;
37699b890bdcSKevin Wolf         }
37709b890bdcSKevin Wolf 
37719b890bdcSKevin Wolf         qdict_del(qdict, BLOCK_OPT_DATA_FILE);
37729b890bdcSKevin Wolf         qdict_put_str(qdict, "data-file", data_bs->node_name);
37739b890bdcSKevin Wolf     }
37749b890bdcSKevin Wolf 
3775b76b4f60SKevin Wolf     /* Set 'driver' and 'node' options */
3776b76b4f60SKevin Wolf     qdict_put_str(qdict, "driver", "qcow2");
3777b76b4f60SKevin Wolf     qdict_put_str(qdict, "file", bs->node_name);
3778b76b4f60SKevin Wolf 
3779b76b4f60SKevin Wolf     /* Now get the QAPI type BlockdevCreateOptions */
3780af91062eSMarkus Armbruster     v = qobject_input_visitor_new_flat_confused(qdict, errp);
3781af91062eSMarkus Armbruster     if (!v) {
3782b76b4f60SKevin Wolf         ret = -EINVAL;
3783b76b4f60SKevin Wolf         goto finish;
3784b76b4f60SKevin Wolf     }
3785b76b4f60SKevin Wolf 
3786b11a093cSMarkus Armbruster     visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
3787b76b4f60SKevin Wolf     visit_free(v);
3788b11a093cSMarkus Armbruster     if (!create_options) {
3789b76b4f60SKevin Wolf         ret = -EINVAL;
3790b76b4f60SKevin Wolf         goto finish;
3791b76b4f60SKevin Wolf     }
3792b76b4f60SKevin Wolf 
3793b76b4f60SKevin Wolf     /* Silently round up size */
3794b76b4f60SKevin Wolf     create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
3795b76b4f60SKevin Wolf                                             BDRV_SECTOR_SIZE);
3796b76b4f60SKevin Wolf 
3797cbf2b7c4SKevin Wolf     /* Create the qcow2 image (format layer) */
3798b76b4f60SKevin Wolf     ret = qcow2_co_create(create_options, errp);
3799cbf2b7c4SKevin Wolf     if (ret < 0) {
3800cbf2b7c4SKevin Wolf         goto finish;
3801cbf2b7c4SKevin Wolf     }
38021bd0e2d1SChunyan Liu 
3803b76b4f60SKevin Wolf     ret = 0;
38041bd0e2d1SChunyan Liu finish:
3805cb3e7f08SMarc-André Lureau     qobject_unref(qdict);
3806cbf2b7c4SKevin Wolf     bdrv_unref(bs);
38079b890bdcSKevin Wolf     bdrv_unref(data_bs);
3808b76b4f60SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
38093ef6c40aSMax Reitz     return ret;
3810de5f3f40SKevin Wolf }
3811de5f3f40SKevin Wolf 
38122928abceSDenis V. Lunev 
3813f06f6b66SEric Blake static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
38142928abceSDenis V. Lunev {
381531826642SEric Blake     int64_t nr;
381631826642SEric Blake     int res;
3817f06f6b66SEric Blake 
3818f06f6b66SEric Blake     /* Clamp to image length, before checking status of underlying sectors */
38198cbf74b2SEric Blake     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
38208cbf74b2SEric Blake         bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
3821fbaa6bb3SEric Blake     }
3822fbaa6bb3SEric Blake 
3823f06f6b66SEric Blake     if (!bytes) {
3824ebb718a5SEric Blake         return true;
38252928abceSDenis V. Lunev     }
38268cbf74b2SEric Blake     res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
382731826642SEric Blake     return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
38282928abceSDenis V. Lunev }
38292928abceSDenis V. Lunev 
38305544b59fSEric Blake static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
3831f5a5ca79SManos Pitsidianakis     int64_t offset, int bytes, BdrvRequestFlags flags)
3832621f0589SKevin Wolf {
3833621f0589SKevin Wolf     int ret;
3834ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3835621f0589SKevin Wolf 
3836*a6841a2dSAlberto Garcia     uint32_t head = offset_into_subcluster(s, offset);
3837*a6841a2dSAlberto Garcia     uint32_t tail = ROUND_UP(offset + bytes, s->subcluster_size) -
3838*a6841a2dSAlberto Garcia         (offset + bytes);
38392928abceSDenis V. Lunev 
3840f5a5ca79SManos Pitsidianakis     trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3841f5a5ca79SManos Pitsidianakis     if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
3842fbaa6bb3SEric Blake         tail = 0;
3843fbaa6bb3SEric Blake     }
38445a64e942SDenis V. Lunev 
3845ebb718a5SEric Blake     if (head || tail) {
3846ebb718a5SEric Blake         uint64_t off;
3847ecfe1863SKevin Wolf         unsigned int nr;
384810dabdc5SAlberto Garcia         QCow2SubclusterType type;
38492928abceSDenis V. Lunev 
3850*a6841a2dSAlberto Garcia         assert(head + bytes + tail <= s->subcluster_size);
38512928abceSDenis V. Lunev 
3852ebb718a5SEric Blake         /* check whether remainder of cluster already reads as zero */
3853f06f6b66SEric Blake         if (!(is_zero(bs, offset - head, head) &&
3854*a6841a2dSAlberto Garcia               is_zero(bs, offset + bytes, tail))) {
3855621f0589SKevin Wolf             return -ENOTSUP;
3856621f0589SKevin Wolf         }
3857621f0589SKevin Wolf 
3858621f0589SKevin Wolf         qemu_co_mutex_lock(&s->lock);
38592928abceSDenis V. Lunev         /* We can have new write after previous check */
3860*a6841a2dSAlberto Garcia         offset -= head;
3861*a6841a2dSAlberto Garcia         bytes = s->subcluster_size;
3862*a6841a2dSAlberto Garcia         nr = s->subcluster_size;
3863ca4a0bb8SAlberto Garcia         ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type);
3864ca4a0bb8SAlberto Garcia         if (ret < 0 ||
386510dabdc5SAlberto Garcia             (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN &&
386697490a14SAlberto Garcia              type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC &&
386710dabdc5SAlberto Garcia              type != QCOW2_SUBCLUSTER_ZERO_PLAIN &&
386810dabdc5SAlberto Garcia              type != QCOW2_SUBCLUSTER_ZERO_ALLOC)) {
38692928abceSDenis V. Lunev             qemu_co_mutex_unlock(&s->lock);
38702928abceSDenis V. Lunev             return -ENOTSUP;
38712928abceSDenis V. Lunev         }
38722928abceSDenis V. Lunev     } else {
38732928abceSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
38742928abceSDenis V. Lunev     }
38752928abceSDenis V. Lunev 
3876f5a5ca79SManos Pitsidianakis     trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
38775a64e942SDenis V. Lunev 
3878*a6841a2dSAlberto Garcia     /* Whatever is left can use real zero subclusters */
3879*a6841a2dSAlberto Garcia     ret = qcow2_subcluster_zeroize(bs, offset, bytes, flags);
3880621f0589SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
3881621f0589SKevin Wolf 
3882621f0589SKevin Wolf     return ret;
3883621f0589SKevin Wolf }
3884621f0589SKevin Wolf 
388582e8a788SEric Blake static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
3886f5a5ca79SManos Pitsidianakis                                           int64_t offset, int bytes)
38875ea929e3SKevin Wolf {
38886db39ae2SPaolo Bonzini     int ret;
3889ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
38906db39ae2SPaolo Bonzini 
389180f5c011SAlberto Garcia     /* If the image does not support QCOW_OFLAG_ZERO then discarding
389280f5c011SAlberto Garcia      * clusters could expose stale data from the backing file. */
389380f5c011SAlberto Garcia     if (s->qcow_version < 3 && bs->backing) {
389480f5c011SAlberto Garcia         return -ENOTSUP;
389580f5c011SAlberto Garcia     }
389680f5c011SAlberto Garcia 
3897f5a5ca79SManos Pitsidianakis     if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3898f5a5ca79SManos Pitsidianakis         assert(bytes < s->cluster_size);
3899048c5fd1SEric Blake         /* Ignore partial clusters, except for the special case of the
3900048c5fd1SEric Blake          * complete partial cluster at the end of an unaligned file */
3901048c5fd1SEric Blake         if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
3902f5a5ca79SManos Pitsidianakis             offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
390349228d1eSEric Blake             return -ENOTSUP;
390449228d1eSEric Blake         }
3905048c5fd1SEric Blake     }
390649228d1eSEric Blake 
39076db39ae2SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
3908f5a5ca79SManos Pitsidianakis     ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
3909d2cb36afSEric Blake                                 false);
39106db39ae2SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
39116db39ae2SPaolo Bonzini     return ret;
39125ea929e3SKevin Wolf }
39135ea929e3SKevin Wolf 
3914fd9fcd37SFam Zheng static int coroutine_fn
3915fd9fcd37SFam Zheng qcow2_co_copy_range_from(BlockDriverState *bs,
3916fd9fcd37SFam Zheng                          BdrvChild *src, uint64_t src_offset,
3917fd9fcd37SFam Zheng                          BdrvChild *dst, uint64_t dst_offset,
391867b51fb9SVladimir Sementsov-Ogievskiy                          uint64_t bytes, BdrvRequestFlags read_flags,
391967b51fb9SVladimir Sementsov-Ogievskiy                          BdrvRequestFlags write_flags)
3920fd9fcd37SFam Zheng {
3921fd9fcd37SFam Zheng     BDRVQcow2State *s = bs->opaque;
3922fd9fcd37SFam Zheng     int ret;
3923fd9fcd37SFam Zheng     unsigned int cur_bytes; /* number of bytes in current iteration */
3924fd9fcd37SFam Zheng     BdrvChild *child = NULL;
392567b51fb9SVladimir Sementsov-Ogievskiy     BdrvRequestFlags cur_write_flags;
3926fd9fcd37SFam Zheng 
3927fd9fcd37SFam Zheng     assert(!bs->encrypted);
3928fd9fcd37SFam Zheng     qemu_co_mutex_lock(&s->lock);
3929fd9fcd37SFam Zheng 
3930fd9fcd37SFam Zheng     while (bytes != 0) {
3931fd9fcd37SFam Zheng         uint64_t copy_offset = 0;
393210dabdc5SAlberto Garcia         QCow2SubclusterType type;
3933fd9fcd37SFam Zheng         /* prepare next request */
3934fd9fcd37SFam Zheng         cur_bytes = MIN(bytes, INT_MAX);
393567b51fb9SVladimir Sementsov-Ogievskiy         cur_write_flags = write_flags;
3936fd9fcd37SFam Zheng 
3937ca4a0bb8SAlberto Garcia         ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes,
3938ca4a0bb8SAlberto Garcia                                     &copy_offset, &type);
3939fd9fcd37SFam Zheng         if (ret < 0) {
3940fd9fcd37SFam Zheng             goto out;
3941fd9fcd37SFam Zheng         }
3942fd9fcd37SFam Zheng 
3943ca4a0bb8SAlberto Garcia         switch (type) {
394410dabdc5SAlberto Garcia         case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
394597490a14SAlberto Garcia         case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
3946fd9fcd37SFam Zheng             if (bs->backing && bs->backing->bs) {
3947fd9fcd37SFam Zheng                 int64_t backing_length = bdrv_getlength(bs->backing->bs);
3948fd9fcd37SFam Zheng                 if (src_offset >= backing_length) {
394967b51fb9SVladimir Sementsov-Ogievskiy                     cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3950fd9fcd37SFam Zheng                 } else {
3951fd9fcd37SFam Zheng                     child = bs->backing;
3952fd9fcd37SFam Zheng                     cur_bytes = MIN(cur_bytes, backing_length - src_offset);
3953fd9fcd37SFam Zheng                     copy_offset = src_offset;
3954fd9fcd37SFam Zheng                 }
3955fd9fcd37SFam Zheng             } else {
395667b51fb9SVladimir Sementsov-Ogievskiy                 cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3957fd9fcd37SFam Zheng             }
3958fd9fcd37SFam Zheng             break;
3959fd9fcd37SFam Zheng 
396010dabdc5SAlberto Garcia         case QCOW2_SUBCLUSTER_ZERO_PLAIN:
396110dabdc5SAlberto Garcia         case QCOW2_SUBCLUSTER_ZERO_ALLOC:
396267b51fb9SVladimir Sementsov-Ogievskiy             cur_write_flags |= BDRV_REQ_ZERO_WRITE;
3963fd9fcd37SFam Zheng             break;
3964fd9fcd37SFam Zheng 
396510dabdc5SAlberto Garcia         case QCOW2_SUBCLUSTER_COMPRESSED:
3966fd9fcd37SFam Zheng             ret = -ENOTSUP;
3967fd9fcd37SFam Zheng             goto out;
3968fd9fcd37SFam Zheng 
396910dabdc5SAlberto Garcia         case QCOW2_SUBCLUSTER_NORMAL:
3970966b000fSKevin Wolf             child = s->data_file;
3971fd9fcd37SFam Zheng             break;
3972fd9fcd37SFam Zheng 
3973fd9fcd37SFam Zheng         default:
3974fd9fcd37SFam Zheng             abort();
3975fd9fcd37SFam Zheng         }
3976fd9fcd37SFam Zheng         qemu_co_mutex_unlock(&s->lock);
3977fd9fcd37SFam Zheng         ret = bdrv_co_copy_range_from(child,
3978fd9fcd37SFam Zheng                                       copy_offset,
3979fd9fcd37SFam Zheng                                       dst, dst_offset,
398067b51fb9SVladimir Sementsov-Ogievskiy                                       cur_bytes, read_flags, cur_write_flags);
3981fd9fcd37SFam Zheng         qemu_co_mutex_lock(&s->lock);
3982fd9fcd37SFam Zheng         if (ret < 0) {
3983fd9fcd37SFam Zheng             goto out;
3984fd9fcd37SFam Zheng         }
3985fd9fcd37SFam Zheng 
3986fd9fcd37SFam Zheng         bytes -= cur_bytes;
3987fd9fcd37SFam Zheng         src_offset += cur_bytes;
3988fd9fcd37SFam Zheng         dst_offset += cur_bytes;
3989fd9fcd37SFam Zheng     }
3990fd9fcd37SFam Zheng     ret = 0;
3991fd9fcd37SFam Zheng 
3992fd9fcd37SFam Zheng out:
3993fd9fcd37SFam Zheng     qemu_co_mutex_unlock(&s->lock);
3994fd9fcd37SFam Zheng     return ret;
3995fd9fcd37SFam Zheng }
3996fd9fcd37SFam Zheng 
3997fd9fcd37SFam Zheng static int coroutine_fn
3998fd9fcd37SFam Zheng qcow2_co_copy_range_to(BlockDriverState *bs,
3999fd9fcd37SFam Zheng                        BdrvChild *src, uint64_t src_offset,
4000fd9fcd37SFam Zheng                        BdrvChild *dst, uint64_t dst_offset,
400167b51fb9SVladimir Sementsov-Ogievskiy                        uint64_t bytes, BdrvRequestFlags read_flags,
400267b51fb9SVladimir Sementsov-Ogievskiy                        BdrvRequestFlags write_flags)
4003fd9fcd37SFam Zheng {
4004fd9fcd37SFam Zheng     BDRVQcow2State *s = bs->opaque;
4005fd9fcd37SFam Zheng     int offset_in_cluster;
4006fd9fcd37SFam Zheng     int ret;
4007fd9fcd37SFam Zheng     unsigned int cur_bytes; /* number of sectors in current iteration */
4008fd9fcd37SFam Zheng     uint64_t cluster_offset;
4009fd9fcd37SFam Zheng     QCowL2Meta *l2meta = NULL;
4010fd9fcd37SFam Zheng 
4011fd9fcd37SFam Zheng     assert(!bs->encrypted);
4012fd9fcd37SFam Zheng 
4013fd9fcd37SFam Zheng     qemu_co_mutex_lock(&s->lock);
4014fd9fcd37SFam Zheng 
4015fd9fcd37SFam Zheng     while (bytes != 0) {
4016fd9fcd37SFam Zheng 
4017fd9fcd37SFam Zheng         l2meta = NULL;
4018fd9fcd37SFam Zheng 
4019fd9fcd37SFam Zheng         offset_in_cluster = offset_into_cluster(s, dst_offset);
4020fd9fcd37SFam Zheng         cur_bytes = MIN(bytes, INT_MAX);
4021fd9fcd37SFam Zheng 
4022fd9fcd37SFam Zheng         /* TODO:
4023fd9fcd37SFam Zheng          * If src->bs == dst->bs, we could simply copy by incrementing
4024fd9fcd37SFam Zheng          * the refcnt, without copying user data.
4025fd9fcd37SFam Zheng          * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
4026fd9fcd37SFam Zheng         ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
4027fd9fcd37SFam Zheng                                          &cluster_offset, &l2meta);
4028fd9fcd37SFam Zheng         if (ret < 0) {
4029fd9fcd37SFam Zheng             goto fail;
4030fd9fcd37SFam Zheng         }
4031fd9fcd37SFam Zheng 
4032344ffea9SAlberto Garcia         assert(offset_into_cluster(s, cluster_offset) == 0);
4033fd9fcd37SFam Zheng 
4034fd9fcd37SFam Zheng         ret = qcow2_pre_write_overlap_check(bs, 0,
4035966b000fSKevin Wolf                 cluster_offset + offset_in_cluster, cur_bytes, true);
4036fd9fcd37SFam Zheng         if (ret < 0) {
4037fd9fcd37SFam Zheng             goto fail;
4038fd9fcd37SFam Zheng         }
4039fd9fcd37SFam Zheng 
4040fd9fcd37SFam Zheng         qemu_co_mutex_unlock(&s->lock);
4041fd9fcd37SFam Zheng         ret = bdrv_co_copy_range_to(src, src_offset,
4042966b000fSKevin Wolf                                     s->data_file,
4043fd9fcd37SFam Zheng                                     cluster_offset + offset_in_cluster,
404467b51fb9SVladimir Sementsov-Ogievskiy                                     cur_bytes, read_flags, write_flags);
4045fd9fcd37SFam Zheng         qemu_co_mutex_lock(&s->lock);
4046fd9fcd37SFam Zheng         if (ret < 0) {
4047fd9fcd37SFam Zheng             goto fail;
4048fd9fcd37SFam Zheng         }
4049fd9fcd37SFam Zheng 
4050fd9fcd37SFam Zheng         ret = qcow2_handle_l2meta(bs, &l2meta, true);
4051fd9fcd37SFam Zheng         if (ret) {
4052fd9fcd37SFam Zheng             goto fail;
4053fd9fcd37SFam Zheng         }
4054fd9fcd37SFam Zheng 
4055fd9fcd37SFam Zheng         bytes -= cur_bytes;
4056e06f4639SFam Zheng         src_offset += cur_bytes;
4057fd9fcd37SFam Zheng         dst_offset += cur_bytes;
4058fd9fcd37SFam Zheng     }
4059fd9fcd37SFam Zheng     ret = 0;
4060fd9fcd37SFam Zheng 
4061fd9fcd37SFam Zheng fail:
4062fd9fcd37SFam Zheng     qcow2_handle_l2meta(bs, &l2meta, false);
4063fd9fcd37SFam Zheng 
4064fd9fcd37SFam Zheng     qemu_co_mutex_unlock(&s->lock);
4065fd9fcd37SFam Zheng 
4066fd9fcd37SFam Zheng     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
4067fd9fcd37SFam Zheng 
4068fd9fcd37SFam Zheng     return ret;
4069fd9fcd37SFam Zheng }
4070fd9fcd37SFam Zheng 
4071061ca8a3SKevin Wolf static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
4072c80d8b06SMax Reitz                                           bool exact, PreallocMode prealloc,
407392b92799SKevin Wolf                                           BdrvRequestFlags flags, Error **errp)
4074419b19d9SStefan Hajnoczi {
4075ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
407695b98f34SMax Reitz     uint64_t old_length;
40772cf7cfa1SKevin Wolf     int64_t new_l1_size;
40782cf7cfa1SKevin Wolf     int ret;
407945b4949cSLeonid Bloch     QDict *options;
4080419b19d9SStefan Hajnoczi 
4081772d1f97SMax Reitz     if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
4082772d1f97SMax Reitz         prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
4083772d1f97SMax Reitz     {
40848243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
4085977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
40868243ccb7SMax Reitz         return -ENOTSUP;
40878243ccb7SMax Reitz     }
40888243ccb7SMax Reitz 
40893afea402SAlberto Garcia     if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
40903afea402SAlberto Garcia         error_setg(errp, "The new size must be a multiple of %u",
40913afea402SAlberto Garcia                    (unsigned) BDRV_SECTOR_SIZE);
4092419b19d9SStefan Hajnoczi         return -EINVAL;
4093419b19d9SStefan Hajnoczi     }
4094419b19d9SStefan Hajnoczi 
4095061ca8a3SKevin Wolf     qemu_co_mutex_lock(&s->lock);
4096061ca8a3SKevin Wolf 
40977fa140abSEric Blake     /*
40987fa140abSEric Blake      * Even though we store snapshot size for all images, it was not
40997fa140abSEric Blake      * required until v3, so it is not safe to proceed for v2.
41007fa140abSEric Blake      */
41017fa140abSEric Blake     if (s->nb_snapshots && s->qcow_version < 3) {
41027fa140abSEric Blake         error_setg(errp, "Can't resize a v2 image which has snapshots");
4103061ca8a3SKevin Wolf         ret = -ENOTSUP;
4104061ca8a3SKevin Wolf         goto fail;
4105419b19d9SStefan Hajnoczi     }
4106419b19d9SStefan Hajnoczi 
4107ee1244a2SEric Blake     /* See qcow2-bitmap.c for which bitmap scenarios prevent a resize. */
4108d19c6b36SJohn Snow     if (qcow2_truncate_bitmaps_check(bs, errp)) {
4109061ca8a3SKevin Wolf         ret = -ENOTSUP;
4110061ca8a3SKevin Wolf         goto fail;
411188ddffaeSVladimir Sementsov-Ogievskiy     }
411288ddffaeSVladimir Sementsov-Ogievskiy 
4113bd016b91SLeonid Bloch     old_length = bs->total_sectors * BDRV_SECTOR_SIZE;
411446b732cdSPavel Butsykin     new_l1_size = size_to_l1(s, offset);
411595b98f34SMax Reitz 
411695b98f34SMax Reitz     if (offset < old_length) {
4117163bc39dSPavel Butsykin         int64_t last_cluster, old_file_size;
411846b732cdSPavel Butsykin         if (prealloc != PREALLOC_MODE_OFF) {
411946b732cdSPavel Butsykin             error_setg(errp,
412046b732cdSPavel Butsykin                        "Preallocation can't be used for shrinking an image");
4121061ca8a3SKevin Wolf             ret = -EINVAL;
4122061ca8a3SKevin Wolf             goto fail;
4123419b19d9SStefan Hajnoczi         }
4124419b19d9SStefan Hajnoczi 
412546b732cdSPavel Butsykin         ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
412646b732cdSPavel Butsykin                                     old_length - ROUND_UP(offset,
412746b732cdSPavel Butsykin                                                           s->cluster_size),
412846b732cdSPavel Butsykin                                     QCOW2_DISCARD_ALWAYS, true);
412946b732cdSPavel Butsykin         if (ret < 0) {
413046b732cdSPavel Butsykin             error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
4131061ca8a3SKevin Wolf             goto fail;
413246b732cdSPavel Butsykin         }
413346b732cdSPavel Butsykin 
413446b732cdSPavel Butsykin         ret = qcow2_shrink_l1_table(bs, new_l1_size);
413546b732cdSPavel Butsykin         if (ret < 0) {
413646b732cdSPavel Butsykin             error_setg_errno(errp, -ret,
413746b732cdSPavel Butsykin                              "Failed to reduce the number of L2 tables");
4138061ca8a3SKevin Wolf             goto fail;
413946b732cdSPavel Butsykin         }
414046b732cdSPavel Butsykin 
414146b732cdSPavel Butsykin         ret = qcow2_shrink_reftable(bs);
414246b732cdSPavel Butsykin         if (ret < 0) {
414346b732cdSPavel Butsykin             error_setg_errno(errp, -ret,
414446b732cdSPavel Butsykin                              "Failed to discard unused refblocks");
4145061ca8a3SKevin Wolf             goto fail;
414646b732cdSPavel Butsykin         }
4147163bc39dSPavel Butsykin 
4148163bc39dSPavel Butsykin         old_file_size = bdrv_getlength(bs->file->bs);
4149163bc39dSPavel Butsykin         if (old_file_size < 0) {
4150163bc39dSPavel Butsykin             error_setg_errno(errp, -old_file_size,
4151163bc39dSPavel Butsykin                              "Failed to inquire current file length");
4152061ca8a3SKevin Wolf             ret = old_file_size;
4153061ca8a3SKevin Wolf             goto fail;
4154163bc39dSPavel Butsykin         }
4155163bc39dSPavel Butsykin         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
4156163bc39dSPavel Butsykin         if (last_cluster < 0) {
4157163bc39dSPavel Butsykin             error_setg_errno(errp, -last_cluster,
4158163bc39dSPavel Butsykin                              "Failed to find the last cluster");
4159061ca8a3SKevin Wolf             ret = last_cluster;
4160061ca8a3SKevin Wolf             goto fail;
4161163bc39dSPavel Butsykin         }
4162163bc39dSPavel Butsykin         if ((last_cluster + 1) * s->cluster_size < old_file_size) {
4163233521b1SMax Reitz             Error *local_err = NULL;
4164233521b1SMax Reitz 
4165e61a28a9SMax Reitz             /*
4166e61a28a9SMax Reitz              * Do not pass @exact here: It will not help the user if
4167e61a28a9SMax Reitz              * we get an error here just because they wanted to shrink
4168e61a28a9SMax Reitz              * their qcow2 image (on a block device) with qemu-img.
4169e61a28a9SMax Reitz              * (And on the qcow2 layer, the @exact requirement is
4170e61a28a9SMax Reitz              * always fulfilled, so there is no need to pass it on.)
4171e61a28a9SMax Reitz              */
4172061ca8a3SKevin Wolf             bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
41737b8e4857SKevin Wolf                              false, PREALLOC_MODE_OFF, 0, &local_err);
4174233521b1SMax Reitz             if (local_err) {
4175233521b1SMax Reitz                 warn_reportf_err(local_err,
4176233521b1SMax Reitz                                  "Failed to truncate the tail of the image: ");
4177163bc39dSPavel Butsykin             }
4178163bc39dSPavel Butsykin         }
417946b732cdSPavel Butsykin     } else {
418072893756SStefan Hajnoczi         ret = qcow2_grow_l1_table(bs, new_l1_size, true);
4181419b19d9SStefan Hajnoczi         if (ret < 0) {
4182f59adb32SMax Reitz             error_setg_errno(errp, -ret, "Failed to grow the L1 table");
4183061ca8a3SKevin Wolf             goto fail;
4184419b19d9SStefan Hajnoczi         }
418546b732cdSPavel Butsykin     }
4186419b19d9SStefan Hajnoczi 
418795b98f34SMax Reitz     switch (prealloc) {
418895b98f34SMax Reitz     case PREALLOC_MODE_OFF:
4189718c0fceSKevin Wolf         if (has_data_file(bs)) {
4190e61a28a9SMax Reitz             /*
4191e61a28a9SMax Reitz              * If the caller wants an exact resize, the external data
4192e61a28a9SMax Reitz              * file should be resized to the exact target size, too,
4193e61a28a9SMax Reitz              * so we pass @exact here.
4194e61a28a9SMax Reitz              */
41957b8e4857SKevin Wolf             ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, 0,
41967b8e4857SKevin Wolf                                    errp);
4197718c0fceSKevin Wolf             if (ret < 0) {
4198718c0fceSKevin Wolf                 goto fail;
4199718c0fceSKevin Wolf             }
4200718c0fceSKevin Wolf         }
420195b98f34SMax Reitz         break;
420295b98f34SMax Reitz 
420395b98f34SMax Reitz     case PREALLOC_MODE_METADATA:
4204718c0fceSKevin Wolf         ret = preallocate_co(bs, old_length, offset, prealloc, errp);
420595b98f34SMax Reitz         if (ret < 0) {
4206061ca8a3SKevin Wolf             goto fail;
420795b98f34SMax Reitz         }
420895b98f34SMax Reitz         break;
420995b98f34SMax Reitz 
4210772d1f97SMax Reitz     case PREALLOC_MODE_FALLOC:
4211772d1f97SMax Reitz     case PREALLOC_MODE_FULL:
4212772d1f97SMax Reitz     {
4213772d1f97SMax Reitz         int64_t allocation_start, host_offset, guest_offset;
4214772d1f97SMax Reitz         int64_t clusters_allocated;
42154b96fa38SMax Reitz         int64_t old_file_size, last_cluster, new_file_size;
4216772d1f97SMax Reitz         uint64_t nb_new_data_clusters, nb_new_l2_tables;
4217772d1f97SMax Reitz 
4218966b000fSKevin Wolf         /* With a data file, preallocation means just allocating the metadata
4219966b000fSKevin Wolf          * and forwarding the truncate request to the data file */
4220966b000fSKevin Wolf         if (has_data_file(bs)) {
4221718c0fceSKevin Wolf             ret = preallocate_co(bs, old_length, offset, prealloc, errp);
4222966b000fSKevin Wolf             if (ret < 0) {
4223966b000fSKevin Wolf                 goto fail;
4224966b000fSKevin Wolf             }
4225966b000fSKevin Wolf             break;
4226966b000fSKevin Wolf         }
4227966b000fSKevin Wolf 
4228772d1f97SMax Reitz         old_file_size = bdrv_getlength(bs->file->bs);
4229772d1f97SMax Reitz         if (old_file_size < 0) {
4230772d1f97SMax Reitz             error_setg_errno(errp, -old_file_size,
4231772d1f97SMax Reitz                              "Failed to inquire current file length");
4232061ca8a3SKevin Wolf             ret = old_file_size;
4233061ca8a3SKevin Wolf             goto fail;
4234772d1f97SMax Reitz         }
42354b96fa38SMax Reitz 
42364b96fa38SMax Reitz         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
42374b96fa38SMax Reitz         if (last_cluster >= 0) {
42384b96fa38SMax Reitz             old_file_size = (last_cluster + 1) * s->cluster_size;
42394b96fa38SMax Reitz         } else {
4240e400ad1eSMax Reitz             old_file_size = ROUND_UP(old_file_size, s->cluster_size);
42414b96fa38SMax Reitz         }
4242772d1f97SMax Reitz 
4243a5675f39SAlberto Garcia         nb_new_data_clusters = (ROUND_UP(offset, s->cluster_size) -
4244a5675f39SAlberto Garcia             start_of_cluster(s, old_length)) >> s->cluster_bits;
4245772d1f97SMax Reitz 
4246772d1f97SMax Reitz         /* This is an overestimation; we will not actually allocate space for
4247772d1f97SMax Reitz          * these in the file but just make sure the new refcount structures are
4248772d1f97SMax Reitz          * able to cover them so we will not have to allocate new refblocks
4249772d1f97SMax Reitz          * while entering the data blocks in the potentially new L2 tables.
4250772d1f97SMax Reitz          * (We do not actually care where the L2 tables are placed. Maybe they
4251772d1f97SMax Reitz          *  are already allocated or they can be placed somewhere before
4252772d1f97SMax Reitz          *  @old_file_size. It does not matter because they will be fully
4253772d1f97SMax Reitz          *  allocated automatically, so they do not need to be covered by the
4254772d1f97SMax Reitz          *  preallocation. All that matters is that we will not have to allocate
4255772d1f97SMax Reitz          *  new refcount structures for them.) */
4256772d1f97SMax Reitz         nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
4257c8fd8554SAlberto Garcia                                         s->cluster_size / l2_entry_size(s));
4258772d1f97SMax Reitz         /* The cluster range may not be aligned to L2 boundaries, so add one L2
4259772d1f97SMax Reitz          * table for a potential head/tail */
4260772d1f97SMax Reitz         nb_new_l2_tables++;
4261772d1f97SMax Reitz 
4262772d1f97SMax Reitz         allocation_start = qcow2_refcount_area(bs, old_file_size,
4263772d1f97SMax Reitz                                                nb_new_data_clusters +
4264772d1f97SMax Reitz                                                nb_new_l2_tables,
4265772d1f97SMax Reitz                                                true, 0, 0);
4266772d1f97SMax Reitz         if (allocation_start < 0) {
4267772d1f97SMax Reitz             error_setg_errno(errp, -allocation_start,
4268772d1f97SMax Reitz                              "Failed to resize refcount structures");
4269061ca8a3SKevin Wolf             ret = allocation_start;
4270061ca8a3SKevin Wolf             goto fail;
4271772d1f97SMax Reitz         }
4272772d1f97SMax Reitz 
4273772d1f97SMax Reitz         clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
4274772d1f97SMax Reitz                                                      nb_new_data_clusters);
4275772d1f97SMax Reitz         if (clusters_allocated < 0) {
4276772d1f97SMax Reitz             error_setg_errno(errp, -clusters_allocated,
4277772d1f97SMax Reitz                              "Failed to allocate data clusters");
4278061ca8a3SKevin Wolf             ret = clusters_allocated;
4279061ca8a3SKevin Wolf             goto fail;
4280772d1f97SMax Reitz         }
4281772d1f97SMax Reitz 
4282772d1f97SMax Reitz         assert(clusters_allocated == nb_new_data_clusters);
4283772d1f97SMax Reitz 
4284772d1f97SMax Reitz         /* Allocate the data area */
4285772d1f97SMax Reitz         new_file_size = allocation_start +
4286772d1f97SMax Reitz                         nb_new_data_clusters * s->cluster_size;
4287eb8a0cf3SKevin Wolf         /*
4288eb8a0cf3SKevin Wolf          * Image file grows, so @exact does not matter.
4289eb8a0cf3SKevin Wolf          *
4290eb8a0cf3SKevin Wolf          * If we need to zero out the new area, try first whether the protocol
4291eb8a0cf3SKevin Wolf          * driver can already take care of this.
4292eb8a0cf3SKevin Wolf          */
4293eb8a0cf3SKevin Wolf         if (flags & BDRV_REQ_ZERO_WRITE) {
4294eb8a0cf3SKevin Wolf             ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc,
4295eb8a0cf3SKevin Wolf                                    BDRV_REQ_ZERO_WRITE, NULL);
4296eb8a0cf3SKevin Wolf             if (ret >= 0) {
4297eb8a0cf3SKevin Wolf                 flags &= ~BDRV_REQ_ZERO_WRITE;
4298eb8a0cf3SKevin Wolf             }
4299eb8a0cf3SKevin Wolf         } else {
4300eb8a0cf3SKevin Wolf             ret = -1;
4301eb8a0cf3SKevin Wolf         }
4302eb8a0cf3SKevin Wolf         if (ret < 0) {
43037b8e4857SKevin Wolf             ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, 0,
43047b8e4857SKevin Wolf                                    errp);
4305eb8a0cf3SKevin Wolf         }
4306772d1f97SMax Reitz         if (ret < 0) {
4307772d1f97SMax Reitz             error_prepend(errp, "Failed to resize underlying file: ");
4308772d1f97SMax Reitz             qcow2_free_clusters(bs, allocation_start,
4309772d1f97SMax Reitz                                 nb_new_data_clusters * s->cluster_size,
4310772d1f97SMax Reitz                                 QCOW2_DISCARD_OTHER);
4311061ca8a3SKevin Wolf             goto fail;
4312772d1f97SMax Reitz         }
4313772d1f97SMax Reitz 
4314772d1f97SMax Reitz         /* Create the necessary L2 entries */
4315772d1f97SMax Reitz         host_offset = allocation_start;
4316772d1f97SMax Reitz         guest_offset = old_length;
4317772d1f97SMax Reitz         while (nb_new_data_clusters) {
431813bec229SAlberto Garcia             int64_t nb_clusters = MIN(
431913bec229SAlberto Garcia                 nb_new_data_clusters,
432013bec229SAlberto Garcia                 s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
4321a5675f39SAlberto Garcia             unsigned cow_start_length = offset_into_cluster(s, guest_offset);
4322a5675f39SAlberto Garcia             QCowL2Meta allocation;
4323a5675f39SAlberto Garcia             guest_offset = start_of_cluster(s, guest_offset);
4324a5675f39SAlberto Garcia             allocation = (QCowL2Meta) {
4325772d1f97SMax Reitz                 .offset       = guest_offset,
4326772d1f97SMax Reitz                 .alloc_offset = host_offset,
4327772d1f97SMax Reitz                 .nb_clusters  = nb_clusters,
4328a5675f39SAlberto Garcia                 .cow_start    = {
4329a5675f39SAlberto Garcia                     .offset       = 0,
4330a5675f39SAlberto Garcia                     .nb_bytes     = cow_start_length,
4331a5675f39SAlberto Garcia                 },
4332a5675f39SAlberto Garcia                 .cow_end      = {
4333a5675f39SAlberto Garcia                     .offset       = nb_clusters << s->cluster_bits,
4334a5675f39SAlberto Garcia                     .nb_bytes     = 0,
4335a5675f39SAlberto Garcia                 },
4336772d1f97SMax Reitz             };
4337772d1f97SMax Reitz             qemu_co_queue_init(&allocation.dependent_requests);
4338772d1f97SMax Reitz 
4339772d1f97SMax Reitz             ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
4340772d1f97SMax Reitz             if (ret < 0) {
4341772d1f97SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update L2 tables");
4342772d1f97SMax Reitz                 qcow2_free_clusters(bs, host_offset,
4343772d1f97SMax Reitz                                     nb_new_data_clusters * s->cluster_size,
4344772d1f97SMax Reitz                                     QCOW2_DISCARD_OTHER);
4345061ca8a3SKevin Wolf                 goto fail;
4346772d1f97SMax Reitz             }
4347772d1f97SMax Reitz 
4348772d1f97SMax Reitz             guest_offset += nb_clusters * s->cluster_size;
4349772d1f97SMax Reitz             host_offset += nb_clusters * s->cluster_size;
4350772d1f97SMax Reitz             nb_new_data_clusters -= nb_clusters;
4351772d1f97SMax Reitz         }
4352772d1f97SMax Reitz         break;
4353772d1f97SMax Reitz     }
4354772d1f97SMax Reitz 
435595b98f34SMax Reitz     default:
435695b98f34SMax Reitz         g_assert_not_reached();
435795b98f34SMax Reitz     }
435895b98f34SMax Reitz 
4359f01643fbSKevin Wolf     if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) {
4360*a6841a2dSAlberto Garcia         uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->subcluster_size);
4361f01643fbSKevin Wolf 
4362f01643fbSKevin Wolf         /*
4363*a6841a2dSAlberto Garcia          * Use zero clusters as much as we can. qcow2_subcluster_zeroize()
4364*a6841a2dSAlberto Garcia          * requires a subcluster-aligned start. The end may be unaligned if
4365*a6841a2dSAlberto Garcia          * it is at the end of the image (which it is here).
4366f01643fbSKevin Wolf          */
4367e4d7019eSAlberto Garcia         if (offset > zero_start) {
4368*a6841a2dSAlberto Garcia             ret = qcow2_subcluster_zeroize(bs, zero_start, offset - zero_start,
4369*a6841a2dSAlberto Garcia                                            0);
4370f01643fbSKevin Wolf             if (ret < 0) {
4371f01643fbSKevin Wolf                 error_setg_errno(errp, -ret, "Failed to zero out new clusters");
4372f01643fbSKevin Wolf                 goto fail;
4373f01643fbSKevin Wolf             }
4374e4d7019eSAlberto Garcia         }
4375f01643fbSKevin Wolf 
4376f01643fbSKevin Wolf         /* Write explicit zeros for the unaligned head */
4377f01643fbSKevin Wolf         if (zero_start > old_length) {
4378e4d7019eSAlberto Garcia             uint64_t len = MIN(zero_start, offset) - old_length;
4379f01643fbSKevin Wolf             uint8_t *buf = qemu_blockalign0(bs, len);
4380f01643fbSKevin Wolf             QEMUIOVector qiov;
4381f01643fbSKevin Wolf             qemu_iovec_init_buf(&qiov, buf, len);
4382f01643fbSKevin Wolf 
4383f01643fbSKevin Wolf             qemu_co_mutex_unlock(&s->lock);
4384f01643fbSKevin Wolf             ret = qcow2_co_pwritev_part(bs, old_length, len, &qiov, 0, 0);
4385f01643fbSKevin Wolf             qemu_co_mutex_lock(&s->lock);
4386f01643fbSKevin Wolf 
4387f01643fbSKevin Wolf             qemu_vfree(buf);
4388f01643fbSKevin Wolf             if (ret < 0) {
4389f01643fbSKevin Wolf                 error_setg_errno(errp, -ret, "Failed to zero out the new area");
4390f01643fbSKevin Wolf                 goto fail;
4391f01643fbSKevin Wolf             }
4392f01643fbSKevin Wolf         }
4393f01643fbSKevin Wolf     }
4394f01643fbSKevin Wolf 
439595b98f34SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
439695b98f34SMax Reitz         /* Flush metadata before actually changing the image size */
4397061ca8a3SKevin Wolf         ret = qcow2_write_caches(bs);
439895b98f34SMax Reitz         if (ret < 0) {
439995b98f34SMax Reitz             error_setg_errno(errp, -ret,
440095b98f34SMax Reitz                              "Failed to flush the preallocated area to disk");
4401061ca8a3SKevin Wolf             goto fail;
440295b98f34SMax Reitz         }
440395b98f34SMax Reitz     }
440495b98f34SMax Reitz 
440545b4949cSLeonid Bloch     bs->total_sectors = offset / BDRV_SECTOR_SIZE;
440645b4949cSLeonid Bloch 
4407419b19d9SStefan Hajnoczi     /* write updated header.size */
4408419b19d9SStefan Hajnoczi     offset = cpu_to_be64(offset);
4409d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
4410419b19d9SStefan Hajnoczi                            &offset, sizeof(uint64_t));
4411419b19d9SStefan Hajnoczi     if (ret < 0) {
4412f59adb32SMax Reitz         error_setg_errno(errp, -ret, "Failed to update the image size");
4413061ca8a3SKevin Wolf         goto fail;
4414419b19d9SStefan Hajnoczi     }
4415419b19d9SStefan Hajnoczi 
4416419b19d9SStefan Hajnoczi     s->l1_vm_state_index = new_l1_size;
441745b4949cSLeonid Bloch 
441845b4949cSLeonid Bloch     /* Update cache sizes */
441945b4949cSLeonid Bloch     options = qdict_clone_shallow(bs->options);
442045b4949cSLeonid Bloch     ret = qcow2_update_options(bs, options, s->flags, errp);
442145b4949cSLeonid Bloch     qobject_unref(options);
442245b4949cSLeonid Bloch     if (ret < 0) {
442345b4949cSLeonid Bloch         goto fail;
442445b4949cSLeonid Bloch     }
4425061ca8a3SKevin Wolf     ret = 0;
4426061ca8a3SKevin Wolf fail:
4427061ca8a3SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
4428061ca8a3SKevin Wolf     return ret;
4429419b19d9SStefan Hajnoczi }
4430419b19d9SStefan Hajnoczi 
4431fcccefc5SPavel Butsykin static coroutine_fn int
44320d483dceSAndrey Shinkevich qcow2_co_pwritev_compressed_task(BlockDriverState *bs,
44335396234bSVladimir Sementsov-Ogievskiy                                  uint64_t offset, uint64_t bytes,
44345396234bSVladimir Sementsov-Ogievskiy                                  QEMUIOVector *qiov, size_t qiov_offset)
443520d97356SBlue Swirl {
4436ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
44372714f13dSVladimir Sementsov-Ogievskiy     int ret;
4438e1f4a37aSAlberto Garcia     ssize_t out_len;
4439fcccefc5SPavel Butsykin     uint8_t *buf, *out_buf;
444077e023ffSKevin Wolf     uint64_t cluster_offset;
444120d97356SBlue Swirl 
44420d483dceSAndrey Shinkevich     assert(bytes == s->cluster_size || (bytes < s->cluster_size &&
44430d483dceSAndrey Shinkevich            (offset + bytes == bs->total_sectors << BDRV_SECTOR_BITS)));
44443e3b838fSAnton Nefedov 
4445fcccefc5SPavel Butsykin     buf = qemu_blockalign(bs, s->cluster_size);
44460d483dceSAndrey Shinkevich     if (bytes < s->cluster_size) {
4447a2c0ca6fSPavel Butsykin         /* Zero-pad last write if image size is not cluster aligned */
4448a2c0ca6fSPavel Butsykin         memset(buf + bytes, 0, s->cluster_size - bytes);
4449a2c0ca6fSPavel Butsykin     }
44505396234bSVladimir Sementsov-Ogievskiy     qemu_iovec_to_buf(qiov, qiov_offset, buf, bytes);
445120d97356SBlue Swirl 
4452ebf7bba0SVladimir Sementsov-Ogievskiy     out_buf = g_malloc(s->cluster_size);
445320d97356SBlue Swirl 
44546994fd78SVladimir Sementsov-Ogievskiy     out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1,
44556994fd78SVladimir Sementsov-Ogievskiy                                 buf, s->cluster_size);
4456e1f4a37aSAlberto Garcia     if (out_len == -ENOMEM) {
445720d97356SBlue Swirl         /* could not compress: write normal cluster */
44585396234bSVladimir Sementsov-Ogievskiy         ret = qcow2_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset, 0);
44598f1efd00SKevin Wolf         if (ret < 0) {
44608f1efd00SKevin Wolf             goto fail;
44618f1efd00SKevin Wolf         }
4462fcccefc5SPavel Butsykin         goto success;
4463e1f4a37aSAlberto Garcia     } else if (out_len < 0) {
4464e1f4a37aSAlberto Garcia         ret = -EINVAL;
4465e1f4a37aSAlberto Garcia         goto fail;
4466fcccefc5SPavel Butsykin     }
4467fcccefc5SPavel Butsykin 
4468fcccefc5SPavel Butsykin     qemu_co_mutex_lock(&s->lock);
446977e023ffSKevin Wolf     ret = qcow2_alloc_compressed_cluster_offset(bs, offset, out_len,
447077e023ffSKevin Wolf                                                 &cluster_offset);
447177e023ffSKevin Wolf     if (ret < 0) {
4472fcccefc5SPavel Butsykin         qemu_co_mutex_unlock(&s->lock);
44738f1efd00SKevin Wolf         goto fail;
44748f1efd00SKevin Wolf     }
4475cf93980eSMax Reitz 
4476966b000fSKevin Wolf     ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len, true);
4477fcccefc5SPavel Butsykin     qemu_co_mutex_unlock(&s->lock);
4478cf93980eSMax Reitz     if (ret < 0) {
4479cf93980eSMax Reitz         goto fail;
4480cf93980eSMax Reitz     }
4481cf93980eSMax Reitz 
4482966b000fSKevin Wolf     BLKDBG_EVENT(s->data_file, BLKDBG_WRITE_COMPRESSED);
4483b00cb15bSVladimir Sementsov-Ogievskiy     ret = bdrv_co_pwrite(s->data_file, cluster_offset, out_len, out_buf, 0);
44848f1efd00SKevin Wolf     if (ret < 0) {
44858f1efd00SKevin Wolf         goto fail;
448620d97356SBlue Swirl     }
4487fcccefc5SPavel Butsykin success:
44888f1efd00SKevin Wolf     ret = 0;
44898f1efd00SKevin Wolf fail:
4490fcccefc5SPavel Butsykin     qemu_vfree(buf);
44917267c094SAnthony Liguori     g_free(out_buf);
44928f1efd00SKevin Wolf     return ret;
449320d97356SBlue Swirl }
449420d97356SBlue Swirl 
44950d483dceSAndrey Shinkevich static coroutine_fn int qcow2_co_pwritev_compressed_task_entry(AioTask *task)
44960d483dceSAndrey Shinkevich {
44970d483dceSAndrey Shinkevich     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
44980d483dceSAndrey Shinkevich 
449910dabdc5SAlberto Garcia     assert(!t->subcluster_type && !t->l2meta);
45000d483dceSAndrey Shinkevich 
45010d483dceSAndrey Shinkevich     return qcow2_co_pwritev_compressed_task(t->bs, t->offset, t->bytes, t->qiov,
45020d483dceSAndrey Shinkevich                                             t->qiov_offset);
45030d483dceSAndrey Shinkevich }
45040d483dceSAndrey Shinkevich 
45050d483dceSAndrey Shinkevich /*
45060d483dceSAndrey Shinkevich  * XXX: put compressed sectors first, then all the cluster aligned
45070d483dceSAndrey Shinkevich  * tables to avoid losing bytes in alignment
45080d483dceSAndrey Shinkevich  */
45090d483dceSAndrey Shinkevich static coroutine_fn int
45100d483dceSAndrey Shinkevich qcow2_co_pwritev_compressed_part(BlockDriverState *bs,
45110d483dceSAndrey Shinkevich                                  uint64_t offset, uint64_t bytes,
45120d483dceSAndrey Shinkevich                                  QEMUIOVector *qiov, size_t qiov_offset)
45130d483dceSAndrey Shinkevich {
45140d483dceSAndrey Shinkevich     BDRVQcow2State *s = bs->opaque;
45150d483dceSAndrey Shinkevich     AioTaskPool *aio = NULL;
45160d483dceSAndrey Shinkevich     int ret = 0;
45170d483dceSAndrey Shinkevich 
45180d483dceSAndrey Shinkevich     if (has_data_file(bs)) {
45190d483dceSAndrey Shinkevich         return -ENOTSUP;
45200d483dceSAndrey Shinkevich     }
45210d483dceSAndrey Shinkevich 
45220d483dceSAndrey Shinkevich     if (bytes == 0) {
45230d483dceSAndrey Shinkevich         /*
45240d483dceSAndrey Shinkevich          * align end of file to a sector boundary to ease reading with
45250d483dceSAndrey Shinkevich          * sector based I/Os
45260d483dceSAndrey Shinkevich          */
45270d483dceSAndrey Shinkevich         int64_t len = bdrv_getlength(bs->file->bs);
45280d483dceSAndrey Shinkevich         if (len < 0) {
45290d483dceSAndrey Shinkevich             return len;
45300d483dceSAndrey Shinkevich         }
45317b8e4857SKevin Wolf         return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, 0,
45327b8e4857SKevin Wolf                                 NULL);
45330d483dceSAndrey Shinkevich     }
45340d483dceSAndrey Shinkevich 
45350d483dceSAndrey Shinkevich     if (offset_into_cluster(s, offset)) {
45360d483dceSAndrey Shinkevich         return -EINVAL;
45370d483dceSAndrey Shinkevich     }
45380d483dceSAndrey Shinkevich 
4539fb43d2d4SAlberto Garcia     if (offset_into_cluster(s, bytes) &&
4540fb43d2d4SAlberto Garcia         (offset + bytes) != (bs->total_sectors << BDRV_SECTOR_BITS)) {
4541fb43d2d4SAlberto Garcia         return -EINVAL;
4542fb43d2d4SAlberto Garcia     }
4543fb43d2d4SAlberto Garcia 
45440d483dceSAndrey Shinkevich     while (bytes && aio_task_pool_status(aio) == 0) {
45450d483dceSAndrey Shinkevich         uint64_t chunk_size = MIN(bytes, s->cluster_size);
45460d483dceSAndrey Shinkevich 
45470d483dceSAndrey Shinkevich         if (!aio && chunk_size != bytes) {
45480d483dceSAndrey Shinkevich             aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
45490d483dceSAndrey Shinkevich         }
45500d483dceSAndrey Shinkevich 
45510d483dceSAndrey Shinkevich         ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_compressed_task_entry,
45520d483dceSAndrey Shinkevich                              0, 0, offset, chunk_size, qiov, qiov_offset, NULL);
45530d483dceSAndrey Shinkevich         if (ret < 0) {
45540d483dceSAndrey Shinkevich             break;
45550d483dceSAndrey Shinkevich         }
45560d483dceSAndrey Shinkevich         qiov_offset += chunk_size;
45570d483dceSAndrey Shinkevich         offset += chunk_size;
45580d483dceSAndrey Shinkevich         bytes -= chunk_size;
45590d483dceSAndrey Shinkevich     }
45600d483dceSAndrey Shinkevich 
45610d483dceSAndrey Shinkevich     if (aio) {
45620d483dceSAndrey Shinkevich         aio_task_pool_wait_all(aio);
45630d483dceSAndrey Shinkevich         if (ret == 0) {
45640d483dceSAndrey Shinkevich             ret = aio_task_pool_status(aio);
45650d483dceSAndrey Shinkevich         }
45660d483dceSAndrey Shinkevich         g_free(aio);
45670d483dceSAndrey Shinkevich     }
45680d483dceSAndrey Shinkevich 
45690d483dceSAndrey Shinkevich     return ret;
45700d483dceSAndrey Shinkevich }
45710d483dceSAndrey Shinkevich 
4572c3c10f72SVladimir Sementsov-Ogievskiy static int coroutine_fn
4573c3c10f72SVladimir Sementsov-Ogievskiy qcow2_co_preadv_compressed(BlockDriverState *bs,
45749c4269d5SAlberto Garcia                            uint64_t cluster_descriptor,
4575c3c10f72SVladimir Sementsov-Ogievskiy                            uint64_t offset,
4576c3c10f72SVladimir Sementsov-Ogievskiy                            uint64_t bytes,
4577df893d25SVladimir Sementsov-Ogievskiy                            QEMUIOVector *qiov,
4578df893d25SVladimir Sementsov-Ogievskiy                            size_t qiov_offset)
4579f4b3e2a9SVladimir Sementsov-Ogievskiy {
4580f4b3e2a9SVladimir Sementsov-Ogievskiy     BDRVQcow2State *s = bs->opaque;
4581c3c10f72SVladimir Sementsov-Ogievskiy     int ret = 0, csize, nb_csectors;
4582f4b3e2a9SVladimir Sementsov-Ogievskiy     uint64_t coffset;
4583c3c10f72SVladimir Sementsov-Ogievskiy     uint8_t *buf, *out_buf;
4584c3c10f72SVladimir Sementsov-Ogievskiy     int offset_in_cluster = offset_into_cluster(s, offset);
4585f4b3e2a9SVladimir Sementsov-Ogievskiy 
45869c4269d5SAlberto Garcia     coffset = cluster_descriptor & s->cluster_offset_mask;
45879c4269d5SAlberto Garcia     nb_csectors = ((cluster_descriptor >> s->csize_shift) & s->csize_mask) + 1;
4588b6c24694SAlberto Garcia     csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
4589b6c24694SAlberto Garcia         (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK);
4590f4b3e2a9SVladimir Sementsov-Ogievskiy 
4591c3c10f72SVladimir Sementsov-Ogievskiy     buf = g_try_malloc(csize);
4592c3c10f72SVladimir Sementsov-Ogievskiy     if (!buf) {
4593f4b3e2a9SVladimir Sementsov-Ogievskiy         return -ENOMEM;
4594f4b3e2a9SVladimir Sementsov-Ogievskiy     }
4595c068a1cdSVladimir Sementsov-Ogievskiy 
4596c3c10f72SVladimir Sementsov-Ogievskiy     out_buf = qemu_blockalign(bs, s->cluster_size);
4597c3c10f72SVladimir Sementsov-Ogievskiy 
4598f4b3e2a9SVladimir Sementsov-Ogievskiy     BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
4599b00cb15bSVladimir Sementsov-Ogievskiy     ret = bdrv_co_pread(bs->file, coffset, csize, buf, 0);
4600f4b3e2a9SVladimir Sementsov-Ogievskiy     if (ret < 0) {
4601c3c10f72SVladimir Sementsov-Ogievskiy         goto fail;
4602c3c10f72SVladimir Sementsov-Ogievskiy     }
4603c3c10f72SVladimir Sementsov-Ogievskiy 
4604e23c9d7aSVladimir Sementsov-Ogievskiy     if (qcow2_co_decompress(bs, out_buf, s->cluster_size, buf, csize) < 0) {
4605c3c10f72SVladimir Sementsov-Ogievskiy         ret = -EIO;
4606c3c10f72SVladimir Sementsov-Ogievskiy         goto fail;
4607c3c10f72SVladimir Sementsov-Ogievskiy     }
4608c3c10f72SVladimir Sementsov-Ogievskiy 
4609df893d25SVladimir Sementsov-Ogievskiy     qemu_iovec_from_buf(qiov, qiov_offset, out_buf + offset_in_cluster, bytes);
4610c3c10f72SVladimir Sementsov-Ogievskiy 
4611c3c10f72SVladimir Sementsov-Ogievskiy fail:
4612c3c10f72SVladimir Sementsov-Ogievskiy     qemu_vfree(out_buf);
4613c3c10f72SVladimir Sementsov-Ogievskiy     g_free(buf);
4614c3c10f72SVladimir Sementsov-Ogievskiy 
4615f4b3e2a9SVladimir Sementsov-Ogievskiy     return ret;
4616f4b3e2a9SVladimir Sementsov-Ogievskiy }
4617f4b3e2a9SVladimir Sementsov-Ogievskiy 
461894054183SMax Reitz static int make_completely_empty(BlockDriverState *bs)
461994054183SMax Reitz {
4620ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
4621ed3d2ec9SMax Reitz     Error *local_err = NULL;
462294054183SMax Reitz     int ret, l1_clusters;
462394054183SMax Reitz     int64_t offset;
462494054183SMax Reitz     uint64_t *new_reftable = NULL;
462594054183SMax Reitz     uint64_t rt_entry, l1_size2;
462694054183SMax Reitz     struct {
462794054183SMax Reitz         uint64_t l1_offset;
462894054183SMax Reitz         uint64_t reftable_offset;
462994054183SMax Reitz         uint32_t reftable_clusters;
463094054183SMax Reitz     } QEMU_PACKED l1_ofs_rt_ofs_cls;
463194054183SMax Reitz 
463294054183SMax Reitz     ret = qcow2_cache_empty(bs, s->l2_table_cache);
463394054183SMax Reitz     if (ret < 0) {
463494054183SMax Reitz         goto fail;
463594054183SMax Reitz     }
463694054183SMax Reitz 
463794054183SMax Reitz     ret = qcow2_cache_empty(bs, s->refcount_block_cache);
463894054183SMax Reitz     if (ret < 0) {
463994054183SMax Reitz         goto fail;
464094054183SMax Reitz     }
464194054183SMax Reitz 
464294054183SMax Reitz     /* Refcounts will be broken utterly */
464394054183SMax Reitz     ret = qcow2_mark_dirty(bs);
464494054183SMax Reitz     if (ret < 0) {
464594054183SMax Reitz         goto fail;
464694054183SMax Reitz     }
464794054183SMax Reitz 
464894054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
464994054183SMax Reitz 
465094054183SMax Reitz     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
465194054183SMax Reitz     l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
465294054183SMax Reitz 
465394054183SMax Reitz     /* After this call, neither the in-memory nor the on-disk refcount
465494054183SMax Reitz      * information accurately describe the actual references */
465594054183SMax Reitz 
4656720ff280SKevin Wolf     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
465774021bc4SEric Blake                              l1_clusters * s->cluster_size, 0);
465894054183SMax Reitz     if (ret < 0) {
465994054183SMax Reitz         goto fail_broken_refcounts;
466094054183SMax Reitz     }
466194054183SMax Reitz     memset(s->l1_table, 0, l1_size2);
466294054183SMax Reitz 
466394054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
466494054183SMax Reitz 
466594054183SMax Reitz     /* Overwrite enough clusters at the beginning of the sectors to place
466694054183SMax Reitz      * the refcount table, a refcount block and the L1 table in; this may
466794054183SMax Reitz      * overwrite parts of the existing refcount and L1 table, which is not
466894054183SMax Reitz      * an issue because the dirty flag is set, complete data loss is in fact
466994054183SMax Reitz      * desired and partial data loss is consequently fine as well */
4670720ff280SKevin Wolf     ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
467174021bc4SEric Blake                              (2 + l1_clusters) * s->cluster_size, 0);
467294054183SMax Reitz     /* This call (even if it failed overall) may have overwritten on-disk
467394054183SMax Reitz      * refcount structures; in that case, the in-memory refcount information
467494054183SMax Reitz      * will probably differ from the on-disk information which makes the BDS
467594054183SMax Reitz      * unusable */
467694054183SMax Reitz     if (ret < 0) {
467794054183SMax Reitz         goto fail_broken_refcounts;
467894054183SMax Reitz     }
467994054183SMax Reitz 
468094054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
468194054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
468294054183SMax Reitz 
468394054183SMax Reitz     /* "Create" an empty reftable (one cluster) directly after the image
468494054183SMax Reitz      * header and an empty L1 table three clusters after the image header;
468594054183SMax Reitz      * the cluster between those two will be used as the first refblock */
4686f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
4687f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
4688f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
4689d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
469094054183SMax Reitz                            &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
469194054183SMax Reitz     if (ret < 0) {
469294054183SMax Reitz         goto fail_broken_refcounts;
469394054183SMax Reitz     }
469494054183SMax Reitz 
469594054183SMax Reitz     s->l1_table_offset = 3 * s->cluster_size;
469694054183SMax Reitz 
469794054183SMax Reitz     new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
469894054183SMax Reitz     if (!new_reftable) {
469994054183SMax Reitz         ret = -ENOMEM;
470094054183SMax Reitz         goto fail_broken_refcounts;
470194054183SMax Reitz     }
470294054183SMax Reitz 
470394054183SMax Reitz     s->refcount_table_offset = s->cluster_size;
470494054183SMax Reitz     s->refcount_table_size   = s->cluster_size / sizeof(uint64_t);
47057061a078SAlberto Garcia     s->max_refcount_table_index = 0;
470694054183SMax Reitz 
470794054183SMax Reitz     g_free(s->refcount_table);
470894054183SMax Reitz     s->refcount_table = new_reftable;
470994054183SMax Reitz     new_reftable = NULL;
471094054183SMax Reitz 
471194054183SMax Reitz     /* Now the in-memory refcount information again corresponds to the on-disk
471294054183SMax Reitz      * information (reftable is empty and no refblocks (the refblock cache is
471394054183SMax Reitz      * empty)); however, this means some clusters (e.g. the image header) are
471494054183SMax Reitz      * referenced, but not refcounted, but the normal qcow2 code assumes that
471594054183SMax Reitz      * the in-memory information is always correct */
471694054183SMax Reitz 
471794054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
471894054183SMax Reitz 
471994054183SMax Reitz     /* Enter the first refblock into the reftable */
472094054183SMax Reitz     rt_entry = cpu_to_be64(2 * s->cluster_size);
4721d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
472294054183SMax Reitz                            &rt_entry, sizeof(rt_entry));
472394054183SMax Reitz     if (ret < 0) {
472494054183SMax Reitz         goto fail_broken_refcounts;
472594054183SMax Reitz     }
472694054183SMax Reitz     s->refcount_table[0] = 2 * s->cluster_size;
472794054183SMax Reitz 
472894054183SMax Reitz     s->free_cluster_index = 0;
472994054183SMax Reitz     assert(3 + l1_clusters <= s->refcount_block_size);
473094054183SMax Reitz     offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
473194054183SMax Reitz     if (offset < 0) {
473294054183SMax Reitz         ret = offset;
473394054183SMax Reitz         goto fail_broken_refcounts;
473494054183SMax Reitz     } else if (offset > 0) {
473594054183SMax Reitz         error_report("First cluster in emptied image is in use");
473694054183SMax Reitz         abort();
473794054183SMax Reitz     }
473894054183SMax Reitz 
473994054183SMax Reitz     /* Now finally the in-memory information corresponds to the on-disk
474094054183SMax Reitz      * structures and is correct */
474194054183SMax Reitz     ret = qcow2_mark_clean(bs);
474294054183SMax Reitz     if (ret < 0) {
474394054183SMax Reitz         goto fail;
474494054183SMax Reitz     }
474594054183SMax Reitz 
4746c80d8b06SMax Reitz     ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false,
47477b8e4857SKevin Wolf                         PREALLOC_MODE_OFF, 0, &local_err);
474894054183SMax Reitz     if (ret < 0) {
4749ed3d2ec9SMax Reitz         error_report_err(local_err);
475094054183SMax Reitz         goto fail;
475194054183SMax Reitz     }
475294054183SMax Reitz 
475394054183SMax Reitz     return 0;
475494054183SMax Reitz 
475594054183SMax Reitz fail_broken_refcounts:
475694054183SMax Reitz     /* The BDS is unusable at this point. If we wanted to make it usable, we
475794054183SMax Reitz      * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
475894054183SMax Reitz      * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
475994054183SMax Reitz      * again. However, because the functions which could have caused this error
476094054183SMax Reitz      * path to be taken are used by those functions as well, it's very likely
476194054183SMax Reitz      * that that sequence will fail as well. Therefore, just eject the BDS. */
476294054183SMax Reitz     bs->drv = NULL;
476394054183SMax Reitz 
476494054183SMax Reitz fail:
476594054183SMax Reitz     g_free(new_reftable);
476694054183SMax Reitz     return ret;
476794054183SMax Reitz }
476894054183SMax Reitz 
4769491d27e2SMax Reitz static int qcow2_make_empty(BlockDriverState *bs)
4770491d27e2SMax Reitz {
4771ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
4772d2cb36afSEric Blake     uint64_t offset, end_offset;
4773d2cb36afSEric Blake     int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
477494054183SMax Reitz     int l1_clusters, ret = 0;
4775491d27e2SMax Reitz 
477694054183SMax Reitz     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
477794054183SMax Reitz 
47784096974eSEric Blake     if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
4779f0603329SDaniel P. Berrange         3 + l1_clusters <= s->refcount_block_size &&
4780db04524fSKevin Wolf         s->crypt_method_header != QCOW_CRYPT_LUKS &&
4781db04524fSKevin Wolf         !has_data_file(bs)) {
47824096974eSEric Blake         /* The following function only works for qcow2 v3 images (it
47834096974eSEric Blake          * requires the dirty flag) and only as long as there are no
47844096974eSEric Blake          * features that reserve extra clusters (such as snapshots,
47854096974eSEric Blake          * LUKS header, or persistent bitmaps), because it completely
47864096974eSEric Blake          * empties the image.  Furthermore, the L1 table and three
47874096974eSEric Blake          * additional clusters (image header, refcount table, one
4788db04524fSKevin Wolf          * refcount block) have to fit inside one refcount block. It
4789db04524fSKevin Wolf          * only resets the image file, i.e. does not work with an
4790db04524fSKevin Wolf          * external data file. */
479194054183SMax Reitz         return make_completely_empty(bs);
479294054183SMax Reitz     }
479394054183SMax Reitz 
479494054183SMax Reitz     /* This fallback code simply discards every active cluster; this is slow,
479594054183SMax Reitz      * but works in all cases */
4796d2cb36afSEric Blake     end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
4797d2cb36afSEric Blake     for (offset = 0; offset < end_offset; offset += step) {
4798491d27e2SMax Reitz         /* As this function is generally used after committing an external
4799491d27e2SMax Reitz          * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
4800491d27e2SMax Reitz          * default action for this kind of discard is to pass the discard,
4801491d27e2SMax Reitz          * which will ideally result in an actually smaller image file, as
4802491d27e2SMax Reitz          * is probably desired. */
4803d2cb36afSEric Blake         ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
4804491d27e2SMax Reitz                                     QCOW2_DISCARD_SNAPSHOT, true);
4805491d27e2SMax Reitz         if (ret < 0) {
4806491d27e2SMax Reitz             break;
4807491d27e2SMax Reitz         }
4808491d27e2SMax Reitz     }
4809491d27e2SMax Reitz 
4810491d27e2SMax Reitz     return ret;
4811491d27e2SMax Reitz }
4812491d27e2SMax Reitz 
4813a968168cSDong Xu Wang static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
481420d97356SBlue Swirl {
4815ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
481629c1a730SKevin Wolf     int ret;
481729c1a730SKevin Wolf 
48188b94ff85SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
48198b220eb7SPaolo Bonzini     ret = qcow2_write_caches(bs);
48208b94ff85SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
482129c1a730SKevin Wolf 
48228b220eb7SPaolo Bonzini     return ret;
4823eb489bb1SKevin Wolf }
4824eb489bb1SKevin Wolf 
4825c501c352SStefan Hajnoczi static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
4826c501c352SStefan Hajnoczi                                        Error **errp)
4827c501c352SStefan Hajnoczi {
4828c501c352SStefan Hajnoczi     Error *local_err = NULL;
4829c501c352SStefan Hajnoczi     BlockMeasureInfo *info;
4830c501c352SStefan Hajnoczi     uint64_t required = 0; /* bytes that contribute to required size */
4831c501c352SStefan Hajnoczi     uint64_t virtual_size; /* disk size as seen by guest */
4832c501c352SStefan Hajnoczi     uint64_t refcount_bits;
4833c501c352SStefan Hajnoczi     uint64_t l2_tables;
483461914f89SStefan Hajnoczi     uint64_t luks_payload_size = 0;
4835c501c352SStefan Hajnoczi     size_t cluster_size;
4836c501c352SStefan Hajnoczi     int version;
4837c501c352SStefan Hajnoczi     char *optstr;
4838c501c352SStefan Hajnoczi     PreallocMode prealloc;
4839c501c352SStefan Hajnoczi     bool has_backing_file;
484061914f89SStefan Hajnoczi     bool has_luks;
4841c501c352SStefan Hajnoczi 
4842c501c352SStefan Hajnoczi     /* Parse image creation options */
4843c501c352SStefan Hajnoczi     cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
4844c501c352SStefan Hajnoczi     if (local_err) {
4845c501c352SStefan Hajnoczi         goto err;
4846c501c352SStefan Hajnoczi     }
4847c501c352SStefan Hajnoczi 
4848c501c352SStefan Hajnoczi     version = qcow2_opt_get_version_del(opts, &local_err);
4849c501c352SStefan Hajnoczi     if (local_err) {
4850c501c352SStefan Hajnoczi         goto err;
4851c501c352SStefan Hajnoczi     }
4852c501c352SStefan Hajnoczi 
4853c501c352SStefan Hajnoczi     refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
4854c501c352SStefan Hajnoczi     if (local_err) {
4855c501c352SStefan Hajnoczi         goto err;
4856c501c352SStefan Hajnoczi     }
4857c501c352SStefan Hajnoczi 
4858c501c352SStefan Hajnoczi     optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
4859f7abe0ecSMarc-André Lureau     prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
486006c60b6cSMarkus Armbruster                                PREALLOC_MODE_OFF, &local_err);
4861c501c352SStefan Hajnoczi     g_free(optstr);
4862c501c352SStefan Hajnoczi     if (local_err) {
4863c501c352SStefan Hajnoczi         goto err;
4864c501c352SStefan Hajnoczi     }
4865c501c352SStefan Hajnoczi 
4866c501c352SStefan Hajnoczi     optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
4867c501c352SStefan Hajnoczi     has_backing_file = !!optstr;
4868c501c352SStefan Hajnoczi     g_free(optstr);
4869c501c352SStefan Hajnoczi 
487061914f89SStefan Hajnoczi     optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
487161914f89SStefan Hajnoczi     has_luks = optstr && strcmp(optstr, "luks") == 0;
487261914f89SStefan Hajnoczi     g_free(optstr);
487361914f89SStefan Hajnoczi 
487461914f89SStefan Hajnoczi     if (has_luks) {
48756d49d3a8SStefan Hajnoczi         g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL;
487690766d9dSMaxim Levitsky         QDict *cryptoopts = qcow2_extract_crypto_opts(opts, "luks", errp);
487761914f89SStefan Hajnoczi         size_t headerlen;
487861914f89SStefan Hajnoczi 
48796d49d3a8SStefan Hajnoczi         create_opts = block_crypto_create_opts_init(cryptoopts, errp);
48806d49d3a8SStefan Hajnoczi         qobject_unref(cryptoopts);
48816d49d3a8SStefan Hajnoczi         if (!create_opts) {
48826d49d3a8SStefan Hajnoczi             goto err;
48836d49d3a8SStefan Hajnoczi         }
48846d49d3a8SStefan Hajnoczi 
48856d49d3a8SStefan Hajnoczi         if (!qcrypto_block_calculate_payload_offset(create_opts,
48866d49d3a8SStefan Hajnoczi                                                     "encrypt.",
48876d49d3a8SStefan Hajnoczi                                                     &headerlen,
48886d49d3a8SStefan Hajnoczi                                                     &local_err)) {
488961914f89SStefan Hajnoczi             goto err;
489061914f89SStefan Hajnoczi         }
489161914f89SStefan Hajnoczi 
489261914f89SStefan Hajnoczi         luks_payload_size = ROUND_UP(headerlen, cluster_size);
489361914f89SStefan Hajnoczi     }
489461914f89SStefan Hajnoczi 
48959e029689SAlberto Garcia     virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
48969e029689SAlberto Garcia     virtual_size = ROUND_UP(virtual_size, cluster_size);
4897c501c352SStefan Hajnoczi 
4898c501c352SStefan Hajnoczi     /* Check that virtual disk size is valid */
4899c501c352SStefan Hajnoczi     l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
4900c501c352SStefan Hajnoczi                              cluster_size / sizeof(uint64_t));
4901c501c352SStefan Hajnoczi     if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
4902c501c352SStefan Hajnoczi         error_setg(&local_err, "The image size is too large "
4903c501c352SStefan Hajnoczi                                "(try using a larger cluster size)");
4904c501c352SStefan Hajnoczi         goto err;
4905c501c352SStefan Hajnoczi     }
4906c501c352SStefan Hajnoczi 
4907c501c352SStefan Hajnoczi     /* Account for input image */
4908c501c352SStefan Hajnoczi     if (in_bs) {
4909c501c352SStefan Hajnoczi         int64_t ssize = bdrv_getlength(in_bs);
4910c501c352SStefan Hajnoczi         if (ssize < 0) {
4911c501c352SStefan Hajnoczi             error_setg_errno(&local_err, -ssize,
4912c501c352SStefan Hajnoczi                              "Unable to get image virtual_size");
4913c501c352SStefan Hajnoczi             goto err;
4914c501c352SStefan Hajnoczi         }
4915c501c352SStefan Hajnoczi 
49169e029689SAlberto Garcia         virtual_size = ROUND_UP(ssize, cluster_size);
4917c501c352SStefan Hajnoczi 
4918c501c352SStefan Hajnoczi         if (has_backing_file) {
4919c501c352SStefan Hajnoczi             /* We don't how much of the backing chain is shared by the input
4920c501c352SStefan Hajnoczi              * image and the new image file.  In the worst case the new image's
4921c501c352SStefan Hajnoczi              * backing file has nothing in common with the input image.  Be
4922c501c352SStefan Hajnoczi              * conservative and assume all clusters need to be written.
4923c501c352SStefan Hajnoczi              */
4924c501c352SStefan Hajnoczi             required = virtual_size;
4925c501c352SStefan Hajnoczi         } else {
4926b85ee453SEric Blake             int64_t offset;
492731826642SEric Blake             int64_t pnum = 0;
4928c501c352SStefan Hajnoczi 
492931826642SEric Blake             for (offset = 0; offset < ssize; offset += pnum) {
493031826642SEric Blake                 int ret;
4931c501c352SStefan Hajnoczi 
493231826642SEric Blake                 ret = bdrv_block_status_above(in_bs, NULL, offset,
493331826642SEric Blake                                               ssize - offset, &pnum, NULL,
493431826642SEric Blake                                               NULL);
4935c501c352SStefan Hajnoczi                 if (ret < 0) {
4936c501c352SStefan Hajnoczi                     error_setg_errno(&local_err, -ret,
4937c501c352SStefan Hajnoczi                                      "Unable to get block status");
4938c501c352SStefan Hajnoczi                     goto err;
4939c501c352SStefan Hajnoczi                 }
4940c501c352SStefan Hajnoczi 
4941c501c352SStefan Hajnoczi                 if (ret & BDRV_BLOCK_ZERO) {
4942c501c352SStefan Hajnoczi                     /* Skip zero regions (safe with no backing file) */
4943c501c352SStefan Hajnoczi                 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
4944c501c352SStefan Hajnoczi                            (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
4945c501c352SStefan Hajnoczi                     /* Extend pnum to end of cluster for next iteration */
494631826642SEric Blake                     pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
4947c501c352SStefan Hajnoczi 
4948c501c352SStefan Hajnoczi                     /* Count clusters we've seen */
494931826642SEric Blake                     required += offset % cluster_size + pnum;
4950c501c352SStefan Hajnoczi                 }
4951c501c352SStefan Hajnoczi             }
4952c501c352SStefan Hajnoczi         }
4953c501c352SStefan Hajnoczi     }
4954c501c352SStefan Hajnoczi 
4955c501c352SStefan Hajnoczi     /* Take into account preallocation.  Nothing special is needed for
4956c501c352SStefan Hajnoczi      * PREALLOC_MODE_METADATA since metadata is always counted.
4957c501c352SStefan Hajnoczi      */
4958c501c352SStefan Hajnoczi     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
4959c501c352SStefan Hajnoczi         required = virtual_size;
4960c501c352SStefan Hajnoczi     }
4961c501c352SStefan Hajnoczi 
49625d72c68bSEric Blake     info = g_new0(BlockMeasureInfo, 1);
4963c501c352SStefan Hajnoczi     info->fully_allocated =
4964c501c352SStefan Hajnoczi         qcow2_calc_prealloc_size(virtual_size, cluster_size,
496561914f89SStefan Hajnoczi                                  ctz32(refcount_bits)) + luks_payload_size;
4966c501c352SStefan Hajnoczi 
49675d72c68bSEric Blake     /*
49685d72c68bSEric Blake      * Remove data clusters that are not required.  This overestimates the
4969c501c352SStefan Hajnoczi      * required size because metadata needed for the fully allocated file is
49705d72c68bSEric Blake      * still counted.  Show bitmaps only if both source and destination
49715d72c68bSEric Blake      * would support them.
4972c501c352SStefan Hajnoczi      */
4973c501c352SStefan Hajnoczi     info->required = info->fully_allocated - virtual_size + required;
49745d72c68bSEric Blake     info->has_bitmaps = version >= 3 && in_bs &&
49755d72c68bSEric Blake         bdrv_supports_persistent_dirty_bitmap(in_bs);
49765d72c68bSEric Blake     if (info->has_bitmaps) {
49775d72c68bSEric Blake         info->bitmaps = qcow2_get_persistent_dirty_bitmap_size(in_bs,
49785d72c68bSEric Blake                                                                cluster_size);
49795d72c68bSEric Blake     }
4980c501c352SStefan Hajnoczi     return info;
4981c501c352SStefan Hajnoczi 
4982c501c352SStefan Hajnoczi err:
4983c501c352SStefan Hajnoczi     error_propagate(errp, local_err);
4984c501c352SStefan Hajnoczi     return NULL;
4985c501c352SStefan Hajnoczi }
4986c501c352SStefan Hajnoczi 
49877c80ab3fSJes Sorensen static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
498820d97356SBlue Swirl {
4989ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
499020d97356SBlue Swirl     bdi->cluster_size = s->cluster_size;
49917c80ab3fSJes Sorensen     bdi->vm_state_offset = qcow2_vm_state_offset(s);
499220d97356SBlue Swirl     return 0;
499320d97356SBlue Swirl }
499420d97356SBlue Swirl 
49951bf6e9caSAndrey Shinkevich static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs,
49961bf6e9caSAndrey Shinkevich                                                   Error **errp)
499737764dfbSMax Reitz {
4998ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
49990a12f6f8SDaniel P. Berrange     ImageInfoSpecific *spec_info;
50000a12f6f8SDaniel P. Berrange     QCryptoBlockInfo *encrypt_info = NULL;
50011bf6e9caSAndrey Shinkevich     Error *local_err = NULL;
500237764dfbSMax Reitz 
50030a12f6f8SDaniel P. Berrange     if (s->crypto != NULL) {
50041bf6e9caSAndrey Shinkevich         encrypt_info = qcrypto_block_get_info(s->crypto, &local_err);
50051bf6e9caSAndrey Shinkevich         if (local_err) {
50061bf6e9caSAndrey Shinkevich             error_propagate(errp, local_err);
50071bf6e9caSAndrey Shinkevich             return NULL;
50081bf6e9caSAndrey Shinkevich         }
50090a12f6f8SDaniel P. Berrange     }
50100a12f6f8SDaniel P. Berrange 
50110a12f6f8SDaniel P. Berrange     spec_info = g_new(ImageInfoSpecific, 1);
501237764dfbSMax Reitz     *spec_info = (ImageInfoSpecific){
50136a8f9661SEric Blake         .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
5014b8968c87SAndrey Shinkevich         .u.qcow2.data = g_new0(ImageInfoSpecificQCow2, 1),
501537764dfbSMax Reitz     };
501637764dfbSMax Reitz     if (s->qcow_version == 2) {
501732bafa8fSEric Blake         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
501837764dfbSMax Reitz             .compat             = g_strdup("0.10"),
50190709c5a1SMax Reitz             .refcount_bits      = s->refcount_bits,
502037764dfbSMax Reitz         };
502137764dfbSMax Reitz     } else if (s->qcow_version == 3) {
5022b8968c87SAndrey Shinkevich         Qcow2BitmapInfoList *bitmaps;
5023b8968c87SAndrey Shinkevich         bitmaps = qcow2_get_bitmap_info_list(bs, &local_err);
5024b8968c87SAndrey Shinkevich         if (local_err) {
5025b8968c87SAndrey Shinkevich             error_propagate(errp, local_err);
5026b8968c87SAndrey Shinkevich             qapi_free_ImageInfoSpecific(spec_info);
502771eaec2eSEric Blake             qapi_free_QCryptoBlockInfo(encrypt_info);
5028b8968c87SAndrey Shinkevich             return NULL;
5029b8968c87SAndrey Shinkevich         }
503032bafa8fSEric Blake         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
503137764dfbSMax Reitz             .compat             = g_strdup("1.1"),
503237764dfbSMax Reitz             .lazy_refcounts     = s->compatible_features &
503337764dfbSMax Reitz                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
503437764dfbSMax Reitz             .has_lazy_refcounts = true,
50359009b196SMax Reitz             .corrupt            = s->incompatible_features &
50369009b196SMax Reitz                                   QCOW2_INCOMPAT_CORRUPT,
50379009b196SMax Reitz             .has_corrupt        = true,
50380709c5a1SMax Reitz             .refcount_bits      = s->refcount_bits,
5039b8968c87SAndrey Shinkevich             .has_bitmaps        = !!bitmaps,
5040b8968c87SAndrey Shinkevich             .bitmaps            = bitmaps,
50419b890bdcSKevin Wolf             .has_data_file      = !!s->image_data_file,
50429b890bdcSKevin Wolf             .data_file          = g_strdup(s->image_data_file),
50436c3944dcSKevin Wolf             .has_data_file_raw  = has_data_file(bs),
50446c3944dcSKevin Wolf             .data_file_raw      = data_file_is_raw(bs),
5045572ad978SDenis Plotnikov             .compression_type   = s->compression_type,
504637764dfbSMax Reitz         };
5047b1fc8f93SDenis V. Lunev     } else {
5048b1fc8f93SDenis V. Lunev         /* if this assertion fails, this probably means a new version was
5049b1fc8f93SDenis V. Lunev          * added without having it covered here */
5050b1fc8f93SDenis V. Lunev         assert(false);
505137764dfbSMax Reitz     }
505237764dfbSMax Reitz 
50530a12f6f8SDaniel P. Berrange     if (encrypt_info) {
50540a12f6f8SDaniel P. Berrange         ImageInfoSpecificQCow2Encryption *qencrypt =
50550a12f6f8SDaniel P. Berrange             g_new(ImageInfoSpecificQCow2Encryption, 1);
50560a12f6f8SDaniel P. Berrange         switch (encrypt_info->format) {
50570a12f6f8SDaniel P. Berrange         case Q_CRYPTO_BLOCK_FORMAT_QCOW:
50580a12f6f8SDaniel P. Berrange             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
50590a12f6f8SDaniel P. Berrange             break;
50600a12f6f8SDaniel P. Berrange         case Q_CRYPTO_BLOCK_FORMAT_LUKS:
50610a12f6f8SDaniel P. Berrange             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
50620a12f6f8SDaniel P. Berrange             qencrypt->u.luks = encrypt_info->u.luks;
50630a12f6f8SDaniel P. Berrange             break;
50640a12f6f8SDaniel P. Berrange         default:
50650a12f6f8SDaniel P. Berrange             abort();
50660a12f6f8SDaniel P. Berrange         }
50670a12f6f8SDaniel P. Berrange         /* Since we did shallow copy above, erase any pointers
50680a12f6f8SDaniel P. Berrange          * in the original info */
50690a12f6f8SDaniel P. Berrange         memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
50700a12f6f8SDaniel P. Berrange         qapi_free_QCryptoBlockInfo(encrypt_info);
50710a12f6f8SDaniel P. Berrange 
50720a12f6f8SDaniel P. Berrange         spec_info->u.qcow2.data->has_encrypt = true;
50730a12f6f8SDaniel P. Berrange         spec_info->u.qcow2.data->encrypt = qencrypt;
50740a12f6f8SDaniel P. Berrange     }
50750a12f6f8SDaniel P. Berrange 
507637764dfbSMax Reitz     return spec_info;
507737764dfbSMax Reitz }
507837764dfbSMax Reitz 
507938841dcdSMax Reitz static int qcow2_has_zero_init(BlockDriverState *bs)
508038841dcdSMax Reitz {
508138841dcdSMax Reitz     BDRVQcow2State *s = bs->opaque;
508238841dcdSMax Reitz     bool preallocated;
508338841dcdSMax Reitz 
508438841dcdSMax Reitz     if (qemu_in_coroutine()) {
508538841dcdSMax Reitz         qemu_co_mutex_lock(&s->lock);
508638841dcdSMax Reitz     }
508738841dcdSMax Reitz     /*
508838841dcdSMax Reitz      * Check preallocation status: Preallocated images have all L2
508938841dcdSMax Reitz      * tables allocated, nonpreallocated images have none.  It is
509038841dcdSMax Reitz      * therefore enough to check the first one.
509138841dcdSMax Reitz      */
509238841dcdSMax Reitz     preallocated = s->l1_size > 0 && s->l1_table[0] != 0;
509338841dcdSMax Reitz     if (qemu_in_coroutine()) {
509438841dcdSMax Reitz         qemu_co_mutex_unlock(&s->lock);
509538841dcdSMax Reitz     }
509638841dcdSMax Reitz 
509738841dcdSMax Reitz     if (!preallocated) {
509838841dcdSMax Reitz         return 1;
509938841dcdSMax Reitz     } else if (bs->encrypted) {
510038841dcdSMax Reitz         return 0;
510138841dcdSMax Reitz     } else {
510238841dcdSMax Reitz         return bdrv_has_zero_init(s->data_file->bs);
510338841dcdSMax Reitz     }
510438841dcdSMax Reitz }
510538841dcdSMax Reitz 
5106cf8074b3SKevin Wolf static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
5107cf8074b3SKevin Wolf                               int64_t pos)
510820d97356SBlue Swirl {
5109ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
511020d97356SBlue Swirl 
511166f82ceeSKevin Wolf     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
51125396234bSVladimir Sementsov-Ogievskiy     return bs->drv->bdrv_co_pwritev_part(bs, qcow2_vm_state_offset(s) + pos,
51135396234bSVladimir Sementsov-Ogievskiy                                          qiov->size, qiov, 0, 0);
511420d97356SBlue Swirl }
511520d97356SBlue Swirl 
51165ddda0b8SKevin Wolf static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
51175ddda0b8SKevin Wolf                               int64_t pos)
511820d97356SBlue Swirl {
5119ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
512020d97356SBlue Swirl 
512166f82ceeSKevin Wolf     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
5122df893d25SVladimir Sementsov-Ogievskiy     return bs->drv->bdrv_co_preadv_part(bs, qcow2_vm_state_offset(s) + pos,
5123df893d25SVladimir Sementsov-Ogievskiy                                         qiov->size, qiov, 0, 0);
512420d97356SBlue Swirl }
512520d97356SBlue Swirl 
51269296b3edSMax Reitz /*
51279296b3edSMax Reitz  * Downgrades an image's version. To achieve this, any incompatible features
51289296b3edSMax Reitz  * have to be removed.
51299296b3edSMax Reitz  */
51304057a2b2SMax Reitz static int qcow2_downgrade(BlockDriverState *bs, int target_version,
5131d1402b50SMax Reitz                            BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
5132d1402b50SMax Reitz                            Error **errp)
51339296b3edSMax Reitz {
5134ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
51359296b3edSMax Reitz     int current_version = s->qcow_version;
51369296b3edSMax Reitz     int ret;
51377fa140abSEric Blake     int i;
51389296b3edSMax Reitz 
5139d1402b50SMax Reitz     /* This is qcow2_downgrade(), not qcow2_upgrade() */
5140d1402b50SMax Reitz     assert(target_version < current_version);
5141d1402b50SMax Reitz 
5142d1402b50SMax Reitz     /* There are no other versions (now) that you can downgrade to */
5143d1402b50SMax Reitz     assert(target_version == 2);
51449296b3edSMax Reitz 
51459296b3edSMax Reitz     if (s->refcount_order != 4) {
5146d1402b50SMax Reitz         error_setg(errp, "compat=0.10 requires refcount_bits=16");
51479296b3edSMax Reitz         return -ENOTSUP;
51489296b3edSMax Reitz     }
51499296b3edSMax Reitz 
5150966b000fSKevin Wolf     if (has_data_file(bs)) {
5151966b000fSKevin Wolf         error_setg(errp, "Cannot downgrade an image with a data file");
5152966b000fSKevin Wolf         return -ENOTSUP;
5153966b000fSKevin Wolf     }
5154966b000fSKevin Wolf 
51557fa140abSEric Blake     /*
51567fa140abSEric Blake      * If any internal snapshot has a different size than the current
51577fa140abSEric Blake      * image size, or VM state size that exceeds 32 bits, downgrading
51587fa140abSEric Blake      * is unsafe.  Even though we would still use v3-compliant output
51597fa140abSEric Blake      * to preserve that data, other v2 programs might not realize
51607fa140abSEric Blake      * those optional fields are important.
51617fa140abSEric Blake      */
51627fa140abSEric Blake     for (i = 0; i < s->nb_snapshots; i++) {
51637fa140abSEric Blake         if (s->snapshots[i].vm_state_size > UINT32_MAX ||
51647fa140abSEric Blake             s->snapshots[i].disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
51657fa140abSEric Blake             error_setg(errp, "Internal snapshots prevent downgrade of image");
51667fa140abSEric Blake             return -ENOTSUP;
51677fa140abSEric Blake         }
51687fa140abSEric Blake     }
51697fa140abSEric Blake 
51709296b3edSMax Reitz     /* clear incompatible features */
51719296b3edSMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
51729296b3edSMax Reitz         ret = qcow2_mark_clean(bs);
51739296b3edSMax Reitz         if (ret < 0) {
5174d1402b50SMax Reitz             error_setg_errno(errp, -ret, "Failed to make the image clean");
51759296b3edSMax Reitz             return ret;
51769296b3edSMax Reitz         }
51779296b3edSMax Reitz     }
51789296b3edSMax Reitz 
51799296b3edSMax Reitz     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
51809296b3edSMax Reitz      * the first place; if that happens nonetheless, returning -ENOTSUP is the
51819296b3edSMax Reitz      * best thing to do anyway */
51829296b3edSMax Reitz 
51839296b3edSMax Reitz     if (s->incompatible_features) {
5184d1402b50SMax Reitz         error_setg(errp, "Cannot downgrade an image with incompatible features "
5185d1402b50SMax Reitz                    "%#" PRIx64 " set", s->incompatible_features);
51869296b3edSMax Reitz         return -ENOTSUP;
51879296b3edSMax Reitz     }
51889296b3edSMax Reitz 
51899296b3edSMax Reitz     /* since we can ignore compatible features, we can set them to 0 as well */
51909296b3edSMax Reitz     s->compatible_features = 0;
51919296b3edSMax Reitz     /* if lazy refcounts have been used, they have already been fixed through
51929296b3edSMax Reitz      * clearing the dirty flag */
51939296b3edSMax Reitz 
51949296b3edSMax Reitz     /* clearing autoclear features is trivial */
51959296b3edSMax Reitz     s->autoclear_features = 0;
51969296b3edSMax Reitz 
51978b13976dSMax Reitz     ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
51989296b3edSMax Reitz     if (ret < 0) {
5199d1402b50SMax Reitz         error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
52009296b3edSMax Reitz         return ret;
52019296b3edSMax Reitz     }
52029296b3edSMax Reitz 
52039296b3edSMax Reitz     s->qcow_version = target_version;
52049296b3edSMax Reitz     ret = qcow2_update_header(bs);
52059296b3edSMax Reitz     if (ret < 0) {
52069296b3edSMax Reitz         s->qcow_version = current_version;
5207d1402b50SMax Reitz         error_setg_errno(errp, -ret, "Failed to update the image header");
52089296b3edSMax Reitz         return ret;
52099296b3edSMax Reitz     }
52109296b3edSMax Reitz     return 0;
52119296b3edSMax Reitz }
52129296b3edSMax Reitz 
5213722efb0cSMax Reitz /*
5214722efb0cSMax Reitz  * Upgrades an image's version.  While newer versions encompass all
5215722efb0cSMax Reitz  * features of older versions, some things may have to be presented
5216722efb0cSMax Reitz  * differently.
5217722efb0cSMax Reitz  */
5218722efb0cSMax Reitz static int qcow2_upgrade(BlockDriverState *bs, int target_version,
5219722efb0cSMax Reitz                          BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
5220722efb0cSMax Reitz                          Error **errp)
5221722efb0cSMax Reitz {
5222722efb0cSMax Reitz     BDRVQcow2State *s = bs->opaque;
52230a85af35SMax Reitz     bool need_snapshot_update;
5224722efb0cSMax Reitz     int current_version = s->qcow_version;
52250a85af35SMax Reitz     int i;
5226722efb0cSMax Reitz     int ret;
5227722efb0cSMax Reitz 
5228722efb0cSMax Reitz     /* This is qcow2_upgrade(), not qcow2_downgrade() */
5229722efb0cSMax Reitz     assert(target_version > current_version);
5230722efb0cSMax Reitz 
5231722efb0cSMax Reitz     /* There are no other versions (yet) that you can upgrade to */
5232722efb0cSMax Reitz     assert(target_version == 3);
5233722efb0cSMax Reitz 
52340a85af35SMax Reitz     status_cb(bs, 0, 2, cb_opaque);
52350a85af35SMax Reitz 
52360a85af35SMax Reitz     /*
52370a85af35SMax Reitz      * In v2, snapshots do not need to have extra data.  v3 requires
52380a85af35SMax Reitz      * the 64-bit VM state size and the virtual disk size to be
52390a85af35SMax Reitz      * present.
52400a85af35SMax Reitz      * qcow2_write_snapshots() will always write the list in the
52410a85af35SMax Reitz      * v3-compliant format.
52420a85af35SMax Reitz      */
52430a85af35SMax Reitz     need_snapshot_update = false;
52440a85af35SMax Reitz     for (i = 0; i < s->nb_snapshots; i++) {
52450a85af35SMax Reitz         if (s->snapshots[i].extra_data_size <
52460a85af35SMax Reitz             sizeof_field(QCowSnapshotExtraData, vm_state_size_large) +
52470a85af35SMax Reitz             sizeof_field(QCowSnapshotExtraData, disk_size))
52480a85af35SMax Reitz         {
52490a85af35SMax Reitz             need_snapshot_update = true;
52500a85af35SMax Reitz             break;
52510a85af35SMax Reitz         }
52520a85af35SMax Reitz     }
52530a85af35SMax Reitz     if (need_snapshot_update) {
52540a85af35SMax Reitz         ret = qcow2_write_snapshots(bs);
52550a85af35SMax Reitz         if (ret < 0) {
52560a85af35SMax Reitz             error_setg_errno(errp, -ret, "Failed to update the snapshot table");
52570a85af35SMax Reitz             return ret;
52580a85af35SMax Reitz         }
52590a85af35SMax Reitz     }
52600a85af35SMax Reitz     status_cb(bs, 1, 2, cb_opaque);
5261722efb0cSMax Reitz 
5262722efb0cSMax Reitz     s->qcow_version = target_version;
5263722efb0cSMax Reitz     ret = qcow2_update_header(bs);
5264722efb0cSMax Reitz     if (ret < 0) {
5265722efb0cSMax Reitz         s->qcow_version = current_version;
5266722efb0cSMax Reitz         error_setg_errno(errp, -ret, "Failed to update the image header");
5267722efb0cSMax Reitz         return ret;
5268722efb0cSMax Reitz     }
52690a85af35SMax Reitz     status_cb(bs, 2, 2, cb_opaque);
5270722efb0cSMax Reitz 
5271722efb0cSMax Reitz     return 0;
5272722efb0cSMax Reitz }
5273722efb0cSMax Reitz 
5274c293a809SMax Reitz typedef enum Qcow2AmendOperation {
5275c293a809SMax Reitz     /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
5276c293a809SMax Reitz      * statically initialized to so that the helper CB can discern the first
5277c293a809SMax Reitz      * invocation from an operation change */
5278c293a809SMax Reitz     QCOW2_NO_OPERATION = 0,
5279c293a809SMax Reitz 
5280722efb0cSMax Reitz     QCOW2_UPGRADING,
528190766d9dSMaxim Levitsky     QCOW2_UPDATING_ENCRYPTION,
528261ce55fcSMax Reitz     QCOW2_CHANGING_REFCOUNT_ORDER,
5283c293a809SMax Reitz     QCOW2_DOWNGRADING,
5284c293a809SMax Reitz } Qcow2AmendOperation;
5285c293a809SMax Reitz 
5286c293a809SMax Reitz typedef struct Qcow2AmendHelperCBInfo {
5287c293a809SMax Reitz     /* The code coordinating the amend operations should only modify
5288c293a809SMax Reitz      * these four fields; the rest will be managed by the CB */
5289c293a809SMax Reitz     BlockDriverAmendStatusCB *original_status_cb;
5290c293a809SMax Reitz     void *original_cb_opaque;
5291c293a809SMax Reitz 
5292c293a809SMax Reitz     Qcow2AmendOperation current_operation;
5293c293a809SMax Reitz 
5294c293a809SMax Reitz     /* Total number of operations to perform (only set once) */
5295c293a809SMax Reitz     int total_operations;
5296c293a809SMax Reitz 
5297c293a809SMax Reitz     /* The following fields are managed by the CB */
5298c293a809SMax Reitz 
5299c293a809SMax Reitz     /* Number of operations completed */
5300c293a809SMax Reitz     int operations_completed;
5301c293a809SMax Reitz 
5302c293a809SMax Reitz     /* Cumulative offset of all completed operations */
5303c293a809SMax Reitz     int64_t offset_completed;
5304c293a809SMax Reitz 
5305c293a809SMax Reitz     Qcow2AmendOperation last_operation;
5306c293a809SMax Reitz     int64_t last_work_size;
5307c293a809SMax Reitz } Qcow2AmendHelperCBInfo;
5308c293a809SMax Reitz 
5309c293a809SMax Reitz static void qcow2_amend_helper_cb(BlockDriverState *bs,
5310c293a809SMax Reitz                                   int64_t operation_offset,
5311c293a809SMax Reitz                                   int64_t operation_work_size, void *opaque)
5312c293a809SMax Reitz {
5313c293a809SMax Reitz     Qcow2AmendHelperCBInfo *info = opaque;
5314c293a809SMax Reitz     int64_t current_work_size;
5315c293a809SMax Reitz     int64_t projected_work_size;
5316c293a809SMax Reitz 
5317c293a809SMax Reitz     if (info->current_operation != info->last_operation) {
5318c293a809SMax Reitz         if (info->last_operation != QCOW2_NO_OPERATION) {
5319c293a809SMax Reitz             info->offset_completed += info->last_work_size;
5320c293a809SMax Reitz             info->operations_completed++;
5321c293a809SMax Reitz         }
5322c293a809SMax Reitz 
5323c293a809SMax Reitz         info->last_operation = info->current_operation;
5324c293a809SMax Reitz     }
5325c293a809SMax Reitz 
5326c293a809SMax Reitz     assert(info->total_operations > 0);
5327c293a809SMax Reitz     assert(info->operations_completed < info->total_operations);
5328c293a809SMax Reitz 
5329c293a809SMax Reitz     info->last_work_size = operation_work_size;
5330c293a809SMax Reitz 
5331c293a809SMax Reitz     current_work_size = info->offset_completed + operation_work_size;
5332c293a809SMax Reitz 
5333c293a809SMax Reitz     /* current_work_size is the total work size for (operations_completed + 1)
5334c293a809SMax Reitz      * operations (which includes this one), so multiply it by the number of
5335c293a809SMax Reitz      * operations not covered and divide it by the number of operations
5336c293a809SMax Reitz      * covered to get a projection for the operations not covered */
5337c293a809SMax Reitz     projected_work_size = current_work_size * (info->total_operations -
5338c293a809SMax Reitz                                                info->operations_completed - 1)
5339c293a809SMax Reitz                                             / (info->operations_completed + 1);
5340c293a809SMax Reitz 
5341c293a809SMax Reitz     info->original_status_cb(bs, info->offset_completed + operation_offset,
5342c293a809SMax Reitz                              current_work_size + projected_work_size,
5343c293a809SMax Reitz                              info->original_cb_opaque);
5344c293a809SMax Reitz }
5345c293a809SMax Reitz 
534677485434SMax Reitz static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
53478b13976dSMax Reitz                                BlockDriverAmendStatusCB *status_cb,
5348d1402b50SMax Reitz                                void *cb_opaque,
5349a3579bfaSMaxim Levitsky                                bool force,
5350d1402b50SMax Reitz                                Error **errp)
53519296b3edSMax Reitz {
5352ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
53539296b3edSMax Reitz     int old_version = s->qcow_version, new_version = old_version;
53549296b3edSMax Reitz     uint64_t new_size = 0;
53559b890bdcSKevin Wolf     const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL;
53569296b3edSMax Reitz     bool lazy_refcounts = s->use_lazy_refcounts;
53576c3944dcSKevin Wolf     bool data_file_raw = data_file_is_raw(bs);
53581bd0e2d1SChunyan Liu     const char *compat = NULL;
535961ce55fcSMax Reitz     int refcount_bits = s->refcount_bits;
53609296b3edSMax Reitz     int ret;
53611bd0e2d1SChunyan Liu     QemuOptDesc *desc = opts->list->desc;
5362c293a809SMax Reitz     Qcow2AmendHelperCBInfo helper_cb_info;
536390766d9dSMaxim Levitsky     bool encryption_update = false;
53649296b3edSMax Reitz 
53651bd0e2d1SChunyan Liu     while (desc && desc->name) {
53661bd0e2d1SChunyan Liu         if (!qemu_opt_find(opts, desc->name)) {
53679296b3edSMax Reitz             /* only change explicitly defined options */
53681bd0e2d1SChunyan Liu             desc++;
53699296b3edSMax Reitz             continue;
53709296b3edSMax Reitz         }
53719296b3edSMax Reitz 
53728a17b83cSMax Reitz         if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
53738a17b83cSMax Reitz             compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
53741bd0e2d1SChunyan Liu             if (!compat) {
53759296b3edSMax Reitz                 /* preserve default */
5376f7077c98SEric Blake             } else if (!strcmp(compat, "0.10") || !strcmp(compat, "v2")) {
53779296b3edSMax Reitz                 new_version = 2;
5378f7077c98SEric Blake             } else if (!strcmp(compat, "1.1") || !strcmp(compat, "v3")) {
53799296b3edSMax Reitz                 new_version = 3;
53809296b3edSMax Reitz             } else {
5381d1402b50SMax Reitz                 error_setg(errp, "Unknown compatibility level %s", compat);
53829296b3edSMax Reitz                 return -EINVAL;
53839296b3edSMax Reitz             }
53848a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
53858a17b83cSMax Reitz             new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
53868a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
53878a17b83cSMax Reitz             backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
53888a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
53898a17b83cSMax Reitz             backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
539090766d9dSMaxim Levitsky         } else if (g_str_has_prefix(desc->name, "encrypt.")) {
539190766d9dSMaxim Levitsky             if (!s->crypto) {
539290766d9dSMaxim Levitsky                 error_setg(errp,
539390766d9dSMaxim Levitsky                            "Can't amend encryption options - encryption not present");
539490766d9dSMaxim Levitsky                 return -EINVAL;
539590766d9dSMaxim Levitsky             }
539690766d9dSMaxim Levitsky             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
539790766d9dSMaxim Levitsky                 error_setg(errp,
539890766d9dSMaxim Levitsky                            "Only LUKS encryption options can be amended");
539990766d9dSMaxim Levitsky                 return -ENOTSUP;
540090766d9dSMaxim Levitsky             }
540190766d9dSMaxim Levitsky             encryption_update = true;
54028a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
54038a17b83cSMax Reitz             lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
54041bd0e2d1SChunyan Liu                                                lazy_refcounts);
540506d05fa7SMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
540661ce55fcSMax Reitz             refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
540761ce55fcSMax Reitz                                                 refcount_bits);
540861ce55fcSMax Reitz 
540961ce55fcSMax Reitz             if (refcount_bits <= 0 || refcount_bits > 64 ||
541061ce55fcSMax Reitz                 !is_power_of_2(refcount_bits))
541161ce55fcSMax Reitz             {
5412d1402b50SMax Reitz                 error_setg(errp, "Refcount width must be a power of two and "
5413d1402b50SMax Reitz                            "may not exceed 64 bits");
541461ce55fcSMax Reitz                 return -EINVAL;
541561ce55fcSMax Reitz             }
54169b890bdcSKevin Wolf         } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) {
54179b890bdcSKevin Wolf             data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE);
54189b890bdcSKevin Wolf             if (data_file && !has_data_file(bs)) {
54199b890bdcSKevin Wolf                 error_setg(errp, "data-file can only be set for images that "
54209b890bdcSKevin Wolf                                  "use an external data file");
54219b890bdcSKevin Wolf                 return -EINVAL;
54229b890bdcSKevin Wolf             }
54236c3944dcSKevin Wolf         } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE_RAW)) {
54246c3944dcSKevin Wolf             data_file_raw = qemu_opt_get_bool(opts, BLOCK_OPT_DATA_FILE_RAW,
54256c3944dcSKevin Wolf                                               data_file_raw);
54266c3944dcSKevin Wolf             if (data_file_raw && !data_file_is_raw(bs)) {
54276c3944dcSKevin Wolf                 error_setg(errp, "data-file-raw cannot be set on existing "
54286c3944dcSKevin Wolf                                  "images");
54296c3944dcSKevin Wolf                 return -EINVAL;
54306c3944dcSKevin Wolf             }
54319296b3edSMax Reitz         } else {
5432164e0f89SMax Reitz             /* if this point is reached, this probably means a new option was
54339296b3edSMax Reitz              * added without having it covered here */
5434164e0f89SMax Reitz             abort();
54359296b3edSMax Reitz         }
54361bd0e2d1SChunyan Liu 
54371bd0e2d1SChunyan Liu         desc++;
54389296b3edSMax Reitz     }
54399296b3edSMax Reitz 
5440c293a809SMax Reitz     helper_cb_info = (Qcow2AmendHelperCBInfo){
5441c293a809SMax Reitz         .original_status_cb = status_cb,
5442c293a809SMax Reitz         .original_cb_opaque = cb_opaque,
5443722efb0cSMax Reitz         .total_operations = (new_version != old_version)
544490766d9dSMaxim Levitsky                           + (s->refcount_bits != refcount_bits) +
544590766d9dSMaxim Levitsky                             (encryption_update == true)
5446c293a809SMax Reitz     };
5447c293a809SMax Reitz 
54481038bbb8SMax Reitz     /* Upgrade first (some features may require compat=1.1) */
54499296b3edSMax Reitz     if (new_version > old_version) {
5450722efb0cSMax Reitz         helper_cb_info.current_operation = QCOW2_UPGRADING;
5451722efb0cSMax Reitz         ret = qcow2_upgrade(bs, new_version, &qcow2_amend_helper_cb,
5452722efb0cSMax Reitz                             &helper_cb_info, errp);
54539296b3edSMax Reitz         if (ret < 0) {
54549296b3edSMax Reitz             return ret;
54559296b3edSMax Reitz         }
54569296b3edSMax Reitz     }
54579296b3edSMax Reitz 
545890766d9dSMaxim Levitsky     if (encryption_update) {
545990766d9dSMaxim Levitsky         QDict *amend_opts_dict;
546090766d9dSMaxim Levitsky         QCryptoBlockAmendOptions *amend_opts;
546190766d9dSMaxim Levitsky 
546290766d9dSMaxim Levitsky         helper_cb_info.current_operation = QCOW2_UPDATING_ENCRYPTION;
546390766d9dSMaxim Levitsky         amend_opts_dict = qcow2_extract_crypto_opts(opts, "luks", errp);
546490766d9dSMaxim Levitsky         if (!amend_opts_dict) {
546590766d9dSMaxim Levitsky             return -EINVAL;
546690766d9dSMaxim Levitsky         }
546790766d9dSMaxim Levitsky         amend_opts = block_crypto_amend_opts_init(amend_opts_dict, errp);
546890766d9dSMaxim Levitsky         qobject_unref(amend_opts_dict);
546990766d9dSMaxim Levitsky         if (!amend_opts) {
547090766d9dSMaxim Levitsky             return -EINVAL;
547190766d9dSMaxim Levitsky         }
547290766d9dSMaxim Levitsky         ret = qcrypto_block_amend_options(s->crypto,
547390766d9dSMaxim Levitsky                                           qcow2_crypto_hdr_read_func,
547490766d9dSMaxim Levitsky                                           qcow2_crypto_hdr_write_func,
547590766d9dSMaxim Levitsky                                           bs,
547690766d9dSMaxim Levitsky                                           amend_opts,
547790766d9dSMaxim Levitsky                                           force,
547890766d9dSMaxim Levitsky                                           errp);
547990766d9dSMaxim Levitsky         qapi_free_QCryptoBlockAmendOptions(amend_opts);
548090766d9dSMaxim Levitsky         if (ret < 0) {
548190766d9dSMaxim Levitsky             return ret;
548290766d9dSMaxim Levitsky         }
548390766d9dSMaxim Levitsky     }
548490766d9dSMaxim Levitsky 
548561ce55fcSMax Reitz     if (s->refcount_bits != refcount_bits) {
548661ce55fcSMax Reitz         int refcount_order = ctz32(refcount_bits);
548761ce55fcSMax Reitz 
548861ce55fcSMax Reitz         if (new_version < 3 && refcount_bits != 16) {
5489d1402b50SMax Reitz             error_setg(errp, "Refcount widths other than 16 bits require "
549061ce55fcSMax Reitz                        "compatibility level 1.1 or above (use compat=1.1 or "
549161ce55fcSMax Reitz                        "greater)");
549261ce55fcSMax Reitz             return -EINVAL;
549361ce55fcSMax Reitz         }
549461ce55fcSMax Reitz 
549561ce55fcSMax Reitz         helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
549661ce55fcSMax Reitz         ret = qcow2_change_refcount_order(bs, refcount_order,
549761ce55fcSMax Reitz                                           &qcow2_amend_helper_cb,
5498d1402b50SMax Reitz                                           &helper_cb_info, errp);
549961ce55fcSMax Reitz         if (ret < 0) {
550061ce55fcSMax Reitz             return ret;
550161ce55fcSMax Reitz         }
550261ce55fcSMax Reitz     }
550361ce55fcSMax Reitz 
55046c3944dcSKevin Wolf     /* data-file-raw blocks backing files, so clear it first if requested */
55056c3944dcSKevin Wolf     if (data_file_raw) {
55066c3944dcSKevin Wolf         s->autoclear_features |= QCOW2_AUTOCLEAR_DATA_FILE_RAW;
55076c3944dcSKevin Wolf     } else {
55086c3944dcSKevin Wolf         s->autoclear_features &= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW;
55096c3944dcSKevin Wolf     }
55106c3944dcSKevin Wolf 
55119b890bdcSKevin Wolf     if (data_file) {
55129b890bdcSKevin Wolf         g_free(s->image_data_file);
55139b890bdcSKevin Wolf         s->image_data_file = *data_file ? g_strdup(data_file) : NULL;
55149b890bdcSKevin Wolf     }
55159b890bdcSKevin Wolf 
55169b890bdcSKevin Wolf     ret = qcow2_update_header(bs);
55179b890bdcSKevin Wolf     if (ret < 0) {
55189b890bdcSKevin Wolf         error_setg_errno(errp, -ret, "Failed to update the image header");
55199b890bdcSKevin Wolf         return ret;
55209b890bdcSKevin Wolf     }
55219b890bdcSKevin Wolf 
55229296b3edSMax Reitz     if (backing_file || backing_format) {
5523bc5ee6daSEric Blake         if (g_strcmp0(backing_file, s->image_backing_file) ||
5524bc5ee6daSEric Blake             g_strcmp0(backing_format, s->image_backing_format)) {
5525bc5ee6daSEric Blake             warn_report("Deprecated use of amend to alter the backing file; "
5526bc5ee6daSEric Blake                         "use qemu-img rebase instead");
5527bc5ee6daSEric Blake         }
5528e4603fe1SKevin Wolf         ret = qcow2_change_backing_file(bs,
5529e4603fe1SKevin Wolf                     backing_file ?: s->image_backing_file,
5530e4603fe1SKevin Wolf                     backing_format ?: s->image_backing_format);
55319296b3edSMax Reitz         if (ret < 0) {
5532d1402b50SMax Reitz             error_setg_errno(errp, -ret, "Failed to change the backing file");
55339296b3edSMax Reitz             return ret;
55349296b3edSMax Reitz         }
55359296b3edSMax Reitz     }
55369296b3edSMax Reitz 
55379296b3edSMax Reitz     if (s->use_lazy_refcounts != lazy_refcounts) {
55389296b3edSMax Reitz         if (lazy_refcounts) {
55391038bbb8SMax Reitz             if (new_version < 3) {
5540d1402b50SMax Reitz                 error_setg(errp, "Lazy refcounts only supported with "
5541d1402b50SMax Reitz                            "compatibility level 1.1 and above (use compat=1.1 "
5542d1402b50SMax Reitz                            "or greater)");
55439296b3edSMax Reitz                 return -EINVAL;
55449296b3edSMax Reitz             }
55459296b3edSMax Reitz             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
55469296b3edSMax Reitz             ret = qcow2_update_header(bs);
55479296b3edSMax Reitz             if (ret < 0) {
55489296b3edSMax Reitz                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
5549d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update the image header");
55509296b3edSMax Reitz                 return ret;
55519296b3edSMax Reitz             }
55529296b3edSMax Reitz             s->use_lazy_refcounts = true;
55539296b3edSMax Reitz         } else {
55549296b3edSMax Reitz             /* make image clean first */
55559296b3edSMax Reitz             ret = qcow2_mark_clean(bs);
55569296b3edSMax Reitz             if (ret < 0) {
5557d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to make the image clean");
55589296b3edSMax Reitz                 return ret;
55599296b3edSMax Reitz             }
55609296b3edSMax Reitz             /* now disallow lazy refcounts */
55619296b3edSMax Reitz             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
55629296b3edSMax Reitz             ret = qcow2_update_header(bs);
55639296b3edSMax Reitz             if (ret < 0) {
55649296b3edSMax Reitz                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
5565d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update the image header");
55669296b3edSMax Reitz                 return ret;
55679296b3edSMax Reitz             }
55689296b3edSMax Reitz             s->use_lazy_refcounts = false;
55699296b3edSMax Reitz         }
55709296b3edSMax Reitz     }
55719296b3edSMax Reitz 
55729296b3edSMax Reitz     if (new_size) {
5573a3aeeab5SEric Blake         BlockBackend *blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL,
5574a3aeeab5SEric Blake                                             errp);
5575a3aeeab5SEric Blake         if (!blk) {
5576a3aeeab5SEric Blake             return -EPERM;
5577d7086422SKevin Wolf         }
5578d7086422SKevin Wolf 
5579e8d04f92SMax Reitz         /*
5580e8d04f92SMax Reitz          * Amending image options should ensure that the image has
5581e8d04f92SMax Reitz          * exactly the given new values, so pass exact=true here.
5582e8d04f92SMax Reitz          */
55838c6242b6SKevin Wolf         ret = blk_truncate(blk, new_size, true, PREALLOC_MODE_OFF, 0, errp);
558470b27f36SKevin Wolf         blk_unref(blk);
55859296b3edSMax Reitz         if (ret < 0) {
55869296b3edSMax Reitz             return ret;
55879296b3edSMax Reitz         }
55889296b3edSMax Reitz     }
55899296b3edSMax Reitz 
55901038bbb8SMax Reitz     /* Downgrade last (so unsupported features can be removed before) */
55911038bbb8SMax Reitz     if (new_version < old_version) {
5592c293a809SMax Reitz         helper_cb_info.current_operation = QCOW2_DOWNGRADING;
5593c293a809SMax Reitz         ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
5594d1402b50SMax Reitz                               &helper_cb_info, errp);
55951038bbb8SMax Reitz         if (ret < 0) {
55961038bbb8SMax Reitz             return ret;
55971038bbb8SMax Reitz         }
55981038bbb8SMax Reitz     }
55991038bbb8SMax Reitz 
56009296b3edSMax Reitz     return 0;
56019296b3edSMax Reitz }
56029296b3edSMax Reitz 
56038ea1613dSMaxim Levitsky static int coroutine_fn qcow2_co_amend(BlockDriverState *bs,
56048ea1613dSMaxim Levitsky                                        BlockdevAmendOptions *opts,
56058ea1613dSMaxim Levitsky                                        bool force,
56068ea1613dSMaxim Levitsky                                        Error **errp)
56078ea1613dSMaxim Levitsky {
56088ea1613dSMaxim Levitsky     BlockdevAmendOptionsQcow2 *qopts = &opts->u.qcow2;
56098ea1613dSMaxim Levitsky     BDRVQcow2State *s = bs->opaque;
56108ea1613dSMaxim Levitsky     int ret = 0;
56118ea1613dSMaxim Levitsky 
56128ea1613dSMaxim Levitsky     if (qopts->has_encrypt) {
56138ea1613dSMaxim Levitsky         if (!s->crypto) {
56148ea1613dSMaxim Levitsky             error_setg(errp, "image is not encrypted, can't amend");
56158ea1613dSMaxim Levitsky             return -EOPNOTSUPP;
56168ea1613dSMaxim Levitsky         }
56178ea1613dSMaxim Levitsky 
56188ea1613dSMaxim Levitsky         if (qopts->encrypt->format != Q_CRYPTO_BLOCK_FORMAT_LUKS) {
56198ea1613dSMaxim Levitsky             error_setg(errp,
56208ea1613dSMaxim Levitsky                        "Amend can't be used to change the qcow2 encryption format");
56218ea1613dSMaxim Levitsky             return -EOPNOTSUPP;
56228ea1613dSMaxim Levitsky         }
56238ea1613dSMaxim Levitsky 
56248ea1613dSMaxim Levitsky         if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
56258ea1613dSMaxim Levitsky             error_setg(errp,
56268ea1613dSMaxim Levitsky                        "Only LUKS encryption options can be amended for qcow2 with blockdev-amend");
56278ea1613dSMaxim Levitsky             return -EOPNOTSUPP;
56288ea1613dSMaxim Levitsky         }
56298ea1613dSMaxim Levitsky 
56308ea1613dSMaxim Levitsky         ret = qcrypto_block_amend_options(s->crypto,
56318ea1613dSMaxim Levitsky                                           qcow2_crypto_hdr_read_func,
56328ea1613dSMaxim Levitsky                                           qcow2_crypto_hdr_write_func,
56338ea1613dSMaxim Levitsky                                           bs,
56348ea1613dSMaxim Levitsky                                           qopts->encrypt,
56358ea1613dSMaxim Levitsky                                           force,
56368ea1613dSMaxim Levitsky                                           errp);
56378ea1613dSMaxim Levitsky     }
56388ea1613dSMaxim Levitsky     return ret;
56398ea1613dSMaxim Levitsky }
56408ea1613dSMaxim Levitsky 
564185186ebdSMax Reitz /*
564285186ebdSMax Reitz  * If offset or size are negative, respectively, they will not be included in
564385186ebdSMax Reitz  * the BLOCK_IMAGE_CORRUPTED event emitted.
564485186ebdSMax Reitz  * fatal will be ignored for read-only BDS; corruptions found there will always
564585186ebdSMax Reitz  * be considered non-fatal.
564685186ebdSMax Reitz  */
564785186ebdSMax Reitz void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
564885186ebdSMax Reitz                              int64_t size, const char *message_format, ...)
564985186ebdSMax Reitz {
5650ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
5651dc881b44SAlberto Garcia     const char *node_name;
565285186ebdSMax Reitz     char *message;
565385186ebdSMax Reitz     va_list ap;
565485186ebdSMax Reitz 
5655ddf3b47eSMax Reitz     fatal = fatal && bdrv_is_writable(bs);
565685186ebdSMax Reitz 
565785186ebdSMax Reitz     if (s->signaled_corruption &&
565885186ebdSMax Reitz         (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
565985186ebdSMax Reitz     {
566085186ebdSMax Reitz         return;
566185186ebdSMax Reitz     }
566285186ebdSMax Reitz 
566385186ebdSMax Reitz     va_start(ap, message_format);
566485186ebdSMax Reitz     message = g_strdup_vprintf(message_format, ap);
566585186ebdSMax Reitz     va_end(ap);
566685186ebdSMax Reitz 
566785186ebdSMax Reitz     if (fatal) {
566885186ebdSMax Reitz         fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
566985186ebdSMax Reitz                 "corruption events will be suppressed\n", message);
567085186ebdSMax Reitz     } else {
567185186ebdSMax Reitz         fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
567285186ebdSMax Reitz                 "corruption events will be suppressed\n", message);
567385186ebdSMax Reitz     }
567485186ebdSMax Reitz 
5675dc881b44SAlberto Garcia     node_name = bdrv_get_node_name(bs);
5676dc881b44SAlberto Garcia     qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
5677dc881b44SAlberto Garcia                                           *node_name != '\0', node_name,
5678dc881b44SAlberto Garcia                                           message, offset >= 0, offset,
5679dc881b44SAlberto Garcia                                           size >= 0, size,
56803ab72385SPeter Xu                                           fatal);
568185186ebdSMax Reitz     g_free(message);
568285186ebdSMax Reitz 
568385186ebdSMax Reitz     if (fatal) {
568485186ebdSMax Reitz         qcow2_mark_corrupt(bs);
568585186ebdSMax Reitz         bs->drv = NULL; /* make BDS unusable */
568685186ebdSMax Reitz     }
568785186ebdSMax Reitz 
568885186ebdSMax Reitz     s->signaled_corruption = true;
568985186ebdSMax Reitz }
569085186ebdSMax Reitz 
5691df373fb0SMaxim Levitsky #define QCOW_COMMON_OPTIONS                                         \
5692df373fb0SMaxim Levitsky     {                                                               \
5693df373fb0SMaxim Levitsky         .name = BLOCK_OPT_SIZE,                                     \
5694df373fb0SMaxim Levitsky         .type = QEMU_OPT_SIZE,                                      \
5695df373fb0SMaxim Levitsky         .help = "Virtual disk size"                                 \
5696df373fb0SMaxim Levitsky     },                                                              \
5697df373fb0SMaxim Levitsky     {                                                               \
5698df373fb0SMaxim Levitsky         .name = BLOCK_OPT_COMPAT_LEVEL,                             \
5699df373fb0SMaxim Levitsky         .type = QEMU_OPT_STRING,                                    \
5700df373fb0SMaxim Levitsky         .help = "Compatibility level (v2 [0.10] or v3 [1.1])"       \
5701df373fb0SMaxim Levitsky     },                                                              \
5702df373fb0SMaxim Levitsky     {                                                               \
5703df373fb0SMaxim Levitsky         .name = BLOCK_OPT_BACKING_FILE,                             \
5704df373fb0SMaxim Levitsky         .type = QEMU_OPT_STRING,                                    \
5705df373fb0SMaxim Levitsky         .help = "File name of a base image"                         \
5706df373fb0SMaxim Levitsky     },                                                              \
5707df373fb0SMaxim Levitsky     {                                                               \
5708df373fb0SMaxim Levitsky         .name = BLOCK_OPT_BACKING_FMT,                              \
5709df373fb0SMaxim Levitsky         .type = QEMU_OPT_STRING,                                    \
5710df373fb0SMaxim Levitsky         .help = "Image format of the base image"                    \
5711df373fb0SMaxim Levitsky     },                                                              \
5712df373fb0SMaxim Levitsky     {                                                               \
5713df373fb0SMaxim Levitsky         .name = BLOCK_OPT_DATA_FILE,                                \
5714df373fb0SMaxim Levitsky         .type = QEMU_OPT_STRING,                                    \
5715df373fb0SMaxim Levitsky         .help = "File name of an external data file"                \
5716df373fb0SMaxim Levitsky     },                                                              \
5717df373fb0SMaxim Levitsky     {                                                               \
5718df373fb0SMaxim Levitsky         .name = BLOCK_OPT_DATA_FILE_RAW,                            \
5719df373fb0SMaxim Levitsky         .type = QEMU_OPT_BOOL,                                      \
5720df373fb0SMaxim Levitsky         .help = "The external data file must stay valid "           \
5721df373fb0SMaxim Levitsky                 "as a raw image"                                    \
5722df373fb0SMaxim Levitsky     },                                                              \
5723df373fb0SMaxim Levitsky     {                                                               \
57240b6786a9SMaxim Levitsky         .name = BLOCK_OPT_LAZY_REFCOUNTS,                           \
57250b6786a9SMaxim Levitsky         .type = QEMU_OPT_BOOL,                                      \
57260b6786a9SMaxim Levitsky         .help = "Postpone refcount updates",                        \
57270b6786a9SMaxim Levitsky         .def_value_str = "off"                                      \
57280b6786a9SMaxim Levitsky     },                                                              \
57290b6786a9SMaxim Levitsky     {                                                               \
57300b6786a9SMaxim Levitsky         .name = BLOCK_OPT_REFCOUNT_BITS,                            \
57310b6786a9SMaxim Levitsky         .type = QEMU_OPT_NUMBER,                                    \
57320b6786a9SMaxim Levitsky         .help = "Width of a reference count entry in bits",         \
57330b6786a9SMaxim Levitsky         .def_value_str = "16"                                       \
57340b6786a9SMaxim Levitsky     }
57350b6786a9SMaxim Levitsky 
57360b6786a9SMaxim Levitsky static QemuOptsList qcow2_create_opts = {
57370b6786a9SMaxim Levitsky     .name = "qcow2-create-opts",
57380b6786a9SMaxim Levitsky     .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
57390b6786a9SMaxim Levitsky     .desc = {
57400b6786a9SMaxim Levitsky         {                                                               \
5741df373fb0SMaxim Levitsky             .name = BLOCK_OPT_ENCRYPT,                                  \
5742df373fb0SMaxim Levitsky             .type = QEMU_OPT_BOOL,                                      \
5743df373fb0SMaxim Levitsky             .help = "Encrypt the image with format 'aes'. (Deprecated " \
5744df373fb0SMaxim Levitsky                     "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",    \
5745df373fb0SMaxim Levitsky         },                                                              \
5746df373fb0SMaxim Levitsky         {                                                               \
5747df373fb0SMaxim Levitsky             .name = BLOCK_OPT_ENCRYPT_FORMAT,                           \
5748df373fb0SMaxim Levitsky             .type = QEMU_OPT_STRING,                                    \
5749df373fb0SMaxim Levitsky             .help = "Encrypt the image, format choices: 'aes', 'luks'", \
5750df373fb0SMaxim Levitsky         },                                                              \
5751df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",                     \
5752df373fb0SMaxim Levitsky             "ID of secret providing qcow AES key or LUKS passphrase"),  \
5753df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),               \
5754df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),              \
5755df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),                \
5756df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),           \
5757df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),                 \
5758df373fb0SMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),                \
5759df373fb0SMaxim Levitsky         {                                                               \
5760df373fb0SMaxim Levitsky             .name = BLOCK_OPT_CLUSTER_SIZE,                             \
5761df373fb0SMaxim Levitsky             .type = QEMU_OPT_SIZE,                                      \
5762df373fb0SMaxim Levitsky             .help = "qcow2 cluster size",                               \
5763df373fb0SMaxim Levitsky             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)            \
5764df373fb0SMaxim Levitsky         },                                                              \
5765df373fb0SMaxim Levitsky         {                                                               \
5766df373fb0SMaxim Levitsky             .name = BLOCK_OPT_PREALLOC,                                 \
5767df373fb0SMaxim Levitsky             .type = QEMU_OPT_STRING,                                    \
5768df373fb0SMaxim Levitsky             .help = "Preallocation mode (allowed values: off, "         \
5769df373fb0SMaxim Levitsky                     "metadata, falloc, full)"                           \
5770df373fb0SMaxim Levitsky         },                                                              \
5771df373fb0SMaxim Levitsky         {                                                               \
5772df373fb0SMaxim Levitsky             .name = BLOCK_OPT_COMPRESSION_TYPE,                         \
5773df373fb0SMaxim Levitsky             .type = QEMU_OPT_STRING,                                    \
5774df373fb0SMaxim Levitsky             .help = "Compression method used for image cluster "        \
5775df373fb0SMaxim Levitsky                     "compression",                                      \
5776df373fb0SMaxim Levitsky             .def_value_str = "zlib"                                     \
57770b6786a9SMaxim Levitsky         },
5778df373fb0SMaxim Levitsky         QCOW_COMMON_OPTIONS,
5779df373fb0SMaxim Levitsky         { /* end of list */ }
5780df373fb0SMaxim Levitsky     }
5781df373fb0SMaxim Levitsky };
5782df373fb0SMaxim Levitsky 
5783df373fb0SMaxim Levitsky static QemuOptsList qcow2_amend_opts = {
5784df373fb0SMaxim Levitsky     .name = "qcow2-amend-opts",
5785df373fb0SMaxim Levitsky     .head = QTAILQ_HEAD_INITIALIZER(qcow2_amend_opts.head),
5786df373fb0SMaxim Levitsky     .desc = {
578790766d9dSMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_STATE("encrypt."),
578890766d9dSMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT("encrypt."),
578990766d9dSMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET("encrypt."),
579090766d9dSMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET("encrypt."),
579190766d9dSMaxim Levitsky         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
5792df373fb0SMaxim Levitsky         QCOW_COMMON_OPTIONS,
57931bd0e2d1SChunyan Liu         { /* end of list */ }
57941bd0e2d1SChunyan Liu     }
579520d97356SBlue Swirl };
579620d97356SBlue Swirl 
57972654267cSMax Reitz static const char *const qcow2_strong_runtime_opts[] = {
57982654267cSMax Reitz     "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET,
57992654267cSMax Reitz 
58002654267cSMax Reitz     NULL
58012654267cSMax Reitz };
58022654267cSMax Reitz 
58035f535a94SMax Reitz BlockDriver bdrv_qcow2 = {
580420d97356SBlue Swirl     .format_name        = "qcow2",
5805ff99129aSKevin Wolf     .instance_size      = sizeof(BDRVQcow2State),
58067c80ab3fSJes Sorensen     .bdrv_probe         = qcow2_probe,
58077c80ab3fSJes Sorensen     .bdrv_open          = qcow2_open,
58087c80ab3fSJes Sorensen     .bdrv_close         = qcow2_close,
580921d82ac9SJeff Cody     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
58105b0959a7SKevin Wolf     .bdrv_reopen_commit   = qcow2_reopen_commit,
581165eb7c85SPeter Krempa     .bdrv_reopen_commit_post = qcow2_reopen_commit_post,
58125b0959a7SKevin Wolf     .bdrv_reopen_abort    = qcow2_reopen_abort,
58135365f44dSKevin Wolf     .bdrv_join_options    = qcow2_join_options,
581469dca43dSMax Reitz     .bdrv_child_perm      = bdrv_default_perms,
5815efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts  = qcow2_co_create_opts,
5816b0292b85SKevin Wolf     .bdrv_co_create       = qcow2_co_create,
581738841dcdSMax Reitz     .bdrv_has_zero_init   = qcow2_has_zero_init,
5818a320fb04SEric Blake     .bdrv_co_block_status = qcow2_co_block_status,
581920d97356SBlue Swirl 
5820df893d25SVladimir Sementsov-Ogievskiy     .bdrv_co_preadv_part    = qcow2_co_preadv_part,
58215396234bSVladimir Sementsov-Ogievskiy     .bdrv_co_pwritev_part   = qcow2_co_pwritev_part,
5822eb489bb1SKevin Wolf     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
5823419b19d9SStefan Hajnoczi 
58245544b59fSEric Blake     .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
582582e8a788SEric Blake     .bdrv_co_pdiscard       = qcow2_co_pdiscard,
5826fd9fcd37SFam Zheng     .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
5827fd9fcd37SFam Zheng     .bdrv_co_copy_range_to  = qcow2_co_copy_range_to,
5828061ca8a3SKevin Wolf     .bdrv_co_truncate       = qcow2_co_truncate,
58295396234bSVladimir Sementsov-Ogievskiy     .bdrv_co_pwritev_compressed_part = qcow2_co_pwritev_compressed_part,
5830491d27e2SMax Reitz     .bdrv_make_empty        = qcow2_make_empty,
583120d97356SBlue Swirl 
583220d97356SBlue Swirl     .bdrv_snapshot_create   = qcow2_snapshot_create,
583320d97356SBlue Swirl     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
583420d97356SBlue Swirl     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
583520d97356SBlue Swirl     .bdrv_snapshot_list     = qcow2_snapshot_list,
583651ef6727Sedison     .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
5837c501c352SStefan Hajnoczi     .bdrv_measure           = qcow2_measure,
58387c80ab3fSJes Sorensen     .bdrv_get_info          = qcow2_get_info,
583937764dfbSMax Reitz     .bdrv_get_specific_info = qcow2_get_specific_info,
584020d97356SBlue Swirl 
58417c80ab3fSJes Sorensen     .bdrv_save_vmstate    = qcow2_save_vmstate,
58427c80ab3fSJes Sorensen     .bdrv_load_vmstate    = qcow2_load_vmstate,
584320d97356SBlue Swirl 
5844d67066d8SMax Reitz     .is_format                  = true,
58458ee79e70SKevin Wolf     .supports_backing           = true,
584620d97356SBlue Swirl     .bdrv_change_backing_file   = qcow2_change_backing_file,
584720d97356SBlue Swirl 
5848d34682cdSKevin Wolf     .bdrv_refresh_limits        = qcow2_refresh_limits,
58492b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache   = qcow2_co_invalidate_cache,
5850ec6d8912SKevin Wolf     .bdrv_inactivate            = qcow2_inactivate,
585106d9260fSAnthony Liguori 
58521bd0e2d1SChunyan Liu     .create_opts         = &qcow2_create_opts,
5853df373fb0SMaxim Levitsky     .amend_opts          = &qcow2_amend_opts,
58542654267cSMax Reitz     .strong_runtime_opts = qcow2_strong_runtime_opts,
58558a2ce0bcSAlberto Garcia     .mutable_opts        = mutable_opts,
58562fd61638SPaolo Bonzini     .bdrv_co_check       = qcow2_co_check,
5857c282e1fdSChunyan Liu     .bdrv_amend_options  = qcow2_amend_options,
58588ea1613dSMaxim Levitsky     .bdrv_co_amend       = qcow2_co_amend,
5859279621c0SAlberto Garcia 
5860279621c0SAlberto Garcia     .bdrv_detach_aio_context  = qcow2_detach_aio_context,
5861279621c0SAlberto Garcia     .bdrv_attach_aio_context  = qcow2_attach_aio_context,
58621b6b0562SVladimir Sementsov-Ogievskiy 
5863ef893b5cSEric Blake     .bdrv_supports_persistent_dirty_bitmap =
5864ef893b5cSEric Blake             qcow2_supports_persistent_dirty_bitmap,
5865d2c3080eSVladimir Sementsov-Ogievskiy     .bdrv_co_can_store_new_dirty_bitmap = qcow2_co_can_store_new_dirty_bitmap,
5866d2c3080eSVladimir Sementsov-Ogievskiy     .bdrv_co_remove_persistent_dirty_bitmap =
5867d2c3080eSVladimir Sementsov-Ogievskiy             qcow2_co_remove_persistent_dirty_bitmap,
586820d97356SBlue Swirl };
586920d97356SBlue Swirl 
58705efa9d5aSAnthony Liguori static void bdrv_qcow2_init(void)
58715efa9d5aSAnthony Liguori {
58725efa9d5aSAnthony Liguori     bdrv_register(&bdrv_qcow2);
58735efa9d5aSAnthony Liguori }
58745efa9d5aSAnthony Liguori 
58755efa9d5aSAnthony Liguori block_init(bdrv_qcow2_init);
5876