]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.c
hw/i386: Improve some of the warning messages
[mirror_qemu.git] / block / qcow2.c
CommitLineData
585f8587
FB
1/*
2 * Block driver for the QCOW version 2 format
5fafdf24 3 *
585f8587 4 * Copyright (c) 2004-2006 Fabrice Bellard
5fafdf24 5 *
585f8587
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
80c71a24 24#include "qemu/osdep.h"
737e150e 25#include "block/block_int.h"
23588797 26#include "sysemu/block-backend.h"
1de7afc9 27#include "qemu/module.h"
585f8587 28#include <zlib.h>
f7d0fe02 29#include "block/qcow2.h"
1de7afc9 30#include "qemu/error-report.h"
7b1b5d19 31#include "qapi/qmp/qerror.h"
acdfb480 32#include "qapi/qmp/qbool.h"
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"
f348b6d1 37#include "qemu/cutils.h"
58369e22 38#include "qemu/bswap.h"
b25b387f
DB
39#include "qapi/opts-visitor.h"
40#include "qapi-visit.h"
41#include "block/crypto.h"
585f8587
FB
42
43/*
44 Differences with QCOW:
45
46 - Support for multiple incremental snapshots.
47 - Memory management by reference counts.
48 - Clusters which have a reference count of one have the bit
49 QCOW_OFLAG_COPIED to optimize write performance.
5fafdf24 50 - Size of compressed clusters is stored in sectors to reduce bit usage
585f8587
FB
51 in the cluster offsets.
52 - Support for storing additional data (such as the VM state) in the
3b46e624 53 snapshots.
585f8587
FB
54 - If a backing store is used, the cluster size is not constrained
55 (could be backported to QCOW).
56 - L2 tables have always a size of one cluster.
57*/
58
9b80ddf3
AL
59
60typedef struct {
61 uint32_t magic;
62 uint32_t len;
c4217f64 63} QEMU_PACKED QCowExtension;
21d82ac9 64
7c80ab3f
JS
65#define QCOW2_EXT_MAGIC_END 0
66#define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
cfcc4c62 67#define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
4652b8f3 68#define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
88ddffae 69#define QCOW2_EXT_MAGIC_BITMAPS 0x23852875
9b80ddf3 70
7c80ab3f 71static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
585f8587
FB
72{
73 const QCowHeader *cow_header = (const void *)buf;
3b46e624 74
585f8587
FB
75 if (buf_size >= sizeof(QCowHeader) &&
76 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
6744cbab 77 be32_to_cpu(cow_header->version) >= 2)
585f8587
FB
78 return 100;
79 else
80 return 0;
81}
82
9b80ddf3 83
4652b8f3
DB
84static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
85 uint8_t *buf, size_t buflen,
86 void *opaque, Error **errp)
87{
88 BlockDriverState *bs = opaque;
89 BDRVQcow2State *s = bs->opaque;
90 ssize_t ret;
91
92 if ((offset + buflen) > s->crypto_header.length) {
93 error_setg(errp, "Request for data outside of extension header");
94 return -1;
95 }
96
97 ret = bdrv_pread(bs->file,
98 s->crypto_header.offset + offset, buf, buflen);
99 if (ret < 0) {
100 error_setg_errno(errp, -ret, "Could not read encryption header");
101 return -1;
102 }
103 return ret;
104}
105
106
107static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen,
108 void *opaque, Error **errp)
109{
110 BlockDriverState *bs = opaque;
111 BDRVQcow2State *s = bs->opaque;
112 int64_t ret;
113 int64_t clusterlen;
114
115 ret = qcow2_alloc_clusters(bs, headerlen);
116 if (ret < 0) {
117 error_setg_errno(errp, -ret,
118 "Cannot allocate cluster for LUKS header size %zu",
119 headerlen);
120 return -1;
121 }
122
123 s->crypto_header.length = headerlen;
124 s->crypto_header.offset = ret;
125
126 /* Zero fill remaining space in cluster so it has predictable
127 * content in case of future spec changes */
128 clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
129 ret = bdrv_pwrite_zeroes(bs->file,
130 ret + headerlen,
131 clusterlen - headerlen, 0);
132 if (ret < 0) {
133 error_setg_errno(errp, -ret, "Could not zero fill encryption header");
134 return -1;
135 }
136
137 return ret;
138}
139
140
141static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
142 const uint8_t *buf, size_t buflen,
143 void *opaque, Error **errp)
144{
145 BlockDriverState *bs = opaque;
146 BDRVQcow2State *s = bs->opaque;
147 ssize_t ret;
148
149 if ((offset + buflen) > s->crypto_header.length) {
150 error_setg(errp, "Request for data outside of extension header");
151 return -1;
152 }
153
154 ret = bdrv_pwrite(bs->file,
155 s->crypto_header.offset + offset, buf, buflen);
156 if (ret < 0) {
157 error_setg_errno(errp, -ret, "Could not read encryption header");
158 return -1;
159 }
160 return ret;
161}
162
163
9b80ddf3
AL
164/*
165 * read qcow2 extension and fill bs
166 * start reading from start_offset
167 * finish reading upon magic of value 0 or when end_offset reached
168 * unknown magic is skipped (future extension this version knows nothing about)
169 * return 0 upon success, non-0 otherwise
170 */
7c80ab3f 171static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
3ef6c40a 172 uint64_t end_offset, void **p_feature_table,
88ddffae
VSO
173 int flags, bool *need_update_header,
174 Error **errp)
9b80ddf3 175{
ff99129a 176 BDRVQcow2State *s = bs->opaque;
9b80ddf3
AL
177 QCowExtension ext;
178 uint64_t offset;
75bab85c 179 int ret;
88ddffae
VSO
180 Qcow2BitmapHeaderExt bitmaps_ext;
181
182 if (need_update_header != NULL) {
183 *need_update_header = false;
184 }
9b80ddf3
AL
185
186#ifdef DEBUG_EXT
7c80ab3f 187 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
9b80ddf3
AL
188#endif
189 offset = start_offset;
190 while (offset < end_offset) {
191
192#ifdef DEBUG_EXT
193 /* Sanity check */
194 if (offset > s->cluster_size)
7c80ab3f 195 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
9b80ddf3 196
9b2260cb 197 printf("attempting to read extended header in offset %lu\n", offset);
9b80ddf3
AL
198#endif
199
cf2ab8fc 200 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
3ef6c40a
HR
201 if (ret < 0) {
202 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
203 "pread fail from offset %" PRIu64, offset);
9b80ddf3
AL
204 return 1;
205 }
206 be32_to_cpus(&ext.magic);
207 be32_to_cpus(&ext.len);
208 offset += sizeof(ext);
209#ifdef DEBUG_EXT
210 printf("ext.magic = 0x%x\n", ext.magic);
211#endif
2ebafc85 212 if (offset > end_offset || ext.len > end_offset - offset) {
3ef6c40a 213 error_setg(errp, "Header extension too large");
64ca6aee
KW
214 return -EINVAL;
215 }
216
9b80ddf3 217 switch (ext.magic) {
7c80ab3f 218 case QCOW2_EXT_MAGIC_END:
9b80ddf3 219 return 0;
f965509c 220
7c80ab3f 221 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
f965509c 222 if (ext.len >= sizeof(bs->backing_format)) {
521b2b5d
HR
223 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
224 " too large (>=%zu)", ext.len,
225 sizeof(bs->backing_format));
f965509c
AL
226 return 2;
227 }
cf2ab8fc 228 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
3ef6c40a
HR
229 if (ret < 0) {
230 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
231 "Could not read format name");
f965509c 232 return 3;
3ef6c40a 233 }
f965509c 234 bs->backing_format[ext.len] = '\0';
e4603fe1 235 s->image_backing_format = g_strdup(bs->backing_format);
f965509c
AL
236#ifdef DEBUG_EXT
237 printf("Qcow2: Got format extension %s\n", bs->backing_format);
238#endif
f965509c
AL
239 break;
240
cfcc4c62
KW
241 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
242 if (p_feature_table != NULL) {
243 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
cf2ab8fc 244 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
cfcc4c62 245 if (ret < 0) {
3ef6c40a
HR
246 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
247 "Could not read table");
cfcc4c62
KW
248 return ret;
249 }
250
251 *p_feature_table = feature_table;
252 }
253 break;
254
4652b8f3
DB
255 case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
256 unsigned int cflags = 0;
257 if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
258 error_setg(errp, "CRYPTO header extension only "
259 "expected with LUKS encryption method");
260 return -EINVAL;
261 }
262 if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
263 error_setg(errp, "CRYPTO header extension size %u, "
264 "but expected size %zu", ext.len,
265 sizeof(Qcow2CryptoHeaderExtension));
266 return -EINVAL;
267 }
268
269 ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len);
270 if (ret < 0) {
271 error_setg_errno(errp, -ret,
272 "Unable to read CRYPTO header extension");
273 return ret;
274 }
275 be64_to_cpus(&s->crypto_header.offset);
276 be64_to_cpus(&s->crypto_header.length);
277
278 if ((s->crypto_header.offset % s->cluster_size) != 0) {
279 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
280 "not a multiple of cluster size '%u'",
281 s->crypto_header.offset, s->cluster_size);
282 return -EINVAL;
283 }
284
285 if (flags & BDRV_O_NO_IO) {
286 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
287 }
1cd9a787 288 s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
4652b8f3
DB
289 qcow2_crypto_hdr_read_func,
290 bs, cflags, errp);
291 if (!s->crypto) {
292 return -EINVAL;
293 }
294 } break;
295
88ddffae
VSO
296 case QCOW2_EXT_MAGIC_BITMAPS:
297 if (ext.len != sizeof(bitmaps_ext)) {
298 error_setg_errno(errp, -ret, "bitmaps_ext: "
299 "Invalid extension length");
300 return -EINVAL;
301 }
302
303 if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
304 error_report("WARNING: a program lacking bitmap support "
305 "modified this file, so all bitmaps are now "
306 "considered inconsistent. Some clusters may be "
307 "leaked, run 'qemu-img check -r' on the image "
308 "file to fix.");
309 if (need_update_header != NULL) {
310 /* Updating is needed to drop invalid bitmap extension. */
311 *need_update_header = true;
312 }
313 break;
314 }
315
316 ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
317 if (ret < 0) {
318 error_setg_errno(errp, -ret, "bitmaps_ext: "
319 "Could not read ext header");
320 return ret;
321 }
322
323 if (bitmaps_ext.reserved32 != 0) {
324 error_setg_errno(errp, -ret, "bitmaps_ext: "
325 "Reserved field is not zero");
326 return -EINVAL;
327 }
328
329 be32_to_cpus(&bitmaps_ext.nb_bitmaps);
330 be64_to_cpus(&bitmaps_ext.bitmap_directory_size);
331 be64_to_cpus(&bitmaps_ext.bitmap_directory_offset);
332
333 if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
334 error_setg(errp,
335 "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
336 "exceeding the QEMU supported maximum of %d",
337 bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
338 return -EINVAL;
339 }
340
341 if (bitmaps_ext.nb_bitmaps == 0) {
342 error_setg(errp, "found bitmaps extension with zero bitmaps");
343 return -EINVAL;
344 }
345
346 if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
347 error_setg(errp, "bitmaps_ext: "
348 "invalid bitmap directory offset");
349 return -EINVAL;
350 }
351
352 if (bitmaps_ext.bitmap_directory_size >
353 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
354 error_setg(errp, "bitmaps_ext: "
355 "bitmap directory size (%" PRIu64 ") exceeds "
356 "the maximum supported size (%d)",
357 bitmaps_ext.bitmap_directory_size,
358 QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
359 return -EINVAL;
360 }
361
362 s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
363 s->bitmap_directory_offset =
364 bitmaps_ext.bitmap_directory_offset;
365 s->bitmap_directory_size =
366 bitmaps_ext.bitmap_directory_size;
367
368#ifdef DEBUG_EXT
369 printf("Qcow2: Got bitmaps extension: "
370 "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
371 s->bitmap_directory_offset, s->nb_bitmaps);
372#endif
373 break;
374
9b80ddf3 375 default:
75bab85c
KW
376 /* unknown magic - save it in case we need to rewrite the header */
377 {
378 Qcow2UnknownHeaderExtension *uext;
379
380 uext = g_malloc0(sizeof(*uext) + ext.len);
381 uext->magic = ext.magic;
382 uext->len = ext.len;
383 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
384
cf2ab8fc 385 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
75bab85c 386 if (ret < 0) {
3ef6c40a
HR
387 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
388 "Could not read data");
75bab85c
KW
389 return ret;
390 }
75bab85c 391 }
9b80ddf3
AL
392 break;
393 }
fd29b4bb
KW
394
395 offset += ((ext.len + 7) & ~7);
9b80ddf3
AL
396 }
397
398 return 0;
399}
400
75bab85c
KW
401static void cleanup_unknown_header_ext(BlockDriverState *bs)
402{
ff99129a 403 BDRVQcow2State *s = bs->opaque;
75bab85c
KW
404 Qcow2UnknownHeaderExtension *uext, *next;
405
406 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
407 QLIST_REMOVE(uext, next);
408 g_free(uext);
409 }
410}
9b80ddf3 411
a55448b3
HR
412static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
413 uint64_t mask)
cfcc4c62 414{
12ac6d3d
KW
415 char *features = g_strdup("");
416 char *old;
417
cfcc4c62
KW
418 while (table && table->name[0] != '\0') {
419 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
12ac6d3d
KW
420 if (mask & (1ULL << table->bit)) {
421 old = features;
422 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
423 table->name);
424 g_free(old);
425 mask &= ~(1ULL << table->bit);
cfcc4c62
KW
426 }
427 }
428 table++;
429 }
430
431 if (mask) {
12ac6d3d
KW
432 old = features;
433 features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
434 old, *old ? ", " : "", mask);
435 g_free(old);
cfcc4c62 436 }
12ac6d3d 437
a55448b3 438 error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
12ac6d3d 439 g_free(features);
cfcc4c62
KW
440}
441
bfe8043e
SH
442/*
443 * Sets the dirty bit and flushes afterwards if necessary.
444 *
445 * The incompatible_features bit is only set if the image file header was
446 * updated successfully. Therefore it is not required to check the return
447 * value of this function.
448 */
280d3735 449int qcow2_mark_dirty(BlockDriverState *bs)
bfe8043e 450{
ff99129a 451 BDRVQcow2State *s = bs->opaque;
bfe8043e
SH
452 uint64_t val;
453 int ret;
454
455 assert(s->qcow_version >= 3);
456
457 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
458 return 0; /* already dirty */
459 }
460
461 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
d9ca2ea2 462 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
bfe8043e
SH
463 &val, sizeof(val));
464 if (ret < 0) {
465 return ret;
466 }
9a4f4c31 467 ret = bdrv_flush(bs->file->bs);
bfe8043e
SH
468 if (ret < 0) {
469 return ret;
470 }
471
472 /* Only treat image as dirty if the header was updated successfully */
473 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
474 return 0;
475}
476
c61d0004
SH
477/*
478 * Clears the dirty bit and flushes before if necessary. Only call this
479 * function when there are no pending requests, it does not guard against
480 * concurrent requests dirtying the image.
481 */
482static int qcow2_mark_clean(BlockDriverState *bs)
483{
ff99129a 484 BDRVQcow2State *s = bs->opaque;
c61d0004
SH
485
486 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
4c2e5f8f
KW
487 int ret;
488
489 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
490
491 ret = bdrv_flush(bs);
c61d0004
SH
492 if (ret < 0) {
493 return ret;
494 }
495
c61d0004
SH
496 return qcow2_update_header(bs);
497 }
498 return 0;
499}
500
69c98726
HR
501/*
502 * Marks the image as corrupt.
503 */
504int qcow2_mark_corrupt(BlockDriverState *bs)
505{
ff99129a 506 BDRVQcow2State *s = bs->opaque;
69c98726
HR
507
508 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
509 return qcow2_update_header(bs);
510}
511
512/*
513 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
514 * before if necessary.
515 */
516int qcow2_mark_consistent(BlockDriverState *bs)
517{
ff99129a 518 BDRVQcow2State *s = bs->opaque;
69c98726
HR
519
520 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
521 int ret = bdrv_flush(bs);
522 if (ret < 0) {
523 return ret;
524 }
525
526 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
527 return qcow2_update_header(bs);
528 }
529 return 0;
530}
531
acbe5982
SH
532static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
533 BdrvCheckMode fix)
534{
535 int ret = qcow2_check_refcounts(bs, result, fix);
536 if (ret < 0) {
537 return ret;
538 }
539
540 if (fix && result->check_errors == 0 && result->corruptions == 0) {
24530f3e
HR
541 ret = qcow2_mark_clean(bs);
542 if (ret < 0) {
543 return ret;
544 }
545 return qcow2_mark_consistent(bs);
acbe5982
SH
546 }
547 return ret;
548}
549
8c7de283
KW
550static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
551 uint64_t entries, size_t entry_len)
552{
ff99129a 553 BDRVQcow2State *s = bs->opaque;
8c7de283
KW
554 uint64_t size;
555
556 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
557 * because values will be passed to qemu functions taking int64_t. */
558 if (entries > INT64_MAX / entry_len) {
559 return -EINVAL;
560 }
561
562 size = entries * entry_len;
563
564 if (INT64_MAX - size < offset) {
565 return -EINVAL;
566 }
567
568 /* Tables must be cluster aligned */
24990c5b 569 if (offset_into_cluster(s, offset) != 0) {
8c7de283
KW
570 return -EINVAL;
571 }
572
573 return 0;
574}
575
74c4510a
KW
576static QemuOptsList qcow2_runtime_opts = {
577 .name = "qcow2",
578 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
579 .desc = {
580 {
64aa99d3 581 .name = QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
582 .type = QEMU_OPT_BOOL,
583 .help = "Postpone refcount updates",
584 },
67af674e
KW
585 {
586 .name = QCOW2_OPT_DISCARD_REQUEST,
587 .type = QEMU_OPT_BOOL,
588 .help = "Pass guest discard requests to the layer below",
589 },
590 {
591 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
592 .type = QEMU_OPT_BOOL,
593 .help = "Generate discard requests when snapshot related space "
594 "is freed",
595 },
596 {
597 .name = QCOW2_OPT_DISCARD_OTHER,
598 .type = QEMU_OPT_BOOL,
599 .help = "Generate discard requests when other clusters are freed",
600 },
05de7e86
HR
601 {
602 .name = QCOW2_OPT_OVERLAP,
603 .type = QEMU_OPT_STRING,
604 .help = "Selects which overlap checks to perform from a range of "
605 "templates (none, constant, cached, all)",
606 },
ee42b5ce
HR
607 {
608 .name = QCOW2_OPT_OVERLAP_TEMPLATE,
609 .type = QEMU_OPT_STRING,
610 .help = "Selects which overlap checks to perform from a range of "
611 "templates (none, constant, cached, all)",
612 },
05de7e86
HR
613 {
614 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
615 .type = QEMU_OPT_BOOL,
616 .help = "Check for unintended writes into the main qcow2 header",
617 },
618 {
619 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
620 .type = QEMU_OPT_BOOL,
621 .help = "Check for unintended writes into the active L1 table",
622 },
623 {
624 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
625 .type = QEMU_OPT_BOOL,
626 .help = "Check for unintended writes into an active L2 table",
627 },
628 {
629 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
630 .type = QEMU_OPT_BOOL,
631 .help = "Check for unintended writes into the refcount table",
632 },
633 {
634 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
635 .type = QEMU_OPT_BOOL,
636 .help = "Check for unintended writes into a refcount block",
637 },
638 {
639 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
640 .type = QEMU_OPT_BOOL,
641 .help = "Check for unintended writes into the snapshot table",
642 },
643 {
644 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
645 .type = QEMU_OPT_BOOL,
646 .help = "Check for unintended writes into an inactive L1 table",
647 },
648 {
649 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
650 .type = QEMU_OPT_BOOL,
651 .help = "Check for unintended writes into an inactive L2 table",
652 },
6c1c8d5d
HR
653 {
654 .name = QCOW2_OPT_CACHE_SIZE,
655 .type = QEMU_OPT_SIZE,
656 .help = "Maximum combined metadata (L2 tables and refcount blocks) "
657 "cache size",
658 },
659 {
660 .name = QCOW2_OPT_L2_CACHE_SIZE,
661 .type = QEMU_OPT_SIZE,
662 .help = "Maximum L2 table cache size",
663 },
664 {
665 .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
666 .type = QEMU_OPT_SIZE,
667 .help = "Maximum refcount block cache size",
668 },
279621c0
AG
669 {
670 .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
671 .type = QEMU_OPT_NUMBER,
672 .help = "Clean unused cache entries after this time (in seconds)",
673 },
4652b8f3
DB
674 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
675 "ID of secret providing qcow2 AES key or LUKS passphrase"),
74c4510a
KW
676 { /* end of list */ }
677 },
678};
679
4092e99d
HR
680static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
681 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
682 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
683 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
684 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
685 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
686 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
687 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
688 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
689};
690
279621c0
AG
691static void cache_clean_timer_cb(void *opaque)
692{
693 BlockDriverState *bs = opaque;
ff99129a 694 BDRVQcow2State *s = bs->opaque;
279621c0
AG
695 qcow2_cache_clean_unused(bs, s->l2_table_cache);
696 qcow2_cache_clean_unused(bs, s->refcount_block_cache);
697 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
698 (int64_t) s->cache_clean_interval * 1000);
699}
700
701static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
702{
ff99129a 703 BDRVQcow2State *s = bs->opaque;
279621c0
AG
704 if (s->cache_clean_interval > 0) {
705 s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL,
706 SCALE_MS, cache_clean_timer_cb,
707 bs);
708 timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
709 (int64_t) s->cache_clean_interval * 1000);
710 }
711}
712
713static void cache_clean_timer_del(BlockDriverState *bs)
714{
ff99129a 715 BDRVQcow2State *s = bs->opaque;
279621c0
AG
716 if (s->cache_clean_timer) {
717 timer_del(s->cache_clean_timer);
718 timer_free(s->cache_clean_timer);
719 s->cache_clean_timer = NULL;
720 }
721}
722
723static void qcow2_detach_aio_context(BlockDriverState *bs)
724{
725 cache_clean_timer_del(bs);
726}
727
728static void qcow2_attach_aio_context(BlockDriverState *bs,
729 AioContext *new_context)
730{
731 cache_clean_timer_init(bs, new_context);
732}
733
bc85ef26
HR
734static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
735 uint64_t *l2_cache_size,
6c1c8d5d
HR
736 uint64_t *refcount_cache_size, Error **errp)
737{
ff99129a 738 BDRVQcow2State *s = bs->opaque;
6c1c8d5d
HR
739 uint64_t combined_cache_size;
740 bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
741
742 combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
743 l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
744 refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
745
746 combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
747 *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
748 *refcount_cache_size = qemu_opt_get_size(opts,
749 QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
750
751 if (combined_cache_size_set) {
752 if (l2_cache_size_set && refcount_cache_size_set) {
753 error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
754 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
755 "the same time");
756 return;
757 } else if (*l2_cache_size > combined_cache_size) {
758 error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
759 QCOW2_OPT_CACHE_SIZE);
760 return;
761 } else if (*refcount_cache_size > combined_cache_size) {
762 error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
763 QCOW2_OPT_CACHE_SIZE);
764 return;
765 }
766
767 if (l2_cache_size_set) {
768 *refcount_cache_size = combined_cache_size - *l2_cache_size;
769 } else if (refcount_cache_size_set) {
770 *l2_cache_size = combined_cache_size - *refcount_cache_size;
771 } else {
772 *refcount_cache_size = combined_cache_size
773 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1);
774 *l2_cache_size = combined_cache_size - *refcount_cache_size;
775 }
776 } else {
777 if (!l2_cache_size_set && !refcount_cache_size_set) {
bc85ef26
HR
778 *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
779 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
780 * s->cluster_size);
6c1c8d5d
HR
781 *refcount_cache_size = *l2_cache_size
782 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
783 } else if (!l2_cache_size_set) {
784 *l2_cache_size = *refcount_cache_size
785 * DEFAULT_L2_REFCOUNT_SIZE_RATIO;
786 } else if (!refcount_cache_size_set) {
787 *refcount_cache_size = *l2_cache_size
788 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
789 }
790 }
791}
792
ee55b173
KW
793typedef struct Qcow2ReopenState {
794 Qcow2Cache *l2_table_cache;
795 Qcow2Cache *refcount_block_cache;
796 bool use_lazy_refcounts;
797 int overlap_check;
798 bool discard_passthrough[QCOW2_DISCARD_MAX];
799 uint64_t cache_clean_interval;
b25b387f 800 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
ee55b173
KW
801} Qcow2ReopenState;
802
803static int qcow2_update_options_prepare(BlockDriverState *bs,
804 Qcow2ReopenState *r,
805 QDict *options, int flags,
806 Error **errp)
4c75d1a1
KW
807{
808 BDRVQcow2State *s = bs->opaque;
94edf3fb 809 QemuOpts *opts = NULL;
4c75d1a1
KW
810 const char *opt_overlap_check, *opt_overlap_check_template;
811 int overlap_check_template = 0;
94edf3fb 812 uint64_t l2_cache_size, refcount_cache_size;
4c75d1a1 813 int i;
b25b387f
DB
814 const char *encryptfmt;
815 QDict *encryptopts = NULL;
94edf3fb 816 Error *local_err = NULL;
4c75d1a1
KW
817 int ret;
818
b25b387f
DB
819 qdict_extract_subqdict(options, &encryptopts, "encrypt.");
820 encryptfmt = qdict_get_try_str(encryptopts, "format");
821
94edf3fb
KW
822 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
823 qemu_opts_absorb_qdict(opts, options, &local_err);
824 if (local_err) {
825 error_propagate(errp, local_err);
826 ret = -EINVAL;
827 goto fail;
828 }
829
830 /* get L2 table/refcount block cache size from command line options */
831 read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size,
832 &local_err);
833 if (local_err) {
834 error_propagate(errp, local_err);
835 ret = -EINVAL;
836 goto fail;
837 }
838
839 l2_cache_size /= s->cluster_size;
840 if (l2_cache_size < MIN_L2_CACHE_SIZE) {
841 l2_cache_size = MIN_L2_CACHE_SIZE;
842 }
843 if (l2_cache_size > INT_MAX) {
844 error_setg(errp, "L2 cache size too big");
845 ret = -EINVAL;
846 goto fail;
847 }
848
849 refcount_cache_size /= s->cluster_size;
850 if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
851 refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
852 }
853 if (refcount_cache_size > INT_MAX) {
854 error_setg(errp, "Refcount cache size too big");
855 ret = -EINVAL;
856 goto fail;
857 }
858
5b0959a7
KW
859 /* alloc new L2 table/refcount block cache, flush old one */
860 if (s->l2_table_cache) {
861 ret = qcow2_cache_flush(bs, s->l2_table_cache);
862 if (ret) {
863 error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
864 goto fail;
865 }
866 }
867
868 if (s->refcount_block_cache) {
869 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
870 if (ret) {
871 error_setg_errno(errp, -ret,
872 "Failed to flush the refcount block cache");
873 goto fail;
874 }
875 }
876
ee55b173
KW
877 r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size);
878 r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size);
879 if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
94edf3fb
KW
880 error_setg(errp, "Could not allocate metadata caches");
881 ret = -ENOMEM;
882 goto fail;
883 }
884
885 /* New interval for cache cleanup timer */
ee55b173 886 r->cache_clean_interval =
5b0959a7
KW
887 qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
888 s->cache_clean_interval);
91203f08
AG
889#ifndef CONFIG_LINUX
890 if (r->cache_clean_interval != 0) {
891 error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
892 " not supported on this host");
893 ret = -EINVAL;
894 goto fail;
895 }
896#endif
ee55b173 897 if (r->cache_clean_interval > UINT_MAX) {
94edf3fb
KW
898 error_setg(errp, "Cache clean interval too big");
899 ret = -EINVAL;
900 goto fail;
901 }
94edf3fb 902
5b0959a7 903 /* lazy-refcounts; flush if going from enabled to disabled */
ee55b173 904 r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
4c75d1a1 905 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
ee55b173 906 if (r->use_lazy_refcounts && s->qcow_version < 3) {
007dbc39
KW
907 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
908 "qemu 1.1 compatibility level");
909 ret = -EINVAL;
910 goto fail;
911 }
4c75d1a1 912
5b0959a7
KW
913 if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
914 ret = qcow2_mark_clean(bs);
915 if (ret < 0) {
916 error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
917 goto fail;
918 }
919 }
920
007dbc39 921 /* Overlap check options */
4c75d1a1
KW
922 opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
923 opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
924 if (opt_overlap_check_template && opt_overlap_check &&
925 strcmp(opt_overlap_check_template, opt_overlap_check))
926 {
927 error_setg(errp, "Conflicting values for qcow2 options '"
928 QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
929 "' ('%s')", opt_overlap_check, opt_overlap_check_template);
930 ret = -EINVAL;
931 goto fail;
932 }
933 if (!opt_overlap_check) {
934 opt_overlap_check = opt_overlap_check_template ?: "cached";
935 }
936
937 if (!strcmp(opt_overlap_check, "none")) {
938 overlap_check_template = 0;
939 } else if (!strcmp(opt_overlap_check, "constant")) {
940 overlap_check_template = QCOW2_OL_CONSTANT;
941 } else if (!strcmp(opt_overlap_check, "cached")) {
942 overlap_check_template = QCOW2_OL_CACHED;
943 } else if (!strcmp(opt_overlap_check, "all")) {
944 overlap_check_template = QCOW2_OL_ALL;
945 } else {
946 error_setg(errp, "Unsupported value '%s' for qcow2 option "
947 "'overlap-check'. Allowed are any of the following: "
948 "none, constant, cached, all", opt_overlap_check);
949 ret = -EINVAL;
950 goto fail;
951 }
952
ee55b173 953 r->overlap_check = 0;
4c75d1a1
KW
954 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
955 /* overlap-check defines a template bitmask, but every flag may be
956 * overwritten through the associated boolean option */
ee55b173 957 r->overlap_check |=
4c75d1a1
KW
958 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
959 overlap_check_template & (1 << i)) << i;
960 }
961
ee55b173
KW
962 r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
963 r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
964 r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
007dbc39
KW
965 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
966 flags & BDRV_O_UNMAP);
ee55b173 967 r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
007dbc39 968 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
ee55b173 969 r->discard_passthrough[QCOW2_DISCARD_OTHER] =
007dbc39
KW
970 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
971
b25b387f
DB
972 switch (s->crypt_method_header) {
973 case QCOW_CRYPT_NONE:
974 if (encryptfmt) {
975 error_setg(errp, "No encryption in image header, but options "
976 "specified format '%s'", encryptfmt);
977 ret = -EINVAL;
978 goto fail;
979 }
980 break;
981
982 case QCOW_CRYPT_AES:
983 if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
984 error_setg(errp,
985 "Header reported 'aes' encryption format but "
986 "options specify '%s'", encryptfmt);
987 ret = -EINVAL;
988 goto fail;
989 }
990 qdict_del(encryptopts, "format");
991 r->crypto_opts = block_crypto_open_opts_init(
992 Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
993 break;
994
4652b8f3
DB
995 case QCOW_CRYPT_LUKS:
996 if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
997 error_setg(errp,
998 "Header reported 'luks' encryption format but "
999 "options specify '%s'", encryptfmt);
1000 ret = -EINVAL;
1001 goto fail;
1002 }
1003 qdict_del(encryptopts, "format");
1004 r->crypto_opts = block_crypto_open_opts_init(
1005 Q_CRYPTO_BLOCK_FORMAT_LUKS, encryptopts, errp);
1006 break;
1007
b25b387f
DB
1008 default:
1009 error_setg(errp, "Unsupported encryption method %d",
1010 s->crypt_method_header);
1011 break;
1012 }
1013 if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) {
1014 ret = -EINVAL;
1015 goto fail;
1016 }
1017
4c75d1a1
KW
1018 ret = 0;
1019fail:
b25b387f 1020 QDECREF(encryptopts);
94edf3fb
KW
1021 qemu_opts_del(opts);
1022 opts = NULL;
ee55b173
KW
1023 return ret;
1024}
1025
1026static void qcow2_update_options_commit(BlockDriverState *bs,
1027 Qcow2ReopenState *r)
1028{
1029 BDRVQcow2State *s = bs->opaque;
1030 int i;
1031
5b0959a7
KW
1032 if (s->l2_table_cache) {
1033 qcow2_cache_destroy(bs, s->l2_table_cache);
1034 }
1035 if (s->refcount_block_cache) {
1036 qcow2_cache_destroy(bs, s->refcount_block_cache);
1037 }
ee55b173
KW
1038 s->l2_table_cache = r->l2_table_cache;
1039 s->refcount_block_cache = r->refcount_block_cache;
1040
1041 s->overlap_check = r->overlap_check;
1042 s->use_lazy_refcounts = r->use_lazy_refcounts;
1043
1044 for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1045 s->discard_passthrough[i] = r->discard_passthrough[i];
1046 }
1047
5b0959a7
KW
1048 if (s->cache_clean_interval != r->cache_clean_interval) {
1049 cache_clean_timer_del(bs);
1050 s->cache_clean_interval = r->cache_clean_interval;
1051 cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1052 }
b25b387f
DB
1053
1054 qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1055 s->crypto_opts = r->crypto_opts;
ee55b173
KW
1056}
1057
1058static void qcow2_update_options_abort(BlockDriverState *bs,
1059 Qcow2ReopenState *r)
1060{
1061 if (r->l2_table_cache) {
1062 qcow2_cache_destroy(bs, r->l2_table_cache);
1063 }
1064 if (r->refcount_block_cache) {
1065 qcow2_cache_destroy(bs, r->refcount_block_cache);
1066 }
b25b387f 1067 qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
ee55b173
KW
1068}
1069
1070static int qcow2_update_options(BlockDriverState *bs, QDict *options,
1071 int flags, Error **errp)
1072{
1073 Qcow2ReopenState r = {};
1074 int ret;
1075
1076 ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1077 if (ret >= 0) {
1078 qcow2_update_options_commit(bs, &r);
1079 } else {
1080 qcow2_update_options_abort(bs, &r);
1081 }
94edf3fb 1082
4c75d1a1
KW
1083 return ret;
1084}
1085
4e4bf5c4
KW
1086static int qcow2_do_open(BlockDriverState *bs, QDict *options, int flags,
1087 Error **errp)
585f8587 1088{
ff99129a 1089 BDRVQcow2State *s = bs->opaque;
6d33e8e7
KW
1090 unsigned int len, i;
1091 int ret = 0;
585f8587 1092 QCowHeader header;
74c4510a 1093 Error *local_err = NULL;
9b80ddf3 1094 uint64_t ext_end;
2cf7cfa1 1095 uint64_t l1_vm_state_index;
88ddffae 1096 bool update_header = false;
585f8587 1097
cf2ab8fc 1098 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
6d85a57e 1099 if (ret < 0) {
3ef6c40a 1100 error_setg_errno(errp, -ret, "Could not read qcow2 header");
585f8587 1101 goto fail;
6d85a57e 1102 }
585f8587
FB
1103 be32_to_cpus(&header.magic);
1104 be32_to_cpus(&header.version);
1105 be64_to_cpus(&header.backing_file_offset);
1106 be32_to_cpus(&header.backing_file_size);
1107 be64_to_cpus(&header.size);
1108 be32_to_cpus(&header.cluster_bits);
1109 be32_to_cpus(&header.crypt_method);
1110 be64_to_cpus(&header.l1_table_offset);
1111 be32_to_cpus(&header.l1_size);
1112 be64_to_cpus(&header.refcount_table_offset);
1113 be32_to_cpus(&header.refcount_table_clusters);
1114 be64_to_cpus(&header.snapshots_offset);
1115 be32_to_cpus(&header.nb_snapshots);
3b46e624 1116
e8cdcec1 1117 if (header.magic != QCOW_MAGIC) {
3ef6c40a 1118 error_setg(errp, "Image is not in qcow2 format");
76abe407 1119 ret = -EINVAL;
585f8587 1120 goto fail;
6d85a57e 1121 }
6744cbab 1122 if (header.version < 2 || header.version > 3) {
a55448b3 1123 error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
6744cbab
KW
1124 ret = -ENOTSUP;
1125 goto fail;
1126 }
1127
1128 s->qcow_version = header.version;
1129
24342f2c
KW
1130 /* Initialise cluster size */
1131 if (header.cluster_bits < MIN_CLUSTER_BITS ||
1132 header.cluster_bits > MAX_CLUSTER_BITS) {
521b2b5d
HR
1133 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1134 header.cluster_bits);
24342f2c
KW
1135 ret = -EINVAL;
1136 goto fail;
1137 }
1138
1139 s->cluster_bits = header.cluster_bits;
1140 s->cluster_size = 1 << s->cluster_bits;
1141 s->cluster_sectors = 1 << (s->cluster_bits - 9);
1142
6744cbab
KW
1143 /* Initialise version 3 header fields */
1144 if (header.version == 2) {
1145 header.incompatible_features = 0;
1146 header.compatible_features = 0;
1147 header.autoclear_features = 0;
1148 header.refcount_order = 4;
1149 header.header_length = 72;
1150 } else {
1151 be64_to_cpus(&header.incompatible_features);
1152 be64_to_cpus(&header.compatible_features);
1153 be64_to_cpus(&header.autoclear_features);
1154 be32_to_cpus(&header.refcount_order);
1155 be32_to_cpus(&header.header_length);
24342f2c
KW
1156
1157 if (header.header_length < 104) {
1158 error_setg(errp, "qcow2 header too short");
1159 ret = -EINVAL;
1160 goto fail;
1161 }
1162 }
1163
1164 if (header.header_length > s->cluster_size) {
1165 error_setg(errp, "qcow2 header exceeds cluster size");
1166 ret = -EINVAL;
1167 goto fail;
6744cbab
KW
1168 }
1169
1170 if (header.header_length > sizeof(header)) {
1171 s->unknown_header_fields_size = header.header_length - sizeof(header);
1172 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
cf2ab8fc 1173 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
6744cbab
KW
1174 s->unknown_header_fields_size);
1175 if (ret < 0) {
3ef6c40a
HR
1176 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
1177 "fields");
6744cbab
KW
1178 goto fail;
1179 }
1180 }
1181
a1b3955c
KW
1182 if (header.backing_file_offset > s->cluster_size) {
1183 error_setg(errp, "Invalid backing file offset");
1184 ret = -EINVAL;
1185 goto fail;
1186 }
1187
cfcc4c62
KW
1188 if (header.backing_file_offset) {
1189 ext_end = header.backing_file_offset;
1190 } else {
1191 ext_end = 1 << header.cluster_bits;
1192 }
1193
6744cbab
KW
1194 /* Handle feature bits */
1195 s->incompatible_features = header.incompatible_features;
1196 s->compatible_features = header.compatible_features;
1197 s->autoclear_features = header.autoclear_features;
1198
c61d0004 1199 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
cfcc4c62
KW
1200 void *feature_table = NULL;
1201 qcow2_read_extensions(bs, header.header_length, ext_end,
88ddffae 1202 &feature_table, flags, NULL, NULL);
a55448b3 1203 report_unsupported_feature(errp, feature_table,
c61d0004
SH
1204 s->incompatible_features &
1205 ~QCOW2_INCOMPAT_MASK);
6744cbab 1206 ret = -ENOTSUP;
c5a33ee9 1207 g_free(feature_table);
6744cbab
KW
1208 goto fail;
1209 }
1210
69c98726
HR
1211 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
1212 /* Corrupt images may not be written to unless they are being repaired
1213 */
1214 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
3ef6c40a
HR
1215 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
1216 "read/write");
69c98726
HR
1217 ret = -EACCES;
1218 goto fail;
1219 }
1220 }
1221
6744cbab 1222 /* Check support for various header values */
b72faf9f
HR
1223 if (header.refcount_order > 6) {
1224 error_setg(errp, "Reference count entry width too large; may not "
1225 "exceed 64 bits");
1226 ret = -EINVAL;
e8cdcec1
KW
1227 goto fail;
1228 }
b6481f37 1229 s->refcount_order = header.refcount_order;
346a53df
HR
1230 s->refcount_bits = 1 << s->refcount_order;
1231 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1232 s->refcount_max += s->refcount_max - 1;
6744cbab 1233
585f8587 1234 s->crypt_method_header = header.crypt_method;
6d85a57e 1235 if (s->crypt_method_header) {
e6ff69bf
DB
1236 if (bdrv_uses_whitelist() &&
1237 s->crypt_method_header == QCOW_CRYPT_AES) {
8c0dcbc4
DB
1238 error_setg(errp,
1239 "Use of AES-CBC encrypted qcow2 images is no longer "
1240 "supported in system emulators");
1241 error_append_hint(errp,
1242 "You can use 'qemu-img convert' to convert your "
1243 "image to an alternative supported format, such "
1244 "as unencrypted qcow2, or raw with the LUKS "
1245 "format instead.\n");
1246 ret = -ENOSYS;
1247 goto fail;
e6ff69bf
DB
1248 }
1249
4652b8f3
DB
1250 if (s->crypt_method_header == QCOW_CRYPT_AES) {
1251 s->crypt_physical_offset = false;
1252 } else {
1253 /* Assuming LUKS and any future crypt methods we
1254 * add will all use physical offsets, due to the
1255 * fact that the alternative is insecure... */
1256 s->crypt_physical_offset = true;
1257 }
1258
54115412 1259 bs->encrypted = true;
6d85a57e 1260 }
24342f2c 1261
585f8587
FB
1262 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
1263 s->l2_size = 1 << s->l2_bits;
1d13d654
HR
1264 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1265 s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
1266 s->refcount_block_size = 1 << s->refcount_block_bits;
585f8587
FB
1267 bs->total_sectors = header.size / 512;
1268 s->csize_shift = (62 - (s->cluster_bits - 8));
1269 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1270 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
5dab2fad 1271
585f8587 1272 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 1273 s->refcount_table_size =
585f8587
FB
1274 header.refcount_table_clusters << (s->cluster_bits - 3);
1275
2b5d5953 1276 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
5dab2fad
KW
1277 error_setg(errp, "Reference count table too large");
1278 ret = -EINVAL;
1279 goto fail;
1280 }
1281
8c7de283
KW
1282 ret = validate_table_offset(bs, s->refcount_table_offset,
1283 s->refcount_table_size, sizeof(uint64_t));
1284 if (ret < 0) {
1285 error_setg(errp, "Invalid reference count table offset");
1286 goto fail;
1287 }
1288
ce48f2f4
KW
1289 /* Snapshot table offset/length */
1290 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
1291 error_setg(errp, "Too many snapshots");
1292 ret = -EINVAL;
1293 goto fail;
1294 }
1295
1296 ret = validate_table_offset(bs, header.snapshots_offset,
1297 header.nb_snapshots,
1298 sizeof(QCowSnapshotHeader));
1299 if (ret < 0) {
1300 error_setg(errp, "Invalid snapshot table offset");
1301 goto fail;
1302 }
1303
585f8587 1304 /* read the level 1 table */
87b86e7e 1305 if (header.l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
2d51c32c
KW
1306 error_setg(errp, "Active L1 table too large");
1307 ret = -EFBIG;
1308 goto fail;
1309 }
585f8587 1310 s->l1_size = header.l1_size;
2cf7cfa1
KW
1311
1312 l1_vm_state_index = size_to_l1(s, header.size);
1313 if (l1_vm_state_index > INT_MAX) {
3ef6c40a 1314 error_setg(errp, "Image is too big");
2cf7cfa1
KW
1315 ret = -EFBIG;
1316 goto fail;
1317 }
1318 s->l1_vm_state_index = l1_vm_state_index;
1319
585f8587
FB
1320 /* the L1 table must contain at least enough entries to put
1321 header.size bytes */
6d85a57e 1322 if (s->l1_size < s->l1_vm_state_index) {
3ef6c40a 1323 error_setg(errp, "L1 table is too small");
6d85a57e 1324 ret = -EINVAL;
585f8587 1325 goto fail;
6d85a57e 1326 }
2d51c32c
KW
1327
1328 ret = validate_table_offset(bs, header.l1_table_offset,
1329 header.l1_size, sizeof(uint64_t));
1330 if (ret < 0) {
1331 error_setg(errp, "Invalid L1 table offset");
1332 goto fail;
1333 }
585f8587 1334 s->l1_table_offset = header.l1_table_offset;
2d51c32c
KW
1335
1336
d191d12d 1337 if (s->l1_size > 0) {
9a4f4c31 1338 s->l1_table = qemu_try_blockalign(bs->file->bs,
d191d12d 1339 align_offset(s->l1_size * sizeof(uint64_t), 512));
de82815d
KW
1340 if (s->l1_table == NULL) {
1341 error_setg(errp, "Could not allocate L1 table");
1342 ret = -ENOMEM;
1343 goto fail;
1344 }
cf2ab8fc 1345 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
6d85a57e
JS
1346 s->l1_size * sizeof(uint64_t));
1347 if (ret < 0) {
3ef6c40a 1348 error_setg_errno(errp, -ret, "Could not read L1 table");
d191d12d 1349 goto fail;
6d85a57e 1350 }
d191d12d
SW
1351 for(i = 0;i < s->l1_size; i++) {
1352 be64_to_cpus(&s->l1_table[i]);
1353 }
585f8587 1354 }
29c1a730 1355
94edf3fb
KW
1356 /* Parse driver-specific options */
1357 ret = qcow2_update_options(bs, options, flags, errp);
90efa0ea
KW
1358 if (ret < 0) {
1359 goto fail;
1360 }
1361
585f8587 1362 s->cluster_cache_offset = -1;
06d9260f 1363 s->flags = flags;
3b46e624 1364
6d85a57e
JS
1365 ret = qcow2_refcount_init(bs);
1366 if (ret != 0) {
3ef6c40a 1367 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
585f8587 1368 goto fail;
6d85a57e 1369 }
585f8587 1370
72cf2d4f 1371 QLIST_INIT(&s->cluster_allocs);
0b919fae 1372 QTAILQ_INIT(&s->discards);
f214978a 1373
9b80ddf3 1374 /* read qcow2 extensions */
3ef6c40a 1375 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
88ddffae 1376 flags, &update_header, &local_err)) {
3ef6c40a 1377 error_propagate(errp, local_err);
6d85a57e 1378 ret = -EINVAL;
9b80ddf3 1379 goto fail;
6d85a57e 1380 }
9b80ddf3 1381
4652b8f3
DB
1382 /* qcow2_read_extension may have set up the crypto context
1383 * if the crypt method needs a header region, some methods
1384 * don't need header extensions, so must check here
1385 */
1386 if (s->crypt_method_header && !s->crypto) {
1387 if (s->crypt_method_header == QCOW_CRYPT_AES) {
1388 unsigned int cflags = 0;
1389 if (flags & BDRV_O_NO_IO) {
1390 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1391 }
1cd9a787
DB
1392 s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
1393 NULL, NULL, cflags, errp);
4652b8f3
DB
1394 if (!s->crypto) {
1395 ret = -EINVAL;
1396 goto fail;
1397 }
1398 } else if (!(flags & BDRV_O_NO_IO)) {
1399 error_setg(errp, "Missing CRYPTO header for crypt method %d",
1400 s->crypt_method_header);
b25b387f
DB
1401 ret = -EINVAL;
1402 goto fail;
1403 }
1404 }
1405
585f8587
FB
1406 /* read the backing file name */
1407 if (header.backing_file_offset != 0) {
1408 len = header.backing_file_size;
9a29e18f 1409 if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
e729fa6a 1410 len >= sizeof(bs->backing_file)) {
6d33e8e7
KW
1411 error_setg(errp, "Backing file name too long");
1412 ret = -EINVAL;
1413 goto fail;
6d85a57e 1414 }
cf2ab8fc 1415 ret = bdrv_pread(bs->file, header.backing_file_offset,
6d85a57e
JS
1416 bs->backing_file, len);
1417 if (ret < 0) {
3ef6c40a 1418 error_setg_errno(errp, -ret, "Could not read backing file name");
585f8587 1419 goto fail;
6d85a57e 1420 }
585f8587 1421 bs->backing_file[len] = '\0';
e4603fe1 1422 s->image_backing_file = g_strdup(bs->backing_file);
585f8587 1423 }
42deb29f 1424
11b128f4
KW
1425 /* Internal snapshots */
1426 s->snapshots_offset = header.snapshots_offset;
1427 s->nb_snapshots = header.nb_snapshots;
1428
42deb29f
KW
1429 ret = qcow2_read_snapshots(bs);
1430 if (ret < 0) {
3ef6c40a 1431 error_setg_errno(errp, -ret, "Could not read snapshots");
585f8587 1432 goto fail;
6d85a57e 1433 }
585f8587 1434
af7b708d 1435 /* Clear unknown autoclear feature bits */
88ddffae 1436 update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
d1258dd0
VSO
1437 update_header =
1438 update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
1439 if (update_header) {
88ddffae 1440 s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
d1258dd0
VSO
1441 }
1442
1443 if (qcow2_load_autoloading_dirty_bitmaps(bs, &local_err)) {
1444 update_header = false;
1445 }
1446 if (local_err != NULL) {
1447 error_propagate(errp, local_err);
1448 ret = -EINVAL;
1449 goto fail;
1450 }
1451
1452 if (update_header) {
af7b708d
SH
1453 ret = qcow2_update_header(bs);
1454 if (ret < 0) {
3ef6c40a 1455 error_setg_errno(errp, -ret, "Could not update qcow2 header");
af7b708d
SH
1456 goto fail;
1457 }
1458 }
1459
68d100e9
KW
1460 /* Initialise locks */
1461 qemu_co_mutex_init(&s->lock);
170f4b2e 1462 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
68d100e9 1463
c61d0004 1464 /* Repair image if dirty */
04c01a5c 1465 if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
058f8f16 1466 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
c61d0004
SH
1467 BdrvCheckResult result = {0};
1468
5b84106b 1469 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
c61d0004 1470 if (ret < 0) {
3ef6c40a 1471 error_setg_errno(errp, -ret, "Could not repair dirty image");
c61d0004
SH
1472 goto fail;
1473 }
1474 }
1475
585f8587 1476#ifdef DEBUG_ALLOC
6cbc3031
PH
1477 {
1478 BdrvCheckResult result = {0};
b35278f7 1479 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 1480 }
585f8587 1481#endif
6d85a57e 1482 return ret;
585f8587
FB
1483
1484 fail:
6744cbab 1485 g_free(s->unknown_header_fields);
75bab85c 1486 cleanup_unknown_header_ext(bs);
ed6ccf0f
KW
1487 qcow2_free_snapshots(bs);
1488 qcow2_refcount_close(bs);
de82815d 1489 qemu_vfree(s->l1_table);
cf93980e
HR
1490 /* else pre-write overlap checks in cache_destroy may crash */
1491 s->l1_table = NULL;
279621c0 1492 cache_clean_timer_del(bs);
29c1a730
KW
1493 if (s->l2_table_cache) {
1494 qcow2_cache_destroy(bs, s->l2_table_cache);
1495 }
c5a33ee9
PJ
1496 if (s->refcount_block_cache) {
1497 qcow2_cache_destroy(bs, s->refcount_block_cache);
1498 }
b25b387f
DB
1499 qcrypto_block_free(s->crypto);
1500 qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
6d85a57e 1501 return ret;
585f8587
FB
1502}
1503
4e4bf5c4
KW
1504static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
1505 Error **errp)
1506{
1507 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
1508 false, errp);
1509 if (!bs->file) {
1510 return -EINVAL;
1511 }
1512
1513 return qcow2_do_open(bs, options, flags, errp);
1514}
1515
3baca891 1516static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
d34682cd 1517{
ff99129a 1518 BDRVQcow2State *s = bs->opaque;
d34682cd 1519
a84178cc
EB
1520 if (bs->encrypted) {
1521 /* Encryption works on a sector granularity */
a5b8dd2c 1522 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
a84178cc 1523 }
cf081fca 1524 bs->bl.pwrite_zeroes_alignment = s->cluster_size;
ecdbead6 1525 bs->bl.pdiscard_alignment = s->cluster_size;
d34682cd
KW
1526}
1527
21d82ac9
JC
1528static int qcow2_reopen_prepare(BDRVReopenState *state,
1529 BlockReopenQueue *queue, Error **errp)
1530{
5b0959a7 1531 Qcow2ReopenState *r;
4c2e5f8f
KW
1532 int ret;
1533
5b0959a7
KW
1534 r = g_new0(Qcow2ReopenState, 1);
1535 state->opaque = r;
1536
1537 ret = qcow2_update_options_prepare(state->bs, r, state->options,
1538 state->flags, errp);
1539 if (ret < 0) {
1540 goto fail;
1541 }
1542
1543 /* We need to write out any unwritten data if we reopen read-only. */
4c2e5f8f 1544 if ((state->flags & BDRV_O_RDWR) == 0) {
169b8793
VSO
1545 ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
1546 if (ret < 0) {
1547 goto fail;
1548 }
1549
4c2e5f8f
KW
1550 ret = bdrv_flush(state->bs);
1551 if (ret < 0) {
5b0959a7 1552 goto fail;
4c2e5f8f
KW
1553 }
1554
1555 ret = qcow2_mark_clean(state->bs);
1556 if (ret < 0) {
5b0959a7 1557 goto fail;
4c2e5f8f
KW
1558 }
1559 }
1560
21d82ac9 1561 return 0;
5b0959a7
KW
1562
1563fail:
1564 qcow2_update_options_abort(state->bs, r);
1565 g_free(r);
1566 return ret;
1567}
1568
1569static void qcow2_reopen_commit(BDRVReopenState *state)
1570{
1571 qcow2_update_options_commit(state->bs, state->opaque);
1572 g_free(state->opaque);
1573}
1574
1575static void qcow2_reopen_abort(BDRVReopenState *state)
1576{
1577 qcow2_update_options_abort(state->bs, state->opaque);
1578 g_free(state->opaque);
21d82ac9
JC
1579}
1580
5365f44d
KW
1581static void qcow2_join_options(QDict *options, QDict *old_options)
1582{
1583 bool has_new_overlap_template =
1584 qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
1585 qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
1586 bool has_new_total_cache_size =
1587 qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
1588 bool has_all_cache_options;
1589
1590 /* New overlap template overrides all old overlap options */
1591 if (has_new_overlap_template) {
1592 qdict_del(old_options, QCOW2_OPT_OVERLAP);
1593 qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
1594 qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
1595 qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
1596 qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
1597 qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
1598 qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
1599 qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
1600 qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
1601 qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
1602 }
1603
1604 /* New total cache size overrides all old options */
1605 if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
1606 qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
1607 qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1608 }
1609
1610 qdict_join(options, old_options, false);
1611
1612 /*
1613 * If after merging all cache size options are set, an old total size is
1614 * overwritten. Do keep all options, however, if all three are new. The
1615 * resulting error message is what we want to happen.
1616 */
1617 has_all_cache_options =
1618 qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
1619 qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
1620 qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
1621
1622 if (has_all_cache_options && !has_new_total_cache_size) {
1623 qdict_del(options, QCOW2_OPT_CACHE_SIZE);
1624 }
1625}
1626
b6b8a333 1627static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
67a0fd2a 1628 int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
585f8587 1629{
ff99129a 1630 BDRVQcow2State *s = bs->opaque;
585f8587 1631 uint64_t cluster_offset;
4bc74be9 1632 int index_in_cluster, ret;
ecfe1863 1633 unsigned int bytes;
4bc74be9 1634 int64_t status = 0;
585f8587 1635
ecfe1863 1636 bytes = MIN(INT_MAX, nb_sectors * BDRV_SECTOR_SIZE);
f8a2e5e3 1637 qemu_co_mutex_lock(&s->lock);
ecfe1863
KW
1638 ret = qcow2_get_cluster_offset(bs, sector_num << 9, &bytes,
1639 &cluster_offset);
f8a2e5e3 1640 qemu_co_mutex_unlock(&s->lock);
1c46efaa 1641 if (ret < 0) {
d663640c 1642 return ret;
1c46efaa 1643 }
095a9c58 1644
ecfe1863
KW
1645 *pnum = bytes >> BDRV_SECTOR_BITS;
1646
4bc74be9 1647 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
b25b387f 1648 !s->crypto) {
4bc74be9
PB
1649 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1650 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
178b4db7 1651 *file = bs->file->bs;
4bc74be9
PB
1652 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
1653 }
fdfab37d 1654 if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
4bc74be9
PB
1655 status |= BDRV_BLOCK_ZERO;
1656 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1657 status |= BDRV_BLOCK_DATA;
1658 }
1659 return status;
585f8587
FB
1660}
1661
a9465922 1662/* handle reading after the end of the backing file */
bd28f835 1663int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
ecfe1863 1664 int64_t offset, int bytes)
a9465922 1665{
ecfe1863 1666 uint64_t bs_size = bs->total_sectors * BDRV_SECTOR_SIZE;
a9465922 1667 int n1;
ecfe1863
KW
1668
1669 if ((offset + bytes) <= bs_size) {
1670 return bytes;
1671 }
1672
1673 if (offset >= bs_size) {
a9465922 1674 n1 = 0;
ecfe1863
KW
1675 } else {
1676 n1 = bs_size - offset;
1677 }
bd28f835 1678
ecfe1863 1679 qemu_iovec_memset(qiov, n1, 0, bytes - n1);
bd28f835 1680
a9465922
FB
1681 return n1;
1682}
1683
ecfe1863
KW
1684static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,
1685 uint64_t bytes, QEMUIOVector *qiov,
1686 int flags)
585f8587 1687{
ff99129a 1688 BDRVQcow2State *s = bs->opaque;
ecfe1863 1689 int offset_in_cluster, n1;
68d100e9 1690 int ret;
ecfe1863 1691 unsigned int cur_bytes; /* number of bytes in current iteration */
c2bdd990 1692 uint64_t cluster_offset = 0;
3fc48d09
FZ
1693 uint64_t bytes_done = 0;
1694 QEMUIOVector hd_qiov;
1695 uint8_t *cluster_data = NULL;
585f8587 1696
3fc48d09
FZ
1697 qemu_iovec_init(&hd_qiov, qiov->niov);
1698
1699 qemu_co_mutex_lock(&s->lock);
1700
ecfe1863 1701 while (bytes != 0) {
bd28f835 1702
5ebaa27e 1703 /* prepare next request */
ecfe1863 1704 cur_bytes = MIN(bytes, INT_MAX);
b25b387f 1705 if (s->crypto) {
ecfe1863
KW
1706 cur_bytes = MIN(cur_bytes,
1707 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
585f8587 1708 }
5ebaa27e 1709
ecfe1863 1710 ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset);
8af36488 1711 if (ret < 0) {
3fc48d09 1712 goto fail;
8af36488 1713 }
bd28f835 1714
ecfe1863 1715 offset_in_cluster = offset_into_cluster(s, offset);
c87c0672 1716
3fc48d09 1717 qemu_iovec_reset(&hd_qiov);
ecfe1863 1718 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
5ebaa27e 1719
68d000a3
KW
1720 switch (ret) {
1721 case QCOW2_CLUSTER_UNALLOCATED:
5ebaa27e 1722
760e0063 1723 if (bs->backing) {
5ebaa27e 1724 /* read from the base image */
760e0063 1725 n1 = qcow2_backing_read1(bs->backing->bs, &hd_qiov,
ecfe1863 1726 offset, cur_bytes);
5ebaa27e 1727 if (n1 > 0) {
44deba5a
KW
1728 QEMUIOVector local_qiov;
1729
1730 qemu_iovec_init(&local_qiov, hd_qiov.niov);
ecfe1863 1731 qemu_iovec_concat(&local_qiov, &hd_qiov, 0, n1);
44deba5a 1732
5ebaa27e
FZ
1733 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1734 qemu_co_mutex_unlock(&s->lock);
a03ef88f 1735 ret = bdrv_co_preadv(bs->backing, offset, n1,
ecfe1863 1736 &local_qiov, 0);
5ebaa27e 1737 qemu_co_mutex_lock(&s->lock);
44deba5a
KW
1738
1739 qemu_iovec_destroy(&local_qiov);
1740
5ebaa27e 1741 if (ret < 0) {
3fc48d09 1742 goto fail;
5ebaa27e
FZ
1743 }
1744 }
1745 } else {
1746 /* Note: in this case, no need to wait */
ecfe1863 1747 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
5ebaa27e 1748 }
68d000a3
KW
1749 break;
1750
fdfab37d
EB
1751 case QCOW2_CLUSTER_ZERO_PLAIN:
1752 case QCOW2_CLUSTER_ZERO_ALLOC:
ecfe1863 1753 qemu_iovec_memset(&hd_qiov, 0, 0, cur_bytes);
6377af48
KW
1754 break;
1755
68d000a3 1756 case QCOW2_CLUSTER_COMPRESSED:
5ebaa27e
FZ
1757 /* add AIO support for compressed blocks ? */
1758 ret = qcow2_decompress_cluster(bs, cluster_offset);
1759 if (ret < 0) {
3fc48d09 1760 goto fail;
bd28f835
KW
1761 }
1762
03396148 1763 qemu_iovec_from_buf(&hd_qiov, 0,
ecfe1863
KW
1764 s->cluster_cache + offset_in_cluster,
1765 cur_bytes);
68d000a3
KW
1766 break;
1767
1768 case QCOW2_CLUSTER_NORMAL:
5ebaa27e 1769 if ((cluster_offset & 511) != 0) {
3fc48d09
FZ
1770 ret = -EIO;
1771 goto fail;
5ebaa27e 1772 }
bd28f835 1773
8336aafa 1774 if (bs->encrypted) {
b25b387f 1775 assert(s->crypto);
8336aafa 1776
5ebaa27e
FZ
1777 /*
1778 * For encrypted images, read everything into a temporary
1779 * contiguous buffer on which the AES functions can work.
1780 */
3fc48d09
FZ
1781 if (!cluster_data) {
1782 cluster_data =
9a4f4c31
KW
1783 qemu_try_blockalign(bs->file->bs,
1784 QCOW_MAX_CRYPT_CLUSTERS
1785 * s->cluster_size);
de82815d
KW
1786 if (cluster_data == NULL) {
1787 ret = -ENOMEM;
1788 goto fail;
1789 }
5ebaa27e
FZ
1790 }
1791
ecfe1863 1792 assert(cur_bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
3fc48d09 1793 qemu_iovec_reset(&hd_qiov);
ecfe1863 1794 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
5ebaa27e
FZ
1795 }
1796
1797 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1798 qemu_co_mutex_unlock(&s->lock);
a03ef88f 1799 ret = bdrv_co_preadv(bs->file,
ecfe1863
KW
1800 cluster_offset + offset_in_cluster,
1801 cur_bytes, &hd_qiov, 0);
5ebaa27e
FZ
1802 qemu_co_mutex_lock(&s->lock);
1803 if (ret < 0) {
3fc48d09 1804 goto fail;
5ebaa27e 1805 }
8336aafa 1806 if (bs->encrypted) {
b25b387f 1807 assert(s->crypto);
ecfe1863
KW
1808 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
1809 assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
b25b387f 1810 if (qcrypto_block_decrypt(s->crypto,
4652b8f3
DB
1811 (s->crypt_physical_offset ?
1812 cluster_offset + offset_in_cluster :
1813 offset) >> BDRV_SECTOR_BITS,
446d306d 1814 cluster_data,
b25b387f 1815 cur_bytes,
c3a8fe33 1816 NULL) < 0) {
f6fa64f6
DB
1817 ret = -EIO;
1818 goto fail;
1819 }
ecfe1863 1820 qemu_iovec_from_buf(qiov, bytes_done, cluster_data, cur_bytes);
5ebaa27e 1821 }
68d000a3
KW
1822 break;
1823
1824 default:
1825 g_assert_not_reached();
1826 ret = -EIO;
1827 goto fail;
faf575c1 1828 }
f141eafe 1829
ecfe1863
KW
1830 bytes -= cur_bytes;
1831 offset += cur_bytes;
1832 bytes_done += cur_bytes;
5ebaa27e 1833 }
3fc48d09 1834 ret = 0;
faf575c1 1835
3fc48d09 1836fail:
68d100e9 1837 qemu_co_mutex_unlock(&s->lock);
42496d62 1838
3fc48d09 1839 qemu_iovec_destroy(&hd_qiov);
dea43a65 1840 qemu_vfree(cluster_data);
68d100e9
KW
1841
1842 return ret;
585f8587
FB
1843}
1844
ee22a9d8
AG
1845/* Check if it's possible to merge a write request with the writing of
1846 * the data from the COW regions */
1847static bool merge_cow(uint64_t offset, unsigned bytes,
1848 QEMUIOVector *hd_qiov, QCowL2Meta *l2meta)
1849{
1850 QCowL2Meta *m;
1851
1852 for (m = l2meta; m != NULL; m = m->next) {
1853 /* If both COW regions are empty then there's nothing to merge */
1854 if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
1855 continue;
1856 }
1857
1858 /* The data (middle) region must be immediately after the
1859 * start region */
1860 if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
1861 continue;
1862 }
1863
1864 /* The end region must be immediately after the data (middle)
1865 * region */
1866 if (m->offset + m->cow_end.offset != offset + bytes) {
1867 continue;
1868 }
1869
1870 /* Make sure that adding both COW regions to the QEMUIOVector
1871 * does not exceed IOV_MAX */
1872 if (hd_qiov->niov > IOV_MAX - 2) {
1873 continue;
1874 }
1875
1876 m->data_qiov = hd_qiov;
1877 return true;
1878 }
1879
1880 return false;
1881}
1882
d46a0bb2
KW
1883static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,
1884 uint64_t bytes, QEMUIOVector *qiov,
1885 int flags)
585f8587 1886{
ff99129a 1887 BDRVQcow2State *s = bs->opaque;
d46a0bb2 1888 int offset_in_cluster;
68d100e9 1889 int ret;
d46a0bb2 1890 unsigned int cur_bytes; /* number of sectors in current iteration */
c2bdd990 1891 uint64_t cluster_offset;
3fc48d09
FZ
1892 QEMUIOVector hd_qiov;
1893 uint64_t bytes_done = 0;
1894 uint8_t *cluster_data = NULL;
8d2497c3 1895 QCowL2Meta *l2meta = NULL;
c2271403 1896
d46a0bb2 1897 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
3cce16f4 1898
3fc48d09
FZ
1899 qemu_iovec_init(&hd_qiov, qiov->niov);
1900
1901 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 1902
3fc48d09
FZ
1903 qemu_co_mutex_lock(&s->lock);
1904
d46a0bb2 1905 while (bytes != 0) {
3fc48d09 1906
f50f88b9 1907 l2meta = NULL;
cf5c1a23 1908
3cce16f4 1909 trace_qcow2_writev_start_part(qemu_coroutine_self());
d46a0bb2
KW
1910 offset_in_cluster = offset_into_cluster(s, offset);
1911 cur_bytes = MIN(bytes, INT_MAX);
1912 if (bs->encrypted) {
1913 cur_bytes = MIN(cur_bytes,
1914 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
1915 - offset_in_cluster);
5ebaa27e 1916 }
095a9c58 1917
d46a0bb2
KW
1918 ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
1919 &cluster_offset, &l2meta);
5ebaa27e 1920 if (ret < 0) {
3fc48d09 1921 goto fail;
5ebaa27e 1922 }
148da7ea 1923
5ebaa27e 1924 assert((cluster_offset & 511) == 0);
148da7ea 1925
3fc48d09 1926 qemu_iovec_reset(&hd_qiov);
d46a0bb2 1927 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, cur_bytes);
6f5f060b 1928
8336aafa 1929 if (bs->encrypted) {
b25b387f 1930 assert(s->crypto);
3fc48d09 1931 if (!cluster_data) {
9a4f4c31 1932 cluster_data = qemu_try_blockalign(bs->file->bs,
de82815d
KW
1933 QCOW_MAX_CRYPT_CLUSTERS
1934 * s->cluster_size);
1935 if (cluster_data == NULL) {
1936 ret = -ENOMEM;
1937 goto fail;
1938 }
5ebaa27e 1939 }
6f5f060b 1940
3fc48d09 1941 assert(hd_qiov.size <=
5ebaa27e 1942 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
d5e6b161 1943 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
6f5f060b 1944
4652b8f3
DB
1945 if (qcrypto_block_encrypt(s->crypto,
1946 (s->crypt_physical_offset ?
1947 cluster_offset + offset_in_cluster :
1948 offset) >> BDRV_SECTOR_BITS,
446d306d 1949 cluster_data,
c3a8fe33 1950 cur_bytes, NULL) < 0) {
f6fa64f6
DB
1951 ret = -EIO;
1952 goto fail;
1953 }
6f5f060b 1954
3fc48d09 1955 qemu_iovec_reset(&hd_qiov);
d46a0bb2 1956 qemu_iovec_add(&hd_qiov, cluster_data, cur_bytes);
5ebaa27e 1957 }
6f5f060b 1958
231bb267 1959 ret = qcow2_pre_write_overlap_check(bs, 0,
d46a0bb2 1960 cluster_offset + offset_in_cluster, cur_bytes);
cf93980e
HR
1961 if (ret < 0) {
1962 goto fail;
1963 }
1964
ee22a9d8
AG
1965 /* If we need to do COW, check if it's possible to merge the
1966 * writing of the guest data together with that of the COW regions.
1967 * If it's not possible (or not necessary) then write the
1968 * guest data now. */
1969 if (!merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) {
1970 qemu_co_mutex_unlock(&s->lock);
1971 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1972 trace_qcow2_writev_data(qemu_coroutine_self(),
1973 cluster_offset + offset_in_cluster);
1974 ret = bdrv_co_pwritev(bs->file,
1975 cluster_offset + offset_in_cluster,
1976 cur_bytes, &hd_qiov, 0);
1977 qemu_co_mutex_lock(&s->lock);
1978 if (ret < 0) {
1979 goto fail;
1980 }
5ebaa27e 1981 }
f141eafe 1982
88c6588c
KW
1983 while (l2meta != NULL) {
1984 QCowL2Meta *next;
1985
f50f88b9
KW
1986 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1987 if (ret < 0) {
1988 goto fail;
1989 }
faf575c1 1990
4e95314e
KW
1991 /* Take the request off the list of running requests */
1992 if (l2meta->nb_clusters != 0) {
1993 QLIST_REMOVE(l2meta, next_in_flight);
1994 }
1995
4e95314e 1996 qemu_co_queue_restart_all(&l2meta->dependent_requests);
4e95314e 1997
88c6588c 1998 next = l2meta->next;
f50f88b9 1999 g_free(l2meta);
88c6588c 2000 l2meta = next;
f50f88b9 2001 }
0fa9131a 2002
d46a0bb2
KW
2003 bytes -= cur_bytes;
2004 offset += cur_bytes;
2005 bytes_done += cur_bytes;
2006 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
5ebaa27e 2007 }
3fc48d09 2008 ret = 0;
faf575c1 2009
3fc48d09 2010fail:
88c6588c
KW
2011 while (l2meta != NULL) {
2012 QCowL2Meta *next;
2013
4e95314e
KW
2014 if (l2meta->nb_clusters != 0) {
2015 QLIST_REMOVE(l2meta, next_in_flight);
2016 }
2017 qemu_co_queue_restart_all(&l2meta->dependent_requests);
88c6588c
KW
2018
2019 next = l2meta->next;
cf5c1a23 2020 g_free(l2meta);
88c6588c 2021 l2meta = next;
cf5c1a23 2022 }
0fa9131a 2023
a8c57408
PB
2024 qemu_co_mutex_unlock(&s->lock);
2025
3fc48d09 2026 qemu_iovec_destroy(&hd_qiov);
dea43a65 2027 qemu_vfree(cluster_data);
3cce16f4 2028 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
42496d62 2029
68d100e9 2030 return ret;
585f8587
FB
2031}
2032
ec6d8912
KW
2033static int qcow2_inactivate(BlockDriverState *bs)
2034{
2035 BDRVQcow2State *s = bs->opaque;
2036 int ret, result = 0;
5f72826e 2037 Error *local_err = NULL;
ec6d8912 2038
83a8c775
PB
2039 qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
2040 if (local_err != NULL) {
2041 result = -EINVAL;
2042 error_report_err(local_err);
2043 error_report("Persistent bitmaps are lost for node '%s'",
2044 bdrv_get_device_or_node_name(bs));
2045 }
2046
ec6d8912
KW
2047 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2048 if (ret) {
2049 result = ret;
2050 error_report("Failed to flush the L2 table cache: %s",
2051 strerror(-ret));
2052 }
2053
2054 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2055 if (ret) {
2056 result = ret;
2057 error_report("Failed to flush the refcount block cache: %s",
2058 strerror(-ret));
2059 }
2060
2061 if (result == 0) {
2062 qcow2_mark_clean(bs);
2063 }
2064
2065 return result;
2066}
2067
7c80ab3f 2068static void qcow2_close(BlockDriverState *bs)
585f8587 2069{
ff99129a 2070 BDRVQcow2State *s = bs->opaque;
de82815d 2071 qemu_vfree(s->l1_table);
cf93980e
HR
2072 /* else pre-write overlap checks in cache_destroy may crash */
2073 s->l1_table = NULL;
29c1a730 2074
140fd5a6 2075 if (!(s->flags & BDRV_O_INACTIVE)) {
ec6d8912 2076 qcow2_inactivate(bs);
27eb6c09 2077 }
c61d0004 2078
279621c0 2079 cache_clean_timer_del(bs);
29c1a730
KW
2080 qcow2_cache_destroy(bs, s->l2_table_cache);
2081 qcow2_cache_destroy(bs, s->refcount_block_cache);
2082
b25b387f
DB
2083 qcrypto_block_free(s->crypto);
2084 s->crypto = NULL;
f6fa64f6 2085
6744cbab 2086 g_free(s->unknown_header_fields);
75bab85c 2087 cleanup_unknown_header_ext(bs);
6744cbab 2088
e4603fe1
KW
2089 g_free(s->image_backing_file);
2090 g_free(s->image_backing_format);
2091
7267c094 2092 g_free(s->cluster_cache);
dea43a65 2093 qemu_vfree(s->cluster_data);
ed6ccf0f 2094 qcow2_refcount_close(bs);
28c1202b 2095 qcow2_free_snapshots(bs);
585f8587
FB
2096}
2097
5a8a30db 2098static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
06d9260f 2099{
ff99129a 2100 BDRVQcow2State *s = bs->opaque;
06d9260f 2101 int flags = s->flags;
b25b387f 2102 QCryptoBlock *crypto = NULL;
acdfb480 2103 QDict *options;
5a8a30db
KW
2104 Error *local_err = NULL;
2105 int ret;
06d9260f
AL
2106
2107 /*
2108 * Backing files are read-only which makes all of their metadata immutable,
2109 * that means we don't have to worry about reopening them here.
2110 */
2111
b25b387f
DB
2112 crypto = s->crypto;
2113 s->crypto = NULL;
06d9260f
AL
2114
2115 qcow2_close(bs);
2116
ff99129a 2117 memset(s, 0, sizeof(BDRVQcow2State));
d475e5ac 2118 options = qdict_clone_shallow(bs->options);
5a8a30db 2119
140fd5a6 2120 flags &= ~BDRV_O_INACTIVE;
4e4bf5c4 2121 ret = qcow2_do_open(bs, options, flags, &local_err);
a1904e48 2122 QDECREF(options);
5a8a30db 2123 if (local_err) {
e43bfd9c
MA
2124 error_propagate(errp, local_err);
2125 error_prepend(errp, "Could not reopen qcow2 layer: ");
191fb11b 2126 bs->drv = NULL;
5a8a30db
KW
2127 return;
2128 } else if (ret < 0) {
2129 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
191fb11b 2130 bs->drv = NULL;
5a8a30db
KW
2131 return;
2132 }
acdfb480 2133
b25b387f 2134 s->crypto = crypto;
06d9260f
AL
2135}
2136
e24e49e6
KW
2137static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2138 size_t len, size_t buflen)
2139{
2140 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2141 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2142
2143 if (buflen < ext_len) {
2144 return -ENOSPC;
2145 }
2146
2147 *ext_backing_fmt = (QCowExtension) {
2148 .magic = cpu_to_be32(magic),
2149 .len = cpu_to_be32(len),
2150 };
0647d47c
SH
2151
2152 if (len) {
2153 memcpy(buf + sizeof(QCowExtension), s, len);
2154 }
e24e49e6
KW
2155
2156 return ext_len;
2157}
2158
756e6736 2159/*
e24e49e6
KW
2160 * Updates the qcow2 header, including the variable length parts of it, i.e.
2161 * the backing file name and all extensions. qcow2 was not designed to allow
2162 * such changes, so if we run out of space (we can only use the first cluster)
2163 * this function may fail.
756e6736
KW
2164 *
2165 * Returns 0 on success, -errno in error cases.
2166 */
e24e49e6 2167int qcow2_update_header(BlockDriverState *bs)
756e6736 2168{
ff99129a 2169 BDRVQcow2State *s = bs->opaque;
e24e49e6
KW
2170 QCowHeader *header;
2171 char *buf;
2172 size_t buflen = s->cluster_size;
756e6736 2173 int ret;
e24e49e6
KW
2174 uint64_t total_size;
2175 uint32_t refcount_table_clusters;
6744cbab 2176 size_t header_length;
75bab85c 2177 Qcow2UnknownHeaderExtension *uext;
756e6736 2178
e24e49e6 2179 buf = qemu_blockalign(bs, buflen);
756e6736 2180
e24e49e6
KW
2181 /* Header structure */
2182 header = (QCowHeader*) buf;
756e6736 2183
e24e49e6
KW
2184 if (buflen < sizeof(*header)) {
2185 ret = -ENOSPC;
2186 goto fail;
756e6736
KW
2187 }
2188
6744cbab 2189 header_length = sizeof(*header) + s->unknown_header_fields_size;
e24e49e6
KW
2190 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2191 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2192
2193 *header = (QCowHeader) {
6744cbab 2194 /* Version 2 fields */
e24e49e6 2195 .magic = cpu_to_be32(QCOW_MAGIC),
6744cbab 2196 .version = cpu_to_be32(s->qcow_version),
e24e49e6
KW
2197 .backing_file_offset = 0,
2198 .backing_file_size = 0,
2199 .cluster_bits = cpu_to_be32(s->cluster_bits),
2200 .size = cpu_to_be64(total_size),
2201 .crypt_method = cpu_to_be32(s->crypt_method_header),
2202 .l1_size = cpu_to_be32(s->l1_size),
2203 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
2204 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
2205 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2206 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
2207 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
6744cbab
KW
2208
2209 /* Version 3 fields */
2210 .incompatible_features = cpu_to_be64(s->incompatible_features),
2211 .compatible_features = cpu_to_be64(s->compatible_features),
2212 .autoclear_features = cpu_to_be64(s->autoclear_features),
b6481f37 2213 .refcount_order = cpu_to_be32(s->refcount_order),
6744cbab 2214 .header_length = cpu_to_be32(header_length),
e24e49e6 2215 };
756e6736 2216
6744cbab
KW
2217 /* For older versions, write a shorter header */
2218 switch (s->qcow_version) {
2219 case 2:
2220 ret = offsetof(QCowHeader, incompatible_features);
2221 break;
2222 case 3:
2223 ret = sizeof(*header);
2224 break;
2225 default:
b6c14762
JM
2226 ret = -EINVAL;
2227 goto fail;
6744cbab
KW
2228 }
2229
2230 buf += ret;
2231 buflen -= ret;
2232 memset(buf, 0, buflen);
2233
2234 /* Preserve any unknown field in the header */
2235 if (s->unknown_header_fields_size) {
2236 if (buflen < s->unknown_header_fields_size) {
2237 ret = -ENOSPC;
2238 goto fail;
2239 }
2240
2241 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
2242 buf += s->unknown_header_fields_size;
2243 buflen -= s->unknown_header_fields_size;
2244 }
756e6736 2245
e24e49e6 2246 /* Backing file format header extension */
e4603fe1 2247 if (s->image_backing_format) {
e24e49e6 2248 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
e4603fe1
KW
2249 s->image_backing_format,
2250 strlen(s->image_backing_format),
e24e49e6
KW
2251 buflen);
2252 if (ret < 0) {
2253 goto fail;
756e6736
KW
2254 }
2255
e24e49e6
KW
2256 buf += ret;
2257 buflen -= ret;
756e6736
KW
2258 }
2259
4652b8f3
DB
2260 /* Full disk encryption header pointer extension */
2261 if (s->crypto_header.offset != 0) {
2262 cpu_to_be64s(&s->crypto_header.offset);
2263 cpu_to_be64s(&s->crypto_header.length);
2264 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
2265 &s->crypto_header, sizeof(s->crypto_header),
2266 buflen);
2267 be64_to_cpus(&s->crypto_header.offset);
2268 be64_to_cpus(&s->crypto_header.length);
2269 if (ret < 0) {
2270 goto fail;
2271 }
2272 buf += ret;
2273 buflen -= ret;
2274 }
2275
cfcc4c62 2276 /* Feature table */
1a4828c7
KW
2277 if (s->qcow_version >= 3) {
2278 Qcow2Feature features[] = {
2279 {
2280 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2281 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
2282 .name = "dirty bit",
2283 },
2284 {
2285 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
2286 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
2287 .name = "corrupt bit",
2288 },
2289 {
2290 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
2291 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
2292 .name = "lazy refcounts",
2293 },
2294 };
2295
2296 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
2297 features, sizeof(features), buflen);
2298 if (ret < 0) {
2299 goto fail;
2300 }
2301 buf += ret;
2302 buflen -= ret;
cfcc4c62 2303 }
cfcc4c62 2304
88ddffae
VSO
2305 /* Bitmap extension */
2306 if (s->nb_bitmaps > 0) {
2307 Qcow2BitmapHeaderExt bitmaps_header = {
2308 .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
2309 .bitmap_directory_size =
2310 cpu_to_be64(s->bitmap_directory_size),
2311 .bitmap_directory_offset =
2312 cpu_to_be64(s->bitmap_directory_offset)
2313 };
2314 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
2315 &bitmaps_header, sizeof(bitmaps_header),
2316 buflen);
2317 if (ret < 0) {
2318 goto fail;
2319 }
2320 buf += ret;
2321 buflen -= ret;
2322 }
2323
75bab85c
KW
2324 /* Keep unknown header extensions */
2325 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
2326 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
2327 if (ret < 0) {
2328 goto fail;
2329 }
2330
2331 buf += ret;
2332 buflen -= ret;
2333 }
2334
e24e49e6
KW
2335 /* End of header extensions */
2336 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
756e6736
KW
2337 if (ret < 0) {
2338 goto fail;
2339 }
2340
e24e49e6
KW
2341 buf += ret;
2342 buflen -= ret;
756e6736 2343
e24e49e6 2344 /* Backing file name */
e4603fe1
KW
2345 if (s->image_backing_file) {
2346 size_t backing_file_len = strlen(s->image_backing_file);
e24e49e6
KW
2347
2348 if (buflen < backing_file_len) {
2349 ret = -ENOSPC;
2350 goto fail;
2351 }
2352
00ea1881 2353 /* Using strncpy is ok here, since buf is not NUL-terminated. */
e4603fe1 2354 strncpy(buf, s->image_backing_file, buflen);
e24e49e6
KW
2355
2356 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
2357 header->backing_file_size = cpu_to_be32(backing_file_len);
756e6736
KW
2358 }
2359
e24e49e6 2360 /* Write the new header */
d9ca2ea2 2361 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
756e6736
KW
2362 if (ret < 0) {
2363 goto fail;
2364 }
2365
2366 ret = 0;
2367fail:
e24e49e6 2368 qemu_vfree(header);
756e6736
KW
2369 return ret;
2370}
2371
2372static int qcow2_change_backing_file(BlockDriverState *bs,
2373 const char *backing_file, const char *backing_fmt)
2374{
ff99129a 2375 BDRVQcow2State *s = bs->opaque;
e4603fe1 2376
4e876bcf
HR
2377 if (backing_file && strlen(backing_file) > 1023) {
2378 return -EINVAL;
2379 }
2380
e24e49e6
KW
2381 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
2382 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
2383
e4603fe1
KW
2384 g_free(s->image_backing_file);
2385 g_free(s->image_backing_format);
2386
2387 s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
2388 s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
2389
e24e49e6 2390 return qcow2_update_header(bs);
756e6736
KW
2391}
2392
4652b8f3
DB
2393static int qcow2_crypt_method_from_format(const char *encryptfmt)
2394{
2395 if (g_str_equal(encryptfmt, "luks")) {
2396 return QCOW_CRYPT_LUKS;
2397 } else if (g_str_equal(encryptfmt, "aes")) {
2398 return QCOW_CRYPT_AES;
2399 } else {
2400 return -EINVAL;
2401 }
2402}
b25b387f
DB
2403
2404static int qcow2_set_up_encryption(BlockDriverState *bs, const char *encryptfmt,
2405 QemuOpts *opts, Error **errp)
2406{
2407 BDRVQcow2State *s = bs->opaque;
2408 QCryptoBlockCreateOptions *cryptoopts = NULL;
2409 QCryptoBlock *crypto = NULL;
2410 int ret = -EINVAL;
2411 QDict *options, *encryptopts;
4652b8f3 2412 int fmt;
b25b387f
DB
2413
2414 options = qemu_opts_to_qdict(opts, NULL);
2415 qdict_extract_subqdict(options, &encryptopts, "encrypt.");
2416 QDECREF(options);
2417
4652b8f3
DB
2418 fmt = qcow2_crypt_method_from_format(encryptfmt);
2419
2420 switch (fmt) {
2421 case QCOW_CRYPT_LUKS:
2422 cryptoopts = block_crypto_create_opts_init(
2423 Q_CRYPTO_BLOCK_FORMAT_LUKS, encryptopts, errp);
2424 break;
2425 case QCOW_CRYPT_AES:
2426 cryptoopts = block_crypto_create_opts_init(
2427 Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
2428 break;
2429 default:
2430 error_setg(errp, "Unknown encryption format '%s'", encryptfmt);
2431 break;
b25b387f 2432 }
b25b387f
DB
2433 if (!cryptoopts) {
2434 ret = -EINVAL;
2435 goto out;
2436 }
4652b8f3 2437 s->crypt_method_header = fmt;
b25b387f 2438
1cd9a787 2439 crypto = qcrypto_block_create(cryptoopts, "encrypt.",
4652b8f3
DB
2440 qcow2_crypto_hdr_init_func,
2441 qcow2_crypto_hdr_write_func,
b25b387f
DB
2442 bs, errp);
2443 if (!crypto) {
2444 ret = -EINVAL;
2445 goto out;
2446 }
2447
2448 ret = qcow2_update_header(bs);
2449 if (ret < 0) {
2450 error_setg_errno(errp, -ret, "Could not write encryption header");
2451 goto out;
2452 }
2453
2454 out:
2455 QDECREF(encryptopts);
2456 qcrypto_block_free(crypto);
2457 qapi_free_QCryptoBlockCreateOptions(cryptoopts);
2458 return ret;
2459}
2460
2461
7bc45dc1
HR
2462/**
2463 * Preallocates metadata structures for data clusters between @offset (in the
2464 * guest disk) and @new_length (which is thus generally the new guest disk
2465 * size).
2466 *
2467 * Returns: 0 on success, -errno on failure.
2468 */
2469static int preallocate(BlockDriverState *bs,
2470 uint64_t offset, uint64_t new_length)
a35e1c17 2471{
652fecd0 2472 BDRVQcow2State *s = bs->opaque;
d46a0bb2 2473 uint64_t bytes;
060bee89 2474 uint64_t host_offset = 0;
d46a0bb2 2475 unsigned int cur_bytes;
148da7ea 2476 int ret;
f50f88b9 2477 QCowL2Meta *meta;
a35e1c17 2478
652fecd0
HR
2479 if (qemu_in_coroutine()) {
2480 qemu_co_mutex_lock(&s->lock);
2481 }
2482
7bc45dc1
HR
2483 assert(offset <= new_length);
2484 bytes = new_length - offset;
a35e1c17 2485
d46a0bb2
KW
2486 while (bytes) {
2487 cur_bytes = MIN(bytes, INT_MAX);
2488 ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes,
060bee89 2489 &host_offset, &meta);
148da7ea 2490 if (ret < 0) {
652fecd0 2491 goto done;
a35e1c17
KW
2492 }
2493
c792707f
SH
2494 while (meta) {
2495 QCowL2Meta *next = meta->next;
2496
7c2bbf4a
HT
2497 ret = qcow2_alloc_cluster_link_l2(bs, meta);
2498 if (ret < 0) {
2499 qcow2_free_any_clusters(bs, meta->alloc_offset,
2500 meta->nb_clusters, QCOW2_DISCARD_NEVER);
652fecd0 2501 goto done;
7c2bbf4a
HT
2502 }
2503
2504 /* There are no dependent requests, but we need to remove our
2505 * request from the list of in-flight requests */
4e95314e 2506 QLIST_REMOVE(meta, next_in_flight);
c792707f
SH
2507
2508 g_free(meta);
2509 meta = next;
f50f88b9 2510 }
f214978a 2511
a35e1c17
KW
2512 /* TODO Preallocate data if requested */
2513
d46a0bb2
KW
2514 bytes -= cur_bytes;
2515 offset += cur_bytes;
a35e1c17
KW
2516 }
2517
2518 /*
2519 * It is expected that the image file is large enough to actually contain
2520 * all of the allocated clusters (otherwise we get failing reads after
2521 * EOF). Extend the image to the last allocated sector.
2522 */
060bee89 2523 if (host_offset != 0) {
d46a0bb2 2524 uint8_t data = 0;
d9ca2ea2 2525 ret = bdrv_pwrite(bs->file, (host_offset + cur_bytes) - 1,
d46a0bb2 2526 &data, 1);
19dbcbf7 2527 if (ret < 0) {
652fecd0 2528 goto done;
19dbcbf7 2529 }
a35e1c17
KW
2530 }
2531
652fecd0
HR
2532 ret = 0;
2533
2534done:
2535 if (qemu_in_coroutine()) {
2536 qemu_co_mutex_unlock(&s->lock);
2537 }
2538 return ret;
a35e1c17
KW
2539}
2540
7c5bcc42
SH
2541/* qcow2_refcount_metadata_size:
2542 * @clusters: number of clusters to refcount (including data and L1/L2 tables)
2543 * @cluster_size: size of a cluster, in bytes
2544 * @refcount_order: refcount bits power-of-2 exponent
12cc30a8
HR
2545 * @generous_increase: allow for the refcount table to be 1.5x as large as it
2546 * needs to be
7c5bcc42
SH
2547 *
2548 * Returns: Number of bytes required for refcount blocks and table metadata.
2549 */
12cc30a8
HR
2550int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
2551 int refcount_order, bool generous_increase,
2552 uint64_t *refblock_count)
7c5bcc42
SH
2553{
2554 /*
2555 * Every host cluster is reference-counted, including metadata (even
2556 * refcount metadata is recursively included).
2557 *
2558 * An accurate formula for the size of refcount metadata size is difficult
2559 * to derive. An easier method of calculation is finding the fixed point
2560 * where no further refcount blocks or table clusters are required to
2561 * reference count every cluster.
2562 */
2563 int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t);
2564 int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
2565 int64_t table = 0; /* number of refcount table clusters */
2566 int64_t blocks = 0; /* number of refcount block clusters */
2567 int64_t last;
2568 int64_t n = 0;
2569
2570 do {
2571 last = n;
2572 blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
2573 table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
2574 n = clusters + blocks + table;
12cc30a8
HR
2575
2576 if (n == last && generous_increase) {
2577 clusters += DIV_ROUND_UP(table, 2);
2578 n = 0; /* force another loop */
2579 generous_increase = false;
2580 }
7c5bcc42
SH
2581 } while (n != last);
2582
12cc30a8
HR
2583 if (refblock_count) {
2584 *refblock_count = blocks;
2585 }
2586
7c5bcc42
SH
2587 return (blocks + table) * cluster_size;
2588}
2589
95c67e3b
SH
2590/**
2591 * qcow2_calc_prealloc_size:
2592 * @total_size: virtual disk size in bytes
2593 * @cluster_size: cluster size in bytes
2594 * @refcount_order: refcount bits power-of-2 exponent
2595 *
2596 * Returns: Total number of bytes required for the fully allocated image
2597 * (including metadata).
2598 */
2599static int64_t qcow2_calc_prealloc_size(int64_t total_size,
2600 size_t cluster_size,
2601 int refcount_order)
2602{
95c67e3b 2603 int64_t meta_size = 0;
7c5bcc42 2604 uint64_t nl1e, nl2e;
95c67e3b 2605 int64_t aligned_total_size = align_offset(total_size, cluster_size);
95c67e3b
SH
2606
2607 /* header: 1 cluster */
2608 meta_size += cluster_size;
2609
2610 /* total size of L2 tables */
2611 nl2e = aligned_total_size / cluster_size;
2612 nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
2613 meta_size += nl2e * sizeof(uint64_t);
2614
2615 /* total size of L1 tables */
2616 nl1e = nl2e * sizeof(uint64_t) / cluster_size;
2617 nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
2618 meta_size += nl1e * sizeof(uint64_t);
2619
7c5bcc42
SH
2620 /* total size of refcount table and blocks */
2621 meta_size += qcow2_refcount_metadata_size(
2622 (meta_size + aligned_total_size) / cluster_size,
12cc30a8 2623 cluster_size, refcount_order, false, NULL);
95c67e3b
SH
2624
2625 return meta_size + aligned_total_size;
2626}
2627
0eb4a8c1 2628static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp)
a9420734 2629{
0eb4a8c1 2630 size_t cluster_size;
a9420734 2631 int cluster_bits;
e6641719 2632
0eb4a8c1
SH
2633 cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2634 DEFAULT_CLUSTER_SIZE);
786a4ea8 2635 cluster_bits = ctz32(cluster_size);
a9420734
KW
2636 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
2637 (1 << cluster_bits) != cluster_size)
2638 {
3ef6c40a
HR
2639 error_setg(errp, "Cluster size must be a power of two between %d and "
2640 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
0eb4a8c1
SH
2641 return 0;
2642 }
2643 return cluster_size;
2644}
2645
2646static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
2647{
2648 char *buf;
2649 int ret;
2650
2651 buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2652 if (!buf) {
2653 ret = 3; /* default */
2654 } else if (!strcmp(buf, "0.10")) {
2655 ret = 2;
2656 } else if (!strcmp(buf, "1.1")) {
2657 ret = 3;
2658 } else {
2659 error_setg(errp, "Invalid compatibility level: '%s'", buf);
2660 ret = -EINVAL;
2661 }
2662 g_free(buf);
2663 return ret;
2664}
2665
2666static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
2667 Error **errp)
2668{
2669 uint64_t refcount_bits;
2670
2671 refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
2672 if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
2673 error_setg(errp, "Refcount width must be a power of two and may not "
2674 "exceed 64 bits");
2675 return 0;
2676 }
2677
2678 if (version < 3 && refcount_bits != 16) {
2679 error_setg(errp, "Different refcount widths than 16 bits require "
2680 "compatibility level 1.1 or above (use compat=1.1 or "
2681 "greater)");
2682 return 0;
a9420734
KW
2683 }
2684
0eb4a8c1
SH
2685 return refcount_bits;
2686}
2687
2688static int qcow2_create2(const char *filename, int64_t total_size,
2689 const char *backing_file, const char *backing_format,
2690 int flags, size_t cluster_size, PreallocMode prealloc,
2691 QemuOpts *opts, int version, int refcount_order,
2692 const char *encryptfmt, Error **errp)
2693{
2694 QDict *options;
2695
a9420734
KW
2696 /*
2697 * Open the image file and write a minimal qcow2 header.
2698 *
2699 * We keep things simple and start with a zero-sized image. We also
2700 * do without refcount blocks or a L1 table for now. We'll fix the
2701 * inconsistency later.
2702 *
2703 * We do need a refcount table because growing the refcount table means
2704 * allocating two new refcount blocks - the seconds of which would be at
2705 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
2706 * size for any qcow2 image.
2707 */
23588797 2708 BlockBackend *blk;
f8413b3c 2709 QCowHeader *header;
b106ad91 2710 uint64_t* refcount_table;
3ef6c40a 2711 Error *local_err = NULL;
a9420734
KW
2712 int ret;
2713
0e4271b7 2714 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
95c67e3b
SH
2715 int64_t prealloc_size =
2716 qcow2_calc_prealloc_size(total_size, cluster_size, refcount_order);
2717 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, prealloc_size, &error_abort);
977c736f 2718 qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_str(prealloc),
f43e47db 2719 &error_abort);
0e4271b7
HT
2720 }
2721
c282e1fd 2722 ret = bdrv_create_file(filename, opts, &local_err);
a9420734 2723 if (ret < 0) {
3ef6c40a 2724 error_propagate(errp, local_err);
a9420734
KW
2725 return ret;
2726 }
2727
efaa7c4e 2728 blk = blk_new_open(filename, NULL, NULL,
55880601
KW
2729 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
2730 &local_err);
23588797 2731 if (blk == NULL) {
3ef6c40a 2732 error_propagate(errp, local_err);
23588797 2733 return -EIO;
a9420734
KW
2734 }
2735
23588797
KW
2736 blk_set_allow_write_beyond_eof(blk, true);
2737
a9420734 2738 /* Write the header */
f8413b3c
KW
2739 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
2740 header = g_malloc0(cluster_size);
2741 *header = (QCowHeader) {
2742 .magic = cpu_to_be32(QCOW_MAGIC),
2743 .version = cpu_to_be32(version),
0eb4a8c1 2744 .cluster_bits = cpu_to_be32(ctz32(cluster_size)),
f8413b3c
KW
2745 .size = cpu_to_be64(0),
2746 .l1_table_offset = cpu_to_be64(0),
2747 .l1_size = cpu_to_be32(0),
2748 .refcount_table_offset = cpu_to_be64(cluster_size),
2749 .refcount_table_clusters = cpu_to_be32(1),
bd4b167f 2750 .refcount_order = cpu_to_be32(refcount_order),
f8413b3c
KW
2751 .header_length = cpu_to_be32(sizeof(*header)),
2752 };
a9420734 2753
b25b387f
DB
2754 /* We'll update this to correct value later */
2755 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
a9420734 2756
bfe8043e 2757 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
f8413b3c 2758 header->compatible_features |=
bfe8043e
SH
2759 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
2760 }
2761
8341f00d 2762 ret = blk_pwrite(blk, 0, header, cluster_size, 0);
f8413b3c 2763 g_free(header);
a9420734 2764 if (ret < 0) {
3ef6c40a 2765 error_setg_errno(errp, -ret, "Could not write qcow2 header");
a9420734
KW
2766 goto out;
2767 }
2768
b106ad91
KW
2769 /* Write a refcount table with one refcount block */
2770 refcount_table = g_malloc0(2 * cluster_size);
2771 refcount_table[0] = cpu_to_be64(2 * cluster_size);
8341f00d 2772 ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0);
7267c094 2773 g_free(refcount_table);
a9420734
KW
2774
2775 if (ret < 0) {
3ef6c40a 2776 error_setg_errno(errp, -ret, "Could not write refcount table");
a9420734
KW
2777 goto out;
2778 }
2779
23588797
KW
2780 blk_unref(blk);
2781 blk = NULL;
a9420734
KW
2782
2783 /*
2784 * And now open the image and make it consistent first (i.e. increase the
2785 * refcount of the cluster that is occupied by the header and the refcount
2786 * table)
2787 */
e6641719 2788 options = qdict_new();
46f5ac20 2789 qdict_put_str(options, "driver", "qcow2");
efaa7c4e 2790 blk = blk_new_open(filename, NULL, options,
55880601
KW
2791 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
2792 &local_err);
23588797 2793 if (blk == NULL) {
3ef6c40a 2794 error_propagate(errp, local_err);
23588797 2795 ret = -EIO;
a9420734
KW
2796 goto out;
2797 }
2798
23588797 2799 ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
a9420734 2800 if (ret < 0) {
3ef6c40a
HR
2801 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
2802 "header and refcount table");
a9420734
KW
2803 goto out;
2804
2805 } else if (ret != 0) {
2806 error_report("Huh, first cluster in empty image is already in use?");
2807 abort();
2808 }
2809
b527c9b3 2810 /* Create a full header (including things like feature table) */
23588797 2811 ret = qcow2_update_header(blk_bs(blk));
b527c9b3
KW
2812 if (ret < 0) {
2813 error_setg_errno(errp, -ret, "Could not update qcow2 header");
2814 goto out;
2815 }
2816
a9420734 2817 /* Okay, now that we have a valid image, let's give it the right size */
3a691c50 2818 ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, errp);
a9420734 2819 if (ret < 0) {
ed3d2ec9 2820 error_prepend(errp, "Could not resize image: ");
a9420734
KW
2821 goto out;
2822 }
2823
2824 /* Want a backing file? There you go.*/
2825 if (backing_file) {
23588797 2826 ret = bdrv_change_backing_file(blk_bs(blk), backing_file, backing_format);
a9420734 2827 if (ret < 0) {
3ef6c40a
HR
2828 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
2829 "with format '%s'", backing_file, backing_format);
a9420734
KW
2830 goto out;
2831 }
2832 }
2833
b25b387f
DB
2834 /* Want encryption? There you go. */
2835 if (encryptfmt) {
2836 ret = qcow2_set_up_encryption(blk_bs(blk), encryptfmt, opts, errp);
2837 if (ret < 0) {
2838 goto out;
2839 }
2840 }
2841
a9420734 2842 /* And if we're supposed to preallocate metadata, do that now */
0e4271b7 2843 if (prealloc != PREALLOC_MODE_OFF) {
7bc45dc1 2844 ret = preallocate(blk_bs(blk), 0, total_size);
a9420734 2845 if (ret < 0) {
3ef6c40a 2846 error_setg_errno(errp, -ret, "Could not preallocate metadata");
a9420734
KW
2847 goto out;
2848 }
2849 }
2850
23588797
KW
2851 blk_unref(blk);
2852 blk = NULL;
ba2ab2f2 2853
b25b387f
DB
2854 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
2855 * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
2856 * have to setup decryption context. We're not doing any I/O on the top
2857 * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
2858 * not have effect.
2859 */
e6641719 2860 options = qdict_new();
46f5ac20 2861 qdict_put_str(options, "driver", "qcow2");
efaa7c4e 2862 blk = blk_new_open(filename, NULL, options,
b25b387f
DB
2863 BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
2864 &local_err);
23588797 2865 if (blk == NULL) {
ba2ab2f2 2866 error_propagate(errp, local_err);
23588797 2867 ret = -EIO;
ba2ab2f2
HR
2868 goto out;
2869 }
2870
a9420734
KW
2871 ret = 0;
2872out:
23588797
KW
2873 if (blk) {
2874 blk_unref(blk);
f67503e5 2875 }
a9420734
KW
2876 return ret;
2877}
de5f3f40 2878
1bd0e2d1 2879static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
de5f3f40 2880{
1bd0e2d1
CL
2881 char *backing_file = NULL;
2882 char *backing_fmt = NULL;
2883 char *buf = NULL;
180e9526 2884 uint64_t size = 0;
de5f3f40 2885 int flags = 0;
99cce9fa 2886 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
ffeaac9b 2887 PreallocMode prealloc;
0eb4a8c1
SH
2888 int version;
2889 uint64_t refcount_bits;
bd4b167f 2890 int refcount_order;
0696ae2c 2891 char *encryptfmt = NULL;
3ef6c40a
HR
2892 Error *local_err = NULL;
2893 int ret;
de5f3f40
KW
2894
2895 /* Read out options */
180e9526
HT
2896 size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2897 BDRV_SECTOR_SIZE);
1bd0e2d1
CL
2898 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2899 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
0cb8d47b
DB
2900 encryptfmt = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
2901 if (encryptfmt) {
0696ae2c 2902 if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) {
0cb8d47b
DB
2903 error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and "
2904 BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive");
2905 ret = -EINVAL;
2906 goto finish;
2907 }
2908 } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
0696ae2c 2909 encryptfmt = g_strdup("aes");
1bd0e2d1 2910 }
0eb4a8c1
SH
2911 cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
2912 if (local_err) {
2913 error_propagate(errp, local_err);
2914 ret = -EINVAL;
2915 goto finish;
2916 }
1bd0e2d1 2917 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
f7abe0ec 2918 prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
06c60b6c 2919 PREALLOC_MODE_OFF, &local_err);
ffeaac9b
HT
2920 if (local_err) {
2921 error_propagate(errp, local_err);
1bd0e2d1
CL
2922 ret = -EINVAL;
2923 goto finish;
2924 }
0eb4a8c1
SH
2925
2926 version = qcow2_opt_get_version_del(opts, &local_err);
2927 if (local_err) {
2928 error_propagate(errp, local_err);
1bd0e2d1
CL
2929 ret = -EINVAL;
2930 goto finish;
2931 }
2932
2933 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) {
2934 flags |= BLOCK_FLAG_LAZY_REFCOUNTS;
de5f3f40
KW
2935 }
2936
ffeaac9b 2937 if (backing_file && prealloc != PREALLOC_MODE_OFF) {
3ef6c40a
HR
2938 error_setg(errp, "Backing file and preallocation cannot be used at "
2939 "the same time");
1bd0e2d1
CL
2940 ret = -EINVAL;
2941 goto finish;
de5f3f40
KW
2942 }
2943
bfe8043e 2944 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
3ef6c40a
HR
2945 error_setg(errp, "Lazy refcounts only supported with compatibility "
2946 "level 1.1 and above (use compat=1.1 or greater)");
1bd0e2d1
CL
2947 ret = -EINVAL;
2948 goto finish;
bfe8043e
SH
2949 }
2950
0eb4a8c1
SH
2951 refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
2952 if (local_err) {
2953 error_propagate(errp, local_err);
bd4b167f
HR
2954 ret = -EINVAL;
2955 goto finish;
2956 }
2957
786a4ea8 2958 refcount_order = ctz32(refcount_bits);
bd4b167f 2959
180e9526 2960 ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
bd4b167f 2961 cluster_size, prealloc, opts, version, refcount_order,
0cb8d47b 2962 encryptfmt, &local_err);
621ff94d 2963 error_propagate(errp, local_err);
1bd0e2d1
CL
2964
2965finish:
2966 g_free(backing_file);
2967 g_free(backing_fmt);
0696ae2c 2968 g_free(encryptfmt);
1bd0e2d1 2969 g_free(buf);
3ef6c40a 2970 return ret;
de5f3f40
KW
2971}
2972
2928abce 2973
ebb718a5
EB
2974static bool is_zero_sectors(BlockDriverState *bs, int64_t start,
2975 uint32_t count)
2928abce 2976{
2928abce
DL
2977 int nr;
2978 BlockDriverState *file;
ebb718a5 2979 int64_t res;
2928abce 2980
fbaa6bb3
EB
2981 if (start + count > bs->total_sectors) {
2982 count = bs->total_sectors - start;
2983 }
2984
ebb718a5
EB
2985 if (!count) {
2986 return true;
2987 }
2988 res = bdrv_get_block_status_above(bs, NULL, start, count,
2989 &nr, &file);
2990 return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == count;
2928abce
DL
2991}
2992
5544b59f 2993static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 2994 int64_t offset, int bytes, BdrvRequestFlags flags)
621f0589
KW
2995{
2996 int ret;
ff99129a 2997 BDRVQcow2State *s = bs->opaque;
621f0589 2998
5544b59f 2999 uint32_t head = offset % s->cluster_size;
f5a5ca79 3000 uint32_t tail = (offset + bytes) % s->cluster_size;
2928abce 3001
f5a5ca79
MP
3002 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
3003 if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
fbaa6bb3
EB
3004 tail = 0;
3005 }
5a64e942 3006
ebb718a5 3007 if (head || tail) {
5544b59f 3008 int64_t cl_start = (offset - head) >> BDRV_SECTOR_BITS;
ebb718a5 3009 uint64_t off;
ecfe1863 3010 unsigned int nr;
2928abce 3011
f5a5ca79 3012 assert(head + bytes <= s->cluster_size);
2928abce 3013
ebb718a5 3014 /* check whether remainder of cluster already reads as zero */
5544b59f
EB
3015 if (!(is_zero_sectors(bs, cl_start,
3016 DIV_ROUND_UP(head, BDRV_SECTOR_SIZE)) &&
f5a5ca79 3017 is_zero_sectors(bs, (offset + bytes) >> BDRV_SECTOR_BITS,
5544b59f
EB
3018 DIV_ROUND_UP(-tail & (s->cluster_size - 1),
3019 BDRV_SECTOR_SIZE)))) {
2928abce
DL
3020 return -ENOTSUP;
3021 }
3022
2928abce
DL
3023 qemu_co_mutex_lock(&s->lock);
3024 /* We can have new write after previous check */
5544b59f 3025 offset = cl_start << BDRV_SECTOR_BITS;
f5a5ca79 3026 bytes = s->cluster_size;
ecfe1863 3027 nr = s->cluster_size;
5544b59f 3028 ret = qcow2_get_cluster_offset(bs, offset, &nr, &off);
fdfab37d
EB
3029 if (ret != QCOW2_CLUSTER_UNALLOCATED &&
3030 ret != QCOW2_CLUSTER_ZERO_PLAIN &&
3031 ret != QCOW2_CLUSTER_ZERO_ALLOC) {
2928abce
DL
3032 qemu_co_mutex_unlock(&s->lock);
3033 return -ENOTSUP;
3034 }
3035 } else {
3036 qemu_co_mutex_lock(&s->lock);
621f0589
KW
3037 }
3038
f5a5ca79 3039 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
5a64e942 3040
621f0589 3041 /* Whatever is left can use real zero clusters */
f5a5ca79 3042 ret = qcow2_cluster_zeroize(bs, offset, bytes, flags);
621f0589
KW
3043 qemu_co_mutex_unlock(&s->lock);
3044
3045 return ret;
3046}
3047
82e8a788 3048static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs,
f5a5ca79 3049 int64_t offset, int bytes)
5ea929e3 3050{
6db39ae2 3051 int ret;
ff99129a 3052 BDRVQcow2State *s = bs->opaque;
6db39ae2 3053
f5a5ca79
MP
3054 if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
3055 assert(bytes < s->cluster_size);
048c5fd1
EB
3056 /* Ignore partial clusters, except for the special case of the
3057 * complete partial cluster at the end of an unaligned file */
3058 if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
f5a5ca79 3059 offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
048c5fd1
EB
3060 return -ENOTSUP;
3061 }
49228d1e
EB
3062 }
3063
6db39ae2 3064 qemu_co_mutex_lock(&s->lock);
f5a5ca79 3065 ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
d2cb36af 3066 false);
6db39ae2
PB
3067 qemu_co_mutex_unlock(&s->lock);
3068 return ret;
5ea929e3
KW
3069}
3070
8243ccb7
HR
3071static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
3072 PreallocMode prealloc, Error **errp)
419b19d9 3073{
ff99129a 3074 BDRVQcow2State *s = bs->opaque;
95b98f34 3075 uint64_t old_length;
2cf7cfa1
KW
3076 int64_t new_l1_size;
3077 int ret;
419b19d9 3078
772d1f97
HR
3079 if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
3080 prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
3081 {
8243ccb7 3082 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 3083 PreallocMode_str(prealloc));
8243ccb7
HR
3084 return -ENOTSUP;
3085 }
3086
419b19d9 3087 if (offset & 511) {
4bff28b8 3088 error_setg(errp, "The new size must be a multiple of 512");
419b19d9
SH
3089 return -EINVAL;
3090 }
3091
3092 /* cannot proceed if image has snapshots */
3093 if (s->nb_snapshots) {
4bff28b8 3094 error_setg(errp, "Can't resize an image which has snapshots");
419b19d9
SH
3095 return -ENOTSUP;
3096 }
3097
88ddffae
VSO
3098 /* cannot proceed if image has bitmaps */
3099 if (s->nb_bitmaps) {
3100 /* TODO: resize bitmaps in the image */
3101 error_setg(errp, "Can't resize an image which has bitmaps");
3102 return -ENOTSUP;
3103 }
3104
95b98f34
HR
3105 old_length = bs->total_sectors * 512;
3106
419b19d9 3107 /* shrinking is currently not supported */
95b98f34 3108 if (offset < old_length) {
4bff28b8 3109 error_setg(errp, "qcow2 doesn't support shrinking images yet");
419b19d9
SH
3110 return -ENOTSUP;
3111 }
3112
3113 new_l1_size = size_to_l1(s, offset);
72893756 3114 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
419b19d9 3115 if (ret < 0) {
f59adb32 3116 error_setg_errno(errp, -ret, "Failed to grow the L1 table");
419b19d9
SH
3117 return ret;
3118 }
3119
95b98f34
HR
3120 switch (prealloc) {
3121 case PREALLOC_MODE_OFF:
3122 break;
3123
3124 case PREALLOC_MODE_METADATA:
3125 ret = preallocate(bs, old_length, offset);
3126 if (ret < 0) {
3127 error_setg_errno(errp, -ret, "Preallocation failed");
3128 return ret;
3129 }
3130 break;
3131
772d1f97
HR
3132 case PREALLOC_MODE_FALLOC:
3133 case PREALLOC_MODE_FULL:
3134 {
3135 int64_t allocation_start, host_offset, guest_offset;
3136 int64_t clusters_allocated;
3137 int64_t old_file_size, new_file_size;
3138 uint64_t nb_new_data_clusters, nb_new_l2_tables;
3139
3140 old_file_size = bdrv_getlength(bs->file->bs);
3141 if (old_file_size < 0) {
3142 error_setg_errno(errp, -old_file_size,
3143 "Failed to inquire current file length");
3144 return ret;
3145 }
3146
3147 nb_new_data_clusters = DIV_ROUND_UP(offset - old_length,
3148 s->cluster_size);
3149
3150 /* This is an overestimation; we will not actually allocate space for
3151 * these in the file but just make sure the new refcount structures are
3152 * able to cover them so we will not have to allocate new refblocks
3153 * while entering the data blocks in the potentially new L2 tables.
3154 * (We do not actually care where the L2 tables are placed. Maybe they
3155 * are already allocated or they can be placed somewhere before
3156 * @old_file_size. It does not matter because they will be fully
3157 * allocated automatically, so they do not need to be covered by the
3158 * preallocation. All that matters is that we will not have to allocate
3159 * new refcount structures for them.) */
3160 nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
3161 s->cluster_size / sizeof(uint64_t));
3162 /* The cluster range may not be aligned to L2 boundaries, so add one L2
3163 * table for a potential head/tail */
3164 nb_new_l2_tables++;
3165
3166 allocation_start = qcow2_refcount_area(bs, old_file_size,
3167 nb_new_data_clusters +
3168 nb_new_l2_tables,
3169 true, 0, 0);
3170 if (allocation_start < 0) {
3171 error_setg_errno(errp, -allocation_start,
3172 "Failed to resize refcount structures");
3173 return -allocation_start;
3174 }
3175
3176 clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
3177 nb_new_data_clusters);
3178 if (clusters_allocated < 0) {
3179 error_setg_errno(errp, -clusters_allocated,
3180 "Failed to allocate data clusters");
3181 return -clusters_allocated;
3182 }
3183
3184 assert(clusters_allocated == nb_new_data_clusters);
3185
3186 /* Allocate the data area */
3187 new_file_size = allocation_start +
3188 nb_new_data_clusters * s->cluster_size;
3189 ret = bdrv_truncate(bs->file, new_file_size, prealloc, errp);
3190 if (ret < 0) {
3191 error_prepend(errp, "Failed to resize underlying file: ");
3192 qcow2_free_clusters(bs, allocation_start,
3193 nb_new_data_clusters * s->cluster_size,
3194 QCOW2_DISCARD_OTHER);
3195 return ret;
3196 }
3197
3198 /* Create the necessary L2 entries */
3199 host_offset = allocation_start;
3200 guest_offset = old_length;
3201 while (nb_new_data_clusters) {
3202 int64_t guest_cluster = guest_offset >> s->cluster_bits;
3203 int64_t nb_clusters = MIN(nb_new_data_clusters,
3204 s->l2_size - guest_cluster % s->l2_size);
3205 QCowL2Meta allocation = {
3206 .offset = guest_offset,
3207 .alloc_offset = host_offset,
3208 .nb_clusters = nb_clusters,
3209 };
3210 qemu_co_queue_init(&allocation.dependent_requests);
3211
3212 ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
3213 if (ret < 0) {
3214 error_setg_errno(errp, -ret, "Failed to update L2 tables");
3215 qcow2_free_clusters(bs, host_offset,
3216 nb_new_data_clusters * s->cluster_size,
3217 QCOW2_DISCARD_OTHER);
3218 return ret;
3219 }
3220
3221 guest_offset += nb_clusters * s->cluster_size;
3222 host_offset += nb_clusters * s->cluster_size;
3223 nb_new_data_clusters -= nb_clusters;
3224 }
3225 break;
3226 }
3227
95b98f34
HR
3228 default:
3229 g_assert_not_reached();
3230 }
3231
3232 if (prealloc != PREALLOC_MODE_OFF) {
3233 /* Flush metadata before actually changing the image size */
3234 ret = bdrv_flush(bs);
3235 if (ret < 0) {
3236 error_setg_errno(errp, -ret,
3237 "Failed to flush the preallocated area to disk");
3238 return ret;
3239 }
3240 }
3241
419b19d9
SH
3242 /* write updated header.size */
3243 offset = cpu_to_be64(offset);
d9ca2ea2 3244 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
8b3b7206 3245 &offset, sizeof(uint64_t));
419b19d9 3246 if (ret < 0) {
f59adb32 3247 error_setg_errno(errp, -ret, "Failed to update the image size");
419b19d9
SH
3248 return ret;
3249 }
3250
3251 s->l1_vm_state_index = new_l1_size;
3252 return 0;
3253}
3254
20d97356
BS
3255/* XXX: put compressed sectors first, then all the cluster aligned
3256 tables to avoid losing bytes in alignment */
fcccefc5
PB
3257static coroutine_fn int
3258qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
3259 uint64_t bytes, QEMUIOVector *qiov)
20d97356 3260{
ff99129a 3261 BDRVQcow2State *s = bs->opaque;
fcccefc5
PB
3262 QEMUIOVector hd_qiov;
3263 struct iovec iov;
20d97356
BS
3264 z_stream strm;
3265 int ret, out_len;
fcccefc5 3266 uint8_t *buf, *out_buf;
d0d5d0e3 3267 int64_t cluster_offset;
20d97356 3268
fcccefc5 3269 if (bytes == 0) {
20d97356
BS
3270 /* align end of file to a sector boundary to ease reading with
3271 sector based I/Os */
9a4f4c31 3272 cluster_offset = bdrv_getlength(bs->file->bs);
d0d5d0e3
EB
3273 if (cluster_offset < 0) {
3274 return cluster_offset;
3275 }
7ea37c30 3276 return bdrv_truncate(bs->file, cluster_offset, PREALLOC_MODE_OFF, NULL);
20d97356
BS
3277 }
3278
a2c0ca6f 3279 buf = qemu_blockalign(bs, s->cluster_size);
fcccefc5 3280 if (bytes != s->cluster_size) {
a2c0ca6f
PB
3281 if (bytes > s->cluster_size ||
3282 offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
fcccefc5 3283 {
a2c0ca6f
PB
3284 qemu_vfree(buf);
3285 return -EINVAL;
f4d38bef 3286 }
a2c0ca6f
PB
3287 /* Zero-pad last write if image size is not cluster aligned */
3288 memset(buf + bytes, 0, s->cluster_size - bytes);
f4d38bef 3289 }
8b2bd093 3290 qemu_iovec_to_buf(qiov, 0, buf, bytes);
20d97356 3291
ebf7bba0 3292 out_buf = g_malloc(s->cluster_size);
20d97356
BS
3293
3294 /* best compression, small window, no zlib header */
3295 memset(&strm, 0, sizeof(strm));
3296 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
3297 Z_DEFLATED, -12,
3298 9, Z_DEFAULT_STRATEGY);
3299 if (ret != 0) {
8f1efd00
KW
3300 ret = -EINVAL;
3301 goto fail;
20d97356
BS
3302 }
3303
3304 strm.avail_in = s->cluster_size;
3305 strm.next_in = (uint8_t *)buf;
3306 strm.avail_out = s->cluster_size;
3307 strm.next_out = out_buf;
3308
3309 ret = deflate(&strm, Z_FINISH);
3310 if (ret != Z_STREAM_END && ret != Z_OK) {
20d97356 3311 deflateEnd(&strm);
8f1efd00
KW
3312 ret = -EINVAL;
3313 goto fail;
20d97356
BS
3314 }
3315 out_len = strm.next_out - out_buf;
3316
3317 deflateEnd(&strm);
3318
3319 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
3320 /* could not compress: write normal cluster */
fcccefc5 3321 ret = qcow2_co_pwritev(bs, offset, bytes, qiov, 0);
8f1efd00
KW
3322 if (ret < 0) {
3323 goto fail;
3324 }
fcccefc5
PB
3325 goto success;
3326 }
cf93980e 3327
fcccefc5
PB
3328 qemu_co_mutex_lock(&s->lock);
3329 cluster_offset =
3330 qcow2_alloc_compressed_cluster_offset(bs, offset, out_len);
3331 if (!cluster_offset) {
3332 qemu_co_mutex_unlock(&s->lock);
3333 ret = -EIO;
3334 goto fail;
3335 }
3336 cluster_offset &= s->cluster_offset_mask;
cf93980e 3337
fcccefc5
PB
3338 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
3339 qemu_co_mutex_unlock(&s->lock);
3340 if (ret < 0) {
3341 goto fail;
20d97356
BS
3342 }
3343
fcccefc5
PB
3344 iov = (struct iovec) {
3345 .iov_base = out_buf,
3346 .iov_len = out_len,
3347 };
3348 qemu_iovec_init_external(&hd_qiov, &iov, 1);
3349
3350 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
3351 ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0);
3352 if (ret < 0) {
3353 goto fail;
3354 }
3355success:
8f1efd00
KW
3356 ret = 0;
3357fail:
fcccefc5 3358 qemu_vfree(buf);
7267c094 3359 g_free(out_buf);
8f1efd00 3360 return ret;
20d97356
BS
3361}
3362
94054183
HR
3363static int make_completely_empty(BlockDriverState *bs)
3364{
ff99129a 3365 BDRVQcow2State *s = bs->opaque;
ed3d2ec9 3366 Error *local_err = NULL;
94054183
HR
3367 int ret, l1_clusters;
3368 int64_t offset;
3369 uint64_t *new_reftable = NULL;
3370 uint64_t rt_entry, l1_size2;
3371 struct {
3372 uint64_t l1_offset;
3373 uint64_t reftable_offset;
3374 uint32_t reftable_clusters;
3375 } QEMU_PACKED l1_ofs_rt_ofs_cls;
3376
3377 ret = qcow2_cache_empty(bs, s->l2_table_cache);
3378 if (ret < 0) {
3379 goto fail;
3380 }
3381
3382 ret = qcow2_cache_empty(bs, s->refcount_block_cache);
3383 if (ret < 0) {
3384 goto fail;
3385 }
3386
3387 /* Refcounts will be broken utterly */
3388 ret = qcow2_mark_dirty(bs);
3389 if (ret < 0) {
3390 goto fail;
3391 }
3392
3393 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3394
3395 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
3396 l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
3397
3398 /* After this call, neither the in-memory nor the on-disk refcount
3399 * information accurately describe the actual references */
3400
720ff280 3401 ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
74021bc4 3402 l1_clusters * s->cluster_size, 0);
94054183
HR
3403 if (ret < 0) {
3404 goto fail_broken_refcounts;
3405 }
3406 memset(s->l1_table, 0, l1_size2);
3407
3408 BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
3409
3410 /* Overwrite enough clusters at the beginning of the sectors to place
3411 * the refcount table, a refcount block and the L1 table in; this may
3412 * overwrite parts of the existing refcount and L1 table, which is not
3413 * an issue because the dirty flag is set, complete data loss is in fact
3414 * desired and partial data loss is consequently fine as well */
720ff280 3415 ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
74021bc4 3416 (2 + l1_clusters) * s->cluster_size, 0);
94054183
HR
3417 /* This call (even if it failed overall) may have overwritten on-disk
3418 * refcount structures; in that case, the in-memory refcount information
3419 * will probably differ from the on-disk information which makes the BDS
3420 * unusable */
3421 if (ret < 0) {
3422 goto fail_broken_refcounts;
3423 }
3424
3425 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
3426 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
3427
3428 /* "Create" an empty reftable (one cluster) directly after the image
3429 * header and an empty L1 table three clusters after the image header;
3430 * the cluster between those two will be used as the first refblock */
f1f7a1dd
PM
3431 l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
3432 l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
3433 l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
d9ca2ea2 3434 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
94054183
HR
3435 &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
3436 if (ret < 0) {
3437 goto fail_broken_refcounts;
3438 }
3439
3440 s->l1_table_offset = 3 * s->cluster_size;
3441
3442 new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
3443 if (!new_reftable) {
3444 ret = -ENOMEM;
3445 goto fail_broken_refcounts;
3446 }
3447
3448 s->refcount_table_offset = s->cluster_size;
3449 s->refcount_table_size = s->cluster_size / sizeof(uint64_t);
7061a078 3450 s->max_refcount_table_index = 0;
94054183
HR
3451
3452 g_free(s->refcount_table);
3453 s->refcount_table = new_reftable;
3454 new_reftable = NULL;
3455
3456 /* Now the in-memory refcount information again corresponds to the on-disk
3457 * information (reftable is empty and no refblocks (the refblock cache is
3458 * empty)); however, this means some clusters (e.g. the image header) are
3459 * referenced, but not refcounted, but the normal qcow2 code assumes that
3460 * the in-memory information is always correct */
3461
3462 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
3463
3464 /* Enter the first refblock into the reftable */
3465 rt_entry = cpu_to_be64(2 * s->cluster_size);
d9ca2ea2 3466 ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
94054183
HR
3467 &rt_entry, sizeof(rt_entry));
3468 if (ret < 0) {
3469 goto fail_broken_refcounts;
3470 }
3471 s->refcount_table[0] = 2 * s->cluster_size;
3472
3473 s->free_cluster_index = 0;
3474 assert(3 + l1_clusters <= s->refcount_block_size);
3475 offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
3476 if (offset < 0) {
3477 ret = offset;
3478 goto fail_broken_refcounts;
3479 } else if (offset > 0) {
3480 error_report("First cluster in emptied image is in use");
3481 abort();
3482 }
3483
3484 /* Now finally the in-memory information corresponds to the on-disk
3485 * structures and is correct */
3486 ret = qcow2_mark_clean(bs);
3487 if (ret < 0) {
3488 goto fail;
3489 }
3490
ed3d2ec9 3491 ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,
7ea37c30 3492 PREALLOC_MODE_OFF, &local_err);
94054183 3493 if (ret < 0) {
ed3d2ec9 3494 error_report_err(local_err);
94054183
HR
3495 goto fail;
3496 }
3497
3498 return 0;
3499
3500fail_broken_refcounts:
3501 /* The BDS is unusable at this point. If we wanted to make it usable, we
3502 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
3503 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
3504 * again. However, because the functions which could have caused this error
3505 * path to be taken are used by those functions as well, it's very likely
3506 * that that sequence will fail as well. Therefore, just eject the BDS. */
3507 bs->drv = NULL;
3508
3509fail:
3510 g_free(new_reftable);
3511 return ret;
3512}
3513
491d27e2
HR
3514static int qcow2_make_empty(BlockDriverState *bs)
3515{
ff99129a 3516 BDRVQcow2State *s = bs->opaque;
d2cb36af
EB
3517 uint64_t offset, end_offset;
3518 int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
94054183
HR
3519 int l1_clusters, ret = 0;
3520
3521 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
3522
3523 if (s->qcow_version >= 3 && !s->snapshots &&
3524 3 + l1_clusters <= s->refcount_block_size) {
3525 /* The following function only works for qcow2 v3 images (it requires
3526 * the dirty flag) and only as long as there are no snapshots (because
3527 * it completely empties the image). Furthermore, the L1 table and three
3528 * additional clusters (image header, refcount table, one refcount
3529 * block) have to fit inside one refcount block. */
3530 return make_completely_empty(bs);
3531 }
491d27e2 3532
94054183
HR
3533 /* This fallback code simply discards every active cluster; this is slow,
3534 * but works in all cases */
d2cb36af
EB
3535 end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
3536 for (offset = 0; offset < end_offset; offset += step) {
491d27e2
HR
3537 /* As this function is generally used after committing an external
3538 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
3539 * default action for this kind of discard is to pass the discard,
3540 * which will ideally result in an actually smaller image file, as
3541 * is probably desired. */
d2cb36af
EB
3542 ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
3543 QCOW2_DISCARD_SNAPSHOT, true);
491d27e2
HR
3544 if (ret < 0) {
3545 break;
3546 }
3547 }
3548
3549 return ret;
3550}
3551
a968168c 3552static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
20d97356 3553{
ff99129a 3554 BDRVQcow2State *s = bs->opaque;
29c1a730
KW
3555 int ret;
3556
8b94ff85 3557 qemu_co_mutex_lock(&s->lock);
f3c3b87d 3558 ret = qcow2_cache_write(bs, s->l2_table_cache);
29c1a730 3559 if (ret < 0) {
c95de7e2 3560 qemu_co_mutex_unlock(&s->lock);
8b94ff85 3561 return ret;
29c1a730
KW
3562 }
3563
bfe8043e 3564 if (qcow2_need_accurate_refcounts(s)) {
f3c3b87d 3565 ret = qcow2_cache_write(bs, s->refcount_block_cache);
bfe8043e
SH
3566 if (ret < 0) {
3567 qemu_co_mutex_unlock(&s->lock);
3568 return ret;
3569 }
29c1a730 3570 }
8b94ff85 3571 qemu_co_mutex_unlock(&s->lock);
29c1a730 3572
eb489bb1
KW
3573 return 0;
3574}
3575
c501c352
SH
3576static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
3577 Error **errp)
3578{
3579 Error *local_err = NULL;
3580 BlockMeasureInfo *info;
3581 uint64_t required = 0; /* bytes that contribute to required size */
3582 uint64_t virtual_size; /* disk size as seen by guest */
3583 uint64_t refcount_bits;
3584 uint64_t l2_tables;
3585 size_t cluster_size;
3586 int version;
3587 char *optstr;
3588 PreallocMode prealloc;
3589 bool has_backing_file;
3590
3591 /* Parse image creation options */
3592 cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err);
3593 if (local_err) {
3594 goto err;
3595 }
3596
3597 version = qcow2_opt_get_version_del(opts, &local_err);
3598 if (local_err) {
3599 goto err;
3600 }
3601
3602 refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
3603 if (local_err) {
3604 goto err;
3605 }
3606
3607 optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
f7abe0ec 3608 prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
06c60b6c 3609 PREALLOC_MODE_OFF, &local_err);
c501c352
SH
3610 g_free(optstr);
3611 if (local_err) {
3612 goto err;
3613 }
3614
3615 optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
3616 has_backing_file = !!optstr;
3617 g_free(optstr);
3618
3619 virtual_size = align_offset(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
3620 cluster_size);
3621
3622 /* Check that virtual disk size is valid */
3623 l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
3624 cluster_size / sizeof(uint64_t));
3625 if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) {
3626 error_setg(&local_err, "The image size is too large "
3627 "(try using a larger cluster size)");
3628 goto err;
3629 }
3630
3631 /* Account for input image */
3632 if (in_bs) {
3633 int64_t ssize = bdrv_getlength(in_bs);
3634 if (ssize < 0) {
3635 error_setg_errno(&local_err, -ssize,
3636 "Unable to get image virtual_size");
3637 goto err;
3638 }
3639
3640 virtual_size = align_offset(ssize, cluster_size);
3641
3642 if (has_backing_file) {
3643 /* We don't how much of the backing chain is shared by the input
3644 * image and the new image file. In the worst case the new image's
3645 * backing file has nothing in common with the input image. Be
3646 * conservative and assume all clusters need to be written.
3647 */
3648 required = virtual_size;
3649 } else {
3650 int cluster_sectors = cluster_size / BDRV_SECTOR_SIZE;
3651 int64_t sector_num;
3652 int pnum = 0;
3653
3654 for (sector_num = 0;
3655 sector_num < ssize / BDRV_SECTOR_SIZE;
3656 sector_num += pnum) {
24bae02b
EB
3657 int nb_sectors = MIN(ssize / BDRV_SECTOR_SIZE - sector_num,
3658 BDRV_REQUEST_MAX_SECTORS);
c501c352
SH
3659 BlockDriverState *file;
3660 int64_t ret;
3661
3662 ret = bdrv_get_block_status_above(in_bs, NULL,
3663 sector_num, nb_sectors,
3664 &pnum, &file);
3665 if (ret < 0) {
3666 error_setg_errno(&local_err, -ret,
3667 "Unable to get block status");
3668 goto err;
3669 }
3670
3671 if (ret & BDRV_BLOCK_ZERO) {
3672 /* Skip zero regions (safe with no backing file) */
3673 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
3674 (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
3675 /* Extend pnum to end of cluster for next iteration */
3676 pnum = ROUND_UP(sector_num + pnum, cluster_sectors) -
3677 sector_num;
3678
3679 /* Count clusters we've seen */
3680 required += (sector_num % cluster_sectors + pnum) *
3681 BDRV_SECTOR_SIZE;
3682 }
3683 }
3684 }
3685 }
3686
3687 /* Take into account preallocation. Nothing special is needed for
3688 * PREALLOC_MODE_METADATA since metadata is always counted.
3689 */
3690 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
3691 required = virtual_size;
3692 }
3693
3694 info = g_new(BlockMeasureInfo, 1);
3695 info->fully_allocated =
3696 qcow2_calc_prealloc_size(virtual_size, cluster_size,
3697 ctz32(refcount_bits));
3698
3699 /* Remove data clusters that are not required. This overestimates the
3700 * required size because metadata needed for the fully allocated file is
3701 * still counted.
3702 */
3703 info->required = info->fully_allocated - virtual_size + required;
3704 return info;
3705
3706err:
3707 error_propagate(errp, local_err);
3708 return NULL;
3709}
3710
7c80ab3f 3711static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
20d97356 3712{
ff99129a 3713 BDRVQcow2State *s = bs->opaque;
95de6d70
PB
3714 bdi->unallocated_blocks_are_zero = true;
3715 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
20d97356 3716 bdi->cluster_size = s->cluster_size;
7c80ab3f 3717 bdi->vm_state_offset = qcow2_vm_state_offset(s);
20d97356
BS
3718 return 0;
3719}
3720
37764dfb
HR
3721static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
3722{
ff99129a 3723 BDRVQcow2State *s = bs->opaque;
0a12f6f8
DB
3724 ImageInfoSpecific *spec_info;
3725 QCryptoBlockInfo *encrypt_info = NULL;
37764dfb 3726
0a12f6f8
DB
3727 if (s->crypto != NULL) {
3728 encrypt_info = qcrypto_block_get_info(s->crypto, &error_abort);
3729 }
3730
3731 spec_info = g_new(ImageInfoSpecific, 1);
37764dfb 3732 *spec_info = (ImageInfoSpecific){
6a8f9661 3733 .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
32bafa8f 3734 .u.qcow2.data = g_new(ImageInfoSpecificQCow2, 1),
37764dfb
HR
3735 };
3736 if (s->qcow_version == 2) {
32bafa8f 3737 *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
0709c5a1
HR
3738 .compat = g_strdup("0.10"),
3739 .refcount_bits = s->refcount_bits,
37764dfb
HR
3740 };
3741 } else if (s->qcow_version == 3) {
32bafa8f 3742 *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
37764dfb
HR
3743 .compat = g_strdup("1.1"),
3744 .lazy_refcounts = s->compatible_features &
3745 QCOW2_COMPAT_LAZY_REFCOUNTS,
3746 .has_lazy_refcounts = true,
9009b196
HR
3747 .corrupt = s->incompatible_features &
3748 QCOW2_INCOMPAT_CORRUPT,
3749 .has_corrupt = true,
0709c5a1 3750 .refcount_bits = s->refcount_bits,
37764dfb 3751 };
b1fc8f93
DL
3752 } else {
3753 /* if this assertion fails, this probably means a new version was
3754 * added without having it covered here */
3755 assert(false);
37764dfb
HR
3756 }
3757
0a12f6f8
DB
3758 if (encrypt_info) {
3759 ImageInfoSpecificQCow2Encryption *qencrypt =
3760 g_new(ImageInfoSpecificQCow2Encryption, 1);
3761 switch (encrypt_info->format) {
3762 case Q_CRYPTO_BLOCK_FORMAT_QCOW:
3763 qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
3764 qencrypt->u.aes = encrypt_info->u.qcow;
3765 break;
3766 case Q_CRYPTO_BLOCK_FORMAT_LUKS:
3767 qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
3768 qencrypt->u.luks = encrypt_info->u.luks;
3769 break;
3770 default:
3771 abort();
3772 }
3773 /* Since we did shallow copy above, erase any pointers
3774 * in the original info */
3775 memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
3776 qapi_free_QCryptoBlockInfo(encrypt_info);
3777
3778 spec_info->u.qcow2.data->has_encrypt = true;
3779 spec_info->u.qcow2.data->encrypt = qencrypt;
3780 }
3781
37764dfb
HR
3782 return spec_info;
3783}
3784
cf8074b3
KW
3785static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
3786 int64_t pos)
20d97356 3787{
ff99129a 3788 BDRVQcow2State *s = bs->opaque;
20d97356 3789
66f82cee 3790 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
734a7758
KW
3791 return bs->drv->bdrv_co_pwritev(bs, qcow2_vm_state_offset(s) + pos,
3792 qiov->size, qiov, 0);
20d97356
BS
3793}
3794
5ddda0b8
KW
3795static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
3796 int64_t pos)
20d97356 3797{
ff99129a 3798 BDRVQcow2State *s = bs->opaque;
20d97356 3799
66f82cee 3800 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
734a7758
KW
3801 return bs->drv->bdrv_co_preadv(bs, qcow2_vm_state_offset(s) + pos,
3802 qiov->size, qiov, 0);
20d97356
BS
3803}
3804
9296b3ed
HR
3805/*
3806 * Downgrades an image's version. To achieve this, any incompatible features
3807 * have to be removed.
3808 */
4057a2b2 3809static int qcow2_downgrade(BlockDriverState *bs, int target_version,
8b13976d 3810 BlockDriverAmendStatusCB *status_cb, void *cb_opaque)
9296b3ed 3811{
ff99129a 3812 BDRVQcow2State *s = bs->opaque;
9296b3ed
HR
3813 int current_version = s->qcow_version;
3814 int ret;
3815
3816 if (target_version == current_version) {
3817 return 0;
3818 } else if (target_version > current_version) {
3819 return -EINVAL;
3820 } else if (target_version != 2) {
3821 return -EINVAL;
3822 }
3823
3824 if (s->refcount_order != 4) {
61ce55fc 3825 error_report("compat=0.10 requires refcount_bits=16");
9296b3ed
HR
3826 return -ENOTSUP;
3827 }
3828
3829 /* clear incompatible features */
3830 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
3831 ret = qcow2_mark_clean(bs);
3832 if (ret < 0) {
3833 return ret;
3834 }
3835 }
3836
3837 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
3838 * the first place; if that happens nonetheless, returning -ENOTSUP is the
3839 * best thing to do anyway */
3840
3841 if (s->incompatible_features) {
3842 return -ENOTSUP;
3843 }
3844
3845 /* since we can ignore compatible features, we can set them to 0 as well */
3846 s->compatible_features = 0;
3847 /* if lazy refcounts have been used, they have already been fixed through
3848 * clearing the dirty flag */
3849
3850 /* clearing autoclear features is trivial */
3851 s->autoclear_features = 0;
3852
8b13976d 3853 ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
9296b3ed
HR
3854 if (ret < 0) {
3855 return ret;
3856 }
3857
3858 s->qcow_version = target_version;
3859 ret = qcow2_update_header(bs);
3860 if (ret < 0) {
3861 s->qcow_version = current_version;
3862 return ret;
3863 }
3864 return 0;
3865}
3866
c293a809
HR
3867typedef enum Qcow2AmendOperation {
3868 /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
3869 * statically initialized to so that the helper CB can discern the first
3870 * invocation from an operation change */
3871 QCOW2_NO_OPERATION = 0,
3872
61ce55fc 3873 QCOW2_CHANGING_REFCOUNT_ORDER,
c293a809
HR
3874 QCOW2_DOWNGRADING,
3875} Qcow2AmendOperation;
3876
3877typedef struct Qcow2AmendHelperCBInfo {
3878 /* The code coordinating the amend operations should only modify
3879 * these four fields; the rest will be managed by the CB */
3880 BlockDriverAmendStatusCB *original_status_cb;
3881 void *original_cb_opaque;
3882
3883 Qcow2AmendOperation current_operation;
3884
3885 /* Total number of operations to perform (only set once) */
3886 int total_operations;
3887
3888 /* The following fields are managed by the CB */
3889
3890 /* Number of operations completed */
3891 int operations_completed;
3892
3893 /* Cumulative offset of all completed operations */
3894 int64_t offset_completed;
3895
3896 Qcow2AmendOperation last_operation;
3897 int64_t last_work_size;
3898} Qcow2AmendHelperCBInfo;
3899
3900static void qcow2_amend_helper_cb(BlockDriverState *bs,
3901 int64_t operation_offset,
3902 int64_t operation_work_size, void *opaque)
3903{
3904 Qcow2AmendHelperCBInfo *info = opaque;
3905 int64_t current_work_size;
3906 int64_t projected_work_size;
3907
3908 if (info->current_operation != info->last_operation) {
3909 if (info->last_operation != QCOW2_NO_OPERATION) {
3910 info->offset_completed += info->last_work_size;
3911 info->operations_completed++;
3912 }
3913
3914 info->last_operation = info->current_operation;
3915 }
3916
3917 assert(info->total_operations > 0);
3918 assert(info->operations_completed < info->total_operations);
3919
3920 info->last_work_size = operation_work_size;
3921
3922 current_work_size = info->offset_completed + operation_work_size;
3923
3924 /* current_work_size is the total work size for (operations_completed + 1)
3925 * operations (which includes this one), so multiply it by the number of
3926 * operations not covered and divide it by the number of operations
3927 * covered to get a projection for the operations not covered */
3928 projected_work_size = current_work_size * (info->total_operations -
3929 info->operations_completed - 1)
3930 / (info->operations_completed + 1);
3931
3932 info->original_status_cb(bs, info->offset_completed + operation_offset,
3933 current_work_size + projected_work_size,
3934 info->original_cb_opaque);
3935}
3936
77485434 3937static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
8b13976d
HR
3938 BlockDriverAmendStatusCB *status_cb,
3939 void *cb_opaque)
9296b3ed 3940{
ff99129a 3941 BDRVQcow2State *s = bs->opaque;
9296b3ed
HR
3942 int old_version = s->qcow_version, new_version = old_version;
3943 uint64_t new_size = 0;
3944 const char *backing_file = NULL, *backing_format = NULL;
3945 bool lazy_refcounts = s->use_lazy_refcounts;
1bd0e2d1
CL
3946 const char *compat = NULL;
3947 uint64_t cluster_size = s->cluster_size;
3948 bool encrypt;
4652b8f3 3949 int encformat;
61ce55fc 3950 int refcount_bits = s->refcount_bits;
d7086422 3951 Error *local_err = NULL;
9296b3ed 3952 int ret;
1bd0e2d1 3953 QemuOptDesc *desc = opts->list->desc;
c293a809 3954 Qcow2AmendHelperCBInfo helper_cb_info;
9296b3ed 3955
1bd0e2d1
CL
3956 while (desc && desc->name) {
3957 if (!qemu_opt_find(opts, desc->name)) {
9296b3ed 3958 /* only change explicitly defined options */
1bd0e2d1 3959 desc++;
9296b3ed
HR
3960 continue;
3961 }
3962
8a17b83c
HR
3963 if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
3964 compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
1bd0e2d1 3965 if (!compat) {
9296b3ed 3966 /* preserve default */
1bd0e2d1 3967 } else if (!strcmp(compat, "0.10")) {
9296b3ed 3968 new_version = 2;
1bd0e2d1 3969 } else if (!strcmp(compat, "1.1")) {
9296b3ed
HR
3970 new_version = 3;
3971 } else {
29d72431 3972 error_report("Unknown compatibility level %s", compat);
9296b3ed
HR
3973 return -EINVAL;
3974 }
8a17b83c 3975 } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) {
29d72431 3976 error_report("Cannot change preallocation mode");
9296b3ed 3977 return -ENOTSUP;
8a17b83c
HR
3978 } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
3979 new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
3980 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
3981 backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
3982 } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
3983 backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
3984 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) {
3985 encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT,
b25b387f 3986 !!s->crypto);
f6fa64f6 3987
b25b387f 3988 if (encrypt != !!s->crypto) {
29d72431 3989 error_report("Changing the encryption flag is not supported");
9296b3ed
HR
3990 return -ENOTSUP;
3991 }
4652b8f3
DB
3992 } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) {
3993 encformat = qcow2_crypt_method_from_format(
3994 qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT));
3995
3996 if (encformat != s->crypt_method_header) {
3997 error_report("Changing the encryption format is not supported");
3998 return -ENOTSUP;
3999 }
8a17b83c
HR
4000 } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) {
4001 cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE,
1bd0e2d1
CL
4002 cluster_size);
4003 if (cluster_size != s->cluster_size) {
29d72431 4004 error_report("Changing the cluster size is not supported");
9296b3ed
HR
4005 return -ENOTSUP;
4006 }
8a17b83c
HR
4007 } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
4008 lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
1bd0e2d1 4009 lazy_refcounts);
06d05fa7 4010 } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
61ce55fc
HR
4011 refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
4012 refcount_bits);
4013
4014 if (refcount_bits <= 0 || refcount_bits > 64 ||
4015 !is_power_of_2(refcount_bits))
4016 {
4017 error_report("Refcount width must be a power of two and may "
4018 "not exceed 64 bits");
4019 return -EINVAL;
4020 }
9296b3ed 4021 } else {
164e0f89 4022 /* if this point is reached, this probably means a new option was
9296b3ed 4023 * added without having it covered here */
164e0f89 4024 abort();
9296b3ed 4025 }
1bd0e2d1
CL
4026
4027 desc++;
9296b3ed
HR
4028 }
4029
c293a809
HR
4030 helper_cb_info = (Qcow2AmendHelperCBInfo){
4031 .original_status_cb = status_cb,
4032 .original_cb_opaque = cb_opaque,
4033 .total_operations = (new_version < old_version)
61ce55fc 4034 + (s->refcount_bits != refcount_bits)
c293a809
HR
4035 };
4036
1038bbb8
HR
4037 /* Upgrade first (some features may require compat=1.1) */
4038 if (new_version > old_version) {
4039 s->qcow_version = new_version;
4040 ret = qcow2_update_header(bs);
4041 if (ret < 0) {
4042 s->qcow_version = old_version;
4043 return ret;
9296b3ed
HR
4044 }
4045 }
4046
61ce55fc
HR
4047 if (s->refcount_bits != refcount_bits) {
4048 int refcount_order = ctz32(refcount_bits);
61ce55fc
HR
4049
4050 if (new_version < 3 && refcount_bits != 16) {
4051 error_report("Different refcount widths than 16 bits require "
4052 "compatibility level 1.1 or above (use compat=1.1 or "
4053 "greater)");
4054 return -EINVAL;
4055 }
4056
4057 helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
4058 ret = qcow2_change_refcount_order(bs, refcount_order,
4059 &qcow2_amend_helper_cb,
a7a6a2bf 4060 &helper_cb_info, &local_err);
61ce55fc 4061 if (ret < 0) {
a7a6a2bf 4062 error_report_err(local_err);
61ce55fc
HR
4063 return ret;
4064 }
4065 }
4066
9296b3ed 4067 if (backing_file || backing_format) {
e4603fe1
KW
4068 ret = qcow2_change_backing_file(bs,
4069 backing_file ?: s->image_backing_file,
4070 backing_format ?: s->image_backing_format);
9296b3ed
HR
4071 if (ret < 0) {
4072 return ret;
4073 }
4074 }
4075
4076 if (s->use_lazy_refcounts != lazy_refcounts) {
4077 if (lazy_refcounts) {
1038bbb8 4078 if (new_version < 3) {
29d72431
HR
4079 error_report("Lazy refcounts only supported with compatibility "
4080 "level 1.1 and above (use compat=1.1 or greater)");
9296b3ed
HR
4081 return -EINVAL;
4082 }
4083 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4084 ret = qcow2_update_header(bs);
4085 if (ret < 0) {
4086 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4087 return ret;
4088 }
4089 s->use_lazy_refcounts = true;
4090 } else {
4091 /* make image clean first */
4092 ret = qcow2_mark_clean(bs);
4093 if (ret < 0) {
4094 return ret;
4095 }
4096 /* now disallow lazy refcounts */
4097 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
4098 ret = qcow2_update_header(bs);
4099 if (ret < 0) {
4100 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
4101 return ret;
4102 }
4103 s->use_lazy_refcounts = false;
4104 }
4105 }
4106
4107 if (new_size) {
6d0eb64d 4108 BlockBackend *blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL);
d7086422
KW
4109 ret = blk_insert_bs(blk, bs, &local_err);
4110 if (ret < 0) {
4111 error_report_err(local_err);
4112 blk_unref(blk);
4113 return ret;
4114 }
4115
3a691c50 4116 ret = blk_truncate(blk, new_size, PREALLOC_MODE_OFF, &local_err);
70b27f36 4117 blk_unref(blk);
9296b3ed 4118 if (ret < 0) {
ed3d2ec9 4119 error_report_err(local_err);
9296b3ed
HR
4120 return ret;
4121 }
4122 }
4123
1038bbb8
HR
4124 /* Downgrade last (so unsupported features can be removed before) */
4125 if (new_version < old_version) {
c293a809
HR
4126 helper_cb_info.current_operation = QCOW2_DOWNGRADING;
4127 ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
4128 &helper_cb_info);
1038bbb8
HR
4129 if (ret < 0) {
4130 return ret;
4131 }
4132 }
4133
9296b3ed
HR
4134 return 0;
4135}
4136
85186ebd
HR
4137/*
4138 * If offset or size are negative, respectively, they will not be included in
4139 * the BLOCK_IMAGE_CORRUPTED event emitted.
4140 * fatal will be ignored for read-only BDS; corruptions found there will always
4141 * be considered non-fatal.
4142 */
4143void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
4144 int64_t size, const char *message_format, ...)
4145{
ff99129a 4146 BDRVQcow2State *s = bs->opaque;
dc881b44 4147 const char *node_name;
85186ebd
HR
4148 char *message;
4149 va_list ap;
4150
4151 fatal = fatal && !bs->read_only;
4152
4153 if (s->signaled_corruption &&
4154 (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
4155 {
4156 return;
4157 }
4158
4159 va_start(ap, message_format);
4160 message = g_strdup_vprintf(message_format, ap);
4161 va_end(ap);
4162
4163 if (fatal) {
4164 fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
4165 "corruption events will be suppressed\n", message);
4166 } else {
4167 fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
4168 "corruption events will be suppressed\n", message);
4169 }
4170
dc881b44
AG
4171 node_name = bdrv_get_node_name(bs);
4172 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
4173 *node_name != '\0', node_name,
4174 message, offset >= 0, offset,
4175 size >= 0, size,
85186ebd
HR
4176 fatal, &error_abort);
4177 g_free(message);
4178
4179 if (fatal) {
4180 qcow2_mark_corrupt(bs);
4181 bs->drv = NULL; /* make BDS unusable */
4182 }
4183
4184 s->signaled_corruption = true;
4185}
4186
1bd0e2d1
CL
4187static QemuOptsList qcow2_create_opts = {
4188 .name = "qcow2-create-opts",
4189 .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
4190 .desc = {
4191 {
4192 .name = BLOCK_OPT_SIZE,
4193 .type = QEMU_OPT_SIZE,
4194 .help = "Virtual disk size"
4195 },
4196 {
4197 .name = BLOCK_OPT_COMPAT_LEVEL,
4198 .type = QEMU_OPT_STRING,
4199 .help = "Compatibility level (0.10 or 1.1)"
4200 },
4201 {
4202 .name = BLOCK_OPT_BACKING_FILE,
4203 .type = QEMU_OPT_STRING,
4204 .help = "File name of a base image"
4205 },
4206 {
4207 .name = BLOCK_OPT_BACKING_FMT,
4208 .type = QEMU_OPT_STRING,
4209 .help = "Image format of the base image"
4210 },
4211 {
4212 .name = BLOCK_OPT_ENCRYPT,
4213 .type = QEMU_OPT_BOOL,
0cb8d47b
DB
4214 .help = "Encrypt the image with format 'aes'. (Deprecated "
4215 "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
4216 },
4217 {
4218 .name = BLOCK_OPT_ENCRYPT_FORMAT,
4219 .type = QEMU_OPT_STRING,
4652b8f3 4220 .help = "Encrypt the image, format choices: 'aes', 'luks'",
1bd0e2d1 4221 },
4652b8f3
DB
4222 BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
4223 "ID of secret providing qcow AES key or LUKS passphrase"),
4224 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),
4225 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),
4226 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),
4227 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),
4228 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),
4229 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
1bd0e2d1
CL
4230 {
4231 .name = BLOCK_OPT_CLUSTER_SIZE,
4232 .type = QEMU_OPT_SIZE,
4233 .help = "qcow2 cluster size",
4234 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
4235 },
4236 {
4237 .name = BLOCK_OPT_PREALLOC,
4238 .type = QEMU_OPT_STRING,
0e4271b7
HT
4239 .help = "Preallocation mode (allowed values: off, metadata, "
4240 "falloc, full)"
1bd0e2d1
CL
4241 },
4242 {
4243 .name = BLOCK_OPT_LAZY_REFCOUNTS,
4244 .type = QEMU_OPT_BOOL,
4245 .help = "Postpone refcount updates",
4246 .def_value_str = "off"
4247 },
06d05fa7
HR
4248 {
4249 .name = BLOCK_OPT_REFCOUNT_BITS,
4250 .type = QEMU_OPT_NUMBER,
4251 .help = "Width of a reference count entry in bits",
4252 .def_value_str = "16"
4253 },
1bd0e2d1
CL
4254 { /* end of list */ }
4255 }
20d97356
BS
4256};
4257
5f535a94 4258BlockDriver bdrv_qcow2 = {
7c80ab3f 4259 .format_name = "qcow2",
ff99129a 4260 .instance_size = sizeof(BDRVQcow2State),
7c80ab3f
JS
4261 .bdrv_probe = qcow2_probe,
4262 .bdrv_open = qcow2_open,
4263 .bdrv_close = qcow2_close,
21d82ac9 4264 .bdrv_reopen_prepare = qcow2_reopen_prepare,
5b0959a7
KW
4265 .bdrv_reopen_commit = qcow2_reopen_commit,
4266 .bdrv_reopen_abort = qcow2_reopen_abort,
5365f44d 4267 .bdrv_join_options = qcow2_join_options,
862f215f 4268 .bdrv_child_perm = bdrv_format_default_perms,
c282e1fd 4269 .bdrv_create = qcow2_create,
3ac21627 4270 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 4271 .bdrv_co_get_block_status = qcow2_co_get_block_status,
7c80ab3f 4272
ecfe1863 4273 .bdrv_co_preadv = qcow2_co_preadv,
d46a0bb2 4274 .bdrv_co_pwritev = qcow2_co_pwritev,
eb489bb1 4275 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
419b19d9 4276
5544b59f 4277 .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes,
82e8a788 4278 .bdrv_co_pdiscard = qcow2_co_pdiscard,
419b19d9 4279 .bdrv_truncate = qcow2_truncate,
fcccefc5 4280 .bdrv_co_pwritev_compressed = qcow2_co_pwritev_compressed,
491d27e2 4281 .bdrv_make_empty = qcow2_make_empty,
20d97356
BS
4282
4283 .bdrv_snapshot_create = qcow2_snapshot_create,
4284 .bdrv_snapshot_goto = qcow2_snapshot_goto,
4285 .bdrv_snapshot_delete = qcow2_snapshot_delete,
4286 .bdrv_snapshot_list = qcow2_snapshot_list,
1bd0e2d1 4287 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
c501c352 4288 .bdrv_measure = qcow2_measure,
1bd0e2d1 4289 .bdrv_get_info = qcow2_get_info,
37764dfb 4290 .bdrv_get_specific_info = qcow2_get_specific_info,
20d97356 4291
7c80ab3f
JS
4292 .bdrv_save_vmstate = qcow2_save_vmstate,
4293 .bdrv_load_vmstate = qcow2_load_vmstate,
20d97356 4294
8ee79e70 4295 .supports_backing = true,
20d97356
BS
4296 .bdrv_change_backing_file = qcow2_change_backing_file,
4297
d34682cd 4298 .bdrv_refresh_limits = qcow2_refresh_limits,
06d9260f 4299 .bdrv_invalidate_cache = qcow2_invalidate_cache,
ec6d8912 4300 .bdrv_inactivate = qcow2_inactivate,
06d9260f 4301
1bd0e2d1
CL
4302 .create_opts = &qcow2_create_opts,
4303 .bdrv_check = qcow2_check,
c282e1fd 4304 .bdrv_amend_options = qcow2_amend_options,
279621c0
AG
4305
4306 .bdrv_detach_aio_context = qcow2_detach_aio_context,
4307 .bdrv_attach_aio_context = qcow2_attach_aio_context,
1b6b0562
VSO
4308
4309 .bdrv_reopen_bitmaps_rw = qcow2_reopen_bitmaps_rw,
da0eb242 4310 .bdrv_can_store_new_dirty_bitmap = qcow2_can_store_new_dirty_bitmap,
469c71ed 4311 .bdrv_remove_persistent_dirty_bitmap = qcow2_remove_persistent_dirty_bitmap,
20d97356
BS
4312};
4313
5efa9d5a
AL
4314static void bdrv_qcow2_init(void)
4315{
4316 bdrv_register(&bdrv_qcow2);
4317}
4318
4319block_init(bdrv_qcow2_init);