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