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