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