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