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