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