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