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"
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
);
276 * Marks the image as corrupt.
278 int qcow2_mark_corrupt(BlockDriverState
*bs
)
280 BDRVQcowState
*s
= bs
->opaque
;
282 s
->incompatible_features
|= QCOW2_INCOMPAT_CORRUPT
;
283 return qcow2_update_header(bs
);
287 * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
288 * before if necessary.
290 int qcow2_mark_consistent(BlockDriverState
*bs
)
292 BDRVQcowState
*s
= bs
->opaque
;
294 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
295 int ret
= bdrv_flush(bs
);
300 s
->incompatible_features
&= ~QCOW2_INCOMPAT_CORRUPT
;
301 return qcow2_update_header(bs
);
306 static int qcow2_check(BlockDriverState
*bs
, BdrvCheckResult
*result
,
309 int ret
= qcow2_check_refcounts(bs
, result
, fix
);
314 if (fix
&& result
->check_errors
== 0 && result
->corruptions
== 0) {
315 return qcow2_mark_clean(bs
);
320 static QemuOptsList qcow2_runtime_opts
= {
322 .head
= QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts
.head
),
325 .name
= QCOW2_OPT_LAZY_REFCOUNTS
,
326 .type
= QEMU_OPT_BOOL
,
327 .help
= "Postpone refcount updates",
330 .name
= QCOW2_OPT_DISCARD_REQUEST
,
331 .type
= QEMU_OPT_BOOL
,
332 .help
= "Pass guest discard requests to the layer below",
335 .name
= QCOW2_OPT_DISCARD_SNAPSHOT
,
336 .type
= QEMU_OPT_BOOL
,
337 .help
= "Generate discard requests when snapshot related space "
341 .name
= QCOW2_OPT_DISCARD_OTHER
,
342 .type
= QEMU_OPT_BOOL
,
343 .help
= "Generate discard requests when other clusters are freed",
345 { /* end of list */ }
349 static int qcow2_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
351 BDRVQcowState
*s
= bs
->opaque
;
355 Error
*local_err
= NULL
;
357 uint64_t l1_vm_state_index
;
359 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
363 be32_to_cpus(&header
.magic
);
364 be32_to_cpus(&header
.version
);
365 be64_to_cpus(&header
.backing_file_offset
);
366 be32_to_cpus(&header
.backing_file_size
);
367 be64_to_cpus(&header
.size
);
368 be32_to_cpus(&header
.cluster_bits
);
369 be32_to_cpus(&header
.crypt_method
);
370 be64_to_cpus(&header
.l1_table_offset
);
371 be32_to_cpus(&header
.l1_size
);
372 be64_to_cpus(&header
.refcount_table_offset
);
373 be32_to_cpus(&header
.refcount_table_clusters
);
374 be64_to_cpus(&header
.snapshots_offset
);
375 be32_to_cpus(&header
.nb_snapshots
);
377 if (header
.magic
!= QCOW_MAGIC
) {
381 if (header
.version
< 2 || header
.version
> 3) {
382 report_unsupported(bs
, "QCOW version %d", header
.version
);
387 s
->qcow_version
= header
.version
;
389 /* Initialise version 3 header fields */
390 if (header
.version
== 2) {
391 header
.incompatible_features
= 0;
392 header
.compatible_features
= 0;
393 header
.autoclear_features
= 0;
394 header
.refcount_order
= 4;
395 header
.header_length
= 72;
397 be64_to_cpus(&header
.incompatible_features
);
398 be64_to_cpus(&header
.compatible_features
);
399 be64_to_cpus(&header
.autoclear_features
);
400 be32_to_cpus(&header
.refcount_order
);
401 be32_to_cpus(&header
.header_length
);
404 if (header
.header_length
> sizeof(header
)) {
405 s
->unknown_header_fields_size
= header
.header_length
- sizeof(header
);
406 s
->unknown_header_fields
= g_malloc(s
->unknown_header_fields_size
);
407 ret
= bdrv_pread(bs
->file
, sizeof(header
), s
->unknown_header_fields
,
408 s
->unknown_header_fields_size
);
414 if (header
.backing_file_offset
) {
415 ext_end
= header
.backing_file_offset
;
417 ext_end
= 1 << header
.cluster_bits
;
420 /* Handle feature bits */
421 s
->incompatible_features
= header
.incompatible_features
;
422 s
->compatible_features
= header
.compatible_features
;
423 s
->autoclear_features
= header
.autoclear_features
;
425 if (s
->incompatible_features
& ~QCOW2_INCOMPAT_MASK
) {
426 void *feature_table
= NULL
;
427 qcow2_read_extensions(bs
, header
.header_length
, ext_end
,
429 report_unsupported_feature(bs
, feature_table
,
430 s
->incompatible_features
&
431 ~QCOW2_INCOMPAT_MASK
);
436 if (s
->incompatible_features
& QCOW2_INCOMPAT_CORRUPT
) {
437 /* Corrupt images may not be written to unless they are being repaired
439 if ((flags
& BDRV_O_RDWR
) && !(flags
& BDRV_O_CHECK
)) {
440 error_report("qcow2: Image is corrupt; cannot be opened "
447 /* Check support for various header values */
448 if (header
.refcount_order
!= 4) {
449 report_unsupported(bs
, "%d bit reference counts",
450 1 << header
.refcount_order
);
455 if (header
.cluster_bits
< MIN_CLUSTER_BITS
||
456 header
.cluster_bits
> MAX_CLUSTER_BITS
) {
460 if (header
.crypt_method
> QCOW_CRYPT_AES
) {
464 s
->crypt_method_header
= header
.crypt_method
;
465 if (s
->crypt_method_header
) {
468 s
->cluster_bits
= header
.cluster_bits
;
469 s
->cluster_size
= 1 << s
->cluster_bits
;
470 s
->cluster_sectors
= 1 << (s
->cluster_bits
- 9);
471 s
->l2_bits
= s
->cluster_bits
- 3; /* L2 is always one cluster */
472 s
->l2_size
= 1 << s
->l2_bits
;
473 bs
->total_sectors
= header
.size
/ 512;
474 s
->csize_shift
= (62 - (s
->cluster_bits
- 8));
475 s
->csize_mask
= (1 << (s
->cluster_bits
- 8)) - 1;
476 s
->cluster_offset_mask
= (1LL << s
->csize_shift
) - 1;
477 s
->refcount_table_offset
= header
.refcount_table_offset
;
478 s
->refcount_table_size
=
479 header
.refcount_table_clusters
<< (s
->cluster_bits
- 3);
481 s
->snapshots_offset
= header
.snapshots_offset
;
482 s
->nb_snapshots
= header
.nb_snapshots
;
484 /* read the level 1 table */
485 s
->l1_size
= header
.l1_size
;
487 l1_vm_state_index
= size_to_l1(s
, header
.size
);
488 if (l1_vm_state_index
> INT_MAX
) {
492 s
->l1_vm_state_index
= l1_vm_state_index
;
494 /* the L1 table must contain at least enough entries to put
496 if (s
->l1_size
< s
->l1_vm_state_index
) {
500 s
->l1_table_offset
= header
.l1_table_offset
;
501 if (s
->l1_size
> 0) {
502 s
->l1_table
= g_malloc0(
503 align_offset(s
->l1_size
* sizeof(uint64_t), 512));
504 ret
= bdrv_pread(bs
->file
, s
->l1_table_offset
, s
->l1_table
,
505 s
->l1_size
* sizeof(uint64_t));
509 for(i
= 0;i
< s
->l1_size
; i
++) {
510 be64_to_cpus(&s
->l1_table
[i
]);
514 /* alloc L2 table/refcount block cache */
515 s
->l2_table_cache
= qcow2_cache_create(bs
, L2_CACHE_SIZE
);
516 s
->refcount_block_cache
= qcow2_cache_create(bs
, REFCOUNT_CACHE_SIZE
);
518 s
->cluster_cache
= g_malloc(s
->cluster_size
);
519 /* one more sector for decompressed data alignment */
520 s
->cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
522 s
->cluster_cache_offset
= -1;
525 ret
= qcow2_refcount_init(bs
);
530 QLIST_INIT(&s
->cluster_allocs
);
531 QTAILQ_INIT(&s
->discards
);
533 /* read qcow2 extensions */
534 if (qcow2_read_extensions(bs
, header
.header_length
, ext_end
, NULL
)) {
539 /* read the backing file name */
540 if (header
.backing_file_offset
!= 0) {
541 len
= header
.backing_file_size
;
545 ret
= bdrv_pread(bs
->file
, header
.backing_file_offset
,
546 bs
->backing_file
, len
);
550 bs
->backing_file
[len
] = '\0';
553 ret
= qcow2_read_snapshots(bs
);
558 /* Clear unknown autoclear feature bits */
559 if (!bs
->read_only
&& s
->autoclear_features
!= 0) {
560 s
->autoclear_features
= 0;
561 ret
= qcow2_update_header(bs
);
567 /* Initialise locks */
568 qemu_co_mutex_init(&s
->lock
);
570 /* Repair image if dirty */
571 if (!(flags
& BDRV_O_CHECK
) && !bs
->read_only
&&
572 (s
->incompatible_features
& QCOW2_INCOMPAT_DIRTY
)) {
573 BdrvCheckResult result
= {0};
575 ret
= qcow2_check(bs
, &result
, BDRV_FIX_ERRORS
);
581 /* Enable lazy_refcounts according to image and command line options */
582 opts
= qemu_opts_create_nofail(&qcow2_runtime_opts
);
583 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
584 if (error_is_set(&local_err
)) {
585 qerror_report_err(local_err
);
586 error_free(local_err
);
591 s
->use_lazy_refcounts
= qemu_opt_get_bool(opts
, QCOW2_OPT_LAZY_REFCOUNTS
,
592 (s
->compatible_features
& QCOW2_COMPAT_LAZY_REFCOUNTS
));
594 s
->discard_passthrough
[QCOW2_DISCARD_NEVER
] = false;
595 s
->discard_passthrough
[QCOW2_DISCARD_ALWAYS
] = true;
596 s
->discard_passthrough
[QCOW2_DISCARD_REQUEST
] =
597 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_REQUEST
,
598 flags
& BDRV_O_UNMAP
);
599 s
->discard_passthrough
[QCOW2_DISCARD_SNAPSHOT
] =
600 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_SNAPSHOT
, true);
601 s
->discard_passthrough
[QCOW2_DISCARD_OTHER
] =
602 qemu_opt_get_bool(opts
, QCOW2_OPT_DISCARD_OTHER
, false);
606 if (s
->use_lazy_refcounts
&& s
->qcow_version
< 3) {
607 qerror_report(ERROR_CLASS_GENERIC_ERROR
, "Lazy refcounts require "
608 "a qcow2 image with at least qemu 1.1 compatibility level");
615 BdrvCheckResult result
= {0};
616 qcow2_check_refcounts(bs
, &result
, 0);
622 g_free(s
->unknown_header_fields
);
623 cleanup_unknown_header_ext(bs
);
624 qcow2_free_snapshots(bs
);
625 qcow2_refcount_close(bs
);
627 if (s
->l2_table_cache
) {
628 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
630 g_free(s
->cluster_cache
);
631 qemu_vfree(s
->cluster_data
);
635 static int qcow2_set_key(BlockDriverState
*bs
, const char *key
)
637 BDRVQcowState
*s
= bs
->opaque
;
641 memset(keybuf
, 0, 16);
645 /* XXX: we could compress the chars to 7 bits to increase
647 for(i
= 0;i
< len
;i
++) {
650 s
->crypt_method
= s
->crypt_method_header
;
652 if (AES_set_encrypt_key(keybuf
, 128, &s
->aes_encrypt_key
) != 0)
654 if (AES_set_decrypt_key(keybuf
, 128, &s
->aes_decrypt_key
) != 0)
664 AES_encrypt(in
, tmp
, &s
->aes_encrypt_key
);
665 AES_decrypt(tmp
, out
, &s
->aes_decrypt_key
);
666 for(i
= 0; i
< 16; i
++)
667 printf(" %02x", tmp
[i
]);
669 for(i
= 0; i
< 16; i
++)
670 printf(" %02x", out
[i
]);
677 /* We have nothing to do for QCOW2 reopen, stubs just return
679 static int qcow2_reopen_prepare(BDRVReopenState
*state
,
680 BlockReopenQueue
*queue
, Error
**errp
)
685 static int coroutine_fn
qcow2_co_is_allocated(BlockDriverState
*bs
,
686 int64_t sector_num
, int nb_sectors
, int *pnum
)
688 BDRVQcowState
*s
= bs
->opaque
;
689 uint64_t cluster_offset
;
693 /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
694 * can't pass them on today */
695 qemu_co_mutex_lock(&s
->lock
);
696 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9, pnum
, &cluster_offset
);
697 qemu_co_mutex_unlock(&s
->lock
);
702 return (cluster_offset
!= 0) || (ret
== QCOW2_CLUSTER_ZERO
);
705 /* handle reading after the end of the backing file */
706 int qcow2_backing_read1(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
707 int64_t sector_num
, int nb_sectors
)
710 if ((sector_num
+ nb_sectors
) <= bs
->total_sectors
)
712 if (sector_num
>= bs
->total_sectors
)
715 n1
= bs
->total_sectors
- sector_num
;
717 qemu_iovec_memset(qiov
, 512 * n1
, 0, 512 * (nb_sectors
- n1
));
722 static coroutine_fn
int qcow2_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
723 int remaining_sectors
, QEMUIOVector
*qiov
)
725 BDRVQcowState
*s
= bs
->opaque
;
726 int index_in_cluster
, n1
;
728 int cur_nr_sectors
; /* number of sectors in current iteration */
729 uint64_t cluster_offset
= 0;
730 uint64_t bytes_done
= 0;
731 QEMUIOVector hd_qiov
;
732 uint8_t *cluster_data
= NULL
;
734 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
736 qemu_co_mutex_lock(&s
->lock
);
738 while (remaining_sectors
!= 0) {
740 /* prepare next request */
741 cur_nr_sectors
= remaining_sectors
;
742 if (s
->crypt_method
) {
743 cur_nr_sectors
= MIN(cur_nr_sectors
,
744 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
747 ret
= qcow2_get_cluster_offset(bs
, sector_num
<< 9,
748 &cur_nr_sectors
, &cluster_offset
);
753 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
755 qemu_iovec_reset(&hd_qiov
);
756 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
757 cur_nr_sectors
* 512);
760 case QCOW2_CLUSTER_UNALLOCATED
:
762 if (bs
->backing_hd
) {
763 /* read from the base image */
764 n1
= qcow2_backing_read1(bs
->backing_hd
, &hd_qiov
,
765 sector_num
, cur_nr_sectors
);
767 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_BACKING_AIO
);
768 qemu_co_mutex_unlock(&s
->lock
);
769 ret
= bdrv_co_readv(bs
->backing_hd
, sector_num
,
771 qemu_co_mutex_lock(&s
->lock
);
777 /* Note: in this case, no need to wait */
778 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
782 case QCOW2_CLUSTER_ZERO
:
783 qemu_iovec_memset(&hd_qiov
, 0, 0, 512 * cur_nr_sectors
);
786 case QCOW2_CLUSTER_COMPRESSED
:
787 /* add AIO support for compressed blocks ? */
788 ret
= qcow2_decompress_cluster(bs
, cluster_offset
);
793 qemu_iovec_from_buf(&hd_qiov
, 0,
794 s
->cluster_cache
+ index_in_cluster
* 512,
795 512 * cur_nr_sectors
);
798 case QCOW2_CLUSTER_NORMAL
:
799 if ((cluster_offset
& 511) != 0) {
804 if (s
->crypt_method
) {
806 * For encrypted images, read everything into a temporary
807 * contiguous buffer on which the AES functions can work.
811 qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
814 assert(cur_nr_sectors
<=
815 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
);
816 qemu_iovec_reset(&hd_qiov
);
817 qemu_iovec_add(&hd_qiov
, cluster_data
,
818 512 * cur_nr_sectors
);
821 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
822 qemu_co_mutex_unlock(&s
->lock
);
823 ret
= bdrv_co_readv(bs
->file
,
824 (cluster_offset
>> 9) + index_in_cluster
,
825 cur_nr_sectors
, &hd_qiov
);
826 qemu_co_mutex_lock(&s
->lock
);
830 if (s
->crypt_method
) {
831 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
832 cluster_data
, cur_nr_sectors
, 0, &s
->aes_decrypt_key
);
833 qemu_iovec_from_buf(qiov
, bytes_done
,
834 cluster_data
, 512 * cur_nr_sectors
);
839 g_assert_not_reached();
844 remaining_sectors
-= cur_nr_sectors
;
845 sector_num
+= cur_nr_sectors
;
846 bytes_done
+= cur_nr_sectors
* 512;
851 qemu_co_mutex_unlock(&s
->lock
);
853 qemu_iovec_destroy(&hd_qiov
);
854 qemu_vfree(cluster_data
);
859 static coroutine_fn
int qcow2_co_writev(BlockDriverState
*bs
,
861 int remaining_sectors
,
864 BDRVQcowState
*s
= bs
->opaque
;
865 int index_in_cluster
;
868 int cur_nr_sectors
; /* number of sectors in current iteration */
869 uint64_t cluster_offset
;
870 QEMUIOVector hd_qiov
;
871 uint64_t bytes_done
= 0;
872 uint8_t *cluster_data
= NULL
;
873 QCowL2Meta
*l2meta
= NULL
;
875 trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num
,
878 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
880 s
->cluster_cache_offset
= -1; /* disable compressed cache */
882 qemu_co_mutex_lock(&s
->lock
);
884 while (remaining_sectors
!= 0) {
888 trace_qcow2_writev_start_part(qemu_coroutine_self());
889 index_in_cluster
= sector_num
& (s
->cluster_sectors
- 1);
890 n_end
= index_in_cluster
+ remaining_sectors
;
891 if (s
->crypt_method
&&
892 n_end
> QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
) {
893 n_end
= QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_sectors
;
896 ret
= qcow2_alloc_cluster_offset(bs
, sector_num
<< 9,
897 index_in_cluster
, n_end
, &cur_nr_sectors
, &cluster_offset
, &l2meta
);
902 assert((cluster_offset
& 511) == 0);
904 qemu_iovec_reset(&hd_qiov
);
905 qemu_iovec_concat(&hd_qiov
, qiov
, bytes_done
,
906 cur_nr_sectors
* 512);
908 if (s
->crypt_method
) {
910 cluster_data
= qemu_blockalign(bs
, QCOW_MAX_CRYPT_CLUSTERS
*
914 assert(hd_qiov
.size
<=
915 QCOW_MAX_CRYPT_CLUSTERS
* s
->cluster_size
);
916 qemu_iovec_to_buf(&hd_qiov
, 0, cluster_data
, hd_qiov
.size
);
918 qcow2_encrypt_sectors(s
, sector_num
, cluster_data
,
919 cluster_data
, cur_nr_sectors
, 1, &s
->aes_encrypt_key
);
921 qemu_iovec_reset(&hd_qiov
);
922 qemu_iovec_add(&hd_qiov
, cluster_data
,
923 cur_nr_sectors
* 512);
926 qemu_co_mutex_unlock(&s
->lock
);
927 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
928 trace_qcow2_writev_data(qemu_coroutine_self(),
929 (cluster_offset
>> 9) + index_in_cluster
);
930 ret
= bdrv_co_writev(bs
->file
,
931 (cluster_offset
>> 9) + index_in_cluster
,
932 cur_nr_sectors
, &hd_qiov
);
933 qemu_co_mutex_lock(&s
->lock
);
938 while (l2meta
!= NULL
) {
941 ret
= qcow2_alloc_cluster_link_l2(bs
, l2meta
);
946 /* Take the request off the list of running requests */
947 if (l2meta
->nb_clusters
!= 0) {
948 QLIST_REMOVE(l2meta
, next_in_flight
);
951 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
958 remaining_sectors
-= cur_nr_sectors
;
959 sector_num
+= cur_nr_sectors
;
960 bytes_done
+= cur_nr_sectors
* 512;
961 trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors
);
966 qemu_co_mutex_unlock(&s
->lock
);
968 while (l2meta
!= NULL
) {
971 if (l2meta
->nb_clusters
!= 0) {
972 QLIST_REMOVE(l2meta
, next_in_flight
);
974 qemu_co_queue_restart_all(&l2meta
->dependent_requests
);
981 qemu_iovec_destroy(&hd_qiov
);
982 qemu_vfree(cluster_data
);
983 trace_qcow2_writev_done_req(qemu_coroutine_self(), ret
);
988 static void qcow2_close(BlockDriverState
*bs
)
990 BDRVQcowState
*s
= bs
->opaque
;
993 qcow2_cache_flush(bs
, s
->l2_table_cache
);
994 qcow2_cache_flush(bs
, s
->refcount_block_cache
);
996 qcow2_mark_clean(bs
);
998 qcow2_cache_destroy(bs
, s
->l2_table_cache
);
999 qcow2_cache_destroy(bs
, s
->refcount_block_cache
);
1001 g_free(s
->unknown_header_fields
);
1002 cleanup_unknown_header_ext(bs
);
1004 g_free(s
->cluster_cache
);
1005 qemu_vfree(s
->cluster_data
);
1006 qcow2_refcount_close(bs
);
1007 qcow2_free_snapshots(bs
);
1010 static void qcow2_invalidate_cache(BlockDriverState
*bs
)
1012 BDRVQcowState
*s
= bs
->opaque
;
1013 int flags
= s
->flags
;
1014 AES_KEY aes_encrypt_key
;
1015 AES_KEY aes_decrypt_key
;
1016 uint32_t crypt_method
= 0;
1020 * Backing files are read-only which makes all of their metadata immutable,
1021 * that means we don't have to worry about reopening them here.
1024 if (s
->crypt_method
) {
1025 crypt_method
= s
->crypt_method
;
1026 memcpy(&aes_encrypt_key
, &s
->aes_encrypt_key
, sizeof(aes_encrypt_key
));
1027 memcpy(&aes_decrypt_key
, &s
->aes_decrypt_key
, sizeof(aes_decrypt_key
));
1032 options
= qdict_new();
1033 qdict_put(options
, QCOW2_OPT_LAZY_REFCOUNTS
,
1034 qbool_from_int(s
->use_lazy_refcounts
));
1036 memset(s
, 0, sizeof(BDRVQcowState
));
1037 qcow2_open(bs
, options
, flags
);
1042 s
->crypt_method
= crypt_method
;
1043 memcpy(&s
->aes_encrypt_key
, &aes_encrypt_key
, sizeof(aes_encrypt_key
));
1044 memcpy(&s
->aes_decrypt_key
, &aes_decrypt_key
, sizeof(aes_decrypt_key
));
1048 static size_t header_ext_add(char *buf
, uint32_t magic
, const void *s
,
1049 size_t len
, size_t buflen
)
1051 QCowExtension
*ext_backing_fmt
= (QCowExtension
*) buf
;
1052 size_t ext_len
= sizeof(QCowExtension
) + ((len
+ 7) & ~7);
1054 if (buflen
< ext_len
) {
1058 *ext_backing_fmt
= (QCowExtension
) {
1059 .magic
= cpu_to_be32(magic
),
1060 .len
= cpu_to_be32(len
),
1062 memcpy(buf
+ sizeof(QCowExtension
), s
, len
);
1068 * Updates the qcow2 header, including the variable length parts of it, i.e.
1069 * the backing file name and all extensions. qcow2 was not designed to allow
1070 * such changes, so if we run out of space (we can only use the first cluster)
1071 * this function may fail.
1073 * Returns 0 on success, -errno in error cases.
1075 int qcow2_update_header(BlockDriverState
*bs
)
1077 BDRVQcowState
*s
= bs
->opaque
;
1080 size_t buflen
= s
->cluster_size
;
1082 uint64_t total_size
;
1083 uint32_t refcount_table_clusters
;
1084 size_t header_length
;
1085 Qcow2UnknownHeaderExtension
*uext
;
1087 buf
= qemu_blockalign(bs
, buflen
);
1089 /* Header structure */
1090 header
= (QCowHeader
*) buf
;
1092 if (buflen
< sizeof(*header
)) {
1097 header_length
= sizeof(*header
) + s
->unknown_header_fields_size
;
1098 total_size
= bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1099 refcount_table_clusters
= s
->refcount_table_size
>> (s
->cluster_bits
- 3);
1101 *header
= (QCowHeader
) {
1102 /* Version 2 fields */
1103 .magic
= cpu_to_be32(QCOW_MAGIC
),
1104 .version
= cpu_to_be32(s
->qcow_version
),
1105 .backing_file_offset
= 0,
1106 .backing_file_size
= 0,
1107 .cluster_bits
= cpu_to_be32(s
->cluster_bits
),
1108 .size
= cpu_to_be64(total_size
),
1109 .crypt_method
= cpu_to_be32(s
->crypt_method_header
),
1110 .l1_size
= cpu_to_be32(s
->l1_size
),
1111 .l1_table_offset
= cpu_to_be64(s
->l1_table_offset
),
1112 .refcount_table_offset
= cpu_to_be64(s
->refcount_table_offset
),
1113 .refcount_table_clusters
= cpu_to_be32(refcount_table_clusters
),
1114 .nb_snapshots
= cpu_to_be32(s
->nb_snapshots
),
1115 .snapshots_offset
= cpu_to_be64(s
->snapshots_offset
),
1117 /* Version 3 fields */
1118 .incompatible_features
= cpu_to_be64(s
->incompatible_features
),
1119 .compatible_features
= cpu_to_be64(s
->compatible_features
),
1120 .autoclear_features
= cpu_to_be64(s
->autoclear_features
),
1121 .refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
),
1122 .header_length
= cpu_to_be32(header_length
),
1125 /* For older versions, write a shorter header */
1126 switch (s
->qcow_version
) {
1128 ret
= offsetof(QCowHeader
, incompatible_features
);
1131 ret
= sizeof(*header
);
1140 memset(buf
, 0, buflen
);
1142 /* Preserve any unknown field in the header */
1143 if (s
->unknown_header_fields_size
) {
1144 if (buflen
< s
->unknown_header_fields_size
) {
1149 memcpy(buf
, s
->unknown_header_fields
, s
->unknown_header_fields_size
);
1150 buf
+= s
->unknown_header_fields_size
;
1151 buflen
-= s
->unknown_header_fields_size
;
1154 /* Backing file format header extension */
1155 if (*bs
->backing_format
) {
1156 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_BACKING_FORMAT
,
1157 bs
->backing_format
, strlen(bs
->backing_format
),
1168 Qcow2Feature features
[] = {
1170 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1171 .bit
= QCOW2_INCOMPAT_DIRTY_BITNR
,
1172 .name
= "dirty bit",
1175 .type
= QCOW2_FEAT_TYPE_INCOMPATIBLE
,
1176 .bit
= QCOW2_INCOMPAT_CORRUPT_BITNR
,
1177 .name
= "corrupt bit",
1180 .type
= QCOW2_FEAT_TYPE_COMPATIBLE
,
1181 .bit
= QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR
,
1182 .name
= "lazy refcounts",
1186 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_FEATURE_TABLE
,
1187 features
, sizeof(features
), buflen
);
1194 /* Keep unknown header extensions */
1195 QLIST_FOREACH(uext
, &s
->unknown_header_ext
, next
) {
1196 ret
= header_ext_add(buf
, uext
->magic
, uext
->data
, uext
->len
, buflen
);
1205 /* End of header extensions */
1206 ret
= header_ext_add(buf
, QCOW2_EXT_MAGIC_END
, NULL
, 0, buflen
);
1214 /* Backing file name */
1215 if (*bs
->backing_file
) {
1216 size_t backing_file_len
= strlen(bs
->backing_file
);
1218 if (buflen
< backing_file_len
) {
1223 /* Using strncpy is ok here, since buf is not NUL-terminated. */
1224 strncpy(buf
, bs
->backing_file
, buflen
);
1226 header
->backing_file_offset
= cpu_to_be64(buf
- ((char*) header
));
1227 header
->backing_file_size
= cpu_to_be32(backing_file_len
);
1230 /* Write the new header */
1231 ret
= bdrv_pwrite(bs
->file
, 0, header
, s
->cluster_size
);
1242 static int qcow2_change_backing_file(BlockDriverState
*bs
,
1243 const char *backing_file
, const char *backing_fmt
)
1245 pstrcpy(bs
->backing_file
, sizeof(bs
->backing_file
), backing_file
?: "");
1246 pstrcpy(bs
->backing_format
, sizeof(bs
->backing_format
), backing_fmt
?: "");
1248 return qcow2_update_header(bs
);
1251 static int preallocate(BlockDriverState
*bs
)
1253 uint64_t nb_sectors
;
1255 uint64_t host_offset
= 0;
1260 nb_sectors
= bdrv_getlength(bs
) >> 9;
1263 while (nb_sectors
) {
1264 num
= MIN(nb_sectors
, INT_MAX
>> 9);
1265 ret
= qcow2_alloc_cluster_offset(bs
, offset
, 0, num
, &num
,
1266 &host_offset
, &meta
);
1271 ret
= qcow2_alloc_cluster_link_l2(bs
, meta
);
1273 qcow2_free_any_clusters(bs
, meta
->alloc_offset
, meta
->nb_clusters
,
1274 QCOW2_DISCARD_NEVER
);
1278 /* There are no dependent requests, but we need to remove our request
1279 * from the list of in-flight requests */
1281 QLIST_REMOVE(meta
, next_in_flight
);
1284 /* TODO Preallocate data if requested */
1291 * It is expected that the image file is large enough to actually contain
1292 * all of the allocated clusters (otherwise we get failing reads after
1293 * EOF). Extend the image to the last allocated sector.
1295 if (host_offset
!= 0) {
1297 memset(buf
, 0, 512);
1298 ret
= bdrv_write(bs
->file
, (host_offset
>> 9) + num
- 1, buf
, 1);
1307 static int qcow2_create2(const char *filename
, int64_t total_size
,
1308 const char *backing_file
, const char *backing_format
,
1309 int flags
, size_t cluster_size
, int prealloc
,
1310 QEMUOptionParameter
*options
, int version
)
1312 /* Calculate cluster_bits */
1314 cluster_bits
= ffs(cluster_size
) - 1;
1315 if (cluster_bits
< MIN_CLUSTER_BITS
|| cluster_bits
> MAX_CLUSTER_BITS
||
1316 (1 << cluster_bits
) != cluster_size
)
1319 "Cluster size must be a power of two between %d and %dk",
1320 1 << MIN_CLUSTER_BITS
, 1 << (MAX_CLUSTER_BITS
- 10));
1325 * Open the image file and write a minimal qcow2 header.
1327 * We keep things simple and start with a zero-sized image. We also
1328 * do without refcount blocks or a L1 table for now. We'll fix the
1329 * inconsistency later.
1331 * We do need a refcount table because growing the refcount table means
1332 * allocating two new refcount blocks - the seconds of which would be at
1333 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1334 * size for any qcow2 image.
1336 BlockDriverState
* bs
;
1338 uint8_t* refcount_table
;
1341 ret
= bdrv_create_file(filename
, options
);
1346 ret
= bdrv_file_open(&bs
, filename
, NULL
, BDRV_O_RDWR
);
1351 /* Write the header */
1352 memset(&header
, 0, sizeof(header
));
1353 header
.magic
= cpu_to_be32(QCOW_MAGIC
);
1354 header
.version
= cpu_to_be32(version
);
1355 header
.cluster_bits
= cpu_to_be32(cluster_bits
);
1356 header
.size
= cpu_to_be64(0);
1357 header
.l1_table_offset
= cpu_to_be64(0);
1358 header
.l1_size
= cpu_to_be32(0);
1359 header
.refcount_table_offset
= cpu_to_be64(cluster_size
);
1360 header
.refcount_table_clusters
= cpu_to_be32(1);
1361 header
.refcount_order
= cpu_to_be32(3 + REFCOUNT_SHIFT
);
1362 header
.header_length
= cpu_to_be32(sizeof(header
));
1364 if (flags
& BLOCK_FLAG_ENCRYPT
) {
1365 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_AES
);
1367 header
.crypt_method
= cpu_to_be32(QCOW_CRYPT_NONE
);
1370 if (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
) {
1371 header
.compatible_features
|=
1372 cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS
);
1375 ret
= bdrv_pwrite(bs
, 0, &header
, sizeof(header
));
1380 /* Write an empty refcount table */
1381 refcount_table
= g_malloc0(cluster_size
);
1382 ret
= bdrv_pwrite(bs
, cluster_size
, refcount_table
, cluster_size
);
1383 g_free(refcount_table
);
1392 * And now open the image and make it consistent first (i.e. increase the
1393 * refcount of the cluster that is occupied by the header and the refcount
1396 BlockDriver
* drv
= bdrv_find_format("qcow2");
1397 assert(drv
!= NULL
);
1398 ret
= bdrv_open(bs
, filename
, NULL
,
1399 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_NO_FLUSH
, drv
);
1404 ret
= qcow2_alloc_clusters(bs
, 2 * cluster_size
);
1408 } else if (ret
!= 0) {
1409 error_report("Huh, first cluster in empty image is already in use?");
1413 /* Okay, now that we have a valid image, let's give it the right size */
1414 ret
= bdrv_truncate(bs
, total_size
* BDRV_SECTOR_SIZE
);
1419 /* Want a backing file? There you go.*/
1421 ret
= bdrv_change_backing_file(bs
, backing_file
, backing_format
);
1427 /* And if we're supposed to preallocate metadata, do that now */
1429 BDRVQcowState
*s
= bs
->opaque
;
1430 qemu_co_mutex_lock(&s
->lock
);
1431 ret
= preallocate(bs
);
1432 qemu_co_mutex_unlock(&s
->lock
);
1444 static int qcow2_create(const char *filename
, QEMUOptionParameter
*options
)
1446 const char *backing_file
= NULL
;
1447 const char *backing_fmt
= NULL
;
1448 uint64_t sectors
= 0;
1450 size_t cluster_size
= DEFAULT_CLUSTER_SIZE
;
1454 /* Read out options */
1455 while (options
&& options
->name
) {
1456 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1457 sectors
= options
->value
.n
/ 512;
1458 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1459 backing_file
= options
->value
.s
;
1460 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FMT
)) {
1461 backing_fmt
= options
->value
.s
;
1462 } else if (!strcmp(options
->name
, BLOCK_OPT_ENCRYPT
)) {
1463 flags
|= options
->value
.n
? BLOCK_FLAG_ENCRYPT
: 0;
1464 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
1465 if (options
->value
.n
) {
1466 cluster_size
= options
->value
.n
;
1468 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1469 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1471 } else if (!strcmp(options
->value
.s
, "metadata")) {
1474 fprintf(stderr
, "Invalid preallocation mode: '%s'\n",
1478 } else if (!strcmp(options
->name
, BLOCK_OPT_COMPAT_LEVEL
)) {
1479 if (!options
->value
.s
) {
1480 /* keep the default */
1481 } else if (!strcmp(options
->value
.s
, "0.10")) {
1483 } else if (!strcmp(options
->value
.s
, "1.1")) {
1486 fprintf(stderr
, "Invalid compatibility level: '%s'\n",
1490 } else if (!strcmp(options
->name
, BLOCK_OPT_LAZY_REFCOUNTS
)) {
1491 flags
|= options
->value
.n
? BLOCK_FLAG_LAZY_REFCOUNTS
: 0;
1496 if (backing_file
&& prealloc
) {
1497 fprintf(stderr
, "Backing file and preallocation cannot be used at "
1502 if (version
< 3 && (flags
& BLOCK_FLAG_LAZY_REFCOUNTS
)) {
1503 fprintf(stderr
, "Lazy refcounts only supported with compatibility "
1504 "level 1.1 and above (use compat=1.1 or greater)\n");
1508 return qcow2_create2(filename
, sectors
, backing_file
, backing_fmt
, flags
,
1509 cluster_size
, prealloc
, options
, version
);
1512 static int qcow2_make_empty(BlockDriverState
*bs
)
1515 /* XXX: not correct */
1516 BDRVQcowState
*s
= bs
->opaque
;
1517 uint32_t l1_length
= s
->l1_size
* sizeof(uint64_t);
1520 memset(s
->l1_table
, 0, l1_length
);
1521 if (bdrv_pwrite(bs
->file
, s
->l1_table_offset
, s
->l1_table
, l1_length
) < 0)
1523 ret
= bdrv_truncate(bs
->file
, s
->l1_table_offset
+ l1_length
);
1532 static coroutine_fn
int qcow2_co_write_zeroes(BlockDriverState
*bs
,
1533 int64_t sector_num
, int nb_sectors
)
1536 BDRVQcowState
*s
= bs
->opaque
;
1538 /* Emulate misaligned zero writes */
1539 if (sector_num
% s
->cluster_sectors
|| nb_sectors
% s
->cluster_sectors
) {
1543 /* Whatever is left can use real zero clusters */
1544 qemu_co_mutex_lock(&s
->lock
);
1545 ret
= qcow2_zero_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1547 qemu_co_mutex_unlock(&s
->lock
);
1552 static coroutine_fn
int qcow2_co_discard(BlockDriverState
*bs
,
1553 int64_t sector_num
, int nb_sectors
)
1556 BDRVQcowState
*s
= bs
->opaque
;
1558 qemu_co_mutex_lock(&s
->lock
);
1559 ret
= qcow2_discard_clusters(bs
, sector_num
<< BDRV_SECTOR_BITS
,
1561 qemu_co_mutex_unlock(&s
->lock
);
1565 static int qcow2_truncate(BlockDriverState
*bs
, int64_t offset
)
1567 BDRVQcowState
*s
= bs
->opaque
;
1568 int64_t new_l1_size
;
1572 error_report("The new size must be a multiple of 512");
1576 /* cannot proceed if image has snapshots */
1577 if (s
->nb_snapshots
) {
1578 error_report("Can't resize an image which has snapshots");
1582 /* shrinking is currently not supported */
1583 if (offset
< bs
->total_sectors
* 512) {
1584 error_report("qcow2 doesn't support shrinking images yet");
1588 new_l1_size
= size_to_l1(s
, offset
);
1589 ret
= qcow2_grow_l1_table(bs
, new_l1_size
, true);
1594 /* write updated header.size */
1595 offset
= cpu_to_be64(offset
);
1596 ret
= bdrv_pwrite_sync(bs
->file
, offsetof(QCowHeader
, size
),
1597 &offset
, sizeof(uint64_t));
1602 s
->l1_vm_state_index
= new_l1_size
;
1606 /* XXX: put compressed sectors first, then all the cluster aligned
1607 tables to avoid losing bytes in alignment */
1608 static int qcow2_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1609 const uint8_t *buf
, int nb_sectors
)
1611 BDRVQcowState
*s
= bs
->opaque
;
1615 uint64_t cluster_offset
;
1617 if (nb_sectors
== 0) {
1618 /* align end of file to a sector boundary to ease reading with
1619 sector based I/Os */
1620 cluster_offset
= bdrv_getlength(bs
->file
);
1621 cluster_offset
= (cluster_offset
+ 511) & ~511;
1622 bdrv_truncate(bs
->file
, cluster_offset
);
1626 if (nb_sectors
!= s
->cluster_sectors
) {
1629 /* Zero-pad last write if image size is not cluster aligned */
1630 if (sector_num
+ nb_sectors
== bs
->total_sectors
&&
1631 nb_sectors
< s
->cluster_sectors
) {
1632 uint8_t *pad_buf
= qemu_blockalign(bs
, s
->cluster_size
);
1633 memset(pad_buf
, 0, s
->cluster_size
);
1634 memcpy(pad_buf
, buf
, nb_sectors
* BDRV_SECTOR_SIZE
);
1635 ret
= qcow2_write_compressed(bs
, sector_num
,
1636 pad_buf
, s
->cluster_sectors
);
1637 qemu_vfree(pad_buf
);
1642 out_buf
= g_malloc(s
->cluster_size
+ (s
->cluster_size
/ 1000) + 128);
1644 /* best compression, small window, no zlib header */
1645 memset(&strm
, 0, sizeof(strm
));
1646 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
,
1648 9, Z_DEFAULT_STRATEGY
);
1654 strm
.avail_in
= s
->cluster_size
;
1655 strm
.next_in
= (uint8_t *)buf
;
1656 strm
.avail_out
= s
->cluster_size
;
1657 strm
.next_out
= out_buf
;
1659 ret
= deflate(&strm
, Z_FINISH
);
1660 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
) {
1665 out_len
= strm
.next_out
- out_buf
;
1669 if (ret
!= Z_STREAM_END
|| out_len
>= s
->cluster_size
) {
1670 /* could not compress: write normal cluster */
1671 ret
= bdrv_write(bs
, sector_num
, buf
, s
->cluster_sectors
);
1676 cluster_offset
= qcow2_alloc_compressed_cluster_offset(bs
,
1677 sector_num
<< 9, out_len
);
1678 if (!cluster_offset
) {
1682 cluster_offset
&= s
->cluster_offset_mask
;
1683 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_COMPRESSED
);
1684 ret
= bdrv_pwrite(bs
->file
, cluster_offset
, out_buf
, out_len
);
1696 static coroutine_fn
int qcow2_co_flush_to_os(BlockDriverState
*bs
)
1698 BDRVQcowState
*s
= bs
->opaque
;
1701 qemu_co_mutex_lock(&s
->lock
);
1702 ret
= qcow2_cache_flush(bs
, s
->l2_table_cache
);
1704 qemu_co_mutex_unlock(&s
->lock
);
1708 if (qcow2_need_accurate_refcounts(s
)) {
1709 ret
= qcow2_cache_flush(bs
, s
->refcount_block_cache
);
1711 qemu_co_mutex_unlock(&s
->lock
);
1715 qemu_co_mutex_unlock(&s
->lock
);
1720 static int64_t qcow2_vm_state_offset(BDRVQcowState
*s
)
1722 return (int64_t)s
->l1_vm_state_index
<< (s
->cluster_bits
+ s
->l2_bits
);
1725 static int qcow2_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1727 BDRVQcowState
*s
= bs
->opaque
;
1728 bdi
->cluster_size
= s
->cluster_size
;
1729 bdi
->vm_state_offset
= qcow2_vm_state_offset(s
);
1734 static void dump_refcounts(BlockDriverState
*bs
)
1736 BDRVQcowState
*s
= bs
->opaque
;
1737 int64_t nb_clusters
, k
, k1
, size
;
1740 size
= bdrv_getlength(bs
->file
);
1741 nb_clusters
= size_to_clusters(s
, size
);
1742 for(k
= 0; k
< nb_clusters
;) {
1744 refcount
= get_refcount(bs
, k
);
1746 while (k
< nb_clusters
&& get_refcount(bs
, k
) == refcount
)
1748 printf("%" PRId64
": refcount=%d nb=%" PRId64
"\n", k
, refcount
,
1754 static int qcow2_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
1757 BDRVQcowState
*s
= bs
->opaque
;
1758 int growable
= bs
->growable
;
1761 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_SAVE
);
1763 ret
= bdrv_pwritev(bs
, qcow2_vm_state_offset(s
) + pos
, qiov
);
1764 bs
->growable
= growable
;
1769 static int qcow2_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1770 int64_t pos
, int size
)
1772 BDRVQcowState
*s
= bs
->opaque
;
1773 int growable
= bs
->growable
;
1774 bool zero_beyond_eof
= bs
->zero_beyond_eof
;
1777 BLKDBG_EVENT(bs
->file
, BLKDBG_VMSTATE_LOAD
);
1779 bs
->zero_beyond_eof
= false;
1780 ret
= bdrv_pread(bs
, qcow2_vm_state_offset(s
) + pos
, buf
, size
);
1781 bs
->growable
= growable
;
1782 bs
->zero_beyond_eof
= zero_beyond_eof
;
1787 static QEMUOptionParameter qcow2_create_options
[] = {
1789 .name
= BLOCK_OPT_SIZE
,
1791 .help
= "Virtual disk size"
1794 .name
= BLOCK_OPT_COMPAT_LEVEL
,
1796 .help
= "Compatibility level (0.10 or 1.1)"
1799 .name
= BLOCK_OPT_BACKING_FILE
,
1801 .help
= "File name of a base image"
1804 .name
= BLOCK_OPT_BACKING_FMT
,
1806 .help
= "Image format of the base image"
1809 .name
= BLOCK_OPT_ENCRYPT
,
1811 .help
= "Encrypt the image"
1814 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1816 .help
= "qcow2 cluster size",
1817 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
1820 .name
= BLOCK_OPT_PREALLOC
,
1822 .help
= "Preallocation mode (allowed values: off, metadata)"
1825 .name
= BLOCK_OPT_LAZY_REFCOUNTS
,
1827 .help
= "Postpone refcount updates",
1832 static BlockDriver bdrv_qcow2
= {
1833 .format_name
= "qcow2",
1834 .instance_size
= sizeof(BDRVQcowState
),
1835 .bdrv_probe
= qcow2_probe
,
1836 .bdrv_open
= qcow2_open
,
1837 .bdrv_close
= qcow2_close
,
1838 .bdrv_reopen_prepare
= qcow2_reopen_prepare
,
1839 .bdrv_create
= qcow2_create
,
1840 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1841 .bdrv_co_is_allocated
= qcow2_co_is_allocated
,
1842 .bdrv_set_key
= qcow2_set_key
,
1843 .bdrv_make_empty
= qcow2_make_empty
,
1845 .bdrv_co_readv
= qcow2_co_readv
,
1846 .bdrv_co_writev
= qcow2_co_writev
,
1847 .bdrv_co_flush_to_os
= qcow2_co_flush_to_os
,
1849 .bdrv_co_write_zeroes
= qcow2_co_write_zeroes
,
1850 .bdrv_co_discard
= qcow2_co_discard
,
1851 .bdrv_truncate
= qcow2_truncate
,
1852 .bdrv_write_compressed
= qcow2_write_compressed
,
1854 .bdrv_snapshot_create
= qcow2_snapshot_create
,
1855 .bdrv_snapshot_goto
= qcow2_snapshot_goto
,
1856 .bdrv_snapshot_delete
= qcow2_snapshot_delete
,
1857 .bdrv_snapshot_list
= qcow2_snapshot_list
,
1858 .bdrv_snapshot_load_tmp
= qcow2_snapshot_load_tmp
,
1859 .bdrv_get_info
= qcow2_get_info
,
1861 .bdrv_save_vmstate
= qcow2_save_vmstate
,
1862 .bdrv_load_vmstate
= qcow2_load_vmstate
,
1864 .bdrv_change_backing_file
= qcow2_change_backing_file
,
1866 .bdrv_invalidate_cache
= qcow2_invalidate_cache
,
1868 .create_options
= qcow2_create_options
,
1869 .bdrv_check
= qcow2_check
,
1872 static void bdrv_qcow2_init(void)
1874 bdrv_register(&bdrv_qcow2
);
1877 block_init(bdrv_qcow2_init
);