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"
35 Differences with QCOW:
37 - Support for multiple incremental snapshots.
38 - Memory management by reference counts.
39 - Clusters which have a reference count of one have the bit
40 QCOW_OFLAG_COPIED to optimize write performance.
41 - Size of compressed clusters is stored in sectors to reduce bit usage
42 in the cluster offsets.
43 - Support for storing additional data (such as the VM state) in the
45 - If a backing store is used, the cluster size is not constrained
46 (could be backported to QCOW).
47 - L2 tables have always a size of one cluster.
56 #define QCOW2_EXT_MAGIC_END 0
57 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
58 #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
60 static int qcow2_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
62 const QCowHeader
*cow_header
= (const void *)buf
;
64 if (buf_size
>= sizeof(QCowHeader
) &&
65 be32_to_cpu(cow_header
->magic
) == QCOW_MAGIC
&&
66 be32_to_cpu(cow_header
->version
) >= 2)
74 * read qcow2 extension and fill bs
75 * start reading from start_offset
76 * finish reading upon magic of value 0 or when end_offset reached
77 * unknown magic is skipped (future extension this version knows nothing about)
78 * return 0 upon success, non-0 otherwise
80 static int qcow2_read_extensions(BlockDriverState
*bs
, uint64_t start_offset
,
81 uint64_t end_offset
, void **p_feature_table
)
83 BDRVQcowState
*s
= bs
->opaque
;
89 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset
, end_offset
);
91 offset
= start_offset
;
92 while (offset
< end_offset
) {
96 if (offset
> s
->cluster_size
)
97 printf("qcow2_read_extension: suspicious offset %lu\n", offset
);
99 printf("attempting to read extended header in offset %lu\n", offset
);
102 if (bdrv_pread(bs
->file
, offset
, &ext
, sizeof(ext
)) != sizeof(ext
)) {
103 fprintf(stderr
, "qcow2_read_extension: ERROR: "
104 "pread fail from offset %" PRIu64
"\n",
108 be32_to_cpus(&ext
.magic
);
109 be32_to_cpus(&ext
.len
);
110 offset
+= sizeof(ext
);
112 printf("ext.magic = 0x%x\n", ext
.magic
);
114 if (ext
.len
> end_offset
- offset
) {
115 error_report("Header extension too large");
120 case QCOW2_EXT_MAGIC_END
:
123 case QCOW2_EXT_MAGIC_BACKING_FORMAT
:
124 if (ext
.len
>= sizeof(bs
->backing_format
)) {
125 fprintf(stderr
, "ERROR: ext_backing_format: len=%u too large"
127 ext
.len
, sizeof(bs
->backing_format
));
130 if (bdrv_pread(bs
->file
, offset
, bs
->backing_format
,
133 bs
->backing_format
[ext
.len
] = '\0';
135 printf("Qcow2: Got format extension %s\n", bs
->backing_format
);
139 case QCOW2_EXT_MAGIC_FEATURE_TABLE
:
140 if (p_feature_table
!= NULL
) {
141 void* feature_table
= g_malloc0(ext
.len
+ 2 * sizeof(Qcow2Feature
));
142 ret
= bdrv_pread(bs
->file
, offset
, feature_table
, ext
.len
);
147 *p_feature_table
= feature_table
;
152 /* unknown magic - save it in case we need to rewrite the header */
154 Qcow2UnknownHeaderExtension
*uext
;
156 uext
= g_malloc0(sizeof(*uext
) + ext
.len
);
157 uext
->magic
= ext
.magic
;
159 QLIST_INSERT_HEAD(&s
->unknown_header_ext
, uext
, next
);
161 ret
= bdrv_pread(bs
->file
, offset
, uext
->data
, uext
->len
);
169 offset
+= ((ext
.len
+ 7) & ~7);
175 static void cleanup_unknown_header_ext(BlockDriverState
*bs
)
177 BDRVQcowState
*s
= bs
->opaque
;
178 Qcow2UnknownHeaderExtension
*uext
, *next
;
180 QLIST_FOREACH_SAFE(uext
, &s
->unknown_header_ext
, next
, next
) {
181 QLIST_REMOVE(uext
, next
);
186 static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState
*bs
,
187 const char *fmt
, ...)
193 vsnprintf(msg
, sizeof(msg
), fmt
, ap
);
196 qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE
,
197 bs
->device_name
, "qcow2", msg
);
200 static void report_unsupported_feature(BlockDriverState
*bs
,
201 Qcow2Feature
*table
, uint64_t mask
)
203 while (table
&& table
->name
[0] != '\0') {
204 if (table
->type
== QCOW2_FEAT_TYPE_INCOMPATIBLE
) {
205 if (mask
& (1 << table
->bit
)) {
206 report_unsupported(bs
, "%.46s",table
->name
);
207 mask
&= ~(1 << table
->bit
);
214 report_unsupported(bs
, "Unknown incompatible feature: %" PRIx64
, mask
);
219 * Sets the dirty bit and flushes afterwards if necessary.
221 * The incompatible_features bit is only set if the image file header was
222 * updated successfully. Therefore it is not required to check the return
223 * value of this function.
225 int qcow2_mark_dirty(BlockDriverState
*bs
)
227 BDRVQcowState
*s
= bs
->opaque
;
231 assert(s
->qcow_version
>= 3);
233 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
234 return 0; /* already dirty */
237 val
= cpu_to_be64(s
->incompatible_features
| QCOW2_INCOMPAT_DIRTY
);
238 ret
= bdrv_pwrite(bs
->file
, offsetof(QCowHeader
, incompatible_features
),
243 ret
= bdrv_flush(bs
->file
);
248 /* Only treat image as dirty if the header was updated successfully */
249 s
->incompatible_features
|= QCOW2_INCOMPAT_DIRTY
;
254 * Clears the dirty bit and flushes before if necessary. Only call this
255 * function when there are no pending requests, it does not guard against
256 * concurrent requests dirtying the image.
258 static int qcow2_mark_clean(BlockDriverState
*bs
)
260 BDRVQcowState
*s
= bs
->opaque
;
262 if (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
) {
263 int ret
= bdrv_flush(bs
);
268 s
->incompatible_features
&= ~QCOW2_INCOMPAT_DIRTY
;
269 return qcow2_update_header(bs
);
274 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
277 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
282 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
283 return qcow2_mark_clean(bs
);
288 static QemuOptsList qcow2_runtime_opts
= {
290 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
293 .name
= "lazy_refcounts",
294 .type
= QEMU_OPT_BOOL
,
295 .help
= "Postpone refcount updates",
297 { /* end of list */ }
301 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
303 BDRVQcowState
*s
= bs
->opaque
;
307 Error
*local_err
= NULL
;
310 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
314 be32_to_cpus(&header
.magic
);
315 be32_to_cpus(&header
.version
);
316 be64_to_cpus(&header
.backing_file_offset
);
317 be32_to_cpus(&header
.backing_file_size
);
318 be64_to_cpus(&header
.size
);
319 be32_to_cpus(&header
.cluster_bits
);
320 be32_to_cpus(&header
.crypt_method
);
321 be64_to_cpus(&header
.l1_table_offset
);
322 be32_to_cpus(&header
.l1_size
);
323 be64_to_cpus(&header
.refcount_table_offset
);
324 be32_to_cpus(&header
.refcount_table_clusters
);
325 be64_to_cpus(&header
.snapshots_offset
);
326 be32_to_cpus(&header
.nb_snapshots
);
328 if (header
.magic
!= QCOW_MAGIC
) {
332 if (header
.version
< 2 || header
.version
> 3) {
333 report_unsupported(bs
, "QCOW version %d", header
.version
);
338 s
->qcow_version
= header
.version
;
340 /* Initialise version 3 header fields */
341 if (header
.version
== 2) {
342 header
.incompatible_features
= 0;
343 header
.compatible_features
= 0;
344 header
.autoclear_features
= 0;
345 header
.refcount_order
= 4;
346 header
.header_length
= 72;
348 be64_to_cpus(&header
.incompatible_features
);
349 be64_to_cpus(&header
.compatible_features
);
350 be64_to_cpus(&header
.autoclear_features
);
351 be32_to_cpus(&header
.refcount_order
);
352 be32_to_cpus(&header
.header_length
);
355 if (header
.header_length
> sizeof(header
)) {
356 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
357 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
358 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
359 s
->unknown_header_fields_size
);
365 if (header
.backing_file_offset
) {
366 ext_end
= header
.backing_file_offset
;
368 ext_end
= 1 << header
.cluster_bits
;
371 /* Handle feature bits */
372 s
->incompatible_features
= header
.incompatible_features
;
373 s
->compatible_features
= header
.compatible_features
;
374 s
->autoclear_features
= header
.autoclear_features
;
376 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
377 void *feature_table
= NULL
;
378 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
380 report_unsupported_feature(bs
, feature_table
,
381 s
->incompatible_features
&
382 ~QCOW2_INCOMPAT_MASK
);
387 /* Check support for various header values */
388 if (header
.refcount_order
!= 4) {
389 report_unsupported(bs
, "%d bit reference counts",
390 1 << header
.refcount_order
);
395 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
396 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
400 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
404 s
->crypt_method_header
= header
.crypt_method
;
405 if (s
->crypt_method_header
) {
408 s
->cluster_bits
= header
.cluster_bits
;
409 s
->cluster_size
= 1 << s
->cluster_bits
;
410 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
411 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
412 s
->l2_size
= 1 << s
->l2_bits
;
413 bs
->total_sectors
= header
.size
/ 512;
414 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
415 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
416 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
417 s
->refcount_table_offset
= header
.refcount_table_offset
;
418 s
->refcount_table_size
=
419 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
421 s
->snapshots_offset
= header
.snapshots_offset
;
422 s
->nb_snapshots
= header
.nb_snapshots
;
424 /* read the level 1 table */
425 s
->l1_size
= header
.l1_size
;
426 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
427 /* the L1 table must contain at least enough entries to put
429 if (s
->l1_size
< s
->l1_vm_state_index
) {
433 s
->l1_table_offset
= header
.l1_table_offset
;
434 if (s
->l1_size
> 0) {
435 s
->l1_table
= g_malloc0(
436 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
437 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
438 s
->l1_size
* sizeof(uint64_t));
442 for(i
= 0;i
< s
->l1_size
; i
++) {
443 be64_to_cpus(&s
->l1_table
[i
]);
447 /* alloc L2 table/refcount block cache */
448 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
449 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
451 s
->cluster_cache
= g_malloc(s
->cluster_size
);
452 /* one more sector for decompressed data alignment */
453 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
455 s
->cluster_cache_offset
= -1;
458 ret
= qcow2_refcount_init(bs
);
463 QLIST_INIT(&s
->cluster_allocs
);
465 /* read qcow2 extensions */
466 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
)) {
471 /* read the backing file name */
472 if (header
.backing_file_offset
!= 0) {
473 len
= header
.backing_file_size
;
477 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
478 bs
->backing_file
, len
);
482 bs
->backing_file
[len
] = '\0';
485 ret
= qcow2_read_snapshots(bs
);
490 /* Clear unknown autoclear feature bits */
491 if (!bs
->read_only
&& s
->autoclear_features
!= 0) {
492 s
->autoclear_features
= 0;
493 ret
= qcow2_update_header(bs
);
499 /* Initialise locks */
500 qemu_co_mutex_init(&s
->lock
);
502 /* Repair image if dirty */
503 if (!(flags
& BDRV_O_CHECK
) && !bs
->read_only
&&
504 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
505 BdrvCheckResult result
= {0};
507 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
513 /* Enable lazy_refcounts according to image and command line options */
514 opts
= qemu_opts_create_nofail(&qcow2_runtime_opts
);
515 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
516 if (error_is_set(&local_err
)) {
517 qerror_report_err(local_err
);
518 error_free(local_err
);
523 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, "lazy_refcounts",
524 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
528 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
529 qerror_report(ERROR_CLASS_GENERIC_ERROR
, "Lazy refcounts require "
530 "a qcow2 image with at least qemu 1.1 compatibility level");
537 BdrvCheckResult result
= {0};
538 qcow2_check_refcounts(bs
, &result
, 0);
544 g_free(s
->unknown_header_fields
);
545 cleanup_unknown_header_ext(bs
);
546 qcow2_free_snapshots(bs
);
547 qcow2_refcount_close(bs
);
549 if (s
->l2_table_cache
) {
550 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
552 g_free(s
->cluster_cache
);
553 qemu_vfree(s
->cluster_data
);
557 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
559 BDRVQcowState
*s
= bs
->opaque
;
563 memset(keybuf
, 0, 16);
567 /* XXX: we could compress the chars to 7 bits to increase
569 for(i
= 0;i
< len
;i
++) {
572 s
->crypt_method
= s
->crypt_method_header
;
574 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
576 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
586 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
587 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
588 for(i
= 0; i
< 16; i
++)
589 printf(" %02x", tmp
[i
]);
591 for(i
= 0; i
< 16; i
++)
592 printf(" %02x", out
[i
]);
599 /* We have nothing to do for QCOW2 reopen, stubs just return
601 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
602 BlockReopenQueue
*queue
, Error
**errp
)
607 static int coroutine_fn
qcow2_co_is_allocated(BlockDriverState
*bs
,
608 int64_t sector_num
, int nb_sectors
, int *pnum
)
610 BDRVQcowState
*s
= bs
->opaque
;
611 uint64_t cluster_offset
;
615 /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
616 * can't pass them on today */
617 qemu_co_mutex_lock(&s
->lock
);
618 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
619 qemu_co_mutex_unlock(&s
->lock
);
624 return (cluster_offset
!= 0) || (ret
== QCOW2_CLUSTER_ZERO
);
627 /* handle reading after the end of the backing file */
628 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
629 int64_t sector_num
, int nb_sectors
)
632 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
634 if (sector_num
>= bs
->total_sectors
)
637 n1
= bs
->total_sectors
- sector_num
;
639 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
644 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
645 int remaining_sectors
, QEMUIOVector
*qiov
)
647 BDRVQcowState
*s
= bs
->opaque
;
648 int index_in_cluster
, n1
;
650 int cur_nr_sectors
; /* number of sectors in current iteration */
651 uint64_t cluster_offset
= 0;
652 uint64_t bytes_done
= 0;
653 QEMUIOVector hd_qiov
;
654 uint8_t *cluster_data
= NULL
;
656 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
658 qemu_co_mutex_lock(&s
->lock
);
660 while (remaining_sectors
!= 0) {
662 /* prepare next request */
663 cur_nr_sectors
= remaining_sectors
;
664 if (s
->crypt_method
) {
665 cur_nr_sectors
= MIN(cur_nr_sectors
,
666 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
669 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
670 &cur_nr_sectors
, &cluster_offset
);
675 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
677 qemu_iovec_reset(&hd_qiov
);
678 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
679 cur_nr_sectors
* 512);
682 case QCOW2_CLUSTER_UNALLOCATED
:
684 if (bs
->backing_hd
) {
685 /* read from the base image */
686 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
687 sector_num
, cur_nr_sectors
);
689 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
690 qemu_co_mutex_unlock(&s
->lock
);
691 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
693 qemu_co_mutex_lock(&s
->lock
);
699 /* Note: in this case, no need to wait */
700 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
704 case QCOW2_CLUSTER_ZERO
:
705 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
708 case QCOW2_CLUSTER_COMPRESSED
:
709 /* add AIO support for compressed blocks ? */
710 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
715 qemu_iovec_from_buf(&hd_qiov
, 0,
716 s
->cluster_cache
+ index_in_cluster
* 512,
717 512 * cur_nr_sectors
);
720 case QCOW2_CLUSTER_NORMAL
:
721 if ((cluster_offset
& 511) != 0) {
726 if (s
->crypt_method
) {
728 * For encrypted images, read everything into a temporary
729 * contiguous buffer on which the AES functions can work.
733 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
736 assert(cur_nr_sectors
<=
737 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
738 qemu_iovec_reset(&hd_qiov
);
739 qemu_iovec_add(&hd_qiov
, cluster_data
,
740 512 * cur_nr_sectors
);
743 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
744 qemu_co_mutex_unlock(&s
->lock
);
745 ret
= bdrv_co_readv(bs
->file
,
746 (cluster_offset
>> 9) + index_in_cluster
,
747 cur_nr_sectors
, &hd_qiov
);
748 qemu_co_mutex_lock(&s
->lock
);
752 if (s
->crypt_method
) {
753 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
754 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
755 qemu_iovec_from_buf(qiov
, bytes_done
,
756 cluster_data
, 512 * cur_nr_sectors
);
761 g_assert_not_reached();
766 remaining_sectors
-= cur_nr_sectors
;
767 sector_num
+= cur_nr_sectors
;
768 bytes_done
+= cur_nr_sectors
* 512;
773 qemu_co_mutex_unlock(&s
->lock
);
775 qemu_iovec_destroy(&hd_qiov
);
776 qemu_vfree(cluster_data
);
781 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
783 int remaining_sectors
,
786 BDRVQcowState
*s
= bs
->opaque
;
787 int index_in_cluster
;
790 int cur_nr_sectors
; /* number of sectors in current iteration */
791 uint64_t cluster_offset
;
792 QEMUIOVector hd_qiov
;
793 uint64_t bytes_done
= 0;
794 uint8_t *cluster_data
= NULL
;
795 QCowL2Meta
*l2meta
= NULL
;
797 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
800 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
802 s
->cluster_cache_offset
= -1; /* disable compressed cache */
804 qemu_co_mutex_lock(&s
->lock
);
806 while (remaining_sectors
!= 0) {
810 trace_qcow2_writev_start_part(qemu_coroutine_self());
811 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
812 n_end
= index_in_cluster
+ remaining_sectors
;
813 if (s
->crypt_method
&&
814 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
) {
815 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
818 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
819 index_in_cluster
, n_end
, &cur_nr_sectors
, &cluster_offset
, &l2meta
);
824 assert((cluster_offset
& 511) == 0);
826 qemu_iovec_reset(&hd_qiov
);
827 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
828 cur_nr_sectors
* 512);
830 if (s
->crypt_method
) {
832 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
836 assert(hd_qiov
.size
<=
837 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
838 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
840 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
841 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
843 qemu_iovec_reset(&hd_qiov
);
844 qemu_iovec_add(&hd_qiov
, cluster_data
,
845 cur_nr_sectors
* 512);
848 qemu_co_mutex_unlock(&s
->lock
);
849 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
850 trace_qcow2_writev_data(qemu_coroutine_self(),
851 (cluster_offset
>> 9) + index_in_cluster
);
852 ret
= bdrv_co_writev(bs
->file
,
853 (cluster_offset
>> 9) + index_in_cluster
,
854 cur_nr_sectors
, &hd_qiov
);
855 qemu_co_mutex_lock(&s
->lock
);
860 if (l2meta
!= NULL
) {
861 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
866 /* Take the request off the list of running requests */
867 if (l2meta
->nb_clusters
!= 0) {
868 QLIST_REMOVE(l2meta
, next_in_flight
);
871 qemu_co_mutex_unlock(&s
->lock
);
872 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
873 qemu_co_mutex_lock(&s
->lock
);
879 remaining_sectors
-= cur_nr_sectors
;
880 sector_num
+= cur_nr_sectors
;
881 bytes_done
+= cur_nr_sectors
* 512;
882 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
887 qemu_co_mutex_unlock(&s
->lock
);
889 if (l2meta
!= NULL
) {
890 if (l2meta
->nb_clusters
!= 0) {
891 QLIST_REMOVE(l2meta
, next_in_flight
);
893 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
897 qemu_iovec_destroy(&hd_qiov
);
898 qemu_vfree(cluster_data
);
899 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
904 static void qcow2_close(BlockDriverState
*bs
)
906 BDRVQcowState
*s
= bs
->opaque
;
909 qcow2_cache_flush(bs
, s
->l2_table_cache
);
910 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
912 qcow2_mark_clean(bs
);
914 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
915 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
917 g_free(s
->unknown_header_fields
);
918 cleanup_unknown_header_ext(bs
);
920 g_free(s
->cluster_cache
);
921 qemu_vfree(s
->cluster_data
);
922 qcow2_refcount_close(bs
);
923 qcow2_free_snapshots(bs
);
926 static void qcow2_invalidate_cache(BlockDriverState
*bs
)
928 BDRVQcowState
*s
= bs
->opaque
;
929 int flags
= s
->flags
;
930 AES_KEY aes_encrypt_key
;
931 AES_KEY aes_decrypt_key
;
932 uint32_t crypt_method
= 0;
935 * Backing files are read-only which makes all of their metadata immutable,
936 * that means we don't have to worry about reopening them here.
939 if (s
->crypt_method
) {
940 crypt_method
= s
->crypt_method
;
941 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
942 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
947 memset(s
, 0, sizeof(BDRVQcowState
));
948 qcow2_open(bs
, NULL
, flags
);
951 s
->crypt_method
= crypt_method
;
952 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
953 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
957 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
958 size_t len
, size_t buflen
)
960 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
961 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
963 if (buflen
< ext_len
) {
967 *ext_backing_fmt
= (QCowExtension
) {
968 .magic
= cpu_to_be32(magic
),
969 .len
= cpu_to_be32(len
),
971 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
977 * Updates the qcow2 header, including the variable length parts of it, i.e.
978 * the backing file name and all extensions. qcow2 was not designed to allow
979 * such changes, so if we run out of space (we can only use the first cluster)
980 * this function may fail.
982 * Returns 0 on success, -errno in error cases.
984 int qcow2_update_header(BlockDriverState
*bs
)
986 BDRVQcowState
*s
= bs
->opaque
;
989 size_t buflen
= s
->cluster_size
;
992 uint32_t refcount_table_clusters
;
993 size_t header_length
;
994 Qcow2UnknownHeaderExtension
*uext
;
996 buf
= qemu_blockalign(bs
, buflen
);
998 /* Header structure */
999 header
= (QCowHeader
*) buf
;
1001 if (buflen
< sizeof(*header
)) {
1006 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1007 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1008 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1010 *header
= (QCowHeader
) {
1011 /* Version 2 fields */
1012 .magic
= cpu_to_be32(QCOW_MAGIC
),
1013 .version
= cpu_to_be32(s
->qcow_version
),
1014 .backing_file_offset
= 0,
1015 .backing_file_size
= 0,
1016 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1017 .size
= cpu_to_be64(total_size
),
1018 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1019 .l1_size
= cpu_to_be32(s
->l1_size
),
1020 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1021 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1022 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1023 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1024 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1026 /* Version 3 fields */
1027 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1028 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1029 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1030 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1031 .header_length
= cpu_to_be32(header_length
),
1034 /* For older versions, write a shorter header */
1035 switch (s
->qcow_version
) {
1037 ret
= offsetof(QCowHeader
, incompatible_features
);
1040 ret
= sizeof(*header
);
1049 memset(buf
, 0, buflen
);
1051 /* Preserve any unknown field in the header */
1052 if (s
->unknown_header_fields_size
) {
1053 if (buflen
< s
->unknown_header_fields_size
) {
1058 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1059 buf
+= s
->unknown_header_fields_size
;
1060 buflen
-= s
->unknown_header_fields_size
;
1063 /* Backing file format header extension */
1064 if (*bs
->backing_format
) {
1065 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1066 bs
->backing_format
, strlen(bs
->backing_format
),
1077 Qcow2Feature features
[] = {
1079 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1080 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1081 .name
= "dirty bit",
1084 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1085 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1086 .name
= "lazy refcounts",
1090 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1091 features
, sizeof(features
), buflen
);
1098 /* Keep unknown header extensions */
1099 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1100 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1109 /* End of header extensions */
1110 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1118 /* Backing file name */
1119 if (*bs
->backing_file
) {
1120 size_t backing_file_len
= strlen(bs
->backing_file
);
1122 if (buflen
< backing_file_len
) {
1127 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1128 strncpy(buf
, bs
->backing_file
, buflen
);
1130 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1131 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1134 /* Write the new header */
1135 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1146 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1147 const char *backing_file
, const char *backing_fmt
)
1149 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1150 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1152 return qcow2_update_header(bs
);
1155 static int preallocate(BlockDriverState
*bs
)
1157 uint64_t nb_sectors
;
1159 uint64_t host_offset
= 0;
1164 nb_sectors
= bdrv_getlength(bs
) >> 9;
1167 while (nb_sectors
) {
1168 num
= MIN(nb_sectors
, INT_MAX
>> 9);
1169 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
,
1170 &host_offset
, &meta
);
1175 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1177 qcow2_free_any_clusters(bs
, meta
->alloc_offset
, meta
->nb_clusters
);
1181 /* There are no dependent requests, but we need to remove our request
1182 * from the list of in-flight requests */
1184 QLIST_REMOVE(meta
, next_in_flight
);
1187 /* TODO Preallocate data if requested */
1194 * It is expected that the image file is large enough to actually contain
1195 * all of the allocated clusters (otherwise we get failing reads after
1196 * EOF). Extend the image to the last allocated sector.
1198 if (host_offset
!= 0) {
1200 memset(buf
, 0, 512);
1201 ret
= bdrv_write(bs
->file
, (host_offset
>> 9) + num
- 1, buf
, 1);
1210 static int qcow2_create2(const char *filename
, int64_t total_size
,
1211 const char *backing_file
, const char *backing_format
,
1212 int flags
, size_t cluster_size
, int prealloc
,
1213 QEMUOptionParameter
*options
, int version
)
1215 /* Calculate cluster_bits */
1217 cluster_bits
= ffs(cluster_size
) - 1;
1218 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1219 (1 << cluster_bits
) != cluster_size
)
1222 "Cluster size must be a power of two between %d and %dk",
1223 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1228 * Open the image file and write a minimal qcow2 header.
1230 * We keep things simple and start with a zero-sized image. We also
1231 * do without refcount blocks or a L1 table for now. We'll fix the
1232 * inconsistency later.
1234 * We do need a refcount table because growing the refcount table means
1235 * allocating two new refcount blocks - the seconds of which would be at
1236 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1237 * size for any qcow2 image.
1239 BlockDriverState
* bs
;
1241 uint8_t* refcount_table
;
1244 ret
= bdrv_create_file(filename
, options
);
1249 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1254 /* Write the header */
1255 memset(&header
, 0, sizeof(header
));
1256 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1257 header
.version
= cpu_to_be32(version
);
1258 header
.cluster_bits
= cpu_to_be32(cluster_bits
);
1259 header
.size
= cpu_to_be64(0);
1260 header
.l1_table_offset
= cpu_to_be64(0);
1261 header
.l1_size
= cpu_to_be32(0);
1262 header
.refcount_table_offset
= cpu_to_be64(cluster_size
);
1263 header
.refcount_table_clusters
= cpu_to_be32(1);
1264 header
.refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
);
1265 header
.header_length
= cpu_to_be32(sizeof(header
));
1267 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1268 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1270 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1273 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1274 header
.compatible_features
|=
1275 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1278 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
1283 /* Write an empty refcount table */
1284 refcount_table
= g_malloc0(cluster_size
);
1285 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1286 g_free(refcount_table
);
1295 * And now open the image and make it consistent first (i.e. increase the
1296 * refcount of the cluster that is occupied by the header and the refcount
1299 BlockDriver
* drv
= bdrv_find_format("qcow2");
1300 assert(drv
!= NULL
);
1301 ret
= bdrv_open(bs
, filename
, NULL
,
1302 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
);
1307 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1311 } else if (ret
!= 0) {
1312 error_report("Huh, first cluster in empty image is already in use?");
1316 /* Okay, now that we have a valid image, let's give it the right size */
1317 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1322 /* Want a backing file? There you go.*/
1324 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1330 /* And if we're supposed to preallocate metadata, do that now */
1332 BDRVQcowState
*s
= bs
->opaque
;
1333 qemu_co_mutex_lock(&s
->lock
);
1334 ret
= preallocate(bs
);
1335 qemu_co_mutex_unlock(&s
->lock
);
1347 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
1349 const char *backing_file
= NULL
;
1350 const char *backing_fmt
= NULL
;
1351 uint64_t sectors
= 0;
1353 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1357 /* Read out options */
1358 while (options
&& options
->name
) {
1359 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1360 sectors
= options
->value
.n
/ 512;
1361 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1362 backing_file
= options
->value
.s
;
1363 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1364 backing_fmt
= options
->value
.s
;
1365 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1366 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1367 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1368 if (options
->value
.n
) {
1369 cluster_size
= options
->value
.n
;
1371 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1372 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1374 } else if (!strcmp(options
->value
.s
, "metadata")) {
1377 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1381 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1382 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "0.10")) {
1384 } else if (!strcmp(options
->value
.s
, "1.1")) {
1387 fprintf(stderr
, "Invalid compatibility level: '%s'\n",
1391 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1392 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1397 if (backing_file
&& prealloc
) {
1398 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1403 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1404 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
1405 "level 1.1 and above (use compat=1.1 or greater)\n");
1409 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1410 cluster_size
, prealloc
, options
, version
);
1413 static int qcow2_make_empty(BlockDriverState
*bs
)
1416 /* XXX: not correct */
1417 BDRVQcowState
*s
= bs
->opaque
;
1418 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1421 memset(s
->l1_table
, 0, l1_length
);
1422 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1424 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1433 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1434 int64_t sector_num
, int nb_sectors
)
1437 BDRVQcowState
*s
= bs
->opaque
;
1439 /* Emulate misaligned zero writes */
1440 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1444 /* Whatever is left can use real zero clusters */
1445 qemu_co_mutex_lock(&s
->lock
);
1446 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1448 qemu_co_mutex_unlock(&s
->lock
);
1453 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1454 int64_t sector_num
, int nb_sectors
)
1457 BDRVQcowState
*s
= bs
->opaque
;
1459 qemu_co_mutex_lock(&s
->lock
);
1460 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1462 qemu_co_mutex_unlock(&s
->lock
);
1466 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1468 BDRVQcowState
*s
= bs
->opaque
;
1469 int ret
, new_l1_size
;
1472 error_report("The new size must be a multiple of 512");
1476 /* cannot proceed if image has snapshots */
1477 if (s
->nb_snapshots
) {
1478 error_report("Can't resize an image which has snapshots");
1482 /* shrinking is currently not supported */
1483 if (offset
< bs
->total_sectors
* 512) {
1484 error_report("qcow2 doesn't support shrinking images yet");
1488 new_l1_size
= size_to_l1(s
, offset
);
1489 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1494 /* write updated header.size */
1495 offset
= cpu_to_be64(offset
);
1496 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1497 &offset
, sizeof(uint64_t));
1502 s
->l1_vm_state_index
= new_l1_size
;
1506 /* XXX: put compressed sectors first, then all the cluster aligned
1507 tables to avoid losing bytes in alignment */
1508 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1509 const uint8_t *buf
, int nb_sectors
)
1511 BDRVQcowState
*s
= bs
->opaque
;
1515 uint64_t cluster_offset
;
1517 if (nb_sectors
== 0) {
1518 /* align end of file to a sector boundary to ease reading with
1519 sector based I/Os */
1520 cluster_offset
= bdrv_getlength(bs
->file
);
1521 cluster_offset
= (cluster_offset
+ 511) & ~511;
1522 bdrv_truncate(bs
->file
, cluster_offset
);
1526 if (nb_sectors
!= s
->cluster_sectors
)
1529 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1531 /* best compression, small window, no zlib header */
1532 memset(&strm
, 0, sizeof(strm
));
1533 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1535 9, Z_DEFAULT_STRATEGY
);
1541 strm
.avail_in
= s
->cluster_size
;
1542 strm
.next_in
= (uint8_t *)buf
;
1543 strm
.avail_out
= s
->cluster_size
;
1544 strm
.next_out
= out_buf
;
1546 ret
= deflate(&strm
, Z_FINISH
);
1547 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1552 out_len
= strm
.next_out
- out_buf
;
1556 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1557 /* could not compress: write normal cluster */
1558 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1563 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1564 sector_num
<< 9, out_len
);
1565 if (!cluster_offset
) {
1569 cluster_offset
&= s
->cluster_offset_mask
;
1570 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1571 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1583 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1585 BDRVQcowState
*s
= bs
->opaque
;
1588 qemu_co_mutex_lock(&s
->lock
);
1589 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1591 qemu_co_mutex_unlock(&s
->lock
);
1595 if (qcow2_need_accurate_refcounts(s
)) {
1596 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1598 qemu_co_mutex_unlock(&s
->lock
);
1602 qemu_co_mutex_unlock(&s
->lock
);
1607 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1609 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1612 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1614 BDRVQcowState
*s
= bs
->opaque
;
1615 bdi
->cluster_size
= s
->cluster_size
;
1616 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1621 static void dump_refcounts(BlockDriverState
*bs
)
1623 BDRVQcowState
*s
= bs
->opaque
;
1624 int64_t nb_clusters
, k
, k1
, size
;
1627 size
= bdrv_getlength(bs
->file
);
1628 nb_clusters
= size_to_clusters(s
, size
);
1629 for(k
= 0; k
< nb_clusters
;) {
1631 refcount
= get_refcount(bs
, k
);
1633 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1635 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1641 static int qcow2_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1642 int64_t pos
, int size
)
1644 BDRVQcowState
*s
= bs
->opaque
;
1645 int growable
= bs
->growable
;
1648 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1650 ret
= bdrv_pwrite(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1651 bs
->growable
= growable
;
1656 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1657 int64_t pos
, int size
)
1659 BDRVQcowState
*s
= bs
->opaque
;
1660 int growable
= bs
->growable
;
1663 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1665 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1666 bs
->growable
= growable
;
1671 static QEMUOptionParameter qcow2_create_options
[] = {
1673 .name
= BLOCK_OPT_SIZE
,
1675 .help
= "Virtual disk size"
1678 .name
= BLOCK_OPT_COMPAT_LEVEL
,
1680 .help
= "Compatibility level (0.10 or 1.1)"
1683 .name
= BLOCK_OPT_BACKING_FILE
,
1685 .help
= "File name of a base image"
1688 .name
= BLOCK_OPT_BACKING_FMT
,
1690 .help
= "Image format of the base image"
1693 .name
= BLOCK_OPT_ENCRYPT
,
1695 .help
= "Encrypt the image"
1698 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1700 .help
= "qcow2 cluster size",
1701 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
1704 .name
= BLOCK_OPT_PREALLOC
,
1706 .help
= "Preallocation mode (allowed values: off, metadata)"
1709 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
1711 .help
= "Postpone refcount updates",
1716 static BlockDriver bdrv_qcow2
= {
1717 .format_name
= "qcow2",
1718 .instance_size
= sizeof(BDRVQcowState
),
1719 .bdrv_probe
= qcow2_probe
,
1720 .bdrv_open
= qcow2_open
,
1721 .bdrv_close
= qcow2_close
,
1722 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
1723 .bdrv_create
= qcow2_create
,
1724 .bdrv_co_is_allocated
= qcow2_co_is_allocated
,
1725 .bdrv_set_key
= qcow2_set_key
,
1726 .bdrv_make_empty
= qcow2_make_empty
,
1728 .bdrv_co_readv
= qcow2_co_readv
,
1729 .bdrv_co_writev
= qcow2_co_writev
,
1730 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
1732 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
1733 .bdrv_co_discard
= qcow2_co_discard
,
1734 .bdrv_truncate
= qcow2_truncate
,
1735 .bdrv_write_compressed
= qcow2_write_compressed
,
1737 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1738 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1739 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1740 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1741 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1742 .bdrv_get_info
= qcow2_get_info
,
1744 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1745 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1747 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1749 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
1751 .create_options
= qcow2_create_options
,
1752 .bdrv_check
= qcow2_check
,
1755 static void bdrv_qcow2_init(void)
1757 bdrv_register(&bdrv_qcow2
);
1760 block_init(bdrv_qcow2_init
);