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 /* This function returns the number of disk sectors covered by a single qcow2
273 * cluster of bitmap data. */
274 static uint64_t sectors_covered_by_bitmap_cluster(const BDRVQcow2State
*s
,
275 const BdrvDirtyBitmap
*bitmap
)
277 uint32_t sector_granularity
=
278 bdrv_dirty_bitmap_granularity(bitmap
) >> BDRV_SECTOR_BITS
;
280 return (uint64_t)sector_granularity
* (s
->cluster_size
<< 3);
284 * @bitmap_table entries must satisfy specification constraints.
285 * @bitmap must be cleared */
286 static int load_bitmap_data(BlockDriverState
*bs
,
287 const uint64_t *bitmap_table
,
288 uint32_t bitmap_table_size
,
289 BdrvDirtyBitmap
*bitmap
)
292 BDRVQcow2State
*s
= bs
->opaque
;
293 uint64_t sector
, sbc
;
294 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
296 uint64_t i
, tab_size
=
298 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
300 if (tab_size
!= bitmap_table_size
|| tab_size
> BME_MAX_TABLE_SIZE
) {
304 buf
= g_malloc(s
->cluster_size
);
305 sbc
= sectors_covered_by_bitmap_cluster(s
, bitmap
);
306 for (i
= 0, sector
= 0; i
< tab_size
; ++i
, sector
+= sbc
) {
307 uint64_t count
= MIN(bm_size
- sector
, sbc
);
308 uint64_t entry
= bitmap_table
[i
];
309 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
311 assert(check_table_entry(entry
, s
->cluster_size
) == 0);
314 if (entry
& BME_TABLE_ENTRY_FLAG_ALL_ONES
) {
315 bdrv_dirty_bitmap_deserialize_ones(bitmap
, sector
, count
,
318 /* No need to deserialize zeros because the dirty bitmap is
322 ret
= bdrv_pread(bs
->file
, offset
, buf
, s
->cluster_size
);
326 bdrv_dirty_bitmap_deserialize_part(bitmap
, buf
, sector
, count
,
332 bdrv_dirty_bitmap_deserialize_finish(bitmap
);
340 static 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 if (bm
->flags
& BME_FLAG_IN_USE
) {
349 error_setg(errp
, "Bitmap '%s' is in use", bm
->name
);
353 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
355 error_setg_errno(errp
, -ret
,
356 "Could not read bitmap_table table from image for "
357 "bitmap '%s'", bm
->name
);
361 granularity
= 1U << bm
->granularity_bits
;
362 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, bm
->name
, errp
);
363 if (bitmap
== NULL
) {
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(bs
, 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 be64_to_cpus(&entry
->bitmap_table_offset
);
398 be32_to_cpus(&entry
->bitmap_table_size
);
399 be32_to_cpus(&entry
->flags
);
400 be16_to_cpus(&entry
->name_size
);
401 be32_to_cpus(&entry
->extra_data_size
);
404 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry
*entry
)
406 cpu_to_be64s(&entry
->bitmap_table_offset
);
407 cpu_to_be32s(&entry
->bitmap_table_size
);
408 cpu_to_be32s(&entry
->flags
);
409 cpu_to_be16s(&entry
->name_size
);
410 cpu_to_be32s(&entry
->extra_data_size
);
413 static inline int calc_dir_entry_size(size_t name_size
, size_t extra_data_size
)
415 return align_offset(sizeof(Qcow2BitmapDirEntry
) +
416 name_size
+ extra_data_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 fail
= (phys_bitmap_bytes
> BME_MAX_PHYS_SIZE
) ||
468 (len
> ((phys_bitmap_bytes
* 8) << entry
->granularity_bits
));
470 return fail
? -EINVAL
: 0;
473 static inline void bitmap_directory_to_be(uint8_t *dir
, size_t size
)
475 uint8_t *end
= dir
+ size
;
477 Qcow2BitmapDirEntry
*e
= (Qcow2BitmapDirEntry
*)dir
;
478 dir
+= dir_entry_size(e
);
480 bitmap_dir_entry_to_be(e
);
485 * Bitmap List public functions
488 static void bitmap_free(Qcow2Bitmap
*bm
)
498 static void bitmap_list_free(Qcow2BitmapList
*bm_list
)
502 if (bm_list
== NULL
) {
506 while ((bm
= QSIMPLEQ_FIRST(bm_list
)) != NULL
) {
507 QSIMPLEQ_REMOVE_HEAD(bm_list
, entry
);
514 static Qcow2BitmapList
*bitmap_list_new(void)
516 Qcow2BitmapList
*bm_list
= g_new(Qcow2BitmapList
, 1);
517 QSIMPLEQ_INIT(bm_list
);
522 static uint32_t bitmap_list_count(Qcow2BitmapList
*bm_list
)
525 uint32_t nb_bitmaps
= 0;
527 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
535 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
536 * checks it and convert to bitmap list.
538 static Qcow2BitmapList
*bitmap_list_load(BlockDriverState
*bs
, uint64_t offset
,
539 uint64_t size
, Error
**errp
)
542 BDRVQcow2State
*s
= bs
->opaque
;
543 uint8_t *dir
, *dir_end
;
544 Qcow2BitmapDirEntry
*e
;
545 uint32_t nb_dir_entries
= 0;
546 Qcow2BitmapList
*bm_list
= NULL
;
549 error_setg(errp
, "Requested bitmap directory size is zero");
553 if (size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
554 error_setg(errp
, "Requested bitmap directory size is too big");
558 dir
= g_try_malloc(size
);
560 error_setg(errp
, "Failed to allocate space for bitmap directory");
563 dir_end
= dir
+ size
;
565 ret
= bdrv_pread(bs
->file
, offset
, dir
, size
);
567 error_setg_errno(errp
, -ret
, "Failed to read bitmap directory");
571 bm_list
= bitmap_list_new();
572 for (e
= (Qcow2BitmapDirEntry
*)dir
;
573 e
< (Qcow2BitmapDirEntry
*)dir_end
;
574 e
= next_dir_entry(e
))
578 if ((uint8_t *)(e
+ 1) > dir_end
) {
582 if (++nb_dir_entries
> s
->nb_bitmaps
) {
583 error_setg(errp
, "More bitmaps found than specified in header"
587 bitmap_dir_entry_to_cpu(e
);
589 if ((uint8_t *)next_dir_entry(e
) > dir_end
) {
593 if (e
->extra_data_size
!= 0) {
594 error_setg(errp
, "Bitmap extra data is not supported");
598 ret
= check_dir_entry(bs
, e
);
600 error_setg(errp
, "Bitmap '%.*s' doesn't satisfy the constraints",
601 e
->name_size
, dir_entry_name_field(e
));
605 bm
= g_new(Qcow2Bitmap
, 1);
606 bm
->table
.offset
= e
->bitmap_table_offset
;
607 bm
->table
.size
= e
->bitmap_table_size
;
608 bm
->flags
= e
->flags
;
609 bm
->granularity_bits
= e
->granularity_bits
;
610 bm
->name
= dir_entry_copy_name(e
);
611 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
614 if (nb_dir_entries
!= s
->nb_bitmaps
) {
615 error_setg(errp
, "Less bitmaps found than specified in header"
620 if ((uint8_t *)e
!= dir_end
) {
629 error_setg(errp
, "Broken bitmap directory");
633 bitmap_list_free(bm_list
);
638 int qcow2_check_bitmaps_refcounts(BlockDriverState
*bs
, BdrvCheckResult
*res
,
639 void **refcount_table
,
640 int64_t *refcount_table_size
)
643 BDRVQcow2State
*s
= bs
->opaque
;
644 Qcow2BitmapList
*bm_list
;
647 if (s
->nb_bitmaps
== 0) {
651 ret
= qcow2_inc_refcounts_imrt(bs
, res
, refcount_table
, refcount_table_size
,
652 s
->bitmap_directory_offset
,
653 s
->bitmap_directory_size
);
658 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
659 s
->bitmap_directory_size
, NULL
);
660 if (bm_list
== NULL
) {
665 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
666 uint64_t *bitmap_table
= NULL
;
669 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
670 refcount_table
, refcount_table_size
,
672 bm
->table
.size
* sizeof(uint64_t));
677 ret
= bitmap_table_load(bs
, &bm
->table
, &bitmap_table
);
683 for (i
= 0; i
< bm
->table
.size
; ++i
) {
684 uint64_t entry
= bitmap_table
[i
];
685 uint64_t offset
= entry
& BME_TABLE_ENTRY_OFFSET_MASK
;
687 if (check_table_entry(entry
, s
->cluster_size
) < 0) {
696 ret
= qcow2_inc_refcounts_imrt(bs
, res
,
697 refcount_table
, refcount_table_size
,
698 offset
, s
->cluster_size
);
700 g_free(bitmap_table
);
705 g_free(bitmap_table
);
709 bitmap_list_free(bm_list
);
715 * Store bitmap list to qcow2 image as a bitmap directory.
716 * Everything is checked.
718 static int bitmap_list_store(BlockDriverState
*bs
, Qcow2BitmapList
*bm_list
,
719 uint64_t *offset
, uint64_t *size
, bool in_place
)
723 int64_t dir_offset
= 0;
724 uint64_t dir_size
= 0;
726 Qcow2BitmapDirEntry
*e
;
728 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
729 dir_size
+= calc_dir_entry_size(strlen(bm
->name
), 0);
732 if (dir_size
== 0 || dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
737 if (*size
!= dir_size
|| *offset
== 0) {
741 dir_offset
= *offset
;
744 dir
= g_try_malloc(dir_size
);
749 e
= (Qcow2BitmapDirEntry
*)dir
;
750 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
751 e
->bitmap_table_offset
= bm
->table
.offset
;
752 e
->bitmap_table_size
= bm
->table
.size
;
753 e
->flags
= bm
->flags
;
754 e
->type
= BT_DIRTY_TRACKING_BITMAP
;
755 e
->granularity_bits
= bm
->granularity_bits
;
756 e
->name_size
= strlen(bm
->name
);
757 e
->extra_data_size
= 0;
758 memcpy(e
+ 1, bm
->name
, e
->name_size
);
760 if (check_dir_entry(bs
, e
) < 0) {
765 e
= next_dir_entry(e
);
768 bitmap_directory_to_be(dir
, dir_size
);
771 dir_offset
= qcow2_alloc_clusters(bs
, dir_size
);
772 if (dir_offset
< 0) {
778 ret
= qcow2_pre_write_overlap_check(bs
, 0, dir_offset
, dir_size
);
783 ret
= bdrv_pwrite(bs
->file
, dir_offset
, dir
, dir_size
);
792 *offset
= dir_offset
;
800 if (!in_place
&& dir_offset
> 0) {
801 qcow2_free_clusters(bs
, dir_offset
, dir_size
, QCOW2_DISCARD_OTHER
);
811 static int update_ext_header_and_dir_in_place(BlockDriverState
*bs
,
812 Qcow2BitmapList
*bm_list
)
814 BDRVQcow2State
*s
= bs
->opaque
;
817 if (!(s
->autoclear_features
& QCOW2_AUTOCLEAR_BITMAPS
) ||
818 bm_list
== NULL
|| QSIMPLEQ_EMPTY(bm_list
) ||
819 bitmap_list_count(bm_list
) != s
->nb_bitmaps
)
824 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
825 ret
= update_header_sync(bs
);
827 /* Two variants are possible here:
828 * 1. Autoclear flag is dropped, all bitmaps will be lost.
829 * 2. Autoclear flag is not dropped, old state is left.
834 /* autoclear bit is not set, so we can safely update bitmap directory */
836 ret
= bitmap_list_store(bs
, bm_list
, &s
->bitmap_directory_offset
,
837 &s
->bitmap_directory_size
, true);
839 /* autoclear bit is cleared, so all leaked clusters would be removed on
844 ret
= update_header_sync(bs
);
846 /* autoclear bit is cleared, so all leaked clusters would be removed on
851 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
852 return update_header_sync(bs
);
853 /* If final update_header_sync() fails, two variants are possible:
854 * 1. Autoclear flag is not set, all bitmaps will be lost.
855 * 2. Autoclear flag is set, header and directory are successfully updated.
859 static int update_ext_header_and_dir(BlockDriverState
*bs
,
860 Qcow2BitmapList
*bm_list
)
862 BDRVQcow2State
*s
= bs
->opaque
;
864 uint64_t new_offset
= 0;
865 uint64_t new_size
= 0;
866 uint32_t new_nb_bitmaps
= 0;
867 uint64_t old_offset
= s
->bitmap_directory_offset
;
868 uint64_t old_size
= s
->bitmap_directory_size
;
869 uint32_t old_nb_bitmaps
= s
->nb_bitmaps
;
870 uint64_t old_autocl
= s
->autoclear_features
;
872 if (bm_list
!= NULL
&& !QSIMPLEQ_EMPTY(bm_list
)) {
873 new_nb_bitmaps
= bitmap_list_count(bm_list
);
875 if (new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
879 ret
= bitmap_list_store(bs
, bm_list
, &new_offset
, &new_size
, false);
884 ret
= bdrv_flush(bs
->file
->bs
);
889 s
->autoclear_features
|= QCOW2_AUTOCLEAR_BITMAPS
;
891 s
->autoclear_features
&= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS
;
894 s
->bitmap_directory_offset
= new_offset
;
895 s
->bitmap_directory_size
= new_size
;
896 s
->nb_bitmaps
= new_nb_bitmaps
;
898 ret
= update_header_sync(bs
);
904 qcow2_free_clusters(bs
, old_offset
, old_size
, QCOW2_DISCARD_OTHER
);
910 if (new_offset
> 0) {
911 qcow2_free_clusters(bs
, new_offset
, new_size
, QCOW2_DISCARD_OTHER
);
914 s
->bitmap_directory_offset
= old_offset
;
915 s
->bitmap_directory_size
= old_size
;
916 s
->nb_bitmaps
= old_nb_bitmaps
;
917 s
->autoclear_features
= old_autocl
;
922 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
923 static void release_dirty_bitmap_helper(gpointer bitmap
,
926 bdrv_release_dirty_bitmap(bs
, bitmap
);
929 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
930 static void set_readonly_helper(gpointer bitmap
, gpointer value
)
932 bdrv_dirty_bitmap_set_readonly(bitmap
, (bool)value
);
935 /* qcow2_load_autoloading_dirty_bitmaps()
936 * Return value is a hint for caller: true means that the Qcow2 header was
937 * updated. (false doesn't mean that the header should be updated by the
938 * caller, it just means that updating was not needed or the image cannot be
940 * On failure the function returns false.
942 bool qcow2_load_autoloading_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
944 BDRVQcow2State
*s
= bs
->opaque
;
945 Qcow2BitmapList
*bm_list
;
947 GSList
*created_dirty_bitmaps
= NULL
;
948 bool header_updated
= false;
950 if (s
->nb_bitmaps
== 0) {
951 /* No bitmaps - nothing to do */
955 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
956 s
->bitmap_directory_size
, errp
);
957 if (bm_list
== NULL
) {
961 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
962 if ((bm
->flags
& BME_FLAG_AUTO
) && !(bm
->flags
& BME_FLAG_IN_USE
)) {
963 BdrvDirtyBitmap
*bitmap
= load_bitmap(bs
, bm
, errp
);
964 if (bitmap
== NULL
) {
968 bdrv_dirty_bitmap_set_persistance(bitmap
, true);
969 bdrv_dirty_bitmap_set_autoload(bitmap
, true);
970 bm
->flags
|= BME_FLAG_IN_USE
;
971 created_dirty_bitmaps
=
972 g_slist_append(created_dirty_bitmaps
, bitmap
);
976 if (created_dirty_bitmaps
!= NULL
) {
978 /* in_use flags must be updated */
979 int ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
981 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
984 header_updated
= true;
986 g_slist_foreach(created_dirty_bitmaps
, set_readonly_helper
,
991 g_slist_free(created_dirty_bitmaps
);
992 bitmap_list_free(bm_list
);
994 return header_updated
;
997 g_slist_foreach(created_dirty_bitmaps
, release_dirty_bitmap_helper
, bs
);
998 g_slist_free(created_dirty_bitmaps
);
999 bitmap_list_free(bm_list
);
1004 int qcow2_reopen_bitmaps_rw(BlockDriverState
*bs
, Error
**errp
)
1006 BDRVQcow2State
*s
= bs
->opaque
;
1007 Qcow2BitmapList
*bm_list
;
1009 GSList
*ro_dirty_bitmaps
= NULL
;
1012 if (s
->nb_bitmaps
== 0) {
1013 /* No bitmaps - nothing to do */
1017 if (!can_write(bs
)) {
1018 error_setg(errp
, "Can't write to the image on reopening bitmaps rw");
1022 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1023 s
->bitmap_directory_size
, errp
);
1024 if (bm_list
== NULL
) {
1028 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1029 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1030 BdrvDirtyBitmap
*bitmap
= bdrv_find_dirty_bitmap(bs
, bm
->name
);
1031 if (bitmap
== NULL
) {
1035 if (!bdrv_dirty_bitmap_readonly(bitmap
)) {
1036 error_setg(errp
, "Bitmap %s is not readonly but not marked"
1037 "'IN_USE' in the image. Something went wrong,"
1038 "all the bitmaps may be corrupted", bm
->name
);
1043 bm
->flags
|= BME_FLAG_IN_USE
;
1044 ro_dirty_bitmaps
= g_slist_append(ro_dirty_bitmaps
, bitmap
);
1048 if (ro_dirty_bitmaps
!= NULL
) {
1049 /* in_use flags must be updated */
1050 ret
= update_ext_header_and_dir_in_place(bs
, bm_list
);
1052 error_setg_errno(errp
, -ret
, "Can't update bitmap directory");
1055 g_slist_foreach(ro_dirty_bitmaps
, set_readonly_helper
, false);
1059 g_slist_free(ro_dirty_bitmaps
);
1060 bitmap_list_free(bm_list
);
1065 /* store_bitmap_data()
1066 * Store bitmap to image, filling bitmap table accordingly.
1068 static uint64_t *store_bitmap_data(BlockDriverState
*bs
,
1069 BdrvDirtyBitmap
*bitmap
,
1070 uint32_t *bitmap_table_size
, Error
**errp
)
1073 BDRVQcow2State
*s
= bs
->opaque
;
1076 uint64_t bm_size
= bdrv_dirty_bitmap_size(bitmap
);
1077 const char *bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1078 uint8_t *buf
= NULL
;
1079 BdrvDirtyBitmapIter
*dbi
;
1083 bdrv_dirty_bitmap_serialization_size(bitmap
, 0, bm_size
));
1085 if (tb_size
> BME_MAX_TABLE_SIZE
||
1086 tb_size
* s
->cluster_size
> BME_MAX_PHYS_SIZE
)
1088 error_setg(errp
, "Bitmap '%s' is too big", bm_name
);
1092 tb
= g_try_new0(uint64_t, tb_size
);
1094 error_setg(errp
, "No memory");
1098 dbi
= bdrv_dirty_iter_new(bitmap
, 0);
1099 buf
= g_malloc(s
->cluster_size
);
1100 sbc
= sectors_covered_by_bitmap_cluster(s
, bitmap
);
1101 assert(DIV_ROUND_UP(bm_size
, sbc
) == tb_size
);
1103 while ((sector
= bdrv_dirty_iter_next(dbi
)) != -1) {
1104 uint64_t cluster
= sector
/ sbc
;
1105 uint64_t end
, write_size
;
1108 sector
= cluster
* sbc
;
1109 end
= MIN(bm_size
, sector
+ sbc
);
1111 bdrv_dirty_bitmap_serialization_size(bitmap
, sector
, end
- sector
);
1112 assert(write_size
<= s
->cluster_size
);
1114 off
= qcow2_alloc_clusters(bs
, s
->cluster_size
);
1116 error_setg_errno(errp
, -off
,
1117 "Failed to allocate clusters for bitmap '%s'",
1123 bdrv_dirty_bitmap_serialize_part(bitmap
, buf
, sector
, end
- sector
);
1124 if (write_size
< s
->cluster_size
) {
1125 memset(buf
+ write_size
, 0, s
->cluster_size
- write_size
);
1128 ret
= qcow2_pre_write_overlap_check(bs
, 0, off
, s
->cluster_size
);
1130 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1134 ret
= bdrv_pwrite(bs
->file
, off
, buf
, s
->cluster_size
);
1136 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1141 if (end
>= bm_size
) {
1145 bdrv_set_dirty_iter(dbi
, end
);
1148 *bitmap_table_size
= tb_size
;
1150 bdrv_dirty_iter_free(dbi
);
1155 clear_bitmap_table(bs
, tb
, tb_size
);
1157 bdrv_dirty_iter_free(dbi
);
1164 * Store bm->dirty_bitmap to qcow2.
1165 * Set bm->table_offset and bm->table_size accordingly.
1167 static int store_bitmap(BlockDriverState
*bs
, Qcow2Bitmap
*bm
, Error
**errp
)
1173 BdrvDirtyBitmap
*bitmap
= bm
->dirty_bitmap
;
1174 const char *bm_name
;
1176 assert(bitmap
!= NULL
);
1178 bm_name
= bdrv_dirty_bitmap_name(bitmap
);
1180 tb
= store_bitmap_data(bs
, bitmap
, &tb_size
, errp
);
1185 assert(tb_size
<= BME_MAX_TABLE_SIZE
);
1186 tb_offset
= qcow2_alloc_clusters(bs
, tb_size
* sizeof(tb
[0]));
1187 if (tb_offset
< 0) {
1188 error_setg_errno(errp
, -tb_offset
,
1189 "Failed to allocate clusters for bitmap '%s'",
1195 ret
= qcow2_pre_write_overlap_check(bs
, 0, tb_offset
,
1196 tb_size
* sizeof(tb
[0]));
1198 error_setg_errno(errp
, -ret
, "Qcow2 overlap check failed");
1202 bitmap_table_to_be(tb
, tb_size
);
1203 ret
= bdrv_pwrite(bs
->file
, tb_offset
, tb
, tb_size
* sizeof(tb
[0]));
1205 error_setg_errno(errp
, -ret
, "Failed to write bitmap '%s' to file",
1212 bm
->table
.offset
= tb_offset
;
1213 bm
->table
.size
= tb_size
;
1218 clear_bitmap_table(bs
, tb
, tb_size
);
1220 if (tb_offset
> 0) {
1221 qcow2_free_clusters(bs
, tb_offset
, tb_size
* sizeof(tb
[0]),
1222 QCOW2_DISCARD_OTHER
);
1230 static Qcow2Bitmap
*find_bitmap_by_name(Qcow2BitmapList
*bm_list
,
1235 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1236 if (strcmp(name
, bm
->name
) == 0) {
1244 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
1249 BDRVQcow2State
*s
= bs
->opaque
;
1251 Qcow2BitmapList
*bm_list
;
1253 if (s
->nb_bitmaps
== 0) {
1254 /* Absence of the bitmap is not an error: see explanation above
1255 * bdrv_remove_persistent_dirty_bitmap() definition. */
1259 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1260 s
->bitmap_directory_size
, errp
);
1261 if (bm_list
== NULL
) {
1265 bm
= find_bitmap_by_name(bm_list
, name
);
1270 QSIMPLEQ_REMOVE(bm_list
, bm
, Qcow2Bitmap
, entry
);
1272 ret
= update_ext_header_and_dir(bs
, bm_list
);
1274 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1278 free_bitmap_clusters(bs
, &bm
->table
);
1282 bitmap_list_free(bm_list
);
1285 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState
*bs
, Error
**errp
)
1287 BdrvDirtyBitmap
*bitmap
;
1288 BDRVQcow2State
*s
= bs
->opaque
;
1289 uint32_t new_nb_bitmaps
= s
->nb_bitmaps
;
1290 uint64_t new_dir_size
= s
->bitmap_directory_size
;
1292 Qcow2BitmapList
*bm_list
;
1294 Qcow2BitmapTableList drop_tables
;
1295 Qcow2BitmapTable
*tb
, *tb_next
;
1297 if (!bdrv_has_changed_persistent_bitmaps(bs
)) {
1302 if (!can_write(bs
)) {
1303 error_setg(errp
, "No write access");
1307 QSIMPLEQ_INIT(&drop_tables
);
1309 if (s
->nb_bitmaps
== 0) {
1310 bm_list
= bitmap_list_new();
1312 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1313 s
->bitmap_directory_size
, errp
);
1314 if (bm_list
== NULL
) {
1319 /* check constraints and names */
1320 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1321 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1323 const char *name
= bdrv_dirty_bitmap_name(bitmap
);
1324 uint32_t granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
1327 if (!bdrv_dirty_bitmap_get_persistance(bitmap
) ||
1328 bdrv_dirty_bitmap_readonly(bitmap
))
1333 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) < 0) {
1334 error_prepend(errp
, "Bitmap '%s' doesn't satisfy the constraints: ",
1339 bm
= find_bitmap_by_name(bm_list
, name
);
1341 if (++new_nb_bitmaps
> QCOW2_MAX_BITMAPS
) {
1342 error_setg(errp
, "Too many persistent bitmaps");
1346 new_dir_size
+= calc_dir_entry_size(strlen(name
), 0);
1347 if (new_dir_size
> QCOW2_MAX_BITMAP_DIRECTORY_SIZE
) {
1348 error_setg(errp
, "Bitmap directory is too large");
1352 bm
= g_new0(Qcow2Bitmap
, 1);
1353 bm
->name
= g_strdup(name
);
1354 QSIMPLEQ_INSERT_TAIL(bm_list
, bm
, entry
);
1356 if (!(bm
->flags
& BME_FLAG_IN_USE
)) {
1357 error_setg(errp
, "Bitmap '%s' already exists in the image",
1361 tb
= g_memdup(&bm
->table
, sizeof(bm
->table
));
1362 bm
->table
.offset
= 0;
1364 QSIMPLEQ_INSERT_TAIL(&drop_tables
, tb
, entry
);
1366 bm
->flags
= bdrv_dirty_bitmap_get_autoload(bitmap
) ? BME_FLAG_AUTO
: 0;
1367 bm
->granularity_bits
= ctz32(bdrv_dirty_bitmap_granularity(bitmap
));
1368 bm
->dirty_bitmap
= bitmap
;
1371 /* allocate clusters and store bitmaps */
1372 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1373 if (bm
->dirty_bitmap
== NULL
) {
1377 ret
= store_bitmap(bs
, bm
, errp
);
1383 ret
= update_ext_header_and_dir(bs
, bm_list
);
1385 error_setg_errno(errp
, -ret
, "Failed to update bitmap extension");
1389 /* Bitmap directory was successfully updated, so, old data can be dropped.
1390 * TODO it is better to reuse these clusters */
1391 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1392 free_bitmap_clusters(bs
, tb
);
1396 bitmap_list_free(bm_list
);
1400 QSIMPLEQ_FOREACH(bm
, bm_list
, entry
) {
1401 if (bm
->dirty_bitmap
== NULL
|| bm
->table
.offset
== 0) {
1405 free_bitmap_clusters(bs
, &bm
->table
);
1408 QSIMPLEQ_FOREACH_SAFE(tb
, &drop_tables
, entry
, tb_next
) {
1412 bitmap_list_free(bm_list
);
1415 int qcow2_reopen_bitmaps_ro(BlockDriverState
*bs
, Error
**errp
)
1417 BdrvDirtyBitmap
*bitmap
;
1418 Error
*local_err
= NULL
;
1420 qcow2_store_persistent_dirty_bitmaps(bs
, &local_err
);
1421 if (local_err
!= NULL
) {
1422 error_propagate(errp
, local_err
);
1426 for (bitmap
= bdrv_dirty_bitmap_next(bs
, NULL
); bitmap
!= NULL
;
1427 bitmap
= bdrv_dirty_bitmap_next(bs
, bitmap
))
1429 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
1430 bdrv_dirty_bitmap_set_readonly(bitmap
, true);
1437 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState
*bs
,
1439 uint32_t granularity
,
1442 BDRVQcow2State
*s
= bs
->opaque
;
1444 Qcow2BitmapList
*bm_list
;
1446 if (check_constraints_on_bitmap(bs
, name
, granularity
, errp
) != 0) {
1450 if (s
->nb_bitmaps
== 0) {
1454 if (s
->nb_bitmaps
>= QCOW2_MAX_BITMAPS
) {
1456 "Maximum number of persistent bitmaps is already reached");
1460 if (s
->bitmap_directory_size
+ calc_dir_entry_size(strlen(name
), 0) >
1461 QCOW2_MAX_BITMAP_DIRECTORY_SIZE
)
1463 error_setg(errp
, "Not enough space in the bitmap directory");
1467 bm_list
= bitmap_list_load(bs
, s
->bitmap_directory_offset
,
1468 s
->bitmap_directory_size
, errp
);
1469 if (bm_list
== NULL
) {
1473 found
= find_bitmap_by_name(bm_list
, name
);
1474 bitmap_list_free(bm_list
);
1476 error_setg(errp
, "Bitmap with the same name is already stored");
1483 error_prepend(errp
, "Can't make bitmap '%s' persistent in '%s': ",
1484 name
, bdrv_get_device_or_node_name(bs
));