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