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