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