2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "block_int.h"
29 #include "block/qcow2.h"
30 #include "qemu-error.h"
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 static 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 int qcow2_open(BlockDriverState
*bs
, int flags
)
290 BDRVQcowState
*s
= bs
->opaque
;
295 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
299 be32_to_cpus(&header
.magic
);
300 be32_to_cpus(&header
.version
);
301 be64_to_cpus(&header
.backing_file_offset
);
302 be32_to_cpus(&header
.backing_file_size
);
303 be64_to_cpus(&header
.size
);
304 be32_to_cpus(&header
.cluster_bits
);
305 be32_to_cpus(&header
.crypt_method
);
306 be64_to_cpus(&header
.l1_table_offset
);
307 be32_to_cpus(&header
.l1_size
);
308 be64_to_cpus(&header
.refcount_table_offset
);
309 be32_to_cpus(&header
.refcount_table_clusters
);
310 be64_to_cpus(&header
.snapshots_offset
);
311 be32_to_cpus(&header
.nb_snapshots
);
313 if (header
.magic
!= QCOW_MAGIC
) {
317 if (header
.version
< 2 || header
.version
> 3) {
318 report_unsupported(bs
, "QCOW version %d", header
.version
);
323 s
->qcow_version
= header
.version
;
325 /* Initialise version 3 header fields */
326 if (header
.version
== 2) {
327 header
.incompatible_features
= 0;
328 header
.compatible_features
= 0;
329 header
.autoclear_features
= 0;
330 header
.refcount_order
= 4;
331 header
.header_length
= 72;
333 be64_to_cpus(&header
.incompatible_features
);
334 be64_to_cpus(&header
.compatible_features
);
335 be64_to_cpus(&header
.autoclear_features
);
336 be32_to_cpus(&header
.refcount_order
);
337 be32_to_cpus(&header
.header_length
);
340 if (header
.header_length
> sizeof(header
)) {
341 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
342 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
343 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
344 s
->unknown_header_fields_size
);
350 if (header
.backing_file_offset
) {
351 ext_end
= header
.backing_file_offset
;
353 ext_end
= 1 << header
.cluster_bits
;
356 /* Handle feature bits */
357 s
->incompatible_features
= header
.incompatible_features
;
358 s
->compatible_features
= header
.compatible_features
;
359 s
->autoclear_features
= header
.autoclear_features
;
361 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
362 void *feature_table
= NULL
;
363 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
365 report_unsupported_feature(bs
, feature_table
,
366 s
->incompatible_features
&
367 ~QCOW2_INCOMPAT_MASK
);
372 /* Check support for various header values */
373 if (header
.refcount_order
!= 4) {
374 report_unsupported(bs
, "%d bit reference counts",
375 1 << header
.refcount_order
);
380 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
381 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
385 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
389 s
->crypt_method_header
= header
.crypt_method
;
390 if (s
->crypt_method_header
) {
393 s
->cluster_bits
= header
.cluster_bits
;
394 s
->cluster_size
= 1 << s
->cluster_bits
;
395 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
396 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
397 s
->l2_size
= 1 << s
->l2_bits
;
398 bs
->total_sectors
= header
.size
/ 512;
399 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
400 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
401 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
402 s
->refcount_table_offset
= header
.refcount_table_offset
;
403 s
->refcount_table_size
=
404 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
406 s
->snapshots_offset
= header
.snapshots_offset
;
407 s
->nb_snapshots
= header
.nb_snapshots
;
409 /* read the level 1 table */
410 s
->l1_size
= header
.l1_size
;
411 s
->l1_vm_state_index
= size_to_l1(s
, header
.size
);
412 /* the L1 table must contain at least enough entries to put
414 if (s
->l1_size
< s
->l1_vm_state_index
) {
418 s
->l1_table_offset
= header
.l1_table_offset
;
419 if (s
->l1_size
> 0) {
420 s
->l1_table
= g_malloc0(
421 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
422 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
423 s
->l1_size
* sizeof(uint64_t));
427 for(i
= 0;i
< s
->l1_size
; i
++) {
428 be64_to_cpus(&s
->l1_table
[i
]);
432 /* alloc L2 table/refcount block cache */
433 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
434 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
436 s
->cluster_cache
= g_malloc(s
->cluster_size
);
437 /* one more sector for decompressed data alignment */
438 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
440 s
->cluster_cache_offset
= -1;
443 ret
= qcow2_refcount_init(bs
);
448 QLIST_INIT(&s
->cluster_allocs
);
450 /* read qcow2 extensions */
451 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
)) {
456 /* read the backing file name */
457 if (header
.backing_file_offset
!= 0) {
458 len
= header
.backing_file_size
;
462 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
463 bs
->backing_file
, len
);
467 bs
->backing_file
[len
] = '\0';
470 ret
= qcow2_read_snapshots(bs
);
475 /* Clear unknown autoclear feature bits */
476 if (!bs
->read_only
&& s
->autoclear_features
!= 0) {
477 s
->autoclear_features
= 0;
478 ret
= qcow2_update_header(bs
);
484 /* Initialise locks */
485 qemu_co_mutex_init(&s
->lock
);
487 /* Repair image if dirty */
488 if (!(flags
& BDRV_O_CHECK
) && !bs
->read_only
&&
489 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
490 BdrvCheckResult result
= {0};
492 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
500 BdrvCheckResult result
= {0};
501 qcow2_check_refcounts(bs
, &result
, 0);
507 g_free(s
->unknown_header_fields
);
508 cleanup_unknown_header_ext(bs
);
509 qcow2_free_snapshots(bs
);
510 qcow2_refcount_close(bs
);
512 if (s
->l2_table_cache
) {
513 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
515 g_free(s
->cluster_cache
);
516 qemu_vfree(s
->cluster_data
);
520 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
522 BDRVQcowState
*s
= bs
->opaque
;
526 memset(keybuf
, 0, 16);
530 /* XXX: we could compress the chars to 7 bits to increase
532 for(i
= 0;i
< len
;i
++) {
535 s
->crypt_method
= s
->crypt_method_header
;
537 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
539 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
549 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
550 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
551 for(i
= 0; i
< 16; i
++)
552 printf(" %02x", tmp
[i
]);
554 for(i
= 0; i
< 16; i
++)
555 printf(" %02x", out
[i
]);
562 /* We have nothing to do for QCOW2 reopen, stubs just return
564 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
565 BlockReopenQueue
*queue
, Error
**errp
)
570 static int coroutine_fn
qcow2_co_is_allocated(BlockDriverState
*bs
,
571 int64_t sector_num
, int nb_sectors
, int *pnum
)
573 BDRVQcowState
*s
= bs
->opaque
;
574 uint64_t cluster_offset
;
578 /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
579 * can't pass them on today */
580 qemu_co_mutex_lock(&s
->lock
);
581 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
582 qemu_co_mutex_unlock(&s
->lock
);
587 return (cluster_offset
!= 0);
590 /* handle reading after the end of the backing file */
591 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
592 int64_t sector_num
, int nb_sectors
)
595 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
597 if (sector_num
>= bs
->total_sectors
)
600 n1
= bs
->total_sectors
- sector_num
;
602 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
607 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
608 int remaining_sectors
, QEMUIOVector
*qiov
)
610 BDRVQcowState
*s
= bs
->opaque
;
611 int index_in_cluster
, n1
;
613 int cur_nr_sectors
; /* number of sectors in current iteration */
614 uint64_t cluster_offset
= 0;
615 uint64_t bytes_done
= 0;
616 QEMUIOVector hd_qiov
;
617 uint8_t *cluster_data
= NULL
;
619 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
621 qemu_co_mutex_lock(&s
->lock
);
623 while (remaining_sectors
!= 0) {
625 /* prepare next request */
626 cur_nr_sectors
= remaining_sectors
;
627 if (s
->crypt_method
) {
628 cur_nr_sectors
= MIN(cur_nr_sectors
,
629 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
632 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
633 &cur_nr_sectors
, &cluster_offset
);
638 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
640 qemu_iovec_reset(&hd_qiov
);
641 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
642 cur_nr_sectors
* 512);
645 case QCOW2_CLUSTER_UNALLOCATED
:
647 if (bs
->backing_hd
) {
648 /* read from the base image */
649 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
650 sector_num
, cur_nr_sectors
);
652 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
653 qemu_co_mutex_unlock(&s
->lock
);
654 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
656 qemu_co_mutex_lock(&s
->lock
);
662 /* Note: in this case, no need to wait */
663 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
667 case QCOW2_CLUSTER_ZERO
:
668 if (s
->qcow_version
< 3) {
672 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
675 case QCOW2_CLUSTER_COMPRESSED
:
676 /* add AIO support for compressed blocks ? */
677 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
682 qemu_iovec_from_buf(&hd_qiov
, 0,
683 s
->cluster_cache
+ index_in_cluster
* 512,
684 512 * cur_nr_sectors
);
687 case QCOW2_CLUSTER_NORMAL
:
688 if ((cluster_offset
& 511) != 0) {
693 if (s
->crypt_method
) {
695 * For encrypted images, read everything into a temporary
696 * contiguous buffer on which the AES functions can work.
700 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
703 assert(cur_nr_sectors
<=
704 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
705 qemu_iovec_reset(&hd_qiov
);
706 qemu_iovec_add(&hd_qiov
, cluster_data
,
707 512 * cur_nr_sectors
);
710 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
711 qemu_co_mutex_unlock(&s
->lock
);
712 ret
= bdrv_co_readv(bs
->file
,
713 (cluster_offset
>> 9) + index_in_cluster
,
714 cur_nr_sectors
, &hd_qiov
);
715 qemu_co_mutex_lock(&s
->lock
);
719 if (s
->crypt_method
) {
720 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
721 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
722 qemu_iovec_from_buf(qiov
, bytes_done
,
723 cluster_data
, 512 * cur_nr_sectors
);
728 g_assert_not_reached();
733 remaining_sectors
-= cur_nr_sectors
;
734 sector_num
+= cur_nr_sectors
;
735 bytes_done
+= cur_nr_sectors
* 512;
740 qemu_co_mutex_unlock(&s
->lock
);
742 qemu_iovec_destroy(&hd_qiov
);
743 qemu_vfree(cluster_data
);
748 static void run_dependent_requests(BDRVQcowState
*s
, QCowL2Meta
*m
)
750 /* Take the request off the list of running requests */
751 if (m
->nb_clusters
!= 0) {
752 QLIST_REMOVE(m
, next_in_flight
);
755 /* Restart all dependent requests */
756 if (!qemu_co_queue_empty(&m
->dependent_requests
)) {
757 qemu_co_mutex_unlock(&s
->lock
);
758 qemu_co_queue_restart_all(&m
->dependent_requests
);
759 qemu_co_mutex_lock(&s
->lock
);
763 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
765 int remaining_sectors
,
768 BDRVQcowState
*s
= bs
->opaque
;
769 int index_in_cluster
;
772 int cur_nr_sectors
; /* number of sectors in current iteration */
773 uint64_t cluster_offset
;
774 QEMUIOVector hd_qiov
;
775 uint64_t bytes_done
= 0;
776 uint8_t *cluster_data
= NULL
;
777 QCowL2Meta l2meta
= {
781 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
784 qemu_co_queue_init(&l2meta
.dependent_requests
);
786 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
788 s
->cluster_cache_offset
= -1; /* disable compressed cache */
790 qemu_co_mutex_lock(&s
->lock
);
792 while (remaining_sectors
!= 0) {
794 trace_qcow2_writev_start_part(qemu_coroutine_self());
795 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
796 n_end
= index_in_cluster
+ remaining_sectors
;
797 if (s
->crypt_method
&&
798 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
) {
799 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
802 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
803 index_in_cluster
, n_end
, &cur_nr_sectors
, &l2meta
);
808 if (l2meta
.nb_clusters
> 0 &&
809 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
)) {
810 qcow2_mark_dirty(bs
);
813 cluster_offset
= l2meta
.cluster_offset
;
814 assert((cluster_offset
& 511) == 0);
816 qemu_iovec_reset(&hd_qiov
);
817 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
818 cur_nr_sectors
* 512);
820 if (s
->crypt_method
) {
822 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
826 assert(hd_qiov
.size
<=
827 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
828 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
830 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
831 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
833 qemu_iovec_reset(&hd_qiov
);
834 qemu_iovec_add(&hd_qiov
, cluster_data
,
835 cur_nr_sectors
* 512);
838 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
839 qemu_co_mutex_unlock(&s
->lock
);
840 trace_qcow2_writev_data(qemu_coroutine_self(),
841 (cluster_offset
>> 9) + index_in_cluster
);
842 ret
= bdrv_co_writev(bs
->file
,
843 (cluster_offset
>> 9) + index_in_cluster
,
844 cur_nr_sectors
, &hd_qiov
);
845 qemu_co_mutex_lock(&s
->lock
);
850 ret
= qcow2_alloc_cluster_link_l2(bs
, &l2meta
);
855 run_dependent_requests(s
, &l2meta
);
857 remaining_sectors
-= cur_nr_sectors
;
858 sector_num
+= cur_nr_sectors
;
859 bytes_done
+= cur_nr_sectors
* 512;
860 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
865 run_dependent_requests(s
, &l2meta
);
867 qemu_co_mutex_unlock(&s
->lock
);
869 qemu_iovec_destroy(&hd_qiov
);
870 qemu_vfree(cluster_data
);
871 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
876 static void qcow2_close(BlockDriverState
*bs
)
878 BDRVQcowState
*s
= bs
->opaque
;
881 qcow2_cache_flush(bs
, s
->l2_table_cache
);
882 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
884 qcow2_mark_clean(bs
);
886 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
887 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
889 g_free(s
->unknown_header_fields
);
890 cleanup_unknown_header_ext(bs
);
892 g_free(s
->cluster_cache
);
893 qemu_vfree(s
->cluster_data
);
894 qcow2_refcount_close(bs
);
895 qcow2_free_snapshots(bs
);
898 static void qcow2_invalidate_cache(BlockDriverState
*bs
)
900 BDRVQcowState
*s
= bs
->opaque
;
901 int flags
= s
->flags
;
902 AES_KEY aes_encrypt_key
;
903 AES_KEY aes_decrypt_key
;
904 uint32_t crypt_method
= 0;
907 * Backing files are read-only which makes all of their metadata immutable,
908 * that means we don't have to worry about reopening them here.
911 if (s
->crypt_method
) {
912 crypt_method
= s
->crypt_method
;
913 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
914 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
919 memset(s
, 0, sizeof(BDRVQcowState
));
920 qcow2_open(bs
, flags
);
923 s
->crypt_method
= crypt_method
;
924 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
925 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
929 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
930 size_t len
, size_t buflen
)
932 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
933 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
935 if (buflen
< ext_len
) {
939 *ext_backing_fmt
= (QCowExtension
) {
940 .magic
= cpu_to_be32(magic
),
941 .len
= cpu_to_be32(len
),
943 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
949 * Updates the qcow2 header, including the variable length parts of it, i.e.
950 * the backing file name and all extensions. qcow2 was not designed to allow
951 * such changes, so if we run out of space (we can only use the first cluster)
952 * this function may fail.
954 * Returns 0 on success, -errno in error cases.
956 int qcow2_update_header(BlockDriverState
*bs
)
958 BDRVQcowState
*s
= bs
->opaque
;
961 size_t buflen
= s
->cluster_size
;
964 uint32_t refcount_table_clusters
;
965 size_t header_length
;
966 Qcow2UnknownHeaderExtension
*uext
;
968 buf
= qemu_blockalign(bs
, buflen
);
970 /* Header structure */
971 header
= (QCowHeader
*) buf
;
973 if (buflen
< sizeof(*header
)) {
978 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
979 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
980 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
982 *header
= (QCowHeader
) {
983 /* Version 2 fields */
984 .magic
= cpu_to_be32(QCOW_MAGIC
),
985 .version
= cpu_to_be32(s
->qcow_version
),
986 .backing_file_offset
= 0,
987 .backing_file_size
= 0,
988 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
989 .size
= cpu_to_be64(total_size
),
990 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
991 .l1_size
= cpu_to_be32(s
->l1_size
),
992 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
993 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
994 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
995 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
996 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
998 /* Version 3 fields */
999 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1000 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1001 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1002 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1003 .header_length
= cpu_to_be32(header_length
),
1006 /* For older versions, write a shorter header */
1007 switch (s
->qcow_version
) {
1009 ret
= offsetof(QCowHeader
, incompatible_features
);
1012 ret
= sizeof(*header
);
1021 memset(buf
, 0, buflen
);
1023 /* Preserve any unknown field in the header */
1024 if (s
->unknown_header_fields_size
) {
1025 if (buflen
< s
->unknown_header_fields_size
) {
1030 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1031 buf
+= s
->unknown_header_fields_size
;
1032 buflen
-= s
->unknown_header_fields_size
;
1035 /* Backing file format header extension */
1036 if (*bs
->backing_format
) {
1037 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1038 bs
->backing_format
, strlen(bs
->backing_format
),
1049 Qcow2Feature features
[] = {
1051 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1052 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1053 .name
= "dirty bit",
1056 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1057 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1058 .name
= "lazy refcounts",
1062 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1063 features
, sizeof(features
), buflen
);
1070 /* Keep unknown header extensions */
1071 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1072 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1081 /* End of header extensions */
1082 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1090 /* Backing file name */
1091 if (*bs
->backing_file
) {
1092 size_t backing_file_len
= strlen(bs
->backing_file
);
1094 if (buflen
< backing_file_len
) {
1099 strncpy(buf
, bs
->backing_file
, buflen
);
1101 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1102 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1105 /* Write the new header */
1106 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1117 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1118 const char *backing_file
, const char *backing_fmt
)
1120 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1121 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1123 return qcow2_update_header(bs
);
1126 static int preallocate(BlockDriverState
*bs
)
1128 uint64_t nb_sectors
;
1134 nb_sectors
= bdrv_getlength(bs
) >> 9;
1136 qemu_co_queue_init(&meta
.dependent_requests
);
1137 meta
.cluster_offset
= 0;
1139 while (nb_sectors
) {
1140 num
= MIN(nb_sectors
, INT_MAX
>> 9);
1141 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
, &meta
);
1146 ret
= qcow2_alloc_cluster_link_l2(bs
, &meta
);
1148 qcow2_free_any_clusters(bs
, meta
.cluster_offset
, meta
.nb_clusters
);
1152 /* There are no dependent requests, but we need to remove our request
1153 * from the list of in-flight requests */
1154 run_dependent_requests(bs
->opaque
, &meta
);
1156 /* TODO Preallocate data if requested */
1163 * It is expected that the image file is large enough to actually contain
1164 * all of the allocated clusters (otherwise we get failing reads after
1165 * EOF). Extend the image to the last allocated sector.
1167 if (meta
.cluster_offset
!= 0) {
1169 memset(buf
, 0, 512);
1170 ret
= bdrv_write(bs
->file
, (meta
.cluster_offset
>> 9) + num
- 1, buf
, 1);
1179 static int qcow2_create2(const char *filename
, int64_t total_size
,
1180 const char *backing_file
, const char *backing_format
,
1181 int flags
, size_t cluster_size
, int prealloc
,
1182 QEMUOptionParameter
*options
, int version
)
1184 /* Calculate cluster_bits */
1186 cluster_bits
= ffs(cluster_size
) - 1;
1187 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1188 (1 << cluster_bits
) != cluster_size
)
1191 "Cluster size must be a power of two between %d and %dk",
1192 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1197 * Open the image file and write a minimal qcow2 header.
1199 * We keep things simple and start with a zero-sized image. We also
1200 * do without refcount blocks or a L1 table for now. We'll fix the
1201 * inconsistency later.
1203 * We do need a refcount table because growing the refcount table means
1204 * allocating two new refcount blocks - the seconds of which would be at
1205 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1206 * size for any qcow2 image.
1208 BlockDriverState
* bs
;
1210 uint8_t* refcount_table
;
1213 ret
= bdrv_create_file(filename
, options
);
1218 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1223 /* Write the header */
1224 memset(&header
, 0, sizeof(header
));
1225 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1226 header
.version
= cpu_to_be32(version
);
1227 header
.cluster_bits
= cpu_to_be32(cluster_bits
);
1228 header
.size
= cpu_to_be64(0);
1229 header
.l1_table_offset
= cpu_to_be64(0);
1230 header
.l1_size
= cpu_to_be32(0);
1231 header
.refcount_table_offset
= cpu_to_be64(cluster_size
);
1232 header
.refcount_table_clusters
= cpu_to_be32(1);
1233 header
.refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
);
1234 header
.header_length
= cpu_to_be32(sizeof(header
));
1236 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1237 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1239 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1242 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1243 header
.compatible_features
|=
1244 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1247 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
1252 /* Write an empty refcount table */
1253 refcount_table
= g_malloc0(cluster_size
);
1254 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1255 g_free(refcount_table
);
1264 * And now open the image and make it consistent first (i.e. increase the
1265 * refcount of the cluster that is occupied by the header and the refcount
1268 BlockDriver
* drv
= bdrv_find_format("qcow2");
1269 assert(drv
!= NULL
);
1270 ret
= bdrv_open(bs
, filename
,
1271 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
);
1276 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1280 } else if (ret
!= 0) {
1281 error_report("Huh, first cluster in empty image is already in use?");
1285 /* Okay, now that we have a valid image, let's give it the right size */
1286 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1291 /* Want a backing file? There you go.*/
1293 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1299 /* And if we're supposed to preallocate metadata, do that now */
1301 BDRVQcowState
*s
= bs
->opaque
;
1302 qemu_co_mutex_lock(&s
->lock
);
1303 ret
= preallocate(bs
);
1304 qemu_co_mutex_unlock(&s
->lock
);
1316 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
1318 const char *backing_file
= NULL
;
1319 const char *backing_fmt
= NULL
;
1320 uint64_t sectors
= 0;
1322 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1326 /* Read out options */
1327 while (options
&& options
->name
) {
1328 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1329 sectors
= options
->value
.n
/ 512;
1330 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1331 backing_file
= options
->value
.s
;
1332 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1333 backing_fmt
= options
->value
.s
;
1334 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1335 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1336 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1337 if (options
->value
.n
) {
1338 cluster_size
= options
->value
.n
;
1340 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1341 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1343 } else if (!strcmp(options
->value
.s
, "metadata")) {
1346 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1350 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1351 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "0.10")) {
1353 } else if (!strcmp(options
->value
.s
, "1.1")) {
1356 fprintf(stderr
, "Invalid compatibility level: '%s'\n",
1360 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1361 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1366 if (backing_file
&& prealloc
) {
1367 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1372 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1373 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
1374 "level 1.1 and above (use compat=1.1 or greater)\n");
1378 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1379 cluster_size
, prealloc
, options
, version
);
1382 static int qcow2_make_empty(BlockDriverState
*bs
)
1385 /* XXX: not correct */
1386 BDRVQcowState
*s
= bs
->opaque
;
1387 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1390 memset(s
->l1_table
, 0, l1_length
);
1391 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1393 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1402 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1403 int64_t sector_num
, int nb_sectors
)
1406 BDRVQcowState
*s
= bs
->opaque
;
1408 /* Emulate misaligned zero writes */
1409 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1413 /* Whatever is left can use real zero clusters */
1414 qemu_co_mutex_lock(&s
->lock
);
1415 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1417 qemu_co_mutex_unlock(&s
->lock
);
1422 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1423 int64_t sector_num
, int nb_sectors
)
1426 BDRVQcowState
*s
= bs
->opaque
;
1428 qemu_co_mutex_lock(&s
->lock
);
1429 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1431 qemu_co_mutex_unlock(&s
->lock
);
1435 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1437 BDRVQcowState
*s
= bs
->opaque
;
1438 int ret
, new_l1_size
;
1441 error_report("The new size must be a multiple of 512");
1445 /* cannot proceed if image has snapshots */
1446 if (s
->nb_snapshots
) {
1447 error_report("Can't resize an image which has snapshots");
1451 /* shrinking is currently not supported */
1452 if (offset
< bs
->total_sectors
* 512) {
1453 error_report("qcow2 doesn't support shrinking images yet");
1457 new_l1_size
= size_to_l1(s
, offset
);
1458 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1463 /* write updated header.size */
1464 offset
= cpu_to_be64(offset
);
1465 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1466 &offset
, sizeof(uint64_t));
1471 s
->l1_vm_state_index
= new_l1_size
;
1475 /* XXX: put compressed sectors first, then all the cluster aligned
1476 tables to avoid losing bytes in alignment */
1477 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1478 const uint8_t *buf
, int nb_sectors
)
1480 BDRVQcowState
*s
= bs
->opaque
;
1484 uint64_t cluster_offset
;
1486 if (nb_sectors
== 0) {
1487 /* align end of file to a sector boundary to ease reading with
1488 sector based I/Os */
1489 cluster_offset
= bdrv_getlength(bs
->file
);
1490 cluster_offset
= (cluster_offset
+ 511) & ~511;
1491 bdrv_truncate(bs
->file
, cluster_offset
);
1495 if (nb_sectors
!= s
->cluster_sectors
)
1498 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1500 /* best compression, small window, no zlib header */
1501 memset(&strm
, 0, sizeof(strm
));
1502 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1504 9, Z_DEFAULT_STRATEGY
);
1510 strm
.avail_in
= s
->cluster_size
;
1511 strm
.next_in
= (uint8_t *)buf
;
1512 strm
.avail_out
= s
->cluster_size
;
1513 strm
.next_out
= out_buf
;
1515 ret
= deflate(&strm
, Z_FINISH
);
1516 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1521 out_len
= strm
.next_out
- out_buf
;
1525 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1526 /* could not compress: write normal cluster */
1527 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1532 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1533 sector_num
<< 9, out_len
);
1534 if (!cluster_offset
) {
1538 cluster_offset
&= s
->cluster_offset_mask
;
1539 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1540 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1552 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1554 BDRVQcowState
*s
= bs
->opaque
;
1557 qemu_co_mutex_lock(&s
->lock
);
1558 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1560 qemu_co_mutex_unlock(&s
->lock
);
1564 if (qcow2_need_accurate_refcounts(s
)) {
1565 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1567 qemu_co_mutex_unlock(&s
->lock
);
1571 qemu_co_mutex_unlock(&s
->lock
);
1576 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1578 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1581 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1583 BDRVQcowState
*s
= bs
->opaque
;
1584 bdi
->cluster_size
= s
->cluster_size
;
1585 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1590 static void dump_refcounts(BlockDriverState
*bs
)
1592 BDRVQcowState
*s
= bs
->opaque
;
1593 int64_t nb_clusters
, k
, k1
, size
;
1596 size
= bdrv_getlength(bs
->file
);
1597 nb_clusters
= size_to_clusters(s
, size
);
1598 for(k
= 0; k
< nb_clusters
;) {
1600 refcount
= get_refcount(bs
, k
);
1602 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1604 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1610 static int qcow2_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1611 int64_t pos
, int size
)
1613 BDRVQcowState
*s
= bs
->opaque
;
1614 int growable
= bs
->growable
;
1617 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1619 ret
= bdrv_pwrite(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1620 bs
->growable
= growable
;
1625 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1626 int64_t pos
, int size
)
1628 BDRVQcowState
*s
= bs
->opaque
;
1629 int growable
= bs
->growable
;
1632 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1634 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1635 bs
->growable
= growable
;
1640 static QEMUOptionParameter qcow2_create_options
[] = {
1642 .name
= BLOCK_OPT_SIZE
,
1644 .help
= "Virtual disk size"
1647 .name
= BLOCK_OPT_COMPAT_LEVEL
,
1649 .help
= "Compatibility level (0.10 or 1.1)"
1652 .name
= BLOCK_OPT_BACKING_FILE
,
1654 .help
= "File name of a base image"
1657 .name
= BLOCK_OPT_BACKING_FMT
,
1659 .help
= "Image format of the base image"
1662 .name
= BLOCK_OPT_ENCRYPT
,
1664 .help
= "Encrypt the image"
1667 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1669 .help
= "qcow2 cluster size",
1670 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
1673 .name
= BLOCK_OPT_PREALLOC
,
1675 .help
= "Preallocation mode (allowed values: off, metadata)"
1678 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
1680 .help
= "Postpone refcount updates",
1685 static BlockDriver bdrv_qcow2
= {
1686 .format_name
= "qcow2",
1687 .instance_size
= sizeof(BDRVQcowState
),
1688 .bdrv_probe
= qcow2_probe
,
1689 .bdrv_open
= qcow2_open
,
1690 .bdrv_close
= qcow2_close
,
1691 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
1692 .bdrv_create
= qcow2_create
,
1693 .bdrv_co_is_allocated
= qcow2_co_is_allocated
,
1694 .bdrv_set_key
= qcow2_set_key
,
1695 .bdrv_make_empty
= qcow2_make_empty
,
1697 .bdrv_co_readv
= qcow2_co_readv
,
1698 .bdrv_co_writev
= qcow2_co_writev
,
1699 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
1701 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
1702 .bdrv_co_discard
= qcow2_co_discard
,
1703 .bdrv_truncate
= qcow2_truncate
,
1704 .bdrv_write_compressed
= qcow2_write_compressed
,
1706 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1707 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1708 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1709 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1710 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1711 .bdrv_get_info
= qcow2_get_info
,
1713 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1714 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1716 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1718 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
1720 .create_options
= qcow2_create_options
,
1721 .bdrv_check
= qcow2_check
,
1724 static void bdrv_qcow2_init(void)
1726 bdrv_register(&bdrv_qcow2
);
1729 block_init(bdrv_qcow2_init
);