]> git.proxmox.com Git - qemu.git/blame - block/qcow2.c
qom: allow casting of a NULL class
[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;
55} 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,
cfcc4c62 82 uint64_t end_offset, void **p_feature_table)
9b80ddf3 83{
75bab85c 84 BDRVQcowState *s = bs->opaque;
9b80ddf3
AL
85 QCowExtension ext;
86 uint64_t offset;
75bab85c 87 int ret;
9b80ddf3
AL
88
89#ifdef DEBUG_EXT
7c80ab3f 90 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
9b80ddf3
AL
91#endif
92 offset = start_offset;
93 while (offset < end_offset) {
94
95#ifdef DEBUG_EXT
96 /* Sanity check */
97 if (offset > s->cluster_size)
7c80ab3f 98 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
9b80ddf3 99
9b2260cb 100 printf("attempting to read extended header in offset %lu\n", offset);
9b80ddf3
AL
101#endif
102
66f82cee 103 if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
7c80ab3f 104 fprintf(stderr, "qcow2_read_extension: ERROR: "
0bfcd599
BS
105 "pread fail from offset %" PRIu64 "\n",
106 offset);
9b80ddf3
AL
107 return 1;
108 }
109 be32_to_cpus(&ext.magic);
110 be32_to_cpus(&ext.len);
111 offset += sizeof(ext);
112#ifdef DEBUG_EXT
113 printf("ext.magic = 0x%x\n", ext.magic);
114#endif
64ca6aee
KW
115 if (ext.len > end_offset - offset) {
116 error_report("Header extension too large");
117 return -EINVAL;
118 }
119
9b80ddf3 120 switch (ext.magic) {
7c80ab3f 121 case QCOW2_EXT_MAGIC_END:
9b80ddf3 122 return 0;
f965509c 123
7c80ab3f 124 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
f965509c
AL
125 if (ext.len >= sizeof(bs->backing_format)) {
126 fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
4c978075 127 " (>=%zu)\n",
f965509c
AL
128 ext.len, sizeof(bs->backing_format));
129 return 2;
130 }
66f82cee 131 if (bdrv_pread(bs->file, offset , bs->backing_format,
f965509c
AL
132 ext.len) != ext.len)
133 return 3;
134 bs->backing_format[ext.len] = '\0';
135#ifdef DEBUG_EXT
136 printf("Qcow2: Got format extension %s\n", bs->backing_format);
137#endif
f965509c
AL
138 break;
139
cfcc4c62
KW
140 case QCOW2_EXT_MAGIC_FEATURE_TABLE:
141 if (p_feature_table != NULL) {
142 void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
143 ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
144 if (ret < 0) {
145 return ret;
146 }
147
148 *p_feature_table = feature_table;
149 }
150 break;
151
9b80ddf3 152 default:
75bab85c
KW
153 /* unknown magic - save it in case we need to rewrite the header */
154 {
155 Qcow2UnknownHeaderExtension *uext;
156
157 uext = g_malloc0(sizeof(*uext) + ext.len);
158 uext->magic = ext.magic;
159 uext->len = ext.len;
160 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
161
162 ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
163 if (ret < 0) {
164 return ret;
165 }
75bab85c 166 }
9b80ddf3
AL
167 break;
168 }
fd29b4bb
KW
169
170 offset += ((ext.len + 7) & ~7);
9b80ddf3
AL
171 }
172
173 return 0;
174}
175
75bab85c
KW
176static void cleanup_unknown_header_ext(BlockDriverState *bs)
177{
178 BDRVQcowState *s = bs->opaque;
179 Qcow2UnknownHeaderExtension *uext, *next;
180
181 QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
182 QLIST_REMOVE(uext, next);
183 g_free(uext);
184 }
185}
9b80ddf3 186
b9531b6e
SW
187static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState *bs,
188 const char *fmt, ...)
6744cbab
KW
189{
190 char msg[64];
191 va_list ap;
192
193 va_start(ap, fmt);
194 vsnprintf(msg, sizeof(msg), fmt, ap);
195 va_end(ap);
196
197 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
198 bs->device_name, "qcow2", msg);
199}
200
cfcc4c62
KW
201static void report_unsupported_feature(BlockDriverState *bs,
202 Qcow2Feature *table, uint64_t mask)
203{
204 while (table && table->name[0] != '\0') {
205 if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
206 if (mask & (1 << table->bit)) {
207 report_unsupported(bs, "%.46s",table->name);
208 mask &= ~(1 << table->bit);
209 }
210 }
211 table++;
212 }
213
214 if (mask) {
215 report_unsupported(bs, "Unknown incompatible feature: %" PRIx64, mask);
216 }
217}
218
bfe8043e
SH
219/*
220 * Sets the dirty bit and flushes afterwards if necessary.
221 *
222 * The incompatible_features bit is only set if the image file header was
223 * updated successfully. Therefore it is not required to check the return
224 * value of this function.
225 */
280d3735 226int qcow2_mark_dirty(BlockDriverState *bs)
bfe8043e
SH
227{
228 BDRVQcowState *s = bs->opaque;
229 uint64_t val;
230 int ret;
231
232 assert(s->qcow_version >= 3);
233
234 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
235 return 0; /* already dirty */
236 }
237
238 val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
239 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
240 &val, sizeof(val));
241 if (ret < 0) {
242 return ret;
243 }
244 ret = bdrv_flush(bs->file);
245 if (ret < 0) {
246 return ret;
247 }
248
249 /* Only treat image as dirty if the header was updated successfully */
250 s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
251 return 0;
252}
253
c61d0004
SH
254/*
255 * Clears the dirty bit and flushes before if necessary. Only call this
256 * function when there are no pending requests, it does not guard against
257 * concurrent requests dirtying the image.
258 */
259static int qcow2_mark_clean(BlockDriverState *bs)
260{
261 BDRVQcowState *s = bs->opaque;
262
263 if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
264 int ret = bdrv_flush(bs);
265 if (ret < 0) {
266 return ret;
267 }
268
269 s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
270 return qcow2_update_header(bs);
271 }
272 return 0;
273}
274
acbe5982
SH
275static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
276 BdrvCheckMode fix)
277{
278 int ret = qcow2_check_refcounts(bs, result, fix);
279 if (ret < 0) {
280 return ret;
281 }
282
283 if (fix && result->check_errors == 0 && result->corruptions == 0) {
284 return qcow2_mark_clean(bs);
285 }
286 return ret;
287}
288
74c4510a
KW
289static QemuOptsList qcow2_runtime_opts = {
290 .name = "qcow2",
291 .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
292 .desc = {
293 {
294 .name = "lazy_refcounts",
295 .type = QEMU_OPT_BOOL,
296 .help = "Postpone refcount updates",
297 },
298 { /* end of list */ }
299 },
300};
301
1a86938f 302static int qcow2_open(BlockDriverState *bs, QDict *options, int flags)
585f8587
FB
303{
304 BDRVQcowState *s = bs->opaque;
6d85a57e 305 int len, i, ret = 0;
585f8587 306 QCowHeader header;
74c4510a
KW
307 QemuOpts *opts;
308 Error *local_err = NULL;
9b80ddf3 309 uint64_t ext_end;
585f8587 310
6d85a57e
JS
311 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
312 if (ret < 0) {
585f8587 313 goto fail;
6d85a57e 314 }
585f8587
FB
315 be32_to_cpus(&header.magic);
316 be32_to_cpus(&header.version);
317 be64_to_cpus(&header.backing_file_offset);
318 be32_to_cpus(&header.backing_file_size);
319 be64_to_cpus(&header.size);
320 be32_to_cpus(&header.cluster_bits);
321 be32_to_cpus(&header.crypt_method);
322 be64_to_cpus(&header.l1_table_offset);
323 be32_to_cpus(&header.l1_size);
324 be64_to_cpus(&header.refcount_table_offset);
325 be32_to_cpus(&header.refcount_table_clusters);
326 be64_to_cpus(&header.snapshots_offset);
327 be32_to_cpus(&header.nb_snapshots);
3b46e624 328
e8cdcec1 329 if (header.magic != QCOW_MAGIC) {
15bac0d5 330 ret = -EMEDIUMTYPE;
585f8587 331 goto fail;
6d85a57e 332 }
6744cbab
KW
333 if (header.version < 2 || header.version > 3) {
334 report_unsupported(bs, "QCOW version %d", header.version);
335 ret = -ENOTSUP;
336 goto fail;
337 }
338
339 s->qcow_version = header.version;
340
341 /* Initialise version 3 header fields */
342 if (header.version == 2) {
343 header.incompatible_features = 0;
344 header.compatible_features = 0;
345 header.autoclear_features = 0;
346 header.refcount_order = 4;
347 header.header_length = 72;
348 } else {
349 be64_to_cpus(&header.incompatible_features);
350 be64_to_cpus(&header.compatible_features);
351 be64_to_cpus(&header.autoclear_features);
352 be32_to_cpus(&header.refcount_order);
353 be32_to_cpus(&header.header_length);
354 }
355
356 if (header.header_length > sizeof(header)) {
357 s->unknown_header_fields_size = header.header_length - sizeof(header);
358 s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
359 ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
360 s->unknown_header_fields_size);
361 if (ret < 0) {
362 goto fail;
363 }
364 }
365
cfcc4c62
KW
366 if (header.backing_file_offset) {
367 ext_end = header.backing_file_offset;
368 } else {
369 ext_end = 1 << header.cluster_bits;
370 }
371
6744cbab
KW
372 /* Handle feature bits */
373 s->incompatible_features = header.incompatible_features;
374 s->compatible_features = header.compatible_features;
375 s->autoclear_features = header.autoclear_features;
376
c61d0004 377 if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
cfcc4c62
KW
378 void *feature_table = NULL;
379 qcow2_read_extensions(bs, header.header_length, ext_end,
380 &feature_table);
381 report_unsupported_feature(bs, feature_table,
c61d0004
SH
382 s->incompatible_features &
383 ~QCOW2_INCOMPAT_MASK);
6744cbab
KW
384 ret = -ENOTSUP;
385 goto fail;
386 }
387
6744cbab
KW
388 /* Check support for various header values */
389 if (header.refcount_order != 4) {
390 report_unsupported(bs, "%d bit reference counts",
391 1 << header.refcount_order);
e8cdcec1
KW
392 ret = -ENOTSUP;
393 goto fail;
394 }
6744cbab 395
d191d12d 396 if (header.cluster_bits < MIN_CLUSTER_BITS ||
6d85a57e
JS
397 header.cluster_bits > MAX_CLUSTER_BITS) {
398 ret = -EINVAL;
585f8587 399 goto fail;
6d85a57e
JS
400 }
401 if (header.crypt_method > QCOW_CRYPT_AES) {
402 ret = -EINVAL;
585f8587 403 goto fail;
6d85a57e 404 }
585f8587 405 s->crypt_method_header = header.crypt_method;
6d85a57e 406 if (s->crypt_method_header) {
585f8587 407 bs->encrypted = 1;
6d85a57e 408 }
585f8587
FB
409 s->cluster_bits = header.cluster_bits;
410 s->cluster_size = 1 << s->cluster_bits;
411 s->cluster_sectors = 1 << (s->cluster_bits - 9);
412 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
413 s->l2_size = 1 << s->l2_bits;
414 bs->total_sectors = header.size / 512;
415 s->csize_shift = (62 - (s->cluster_bits - 8));
416 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
417 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
418 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 419 s->refcount_table_size =
585f8587
FB
420 header.refcount_table_clusters << (s->cluster_bits - 3);
421
422 s->snapshots_offset = header.snapshots_offset;
423 s->nb_snapshots = header.nb_snapshots;
424
425 /* read the level 1 table */
426 s->l1_size = header.l1_size;
419b19d9 427 s->l1_vm_state_index = size_to_l1(s, header.size);
585f8587
FB
428 /* the L1 table must contain at least enough entries to put
429 header.size bytes */
6d85a57e
JS
430 if (s->l1_size < s->l1_vm_state_index) {
431 ret = -EINVAL;
585f8587 432 goto fail;
6d85a57e 433 }
585f8587 434 s->l1_table_offset = header.l1_table_offset;
d191d12d 435 if (s->l1_size > 0) {
7267c094 436 s->l1_table = g_malloc0(
d191d12d 437 align_offset(s->l1_size * sizeof(uint64_t), 512));
6d85a57e
JS
438 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
439 s->l1_size * sizeof(uint64_t));
440 if (ret < 0) {
d191d12d 441 goto fail;
6d85a57e 442 }
d191d12d
SW
443 for(i = 0;i < s->l1_size; i++) {
444 be64_to_cpus(&s->l1_table[i]);
445 }
585f8587 446 }
29c1a730
KW
447
448 /* alloc L2 table/refcount block cache */
6af4e9ea
PB
449 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
450 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
29c1a730 451
7267c094 452 s->cluster_cache = g_malloc(s->cluster_size);
585f8587 453 /* one more sector for decompressed data alignment */
dea43a65 454 s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
095a9c58 455 + 512);
585f8587 456 s->cluster_cache_offset = -1;
06d9260f 457 s->flags = flags;
3b46e624 458
6d85a57e
JS
459 ret = qcow2_refcount_init(bs);
460 if (ret != 0) {
585f8587 461 goto fail;
6d85a57e 462 }
585f8587 463
72cf2d4f 464 QLIST_INIT(&s->cluster_allocs);
f214978a 465
9b80ddf3 466 /* read qcow2 extensions */
cfcc4c62 467 if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL)) {
6d85a57e 468 ret = -EINVAL;
9b80ddf3 469 goto fail;
6d85a57e 470 }
9b80ddf3 471
585f8587
FB
472 /* read the backing file name */
473 if (header.backing_file_offset != 0) {
474 len = header.backing_file_size;
6d85a57e 475 if (len > 1023) {
585f8587 476 len = 1023;
6d85a57e
JS
477 }
478 ret = bdrv_pread(bs->file, header.backing_file_offset,
479 bs->backing_file, len);
480 if (ret < 0) {
585f8587 481 goto fail;
6d85a57e 482 }
585f8587
FB
483 bs->backing_file[len] = '\0';
484 }
42deb29f
KW
485
486 ret = qcow2_read_snapshots(bs);
487 if (ret < 0) {
585f8587 488 goto fail;
6d85a57e 489 }
585f8587 490
af7b708d
SH
491 /* Clear unknown autoclear feature bits */
492 if (!bs->read_only && s->autoclear_features != 0) {
493 s->autoclear_features = 0;
494 ret = qcow2_update_header(bs);
495 if (ret < 0) {
496 goto fail;
497 }
498 }
499
68d100e9
KW
500 /* Initialise locks */
501 qemu_co_mutex_init(&s->lock);
502
c61d0004 503 /* Repair image if dirty */
058f8f16
SH
504 if (!(flags & BDRV_O_CHECK) && !bs->read_only &&
505 (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
c61d0004
SH
506 BdrvCheckResult result = {0};
507
acbe5982 508 ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS);
c61d0004
SH
509 if (ret < 0) {
510 goto fail;
511 }
512 }
513
74c4510a
KW
514 /* Enable lazy_refcounts according to image and command line options */
515 opts = qemu_opts_create_nofail(&qcow2_runtime_opts);
516 qemu_opts_absorb_qdict(opts, options, &local_err);
517 if (error_is_set(&local_err)) {
518 qerror_report_err(local_err);
519 error_free(local_err);
520 ret = -EINVAL;
521 goto fail;
522 }
523
acdfb480 524 s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
74c4510a
KW
525 (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
526
527 qemu_opts_del(opts);
528
529 if (s->use_lazy_refcounts && s->qcow_version < 3) {
530 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Lazy refcounts require "
531 "a qcow2 image with at least qemu 1.1 compatibility level");
532 ret = -EINVAL;
533 goto fail;
534 }
535
585f8587 536#ifdef DEBUG_ALLOC
6cbc3031
PH
537 {
538 BdrvCheckResult result = {0};
b35278f7 539 qcow2_check_refcounts(bs, &result, 0);
6cbc3031 540 }
585f8587 541#endif
6d85a57e 542 return ret;
585f8587
FB
543
544 fail:
6744cbab 545 g_free(s->unknown_header_fields);
75bab85c 546 cleanup_unknown_header_ext(bs);
ed6ccf0f
KW
547 qcow2_free_snapshots(bs);
548 qcow2_refcount_close(bs);
7267c094 549 g_free(s->l1_table);
29c1a730
KW
550 if (s->l2_table_cache) {
551 qcow2_cache_destroy(bs, s->l2_table_cache);
552 }
7267c094 553 g_free(s->cluster_cache);
dea43a65 554 qemu_vfree(s->cluster_data);
6d85a57e 555 return ret;
585f8587
FB
556}
557
7c80ab3f 558static int qcow2_set_key(BlockDriverState *bs, const char *key)
585f8587
FB
559{
560 BDRVQcowState *s = bs->opaque;
561 uint8_t keybuf[16];
562 int len, i;
3b46e624 563
585f8587
FB
564 memset(keybuf, 0, 16);
565 len = strlen(key);
566 if (len > 16)
567 len = 16;
568 /* XXX: we could compress the chars to 7 bits to increase
569 entropy */
570 for(i = 0;i < len;i++) {
571 keybuf[i] = key[i];
572 }
573 s->crypt_method = s->crypt_method_header;
574
575 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
576 return -1;
577 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
578 return -1;
579#if 0
580 /* test */
581 {
582 uint8_t in[16];
583 uint8_t out[16];
584 uint8_t tmp[16];
585 for(i=0;i<16;i++)
586 in[i] = i;
587 AES_encrypt(in, tmp, &s->aes_encrypt_key);
588 AES_decrypt(tmp, out, &s->aes_decrypt_key);
589 for(i = 0; i < 16; i++)
590 printf(" %02x", tmp[i]);
591 printf("\n");
592 for(i = 0; i < 16; i++)
593 printf(" %02x", out[i]);
594 printf("\n");
595 }
596#endif
597 return 0;
598}
599
21d82ac9
JC
600/* We have nothing to do for QCOW2 reopen, stubs just return
601 * success */
602static int qcow2_reopen_prepare(BDRVReopenState *state,
603 BlockReopenQueue *queue, Error **errp)
604{
605 return 0;
606}
607
f8a2e5e3
SH
608static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs,
609 int64_t sector_num, int nb_sectors, int *pnum)
585f8587 610{
f8a2e5e3 611 BDRVQcowState *s = bs->opaque;
585f8587 612 uint64_t cluster_offset;
1c46efaa 613 int ret;
585f8587 614
095a9c58 615 *pnum = nb_sectors;
f8a2e5e3
SH
616 /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
617 * can't pass them on today */
618 qemu_co_mutex_lock(&s->lock);
1c46efaa 619 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
f8a2e5e3 620 qemu_co_mutex_unlock(&s->lock);
1c46efaa
KW
621 if (ret < 0) {
622 *pnum = 0;
623 }
095a9c58 624
381b487d 625 return (cluster_offset != 0) || (ret == QCOW2_CLUSTER_ZERO);
585f8587
FB
626}
627
a9465922 628/* handle reading after the end of the backing file */
bd28f835
KW
629int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
630 int64_t sector_num, int nb_sectors)
a9465922
FB
631{
632 int n1;
633 if ((sector_num + nb_sectors) <= bs->total_sectors)
634 return nb_sectors;
635 if (sector_num >= bs->total_sectors)
636 n1 = 0;
637 else
638 n1 = bs->total_sectors - sector_num;
bd28f835 639
3d9b4925 640 qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
bd28f835 641
a9465922
FB
642 return n1;
643}
644
a968168c 645static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
3fc48d09 646 int remaining_sectors, QEMUIOVector *qiov)
585f8587 647{
585f8587 648 BDRVQcowState *s = bs->opaque;
a9465922 649 int index_in_cluster, n1;
68d100e9 650 int ret;
faf575c1 651 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 652 uint64_t cluster_offset = 0;
3fc48d09
FZ
653 uint64_t bytes_done = 0;
654 QEMUIOVector hd_qiov;
655 uint8_t *cluster_data = NULL;
585f8587 656
3fc48d09
FZ
657 qemu_iovec_init(&hd_qiov, qiov->niov);
658
659 qemu_co_mutex_lock(&s->lock);
660
661 while (remaining_sectors != 0) {
bd28f835 662
5ebaa27e 663 /* prepare next request */
3fc48d09 664 cur_nr_sectors = remaining_sectors;
5ebaa27e
FZ
665 if (s->crypt_method) {
666 cur_nr_sectors = MIN(cur_nr_sectors,
667 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
585f8587 668 }
5ebaa27e 669
3fc48d09 670 ret = qcow2_get_cluster_offset(bs, sector_num << 9,
5ebaa27e 671 &cur_nr_sectors, &cluster_offset);
8af36488 672 if (ret < 0) {
3fc48d09 673 goto fail;
8af36488 674 }
bd28f835 675
3fc48d09 676 index_in_cluster = sector_num & (s->cluster_sectors - 1);
c87c0672 677
3fc48d09 678 qemu_iovec_reset(&hd_qiov);
1b093c48 679 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e
FZ
680 cur_nr_sectors * 512);
681
68d000a3
KW
682 switch (ret) {
683 case QCOW2_CLUSTER_UNALLOCATED:
5ebaa27e
FZ
684
685 if (bs->backing_hd) {
686 /* read from the base image */
3fc48d09
FZ
687 n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
688 sector_num, cur_nr_sectors);
5ebaa27e
FZ
689 if (n1 > 0) {
690 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
691 qemu_co_mutex_unlock(&s->lock);
3fc48d09
FZ
692 ret = bdrv_co_readv(bs->backing_hd, sector_num,
693 n1, &hd_qiov);
5ebaa27e
FZ
694 qemu_co_mutex_lock(&s->lock);
695 if (ret < 0) {
3fc48d09 696 goto fail;
5ebaa27e
FZ
697 }
698 }
699 } else {
700 /* Note: in this case, no need to wait */
3d9b4925 701 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
5ebaa27e 702 }
68d000a3
KW
703 break;
704
6377af48 705 case QCOW2_CLUSTER_ZERO:
3d9b4925 706 qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
6377af48
KW
707 break;
708
68d000a3 709 case QCOW2_CLUSTER_COMPRESSED:
5ebaa27e
FZ
710 /* add AIO support for compressed blocks ? */
711 ret = qcow2_decompress_cluster(bs, cluster_offset);
712 if (ret < 0) {
3fc48d09 713 goto fail;
bd28f835
KW
714 }
715
03396148 716 qemu_iovec_from_buf(&hd_qiov, 0,
5ebaa27e 717 s->cluster_cache + index_in_cluster * 512,
faf575c1 718 512 * cur_nr_sectors);
68d000a3
KW
719 break;
720
721 case QCOW2_CLUSTER_NORMAL:
5ebaa27e 722 if ((cluster_offset & 511) != 0) {
3fc48d09
FZ
723 ret = -EIO;
724 goto fail;
5ebaa27e 725 }
bd28f835 726
5ebaa27e
FZ
727 if (s->crypt_method) {
728 /*
729 * For encrypted images, read everything into a temporary
730 * contiguous buffer on which the AES functions can work.
731 */
3fc48d09
FZ
732 if (!cluster_data) {
733 cluster_data =
dea43a65 734 qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
5ebaa27e
FZ
735 }
736
737 assert(cur_nr_sectors <=
738 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
3fc48d09
FZ
739 qemu_iovec_reset(&hd_qiov);
740 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
741 512 * cur_nr_sectors);
742 }
743
744 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
745 qemu_co_mutex_unlock(&s->lock);
746 ret = bdrv_co_readv(bs->file,
747 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 748 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
749 qemu_co_mutex_lock(&s->lock);
750 if (ret < 0) {
3fc48d09 751 goto fail;
5ebaa27e
FZ
752 }
753 if (s->crypt_method) {
3fc48d09
FZ
754 qcow2_encrypt_sectors(s, sector_num, cluster_data,
755 cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
03396148
MT
756 qemu_iovec_from_buf(qiov, bytes_done,
757 cluster_data, 512 * cur_nr_sectors);
5ebaa27e 758 }
68d000a3
KW
759 break;
760
761 default:
762 g_assert_not_reached();
763 ret = -EIO;
764 goto fail;
faf575c1 765 }
f141eafe 766
3fc48d09
FZ
767 remaining_sectors -= cur_nr_sectors;
768 sector_num += cur_nr_sectors;
769 bytes_done += cur_nr_sectors * 512;
5ebaa27e 770 }
3fc48d09 771 ret = 0;
faf575c1 772
3fc48d09 773fail:
68d100e9 774 qemu_co_mutex_unlock(&s->lock);
42496d62 775
3fc48d09 776 qemu_iovec_destroy(&hd_qiov);
dea43a65 777 qemu_vfree(cluster_data);
68d100e9
KW
778
779 return ret;
585f8587
FB
780}
781
a968168c 782static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
3fc48d09
FZ
783 int64_t sector_num,
784 int remaining_sectors,
785 QEMUIOVector *qiov)
585f8587 786{
585f8587 787 BDRVQcowState *s = bs->opaque;
585f8587 788 int index_in_cluster;
095a9c58 789 int n_end;
68d100e9 790 int ret;
faf575c1 791 int cur_nr_sectors; /* number of sectors in current iteration */
c2bdd990 792 uint64_t cluster_offset;
3fc48d09
FZ
793 QEMUIOVector hd_qiov;
794 uint64_t bytes_done = 0;
795 uint8_t *cluster_data = NULL;
8d2497c3 796 QCowL2Meta *l2meta = NULL;
c2271403 797
3cce16f4
KW
798 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
799 remaining_sectors);
800
3fc48d09
FZ
801 qemu_iovec_init(&hd_qiov, qiov->niov);
802
803 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 804
3fc48d09
FZ
805 qemu_co_mutex_lock(&s->lock);
806
807 while (remaining_sectors != 0) {
808
f50f88b9 809 l2meta = NULL;
cf5c1a23 810
3cce16f4 811 trace_qcow2_writev_start_part(qemu_coroutine_self());
3fc48d09
FZ
812 index_in_cluster = sector_num & (s->cluster_sectors - 1);
813 n_end = index_in_cluster + remaining_sectors;
5ebaa27e
FZ
814 if (s->crypt_method &&
815 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
816 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
817 }
095a9c58 818
3fc48d09 819 ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
f50f88b9 820 index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta);
5ebaa27e 821 if (ret < 0) {
3fc48d09 822 goto fail;
5ebaa27e 823 }
148da7ea 824
5ebaa27e 825 assert((cluster_offset & 511) == 0);
148da7ea 826
3fc48d09 827 qemu_iovec_reset(&hd_qiov);
1b093c48 828 qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
5ebaa27e 829 cur_nr_sectors * 512);
6f5f060b 830
5ebaa27e 831 if (s->crypt_method) {
3fc48d09 832 if (!cluster_data) {
dea43a65 833 cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
5ebaa27e
FZ
834 s->cluster_size);
835 }
6f5f060b 836
3fc48d09 837 assert(hd_qiov.size <=
5ebaa27e 838 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
d5e6b161 839 qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
6f5f060b 840
3fc48d09
FZ
841 qcow2_encrypt_sectors(s, sector_num, cluster_data,
842 cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
6f5f060b 843
3fc48d09
FZ
844 qemu_iovec_reset(&hd_qiov);
845 qemu_iovec_add(&hd_qiov, cluster_data,
5ebaa27e
FZ
846 cur_nr_sectors * 512);
847 }
6f5f060b 848
5ebaa27e 849 qemu_co_mutex_unlock(&s->lock);
67a7a0eb 850 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
3cce16f4
KW
851 trace_qcow2_writev_data(qemu_coroutine_self(),
852 (cluster_offset >> 9) + index_in_cluster);
5ebaa27e
FZ
853 ret = bdrv_co_writev(bs->file,
854 (cluster_offset >> 9) + index_in_cluster,
3fc48d09 855 cur_nr_sectors, &hd_qiov);
5ebaa27e
FZ
856 qemu_co_mutex_lock(&s->lock);
857 if (ret < 0) {
3fc48d09 858 goto fail;
5ebaa27e 859 }
f141eafe 860
88c6588c
KW
861 while (l2meta != NULL) {
862 QCowL2Meta *next;
863
f50f88b9
KW
864 ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
865 if (ret < 0) {
866 goto fail;
867 }
faf575c1 868
4e95314e
KW
869 /* Take the request off the list of running requests */
870 if (l2meta->nb_clusters != 0) {
871 QLIST_REMOVE(l2meta, next_in_flight);
872 }
873
4e95314e 874 qemu_co_queue_restart_all(&l2meta->dependent_requests);
4e95314e 875
88c6588c 876 next = l2meta->next;
f50f88b9 877 g_free(l2meta);
88c6588c 878 l2meta = next;
f50f88b9 879 }
0fa9131a 880
3fc48d09
FZ
881 remaining_sectors -= cur_nr_sectors;
882 sector_num += cur_nr_sectors;
883 bytes_done += cur_nr_sectors * 512;
3cce16f4 884 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
5ebaa27e 885 }
3fc48d09 886 ret = 0;
faf575c1 887
3fc48d09 888fail:
4e95314e
KW
889 qemu_co_mutex_unlock(&s->lock);
890
88c6588c
KW
891 while (l2meta != NULL) {
892 QCowL2Meta *next;
893
4e95314e
KW
894 if (l2meta->nb_clusters != 0) {
895 QLIST_REMOVE(l2meta, next_in_flight);
896 }
897 qemu_co_queue_restart_all(&l2meta->dependent_requests);
88c6588c
KW
898
899 next = l2meta->next;
cf5c1a23 900 g_free(l2meta);
88c6588c 901 l2meta = next;
cf5c1a23 902 }
0fa9131a 903
3fc48d09 904 qemu_iovec_destroy(&hd_qiov);
dea43a65 905 qemu_vfree(cluster_data);
3cce16f4 906 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
42496d62 907
68d100e9 908 return ret;
585f8587
FB
909}
910
7c80ab3f 911static void qcow2_close(BlockDriverState *bs)
585f8587
FB
912{
913 BDRVQcowState *s = bs->opaque;
7267c094 914 g_free(s->l1_table);
29c1a730
KW
915
916 qcow2_cache_flush(bs, s->l2_table_cache);
917 qcow2_cache_flush(bs, s->refcount_block_cache);
918
c61d0004
SH
919 qcow2_mark_clean(bs);
920
29c1a730
KW
921 qcow2_cache_destroy(bs, s->l2_table_cache);
922 qcow2_cache_destroy(bs, s->refcount_block_cache);
923
6744cbab 924 g_free(s->unknown_header_fields);
75bab85c 925 cleanup_unknown_header_ext(bs);
6744cbab 926
7267c094 927 g_free(s->cluster_cache);
dea43a65 928 qemu_vfree(s->cluster_data);
ed6ccf0f 929 qcow2_refcount_close(bs);
28c1202b 930 qcow2_free_snapshots(bs);
585f8587
FB
931}
932
06d9260f
AL
933static void qcow2_invalidate_cache(BlockDriverState *bs)
934{
935 BDRVQcowState *s = bs->opaque;
936 int flags = s->flags;
937 AES_KEY aes_encrypt_key;
938 AES_KEY aes_decrypt_key;
939 uint32_t crypt_method = 0;
acdfb480 940 QDict *options;
06d9260f
AL
941
942 /*
943 * Backing files are read-only which makes all of their metadata immutable,
944 * that means we don't have to worry about reopening them here.
945 */
946
947 if (s->crypt_method) {
948 crypt_method = s->crypt_method;
949 memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
950 memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
951 }
952
953 qcow2_close(bs);
954
acdfb480
KW
955 options = qdict_new();
956 qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS,
957 qbool_from_int(s->use_lazy_refcounts));
958
06d9260f 959 memset(s, 0, sizeof(BDRVQcowState));
acdfb480
KW
960 qcow2_open(bs, options, flags);
961
962 QDECREF(options);
06d9260f
AL
963
964 if (crypt_method) {
965 s->crypt_method = crypt_method;
966 memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
967 memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
968 }
969}
970
e24e49e6
KW
971static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
972 size_t len, size_t buflen)
973{
974 QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
975 size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
976
977 if (buflen < ext_len) {
978 return -ENOSPC;
979 }
980
981 *ext_backing_fmt = (QCowExtension) {
982 .magic = cpu_to_be32(magic),
983 .len = cpu_to_be32(len),
984 };
985 memcpy(buf + sizeof(QCowExtension), s, len);
986
987 return ext_len;
988}
989
756e6736 990/*
e24e49e6
KW
991 * Updates the qcow2 header, including the variable length parts of it, i.e.
992 * the backing file name and all extensions. qcow2 was not designed to allow
993 * such changes, so if we run out of space (we can only use the first cluster)
994 * this function may fail.
756e6736
KW
995 *
996 * Returns 0 on success, -errno in error cases.
997 */
e24e49e6 998int qcow2_update_header(BlockDriverState *bs)
756e6736 999{
756e6736 1000 BDRVQcowState *s = bs->opaque;
e24e49e6
KW
1001 QCowHeader *header;
1002 char *buf;
1003 size_t buflen = s->cluster_size;
756e6736 1004 int ret;
e24e49e6
KW
1005 uint64_t total_size;
1006 uint32_t refcount_table_clusters;
6744cbab 1007 size_t header_length;
75bab85c 1008 Qcow2UnknownHeaderExtension *uext;
756e6736 1009
e24e49e6 1010 buf = qemu_blockalign(bs, buflen);
756e6736 1011
e24e49e6
KW
1012 /* Header structure */
1013 header = (QCowHeader*) buf;
756e6736 1014
e24e49e6
KW
1015 if (buflen < sizeof(*header)) {
1016 ret = -ENOSPC;
1017 goto fail;
756e6736
KW
1018 }
1019
6744cbab 1020 header_length = sizeof(*header) + s->unknown_header_fields_size;
e24e49e6
KW
1021 total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
1022 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1023
1024 *header = (QCowHeader) {
6744cbab 1025 /* Version 2 fields */
e24e49e6 1026 .magic = cpu_to_be32(QCOW_MAGIC),
6744cbab 1027 .version = cpu_to_be32(s->qcow_version),
e24e49e6
KW
1028 .backing_file_offset = 0,
1029 .backing_file_size = 0,
1030 .cluster_bits = cpu_to_be32(s->cluster_bits),
1031 .size = cpu_to_be64(total_size),
1032 .crypt_method = cpu_to_be32(s->crypt_method_header),
1033 .l1_size = cpu_to_be32(s->l1_size),
1034 .l1_table_offset = cpu_to_be64(s->l1_table_offset),
1035 .refcount_table_offset = cpu_to_be64(s->refcount_table_offset),
1036 .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
1037 .nb_snapshots = cpu_to_be32(s->nb_snapshots),
1038 .snapshots_offset = cpu_to_be64(s->snapshots_offset),
6744cbab
KW
1039
1040 /* Version 3 fields */
1041 .incompatible_features = cpu_to_be64(s->incompatible_features),
1042 .compatible_features = cpu_to_be64(s->compatible_features),
1043 .autoclear_features = cpu_to_be64(s->autoclear_features),
1044 .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT),
1045 .header_length = cpu_to_be32(header_length),
e24e49e6 1046 };
756e6736 1047
6744cbab
KW
1048 /* For older versions, write a shorter header */
1049 switch (s->qcow_version) {
1050 case 2:
1051 ret = offsetof(QCowHeader, incompatible_features);
1052 break;
1053 case 3:
1054 ret = sizeof(*header);
1055 break;
1056 default:
b6c14762
JM
1057 ret = -EINVAL;
1058 goto fail;
6744cbab
KW
1059 }
1060
1061 buf += ret;
1062 buflen -= ret;
1063 memset(buf, 0, buflen);
1064
1065 /* Preserve any unknown field in the header */
1066 if (s->unknown_header_fields_size) {
1067 if (buflen < s->unknown_header_fields_size) {
1068 ret = -ENOSPC;
1069 goto fail;
1070 }
1071
1072 memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1073 buf += s->unknown_header_fields_size;
1074 buflen -= s->unknown_header_fields_size;
1075 }
756e6736 1076
e24e49e6
KW
1077 /* Backing file format header extension */
1078 if (*bs->backing_format) {
1079 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1080 bs->backing_format, strlen(bs->backing_format),
1081 buflen);
1082 if (ret < 0) {
1083 goto fail;
756e6736
KW
1084 }
1085
e24e49e6
KW
1086 buf += ret;
1087 buflen -= ret;
756e6736
KW
1088 }
1089
cfcc4c62
KW
1090 /* Feature table */
1091 Qcow2Feature features[] = {
c61d0004
SH
1092 {
1093 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1094 .bit = QCOW2_INCOMPAT_DIRTY_BITNR,
1095 .name = "dirty bit",
1096 },
bfe8043e
SH
1097 {
1098 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1099 .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1100 .name = "lazy refcounts",
1101 },
cfcc4c62
KW
1102 };
1103
1104 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1105 features, sizeof(features), buflen);
1106 if (ret < 0) {
1107 goto fail;
1108 }
1109 buf += ret;
1110 buflen -= ret;
1111
75bab85c
KW
1112 /* Keep unknown header extensions */
1113 QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1114 ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1115 if (ret < 0) {
1116 goto fail;
1117 }
1118
1119 buf += ret;
1120 buflen -= ret;
1121 }
1122
e24e49e6
KW
1123 /* End of header extensions */
1124 ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
756e6736
KW
1125 if (ret < 0) {
1126 goto fail;
1127 }
1128
e24e49e6
KW
1129 buf += ret;
1130 buflen -= ret;
756e6736 1131
e24e49e6
KW
1132 /* Backing file name */
1133 if (*bs->backing_file) {
1134 size_t backing_file_len = strlen(bs->backing_file);
1135
1136 if (buflen < backing_file_len) {
1137 ret = -ENOSPC;
1138 goto fail;
1139 }
1140
00ea1881 1141 /* Using strncpy is ok here, since buf is not NUL-terminated. */
e24e49e6
KW
1142 strncpy(buf, bs->backing_file, buflen);
1143
1144 header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1145 header->backing_file_size = cpu_to_be32(backing_file_len);
756e6736
KW
1146 }
1147
e24e49e6
KW
1148 /* Write the new header */
1149 ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
756e6736
KW
1150 if (ret < 0) {
1151 goto fail;
1152 }
1153
1154 ret = 0;
1155fail:
e24e49e6 1156 qemu_vfree(header);
756e6736
KW
1157 return ret;
1158}
1159
1160static int qcow2_change_backing_file(BlockDriverState *bs,
1161 const char *backing_file, const char *backing_fmt)
1162{
e24e49e6
KW
1163 pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1164 pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1165
1166 return qcow2_update_header(bs);
756e6736
KW
1167}
1168
a35e1c17
KW
1169static int preallocate(BlockDriverState *bs)
1170{
a35e1c17
KW
1171 uint64_t nb_sectors;
1172 uint64_t offset;
060bee89 1173 uint64_t host_offset = 0;
a35e1c17 1174 int num;
148da7ea 1175 int ret;
f50f88b9 1176 QCowL2Meta *meta;
a35e1c17
KW
1177
1178 nb_sectors = bdrv_getlength(bs) >> 9;
1179 offset = 0;
1180
1181 while (nb_sectors) {
1182 num = MIN(nb_sectors, INT_MAX >> 9);
060bee89
KW
1183 ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
1184 &host_offset, &meta);
148da7ea 1185 if (ret < 0) {
19dbcbf7 1186 return ret;
a35e1c17
KW
1187 }
1188
f50f88b9 1189 ret = qcow2_alloc_cluster_link_l2(bs, meta);
19dbcbf7 1190 if (ret < 0) {
f50f88b9 1191 qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters);
19dbcbf7 1192 return ret;
a35e1c17
KW
1193 }
1194
f214978a
KW
1195 /* There are no dependent requests, but we need to remove our request
1196 * from the list of in-flight requests */
f50f88b9 1197 if (meta != NULL) {
4e95314e 1198 QLIST_REMOVE(meta, next_in_flight);
f50f88b9 1199 }
f214978a 1200
a35e1c17
KW
1201 /* TODO Preallocate data if requested */
1202
1203 nb_sectors -= num;
1204 offset += num << 9;
1205 }
1206
1207 /*
1208 * It is expected that the image file is large enough to actually contain
1209 * all of the allocated clusters (otherwise we get failing reads after
1210 * EOF). Extend the image to the last allocated sector.
1211 */
060bee89 1212 if (host_offset != 0) {
ea80b906
KW
1213 uint8_t buf[512];
1214 memset(buf, 0, 512);
060bee89 1215 ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1);
19dbcbf7
KW
1216 if (ret < 0) {
1217 return ret;
1218 }
a35e1c17
KW
1219 }
1220
1221 return 0;
1222}
1223
7c80ab3f
JS
1224static int qcow2_create2(const char *filename, int64_t total_size,
1225 const char *backing_file, const char *backing_format,
1226 int flags, size_t cluster_size, int prealloc,
6744cbab 1227 QEMUOptionParameter *options, int version)
a9420734 1228{
9b2260cb 1229 /* Calculate cluster_bits */
a9420734
KW
1230 int cluster_bits;
1231 cluster_bits = ffs(cluster_size) - 1;
1232 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1233 (1 << cluster_bits) != cluster_size)
1234 {
1235 error_report(
6daf194d 1236 "Cluster size must be a power of two between %d and %dk",
a9420734
KW
1237 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1238 return -EINVAL;
1239 }
1240
1241 /*
1242 * Open the image file and write a minimal qcow2 header.
1243 *
1244 * We keep things simple and start with a zero-sized image. We also
1245 * do without refcount blocks or a L1 table for now. We'll fix the
1246 * inconsistency later.
1247 *
1248 * We do need a refcount table because growing the refcount table means
1249 * allocating two new refcount blocks - the seconds of which would be at
1250 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1251 * size for any qcow2 image.
1252 */
1253 BlockDriverState* bs;
1254 QCowHeader header;
1255 uint8_t* refcount_table;
1256 int ret;
1257
1258 ret = bdrv_create_file(filename, options);
1259 if (ret < 0) {
1260 return ret;
1261 }
1262
787e4a85 1263 ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR);
a9420734
KW
1264 if (ret < 0) {
1265 return ret;
1266 }
1267
1268 /* Write the header */
1269 memset(&header, 0, sizeof(header));
1270 header.magic = cpu_to_be32(QCOW_MAGIC);
6744cbab 1271 header.version = cpu_to_be32(version);
a9420734
KW
1272 header.cluster_bits = cpu_to_be32(cluster_bits);
1273 header.size = cpu_to_be64(0);
1274 header.l1_table_offset = cpu_to_be64(0);
1275 header.l1_size = cpu_to_be32(0);
1276 header.refcount_table_offset = cpu_to_be64(cluster_size);
1277 header.refcount_table_clusters = cpu_to_be32(1);
6744cbab
KW
1278 header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
1279 header.header_length = cpu_to_be32(sizeof(header));
a9420734
KW
1280
1281 if (flags & BLOCK_FLAG_ENCRYPT) {
1282 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1283 } else {
1284 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1285 }
1286
bfe8043e
SH
1287 if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1288 header.compatible_features |=
1289 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1290 }
1291
a9420734
KW
1292 ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1293 if (ret < 0) {
1294 goto out;
1295 }
1296
1297 /* Write an empty refcount table */
7267c094 1298 refcount_table = g_malloc0(cluster_size);
a9420734 1299 ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
7267c094 1300 g_free(refcount_table);
a9420734
KW
1301
1302 if (ret < 0) {
1303 goto out;
1304 }
1305
1306 bdrv_close(bs);
1307
1308 /*
1309 * And now open the image and make it consistent first (i.e. increase the
1310 * refcount of the cluster that is occupied by the header and the refcount
1311 * table)
1312 */
1313 BlockDriver* drv = bdrv_find_format("qcow2");
1314 assert(drv != NULL);
de9c0cec 1315 ret = bdrv_open(bs, filename, NULL,
e1a7107f 1316 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
a9420734
KW
1317 if (ret < 0) {
1318 goto out;
1319 }
1320
1321 ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1322 if (ret < 0) {
1323 goto out;
1324
1325 } else if (ret != 0) {
1326 error_report("Huh, first cluster in empty image is already in use?");
1327 abort();
1328 }
1329
1330 /* Okay, now that we have a valid image, let's give it the right size */
1331 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1332 if (ret < 0) {
1333 goto out;
1334 }
1335
1336 /* Want a backing file? There you go.*/
1337 if (backing_file) {
1338 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1339 if (ret < 0) {
1340 goto out;
1341 }
1342 }
1343
1344 /* And if we're supposed to preallocate metadata, do that now */
1345 if (prealloc) {
15552c4a
ZYW
1346 BDRVQcowState *s = bs->opaque;
1347 qemu_co_mutex_lock(&s->lock);
a9420734 1348 ret = preallocate(bs);
15552c4a 1349 qemu_co_mutex_unlock(&s->lock);
a9420734
KW
1350 if (ret < 0) {
1351 goto out;
1352 }
1353 }
1354
1355 ret = 0;
1356out:
1357 bdrv_delete(bs);
1358 return ret;
1359}
de5f3f40 1360
7c80ab3f 1361static int qcow2_create(const char *filename, QEMUOptionParameter *options)
de5f3f40
KW
1362{
1363 const char *backing_file = NULL;
1364 const char *backing_fmt = NULL;
1365 uint64_t sectors = 0;
1366 int flags = 0;
99cce9fa 1367 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
de5f3f40 1368 int prealloc = 0;
6744cbab 1369 int version = 2;
de5f3f40
KW
1370
1371 /* Read out options */
1372 while (options && options->name) {
1373 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1374 sectors = options->value.n / 512;
1375 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1376 backing_file = options->value.s;
1377 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1378 backing_fmt = options->value.s;
1379 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1380 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1381 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1382 if (options->value.n) {
1383 cluster_size = options->value.n;
1384 }
1385 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1386 if (!options->value.s || !strcmp(options->value.s, "off")) {
1387 prealloc = 0;
1388 } else if (!strcmp(options->value.s, "metadata")) {
1389 prealloc = 1;
1390 } else {
1391 fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1392 options->value.s);
1393 return -EINVAL;
1394 }
6744cbab
KW
1395 } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
1396 if (!options->value.s || !strcmp(options->value.s, "0.10")) {
1397 version = 2;
1398 } else if (!strcmp(options->value.s, "1.1")) {
1399 version = 3;
1400 } else {
1401 fprintf(stderr, "Invalid compatibility level: '%s'\n",
1402 options->value.s);
1403 return -EINVAL;
1404 }
bfe8043e
SH
1405 } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1406 flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
de5f3f40
KW
1407 }
1408 options++;
1409 }
1410
1411 if (backing_file && prealloc) {
1412 fprintf(stderr, "Backing file and preallocation cannot be used at "
1413 "the same time\n");
1414 return -EINVAL;
1415 }
1416
bfe8043e
SH
1417 if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
1418 fprintf(stderr, "Lazy refcounts only supported with compatibility "
1419 "level 1.1 and above (use compat=1.1 or greater)\n");
1420 return -EINVAL;
1421 }
1422
7c80ab3f 1423 return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
6744cbab 1424 cluster_size, prealloc, options, version);
de5f3f40
KW
1425}
1426
7c80ab3f 1427static int qcow2_make_empty(BlockDriverState *bs)
20d97356
BS
1428{
1429#if 0
1430 /* XXX: not correct */
1431 BDRVQcowState *s = bs->opaque;
1432 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1433 int ret;
1434
1435 memset(s->l1_table, 0, l1_length);
66f82cee 1436 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
20d97356 1437 return -1;
66f82cee 1438 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
20d97356
BS
1439 if (ret < 0)
1440 return ret;
1441
1442 l2_cache_reset(bs);
1443#endif
1444 return 0;
1445}
1446
621f0589
KW
1447static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
1448 int64_t sector_num, int nb_sectors)
1449{
1450 int ret;
1451 BDRVQcowState *s = bs->opaque;
1452
1453 /* Emulate misaligned zero writes */
1454 if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1455 return -ENOTSUP;
1456 }
1457
1458 /* Whatever is left can use real zero clusters */
1459 qemu_co_mutex_lock(&s->lock);
1460 ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1461 nb_sectors);
1462 qemu_co_mutex_unlock(&s->lock);
1463
1464 return ret;
1465}
1466
6db39ae2
PB
1467static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1468 int64_t sector_num, int nb_sectors)
5ea929e3 1469{
6db39ae2
PB
1470 int ret;
1471 BDRVQcowState *s = bs->opaque;
1472
1473 qemu_co_mutex_lock(&s->lock);
1474 ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
5ea929e3 1475 nb_sectors);
6db39ae2
PB
1476 qemu_co_mutex_unlock(&s->lock);
1477 return ret;
5ea929e3
KW
1478}
1479
419b19d9
SH
1480static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1481{
1482 BDRVQcowState *s = bs->opaque;
1483 int ret, new_l1_size;
1484
1485 if (offset & 511) {
259b2173 1486 error_report("The new size must be a multiple of 512");
419b19d9
SH
1487 return -EINVAL;
1488 }
1489
1490 /* cannot proceed if image has snapshots */
1491 if (s->nb_snapshots) {
259b2173 1492 error_report("Can't resize an image which has snapshots");
419b19d9
SH
1493 return -ENOTSUP;
1494 }
1495
1496 /* shrinking is currently not supported */
1497 if (offset < bs->total_sectors * 512) {
259b2173 1498 error_report("qcow2 doesn't support shrinking images yet");
419b19d9
SH
1499 return -ENOTSUP;
1500 }
1501
1502 new_l1_size = size_to_l1(s, offset);
72893756 1503 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
419b19d9
SH
1504 if (ret < 0) {
1505 return ret;
1506 }
1507
1508 /* write updated header.size */
1509 offset = cpu_to_be64(offset);
8b3b7206
KW
1510 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1511 &offset, sizeof(uint64_t));
419b19d9
SH
1512 if (ret < 0) {
1513 return ret;
1514 }
1515
1516 s->l1_vm_state_index = new_l1_size;
1517 return 0;
1518}
1519
20d97356
BS
1520/* XXX: put compressed sectors first, then all the cluster aligned
1521 tables to avoid losing bytes in alignment */
7c80ab3f
JS
1522static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1523 const uint8_t *buf, int nb_sectors)
20d97356
BS
1524{
1525 BDRVQcowState *s = bs->opaque;
1526 z_stream strm;
1527 int ret, out_len;
1528 uint8_t *out_buf;
1529 uint64_t cluster_offset;
1530
1531 if (nb_sectors == 0) {
1532 /* align end of file to a sector boundary to ease reading with
1533 sector based I/Os */
66f82cee 1534 cluster_offset = bdrv_getlength(bs->file);
20d97356 1535 cluster_offset = (cluster_offset + 511) & ~511;
66f82cee 1536 bdrv_truncate(bs->file, cluster_offset);
20d97356
BS
1537 return 0;
1538 }
1539
f4d38bef
SH
1540 if (nb_sectors != s->cluster_sectors) {
1541 ret = -EINVAL;
1542
1543 /* Zero-pad last write if image size is not cluster aligned */
1544 if (sector_num + nb_sectors == bs->total_sectors &&
1545 nb_sectors < s->cluster_sectors) {
1546 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
1547 memset(pad_buf, 0, s->cluster_size);
1548 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
1549 ret = qcow2_write_compressed(bs, sector_num,
1550 pad_buf, s->cluster_sectors);
1551 qemu_vfree(pad_buf);
1552 }
1553 return ret;
1554 }
20d97356 1555
7267c094 1556 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
20d97356
BS
1557
1558 /* best compression, small window, no zlib header */
1559 memset(&strm, 0, sizeof(strm));
1560 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1561 Z_DEFLATED, -12,
1562 9, Z_DEFAULT_STRATEGY);
1563 if (ret != 0) {
8f1efd00
KW
1564 ret = -EINVAL;
1565 goto fail;
20d97356
BS
1566 }
1567
1568 strm.avail_in = s->cluster_size;
1569 strm.next_in = (uint8_t *)buf;
1570 strm.avail_out = s->cluster_size;
1571 strm.next_out = out_buf;
1572
1573 ret = deflate(&strm, Z_FINISH);
1574 if (ret != Z_STREAM_END && ret != Z_OK) {
20d97356 1575 deflateEnd(&strm);
8f1efd00
KW
1576 ret = -EINVAL;
1577 goto fail;
20d97356
BS
1578 }
1579 out_len = strm.next_out - out_buf;
1580
1581 deflateEnd(&strm);
1582
1583 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1584 /* could not compress: write normal cluster */
8f1efd00
KW
1585 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1586 if (ret < 0) {
1587 goto fail;
1588 }
20d97356
BS
1589 } else {
1590 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1591 sector_num << 9, out_len);
8f1efd00
KW
1592 if (!cluster_offset) {
1593 ret = -EIO;
1594 goto fail;
1595 }
20d97356 1596 cluster_offset &= s->cluster_offset_mask;
66f82cee 1597 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
8f1efd00
KW
1598 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1599 if (ret < 0) {
1600 goto fail;
20d97356
BS
1601 }
1602 }
1603
8f1efd00
KW
1604 ret = 0;
1605fail:
7267c094 1606 g_free(out_buf);
8f1efd00 1607 return ret;
20d97356
BS
1608}
1609
a968168c 1610static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
20d97356 1611{
29c1a730
KW
1612 BDRVQcowState *s = bs->opaque;
1613 int ret;
1614
8b94ff85 1615 qemu_co_mutex_lock(&s->lock);
29c1a730
KW
1616 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1617 if (ret < 0) {
c95de7e2 1618 qemu_co_mutex_unlock(&s->lock);
8b94ff85 1619 return ret;
29c1a730
KW
1620 }
1621
bfe8043e
SH
1622 if (qcow2_need_accurate_refcounts(s)) {
1623 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1624 if (ret < 0) {
1625 qemu_co_mutex_unlock(&s->lock);
1626 return ret;
1627 }
29c1a730 1628 }
8b94ff85 1629 qemu_co_mutex_unlock(&s->lock);
29c1a730 1630
eb489bb1
KW
1631 return 0;
1632}
1633
7c80ab3f 1634static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
20d97356
BS
1635{
1636 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1637}
1638
7c80ab3f 1639static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
20d97356
BS
1640{
1641 BDRVQcowState *s = bs->opaque;
1642 bdi->cluster_size = s->cluster_size;
7c80ab3f 1643 bdi->vm_state_offset = qcow2_vm_state_offset(s);
20d97356
BS
1644 return 0;
1645}
1646
20d97356
BS
1647#if 0
1648static void dump_refcounts(BlockDriverState *bs)
1649{
1650 BDRVQcowState *s = bs->opaque;
1651 int64_t nb_clusters, k, k1, size;
1652 int refcount;
1653
66f82cee 1654 size = bdrv_getlength(bs->file);
20d97356
BS
1655 nb_clusters = size_to_clusters(s, size);
1656 for(k = 0; k < nb_clusters;) {
1657 k1 = k;
1658 refcount = get_refcount(bs, k);
1659 k++;
1660 while (k < nb_clusters && get_refcount(bs, k) == refcount)
1661 k++;
0bfcd599
BS
1662 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1663 k - k1);
20d97356
BS
1664 }
1665}
1666#endif
1667
cf8074b3
KW
1668static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
1669 int64_t pos)
20d97356
BS
1670{
1671 BDRVQcowState *s = bs->opaque;
1672 int growable = bs->growable;
1673 int ret;
1674
66f82cee 1675 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
20d97356 1676 bs->growable = 1;
8d3b1a2d 1677 ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
20d97356
BS
1678 bs->growable = growable;
1679
1680 return ret;
1681}
1682
7c80ab3f
JS
1683static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1684 int64_t pos, int size)
20d97356
BS
1685{
1686 BDRVQcowState *s = bs->opaque;
1687 int growable = bs->growable;
1688 int ret;
1689
66f82cee 1690 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
20d97356 1691 bs->growable = 1;
7c80ab3f 1692 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
20d97356
BS
1693 bs->growable = growable;
1694
1695 return ret;
1696}
1697
7c80ab3f 1698static QEMUOptionParameter qcow2_create_options[] = {
20d97356
BS
1699 {
1700 .name = BLOCK_OPT_SIZE,
1701 .type = OPT_SIZE,
1702 .help = "Virtual disk size"
1703 },
6744cbab
KW
1704 {
1705 .name = BLOCK_OPT_COMPAT_LEVEL,
1706 .type = OPT_STRING,
1707 .help = "Compatibility level (0.10 or 1.1)"
1708 },
20d97356
BS
1709 {
1710 .name = BLOCK_OPT_BACKING_FILE,
1711 .type = OPT_STRING,
1712 .help = "File name of a base image"
1713 },
1714 {
1715 .name = BLOCK_OPT_BACKING_FMT,
1716 .type = OPT_STRING,
1717 .help = "Image format of the base image"
1718 },
1719 {
1720 .name = BLOCK_OPT_ENCRYPT,
1721 .type = OPT_FLAG,
1722 .help = "Encrypt the image"
1723 },
1724 {
1725 .name = BLOCK_OPT_CLUSTER_SIZE,
1726 .type = OPT_SIZE,
99cce9fa
KW
1727 .help = "qcow2 cluster size",
1728 .value = { .n = DEFAULT_CLUSTER_SIZE },
20d97356
BS
1729 },
1730 {
1731 .name = BLOCK_OPT_PREALLOC,
1732 .type = OPT_STRING,
1733 .help = "Preallocation mode (allowed values: off, metadata)"
1734 },
bfe8043e
SH
1735 {
1736 .name = BLOCK_OPT_LAZY_REFCOUNTS,
1737 .type = OPT_FLAG,
1738 .help = "Postpone refcount updates",
1739 },
20d97356
BS
1740 { NULL }
1741};
1742
1743static BlockDriver bdrv_qcow2 = {
7c80ab3f
JS
1744 .format_name = "qcow2",
1745 .instance_size = sizeof(BDRVQcowState),
1746 .bdrv_probe = qcow2_probe,
1747 .bdrv_open = qcow2_open,
1748 .bdrv_close = qcow2_close,
21d82ac9 1749 .bdrv_reopen_prepare = qcow2_reopen_prepare,
7c80ab3f 1750 .bdrv_create = qcow2_create,
f8a2e5e3 1751 .bdrv_co_is_allocated = qcow2_co_is_allocated,
7c80ab3f
JS
1752 .bdrv_set_key = qcow2_set_key,
1753 .bdrv_make_empty = qcow2_make_empty,
1754
c68b89ac
KW
1755 .bdrv_co_readv = qcow2_co_readv,
1756 .bdrv_co_writev = qcow2_co_writev,
eb489bb1 1757 .bdrv_co_flush_to_os = qcow2_co_flush_to_os,
419b19d9 1758
621f0589 1759 .bdrv_co_write_zeroes = qcow2_co_write_zeroes,
6db39ae2 1760 .bdrv_co_discard = qcow2_co_discard,
419b19d9 1761 .bdrv_truncate = qcow2_truncate,
7c80ab3f 1762 .bdrv_write_compressed = qcow2_write_compressed,
20d97356
BS
1763
1764 .bdrv_snapshot_create = qcow2_snapshot_create,
1765 .bdrv_snapshot_goto = qcow2_snapshot_goto,
1766 .bdrv_snapshot_delete = qcow2_snapshot_delete,
1767 .bdrv_snapshot_list = qcow2_snapshot_list,
51ef6727 1768 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
7c80ab3f 1769 .bdrv_get_info = qcow2_get_info,
20d97356 1770
7c80ab3f
JS
1771 .bdrv_save_vmstate = qcow2_save_vmstate,
1772 .bdrv_load_vmstate = qcow2_load_vmstate,
20d97356
BS
1773
1774 .bdrv_change_backing_file = qcow2_change_backing_file,
1775
06d9260f
AL
1776 .bdrv_invalidate_cache = qcow2_invalidate_cache,
1777
7c80ab3f
JS
1778 .create_options = qcow2_create_options,
1779 .bdrv_check = qcow2_check,
20d97356
BS
1780};
1781
5efa9d5a
AL
1782static void bdrv_qcow2_init(void)
1783{
1784 bdrv_register(&bdrv_qcow2);
1785}
1786
1787block_init(bdrv_qcow2_init);