]> git.proxmox.com Git - mirror_qemu.git/blob - block/qcow2.c
e4e690a42b2d6413491c277c6dac3280b49f51b0
[mirror_qemu.git] / block / qcow2.c
1 /*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
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 */
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/module.h"
27 #include <zlib.h>
28 #include "qemu/aes.h"
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/util.h"
34 #include "qapi/qmp/types.h"
35 #include "qapi-event.h"
36 #include "trace.h"
37 #include "qemu/option_int.h"
38
39 /*
40 Differences with QCOW:
41
42 - Support for multiple incremental snapshots.
43 - Memory management by reference counts.
44 - Clusters which have a reference count of one have the bit
45 QCOW_OFLAG_COPIED to optimize write performance.
46 - Size of compressed clusters is stored in sectors to reduce bit usage
47 in the cluster offsets.
48 - Support for storing additional data (such as the VM state) in the
49 snapshots.
50 - If a backing store is used, the cluster size is not constrained
51 (could be backported to QCOW).
52 - L2 tables have always a size of one cluster.
53 */
54
55
56 typedef struct {
57 uint32_t magic;
58 uint32_t len;
59 } QEMU_PACKED QCowExtension;
60
61 #define QCOW2_EXT_MAGIC_END 0
62 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
63 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
64
65 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
66 {
67 const QCowHeader *cow_header = (const void *)buf;
68
69 if (buf_size >= sizeof(QCowHeader) &&
70 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
71 be32_to_cpu(cow_header->version) >= 2)
72 return 100;
73 else
74 return 0;
75 }
76
77
78 /*
79 * read qcow2 extension and fill bs
80 * start reading from start_offset
81 * finish reading upon magic of value 0 or when end_offset reached
82 * unknown magic is skipped (future extension this version knows nothing about)
83 * return 0 upon success, non-0 otherwise
84 */
85 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
86 uint64_t end_offset, void **p_feature_table,
87 Error **errp)
88 {
89 BDRVQcowState *s = bs->opaque;
90 QCowExtension ext;
91 uint64_t offset;
92 int ret;
93
94 #ifdef DEBUG_EXT
95 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
96 #endif
97 offset = start_offset;
98 while (offset < end_offset) {
99
100 #ifdef DEBUG_EXT
101 /* Sanity check */
102 if (offset > s->cluster_size)
103 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
104
105 printf("attempting to read extended header in offset %lu\n", offset);
106 #endif
107
108 ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext));
109 if (ret < 0) {
110 error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
111 "pread fail from offset %" PRIu64, offset);
112 return 1;
113 }
114 be32_to_cpus(&ext.magic);
115 be32_to_cpus(&ext.len);
116 offset += sizeof(ext);
117 #ifdef DEBUG_EXT
118 printf("ext.magic = 0x%x\n", ext.magic);
119 #endif
120 if (offset > end_offset || ext.len > end_offset - offset) {
121 error_setg(errp, "Header extension too large");
122 return -EINVAL;
123 }
124
125 switch (ext.magic) {
126 case QCOW2_EXT_MAGIC_END:
127 return 0;
128
129 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
130 if (ext.len >= sizeof(bs->backing_format)) {
131 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
132 " too large (>=%zu)", ext.len,
133 sizeof(bs->backing_format));
134 return 2;
135 }
136 ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len);
137 if (ret < 0) {
138 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
139 "Could not read format name");
140 return 3;
141 }
142 bs->backing_format[ext.len] = '\0';
143 #ifdef DEBUG_EXT
144 printf("Qcow2: Got format extension %s\n", bs->backing_format);
145 #endif
146 break;
147
148 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
149 if (p_feature_table != NULL) {
150 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
151 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
152 if (ret < 0) {
153 error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
154 "Could not read table");
155 return ret;
156 }
157
158 *p_feature_table = feature_table;
159 }
160 break;
161
162 default:
163 /* unknown magic - save it in case we need to rewrite the header */
164 {
165 Qcow2UnknownHeaderExtension *uext;
166
167 uext = g_malloc0(sizeof(*uext) + ext.len);
168 uext->magic = ext.magic;
169 uext->len = ext.len;
170 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
171
172 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
173 if (ret < 0) {
174 error_setg_errno(errp, -ret, "ERROR: unknown extension: "
175 "Could not read data");
176 return ret;
177 }
178 }
179 break;
180 }
181
182 offset += ((ext.len + 7) & ~7);
183 }
184
185 return 0;
186 }
187
188 static void cleanup_unknown_header_ext(BlockDriverState *bs)
189 {
190 BDRVQcowState *s = bs->opaque;
191 Qcow2UnknownHeaderExtension *uext, *next;
192
193 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
194 QLIST_REMOVE(uext, next);
195 g_free(uext);
196 }
197 }
198
199 static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
200 Error **errp, const char *fmt, ...)
201 {
202 char msg[64];
203 va_list ap;
204
205 va_start(ap, fmt);
206 vsnprintf(msg, sizeof(msg), fmt, ap);
207 va_end(ap);
208
209 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
210 bdrv_get_device_name(bs), "qcow2", msg);
211 }
212
213 static void report_unsupported_feature(BlockDriverState *bs,
214 Error **errp, Qcow2Feature *table, uint64_t mask)
215 {
216 char *features = g_strdup("");
217 char *old;
218
219 while (table && table->name[0] != '\0') {
220 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
221 if (mask & (1ULL << table->bit)) {
222 old = features;
223 features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
224 table->name);
225 g_free(old);
226 mask &= ~(1ULL << table->bit);
227 }
228 }
229 table++;
230 }
231
232 if (mask) {
233 old = features;
234 features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
235 old, *old ? ", " : "", mask);
236 g_free(old);
237 }
238
239 report_unsupported(bs, errp, "%s", features);
240 g_free(features);
241 }
242
243 /*
244 * Sets the dirty bit and flushes afterwards if necessary.
245 *
246 * The incompatible_features bit is only set if the image file header was
247 * updated successfully. Therefore it is not required to check the return
248 * value of this function.
249 */
250 int qcow2_mark_dirty(BlockDriverState *bs)
251 {
252 BDRVQcowState *s = bs->opaque;
253 uint64_t val;
254 int ret;
255
256 assert(s->qcow_version >= 3);
257
258 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
259 return 0; /* already dirty */
260 }
261
262 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
263 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
264 &val, sizeof(val));
265 if (ret < 0) {
266 return ret;
267 }
268 ret = bdrv_flush(bs->file);
269 if (ret < 0) {
270 return ret;
271 }
272
273 /* Only treat image as dirty if the header was updated successfully */
274 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
275 return 0;
276 }
277
278 /*
279 * Clears the dirty bit and flushes before if necessary. Only call this
280 * function when there are no pending requests, it does not guard against
281 * concurrent requests dirtying the image.
282 */
283 static int qcow2_mark_clean(BlockDriverState *bs)
284 {
285 BDRVQcowState *s = bs->opaque;
286
287 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
288 int ret;
289
290 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
291
292 ret = bdrv_flush(bs);
293 if (ret < 0) {
294 return ret;
295 }
296
297 return qcow2_update_header(bs);
298 }
299 return 0;
300 }
301
302 /*
303 * Marks the image as corrupt.
304 */
305 int qcow2_mark_corrupt(BlockDriverState *bs)
306 {
307 BDRVQcowState *s = bs->opaque;
308
309 s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
310 return qcow2_update_header(bs);
311 }
312
313 /*
314 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
315 * before if necessary.
316 */
317 int qcow2_mark_consistent(BlockDriverState *bs)
318 {
319 BDRVQcowState *s = bs->opaque;
320
321 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
322 int ret = bdrv_flush(bs);
323 if (ret < 0) {
324 return ret;
325 }
326
327 s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
328 return qcow2_update_header(bs);
329 }
330 return 0;
331 }
332
333 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
334 BdrvCheckMode fix)
335 {
336 int ret = qcow2_check_refcounts(bs, result, fix);
337 if (ret < 0) {
338 return ret;
339 }
340
341 if (fix && result->check_errors == 0 && result->corruptions == 0) {
342 ret = qcow2_mark_clean(bs);
343 if (ret < 0) {
344 return ret;
345 }
346 return qcow2_mark_consistent(bs);
347 }
348 return ret;
349 }
350
351 static int validate_table_offset(BlockDriverState *bs, uint64_t offset,
352 uint64_t entries, size_t entry_len)
353 {
354 BDRVQcowState *s = bs->opaque;
355 uint64_t size;
356
357 /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
358 * because values will be passed to qemu functions taking int64_t. */
359 if (entries > INT64_MAX / entry_len) {
360 return -EINVAL;
361 }
362
363 size = entries * entry_len;
364
365 if (INT64_MAX - size < offset) {
366 return -EINVAL;
367 }
368
369 /* Tables must be cluster aligned */
370 if (offset & (s->cluster_size - 1)) {
371 return -EINVAL;
372 }
373
374 return 0;
375 }
376
377 static QemuOptsList qcow2_runtime_opts = {
378 .name = "qcow2",
379 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
380 .desc = {
381 {
382 .name = QCOW2_OPT_LAZY_REFCOUNTS,
383 .type = QEMU_OPT_BOOL,
384 .help = "Postpone refcount updates",
385 },
386 {
387 .name = QCOW2_OPT_DISCARD_REQUEST,
388 .type = QEMU_OPT_BOOL,
389 .help = "Pass guest discard requests to the layer below",
390 },
391 {
392 .name = QCOW2_OPT_DISCARD_SNAPSHOT,
393 .type = QEMU_OPT_BOOL,
394 .help = "Generate discard requests when snapshot related space "
395 "is freed",
396 },
397 {
398 .name = QCOW2_OPT_DISCARD_OTHER,
399 .type = QEMU_OPT_BOOL,
400 .help = "Generate discard requests when other clusters are freed",
401 },
402 {
403 .name = QCOW2_OPT_OVERLAP,
404 .type = QEMU_OPT_STRING,
405 .help = "Selects which overlap checks to perform from a range of "
406 "templates (none, constant, cached, all)",
407 },
408 {
409 .name = QCOW2_OPT_OVERLAP_TEMPLATE,
410 .type = QEMU_OPT_STRING,
411 .help = "Selects which overlap checks to perform from a range of "
412 "templates (none, constant, cached, all)",
413 },
414 {
415 .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
416 .type = QEMU_OPT_BOOL,
417 .help = "Check for unintended writes into the main qcow2 header",
418 },
419 {
420 .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
421 .type = QEMU_OPT_BOOL,
422 .help = "Check for unintended writes into the active L1 table",
423 },
424 {
425 .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
426 .type = QEMU_OPT_BOOL,
427 .help = "Check for unintended writes into an active L2 table",
428 },
429 {
430 .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
431 .type = QEMU_OPT_BOOL,
432 .help = "Check for unintended writes into the refcount table",
433 },
434 {
435 .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
436 .type = QEMU_OPT_BOOL,
437 .help = "Check for unintended writes into a refcount block",
438 },
439 {
440 .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
441 .type = QEMU_OPT_BOOL,
442 .help = "Check for unintended writes into the snapshot table",
443 },
444 {
445 .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
446 .type = QEMU_OPT_BOOL,
447 .help = "Check for unintended writes into an inactive L1 table",
448 },
449 {
450 .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
451 .type = QEMU_OPT_BOOL,
452 .help = "Check for unintended writes into an inactive L2 table",
453 },
454 {
455 .name = QCOW2_OPT_CACHE_SIZE,
456 .type = QEMU_OPT_SIZE,
457 .help = "Maximum combined metadata (L2 tables and refcount blocks) "
458 "cache size",
459 },
460 {
461 .name = QCOW2_OPT_L2_CACHE_SIZE,
462 .type = QEMU_OPT_SIZE,
463 .help = "Maximum L2 table cache size",
464 },
465 {
466 .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
467 .type = QEMU_OPT_SIZE,
468 .help = "Maximum refcount block cache size",
469 },
470 { /* end of list */ }
471 },
472 };
473
474 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
475 [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER,
476 [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1,
477 [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2,
478 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
479 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
480 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
481 [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1,
482 [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2,
483 };
484
485 static void read_cache_sizes(QemuOpts *opts, uint64_t *l2_cache_size,
486 uint64_t *refcount_cache_size, Error **errp)
487 {
488 uint64_t combined_cache_size;
489 bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
490
491 combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
492 l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
493 refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
494
495 combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
496 *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0);
497 *refcount_cache_size = qemu_opt_get_size(opts,
498 QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
499
500 if (combined_cache_size_set) {
501 if (l2_cache_size_set && refcount_cache_size_set) {
502 error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
503 " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
504 "the same time");
505 return;
506 } else if (*l2_cache_size > combined_cache_size) {
507 error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
508 QCOW2_OPT_CACHE_SIZE);
509 return;
510 } else if (*refcount_cache_size > combined_cache_size) {
511 error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
512 QCOW2_OPT_CACHE_SIZE);
513 return;
514 }
515
516 if (l2_cache_size_set) {
517 *refcount_cache_size = combined_cache_size - *l2_cache_size;
518 } else if (refcount_cache_size_set) {
519 *l2_cache_size = combined_cache_size - *refcount_cache_size;
520 } else {
521 *refcount_cache_size = combined_cache_size
522 / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1);
523 *l2_cache_size = combined_cache_size - *refcount_cache_size;
524 }
525 } else {
526 if (!l2_cache_size_set && !refcount_cache_size_set) {
527 *l2_cache_size = DEFAULT_L2_CACHE_BYTE_SIZE;
528 *refcount_cache_size = *l2_cache_size
529 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
530 } else if (!l2_cache_size_set) {
531 *l2_cache_size = *refcount_cache_size
532 * DEFAULT_L2_REFCOUNT_SIZE_RATIO;
533 } else if (!refcount_cache_size_set) {
534 *refcount_cache_size = *l2_cache_size
535 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
536 }
537 }
538 }
539
540 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
541 Error **errp)
542 {
543 BDRVQcowState *s = bs->opaque;
544 unsigned int len, i;
545 int ret = 0;
546 QCowHeader header;
547 QemuOpts *opts = NULL;
548 Error *local_err = NULL;
549 uint64_t ext_end;
550 uint64_t l1_vm_state_index;
551 const char *opt_overlap_check, *opt_overlap_check_template;
552 int overlap_check_template = 0;
553 uint64_t l2_cache_size, refcount_cache_size;
554
555 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
556 if (ret < 0) {
557 error_setg_errno(errp, -ret, "Could not read qcow2 header");
558 goto fail;
559 }
560 be32_to_cpus(&header.magic);
561 be32_to_cpus(&header.version);
562 be64_to_cpus(&header.backing_file_offset);
563 be32_to_cpus(&header.backing_file_size);
564 be64_to_cpus(&header.size);
565 be32_to_cpus(&header.cluster_bits);
566 be32_to_cpus(&header.crypt_method);
567 be64_to_cpus(&header.l1_table_offset);
568 be32_to_cpus(&header.l1_size);
569 be64_to_cpus(&header.refcount_table_offset);
570 be32_to_cpus(&header.refcount_table_clusters);
571 be64_to_cpus(&header.snapshots_offset);
572 be32_to_cpus(&header.nb_snapshots);
573
574 if (header.magic != QCOW_MAGIC) {
575 error_setg(errp, "Image is not in qcow2 format");
576 ret = -EINVAL;
577 goto fail;
578 }
579 if (header.version < 2 || header.version > 3) {
580 report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version);
581 ret = -ENOTSUP;
582 goto fail;
583 }
584
585 s->qcow_version = header.version;
586
587 /* Initialise cluster size */
588 if (header.cluster_bits < MIN_CLUSTER_BITS ||
589 header.cluster_bits > MAX_CLUSTER_BITS) {
590 error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
591 header.cluster_bits);
592 ret = -EINVAL;
593 goto fail;
594 }
595
596 s->cluster_bits = header.cluster_bits;
597 s->cluster_size = 1 << s->cluster_bits;
598 s->cluster_sectors = 1 << (s->cluster_bits - 9);
599
600 /* Initialise version 3 header fields */
601 if (header.version == 2) {
602 header.incompatible_features = 0;
603 header.compatible_features = 0;
604 header.autoclear_features = 0;
605 header.refcount_order = 4;
606 header.header_length = 72;
607 } else {
608 be64_to_cpus(&header.incompatible_features);
609 be64_to_cpus(&header.compatible_features);
610 be64_to_cpus(&header.autoclear_features);
611 be32_to_cpus(&header.refcount_order);
612 be32_to_cpus(&header.header_length);
613
614 if (header.header_length < 104) {
615 error_setg(errp, "qcow2 header too short");
616 ret = -EINVAL;
617 goto fail;
618 }
619 }
620
621 if (header.header_length > s->cluster_size) {
622 error_setg(errp, "qcow2 header exceeds cluster size");
623 ret = -EINVAL;
624 goto fail;
625 }
626
627 if (header.header_length > sizeof(header)) {
628 s->unknown_header_fields_size = header.header_length - sizeof(header);
629 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
630 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
631 s->unknown_header_fields_size);
632 if (ret < 0) {
633 error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
634 "fields");
635 goto fail;
636 }
637 }
638
639 if (header.backing_file_offset > s->cluster_size) {
640 error_setg(errp, "Invalid backing file offset");
641 ret = -EINVAL;
642 goto fail;
643 }
644
645 if (header.backing_file_offset) {
646 ext_end = header.backing_file_offset;
647 } else {
648 ext_end = 1 << header.cluster_bits;
649 }
650
651 /* Handle feature bits */
652 s->incompatible_features = header.incompatible_features;
653 s->compatible_features = header.compatible_features;
654 s->autoclear_features = header.autoclear_features;
655
656 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
657 void *feature_table = NULL;
658 qcow2_read_extensions(bs, header.header_length, ext_end,
659 &feature_table, NULL);
660 report_unsupported_feature(bs, errp, feature_table,
661 s->incompatible_features &
662 ~QCOW2_INCOMPAT_MASK);
663 ret = -ENOTSUP;
664 g_free(feature_table);
665 goto fail;
666 }
667
668 if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
669 /* Corrupt images may not be written to unless they are being repaired
670 */
671 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
672 error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
673 "read/write");
674 ret = -EACCES;
675 goto fail;
676 }
677 }
678
679 /* Check support for various header values */
680 if (header.refcount_order != 4) {
681 report_unsupported(bs, errp, "%d bit reference counts",
682 1 << header.refcount_order);
683 ret = -ENOTSUP;
684 goto fail;
685 }
686 s->refcount_order = header.refcount_order;
687
688 if (header.crypt_method > QCOW_CRYPT_AES) {
689 error_setg(errp, "Unsupported encryption method: %" PRIu32,
690 header.crypt_method);
691 ret = -EINVAL;
692 goto fail;
693 }
694 s->crypt_method_header = header.crypt_method;
695 if (s->crypt_method_header) {
696 bs->encrypted = 1;
697 }
698
699 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
700 s->l2_size = 1 << s->l2_bits;
701 /* 2^(s->refcount_order - 3) is the refcount width in bytes */
702 s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
703 s->refcount_block_size = 1 << s->refcount_block_bits;
704 bs->total_sectors = header.size / 512;
705 s->csize_shift = (62 - (s->cluster_bits - 8));
706 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
707 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
708
709 s->refcount_table_offset = header.refcount_table_offset;
710 s->refcount_table_size =
711 header.refcount_table_clusters << (s->cluster_bits - 3);
712
713 if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
714 error_setg(errp, "Reference count table too large");
715 ret = -EINVAL;
716 goto fail;
717 }
718
719 ret = validate_table_offset(bs, s->refcount_table_offset,
720 s->refcount_table_size, sizeof(uint64_t));
721 if (ret < 0) {
722 error_setg(errp, "Invalid reference count table offset");
723 goto fail;
724 }
725
726 /* Snapshot table offset/length */
727 if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) {
728 error_setg(errp, "Too many snapshots");
729 ret = -EINVAL;
730 goto fail;
731 }
732
733 ret = validate_table_offset(bs, header.snapshots_offset,
734 header.nb_snapshots,
735 sizeof(QCowSnapshotHeader));
736 if (ret < 0) {
737 error_setg(errp, "Invalid snapshot table offset");
738 goto fail;
739 }
740
741 /* read the level 1 table */
742 if (header.l1_size > QCOW_MAX_L1_SIZE) {
743 error_setg(errp, "Active L1 table too large");
744 ret = -EFBIG;
745 goto fail;
746 }
747 s->l1_size = header.l1_size;
748
749 l1_vm_state_index = size_to_l1(s, header.size);
750 if (l1_vm_state_index > INT_MAX) {
751 error_setg(errp, "Image is too big");
752 ret = -EFBIG;
753 goto fail;
754 }
755 s->l1_vm_state_index = l1_vm_state_index;
756
757 /* the L1 table must contain at least enough entries to put
758 header.size bytes */
759 if (s->l1_size < s->l1_vm_state_index) {
760 error_setg(errp, "L1 table is too small");
761 ret = -EINVAL;
762 goto fail;
763 }
764
765 ret = validate_table_offset(bs, header.l1_table_offset,
766 header.l1_size, sizeof(uint64_t));
767 if (ret < 0) {
768 error_setg(errp, "Invalid L1 table offset");
769 goto fail;
770 }
771 s->l1_table_offset = header.l1_table_offset;
772
773
774 if (s->l1_size > 0) {
775 s->l1_table = qemu_try_blockalign(bs->file,
776 align_offset(s->l1_size * sizeof(uint64_t), 512));
777 if (s->l1_table == NULL) {
778 error_setg(errp, "Could not allocate L1 table");
779 ret = -ENOMEM;
780 goto fail;
781 }
782 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
783 s->l1_size * sizeof(uint64_t));
784 if (ret < 0) {
785 error_setg_errno(errp, -ret, "Could not read L1 table");
786 goto fail;
787 }
788 for(i = 0;i < s->l1_size; i++) {
789 be64_to_cpus(&s->l1_table[i]);
790 }
791 }
792
793 /* get L2 table/refcount block cache size from command line options */
794 opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
795 qemu_opts_absorb_qdict(opts, options, &local_err);
796 if (local_err) {
797 error_propagate(errp, local_err);
798 ret = -EINVAL;
799 goto fail;
800 }
801
802 read_cache_sizes(opts, &l2_cache_size, &refcount_cache_size, &local_err);
803 if (local_err) {
804 error_propagate(errp, local_err);
805 ret = -EINVAL;
806 goto fail;
807 }
808
809 l2_cache_size /= s->cluster_size;
810 if (l2_cache_size < MIN_L2_CACHE_SIZE) {
811 l2_cache_size = MIN_L2_CACHE_SIZE;
812 }
813 if (l2_cache_size > INT_MAX) {
814 error_setg(errp, "L2 cache size too big");
815 ret = -EINVAL;
816 goto fail;
817 }
818
819 refcount_cache_size /= s->cluster_size;
820 if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
821 refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
822 }
823 if (refcount_cache_size > INT_MAX) {
824 error_setg(errp, "Refcount cache size too big");
825 ret = -EINVAL;
826 goto fail;
827 }
828
829 /* alloc L2 table/refcount block cache */
830 s->l2_table_cache = qcow2_cache_create(bs, l2_cache_size);
831 s->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size);
832 if (s->l2_table_cache == NULL || s->refcount_block_cache == NULL) {
833 error_setg(errp, "Could not allocate metadata caches");
834 ret = -ENOMEM;
835 goto fail;
836 }
837
838 s->cluster_cache = g_malloc(s->cluster_size);
839 /* one more sector for decompressed data alignment */
840 s->cluster_data = qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
841 * s->cluster_size + 512);
842 if (s->cluster_data == NULL) {
843 error_setg(errp, "Could not allocate temporary cluster buffer");
844 ret = -ENOMEM;
845 goto fail;
846 }
847
848 s->cluster_cache_offset = -1;
849 s->flags = flags;
850
851 ret = qcow2_refcount_init(bs);
852 if (ret != 0) {
853 error_setg_errno(errp, -ret, "Could not initialize refcount handling");
854 goto fail;
855 }
856
857 QLIST_INIT(&s->cluster_allocs);
858 QTAILQ_INIT(&s->discards);
859
860 /* read qcow2 extensions */
861 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
862 &local_err)) {
863 error_propagate(errp, local_err);
864 ret = -EINVAL;
865 goto fail;
866 }
867
868 /* read the backing file name */
869 if (header.backing_file_offset != 0) {
870 len = header.backing_file_size;
871 if (len > MIN(1023, s->cluster_size - header.backing_file_offset)) {
872 error_setg(errp, "Backing file name too long");
873 ret = -EINVAL;
874 goto fail;
875 }
876 ret = bdrv_pread(bs->file, header.backing_file_offset,
877 bs->backing_file, len);
878 if (ret < 0) {
879 error_setg_errno(errp, -ret, "Could not read backing file name");
880 goto fail;
881 }
882 bs->backing_file[len] = '\0';
883 }
884
885 /* Internal snapshots */
886 s->snapshots_offset = header.snapshots_offset;
887 s->nb_snapshots = header.nb_snapshots;
888
889 ret = qcow2_read_snapshots(bs);
890 if (ret < 0) {
891 error_setg_errno(errp, -ret, "Could not read snapshots");
892 goto fail;
893 }
894
895 /* Clear unknown autoclear feature bits */
896 if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) {
897 s->autoclear_features = 0;
898 ret = qcow2_update_header(bs);
899 if (ret < 0) {
900 error_setg_errno(errp, -ret, "Could not update qcow2 header");
901 goto fail;
902 }
903 }
904
905 /* Initialise locks */
906 qemu_co_mutex_init(&s->lock);
907
908 /* Repair image if dirty */
909 if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only &&
910 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
911 BdrvCheckResult result = {0};
912
913 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
914 if (ret < 0) {
915 error_setg_errno(errp, -ret, "Could not repair dirty image");
916 goto fail;
917 }
918 }
919
920 /* Enable lazy_refcounts according to image and command line options */
921 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
922 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
923
924 s->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
925 s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
926 s->discard_passthrough[QCOW2_DISCARD_REQUEST] =
927 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
928 flags & BDRV_O_UNMAP);
929 s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
930 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
931 s->discard_passthrough[QCOW2_DISCARD_OTHER] =
932 qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
933
934 opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
935 opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
936 if (opt_overlap_check_template && opt_overlap_check &&
937 strcmp(opt_overlap_check_template, opt_overlap_check))
938 {
939 error_setg(errp, "Conflicting values for qcow2 options '"
940 QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
941 "' ('%s')", opt_overlap_check, opt_overlap_check_template);
942 ret = -EINVAL;
943 goto fail;
944 }
945 if (!opt_overlap_check) {
946 opt_overlap_check = opt_overlap_check_template ?: "cached";
947 }
948
949 if (!strcmp(opt_overlap_check, "none")) {
950 overlap_check_template = 0;
951 } else if (!strcmp(opt_overlap_check, "constant")) {
952 overlap_check_template = QCOW2_OL_CONSTANT;
953 } else if (!strcmp(opt_overlap_check, "cached")) {
954 overlap_check_template = QCOW2_OL_CACHED;
955 } else if (!strcmp(opt_overlap_check, "all")) {
956 overlap_check_template = QCOW2_OL_ALL;
957 } else {
958 error_setg(errp, "Unsupported value '%s' for qcow2 option "
959 "'overlap-check'. Allowed are either of the following: "
960 "none, constant, cached, all", opt_overlap_check);
961 ret = -EINVAL;
962 goto fail;
963 }
964
965 s->overlap_check = 0;
966 for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
967 /* overlap-check defines a template bitmask, but every flag may be
968 * overwritten through the associated boolean option */
969 s->overlap_check |=
970 qemu_opt_get_bool(opts, overlap_bool_option_names[i],
971 overlap_check_template & (1 << i)) << i;
972 }
973
974 qemu_opts_del(opts);
975 opts = NULL;
976
977 if (s->use_lazy_refcounts && s->qcow_version < 3) {
978 error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
979 "qemu 1.1 compatibility level");
980 ret = -EINVAL;
981 goto fail;
982 }
983
984 #ifdef DEBUG_ALLOC
985 {
986 BdrvCheckResult result = {0};
987 qcow2_check_refcounts(bs, &result, 0);
988 }
989 #endif
990 return ret;
991
992 fail:
993 qemu_opts_del(opts);
994 g_free(s->unknown_header_fields);
995 cleanup_unknown_header_ext(bs);
996 qcow2_free_snapshots(bs);
997 qcow2_refcount_close(bs);
998 qemu_vfree(s->l1_table);
999 /* else pre-write overlap checks in cache_destroy may crash */
1000 s->l1_table = NULL;
1001 if (s->l2_table_cache) {
1002 qcow2_cache_destroy(bs, s->l2_table_cache);
1003 }
1004 if (s->refcount_block_cache) {
1005 qcow2_cache_destroy(bs, s->refcount_block_cache);
1006 }
1007 g_free(s->cluster_cache);
1008 qemu_vfree(s->cluster_data);
1009 return ret;
1010 }
1011
1012 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1013 {
1014 BDRVQcowState *s = bs->opaque;
1015
1016 bs->bl.write_zeroes_alignment = s->cluster_sectors;
1017 }
1018
1019 static int qcow2_set_key(BlockDriverState *bs, const char *key)
1020 {
1021 BDRVQcowState *s = bs->opaque;
1022 uint8_t keybuf[16];
1023 int len, i;
1024
1025 memset(keybuf, 0, 16);
1026 len = strlen(key);
1027 if (len > 16)
1028 len = 16;
1029 /* XXX: we could compress the chars to 7 bits to increase
1030 entropy */
1031 for(i = 0;i < len;i++) {
1032 keybuf[i] = key[i];
1033 }
1034 s->crypt_method = s->crypt_method_header;
1035
1036 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
1037 return -1;
1038 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
1039 return -1;
1040 #if 0
1041 /* test */
1042 {
1043 uint8_t in[16];
1044 uint8_t out[16];
1045 uint8_t tmp[16];
1046 for(i=0;i<16;i++)
1047 in[i] = i;
1048 AES_encrypt(in, tmp, &s->aes_encrypt_key);
1049 AES_decrypt(tmp, out, &s->aes_decrypt_key);
1050 for(i = 0; i < 16; i++)
1051 printf(" %02x", tmp[i]);
1052 printf("\n");
1053 for(i = 0; i < 16; i++)
1054 printf(" %02x", out[i]);
1055 printf("\n");
1056 }
1057 #endif
1058 return 0;
1059 }
1060
1061 /* We have no actual commit/abort logic for qcow2, but we need to write out any
1062 * unwritten data if we reopen read-only. */
1063 static int qcow2_reopen_prepare(BDRVReopenState *state,
1064 BlockReopenQueue *queue, Error **errp)
1065 {
1066 int ret;
1067
1068 if ((state->flags & BDRV_O_RDWR) == 0) {
1069 ret = bdrv_flush(state->bs);
1070 if (ret < 0) {
1071 return ret;
1072 }
1073
1074 ret = qcow2_mark_clean(state->bs);
1075 if (ret < 0) {
1076 return ret;
1077 }
1078 }
1079
1080 return 0;
1081 }
1082
1083 static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
1084 int64_t sector_num, int nb_sectors, int *pnum)
1085 {
1086 BDRVQcowState *s = bs->opaque;
1087 uint64_t cluster_offset;
1088 int index_in_cluster, ret;
1089 int64_t status = 0;
1090
1091 *pnum = nb_sectors;
1092 qemu_co_mutex_lock(&s->lock);
1093 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
1094 qemu_co_mutex_unlock(&s->lock);
1095 if (ret < 0) {
1096 return ret;
1097 }
1098
1099 if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED &&
1100 !s->crypt_method) {
1101 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1102 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
1103 status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset;
1104 }
1105 if (ret == QCOW2_CLUSTER_ZERO) {
1106 status |= BDRV_BLOCK_ZERO;
1107 } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
1108 status |= BDRV_BLOCK_DATA;
1109 }
1110 return status;
1111 }
1112
1113 /* handle reading after the end of the backing file */
1114 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
1115 int64_t sector_num, int nb_sectors)
1116 {
1117 int n1;
1118 if ((sector_num + nb_sectors) <= bs->total_sectors)
1119 return nb_sectors;
1120 if (sector_num >= bs->total_sectors)
1121 n1 = 0;
1122 else
1123 n1 = bs->total_sectors - sector_num;
1124
1125 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
1126
1127 return n1;
1128 }
1129
1130 static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
1131 int remaining_sectors, QEMUIOVector *qiov)
1132 {
1133 BDRVQcowState *s = bs->opaque;
1134 int index_in_cluster, n1;
1135 int ret;
1136 int cur_nr_sectors; /* number of sectors in current iteration */
1137 uint64_t cluster_offset = 0;
1138 uint64_t bytes_done = 0;
1139 QEMUIOVector hd_qiov;
1140 uint8_t *cluster_data = NULL;
1141
1142 qemu_iovec_init(&hd_qiov, qiov->niov);
1143
1144 qemu_co_mutex_lock(&s->lock);
1145
1146 while (remaining_sectors != 0) {
1147
1148 /* prepare next request */
1149 cur_nr_sectors = remaining_sectors;
1150 if (s->crypt_method) {
1151 cur_nr_sectors = MIN(cur_nr_sectors,
1152 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1153 }
1154
1155 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
1156 &cur_nr_sectors, &cluster_offset);
1157 if (ret < 0) {
1158 goto fail;
1159 }
1160
1161 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1162
1163 qemu_iovec_reset(&hd_qiov);
1164 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1165 cur_nr_sectors * 512);
1166
1167 switch (ret) {
1168 case QCOW2_CLUSTER_UNALLOCATED:
1169
1170 if (bs->backing_hd) {
1171 /* read from the base image */
1172 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
1173 sector_num, cur_nr_sectors);
1174 if (n1 > 0) {
1175 QEMUIOVector local_qiov;
1176
1177 qemu_iovec_init(&local_qiov, hd_qiov.niov);
1178 qemu_iovec_concat(&local_qiov, &hd_qiov, 0,
1179 n1 * BDRV_SECTOR_SIZE);
1180
1181 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
1182 qemu_co_mutex_unlock(&s->lock);
1183 ret = bdrv_co_readv(bs->backing_hd, sector_num,
1184 n1, &local_qiov);
1185 qemu_co_mutex_lock(&s->lock);
1186
1187 qemu_iovec_destroy(&local_qiov);
1188
1189 if (ret < 0) {
1190 goto fail;
1191 }
1192 }
1193 } else {
1194 /* Note: in this case, no need to wait */
1195 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1196 }
1197 break;
1198
1199 case QCOW2_CLUSTER_ZERO:
1200 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
1201 break;
1202
1203 case QCOW2_CLUSTER_COMPRESSED:
1204 /* add AIO support for compressed blocks ? */
1205 ret = qcow2_decompress_cluster(bs, cluster_offset);
1206 if (ret < 0) {
1207 goto fail;
1208 }
1209
1210 qemu_iovec_from_buf(&hd_qiov, 0,
1211 s->cluster_cache + index_in_cluster * 512,
1212 512 * cur_nr_sectors);
1213 break;
1214
1215 case QCOW2_CLUSTER_NORMAL:
1216 if ((cluster_offset & 511) != 0) {
1217 ret = -EIO;
1218 goto fail;
1219 }
1220
1221 if (s->crypt_method) {
1222 /*
1223 * For encrypted images, read everything into a temporary
1224 * contiguous buffer on which the AES functions can work.
1225 */
1226 if (!cluster_data) {
1227 cluster_data =
1228 qemu_try_blockalign(bs->file, QCOW_MAX_CRYPT_CLUSTERS
1229 * s->cluster_size);
1230 if (cluster_data == NULL) {
1231 ret = -ENOMEM;
1232 goto fail;
1233 }
1234 }
1235
1236 assert(cur_nr_sectors <=
1237 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
1238 qemu_iovec_reset(&hd_qiov);
1239 qemu_iovec_add(&hd_qiov, cluster_data,
1240 512 * cur_nr_sectors);
1241 }
1242
1243 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
1244 qemu_co_mutex_unlock(&s->lock);
1245 ret = bdrv_co_readv(bs->file,
1246 (cluster_offset >> 9) + index_in_cluster,
1247 cur_nr_sectors, &hd_qiov);
1248 qemu_co_mutex_lock(&s->lock);
1249 if (ret < 0) {
1250 goto fail;
1251 }
1252 if (s->crypt_method) {
1253 qcow2_encrypt_sectors(s, sector_num, cluster_data,
1254 cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
1255 qemu_iovec_from_buf(qiov, bytes_done,
1256 cluster_data, 512 * cur_nr_sectors);
1257 }
1258 break;
1259
1260 default:
1261 g_assert_not_reached();
1262 ret = -EIO;
1263 goto fail;
1264 }
1265
1266 remaining_sectors -= cur_nr_sectors;
1267 sector_num += cur_nr_sectors;
1268 bytes_done += cur_nr_sectors * 512;
1269 }
1270 ret = 0;
1271
1272 fail:
1273 qemu_co_mutex_unlock(&s->lock);
1274
1275 qemu_iovec_destroy(&hd_qiov);
1276 qemu_vfree(cluster_data);
1277
1278 return ret;
1279 }
1280
1281 static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
1282 int64_t sector_num,
1283 int remaining_sectors,
1284 QEMUIOVector *qiov)
1285 {
1286 BDRVQcowState *s = bs->opaque;
1287 int index_in_cluster;
1288 int ret;
1289 int cur_nr_sectors; /* number of sectors in current iteration */
1290 uint64_t cluster_offset;
1291 QEMUIOVector hd_qiov;
1292 uint64_t bytes_done = 0;
1293 uint8_t *cluster_data = NULL;
1294 QCowL2Meta *l2meta = NULL;
1295
1296 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
1297 remaining_sectors);
1298
1299 qemu_iovec_init(&hd_qiov, qiov->niov);
1300
1301 s->cluster_cache_offset = -1; /* disable compressed cache */
1302
1303 qemu_co_mutex_lock(&s->lock);
1304
1305 while (remaining_sectors != 0) {
1306
1307 l2meta = NULL;
1308
1309 trace_qcow2_writev_start_part(qemu_coroutine_self());
1310 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1311 cur_nr_sectors = remaining_sectors;
1312 if (s->crypt_method &&
1313 cur_nr_sectors >
1314 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
1315 cur_nr_sectors =
1316 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster;
1317 }
1318
1319 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
1320 &cur_nr_sectors, &cluster_offset, &l2meta);
1321 if (ret < 0) {
1322 goto fail;
1323 }
1324
1325 assert((cluster_offset & 511) == 0);
1326
1327 qemu_iovec_reset(&hd_qiov);
1328 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
1329 cur_nr_sectors * 512);
1330
1331 if (s->crypt_method) {
1332 if (!cluster_data) {
1333 cluster_data = qemu_try_blockalign(bs->file,
1334 QCOW_MAX_CRYPT_CLUSTERS
1335 * s->cluster_size);
1336 if (cluster_data == NULL) {
1337 ret = -ENOMEM;
1338 goto fail;
1339 }
1340 }
1341
1342 assert(hd_qiov.size <=
1343 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
1344 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
1345
1346 qcow2_encrypt_sectors(s, sector_num, cluster_data,
1347 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
1348
1349 qemu_iovec_reset(&hd_qiov);
1350 qemu_iovec_add(&hd_qiov, cluster_data,
1351 cur_nr_sectors * 512);
1352 }
1353
1354 ret = qcow2_pre_write_overlap_check(bs, 0,
1355 cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE,
1356 cur_nr_sectors * BDRV_SECTOR_SIZE);
1357 if (ret < 0) {
1358 goto fail;
1359 }
1360
1361 qemu_co_mutex_unlock(&s->lock);
1362 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
1363 trace_qcow2_writev_data(qemu_coroutine_self(),
1364 (cluster_offset >> 9) + index_in_cluster);
1365 ret = bdrv_co_writev(bs->file,
1366 (cluster_offset >> 9) + index_in_cluster,
1367 cur_nr_sectors, &hd_qiov);
1368 qemu_co_mutex_lock(&s->lock);
1369 if (ret < 0) {
1370 goto fail;
1371 }
1372
1373 while (l2meta != NULL) {
1374 QCowL2Meta *next;
1375
1376 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
1377 if (ret < 0) {
1378 goto fail;
1379 }
1380
1381 /* Take the request off the list of running requests */
1382 if (l2meta->nb_clusters != 0) {
1383 QLIST_REMOVE(l2meta, next_in_flight);
1384 }
1385
1386 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1387
1388 next = l2meta->next;
1389 g_free(l2meta);
1390 l2meta = next;
1391 }
1392
1393 remaining_sectors -= cur_nr_sectors;
1394 sector_num += cur_nr_sectors;
1395 bytes_done += cur_nr_sectors * 512;
1396 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
1397 }
1398 ret = 0;
1399
1400 fail:
1401 qemu_co_mutex_unlock(&s->lock);
1402
1403 while (l2meta != NULL) {
1404 QCowL2Meta *next;
1405
1406 if (l2meta->nb_clusters != 0) {
1407 QLIST_REMOVE(l2meta, next_in_flight);
1408 }
1409 qemu_co_queue_restart_all(&l2meta->dependent_requests);
1410
1411 next = l2meta->next;
1412 g_free(l2meta);
1413 l2meta = next;
1414 }
1415
1416 qemu_iovec_destroy(&hd_qiov);
1417 qemu_vfree(cluster_data);
1418 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
1419
1420 return ret;
1421 }
1422
1423 static void qcow2_close(BlockDriverState *bs)
1424 {
1425 BDRVQcowState *s = bs->opaque;
1426 qemu_vfree(s->l1_table);
1427 /* else pre-write overlap checks in cache_destroy may crash */
1428 s->l1_table = NULL;
1429
1430 if (!(bs->open_flags & BDRV_O_INCOMING)) {
1431 int ret1, ret2;
1432
1433 ret1 = qcow2_cache_flush(bs, s->l2_table_cache);
1434 ret2 = qcow2_cache_flush(bs, s->refcount_block_cache);
1435
1436 if (ret1) {
1437 error_report("Failed to flush the L2 table cache: %s",
1438 strerror(-ret1));
1439 }
1440 if (ret2) {
1441 error_report("Failed to flush the refcount block cache: %s",
1442 strerror(-ret2));
1443 }
1444
1445 if (!ret1 && !ret2) {
1446 qcow2_mark_clean(bs);
1447 }
1448 }
1449
1450 qcow2_cache_destroy(bs, s->l2_table_cache);
1451 qcow2_cache_destroy(bs, s->refcount_block_cache);
1452
1453 g_free(s->unknown_header_fields);
1454 cleanup_unknown_header_ext(bs);
1455
1456 g_free(s->cluster_cache);
1457 qemu_vfree(s->cluster_data);
1458 qcow2_refcount_close(bs);
1459 qcow2_free_snapshots(bs);
1460 }
1461
1462 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
1463 {
1464 BDRVQcowState *s = bs->opaque;
1465 int flags = s->flags;
1466 AES_KEY aes_encrypt_key;
1467 AES_KEY aes_decrypt_key;
1468 uint32_t crypt_method = 0;
1469 QDict *options;
1470 Error *local_err = NULL;
1471 int ret;
1472
1473 /*
1474 * Backing files are read-only which makes all of their metadata immutable,
1475 * that means we don't have to worry about reopening them here.
1476 */
1477
1478 if (s->crypt_method) {
1479 crypt_method = s->crypt_method;
1480 memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
1481 memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
1482 }
1483
1484 qcow2_close(bs);
1485
1486 bdrv_invalidate_cache(bs->file, &local_err);
1487 if (local_err) {
1488 error_propagate(errp, local_err);
1489 return;
1490 }
1491
1492 memset(s, 0, sizeof(BDRVQcowState));
1493 options = qdict_clone_shallow(bs->options);
1494
1495 ret = qcow2_open(bs, options, flags, &local_err);
1496 QDECREF(options);
1497 if (local_err) {
1498 error_setg(errp, "Could not reopen qcow2 layer: %s",
1499 error_get_pretty(local_err));
1500 error_free(local_err);
1501 return;
1502 } else if (ret < 0) {
1503 error_setg_errno(errp, -ret, "Could not reopen qcow2 layer");
1504 return;
1505 }
1506
1507 if (crypt_method) {
1508 s->crypt_method = crypt_method;
1509 memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
1510 memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
1511 }
1512 }
1513
1514 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
1515 size_t len, size_t buflen)
1516 {
1517 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
1518 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
1519
1520 if (buflen < ext_len) {
1521 return -ENOSPC;
1522 }
1523
1524 *ext_backing_fmt = (QCowExtension) {
1525 .magic = cpu_to_be32(magic),
1526 .len = cpu_to_be32(len),
1527 };
1528 memcpy(buf + sizeof(QCowExtension), s, len);
1529
1530 return ext_len;
1531 }
1532
1533 /*
1534 * Updates the qcow2 header, including the variable length parts of it, i.e.
1535 * the backing file name and all extensions. qcow2 was not designed to allow
1536 * such changes, so if we run out of space (we can only use the first cluster)
1537 * this function may fail.
1538 *
1539 * Returns 0 on success, -errno in error cases.
1540 */
1541 int qcow2_update_header(BlockDriverState *bs)
1542 {
1543 BDRVQcowState *s = bs->opaque;
1544 QCowHeader *header;
1545 char *buf;
1546 size_t buflen = s->cluster_size;
1547 int ret;
1548 uint64_t total_size;
1549 uint32_t refcount_table_clusters;
1550 size_t header_length;
1551 Qcow2UnknownHeaderExtension *uext;
1552
1553 buf = qemu_blockalign(bs, buflen);
1554
1555 /* Header structure */
1556 header = (QCowHeader*) buf;
1557
1558 if (buflen < sizeof(*header)) {
1559 ret = -ENOSPC;
1560 goto fail;
1561 }
1562
1563 header_length = sizeof(*header) + s->unknown_header_fields_size;
1564 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1565 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1566
1567 *header = (QCowHeader) {
1568 /* Version 2 fields */
1569 .magic = cpu_to_be32(QCOW_MAGIC),
1570 .version = cpu_to_be32(s->qcow_version),
1571 .backing_file_offset = 0,
1572 .backing_file_size = 0,
1573 .cluster_bits = cpu_to_be32(s->cluster_bits),
1574 .size = cpu_to_be64(total_size),
1575 .crypt_method = cpu_to_be32(s->crypt_method_header),
1576 .l1_size = cpu_to_be32(s->l1_size),
1577 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1578 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1579 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1580 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1581 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
1582
1583 /* Version 3 fields */
1584 .incompatible_features = cpu_to_be64(s->incompatible_features),
1585 .compatible_features = cpu_to_be64(s->compatible_features),
1586 .autoclear_features = cpu_to_be64(s->autoclear_features),
1587 .refcount_order = cpu_to_be32(s->refcount_order),
1588 .header_length = cpu_to_be32(header_length),
1589 };
1590
1591 /* For older versions, write a shorter header */
1592 switch (s->qcow_version) {
1593 case 2:
1594 ret = offsetof(QCowHeader, incompatible_features);
1595 break;
1596 case 3:
1597 ret = sizeof(*header);
1598 break;
1599 default:
1600 ret = -EINVAL;
1601 goto fail;
1602 }
1603
1604 buf += ret;
1605 buflen -= ret;
1606 memset(buf, 0, buflen);
1607
1608 /* Preserve any unknown field in the header */
1609 if (s->unknown_header_fields_size) {
1610 if (buflen < s->unknown_header_fields_size) {
1611 ret = -ENOSPC;
1612 goto fail;
1613 }
1614
1615 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1616 buf += s->unknown_header_fields_size;
1617 buflen -= s->unknown_header_fields_size;
1618 }
1619
1620 /* Backing file format header extension */
1621 if (*bs->backing_format) {
1622 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1623 bs->backing_format, strlen(bs->backing_format),
1624 buflen);
1625 if (ret < 0) {
1626 goto fail;
1627 }
1628
1629 buf += ret;
1630 buflen -= ret;
1631 }
1632
1633 /* Feature table */
1634 Qcow2Feature features[] = {
1635 {
1636 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1637 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1638 .name = "dirty bit",
1639 },
1640 {
1641 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1642 .bit = QCOW2_INCOMPAT_CORRUPT_BITNR,
1643 .name = "corrupt bit",
1644 },
1645 {
1646 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1647 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1648 .name = "lazy refcounts",
1649 },
1650 };
1651
1652 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1653 features, sizeof(features), buflen);
1654 if (ret < 0) {
1655 goto fail;
1656 }
1657 buf += ret;
1658 buflen -= ret;
1659
1660 /* Keep unknown header extensions */
1661 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1662 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1663 if (ret < 0) {
1664 goto fail;
1665 }
1666
1667 buf += ret;
1668 buflen -= ret;
1669 }
1670
1671 /* End of header extensions */
1672 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1673 if (ret < 0) {
1674 goto fail;
1675 }
1676
1677 buf += ret;
1678 buflen -= ret;
1679
1680 /* Backing file name */
1681 if (*bs->backing_file) {
1682 size_t backing_file_len = strlen(bs->backing_file);
1683
1684 if (buflen < backing_file_len) {
1685 ret = -ENOSPC;
1686 goto fail;
1687 }
1688
1689 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1690 strncpy(buf, bs->backing_file, buflen);
1691
1692 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1693 header->backing_file_size = cpu_to_be32(backing_file_len);
1694 }
1695
1696 /* Write the new header */
1697 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1698 if (ret < 0) {
1699 goto fail;
1700 }
1701
1702 ret = 0;
1703 fail:
1704 qemu_vfree(header);
1705 return ret;
1706 }
1707
1708 static int qcow2_change_backing_file(BlockDriverState *bs,
1709 const char *backing_file, const char *backing_fmt)
1710 {
1711 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1712 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1713
1714 return qcow2_update_header(bs);
1715 }
1716
1717 static int preallocate(BlockDriverState *bs)
1718 {
1719 uint64_t nb_sectors;
1720 uint64_t offset;
1721 uint64_t host_offset = 0;
1722 int num;
1723 int ret;
1724 QCowL2Meta *meta;
1725
1726 nb_sectors = bdrv_nb_sectors(bs);
1727 offset = 0;
1728
1729 while (nb_sectors) {
1730 num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS);
1731 ret = qcow2_alloc_cluster_offset(bs, offset, &num,
1732 &host_offset, &meta);
1733 if (ret < 0) {
1734 return ret;
1735 }
1736
1737 while (meta) {
1738 QCowL2Meta *next = meta->next;
1739
1740 ret = qcow2_alloc_cluster_link_l2(bs, meta);
1741 if (ret < 0) {
1742 qcow2_free_any_clusters(bs, meta->alloc_offset,
1743 meta->nb_clusters, QCOW2_DISCARD_NEVER);
1744 return ret;
1745 }
1746
1747 /* There are no dependent requests, but we need to remove our
1748 * request from the list of in-flight requests */
1749 QLIST_REMOVE(meta, next_in_flight);
1750
1751 g_free(meta);
1752 meta = next;
1753 }
1754
1755 /* TODO Preallocate data if requested */
1756
1757 nb_sectors -= num;
1758 offset += num << BDRV_SECTOR_BITS;
1759 }
1760
1761 /*
1762 * It is expected that the image file is large enough to actually contain
1763 * all of the allocated clusters (otherwise we get failing reads after
1764 * EOF). Extend the image to the last allocated sector.
1765 */
1766 if (host_offset != 0) {
1767 uint8_t buf[BDRV_SECTOR_SIZE];
1768 memset(buf, 0, BDRV_SECTOR_SIZE);
1769 ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1,
1770 buf, 1);
1771 if (ret < 0) {
1772 return ret;
1773 }
1774 }
1775
1776 return 0;
1777 }
1778
1779 static int qcow2_create2(const char *filename, int64_t total_size,
1780 const char *backing_file, const char *backing_format,
1781 int flags, size_t cluster_size, PreallocMode prealloc,
1782 QemuOpts *opts, int version,
1783 Error **errp)
1784 {
1785 /* Calculate cluster_bits */
1786 int cluster_bits;
1787 cluster_bits = ffs(cluster_size) - 1;
1788 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1789 (1 << cluster_bits) != cluster_size)
1790 {
1791 error_setg(errp, "Cluster size must be a power of two between %d and "
1792 "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1793 return -EINVAL;
1794 }
1795
1796 /*
1797 * Open the image file and write a minimal qcow2 header.
1798 *
1799 * We keep things simple and start with a zero-sized image. We also
1800 * do without refcount blocks or a L1 table for now. We'll fix the
1801 * inconsistency later.
1802 *
1803 * We do need a refcount table because growing the refcount table means
1804 * allocating two new refcount blocks - the seconds of which would be at
1805 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1806 * size for any qcow2 image.
1807 */
1808 BlockDriverState* bs;
1809 QCowHeader *header;
1810 uint64_t* refcount_table;
1811 Error *local_err = NULL;
1812 int ret;
1813
1814 if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
1815 int64_t meta_size = 0;
1816 uint64_t nreftablee, nrefblocke, nl1e, nl2e;
1817 int64_t aligned_total_size = align_offset(total_size, cluster_size);
1818
1819 /* header: 1 cluster */
1820 meta_size += cluster_size;
1821
1822 /* total size of L2 tables */
1823 nl2e = aligned_total_size / cluster_size;
1824 nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t));
1825 meta_size += nl2e * sizeof(uint64_t);
1826
1827 /* total size of L1 tables */
1828 nl1e = nl2e * sizeof(uint64_t) / cluster_size;
1829 nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t));
1830 meta_size += nl1e * sizeof(uint64_t);
1831
1832 /* total size of refcount blocks
1833 *
1834 * note: every host cluster is reference-counted, including metadata
1835 * (even refcount blocks are recursively included).
1836 * Let:
1837 * a = total_size (this is the guest disk size)
1838 * m = meta size not including refcount blocks and refcount tables
1839 * c = cluster size
1840 * y1 = number of refcount blocks entries
1841 * y2 = meta size including everything
1842 * then,
1843 * y1 = (y2 + a)/c
1844 * y2 = y1 * sizeof(u16) + y1 * sizeof(u16) * sizeof(u64) / c + m
1845 * we can get y1:
1846 * y1 = (a + m) / (c - sizeof(u16) - sizeof(u16) * sizeof(u64) / c)
1847 */
1848 nrefblocke = (aligned_total_size + meta_size + cluster_size) /
1849 (cluster_size - sizeof(uint16_t) -
1850 1.0 * sizeof(uint16_t) * sizeof(uint64_t) / cluster_size);
1851 nrefblocke = align_offset(nrefblocke, cluster_size / sizeof(uint16_t));
1852 meta_size += nrefblocke * sizeof(uint16_t);
1853
1854 /* total size of refcount tables */
1855 nreftablee = nrefblocke * sizeof(uint16_t) / cluster_size;
1856 nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t));
1857 meta_size += nreftablee * sizeof(uint64_t);
1858
1859 qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
1860 aligned_total_size + meta_size);
1861 qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc]);
1862 }
1863
1864 ret = bdrv_create_file(filename, opts, &local_err);
1865 if (ret < 0) {
1866 error_propagate(errp, local_err);
1867 return ret;
1868 }
1869
1870 bs = NULL;
1871 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
1872 NULL, &local_err);
1873 if (ret < 0) {
1874 error_propagate(errp, local_err);
1875 return ret;
1876 }
1877
1878 /* Write the header */
1879 QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
1880 header = g_malloc0(cluster_size);
1881 *header = (QCowHeader) {
1882 .magic = cpu_to_be32(QCOW_MAGIC),
1883 .version = cpu_to_be32(version),
1884 .cluster_bits = cpu_to_be32(cluster_bits),
1885 .size = cpu_to_be64(0),
1886 .l1_table_offset = cpu_to_be64(0),
1887 .l1_size = cpu_to_be32(0),
1888 .refcount_table_offset = cpu_to_be64(cluster_size),
1889 .refcount_table_clusters = cpu_to_be32(1),
1890 .refcount_order = cpu_to_be32(4),
1891 .header_length = cpu_to_be32(sizeof(*header)),
1892 };
1893
1894 if (flags & BLOCK_FLAG_ENCRYPT) {
1895 header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1896 } else {
1897 header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1898 }
1899
1900 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1901 header->compatible_features |=
1902 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1903 }
1904
1905 ret = bdrv_pwrite(bs, 0, header, cluster_size);
1906 g_free(header);
1907 if (ret < 0) {
1908 error_setg_errno(errp, -ret, "Could not write qcow2 header");
1909 goto out;
1910 }
1911
1912 /* Write a refcount table with one refcount block */
1913 refcount_table = g_malloc0(2 * cluster_size);
1914 refcount_table[0] = cpu_to_be64(2 * cluster_size);
1915 ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size);
1916 g_free(refcount_table);
1917
1918 if (ret < 0) {
1919 error_setg_errno(errp, -ret, "Could not write refcount table");
1920 goto out;
1921 }
1922
1923 bdrv_unref(bs);
1924 bs = NULL;
1925
1926 /*
1927 * And now open the image and make it consistent first (i.e. increase the
1928 * refcount of the cluster that is occupied by the header and the refcount
1929 * table)
1930 */
1931 ret = bdrv_open(&bs, filename, NULL, NULL,
1932 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
1933 &bdrv_qcow2, &local_err);
1934 if (ret < 0) {
1935 error_propagate(errp, local_err);
1936 goto out;
1937 }
1938
1939 ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
1940 if (ret < 0) {
1941 error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
1942 "header and refcount table");
1943 goto out;
1944
1945 } else if (ret != 0) {
1946 error_report("Huh, first cluster in empty image is already in use?");
1947 abort();
1948 }
1949
1950 /* Okay, now that we have a valid image, let's give it the right size */
1951 ret = bdrv_truncate(bs, total_size);
1952 if (ret < 0) {
1953 error_setg_errno(errp, -ret, "Could not resize image");
1954 goto out;
1955 }
1956
1957 /* Want a backing file? There you go.*/
1958 if (backing_file) {
1959 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1960 if (ret < 0) {
1961 error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
1962 "with format '%s'", backing_file, backing_format);
1963 goto out;
1964 }
1965 }
1966
1967 /* And if we're supposed to preallocate metadata, do that now */
1968 if (prealloc != PREALLOC_MODE_OFF) {
1969 BDRVQcowState *s = bs->opaque;
1970 qemu_co_mutex_lock(&s->lock);
1971 ret = preallocate(bs);
1972 qemu_co_mutex_unlock(&s->lock);
1973 if (ret < 0) {
1974 error_setg_errno(errp, -ret, "Could not preallocate metadata");
1975 goto out;
1976 }
1977 }
1978
1979 bdrv_unref(bs);
1980 bs = NULL;
1981
1982 /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
1983 ret = bdrv_open(&bs, filename, NULL, NULL,
1984 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
1985 &bdrv_qcow2, &local_err);
1986 if (local_err) {
1987 error_propagate(errp, local_err);
1988 goto out;
1989 }
1990
1991 ret = 0;
1992 out:
1993 if (bs) {
1994 bdrv_unref(bs);
1995 }
1996 return ret;
1997 }
1998
1999 static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
2000 {
2001 char *backing_file = NULL;
2002 char *backing_fmt = NULL;
2003 char *buf = NULL;
2004 uint64_t size = 0;
2005 int flags = 0;
2006 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
2007 PreallocMode prealloc;
2008 int version = 3;
2009 Error *local_err = NULL;
2010 int ret;
2011
2012 /* Read out options */
2013 size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2014 BDRV_SECTOR_SIZE);
2015 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
2016 backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
2017 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
2018 flags |= BLOCK_FLAG_ENCRYPT;
2019 }
2020 cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
2021 DEFAULT_CLUSTER_SIZE);
2022 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
2023 prealloc = qapi_enum_parse(PreallocMode_lookup, buf,
2024 PREALLOC_MODE_MAX, PREALLOC_MODE_OFF,
2025 &local_err);
2026 if (local_err) {
2027 error_propagate(errp, local_err);
2028 ret = -EINVAL;
2029 goto finish;
2030 }
2031 g_free(buf);
2032 buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
2033 if (!buf) {
2034 /* keep the default */
2035 } else if (!strcmp(buf, "0.10")) {
2036 version = 2;
2037 } else if (!strcmp(buf, "1.1")) {
2038 version = 3;
2039 } else {
2040 error_setg(errp, "Invalid compatibility level: '%s'", buf);
2041 ret = -EINVAL;
2042 goto finish;
2043 }
2044
2045 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) {
2046 flags |= BLOCK_FLAG_LAZY_REFCOUNTS;
2047 }
2048
2049 if (backing_file && prealloc != PREALLOC_MODE_OFF) {
2050 error_setg(errp, "Backing file and preallocation cannot be used at "
2051 "the same time");
2052 ret = -EINVAL;
2053 goto finish;
2054 }
2055
2056 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
2057 error_setg(errp, "Lazy refcounts only supported with compatibility "
2058 "level 1.1 and above (use compat=1.1 or greater)");
2059 ret = -EINVAL;
2060 goto finish;
2061 }
2062
2063 ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
2064 cluster_size, prealloc, opts, version, &local_err);
2065 if (local_err) {
2066 error_propagate(errp, local_err);
2067 }
2068
2069 finish:
2070 g_free(backing_file);
2071 g_free(backing_fmt);
2072 g_free(buf);
2073 return ret;
2074 }
2075
2076 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
2077 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
2078 {
2079 int ret;
2080 BDRVQcowState *s = bs->opaque;
2081
2082 /* Emulate misaligned zero writes */
2083 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
2084 return -ENOTSUP;
2085 }
2086
2087 /* Whatever is left can use real zero clusters */
2088 qemu_co_mutex_lock(&s->lock);
2089 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2090 nb_sectors);
2091 qemu_co_mutex_unlock(&s->lock);
2092
2093 return ret;
2094 }
2095
2096 static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
2097 int64_t sector_num, int nb_sectors)
2098 {
2099 int ret;
2100 BDRVQcowState *s = bs->opaque;
2101
2102 qemu_co_mutex_lock(&s->lock);
2103 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
2104 nb_sectors, QCOW2_DISCARD_REQUEST, false);
2105 qemu_co_mutex_unlock(&s->lock);
2106 return ret;
2107 }
2108
2109 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
2110 {
2111 BDRVQcowState *s = bs->opaque;
2112 int64_t new_l1_size;
2113 int ret;
2114
2115 if (offset & 511) {
2116 error_report("The new size must be a multiple of 512");
2117 return -EINVAL;
2118 }
2119
2120 /* cannot proceed if image has snapshots */
2121 if (s->nb_snapshots) {
2122 error_report("Can't resize an image which has snapshots");
2123 return -ENOTSUP;
2124 }
2125
2126 /* shrinking is currently not supported */
2127 if (offset < bs->total_sectors * 512) {
2128 error_report("qcow2 doesn't support shrinking images yet");
2129 return -ENOTSUP;
2130 }
2131
2132 new_l1_size = size_to_l1(s, offset);
2133 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
2134 if (ret < 0) {
2135 return ret;
2136 }
2137
2138 /* write updated header.size */
2139 offset = cpu_to_be64(offset);
2140 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
2141 &offset, sizeof(uint64_t));
2142 if (ret < 0) {
2143 return ret;
2144 }
2145
2146 s->l1_vm_state_index = new_l1_size;
2147 return 0;
2148 }
2149
2150 /* XXX: put compressed sectors first, then all the cluster aligned
2151 tables to avoid losing bytes in alignment */
2152 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
2153 const uint8_t *buf, int nb_sectors)
2154 {
2155 BDRVQcowState *s = bs->opaque;
2156 z_stream strm;
2157 int ret, out_len;
2158 uint8_t *out_buf;
2159 uint64_t cluster_offset;
2160
2161 if (nb_sectors == 0) {
2162 /* align end of file to a sector boundary to ease reading with
2163 sector based I/Os */
2164 cluster_offset = bdrv_getlength(bs->file);
2165 return bdrv_truncate(bs->file, cluster_offset);
2166 }
2167
2168 if (nb_sectors != s->cluster_sectors) {
2169 ret = -EINVAL;
2170
2171 /* Zero-pad last write if image size is not cluster aligned */
2172 if (sector_num + nb_sectors == bs->total_sectors &&
2173 nb_sectors < s->cluster_sectors) {
2174 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
2175 memset(pad_buf, 0, s->cluster_size);
2176 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
2177 ret = qcow2_write_compressed(bs, sector_num,
2178 pad_buf, s->cluster_sectors);
2179 qemu_vfree(pad_buf);
2180 }
2181 return ret;
2182 }
2183
2184 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
2185
2186 /* best compression, small window, no zlib header */
2187 memset(&strm, 0, sizeof(strm));
2188 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
2189 Z_DEFLATED, -12,
2190 9, Z_DEFAULT_STRATEGY);
2191 if (ret != 0) {
2192 ret = -EINVAL;
2193 goto fail;
2194 }
2195
2196 strm.avail_in = s->cluster_size;
2197 strm.next_in = (uint8_t *)buf;
2198 strm.avail_out = s->cluster_size;
2199 strm.next_out = out_buf;
2200
2201 ret = deflate(&strm, Z_FINISH);
2202 if (ret != Z_STREAM_END && ret != Z_OK) {
2203 deflateEnd(&strm);
2204 ret = -EINVAL;
2205 goto fail;
2206 }
2207 out_len = strm.next_out - out_buf;
2208
2209 deflateEnd(&strm);
2210
2211 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
2212 /* could not compress: write normal cluster */
2213 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
2214 if (ret < 0) {
2215 goto fail;
2216 }
2217 } else {
2218 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
2219 sector_num << 9, out_len);
2220 if (!cluster_offset) {
2221 ret = -EIO;
2222 goto fail;
2223 }
2224 cluster_offset &= s->cluster_offset_mask;
2225
2226 ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len);
2227 if (ret < 0) {
2228 goto fail;
2229 }
2230
2231 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
2232 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
2233 if (ret < 0) {
2234 goto fail;
2235 }
2236 }
2237
2238 ret = 0;
2239 fail:
2240 g_free(out_buf);
2241 return ret;
2242 }
2243
2244 static int make_completely_empty(BlockDriverState *bs)
2245 {
2246 BDRVQcowState *s = bs->opaque;
2247 int ret, l1_clusters;
2248 int64_t offset;
2249 uint64_t *new_reftable = NULL;
2250 uint64_t rt_entry, l1_size2;
2251 struct {
2252 uint64_t l1_offset;
2253 uint64_t reftable_offset;
2254 uint32_t reftable_clusters;
2255 } QEMU_PACKED l1_ofs_rt_ofs_cls;
2256
2257 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2258 if (ret < 0) {
2259 goto fail;
2260 }
2261
2262 ret = qcow2_cache_empty(bs, s->refcount_block_cache);
2263 if (ret < 0) {
2264 goto fail;
2265 }
2266
2267 /* Refcounts will be broken utterly */
2268 ret = qcow2_mark_dirty(bs);
2269 if (ret < 0) {
2270 goto fail;
2271 }
2272
2273 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2274
2275 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2276 l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t);
2277
2278 /* After this call, neither the in-memory nor the on-disk refcount
2279 * information accurately describe the actual references */
2280
2281 ret = bdrv_write_zeroes(bs->file, s->l1_table_offset / BDRV_SECTOR_SIZE,
2282 l1_clusters * s->cluster_sectors, 0);
2283 if (ret < 0) {
2284 goto fail_broken_refcounts;
2285 }
2286 memset(s->l1_table, 0, l1_size2);
2287
2288 BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
2289
2290 /* Overwrite enough clusters at the beginning of the sectors to place
2291 * the refcount table, a refcount block and the L1 table in; this may
2292 * overwrite parts of the existing refcount and L1 table, which is not
2293 * an issue because the dirty flag is set, complete data loss is in fact
2294 * desired and partial data loss is consequently fine as well */
2295 ret = bdrv_write_zeroes(bs->file, s->cluster_size / BDRV_SECTOR_SIZE,
2296 (2 + l1_clusters) * s->cluster_size /
2297 BDRV_SECTOR_SIZE, 0);
2298 /* This call (even if it failed overall) may have overwritten on-disk
2299 * refcount structures; in that case, the in-memory refcount information
2300 * will probably differ from the on-disk information which makes the BDS
2301 * unusable */
2302 if (ret < 0) {
2303 goto fail_broken_refcounts;
2304 }
2305
2306 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
2307 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
2308
2309 /* "Create" an empty reftable (one cluster) directly after the image
2310 * header and an empty L1 table three clusters after the image header;
2311 * the cluster between those two will be used as the first refblock */
2312 cpu_to_be64w(&l1_ofs_rt_ofs_cls.l1_offset, 3 * s->cluster_size);
2313 cpu_to_be64w(&l1_ofs_rt_ofs_cls.reftable_offset, s->cluster_size);
2314 cpu_to_be32w(&l1_ofs_rt_ofs_cls.reftable_clusters, 1);
2315 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
2316 &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls));
2317 if (ret < 0) {
2318 goto fail_broken_refcounts;
2319 }
2320
2321 s->l1_table_offset = 3 * s->cluster_size;
2322
2323 new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t));
2324 if (!new_reftable) {
2325 ret = -ENOMEM;
2326 goto fail_broken_refcounts;
2327 }
2328
2329 s->refcount_table_offset = s->cluster_size;
2330 s->refcount_table_size = s->cluster_size / sizeof(uint64_t);
2331
2332 g_free(s->refcount_table);
2333 s->refcount_table = new_reftable;
2334 new_reftable = NULL;
2335
2336 /* Now the in-memory refcount information again corresponds to the on-disk
2337 * information (reftable is empty and no refblocks (the refblock cache is
2338 * empty)); however, this means some clusters (e.g. the image header) are
2339 * referenced, but not refcounted, but the normal qcow2 code assumes that
2340 * the in-memory information is always correct */
2341
2342 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
2343
2344 /* Enter the first refblock into the reftable */
2345 rt_entry = cpu_to_be64(2 * s->cluster_size);
2346 ret = bdrv_pwrite_sync(bs->file, s->cluster_size,
2347 &rt_entry, sizeof(rt_entry));
2348 if (ret < 0) {
2349 goto fail_broken_refcounts;
2350 }
2351 s->refcount_table[0] = 2 * s->cluster_size;
2352
2353 s->free_cluster_index = 0;
2354 assert(3 + l1_clusters <= s->refcount_block_size);
2355 offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
2356 if (offset < 0) {
2357 ret = offset;
2358 goto fail_broken_refcounts;
2359 } else if (offset > 0) {
2360 error_report("First cluster in emptied image is in use");
2361 abort();
2362 }
2363
2364 /* Now finally the in-memory information corresponds to the on-disk
2365 * structures and is correct */
2366 ret = qcow2_mark_clean(bs);
2367 if (ret < 0) {
2368 goto fail;
2369 }
2370
2371 ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size);
2372 if (ret < 0) {
2373 goto fail;
2374 }
2375
2376 return 0;
2377
2378 fail_broken_refcounts:
2379 /* The BDS is unusable at this point. If we wanted to make it usable, we
2380 * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
2381 * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
2382 * again. However, because the functions which could have caused this error
2383 * path to be taken are used by those functions as well, it's very likely
2384 * that that sequence will fail as well. Therefore, just eject the BDS. */
2385 bs->drv = NULL;
2386
2387 fail:
2388 g_free(new_reftable);
2389 return ret;
2390 }
2391
2392 static int qcow2_make_empty(BlockDriverState *bs)
2393 {
2394 BDRVQcowState *s = bs->opaque;
2395 uint64_t start_sector;
2396 int sector_step = INT_MAX / BDRV_SECTOR_SIZE;
2397 int l1_clusters, ret = 0;
2398
2399 l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t));
2400
2401 if (s->qcow_version >= 3 && !s->snapshots &&
2402 3 + l1_clusters <= s->refcount_block_size) {
2403 /* The following function only works for qcow2 v3 images (it requires
2404 * the dirty flag) and only as long as there are no snapshots (because
2405 * it completely empties the image). Furthermore, the L1 table and three
2406 * additional clusters (image header, refcount table, one refcount
2407 * block) have to fit inside one refcount block. */
2408 return make_completely_empty(bs);
2409 }
2410
2411 /* This fallback code simply discards every active cluster; this is slow,
2412 * but works in all cases */
2413 for (start_sector = 0; start_sector < bs->total_sectors;
2414 start_sector += sector_step)
2415 {
2416 /* As this function is generally used after committing an external
2417 * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
2418 * default action for this kind of discard is to pass the discard,
2419 * which will ideally result in an actually smaller image file, as
2420 * is probably desired. */
2421 ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE,
2422 MIN(sector_step,
2423 bs->total_sectors - start_sector),
2424 QCOW2_DISCARD_SNAPSHOT, true);
2425 if (ret < 0) {
2426 break;
2427 }
2428 }
2429
2430 return ret;
2431 }
2432
2433 static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
2434 {
2435 BDRVQcowState *s = bs->opaque;
2436 int ret;
2437
2438 qemu_co_mutex_lock(&s->lock);
2439 ret = qcow2_cache_flush(bs, s->l2_table_cache);
2440 if (ret < 0) {
2441 qemu_co_mutex_unlock(&s->lock);
2442 return ret;
2443 }
2444
2445 if (qcow2_need_accurate_refcounts(s)) {
2446 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2447 if (ret < 0) {
2448 qemu_co_mutex_unlock(&s->lock);
2449 return ret;
2450 }
2451 }
2452 qemu_co_mutex_unlock(&s->lock);
2453
2454 return 0;
2455 }
2456
2457 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2458 {
2459 BDRVQcowState *s = bs->opaque;
2460 bdi->unallocated_blocks_are_zero = true;
2461 bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3);
2462 bdi->cluster_size = s->cluster_size;
2463 bdi->vm_state_offset = qcow2_vm_state_offset(s);
2464 return 0;
2465 }
2466
2467 static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs)
2468 {
2469 BDRVQcowState *s = bs->opaque;
2470 ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1);
2471
2472 *spec_info = (ImageInfoSpecific){
2473 .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
2474 {
2475 .qcow2 = g_new(ImageInfoSpecificQCow2, 1),
2476 },
2477 };
2478 if (s->qcow_version == 2) {
2479 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2480 .compat = g_strdup("0.10"),
2481 };
2482 } else if (s->qcow_version == 3) {
2483 *spec_info->qcow2 = (ImageInfoSpecificQCow2){
2484 .compat = g_strdup("1.1"),
2485 .lazy_refcounts = s->compatible_features &
2486 QCOW2_COMPAT_LAZY_REFCOUNTS,
2487 .has_lazy_refcounts = true,
2488 .corrupt = s->incompatible_features &
2489 QCOW2_INCOMPAT_CORRUPT,
2490 .has_corrupt = true,
2491 };
2492 }
2493
2494 return spec_info;
2495 }
2496
2497 #if 0
2498 static void dump_refcounts(BlockDriverState *bs)
2499 {
2500 BDRVQcowState *s = bs->opaque;
2501 int64_t nb_clusters, k, k1, size;
2502 int refcount;
2503
2504 size = bdrv_getlength(bs->file);
2505 nb_clusters = size_to_clusters(s, size);
2506 for(k = 0; k < nb_clusters;) {
2507 k1 = k;
2508 refcount = get_refcount(bs, k);
2509 k++;
2510 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2511 k++;
2512 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
2513 k - k1);
2514 }
2515 }
2516 #endif
2517
2518 static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2519 int64_t pos)
2520 {
2521 BDRVQcowState *s = bs->opaque;
2522 int64_t total_sectors = bs->total_sectors;
2523 int growable = bs->growable;
2524 bool zero_beyond_eof = bs->zero_beyond_eof;
2525 int ret;
2526
2527 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
2528 bs->growable = 1;
2529 bs->zero_beyond_eof = false;
2530 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
2531 bs->growable = growable;
2532 bs->zero_beyond_eof = zero_beyond_eof;
2533
2534 /* bdrv_co_do_writev will have increased the total_sectors value to include
2535 * the VM state - the VM state is however not an actual part of the block
2536 * device, therefore, we need to restore the old value. */
2537 bs->total_sectors = total_sectors;
2538
2539 return ret;
2540 }
2541
2542 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2543 int64_t pos, int size)
2544 {
2545 BDRVQcowState *s = bs->opaque;
2546 int growable = bs->growable;
2547 bool zero_beyond_eof = bs->zero_beyond_eof;
2548 int ret;
2549
2550 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
2551 bs->growable = 1;
2552 bs->zero_beyond_eof = false;
2553 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
2554 bs->growable = growable;
2555 bs->zero_beyond_eof = zero_beyond_eof;
2556
2557 return ret;
2558 }
2559
2560 /*
2561 * Downgrades an image's version. To achieve this, any incompatible features
2562 * have to be removed.
2563 */
2564 static int qcow2_downgrade(BlockDriverState *bs, int target_version,
2565 BlockDriverAmendStatusCB *status_cb)
2566 {
2567 BDRVQcowState *s = bs->opaque;
2568 int current_version = s->qcow_version;
2569 int ret;
2570
2571 if (target_version == current_version) {
2572 return 0;
2573 } else if (target_version > current_version) {
2574 return -EINVAL;
2575 } else if (target_version != 2) {
2576 return -EINVAL;
2577 }
2578
2579 if (s->refcount_order != 4) {
2580 /* we would have to convert the image to a refcount_order == 4 image
2581 * here; however, since qemu (at the time of writing this) does not
2582 * support anything different than 4 anyway, there is no point in doing
2583 * so right now; however, we should error out (if qemu supports this in
2584 * the future and this code has not been adapted) */
2585 error_report("qcow2_downgrade: Image refcount orders other than 4 are "
2586 "currently not supported.");
2587 return -ENOTSUP;
2588 }
2589
2590 /* clear incompatible features */
2591 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
2592 ret = qcow2_mark_clean(bs);
2593 if (ret < 0) {
2594 return ret;
2595 }
2596 }
2597
2598 /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
2599 * the first place; if that happens nonetheless, returning -ENOTSUP is the
2600 * best thing to do anyway */
2601
2602 if (s->incompatible_features) {
2603 return -ENOTSUP;
2604 }
2605
2606 /* since we can ignore compatible features, we can set them to 0 as well */
2607 s->compatible_features = 0;
2608 /* if lazy refcounts have been used, they have already been fixed through
2609 * clearing the dirty flag */
2610
2611 /* clearing autoclear features is trivial */
2612 s->autoclear_features = 0;
2613
2614 ret = qcow2_expand_zero_clusters(bs, status_cb);
2615 if (ret < 0) {
2616 return ret;
2617 }
2618
2619 s->qcow_version = target_version;
2620 ret = qcow2_update_header(bs);
2621 if (ret < 0) {
2622 s->qcow_version = current_version;
2623 return ret;
2624 }
2625 return 0;
2626 }
2627
2628 static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
2629 BlockDriverAmendStatusCB *status_cb)
2630 {
2631 BDRVQcowState *s = bs->opaque;
2632 int old_version = s->qcow_version, new_version = old_version;
2633 uint64_t new_size = 0;
2634 const char *backing_file = NULL, *backing_format = NULL;
2635 bool lazy_refcounts = s->use_lazy_refcounts;
2636 const char *compat = NULL;
2637 uint64_t cluster_size = s->cluster_size;
2638 bool encrypt;
2639 int ret;
2640 QemuOptDesc *desc = opts->list->desc;
2641
2642 while (desc && desc->name) {
2643 if (!qemu_opt_find(opts, desc->name)) {
2644 /* only change explicitly defined options */
2645 desc++;
2646 continue;
2647 }
2648
2649 if (!strcmp(desc->name, "compat")) {
2650 compat = qemu_opt_get(opts, "compat");
2651 if (!compat) {
2652 /* preserve default */
2653 } else if (!strcmp(compat, "0.10")) {
2654 new_version = 2;
2655 } else if (!strcmp(compat, "1.1")) {
2656 new_version = 3;
2657 } else {
2658 fprintf(stderr, "Unknown compatibility level %s.\n", compat);
2659 return -EINVAL;
2660 }
2661 } else if (!strcmp(desc->name, "preallocation")) {
2662 fprintf(stderr, "Cannot change preallocation mode.\n");
2663 return -ENOTSUP;
2664 } else if (!strcmp(desc->name, "size")) {
2665 new_size = qemu_opt_get_size(opts, "size", 0);
2666 } else if (!strcmp(desc->name, "backing_file")) {
2667 backing_file = qemu_opt_get(opts, "backing_file");
2668 } else if (!strcmp(desc->name, "backing_fmt")) {
2669 backing_format = qemu_opt_get(opts, "backing_fmt");
2670 } else if (!strcmp(desc->name, "encryption")) {
2671 encrypt = qemu_opt_get_bool(opts, "encryption", s->crypt_method);
2672 if (encrypt != !!s->crypt_method) {
2673 fprintf(stderr, "Changing the encryption flag is not "
2674 "supported.\n");
2675 return -ENOTSUP;
2676 }
2677 } else if (!strcmp(desc->name, "cluster_size")) {
2678 cluster_size = qemu_opt_get_size(opts, "cluster_size",
2679 cluster_size);
2680 if (cluster_size != s->cluster_size) {
2681 fprintf(stderr, "Changing the cluster size is not "
2682 "supported.\n");
2683 return -ENOTSUP;
2684 }
2685 } else if (!strcmp(desc->name, "lazy_refcounts")) {
2686 lazy_refcounts = qemu_opt_get_bool(opts, "lazy_refcounts",
2687 lazy_refcounts);
2688 } else {
2689 /* if this assertion fails, this probably means a new option was
2690 * added without having it covered here */
2691 assert(false);
2692 }
2693
2694 desc++;
2695 }
2696
2697 if (new_version != old_version) {
2698 if (new_version > old_version) {
2699 /* Upgrade */
2700 s->qcow_version = new_version;
2701 ret = qcow2_update_header(bs);
2702 if (ret < 0) {
2703 s->qcow_version = old_version;
2704 return ret;
2705 }
2706 } else {
2707 ret = qcow2_downgrade(bs, new_version, status_cb);
2708 if (ret < 0) {
2709 return ret;
2710 }
2711 }
2712 }
2713
2714 if (backing_file || backing_format) {
2715 ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file,
2716 backing_format ?: bs->backing_format);
2717 if (ret < 0) {
2718 return ret;
2719 }
2720 }
2721
2722 if (s->use_lazy_refcounts != lazy_refcounts) {
2723 if (lazy_refcounts) {
2724 if (s->qcow_version < 3) {
2725 fprintf(stderr, "Lazy refcounts only supported with compatibility "
2726 "level 1.1 and above (use compat=1.1 or greater)\n");
2727 return -EINVAL;
2728 }
2729 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2730 ret = qcow2_update_header(bs);
2731 if (ret < 0) {
2732 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2733 return ret;
2734 }
2735 s->use_lazy_refcounts = true;
2736 } else {
2737 /* make image clean first */
2738 ret = qcow2_mark_clean(bs);
2739 if (ret < 0) {
2740 return ret;
2741 }
2742 /* now disallow lazy refcounts */
2743 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
2744 ret = qcow2_update_header(bs);
2745 if (ret < 0) {
2746 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
2747 return ret;
2748 }
2749 s->use_lazy_refcounts = false;
2750 }
2751 }
2752
2753 if (new_size) {
2754 ret = bdrv_truncate(bs, new_size);
2755 if (ret < 0) {
2756 return ret;
2757 }
2758 }
2759
2760 return 0;
2761 }
2762
2763 /*
2764 * If offset or size are negative, respectively, they will not be included in
2765 * the BLOCK_IMAGE_CORRUPTED event emitted.
2766 * fatal will be ignored for read-only BDS; corruptions found there will always
2767 * be considered non-fatal.
2768 */
2769 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
2770 int64_t size, const char *message_format, ...)
2771 {
2772 BDRVQcowState *s = bs->opaque;
2773 char *message;
2774 va_list ap;
2775
2776 fatal = fatal && !bs->read_only;
2777
2778 if (s->signaled_corruption &&
2779 (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
2780 {
2781 return;
2782 }
2783
2784 va_start(ap, message_format);
2785 message = g_strdup_vprintf(message_format, ap);
2786 va_end(ap);
2787
2788 if (fatal) {
2789 fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
2790 "corruption events will be suppressed\n", message);
2791 } else {
2792 fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
2793 "corruption events will be suppressed\n", message);
2794 }
2795
2796 qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs), message,
2797 offset >= 0, offset, size >= 0, size,
2798 fatal, &error_abort);
2799 g_free(message);
2800
2801 if (fatal) {
2802 qcow2_mark_corrupt(bs);
2803 bs->drv = NULL; /* make BDS unusable */
2804 }
2805
2806 s->signaled_corruption = true;
2807 }
2808
2809 static QemuOptsList qcow2_create_opts = {
2810 .name = "qcow2-create-opts",
2811 .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
2812 .desc = {
2813 {
2814 .name = BLOCK_OPT_SIZE,
2815 .type = QEMU_OPT_SIZE,
2816 .help = "Virtual disk size"
2817 },
2818 {
2819 .name = BLOCK_OPT_COMPAT_LEVEL,
2820 .type = QEMU_OPT_STRING,
2821 .help = "Compatibility level (0.10 or 1.1)"
2822 },
2823 {
2824 .name = BLOCK_OPT_BACKING_FILE,
2825 .type = QEMU_OPT_STRING,
2826 .help = "File name of a base image"
2827 },
2828 {
2829 .name = BLOCK_OPT_BACKING_FMT,
2830 .type = QEMU_OPT_STRING,
2831 .help = "Image format of the base image"
2832 },
2833 {
2834 .name = BLOCK_OPT_ENCRYPT,
2835 .type = QEMU_OPT_BOOL,
2836 .help = "Encrypt the image",
2837 .def_value_str = "off"
2838 },
2839 {
2840 .name = BLOCK_OPT_CLUSTER_SIZE,
2841 .type = QEMU_OPT_SIZE,
2842 .help = "qcow2 cluster size",
2843 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
2844 },
2845 {
2846 .name = BLOCK_OPT_PREALLOC,
2847 .type = QEMU_OPT_STRING,
2848 .help = "Preallocation mode (allowed values: off, metadata, "
2849 "falloc, full)"
2850 },
2851 {
2852 .name = BLOCK_OPT_LAZY_REFCOUNTS,
2853 .type = QEMU_OPT_BOOL,
2854 .help = "Postpone refcount updates",
2855 .def_value_str = "off"
2856 },
2857 { /* end of list */ }
2858 }
2859 };
2860
2861 BlockDriver bdrv_qcow2 = {
2862 .format_name = "qcow2",
2863 .instance_size = sizeof(BDRVQcowState),
2864 .bdrv_probe = qcow2_probe,
2865 .bdrv_open = qcow2_open,
2866 .bdrv_close = qcow2_close,
2867 .bdrv_reopen_prepare = qcow2_reopen_prepare,
2868 .bdrv_create = qcow2_create,
2869 .bdrv_has_zero_init = bdrv_has_zero_init_1,
2870 .bdrv_co_get_block_status = qcow2_co_get_block_status,
2871 .bdrv_set_key = qcow2_set_key,
2872
2873 .bdrv_co_readv = qcow2_co_readv,
2874 .bdrv_co_writev = qcow2_co_writev,
2875 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
2876
2877 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
2878 .bdrv_co_discard = qcow2_co_discard,
2879 .bdrv_truncate = qcow2_truncate,
2880 .bdrv_write_compressed = qcow2_write_compressed,
2881 .bdrv_make_empty = qcow2_make_empty,
2882
2883 .bdrv_snapshot_create = qcow2_snapshot_create,
2884 .bdrv_snapshot_goto = qcow2_snapshot_goto,
2885 .bdrv_snapshot_delete = qcow2_snapshot_delete,
2886 .bdrv_snapshot_list = qcow2_snapshot_list,
2887 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
2888 .bdrv_get_info = qcow2_get_info,
2889 .bdrv_get_specific_info = qcow2_get_specific_info,
2890
2891 .bdrv_save_vmstate = qcow2_save_vmstate,
2892 .bdrv_load_vmstate = qcow2_load_vmstate,
2893
2894 .supports_backing = true,
2895 .bdrv_change_backing_file = qcow2_change_backing_file,
2896
2897 .bdrv_refresh_limits = qcow2_refresh_limits,
2898 .bdrv_invalidate_cache = qcow2_invalidate_cache,
2899
2900 .create_opts = &qcow2_create_opts,
2901 .bdrv_check = qcow2_check,
2902 .bdrv_amend_options = qcow2_amend_options,
2903 };
2904
2905 static void bdrv_qcow2_init(void)
2906 {
2907 bdrv_register(&bdrv_qcow2);
2908 }
2909
2910 block_init(bdrv_qcow2_init);