xref: /qemu/block/qcow2.c (revision 609f45ea)
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 #include "block/block_int.h"
27 #include "block/qdict.h"
28 #include "sysemu/block-backend.h"
29 #include "qemu/module.h"
30 #include <zlib.h>
31 #include "qcow2.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-block-core.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qstring.h"
37 #include "trace.h"
38 #include "qemu/option_int.h"
39 #include "qemu/cutils.h"
40 #include "qemu/bswap.h"
41 #include "qapi/qobject-input-visitor.h"
42 #include "qapi/qapi-visit-block-core.h"
43 #include "crypto.h"
44 
45 /*
46   Differences with QCOW:
47 
48   - Support for multiple incremental snapshots.
49   - Memory management by reference counts.
50   - Clusters which have a reference count of one have the bit
51     QCOW_OFLAG_COPIED to optimize write performance.
52   - Size of compressed clusters is stored in sectors to reduce bit usage
53     in the cluster offsets.
54   - Support for storing additional data (such as the VM state) in the
55     snapshots.
56   - If a backing store is used, the cluster size is not constrained
57     (could be backported to QCOW).
58   - L2 tables have always a size of one cluster.
59 */
60 
61 
62 typedef struct {
63     uint32_t magic;
64     uint32_t len;
65 } QEMU_PACKED QCowExtension;
66 
67 #define  QCOW2_EXT_MAGIC_END 0
68 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
69 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
70 #define  QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
71 #define  QCOW2_EXT_MAGIC_BITMAPS 0x23852875
72 
73 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
74 {
75     const QCowHeader *cow_header = (const void *)buf;
76 
77     if (buf_size >= sizeof(QCowHeader) &&
78         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
79         be32_to_cpu(cow_header->version) >= 2)
80         return 100;
81     else
82         return 0;
83 }
84 
85 
86 static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
87                                           uint8_t *buf, size_t buflen,
88                                           void *opaque, Error **errp)
89 {
90     BlockDriverState *bs = opaque;
91     BDRVQcow2State *s = bs->opaque;
92     ssize_t ret;
93 
94     if ((offset + buflen) > s->crypto_header.length) {
95         error_setg(errp, "Request for data outside of extension header");
96         return -1;
97     }
98 
99     ret = bdrv_pread(bs->file,
100                      s->crypto_header.offset + offset, buf, buflen);
101     if (ret < 0) {
102         error_setg_errno(errp, -ret, "Could not read encryption header");
103         return -1;
104     }
105     return ret;
106 }
107 
108 
109 static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
110                                           void *opaque, Error **errp)
111 {
112     BlockDriverState *bs = opaque;
113     BDRVQcow2State *s = bs->opaque;
114     int64_t ret;
115     int64_t clusterlen;
116 
117     ret = qcow2_alloc_clusters(bs, headerlen);
118     if (ret < 0) {
119         error_setg_errno(errp, -ret,
120                          "Cannot allocate cluster for LUKS header size %zu",
121                          headerlen);
122         return -1;
123     }
124 
125     s->crypto_header.length = headerlen;
126     s->crypto_header.offset = ret;
127 
128     /* Zero fill remaining space in cluster so it has predictable
129      * content in case of future spec changes */
130     clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
131     assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen) == 0);
132     ret = bdrv_pwrite_zeroes(bs->file,
133                              ret + headerlen,
134                              clusterlen - headerlen, 0);
135     if (ret < 0) {
136         error_setg_errno(errp, -ret, "Could not zero fill encryption header");
137         return -1;
138     }
139 
140     return ret;
141 }
142 
143 
144 static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
145                                            const uint8_t *buf, size_t buflen,
146                                            void *opaque, Error **errp)
147 {
148     BlockDriverState *bs = opaque;
149     BDRVQcow2State *s = bs->opaque;
150     ssize_t ret;
151 
152     if ((offset + buflen) > s->crypto_header.length) {
153         error_setg(errp, "Request for data outside of extension header");
154         return -1;
155     }
156 
157     ret = bdrv_pwrite(bs->file,
158                       s->crypto_header.offset + offset, buf, buflen);
159     if (ret < 0) {
160         error_setg_errno(errp, -ret, "Could not read encryption header");
161         return -1;
162     }
163     return ret;
164 }
165 
166 
167 /*
168  * read qcow2 extension and fill bs
169  * start reading from start_offset
170  * finish reading upon magic of value 0 or when end_offset reached
171  * unknown magic is skipped (future extension this version knows nothing about)
172  * return 0 upon success, non-0 otherwise
173  */
174 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
175                                  uint64_t end_offset, void **p_feature_table,
176                                  int flags, bool *need_update_header,
177                                  Error **errp)
178 {
179     BDRVQcow2State *s = bs->opaque;
180     QCowExtension ext;
181     uint64_t offset;
182     int ret;
183     Qcow2BitmapHeaderExt bitmaps_ext;
184 
185     if (need_update_header != NULL) {
186         *need_update_header = false;
187     }
188 
189 #ifdef DEBUG_EXT
190     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
191 #endif
192     offset = start_offset;
193     while (offset < end_offset) {
194 
195 #ifdef DEBUG_EXT
196         /* Sanity check */
197         if (offset > s->cluster_size)
198             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
199 
200         printf("attempting to read extended header in offset %lu\n", offset);
201 #endif
202 
203         ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
204         if (ret < 0) {
205             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
206                              "pread fail from offset %" PRIu64, offset);
207             return 1;
208         }
209         be32_to_cpus(&ext.magic);
210         be32_to_cpus(&ext.len);
211         offset += sizeof(ext);
212 #ifdef DEBUG_EXT
213         printf("ext.magic = 0x%x\n", ext.magic);
214 #endif
215         if (offset > end_offset || ext.len > end_offset - offset) {
216             error_setg(errp, "Header extension too large");
217             return -EINVAL;
218         }
219 
220         switch (ext.magic) {
221         case QCOW2_EXT_MAGIC_END:
222             return 0;
223 
224         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
225             if (ext.len >= sizeof(bs->backing_format)) {
226                 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
227                            " too large (>=%zu)", ext.len,
228                            sizeof(bs->backing_format));
229                 return 2;
230             }
231             ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
232             if (ret < 0) {
233                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
234                                  "Could not read format name");
235                 return 3;
236             }
237             bs->backing_format[ext.len] = '\0';
238             s->image_backing_format = g_strdup(bs->backing_format);
239 #ifdef DEBUG_EXT
240             printf("Qcow2: Got format extension %s\n", bs->backing_format);
241 #endif
242             break;
243 
244         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
245             if (p_feature_table != NULL) {
246                 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
247                 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
248                 if (ret < 0) {
249                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
250                                      "Could not read table");
251                     return ret;
252                 }
253 
254                 *p_feature_table = feature_table;
255             }
256             break;
257 
258         case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
259             unsigned int cflags = 0;
260             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
261                 error_setg(errp, "CRYPTO header extension only "
262                            "expected with LUKS encryption method");
263                 return -EINVAL;
264             }
265             if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
266                 error_setg(errp, "CRYPTO header extension size %u, "
267                            "but expected size %zu", ext.len,
268                            sizeof(Qcow2CryptoHeaderExtension));
269                 return -EINVAL;
270             }
271 
272             ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
273             if (ret < 0) {
274                 error_setg_errno(errp, -ret,
275                                  "Unable to read CRYPTO header extension");
276                 return ret;
277             }
278             be64_to_cpus(&s->crypto_header.offset);
279             be64_to_cpus(&s->crypto_header.length);
280 
281             if ((s->crypto_header.offset % s->cluster_size) != 0) {
282                 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
283                            "not a multiple of cluster size '%u'",
284                            s->crypto_header.offset, s->cluster_size);
285                 return -EINVAL;
286             }
287 
288             if (flags & BDRV_O_NO_IO) {
289                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
290             }
291             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
292                                            qcow2_crypto_hdr_read_func,
293                                            bs, cflags, errp);
294             if (!s->crypto) {
295                 return -EINVAL;
296             }
297         }   break;
298 
299         case QCOW2_EXT_MAGIC_BITMAPS:
300             if (ext.len != sizeof(bitmaps_ext)) {
301                 error_setg_errno(errp, -ret, "bitmaps_ext: "
302                                  "Invalid extension length");
303                 return -EINVAL;
304             }
305 
306             if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
307                 if (s->qcow_version < 3) {
308                     /* Let's be a bit more specific */
309                     warn_report("This qcow2 v2 image contains bitmaps, but "
310                                 "they may have been modified by a program "
311                                 "without persistent bitmap support; so now "
312                                 "they must all be considered inconsistent");
313                 } else {
314                     warn_report("a program lacking bitmap support "
315                                 "modified this file, so all bitmaps are now "
316                                 "considered inconsistent");
317                 }
318                 error_printf("Some clusters may be leaked, "
319                              "run 'qemu-img check -r' on the image "
320                              "file to fix.");
321                 if (need_update_header != NULL) {
322                     /* Updating is needed to drop invalid bitmap extension. */
323                     *need_update_header = true;
324                 }
325                 break;
326             }
327 
328             ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
329             if (ret < 0) {
330                 error_setg_errno(errp, -ret, "bitmaps_ext: "
331                                  "Could not read ext header");
332                 return ret;
333             }
334 
335             if (bitmaps_ext.reserved32 != 0) {
336                 error_setg_errno(errp, -ret, "bitmaps_ext: "
337                                  "Reserved field is not zero");
338                 return -EINVAL;
339             }
340 
341             be32_to_cpus(&bitmaps_ext.nb_bitmaps);
342             be64_to_cpus(&bitmaps_ext.bitmap_directory_size);
343             be64_to_cpus(&bitmaps_ext.bitmap_directory_offset);
344 
345             if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
346                 error_setg(errp,
347                            "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
348                            "exceeding the QEMU supported maximum of %d",
349                            bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
350                 return -EINVAL;
351             }
352 
353             if (bitmaps_ext.nb_bitmaps == 0) {
354                 error_setg(errp, "found bitmaps extension with zero bitmaps");
355                 return -EINVAL;
356             }
357 
358             if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
359                 error_setg(errp, "bitmaps_ext: "
360                                  "invalid bitmap directory offset");
361                 return -EINVAL;
362             }
363 
364             if (bitmaps_ext.bitmap_directory_size >
365                 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
366                 error_setg(errp, "bitmaps_ext: "
367                                  "bitmap directory size (%" PRIu64 ") exceeds "
368                                  "the maximum supported size (%d)",
369                                  bitmaps_ext.bitmap_directory_size,
370                                  QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
371                 return -EINVAL;
372             }
373 
374             s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
375             s->bitmap_directory_offset =
376                     bitmaps_ext.bitmap_directory_offset;
377             s->bitmap_directory_size =
378                     bitmaps_ext.bitmap_directory_size;
379 
380 #ifdef DEBUG_EXT
381             printf("Qcow2: Got bitmaps extension: "
382                    "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
383                    s->bitmap_directory_offset, s->nb_bitmaps);
384 #endif
385             break;
386 
387         default:
388             /* unknown magic - save it in case we need to rewrite the header */
389             /* If you add a new feature, make sure to also update the fast
390              * path of qcow2_make_empty() to deal with it. */
391             {
392                 Qcow2UnknownHeaderExtension *uext;
393 
394                 uext = g_malloc0(sizeof(*uext)  + ext.len);
395                 uext->magic = ext.magic;
396                 uext->len = ext.len;
397                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
398 
399                 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
400                 if (ret < 0) {
401                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
402                                      "Could not read data");
403                     return ret;
404                 }
405             }
406             break;
407         }
408 
409         offset += ((ext.len + 7) & ~7);
410     }
411 
412     return 0;
413 }
414 
415 static void cleanup_unknown_header_ext(BlockDriverState *bs)
416 {
417     BDRVQcow2State *s = bs->opaque;
418     Qcow2UnknownHeaderExtension *uext, *next;
419 
420     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
421         QLIST_REMOVE(uext, next);
422         g_free(uext);
423     }
424 }
425 
426 static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
427                                        uint64_t mask)
428 {
429     char *features = g_strdup("");
430     char *old;
431 
432     while (table && table->name[0] != '\0') {
433         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
434             if (mask & (1ULL << table->bit)) {
435                 old = features;
436                 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
437                                            table->name);
438                 g_free(old);
439                 mask &= ~(1ULL << table->bit);
440             }
441         }
442         table++;
443     }
444 
445     if (mask) {
446         old = features;
447         features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
448                                    old, *old ? ", " : "", mask);
449         g_free(old);
450     }
451 
452     error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
453     g_free(features);
454 }
455 
456 /*
457  * Sets the dirty bit and flushes afterwards if necessary.
458  *
459  * The incompatible_features bit is only set if the image file header was
460  * updated successfully.  Therefore it is not required to check the return
461  * value of this function.
462  */
463 int qcow2_mark_dirty(BlockDriverState *bs)
464 {
465     BDRVQcow2State *s = bs->opaque;
466     uint64_t val;
467     int ret;
468 
469     assert(s->qcow_version >= 3);
470 
471     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
472         return 0; /* already dirty */
473     }
474 
475     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
476     ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
477                       &val, sizeof(val));
478     if (ret < 0) {
479         return ret;
480     }
481     ret = bdrv_flush(bs->file->bs);
482     if (ret < 0) {
483         return ret;
484     }
485 
486     /* Only treat image as dirty if the header was updated successfully */
487     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
488     return 0;
489 }
490 
491 /*
492  * Clears the dirty bit and flushes before if necessary.  Only call this
493  * function when there are no pending requests, it does not guard against
494  * concurrent requests dirtying the image.
495  */
496 static int qcow2_mark_clean(BlockDriverState *bs)
497 {
498     BDRVQcow2State *s = bs->opaque;
499 
500     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
501         int ret;
502 
503         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
504 
505         ret = qcow2_flush_caches(bs);
506         if (ret < 0) {
507             return ret;
508         }
509 
510         return qcow2_update_header(bs);
511     }
512     return 0;
513 }
514 
515 /*
516  * Marks the image as corrupt.
517  */
518 int qcow2_mark_corrupt(BlockDriverState *bs)
519 {
520     BDRVQcow2State *s = bs->opaque;
521 
522     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
523     return qcow2_update_header(bs);
524 }
525 
526 /*
527  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
528  * before if necessary.
529  */
530 int qcow2_mark_consistent(BlockDriverState *bs)
531 {
532     BDRVQcow2State *s = bs->opaque;
533 
534     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
535         int ret = qcow2_flush_caches(bs);
536         if (ret < 0) {
537             return ret;
538         }
539 
540         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
541         return qcow2_update_header(bs);
542     }
543     return 0;
544 }
545 
546 static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs,
547                                               BdrvCheckResult *result,
548                                               BdrvCheckMode fix)
549 {
550     int ret = qcow2_check_refcounts(bs, result, fix);
551     if (ret < 0) {
552         return ret;
553     }
554 
555     if (fix && result->check_errors == 0 && result->corruptions == 0) {
556         ret = qcow2_mark_clean(bs);
557         if (ret < 0) {
558             return ret;
559         }
560         return qcow2_mark_consistent(bs);
561     }
562     return ret;
563 }
564 
565 static int coroutine_fn qcow2_co_check(BlockDriverState *bs,
566                                        BdrvCheckResult *result,
567                                        BdrvCheckMode fix)
568 {
569     BDRVQcow2State *s = bs->opaque;
570     int ret;
571 
572     qemu_co_mutex_lock(&s->lock);
573     ret = qcow2_co_check_locked(bs, result, fix);
574     qemu_co_mutex_unlock(&s->lock);
575     return ret;
576 }
577 
578 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
579                          uint64_t entries, size_t entry_len,
580                          int64_t max_size_bytes, const char *table_name,
581                          Error **errp)
582 {
583     BDRVQcow2State *s = bs->opaque;
584 
585     if (entries > max_size_bytes / entry_len) {
586         error_setg(errp, "%s too large", table_name);
587         return -EFBIG;
588     }
589 
590     /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
591      * because values will be passed to qemu functions taking int64_t. */
592     if ((INT64_MAX - entries * entry_len < offset) ||
593         (offset_into_cluster(s, offset) != 0)) {
594         error_setg(errp, "%s offset invalid", table_name);
595         return -EINVAL;
596     }
597 
598     return 0;
599 }
600 
601 static QemuOptsList qcow2_runtime_opts = {
602     .name = "qcow2",
603     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
604     .desc = {
605         {
606             .name = QCOW2_OPT_LAZY_REFCOUNTS,
607             .type = QEMU_OPT_BOOL,
608             .help = "Postpone refcount updates",
609         },
610         {
611             .name = QCOW2_OPT_DISCARD_REQUEST,
612             .type = QEMU_OPT_BOOL,
613             .help = "Pass guest discard requests to the layer below",
614         },
615         {
616             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
617             .type = QEMU_OPT_BOOL,
618             .help = "Generate discard requests when snapshot related space "
619                     "is freed",
620         },
621         {
622             .name = QCOW2_OPT_DISCARD_OTHER,
623             .type = QEMU_OPT_BOOL,
624             .help = "Generate discard requests when other clusters are freed",
625         },
626         {
627             .name = QCOW2_OPT_OVERLAP,
628             .type = QEMU_OPT_STRING,
629             .help = "Selects which overlap checks to perform from a range of "
630                     "templates (none, constant, cached, all)",
631         },
632         {
633             .name = QCOW2_OPT_OVERLAP_TEMPLATE,
634             .type = QEMU_OPT_STRING,
635             .help = "Selects which overlap checks to perform from a range of "
636                     "templates (none, constant, cached, all)",
637         },
638         {
639             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
640             .type = QEMU_OPT_BOOL,
641             .help = "Check for unintended writes into the main qcow2 header",
642         },
643         {
644             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
645             .type = QEMU_OPT_BOOL,
646             .help = "Check for unintended writes into the active L1 table",
647         },
648         {
649             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
650             .type = QEMU_OPT_BOOL,
651             .help = "Check for unintended writes into an active L2 table",
652         },
653         {
654             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
655             .type = QEMU_OPT_BOOL,
656             .help = "Check for unintended writes into the refcount table",
657         },
658         {
659             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
660             .type = QEMU_OPT_BOOL,
661             .help = "Check for unintended writes into a refcount block",
662         },
663         {
664             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
665             .type = QEMU_OPT_BOOL,
666             .help = "Check for unintended writes into the snapshot table",
667         },
668         {
669             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
670             .type = QEMU_OPT_BOOL,
671             .help = "Check for unintended writes into an inactive L1 table",
672         },
673         {
674             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
675             .type = QEMU_OPT_BOOL,
676             .help = "Check for unintended writes into an inactive L2 table",
677         },
678         {
679             .name = QCOW2_OPT_CACHE_SIZE,
680             .type = QEMU_OPT_SIZE,
681             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
682                     "cache size",
683         },
684         {
685             .name = QCOW2_OPT_L2_CACHE_SIZE,
686             .type = QEMU_OPT_SIZE,
687             .help = "Maximum L2 table cache size",
688         },
689         {
690             .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
691             .type = QEMU_OPT_SIZE,
692             .help = "Size of each entry in the L2 cache",
693         },
694         {
695             .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
696             .type = QEMU_OPT_SIZE,
697             .help = "Maximum refcount block cache size",
698         },
699         {
700             .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
701             .type = QEMU_OPT_NUMBER,
702             .help = "Clean unused cache entries after this time (in seconds)",
703         },
704         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
705             "ID of secret providing qcow2 AES key or LUKS passphrase"),
706         { /* end of list */ }
707     },
708 };
709 
710 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
711     [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
712     [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
713     [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
714     [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
715     [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
716     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
717     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
718     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
719 };
720 
721 static void cache_clean_timer_cb(void *opaque)
722 {
723     BlockDriverState *bs = opaque;
724     BDRVQcow2State *s = bs->opaque;
725     qcow2_cache_clean_unused(s->l2_table_cache);
726     qcow2_cache_clean_unused(s->refcount_block_cache);
727     timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
728               (int64_t) s->cache_clean_interval * 1000);
729 }
730 
731 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
732 {
733     BDRVQcow2State *s = bs->opaque;
734     if (s->cache_clean_interval > 0) {
735         s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
736                                              SCALE_MS, cache_clean_timer_cb,
737                                              bs);
738         timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
739                   (int64_t) s->cache_clean_interval * 1000);
740     }
741 }
742 
743 static void cache_clean_timer_del(BlockDriverState *bs)
744 {
745     BDRVQcow2State *s = bs->opaque;
746     if (s->cache_clean_timer) {
747         timer_del(s->cache_clean_timer);
748         timer_free(s->cache_clean_timer);
749         s->cache_clean_timer = NULL;
750     }
751 }
752 
753 static void qcow2_detach_aio_context(BlockDriverState *bs)
754 {
755     cache_clean_timer_del(bs);
756 }
757 
758 static void qcow2_attach_aio_context(BlockDriverState *bs,
759                                      AioContext *new_context)
760 {
761     cache_clean_timer_init(bs, new_context);
762 }
763 
764 static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
765                              uint64_t *l2_cache_size,
766                              uint64_t *l2_cache_entry_size,
767                              uint64_t *refcount_cache_size, Error **errp)
768 {
769     BDRVQcow2State *s = bs->opaque;
770     uint64_t combined_cache_size;
771     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
772     int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
773 
774     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
775     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
776     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
777 
778     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
779     *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
780     *refcount_cache_size = qemu_opt_get_size(opts,
781                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
782 
783     *l2_cache_entry_size = qemu_opt_get_size(
784         opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
785 
786     if (combined_cache_size_set) {
787         if (l2_cache_size_set && refcount_cache_size_set) {
788             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
789                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
790                        "the same time");
791             return;
792         } else if (*l2_cache_size > combined_cache_size) {
793             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
794                        QCOW2_OPT_CACHE_SIZE);
795             return;
796         } else if (*refcount_cache_size > combined_cache_size) {
797             error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
798                        QCOW2_OPT_CACHE_SIZE);
799             return;
800         }
801 
802         if (l2_cache_size_set) {
803             *refcount_cache_size = combined_cache_size - *l2_cache_size;
804         } else if (refcount_cache_size_set) {
805             *l2_cache_size = combined_cache_size - *refcount_cache_size;
806         } else {
807             uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
808             uint64_t max_l2_cache = virtual_disk_size / (s->cluster_size / 8);
809 
810             /* Assign as much memory as possible to the L2 cache, and
811              * use the remainder for the refcount cache */
812             if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
813                 *l2_cache_size = max_l2_cache;
814                 *refcount_cache_size = combined_cache_size - *l2_cache_size;
815             } else {
816                 *refcount_cache_size =
817                     MIN(combined_cache_size, min_refcount_cache);
818                 *l2_cache_size = combined_cache_size - *refcount_cache_size;
819             }
820         }
821     } else {
822         if (!l2_cache_size_set) {
823             *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
824                                  (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
825                                  * s->cluster_size);
826         }
827         if (!refcount_cache_size_set) {
828             *refcount_cache_size = min_refcount_cache;
829         }
830     }
831 
832     if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
833         *l2_cache_entry_size > s->cluster_size ||
834         !is_power_of_2(*l2_cache_entry_size)) {
835         error_setg(errp, "L2 cache entry size must be a power of two "
836                    "between %d and the cluster size (%d)",
837                    1 << MIN_CLUSTER_BITS, s->cluster_size);
838         return;
839     }
840 }
841 
842 typedef struct Qcow2ReopenState {
843     Qcow2Cache *l2_table_cache;
844     Qcow2Cache *refcount_block_cache;
845     int l2_slice_size; /* Number of entries in a slice of the L2 table */
846     bool use_lazy_refcounts;
847     int overlap_check;
848     bool discard_passthrough[QCOW2_DISCARD_MAX];
849     uint64_t cache_clean_interval;
850     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
851 } Qcow2ReopenState;
852 
853 static int qcow2_update_options_prepare(BlockDriverState *bs,
854                                         Qcow2ReopenState *r,
855                                         QDict *options, int flags,
856                                         Error **errp)
857 {
858     BDRVQcow2State *s = bs->opaque;
859     QemuOpts *opts = NULL;
860     const char *opt_overlap_check, *opt_overlap_check_template;
861     int overlap_check_template = 0;
862     uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
863     int i;
864     const char *encryptfmt;
865     QDict *encryptopts = NULL;
866     Error *local_err = NULL;
867     int ret;
868 
869     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
870     encryptfmt = qdict_get_try_str(encryptopts, "format");
871 
872     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
873     qemu_opts_absorb_qdict(opts, options, &local_err);
874     if (local_err) {
875         error_propagate(errp, local_err);
876         ret = -EINVAL;
877         goto fail;
878     }
879 
880     /* get L2 table/refcount block cache size from command line options */
881     read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
882                      &refcount_cache_size, &local_err);
883     if (local_err) {
884         error_propagate(errp, local_err);
885         ret = -EINVAL;
886         goto fail;
887     }
888 
889     l2_cache_size /= l2_cache_entry_size;
890     if (l2_cache_size < MIN_L2_CACHE_SIZE) {
891         l2_cache_size = MIN_L2_CACHE_SIZE;
892     }
893     if (l2_cache_size > INT_MAX) {
894         error_setg(errp, "L2 cache size too big");
895         ret = -EINVAL;
896         goto fail;
897     }
898 
899     refcount_cache_size /= s->cluster_size;
900     if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
901         refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
902     }
903     if (refcount_cache_size > INT_MAX) {
904         error_setg(errp, "Refcount cache size too big");
905         ret = -EINVAL;
906         goto fail;
907     }
908 
909     /* alloc new L2 table/refcount block cache, flush old one */
910     if (s->l2_table_cache) {
911         ret = qcow2_cache_flush(bs, s->l2_table_cache);
912         if (ret) {
913             error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
914             goto fail;
915         }
916     }
917 
918     if (s->refcount_block_cache) {
919         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
920         if (ret) {
921             error_setg_errno(errp, -ret,
922                              "Failed to flush the refcount block cache");
923             goto fail;
924         }
925     }
926 
927     r->l2_slice_size = l2_cache_entry_size / sizeof(uint64_t);
928     r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
929                                            l2_cache_entry_size);
930     r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
931                                                  s->cluster_size);
932     if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
933         error_setg(errp, "Could not allocate metadata caches");
934         ret = -ENOMEM;
935         goto fail;
936     }
937 
938     /* New interval for cache cleanup timer */
939     r->cache_clean_interval =
940         qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
941                             s->cache_clean_interval);
942 #ifndef CONFIG_LINUX
943     if (r->cache_clean_interval != 0) {
944         error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
945                    " not supported on this host");
946         ret = -EINVAL;
947         goto fail;
948     }
949 #endif
950     if (r->cache_clean_interval > UINT_MAX) {
951         error_setg(errp, "Cache clean interval too big");
952         ret = -EINVAL;
953         goto fail;
954     }
955 
956     /* lazy-refcounts; flush if going from enabled to disabled */
957     r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
958         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
959     if (r->use_lazy_refcounts && s->qcow_version < 3) {
960         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
961                    "qemu 1.1 compatibility level");
962         ret = -EINVAL;
963         goto fail;
964     }
965 
966     if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
967         ret = qcow2_mark_clean(bs);
968         if (ret < 0) {
969             error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
970             goto fail;
971         }
972     }
973 
974     /* Overlap check options */
975     opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
976     opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
977     if (opt_overlap_check_template && opt_overlap_check &&
978         strcmp(opt_overlap_check_template, opt_overlap_check))
979     {
980         error_setg(errp, "Conflicting values for qcow2 options '"
981                    QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
982                    "' ('%s')", opt_overlap_check, opt_overlap_check_template);
983         ret = -EINVAL;
984         goto fail;
985     }
986     if (!opt_overlap_check) {
987         opt_overlap_check = opt_overlap_check_template ?: "cached";
988     }
989 
990     if (!strcmp(opt_overlap_check, "none")) {
991         overlap_check_template = 0;
992     } else if (!strcmp(opt_overlap_check, "constant")) {
993         overlap_check_template = QCOW2_OL_CONSTANT;
994     } else if (!strcmp(opt_overlap_check, "cached")) {
995         overlap_check_template = QCOW2_OL_CACHED;
996     } else if (!strcmp(opt_overlap_check, "all")) {
997         overlap_check_template = QCOW2_OL_ALL;
998     } else {
999         error_setg(errp, "Unsupported value '%s' for qcow2 option "
1000                    "'overlap-check'. Allowed are any of the following: "
1001                    "none, constant, cached, all", opt_overlap_check);
1002         ret = -EINVAL;
1003         goto fail;
1004     }
1005 
1006     r->overlap_check = 0;
1007     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
1008         /* overlap-check defines a template bitmask, but every flag may be
1009          * overwritten through the associated boolean option */
1010         r->overlap_check |=
1011             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
1012                               overlap_check_template & (1 << i)) << i;
1013     }
1014 
1015     r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1016     r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1017     r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1018         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1019                           flags & BDRV_O_UNMAP);
1020     r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1021         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1022     r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1023         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1024 
1025     switch (s->crypt_method_header) {
1026     case QCOW_CRYPT_NONE:
1027         if (encryptfmt) {
1028             error_setg(errp, "No encryption in image header, but options "
1029                        "specified format '%s'", encryptfmt);
1030             ret = -EINVAL;
1031             goto fail;
1032         }
1033         break;
1034 
1035     case QCOW_CRYPT_AES:
1036         if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1037             error_setg(errp,
1038                        "Header reported 'aes' encryption format but "
1039                        "options specify '%s'", encryptfmt);
1040             ret = -EINVAL;
1041             goto fail;
1042         }
1043         qdict_del(encryptopts, "format");
1044         r->crypto_opts = block_crypto_open_opts_init(
1045             Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
1046         break;
1047 
1048     case QCOW_CRYPT_LUKS:
1049         if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
1050             error_setg(errp,
1051                        "Header reported 'luks' encryption format but "
1052                        "options specify '%s'", encryptfmt);
1053             ret = -EINVAL;
1054             goto fail;
1055         }
1056         qdict_del(encryptopts, "format");
1057         r->crypto_opts = block_crypto_open_opts_init(
1058             Q_CRYPTO_BLOCK_FORMAT_LUKS, encryptopts, errp);
1059         break;
1060 
1061     default:
1062         error_setg(errp, "Unsupported encryption method %d",
1063                    s->crypt_method_header);
1064         break;
1065     }
1066     if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1067         ret = -EINVAL;
1068         goto fail;
1069     }
1070 
1071     ret = 0;
1072 fail:
1073     qobject_unref(encryptopts);
1074     qemu_opts_del(opts);
1075     opts = NULL;
1076     return ret;
1077 }
1078 
1079 static void qcow2_update_options_commit(BlockDriverState *bs,
1080                                         Qcow2ReopenState *r)
1081 {
1082     BDRVQcow2State *s = bs->opaque;
1083     int i;
1084 
1085     if (s->l2_table_cache) {
1086         qcow2_cache_destroy(s->l2_table_cache);
1087     }
1088     if (s->refcount_block_cache) {
1089         qcow2_cache_destroy(s->refcount_block_cache);
1090     }
1091     s->l2_table_cache = r->l2_table_cache;
1092     s->refcount_block_cache = r->refcount_block_cache;
1093     s->l2_slice_size = r->l2_slice_size;
1094 
1095     s->overlap_check = r->overlap_check;
1096     s->use_lazy_refcounts = r->use_lazy_refcounts;
1097 
1098     for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1099         s->discard_passthrough[i] = r->discard_passthrough[i];
1100     }
1101 
1102     if (s->cache_clean_interval != r->cache_clean_interval) {
1103         cache_clean_timer_del(bs);
1104         s->cache_clean_interval = r->cache_clean_interval;
1105         cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1106     }
1107 
1108     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1109     s->crypto_opts = r->crypto_opts;
1110 }
1111 
1112 static void qcow2_update_options_abort(BlockDriverState *bs,
1113                                        Qcow2ReopenState *r)
1114 {
1115     if (r->l2_table_cache) {
1116         qcow2_cache_destroy(r->l2_table_cache);
1117     }
1118     if (r->refcount_block_cache) {
1119         qcow2_cache_destroy(r->refcount_block_cache);
1120     }
1121     qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1122 }
1123 
1124 static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1125                                 int flags, Error **errp)
1126 {
1127     Qcow2ReopenState r = {};
1128     int ret;
1129 
1130     ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1131     if (ret >= 0) {
1132         qcow2_update_options_commit(bs, &r);
1133     } else {
1134         qcow2_update_options_abort(bs, &r);
1135     }
1136 
1137     return ret;
1138 }
1139 
1140 /* Called with s->lock held.  */
1141 static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
1142                                       int flags, Error **errp)
1143 {
1144     BDRVQcow2State *s = bs->opaque;
1145     unsigned int len, i;
1146     int ret = 0;
1147     QCowHeader header;
1148     Error *local_err = NULL;
1149     uint64_t ext_end;
1150     uint64_t l1_vm_state_index;
1151     bool update_header = false;
1152     bool header_updated = false;
1153 
1154     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
1155     if (ret < 0) {
1156         error_setg_errno(errp, -ret, "Could not read qcow2 header");
1157         goto fail;
1158     }
1159     be32_to_cpus(&header.magic);
1160     be32_to_cpus(&header.version);
1161     be64_to_cpus(&header.backing_file_offset);
1162     be32_to_cpus(&header.backing_file_size);
1163     be64_to_cpus(&header.size);
1164     be32_to_cpus(&header.cluster_bits);
1165     be32_to_cpus(&header.crypt_method);
1166     be64_to_cpus(&header.l1_table_offset);
1167     be32_to_cpus(&header.l1_size);
1168     be64_to_cpus(&header.refcount_table_offset);
1169     be32_to_cpus(&header.refcount_table_clusters);
1170     be64_to_cpus(&header.snapshots_offset);
1171     be32_to_cpus(&header.nb_snapshots);
1172 
1173     if (header.magic != QCOW_MAGIC) {
1174         error_setg(errp, "Image is not in qcow2 format");
1175         ret = -EINVAL;
1176         goto fail;
1177     }
1178     if (header.version < 2 || header.version > 3) {
1179         error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1180         ret = -ENOTSUP;
1181         goto fail;
1182     }
1183 
1184     s->qcow_version = header.version;
1185 
1186     /* Initialise cluster size */
1187     if (header.cluster_bits < MIN_CLUSTER_BITS ||
1188         header.cluster_bits > MAX_CLUSTER_BITS) {
1189         error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1190                    header.cluster_bits);
1191         ret = -EINVAL;
1192         goto fail;
1193     }
1194 
1195     s->cluster_bits = header.cluster_bits;
1196     s->cluster_size = 1 << s->cluster_bits;
1197     s->cluster_sectors = 1 << (s->cluster_bits - BDRV_SECTOR_BITS);
1198 
1199     /* Initialise version 3 header fields */
1200     if (header.version == 2) {
1201         header.incompatible_features    = 0;
1202         header.compatible_features      = 0;
1203         header.autoclear_features       = 0;
1204         header.refcount_order           = 4;
1205         header.header_length            = 72;
1206     } else {
1207         be64_to_cpus(&header.incompatible_features);
1208         be64_to_cpus(&header.compatible_features);
1209         be64_to_cpus(&header.autoclear_features);
1210         be32_to_cpus(&header.refcount_order);
1211         be32_to_cpus(&header.header_length);
1212 
1213         if (header.header_length < 104) {
1214             error_setg(errp, "qcow2 header too short");
1215             ret = -EINVAL;
1216             goto fail;
1217         }
1218     }
1219 
1220     if (header.header_length > s->cluster_size) {
1221         error_setg(errp, "qcow2 header exceeds cluster size");
1222         ret = -EINVAL;
1223         goto fail;
1224     }
1225 
1226     if (header.header_length > sizeof(header)) {
1227         s->unknown_header_fields_size = header.header_length - sizeof(header);
1228         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1229         ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
1230                          s->unknown_header_fields_size);
1231         if (ret < 0) {
1232             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
1233                              "fields");
1234             goto fail;
1235         }
1236     }
1237 
1238     if (header.backing_file_offset > s->cluster_size) {
1239         error_setg(errp, "Invalid backing file offset");
1240         ret = -EINVAL;
1241         goto fail;
1242     }
1243 
1244     if (header.backing_file_offset) {
1245         ext_end = header.backing_file_offset;
1246     } else {
1247         ext_end = 1 << header.cluster_bits;
1248     }
1249 
1250     /* Handle feature bits */
1251     s->incompatible_features    = header.incompatible_features;
1252     s->compatible_features      = header.compatible_features;
1253     s->autoclear_features       = header.autoclear_features;
1254 
1255     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1256         void *feature_table = NULL;
1257         qcow2_read_extensions(bs, header.header_length, ext_end,
1258                               &feature_table, flags, NULL, NULL);
1259         report_unsupported_feature(errp, feature_table,
1260                                    s->incompatible_features &
1261                                    ~QCOW2_INCOMPAT_MASK);
1262         ret = -ENOTSUP;
1263         g_free(feature_table);
1264         goto fail;
1265     }
1266 
1267     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
1268         /* Corrupt images may not be written to unless they are being repaired
1269          */
1270         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
1271             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
1272                        "read/write");
1273             ret = -EACCES;
1274             goto fail;
1275         }
1276     }
1277 
1278     /* Check support for various header values */
1279     if (header.refcount_order > 6) {
1280         error_setg(errp, "Reference count entry width too large; may not "
1281                    "exceed 64 bits");
1282         ret = -EINVAL;
1283         goto fail;
1284     }
1285     s->refcount_order = header.refcount_order;
1286     s->refcount_bits = 1 << s->refcount_order;
1287     s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1288     s->refcount_max += s->refcount_max - 1;
1289 
1290     s->crypt_method_header = header.crypt_method;
1291     if (s->crypt_method_header) {
1292         if (bdrv_uses_whitelist() &&
1293             s->crypt_method_header == QCOW_CRYPT_AES) {
1294             error_setg(errp,
1295                        "Use of AES-CBC encrypted qcow2 images is no longer "
1296                        "supported in system emulators");
1297             error_append_hint(errp,
1298                               "You can use 'qemu-img convert' to convert your "
1299                               "image to an alternative supported format, such "
1300                               "as unencrypted qcow2, or raw with the LUKS "
1301                               "format instead.\n");
1302             ret = -ENOSYS;
1303             goto fail;
1304         }
1305 
1306         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1307             s->crypt_physical_offset = false;
1308         } else {
1309             /* Assuming LUKS and any future crypt methods we
1310              * add will all use physical offsets, due to the
1311              * fact that the alternative is insecure...  */
1312             s->crypt_physical_offset = true;
1313         }
1314 
1315         bs->encrypted = true;
1316     }
1317 
1318     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
1319     s->l2_size = 1 << s->l2_bits;
1320     /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1321     s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
1322     s->refcount_block_size = 1 << s->refcount_block_bits;
1323     bs->total_sectors = header.size / 512;
1324     s->csize_shift = (62 - (s->cluster_bits - 8));
1325     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1326     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
1327 
1328     s->refcount_table_offset = header.refcount_table_offset;
1329     s->refcount_table_size =
1330         header.refcount_table_clusters << (s->cluster_bits - 3);
1331 
1332     if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1333         error_setg(errp, "Image does not contain a reference count table");
1334         ret = -EINVAL;
1335         goto fail;
1336     }
1337 
1338     ret = qcow2_validate_table(bs, s->refcount_table_offset,
1339                                header.refcount_table_clusters,
1340                                s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
1341                                "Reference count table", errp);
1342     if (ret < 0) {
1343         goto fail;
1344     }
1345 
1346     /* The total size in bytes of the snapshot table is checked in
1347      * qcow2_read_snapshots() because the size of each snapshot is
1348      * variable and we don't know it yet.
1349      * Here we only check the offset and number of snapshots. */
1350     ret = qcow2_validate_table(bs, header.snapshots_offset,
1351                                header.nb_snapshots,
1352                                sizeof(QCowSnapshotHeader),
1353                                sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
1354                                "Snapshot table", errp);
1355     if (ret < 0) {
1356         goto fail;
1357     }
1358 
1359     /* read the level 1 table */
1360     ret = qcow2_validate_table(bs, header.l1_table_offset,
1361                                header.l1_size, sizeof(uint64_t),
1362                                QCOW_MAX_L1_SIZE, "Active L1 table", errp);
1363     if (ret < 0) {
1364         goto fail;
1365     }
1366     s->l1_size = header.l1_size;
1367     s->l1_table_offset = header.l1_table_offset;
1368 
1369     l1_vm_state_index = size_to_l1(s, header.size);
1370     if (l1_vm_state_index > INT_MAX) {
1371         error_setg(errp, "Image is too big");
1372         ret = -EFBIG;
1373         goto fail;
1374     }
1375     s->l1_vm_state_index = l1_vm_state_index;
1376 
1377     /* the L1 table must contain at least enough entries to put
1378        header.size bytes */
1379     if (s->l1_size < s->l1_vm_state_index) {
1380         error_setg(errp, "L1 table is too small");
1381         ret = -EINVAL;
1382         goto fail;
1383     }
1384 
1385     if (s->l1_size > 0) {
1386         s->l1_table = qemu_try_blockalign(bs->file->bs,
1387             ROUND_UP(s->l1_size * sizeof(uint64_t), 512));
1388         if (s->l1_table == NULL) {
1389             error_setg(errp, "Could not allocate L1 table");
1390             ret = -ENOMEM;
1391             goto fail;
1392         }
1393         ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
1394                          s->l1_size * sizeof(uint64_t));
1395         if (ret < 0) {
1396             error_setg_errno(errp, -ret, "Could not read L1 table");
1397             goto fail;
1398         }
1399         for(i = 0;i < s->l1_size; i++) {
1400             be64_to_cpus(&s->l1_table[i]);
1401         }
1402     }
1403 
1404     /* Parse driver-specific options */
1405     ret = qcow2_update_options(bs, options, flags, errp);
1406     if (ret < 0) {
1407         goto fail;
1408     }
1409 
1410     s->cluster_cache_offset = -1;
1411     s->flags = flags;
1412 
1413     ret = qcow2_refcount_init(bs);
1414     if (ret != 0) {
1415         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1416         goto fail;
1417     }
1418 
1419     QLIST_INIT(&s->cluster_allocs);
1420     QTAILQ_INIT(&s->discards);
1421 
1422     /* read qcow2 extensions */
1423     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1424                               flags, &update_header, &local_err)) {
1425         error_propagate(errp, local_err);
1426         ret = -EINVAL;
1427         goto fail;
1428     }
1429 
1430     /* qcow2_read_extension may have set up the crypto context
1431      * if the crypt method needs a header region, some methods
1432      * don't need header extensions, so must check here
1433      */
1434     if (s->crypt_method_header && !s->crypto) {
1435         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1436             unsigned int cflags = 0;
1437             if (flags & BDRV_O_NO_IO) {
1438                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1439             }
1440             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
1441                                            NULL, NULL, cflags, errp);
1442             if (!s->crypto) {
1443                 ret = -EINVAL;
1444                 goto fail;
1445             }
1446         } else if (!(flags & BDRV_O_NO_IO)) {
1447             error_setg(errp, "Missing CRYPTO header for crypt method %d",
1448                        s->crypt_method_header);
1449             ret = -EINVAL;
1450             goto fail;
1451         }
1452     }
1453 
1454     /* read the backing file name */
1455     if (header.backing_file_offset != 0) {
1456         len = header.backing_file_size;
1457         if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1458             len >= sizeof(bs->backing_file)) {
1459             error_setg(errp, "Backing file name too long");
1460             ret = -EINVAL;
1461             goto fail;
1462         }
1463         ret = bdrv_pread(bs->file, header.backing_file_offset,
1464                          bs->backing_file, len);
1465         if (ret < 0) {
1466             error_setg_errno(errp, -ret, "Could not read backing file name");
1467             goto fail;
1468         }
1469         bs->backing_file[len] = '\0';
1470         s->image_backing_file = g_strdup(bs->backing_file);
1471     }
1472 
1473     /* Internal snapshots */
1474     s->snapshots_offset = header.snapshots_offset;
1475     s->nb_snapshots = header.nb_snapshots;
1476 
1477     ret = qcow2_read_snapshots(bs);
1478     if (ret < 0) {
1479         error_setg_errno(errp, -ret, "Could not read snapshots");
1480         goto fail;
1481     }
1482 
1483     /* Clear unknown autoclear feature bits */
1484     update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1485     update_header =
1486         update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1487     if (update_header) {
1488         s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1489     }
1490 
1491     if (s->dirty_bitmaps_loaded) {
1492         /* It's some kind of reopen. There are no known cases where we need to
1493          * reload bitmaps in such a situation, so it's safer to skip them.
1494          *
1495          * Moreover, if we have some readonly bitmaps and we are reopening for
1496          * rw we should reopen bitmaps correspondingly.
1497          */
1498         if (bdrv_has_readonly_bitmaps(bs) &&
1499             !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE))
1500         {
1501             qcow2_reopen_bitmaps_rw_hint(bs, &header_updated, &local_err);
1502         }
1503     } else {
1504         header_updated = qcow2_load_dirty_bitmaps(bs, &local_err);
1505         s->dirty_bitmaps_loaded = true;
1506     }
1507     update_header = update_header && !header_updated;
1508     if (local_err != NULL) {
1509         error_propagate(errp, local_err);
1510         ret = -EINVAL;
1511         goto fail;
1512     }
1513 
1514     if (update_header) {
1515         ret = qcow2_update_header(bs);
1516         if (ret < 0) {
1517             error_setg_errno(errp, -ret, "Could not update qcow2 header");
1518             goto fail;
1519         }
1520     }
1521 
1522     bs->supported_zero_flags = header.version >= 3 ? BDRV_REQ_MAY_UNMAP : 0;
1523 
1524     /* Repair image if dirty */
1525     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1526         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1527         BdrvCheckResult result = {0};
1528 
1529         ret = qcow2_co_check_locked(bs, &result,
1530                                     BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1531         if (ret < 0 || result.check_errors) {
1532             if (ret >= 0) {
1533                 ret = -EIO;
1534             }
1535             error_setg_errno(errp, -ret, "Could not repair dirty image");
1536             goto fail;
1537         }
1538     }
1539 
1540 #ifdef DEBUG_ALLOC
1541     {
1542         BdrvCheckResult result = {0};
1543         qcow2_check_refcounts(bs, &result, 0);
1544     }
1545 #endif
1546     return ret;
1547 
1548  fail:
1549     g_free(s->unknown_header_fields);
1550     cleanup_unknown_header_ext(bs);
1551     qcow2_free_snapshots(bs);
1552     qcow2_refcount_close(bs);
1553     qemu_vfree(s->l1_table);
1554     /* else pre-write overlap checks in cache_destroy may crash */
1555     s->l1_table = NULL;
1556     cache_clean_timer_del(bs);
1557     if (s->l2_table_cache) {
1558         qcow2_cache_destroy(s->l2_table_cache);
1559     }
1560     if (s->refcount_block_cache) {
1561         qcow2_cache_destroy(s->refcount_block_cache);
1562     }
1563     qcrypto_block_free(s->crypto);
1564     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1565     return ret;
1566 }
1567 
1568 typedef struct QCow2OpenCo {
1569     BlockDriverState *bs;
1570     QDict *options;
1571     int flags;
1572     Error **errp;
1573     int ret;
1574 } QCow2OpenCo;
1575 
1576 static void coroutine_fn qcow2_open_entry(void *opaque)
1577 {
1578     QCow2OpenCo *qoc = opaque;
1579     BDRVQcow2State *s = qoc->bs->opaque;
1580 
1581     qemu_co_mutex_lock(&s->lock);
1582     qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp);
1583     qemu_co_mutex_unlock(&s->lock);
1584 }
1585 
1586 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
1587                       Error **errp)
1588 {
1589     BDRVQcow2State *s = bs->opaque;
1590     QCow2OpenCo qoc = {
1591         .bs = bs,
1592         .options = options,
1593         .flags = flags,
1594         .errp = errp,
1595         .ret = -EINPROGRESS
1596     };
1597 
1598     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
1599                                false, errp);
1600     if (!bs->file) {
1601         return -EINVAL;
1602     }
1603 
1604     /* Initialise locks */
1605     qemu_co_mutex_init(&s->lock);
1606 
1607     if (qemu_in_coroutine()) {
1608         /* From bdrv_co_create.  */
1609         qcow2_open_entry(&qoc);
1610     } else {
1611         qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc));
1612         BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS);
1613     }
1614     return qoc.ret;
1615 }
1616 
1617 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1618 {
1619     BDRVQcow2State *s = bs->opaque;
1620 
1621     if (bs->encrypted) {
1622         /* Encryption works on a sector granularity */
1623         bs->bl.request_alignment = BDRV_SECTOR_SIZE;
1624     }
1625     bs->bl.pwrite_zeroes_alignment = s->cluster_size;
1626     bs->bl.pdiscard_alignment = s->cluster_size;
1627 }
1628 
1629 static int qcow2_reopen_prepare(BDRVReopenState *state,
1630                                 BlockReopenQueue *queue, Error **errp)
1631 {
1632     Qcow2ReopenState *r;
1633     int ret;
1634 
1635     r = g_new0(Qcow2ReopenState, 1);
1636     state->opaque = r;
1637 
1638     ret = qcow2_update_options_prepare(state->bs, r, state->options,
1639                                        state->flags, errp);
1640     if (ret < 0) {
1641         goto fail;
1642     }
1643 
1644     /* We need to write out any unwritten data if we reopen read-only. */
1645     if ((state->flags & BDRV_O_RDWR) == 0) {
1646         ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1647         if (ret < 0) {
1648             goto fail;
1649         }
1650 
1651         ret = bdrv_flush(state->bs);
1652         if (ret < 0) {
1653             goto fail;
1654         }
1655 
1656         ret = qcow2_mark_clean(state->bs);
1657         if (ret < 0) {
1658             goto fail;
1659         }
1660     }
1661 
1662     return 0;
1663 
1664 fail:
1665     qcow2_update_options_abort(state->bs, r);
1666     g_free(r);
1667     return ret;
1668 }
1669 
1670 static void qcow2_reopen_commit(BDRVReopenState *state)
1671 {
1672     qcow2_update_options_commit(state->bs, state->opaque);
1673     g_free(state->opaque);
1674 }
1675 
1676 static void qcow2_reopen_abort(BDRVReopenState *state)
1677 {
1678     qcow2_update_options_abort(state->bs, state->opaque);
1679     g_free(state->opaque);
1680 }
1681 
1682 static void qcow2_join_options(QDict *options, QDict *old_options)
1683 {
1684     bool has_new_overlap_template =
1685         qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
1686         qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
1687     bool has_new_total_cache_size =
1688         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
1689     bool has_all_cache_options;
1690 
1691     /* New overlap template overrides all old overlap options */
1692     if (has_new_overlap_template) {
1693         qdict_del(old_options, QCOW2_OPT_OVERLAP);
1694         qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
1695         qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
1696         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
1697         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
1698         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
1699         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
1700         qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
1701         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
1702         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
1703     }
1704 
1705     /* New total cache size overrides all old options */
1706     if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
1707         qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
1708         qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1709     }
1710 
1711     qdict_join(options, old_options, false);
1712 
1713     /*
1714      * If after merging all cache size options are set, an old total size is
1715      * overwritten. Do keep all options, however, if all three are new. The
1716      * resulting error message is what we want to happen.
1717      */
1718     has_all_cache_options =
1719         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
1720         qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
1721         qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1722 
1723     if (has_all_cache_options && !has_new_total_cache_size) {
1724         qdict_del(options, QCOW2_OPT_CACHE_SIZE);
1725     }
1726 }
1727 
1728 static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
1729                                               bool want_zero,
1730                                               int64_t offset, int64_t count,
1731                                               int64_t *pnum, int64_t *map,
1732                                               BlockDriverState **file)
1733 {
1734     BDRVQcow2State *s = bs->opaque;
1735     uint64_t cluster_offset;
1736     int index_in_cluster, ret;
1737     unsigned int bytes;
1738     int status = 0;
1739 
1740     bytes = MIN(INT_MAX, count);
1741     qemu_co_mutex_lock(&s->lock);
1742     ret = qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset);
1743     qemu_co_mutex_unlock(&s->lock);
1744     if (ret < 0) {
1745         return ret;
1746     }
1747 
1748     *pnum = bytes;
1749 
1750     if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1751         !s->crypto) {
1752         index_in_cluster = offset & (s->cluster_size - 1);
1753         *map = cluster_offset | index_in_cluster;
1754         *file = bs->file->bs;
1755         status |= BDRV_BLOCK_OFFSET_VALID;
1756     }
1757     if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
1758         status |= BDRV_BLOCK_ZERO;
1759     } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1760         status |= BDRV_BLOCK_DATA;
1761     }
1762     return status;
1763 }
1764 
1765 static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs,
1766                                             QCowL2Meta **pl2meta,
1767                                             bool link_l2)
1768 {
1769     int ret = 0;
1770     QCowL2Meta *l2meta = *pl2meta;
1771 
1772     while (l2meta != NULL) {
1773         QCowL2Meta *next;
1774 
1775         if (!ret && link_l2) {
1776             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1777             if (ret) {
1778                 goto out;
1779             }
1780         }
1781 
1782         /* Take the request off the list of running requests */
1783         if (l2meta->nb_clusters != 0) {
1784             QLIST_REMOVE(l2meta, next_in_flight);
1785         }
1786 
1787         qemu_co_queue_restart_all(&l2meta->dependent_requests);
1788 
1789         next = l2meta->next;
1790         g_free(l2meta);
1791         l2meta = next;
1792     }
1793 out:
1794     *pl2meta = l2meta;
1795     return ret;
1796 }
1797 
1798 static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
1799                                         uint64_t bytes, QEMUIOVector *qiov,
1800                                         int flags)
1801 {
1802     BDRVQcow2State *s = bs->opaque;
1803     int offset_in_cluster;
1804     int ret;
1805     unsigned int cur_bytes; /* number of bytes in current iteration */
1806     uint64_t cluster_offset = 0;
1807     uint64_t bytes_done = 0;
1808     QEMUIOVector hd_qiov;
1809     uint8_t *cluster_data = NULL;
1810 
1811     qemu_iovec_init(&hd_qiov, qiov->niov);
1812 
1813     qemu_co_mutex_lock(&s->lock);
1814 
1815     while (bytes != 0) {
1816 
1817         /* prepare next request */
1818         cur_bytes = MIN(bytes, INT_MAX);
1819         if (s->crypto) {
1820             cur_bytes = MIN(cur_bytes,
1821                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1822         }
1823 
1824         ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
1825         if (ret < 0) {
1826             goto fail;
1827         }
1828 
1829         offset_in_cluster = offset_into_cluster(s, offset);
1830 
1831         qemu_iovec_reset(&hd_qiov);
1832         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
1833 
1834         switch (ret) {
1835         case QCOW2_CLUSTER_UNALLOCATED:
1836 
1837             if (bs->backing) {
1838                 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1839                 qemu_co_mutex_unlock(&s->lock);
1840                 ret = bdrv_co_preadv(bs->backing, offset, cur_bytes,
1841                                      &hd_qiov, 0);
1842                 qemu_co_mutex_lock(&s->lock);
1843                 if (ret < 0) {
1844                     goto fail;
1845                 }
1846             } else {
1847                 /* Note: in this case, no need to wait */
1848                 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
1849             }
1850             break;
1851 
1852         case QCOW2_CLUSTER_ZERO_PLAIN:
1853         case QCOW2_CLUSTER_ZERO_ALLOC:
1854             qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
1855             break;
1856 
1857         case QCOW2_CLUSTER_COMPRESSED:
1858             /* add AIO support for compressed blocks ? */
1859             ret = qcow2_decompress_cluster(bs, cluster_offset);
1860             if (ret < 0) {
1861                 goto fail;
1862             }
1863 
1864             qemu_iovec_from_buf(&hd_qiov, 0,
1865                                 s->cluster_cache + offset_in_cluster,
1866                                 cur_bytes);
1867             break;
1868 
1869         case QCOW2_CLUSTER_NORMAL:
1870             if ((cluster_offset & 511) != 0) {
1871                 ret = -EIO;
1872                 goto fail;
1873             }
1874 
1875             if (bs->encrypted) {
1876                 assert(s->crypto);
1877 
1878                 /*
1879                  * For encrypted images, read everything into a temporary
1880                  * contiguous buffer on which the AES functions can work.
1881                  */
1882                 if (!cluster_data) {
1883                     cluster_data =
1884                         qemu_try_blockalign(bs->file->bs,
1885                                             QCOW_MAX_CRYPT_CLUSTERS
1886                                             * s->cluster_size);
1887                     if (cluster_data == NULL) {
1888                         ret = -ENOMEM;
1889                         goto fail;
1890                     }
1891                 }
1892 
1893                 assert(cur_bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1894                 qemu_iovec_reset(&hd_qiov);
1895                 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
1896             }
1897 
1898             BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1899             qemu_co_mutex_unlock(&s->lock);
1900             ret = bdrv_co_preadv(bs->file,
1901                                  cluster_offset + offset_in_cluster,
1902                                  cur_bytes, &hd_qiov, 0);
1903             qemu_co_mutex_lock(&s->lock);
1904             if (ret < 0) {
1905                 goto fail;
1906             }
1907             if (bs->encrypted) {
1908                 assert(s->crypto);
1909                 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1910                 assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
1911                 if (qcrypto_block_decrypt(s->crypto,
1912                                           (s->crypt_physical_offset ?
1913                                            cluster_offset + offset_in_cluster :
1914                                            offset),
1915                                           cluster_data,
1916                                           cur_bytes,
1917                                           NULL) < 0) {
1918                     ret = -EIO;
1919                     goto fail;
1920                 }
1921                 qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_bytes);
1922             }
1923             break;
1924 
1925         default:
1926             g_assert_not_reached();
1927             ret = -EIO;
1928             goto fail;
1929         }
1930 
1931         bytes -= cur_bytes;
1932         offset += cur_bytes;
1933         bytes_done += cur_bytes;
1934     }
1935     ret = 0;
1936 
1937 fail:
1938     qemu_co_mutex_unlock(&s->lock);
1939 
1940     qemu_iovec_destroy(&hd_qiov);
1941     qemu_vfree(cluster_data);
1942 
1943     return ret;
1944 }
1945 
1946 /* Check if it's possible to merge a write request with the writing of
1947  * the data from the COW regions */
1948 static bool merge_cow(uint64_t offset, unsigned bytes,
1949                       QEMUIOVector *hd_qiov, QCowL2Meta *l2meta)
1950 {
1951     QCowL2Meta *m;
1952 
1953     for (m = l2meta; m != NULL; m = m->next) {
1954         /* If both COW regions are empty then there's nothing to merge */
1955         if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
1956             continue;
1957         }
1958 
1959         /* The data (middle) region must be immediately after the
1960          * start region */
1961         if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
1962             continue;
1963         }
1964 
1965         /* The end region must be immediately after the data (middle)
1966          * region */
1967         if (m->offset + m->cow_end.offset != offset + bytes) {
1968             continue;
1969         }
1970 
1971         /* Make sure that adding both COW regions to the QEMUIOVector
1972          * does not exceed IOV_MAX */
1973         if (hd_qiov->niov > IOV_MAX - 2) {
1974             continue;
1975         }
1976 
1977         m->data_qiov = hd_qiov;
1978         return true;
1979     }
1980 
1981     return false;
1982 }
1983 
1984 static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
1985                                          uint64_t bytes, QEMUIOVector *qiov,
1986                                          int flags)
1987 {
1988     BDRVQcow2State *s = bs->opaque;
1989     int offset_in_cluster;
1990     int ret;
1991     unsigned int cur_bytes; /* number of sectors in current iteration */
1992     uint64_t cluster_offset;
1993     QEMUIOVector hd_qiov;
1994     uint64_t bytes_done = 0;
1995     uint8_t *cluster_data = NULL;
1996     QCowL2Meta *l2meta = NULL;
1997 
1998     trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
1999 
2000     qemu_iovec_init(&hd_qiov, qiov->niov);
2001 
2002     s->cluster_cache_offset = -1; /* disable compressed cache */
2003 
2004     qemu_co_mutex_lock(&s->lock);
2005 
2006     while (bytes != 0) {
2007 
2008         l2meta = NULL;
2009 
2010         trace_qcow2_writev_start_part(qemu_coroutine_self());
2011         offset_in_cluster = offset_into_cluster(s, offset);
2012         cur_bytes = MIN(bytes, INT_MAX);
2013         if (bs->encrypted) {
2014             cur_bytes = MIN(cur_bytes,
2015                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2016                             - offset_in_cluster);
2017         }
2018 
2019         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2020                                          &cluster_offset, &l2meta);
2021         if (ret < 0) {
2022             goto fail;
2023         }
2024 
2025         assert((cluster_offset & 511) == 0);
2026 
2027         qemu_iovec_reset(&hd_qiov);
2028         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
2029 
2030         if (bs->encrypted) {
2031             assert(s->crypto);
2032             if (!cluster_data) {
2033                 cluster_data = qemu_try_blockalign(bs->file->bs,
2034                                                    QCOW_MAX_CRYPT_CLUSTERS
2035                                                    * s->cluster_size);
2036                 if (cluster_data == NULL) {
2037                     ret = -ENOMEM;
2038                     goto fail;
2039                 }
2040             }
2041 
2042             assert(hd_qiov.size <=
2043                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2044             qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
2045 
2046             if (qcrypto_block_encrypt(s->crypto,
2047                                       (s->crypt_physical_offset ?
2048                                        cluster_offset + offset_in_cluster :
2049                                        offset),
2050                                       cluster_data,
2051                                       cur_bytes, NULL) < 0) {
2052                 ret = -EIO;
2053                 goto fail;
2054             }
2055 
2056             qemu_iovec_reset(&hd_qiov);
2057             qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
2058         }
2059 
2060         ret = qcow2_pre_write_overlap_check(bs, 0,
2061                 cluster_offset + offset_in_cluster, cur_bytes);
2062         if (ret < 0) {
2063             goto fail;
2064         }
2065 
2066         /* If we need to do COW, check if it's possible to merge the
2067          * writing of the guest data together with that of the COW regions.
2068          * If it's not possible (or not necessary) then write the
2069          * guest data now. */
2070         if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
2071             qemu_co_mutex_unlock(&s->lock);
2072             BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
2073             trace_qcow2_writev_data(qemu_coroutine_self(),
2074                                     cluster_offset + offset_in_cluster);
2075             ret = bdrv_co_pwritev(bs->file,
2076                                   cluster_offset + offset_in_cluster,
2077                                   cur_bytes, &hd_qiov, 0);
2078             qemu_co_mutex_lock(&s->lock);
2079             if (ret < 0) {
2080                 goto fail;
2081             }
2082         }
2083 
2084         ret = qcow2_handle_l2meta(bs, &l2meta, true);
2085         if (ret) {
2086             goto fail;
2087         }
2088 
2089         bytes -= cur_bytes;
2090         offset += cur_bytes;
2091         bytes_done += cur_bytes;
2092         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
2093     }
2094     ret = 0;
2095 
2096 fail:
2097     qcow2_handle_l2meta(bs, &l2meta, false);
2098 
2099     qemu_co_mutex_unlock(&s->lock);
2100 
2101     qemu_iovec_destroy(&hd_qiov);
2102     qemu_vfree(cluster_data);
2103     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
2104 
2105     return ret;
2106 }
2107 
2108 static int qcow2_inactivate(BlockDriverState *bs)
2109 {
2110     BDRVQcow2State *s = bs->opaque;
2111     int ret, result = 0;
2112     Error *local_err = NULL;
2113 
2114     qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
2115     if (local_err != NULL) {
2116         result = -EINVAL;
2117         error_report_err(local_err);
2118         error_report("Persistent bitmaps are lost for node '%s'",
2119                      bdrv_get_device_or_node_name(bs));
2120     }
2121 
2122     ret = qcow2_cache_flush(bs, s->l2_table_cache);
2123     if (ret) {
2124         result = ret;
2125         error_report("Failed to flush the L2 table cache: %s",
2126                      strerror(-ret));
2127     }
2128 
2129     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2130     if (ret) {
2131         result = ret;
2132         error_report("Failed to flush the refcount block cache: %s",
2133                      strerror(-ret));
2134     }
2135 
2136     if (result == 0) {
2137         qcow2_mark_clean(bs);
2138     }
2139 
2140     return result;
2141 }
2142 
2143 static void qcow2_close(BlockDriverState *bs)
2144 {
2145     BDRVQcow2State *s = bs->opaque;
2146     qemu_vfree(s->l1_table);
2147     /* else pre-write overlap checks in cache_destroy may crash */
2148     s->l1_table = NULL;
2149 
2150     if (!(s->flags & BDRV_O_INACTIVE)) {
2151         qcow2_inactivate(bs);
2152     }
2153 
2154     cache_clean_timer_del(bs);
2155     qcow2_cache_destroy(s->l2_table_cache);
2156     qcow2_cache_destroy(s->refcount_block_cache);
2157 
2158     qcrypto_block_free(s->crypto);
2159     s->crypto = NULL;
2160 
2161     g_free(s->unknown_header_fields);
2162     cleanup_unknown_header_ext(bs);
2163 
2164     g_free(s->image_backing_file);
2165     g_free(s->image_backing_format);
2166 
2167     g_free(s->cluster_cache);
2168     qemu_vfree(s->cluster_data);
2169     qcow2_refcount_close(bs);
2170     qcow2_free_snapshots(bs);
2171 }
2172 
2173 static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs,
2174                                                    Error **errp)
2175 {
2176     BDRVQcow2State *s = bs->opaque;
2177     int flags = s->flags;
2178     QCryptoBlock *crypto = NULL;
2179     QDict *options;
2180     Error *local_err = NULL;
2181     int ret;
2182 
2183     /*
2184      * Backing files are read-only which makes all of their metadata immutable,
2185      * that means we don't have to worry about reopening them here.
2186      */
2187 
2188     crypto = s->crypto;
2189     s->crypto = NULL;
2190 
2191     qcow2_close(bs);
2192 
2193     memset(s, 0, sizeof(BDRVQcow2State));
2194     options = qdict_clone_shallow(bs->options);
2195 
2196     flags &= ~BDRV_O_INACTIVE;
2197     qemu_co_mutex_lock(&s->lock);
2198     ret = qcow2_do_open(bs, options, flags, &local_err);
2199     qemu_co_mutex_unlock(&s->lock);
2200     qobject_unref(options);
2201     if (local_err) {
2202         error_propagate(errp, local_err);
2203         error_prepend(errp, "Could not reopen qcow2 layer: ");
2204         bs->drv = NULL;
2205         return;
2206     } else if (ret < 0) {
2207         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
2208         bs->drv = NULL;
2209         return;
2210     }
2211 
2212     s->crypto = crypto;
2213 }
2214 
2215 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2216     size_t len, size_t buflen)
2217 {
2218     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2219     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2220 
2221     if (buflen < ext_len) {
2222         return -ENOSPC;
2223     }
2224 
2225     *ext_backing_fmt = (QCowExtension) {
2226         .magic  = cpu_to_be32(magic),
2227         .len    = cpu_to_be32(len),
2228     };
2229 
2230     if (len) {
2231         memcpy(buf + sizeof(QCowExtension), s, len);
2232     }
2233 
2234     return ext_len;
2235 }
2236 
2237 /*
2238  * Updates the qcow2 header, including the variable length parts of it, i.e.
2239  * the backing file name and all extensions. qcow2 was not designed to allow
2240  * such changes, so if we run out of space (we can only use the first cluster)
2241  * this function may fail.
2242  *
2243  * Returns 0 on success, -errno in error cases.
2244  */
2245 int qcow2_update_header(BlockDriverState *bs)
2246 {
2247     BDRVQcow2State *s = bs->opaque;
2248     QCowHeader *header;
2249     char *buf;
2250     size_t buflen = s->cluster_size;
2251     int ret;
2252     uint64_t total_size;
2253     uint32_t refcount_table_clusters;
2254     size_t header_length;
2255     Qcow2UnknownHeaderExtension *uext;
2256 
2257     buf = qemu_blockalign(bs, buflen);
2258 
2259     /* Header structure */
2260     header = (QCowHeader*) buf;
2261 
2262     if (buflen < sizeof(*header)) {
2263         ret = -ENOSPC;
2264         goto fail;
2265     }
2266 
2267     header_length = sizeof(*header) + s->unknown_header_fields_size;
2268     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2269     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2270 
2271     *header = (QCowHeader) {
2272         /* Version 2 fields */
2273         .magic                  = cpu_to_be32(QCOW_MAGIC),
2274         .version                = cpu_to_be32(s->qcow_version),
2275         .backing_file_offset    = 0,
2276         .backing_file_size      = 0,
2277         .cluster_bits           = cpu_to_be32(s->cluster_bits),
2278         .size                   = cpu_to_be64(total_size),
2279         .crypt_method           = cpu_to_be32(s->crypt_method_header),
2280         .l1_size                = cpu_to_be32(s->l1_size),
2281         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
2282         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
2283         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2284         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
2285         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
2286 
2287         /* Version 3 fields */
2288         .incompatible_features  = cpu_to_be64(s->incompatible_features),
2289         .compatible_features    = cpu_to_be64(s->compatible_features),
2290         .autoclear_features     = cpu_to_be64(s->autoclear_features),
2291         .refcount_order         = cpu_to_be32(s->refcount_order),
2292         .header_length          = cpu_to_be32(header_length),
2293     };
2294 
2295     /* For older versions, write a shorter header */
2296     switch (s->qcow_version) {
2297     case 2:
2298         ret = offsetof(QCowHeader, incompatible_features);
2299         break;
2300     case 3:
2301         ret = sizeof(*header);
2302         break;
2303     default:
2304         ret = -EINVAL;
2305         goto fail;
2306     }
2307 
2308     buf += ret;
2309     buflen -= ret;
2310     memset(buf, 0, buflen);
2311 
2312     /* Preserve any unknown field in the header */
2313     if (s->unknown_header_fields_size) {
2314         if (buflen < s->unknown_header_fields_size) {
2315             ret = -ENOSPC;
2316             goto fail;
2317         }
2318 
2319         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
2320         buf += s->unknown_header_fields_size;
2321         buflen -= s->unknown_header_fields_size;
2322     }
2323 
2324     /* Backing file format header extension */
2325     if (s->image_backing_format) {
2326         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
2327                              s->image_backing_format,
2328                              strlen(s->image_backing_format),
2329                              buflen);
2330         if (ret < 0) {
2331             goto fail;
2332         }
2333 
2334         buf += ret;
2335         buflen -= ret;
2336     }
2337 
2338     /* Full disk encryption header pointer extension */
2339     if (s->crypto_header.offset != 0) {
2340         cpu_to_be64s(&s->crypto_header.offset);
2341         cpu_to_be64s(&s->crypto_header.length);
2342         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
2343                              &s->crypto_header, sizeof(s->crypto_header),
2344                              buflen);
2345         be64_to_cpus(&s->crypto_header.offset);
2346         be64_to_cpus(&s->crypto_header.length);
2347         if (ret < 0) {
2348             goto fail;
2349         }
2350         buf += ret;
2351         buflen -= ret;
2352     }
2353 
2354     /* Feature table */
2355     if (s->qcow_version >= 3) {
2356         Qcow2Feature features[] = {
2357             {
2358                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2359                 .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
2360                 .name = "dirty bit",
2361             },
2362             {
2363                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2364                 .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
2365                 .name = "corrupt bit",
2366             },
2367             {
2368                 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2369                 .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2370                 .name = "lazy refcounts",
2371             },
2372         };
2373 
2374         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2375                              features, sizeof(features), buflen);
2376         if (ret < 0) {
2377             goto fail;
2378         }
2379         buf += ret;
2380         buflen -= ret;
2381     }
2382 
2383     /* Bitmap extension */
2384     if (s->nb_bitmaps > 0) {
2385         Qcow2BitmapHeaderExt bitmaps_header = {
2386             .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
2387             .bitmap_directory_size =
2388                     cpu_to_be64(s->bitmap_directory_size),
2389             .bitmap_directory_offset =
2390                     cpu_to_be64(s->bitmap_directory_offset)
2391         };
2392         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
2393                              &bitmaps_header, sizeof(bitmaps_header),
2394                              buflen);
2395         if (ret < 0) {
2396             goto fail;
2397         }
2398         buf += ret;
2399         buflen -= ret;
2400     }
2401 
2402     /* Keep unknown header extensions */
2403     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
2404         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
2405         if (ret < 0) {
2406             goto fail;
2407         }
2408 
2409         buf += ret;
2410         buflen -= ret;
2411     }
2412 
2413     /* End of header extensions */
2414     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
2415     if (ret < 0) {
2416         goto fail;
2417     }
2418 
2419     buf += ret;
2420     buflen -= ret;
2421 
2422     /* Backing file name */
2423     if (s->image_backing_file) {
2424         size_t backing_file_len = strlen(s->image_backing_file);
2425 
2426         if (buflen < backing_file_len) {
2427             ret = -ENOSPC;
2428             goto fail;
2429         }
2430 
2431         /* Using strncpy is ok here, since buf is not NUL-terminated. */
2432         strncpy(buf, s->image_backing_file, buflen);
2433 
2434         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
2435         header->backing_file_size   = cpu_to_be32(backing_file_len);
2436     }
2437 
2438     /* Write the new header */
2439     ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
2440     if (ret < 0) {
2441         goto fail;
2442     }
2443 
2444     ret = 0;
2445 fail:
2446     qemu_vfree(header);
2447     return ret;
2448 }
2449 
2450 static int qcow2_change_backing_file(BlockDriverState *bs,
2451     const char *backing_file, const char *backing_fmt)
2452 {
2453     BDRVQcow2State *s = bs->opaque;
2454 
2455     if (backing_file && strlen(backing_file) > 1023) {
2456         return -EINVAL;
2457     }
2458 
2459     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2460     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2461 
2462     g_free(s->image_backing_file);
2463     g_free(s->image_backing_format);
2464 
2465     s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2466     s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2467 
2468     return qcow2_update_header(bs);
2469 }
2470 
2471 static int qcow2_crypt_method_from_format(const char *encryptfmt)
2472 {
2473     if (g_str_equal(encryptfmt, "luks")) {
2474         return QCOW_CRYPT_LUKS;
2475     } else if (g_str_equal(encryptfmt, "aes")) {
2476         return QCOW_CRYPT_AES;
2477     } else {
2478         return -EINVAL;
2479     }
2480 }
2481 
2482 static int qcow2_set_up_encryption(BlockDriverState *bs,
2483                                    QCryptoBlockCreateOptions *cryptoopts,
2484                                    Error **errp)
2485 {
2486     BDRVQcow2State *s = bs->opaque;
2487     QCryptoBlock *crypto = NULL;
2488     int fmt, ret;
2489 
2490     switch (cryptoopts->format) {
2491     case Q_CRYPTO_BLOCK_FORMAT_LUKS:
2492         fmt = QCOW_CRYPT_LUKS;
2493         break;
2494     case Q_CRYPTO_BLOCK_FORMAT_QCOW:
2495         fmt = QCOW_CRYPT_AES;
2496         break;
2497     default:
2498         error_setg(errp, "Crypto format not supported in qcow2");
2499         return -EINVAL;
2500     }
2501 
2502     s->crypt_method_header = fmt;
2503 
2504     crypto = qcrypto_block_create(cryptoopts, "encrypt.",
2505                                   qcow2_crypto_hdr_init_func,
2506                                   qcow2_crypto_hdr_write_func,
2507                                   bs, errp);
2508     if (!crypto) {
2509         return -EINVAL;
2510     }
2511 
2512     ret = qcow2_update_header(bs);
2513     if (ret < 0) {
2514         error_setg_errno(errp, -ret, "Could not write encryption header");
2515         goto out;
2516     }
2517 
2518     ret = 0;
2519  out:
2520     qcrypto_block_free(crypto);
2521     return ret;
2522 }
2523 
2524 
2525 typedef struct PreallocCo {
2526     BlockDriverState *bs;
2527     uint64_t offset;
2528     uint64_t new_length;
2529 
2530     int ret;
2531 } PreallocCo;
2532 
2533 /**
2534  * Preallocates metadata structures for data clusters between @offset (in the
2535  * guest disk) and @new_length (which is thus generally the new guest disk
2536  * size).
2537  *
2538  * Returns: 0 on success, -errno on failure.
2539  */
2540 static void coroutine_fn preallocate_co(void *opaque)
2541 {
2542     PreallocCo *params = opaque;
2543     BlockDriverState *bs = params->bs;
2544     uint64_t offset = params->offset;
2545     uint64_t new_length = params->new_length;
2546     BDRVQcow2State *s = bs->opaque;
2547     uint64_t bytes;
2548     uint64_t host_offset = 0;
2549     unsigned int cur_bytes;
2550     int ret;
2551     QCowL2Meta *meta;
2552 
2553     qemu_co_mutex_lock(&s->lock);
2554 
2555     assert(offset <= new_length);
2556     bytes = new_length - offset;
2557 
2558     while (bytes) {
2559         cur_bytes = MIN(bytes, INT_MAX);
2560         ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
2561                                          &host_offset, &meta);
2562         if (ret < 0) {
2563             goto done;
2564         }
2565 
2566         while (meta) {
2567             QCowL2Meta *next = meta->next;
2568 
2569             ret = qcow2_alloc_cluster_link_l2(bs, meta);
2570             if (ret < 0) {
2571                 qcow2_free_any_clusters(bs, meta->alloc_offset,
2572                                         meta->nb_clusters, QCOW2_DISCARD_NEVER);
2573                 goto done;
2574             }
2575 
2576             /* There are no dependent requests, but we need to remove our
2577              * request from the list of in-flight requests */
2578             QLIST_REMOVE(meta, next_in_flight);
2579 
2580             g_free(meta);
2581             meta = next;
2582         }
2583 
2584         /* TODO Preallocate data if requested */
2585 
2586         bytes -= cur_bytes;
2587         offset += cur_bytes;
2588     }
2589 
2590     /*
2591      * It is expected that the image file is large enough to actually contain
2592      * all of the allocated clusters (otherwise we get failing reads after
2593      * EOF). Extend the image to the last allocated sector.
2594      */
2595     if (host_offset != 0) {
2596         uint8_t data = 0;
2597         ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
2598                           &data, 1);
2599         if (ret < 0) {
2600             goto done;
2601         }
2602     }
2603 
2604     ret = 0;
2605 
2606 done:
2607     qemu_co_mutex_unlock(&s->lock);
2608     params->ret = ret;
2609 }
2610 
2611 static int preallocate(BlockDriverState *bs,
2612                        uint64_t offset, uint64_t new_length)
2613 {
2614     PreallocCo params = {
2615         .bs         = bs,
2616         .offset     = offset,
2617         .new_length = new_length,
2618         .ret        = -EINPROGRESS,
2619     };
2620 
2621     if (qemu_in_coroutine()) {
2622         preallocate_co(&params);
2623     } else {
2624         Coroutine *co = qemu_coroutine_create(preallocate_co, &params);
2625         bdrv_coroutine_enter(bs, co);
2626         BDRV_POLL_WHILE(bs, params.ret == -EINPROGRESS);
2627     }
2628     return params.ret;
2629 }
2630 
2631 /* qcow2_refcount_metadata_size:
2632  * @clusters: number of clusters to refcount (including data and L1/L2 tables)
2633  * @cluster_size: size of a cluster, in bytes
2634  * @refcount_order: refcount bits power-of-2 exponent
2635  * @generous_increase: allow for the refcount table to be 1.5x as large as it
2636  *                     needs to be
2637  *
2638  * Returns: Number of bytes required for refcount blocks and table metadata.
2639  */
2640 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
2641                                      int refcount_order, bool generous_increase,
2642                                      uint64_t *refblock_count)
2643 {
2644     /*
2645      * Every host cluster is reference-counted, including metadata (even
2646      * refcount metadata is recursively included).
2647      *
2648      * An accurate formula for the size of refcount metadata size is difficult
2649      * to derive.  An easier method of calculation is finding the fixed point
2650      * where no further refcount blocks or table clusters are required to
2651      * reference count every cluster.
2652      */
2653     int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
2654     int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
2655     int64_t table = 0;  /* number of refcount table clusters */
2656     int64_t blocks = 0; /* number of refcount block clusters */
2657     int64_t last;
2658     int64_t n = 0;
2659 
2660     do {
2661         last = n;
2662         blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
2663         table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
2664         n = clusters + blocks + table;
2665 
2666         if (n == last && generous_increase) {
2667             clusters += DIV_ROUND_UP(table, 2);
2668             n = 0; /* force another loop */
2669             generous_increase = false;
2670         }
2671     } while (n != last);
2672 
2673     if (refblock_count) {
2674         *refblock_count = blocks;
2675     }
2676 
2677     return (blocks + table) * cluster_size;
2678 }
2679 
2680 /**
2681  * qcow2_calc_prealloc_size:
2682  * @total_size: virtual disk size in bytes
2683  * @cluster_size: cluster size in bytes
2684  * @refcount_order: refcount bits power-of-2 exponent
2685  *
2686  * Returns: Total number of bytes required for the fully allocated image
2687  * (including metadata).
2688  */
2689 static int64_t qcow2_calc_prealloc_size(int64_t total_size,
2690                                         size_t cluster_size,
2691                                         int refcount_order)
2692 {
2693     int64_t meta_size = 0;
2694     uint64_t nl1e, nl2e;
2695     int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
2696 
2697     /* header: 1 cluster */
2698     meta_size += cluster_size;
2699 
2700     /* total size of L2 tables */
2701     nl2e = aligned_total_size / cluster_size;
2702     nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t));
2703     meta_size += nl2e * sizeof(uint64_t);
2704 
2705     /* total size of L1 tables */
2706     nl1e = nl2e * sizeof(uint64_t) / cluster_size;
2707     nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t));
2708     meta_size += nl1e * sizeof(uint64_t);
2709 
2710     /* total size of refcount table and blocks */
2711     meta_size += qcow2_refcount_metadata_size(
2712             (meta_size + aligned_total_size) / cluster_size,
2713             cluster_size, refcount_order, false, NULL);
2714 
2715     return meta_size + aligned_total_size;
2716 }
2717 
2718 static bool validate_cluster_size(size_t cluster_size, Error **errp)
2719 {
2720     int cluster_bits = ctz32(cluster_size);
2721     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
2722         (1 << cluster_bits) != cluster_size)
2723     {
2724         error_setg(errp, "Cluster size must be a power of two between %d and "
2725                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
2726         return false;
2727     }
2728     return true;
2729 }
2730 
2731 static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
2732 {
2733     size_t cluster_size;
2734 
2735     cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2736                                          DEFAULT_CLUSTER_SIZE);
2737     if (!validate_cluster_size(cluster_size, errp)) {
2738         return 0;
2739     }
2740     return cluster_size;
2741 }
2742 
2743 static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
2744 {
2745     char *buf;
2746     int ret;
2747 
2748     buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2749     if (!buf) {
2750         ret = 3; /* default */
2751     } else if (!strcmp(buf, "0.10")) {
2752         ret = 2;
2753     } else if (!strcmp(buf, "1.1")) {
2754         ret = 3;
2755     } else {
2756         error_setg(errp, "Invalid compatibility level: '%s'", buf);
2757         ret = -EINVAL;
2758     }
2759     g_free(buf);
2760     return ret;
2761 }
2762 
2763 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
2764                                                 Error **errp)
2765 {
2766     uint64_t refcount_bits;
2767 
2768     refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
2769     if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2770         error_setg(errp, "Refcount width must be a power of two and may not "
2771                    "exceed 64 bits");
2772         return 0;
2773     }
2774 
2775     if (version < 3 && refcount_bits != 16) {
2776         error_setg(errp, "Different refcount widths than 16 bits require "
2777                    "compatibility level 1.1 or above (use compat=1.1 or "
2778                    "greater)");
2779         return 0;
2780     }
2781 
2782     return refcount_bits;
2783 }
2784 
2785 static int coroutine_fn
2786 qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
2787 {
2788     BlockdevCreateOptionsQcow2 *qcow2_opts;
2789     QDict *options;
2790 
2791     /*
2792      * Open the image file and write a minimal qcow2 header.
2793      *
2794      * We keep things simple and start with a zero-sized image. We also
2795      * do without refcount blocks or a L1 table for now. We'll fix the
2796      * inconsistency later.
2797      *
2798      * We do need a refcount table because growing the refcount table means
2799      * allocating two new refcount blocks - the seconds of which would be at
2800      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
2801      * size for any qcow2 image.
2802      */
2803     BlockBackend *blk = NULL;
2804     BlockDriverState *bs = NULL;
2805     QCowHeader *header;
2806     size_t cluster_size;
2807     int version;
2808     int refcount_order;
2809     uint64_t* refcount_table;
2810     Error *local_err = NULL;
2811     int ret;
2812 
2813     assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
2814     qcow2_opts = &create_options->u.qcow2;
2815 
2816     bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp);
2817     if (bs == NULL) {
2818         return -EIO;
2819     }
2820 
2821     /* Validate options and set default values */
2822     if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
2823         error_setg(errp, "Image size must be a multiple of 512 bytes");
2824         ret = -EINVAL;
2825         goto out;
2826     }
2827 
2828     if (qcow2_opts->has_version) {
2829         switch (qcow2_opts->version) {
2830         case BLOCKDEV_QCOW2_VERSION_V2:
2831             version = 2;
2832             break;
2833         case BLOCKDEV_QCOW2_VERSION_V3:
2834             version = 3;
2835             break;
2836         default:
2837             g_assert_not_reached();
2838         }
2839     } else {
2840         version = 3;
2841     }
2842 
2843     if (qcow2_opts->has_cluster_size) {
2844         cluster_size = qcow2_opts->cluster_size;
2845     } else {
2846         cluster_size = DEFAULT_CLUSTER_SIZE;
2847     }
2848 
2849     if (!validate_cluster_size(cluster_size, errp)) {
2850         ret = -EINVAL;
2851         goto out;
2852     }
2853 
2854     if (!qcow2_opts->has_preallocation) {
2855         qcow2_opts->preallocation = PREALLOC_MODE_OFF;
2856     }
2857     if (qcow2_opts->has_backing_file &&
2858         qcow2_opts->preallocation != PREALLOC_MODE_OFF)
2859     {
2860         error_setg(errp, "Backing file and preallocation cannot be used at "
2861                    "the same time");
2862         ret = -EINVAL;
2863         goto out;
2864     }
2865     if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) {
2866         error_setg(errp, "Backing format cannot be used without backing file");
2867         ret = -EINVAL;
2868         goto out;
2869     }
2870 
2871     if (!qcow2_opts->has_lazy_refcounts) {
2872         qcow2_opts->lazy_refcounts = false;
2873     }
2874     if (version < 3 && qcow2_opts->lazy_refcounts) {
2875         error_setg(errp, "Lazy refcounts only supported with compatibility "
2876                    "level 1.1 and above (use version=v3 or greater)");
2877         ret = -EINVAL;
2878         goto out;
2879     }
2880 
2881     if (!qcow2_opts->has_refcount_bits) {
2882         qcow2_opts->refcount_bits = 16;
2883     }
2884     if (qcow2_opts->refcount_bits > 64 ||
2885         !is_power_of_2(qcow2_opts->refcount_bits))
2886     {
2887         error_setg(errp, "Refcount width must be a power of two and may not "
2888                    "exceed 64 bits");
2889         ret = -EINVAL;
2890         goto out;
2891     }
2892     if (version < 3 && qcow2_opts->refcount_bits != 16) {
2893         error_setg(errp, "Different refcount widths than 16 bits require "
2894                    "compatibility level 1.1 or above (use version=v3 or "
2895                    "greater)");
2896         ret = -EINVAL;
2897         goto out;
2898     }
2899     refcount_order = ctz32(qcow2_opts->refcount_bits);
2900 
2901 
2902     /* Create BlockBackend to write to the image */
2903     blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
2904     ret = blk_insert_bs(blk, bs, errp);
2905     if (ret < 0) {
2906         goto out;
2907     }
2908     blk_set_allow_write_beyond_eof(blk, true);
2909 
2910     /* Clear the protocol layer and preallocate it if necessary */
2911     ret = blk_truncate(blk, 0, PREALLOC_MODE_OFF, errp);
2912     if (ret < 0) {
2913         goto out;
2914     }
2915 
2916     if (qcow2_opts->preallocation == PREALLOC_MODE_FULL ||
2917         qcow2_opts->preallocation == PREALLOC_MODE_FALLOC)
2918     {
2919         int64_t prealloc_size =
2920             qcow2_calc_prealloc_size(qcow2_opts->size, cluster_size,
2921                                      refcount_order);
2922 
2923         ret = blk_truncate(blk, prealloc_size, qcow2_opts->preallocation, errp);
2924         if (ret < 0) {
2925             goto out;
2926         }
2927     }
2928 
2929     /* Write the header */
2930     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2931     header = g_malloc0(cluster_size);
2932     *header = (QCowHeader) {
2933         .magic                      = cpu_to_be32(QCOW_MAGIC),
2934         .version                    = cpu_to_be32(version),
2935         .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
2936         .size                       = cpu_to_be64(0),
2937         .l1_table_offset            = cpu_to_be64(0),
2938         .l1_size                    = cpu_to_be32(0),
2939         .refcount_table_offset      = cpu_to_be64(cluster_size),
2940         .refcount_table_clusters    = cpu_to_be32(1),
2941         .refcount_order             = cpu_to_be32(refcount_order),
2942         .header_length              = cpu_to_be32(sizeof(*header)),
2943     };
2944 
2945     /* We'll update this to correct value later */
2946     header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2947 
2948     if (qcow2_opts->lazy_refcounts) {
2949         header->compatible_features |=
2950             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2951     }
2952 
2953     ret = blk_pwrite(blk, 0, header, cluster_size, 0);
2954     g_free(header);
2955     if (ret < 0) {
2956         error_setg_errno(errp, -ret, "Could not write qcow2 header");
2957         goto out;
2958     }
2959 
2960     /* Write a refcount table with one refcount block */
2961     refcount_table = g_malloc0(2 * cluster_size);
2962     refcount_table[0] = cpu_to_be64(2 * cluster_size);
2963     ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
2964     g_free(refcount_table);
2965 
2966     if (ret < 0) {
2967         error_setg_errno(errp, -ret, "Could not write refcount table");
2968         goto out;
2969     }
2970 
2971     blk_unref(blk);
2972     blk = NULL;
2973 
2974     /*
2975      * And now open the image and make it consistent first (i.e. increase the
2976      * refcount of the cluster that is occupied by the header and the refcount
2977      * table)
2978      */
2979     options = qdict_new();
2980     qdict_put_str(options, "driver", "qcow2");
2981     qdict_put_str(options, "file", bs->node_name);
2982     blk = blk_new_open(NULL, NULL, options,
2983                        BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
2984                        &local_err);
2985     if (blk == NULL) {
2986         error_propagate(errp, local_err);
2987         ret = -EIO;
2988         goto out;
2989     }
2990 
2991     ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
2992     if (ret < 0) {
2993         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2994                          "header and refcount table");
2995         goto out;
2996 
2997     } else if (ret != 0) {
2998         error_report("Huh, first cluster in empty image is already in use?");
2999         abort();
3000     }
3001 
3002     /* Create a full header (including things like feature table) */
3003     ret = qcow2_update_header(blk_bs(blk));
3004     if (ret < 0) {
3005         error_setg_errno(errp, -ret, "Could not update qcow2 header");
3006         goto out;
3007     }
3008 
3009     /* Okay, now that we have a valid image, let's give it the right size */
3010     ret = blk_truncate(blk, qcow2_opts->size, PREALLOC_MODE_OFF, errp);
3011     if (ret < 0) {
3012         error_prepend(errp, "Could not resize image: ");
3013         goto out;
3014     }
3015 
3016     /* Want a backing file? There you go.*/
3017     if (qcow2_opts->has_backing_file) {
3018         const char *backing_format = NULL;
3019 
3020         if (qcow2_opts->has_backing_fmt) {
3021             backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
3022         }
3023 
3024         ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
3025                                        backing_format);
3026         if (ret < 0) {
3027             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
3028                              "with format '%s'", qcow2_opts->backing_file,
3029                              backing_format);
3030             goto out;
3031         }
3032     }
3033 
3034     /* Want encryption? There you go. */
3035     if (qcow2_opts->has_encrypt) {
3036         ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
3037         if (ret < 0) {
3038             goto out;
3039         }
3040     }
3041 
3042     /* And if we're supposed to preallocate metadata, do that now */
3043     if (qcow2_opts->preallocation != PREALLOC_MODE_OFF) {
3044         ret = preallocate(blk_bs(blk), 0, qcow2_opts->size);
3045         if (ret < 0) {
3046             error_setg_errno(errp, -ret, "Could not preallocate metadata");
3047             goto out;
3048         }
3049     }
3050 
3051     blk_unref(blk);
3052     blk = NULL;
3053 
3054     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3055      * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3056      * have to setup decryption context. We're not doing any I/O on the top
3057      * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3058      * not have effect.
3059      */
3060     options = qdict_new();
3061     qdict_put_str(options, "driver", "qcow2");
3062     qdict_put_str(options, "file", bs->node_name);
3063     blk = blk_new_open(NULL, NULL, options,
3064                        BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3065                        &local_err);
3066     if (blk == NULL) {
3067         error_propagate(errp, local_err);
3068         ret = -EIO;
3069         goto out;
3070     }
3071 
3072     ret = 0;
3073 out:
3074     blk_unref(blk);
3075     bdrv_unref(bs);
3076     return ret;
3077 }
3078 
3079 static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opts,
3080                                              Error **errp)
3081 {
3082     BlockdevCreateOptions *create_options = NULL;
3083     QDict *qdict = NULL;
3084     QObject *qobj;
3085     Visitor *v;
3086     BlockDriverState *bs = NULL;
3087     Error *local_err = NULL;
3088     const char *val;
3089     int ret;
3090 
3091     /* Only the keyval visitor supports the dotted syntax needed for
3092      * encryption, so go through a QDict before getting a QAPI type. Ignore
3093      * options meant for the protocol layer so that the visitor doesn't
3094      * complain. */
3095     qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3096                                         true);
3097 
3098     /* Handle encryption options */
3099     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3100     if (val && !strcmp(val, "on")) {
3101         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3102     } else if (val && !strcmp(val, "off")) {
3103         qdict_del(qdict, BLOCK_OPT_ENCRYPT);
3104     }
3105 
3106     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3107     if (val && !strcmp(val, "aes")) {
3108         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
3109     }
3110 
3111     /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3112      * version=v2/v3 below. */
3113     val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3114     if (val && !strcmp(val, "0.10")) {
3115         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3116     } else if (val && !strcmp(val, "1.1")) {
3117         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3118     }
3119 
3120     /* Change legacy command line options into QMP ones */
3121     static const QDictRenames opt_renames[] = {
3122         { BLOCK_OPT_BACKING_FILE,       "backing-file" },
3123         { BLOCK_OPT_BACKING_FMT,        "backing-fmt" },
3124         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
3125         { BLOCK_OPT_LAZY_REFCOUNTS,     "lazy-refcounts" },
3126         { BLOCK_OPT_REFCOUNT_BITS,      "refcount-bits" },
3127         { BLOCK_OPT_ENCRYPT,            BLOCK_OPT_ENCRYPT_FORMAT },
3128         { BLOCK_OPT_COMPAT_LEVEL,       "version" },
3129         { NULL, NULL },
3130     };
3131 
3132     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
3133         ret = -EINVAL;
3134         goto finish;
3135     }
3136 
3137     /* Create and open the file (protocol layer) */
3138     ret = bdrv_create_file(filename, opts, errp);
3139     if (ret < 0) {
3140         goto finish;
3141     }
3142 
3143     bs = bdrv_open(filename, NULL, NULL,
3144                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3145     if (bs == NULL) {
3146         ret = -EIO;
3147         goto finish;
3148     }
3149 
3150     /* Set 'driver' and 'node' options */
3151     qdict_put_str(qdict, "driver", "qcow2");
3152     qdict_put_str(qdict, "file", bs->node_name);
3153 
3154     /* Now get the QAPI type BlockdevCreateOptions */
3155     qobj = qdict_crumple(qdict, errp);
3156     qobject_unref(qdict);
3157     qdict = qobject_to(QDict, qobj);
3158     if (qdict == NULL) {
3159         ret = -EINVAL;
3160         goto finish;
3161     }
3162 
3163     v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
3164     visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
3165     visit_free(v);
3166 
3167     if (local_err) {
3168         error_propagate(errp, local_err);
3169         ret = -EINVAL;
3170         goto finish;
3171     }
3172 
3173     /* Silently round up size */
3174     create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
3175                                             BDRV_SECTOR_SIZE);
3176 
3177     /* Create the qcow2 image (format layer) */
3178     ret = qcow2_co_create(create_options, errp);
3179     if (ret < 0) {
3180         goto finish;
3181     }
3182 
3183     ret = 0;
3184 finish:
3185     qobject_unref(qdict);
3186     bdrv_unref(bs);
3187     qapi_free_BlockdevCreateOptions(create_options);
3188     return ret;
3189 }
3190 
3191 
3192 static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
3193 {
3194     int64_t nr;
3195     int res;
3196 
3197     /* Clamp to image length, before checking status of underlying sectors */
3198     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
3199         bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
3200     }
3201 
3202     if (!bytes) {
3203         return true;
3204     }
3205     res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
3206     return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes;
3207 }
3208 
3209 static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
3210     int64_t offset, int bytes, BdrvRequestFlags flags)
3211 {
3212     int ret;
3213     BDRVQcow2State *s = bs->opaque;
3214 
3215     uint32_t head = offset % s->cluster_size;
3216     uint32_t tail = (offset + bytes) % s->cluster_size;
3217 
3218     trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3219     if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
3220         tail = 0;
3221     }
3222 
3223     if (head || tail) {
3224         uint64_t off;
3225         unsigned int nr;
3226 
3227         assert(head + bytes <= s->cluster_size);
3228 
3229         /* check whether remainder of cluster already reads as zero */
3230         if (!(is_zero(bs, offset - head, head) &&
3231               is_zero(bs, offset + bytes,
3232                       tail ? s->cluster_size - tail : 0))) {
3233             return -ENOTSUP;
3234         }
3235 
3236         qemu_co_mutex_lock(&s->lock);
3237         /* We can have new write after previous check */
3238         offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
3239         bytes = s->cluster_size;
3240         nr = s->cluster_size;
3241         ret = qcow2_get_cluster_offset(bs, offset, &nr, &off);
3242         if (ret != QCOW2_CLUSTER_UNALLOCATED &&
3243             ret != QCOW2_CLUSTER_ZERO_PLAIN &&
3244             ret != QCOW2_CLUSTER_ZERO_ALLOC) {
3245             qemu_co_mutex_unlock(&s->lock);
3246             return -ENOTSUP;
3247         }
3248     } else {
3249         qemu_co_mutex_lock(&s->lock);
3250     }
3251 
3252     trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
3253 
3254     /* Whatever is left can use real zero clusters */
3255     ret = qcow2_cluster_zeroize(bs, offset, bytes, flags);
3256     qemu_co_mutex_unlock(&s->lock);
3257 
3258     return ret;
3259 }
3260 
3261 static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
3262                                           int64_t offset, int bytes)
3263 {
3264     int ret;
3265     BDRVQcow2State *s = bs->opaque;
3266 
3267     if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3268         assert(bytes < s->cluster_size);
3269         /* Ignore partial clusters, except for the special case of the
3270          * complete partial cluster at the end of an unaligned file */
3271         if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
3272             offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
3273             return -ENOTSUP;
3274         }
3275     }
3276 
3277     qemu_co_mutex_lock(&s->lock);
3278     ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
3279                                 false);
3280     qemu_co_mutex_unlock(&s->lock);
3281     return ret;
3282 }
3283 
3284 static int coroutine_fn
3285 qcow2_co_copy_range_from(BlockDriverState *bs,
3286                          BdrvChild *src, uint64_t src_offset,
3287                          BdrvChild *dst, uint64_t dst_offset,
3288                          uint64_t bytes, BdrvRequestFlags flags)
3289 {
3290     BDRVQcow2State *s = bs->opaque;
3291     int ret;
3292     unsigned int cur_bytes; /* number of bytes in current iteration */
3293     BdrvChild *child = NULL;
3294     BdrvRequestFlags cur_flags;
3295 
3296     assert(!bs->encrypted);
3297     qemu_co_mutex_lock(&s->lock);
3298 
3299     while (bytes != 0) {
3300         uint64_t copy_offset = 0;
3301         /* prepare next request */
3302         cur_bytes = MIN(bytes, INT_MAX);
3303         cur_flags = flags;
3304 
3305         ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, &copy_offset);
3306         if (ret < 0) {
3307             goto out;
3308         }
3309 
3310         switch (ret) {
3311         case QCOW2_CLUSTER_UNALLOCATED:
3312             if (bs->backing && bs->backing->bs) {
3313                 int64_t backing_length = bdrv_getlength(bs->backing->bs);
3314                 if (src_offset >= backing_length) {
3315                     cur_flags |= BDRV_REQ_ZERO_WRITE;
3316                 } else {
3317                     child = bs->backing;
3318                     cur_bytes = MIN(cur_bytes, backing_length - src_offset);
3319                     copy_offset = src_offset;
3320                 }
3321             } else {
3322                 cur_flags |= BDRV_REQ_ZERO_WRITE;
3323             }
3324             break;
3325 
3326         case QCOW2_CLUSTER_ZERO_PLAIN:
3327         case QCOW2_CLUSTER_ZERO_ALLOC:
3328             cur_flags |= BDRV_REQ_ZERO_WRITE;
3329             break;
3330 
3331         case QCOW2_CLUSTER_COMPRESSED:
3332             ret = -ENOTSUP;
3333             goto out;
3334             break;
3335 
3336         case QCOW2_CLUSTER_NORMAL:
3337             child = bs->file;
3338             copy_offset += offset_into_cluster(s, src_offset);
3339             if ((copy_offset & 511) != 0) {
3340                 ret = -EIO;
3341                 goto out;
3342             }
3343             break;
3344 
3345         default:
3346             abort();
3347         }
3348         qemu_co_mutex_unlock(&s->lock);
3349         ret = bdrv_co_copy_range_from(child,
3350                                       copy_offset,
3351                                       dst, dst_offset,
3352                                       cur_bytes, cur_flags);
3353         qemu_co_mutex_lock(&s->lock);
3354         if (ret < 0) {
3355             goto out;
3356         }
3357 
3358         bytes -= cur_bytes;
3359         src_offset += cur_bytes;
3360         dst_offset += cur_bytes;
3361     }
3362     ret = 0;
3363 
3364 out:
3365     qemu_co_mutex_unlock(&s->lock);
3366     return ret;
3367 }
3368 
3369 static int coroutine_fn
3370 qcow2_co_copy_range_to(BlockDriverState *bs,
3371                        BdrvChild *src, uint64_t src_offset,
3372                        BdrvChild *dst, uint64_t dst_offset,
3373                        uint64_t bytes, BdrvRequestFlags flags)
3374 {
3375     BDRVQcow2State *s = bs->opaque;
3376     int offset_in_cluster;
3377     int ret;
3378     unsigned int cur_bytes; /* number of sectors in current iteration */
3379     uint64_t cluster_offset;
3380     uint8_t *cluster_data = NULL;
3381     QCowL2Meta *l2meta = NULL;
3382 
3383     assert(!bs->encrypted);
3384     s->cluster_cache_offset = -1; /* disable compressed cache */
3385 
3386     qemu_co_mutex_lock(&s->lock);
3387 
3388     while (bytes != 0) {
3389 
3390         l2meta = NULL;
3391 
3392         offset_in_cluster = offset_into_cluster(s, dst_offset);
3393         cur_bytes = MIN(bytes, INT_MAX);
3394 
3395         /* TODO:
3396          * If src->bs == dst->bs, we could simply copy by incrementing
3397          * the refcnt, without copying user data.
3398          * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
3399         ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes,
3400                                          &cluster_offset, &l2meta);
3401         if (ret < 0) {
3402             goto fail;
3403         }
3404 
3405         assert((cluster_offset & 511) == 0);
3406 
3407         ret = qcow2_pre_write_overlap_check(bs, 0,
3408                 cluster_offset + offset_in_cluster, cur_bytes);
3409         if (ret < 0) {
3410             goto fail;
3411         }
3412 
3413         qemu_co_mutex_unlock(&s->lock);
3414         ret = bdrv_co_copy_range_to(src, src_offset,
3415                                     bs->file,
3416                                     cluster_offset + offset_in_cluster,
3417                                     cur_bytes, flags);
3418         qemu_co_mutex_lock(&s->lock);
3419         if (ret < 0) {
3420             goto fail;
3421         }
3422 
3423         ret = qcow2_handle_l2meta(bs, &l2meta, true);
3424         if (ret) {
3425             goto fail;
3426         }
3427 
3428         bytes -= cur_bytes;
3429         dst_offset += cur_bytes;
3430     }
3431     ret = 0;
3432 
3433 fail:
3434     qcow2_handle_l2meta(bs, &l2meta, false);
3435 
3436     qemu_co_mutex_unlock(&s->lock);
3437 
3438     qemu_vfree(cluster_data);
3439     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
3440 
3441     return ret;
3442 }
3443 
3444 static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
3445                           PreallocMode prealloc, Error **errp)
3446 {
3447     BDRVQcow2State *s = bs->opaque;
3448     uint64_t old_length;
3449     int64_t new_l1_size;
3450     int ret;
3451 
3452     if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
3453         prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
3454     {
3455         error_setg(errp, "Unsupported preallocation mode '%s'",
3456                    PreallocMode_str(prealloc));
3457         return -ENOTSUP;
3458     }
3459 
3460     if (offset & 511) {
3461         error_setg(errp, "The new size must be a multiple of 512");
3462         return -EINVAL;
3463     }
3464 
3465     /* cannot proceed if image has snapshots */
3466     if (s->nb_snapshots) {
3467         error_setg(errp, "Can't resize an image which has snapshots");
3468         return -ENOTSUP;
3469     }
3470 
3471     /* cannot proceed if image has bitmaps */
3472     if (s->nb_bitmaps) {
3473         /* TODO: resize bitmaps in the image */
3474         error_setg(errp, "Can't resize an image which has bitmaps");
3475         return -ENOTSUP;
3476     }
3477 
3478     old_length = bs->total_sectors * 512;
3479     new_l1_size = size_to_l1(s, offset);
3480 
3481     if (offset < old_length) {
3482         int64_t last_cluster, old_file_size;
3483         if (prealloc != PREALLOC_MODE_OFF) {
3484             error_setg(errp,
3485                        "Preallocation can't be used for shrinking an image");
3486             return -EINVAL;
3487         }
3488 
3489         ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
3490                                     old_length - ROUND_UP(offset,
3491                                                           s->cluster_size),
3492                                     QCOW2_DISCARD_ALWAYS, true);
3493         if (ret < 0) {
3494             error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
3495             return ret;
3496         }
3497 
3498         ret = qcow2_shrink_l1_table(bs, new_l1_size);
3499         if (ret < 0) {
3500             error_setg_errno(errp, -ret,
3501                              "Failed to reduce the number of L2 tables");
3502             return ret;
3503         }
3504 
3505         ret = qcow2_shrink_reftable(bs);
3506         if (ret < 0) {
3507             error_setg_errno(errp, -ret,
3508                              "Failed to discard unused refblocks");
3509             return ret;
3510         }
3511 
3512         old_file_size = bdrv_getlength(bs->file->bs);
3513         if (old_file_size < 0) {
3514             error_setg_errno(errp, -old_file_size,
3515                              "Failed to inquire current file length");
3516             return old_file_size;
3517         }
3518         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
3519         if (last_cluster < 0) {
3520             error_setg_errno(errp, -last_cluster,
3521                              "Failed to find the last cluster");
3522             return last_cluster;
3523         }
3524         if ((last_cluster + 1) * s->cluster_size < old_file_size) {
3525             Error *local_err = NULL;
3526 
3527             bdrv_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
3528                           PREALLOC_MODE_OFF, &local_err);
3529             if (local_err) {
3530                 warn_reportf_err(local_err,
3531                                  "Failed to truncate the tail of the image: ");
3532             }
3533         }
3534     } else {
3535         ret = qcow2_grow_l1_table(bs, new_l1_size, true);
3536         if (ret < 0) {
3537             error_setg_errno(errp, -ret, "Failed to grow the L1 table");
3538             return ret;
3539         }
3540     }
3541 
3542     switch (prealloc) {
3543     case PREALLOC_MODE_OFF:
3544         break;
3545 
3546     case PREALLOC_MODE_METADATA:
3547         ret = preallocate(bs, old_length, offset);
3548         if (ret < 0) {
3549             error_setg_errno(errp, -ret, "Preallocation failed");
3550             return ret;
3551         }
3552         break;
3553 
3554     case PREALLOC_MODE_FALLOC:
3555     case PREALLOC_MODE_FULL:
3556     {
3557         int64_t allocation_start, host_offset, guest_offset;
3558         int64_t clusters_allocated;
3559         int64_t old_file_size, new_file_size;
3560         uint64_t nb_new_data_clusters, nb_new_l2_tables;
3561 
3562         old_file_size = bdrv_getlength(bs->file->bs);
3563         if (old_file_size < 0) {
3564             error_setg_errno(errp, -old_file_size,
3565                              "Failed to inquire current file length");
3566             return old_file_size;
3567         }
3568         old_file_size = ROUND_UP(old_file_size, s->cluster_size);
3569 
3570         nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
3571                                             s->cluster_size);
3572 
3573         /* This is an overestimation; we will not actually allocate space for
3574          * these in the file but just make sure the new refcount structures are
3575          * able to cover them so we will not have to allocate new refblocks
3576          * while entering the data blocks in the potentially new L2 tables.
3577          * (We do not actually care where the L2 tables are placed. Maybe they
3578          *  are already allocated or they can be placed somewhere before
3579          *  @old_file_size. It does not matter because they will be fully
3580          *  allocated automatically, so they do not need to be covered by the
3581          *  preallocation. All that matters is that we will not have to allocate
3582          *  new refcount structures for them.) */
3583         nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
3584                                         s->cluster_size / sizeof(uint64_t));
3585         /* The cluster range may not be aligned to L2 boundaries, so add one L2
3586          * table for a potential head/tail */
3587         nb_new_l2_tables++;
3588 
3589         allocation_start = qcow2_refcount_area(bs, old_file_size,
3590                                                nb_new_data_clusters +
3591                                                nb_new_l2_tables,
3592                                                true, 0, 0);
3593         if (allocation_start < 0) {
3594             error_setg_errno(errp, -allocation_start,
3595                              "Failed to resize refcount structures");
3596             return allocation_start;
3597         }
3598 
3599         clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
3600                                                      nb_new_data_clusters);
3601         if (clusters_allocated < 0) {
3602             error_setg_errno(errp, -clusters_allocated,
3603                              "Failed to allocate data clusters");
3604             return -clusters_allocated;
3605         }
3606 
3607         assert(clusters_allocated == nb_new_data_clusters);
3608 
3609         /* Allocate the data area */
3610         new_file_size = allocation_start +
3611                         nb_new_data_clusters * s->cluster_size;
3612         ret = bdrv_truncate(bs->file, new_file_size, prealloc, errp);
3613         if (ret < 0) {
3614             error_prepend(errp, "Failed to resize underlying file: ");
3615             qcow2_free_clusters(bs, allocation_start,
3616                                 nb_new_data_clusters * s->cluster_size,
3617                                 QCOW2_DISCARD_OTHER);
3618             return ret;
3619         }
3620 
3621         /* Create the necessary L2 entries */
3622         host_offset = allocation_start;
3623         guest_offset = old_length;
3624         while (nb_new_data_clusters) {
3625             int64_t nb_clusters = MIN(
3626                 nb_new_data_clusters,
3627                 s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
3628             QCowL2Meta allocation = {
3629                 .offset       = guest_offset,
3630                 .alloc_offset = host_offset,
3631                 .nb_clusters  = nb_clusters,
3632             };
3633             qemu_co_queue_init(&allocation.dependent_requests);
3634 
3635             ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
3636             if (ret < 0) {
3637                 error_setg_errno(errp, -ret, "Failed to update L2 tables");
3638                 qcow2_free_clusters(bs, host_offset,
3639                                     nb_new_data_clusters * s->cluster_size,
3640                                     QCOW2_DISCARD_OTHER);
3641                 return ret;
3642             }
3643 
3644             guest_offset += nb_clusters * s->cluster_size;
3645             host_offset += nb_clusters * s->cluster_size;
3646             nb_new_data_clusters -= nb_clusters;
3647         }
3648         break;
3649     }
3650 
3651     default:
3652         g_assert_not_reached();
3653     }
3654 
3655     if (prealloc != PREALLOC_MODE_OFF) {
3656         /* Flush metadata before actually changing the image size */
3657         ret = bdrv_flush(bs);
3658         if (ret < 0) {
3659             error_setg_errno(errp, -ret,
3660                              "Failed to flush the preallocated area to disk");
3661             return ret;
3662         }
3663     }
3664 
3665     /* write updated header.size */
3666     offset = cpu_to_be64(offset);
3667     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
3668                            &offset, sizeof(uint64_t));
3669     if (ret < 0) {
3670         error_setg_errno(errp, -ret, "Failed to update the image size");
3671         return ret;
3672     }
3673 
3674     s->l1_vm_state_index = new_l1_size;
3675     return 0;
3676 }
3677 
3678 /* XXX: put compressed sectors first, then all the cluster aligned
3679    tables to avoid losing bytes in alignment */
3680 static coroutine_fn int
3681 qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
3682                             uint64_t bytes, QEMUIOVector *qiov)
3683 {
3684     BDRVQcow2State *s = bs->opaque;
3685     QEMUIOVector hd_qiov;
3686     struct iovec iov;
3687     z_stream strm;
3688     int ret, out_len;
3689     uint8_t *buf, *out_buf;
3690     int64_t cluster_offset;
3691 
3692     if (bytes == 0) {
3693         /* align end of file to a sector boundary to ease reading with
3694            sector based I/Os */
3695         cluster_offset = bdrv_getlength(bs->file->bs);
3696         if (cluster_offset < 0) {
3697             return cluster_offset;
3698         }
3699         return bdrv_truncate(bs->file, cluster_offset, PREALLOC_MODE_OFF, NULL);
3700     }
3701 
3702     if (offset_into_cluster(s, offset)) {
3703         return -EINVAL;
3704     }
3705 
3706     buf = qemu_blockalign(bs, s->cluster_size);
3707     if (bytes != s->cluster_size) {
3708         if (bytes > s->cluster_size ||
3709             offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
3710         {
3711             qemu_vfree(buf);
3712             return -EINVAL;
3713         }
3714         /* Zero-pad last write if image size is not cluster aligned */
3715         memset(buf + bytes, 0, s->cluster_size - bytes);
3716     }
3717     qemu_iovec_to_buf(qiov, 0, buf, bytes);
3718 
3719     out_buf = g_malloc(s->cluster_size);
3720 
3721     /* best compression, small window, no zlib header */
3722     memset(&strm, 0, sizeof(strm));
3723     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
3724                        Z_DEFLATED, -12,
3725                        9, Z_DEFAULT_STRATEGY);
3726     if (ret != 0) {
3727         ret = -EINVAL;
3728         goto fail;
3729     }
3730 
3731     strm.avail_in = s->cluster_size;
3732     strm.next_in = (uint8_t *)buf;
3733     strm.avail_out = s->cluster_size;
3734     strm.next_out = out_buf;
3735 
3736     ret = deflate(&strm, Z_FINISH);
3737     if (ret != Z_STREAM_END && ret != Z_OK) {
3738         deflateEnd(&strm);
3739         ret = -EINVAL;
3740         goto fail;
3741     }
3742     out_len = strm.next_out - out_buf;
3743 
3744     deflateEnd(&strm);
3745 
3746     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
3747         /* could not compress: write normal cluster */
3748         ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
3749         if (ret < 0) {
3750             goto fail;
3751         }
3752         goto success;
3753     }
3754 
3755     qemu_co_mutex_lock(&s->lock);
3756     cluster_offset =
3757         qcow2_alloc_compressed_cluster_offset(bs, offset, out_len);
3758     if (!cluster_offset) {
3759         qemu_co_mutex_unlock(&s->lock);
3760         ret = -EIO;
3761         goto fail;
3762     }
3763     cluster_offset &= s->cluster_offset_mask;
3764 
3765     ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
3766     qemu_co_mutex_unlock(&s->lock);
3767     if (ret < 0) {
3768         goto fail;
3769     }
3770 
3771     iov = (struct iovec) {
3772         .iov_base   = out_buf,
3773         .iov_len    = out_len,
3774     };
3775     qemu_iovec_init_external(&hd_qiov, &iov, 1);
3776 
3777     BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
3778     ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0);
3779     if (ret < 0) {
3780         goto fail;
3781     }
3782 success:
3783     ret = 0;
3784 fail:
3785     qemu_vfree(buf);
3786     g_free(out_buf);
3787     return ret;
3788 }
3789 
3790 static int make_completely_empty(BlockDriverState *bs)
3791 {
3792     BDRVQcow2State *s = bs->opaque;
3793     Error *local_err = NULL;
3794     int ret, l1_clusters;
3795     int64_t offset;
3796     uint64_t *new_reftable = NULL;
3797     uint64_t rt_entry, l1_size2;
3798     struct {
3799         uint64_t l1_offset;
3800         uint64_t reftable_offset;
3801         uint32_t reftable_clusters;
3802     } QEMU_PACKED l1_ofs_rt_ofs_cls;
3803 
3804     ret = qcow2_cache_empty(bs, s->l2_table_cache);
3805     if (ret < 0) {
3806         goto fail;
3807     }
3808 
3809     ret = qcow2_cache_empty(bs, s->refcount_block_cache);
3810     if (ret < 0) {
3811         goto fail;
3812     }
3813 
3814     /* Refcounts will be broken utterly */
3815     ret = qcow2_mark_dirty(bs);
3816     if (ret < 0) {
3817         goto fail;
3818     }
3819 
3820     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3821 
3822     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
3823     l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
3824 
3825     /* After this call, neither the in-memory nor the on-disk refcount
3826      * information accurately describe the actual references */
3827 
3828     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
3829                              l1_clusters * s->cluster_size, 0);
3830     if (ret < 0) {
3831         goto fail_broken_refcounts;
3832     }
3833     memset(s->l1_table, 0, l1_size2);
3834 
3835     BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
3836 
3837     /* Overwrite enough clusters at the beginning of the sectors to place
3838      * the refcount table, a refcount block and the L1 table in; this may
3839      * overwrite parts of the existing refcount and L1 table, which is not
3840      * an issue because the dirty flag is set, complete data loss is in fact
3841      * desired and partial data loss is consequently fine as well */
3842     ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
3843                              (2 + l1_clusters) * s->cluster_size, 0);
3844     /* This call (even if it failed overall) may have overwritten on-disk
3845      * refcount structures; in that case, the in-memory refcount information
3846      * will probably differ from the on-disk information which makes the BDS
3847      * unusable */
3848     if (ret < 0) {
3849         goto fail_broken_refcounts;
3850     }
3851 
3852     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3853     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
3854 
3855     /* "Create" an empty reftable (one cluster) directly after the image
3856      * header and an empty L1 table three clusters after the image header;
3857      * the cluster between those two will be used as the first refblock */
3858     l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
3859     l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
3860     l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
3861     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
3862                            &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
3863     if (ret < 0) {
3864         goto fail_broken_refcounts;
3865     }
3866 
3867     s->l1_table_offset = 3 * s->cluster_size;
3868 
3869     new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
3870     if (!new_reftable) {
3871         ret = -ENOMEM;
3872         goto fail_broken_refcounts;
3873     }
3874 
3875     s->refcount_table_offset = s->cluster_size;
3876     s->refcount_table_size   = s->cluster_size / sizeof(uint64_t);
3877     s->max_refcount_table_index = 0;
3878 
3879     g_free(s->refcount_table);
3880     s->refcount_table = new_reftable;
3881     new_reftable = NULL;
3882 
3883     /* Now the in-memory refcount information again corresponds to the on-disk
3884      * information (reftable is empty and no refblocks (the refblock cache is
3885      * empty)); however, this means some clusters (e.g. the image header) are
3886      * referenced, but not refcounted, but the normal qcow2 code assumes that
3887      * the in-memory information is always correct */
3888 
3889     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
3890 
3891     /* Enter the first refblock into the reftable */
3892     rt_entry = cpu_to_be64(2 * s->cluster_size);
3893     ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
3894                            &rt_entry, sizeof(rt_entry));
3895     if (ret < 0) {
3896         goto fail_broken_refcounts;
3897     }
3898     s->refcount_table[0] = 2 * s->cluster_size;
3899 
3900     s->free_cluster_index = 0;
3901     assert(3 + l1_clusters <= s->refcount_block_size);
3902     offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
3903     if (offset < 0) {
3904         ret = offset;
3905         goto fail_broken_refcounts;
3906     } else if (offset > 0) {
3907         error_report("First cluster in emptied image is in use");
3908         abort();
3909     }
3910 
3911     /* Now finally the in-memory information corresponds to the on-disk
3912      * structures and is correct */
3913     ret = qcow2_mark_clean(bs);
3914     if (ret < 0) {
3915         goto fail;
3916     }
3917 
3918     ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
3919                         PREALLOC_MODE_OFF, &local_err);
3920     if (ret < 0) {
3921         error_report_err(local_err);
3922         goto fail;
3923     }
3924 
3925     return 0;
3926 
3927 fail_broken_refcounts:
3928     /* The BDS is unusable at this point. If we wanted to make it usable, we
3929      * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
3930      * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
3931      * again. However, because the functions which could have caused this error
3932      * path to be taken are used by those functions as well, it's very likely
3933      * that that sequence will fail as well. Therefore, just eject the BDS. */
3934     bs->drv = NULL;
3935 
3936 fail:
3937     g_free(new_reftable);
3938     return ret;
3939 }
3940 
3941 static int qcow2_make_empty(BlockDriverState *bs)
3942 {
3943     BDRVQcow2State *s = bs->opaque;
3944     uint64_t offset, end_offset;
3945     int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
3946     int l1_clusters, ret = 0;
3947 
3948     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
3949 
3950     if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
3951         3 + l1_clusters <= s->refcount_block_size &&
3952         s->crypt_method_header != QCOW_CRYPT_LUKS) {
3953         /* The following function only works for qcow2 v3 images (it
3954          * requires the dirty flag) and only as long as there are no
3955          * features that reserve extra clusters (such as snapshots,
3956          * LUKS header, or persistent bitmaps), because it completely
3957          * empties the image.  Furthermore, the L1 table and three
3958          * additional clusters (image header, refcount table, one
3959          * refcount block) have to fit inside one refcount block. */
3960         return make_completely_empty(bs);
3961     }
3962 
3963     /* This fallback code simply discards every active cluster; this is slow,
3964      * but works in all cases */
3965     end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3966     for (offset = 0; offset < end_offset; offset += step) {
3967         /* As this function is generally used after committing an external
3968          * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
3969          * default action for this kind of discard is to pass the discard,
3970          * which will ideally result in an actually smaller image file, as
3971          * is probably desired. */
3972         ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
3973                                     QCOW2_DISCARD_SNAPSHOT, true);
3974         if (ret < 0) {
3975             break;
3976         }
3977     }
3978 
3979     return ret;
3980 }
3981 
3982 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
3983 {
3984     BDRVQcow2State *s = bs->opaque;
3985     int ret;
3986 
3987     qemu_co_mutex_lock(&s->lock);
3988     ret = qcow2_write_caches(bs);
3989     qemu_co_mutex_unlock(&s->lock);
3990 
3991     return ret;
3992 }
3993 
3994 static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
3995                                        Error **errp)
3996 {
3997     Error *local_err = NULL;
3998     BlockMeasureInfo *info;
3999     uint64_t required = 0; /* bytes that contribute to required size */
4000     uint64_t virtual_size; /* disk size as seen by guest */
4001     uint64_t refcount_bits;
4002     uint64_t l2_tables;
4003     size_t cluster_size;
4004     int version;
4005     char *optstr;
4006     PreallocMode prealloc;
4007     bool has_backing_file;
4008 
4009     /* Parse image creation options */
4010     cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
4011     if (local_err) {
4012         goto err;
4013     }
4014 
4015     version = qcow2_opt_get_version_del(opts, &local_err);
4016     if (local_err) {
4017         goto err;
4018     }
4019 
4020     refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
4021     if (local_err) {
4022         goto err;
4023     }
4024 
4025     optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
4026     prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
4027                                PREALLOC_MODE_OFF, &local_err);
4028     g_free(optstr);
4029     if (local_err) {
4030         goto err;
4031     }
4032 
4033     optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
4034     has_backing_file = !!optstr;
4035     g_free(optstr);
4036 
4037     virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
4038     virtual_size = ROUND_UP(virtual_size, cluster_size);
4039 
4040     /* Check that virtual disk size is valid */
4041     l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
4042                              cluster_size / sizeof(uint64_t));
4043     if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
4044         error_setg(&local_err, "The image size is too large "
4045                                "(try using a larger cluster size)");
4046         goto err;
4047     }
4048 
4049     /* Account for input image */
4050     if (in_bs) {
4051         int64_t ssize = bdrv_getlength(in_bs);
4052         if (ssize < 0) {
4053             error_setg_errno(&local_err, -ssize,
4054                              "Unable to get image virtual_size");
4055             goto err;
4056         }
4057 
4058         virtual_size = ROUND_UP(ssize, cluster_size);
4059 
4060         if (has_backing_file) {
4061             /* We don't how much of the backing chain is shared by the input
4062              * image and the new image file.  In the worst case the new image's
4063              * backing file has nothing in common with the input image.  Be
4064              * conservative and assume all clusters need to be written.
4065              */
4066             required = virtual_size;
4067         } else {
4068             int64_t offset;
4069             int64_t pnum = 0;
4070 
4071             for (offset = 0; offset < ssize; offset += pnum) {
4072                 int ret;
4073 
4074                 ret = bdrv_block_status_above(in_bs, NULL, offset,
4075                                               ssize - offset, &pnum, NULL,
4076                                               NULL);
4077                 if (ret < 0) {
4078                     error_setg_errno(&local_err, -ret,
4079                                      "Unable to get block status");
4080                     goto err;
4081                 }
4082 
4083                 if (ret & BDRV_BLOCK_ZERO) {
4084                     /* Skip zero regions (safe with no backing file) */
4085                 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
4086                            (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
4087                     /* Extend pnum to end of cluster for next iteration */
4088                     pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
4089 
4090                     /* Count clusters we've seen */
4091                     required += offset % cluster_size + pnum;
4092                 }
4093             }
4094         }
4095     }
4096 
4097     /* Take into account preallocation.  Nothing special is needed for
4098      * PREALLOC_MODE_METADATA since metadata is always counted.
4099      */
4100     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
4101         required = virtual_size;
4102     }
4103 
4104     info = g_new(BlockMeasureInfo, 1);
4105     info->fully_allocated =
4106         qcow2_calc_prealloc_size(virtual_size, cluster_size,
4107                                  ctz32(refcount_bits));
4108 
4109     /* Remove data clusters that are not required.  This overestimates the
4110      * required size because metadata needed for the fully allocated file is
4111      * still counted.
4112      */
4113     info->required = info->fully_allocated - virtual_size + required;
4114     return info;
4115 
4116 err:
4117     error_propagate(errp, local_err);
4118     return NULL;
4119 }
4120 
4121 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
4122 {
4123     BDRVQcow2State *s = bs->opaque;
4124     bdi->unallocated_blocks_are_zero = true;
4125     bdi->cluster_size = s->cluster_size;
4126     bdi->vm_state_offset = qcow2_vm_state_offset(s);
4127     return 0;
4128 }
4129 
4130 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
4131 {
4132     BDRVQcow2State *s = bs->opaque;
4133     ImageInfoSpecific *spec_info;
4134     QCryptoBlockInfo *encrypt_info = NULL;
4135 
4136     if (s->crypto != NULL) {
4137         encrypt_info = qcrypto_block_get_info(s->crypto, &error_abort);
4138     }
4139 
4140     spec_info = g_new(ImageInfoSpecific, 1);
4141     *spec_info = (ImageInfoSpecific){
4142         .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
4143         .u.qcow2.data = g_new(ImageInfoSpecificQCow2, 1),
4144     };
4145     if (s->qcow_version == 2) {
4146         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4147             .compat             = g_strdup("0.10"),
4148             .refcount_bits      = s->refcount_bits,
4149         };
4150     } else if (s->qcow_version == 3) {
4151         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
4152             .compat             = g_strdup("1.1"),
4153             .lazy_refcounts     = s->compatible_features &
4154                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
4155             .has_lazy_refcounts = true,
4156             .corrupt            = s->incompatible_features &
4157                                   QCOW2_INCOMPAT_CORRUPT,
4158             .has_corrupt        = true,
4159             .refcount_bits      = s->refcount_bits,
4160         };
4161     } else {
4162         /* if this assertion fails, this probably means a new version was
4163          * added without having it covered here */
4164         assert(false);
4165     }
4166 
4167     if (encrypt_info) {
4168         ImageInfoSpecificQCow2Encryption *qencrypt =
4169             g_new(ImageInfoSpecificQCow2Encryption, 1);
4170         switch (encrypt_info->format) {
4171         case Q_CRYPTO_BLOCK_FORMAT_QCOW:
4172             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
4173             qencrypt->u.aes = encrypt_info->u.qcow;
4174             break;
4175         case Q_CRYPTO_BLOCK_FORMAT_LUKS:
4176             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
4177             qencrypt->u.luks = encrypt_info->u.luks;
4178             break;
4179         default:
4180             abort();
4181         }
4182         /* Since we did shallow copy above, erase any pointers
4183          * in the original info */
4184         memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
4185         qapi_free_QCryptoBlockInfo(encrypt_info);
4186 
4187         spec_info->u.qcow2.data->has_encrypt = true;
4188         spec_info->u.qcow2.data->encrypt = qencrypt;
4189     }
4190 
4191     return spec_info;
4192 }
4193 
4194 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4195                               int64_t pos)
4196 {
4197     BDRVQcow2State *s = bs->opaque;
4198 
4199     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
4200     return bs->drv->bdrv_co_pwritev(bs, qcow2_vm_state_offset(s) + pos,
4201                                     qiov->size, qiov, 0);
4202 }
4203 
4204 static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
4205                               int64_t pos)
4206 {
4207     BDRVQcow2State *s = bs->opaque;
4208 
4209     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
4210     return bs->drv->bdrv_co_preadv(bs, qcow2_vm_state_offset(s) + pos,
4211                                    qiov->size, qiov, 0);
4212 }
4213 
4214 /*
4215  * Downgrades an image's version. To achieve this, any incompatible features
4216  * have to be removed.
4217  */
4218 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
4219                            BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
4220                            Error **errp)
4221 {
4222     BDRVQcow2State *s = bs->opaque;
4223     int current_version = s->qcow_version;
4224     int ret;
4225 
4226     /* This is qcow2_downgrade(), not qcow2_upgrade() */
4227     assert(target_version < current_version);
4228 
4229     /* There are no other versions (now) that you can downgrade to */
4230     assert(target_version == 2);
4231 
4232     if (s->refcount_order != 4) {
4233         error_setg(errp, "compat=0.10 requires refcount_bits=16");
4234         return -ENOTSUP;
4235     }
4236 
4237     /* clear incompatible features */
4238     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
4239         ret = qcow2_mark_clean(bs);
4240         if (ret < 0) {
4241             error_setg_errno(errp, -ret, "Failed to make the image clean");
4242             return ret;
4243         }
4244     }
4245 
4246     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
4247      * the first place; if that happens nonetheless, returning -ENOTSUP is the
4248      * best thing to do anyway */
4249 
4250     if (s->incompatible_features) {
4251         error_setg(errp, "Cannot downgrade an image with incompatible features "
4252                    "%#" PRIx64 " set", s->incompatible_features);
4253         return -ENOTSUP;
4254     }
4255 
4256     /* since we can ignore compatible features, we can set them to 0 as well */
4257     s->compatible_features = 0;
4258     /* if lazy refcounts have been used, they have already been fixed through
4259      * clearing the dirty flag */
4260 
4261     /* clearing autoclear features is trivial */
4262     s->autoclear_features = 0;
4263 
4264     ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
4265     if (ret < 0) {
4266         error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
4267         return ret;
4268     }
4269 
4270     s->qcow_version = target_version;
4271     ret = qcow2_update_header(bs);
4272     if (ret < 0) {
4273         s->qcow_version = current_version;
4274         error_setg_errno(errp, -ret, "Failed to update the image header");
4275         return ret;
4276     }
4277     return 0;
4278 }
4279 
4280 typedef enum Qcow2AmendOperation {
4281     /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
4282      * statically initialized to so that the helper CB can discern the first
4283      * invocation from an operation change */
4284     QCOW2_NO_OPERATION = 0,
4285 
4286     QCOW2_CHANGING_REFCOUNT_ORDER,
4287     QCOW2_DOWNGRADING,
4288 } Qcow2AmendOperation;
4289 
4290 typedef struct Qcow2AmendHelperCBInfo {
4291     /* The code coordinating the amend operations should only modify
4292      * these four fields; the rest will be managed by the CB */
4293     BlockDriverAmendStatusCB *original_status_cb;
4294     void *original_cb_opaque;
4295 
4296     Qcow2AmendOperation current_operation;
4297 
4298     /* Total number of operations to perform (only set once) */
4299     int total_operations;
4300 
4301     /* The following fields are managed by the CB */
4302 
4303     /* Number of operations completed */
4304     int operations_completed;
4305 
4306     /* Cumulative offset of all completed operations */
4307     int64_t offset_completed;
4308 
4309     Qcow2AmendOperation last_operation;
4310     int64_t last_work_size;
4311 } Qcow2AmendHelperCBInfo;
4312 
4313 static void qcow2_amend_helper_cb(BlockDriverState *bs,
4314                                   int64_t operation_offset,
4315                                   int64_t operation_work_size, void *opaque)
4316 {
4317     Qcow2AmendHelperCBInfo *info = opaque;
4318     int64_t current_work_size;
4319     int64_t projected_work_size;
4320 
4321     if (info->current_operation != info->last_operation) {
4322         if (info->last_operation != QCOW2_NO_OPERATION) {
4323             info->offset_completed += info->last_work_size;
4324             info->operations_completed++;
4325         }
4326 
4327         info->last_operation = info->current_operation;
4328     }
4329 
4330     assert(info->total_operations > 0);
4331     assert(info->operations_completed < info->total_operations);
4332 
4333     info->last_work_size = operation_work_size;
4334 
4335     current_work_size = info->offset_completed + operation_work_size;
4336 
4337     /* current_work_size is the total work size for (operations_completed + 1)
4338      * operations (which includes this one), so multiply it by the number of
4339      * operations not covered and divide it by the number of operations
4340      * covered to get a projection for the operations not covered */
4341     projected_work_size = current_work_size * (info->total_operations -
4342                                                info->operations_completed - 1)
4343                                             / (info->operations_completed + 1);
4344 
4345     info->original_status_cb(bs, info->offset_completed + operation_offset,
4346                              current_work_size + projected_work_size,
4347                              info->original_cb_opaque);
4348 }
4349 
4350 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
4351                                BlockDriverAmendStatusCB *status_cb,
4352                                void *cb_opaque,
4353                                Error **errp)
4354 {
4355     BDRVQcow2State *s = bs->opaque;
4356     int old_version = s->qcow_version, new_version = old_version;
4357     uint64_t new_size = 0;
4358     const char *backing_file = NULL, *backing_format = NULL;
4359     bool lazy_refcounts = s->use_lazy_refcounts;
4360     const char *compat = NULL;
4361     uint64_t cluster_size = s->cluster_size;
4362     bool encrypt;
4363     int encformat;
4364     int refcount_bits = s->refcount_bits;
4365     int ret;
4366     QemuOptDesc *desc = opts->list->desc;
4367     Qcow2AmendHelperCBInfo helper_cb_info;
4368 
4369     while (desc && desc->name) {
4370         if (!qemu_opt_find(opts, desc->name)) {
4371             /* only change explicitly defined options */
4372             desc++;
4373             continue;
4374         }
4375 
4376         if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
4377             compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
4378             if (!compat) {
4379                 /* preserve default */
4380             } else if (!strcmp(compat, "0.10")) {
4381                 new_version = 2;
4382             } else if (!strcmp(compat, "1.1")) {
4383                 new_version = 3;
4384             } else {
4385                 error_setg(errp, "Unknown compatibility level %s", compat);
4386                 return -EINVAL;
4387             }
4388         } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
4389             error_setg(errp, "Cannot change preallocation mode");
4390             return -ENOTSUP;
4391         } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
4392             new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
4393         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
4394             backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
4395         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
4396             backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
4397         } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
4398             encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
4399                                         !!s->crypto);
4400 
4401             if (encrypt != !!s->crypto) {
4402                 error_setg(errp,
4403                            "Changing the encryption flag is not supported");
4404                 return -ENOTSUP;
4405             }
4406         } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) {
4407             encformat = qcow2_crypt_method_from_format(
4408                 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT));
4409 
4410             if (encformat != s->crypt_method_header) {
4411                 error_setg(errp,
4412                            "Changing the encryption format is not supported");
4413                 return -ENOTSUP;
4414             }
4415         } else if (g_str_has_prefix(desc->name, "encrypt.")) {
4416             error_setg(errp,
4417                        "Changing the encryption parameters is not supported");
4418             return -ENOTSUP;
4419         } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
4420             cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
4421                                              cluster_size);
4422             if (cluster_size != s->cluster_size) {
4423                 error_setg(errp, "Changing the cluster size is not supported");
4424                 return -ENOTSUP;
4425             }
4426         } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
4427             lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
4428                                                lazy_refcounts);
4429         } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
4430             refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
4431                                                 refcount_bits);
4432 
4433             if (refcount_bits <= 0 || refcount_bits > 64 ||
4434                 !is_power_of_2(refcount_bits))
4435             {
4436                 error_setg(errp, "Refcount width must be a power of two and "
4437                            "may not exceed 64 bits");
4438                 return -EINVAL;
4439             }
4440         } else {
4441             /* if this point is reached, this probably means a new option was
4442              * added without having it covered here */
4443             abort();
4444         }
4445 
4446         desc++;
4447     }
4448 
4449     helper_cb_info = (Qcow2AmendHelperCBInfo){
4450         .original_status_cb = status_cb,
4451         .original_cb_opaque = cb_opaque,
4452         .total_operations = (new_version < old_version)
4453                           + (s->refcount_bits != refcount_bits)
4454     };
4455 
4456     /* Upgrade first (some features may require compat=1.1) */
4457     if (new_version > old_version) {
4458         s->qcow_version = new_version;
4459         ret = qcow2_update_header(bs);
4460         if (ret < 0) {
4461             s->qcow_version = old_version;
4462             error_setg_errno(errp, -ret, "Failed to update the image header");
4463             return ret;
4464         }
4465     }
4466 
4467     if (s->refcount_bits != refcount_bits) {
4468         int refcount_order = ctz32(refcount_bits);
4469 
4470         if (new_version < 3 && refcount_bits != 16) {
4471             error_setg(errp, "Refcount widths other than 16 bits require "
4472                        "compatibility level 1.1 or above (use compat=1.1 or "
4473                        "greater)");
4474             return -EINVAL;
4475         }
4476 
4477         helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
4478         ret = qcow2_change_refcount_order(bs, refcount_order,
4479                                           &qcow2_amend_helper_cb,
4480                                           &helper_cb_info, errp);
4481         if (ret < 0) {
4482             return ret;
4483         }
4484     }
4485 
4486     if (backing_file || backing_format) {
4487         ret = qcow2_change_backing_file(bs,
4488                     backing_file ?: s->image_backing_file,
4489                     backing_format ?: s->image_backing_format);
4490         if (ret < 0) {
4491             error_setg_errno(errp, -ret, "Failed to change the backing file");
4492             return ret;
4493         }
4494     }
4495 
4496     if (s->use_lazy_refcounts != lazy_refcounts) {
4497         if (lazy_refcounts) {
4498             if (new_version < 3) {
4499                 error_setg(errp, "Lazy refcounts only supported with "
4500                            "compatibility level 1.1 and above (use compat=1.1 "
4501                            "or greater)");
4502                 return -EINVAL;
4503             }
4504             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4505             ret = qcow2_update_header(bs);
4506             if (ret < 0) {
4507                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4508                 error_setg_errno(errp, -ret, "Failed to update the image header");
4509                 return ret;
4510             }
4511             s->use_lazy_refcounts = true;
4512         } else {
4513             /* make image clean first */
4514             ret = qcow2_mark_clean(bs);
4515             if (ret < 0) {
4516                 error_setg_errno(errp, -ret, "Failed to make the image clean");
4517                 return ret;
4518             }
4519             /* now disallow lazy refcounts */
4520             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4521             ret = qcow2_update_header(bs);
4522             if (ret < 0) {
4523                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4524                 error_setg_errno(errp, -ret, "Failed to update the image header");
4525                 return ret;
4526             }
4527             s->use_lazy_refcounts = false;
4528         }
4529     }
4530 
4531     if (new_size) {
4532         BlockBackend *blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
4533         ret = blk_insert_bs(blk, bs, errp);
4534         if (ret < 0) {
4535             blk_unref(blk);
4536             return ret;
4537         }
4538 
4539         ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, errp);
4540         blk_unref(blk);
4541         if (ret < 0) {
4542             return ret;
4543         }
4544     }
4545 
4546     /* Downgrade last (so unsupported features can be removed before) */
4547     if (new_version < old_version) {
4548         helper_cb_info.current_operation = QCOW2_DOWNGRADING;
4549         ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
4550                               &helper_cb_info, errp);
4551         if (ret < 0) {
4552             return ret;
4553         }
4554     }
4555 
4556     return 0;
4557 }
4558 
4559 /*
4560  * If offset or size are negative, respectively, they will not be included in
4561  * the BLOCK_IMAGE_CORRUPTED event emitted.
4562  * fatal will be ignored for read-only BDS; corruptions found there will always
4563  * be considered non-fatal.
4564  */
4565 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
4566                              int64_t size, const char *message_format, ...)
4567 {
4568     BDRVQcow2State *s = bs->opaque;
4569     const char *node_name;
4570     char *message;
4571     va_list ap;
4572 
4573     fatal = fatal && bdrv_is_writable(bs);
4574 
4575     if (s->signaled_corruption &&
4576         (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
4577     {
4578         return;
4579     }
4580 
4581     va_start(ap, message_format);
4582     message = g_strdup_vprintf(message_format, ap);
4583     va_end(ap);
4584 
4585     if (fatal) {
4586         fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
4587                 "corruption events will be suppressed\n", message);
4588     } else {
4589         fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
4590                 "corruption events will be suppressed\n", message);
4591     }
4592 
4593     node_name = bdrv_get_node_name(bs);
4594     qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
4595                                           *node_name != '\0', node_name,
4596                                           message, offset >= 0, offset,
4597                                           size >= 0, size,
4598                                           fatal, &error_abort);
4599     g_free(message);
4600 
4601     if (fatal) {
4602         qcow2_mark_corrupt(bs);
4603         bs->drv = NULL; /* make BDS unusable */
4604     }
4605 
4606     s->signaled_corruption = true;
4607 }
4608 
4609 static QemuOptsList qcow2_create_opts = {
4610     .name = "qcow2-create-opts",
4611     .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
4612     .desc = {
4613         {
4614             .name = BLOCK_OPT_SIZE,
4615             .type = QEMU_OPT_SIZE,
4616             .help = "Virtual disk size"
4617         },
4618         {
4619             .name = BLOCK_OPT_COMPAT_LEVEL,
4620             .type = QEMU_OPT_STRING,
4621             .help = "Compatibility level (0.10 or 1.1)"
4622         },
4623         {
4624             .name = BLOCK_OPT_BACKING_FILE,
4625             .type = QEMU_OPT_STRING,
4626             .help = "File name of a base image"
4627         },
4628         {
4629             .name = BLOCK_OPT_BACKING_FMT,
4630             .type = QEMU_OPT_STRING,
4631             .help = "Image format of the base image"
4632         },
4633         {
4634             .name = BLOCK_OPT_ENCRYPT,
4635             .type = QEMU_OPT_BOOL,
4636             .help = "Encrypt the image with format 'aes'. (Deprecated "
4637                     "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
4638         },
4639         {
4640             .name = BLOCK_OPT_ENCRYPT_FORMAT,
4641             .type = QEMU_OPT_STRING,
4642             .help = "Encrypt the image, format choices: 'aes', 'luks'",
4643         },
4644         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
4645             "ID of secret providing qcow AES key or LUKS passphrase"),
4646         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
4647         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
4648         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
4649         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
4650         BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
4651         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
4652         {
4653             .name = BLOCK_OPT_CLUSTER_SIZE,
4654             .type = QEMU_OPT_SIZE,
4655             .help = "qcow2 cluster size",
4656             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
4657         },
4658         {
4659             .name = BLOCK_OPT_PREALLOC,
4660             .type = QEMU_OPT_STRING,
4661             .help = "Preallocation mode (allowed values: off, metadata, "
4662                     "falloc, full)"
4663         },
4664         {
4665             .name = BLOCK_OPT_LAZY_REFCOUNTS,
4666             .type = QEMU_OPT_BOOL,
4667             .help = "Postpone refcount updates",
4668             .def_value_str = "off"
4669         },
4670         {
4671             .name = BLOCK_OPT_REFCOUNT_BITS,
4672             .type = QEMU_OPT_NUMBER,
4673             .help = "Width of a reference count entry in bits",
4674             .def_value_str = "16"
4675         },
4676         { /* end of list */ }
4677     }
4678 };
4679 
4680 BlockDriver bdrv_qcow2 = {
4681     .format_name        = "qcow2",
4682     .instance_size      = sizeof(BDRVQcow2State),
4683     .bdrv_probe         = qcow2_probe,
4684     .bdrv_open          = qcow2_open,
4685     .bdrv_close         = qcow2_close,
4686     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
4687     .bdrv_reopen_commit   = qcow2_reopen_commit,
4688     .bdrv_reopen_abort    = qcow2_reopen_abort,
4689     .bdrv_join_options    = qcow2_join_options,
4690     .bdrv_child_perm      = bdrv_format_default_perms,
4691     .bdrv_co_create_opts  = qcow2_co_create_opts,
4692     .bdrv_co_create       = qcow2_co_create,
4693     .bdrv_has_zero_init = bdrv_has_zero_init_1,
4694     .bdrv_co_block_status = qcow2_co_block_status,
4695 
4696     .bdrv_co_preadv         = qcow2_co_preadv,
4697     .bdrv_co_pwritev        = qcow2_co_pwritev,
4698     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
4699 
4700     .bdrv_co_pwrite_zeroes  = qcow2_co_pwrite_zeroes,
4701     .bdrv_co_pdiscard       = qcow2_co_pdiscard,
4702     .bdrv_co_copy_range_from = qcow2_co_copy_range_from,
4703     .bdrv_co_copy_range_to  = qcow2_co_copy_range_to,
4704     .bdrv_truncate          = qcow2_truncate,
4705     .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
4706     .bdrv_make_empty        = qcow2_make_empty,
4707 
4708     .bdrv_snapshot_create   = qcow2_snapshot_create,
4709     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
4710     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
4711     .bdrv_snapshot_list     = qcow2_snapshot_list,
4712     .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
4713     .bdrv_measure           = qcow2_measure,
4714     .bdrv_get_info          = qcow2_get_info,
4715     .bdrv_get_specific_info = qcow2_get_specific_info,
4716 
4717     .bdrv_save_vmstate    = qcow2_save_vmstate,
4718     .bdrv_load_vmstate    = qcow2_load_vmstate,
4719 
4720     .supports_backing           = true,
4721     .bdrv_change_backing_file   = qcow2_change_backing_file,
4722 
4723     .bdrv_refresh_limits        = qcow2_refresh_limits,
4724     .bdrv_co_invalidate_cache   = qcow2_co_invalidate_cache,
4725     .bdrv_inactivate            = qcow2_inactivate,
4726 
4727     .create_opts         = &qcow2_create_opts,
4728     .bdrv_co_check       = qcow2_co_check,
4729     .bdrv_amend_options  = qcow2_amend_options,
4730 
4731     .bdrv_detach_aio_context  = qcow2_detach_aio_context,
4732     .bdrv_attach_aio_context  = qcow2_attach_aio_context,
4733 
4734     .bdrv_reopen_bitmaps_rw = qcow2_reopen_bitmaps_rw,
4735     .bdrv_can_store_new_dirty_bitmap = qcow2_can_store_new_dirty_bitmap,
4736     .bdrv_remove_persistent_dirty_bitmap = qcow2_remove_persistent_dirty_bitmap,
4737 };
4738 
4739 static void bdrv_qcow2_init(void)
4740 {
4741     bdrv_register(&bdrv_qcow2);
4742 }
4743 
4744 block_init(bdrv_qcow2_init);
4745