]> git.proxmox.com Git - qemu.git/blame - block/qcow2.c
qcow2: Removed unused AIOCB fields
[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"
585f8587 25#include "block_int.h"
5efa9d5a 26#include "module.h"
585f8587
FB
27#include <zlib.h>
28#include "aes.h"
f7d0fe02 29#include "block/qcow2.h"
a9420734 30#include "qemu-error.h"
e8cdcec1 31#include "qerror.h"
585f8587
FB
32
33/*
34 Differences with QCOW:
35
36 - Support for multiple incremental snapshots.
37 - Memory management by reference counts.
38 - Clusters which have a reference count of one have the bit
39 QCOW_OFLAG_COPIED to optimize write performance.
5fafdf24 40 - Size of compressed clusters is stored in sectors to reduce bit usage
585f8587
FB
41 in the cluster offsets.
42 - Support for storing additional data (such as the VM state) in the
3b46e624 43 snapshots.
585f8587
FB
44 - If a backing store is used, the cluster size is not constrained
45 (could be backported to QCOW).
46 - L2 tables have always a size of one cluster.
47*/
48
9b80ddf3
AL
49
50typedef struct {
51 uint32_t magic;
52 uint32_t len;
53} QCowExtension;
7c80ab3f
JS
54#define QCOW2_EXT_MAGIC_END 0
55#define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
9b80ddf3 56
7c80ab3f 57static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
585f8587
FB
58{
59 const QCowHeader *cow_header = (const void *)buf;
3b46e624 60
585f8587
FB
61 if (buf_size >= sizeof(QCowHeader) &&
62 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
e8cdcec1 63 be32_to_cpu(cow_header->version) >= QCOW_VERSION)
585f8587
FB
64 return 100;
65 else
66 return 0;
67}
68
9b80ddf3
AL
69
70/*
71 * read qcow2 extension and fill bs
72 * start reading from start_offset
73 * finish reading upon magic of value 0 or when end_offset reached
74 * unknown magic is skipped (future extension this version knows nothing about)
75 * return 0 upon success, non-0 otherwise
76 */
7c80ab3f
JS
77static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
78 uint64_t end_offset)
9b80ddf3 79{
9b80ddf3
AL
80 QCowExtension ext;
81 uint64_t offset;
82
83#ifdef DEBUG_EXT
7c80ab3f 84 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
9b80ddf3
AL
85#endif
86 offset = start_offset;
87 while (offset < end_offset) {
88
89#ifdef DEBUG_EXT
6cbc3031 90 BDRVQcowState *s = bs->opaque;
9b80ddf3
AL
91 /* Sanity check */
92 if (offset > s->cluster_size)
7c80ab3f 93 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
9b80ddf3
AL
94
95 printf("attemting to read extended header in offset %lu\n", offset);
96#endif
97
66f82cee 98 if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
7c80ab3f 99 fprintf(stderr, "qcow2_read_extension: ERROR: "
0bfcd599
BS
100 "pread fail from offset %" PRIu64 "\n",
101 offset);
9b80ddf3
AL
102 return 1;
103 }
104 be32_to_cpus(&ext.magic);
105 be32_to_cpus(&ext.len);
106 offset += sizeof(ext);
107#ifdef DEBUG_EXT
108 printf("ext.magic = 0x%x\n", ext.magic);
109#endif
110 switch (ext.magic) {
7c80ab3f 111 case QCOW2_EXT_MAGIC_END:
9b80ddf3 112 return 0;
f965509c 113
7c80ab3f 114 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
f965509c
AL
115 if (ext.len >= sizeof(bs->backing_format)) {
116 fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
4c978075 117 " (>=%zu)\n",
f965509c
AL
118 ext.len, sizeof(bs->backing_format));
119 return 2;
120 }
66f82cee 121 if (bdrv_pread(bs->file, offset , bs->backing_format,
f965509c
AL
122 ext.len) != ext.len)
123 return 3;
124 bs->backing_format[ext.len] = '\0';
125#ifdef DEBUG_EXT
126 printf("Qcow2: Got format extension %s\n", bs->backing_format);
127#endif
e1c7f0e3 128 offset = ((offset + ext.len + 7) & ~7);
f965509c
AL
129 break;
130
9b80ddf3
AL
131 default:
132 /* unknown magic -- just skip it */
e1c7f0e3 133 offset = ((offset + ext.len + 7) & ~7);
9b80ddf3
AL
134 break;
135 }
136 }
137
138 return 0;
139}
140
141
7c80ab3f 142static int qcow2_open(BlockDriverState *bs, int flags)
585f8587
FB
143{
144 BDRVQcowState *s = bs->opaque;
6d85a57e 145 int len, i, ret = 0;
585f8587 146 QCowHeader header;
9b80ddf3 147 uint64_t ext_end;
29c1a730 148 bool writethrough;
585f8587 149
6d85a57e
JS
150 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
151 if (ret < 0) {
585f8587 152 goto fail;
6d85a57e 153 }
585f8587
FB
154 be32_to_cpus(&header.magic);
155 be32_to_cpus(&header.version);
156 be64_to_cpus(&header.backing_file_offset);
157 be32_to_cpus(&header.backing_file_size);
158 be64_to_cpus(&header.size);
159 be32_to_cpus(&header.cluster_bits);
160 be32_to_cpus(&header.crypt_method);
161 be64_to_cpus(&header.l1_table_offset);
162 be32_to_cpus(&header.l1_size);
163 be64_to_cpus(&header.refcount_table_offset);
164 be32_to_cpus(&header.refcount_table_clusters);
165 be64_to_cpus(&header.snapshots_offset);
166 be32_to_cpus(&header.nb_snapshots);
3b46e624 167
e8cdcec1 168 if (header.magic != QCOW_MAGIC) {
6d85a57e 169 ret = -EINVAL;
585f8587 170 goto fail;
6d85a57e 171 }
e8cdcec1
KW
172 if (header.version != QCOW_VERSION) {
173 char version[64];
174 snprintf(version, sizeof(version), "QCOW version %d", header.version);
175 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
176 bs->device_name, "qcow2", version);
177 ret = -ENOTSUP;
178 goto fail;
179 }
d191d12d 180 if (header.cluster_bits < MIN_CLUSTER_BITS ||
6d85a57e
JS
181 header.cluster_bits > MAX_CLUSTER_BITS) {
182 ret = -EINVAL;
585f8587 183 goto fail;
6d85a57e
JS
184 }
185 if (header.crypt_method > QCOW_CRYPT_AES) {
186 ret = -EINVAL;
585f8587 187 goto fail;
6d85a57e 188 }
585f8587 189 s->crypt_method_header = header.crypt_method;
6d85a57e 190 if (s->crypt_method_header) {
585f8587 191 bs->encrypted = 1;
6d85a57e 192 }
585f8587
FB
193 s->cluster_bits = header.cluster_bits;
194 s->cluster_size = 1 << s->cluster_bits;
195 s->cluster_sectors = 1 << (s->cluster_bits - 9);
196 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
197 s->l2_size = 1 << s->l2_bits;
198 bs->total_sectors = header.size / 512;
199 s->csize_shift = (62 - (s->cluster_bits - 8));
200 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
201 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
202 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 203 s->refcount_table_size =
585f8587
FB
204 header.refcount_table_clusters << (s->cluster_bits - 3);
205
206 s->snapshots_offset = header.snapshots_offset;
207 s->nb_snapshots = header.nb_snapshots;
208
209 /* read the level 1 table */
210 s->l1_size = header.l1_size;
419b19d9 211 s->l1_vm_state_index = size_to_l1(s, header.size);
585f8587
FB
212 /* the L1 table must contain at least enough entries to put
213 header.size bytes */
6d85a57e
JS
214 if (s->l1_size < s->l1_vm_state_index) {
215 ret = -EINVAL;
585f8587 216 goto fail;
6d85a57e 217 }
585f8587 218 s->l1_table_offset = header.l1_table_offset;
d191d12d 219 if (s->l1_size > 0) {
7267c094 220 s->l1_table = g_malloc0(
d191d12d 221 align_offset(s->l1_size * sizeof(uint64_t), 512));
6d85a57e
JS
222 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
223 s->l1_size * sizeof(uint64_t));
224 if (ret < 0) {
d191d12d 225 goto fail;
6d85a57e 226 }
d191d12d
SW
227 for(i = 0;i < s->l1_size; i++) {
228 be64_to_cpus(&s->l1_table[i]);
229 }
585f8587 230 }
29c1a730
KW
231
232 /* alloc L2 table/refcount block cache */
a6599793 233 writethrough = ((flags & BDRV_O_CACHE_WB) == 0);
29c1a730
KW
234 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE, writethrough);
235 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE,
236 writethrough);
237
7267c094 238 s->cluster_cache = g_malloc(s->cluster_size);
585f8587 239 /* one more sector for decompressed data alignment */
7267c094 240 s->cluster_data = g_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
095a9c58 241 + 512);
585f8587 242 s->cluster_cache_offset = -1;
3b46e624 243
6d85a57e
JS
244 ret = qcow2_refcount_init(bs);
245 if (ret != 0) {
585f8587 246 goto fail;
6d85a57e 247 }
585f8587 248
72cf2d4f 249 QLIST_INIT(&s->cluster_allocs);
f214978a 250
9b80ddf3 251 /* read qcow2 extensions */
6d85a57e 252 if (header.backing_file_offset) {
9b80ddf3 253 ext_end = header.backing_file_offset;
6d85a57e 254 } else {
9b80ddf3 255 ext_end = s->cluster_size;
6d85a57e
JS
256 }
257 if (qcow2_read_extensions(bs, sizeof(header), ext_end)) {
258 ret = -EINVAL;
9b80ddf3 259 goto fail;
6d85a57e 260 }
9b80ddf3 261
585f8587
FB
262 /* read the backing file name */
263 if (header.backing_file_offset != 0) {
264 len = header.backing_file_size;
6d85a57e 265 if (len > 1023) {
585f8587 266 len = 1023;
6d85a57e
JS
267 }
268 ret = bdrv_pread(bs->file, header.backing_file_offset,
269 bs->backing_file, len);
270 if (ret < 0) {
585f8587 271 goto fail;
6d85a57e 272 }
585f8587
FB
273 bs->backing_file[len] = '\0';
274 }
6d85a57e
JS
275 if (qcow2_read_snapshots(bs) < 0) {
276 ret = -EINVAL;
585f8587 277 goto fail;
6d85a57e 278 }
585f8587 279
68d100e9
KW
280 /* Initialise locks */
281 qemu_co_mutex_init(&s->lock);
282
585f8587 283#ifdef DEBUG_ALLOC
6cbc3031
PH
284 {
285 BdrvCheckResult result = {0};
286 qcow2_check_refcounts(bs, &result);
287 }
585f8587 288#endif
6d85a57e 289 return ret;
585f8587
FB
290
291 fail:
ed6ccf0f
KW
292 qcow2_free_snapshots(bs);
293 qcow2_refcount_close(bs);
7267c094 294 g_free(s->l1_table);
29c1a730
KW
295 if (s->l2_table_cache) {
296 qcow2_cache_destroy(bs, s->l2_table_cache);
297 }
7267c094
AL
298 g_free(s->cluster_cache);
299 g_free(s->cluster_data);
6d85a57e 300 return ret;
585f8587
FB
301}
302
7c80ab3f 303static int qcow2_set_key(BlockDriverState *bs, const char *key)
585f8587
FB
304{
305 BDRVQcowState *s = bs->opaque;
306 uint8_t keybuf[16];
307 int len, i;
3b46e624 308
585f8587
FB
309 memset(keybuf, 0, 16);
310 len = strlen(key);
311 if (len > 16)
312 len = 16;
313 /* XXX: we could compress the chars to 7 bits to increase
314 entropy */
315 for(i = 0;i < len;i++) {
316 keybuf[i] = key[i];
317 }
318 s->crypt_method = s->crypt_method_header;
319
320 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
321 return -1;
322 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
323 return -1;
324#if 0
325 /* test */
326 {
327 uint8_t in[16];
328 uint8_t out[16];
329 uint8_t tmp[16];
330 for(i=0;i<16;i++)
331 in[i] = i;
332 AES_encrypt(in, tmp, &s->aes_encrypt_key);
333 AES_decrypt(tmp, out, &s->aes_decrypt_key);
334 for(i = 0; i < 16; i++)
335 printf(" %02x", tmp[i]);
336 printf("\n");
337 for(i = 0; i < 16; i++)
338 printf(" %02x", out[i]);
339 printf("\n");
340 }
341#endif
342 return 0;
343}
344
7c80ab3f
JS
345static int qcow2_is_allocated(BlockDriverState *bs, int64_t sector_num,
346 int nb_sectors, int *pnum)
585f8587 347{
585f8587 348 uint64_t cluster_offset;
1c46efaa 349 int ret;
585f8587 350
095a9c58 351 *pnum = nb_sectors;
1c46efaa
KW
352 /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
353 * pass them on today */
354 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
355 if (ret < 0) {
356 *pnum = 0;
357 }
095a9c58 358
585f8587
FB
359 return (cluster_offset != 0);
360}
361
a9465922 362/* handle reading after the end of the backing file */
bd28f835
KW
363int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
364 int64_t sector_num, int nb_sectors)
a9465922
FB
365{
366 int n1;
367 if ((sector_num + nb_sectors) <= bs->total_sectors)
368 return nb_sectors;
369 if (sector_num >= bs->total_sectors)
370 n1 = 0;
371 else
372 n1 = bs->total_sectors - sector_num;
bd28f835 373
e0d9c6f9 374 qemu_iovec_memset_skip(qiov, 0, 512 * (nb_sectors - n1), 512 * n1);
bd28f835 375
a9465922
FB
376 return n1;
377}
378
ce1a14dc
PB
379typedef struct QCowAIOCB {
380 BlockDriverAIOCB common;
585f8587 381 int64_t sector_num;
f141eafe 382 QEMUIOVector *qiov;
7b88e48b
CH
383 int remaining_sectors;
384 int cur_nr_sectors; /* number of sectors in current iteration */
bd28f835 385 uint64_t bytes_done;
585f8587 386 uint64_t cluster_offset;
5fafdf24 387 uint8_t *cluster_data;
c87c0672 388 QEMUIOVector hd_qiov;
e976c6a1 389 QCowL2Meta l2meta;
585f8587
FB
390} QCowAIOCB;
391
68d100e9
KW
392/*
393 * Returns 0 when the request is completed successfully, 1 when there is still
394 * a part left to do and -errno in error cases.
395 */
396static int qcow2_aio_read_cb(QCowAIOCB *acb)
585f8587 397{
ce1a14dc 398 BlockDriverState *bs = acb->common.bs;
585f8587 399 BDRVQcowState *s = bs->opaque;
a9465922 400 int index_in_cluster, n1;
68d100e9 401 int ret;
585f8587 402
585f8587 403 /* post process the read buffer */
ce1a14dc 404 if (!acb->cluster_offset) {
585f8587 405 /* nothing to do */
ce1a14dc 406 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
585f8587
FB
407 /* nothing to do */
408 } else {
409 if (s->crypt_method) {
bd28f835
KW
410 qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
411 acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key);
412 qemu_iovec_reset(&acb->hd_qiov);
413 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
414 acb->cur_nr_sectors * 512);
415 qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data,
416 512 * acb->cur_nr_sectors);
585f8587
FB
417 }
418 }
419
7b88e48b
CH
420 acb->remaining_sectors -= acb->cur_nr_sectors;
421 acb->sector_num += acb->cur_nr_sectors;
bd28f835 422 acb->bytes_done += acb->cur_nr_sectors * 512;
585f8587 423
7b88e48b 424 if (acb->remaining_sectors == 0) {
585f8587 425 /* request completed */
68d100e9 426 return 0;
585f8587 427 }
3b46e624 428
585f8587 429 /* prepare next AIO request */
7b88e48b 430 acb->cur_nr_sectors = acb->remaining_sectors;
bd28f835
KW
431 if (s->crypt_method) {
432 acb->cur_nr_sectors = MIN(acb->cur_nr_sectors,
433 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
434 }
435
1c46efaa
KW
436 ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9,
437 &acb->cur_nr_sectors, &acb->cluster_offset);
438 if (ret < 0) {
68d100e9 439 return ret;
1c46efaa
KW
440 }
441
ce1a14dc 442 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
ce1a14dc 443
bd28f835
KW
444 qemu_iovec_reset(&acb->hd_qiov);
445 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
446 acb->cur_nr_sectors * 512);
447
ce1a14dc 448 if (!acb->cluster_offset) {
bd28f835 449
585f8587
FB
450 if (bs->backing_hd) {
451 /* read from the base image */
bd28f835
KW
452 n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov,
453 acb->sector_num, acb->cur_nr_sectors);
a9465922 454 if (n1 > 0) {
66f82cee 455 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
68d100e9
KW
456 qemu_co_mutex_unlock(&s->lock);
457 ret = bdrv_co_readv(bs->backing_hd, acb->sector_num,
458 n1, &acb->hd_qiov);
459 qemu_co_mutex_lock(&s->lock);
460 if (ret < 0) {
461 return ret;
3ab4c7e9 462 }
a9465922 463 }
68d100e9 464 return 1;
585f8587
FB
465 } else {
466 /* Note: in this case, no need to wait */
bd28f835 467 qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors);
68d100e9 468 return 1;
585f8587 469 }
ce1a14dc 470 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
585f8587 471 /* add AIO support for compressed blocks ? */
8af36488
KW
472 ret = qcow2_decompress_cluster(bs, acb->cluster_offset);
473 if (ret < 0) {
68d100e9 474 return ret;
8af36488 475 }
bd28f835
KW
476
477 qemu_iovec_from_buffer(&acb->hd_qiov,
478 s->cluster_cache + index_in_cluster * 512,
479 512 * acb->cur_nr_sectors);
480
68d100e9 481 return 1;
585f8587 482 } else {
ce1a14dc 483 if ((acb->cluster_offset & 511) != 0) {
68d100e9 484 return -EIO;
585f8587 485 }
c87c0672 486
bd28f835
KW
487 if (s->crypt_method) {
488 /*
489 * For encrypted images, read everything into a temporary
490 * contiguous buffer on which the AES functions can work.
491 */
492 if (!acb->cluster_data) {
493 acb->cluster_data =
7267c094 494 g_malloc0(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
bd28f835
KW
495 }
496
497 assert(acb->cur_nr_sectors <=
498 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
499 qemu_iovec_reset(&acb->hd_qiov);
500 qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
501 512 * acb->cur_nr_sectors);
502 }
503
66f82cee 504 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
68d100e9
KW
505 qemu_co_mutex_unlock(&s->lock);
506 ret = bdrv_co_readv(bs->file,
5fafdf24 507 (acb->cluster_offset >> 9) + index_in_cluster,
68d100e9
KW
508 acb->cur_nr_sectors, &acb->hd_qiov);
509 qemu_co_mutex_lock(&s->lock);
510 if (ret < 0) {
511 return ret;
171e3d6b 512 }
f141eafe
AL
513 }
514
68d100e9 515 return 1;
585f8587
FB
516}
517
7c80ab3f
JS
518static QCowAIOCB *qcow2_aio_setup(BlockDriverState *bs, int64_t sector_num,
519 QEMUIOVector *qiov, int nb_sectors,
520 BlockDriverCompletionFunc *cb,
4617310c 521 void *opaque, QCowAIOCB *acb)
585f8587 522{
f5cd8173
FZ
523 memset(acb, 0, sizeof(*acb));
524 acb->common.bs = bs;
ce1a14dc 525 acb->sector_num = sector_num;
f141eafe 526 acb->qiov = qiov;
bd28f835 527
6f5f060b 528 qemu_iovec_init(&acb->hd_qiov, qiov->niov);
bd28f835
KW
529
530 acb->bytes_done = 0;
7b88e48b
CH
531 acb->remaining_sectors = nb_sectors;
532 acb->cur_nr_sectors = 0;
ce1a14dc 533 acb->cluster_offset = 0;
e976c6a1 534 acb->l2meta.nb_clusters = 0;
68d100e9 535 qemu_co_queue_init(&acb->l2meta.dependent_requests);
ce1a14dc
PB
536 return acb;
537}
538
68d100e9
KW
539static int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
540 int nb_sectors, QEMUIOVector *qiov)
ce1a14dc 541{
68d100e9 542 BDRVQcowState *s = bs->opaque;
f5cd8173 543 QCowAIOCB acb;
42496d62 544 int ret;
ce1a14dc 545
4617310c 546 qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, NULL, NULL, &acb);
585f8587 547
68d100e9
KW
548 qemu_co_mutex_lock(&s->lock);
549 do {
f5cd8173 550 ret = qcow2_aio_read_cb(&acb);
68d100e9
KW
551 } while (ret > 0);
552 qemu_co_mutex_unlock(&s->lock);
42496d62 553
f5cd8173 554 qemu_iovec_destroy(&acb.hd_qiov);
68d100e9
KW
555
556 return ret;
585f8587
FB
557}
558
68d100e9 559static void run_dependent_requests(BDRVQcowState *s, QCowL2Meta *m)
f214978a 560{
f214978a
KW
561 /* Take the request off the list of running requests */
562 if (m->nb_clusters != 0) {
72cf2d4f 563 QLIST_REMOVE(m, next_in_flight);
f214978a
KW
564 }
565
d4c146f0 566 /* Restart all dependent requests */
68d100e9
KW
567 if (!qemu_co_queue_empty(&m->dependent_requests)) {
568 qemu_co_mutex_unlock(&s->lock);
569 while(qemu_co_queue_next(&m->dependent_requests));
570 qemu_co_mutex_lock(&s->lock);
f214978a 571 }
f214978a
KW
572}
573
68d100e9
KW
574/*
575 * Returns 0 when the request is completed successfully, 1 when there is still
576 * a part left to do and -errno in error cases.
577 */
578static int qcow2_aio_write_cb(QCowAIOCB *acb)
585f8587 579{
ce1a14dc 580 BlockDriverState *bs = acb->common.bs;
585f8587 581 BDRVQcowState *s = bs->opaque;
585f8587 582 int index_in_cluster;
095a9c58 583 int n_end;
68d100e9 584 int ret;
ce1a14dc 585
68d100e9 586 ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta);
f214978a 587
68d100e9 588 run_dependent_requests(s, &acb->l2meta);
f214978a 589
68d100e9
KW
590 if (ret < 0) {
591 return ret;
592 }
585f8587 593
7b88e48b
CH
594 acb->remaining_sectors -= acb->cur_nr_sectors;
595 acb->sector_num += acb->cur_nr_sectors;
6f5f060b 596 acb->bytes_done += acb->cur_nr_sectors * 512;
585f8587 597
7b88e48b 598 if (acb->remaining_sectors == 0) {
585f8587 599 /* request completed */
68d100e9 600 return 0;
585f8587 601 }
3b46e624 602
ce1a14dc 603 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
7b88e48b 604 n_end = index_in_cluster + acb->remaining_sectors;
095a9c58
AL
605 if (s->crypt_method &&
606 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
607 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
608
148da7ea 609 ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
7b88e48b 610 index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta);
148da7ea 611 if (ret < 0) {
68d100e9 612 return ret;
148da7ea
KW
613 }
614
615 acb->cluster_offset = acb->l2meta.cluster_offset;
148da7ea
KW
616 assert((acb->cluster_offset & 511) == 0);
617
6f5f060b
KW
618 qemu_iovec_reset(&acb->hd_qiov);
619 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
620 acb->cur_nr_sectors * 512);
621
585f8587 622 if (s->crypt_method) {
ce1a14dc 623 if (!acb->cluster_data) {
7267c094 624 acb->cluster_data = g_malloc0(QCOW_MAX_CRYPT_CLUSTERS *
095a9c58 625 s->cluster_size);
585f8587 626 }
6f5f060b
KW
627
628 assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
629 qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data);
630
631 qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
632 acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key);
633
634 qemu_iovec_reset(&acb->hd_qiov);
635 qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
636 acb->cur_nr_sectors * 512);
585f8587 637 }
6f5f060b 638
66f82cee 639 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
68d100e9
KW
640 qemu_co_mutex_unlock(&s->lock);
641 ret = bdrv_co_writev(bs->file,
642 (acb->cluster_offset >> 9) + index_in_cluster,
643 acb->cur_nr_sectors, &acb->hd_qiov);
644 qemu_co_mutex_lock(&s->lock);
645 if (ret < 0) {
646 return ret;
171e3d6b 647 }
f141eafe 648
68d100e9 649 return 1;
585f8587
FB
650}
651
68d100e9
KW
652static int qcow2_co_writev(BlockDriverState *bs,
653 int64_t sector_num,
654 int nb_sectors,
655 QEMUIOVector *qiov)
585f8587 656{
585f8587 657 BDRVQcowState *s = bs->opaque;
f5cd8173 658 QCowAIOCB acb;
42496d62 659 int ret;
3b46e624 660
4617310c 661 qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, NULL, NULL, &acb);
585f8587
FB
662 s->cluster_cache_offset = -1; /* disable compressed cache */
663
68d100e9
KW
664 qemu_co_mutex_lock(&s->lock);
665 do {
f5cd8173 666 ret = qcow2_aio_write_cb(&acb);
68d100e9
KW
667 } while (ret > 0);
668 qemu_co_mutex_unlock(&s->lock);
3b46e624 669
f5cd8173 670 qemu_iovec_destroy(&acb.hd_qiov);
42496d62 671
68d100e9 672 return ret;
585f8587
FB
673}
674
7c80ab3f 675static void qcow2_close(BlockDriverState *bs)
585f8587
FB
676{
677 BDRVQcowState *s = bs->opaque;
7267c094 678 g_free(s->l1_table);
29c1a730
KW
679
680 qcow2_cache_flush(bs, s->l2_table_cache);
681 qcow2_cache_flush(bs, s->refcount_block_cache);
682
683 qcow2_cache_destroy(bs, s->l2_table_cache);
684 qcow2_cache_destroy(bs, s->refcount_block_cache);
685
7267c094
AL
686 g_free(s->cluster_cache);
687 g_free(s->cluster_data);
ed6ccf0f 688 qcow2_refcount_close(bs);
585f8587
FB
689}
690
756e6736
KW
691/*
692 * Updates the variable length parts of the qcow2 header, i.e. the backing file
693 * name and all extensions. qcow2 was not designed to allow such changes, so if
694 * we run out of space (we can only use the first cluster) this function may
695 * fail.
696 *
697 * Returns 0 on success, -errno in error cases.
698 */
699static int qcow2_update_ext_header(BlockDriverState *bs,
700 const char *backing_file, const char *backing_fmt)
701{
702 size_t backing_file_len = 0;
703 size_t backing_fmt_len = 0;
704 BDRVQcowState *s = bs->opaque;
705 QCowExtension ext_backing_fmt = {0, 0};
706 int ret;
707
708 /* Backing file format doesn't make sense without a backing file */
709 if (backing_fmt && !backing_file) {
710 return -EINVAL;
711 }
712
713 /* Prepare the backing file format extension if needed */
714 if (backing_fmt) {
715 ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt));
7c80ab3f 716 ext_backing_fmt.magic = cpu_to_be32(QCOW2_EXT_MAGIC_BACKING_FORMAT);
756e6736
KW
717 backing_fmt_len = ((sizeof(ext_backing_fmt)
718 + strlen(backing_fmt) + 7) & ~7);
719 }
720
721 /* Check if we can fit the new header into the first cluster */
722 if (backing_file) {
723 backing_file_len = strlen(backing_file);
724 }
725
726 size_t header_size = sizeof(QCowHeader) + backing_file_len
727 + backing_fmt_len;
728
729 if (header_size > s->cluster_size) {
730 return -ENOSPC;
731 }
732
733 /* Rewrite backing file name and qcow2 extensions */
734 size_t ext_size = header_size - sizeof(QCowHeader);
735 uint8_t buf[ext_size];
736 size_t offset = 0;
737 size_t backing_file_offset = 0;
738
739 if (backing_file) {
740 if (backing_fmt) {
741 int padding = backing_fmt_len -
742 (sizeof(ext_backing_fmt) + strlen(backing_fmt));
743
744 memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
745 offset += sizeof(ext_backing_fmt);
746
747 memcpy(buf + offset, backing_fmt, strlen(backing_fmt));
748 offset += strlen(backing_fmt);
749
750 memset(buf + offset, 0, padding);
751 offset += padding;
752 }
753
754 memcpy(buf + offset, backing_file, backing_file_len);
755 backing_file_offset = sizeof(QCowHeader) + offset;
756 }
757
8b3b7206 758 ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size);
756e6736
KW
759 if (ret < 0) {
760 goto fail;
761 }
762
763 /* Update header fields */
764 uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset);
765 uint32_t be_backing_file_size = cpu_to_be32(backing_file_len);
766
8b3b7206 767 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset),
756e6736
KW
768 &be_backing_file_offset, sizeof(uint64_t));
769 if (ret < 0) {
770 goto fail;
771 }
772
8b3b7206 773 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size),
756e6736
KW
774 &be_backing_file_size, sizeof(uint32_t));
775 if (ret < 0) {
776 goto fail;
777 }
778
779 ret = 0;
780fail:
781 return ret;
782}
783
784static int qcow2_change_backing_file(BlockDriverState *bs,
785 const char *backing_file, const char *backing_fmt)
786{
787 return qcow2_update_ext_header(bs, backing_file, backing_fmt);
788}
789
a35e1c17
KW
790static int preallocate(BlockDriverState *bs)
791{
a35e1c17
KW
792 uint64_t nb_sectors;
793 uint64_t offset;
794 int num;
148da7ea 795 int ret;
a35e1c17
KW
796 QCowL2Meta meta;
797
798 nb_sectors = bdrv_getlength(bs) >> 9;
799 offset = 0;
68d100e9 800 qemu_co_queue_init(&meta.dependent_requests);
148da7ea 801 meta.cluster_offset = 0;
a35e1c17
KW
802
803 while (nb_sectors) {
804 num = MIN(nb_sectors, INT_MAX >> 9);
148da7ea 805 ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
148da7ea 806 if (ret < 0) {
19dbcbf7 807 return ret;
a35e1c17
KW
808 }
809
19dbcbf7
KW
810 ret = qcow2_alloc_cluster_link_l2(bs, &meta);
811 if (ret < 0) {
148da7ea 812 qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
19dbcbf7 813 return ret;
a35e1c17
KW
814 }
815
f214978a
KW
816 /* There are no dependent requests, but we need to remove our request
817 * from the list of in-flight requests */
68d100e9 818 run_dependent_requests(bs->opaque, &meta);
f214978a 819
a35e1c17
KW
820 /* TODO Preallocate data if requested */
821
822 nb_sectors -= num;
823 offset += num << 9;
824 }
825
826 /*
827 * It is expected that the image file is large enough to actually contain
828 * all of the allocated clusters (otherwise we get failing reads after
829 * EOF). Extend the image to the last allocated sector.
830 */
148da7ea 831 if (meta.cluster_offset != 0) {
ea80b906
KW
832 uint8_t buf[512];
833 memset(buf, 0, 512);
19dbcbf7
KW
834 ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
835 if (ret < 0) {
836 return ret;
837 }
a35e1c17
KW
838 }
839
840 return 0;
841}
842
7c80ab3f
JS
843static int qcow2_create2(const char *filename, int64_t total_size,
844 const char *backing_file, const char *backing_format,
845 int flags, size_t cluster_size, int prealloc,
846 QEMUOptionParameter *options)
a9420734
KW
847{
848 /* Calulate cluster_bits */
849 int cluster_bits;
850 cluster_bits = ffs(cluster_size) - 1;
851 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
852 (1 << cluster_bits) != cluster_size)
853 {
854 error_report(
6daf194d 855 "Cluster size must be a power of two between %d and %dk",
a9420734
KW
856 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
857 return -EINVAL;
858 }
859
860 /*
861 * Open the image file and write a minimal qcow2 header.
862 *
863 * We keep things simple and start with a zero-sized image. We also
864 * do without refcount blocks or a L1 table for now. We'll fix the
865 * inconsistency later.
866 *
867 * We do need a refcount table because growing the refcount table means
868 * allocating two new refcount blocks - the seconds of which would be at
869 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
870 * size for any qcow2 image.
871 */
872 BlockDriverState* bs;
873 QCowHeader header;
874 uint8_t* refcount_table;
875 int ret;
876
877 ret = bdrv_create_file(filename, options);
878 if (ret < 0) {
879 return ret;
880 }
881
882 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
883 if (ret < 0) {
884 return ret;
885 }
886
887 /* Write the header */
888 memset(&header, 0, sizeof(header));
889 header.magic = cpu_to_be32(QCOW_MAGIC);
890 header.version = cpu_to_be32(QCOW_VERSION);
891 header.cluster_bits = cpu_to_be32(cluster_bits);
892 header.size = cpu_to_be64(0);
893 header.l1_table_offset = cpu_to_be64(0);
894 header.l1_size = cpu_to_be32(0);
895 header.refcount_table_offset = cpu_to_be64(cluster_size);
896 header.refcount_table_clusters = cpu_to_be32(1);
897
898 if (flags & BLOCK_FLAG_ENCRYPT) {
899 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
900 } else {
901 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
902 }
903
904 ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
905 if (ret < 0) {
906 goto out;
907 }
908
909 /* Write an empty refcount table */
7267c094 910 refcount_table = g_malloc0(cluster_size);
a9420734 911 ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
7267c094 912 g_free(refcount_table);
a9420734
KW
913
914 if (ret < 0) {
915 goto out;
916 }
917
918 bdrv_close(bs);
919
920 /*
921 * And now open the image and make it consistent first (i.e. increase the
922 * refcount of the cluster that is occupied by the header and the refcount
923 * table)
924 */
925 BlockDriver* drv = bdrv_find_format("qcow2");
926 assert(drv != NULL);
e1a7107f
KW
927 ret = bdrv_open(bs, filename,
928 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
a9420734
KW
929 if (ret < 0) {
930 goto out;
931 }
932
933 ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
934 if (ret < 0) {
935 goto out;
936
937 } else if (ret != 0) {
938 error_report("Huh, first cluster in empty image is already in use?");
939 abort();
940 }
941
942 /* Okay, now that we have a valid image, let's give it the right size */
943 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
944 if (ret < 0) {
945 goto out;
946 }
947
948 /* Want a backing file? There you go.*/
949 if (backing_file) {
950 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
951 if (ret < 0) {
952 goto out;
953 }
954 }
955
956 /* And if we're supposed to preallocate metadata, do that now */
957 if (prealloc) {
958 ret = preallocate(bs);
959 if (ret < 0) {
960 goto out;
961 }
962 }
963
964 ret = 0;
965out:
966 bdrv_delete(bs);
967 return ret;
968}
de5f3f40 969
7c80ab3f 970static int qcow2_create(const char *filename, QEMUOptionParameter *options)
de5f3f40
KW
971{
972 const char *backing_file = NULL;
973 const char *backing_fmt = NULL;
974 uint64_t sectors = 0;
975 int flags = 0;
99cce9fa 976 size_t cluster_size = DEFAULT_CLUSTER_SIZE;
de5f3f40
KW
977 int prealloc = 0;
978
979 /* Read out options */
980 while (options && options->name) {
981 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
982 sectors = options->value.n / 512;
983 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
984 backing_file = options->value.s;
985 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
986 backing_fmt = options->value.s;
987 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
988 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
989 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
990 if (options->value.n) {
991 cluster_size = options->value.n;
992 }
993 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
994 if (!options->value.s || !strcmp(options->value.s, "off")) {
995 prealloc = 0;
996 } else if (!strcmp(options->value.s, "metadata")) {
997 prealloc = 1;
998 } else {
999 fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1000 options->value.s);
1001 return -EINVAL;
1002 }
1003 }
1004 options++;
1005 }
1006
1007 if (backing_file && prealloc) {
1008 fprintf(stderr, "Backing file and preallocation cannot be used at "
1009 "the same time\n");
1010 return -EINVAL;
1011 }
1012
7c80ab3f
JS
1013 return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1014 cluster_size, prealloc, options);
de5f3f40
KW
1015}
1016
7c80ab3f 1017static int qcow2_make_empty(BlockDriverState *bs)
20d97356
BS
1018{
1019#if 0
1020 /* XXX: not correct */
1021 BDRVQcowState *s = bs->opaque;
1022 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1023 int ret;
1024
1025 memset(s->l1_table, 0, l1_length);
66f82cee 1026 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
20d97356 1027 return -1;
66f82cee 1028 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
20d97356
BS
1029 if (ret < 0)
1030 return ret;
1031
1032 l2_cache_reset(bs);
1033#endif
1034 return 0;
1035}
1036
5ea929e3
KW
1037static int qcow2_discard(BlockDriverState *bs, int64_t sector_num,
1038 int nb_sectors)
1039{
1040 return qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1041 nb_sectors);
1042}
1043
419b19d9
SH
1044static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1045{
1046 BDRVQcowState *s = bs->opaque;
1047 int ret, new_l1_size;
1048
1049 if (offset & 511) {
1050 return -EINVAL;
1051 }
1052
1053 /* cannot proceed if image has snapshots */
1054 if (s->nb_snapshots) {
1055 return -ENOTSUP;
1056 }
1057
1058 /* shrinking is currently not supported */
1059 if (offset < bs->total_sectors * 512) {
1060 return -ENOTSUP;
1061 }
1062
1063 new_l1_size = size_to_l1(s, offset);
72893756 1064 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
419b19d9
SH
1065 if (ret < 0) {
1066 return ret;
1067 }
1068
1069 /* write updated header.size */
1070 offset = cpu_to_be64(offset);
8b3b7206
KW
1071 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1072 &offset, sizeof(uint64_t));
419b19d9
SH
1073 if (ret < 0) {
1074 return ret;
1075 }
1076
1077 s->l1_vm_state_index = new_l1_size;
1078 return 0;
1079}
1080
20d97356
BS
1081/* XXX: put compressed sectors first, then all the cluster aligned
1082 tables to avoid losing bytes in alignment */
7c80ab3f
JS
1083static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1084 const uint8_t *buf, int nb_sectors)
20d97356
BS
1085{
1086 BDRVQcowState *s = bs->opaque;
1087 z_stream strm;
1088 int ret, out_len;
1089 uint8_t *out_buf;
1090 uint64_t cluster_offset;
1091
1092 if (nb_sectors == 0) {
1093 /* align end of file to a sector boundary to ease reading with
1094 sector based I/Os */
66f82cee 1095 cluster_offset = bdrv_getlength(bs->file);
20d97356 1096 cluster_offset = (cluster_offset + 511) & ~511;
66f82cee 1097 bdrv_truncate(bs->file, cluster_offset);
20d97356
BS
1098 return 0;
1099 }
1100
1101 if (nb_sectors != s->cluster_sectors)
1102 return -EINVAL;
1103
7267c094 1104 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
20d97356
BS
1105
1106 /* best compression, small window, no zlib header */
1107 memset(&strm, 0, sizeof(strm));
1108 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1109 Z_DEFLATED, -12,
1110 9, Z_DEFAULT_STRATEGY);
1111 if (ret != 0) {
7267c094 1112 g_free(out_buf);
20d97356
BS
1113 return -1;
1114 }
1115
1116 strm.avail_in = s->cluster_size;
1117 strm.next_in = (uint8_t *)buf;
1118 strm.avail_out = s->cluster_size;
1119 strm.next_out = out_buf;
1120
1121 ret = deflate(&strm, Z_FINISH);
1122 if (ret != Z_STREAM_END && ret != Z_OK) {
7267c094 1123 g_free(out_buf);
20d97356
BS
1124 deflateEnd(&strm);
1125 return -1;
1126 }
1127 out_len = strm.next_out - out_buf;
1128
1129 deflateEnd(&strm);
1130
1131 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1132 /* could not compress: write normal cluster */
1133 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1134 } else {
1135 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1136 sector_num << 9, out_len);
1137 if (!cluster_offset)
1138 return -1;
1139 cluster_offset &= s->cluster_offset_mask;
66f82cee
KW
1140 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1141 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
7267c094 1142 g_free(out_buf);
20d97356
BS
1143 return -1;
1144 }
1145 }
1146
7267c094 1147 g_free(out_buf);
20d97356
BS
1148 return 0;
1149}
1150
7c80ab3f 1151static int qcow2_flush(BlockDriverState *bs)
20d97356 1152{
29c1a730
KW
1153 BDRVQcowState *s = bs->opaque;
1154 int ret;
1155
1156 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1157 if (ret < 0) {
1158 return ret;
1159 }
1160
1161 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1162 if (ret < 0) {
1163 return ret;
1164 }
1165
205ef796 1166 return bdrv_flush(bs->file);
20d97356
BS
1167}
1168
7c80ab3f
JS
1169static BlockDriverAIOCB *qcow2_aio_flush(BlockDriverState *bs,
1170 BlockDriverCompletionFunc *cb,
1171 void *opaque)
20d97356 1172{
29c1a730
KW
1173 BDRVQcowState *s = bs->opaque;
1174 int ret;
1175
1176 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1177 if (ret < 0) {
1178 return NULL;
1179 }
1180
1181 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1182 if (ret < 0) {
1183 return NULL;
1184 }
1185
66f82cee 1186 return bdrv_aio_flush(bs->file, cb, opaque);
20d97356
BS
1187}
1188
7c80ab3f 1189static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
20d97356
BS
1190{
1191 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1192}
1193
7c80ab3f 1194static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
20d97356
BS
1195{
1196 BDRVQcowState *s = bs->opaque;
1197 bdi->cluster_size = s->cluster_size;
7c80ab3f 1198 bdi->vm_state_offset = qcow2_vm_state_offset(s);
20d97356
BS
1199 return 0;
1200}
1201
1202
7c80ab3f 1203static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result)
20d97356 1204{
9ac228e0 1205 return qcow2_check_refcounts(bs, result);
20d97356
BS
1206}
1207
1208#if 0
1209static void dump_refcounts(BlockDriverState *bs)
1210{
1211 BDRVQcowState *s = bs->opaque;
1212 int64_t nb_clusters, k, k1, size;
1213 int refcount;
1214
66f82cee 1215 size = bdrv_getlength(bs->file);
20d97356
BS
1216 nb_clusters = size_to_clusters(s, size);
1217 for(k = 0; k < nb_clusters;) {
1218 k1 = k;
1219 refcount = get_refcount(bs, k);
1220 k++;
1221 while (k < nb_clusters && get_refcount(bs, k) == refcount)
1222 k++;
0bfcd599
BS
1223 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1224 k - k1);
20d97356
BS
1225 }
1226}
1227#endif
1228
7c80ab3f
JS
1229static int qcow2_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1230 int64_t pos, int size)
20d97356
BS
1231{
1232 BDRVQcowState *s = bs->opaque;
1233 int growable = bs->growable;
1234 int ret;
1235
66f82cee 1236 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
20d97356 1237 bs->growable = 1;
7c80ab3f 1238 ret = bdrv_pwrite(bs, qcow2_vm_state_offset(s) + pos, buf, size);
20d97356
BS
1239 bs->growable = growable;
1240
1241 return ret;
1242}
1243
7c80ab3f
JS
1244static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1245 int64_t pos, int size)
20d97356
BS
1246{
1247 BDRVQcowState *s = bs->opaque;
1248 int growable = bs->growable;
1249 int ret;
1250
66f82cee 1251 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
20d97356 1252 bs->growable = 1;
7c80ab3f 1253 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
20d97356
BS
1254 bs->growable = growable;
1255
1256 return ret;
1257}
1258
7c80ab3f 1259static QEMUOptionParameter qcow2_create_options[] = {
20d97356
BS
1260 {
1261 .name = BLOCK_OPT_SIZE,
1262 .type = OPT_SIZE,
1263 .help = "Virtual disk size"
1264 },
1265 {
1266 .name = BLOCK_OPT_BACKING_FILE,
1267 .type = OPT_STRING,
1268 .help = "File name of a base image"
1269 },
1270 {
1271 .name = BLOCK_OPT_BACKING_FMT,
1272 .type = OPT_STRING,
1273 .help = "Image format of the base image"
1274 },
1275 {
1276 .name = BLOCK_OPT_ENCRYPT,
1277 .type = OPT_FLAG,
1278 .help = "Encrypt the image"
1279 },
1280 {
1281 .name = BLOCK_OPT_CLUSTER_SIZE,
1282 .type = OPT_SIZE,
99cce9fa
KW
1283 .help = "qcow2 cluster size",
1284 .value = { .n = DEFAULT_CLUSTER_SIZE },
20d97356
BS
1285 },
1286 {
1287 .name = BLOCK_OPT_PREALLOC,
1288 .type = OPT_STRING,
1289 .help = "Preallocation mode (allowed values: off, metadata)"
1290 },
1291 { NULL }
1292};
1293
1294static BlockDriver bdrv_qcow2 = {
7c80ab3f
JS
1295 .format_name = "qcow2",
1296 .instance_size = sizeof(BDRVQcowState),
1297 .bdrv_probe = qcow2_probe,
1298 .bdrv_open = qcow2_open,
1299 .bdrv_close = qcow2_close,
1300 .bdrv_create = qcow2_create,
1301 .bdrv_flush = qcow2_flush,
1302 .bdrv_is_allocated = qcow2_is_allocated,
1303 .bdrv_set_key = qcow2_set_key,
1304 .bdrv_make_empty = qcow2_make_empty,
1305
68d100e9
KW
1306 .bdrv_co_readv = qcow2_co_readv,
1307 .bdrv_co_writev = qcow2_co_writev,
7c80ab3f 1308 .bdrv_aio_flush = qcow2_aio_flush,
419b19d9 1309
5ea929e3 1310 .bdrv_discard = qcow2_discard,
419b19d9 1311 .bdrv_truncate = qcow2_truncate,
7c80ab3f 1312 .bdrv_write_compressed = qcow2_write_compressed,
20d97356
BS
1313
1314 .bdrv_snapshot_create = qcow2_snapshot_create,
1315 .bdrv_snapshot_goto = qcow2_snapshot_goto,
1316 .bdrv_snapshot_delete = qcow2_snapshot_delete,
1317 .bdrv_snapshot_list = qcow2_snapshot_list,
51ef6727 1318 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
7c80ab3f 1319 .bdrv_get_info = qcow2_get_info,
20d97356 1320
7c80ab3f
JS
1321 .bdrv_save_vmstate = qcow2_save_vmstate,
1322 .bdrv_load_vmstate = qcow2_load_vmstate,
20d97356
BS
1323
1324 .bdrv_change_backing_file = qcow2_change_backing_file,
1325
7c80ab3f
JS
1326 .create_options = qcow2_create_options,
1327 .bdrv_check = qcow2_check,
20d97356
BS
1328};
1329
5efa9d5a
AL
1330static void bdrv_qcow2_init(void)
1331{
1332 bdrv_register(&bdrv_qcow2);
1333}
1334
1335block_init(bdrv_qcow2_init);