]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.c
QemuOpts: export qemu_opt_find
[mirror_qemu.git] / block / qcow2.c
CommitLineData
585f8587
FB
1/*
2 * Block driver for the QCOW version 2 format
5fafdf24 3 *
585f8587 4 * Copyright (c) 2004-2006 Fabrice Bellard
5fafdf24 5 *
585f8587
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
faf07963 24#include "qemu-common.h"
737e150e 25#include "block/block_int.h"
1de7afc9 26#include "qemu/module.h"
585f8587 27#include <zlib.h>
753d9b82 28#include "qemu/aes.h"
f7d0fe02 29#include "block/qcow2.h"
1de7afc9 30#include "qemu/error-report.h"
7b1b5d19 31#include "qapi/qmp/qerror.h"
acdfb480 32#include "qapi/qmp/qbool.h"
3cce16f4 33#include "trace.h"
585f8587
FB
34
35/*
36 Differences with QCOW:
37
38 - Support for multiple incremental snapshots.
39 - Memory management by reference counts.
40 - Clusters which have a reference count of one have the bit
41 QCOW_OFLAG_COPIED to optimize write performance.
5fafdf24 42 - Size of compressed clusters is stored in sectors to reduce bit usage
585f8587
FB
43 in the cluster offsets.
44 - Support for storing additional data (such as the VM state) in the
3b46e624 45 snapshots.
585f8587
FB
46 - If a backing store is used, the cluster size is not constrained
47 (could be backported to QCOW).
48 - L2 tables have always a size of one cluster.
49*/
50
9b80ddf3
AL
51
52typedef struct {
53 uint32_t magic;
54 uint32_t len;
c4217f64 55} QEMU_PACKED QCowExtension;
21d82ac9 56
7c80ab3f
JS
57#define QCOW2_EXT_MAGIC_END 0
58#define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
cfcc4c62 59#define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
9b80ddf3 60
7c80ab3f 61static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
585f8587
FB
62{
63 const QCowHeader *cow_header = (const void *)buf;
3b46e624 64
585f8587
FB
65 if (buf_size >= sizeof(QCowHeader) &&
66 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
6744cbab 67 be32_to_cpu(cow_header->version) >= 2)
585f8587
FB
68 return 100;
69 else
70 return 0;
71}
72
9b80ddf3
AL
73
74/*
75 * read qcow2 extension and fill bs
76 * start reading from start_offset
77 * finish reading upon magic of value 0 or when end_offset reached
78 * unknown magic is skipped (future extension this version knows nothing about)
79 * return 0 upon success, non-0 otherwise
80 */
7c80ab3f 81static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
3ef6c40a
HR
82 uint64_t end_offset, void **p_feature_table,
83 Error **errp)
9b80ddf3 84{
75bab85c 85 BDRVQcowState *s = bs->opaque;
9b80ddf3
AL
86 QCowExtension ext;
87 uint64_t offset;
75bab85c 88 int ret;
9b80ddf3
AL
89
90#ifdef DEBUG_EXT
7c80ab3f 91 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
9b80ddf3
AL
92#endif
93 offset = start_offset;
94 while (offset < end_offset) {
95
96#ifdef DEBUG_EXT
97 /* Sanity check */
98 if (offset > s->cluster_size)
7c80ab3f 99 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
9b80ddf3 100
9b2260cb 101 printf("attempting to read extended header in offset %lu\n", offset);
9b80ddf3
AL
102#endif
103
3ef6c40a
HR
104 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
105 if (ret < 0) {
106 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
107 "pread fail from offset %" PRIu64, offset);
9b80ddf3
AL
108 return 1;
109 }
110 be32_to_cpus(&ext.magic);
111 be32_to_cpus(&ext.len);
112 offset += sizeof(ext);
113#ifdef DEBUG_EXT
114 printf("ext.magic = 0x%x\n", ext.magic);
115#endif
64ca6aee 116 if (ext.len > end_offset - offset) {
3ef6c40a 117 error_setg(errp, "Header extension too large");
64ca6aee
KW
118 return -EINVAL;
119 }
120
9b80ddf3 121 switch (ext.magic) {
7c80ab3f 122 case QCOW2_EXT_MAGIC_END:
9b80ddf3 123 return 0;
f965509c 124
7c80ab3f 125 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
f965509c 126 if (ext.len >= sizeof(bs->backing_format)) {
521b2b5d
HR
127 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
128 " too large (>=%zu)", ext.len,
129 sizeof(bs->backing_format));
f965509c
AL
130 return 2;
131 }
3ef6c40a
HR
132 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
133 if (ret < 0) {
134 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
135 "Could not read format name");
f965509c 136 return 3;
3ef6c40a 137 }
f965509c
AL
138 bs->backing_format[ext.len] = '\0';
139#ifdef DEBUG_EXT
140 printf("Qcow2: Got format extension %s\n", bs->backing_format);
141#endif
f965509c
AL
142 break;
143
cfcc4c62
KW
144 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
145 if (p_feature_table != NULL) {
146 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
147 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
148 if (ret < 0) {
3ef6c40a
HR
149 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
150 "Could not read table");
cfcc4c62
KW
151 return ret;
152 }
153
154 *p_feature_table = feature_table;
155 }
156 break;
157
9b80ddf3 158 default:
75bab85c
KW
159 /* unknown magic - save it in case we need to rewrite the header */
160 {
161 Qcow2UnknownHeaderExtension *uext;
162
163 uext = g_malloc0(sizeof(*uext) + ext.len);
164 uext->magic = ext.magic;
165 uext->len = ext.len;
166 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
167
168 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
169 if (ret < 0) {
3ef6c40a
HR
170 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
171 "Could not read data");
75bab85c
KW
172 return ret;
173 }
75bab85c 174 }
9b80ddf3
AL
175 break;
176 }
fd29b4bb
KW
177
178 offset += ((ext.len + 7) & ~7);
9b80ddf3
AL
179 }
180
181 return 0;
182}
183
75bab85c
KW
184static void cleanup_unknown_header_ext(BlockDriverState *bs)
185{
186 BDRVQcowState *s = bs->opaque;
187 Qcow2UnknownHeaderExtension *uext, *next;
188
189 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
190 QLIST_REMOVE(uext, next);
191 g_free(uext);
192 }
193}
9b80ddf3 194
3ef6c40a
HR
195static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
196 Error **errp, const char *fmt, ...)
6744cbab
KW
197{
198 char msg[64];
199 va_list ap;
200
201 va_start(ap, fmt);
202 vsnprintf(msg, sizeof(msg), fmt, ap);
203 va_end(ap);
204
3ef6c40a
HR
205 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, bs->device_name, "qcow2",
206 msg);
6744cbab
KW
207}
208
cfcc4c62 209static void report_unsupported_feature(BlockDriverState *bs,
3ef6c40a 210 Error **errp, Qcow2Feature *table, uint64_t mask)
cfcc4c62
KW
211{
212 while (table && table->name[0] != '\0') {
213 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
214 if (mask & (1 << table->bit)) {
3ef6c40a 215 report_unsupported(bs, errp, "%.46s", table->name);
cfcc4c62
KW
216 mask &= ~(1 << table->bit);
217 }
218 }
219 table++;
220 }
221
222 if (mask) {
3ef6c40a
HR
223 report_unsupported(bs, errp, "Unknown incompatible feature: %" PRIx64,
224 mask);
cfcc4c62
KW
225 }
226}
227
bfe8043e
SH
228/*
229 * Sets the dirty bit and flushes afterwards if necessary.
230 *
231 * The incompatible_features bit is only set if the image file header was
232 * updated successfully. Therefore it is not required to check the return
233 * value of this function.
234 */
280d3735 235int qcow2_mark_dirty(BlockDriverState *bs)
bfe8043e
SH
236{
237 BDRVQcowState *s = bs->opaque;
238 uint64_t val;
239 int ret;
240
241 assert(s->qcow_version >= 3);
242
243 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
244 return 0; /* already dirty */
245 }
246
247 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
248 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
249 &val, sizeof(val));
250 if (ret < 0) {
251 return ret;
252 }
253 ret = bdrv_flush(bs->file);
254 if (ret < 0) {
255 return ret;
256 }
257
258 /* Only treat image as dirty if the header was updated successfully */
259 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
260 return 0;
261}
262
c61d0004
SH
263/*
264 * Clears the dirty bit and flushes before if necessary. Only call this
265 * function when there are no pending requests, it does not guard against
266 * concurrent requests dirtying the image.
267 */
268static int qcow2_mark_clean(BlockDriverState *bs)
269{
270 BDRVQcowState *s = bs->opaque;
271
272 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
4c2e5f8f
KW
273 int ret;
274
275 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
276
277 ret = bdrv_flush(bs);
c61d0004
SH
278 if (ret < 0) {
279 return ret;
280 }
281
c61d0004
SH
282 return qcow2_update_header(bs);
283 }
284 return 0;
285}
286
69c98726
HR
287/*
288 * Marks the image as corrupt.
289 */
290int qcow2_mark_corrupt(BlockDriverState *bs)
291{
292 BDRVQcowState *s = bs->opaque;
293
294 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
295 return qcow2_update_header(bs);
296}
297
298/*
299 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
300 * before if necessary.
301 */
302int qcow2_mark_consistent(BlockDriverState *bs)
303{
304 BDRVQcowState *s = bs->opaque;
305
306 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
307 int ret = bdrv_flush(bs);
308 if (ret < 0) {
309 return ret;
310 }
311
312 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
313 return qcow2_update_header(bs);
314 }
315 return 0;
316}
317
acbe5982
SH
318static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
319 BdrvCheckMode fix)
320{
321 int ret = qcow2_check_refcounts(bs, result, fix);
322 if (ret < 0) {
323 return ret;
324 }
325
326 if (fix && result->check_errors == 0 && result->corruptions == 0) {
24530f3e
HR
327 ret = qcow2_mark_clean(bs);
328 if (ret < 0) {
329 return ret;
330 }
331 return qcow2_mark_consistent(bs);
acbe5982
SH
332 }
333 return ret;
334}
335
8c7de283
KW
336static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
337 uint64_t entries, size_t entry_len)
338{
339 BDRVQcowState *s = bs->opaque;
340 uint64_t size;
341
342 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
343 * because values will be passed to qemu functions taking int64_t. */
344 if (entries > INT64_MAX / entry_len) {
345 return -EINVAL;
346 }
347
348 size = entries * entry_len;
349
350 if (INT64_MAX - size < offset) {
351 return -EINVAL;
352 }
353
354 /* Tables must be cluster aligned */
355 if (offset & (s->cluster_size - 1)) {
356 return -EINVAL;
357 }
358
359 return 0;
360}
361
74c4510a
KW
362static QemuOptsList qcow2_runtime_opts = {
363 .name = "qcow2",
364 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
365 .desc = {
366 {
64aa99d3 367 .name = QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
368 .type = QEMU_OPT_BOOL,
369 .help = "Postpone refcount updates",
370 },
67af674e
KW
371 {
372 .name = QCOW2_OPT_DISCARD_REQUEST,
373 .type = QEMU_OPT_BOOL,
374 .help = "Pass guest discard requests to the layer below",
375 },
376 {
377 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
378 .type = QEMU_OPT_BOOL,
379 .help = "Generate discard requests when snapshot related space "
380 "is freed",
381 },
382 {
383 .name = QCOW2_OPT_DISCARD_OTHER,
384 .type = QEMU_OPT_BOOL,
385 .help = "Generate discard requests when other clusters are freed",
386 },
05de7e86
HR
387 {
388 .name = QCOW2_OPT_OVERLAP,
389 .type = QEMU_OPT_STRING,
390 .help = "Selects which overlap checks to perform from a range of "
391 "templates (none, constant, cached, all)",
392 },
393 {
394 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
395 .type = QEMU_OPT_BOOL,
396 .help = "Check for unintended writes into the main qcow2 header",
397 },
398 {
399 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
400 .type = QEMU_OPT_BOOL,
401 .help = "Check for unintended writes into the active L1 table",
402 },
403 {
404 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
405 .type = QEMU_OPT_BOOL,
406 .help = "Check for unintended writes into an active L2 table",
407 },
408 {
409 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
410 .type = QEMU_OPT_BOOL,
411 .help = "Check for unintended writes into the refcount table",
412 },
413 {
414 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
415 .type = QEMU_OPT_BOOL,
416 .help = "Check for unintended writes into a refcount block",
417 },
418 {
419 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
420 .type = QEMU_OPT_BOOL,
421 .help = "Check for unintended writes into the snapshot table",
422 },
423 {
424 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
425 .type = QEMU_OPT_BOOL,
426 .help = "Check for unintended writes into an inactive L1 table",
427 },
428 {
429 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
430 .type = QEMU_OPT_BOOL,
431 .help = "Check for unintended writes into an inactive L2 table",
432 },
74c4510a
KW
433 { /* end of list */ }
434 },
435};
436
4092e99d
HR
437static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
438 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
439 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
440 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
441 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
442 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
443 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
444 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
445 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
446};
447
015a1036
HR
448static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
449 Error **errp)
585f8587
FB
450{
451 BDRVQcowState *s = bs->opaque;
6d33e8e7
KW
452 unsigned int len, i;
453 int ret = 0;
585f8587 454 QCowHeader header;
74c4510a
KW
455 QemuOpts *opts;
456 Error *local_err = NULL;
9b80ddf3 457 uint64_t ext_end;
2cf7cfa1 458 uint64_t l1_vm_state_index;
1fa5cc83
HR
459 const char *opt_overlap_check;
460 int overlap_check_template = 0;
585f8587 461
6d85a57e
JS
462 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
463 if (ret < 0) {
3ef6c40a 464 error_setg_errno(errp, -ret, "Could not read qcow2 header");
585f8587 465 goto fail;
6d85a57e 466 }
585f8587
FB
467 be32_to_cpus(&header.magic);
468 be32_to_cpus(&header.version);
469 be64_to_cpus(&header.backing_file_offset);
470 be32_to_cpus(&header.backing_file_size);
471 be64_to_cpus(&header.size);
472 be32_to_cpus(&header.cluster_bits);
473 be32_to_cpus(&header.crypt_method);
474 be64_to_cpus(&header.l1_table_offset);
475 be32_to_cpus(&header.l1_size);
476 be64_to_cpus(&header.refcount_table_offset);
477 be32_to_cpus(&header.refcount_table_clusters);
478 be64_to_cpus(&header.snapshots_offset);
479 be32_to_cpus(&header.nb_snapshots);
3b46e624 480
e8cdcec1 481 if (header.magic != QCOW_MAGIC) {
3ef6c40a 482 error_setg(errp, "Image is not in qcow2 format");
76abe407 483 ret = -EINVAL;
585f8587 484 goto fail;
6d85a57e 485 }
6744cbab 486 if (header.version < 2 || header.version > 3) {
521b2b5d 487 report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version);
6744cbab
KW
488 ret = -ENOTSUP;
489 goto fail;
490 }
491
492 s->qcow_version = header.version;
493
24342f2c
KW
494 /* Initialise cluster size */
495 if (header.cluster_bits < MIN_CLUSTER_BITS ||
496 header.cluster_bits > MAX_CLUSTER_BITS) {
521b2b5d
HR
497 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
498 header.cluster_bits);
24342f2c
KW
499 ret = -EINVAL;
500 goto fail;
501 }
502
503 s->cluster_bits = header.cluster_bits;
504 s->cluster_size = 1 << s->cluster_bits;
505 s->cluster_sectors = 1 << (s->cluster_bits - 9);
506
6744cbab
KW
507 /* Initialise version 3 header fields */
508 if (header.version == 2) {
509 header.incompatible_features = 0;
510 header.compatible_features = 0;
511 header.autoclear_features = 0;
512 header.refcount_order = 4;
513 header.header_length = 72;
514 } else {
515 be64_to_cpus(&header.incompatible_features);
516 be64_to_cpus(&header.compatible_features);
517 be64_to_cpus(&header.autoclear_features);
518 be32_to_cpus(&header.refcount_order);
519 be32_to_cpus(&header.header_length);
24342f2c
KW
520
521 if (header.header_length < 104) {
522 error_setg(errp, "qcow2 header too short");
523 ret = -EINVAL;
524 goto fail;
525 }
526 }
527
528 if (header.header_length > s->cluster_size) {
529 error_setg(errp, "qcow2 header exceeds cluster size");
530 ret = -EINVAL;
531 goto fail;
6744cbab
KW
532 }
533
534 if (header.header_length > sizeof(header)) {
535 s->unknown_header_fields_size = header.header_length - sizeof(header);
536 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
537 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
538 s->unknown_header_fields_size);
539 if (ret < 0) {
3ef6c40a
HR
540 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
541 "fields");
6744cbab
KW
542 goto fail;
543 }
544 }
545
a1b3955c
KW
546 if (header.backing_file_offset > s->cluster_size) {
547 error_setg(errp, "Invalid backing file offset");
548 ret = -EINVAL;
549 goto fail;
550 }
551
cfcc4c62
KW
552 if (header.backing_file_offset) {
553 ext_end = header.backing_file_offset;
554 } else {
555 ext_end = 1 << header.cluster_bits;
556 }
557
6744cbab
KW
558 /* Handle feature bits */
559 s->incompatible_features = header.incompatible_features;
560 s->compatible_features = header.compatible_features;
561 s->autoclear_features = header.autoclear_features;
562
c61d0004 563 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
cfcc4c62
KW
564 void *feature_table = NULL;
565 qcow2_read_extensions(bs, header.header_length, ext_end,
3ef6c40a
HR
566 &feature_table, NULL);
567 report_unsupported_feature(bs, errp, feature_table,
c61d0004
SH
568 s->incompatible_features &
569 ~QCOW2_INCOMPAT_MASK);
6744cbab 570 ret = -ENOTSUP;
c5a33ee9 571 g_free(feature_table);
6744cbab
KW
572 goto fail;
573 }
574
69c98726
HR
575 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
576 /* Corrupt images may not be written to unless they are being repaired
577 */
578 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
3ef6c40a
HR
579 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
580 "read/write");
69c98726
HR
581 ret = -EACCES;
582 goto fail;
583 }
584 }
585
6744cbab
KW
586 /* Check support for various header values */
587 if (header.refcount_order != 4) {
3ef6c40a 588 report_unsupported(bs, errp, "%d bit reference counts",
6744cbab 589 1 << header.refcount_order);
e8cdcec1
KW
590 ret = -ENOTSUP;
591 goto fail;
592 }
b6481f37 593 s->refcount_order = header.refcount_order;
6744cbab 594
6d85a57e 595 if (header.crypt_method > QCOW_CRYPT_AES) {
521b2b5d 596 error_setg(errp, "Unsupported encryption method: %" PRIu32,
3ef6c40a 597 header.crypt_method);
6d85a57e 598 ret = -EINVAL;
585f8587 599 goto fail;
6d85a57e 600 }
585f8587 601 s->crypt_method_header = header.crypt_method;
6d85a57e 602 if (s->crypt_method_header) {
585f8587 603 bs->encrypted = 1;
6d85a57e 604 }
24342f2c 605
585f8587
FB
606 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
607 s->l2_size = 1 << s->l2_bits;
608 bs->total_sectors = header.size / 512;
609 s->csize_shift = (62 - (s->cluster_bits - 8));
610 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
611 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
5dab2fad 612
585f8587 613 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 614 s->refcount_table_size =
585f8587
FB
615 header.refcount_table_clusters << (s->cluster_bits - 3);
616
2b5d5953 617 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
5dab2fad
KW
618 error_setg(errp, "Reference count table too large");
619 ret = -EINVAL;
620 goto fail;
621 }
622
8c7de283
KW
623 ret = validate_table_offset(bs, s->refcount_table_offset,
624 s->refcount_table_size, sizeof(uint64_t));
625 if (ret < 0) {
626 error_setg(errp, "Invalid reference count table offset");
627 goto fail;
628 }
629
ce48f2f4
KW
630 /* Snapshot table offset/length */
631 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
632 error_setg(errp, "Too many snapshots");
633 ret = -EINVAL;
634 goto fail;
635 }
636
637 ret = validate_table_offset(bs, header.snapshots_offset,
638 header.nb_snapshots,
639 sizeof(QCowSnapshotHeader));
640 if (ret < 0) {
641 error_setg(errp, "Invalid snapshot table offset");
642 goto fail;
643 }
644
585f8587 645 /* read the level 1 table */
6a83f8b5 646 if (header.l1_size > QCOW_MAX_L1_SIZE) {
2d51c32c
KW
647 error_setg(errp, "Active L1 table too large");
648 ret = -EFBIG;
649 goto fail;
650 }
585f8587 651 s->l1_size = header.l1_size;
2cf7cfa1
KW
652
653 l1_vm_state_index = size_to_l1(s, header.size);
654 if (l1_vm_state_index > INT_MAX) {
3ef6c40a 655 error_setg(errp, "Image is too big");
2cf7cfa1
KW
656 ret = -EFBIG;
657 goto fail;
658 }
659 s->l1_vm_state_index = l1_vm_state_index;
660
585f8587
FB
661 /* the L1 table must contain at least enough entries to put
662 header.size bytes */
6d85a57e 663 if (s->l1_size < s->l1_vm_state_index) {
3ef6c40a 664 error_setg(errp, "L1 table is too small");
6d85a57e 665 ret = -EINVAL;
585f8587 666 goto fail;
6d85a57e 667 }
2d51c32c
KW
668
669 ret = validate_table_offset(bs, header.l1_table_offset,
670 header.l1_size, sizeof(uint64_t));
671 if (ret < 0) {
672 error_setg(errp, "Invalid L1 table offset");
673 goto fail;
674 }
585f8587 675 s->l1_table_offset = header.l1_table_offset;
2d51c32c
KW
676
677
d191d12d 678 if (s->l1_size > 0) {
7267c094 679 s->l1_table = g_malloc0(
d191d12d 680 align_offset(s->l1_size * sizeof(uint64_t), 512));
6d85a57e
JS
681 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
682 s->l1_size * sizeof(uint64_t));
683 if (ret < 0) {
3ef6c40a 684 error_setg_errno(errp, -ret, "Could not read L1 table");
d191d12d 685 goto fail;
6d85a57e 686 }
d191d12d
SW
687 for(i = 0;i < s->l1_size; i++) {
688 be64_to_cpus(&s->l1_table[i]);
689 }
585f8587 690 }
29c1a730
KW
691
692 /* alloc L2 table/refcount block cache */
6af4e9ea
PB
693 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
694 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
29c1a730 695
7267c094 696 s->cluster_cache = g_malloc(s->cluster_size);
585f8587 697 /* one more sector for decompressed data alignment */
dea43a65 698 s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
095a9c58 699 + 512);
585f8587 700 s->cluster_cache_offset = -1;
06d9260f 701 s->flags = flags;
3b46e624 702
6d85a57e
JS
703 ret = qcow2_refcount_init(bs);
704 if (ret != 0) {
3ef6c40a 705 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
585f8587 706 goto fail;
6d85a57e 707 }
585f8587 708
72cf2d4f 709 QLIST_INIT(&s->cluster_allocs);
0b919fae 710 QTAILQ_INIT(&s->discards);
f214978a 711
9b80ddf3 712 /* read qcow2 extensions */
3ef6c40a
HR
713 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
714 &local_err)) {
715 error_propagate(errp, local_err);
6d85a57e 716 ret = -EINVAL;
9b80ddf3 717 goto fail;
6d85a57e 718 }
9b80ddf3 719
585f8587
FB
720 /* read the backing file name */
721 if (header.backing_file_offset != 0) {
722 len = header.backing_file_size;
6d33e8e7
KW
723 if (len > MIN(1023, s->cluster_size - header.backing_file_offset)) {
724 error_setg(errp, "Backing file name too long");
725 ret = -EINVAL;
726 goto fail;
6d85a57e
JS
727 }
728 ret = bdrv_pread(bs->file, header.backing_file_offset,
729 bs->backing_file, len);
730 if (ret < 0) {
3ef6c40a 731 error_setg_errno(errp, -ret, "Could not read backing file name");
585f8587 732 goto fail;
6d85a57e 733 }
585f8587
FB
734 bs->backing_file[len] = '\0';
735 }
42deb29f 736
11b128f4
KW
737 /* Internal snapshots */
738 s->snapshots_offset = header.snapshots_offset;
739 s->nb_snapshots = header.nb_snapshots;
740
42deb29f
KW
741 ret = qcow2_read_snapshots(bs);
742 if (ret < 0) {
3ef6c40a 743 error_setg_errno(errp, -ret, "Could not read snapshots");
585f8587 744 goto fail;
6d85a57e 745 }
585f8587 746
af7b708d 747 /* Clear unknown autoclear feature bits */
27eb6c09 748 if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
af7b708d
SH
749 s->autoclear_features = 0;
750 ret = qcow2_update_header(bs);
751 if (ret < 0) {
3ef6c40a 752 error_setg_errno(errp, -ret, "Could not update qcow2 header");
af7b708d
SH
753 goto fail;
754 }
755 }
756
68d100e9
KW
757 /* Initialise locks */
758 qemu_co_mutex_init(&s->lock);
759
c61d0004 760 /* Repair image if dirty */
27eb6c09 761 if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only &&
058f8f16 762 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
c61d0004
SH
763 BdrvCheckResult result = {0};
764
acbe5982 765 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS);
c61d0004 766 if (ret < 0) {
3ef6c40a 767 error_setg_errno(errp, -ret, "Could not repair dirty image");
c61d0004
SH
768 goto fail;
769 }
770 }
771
74c4510a 772 /* Enable lazy_refcounts according to image and command line options */
87ea75d5 773 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
74c4510a 774 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 775 if (local_err) {
3ef6c40a 776 error_propagate(errp, local_err);
74c4510a
KW
777 ret = -EINVAL;
778 goto fail;
779 }
780
acdfb480 781 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
782 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
783
67af674e
KW
784 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
785 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
786 s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
787 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
788 flags & BDRV_O_UNMAP);
789 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
790 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
791 s->discard_passthrough[QCOW2_DISCARD_OTHER] =
792 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
793
1fa5cc83
HR
794 opt_overlap_check = qemu_opt_get(opts, "overlap-check") ?: "cached";
795 if (!strcmp(opt_overlap_check, "none")) {
796 overlap_check_template = 0;
797 } else if (!strcmp(opt_overlap_check, "constant")) {
798 overlap_check_template = QCOW2_OL_CONSTANT;
799 } else if (!strcmp(opt_overlap_check, "cached")) {
800 overlap_check_template = QCOW2_OL_CACHED;
801 } else if (!strcmp(opt_overlap_check, "all")) {
802 overlap_check_template = QCOW2_OL_ALL;
803 } else {
804 error_setg(errp, "Unsupported value '%s' for qcow2 option "
805 "'overlap-check'. Allowed are either of the following: "
806 "none, constant, cached, all", opt_overlap_check);
807 qemu_opts_del(opts);
808 ret = -EINVAL;
809 goto fail;
810 }
811
812 s->overlap_check = 0;
813 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
814 /* overlap-check defines a template bitmask, but every flag may be
815 * overwritten through the associated boolean option */
816 s->overlap_check |=
817 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
818 overlap_check_template & (1 << i)) << i;
819 }
3e355390 820
74c4510a
KW
821 qemu_opts_del(opts);
822
823 if (s->use_lazy_refcounts && s->qcow_version < 3) {
3ef6c40a
HR
824 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
825 "qemu 1.1 compatibility level");
74c4510a
KW
826 ret = -EINVAL;
827 goto fail;
828 }
829
585f8587 830#ifdef DEBUG_ALLOC
6cbc3031
PH
831 {
832 BdrvCheckResult result = {0};
b35278f7 833 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 834 }
585f8587 835#endif
6d85a57e 836 return ret;
585f8587
FB
837
838 fail:
6744cbab 839 g_free(s->unknown_header_fields);
75bab85c 840 cleanup_unknown_header_ext(bs);
ed6ccf0f
KW
841 qcow2_free_snapshots(bs);
842 qcow2_refcount_close(bs);
7267c094 843 g_free(s->l1_table);
cf93980e
HR
844 /* else pre-write overlap checks in cache_destroy may crash */
845 s->l1_table = NULL;
29c1a730
KW
846 if (s->l2_table_cache) {
847 qcow2_cache_destroy(bs, s->l2_table_cache);
848 }
c5a33ee9
PJ
849 if (s->refcount_block_cache) {
850 qcow2_cache_destroy(bs, s->refcount_block_cache);
851 }
7267c094 852 g_free(s->cluster_cache);
dea43a65 853 qemu_vfree(s->cluster_data);
6d85a57e 854 return ret;
585f8587
FB
855}
856
d34682cd
KW
857static int qcow2_refresh_limits(BlockDriverState *bs)
858{
859 BDRVQcowState *s = bs->opaque;
860
861 bs->bl.write_zeroes_alignment = s->cluster_sectors;
862
863 return 0;
864}
865
7c80ab3f 866static int qcow2_set_key(BlockDriverState *bs, const char *key)
585f8587
FB
867{
868 BDRVQcowState *s = bs->opaque;
869 uint8_t keybuf[16];
870 int len, i;
3b46e624 871
585f8587
FB
872 memset(keybuf, 0, 16);
873 len = strlen(key);
874 if (len > 16)
875 len = 16;
876 /* XXX: we could compress the chars to 7 bits to increase
877 entropy */
878 for(i = 0;i < len;i++) {
879 keybuf[i] = key[i];
880 }
881 s->crypt_method = s->crypt_method_header;
882
883 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
884 return -1;
885 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
886 return -1;
887#if 0
888 /* test */
889 {
890 uint8_t in[16];
891 uint8_t out[16];
892 uint8_t tmp[16];
893 for(i=0;i<16;i++)
894 in[i] = i;
895 AES_encrypt(in, tmp, &s->aes_encrypt_key);
896 AES_decrypt(tmp, out, &s->aes_decrypt_key);
897 for(i = 0; i < 16; i++)
898 printf(" %02x", tmp[i]);
899 printf("\n");
900 for(i = 0; i < 16; i++)
901 printf(" %02x", out[i]);
902 printf("\n");
903 }
904#endif
905 return 0;
906}
907
4c2e5f8f
KW
908/* We have no actual commit/abort logic for qcow2, but we need to write out any
909 * unwritten data if we reopen read-only. */
21d82ac9
JC
910static int qcow2_reopen_prepare(BDRVReopenState *state,
911 BlockReopenQueue *queue, Error **errp)
912{
4c2e5f8f
KW
913 int ret;
914
915 if ((state->flags & BDRV_O_RDWR) == 0) {
916 ret = bdrv_flush(state->bs);
917 if (ret < 0) {
918 return ret;
919 }
920
921 ret = qcow2_mark_clean(state->bs);
922 if (ret < 0) {
923 return ret;
924 }
925 }
926
21d82ac9
JC
927 return 0;
928}
929
b6b8a333 930static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
f8a2e5e3 931 int64_t sector_num, int nb_sectors, int *pnum)
585f8587 932{
f8a2e5e3 933 BDRVQcowState *s = bs->opaque;
585f8587 934 uint64_t cluster_offset;
4bc74be9
PB
935 int index_in_cluster, ret;
936 int64_t status = 0;
585f8587 937
095a9c58 938 *pnum = nb_sectors;
f8a2e5e3 939 qemu_co_mutex_lock(&s->lock);
1c46efaa 940 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
f8a2e5e3 941 qemu_co_mutex_unlock(&s->lock);
1c46efaa 942 if (ret < 0) {
d663640c 943 return ret;
1c46efaa 944 }
095a9c58 945
4bc74be9
PB
946 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
947 !s->crypt_method) {
948 index_in_cluster = sector_num & (s->cluster_sectors - 1);
949 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
950 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
951 }
952 if (ret == QCOW2_CLUSTER_ZERO) {
953 status |= BDRV_BLOCK_ZERO;
954 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
955 status |= BDRV_BLOCK_DATA;
956 }
957 return status;
585f8587
FB
958}
959
a9465922 960/* handle reading after the end of the backing file */
bd28f835
KW
961int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
962 int64_t sector_num, int nb_sectors)
a9465922
FB
963{
964 int n1;
965 if ((sector_num + nb_sectors) <= bs->total_sectors)
966 return nb_sectors;
967 if (sector_num >= bs->total_sectors)
968 n1 = 0;
969 else
970 n1 = bs->total_sectors - sector_num;
bd28f835 971
3d9b4925 972 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
bd28f835 973
a9465922
FB
974 return n1;
975}
976
a968168c 977static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
3fc48d09 978 int remaining_sectors, QEMUIOVector *qiov)
585f8587 979{
585f8587 980 BDRVQcowState *s = bs->opaque;
a9465922 981 int index_in_cluster, n1;
68d100e9 982 int ret;
faf575c1 983 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 984 uint64_t cluster_offset = 0;
3fc48d09
FZ
985 uint64_t bytes_done = 0;
986 QEMUIOVector hd_qiov;
987 uint8_t *cluster_data = NULL;
585f8587 988
3fc48d09
FZ
989 qemu_iovec_init(&hd_qiov, qiov->niov);
990
991 qemu_co_mutex_lock(&s->lock);
992
993 while (remaining_sectors != 0) {
bd28f835 994
5ebaa27e 995 /* prepare next request */
3fc48d09 996 cur_nr_sectors = remaining_sectors;
5ebaa27e
FZ
997 if (s->crypt_method) {
998 cur_nr_sectors = MIN(cur_nr_sectors,
999 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
585f8587 1000 }
5ebaa27e 1001
3fc48d09 1002 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
5ebaa27e 1003 &cur_nr_sectors, &cluster_offset);
8af36488 1004 if (ret < 0) {
3fc48d09 1005 goto fail;
8af36488 1006 }
bd28f835 1007
3fc48d09 1008 index_in_cluster = sector_num & (s->cluster_sectors - 1);
c87c0672 1009
3fc48d09 1010 qemu_iovec_reset(&hd_qiov);
1b093c48 1011 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e
FZ
1012 cur_nr_sectors * 512);
1013
68d000a3
KW
1014 switch (ret) {
1015 case QCOW2_CLUSTER_UNALLOCATED:
5ebaa27e
FZ
1016
1017 if (bs->backing_hd) {
1018 /* read from the base image */
3fc48d09
FZ
1019 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
1020 sector_num, cur_nr_sectors);
5ebaa27e
FZ
1021 if (n1 > 0) {
1022 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1023 qemu_co_mutex_unlock(&s->lock);
3fc48d09
FZ
1024 ret = bdrv_co_readv(bs->backing_hd, sector_num,
1025 n1, &hd_qiov);
5ebaa27e
FZ
1026 qemu_co_mutex_lock(&s->lock);
1027 if (ret < 0) {
3fc48d09 1028 goto fail;
5ebaa27e
FZ
1029 }
1030 }
1031 } else {
1032 /* Note: in this case, no need to wait */
3d9b4925 1033 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
5ebaa27e 1034 }
68d000a3
KW
1035 break;
1036
6377af48 1037 case QCOW2_CLUSTER_ZERO:
3d9b4925 1038 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
6377af48
KW
1039 break;
1040
68d000a3 1041 case QCOW2_CLUSTER_COMPRESSED:
5ebaa27e
FZ
1042 /* add AIO support for compressed blocks ? */
1043 ret = qcow2_decompress_cluster(bs, cluster_offset);
1044 if (ret < 0) {
3fc48d09 1045 goto fail;
bd28f835
KW
1046 }
1047
03396148 1048 qemu_iovec_from_buf(&hd_qiov, 0,
5ebaa27e 1049 s->cluster_cache + index_in_cluster * 512,
faf575c1 1050 512 * cur_nr_sectors);
68d000a3
KW
1051 break;
1052
1053 case QCOW2_CLUSTER_NORMAL:
5ebaa27e 1054 if ((cluster_offset & 511) != 0) {
3fc48d09
FZ
1055 ret = -EIO;
1056 goto fail;
5ebaa27e 1057 }
bd28f835 1058
5ebaa27e
FZ
1059 if (s->crypt_method) {
1060 /*
1061 * For encrypted images, read everything into a temporary
1062 * contiguous buffer on which the AES functions can work.
1063 */
3fc48d09
FZ
1064 if (!cluster_data) {
1065 cluster_data =
dea43a65 1066 qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
5ebaa27e
FZ
1067 }
1068
1069 assert(cur_nr_sectors <=
1070 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
3fc48d09
FZ
1071 qemu_iovec_reset(&hd_qiov);
1072 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
1073 512 * cur_nr_sectors);
1074 }
1075
1076 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1077 qemu_co_mutex_unlock(&s->lock);
1078 ret = bdrv_co_readv(bs->file,
1079 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 1080 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
1081 qemu_co_mutex_lock(&s->lock);
1082 if (ret < 0) {
3fc48d09 1083 goto fail;
5ebaa27e
FZ
1084 }
1085 if (s->crypt_method) {
3fc48d09
FZ
1086 qcow2_encrypt_sectors(s, sector_num, cluster_data,
1087 cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
03396148
MT
1088 qemu_iovec_from_buf(qiov, bytes_done,
1089 cluster_data, 512 * cur_nr_sectors);
5ebaa27e 1090 }
68d000a3
KW
1091 break;
1092
1093 default:
1094 g_assert_not_reached();
1095 ret = -EIO;
1096 goto fail;
faf575c1 1097 }
f141eafe 1098
3fc48d09
FZ
1099 remaining_sectors -= cur_nr_sectors;
1100 sector_num += cur_nr_sectors;
1101 bytes_done += cur_nr_sectors * 512;
5ebaa27e 1102 }
3fc48d09 1103 ret = 0;
faf575c1 1104
3fc48d09 1105fail:
68d100e9 1106 qemu_co_mutex_unlock(&s->lock);
42496d62 1107
3fc48d09 1108 qemu_iovec_destroy(&hd_qiov);
dea43a65 1109 qemu_vfree(cluster_data);
68d100e9
KW
1110
1111 return ret;
585f8587
FB
1112}
1113
a968168c 1114static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
3fc48d09
FZ
1115 int64_t sector_num,
1116 int remaining_sectors,
1117 QEMUIOVector *qiov)
585f8587 1118{
585f8587 1119 BDRVQcowState *s = bs->opaque;
585f8587 1120 int index_in_cluster;
68d100e9 1121 int ret;
faf575c1 1122 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 1123 uint64_t cluster_offset;
3fc48d09
FZ
1124 QEMUIOVector hd_qiov;
1125 uint64_t bytes_done = 0;
1126 uint8_t *cluster_data = NULL;
8d2497c3 1127 QCowL2Meta *l2meta = NULL;
c2271403 1128
3cce16f4
KW
1129 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1130 remaining_sectors);
1131
3fc48d09
FZ
1132 qemu_iovec_init(&hd_qiov, qiov->niov);
1133
1134 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 1135
3fc48d09
FZ
1136 qemu_co_mutex_lock(&s->lock);
1137
1138 while (remaining_sectors != 0) {
1139
f50f88b9 1140 l2meta = NULL;
cf5c1a23 1141
3cce16f4 1142 trace_qcow2_writev_start_part(qemu_coroutine_self());
3fc48d09 1143 index_in_cluster = sector_num & (s->cluster_sectors - 1);
16f0587e 1144 cur_nr_sectors = remaining_sectors;
5ebaa27e 1145 if (s->crypt_method &&
16f0587e
HT
1146 cur_nr_sectors >
1147 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1148 cur_nr_sectors =
1149 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
5ebaa27e 1150 }
095a9c58 1151
3fc48d09 1152 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
16f0587e 1153 &cur_nr_sectors, &cluster_offset, &l2meta);
5ebaa27e 1154 if (ret < 0) {
3fc48d09 1155 goto fail;
5ebaa27e 1156 }
148da7ea 1157
5ebaa27e 1158 assert((cluster_offset & 511) == 0);
148da7ea 1159
3fc48d09 1160 qemu_iovec_reset(&hd_qiov);
1b093c48 1161 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e 1162 cur_nr_sectors * 512);
6f5f060b 1163
5ebaa27e 1164 if (s->crypt_method) {
3fc48d09 1165 if (!cluster_data) {
dea43a65 1166 cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
5ebaa27e
FZ
1167 s->cluster_size);
1168 }
6f5f060b 1169
3fc48d09 1170 assert(hd_qiov.size <=
5ebaa27e 1171 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
d5e6b161 1172 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
6f5f060b 1173
3fc48d09
FZ
1174 qcow2_encrypt_sectors(s, sector_num, cluster_data,
1175 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
6f5f060b 1176
3fc48d09
FZ
1177 qemu_iovec_reset(&hd_qiov);
1178 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
1179 cur_nr_sectors * 512);
1180 }
6f5f060b 1181
231bb267 1182 ret = qcow2_pre_write_overlap_check(bs, 0,
cf93980e
HR
1183 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1184 cur_nr_sectors * BDRV_SECTOR_SIZE);
1185 if (ret < 0) {
1186 goto fail;
1187 }
1188
5ebaa27e 1189 qemu_co_mutex_unlock(&s->lock);
67a7a0eb 1190 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
3cce16f4
KW
1191 trace_qcow2_writev_data(qemu_coroutine_self(),
1192 (cluster_offset >> 9) + index_in_cluster);
5ebaa27e
FZ
1193 ret = bdrv_co_writev(bs->file,
1194 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 1195 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
1196 qemu_co_mutex_lock(&s->lock);
1197 if (ret < 0) {
3fc48d09 1198 goto fail;
5ebaa27e 1199 }
f141eafe 1200
88c6588c
KW
1201 while (l2meta != NULL) {
1202 QCowL2Meta *next;
1203
f50f88b9
KW
1204 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1205 if (ret < 0) {
1206 goto fail;
1207 }
faf575c1 1208
4e95314e
KW
1209 /* Take the request off the list of running requests */
1210 if (l2meta->nb_clusters != 0) {
1211 QLIST_REMOVE(l2meta, next_in_flight);
1212 }
1213
4e95314e 1214 qemu_co_queue_restart_all(&l2meta->dependent_requests);
4e95314e 1215
88c6588c 1216 next = l2meta->next;
f50f88b9 1217 g_free(l2meta);
88c6588c 1218 l2meta = next;
f50f88b9 1219 }
0fa9131a 1220
3fc48d09
FZ
1221 remaining_sectors -= cur_nr_sectors;
1222 sector_num += cur_nr_sectors;
1223 bytes_done += cur_nr_sectors * 512;
3cce16f4 1224 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
5ebaa27e 1225 }
3fc48d09 1226 ret = 0;
faf575c1 1227
3fc48d09 1228fail:
4e95314e
KW
1229 qemu_co_mutex_unlock(&s->lock);
1230
88c6588c
KW
1231 while (l2meta != NULL) {
1232 QCowL2Meta *next;
1233
4e95314e
KW
1234 if (l2meta->nb_clusters != 0) {
1235 QLIST_REMOVE(l2meta, next_in_flight);
1236 }
1237 qemu_co_queue_restart_all(&l2meta->dependent_requests);
88c6588c
KW
1238
1239 next = l2meta->next;
cf5c1a23 1240 g_free(l2meta);
88c6588c 1241 l2meta = next;
cf5c1a23 1242 }
0fa9131a 1243
3fc48d09 1244 qemu_iovec_destroy(&hd_qiov);
dea43a65 1245 qemu_vfree(cluster_data);
3cce16f4 1246 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
42496d62 1247
68d100e9 1248 return ret;
585f8587
FB
1249}
1250
7c80ab3f 1251static void qcow2_close(BlockDriverState *bs)
585f8587
FB
1252{
1253 BDRVQcowState *s = bs->opaque;
7267c094 1254 g_free(s->l1_table);
cf93980e
HR
1255 /* else pre-write overlap checks in cache_destroy may crash */
1256 s->l1_table = NULL;
29c1a730 1257
27eb6c09
KW
1258 if (!(bs->open_flags & BDRV_O_INCOMING)) {
1259 qcow2_cache_flush(bs, s->l2_table_cache);
1260 qcow2_cache_flush(bs, s->refcount_block_cache);
29c1a730 1261
27eb6c09
KW
1262 qcow2_mark_clean(bs);
1263 }
c61d0004 1264
29c1a730
KW
1265 qcow2_cache_destroy(bs, s->l2_table_cache);
1266 qcow2_cache_destroy(bs, s->refcount_block_cache);
1267
6744cbab 1268 g_free(s->unknown_header_fields);
75bab85c 1269 cleanup_unknown_header_ext(bs);
6744cbab 1270
7267c094 1271 g_free(s->cluster_cache);
dea43a65 1272 qemu_vfree(s->cluster_data);
ed6ccf0f 1273 qcow2_refcount_close(bs);
28c1202b 1274 qcow2_free_snapshots(bs);
585f8587
FB
1275}
1276
5a8a30db 1277static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
06d9260f
AL
1278{
1279 BDRVQcowState *s = bs->opaque;
1280 int flags = s->flags;
1281 AES_KEY aes_encrypt_key;
1282 AES_KEY aes_decrypt_key;
1283 uint32_t crypt_method = 0;
acdfb480 1284 QDict *options;
5a8a30db
KW
1285 Error *local_err = NULL;
1286 int ret;
06d9260f
AL
1287
1288 /*
1289 * Backing files are read-only which makes all of their metadata immutable,
1290 * that means we don't have to worry about reopening them here.
1291 */
1292
1293 if (s->crypt_method) {
1294 crypt_method = s->crypt_method;
1295 memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
1296 memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
1297 }
1298
1299 qcow2_close(bs);
1300
5a8a30db
KW
1301 bdrv_invalidate_cache(bs->file, &local_err);
1302 if (local_err) {
1303 error_propagate(errp, local_err);
1304 return;
1305 }
3456a8d1 1306
06d9260f 1307 memset(s, 0, sizeof(BDRVQcowState));
d475e5ac 1308 options = qdict_clone_shallow(bs->options);
5a8a30db
KW
1309
1310 ret = qcow2_open(bs, options, flags, &local_err);
a1904e48 1311 QDECREF(options);
5a8a30db
KW
1312 if (local_err) {
1313 error_setg(errp, "Could not reopen qcow2 layer: %s",
1314 error_get_pretty(local_err));
1315 error_free(local_err);
1316 return;
1317 } else if (ret < 0) {
1318 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1319 return;
1320 }
acdfb480 1321
06d9260f
AL
1322 if (crypt_method) {
1323 s->crypt_method = crypt_method;
1324 memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
1325 memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
1326 }
1327}
1328
e24e49e6
KW
1329static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1330 size_t len, size_t buflen)
1331{
1332 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1333 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1334
1335 if (buflen < ext_len) {
1336 return -ENOSPC;
1337 }
1338
1339 *ext_backing_fmt = (QCowExtension) {
1340 .magic = cpu_to_be32(magic),
1341 .len = cpu_to_be32(len),
1342 };
1343 memcpy(buf + sizeof(QCowExtension), s, len);
1344
1345 return ext_len;
1346}
1347
756e6736 1348/*
e24e49e6
KW
1349 * Updates the qcow2 header, including the variable length parts of it, i.e.
1350 * the backing file name and all extensions. qcow2 was not designed to allow
1351 * such changes, so if we run out of space (we can only use the first cluster)
1352 * this function may fail.
756e6736
KW
1353 *
1354 * Returns 0 on success, -errno in error cases.
1355 */
e24e49e6 1356int qcow2_update_header(BlockDriverState *bs)
756e6736 1357{
756e6736 1358 BDRVQcowState *s = bs->opaque;
e24e49e6
KW
1359 QCowHeader *header;
1360 char *buf;
1361 size_t buflen = s->cluster_size;
756e6736 1362 int ret;
e24e49e6
KW
1363 uint64_t total_size;
1364 uint32_t refcount_table_clusters;
6744cbab 1365 size_t header_length;
75bab85c 1366 Qcow2UnknownHeaderExtension *uext;
756e6736 1367
e24e49e6 1368 buf = qemu_blockalign(bs, buflen);
756e6736 1369
e24e49e6
KW
1370 /* Header structure */
1371 header = (QCowHeader*) buf;
756e6736 1372
e24e49e6
KW
1373 if (buflen < sizeof(*header)) {
1374 ret = -ENOSPC;
1375 goto fail;
756e6736
KW
1376 }
1377
6744cbab 1378 header_length = sizeof(*header) + s->unknown_header_fields_size;
e24e49e6
KW
1379 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1380 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1381
1382 *header = (QCowHeader) {
6744cbab 1383 /* Version 2 fields */
e24e49e6 1384 .magic = cpu_to_be32(QCOW_MAGIC),
6744cbab 1385 .version = cpu_to_be32(s->qcow_version),
e24e49e6
KW
1386 .backing_file_offset = 0,
1387 .backing_file_size = 0,
1388 .cluster_bits = cpu_to_be32(s->cluster_bits),
1389 .size = cpu_to_be64(total_size),
1390 .crypt_method = cpu_to_be32(s->crypt_method_header),
1391 .l1_size = cpu_to_be32(s->l1_size),
1392 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1393 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1394 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1395 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1396 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
6744cbab
KW
1397
1398 /* Version 3 fields */
1399 .incompatible_features = cpu_to_be64(s->incompatible_features),
1400 .compatible_features = cpu_to_be64(s->compatible_features),
1401 .autoclear_features = cpu_to_be64(s->autoclear_features),
b6481f37 1402 .refcount_order = cpu_to_be32(s->refcount_order),
6744cbab 1403 .header_length = cpu_to_be32(header_length),
e24e49e6 1404 };
756e6736 1405
6744cbab
KW
1406 /* For older versions, write a shorter header */
1407 switch (s->qcow_version) {
1408 case 2:
1409 ret = offsetof(QCowHeader, incompatible_features);
1410 break;
1411 case 3:
1412 ret = sizeof(*header);
1413 break;
1414 default:
b6c14762
JM
1415 ret = -EINVAL;
1416 goto fail;
6744cbab
KW
1417 }
1418
1419 buf += ret;
1420 buflen -= ret;
1421 memset(buf, 0, buflen);
1422
1423 /* Preserve any unknown field in the header */
1424 if (s->unknown_header_fields_size) {
1425 if (buflen < s->unknown_header_fields_size) {
1426 ret = -ENOSPC;
1427 goto fail;
1428 }
1429
1430 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1431 buf += s->unknown_header_fields_size;
1432 buflen -= s->unknown_header_fields_size;
1433 }
756e6736 1434
e24e49e6
KW
1435 /* Backing file format header extension */
1436 if (*bs->backing_format) {
1437 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1438 bs->backing_format, strlen(bs->backing_format),
1439 buflen);
1440 if (ret < 0) {
1441 goto fail;
756e6736
KW
1442 }
1443
e24e49e6
KW
1444 buf += ret;
1445 buflen -= ret;
756e6736
KW
1446 }
1447
cfcc4c62
KW
1448 /* Feature table */
1449 Qcow2Feature features[] = {
c61d0004
SH
1450 {
1451 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1452 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1453 .name = "dirty bit",
1454 },
69c98726
HR
1455 {
1456 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1457 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
1458 .name = "corrupt bit",
1459 },
bfe8043e
SH
1460 {
1461 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1462 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1463 .name = "lazy refcounts",
1464 },
cfcc4c62
KW
1465 };
1466
1467 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1468 features, sizeof(features), buflen);
1469 if (ret < 0) {
1470 goto fail;
1471 }
1472 buf += ret;
1473 buflen -= ret;
1474
75bab85c
KW
1475 /* Keep unknown header extensions */
1476 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1477 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1478 if (ret < 0) {
1479 goto fail;
1480 }
1481
1482 buf += ret;
1483 buflen -= ret;
1484 }
1485
e24e49e6
KW
1486 /* End of header extensions */
1487 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
756e6736
KW
1488 if (ret < 0) {
1489 goto fail;
1490 }
1491
e24e49e6
KW
1492 buf += ret;
1493 buflen -= ret;
756e6736 1494
e24e49e6
KW
1495 /* Backing file name */
1496 if (*bs->backing_file) {
1497 size_t backing_file_len = strlen(bs->backing_file);
1498
1499 if (buflen < backing_file_len) {
1500 ret = -ENOSPC;
1501 goto fail;
1502 }
1503
00ea1881 1504 /* Using strncpy is ok here, since buf is not NUL-terminated. */
e24e49e6
KW
1505 strncpy(buf, bs->backing_file, buflen);
1506
1507 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1508 header->backing_file_size = cpu_to_be32(backing_file_len);
756e6736
KW
1509 }
1510
e24e49e6
KW
1511 /* Write the new header */
1512 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
756e6736
KW
1513 if (ret < 0) {
1514 goto fail;
1515 }
1516
1517 ret = 0;
1518fail:
e24e49e6 1519 qemu_vfree(header);
756e6736
KW
1520 return ret;
1521}
1522
1523static int qcow2_change_backing_file(BlockDriverState *bs,
1524 const char *backing_file, const char *backing_fmt)
1525{
e24e49e6
KW
1526 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1527 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1528
1529 return qcow2_update_header(bs);
756e6736
KW
1530}
1531
a35e1c17
KW
1532static int preallocate(BlockDriverState *bs)
1533{
a35e1c17
KW
1534 uint64_t nb_sectors;
1535 uint64_t offset;
060bee89 1536 uint64_t host_offset = 0;
a35e1c17 1537 int num;
148da7ea 1538 int ret;
f50f88b9 1539 QCowL2Meta *meta;
a35e1c17 1540
7c2bbf4a 1541 nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
a35e1c17
KW
1542 offset = 0;
1543
1544 while (nb_sectors) {
7c2bbf4a 1545 num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
16f0587e 1546 ret = qcow2_alloc_cluster_offset(bs, offset, &num,
060bee89 1547 &host_offset, &meta);
148da7ea 1548 if (ret < 0) {
19dbcbf7 1549 return ret;
a35e1c17
KW
1550 }
1551
c792707f
SH
1552 while (meta) {
1553 QCowL2Meta *next = meta->next;
1554
7c2bbf4a
HT
1555 ret = qcow2_alloc_cluster_link_l2(bs, meta);
1556 if (ret < 0) {
1557 qcow2_free_any_clusters(bs, meta->alloc_offset,
1558 meta->nb_clusters, QCOW2_DISCARD_NEVER);
1559 return ret;
1560 }
1561
1562 /* There are no dependent requests, but we need to remove our
1563 * request from the list of in-flight requests */
4e95314e 1564 QLIST_REMOVE(meta, next_in_flight);
c792707f
SH
1565
1566 g_free(meta);
1567 meta = next;
f50f88b9 1568 }
f214978a 1569
a35e1c17
KW
1570 /* TODO Preallocate data if requested */
1571
1572 nb_sectors -= num;
7c2bbf4a 1573 offset += num << BDRV_SECTOR_BITS;
a35e1c17
KW
1574 }
1575
1576 /*
1577 * It is expected that the image file is large enough to actually contain
1578 * all of the allocated clusters (otherwise we get failing reads after
1579 * EOF). Extend the image to the last allocated sector.
1580 */
060bee89 1581 if (host_offset != 0) {
7c2bbf4a
HT
1582 uint8_t buf[BDRV_SECTOR_SIZE];
1583 memset(buf, 0, BDRV_SECTOR_SIZE);
1584 ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
1585 buf, 1);
19dbcbf7
KW
1586 if (ret < 0) {
1587 return ret;
1588 }
a35e1c17
KW
1589 }
1590
1591 return 0;
1592}
1593
7c80ab3f
JS
1594static int qcow2_create2(const char *filename, int64_t total_size,
1595 const char *backing_file, const char *backing_format,
1596 int flags, size_t cluster_size, int prealloc,
3ef6c40a
HR
1597 QEMUOptionParameter *options, int version,
1598 Error **errp)
a9420734 1599{
9b2260cb 1600 /* Calculate cluster_bits */
a9420734
KW
1601 int cluster_bits;
1602 cluster_bits = ffs(cluster_size) - 1;
1603 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1604 (1 << cluster_bits) != cluster_size)
1605 {
3ef6c40a
HR
1606 error_setg(errp, "Cluster size must be a power of two between %d and "
1607 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
a9420734
KW
1608 return -EINVAL;
1609 }
1610
1611 /*
1612 * Open the image file and write a minimal qcow2 header.
1613 *
1614 * We keep things simple and start with a zero-sized image. We also
1615 * do without refcount blocks or a L1 table for now. We'll fix the
1616 * inconsistency later.
1617 *
1618 * We do need a refcount table because growing the refcount table means
1619 * allocating two new refcount blocks - the seconds of which would be at
1620 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1621 * size for any qcow2 image.
1622 */
1623 BlockDriverState* bs;
f8413b3c 1624 QCowHeader *header;
b106ad91 1625 uint64_t* refcount_table;
3ef6c40a 1626 Error *local_err = NULL;
a9420734
KW
1627 int ret;
1628
83d0521a 1629 ret = bdrv_create_file(filename, options, NULL, &local_err);
a9420734 1630 if (ret < 0) {
3ef6c40a 1631 error_propagate(errp, local_err);
a9420734
KW
1632 return ret;
1633 }
1634
2e40134b
HR
1635 bs = NULL;
1636 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1637 NULL, &local_err);
a9420734 1638 if (ret < 0) {
3ef6c40a 1639 error_propagate(errp, local_err);
a9420734
KW
1640 return ret;
1641 }
1642
1643 /* Write the header */
f8413b3c
KW
1644 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
1645 header = g_malloc0(cluster_size);
1646 *header = (QCowHeader) {
1647 .magic = cpu_to_be32(QCOW_MAGIC),
1648 .version = cpu_to_be32(version),
1649 .cluster_bits = cpu_to_be32(cluster_bits),
1650 .size = cpu_to_be64(0),
1651 .l1_table_offset = cpu_to_be64(0),
1652 .l1_size = cpu_to_be32(0),
1653 .refcount_table_offset = cpu_to_be64(cluster_size),
1654 .refcount_table_clusters = cpu_to_be32(1),
1655 .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT),
1656 .header_length = cpu_to_be32(sizeof(*header)),
1657 };
a9420734
KW
1658
1659 if (flags & BLOCK_FLAG_ENCRYPT) {
f8413b3c 1660 header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
a9420734 1661 } else {
f8413b3c 1662 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
a9420734
KW
1663 }
1664
bfe8043e 1665 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
f8413b3c 1666 header->compatible_features |=
bfe8043e
SH
1667 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1668 }
1669
f8413b3c
KW
1670 ret = bdrv_pwrite(bs, 0, header, cluster_size);
1671 g_free(header);
a9420734 1672 if (ret < 0) {
3ef6c40a 1673 error_setg_errno(errp, -ret, "Could not write qcow2 header");
a9420734
KW
1674 goto out;
1675 }
1676
b106ad91
KW
1677 /* Write a refcount table with one refcount block */
1678 refcount_table = g_malloc0(2 * cluster_size);
1679 refcount_table[0] = cpu_to_be64(2 * cluster_size);
1680 ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size);
7267c094 1681 g_free(refcount_table);
a9420734
KW
1682
1683 if (ret < 0) {
3ef6c40a 1684 error_setg_errno(errp, -ret, "Could not write refcount table");
a9420734
KW
1685 goto out;
1686 }
1687
f67503e5
HR
1688 bdrv_unref(bs);
1689 bs = NULL;
a9420734
KW
1690
1691 /*
1692 * And now open the image and make it consistent first (i.e. increase the
1693 * refcount of the cluster that is occupied by the header and the refcount
1694 * table)
1695 */
1696 BlockDriver* drv = bdrv_find_format("qcow2");
1697 assert(drv != NULL);
ddf5636d 1698 ret = bdrv_open(&bs, filename, NULL, NULL,
3ef6c40a 1699 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, &local_err);
a9420734 1700 if (ret < 0) {
3ef6c40a 1701 error_propagate(errp, local_err);
a9420734
KW
1702 goto out;
1703 }
1704
b106ad91 1705 ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
a9420734 1706 if (ret < 0) {
3ef6c40a
HR
1707 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
1708 "header and refcount table");
a9420734
KW
1709 goto out;
1710
1711 } else if (ret != 0) {
1712 error_report("Huh, first cluster in empty image is already in use?");
1713 abort();
1714 }
1715
1716 /* Okay, now that we have a valid image, let's give it the right size */
1717 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1718 if (ret < 0) {
3ef6c40a 1719 error_setg_errno(errp, -ret, "Could not resize image");
a9420734
KW
1720 goto out;
1721 }
1722
1723 /* Want a backing file? There you go.*/
1724 if (backing_file) {
1725 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1726 if (ret < 0) {
3ef6c40a
HR
1727 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
1728 "with format '%s'", backing_file, backing_format);
a9420734
KW
1729 goto out;
1730 }
1731 }
1732
1733 /* And if we're supposed to preallocate metadata, do that now */
1734 if (prealloc) {
15552c4a
ZYW
1735 BDRVQcowState *s = bs->opaque;
1736 qemu_co_mutex_lock(&s->lock);
a9420734 1737 ret = preallocate(bs);
15552c4a 1738 qemu_co_mutex_unlock(&s->lock);
a9420734 1739 if (ret < 0) {
3ef6c40a 1740 error_setg_errno(errp, -ret, "Could not preallocate metadata");
a9420734
KW
1741 goto out;
1742 }
1743 }
1744
f67503e5
HR
1745 bdrv_unref(bs);
1746 bs = NULL;
ba2ab2f2
HR
1747
1748 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
ddf5636d 1749 ret = bdrv_open(&bs, filename, NULL, NULL,
c9fbb99d
KW
1750 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
1751 drv, &local_err);
84d18f06 1752 if (local_err) {
ba2ab2f2
HR
1753 error_propagate(errp, local_err);
1754 goto out;
1755 }
1756
a9420734
KW
1757 ret = 0;
1758out:
f67503e5
HR
1759 if (bs) {
1760 bdrv_unref(bs);
1761 }
a9420734
KW
1762 return ret;
1763}
de5f3f40 1764
d5124c00
HR
1765static int qcow2_create(const char *filename, QEMUOptionParameter *options,
1766 Error **errp)
de5f3f40
KW
1767{
1768 const char *backing_file = NULL;
1769 const char *backing_fmt = NULL;
1770 uint64_t sectors = 0;
1771 int flags = 0;
99cce9fa 1772 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
de5f3f40 1773 int prealloc = 0;
8ad1898c 1774 int version = 3;
3ef6c40a
HR
1775 Error *local_err = NULL;
1776 int ret;
de5f3f40
KW
1777
1778 /* Read out options */
1779 while (options && options->name) {
1780 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1781 sectors = options->value.n / 512;
1782 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1783 backing_file = options->value.s;
1784 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1785 backing_fmt = options->value.s;
1786 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1787 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1788 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1789 if (options->value.n) {
1790 cluster_size = options->value.n;
1791 }
1792 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1793 if (!options->value.s || !strcmp(options->value.s, "off")) {
1794 prealloc = 0;
1795 } else if (!strcmp(options->value.s, "metadata")) {
1796 prealloc = 1;
1797 } else {
3ef6c40a
HR
1798 error_setg(errp, "Invalid preallocation mode: '%s'",
1799 options->value.s);
de5f3f40
KW
1800 return -EINVAL;
1801 }
6744cbab 1802 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
9117b477
KW
1803 if (!options->value.s) {
1804 /* keep the default */
1805 } else if (!strcmp(options->value.s, "0.10")) {
6744cbab
KW
1806 version = 2;
1807 } else if (!strcmp(options->value.s, "1.1")) {
1808 version = 3;
1809 } else {
3ef6c40a
HR
1810 error_setg(errp, "Invalid compatibility level: '%s'",
1811 options->value.s);
6744cbab
KW
1812 return -EINVAL;
1813 }
bfe8043e
SH
1814 } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1815 flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
de5f3f40
KW
1816 }
1817 options++;
1818 }
1819
1820 if (backing_file && prealloc) {
3ef6c40a
HR
1821 error_setg(errp, "Backing file and preallocation cannot be used at "
1822 "the same time");
de5f3f40
KW
1823 return -EINVAL;
1824 }
1825
bfe8043e 1826 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
3ef6c40a
HR
1827 error_setg(errp, "Lazy refcounts only supported with compatibility "
1828 "level 1.1 and above (use compat=1.1 or greater)");
bfe8043e
SH
1829 return -EINVAL;
1830 }
1831
3ef6c40a
HR
1832 ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1833 cluster_size, prealloc, options, version, &local_err);
84d18f06 1834 if (local_err) {
3ef6c40a
HR
1835 error_propagate(errp, local_err);
1836 }
1837 return ret;
de5f3f40
KW
1838}
1839
621f0589 1840static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
aa7bfbff 1841 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
621f0589
KW
1842{
1843 int ret;
1844 BDRVQcowState *s = bs->opaque;
1845
1846 /* Emulate misaligned zero writes */
1847 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1848 return -ENOTSUP;
1849 }
1850
1851 /* Whatever is left can use real zero clusters */
1852 qemu_co_mutex_lock(&s->lock);
1853 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1854 nb_sectors);
1855 qemu_co_mutex_unlock(&s->lock);
1856
1857 return ret;
1858}
1859
6db39ae2
PB
1860static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1861 int64_t sector_num, int nb_sectors)
5ea929e3 1862{
6db39ae2
PB
1863 int ret;
1864 BDRVQcowState *s = bs->opaque;
1865
1866 qemu_co_mutex_lock(&s->lock);
1867 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
670df5e3 1868 nb_sectors, QCOW2_DISCARD_REQUEST);
6db39ae2
PB
1869 qemu_co_mutex_unlock(&s->lock);
1870 return ret;
5ea929e3
KW
1871}
1872
419b19d9
SH
1873static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1874{
1875 BDRVQcowState *s = bs->opaque;
2cf7cfa1
KW
1876 int64_t new_l1_size;
1877 int ret;
419b19d9
SH
1878
1879 if (offset & 511) {
259b2173 1880 error_report("The new size must be a multiple of 512");
419b19d9
SH
1881 return -EINVAL;
1882 }
1883
1884 /* cannot proceed if image has snapshots */
1885 if (s->nb_snapshots) {
259b2173 1886 error_report("Can't resize an image which has snapshots");
419b19d9
SH
1887 return -ENOTSUP;
1888 }
1889
1890 /* shrinking is currently not supported */
1891 if (offset < bs->total_sectors * 512) {
259b2173 1892 error_report("qcow2 doesn't support shrinking images yet");
419b19d9
SH
1893 return -ENOTSUP;
1894 }
1895
1896 new_l1_size = size_to_l1(s, offset);
72893756 1897 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
419b19d9
SH
1898 if (ret < 0) {
1899 return ret;
1900 }
1901
1902 /* write updated header.size */
1903 offset = cpu_to_be64(offset);
8b3b7206
KW
1904 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1905 &offset, sizeof(uint64_t));
419b19d9
SH
1906 if (ret < 0) {
1907 return ret;
1908 }
1909
1910 s->l1_vm_state_index = new_l1_size;
1911 return 0;
1912}
1913
20d97356
BS
1914/* XXX: put compressed sectors first, then all the cluster aligned
1915 tables to avoid losing bytes in alignment */
7c80ab3f
JS
1916static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1917 const uint8_t *buf, int nb_sectors)
20d97356
BS
1918{
1919 BDRVQcowState *s = bs->opaque;
1920 z_stream strm;
1921 int ret, out_len;
1922 uint8_t *out_buf;
1923 uint64_t cluster_offset;
1924
1925 if (nb_sectors == 0) {
1926 /* align end of file to a sector boundary to ease reading with
1927 sector based I/Os */
66f82cee 1928 cluster_offset = bdrv_getlength(bs->file);
20d97356 1929 cluster_offset = (cluster_offset + 511) & ~511;
66f82cee 1930 bdrv_truncate(bs->file, cluster_offset);
20d97356
BS
1931 return 0;
1932 }
1933
f4d38bef
SH
1934 if (nb_sectors != s->cluster_sectors) {
1935 ret = -EINVAL;
1936
1937 /* Zero-pad last write if image size is not cluster aligned */
1938 if (sector_num + nb_sectors == bs->total_sectors &&
1939 nb_sectors < s->cluster_sectors) {
1940 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
1941 memset(pad_buf, 0, s->cluster_size);
1942 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
1943 ret = qcow2_write_compressed(bs, sector_num,
1944 pad_buf, s->cluster_sectors);
1945 qemu_vfree(pad_buf);
1946 }
1947 return ret;
1948 }
20d97356 1949
7267c094 1950 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
20d97356
BS
1951
1952 /* best compression, small window, no zlib header */
1953 memset(&strm, 0, sizeof(strm));
1954 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1955 Z_DEFLATED, -12,
1956 9, Z_DEFAULT_STRATEGY);
1957 if (ret != 0) {
8f1efd00
KW
1958 ret = -EINVAL;
1959 goto fail;
20d97356
BS
1960 }
1961
1962 strm.avail_in = s->cluster_size;
1963 strm.next_in = (uint8_t *)buf;
1964 strm.avail_out = s->cluster_size;
1965 strm.next_out = out_buf;
1966
1967 ret = deflate(&strm, Z_FINISH);
1968 if (ret != Z_STREAM_END && ret != Z_OK) {
20d97356 1969 deflateEnd(&strm);
8f1efd00
KW
1970 ret = -EINVAL;
1971 goto fail;
20d97356
BS
1972 }
1973 out_len = strm.next_out - out_buf;
1974
1975 deflateEnd(&strm);
1976
1977 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1978 /* could not compress: write normal cluster */
8f1efd00
KW
1979 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1980 if (ret < 0) {
1981 goto fail;
1982 }
20d97356
BS
1983 } else {
1984 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1985 sector_num << 9, out_len);
8f1efd00
KW
1986 if (!cluster_offset) {
1987 ret = -EIO;
1988 goto fail;
1989 }
20d97356 1990 cluster_offset &= s->cluster_offset_mask;
cf93980e 1991
231bb267 1992 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
cf93980e
HR
1993 if (ret < 0) {
1994 goto fail;
1995 }
1996
66f82cee 1997 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
8f1efd00
KW
1998 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1999 if (ret < 0) {
2000 goto fail;
20d97356
BS
2001 }
2002 }
2003
8f1efd00
KW
2004 ret = 0;
2005fail:
7267c094 2006 g_free(out_buf);
8f1efd00 2007 return ret;
20d97356
BS
2008}
2009
a968168c 2010static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
20d97356 2011{
29c1a730
KW
2012 BDRVQcowState *s = bs->opaque;
2013 int ret;
2014
8b94ff85 2015 qemu_co_mutex_lock(&s->lock);
29c1a730
KW
2016 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2017 if (ret < 0) {
c95de7e2 2018 qemu_co_mutex_unlock(&s->lock);
8b94ff85 2019 return ret;
29c1a730
KW
2020 }
2021
bfe8043e
SH
2022 if (qcow2_need_accurate_refcounts(s)) {
2023 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2024 if (ret < 0) {
2025 qemu_co_mutex_unlock(&s->lock);
2026 return ret;
2027 }
29c1a730 2028 }
8b94ff85 2029 qemu_co_mutex_unlock(&s->lock);
29c1a730 2030
eb489bb1
KW
2031 return 0;
2032}
2033
7c80ab3f 2034static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
20d97356
BS
2035{
2036 BDRVQcowState *s = bs->opaque;
95de6d70
PB
2037 bdi->unallocated_blocks_are_zero = true;
2038 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
20d97356 2039 bdi->cluster_size = s->cluster_size;
7c80ab3f 2040 bdi->vm_state_offset = qcow2_vm_state_offset(s);
20d97356
BS
2041 return 0;
2042}
2043
37764dfb
HR
2044static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
2045{
2046 BDRVQcowState *s = bs->opaque;
2047 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
2048
2049 *spec_info = (ImageInfoSpecific){
2050 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
2051 {
2052 .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
2053 },
2054 };
2055 if (s->qcow_version == 2) {
2056 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2057 .compat = g_strdup("0.10"),
2058 };
2059 } else if (s->qcow_version == 3) {
2060 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2061 .compat = g_strdup("1.1"),
2062 .lazy_refcounts = s->compatible_features &
2063 QCOW2_COMPAT_LAZY_REFCOUNTS,
2064 .has_lazy_refcounts = true,
2065 };
2066 }
2067
2068 return spec_info;
2069}
2070
20d97356
BS
2071#if 0
2072static void dump_refcounts(BlockDriverState *bs)
2073{
2074 BDRVQcowState *s = bs->opaque;
2075 int64_t nb_clusters, k, k1, size;
2076 int refcount;
2077
66f82cee 2078 size = bdrv_getlength(bs->file);
20d97356
BS
2079 nb_clusters = size_to_clusters(s, size);
2080 for(k = 0; k < nb_clusters;) {
2081 k1 = k;
2082 refcount = get_refcount(bs, k);
2083 k++;
2084 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2085 k++;
0bfcd599
BS
2086 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
2087 k - k1);
20d97356
BS
2088 }
2089}
2090#endif
2091
cf8074b3
KW
2092static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2093 int64_t pos)
20d97356
BS
2094{
2095 BDRVQcowState *s = bs->opaque;
eedff66f 2096 int64_t total_sectors = bs->total_sectors;
20d97356 2097 int growable = bs->growable;
6e13610a 2098 bool zero_beyond_eof = bs->zero_beyond_eof;
20d97356
BS
2099 int ret;
2100
66f82cee 2101 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
20d97356 2102 bs->growable = 1;
6e13610a 2103 bs->zero_beyond_eof = false;
8d3b1a2d 2104 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
20d97356 2105 bs->growable = growable;
6e13610a 2106 bs->zero_beyond_eof = zero_beyond_eof;
20d97356 2107
eedff66f
HR
2108 /* bdrv_co_do_writev will have increased the total_sectors value to include
2109 * the VM state - the VM state is however not an actual part of the block
2110 * device, therefore, we need to restore the old value. */
2111 bs->total_sectors = total_sectors;
2112
20d97356
BS
2113 return ret;
2114}
2115
7c80ab3f
JS
2116static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2117 int64_t pos, int size)
20d97356
BS
2118{
2119 BDRVQcowState *s = bs->opaque;
2120 int growable = bs->growable;
0d51b4de 2121 bool zero_beyond_eof = bs->zero_beyond_eof;
20d97356
BS
2122 int ret;
2123
66f82cee 2124 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
20d97356 2125 bs->growable = 1;
0d51b4de 2126 bs->zero_beyond_eof = false;
7c80ab3f 2127 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
20d97356 2128 bs->growable = growable;
0d51b4de 2129 bs->zero_beyond_eof = zero_beyond_eof;
20d97356
BS
2130
2131 return ret;
2132}
2133
9296b3ed
HR
2134/*
2135 * Downgrades an image's version. To achieve this, any incompatible features
2136 * have to be removed.
2137 */
2138static int qcow2_downgrade(BlockDriverState *bs, int target_version)
2139{
2140 BDRVQcowState *s = bs->opaque;
2141 int current_version = s->qcow_version;
2142 int ret;
2143
2144 if (target_version == current_version) {
2145 return 0;
2146 } else if (target_version > current_version) {
2147 return -EINVAL;
2148 } else if (target_version != 2) {
2149 return -EINVAL;
2150 }
2151
2152 if (s->refcount_order != 4) {
2153 /* we would have to convert the image to a refcount_order == 4 image
2154 * here; however, since qemu (at the time of writing this) does not
2155 * support anything different than 4 anyway, there is no point in doing
2156 * so right now; however, we should error out (if qemu supports this in
2157 * the future and this code has not been adapted) */
9e3f0892 2158 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
9296b3ed
HR
2159 "currently not supported.");
2160 return -ENOTSUP;
2161 }
2162
2163 /* clear incompatible features */
2164 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2165 ret = qcow2_mark_clean(bs);
2166 if (ret < 0) {
2167 return ret;
2168 }
2169 }
2170
2171 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2172 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2173 * best thing to do anyway */
2174
2175 if (s->incompatible_features) {
2176 return -ENOTSUP;
2177 }
2178
2179 /* since we can ignore compatible features, we can set them to 0 as well */
2180 s->compatible_features = 0;
2181 /* if lazy refcounts have been used, they have already been fixed through
2182 * clearing the dirty flag */
2183
2184 /* clearing autoclear features is trivial */
2185 s->autoclear_features = 0;
2186
2187 ret = qcow2_expand_zero_clusters(bs);
2188 if (ret < 0) {
2189 return ret;
2190 }
2191
2192 s->qcow_version = target_version;
2193 ret = qcow2_update_header(bs);
2194 if (ret < 0) {
2195 s->qcow_version = current_version;
2196 return ret;
2197 }
2198 return 0;
2199}
2200
2201static int qcow2_amend_options(BlockDriverState *bs,
2202 QEMUOptionParameter *options)
2203{
2204 BDRVQcowState *s = bs->opaque;
2205 int old_version = s->qcow_version, new_version = old_version;
2206 uint64_t new_size = 0;
2207 const char *backing_file = NULL, *backing_format = NULL;
2208 bool lazy_refcounts = s->use_lazy_refcounts;
2209 int ret;
2210 int i;
2211
2212 for (i = 0; options[i].name; i++)
2213 {
2214 if (!options[i].assigned) {
2215 /* only change explicitly defined options */
2216 continue;
2217 }
2218
2219 if (!strcmp(options[i].name, "compat")) {
2220 if (!options[i].value.s) {
2221 /* preserve default */
2222 } else if (!strcmp(options[i].value.s, "0.10")) {
2223 new_version = 2;
2224 } else if (!strcmp(options[i].value.s, "1.1")) {
2225 new_version = 3;
2226 } else {
2227 fprintf(stderr, "Unknown compatibility level %s.\n",
2228 options[i].value.s);
2229 return -EINVAL;
2230 }
2231 } else if (!strcmp(options[i].name, "preallocation")) {
2232 fprintf(stderr, "Cannot change preallocation mode.\n");
2233 return -ENOTSUP;
2234 } else if (!strcmp(options[i].name, "size")) {
2235 new_size = options[i].value.n;
2236 } else if (!strcmp(options[i].name, "backing_file")) {
2237 backing_file = options[i].value.s;
2238 } else if (!strcmp(options[i].name, "backing_fmt")) {
2239 backing_format = options[i].value.s;
2240 } else if (!strcmp(options[i].name, "encryption")) {
2241 if ((options[i].value.n != !!s->crypt_method)) {
2242 fprintf(stderr, "Changing the encryption flag is not "
2243 "supported.\n");
2244 return -ENOTSUP;
2245 }
2246 } else if (!strcmp(options[i].name, "cluster_size")) {
2247 if (options[i].value.n != s->cluster_size) {
2248 fprintf(stderr, "Changing the cluster size is not "
2249 "supported.\n");
2250 return -ENOTSUP;
2251 }
2252 } else if (!strcmp(options[i].name, "lazy_refcounts")) {
2253 lazy_refcounts = options[i].value.n;
2254 } else {
2255 /* if this assertion fails, this probably means a new option was
2256 * added without having it covered here */
2257 assert(false);
2258 }
2259 }
2260
2261 if (new_version != old_version) {
2262 if (new_version > old_version) {
2263 /* Upgrade */
2264 s->qcow_version = new_version;
2265 ret = qcow2_update_header(bs);
2266 if (ret < 0) {
2267 s->qcow_version = old_version;
2268 return ret;
2269 }
2270 } else {
2271 ret = qcow2_downgrade(bs, new_version);
2272 if (ret < 0) {
2273 return ret;
2274 }
2275 }
2276 }
2277
2278 if (backing_file || backing_format) {
2279 ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
2280 backing_format ?: bs->backing_format);
2281 if (ret < 0) {
2282 return ret;
2283 }
2284 }
2285
2286 if (s->use_lazy_refcounts != lazy_refcounts) {
2287 if (lazy_refcounts) {
2288 if (s->qcow_version < 3) {
2289 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2290 "level 1.1 and above (use compat=1.1 or greater)\n");
2291 return -EINVAL;
2292 }
2293 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2294 ret = qcow2_update_header(bs);
2295 if (ret < 0) {
2296 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2297 return ret;
2298 }
2299 s->use_lazy_refcounts = true;
2300 } else {
2301 /* make image clean first */
2302 ret = qcow2_mark_clean(bs);
2303 if (ret < 0) {
2304 return ret;
2305 }
2306 /* now disallow lazy refcounts */
2307 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2308 ret = qcow2_update_header(bs);
2309 if (ret < 0) {
2310 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2311 return ret;
2312 }
2313 s->use_lazy_refcounts = false;
2314 }
2315 }
2316
2317 if (new_size) {
2318 ret = bdrv_truncate(bs, new_size);
2319 if (ret < 0) {
2320 return ret;
2321 }
2322 }
2323
2324 return 0;
2325}
2326
7c80ab3f 2327static QEMUOptionParameter qcow2_create_options[] = {
20d97356
BS
2328 {
2329 .name = BLOCK_OPT_SIZE,
2330 .type = OPT_SIZE,
2331 .help = "Virtual disk size"
2332 },
6744cbab
KW
2333 {
2334 .name = BLOCK_OPT_COMPAT_LEVEL,
2335 .type = OPT_STRING,
2336 .help = "Compatibility level (0.10 or 1.1)"
2337 },
20d97356
BS
2338 {
2339 .name = BLOCK_OPT_BACKING_FILE,
2340 .type = OPT_STRING,
2341 .help = "File name of a base image"
2342 },
2343 {
2344 .name = BLOCK_OPT_BACKING_FMT,
2345 .type = OPT_STRING,
2346 .help = "Image format of the base image"
2347 },
2348 {
2349 .name = BLOCK_OPT_ENCRYPT,
2350 .type = OPT_FLAG,
2351 .help = "Encrypt the image"
2352 },
2353 {
2354 .name = BLOCK_OPT_CLUSTER_SIZE,
2355 .type = OPT_SIZE,
99cce9fa
KW
2356 .help = "qcow2 cluster size",
2357 .value = { .n = DEFAULT_CLUSTER_SIZE },
20d97356
BS
2358 },
2359 {
2360 .name = BLOCK_OPT_PREALLOC,
2361 .type = OPT_STRING,
2362 .help = "Preallocation mode (allowed values: off, metadata)"
2363 },
bfe8043e
SH
2364 {
2365 .name = BLOCK_OPT_LAZY_REFCOUNTS,
2366 .type = OPT_FLAG,
2367 .help = "Postpone refcount updates",
2368 },
20d97356
BS
2369 { NULL }
2370};
2371
2372static BlockDriver bdrv_qcow2 = {
7c80ab3f
JS
2373 .format_name = "qcow2",
2374 .instance_size = sizeof(BDRVQcowState),
2375 .bdrv_probe = qcow2_probe,
2376 .bdrv_open = qcow2_open,
2377 .bdrv_close = qcow2_close,
21d82ac9 2378 .bdrv_reopen_prepare = qcow2_reopen_prepare,
7c80ab3f 2379 .bdrv_create = qcow2_create,
3ac21627 2380 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 2381 .bdrv_co_get_block_status = qcow2_co_get_block_status,
7c80ab3f 2382 .bdrv_set_key = qcow2_set_key,
7c80ab3f 2383
c68b89ac
KW
2384 .bdrv_co_readv = qcow2_co_readv,
2385 .bdrv_co_writev = qcow2_co_writev,
eb489bb1 2386 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
419b19d9 2387
621f0589 2388 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
6db39ae2 2389 .bdrv_co_discard = qcow2_co_discard,
419b19d9 2390 .bdrv_truncate = qcow2_truncate,
7c80ab3f 2391 .bdrv_write_compressed = qcow2_write_compressed,
20d97356
BS
2392
2393 .bdrv_snapshot_create = qcow2_snapshot_create,
2394 .bdrv_snapshot_goto = qcow2_snapshot_goto,
2395 .bdrv_snapshot_delete = qcow2_snapshot_delete,
2396 .bdrv_snapshot_list = qcow2_snapshot_list,
51ef6727 2397 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
7c80ab3f 2398 .bdrv_get_info = qcow2_get_info,
37764dfb 2399 .bdrv_get_specific_info = qcow2_get_specific_info,
20d97356 2400
7c80ab3f
JS
2401 .bdrv_save_vmstate = qcow2_save_vmstate,
2402 .bdrv_load_vmstate = qcow2_load_vmstate,
20d97356
BS
2403
2404 .bdrv_change_backing_file = qcow2_change_backing_file,
2405
d34682cd 2406 .bdrv_refresh_limits = qcow2_refresh_limits,
06d9260f
AL
2407 .bdrv_invalidate_cache = qcow2_invalidate_cache,
2408
7c80ab3f
JS
2409 .create_options = qcow2_create_options,
2410 .bdrv_check = qcow2_check,
9296b3ed 2411 .bdrv_amend_options = qcow2_amend_options,
20d97356
BS
2412};
2413
5efa9d5a
AL
2414static void bdrv_qcow2_init(void)
2415{
2416 bdrv_register(&bdrv_qcow2);
2417}
2418
2419block_init(bdrv_qcow2_init);