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