xref: /qemu/block/qcow2.c (revision 354d930dc6e90e97599459b79c071ff1b93e433b)
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"
26737e150eSPaolo Bonzini #include "block/block_int.h"
27609f45eaSMax Reitz #include "block/qdict.h"
2823588797SKevin Wolf #include "sysemu/block-backend.h"
291de7afc9SPaolo Bonzini #include "qemu/module.h"
30585f8587Sbellard #include <zlib.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"
44585f8587Sbellard 
45585f8587Sbellard /*
46585f8587Sbellard   Differences with QCOW:
47585f8587Sbellard 
48585f8587Sbellard   - Support for multiple incremental snapshots.
49585f8587Sbellard   - Memory management by reference counts.
50585f8587Sbellard   - Clusters which have a reference count of one have the bit
51585f8587Sbellard     QCOW_OFLAG_COPIED to optimize write performance.
52585f8587Sbellard   - Size of compressed clusters is stored in sectors to reduce bit usage
53585f8587Sbellard     in the cluster offsets.
54585f8587Sbellard   - Support for storing additional data (such as the VM state) in the
55585f8587Sbellard     snapshots.
56585f8587Sbellard   - If a backing store is used, the cluster size is not constrained
57585f8587Sbellard     (could be backported to QCOW).
58585f8587Sbellard   - L2 tables have always a size of one cluster.
59585f8587Sbellard */
60585f8587Sbellard 
619b80ddf3Saliguori 
629b80ddf3Saliguori typedef struct {
639b80ddf3Saliguori     uint32_t magic;
649b80ddf3Saliguori     uint32_t len;
65c4217f64SJeff Cody } QEMU_PACKED QCowExtension;
6621d82ac9SJeff Cody 
677c80ab3fSJes Sorensen #define  QCOW2_EXT_MAGIC_END 0
687c80ab3fSJes Sorensen #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
69cfcc4c62SKevin Wolf #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
704652b8f3SDaniel P. Berrange #define  QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
7188ddffaeSVladimir Sementsov-Ogievskiy #define  QCOW2_EXT_MAGIC_BITMAPS 0x23852875
729b80ddf3Saliguori 
737c80ab3fSJes Sorensen static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
74585f8587Sbellard {
75585f8587Sbellard     const QCowHeader *cow_header = (const void *)buf;
76585f8587Sbellard 
77585f8587Sbellard     if (buf_size >= sizeof(QCowHeader) &&
78585f8587Sbellard         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
796744cbabSKevin Wolf         be32_to_cpu(cow_header->version) >= 2)
80585f8587Sbellard         return 100;
81585f8587Sbellard     else
82585f8587Sbellard         return 0;
83585f8587Sbellard }
84585f8587Sbellard 
859b80ddf3Saliguori 
864652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
874652b8f3SDaniel P. Berrange                                           uint8_t *buf, size_t buflen,
884652b8f3SDaniel P. Berrange                                           void *opaque, Error **errp)
894652b8f3SDaniel P. Berrange {
904652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
914652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
924652b8f3SDaniel P. Berrange     ssize_t ret;
934652b8f3SDaniel P. Berrange 
944652b8f3SDaniel P. Berrange     if ((offset + buflen) > s->crypto_header.length) {
954652b8f3SDaniel P. Berrange         error_setg(errp, "Request for data outside of extension header");
964652b8f3SDaniel P. Berrange         return -1;
974652b8f3SDaniel P. Berrange     }
984652b8f3SDaniel P. Berrange 
994652b8f3SDaniel P. Berrange     ret = bdrv_pread(bs->file,
1004652b8f3SDaniel P. Berrange                      s->crypto_header.offset + offset, buf, buflen);
1014652b8f3SDaniel P. Berrange     if (ret < 0) {
1024652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not read encryption header");
1034652b8f3SDaniel P. Berrange         return -1;
1044652b8f3SDaniel P. Berrange     }
1054652b8f3SDaniel P. Berrange     return ret;
1064652b8f3SDaniel P. Berrange }
1074652b8f3SDaniel P. Berrange 
1084652b8f3SDaniel P. Berrange 
1094652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
1104652b8f3SDaniel P. Berrange                                           void *opaque, Error **errp)
1114652b8f3SDaniel P. Berrange {
1124652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
1134652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
1144652b8f3SDaniel P. Berrange     int64_t ret;
1154652b8f3SDaniel P. Berrange     int64_t clusterlen;
1164652b8f3SDaniel P. Berrange 
1174652b8f3SDaniel P. Berrange     ret = qcow2_alloc_clusters(bs, headerlen);
1184652b8f3SDaniel P. Berrange     if (ret < 0) {
1194652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret,
1204652b8f3SDaniel P. Berrange                          "Cannot allocate cluster for LUKS header size %zu",
1214652b8f3SDaniel P. Berrange                          headerlen);
1224652b8f3SDaniel P. Berrange         return -1;
1234652b8f3SDaniel P. Berrange     }
1244652b8f3SDaniel P. Berrange 
1254652b8f3SDaniel P. Berrange     s->crypto_header.length = headerlen;
1264652b8f3SDaniel P. Berrange     s->crypto_header.offset = ret;
1274652b8f3SDaniel P. Berrange 
1284652b8f3SDaniel P. Berrange     /* Zero fill remaining space in cluster so it has predictable
1294652b8f3SDaniel P. Berrange      * content in case of future spec changes */
1304652b8f3SDaniel P. Berrange     clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
131c9b83e9cSAlberto Garcia     assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen) == 0);
1324652b8f3SDaniel P. Berrange     ret = bdrv_pwrite_zeroes(bs->file,
1334652b8f3SDaniel P. Berrange                              ret + headerlen,
1344652b8f3SDaniel P. Berrange                              clusterlen - headerlen, 0);
1354652b8f3SDaniel P. Berrange     if (ret < 0) {
1364652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not zero fill encryption header");
1374652b8f3SDaniel P. Berrange         return -1;
1384652b8f3SDaniel P. Berrange     }
1394652b8f3SDaniel P. Berrange 
1404652b8f3SDaniel P. Berrange     return ret;
1414652b8f3SDaniel P. Berrange }
1424652b8f3SDaniel P. Berrange 
1434652b8f3SDaniel P. Berrange 
1444652b8f3SDaniel P. Berrange static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
1454652b8f3SDaniel P. Berrange                                            const uint8_t *buf, size_t buflen,
1464652b8f3SDaniel P. Berrange                                            void *opaque, Error **errp)
1474652b8f3SDaniel P. Berrange {
1484652b8f3SDaniel P. Berrange     BlockDriverState *bs = opaque;
1494652b8f3SDaniel P. Berrange     BDRVQcow2State *s = bs->opaque;
1504652b8f3SDaniel P. Berrange     ssize_t ret;
1514652b8f3SDaniel P. Berrange 
1524652b8f3SDaniel P. Berrange     if ((offset + buflen) > s->crypto_header.length) {
1534652b8f3SDaniel P. Berrange         error_setg(errp, "Request for data outside of extension header");
1544652b8f3SDaniel P. Berrange         return -1;
1554652b8f3SDaniel P. Berrange     }
1564652b8f3SDaniel P. Berrange 
1574652b8f3SDaniel P. Berrange     ret = bdrv_pwrite(bs->file,
1584652b8f3SDaniel P. Berrange                       s->crypto_header.offset + offset, buf, buflen);
1594652b8f3SDaniel P. Berrange     if (ret < 0) {
1604652b8f3SDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not read encryption header");
1614652b8f3SDaniel P. Berrange         return -1;
1624652b8f3SDaniel P. Berrange     }
1634652b8f3SDaniel P. Berrange     return ret;
1644652b8f3SDaniel P. Berrange }
1654652b8f3SDaniel P. Berrange 
1664652b8f3SDaniel P. Berrange 
1679b80ddf3Saliguori /*
1689b80ddf3Saliguori  * read qcow2 extension and fill bs
1699b80ddf3Saliguori  * start reading from start_offset
1709b80ddf3Saliguori  * finish reading upon magic of value 0 or when end_offset reached
1719b80ddf3Saliguori  * unknown magic is skipped (future extension this version knows nothing about)
1729b80ddf3Saliguori  * return 0 upon success, non-0 otherwise
1739b80ddf3Saliguori  */
1747c80ab3fSJes Sorensen static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
1753ef6c40aSMax Reitz                                  uint64_t end_offset, void **p_feature_table,
17688ddffaeSVladimir Sementsov-Ogievskiy                                  int flags, bool *need_update_header,
17788ddffaeSVladimir Sementsov-Ogievskiy                                  Error **errp)
1789b80ddf3Saliguori {
179ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1809b80ddf3Saliguori     QCowExtension ext;
1819b80ddf3Saliguori     uint64_t offset;
18275bab85cSKevin Wolf     int ret;
18388ddffaeSVladimir Sementsov-Ogievskiy     Qcow2BitmapHeaderExt bitmaps_ext;
18488ddffaeSVladimir Sementsov-Ogievskiy 
18588ddffaeSVladimir Sementsov-Ogievskiy     if (need_update_header != NULL) {
18688ddffaeSVladimir Sementsov-Ogievskiy         *need_update_header = false;
18788ddffaeSVladimir Sementsov-Ogievskiy     }
1889b80ddf3Saliguori 
1899b80ddf3Saliguori #ifdef DEBUG_EXT
1907c80ab3fSJes Sorensen     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
1919b80ddf3Saliguori #endif
1929b80ddf3Saliguori     offset = start_offset;
1939b80ddf3Saliguori     while (offset < end_offset) {
1949b80ddf3Saliguori 
1959b80ddf3Saliguori #ifdef DEBUG_EXT
1969b80ddf3Saliguori         /* Sanity check */
1979b80ddf3Saliguori         if (offset > s->cluster_size)
1987c80ab3fSJes Sorensen             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
1999b80ddf3Saliguori 
2009b2260cbSDong Xu Wang         printf("attempting to read extended header in offset %lu\n", offset);
2019b80ddf3Saliguori #endif
2029b80ddf3Saliguori 
203cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
2043ef6c40aSMax Reitz         if (ret < 0) {
2053ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
2063ef6c40aSMax Reitz                              "pread fail from offset %" PRIu64, offset);
2079b80ddf3Saliguori             return 1;
2089b80ddf3Saliguori         }
2099b80ddf3Saliguori         be32_to_cpus(&ext.magic);
2109b80ddf3Saliguori         be32_to_cpus(&ext.len);
2119b80ddf3Saliguori         offset += sizeof(ext);
2129b80ddf3Saliguori #ifdef DEBUG_EXT
2139b80ddf3Saliguori         printf("ext.magic = 0x%x\n", ext.magic);
2149b80ddf3Saliguori #endif
2152ebafc85SKevin Wolf         if (offset > end_offset || ext.len > end_offset - offset) {
2163ef6c40aSMax Reitz             error_setg(errp, "Header extension too large");
21764ca6aeeSKevin Wolf             return -EINVAL;
21864ca6aeeSKevin Wolf         }
21964ca6aeeSKevin Wolf 
2209b80ddf3Saliguori         switch (ext.magic) {
2217c80ab3fSJes Sorensen         case QCOW2_EXT_MAGIC_END:
2229b80ddf3Saliguori             return 0;
223f965509cSaliguori 
2247c80ab3fSJes Sorensen         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
225f965509cSaliguori             if (ext.len >= sizeof(bs->backing_format)) {
226521b2b5dSMax Reitz                 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
227521b2b5dSMax Reitz                            " too large (>=%zu)", ext.len,
228521b2b5dSMax Reitz                            sizeof(bs->backing_format));
229f965509cSaliguori                 return 2;
230f965509cSaliguori             }
231cf2ab8fcSKevin Wolf             ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
2323ef6c40aSMax Reitz             if (ret < 0) {
2333ef6c40aSMax Reitz                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
2343ef6c40aSMax Reitz                                  "Could not read format name");
235f965509cSaliguori                 return 3;
2363ef6c40aSMax Reitz             }
237f965509cSaliguori             bs->backing_format[ext.len] = '\0';
238e4603fe1SKevin Wolf             s->image_backing_format = g_strdup(bs->backing_format);
239f965509cSaliguori #ifdef DEBUG_EXT
240f965509cSaliguori             printf("Qcow2: Got format extension %s\n", bs->backing_format);
241f965509cSaliguori #endif
242f965509cSaliguori             break;
243f965509cSaliguori 
244cfcc4c62SKevin Wolf         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
245cfcc4c62SKevin Wolf             if (p_feature_table != NULL) {
246cfcc4c62SKevin Wolf                 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
247cf2ab8fcSKevin Wolf                 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
248cfcc4c62SKevin Wolf                 if (ret < 0) {
2493ef6c40aSMax Reitz                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
2503ef6c40aSMax Reitz                                      "Could not read table");
251cfcc4c62SKevin Wolf                     return ret;
252cfcc4c62SKevin Wolf                 }
253cfcc4c62SKevin Wolf 
254cfcc4c62SKevin Wolf                 *p_feature_table = feature_table;
255cfcc4c62SKevin Wolf             }
256cfcc4c62SKevin Wolf             break;
257cfcc4c62SKevin Wolf 
2584652b8f3SDaniel P. Berrange         case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
2594652b8f3SDaniel P. Berrange             unsigned int cflags = 0;
2604652b8f3SDaniel P. Berrange             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
2614652b8f3SDaniel P. Berrange                 error_setg(errp, "CRYPTO header extension only "
2624652b8f3SDaniel P. Berrange                            "expected with LUKS encryption method");
2634652b8f3SDaniel P. Berrange                 return -EINVAL;
2644652b8f3SDaniel P. Berrange             }
2654652b8f3SDaniel P. Berrange             if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
2664652b8f3SDaniel P. Berrange                 error_setg(errp, "CRYPTO header extension size %u, "
2674652b8f3SDaniel P. Berrange                            "but expected size %zu", ext.len,
2684652b8f3SDaniel P. Berrange                            sizeof(Qcow2CryptoHeaderExtension));
2694652b8f3SDaniel P. Berrange                 return -EINVAL;
2704652b8f3SDaniel P. Berrange             }
2714652b8f3SDaniel P. Berrange 
2724652b8f3SDaniel P. Berrange             ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
2734652b8f3SDaniel P. Berrange             if (ret < 0) {
2744652b8f3SDaniel P. Berrange                 error_setg_errno(errp, -ret,
2754652b8f3SDaniel P. Berrange                                  "Unable to read CRYPTO header extension");
2764652b8f3SDaniel P. Berrange                 return ret;
2774652b8f3SDaniel P. Berrange             }
2784652b8f3SDaniel P. Berrange             be64_to_cpus(&s->crypto_header.offset);
2794652b8f3SDaniel P. Berrange             be64_to_cpus(&s->crypto_header.length);
2804652b8f3SDaniel P. Berrange 
2814652b8f3SDaniel P. Berrange             if ((s->crypto_header.offset % s->cluster_size) != 0) {
2824652b8f3SDaniel P. Berrange                 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
2834652b8f3SDaniel P. Berrange                            "not a multiple of cluster size '%u'",
2844652b8f3SDaniel P. Berrange                            s->crypto_header.offset, s->cluster_size);
2854652b8f3SDaniel P. Berrange                 return -EINVAL;
2864652b8f3SDaniel P. Berrange             }
2874652b8f3SDaniel P. Berrange 
2884652b8f3SDaniel P. Berrange             if (flags & BDRV_O_NO_IO) {
2894652b8f3SDaniel P. Berrange                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
2904652b8f3SDaniel P. Berrange             }
2911cd9a787SDaniel P. Berrange             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
2924652b8f3SDaniel P. Berrange                                            qcow2_crypto_hdr_read_func,
2934652b8f3SDaniel P. Berrange                                            bs, cflags, errp);
2944652b8f3SDaniel P. Berrange             if (!s->crypto) {
2954652b8f3SDaniel P. Berrange                 return -EINVAL;
2964652b8f3SDaniel P. Berrange             }
2974652b8f3SDaniel P. Berrange         }   break;
2984652b8f3SDaniel P. Berrange 
29988ddffaeSVladimir Sementsov-Ogievskiy         case QCOW2_EXT_MAGIC_BITMAPS:
30088ddffaeSVladimir Sementsov-Ogievskiy             if (ext.len != sizeof(bitmaps_ext)) {
30188ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
30288ddffaeSVladimir Sementsov-Ogievskiy                                  "Invalid extension length");
30388ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
30488ddffaeSVladimir Sementsov-Ogievskiy             }
30588ddffaeSVladimir Sementsov-Ogievskiy 
30688ddffaeSVladimir Sementsov-Ogievskiy             if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
307c9ceb3ecSMax Reitz                 if (s->qcow_version < 3) {
308c9ceb3ecSMax Reitz                     /* Let's be a bit more specific */
309c9ceb3ecSMax Reitz                     warn_report("This qcow2 v2 image contains bitmaps, but "
310c9ceb3ecSMax Reitz                                 "they may have been modified by a program "
311c9ceb3ecSMax Reitz                                 "without persistent bitmap support; so now "
312c9ceb3ecSMax Reitz                                 "they must all be considered inconsistent");
313c9ceb3ecSMax Reitz                 } else {
31455d527a9SAlistair Francis                     warn_report("a program lacking bitmap support "
31588ddffaeSVladimir Sementsov-Ogievskiy                                 "modified this file, so all bitmaps are now "
31655d527a9SAlistair Francis                                 "considered inconsistent");
317c9ceb3ecSMax Reitz                 }
31855d527a9SAlistair Francis                 error_printf("Some clusters may be leaked, "
31955d527a9SAlistair Francis                              "run 'qemu-img check -r' on the image "
32088ddffaeSVladimir Sementsov-Ogievskiy                              "file to fix.");
32188ddffaeSVladimir Sementsov-Ogievskiy                 if (need_update_header != NULL) {
32288ddffaeSVladimir Sementsov-Ogievskiy                     /* Updating is needed to drop invalid bitmap extension. */
32388ddffaeSVladimir Sementsov-Ogievskiy                     *need_update_header = true;
32488ddffaeSVladimir Sementsov-Ogievskiy                 }
32588ddffaeSVladimir Sementsov-Ogievskiy                 break;
32688ddffaeSVladimir Sementsov-Ogievskiy             }
32788ddffaeSVladimir Sementsov-Ogievskiy 
32888ddffaeSVladimir Sementsov-Ogievskiy             ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
32988ddffaeSVladimir Sementsov-Ogievskiy             if (ret < 0) {
33088ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
33188ddffaeSVladimir Sementsov-Ogievskiy                                  "Could not read ext header");
33288ddffaeSVladimir Sementsov-Ogievskiy                 return ret;
33388ddffaeSVladimir Sementsov-Ogievskiy             }
33488ddffaeSVladimir Sementsov-Ogievskiy 
33588ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.reserved32 != 0) {
33688ddffaeSVladimir Sementsov-Ogievskiy                 error_setg_errno(errp, -ret, "bitmaps_ext: "
33788ddffaeSVladimir Sementsov-Ogievskiy                                  "Reserved field is not zero");
33888ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
33988ddffaeSVladimir Sementsov-Ogievskiy             }
34088ddffaeSVladimir Sementsov-Ogievskiy 
34188ddffaeSVladimir Sementsov-Ogievskiy             be32_to_cpus(&bitmaps_ext.nb_bitmaps);
34288ddffaeSVladimir Sementsov-Ogievskiy             be64_to_cpus(&bitmaps_ext.bitmap_directory_size);
34388ddffaeSVladimir Sementsov-Ogievskiy             be64_to_cpus(&bitmaps_ext.bitmap_directory_offset);
34488ddffaeSVladimir Sementsov-Ogievskiy 
34588ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
34688ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp,
34788ddffaeSVladimir Sementsov-Ogievskiy                            "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
34888ddffaeSVladimir Sementsov-Ogievskiy                            "exceeding the QEMU supported maximum of %d",
34988ddffaeSVladimir Sementsov-Ogievskiy                            bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
35088ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
35188ddffaeSVladimir Sementsov-Ogievskiy             }
35288ddffaeSVladimir Sementsov-Ogievskiy 
35388ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.nb_bitmaps == 0) {
35488ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "found bitmaps extension with zero bitmaps");
35588ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
35688ddffaeSVladimir Sementsov-Ogievskiy             }
35788ddffaeSVladimir Sementsov-Ogievskiy 
35888ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
35988ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "bitmaps_ext: "
36088ddffaeSVladimir Sementsov-Ogievskiy                                  "invalid bitmap directory offset");
36188ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
36288ddffaeSVladimir Sementsov-Ogievskiy             }
36388ddffaeSVladimir Sementsov-Ogievskiy 
36488ddffaeSVladimir Sementsov-Ogievskiy             if (bitmaps_ext.bitmap_directory_size >
36588ddffaeSVladimir Sementsov-Ogievskiy                 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
36688ddffaeSVladimir Sementsov-Ogievskiy                 error_setg(errp, "bitmaps_ext: "
36788ddffaeSVladimir Sementsov-Ogievskiy                                  "bitmap directory size (%" PRIu64 ") exceeds "
36888ddffaeSVladimir Sementsov-Ogievskiy                                  "the maximum supported size (%d)",
36988ddffaeSVladimir Sementsov-Ogievskiy                                  bitmaps_ext.bitmap_directory_size,
37088ddffaeSVladimir Sementsov-Ogievskiy                                  QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
37188ddffaeSVladimir Sementsov-Ogievskiy                 return -EINVAL;
37288ddffaeSVladimir Sementsov-Ogievskiy             }
37388ddffaeSVladimir Sementsov-Ogievskiy 
37488ddffaeSVladimir Sementsov-Ogievskiy             s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
37588ddffaeSVladimir Sementsov-Ogievskiy             s->bitmap_directory_offset =
37688ddffaeSVladimir Sementsov-Ogievskiy                     bitmaps_ext.bitmap_directory_offset;
37788ddffaeSVladimir Sementsov-Ogievskiy             s->bitmap_directory_size =
37888ddffaeSVladimir Sementsov-Ogievskiy                     bitmaps_ext.bitmap_directory_size;
37988ddffaeSVladimir Sementsov-Ogievskiy 
38088ddffaeSVladimir Sementsov-Ogievskiy #ifdef DEBUG_EXT
38188ddffaeSVladimir Sementsov-Ogievskiy             printf("Qcow2: Got bitmaps extension: "
38288ddffaeSVladimir Sementsov-Ogievskiy                    "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
38388ddffaeSVladimir Sementsov-Ogievskiy                    s->bitmap_directory_offset, s->nb_bitmaps);
38488ddffaeSVladimir Sementsov-Ogievskiy #endif
38588ddffaeSVladimir Sementsov-Ogievskiy             break;
38688ddffaeSVladimir Sementsov-Ogievskiy 
3879b80ddf3Saliguori         default:
38875bab85cSKevin Wolf             /* unknown magic - save it in case we need to rewrite the header */
3894096974eSEric Blake             /* If you add a new feature, make sure to also update the fast
3904096974eSEric Blake              * path of qcow2_make_empty() to deal with it. */
39175bab85cSKevin Wolf             {
39275bab85cSKevin Wolf                 Qcow2UnknownHeaderExtension *uext;
39375bab85cSKevin Wolf 
39475bab85cSKevin Wolf                 uext = g_malloc0(sizeof(*uext)  + ext.len);
39575bab85cSKevin Wolf                 uext->magic = ext.magic;
39675bab85cSKevin Wolf                 uext->len = ext.len;
39775bab85cSKevin Wolf                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
39875bab85cSKevin Wolf 
399cf2ab8fcSKevin Wolf                 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
40075bab85cSKevin Wolf                 if (ret < 0) {
4013ef6c40aSMax Reitz                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
4023ef6c40aSMax Reitz                                      "Could not read data");
40375bab85cSKevin Wolf                     return ret;
40475bab85cSKevin Wolf                 }
40575bab85cSKevin Wolf             }
4069b80ddf3Saliguori             break;
4079b80ddf3Saliguori         }
408fd29b4bbSKevin Wolf 
409fd29b4bbSKevin Wolf         offset += ((ext.len + 7) & ~7);
4109b80ddf3Saliguori     }
4119b80ddf3Saliguori 
4129b80ddf3Saliguori     return 0;
4139b80ddf3Saliguori }
4149b80ddf3Saliguori 
41575bab85cSKevin Wolf static void cleanup_unknown_header_ext(BlockDriverState *bs)
41675bab85cSKevin Wolf {
417ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
41875bab85cSKevin Wolf     Qcow2UnknownHeaderExtension *uext, *next;
41975bab85cSKevin Wolf 
42075bab85cSKevin Wolf     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
42175bab85cSKevin Wolf         QLIST_REMOVE(uext, next);
42275bab85cSKevin Wolf         g_free(uext);
42375bab85cSKevin Wolf     }
42475bab85cSKevin Wolf }
4259b80ddf3Saliguori 
426a55448b3SMax Reitz static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
427a55448b3SMax Reitz                                        uint64_t mask)
428cfcc4c62SKevin Wolf {
42912ac6d3dSKevin Wolf     char *features = g_strdup("");
43012ac6d3dSKevin Wolf     char *old;
43112ac6d3dSKevin Wolf 
432cfcc4c62SKevin Wolf     while (table && table->name[0] != '\0') {
433cfcc4c62SKevin Wolf         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
43412ac6d3dSKevin Wolf             if (mask & (1ULL << table->bit)) {
43512ac6d3dSKevin Wolf                 old = features;
43612ac6d3dSKevin Wolf                 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
43712ac6d3dSKevin Wolf                                            table->name);
43812ac6d3dSKevin Wolf                 g_free(old);
43912ac6d3dSKevin Wolf                 mask &= ~(1ULL << table->bit);
440cfcc4c62SKevin Wolf             }
441cfcc4c62SKevin Wolf         }
442cfcc4c62SKevin Wolf         table++;
443cfcc4c62SKevin Wolf     }
444cfcc4c62SKevin Wolf 
445cfcc4c62SKevin Wolf     if (mask) {
44612ac6d3dSKevin Wolf         old = features;
44712ac6d3dSKevin Wolf         features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
44812ac6d3dSKevin Wolf                                    old, *old ? ", " : "", mask);
44912ac6d3dSKevin Wolf         g_free(old);
450cfcc4c62SKevin Wolf     }
45112ac6d3dSKevin Wolf 
452a55448b3SMax Reitz     error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
45312ac6d3dSKevin Wolf     g_free(features);
454cfcc4c62SKevin Wolf }
455cfcc4c62SKevin Wolf 
456c61d0004SStefan Hajnoczi /*
457bfe8043eSStefan Hajnoczi  * Sets the dirty bit and flushes afterwards if necessary.
458bfe8043eSStefan Hajnoczi  *
459bfe8043eSStefan Hajnoczi  * The incompatible_features bit is only set if the image file header was
460bfe8043eSStefan Hajnoczi  * updated successfully.  Therefore it is not required to check the return
461bfe8043eSStefan Hajnoczi  * value of this function.
462bfe8043eSStefan Hajnoczi  */
463280d3735SKevin Wolf int qcow2_mark_dirty(BlockDriverState *bs)
464bfe8043eSStefan Hajnoczi {
465ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
466bfe8043eSStefan Hajnoczi     uint64_t val;
467bfe8043eSStefan Hajnoczi     int ret;
468bfe8043eSStefan Hajnoczi 
469bfe8043eSStefan Hajnoczi     assert(s->qcow_version >= 3);
470bfe8043eSStefan Hajnoczi 
471bfe8043eSStefan Hajnoczi     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
472bfe8043eSStefan Hajnoczi         return 0; /* already dirty */
473bfe8043eSStefan Hajnoczi     }
474bfe8043eSStefan Hajnoczi 
475bfe8043eSStefan Hajnoczi     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
476d9ca2ea2SKevin Wolf     ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
477bfe8043eSStefan Hajnoczi                       &val, sizeof(val));
478bfe8043eSStefan Hajnoczi     if (ret < 0) {
479bfe8043eSStefan Hajnoczi         return ret;
480bfe8043eSStefan Hajnoczi     }
4819a4f4c31SKevin Wolf     ret = bdrv_flush(bs->file->bs);
482bfe8043eSStefan Hajnoczi     if (ret < 0) {
483bfe8043eSStefan Hajnoczi         return ret;
484bfe8043eSStefan Hajnoczi     }
485bfe8043eSStefan Hajnoczi 
486bfe8043eSStefan Hajnoczi     /* Only treat image as dirty if the header was updated successfully */
487bfe8043eSStefan Hajnoczi     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
488bfe8043eSStefan Hajnoczi     return 0;
489bfe8043eSStefan Hajnoczi }
490bfe8043eSStefan Hajnoczi 
491bfe8043eSStefan Hajnoczi /*
492c61d0004SStefan Hajnoczi  * Clears the dirty bit and flushes before if necessary.  Only call this
493c61d0004SStefan Hajnoczi  * function when there are no pending requests, it does not guard against
494c61d0004SStefan Hajnoczi  * concurrent requests dirtying the image.
495c61d0004SStefan Hajnoczi  */
496c61d0004SStefan Hajnoczi static int qcow2_mark_clean(BlockDriverState *bs)
497c61d0004SStefan Hajnoczi {
498ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
499c61d0004SStefan Hajnoczi 
500c61d0004SStefan Hajnoczi     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
5014c2e5f8fSKevin Wolf         int ret;
5024c2e5f8fSKevin Wolf 
5034c2e5f8fSKevin Wolf         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
5044c2e5f8fSKevin Wolf 
5058b220eb7SPaolo Bonzini         ret = qcow2_flush_caches(bs);
506c61d0004SStefan Hajnoczi         if (ret < 0) {
507c61d0004SStefan Hajnoczi             return ret;
508c61d0004SStefan Hajnoczi         }
509c61d0004SStefan Hajnoczi 
510c61d0004SStefan Hajnoczi         return qcow2_update_header(bs);
511c61d0004SStefan Hajnoczi     }
512c61d0004SStefan Hajnoczi     return 0;
513c61d0004SStefan Hajnoczi }
514c61d0004SStefan Hajnoczi 
51569c98726SMax Reitz /*
51669c98726SMax Reitz  * Marks the image as corrupt.
51769c98726SMax Reitz  */
51869c98726SMax Reitz int qcow2_mark_corrupt(BlockDriverState *bs)
51969c98726SMax Reitz {
520ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
52169c98726SMax Reitz 
52269c98726SMax Reitz     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
52369c98726SMax Reitz     return qcow2_update_header(bs);
52469c98726SMax Reitz }
52569c98726SMax Reitz 
52669c98726SMax Reitz /*
52769c98726SMax Reitz  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
52869c98726SMax Reitz  * before if necessary.
52969c98726SMax Reitz  */
53069c98726SMax Reitz int qcow2_mark_consistent(BlockDriverState *bs)
53169c98726SMax Reitz {
532ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
53369c98726SMax Reitz 
53469c98726SMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
5358b220eb7SPaolo Bonzini         int ret = qcow2_flush_caches(bs);
53669c98726SMax Reitz         if (ret < 0) {
53769c98726SMax Reitz             return ret;
53869c98726SMax Reitz         }
53969c98726SMax Reitz 
54069c98726SMax Reitz         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
54169c98726SMax Reitz         return qcow2_update_header(bs);
54269c98726SMax Reitz     }
54369c98726SMax Reitz     return 0;
54469c98726SMax Reitz }
54569c98726SMax Reitz 
5462fd61638SPaolo Bonzini static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs,
5472fd61638SPaolo Bonzini                                               BdrvCheckResult *result,
548acbe5982SStefan Hajnoczi                                               BdrvCheckMode fix)
549acbe5982SStefan Hajnoczi {
550acbe5982SStefan Hajnoczi     int ret = qcow2_check_refcounts(bs, result, fix);
551acbe5982SStefan Hajnoczi     if (ret < 0) {
552acbe5982SStefan Hajnoczi         return ret;
553acbe5982SStefan Hajnoczi     }
554acbe5982SStefan Hajnoczi 
555acbe5982SStefan Hajnoczi     if (fix && result->check_errors == 0 && result->corruptions == 0) {
55624530f3eSMax Reitz         ret = qcow2_mark_clean(bs);
55724530f3eSMax Reitz         if (ret < 0) {
55824530f3eSMax Reitz             return ret;
55924530f3eSMax Reitz         }
56024530f3eSMax Reitz         return qcow2_mark_consistent(bs);
561acbe5982SStefan Hajnoczi     }
562acbe5982SStefan Hajnoczi     return ret;
563acbe5982SStefan Hajnoczi }
564acbe5982SStefan Hajnoczi 
5652fd61638SPaolo Bonzini static int coroutine_fn qcow2_co_check(BlockDriverState *bs,
5662fd61638SPaolo Bonzini                                        BdrvCheckResult *result,
5672fd61638SPaolo Bonzini                                        BdrvCheckMode fix)
5682fd61638SPaolo Bonzini {
5692fd61638SPaolo Bonzini     BDRVQcow2State *s = bs->opaque;
5702fd61638SPaolo Bonzini     int ret;
5712fd61638SPaolo Bonzini 
5722fd61638SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
5732fd61638SPaolo Bonzini     ret = qcow2_co_check_locked(bs, result, fix);
5742fd61638SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
5752fd61638SPaolo Bonzini     return ret;
5762fd61638SPaolo Bonzini }
5772fd61638SPaolo Bonzini 
5780cf0e598SAlberto Garcia int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
5790cf0e598SAlberto Garcia                          uint64_t entries, size_t entry_len,
5800cf0e598SAlberto Garcia                          int64_t max_size_bytes, const char *table_name,
5810cf0e598SAlberto Garcia                          Error **errp)
5828c7de283SKevin Wolf {
583ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
5840cf0e598SAlberto Garcia 
5850cf0e598SAlberto Garcia     if (entries > max_size_bytes / entry_len) {
5860cf0e598SAlberto Garcia         error_setg(errp, "%s too large", table_name);
5870cf0e598SAlberto Garcia         return -EFBIG;
5880cf0e598SAlberto Garcia     }
5898c7de283SKevin Wolf 
5908c7de283SKevin Wolf     /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
5918c7de283SKevin Wolf      * because values will be passed to qemu functions taking int64_t. */
5920cf0e598SAlberto Garcia     if ((INT64_MAX - entries * entry_len < offset) ||
5930cf0e598SAlberto Garcia         (offset_into_cluster(s, offset) != 0)) {
5940cf0e598SAlberto Garcia         error_setg(errp, "%s offset invalid", table_name);
5958c7de283SKevin Wolf         return -EINVAL;
5968c7de283SKevin Wolf     }
5978c7de283SKevin Wolf 
5988c7de283SKevin Wolf     return 0;
5998c7de283SKevin Wolf }
6008c7de283SKevin Wolf 
60174c4510aSKevin Wolf static QemuOptsList qcow2_runtime_opts = {
60274c4510aSKevin Wolf     .name = "qcow2",
60374c4510aSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
60474c4510aSKevin Wolf     .desc = {
60574c4510aSKevin Wolf         {
60664aa99d3SKevin Wolf             .name = QCOW2_OPT_LAZY_REFCOUNTS,
60774c4510aSKevin Wolf             .type = QEMU_OPT_BOOL,
60874c4510aSKevin Wolf             .help = "Postpone refcount updates",
60974c4510aSKevin Wolf         },
61067af674eSKevin Wolf         {
61167af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_REQUEST,
61267af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
61367af674eSKevin Wolf             .help = "Pass guest discard requests to the layer below",
61467af674eSKevin Wolf         },
61567af674eSKevin Wolf         {
61667af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
61767af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
61867af674eSKevin Wolf             .help = "Generate discard requests when snapshot related space "
61967af674eSKevin Wolf                     "is freed",
62067af674eSKevin Wolf         },
62167af674eSKevin Wolf         {
62267af674eSKevin Wolf             .name = QCOW2_OPT_DISCARD_OTHER,
62367af674eSKevin Wolf             .type = QEMU_OPT_BOOL,
62467af674eSKevin Wolf             .help = "Generate discard requests when other clusters are freed",
62567af674eSKevin Wolf         },
62605de7e86SMax Reitz         {
62705de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP,
62805de7e86SMax Reitz             .type = QEMU_OPT_STRING,
62905de7e86SMax Reitz             .help = "Selects which overlap checks to perform from a range of "
63005de7e86SMax Reitz                     "templates (none, constant, cached, all)",
63105de7e86SMax Reitz         },
63205de7e86SMax Reitz         {
633ee42b5ceSMax Reitz             .name = QCOW2_OPT_OVERLAP_TEMPLATE,
634ee42b5ceSMax Reitz             .type = QEMU_OPT_STRING,
635ee42b5ceSMax Reitz             .help = "Selects which overlap checks to perform from a range of "
636ee42b5ceSMax Reitz                     "templates (none, constant, cached, all)",
637ee42b5ceSMax Reitz         },
638ee42b5ceSMax Reitz         {
63905de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
64005de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
64105de7e86SMax Reitz             .help = "Check for unintended writes into the main qcow2 header",
64205de7e86SMax Reitz         },
64305de7e86SMax Reitz         {
64405de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
64505de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
64605de7e86SMax Reitz             .help = "Check for unintended writes into the active L1 table",
64705de7e86SMax Reitz         },
64805de7e86SMax Reitz         {
64905de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
65005de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
65105de7e86SMax Reitz             .help = "Check for unintended writes into an active L2 table",
65205de7e86SMax Reitz         },
65305de7e86SMax Reitz         {
65405de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
65505de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
65605de7e86SMax Reitz             .help = "Check for unintended writes into the refcount table",
65705de7e86SMax Reitz         },
65805de7e86SMax Reitz         {
65905de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
66005de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
66105de7e86SMax Reitz             .help = "Check for unintended writes into a refcount block",
66205de7e86SMax Reitz         },
66305de7e86SMax Reitz         {
66405de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
66505de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
66605de7e86SMax Reitz             .help = "Check for unintended writes into the snapshot table",
66705de7e86SMax Reitz         },
66805de7e86SMax Reitz         {
66905de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
67005de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
67105de7e86SMax Reitz             .help = "Check for unintended writes into an inactive L1 table",
67205de7e86SMax Reitz         },
67305de7e86SMax Reitz         {
67405de7e86SMax Reitz             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
67505de7e86SMax Reitz             .type = QEMU_OPT_BOOL,
67605de7e86SMax Reitz             .help = "Check for unintended writes into an inactive L2 table",
67705de7e86SMax Reitz         },
6786c1c8d5dSMax Reitz         {
6796c1c8d5dSMax Reitz             .name = QCOW2_OPT_CACHE_SIZE,
6806c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
6816c1c8d5dSMax Reitz             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
6826c1c8d5dSMax Reitz                     "cache size",
6836c1c8d5dSMax Reitz         },
6846c1c8d5dSMax Reitz         {
6856c1c8d5dSMax Reitz             .name = QCOW2_OPT_L2_CACHE_SIZE,
6866c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
6876c1c8d5dSMax Reitz             .help = "Maximum L2 table cache size",
6886c1c8d5dSMax Reitz         },
6896c1c8d5dSMax Reitz         {
6901221fe6fSAlberto Garcia             .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
6911221fe6fSAlberto Garcia             .type = QEMU_OPT_SIZE,
6921221fe6fSAlberto Garcia             .help = "Size of each entry in the L2 cache",
6931221fe6fSAlberto Garcia         },
6941221fe6fSAlberto Garcia         {
6956c1c8d5dSMax Reitz             .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
6966c1c8d5dSMax Reitz             .type = QEMU_OPT_SIZE,
6976c1c8d5dSMax Reitz             .help = "Maximum refcount block cache size",
6986c1c8d5dSMax Reitz         },
699279621c0SAlberto Garcia         {
700279621c0SAlberto Garcia             .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
701279621c0SAlberto Garcia             .type = QEMU_OPT_NUMBER,
702279621c0SAlberto Garcia             .help = "Clean unused cache entries after this time (in seconds)",
703279621c0SAlberto Garcia         },
7044652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
7054652b8f3SDaniel P. Berrange             "ID of secret providing qcow2 AES key or LUKS passphrase"),
70674c4510aSKevin Wolf         { /* end of list */ }
70774c4510aSKevin Wolf     },
70874c4510aSKevin Wolf };
70974c4510aSKevin Wolf 
7104092e99dSMax Reitz static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
7114092e99dSMax Reitz     [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
7124092e99dSMax Reitz     [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
7134092e99dSMax Reitz     [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
7144092e99dSMax Reitz     [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
7154092e99dSMax Reitz     [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
7164092e99dSMax Reitz     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
7174092e99dSMax Reitz     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
7184092e99dSMax Reitz     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
7194092e99dSMax Reitz };
7204092e99dSMax Reitz 
721279621c0SAlberto Garcia static void cache_clean_timer_cb(void *opaque)
722279621c0SAlberto Garcia {
723279621c0SAlberto Garcia     BlockDriverState *bs = opaque;
724ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
725b2f68bffSAlberto Garcia     qcow2_cache_clean_unused(s->l2_table_cache);
726b2f68bffSAlberto Garcia     qcow2_cache_clean_unused(s->refcount_block_cache);
727279621c0SAlberto Garcia     timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
728279621c0SAlberto Garcia               (int64_t) s->cache_clean_interval * 1000);
729279621c0SAlberto Garcia }
730279621c0SAlberto Garcia 
731279621c0SAlberto Garcia static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
732279621c0SAlberto Garcia {
733ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
734279621c0SAlberto Garcia     if (s->cache_clean_interval > 0) {
735279621c0SAlberto Garcia         s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
736279621c0SAlberto Garcia                                              SCALE_MS, cache_clean_timer_cb,
737279621c0SAlberto Garcia                                              bs);
738279621c0SAlberto Garcia         timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
739279621c0SAlberto Garcia                   (int64_t) s->cache_clean_interval * 1000);
740279621c0SAlberto Garcia     }
741279621c0SAlberto Garcia }
742279621c0SAlberto Garcia 
743279621c0SAlberto Garcia static void cache_clean_timer_del(BlockDriverState *bs)
744279621c0SAlberto Garcia {
745ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
746279621c0SAlberto Garcia     if (s->cache_clean_timer) {
747279621c0SAlberto Garcia         timer_del(s->cache_clean_timer);
748279621c0SAlberto Garcia         timer_free(s->cache_clean_timer);
749279621c0SAlberto Garcia         s->cache_clean_timer = NULL;
750279621c0SAlberto Garcia     }
751279621c0SAlberto Garcia }
752279621c0SAlberto Garcia 
753279621c0SAlberto Garcia static void qcow2_detach_aio_context(BlockDriverState *bs)
754279621c0SAlberto Garcia {
755279621c0SAlberto Garcia     cache_clean_timer_del(bs);
756279621c0SAlberto Garcia }
757279621c0SAlberto Garcia 
758279621c0SAlberto Garcia static void qcow2_attach_aio_context(BlockDriverState *bs,
759279621c0SAlberto Garcia                                      AioContext *new_context)
760279621c0SAlberto Garcia {
761279621c0SAlberto Garcia     cache_clean_timer_init(bs, new_context);
762279621c0SAlberto Garcia }
763279621c0SAlberto Garcia 
764bc85ef26SMax Reitz static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
765bc85ef26SMax Reitz                              uint64_t *l2_cache_size,
7661221fe6fSAlberto Garcia                              uint64_t *l2_cache_entry_size,
7676c1c8d5dSMax Reitz                              uint64_t *refcount_cache_size, Error **errp)
7686c1c8d5dSMax Reitz {
769ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
7706c1c8d5dSMax Reitz     uint64_t combined_cache_size;
7716c1c8d5dSMax Reitz     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
7727af5eea9SAlberto Garcia     int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
7736c1c8d5dSMax Reitz 
7746c1c8d5dSMax Reitz     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
7756c1c8d5dSMax Reitz     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
7766c1c8d5dSMax Reitz     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
7776c1c8d5dSMax Reitz 
7786c1c8d5dSMax Reitz     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
7796c1c8d5dSMax Reitz     *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
7806c1c8d5dSMax Reitz     *refcount_cache_size = qemu_opt_get_size(opts,
7816c1c8d5dSMax Reitz                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
7826c1c8d5dSMax Reitz 
7831221fe6fSAlberto Garcia     *l2_cache_entry_size = qemu_opt_get_size(
7841221fe6fSAlberto Garcia         opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
7851221fe6fSAlberto Garcia 
7866c1c8d5dSMax Reitz     if (combined_cache_size_set) {
7876c1c8d5dSMax Reitz         if (l2_cache_size_set && refcount_cache_size_set) {
7886c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
7896c1c8d5dSMax Reitz                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
7906c1c8d5dSMax Reitz                        "the same time");
7916c1c8d5dSMax Reitz             return;
7926c1c8d5dSMax Reitz         } else if (*l2_cache_size > combined_cache_size) {
7936c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
7946c1c8d5dSMax Reitz                        QCOW2_OPT_CACHE_SIZE);
7956c1c8d5dSMax Reitz             return;
7966c1c8d5dSMax Reitz         } else if (*refcount_cache_size > combined_cache_size) {
7976c1c8d5dSMax Reitz             error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
7986c1c8d5dSMax Reitz                        QCOW2_OPT_CACHE_SIZE);
7996c1c8d5dSMax Reitz             return;
8006c1c8d5dSMax Reitz         }
8016c1c8d5dSMax Reitz 
8026c1c8d5dSMax Reitz         if (l2_cache_size_set) {
8036c1c8d5dSMax Reitz             *refcount_cache_size = combined_cache_size - *l2_cache_size;
8046c1c8d5dSMax Reitz         } else if (refcount_cache_size_set) {
8056c1c8d5dSMax Reitz             *l2_cache_size = combined_cache_size - *refcount_cache_size;
8066c1c8d5dSMax Reitz         } else {
80752253998SAlberto Garcia             uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
80852253998SAlberto Garcia             uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
80952253998SAlberto Garcia 
81052253998SAlberto Garcia             /* Assign as much memory as possible to the L2 cache, and
81152253998SAlberto Garcia              * use the remainder for the refcount cache */
81252253998SAlberto Garcia             if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
81352253998SAlberto Garcia                 *l2_cache_size = max_l2_cache;
81452253998SAlberto Garcia                 *refcount_cache_size = combined_cache_size - *l2_cache_size;
81552253998SAlberto Garcia             } else {
81652253998SAlberto Garcia                 *refcount_cache_size =
81752253998SAlberto Garcia                     MIN(combined_cache_size, min_refcount_cache);
8186c1c8d5dSMax Reitz                 *l2_cache_size = combined_cache_size - *refcount_cache_size;
8196c1c8d5dSMax Reitz             }
82052253998SAlberto Garcia         }
8216c1c8d5dSMax Reitz     } else {
82252253998SAlberto Garcia         if (!l2_cache_size_set) {
823bc85ef26SMax Reitz             *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
824bc85ef26SMax Reitz                                  (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
825bc85ef26SMax Reitz                                  * s->cluster_size);
82652253998SAlberto Garcia         }
82752253998SAlberto Garcia         if (!refcount_cache_size_set) {
8287af5eea9SAlberto Garcia             *refcount_cache_size = min_refcount_cache;
8296c1c8d5dSMax Reitz         }
8306c1c8d5dSMax Reitz     }
8311221fe6fSAlberto Garcia 
8321221fe6fSAlberto Garcia     if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
8331221fe6fSAlberto Garcia         *l2_cache_entry_size > s->cluster_size ||
8341221fe6fSAlberto Garcia         !is_power_of_2(*l2_cache_entry_size)) {
8351221fe6fSAlberto Garcia         error_setg(errp, "L2 cache entry size must be a power of two "
8361221fe6fSAlberto Garcia                    "between %d and the cluster size (%d)",
8371221fe6fSAlberto Garcia                    1 << MIN_CLUSTER_BITS, s->cluster_size);
8381221fe6fSAlberto Garcia         return;
8391221fe6fSAlberto Garcia     }
8406c1c8d5dSMax Reitz }
8416c1c8d5dSMax Reitz 
842ee55b173SKevin Wolf typedef struct Qcow2ReopenState {
843ee55b173SKevin Wolf     Qcow2Cache *l2_table_cache;
844ee55b173SKevin Wolf     Qcow2Cache *refcount_block_cache;
8453c2e511aSAlberto Garcia     int l2_slice_size; /* Number of entries in a slice of the L2 table */
846ee55b173SKevin Wolf     bool use_lazy_refcounts;
847ee55b173SKevin Wolf     int overlap_check;
848ee55b173SKevin Wolf     bool discard_passthrough[QCOW2_DISCARD_MAX];
849ee55b173SKevin Wolf     uint64_t cache_clean_interval;
850b25b387fSDaniel P. Berrange     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
851ee55b173SKevin Wolf } Qcow2ReopenState;
852ee55b173SKevin Wolf 
853ee55b173SKevin Wolf static int qcow2_update_options_prepare(BlockDriverState *bs,
854ee55b173SKevin Wolf                                         Qcow2ReopenState *r,
855ee55b173SKevin Wolf                                         QDict *options, int flags,
856ee55b173SKevin Wolf                                         Error **errp)
8574c75d1a1SKevin Wolf {
8584c75d1a1SKevin Wolf     BDRVQcow2State *s = bs->opaque;
85994edf3fbSKevin Wolf     QemuOpts *opts = NULL;
8604c75d1a1SKevin Wolf     const char *opt_overlap_check, *opt_overlap_check_template;
8614c75d1a1SKevin Wolf     int overlap_check_template = 0;
8621221fe6fSAlberto Garcia     uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
8634c75d1a1SKevin Wolf     int i;
864b25b387fSDaniel P. Berrange     const char *encryptfmt;
865b25b387fSDaniel P. Berrange     QDict *encryptopts = NULL;
86694edf3fbSKevin Wolf     Error *local_err = NULL;
8674c75d1a1SKevin Wolf     int ret;
8684c75d1a1SKevin Wolf 
869b25b387fSDaniel P. Berrange     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
870b25b387fSDaniel P. Berrange     encryptfmt = qdict_get_try_str(encryptopts, "format");
871b25b387fSDaniel P. Berrange 
87294edf3fbSKevin Wolf     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
87394edf3fbSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
87494edf3fbSKevin Wolf     if (local_err) {
87594edf3fbSKevin Wolf         error_propagate(errp, local_err);
87694edf3fbSKevin Wolf         ret = -EINVAL;
87794edf3fbSKevin Wolf         goto fail;
87894edf3fbSKevin Wolf     }
87994edf3fbSKevin Wolf 
88094edf3fbSKevin Wolf     /* get L2 table/refcount block cache size from command line options */
8811221fe6fSAlberto Garcia     read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
8821221fe6fSAlberto Garcia                      &refcount_cache_size, &local_err);
88394edf3fbSKevin Wolf     if (local_err) {
88494edf3fbSKevin Wolf         error_propagate(errp, local_err);
88594edf3fbSKevin Wolf         ret = -EINVAL;
88694edf3fbSKevin Wolf         goto fail;
88794edf3fbSKevin Wolf     }
88894edf3fbSKevin Wolf 
8891221fe6fSAlberto Garcia     l2_cache_size /= l2_cache_entry_size;
89094edf3fbSKevin Wolf     if (l2_cache_size < MIN_L2_CACHE_SIZE) {
89194edf3fbSKevin Wolf         l2_cache_size = MIN_L2_CACHE_SIZE;
89294edf3fbSKevin Wolf     }
89394edf3fbSKevin Wolf     if (l2_cache_size > INT_MAX) {
89494edf3fbSKevin Wolf         error_setg(errp, "L2 cache size too big");
89594edf3fbSKevin Wolf         ret = -EINVAL;
89694edf3fbSKevin Wolf         goto fail;
89794edf3fbSKevin Wolf     }
89894edf3fbSKevin Wolf 
89994edf3fbSKevin Wolf     refcount_cache_size /= s->cluster_size;
90094edf3fbSKevin Wolf     if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
90194edf3fbSKevin Wolf         refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
90294edf3fbSKevin Wolf     }
90394edf3fbSKevin Wolf     if (refcount_cache_size > INT_MAX) {
90494edf3fbSKevin Wolf         error_setg(errp, "Refcount cache size too big");
90594edf3fbSKevin Wolf         ret = -EINVAL;
90694edf3fbSKevin Wolf         goto fail;
90794edf3fbSKevin Wolf     }
90894edf3fbSKevin Wolf 
9095b0959a7SKevin Wolf     /* alloc new L2 table/refcount block cache, flush old one */
9105b0959a7SKevin Wolf     if (s->l2_table_cache) {
9115b0959a7SKevin Wolf         ret = qcow2_cache_flush(bs, s->l2_table_cache);
9125b0959a7SKevin Wolf         if (ret) {
9135b0959a7SKevin Wolf             error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
9145b0959a7SKevin Wolf             goto fail;
9155b0959a7SKevin Wolf         }
9165b0959a7SKevin Wolf     }
9175b0959a7SKevin Wolf 
9185b0959a7SKevin Wolf     if (s->refcount_block_cache) {
9195b0959a7SKevin Wolf         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
9205b0959a7SKevin Wolf         if (ret) {
9215b0959a7SKevin Wolf             error_setg_errno(errp, -ret,
9225b0959a7SKevin Wolf                              "Failed to flush the refcount block cache");
9235b0959a7SKevin Wolf             goto fail;
9245b0959a7SKevin Wolf         }
9255b0959a7SKevin Wolf     }
9265b0959a7SKevin Wolf 
9271221fe6fSAlberto Garcia     r->l2_slice_size = l2_cache_entry_size / sizeof(uint64_t);
9281221fe6fSAlberto Garcia     r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
9291221fe6fSAlberto Garcia                                            l2_cache_entry_size);
9301221fe6fSAlberto Garcia     r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
9311221fe6fSAlberto Garcia                                                  s->cluster_size);
932ee55b173SKevin Wolf     if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
93394edf3fbSKevin Wolf         error_setg(errp, "Could not allocate metadata caches");
93494edf3fbSKevin Wolf         ret = -ENOMEM;
93594edf3fbSKevin Wolf         goto fail;
93694edf3fbSKevin Wolf     }
93794edf3fbSKevin Wolf 
93894edf3fbSKevin Wolf     /* New interval for cache cleanup timer */
939ee55b173SKevin Wolf     r->cache_clean_interval =
9405b0959a7SKevin Wolf         qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
9415b0959a7SKevin Wolf                             s->cache_clean_interval);
94291203f08SAlberto Garcia #ifndef CONFIG_LINUX
94391203f08SAlberto Garcia     if (r->cache_clean_interval != 0) {
94491203f08SAlberto Garcia         error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
94591203f08SAlberto Garcia                    " not supported on this host");
94691203f08SAlberto Garcia         ret = -EINVAL;
94791203f08SAlberto Garcia         goto fail;
94891203f08SAlberto Garcia     }
94991203f08SAlberto Garcia #endif
950ee55b173SKevin Wolf     if (r->cache_clean_interval > UINT_MAX) {
95194edf3fbSKevin Wolf         error_setg(errp, "Cache clean interval too big");
95294edf3fbSKevin Wolf         ret = -EINVAL;
95394edf3fbSKevin Wolf         goto fail;
95494edf3fbSKevin Wolf     }
95594edf3fbSKevin Wolf 
9565b0959a7SKevin Wolf     /* lazy-refcounts; flush if going from enabled to disabled */
957ee55b173SKevin Wolf     r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
9584c75d1a1SKevin Wolf         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
959ee55b173SKevin Wolf     if (r->use_lazy_refcounts && s->qcow_version < 3) {
960007dbc39SKevin Wolf         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
961007dbc39SKevin Wolf                    "qemu 1.1 compatibility level");
962007dbc39SKevin Wolf         ret = -EINVAL;
963007dbc39SKevin Wolf         goto fail;
964007dbc39SKevin Wolf     }
9654c75d1a1SKevin Wolf 
9665b0959a7SKevin Wolf     if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
9675b0959a7SKevin Wolf         ret = qcow2_mark_clean(bs);
9685b0959a7SKevin Wolf         if (ret < 0) {
9695b0959a7SKevin Wolf             error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
9705b0959a7SKevin Wolf             goto fail;
9715b0959a7SKevin Wolf         }
9725b0959a7SKevin Wolf     }
9735b0959a7SKevin Wolf 
974007dbc39SKevin Wolf     /* Overlap check options */
9754c75d1a1SKevin Wolf     opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
9764c75d1a1SKevin Wolf     opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
9774c75d1a1SKevin Wolf     if (opt_overlap_check_template && opt_overlap_check &&
9784c75d1a1SKevin Wolf         strcmp(opt_overlap_check_template, opt_overlap_check))
9794c75d1a1SKevin Wolf     {
9804c75d1a1SKevin Wolf         error_setg(errp, "Conflicting values for qcow2 options '"
9814c75d1a1SKevin Wolf                    QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
9824c75d1a1SKevin Wolf                    "' ('%s')", opt_overlap_check, opt_overlap_check_template);
9834c75d1a1SKevin Wolf         ret = -EINVAL;
9844c75d1a1SKevin Wolf         goto fail;
9854c75d1a1SKevin Wolf     }
9864c75d1a1SKevin Wolf     if (!opt_overlap_check) {
9874c75d1a1SKevin Wolf         opt_overlap_check = opt_overlap_check_template ?: "cached";
9884c75d1a1SKevin Wolf     }
9894c75d1a1SKevin Wolf 
9904c75d1a1SKevin Wolf     if (!strcmp(opt_overlap_check, "none")) {
9914c75d1a1SKevin Wolf         overlap_check_template = 0;
9924c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "constant")) {
9934c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_CONSTANT;
9944c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "cached")) {
9954c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_CACHED;
9964c75d1a1SKevin Wolf     } else if (!strcmp(opt_overlap_check, "all")) {
9974c75d1a1SKevin Wolf         overlap_check_template = QCOW2_OL_ALL;
9984c75d1a1SKevin Wolf     } else {
9994c75d1a1SKevin Wolf         error_setg(errp, "Unsupported value '%s' for qcow2 option "
10004c75d1a1SKevin Wolf                    "'overlap-check'. Allowed are any of the following: "
10014c75d1a1SKevin Wolf                    "none, constant, cached, all", opt_overlap_check);
10024c75d1a1SKevin Wolf         ret = -EINVAL;
10034c75d1a1SKevin Wolf         goto fail;
10044c75d1a1SKevin Wolf     }
10054c75d1a1SKevin Wolf 
1006ee55b173SKevin Wolf     r->overlap_check = 0;
10074c75d1a1SKevin Wolf     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
10084c75d1a1SKevin Wolf         /* overlap-check defines a template bitmask, but every flag may be
10094c75d1a1SKevin Wolf          * overwritten through the associated boolean option */
1010ee55b173SKevin Wolf         r->overlap_check |=
10114c75d1a1SKevin Wolf             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
10124c75d1a1SKevin Wolf                               overlap_check_template & (1 << i)) << i;
10134c75d1a1SKevin Wolf     }
10144c75d1a1SKevin Wolf 
1015ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1016ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1017ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1018007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1019007dbc39SKevin Wolf                           flags & BDRV_O_UNMAP);
1020ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1021007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1022ee55b173SKevin Wolf     r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1023007dbc39SKevin Wolf         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1024007dbc39SKevin Wolf 
1025b25b387fSDaniel P. Berrange     switch (s->crypt_method_header) {
1026b25b387fSDaniel P. Berrange     case QCOW_CRYPT_NONE:
1027b25b387fSDaniel P. Berrange         if (encryptfmt) {
1028b25b387fSDaniel P. Berrange             error_setg(errp, "No encryption in image header, but options "
1029b25b387fSDaniel P. Berrange                        "specified format '%s'", encryptfmt);
1030b25b387fSDaniel P. Berrange             ret = -EINVAL;
1031b25b387fSDaniel P. Berrange             goto fail;
1032b25b387fSDaniel P. Berrange         }
1033b25b387fSDaniel P. Berrange         break;
1034b25b387fSDaniel P. Berrange 
1035b25b387fSDaniel P. Berrange     case QCOW_CRYPT_AES:
1036b25b387fSDaniel P. Berrange         if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1037b25b387fSDaniel P. Berrange             error_setg(errp,
1038b25b387fSDaniel P. Berrange                        "Header reported 'aes' encryption format but "
1039b25b387fSDaniel P. Berrange                        "options specify '%s'", encryptfmt);
1040b25b387fSDaniel P. Berrange             ret = -EINVAL;
1041b25b387fSDaniel P. Berrange             goto fail;
1042b25b387fSDaniel P. Berrange         }
1043b25b387fSDaniel P. Berrange         qdict_del(encryptopts, "format");
1044b25b387fSDaniel P. Berrange         r->crypto_opts = block_crypto_open_opts_init(
1045b25b387fSDaniel P. Berrange             Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
1046b25b387fSDaniel P. Berrange         break;
1047b25b387fSDaniel P. Berrange 
10484652b8f3SDaniel P. Berrange     case QCOW_CRYPT_LUKS:
10494652b8f3SDaniel P. Berrange         if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
10504652b8f3SDaniel P. Berrange             error_setg(errp,
10514652b8f3SDaniel P. Berrange                        "Header reported 'luks' encryption format but "
10524652b8f3SDaniel P. Berrange                        "options specify '%s'", encryptfmt);
10534652b8f3SDaniel P. Berrange             ret = -EINVAL;
10544652b8f3SDaniel P. Berrange             goto fail;
10554652b8f3SDaniel P. Berrange         }
10564652b8f3SDaniel P. Berrange         qdict_del(encryptopts, "format");
10574652b8f3SDaniel P. Berrange         r->crypto_opts = block_crypto_open_opts_init(
10584652b8f3SDaniel P. Berrange             Q_CRYPTO_BLOCK_FORMAT_LUKS, encryptopts, errp);
10594652b8f3SDaniel P. Berrange         break;
10604652b8f3SDaniel P. Berrange 
1061b25b387fSDaniel P. Berrange     default:
1062b25b387fSDaniel P. Berrange         error_setg(errp, "Unsupported encryption method %d",
1063b25b387fSDaniel P. Berrange                    s->crypt_method_header);
1064b25b387fSDaniel P. Berrange         break;
1065b25b387fSDaniel P. Berrange     }
1066b25b387fSDaniel P. Berrange     if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1067b25b387fSDaniel P. Berrange         ret = -EINVAL;
1068b25b387fSDaniel P. Berrange         goto fail;
1069b25b387fSDaniel P. Berrange     }
1070b25b387fSDaniel P. Berrange 
10714c75d1a1SKevin Wolf     ret = 0;
10724c75d1a1SKevin Wolf fail:
1073cb3e7f08SMarc-André Lureau     qobject_unref(encryptopts);
107494edf3fbSKevin Wolf     qemu_opts_del(opts);
107594edf3fbSKevin Wolf     opts = NULL;
1076ee55b173SKevin Wolf     return ret;
1077ee55b173SKevin Wolf }
1078ee55b173SKevin Wolf 
1079ee55b173SKevin Wolf static void qcow2_update_options_commit(BlockDriverState *bs,
1080ee55b173SKevin Wolf                                         Qcow2ReopenState *r)
1081ee55b173SKevin Wolf {
1082ee55b173SKevin Wolf     BDRVQcow2State *s = bs->opaque;
1083ee55b173SKevin Wolf     int i;
1084ee55b173SKevin Wolf 
10855b0959a7SKevin Wolf     if (s->l2_table_cache) {
1086e64d4072SAlberto Garcia         qcow2_cache_destroy(s->l2_table_cache);
10875b0959a7SKevin Wolf     }
10885b0959a7SKevin Wolf     if (s->refcount_block_cache) {
1089e64d4072SAlberto Garcia         qcow2_cache_destroy(s->refcount_block_cache);
10905b0959a7SKevin Wolf     }
1091ee55b173SKevin Wolf     s->l2_table_cache = r->l2_table_cache;
1092ee55b173SKevin Wolf     s->refcount_block_cache = r->refcount_block_cache;
10933c2e511aSAlberto Garcia     s->l2_slice_size = r->l2_slice_size;
1094ee55b173SKevin Wolf 
1095ee55b173SKevin Wolf     s->overlap_check = r->overlap_check;
1096ee55b173SKevin Wolf     s->use_lazy_refcounts = r->use_lazy_refcounts;
1097ee55b173SKevin Wolf 
1098ee55b173SKevin Wolf     for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1099ee55b173SKevin Wolf         s->discard_passthrough[i] = r->discard_passthrough[i];
1100ee55b173SKevin Wolf     }
1101ee55b173SKevin Wolf 
11025b0959a7SKevin Wolf     if (s->cache_clean_interval != r->cache_clean_interval) {
11035b0959a7SKevin Wolf         cache_clean_timer_del(bs);
1104ee55b173SKevin Wolf         s->cache_clean_interval = r->cache_clean_interval;
1105ee55b173SKevin Wolf         cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1106ee55b173SKevin Wolf     }
1107b25b387fSDaniel P. Berrange 
1108b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1109b25b387fSDaniel P. Berrange     s->crypto_opts = r->crypto_opts;
11105b0959a7SKevin Wolf }
1111ee55b173SKevin Wolf 
1112ee55b173SKevin Wolf static void qcow2_update_options_abort(BlockDriverState *bs,
1113ee55b173SKevin Wolf                                        Qcow2ReopenState *r)
1114ee55b173SKevin Wolf {
1115ee55b173SKevin Wolf     if (r->l2_table_cache) {
1116e64d4072SAlberto Garcia         qcow2_cache_destroy(r->l2_table_cache);
1117ee55b173SKevin Wolf     }
1118ee55b173SKevin Wolf     if (r->refcount_block_cache) {
1119e64d4072SAlberto Garcia         qcow2_cache_destroy(r->refcount_block_cache);
1120ee55b173SKevin Wolf     }
1121b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1122ee55b173SKevin Wolf }
1123ee55b173SKevin Wolf 
1124ee55b173SKevin Wolf static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1125ee55b173SKevin Wolf                                 int flags, Error **errp)
1126ee55b173SKevin Wolf {
1127ee55b173SKevin Wolf     Qcow2ReopenState r = {};
1128ee55b173SKevin Wolf     int ret;
1129ee55b173SKevin Wolf 
1130ee55b173SKevin Wolf     ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1131ee55b173SKevin Wolf     if (ret >= 0) {
1132ee55b173SKevin Wolf         qcow2_update_options_commit(bs, &r);
1133ee55b173SKevin Wolf     } else {
1134ee55b173SKevin Wolf         qcow2_update_options_abort(bs, &r);
1135ee55b173SKevin Wolf     }
113694edf3fbSKevin Wolf 
11374c75d1a1SKevin Wolf     return ret;
11384c75d1a1SKevin Wolf }
11394c75d1a1SKevin Wolf 
11401fafcd93SPaolo Bonzini /* Called with s->lock held.  */
11411fafcd93SPaolo Bonzini static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
11421fafcd93SPaolo Bonzini                                       int flags, Error **errp)
1143585f8587Sbellard {
1144ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
11456d33e8e7SKevin Wolf     unsigned int len, i;
11466d33e8e7SKevin Wolf     int ret = 0;
1147585f8587Sbellard     QCowHeader header;
114874c4510aSKevin Wolf     Error *local_err = NULL;
11499b80ddf3Saliguori     uint64_t ext_end;
11502cf7cfa1SKevin Wolf     uint64_t l1_vm_state_index;
115188ddffaeSVladimir Sementsov-Ogievskiy     bool update_header = false;
1152605bc8beSVladimir Sementsov-Ogievskiy     bool header_updated = false;
1153585f8587Sbellard 
1154cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
11556d85a57eSJes Sorensen     if (ret < 0) {
11563ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not read qcow2 header");
1157585f8587Sbellard         goto fail;
11586d85a57eSJes Sorensen     }
1159585f8587Sbellard     be32_to_cpus(&header.magic);
1160585f8587Sbellard     be32_to_cpus(&header.version);
1161585f8587Sbellard     be64_to_cpus(&header.backing_file_offset);
1162585f8587Sbellard     be32_to_cpus(&header.backing_file_size);
1163585f8587Sbellard     be64_to_cpus(&header.size);
1164585f8587Sbellard     be32_to_cpus(&header.cluster_bits);
1165585f8587Sbellard     be32_to_cpus(&header.crypt_method);
1166585f8587Sbellard     be64_to_cpus(&header.l1_table_offset);
1167585f8587Sbellard     be32_to_cpus(&header.l1_size);
1168585f8587Sbellard     be64_to_cpus(&header.refcount_table_offset);
1169585f8587Sbellard     be32_to_cpus(&header.refcount_table_clusters);
1170585f8587Sbellard     be64_to_cpus(&header.snapshots_offset);
1171585f8587Sbellard     be32_to_cpus(&header.nb_snapshots);
1172585f8587Sbellard 
1173e8cdcec1SKevin Wolf     if (header.magic != QCOW_MAGIC) {
11743ef6c40aSMax Reitz         error_setg(errp, "Image is not in qcow2 format");
117576abe407SPaolo Bonzini         ret = -EINVAL;
1176585f8587Sbellard         goto fail;
11776d85a57eSJes Sorensen     }
11786744cbabSKevin Wolf     if (header.version < 2 || header.version > 3) {
1179a55448b3SMax Reitz         error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1180e8cdcec1SKevin Wolf         ret = -ENOTSUP;
1181e8cdcec1SKevin Wolf         goto fail;
1182e8cdcec1SKevin Wolf     }
11836744cbabSKevin Wolf 
11846744cbabSKevin Wolf     s->qcow_version = header.version;
11856744cbabSKevin Wolf 
118624342f2cSKevin Wolf     /* Initialise cluster size */
118724342f2cSKevin Wolf     if (header.cluster_bits < MIN_CLUSTER_BITS ||
118824342f2cSKevin Wolf         header.cluster_bits > MAX_CLUSTER_BITS) {
1189521b2b5dSMax Reitz         error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1190521b2b5dSMax Reitz                    header.cluster_bits);
119124342f2cSKevin Wolf         ret = -EINVAL;
119224342f2cSKevin Wolf         goto fail;
119324342f2cSKevin Wolf     }
119424342f2cSKevin Wolf 
119524342f2cSKevin Wolf     s->cluster_bits = header.cluster_bits;
119624342f2cSKevin Wolf     s->cluster_size = 1 << s->cluster_bits;
1197a35f87f5SAlberto Garcia     s->cluster_sectors = 1 << (s->cluster_bits - BDRV_SECTOR_BITS);
119824342f2cSKevin Wolf 
11996744cbabSKevin Wolf     /* Initialise version 3 header fields */
12006744cbabSKevin Wolf     if (header.version == 2) {
12016744cbabSKevin Wolf         header.incompatible_features    = 0;
12026744cbabSKevin Wolf         header.compatible_features      = 0;
12036744cbabSKevin Wolf         header.autoclear_features       = 0;
12046744cbabSKevin Wolf         header.refcount_order           = 4;
12056744cbabSKevin Wolf         header.header_length            = 72;
12066744cbabSKevin Wolf     } else {
12076744cbabSKevin Wolf         be64_to_cpus(&header.incompatible_features);
12086744cbabSKevin Wolf         be64_to_cpus(&header.compatible_features);
12096744cbabSKevin Wolf         be64_to_cpus(&header.autoclear_features);
12106744cbabSKevin Wolf         be32_to_cpus(&header.refcount_order);
12116744cbabSKevin Wolf         be32_to_cpus(&header.header_length);
121224342f2cSKevin Wolf 
121324342f2cSKevin Wolf         if (header.header_length < 104) {
121424342f2cSKevin Wolf             error_setg(errp, "qcow2 header too short");
121524342f2cSKevin Wolf             ret = -EINVAL;
121624342f2cSKevin Wolf             goto fail;
121724342f2cSKevin Wolf         }
121824342f2cSKevin Wolf     }
121924342f2cSKevin Wolf 
122024342f2cSKevin Wolf     if (header.header_length > s->cluster_size) {
122124342f2cSKevin Wolf         error_setg(errp, "qcow2 header exceeds cluster size");
122224342f2cSKevin Wolf         ret = -EINVAL;
122324342f2cSKevin Wolf         goto fail;
12246744cbabSKevin Wolf     }
12256744cbabSKevin Wolf 
12266744cbabSKevin Wolf     if (header.header_length > sizeof(header)) {
12276744cbabSKevin Wolf         s->unknown_header_fields_size = header.header_length - sizeof(header);
12286744cbabSKevin Wolf         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1229cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
12306744cbabSKevin Wolf                          s->unknown_header_fields_size);
12316744cbabSKevin Wolf         if (ret < 0) {
12323ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
12333ef6c40aSMax Reitz                              "fields");
12346744cbabSKevin Wolf             goto fail;
12356744cbabSKevin Wolf         }
12366744cbabSKevin Wolf     }
12376744cbabSKevin Wolf 
1238a1b3955cSKevin Wolf     if (header.backing_file_offset > s->cluster_size) {
1239a1b3955cSKevin Wolf         error_setg(errp, "Invalid backing file offset");
1240a1b3955cSKevin Wolf         ret = -EINVAL;
1241a1b3955cSKevin Wolf         goto fail;
1242a1b3955cSKevin Wolf     }
1243a1b3955cSKevin Wolf 
1244cfcc4c62SKevin Wolf     if (header.backing_file_offset) {
1245cfcc4c62SKevin Wolf         ext_end = header.backing_file_offset;
1246cfcc4c62SKevin Wolf     } else {
1247cfcc4c62SKevin Wolf         ext_end = 1 << header.cluster_bits;
1248cfcc4c62SKevin Wolf     }
1249cfcc4c62SKevin Wolf 
12506744cbabSKevin Wolf     /* Handle feature bits */
12516744cbabSKevin Wolf     s->incompatible_features    = header.incompatible_features;
12526744cbabSKevin Wolf     s->compatible_features      = header.compatible_features;
12536744cbabSKevin Wolf     s->autoclear_features       = header.autoclear_features;
12546744cbabSKevin Wolf 
1255c61d0004SStefan Hajnoczi     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1256cfcc4c62SKevin Wolf         void *feature_table = NULL;
1257cfcc4c62SKevin Wolf         qcow2_read_extensions(bs, header.header_length, ext_end,
125888ddffaeSVladimir Sementsov-Ogievskiy                               &feature_table, flags, NULL, NULL);
1259a55448b3SMax Reitz         report_unsupported_feature(errp, feature_table,
1260c61d0004SStefan Hajnoczi                                    s->incompatible_features &
1261c61d0004SStefan Hajnoczi                                    ~QCOW2_INCOMPAT_MASK);
12626744cbabSKevin Wolf         ret = -ENOTSUP;
1263c5a33ee9SPrasad Joshi         g_free(feature_table);
12646744cbabSKevin Wolf         goto fail;
12656744cbabSKevin Wolf     }
12666744cbabSKevin Wolf 
126769c98726SMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
126869c98726SMax Reitz         /* Corrupt images may not be written to unless they are being repaired
126969c98726SMax Reitz          */
127069c98726SMax Reitz         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
12713ef6c40aSMax Reitz             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
12723ef6c40aSMax Reitz                        "read/write");
127369c98726SMax Reitz             ret = -EACCES;
127469c98726SMax Reitz             goto fail;
127569c98726SMax Reitz         }
127669c98726SMax Reitz     }
127769c98726SMax Reitz 
12786744cbabSKevin Wolf     /* Check support for various header values */
1279b72faf9fSMax Reitz     if (header.refcount_order > 6) {
1280b72faf9fSMax Reitz         error_setg(errp, "Reference count entry width too large; may not "
1281b72faf9fSMax Reitz                    "exceed 64 bits");
1282b72faf9fSMax Reitz         ret = -EINVAL;
12836744cbabSKevin Wolf         goto fail;
12846744cbabSKevin Wolf     }
1285b6481f37SMax Reitz     s->refcount_order = header.refcount_order;
1286346a53dfSMax Reitz     s->refcount_bits = 1 << s->refcount_order;
1287346a53dfSMax Reitz     s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1288346a53dfSMax Reitz     s->refcount_max += s->refcount_max - 1;
12896744cbabSKevin Wolf 
1290585f8587Sbellard     s->crypt_method_header = header.crypt_method;
12916d85a57eSJes Sorensen     if (s->crypt_method_header) {
1292e6ff69bfSDaniel P. Berrange         if (bdrv_uses_whitelist() &&
1293e6ff69bfSDaniel P. Berrange             s->crypt_method_header == QCOW_CRYPT_AES) {
12948c0dcbc4SDaniel P. Berrange             error_setg(errp,
12958c0dcbc4SDaniel P. Berrange                        "Use of AES-CBC encrypted qcow2 images is no longer "
12968c0dcbc4SDaniel P. Berrange                        "supported in system emulators");
12978c0dcbc4SDaniel P. Berrange             error_append_hint(errp,
12988c0dcbc4SDaniel P. Berrange                               "You can use 'qemu-img convert' to convert your "
12998c0dcbc4SDaniel P. Berrange                               "image to an alternative supported format, such "
13008c0dcbc4SDaniel P. Berrange                               "as unencrypted qcow2, or raw with the LUKS "
13018c0dcbc4SDaniel P. Berrange                               "format instead.\n");
13028c0dcbc4SDaniel P. Berrange             ret = -ENOSYS;
13038c0dcbc4SDaniel P. Berrange             goto fail;
1304e6ff69bfSDaniel P. Berrange         }
1305e6ff69bfSDaniel P. Berrange 
13064652b8f3SDaniel P. Berrange         if (s->crypt_method_header == QCOW_CRYPT_AES) {
13074652b8f3SDaniel P. Berrange             s->crypt_physical_offset = false;
13084652b8f3SDaniel P. Berrange         } else {
13094652b8f3SDaniel P. Berrange             /* Assuming LUKS and any future crypt methods we
13104652b8f3SDaniel P. Berrange              * add will all use physical offsets, due to the
13114652b8f3SDaniel P. Berrange              * fact that the alternative is insecure...  */
13124652b8f3SDaniel P. Berrange             s->crypt_physical_offset = true;
13134652b8f3SDaniel P. Berrange         }
13144652b8f3SDaniel P. Berrange 
131554115412SEric Blake         bs->encrypted = true;
13166d85a57eSJes Sorensen     }
131724342f2cSKevin Wolf 
1318585f8587Sbellard     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
1319585f8587Sbellard     s->l2_size = 1 << s->l2_bits;
13201d13d654SMax Reitz     /* 2^(s->refcount_order - 3) is the refcount width in bytes */
13211d13d654SMax Reitz     s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
13221d13d654SMax Reitz     s->refcount_block_size = 1 << s->refcount_block_bits;
1323585f8587Sbellard     bs->total_sectors = header.size / 512;
1324585f8587Sbellard     s->csize_shift = (62 - (s->cluster_bits - 8));
1325585f8587Sbellard     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1326585f8587Sbellard     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
13275dab2fadSKevin Wolf 
1328585f8587Sbellard     s->refcount_table_offset = header.refcount_table_offset;
1329585f8587Sbellard     s->refcount_table_size =
1330585f8587Sbellard         header.refcount_table_clusters << (s->cluster_bits - 3);
1331585f8587Sbellard 
1332951053a9SAlberto Garcia     if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1333951053a9SAlberto Garcia         error_setg(errp, "Image does not contain a reference count table");
1334951053a9SAlberto Garcia         ret = -EINVAL;
1335951053a9SAlberto Garcia         goto fail;
1336951053a9SAlberto Garcia     }
1337951053a9SAlberto Garcia 
13380cf0e598SAlberto Garcia     ret = qcow2_validate_table(bs, s->refcount_table_offset,
13390cf0e598SAlberto Garcia                                header.refcount_table_clusters,
13400cf0e598SAlberto Garcia                                s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
13410cf0e598SAlberto Garcia                                "Reference count table", errp);
13428c7de283SKevin Wolf     if (ret < 0) {
13438c7de283SKevin Wolf         goto fail;
13448c7de283SKevin Wolf     }
13458c7de283SKevin Wolf 
13460cf0e598SAlberto Garcia     /* The total size in bytes of the snapshot table is checked in
13470cf0e598SAlberto Garcia      * qcow2_read_snapshots() because the size of each snapshot is
13480cf0e598SAlberto Garcia      * variable and we don't know it yet.
13490cf0e598SAlberto Garcia      * Here we only check the offset and number of snapshots. */
13500cf0e598SAlberto Garcia     ret = qcow2_validate_table(bs, header.snapshots_offset,
1351ce48f2f4SKevin Wolf                                header.nb_snapshots,
13520cf0e598SAlberto Garcia                                sizeof(QCowSnapshotHeader),
13530cf0e598SAlberto Garcia                                sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
13540cf0e598SAlberto Garcia                                "Snapshot table", errp);
1355ce48f2f4SKevin Wolf     if (ret < 0) {
1356ce48f2f4SKevin Wolf         goto fail;
1357ce48f2f4SKevin Wolf     }
1358ce48f2f4SKevin Wolf 
1359585f8587Sbellard     /* read the level 1 table */
13600cf0e598SAlberto Garcia     ret = qcow2_validate_table(bs, header.l1_table_offset,
13610cf0e598SAlberto Garcia                                header.l1_size, sizeof(uint64_t),
13620cf0e598SAlberto Garcia                                QCOW_MAX_L1_SIZE, "Active L1 table", errp);
13630cf0e598SAlberto Garcia     if (ret < 0) {
13642d51c32cSKevin Wolf         goto fail;
13652d51c32cSKevin Wolf     }
1366585f8587Sbellard     s->l1_size = header.l1_size;
13670cf0e598SAlberto Garcia     s->l1_table_offset = header.l1_table_offset;
13682cf7cfa1SKevin Wolf 
13692cf7cfa1SKevin Wolf     l1_vm_state_index = size_to_l1(s, header.size);
13702cf7cfa1SKevin Wolf     if (l1_vm_state_index > INT_MAX) {
13713ef6c40aSMax Reitz         error_setg(errp, "Image is too big");
13722cf7cfa1SKevin Wolf         ret = -EFBIG;
13732cf7cfa1SKevin Wolf         goto fail;
13742cf7cfa1SKevin Wolf     }
13752cf7cfa1SKevin Wolf     s->l1_vm_state_index = l1_vm_state_index;
13762cf7cfa1SKevin Wolf 
1377585f8587Sbellard     /* the L1 table must contain at least enough entries to put
1378585f8587Sbellard        header.size bytes */
13796d85a57eSJes Sorensen     if (s->l1_size < s->l1_vm_state_index) {
13803ef6c40aSMax Reitz         error_setg(errp, "L1 table is too small");
13816d85a57eSJes Sorensen         ret = -EINVAL;
1382585f8587Sbellard         goto fail;
13836d85a57eSJes Sorensen     }
13842d51c32cSKevin Wolf 
1385d191d12dSStefan Weil     if (s->l1_size > 0) {
13869a4f4c31SKevin Wolf         s->l1_table = qemu_try_blockalign(bs->file->bs,
13879e029689SAlberto Garcia             ROUND_UP(s->l1_size * sizeof(uint64_t), 512));
1388de82815dSKevin Wolf         if (s->l1_table == NULL) {
1389de82815dSKevin Wolf             error_setg(errp, "Could not allocate L1 table");
1390de82815dSKevin Wolf             ret = -ENOMEM;
1391de82815dSKevin Wolf             goto fail;
1392de82815dSKevin Wolf         }
1393cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
13946d85a57eSJes Sorensen                          s->l1_size * sizeof(uint64_t));
13956d85a57eSJes Sorensen         if (ret < 0) {
13963ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read L1 table");
1397585f8587Sbellard             goto fail;
13986d85a57eSJes Sorensen         }
1399585f8587Sbellard         for(i = 0;i < s->l1_size; i++) {
1400585f8587Sbellard             be64_to_cpus(&s->l1_table[i]);
1401585f8587Sbellard         }
1402d191d12dSStefan Weil     }
140329c1a730SKevin Wolf 
140494edf3fbSKevin Wolf     /* Parse driver-specific options */
140594edf3fbSKevin Wolf     ret = qcow2_update_options(bs, options, flags, errp);
140690efa0eaSKevin Wolf     if (ret < 0) {
140790efa0eaSKevin Wolf         goto fail;
140890efa0eaSKevin Wolf     }
140990efa0eaSKevin Wolf 
1410585f8587Sbellard     s->cluster_cache_offset = -1;
141106d9260fSAnthony Liguori     s->flags = flags;
1412585f8587Sbellard 
14136d85a57eSJes Sorensen     ret = qcow2_refcount_init(bs);
14146d85a57eSJes Sorensen     if (ret != 0) {
14153ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1416585f8587Sbellard         goto fail;
14176d85a57eSJes Sorensen     }
1418585f8587Sbellard 
141972cf2d4fSBlue Swirl     QLIST_INIT(&s->cluster_allocs);
14200b919faeSKevin Wolf     QTAILQ_INIT(&s->discards);
1421f214978aSKevin Wolf 
14229b80ddf3Saliguori     /* read qcow2 extensions */
14233ef6c40aSMax Reitz     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
142488ddffaeSVladimir Sementsov-Ogievskiy                               flags, &update_header, &local_err)) {
14253ef6c40aSMax Reitz         error_propagate(errp, local_err);
14266d85a57eSJes Sorensen         ret = -EINVAL;
14279b80ddf3Saliguori         goto fail;
14286d85a57eSJes Sorensen     }
14299b80ddf3Saliguori 
14304652b8f3SDaniel P. Berrange     /* qcow2_read_extension may have set up the crypto context
14314652b8f3SDaniel P. Berrange      * if the crypt method needs a header region, some methods
14324652b8f3SDaniel P. Berrange      * don't need header extensions, so must check here
14334652b8f3SDaniel P. Berrange      */
14344652b8f3SDaniel P. Berrange     if (s->crypt_method_header && !s->crypto) {
1435b25b387fSDaniel P. Berrange         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1436b25b387fSDaniel P. Berrange             unsigned int cflags = 0;
1437b25b387fSDaniel P. Berrange             if (flags & BDRV_O_NO_IO) {
1438b25b387fSDaniel P. Berrange                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1439b25b387fSDaniel P. Berrange             }
14401cd9a787SDaniel P. Berrange             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
14411cd9a787SDaniel P. Berrange                                            NULL, NULL, cflags, errp);
1442b25b387fSDaniel P. Berrange             if (!s->crypto) {
1443b25b387fSDaniel P. Berrange                 ret = -EINVAL;
1444b25b387fSDaniel P. Berrange                 goto fail;
1445b25b387fSDaniel P. Berrange             }
14464652b8f3SDaniel P. Berrange         } else if (!(flags & BDRV_O_NO_IO)) {
14474652b8f3SDaniel P. Berrange             error_setg(errp, "Missing CRYPTO header for crypt method %d",
14484652b8f3SDaniel P. Berrange                        s->crypt_method_header);
14494652b8f3SDaniel P. Berrange             ret = -EINVAL;
14504652b8f3SDaniel P. Berrange             goto fail;
14514652b8f3SDaniel P. Berrange         }
1452b25b387fSDaniel P. Berrange     }
1453b25b387fSDaniel P. Berrange 
1454585f8587Sbellard     /* read the backing file name */
1455585f8587Sbellard     if (header.backing_file_offset != 0) {
1456585f8587Sbellard         len = header.backing_file_size;
14579a29e18fSJeff Cody         if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1458e729fa6aSJeff Cody             len >= sizeof(bs->backing_file)) {
14596d33e8e7SKevin Wolf             error_setg(errp, "Backing file name too long");
14606d33e8e7SKevin Wolf             ret = -EINVAL;
14616d33e8e7SKevin Wolf             goto fail;
14626d85a57eSJes Sorensen         }
1463cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, header.backing_file_offset,
14646d85a57eSJes Sorensen                          bs->backing_file, len);
14656d85a57eSJes Sorensen         if (ret < 0) {
14663ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not read backing file name");
1467585f8587Sbellard             goto fail;
14686d85a57eSJes Sorensen         }
1469585f8587Sbellard         bs->backing_file[len] = '\0';
1470e4603fe1SKevin Wolf         s->image_backing_file = g_strdup(bs->backing_file);
1471585f8587Sbellard     }
147242deb29fSKevin Wolf 
147311b128f4SKevin Wolf     /* Internal snapshots */
147411b128f4SKevin Wolf     s->snapshots_offset = header.snapshots_offset;
147511b128f4SKevin Wolf     s->nb_snapshots = header.nb_snapshots;
147611b128f4SKevin Wolf 
147742deb29fSKevin Wolf     ret = qcow2_read_snapshots(bs);
147842deb29fSKevin Wolf     if (ret < 0) {
14793ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not read snapshots");
1480585f8587Sbellard         goto fail;
14816d85a57eSJes Sorensen     }
1482585f8587Sbellard 
1483af7b708dSStefan Hajnoczi     /* Clear unknown autoclear feature bits */
148488ddffaeSVladimir Sementsov-Ogievskiy     update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1485d1258dd0SVladimir Sementsov-Ogievskiy     update_header =
1486d1258dd0SVladimir Sementsov-Ogievskiy         update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1487d1258dd0SVladimir Sementsov-Ogievskiy     if (update_header) {
148888ddffaeSVladimir Sementsov-Ogievskiy         s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1489d1258dd0SVladimir Sementsov-Ogievskiy     }
1490d1258dd0SVladimir Sementsov-Ogievskiy 
1491605bc8beSVladimir Sementsov-Ogievskiy     if (s->dirty_bitmaps_loaded) {
1492605bc8beSVladimir Sementsov-Ogievskiy         /* It's some kind of reopen. There are no known cases where we need to
1493605bc8beSVladimir Sementsov-Ogievskiy          * reload bitmaps in such a situation, so it's safer to skip them.
14942d949dfcSVladimir Sementsov-Ogievskiy          *
14952d949dfcSVladimir Sementsov-Ogievskiy          * Moreover, if we have some readonly bitmaps and we are reopening for
14962d949dfcSVladimir Sementsov-Ogievskiy          * rw we should reopen bitmaps correspondingly.
14972d949dfcSVladimir Sementsov-Ogievskiy          */
14982d949dfcSVladimir Sementsov-Ogievskiy         if (bdrv_has_readonly_bitmaps(bs) &&
14992d949dfcSVladimir Sementsov-Ogievskiy             !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE))
15002d949dfcSVladimir Sementsov-Ogievskiy         {
15012d949dfcSVladimir Sementsov-Ogievskiy             qcow2_reopen_bitmaps_rw_hint(bs, &header_updated, &local_err);
1502605bc8beSVladimir Sementsov-Ogievskiy         }
1503605bc8beSVladimir Sementsov-Ogievskiy     } else {
1504605bc8beSVladimir Sementsov-Ogievskiy         header_updated = qcow2_load_dirty_bitmaps(bs, &local_err);
1505605bc8beSVladimir Sementsov-Ogievskiy         s->dirty_bitmaps_loaded = true;
1506605bc8beSVladimir Sementsov-Ogievskiy     }
15072d949dfcSVladimir Sementsov-Ogievskiy     update_header = update_header && !header_updated;
1508d1258dd0SVladimir Sementsov-Ogievskiy     if (local_err != NULL) {
1509d1258dd0SVladimir Sementsov-Ogievskiy         error_propagate(errp, local_err);
1510d1258dd0SVladimir Sementsov-Ogievskiy         ret = -EINVAL;
1511d1258dd0SVladimir Sementsov-Ogievskiy         goto fail;
1512d1258dd0SVladimir Sementsov-Ogievskiy     }
1513d1258dd0SVladimir Sementsov-Ogievskiy 
1514d1258dd0SVladimir Sementsov-Ogievskiy     if (update_header) {
1515af7b708dSStefan Hajnoczi         ret = qcow2_update_header(bs);
1516af7b708dSStefan Hajnoczi         if (ret < 0) {
15173ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not update qcow2 header");
1518af7b708dSStefan Hajnoczi             goto fail;
1519af7b708dSStefan Hajnoczi         }
1520af7b708dSStefan Hajnoczi     }
1521af7b708dSStefan Hajnoczi 
1522e24d813bSEric Blake     bs->supported_zero_flags = header.version >= 3 ? BDRV_REQ_MAY_UNMAP : 0;
152368d100e9SKevin Wolf 
1524c61d0004SStefan Hajnoczi     /* Repair image if dirty */
152504c01a5cSKevin Wolf     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1526058f8f16SStefan Hajnoczi         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1527c61d0004SStefan Hajnoczi         BdrvCheckResult result = {0};
1528c61d0004SStefan Hajnoczi 
15292fd61638SPaolo Bonzini         ret = qcow2_co_check_locked(bs, &result,
15302fd61638SPaolo Bonzini                                     BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1531791fff50SMax Reitz         if (ret < 0 || result.check_errors) {
1532791fff50SMax Reitz             if (ret >= 0) {
1533791fff50SMax Reitz                 ret = -EIO;
1534791fff50SMax Reitz             }
15353ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not repair dirty image");
1536c61d0004SStefan Hajnoczi             goto fail;
1537c61d0004SStefan Hajnoczi         }
1538c61d0004SStefan Hajnoczi     }
1539c61d0004SStefan Hajnoczi 
1540585f8587Sbellard #ifdef DEBUG_ALLOC
15416cbc3031SPhilipp Hahn     {
15426cbc3031SPhilipp Hahn         BdrvCheckResult result = {0};
1543b35278f7SStefan Hajnoczi         qcow2_check_refcounts(bs, &result, 0);
15446cbc3031SPhilipp Hahn     }
1545585f8587Sbellard #endif
15466d85a57eSJes Sorensen     return ret;
1547585f8587Sbellard 
1548585f8587Sbellard  fail:
15496744cbabSKevin Wolf     g_free(s->unknown_header_fields);
155075bab85cSKevin Wolf     cleanup_unknown_header_ext(bs);
1551ed6ccf0fSKevin Wolf     qcow2_free_snapshots(bs);
1552ed6ccf0fSKevin Wolf     qcow2_refcount_close(bs);
1553de82815dSKevin Wolf     qemu_vfree(s->l1_table);
1554cf93980eSMax Reitz     /* else pre-write overlap checks in cache_destroy may crash */
1555cf93980eSMax Reitz     s->l1_table = NULL;
1556279621c0SAlberto Garcia     cache_clean_timer_del(bs);
155729c1a730SKevin Wolf     if (s->l2_table_cache) {
1558e64d4072SAlberto Garcia         qcow2_cache_destroy(s->l2_table_cache);
155929c1a730SKevin Wolf     }
1560c5a33ee9SPrasad Joshi     if (s->refcount_block_cache) {
1561e64d4072SAlberto Garcia         qcow2_cache_destroy(s->refcount_block_cache);
1562c5a33ee9SPrasad Joshi     }
1563b25b387fSDaniel P. Berrange     qcrypto_block_free(s->crypto);
1564b25b387fSDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
15656d85a57eSJes Sorensen     return ret;
1566585f8587Sbellard }
1567585f8587Sbellard 
15681fafcd93SPaolo Bonzini typedef struct QCow2OpenCo {
15691fafcd93SPaolo Bonzini     BlockDriverState *bs;
15701fafcd93SPaolo Bonzini     QDict *options;
15711fafcd93SPaolo Bonzini     int flags;
15721fafcd93SPaolo Bonzini     Error **errp;
15731fafcd93SPaolo Bonzini     int ret;
15741fafcd93SPaolo Bonzini } QCow2OpenCo;
15751fafcd93SPaolo Bonzini 
15761fafcd93SPaolo Bonzini static void coroutine_fn qcow2_open_entry(void *opaque)
15771fafcd93SPaolo Bonzini {
15781fafcd93SPaolo Bonzini     QCow2OpenCo *qoc = opaque;
15791fafcd93SPaolo Bonzini     BDRVQcow2State *s = qoc->bs->opaque;
15801fafcd93SPaolo Bonzini 
15811fafcd93SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
15821fafcd93SPaolo Bonzini     qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
15831fafcd93SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
15841fafcd93SPaolo Bonzini }
15851fafcd93SPaolo Bonzini 
15864e4bf5c4SKevin Wolf static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
15874e4bf5c4SKevin Wolf                       Error **errp)
15884e4bf5c4SKevin Wolf {
15891fafcd93SPaolo Bonzini     BDRVQcow2State *s = bs->opaque;
15901fafcd93SPaolo Bonzini     QCow2OpenCo qoc = {
15911fafcd93SPaolo Bonzini         .bs = bs,
15921fafcd93SPaolo Bonzini         .options = options,
15931fafcd93SPaolo Bonzini         .flags = flags,
15941fafcd93SPaolo Bonzini         .errp = errp,
15951fafcd93SPaolo Bonzini         .ret = -EINPROGRESS
15961fafcd93SPaolo Bonzini     };
15971fafcd93SPaolo Bonzini 
15984e4bf5c4SKevin Wolf     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
15994e4bf5c4SKevin Wolf                                false, errp);
16004e4bf5c4SKevin Wolf     if (!bs->file) {
16014e4bf5c4SKevin Wolf         return -EINVAL;
16024e4bf5c4SKevin Wolf     }
16034e4bf5c4SKevin Wolf 
16041fafcd93SPaolo Bonzini     /* Initialise locks */
16051fafcd93SPaolo Bonzini     qemu_co_mutex_init(&s->lock);
16061fafcd93SPaolo Bonzini 
16071fafcd93SPaolo Bonzini     if (qemu_in_coroutine()) {
16081fafcd93SPaolo Bonzini         /* From bdrv_co_create.  */
16091fafcd93SPaolo Bonzini         qcow2_open_entry(&qoc);
16101fafcd93SPaolo Bonzini     } else {
16111fafcd93SPaolo Bonzini         qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc));
16121fafcd93SPaolo Bonzini         BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
16131fafcd93SPaolo Bonzini     }
16141fafcd93SPaolo Bonzini     return qoc.ret;
16154e4bf5c4SKevin Wolf }
16164e4bf5c4SKevin Wolf 
16173baca891SKevin Wolf static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1618d34682cdSKevin Wolf {
1619ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1620d34682cdSKevin Wolf 
1621a84178ccSEric Blake     if (bs->encrypted) {
1622a84178ccSEric Blake         /* Encryption works on a sector granularity */
1623a5b8dd2cSEric Blake         bs->bl.request_alignment = BDRV_SECTOR_SIZE;
1624a84178ccSEric Blake     }
1625cf081fcaSEric Blake     bs->bl.pwrite_zeroes_alignment = s->cluster_size;
1626ecdbead6SEric Blake     bs->bl.pdiscard_alignment = s->cluster_size;
1627d34682cdSKevin Wolf }
1628d34682cdSKevin Wolf 
162921d82ac9SJeff Cody static int qcow2_reopen_prepare(BDRVReopenState *state,
163021d82ac9SJeff Cody                                 BlockReopenQueue *queue, Error **errp)
163121d82ac9SJeff Cody {
16325b0959a7SKevin Wolf     Qcow2ReopenState *r;
16334c2e5f8fSKevin Wolf     int ret;
16344c2e5f8fSKevin Wolf 
16355b0959a7SKevin Wolf     r = g_new0(Qcow2ReopenState, 1);
16365b0959a7SKevin Wolf     state->opaque = r;
16375b0959a7SKevin Wolf 
16385b0959a7SKevin Wolf     ret = qcow2_update_options_prepare(state->bs, r, state->options,
16395b0959a7SKevin Wolf                                        state->flags, errp);
16405b0959a7SKevin Wolf     if (ret < 0) {
16415b0959a7SKevin Wolf         goto fail;
16425b0959a7SKevin Wolf     }
16435b0959a7SKevin Wolf 
16445b0959a7SKevin Wolf     /* We need to write out any unwritten data if we reopen read-only. */
16454c2e5f8fSKevin Wolf     if ((state->flags & BDRV_O_RDWR) == 0) {
1646169b8793SVladimir Sementsov-Ogievskiy         ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1647169b8793SVladimir Sementsov-Ogievskiy         if (ret < 0) {
1648169b8793SVladimir Sementsov-Ogievskiy             goto fail;
1649169b8793SVladimir Sementsov-Ogievskiy         }
1650169b8793SVladimir Sementsov-Ogievskiy 
16514c2e5f8fSKevin Wolf         ret = bdrv_flush(state->bs);
16524c2e5f8fSKevin Wolf         if (ret < 0) {
16535b0959a7SKevin Wolf             goto fail;
16544c2e5f8fSKevin Wolf         }
16554c2e5f8fSKevin Wolf 
16564c2e5f8fSKevin Wolf         ret = qcow2_mark_clean(state->bs);
16574c2e5f8fSKevin Wolf         if (ret < 0) {
16585b0959a7SKevin Wolf             goto fail;
16594c2e5f8fSKevin Wolf         }
16604c2e5f8fSKevin Wolf     }
16614c2e5f8fSKevin Wolf 
166221d82ac9SJeff Cody     return 0;
16635b0959a7SKevin Wolf 
16645b0959a7SKevin Wolf fail:
16655b0959a7SKevin Wolf     qcow2_update_options_abort(state->bs, r);
16665b0959a7SKevin Wolf     g_free(r);
16675b0959a7SKevin Wolf     return ret;
16685b0959a7SKevin Wolf }
16695b0959a7SKevin Wolf 
16705b0959a7SKevin Wolf static void qcow2_reopen_commit(BDRVReopenState *state)
16715b0959a7SKevin Wolf {
16725b0959a7SKevin Wolf     qcow2_update_options_commit(state->bs, state->opaque);
16735b0959a7SKevin Wolf     g_free(state->opaque);
16745b0959a7SKevin Wolf }
16755b0959a7SKevin Wolf 
16765b0959a7SKevin Wolf static void qcow2_reopen_abort(BDRVReopenState *state)
16775b0959a7SKevin Wolf {
16785b0959a7SKevin Wolf     qcow2_update_options_abort(state->bs, state->opaque);
16795b0959a7SKevin Wolf     g_free(state->opaque);
168021d82ac9SJeff Cody }
168121d82ac9SJeff Cody 
16825365f44dSKevin Wolf static void qcow2_join_options(QDict *options, QDict *old_options)
16835365f44dSKevin Wolf {
16845365f44dSKevin Wolf     bool has_new_overlap_template =
16855365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
16865365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
16875365f44dSKevin Wolf     bool has_new_total_cache_size =
16885365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
16895365f44dSKevin Wolf     bool has_all_cache_options;
16905365f44dSKevin Wolf 
16915365f44dSKevin Wolf     /* New overlap template overrides all old overlap options */
16925365f44dSKevin Wolf     if (has_new_overlap_template) {
16935365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP);
16945365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
16955365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
16965365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
16975365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
16985365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
16995365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
17005365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
17015365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
17025365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
17035365f44dSKevin Wolf     }
17045365f44dSKevin Wolf 
17055365f44dSKevin Wolf     /* New total cache size overrides all old options */
17065365f44dSKevin Wolf     if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
17075365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
17085365f44dSKevin Wolf         qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
17095365f44dSKevin Wolf     }
17105365f44dSKevin Wolf 
17115365f44dSKevin Wolf     qdict_join(options, old_options, false);
17125365f44dSKevin Wolf 
17135365f44dSKevin Wolf     /*
17145365f44dSKevin Wolf      * If after merging all cache size options are set, an old total size is
17155365f44dSKevin Wolf      * overwritten. Do keep all options, however, if all three are new. The
17165365f44dSKevin Wolf      * resulting error message is what we want to happen.
17175365f44dSKevin Wolf      */
17185365f44dSKevin Wolf     has_all_cache_options =
17195365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
17205365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
17215365f44dSKevin Wolf         qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
17225365f44dSKevin Wolf 
17235365f44dSKevin Wolf     if (has_all_cache_options && !has_new_total_cache_size) {
17245365f44dSKevin Wolf         qdict_del(options, QCOW2_OPT_CACHE_SIZE);
17255365f44dSKevin Wolf     }
17265365f44dSKevin Wolf }
17275365f44dSKevin Wolf 
1728a320fb04SEric Blake static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
1729a320fb04SEric Blake                                               bool want_zero,
1730a320fb04SEric Blake                                               int64_t offset, int64_t count,
1731a320fb04SEric Blake                                               int64_t *pnum, int64_t *map,
1732a320fb04SEric Blake                                               BlockDriverState **file)
1733585f8587Sbellard {
1734ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1735585f8587Sbellard     uint64_t cluster_offset;
17364bc74be9SPaolo Bonzini     int index_in_cluster, ret;
1737ecfe1863SKevin Wolf     unsigned int bytes;
1738a320fb04SEric Blake     int status = 0;
1739585f8587Sbellard 
1740a320fb04SEric Blake     bytes = MIN(INT_MAX, count);
1741f8a2e5e3SStefan Hajnoczi     qemu_co_mutex_lock(&s->lock);
1742a320fb04SEric Blake     ret = qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset);
1743f8a2e5e3SStefan Hajnoczi     qemu_co_mutex_unlock(&s->lock);
17441c46efaaSKevin Wolf     if (ret < 0) {
1745d663640cSPaolo Bonzini         return ret;
17461c46efaaSKevin Wolf     }
1747095a9c58Saliguori 
1748a320fb04SEric Blake     *pnum = bytes;
1749ecfe1863SKevin Wolf 
17504bc74be9SPaolo Bonzini     if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1751b25b387fSDaniel P. Berrange         !s->crypto) {
1752a320fb04SEric Blake         index_in_cluster = offset & (s->cluster_size - 1);
1753a320fb04SEric Blake         *map = cluster_offset | index_in_cluster;
1754178b4db7SFam Zheng         *file = bs->file->bs;
1755a320fb04SEric Blake         status |= BDRV_BLOCK_OFFSET_VALID;
17564bc74be9SPaolo Bonzini     }
1757fdfab37dSEric Blake     if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
17584bc74be9SPaolo Bonzini         status |= BDRV_BLOCK_ZERO;
17594bc74be9SPaolo Bonzini     } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
17604bc74be9SPaolo Bonzini         status |= BDRV_BLOCK_DATA;
17614bc74be9SPaolo Bonzini     }
17624bc74be9SPaolo Bonzini     return status;
1763585f8587Sbellard }
1764585f8587Sbellard 
1765fd9fcd37SFam Zheng static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
1766fd9fcd37SFam Zheng                                             QCowL2Meta **pl2meta,
1767fd9fcd37SFam Zheng                                             bool link_l2)
1768fd9fcd37SFam Zheng {
1769fd9fcd37SFam Zheng     int ret = 0;
1770fd9fcd37SFam Zheng     QCowL2Meta *l2meta = *pl2meta;
1771fd9fcd37SFam Zheng 
1772fd9fcd37SFam Zheng     while (l2meta != NULL) {
1773fd9fcd37SFam Zheng         QCowL2Meta *next;
1774fd9fcd37SFam Zheng 
1775*354d930dSFam Zheng         if (link_l2) {
1776fd9fcd37SFam Zheng             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1777fd9fcd37SFam Zheng             if (ret) {
1778fd9fcd37SFam Zheng                 goto out;
1779fd9fcd37SFam Zheng             }
1780fd9fcd37SFam Zheng         }
1781fd9fcd37SFam Zheng 
1782fd9fcd37SFam Zheng         /* Take the request off the list of running requests */
1783fd9fcd37SFam Zheng         if (l2meta->nb_clusters != 0) {
1784fd9fcd37SFam Zheng             QLIST_REMOVE(l2meta, next_in_flight);
1785fd9fcd37SFam Zheng         }
1786fd9fcd37SFam Zheng 
1787fd9fcd37SFam Zheng         qemu_co_queue_restart_all(&l2meta->dependent_requests);
1788fd9fcd37SFam Zheng 
1789fd9fcd37SFam Zheng         next = l2meta->next;
1790fd9fcd37SFam Zheng         g_free(l2meta);
1791fd9fcd37SFam Zheng         l2meta = next;
1792fd9fcd37SFam Zheng     }
1793fd9fcd37SFam Zheng out:
1794fd9fcd37SFam Zheng     *pl2meta = l2meta;
1795fd9fcd37SFam Zheng     return ret;
1796fd9fcd37SFam Zheng }
1797fd9fcd37SFam Zheng 
1798ecfe1863SKevin Wolf static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
1799ecfe1863SKevin Wolf                                         uint64_t bytes, QEMUIOVector *qiov,
1800ecfe1863SKevin Wolf                                         int flags)
18011490791fSaliguori {
1802ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1803546a7dc4SEdgar Kaziakhmedov     int offset_in_cluster;
180468d100e9SKevin Wolf     int ret;
1805ecfe1863SKevin Wolf     unsigned int cur_bytes; /* number of bytes in current iteration */
1806c2bdd990SFrediano Ziglio     uint64_t cluster_offset = 0;
18073fc48d09SFrediano Ziglio     uint64_t bytes_done = 0;
18083fc48d09SFrediano Ziglio     QEMUIOVector hd_qiov;
18093fc48d09SFrediano Ziglio     uint8_t *cluster_data = NULL;
1810585f8587Sbellard 
18113fc48d09SFrediano Ziglio     qemu_iovec_init(&hd_qiov, qiov->niov);
18123fc48d09SFrediano Ziglio 
18133fc48d09SFrediano Ziglio     qemu_co_mutex_lock(&s->lock);
18143fc48d09SFrediano Ziglio 
1815ecfe1863SKevin Wolf     while (bytes != 0) {
1816585f8587Sbellard 
1817faf575c1SFrediano Ziglio         /* prepare next request */
1818ecfe1863SKevin Wolf         cur_bytes = MIN(bytes, INT_MAX);
1819b25b387fSDaniel P. Berrange         if (s->crypto) {
1820ecfe1863SKevin Wolf             cur_bytes = MIN(cur_bytes,
1821ecfe1863SKevin Wolf                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1822bd28f835SKevin Wolf         }
1823bd28f835SKevin Wolf 
1824ecfe1863SKevin Wolf         ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
18251c46efaaSKevin Wolf         if (ret < 0) {
18263fc48d09SFrediano Ziglio             goto fail;
18271c46efaaSKevin Wolf         }
18281c46efaaSKevin Wolf 
1829ecfe1863SKevin Wolf         offset_in_cluster = offset_into_cluster(s, offset);
1830585f8587Sbellard 
18313fc48d09SFrediano Ziglio         qemu_iovec_reset(&hd_qiov);
1832ecfe1863SKevin Wolf         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
1833bd28f835SKevin Wolf 
183468d000a3SKevin Wolf         switch (ret) {
183568d000a3SKevin Wolf         case QCOW2_CLUSTER_UNALLOCATED:
1836bd28f835SKevin Wolf 
1837760e0063SKevin Wolf             if (bs->backing) {
183866f82ceeSKevin Wolf                 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
183968d100e9SKevin Wolf                 qemu_co_mutex_unlock(&s->lock);
1840546a7dc4SEdgar Kaziakhmedov                 ret = bdrv_co_preadv(bs->backing, offset, cur_bytes,
1841546a7dc4SEdgar Kaziakhmedov                                      &hd_qiov, 0);
184268d100e9SKevin Wolf                 qemu_co_mutex_lock(&s->lock);
184368d100e9SKevin Wolf                 if (ret < 0) {
18443fc48d09SFrediano Ziglio                     goto fail;
18453ab4c7e9SKevin Wolf                 }
1846a9465922Sbellard             } else {
1847585f8587Sbellard                 /* Note: in this case, no need to wait */
1848ecfe1863SKevin Wolf                 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
18491490791fSaliguori             }
185068d000a3SKevin Wolf             break;
185168d000a3SKevin Wolf 
1852fdfab37dSEric Blake         case QCOW2_CLUSTER_ZERO_PLAIN:
1853fdfab37dSEric Blake         case QCOW2_CLUSTER_ZERO_ALLOC:
1854ecfe1863SKevin Wolf             qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
18556377af48SKevin Wolf             break;
18566377af48SKevin Wolf 
185768d000a3SKevin Wolf         case QCOW2_CLUSTER_COMPRESSED:
1858585f8587Sbellard             /* add AIO support for compressed blocks ? */
1859c2bdd990SFrediano Ziglio             ret = qcow2_decompress_cluster(bs, cluster_offset);
18608af36488SKevin Wolf             if (ret < 0) {
18613fc48d09SFrediano Ziglio                 goto fail;
18628af36488SKevin Wolf             }
1863bd28f835SKevin Wolf 
186403396148SMichael Tokarev             qemu_iovec_from_buf(&hd_qiov, 0,
1865ecfe1863SKevin Wolf                                 s->cluster_cache + offset_in_cluster,
1866ecfe1863SKevin Wolf                                 cur_bytes);
186768d000a3SKevin Wolf             break;
186868d000a3SKevin Wolf 
186968d000a3SKevin Wolf         case QCOW2_CLUSTER_NORMAL:
1870c2bdd990SFrediano Ziglio             if ((cluster_offset & 511) != 0) {
18713fc48d09SFrediano Ziglio                 ret = -EIO;
18723fc48d09SFrediano Ziglio                 goto fail;
1873585f8587Sbellard             }
1874c87c0672Saliguori 
18758336aafaSDaniel P. Berrange             if (bs->encrypted) {
1876b25b387fSDaniel P. Berrange                 assert(s->crypto);
18778336aafaSDaniel P. Berrange 
1878bd28f835SKevin Wolf                 /*
1879bd28f835SKevin Wolf                  * For encrypted images, read everything into a temporary
1880bd28f835SKevin Wolf                  * contiguous buffer on which the AES functions can work.
1881bd28f835SKevin Wolf                  */
18823fc48d09SFrediano Ziglio                 if (!cluster_data) {
18833fc48d09SFrediano Ziglio                     cluster_data =
18849a4f4c31SKevin Wolf                         qemu_try_blockalign(bs->file->bs,
18859a4f4c31SKevin Wolf                                             QCOW_MAX_CRYPT_CLUSTERS
1886de82815dSKevin Wolf                                             * s->cluster_size);
1887de82815dSKevin Wolf                     if (cluster_data == NULL) {
1888de82815dSKevin Wolf                         ret = -ENOMEM;
1889de82815dSKevin Wolf                         goto fail;
1890de82815dSKevin Wolf                     }
1891bd28f835SKevin Wolf                 }
1892bd28f835SKevin Wolf 
1893ecfe1863SKevin Wolf                 assert(cur_bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
18943fc48d09SFrediano Ziglio                 qemu_iovec_reset(&hd_qiov);
1895ecfe1863SKevin Wolf                 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
1896bd28f835SKevin Wolf             }
1897bd28f835SKevin Wolf 
189866f82ceeSKevin Wolf             BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
189968d100e9SKevin Wolf             qemu_co_mutex_unlock(&s->lock);
1900a03ef88fSKevin Wolf             ret = bdrv_co_preadv(bs->file,
1901ecfe1863SKevin Wolf                                  cluster_offset + offset_in_cluster,
1902ecfe1863SKevin Wolf                                  cur_bytes, &hd_qiov, 0);
190368d100e9SKevin Wolf             qemu_co_mutex_lock(&s->lock);
190468d100e9SKevin Wolf             if (ret < 0) {
19053fc48d09SFrediano Ziglio                 goto fail;
1906585f8587Sbellard             }
19078336aafaSDaniel P. Berrange             if (bs->encrypted) {
1908b25b387fSDaniel P. Berrange                 assert(s->crypto);
1909ecfe1863SKevin Wolf                 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1910ecfe1863SKevin Wolf                 assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1911b25b387fSDaniel P. Berrange                 if (qcrypto_block_decrypt(s->crypto,
19124652b8f3SDaniel P. Berrange                                           (s->crypt_physical_offset ?
19134652b8f3SDaniel P. Berrange                                            cluster_offset + offset_in_cluster :
19144609742aSDaniel P. Berrange                                            offset),
1915446d306dSDaniel P. Berrange                                           cluster_data,
1916b25b387fSDaniel P. Berrange                                           cur_bytes,
1917c3a8fe33SAlberto Garcia                                           NULL) < 0) {
1918f6fa64f6SDaniel P. Berrange                     ret = -EIO;
1919f6fa64f6SDaniel P. Berrange                     goto fail;
1920f6fa64f6SDaniel P. Berrange                 }
1921ecfe1863SKevin Wolf                 qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_bytes);
1922171e3d6bSKevin Wolf             }
192368d000a3SKevin Wolf             break;
192468d000a3SKevin Wolf 
192568d000a3SKevin Wolf         default:
192668d000a3SKevin Wolf             g_assert_not_reached();
192768d000a3SKevin Wolf             ret = -EIO;
192868d000a3SKevin Wolf             goto fail;
1929faf575c1SFrediano Ziglio         }
1930faf575c1SFrediano Ziglio 
1931ecfe1863SKevin Wolf         bytes -= cur_bytes;
1932ecfe1863SKevin Wolf         offset += cur_bytes;
1933ecfe1863SKevin Wolf         bytes_done += cur_bytes;
19345ebaa27eSFrediano Ziglio     }
19353fc48d09SFrediano Ziglio     ret = 0;
1936f141eafeSaliguori 
19373fc48d09SFrediano Ziglio fail:
193868d100e9SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
193968d100e9SKevin Wolf 
19403fc48d09SFrediano Ziglio     qemu_iovec_destroy(&hd_qiov);
1941dea43a65SFrediano Ziglio     qemu_vfree(cluster_data);
194268d100e9SKevin Wolf 
194368d100e9SKevin Wolf     return ret;
1944585f8587Sbellard }
1945585f8587Sbellard 
1946ee22a9d8SAlberto Garcia /* Check if it's possible to merge a write request with the writing of
1947ee22a9d8SAlberto Garcia  * the data from the COW regions */
1948ee22a9d8SAlberto Garcia static bool merge_cow(uint64_t offset, unsigned bytes,
1949ee22a9d8SAlberto Garcia                       QEMUIOVector *hd_qiov, QCowL2Meta *l2meta)
1950ee22a9d8SAlberto Garcia {
1951ee22a9d8SAlberto Garcia     QCowL2Meta *m;
1952ee22a9d8SAlberto Garcia 
1953ee22a9d8SAlberto Garcia     for (m = l2meta; m != NULL; m = m->next) {
1954ee22a9d8SAlberto Garcia         /* If both COW regions are empty then there's nothing to merge */
1955ee22a9d8SAlberto Garcia         if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
1956ee22a9d8SAlberto Garcia             continue;
1957ee22a9d8SAlberto Garcia         }
1958ee22a9d8SAlberto Garcia 
1959ee22a9d8SAlberto Garcia         /* The data (middle) region must be immediately after the
1960ee22a9d8SAlberto Garcia          * start region */
1961ee22a9d8SAlberto Garcia         if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
1962ee22a9d8SAlberto Garcia             continue;
1963ee22a9d8SAlberto Garcia         }
1964ee22a9d8SAlberto Garcia 
1965ee22a9d8SAlberto Garcia         /* The end region must be immediately after the data (middle)
1966ee22a9d8SAlberto Garcia          * region */
1967ee22a9d8SAlberto Garcia         if (m->offset + m->cow_end.offset != offset + bytes) {
1968ee22a9d8SAlberto Garcia             continue;
1969ee22a9d8SAlberto Garcia         }
1970ee22a9d8SAlberto Garcia 
1971ee22a9d8SAlberto Garcia         /* Make sure that adding both COW regions to the QEMUIOVector
1972ee22a9d8SAlberto Garcia          * does not exceed IOV_MAX */
1973ee22a9d8SAlberto Garcia         if (hd_qiov->niov > IOV_MAX - 2) {
1974ee22a9d8SAlberto Garcia             continue;
1975ee22a9d8SAlberto Garcia         }
1976ee22a9d8SAlberto Garcia 
1977ee22a9d8SAlberto Garcia         m->data_qiov = hd_qiov;
1978ee22a9d8SAlberto Garcia         return true;
1979ee22a9d8SAlberto Garcia     }
1980ee22a9d8SAlberto Garcia 
1981ee22a9d8SAlberto Garcia     return false;
1982ee22a9d8SAlberto Garcia }
1983ee22a9d8SAlberto Garcia 
1984d46a0bb2SKevin Wolf static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
1985d46a0bb2SKevin Wolf                                          uint64_t bytes, QEMUIOVector *qiov,
1986d46a0bb2SKevin Wolf                                          int flags)
1987585f8587Sbellard {
1988ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
1989d46a0bb2SKevin Wolf     int offset_in_cluster;
199068d100e9SKevin Wolf     int ret;
1991d46a0bb2SKevin Wolf     unsigned int cur_bytes; /* number of sectors in current iteration */
1992c2bdd990SFrediano Ziglio     uint64_t cluster_offset;
19933fc48d09SFrediano Ziglio     QEMUIOVector hd_qiov;
19943fc48d09SFrediano Ziglio     uint64_t bytes_done = 0;
19953fc48d09SFrediano Ziglio     uint8_t *cluster_data = NULL;
19968d2497c3SKevin Wolf     QCowL2Meta *l2meta = NULL;
1997c2271403SFrediano Ziglio 
1998d46a0bb2SKevin Wolf     trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
19993cce16f4SKevin Wolf 
20003fc48d09SFrediano Ziglio     qemu_iovec_init(&hd_qiov, qiov->niov);
2001585f8587Sbellard 
20023fc48d09SFrediano Ziglio     s->cluster_cache_offset = -1; /* disable compressed cache */
20033fc48d09SFrediano Ziglio 
20043fc48d09SFrediano Ziglio     qemu_co_mutex_lock(&s->lock);
20053fc48d09SFrediano Ziglio 
2006d46a0bb2SKevin Wolf     while (bytes != 0) {
20073fc48d09SFrediano Ziglio 
2008f50f88b9SKevin Wolf         l2meta = NULL;
2009cf5c1a23SKevin Wolf 
20103cce16f4SKevin Wolf         trace_qcow2_writev_start_part(qemu_coroutine_self());
2011d46a0bb2SKevin Wolf         offset_in_cluster = offset_into_cluster(s, offset);
2012d46a0bb2SKevin Wolf         cur_bytes = MIN(bytes, INT_MAX);
2013d46a0bb2SKevin Wolf         if (bs->encrypted) {
2014d46a0bb2SKevin Wolf             cur_bytes = MIN(cur_bytes,
2015d46a0bb2SKevin Wolf                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2016d46a0bb2SKevin Wolf                             - offset_in_cluster);
20175ebaa27eSFrediano Ziglio         }
2018095a9c58Saliguori 
2019d46a0bb2SKevin Wolf         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2020d46a0bb2SKevin Wolf                                          &cluster_offset, &l2meta);
2021148da7eaSKevin Wolf         if (ret < 0) {
20223fc48d09SFrediano Ziglio             goto fail;
2023148da7eaSKevin Wolf         }
2024148da7eaSKevin Wolf 
2025c2bdd990SFrediano Ziglio         assert((cluster_offset & 511) == 0);
2026148da7eaSKevin Wolf 
20273fc48d09SFrediano Ziglio         qemu_iovec_reset(&hd_qiov);
2028d46a0bb2SKevin Wolf         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
20296f5f060bSKevin Wolf 
20308336aafaSDaniel P. Berrange         if (bs->encrypted) {
2031b25b387fSDaniel P. Berrange             assert(s->crypto);
20323fc48d09SFrediano Ziglio             if (!cluster_data) {
20339a4f4c31SKevin Wolf                 cluster_data = qemu_try_blockalign(bs->file->bs,
2034de82815dSKevin Wolf                                                    QCOW_MAX_CRYPT_CLUSTERS
2035de82815dSKevin Wolf                                                    * s->cluster_size);
2036de82815dSKevin Wolf                 if (cluster_data == NULL) {
2037de82815dSKevin Wolf                     ret = -ENOMEM;
2038de82815dSKevin Wolf                     goto fail;
2039de82815dSKevin Wolf                 }
2040585f8587Sbellard             }
20416f5f060bSKevin Wolf 
20423fc48d09SFrediano Ziglio             assert(hd_qiov.size <=
20435ebaa27eSFrediano Ziglio                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2044d5e6b161SMichael Tokarev             qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
20456f5f060bSKevin Wolf 
20464652b8f3SDaniel P. Berrange             if (qcrypto_block_encrypt(s->crypto,
20474652b8f3SDaniel P. Berrange                                       (s->crypt_physical_offset ?
20484652b8f3SDaniel P. Berrange                                        cluster_offset + offset_in_cluster :
20494609742aSDaniel P. Berrange                                        offset),
2050446d306dSDaniel P. Berrange                                       cluster_data,
2051c3a8fe33SAlberto Garcia                                       cur_bytes, NULL) < 0) {
2052f6fa64f6SDaniel P. Berrange                 ret = -EIO;
2053f6fa64f6SDaniel P. Berrange                 goto fail;
2054f6fa64f6SDaniel P. Berrange             }
20556f5f060bSKevin Wolf 
20563fc48d09SFrediano Ziglio             qemu_iovec_reset(&hd_qiov);
2057d46a0bb2SKevin Wolf             qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
2058585f8587Sbellard         }
20596f5f060bSKevin Wolf 
2060231bb267SMax Reitz         ret = qcow2_pre_write_overlap_check(bs, 0,
2061d46a0bb2SKevin Wolf                 cluster_offset + offset_in_cluster, cur_bytes);
2062cf93980eSMax Reitz         if (ret < 0) {
2063cf93980eSMax Reitz             goto fail;
2064cf93980eSMax Reitz         }
2065cf93980eSMax Reitz 
2066ee22a9d8SAlberto Garcia         /* If we need to do COW, check if it's possible to merge the
2067ee22a9d8SAlberto Garcia          * writing of the guest data together with that of the COW regions.
2068ee22a9d8SAlberto Garcia          * If it's not possible (or not necessary) then write the
2069ee22a9d8SAlberto Garcia          * guest data now. */
2070ee22a9d8SAlberto Garcia         if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
207168d100e9SKevin Wolf             qemu_co_mutex_unlock(&s->lock);
207267a7a0ebSKevin Wolf             BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
20733cce16f4SKevin Wolf             trace_qcow2_writev_data(qemu_coroutine_self(),
2074d46a0bb2SKevin Wolf                                     cluster_offset + offset_in_cluster);
2075a03ef88fSKevin Wolf             ret = bdrv_co_pwritev(bs->file,
2076d46a0bb2SKevin Wolf                                   cluster_offset + offset_in_cluster,
2077d46a0bb2SKevin Wolf                                   cur_bytes, &hd_qiov, 0);
207868d100e9SKevin Wolf             qemu_co_mutex_lock(&s->lock);
207968d100e9SKevin Wolf             if (ret < 0) {
20803fc48d09SFrediano Ziglio                 goto fail;
2081171e3d6bSKevin Wolf             }
2082ee22a9d8SAlberto Garcia         }
2083f141eafeSaliguori 
2084fd9fcd37SFam Zheng         ret = qcow2_handle_l2meta(bs, &l2meta, true);
2085fd9fcd37SFam Zheng         if (ret) {
20863fc48d09SFrediano Ziglio             goto fail;
2087faf575c1SFrediano Ziglio         }
2088faf575c1SFrediano Ziglio 
2089d46a0bb2SKevin Wolf         bytes -= cur_bytes;
2090d46a0bb2SKevin Wolf         offset += cur_bytes;
2091d46a0bb2SKevin Wolf         bytes_done += cur_bytes;
2092d46a0bb2SKevin Wolf         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
20935ebaa27eSFrediano Ziglio     }
20943fc48d09SFrediano Ziglio     ret = 0;
2095faf575c1SFrediano Ziglio 
20963fc48d09SFrediano Ziglio fail:
2097fd9fcd37SFam Zheng     qcow2_handle_l2meta(bs, &l2meta, false);
20980fa9131aSKevin Wolf 
2099a8c57408SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2100a8c57408SPaolo Bonzini 
21013fc48d09SFrediano Ziglio     qemu_iovec_destroy(&hd_qiov);
2102dea43a65SFrediano Ziglio     qemu_vfree(cluster_data);
21033cce16f4SKevin Wolf     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
210442496d62SKevin Wolf 
210568d100e9SKevin Wolf     return ret;
2106585f8587Sbellard }
2107585f8587Sbellard 
2108ec6d8912SKevin Wolf static int qcow2_inactivate(BlockDriverState *bs)
2109ec6d8912SKevin Wolf {
2110ec6d8912SKevin Wolf     BDRVQcow2State *s = bs->opaque;
2111ec6d8912SKevin Wolf     int ret, result = 0;
21125f72826eSVladimir Sementsov-Ogievskiy     Error *local_err = NULL;
2113ec6d8912SKevin Wolf 
211483a8c775SPavel Butsykin     qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
211583a8c775SPavel Butsykin     if (local_err != NULL) {
211683a8c775SPavel Butsykin         result = -EINVAL;
211783a8c775SPavel Butsykin         error_report_err(local_err);
211883a8c775SPavel Butsykin         error_report("Persistent bitmaps are lost for node '%s'",
211983a8c775SPavel Butsykin                      bdrv_get_device_or_node_name(bs));
212083a8c775SPavel Butsykin     }
212183a8c775SPavel Butsykin 
2122ec6d8912SKevin Wolf     ret = qcow2_cache_flush(bs, s->l2_table_cache);
2123ec6d8912SKevin Wolf     if (ret) {
2124ec6d8912SKevin Wolf         result = ret;
2125ec6d8912SKevin Wolf         error_report("Failed to flush the L2 table cache: %s",
2126ec6d8912SKevin Wolf                      strerror(-ret));
2127ec6d8912SKevin Wolf     }
2128ec6d8912SKevin Wolf 
2129ec6d8912SKevin Wolf     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2130ec6d8912SKevin Wolf     if (ret) {
2131ec6d8912SKevin Wolf         result = ret;
2132ec6d8912SKevin Wolf         error_report("Failed to flush the refcount block cache: %s",
2133ec6d8912SKevin Wolf                      strerror(-ret));
2134ec6d8912SKevin Wolf     }
2135ec6d8912SKevin Wolf 
2136ec6d8912SKevin Wolf     if (result == 0) {
2137ec6d8912SKevin Wolf         qcow2_mark_clean(bs);
2138ec6d8912SKevin Wolf     }
2139ec6d8912SKevin Wolf 
2140ec6d8912SKevin Wolf     return result;
2141ec6d8912SKevin Wolf }
2142ec6d8912SKevin Wolf 
21437c80ab3fSJes Sorensen static void qcow2_close(BlockDriverState *bs)
2144585f8587Sbellard {
2145ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2146de82815dSKevin Wolf     qemu_vfree(s->l1_table);
2147cf93980eSMax Reitz     /* else pre-write overlap checks in cache_destroy may crash */
2148cf93980eSMax Reitz     s->l1_table = NULL;
214929c1a730SKevin Wolf 
2150140fd5a6SKevin Wolf     if (!(s->flags & BDRV_O_INACTIVE)) {
2151ec6d8912SKevin Wolf         qcow2_inactivate(bs);
21523b5e14c7SMax Reitz     }
2153c61d0004SStefan Hajnoczi 
2154279621c0SAlberto Garcia     cache_clean_timer_del(bs);
2155e64d4072SAlberto Garcia     qcow2_cache_destroy(s->l2_table_cache);
2156e64d4072SAlberto Garcia     qcow2_cache_destroy(s->refcount_block_cache);
215729c1a730SKevin Wolf 
2158b25b387fSDaniel P. Berrange     qcrypto_block_free(s->crypto);
2159b25b387fSDaniel P. Berrange     s->crypto = NULL;
2160f6fa64f6SDaniel P. Berrange 
21616744cbabSKevin Wolf     g_free(s->unknown_header_fields);
216275bab85cSKevin Wolf     cleanup_unknown_header_ext(bs);
21636744cbabSKevin Wolf 
2164e4603fe1SKevin Wolf     g_free(s->image_backing_file);
2165e4603fe1SKevin Wolf     g_free(s->image_backing_format);
2166e4603fe1SKevin Wolf 
21677267c094SAnthony Liguori     g_free(s->cluster_cache);
2168dea43a65SFrediano Ziglio     qemu_vfree(s->cluster_data);
2169ed6ccf0fSKevin Wolf     qcow2_refcount_close(bs);
217028c1202bSLi Zhi Hui     qcow2_free_snapshots(bs);
2171585f8587Sbellard }
2172585f8587Sbellard 
21732b148f39SPaolo Bonzini static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
21742b148f39SPaolo Bonzini                                                    Error **errp)
217506d9260fSAnthony Liguori {
2176ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
217706d9260fSAnthony Liguori     int flags = s->flags;
2178b25b387fSDaniel P. Berrange     QCryptoBlock *crypto = NULL;
2179acdfb480SKevin Wolf     QDict *options;
21805a8a30dbSKevin Wolf     Error *local_err = NULL;
21815a8a30dbSKevin Wolf     int ret;
218206d9260fSAnthony Liguori 
218306d9260fSAnthony Liguori     /*
218406d9260fSAnthony Liguori      * Backing files are read-only which makes all of their metadata immutable,
218506d9260fSAnthony Liguori      * that means we don't have to worry about reopening them here.
218606d9260fSAnthony Liguori      */
218706d9260fSAnthony Liguori 
2188b25b387fSDaniel P. Berrange     crypto = s->crypto;
2189b25b387fSDaniel P. Berrange     s->crypto = NULL;
219006d9260fSAnthony Liguori 
219106d9260fSAnthony Liguori     qcow2_close(bs);
219206d9260fSAnthony Liguori 
2193ff99129aSKevin Wolf     memset(s, 0, sizeof(BDRVQcow2State));
2194d475e5acSKevin Wolf     options = qdict_clone_shallow(bs->options);
21955a8a30dbSKevin Wolf 
2196140fd5a6SKevin Wolf     flags &= ~BDRV_O_INACTIVE;
21972b148f39SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
21984e4bf5c4SKevin Wolf     ret = qcow2_do_open(bs, options, flags, &local_err);
21992b148f39SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
2200cb3e7f08SMarc-André Lureau     qobject_unref(options);
22015a8a30dbSKevin Wolf     if (local_err) {
2202e43bfd9cSMarkus Armbruster         error_propagate(errp, local_err);
2203e43bfd9cSMarkus Armbruster         error_prepend(errp, "Could not reopen qcow2 layer: ");
2204191fb11bSKevin Wolf         bs->drv = NULL;
22055a8a30dbSKevin Wolf         return;
22065a8a30dbSKevin Wolf     } else if (ret < 0) {
22075a8a30dbSKevin Wolf         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
2208191fb11bSKevin Wolf         bs->drv = NULL;
22095a8a30dbSKevin Wolf         return;
22105a8a30dbSKevin Wolf     }
2211acdfb480SKevin Wolf 
2212b25b387fSDaniel P. Berrange     s->crypto = crypto;
221306d9260fSAnthony Liguori }
221406d9260fSAnthony Liguori 
2215e24e49e6SKevin Wolf static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2216e24e49e6SKevin Wolf     size_t len, size_t buflen)
2217756e6736SKevin Wolf {
2218e24e49e6SKevin Wolf     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2219e24e49e6SKevin Wolf     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2220756e6736SKevin Wolf 
2221e24e49e6SKevin Wolf     if (buflen < ext_len) {
2222756e6736SKevin Wolf         return -ENOSPC;
2223756e6736SKevin Wolf     }
2224756e6736SKevin Wolf 
2225e24e49e6SKevin Wolf     *ext_backing_fmt = (QCowExtension) {
2226e24e49e6SKevin Wolf         .magic  = cpu_to_be32(magic),
2227e24e49e6SKevin Wolf         .len    = cpu_to_be32(len),
2228e24e49e6SKevin Wolf     };
22290647d47cSStefan Hajnoczi 
22300647d47cSStefan Hajnoczi     if (len) {
2231e24e49e6SKevin Wolf         memcpy(buf + sizeof(QCowExtension), s, len);
22320647d47cSStefan Hajnoczi     }
2233756e6736SKevin Wolf 
2234e24e49e6SKevin Wolf     return ext_len;
2235756e6736SKevin Wolf }
2236756e6736SKevin Wolf 
2237e24e49e6SKevin Wolf /*
2238e24e49e6SKevin Wolf  * Updates the qcow2 header, including the variable length parts of it, i.e.
2239e24e49e6SKevin Wolf  * the backing file name and all extensions. qcow2 was not designed to allow
2240e24e49e6SKevin Wolf  * such changes, so if we run out of space (we can only use the first cluster)
2241e24e49e6SKevin Wolf  * this function may fail.
2242e24e49e6SKevin Wolf  *
2243e24e49e6SKevin Wolf  * Returns 0 on success, -errno in error cases.
2244e24e49e6SKevin Wolf  */
2245e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs)
2246e24e49e6SKevin Wolf {
2247ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2248e24e49e6SKevin Wolf     QCowHeader *header;
2249e24e49e6SKevin Wolf     char *buf;
2250e24e49e6SKevin Wolf     size_t buflen = s->cluster_size;
2251e24e49e6SKevin Wolf     int ret;
2252e24e49e6SKevin Wolf     uint64_t total_size;
2253e24e49e6SKevin Wolf     uint32_t refcount_table_clusters;
22546744cbabSKevin Wolf     size_t header_length;
225575bab85cSKevin Wolf     Qcow2UnknownHeaderExtension *uext;
2256e24e49e6SKevin Wolf 
2257e24e49e6SKevin Wolf     buf = qemu_blockalign(bs, buflen);
2258e24e49e6SKevin Wolf 
2259e24e49e6SKevin Wolf     /* Header structure */
2260e24e49e6SKevin Wolf     header = (QCowHeader*) buf;
2261e24e49e6SKevin Wolf 
2262e24e49e6SKevin Wolf     if (buflen < sizeof(*header)) {
2263e24e49e6SKevin Wolf         ret = -ENOSPC;
2264e24e49e6SKevin Wolf         goto fail;
2265756e6736SKevin Wolf     }
2266756e6736SKevin Wolf 
22676744cbabSKevin Wolf     header_length = sizeof(*header) + s->unknown_header_fields_size;
2268e24e49e6SKevin Wolf     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2269e24e49e6SKevin Wolf     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2270e24e49e6SKevin Wolf 
2271e24e49e6SKevin Wolf     *header = (QCowHeader) {
22726744cbabSKevin Wolf         /* Version 2 fields */
2273e24e49e6SKevin Wolf         .magic                  = cpu_to_be32(QCOW_MAGIC),
22746744cbabSKevin Wolf         .version                = cpu_to_be32(s->qcow_version),
2275e24e49e6SKevin Wolf         .backing_file_offset    = 0,
2276e24e49e6SKevin Wolf         .backing_file_size      = 0,
2277e24e49e6SKevin Wolf         .cluster_bits           = cpu_to_be32(s->cluster_bits),
2278e24e49e6SKevin Wolf         .size                   = cpu_to_be64(total_size),
2279e24e49e6SKevin Wolf         .crypt_method           = cpu_to_be32(s->crypt_method_header),
2280e24e49e6SKevin Wolf         .l1_size                = cpu_to_be32(s->l1_size),
2281e24e49e6SKevin Wolf         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
2282e24e49e6SKevin Wolf         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
2283e24e49e6SKevin Wolf         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2284e24e49e6SKevin Wolf         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
2285e24e49e6SKevin Wolf         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
22866744cbabSKevin Wolf 
22876744cbabSKevin Wolf         /* Version 3 fields */
22886744cbabSKevin Wolf         .incompatible_features  = cpu_to_be64(s->incompatible_features),
22896744cbabSKevin Wolf         .compatible_features    = cpu_to_be64(s->compatible_features),
22906744cbabSKevin Wolf         .autoclear_features     = cpu_to_be64(s->autoclear_features),
2291b6481f37SMax Reitz         .refcount_order         = cpu_to_be32(s->refcount_order),
22926744cbabSKevin Wolf         .header_length          = cpu_to_be32(header_length),
2293e24e49e6SKevin Wolf     };
2294e24e49e6SKevin Wolf 
22956744cbabSKevin Wolf     /* For older versions, write a shorter header */
22966744cbabSKevin Wolf     switch (s->qcow_version) {
22976744cbabSKevin Wolf     case 2:
22986744cbabSKevin Wolf         ret = offsetof(QCowHeader, incompatible_features);
22996744cbabSKevin Wolf         break;
23006744cbabSKevin Wolf     case 3:
23016744cbabSKevin Wolf         ret = sizeof(*header);
23026744cbabSKevin Wolf         break;
23036744cbabSKevin Wolf     default:
2304b6c14762SJim Meyering         ret = -EINVAL;
2305b6c14762SJim Meyering         goto fail;
23066744cbabSKevin Wolf     }
23076744cbabSKevin Wolf 
23086744cbabSKevin Wolf     buf += ret;
23096744cbabSKevin Wolf     buflen -= ret;
23106744cbabSKevin Wolf     memset(buf, 0, buflen);
23116744cbabSKevin Wolf 
23126744cbabSKevin Wolf     /* Preserve any unknown field in the header */
23136744cbabSKevin Wolf     if (s->unknown_header_fields_size) {
23146744cbabSKevin Wolf         if (buflen < s->unknown_header_fields_size) {
23156744cbabSKevin Wolf             ret = -ENOSPC;
23166744cbabSKevin Wolf             goto fail;
23176744cbabSKevin Wolf         }
23186744cbabSKevin Wolf 
23196744cbabSKevin Wolf         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
23206744cbabSKevin Wolf         buf += s->unknown_header_fields_size;
23216744cbabSKevin Wolf         buflen -= s->unknown_header_fields_size;
23226744cbabSKevin Wolf     }
2323e24e49e6SKevin Wolf 
2324e24e49e6SKevin Wolf     /* Backing file format header extension */
2325e4603fe1SKevin Wolf     if (s->image_backing_format) {
2326e24e49e6SKevin Wolf         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
2327e4603fe1SKevin Wolf                              s->image_backing_format,
2328e4603fe1SKevin Wolf                              strlen(s->image_backing_format),
2329e24e49e6SKevin Wolf                              buflen);
2330756e6736SKevin Wolf         if (ret < 0) {
2331756e6736SKevin Wolf             goto fail;
2332756e6736SKevin Wolf         }
2333756e6736SKevin Wolf 
2334e24e49e6SKevin Wolf         buf += ret;
2335e24e49e6SKevin Wolf         buflen -= ret;
2336e24e49e6SKevin Wolf     }
2337756e6736SKevin Wolf 
23384652b8f3SDaniel P. Berrange     /* Full disk encryption header pointer extension */
23394652b8f3SDaniel P. Berrange     if (s->crypto_header.offset != 0) {
23404652b8f3SDaniel P. Berrange         cpu_to_be64s(&s->crypto_header.offset);
23414652b8f3SDaniel P. Berrange         cpu_to_be64s(&s->crypto_header.length);
23424652b8f3SDaniel P. Berrange         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
23434652b8f3SDaniel P. Berrange                              &s->crypto_header, sizeof(s->crypto_header),
23444652b8f3SDaniel P. Berrange                              buflen);
23454652b8f3SDaniel P. Berrange         be64_to_cpus(&s->crypto_header.offset);
23464652b8f3SDaniel P. Berrange         be64_to_cpus(&s->crypto_header.length);
23474652b8f3SDaniel P. Berrange         if (ret < 0) {
23484652b8f3SDaniel P. Berrange             goto fail;
23494652b8f3SDaniel P. Berrange         }
23504652b8f3SDaniel P. Berrange         buf += ret;
23514652b8f3SDaniel P. Berrange         buflen -= ret;
23524652b8f3SDaniel P. Berrange     }
23534652b8f3SDaniel P. Berrange 
2354cfcc4c62SKevin Wolf     /* Feature table */
23551a4828c7SKevin Wolf     if (s->qcow_version >= 3) {
2356cfcc4c62SKevin Wolf         Qcow2Feature features[] = {
2357c61d0004SStefan Hajnoczi             {
2358c61d0004SStefan Hajnoczi                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2359c61d0004SStefan Hajnoczi                 .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
2360c61d0004SStefan Hajnoczi                 .name = "dirty bit",
2361c61d0004SStefan Hajnoczi             },
2362bfe8043eSStefan Hajnoczi             {
236369c98726SMax Reitz                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
236469c98726SMax Reitz                 .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
236569c98726SMax Reitz                 .name = "corrupt bit",
236669c98726SMax Reitz             },
236769c98726SMax Reitz             {
2368bfe8043eSStefan Hajnoczi                 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2369bfe8043eSStefan Hajnoczi                 .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2370bfe8043eSStefan Hajnoczi                 .name = "lazy refcounts",
2371bfe8043eSStefan Hajnoczi             },
2372cfcc4c62SKevin Wolf         };
2373cfcc4c62SKevin Wolf 
2374cfcc4c62SKevin Wolf         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2375cfcc4c62SKevin Wolf                              features, sizeof(features), buflen);
2376cfcc4c62SKevin Wolf         if (ret < 0) {
2377cfcc4c62SKevin Wolf             goto fail;
2378cfcc4c62SKevin Wolf         }
2379cfcc4c62SKevin Wolf         buf += ret;
2380cfcc4c62SKevin Wolf         buflen -= ret;
23811a4828c7SKevin Wolf     }
2382cfcc4c62SKevin Wolf 
238388ddffaeSVladimir Sementsov-Ogievskiy     /* Bitmap extension */
238488ddffaeSVladimir Sementsov-Ogievskiy     if (s->nb_bitmaps > 0) {
238588ddffaeSVladimir Sementsov-Ogievskiy         Qcow2BitmapHeaderExt bitmaps_header = {
238688ddffaeSVladimir Sementsov-Ogievskiy             .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
238788ddffaeSVladimir Sementsov-Ogievskiy             .bitmap_directory_size =
238888ddffaeSVladimir Sementsov-Ogievskiy                     cpu_to_be64(s->bitmap_directory_size),
238988ddffaeSVladimir Sementsov-Ogievskiy             .bitmap_directory_offset =
239088ddffaeSVladimir Sementsov-Ogievskiy                     cpu_to_be64(s->bitmap_directory_offset)
239188ddffaeSVladimir Sementsov-Ogievskiy         };
239288ddffaeSVladimir Sementsov-Ogievskiy         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
239388ddffaeSVladimir Sementsov-Ogievskiy                              &bitmaps_header, sizeof(bitmaps_header),
239488ddffaeSVladimir Sementsov-Ogievskiy                              buflen);
239588ddffaeSVladimir Sementsov-Ogievskiy         if (ret < 0) {
239688ddffaeSVladimir Sementsov-Ogievskiy             goto fail;
239788ddffaeSVladimir Sementsov-Ogievskiy         }
239888ddffaeSVladimir Sementsov-Ogievskiy         buf += ret;
239988ddffaeSVladimir Sementsov-Ogievskiy         buflen -= ret;
240088ddffaeSVladimir Sementsov-Ogievskiy     }
240188ddffaeSVladimir Sementsov-Ogievskiy 
240275bab85cSKevin Wolf     /* Keep unknown header extensions */
240375bab85cSKevin Wolf     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
240475bab85cSKevin Wolf         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
240575bab85cSKevin Wolf         if (ret < 0) {
240675bab85cSKevin Wolf             goto fail;
240775bab85cSKevin Wolf         }
240875bab85cSKevin Wolf 
240975bab85cSKevin Wolf         buf += ret;
241075bab85cSKevin Wolf         buflen -= ret;
241175bab85cSKevin Wolf     }
241275bab85cSKevin Wolf 
2413e24e49e6SKevin Wolf     /* End of header extensions */
2414e24e49e6SKevin Wolf     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
2415756e6736SKevin Wolf     if (ret < 0) {
2416756e6736SKevin Wolf         goto fail;
2417756e6736SKevin Wolf     }
2418756e6736SKevin Wolf 
2419e24e49e6SKevin Wolf     buf += ret;
2420e24e49e6SKevin Wolf     buflen -= ret;
2421e24e49e6SKevin Wolf 
2422e24e49e6SKevin Wolf     /* Backing file name */
2423e4603fe1SKevin Wolf     if (s->image_backing_file) {
2424e4603fe1SKevin Wolf         size_t backing_file_len = strlen(s->image_backing_file);
2425e24e49e6SKevin Wolf 
2426e24e49e6SKevin Wolf         if (buflen < backing_file_len) {
2427e24e49e6SKevin Wolf             ret = -ENOSPC;
2428e24e49e6SKevin Wolf             goto fail;
2429e24e49e6SKevin Wolf         }
2430e24e49e6SKevin Wolf 
243100ea1881SJim Meyering         /* Using strncpy is ok here, since buf is not NUL-terminated. */
2432e4603fe1SKevin Wolf         strncpy(buf, s->image_backing_file, buflen);
2433e24e49e6SKevin Wolf 
2434e24e49e6SKevin Wolf         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
2435e24e49e6SKevin Wolf         header->backing_file_size   = cpu_to_be32(backing_file_len);
2436e24e49e6SKevin Wolf     }
2437e24e49e6SKevin Wolf 
2438e24e49e6SKevin Wolf     /* Write the new header */
2439d9ca2ea2SKevin Wolf     ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
2440756e6736SKevin Wolf     if (ret < 0) {
2441756e6736SKevin Wolf         goto fail;
2442756e6736SKevin Wolf     }
2443756e6736SKevin Wolf 
2444756e6736SKevin Wolf     ret = 0;
2445756e6736SKevin Wolf fail:
2446e24e49e6SKevin Wolf     qemu_vfree(header);
2447756e6736SKevin Wolf     return ret;
2448756e6736SKevin Wolf }
2449756e6736SKevin Wolf 
2450756e6736SKevin Wolf static int qcow2_change_backing_file(BlockDriverState *bs,
2451756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
2452756e6736SKevin Wolf {
2453ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
2454e4603fe1SKevin Wolf 
24554e876bcfSMax Reitz     if (backing_file && strlen(backing_file) > 1023) {
24564e876bcfSMax Reitz         return -EINVAL;
24574e876bcfSMax Reitz     }
24584e876bcfSMax Reitz 
2459e24e49e6SKevin Wolf     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2460e24e49e6SKevin Wolf     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2461e24e49e6SKevin Wolf 
2462e4603fe1SKevin Wolf     g_free(s->image_backing_file);
2463e4603fe1SKevin Wolf     g_free(s->image_backing_format);
2464e4603fe1SKevin Wolf 
2465e4603fe1SKevin Wolf     s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2466e4603fe1SKevin Wolf     s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2467e4603fe1SKevin Wolf 
2468e24e49e6SKevin Wolf     return qcow2_update_header(bs);
2469756e6736SKevin Wolf }
2470756e6736SKevin Wolf 
24714652b8f3SDaniel P. Berrange static int qcow2_crypt_method_from_format(const char *encryptfmt)
24724652b8f3SDaniel P. Berrange {
24734652b8f3SDaniel P. Berrange     if (g_str_equal(encryptfmt, "luks")) {
24744652b8f3SDaniel P. Berrange         return QCOW_CRYPT_LUKS;
24754652b8f3SDaniel P. Berrange     } else if (g_str_equal(encryptfmt, "aes")) {
24764652b8f3SDaniel P. Berrange         return QCOW_CRYPT_AES;
24774652b8f3SDaniel P. Berrange     } else {
24784652b8f3SDaniel P. Berrange         return -EINVAL;
24794652b8f3SDaniel P. Berrange     }
24804652b8f3SDaniel P. Berrange }
2481b25b387fSDaniel P. Berrange 
248260900b7bSKevin Wolf static int qcow2_set_up_encryption(BlockDriverState *bs,
248360900b7bSKevin Wolf                                    QCryptoBlockCreateOptions *cryptoopts,
248460900b7bSKevin Wolf                                    Error **errp)
248560900b7bSKevin Wolf {
248660900b7bSKevin Wolf     BDRVQcow2State *s = bs->opaque;
248760900b7bSKevin Wolf     QCryptoBlock *crypto = NULL;
248860900b7bSKevin Wolf     int fmt, ret;
248960900b7bSKevin Wolf 
249060900b7bSKevin Wolf     switch (cryptoopts->format) {
249160900b7bSKevin Wolf     case Q_CRYPTO_BLOCK_FORMAT_LUKS:
249260900b7bSKevin Wolf         fmt = QCOW_CRYPT_LUKS;
249360900b7bSKevin Wolf         break;
249460900b7bSKevin Wolf     case Q_CRYPTO_BLOCK_FORMAT_QCOW:
249560900b7bSKevin Wolf         fmt = QCOW_CRYPT_AES;
249660900b7bSKevin Wolf         break;
249760900b7bSKevin Wolf     default:
249860900b7bSKevin Wolf         error_setg(errp, "Crypto format not supported in qcow2");
249960900b7bSKevin Wolf         return -EINVAL;
250060900b7bSKevin Wolf     }
250160900b7bSKevin Wolf 
25024652b8f3SDaniel P. Berrange     s->crypt_method_header = fmt;
2503b25b387fSDaniel P. Berrange 
25041cd9a787SDaniel P. Berrange     crypto = qcrypto_block_create(cryptoopts, "encrypt.",
25054652b8f3SDaniel P. Berrange                                   qcow2_crypto_hdr_init_func,
25064652b8f3SDaniel P. Berrange                                   qcow2_crypto_hdr_write_func,
2507b25b387fSDaniel P. Berrange                                   bs, errp);
2508b25b387fSDaniel P. Berrange     if (!crypto) {
250960900b7bSKevin Wolf         return -EINVAL;
2510b25b387fSDaniel P. Berrange     }
2511b25b387fSDaniel P. Berrange 
2512b25b387fSDaniel P. Berrange     ret = qcow2_update_header(bs);
2513b25b387fSDaniel P. Berrange     if (ret < 0) {
2514b25b387fSDaniel P. Berrange         error_setg_errno(errp, -ret, "Could not write encryption header");
2515b25b387fSDaniel P. Berrange         goto out;
2516b25b387fSDaniel P. Berrange     }
2517b25b387fSDaniel P. Berrange 
251860900b7bSKevin Wolf     ret = 0;
2519b25b387fSDaniel P. Berrange  out:
2520b25b387fSDaniel P. Berrange     qcrypto_block_free(crypto);
2521b25b387fSDaniel P. Berrange     return ret;
2522b25b387fSDaniel P. Berrange }
2523b25b387fSDaniel P. Berrange 
25247bc45dc1SMax Reitz /**
25257bc45dc1SMax Reitz  * Preallocates metadata structures for data clusters between @offset (in the
25267bc45dc1SMax Reitz  * guest disk) and @new_length (which is thus generally the new guest disk
25277bc45dc1SMax Reitz  * size).
25287bc45dc1SMax Reitz  *
25297bc45dc1SMax Reitz  * Returns: 0 on success, -errno on failure.
25307bc45dc1SMax Reitz  */
253147e86b86SKevin Wolf static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset,
253247e86b86SKevin Wolf                                        uint64_t new_length)
2533a35e1c17SKevin Wolf {
2534d46a0bb2SKevin Wolf     uint64_t bytes;
2535060bee89SKevin Wolf     uint64_t host_offset = 0;
2536d46a0bb2SKevin Wolf     unsigned int cur_bytes;
2537148da7eaSKevin Wolf     int ret;
2538f50f88b9SKevin Wolf     QCowL2Meta *meta;
2539a35e1c17SKevin Wolf 
25407bc45dc1SMax Reitz     assert(offset <= new_length);
25417bc45dc1SMax Reitz     bytes = new_length - offset;
2542a35e1c17SKevin Wolf 
2543d46a0bb2SKevin Wolf     while (bytes) {
2544d46a0bb2SKevin Wolf         cur_bytes = MIN(bytes, INT_MAX);
2545d46a0bb2SKevin Wolf         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2546060bee89SKevin Wolf                                          &host_offset, &meta);
2547148da7eaSKevin Wolf         if (ret < 0) {
254847e86b86SKevin Wolf             return ret;
2549a35e1c17SKevin Wolf         }
2550a35e1c17SKevin Wolf 
2551c792707fSStefan Hajnoczi         while (meta) {
2552c792707fSStefan Hajnoczi             QCowL2Meta *next = meta->next;
2553c792707fSStefan Hajnoczi 
2554f50f88b9SKevin Wolf             ret = qcow2_alloc_cluster_link_l2(bs, meta);
255519dbcbf7SKevin Wolf             if (ret < 0) {
25567c2bbf4aSHu Tao                 qcow2_free_any_clusters(bs, meta->alloc_offset,
25577c2bbf4aSHu Tao                                         meta->nb_clusters, QCOW2_DISCARD_NEVER);
255847e86b86SKevin Wolf                 return ret;
2559a35e1c17SKevin Wolf             }
2560a35e1c17SKevin Wolf 
25617c2bbf4aSHu Tao             /* There are no dependent requests, but we need to remove our
25627c2bbf4aSHu Tao              * request from the list of in-flight requests */
25634e95314eSKevin Wolf             QLIST_REMOVE(meta, next_in_flight);
2564c792707fSStefan Hajnoczi 
2565c792707fSStefan Hajnoczi             g_free(meta);
2566c792707fSStefan Hajnoczi             meta = next;
2567f50f88b9SKevin Wolf         }
2568f214978aSKevin Wolf 
2569a35e1c17SKevin Wolf         /* TODO Preallocate data if requested */
2570a35e1c17SKevin Wolf 
2571d46a0bb2SKevin Wolf         bytes -= cur_bytes;
2572d46a0bb2SKevin Wolf         offset += cur_bytes;
2573a35e1c17SKevin Wolf     }
2574a35e1c17SKevin Wolf 
2575a35e1c17SKevin Wolf     /*
2576a35e1c17SKevin Wolf      * It is expected that the image file is large enough to actually contain
2577a35e1c17SKevin Wolf      * all of the allocated clusters (otherwise we get failing reads after
2578a35e1c17SKevin Wolf      * EOF). Extend the image to the last allocated sector.
2579a35e1c17SKevin Wolf      */
2580060bee89SKevin Wolf     if (host_offset != 0) {
2581d46a0bb2SKevin Wolf         uint8_t data = 0;
2582d9ca2ea2SKevin Wolf         ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
2583d46a0bb2SKevin Wolf                           &data, 1);
258419dbcbf7SKevin Wolf         if (ret < 0) {
258547e86b86SKevin Wolf             return ret;
258619dbcbf7SKevin Wolf         }
2587a35e1c17SKevin Wolf     }
2588a35e1c17SKevin Wolf 
258947e86b86SKevin Wolf     return 0;
2590a35e1c17SKevin Wolf }
2591a35e1c17SKevin Wolf 
25927c5bcc42SStefan Hajnoczi /* qcow2_refcount_metadata_size:
25937c5bcc42SStefan Hajnoczi  * @clusters: number of clusters to refcount (including data and L1/L2 tables)
25947c5bcc42SStefan Hajnoczi  * @cluster_size: size of a cluster, in bytes
25957c5bcc42SStefan Hajnoczi  * @refcount_order: refcount bits power-of-2 exponent
259612cc30a8SMax Reitz  * @generous_increase: allow for the refcount table to be 1.5x as large as it
259712cc30a8SMax Reitz  *                     needs to be
25987c5bcc42SStefan Hajnoczi  *
25997c5bcc42SStefan Hajnoczi  * Returns: Number of bytes required for refcount blocks and table metadata.
26007c5bcc42SStefan Hajnoczi  */
260112cc30a8SMax Reitz int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
260212cc30a8SMax Reitz                                      int refcount_order, bool generous_increase,
260312cc30a8SMax Reitz                                      uint64_t *refblock_count)
26047c5bcc42SStefan Hajnoczi {
26057c5bcc42SStefan Hajnoczi     /*
26067c5bcc42SStefan Hajnoczi      * Every host cluster is reference-counted, including metadata (even
26077c5bcc42SStefan Hajnoczi      * refcount metadata is recursively included).
26087c5bcc42SStefan Hajnoczi      *
26097c5bcc42SStefan Hajnoczi      * An accurate formula for the size of refcount metadata size is difficult
26107c5bcc42SStefan Hajnoczi      * to derive.  An easier method of calculation is finding the fixed point
26117c5bcc42SStefan Hajnoczi      * where no further refcount blocks or table clusters are required to
26127c5bcc42SStefan Hajnoczi      * reference count every cluster.
26137c5bcc42SStefan Hajnoczi      */
26147c5bcc42SStefan Hajnoczi     int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
26157c5bcc42SStefan Hajnoczi     int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
26167c5bcc42SStefan Hajnoczi     int64_t table = 0;  /* number of refcount table clusters */
26177c5bcc42SStefan Hajnoczi     int64_t blocks = 0; /* number of refcount block clusters */
26187c5bcc42SStefan Hajnoczi     int64_t last;
26197c5bcc42SStefan Hajnoczi     int64_t n = 0;
26207c5bcc42SStefan Hajnoczi 
26217c5bcc42SStefan Hajnoczi     do {
26227c5bcc42SStefan Hajnoczi         last = n;
26237c5bcc42SStefan Hajnoczi         blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
26247c5bcc42SStefan Hajnoczi         table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
26257c5bcc42SStefan Hajnoczi         n = clusters + blocks + table;
262612cc30a8SMax Reitz 
262712cc30a8SMax Reitz         if (n == last && generous_increase) {
262812cc30a8SMax Reitz             clusters += DIV_ROUND_UP(table, 2);
262912cc30a8SMax Reitz             n = 0; /* force another loop */
263012cc30a8SMax Reitz             generous_increase = false;
263112cc30a8SMax Reitz         }
26327c5bcc42SStefan Hajnoczi     } while (n != last);
26337c5bcc42SStefan Hajnoczi 
263412cc30a8SMax Reitz     if (refblock_count) {
263512cc30a8SMax Reitz         *refblock_count = blocks;
263612cc30a8SMax Reitz     }
263712cc30a8SMax Reitz 
26387c5bcc42SStefan Hajnoczi     return (blocks + table) * cluster_size;
26397c5bcc42SStefan Hajnoczi }
26407c5bcc42SStefan Hajnoczi 
264195c67e3bSStefan Hajnoczi /**
264295c67e3bSStefan Hajnoczi  * qcow2_calc_prealloc_size:
264395c67e3bSStefan Hajnoczi  * @total_size: virtual disk size in bytes
264495c67e3bSStefan Hajnoczi  * @cluster_size: cluster size in bytes
264595c67e3bSStefan Hajnoczi  * @refcount_order: refcount bits power-of-2 exponent
2646a9420734SKevin Wolf  *
264795c67e3bSStefan Hajnoczi  * Returns: Total number of bytes required for the fully allocated image
264895c67e3bSStefan Hajnoczi  * (including metadata).
2649a9420734SKevin Wolf  */
265095c67e3bSStefan Hajnoczi static int64_t qcow2_calc_prealloc_size(int64_t total_size,
265195c67e3bSStefan Hajnoczi                                         size_t cluster_size,
265295c67e3bSStefan Hajnoczi                                         int refcount_order)
265395c67e3bSStefan Hajnoczi {
26540e4271b7SHu Tao     int64_t meta_size = 0;
26557c5bcc42SStefan Hajnoczi     uint64_t nl1e, nl2e;
26569e029689SAlberto Garcia     int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
26570e4271b7SHu Tao 
26580e4271b7SHu Tao     /* header: 1 cluster */
26590e4271b7SHu Tao     meta_size += cluster_size;
26600e4271b7SHu Tao 
26610e4271b7SHu Tao     /* total size of L2 tables */
26620e4271b7SHu Tao     nl2e = aligned_total_size / cluster_size;
26639e029689SAlberto Garcia     nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t));
26640e4271b7SHu Tao     meta_size += nl2e * sizeof(uint64_t);
26650e4271b7SHu Tao 
26660e4271b7SHu Tao     /* total size of L1 tables */
26670e4271b7SHu Tao     nl1e = nl2e * sizeof(uint64_t) / cluster_size;
26689e029689SAlberto Garcia     nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t));
26690e4271b7SHu Tao     meta_size += nl1e * sizeof(uint64_t);
26700e4271b7SHu Tao 
26717c5bcc42SStefan Hajnoczi     /* total size of refcount table and blocks */
26727c5bcc42SStefan Hajnoczi     meta_size += qcow2_refcount_metadata_size(
26737c5bcc42SStefan Hajnoczi             (meta_size + aligned_total_size) / cluster_size,
267412cc30a8SMax Reitz             cluster_size, refcount_order, false, NULL);
26750e4271b7SHu Tao 
267695c67e3bSStefan Hajnoczi     return meta_size + aligned_total_size;
267795c67e3bSStefan Hajnoczi }
267895c67e3bSStefan Hajnoczi 
267929ca9e45SKevin Wolf static bool validate_cluster_size(size_t cluster_size, Error **errp)
268095c67e3bSStefan Hajnoczi {
268129ca9e45SKevin Wolf     int cluster_bits = ctz32(cluster_size);
268295c67e3bSStefan Hajnoczi     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
268395c67e3bSStefan Hajnoczi         (1 << cluster_bits) != cluster_size)
268495c67e3bSStefan Hajnoczi     {
268595c67e3bSStefan Hajnoczi         error_setg(errp, "Cluster size must be a power of two between %d and "
268695c67e3bSStefan Hajnoczi                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
268729ca9e45SKevin Wolf         return false;
268829ca9e45SKevin Wolf     }
268929ca9e45SKevin Wolf     return true;
269029ca9e45SKevin Wolf }
269129ca9e45SKevin Wolf 
269229ca9e45SKevin Wolf static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
269329ca9e45SKevin Wolf {
269429ca9e45SKevin Wolf     size_t cluster_size;
269529ca9e45SKevin Wolf 
269629ca9e45SKevin Wolf     cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
269729ca9e45SKevin Wolf                                          DEFAULT_CLUSTER_SIZE);
269829ca9e45SKevin Wolf     if (!validate_cluster_size(cluster_size, errp)) {
26990eb4a8c1SStefan Hajnoczi         return 0;
270095c67e3bSStefan Hajnoczi     }
27010eb4a8c1SStefan Hajnoczi     return cluster_size;
27020eb4a8c1SStefan Hajnoczi }
27030eb4a8c1SStefan Hajnoczi 
27040eb4a8c1SStefan Hajnoczi static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
27050eb4a8c1SStefan Hajnoczi {
27060eb4a8c1SStefan Hajnoczi     char *buf;
27070eb4a8c1SStefan Hajnoczi     int ret;
27080eb4a8c1SStefan Hajnoczi 
27090eb4a8c1SStefan Hajnoczi     buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
27100eb4a8c1SStefan Hajnoczi     if (!buf) {
27110eb4a8c1SStefan Hajnoczi         ret = 3; /* default */
27120eb4a8c1SStefan Hajnoczi     } else if (!strcmp(buf, "0.10")) {
27130eb4a8c1SStefan Hajnoczi         ret = 2;
27140eb4a8c1SStefan Hajnoczi     } else if (!strcmp(buf, "1.1")) {
27150eb4a8c1SStefan Hajnoczi         ret = 3;
27160eb4a8c1SStefan Hajnoczi     } else {
27170eb4a8c1SStefan Hajnoczi         error_setg(errp, "Invalid compatibility level: '%s'", buf);
27180eb4a8c1SStefan Hajnoczi         ret = -EINVAL;
27190eb4a8c1SStefan Hajnoczi     }
27200eb4a8c1SStefan Hajnoczi     g_free(buf);
27210eb4a8c1SStefan Hajnoczi     return ret;
27220eb4a8c1SStefan Hajnoczi }
27230eb4a8c1SStefan Hajnoczi 
27240eb4a8c1SStefan Hajnoczi static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
27250eb4a8c1SStefan Hajnoczi                                                 Error **errp)
27260eb4a8c1SStefan Hajnoczi {
27270eb4a8c1SStefan Hajnoczi     uint64_t refcount_bits;
27280eb4a8c1SStefan Hajnoczi 
27290eb4a8c1SStefan Hajnoczi     refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
27300eb4a8c1SStefan Hajnoczi     if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
27310eb4a8c1SStefan Hajnoczi         error_setg(errp, "Refcount width must be a power of two and may not "
27320eb4a8c1SStefan Hajnoczi                    "exceed 64 bits");
27330eb4a8c1SStefan Hajnoczi         return 0;
27340eb4a8c1SStefan Hajnoczi     }
27350eb4a8c1SStefan Hajnoczi 
27360eb4a8c1SStefan Hajnoczi     if (version < 3 && refcount_bits != 16) {
27370eb4a8c1SStefan Hajnoczi         error_setg(errp, "Different refcount widths than 16 bits require "
27380eb4a8c1SStefan Hajnoczi                    "compatibility level 1.1 or above (use compat=1.1 or "
27390eb4a8c1SStefan Hajnoczi                    "greater)");
27400eb4a8c1SStefan Hajnoczi         return 0;
27410eb4a8c1SStefan Hajnoczi     }
27420eb4a8c1SStefan Hajnoczi 
27430eb4a8c1SStefan Hajnoczi     return refcount_bits;
27440eb4a8c1SStefan Hajnoczi }
27450eb4a8c1SStefan Hajnoczi 
2746c274393aSStefan Hajnoczi static int coroutine_fn
274760900b7bSKevin Wolf qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
27480eb4a8c1SStefan Hajnoczi {
274929ca9e45SKevin Wolf     BlockdevCreateOptionsQcow2 *qcow2_opts;
27500eb4a8c1SStefan Hajnoczi     QDict *options;
275195c67e3bSStefan Hajnoczi 
275295c67e3bSStefan Hajnoczi     /*
275395c67e3bSStefan Hajnoczi      * Open the image file and write a minimal qcow2 header.
275495c67e3bSStefan Hajnoczi      *
275595c67e3bSStefan Hajnoczi      * We keep things simple and start with a zero-sized image. We also
275695c67e3bSStefan Hajnoczi      * do without refcount blocks or a L1 table for now. We'll fix the
275795c67e3bSStefan Hajnoczi      * inconsistency later.
275895c67e3bSStefan Hajnoczi      *
275995c67e3bSStefan Hajnoczi      * We do need a refcount table because growing the refcount table means
276095c67e3bSStefan Hajnoczi      * allocating two new refcount blocks - the seconds of which would be at
276195c67e3bSStefan Hajnoczi      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
276295c67e3bSStefan Hajnoczi      * size for any qcow2 image.
276395c67e3bSStefan Hajnoczi      */
2764e1d74bc6SKevin Wolf     BlockBackend *blk = NULL;
2765e1d74bc6SKevin Wolf     BlockDriverState *bs = NULL;
276695c67e3bSStefan Hajnoczi     QCowHeader *header;
276729ca9e45SKevin Wolf     size_t cluster_size;
276829ca9e45SKevin Wolf     int version;
276929ca9e45SKevin Wolf     int refcount_order;
277095c67e3bSStefan Hajnoczi     uint64_t* refcount_table;
277195c67e3bSStefan Hajnoczi     Error *local_err = NULL;
277295c67e3bSStefan Hajnoczi     int ret;
277395c67e3bSStefan Hajnoczi 
277429ca9e45SKevin Wolf     assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
277529ca9e45SKevin Wolf     qcow2_opts = &create_options->u.qcow2;
277629ca9e45SKevin Wolf 
2777e1d74bc6SKevin Wolf     bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp);
2778e1d74bc6SKevin Wolf     if (bs == NULL) {
2779e1d74bc6SKevin Wolf         return -EIO;
2780e1d74bc6SKevin Wolf     }
2781e1d74bc6SKevin Wolf 
2782e1d74bc6SKevin Wolf     /* Validate options and set default values */
278329ca9e45SKevin Wolf     if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
278429ca9e45SKevin Wolf         error_setg(errp, "Image size must be a multiple of 512 bytes");
278529ca9e45SKevin Wolf         ret = -EINVAL;
278629ca9e45SKevin Wolf         goto out;
278729ca9e45SKevin Wolf     }
278829ca9e45SKevin Wolf 
278929ca9e45SKevin Wolf     if (qcow2_opts->has_version) {
279029ca9e45SKevin Wolf         switch (qcow2_opts->version) {
279129ca9e45SKevin Wolf         case BLOCKDEV_QCOW2_VERSION_V2:
279229ca9e45SKevin Wolf             version = 2;
279329ca9e45SKevin Wolf             break;
279429ca9e45SKevin Wolf         case BLOCKDEV_QCOW2_VERSION_V3:
279529ca9e45SKevin Wolf             version = 3;
279629ca9e45SKevin Wolf             break;
279729ca9e45SKevin Wolf         default:
279829ca9e45SKevin Wolf             g_assert_not_reached();
279929ca9e45SKevin Wolf         }
280029ca9e45SKevin Wolf     } else {
280129ca9e45SKevin Wolf         version = 3;
280229ca9e45SKevin Wolf     }
280329ca9e45SKevin Wolf 
280429ca9e45SKevin Wolf     if (qcow2_opts->has_cluster_size) {
280529ca9e45SKevin Wolf         cluster_size = qcow2_opts->cluster_size;
280629ca9e45SKevin Wolf     } else {
280729ca9e45SKevin Wolf         cluster_size = DEFAULT_CLUSTER_SIZE;
280829ca9e45SKevin Wolf     }
280929ca9e45SKevin Wolf 
281029ca9e45SKevin Wolf     if (!validate_cluster_size(cluster_size, errp)) {
2811e1d74bc6SKevin Wolf         ret = -EINVAL;
2812e1d74bc6SKevin Wolf         goto out;
281329ca9e45SKevin Wolf     }
281429ca9e45SKevin Wolf 
281529ca9e45SKevin Wolf     if (!qcow2_opts->has_preallocation) {
281629ca9e45SKevin Wolf         qcow2_opts->preallocation = PREALLOC_MODE_OFF;
281729ca9e45SKevin Wolf     }
281829ca9e45SKevin Wolf     if (qcow2_opts->has_backing_file &&
281929ca9e45SKevin Wolf         qcow2_opts->preallocation != PREALLOC_MODE_OFF)
282029ca9e45SKevin Wolf     {
282129ca9e45SKevin Wolf         error_setg(errp, "Backing file and preallocation cannot be used at "
282229ca9e45SKevin Wolf                    "the same time");
2823e1d74bc6SKevin Wolf         ret = -EINVAL;
2824e1d74bc6SKevin Wolf         goto out;
282529ca9e45SKevin Wolf     }
282629ca9e45SKevin Wolf     if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) {
282729ca9e45SKevin Wolf         error_setg(errp, "Backing format cannot be used without backing file");
2828e1d74bc6SKevin Wolf         ret = -EINVAL;
2829e1d74bc6SKevin Wolf         goto out;
283029ca9e45SKevin Wolf     }
283129ca9e45SKevin Wolf 
283229ca9e45SKevin Wolf     if (!qcow2_opts->has_lazy_refcounts) {
283329ca9e45SKevin Wolf         qcow2_opts->lazy_refcounts = false;
283429ca9e45SKevin Wolf     }
283529ca9e45SKevin Wolf     if (version < 3 && qcow2_opts->lazy_refcounts) {
283629ca9e45SKevin Wolf         error_setg(errp, "Lazy refcounts only supported with compatibility "
2837b76b4f60SKevin Wolf                    "level 1.1 and above (use version=v3 or greater)");
2838e1d74bc6SKevin Wolf         ret = -EINVAL;
2839e1d74bc6SKevin Wolf         goto out;
284029ca9e45SKevin Wolf     }
284129ca9e45SKevin Wolf 
284229ca9e45SKevin Wolf     if (!qcow2_opts->has_refcount_bits) {
284329ca9e45SKevin Wolf         qcow2_opts->refcount_bits = 16;
284429ca9e45SKevin Wolf     }
284529ca9e45SKevin Wolf     if (qcow2_opts->refcount_bits > 64 ||
284629ca9e45SKevin Wolf         !is_power_of_2(qcow2_opts->refcount_bits))
284729ca9e45SKevin Wolf     {
284829ca9e45SKevin Wolf         error_setg(errp, "Refcount width must be a power of two and may not "
284929ca9e45SKevin Wolf                    "exceed 64 bits");
2850e1d74bc6SKevin Wolf         ret = -EINVAL;
2851e1d74bc6SKevin Wolf         goto out;
285229ca9e45SKevin Wolf     }
285329ca9e45SKevin Wolf     if (version < 3 && qcow2_opts->refcount_bits != 16) {
285429ca9e45SKevin Wolf         error_setg(errp, "Different refcount widths than 16 bits require "
2855b76b4f60SKevin Wolf                    "compatibility level 1.1 or above (use version=v3 or "
285629ca9e45SKevin Wolf                    "greater)");
2857e1d74bc6SKevin Wolf         ret = -EINVAL;
2858e1d74bc6SKevin Wolf         goto out;
285929ca9e45SKevin Wolf     }
286029ca9e45SKevin Wolf     refcount_order = ctz32(qcow2_opts->refcount_bits);
286129ca9e45SKevin Wolf 
286229ca9e45SKevin Wolf 
286329ca9e45SKevin Wolf     /* Create BlockBackend to write to the image */
2864cbf2b7c4SKevin Wolf     blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
2865cbf2b7c4SKevin Wolf     ret = blk_insert_bs(blk, bs, errp);
2866a9420734SKevin Wolf     if (ret < 0) {
2867cbf2b7c4SKevin Wolf         goto out;
2868a9420734SKevin Wolf     }
286923588797SKevin Wolf     blk_set_allow_write_beyond_eof(blk, true);
287023588797SKevin Wolf 
2871e4b5dad8SKevin Wolf     /* Clear the protocol layer and preallocate it if necessary */
2872e4b5dad8SKevin Wolf     ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp);
2873e4b5dad8SKevin Wolf     if (ret < 0) {
2874e4b5dad8SKevin Wolf         goto out;
2875e4b5dad8SKevin Wolf     }
2876e4b5dad8SKevin Wolf 
2877e4b5dad8SKevin Wolf     if (qcow2_opts->preallocation == PREALLOC_MODE_FULL ||
2878e4b5dad8SKevin Wolf         qcow2_opts->preallocation == PREALLOC_MODE_FALLOC)
2879e4b5dad8SKevin Wolf     {
2880e4b5dad8SKevin Wolf         int64_t prealloc_size =
2881e4b5dad8SKevin Wolf             qcow2_calc_prealloc_size(qcow2_opts->size, cluster_size,
2882e4b5dad8SKevin Wolf                                      refcount_order);
2883e4b5dad8SKevin Wolf 
2884e4b5dad8SKevin Wolf         ret = blk_truncate(blk, prealloc_size, qcow2_opts->preallocation, errp);
2885e4b5dad8SKevin Wolf         if (ret < 0) {
2886e4b5dad8SKevin Wolf             goto out;
2887e4b5dad8SKevin Wolf         }
2888e4b5dad8SKevin Wolf     }
2889e4b5dad8SKevin Wolf 
2890a9420734SKevin Wolf     /* Write the header */
2891f8413b3cSKevin Wolf     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2892f8413b3cSKevin Wolf     header = g_malloc0(cluster_size);
2893f8413b3cSKevin Wolf     *header = (QCowHeader) {
2894f8413b3cSKevin Wolf         .magic                      = cpu_to_be32(QCOW_MAGIC),
2895f8413b3cSKevin Wolf         .version                    = cpu_to_be32(version),
28960eb4a8c1SStefan Hajnoczi         .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
2897f8413b3cSKevin Wolf         .size                       = cpu_to_be64(0),
2898f8413b3cSKevin Wolf         .l1_table_offset            = cpu_to_be64(0),
2899f8413b3cSKevin Wolf         .l1_size                    = cpu_to_be32(0),
2900f8413b3cSKevin Wolf         .refcount_table_offset      = cpu_to_be64(cluster_size),
2901f8413b3cSKevin Wolf         .refcount_table_clusters    = cpu_to_be32(1),
2902bd4b167fSMax Reitz         .refcount_order             = cpu_to_be32(refcount_order),
2903f8413b3cSKevin Wolf         .header_length              = cpu_to_be32(sizeof(*header)),
2904f8413b3cSKevin Wolf     };
2905a9420734SKevin Wolf 
2906b25b387fSDaniel P. Berrange     /* We'll update this to correct value later */
2907f8413b3cSKevin Wolf     header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2908a9420734SKevin Wolf 
290929ca9e45SKevin Wolf     if (qcow2_opts->lazy_refcounts) {
2910f8413b3cSKevin Wolf         header->compatible_features |=
2911bfe8043eSStefan Hajnoczi             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2912bfe8043eSStefan Hajnoczi     }
2913bfe8043eSStefan Hajnoczi 
29148341f00dSEric Blake     ret = blk_pwrite(blk, 0, header, cluster_size, 0);
2915f8413b3cSKevin Wolf     g_free(header);
2916a9420734SKevin Wolf     if (ret < 0) {
29173ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not write qcow2 header");
2918a9420734SKevin Wolf         goto out;
2919a9420734SKevin Wolf     }
2920a9420734SKevin Wolf 
2921b106ad91SKevin Wolf     /* Write a refcount table with one refcount block */
2922b106ad91SKevin Wolf     refcount_table = g_malloc0(2 * cluster_size);
2923b106ad91SKevin Wolf     refcount_table[0] = cpu_to_be64(2 * cluster_size);
29248341f00dSEric Blake     ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
29257267c094SAnthony Liguori     g_free(refcount_table);
2926a9420734SKevin Wolf 
2927a9420734SKevin Wolf     if (ret < 0) {
29283ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not write refcount table");
2929a9420734SKevin Wolf         goto out;
2930a9420734SKevin Wolf     }
2931a9420734SKevin Wolf 
293223588797SKevin Wolf     blk_unref(blk);
293323588797SKevin Wolf     blk = NULL;
2934a9420734SKevin Wolf 
2935a9420734SKevin Wolf     /*
2936a9420734SKevin Wolf      * And now open the image and make it consistent first (i.e. increase the
2937a9420734SKevin Wolf      * refcount of the cluster that is occupied by the header and the refcount
2938a9420734SKevin Wolf      * table)
2939a9420734SKevin Wolf      */
2940e6641719SMax Reitz     options = qdict_new();
294146f5ac20SEric Blake     qdict_put_str(options, "driver", "qcow2");
2942cbf2b7c4SKevin Wolf     qdict_put_str(options, "file", bs->node_name);
2943cbf2b7c4SKevin Wolf     blk = blk_new_open(NULL, NULL, options,
294455880601SKevin Wolf                        BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
294555880601SKevin Wolf                        &local_err);
294623588797SKevin Wolf     if (blk == NULL) {
29473ef6c40aSMax Reitz         error_propagate(errp, local_err);
294823588797SKevin Wolf         ret = -EIO;
2949a9420734SKevin Wolf         goto out;
2950a9420734SKevin Wolf     }
2951a9420734SKevin Wolf 
295223588797SKevin Wolf     ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
2953a9420734SKevin Wolf     if (ret < 0) {
29543ef6c40aSMax Reitz         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
29553ef6c40aSMax Reitz                          "header and refcount table");
2956a9420734SKevin Wolf         goto out;
2957a9420734SKevin Wolf 
2958a9420734SKevin Wolf     } else if (ret != 0) {
2959a9420734SKevin Wolf         error_report("Huh, first cluster in empty image is already in use?");
2960a9420734SKevin Wolf         abort();
2961a9420734SKevin Wolf     }
2962a9420734SKevin Wolf 
2963b527c9b3SKevin Wolf     /* Create a full header (including things like feature table) */
296423588797SKevin Wolf     ret = qcow2_update_header(blk_bs(blk));
2965b527c9b3SKevin Wolf     if (ret < 0) {
2966b527c9b3SKevin Wolf         error_setg_errno(errp, -ret, "Could not update qcow2 header");
2967b527c9b3SKevin Wolf         goto out;
2968b527c9b3SKevin Wolf     }
2969b527c9b3SKevin Wolf 
2970a9420734SKevin Wolf     /* Okay, now that we have a valid image, let's give it the right size */
297129ca9e45SKevin Wolf     ret = blk_truncate(blk, qcow2_opts->size, PREALLOC_MODE_OFF, errp);
2972a9420734SKevin Wolf     if (ret < 0) {
2973ed3d2ec9SMax Reitz         error_prepend(errp, "Could not resize image: ");
2974a9420734SKevin Wolf         goto out;
2975a9420734SKevin Wolf     }
2976a9420734SKevin Wolf 
2977a9420734SKevin Wolf     /* Want a backing file? There you go.*/
297829ca9e45SKevin Wolf     if (qcow2_opts->has_backing_file) {
297929ca9e45SKevin Wolf         const char *backing_format = NULL;
298029ca9e45SKevin Wolf 
298129ca9e45SKevin Wolf         if (qcow2_opts->has_backing_fmt) {
298229ca9e45SKevin Wolf             backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
298329ca9e45SKevin Wolf         }
298429ca9e45SKevin Wolf 
298529ca9e45SKevin Wolf         ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
298629ca9e45SKevin Wolf                                        backing_format);
2987a9420734SKevin Wolf         if (ret < 0) {
29883ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
298929ca9e45SKevin Wolf                              "with format '%s'", qcow2_opts->backing_file,
299029ca9e45SKevin Wolf                              backing_format);
2991a9420734SKevin Wolf             goto out;
2992a9420734SKevin Wolf         }
2993a9420734SKevin Wolf     }
2994a9420734SKevin Wolf 
2995b25b387fSDaniel P. Berrange     /* Want encryption? There you go. */
299660900b7bSKevin Wolf     if (qcow2_opts->has_encrypt) {
299760900b7bSKevin Wolf         ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
2998b25b387fSDaniel P. Berrange         if (ret < 0) {
2999b25b387fSDaniel P. Berrange             goto out;
3000b25b387fSDaniel P. Berrange         }
3001b25b387fSDaniel P. Berrange     }
3002b25b387fSDaniel P. Berrange 
3003a9420734SKevin Wolf     /* And if we're supposed to preallocate metadata, do that now */
300429ca9e45SKevin Wolf     if (qcow2_opts->preallocation != PREALLOC_MODE_OFF) {
3005061ca8a3SKevin Wolf         BDRVQcow2State *s = blk_bs(blk)->opaque;
3006061ca8a3SKevin Wolf         qemu_co_mutex_lock(&s->lock);
300747e86b86SKevin Wolf         ret = preallocate_co(blk_bs(blk), 0, qcow2_opts->size);
3008061ca8a3SKevin Wolf         qemu_co_mutex_unlock(&s->lock);
3009061ca8a3SKevin Wolf 
3010a9420734SKevin Wolf         if (ret < 0) {
30113ef6c40aSMax Reitz             error_setg_errno(errp, -ret, "Could not preallocate metadata");
3012a9420734SKevin Wolf             goto out;
3013a9420734SKevin Wolf         }
3014a9420734SKevin Wolf     }
3015a9420734SKevin Wolf 
301623588797SKevin Wolf     blk_unref(blk);
301723588797SKevin Wolf     blk = NULL;
3018ba2ab2f2SMax Reitz 
3019b25b387fSDaniel P. Berrange     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3020b25b387fSDaniel P. Berrange      * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3021b25b387fSDaniel P. Berrange      * have to setup decryption context. We're not doing any I/O on the top
3022b25b387fSDaniel P. Berrange      * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3023b25b387fSDaniel P. Berrange      * not have effect.
3024b25b387fSDaniel P. Berrange      */
3025e6641719SMax Reitz     options = qdict_new();
302646f5ac20SEric Blake     qdict_put_str(options, "driver", "qcow2");
3027cbf2b7c4SKevin Wolf     qdict_put_str(options, "file", bs->node_name);
3028cbf2b7c4SKevin Wolf     blk = blk_new_open(NULL, NULL, options,
3029b25b387fSDaniel P. Berrange                        BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3030b25b387fSDaniel P. Berrange                        &local_err);
303123588797SKevin Wolf     if (blk == NULL) {
3032ba2ab2f2SMax Reitz         error_propagate(errp, local_err);
303323588797SKevin Wolf         ret = -EIO;
3034ba2ab2f2SMax Reitz         goto out;
3035ba2ab2f2SMax Reitz     }
3036ba2ab2f2SMax Reitz 
3037a9420734SKevin Wolf     ret = 0;
3038a9420734SKevin Wolf out:
303923588797SKevin Wolf     blk_unref(blk);
3040e1d74bc6SKevin Wolf     bdrv_unref(bs);
3041a9420734SKevin Wolf     return ret;
3042a9420734SKevin Wolf }
3043de5f3f40SKevin Wolf 
3044efc75e2aSStefan Hajnoczi static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opts,
3045efc75e2aSStefan Hajnoczi                                              Error **errp)
3046de5f3f40SKevin Wolf {
3047b76b4f60SKevin Wolf     BlockdevCreateOptions *create_options = NULL;
304892adf9dbSMarkus Armbruster     QDict *qdict;
3049b76b4f60SKevin Wolf     Visitor *v;
3050cbf2b7c4SKevin Wolf     BlockDriverState *bs = NULL;
30513ef6c40aSMax Reitz     Error *local_err = NULL;
3052b76b4f60SKevin Wolf     const char *val;
30533ef6c40aSMax Reitz     int ret;
3054de5f3f40SKevin Wolf 
3055b76b4f60SKevin Wolf     /* Only the keyval visitor supports the dotted syntax needed for
3056b76b4f60SKevin Wolf      * encryption, so go through a QDict before getting a QAPI type. Ignore
3057b76b4f60SKevin Wolf      * options meant for the protocol layer so that the visitor doesn't
3058b76b4f60SKevin Wolf      * complain. */
3059b76b4f60SKevin Wolf     qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3060b76b4f60SKevin Wolf                                         true);
3061b76b4f60SKevin Wolf 
3062b76b4f60SKevin Wolf     /* Handle encryption options */
3063b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3064b76b4f60SKevin Wolf     if (val && !strcmp(val, "on")) {
3065b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3066b76b4f60SKevin Wolf     } else if (val && !strcmp(val, "off")) {
3067b76b4f60SKevin Wolf         qdict_del(qdict, BLOCK_OPT_ENCRYPT);
306829ca9e45SKevin Wolf     }
306960900b7bSKevin Wolf 
3070b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3071b76b4f60SKevin Wolf     if (val && !strcmp(val, "aes")) {
3072b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
307360900b7bSKevin Wolf     }
307460900b7bSKevin Wolf 
3075b76b4f60SKevin Wolf     /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3076b76b4f60SKevin Wolf      * version=v2/v3 below. */
3077b76b4f60SKevin Wolf     val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3078b76b4f60SKevin Wolf     if (val && !strcmp(val, "0.10")) {
3079b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3080b76b4f60SKevin Wolf     } else if (val && !strcmp(val, "1.1")) {
3081b76b4f60SKevin Wolf         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3082b76b4f60SKevin Wolf     }
3083b76b4f60SKevin Wolf 
3084b76b4f60SKevin Wolf     /* Change legacy command line options into QMP ones */
3085b76b4f60SKevin Wolf     static const QDictRenames opt_renames[] = {
3086b76b4f60SKevin Wolf         { BLOCK_OPT_BACKING_FILE,       "backing-file" },
3087b76b4f60SKevin Wolf         { BLOCK_OPT_BACKING_FMT,        "backing-fmt" },
3088b76b4f60SKevin Wolf         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
3089b76b4f60SKevin Wolf         { BLOCK_OPT_LAZY_REFCOUNTS,     "lazy-refcounts" },
3090b76b4f60SKevin Wolf         { BLOCK_OPT_REFCOUNT_BITS,      "refcount-bits" },
3091b76b4f60SKevin Wolf         { BLOCK_OPT_ENCRYPT,            BLOCK_OPT_ENCRYPT_FORMAT },
3092b76b4f60SKevin Wolf         { BLOCK_OPT_COMPAT_LEVEL,       "version" },
3093b76b4f60SKevin Wolf         { NULL, NULL },
3094b76b4f60SKevin Wolf     };
3095b76b4f60SKevin Wolf 
3096b76b4f60SKevin Wolf     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
30970eb4a8c1SStefan Hajnoczi         ret = -EINVAL;
30980eb4a8c1SStefan Hajnoczi         goto finish;
30990eb4a8c1SStefan Hajnoczi     }
3100bd4b167fSMax Reitz 
3101cbf2b7c4SKevin Wolf     /* Create and open the file (protocol layer) */
3102cbf2b7c4SKevin Wolf     ret = bdrv_create_file(filename, opts, errp);
3103cbf2b7c4SKevin Wolf     if (ret < 0) {
3104cbf2b7c4SKevin Wolf         goto finish;
3105cbf2b7c4SKevin Wolf     }
3106cbf2b7c4SKevin Wolf 
3107cbf2b7c4SKevin Wolf     bs = bdrv_open(filename, NULL, NULL,
3108cbf2b7c4SKevin Wolf                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3109cbf2b7c4SKevin Wolf     if (bs == NULL) {
3110cbf2b7c4SKevin Wolf         ret = -EIO;
3111cbf2b7c4SKevin Wolf         goto finish;
3112cbf2b7c4SKevin Wolf     }
3113cbf2b7c4SKevin Wolf 
3114b76b4f60SKevin Wolf     /* Set 'driver' and 'node' options */
3115b76b4f60SKevin Wolf     qdict_put_str(qdict, "driver", "qcow2");
3116b76b4f60SKevin Wolf     qdict_put_str(qdict, "file", bs->node_name);
3117b76b4f60SKevin Wolf 
3118b76b4f60SKevin Wolf     /* Now get the QAPI type BlockdevCreateOptions */
3119af91062eSMarkus Armbruster     v = qobject_input_visitor_new_flat_confused(qdict, errp);
3120af91062eSMarkus Armbruster     if (!v) {
3121b76b4f60SKevin Wolf         ret = -EINVAL;
3122b76b4f60SKevin Wolf         goto finish;
3123b76b4f60SKevin Wolf     }
3124b76b4f60SKevin Wolf 
3125b76b4f60SKevin Wolf     visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
3126b76b4f60SKevin Wolf     visit_free(v);
3127b76b4f60SKevin Wolf 
3128b76b4f60SKevin Wolf     if (local_err) {
3129b76b4f60SKevin Wolf         error_propagate(errp, local_err);
3130b76b4f60SKevin Wolf         ret = -EINVAL;
3131b76b4f60SKevin Wolf         goto finish;
3132b76b4f60SKevin Wolf     }
3133b76b4f60SKevin Wolf 
3134b76b4f60SKevin Wolf     /* Silently round up size */
3135b76b4f60SKevin Wolf     create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
3136b76b4f60SKevin Wolf                                             BDRV_SECTOR_SIZE);
3137b76b4f60SKevin Wolf 
3138cbf2b7c4SKevin Wolf     /* Create the qcow2 image (format layer) */
3139b76b4f60SKevin Wolf     ret = qcow2_co_create(create_options, errp);
3140cbf2b7c4SKevin Wolf     if (ret < 0) {
3141cbf2b7c4SKevin Wolf         goto finish;
3142cbf2b7c4SKevin Wolf     }
31431bd0e2d1SChunyan Liu 
3144b76b4f60SKevin Wolf     ret = 0;
31451bd0e2d1SChunyan Liu finish:
3146cb3e7f08SMarc-André Lureau     qobject_unref(qdict);
3147cbf2b7c4SKevin Wolf     bdrv_unref(bs);
3148b76b4f60SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
31493ef6c40aSMax Reitz     return ret;
3150de5f3f40SKevin Wolf }
3151de5f3f40SKevin Wolf 
31522928abceSDenis V. Lunev 
3153f06f6b66SEric Blake static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
31542928abceSDenis V. Lunev {
315531826642SEric Blake     int64_t nr;
315631826642SEric Blake     int res;
3157f06f6b66SEric Blake 
3158f06f6b66SEric Blake     /* Clamp to image length, before checking status of underlying sectors */
31598cbf74b2SEric Blake     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
31608cbf74b2SEric Blake         bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
3161fbaa6bb3SEric Blake     }
3162fbaa6bb3SEric Blake 
3163f06f6b66SEric Blake     if (!bytes) {
3164ebb718a5SEric Blake         return true;
31652928abceSDenis V. Lunev     }
31668cbf74b2SEric Blake     res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
316731826642SEric Blake     return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
31682928abceSDenis V. Lunev }
31692928abceSDenis V. Lunev 
31705544b59fSEric Blake static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
3171f5a5ca79SManos Pitsidianakis     int64_t offset, int bytes, BdrvRequestFlags flags)
3172621f0589SKevin Wolf {
3173621f0589SKevin Wolf     int ret;
3174ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3175621f0589SKevin Wolf 
31765544b59fSEric Blake     uint32_t head = offset % s->cluster_size;
3177f5a5ca79SManos Pitsidianakis     uint32_t tail = (offset + bytes) % s->cluster_size;
31782928abceSDenis V. Lunev 
3179f5a5ca79SManos Pitsidianakis     trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3180f5a5ca79SManos Pitsidianakis     if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
3181fbaa6bb3SEric Blake         tail = 0;
3182fbaa6bb3SEric Blake     }
31835a64e942SDenis V. Lunev 
3184ebb718a5SEric Blake     if (head || tail) {
3185ebb718a5SEric Blake         uint64_t off;
3186ecfe1863SKevin Wolf         unsigned int nr;
31872928abceSDenis V. Lunev 
3188f5a5ca79SManos Pitsidianakis         assert(head + bytes <= s->cluster_size);
31892928abceSDenis V. Lunev 
3190ebb718a5SEric Blake         /* check whether remainder of cluster already reads as zero */
3191f06f6b66SEric Blake         if (!(is_zero(bs, offset - head, head) &&
3192f06f6b66SEric Blake               is_zero(bs, offset + bytes,
3193f06f6b66SEric Blake                       tail ? s->cluster_size - tail : 0))) {
3194621f0589SKevin Wolf             return -ENOTSUP;
3195621f0589SKevin Wolf         }
3196621f0589SKevin Wolf 
3197621f0589SKevin Wolf         qemu_co_mutex_lock(&s->lock);
31982928abceSDenis V. Lunev         /* We can have new write after previous check */
3199f06f6b66SEric Blake         offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
3200f5a5ca79SManos Pitsidianakis         bytes = s->cluster_size;
3201ecfe1863SKevin Wolf         nr = s->cluster_size;
32025544b59fSEric Blake         ret = qcow2_get_cluster_offset(bs, offset, &nr, &off);
3203fdfab37dSEric Blake         if (ret != QCOW2_CLUSTER_UNALLOCATED &&
3204fdfab37dSEric Blake             ret != QCOW2_CLUSTER_ZERO_PLAIN &&
3205fdfab37dSEric Blake             ret != QCOW2_CLUSTER_ZERO_ALLOC) {
32062928abceSDenis V. Lunev             qemu_co_mutex_unlock(&s->lock);
32072928abceSDenis V. Lunev             return -ENOTSUP;
32082928abceSDenis V. Lunev         }
32092928abceSDenis V. Lunev     } else {
32102928abceSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
32112928abceSDenis V. Lunev     }
32122928abceSDenis V. Lunev 
3213f5a5ca79SManos Pitsidianakis     trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
32145a64e942SDenis V. Lunev 
32152928abceSDenis V. Lunev     /* Whatever is left can use real zero clusters */
3216f5a5ca79SManos Pitsidianakis     ret = qcow2_cluster_zeroize(bs, offset, bytes, flags);
3217621f0589SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
3218621f0589SKevin Wolf 
3219621f0589SKevin Wolf     return ret;
3220621f0589SKevin Wolf }
3221621f0589SKevin Wolf 
322282e8a788SEric Blake static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
3223f5a5ca79SManos Pitsidianakis                                           int64_t offset, int bytes)
32245ea929e3SKevin Wolf {
32256db39ae2SPaolo Bonzini     int ret;
3226ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
32276db39ae2SPaolo Bonzini 
3228f5a5ca79SManos Pitsidianakis     if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3229f5a5ca79SManos Pitsidianakis         assert(bytes < s->cluster_size);
3230048c5fd1SEric Blake         /* Ignore partial clusters, except for the special case of the
3231048c5fd1SEric Blake          * complete partial cluster at the end of an unaligned file */
3232048c5fd1SEric Blake         if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
3233f5a5ca79SManos Pitsidianakis             offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
323449228d1eSEric Blake             return -ENOTSUP;
323549228d1eSEric Blake         }
3236048c5fd1SEric Blake     }
323749228d1eSEric Blake 
32386db39ae2SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
3239f5a5ca79SManos Pitsidianakis     ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
3240d2cb36afSEric Blake                                 false);
32416db39ae2SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
32426db39ae2SPaolo Bonzini     return ret;
32435ea929e3SKevin Wolf }
32445ea929e3SKevin Wolf 
3245fd9fcd37SFam Zheng static int coroutine_fn
3246fd9fcd37SFam Zheng qcow2_co_copy_range_from(BlockDriverState *bs,
3247fd9fcd37SFam Zheng                          BdrvChild *src, uint64_t src_offset,
3248fd9fcd37SFam Zheng                          BdrvChild *dst, uint64_t dst_offset,
3249fd9fcd37SFam Zheng                          uint64_t bytes, BdrvRequestFlags flags)
3250fd9fcd37SFam Zheng {
3251fd9fcd37SFam Zheng     BDRVQcow2State *s = bs->opaque;
3252fd9fcd37SFam Zheng     int ret;
3253fd9fcd37SFam Zheng     unsigned int cur_bytes; /* number of bytes in current iteration */
3254fd9fcd37SFam Zheng     BdrvChild *child = NULL;
3255fd9fcd37SFam Zheng     BdrvRequestFlags cur_flags;
3256fd9fcd37SFam Zheng 
3257fd9fcd37SFam Zheng     assert(!bs->encrypted);
3258fd9fcd37SFam Zheng     qemu_co_mutex_lock(&s->lock);
3259fd9fcd37SFam Zheng 
3260fd9fcd37SFam Zheng     while (bytes != 0) {
3261fd9fcd37SFam Zheng         uint64_t copy_offset = 0;
3262fd9fcd37SFam Zheng         /* prepare next request */
3263fd9fcd37SFam Zheng         cur_bytes = MIN(bytes, INT_MAX);
3264fd9fcd37SFam Zheng         cur_flags = flags;
3265fd9fcd37SFam Zheng 
3266fd9fcd37SFam Zheng         ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, &copy_offset);
3267fd9fcd37SFam Zheng         if (ret < 0) {
3268fd9fcd37SFam Zheng             goto out;
3269fd9fcd37SFam Zheng         }
3270fd9fcd37SFam Zheng 
3271fd9fcd37SFam Zheng         switch (ret) {
3272fd9fcd37SFam Zheng         case QCOW2_CLUSTER_UNALLOCATED:
3273fd9fcd37SFam Zheng             if (bs->backing && bs->backing->bs) {
3274fd9fcd37SFam Zheng                 int64_t backing_length = bdrv_getlength(bs->backing->bs);
3275fd9fcd37SFam Zheng                 if (src_offset >= backing_length) {
3276fd9fcd37SFam Zheng                     cur_flags |= BDRV_REQ_ZERO_WRITE;
3277fd9fcd37SFam Zheng                 } else {
3278fd9fcd37SFam Zheng                     child = bs->backing;
3279fd9fcd37SFam Zheng                     cur_bytes = MIN(cur_bytes, backing_length - src_offset);
3280fd9fcd37SFam Zheng                     copy_offset = src_offset;
3281fd9fcd37SFam Zheng                 }
3282fd9fcd37SFam Zheng             } else {
3283fd9fcd37SFam Zheng                 cur_flags |= BDRV_REQ_ZERO_WRITE;
3284fd9fcd37SFam Zheng             }
3285fd9fcd37SFam Zheng             break;
3286fd9fcd37SFam Zheng 
3287fd9fcd37SFam Zheng         case QCOW2_CLUSTER_ZERO_PLAIN:
3288fd9fcd37SFam Zheng         case QCOW2_CLUSTER_ZERO_ALLOC:
3289fd9fcd37SFam Zheng             cur_flags |= BDRV_REQ_ZERO_WRITE;
3290fd9fcd37SFam Zheng             break;
3291fd9fcd37SFam Zheng 
3292fd9fcd37SFam Zheng         case QCOW2_CLUSTER_COMPRESSED:
3293fd9fcd37SFam Zheng             ret = -ENOTSUP;
3294fd9fcd37SFam Zheng             goto out;
3295fd9fcd37SFam Zheng             break;
3296fd9fcd37SFam Zheng 
3297fd9fcd37SFam Zheng         case QCOW2_CLUSTER_NORMAL:
3298fd9fcd37SFam Zheng             child = bs->file;
3299fd9fcd37SFam Zheng             copy_offset += offset_into_cluster(s, src_offset);
3300fd9fcd37SFam Zheng             if ((copy_offset & 511) != 0) {
3301fd9fcd37SFam Zheng                 ret = -EIO;
3302fd9fcd37SFam Zheng                 goto out;
3303fd9fcd37SFam Zheng             }
3304fd9fcd37SFam Zheng             break;
3305fd9fcd37SFam Zheng 
3306fd9fcd37SFam Zheng         default:
3307fd9fcd37SFam Zheng             abort();
3308fd9fcd37SFam Zheng         }
3309fd9fcd37SFam Zheng         qemu_co_mutex_unlock(&s->lock);
3310fd9fcd37SFam Zheng         ret = bdrv_co_copy_range_from(child,
3311fd9fcd37SFam Zheng                                       copy_offset,
3312fd9fcd37SFam Zheng                                       dst, dst_offset,
3313fd9fcd37SFam Zheng                                       cur_bytes, cur_flags);
3314fd9fcd37SFam Zheng         qemu_co_mutex_lock(&s->lock);
3315fd9fcd37SFam Zheng         if (ret < 0) {
3316fd9fcd37SFam Zheng             goto out;
3317fd9fcd37SFam Zheng         }
3318fd9fcd37SFam Zheng 
3319fd9fcd37SFam Zheng         bytes -= cur_bytes;
3320fd9fcd37SFam Zheng         src_offset += cur_bytes;
3321fd9fcd37SFam Zheng         dst_offset += cur_bytes;
3322fd9fcd37SFam Zheng     }
3323fd9fcd37SFam Zheng     ret = 0;
3324fd9fcd37SFam Zheng 
3325fd9fcd37SFam Zheng out:
3326fd9fcd37SFam Zheng     qemu_co_mutex_unlock(&s->lock);
3327fd9fcd37SFam Zheng     return ret;
3328fd9fcd37SFam Zheng }
3329fd9fcd37SFam Zheng 
3330fd9fcd37SFam Zheng static int coroutine_fn
3331fd9fcd37SFam Zheng qcow2_co_copy_range_to(BlockDriverState *bs,
3332fd9fcd37SFam Zheng                        BdrvChild *src, uint64_t src_offset,
3333fd9fcd37SFam Zheng                        BdrvChild *dst, uint64_t dst_offset,
3334fd9fcd37SFam Zheng                        uint64_t bytes, BdrvRequestFlags flags)
3335fd9fcd37SFam Zheng {
3336fd9fcd37SFam Zheng     BDRVQcow2State *s = bs->opaque;
3337fd9fcd37SFam Zheng     int offset_in_cluster;
3338fd9fcd37SFam Zheng     int ret;
3339fd9fcd37SFam Zheng     unsigned int cur_bytes; /* number of sectors in current iteration */
3340fd9fcd37SFam Zheng     uint64_t cluster_offset;
3341fd9fcd37SFam Zheng     uint8_t *cluster_data = NULL;
3342fd9fcd37SFam Zheng     QCowL2Meta *l2meta = NULL;
3343fd9fcd37SFam Zheng 
3344fd9fcd37SFam Zheng     assert(!bs->encrypted);
3345fd9fcd37SFam Zheng     s->cluster_cache_offset = -1; /* disable compressed cache */
3346fd9fcd37SFam Zheng 
3347fd9fcd37SFam Zheng     qemu_co_mutex_lock(&s->lock);
3348fd9fcd37SFam Zheng 
3349fd9fcd37SFam Zheng     while (bytes != 0) {
3350fd9fcd37SFam Zheng 
3351fd9fcd37SFam Zheng         l2meta = NULL;
3352fd9fcd37SFam Zheng 
3353fd9fcd37SFam Zheng         offset_in_cluster = offset_into_cluster(s, dst_offset);
3354fd9fcd37SFam Zheng         cur_bytes = MIN(bytes, INT_MAX);
3355fd9fcd37SFam Zheng 
3356fd9fcd37SFam Zheng         /* TODO:
3357fd9fcd37SFam Zheng          * If src->bs == dst->bs, we could simply copy by incrementing
3358fd9fcd37SFam Zheng          * the refcnt, without copying user data.
3359fd9fcd37SFam Zheng          * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3360fd9fcd37SFam Zheng         ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
3361fd9fcd37SFam Zheng                                          &cluster_offset, &l2meta);
3362fd9fcd37SFam Zheng         if (ret < 0) {
3363fd9fcd37SFam Zheng             goto fail;
3364fd9fcd37SFam Zheng         }
3365fd9fcd37SFam Zheng 
3366fd9fcd37SFam Zheng         assert((cluster_offset & 511) == 0);
3367fd9fcd37SFam Zheng 
3368fd9fcd37SFam Zheng         ret = qcow2_pre_write_overlap_check(bs, 0,
3369fd9fcd37SFam Zheng                 cluster_offset + offset_in_cluster, cur_bytes);
3370fd9fcd37SFam Zheng         if (ret < 0) {
3371fd9fcd37SFam Zheng             goto fail;
3372fd9fcd37SFam Zheng         }
3373fd9fcd37SFam Zheng 
3374fd9fcd37SFam Zheng         qemu_co_mutex_unlock(&s->lock);
3375fd9fcd37SFam Zheng         ret = bdrv_co_copy_range_to(src, src_offset,
3376fd9fcd37SFam Zheng                                     bs->file,
3377fd9fcd37SFam Zheng                                     cluster_offset + offset_in_cluster,
3378fd9fcd37SFam Zheng                                     cur_bytes, flags);
3379fd9fcd37SFam Zheng         qemu_co_mutex_lock(&s->lock);
3380fd9fcd37SFam Zheng         if (ret < 0) {
3381fd9fcd37SFam Zheng             goto fail;
3382fd9fcd37SFam Zheng         }
3383fd9fcd37SFam Zheng 
3384fd9fcd37SFam Zheng         ret = qcow2_handle_l2meta(bs, &l2meta, true);
3385fd9fcd37SFam Zheng         if (ret) {
3386fd9fcd37SFam Zheng             goto fail;
3387fd9fcd37SFam Zheng         }
3388fd9fcd37SFam Zheng 
3389fd9fcd37SFam Zheng         bytes -= cur_bytes;
3390fd9fcd37SFam Zheng         dst_offset += cur_bytes;
3391fd9fcd37SFam Zheng     }
3392fd9fcd37SFam Zheng     ret = 0;
3393fd9fcd37SFam Zheng 
3394fd9fcd37SFam Zheng fail:
3395fd9fcd37SFam Zheng     qcow2_handle_l2meta(bs, &l2meta, false);
3396fd9fcd37SFam Zheng 
3397fd9fcd37SFam Zheng     qemu_co_mutex_unlock(&s->lock);
3398fd9fcd37SFam Zheng 
3399fd9fcd37SFam Zheng     qemu_vfree(cluster_data);
3400fd9fcd37SFam Zheng     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
3401fd9fcd37SFam Zheng 
3402fd9fcd37SFam Zheng     return ret;
3403fd9fcd37SFam Zheng }
3404fd9fcd37SFam Zheng 
3405061ca8a3SKevin Wolf static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
34068243ccb7SMax Reitz                                           PreallocMode prealloc, Error **errp)
3407419b19d9SStefan Hajnoczi {
3408ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
340995b98f34SMax Reitz     uint64_t old_length;
34102cf7cfa1SKevin Wolf     int64_t new_l1_size;
34112cf7cfa1SKevin Wolf     int ret;
3412419b19d9SStefan Hajnoczi 
3413772d1f97SMax Reitz     if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
3414772d1f97SMax Reitz         prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
3415772d1f97SMax Reitz     {
34168243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
3417977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
34188243ccb7SMax Reitz         return -ENOTSUP;
34198243ccb7SMax Reitz     }
34208243ccb7SMax Reitz 
3421419b19d9SStefan Hajnoczi     if (offset & 511) {
34224bff28b8SMax Reitz         error_setg(errp, "The new size must be a multiple of 512");
3423419b19d9SStefan Hajnoczi         return -EINVAL;
3424419b19d9SStefan Hajnoczi     }
3425419b19d9SStefan Hajnoczi 
3426061ca8a3SKevin Wolf     qemu_co_mutex_lock(&s->lock);
3427061ca8a3SKevin Wolf 
3428419b19d9SStefan Hajnoczi     /* cannot proceed if image has snapshots */
3429419b19d9SStefan Hajnoczi     if (s->nb_snapshots) {
34304bff28b8SMax Reitz         error_setg(errp, "Can't resize an image which has snapshots");
3431061ca8a3SKevin Wolf         ret = -ENOTSUP;
3432061ca8a3SKevin Wolf         goto fail;
3433419b19d9SStefan Hajnoczi     }
3434419b19d9SStefan Hajnoczi 
343588ddffaeSVladimir Sementsov-Ogievskiy     /* cannot proceed if image has bitmaps */
343688ddffaeSVladimir Sementsov-Ogievskiy     if (s->nb_bitmaps) {
343788ddffaeSVladimir Sementsov-Ogievskiy         /* TODO: resize bitmaps in the image */
343888ddffaeSVladimir Sementsov-Ogievskiy         error_setg(errp, "Can't resize an image which has bitmaps");
3439061ca8a3SKevin Wolf         ret = -ENOTSUP;
3440061ca8a3SKevin Wolf         goto fail;
344188ddffaeSVladimir Sementsov-Ogievskiy     }
344288ddffaeSVladimir Sementsov-Ogievskiy 
344395b98f34SMax Reitz     old_length = bs->total_sectors * 512;
344446b732cdSPavel Butsykin     new_l1_size = size_to_l1(s, offset);
344595b98f34SMax Reitz 
344695b98f34SMax Reitz     if (offset < old_length) {
3447163bc39dSPavel Butsykin         int64_t last_cluster, old_file_size;
344846b732cdSPavel Butsykin         if (prealloc != PREALLOC_MODE_OFF) {
344946b732cdSPavel Butsykin             error_setg(errp,
345046b732cdSPavel Butsykin                        "Preallocation can't be used for shrinking an image");
3451061ca8a3SKevin Wolf             ret = -EINVAL;
3452061ca8a3SKevin Wolf             goto fail;
3453419b19d9SStefan Hajnoczi         }
3454419b19d9SStefan Hajnoczi 
345546b732cdSPavel Butsykin         ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
345646b732cdSPavel Butsykin                                     old_length - ROUND_UP(offset,
345746b732cdSPavel Butsykin                                                           s->cluster_size),
345846b732cdSPavel Butsykin                                     QCOW2_DISCARD_ALWAYS, true);
345946b732cdSPavel Butsykin         if (ret < 0) {
346046b732cdSPavel Butsykin             error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
3461061ca8a3SKevin Wolf             goto fail;
346246b732cdSPavel Butsykin         }
346346b732cdSPavel Butsykin 
346446b732cdSPavel Butsykin         ret = qcow2_shrink_l1_table(bs, new_l1_size);
346546b732cdSPavel Butsykin         if (ret < 0) {
346646b732cdSPavel Butsykin             error_setg_errno(errp, -ret,
346746b732cdSPavel Butsykin                              "Failed to reduce the number of L2 tables");
3468061ca8a3SKevin Wolf             goto fail;
346946b732cdSPavel Butsykin         }
347046b732cdSPavel Butsykin 
347146b732cdSPavel Butsykin         ret = qcow2_shrink_reftable(bs);
347246b732cdSPavel Butsykin         if (ret < 0) {
347346b732cdSPavel Butsykin             error_setg_errno(errp, -ret,
347446b732cdSPavel Butsykin                              "Failed to discard unused refblocks");
3475061ca8a3SKevin Wolf             goto fail;
347646b732cdSPavel Butsykin         }
3477163bc39dSPavel Butsykin 
3478163bc39dSPavel Butsykin         old_file_size = bdrv_getlength(bs->file->bs);
3479163bc39dSPavel Butsykin         if (old_file_size < 0) {
3480163bc39dSPavel Butsykin             error_setg_errno(errp, -old_file_size,
3481163bc39dSPavel Butsykin                              "Failed to inquire current file length");
3482061ca8a3SKevin Wolf             ret = old_file_size;
3483061ca8a3SKevin Wolf             goto fail;
3484163bc39dSPavel Butsykin         }
3485163bc39dSPavel Butsykin         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
3486163bc39dSPavel Butsykin         if (last_cluster < 0) {
3487163bc39dSPavel Butsykin             error_setg_errno(errp, -last_cluster,
3488163bc39dSPavel Butsykin                              "Failed to find the last cluster");
3489061ca8a3SKevin Wolf             ret = last_cluster;
3490061ca8a3SKevin Wolf             goto fail;
3491163bc39dSPavel Butsykin         }
3492163bc39dSPavel Butsykin         if ((last_cluster + 1) * s->cluster_size < old_file_size) {
3493233521b1SMax Reitz             Error *local_err = NULL;
3494233521b1SMax Reitz 
3495061ca8a3SKevin Wolf             bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
3496233521b1SMax Reitz                              PREALLOC_MODE_OFF, &local_err);
3497233521b1SMax Reitz             if (local_err) {
3498233521b1SMax Reitz                 warn_reportf_err(local_err,
3499233521b1SMax Reitz                                  "Failed to truncate the tail of the image: ");
3500163bc39dSPavel Butsykin             }
3501163bc39dSPavel Butsykin         }
350246b732cdSPavel Butsykin     } else {
350372893756SStefan Hajnoczi         ret = qcow2_grow_l1_table(bs, new_l1_size, true);
3504419b19d9SStefan Hajnoczi         if (ret < 0) {
3505f59adb32SMax Reitz             error_setg_errno(errp, -ret, "Failed to grow the L1 table");
3506061ca8a3SKevin Wolf             goto fail;
3507419b19d9SStefan Hajnoczi         }
350846b732cdSPavel Butsykin     }
3509419b19d9SStefan Hajnoczi 
351095b98f34SMax Reitz     switch (prealloc) {
351195b98f34SMax Reitz     case PREALLOC_MODE_OFF:
351295b98f34SMax Reitz         break;
351395b98f34SMax Reitz 
351495b98f34SMax Reitz     case PREALLOC_MODE_METADATA:
351547e86b86SKevin Wolf         ret = preallocate_co(bs, old_length, offset);
351695b98f34SMax Reitz         if (ret < 0) {
351795b98f34SMax Reitz             error_setg_errno(errp, -ret, "Preallocation failed");
3518061ca8a3SKevin Wolf             goto fail;
351995b98f34SMax Reitz         }
352095b98f34SMax Reitz         break;
352195b98f34SMax Reitz 
3522772d1f97SMax Reitz     case PREALLOC_MODE_FALLOC:
3523772d1f97SMax Reitz     case PREALLOC_MODE_FULL:
3524772d1f97SMax Reitz     {
3525772d1f97SMax Reitz         int64_t allocation_start, host_offset, guest_offset;
3526772d1f97SMax Reitz         int64_t clusters_allocated;
3527772d1f97SMax Reitz         int64_t old_file_size, new_file_size;
3528772d1f97SMax Reitz         uint64_t nb_new_data_clusters, nb_new_l2_tables;
3529772d1f97SMax Reitz 
3530772d1f97SMax Reitz         old_file_size = bdrv_getlength(bs->file->bs);
3531772d1f97SMax Reitz         if (old_file_size < 0) {
3532772d1f97SMax Reitz             error_setg_errno(errp, -old_file_size,
3533772d1f97SMax Reitz                              "Failed to inquire current file length");
3534061ca8a3SKevin Wolf             ret = old_file_size;
3535061ca8a3SKevin Wolf             goto fail;
3536772d1f97SMax Reitz         }
3537e400ad1eSMax Reitz         old_file_size = ROUND_UP(old_file_size, s->cluster_size);
3538772d1f97SMax Reitz 
3539772d1f97SMax Reitz         nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
3540772d1f97SMax Reitz                                             s->cluster_size);
3541772d1f97SMax Reitz 
3542772d1f97SMax Reitz         /* This is an overestimation; we will not actually allocate space for
3543772d1f97SMax Reitz          * these in the file but just make sure the new refcount structures are
3544772d1f97SMax Reitz          * able to cover them so we will not have to allocate new refblocks
3545772d1f97SMax Reitz          * while entering the data blocks in the potentially new L2 tables.
3546772d1f97SMax Reitz          * (We do not actually care where the L2 tables are placed. Maybe they
3547772d1f97SMax Reitz          *  are already allocated or they can be placed somewhere before
3548772d1f97SMax Reitz          *  @old_file_size. It does not matter because they will be fully
3549772d1f97SMax Reitz          *  allocated automatically, so they do not need to be covered by the
3550772d1f97SMax Reitz          *  preallocation. All that matters is that we will not have to allocate
3551772d1f97SMax Reitz          *  new refcount structures for them.) */
3552772d1f97SMax Reitz         nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
3553772d1f97SMax Reitz                                         s->cluster_size / sizeof(uint64_t));
3554772d1f97SMax Reitz         /* The cluster range may not be aligned to L2 boundaries, so add one L2
3555772d1f97SMax Reitz          * table for a potential head/tail */
3556772d1f97SMax Reitz         nb_new_l2_tables++;
3557772d1f97SMax Reitz 
3558772d1f97SMax Reitz         allocation_start = qcow2_refcount_area(bs, old_file_size,
3559772d1f97SMax Reitz                                                nb_new_data_clusters +
3560772d1f97SMax Reitz                                                nb_new_l2_tables,
3561772d1f97SMax Reitz                                                true, 0, 0);
3562772d1f97SMax Reitz         if (allocation_start < 0) {
3563772d1f97SMax Reitz             error_setg_errno(errp, -allocation_start,
3564772d1f97SMax Reitz                              "Failed to resize refcount structures");
3565061ca8a3SKevin Wolf             ret = allocation_start;
3566061ca8a3SKevin Wolf             goto fail;
3567772d1f97SMax Reitz         }
3568772d1f97SMax Reitz 
3569772d1f97SMax Reitz         clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
3570772d1f97SMax Reitz                                                      nb_new_data_clusters);
3571772d1f97SMax Reitz         if (clusters_allocated < 0) {
3572772d1f97SMax Reitz             error_setg_errno(errp, -clusters_allocated,
3573772d1f97SMax Reitz                              "Failed to allocate data clusters");
3574061ca8a3SKevin Wolf             ret = clusters_allocated;
3575061ca8a3SKevin Wolf             goto fail;
3576772d1f97SMax Reitz         }
3577772d1f97SMax Reitz 
3578772d1f97SMax Reitz         assert(clusters_allocated == nb_new_data_clusters);
3579772d1f97SMax Reitz 
3580772d1f97SMax Reitz         /* Allocate the data area */
3581772d1f97SMax Reitz         new_file_size = allocation_start +
3582772d1f97SMax Reitz                         nb_new_data_clusters * s->cluster_size;
3583061ca8a3SKevin Wolf         ret = bdrv_co_truncate(bs->file, new_file_size, prealloc, errp);
3584772d1f97SMax Reitz         if (ret < 0) {
3585772d1f97SMax Reitz             error_prepend(errp, "Failed to resize underlying file: ");
3586772d1f97SMax Reitz             qcow2_free_clusters(bs, allocation_start,
3587772d1f97SMax Reitz                                 nb_new_data_clusters * s->cluster_size,
3588772d1f97SMax Reitz                                 QCOW2_DISCARD_OTHER);
3589061ca8a3SKevin Wolf             goto fail;
3590772d1f97SMax Reitz         }
3591772d1f97SMax Reitz 
3592772d1f97SMax Reitz         /* Create the necessary L2 entries */
3593772d1f97SMax Reitz         host_offset = allocation_start;
3594772d1f97SMax Reitz         guest_offset = old_length;
3595772d1f97SMax Reitz         while (nb_new_data_clusters) {
359613bec229SAlberto Garcia             int64_t nb_clusters = MIN(
359713bec229SAlberto Garcia                 nb_new_data_clusters,
359813bec229SAlberto Garcia                 s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
3599772d1f97SMax Reitz             QCowL2Meta allocation = {
3600772d1f97SMax Reitz                 .offset       = guest_offset,
3601772d1f97SMax Reitz                 .alloc_offset = host_offset,
3602772d1f97SMax Reitz                 .nb_clusters  = nb_clusters,
3603772d1f97SMax Reitz             };
3604772d1f97SMax Reitz             qemu_co_queue_init(&allocation.dependent_requests);
3605772d1f97SMax Reitz 
3606772d1f97SMax Reitz             ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
3607772d1f97SMax Reitz             if (ret < 0) {
3608772d1f97SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update L2 tables");
3609772d1f97SMax Reitz                 qcow2_free_clusters(bs, host_offset,
3610772d1f97SMax Reitz                                     nb_new_data_clusters * s->cluster_size,
3611772d1f97SMax Reitz                                     QCOW2_DISCARD_OTHER);
3612061ca8a3SKevin Wolf                 goto fail;
3613772d1f97SMax Reitz             }
3614772d1f97SMax Reitz 
3615772d1f97SMax Reitz             guest_offset += nb_clusters * s->cluster_size;
3616772d1f97SMax Reitz             host_offset += nb_clusters * s->cluster_size;
3617772d1f97SMax Reitz             nb_new_data_clusters -= nb_clusters;
3618772d1f97SMax Reitz         }
3619772d1f97SMax Reitz         break;
3620772d1f97SMax Reitz     }
3621772d1f97SMax Reitz 
362295b98f34SMax Reitz     default:
362395b98f34SMax Reitz         g_assert_not_reached();
362495b98f34SMax Reitz     }
362595b98f34SMax Reitz 
362695b98f34SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
362795b98f34SMax Reitz         /* Flush metadata before actually changing the image size */
3628061ca8a3SKevin Wolf         ret = qcow2_write_caches(bs);
362995b98f34SMax Reitz         if (ret < 0) {
363095b98f34SMax Reitz             error_setg_errno(errp, -ret,
363195b98f34SMax Reitz                              "Failed to flush the preallocated area to disk");
3632061ca8a3SKevin Wolf             goto fail;
363395b98f34SMax Reitz         }
363495b98f34SMax Reitz     }
363595b98f34SMax Reitz 
3636419b19d9SStefan Hajnoczi     /* write updated header.size */
3637419b19d9SStefan Hajnoczi     offset = cpu_to_be64(offset);
3638d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
3639419b19d9SStefan Hajnoczi                            &offset, sizeof(uint64_t));
3640419b19d9SStefan Hajnoczi     if (ret < 0) {
3641f59adb32SMax Reitz         error_setg_errno(errp, -ret, "Failed to update the image size");
3642061ca8a3SKevin Wolf         goto fail;
3643419b19d9SStefan Hajnoczi     }
3644419b19d9SStefan Hajnoczi 
3645419b19d9SStefan Hajnoczi     s->l1_vm_state_index = new_l1_size;
3646061ca8a3SKevin Wolf     ret = 0;
3647061ca8a3SKevin Wolf fail:
3648061ca8a3SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
3649061ca8a3SKevin Wolf     return ret;
3650419b19d9SStefan Hajnoczi }
3651419b19d9SStefan Hajnoczi 
365220d97356SBlue Swirl /* XXX: put compressed sectors first, then all the cluster aligned
365320d97356SBlue Swirl    tables to avoid losing bytes in alignment */
3654fcccefc5SPavel Butsykin static coroutine_fn int
3655fcccefc5SPavel Butsykin qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
3656fcccefc5SPavel Butsykin                             uint64_t bytes, QEMUIOVector *qiov)
365720d97356SBlue Swirl {
3658ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3659fcccefc5SPavel Butsykin     QEMUIOVector hd_qiov;
3660fcccefc5SPavel Butsykin     struct iovec iov;
366120d97356SBlue Swirl     z_stream strm;
366220d97356SBlue Swirl     int ret, out_len;
3663fcccefc5SPavel Butsykin     uint8_t *buf, *out_buf;
3664d0d5d0e3SEric Blake     int64_t cluster_offset;
366520d97356SBlue Swirl 
3666fcccefc5SPavel Butsykin     if (bytes == 0) {
366720d97356SBlue Swirl         /* align end of file to a sector boundary to ease reading with
366820d97356SBlue Swirl            sector based I/Os */
36699a4f4c31SKevin Wolf         cluster_offset = bdrv_getlength(bs->file->bs);
3670d0d5d0e3SEric Blake         if (cluster_offset < 0) {
3671d0d5d0e3SEric Blake             return cluster_offset;
3672d0d5d0e3SEric Blake         }
3673061ca8a3SKevin Wolf         return bdrv_co_truncate(bs->file, cluster_offset, PREALLOC_MODE_OFF,
3674061ca8a3SKevin Wolf                                 NULL);
367520d97356SBlue Swirl     }
367620d97356SBlue Swirl 
36773e3b838fSAnton Nefedov     if (offset_into_cluster(s, offset)) {
36783e3b838fSAnton Nefedov         return -EINVAL;
36793e3b838fSAnton Nefedov     }
36803e3b838fSAnton Nefedov 
3681fcccefc5SPavel Butsykin     buf = qemu_blockalign(bs, s->cluster_size);
3682a2c0ca6fSPavel Butsykin     if (bytes != s->cluster_size) {
3683a2c0ca6fSPavel Butsykin         if (bytes > s->cluster_size ||
3684a2c0ca6fSPavel Butsykin             offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
3685a2c0ca6fSPavel Butsykin         {
3686a2c0ca6fSPavel Butsykin             qemu_vfree(buf);
3687a2c0ca6fSPavel Butsykin             return -EINVAL;
3688a2c0ca6fSPavel Butsykin         }
3689a2c0ca6fSPavel Butsykin         /* Zero-pad last write if image size is not cluster aligned */
3690a2c0ca6fSPavel Butsykin         memset(buf + bytes, 0, s->cluster_size - bytes);
3691a2c0ca6fSPavel Butsykin     }
36928b2bd093SPavel Butsykin     qemu_iovec_to_buf(qiov, 0, buf, bytes);
369320d97356SBlue Swirl 
3694ebf7bba0SVladimir Sementsov-Ogievskiy     out_buf = g_malloc(s->cluster_size);
369520d97356SBlue Swirl 
369620d97356SBlue Swirl     /* best compression, small window, no zlib header */
369720d97356SBlue Swirl     memset(&strm, 0, sizeof(strm));
369820d97356SBlue Swirl     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
369920d97356SBlue Swirl                        Z_DEFLATED, -12,
370020d97356SBlue Swirl                        9, Z_DEFAULT_STRATEGY);
370120d97356SBlue Swirl     if (ret != 0) {
37028f1efd00SKevin Wolf         ret = -EINVAL;
37038f1efd00SKevin Wolf         goto fail;
370420d97356SBlue Swirl     }
370520d97356SBlue Swirl 
370620d97356SBlue Swirl     strm.avail_in = s->cluster_size;
370720d97356SBlue Swirl     strm.next_in = (uint8_t *)buf;
370820d97356SBlue Swirl     strm.avail_out = s->cluster_size;
370920d97356SBlue Swirl     strm.next_out = out_buf;
371020d97356SBlue Swirl 
371120d97356SBlue Swirl     ret = deflate(&strm, Z_FINISH);
371220d97356SBlue Swirl     if (ret != Z_STREAM_END && ret != Z_OK) {
371320d97356SBlue Swirl         deflateEnd(&strm);
37148f1efd00SKevin Wolf         ret = -EINVAL;
37158f1efd00SKevin Wolf         goto fail;
371620d97356SBlue Swirl     }
371720d97356SBlue Swirl     out_len = strm.next_out - out_buf;
371820d97356SBlue Swirl 
371920d97356SBlue Swirl     deflateEnd(&strm);
372020d97356SBlue Swirl 
372120d97356SBlue Swirl     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
372220d97356SBlue Swirl         /* could not compress: write normal cluster */
3723fcccefc5SPavel Butsykin         ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
37248f1efd00SKevin Wolf         if (ret < 0) {
37258f1efd00SKevin Wolf             goto fail;
37268f1efd00SKevin Wolf         }
3727fcccefc5SPavel Butsykin         goto success;
3728fcccefc5SPavel Butsykin     }
3729fcccefc5SPavel Butsykin 
3730fcccefc5SPavel Butsykin     qemu_co_mutex_lock(&s->lock);
3731fcccefc5SPavel Butsykin     cluster_offset =
3732fcccefc5SPavel Butsykin         qcow2_alloc_compressed_cluster_offset(bs, offset, out_len);
37338f1efd00SKevin Wolf     if (!cluster_offset) {
3734fcccefc5SPavel Butsykin         qemu_co_mutex_unlock(&s->lock);
37358f1efd00SKevin Wolf         ret = -EIO;
37368f1efd00SKevin Wolf         goto fail;
37378f1efd00SKevin Wolf     }
373820d97356SBlue Swirl     cluster_offset &= s->cluster_offset_mask;
3739cf93980eSMax Reitz 
3740231bb267SMax Reitz     ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
3741fcccefc5SPavel Butsykin     qemu_co_mutex_unlock(&s->lock);
3742cf93980eSMax Reitz     if (ret < 0) {
3743cf93980eSMax Reitz         goto fail;
3744cf93980eSMax Reitz     }
3745cf93980eSMax Reitz 
3746fcccefc5SPavel Butsykin     iov = (struct iovec) {
3747fcccefc5SPavel Butsykin         .iov_base   = out_buf,
3748fcccefc5SPavel Butsykin         .iov_len    = out_len,
3749fcccefc5SPavel Butsykin     };
3750fcccefc5SPavel Butsykin     qemu_iovec_init_external(&hd_qiov, &iov, 1);
3751fcccefc5SPavel Butsykin 
375266f82ceeSKevin Wolf     BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
3753fcccefc5SPavel Butsykin     ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0);
37548f1efd00SKevin Wolf     if (ret < 0) {
37558f1efd00SKevin Wolf         goto fail;
375620d97356SBlue Swirl     }
3757fcccefc5SPavel Butsykin success:
37588f1efd00SKevin Wolf     ret = 0;
37598f1efd00SKevin Wolf fail:
3760fcccefc5SPavel Butsykin     qemu_vfree(buf);
37617267c094SAnthony Liguori     g_free(out_buf);
37628f1efd00SKevin Wolf     return ret;
376320d97356SBlue Swirl }
376420d97356SBlue Swirl 
376594054183SMax Reitz static int make_completely_empty(BlockDriverState *bs)
376694054183SMax Reitz {
3767ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3768ed3d2ec9SMax Reitz     Error *local_err = NULL;
376994054183SMax Reitz     int ret, l1_clusters;
377094054183SMax Reitz     int64_t offset;
377194054183SMax Reitz     uint64_t *new_reftable = NULL;
377294054183SMax Reitz     uint64_t rt_entry, l1_size2;
377394054183SMax Reitz     struct {
377494054183SMax Reitz         uint64_t l1_offset;
377594054183SMax Reitz         uint64_t reftable_offset;
377694054183SMax Reitz         uint32_t reftable_clusters;
377794054183SMax Reitz     } QEMU_PACKED l1_ofs_rt_ofs_cls;
377894054183SMax Reitz 
377994054183SMax Reitz     ret = qcow2_cache_empty(bs, s->l2_table_cache);
378094054183SMax Reitz     if (ret < 0) {
378194054183SMax Reitz         goto fail;
378294054183SMax Reitz     }
378394054183SMax Reitz 
378494054183SMax Reitz     ret = qcow2_cache_empty(bs, s->refcount_block_cache);
378594054183SMax Reitz     if (ret < 0) {
378694054183SMax Reitz         goto fail;
378794054183SMax Reitz     }
378894054183SMax Reitz 
378994054183SMax Reitz     /* Refcounts will be broken utterly */
379094054183SMax Reitz     ret = qcow2_mark_dirty(bs);
379194054183SMax Reitz     if (ret < 0) {
379294054183SMax Reitz         goto fail;
379394054183SMax Reitz     }
379494054183SMax Reitz 
379594054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
379694054183SMax Reitz 
379794054183SMax Reitz     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
379894054183SMax Reitz     l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
379994054183SMax Reitz 
380094054183SMax Reitz     /* After this call, neither the in-memory nor the on-disk refcount
380194054183SMax Reitz      * information accurately describe the actual references */
380294054183SMax Reitz 
3803720ff280SKevin Wolf     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
380474021bc4SEric Blake                              l1_clusters * s->cluster_size, 0);
380594054183SMax Reitz     if (ret < 0) {
380694054183SMax Reitz         goto fail_broken_refcounts;
380794054183SMax Reitz     }
380894054183SMax Reitz     memset(s->l1_table, 0, l1_size2);
380994054183SMax Reitz 
381094054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
381194054183SMax Reitz 
381294054183SMax Reitz     /* Overwrite enough clusters at the beginning of the sectors to place
381394054183SMax Reitz      * the refcount table, a refcount block and the L1 table in; this may
381494054183SMax Reitz      * overwrite parts of the existing refcount and L1 table, which is not
381594054183SMax Reitz      * an issue because the dirty flag is set, complete data loss is in fact
381694054183SMax Reitz      * desired and partial data loss is consequently fine as well */
3817720ff280SKevin Wolf     ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
381874021bc4SEric Blake                              (2 + l1_clusters) * s->cluster_size, 0);
381994054183SMax Reitz     /* This call (even if it failed overall) may have overwritten on-disk
382094054183SMax Reitz      * refcount structures; in that case, the in-memory refcount information
382194054183SMax Reitz      * will probably differ from the on-disk information which makes the BDS
382294054183SMax Reitz      * unusable */
382394054183SMax Reitz     if (ret < 0) {
382494054183SMax Reitz         goto fail_broken_refcounts;
382594054183SMax Reitz     }
382694054183SMax Reitz 
382794054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
382894054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
382994054183SMax Reitz 
383094054183SMax Reitz     /* "Create" an empty reftable (one cluster) directly after the image
383194054183SMax Reitz      * header and an empty L1 table three clusters after the image header;
383294054183SMax Reitz      * the cluster between those two will be used as the first refblock */
3833f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
3834f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
3835f1f7a1ddSPeter Maydell     l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
3836d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
383794054183SMax Reitz                            &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
383894054183SMax Reitz     if (ret < 0) {
383994054183SMax Reitz         goto fail_broken_refcounts;
384094054183SMax Reitz     }
384194054183SMax Reitz 
384294054183SMax Reitz     s->l1_table_offset = 3 * s->cluster_size;
384394054183SMax Reitz 
384494054183SMax Reitz     new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
384594054183SMax Reitz     if (!new_reftable) {
384694054183SMax Reitz         ret = -ENOMEM;
384794054183SMax Reitz         goto fail_broken_refcounts;
384894054183SMax Reitz     }
384994054183SMax Reitz 
385094054183SMax Reitz     s->refcount_table_offset = s->cluster_size;
385194054183SMax Reitz     s->refcount_table_size   = s->cluster_size / sizeof(uint64_t);
38527061a078SAlberto Garcia     s->max_refcount_table_index = 0;
385394054183SMax Reitz 
385494054183SMax Reitz     g_free(s->refcount_table);
385594054183SMax Reitz     s->refcount_table = new_reftable;
385694054183SMax Reitz     new_reftable = NULL;
385794054183SMax Reitz 
385894054183SMax Reitz     /* Now the in-memory refcount information again corresponds to the on-disk
385994054183SMax Reitz      * information (reftable is empty and no refblocks (the refblock cache is
386094054183SMax Reitz      * empty)); however, this means some clusters (e.g. the image header) are
386194054183SMax Reitz      * referenced, but not refcounted, but the normal qcow2 code assumes that
386294054183SMax Reitz      * the in-memory information is always correct */
386394054183SMax Reitz 
386494054183SMax Reitz     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
386594054183SMax Reitz 
386694054183SMax Reitz     /* Enter the first refblock into the reftable */
386794054183SMax Reitz     rt_entry = cpu_to_be64(2 * s->cluster_size);
3868d9ca2ea2SKevin Wolf     ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
386994054183SMax Reitz                            &rt_entry, sizeof(rt_entry));
387094054183SMax Reitz     if (ret < 0) {
387194054183SMax Reitz         goto fail_broken_refcounts;
387294054183SMax Reitz     }
387394054183SMax Reitz     s->refcount_table[0] = 2 * s->cluster_size;
387494054183SMax Reitz 
387594054183SMax Reitz     s->free_cluster_index = 0;
387694054183SMax Reitz     assert(3 + l1_clusters <= s->refcount_block_size);
387794054183SMax Reitz     offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
387894054183SMax Reitz     if (offset < 0) {
387994054183SMax Reitz         ret = offset;
388094054183SMax Reitz         goto fail_broken_refcounts;
388194054183SMax Reitz     } else if (offset > 0) {
388294054183SMax Reitz         error_report("First cluster in emptied image is in use");
388394054183SMax Reitz         abort();
388494054183SMax Reitz     }
388594054183SMax Reitz 
388694054183SMax Reitz     /* Now finally the in-memory information corresponds to the on-disk
388794054183SMax Reitz      * structures and is correct */
388894054183SMax Reitz     ret = qcow2_mark_clean(bs);
388994054183SMax Reitz     if (ret < 0) {
389094054183SMax Reitz         goto fail;
389194054183SMax Reitz     }
389294054183SMax Reitz 
3893ed3d2ec9SMax Reitz     ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
38947ea37c30SMax Reitz                         PREALLOC_MODE_OFF, &local_err);
389594054183SMax Reitz     if (ret < 0) {
3896ed3d2ec9SMax Reitz         error_report_err(local_err);
389794054183SMax Reitz         goto fail;
389894054183SMax Reitz     }
389994054183SMax Reitz 
390094054183SMax Reitz     return 0;
390194054183SMax Reitz 
390294054183SMax Reitz fail_broken_refcounts:
390394054183SMax Reitz     /* The BDS is unusable at this point. If we wanted to make it usable, we
390494054183SMax Reitz      * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
390594054183SMax Reitz      * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
390694054183SMax Reitz      * again. However, because the functions which could have caused this error
390794054183SMax Reitz      * path to be taken are used by those functions as well, it's very likely
390894054183SMax Reitz      * that that sequence will fail as well. Therefore, just eject the BDS. */
390994054183SMax Reitz     bs->drv = NULL;
391094054183SMax Reitz 
391194054183SMax Reitz fail:
391294054183SMax Reitz     g_free(new_reftable);
391394054183SMax Reitz     return ret;
391494054183SMax Reitz }
391594054183SMax Reitz 
3916491d27e2SMax Reitz static int qcow2_make_empty(BlockDriverState *bs)
3917491d27e2SMax Reitz {
3918ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
3919d2cb36afSEric Blake     uint64_t offset, end_offset;
3920d2cb36afSEric Blake     int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
392194054183SMax Reitz     int l1_clusters, ret = 0;
3922491d27e2SMax Reitz 
392394054183SMax Reitz     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
392494054183SMax Reitz 
39254096974eSEric Blake     if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
3926f0603329SDaniel P. Berrange         3 + l1_clusters <= s->refcount_block_size &&
3927f0603329SDaniel P. Berrange         s->crypt_method_header != QCOW_CRYPT_LUKS) {
39284096974eSEric Blake         /* The following function only works for qcow2 v3 images (it
39294096974eSEric Blake          * requires the dirty flag) and only as long as there are no
39304096974eSEric Blake          * features that reserve extra clusters (such as snapshots,
39314096974eSEric Blake          * LUKS header, or persistent bitmaps), because it completely
39324096974eSEric Blake          * empties the image.  Furthermore, the L1 table and three
39334096974eSEric Blake          * additional clusters (image header, refcount table, one
39344096974eSEric Blake          * refcount block) have to fit inside one refcount block. */
393594054183SMax Reitz         return make_completely_empty(bs);
393694054183SMax Reitz     }
393794054183SMax Reitz 
393894054183SMax Reitz     /* This fallback code simply discards every active cluster; this is slow,
393994054183SMax Reitz      * but works in all cases */
3940d2cb36afSEric Blake     end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3941d2cb36afSEric Blake     for (offset = 0; offset < end_offset; offset += step) {
3942491d27e2SMax Reitz         /* As this function is generally used after committing an external
3943491d27e2SMax Reitz          * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
3944491d27e2SMax Reitz          * default action for this kind of discard is to pass the discard,
3945491d27e2SMax Reitz          * which will ideally result in an actually smaller image file, as
3946491d27e2SMax Reitz          * is probably desired. */
3947d2cb36afSEric Blake         ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
3948491d27e2SMax Reitz                                     QCOW2_DISCARD_SNAPSHOT, true);
3949491d27e2SMax Reitz         if (ret < 0) {
3950491d27e2SMax Reitz             break;
3951491d27e2SMax Reitz         }
3952491d27e2SMax Reitz     }
3953491d27e2SMax Reitz 
3954491d27e2SMax Reitz     return ret;
3955491d27e2SMax Reitz }
3956491d27e2SMax Reitz 
3957a968168cSDong Xu Wang static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
395820d97356SBlue Swirl {
3959ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
396029c1a730SKevin Wolf     int ret;
396129c1a730SKevin Wolf 
39628b94ff85SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
39638b220eb7SPaolo Bonzini     ret = qcow2_write_caches(bs);
39648b94ff85SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
396529c1a730SKevin Wolf 
39668b220eb7SPaolo Bonzini     return ret;
3967eb489bb1SKevin Wolf }
3968eb489bb1SKevin Wolf 
3969c501c352SStefan Hajnoczi static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
3970c501c352SStefan Hajnoczi                                        Error **errp)
3971c501c352SStefan Hajnoczi {
3972c501c352SStefan Hajnoczi     Error *local_err = NULL;
3973c501c352SStefan Hajnoczi     BlockMeasureInfo *info;
3974c501c352SStefan Hajnoczi     uint64_t required = 0; /* bytes that contribute to required size */
3975c501c352SStefan Hajnoczi     uint64_t virtual_size; /* disk size as seen by guest */
3976c501c352SStefan Hajnoczi     uint64_t refcount_bits;
3977c501c352SStefan Hajnoczi     uint64_t l2_tables;
3978c501c352SStefan Hajnoczi     size_t cluster_size;
3979c501c352SStefan Hajnoczi     int version;
3980c501c352SStefan Hajnoczi     char *optstr;
3981c501c352SStefan Hajnoczi     PreallocMode prealloc;
3982c501c352SStefan Hajnoczi     bool has_backing_file;
3983c501c352SStefan Hajnoczi 
3984c501c352SStefan Hajnoczi     /* Parse image creation options */
3985c501c352SStefan Hajnoczi     cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
3986c501c352SStefan Hajnoczi     if (local_err) {
3987c501c352SStefan Hajnoczi         goto err;
3988c501c352SStefan Hajnoczi     }
3989c501c352SStefan Hajnoczi 
3990c501c352SStefan Hajnoczi     version = qcow2_opt_get_version_del(opts, &local_err);
3991c501c352SStefan Hajnoczi     if (local_err) {
3992c501c352SStefan Hajnoczi         goto err;
3993c501c352SStefan Hajnoczi     }
3994c501c352SStefan Hajnoczi 
3995c501c352SStefan Hajnoczi     refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
3996c501c352SStefan Hajnoczi     if (local_err) {
3997c501c352SStefan Hajnoczi         goto err;
3998c501c352SStefan Hajnoczi     }
3999c501c352SStefan Hajnoczi 
4000c501c352SStefan Hajnoczi     optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
4001f7abe0ecSMarc-André Lureau     prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
400206c60b6cSMarkus Armbruster                                PREALLOC_MODE_OFF, &local_err);
4003c501c352SStefan Hajnoczi     g_free(optstr);
4004c501c352SStefan Hajnoczi     if (local_err) {
4005c501c352SStefan Hajnoczi         goto err;
4006c501c352SStefan Hajnoczi     }
4007c501c352SStefan Hajnoczi 
4008c501c352SStefan Hajnoczi     optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
4009c501c352SStefan Hajnoczi     has_backing_file = !!optstr;
4010c501c352SStefan Hajnoczi     g_free(optstr);
4011c501c352SStefan Hajnoczi 
40129e029689SAlberto Garcia     virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
40139e029689SAlberto Garcia     virtual_size = ROUND_UP(virtual_size, cluster_size);
4014c501c352SStefan Hajnoczi 
4015c501c352SStefan Hajnoczi     /* Check that virtual disk size is valid */
4016c501c352SStefan Hajnoczi     l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
4017c501c352SStefan Hajnoczi                              cluster_size / sizeof(uint64_t));
4018c501c352SStefan Hajnoczi     if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
4019c501c352SStefan Hajnoczi         error_setg(&local_err, "The image size is too large "
4020c501c352SStefan Hajnoczi                                "(try using a larger cluster size)");
4021c501c352SStefan Hajnoczi         goto err;
4022c501c352SStefan Hajnoczi     }
4023c501c352SStefan Hajnoczi 
4024c501c352SStefan Hajnoczi     /* Account for input image */
4025c501c352SStefan Hajnoczi     if (in_bs) {
4026c501c352SStefan Hajnoczi         int64_t ssize = bdrv_getlength(in_bs);
4027c501c352SStefan Hajnoczi         if (ssize < 0) {
4028c501c352SStefan Hajnoczi             error_setg_errno(&local_err, -ssize,
4029c501c352SStefan Hajnoczi                              "Unable to get image virtual_size");
4030c501c352SStefan Hajnoczi             goto err;
4031c501c352SStefan Hajnoczi         }
4032c501c352SStefan Hajnoczi 
40339e029689SAlberto Garcia         virtual_size = ROUND_UP(ssize, cluster_size);
4034c501c352SStefan Hajnoczi 
4035c501c352SStefan Hajnoczi         if (has_backing_file) {
4036c501c352SStefan Hajnoczi             /* We don't how much of the backing chain is shared by the input
4037c501c352SStefan Hajnoczi              * image and the new image file.  In the worst case the new image's
4038c501c352SStefan Hajnoczi              * backing file has nothing in common with the input image.  Be
4039c501c352SStefan Hajnoczi              * conservative and assume all clusters need to be written.
4040c501c352SStefan Hajnoczi              */
4041c501c352SStefan Hajnoczi             required = virtual_size;
4042c501c352SStefan Hajnoczi         } else {
4043b85ee453SEric Blake             int64_t offset;
404431826642SEric Blake             int64_t pnum = 0;
4045c501c352SStefan Hajnoczi 
404631826642SEric Blake             for (offset = 0; offset < ssize; offset += pnum) {
404731826642SEric Blake                 int ret;
4048c501c352SStefan Hajnoczi 
404931826642SEric Blake                 ret = bdrv_block_status_above(in_bs, NULL, offset,
405031826642SEric Blake                                               ssize - offset, &pnum, NULL,
405131826642SEric Blake                                               NULL);
4052c501c352SStefan Hajnoczi                 if (ret < 0) {
4053c501c352SStefan Hajnoczi                     error_setg_errno(&local_err, -ret,
4054c501c352SStefan Hajnoczi                                      "Unable to get block status");
4055c501c352SStefan Hajnoczi                     goto err;
4056c501c352SStefan Hajnoczi                 }
4057c501c352SStefan Hajnoczi 
4058c501c352SStefan Hajnoczi                 if (ret & BDRV_BLOCK_ZERO) {
4059c501c352SStefan Hajnoczi                     /* Skip zero regions (safe with no backing file) */
4060c501c352SStefan Hajnoczi                 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
4061c501c352SStefan Hajnoczi                            (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
4062c501c352SStefan Hajnoczi                     /* Extend pnum to end of cluster for next iteration */
406331826642SEric Blake                     pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
4064c501c352SStefan Hajnoczi 
4065c501c352SStefan Hajnoczi                     /* Count clusters we've seen */
406631826642SEric Blake                     required += offset % cluster_size + pnum;
4067c501c352SStefan Hajnoczi                 }
4068c501c352SStefan Hajnoczi             }
4069c501c352SStefan Hajnoczi         }
4070c501c352SStefan Hajnoczi     }
4071c501c352SStefan Hajnoczi 
4072c501c352SStefan Hajnoczi     /* Take into account preallocation.  Nothing special is needed for
4073c501c352SStefan Hajnoczi      * PREALLOC_MODE_METADATA since metadata is always counted.
4074c501c352SStefan Hajnoczi      */
4075c501c352SStefan Hajnoczi     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
4076c501c352SStefan Hajnoczi         required = virtual_size;
4077c501c352SStefan Hajnoczi     }
4078c501c352SStefan Hajnoczi 
4079c501c352SStefan Hajnoczi     info = g_new(BlockMeasureInfo, 1);
4080c501c352SStefan Hajnoczi     info->fully_allocated =
4081c501c352SStefan Hajnoczi         qcow2_calc_prealloc_size(virtual_size, cluster_size,
4082c501c352SStefan Hajnoczi                                  ctz32(refcount_bits));
4083c501c352SStefan Hajnoczi 
4084c501c352SStefan Hajnoczi     /* Remove data clusters that are not required.  This overestimates the
4085c501c352SStefan Hajnoczi      * required size because metadata needed for the fully allocated file is
4086c501c352SStefan Hajnoczi      * still counted.
4087c501c352SStefan Hajnoczi      */
4088c501c352SStefan Hajnoczi     info->required = info->fully_allocated - virtual_size + required;
4089c501c352SStefan Hajnoczi     return info;
4090c501c352SStefan Hajnoczi 
4091c501c352SStefan Hajnoczi err:
4092c501c352SStefan Hajnoczi     error_propagate(errp, local_err);
4093c501c352SStefan Hajnoczi     return NULL;
4094c501c352SStefan Hajnoczi }
4095c501c352SStefan Hajnoczi 
40967c80ab3fSJes Sorensen static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
409720d97356SBlue Swirl {
4098ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
409995de6d70SPaolo Bonzini     bdi->unallocated_blocks_are_zero = true;
410020d97356SBlue Swirl     bdi->cluster_size = s->cluster_size;
41017c80ab3fSJes Sorensen     bdi->vm_state_offset = qcow2_vm_state_offset(s);
410220d97356SBlue Swirl     return 0;
410320d97356SBlue Swirl }
410420d97356SBlue Swirl 
410537764dfbSMax Reitz static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
410637764dfbSMax Reitz {
4107ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
41080a12f6f8SDaniel P. Berrange     ImageInfoSpecific *spec_info;
41090a12f6f8SDaniel P. Berrange     QCryptoBlockInfo *encrypt_info = NULL;
411037764dfbSMax Reitz 
41110a12f6f8SDaniel P. Berrange     if (s->crypto != NULL) {
41120a12f6f8SDaniel P. Berrange         encrypt_info = qcrypto_block_get_info(s->crypto, &error_abort);
41130a12f6f8SDaniel P. Berrange     }
41140a12f6f8SDaniel P. Berrange 
41150a12f6f8SDaniel P. Berrange     spec_info = g_new(ImageInfoSpecific, 1);
411637764dfbSMax Reitz     *spec_info = (ImageInfoSpecific){
41176a8f9661SEric Blake         .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
411832bafa8fSEric Blake         .u.qcow2.data = g_new(ImageInfoSpecificQCow2, 1),
411937764dfbSMax Reitz     };
412037764dfbSMax Reitz     if (s->qcow_version == 2) {
412132bafa8fSEric Blake         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
412237764dfbSMax Reitz             .compat             = g_strdup("0.10"),
41230709c5a1SMax Reitz             .refcount_bits      = s->refcount_bits,
412437764dfbSMax Reitz         };
412537764dfbSMax Reitz     } else if (s->qcow_version == 3) {
412632bafa8fSEric Blake         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
412737764dfbSMax Reitz             .compat             = g_strdup("1.1"),
412837764dfbSMax Reitz             .lazy_refcounts     = s->compatible_features &
412937764dfbSMax Reitz                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
413037764dfbSMax Reitz             .has_lazy_refcounts = true,
41319009b196SMax Reitz             .corrupt            = s->incompatible_features &
41329009b196SMax Reitz                                   QCOW2_INCOMPAT_CORRUPT,
41339009b196SMax Reitz             .has_corrupt        = true,
41340709c5a1SMax Reitz             .refcount_bits      = s->refcount_bits,
413537764dfbSMax Reitz         };
4136b1fc8f93SDenis V. Lunev     } else {
4137b1fc8f93SDenis V. Lunev         /* if this assertion fails, this probably means a new version was
4138b1fc8f93SDenis V. Lunev          * added without having it covered here */
4139b1fc8f93SDenis V. Lunev         assert(false);
414037764dfbSMax Reitz     }
414137764dfbSMax Reitz 
41420a12f6f8SDaniel P. Berrange     if (encrypt_info) {
41430a12f6f8SDaniel P. Berrange         ImageInfoSpecificQCow2Encryption *qencrypt =
41440a12f6f8SDaniel P. Berrange             g_new(ImageInfoSpecificQCow2Encryption, 1);
41450a12f6f8SDaniel P. Berrange         switch (encrypt_info->format) {
41460a12f6f8SDaniel P. Berrange         case Q_CRYPTO_BLOCK_FORMAT_QCOW:
41470a12f6f8SDaniel P. Berrange             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
41480a12f6f8SDaniel P. Berrange             break;
41490a12f6f8SDaniel P. Berrange         case Q_CRYPTO_BLOCK_FORMAT_LUKS:
41500a12f6f8SDaniel P. Berrange             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
41510a12f6f8SDaniel P. Berrange             qencrypt->u.luks = encrypt_info->u.luks;
41520a12f6f8SDaniel P. Berrange             break;
41530a12f6f8SDaniel P. Berrange         default:
41540a12f6f8SDaniel P. Berrange             abort();
41550a12f6f8SDaniel P. Berrange         }
41560a12f6f8SDaniel P. Berrange         /* Since we did shallow copy above, erase any pointers
41570a12f6f8SDaniel P. Berrange          * in the original info */
41580a12f6f8SDaniel P. Berrange         memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
41590a12f6f8SDaniel P. Berrange         qapi_free_QCryptoBlockInfo(encrypt_info);
41600a12f6f8SDaniel P. Berrange 
41610a12f6f8SDaniel P. Berrange         spec_info->u.qcow2.data->has_encrypt = true;
41620a12f6f8SDaniel P. Berrange         spec_info->u.qcow2.data->encrypt = qencrypt;
41630a12f6f8SDaniel P. Berrange     }
41640a12f6f8SDaniel P. Berrange 
416537764dfbSMax Reitz     return spec_info;
416637764dfbSMax Reitz }
416737764dfbSMax Reitz 
4168cf8074b3SKevin Wolf static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4169cf8074b3SKevin Wolf                               int64_t pos)
417020d97356SBlue Swirl {
4171ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
417220d97356SBlue Swirl 
417366f82ceeSKevin Wolf     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
4174734a7758SKevin Wolf     return bs->drv->bdrv_co_pwritev(bs, qcow2_vm_state_offset(s) + pos,
4175734a7758SKevin Wolf                                     qiov->size, qiov, 0);
417620d97356SBlue Swirl }
417720d97356SBlue Swirl 
41785ddda0b8SKevin Wolf static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
41795ddda0b8SKevin Wolf                               int64_t pos)
418020d97356SBlue Swirl {
4181ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
418220d97356SBlue Swirl 
418366f82ceeSKevin Wolf     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
4184734a7758SKevin Wolf     return bs->drv->bdrv_co_preadv(bs, qcow2_vm_state_offset(s) + pos,
4185734a7758SKevin Wolf                                    qiov->size, qiov, 0);
418620d97356SBlue Swirl }
418720d97356SBlue Swirl 
41889296b3edSMax Reitz /*
41899296b3edSMax Reitz  * Downgrades an image's version. To achieve this, any incompatible features
41909296b3edSMax Reitz  * have to be removed.
41919296b3edSMax Reitz  */
41924057a2b2SMax Reitz static int qcow2_downgrade(BlockDriverState *bs, int target_version,
4193d1402b50SMax Reitz                            BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
4194d1402b50SMax Reitz                            Error **errp)
41959296b3edSMax Reitz {
4196ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
41979296b3edSMax Reitz     int current_version = s->qcow_version;
41989296b3edSMax Reitz     int ret;
41999296b3edSMax Reitz 
4200d1402b50SMax Reitz     /* This is qcow2_downgrade(), not qcow2_upgrade() */
4201d1402b50SMax Reitz     assert(target_version < current_version);
4202d1402b50SMax Reitz 
4203d1402b50SMax Reitz     /* There are no other versions (now) that you can downgrade to */
4204d1402b50SMax Reitz     assert(target_version == 2);
42059296b3edSMax Reitz 
42069296b3edSMax Reitz     if (s->refcount_order != 4) {
4207d1402b50SMax Reitz         error_setg(errp, "compat=0.10 requires refcount_bits=16");
42089296b3edSMax Reitz         return -ENOTSUP;
42099296b3edSMax Reitz     }
42109296b3edSMax Reitz 
42119296b3edSMax Reitz     /* clear incompatible features */
42129296b3edSMax Reitz     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
42139296b3edSMax Reitz         ret = qcow2_mark_clean(bs);
42149296b3edSMax Reitz         if (ret < 0) {
4215d1402b50SMax Reitz             error_setg_errno(errp, -ret, "Failed to make the image clean");
42169296b3edSMax Reitz             return ret;
42179296b3edSMax Reitz         }
42189296b3edSMax Reitz     }
42199296b3edSMax Reitz 
42209296b3edSMax Reitz     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
42219296b3edSMax Reitz      * the first place; if that happens nonetheless, returning -ENOTSUP is the
42229296b3edSMax Reitz      * best thing to do anyway */
42239296b3edSMax Reitz 
42249296b3edSMax Reitz     if (s->incompatible_features) {
4225d1402b50SMax Reitz         error_setg(errp, "Cannot downgrade an image with incompatible features "
4226d1402b50SMax Reitz                    "%#" PRIx64 " set", s->incompatible_features);
42279296b3edSMax Reitz         return -ENOTSUP;
42289296b3edSMax Reitz     }
42299296b3edSMax Reitz 
42309296b3edSMax Reitz     /* since we can ignore compatible features, we can set them to 0 as well */
42319296b3edSMax Reitz     s->compatible_features = 0;
42329296b3edSMax Reitz     /* if lazy refcounts have been used, they have already been fixed through
42339296b3edSMax Reitz      * clearing the dirty flag */
42349296b3edSMax Reitz 
42359296b3edSMax Reitz     /* clearing autoclear features is trivial */
42369296b3edSMax Reitz     s->autoclear_features = 0;
42379296b3edSMax Reitz 
42388b13976dSMax Reitz     ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
42399296b3edSMax Reitz     if (ret < 0) {
4240d1402b50SMax Reitz         error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
42419296b3edSMax Reitz         return ret;
42429296b3edSMax Reitz     }
42439296b3edSMax Reitz 
42449296b3edSMax Reitz     s->qcow_version = target_version;
42459296b3edSMax Reitz     ret = qcow2_update_header(bs);
42469296b3edSMax Reitz     if (ret < 0) {
42479296b3edSMax Reitz         s->qcow_version = current_version;
4248d1402b50SMax Reitz         error_setg_errno(errp, -ret, "Failed to update the image header");
42499296b3edSMax Reitz         return ret;
42509296b3edSMax Reitz     }
42519296b3edSMax Reitz     return 0;
42529296b3edSMax Reitz }
42539296b3edSMax Reitz 
4254c293a809SMax Reitz typedef enum Qcow2AmendOperation {
4255c293a809SMax Reitz     /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
4256c293a809SMax Reitz      * statically initialized to so that the helper CB can discern the first
4257c293a809SMax Reitz      * invocation from an operation change */
4258c293a809SMax Reitz     QCOW2_NO_OPERATION = 0,
4259c293a809SMax Reitz 
426061ce55fcSMax Reitz     QCOW2_CHANGING_REFCOUNT_ORDER,
4261c293a809SMax Reitz     QCOW2_DOWNGRADING,
4262c293a809SMax Reitz } Qcow2AmendOperation;
4263c293a809SMax Reitz 
4264c293a809SMax Reitz typedef struct Qcow2AmendHelperCBInfo {
4265c293a809SMax Reitz     /* The code coordinating the amend operations should only modify
4266c293a809SMax Reitz      * these four fields; the rest will be managed by the CB */
4267c293a809SMax Reitz     BlockDriverAmendStatusCB *original_status_cb;
4268c293a809SMax Reitz     void *original_cb_opaque;
4269c293a809SMax Reitz 
4270c293a809SMax Reitz     Qcow2AmendOperation current_operation;
4271c293a809SMax Reitz 
4272c293a809SMax Reitz     /* Total number of operations to perform (only set once) */
4273c293a809SMax Reitz     int total_operations;
4274c293a809SMax Reitz 
4275c293a809SMax Reitz     /* The following fields are managed by the CB */
4276c293a809SMax Reitz 
4277c293a809SMax Reitz     /* Number of operations completed */
4278c293a809SMax Reitz     int operations_completed;
4279c293a809SMax Reitz 
4280c293a809SMax Reitz     /* Cumulative offset of all completed operations */
4281c293a809SMax Reitz     int64_t offset_completed;
4282c293a809SMax Reitz 
4283c293a809SMax Reitz     Qcow2AmendOperation last_operation;
4284c293a809SMax Reitz     int64_t last_work_size;
4285c293a809SMax Reitz } Qcow2AmendHelperCBInfo;
4286c293a809SMax Reitz 
4287c293a809SMax Reitz static void qcow2_amend_helper_cb(BlockDriverState *bs,
4288c293a809SMax Reitz                                   int64_t operation_offset,
4289c293a809SMax Reitz                                   int64_t operation_work_size, void *opaque)
4290c293a809SMax Reitz {
4291c293a809SMax Reitz     Qcow2AmendHelperCBInfo *info = opaque;
4292c293a809SMax Reitz     int64_t current_work_size;
4293c293a809SMax Reitz     int64_t projected_work_size;
4294c293a809SMax Reitz 
4295c293a809SMax Reitz     if (info->current_operation != info->last_operation) {
4296c293a809SMax Reitz         if (info->last_operation != QCOW2_NO_OPERATION) {
4297c293a809SMax Reitz             info->offset_completed += info->last_work_size;
4298c293a809SMax Reitz             info->operations_completed++;
4299c293a809SMax Reitz         }
4300c293a809SMax Reitz 
4301c293a809SMax Reitz         info->last_operation = info->current_operation;
4302c293a809SMax Reitz     }
4303c293a809SMax Reitz 
4304c293a809SMax Reitz     assert(info->total_operations > 0);
4305c293a809SMax Reitz     assert(info->operations_completed < info->total_operations);
4306c293a809SMax Reitz 
4307c293a809SMax Reitz     info->last_work_size = operation_work_size;
4308c293a809SMax Reitz 
4309c293a809SMax Reitz     current_work_size = info->offset_completed + operation_work_size;
4310c293a809SMax Reitz 
4311c293a809SMax Reitz     /* current_work_size is the total work size for (operations_completed + 1)
4312c293a809SMax Reitz      * operations (which includes this one), so multiply it by the number of
4313c293a809SMax Reitz      * operations not covered and divide it by the number of operations
4314c293a809SMax Reitz      * covered to get a projection for the operations not covered */
4315c293a809SMax Reitz     projected_work_size = current_work_size * (info->total_operations -
4316c293a809SMax Reitz                                                info->operations_completed - 1)
4317c293a809SMax Reitz                                             / (info->operations_completed + 1);
4318c293a809SMax Reitz 
4319c293a809SMax Reitz     info->original_status_cb(bs, info->offset_completed + operation_offset,
4320c293a809SMax Reitz                              current_work_size + projected_work_size,
4321c293a809SMax Reitz                              info->original_cb_opaque);
4322c293a809SMax Reitz }
4323c293a809SMax Reitz 
432477485434SMax Reitz static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
43258b13976dSMax Reitz                                BlockDriverAmendStatusCB *status_cb,
4326d1402b50SMax Reitz                                void *cb_opaque,
4327d1402b50SMax Reitz                                Error **errp)
43289296b3edSMax Reitz {
4329ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
43309296b3edSMax Reitz     int old_version = s->qcow_version, new_version = old_version;
43319296b3edSMax Reitz     uint64_t new_size = 0;
43329296b3edSMax Reitz     const char *backing_file = NULL, *backing_format = NULL;
43339296b3edSMax Reitz     bool lazy_refcounts = s->use_lazy_refcounts;
43341bd0e2d1SChunyan Liu     const char *compat = NULL;
43351bd0e2d1SChunyan Liu     uint64_t cluster_size = s->cluster_size;
43361bd0e2d1SChunyan Liu     bool encrypt;
43374652b8f3SDaniel P. Berrange     int encformat;
433861ce55fcSMax Reitz     int refcount_bits = s->refcount_bits;
43399296b3edSMax Reitz     int ret;
43401bd0e2d1SChunyan Liu     QemuOptDesc *desc = opts->list->desc;
4341c293a809SMax Reitz     Qcow2AmendHelperCBInfo helper_cb_info;
43429296b3edSMax Reitz 
43431bd0e2d1SChunyan Liu     while (desc && desc->name) {
43441bd0e2d1SChunyan Liu         if (!qemu_opt_find(opts, desc->name)) {
43459296b3edSMax Reitz             /* only change explicitly defined options */
43461bd0e2d1SChunyan Liu             desc++;
43479296b3edSMax Reitz             continue;
43489296b3edSMax Reitz         }
43499296b3edSMax Reitz 
43508a17b83cSMax Reitz         if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
43518a17b83cSMax Reitz             compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
43521bd0e2d1SChunyan Liu             if (!compat) {
43539296b3edSMax Reitz                 /* preserve default */
43541bd0e2d1SChunyan Liu             } else if (!strcmp(compat, "0.10")) {
43559296b3edSMax Reitz                 new_version = 2;
43561bd0e2d1SChunyan Liu             } else if (!strcmp(compat, "1.1")) {
43579296b3edSMax Reitz                 new_version = 3;
43589296b3edSMax Reitz             } else {
4359d1402b50SMax Reitz                 error_setg(errp, "Unknown compatibility level %s", compat);
43609296b3edSMax Reitz                 return -EINVAL;
43619296b3edSMax Reitz             }
43628a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
4363d1402b50SMax Reitz             error_setg(errp, "Cannot change preallocation mode");
43649296b3edSMax Reitz             return -ENOTSUP;
43658a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
43668a17b83cSMax Reitz             new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
43678a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
43688a17b83cSMax Reitz             backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
43698a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
43708a17b83cSMax Reitz             backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
43718a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
43728a17b83cSMax Reitz             encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
4373b25b387fSDaniel P. Berrange                                         !!s->crypto);
4374f6fa64f6SDaniel P. Berrange 
4375b25b387fSDaniel P. Berrange             if (encrypt != !!s->crypto) {
4376d1402b50SMax Reitz                 error_setg(errp,
4377d1402b50SMax Reitz                            "Changing the encryption flag is not supported");
43789296b3edSMax Reitz                 return -ENOTSUP;
43799296b3edSMax Reitz             }
43804652b8f3SDaniel P. Berrange         } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) {
43814652b8f3SDaniel P. Berrange             encformat = qcow2_crypt_method_from_format(
43824652b8f3SDaniel P. Berrange                 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT));
43834652b8f3SDaniel P. Berrange 
43844652b8f3SDaniel P. Berrange             if (encformat != s->crypt_method_header) {
4385d1402b50SMax Reitz                 error_setg(errp,
4386d1402b50SMax Reitz                            "Changing the encryption format is not supported");
43874652b8f3SDaniel P. Berrange                 return -ENOTSUP;
43884652b8f3SDaniel P. Berrange             }
4389f66afbe2SDaniel P. Berrange         } else if (g_str_has_prefix(desc->name, "encrypt.")) {
4390d1402b50SMax Reitz             error_setg(errp,
4391d1402b50SMax Reitz                        "Changing the encryption parameters is not supported");
4392f66afbe2SDaniel P. Berrange             return -ENOTSUP;
43938a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
43948a17b83cSMax Reitz             cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
43951bd0e2d1SChunyan Liu                                              cluster_size);
43961bd0e2d1SChunyan Liu             if (cluster_size != s->cluster_size) {
4397d1402b50SMax Reitz                 error_setg(errp, "Changing the cluster size is not supported");
43989296b3edSMax Reitz                 return -ENOTSUP;
43999296b3edSMax Reitz             }
44008a17b83cSMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
44018a17b83cSMax Reitz             lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
44021bd0e2d1SChunyan Liu                                                lazy_refcounts);
440306d05fa7SMax Reitz         } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
440461ce55fcSMax Reitz             refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
440561ce55fcSMax Reitz                                                 refcount_bits);
440661ce55fcSMax Reitz 
440761ce55fcSMax Reitz             if (refcount_bits <= 0 || refcount_bits > 64 ||
440861ce55fcSMax Reitz                 !is_power_of_2(refcount_bits))
440961ce55fcSMax Reitz             {
4410d1402b50SMax Reitz                 error_setg(errp, "Refcount width must be a power of two and "
4411d1402b50SMax Reitz                            "may not exceed 64 bits");
441261ce55fcSMax Reitz                 return -EINVAL;
441361ce55fcSMax Reitz             }
44149296b3edSMax Reitz         } else {
4415164e0f89SMax Reitz             /* if this point is reached, this probably means a new option was
44169296b3edSMax Reitz              * added without having it covered here */
4417164e0f89SMax Reitz             abort();
44189296b3edSMax Reitz         }
44191bd0e2d1SChunyan Liu 
44201bd0e2d1SChunyan Liu         desc++;
44219296b3edSMax Reitz     }
44229296b3edSMax Reitz 
4423c293a809SMax Reitz     helper_cb_info = (Qcow2AmendHelperCBInfo){
4424c293a809SMax Reitz         .original_status_cb = status_cb,
4425c293a809SMax Reitz         .original_cb_opaque = cb_opaque,
4426c293a809SMax Reitz         .total_operations = (new_version < old_version)
442761ce55fcSMax Reitz                           + (s->refcount_bits != refcount_bits)
4428c293a809SMax Reitz     };
4429c293a809SMax Reitz 
44301038bbb8SMax Reitz     /* Upgrade first (some features may require compat=1.1) */
44319296b3edSMax Reitz     if (new_version > old_version) {
44329296b3edSMax Reitz         s->qcow_version = new_version;
44339296b3edSMax Reitz         ret = qcow2_update_header(bs);
44349296b3edSMax Reitz         if (ret < 0) {
44359296b3edSMax Reitz             s->qcow_version = old_version;
4436d1402b50SMax Reitz             error_setg_errno(errp, -ret, "Failed to update the image header");
44379296b3edSMax Reitz             return ret;
44389296b3edSMax Reitz         }
44399296b3edSMax Reitz     }
44409296b3edSMax Reitz 
444161ce55fcSMax Reitz     if (s->refcount_bits != refcount_bits) {
444261ce55fcSMax Reitz         int refcount_order = ctz32(refcount_bits);
444361ce55fcSMax Reitz 
444461ce55fcSMax Reitz         if (new_version < 3 && refcount_bits != 16) {
4445d1402b50SMax Reitz             error_setg(errp, "Refcount widths other than 16 bits require "
444661ce55fcSMax Reitz                        "compatibility level 1.1 or above (use compat=1.1 or "
444761ce55fcSMax Reitz                        "greater)");
444861ce55fcSMax Reitz             return -EINVAL;
444961ce55fcSMax Reitz         }
445061ce55fcSMax Reitz 
445161ce55fcSMax Reitz         helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
445261ce55fcSMax Reitz         ret = qcow2_change_refcount_order(bs, refcount_order,
445361ce55fcSMax Reitz                                           &qcow2_amend_helper_cb,
4454d1402b50SMax Reitz                                           &helper_cb_info, errp);
445561ce55fcSMax Reitz         if (ret < 0) {
445661ce55fcSMax Reitz             return ret;
445761ce55fcSMax Reitz         }
445861ce55fcSMax Reitz     }
445961ce55fcSMax Reitz 
44609296b3edSMax Reitz     if (backing_file || backing_format) {
4461e4603fe1SKevin Wolf         ret = qcow2_change_backing_file(bs,
4462e4603fe1SKevin Wolf                     backing_file ?: s->image_backing_file,
4463e4603fe1SKevin Wolf                     backing_format ?: s->image_backing_format);
44649296b3edSMax Reitz         if (ret < 0) {
4465d1402b50SMax Reitz             error_setg_errno(errp, -ret, "Failed to change the backing file");
44669296b3edSMax Reitz             return ret;
44679296b3edSMax Reitz         }
44689296b3edSMax Reitz     }
44699296b3edSMax Reitz 
44709296b3edSMax Reitz     if (s->use_lazy_refcounts != lazy_refcounts) {
44719296b3edSMax Reitz         if (lazy_refcounts) {
44721038bbb8SMax Reitz             if (new_version < 3) {
4473d1402b50SMax Reitz                 error_setg(errp, "Lazy refcounts only supported with "
4474d1402b50SMax Reitz                            "compatibility level 1.1 and above (use compat=1.1 "
4475d1402b50SMax Reitz                            "or greater)");
44769296b3edSMax Reitz                 return -EINVAL;
44779296b3edSMax Reitz             }
44789296b3edSMax Reitz             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
44799296b3edSMax Reitz             ret = qcow2_update_header(bs);
44809296b3edSMax Reitz             if (ret < 0) {
44819296b3edSMax Reitz                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4482d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update the image header");
44839296b3edSMax Reitz                 return ret;
44849296b3edSMax Reitz             }
44859296b3edSMax Reitz             s->use_lazy_refcounts = true;
44869296b3edSMax Reitz         } else {
44879296b3edSMax Reitz             /* make image clean first */
44889296b3edSMax Reitz             ret = qcow2_mark_clean(bs);
44899296b3edSMax Reitz             if (ret < 0) {
4490d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to make the image clean");
44919296b3edSMax Reitz                 return ret;
44929296b3edSMax Reitz             }
44939296b3edSMax Reitz             /* now disallow lazy refcounts */
44949296b3edSMax Reitz             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
44959296b3edSMax Reitz             ret = qcow2_update_header(bs);
44969296b3edSMax Reitz             if (ret < 0) {
44979296b3edSMax Reitz                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4498d1402b50SMax Reitz                 error_setg_errno(errp, -ret, "Failed to update the image header");
44999296b3edSMax Reitz                 return ret;
45009296b3edSMax Reitz             }
45019296b3edSMax Reitz             s->use_lazy_refcounts = false;
45029296b3edSMax Reitz         }
45039296b3edSMax Reitz     }
45049296b3edSMax Reitz 
45059296b3edSMax Reitz     if (new_size) {
45066d0eb64dSKevin Wolf         BlockBackend *blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
4507d1402b50SMax Reitz         ret = blk_insert_bs(blk, bs, errp);
4508d7086422SKevin Wolf         if (ret < 0) {
4509d7086422SKevin Wolf             blk_unref(blk);
4510d7086422SKevin Wolf             return ret;
4511d7086422SKevin Wolf         }
4512d7086422SKevin Wolf 
4513d1402b50SMax Reitz         ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, errp);
451470b27f36SKevin Wolf         blk_unref(blk);
45159296b3edSMax Reitz         if (ret < 0) {
45169296b3edSMax Reitz             return ret;
45179296b3edSMax Reitz         }
45189296b3edSMax Reitz     }
45199296b3edSMax Reitz 
45201038bbb8SMax Reitz     /* Downgrade last (so unsupported features can be removed before) */
45211038bbb8SMax Reitz     if (new_version < old_version) {
4522c293a809SMax Reitz         helper_cb_info.current_operation = QCOW2_DOWNGRADING;
4523c293a809SMax Reitz         ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
4524d1402b50SMax Reitz                               &helper_cb_info, errp);
45251038bbb8SMax Reitz         if (ret < 0) {
45261038bbb8SMax Reitz             return ret;
45271038bbb8SMax Reitz         }
45281038bbb8SMax Reitz     }
45291038bbb8SMax Reitz 
45309296b3edSMax Reitz     return 0;
45319296b3edSMax Reitz }
45329296b3edSMax Reitz 
453385186ebdSMax Reitz /*
453485186ebdSMax Reitz  * If offset or size are negative, respectively, they will not be included in
453585186ebdSMax Reitz  * the BLOCK_IMAGE_CORRUPTED event emitted.
453685186ebdSMax Reitz  * fatal will be ignored for read-only BDS; corruptions found there will always
453785186ebdSMax Reitz  * be considered non-fatal.
453885186ebdSMax Reitz  */
453985186ebdSMax Reitz void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
454085186ebdSMax Reitz                              int64_t size, const char *message_format, ...)
454185186ebdSMax Reitz {
4542ff99129aSKevin Wolf     BDRVQcow2State *s = bs->opaque;
4543dc881b44SAlberto Garcia     const char *node_name;
454485186ebdSMax Reitz     char *message;
454585186ebdSMax Reitz     va_list ap;
454685186ebdSMax Reitz 
4547ddf3b47eSMax Reitz     fatal = fatal && bdrv_is_writable(bs);
454885186ebdSMax Reitz 
454985186ebdSMax Reitz     if (s->signaled_corruption &&
455085186ebdSMax Reitz         (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
455185186ebdSMax Reitz     {
455285186ebdSMax Reitz         return;
455385186ebdSMax Reitz     }
455485186ebdSMax Reitz 
455585186ebdSMax Reitz     va_start(ap, message_format);
455685186ebdSMax Reitz     message = g_strdup_vprintf(message_format, ap);
455785186ebdSMax Reitz     va_end(ap);
455885186ebdSMax Reitz 
455985186ebdSMax Reitz     if (fatal) {
456085186ebdSMax Reitz         fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
456185186ebdSMax Reitz                 "corruption events will be suppressed\n", message);
456285186ebdSMax Reitz     } else {
456385186ebdSMax Reitz         fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
456485186ebdSMax Reitz                 "corruption events will be suppressed\n", message);
456585186ebdSMax Reitz     }
456685186ebdSMax Reitz 
4567dc881b44SAlberto Garcia     node_name = bdrv_get_node_name(bs);
4568dc881b44SAlberto Garcia     qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
4569dc881b44SAlberto Garcia                                           *node_name != '\0', node_name,
4570dc881b44SAlberto Garcia                                           message, offset >= 0, offset,
4571dc881b44SAlberto Garcia                                           size >= 0, size,
457285186ebdSMax Reitz                                           fatal, &error_abort);
457385186ebdSMax Reitz     g_free(message);
457485186ebdSMax Reitz 
457585186ebdSMax Reitz     if (fatal) {
457685186ebdSMax Reitz         qcow2_mark_corrupt(bs);
457785186ebdSMax Reitz         bs->drv = NULL; /* make BDS unusable */
457885186ebdSMax Reitz     }
457985186ebdSMax Reitz 
458085186ebdSMax Reitz     s->signaled_corruption = true;
458185186ebdSMax Reitz }
458285186ebdSMax Reitz 
45831bd0e2d1SChunyan Liu static QemuOptsList qcow2_create_opts = {
45841bd0e2d1SChunyan Liu     .name = "qcow2-create-opts",
45851bd0e2d1SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
45861bd0e2d1SChunyan Liu     .desc = {
458720d97356SBlue Swirl         {
458820d97356SBlue Swirl             .name = BLOCK_OPT_SIZE,
45891bd0e2d1SChunyan Liu             .type = QEMU_OPT_SIZE,
459020d97356SBlue Swirl             .help = "Virtual disk size"
459120d97356SBlue Swirl         },
459220d97356SBlue Swirl         {
45936744cbabSKevin Wolf             .name = BLOCK_OPT_COMPAT_LEVEL,
45941bd0e2d1SChunyan Liu             .type = QEMU_OPT_STRING,
45956744cbabSKevin Wolf             .help = "Compatibility level (0.10 or 1.1)"
45966744cbabSKevin Wolf         },
45976744cbabSKevin Wolf         {
459820d97356SBlue Swirl             .name = BLOCK_OPT_BACKING_FILE,
45991bd0e2d1SChunyan Liu             .type = QEMU_OPT_STRING,
460020d97356SBlue Swirl             .help = "File name of a base image"
460120d97356SBlue Swirl         },
460220d97356SBlue Swirl         {
460320d97356SBlue Swirl             .name = BLOCK_OPT_BACKING_FMT,
46041bd0e2d1SChunyan Liu             .type = QEMU_OPT_STRING,
460520d97356SBlue Swirl             .help = "Image format of the base image"
460620d97356SBlue Swirl         },
460720d97356SBlue Swirl         {
460820d97356SBlue Swirl             .name = BLOCK_OPT_ENCRYPT,
46091bd0e2d1SChunyan Liu             .type = QEMU_OPT_BOOL,
46100cb8d47bSDaniel P. Berrange             .help = "Encrypt the image with format 'aes'. (Deprecated "
46110cb8d47bSDaniel P. Berrange                     "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
46120cb8d47bSDaniel P. Berrange         },
46130cb8d47bSDaniel P. Berrange         {
46140cb8d47bSDaniel P. Berrange             .name = BLOCK_OPT_ENCRYPT_FORMAT,
46150cb8d47bSDaniel P. Berrange             .type = QEMU_OPT_STRING,
46164652b8f3SDaniel P. Berrange             .help = "Encrypt the image, format choices: 'aes', 'luks'",
461720d97356SBlue Swirl         },
46184652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
46194652b8f3SDaniel P. Berrange             "ID of secret providing qcow AES key or LUKS passphrase"),
46204652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
46214652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
46224652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
46234652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
46244652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
46254652b8f3SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
462620d97356SBlue Swirl         {
462720d97356SBlue Swirl             .name = BLOCK_OPT_CLUSTER_SIZE,
46281bd0e2d1SChunyan Liu             .type = QEMU_OPT_SIZE,
462999cce9faSKevin Wolf             .help = "qcow2 cluster size",
46301bd0e2d1SChunyan Liu             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
463120d97356SBlue Swirl         },
463220d97356SBlue Swirl         {
463320d97356SBlue Swirl             .name = BLOCK_OPT_PREALLOC,
46341bd0e2d1SChunyan Liu             .type = QEMU_OPT_STRING,
46350e4271b7SHu Tao             .help = "Preallocation mode (allowed values: off, metadata, "
46360e4271b7SHu Tao                     "falloc, full)"
463720d97356SBlue Swirl         },
4638bfe8043eSStefan Hajnoczi         {
4639bfe8043eSStefan Hajnoczi             .name = BLOCK_OPT_LAZY_REFCOUNTS,
46401bd0e2d1SChunyan Liu             .type = QEMU_OPT_BOOL,
4641bfe8043eSStefan Hajnoczi             .help = "Postpone refcount updates",
46421bd0e2d1SChunyan Liu             .def_value_str = "off"
4643bfe8043eSStefan Hajnoczi         },
464406d05fa7SMax Reitz         {
464506d05fa7SMax Reitz             .name = BLOCK_OPT_REFCOUNT_BITS,
464606d05fa7SMax Reitz             .type = QEMU_OPT_NUMBER,
464706d05fa7SMax Reitz             .help = "Width of a reference count entry in bits",
464806d05fa7SMax Reitz             .def_value_str = "16"
464906d05fa7SMax Reitz         },
46501bd0e2d1SChunyan Liu         { /* end of list */ }
46511bd0e2d1SChunyan Liu     }
465220d97356SBlue Swirl };
465320d97356SBlue Swirl 
46545f535a94SMax Reitz BlockDriver bdrv_qcow2 = {
465520d97356SBlue Swirl     .format_name        = "qcow2",
4656ff99129aSKevin Wolf     .instance_size      = sizeof(BDRVQcow2State),
46577c80ab3fSJes Sorensen     .bdrv_probe         = qcow2_probe,
46587c80ab3fSJes Sorensen     .bdrv_open          = qcow2_open,
46597c80ab3fSJes Sorensen     .bdrv_close         = qcow2_close,
466021d82ac9SJeff Cody     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
46615b0959a7SKevin Wolf     .bdrv_reopen_commit   = qcow2_reopen_commit,
46625b0959a7SKevin Wolf     .bdrv_reopen_abort    = qcow2_reopen_abort,
46635365f44dSKevin Wolf     .bdrv_join_options    = qcow2_join_options,
4664862f215fSKevin Wolf     .bdrv_child_perm      = bdrv_format_default_perms,
4665efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts  = qcow2_co_create_opts,
4666b0292b85SKevin Wolf     .bdrv_co_create       = qcow2_co_create,
46673ac21627SPeter Lieven     .bdrv_has_zero_init = bdrv_has_zero_init_1,
4668a320fb04SEric Blake     .bdrv_co_block_status = qcow2_co_block_status,
466920d97356SBlue Swirl 
4670ecfe1863SKevin Wolf     .bdrv_co_preadv         = qcow2_co_preadv,
4671d46a0bb2SKevin Wolf     .bdrv_co_pwritev        = qcow2_co_pwritev,
4672eb489bb1SKevin Wolf     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
4673419b19d9SStefan Hajnoczi 
46745544b59fSEric Blake     .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
467582e8a788SEric Blake     .bdrv_co_pdiscard       = qcow2_co_pdiscard,
4676fd9fcd37SFam Zheng     .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
4677fd9fcd37SFam Zheng     .bdrv_co_copy_range_to  = qcow2_co_copy_range_to,
4678061ca8a3SKevin Wolf     .bdrv_co_truncate       = qcow2_co_truncate,
4679fcccefc5SPavel Butsykin     .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
4680491d27e2SMax Reitz     .bdrv_make_empty        = qcow2_make_empty,
468120d97356SBlue Swirl 
468220d97356SBlue Swirl     .bdrv_snapshot_create   = qcow2_snapshot_create,
468320d97356SBlue Swirl     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
468420d97356SBlue Swirl     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
468520d97356SBlue Swirl     .bdrv_snapshot_list     = qcow2_snapshot_list,
468651ef6727Sedison     .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
4687c501c352SStefan Hajnoczi     .bdrv_measure           = qcow2_measure,
46887c80ab3fSJes Sorensen     .bdrv_get_info          = qcow2_get_info,
468937764dfbSMax Reitz     .bdrv_get_specific_info = qcow2_get_specific_info,
469020d97356SBlue Swirl 
46917c80ab3fSJes Sorensen     .bdrv_save_vmstate    = qcow2_save_vmstate,
46927c80ab3fSJes Sorensen     .bdrv_load_vmstate    = qcow2_load_vmstate,
469320d97356SBlue Swirl 
46948ee79e70SKevin Wolf     .supports_backing           = true,
469520d97356SBlue Swirl     .bdrv_change_backing_file   = qcow2_change_backing_file,
469620d97356SBlue Swirl 
4697d34682cdSKevin Wolf     .bdrv_refresh_limits        = qcow2_refresh_limits,
46982b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache   = qcow2_co_invalidate_cache,
4699ec6d8912SKevin Wolf     .bdrv_inactivate            = qcow2_inactivate,
470006d9260fSAnthony Liguori 
47011bd0e2d1SChunyan Liu     .create_opts         = &qcow2_create_opts,
47022fd61638SPaolo Bonzini     .bdrv_co_check       = qcow2_co_check,
4703c282e1fdSChunyan Liu     .bdrv_amend_options  = qcow2_amend_options,
4704279621c0SAlberto Garcia 
4705279621c0SAlberto Garcia     .bdrv_detach_aio_context  = qcow2_detach_aio_context,
4706279621c0SAlberto Garcia     .bdrv_attach_aio_context  = qcow2_attach_aio_context,
47071b6b0562SVladimir Sementsov-Ogievskiy 
47081b6b0562SVladimir Sementsov-Ogievskiy     .bdrv_reopen_bitmaps_rw = qcow2_reopen_bitmaps_rw,
4709da0eb242SVladimir Sementsov-Ogievskiy     .bdrv_can_store_new_dirty_bitmap = qcow2_can_store_new_dirty_bitmap,
4710469c71edSVladimir Sementsov-Ogievskiy     .bdrv_remove_persistent_dirty_bitmap = qcow2_remove_persistent_dirty_bitmap,
471120d97356SBlue Swirl };
471220d97356SBlue Swirl 
47135efa9d5aSAnthony Liguori static void bdrv_qcow2_init(void)
47145efa9d5aSAnthony Liguori {
47155efa9d5aSAnthony Liguori     bdrv_register(&bdrv_qcow2);
47165efa9d5aSAnthony Liguori }
47175efa9d5aSAnthony Liguori 
47185efa9d5aSAnthony Liguori block_init(bdrv_qcow2_init);
4719