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