2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "block_int.h"
29 #include "block/qcow2.h"
30 #include "qemu-error.h"
33 Differences with QCOW:
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.
39 - Size of compressed clusters is stored in sectors to reduce bit usage
40 in the cluster offsets.
41 - Support for storing additional data (such as the VM state) in the
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.
53 #define QCOW2_EXT_MAGIC_END 0
54 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
56 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
58 const QCowHeader
*cow_header
= (const void *)buf
;
60 if (buf_size
>= sizeof(QCowHeader
) &&
61 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
62 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
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
76 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
83 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
85 offset
= start_offset
;
86 while (offset
< end_offset
) {
90 if (offset
> s
->cluster_size
)
91 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
93 printf("attemting to read extended header in offset %lu\n", offset
);
96 if (bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
97 fprintf(stderr
, "qcow2_read_extension: ERROR: "
98 "pread fail from offset %" PRIu64
"\n",
102 be32_to_cpus(&ext
.magic
);
103 be32_to_cpus(&ext
.len
);
104 offset
+= sizeof(ext
);
106 printf("ext.magic = 0x%x\n", ext
.magic
);
109 case QCOW2_EXT_MAGIC_END
:
112 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
113 if (ext
.len
>= sizeof(bs
->backing_format
)) {
114 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
116 ext
.len
, sizeof(bs
->backing_format
));
119 if (bdrv_pread(bs
->file
, offset
, bs
->backing_format
,
122 bs
->backing_format
[ext
.len
] = '\0';
124 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
126 offset
= ((offset
+ ext
.len
+ 7) & ~7);
130 /* unknown magic -- just skip it */
131 offset
= ((offset
+ ext
.len
+ 7) & ~7);
140 static int qcow2_open(BlockDriverState
*bs
, int flags
)
142 BDRVQcowState
*s
= bs
->opaque
;
147 if (bdrv_pread(bs
->file
, 0, &header
, sizeof(header
)) != sizeof(header
))
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
);
163 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
165 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
166 header
.cluster_bits
> MAX_CLUSTER_BITS
)
168 if (header
.crypt_method
> QCOW_CRYPT_AES
)
170 s
->crypt_method_header
= header
.crypt_method
;
171 if (s
->crypt_method_header
)
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
;
183 s
->refcount_table_size
=
184 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
186 s
->snapshots_offset
= header
.snapshots_offset
;
187 s
->nb_snapshots
= header
.nb_snapshots
;
189 /* read the level 1 table */
190 s
->l1_size
= header
.l1_size
;
191 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
192 /* the L1 table must contain at least enough entries to put
194 if (s
->l1_size
< s
->l1_vm_state_index
)
196 s
->l1_table_offset
= header
.l1_table_offset
;
197 if (s
->l1_size
> 0) {
198 s
->l1_table
= qemu_mallocz(
199 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
200 if (bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
201 s
->l1_size
* sizeof(uint64_t))
203 for(i
= 0;i
< s
->l1_size
; i
++) {
204 be64_to_cpus(&s
->l1_table
[i
]);
208 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
209 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
210 /* one more sector for decompressed data alignment */
211 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
213 s
->cluster_cache_offset
= -1;
215 if (qcow2_refcount_init(bs
) < 0)
218 QLIST_INIT(&s
->cluster_allocs
);
220 /* read qcow2 extensions */
221 if (header
.backing_file_offset
)
222 ext_end
= header
.backing_file_offset
;
224 ext_end
= s
->cluster_size
;
225 if (qcow2_read_extensions(bs
, sizeof(header
), ext_end
))
228 /* read the backing file name */
229 if (header
.backing_file_offset
!= 0) {
230 len
= header
.backing_file_size
;
233 if (bdrv_pread(bs
->file
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
235 bs
->backing_file
[len
] = '\0';
237 if (qcow2_read_snapshots(bs
) < 0)
241 qcow2_check_refcounts(bs
);
246 qcow2_free_snapshots(bs
);
247 qcow2_refcount_close(bs
);
248 qemu_free(s
->l1_table
);
249 qemu_free(s
->l2_cache
);
250 qemu_free(s
->cluster_cache
);
251 qemu_free(s
->cluster_data
);
255 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
257 BDRVQcowState
*s
= bs
->opaque
;
261 memset(keybuf
, 0, 16);
265 /* XXX: we could compress the chars to 7 bits to increase
267 for(i
= 0;i
< len
;i
++) {
270 s
->crypt_method
= s
->crypt_method_header
;
272 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
274 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
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
]);
289 for(i
= 0; i
< 16; i
++)
290 printf(" %02x", out
[i
]);
297 static int qcow2_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
298 int nb_sectors
, int *pnum
)
300 uint64_t cluster_offset
;
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
);
311 return (cluster_offset
!= 0);
314 /* handle reading after the end of the backing file */
315 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
316 int64_t sector_num
, int nb_sectors
)
319 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
321 if (sector_num
>= bs
->total_sectors
)
324 n1
= bs
->total_sectors
- sector_num
;
326 qemu_iovec_memset(qiov
, 0, 512 * (nb_sectors
- n1
));
331 typedef struct QCowAIOCB
{
332 BlockDriverAIOCB common
;
335 int remaining_sectors
;
336 int cur_nr_sectors
; /* number of sectors in current iteration */
338 uint64_t cluster_offset
;
339 uint8_t *cluster_data
;
340 BlockDriverAIOCB
*hd_aiocb
;
341 QEMUIOVector hd_qiov
;
344 QLIST_ENTRY(QCowAIOCB
) next_depend
;
347 static void qcow2_aio_cancel(BlockDriverAIOCB
*blockacb
)
349 QCowAIOCB
*acb
= container_of(blockacb
, QCowAIOCB
, common
);
351 bdrv_aio_cancel(acb
->hd_aiocb
);
352 qemu_aio_release(acb
);
355 static AIOPool qcow2_aio_pool
= {
356 .aiocb_size
= sizeof(QCowAIOCB
),
357 .cancel
= qcow2_aio_cancel
,
360 static void qcow2_aio_read_cb(void *opaque
, int ret
);
361 static void qcow2_aio_read_bh(void *opaque
)
363 QCowAIOCB
*acb
= opaque
;
364 qemu_bh_delete(acb
->bh
);
366 qcow2_aio_read_cb(opaque
, 0);
369 static int qcow2_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
374 acb
->bh
= qemu_bh_new(cb
, acb
);
378 qemu_bh_schedule(acb
->bh
);
383 static void qcow2_aio_read_cb(void *opaque
, int ret
)
385 QCowAIOCB
*acb
= opaque
;
386 BlockDriverState
*bs
= acb
->common
.bs
;
387 BDRVQcowState
*s
= bs
->opaque
;
388 int index_in_cluster
, n1
;
390 acb
->hd_aiocb
= NULL
;
394 /* post process the read buffer */
395 if (!acb
->cluster_offset
) {
397 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
400 if (s
->crypt_method
) {
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
);
411 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
412 acb
->sector_num
+= acb
->cur_nr_sectors
;
413 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
415 if (acb
->remaining_sectors
== 0) {
416 /* request completed */
421 /* prepare next AIO request */
422 acb
->cur_nr_sectors
= acb
->remaining_sectors
;
423 if (s
->crypt_method
) {
424 acb
->cur_nr_sectors
= MIN(acb
->cur_nr_sectors
,
425 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
428 ret
= qcow2_get_cluster_offset(bs
, acb
->sector_num
<< 9,
429 &acb
->cur_nr_sectors
, &acb
->cluster_offset
);
434 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
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);
440 if (!acb
->cluster_offset
) {
442 if (bs
->backing_hd
) {
443 /* read from the base image */
444 n1
= qcow2_backing_read1(bs
->backing_hd
, &acb
->hd_qiov
,
445 acb
->sector_num
, acb
->cur_nr_sectors
);
447 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
448 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
449 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
450 qcow2_aio_read_cb
, acb
);
451 if (acb
->hd_aiocb
== NULL
)
454 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
459 /* Note: in this case, no need to wait */
460 qemu_iovec_memset(&acb
->hd_qiov
, 0, 512 * acb
->cur_nr_sectors
);
461 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
465 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
466 /* add AIO support for compressed blocks ? */
467 if (qcow2_decompress_cluster(bs
, acb
->cluster_offset
) < 0)
470 qemu_iovec_from_buffer(&acb
->hd_qiov
,
471 s
->cluster_cache
+ index_in_cluster
* 512,
472 512 * acb
->cur_nr_sectors
);
474 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
478 if ((acb
->cluster_offset
& 511) != 0) {
483 if (s
->crypt_method
) {
485 * For encrypted images, read everything into a temporary
486 * contiguous buffer on which the AES functions can work.
488 if (!acb
->cluster_data
) {
490 qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
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
);
500 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
501 acb
->hd_aiocb
= bdrv_aio_readv(bs
->file
,
502 (acb
->cluster_offset
>> 9) + index_in_cluster
,
503 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
504 qcow2_aio_read_cb
, acb
);
505 if (acb
->hd_aiocb
== NULL
) {
513 acb
->common
.cb(acb
->common
.opaque
, ret
);
514 qemu_iovec_destroy(&acb
->hd_qiov
);
515 qemu_aio_release(acb
);
518 static 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
)
525 acb
= qemu_aio_get(&qcow2_aio_pool
, bs
, cb
, opaque
);
528 acb
->hd_aiocb
= NULL
;
529 acb
->sector_num
= sector_num
;
532 qemu_iovec_init(&acb
->hd_qiov
, qiov
->niov
);
535 acb
->remaining_sectors
= nb_sectors
;
536 acb
->cur_nr_sectors
= 0;
537 acb
->cluster_offset
= 0;
538 acb
->l2meta
.nb_clusters
= 0;
539 QLIST_INIT(&acb
->l2meta
.dependent_requests
);
543 static BlockDriverAIOCB
*qcow2_aio_readv(BlockDriverState
*bs
,
545 QEMUIOVector
*qiov
, int nb_sectors
,
546 BlockDriverCompletionFunc
*cb
,
551 acb
= qcow2_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
555 qcow2_aio_read_cb(acb
, 0);
559 static void qcow2_aio_write_cb(void *opaque
, int ret
);
561 static void run_dependent_requests(QCowL2Meta
*m
)
566 /* Take the request off the list of running requests */
567 if (m
->nb_clusters
!= 0) {
568 QLIST_REMOVE(m
, next_in_flight
);
571 /* Restart all dependent requests */
572 QLIST_FOREACH_SAFE(req
, &m
->dependent_requests
, next_depend
, next
) {
573 qcow2_aio_write_cb(req
, 0);
576 /* Empty the list for the next part of the request */
577 QLIST_INIT(&m
->dependent_requests
);
580 static void qcow2_aio_write_cb(void *opaque
, int ret
)
582 QCowAIOCB
*acb
= opaque
;
583 BlockDriverState
*bs
= acb
->common
.bs
;
584 BDRVQcowState
*s
= bs
->opaque
;
585 int index_in_cluster
;
588 acb
->hd_aiocb
= NULL
;
591 ret
= qcow2_alloc_cluster_link_l2(bs
, &acb
->l2meta
);
594 run_dependent_requests(&acb
->l2meta
);
599 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
600 acb
->sector_num
+= acb
->cur_nr_sectors
;
601 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
603 if (acb
->remaining_sectors
== 0) {
604 /* request completed */
609 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
610 n_end
= index_in_cluster
+ acb
->remaining_sectors
;
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
;
615 ret
= qcow2_alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
616 index_in_cluster
, n_end
, &acb
->cur_nr_sectors
, &acb
->l2meta
);
621 acb
->cluster_offset
= acb
->l2meta
.cluster_offset
;
623 /* Need to wait for another request? If so, we are done for now. */
624 if (acb
->l2meta
.nb_clusters
== 0 && acb
->l2meta
.depends_on
!= NULL
) {
625 QLIST_INSERT_HEAD(&acb
->l2meta
.depends_on
->dependent_requests
,
630 assert((acb
->cluster_offset
& 511) == 0);
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);
636 if (s
->crypt_method
) {
637 if (!acb
->cluster_data
) {
638 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
642 assert(acb
->hd_qiov
.size
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
643 qemu_iovec_to_buffer(&acb
->hd_qiov
, acb
->cluster_data
);
645 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
,
646 acb
->cluster_data
, acb
->cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
648 qemu_iovec_reset(&acb
->hd_qiov
);
649 qemu_iovec_add(&acb
->hd_qiov
, acb
->cluster_data
,
650 acb
->cur_nr_sectors
* 512);
653 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
654 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
,
655 (acb
->cluster_offset
>> 9) + index_in_cluster
,
656 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
657 qcow2_aio_write_cb
, acb
);
658 if (acb
->hd_aiocb
== NULL
) {
666 if (acb
->l2meta
.nb_clusters
!= 0) {
667 QLIST_REMOVE(&acb
->l2meta
, next_in_flight
);
670 acb
->common
.cb(acb
->common
.opaque
, ret
);
671 qemu_iovec_destroy(&acb
->hd_qiov
);
672 qemu_aio_release(acb
);
675 static BlockDriverAIOCB
*qcow2_aio_writev(BlockDriverState
*bs
,
677 QEMUIOVector
*qiov
, int nb_sectors
,
678 BlockDriverCompletionFunc
*cb
,
681 BDRVQcowState
*s
= bs
->opaque
;
684 s
->cluster_cache_offset
= -1; /* disable compressed cache */
686 acb
= qcow2_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
690 qcow2_aio_write_cb(acb
, 0);
694 static void qcow2_close(BlockDriverState
*bs
)
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
);
701 qcow2_refcount_close(bs
);
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
710 * Returns 0 on success, -errno in error cases.
712 static int qcow2_update_ext_header(BlockDriverState
*bs
,
713 const char *backing_file
, const char *backing_fmt
)
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};
721 /* Backing file format doesn't make sense without a backing file */
722 if (backing_fmt
&& !backing_file
) {
726 /* Prepare the backing file format extension if needed */
728 ext_backing_fmt
.len
= cpu_to_be32(strlen(backing_fmt
));
729 ext_backing_fmt
.magic
= cpu_to_be32(QCOW2_EXT_MAGIC_BACKING_FORMAT
);
730 backing_fmt_len
= ((sizeof(ext_backing_fmt
)
731 + strlen(backing_fmt
) + 7) & ~7);
734 /* Check if we can fit the new header into the first cluster */
736 backing_file_len
= strlen(backing_file
);
739 size_t header_size
= sizeof(QCowHeader
) + backing_file_len
742 if (header_size
> s
->cluster_size
) {
746 /* Rewrite backing file name and qcow2 extensions */
747 size_t ext_size
= header_size
- sizeof(QCowHeader
);
748 uint8_t buf
[ext_size
];
750 size_t backing_file_offset
= 0;
754 int padding
= backing_fmt_len
-
755 (sizeof(ext_backing_fmt
) + strlen(backing_fmt
));
757 memcpy(buf
+ offset
, &ext_backing_fmt
, sizeof(ext_backing_fmt
));
758 offset
+= sizeof(ext_backing_fmt
);
760 memcpy(buf
+ offset
, backing_fmt
, strlen(backing_fmt
));
761 offset
+= strlen(backing_fmt
);
763 memset(buf
+ offset
, 0, padding
);
767 memcpy(buf
+ offset
, backing_file
, backing_file_len
);
768 backing_file_offset
= sizeof(QCowHeader
) + offset
;
771 ret
= bdrv_pwrite_sync(bs
->file
, sizeof(QCowHeader
), buf
, ext_size
);
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
);
780 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_offset
),
781 &be_backing_file_offset
, sizeof(uint64_t));
786 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_size
),
787 &be_backing_file_size
, sizeof(uint32_t));
797 static int qcow2_change_backing_file(BlockDriverState
*bs
,
798 const char *backing_file
, const char *backing_fmt
)
800 return qcow2_update_ext_header(bs
, backing_file
, backing_fmt
);
803 static int preallocate(BlockDriverState
*bs
)
811 nb_sectors
= bdrv_getlength(bs
) >> 9;
813 QLIST_INIT(&meta
.dependent_requests
);
814 meta
.cluster_offset
= 0;
817 num
= MIN(nb_sectors
, INT_MAX
>> 9);
818 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
, &meta
);
823 ret
= qcow2_alloc_cluster_link_l2(bs
, &meta
);
825 qcow2_free_any_clusters(bs
, meta
.cluster_offset
, meta
.nb_clusters
);
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
);
833 /* TODO Preallocate data if requested */
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.
844 if (meta
.cluster_offset
!= 0) {
847 ret
= bdrv_write(bs
->file
, (meta
.cluster_offset
>> 9) + num
- 1, buf
, 1);
856 static 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
)
861 /* Calulate 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
)
868 "Cluster size must be a power of two between %d and %dk\n",
869 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
874 * Open the image file and write a minimal qcow2 header.
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.
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.
885 BlockDriverState
* bs
;
887 uint8_t* refcount_table
;
890 ret
= bdrv_create_file(filename
, options
);
895 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
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);
911 if (flags
& BLOCK_FLAG_ENCRYPT
) {
912 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
914 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
917 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
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
);
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
938 BlockDriver
* drv
= bdrv_find_format("qcow2");
940 ret
= bdrv_open(bs
, filename
, BDRV_O_RDWR
| BDRV_O_NO_FLUSH
, drv
);
945 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
949 } else if (ret
!= 0) {
950 error_report("Huh, first cluster in empty image is already in use?");
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
);
960 /* Want a backing file? There you go.*/
962 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
968 /* And if we're supposed to preallocate metadata, do that now */
970 ret
= preallocate(bs
);
982 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
984 const char *backing_file
= NULL
;
985 const char *backing_fmt
= NULL
;
986 uint64_t sectors
= 0;
988 size_t cluster_size
= 65536;
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
;
1005 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1006 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1008 } else if (!strcmp(options
->value
.s
, "metadata")) {
1011 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1019 if (backing_file
&& prealloc
) {
1020 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1025 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1026 cluster_size
, prealloc
, options
);
1029 static int qcow2_make_empty(BlockDriverState
*bs
)
1032 /* XXX: not correct */
1033 BDRVQcowState
*s
= bs
->opaque
;
1034 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1037 memset(s
->l1_table
, 0, l1_length
);
1038 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1040 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1049 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1051 BDRVQcowState
*s
= bs
->opaque
;
1052 int ret
, new_l1_size
;
1058 /* cannot proceed if image has snapshots */
1059 if (s
->nb_snapshots
) {
1063 /* shrinking is currently not supported */
1064 if (offset
< bs
->total_sectors
* 512) {
1068 new_l1_size
= size_to_l1(s
, offset
);
1069 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1074 /* write updated header.size */
1075 offset
= cpu_to_be64(offset
);
1076 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1077 &offset
, sizeof(uint64_t));
1082 s
->l1_vm_state_index
= new_l1_size
;
1086 /* XXX: put compressed sectors first, then all the cluster aligned
1087 tables to avoid losing bytes in alignment */
1088 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1089 const uint8_t *buf
, int nb_sectors
)
1091 BDRVQcowState
*s
= bs
->opaque
;
1095 uint64_t cluster_offset
;
1097 if (nb_sectors
== 0) {
1098 /* align end of file to a sector boundary to ease reading with
1099 sector based I/Os */
1100 cluster_offset
= bdrv_getlength(bs
->file
);
1101 cluster_offset
= (cluster_offset
+ 511) & ~511;
1102 bdrv_truncate(bs
->file
, cluster_offset
);
1106 if (nb_sectors
!= s
->cluster_sectors
)
1109 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1111 /* best compression, small window, no zlib header */
1112 memset(&strm
, 0, sizeof(strm
));
1113 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1115 9, Z_DEFAULT_STRATEGY
);
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
;
1126 ret
= deflate(&strm
, Z_FINISH
);
1127 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1132 out_len
= strm
.next_out
- out_buf
;
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
);
1140 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1141 sector_num
<< 9, out_len
);
1142 if (!cluster_offset
)
1144 cluster_offset
&= s
->cluster_offset_mask
;
1145 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1146 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1156 static int qcow2_flush(BlockDriverState
*bs
)
1158 return bdrv_flush(bs
->file
);
1161 static BlockDriverAIOCB
*qcow2_aio_flush(BlockDriverState
*bs
,
1162 BlockDriverCompletionFunc
*cb
,
1165 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
1168 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1170 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1173 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1175 BDRVQcowState
*s
= bs
->opaque
;
1176 bdi
->cluster_size
= s
->cluster_size
;
1177 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1182 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
)
1184 return qcow2_check_refcounts(bs
, result
);
1188 static void dump_refcounts(BlockDriverState
*bs
)
1190 BDRVQcowState
*s
= bs
->opaque
;
1191 int64_t nb_clusters
, k
, k1
, size
;
1194 size
= bdrv_getlength(bs
->file
);
1195 nb_clusters
= size_to_clusters(s
, size
);
1196 for(k
= 0; k
< nb_clusters
;) {
1198 refcount
= get_refcount(bs
, k
);
1200 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1202 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1208 static int qcow2_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1209 int64_t pos
, int size
)
1211 BDRVQcowState
*s
= bs
->opaque
;
1212 int growable
= bs
->growable
;
1215 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1217 ret
= bdrv_pwrite(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1218 bs
->growable
= growable
;
1223 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1224 int64_t pos
, int size
)
1226 BDRVQcowState
*s
= bs
->opaque
;
1227 int growable
= bs
->growable
;
1230 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1232 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1233 bs
->growable
= growable
;
1238 static QEMUOptionParameter qcow2_create_options
[] = {
1240 .name
= BLOCK_OPT_SIZE
,
1242 .help
= "Virtual disk size"
1245 .name
= BLOCK_OPT_BACKING_FILE
,
1247 .help
= "File name of a base image"
1250 .name
= BLOCK_OPT_BACKING_FMT
,
1252 .help
= "Image format of the base image"
1255 .name
= BLOCK_OPT_ENCRYPT
,
1257 .help
= "Encrypt the image"
1260 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1262 .help
= "qcow2 cluster size"
1265 .name
= BLOCK_OPT_PREALLOC
,
1267 .help
= "Preallocation mode (allowed values: off, metadata)"
1272 static BlockDriver bdrv_qcow2
= {
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
,
1284 .bdrv_aio_readv
= qcow2_aio_readv
,
1285 .bdrv_aio_writev
= qcow2_aio_writev
,
1286 .bdrv_aio_flush
= qcow2_aio_flush
,
1288 .bdrv_truncate
= qcow2_truncate
,
1289 .bdrv_write_compressed
= qcow2_write_compressed
,
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
,
1295 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1296 .bdrv_get_info
= qcow2_get_info
,
1298 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1299 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1301 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1303 .create_options
= qcow2_create_options
,
1304 .bdrv_check
= qcow2_check
,
1307 static void bdrv_qcow2_init(void)
1309 bdrv_register(&bdrv_qcow2
);
1312 block_init(bdrv_qcow2_init
);