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