]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - block/qcow2.c
qcow2: Check backing_file_offset (CVE-2014-0144)
[lisovros/qemu_apohw.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 "qemu/aes.h"
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
33 #include "trace.h"
34
35 /*
36   Differences with QCOW:
37
38   - Support for multiple incremental snapshots.
39   - Memory management by reference counts.
40   - Clusters which have a reference count of one have the bit
41     QCOW_OFLAG_COPIED to optimize write performance.
42   - Size of compressed clusters is stored in sectors to reduce bit usage
43     in the cluster offsets.
44   - Support for storing additional data (such as the VM state) in the
45     snapshots.
46   - If a backing store is used, the cluster size is not constrained
47     (could be backported to QCOW).
48   - L2 tables have always a size of one cluster.
49 */
50
51
52 typedef struct {
53     uint32_t magic;
54     uint32_t len;
55 } QEMU_PACKED QCowExtension;
56
57 #define  QCOW2_EXT_MAGIC_END 0
58 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
59 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
60
61 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
62 {
63     const QCowHeader *cow_header = (const void *)buf;
64
65     if (buf_size >= sizeof(QCowHeader) &&
66         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
67         be32_to_cpu(cow_header->version) >= 2)
68         return 100;
69     else
70         return 0;
71 }
72
73
74 /* 
75  * read qcow2 extension and fill bs
76  * start reading from start_offset
77  * finish reading upon magic of value 0 or when end_offset reached
78  * unknown magic is skipped (future extension this version knows nothing about)
79  * return 0 upon success, non-0 otherwise
80  */
81 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
82                                  uint64_t end_offset, void **p_feature_table,
83                                  Error **errp)
84 {
85     BDRVQcowState *s = bs->opaque;
86     QCowExtension ext;
87     uint64_t offset;
88     int ret;
89
90 #ifdef DEBUG_EXT
91     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
92 #endif
93     offset = start_offset;
94     while (offset < end_offset) {
95
96 #ifdef DEBUG_EXT
97         /* Sanity check */
98         if (offset > s->cluster_size)
99             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
100
101         printf("attempting to read extended header in offset %lu\n", offset);
102 #endif
103
104         ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
105         if (ret < 0) {
106             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
107                              "pread fail from offset %" PRIu64, offset);
108             return 1;
109         }
110         be32_to_cpus(&ext.magic);
111         be32_to_cpus(&ext.len);
112         offset += sizeof(ext);
113 #ifdef DEBUG_EXT
114         printf("ext.magic = 0x%x\n", ext.magic);
115 #endif
116         if (ext.len > end_offset - offset) {
117             error_setg(errp, "Header extension too large");
118             return -EINVAL;
119         }
120
121         switch (ext.magic) {
122         case QCOW2_EXT_MAGIC_END:
123             return 0;
124
125         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
126             if (ext.len >= sizeof(bs->backing_format)) {
127                 error_setg(errp, "ERROR: ext_backing_format: len=%u too large"
128                            " (>=%zu)", ext.len, sizeof(bs->backing_format));
129                 return 2;
130             }
131             ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
132             if (ret < 0) {
133                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
134                                  "Could not read format name");
135                 return 3;
136             }
137             bs->backing_format[ext.len] = '\0';
138 #ifdef DEBUG_EXT
139             printf("Qcow2: Got format extension %s\n", bs->backing_format);
140 #endif
141             break;
142
143         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
144             if (p_feature_table != NULL) {
145                 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
146                 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
147                 if (ret < 0) {
148                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
149                                      "Could not read table");
150                     return ret;
151                 }
152
153                 *p_feature_table = feature_table;
154             }
155             break;
156
157         default:
158             /* unknown magic - save it in case we need to rewrite the header */
159             {
160                 Qcow2UnknownHeaderExtension *uext;
161
162                 uext = g_malloc0(sizeof(*uext)  + ext.len);
163                 uext->magic = ext.magic;
164                 uext->len = ext.len;
165                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
166
167                 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
168                 if (ret < 0) {
169                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
170                                      "Could not read data");
171                     return ret;
172                 }
173             }
174             break;
175         }
176
177         offset += ((ext.len + 7) & ~7);
178     }
179
180     return 0;
181 }
182
183 static void cleanup_unknown_header_ext(BlockDriverState *bs)
184 {
185     BDRVQcowState *s = bs->opaque;
186     Qcow2UnknownHeaderExtension *uext, *next;
187
188     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
189         QLIST_REMOVE(uext, next);
190         g_free(uext);
191     }
192 }
193
194 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
195     Error **errp, const char *fmt, ...)
196 {
197     char msg[64];
198     va_list ap;
199
200     va_start(ap, fmt);
201     vsnprintf(msg, sizeof(msg), fmt, ap);
202     va_end(ap);
203
204     error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, bs->device_name, "qcow2",
205               msg);
206 }
207
208 static void report_unsupported_feature(BlockDriverState *bs,
209     Error **errp, Qcow2Feature *table, uint64_t mask)
210 {
211     while (table && table->name[0] != '\0') {
212         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
213             if (mask & (1 << table->bit)) {
214                 report_unsupported(bs, errp, "%.46s", table->name);
215                 mask &= ~(1 << table->bit);
216             }
217         }
218         table++;
219     }
220
221     if (mask) {
222         report_unsupported(bs, errp, "Unknown incompatible feature: %" PRIx64,
223                            mask);
224     }
225 }
226
227 /*
228  * Sets the dirty bit and flushes afterwards if necessary.
229  *
230  * The incompatible_features bit is only set if the image file header was
231  * updated successfully.  Therefore it is not required to check the return
232  * value of this function.
233  */
234 int qcow2_mark_dirty(BlockDriverState *bs)
235 {
236     BDRVQcowState *s = bs->opaque;
237     uint64_t val;
238     int ret;
239
240     assert(s->qcow_version >= 3);
241
242     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
243         return 0; /* already dirty */
244     }
245
246     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
247     ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
248                       &val, sizeof(val));
249     if (ret < 0) {
250         return ret;
251     }
252     ret = bdrv_flush(bs->file);
253     if (ret < 0) {
254         return ret;
255     }
256
257     /* Only treat image as dirty if the header was updated successfully */
258     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
259     return 0;
260 }
261
262 /*
263  * Clears the dirty bit and flushes before if necessary.  Only call this
264  * function when there are no pending requests, it does not guard against
265  * concurrent requests dirtying the image.
266  */
267 static int qcow2_mark_clean(BlockDriverState *bs)
268 {
269     BDRVQcowState *s = bs->opaque;
270
271     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
272         int ret = bdrv_flush(bs);
273         if (ret < 0) {
274             return ret;
275         }
276
277         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
278         return qcow2_update_header(bs);
279     }
280     return 0;
281 }
282
283 /*
284  * Marks the image as corrupt.
285  */
286 int qcow2_mark_corrupt(BlockDriverState *bs)
287 {
288     BDRVQcowState *s = bs->opaque;
289
290     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
291     return qcow2_update_header(bs);
292 }
293
294 /*
295  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
296  * before if necessary.
297  */
298 int qcow2_mark_consistent(BlockDriverState *bs)
299 {
300     BDRVQcowState *s = bs->opaque;
301
302     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
303         int ret = bdrv_flush(bs);
304         if (ret < 0) {
305             return ret;
306         }
307
308         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
309         return qcow2_update_header(bs);
310     }
311     return 0;
312 }
313
314 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
315                        BdrvCheckMode fix)
316 {
317     int ret = qcow2_check_refcounts(bs, result, fix);
318     if (ret < 0) {
319         return ret;
320     }
321
322     if (fix && result->check_errors == 0 && result->corruptions == 0) {
323         ret = qcow2_mark_clean(bs);
324         if (ret < 0) {
325             return ret;
326         }
327         return qcow2_mark_consistent(bs);
328     }
329     return ret;
330 }
331
332 static QemuOptsList qcow2_runtime_opts = {
333     .name = "qcow2",
334     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
335     .desc = {
336         {
337             .name = QCOW2_OPT_LAZY_REFCOUNTS,
338             .type = QEMU_OPT_BOOL,
339             .help = "Postpone refcount updates",
340         },
341         {
342             .name = QCOW2_OPT_DISCARD_REQUEST,
343             .type = QEMU_OPT_BOOL,
344             .help = "Pass guest discard requests to the layer below",
345         },
346         {
347             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
348             .type = QEMU_OPT_BOOL,
349             .help = "Generate discard requests when snapshot related space "
350                     "is freed",
351         },
352         {
353             .name = QCOW2_OPT_DISCARD_OTHER,
354             .type = QEMU_OPT_BOOL,
355             .help = "Generate discard requests when other clusters are freed",
356         },
357         {
358             .name = QCOW2_OPT_OVERLAP,
359             .type = QEMU_OPT_STRING,
360             .help = "Selects which overlap checks to perform from a range of "
361                     "templates (none, constant, cached, all)",
362         },
363         {
364             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
365             .type = QEMU_OPT_BOOL,
366             .help = "Check for unintended writes into the main qcow2 header",
367         },
368         {
369             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
370             .type = QEMU_OPT_BOOL,
371             .help = "Check for unintended writes into the active L1 table",
372         },
373         {
374             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
375             .type = QEMU_OPT_BOOL,
376             .help = "Check for unintended writes into an active L2 table",
377         },
378         {
379             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
380             .type = QEMU_OPT_BOOL,
381             .help = "Check for unintended writes into the refcount table",
382         },
383         {
384             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
385             .type = QEMU_OPT_BOOL,
386             .help = "Check for unintended writes into a refcount block",
387         },
388         {
389             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
390             .type = QEMU_OPT_BOOL,
391             .help = "Check for unintended writes into the snapshot table",
392         },
393         {
394             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
395             .type = QEMU_OPT_BOOL,
396             .help = "Check for unintended writes into an inactive L1 table",
397         },
398         {
399             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
400             .type = QEMU_OPT_BOOL,
401             .help = "Check for unintended writes into an inactive L2 table",
402         },
403         { /* end of list */ }
404     },
405 };
406
407 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
408     [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
409     [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
410     [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
411     [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
412     [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
413     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
414     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
415     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
416 };
417
418 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
419                       Error **errp)
420 {
421     BDRVQcowState *s = bs->opaque;
422     int len, i, ret = 0;
423     QCowHeader header;
424     QemuOpts *opts;
425     Error *local_err = NULL;
426     uint64_t ext_end;
427     uint64_t l1_vm_state_index;
428     const char *opt_overlap_check;
429     int overlap_check_template = 0;
430
431     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
432     if (ret < 0) {
433         error_setg_errno(errp, -ret, "Could not read qcow2 header");
434         goto fail;
435     }
436     be32_to_cpus(&header.magic);
437     be32_to_cpus(&header.version);
438     be64_to_cpus(&header.backing_file_offset);
439     be32_to_cpus(&header.backing_file_size);
440     be64_to_cpus(&header.size);
441     be32_to_cpus(&header.cluster_bits);
442     be32_to_cpus(&header.crypt_method);
443     be64_to_cpus(&header.l1_table_offset);
444     be32_to_cpus(&header.l1_size);
445     be64_to_cpus(&header.refcount_table_offset);
446     be32_to_cpus(&header.refcount_table_clusters);
447     be64_to_cpus(&header.snapshots_offset);
448     be32_to_cpus(&header.nb_snapshots);
449
450     if (header.magic != QCOW_MAGIC) {
451         error_setg(errp, "Image is not in qcow2 format");
452         ret = -EINVAL;
453         goto fail;
454     }
455     if (header.version < 2 || header.version > 3) {
456         report_unsupported(bs, errp, "QCOW version %d", header.version);
457         ret = -ENOTSUP;
458         goto fail;
459     }
460
461     s->qcow_version = header.version;
462
463     /* Initialise cluster size */
464     if (header.cluster_bits < MIN_CLUSTER_BITS ||
465         header.cluster_bits > MAX_CLUSTER_BITS) {
466         error_setg(errp, "Unsupported cluster size: 2^%i", header.cluster_bits);
467         ret = -EINVAL;
468         goto fail;
469     }
470
471     s->cluster_bits = header.cluster_bits;
472     s->cluster_size = 1 << s->cluster_bits;
473     s->cluster_sectors = 1 << (s->cluster_bits - 9);
474
475     /* Initialise version 3 header fields */
476     if (header.version == 2) {
477         header.incompatible_features    = 0;
478         header.compatible_features      = 0;
479         header.autoclear_features       = 0;
480         header.refcount_order           = 4;
481         header.header_length            = 72;
482     } else {
483         be64_to_cpus(&header.incompatible_features);
484         be64_to_cpus(&header.compatible_features);
485         be64_to_cpus(&header.autoclear_features);
486         be32_to_cpus(&header.refcount_order);
487         be32_to_cpus(&header.header_length);
488
489         if (header.header_length < 104) {
490             error_setg(errp, "qcow2 header too short");
491             ret = -EINVAL;
492             goto fail;
493         }
494     }
495
496     if (header.header_length > s->cluster_size) {
497         error_setg(errp, "qcow2 header exceeds cluster size");
498         ret = -EINVAL;
499         goto fail;
500     }
501
502     if (header.header_length > sizeof(header)) {
503         s->unknown_header_fields_size = header.header_length - sizeof(header);
504         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
505         ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
506                          s->unknown_header_fields_size);
507         if (ret < 0) {
508             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
509                              "fields");
510             goto fail;
511         }
512     }
513
514     if (header.backing_file_offset > s->cluster_size) {
515         error_setg(errp, "Invalid backing file offset");
516         ret = -EINVAL;
517         goto fail;
518     }
519
520     if (header.backing_file_offset) {
521         ext_end = header.backing_file_offset;
522     } else {
523         ext_end = 1 << header.cluster_bits;
524     }
525
526     /* Handle feature bits */
527     s->incompatible_features    = header.incompatible_features;
528     s->compatible_features      = header.compatible_features;
529     s->autoclear_features       = header.autoclear_features;
530
531     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
532         void *feature_table = NULL;
533         qcow2_read_extensions(bs, header.header_length, ext_end,
534                               &feature_table, NULL);
535         report_unsupported_feature(bs, errp, feature_table,
536                                    s->incompatible_features &
537                                    ~QCOW2_INCOMPAT_MASK);
538         ret = -ENOTSUP;
539         g_free(feature_table);
540         goto fail;
541     }
542
543     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
544         /* Corrupt images may not be written to unless they are being repaired
545          */
546         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
547             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
548                        "read/write");
549             ret = -EACCES;
550             goto fail;
551         }
552     }
553
554     /* Check support for various header values */
555     if (header.refcount_order != 4) {
556         report_unsupported(bs, errp, "%d bit reference counts",
557                            1 << header.refcount_order);
558         ret = -ENOTSUP;
559         goto fail;
560     }
561     s->refcount_order = header.refcount_order;
562
563     if (header.crypt_method > QCOW_CRYPT_AES) {
564         error_setg(errp, "Unsupported encryption method: %i",
565                    header.crypt_method);
566         ret = -EINVAL;
567         goto fail;
568     }
569     s->crypt_method_header = header.crypt_method;
570     if (s->crypt_method_header) {
571         bs->encrypted = 1;
572     }
573
574     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
575     s->l2_size = 1 << s->l2_bits;
576     bs->total_sectors = header.size / 512;
577     s->csize_shift = (62 - (s->cluster_bits - 8));
578     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
579     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
580     s->refcount_table_offset = header.refcount_table_offset;
581     s->refcount_table_size =
582         header.refcount_table_clusters << (s->cluster_bits - 3);
583
584     s->snapshots_offset = header.snapshots_offset;
585     s->nb_snapshots = header.nb_snapshots;
586
587     /* read the level 1 table */
588     s->l1_size = header.l1_size;
589
590     l1_vm_state_index = size_to_l1(s, header.size);
591     if (l1_vm_state_index > INT_MAX) {
592         error_setg(errp, "Image is too big");
593         ret = -EFBIG;
594         goto fail;
595     }
596     s->l1_vm_state_index = l1_vm_state_index;
597
598     /* the L1 table must contain at least enough entries to put
599        header.size bytes */
600     if (s->l1_size < s->l1_vm_state_index) {
601         error_setg(errp, "L1 table is too small");
602         ret = -EINVAL;
603         goto fail;
604     }
605     s->l1_table_offset = header.l1_table_offset;
606     if (s->l1_size > 0) {
607         s->l1_table = g_malloc0(
608             align_offset(s->l1_size * sizeof(uint64_t), 512));
609         ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
610                          s->l1_size * sizeof(uint64_t));
611         if (ret < 0) {
612             error_setg_errno(errp, -ret, "Could not read L1 table");
613             goto fail;
614         }
615         for(i = 0;i < s->l1_size; i++) {
616             be64_to_cpus(&s->l1_table[i]);
617         }
618     }
619
620     /* alloc L2 table/refcount block cache */
621     s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
622     s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
623
624     s->cluster_cache = g_malloc(s->cluster_size);
625     /* one more sector for decompressed data alignment */
626     s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
627                                   + 512);
628     s->cluster_cache_offset = -1;
629     s->flags = flags;
630
631     ret = qcow2_refcount_init(bs);
632     if (ret != 0) {
633         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
634         goto fail;
635     }
636
637     QLIST_INIT(&s->cluster_allocs);
638     QTAILQ_INIT(&s->discards);
639
640     /* read qcow2 extensions */
641     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
642         &local_err)) {
643         error_propagate(errp, local_err);
644         ret = -EINVAL;
645         goto fail;
646     }
647
648     /* read the backing file name */
649     if (header.backing_file_offset != 0) {
650         len = header.backing_file_size;
651         if (len > 1023) {
652             len = 1023;
653         }
654         ret = bdrv_pread(bs->file, header.backing_file_offset,
655                          bs->backing_file, len);
656         if (ret < 0) {
657             error_setg_errno(errp, -ret, "Could not read backing file name");
658             goto fail;
659         }
660         bs->backing_file[len] = '\0';
661     }
662
663     ret = qcow2_read_snapshots(bs);
664     if (ret < 0) {
665         error_setg_errno(errp, -ret, "Could not read snapshots");
666         goto fail;
667     }
668
669     /* Clear unknown autoclear feature bits */
670     if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
671         s->autoclear_features = 0;
672         ret = qcow2_update_header(bs);
673         if (ret < 0) {
674             error_setg_errno(errp, -ret, "Could not update qcow2 header");
675             goto fail;
676         }
677     }
678
679     /* Initialise locks */
680     qemu_co_mutex_init(&s->lock);
681
682     /* Repair image if dirty */
683     if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only &&
684         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
685         BdrvCheckResult result = {0};
686
687         ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS);
688         if (ret < 0) {
689             error_setg_errno(errp, -ret, "Could not repair dirty image");
690             goto fail;
691         }
692     }
693
694     /* Enable lazy_refcounts according to image and command line options */
695     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
696     qemu_opts_absorb_qdict(opts, options, &local_err);
697     if (local_err) {
698         error_propagate(errp, local_err);
699         ret = -EINVAL;
700         goto fail;
701     }
702
703     s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
704         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
705
706     s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
707     s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
708     s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
709         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
710                           flags & BDRV_O_UNMAP);
711     s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
712         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
713     s->discard_passthrough[QCOW2_DISCARD_OTHER] =
714         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
715
716     opt_overlap_check = qemu_opt_get(opts, "overlap-check") ?: "cached";
717     if (!strcmp(opt_overlap_check, "none")) {
718         overlap_check_template = 0;
719     } else if (!strcmp(opt_overlap_check, "constant")) {
720         overlap_check_template = QCOW2_OL_CONSTANT;
721     } else if (!strcmp(opt_overlap_check, "cached")) {
722         overlap_check_template = QCOW2_OL_CACHED;
723     } else if (!strcmp(opt_overlap_check, "all")) {
724         overlap_check_template = QCOW2_OL_ALL;
725     } else {
726         error_setg(errp, "Unsupported value '%s' for qcow2 option "
727                    "'overlap-check'. Allowed are either of the following: "
728                    "none, constant, cached, all", opt_overlap_check);
729         qemu_opts_del(opts);
730         ret = -EINVAL;
731         goto fail;
732     }
733
734     s->overlap_check = 0;
735     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
736         /* overlap-check defines a template bitmask, but every flag may be
737          * overwritten through the associated boolean option */
738         s->overlap_check |=
739             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
740                               overlap_check_template & (1 << i)) << i;
741     }
742
743     qemu_opts_del(opts);
744
745     if (s->use_lazy_refcounts && s->qcow_version < 3) {
746         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
747                    "qemu 1.1 compatibility level");
748         ret = -EINVAL;
749         goto fail;
750     }
751
752 #ifdef DEBUG_ALLOC
753     {
754         BdrvCheckResult result = {0};
755         qcow2_check_refcounts(bs, &result, 0);
756     }
757 #endif
758     return ret;
759
760  fail:
761     g_free(s->unknown_header_fields);
762     cleanup_unknown_header_ext(bs);
763     qcow2_free_snapshots(bs);
764     qcow2_refcount_close(bs);
765     g_free(s->l1_table);
766     /* else pre-write overlap checks in cache_destroy may crash */
767     s->l1_table = NULL;
768     if (s->l2_table_cache) {
769         qcow2_cache_destroy(bs, s->l2_table_cache);
770     }
771     if (s->refcount_block_cache) {
772         qcow2_cache_destroy(bs, s->refcount_block_cache);
773     }
774     g_free(s->cluster_cache);
775     qemu_vfree(s->cluster_data);
776     return ret;
777 }
778
779 static int qcow2_refresh_limits(BlockDriverState *bs)
780 {
781     BDRVQcowState *s = bs->opaque;
782
783     bs->bl.write_zeroes_alignment = s->cluster_sectors;
784
785     return 0;
786 }
787
788 static int qcow2_set_key(BlockDriverState *bs, const char *key)
789 {
790     BDRVQcowState *s = bs->opaque;
791     uint8_t keybuf[16];
792     int len, i;
793
794     memset(keybuf, 0, 16);
795     len = strlen(key);
796     if (len > 16)
797         len = 16;
798     /* XXX: we could compress the chars to 7 bits to increase
799        entropy */
800     for(i = 0;i < len;i++) {
801         keybuf[i] = key[i];
802     }
803     s->crypt_method = s->crypt_method_header;
804
805     if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
806         return -1;
807     if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
808         return -1;
809 #if 0
810     /* test */
811     {
812         uint8_t in[16];
813         uint8_t out[16];
814         uint8_t tmp[16];
815         for(i=0;i<16;i++)
816             in[i] = i;
817         AES_encrypt(in, tmp, &s->aes_encrypt_key);
818         AES_decrypt(tmp, out, &s->aes_decrypt_key);
819         for(i = 0; i < 16; i++)
820             printf(" %02x", tmp[i]);
821         printf("\n");
822         for(i = 0; i < 16; i++)
823             printf(" %02x", out[i]);
824         printf("\n");
825     }
826 #endif
827     return 0;
828 }
829
830 /* We have nothing to do for QCOW2 reopen, stubs just return
831  * success */
832 static int qcow2_reopen_prepare(BDRVReopenState *state,
833                                 BlockReopenQueue *queue, Error **errp)
834 {
835     return 0;
836 }
837
838 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
839         int64_t sector_num, int nb_sectors, int *pnum)
840 {
841     BDRVQcowState *s = bs->opaque;
842     uint64_t cluster_offset;
843     int index_in_cluster, ret;
844     int64_t status = 0;
845
846     *pnum = nb_sectors;
847     qemu_co_mutex_lock(&s->lock);
848     ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
849     qemu_co_mutex_unlock(&s->lock);
850     if (ret < 0) {
851         return ret;
852     }
853
854     if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
855         !s->crypt_method) {
856         index_in_cluster = sector_num & (s->cluster_sectors - 1);
857         cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
858         status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
859     }
860     if (ret == QCOW2_CLUSTER_ZERO) {
861         status |= BDRV_BLOCK_ZERO;
862     } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
863         status |= BDRV_BLOCK_DATA;
864     }
865     return status;
866 }
867
868 /* handle reading after the end of the backing file */
869 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
870                   int64_t sector_num, int nb_sectors)
871 {
872     int n1;
873     if ((sector_num + nb_sectors) <= bs->total_sectors)
874         return nb_sectors;
875     if (sector_num >= bs->total_sectors)
876         n1 = 0;
877     else
878         n1 = bs->total_sectors - sector_num;
879
880     qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
881
882     return n1;
883 }
884
885 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
886                           int remaining_sectors, QEMUIOVector *qiov)
887 {
888     BDRVQcowState *s = bs->opaque;
889     int index_in_cluster, n1;
890     int ret;
891     int cur_nr_sectors; /* number of sectors in current iteration */
892     uint64_t cluster_offset = 0;
893     uint64_t bytes_done = 0;
894     QEMUIOVector hd_qiov;
895     uint8_t *cluster_data = NULL;
896
897     qemu_iovec_init(&hd_qiov, qiov->niov);
898
899     qemu_co_mutex_lock(&s->lock);
900
901     while (remaining_sectors != 0) {
902
903         /* prepare next request */
904         cur_nr_sectors = remaining_sectors;
905         if (s->crypt_method) {
906             cur_nr_sectors = MIN(cur_nr_sectors,
907                 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
908         }
909
910         ret = qcow2_get_cluster_offset(bs, sector_num << 9,
911             &cur_nr_sectors, &cluster_offset);
912         if (ret < 0) {
913             goto fail;
914         }
915
916         index_in_cluster = sector_num & (s->cluster_sectors - 1);
917
918         qemu_iovec_reset(&hd_qiov);
919         qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
920             cur_nr_sectors * 512);
921
922         switch (ret) {
923         case QCOW2_CLUSTER_UNALLOCATED:
924
925             if (bs->backing_hd) {
926                 /* read from the base image */
927                 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
928                     sector_num, cur_nr_sectors);
929                 if (n1 > 0) {
930                     BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
931                     qemu_co_mutex_unlock(&s->lock);
932                     ret = bdrv_co_readv(bs->backing_hd, sector_num,
933                                         n1, &hd_qiov);
934                     qemu_co_mutex_lock(&s->lock);
935                     if (ret < 0) {
936                         goto fail;
937                     }
938                 }
939             } else {
940                 /* Note: in this case, no need to wait */
941                 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
942             }
943             break;
944
945         case QCOW2_CLUSTER_ZERO:
946             qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
947             break;
948
949         case QCOW2_CLUSTER_COMPRESSED:
950             /* add AIO support for compressed blocks ? */
951             ret = qcow2_decompress_cluster(bs, cluster_offset);
952             if (ret < 0) {
953                 goto fail;
954             }
955
956             qemu_iovec_from_buf(&hd_qiov, 0,
957                 s->cluster_cache + index_in_cluster * 512,
958                 512 * cur_nr_sectors);
959             break;
960
961         case QCOW2_CLUSTER_NORMAL:
962             if ((cluster_offset & 511) != 0) {
963                 ret = -EIO;
964                 goto fail;
965             }
966
967             if (s->crypt_method) {
968                 /*
969                  * For encrypted images, read everything into a temporary
970                  * contiguous buffer on which the AES functions can work.
971                  */
972                 if (!cluster_data) {
973                     cluster_data =
974                         qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
975                 }
976
977                 assert(cur_nr_sectors <=
978                     QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
979                 qemu_iovec_reset(&hd_qiov);
980                 qemu_iovec_add(&hd_qiov, cluster_data,
981                     512 * cur_nr_sectors);
982             }
983
984             BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
985             qemu_co_mutex_unlock(&s->lock);
986             ret = bdrv_co_readv(bs->file,
987                                 (cluster_offset >> 9) + index_in_cluster,
988                                 cur_nr_sectors, &hd_qiov);
989             qemu_co_mutex_lock(&s->lock);
990             if (ret < 0) {
991                 goto fail;
992             }
993             if (s->crypt_method) {
994                 qcow2_encrypt_sectors(s, sector_num,  cluster_data,
995                     cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
996                 qemu_iovec_from_buf(qiov, bytes_done,
997                     cluster_data, 512 * cur_nr_sectors);
998             }
999             break;
1000
1001         default:
1002             g_assert_not_reached();
1003             ret = -EIO;
1004             goto fail;
1005         }
1006
1007         remaining_sectors -= cur_nr_sectors;
1008         sector_num += cur_nr_sectors;
1009         bytes_done += cur_nr_sectors * 512;
1010     }
1011     ret = 0;
1012
1013 fail:
1014     qemu_co_mutex_unlock(&s->lock);
1015
1016     qemu_iovec_destroy(&hd_qiov);
1017     qemu_vfree(cluster_data);
1018
1019     return ret;
1020 }
1021
1022 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
1023                            int64_t sector_num,
1024                            int remaining_sectors,
1025                            QEMUIOVector *qiov)
1026 {
1027     BDRVQcowState *s = bs->opaque;
1028     int index_in_cluster;
1029     int ret;
1030     int cur_nr_sectors; /* number of sectors in current iteration */
1031     uint64_t cluster_offset;
1032     QEMUIOVector hd_qiov;
1033     uint64_t bytes_done = 0;
1034     uint8_t *cluster_data = NULL;
1035     QCowL2Meta *l2meta = NULL;
1036
1037     trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1038                                  remaining_sectors);
1039
1040     qemu_iovec_init(&hd_qiov, qiov->niov);
1041
1042     s->cluster_cache_offset = -1; /* disable compressed cache */
1043
1044     qemu_co_mutex_lock(&s->lock);
1045
1046     while (remaining_sectors != 0) {
1047
1048         l2meta = NULL;
1049
1050         trace_qcow2_writev_start_part(qemu_coroutine_self());
1051         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1052         cur_nr_sectors = remaining_sectors;
1053         if (s->crypt_method &&
1054             cur_nr_sectors >
1055             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1056             cur_nr_sectors =
1057                 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
1058         }
1059
1060         ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
1061             &cur_nr_sectors, &cluster_offset, &l2meta);
1062         if (ret < 0) {
1063             goto fail;
1064         }
1065
1066         assert((cluster_offset & 511) == 0);
1067
1068         qemu_iovec_reset(&hd_qiov);
1069         qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1070             cur_nr_sectors * 512);
1071
1072         if (s->crypt_method) {
1073             if (!cluster_data) {
1074                 cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
1075                                                  s->cluster_size);
1076             }
1077
1078             assert(hd_qiov.size <=
1079                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1080             qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
1081
1082             qcow2_encrypt_sectors(s, sector_num, cluster_data,
1083                 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
1084
1085             qemu_iovec_reset(&hd_qiov);
1086             qemu_iovec_add(&hd_qiov, cluster_data,
1087                 cur_nr_sectors * 512);
1088         }
1089
1090         ret = qcow2_pre_write_overlap_check(bs, 0,
1091                 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1092                 cur_nr_sectors * BDRV_SECTOR_SIZE);
1093         if (ret < 0) {
1094             goto fail;
1095         }
1096
1097         qemu_co_mutex_unlock(&s->lock);
1098         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1099         trace_qcow2_writev_data(qemu_coroutine_self(),
1100                                 (cluster_offset >> 9) + index_in_cluster);
1101         ret = bdrv_co_writev(bs->file,
1102                              (cluster_offset >> 9) + index_in_cluster,
1103                              cur_nr_sectors, &hd_qiov);
1104         qemu_co_mutex_lock(&s->lock);
1105         if (ret < 0) {
1106             goto fail;
1107         }
1108
1109         while (l2meta != NULL) {
1110             QCowL2Meta *next;
1111
1112             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1113             if (ret < 0) {
1114                 goto fail;
1115             }
1116
1117             /* Take the request off the list of running requests */
1118             if (l2meta->nb_clusters != 0) {
1119                 QLIST_REMOVE(l2meta, next_in_flight);
1120             }
1121
1122             qemu_co_queue_restart_all(&l2meta->dependent_requests);
1123
1124             next = l2meta->next;
1125             g_free(l2meta);
1126             l2meta = next;
1127         }
1128
1129         remaining_sectors -= cur_nr_sectors;
1130         sector_num += cur_nr_sectors;
1131         bytes_done += cur_nr_sectors * 512;
1132         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
1133     }
1134     ret = 0;
1135
1136 fail:
1137     qemu_co_mutex_unlock(&s->lock);
1138
1139     while (l2meta != NULL) {
1140         QCowL2Meta *next;
1141
1142         if (l2meta->nb_clusters != 0) {
1143             QLIST_REMOVE(l2meta, next_in_flight);
1144         }
1145         qemu_co_queue_restart_all(&l2meta->dependent_requests);
1146
1147         next = l2meta->next;
1148         g_free(l2meta);
1149         l2meta = next;
1150     }
1151
1152     qemu_iovec_destroy(&hd_qiov);
1153     qemu_vfree(cluster_data);
1154     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
1155
1156     return ret;
1157 }
1158
1159 static void qcow2_close(BlockDriverState *bs)
1160 {
1161     BDRVQcowState *s = bs->opaque;
1162     g_free(s->l1_table);
1163     /* else pre-write overlap checks in cache_destroy may crash */
1164     s->l1_table = NULL;
1165
1166     if (!(bs->open_flags & BDRV_O_INCOMING)) {
1167         qcow2_cache_flush(bs, s->l2_table_cache);
1168         qcow2_cache_flush(bs, s->refcount_block_cache);
1169
1170         qcow2_mark_clean(bs);
1171     }
1172
1173     qcow2_cache_destroy(bs, s->l2_table_cache);
1174     qcow2_cache_destroy(bs, s->refcount_block_cache);
1175
1176     g_free(s->unknown_header_fields);
1177     cleanup_unknown_header_ext(bs);
1178
1179     g_free(s->cluster_cache);
1180     qemu_vfree(s->cluster_data);
1181     qcow2_refcount_close(bs);
1182     qcow2_free_snapshots(bs);
1183 }
1184
1185 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
1186 {
1187     BDRVQcowState *s = bs->opaque;
1188     int flags = s->flags;
1189     AES_KEY aes_encrypt_key;
1190     AES_KEY aes_decrypt_key;
1191     uint32_t crypt_method = 0;
1192     QDict *options;
1193     Error *local_err = NULL;
1194     int ret;
1195
1196     /*
1197      * Backing files are read-only which makes all of their metadata immutable,
1198      * that means we don't have to worry about reopening them here.
1199      */
1200
1201     if (s->crypt_method) {
1202         crypt_method = s->crypt_method;
1203         memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
1204         memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
1205     }
1206
1207     qcow2_close(bs);
1208
1209     bdrv_invalidate_cache(bs->file, &local_err);
1210     if (local_err) {
1211         error_propagate(errp, local_err);
1212         return;
1213     }
1214
1215     memset(s, 0, sizeof(BDRVQcowState));
1216     options = qdict_clone_shallow(bs->options);
1217
1218     ret = qcow2_open(bs, options, flags, &local_err);
1219     if (local_err) {
1220         error_setg(errp, "Could not reopen qcow2 layer: %s",
1221                    error_get_pretty(local_err));
1222         error_free(local_err);
1223         return;
1224     } else if (ret < 0) {
1225         error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1226         return;
1227     }
1228
1229     QDECREF(options);
1230
1231     if (crypt_method) {
1232         s->crypt_method = crypt_method;
1233         memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
1234         memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
1235     }
1236 }
1237
1238 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1239     size_t len, size_t buflen)
1240 {
1241     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1242     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1243
1244     if (buflen < ext_len) {
1245         return -ENOSPC;
1246     }
1247
1248     *ext_backing_fmt = (QCowExtension) {
1249         .magic  = cpu_to_be32(magic),
1250         .len    = cpu_to_be32(len),
1251     };
1252     memcpy(buf + sizeof(QCowExtension), s, len);
1253
1254     return ext_len;
1255 }
1256
1257 /*
1258  * Updates the qcow2 header, including the variable length parts of it, i.e.
1259  * the backing file name and all extensions. qcow2 was not designed to allow
1260  * such changes, so if we run out of space (we can only use the first cluster)
1261  * this function may fail.
1262  *
1263  * Returns 0 on success, -errno in error cases.
1264  */
1265 int qcow2_update_header(BlockDriverState *bs)
1266 {
1267     BDRVQcowState *s = bs->opaque;
1268     QCowHeader *header;
1269     char *buf;
1270     size_t buflen = s->cluster_size;
1271     int ret;
1272     uint64_t total_size;
1273     uint32_t refcount_table_clusters;
1274     size_t header_length;
1275     Qcow2UnknownHeaderExtension *uext;
1276
1277     buf = qemu_blockalign(bs, buflen);
1278
1279     /* Header structure */
1280     header = (QCowHeader*) buf;
1281
1282     if (buflen < sizeof(*header)) {
1283         ret = -ENOSPC;
1284         goto fail;
1285     }
1286
1287     header_length = sizeof(*header) + s->unknown_header_fields_size;
1288     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1289     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1290
1291     *header = (QCowHeader) {
1292         /* Version 2 fields */
1293         .magic                  = cpu_to_be32(QCOW_MAGIC),
1294         .version                = cpu_to_be32(s->qcow_version),
1295         .backing_file_offset    = 0,
1296         .backing_file_size      = 0,
1297         .cluster_bits           = cpu_to_be32(s->cluster_bits),
1298         .size                   = cpu_to_be64(total_size),
1299         .crypt_method           = cpu_to_be32(s->crypt_method_header),
1300         .l1_size                = cpu_to_be32(s->l1_size),
1301         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
1302         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
1303         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1304         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
1305         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
1306
1307         /* Version 3 fields */
1308         .incompatible_features  = cpu_to_be64(s->incompatible_features),
1309         .compatible_features    = cpu_to_be64(s->compatible_features),
1310         .autoclear_features     = cpu_to_be64(s->autoclear_features),
1311         .refcount_order         = cpu_to_be32(s->refcount_order),
1312         .header_length          = cpu_to_be32(header_length),
1313     };
1314
1315     /* For older versions, write a shorter header */
1316     switch (s->qcow_version) {
1317     case 2:
1318         ret = offsetof(QCowHeader, incompatible_features);
1319         break;
1320     case 3:
1321         ret = sizeof(*header);
1322         break;
1323     default:
1324         ret = -EINVAL;
1325         goto fail;
1326     }
1327
1328     buf += ret;
1329     buflen -= ret;
1330     memset(buf, 0, buflen);
1331
1332     /* Preserve any unknown field in the header */
1333     if (s->unknown_header_fields_size) {
1334         if (buflen < s->unknown_header_fields_size) {
1335             ret = -ENOSPC;
1336             goto fail;
1337         }
1338
1339         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1340         buf += s->unknown_header_fields_size;
1341         buflen -= s->unknown_header_fields_size;
1342     }
1343
1344     /* Backing file format header extension */
1345     if (*bs->backing_format) {
1346         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1347                              bs->backing_format, strlen(bs->backing_format),
1348                              buflen);
1349         if (ret < 0) {
1350             goto fail;
1351         }
1352
1353         buf += ret;
1354         buflen -= ret;
1355     }
1356
1357     /* Feature table */
1358     Qcow2Feature features[] = {
1359         {
1360             .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1361             .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
1362             .name = "dirty bit",
1363         },
1364         {
1365             .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1366             .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
1367             .name = "corrupt bit",
1368         },
1369         {
1370             .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1371             .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1372             .name = "lazy refcounts",
1373         },
1374     };
1375
1376     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1377                          features, sizeof(features), buflen);
1378     if (ret < 0) {
1379         goto fail;
1380     }
1381     buf += ret;
1382     buflen -= ret;
1383
1384     /* Keep unknown header extensions */
1385     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1386         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1387         if (ret < 0) {
1388             goto fail;
1389         }
1390
1391         buf += ret;
1392         buflen -= ret;
1393     }
1394
1395     /* End of header extensions */
1396     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1397     if (ret < 0) {
1398         goto fail;
1399     }
1400
1401     buf += ret;
1402     buflen -= ret;
1403
1404     /* Backing file name */
1405     if (*bs->backing_file) {
1406         size_t backing_file_len = strlen(bs->backing_file);
1407
1408         if (buflen < backing_file_len) {
1409             ret = -ENOSPC;
1410             goto fail;
1411         }
1412
1413         /* Using strncpy is ok here, since buf is not NUL-terminated. */
1414         strncpy(buf, bs->backing_file, buflen);
1415
1416         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1417         header->backing_file_size   = cpu_to_be32(backing_file_len);
1418     }
1419
1420     /* Write the new header */
1421     ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1422     if (ret < 0) {
1423         goto fail;
1424     }
1425
1426     ret = 0;
1427 fail:
1428     qemu_vfree(header);
1429     return ret;
1430 }
1431
1432 static int qcow2_change_backing_file(BlockDriverState *bs,
1433     const char *backing_file, const char *backing_fmt)
1434 {
1435     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1436     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1437
1438     return qcow2_update_header(bs);
1439 }
1440
1441 static int preallocate(BlockDriverState *bs)
1442 {
1443     uint64_t nb_sectors;
1444     uint64_t offset;
1445     uint64_t host_offset = 0;
1446     int num;
1447     int ret;
1448     QCowL2Meta *meta;
1449
1450     nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
1451     offset = 0;
1452
1453     while (nb_sectors) {
1454         num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
1455         ret = qcow2_alloc_cluster_offset(bs, offset, &num,
1456                                          &host_offset, &meta);
1457         if (ret < 0) {
1458             return ret;
1459         }
1460
1461         if (meta != NULL) {
1462             ret = qcow2_alloc_cluster_link_l2(bs, meta);
1463             if (ret < 0) {
1464                 qcow2_free_any_clusters(bs, meta->alloc_offset,
1465                                         meta->nb_clusters, QCOW2_DISCARD_NEVER);
1466                 return ret;
1467             }
1468
1469             /* There are no dependent requests, but we need to remove our
1470              * request from the list of in-flight requests */
1471             QLIST_REMOVE(meta, next_in_flight);
1472         }
1473
1474         /* TODO Preallocate data if requested */
1475
1476         nb_sectors -= num;
1477         offset += num << BDRV_SECTOR_BITS;
1478     }
1479
1480     /*
1481      * It is expected that the image file is large enough to actually contain
1482      * all of the allocated clusters (otherwise we get failing reads after
1483      * EOF). Extend the image to the last allocated sector.
1484      */
1485     if (host_offset != 0) {
1486         uint8_t buf[BDRV_SECTOR_SIZE];
1487         memset(buf, 0, BDRV_SECTOR_SIZE);
1488         ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
1489                          buf, 1);
1490         if (ret < 0) {
1491             return ret;
1492         }
1493     }
1494
1495     return 0;
1496 }
1497
1498 static int qcow2_create2(const char *filename, int64_t total_size,
1499                          const char *backing_file, const char *backing_format,
1500                          int flags, size_t cluster_size, int prealloc,
1501                          QEMUOptionParameter *options, int version,
1502                          Error **errp)
1503 {
1504     /* Calculate cluster_bits */
1505     int cluster_bits;
1506     cluster_bits = ffs(cluster_size) - 1;
1507     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1508         (1 << cluster_bits) != cluster_size)
1509     {
1510         error_setg(errp, "Cluster size must be a power of two between %d and "
1511                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1512         return -EINVAL;
1513     }
1514
1515     /*
1516      * Open the image file and write a minimal qcow2 header.
1517      *
1518      * We keep things simple and start with a zero-sized image. We also
1519      * do without refcount blocks or a L1 table for now. We'll fix the
1520      * inconsistency later.
1521      *
1522      * We do need a refcount table because growing the refcount table means
1523      * allocating two new refcount blocks - the seconds of which would be at
1524      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1525      * size for any qcow2 image.
1526      */
1527     BlockDriverState* bs;
1528     QCowHeader *header;
1529     uint8_t* refcount_table;
1530     Error *local_err = NULL;
1531     int ret;
1532
1533     ret = bdrv_create_file(filename, options, &local_err);
1534     if (ret < 0) {
1535         error_propagate(errp, local_err);
1536         return ret;
1537     }
1538
1539     bs = NULL;
1540     ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1541                     NULL, &local_err);
1542     if (ret < 0) {
1543         error_propagate(errp, local_err);
1544         return ret;
1545     }
1546
1547     /* Write the header */
1548     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
1549     header = g_malloc0(cluster_size);
1550     *header = (QCowHeader) {
1551         .magic                      = cpu_to_be32(QCOW_MAGIC),
1552         .version                    = cpu_to_be32(version),
1553         .cluster_bits               = cpu_to_be32(cluster_bits),
1554         .size                       = cpu_to_be64(0),
1555         .l1_table_offset            = cpu_to_be64(0),
1556         .l1_size                    = cpu_to_be32(0),
1557         .refcount_table_offset      = cpu_to_be64(cluster_size),
1558         .refcount_table_clusters    = cpu_to_be32(1),
1559         .refcount_order             = cpu_to_be32(3 + REFCOUNT_SHIFT),
1560         .header_length              = cpu_to_be32(sizeof(*header)),
1561     };
1562
1563     if (flags & BLOCK_FLAG_ENCRYPT) {
1564         header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1565     } else {
1566         header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1567     }
1568
1569     if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1570         header->compatible_features |=
1571             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1572     }
1573
1574     ret = bdrv_pwrite(bs, 0, header, cluster_size);
1575     g_free(header);
1576     if (ret < 0) {
1577         error_setg_errno(errp, -ret, "Could not write qcow2 header");
1578         goto out;
1579     }
1580
1581     /* Write an empty refcount table */
1582     refcount_table = g_malloc0(cluster_size);
1583     ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
1584     g_free(refcount_table);
1585
1586     if (ret < 0) {
1587         error_setg_errno(errp, -ret, "Could not write refcount table");
1588         goto out;
1589     }
1590
1591     bdrv_unref(bs);
1592     bs = NULL;
1593
1594     /*
1595      * And now open the image and make it consistent first (i.e. increase the
1596      * refcount of the cluster that is occupied by the header and the refcount
1597      * table)
1598      */
1599     BlockDriver* drv = bdrv_find_format("qcow2");
1600     assert(drv != NULL);
1601     ret = bdrv_open(&bs, filename, NULL, NULL,
1602         BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, &local_err);
1603     if (ret < 0) {
1604         error_propagate(errp, local_err);
1605         goto out;
1606     }
1607
1608     ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1609     if (ret < 0) {
1610         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
1611                          "header and refcount table");
1612         goto out;
1613
1614     } else if (ret != 0) {
1615         error_report("Huh, first cluster in empty image is already in use?");
1616         abort();
1617     }
1618
1619     /* Okay, now that we have a valid image, let's give it the right size */
1620     ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1621     if (ret < 0) {
1622         error_setg_errno(errp, -ret, "Could not resize image");
1623         goto out;
1624     }
1625
1626     /* Want a backing file? There you go.*/
1627     if (backing_file) {
1628         ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1629         if (ret < 0) {
1630             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
1631                              "with format '%s'", backing_file, backing_format);
1632             goto out;
1633         }
1634     }
1635
1636     /* And if we're supposed to preallocate metadata, do that now */
1637     if (prealloc) {
1638         BDRVQcowState *s = bs->opaque;
1639         qemu_co_mutex_lock(&s->lock);
1640         ret = preallocate(bs);
1641         qemu_co_mutex_unlock(&s->lock);
1642         if (ret < 0) {
1643             error_setg_errno(errp, -ret, "Could not preallocate metadata");
1644             goto out;
1645         }
1646     }
1647
1648     bdrv_unref(bs);
1649     bs = NULL;
1650
1651     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
1652     ret = bdrv_open(&bs, filename, NULL, NULL,
1653                     BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
1654                     drv, &local_err);
1655     if (local_err) {
1656         error_propagate(errp, local_err);
1657         goto out;
1658     }
1659
1660     ret = 0;
1661 out:
1662     if (bs) {
1663         bdrv_unref(bs);
1664     }
1665     return ret;
1666 }
1667
1668 static int qcow2_create(const char *filename, QEMUOptionParameter *options,
1669                         Error **errp)
1670 {
1671     const char *backing_file = NULL;
1672     const char *backing_fmt = NULL;
1673     uint64_t sectors = 0;
1674     int flags = 0;
1675     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
1676     int prealloc = 0;
1677     int version = 3;
1678     Error *local_err = NULL;
1679     int ret;
1680
1681     /* Read out options */
1682     while (options && options->name) {
1683         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1684             sectors = options->value.n / 512;
1685         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1686             backing_file = options->value.s;
1687         } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1688             backing_fmt = options->value.s;
1689         } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1690             flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1691         } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1692             if (options->value.n) {
1693                 cluster_size = options->value.n;
1694             }
1695         } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1696             if (!options->value.s || !strcmp(options->value.s, "off")) {
1697                 prealloc = 0;
1698             } else if (!strcmp(options->value.s, "metadata")) {
1699                 prealloc = 1;
1700             } else {
1701                 error_setg(errp, "Invalid preallocation mode: '%s'",
1702                            options->value.s);
1703                 return -EINVAL;
1704             }
1705         } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
1706             if (!options->value.s) {
1707                 /* keep the default */
1708             } else if (!strcmp(options->value.s, "0.10")) {
1709                 version = 2;
1710             } else if (!strcmp(options->value.s, "1.1")) {
1711                 version = 3;
1712             } else {
1713                 error_setg(errp, "Invalid compatibility level: '%s'",
1714                            options->value.s);
1715                 return -EINVAL;
1716             }
1717         } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1718             flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
1719         }
1720         options++;
1721     }
1722
1723     if (backing_file && prealloc) {
1724         error_setg(errp, "Backing file and preallocation cannot be used at "
1725                    "the same time");
1726         return -EINVAL;
1727     }
1728
1729     if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
1730         error_setg(errp, "Lazy refcounts only supported with compatibility "
1731                    "level 1.1 and above (use compat=1.1 or greater)");
1732         return -EINVAL;
1733     }
1734
1735     ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1736                         cluster_size, prealloc, options, version, &local_err);
1737     if (local_err) {
1738         error_propagate(errp, local_err);
1739     }
1740     return ret;
1741 }
1742
1743 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
1744     int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
1745 {
1746     int ret;
1747     BDRVQcowState *s = bs->opaque;
1748
1749     /* Emulate misaligned zero writes */
1750     if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1751         return -ENOTSUP;
1752     }
1753
1754     /* Whatever is left can use real zero clusters */
1755     qemu_co_mutex_lock(&s->lock);
1756     ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1757         nb_sectors);
1758     qemu_co_mutex_unlock(&s->lock);
1759
1760     return ret;
1761 }
1762
1763 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1764     int64_t sector_num, int nb_sectors)
1765 {
1766     int ret;
1767     BDRVQcowState *s = bs->opaque;
1768
1769     qemu_co_mutex_lock(&s->lock);
1770     ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1771         nb_sectors, QCOW2_DISCARD_REQUEST);
1772     qemu_co_mutex_unlock(&s->lock);
1773     return ret;
1774 }
1775
1776 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1777 {
1778     BDRVQcowState *s = bs->opaque;
1779     int64_t new_l1_size;
1780     int ret;
1781
1782     if (offset & 511) {
1783         error_report("The new size must be a multiple of 512");
1784         return -EINVAL;
1785     }
1786
1787     /* cannot proceed if image has snapshots */
1788     if (s->nb_snapshots) {
1789         error_report("Can't resize an image which has snapshots");
1790         return -ENOTSUP;
1791     }
1792
1793     /* shrinking is currently not supported */
1794     if (offset < bs->total_sectors * 512) {
1795         error_report("qcow2 doesn't support shrinking images yet");
1796         return -ENOTSUP;
1797     }
1798
1799     new_l1_size = size_to_l1(s, offset);
1800     ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1801     if (ret < 0) {
1802         return ret;
1803     }
1804
1805     /* write updated header.size */
1806     offset = cpu_to_be64(offset);
1807     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1808                            &offset, sizeof(uint64_t));
1809     if (ret < 0) {
1810         return ret;
1811     }
1812
1813     s->l1_vm_state_index = new_l1_size;
1814     return 0;
1815 }
1816
1817 /* XXX: put compressed sectors first, then all the cluster aligned
1818    tables to avoid losing bytes in alignment */
1819 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1820                                   const uint8_t *buf, int nb_sectors)
1821 {
1822     BDRVQcowState *s = bs->opaque;
1823     z_stream strm;
1824     int ret, out_len;
1825     uint8_t *out_buf;
1826     uint64_t cluster_offset;
1827
1828     if (nb_sectors == 0) {
1829         /* align end of file to a sector boundary to ease reading with
1830            sector based I/Os */
1831         cluster_offset = bdrv_getlength(bs->file);
1832         cluster_offset = (cluster_offset + 511) & ~511;
1833         bdrv_truncate(bs->file, cluster_offset);
1834         return 0;
1835     }
1836
1837     if (nb_sectors != s->cluster_sectors) {
1838         ret = -EINVAL;
1839
1840         /* Zero-pad last write if image size is not cluster aligned */
1841         if (sector_num + nb_sectors == bs->total_sectors &&
1842             nb_sectors < s->cluster_sectors) {
1843             uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
1844             memset(pad_buf, 0, s->cluster_size);
1845             memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
1846             ret = qcow2_write_compressed(bs, sector_num,
1847                                          pad_buf, s->cluster_sectors);
1848             qemu_vfree(pad_buf);
1849         }
1850         return ret;
1851     }
1852
1853     out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1854
1855     /* best compression, small window, no zlib header */
1856     memset(&strm, 0, sizeof(strm));
1857     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1858                        Z_DEFLATED, -12,
1859                        9, Z_DEFAULT_STRATEGY);
1860     if (ret != 0) {
1861         ret = -EINVAL;
1862         goto fail;
1863     }
1864
1865     strm.avail_in = s->cluster_size;
1866     strm.next_in = (uint8_t *)buf;
1867     strm.avail_out = s->cluster_size;
1868     strm.next_out = out_buf;
1869
1870     ret = deflate(&strm, Z_FINISH);
1871     if (ret != Z_STREAM_END && ret != Z_OK) {
1872         deflateEnd(&strm);
1873         ret = -EINVAL;
1874         goto fail;
1875     }
1876     out_len = strm.next_out - out_buf;
1877
1878     deflateEnd(&strm);
1879
1880     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1881         /* could not compress: write normal cluster */
1882         ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1883         if (ret < 0) {
1884             goto fail;
1885         }
1886     } else {
1887         cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1888             sector_num << 9, out_len);
1889         if (!cluster_offset) {
1890             ret = -EIO;
1891             goto fail;
1892         }
1893         cluster_offset &= s->cluster_offset_mask;
1894
1895         ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
1896         if (ret < 0) {
1897             goto fail;
1898         }
1899
1900         BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1901         ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1902         if (ret < 0) {
1903             goto fail;
1904         }
1905     }
1906
1907     ret = 0;
1908 fail:
1909     g_free(out_buf);
1910     return ret;
1911 }
1912
1913 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
1914 {
1915     BDRVQcowState *s = bs->opaque;
1916     int ret;
1917
1918     qemu_co_mutex_lock(&s->lock);
1919     ret = qcow2_cache_flush(bs, s->l2_table_cache);
1920     if (ret < 0) {
1921         qemu_co_mutex_unlock(&s->lock);
1922         return ret;
1923     }
1924
1925     if (qcow2_need_accurate_refcounts(s)) {
1926         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1927         if (ret < 0) {
1928             qemu_co_mutex_unlock(&s->lock);
1929             return ret;
1930         }
1931     }
1932     qemu_co_mutex_unlock(&s->lock);
1933
1934     return 0;
1935 }
1936
1937 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1938 {
1939     BDRVQcowState *s = bs->opaque;
1940     bdi->unallocated_blocks_are_zero = true;
1941     bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
1942     bdi->cluster_size = s->cluster_size;
1943     bdi->vm_state_offset = qcow2_vm_state_offset(s);
1944     return 0;
1945 }
1946
1947 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
1948 {
1949     BDRVQcowState *s = bs->opaque;
1950     ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
1951
1952     *spec_info = (ImageInfoSpecific){
1953         .kind  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
1954         {
1955             .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
1956         },
1957     };
1958     if (s->qcow_version == 2) {
1959         *spec_info->qcow2 = (ImageInfoSpecificQCow2){
1960             .compat = g_strdup("0.10"),
1961         };
1962     } else if (s->qcow_version == 3) {
1963         *spec_info->qcow2 = (ImageInfoSpecificQCow2){
1964             .compat             = g_strdup("1.1"),
1965             .lazy_refcounts     = s->compatible_features &
1966                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
1967             .has_lazy_refcounts = true,
1968         };
1969     }
1970
1971     return spec_info;
1972 }
1973
1974 #if 0
1975 static void dump_refcounts(BlockDriverState *bs)
1976 {
1977     BDRVQcowState *s = bs->opaque;
1978     int64_t nb_clusters, k, k1, size;
1979     int refcount;
1980
1981     size = bdrv_getlength(bs->file);
1982     nb_clusters = size_to_clusters(s, size);
1983     for(k = 0; k < nb_clusters;) {
1984         k1 = k;
1985         refcount = get_refcount(bs, k);
1986         k++;
1987         while (k < nb_clusters && get_refcount(bs, k) == refcount)
1988             k++;
1989         printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1990                k - k1);
1991     }
1992 }
1993 #endif
1994
1995 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
1996                               int64_t pos)
1997 {
1998     BDRVQcowState *s = bs->opaque;
1999     int64_t total_sectors = bs->total_sectors;
2000     int growable = bs->growable;
2001     bool zero_beyond_eof = bs->zero_beyond_eof;
2002     int ret;
2003
2004     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
2005     bs->growable = 1;
2006     bs->zero_beyond_eof = false;
2007     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
2008     bs->growable = growable;
2009     bs->zero_beyond_eof = zero_beyond_eof;
2010
2011     /* bdrv_co_do_writev will have increased the total_sectors value to include
2012      * the VM state - the VM state is however not an actual part of the block
2013      * device, therefore, we need to restore the old value. */
2014     bs->total_sectors = total_sectors;
2015
2016     return ret;
2017 }
2018
2019 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2020                               int64_t pos, int size)
2021 {
2022     BDRVQcowState *s = bs->opaque;
2023     int growable = bs->growable;
2024     bool zero_beyond_eof = bs->zero_beyond_eof;
2025     int ret;
2026
2027     BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
2028     bs->growable = 1;
2029     bs->zero_beyond_eof = false;
2030     ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
2031     bs->growable = growable;
2032     bs->zero_beyond_eof = zero_beyond_eof;
2033
2034     return ret;
2035 }
2036
2037 /*
2038  * Downgrades an image's version. To achieve this, any incompatible features
2039  * have to be removed.
2040  */
2041 static int qcow2_downgrade(BlockDriverState *bs, int target_version)
2042 {
2043     BDRVQcowState *s = bs->opaque;
2044     int current_version = s->qcow_version;
2045     int ret;
2046
2047     if (target_version == current_version) {
2048         return 0;
2049     } else if (target_version > current_version) {
2050         return -EINVAL;
2051     } else if (target_version != 2) {
2052         return -EINVAL;
2053     }
2054
2055     if (s->refcount_order != 4) {
2056         /* we would have to convert the image to a refcount_order == 4 image
2057          * here; however, since qemu (at the time of writing this) does not
2058          * support anything different than 4 anyway, there is no point in doing
2059          * so right now; however, we should error out (if qemu supports this in
2060          * the future and this code has not been adapted) */
2061         error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2062                      "currently not supported.");
2063         return -ENOTSUP;
2064     }
2065
2066     /* clear incompatible features */
2067     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2068         ret = qcow2_mark_clean(bs);
2069         if (ret < 0) {
2070             return ret;
2071         }
2072     }
2073
2074     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2075      * the first place; if that happens nonetheless, returning -ENOTSUP is the
2076      * best thing to do anyway */
2077
2078     if (s->incompatible_features) {
2079         return -ENOTSUP;
2080     }
2081
2082     /* since we can ignore compatible features, we can set them to 0 as well */
2083     s->compatible_features = 0;
2084     /* if lazy refcounts have been used, they have already been fixed through
2085      * clearing the dirty flag */
2086
2087     /* clearing autoclear features is trivial */
2088     s->autoclear_features = 0;
2089
2090     ret = qcow2_expand_zero_clusters(bs);
2091     if (ret < 0) {
2092         return ret;
2093     }
2094
2095     s->qcow_version = target_version;
2096     ret = qcow2_update_header(bs);
2097     if (ret < 0) {
2098         s->qcow_version = current_version;
2099         return ret;
2100     }
2101     return 0;
2102 }
2103
2104 static int qcow2_amend_options(BlockDriverState *bs,
2105                                QEMUOptionParameter *options)
2106 {
2107     BDRVQcowState *s = bs->opaque;
2108     int old_version = s->qcow_version, new_version = old_version;
2109     uint64_t new_size = 0;
2110     const char *backing_file = NULL, *backing_format = NULL;
2111     bool lazy_refcounts = s->use_lazy_refcounts;
2112     int ret;
2113     int i;
2114
2115     for (i = 0; options[i].name; i++)
2116     {
2117         if (!options[i].assigned) {
2118             /* only change explicitly defined options */
2119             continue;
2120         }
2121
2122         if (!strcmp(options[i].name, "compat")) {
2123             if (!options[i].value.s) {
2124                 /* preserve default */
2125             } else if (!strcmp(options[i].value.s, "0.10")) {
2126                 new_version = 2;
2127             } else if (!strcmp(options[i].value.s, "1.1")) {
2128                 new_version = 3;
2129             } else {
2130                 fprintf(stderr, "Unknown compatibility level %s.\n",
2131                         options[i].value.s);
2132                 return -EINVAL;
2133             }
2134         } else if (!strcmp(options[i].name, "preallocation")) {
2135             fprintf(stderr, "Cannot change preallocation mode.\n");
2136             return -ENOTSUP;
2137         } else if (!strcmp(options[i].name, "size")) {
2138             new_size = options[i].value.n;
2139         } else if (!strcmp(options[i].name, "backing_file")) {
2140             backing_file = options[i].value.s;
2141         } else if (!strcmp(options[i].name, "backing_fmt")) {
2142             backing_format = options[i].value.s;
2143         } else if (!strcmp(options[i].name, "encryption")) {
2144             if ((options[i].value.n != !!s->crypt_method)) {
2145                 fprintf(stderr, "Changing the encryption flag is not "
2146                         "supported.\n");
2147                 return -ENOTSUP;
2148             }
2149         } else if (!strcmp(options[i].name, "cluster_size")) {
2150             if (options[i].value.n != s->cluster_size) {
2151                 fprintf(stderr, "Changing the cluster size is not "
2152                         "supported.\n");
2153                 return -ENOTSUP;
2154             }
2155         } else if (!strcmp(options[i].name, "lazy_refcounts")) {
2156             lazy_refcounts = options[i].value.n;
2157         } else {
2158             /* if this assertion fails, this probably means a new option was
2159              * added without having it covered here */
2160             assert(false);
2161         }
2162     }
2163
2164     if (new_version != old_version) {
2165         if (new_version > old_version) {
2166             /* Upgrade */
2167             s->qcow_version = new_version;
2168             ret = qcow2_update_header(bs);
2169             if (ret < 0) {
2170                 s->qcow_version = old_version;
2171                 return ret;
2172             }
2173         } else {
2174             ret = qcow2_downgrade(bs, new_version);
2175             if (ret < 0) {
2176                 return ret;
2177             }
2178         }
2179     }
2180
2181     if (backing_file || backing_format) {
2182         ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
2183                                         backing_format ?: bs->backing_format);
2184         if (ret < 0) {
2185             return ret;
2186         }
2187     }
2188
2189     if (s->use_lazy_refcounts != lazy_refcounts) {
2190         if (lazy_refcounts) {
2191             if (s->qcow_version < 3) {
2192                 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2193                         "level 1.1 and above (use compat=1.1 or greater)\n");
2194                 return -EINVAL;
2195             }
2196             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2197             ret = qcow2_update_header(bs);
2198             if (ret < 0) {
2199                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2200                 return ret;
2201             }
2202             s->use_lazy_refcounts = true;
2203         } else {
2204             /* make image clean first */
2205             ret = qcow2_mark_clean(bs);
2206             if (ret < 0) {
2207                 return ret;
2208             }
2209             /* now disallow lazy refcounts */
2210             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2211             ret = qcow2_update_header(bs);
2212             if (ret < 0) {
2213                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2214                 return ret;
2215             }
2216             s->use_lazy_refcounts = false;
2217         }
2218     }
2219
2220     if (new_size) {
2221         ret = bdrv_truncate(bs, new_size);
2222         if (ret < 0) {
2223             return ret;
2224         }
2225     }
2226
2227     return 0;
2228 }
2229
2230 static QEMUOptionParameter qcow2_create_options[] = {
2231     {
2232         .name = BLOCK_OPT_SIZE,
2233         .type = OPT_SIZE,
2234         .help = "Virtual disk size"
2235     },
2236     {
2237         .name = BLOCK_OPT_COMPAT_LEVEL,
2238         .type = OPT_STRING,
2239         .help = "Compatibility level (0.10 or 1.1)"
2240     },
2241     {
2242         .name = BLOCK_OPT_BACKING_FILE,
2243         .type = OPT_STRING,
2244         .help = "File name of a base image"
2245     },
2246     {
2247         .name = BLOCK_OPT_BACKING_FMT,
2248         .type = OPT_STRING,
2249         .help = "Image format of the base image"
2250     },
2251     {
2252         .name = BLOCK_OPT_ENCRYPT,
2253         .type = OPT_FLAG,
2254         .help = "Encrypt the image"
2255     },
2256     {
2257         .name = BLOCK_OPT_CLUSTER_SIZE,
2258         .type = OPT_SIZE,
2259         .help = "qcow2 cluster size",
2260         .value = { .n = DEFAULT_CLUSTER_SIZE },
2261     },
2262     {
2263         .name = BLOCK_OPT_PREALLOC,
2264         .type = OPT_STRING,
2265         .help = "Preallocation mode (allowed values: off, metadata)"
2266     },
2267     {
2268         .name = BLOCK_OPT_LAZY_REFCOUNTS,
2269         .type = OPT_FLAG,
2270         .help = "Postpone refcount updates",
2271     },
2272     { NULL }
2273 };
2274
2275 static BlockDriver bdrv_qcow2 = {
2276     .format_name        = "qcow2",
2277     .instance_size      = sizeof(BDRVQcowState),
2278     .bdrv_probe         = qcow2_probe,
2279     .bdrv_open          = qcow2_open,
2280     .bdrv_close         = qcow2_close,
2281     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
2282     .bdrv_create        = qcow2_create,
2283     .bdrv_has_zero_init = bdrv_has_zero_init_1,
2284     .bdrv_co_get_block_status = qcow2_co_get_block_status,
2285     .bdrv_set_key       = qcow2_set_key,
2286
2287     .bdrv_co_readv          = qcow2_co_readv,
2288     .bdrv_co_writev         = qcow2_co_writev,
2289     .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
2290
2291     .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
2292     .bdrv_co_discard        = qcow2_co_discard,
2293     .bdrv_truncate          = qcow2_truncate,
2294     .bdrv_write_compressed  = qcow2_write_compressed,
2295
2296     .bdrv_snapshot_create   = qcow2_snapshot_create,
2297     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
2298     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
2299     .bdrv_snapshot_list     = qcow2_snapshot_list,
2300     .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
2301     .bdrv_get_info      = qcow2_get_info,
2302     .bdrv_get_specific_info = qcow2_get_specific_info,
2303
2304     .bdrv_save_vmstate    = qcow2_save_vmstate,
2305     .bdrv_load_vmstate    = qcow2_load_vmstate,
2306
2307     .bdrv_change_backing_file   = qcow2_change_backing_file,
2308
2309     .bdrv_refresh_limits        = qcow2_refresh_limits,
2310     .bdrv_invalidate_cache      = qcow2_invalidate_cache,
2311
2312     .create_options = qcow2_create_options,
2313     .bdrv_check = qcow2_check,
2314     .bdrv_amend_options = qcow2_amend_options,
2315 };
2316
2317 static void bdrv_qcow2_init(void)
2318 {
2319     bdrv_register(&bdrv_qcow2);
2320 }
2321
2322 block_init(bdrv_qcow2_init);