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