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
;
148 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
152 be32_to_cpus(&header
.magic
);
153 be32_to_cpus(&header
.version
);
154 be64_to_cpus(&header
.backing_file_offset
);
155 be32_to_cpus(&header
.backing_file_size
);
156 be64_to_cpus(&header
.size
);
157 be32_to_cpus(&header
.cluster_bits
);
158 be32_to_cpus(&header
.crypt_method
);
159 be64_to_cpus(&header
.l1_table_offset
);
160 be32_to_cpus(&header
.l1_size
);
161 be64_to_cpus(&header
.refcount_table_offset
);
162 be32_to_cpus(&header
.refcount_table_clusters
);
163 be64_to_cpus(&header
.snapshots_offset
);
164 be32_to_cpus(&header
.nb_snapshots
);
166 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
) {
170 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
171 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
175 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
179 s
->crypt_method_header
= header
.crypt_method
;
180 if (s
->crypt_method_header
) {
183 s
->cluster_bits
= header
.cluster_bits
;
184 s
->cluster_size
= 1 << s
->cluster_bits
;
185 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
186 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
187 s
->l2_size
= 1 << s
->l2_bits
;
188 bs
->total_sectors
= header
.size
/ 512;
189 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
190 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
191 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
192 s
->refcount_table_offset
= header
.refcount_table_offset
;
193 s
->refcount_table_size
=
194 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
196 s
->snapshots_offset
= header
.snapshots_offset
;
197 s
->nb_snapshots
= header
.nb_snapshots
;
199 /* read the level 1 table */
200 s
->l1_size
= header
.l1_size
;
201 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
202 /* the L1 table must contain at least enough entries to put
204 if (s
->l1_size
< s
->l1_vm_state_index
) {
208 s
->l1_table_offset
= header
.l1_table_offset
;
209 if (s
->l1_size
> 0) {
210 s
->l1_table
= qemu_mallocz(
211 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
212 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
213 s
->l1_size
* sizeof(uint64_t));
217 for(i
= 0;i
< s
->l1_size
; i
++) {
218 be64_to_cpus(&s
->l1_table
[i
]);
222 /* alloc L2 table/refcount block cache */
223 writethrough
= ((flags
& BDRV_O_CACHE_MASK
) == 0);
224 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
, writethrough
);
225 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
,
228 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
229 /* one more sector for decompressed data alignment */
230 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
232 s
->cluster_cache_offset
= -1;
234 ret
= qcow2_refcount_init(bs
);
239 QLIST_INIT(&s
->cluster_allocs
);
241 /* read qcow2 extensions */
242 if (header
.backing_file_offset
) {
243 ext_end
= header
.backing_file_offset
;
245 ext_end
= s
->cluster_size
;
247 if (qcow2_read_extensions(bs
, sizeof(header
), ext_end
)) {
252 /* read the backing file name */
253 if (header
.backing_file_offset
!= 0) {
254 len
= header
.backing_file_size
;
258 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
259 bs
->backing_file
, len
);
263 bs
->backing_file
[len
] = '\0';
265 if (qcow2_read_snapshots(bs
) < 0) {
271 qcow2_check_refcounts(bs
);
276 qcow2_free_snapshots(bs
);
277 qcow2_refcount_close(bs
);
278 qemu_free(s
->l1_table
);
279 if (s
->l2_table_cache
) {
280 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
282 qemu_free(s
->cluster_cache
);
283 qemu_free(s
->cluster_data
);
287 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
289 BDRVQcowState
*s
= bs
->opaque
;
293 memset(keybuf
, 0, 16);
297 /* XXX: we could compress the chars to 7 bits to increase
299 for(i
= 0;i
< len
;i
++) {
302 s
->crypt_method
= s
->crypt_method_header
;
304 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
306 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
316 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
317 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
318 for(i
= 0; i
< 16; i
++)
319 printf(" %02x", tmp
[i
]);
321 for(i
= 0; i
< 16; i
++)
322 printf(" %02x", out
[i
]);
329 static int qcow2_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
330 int nb_sectors
, int *pnum
)
332 uint64_t cluster_offset
;
336 /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
337 * pass them on today */
338 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
343 return (cluster_offset
!= 0);
346 /* handle reading after the end of the backing file */
347 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
348 int64_t sector_num
, int nb_sectors
)
351 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
353 if (sector_num
>= bs
->total_sectors
)
356 n1
= bs
->total_sectors
- sector_num
;
358 qemu_iovec_memset_skip(qiov
, 0, 512 * (nb_sectors
- n1
), 512 * n1
);
363 typedef struct QCowAIOCB
{
364 BlockDriverAIOCB common
;
367 int remaining_sectors
;
368 int cur_nr_sectors
; /* number of sectors in current iteration */
370 uint64_t cluster_offset
;
371 uint8_t *cluster_data
;
372 BlockDriverAIOCB
*hd_aiocb
;
373 QEMUIOVector hd_qiov
;
376 QLIST_ENTRY(QCowAIOCB
) next_depend
;
379 static void qcow2_aio_cancel(BlockDriverAIOCB
*blockacb
)
381 QCowAIOCB
*acb
= container_of(blockacb
, QCowAIOCB
, common
);
383 bdrv_aio_cancel(acb
->hd_aiocb
);
384 qemu_aio_release(acb
);
387 static AIOPool qcow2_aio_pool
= {
388 .aiocb_size
= sizeof(QCowAIOCB
),
389 .cancel
= qcow2_aio_cancel
,
392 static void qcow2_aio_read_cb(void *opaque
, int ret
);
393 static void qcow2_aio_read_bh(void *opaque
)
395 QCowAIOCB
*acb
= opaque
;
396 qemu_bh_delete(acb
->bh
);
398 qcow2_aio_read_cb(opaque
, 0);
401 static int qcow2_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
406 acb
->bh
= qemu_bh_new(cb
, acb
);
410 qemu_bh_schedule(acb
->bh
);
415 static void qcow2_aio_read_cb(void *opaque
, int ret
)
417 QCowAIOCB
*acb
= opaque
;
418 BlockDriverState
*bs
= acb
->common
.bs
;
419 BDRVQcowState
*s
= bs
->opaque
;
420 int index_in_cluster
, n1
;
422 acb
->hd_aiocb
= NULL
;
426 /* post process the read buffer */
427 if (!acb
->cluster_offset
) {
429 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
432 if (s
->crypt_method
) {
433 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
,
434 acb
->cluster_data
, acb
->cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
435 qemu_iovec_reset(&acb
->hd_qiov
);
436 qemu_iovec_copy(&acb
->hd_qiov
, acb
->qiov
, acb
->bytes_done
,
437 acb
->cur_nr_sectors
* 512);
438 qemu_iovec_from_buffer(&acb
->hd_qiov
, acb
->cluster_data
,
439 512 * acb
->cur_nr_sectors
);
443 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
444 acb
->sector_num
+= acb
->cur_nr_sectors
;
445 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
447 if (acb
->remaining_sectors
== 0) {
448 /* request completed */
453 /* prepare next AIO request */
454 acb
->cur_nr_sectors
= acb
->remaining_sectors
;
455 if (s
->crypt_method
) {
456 acb
->cur_nr_sectors
= MIN(acb
->cur_nr_sectors
,
457 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
460 ret
= qcow2_get_cluster_offset(bs
, acb
->sector_num
<< 9,
461 &acb
->cur_nr_sectors
, &acb
->cluster_offset
);
466 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
468 qemu_iovec_reset(&acb
->hd_qiov
);
469 qemu_iovec_copy(&acb
->hd_qiov
, acb
->qiov
, acb
->bytes_done
,
470 acb
->cur_nr_sectors
* 512);
472 if (!acb
->cluster_offset
) {
474 if (bs
->backing_hd
) {
475 /* read from the base image */
476 n1
= qcow2_backing_read1(bs
->backing_hd
, &acb
->hd_qiov
,
477 acb
->sector_num
, acb
->cur_nr_sectors
);
479 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
480 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
481 &acb
->hd_qiov
, n1
, qcow2_aio_read_cb
, acb
);
482 if (acb
->hd_aiocb
== NULL
)
485 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
490 /* Note: in this case, no need to wait */
491 qemu_iovec_memset(&acb
->hd_qiov
, 0, 512 * acb
->cur_nr_sectors
);
492 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
496 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
497 /* add AIO support for compressed blocks ? */
498 if (qcow2_decompress_cluster(bs
, acb
->cluster_offset
) < 0)
501 qemu_iovec_from_buffer(&acb
->hd_qiov
,
502 s
->cluster_cache
+ index_in_cluster
* 512,
503 512 * acb
->cur_nr_sectors
);
505 ret
= qcow2_schedule_bh(qcow2_aio_read_bh
, acb
);
509 if ((acb
->cluster_offset
& 511) != 0) {
514 if (s
->crypt_method
) {
516 * For encrypted images, read everything into a temporary
517 * contiguous buffer on which the AES functions can work.
519 if (!acb
->cluster_data
) {
521 qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
524 assert(acb
->cur_nr_sectors
<=
525 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
526 qemu_iovec_reset(&acb
->hd_qiov
);
527 qemu_iovec_add(&acb
->hd_qiov
, acb
->cluster_data
,
528 512 * acb
->cur_nr_sectors
);
531 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
532 acb
->hd_aiocb
= bdrv_aio_readv(bs
->file
,
533 (acb
->cluster_offset
>> 9) + index_in_cluster
,
534 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
535 qcow2_aio_read_cb
, acb
);
536 if (acb
->hd_aiocb
== NULL
) {
544 acb
->common
.cb(acb
->common
.opaque
, ret
);
545 qemu_iovec_destroy(&acb
->hd_qiov
);
546 qemu_aio_release(acb
);
549 static QCowAIOCB
*qcow2_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
550 QEMUIOVector
*qiov
, int nb_sectors
,
551 BlockDriverCompletionFunc
*cb
,
552 void *opaque
, int is_write
)
556 acb
= qemu_aio_get(&qcow2_aio_pool
, bs
, cb
, opaque
);
559 acb
->hd_aiocb
= NULL
;
560 acb
->sector_num
= sector_num
;
563 qemu_iovec_init(&acb
->hd_qiov
, qiov
->niov
);
566 acb
->remaining_sectors
= nb_sectors
;
567 acb
->cur_nr_sectors
= 0;
568 acb
->cluster_offset
= 0;
569 acb
->l2meta
.nb_clusters
= 0;
570 QLIST_INIT(&acb
->l2meta
.dependent_requests
);
574 static BlockDriverAIOCB
*qcow2_aio_readv(BlockDriverState
*bs
,
576 QEMUIOVector
*qiov
, int nb_sectors
,
577 BlockDriverCompletionFunc
*cb
,
582 acb
= qcow2_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
586 qcow2_aio_read_cb(acb
, 0);
590 static void qcow2_aio_write_cb(void *opaque
, int ret
);
592 static void run_dependent_requests(QCowL2Meta
*m
)
597 /* Take the request off the list of running requests */
598 if (m
->nb_clusters
!= 0) {
599 QLIST_REMOVE(m
, next_in_flight
);
602 /* Restart all dependent requests */
603 QLIST_FOREACH_SAFE(req
, &m
->dependent_requests
, next_depend
, next
) {
604 qcow2_aio_write_cb(req
, 0);
607 /* Empty the list for the next part of the request */
608 QLIST_INIT(&m
->dependent_requests
);
611 static void qcow2_aio_write_cb(void *opaque
, int ret
)
613 QCowAIOCB
*acb
= opaque
;
614 BlockDriverState
*bs
= acb
->common
.bs
;
615 BDRVQcowState
*s
= bs
->opaque
;
616 int index_in_cluster
;
619 acb
->hd_aiocb
= NULL
;
622 ret
= qcow2_alloc_cluster_link_l2(bs
, &acb
->l2meta
);
625 run_dependent_requests(&acb
->l2meta
);
630 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
631 acb
->sector_num
+= acb
->cur_nr_sectors
;
632 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
634 if (acb
->remaining_sectors
== 0) {
635 /* request completed */
640 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
641 n_end
= index_in_cluster
+ acb
->remaining_sectors
;
642 if (s
->crypt_method
&&
643 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
644 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
646 ret
= qcow2_alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
647 index_in_cluster
, n_end
, &acb
->cur_nr_sectors
, &acb
->l2meta
);
652 acb
->cluster_offset
= acb
->l2meta
.cluster_offset
;
654 /* Need to wait for another request? If so, we are done for now. */
655 if (acb
->l2meta
.nb_clusters
== 0 && acb
->l2meta
.depends_on
!= NULL
) {
656 QLIST_INSERT_HEAD(&acb
->l2meta
.depends_on
->dependent_requests
,
661 assert((acb
->cluster_offset
& 511) == 0);
663 qemu_iovec_reset(&acb
->hd_qiov
);
664 qemu_iovec_copy(&acb
->hd_qiov
, acb
->qiov
, acb
->bytes_done
,
665 acb
->cur_nr_sectors
* 512);
667 if (s
->crypt_method
) {
668 if (!acb
->cluster_data
) {
669 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
673 assert(acb
->hd_qiov
.size
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
674 qemu_iovec_to_buffer(&acb
->hd_qiov
, acb
->cluster_data
);
676 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
,
677 acb
->cluster_data
, acb
->cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
679 qemu_iovec_reset(&acb
->hd_qiov
);
680 qemu_iovec_add(&acb
->hd_qiov
, acb
->cluster_data
,
681 acb
->cur_nr_sectors
* 512);
684 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
685 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
,
686 (acb
->cluster_offset
>> 9) + index_in_cluster
,
687 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
688 qcow2_aio_write_cb
, acb
);
689 if (acb
->hd_aiocb
== NULL
) {
697 if (acb
->l2meta
.nb_clusters
!= 0) {
698 QLIST_REMOVE(&acb
->l2meta
, next_in_flight
);
701 acb
->common
.cb(acb
->common
.opaque
, ret
);
702 qemu_iovec_destroy(&acb
->hd_qiov
);
703 qemu_aio_release(acb
);
706 static BlockDriverAIOCB
*qcow2_aio_writev(BlockDriverState
*bs
,
708 QEMUIOVector
*qiov
, int nb_sectors
,
709 BlockDriverCompletionFunc
*cb
,
712 BDRVQcowState
*s
= bs
->opaque
;
715 s
->cluster_cache_offset
= -1; /* disable compressed cache */
717 acb
= qcow2_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
721 qcow2_aio_write_cb(acb
, 0);
725 static void qcow2_close(BlockDriverState
*bs
)
727 BDRVQcowState
*s
= bs
->opaque
;
728 qemu_free(s
->l1_table
);
730 qcow2_cache_flush(bs
, s
->l2_table_cache
);
731 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
733 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
734 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
736 qemu_free(s
->cluster_cache
);
737 qemu_free(s
->cluster_data
);
738 qcow2_refcount_close(bs
);
742 * Updates the variable length parts of the qcow2 header, i.e. the backing file
743 * name and all extensions. qcow2 was not designed to allow such changes, so if
744 * we run out of space (we can only use the first cluster) this function may
747 * Returns 0 on success, -errno in error cases.
749 static int qcow2_update_ext_header(BlockDriverState
*bs
,
750 const char *backing_file
, const char *backing_fmt
)
752 size_t backing_file_len
= 0;
753 size_t backing_fmt_len
= 0;
754 BDRVQcowState
*s
= bs
->opaque
;
755 QCowExtension ext_backing_fmt
= {0, 0};
758 /* Backing file format doesn't make sense without a backing file */
759 if (backing_fmt
&& !backing_file
) {
763 /* Prepare the backing file format extension if needed */
765 ext_backing_fmt
.len
= cpu_to_be32(strlen(backing_fmt
));
766 ext_backing_fmt
.magic
= cpu_to_be32(QCOW2_EXT_MAGIC_BACKING_FORMAT
);
767 backing_fmt_len
= ((sizeof(ext_backing_fmt
)
768 + strlen(backing_fmt
) + 7) & ~7);
771 /* Check if we can fit the new header into the first cluster */
773 backing_file_len
= strlen(backing_file
);
776 size_t header_size
= sizeof(QCowHeader
) + backing_file_len
779 if (header_size
> s
->cluster_size
) {
783 /* Rewrite backing file name and qcow2 extensions */
784 size_t ext_size
= header_size
- sizeof(QCowHeader
);
785 uint8_t buf
[ext_size
];
787 size_t backing_file_offset
= 0;
791 int padding
= backing_fmt_len
-
792 (sizeof(ext_backing_fmt
) + strlen(backing_fmt
));
794 memcpy(buf
+ offset
, &ext_backing_fmt
, sizeof(ext_backing_fmt
));
795 offset
+= sizeof(ext_backing_fmt
);
797 memcpy(buf
+ offset
, backing_fmt
, strlen(backing_fmt
));
798 offset
+= strlen(backing_fmt
);
800 memset(buf
+ offset
, 0, padding
);
804 memcpy(buf
+ offset
, backing_file
, backing_file_len
);
805 backing_file_offset
= sizeof(QCowHeader
) + offset
;
808 ret
= bdrv_pwrite_sync(bs
->file
, sizeof(QCowHeader
), buf
, ext_size
);
813 /* Update header fields */
814 uint64_t be_backing_file_offset
= cpu_to_be64(backing_file_offset
);
815 uint32_t be_backing_file_size
= cpu_to_be32(backing_file_len
);
817 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_offset
),
818 &be_backing_file_offset
, sizeof(uint64_t));
823 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_size
),
824 &be_backing_file_size
, sizeof(uint32_t));
834 static int qcow2_change_backing_file(BlockDriverState
*bs
,
835 const char *backing_file
, const char *backing_fmt
)
837 return qcow2_update_ext_header(bs
, backing_file
, backing_fmt
);
840 static int preallocate(BlockDriverState
*bs
)
848 nb_sectors
= bdrv_getlength(bs
) >> 9;
850 QLIST_INIT(&meta
.dependent_requests
);
851 meta
.cluster_offset
= 0;
854 num
= MIN(nb_sectors
, INT_MAX
>> 9);
855 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
, &meta
);
860 ret
= qcow2_alloc_cluster_link_l2(bs
, &meta
);
862 qcow2_free_any_clusters(bs
, meta
.cluster_offset
, meta
.nb_clusters
);
866 /* There are no dependent requests, but we need to remove our request
867 * from the list of in-flight requests */
868 run_dependent_requests(&meta
);
870 /* TODO Preallocate data if requested */
877 * It is expected that the image file is large enough to actually contain
878 * all of the allocated clusters (otherwise we get failing reads after
879 * EOF). Extend the image to the last allocated sector.
881 if (meta
.cluster_offset
!= 0) {
884 ret
= bdrv_write(bs
->file
, (meta
.cluster_offset
>> 9) + num
- 1, buf
, 1);
893 static int qcow2_create2(const char *filename
, int64_t total_size
,
894 const char *backing_file
, const char *backing_format
,
895 int flags
, size_t cluster_size
, int prealloc
,
896 QEMUOptionParameter
*options
)
898 /* Calulate cluster_bits */
900 cluster_bits
= ffs(cluster_size
) - 1;
901 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
902 (1 << cluster_bits
) != cluster_size
)
905 "Cluster size must be a power of two between %d and %dk\n",
906 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
911 * Open the image file and write a minimal qcow2 header.
913 * We keep things simple and start with a zero-sized image. We also
914 * do without refcount blocks or a L1 table for now. We'll fix the
915 * inconsistency later.
917 * We do need a refcount table because growing the refcount table means
918 * allocating two new refcount blocks - the seconds of which would be at
919 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
920 * size for any qcow2 image.
922 BlockDriverState
* bs
;
924 uint8_t* refcount_table
;
927 ret
= bdrv_create_file(filename
, options
);
932 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
937 /* Write the header */
938 memset(&header
, 0, sizeof(header
));
939 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
940 header
.version
= cpu_to_be32(QCOW_VERSION
);
941 header
.cluster_bits
= cpu_to_be32(cluster_bits
);
942 header
.size
= cpu_to_be64(0);
943 header
.l1_table_offset
= cpu_to_be64(0);
944 header
.l1_size
= cpu_to_be32(0);
945 header
.refcount_table_offset
= cpu_to_be64(cluster_size
);
946 header
.refcount_table_clusters
= cpu_to_be32(1);
948 if (flags
& BLOCK_FLAG_ENCRYPT
) {
949 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
951 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
954 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
959 /* Write an empty refcount table */
960 refcount_table
= qemu_mallocz(cluster_size
);
961 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
962 qemu_free(refcount_table
);
971 * And now open the image and make it consistent first (i.e. increase the
972 * refcount of the cluster that is occupied by the header and the refcount
975 BlockDriver
* drv
= bdrv_find_format("qcow2");
977 ret
= bdrv_open(bs
, filename
,
978 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
);
983 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
987 } else if (ret
!= 0) {
988 error_report("Huh, first cluster in empty image is already in use?");
992 /* Okay, now that we have a valid image, let's give it the right size */
993 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
998 /* Want a backing file? There you go.*/
1000 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1006 /* And if we're supposed to preallocate metadata, do that now */
1008 ret
= preallocate(bs
);
1020 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
1022 const char *backing_file
= NULL
;
1023 const char *backing_fmt
= NULL
;
1024 uint64_t sectors
= 0;
1026 size_t cluster_size
= 65536;
1029 /* Read out options */
1030 while (options
&& options
->name
) {
1031 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1032 sectors
= options
->value
.n
/ 512;
1033 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1034 backing_file
= options
->value
.s
;
1035 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1036 backing_fmt
= options
->value
.s
;
1037 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1038 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1039 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1040 if (options
->value
.n
) {
1041 cluster_size
= options
->value
.n
;
1043 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1044 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1046 } else if (!strcmp(options
->value
.s
, "metadata")) {
1049 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1057 if (backing_file
&& prealloc
) {
1058 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1063 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1064 cluster_size
, prealloc
, options
);
1067 static int qcow2_make_empty(BlockDriverState
*bs
)
1070 /* XXX: not correct */
1071 BDRVQcowState
*s
= bs
->opaque
;
1072 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1075 memset(s
->l1_table
, 0, l1_length
);
1076 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1078 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1087 static int qcow2_discard(BlockDriverState
*bs
, int64_t sector_num
,
1090 return qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1094 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1096 BDRVQcowState
*s
= bs
->opaque
;
1097 int ret
, new_l1_size
;
1103 /* cannot proceed if image has snapshots */
1104 if (s
->nb_snapshots
) {
1108 /* shrinking is currently not supported */
1109 if (offset
< bs
->total_sectors
* 512) {
1113 new_l1_size
= size_to_l1(s
, offset
);
1114 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1119 /* write updated header.size */
1120 offset
= cpu_to_be64(offset
);
1121 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1122 &offset
, sizeof(uint64_t));
1127 s
->l1_vm_state_index
= new_l1_size
;
1131 /* XXX: put compressed sectors first, then all the cluster aligned
1132 tables to avoid losing bytes in alignment */
1133 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1134 const uint8_t *buf
, int nb_sectors
)
1136 BDRVQcowState
*s
= bs
->opaque
;
1140 uint64_t cluster_offset
;
1142 if (nb_sectors
== 0) {
1143 /* align end of file to a sector boundary to ease reading with
1144 sector based I/Os */
1145 cluster_offset
= bdrv_getlength(bs
->file
);
1146 cluster_offset
= (cluster_offset
+ 511) & ~511;
1147 bdrv_truncate(bs
->file
, cluster_offset
);
1151 if (nb_sectors
!= s
->cluster_sectors
)
1154 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1156 /* best compression, small window, no zlib header */
1157 memset(&strm
, 0, sizeof(strm
));
1158 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1160 9, Z_DEFAULT_STRATEGY
);
1166 strm
.avail_in
= s
->cluster_size
;
1167 strm
.next_in
= (uint8_t *)buf
;
1168 strm
.avail_out
= s
->cluster_size
;
1169 strm
.next_out
= out_buf
;
1171 ret
= deflate(&strm
, Z_FINISH
);
1172 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1177 out_len
= strm
.next_out
- out_buf
;
1181 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1182 /* could not compress: write normal cluster */
1183 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1185 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1186 sector_num
<< 9, out_len
);
1187 if (!cluster_offset
)
1189 cluster_offset
&= s
->cluster_offset_mask
;
1190 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1191 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1201 static int qcow2_flush(BlockDriverState
*bs
)
1203 BDRVQcowState
*s
= bs
->opaque
;
1206 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1211 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1216 return bdrv_flush(bs
->file
);
1219 static BlockDriverAIOCB
*qcow2_aio_flush(BlockDriverState
*bs
,
1220 BlockDriverCompletionFunc
*cb
,
1223 BDRVQcowState
*s
= bs
->opaque
;
1226 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1231 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1236 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
1239 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1241 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1244 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1246 BDRVQcowState
*s
= bs
->opaque
;
1247 bdi
->cluster_size
= s
->cluster_size
;
1248 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1253 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
)
1255 return qcow2_check_refcounts(bs
, result
);
1259 static void dump_refcounts(BlockDriverState
*bs
)
1261 BDRVQcowState
*s
= bs
->opaque
;
1262 int64_t nb_clusters
, k
, k1
, size
;
1265 size
= bdrv_getlength(bs
->file
);
1266 nb_clusters
= size_to_clusters(s
, size
);
1267 for(k
= 0; k
< nb_clusters
;) {
1269 refcount
= get_refcount(bs
, k
);
1271 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1273 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1279 static int qcow2_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1280 int64_t pos
, int size
)
1282 BDRVQcowState
*s
= bs
->opaque
;
1283 int growable
= bs
->growable
;
1286 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1288 ret
= bdrv_pwrite(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1289 bs
->growable
= growable
;
1294 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1295 int64_t pos
, int size
)
1297 BDRVQcowState
*s
= bs
->opaque
;
1298 int growable
= bs
->growable
;
1301 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1303 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1304 bs
->growable
= growable
;
1309 static QEMUOptionParameter qcow2_create_options
[] = {
1311 .name
= BLOCK_OPT_SIZE
,
1313 .help
= "Virtual disk size"
1316 .name
= BLOCK_OPT_BACKING_FILE
,
1318 .help
= "File name of a base image"
1321 .name
= BLOCK_OPT_BACKING_FMT
,
1323 .help
= "Image format of the base image"
1326 .name
= BLOCK_OPT_ENCRYPT
,
1328 .help
= "Encrypt the image"
1331 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1333 .help
= "qcow2 cluster size"
1336 .name
= BLOCK_OPT_PREALLOC
,
1338 .help
= "Preallocation mode (allowed values: off, metadata)"
1343 static BlockDriver bdrv_qcow2
= {
1344 .format_name
= "qcow2",
1345 .instance_size
= sizeof(BDRVQcowState
),
1346 .bdrv_probe
= qcow2_probe
,
1347 .bdrv_open
= qcow2_open
,
1348 .bdrv_close
= qcow2_close
,
1349 .bdrv_create
= qcow2_create
,
1350 .bdrv_flush
= qcow2_flush
,
1351 .bdrv_is_allocated
= qcow2_is_allocated
,
1352 .bdrv_set_key
= qcow2_set_key
,
1353 .bdrv_make_empty
= qcow2_make_empty
,
1355 .bdrv_aio_readv
= qcow2_aio_readv
,
1356 .bdrv_aio_writev
= qcow2_aio_writev
,
1357 .bdrv_aio_flush
= qcow2_aio_flush
,
1359 .bdrv_discard
= qcow2_discard
,
1360 .bdrv_truncate
= qcow2_truncate
,
1361 .bdrv_write_compressed
= qcow2_write_compressed
,
1363 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1364 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1365 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1366 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1367 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1368 .bdrv_get_info
= qcow2_get_info
,
1370 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1371 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1373 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1375 .create_options
= qcow2_create_options
,
1376 .bdrv_check
= qcow2_check
,
1379 static void bdrv_qcow2_init(void)
1381 bdrv_register(&bdrv_qcow2
);
1384 block_init(bdrv_qcow2_init
);