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"
33 #include "block/qcow2.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
;
80 typedef QSIMPLEQ_HEAD(Qcow2BitmapTableList
, Qcow2BitmapTable
)
83 typedef struct Qcow2Bitmap
{
84 Qcow2BitmapTable table
;
86 uint8_t granularity_bits
;
89 BdrvDirtyBitmap
*dirty_bitmap
;
91 QSIMPLEQ_ENTRY(Qcow2Bitmap
) entry
;
93 typedef QSIMPLEQ_HEAD(Qcow2BitmapList
, Qcow2Bitmap
) Qcow2BitmapList
;
95 typedef enum BitmapType
{
96 BT_DIRTY_TRACKING_BITMAP
= 1
99 static inline bool can_write(BlockDriverState
*bs
)
101 return !bdrv_is_read_only(bs
) && !(bdrv_get_flags(bs
) & BDRV_O_INACTIVE
);
104 static int update_header_sync(BlockDriverState
*bs
)
108 ret
= qcow2_update_header(bs
);
113 return bdrv_flush(bs
);
116 static inline void bitmap_table_to_be(uint64_t *bitmap_table
, size_t size
)
120 for (i
= 0; i
< size
; ++i
) {
121 cpu_to_be64s(&bitmap_table
[i
]);
125 static int check_table_entry(uint64_t entry
, int cluster_size
)
129 if (entry
& BME_TABLE_ENTRY_RESERVED_MASK
) {
133 offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
135 /* if offset specified, bit 0 is reserved */
136 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
140 if (offset
% cluster_size
!= 0) {
148 static int check_constraints_on_bitmap(BlockDriverState
*bs
,
150 uint32_t granularity
,
153 BDRVQcow2State
*s
= bs
->opaque
;
154 int granularity_bits
= ctz32(granularity
);
155 int64_t len
= bdrv_getlength(bs
);
157 assert(granularity
> 0);
158 assert((granularity
& (granularity
- 1)) == 0);
161 error_setg_errno(errp
, -len
, "Failed to get size of '%s'",
162 bdrv_get_device_or_node_name(bs
));
166 if (granularity_bits
> BME_MAX_GRANULARITY_BITS
) {
167 error_setg(errp
, "Granularity exceeds maximum (%llu bytes)",
168 1ULL << BME_MAX_GRANULARITY_BITS
);
171 if (granularity_bits
< BME_MIN_GRANULARITY_BITS
) {
172 error_setg(errp
, "Granularity is under minimum (%llu bytes)",
173 1ULL << BME_MIN_GRANULARITY_BITS
);
177 if ((len
> (uint64_t)BME_MAX_PHYS_SIZE
<< granularity_bits
) ||
178 (len
> (uint64_t)BME_MAX_TABLE_SIZE
* s
->cluster_size
<<
181 error_setg(errp
, "Too much space will be occupied by the bitmap. "
182 "Use larger granularity");
186 if (strlen(name
) > BME_MAX_NAME_SIZE
) {
187 error_setg(errp
, "Name length exceeds maximum (%u characters)",
195 static void clear_bitmap_table(BlockDriverState
*bs
, uint64_t *bitmap_table
,
196 uint32_t bitmap_table_size
)
198 BDRVQcow2State
*s
= bs
->opaque
;
201 for (i
= 0; i
< bitmap_table_size
; ++i
) {
202 uint64_t addr
= bitmap_table
[i
] & BME_TABLE_ENTRY_OFFSET_MASK
;
207 qcow2_free_clusters(bs
, addr
, s
->cluster_size
, QCOW2_DISCARD_OTHER
);
212 static int bitmap_table_load(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
,
213 uint64_t **bitmap_table
)
216 BDRVQcow2State
*s
= bs
->opaque
;
220 assert(tb
->size
!= 0);
221 table
= g_try_new(uint64_t, tb
->size
);
226 assert(tb
->size
<= BME_MAX_TABLE_SIZE
);
227 ret
= bdrv_pread(bs
->file
, tb
->offset
,
228 table
, tb
->size
* sizeof(uint64_t));
233 for (i
= 0; i
< tb
->size
; ++i
) {
234 be64_to_cpus(&table
[i
]);
235 ret
= check_table_entry(table
[i
], s
->cluster_size
);
241 *bitmap_table
= table
;
250 static int free_bitmap_clusters(BlockDriverState
*bs
, Qcow2BitmapTable
*tb
)
253 uint64_t *bitmap_table
;
255 ret
= bitmap_table_load(bs
, tb
, &bitmap_table
);
257 assert(bitmap_table
== NULL
);
261 clear_bitmap_table(bs
, bitmap_table
, tb
->size
);
262 qcow2_free_clusters(bs
, tb
->offset
, tb
->size
* sizeof(uint64_t),
263 QCOW2_DISCARD_OTHER
);
264 g_free(bitmap_table
);
272 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
273 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State
*s
,
274 const BdrvDirtyBitmap
*bitmap
)
276 uint64_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
277 uint64_t limit
= granularity
* (s
->cluster_size
<< 3);
279 assert(QEMU_IS_ALIGNED(limit
,
280 bdrv_dirty_bitmap_serialization_align(bitmap
)));
285 * @bitmap_table entries must satisfy specification constraints.
286 * @bitmap must be cleared */
287 static int load_bitmap_data(BlockDriverState
*bs
,
288 const uint64_t *bitmap_table
,
289 uint32_t bitmap_table_size
,
290 BdrvDirtyBitmap
*bitmap
)
293 BDRVQcow2State
*s
= bs
->opaque
;
294 uint64_t offset
, limit
;
295 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
297 uint64_t i
, tab_size
=
299 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
301 if (tab_size
!= bitmap_table_size
|| tab_size
> BME_MAX_TABLE_SIZE
) {
305 buf
= g_malloc(s
->cluster_size
);
306 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
307 for (i
= 0, offset
= 0; i
< tab_size
; ++i
, offset
+= limit
) {
308 uint64_t count
= MIN(bm_size
- offset
, limit
);
309 uint64_t entry
= bitmap_table
[i
];
310 uint64_t data_offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
312 assert(check_table_entry(entry
, s
->cluster_size
) == 0);
314 if (data_offset
== 0) {
315 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
316 bdrv_dirty_bitmap_deserialize_ones(bitmap
, offset
, count
,
319 /* No need to deserialize zeros because the dirty bitmap is
323 ret
= bdrv_pread(bs
->file
, data_offset
, buf
, s
->cluster_size
);
327 bdrv_dirty_bitmap_deserialize_part(bitmap
, buf
, offset
, count
,
333 bdrv_dirty_bitmap_deserialize_finish(bitmap
);
341 static BdrvDirtyBitmap
*load_bitmap(BlockDriverState
*bs
,
342 Qcow2Bitmap
*bm
, Error
**errp
)
345 uint64_t *bitmap_table
= NULL
;
346 uint32_t granularity
;
347 BdrvDirtyBitmap
*bitmap
= NULL
;
349 if (bm
->flags
& BME_FLAG_IN_USE
) {
350 error_setg(errp
, "Bitmap '%s' is in use", bm
->name
);
354 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
356 error_setg_errno(errp
, -ret
,
357 "Could not read bitmap_table table from image for "
358 "bitmap '%s'", bm
->name
);
362 granularity
= 1U << bm
->granularity_bits
;
363 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, bm
->name
, errp
);
364 if (bitmap
== NULL
) {
368 ret
= load_bitmap_data(bs
, bitmap_table
, bm
->table
.size
, bitmap
);
370 error_setg_errno(errp
, -ret
, "Could not read bitmap '%s' from image",
375 g_free(bitmap_table
);
379 g_free(bitmap_table
);
380 if (bitmap
!= NULL
) {
381 bdrv_release_dirty_bitmap(bs
, bitmap
);
392 * Bitmap List private functions
393 * Only Bitmap List knows about bitmap directory structure in Qcow2.
396 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry
*entry
)
398 be64_to_cpus(&entry
->bitmap_table_offset
);
399 be32_to_cpus(&entry
->bitmap_table_size
);
400 be32_to_cpus(&entry
->flags
);
401 be16_to_cpus(&entry
->name_size
);
402 be32_to_cpus(&entry
->extra_data_size
);
405 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry
*entry
)
407 cpu_to_be64s(&entry
->bitmap_table_offset
);
408 cpu_to_be32s(&entry
->bitmap_table_size
);
409 cpu_to_be32s(&entry
->flags
);
410 cpu_to_be16s(&entry
->name_size
);
411 cpu_to_be32s(&entry
->extra_data_size
);
414 static inline int calc_dir_entry_size(size_t name_size
, size_t extra_data_size
)
416 return align_offset(sizeof(Qcow2BitmapDirEntry
) +
417 name_size
+ extra_data_size
, 8);
420 static inline int dir_entry_size(Qcow2BitmapDirEntry
*entry
)
422 return calc_dir_entry_size(entry
->name_size
, entry
->extra_data_size
);
425 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry
*entry
)
427 return (const char *)(entry
+ 1) + entry
->extra_data_size
;
430 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry
*entry
)
432 const char *name_field
= dir_entry_name_field(entry
);
433 return g_strndup(name_field
, entry
->name_size
);
436 static inline Qcow2BitmapDirEntry
*next_dir_entry(Qcow2BitmapDirEntry
*entry
)
438 return (Qcow2BitmapDirEntry
*)((uint8_t *)entry
+ dir_entry_size(entry
));
441 static int check_dir_entry(BlockDriverState
*bs
, Qcow2BitmapDirEntry
*entry
)
443 BDRVQcow2State
*s
= bs
->opaque
;
444 uint64_t phys_bitmap_bytes
;
447 bool fail
= (entry
->bitmap_table_size
== 0) ||
448 (entry
->bitmap_table_offset
== 0) ||
449 (entry
->bitmap_table_offset
% s
->cluster_size
) ||
450 (entry
->bitmap_table_size
> BME_MAX_TABLE_SIZE
) ||
451 (entry
->granularity_bits
> BME_MAX_GRANULARITY_BITS
) ||
452 (entry
->granularity_bits
< BME_MIN_GRANULARITY_BITS
) ||
453 (entry
->flags
& BME_RESERVED_FLAGS
) ||
454 (entry
->name_size
> BME_MAX_NAME_SIZE
) ||
455 (entry
->type
!= BT_DIRTY_TRACKING_BITMAP
);
461 phys_bitmap_bytes
= (uint64_t)entry
->bitmap_table_size
* s
->cluster_size
;
462 len
= bdrv_getlength(bs
);
468 fail
= (phys_bitmap_bytes
> BME_MAX_PHYS_SIZE
) ||
469 (len
> ((phys_bitmap_bytes
* 8) << entry
->granularity_bits
));
471 return fail
? -EINVAL
: 0;
474 static inline void bitmap_directory_to_be(uint8_t *dir
, size_t size
)
476 uint8_t *end
= dir
+ size
;
478 Qcow2BitmapDirEntry
*e
= (Qcow2BitmapDirEntry
*)dir
;
479 dir
+= dir_entry_size(e
);
481 bitmap_dir_entry_to_be(e
);
486 * Bitmap List public functions
489 static void bitmap_free(Qcow2Bitmap
*bm
)
499 static void bitmap_list_free(Qcow2BitmapList
*bm_list
)
503 if (bm_list
== NULL
) {
507 while ((bm
= QSIMPLEQ_FIRST(bm_list
)) != NULL
) {
508 QSIMPLEQ_REMOVE_HEAD(bm_list
, entry
);
515 static Qcow2BitmapList
*bitmap_list_new(void)
517 Qcow2BitmapList
*bm_list
= g_new(Qcow2BitmapList
, 1);
518 QSIMPLEQ_INIT(bm_list
);
523 static uint32_t bitmap_list_count(Qcow2BitmapList
*bm_list
)
526 uint32_t nb_bitmaps
= 0;
528 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
536 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
537 * checks it and convert to bitmap list.
539 static Qcow2BitmapList
*bitmap_list_load(BlockDriverState
*bs
, uint64_t offset
,
540 uint64_t size
, Error
**errp
)
543 BDRVQcow2State
*s
= bs
->opaque
;
544 uint8_t *dir
, *dir_end
;
545 Qcow2BitmapDirEntry
*e
;
546 uint32_t nb_dir_entries
= 0;
547 Qcow2BitmapList
*bm_list
= NULL
;
550 error_setg(errp
, "Requested bitmap directory size is zero");
554 if (size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
555 error_setg(errp
, "Requested bitmap directory size is too big");
559 dir
= g_try_malloc(size
);
561 error_setg(errp
, "Failed to allocate space for bitmap directory");
564 dir_end
= dir
+ size
;
566 ret
= bdrv_pread(bs
->file
, offset
, dir
, size
);
568 error_setg_errno(errp
, -ret
, "Failed to read bitmap directory");
572 bm_list
= bitmap_list_new();
573 for (e
= (Qcow2BitmapDirEntry
*)dir
;
574 e
< (Qcow2BitmapDirEntry
*)dir_end
;
575 e
= next_dir_entry(e
))
579 if ((uint8_t *)(e
+ 1) > dir_end
) {
583 if (++nb_dir_entries
> s
->nb_bitmaps
) {
584 error_setg(errp
, "More bitmaps found than specified in header"
588 bitmap_dir_entry_to_cpu(e
);
590 if ((uint8_t *)next_dir_entry(e
) > dir_end
) {
594 if (e
->extra_data_size
!= 0) {
595 error_setg(errp
, "Bitmap extra data is not supported");
599 ret
= check_dir_entry(bs
, e
);
601 error_setg(errp
, "Bitmap '%.*s' doesn't satisfy the constraints",
602 e
->name_size
, dir_entry_name_field(e
));
606 bm
= g_new0(Qcow2Bitmap
, 1);
607 bm
->table
.offset
= e
->bitmap_table_offset
;
608 bm
->table
.size
= e
->bitmap_table_size
;
609 bm
->flags
= e
->flags
;
610 bm
->granularity_bits
= e
->granularity_bits
;
611 bm
->name
= dir_entry_copy_name(e
);
612 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
615 if (nb_dir_entries
!= s
->nb_bitmaps
) {
616 error_setg(errp
, "Less bitmaps found than specified in header"
621 if ((uint8_t *)e
!= dir_end
) {
630 error_setg(errp
, "Broken bitmap directory");
634 bitmap_list_free(bm_list
);
639 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
640 void **refcount_table
,
641 int64_t *refcount_table_size
)
644 BDRVQcow2State
*s
= bs
->opaque
;
645 Qcow2BitmapList
*bm_list
;
648 if (s
->nb_bitmaps
== 0) {
652 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
653 s
->bitmap_directory_offset
,
654 s
->bitmap_directory_size
);
659 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
660 s
->bitmap_directory_size
, NULL
);
661 if (bm_list
== NULL
) {
666 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
667 uint64_t *bitmap_table
= NULL
;
670 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
671 refcount_table
, refcount_table_size
,
673 bm
->table
.size
* sizeof(uint64_t));
678 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
684 for (i
= 0; i
< bm
->table
.size
; ++i
) {
685 uint64_t entry
= bitmap_table
[i
];
686 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
688 if (check_table_entry(entry
, s
->cluster_size
) < 0) {
697 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
698 refcount_table
, refcount_table_size
,
699 offset
, s
->cluster_size
);
701 g_free(bitmap_table
);
706 g_free(bitmap_table
);
710 bitmap_list_free(bm_list
);
716 * Store bitmap list to qcow2 image as a bitmap directory.
717 * Everything is checked.
719 static int bitmap_list_store(BlockDriverState
*bs
, Qcow2BitmapList
*bm_list
,
720 uint64_t *offset
, uint64_t *size
, bool in_place
)
724 int64_t dir_offset
= 0;
725 uint64_t dir_size
= 0;
727 Qcow2BitmapDirEntry
*e
;
729 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
730 dir_size
+= calc_dir_entry_size(strlen(bm
->name
), 0);
733 if (dir_size
== 0 || dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
738 if (*size
!= dir_size
|| *offset
== 0) {
742 dir_offset
= *offset
;
745 dir
= g_try_malloc(dir_size
);
750 e
= (Qcow2BitmapDirEntry
*)dir
;
751 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
752 e
->bitmap_table_offset
= bm
->table
.offset
;
753 e
->bitmap_table_size
= bm
->table
.size
;
754 e
->flags
= bm
->flags
;
755 e
->type
= BT_DIRTY_TRACKING_BITMAP
;
756 e
->granularity_bits
= bm
->granularity_bits
;
757 e
->name_size
= strlen(bm
->name
);
758 e
->extra_data_size
= 0;
759 memcpy(e
+ 1, bm
->name
, e
->name_size
);
761 if (check_dir_entry(bs
, e
) < 0) {
766 e
= next_dir_entry(e
);
769 bitmap_directory_to_be(dir
, dir_size
);
772 dir_offset
= qcow2_alloc_clusters(bs
, dir_size
);
773 if (dir_offset
< 0) {
779 ret
= qcow2_pre_write_overlap_check(bs
, 0, dir_offset
, dir_size
);
784 ret
= bdrv_pwrite(bs
->file
, dir_offset
, dir
, dir_size
);
793 *offset
= dir_offset
;
801 if (!in_place
&& dir_offset
> 0) {
802 qcow2_free_clusters(bs
, dir_offset
, dir_size
, QCOW2_DISCARD_OTHER
);
812 static int update_ext_header_and_dir_in_place(BlockDriverState
*bs
,
813 Qcow2BitmapList
*bm_list
)
815 BDRVQcow2State
*s
= bs
->opaque
;
818 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
) ||
819 bm_list
== NULL
|| QSIMPLEQ_EMPTY(bm_list
) ||
820 bitmap_list_count(bm_list
) != s
->nb_bitmaps
)
825 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
826 ret
= update_header_sync(bs
);
828 /* Two variants are possible here:
829 * 1. Autoclear flag is dropped, all bitmaps will be lost.
830 * 2. Autoclear flag is not dropped, old state is left.
835 /* autoclear bit is not set, so we can safely update bitmap directory */
837 ret
= bitmap_list_store(bs
, bm_list
, &s
->bitmap_directory_offset
,
838 &s
->bitmap_directory_size
, true);
840 /* autoclear bit is cleared, so all leaked clusters would be removed on
845 ret
= update_header_sync(bs
);
847 /* autoclear bit is cleared, so all leaked clusters would be removed on
852 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
853 return update_header_sync(bs
);
854 /* If final update_header_sync() fails, two variants are possible:
855 * 1. Autoclear flag is not set, all bitmaps will be lost.
856 * 2. Autoclear flag is set, header and directory are successfully updated.
860 static int update_ext_header_and_dir(BlockDriverState
*bs
,
861 Qcow2BitmapList
*bm_list
)
863 BDRVQcow2State
*s
= bs
->opaque
;
865 uint64_t new_offset
= 0;
866 uint64_t new_size
= 0;
867 uint32_t new_nb_bitmaps
= 0;
868 uint64_t old_offset
= s
->bitmap_directory_offset
;
869 uint64_t old_size
= s
->bitmap_directory_size
;
870 uint32_t old_nb_bitmaps
= s
->nb_bitmaps
;
871 uint64_t old_autocl
= s
->autoclear_features
;
873 if (bm_list
!= NULL
&& !QSIMPLEQ_EMPTY(bm_list
)) {
874 new_nb_bitmaps
= bitmap_list_count(bm_list
);
876 if (new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
880 ret
= bitmap_list_store(bs
, bm_list
, &new_offset
, &new_size
, false);
885 ret
= bdrv_flush(bs
->file
->bs
);
890 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
892 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
895 s
->bitmap_directory_offset
= new_offset
;
896 s
->bitmap_directory_size
= new_size
;
897 s
->nb_bitmaps
= new_nb_bitmaps
;
899 ret
= update_header_sync(bs
);
905 qcow2_free_clusters(bs
, old_offset
, old_size
, QCOW2_DISCARD_OTHER
);
911 if (new_offset
> 0) {
912 qcow2_free_clusters(bs
, new_offset
, new_size
, QCOW2_DISCARD_OTHER
);
915 s
->bitmap_directory_offset
= old_offset
;
916 s
->bitmap_directory_size
= old_size
;
917 s
->nb_bitmaps
= old_nb_bitmaps
;
918 s
->autoclear_features
= old_autocl
;
923 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
924 static void release_dirty_bitmap_helper(gpointer bitmap
,
927 bdrv_release_dirty_bitmap(bs
, bitmap
);
930 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
931 static void set_readonly_helper(gpointer bitmap
, gpointer value
)
933 bdrv_dirty_bitmap_set_readonly(bitmap
, (bool)value
);
936 /* qcow2_load_autoloading_dirty_bitmaps()
937 * Return value is a hint for caller: true means that the Qcow2 header was
938 * updated. (false doesn't mean that the header should be updated by the
939 * caller, it just means that updating was not needed or the image cannot be
941 * On failure the function returns false.
943 bool qcow2_load_autoloading_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
945 BDRVQcow2State
*s
= bs
->opaque
;
946 Qcow2BitmapList
*bm_list
;
948 GSList
*created_dirty_bitmaps
= NULL
;
949 bool header_updated
= false;
951 if (s
->nb_bitmaps
== 0) {
952 /* No bitmaps - nothing to do */
956 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
957 s
->bitmap_directory_size
, errp
);
958 if (bm_list
== NULL
) {
962 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
963 if ((bm
->flags
& BME_FLAG_AUTO
) && !(bm
->flags
& BME_FLAG_IN_USE
)) {
964 BdrvDirtyBitmap
*bitmap
= load_bitmap(bs
, bm
, errp
);
965 if (bitmap
== NULL
) {
969 bdrv_dirty_bitmap_set_persistance(bitmap
, true);
970 bdrv_dirty_bitmap_set_autoload(bitmap
, true);
971 bm
->flags
|= BME_FLAG_IN_USE
;
972 created_dirty_bitmaps
=
973 g_slist_append(created_dirty_bitmaps
, bitmap
);
977 if (created_dirty_bitmaps
!= NULL
) {
979 /* in_use flags must be updated */
980 int ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
982 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
985 header_updated
= true;
987 g_slist_foreach(created_dirty_bitmaps
, set_readonly_helper
,
992 g_slist_free(created_dirty_bitmaps
);
993 bitmap_list_free(bm_list
);
995 return header_updated
;
998 g_slist_foreach(created_dirty_bitmaps
, release_dirty_bitmap_helper
, bs
);
999 g_slist_free(created_dirty_bitmaps
);
1000 bitmap_list_free(bm_list
);
1005 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
)
1007 BDRVQcow2State
*s
= bs
->opaque
;
1008 Qcow2BitmapList
*bm_list
;
1010 GSList
*ro_dirty_bitmaps
= NULL
;
1013 if (s
->nb_bitmaps
== 0) {
1014 /* No bitmaps - nothing to do */
1018 if (!can_write(bs
)) {
1019 error_setg(errp
, "Can't write to the image on reopening bitmaps rw");
1023 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1024 s
->bitmap_directory_size
, errp
);
1025 if (bm_list
== NULL
) {
1029 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1030 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1031 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1032 if (bitmap
== NULL
) {
1036 if (!bdrv_dirty_bitmap_readonly(bitmap
)) {
1037 error_setg(errp
, "Bitmap %s is not readonly but not marked"
1038 "'IN_USE' in the image. Something went wrong,"
1039 "all the bitmaps may be corrupted", bm
->name
);
1044 bm
->flags
|= BME_FLAG_IN_USE
;
1045 ro_dirty_bitmaps
= g_slist_append(ro_dirty_bitmaps
, bitmap
);
1049 if (ro_dirty_bitmaps
!= NULL
) {
1050 /* in_use flags must be updated */
1051 ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1053 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
1056 g_slist_foreach(ro_dirty_bitmaps
, set_readonly_helper
, false);
1060 g_slist_free(ro_dirty_bitmaps
);
1061 bitmap_list_free(bm_list
);
1066 /* store_bitmap_data()
1067 * Store bitmap to image, filling bitmap table accordingly.
1069 static uint64_t *store_bitmap_data(BlockDriverState
*bs
,
1070 BdrvDirtyBitmap
*bitmap
,
1071 uint32_t *bitmap_table_size
, Error
**errp
)
1074 BDRVQcow2State
*s
= bs
->opaque
;
1077 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
1078 const char *bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1079 uint8_t *buf
= NULL
;
1080 BdrvDirtyBitmapIter
*dbi
;
1084 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
1086 if (tb_size
> BME_MAX_TABLE_SIZE
||
1087 tb_size
* s
->cluster_size
> BME_MAX_PHYS_SIZE
)
1089 error_setg(errp
, "Bitmap '%s' is too big", bm_name
);
1093 tb
= g_try_new0(uint64_t, tb_size
);
1095 error_setg(errp
, "No memory");
1099 dbi
= bdrv_dirty_iter_new(bitmap
);
1100 buf
= g_malloc(s
->cluster_size
);
1101 limit
= bytes_covered_by_bitmap_cluster(s
, bitmap
);
1102 assert(DIV_ROUND_UP(bm_size
, limit
) == tb_size
);
1104 while ((offset
= bdrv_dirty_iter_next(dbi
)) >= 0) {
1105 uint64_t cluster
= offset
/ limit
;
1106 uint64_t end
, write_size
;
1110 * We found the first dirty offset, but want to write out the
1111 * entire cluster of the bitmap that includes that offset,
1112 * including any leading zero bits.
1114 offset
= QEMU_ALIGN_DOWN(offset
, limit
);
1115 end
= MIN(bm_size
, offset
+ limit
);
1116 write_size
= bdrv_dirty_bitmap_serialization_size(bitmap
, offset
,
1118 assert(write_size
<= s
->cluster_size
);
1120 off
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1122 error_setg_errno(errp
, -off
,
1123 "Failed to allocate clusters for bitmap '%s'",
1129 bdrv_dirty_bitmap_serialize_part(bitmap
, buf
, offset
, end
- offset
);
1130 if (write_size
< s
->cluster_size
) {
1131 memset(buf
+ write_size
, 0, s
->cluster_size
- write_size
);
1134 ret
= qcow2_pre_write_overlap_check(bs
, 0, off
, s
->cluster_size
);
1136 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1140 ret
= bdrv_pwrite(bs
->file
, off
, buf
, s
->cluster_size
);
1142 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1147 if (end
>= bm_size
) {
1151 bdrv_set_dirty_iter(dbi
, end
);
1154 *bitmap_table_size
= tb_size
;
1156 bdrv_dirty_iter_free(dbi
);
1161 clear_bitmap_table(bs
, tb
, tb_size
);
1163 bdrv_dirty_iter_free(dbi
);
1170 * Store bm->dirty_bitmap to qcow2.
1171 * Set bm->table_offset and bm->table_size accordingly.
1173 static int store_bitmap(BlockDriverState
*bs
, Qcow2Bitmap
*bm
, Error
**errp
)
1179 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1180 const char *bm_name
;
1182 assert(bitmap
!= NULL
);
1184 bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1186 tb
= store_bitmap_data(bs
, bitmap
, &tb_size
, errp
);
1191 assert(tb_size
<= BME_MAX_TABLE_SIZE
);
1192 tb_offset
= qcow2_alloc_clusters(bs
, tb_size
* sizeof(tb
[0]));
1193 if (tb_offset
< 0) {
1194 error_setg_errno(errp
, -tb_offset
,
1195 "Failed to allocate clusters for bitmap '%s'",
1201 ret
= qcow2_pre_write_overlap_check(bs
, 0, tb_offset
,
1202 tb_size
* sizeof(tb
[0]));
1204 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1208 bitmap_table_to_be(tb
, tb_size
);
1209 ret
= bdrv_pwrite(bs
->file
, tb_offset
, tb
, tb_size
* sizeof(tb
[0]));
1211 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1218 bm
->table
.offset
= tb_offset
;
1219 bm
->table
.size
= tb_size
;
1224 clear_bitmap_table(bs
, tb
, tb_size
);
1226 if (tb_offset
> 0) {
1227 qcow2_free_clusters(bs
, tb_offset
, tb_size
* sizeof(tb
[0]),
1228 QCOW2_DISCARD_OTHER
);
1236 static Qcow2Bitmap
*find_bitmap_by_name(Qcow2BitmapList
*bm_list
,
1241 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1242 if (strcmp(name
, bm
->name
) == 0) {
1250 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
1255 BDRVQcow2State
*s
= bs
->opaque
;
1257 Qcow2BitmapList
*bm_list
;
1259 if (s
->nb_bitmaps
== 0) {
1260 /* Absence of the bitmap is not an error: see explanation above
1261 * bdrv_remove_persistent_dirty_bitmap() definition. */
1265 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1266 s
->bitmap_directory_size
, errp
);
1267 if (bm_list
== NULL
) {
1271 bm
= find_bitmap_by_name(bm_list
, name
);
1276 QSIMPLEQ_REMOVE(bm_list
, bm
, Qcow2Bitmap
, entry
);
1278 ret
= update_ext_header_and_dir(bs
, bm_list
);
1280 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1284 free_bitmap_clusters(bs
, &bm
->table
);
1288 bitmap_list_free(bm_list
);
1291 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
1293 BdrvDirtyBitmap
*bitmap
;
1294 BDRVQcow2State
*s
= bs
->opaque
;
1295 uint32_t new_nb_bitmaps
= s
->nb_bitmaps
;
1296 uint64_t new_dir_size
= s
->bitmap_directory_size
;
1298 Qcow2BitmapList
*bm_list
;
1300 Qcow2BitmapTableList drop_tables
;
1301 Qcow2BitmapTable
*tb
, *tb_next
;
1303 if (!bdrv_has_changed_persistent_bitmaps(bs
)) {
1308 if (!can_write(bs
)) {
1309 error_setg(errp
, "No write access");
1313 QSIMPLEQ_INIT(&drop_tables
);
1315 if (s
->nb_bitmaps
== 0) {
1316 bm_list
= bitmap_list_new();
1318 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1319 s
->bitmap_directory_size
, errp
);
1320 if (bm_list
== NULL
) {
1325 /* check constraints and names */
1326 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1327 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1329 const char *name
= bdrv_dirty_bitmap_name(bitmap
);
1330 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
1333 if (!bdrv_dirty_bitmap_get_persistance(bitmap
) ||
1334 bdrv_dirty_bitmap_readonly(bitmap
))
1339 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) < 0) {
1340 error_prepend(errp
, "Bitmap '%s' doesn't satisfy the constraints: ",
1345 bm
= find_bitmap_by_name(bm_list
, name
);
1347 if (++new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1348 error_setg(errp
, "Too many persistent bitmaps");
1352 new_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1353 if (new_dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1354 error_setg(errp
, "Bitmap directory is too large");
1358 bm
= g_new0(Qcow2Bitmap
, 1);
1359 bm
->name
= g_strdup(name
);
1360 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
1362 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1363 error_setg(errp
, "Bitmap '%s' already exists in the image",
1367 tb
= g_memdup(&bm
->table
, sizeof(bm
->table
));
1368 bm
->table
.offset
= 0;
1370 QSIMPLEQ_INSERT_TAIL(&drop_tables
, tb
, entry
);
1372 bm
->flags
= bdrv_dirty_bitmap_get_autoload(bitmap
) ? BME_FLAG_AUTO
: 0;
1373 bm
->granularity_bits
= ctz32(bdrv_dirty_bitmap_granularity(bitmap
));
1374 bm
->dirty_bitmap
= bitmap
;
1377 /* allocate clusters and store bitmaps */
1378 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1379 if (bm
->dirty_bitmap
== NULL
) {
1383 ret
= store_bitmap(bs
, bm
, errp
);
1389 ret
= update_ext_header_and_dir(bs
, bm_list
);
1391 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1395 /* Bitmap directory was successfully updated, so, old data can be dropped.
1396 * TODO it is better to reuse these clusters */
1397 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1398 free_bitmap_clusters(bs
, tb
);
1402 bitmap_list_free(bm_list
);
1406 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1407 if (bm
->dirty_bitmap
== NULL
|| bm
->table
.offset
== 0) {
1411 free_bitmap_clusters(bs
, &bm
->table
);
1414 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1418 bitmap_list_free(bm_list
);
1421 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
)
1423 BdrvDirtyBitmap
*bitmap
;
1424 Error
*local_err
= NULL
;
1426 qcow2_store_persistent_dirty_bitmaps(bs
, &local_err
);
1427 if (local_err
!= NULL
) {
1428 error_propagate(errp
, local_err
);
1432 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1433 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1435 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
1436 bdrv_dirty_bitmap_set_readonly(bitmap
, true);
1443 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
1445 uint32_t granularity
,
1448 BDRVQcow2State
*s
= bs
->opaque
;
1450 Qcow2BitmapList
*bm_list
;
1452 if (s
->qcow_version
< 3) {
1453 /* Without autoclear_features, we would always have to assume
1454 * that a program without persistent dirty bitmap support has
1455 * accessed this qcow2 file when opening it, and would thus
1456 * have to drop all dirty bitmaps (defeating their purpose).
1458 error_setg(errp
, "Cannot store dirty bitmaps in qcow2 v2 files");
1462 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) != 0) {
1466 if (s
->nb_bitmaps
== 0) {
1470 if (s
->nb_bitmaps
>= QCOW2_MAX_BITMAPS
) {
1472 "Maximum number of persistent bitmaps is already reached");
1476 if (s
->bitmap_directory_size
+ calc_dir_entry_size(strlen(name
), 0) >
1477 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
)
1479 error_setg(errp
, "Not enough space in the bitmap directory");
1483 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1484 s
->bitmap_directory_size
, errp
);
1485 if (bm_list
== NULL
) {
1489 found
= find_bitmap_by_name(bm_list
, name
);
1490 bitmap_list_free(bm_list
);
1492 error_setg(errp
, "Bitmap with the same name is already stored");
1499 error_prepend(errp
, "Can't make bitmap '%s' persistent in '%s': ",
1500 name
, bdrv_get_device_or_node_name(bs
));