xref: /openbmc/qemu/block/qcow2.c (revision 87c9b5e0)
1 /*
2  * Block driver for the QCOW version 2 format
3  *
4  * Copyright (c) 2004-2006 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "qemu-common.h"
26 #include "block/block_int.h"
27 #include "qemu/module.h"
28 #include <zlib.h>
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/util.h"
34 #include "qapi/qmp/types.h"
35 #include "qapi-event.h"
36 #include "trace.h"
37 #include "qemu/option_int.h"
38 
39 /*
40   Differences with QCOW:
41 
42   - Support for multiple incremental snapshots.
43   - Memory management by reference counts.
44   - Clusters which have a reference count of one have the bit
45     QCOW_OFLAG_COPIED to optimize write performance.
46   - Size of compressed clusters is stored in sectors to reduce bit usage
47     in the cluster offsets.
48   - Support for storing additional data (such as the VM state) in the
49     snapshots.
50   - If a backing store is used, the cluster size is not constrained
51     (could be backported to QCOW).
52   - L2 tables have always a size of one cluster.
53 */
54 
55 
56 typedef struct {
57     uint32_t magic;
58     uint32_t len;
59 } QEMU_PACKED QCowExtension;
60 
61 #define  QCOW2_EXT_MAGIC_END 0
62 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
63 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
64 
65 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
66 {
67     const QCowHeader *cow_header = (const void *)buf;
68 
69     if (buf_size >= sizeof(QCowHeader) &&
70         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
71         be32_to_cpu(cow_header->version) >= 2)
72         return 100;
73     else
74         return 0;
75 }
76 
77 
78 /*
79  * read qcow2 extension and fill bs
80  * start reading from start_offset
81  * finish reading upon magic of value 0 or when end_offset reached
82  * unknown magic is skipped (future extension this version knows nothing about)
83  * return 0 upon success, non-0 otherwise
84  */
85 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
86                                  uint64_t end_offset, void **p_feature_table,
87                                  Error **errp)
88 {
89     BDRVQcow2State *s = bs->opaque;
90     QCowExtension ext;
91     uint64_t offset;
92     int ret;
93 
94 #ifdef DEBUG_EXT
95     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
96 #endif
97     offset = start_offset;
98     while (offset < end_offset) {
99 
100 #ifdef DEBUG_EXT
101         /* Sanity check */
102         if (offset > s->cluster_size)
103             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
104 
105         printf("attempting to read extended header in offset %lu\n", offset);
106 #endif
107 
108         ret = bdrv_pread(bs->file->bs, offset, &ext, sizeof(ext));
109         if (ret < 0) {
110             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
111                              "pread fail from offset %" PRIu64, offset);
112             return 1;
113         }
114         be32_to_cpus(&ext.magic);
115         be32_to_cpus(&ext.len);
116         offset += sizeof(ext);
117 #ifdef DEBUG_EXT
118         printf("ext.magic = 0x%x\n", ext.magic);
119 #endif
120         if (offset > end_offset || ext.len > end_offset - offset) {
121             error_setg(errp, "Header extension too large");
122             return -EINVAL;
123         }
124 
125         switch (ext.magic) {
126         case QCOW2_EXT_MAGIC_END:
127             return 0;
128 
129         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
130             if (ext.len >= sizeof(bs->backing_format)) {
131                 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
132                            " too large (>=%zu)", ext.len,
133                            sizeof(bs->backing_format));
134                 return 2;
135             }
136             ret = bdrv_pread(bs->file->bs, offset, bs->backing_format, ext.len);
137             if (ret < 0) {
138                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
139                                  "Could not read format name");
140                 return 3;
141             }
142             bs->backing_format[ext.len] = '\0';
143             s->image_backing_format = g_strdup(bs->backing_format);
144 #ifdef DEBUG_EXT
145             printf("Qcow2: Got format extension %s\n", bs->backing_format);
146 #endif
147             break;
148 
149         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
150             if (p_feature_table != NULL) {
151                 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
152                 ret = bdrv_pread(bs->file->bs, offset , feature_table, ext.len);
153                 if (ret < 0) {
154                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
155                                      "Could not read table");
156                     return ret;
157                 }
158 
159                 *p_feature_table = feature_table;
160             }
161             break;
162 
163         default:
164             /* unknown magic - save it in case we need to rewrite the header */
165             {
166                 Qcow2UnknownHeaderExtension *uext;
167 
168                 uext = g_malloc0(sizeof(*uext)  + ext.len);
169                 uext->magic = ext.magic;
170                 uext->len = ext.len;
171                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
172 
173                 ret = bdrv_pread(bs->file->bs, offset , uext->data, uext->len);
174                 if (ret < 0) {
175                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
176                                      "Could not read data");
177                     return ret;
178                 }
179             }
180             break;
181         }
182 
183         offset += ((ext.len + 7) & ~7);
184     }
185 
186     return 0;
187 }
188 
189 static void cleanup_unknown_header_ext(BlockDriverState *bs)
190 {
191     BDRVQcow2State *s = bs->opaque;
192     Qcow2UnknownHeaderExtension *uext, *next;
193 
194     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
195         QLIST_REMOVE(uext, next);
196         g_free(uext);
197     }
198 }
199 
200 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
201     Error **errp, const char *fmt, ...)
202 {
203     char msg[64];
204     va_list ap;
205 
206     va_start(ap, fmt);
207     vsnprintf(msg, sizeof(msg), fmt, ap);
208     va_end(ap);
209 
210     error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
211                bdrv_get_device_or_node_name(bs), "qcow2", msg);
212 }
213 
214 static void report_unsupported_feature(BlockDriverState *bs,
215     Error **errp, Qcow2Feature *table, uint64_t mask)
216 {
217     char *features = g_strdup("");
218     char *old;
219 
220     while (table && table->name[0] != '\0') {
221         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
222             if (mask & (1ULL << table->bit)) {
223                 old = features;
224                 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
225                                            table->name);
226                 g_free(old);
227                 mask &= ~(1ULL << table->bit);
228             }
229         }
230         table++;
231     }
232 
233     if (mask) {
234         old = features;
235         features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
236                                    old, *old ? ", " : "", mask);
237         g_free(old);
238     }
239 
240     report_unsupported(bs, errp, "%s", features);
241     g_free(features);
242 }
243 
244 /*
245  * Sets the dirty bit and flushes afterwards if necessary.
246  *
247  * The incompatible_features bit is only set if the image file header was
248  * updated successfully.  Therefore it is not required to check the return
249  * value of this function.
250  */
251 int qcow2_mark_dirty(BlockDriverState *bs)
252 {
253     BDRVQcow2State *s = bs->opaque;
254     uint64_t val;
255     int ret;
256 
257     assert(s->qcow_version >= 3);
258 
259     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
260         return 0; /* already dirty */
261     }
262 
263     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
264     ret = bdrv_pwrite(bs->file->bs, offsetof(QCowHeader, incompatible_features),
265                       &val, sizeof(val));
266     if (ret < 0) {
267         return ret;
268     }
269     ret = bdrv_flush(bs->file->bs);
270     if (ret < 0) {
271         return ret;
272     }
273 
274     /* Only treat image as dirty if the header was updated successfully */
275     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
276     return 0;
277 }
278 
279 /*
280  * Clears the dirty bit and flushes before if necessary.  Only call this
281  * function when there are no pending requests, it does not guard against
282  * concurrent requests dirtying the image.
283  */
284 static int qcow2_mark_clean(BlockDriverState *bs)
285 {
286     BDRVQcow2State *s = bs->opaque;
287 
288     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
289         int ret;
290 
291         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
292 
293         ret = bdrv_flush(bs);
294         if (ret < 0) {
295             return ret;
296         }
297 
298         return qcow2_update_header(bs);
299     }
300     return 0;
301 }
302 
303 /*
304  * Marks the image as corrupt.
305  */
306 int qcow2_mark_corrupt(BlockDriverState *bs)
307 {
308     BDRVQcow2State *s = bs->opaque;
309 
310     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
311     return qcow2_update_header(bs);
312 }
313 
314 /*
315  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
316  * before if necessary.
317  */
318 int qcow2_mark_consistent(BlockDriverState *bs)
319 {
320     BDRVQcow2State *s = bs->opaque;
321 
322     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
323         int ret = bdrv_flush(bs);
324         if (ret < 0) {
325             return ret;
326         }
327 
328         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
329         return qcow2_update_header(bs);
330     }
331     return 0;
332 }
333 
334 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
335                        BdrvCheckMode fix)
336 {
337     int ret = qcow2_check_refcounts(bs, result, fix);
338     if (ret < 0) {
339         return ret;
340     }
341 
342     if (fix && result->check_errors == 0 && result->corruptions == 0) {
343         ret = qcow2_mark_clean(bs);
344         if (ret < 0) {
345             return ret;
346         }
347         return qcow2_mark_consistent(bs);
348     }
349     return ret;
350 }
351 
352 static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
353                                  uint64_t entries, size_t entry_len)
354 {
355     BDRVQcow2State *s = bs->opaque;
356     uint64_t size;
357 
358     /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
359      * because values will be passed to qemu functions taking int64_t. */
360     if (entries > INT64_MAX / entry_len) {
361         return -EINVAL;
362     }
363 
364     size = entries * entry_len;
365 
366     if (INT64_MAX - size < offset) {
367         return -EINVAL;
368     }
369 
370     /* Tables must be cluster aligned */
371     if (offset & (s->cluster_size - 1)) {
372         return -EINVAL;
373     }
374 
375     return 0;
376 }
377 
378 static QemuOptsList qcow2_runtime_opts = {
379     .name = "qcow2",
380     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
381     .desc = {
382         {
383             .name = QCOW2_OPT_LAZY_REFCOUNTS,
384             .type = QEMU_OPT_BOOL,
385             .help = "Postpone refcount updates",
386         },
387         {
388             .name = QCOW2_OPT_DISCARD_REQUEST,
389             .type = QEMU_OPT_BOOL,
390             .help = "Pass guest discard requests to the layer below",
391         },
392         {
393             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
394             .type = QEMU_OPT_BOOL,
395             .help = "Generate discard requests when snapshot related space "
396                     "is freed",
397         },
398         {
399             .name = QCOW2_OPT_DISCARD_OTHER,
400             .type = QEMU_OPT_BOOL,
401             .help = "Generate discard requests when other clusters are freed",
402         },
403         {
404             .name = QCOW2_OPT_OVERLAP,
405             .type = QEMU_OPT_STRING,
406             .help = "Selects which overlap checks to perform from a range of "
407                     "templates (none, constant, cached, all)",
408         },
409         {
410             .name = QCOW2_OPT_OVERLAP_TEMPLATE,
411             .type = QEMU_OPT_STRING,
412             .help = "Selects which overlap checks to perform from a range of "
413                     "templates (none, constant, cached, all)",
414         },
415         {
416             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
417             .type = QEMU_OPT_BOOL,
418             .help = "Check for unintended writes into the main qcow2 header",
419         },
420         {
421             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
422             .type = QEMU_OPT_BOOL,
423             .help = "Check for unintended writes into the active L1 table",
424         },
425         {
426             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
427             .type = QEMU_OPT_BOOL,
428             .help = "Check for unintended writes into an active L2 table",
429         },
430         {
431             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
432             .type = QEMU_OPT_BOOL,
433             .help = "Check for unintended writes into the refcount table",
434         },
435         {
436             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
437             .type = QEMU_OPT_BOOL,
438             .help = "Check for unintended writes into a refcount block",
439         },
440         {
441             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
442             .type = QEMU_OPT_BOOL,
443             .help = "Check for unintended writes into the snapshot table",
444         },
445         {
446             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
447             .type = QEMU_OPT_BOOL,
448             .help = "Check for unintended writes into an inactive L1 table",
449         },
450         {
451             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
452             .type = QEMU_OPT_BOOL,
453             .help = "Check for unintended writes into an inactive L2 table",
454         },
455         {
456             .name = QCOW2_OPT_CACHE_SIZE,
457             .type = QEMU_OPT_SIZE,
458             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
459                     "cache size",
460         },
461         {
462             .name = QCOW2_OPT_L2_CACHE_SIZE,
463             .type = QEMU_OPT_SIZE,
464             .help = "Maximum L2 table cache size",
465         },
466         {
467             .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
468             .type = QEMU_OPT_SIZE,
469             .help = "Maximum refcount block cache size",
470         },
471         {
472             .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
473             .type = QEMU_OPT_NUMBER,
474             .help = "Clean unused cache entries after this time (in seconds)",
475         },
476         { /* end of list */ }
477     },
478 };
479 
480 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
481     [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
482     [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
483     [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
484     [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
485     [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
486     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
487     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
488     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
489 };
490 
491 static void cache_clean_timer_cb(void *opaque)
492 {
493     BlockDriverState *bs = opaque;
494     BDRVQcow2State *s = bs->opaque;
495     qcow2_cache_clean_unused(bs, s->l2_table_cache);
496     qcow2_cache_clean_unused(bs, s->refcount_block_cache);
497     timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
498               (int64_t) s->cache_clean_interval * 1000);
499 }
500 
501 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
502 {
503     BDRVQcow2State *s = bs->opaque;
504     if (s->cache_clean_interval > 0) {
505         s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
506                                              SCALE_MS, cache_clean_timer_cb,
507                                              bs);
508         timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
509                   (int64_t) s->cache_clean_interval * 1000);
510     }
511 }
512 
513 static void cache_clean_timer_del(BlockDriverState *bs)
514 {
515     BDRVQcow2State *s = bs->opaque;
516     if (s->cache_clean_timer) {
517         timer_del(s->cache_clean_timer);
518         timer_free(s->cache_clean_timer);
519         s->cache_clean_timer = NULL;
520     }
521 }
522 
523 static void qcow2_detach_aio_context(BlockDriverState *bs)
524 {
525     cache_clean_timer_del(bs);
526 }
527 
528 static void qcow2_attach_aio_context(BlockDriverState *bs,
529                                      AioContext *new_context)
530 {
531     cache_clean_timer_init(bs, new_context);
532 }
533 
534 static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
535                              uint64_t *l2_cache_size,
536                              uint64_t *refcount_cache_size, Error **errp)
537 {
538     BDRVQcow2State *s = bs->opaque;
539     uint64_t combined_cache_size;
540     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
541 
542     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
543     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
544     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
545 
546     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
547     *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
548     *refcount_cache_size = qemu_opt_get_size(opts,
549                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
550 
551     if (combined_cache_size_set) {
552         if (l2_cache_size_set && refcount_cache_size_set) {
553             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
554                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
555                        "the same time");
556             return;
557         } else if (*l2_cache_size > combined_cache_size) {
558             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
559                        QCOW2_OPT_CACHE_SIZE);
560             return;
561         } else if (*refcount_cache_size > combined_cache_size) {
562             error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
563                        QCOW2_OPT_CACHE_SIZE);
564             return;
565         }
566 
567         if (l2_cache_size_set) {
568             *refcount_cache_size = combined_cache_size - *l2_cache_size;
569         } else if (refcount_cache_size_set) {
570             *l2_cache_size = combined_cache_size - *refcount_cache_size;
571         } else {
572             *refcount_cache_size = combined_cache_size
573                                  / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1);
574             *l2_cache_size = combined_cache_size - *refcount_cache_size;
575         }
576     } else {
577         if (!l2_cache_size_set && !refcount_cache_size_set) {
578             *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
579                                  (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
580                                  * s->cluster_size);
581             *refcount_cache_size = *l2_cache_size
582                                  / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
583         } else if (!l2_cache_size_set) {
584             *l2_cache_size = *refcount_cache_size
585                            * DEFAULT_L2_REFCOUNT_SIZE_RATIO;
586         } else if (!refcount_cache_size_set) {
587             *refcount_cache_size = *l2_cache_size
588                                  / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
589         }
590     }
591 }
592 
593 typedef struct Qcow2ReopenState {
594     Qcow2Cache *l2_table_cache;
595     Qcow2Cache *refcount_block_cache;
596     bool use_lazy_refcounts;
597     int overlap_check;
598     bool discard_passthrough[QCOW2_DISCARD_MAX];
599     uint64_t cache_clean_interval;
600 } Qcow2ReopenState;
601 
602 static int qcow2_update_options_prepare(BlockDriverState *bs,
603                                         Qcow2ReopenState *r,
604                                         QDict *options, int flags,
605                                         Error **errp)
606 {
607     BDRVQcow2State *s = bs->opaque;
608     QemuOpts *opts = NULL;
609     const char *opt_overlap_check, *opt_overlap_check_template;
610     int overlap_check_template = 0;
611     uint64_t l2_cache_size, refcount_cache_size;
612     int i;
613     Error *local_err = NULL;
614     int ret;
615 
616     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
617     qemu_opts_absorb_qdict(opts, options, &local_err);
618     if (local_err) {
619         error_propagate(errp, local_err);
620         ret = -EINVAL;
621         goto fail;
622     }
623 
624     /* get L2 table/refcount block cache size from command line options */
625     read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size,
626                      &local_err);
627     if (local_err) {
628         error_propagate(errp, local_err);
629         ret = -EINVAL;
630         goto fail;
631     }
632 
633     l2_cache_size /= s->cluster_size;
634     if (l2_cache_size < MIN_L2_CACHE_SIZE) {
635         l2_cache_size = MIN_L2_CACHE_SIZE;
636     }
637     if (l2_cache_size > INT_MAX) {
638         error_setg(errp, "L2 cache size too big");
639         ret = -EINVAL;
640         goto fail;
641     }
642 
643     refcount_cache_size /= s->cluster_size;
644     if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
645         refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
646     }
647     if (refcount_cache_size > INT_MAX) {
648         error_setg(errp, "Refcount cache size too big");
649         ret = -EINVAL;
650         goto fail;
651     }
652 
653     /* alloc new L2 table/refcount block cache, flush old one */
654     if (s->l2_table_cache) {
655         ret = qcow2_cache_flush(bs, s->l2_table_cache);
656         if (ret) {
657             error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
658             goto fail;
659         }
660     }
661 
662     if (s->refcount_block_cache) {
663         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
664         if (ret) {
665             error_setg_errno(errp, -ret,
666                              "Failed to flush the refcount block cache");
667             goto fail;
668         }
669     }
670 
671     r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size);
672     r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size);
673     if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
674         error_setg(errp, "Could not allocate metadata caches");
675         ret = -ENOMEM;
676         goto fail;
677     }
678 
679     /* New interval for cache cleanup timer */
680     r->cache_clean_interval =
681         qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
682                             s->cache_clean_interval);
683     if (r->cache_clean_interval > UINT_MAX) {
684         error_setg(errp, "Cache clean interval too big");
685         ret = -EINVAL;
686         goto fail;
687     }
688 
689     /* lazy-refcounts; flush if going from enabled to disabled */
690     r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
691         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
692     if (r->use_lazy_refcounts && s->qcow_version < 3) {
693         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
694                    "qemu 1.1 compatibility level");
695         ret = -EINVAL;
696         goto fail;
697     }
698 
699     if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
700         ret = qcow2_mark_clean(bs);
701         if (ret < 0) {
702             error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
703             goto fail;
704         }
705     }
706 
707     /* Overlap check options */
708     opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
709     opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
710     if (opt_overlap_check_template && opt_overlap_check &&
711         strcmp(opt_overlap_check_template, opt_overlap_check))
712     {
713         error_setg(errp, "Conflicting values for qcow2 options '"
714                    QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
715                    "' ('%s')", opt_overlap_check, opt_overlap_check_template);
716         ret = -EINVAL;
717         goto fail;
718     }
719     if (!opt_overlap_check) {
720         opt_overlap_check = opt_overlap_check_template ?: "cached";
721     }
722 
723     if (!strcmp(opt_overlap_check, "none")) {
724         overlap_check_template = 0;
725     } else if (!strcmp(opt_overlap_check, "constant")) {
726         overlap_check_template = QCOW2_OL_CONSTANT;
727     } else if (!strcmp(opt_overlap_check, "cached")) {
728         overlap_check_template = QCOW2_OL_CACHED;
729     } else if (!strcmp(opt_overlap_check, "all")) {
730         overlap_check_template = QCOW2_OL_ALL;
731     } else {
732         error_setg(errp, "Unsupported value '%s' for qcow2 option "
733                    "'overlap-check'. Allowed are any of the following: "
734                    "none, constant, cached, all", opt_overlap_check);
735         ret = -EINVAL;
736         goto fail;
737     }
738 
739     r->overlap_check = 0;
740     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
741         /* overlap-check defines a template bitmask, but every flag may be
742          * overwritten through the associated boolean option */
743         r->overlap_check |=
744             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
745                               overlap_check_template & (1 << i)) << i;
746     }
747 
748     r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
749     r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
750     r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
751         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
752                           flags & BDRV_O_UNMAP);
753     r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
754         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
755     r->discard_passthrough[QCOW2_DISCARD_OTHER] =
756         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
757 
758     ret = 0;
759 fail:
760     qemu_opts_del(opts);
761     opts = NULL;
762     return ret;
763 }
764 
765 static void qcow2_update_options_commit(BlockDriverState *bs,
766                                         Qcow2ReopenState *r)
767 {
768     BDRVQcow2State *s = bs->opaque;
769     int i;
770 
771     if (s->l2_table_cache) {
772         qcow2_cache_destroy(bs, s->l2_table_cache);
773     }
774     if (s->refcount_block_cache) {
775         qcow2_cache_destroy(bs, s->refcount_block_cache);
776     }
777     s->l2_table_cache = r->l2_table_cache;
778     s->refcount_block_cache = r->refcount_block_cache;
779 
780     s->overlap_check = r->overlap_check;
781     s->use_lazy_refcounts = r->use_lazy_refcounts;
782 
783     for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
784         s->discard_passthrough[i] = r->discard_passthrough[i];
785     }
786 
787     if (s->cache_clean_interval != r->cache_clean_interval) {
788         cache_clean_timer_del(bs);
789         s->cache_clean_interval = r->cache_clean_interval;
790         cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
791     }
792 }
793 
794 static void qcow2_update_options_abort(BlockDriverState *bs,
795                                        Qcow2ReopenState *r)
796 {
797     if (r->l2_table_cache) {
798         qcow2_cache_destroy(bs, r->l2_table_cache);
799     }
800     if (r->refcount_block_cache) {
801         qcow2_cache_destroy(bs, r->refcount_block_cache);
802     }
803 }
804 
805 static int qcow2_update_options(BlockDriverState *bs, QDict *options,
806                                 int flags, Error **errp)
807 {
808     Qcow2ReopenState r = {};
809     int ret;
810 
811     ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
812     if (ret >= 0) {
813         qcow2_update_options_commit(bs, &r);
814     } else {
815         qcow2_update_options_abort(bs, &r);
816     }
817 
818     return ret;
819 }
820 
821 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
822                       Error **errp)
823 {
824     BDRVQcow2State *s = bs->opaque;
825     unsigned int len, i;
826     int ret = 0;
827     QCowHeader header;
828     Error *local_err = NULL;
829     uint64_t ext_end;
830     uint64_t l1_vm_state_index;
831 
832     ret = bdrv_pread(bs->file->bs, 0, &header, sizeof(header));
833     if (ret < 0) {
834         error_setg_errno(errp, -ret, "Could not read qcow2 header");
835         goto fail;
836     }
837     be32_to_cpus(&header.magic);
838     be32_to_cpus(&header.version);
839     be64_to_cpus(&header.backing_file_offset);
840     be32_to_cpus(&header.backing_file_size);
841     be64_to_cpus(&header.size);
842     be32_to_cpus(&header.cluster_bits);
843     be32_to_cpus(&header.crypt_method);
844     be64_to_cpus(&header.l1_table_offset);
845     be32_to_cpus(&header.l1_size);
846     be64_to_cpus(&header.refcount_table_offset);
847     be32_to_cpus(&header.refcount_table_clusters);
848     be64_to_cpus(&header.snapshots_offset);
849     be32_to_cpus(&header.nb_snapshots);
850 
851     if (header.magic != QCOW_MAGIC) {
852         error_setg(errp, "Image is not in qcow2 format");
853         ret = -EINVAL;
854         goto fail;
855     }
856     if (header.version < 2 || header.version > 3) {
857         report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version);
858         ret = -ENOTSUP;
859         goto fail;
860     }
861 
862     s->qcow_version = header.version;
863 
864     /* Initialise cluster size */
865     if (header.cluster_bits < MIN_CLUSTER_BITS ||
866         header.cluster_bits > MAX_CLUSTER_BITS) {
867         error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
868                    header.cluster_bits);
869         ret = -EINVAL;
870         goto fail;
871     }
872 
873     s->cluster_bits = header.cluster_bits;
874     s->cluster_size = 1 << s->cluster_bits;
875     s->cluster_sectors = 1 << (s->cluster_bits - 9);
876 
877     /* Initialise version 3 header fields */
878     if (header.version == 2) {
879         header.incompatible_features    = 0;
880         header.compatible_features      = 0;
881         header.autoclear_features       = 0;
882         header.refcount_order           = 4;
883         header.header_length            = 72;
884     } else {
885         be64_to_cpus(&header.incompatible_features);
886         be64_to_cpus(&header.compatible_features);
887         be64_to_cpus(&header.autoclear_features);
888         be32_to_cpus(&header.refcount_order);
889         be32_to_cpus(&header.header_length);
890 
891         if (header.header_length < 104) {
892             error_setg(errp, "qcow2 header too short");
893             ret = -EINVAL;
894             goto fail;
895         }
896     }
897 
898     if (header.header_length > s->cluster_size) {
899         error_setg(errp, "qcow2 header exceeds cluster size");
900         ret = -EINVAL;
901         goto fail;
902     }
903 
904     if (header.header_length > sizeof(header)) {
905         s->unknown_header_fields_size = header.header_length - sizeof(header);
906         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
907         ret = bdrv_pread(bs->file->bs, sizeof(header), s->unknown_header_fields,
908                          s->unknown_header_fields_size);
909         if (ret < 0) {
910             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
911                              "fields");
912             goto fail;
913         }
914     }
915 
916     if (header.backing_file_offset > s->cluster_size) {
917         error_setg(errp, "Invalid backing file offset");
918         ret = -EINVAL;
919         goto fail;
920     }
921 
922     if (header.backing_file_offset) {
923         ext_end = header.backing_file_offset;
924     } else {
925         ext_end = 1 << header.cluster_bits;
926     }
927 
928     /* Handle feature bits */
929     s->incompatible_features    = header.incompatible_features;
930     s->compatible_features      = header.compatible_features;
931     s->autoclear_features       = header.autoclear_features;
932 
933     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
934         void *feature_table = NULL;
935         qcow2_read_extensions(bs, header.header_length, ext_end,
936                               &feature_table, NULL);
937         report_unsupported_feature(bs, errp, feature_table,
938                                    s->incompatible_features &
939                                    ~QCOW2_INCOMPAT_MASK);
940         ret = -ENOTSUP;
941         g_free(feature_table);
942         goto fail;
943     }
944 
945     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
946         /* Corrupt images may not be written to unless they are being repaired
947          */
948         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
949             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
950                        "read/write");
951             ret = -EACCES;
952             goto fail;
953         }
954     }
955 
956     /* Check support for various header values */
957     if (header.refcount_order > 6) {
958         error_setg(errp, "Reference count entry width too large; may not "
959                    "exceed 64 bits");
960         ret = -EINVAL;
961         goto fail;
962     }
963     s->refcount_order = header.refcount_order;
964     s->refcount_bits = 1 << s->refcount_order;
965     s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
966     s->refcount_max += s->refcount_max - 1;
967 
968     if (header.crypt_method > QCOW_CRYPT_AES) {
969         error_setg(errp, "Unsupported encryption method: %" PRIu32,
970                    header.crypt_method);
971         ret = -EINVAL;
972         goto fail;
973     }
974     if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
975         error_setg(errp, "AES cipher not available");
976         ret = -EINVAL;
977         goto fail;
978     }
979     s->crypt_method_header = header.crypt_method;
980     if (s->crypt_method_header) {
981         bs->encrypted = 1;
982     }
983 
984     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
985     s->l2_size = 1 << s->l2_bits;
986     /* 2^(s->refcount_order - 3) is the refcount width in bytes */
987     s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
988     s->refcount_block_size = 1 << s->refcount_block_bits;
989     bs->total_sectors = header.size / 512;
990     s->csize_shift = (62 - (s->cluster_bits - 8));
991     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
992     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
993 
994     s->refcount_table_offset = header.refcount_table_offset;
995     s->refcount_table_size =
996         header.refcount_table_clusters << (s->cluster_bits - 3);
997 
998     if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
999         error_setg(errp, "Reference count table too large");
1000         ret = -EINVAL;
1001         goto fail;
1002     }
1003 
1004     ret = validate_table_offset(bs, s->refcount_table_offset,
1005                                 s->refcount_table_size, sizeof(uint64_t));
1006     if (ret < 0) {
1007         error_setg(errp, "Invalid reference count table offset");
1008         goto fail;
1009     }
1010 
1011     /* Snapshot table offset/length */
1012     if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
1013         error_setg(errp, "Too many snapshots");
1014         ret = -EINVAL;
1015         goto fail;
1016     }
1017 
1018     ret = validate_table_offset(bs, header.snapshots_offset,
1019                                 header.nb_snapshots,
1020                                 sizeof(QCowSnapshotHeader));
1021     if (ret < 0) {
1022         error_setg(errp, "Invalid snapshot table offset");
1023         goto fail;
1024     }
1025 
1026     /* read the level 1 table */
1027     if (header.l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
1028         error_setg(errp, "Active L1 table too large");
1029         ret = -EFBIG;
1030         goto fail;
1031     }
1032     s->l1_size = header.l1_size;
1033 
1034     l1_vm_state_index = size_to_l1(s, header.size);
1035     if (l1_vm_state_index > INT_MAX) {
1036         error_setg(errp, "Image is too big");
1037         ret = -EFBIG;
1038         goto fail;
1039     }
1040     s->l1_vm_state_index = l1_vm_state_index;
1041 
1042     /* the L1 table must contain at least enough entries to put
1043        header.size bytes */
1044     if (s->l1_size < s->l1_vm_state_index) {
1045         error_setg(errp, "L1 table is too small");
1046         ret = -EINVAL;
1047         goto fail;
1048     }
1049 
1050     ret = validate_table_offset(bs, header.l1_table_offset,
1051                                 header.l1_size, sizeof(uint64_t));
1052     if (ret < 0) {
1053         error_setg(errp, "Invalid L1 table offset");
1054         goto fail;
1055     }
1056     s->l1_table_offset = header.l1_table_offset;
1057 
1058 
1059     if (s->l1_size > 0) {
1060         s->l1_table = qemu_try_blockalign(bs->file->bs,
1061             align_offset(s->l1_size * sizeof(uint64_t), 512));
1062         if (s->l1_table == NULL) {
1063             error_setg(errp, "Could not allocate L1 table");
1064             ret = -ENOMEM;
1065             goto fail;
1066         }
1067         ret = bdrv_pread(bs->file->bs, s->l1_table_offset, s->l1_table,
1068                          s->l1_size * sizeof(uint64_t));
1069         if (ret < 0) {
1070             error_setg_errno(errp, -ret, "Could not read L1 table");
1071             goto fail;
1072         }
1073         for(i = 0;i < s->l1_size; i++) {
1074             be64_to_cpus(&s->l1_table[i]);
1075         }
1076     }
1077 
1078     /* Parse driver-specific options */
1079     ret = qcow2_update_options(bs, options, flags, errp);
1080     if (ret < 0) {
1081         goto fail;
1082     }
1083 
1084     s->cluster_cache = g_malloc(s->cluster_size);
1085     /* one more sector for decompressed data alignment */
1086     s->cluster_data = qemu_try_blockalign(bs->file->bs, QCOW_MAX_CRYPT_CLUSTERS
1087                                                     * s->cluster_size + 512);
1088     if (s->cluster_data == NULL) {
1089         error_setg(errp, "Could not allocate temporary cluster buffer");
1090         ret = -ENOMEM;
1091         goto fail;
1092     }
1093 
1094     s->cluster_cache_offset = -1;
1095     s->flags = flags;
1096 
1097     ret = qcow2_refcount_init(bs);
1098     if (ret != 0) {
1099         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1100         goto fail;
1101     }
1102 
1103     QLIST_INIT(&s->cluster_allocs);
1104     QTAILQ_INIT(&s->discards);
1105 
1106     /* read qcow2 extensions */
1107     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1108         &local_err)) {
1109         error_propagate(errp, local_err);
1110         ret = -EINVAL;
1111         goto fail;
1112     }
1113 
1114     /* read the backing file name */
1115     if (header.backing_file_offset != 0) {
1116         len = header.backing_file_size;
1117         if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1118             len >= sizeof(bs->backing_file)) {
1119             error_setg(errp, "Backing file name too long");
1120             ret = -EINVAL;
1121             goto fail;
1122         }
1123         ret = bdrv_pread(bs->file->bs, header.backing_file_offset,
1124                          bs->backing_file, len);
1125         if (ret < 0) {
1126             error_setg_errno(errp, -ret, "Could not read backing file name");
1127             goto fail;
1128         }
1129         bs->backing_file[len] = '\0';
1130         s->image_backing_file = g_strdup(bs->backing_file);
1131     }
1132 
1133     /* Internal snapshots */
1134     s->snapshots_offset = header.snapshots_offset;
1135     s->nb_snapshots = header.nb_snapshots;
1136 
1137     ret = qcow2_read_snapshots(bs);
1138     if (ret < 0) {
1139         error_setg_errno(errp, -ret, "Could not read snapshots");
1140         goto fail;
1141     }
1142 
1143     /* Clear unknown autoclear feature bits */
1144     if (!bs->read_only && !(flags & BDRV_O_INACTIVE) && s->autoclear_features) {
1145         s->autoclear_features = 0;
1146         ret = qcow2_update_header(bs);
1147         if (ret < 0) {
1148             error_setg_errno(errp, -ret, "Could not update qcow2 header");
1149             goto fail;
1150         }
1151     }
1152 
1153     /* Initialise locks */
1154     qemu_co_mutex_init(&s->lock);
1155 
1156     /* Repair image if dirty */
1157     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
1158         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1159         BdrvCheckResult result = {0};
1160 
1161         ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1162         if (ret < 0) {
1163             error_setg_errno(errp, -ret, "Could not repair dirty image");
1164             goto fail;
1165         }
1166     }
1167 
1168 #ifdef DEBUG_ALLOC
1169     {
1170         BdrvCheckResult result = {0};
1171         qcow2_check_refcounts(bs, &result, 0);
1172     }
1173 #endif
1174     return ret;
1175 
1176  fail:
1177     g_free(s->unknown_header_fields);
1178     cleanup_unknown_header_ext(bs);
1179     qcow2_free_snapshots(bs);
1180     qcow2_refcount_close(bs);
1181     qemu_vfree(s->l1_table);
1182     /* else pre-write overlap checks in cache_destroy may crash */
1183     s->l1_table = NULL;
1184     cache_clean_timer_del(bs);
1185     if (s->l2_table_cache) {
1186         qcow2_cache_destroy(bs, s->l2_table_cache);
1187     }
1188     if (s->refcount_block_cache) {
1189         qcow2_cache_destroy(bs, s->refcount_block_cache);
1190     }
1191     g_free(s->cluster_cache);
1192     qemu_vfree(s->cluster_data);
1193     return ret;
1194 }
1195 
1196 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1197 {
1198     BDRVQcow2State *s = bs->opaque;
1199 
1200     bs->bl.write_zeroes_alignment = s->cluster_sectors;
1201 }
1202 
1203 static int qcow2_set_key(BlockDriverState *bs, const char *key)
1204 {
1205     BDRVQcow2State *s = bs->opaque;
1206     uint8_t keybuf[16];
1207     int len, i;
1208     Error *err = NULL;
1209 
1210     memset(keybuf, 0, 16);
1211     len = strlen(key);
1212     if (len > 16)
1213         len = 16;
1214     /* XXX: we could compress the chars to 7 bits to increase
1215        entropy */
1216     for(i = 0;i < len;i++) {
1217         keybuf[i] = key[i];
1218     }
1219     assert(bs->encrypted);
1220 
1221     qcrypto_cipher_free(s->cipher);
1222     s->cipher = qcrypto_cipher_new(
1223         QCRYPTO_CIPHER_ALG_AES_128,
1224         QCRYPTO_CIPHER_MODE_CBC,
1225         keybuf, G_N_ELEMENTS(keybuf),
1226         &err);
1227 
1228     if (!s->cipher) {
1229         /* XXX would be nice if errors in this method could
1230          * be properly propagate to the caller. Would need
1231          * the bdrv_set_key() API signature to be fixed. */
1232         error_free(err);
1233         return -1;
1234     }
1235     return 0;
1236 }
1237 
1238 static int qcow2_reopen_prepare(BDRVReopenState *state,
1239                                 BlockReopenQueue *queue, Error **errp)
1240 {
1241     Qcow2ReopenState *r;
1242     int ret;
1243 
1244     r = g_new0(Qcow2ReopenState, 1);
1245     state->opaque = r;
1246 
1247     ret = qcow2_update_options_prepare(state->bs, r, state->options,
1248                                        state->flags, errp);
1249     if (ret < 0) {
1250         goto fail;
1251     }
1252 
1253     /* We need to write out any unwritten data if we reopen read-only. */
1254     if ((state->flags & BDRV_O_RDWR) == 0) {
1255         ret = bdrv_flush(state->bs);
1256         if (ret < 0) {
1257             goto fail;
1258         }
1259 
1260         ret = qcow2_mark_clean(state->bs);
1261         if (ret < 0) {
1262             goto fail;
1263         }
1264     }
1265 
1266     return 0;
1267 
1268 fail:
1269     qcow2_update_options_abort(state->bs, r);
1270     g_free(r);
1271     return ret;
1272 }
1273 
1274 static void qcow2_reopen_commit(BDRVReopenState *state)
1275 {
1276     qcow2_update_options_commit(state->bs, state->opaque);
1277     g_free(state->opaque);
1278 }
1279 
1280 static void qcow2_reopen_abort(BDRVReopenState *state)
1281 {
1282     qcow2_update_options_abort(state->bs, state->opaque);
1283     g_free(state->opaque);
1284 }
1285 
1286 static void qcow2_join_options(QDict *options, QDict *old_options)
1287 {
1288     bool has_new_overlap_template =
1289         qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
1290         qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
1291     bool has_new_total_cache_size =
1292         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
1293     bool has_all_cache_options;
1294 
1295     /* New overlap template overrides all old overlap options */
1296     if (has_new_overlap_template) {
1297         qdict_del(old_options, QCOW2_OPT_OVERLAP);
1298         qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
1299         qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
1300         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
1301         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
1302         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
1303         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
1304         qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
1305         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
1306         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
1307     }
1308 
1309     /* New total cache size overrides all old options */
1310     if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
1311         qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
1312         qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1313     }
1314 
1315     qdict_join(options, old_options, false);
1316 
1317     /*
1318      * If after merging all cache size options are set, an old total size is
1319      * overwritten. Do keep all options, however, if all three are new. The
1320      * resulting error message is what we want to happen.
1321      */
1322     has_all_cache_options =
1323         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
1324         qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
1325         qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1326 
1327     if (has_all_cache_options && !has_new_total_cache_size) {
1328         qdict_del(options, QCOW2_OPT_CACHE_SIZE);
1329     }
1330 }
1331 
1332 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
1333         int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
1334 {
1335     BDRVQcow2State *s = bs->opaque;
1336     uint64_t cluster_offset;
1337     int index_in_cluster, ret;
1338     int64_t status = 0;
1339 
1340     *pnum = nb_sectors;
1341     qemu_co_mutex_lock(&s->lock);
1342     ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
1343     qemu_co_mutex_unlock(&s->lock);
1344     if (ret < 0) {
1345         return ret;
1346     }
1347 
1348     if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1349         !s->cipher) {
1350         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1351         cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
1352         *file = bs->file->bs;
1353         status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
1354     }
1355     if (ret == QCOW2_CLUSTER_ZERO) {
1356         status |= BDRV_BLOCK_ZERO;
1357     } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1358         status |= BDRV_BLOCK_DATA;
1359     }
1360     return status;
1361 }
1362 
1363 /* handle reading after the end of the backing file */
1364 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
1365                   int64_t sector_num, int nb_sectors)
1366 {
1367     int n1;
1368     if ((sector_num + nb_sectors) <= bs->total_sectors)
1369         return nb_sectors;
1370     if (sector_num >= bs->total_sectors)
1371         n1 = 0;
1372     else
1373         n1 = bs->total_sectors - sector_num;
1374 
1375     qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
1376 
1377     return n1;
1378 }
1379 
1380 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
1381                           int remaining_sectors, QEMUIOVector *qiov)
1382 {
1383     BDRVQcow2State *s = bs->opaque;
1384     int index_in_cluster, n1;
1385     int ret;
1386     int cur_nr_sectors; /* number of sectors in current iteration */
1387     uint64_t cluster_offset = 0;
1388     uint64_t bytes_done = 0;
1389     QEMUIOVector hd_qiov;
1390     uint8_t *cluster_data = NULL;
1391 
1392     qemu_iovec_init(&hd_qiov, qiov->niov);
1393 
1394     qemu_co_mutex_lock(&s->lock);
1395 
1396     while (remaining_sectors != 0) {
1397 
1398         /* prepare next request */
1399         cur_nr_sectors = remaining_sectors;
1400         if (s->cipher) {
1401             cur_nr_sectors = MIN(cur_nr_sectors,
1402                 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1403         }
1404 
1405         ret = qcow2_get_cluster_offset(bs, sector_num << 9,
1406             &cur_nr_sectors, &cluster_offset);
1407         if (ret < 0) {
1408             goto fail;
1409         }
1410 
1411         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1412 
1413         qemu_iovec_reset(&hd_qiov);
1414         qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1415             cur_nr_sectors * 512);
1416 
1417         switch (ret) {
1418         case QCOW2_CLUSTER_UNALLOCATED:
1419 
1420             if (bs->backing) {
1421                 /* read from the base image */
1422                 n1 = qcow2_backing_read1(bs->backing->bs, &hd_qiov,
1423                     sector_num, cur_nr_sectors);
1424                 if (n1 > 0) {
1425                     QEMUIOVector local_qiov;
1426 
1427                     qemu_iovec_init(&local_qiov, hd_qiov.niov);
1428                     qemu_iovec_concat(&local_qiov, &hd_qiov, 0,
1429                                       n1 * BDRV_SECTOR_SIZE);
1430 
1431                     BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1432                     qemu_co_mutex_unlock(&s->lock);
1433                     ret = bdrv_co_readv(bs->backing->bs, sector_num,
1434                                         n1, &local_qiov);
1435                     qemu_co_mutex_lock(&s->lock);
1436 
1437                     qemu_iovec_destroy(&local_qiov);
1438 
1439                     if (ret < 0) {
1440                         goto fail;
1441                     }
1442                 }
1443             } else {
1444                 /* Note: in this case, no need to wait */
1445                 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1446             }
1447             break;
1448 
1449         case QCOW2_CLUSTER_ZERO:
1450             qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1451             break;
1452 
1453         case QCOW2_CLUSTER_COMPRESSED:
1454             /* add AIO support for compressed blocks ? */
1455             ret = qcow2_decompress_cluster(bs, cluster_offset);
1456             if (ret < 0) {
1457                 goto fail;
1458             }
1459 
1460             qemu_iovec_from_buf(&hd_qiov, 0,
1461                 s->cluster_cache + index_in_cluster * 512,
1462                 512 * cur_nr_sectors);
1463             break;
1464 
1465         case QCOW2_CLUSTER_NORMAL:
1466             if ((cluster_offset & 511) != 0) {
1467                 ret = -EIO;
1468                 goto fail;
1469             }
1470 
1471             if (bs->encrypted) {
1472                 assert(s->cipher);
1473 
1474                 /*
1475                  * For encrypted images, read everything into a temporary
1476                  * contiguous buffer on which the AES functions can work.
1477                  */
1478                 if (!cluster_data) {
1479                     cluster_data =
1480                         qemu_try_blockalign(bs->file->bs,
1481                                             QCOW_MAX_CRYPT_CLUSTERS
1482                                             * s->cluster_size);
1483                     if (cluster_data == NULL) {
1484                         ret = -ENOMEM;
1485                         goto fail;
1486                     }
1487                 }
1488 
1489                 assert(cur_nr_sectors <=
1490                     QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1491                 qemu_iovec_reset(&hd_qiov);
1492                 qemu_iovec_add(&hd_qiov, cluster_data,
1493                     512 * cur_nr_sectors);
1494             }
1495 
1496             BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1497             qemu_co_mutex_unlock(&s->lock);
1498             ret = bdrv_co_readv(bs->file->bs,
1499                                 (cluster_offset >> 9) + index_in_cluster,
1500                                 cur_nr_sectors, &hd_qiov);
1501             qemu_co_mutex_lock(&s->lock);
1502             if (ret < 0) {
1503                 goto fail;
1504             }
1505             if (bs->encrypted) {
1506                 assert(s->cipher);
1507                 Error *err = NULL;
1508                 if (qcow2_encrypt_sectors(s, sector_num,  cluster_data,
1509                                           cluster_data, cur_nr_sectors, false,
1510                                           &err) < 0) {
1511                     error_free(err);
1512                     ret = -EIO;
1513                     goto fail;
1514                 }
1515                 qemu_iovec_from_buf(qiov, bytes_done,
1516                     cluster_data, 512 * cur_nr_sectors);
1517             }
1518             break;
1519 
1520         default:
1521             g_assert_not_reached();
1522             ret = -EIO;
1523             goto fail;
1524         }
1525 
1526         remaining_sectors -= cur_nr_sectors;
1527         sector_num += cur_nr_sectors;
1528         bytes_done += cur_nr_sectors * 512;
1529     }
1530     ret = 0;
1531 
1532 fail:
1533     qemu_co_mutex_unlock(&s->lock);
1534 
1535     qemu_iovec_destroy(&hd_qiov);
1536     qemu_vfree(cluster_data);
1537 
1538     return ret;
1539 }
1540 
1541 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
1542                            int64_t sector_num,
1543                            int remaining_sectors,
1544                            QEMUIOVector *qiov)
1545 {
1546     BDRVQcow2State *s = bs->opaque;
1547     int index_in_cluster;
1548     int ret;
1549     int cur_nr_sectors; /* number of sectors in current iteration */
1550     uint64_t cluster_offset;
1551     QEMUIOVector hd_qiov;
1552     uint64_t bytes_done = 0;
1553     uint8_t *cluster_data = NULL;
1554     QCowL2Meta *l2meta = NULL;
1555 
1556     trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1557                                  remaining_sectors);
1558 
1559     qemu_iovec_init(&hd_qiov, qiov->niov);
1560 
1561     s->cluster_cache_offset = -1; /* disable compressed cache */
1562 
1563     qemu_co_mutex_lock(&s->lock);
1564 
1565     while (remaining_sectors != 0) {
1566 
1567         l2meta = NULL;
1568 
1569         trace_qcow2_writev_start_part(qemu_coroutine_self());
1570         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1571         cur_nr_sectors = remaining_sectors;
1572         if (bs->encrypted &&
1573             cur_nr_sectors >
1574             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1575             cur_nr_sectors =
1576                 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
1577         }
1578 
1579         ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
1580             &cur_nr_sectors, &cluster_offset, &l2meta);
1581         if (ret < 0) {
1582             goto fail;
1583         }
1584 
1585         assert((cluster_offset & 511) == 0);
1586 
1587         qemu_iovec_reset(&hd_qiov);
1588         qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1589             cur_nr_sectors * 512);
1590 
1591         if (bs->encrypted) {
1592             Error *err = NULL;
1593             assert(s->cipher);
1594             if (!cluster_data) {
1595                 cluster_data = qemu_try_blockalign(bs->file->bs,
1596                                                    QCOW_MAX_CRYPT_CLUSTERS
1597                                                    * s->cluster_size);
1598                 if (cluster_data == NULL) {
1599                     ret = -ENOMEM;
1600                     goto fail;
1601                 }
1602             }
1603 
1604             assert(hd_qiov.size <=
1605                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1606             qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
1607 
1608             if (qcow2_encrypt_sectors(s, sector_num, cluster_data,
1609                                       cluster_data, cur_nr_sectors,
1610                                       true, &err) < 0) {
1611                 error_free(err);
1612                 ret = -EIO;
1613                 goto fail;
1614             }
1615 
1616             qemu_iovec_reset(&hd_qiov);
1617             qemu_iovec_add(&hd_qiov, cluster_data,
1618                 cur_nr_sectors * 512);
1619         }
1620 
1621         ret = qcow2_pre_write_overlap_check(bs, 0,
1622                 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1623                 cur_nr_sectors * BDRV_SECTOR_SIZE);
1624         if (ret < 0) {
1625             goto fail;
1626         }
1627 
1628         qemu_co_mutex_unlock(&s->lock);
1629         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1630         trace_qcow2_writev_data(qemu_coroutine_self(),
1631                                 (cluster_offset >> 9) + index_in_cluster);
1632         ret = bdrv_co_writev(bs->file->bs,
1633                              (cluster_offset >> 9) + index_in_cluster,
1634                              cur_nr_sectors, &hd_qiov);
1635         qemu_co_mutex_lock(&s->lock);
1636         if (ret < 0) {
1637             goto fail;
1638         }
1639 
1640         while (l2meta != NULL) {
1641             QCowL2Meta *next;
1642 
1643             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1644             if (ret < 0) {
1645                 goto fail;
1646             }
1647 
1648             /* Take the request off the list of running requests */
1649             if (l2meta->nb_clusters != 0) {
1650                 QLIST_REMOVE(l2meta, next_in_flight);
1651             }
1652 
1653             qemu_co_queue_restart_all(&l2meta->dependent_requests);
1654 
1655             next = l2meta->next;
1656             g_free(l2meta);
1657             l2meta = next;
1658         }
1659 
1660         remaining_sectors -= cur_nr_sectors;
1661         sector_num += cur_nr_sectors;
1662         bytes_done += cur_nr_sectors * 512;
1663         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
1664     }
1665     ret = 0;
1666 
1667 fail:
1668     qemu_co_mutex_unlock(&s->lock);
1669 
1670     while (l2meta != NULL) {
1671         QCowL2Meta *next;
1672 
1673         if (l2meta->nb_clusters != 0) {
1674             QLIST_REMOVE(l2meta, next_in_flight);
1675         }
1676         qemu_co_queue_restart_all(&l2meta->dependent_requests);
1677 
1678         next = l2meta->next;
1679         g_free(l2meta);
1680         l2meta = next;
1681     }
1682 
1683     qemu_iovec_destroy(&hd_qiov);
1684     qemu_vfree(cluster_data);
1685     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
1686 
1687     return ret;
1688 }
1689 
1690 static int qcow2_inactivate(BlockDriverState *bs)
1691 {
1692     BDRVQcow2State *s = bs->opaque;
1693     int ret, result = 0;
1694 
1695     ret = qcow2_cache_flush(bs, s->l2_table_cache);
1696     if (ret) {
1697         result = ret;
1698         error_report("Failed to flush the L2 table cache: %s",
1699                      strerror(-ret));
1700     }
1701 
1702     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1703     if (ret) {
1704         result = ret;
1705         error_report("Failed to flush the refcount block cache: %s",
1706                      strerror(-ret));
1707     }
1708 
1709     if (result == 0) {
1710         qcow2_mark_clean(bs);
1711     }
1712 
1713     return result;
1714 }
1715 
1716 static void qcow2_close(BlockDriverState *bs)
1717 {
1718     BDRVQcow2State *s = bs->opaque;
1719     qemu_vfree(s->l1_table);
1720     /* else pre-write overlap checks in cache_destroy may crash */
1721     s->l1_table = NULL;
1722 
1723     if (!(s->flags & BDRV_O_INACTIVE)) {
1724         qcow2_inactivate(bs);
1725     }
1726 
1727     cache_clean_timer_del(bs);
1728     qcow2_cache_destroy(bs, s->l2_table_cache);
1729     qcow2_cache_destroy(bs, s->refcount_block_cache);
1730 
1731     qcrypto_cipher_free(s->cipher);
1732     s->cipher = NULL;
1733 
1734     g_free(s->unknown_header_fields);
1735     cleanup_unknown_header_ext(bs);
1736 
1737     g_free(s->image_backing_file);
1738     g_free(s->image_backing_format);
1739 
1740     g_free(s->cluster_cache);
1741     qemu_vfree(s->cluster_data);
1742     qcow2_refcount_close(bs);
1743     qcow2_free_snapshots(bs);
1744 }
1745 
1746 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
1747 {
1748     BDRVQcow2State *s = bs->opaque;
1749     int flags = s->flags;
1750     QCryptoCipher *cipher = NULL;
1751     QDict *options;
1752     Error *local_err = NULL;
1753     int ret;
1754 
1755     /*
1756      * Backing files are read-only which makes all of their metadata immutable,
1757      * that means we don't have to worry about reopening them here.
1758      */
1759 
1760     cipher = s->cipher;
1761     s->cipher = NULL;
1762 
1763     qcow2_close(bs);
1764 
1765     bdrv_invalidate_cache(bs->file->bs, &local_err);
1766     if (local_err) {
1767         error_propagate(errp, local_err);
1768         bs->drv = NULL;
1769         return;
1770     }
1771 
1772     memset(s, 0, sizeof(BDRVQcow2State));
1773     options = qdict_clone_shallow(bs->options);
1774 
1775     flags &= ~BDRV_O_INACTIVE;
1776     ret = qcow2_open(bs, options, flags, &local_err);
1777     QDECREF(options);
1778     if (local_err) {
1779         error_propagate(errp, local_err);
1780         error_prepend(errp, "Could not reopen qcow2 layer: ");
1781         bs->drv = NULL;
1782         return;
1783     } else if (ret < 0) {
1784         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1785         bs->drv = NULL;
1786         return;
1787     }
1788 
1789     s->cipher = cipher;
1790 }
1791 
1792 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1793     size_t len, size_t buflen)
1794 {
1795     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1796     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1797 
1798     if (buflen < ext_len) {
1799         return -ENOSPC;
1800     }
1801 
1802     *ext_backing_fmt = (QCowExtension) {
1803         .magic  = cpu_to_be32(magic),
1804         .len    = cpu_to_be32(len),
1805     };
1806     memcpy(buf + sizeof(QCowExtension), s, len);
1807 
1808     return ext_len;
1809 }
1810 
1811 /*
1812  * Updates the qcow2 header, including the variable length parts of it, i.e.
1813  * the backing file name and all extensions. qcow2 was not designed to allow
1814  * such changes, so if we run out of space (we can only use the first cluster)
1815  * this function may fail.
1816  *
1817  * Returns 0 on success, -errno in error cases.
1818  */
1819 int qcow2_update_header(BlockDriverState *bs)
1820 {
1821     BDRVQcow2State *s = bs->opaque;
1822     QCowHeader *header;
1823     char *buf;
1824     size_t buflen = s->cluster_size;
1825     int ret;
1826     uint64_t total_size;
1827     uint32_t refcount_table_clusters;
1828     size_t header_length;
1829     Qcow2UnknownHeaderExtension *uext;
1830 
1831     buf = qemu_blockalign(bs, buflen);
1832 
1833     /* Header structure */
1834     header = (QCowHeader*) buf;
1835 
1836     if (buflen < sizeof(*header)) {
1837         ret = -ENOSPC;
1838         goto fail;
1839     }
1840 
1841     header_length = sizeof(*header) + s->unknown_header_fields_size;
1842     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1843     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1844 
1845     *header = (QCowHeader) {
1846         /* Version 2 fields */
1847         .magic                  = cpu_to_be32(QCOW_MAGIC),
1848         .version                = cpu_to_be32(s->qcow_version),
1849         .backing_file_offset    = 0,
1850         .backing_file_size      = 0,
1851         .cluster_bits           = cpu_to_be32(s->cluster_bits),
1852         .size                   = cpu_to_be64(total_size),
1853         .crypt_method           = cpu_to_be32(s->crypt_method_header),
1854         .l1_size                = cpu_to_be32(s->l1_size),
1855         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
1856         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
1857         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1858         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
1859         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
1860 
1861         /* Version 3 fields */
1862         .incompatible_features  = cpu_to_be64(s->incompatible_features),
1863         .compatible_features    = cpu_to_be64(s->compatible_features),
1864         .autoclear_features     = cpu_to_be64(s->autoclear_features),
1865         .refcount_order         = cpu_to_be32(s->refcount_order),
1866         .header_length          = cpu_to_be32(header_length),
1867     };
1868 
1869     /* For older versions, write a shorter header */
1870     switch (s->qcow_version) {
1871     case 2:
1872         ret = offsetof(QCowHeader, incompatible_features);
1873         break;
1874     case 3:
1875         ret = sizeof(*header);
1876         break;
1877     default:
1878         ret = -EINVAL;
1879         goto fail;
1880     }
1881 
1882     buf += ret;
1883     buflen -= ret;
1884     memset(buf, 0, buflen);
1885 
1886     /* Preserve any unknown field in the header */
1887     if (s->unknown_header_fields_size) {
1888         if (buflen < s->unknown_header_fields_size) {
1889             ret = -ENOSPC;
1890             goto fail;
1891         }
1892 
1893         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1894         buf += s->unknown_header_fields_size;
1895         buflen -= s->unknown_header_fields_size;
1896     }
1897 
1898     /* Backing file format header extension */
1899     if (s->image_backing_format) {
1900         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1901                              s->image_backing_format,
1902                              strlen(s->image_backing_format),
1903                              buflen);
1904         if (ret < 0) {
1905             goto fail;
1906         }
1907 
1908         buf += ret;
1909         buflen -= ret;
1910     }
1911 
1912     /* Feature table */
1913     if (s->qcow_version >= 3) {
1914         Qcow2Feature features[] = {
1915             {
1916                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1917                 .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
1918                 .name = "dirty bit",
1919             },
1920             {
1921                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1922                 .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
1923                 .name = "corrupt bit",
1924             },
1925             {
1926                 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1927                 .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1928                 .name = "lazy refcounts",
1929             },
1930         };
1931 
1932         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1933                              features, sizeof(features), buflen);
1934         if (ret < 0) {
1935             goto fail;
1936         }
1937         buf += ret;
1938         buflen -= ret;
1939     }
1940 
1941     /* Keep unknown header extensions */
1942     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1943         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1944         if (ret < 0) {
1945             goto fail;
1946         }
1947 
1948         buf += ret;
1949         buflen -= ret;
1950     }
1951 
1952     /* End of header extensions */
1953     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1954     if (ret < 0) {
1955         goto fail;
1956     }
1957 
1958     buf += ret;
1959     buflen -= ret;
1960 
1961     /* Backing file name */
1962     if (s->image_backing_file) {
1963         size_t backing_file_len = strlen(s->image_backing_file);
1964 
1965         if (buflen < backing_file_len) {
1966             ret = -ENOSPC;
1967             goto fail;
1968         }
1969 
1970         /* Using strncpy is ok here, since buf is not NUL-terminated. */
1971         strncpy(buf, s->image_backing_file, buflen);
1972 
1973         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1974         header->backing_file_size   = cpu_to_be32(backing_file_len);
1975     }
1976 
1977     /* Write the new header */
1978     ret = bdrv_pwrite(bs->file->bs, 0, header, s->cluster_size);
1979     if (ret < 0) {
1980         goto fail;
1981     }
1982 
1983     ret = 0;
1984 fail:
1985     qemu_vfree(header);
1986     return ret;
1987 }
1988 
1989 static int qcow2_change_backing_file(BlockDriverState *bs,
1990     const char *backing_file, const char *backing_fmt)
1991 {
1992     BDRVQcow2State *s = bs->opaque;
1993 
1994     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1995     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1996 
1997     g_free(s->image_backing_file);
1998     g_free(s->image_backing_format);
1999 
2000     s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2001     s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2002 
2003     return qcow2_update_header(bs);
2004 }
2005 
2006 static int preallocate(BlockDriverState *bs)
2007 {
2008     uint64_t nb_sectors;
2009     uint64_t offset;
2010     uint64_t host_offset = 0;
2011     int num;
2012     int ret;
2013     QCowL2Meta *meta;
2014 
2015     nb_sectors = bdrv_nb_sectors(bs);
2016     offset = 0;
2017 
2018     while (nb_sectors) {
2019         num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
2020         ret = qcow2_alloc_cluster_offset(bs, offset, &num,
2021                                          &host_offset, &meta);
2022         if (ret < 0) {
2023             return ret;
2024         }
2025 
2026         while (meta) {
2027             QCowL2Meta *next = meta->next;
2028 
2029             ret = qcow2_alloc_cluster_link_l2(bs, meta);
2030             if (ret < 0) {
2031                 qcow2_free_any_clusters(bs, meta->alloc_offset,
2032                                         meta->nb_clusters, QCOW2_DISCARD_NEVER);
2033                 return ret;
2034             }
2035 
2036             /* There are no dependent requests, but we need to remove our
2037              * request from the list of in-flight requests */
2038             QLIST_REMOVE(meta, next_in_flight);
2039 
2040             g_free(meta);
2041             meta = next;
2042         }
2043 
2044         /* TODO Preallocate data if requested */
2045 
2046         nb_sectors -= num;
2047         offset += num << BDRV_SECTOR_BITS;
2048     }
2049 
2050     /*
2051      * It is expected that the image file is large enough to actually contain
2052      * all of the allocated clusters (otherwise we get failing reads after
2053      * EOF). Extend the image to the last allocated sector.
2054      */
2055     if (host_offset != 0) {
2056         uint8_t buf[BDRV_SECTOR_SIZE];
2057         memset(buf, 0, BDRV_SECTOR_SIZE);
2058         ret = bdrv_write(bs->file->bs,
2059                          (host_offset >> BDRV_SECTOR_BITS) + num - 1,
2060                          buf, 1);
2061         if (ret < 0) {
2062             return ret;
2063         }
2064     }
2065 
2066     return 0;
2067 }
2068 
2069 static int qcow2_create2(const char *filename, int64_t total_size,
2070                          const char *backing_file, const char *backing_format,
2071                          int flags, size_t cluster_size, PreallocMode prealloc,
2072                          QemuOpts *opts, int version, int refcount_order,
2073                          Error **errp)
2074 {
2075     int cluster_bits;
2076     QDict *options;
2077 
2078     /* Calculate cluster_bits */
2079     cluster_bits = ctz32(cluster_size);
2080     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
2081         (1 << cluster_bits) != cluster_size)
2082     {
2083         error_setg(errp, "Cluster size must be a power of two between %d and "
2084                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
2085         return -EINVAL;
2086     }
2087 
2088     /*
2089      * Open the image file and write a minimal qcow2 header.
2090      *
2091      * We keep things simple and start with a zero-sized image. We also
2092      * do without refcount blocks or a L1 table for now. We'll fix the
2093      * inconsistency later.
2094      *
2095      * We do need a refcount table because growing the refcount table means
2096      * allocating two new refcount blocks - the seconds of which would be at
2097      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
2098      * size for any qcow2 image.
2099      */
2100     BlockDriverState* bs;
2101     QCowHeader *header;
2102     uint64_t* refcount_table;
2103     Error *local_err = NULL;
2104     int ret;
2105 
2106     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
2107         /* Note: The following calculation does not need to be exact; if it is a
2108          * bit off, either some bytes will be "leaked" (which is fine) or we
2109          * will need to increase the file size by some bytes (which is fine,
2110          * too, as long as the bulk is allocated here). Therefore, using
2111          * floating point arithmetic is fine. */
2112         int64_t meta_size = 0;
2113         uint64_t nreftablee, nrefblocke, nl1e, nl2e;
2114         int64_t aligned_total_size = align_offset(total_size, cluster_size);
2115         int refblock_bits, refblock_size;
2116         /* refcount entry size in bytes */
2117         double rces = (1 << refcount_order) / 8.;
2118 
2119         /* see qcow2_open() */
2120         refblock_bits = cluster_bits - (refcount_order - 3);
2121         refblock_size = 1 << refblock_bits;
2122 
2123         /* header: 1 cluster */
2124         meta_size += cluster_size;
2125 
2126         /* total size of L2 tables */
2127         nl2e = aligned_total_size / cluster_size;
2128         nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
2129         meta_size += nl2e * sizeof(uint64_t);
2130 
2131         /* total size of L1 tables */
2132         nl1e = nl2e * sizeof(uint64_t) / cluster_size;
2133         nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
2134         meta_size += nl1e * sizeof(uint64_t);
2135 
2136         /* total size of refcount blocks
2137          *
2138          * note: every host cluster is reference-counted, including metadata
2139          * (even refcount blocks are recursively included).
2140          * Let:
2141          *   a = total_size (this is the guest disk size)
2142          *   m = meta size not including refcount blocks and refcount tables
2143          *   c = cluster size
2144          *   y1 = number of refcount blocks entries
2145          *   y2 = meta size including everything
2146          *   rces = refcount entry size in bytes
2147          * then,
2148          *   y1 = (y2 + a)/c
2149          *   y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m
2150          * we can get y1:
2151          *   y1 = (a + m) / (c - rces - rces * sizeof(u64) / c)
2152          */
2153         nrefblocke = (aligned_total_size + meta_size + cluster_size)
2154                    / (cluster_size - rces - rces * sizeof(uint64_t)
2155                                                  / cluster_size);
2156         meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size;
2157 
2158         /* total size of refcount tables */
2159         nreftablee = nrefblocke / refblock_size;
2160         nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
2161         meta_size += nreftablee * sizeof(uint64_t);
2162 
2163         qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
2164                             aligned_total_size + meta_size, &error_abort);
2165         qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc],
2166                      &error_abort);
2167     }
2168 
2169     ret = bdrv_create_file(filename, opts, &local_err);
2170     if (ret < 0) {
2171         error_propagate(errp, local_err);
2172         return ret;
2173     }
2174 
2175     bs = NULL;
2176     ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
2177                     &local_err);
2178     if (ret < 0) {
2179         error_propagate(errp, local_err);
2180         return ret;
2181     }
2182 
2183     /* Write the header */
2184     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2185     header = g_malloc0(cluster_size);
2186     *header = (QCowHeader) {
2187         .magic                      = cpu_to_be32(QCOW_MAGIC),
2188         .version                    = cpu_to_be32(version),
2189         .cluster_bits               = cpu_to_be32(cluster_bits),
2190         .size                       = cpu_to_be64(0),
2191         .l1_table_offset            = cpu_to_be64(0),
2192         .l1_size                    = cpu_to_be32(0),
2193         .refcount_table_offset      = cpu_to_be64(cluster_size),
2194         .refcount_table_clusters    = cpu_to_be32(1),
2195         .refcount_order             = cpu_to_be32(refcount_order),
2196         .header_length              = cpu_to_be32(sizeof(*header)),
2197     };
2198 
2199     if (flags & BLOCK_FLAG_ENCRYPT) {
2200         header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
2201     } else {
2202         header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2203     }
2204 
2205     if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
2206         header->compatible_features |=
2207             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2208     }
2209 
2210     ret = bdrv_pwrite(bs, 0, header, cluster_size);
2211     g_free(header);
2212     if (ret < 0) {
2213         error_setg_errno(errp, -ret, "Could not write qcow2 header");
2214         goto out;
2215     }
2216 
2217     /* Write a refcount table with one refcount block */
2218     refcount_table = g_malloc0(2 * cluster_size);
2219     refcount_table[0] = cpu_to_be64(2 * cluster_size);
2220     ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size);
2221     g_free(refcount_table);
2222 
2223     if (ret < 0) {
2224         error_setg_errno(errp, -ret, "Could not write refcount table");
2225         goto out;
2226     }
2227 
2228     bdrv_unref(bs);
2229     bs = NULL;
2230 
2231     /*
2232      * And now open the image and make it consistent first (i.e. increase the
2233      * refcount of the cluster that is occupied by the header and the refcount
2234      * table)
2235      */
2236     options = qdict_new();
2237     qdict_put(options, "driver", qstring_from_str("qcow2"));
2238     ret = bdrv_open(&bs, filename, NULL, options,
2239                     BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
2240                     &local_err);
2241     if (ret < 0) {
2242         error_propagate(errp, local_err);
2243         goto out;
2244     }
2245 
2246     ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
2247     if (ret < 0) {
2248         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2249                          "header and refcount table");
2250         goto out;
2251 
2252     } else if (ret != 0) {
2253         error_report("Huh, first cluster in empty image is already in use?");
2254         abort();
2255     }
2256 
2257     /* Create a full header (including things like feature table) */
2258     ret = qcow2_update_header(bs);
2259     if (ret < 0) {
2260         error_setg_errno(errp, -ret, "Could not update qcow2 header");
2261         goto out;
2262     }
2263 
2264     /* Okay, now that we have a valid image, let's give it the right size */
2265     ret = bdrv_truncate(bs, total_size);
2266     if (ret < 0) {
2267         error_setg_errno(errp, -ret, "Could not resize image");
2268         goto out;
2269     }
2270 
2271     /* Want a backing file? There you go.*/
2272     if (backing_file) {
2273         ret = bdrv_change_backing_file(bs, backing_file, backing_format);
2274         if (ret < 0) {
2275             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
2276                              "with format '%s'", backing_file, backing_format);
2277             goto out;
2278         }
2279     }
2280 
2281     /* And if we're supposed to preallocate metadata, do that now */
2282     if (prealloc != PREALLOC_MODE_OFF) {
2283         BDRVQcow2State *s = bs->opaque;
2284         qemu_co_mutex_lock(&s->lock);
2285         ret = preallocate(bs);
2286         qemu_co_mutex_unlock(&s->lock);
2287         if (ret < 0) {
2288             error_setg_errno(errp, -ret, "Could not preallocate metadata");
2289             goto out;
2290         }
2291     }
2292 
2293     bdrv_unref(bs);
2294     bs = NULL;
2295 
2296     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
2297     options = qdict_new();
2298     qdict_put(options, "driver", qstring_from_str("qcow2"));
2299     ret = bdrv_open(&bs, filename, NULL, options,
2300                     BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
2301                     &local_err);
2302     if (local_err) {
2303         error_propagate(errp, local_err);
2304         goto out;
2305     }
2306 
2307     ret = 0;
2308 out:
2309     if (bs) {
2310         bdrv_unref(bs);
2311     }
2312     return ret;
2313 }
2314 
2315 static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
2316 {
2317     char *backing_file = NULL;
2318     char *backing_fmt = NULL;
2319     char *buf = NULL;
2320     uint64_t size = 0;
2321     int flags = 0;
2322     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
2323     PreallocMode prealloc;
2324     int version = 3;
2325     uint64_t refcount_bits = 16;
2326     int refcount_order;
2327     Error *local_err = NULL;
2328     int ret;
2329 
2330     /* Read out options */
2331     size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2332                     BDRV_SECTOR_SIZE);
2333     backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2334     backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
2335     if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
2336         flags |= BLOCK_FLAG_ENCRYPT;
2337     }
2338     cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2339                                          DEFAULT_CLUSTER_SIZE);
2340     buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
2341     prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
2342                                PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
2343                                &local_err);
2344     if (local_err) {
2345         error_propagate(errp, local_err);
2346         ret = -EINVAL;
2347         goto finish;
2348     }
2349     g_free(buf);
2350     buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2351     if (!buf) {
2352         /* keep the default */
2353     } else if (!strcmp(buf, "0.10")) {
2354         version = 2;
2355     } else if (!strcmp(buf, "1.1")) {
2356         version = 3;
2357     } else {
2358         error_setg(errp, "Invalid compatibility level: '%s'", buf);
2359         ret = -EINVAL;
2360         goto finish;
2361     }
2362 
2363     if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) {
2364         flags |= BLOCK_FLAG_LAZY_REFCOUNTS;
2365     }
2366 
2367     if (backing_file && prealloc != PREALLOC_MODE_OFF) {
2368         error_setg(errp, "Backing file and preallocation cannot be used at "
2369                    "the same time");
2370         ret = -EINVAL;
2371         goto finish;
2372     }
2373 
2374     if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
2375         error_setg(errp, "Lazy refcounts only supported with compatibility "
2376                    "level 1.1 and above (use compat=1.1 or greater)");
2377         ret = -EINVAL;
2378         goto finish;
2379     }
2380 
2381     refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS,
2382                                             refcount_bits);
2383     if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2384         error_setg(errp, "Refcount width must be a power of two and may not "
2385                    "exceed 64 bits");
2386         ret = -EINVAL;
2387         goto finish;
2388     }
2389 
2390     if (version < 3 && refcount_bits != 16) {
2391         error_setg(errp, "Different refcount widths than 16 bits require "
2392                    "compatibility level 1.1 or above (use compat=1.1 or "
2393                    "greater)");
2394         ret = -EINVAL;
2395         goto finish;
2396     }
2397 
2398     refcount_order = ctz32(refcount_bits);
2399 
2400     ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
2401                         cluster_size, prealloc, opts, version, refcount_order,
2402                         &local_err);
2403     if (local_err) {
2404         error_propagate(errp, local_err);
2405     }
2406 
2407 finish:
2408     g_free(backing_file);
2409     g_free(backing_fmt);
2410     g_free(buf);
2411     return ret;
2412 }
2413 
2414 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
2415     int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
2416 {
2417     int ret;
2418     BDRVQcow2State *s = bs->opaque;
2419 
2420     /* Emulate misaligned zero writes */
2421     if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
2422         return -ENOTSUP;
2423     }
2424 
2425     /* Whatever is left can use real zero clusters */
2426     qemu_co_mutex_lock(&s->lock);
2427     ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2428         nb_sectors);
2429     qemu_co_mutex_unlock(&s->lock);
2430 
2431     return ret;
2432 }
2433 
2434 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
2435     int64_t sector_num, int nb_sectors)
2436 {
2437     int ret;
2438     BDRVQcow2State *s = bs->opaque;
2439 
2440     qemu_co_mutex_lock(&s->lock);
2441     ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2442         nb_sectors, QCOW2_DISCARD_REQUEST, false);
2443     qemu_co_mutex_unlock(&s->lock);
2444     return ret;
2445 }
2446 
2447 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
2448 {
2449     BDRVQcow2State *s = bs->opaque;
2450     int64_t new_l1_size;
2451     int ret;
2452 
2453     if (offset & 511) {
2454         error_report("The new size must be a multiple of 512");
2455         return -EINVAL;
2456     }
2457 
2458     /* cannot proceed if image has snapshots */
2459     if (s->nb_snapshots) {
2460         error_report("Can't resize an image which has snapshots");
2461         return -ENOTSUP;
2462     }
2463 
2464     /* shrinking is currently not supported */
2465     if (offset < bs->total_sectors * 512) {
2466         error_report("qcow2 doesn't support shrinking images yet");
2467         return -ENOTSUP;
2468     }
2469 
2470     new_l1_size = size_to_l1(s, offset);
2471     ret = qcow2_grow_l1_table(bs, new_l1_size, true);
2472     if (ret < 0) {
2473         return ret;
2474     }
2475 
2476     /* write updated header.size */
2477     offset = cpu_to_be64(offset);
2478     ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, size),
2479                            &offset, sizeof(uint64_t));
2480     if (ret < 0) {
2481         return ret;
2482     }
2483 
2484     s->l1_vm_state_index = new_l1_size;
2485     return 0;
2486 }
2487 
2488 /* XXX: put compressed sectors first, then all the cluster aligned
2489    tables to avoid losing bytes in alignment */
2490 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
2491                                   const uint8_t *buf, int nb_sectors)
2492 {
2493     BDRVQcow2State *s = bs->opaque;
2494     z_stream strm;
2495     int ret, out_len;
2496     uint8_t *out_buf;
2497     uint64_t cluster_offset;
2498 
2499     if (nb_sectors == 0) {
2500         /* align end of file to a sector boundary to ease reading with
2501            sector based I/Os */
2502         cluster_offset = bdrv_getlength(bs->file->bs);
2503         return bdrv_truncate(bs->file->bs, cluster_offset);
2504     }
2505 
2506     if (nb_sectors != s->cluster_sectors) {
2507         ret = -EINVAL;
2508 
2509         /* Zero-pad last write if image size is not cluster aligned */
2510         if (sector_num + nb_sectors == bs->total_sectors &&
2511             nb_sectors < s->cluster_sectors) {
2512             uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
2513             memset(pad_buf, 0, s->cluster_size);
2514             memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
2515             ret = qcow2_write_compressed(bs, sector_num,
2516                                          pad_buf, s->cluster_sectors);
2517             qemu_vfree(pad_buf);
2518         }
2519         return ret;
2520     }
2521 
2522     out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
2523 
2524     /* best compression, small window, no zlib header */
2525     memset(&strm, 0, sizeof(strm));
2526     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
2527                        Z_DEFLATED, -12,
2528                        9, Z_DEFAULT_STRATEGY);
2529     if (ret != 0) {
2530         ret = -EINVAL;
2531         goto fail;
2532     }
2533 
2534     strm.avail_in = s->cluster_size;
2535     strm.next_in = (uint8_t *)buf;
2536     strm.avail_out = s->cluster_size;
2537     strm.next_out = out_buf;
2538 
2539     ret = deflate(&strm, Z_FINISH);
2540     if (ret != Z_STREAM_END && ret != Z_OK) {
2541         deflateEnd(&strm);
2542         ret = -EINVAL;
2543         goto fail;
2544     }
2545     out_len = strm.next_out - out_buf;
2546 
2547     deflateEnd(&strm);
2548 
2549     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
2550         /* could not compress: write normal cluster */
2551         ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
2552         if (ret < 0) {
2553             goto fail;
2554         }
2555     } else {
2556         cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
2557             sector_num << 9, out_len);
2558         if (!cluster_offset) {
2559             ret = -EIO;
2560             goto fail;
2561         }
2562         cluster_offset &= s->cluster_offset_mask;
2563 
2564         ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
2565         if (ret < 0) {
2566             goto fail;
2567         }
2568 
2569         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
2570         ret = bdrv_pwrite(bs->file->bs, cluster_offset, out_buf, out_len);
2571         if (ret < 0) {
2572             goto fail;
2573         }
2574     }
2575 
2576     ret = 0;
2577 fail:
2578     g_free(out_buf);
2579     return ret;
2580 }
2581 
2582 static int make_completely_empty(BlockDriverState *bs)
2583 {
2584     BDRVQcow2State *s = bs->opaque;
2585     int ret, l1_clusters;
2586     int64_t offset;
2587     uint64_t *new_reftable = NULL;
2588     uint64_t rt_entry, l1_size2;
2589     struct {
2590         uint64_t l1_offset;
2591         uint64_t reftable_offset;
2592         uint32_t reftable_clusters;
2593     } QEMU_PACKED l1_ofs_rt_ofs_cls;
2594 
2595     ret = qcow2_cache_empty(bs, s->l2_table_cache);
2596     if (ret < 0) {
2597         goto fail;
2598     }
2599 
2600     ret = qcow2_cache_empty(bs, s->refcount_block_cache);
2601     if (ret < 0) {
2602         goto fail;
2603     }
2604 
2605     /* Refcounts will be broken utterly */
2606     ret = qcow2_mark_dirty(bs);
2607     if (ret < 0) {
2608         goto fail;
2609     }
2610 
2611     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2612 
2613     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2614     l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
2615 
2616     /* After this call, neither the in-memory nor the on-disk refcount
2617      * information accurately describe the actual references */
2618 
2619     ret = bdrv_write_zeroes(bs->file->bs, s->l1_table_offset / BDRV_SECTOR_SIZE,
2620                             l1_clusters * s->cluster_sectors, 0);
2621     if (ret < 0) {
2622         goto fail_broken_refcounts;
2623     }
2624     memset(s->l1_table, 0, l1_size2);
2625 
2626     BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
2627 
2628     /* Overwrite enough clusters at the beginning of the sectors to place
2629      * the refcount table, a refcount block and the L1 table in; this may
2630      * overwrite parts of the existing refcount and L1 table, which is not
2631      * an issue because the dirty flag is set, complete data loss is in fact
2632      * desired and partial data loss is consequently fine as well */
2633     ret = bdrv_write_zeroes(bs->file->bs, s->cluster_size / BDRV_SECTOR_SIZE,
2634                             (2 + l1_clusters) * s->cluster_size /
2635                             BDRV_SECTOR_SIZE, 0);
2636     /* This call (even if it failed overall) may have overwritten on-disk
2637      * refcount structures; in that case, the in-memory refcount information
2638      * will probably differ from the on-disk information which makes the BDS
2639      * unusable */
2640     if (ret < 0) {
2641         goto fail_broken_refcounts;
2642     }
2643 
2644     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2645     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
2646 
2647     /* "Create" an empty reftable (one cluster) directly after the image
2648      * header and an empty L1 table three clusters after the image header;
2649      * the cluster between those two will be used as the first refblock */
2650     cpu_to_be64w(&l1_ofs_rt_ofs_cls.l1_offset, 3 * s->cluster_size);
2651     cpu_to_be64w(&l1_ofs_rt_ofs_cls.reftable_offset, s->cluster_size);
2652     cpu_to_be32w(&l1_ofs_rt_ofs_cls.reftable_clusters, 1);
2653     ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, l1_table_offset),
2654                            &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
2655     if (ret < 0) {
2656         goto fail_broken_refcounts;
2657     }
2658 
2659     s->l1_table_offset = 3 * s->cluster_size;
2660 
2661     new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
2662     if (!new_reftable) {
2663         ret = -ENOMEM;
2664         goto fail_broken_refcounts;
2665     }
2666 
2667     s->refcount_table_offset = s->cluster_size;
2668     s->refcount_table_size   = s->cluster_size / sizeof(uint64_t);
2669 
2670     g_free(s->refcount_table);
2671     s->refcount_table = new_reftable;
2672     new_reftable = NULL;
2673 
2674     /* Now the in-memory refcount information again corresponds to the on-disk
2675      * information (reftable is empty and no refblocks (the refblock cache is
2676      * empty)); however, this means some clusters (e.g. the image header) are
2677      * referenced, but not refcounted, but the normal qcow2 code assumes that
2678      * the in-memory information is always correct */
2679 
2680     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
2681 
2682     /* Enter the first refblock into the reftable */
2683     rt_entry = cpu_to_be64(2 * s->cluster_size);
2684     ret = bdrv_pwrite_sync(bs->file->bs, s->cluster_size,
2685                            &rt_entry, sizeof(rt_entry));
2686     if (ret < 0) {
2687         goto fail_broken_refcounts;
2688     }
2689     s->refcount_table[0] = 2 * s->cluster_size;
2690 
2691     s->free_cluster_index = 0;
2692     assert(3 + l1_clusters <= s->refcount_block_size);
2693     offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
2694     if (offset < 0) {
2695         ret = offset;
2696         goto fail_broken_refcounts;
2697     } else if (offset > 0) {
2698         error_report("First cluster in emptied image is in use");
2699         abort();
2700     }
2701 
2702     /* Now finally the in-memory information corresponds to the on-disk
2703      * structures and is correct */
2704     ret = qcow2_mark_clean(bs);
2705     if (ret < 0) {
2706         goto fail;
2707     }
2708 
2709     ret = bdrv_truncate(bs->file->bs, (3 + l1_clusters) * s->cluster_size);
2710     if (ret < 0) {
2711         goto fail;
2712     }
2713 
2714     return 0;
2715 
2716 fail_broken_refcounts:
2717     /* The BDS is unusable at this point. If we wanted to make it usable, we
2718      * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2719      * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2720      * again. However, because the functions which could have caused this error
2721      * path to be taken are used by those functions as well, it's very likely
2722      * that that sequence will fail as well. Therefore, just eject the BDS. */
2723     bs->drv = NULL;
2724 
2725 fail:
2726     g_free(new_reftable);
2727     return ret;
2728 }
2729 
2730 static int qcow2_make_empty(BlockDriverState *bs)
2731 {
2732     BDRVQcow2State *s = bs->opaque;
2733     uint64_t start_sector;
2734     int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
2735     int l1_clusters, ret = 0;
2736 
2737     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2738 
2739     if (s->qcow_version >= 3 && !s->snapshots &&
2740         3 + l1_clusters <= s->refcount_block_size) {
2741         /* The following function only works for qcow2 v3 images (it requires
2742          * the dirty flag) and only as long as there are no snapshots (because
2743          * it completely empties the image). Furthermore, the L1 table and three
2744          * additional clusters (image header, refcount table, one refcount
2745          * block) have to fit inside one refcount block. */
2746         return make_completely_empty(bs);
2747     }
2748 
2749     /* This fallback code simply discards every active cluster; this is slow,
2750      * but works in all cases */
2751     for (start_sector = 0; start_sector < bs->total_sectors;
2752          start_sector += sector_step)
2753     {
2754         /* As this function is generally used after committing an external
2755          * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2756          * default action for this kind of discard is to pass the discard,
2757          * which will ideally result in an actually smaller image file, as
2758          * is probably desired. */
2759         ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE,
2760                                      MIN(sector_step,
2761                                          bs->total_sectors - start_sector),
2762                                      QCOW2_DISCARD_SNAPSHOT, true);
2763         if (ret < 0) {
2764             break;
2765         }
2766     }
2767 
2768     return ret;
2769 }
2770 
2771 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
2772 {
2773     BDRVQcow2State *s = bs->opaque;
2774     int ret;
2775 
2776     qemu_co_mutex_lock(&s->lock);
2777     ret = qcow2_cache_flush(bs, s->l2_table_cache);
2778     if (ret < 0) {
2779         qemu_co_mutex_unlock(&s->lock);
2780         return ret;
2781     }
2782 
2783     if (qcow2_need_accurate_refcounts(s)) {
2784         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2785         if (ret < 0) {
2786             qemu_co_mutex_unlock(&s->lock);
2787             return ret;
2788         }
2789     }
2790     qemu_co_mutex_unlock(&s->lock);
2791 
2792     return 0;
2793 }
2794 
2795 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2796 {
2797     BDRVQcow2State *s = bs->opaque;
2798     bdi->unallocated_blocks_are_zero = true;
2799     bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
2800     bdi->cluster_size = s->cluster_size;
2801     bdi->vm_state_offset = qcow2_vm_state_offset(s);
2802     return 0;
2803 }
2804 
2805 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
2806 {
2807     BDRVQcow2State *s = bs->opaque;
2808     ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
2809 
2810     *spec_info = (ImageInfoSpecific){
2811         .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
2812         .u.qcow2 = g_new(ImageInfoSpecificQCow2, 1),
2813     };
2814     if (s->qcow_version == 2) {
2815         *spec_info->u.qcow2 = (ImageInfoSpecificQCow2){
2816             .compat             = g_strdup("0.10"),
2817             .refcount_bits      = s->refcount_bits,
2818         };
2819     } else if (s->qcow_version == 3) {
2820         *spec_info->u.qcow2 = (ImageInfoSpecificQCow2){
2821             .compat             = g_strdup("1.1"),
2822             .lazy_refcounts     = s->compatible_features &
2823                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
2824             .has_lazy_refcounts = true,
2825             .corrupt            = s->incompatible_features &
2826                                   QCOW2_INCOMPAT_CORRUPT,
2827             .has_corrupt        = true,
2828             .refcount_bits      = s->refcount_bits,
2829         };
2830     } else {
2831         /* if this assertion fails, this probably means a new version was
2832          * added without having it covered here */
2833         assert(false);
2834     }
2835 
2836     return spec_info;
2837 }
2838 
2839 #if 0
2840 static void dump_refcounts(BlockDriverState *bs)
2841 {
2842     BDRVQcow2State *s = bs->opaque;
2843     int64_t nb_clusters, k, k1, size;
2844     int refcount;
2845 
2846     size = bdrv_getlength(bs->file->bs);
2847     nb_clusters = size_to_clusters(s, size);
2848     for(k = 0; k < nb_clusters;) {
2849         k1 = k;
2850         refcount = get_refcount(bs, k);
2851         k++;
2852         while (k < nb_clusters && get_refcount(bs, k) == refcount)
2853             k++;
2854         printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
2855                k - k1);
2856     }
2857 }
2858 #endif
2859 
2860 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2861                               int64_t pos)
2862 {
2863     BDRVQcow2State *s = bs->opaque;
2864     int64_t total_sectors = bs->total_sectors;
2865     bool zero_beyond_eof = bs->zero_beyond_eof;
2866     int ret;
2867 
2868     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
2869     bs->zero_beyond_eof = false;
2870     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
2871     bs->zero_beyond_eof = zero_beyond_eof;
2872 
2873     /* bdrv_co_do_writev will have increased the total_sectors value to include
2874      * the VM state - the VM state is however not an actual part of the block
2875      * device, therefore, we need to restore the old value. */
2876     bs->total_sectors = total_sectors;
2877 
2878     return ret;
2879 }
2880 
2881 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2882                               int64_t pos, int size)
2883 {
2884     BDRVQcow2State *s = bs->opaque;
2885     bool zero_beyond_eof = bs->zero_beyond_eof;
2886     int ret;
2887 
2888     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
2889     bs->zero_beyond_eof = false;
2890     ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
2891     bs->zero_beyond_eof = zero_beyond_eof;
2892 
2893     return ret;
2894 }
2895 
2896 /*
2897  * Downgrades an image's version. To achieve this, any incompatible features
2898  * have to be removed.
2899  */
2900 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
2901                            BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
2902 {
2903     BDRVQcow2State *s = bs->opaque;
2904     int current_version = s->qcow_version;
2905     int ret;
2906 
2907     if (target_version == current_version) {
2908         return 0;
2909     } else if (target_version > current_version) {
2910         return -EINVAL;
2911     } else if (target_version != 2) {
2912         return -EINVAL;
2913     }
2914 
2915     if (s->refcount_order != 4) {
2916         error_report("compat=0.10 requires refcount_bits=16");
2917         return -ENOTSUP;
2918     }
2919 
2920     /* clear incompatible features */
2921     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2922         ret = qcow2_mark_clean(bs);
2923         if (ret < 0) {
2924             return ret;
2925         }
2926     }
2927 
2928     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2929      * the first place; if that happens nonetheless, returning -ENOTSUP is the
2930      * best thing to do anyway */
2931 
2932     if (s->incompatible_features) {
2933         return -ENOTSUP;
2934     }
2935 
2936     /* since we can ignore compatible features, we can set them to 0 as well */
2937     s->compatible_features = 0;
2938     /* if lazy refcounts have been used, they have already been fixed through
2939      * clearing the dirty flag */
2940 
2941     /* clearing autoclear features is trivial */
2942     s->autoclear_features = 0;
2943 
2944     ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
2945     if (ret < 0) {
2946         return ret;
2947     }
2948 
2949     s->qcow_version = target_version;
2950     ret = qcow2_update_header(bs);
2951     if (ret < 0) {
2952         s->qcow_version = current_version;
2953         return ret;
2954     }
2955     return 0;
2956 }
2957 
2958 typedef enum Qcow2AmendOperation {
2959     /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
2960      * statically initialized to so that the helper CB can discern the first
2961      * invocation from an operation change */
2962     QCOW2_NO_OPERATION = 0,
2963 
2964     QCOW2_CHANGING_REFCOUNT_ORDER,
2965     QCOW2_DOWNGRADING,
2966 } Qcow2AmendOperation;
2967 
2968 typedef struct Qcow2AmendHelperCBInfo {
2969     /* The code coordinating the amend operations should only modify
2970      * these four fields; the rest will be managed by the CB */
2971     BlockDriverAmendStatusCB *original_status_cb;
2972     void *original_cb_opaque;
2973 
2974     Qcow2AmendOperation current_operation;
2975 
2976     /* Total number of operations to perform (only set once) */
2977     int total_operations;
2978 
2979     /* The following fields are managed by the CB */
2980 
2981     /* Number of operations completed */
2982     int operations_completed;
2983 
2984     /* Cumulative offset of all completed operations */
2985     int64_t offset_completed;
2986 
2987     Qcow2AmendOperation last_operation;
2988     int64_t last_work_size;
2989 } Qcow2AmendHelperCBInfo;
2990 
2991 static void qcow2_amend_helper_cb(BlockDriverState *bs,
2992                                   int64_t operation_offset,
2993                                   int64_t operation_work_size, void *opaque)
2994 {
2995     Qcow2AmendHelperCBInfo *info = opaque;
2996     int64_t current_work_size;
2997     int64_t projected_work_size;
2998 
2999     if (info->current_operation != info->last_operation) {
3000         if (info->last_operation != QCOW2_NO_OPERATION) {
3001             info->offset_completed += info->last_work_size;
3002             info->operations_completed++;
3003         }
3004 
3005         info->last_operation = info->current_operation;
3006     }
3007 
3008     assert(info->total_operations > 0);
3009     assert(info->operations_completed < info->total_operations);
3010 
3011     info->last_work_size = operation_work_size;
3012 
3013     current_work_size = info->offset_completed + operation_work_size;
3014 
3015     /* current_work_size is the total work size for (operations_completed + 1)
3016      * operations (which includes this one), so multiply it by the number of
3017      * operations not covered and divide it by the number of operations
3018      * covered to get a projection for the operations not covered */
3019     projected_work_size = current_work_size * (info->total_operations -
3020                                                info->operations_completed - 1)
3021                                             / (info->operations_completed + 1);
3022 
3023     info->original_status_cb(bs, info->offset_completed + operation_offset,
3024                              current_work_size + projected_work_size,
3025                              info->original_cb_opaque);
3026 }
3027 
3028 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
3029                                BlockDriverAmendStatusCB *status_cb,
3030                                void *cb_opaque)
3031 {
3032     BDRVQcow2State *s = bs->opaque;
3033     int old_version = s->qcow_version, new_version = old_version;
3034     uint64_t new_size = 0;
3035     const char *backing_file = NULL, *backing_format = NULL;
3036     bool lazy_refcounts = s->use_lazy_refcounts;
3037     const char *compat = NULL;
3038     uint64_t cluster_size = s->cluster_size;
3039     bool encrypt;
3040     int refcount_bits = s->refcount_bits;
3041     int ret;
3042     QemuOptDesc *desc = opts->list->desc;
3043     Qcow2AmendHelperCBInfo helper_cb_info;
3044 
3045     while (desc && desc->name) {
3046         if (!qemu_opt_find(opts, desc->name)) {
3047             /* only change explicitly defined options */
3048             desc++;
3049             continue;
3050         }
3051 
3052         if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
3053             compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
3054             if (!compat) {
3055                 /* preserve default */
3056             } else if (!strcmp(compat, "0.10")) {
3057                 new_version = 2;
3058             } else if (!strcmp(compat, "1.1")) {
3059                 new_version = 3;
3060             } else {
3061                 error_report("Unknown compatibility level %s", compat);
3062                 return -EINVAL;
3063             }
3064         } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
3065             error_report("Cannot change preallocation mode");
3066             return -ENOTSUP;
3067         } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
3068             new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3069         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
3070             backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3071         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
3072             backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
3073         } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
3074             encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
3075                                         !!s->cipher);
3076 
3077             if (encrypt != !!s->cipher) {
3078                 error_report("Changing the encryption flag is not supported");
3079                 return -ENOTSUP;
3080             }
3081         } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
3082             cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
3083                                              cluster_size);
3084             if (cluster_size != s->cluster_size) {
3085                 error_report("Changing the cluster size is not supported");
3086                 return -ENOTSUP;
3087             }
3088         } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
3089             lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
3090                                                lazy_refcounts);
3091         } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
3092             refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
3093                                                 refcount_bits);
3094 
3095             if (refcount_bits <= 0 || refcount_bits > 64 ||
3096                 !is_power_of_2(refcount_bits))
3097             {
3098                 error_report("Refcount width must be a power of two and may "
3099                              "not exceed 64 bits");
3100                 return -EINVAL;
3101             }
3102         } else {
3103             /* if this point is reached, this probably means a new option was
3104              * added without having it covered here */
3105             abort();
3106         }
3107 
3108         desc++;
3109     }
3110 
3111     helper_cb_info = (Qcow2AmendHelperCBInfo){
3112         .original_status_cb = status_cb,
3113         .original_cb_opaque = cb_opaque,
3114         .total_operations = (new_version < old_version)
3115                           + (s->refcount_bits != refcount_bits)
3116     };
3117 
3118     /* Upgrade first (some features may require compat=1.1) */
3119     if (new_version > old_version) {
3120         s->qcow_version = new_version;
3121         ret = qcow2_update_header(bs);
3122         if (ret < 0) {
3123             s->qcow_version = old_version;
3124             return ret;
3125         }
3126     }
3127 
3128     if (s->refcount_bits != refcount_bits) {
3129         int refcount_order = ctz32(refcount_bits);
3130         Error *local_error = NULL;
3131 
3132         if (new_version < 3 && refcount_bits != 16) {
3133             error_report("Different refcount widths than 16 bits require "
3134                          "compatibility level 1.1 or above (use compat=1.1 or "
3135                          "greater)");
3136             return -EINVAL;
3137         }
3138 
3139         helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
3140         ret = qcow2_change_refcount_order(bs, refcount_order,
3141                                           &qcow2_amend_helper_cb,
3142                                           &helper_cb_info, &local_error);
3143         if (ret < 0) {
3144             error_report_err(local_error);
3145             return ret;
3146         }
3147     }
3148 
3149     if (backing_file || backing_format) {
3150         ret = qcow2_change_backing_file(bs,
3151                     backing_file ?: s->image_backing_file,
3152                     backing_format ?: s->image_backing_format);
3153         if (ret < 0) {
3154             return ret;
3155         }
3156     }
3157 
3158     if (s->use_lazy_refcounts != lazy_refcounts) {
3159         if (lazy_refcounts) {
3160             if (new_version < 3) {
3161                 error_report("Lazy refcounts only supported with compatibility "
3162                              "level 1.1 and above (use compat=1.1 or greater)");
3163                 return -EINVAL;
3164             }
3165             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
3166             ret = qcow2_update_header(bs);
3167             if (ret < 0) {
3168                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
3169                 return ret;
3170             }
3171             s->use_lazy_refcounts = true;
3172         } else {
3173             /* make image clean first */
3174             ret = qcow2_mark_clean(bs);
3175             if (ret < 0) {
3176                 return ret;
3177             }
3178             /* now disallow lazy refcounts */
3179             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
3180             ret = qcow2_update_header(bs);
3181             if (ret < 0) {
3182                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
3183                 return ret;
3184             }
3185             s->use_lazy_refcounts = false;
3186         }
3187     }
3188 
3189     if (new_size) {
3190         ret = bdrv_truncate(bs, new_size);
3191         if (ret < 0) {
3192             return ret;
3193         }
3194     }
3195 
3196     /* Downgrade last (so unsupported features can be removed before) */
3197     if (new_version < old_version) {
3198         helper_cb_info.current_operation = QCOW2_DOWNGRADING;
3199         ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
3200                               &helper_cb_info);
3201         if (ret < 0) {
3202             return ret;
3203         }
3204     }
3205 
3206     return 0;
3207 }
3208 
3209 /*
3210  * If offset or size are negative, respectively, they will not be included in
3211  * the BLOCK_IMAGE_CORRUPTED event emitted.
3212  * fatal will be ignored for read-only BDS; corruptions found there will always
3213  * be considered non-fatal.
3214  */
3215 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
3216                              int64_t size, const char *message_format, ...)
3217 {
3218     BDRVQcow2State *s = bs->opaque;
3219     const char *node_name;
3220     char *message;
3221     va_list ap;
3222 
3223     fatal = fatal && !bs->read_only;
3224 
3225     if (s->signaled_corruption &&
3226         (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
3227     {
3228         return;
3229     }
3230 
3231     va_start(ap, message_format);
3232     message = g_strdup_vprintf(message_format, ap);
3233     va_end(ap);
3234 
3235     if (fatal) {
3236         fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
3237                 "corruption events will be suppressed\n", message);
3238     } else {
3239         fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
3240                 "corruption events will be suppressed\n", message);
3241     }
3242 
3243     node_name = bdrv_get_node_name(bs);
3244     qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
3245                                           *node_name != '\0', node_name,
3246                                           message, offset >= 0, offset,
3247                                           size >= 0, size,
3248                                           fatal, &error_abort);
3249     g_free(message);
3250 
3251     if (fatal) {
3252         qcow2_mark_corrupt(bs);
3253         bs->drv = NULL; /* make BDS unusable */
3254     }
3255 
3256     s->signaled_corruption = true;
3257 }
3258 
3259 static QemuOptsList qcow2_create_opts = {
3260     .name = "qcow2-create-opts",
3261     .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
3262     .desc = {
3263         {
3264             .name = BLOCK_OPT_SIZE,
3265             .type = QEMU_OPT_SIZE,
3266             .help = "Virtual disk size"
3267         },
3268         {
3269             .name = BLOCK_OPT_COMPAT_LEVEL,
3270             .type = QEMU_OPT_STRING,
3271             .help = "Compatibility level (0.10 or 1.1)"
3272         },
3273         {
3274             .name = BLOCK_OPT_BACKING_FILE,
3275             .type = QEMU_OPT_STRING,
3276             .help = "File name of a base image"
3277         },
3278         {
3279             .name = BLOCK_OPT_BACKING_FMT,
3280             .type = QEMU_OPT_STRING,
3281             .help = "Image format of the base image"
3282         },
3283         {
3284             .name = BLOCK_OPT_ENCRYPT,
3285             .type = QEMU_OPT_BOOL,
3286             .help = "Encrypt the image",
3287             .def_value_str = "off"
3288         },
3289         {
3290             .name = BLOCK_OPT_CLUSTER_SIZE,
3291             .type = QEMU_OPT_SIZE,
3292             .help = "qcow2 cluster size",
3293             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
3294         },
3295         {
3296             .name = BLOCK_OPT_PREALLOC,
3297             .type = QEMU_OPT_STRING,
3298             .help = "Preallocation mode (allowed values: off, metadata, "
3299                     "falloc, full)"
3300         },
3301         {
3302             .name = BLOCK_OPT_LAZY_REFCOUNTS,
3303             .type = QEMU_OPT_BOOL,
3304             .help = "Postpone refcount updates",
3305             .def_value_str = "off"
3306         },
3307         {
3308             .name = BLOCK_OPT_REFCOUNT_BITS,
3309             .type = QEMU_OPT_NUMBER,
3310             .help = "Width of a reference count entry in bits",
3311             .def_value_str = "16"
3312         },
3313         { /* end of list */ }
3314     }
3315 };
3316 
3317 BlockDriver bdrv_qcow2 = {
3318     .format_name        = "qcow2",
3319     .instance_size      = sizeof(BDRVQcow2State),
3320     .bdrv_probe         = qcow2_probe,
3321     .bdrv_open          = qcow2_open,
3322     .bdrv_close         = qcow2_close,
3323     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
3324     .bdrv_reopen_commit   = qcow2_reopen_commit,
3325     .bdrv_reopen_abort    = qcow2_reopen_abort,
3326     .bdrv_join_options    = qcow2_join_options,
3327     .bdrv_create        = qcow2_create,
3328     .bdrv_has_zero_init = bdrv_has_zero_init_1,
3329     .bdrv_co_get_block_status = qcow2_co_get_block_status,
3330     .bdrv_set_key       = qcow2_set_key,
3331 
3332     .bdrv_co_readv          = qcow2_co_readv,
3333     .bdrv_co_writev         = qcow2_co_writev,
3334     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
3335 
3336     .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
3337     .bdrv_co_discard        = qcow2_co_discard,
3338     .bdrv_truncate          = qcow2_truncate,
3339     .bdrv_write_compressed  = qcow2_write_compressed,
3340     .bdrv_make_empty        = qcow2_make_empty,
3341 
3342     .bdrv_snapshot_create   = qcow2_snapshot_create,
3343     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
3344     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
3345     .bdrv_snapshot_list     = qcow2_snapshot_list,
3346     .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
3347     .bdrv_get_info          = qcow2_get_info,
3348     .bdrv_get_specific_info = qcow2_get_specific_info,
3349 
3350     .bdrv_save_vmstate    = qcow2_save_vmstate,
3351     .bdrv_load_vmstate    = qcow2_load_vmstate,
3352 
3353     .supports_backing           = true,
3354     .bdrv_change_backing_file   = qcow2_change_backing_file,
3355 
3356     .bdrv_refresh_limits        = qcow2_refresh_limits,
3357     .bdrv_invalidate_cache      = qcow2_invalidate_cache,
3358     .bdrv_inactivate            = qcow2_inactivate,
3359 
3360     .create_opts         = &qcow2_create_opts,
3361     .bdrv_check          = qcow2_check,
3362     .bdrv_amend_options  = qcow2_amend_options,
3363 
3364     .bdrv_detach_aio_context  = qcow2_detach_aio_context,
3365     .bdrv_attach_aio_context  = qcow2_attach_aio_context,
3366 };
3367 
3368 static void bdrv_qcow2_init(void)
3369 {
3370     bdrv_register(&bdrv_qcow2);
3371 }
3372 
3373 block_init(bdrv_qcow2_init);
3374