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"
32 Differences with QCOW:
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.
38 - Size of compressed clusters is stored in sectors to reduce bit usage
39 in the cluster offsets.
40 - Support for storing additional data (such as the VM state) in the
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.
52 #define QCOW_EXT_MAGIC_END 0
53 #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
55 static int qcow_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
57 const QCowHeader
*cow_header
= (const void *)buf
;
59 if (buf_size
>= sizeof(QCowHeader
) &&
60 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
61 be32_to_cpu(cow_header
->version
) == QCOW_VERSION
)
69 * read qcow2 extension and fill bs
70 * start reading from start_offset
71 * finish reading upon magic of value 0 or when end_offset reached
72 * unknown magic is skipped (future extension this version knows nothing about)
73 * return 0 upon success, non-0 otherwise
75 static int qcow_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
82 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
84 offset
= start_offset
;
85 while (offset
< end_offset
) {
89 if (offset
> s
->cluster_size
)
90 printf("qcow_handle_extension: suspicious offset %lu\n", offset
);
92 printf("attemting to read extended header in offset %lu\n", offset
);
95 if (bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
96 fprintf(stderr
, "qcow_handle_extension: ERROR: "
97 "pread fail from offset %" PRIu64
"\n",
101 be32_to_cpus(&ext
.magic
);
102 be32_to_cpus(&ext
.len
);
103 offset
+= sizeof(ext
);
105 printf("ext.magic = 0x%x\n", ext
.magic
);
108 case QCOW_EXT_MAGIC_END
:
111 case QCOW_EXT_MAGIC_BACKING_FORMAT
:
112 if (ext
.len
>= sizeof(bs
->backing_format
)) {
113 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
115 ext
.len
, sizeof(bs
->backing_format
));
118 if (bdrv_pread(bs
->file
, offset
, bs
->backing_format
,
121 bs
->backing_format
[ext
.len
] = '\0';
123 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
125 offset
= ((offset
+ ext
.len
+ 7) & ~7);
129 /* unknown magic -- just skip it */
130 offset
= ((offset
+ ext
.len
+ 7) & ~7);
139 static int qcow_open(BlockDriverState
*bs
, int flags
)
141 BDRVQcowState
*s
= bs
->opaque
;
146 if (bdrv_pread(bs
->file
, 0, &header
, sizeof(header
)) != sizeof(header
))
148 be32_to_cpus(&header
.magic
);
149 be32_to_cpus(&header
.version
);
150 be64_to_cpus(&header
.backing_file_offset
);
151 be32_to_cpus(&header
.backing_file_size
);
152 be64_to_cpus(&header
.size
);
153 be32_to_cpus(&header
.cluster_bits
);
154 be32_to_cpus(&header
.crypt_method
);
155 be64_to_cpus(&header
.l1_table_offset
);
156 be32_to_cpus(&header
.l1_size
);
157 be64_to_cpus(&header
.refcount_table_offset
);
158 be32_to_cpus(&header
.refcount_table_clusters
);
159 be64_to_cpus(&header
.snapshots_offset
);
160 be32_to_cpus(&header
.nb_snapshots
);
162 if (header
.magic
!= QCOW_MAGIC
|| header
.version
!= QCOW_VERSION
)
164 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
165 header
.cluster_bits
> MAX_CLUSTER_BITS
)
167 if (header
.crypt_method
> QCOW_CRYPT_AES
)
169 s
->crypt_method_header
= header
.crypt_method
;
170 if (s
->crypt_method_header
)
172 s
->cluster_bits
= header
.cluster_bits
;
173 s
->cluster_size
= 1 << s
->cluster_bits
;
174 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
175 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
176 s
->l2_size
= 1 << s
->l2_bits
;
177 bs
->total_sectors
= header
.size
/ 512;
178 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
179 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
180 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
181 s
->refcount_table_offset
= header
.refcount_table_offset
;
182 s
->refcount_table_size
=
183 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
185 s
->snapshots_offset
= header
.snapshots_offset
;
186 s
->nb_snapshots
= header
.nb_snapshots
;
188 /* read the level 1 table */
189 s
->l1_size
= header
.l1_size
;
190 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
191 /* the L1 table must contain at least enough entries to put
193 if (s
->l1_size
< s
->l1_vm_state_index
)
195 s
->l1_table_offset
= header
.l1_table_offset
;
196 if (s
->l1_size
> 0) {
197 s
->l1_table
= qemu_mallocz(
198 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
199 if (bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
, s
->l1_size
* sizeof(uint64_t)) !=
200 s
->l1_size
* sizeof(uint64_t))
202 for(i
= 0;i
< s
->l1_size
; i
++) {
203 be64_to_cpus(&s
->l1_table
[i
]);
207 s
->l2_cache
= qemu_malloc(s
->l2_size
* L2_CACHE_SIZE
* sizeof(uint64_t));
208 s
->cluster_cache
= qemu_malloc(s
->cluster_size
);
209 /* one more sector for decompressed data alignment */
210 s
->cluster_data
= qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
212 s
->cluster_cache_offset
= -1;
214 if (qcow2_refcount_init(bs
) < 0)
217 QLIST_INIT(&s
->cluster_allocs
);
219 /* read qcow2 extensions */
220 if (header
.backing_file_offset
)
221 ext_end
= header
.backing_file_offset
;
223 ext_end
= s
->cluster_size
;
224 if (qcow_read_extensions(bs
, sizeof(header
), ext_end
))
227 /* read the backing file name */
228 if (header
.backing_file_offset
!= 0) {
229 len
= header
.backing_file_size
;
232 if (bdrv_pread(bs
->file
, header
.backing_file_offset
, bs
->backing_file
, len
) != len
)
234 bs
->backing_file
[len
] = '\0';
236 if (qcow2_read_snapshots(bs
) < 0)
240 qcow2_check_refcounts(bs
);
245 qcow2_free_snapshots(bs
);
246 qcow2_refcount_close(bs
);
247 qemu_free(s
->l1_table
);
248 qemu_free(s
->l2_cache
);
249 qemu_free(s
->cluster_cache
);
250 qemu_free(s
->cluster_data
);
254 static int qcow_set_key(BlockDriverState
*bs
, const char *key
)
256 BDRVQcowState
*s
= bs
->opaque
;
260 memset(keybuf
, 0, 16);
264 /* XXX: we could compress the chars to 7 bits to increase
266 for(i
= 0;i
< len
;i
++) {
269 s
->crypt_method
= s
->crypt_method_header
;
271 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
273 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
283 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
284 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
285 for(i
= 0; i
< 16; i
++)
286 printf(" %02x", tmp
[i
]);
288 for(i
= 0; i
< 16; i
++)
289 printf(" %02x", out
[i
]);
296 static int qcow_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
297 int nb_sectors
, int *pnum
)
299 uint64_t cluster_offset
;
303 /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
304 * pass them on today */
305 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
310 return (cluster_offset
!= 0);
313 /* handle reading after the end of the backing file */
314 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
315 int64_t sector_num
, int nb_sectors
)
318 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
320 if (sector_num
>= bs
->total_sectors
)
323 n1
= bs
->total_sectors
- sector_num
;
325 qemu_iovec_memset(qiov
, 0, 512 * (nb_sectors
- n1
));
330 typedef struct QCowAIOCB
{
331 BlockDriverAIOCB common
;
334 int remaining_sectors
;
335 int cur_nr_sectors
; /* number of sectors in current iteration */
337 uint64_t cluster_offset
;
338 uint8_t *cluster_data
;
339 BlockDriverAIOCB
*hd_aiocb
;
340 QEMUIOVector hd_qiov
;
343 QLIST_ENTRY(QCowAIOCB
) next_depend
;
346 static void qcow_aio_cancel(BlockDriverAIOCB
*blockacb
)
348 QCowAIOCB
*acb
= container_of(blockacb
, QCowAIOCB
, common
);
350 bdrv_aio_cancel(acb
->hd_aiocb
);
351 qemu_aio_release(acb
);
354 static AIOPool qcow_aio_pool
= {
355 .aiocb_size
= sizeof(QCowAIOCB
),
356 .cancel
= qcow_aio_cancel
,
359 static void qcow_aio_read_cb(void *opaque
, int ret
);
360 static void qcow_aio_read_bh(void *opaque
)
362 QCowAIOCB
*acb
= opaque
;
363 qemu_bh_delete(acb
->bh
);
365 qcow_aio_read_cb(opaque
, 0);
368 static int qcow_schedule_bh(QEMUBHFunc
*cb
, QCowAIOCB
*acb
)
373 acb
->bh
= qemu_bh_new(cb
, acb
);
377 qemu_bh_schedule(acb
->bh
);
382 static void qcow_aio_read_cb(void *opaque
, int ret
)
384 QCowAIOCB
*acb
= opaque
;
385 BlockDriverState
*bs
= acb
->common
.bs
;
386 BDRVQcowState
*s
= bs
->opaque
;
387 int index_in_cluster
, n1
;
389 acb
->hd_aiocb
= NULL
;
393 /* post process the read buffer */
394 if (!acb
->cluster_offset
) {
396 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
399 if (s
->crypt_method
) {
400 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
,
401 acb
->cluster_data
, acb
->cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
402 qemu_iovec_reset(&acb
->hd_qiov
);
403 qemu_iovec_copy(&acb
->hd_qiov
, acb
->qiov
, acb
->bytes_done
,
404 acb
->cur_nr_sectors
* 512);
405 qemu_iovec_from_buffer(&acb
->hd_qiov
, acb
->cluster_data
,
406 512 * acb
->cur_nr_sectors
);
410 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
411 acb
->sector_num
+= acb
->cur_nr_sectors
;
412 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
414 if (acb
->remaining_sectors
== 0) {
415 /* request completed */
420 /* prepare next AIO request */
421 acb
->cur_nr_sectors
= acb
->remaining_sectors
;
422 if (s
->crypt_method
) {
423 acb
->cur_nr_sectors
= MIN(acb
->cur_nr_sectors
,
424 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
427 ret
= qcow2_get_cluster_offset(bs
, acb
->sector_num
<< 9,
428 &acb
->cur_nr_sectors
, &acb
->cluster_offset
);
433 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
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);
439 if (!acb
->cluster_offset
) {
441 if (bs
->backing_hd
) {
442 /* read from the base image */
443 n1
= qcow2_backing_read1(bs
->backing_hd
, &acb
->hd_qiov
,
444 acb
->sector_num
, acb
->cur_nr_sectors
);
446 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
447 acb
->hd_aiocb
= bdrv_aio_readv(bs
->backing_hd
, acb
->sector_num
,
448 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
449 qcow_aio_read_cb
, acb
);
450 if (acb
->hd_aiocb
== NULL
)
453 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
458 /* Note: in this case, no need to wait */
459 qemu_iovec_memset(&acb
->hd_qiov
, 0, 512 * acb
->cur_nr_sectors
);
460 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
464 } else if (acb
->cluster_offset
& QCOW_OFLAG_COMPRESSED
) {
465 /* add AIO support for compressed blocks ? */
466 if (qcow2_decompress_cluster(bs
, acb
->cluster_offset
) < 0)
469 qemu_iovec_from_buffer(&acb
->hd_qiov
,
470 s
->cluster_cache
+ index_in_cluster
* 512,
471 512 * acb
->cur_nr_sectors
);
473 ret
= qcow_schedule_bh(qcow_aio_read_bh
, acb
);
477 if ((acb
->cluster_offset
& 511) != 0) {
482 if (s
->crypt_method
) {
484 * For encrypted images, read everything into a temporary
485 * contiguous buffer on which the AES functions can work.
487 if (!acb
->cluster_data
) {
489 qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
492 assert(acb
->cur_nr_sectors
<=
493 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
494 qemu_iovec_reset(&acb
->hd_qiov
);
495 qemu_iovec_add(&acb
->hd_qiov
, acb
->cluster_data
,
496 512 * acb
->cur_nr_sectors
);
499 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
500 acb
->hd_aiocb
= bdrv_aio_readv(bs
->file
,
501 (acb
->cluster_offset
>> 9) + index_in_cluster
,
502 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
503 qcow_aio_read_cb
, acb
);
504 if (acb
->hd_aiocb
== NULL
) {
512 acb
->common
.cb(acb
->common
.opaque
, ret
);
513 qemu_iovec_destroy(&acb
->hd_qiov
);
514 qemu_aio_release(acb
);
517 static QCowAIOCB
*qcow_aio_setup(BlockDriverState
*bs
,
518 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
519 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
523 acb
= qemu_aio_get(&qcow_aio_pool
, bs
, cb
, opaque
);
526 acb
->hd_aiocb
= NULL
;
527 acb
->sector_num
= sector_num
;
530 qemu_iovec_init(&acb
->hd_qiov
, qiov
->niov
);
533 acb
->remaining_sectors
= nb_sectors
;
534 acb
->cur_nr_sectors
= 0;
535 acb
->cluster_offset
= 0;
536 acb
->l2meta
.nb_clusters
= 0;
537 QLIST_INIT(&acb
->l2meta
.dependent_requests
);
541 static BlockDriverAIOCB
*qcow_aio_readv(BlockDriverState
*bs
,
542 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
543 BlockDriverCompletionFunc
*cb
, void *opaque
)
547 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
551 qcow_aio_read_cb(acb
, 0);
555 static void qcow_aio_write_cb(void *opaque
, int ret
);
557 static void run_dependent_requests(QCowL2Meta
*m
)
562 /* Take the request off the list of running requests */
563 if (m
->nb_clusters
!= 0) {
564 QLIST_REMOVE(m
, next_in_flight
);
567 /* Restart all dependent requests */
568 QLIST_FOREACH_SAFE(req
, &m
->dependent_requests
, next_depend
, next
) {
569 qcow_aio_write_cb(req
, 0);
572 /* Empty the list for the next part of the request */
573 QLIST_INIT(&m
->dependent_requests
);
576 static void qcow_aio_write_cb(void *opaque
, int ret
)
578 QCowAIOCB
*acb
= opaque
;
579 BlockDriverState
*bs
= acb
->common
.bs
;
580 BDRVQcowState
*s
= bs
->opaque
;
581 int index_in_cluster
;
584 acb
->hd_aiocb
= NULL
;
587 ret
= qcow2_alloc_cluster_link_l2(bs
, &acb
->l2meta
);
590 run_dependent_requests(&acb
->l2meta
);
595 acb
->remaining_sectors
-= acb
->cur_nr_sectors
;
596 acb
->sector_num
+= acb
->cur_nr_sectors
;
597 acb
->bytes_done
+= acb
->cur_nr_sectors
* 512;
599 if (acb
->remaining_sectors
== 0) {
600 /* request completed */
605 index_in_cluster
= acb
->sector_num
& (s
->cluster_sectors
- 1);
606 n_end
= index_in_cluster
+ acb
->remaining_sectors
;
607 if (s
->crypt_method
&&
608 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
)
609 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
611 ret
= qcow2_alloc_cluster_offset(bs
, acb
->sector_num
<< 9,
612 index_in_cluster
, n_end
, &acb
->cur_nr_sectors
, &acb
->l2meta
);
617 acb
->cluster_offset
= acb
->l2meta
.cluster_offset
;
619 /* Need to wait for another request? If so, we are done for now. */
620 if (acb
->l2meta
.nb_clusters
== 0 && acb
->l2meta
.depends_on
!= NULL
) {
621 QLIST_INSERT_HEAD(&acb
->l2meta
.depends_on
->dependent_requests
,
626 assert((acb
->cluster_offset
& 511) == 0);
628 qemu_iovec_reset(&acb
->hd_qiov
);
629 qemu_iovec_copy(&acb
->hd_qiov
, acb
->qiov
, acb
->bytes_done
,
630 acb
->cur_nr_sectors
* 512);
632 if (s
->crypt_method
) {
633 if (!acb
->cluster_data
) {
634 acb
->cluster_data
= qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS
*
638 assert(acb
->hd_qiov
.size
<= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
639 qemu_iovec_to_buffer(&acb
->hd_qiov
, acb
->cluster_data
);
641 qcow2_encrypt_sectors(s
, acb
->sector_num
, acb
->cluster_data
,
642 acb
->cluster_data
, acb
->cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
644 qemu_iovec_reset(&acb
->hd_qiov
);
645 qemu_iovec_add(&acb
->hd_qiov
, acb
->cluster_data
,
646 acb
->cur_nr_sectors
* 512);
649 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
650 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
,
651 (acb
->cluster_offset
>> 9) + index_in_cluster
,
652 &acb
->hd_qiov
, acb
->cur_nr_sectors
,
653 qcow_aio_write_cb
, acb
);
654 if (acb
->hd_aiocb
== NULL
) {
662 if (acb
->l2meta
.nb_clusters
!= 0) {
663 QLIST_REMOVE(&acb
->l2meta
, next_in_flight
);
666 acb
->common
.cb(acb
->common
.opaque
, ret
);
667 qemu_iovec_destroy(&acb
->hd_qiov
);
668 qemu_aio_release(acb
);
671 static BlockDriverAIOCB
*qcow_aio_writev(BlockDriverState
*bs
,
672 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
673 BlockDriverCompletionFunc
*cb
, void *opaque
)
675 BDRVQcowState
*s
= bs
->opaque
;
678 s
->cluster_cache_offset
= -1; /* disable compressed cache */
680 acb
= qcow_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
684 qcow_aio_write_cb(acb
, 0);
688 static void qcow_close(BlockDriverState
*bs
)
690 BDRVQcowState
*s
= bs
->opaque
;
691 qemu_free(s
->l1_table
);
692 qemu_free(s
->l2_cache
);
693 qemu_free(s
->cluster_cache
);
694 qemu_free(s
->cluster_data
);
695 qcow2_refcount_close(bs
);
699 * Updates the variable length parts of the qcow2 header, i.e. the backing file
700 * name and all extensions. qcow2 was not designed to allow such changes, so if
701 * we run out of space (we can only use the first cluster) this function may
704 * Returns 0 on success, -errno in error cases.
706 static int qcow2_update_ext_header(BlockDriverState
*bs
,
707 const char *backing_file
, const char *backing_fmt
)
709 size_t backing_file_len
= 0;
710 size_t backing_fmt_len
= 0;
711 BDRVQcowState
*s
= bs
->opaque
;
712 QCowExtension ext_backing_fmt
= {0, 0};
715 /* Backing file format doesn't make sense without a backing file */
716 if (backing_fmt
&& !backing_file
) {
720 /* Prepare the backing file format extension if needed */
722 ext_backing_fmt
.len
= cpu_to_be32(strlen(backing_fmt
));
723 ext_backing_fmt
.magic
= cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT
);
724 backing_fmt_len
= ((sizeof(ext_backing_fmt
)
725 + strlen(backing_fmt
) + 7) & ~7);
728 /* Check if we can fit the new header into the first cluster */
730 backing_file_len
= strlen(backing_file
);
733 size_t header_size
= sizeof(QCowHeader
) + backing_file_len
736 if (header_size
> s
->cluster_size
) {
740 /* Rewrite backing file name and qcow2 extensions */
741 size_t ext_size
= header_size
- sizeof(QCowHeader
);
742 uint8_t buf
[ext_size
];
744 size_t backing_file_offset
= 0;
748 int padding
= backing_fmt_len
-
749 (sizeof(ext_backing_fmt
) + strlen(backing_fmt
));
751 memcpy(buf
+ offset
, &ext_backing_fmt
, sizeof(ext_backing_fmt
));
752 offset
+= sizeof(ext_backing_fmt
);
754 memcpy(buf
+ offset
, backing_fmt
, strlen(backing_fmt
));
755 offset
+= strlen(backing_fmt
);
757 memset(buf
+ offset
, 0, padding
);
761 memcpy(buf
+ offset
, backing_file
, backing_file_len
);
762 backing_file_offset
= sizeof(QCowHeader
) + offset
;
765 ret
= bdrv_pwrite_sync(bs
->file
, sizeof(QCowHeader
), buf
, ext_size
);
770 /* Update header fields */
771 uint64_t be_backing_file_offset
= cpu_to_be64(backing_file_offset
);
772 uint32_t be_backing_file_size
= cpu_to_be32(backing_file_len
);
774 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_offset
),
775 &be_backing_file_offset
, sizeof(uint64_t));
780 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, backing_file_size
),
781 &be_backing_file_size
, sizeof(uint32_t));
791 static int qcow2_change_backing_file(BlockDriverState
*bs
,
792 const char *backing_file
, const char *backing_fmt
)
794 return qcow2_update_ext_header(bs
, backing_file
, backing_fmt
);
797 static int get_bits_from_size(size_t size
)
806 /* Not a power of two */
819 static int preallocate(BlockDriverState
*bs
)
827 nb_sectors
= bdrv_getlength(bs
) >> 9;
829 QLIST_INIT(&meta
.dependent_requests
);
830 meta
.cluster_offset
= 0;
833 num
= MIN(nb_sectors
, INT_MAX
>> 9);
834 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
, &meta
);
839 ret
= qcow2_alloc_cluster_link_l2(bs
, &meta
);
841 qcow2_free_any_clusters(bs
, meta
.cluster_offset
, meta
.nb_clusters
);
845 /* There are no dependent requests, but we need to remove our request
846 * from the list of in-flight requests */
847 run_dependent_requests(&meta
);
849 /* TODO Preallocate data if requested */
856 * It is expected that the image file is large enough to actually contain
857 * all of the allocated clusters (otherwise we get failing reads after
858 * EOF). Extend the image to the last allocated sector.
860 if (meta
.cluster_offset
!= 0) {
863 ret
= bdrv_write(bs
->file
, (meta
.cluster_offset
>> 9) + num
- 1, buf
, 1);
872 static int qcow_create2(const char *filename
, int64_t total_size
,
873 const char *backing_file
, const char *backing_format
,
874 int flags
, size_t cluster_size
, int prealloc
)
877 int fd
, header_size
, backing_filename_len
, l1_size
, i
, shift
, l2_bits
;
878 int ref_clusters
, reftable_clusters
, backing_format_len
= 0;
879 int rounded_ext_bf_len
= 0;
881 uint64_t tmp
, offset
;
882 uint64_t old_ref_clusters
;
883 QCowCreateState s1
, *s
= &s1
;
884 QCowExtension ext_bf
= {0, 0};
887 memset(s
, 0, sizeof(*s
));
889 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0644);
892 memset(&header
, 0, sizeof(header
));
893 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
894 header
.version
= cpu_to_be32(QCOW_VERSION
);
895 header
.size
= cpu_to_be64(total_size
* 512);
896 header_size
= sizeof(header
);
897 backing_filename_len
= 0;
899 if (backing_format
) {
900 ext_bf
.magic
= QCOW_EXT_MAGIC_BACKING_FORMAT
;
901 backing_format_len
= strlen(backing_format
);
902 ext_bf
.len
= backing_format_len
;
903 rounded_ext_bf_len
= (sizeof(ext_bf
) + ext_bf
.len
+ 7) & ~7;
904 header_size
+= rounded_ext_bf_len
;
906 header
.backing_file_offset
= cpu_to_be64(header_size
);
907 backing_filename_len
= strlen(backing_file
);
908 header
.backing_file_size
= cpu_to_be32(backing_filename_len
);
909 header_size
+= backing_filename_len
;
913 s
->cluster_bits
= get_bits_from_size(cluster_size
);
914 if (s
->cluster_bits
< MIN_CLUSTER_BITS
||
915 s
->cluster_bits
> MAX_CLUSTER_BITS
)
917 fprintf(stderr
, "Cluster size must be a power of two between "
919 1 << MIN_CLUSTER_BITS
,
920 1 << (MAX_CLUSTER_BITS
- 10));
923 s
->cluster_size
= 1 << s
->cluster_bits
;
925 header
.cluster_bits
= cpu_to_be32(s
->cluster_bits
);
926 header_size
= (header_size
+ 7) & ~7;
927 if (flags
& BLOCK_FLAG_ENCRYPT
) {
928 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
930 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
932 l2_bits
= s
->cluster_bits
- 3;
933 shift
= s
->cluster_bits
+ l2_bits
;
934 l1_size
= (((total_size
* 512) + (1LL << shift
) - 1) >> shift
);
935 offset
= align_offset(header_size
, s
->cluster_size
);
936 s
->l1_table_offset
= offset
;
937 header
.l1_table_offset
= cpu_to_be64(s
->l1_table_offset
);
938 header
.l1_size
= cpu_to_be32(l1_size
);
939 offset
+= align_offset(l1_size
* sizeof(uint64_t), s
->cluster_size
);
941 /* count how many refcount blocks needed */
943 #define NUM_CLUSTERS(bytes) \
944 (((bytes) + (s->cluster_size) - 1) / (s->cluster_size))
946 ref_clusters
= NUM_CLUSTERS(NUM_CLUSTERS(offset
) * sizeof(uint16_t));
949 uint64_t image_clusters
;
950 old_ref_clusters
= ref_clusters
;
952 /* Number of clusters used for the refcount table */
953 reftable_clusters
= NUM_CLUSTERS(ref_clusters
* sizeof(uint64_t));
955 /* Number of clusters that the whole image will have */
956 image_clusters
= NUM_CLUSTERS(offset
) + ref_clusters
959 /* Number of refcount blocks needed for the image */
960 ref_clusters
= NUM_CLUSTERS(image_clusters
* sizeof(uint16_t));
962 } while (ref_clusters
!= old_ref_clusters
);
964 s
->refcount_table
= qemu_mallocz(reftable_clusters
* s
->cluster_size
);
966 s
->refcount_table_offset
= offset
;
967 header
.refcount_table_offset
= cpu_to_be64(offset
);
968 header
.refcount_table_clusters
= cpu_to_be32(reftable_clusters
);
969 offset
+= (reftable_clusters
* s
->cluster_size
);
970 s
->refcount_block_offset
= offset
;
972 for (i
=0; i
< ref_clusters
; i
++) {
973 s
->refcount_table
[i
] = cpu_to_be64(offset
);
974 offset
+= s
->cluster_size
;
977 s
->refcount_block
= qemu_mallocz(ref_clusters
* s
->cluster_size
);
979 /* update refcounts */
980 qcow2_create_refcount_update(s
, 0, header_size
);
981 qcow2_create_refcount_update(s
, s
->l1_table_offset
,
982 l1_size
* sizeof(uint64_t));
983 qcow2_create_refcount_update(s
, s
->refcount_table_offset
,
984 reftable_clusters
* s
->cluster_size
);
985 qcow2_create_refcount_update(s
, s
->refcount_block_offset
,
986 ref_clusters
* s
->cluster_size
);
988 /* write all the data */
989 ret
= qemu_write_full(fd
, &header
, sizeof(header
));
990 if (ret
!= sizeof(header
)) {
995 if (backing_format_len
) {
997 int padding
= rounded_ext_bf_len
- (ext_bf
.len
+ sizeof(ext_bf
));
999 memset(zero
, 0, sizeof(zero
));
1000 cpu_to_be32s(&ext_bf
.magic
);
1001 cpu_to_be32s(&ext_bf
.len
);
1002 ret
= qemu_write_full(fd
, &ext_bf
, sizeof(ext_bf
));
1003 if (ret
!= sizeof(ext_bf
)) {
1007 ret
= qemu_write_full(fd
, backing_format
, backing_format_len
);
1008 if (ret
!= backing_format_len
) {
1013 ret
= qemu_write_full(fd
, zero
, padding
);
1014 if (ret
!= padding
) {
1020 ret
= qemu_write_full(fd
, backing_file
, backing_filename_len
);
1021 if (ret
!= backing_filename_len
) {
1026 lseek(fd
, s
->l1_table_offset
, SEEK_SET
);
1028 for(i
= 0;i
< l1_size
; i
++) {
1029 ret
= qemu_write_full(fd
, &tmp
, sizeof(tmp
));
1030 if (ret
!= sizeof(tmp
)) {
1035 lseek(fd
, s
->refcount_table_offset
, SEEK_SET
);
1036 ret
= qemu_write_full(fd
, s
->refcount_table
,
1037 reftable_clusters
* s
->cluster_size
);
1038 if (ret
!= reftable_clusters
* s
->cluster_size
) {
1043 lseek(fd
, s
->refcount_block_offset
, SEEK_SET
);
1044 ret
= qemu_write_full(fd
, s
->refcount_block
,
1045 ref_clusters
* s
->cluster_size
);
1046 if (ret
!= ref_clusters
* s
->cluster_size
) {
1053 qemu_free(s
->refcount_table
);
1054 qemu_free(s
->refcount_block
);
1057 /* Preallocate metadata */
1058 if (ret
== 0 && prealloc
) {
1059 BlockDriverState
*bs
;
1060 BlockDriver
*drv
= bdrv_find_format("qcow2");
1062 bdrv_open(bs
, filename
, BDRV_O_CACHE_WB
| BDRV_O_RDWR
, drv
);
1063 ret
= preallocate(bs
);
1070 static int qcow_create(const char *filename
, QEMUOptionParameter
*options
)
1072 const char *backing_file
= NULL
;
1073 const char *backing_fmt
= NULL
;
1074 uint64_t sectors
= 0;
1076 size_t cluster_size
= 65536;
1079 /* Read out options */
1080 while (options
&& options
->name
) {
1081 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1082 sectors
= options
->value
.n
/ 512;
1083 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1084 backing_file
= options
->value
.s
;
1085 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1086 backing_fmt
= options
->value
.s
;
1087 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1088 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1089 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1090 if (options
->value
.n
) {
1091 cluster_size
= options
->value
.n
;
1093 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1094 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1096 } else if (!strcmp(options
->value
.s
, "metadata")) {
1099 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1107 if (backing_file
&& prealloc
) {
1108 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1113 return qcow_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1114 cluster_size
, prealloc
);
1117 static int qcow_make_empty(BlockDriverState
*bs
)
1120 /* XXX: not correct */
1121 BDRVQcowState
*s
= bs
->opaque
;
1122 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1125 memset(s
->l1_table
, 0, l1_length
);
1126 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1128 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1137 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1139 BDRVQcowState
*s
= bs
->opaque
;
1140 int ret
, new_l1_size
;
1146 /* cannot proceed if image has snapshots */
1147 if (s
->nb_snapshots
) {
1151 /* shrinking is currently not supported */
1152 if (offset
< bs
->total_sectors
* 512) {
1156 new_l1_size
= size_to_l1(s
, offset
);
1157 ret
= qcow2_grow_l1_table(bs
, new_l1_size
);
1162 /* write updated header.size */
1163 offset
= cpu_to_be64(offset
);
1164 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1165 &offset
, sizeof(uint64_t));
1170 s
->l1_vm_state_index
= new_l1_size
;
1174 /* XXX: put compressed sectors first, then all the cluster aligned
1175 tables to avoid losing bytes in alignment */
1176 static int qcow_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1177 const uint8_t *buf
, int nb_sectors
)
1179 BDRVQcowState
*s
= bs
->opaque
;
1183 uint64_t cluster_offset
;
1185 if (nb_sectors
== 0) {
1186 /* align end of file to a sector boundary to ease reading with
1187 sector based I/Os */
1188 cluster_offset
= bdrv_getlength(bs
->file
);
1189 cluster_offset
= (cluster_offset
+ 511) & ~511;
1190 bdrv_truncate(bs
->file
, cluster_offset
);
1194 if (nb_sectors
!= s
->cluster_sectors
)
1197 out_buf
= qemu_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1199 /* best compression, small window, no zlib header */
1200 memset(&strm
, 0, sizeof(strm
));
1201 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1203 9, Z_DEFAULT_STRATEGY
);
1209 strm
.avail_in
= s
->cluster_size
;
1210 strm
.next_in
= (uint8_t *)buf
;
1211 strm
.avail_out
= s
->cluster_size
;
1212 strm
.next_out
= out_buf
;
1214 ret
= deflate(&strm
, Z_FINISH
);
1215 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1220 out_len
= strm
.next_out
- out_buf
;
1224 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1225 /* could not compress: write normal cluster */
1226 bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1228 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1229 sector_num
<< 9, out_len
);
1230 if (!cluster_offset
)
1232 cluster_offset
&= s
->cluster_offset_mask
;
1233 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1234 if (bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
) != out_len
) {
1244 static void qcow_flush(BlockDriverState
*bs
)
1246 bdrv_flush(bs
->file
);
1249 static BlockDriverAIOCB
*qcow_aio_flush(BlockDriverState
*bs
,
1250 BlockDriverCompletionFunc
*cb
, void *opaque
)
1252 return bdrv_aio_flush(bs
->file
, cb
, opaque
);
1255 static int64_t qcow_vm_state_offset(BDRVQcowState
*s
)
1257 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1260 static int qcow_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1262 BDRVQcowState
*s
= bs
->opaque
;
1263 bdi
->cluster_size
= s
->cluster_size
;
1264 bdi
->vm_state_offset
= qcow_vm_state_offset(s
);
1269 static int qcow_check(BlockDriverState
*bs
, BdrvCheckResult
*result
)
1271 return qcow2_check_refcounts(bs
, result
);
1275 static void dump_refcounts(BlockDriverState
*bs
)
1277 BDRVQcowState
*s
= bs
->opaque
;
1278 int64_t nb_clusters
, k
, k1
, size
;
1281 size
= bdrv_getlength(bs
->file
);
1282 nb_clusters
= size_to_clusters(s
, size
);
1283 for(k
= 0; k
< nb_clusters
;) {
1285 refcount
= get_refcount(bs
, k
);
1287 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1289 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1295 static int qcow_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1296 int64_t pos
, int size
)
1298 BDRVQcowState
*s
= bs
->opaque
;
1299 int growable
= bs
->growable
;
1302 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1304 ret
= bdrv_pwrite(bs
, qcow_vm_state_offset(s
) + pos
, buf
, size
);
1305 bs
->growable
= growable
;
1310 static int qcow_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1311 int64_t pos
, int size
)
1313 BDRVQcowState
*s
= bs
->opaque
;
1314 int growable
= bs
->growable
;
1317 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1319 ret
= bdrv_pread(bs
, qcow_vm_state_offset(s
) + pos
, buf
, size
);
1320 bs
->growable
= growable
;
1325 static QEMUOptionParameter qcow_create_options
[] = {
1327 .name
= BLOCK_OPT_SIZE
,
1329 .help
= "Virtual disk size"
1332 .name
= BLOCK_OPT_BACKING_FILE
,
1334 .help
= "File name of a base image"
1337 .name
= BLOCK_OPT_BACKING_FMT
,
1339 .help
= "Image format of the base image"
1342 .name
= BLOCK_OPT_ENCRYPT
,
1344 .help
= "Encrypt the image"
1347 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1349 .help
= "qcow2 cluster size"
1352 .name
= BLOCK_OPT_PREALLOC
,
1354 .help
= "Preallocation mode (allowed values: off, metadata)"
1359 static BlockDriver bdrv_qcow2
= {
1360 .format_name
= "qcow2",
1361 .instance_size
= sizeof(BDRVQcowState
),
1362 .bdrv_probe
= qcow_probe
,
1363 .bdrv_open
= qcow_open
,
1364 .bdrv_close
= qcow_close
,
1365 .bdrv_create
= qcow_create
,
1366 .bdrv_flush
= qcow_flush
,
1367 .bdrv_is_allocated
= qcow_is_allocated
,
1368 .bdrv_set_key
= qcow_set_key
,
1369 .bdrv_make_empty
= qcow_make_empty
,
1371 .bdrv_aio_readv
= qcow_aio_readv
,
1372 .bdrv_aio_writev
= qcow_aio_writev
,
1373 .bdrv_aio_flush
= qcow_aio_flush
,
1375 .bdrv_truncate
= qcow2_truncate
,
1376 .bdrv_write_compressed
= qcow_write_compressed
,
1378 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1379 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1380 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1381 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1382 .bdrv_get_info
= qcow_get_info
,
1384 .bdrv_save_vmstate
= qcow_save_vmstate
,
1385 .bdrv_load_vmstate
= qcow_load_vmstate
,
1387 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1389 .create_options
= qcow_create_options
,
1390 .bdrv_check
= qcow_check
,
1393 static void bdrv_qcow2_init(void)
1395 bdrv_register(&bdrv_qcow2
);
1398 block_init(bdrv_qcow2_init
);