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"
34 /* NOTICE: BME here means Bitmaps Extension and used as a namespace for
35 * _internal_ constants. Please do not use this _internal_ abbreviation for
36 * other needs and/or outside of this file. */
38 /* Bitmap directory entry constraints */
39 #define BME_MAX_TABLE_SIZE 0x8000000
40 #define BME_MAX_PHYS_SIZE 0x20000000 /* restrict BdrvDirtyBitmap size in RAM */
41 #define BME_MAX_GRANULARITY_BITS 31
42 #define BME_MIN_GRANULARITY_BITS 9
43 #define BME_MAX_NAME_SIZE 1023
45 /* Size of bitmap table entries */
46 #define BME_TABLE_ENTRY_SIZE (sizeof(uint64_t))
48 QEMU_BUILD_BUG_ON(BME_MAX_NAME_SIZE
!= BDRV_BITMAP_MAX_NAME_SIZE
);
50 #if BME_MAX_TABLE_SIZE * 8ULL > INT_MAX
51 #error In the code bitmap table physical size assumed to fit into int
54 /* Bitmap directory entry flags */
55 #define BME_RESERVED_FLAGS 0xfffffffcU
56 #define BME_FLAG_IN_USE (1U << 0)
57 #define BME_FLAG_AUTO (1U << 1)
59 /* bits [1, 8] U [56, 63] are reserved */
60 #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001feULL
61 #define BME_TABLE_ENTRY_OFFSET_MASK 0x00fffffffffffe00ULL
62 #define BME_TABLE_ENTRY_FLAG_ALL_ONES (1ULL << 0)
64 typedef struct QEMU_PACKED Qcow2BitmapDirEntry
{
65 /* header is 8 byte aligned */
66 uint64_t bitmap_table_offset
;
68 uint32_t bitmap_table_size
;
72 uint8_t granularity_bits
;
74 uint32_t extra_data_size
;
75 /* extra data follows */
77 } Qcow2BitmapDirEntry
;
79 typedef struct Qcow2BitmapTable
{
81 uint32_t size
; /* number of 64bit entries */
82 QSIMPLEQ_ENTRY(Qcow2BitmapTable
) entry
;
85 typedef struct Qcow2Bitmap
{
86 Qcow2BitmapTable table
;
88 uint8_t granularity_bits
;
91 BdrvDirtyBitmap
*dirty_bitmap
;
93 QSIMPLEQ_ENTRY(Qcow2Bitmap
) entry
;
95 typedef QSIMPLEQ_HEAD(Qcow2BitmapList
, Qcow2Bitmap
) Qcow2BitmapList
;
97 typedef enum BitmapType
{
98 BT_DIRTY_TRACKING_BITMAP
= 1
101 static inline bool can_write(BlockDriverState
*bs
)
103 return !bdrv_is_read_only(bs
) && !(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
);
106 static int update_header_sync(BlockDriverState
*bs
)
110 ret
= qcow2_update_header(bs
);
115 return bdrv_flush(bs
->file
->bs
);
118 static inline void bitmap_table_to_be(uint64_t *bitmap_table
, size_t size
)
122 for (i
= 0; i
< size
; ++i
) {
123 bitmap_table
[i
] = cpu_to_be64(bitmap_table
[i
]);
127 static int check_table_entry(uint64_t entry
, int cluster_size
)
131 if (entry
& BME_TABLE_ENTRY_RESERVED_MASK
) {
135 offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
137 /* if offset specified, bit 0 is reserved */
138 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
142 if (offset
% cluster_size
!= 0) {
150 static int64_t get_bitmap_bytes_needed(int64_t len
, uint32_t granularity
)
152 int64_t num_bits
= DIV_ROUND_UP(len
, granularity
);
154 return DIV_ROUND_UP(num_bits
, 8);
157 static int check_constraints_on_bitmap(BlockDriverState
*bs
,
159 uint32_t granularity
,
162 BDRVQcow2State
*s
= bs
->opaque
;
163 int granularity_bits
= ctz32(granularity
);
164 int64_t len
= bdrv_getlength(bs
);
165 int64_t bitmap_bytes
;
167 assert(granularity
> 0);
168 assert((granularity
& (granularity
- 1)) == 0);
171 error_setg_errno(errp
, -len
, "Failed to get size of '%s'",
172 bdrv_get_device_or_node_name(bs
));
176 if (granularity_bits
> BME_MAX_GRANULARITY_BITS
) {
177 error_setg(errp
, "Granularity exceeds maximum (%llu bytes)",
178 1ULL << BME_MAX_GRANULARITY_BITS
);
181 if (granularity_bits
< BME_MIN_GRANULARITY_BITS
) {
182 error_setg(errp
, "Granularity is under minimum (%llu bytes)",
183 1ULL << BME_MIN_GRANULARITY_BITS
);
187 bitmap_bytes
= get_bitmap_bytes_needed(len
, granularity
);
188 if ((bitmap_bytes
> (uint64_t)BME_MAX_PHYS_SIZE
) ||
189 (bitmap_bytes
> (uint64_t)BME_MAX_TABLE_SIZE
* s
->cluster_size
))
191 error_setg(errp
, "Too much space will be occupied by the bitmap. "
192 "Use larger granularity");
196 if (strlen(name
) > BME_MAX_NAME_SIZE
) {
197 error_setg(errp
, "Name length exceeds maximum (%u characters)",
205 static void clear_bitmap_table(BlockDriverState
*bs
, uint64_t *bitmap_table
,
206 uint32_t bitmap_table_size
)
208 BDRVQcow2State
*s
= bs
->opaque
;
211 for (i
= 0; i
< bitmap_table_size
; ++i
) {
212 uint64_t addr
= bitmap_table
[i
] & BME_TABLE_ENTRY_OFFSET_MASK
;
217 qcow2_free_clusters(bs
, addr
, s
->cluster_size
, QCOW2_DISCARD_ALWAYS
);
222 static int bitmap_table_load(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
,
223 uint64_t **bitmap_table
)
226 BDRVQcow2State
*s
= bs
->opaque
;
230 assert(tb
->size
!= 0);
231 table
= g_try_new(uint64_t, tb
->size
);
236 assert(tb
->size
<= BME_MAX_TABLE_SIZE
);
237 ret
= bdrv_pread(bs
->file
, tb
->offset
,
238 table
, tb
->size
* BME_TABLE_ENTRY_SIZE
);
243 for (i
= 0; i
< tb
->size
; ++i
) {
244 table
[i
] = be64_to_cpu(table
[i
]);
245 ret
= check_table_entry(table
[i
], s
->cluster_size
);
251 *bitmap_table
= table
;
260 static int free_bitmap_clusters(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
)
263 uint64_t *bitmap_table
;
265 ret
= bitmap_table_load(bs
, tb
, &bitmap_table
);
270 clear_bitmap_table(bs
, bitmap_table
, tb
->size
);
271 qcow2_free_clusters(bs
, tb
->offset
, tb
->size
* BME_TABLE_ENTRY_SIZE
,
272 QCOW2_DISCARD_OTHER
);
273 g_free(bitmap_table
);
281 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
282 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State
*s
,
283 const BdrvDirtyBitmap
*bitmap
)
285 uint64_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
286 uint64_t limit
= granularity
* (s
->cluster_size
<< 3);
288 assert(QEMU_IS_ALIGNED(limit
,
289 bdrv_dirty_bitmap_serialization_align(bitmap
)));
294 * @bitmap_table entries must satisfy specification constraints.
295 * @bitmap must be cleared */
296 static int load_bitmap_data(BlockDriverState
*bs
,
297 const uint64_t *bitmap_table
,
298 uint32_t bitmap_table_size
,
299 BdrvDirtyBitmap
*bitmap
)
302 BDRVQcow2State
*s
= bs
->opaque
;
303 uint64_t offset
, limit
;
304 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
306 uint64_t i
, tab_size
=
308 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
310 if (tab_size
!= bitmap_table_size
|| tab_size
> BME_MAX_TABLE_SIZE
) {
314 buf
= g_malloc(s
->cluster_size
);
315 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
316 for (i
= 0, offset
= 0; i
< tab_size
; ++i
, offset
+= limit
) {
317 uint64_t count
= MIN(bm_size
- offset
, limit
);
318 uint64_t entry
= bitmap_table
[i
];
319 uint64_t data_offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
321 assert(check_table_entry(entry
, s
->cluster_size
) == 0);
323 if (data_offset
== 0) {
324 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
325 bdrv_dirty_bitmap_deserialize_ones(bitmap
, offset
, count
,
328 /* No need to deserialize zeros because the dirty bitmap is
332 ret
= bdrv_pread(bs
->file
, data_offset
, buf
, s
->cluster_size
);
336 bdrv_dirty_bitmap_deserialize_part(bitmap
, buf
, offset
, count
,
342 bdrv_dirty_bitmap_deserialize_finish(bitmap
);
350 static BdrvDirtyBitmap
*load_bitmap(BlockDriverState
*bs
,
351 Qcow2Bitmap
*bm
, Error
**errp
)
354 uint64_t *bitmap_table
= NULL
;
355 uint32_t granularity
;
356 BdrvDirtyBitmap
*bitmap
= NULL
;
358 granularity
= 1U << bm
->granularity_bits
;
359 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, bm
->name
, errp
);
360 if (bitmap
== NULL
) {
364 if (bm
->flags
& BME_FLAG_IN_USE
) {
365 /* Data is unusable, skip loading it */
369 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
371 error_setg_errno(errp
, -ret
,
372 "Could not read bitmap_table table from image for "
373 "bitmap '%s'", bm
->name
);
377 ret
= load_bitmap_data(bs
, bitmap_table
, bm
->table
.size
, bitmap
);
379 error_setg_errno(errp
, -ret
, "Could not read bitmap '%s' from image",
384 g_free(bitmap_table
);
388 g_free(bitmap_table
);
389 if (bitmap
!= NULL
) {
390 bdrv_release_dirty_bitmap(bitmap
);
401 * Bitmap List private functions
402 * Only Bitmap List knows about bitmap directory structure in Qcow2.
405 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry
*entry
)
407 entry
->bitmap_table_offset
= be64_to_cpu(entry
->bitmap_table_offset
);
408 entry
->bitmap_table_size
= be32_to_cpu(entry
->bitmap_table_size
);
409 entry
->flags
= be32_to_cpu(entry
->flags
);
410 entry
->name_size
= be16_to_cpu(entry
->name_size
);
411 entry
->extra_data_size
= be32_to_cpu(entry
->extra_data_size
);
414 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry
*entry
)
416 entry
->bitmap_table_offset
= cpu_to_be64(entry
->bitmap_table_offset
);
417 entry
->bitmap_table_size
= cpu_to_be32(entry
->bitmap_table_size
);
418 entry
->flags
= cpu_to_be32(entry
->flags
);
419 entry
->name_size
= cpu_to_be16(entry
->name_size
);
420 entry
->extra_data_size
= cpu_to_be32(entry
->extra_data_size
);
423 static inline int calc_dir_entry_size(size_t name_size
, size_t extra_data_size
)
425 int size
= sizeof(Qcow2BitmapDirEntry
) + name_size
+ extra_data_size
;
426 return ROUND_UP(size
, 8);
429 static inline int dir_entry_size(Qcow2BitmapDirEntry
*entry
)
431 return calc_dir_entry_size(entry
->name_size
, entry
->extra_data_size
);
434 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry
*entry
)
436 return (const char *)(entry
+ 1) + entry
->extra_data_size
;
439 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry
*entry
)
441 const char *name_field
= dir_entry_name_field(entry
);
442 return g_strndup(name_field
, entry
->name_size
);
445 static inline Qcow2BitmapDirEntry
*next_dir_entry(Qcow2BitmapDirEntry
*entry
)
447 return (Qcow2BitmapDirEntry
*)((uint8_t *)entry
+ dir_entry_size(entry
));
450 static int check_dir_entry(BlockDriverState
*bs
, Qcow2BitmapDirEntry
*entry
)
452 BDRVQcow2State
*s
= bs
->opaque
;
453 uint64_t phys_bitmap_bytes
;
456 bool fail
= (entry
->bitmap_table_size
== 0) ||
457 (entry
->bitmap_table_offset
== 0) ||
458 (entry
->bitmap_table_offset
% s
->cluster_size
) ||
459 (entry
->bitmap_table_size
> BME_MAX_TABLE_SIZE
) ||
460 (entry
->granularity_bits
> BME_MAX_GRANULARITY_BITS
) ||
461 (entry
->granularity_bits
< BME_MIN_GRANULARITY_BITS
) ||
462 (entry
->flags
& BME_RESERVED_FLAGS
) ||
463 (entry
->name_size
> BME_MAX_NAME_SIZE
) ||
464 (entry
->type
!= BT_DIRTY_TRACKING_BITMAP
);
470 phys_bitmap_bytes
= (uint64_t)entry
->bitmap_table_size
* s
->cluster_size
;
471 len
= bdrv_getlength(bs
);
477 if (phys_bitmap_bytes
> BME_MAX_PHYS_SIZE
) {
481 if (!(entry
->flags
& BME_FLAG_IN_USE
) &&
482 (len
> ((phys_bitmap_bytes
* 8) << entry
->granularity_bits
)))
485 * We've loaded a valid bitmap (IN_USE not set) or we are going to
486 * store a valid bitmap, but the allocated bitmap table size is not
487 * enough to store this bitmap.
489 * Note, that it's OK to have an invalid bitmap with invalid size due
490 * to a bitmap that was not correctly saved after image resize.
498 static inline void bitmap_directory_to_be(uint8_t *dir
, size_t size
)
500 uint8_t *end
= dir
+ size
;
502 Qcow2BitmapDirEntry
*e
= (Qcow2BitmapDirEntry
*)dir
;
503 dir
+= dir_entry_size(e
);
505 bitmap_dir_entry_to_be(e
);
510 * Bitmap List public functions
513 static void bitmap_free(Qcow2Bitmap
*bm
)
523 static void bitmap_list_free(Qcow2BitmapList
*bm_list
)
527 if (bm_list
== NULL
) {
531 while ((bm
= QSIMPLEQ_FIRST(bm_list
)) != NULL
) {
532 QSIMPLEQ_REMOVE_HEAD(bm_list
, entry
);
539 static Qcow2BitmapList
*bitmap_list_new(void)
541 Qcow2BitmapList
*bm_list
= g_new(Qcow2BitmapList
, 1);
542 QSIMPLEQ_INIT(bm_list
);
547 static uint32_t bitmap_list_count(Qcow2BitmapList
*bm_list
)
550 uint32_t nb_bitmaps
= 0;
552 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
560 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
561 * checks it and convert to bitmap list.
563 static Qcow2BitmapList
*bitmap_list_load(BlockDriverState
*bs
, uint64_t offset
,
564 uint64_t size
, Error
**errp
)
567 BDRVQcow2State
*s
= bs
->opaque
;
568 uint8_t *dir
, *dir_end
;
569 Qcow2BitmapDirEntry
*e
;
570 uint32_t nb_dir_entries
= 0;
571 Qcow2BitmapList
*bm_list
= NULL
;
574 error_setg(errp
, "Requested bitmap directory size is zero");
578 if (size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
579 error_setg(errp
, "Requested bitmap directory size is too big");
583 dir
= g_try_malloc(size
);
585 error_setg(errp
, "Failed to allocate space for bitmap directory");
588 dir_end
= dir
+ size
;
590 ret
= bdrv_pread(bs
->file
, offset
, dir
, size
);
592 error_setg_errno(errp
, -ret
, "Failed to read bitmap directory");
596 bm_list
= bitmap_list_new();
597 for (e
= (Qcow2BitmapDirEntry
*)dir
;
598 e
< (Qcow2BitmapDirEntry
*)dir_end
;
599 e
= next_dir_entry(e
))
603 if ((uint8_t *)(e
+ 1) > dir_end
) {
607 if (++nb_dir_entries
> s
->nb_bitmaps
) {
608 error_setg(errp
, "More bitmaps found than specified in header"
612 bitmap_dir_entry_to_cpu(e
);
614 if ((uint8_t *)next_dir_entry(e
) > dir_end
) {
618 if (e
->extra_data_size
!= 0) {
619 error_setg(errp
, "Bitmap extra data is not supported");
623 ret
= check_dir_entry(bs
, e
);
625 error_setg(errp
, "Bitmap '%.*s' doesn't satisfy the constraints",
626 e
->name_size
, dir_entry_name_field(e
));
630 bm
= g_new0(Qcow2Bitmap
, 1);
631 bm
->table
.offset
= e
->bitmap_table_offset
;
632 bm
->table
.size
= e
->bitmap_table_size
;
633 bm
->flags
= e
->flags
;
634 bm
->granularity_bits
= e
->granularity_bits
;
635 bm
->name
= dir_entry_copy_name(e
);
636 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
639 if (nb_dir_entries
!= s
->nb_bitmaps
) {
640 error_setg(errp
, "Less bitmaps found than specified in header"
645 if ((uint8_t *)e
!= dir_end
) {
653 error_setg(errp
, "Broken bitmap directory");
657 bitmap_list_free(bm_list
);
662 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
663 void **refcount_table
,
664 int64_t *refcount_table_size
)
667 BDRVQcow2State
*s
= bs
->opaque
;
668 Qcow2BitmapList
*bm_list
;
671 if (s
->nb_bitmaps
== 0) {
675 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
676 s
->bitmap_directory_offset
,
677 s
->bitmap_directory_size
);
682 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
683 s
->bitmap_directory_size
, NULL
);
684 if (bm_list
== NULL
) {
689 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
690 uint64_t *bitmap_table
= NULL
;
693 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
694 refcount_table
, refcount_table_size
,
696 bm
->table
.size
* BME_TABLE_ENTRY_SIZE
);
701 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
707 for (i
= 0; i
< bm
->table
.size
; ++i
) {
708 uint64_t entry
= bitmap_table
[i
];
709 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
711 if (check_table_entry(entry
, s
->cluster_size
) < 0) {
720 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
721 refcount_table
, refcount_table_size
,
722 offset
, s
->cluster_size
);
724 g_free(bitmap_table
);
729 g_free(bitmap_table
);
733 bitmap_list_free(bm_list
);
739 * Store bitmap list to qcow2 image as a bitmap directory.
740 * Everything is checked.
742 static int bitmap_list_store(BlockDriverState
*bs
, Qcow2BitmapList
*bm_list
,
743 uint64_t *offset
, uint64_t *size
, bool in_place
)
747 int64_t dir_offset
= 0;
748 uint64_t dir_size
= 0;
750 Qcow2BitmapDirEntry
*e
;
752 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
753 dir_size
+= calc_dir_entry_size(strlen(bm
->name
), 0);
756 if (dir_size
== 0 || dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
761 if (*size
!= dir_size
|| *offset
== 0) {
765 dir_offset
= *offset
;
768 dir
= g_try_malloc0(dir_size
);
773 e
= (Qcow2BitmapDirEntry
*)dir
;
774 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
775 e
->bitmap_table_offset
= bm
->table
.offset
;
776 e
->bitmap_table_size
= bm
->table
.size
;
777 e
->flags
= bm
->flags
;
778 e
->type
= BT_DIRTY_TRACKING_BITMAP
;
779 e
->granularity_bits
= bm
->granularity_bits
;
780 e
->name_size
= strlen(bm
->name
);
781 e
->extra_data_size
= 0;
782 memcpy(e
+ 1, bm
->name
, e
->name_size
);
784 if (check_dir_entry(bs
, e
) < 0) {
789 e
= next_dir_entry(e
);
792 bitmap_directory_to_be(dir
, dir_size
);
795 dir_offset
= qcow2_alloc_clusters(bs
, dir_size
);
796 if (dir_offset
< 0) {
802 /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
803 * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
804 * directory in-place (actually, turn-off the extension), which is checked
805 * in qcow2_check_metadata_overlap() */
806 ret
= qcow2_pre_write_overlap_check(
807 bs
, in_place
? QCOW2_OL_BITMAP_DIRECTORY
: 0, dir_offset
, dir_size
,
813 ret
= bdrv_pwrite(bs
->file
, dir_offset
, dir
, dir_size
);
822 *offset
= dir_offset
;
830 if (!in_place
&& dir_offset
> 0) {
831 qcow2_free_clusters(bs
, dir_offset
, dir_size
, QCOW2_DISCARD_OTHER
);
841 static int update_ext_header_and_dir_in_place(BlockDriverState
*bs
,
842 Qcow2BitmapList
*bm_list
)
844 BDRVQcow2State
*s
= bs
->opaque
;
847 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
) ||
848 bm_list
== NULL
|| QSIMPLEQ_EMPTY(bm_list
) ||
849 bitmap_list_count(bm_list
) != s
->nb_bitmaps
)
854 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
855 ret
= update_header_sync(bs
);
857 /* Two variants are possible here:
858 * 1. Autoclear flag is dropped, all bitmaps will be lost.
859 * 2. Autoclear flag is not dropped, old state is left.
864 /* autoclear bit is not set, so we can safely update bitmap directory */
866 ret
= bitmap_list_store(bs
, bm_list
, &s
->bitmap_directory_offset
,
867 &s
->bitmap_directory_size
, true);
869 /* autoclear bit is cleared, so all leaked clusters would be removed on
874 ret
= update_header_sync(bs
);
876 /* autoclear bit is cleared, so all leaked clusters would be removed on
881 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
882 return update_header_sync(bs
);
883 /* If final update_header_sync() fails, two variants are possible:
884 * 1. Autoclear flag is not set, all bitmaps will be lost.
885 * 2. Autoclear flag is set, header and directory are successfully updated.
889 static int update_ext_header_and_dir(BlockDriverState
*bs
,
890 Qcow2BitmapList
*bm_list
)
892 BDRVQcow2State
*s
= bs
->opaque
;
894 uint64_t new_offset
= 0;
895 uint64_t new_size
= 0;
896 uint32_t new_nb_bitmaps
= 0;
897 uint64_t old_offset
= s
->bitmap_directory_offset
;
898 uint64_t old_size
= s
->bitmap_directory_size
;
899 uint32_t old_nb_bitmaps
= s
->nb_bitmaps
;
900 uint64_t old_autocl
= s
->autoclear_features
;
902 if (bm_list
!= NULL
&& !QSIMPLEQ_EMPTY(bm_list
)) {
903 new_nb_bitmaps
= bitmap_list_count(bm_list
);
905 if (new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
909 ret
= bitmap_list_store(bs
, bm_list
, &new_offset
, &new_size
, false);
914 ret
= qcow2_flush_caches(bs
);
919 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
921 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
924 s
->bitmap_directory_offset
= new_offset
;
925 s
->bitmap_directory_size
= new_size
;
926 s
->nb_bitmaps
= new_nb_bitmaps
;
928 ret
= update_header_sync(bs
);
934 qcow2_free_clusters(bs
, old_offset
, old_size
, QCOW2_DISCARD_OTHER
);
940 if (new_offset
> 0) {
941 qcow2_free_clusters(bs
, new_offset
, new_size
, QCOW2_DISCARD_OTHER
);
944 s
->bitmap_directory_offset
= old_offset
;
945 s
->bitmap_directory_size
= old_size
;
946 s
->nb_bitmaps
= old_nb_bitmaps
;
947 s
->autoclear_features
= old_autocl
;
952 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
953 static void release_dirty_bitmap_helper(gpointer bitmap
,
956 bdrv_release_dirty_bitmap(bitmap
);
959 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
960 static void set_readonly_helper(gpointer bitmap
, gpointer value
)
962 bdrv_dirty_bitmap_set_readonly(bitmap
, (bool)value
);
965 /* qcow2_load_dirty_bitmaps()
966 * Return value is a hint for caller: true means that the Qcow2 header was
967 * updated. (false doesn't mean that the header should be updated by the
968 * caller, it just means that updating was not needed or the image cannot be
970 * On failure the function returns false.
972 bool qcow2_load_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
974 BDRVQcow2State
*s
= bs
->opaque
;
975 Qcow2BitmapList
*bm_list
;
977 GSList
*created_dirty_bitmaps
= NULL
;
978 bool header_updated
= false;
979 bool needs_update
= false;
981 if (s
->nb_bitmaps
== 0) {
982 /* No bitmaps - nothing to do */
986 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
987 s
->bitmap_directory_size
, errp
);
988 if (bm_list
== NULL
) {
992 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
993 BdrvDirtyBitmap
*bitmap
;
995 if ((bm
->flags
& BME_FLAG_IN_USE
) &&
996 bdrv_find_dirty_bitmap(bs
, bm
->name
))
999 * We already have corresponding BdrvDirtyBitmap, and bitmap in the
1000 * image is marked IN_USE. Firstly, this state is valid, no reason
1001 * to consider existing BdrvDirtyBitmap to be bad. Secondly it's
1002 * absolutely possible, when we do migration with shared storage
1003 * with dirty-bitmaps capability enabled: if the bitmap was loaded
1004 * from this storage before migration start, the storage will
1005 * of-course contain IN_USE outdated version of the bitmap, and we
1006 * should not load it on migration target, as we already have this
1007 * bitmap, being migrated.
1012 bitmap
= load_bitmap(bs
, bm
, errp
);
1013 if (bitmap
== NULL
) {
1017 bdrv_dirty_bitmap_set_persistence(bitmap
, true);
1018 if (bm
->flags
& BME_FLAG_IN_USE
) {
1019 bdrv_dirty_bitmap_set_inconsistent(bitmap
);
1021 /* NB: updated flags only get written if can_write(bs) is true. */
1022 bm
->flags
|= BME_FLAG_IN_USE
;
1023 needs_update
= true;
1025 if (!(bm
->flags
& BME_FLAG_AUTO
)) {
1026 bdrv_disable_dirty_bitmap(bitmap
);
1028 created_dirty_bitmaps
=
1029 g_slist_append(created_dirty_bitmaps
, bitmap
);
1032 if (needs_update
&& can_write(bs
)) {
1033 /* in_use flags must be updated */
1034 int ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1036 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
1039 header_updated
= true;
1042 if (!can_write(bs
)) {
1043 g_slist_foreach(created_dirty_bitmaps
, set_readonly_helper
,
1047 g_slist_free(created_dirty_bitmaps
);
1048 bitmap_list_free(bm_list
);
1050 return header_updated
;
1053 g_slist_foreach(created_dirty_bitmaps
, release_dirty_bitmap_helper
, bs
);
1054 g_slist_free(created_dirty_bitmaps
);
1055 bitmap_list_free(bm_list
);
1061 static Qcow2BitmapInfoFlagsList
*get_bitmap_info_flags(uint32_t flags
)
1063 Qcow2BitmapInfoFlagsList
*list
= NULL
;
1064 Qcow2BitmapInfoFlagsList
**tail
= &list
;
1067 static const struct {
1068 int bme
; /* Bitmap directory entry flags */
1069 int info
; /* The flags to report to the user */
1071 { BME_FLAG_IN_USE
, QCOW2_BITMAP_INFO_FLAGS_IN_USE
},
1072 { BME_FLAG_AUTO
, QCOW2_BITMAP_INFO_FLAGS_AUTO
},
1075 int map_size
= ARRAY_SIZE(map
);
1077 for (i
= 0; i
< map_size
; ++i
) {
1078 if (flags
& map
[i
].bme
) {
1079 QAPI_LIST_APPEND(tail
, map
[i
].info
);
1080 flags
&= ~map
[i
].bme
;
1083 /* Check if the BME_* mapping above is complete */
1090 * qcow2_get_bitmap_info_list()
1091 * Returns a list of QCOW2 bitmap details.
1092 * In case of no bitmaps, the function returns NULL and
1093 * the @errp parameter is not set.
1094 * When bitmap information can not be obtained, the function returns
1095 * NULL and the @errp parameter is set.
1097 Qcow2BitmapInfoList
*qcow2_get_bitmap_info_list(BlockDriverState
*bs
,
1100 BDRVQcow2State
*s
= bs
->opaque
;
1101 Qcow2BitmapList
*bm_list
;
1103 Qcow2BitmapInfoList
*list
= NULL
;
1104 Qcow2BitmapInfoList
**tail
= &list
;
1106 if (s
->nb_bitmaps
== 0) {
1110 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1111 s
->bitmap_directory_size
, errp
);
1112 if (bm_list
== NULL
) {
1116 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1117 Qcow2BitmapInfo
*info
= g_new0(Qcow2BitmapInfo
, 1);
1118 info
->granularity
= 1U << bm
->granularity_bits
;
1119 info
->name
= g_strdup(bm
->name
);
1120 info
->flags
= get_bitmap_info_flags(bm
->flags
& ~BME_RESERVED_FLAGS
);
1121 QAPI_LIST_APPEND(tail
, info
);
1124 bitmap_list_free(bm_list
);
1129 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
)
1131 BDRVQcow2State
*s
= bs
->opaque
;
1132 Qcow2BitmapList
*bm_list
;
1134 GSList
*ro_dirty_bitmaps
= NULL
;
1136 bool need_header_update
= false;
1138 if (s
->nb_bitmaps
== 0) {
1139 /* No bitmaps - nothing to do */
1143 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1144 s
->bitmap_directory_size
, errp
);
1145 if (bm_list
== NULL
) {
1149 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1150 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1153 error_setg(errp
, "Unexpected bitmap '%s' in image '%s'",
1154 bm
->name
, bs
->filename
);
1158 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1159 if (!bdrv_dirty_bitmap_readonly(bitmap
)) {
1160 error_setg(errp
, "Corruption: bitmap '%s' is not marked IN_USE "
1161 "in the image '%s' and not marked readonly in RAM",
1162 bm
->name
, bs
->filename
);
1165 if (bdrv_dirty_bitmap_inconsistent(bitmap
)) {
1166 error_setg(errp
, "Corruption: bitmap '%s' is inconsistent but "
1167 "is not marked IN_USE in the image '%s'", bm
->name
,
1172 bm
->flags
|= BME_FLAG_IN_USE
;
1173 need_header_update
= true;
1176 * What if flags already has BME_FLAG_IN_USE ?
1178 * 1. if we are reopening RW -> RW it's OK, of course.
1179 * 2. if we are reopening RO -> RW:
1180 * 2.1 if @bitmap is inconsistent, it's OK. It means that it was
1181 * inconsistent (IN_USE) when we loaded it
1182 * 2.2 if @bitmap is not inconsistent. This seems to be impossible
1183 * and implies third party interaction. Let's error-out for
1186 if (bdrv_dirty_bitmap_readonly(bitmap
) &&
1187 !bdrv_dirty_bitmap_inconsistent(bitmap
))
1189 error_setg(errp
, "Corruption: bitmap '%s' is marked IN_USE "
1190 "in the image '%s' but it is readonly and "
1191 "consistent in RAM",
1192 bm
->name
, bs
->filename
);
1197 if (bdrv_dirty_bitmap_readonly(bitmap
)) {
1198 ro_dirty_bitmaps
= g_slist_append(ro_dirty_bitmaps
, bitmap
);
1202 if (need_header_update
) {
1203 if (!can_write(bs
->file
->bs
) || !(bs
->file
->perm
& BLK_PERM_WRITE
)) {
1204 error_setg(errp
, "Failed to reopen bitmaps rw: no write access "
1205 "the protocol file");
1209 /* in_use flags must be updated */
1210 ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1212 error_setg_errno(errp
, -ret
, "Cannot update bitmap directory");
1217 g_slist_foreach(ro_dirty_bitmaps
, set_readonly_helper
, false);
1221 g_slist_free(ro_dirty_bitmaps
);
1222 bitmap_list_free(bm_list
);
1227 /* Checks to see if it's safe to resize bitmaps */
1228 int qcow2_truncate_bitmaps_check(BlockDriverState
*bs
, Error
**errp
)
1230 BDRVQcow2State
*s
= bs
->opaque
;
1231 Qcow2BitmapList
*bm_list
;
1235 if (s
->nb_bitmaps
== 0) {
1239 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1240 s
->bitmap_directory_size
, errp
);
1241 if (bm_list
== NULL
) {
1245 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1246 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1247 if (bitmap
== NULL
) {
1249 * We rely on all bitmaps being in-memory to be able to resize them,
1250 * Otherwise, we'd need to resize them on disk explicitly
1252 error_setg(errp
, "Cannot resize qcow2 with persistent bitmaps that "
1253 "were not loaded into memory");
1259 * The checks against readonly and busy are redundant, but certainly
1260 * do no harm. checks against inconsistent are crucial:
1262 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
1269 bitmap_list_free(bm_list
);
1273 /* store_bitmap_data()
1274 * Store bitmap to image, filling bitmap table accordingly.
1276 static uint64_t *store_bitmap_data(BlockDriverState
*bs
,
1277 BdrvDirtyBitmap
*bitmap
,
1278 uint32_t *bitmap_table_size
, Error
**errp
)
1281 BDRVQcow2State
*s
= bs
->opaque
;
1284 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
1285 const char *bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1286 uint8_t *buf
= NULL
;
1290 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
1292 if (tb_size
> BME_MAX_TABLE_SIZE
||
1293 tb_size
* s
->cluster_size
> BME_MAX_PHYS_SIZE
)
1295 error_setg(errp
, "Bitmap '%s' is too big", bm_name
);
1299 tb
= g_try_new0(uint64_t, tb_size
);
1301 error_setg(errp
, "No memory");
1305 buf
= g_malloc(s
->cluster_size
);
1306 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
1307 assert(DIV_ROUND_UP(bm_size
, limit
) == tb_size
);
1310 while ((offset
= bdrv_dirty_bitmap_next_dirty(bitmap
, offset
, INT64_MAX
))
1313 uint64_t cluster
= offset
/ limit
;
1314 uint64_t end
, write_size
;
1318 * We found the first dirty offset, but want to write out the
1319 * entire cluster of the bitmap that includes that offset,
1320 * including any leading zero bits.
1322 offset
= QEMU_ALIGN_DOWN(offset
, limit
);
1323 end
= MIN(bm_size
, offset
+ limit
);
1324 write_size
= bdrv_dirty_bitmap_serialization_size(bitmap
, offset
,
1326 assert(write_size
<= s
->cluster_size
);
1328 off
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1330 error_setg_errno(errp
, -off
,
1331 "Failed to allocate clusters for bitmap '%s'",
1337 bdrv_dirty_bitmap_serialize_part(bitmap
, buf
, offset
, end
- offset
);
1338 if (write_size
< s
->cluster_size
) {
1339 memset(buf
+ write_size
, 0, s
->cluster_size
- write_size
);
1342 ret
= qcow2_pre_write_overlap_check(bs
, 0, off
, s
->cluster_size
, false);
1344 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1348 ret
= bdrv_pwrite(bs
->file
, off
, buf
, s
->cluster_size
);
1350 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1358 *bitmap_table_size
= tb_size
;
1364 clear_bitmap_table(bs
, tb
, tb_size
);
1372 * Store bm->dirty_bitmap to qcow2.
1373 * Set bm->table_offset and bm->table_size accordingly.
1375 static int store_bitmap(BlockDriverState
*bs
, Qcow2Bitmap
*bm
, Error
**errp
)
1381 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1382 const char *bm_name
;
1384 assert(bitmap
!= NULL
);
1386 bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1388 tb
= store_bitmap_data(bs
, bitmap
, &tb_size
, errp
);
1393 assert(tb_size
<= BME_MAX_TABLE_SIZE
);
1394 tb_offset
= qcow2_alloc_clusters(bs
, tb_size
* sizeof(tb
[0]));
1395 if (tb_offset
< 0) {
1396 error_setg_errno(errp
, -tb_offset
,
1397 "Failed to allocate clusters for bitmap '%s'",
1403 ret
= qcow2_pre_write_overlap_check(bs
, 0, tb_offset
,
1404 tb_size
* sizeof(tb
[0]), false);
1406 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1410 bitmap_table_to_be(tb
, tb_size
);
1411 ret
= bdrv_pwrite(bs
->file
, tb_offset
, tb
, tb_size
* sizeof(tb
[0]));
1413 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1420 bm
->table
.offset
= tb_offset
;
1421 bm
->table
.size
= tb_size
;
1426 clear_bitmap_table(bs
, tb
, tb_size
);
1428 if (tb_offset
> 0) {
1429 qcow2_free_clusters(bs
, tb_offset
, tb_size
* sizeof(tb
[0]),
1430 QCOW2_DISCARD_OTHER
);
1438 static Qcow2Bitmap
*find_bitmap_by_name(Qcow2BitmapList
*bm_list
,
1443 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1444 if (strcmp(name
, bm
->name
) == 0) {
1452 int coroutine_fn
qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
1457 BDRVQcow2State
*s
= bs
->opaque
;
1458 Qcow2Bitmap
*bm
= NULL
;
1459 Qcow2BitmapList
*bm_list
;
1461 if (s
->nb_bitmaps
== 0) {
1463 * Absence of the bitmap is not an error: see explanation above
1464 * bdrv_co_remove_persistent_dirty_bitmap() definition.
1469 qemu_co_mutex_lock(&s
->lock
);
1471 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1472 s
->bitmap_directory_size
, errp
);
1473 if (bm_list
== NULL
) {
1478 bm
= find_bitmap_by_name(bm_list
, name
);
1480 /* Absence of the bitmap is not an error, see above. */
1485 QSIMPLEQ_REMOVE(bm_list
, bm
, Qcow2Bitmap
, entry
);
1487 ret
= update_ext_header_and_dir(bs
, bm_list
);
1489 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1493 free_bitmap_clusters(bs
, &bm
->table
);
1496 qemu_co_mutex_unlock(&s
->lock
);
1499 bitmap_list_free(bm_list
);
1505 * qcow2_store_persistent_dirty_bitmaps
1507 * Stores persistent BdrvDirtyBitmap objects.
1509 * @release_stored: if true, release BdrvDirtyBitmap's after storing to the
1510 * image. This is used in two cases, both via qcow2_inactivate:
1511 * 1. bdrv_close: It's correct to remove bitmaps on close.
1512 * 2. migration: If bitmaps are migrated through migration channel via
1513 * 'dirty-bitmaps' migration capability they are not handled by this code.
1514 * Otherwise, it's OK to drop BdrvDirtyBitmap's and reload them on
1517 * Anyway, it's correct to remove BdrvDirtyBitmap's on inactivation, as
1518 * inactivation means that we lose control on disk, and therefore on bitmaps,
1519 * we should sync them and do not touch more.
1521 * Contrariwise, we don't want to release any bitmaps on just reopen-to-ro,
1522 * when we need to store them, as image is still under our control, and it's
1523 * good to keep all the bitmaps in read-only mode. Moreover, keeping them
1524 * read-only is correct because this is what would happen if we opened the node
1525 * readonly to begin with, and whether we opened directly or reopened to that
1526 * state shouldn't matter for the state we get afterward.
1528 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
,
1529 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
);
1560 if (!bdrv_dirty_bitmap_get_persistence(bitmap
) ||
1561 bdrv_dirty_bitmap_inconsistent(bitmap
)) {
1565 if (bdrv_dirty_bitmap_readonly(bitmap
)) {
1567 * Store the bitmap in the associated Qcow2Bitmap so it
1568 * can be released later
1570 bm
= find_bitmap_by_name(bm_list
, name
);
1572 bm
->dirty_bitmap
= bitmap
;
1579 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) < 0) {
1580 error_prepend(errp
, "Bitmap '%s' doesn't satisfy the constraints: ",
1585 bm
= find_bitmap_by_name(bm_list
, name
);
1587 if (++new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1588 error_setg(errp
, "Too many persistent bitmaps");
1592 new_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1593 if (new_dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1594 error_setg(errp
, "Bitmap directory is too large");
1598 bm
= g_new0(Qcow2Bitmap
, 1);
1599 bm
->name
= g_strdup(name
);
1600 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
1602 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1603 error_setg(errp
, "Bitmap '%s' already exists in the image",
1607 tb
= g_memdup(&bm
->table
, sizeof(bm
->table
));
1608 bm
->table
.offset
= 0;
1610 QSIMPLEQ_INSERT_TAIL(&drop_tables
, tb
, entry
);
1612 bm
->flags
= bdrv_dirty_bitmap_enabled(bitmap
) ? BME_FLAG_AUTO
: 0;
1613 bm
->granularity_bits
= ctz32(bdrv_dirty_bitmap_granularity(bitmap
));
1614 bm
->dirty_bitmap
= bitmap
;
1621 if (!can_write(bs
)) {
1622 error_setg(errp
, "No write access");
1626 /* allocate clusters and store bitmaps */
1627 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1628 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1630 if (bitmap
== NULL
|| bdrv_dirty_bitmap_readonly(bitmap
)) {
1634 ret
= store_bitmap(bs
, bm
, errp
);
1640 ret
= update_ext_header_and_dir(bs
, bm_list
);
1642 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1646 /* Bitmap directory was successfully updated, so, old data can be dropped.
1647 * TODO it is better to reuse these clusters */
1648 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1649 free_bitmap_clusters(bs
, tb
);
1654 if (release_stored
) {
1655 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1656 if (bm
->dirty_bitmap
== NULL
) {
1660 bdrv_release_dirty_bitmap(bm
->dirty_bitmap
);
1664 bitmap_list_free(bm_list
);
1668 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1669 if (bm
->dirty_bitmap
== NULL
|| bm
->table
.offset
== 0 ||
1670 bdrv_dirty_bitmap_readonly(bm
->dirty_bitmap
))
1675 free_bitmap_clusters(bs
, &bm
->table
);
1678 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1682 bitmap_list_free(bm_list
);
1685 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
)
1687 BdrvDirtyBitmap
*bitmap
;
1688 Error
*local_err
= NULL
;
1690 qcow2_store_persistent_dirty_bitmaps(bs
, false, &local_err
);
1691 if (local_err
!= NULL
) {
1692 error_propagate(errp
, local_err
);
1696 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
1697 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
1698 bdrv_dirty_bitmap_set_readonly(bitmap
, true);
1705 bool coroutine_fn
qcow2_co_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
1707 uint32_t granularity
,
1710 BDRVQcow2State
*s
= bs
->opaque
;
1711 BdrvDirtyBitmap
*bitmap
;
1712 uint64_t bitmap_directory_size
= 0;
1713 uint32_t nb_bitmaps
= 0;
1715 if (bdrv_find_dirty_bitmap(bs
, name
)) {
1716 error_setg(errp
, "Bitmap already exists: %s", name
);
1720 if (s
->qcow_version
< 3) {
1721 /* Without autoclear_features, we would always have to assume
1722 * that a program without persistent dirty bitmap support has
1723 * accessed this qcow2 file when opening it, and would thus
1724 * have to drop all dirty bitmaps (defeating their purpose).
1726 error_setg(errp
, "Cannot store dirty bitmaps in qcow2 v2 files");
1730 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) != 0) {
1734 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
1735 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
1737 bitmap_directory_size
+=
1738 calc_dir_entry_size(strlen(bdrv_dirty_bitmap_name(bitmap
)), 0);
1742 bitmap_directory_size
+= calc_dir_entry_size(strlen(name
), 0);
1744 if (nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1746 "Maximum number of persistent bitmaps is already reached");
1750 if (bitmap_directory_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1751 error_setg(errp
, "Not enough space in the bitmap directory");
1758 error_prepend(errp
, "Can't make bitmap '%s' persistent in '%s': ",
1759 name
, bdrv_get_device_or_node_name(bs
));
1763 bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState
*bs
)
1765 BDRVQcow2State
*s
= bs
->opaque
;
1767 return s
->qcow_version
>= 3;
1771 * Compute the space required to copy bitmaps from @in_bs.
1773 * The computation is based as if copying to a new image with the
1774 * given @cluster_size, which may differ from the cluster size in
1775 * @in_bs; in fact, @in_bs might be something other than qcow2.
1777 uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState
*in_bs
,
1778 uint32_t cluster_size
)
1780 uint64_t bitmaps_size
= 0;
1781 BdrvDirtyBitmap
*bm
;
1782 size_t bitmap_dir_size
= 0;
1784 FOR_EACH_DIRTY_BITMAP(in_bs
, bm
) {
1785 if (bdrv_dirty_bitmap_get_persistence(bm
)) {
1786 const char *name
= bdrv_dirty_bitmap_name(bm
);
1787 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bm
);
1789 get_bitmap_bytes_needed(bdrv_dirty_bitmap_size(bm
),
1791 uint64_t bmclusters
= DIV_ROUND_UP(bmbytes
, cluster_size
);
1793 /* Assume the entire bitmap is allocated */
1794 bitmaps_size
+= bmclusters
* cluster_size
;
1795 /* Also reserve space for the bitmap table entries */
1796 bitmaps_size
+= ROUND_UP(bmclusters
* BME_TABLE_ENTRY_SIZE
,
1798 /* And space for contribution to bitmap directory size */
1799 bitmap_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1802 bitmaps_size
+= ROUND_UP(bitmap_dir_size
, cluster_size
);
1804 return bitmaps_size
;