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