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