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 "block/block-io.h"
30 #include "block/dirty-bitmap.h"
31 #include "qapi/error.h"
32 #include "qemu/cutils.h"
36 /* NOTICE: BME here means Bitmaps Extension and used as a namespace for
37 * _internal_ constants. Please do not use this _internal_ abbreviation for
38 * other needs and/or outside of this file. */
40 /* Bitmap directory entry constraints */
41 #define BME_MAX_TABLE_SIZE 0x8000000
42 #define BME_MAX_PHYS_SIZE 0x20000000 /* restrict BdrvDirtyBitmap size in RAM */
43 #define BME_MAX_GRANULARITY_BITS 31
44 #define BME_MIN_GRANULARITY_BITS 9
45 #define BME_MAX_NAME_SIZE 1023
47 /* Size of bitmap table entries */
48 #define BME_TABLE_ENTRY_SIZE (sizeof(uint64_t))
50 QEMU_BUILD_BUG_ON(BME_MAX_NAME_SIZE
!= BDRV_BITMAP_MAX_NAME_SIZE
);
52 #if BME_MAX_TABLE_SIZE * 8ULL > INT_MAX
53 #error In the code bitmap table physical size assumed to fit into int
56 /* Bitmap directory entry flags */
57 #define BME_RESERVED_FLAGS 0xfffffffcU
58 #define BME_FLAG_IN_USE (1U << 0)
59 #define BME_FLAG_AUTO (1U << 1)
61 /* bits [1, 8] U [56, 63] are reserved */
62 #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001feULL
63 #define BME_TABLE_ENTRY_OFFSET_MASK 0x00fffffffffffe00ULL
64 #define BME_TABLE_ENTRY_FLAG_ALL_ONES (1ULL << 0)
66 typedef struct QEMU_PACKED Qcow2BitmapDirEntry
{
67 /* header is 8 byte aligned */
68 uint64_t bitmap_table_offset
;
70 uint32_t bitmap_table_size
;
74 uint8_t granularity_bits
;
76 uint32_t extra_data_size
;
77 /* extra data follows */
79 } Qcow2BitmapDirEntry
;
81 typedef struct Qcow2BitmapTable
{
83 uint32_t size
; /* number of 64bit entries */
84 QSIMPLEQ_ENTRY(Qcow2BitmapTable
) entry
;
87 typedef struct Qcow2Bitmap
{
88 Qcow2BitmapTable table
;
90 uint8_t granularity_bits
;
93 BdrvDirtyBitmap
*dirty_bitmap
;
95 QSIMPLEQ_ENTRY(Qcow2Bitmap
) entry
;
97 typedef QSIMPLEQ_HEAD(Qcow2BitmapList
, Qcow2Bitmap
) Qcow2BitmapList
;
99 typedef enum BitmapType
{
100 BT_DIRTY_TRACKING_BITMAP
= 1
103 static inline bool can_write(BlockDriverState
*bs
)
105 return !bdrv_is_read_only(bs
) && !(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
);
108 static int update_header_sync(BlockDriverState
*bs
)
112 ret
= qcow2_update_header(bs
);
117 return bdrv_flush(bs
->file
->bs
);
120 static inline void bitmap_table_bswap_be(uint64_t *bitmap_table
, size_t size
)
124 for (i
= 0; i
< size
; ++i
) {
125 bitmap_table
[i
] = cpu_to_be64(bitmap_table
[i
]);
129 static int check_table_entry(uint64_t entry
, int cluster_size
)
133 if (entry
& BME_TABLE_ENTRY_RESERVED_MASK
) {
137 offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
139 /* if offset specified, bit 0 is reserved */
140 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
144 if (offset
% cluster_size
!= 0) {
152 static int64_t get_bitmap_bytes_needed(int64_t len
, uint32_t granularity
)
154 int64_t num_bits
= DIV_ROUND_UP(len
, granularity
);
156 return DIV_ROUND_UP(num_bits
, 8);
159 static int check_constraints_on_bitmap(BlockDriverState
*bs
,
161 uint32_t granularity
,
164 BDRVQcow2State
*s
= bs
->opaque
;
165 int granularity_bits
= ctz32(granularity
);
166 int64_t len
= bdrv_getlength(bs
);
167 int64_t bitmap_bytes
;
169 assert(granularity
> 0);
170 assert((granularity
& (granularity
- 1)) == 0);
173 error_setg_errno(errp
, -len
, "Failed to get size of '%s'",
174 bdrv_get_device_or_node_name(bs
));
178 if (granularity_bits
> BME_MAX_GRANULARITY_BITS
) {
179 error_setg(errp
, "Granularity exceeds maximum (%llu bytes)",
180 1ULL << BME_MAX_GRANULARITY_BITS
);
183 if (granularity_bits
< BME_MIN_GRANULARITY_BITS
) {
184 error_setg(errp
, "Granularity is under minimum (%llu bytes)",
185 1ULL << BME_MIN_GRANULARITY_BITS
);
189 bitmap_bytes
= get_bitmap_bytes_needed(len
, granularity
);
190 if ((bitmap_bytes
> (uint64_t)BME_MAX_PHYS_SIZE
) ||
191 (bitmap_bytes
> (uint64_t)BME_MAX_TABLE_SIZE
* s
->cluster_size
))
193 error_setg(errp
, "Too much space will be occupied by the bitmap. "
194 "Use larger granularity");
198 if (strlen(name
) > BME_MAX_NAME_SIZE
) {
199 error_setg(errp
, "Name length exceeds maximum (%u characters)",
207 static void clear_bitmap_table(BlockDriverState
*bs
, uint64_t *bitmap_table
,
208 uint32_t bitmap_table_size
)
210 BDRVQcow2State
*s
= bs
->opaque
;
213 for (i
= 0; i
< bitmap_table_size
; ++i
) {
214 uint64_t addr
= bitmap_table
[i
] & BME_TABLE_ENTRY_OFFSET_MASK
;
219 qcow2_free_clusters(bs
, addr
, s
->cluster_size
, QCOW2_DISCARD_ALWAYS
);
224 static int bitmap_table_load(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
,
225 uint64_t **bitmap_table
)
228 BDRVQcow2State
*s
= bs
->opaque
;
232 assert(tb
->size
!= 0);
233 table
= g_try_new(uint64_t, tb
->size
);
238 assert(tb
->size
<= BME_MAX_TABLE_SIZE
);
239 ret
= bdrv_pread(bs
->file
, tb
->offset
, tb
->size
* BME_TABLE_ENTRY_SIZE
,
245 for (i
= 0; i
< tb
->size
; ++i
) {
246 table
[i
] = be64_to_cpu(table
[i
]);
247 ret
= check_table_entry(table
[i
], s
->cluster_size
);
253 *bitmap_table
= table
;
262 static int free_bitmap_clusters(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
)
265 uint64_t *bitmap_table
;
267 ret
= bitmap_table_load(bs
, tb
, &bitmap_table
);
272 clear_bitmap_table(bs
, bitmap_table
, tb
->size
);
273 qcow2_free_clusters(bs
, tb
->offset
, tb
->size
* BME_TABLE_ENTRY_SIZE
,
274 QCOW2_DISCARD_OTHER
);
275 g_free(bitmap_table
);
284 * @bitmap_table entries must satisfy specification constraints.
285 * @bitmap must be cleared */
286 static int coroutine_fn GRAPH_RDLOCK
287 load_bitmap_data(BlockDriverState
*bs
, const uint64_t *bitmap_table
,
288 uint32_t bitmap_table_size
, BdrvDirtyBitmap
*bitmap
)
291 BDRVQcow2State
*s
= bs
->opaque
;
292 uint64_t offset
, limit
;
293 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
295 uint64_t i
, tab_size
=
297 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
299 if (tab_size
!= bitmap_table_size
|| tab_size
> BME_MAX_TABLE_SIZE
) {
303 buf
= g_malloc(s
->cluster_size
);
304 limit
= bdrv_dirty_bitmap_serialization_coverage(s
->cluster_size
, bitmap
);
305 for (i
= 0, offset
= 0; i
< tab_size
; ++i
, offset
+= limit
) {
306 uint64_t count
= MIN(bm_size
- offset
, limit
);
307 uint64_t entry
= bitmap_table
[i
];
308 uint64_t data_offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
310 assert(check_table_entry(entry
, s
->cluster_size
) == 0);
312 if (data_offset
== 0) {
313 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
314 bdrv_dirty_bitmap_deserialize_ones(bitmap
, offset
, count
,
317 /* No need to deserialize zeros because the dirty bitmap is
321 ret
= bdrv_co_pread(bs
->file
, data_offset
, s
->cluster_size
, buf
, 0);
325 bdrv_dirty_bitmap_deserialize_part(bitmap
, buf
, offset
, count
,
331 bdrv_dirty_bitmap_deserialize_finish(bitmap
);
339 static coroutine_fn GRAPH_RDLOCK
340 BdrvDirtyBitmap
*load_bitmap(BlockDriverState
*bs
,
341 Qcow2Bitmap
*bm
, Error
**errp
)
344 uint64_t *bitmap_table
= NULL
;
345 uint32_t granularity
;
346 BdrvDirtyBitmap
*bitmap
= NULL
;
348 granularity
= 1U << bm
->granularity_bits
;
349 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, bm
->name
, errp
);
350 if (bitmap
== NULL
) {
354 if (bm
->flags
& BME_FLAG_IN_USE
) {
355 /* Data is unusable, skip loading it */
359 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
361 error_setg_errno(errp
, -ret
,
362 "Could not read bitmap_table table from image for "
363 "bitmap '%s'", bm
->name
);
367 ret
= load_bitmap_data(bs
, bitmap_table
, bm
->table
.size
, bitmap
);
369 error_setg_errno(errp
, -ret
, "Could not read bitmap '%s' from image",
374 g_free(bitmap_table
);
378 g_free(bitmap_table
);
379 if (bitmap
!= NULL
) {
380 bdrv_release_dirty_bitmap(bitmap
);
391 * Bitmap List private functions
392 * Only Bitmap List knows about bitmap directory structure in Qcow2.
395 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry
*entry
)
397 entry
->bitmap_table_offset
= be64_to_cpu(entry
->bitmap_table_offset
);
398 entry
->bitmap_table_size
= be32_to_cpu(entry
->bitmap_table_size
);
399 entry
->flags
= be32_to_cpu(entry
->flags
);
400 entry
->name_size
= be16_to_cpu(entry
->name_size
);
401 entry
->extra_data_size
= be32_to_cpu(entry
->extra_data_size
);
404 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry
*entry
)
406 entry
->bitmap_table_offset
= cpu_to_be64(entry
->bitmap_table_offset
);
407 entry
->bitmap_table_size
= cpu_to_be32(entry
->bitmap_table_size
);
408 entry
->flags
= cpu_to_be32(entry
->flags
);
409 entry
->name_size
= cpu_to_be16(entry
->name_size
);
410 entry
->extra_data_size
= cpu_to_be32(entry
->extra_data_size
);
413 static inline int calc_dir_entry_size(size_t name_size
, size_t extra_data_size
)
415 int size
= sizeof(Qcow2BitmapDirEntry
) + name_size
+ extra_data_size
;
416 return ROUND_UP(size
, 8);
419 static inline int dir_entry_size(Qcow2BitmapDirEntry
*entry
)
421 return calc_dir_entry_size(entry
->name_size
, entry
->extra_data_size
);
424 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry
*entry
)
426 return (const char *)(entry
+ 1) + entry
->extra_data_size
;
429 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry
*entry
)
431 const char *name_field
= dir_entry_name_field(entry
);
432 return g_strndup(name_field
, entry
->name_size
);
435 static inline Qcow2BitmapDirEntry
*next_dir_entry(Qcow2BitmapDirEntry
*entry
)
437 return (Qcow2BitmapDirEntry
*)((uint8_t *)entry
+ dir_entry_size(entry
));
440 static int check_dir_entry(BlockDriverState
*bs
, Qcow2BitmapDirEntry
*entry
)
442 BDRVQcow2State
*s
= bs
->opaque
;
443 uint64_t phys_bitmap_bytes
;
446 bool fail
= (entry
->bitmap_table_size
== 0) ||
447 (entry
->bitmap_table_offset
== 0) ||
448 (entry
->bitmap_table_offset
% s
->cluster_size
) ||
449 (entry
->bitmap_table_size
> BME_MAX_TABLE_SIZE
) ||
450 (entry
->granularity_bits
> BME_MAX_GRANULARITY_BITS
) ||
451 (entry
->granularity_bits
< BME_MIN_GRANULARITY_BITS
) ||
452 (entry
->flags
& BME_RESERVED_FLAGS
) ||
453 (entry
->name_size
> BME_MAX_NAME_SIZE
) ||
454 (entry
->type
!= BT_DIRTY_TRACKING_BITMAP
);
460 phys_bitmap_bytes
= (uint64_t)entry
->bitmap_table_size
* s
->cluster_size
;
461 len
= bdrv_getlength(bs
);
467 if (phys_bitmap_bytes
> BME_MAX_PHYS_SIZE
) {
471 if (!(entry
->flags
& BME_FLAG_IN_USE
) &&
472 (len
> ((phys_bitmap_bytes
* 8) << entry
->granularity_bits
)))
475 * We've loaded a valid bitmap (IN_USE not set) or we are going to
476 * store a valid bitmap, but the allocated bitmap table size is not
477 * enough to store this bitmap.
479 * Note, that it's OK to have an invalid bitmap with invalid size due
480 * to a bitmap that was not correctly saved after image resize.
488 static inline void bitmap_directory_to_be(uint8_t *dir
, size_t size
)
490 uint8_t *end
= dir
+ size
;
492 Qcow2BitmapDirEntry
*e
= (Qcow2BitmapDirEntry
*)dir
;
493 dir
+= dir_entry_size(e
);
495 bitmap_dir_entry_to_be(e
);
500 * Bitmap List public functions
503 static void bitmap_free(Qcow2Bitmap
*bm
)
513 static void bitmap_list_free(Qcow2BitmapList
*bm_list
)
517 if (bm_list
== NULL
) {
521 while ((bm
= QSIMPLEQ_FIRST(bm_list
)) != NULL
) {
522 QSIMPLEQ_REMOVE_HEAD(bm_list
, entry
);
529 static Qcow2BitmapList
*bitmap_list_new(void)
531 Qcow2BitmapList
*bm_list
= g_new(Qcow2BitmapList
, 1);
532 QSIMPLEQ_INIT(bm_list
);
537 static uint32_t bitmap_list_count(Qcow2BitmapList
*bm_list
)
540 uint32_t nb_bitmaps
= 0;
542 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
550 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
551 * checks it and convert to bitmap list.
553 static Qcow2BitmapList
*bitmap_list_load(BlockDriverState
*bs
, uint64_t offset
,
554 uint64_t size
, Error
**errp
)
557 BDRVQcow2State
*s
= bs
->opaque
;
558 uint8_t *dir
, *dir_end
;
559 Qcow2BitmapDirEntry
*e
;
560 uint32_t nb_dir_entries
= 0;
561 Qcow2BitmapList
*bm_list
= NULL
;
564 error_setg(errp
, "Requested bitmap directory size is zero");
568 if (size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
569 error_setg(errp
, "Requested bitmap directory size is too big");
573 dir
= g_try_malloc(size
);
575 error_setg(errp
, "Failed to allocate space for bitmap directory");
578 dir_end
= dir
+ size
;
580 ret
= bdrv_pread(bs
->file
, offset
, size
, dir
, 0);
582 error_setg_errno(errp
, -ret
, "Failed to read bitmap directory");
586 bm_list
= bitmap_list_new();
587 for (e
= (Qcow2BitmapDirEntry
*)dir
;
588 e
< (Qcow2BitmapDirEntry
*)dir_end
;
589 e
= next_dir_entry(e
))
593 if ((uint8_t *)(e
+ 1) > dir_end
) {
597 if (++nb_dir_entries
> s
->nb_bitmaps
) {
598 error_setg(errp
, "More bitmaps found than specified in header"
602 bitmap_dir_entry_to_cpu(e
);
604 if ((uint8_t *)next_dir_entry(e
) > dir_end
) {
608 if (e
->extra_data_size
!= 0) {
609 error_setg(errp
, "Bitmap extra data is not supported");
613 ret
= check_dir_entry(bs
, e
);
615 error_setg(errp
, "Bitmap '%.*s' doesn't satisfy the constraints",
616 e
->name_size
, dir_entry_name_field(e
));
620 bm
= g_new0(Qcow2Bitmap
, 1);
621 bm
->table
.offset
= e
->bitmap_table_offset
;
622 bm
->table
.size
= e
->bitmap_table_size
;
623 bm
->flags
= e
->flags
;
624 bm
->granularity_bits
= e
->granularity_bits
;
625 bm
->name
= dir_entry_copy_name(e
);
626 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
629 if (nb_dir_entries
!= s
->nb_bitmaps
) {
630 error_setg(errp
, "Less bitmaps found than specified in header"
635 if ((uint8_t *)e
!= dir_end
) {
643 error_setg(errp
, "Broken bitmap directory");
647 bitmap_list_free(bm_list
);
653 qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
654 void **refcount_table
,
655 int64_t *refcount_table_size
)
658 BDRVQcow2State
*s
= bs
->opaque
;
659 Qcow2BitmapList
*bm_list
;
662 if (s
->nb_bitmaps
== 0) {
666 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
667 s
->bitmap_directory_offset
,
668 s
->bitmap_directory_size
);
673 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
674 s
->bitmap_directory_size
, NULL
);
675 if (bm_list
== NULL
) {
680 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
681 uint64_t *bitmap_table
= NULL
;
684 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
685 refcount_table
, refcount_table_size
,
687 bm
->table
.size
* BME_TABLE_ENTRY_SIZE
);
692 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
698 for (i
= 0; i
< bm
->table
.size
; ++i
) {
699 uint64_t entry
= bitmap_table
[i
];
700 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
702 if (check_table_entry(entry
, s
->cluster_size
) < 0) {
711 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
712 refcount_table
, refcount_table_size
,
713 offset
, s
->cluster_size
);
715 g_free(bitmap_table
);
720 g_free(bitmap_table
);
724 bitmap_list_free(bm_list
);
730 * Store bitmap list to qcow2 image as a bitmap directory.
731 * Everything is checked.
733 static int bitmap_list_store(BlockDriverState
*bs
, Qcow2BitmapList
*bm_list
,
734 uint64_t *offset
, uint64_t *size
, bool in_place
)
738 int64_t dir_offset
= 0;
739 uint64_t dir_size
= 0;
741 Qcow2BitmapDirEntry
*e
;
743 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
744 dir_size
+= calc_dir_entry_size(strlen(bm
->name
), 0);
747 if (dir_size
== 0 || dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
752 if (*size
!= dir_size
|| *offset
== 0) {
756 dir_offset
= *offset
;
759 dir
= g_try_malloc0(dir_size
);
764 e
= (Qcow2BitmapDirEntry
*)dir
;
765 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
766 e
->bitmap_table_offset
= bm
->table
.offset
;
767 e
->bitmap_table_size
= bm
->table
.size
;
768 e
->flags
= bm
->flags
;
769 e
->type
= BT_DIRTY_TRACKING_BITMAP
;
770 e
->granularity_bits
= bm
->granularity_bits
;
771 e
->name_size
= strlen(bm
->name
);
772 e
->extra_data_size
= 0;
773 memcpy(e
+ 1, bm
->name
, e
->name_size
);
775 if (check_dir_entry(bs
, e
) < 0) {
780 e
= next_dir_entry(e
);
783 bitmap_directory_to_be(dir
, dir_size
);
786 dir_offset
= qcow2_alloc_clusters(bs
, dir_size
);
787 if (dir_offset
< 0) {
793 /* Actually, even in the in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY
794 * is not necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating
795 * bitmap directory in-place (actually, turn-off the extension), which is
796 * checked in qcow2_check_metadata_overlap() */
797 ret
= qcow2_pre_write_overlap_check(
798 bs
, in_place
? QCOW2_OL_BITMAP_DIRECTORY
: 0, dir_offset
, dir_size
,
804 ret
= bdrv_pwrite(bs
->file
, dir_offset
, dir_size
, dir
, 0);
813 *offset
= dir_offset
;
821 if (!in_place
&& dir_offset
> 0) {
822 qcow2_free_clusters(bs
, dir_offset
, dir_size
, QCOW2_DISCARD_OTHER
);
832 static int update_ext_header_and_dir_in_place(BlockDriverState
*bs
,
833 Qcow2BitmapList
*bm_list
)
835 BDRVQcow2State
*s
= bs
->opaque
;
838 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
) ||
839 bm_list
== NULL
|| QSIMPLEQ_EMPTY(bm_list
) ||
840 bitmap_list_count(bm_list
) != s
->nb_bitmaps
)
845 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
846 ret
= update_header_sync(bs
);
848 /* Two variants are possible here:
849 * 1. Autoclear flag is dropped, all bitmaps will be lost.
850 * 2. Autoclear flag is not dropped, old state is left.
855 /* autoclear bit is not set, so we can safely update bitmap directory */
857 ret
= bitmap_list_store(bs
, bm_list
, &s
->bitmap_directory_offset
,
858 &s
->bitmap_directory_size
, true);
860 /* autoclear bit is cleared, so all leaked clusters would be removed on
865 ret
= update_header_sync(bs
);
867 /* autoclear bit is cleared, so all leaked clusters would be removed on
872 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
873 return update_header_sync(bs
);
874 /* If final update_header_sync() fails, two variants are possible:
875 * 1. Autoclear flag is not set, all bitmaps will be lost.
876 * 2. Autoclear flag is set, header and directory are successfully updated.
880 static int update_ext_header_and_dir(BlockDriverState
*bs
,
881 Qcow2BitmapList
*bm_list
)
883 BDRVQcow2State
*s
= bs
->opaque
;
885 uint64_t new_offset
= 0;
886 uint64_t new_size
= 0;
887 uint32_t new_nb_bitmaps
= 0;
888 uint64_t old_offset
= s
->bitmap_directory_offset
;
889 uint64_t old_size
= s
->bitmap_directory_size
;
890 uint32_t old_nb_bitmaps
= s
->nb_bitmaps
;
891 uint64_t old_autocl
= s
->autoclear_features
;
893 if (bm_list
!= NULL
&& !QSIMPLEQ_EMPTY(bm_list
)) {
894 new_nb_bitmaps
= bitmap_list_count(bm_list
);
896 if (new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
900 ret
= bitmap_list_store(bs
, bm_list
, &new_offset
, &new_size
, false);
905 ret
= qcow2_flush_caches(bs
);
910 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
912 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
915 s
->bitmap_directory_offset
= new_offset
;
916 s
->bitmap_directory_size
= new_size
;
917 s
->nb_bitmaps
= new_nb_bitmaps
;
919 ret
= update_header_sync(bs
);
925 qcow2_free_clusters(bs
, old_offset
, old_size
, QCOW2_DISCARD_OTHER
);
931 if (new_offset
> 0) {
932 qcow2_free_clusters(bs
, new_offset
, new_size
, QCOW2_DISCARD_OTHER
);
935 s
->bitmap_directory_offset
= old_offset
;
936 s
->bitmap_directory_size
= old_size
;
937 s
->nb_bitmaps
= old_nb_bitmaps
;
938 s
->autoclear_features
= old_autocl
;
943 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
944 static void release_dirty_bitmap_helper(gpointer bitmap
,
947 bdrv_release_dirty_bitmap(bitmap
);
950 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
951 static void set_readonly_helper(gpointer bitmap
, gpointer value
)
953 bdrv_dirty_bitmap_set_readonly(bitmap
, (bool)value
);
957 * Return true on success, false on failure.
958 * If header_updated is not NULL then it is set appropriately regardless of
961 bool coroutine_fn GRAPH_RDLOCK
962 qcow2_load_dirty_bitmaps(BlockDriverState
*bs
,
963 bool *header_updated
, Error
**errp
)
965 BDRVQcow2State
*s
= bs
->opaque
;
966 Qcow2BitmapList
*bm_list
;
968 GSList
*created_dirty_bitmaps
= NULL
;
969 bool needs_update
= false;
971 if (header_updated
) {
972 *header_updated
= false;
975 if (s
->nb_bitmaps
== 0) {
976 /* No bitmaps - nothing to do */
980 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
981 s
->bitmap_directory_size
, errp
);
982 if (bm_list
== NULL
) {
986 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
987 BdrvDirtyBitmap
*bitmap
;
989 if ((bm
->flags
& BME_FLAG_IN_USE
) &&
990 bdrv_find_dirty_bitmap(bs
, bm
->name
))
993 * We already have corresponding BdrvDirtyBitmap, and bitmap in the
994 * image is marked IN_USE. Firstly, this state is valid, no reason
995 * to consider existing BdrvDirtyBitmap to be bad. Secondly it's
996 * absolutely possible, when we do migration with shared storage
997 * with dirty-bitmaps capability enabled: if the bitmap was loaded
998 * from this storage before migration start, the storage will
999 * of-course contain IN_USE outdated version of the bitmap, and we
1000 * should not load it on migration target, as we already have this
1001 * bitmap, being migrated.
1006 bitmap
= load_bitmap(bs
, bm
, errp
);
1007 if (bitmap
== NULL
) {
1011 bdrv_dirty_bitmap_set_persistence(bitmap
, true);
1012 if (bm
->flags
& BME_FLAG_IN_USE
) {
1013 bdrv_dirty_bitmap_set_inconsistent(bitmap
);
1015 /* NB: updated flags only get written if can_write(bs) is true. */
1016 bm
->flags
|= BME_FLAG_IN_USE
;
1017 needs_update
= true;
1019 if (!(bm
->flags
& BME_FLAG_AUTO
)) {
1020 bdrv_disable_dirty_bitmap(bitmap
);
1022 created_dirty_bitmaps
=
1023 g_slist_append(created_dirty_bitmaps
, bitmap
);
1026 if (needs_update
&& can_write(bs
)) {
1027 /* in_use flags must be updated */
1028 int ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1030 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
1033 if (header_updated
) {
1034 *header_updated
= true;
1038 if (!can_write(bs
)) {
1039 g_slist_foreach(created_dirty_bitmaps
, set_readonly_helper
,
1043 g_slist_free(created_dirty_bitmaps
);
1044 bitmap_list_free(bm_list
);
1049 g_slist_foreach(created_dirty_bitmaps
, release_dirty_bitmap_helper
, bs
);
1050 g_slist_free(created_dirty_bitmaps
);
1051 bitmap_list_free(bm_list
);
1057 static Qcow2BitmapInfoFlagsList
*get_bitmap_info_flags(uint32_t flags
)
1059 Qcow2BitmapInfoFlagsList
*list
= NULL
;
1060 Qcow2BitmapInfoFlagsList
**tail
= &list
;
1063 static const struct {
1064 int bme
; /* Bitmap directory entry flags */
1065 int info
; /* The flags to report to the user */
1067 { BME_FLAG_IN_USE
, QCOW2_BITMAP_INFO_FLAGS_IN_USE
},
1068 { BME_FLAG_AUTO
, QCOW2_BITMAP_INFO_FLAGS_AUTO
},
1071 int map_size
= ARRAY_SIZE(map
);
1073 for (i
= 0; i
< map_size
; ++i
) {
1074 if (flags
& map
[i
].bme
) {
1075 QAPI_LIST_APPEND(tail
, map
[i
].info
);
1076 flags
&= ~map
[i
].bme
;
1079 /* Check if the BME_* mapping above is complete */
1086 * qcow2_get_bitmap_info_list()
1087 * Returns a list of QCOW2 bitmap details.
1088 * On success return true with info_list set (note, that if there are no
1089 * bitmaps, info_list is set to NULL).
1090 * On failure return false with errp set.
1092 bool qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
1093 Qcow2BitmapInfoList
**info_list
, Error
**errp
)
1095 BDRVQcow2State
*s
= bs
->opaque
;
1096 Qcow2BitmapList
*bm_list
;
1098 Qcow2BitmapInfoList
**tail
;
1100 if (s
->nb_bitmaps
== 0) {
1105 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1106 s
->bitmap_directory_size
, errp
);
1114 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1115 Qcow2BitmapInfo
*info
= g_new0(Qcow2BitmapInfo
, 1);
1116 info
->granularity
= 1U << bm
->granularity_bits
;
1117 info
->name
= g_strdup(bm
->name
);
1118 info
->flags
= get_bitmap_info_flags(bm
->flags
& ~BME_RESERVED_FLAGS
);
1119 QAPI_LIST_APPEND(tail
, info
);
1122 bitmap_list_free(bm_list
);
1127 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
)
1129 BDRVQcow2State
*s
= bs
->opaque
;
1130 Qcow2BitmapList
*bm_list
;
1132 GSList
*ro_dirty_bitmaps
= NULL
;
1134 bool need_header_update
= false;
1136 if (s
->nb_bitmaps
== 0) {
1137 /* No bitmaps - nothing to do */
1141 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1142 s
->bitmap_directory_size
, errp
);
1143 if (bm_list
== NULL
) {
1147 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1148 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1151 error_setg(errp
, "Unexpected bitmap '%s' in image '%s'",
1152 bm
->name
, bs
->filename
);
1156 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1157 if (!bdrv_dirty_bitmap_readonly(bitmap
)) {
1158 error_setg(errp
, "Corruption: bitmap '%s' is not marked IN_USE "
1159 "in the image '%s' and not marked readonly in RAM",
1160 bm
->name
, bs
->filename
);
1163 if (bdrv_dirty_bitmap_inconsistent(bitmap
)) {
1164 error_setg(errp
, "Corruption: bitmap '%s' is inconsistent but "
1165 "is not marked IN_USE in the image '%s'", bm
->name
,
1170 bm
->flags
|= BME_FLAG_IN_USE
;
1171 need_header_update
= true;
1174 * What if flags already has BME_FLAG_IN_USE ?
1176 * 1. if we are reopening RW -> RW it's OK, of course.
1177 * 2. if we are reopening RO -> RW:
1178 * 2.1 if @bitmap is inconsistent, it's OK. It means that it was
1179 * inconsistent (IN_USE) when we loaded it
1180 * 2.2 if @bitmap is not inconsistent. This seems to be impossible
1181 * and implies third party interaction. Let's error-out for
1184 if (bdrv_dirty_bitmap_readonly(bitmap
) &&
1185 !bdrv_dirty_bitmap_inconsistent(bitmap
))
1187 error_setg(errp
, "Corruption: bitmap '%s' is marked IN_USE "
1188 "in the image '%s' but it is readonly and "
1189 "consistent in RAM",
1190 bm
->name
, bs
->filename
);
1195 if (bdrv_dirty_bitmap_readonly(bitmap
)) {
1196 ro_dirty_bitmaps
= g_slist_append(ro_dirty_bitmaps
, bitmap
);
1200 if (need_header_update
) {
1201 if (!can_write(bs
->file
->bs
) || !(bs
->file
->perm
& BLK_PERM_WRITE
)) {
1202 error_setg(errp
, "Failed to reopen bitmaps rw: no write access "
1203 "the protocol file");
1207 /* in_use flags must be updated */
1208 ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1210 error_setg_errno(errp
, -ret
, "Cannot update bitmap directory");
1215 g_slist_foreach(ro_dirty_bitmaps
, set_readonly_helper
, (gpointer
)false);
1219 g_slist_free(ro_dirty_bitmaps
);
1220 bitmap_list_free(bm_list
);
1225 /* Checks to see if it's safe to resize bitmaps */
1226 int coroutine_fn
qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
)
1228 BDRVQcow2State
*s
= bs
->opaque
;
1229 Qcow2BitmapList
*bm_list
;
1233 if (s
->nb_bitmaps
== 0) {
1237 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1238 s
->bitmap_directory_size
, errp
);
1239 if (bm_list
== NULL
) {
1243 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1244 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1245 if (bitmap
== NULL
) {
1247 * We rely on all bitmaps being in-memory to be able to resize them,
1248 * Otherwise, we'd need to resize them on disk explicitly
1250 error_setg(errp
, "Cannot resize qcow2 with persistent bitmaps that "
1251 "were not loaded into memory");
1257 * The checks against readonly and busy are redundant, but certainly
1258 * do no harm. checks against inconsistent are crucial:
1260 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
1267 bitmap_list_free(bm_list
);
1271 /* store_bitmap_data()
1272 * Store bitmap to image, filling bitmap table accordingly.
1274 static uint64_t *store_bitmap_data(BlockDriverState
*bs
,
1275 BdrvDirtyBitmap
*bitmap
,
1276 uint32_t *bitmap_table_size
, Error
**errp
)
1279 BDRVQcow2State
*s
= bs
->opaque
;
1282 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
1283 const char *bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1284 uint8_t *buf
= NULL
;
1288 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
1290 if (tb_size
> BME_MAX_TABLE_SIZE
||
1291 tb_size
* s
->cluster_size
> BME_MAX_PHYS_SIZE
)
1293 error_setg(errp
, "Bitmap '%s' is too big", bm_name
);
1297 tb
= g_try_new0(uint64_t, tb_size
);
1299 error_setg(errp
, "No memory");
1303 buf
= g_malloc(s
->cluster_size
);
1304 limit
= bdrv_dirty_bitmap_serialization_coverage(s
->cluster_size
, bitmap
);
1305 assert(DIV_ROUND_UP(bm_size
, limit
) == tb_size
);
1308 while ((offset
= bdrv_dirty_bitmap_next_dirty(bitmap
, offset
, INT64_MAX
))
1311 uint64_t cluster
= offset
/ limit
;
1312 uint64_t end
, write_size
;
1316 * We found the first dirty offset, but want to write out the
1317 * entire cluster of the bitmap that includes that offset,
1318 * including any leading zero bits.
1320 offset
= QEMU_ALIGN_DOWN(offset
, limit
);
1321 end
= MIN(bm_size
, offset
+ limit
);
1322 write_size
= bdrv_dirty_bitmap_serialization_size(bitmap
, offset
,
1324 assert(write_size
<= s
->cluster_size
);
1326 off
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1328 error_setg_errno(errp
, -off
,
1329 "Failed to allocate clusters for bitmap '%s'",
1335 bdrv_dirty_bitmap_serialize_part(bitmap
, buf
, offset
, end
- offset
);
1336 if (write_size
< s
->cluster_size
) {
1337 memset(buf
+ write_size
, 0, s
->cluster_size
- write_size
);
1340 ret
= qcow2_pre_write_overlap_check(bs
, 0, off
, s
->cluster_size
, false);
1342 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1346 ret
= bdrv_pwrite(bs
->file
, off
, s
->cluster_size
, buf
, 0);
1348 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1356 *bitmap_table_size
= tb_size
;
1362 clear_bitmap_table(bs
, tb
, tb_size
);
1370 * Store bm->dirty_bitmap to qcow2.
1371 * Set bm->table_offset and bm->table_size accordingly.
1373 static int store_bitmap(BlockDriverState
*bs
, Qcow2Bitmap
*bm
, Error
**errp
)
1379 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1380 const char *bm_name
;
1382 assert(bitmap
!= NULL
);
1384 bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1386 tb
= store_bitmap_data(bs
, bitmap
, &tb_size
, errp
);
1391 assert(tb_size
<= BME_MAX_TABLE_SIZE
);
1392 tb_offset
= qcow2_alloc_clusters(bs
, tb_size
* sizeof(tb
[0]));
1393 if (tb_offset
< 0) {
1394 error_setg_errno(errp
, -tb_offset
,
1395 "Failed to allocate clusters for bitmap '%s'",
1401 ret
= qcow2_pre_write_overlap_check(bs
, 0, tb_offset
,
1402 tb_size
* sizeof(tb
[0]), false);
1404 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1408 bitmap_table_bswap_be(tb
, tb_size
);
1409 ret
= bdrv_pwrite(bs
->file
, tb_offset
, tb_size
* sizeof(tb
[0]), tb
, 0);
1411 bitmap_table_bswap_be(tb
, tb_size
);
1412 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1419 bm
->table
.offset
= tb_offset
;
1420 bm
->table
.size
= tb_size
;
1425 clear_bitmap_table(bs
, tb
, tb_size
);
1427 if (tb_offset
> 0) {
1428 qcow2_free_clusters(bs
, tb_offset
, tb_size
* sizeof(tb
[0]),
1429 QCOW2_DISCARD_OTHER
);
1437 static Qcow2Bitmap
*find_bitmap_by_name(Qcow2BitmapList
*bm_list
,
1442 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1443 if (strcmp(name
, bm
->name
) == 0) {
1451 int coroutine_fn
qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
1456 BDRVQcow2State
*s
= bs
->opaque
;
1457 Qcow2Bitmap
*bm
= NULL
;
1458 Qcow2BitmapList
*bm_list
;
1460 if (s
->nb_bitmaps
== 0) {
1462 * Absence of the bitmap is not an error: see explanation above
1463 * bdrv_co_remove_persistent_dirty_bitmap() definition.
1468 qemu_co_mutex_lock(&s
->lock
);
1470 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1471 s
->bitmap_directory_size
, errp
);
1472 if (bm_list
== NULL
) {
1477 bm
= find_bitmap_by_name(bm_list
, name
);
1479 /* Absence of the bitmap is not an error, see above. */
1484 QSIMPLEQ_REMOVE(bm_list
, bm
, Qcow2Bitmap
, entry
);
1486 ret
= update_ext_header_and_dir(bs
, bm_list
);
1488 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1492 free_bitmap_clusters(bs
, &bm
->table
);
1495 qemu_co_mutex_unlock(&s
->lock
);
1498 bitmap_list_free(bm_list
);
1504 * qcow2_store_persistent_dirty_bitmaps
1506 * Stores persistent BdrvDirtyBitmap objects.
1508 * @release_stored: if true, release BdrvDirtyBitmap's after storing to the
1509 * image. This is used in two cases, both via qcow2_inactivate:
1510 * 1. bdrv_close: It's correct to remove bitmaps on close.
1511 * 2. migration: If bitmaps are migrated through migration channel via
1512 * 'dirty-bitmaps' migration capability they are not handled by this code.
1513 * Otherwise, it's OK to drop BdrvDirtyBitmap's and reload them on
1516 * Anyway, it's correct to remove BdrvDirtyBitmap's on inactivation, as
1517 * inactivation means that we lose control on disk, and therefore on bitmaps,
1518 * we should sync them and do not touch more.
1520 * Contrariwise, we don't want to release any bitmaps on just reopen-to-ro,
1521 * when we need to store them, as image is still under our control, and it's
1522 * good to keep all the bitmaps in read-only mode. Moreover, keeping them
1523 * read-only is correct because this is what would happen if we opened the node
1524 * readonly to begin with, and whether we opened directly or reopened to that
1525 * state shouldn't matter for the state we get afterward.
1527 bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
,
1528 bool release_stored
, Error
**errp
)
1531 BdrvDirtyBitmap
*bitmap
;
1532 BDRVQcow2State
*s
= bs
->opaque
;
1533 uint32_t new_nb_bitmaps
= s
->nb_bitmaps
;
1534 uint64_t new_dir_size
= s
->bitmap_directory_size
;
1536 Qcow2BitmapList
*bm_list
;
1538 QSIMPLEQ_HEAD(, Qcow2BitmapTable
) drop_tables
;
1539 Qcow2BitmapTable
*tb
, *tb_next
;
1540 bool need_write
= false;
1542 QSIMPLEQ_INIT(&drop_tables
);
1544 if (s
->nb_bitmaps
== 0) {
1545 bm_list
= bitmap_list_new();
1547 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1548 s
->bitmap_directory_size
, errp
);
1549 if (bm_list
== NULL
) {
1554 /* check constraints and names */
1555 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
1556 const char *name
= bdrv_dirty_bitmap_name(bitmap
);
1557 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
1559 if (!bdrv_dirty_bitmap_get_persistence(bitmap
) ||
1560 bdrv_dirty_bitmap_inconsistent(bitmap
)) {
1564 if (bdrv_dirty_bitmap_readonly(bitmap
)) {
1566 * Store the bitmap in the associated Qcow2Bitmap so it
1567 * can be released later
1569 bm
= find_bitmap_by_name(bm_list
, name
);
1571 bm
->dirty_bitmap
= bitmap
;
1578 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) < 0) {
1579 error_prepend(errp
, "Bitmap '%s' doesn't satisfy the constraints: ",
1584 bm
= find_bitmap_by_name(bm_list
, name
);
1586 if (++new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1587 error_setg(errp
, "Too many persistent bitmaps");
1591 new_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1592 if (new_dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1593 error_setg(errp
, "Bitmap directory is too large");
1597 bm
= g_new0(Qcow2Bitmap
, 1);
1598 bm
->name
= g_strdup(name
);
1599 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
1601 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1602 error_setg(errp
, "Bitmap '%s' already exists in the image",
1606 tb
= g_memdup(&bm
->table
, sizeof(bm
->table
));
1607 bm
->table
.offset
= 0;
1609 QSIMPLEQ_INSERT_TAIL(&drop_tables
, tb
, entry
);
1611 bm
->flags
= bdrv_dirty_bitmap_enabled(bitmap
) ? BME_FLAG_AUTO
: 0;
1612 bm
->granularity_bits
= ctz32(bdrv_dirty_bitmap_granularity(bitmap
));
1613 bm
->dirty_bitmap
= bitmap
;
1620 if (!can_write(bs
)) {
1621 error_setg(errp
, "No write access");
1625 /* allocate clusters and store bitmaps */
1626 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1627 bitmap
= bm
->dirty_bitmap
;
1629 if (bitmap
== NULL
|| bdrv_dirty_bitmap_readonly(bitmap
)) {
1633 ret
= store_bitmap(bs
, bm
, errp
);
1639 ret
= update_ext_header_and_dir(bs
, bm_list
);
1641 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1645 /* Bitmap directory was successfully updated, so, old data can be dropped.
1646 * TODO it is better to reuse these clusters */
1647 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1648 free_bitmap_clusters(bs
, tb
);
1653 if (release_stored
) {
1654 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1655 if (bm
->dirty_bitmap
== NULL
) {
1659 bdrv_release_dirty_bitmap(bm
->dirty_bitmap
);
1663 bitmap_list_free(bm_list
);
1667 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1668 if (bm
->dirty_bitmap
== NULL
|| bm
->table
.offset
== 0 ||
1669 bdrv_dirty_bitmap_readonly(bm
->dirty_bitmap
))
1674 free_bitmap_clusters(bs
, &bm
->table
);
1677 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1681 bitmap_list_free(bm_list
);
1685 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
)
1687 BdrvDirtyBitmap
*bitmap
;
1689 if (!qcow2_store_persistent_dirty_bitmaps(bs
, false, errp
)) {
1693 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
1694 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
1695 bdrv_dirty_bitmap_set_readonly(bitmap
, true);
1702 bool coroutine_fn
qcow2_co_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
1704 uint32_t granularity
,
1707 BDRVQcow2State
*s
= bs
->opaque
;
1708 BdrvDirtyBitmap
*bitmap
;
1709 uint64_t bitmap_directory_size
= 0;
1710 uint32_t nb_bitmaps
= 0;
1712 if (bdrv_find_dirty_bitmap(bs
, name
)) {
1713 error_setg(errp
, "Bitmap already exists: %s", name
);
1717 if (s
->qcow_version
< 3) {
1718 /* Without autoclear_features, we would always have to assume
1719 * that a program without persistent dirty bitmap support has
1720 * accessed this qcow2 file when opening it, and would thus
1721 * have to drop all dirty bitmaps (defeating their purpose).
1723 error_setg(errp
, "Cannot store dirty bitmaps in qcow2 v2 files");
1727 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) != 0) {
1731 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
1732 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
1734 bitmap_directory_size
+=
1735 calc_dir_entry_size(strlen(bdrv_dirty_bitmap_name(bitmap
)), 0);
1739 bitmap_directory_size
+= calc_dir_entry_size(strlen(name
), 0);
1741 if (nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1743 "Maximum number of persistent bitmaps is already reached");
1747 if (bitmap_directory_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1748 error_setg(errp
, "Not enough space in the bitmap directory");
1755 error_prepend(errp
, "Can't make bitmap '%s' persistent in '%s': ",
1756 name
, bdrv_get_device_or_node_name(bs
));
1760 bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState
*bs
)
1762 BDRVQcow2State
*s
= bs
->opaque
;
1764 return s
->qcow_version
>= 3;
1768 * Compute the space required to copy bitmaps from @in_bs.
1770 * The computation is based as if copying to a new image with the
1771 * given @cluster_size, which may differ from the cluster size in
1772 * @in_bs; in fact, @in_bs might be something other than qcow2.
1774 uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState
*in_bs
,
1775 uint32_t cluster_size
)
1777 uint64_t bitmaps_size
= 0;
1778 BdrvDirtyBitmap
*bm
;
1779 size_t bitmap_dir_size
= 0;
1781 FOR_EACH_DIRTY_BITMAP(in_bs
, bm
) {
1782 if (bdrv_dirty_bitmap_get_persistence(bm
)) {
1783 const char *name
= bdrv_dirty_bitmap_name(bm
);
1784 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bm
);
1786 get_bitmap_bytes_needed(bdrv_dirty_bitmap_size(bm
),
1788 uint64_t bmclusters
= DIV_ROUND_UP(bmbytes
, cluster_size
);
1790 /* Assume the entire bitmap is allocated */
1791 bitmaps_size
+= bmclusters
* cluster_size
;
1792 /* Also reserve space for the bitmap table entries */
1793 bitmaps_size
+= ROUND_UP(bmclusters
* BME_TABLE_ENTRY_SIZE
,
1795 /* And space for contribution to bitmap directory size */
1796 bitmap_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1799 bitmaps_size
+= ROUND_UP(bitmap_dir_size
, cluster_size
);
1801 return bitmaps_size
;