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