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