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/block_int.h"
26 #include "qemu/module.h"
28 #include "block/aes.h"
29 #include "block/qcow2.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qbool.h"
36 Differences with QCOW:
38 - Support for multiple incremental snapshots.
39 - Memory management by reference counts.
40 - Clusters which have a reference count of one have the bit
41 QCOW_OFLAG_COPIED to optimize write performance.
42 - Size of compressed clusters is stored in sectors to reduce bit usage
43 in the cluster offsets.
44 - Support for storing additional data (such as the VM state) in the
46 - If a backing store is used, the cluster size is not constrained
47 (could be backported to QCOW).
48 - L2 tables have always a size of one cluster.
57 #define QCOW2_EXT_MAGIC_END 0
58 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
59 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
61 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
63 const QCowHeader
*cow_header
= (const void *)buf
;
65 if (buf_size
>= sizeof(QCowHeader
) &&
66 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
67 be32_to_cpu(cow_header
->version
) >= 2)
75 * read qcow2 extension and fill bs
76 * start reading from start_offset
77 * finish reading upon magic of value 0 or when end_offset reached
78 * unknown magic is skipped (future extension this version knows nothing about)
79 * return 0 upon success, non-0 otherwise
81 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
82 uint64_t end_offset
, void **p_feature_table
)
84 BDRVQcowState
*s
= bs
->opaque
;
90 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
92 offset
= start_offset
;
93 while (offset
< end_offset
) {
97 if (offset
> s
->cluster_size
)
98 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
100 printf("attempting to read extended header in offset %lu\n", offset
);
103 if (bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
104 fprintf(stderr
, "qcow2_read_extension: ERROR: "
105 "pread fail from offset %" PRIu64
"\n",
109 be32_to_cpus(&ext
.magic
);
110 be32_to_cpus(&ext
.len
);
111 offset
+= sizeof(ext
);
113 printf("ext.magic = 0x%x\n", ext
.magic
);
115 if (ext
.len
> end_offset
- offset
) {
116 error_report("Header extension too large");
121 case QCOW2_EXT_MAGIC_END
:
124 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
125 if (ext
.len
>= sizeof(bs
->backing_format
)) {
126 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
128 ext
.len
, sizeof(bs
->backing_format
));
131 if (bdrv_pread(bs
->file
, offset
, bs
->backing_format
,
134 bs
->backing_format
[ext
.len
] = '\0';
136 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
140 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
141 if (p_feature_table
!= NULL
) {
142 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
143 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
148 *p_feature_table
= feature_table
;
153 /* unknown magic - save it in case we need to rewrite the header */
155 Qcow2UnknownHeaderExtension
*uext
;
157 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
158 uext
->magic
= ext
.magic
;
160 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
162 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
170 offset
+= ((ext
.len
+ 7) & ~7);
176 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
178 BDRVQcowState
*s
= bs
->opaque
;
179 Qcow2UnknownHeaderExtension
*uext
, *next
;
181 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
182 QLIST_REMOVE(uext
, next
);
187 static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState
*bs
,
188 const char *fmt
, ...)
194 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
197 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
198 bs
->device_name
, "qcow2", msg
);
201 static void report_unsupported_feature(BlockDriverState
*bs
,
202 Qcow2Feature
*table
, uint64_t mask
)
204 while (table
&& table
->name
[0] != '\0') {
205 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
206 if (mask
& (1 << table
->bit
)) {
207 report_unsupported(bs
, "%.46s",table
->name
);
208 mask
&= ~(1 << table
->bit
);
215 report_unsupported(bs
, "Unknown incompatible feature: %" PRIx64
, mask
);
220 * Sets the dirty bit and flushes afterwards if necessary.
222 * The incompatible_features bit is only set if the image file header was
223 * updated successfully. Therefore it is not required to check the return
224 * value of this function.
226 int qcow2_mark_dirty(BlockDriverState
*bs
)
228 BDRVQcowState
*s
= bs
->opaque
;
232 assert(s
->qcow_version
>= 3);
234 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
235 return 0; /* already dirty */
238 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
239 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
244 ret
= bdrv_flush(bs
->file
);
249 /* Only treat image as dirty if the header was updated successfully */
250 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
255 * Clears the dirty bit and flushes before if necessary. Only call this
256 * function when there are no pending requests, it does not guard against
257 * concurrent requests dirtying the image.
259 static int qcow2_mark_clean(BlockDriverState
*bs
)
261 BDRVQcowState
*s
= bs
->opaque
;
263 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
264 int ret
= bdrv_flush(bs
);
269 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
270 return qcow2_update_header(bs
);
275 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
278 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
283 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
284 return qcow2_mark_clean(bs
);
289 static QemuOptsList qcow2_runtime_opts
= {
291 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
294 .name
= "lazy_refcounts",
295 .type
= QEMU_OPT_BOOL
,
296 .help
= "Postpone refcount updates",
298 { /* end of list */ }
302 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
304 BDRVQcowState
*s
= bs
->opaque
;
308 Error
*local_err
= NULL
;
311 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
315 be32_to_cpus(&header
.magic
);
316 be32_to_cpus(&header
.version
);
317 be64_to_cpus(&header
.backing_file_offset
);
318 be32_to_cpus(&header
.backing_file_size
);
319 be64_to_cpus(&header
.size
);
320 be32_to_cpus(&header
.cluster_bits
);
321 be32_to_cpus(&header
.crypt_method
);
322 be64_to_cpus(&header
.l1_table_offset
);
323 be32_to_cpus(&header
.l1_size
);
324 be64_to_cpus(&header
.refcount_table_offset
);
325 be32_to_cpus(&header
.refcount_table_clusters
);
326 be64_to_cpus(&header
.snapshots_offset
);
327 be32_to_cpus(&header
.nb_snapshots
);
329 if (header
.magic
!= QCOW_MAGIC
) {
333 if (header
.version
< 2 || header
.version
> 3) {
334 report_unsupported(bs
, "QCOW version %d", header
.version
);
339 s
->qcow_version
= header
.version
;
341 /* Initialise version 3 header fields */
342 if (header
.version
== 2) {
343 header
.incompatible_features
= 0;
344 header
.compatible_features
= 0;
345 header
.autoclear_features
= 0;
346 header
.refcount_order
= 4;
347 header
.header_length
= 72;
349 be64_to_cpus(&header
.incompatible_features
);
350 be64_to_cpus(&header
.compatible_features
);
351 be64_to_cpus(&header
.autoclear_features
);
352 be32_to_cpus(&header
.refcount_order
);
353 be32_to_cpus(&header
.header_length
);
356 if (header
.header_length
> sizeof(header
)) {
357 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
358 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
359 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
360 s
->unknown_header_fields_size
);
366 if (header
.backing_file_offset
) {
367 ext_end
= header
.backing_file_offset
;
369 ext_end
= 1 << header
.cluster_bits
;
372 /* Handle feature bits */
373 s
->incompatible_features
= header
.incompatible_features
;
374 s
->compatible_features
= header
.compatible_features
;
375 s
->autoclear_features
= header
.autoclear_features
;
377 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
378 void *feature_table
= NULL
;
379 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
381 report_unsupported_feature(bs
, feature_table
,
382 s
->incompatible_features
&
383 ~QCOW2_INCOMPAT_MASK
);
388 /* Check support for various header values */
389 if (header
.refcount_order
!= 4) {
390 report_unsupported(bs
, "%d bit reference counts",
391 1 << header
.refcount_order
);
396 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
397 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
401 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
405 s
->crypt_method_header
= header
.crypt_method
;
406 if (s
->crypt_method_header
) {
409 s
->cluster_bits
= header
.cluster_bits
;
410 s
->cluster_size
= 1 << s
->cluster_bits
;
411 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
412 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
413 s
->l2_size
= 1 << s
->l2_bits
;
414 bs
->total_sectors
= header
.size
/ 512;
415 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
416 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
417 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
418 s
->refcount_table_offset
= header
.refcount_table_offset
;
419 s
->refcount_table_size
=
420 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
422 s
->snapshots_offset
= header
.snapshots_offset
;
423 s
->nb_snapshots
= header
.nb_snapshots
;
425 /* read the level 1 table */
426 s
->l1_size
= header
.l1_size
;
427 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
428 /* the L1 table must contain at least enough entries to put
430 if (s
->l1_size
< s
->l1_vm_state_index
) {
434 s
->l1_table_offset
= header
.l1_table_offset
;
435 if (s
->l1_size
> 0) {
436 s
->l1_table
= g_malloc0(
437 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
438 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
439 s
->l1_size
* sizeof(uint64_t));
443 for(i
= 0;i
< s
->l1_size
; i
++) {
444 be64_to_cpus(&s
->l1_table
[i
]);
448 /* alloc L2 table/refcount block cache */
449 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
450 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
452 s
->cluster_cache
= g_malloc(s
->cluster_size
);
453 /* one more sector for decompressed data alignment */
454 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
456 s
->cluster_cache_offset
= -1;
459 ret
= qcow2_refcount_init(bs
);
464 QLIST_INIT(&s
->cluster_allocs
);
466 /* read qcow2 extensions */
467 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
)) {
472 /* read the backing file name */
473 if (header
.backing_file_offset
!= 0) {
474 len
= header
.backing_file_size
;
478 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
479 bs
->backing_file
, len
);
483 bs
->backing_file
[len
] = '\0';
486 ret
= qcow2_read_snapshots(bs
);
491 /* Clear unknown autoclear feature bits */
492 if (!bs
->read_only
&& s
->autoclear_features
!= 0) {
493 s
->autoclear_features
= 0;
494 ret
= qcow2_update_header(bs
);
500 /* Initialise locks */
501 qemu_co_mutex_init(&s
->lock
);
503 /* Repair image if dirty */
504 if (!(flags
& BDRV_O_CHECK
) && !bs
->read_only
&&
505 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
506 BdrvCheckResult result
= {0};
508 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
514 /* Enable lazy_refcounts according to image and command line options */
515 opts
= qemu_opts_create_nofail(&qcow2_runtime_opts
);
516 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
517 if (error_is_set(&local_err
)) {
518 qerror_report_err(local_err
);
519 error_free(local_err
);
524 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
525 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
529 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
530 qerror_report(ERROR_CLASS_GENERIC_ERROR
, "Lazy refcounts require "
531 "a qcow2 image with at least qemu 1.1 compatibility level");
538 BdrvCheckResult result
= {0};
539 qcow2_check_refcounts(bs
, &result
, 0);
545 g_free(s
->unknown_header_fields
);
546 cleanup_unknown_header_ext(bs
);
547 qcow2_free_snapshots(bs
);
548 qcow2_refcount_close(bs
);
550 if (s
->l2_table_cache
) {
551 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
553 g_free(s
->cluster_cache
);
554 qemu_vfree(s
->cluster_data
);
558 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
560 BDRVQcowState
*s
= bs
->opaque
;
564 memset(keybuf
, 0, 16);
568 /* XXX: we could compress the chars to 7 bits to increase
570 for(i
= 0;i
< len
;i
++) {
573 s
->crypt_method
= s
->crypt_method_header
;
575 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
577 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
587 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
588 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
589 for(i
= 0; i
< 16; i
++)
590 printf(" %02x", tmp
[i
]);
592 for(i
= 0; i
< 16; i
++)
593 printf(" %02x", out
[i
]);
600 /* We have nothing to do for QCOW2 reopen, stubs just return
602 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
603 BlockReopenQueue
*queue
, Error
**errp
)
608 static int coroutine_fn
qcow2_co_is_allocated(BlockDriverState
*bs
,
609 int64_t sector_num
, int nb_sectors
, int *pnum
)
611 BDRVQcowState
*s
= bs
->opaque
;
612 uint64_t cluster_offset
;
616 /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
617 * can't pass them on today */
618 qemu_co_mutex_lock(&s
->lock
);
619 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
620 qemu_co_mutex_unlock(&s
->lock
);
625 return (cluster_offset
!= 0) || (ret
== QCOW2_CLUSTER_ZERO
);
628 /* handle reading after the end of the backing file */
629 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
630 int64_t sector_num
, int nb_sectors
)
633 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
635 if (sector_num
>= bs
->total_sectors
)
638 n1
= bs
->total_sectors
- sector_num
;
640 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
645 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
646 int remaining_sectors
, QEMUIOVector
*qiov
)
648 BDRVQcowState
*s
= bs
->opaque
;
649 int index_in_cluster
, n1
;
651 int cur_nr_sectors
; /* number of sectors in current iteration */
652 uint64_t cluster_offset
= 0;
653 uint64_t bytes_done
= 0;
654 QEMUIOVector hd_qiov
;
655 uint8_t *cluster_data
= NULL
;
657 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
659 qemu_co_mutex_lock(&s
->lock
);
661 while (remaining_sectors
!= 0) {
663 /* prepare next request */
664 cur_nr_sectors
= remaining_sectors
;
665 if (s
->crypt_method
) {
666 cur_nr_sectors
= MIN(cur_nr_sectors
,
667 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
670 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
671 &cur_nr_sectors
, &cluster_offset
);
676 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
678 qemu_iovec_reset(&hd_qiov
);
679 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
680 cur_nr_sectors
* 512);
683 case QCOW2_CLUSTER_UNALLOCATED
:
685 if (bs
->backing_hd
) {
686 /* read from the base image */
687 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
688 sector_num
, cur_nr_sectors
);
690 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
691 qemu_co_mutex_unlock(&s
->lock
);
692 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
694 qemu_co_mutex_lock(&s
->lock
);
700 /* Note: in this case, no need to wait */
701 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
705 case QCOW2_CLUSTER_ZERO
:
706 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
709 case QCOW2_CLUSTER_COMPRESSED
:
710 /* add AIO support for compressed blocks ? */
711 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
716 qemu_iovec_from_buf(&hd_qiov
, 0,
717 s
->cluster_cache
+ index_in_cluster
* 512,
718 512 * cur_nr_sectors
);
721 case QCOW2_CLUSTER_NORMAL
:
722 if ((cluster_offset
& 511) != 0) {
727 if (s
->crypt_method
) {
729 * For encrypted images, read everything into a temporary
730 * contiguous buffer on which the AES functions can work.
734 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
737 assert(cur_nr_sectors
<=
738 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
739 qemu_iovec_reset(&hd_qiov
);
740 qemu_iovec_add(&hd_qiov
, cluster_data
,
741 512 * cur_nr_sectors
);
744 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
745 qemu_co_mutex_unlock(&s
->lock
);
746 ret
= bdrv_co_readv(bs
->file
,
747 (cluster_offset
>> 9) + index_in_cluster
,
748 cur_nr_sectors
, &hd_qiov
);
749 qemu_co_mutex_lock(&s
->lock
);
753 if (s
->crypt_method
) {
754 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
755 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
756 qemu_iovec_from_buf(qiov
, bytes_done
,
757 cluster_data
, 512 * cur_nr_sectors
);
762 g_assert_not_reached();
767 remaining_sectors
-= cur_nr_sectors
;
768 sector_num
+= cur_nr_sectors
;
769 bytes_done
+= cur_nr_sectors
* 512;
774 qemu_co_mutex_unlock(&s
->lock
);
776 qemu_iovec_destroy(&hd_qiov
);
777 qemu_vfree(cluster_data
);
782 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
784 int remaining_sectors
,
787 BDRVQcowState
*s
= bs
->opaque
;
788 int index_in_cluster
;
791 int cur_nr_sectors
; /* number of sectors in current iteration */
792 uint64_t cluster_offset
;
793 QEMUIOVector hd_qiov
;
794 uint64_t bytes_done
= 0;
795 uint8_t *cluster_data
= NULL
;
796 QCowL2Meta
*l2meta
= NULL
;
798 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
801 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
803 s
->cluster_cache_offset
= -1; /* disable compressed cache */
805 qemu_co_mutex_lock(&s
->lock
);
807 while (remaining_sectors
!= 0) {
811 trace_qcow2_writev_start_part(qemu_coroutine_self());
812 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
813 n_end
= index_in_cluster
+ remaining_sectors
;
814 if (s
->crypt_method
&&
815 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
) {
816 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
819 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
820 index_in_cluster
, n_end
, &cur_nr_sectors
, &cluster_offset
, &l2meta
);
825 assert((cluster_offset
& 511) == 0);
827 qemu_iovec_reset(&hd_qiov
);
828 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
829 cur_nr_sectors
* 512);
831 if (s
->crypt_method
) {
833 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
837 assert(hd_qiov
.size
<=
838 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
839 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
841 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
842 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
844 qemu_iovec_reset(&hd_qiov
);
845 qemu_iovec_add(&hd_qiov
, cluster_data
,
846 cur_nr_sectors
* 512);
849 qemu_co_mutex_unlock(&s
->lock
);
850 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
851 trace_qcow2_writev_data(qemu_coroutine_self(),
852 (cluster_offset
>> 9) + index_in_cluster
);
853 ret
= bdrv_co_writev(bs
->file
,
854 (cluster_offset
>> 9) + index_in_cluster
,
855 cur_nr_sectors
, &hd_qiov
);
856 qemu_co_mutex_lock(&s
->lock
);
861 while (l2meta
!= NULL
) {
864 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
869 /* Take the request off the list of running requests */
870 if (l2meta
->nb_clusters
!= 0) {
871 QLIST_REMOVE(l2meta
, next_in_flight
);
874 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
881 remaining_sectors
-= cur_nr_sectors
;
882 sector_num
+= cur_nr_sectors
;
883 bytes_done
+= cur_nr_sectors
* 512;
884 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
889 qemu_co_mutex_unlock(&s
->lock
);
891 while (l2meta
!= NULL
) {
894 if (l2meta
->nb_clusters
!= 0) {
895 QLIST_REMOVE(l2meta
, next_in_flight
);
897 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
904 qemu_iovec_destroy(&hd_qiov
);
905 qemu_vfree(cluster_data
);
906 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
911 static void qcow2_close(BlockDriverState
*bs
)
913 BDRVQcowState
*s
= bs
->opaque
;
916 qcow2_cache_flush(bs
, s
->l2_table_cache
);
917 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
919 qcow2_mark_clean(bs
);
921 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
922 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
924 g_free(s
->unknown_header_fields
);
925 cleanup_unknown_header_ext(bs
);
927 g_free(s
->cluster_cache
);
928 qemu_vfree(s
->cluster_data
);
929 qcow2_refcount_close(bs
);
930 qcow2_free_snapshots(bs
);
933 static void qcow2_invalidate_cache(BlockDriverState
*bs
)
935 BDRVQcowState
*s
= bs
->opaque
;
936 int flags
= s
->flags
;
937 AES_KEY aes_encrypt_key
;
938 AES_KEY aes_decrypt_key
;
939 uint32_t crypt_method
= 0;
943 * Backing files are read-only which makes all of their metadata immutable,
944 * that means we don't have to worry about reopening them here.
947 if (s
->crypt_method
) {
948 crypt_method
= s
->crypt_method
;
949 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
950 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
955 options
= qdict_new();
956 qdict_put(options
, QCOW2_OPT_LAZY_REFCOUNTS
,
957 qbool_from_int(s
->use_lazy_refcounts
));
959 memset(s
, 0, sizeof(BDRVQcowState
));
960 qcow2_open(bs
, options
, flags
);
965 s
->crypt_method
= crypt_method
;
966 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
967 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
971 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
972 size_t len
, size_t buflen
)
974 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
975 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
977 if (buflen
< ext_len
) {
981 *ext_backing_fmt
= (QCowExtension
) {
982 .magic
= cpu_to_be32(magic
),
983 .len
= cpu_to_be32(len
),
985 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
991 * Updates the qcow2 header, including the variable length parts of it, i.e.
992 * the backing file name and all extensions. qcow2 was not designed to allow
993 * such changes, so if we run out of space (we can only use the first cluster)
994 * this function may fail.
996 * Returns 0 on success, -errno in error cases.
998 int qcow2_update_header(BlockDriverState
*bs
)
1000 BDRVQcowState
*s
= bs
->opaque
;
1003 size_t buflen
= s
->cluster_size
;
1005 uint64_t total_size
;
1006 uint32_t refcount_table_clusters
;
1007 size_t header_length
;
1008 Qcow2UnknownHeaderExtension
*uext
;
1010 buf
= qemu_blockalign(bs
, buflen
);
1012 /* Header structure */
1013 header
= (QCowHeader
*) buf
;
1015 if (buflen
< sizeof(*header
)) {
1020 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1021 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1022 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1024 *header
= (QCowHeader
) {
1025 /* Version 2 fields */
1026 .magic
= cpu_to_be32(QCOW_MAGIC
),
1027 .version
= cpu_to_be32(s
->qcow_version
),
1028 .backing_file_offset
= 0,
1029 .backing_file_size
= 0,
1030 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1031 .size
= cpu_to_be64(total_size
),
1032 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1033 .l1_size
= cpu_to_be32(s
->l1_size
),
1034 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1035 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1036 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1037 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1038 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1040 /* Version 3 fields */
1041 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1042 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1043 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1044 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1045 .header_length
= cpu_to_be32(header_length
),
1048 /* For older versions, write a shorter header */
1049 switch (s
->qcow_version
) {
1051 ret
= offsetof(QCowHeader
, incompatible_features
);
1054 ret
= sizeof(*header
);
1063 memset(buf
, 0, buflen
);
1065 /* Preserve any unknown field in the header */
1066 if (s
->unknown_header_fields_size
) {
1067 if (buflen
< s
->unknown_header_fields_size
) {
1072 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1073 buf
+= s
->unknown_header_fields_size
;
1074 buflen
-= s
->unknown_header_fields_size
;
1077 /* Backing file format header extension */
1078 if (*bs
->backing_format
) {
1079 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1080 bs
->backing_format
, strlen(bs
->backing_format
),
1091 Qcow2Feature features
[] = {
1093 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1094 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1095 .name
= "dirty bit",
1098 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1099 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1100 .name
= "lazy refcounts",
1104 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1105 features
, sizeof(features
), buflen
);
1112 /* Keep unknown header extensions */
1113 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1114 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1123 /* End of header extensions */
1124 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1132 /* Backing file name */
1133 if (*bs
->backing_file
) {
1134 size_t backing_file_len
= strlen(bs
->backing_file
);
1136 if (buflen
< backing_file_len
) {
1141 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1142 strncpy(buf
, bs
->backing_file
, buflen
);
1144 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1145 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1148 /* Write the new header */
1149 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1160 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1161 const char *backing_file
, const char *backing_fmt
)
1163 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1164 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1166 return qcow2_update_header(bs
);
1169 static int preallocate(BlockDriverState
*bs
)
1171 uint64_t nb_sectors
;
1173 uint64_t host_offset
= 0;
1178 nb_sectors
= bdrv_getlength(bs
) >> 9;
1181 while (nb_sectors
) {
1182 num
= MIN(nb_sectors
, INT_MAX
>> 9);
1183 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
,
1184 &host_offset
, &meta
);
1189 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1191 qcow2_free_any_clusters(bs
, meta
->alloc_offset
, meta
->nb_clusters
);
1195 /* There are no dependent requests, but we need to remove our request
1196 * from the list of in-flight requests */
1198 QLIST_REMOVE(meta
, next_in_flight
);
1201 /* TODO Preallocate data if requested */
1208 * It is expected that the image file is large enough to actually contain
1209 * all of the allocated clusters (otherwise we get failing reads after
1210 * EOF). Extend the image to the last allocated sector.
1212 if (host_offset
!= 0) {
1214 memset(buf
, 0, 512);
1215 ret
= bdrv_write(bs
->file
, (host_offset
>> 9) + num
- 1, buf
, 1);
1224 static int qcow2_create2(const char *filename
, int64_t total_size
,
1225 const char *backing_file
, const char *backing_format
,
1226 int flags
, size_t cluster_size
, int prealloc
,
1227 QEMUOptionParameter
*options
, int version
)
1229 /* Calculate cluster_bits */
1231 cluster_bits
= ffs(cluster_size
) - 1;
1232 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1233 (1 << cluster_bits
) != cluster_size
)
1236 "Cluster size must be a power of two between %d and %dk",
1237 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1242 * Open the image file and write a minimal qcow2 header.
1244 * We keep things simple and start with a zero-sized image. We also
1245 * do without refcount blocks or a L1 table for now. We'll fix the
1246 * inconsistency later.
1248 * We do need a refcount table because growing the refcount table means
1249 * allocating two new refcount blocks - the seconds of which would be at
1250 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1251 * size for any qcow2 image.
1253 BlockDriverState
* bs
;
1255 uint8_t* refcount_table
;
1258 ret
= bdrv_create_file(filename
, options
);
1263 ret
= bdrv_file_open(&bs
, filename
, NULL
, BDRV_O_RDWR
);
1268 /* Write the header */
1269 memset(&header
, 0, sizeof(header
));
1270 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1271 header
.version
= cpu_to_be32(version
);
1272 header
.cluster_bits
= cpu_to_be32(cluster_bits
);
1273 header
.size
= cpu_to_be64(0);
1274 header
.l1_table_offset
= cpu_to_be64(0);
1275 header
.l1_size
= cpu_to_be32(0);
1276 header
.refcount_table_offset
= cpu_to_be64(cluster_size
);
1277 header
.refcount_table_clusters
= cpu_to_be32(1);
1278 header
.refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
);
1279 header
.header_length
= cpu_to_be32(sizeof(header
));
1281 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1282 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1284 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1287 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1288 header
.compatible_features
|=
1289 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1292 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
1297 /* Write an empty refcount table */
1298 refcount_table
= g_malloc0(cluster_size
);
1299 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1300 g_free(refcount_table
);
1309 * And now open the image and make it consistent first (i.e. increase the
1310 * refcount of the cluster that is occupied by the header and the refcount
1313 BlockDriver
* drv
= bdrv_find_format("qcow2");
1314 assert(drv
!= NULL
);
1315 ret
= bdrv_open(bs
, filename
, NULL
,
1316 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
);
1321 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1325 } else if (ret
!= 0) {
1326 error_report("Huh, first cluster in empty image is already in use?");
1330 /* Okay, now that we have a valid image, let's give it the right size */
1331 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1336 /* Want a backing file? There you go.*/
1338 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1344 /* And if we're supposed to preallocate metadata, do that now */
1346 BDRVQcowState
*s
= bs
->opaque
;
1347 qemu_co_mutex_lock(&s
->lock
);
1348 ret
= preallocate(bs
);
1349 qemu_co_mutex_unlock(&s
->lock
);
1361 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
1363 const char *backing_file
= NULL
;
1364 const char *backing_fmt
= NULL
;
1365 uint64_t sectors
= 0;
1367 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1371 /* Read out options */
1372 while (options
&& options
->name
) {
1373 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1374 sectors
= options
->value
.n
/ 512;
1375 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1376 backing_file
= options
->value
.s
;
1377 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1378 backing_fmt
= options
->value
.s
;
1379 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1380 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1381 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1382 if (options
->value
.n
) {
1383 cluster_size
= options
->value
.n
;
1385 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1386 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1388 } else if (!strcmp(options
->value
.s
, "metadata")) {
1391 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1395 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1396 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "0.10")) {
1398 } else if (!strcmp(options
->value
.s
, "1.1")) {
1401 fprintf(stderr
, "Invalid compatibility level: '%s'\n",
1405 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1406 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1411 if (backing_file
&& prealloc
) {
1412 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1417 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1418 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
1419 "level 1.1 and above (use compat=1.1 or greater)\n");
1423 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1424 cluster_size
, prealloc
, options
, version
);
1427 static int qcow2_make_empty(BlockDriverState
*bs
)
1430 /* XXX: not correct */
1431 BDRVQcowState
*s
= bs
->opaque
;
1432 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1435 memset(s
->l1_table
, 0, l1_length
);
1436 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1438 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1447 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1448 int64_t sector_num
, int nb_sectors
)
1451 BDRVQcowState
*s
= bs
->opaque
;
1453 /* Emulate misaligned zero writes */
1454 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1458 /* Whatever is left can use real zero clusters */
1459 qemu_co_mutex_lock(&s
->lock
);
1460 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1462 qemu_co_mutex_unlock(&s
->lock
);
1467 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1468 int64_t sector_num
, int nb_sectors
)
1471 BDRVQcowState
*s
= bs
->opaque
;
1473 qemu_co_mutex_lock(&s
->lock
);
1474 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1476 qemu_co_mutex_unlock(&s
->lock
);
1480 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1482 BDRVQcowState
*s
= bs
->opaque
;
1483 int ret
, new_l1_size
;
1486 error_report("The new size must be a multiple of 512");
1490 /* cannot proceed if image has snapshots */
1491 if (s
->nb_snapshots
) {
1492 error_report("Can't resize an image which has snapshots");
1496 /* shrinking is currently not supported */
1497 if (offset
< bs
->total_sectors
* 512) {
1498 error_report("qcow2 doesn't support shrinking images yet");
1502 new_l1_size
= size_to_l1(s
, offset
);
1503 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1508 /* write updated header.size */
1509 offset
= cpu_to_be64(offset
);
1510 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1511 &offset
, sizeof(uint64_t));
1516 s
->l1_vm_state_index
= new_l1_size
;
1520 /* XXX: put compressed sectors first, then all the cluster aligned
1521 tables to avoid losing bytes in alignment */
1522 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1523 const uint8_t *buf
, int nb_sectors
)
1525 BDRVQcowState
*s
= bs
->opaque
;
1529 uint64_t cluster_offset
;
1531 if (nb_sectors
== 0) {
1532 /* align end of file to a sector boundary to ease reading with
1533 sector based I/Os */
1534 cluster_offset
= bdrv_getlength(bs
->file
);
1535 cluster_offset
= (cluster_offset
+ 511) & ~511;
1536 bdrv_truncate(bs
->file
, cluster_offset
);
1540 if (nb_sectors
!= s
->cluster_sectors
)
1543 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1545 /* best compression, small window, no zlib header */
1546 memset(&strm
, 0, sizeof(strm
));
1547 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1549 9, Z_DEFAULT_STRATEGY
);
1555 strm
.avail_in
= s
->cluster_size
;
1556 strm
.next_in
= (uint8_t *)buf
;
1557 strm
.avail_out
= s
->cluster_size
;
1558 strm
.next_out
= out_buf
;
1560 ret
= deflate(&strm
, Z_FINISH
);
1561 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1566 out_len
= strm
.next_out
- out_buf
;
1570 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1571 /* could not compress: write normal cluster */
1572 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1577 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1578 sector_num
<< 9, out_len
);
1579 if (!cluster_offset
) {
1583 cluster_offset
&= s
->cluster_offset_mask
;
1584 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1585 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1597 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1599 BDRVQcowState
*s
= bs
->opaque
;
1602 qemu_co_mutex_lock(&s
->lock
);
1603 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1605 qemu_co_mutex_unlock(&s
->lock
);
1609 if (qcow2_need_accurate_refcounts(s
)) {
1610 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1612 qemu_co_mutex_unlock(&s
->lock
);
1616 qemu_co_mutex_unlock(&s
->lock
);
1621 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1623 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1626 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1628 BDRVQcowState
*s
= bs
->opaque
;
1629 bdi
->cluster_size
= s
->cluster_size
;
1630 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1635 static void dump_refcounts(BlockDriverState
*bs
)
1637 BDRVQcowState
*s
= bs
->opaque
;
1638 int64_t nb_clusters
, k
, k1
, size
;
1641 size
= bdrv_getlength(bs
->file
);
1642 nb_clusters
= size_to_clusters(s
, size
);
1643 for(k
= 0; k
< nb_clusters
;) {
1645 refcount
= get_refcount(bs
, k
);
1647 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1649 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1655 static int qcow2_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1656 int64_t pos
, int size
)
1658 BDRVQcowState
*s
= bs
->opaque
;
1659 int growable
= bs
->growable
;
1662 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1664 ret
= bdrv_pwrite(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1665 bs
->growable
= growable
;
1670 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1671 int64_t pos
, int size
)
1673 BDRVQcowState
*s
= bs
->opaque
;
1674 int growable
= bs
->growable
;
1677 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1679 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1680 bs
->growable
= growable
;
1685 static QEMUOptionParameter qcow2_create_options
[] = {
1687 .name
= BLOCK_OPT_SIZE
,
1689 .help
= "Virtual disk size"
1692 .name
= BLOCK_OPT_COMPAT_LEVEL
,
1694 .help
= "Compatibility level (0.10 or 1.1)"
1697 .name
= BLOCK_OPT_BACKING_FILE
,
1699 .help
= "File name of a base image"
1702 .name
= BLOCK_OPT_BACKING_FMT
,
1704 .help
= "Image format of the base image"
1707 .name
= BLOCK_OPT_ENCRYPT
,
1709 .help
= "Encrypt the image"
1712 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1714 .help
= "qcow2 cluster size",
1715 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
1718 .name
= BLOCK_OPT_PREALLOC
,
1720 .help
= "Preallocation mode (allowed values: off, metadata)"
1723 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
1725 .help
= "Postpone refcount updates",
1730 static BlockDriver bdrv_qcow2
= {
1731 .format_name
= "qcow2",
1732 .instance_size
= sizeof(BDRVQcowState
),
1733 .bdrv_probe
= qcow2_probe
,
1734 .bdrv_open
= qcow2_open
,
1735 .bdrv_close
= qcow2_close
,
1736 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
1737 .bdrv_create
= qcow2_create
,
1738 .bdrv_co_is_allocated
= qcow2_co_is_allocated
,
1739 .bdrv_set_key
= qcow2_set_key
,
1740 .bdrv_make_empty
= qcow2_make_empty
,
1742 .bdrv_co_readv
= qcow2_co_readv
,
1743 .bdrv_co_writev
= qcow2_co_writev
,
1744 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
1746 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
1747 .bdrv_co_discard
= qcow2_co_discard
,
1748 .bdrv_truncate
= qcow2_truncate
,
1749 .bdrv_write_compressed
= qcow2_write_compressed
,
1751 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1752 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1753 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1754 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1755 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1756 .bdrv_get_info
= qcow2_get_info
,
1758 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1759 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1761 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1763 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
1765 .create_options
= qcow2_create_options
,
1766 .bdrv_check
= qcow2_check
,
1769 static void bdrv_qcow2_init(void)
1771 bdrv_register(&bdrv_qcow2
);
1774 block_init(bdrv_qcow2_init
);