2 * Bitmaps for the QCOW version 2 format
4 * Copyright (c) 2014-2017 Vladimir Sementsov-Ogievskiy
6 * This file is derived from qcow2-snapshot.c, original copyright:
7 * Copyright (c) 2004-2006 Fabrice Bellard
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu/cutils.h"
32 #include "block/block_int.h"
35 /* NOTICE: BME here means Bitmaps Extension and used as a namespace for
36 * _internal_ constants. Please do not use this _internal_ abbreviation for
37 * other needs and/or outside of this file. */
39 /* Bitmap directory entry constraints */
40 #define BME_MAX_TABLE_SIZE 0x8000000
41 #define BME_MAX_PHYS_SIZE 0x20000000 /* restrict BdrvDirtyBitmap size in RAM */
42 #define BME_MAX_GRANULARITY_BITS 31
43 #define BME_MIN_GRANULARITY_BITS 9
44 #define BME_MAX_NAME_SIZE 1023
46 #if BME_MAX_TABLE_SIZE * 8ULL > INT_MAX
47 #error In the code bitmap table physical size assumed to fit into int
50 /* Bitmap directory entry flags */
51 #define BME_RESERVED_FLAGS 0xfffffffcU
52 #define BME_FLAG_IN_USE (1U << 0)
53 #define BME_FLAG_AUTO (1U << 1)
55 /* bits [1, 8] U [56, 63] are reserved */
56 #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001feULL
57 #define BME_TABLE_ENTRY_OFFSET_MASK 0x00fffffffffffe00ULL
58 #define BME_TABLE_ENTRY_FLAG_ALL_ONES (1ULL << 0)
60 typedef struct QEMU_PACKED Qcow2BitmapDirEntry
{
61 /* header is 8 byte aligned */
62 uint64_t bitmap_table_offset
;
64 uint32_t bitmap_table_size
;
68 uint8_t granularity_bits
;
70 uint32_t extra_data_size
;
71 /* extra data follows */
73 } Qcow2BitmapDirEntry
;
75 typedef struct Qcow2BitmapTable
{
77 uint32_t size
; /* number of 64bit entries */
78 QSIMPLEQ_ENTRY(Qcow2BitmapTable
) entry
;
81 typedef struct Qcow2Bitmap
{
82 Qcow2BitmapTable table
;
84 uint8_t granularity_bits
;
87 BdrvDirtyBitmap
*dirty_bitmap
;
89 QSIMPLEQ_ENTRY(Qcow2Bitmap
) entry
;
91 typedef QSIMPLEQ_HEAD(Qcow2BitmapList
, Qcow2Bitmap
) Qcow2BitmapList
;
93 typedef enum BitmapType
{
94 BT_DIRTY_TRACKING_BITMAP
= 1
97 static inline bool can_write(BlockDriverState
*bs
)
99 return !bdrv_is_read_only(bs
) && !(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
);
102 static int update_header_sync(BlockDriverState
*bs
)
106 ret
= qcow2_update_header(bs
);
111 return bdrv_flush(bs
->file
->bs
);
114 static inline void bitmap_table_to_be(uint64_t *bitmap_table
, size_t size
)
118 for (i
= 0; i
< size
; ++i
) {
119 bitmap_table
[i
] = cpu_to_be64(bitmap_table
[i
]);
123 static int check_table_entry(uint64_t entry
, int cluster_size
)
127 if (entry
& BME_TABLE_ENTRY_RESERVED_MASK
) {
131 offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
133 /* if offset specified, bit 0 is reserved */
134 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
138 if (offset
% cluster_size
!= 0) {
146 static int check_constraints_on_bitmap(BlockDriverState
*bs
,
148 uint32_t granularity
,
151 BDRVQcow2State
*s
= bs
->opaque
;
152 int granularity_bits
= ctz32(granularity
);
153 int64_t len
= bdrv_getlength(bs
);
155 assert(granularity
> 0);
156 assert((granularity
& (granularity
- 1)) == 0);
159 error_setg_errno(errp
, -len
, "Failed to get size of '%s'",
160 bdrv_get_device_or_node_name(bs
));
164 if (granularity_bits
> BME_MAX_GRANULARITY_BITS
) {
165 error_setg(errp
, "Granularity exceeds maximum (%llu bytes)",
166 1ULL << BME_MAX_GRANULARITY_BITS
);
169 if (granularity_bits
< BME_MIN_GRANULARITY_BITS
) {
170 error_setg(errp
, "Granularity is under minimum (%llu bytes)",
171 1ULL << BME_MIN_GRANULARITY_BITS
);
175 if ((len
> (uint64_t)BME_MAX_PHYS_SIZE
<< granularity_bits
) ||
176 (len
> (uint64_t)BME_MAX_TABLE_SIZE
* s
->cluster_size
<<
179 error_setg(errp
, "Too much space will be occupied by the bitmap. "
180 "Use larger granularity");
184 if (strlen(name
) > BME_MAX_NAME_SIZE
) {
185 error_setg(errp
, "Name length exceeds maximum (%u characters)",
193 static void clear_bitmap_table(BlockDriverState
*bs
, uint64_t *bitmap_table
,
194 uint32_t bitmap_table_size
)
196 BDRVQcow2State
*s
= bs
->opaque
;
199 for (i
= 0; i
< bitmap_table_size
; ++i
) {
200 uint64_t addr
= bitmap_table
[i
] & BME_TABLE_ENTRY_OFFSET_MASK
;
205 qcow2_free_clusters(bs
, addr
, s
->cluster_size
, QCOW2_DISCARD_OTHER
);
210 static int bitmap_table_load(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
,
211 uint64_t **bitmap_table
)
214 BDRVQcow2State
*s
= bs
->opaque
;
218 assert(tb
->size
!= 0);
219 table
= g_try_new(uint64_t, tb
->size
);
224 assert(tb
->size
<= BME_MAX_TABLE_SIZE
);
225 ret
= bdrv_pread(bs
->file
, tb
->offset
,
226 table
, tb
->size
* sizeof(uint64_t));
231 for (i
= 0; i
< tb
->size
; ++i
) {
232 table
[i
] = be64_to_cpu(table
[i
]);
233 ret
= check_table_entry(table
[i
], s
->cluster_size
);
239 *bitmap_table
= table
;
248 static int free_bitmap_clusters(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
)
251 uint64_t *bitmap_table
;
253 ret
= bitmap_table_load(bs
, tb
, &bitmap_table
);
258 clear_bitmap_table(bs
, bitmap_table
, tb
->size
);
259 qcow2_free_clusters(bs
, tb
->offset
, tb
->size
* sizeof(uint64_t),
260 QCOW2_DISCARD_OTHER
);
261 g_free(bitmap_table
);
269 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
270 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State
*s
,
271 const BdrvDirtyBitmap
*bitmap
)
273 uint64_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
274 uint64_t limit
= granularity
* (s
->cluster_size
<< 3);
276 assert(QEMU_IS_ALIGNED(limit
,
277 bdrv_dirty_bitmap_serialization_align(bitmap
)));
282 * @bitmap_table entries must satisfy specification constraints.
283 * @bitmap must be cleared */
284 static int load_bitmap_data(BlockDriverState
*bs
,
285 const uint64_t *bitmap_table
,
286 uint32_t bitmap_table_size
,
287 BdrvDirtyBitmap
*bitmap
)
290 BDRVQcow2State
*s
= bs
->opaque
;
291 uint64_t offset
, limit
;
292 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
294 uint64_t i
, tab_size
=
296 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
298 if (tab_size
!= bitmap_table_size
|| tab_size
> BME_MAX_TABLE_SIZE
) {
302 buf
= g_malloc(s
->cluster_size
);
303 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
304 for (i
= 0, offset
= 0; i
< tab_size
; ++i
, offset
+= limit
) {
305 uint64_t count
= MIN(bm_size
- offset
, limit
);
306 uint64_t entry
= bitmap_table
[i
];
307 uint64_t data_offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
309 assert(check_table_entry(entry
, s
->cluster_size
) == 0);
311 if (data_offset
== 0) {
312 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
313 bdrv_dirty_bitmap_deserialize_ones(bitmap
, offset
, count
,
316 /* No need to deserialize zeros because the dirty bitmap is
320 ret
= bdrv_pread(bs
->file
, data_offset
, buf
, s
->cluster_size
);
324 bdrv_dirty_bitmap_deserialize_part(bitmap
, buf
, offset
, count
,
330 bdrv_dirty_bitmap_deserialize_finish(bitmap
);
338 static BdrvDirtyBitmap
*load_bitmap(BlockDriverState
*bs
,
339 Qcow2Bitmap
*bm
, Error
**errp
)
342 uint64_t *bitmap_table
= NULL
;
343 uint32_t granularity
;
344 BdrvDirtyBitmap
*bitmap
= NULL
;
346 if (bm
->flags
& BME_FLAG_IN_USE
) {
347 error_setg(errp
, "Bitmap '%s' is in use", bm
->name
);
351 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
353 error_setg_errno(errp
, -ret
,
354 "Could not read bitmap_table table from image for "
355 "bitmap '%s'", bm
->name
);
359 granularity
= 1U << bm
->granularity_bits
;
360 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, bm
->name
, errp
);
361 if (bitmap
== NULL
) {
365 ret
= load_bitmap_data(bs
, bitmap_table
, bm
->table
.size
, bitmap
);
367 error_setg_errno(errp
, -ret
, "Could not read bitmap '%s' from image",
372 g_free(bitmap_table
);
376 g_free(bitmap_table
);
377 if (bitmap
!= NULL
) {
378 bdrv_release_dirty_bitmap(bs
, bitmap
);
389 * Bitmap List private functions
390 * Only Bitmap List knows about bitmap directory structure in Qcow2.
393 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry
*entry
)
395 entry
->bitmap_table_offset
= be64_to_cpu(entry
->bitmap_table_offset
);
396 entry
->bitmap_table_size
= be32_to_cpu(entry
->bitmap_table_size
);
397 entry
->flags
= be32_to_cpu(entry
->flags
);
398 entry
->name_size
= be16_to_cpu(entry
->name_size
);
399 entry
->extra_data_size
= be32_to_cpu(entry
->extra_data_size
);
402 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry
*entry
)
404 entry
->bitmap_table_offset
= cpu_to_be64(entry
->bitmap_table_offset
);
405 entry
->bitmap_table_size
= cpu_to_be32(entry
->bitmap_table_size
);
406 entry
->flags
= cpu_to_be32(entry
->flags
);
407 entry
->name_size
= cpu_to_be16(entry
->name_size
);
408 entry
->extra_data_size
= cpu_to_be32(entry
->extra_data_size
);
411 static inline int calc_dir_entry_size(size_t name_size
, size_t extra_data_size
)
413 int size
= sizeof(Qcow2BitmapDirEntry
) + name_size
+ extra_data_size
;
414 return ROUND_UP(size
, 8);
417 static inline int dir_entry_size(Qcow2BitmapDirEntry
*entry
)
419 return calc_dir_entry_size(entry
->name_size
, entry
->extra_data_size
);
422 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry
*entry
)
424 return (const char *)(entry
+ 1) + entry
->extra_data_size
;
427 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry
*entry
)
429 const char *name_field
= dir_entry_name_field(entry
);
430 return g_strndup(name_field
, entry
->name_size
);
433 static inline Qcow2BitmapDirEntry
*next_dir_entry(Qcow2BitmapDirEntry
*entry
)
435 return (Qcow2BitmapDirEntry
*)((uint8_t *)entry
+ dir_entry_size(entry
));
438 static int check_dir_entry(BlockDriverState
*bs
, Qcow2BitmapDirEntry
*entry
)
440 BDRVQcow2State
*s
= bs
->opaque
;
441 uint64_t phys_bitmap_bytes
;
444 bool fail
= (entry
->bitmap_table_size
== 0) ||
445 (entry
->bitmap_table_offset
== 0) ||
446 (entry
->bitmap_table_offset
% s
->cluster_size
) ||
447 (entry
->bitmap_table_size
> BME_MAX_TABLE_SIZE
) ||
448 (entry
->granularity_bits
> BME_MAX_GRANULARITY_BITS
) ||
449 (entry
->granularity_bits
< BME_MIN_GRANULARITY_BITS
) ||
450 (entry
->flags
& BME_RESERVED_FLAGS
) ||
451 (entry
->name_size
> BME_MAX_NAME_SIZE
) ||
452 (entry
->type
!= BT_DIRTY_TRACKING_BITMAP
);
458 phys_bitmap_bytes
= (uint64_t)entry
->bitmap_table_size
* s
->cluster_size
;
459 len
= bdrv_getlength(bs
);
465 fail
= (phys_bitmap_bytes
> BME_MAX_PHYS_SIZE
) ||
466 (len
> ((phys_bitmap_bytes
* 8) << entry
->granularity_bits
));
468 return fail
? -EINVAL
: 0;
471 static inline void bitmap_directory_to_be(uint8_t *dir
, size_t size
)
473 uint8_t *end
= dir
+ size
;
475 Qcow2BitmapDirEntry
*e
= (Qcow2BitmapDirEntry
*)dir
;
476 dir
+= dir_entry_size(e
);
478 bitmap_dir_entry_to_be(e
);
483 * Bitmap List public functions
486 static void bitmap_free(Qcow2Bitmap
*bm
)
496 static void bitmap_list_free(Qcow2BitmapList
*bm_list
)
500 if (bm_list
== NULL
) {
504 while ((bm
= QSIMPLEQ_FIRST(bm_list
)) != NULL
) {
505 QSIMPLEQ_REMOVE_HEAD(bm_list
, entry
);
512 static Qcow2BitmapList
*bitmap_list_new(void)
514 Qcow2BitmapList
*bm_list
= g_new(Qcow2BitmapList
, 1);
515 QSIMPLEQ_INIT(bm_list
);
520 static uint32_t bitmap_list_count(Qcow2BitmapList
*bm_list
)
523 uint32_t nb_bitmaps
= 0;
525 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
533 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
534 * checks it and convert to bitmap list.
536 static Qcow2BitmapList
*bitmap_list_load(BlockDriverState
*bs
, uint64_t offset
,
537 uint64_t size
, Error
**errp
)
540 BDRVQcow2State
*s
= bs
->opaque
;
541 uint8_t *dir
, *dir_end
;
542 Qcow2BitmapDirEntry
*e
;
543 uint32_t nb_dir_entries
= 0;
544 Qcow2BitmapList
*bm_list
= NULL
;
547 error_setg(errp
, "Requested bitmap directory size is zero");
551 if (size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
552 error_setg(errp
, "Requested bitmap directory size is too big");
556 dir
= g_try_malloc(size
);
558 error_setg(errp
, "Failed to allocate space for bitmap directory");
561 dir_end
= dir
+ size
;
563 ret
= bdrv_pread(bs
->file
, offset
, dir
, size
);
565 error_setg_errno(errp
, -ret
, "Failed to read bitmap directory");
569 bm_list
= bitmap_list_new();
570 for (e
= (Qcow2BitmapDirEntry
*)dir
;
571 e
< (Qcow2BitmapDirEntry
*)dir_end
;
572 e
= next_dir_entry(e
))
576 if ((uint8_t *)(e
+ 1) > dir_end
) {
580 if (++nb_dir_entries
> s
->nb_bitmaps
) {
581 error_setg(errp
, "More bitmaps found than specified in header"
585 bitmap_dir_entry_to_cpu(e
);
587 if ((uint8_t *)next_dir_entry(e
) > dir_end
) {
591 if (e
->extra_data_size
!= 0) {
592 error_setg(errp
, "Bitmap extra data is not supported");
596 ret
= check_dir_entry(bs
, e
);
598 error_setg(errp
, "Bitmap '%.*s' doesn't satisfy the constraints",
599 e
->name_size
, dir_entry_name_field(e
));
603 bm
= g_new0(Qcow2Bitmap
, 1);
604 bm
->table
.offset
= e
->bitmap_table_offset
;
605 bm
->table
.size
= e
->bitmap_table_size
;
606 bm
->flags
= e
->flags
;
607 bm
->granularity_bits
= e
->granularity_bits
;
608 bm
->name
= dir_entry_copy_name(e
);
609 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
612 if (nb_dir_entries
!= s
->nb_bitmaps
) {
613 error_setg(errp
, "Less bitmaps found than specified in header"
618 if ((uint8_t *)e
!= dir_end
) {
627 error_setg(errp
, "Broken bitmap directory");
631 bitmap_list_free(bm_list
);
636 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
637 void **refcount_table
,
638 int64_t *refcount_table_size
)
641 BDRVQcow2State
*s
= bs
->opaque
;
642 Qcow2BitmapList
*bm_list
;
645 if (s
->nb_bitmaps
== 0) {
649 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
650 s
->bitmap_directory_offset
,
651 s
->bitmap_directory_size
);
656 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
657 s
->bitmap_directory_size
, NULL
);
658 if (bm_list
== NULL
) {
663 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
664 uint64_t *bitmap_table
= NULL
;
667 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
668 refcount_table
, refcount_table_size
,
670 bm
->table
.size
* sizeof(uint64_t));
675 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
681 for (i
= 0; i
< bm
->table
.size
; ++i
) {
682 uint64_t entry
= bitmap_table
[i
];
683 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
685 if (check_table_entry(entry
, s
->cluster_size
) < 0) {
694 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
695 refcount_table
, refcount_table_size
,
696 offset
, s
->cluster_size
);
698 g_free(bitmap_table
);
703 g_free(bitmap_table
);
707 bitmap_list_free(bm_list
);
713 * Store bitmap list to qcow2 image as a bitmap directory.
714 * Everything is checked.
716 static int bitmap_list_store(BlockDriverState
*bs
, Qcow2BitmapList
*bm_list
,
717 uint64_t *offset
, uint64_t *size
, bool in_place
)
721 int64_t dir_offset
= 0;
722 uint64_t dir_size
= 0;
724 Qcow2BitmapDirEntry
*e
;
726 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
727 dir_size
+= calc_dir_entry_size(strlen(bm
->name
), 0);
730 if (dir_size
== 0 || dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
735 if (*size
!= dir_size
|| *offset
== 0) {
739 dir_offset
= *offset
;
742 dir
= g_try_malloc(dir_size
);
747 e
= (Qcow2BitmapDirEntry
*)dir
;
748 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
749 e
->bitmap_table_offset
= bm
->table
.offset
;
750 e
->bitmap_table_size
= bm
->table
.size
;
751 e
->flags
= bm
->flags
;
752 e
->type
= BT_DIRTY_TRACKING_BITMAP
;
753 e
->granularity_bits
= bm
->granularity_bits
;
754 e
->name_size
= strlen(bm
->name
);
755 e
->extra_data_size
= 0;
756 memcpy(e
+ 1, bm
->name
, e
->name_size
);
758 if (check_dir_entry(bs
, e
) < 0) {
763 e
= next_dir_entry(e
);
766 bitmap_directory_to_be(dir
, dir_size
);
769 dir_offset
= qcow2_alloc_clusters(bs
, dir_size
);
770 if (dir_offset
< 0) {
776 /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
777 * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
778 * directory in-place (actually, turn-off the extension), which is checked
779 * in qcow2_check_metadata_overlap() */
780 ret
= qcow2_pre_write_overlap_check(
781 bs
, in_place
? QCOW2_OL_BITMAP_DIRECTORY
: 0, dir_offset
, dir_size
,
787 ret
= bdrv_pwrite(bs
->file
, dir_offset
, dir
, dir_size
);
796 *offset
= dir_offset
;
804 if (!in_place
&& dir_offset
> 0) {
805 qcow2_free_clusters(bs
, dir_offset
, dir_size
, QCOW2_DISCARD_OTHER
);
815 static int update_ext_header_and_dir_in_place(BlockDriverState
*bs
,
816 Qcow2BitmapList
*bm_list
)
818 BDRVQcow2State
*s
= bs
->opaque
;
821 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
) ||
822 bm_list
== NULL
|| QSIMPLEQ_EMPTY(bm_list
) ||
823 bitmap_list_count(bm_list
) != s
->nb_bitmaps
)
828 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
829 ret
= update_header_sync(bs
);
831 /* Two variants are possible here:
832 * 1. Autoclear flag is dropped, all bitmaps will be lost.
833 * 2. Autoclear flag is not dropped, old state is left.
838 /* autoclear bit is not set, so we can safely update bitmap directory */
840 ret
= bitmap_list_store(bs
, bm_list
, &s
->bitmap_directory_offset
,
841 &s
->bitmap_directory_size
, true);
843 /* autoclear bit is cleared, so all leaked clusters would be removed on
848 ret
= update_header_sync(bs
);
850 /* autoclear bit is cleared, so all leaked clusters would be removed on
855 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
856 return update_header_sync(bs
);
857 /* If final update_header_sync() fails, two variants are possible:
858 * 1. Autoclear flag is not set, all bitmaps will be lost.
859 * 2. Autoclear flag is set, header and directory are successfully updated.
863 static int update_ext_header_and_dir(BlockDriverState
*bs
,
864 Qcow2BitmapList
*bm_list
)
866 BDRVQcow2State
*s
= bs
->opaque
;
868 uint64_t new_offset
= 0;
869 uint64_t new_size
= 0;
870 uint32_t new_nb_bitmaps
= 0;
871 uint64_t old_offset
= s
->bitmap_directory_offset
;
872 uint64_t old_size
= s
->bitmap_directory_size
;
873 uint32_t old_nb_bitmaps
= s
->nb_bitmaps
;
874 uint64_t old_autocl
= s
->autoclear_features
;
876 if (bm_list
!= NULL
&& !QSIMPLEQ_EMPTY(bm_list
)) {
877 new_nb_bitmaps
= bitmap_list_count(bm_list
);
879 if (new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
883 ret
= bitmap_list_store(bs
, bm_list
, &new_offset
, &new_size
, false);
888 ret
= qcow2_flush_caches(bs
);
893 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
895 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
898 s
->bitmap_directory_offset
= new_offset
;
899 s
->bitmap_directory_size
= new_size
;
900 s
->nb_bitmaps
= new_nb_bitmaps
;
902 ret
= update_header_sync(bs
);
908 qcow2_free_clusters(bs
, old_offset
, old_size
, QCOW2_DISCARD_OTHER
);
914 if (new_offset
> 0) {
915 qcow2_free_clusters(bs
, new_offset
, new_size
, QCOW2_DISCARD_OTHER
);
918 s
->bitmap_directory_offset
= old_offset
;
919 s
->bitmap_directory_size
= old_size
;
920 s
->nb_bitmaps
= old_nb_bitmaps
;
921 s
->autoclear_features
= old_autocl
;
926 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
927 static void release_dirty_bitmap_helper(gpointer bitmap
,
930 bdrv_release_dirty_bitmap(bs
, bitmap
);
933 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
934 static void set_readonly_helper(gpointer bitmap
, gpointer value
)
936 bdrv_dirty_bitmap_set_readonly(bitmap
, (bool)value
);
939 /* qcow2_load_dirty_bitmaps()
940 * Return value is a hint for caller: true means that the Qcow2 header was
941 * updated. (false doesn't mean that the header should be updated by the
942 * caller, it just means that updating was not needed or the image cannot be
944 * On failure the function returns false.
946 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
948 BDRVQcow2State
*s
= bs
->opaque
;
949 Qcow2BitmapList
*bm_list
;
951 GSList
*created_dirty_bitmaps
= NULL
;
952 bool header_updated
= false;
954 if (s
->nb_bitmaps
== 0) {
955 /* No bitmaps - nothing to do */
959 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
960 s
->bitmap_directory_size
, errp
);
961 if (bm_list
== NULL
) {
965 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
966 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
967 BdrvDirtyBitmap
*bitmap
= load_bitmap(bs
, bm
, errp
);
968 if (bitmap
== NULL
) {
972 if (!(bm
->flags
& BME_FLAG_AUTO
)) {
973 bdrv_disable_dirty_bitmap(bitmap
);
975 bdrv_dirty_bitmap_set_persistance(bitmap
, true);
976 bm
->flags
|= BME_FLAG_IN_USE
;
977 created_dirty_bitmaps
=
978 g_slist_append(created_dirty_bitmaps
, bitmap
);
982 if (created_dirty_bitmaps
!= NULL
) {
984 /* in_use flags must be updated */
985 int ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
987 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
990 header_updated
= true;
992 g_slist_foreach(created_dirty_bitmaps
, set_readonly_helper
,
997 g_slist_free(created_dirty_bitmaps
);
998 bitmap_list_free(bm_list
);
1000 return header_updated
;
1003 g_slist_foreach(created_dirty_bitmaps
, release_dirty_bitmap_helper
, bs
);
1004 g_slist_free(created_dirty_bitmaps
);
1005 bitmap_list_free(bm_list
);
1011 static Qcow2BitmapInfoFlagsList
*get_bitmap_info_flags(uint32_t flags
)
1013 Qcow2BitmapInfoFlagsList
*list
= NULL
;
1014 Qcow2BitmapInfoFlagsList
**plist
= &list
;
1017 static const struct {
1018 int bme
; /* Bitmap directory entry flags */
1019 int info
; /* The flags to report to the user */
1021 { BME_FLAG_IN_USE
, QCOW2_BITMAP_INFO_FLAGS_IN_USE
},
1022 { BME_FLAG_AUTO
, QCOW2_BITMAP_INFO_FLAGS_AUTO
},
1025 int map_size
= ARRAY_SIZE(map
);
1027 for (i
= 0; i
< map_size
; ++i
) {
1028 if (flags
& map
[i
].bme
) {
1029 Qcow2BitmapInfoFlagsList
*entry
=
1030 g_new0(Qcow2BitmapInfoFlagsList
, 1);
1031 entry
->value
= map
[i
].info
;
1033 plist
= &entry
->next
;
1034 flags
&= ~map
[i
].bme
;
1037 /* Check if the BME_* mapping above is complete */
1044 * qcow2_get_bitmap_info_list()
1045 * Returns a list of QCOW2 bitmap details.
1046 * In case of no bitmaps, the function returns NULL and
1047 * the @errp parameter is not set.
1048 * When bitmap information can not be obtained, the function returns
1049 * NULL and the @errp parameter is set.
1051 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
1054 BDRVQcow2State
*s
= bs
->opaque
;
1055 Qcow2BitmapList
*bm_list
;
1057 Qcow2BitmapInfoList
*list
= NULL
;
1058 Qcow2BitmapInfoList
**plist
= &list
;
1060 if (s
->nb_bitmaps
== 0) {
1064 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1065 s
->bitmap_directory_size
, errp
);
1066 if (bm_list
== NULL
) {
1070 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1071 Qcow2BitmapInfo
*info
= g_new0(Qcow2BitmapInfo
, 1);
1072 Qcow2BitmapInfoList
*obj
= g_new0(Qcow2BitmapInfoList
, 1);
1073 info
->granularity
= 1U << bm
->granularity_bits
;
1074 info
->name
= g_strdup(bm
->name
);
1075 info
->flags
= get_bitmap_info_flags(bm
->flags
& ~BME_RESERVED_FLAGS
);
1081 bitmap_list_free(bm_list
);
1086 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState
*bs
, bool *header_updated
,
1089 BDRVQcow2State
*s
= bs
->opaque
;
1090 Qcow2BitmapList
*bm_list
;
1092 GSList
*ro_dirty_bitmaps
= NULL
;
1095 if (header_updated
!= NULL
) {
1096 *header_updated
= false;
1099 if (s
->nb_bitmaps
== 0) {
1100 /* No bitmaps - nothing to do */
1104 if (!can_write(bs
)) {
1105 error_setg(errp
, "Can't write to the image on reopening bitmaps rw");
1109 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1110 s
->bitmap_directory_size
, errp
);
1111 if (bm_list
== NULL
) {
1115 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1116 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1117 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1118 if (bitmap
== NULL
) {
1122 if (!bdrv_dirty_bitmap_readonly(bitmap
)) {
1123 error_setg(errp
, "Bitmap %s is not readonly but not marked"
1124 "'IN_USE' in the image. Something went wrong,"
1125 "all the bitmaps may be corrupted", bm
->name
);
1130 bm
->flags
|= BME_FLAG_IN_USE
;
1131 ro_dirty_bitmaps
= g_slist_append(ro_dirty_bitmaps
, bitmap
);
1135 if (ro_dirty_bitmaps
!= NULL
) {
1136 /* in_use flags must be updated */
1137 ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1139 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
1142 if (header_updated
!= NULL
) {
1143 *header_updated
= true;
1145 g_slist_foreach(ro_dirty_bitmaps
, set_readonly_helper
, false);
1149 g_slist_free(ro_dirty_bitmaps
);
1150 bitmap_list_free(bm_list
);
1155 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
)
1157 return qcow2_reopen_bitmaps_rw_hint(bs
, NULL
, errp
);
1160 /* store_bitmap_data()
1161 * Store bitmap to image, filling bitmap table accordingly.
1163 static uint64_t *store_bitmap_data(BlockDriverState
*bs
,
1164 BdrvDirtyBitmap
*bitmap
,
1165 uint32_t *bitmap_table_size
, Error
**errp
)
1168 BDRVQcow2State
*s
= bs
->opaque
;
1171 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
1172 const char *bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1173 uint8_t *buf
= NULL
;
1174 BdrvDirtyBitmapIter
*dbi
;
1178 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
1180 if (tb_size
> BME_MAX_TABLE_SIZE
||
1181 tb_size
* s
->cluster_size
> BME_MAX_PHYS_SIZE
)
1183 error_setg(errp
, "Bitmap '%s' is too big", bm_name
);
1187 tb
= g_try_new0(uint64_t, tb_size
);
1189 error_setg(errp
, "No memory");
1193 dbi
= bdrv_dirty_iter_new(bitmap
);
1194 buf
= g_malloc(s
->cluster_size
);
1195 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
1196 assert(DIV_ROUND_UP(bm_size
, limit
) == tb_size
);
1198 while ((offset
= bdrv_dirty_iter_next(dbi
)) >= 0) {
1199 uint64_t cluster
= offset
/ limit
;
1200 uint64_t end
, write_size
;
1204 * We found the first dirty offset, but want to write out the
1205 * entire cluster of the bitmap that includes that offset,
1206 * including any leading zero bits.
1208 offset
= QEMU_ALIGN_DOWN(offset
, limit
);
1209 end
= MIN(bm_size
, offset
+ limit
);
1210 write_size
= bdrv_dirty_bitmap_serialization_size(bitmap
, offset
,
1212 assert(write_size
<= s
->cluster_size
);
1214 off
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1216 error_setg_errno(errp
, -off
,
1217 "Failed to allocate clusters for bitmap '%s'",
1223 bdrv_dirty_bitmap_serialize_part(bitmap
, buf
, offset
, end
- offset
);
1224 if (write_size
< s
->cluster_size
) {
1225 memset(buf
+ write_size
, 0, s
->cluster_size
- write_size
);
1228 ret
= qcow2_pre_write_overlap_check(bs
, 0, off
, s
->cluster_size
, false);
1230 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1234 ret
= bdrv_pwrite(bs
->file
, off
, buf
, s
->cluster_size
);
1236 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1241 if (end
>= bm_size
) {
1245 bdrv_set_dirty_iter(dbi
, end
);
1248 *bitmap_table_size
= tb_size
;
1250 bdrv_dirty_iter_free(dbi
);
1255 clear_bitmap_table(bs
, tb
, tb_size
);
1257 bdrv_dirty_iter_free(dbi
);
1264 * Store bm->dirty_bitmap to qcow2.
1265 * Set bm->table_offset and bm->table_size accordingly.
1267 static int store_bitmap(BlockDriverState
*bs
, Qcow2Bitmap
*bm
, Error
**errp
)
1273 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1274 const char *bm_name
;
1276 assert(bitmap
!= NULL
);
1278 bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1280 tb
= store_bitmap_data(bs
, bitmap
, &tb_size
, errp
);
1285 assert(tb_size
<= BME_MAX_TABLE_SIZE
);
1286 tb_offset
= qcow2_alloc_clusters(bs
, tb_size
* sizeof(tb
[0]));
1287 if (tb_offset
< 0) {
1288 error_setg_errno(errp
, -tb_offset
,
1289 "Failed to allocate clusters for bitmap '%s'",
1295 ret
= qcow2_pre_write_overlap_check(bs
, 0, tb_offset
,
1296 tb_size
* sizeof(tb
[0]), false);
1298 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1302 bitmap_table_to_be(tb
, tb_size
);
1303 ret
= bdrv_pwrite(bs
->file
, tb_offset
, tb
, tb_size
* sizeof(tb
[0]));
1305 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1312 bm
->table
.offset
= tb_offset
;
1313 bm
->table
.size
= tb_size
;
1318 clear_bitmap_table(bs
, tb
, tb_size
);
1320 if (tb_offset
> 0) {
1321 qcow2_free_clusters(bs
, tb_offset
, tb_size
* sizeof(tb
[0]),
1322 QCOW2_DISCARD_OTHER
);
1330 static Qcow2Bitmap
*find_bitmap_by_name(Qcow2BitmapList
*bm_list
,
1335 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1336 if (strcmp(name
, bm
->name
) == 0) {
1344 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
1349 BDRVQcow2State
*s
= bs
->opaque
;
1351 Qcow2BitmapList
*bm_list
;
1353 if (s
->nb_bitmaps
== 0) {
1354 /* Absence of the bitmap is not an error: see explanation above
1355 * bdrv_remove_persistent_dirty_bitmap() definition. */
1359 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1360 s
->bitmap_directory_size
, errp
);
1361 if (bm_list
== NULL
) {
1365 bm
= find_bitmap_by_name(bm_list
, name
);
1370 QSIMPLEQ_REMOVE(bm_list
, bm
, Qcow2Bitmap
, entry
);
1372 ret
= update_ext_header_and_dir(bs
, bm_list
);
1374 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1378 free_bitmap_clusters(bs
, &bm
->table
);
1382 bitmap_list_free(bm_list
);
1385 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
1387 BdrvDirtyBitmap
*bitmap
;
1388 BDRVQcow2State
*s
= bs
->opaque
;
1389 uint32_t new_nb_bitmaps
= s
->nb_bitmaps
;
1390 uint64_t new_dir_size
= s
->bitmap_directory_size
;
1392 Qcow2BitmapList
*bm_list
;
1394 QSIMPLEQ_HEAD(, Qcow2BitmapTable
) drop_tables
;
1395 Qcow2BitmapTable
*tb
, *tb_next
;
1397 if (!bdrv_has_changed_persistent_bitmaps(bs
)) {
1402 if (!can_write(bs
)) {
1403 error_setg(errp
, "No write access");
1407 QSIMPLEQ_INIT(&drop_tables
);
1409 if (s
->nb_bitmaps
== 0) {
1410 bm_list
= bitmap_list_new();
1412 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1413 s
->bitmap_directory_size
, errp
);
1414 if (bm_list
== NULL
) {
1419 /* check constraints and names */
1420 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1421 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1423 const char *name
= bdrv_dirty_bitmap_name(bitmap
);
1424 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
1427 if (!bdrv_dirty_bitmap_get_persistance(bitmap
) ||
1428 bdrv_dirty_bitmap_readonly(bitmap
))
1433 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) < 0) {
1434 error_prepend(errp
, "Bitmap '%s' doesn't satisfy the constraints: ",
1439 bm
= find_bitmap_by_name(bm_list
, name
);
1441 if (++new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1442 error_setg(errp
, "Too many persistent bitmaps");
1446 new_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1447 if (new_dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1448 error_setg(errp
, "Bitmap directory is too large");
1452 bm
= g_new0(Qcow2Bitmap
, 1);
1453 bm
->name
= g_strdup(name
);
1454 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
1456 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1457 error_setg(errp
, "Bitmap '%s' already exists in the image",
1461 tb
= g_memdup(&bm
->table
, sizeof(bm
->table
));
1462 bm
->table
.offset
= 0;
1464 QSIMPLEQ_INSERT_TAIL(&drop_tables
, tb
, entry
);
1466 bm
->flags
= bdrv_dirty_bitmap_enabled(bitmap
) ? BME_FLAG_AUTO
: 0;
1467 bm
->granularity_bits
= ctz32(bdrv_dirty_bitmap_granularity(bitmap
));
1468 bm
->dirty_bitmap
= bitmap
;
1471 /* allocate clusters and store bitmaps */
1472 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1473 if (bm
->dirty_bitmap
== NULL
) {
1477 ret
= store_bitmap(bs
, bm
, errp
);
1483 ret
= update_ext_header_and_dir(bs
, bm_list
);
1485 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1489 /* Bitmap directory was successfully updated, so, old data can be dropped.
1490 * TODO it is better to reuse these clusters */
1491 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1492 free_bitmap_clusters(bs
, tb
);
1496 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1497 /* For safety, we remove bitmap after storing.
1498 * We may be here in two cases:
1499 * 1. bdrv_close. It's ok to drop bitmap.
1500 * 2. inactivation. It means migration without 'dirty-bitmaps'
1501 * capability, so bitmaps are not marked with
1502 * BdrvDirtyBitmap.migration flags. It's not bad to drop them too,
1503 * and reload on invalidation.
1505 if (bm
->dirty_bitmap
== NULL
) {
1509 bdrv_release_dirty_bitmap(bs
, bm
->dirty_bitmap
);
1512 bitmap_list_free(bm_list
);
1516 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1517 if (bm
->dirty_bitmap
== NULL
|| bm
->table
.offset
== 0) {
1521 free_bitmap_clusters(bs
, &bm
->table
);
1524 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1528 bitmap_list_free(bm_list
);
1531 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
)
1533 BdrvDirtyBitmap
*bitmap
;
1534 Error
*local_err
= NULL
;
1536 qcow2_store_persistent_dirty_bitmaps(bs
, &local_err
);
1537 if (local_err
!= NULL
) {
1538 error_propagate(errp
, local_err
);
1542 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1543 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1545 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
1546 bdrv_dirty_bitmap_set_readonly(bitmap
, true);
1553 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
1555 uint32_t granularity
,
1558 BDRVQcow2State
*s
= bs
->opaque
;
1560 Qcow2BitmapList
*bm_list
;
1562 if (s
->qcow_version
< 3) {
1563 /* Without autoclear_features, we would always have to assume
1564 * that a program without persistent dirty bitmap support has
1565 * accessed this qcow2 file when opening it, and would thus
1566 * have to drop all dirty bitmaps (defeating their purpose).
1568 error_setg(errp
, "Cannot store dirty bitmaps in qcow2 v2 files");
1572 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) != 0) {
1576 if (s
->nb_bitmaps
== 0) {
1580 if (s
->nb_bitmaps
>= QCOW2_MAX_BITMAPS
) {
1582 "Maximum number of persistent bitmaps is already reached");
1586 if (s
->bitmap_directory_size
+ calc_dir_entry_size(strlen(name
), 0) >
1587 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
)
1589 error_setg(errp
, "Not enough space in the bitmap directory");
1593 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1594 s
->bitmap_directory_size
, errp
);
1595 if (bm_list
== NULL
) {
1599 found
= find_bitmap_by_name(bm_list
, name
);
1600 bitmap_list_free(bm_list
);
1602 error_setg(errp
, "Bitmap with the same name is already stored");
1609 error_prepend(errp
, "Can't make bitmap '%s' persistent in '%s': ",
1610 name
, bdrv_get_device_or_node_name(bs
));