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