block/qcow2: proper locking on bitmap add/remove paths
[qemu/ar7.git] / block / qcow2-bitmap.c
blob644837eb033173dab82d6dc2a497c671e667cf18
1 /*
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
25 * THE SOFTWARE.
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu/cutils.h"
32 #include "qcow2.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 #if BME_MAX_TABLE_SIZE * 8ULL > INT_MAX
46 #error In the code bitmap table physical size assumed to fit into int
47 #endif
49 /* Bitmap directory entry flags */
50 #define BME_RESERVED_FLAGS 0xfffffffcU
51 #define BME_FLAG_IN_USE (1U << 0)
52 #define BME_FLAG_AUTO (1U << 1)
54 /* bits [1, 8] U [56, 63] are reserved */
55 #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001feULL
56 #define BME_TABLE_ENTRY_OFFSET_MASK 0x00fffffffffffe00ULL
57 #define BME_TABLE_ENTRY_FLAG_ALL_ONES (1ULL << 0)
59 typedef struct QEMU_PACKED Qcow2BitmapDirEntry {
60 /* header is 8 byte aligned */
61 uint64_t bitmap_table_offset;
63 uint32_t bitmap_table_size;
64 uint32_t flags;
66 uint8_t type;
67 uint8_t granularity_bits;
68 uint16_t name_size;
69 uint32_t extra_data_size;
70 /* extra data follows */
71 /* name follows */
72 } Qcow2BitmapDirEntry;
74 typedef struct Qcow2BitmapTable {
75 uint64_t offset;
76 uint32_t size; /* number of 64bit entries */
77 QSIMPLEQ_ENTRY(Qcow2BitmapTable) entry;
78 } Qcow2BitmapTable;
80 typedef struct Qcow2Bitmap {
81 Qcow2BitmapTable table;
82 uint32_t flags;
83 uint8_t granularity_bits;
84 char *name;
86 BdrvDirtyBitmap *dirty_bitmap;
88 QSIMPLEQ_ENTRY(Qcow2Bitmap) entry;
89 } Qcow2Bitmap;
90 typedef QSIMPLEQ_HEAD(Qcow2BitmapList, Qcow2Bitmap) Qcow2BitmapList;
92 typedef enum BitmapType {
93 BT_DIRTY_TRACKING_BITMAP = 1
94 } BitmapType;
96 static inline bool can_write(BlockDriverState *bs)
98 return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
101 static int update_header_sync(BlockDriverState *bs)
103 int ret;
105 ret = qcow2_update_header(bs);
106 if (ret < 0) {
107 return ret;
110 return bdrv_flush(bs->file->bs);
113 static inline void bitmap_table_to_be(uint64_t *bitmap_table, size_t size)
115 size_t i;
117 for (i = 0; i < size; ++i) {
118 bitmap_table[i] = cpu_to_be64(bitmap_table[i]);
122 static int check_table_entry(uint64_t entry, int cluster_size)
124 uint64_t offset;
126 if (entry & BME_TABLE_ENTRY_RESERVED_MASK) {
127 return -EINVAL;
130 offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
131 if (offset != 0) {
132 /* if offset specified, bit 0 is reserved */
133 if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
134 return -EINVAL;
137 if (offset % cluster_size != 0) {
138 return -EINVAL;
142 return 0;
145 static int check_constraints_on_bitmap(BlockDriverState *bs,
146 const char *name,
147 uint32_t granularity,
148 Error **errp)
150 BDRVQcow2State *s = bs->opaque;
151 int granularity_bits = ctz32(granularity);
152 int64_t len = bdrv_getlength(bs);
154 assert(granularity > 0);
155 assert((granularity & (granularity - 1)) == 0);
157 if (len < 0) {
158 error_setg_errno(errp, -len, "Failed to get size of '%s'",
159 bdrv_get_device_or_node_name(bs));
160 return len;
163 if (granularity_bits > BME_MAX_GRANULARITY_BITS) {
164 error_setg(errp, "Granularity exceeds maximum (%llu bytes)",
165 1ULL << BME_MAX_GRANULARITY_BITS);
166 return -EINVAL;
168 if (granularity_bits < BME_MIN_GRANULARITY_BITS) {
169 error_setg(errp, "Granularity is under minimum (%llu bytes)",
170 1ULL << BME_MIN_GRANULARITY_BITS);
171 return -EINVAL;
174 if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
175 (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
176 granularity_bits))
178 error_setg(errp, "Too much space will be occupied by the bitmap. "
179 "Use larger granularity");
180 return -EINVAL;
183 if (strlen(name) > BME_MAX_NAME_SIZE) {
184 error_setg(errp, "Name length exceeds maximum (%u characters)",
185 BME_MAX_NAME_SIZE);
186 return -EINVAL;
189 return 0;
192 static void clear_bitmap_table(BlockDriverState *bs, uint64_t *bitmap_table,
193 uint32_t bitmap_table_size)
195 BDRVQcow2State *s = bs->opaque;
196 int i;
198 for (i = 0; i < bitmap_table_size; ++i) {
199 uint64_t addr = bitmap_table[i] & BME_TABLE_ENTRY_OFFSET_MASK;
200 if (!addr) {
201 continue;
204 qcow2_free_clusters(bs, addr, s->cluster_size, QCOW2_DISCARD_ALWAYS);
205 bitmap_table[i] = 0;
209 static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb,
210 uint64_t **bitmap_table)
212 int ret;
213 BDRVQcow2State *s = bs->opaque;
214 uint32_t i;
215 uint64_t *table;
217 assert(tb->size != 0);
218 table = g_try_new(uint64_t, tb->size);
219 if (table == NULL) {
220 return -ENOMEM;
223 assert(tb->size <= BME_MAX_TABLE_SIZE);
224 ret = bdrv_pread(bs->file, tb->offset,
225 table, tb->size * sizeof(uint64_t));
226 if (ret < 0) {
227 goto fail;
230 for (i = 0; i < tb->size; ++i) {
231 table[i] = be64_to_cpu(table[i]);
232 ret = check_table_entry(table[i], s->cluster_size);
233 if (ret < 0) {
234 goto fail;
238 *bitmap_table = table;
239 return 0;
241 fail:
242 g_free(table);
244 return ret;
247 static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
249 int ret;
250 uint64_t *bitmap_table;
252 ret = bitmap_table_load(bs, tb, &bitmap_table);
253 if (ret < 0) {
254 return ret;
257 clear_bitmap_table(bs, bitmap_table, tb->size);
258 qcow2_free_clusters(bs, tb->offset, tb->size * sizeof(uint64_t),
259 QCOW2_DISCARD_OTHER);
260 g_free(bitmap_table);
262 tb->offset = 0;
263 tb->size = 0;
265 return 0;
268 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
269 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State *s,
270 const BdrvDirtyBitmap *bitmap)
272 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
273 uint64_t limit = granularity * (s->cluster_size << 3);
275 assert(QEMU_IS_ALIGNED(limit,
276 bdrv_dirty_bitmap_serialization_align(bitmap)));
277 return limit;
280 /* load_bitmap_data
281 * @bitmap_table entries must satisfy specification constraints.
282 * @bitmap must be cleared */
283 static int load_bitmap_data(BlockDriverState *bs,
284 const uint64_t *bitmap_table,
285 uint32_t bitmap_table_size,
286 BdrvDirtyBitmap *bitmap)
288 int ret = 0;
289 BDRVQcow2State *s = bs->opaque;
290 uint64_t offset, limit;
291 uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
292 uint8_t *buf = NULL;
293 uint64_t i, tab_size =
294 size_to_clusters(s,
295 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
297 if (tab_size != bitmap_table_size || tab_size > BME_MAX_TABLE_SIZE) {
298 return -EINVAL;
301 buf = g_malloc(s->cluster_size);
302 limit = bytes_covered_by_bitmap_cluster(s, bitmap);
303 for (i = 0, offset = 0; i < tab_size; ++i, offset += limit) {
304 uint64_t count = MIN(bm_size - offset, limit);
305 uint64_t entry = bitmap_table[i];
306 uint64_t data_offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
308 assert(check_table_entry(entry, s->cluster_size) == 0);
310 if (data_offset == 0) {
311 if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
312 bdrv_dirty_bitmap_deserialize_ones(bitmap, offset, count,
313 false);
314 } else {
315 /* No need to deserialize zeros because the dirty bitmap is
316 * already cleared */
318 } else {
319 ret = bdrv_pread(bs->file, data_offset, buf, s->cluster_size);
320 if (ret < 0) {
321 goto finish;
323 bdrv_dirty_bitmap_deserialize_part(bitmap, buf, offset, count,
324 false);
327 ret = 0;
329 bdrv_dirty_bitmap_deserialize_finish(bitmap);
331 finish:
332 g_free(buf);
334 return ret;
337 static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs,
338 Qcow2Bitmap *bm, Error **errp)
340 int ret;
341 uint64_t *bitmap_table = NULL;
342 uint32_t granularity;
343 BdrvDirtyBitmap *bitmap = NULL;
345 granularity = 1U << bm->granularity_bits;
346 bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
347 if (bitmap == NULL) {
348 goto fail;
351 if (bm->flags & BME_FLAG_IN_USE) {
352 /* Data is unusable, skip loading it */
353 return bitmap;
356 ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
357 if (ret < 0) {
358 error_setg_errno(errp, -ret,
359 "Could not read bitmap_table table from image for "
360 "bitmap '%s'", bm->name);
361 goto fail;
364 ret = load_bitmap_data(bs, bitmap_table, bm->table.size, bitmap);
365 if (ret < 0) {
366 error_setg_errno(errp, -ret, "Could not read bitmap '%s' from image",
367 bm->name);
368 goto fail;
371 g_free(bitmap_table);
372 return bitmap;
374 fail:
375 g_free(bitmap_table);
376 if (bitmap != NULL) {
377 bdrv_release_dirty_bitmap(bs, bitmap);
380 return NULL;
384 * Bitmap List
388 * Bitmap List private functions
389 * Only Bitmap List knows about bitmap directory structure in Qcow2.
392 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry *entry)
394 entry->bitmap_table_offset = be64_to_cpu(entry->bitmap_table_offset);
395 entry->bitmap_table_size = be32_to_cpu(entry->bitmap_table_size);
396 entry->flags = be32_to_cpu(entry->flags);
397 entry->name_size = be16_to_cpu(entry->name_size);
398 entry->extra_data_size = be32_to_cpu(entry->extra_data_size);
401 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry *entry)
403 entry->bitmap_table_offset = cpu_to_be64(entry->bitmap_table_offset);
404 entry->bitmap_table_size = cpu_to_be32(entry->bitmap_table_size);
405 entry->flags = cpu_to_be32(entry->flags);
406 entry->name_size = cpu_to_be16(entry->name_size);
407 entry->extra_data_size = cpu_to_be32(entry->extra_data_size);
410 static inline int calc_dir_entry_size(size_t name_size, size_t extra_data_size)
412 int size = sizeof(Qcow2BitmapDirEntry) + name_size + extra_data_size;
413 return ROUND_UP(size, 8);
416 static inline int dir_entry_size(Qcow2BitmapDirEntry *entry)
418 return calc_dir_entry_size(entry->name_size, entry->extra_data_size);
421 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry *entry)
423 return (const char *)(entry + 1) + entry->extra_data_size;
426 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry *entry)
428 const char *name_field = dir_entry_name_field(entry);
429 return g_strndup(name_field, entry->name_size);
432 static inline Qcow2BitmapDirEntry *next_dir_entry(Qcow2BitmapDirEntry *entry)
434 return (Qcow2BitmapDirEntry *)((uint8_t *)entry + dir_entry_size(entry));
437 static int check_dir_entry(BlockDriverState *bs, Qcow2BitmapDirEntry *entry)
439 BDRVQcow2State *s = bs->opaque;
440 uint64_t phys_bitmap_bytes;
441 int64_t len;
443 bool fail = (entry->bitmap_table_size == 0) ||
444 (entry->bitmap_table_offset == 0) ||
445 (entry->bitmap_table_offset % s->cluster_size) ||
446 (entry->bitmap_table_size > BME_MAX_TABLE_SIZE) ||
447 (entry->granularity_bits > BME_MAX_GRANULARITY_BITS) ||
448 (entry->granularity_bits < BME_MIN_GRANULARITY_BITS) ||
449 (entry->flags & BME_RESERVED_FLAGS) ||
450 (entry->name_size > BME_MAX_NAME_SIZE) ||
451 (entry->type != BT_DIRTY_TRACKING_BITMAP);
453 if (fail) {
454 return -EINVAL;
457 phys_bitmap_bytes = (uint64_t)entry->bitmap_table_size * s->cluster_size;
458 len = bdrv_getlength(bs);
460 if (len < 0) {
461 return len;
464 if (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) {
465 return -EINVAL;
468 if (!(entry->flags & BME_FLAG_IN_USE) &&
469 (len > ((phys_bitmap_bytes * 8) << entry->granularity_bits)))
472 * We've loaded a valid bitmap (IN_USE not set) or we are going to
473 * store a valid bitmap, but the allocated bitmap table size is not
474 * enough to store this bitmap.
476 * Note, that it's OK to have an invalid bitmap with invalid size due
477 * to a bitmap that was not correctly saved after image resize.
479 return -EINVAL;
482 return 0;
485 static inline void bitmap_directory_to_be(uint8_t *dir, size_t size)
487 uint8_t *end = dir + size;
488 while (dir < end) {
489 Qcow2BitmapDirEntry *e = (Qcow2BitmapDirEntry *)dir;
490 dir += dir_entry_size(e);
492 bitmap_dir_entry_to_be(e);
497 * Bitmap List public functions
500 static void bitmap_free(Qcow2Bitmap *bm)
502 if (bm == NULL) {
503 return;
506 g_free(bm->name);
507 g_free(bm);
510 static void bitmap_list_free(Qcow2BitmapList *bm_list)
512 Qcow2Bitmap *bm;
514 if (bm_list == NULL) {
515 return;
518 while ((bm = QSIMPLEQ_FIRST(bm_list)) != NULL) {
519 QSIMPLEQ_REMOVE_HEAD(bm_list, entry);
520 bitmap_free(bm);
523 g_free(bm_list);
526 static Qcow2BitmapList *bitmap_list_new(void)
528 Qcow2BitmapList *bm_list = g_new(Qcow2BitmapList, 1);
529 QSIMPLEQ_INIT(bm_list);
531 return bm_list;
534 static uint32_t bitmap_list_count(Qcow2BitmapList *bm_list)
536 Qcow2Bitmap *bm;
537 uint32_t nb_bitmaps = 0;
539 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
540 nb_bitmaps++;
543 return nb_bitmaps;
546 /* bitmap_list_load
547 * Get bitmap list from qcow2 image. Actually reads bitmap directory,
548 * checks it and convert to bitmap list.
550 static Qcow2BitmapList *bitmap_list_load(BlockDriverState *bs, uint64_t offset,
551 uint64_t size, Error **errp)
553 int ret;
554 BDRVQcow2State *s = bs->opaque;
555 uint8_t *dir, *dir_end;
556 Qcow2BitmapDirEntry *e;
557 uint32_t nb_dir_entries = 0;
558 Qcow2BitmapList *bm_list = NULL;
560 if (size == 0) {
561 error_setg(errp, "Requested bitmap directory size is zero");
562 return NULL;
565 if (size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
566 error_setg(errp, "Requested bitmap directory size is too big");
567 return NULL;
570 dir = g_try_malloc(size);
571 if (dir == NULL) {
572 error_setg(errp, "Failed to allocate space for bitmap directory");
573 return NULL;
575 dir_end = dir + size;
577 ret = bdrv_pread(bs->file, offset, dir, size);
578 if (ret < 0) {
579 error_setg_errno(errp, -ret, "Failed to read bitmap directory");
580 goto fail;
583 bm_list = bitmap_list_new();
584 for (e = (Qcow2BitmapDirEntry *)dir;
585 e < (Qcow2BitmapDirEntry *)dir_end;
586 e = next_dir_entry(e))
588 Qcow2Bitmap *bm;
590 if ((uint8_t *)(e + 1) > dir_end) {
591 goto broken_dir;
594 if (++nb_dir_entries > s->nb_bitmaps) {
595 error_setg(errp, "More bitmaps found than specified in header"
596 " extension");
597 goto fail;
599 bitmap_dir_entry_to_cpu(e);
601 if ((uint8_t *)next_dir_entry(e) > dir_end) {
602 goto broken_dir;
605 if (e->extra_data_size != 0) {
606 error_setg(errp, "Bitmap extra data is not supported");
607 goto fail;
610 ret = check_dir_entry(bs, e);
611 if (ret < 0) {
612 error_setg(errp, "Bitmap '%.*s' doesn't satisfy the constraints",
613 e->name_size, dir_entry_name_field(e));
614 goto fail;
617 bm = g_new0(Qcow2Bitmap, 1);
618 bm->table.offset = e->bitmap_table_offset;
619 bm->table.size = e->bitmap_table_size;
620 bm->flags = e->flags;
621 bm->granularity_bits = e->granularity_bits;
622 bm->name = dir_entry_copy_name(e);
623 QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry);
626 if (nb_dir_entries != s->nb_bitmaps) {
627 error_setg(errp, "Less bitmaps found than specified in header"
628 " extension");
629 goto fail;
632 if ((uint8_t *)e != dir_end) {
633 goto broken_dir;
636 g_free(dir);
637 return bm_list;
639 broken_dir:
640 ret = -EINVAL;
641 error_setg(errp, "Broken bitmap directory");
643 fail:
644 g_free(dir);
645 bitmap_list_free(bm_list);
647 return NULL;
650 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
651 void **refcount_table,
652 int64_t *refcount_table_size)
654 int ret;
655 BDRVQcow2State *s = bs->opaque;
656 Qcow2BitmapList *bm_list;
657 Qcow2Bitmap *bm;
659 if (s->nb_bitmaps == 0) {
660 return 0;
663 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
664 s->bitmap_directory_offset,
665 s->bitmap_directory_size);
666 if (ret < 0) {
667 return ret;
670 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
671 s->bitmap_directory_size, NULL);
672 if (bm_list == NULL) {
673 res->corruptions++;
674 return -EINVAL;
677 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
678 uint64_t *bitmap_table = NULL;
679 int i;
681 ret = qcow2_inc_refcounts_imrt(bs, res,
682 refcount_table, refcount_table_size,
683 bm->table.offset,
684 bm->table.size * sizeof(uint64_t));
685 if (ret < 0) {
686 goto out;
689 ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
690 if (ret < 0) {
691 res->corruptions++;
692 goto out;
695 for (i = 0; i < bm->table.size; ++i) {
696 uint64_t entry = bitmap_table[i];
697 uint64_t offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
699 if (check_table_entry(entry, s->cluster_size) < 0) {
700 res->corruptions++;
701 continue;
704 if (offset == 0) {
705 continue;
708 ret = qcow2_inc_refcounts_imrt(bs, res,
709 refcount_table, refcount_table_size,
710 offset, s->cluster_size);
711 if (ret < 0) {
712 g_free(bitmap_table);
713 goto out;
717 g_free(bitmap_table);
720 out:
721 bitmap_list_free(bm_list);
723 return ret;
726 /* bitmap_list_store
727 * Store bitmap list to qcow2 image as a bitmap directory.
728 * Everything is checked.
730 static int bitmap_list_store(BlockDriverState *bs, Qcow2BitmapList *bm_list,
731 uint64_t *offset, uint64_t *size, bool in_place)
733 int ret;
734 uint8_t *dir;
735 int64_t dir_offset = 0;
736 uint64_t dir_size = 0;
737 Qcow2Bitmap *bm;
738 Qcow2BitmapDirEntry *e;
740 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
741 dir_size += calc_dir_entry_size(strlen(bm->name), 0);
744 if (dir_size == 0 || dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
745 return -EINVAL;
748 if (in_place) {
749 if (*size != dir_size || *offset == 0) {
750 return -EINVAL;
753 dir_offset = *offset;
756 dir = g_try_malloc0(dir_size);
757 if (dir == NULL) {
758 return -ENOMEM;
761 e = (Qcow2BitmapDirEntry *)dir;
762 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
763 e->bitmap_table_offset = bm->table.offset;
764 e->bitmap_table_size = bm->table.size;
765 e->flags = bm->flags;
766 e->type = BT_DIRTY_TRACKING_BITMAP;
767 e->granularity_bits = bm->granularity_bits;
768 e->name_size = strlen(bm->name);
769 e->extra_data_size = 0;
770 memcpy(e + 1, bm->name, e->name_size);
772 if (check_dir_entry(bs, e) < 0) {
773 ret = -EINVAL;
774 goto fail;
777 e = next_dir_entry(e);
780 bitmap_directory_to_be(dir, dir_size);
782 if (!in_place) {
783 dir_offset = qcow2_alloc_clusters(bs, dir_size);
784 if (dir_offset < 0) {
785 ret = dir_offset;
786 goto fail;
790 /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
791 * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
792 * directory in-place (actually, turn-off the extension), which is checked
793 * in qcow2_check_metadata_overlap() */
794 ret = qcow2_pre_write_overlap_check(
795 bs, in_place ? QCOW2_OL_BITMAP_DIRECTORY : 0, dir_offset, dir_size,
796 false);
797 if (ret < 0) {
798 goto fail;
801 ret = bdrv_pwrite(bs->file, dir_offset, dir, dir_size);
802 if (ret < 0) {
803 goto fail;
806 g_free(dir);
808 if (!in_place) {
809 *size = dir_size;
810 *offset = dir_offset;
813 return 0;
815 fail:
816 g_free(dir);
818 if (!in_place && dir_offset > 0) {
819 qcow2_free_clusters(bs, dir_offset, dir_size, QCOW2_DISCARD_OTHER);
822 return ret;
826 * Bitmap List end
829 static int update_ext_header_and_dir_in_place(BlockDriverState *bs,
830 Qcow2BitmapList *bm_list)
832 BDRVQcow2State *s = bs->opaque;
833 int ret;
835 if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS) ||
836 bm_list == NULL || QSIMPLEQ_EMPTY(bm_list) ||
837 bitmap_list_count(bm_list) != s->nb_bitmaps)
839 return -EINVAL;
842 s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
843 ret = update_header_sync(bs);
844 if (ret < 0) {
845 /* Two variants are possible here:
846 * 1. Autoclear flag is dropped, all bitmaps will be lost.
847 * 2. Autoclear flag is not dropped, old state is left.
849 return ret;
852 /* autoclear bit is not set, so we can safely update bitmap directory */
854 ret = bitmap_list_store(bs, bm_list, &s->bitmap_directory_offset,
855 &s->bitmap_directory_size, true);
856 if (ret < 0) {
857 /* autoclear bit is cleared, so all leaked clusters would be removed on
858 * qemu-img check */
859 return ret;
862 ret = update_header_sync(bs);
863 if (ret < 0) {
864 /* autoclear bit is cleared, so all leaked clusters would be removed on
865 * qemu-img check */
866 return ret;
869 s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
870 return update_header_sync(bs);
871 /* If final update_header_sync() fails, two variants are possible:
872 * 1. Autoclear flag is not set, all bitmaps will be lost.
873 * 2. Autoclear flag is set, header and directory are successfully updated.
877 static int update_ext_header_and_dir(BlockDriverState *bs,
878 Qcow2BitmapList *bm_list)
880 BDRVQcow2State *s = bs->opaque;
881 int ret;
882 uint64_t new_offset = 0;
883 uint64_t new_size = 0;
884 uint32_t new_nb_bitmaps = 0;
885 uint64_t old_offset = s->bitmap_directory_offset;
886 uint64_t old_size = s->bitmap_directory_size;
887 uint32_t old_nb_bitmaps = s->nb_bitmaps;
888 uint64_t old_autocl = s->autoclear_features;
890 if (bm_list != NULL && !QSIMPLEQ_EMPTY(bm_list)) {
891 new_nb_bitmaps = bitmap_list_count(bm_list);
893 if (new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
894 return -EINVAL;
897 ret = bitmap_list_store(bs, bm_list, &new_offset, &new_size, false);
898 if (ret < 0) {
899 return ret;
902 ret = qcow2_flush_caches(bs);
903 if (ret < 0) {
904 goto fail;
907 s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
908 } else {
909 s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
912 s->bitmap_directory_offset = new_offset;
913 s->bitmap_directory_size = new_size;
914 s->nb_bitmaps = new_nb_bitmaps;
916 ret = update_header_sync(bs);
917 if (ret < 0) {
918 goto fail;
921 if (old_size > 0) {
922 qcow2_free_clusters(bs, old_offset, old_size, QCOW2_DISCARD_OTHER);
925 return 0;
927 fail:
928 if (new_offset > 0) {
929 qcow2_free_clusters(bs, new_offset, new_size, QCOW2_DISCARD_OTHER);
932 s->bitmap_directory_offset = old_offset;
933 s->bitmap_directory_size = old_size;
934 s->nb_bitmaps = old_nb_bitmaps;
935 s->autoclear_features = old_autocl;
937 return ret;
940 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
941 static void release_dirty_bitmap_helper(gpointer bitmap,
942 gpointer bs)
944 bdrv_release_dirty_bitmap(bs, bitmap);
947 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
948 static void set_readonly_helper(gpointer bitmap, gpointer value)
950 bdrv_dirty_bitmap_set_readonly(bitmap, (bool)value);
953 /* qcow2_load_dirty_bitmaps()
954 * Return value is a hint for caller: true means that the Qcow2 header was
955 * updated. (false doesn't mean that the header should be updated by the
956 * caller, it just means that updating was not needed or the image cannot be
957 * written to).
958 * On failure the function returns false.
960 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
962 BDRVQcow2State *s = bs->opaque;
963 Qcow2BitmapList *bm_list;
964 Qcow2Bitmap *bm;
965 GSList *created_dirty_bitmaps = NULL;
966 bool header_updated = false;
967 bool needs_update = false;
969 if (s->nb_bitmaps == 0) {
970 /* No bitmaps - nothing to do */
971 return false;
974 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
975 s->bitmap_directory_size, errp);
976 if (bm_list == NULL) {
977 return false;
980 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
981 BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp);
982 if (bitmap == NULL) {
983 goto fail;
986 bdrv_dirty_bitmap_set_persistence(bitmap, true);
987 if (bm->flags & BME_FLAG_IN_USE) {
988 bdrv_dirty_bitmap_set_inconsistent(bitmap);
989 } else {
990 /* NB: updated flags only get written if can_write(bs) is true. */
991 bm->flags |= BME_FLAG_IN_USE;
992 needs_update = true;
994 if (!(bm->flags & BME_FLAG_AUTO)) {
995 bdrv_disable_dirty_bitmap(bitmap);
997 created_dirty_bitmaps =
998 g_slist_append(created_dirty_bitmaps, bitmap);
1001 if (needs_update && can_write(bs)) {
1002 /* in_use flags must be updated */
1003 int ret = update_ext_header_and_dir_in_place(bs, bm_list);
1004 if (ret < 0) {
1005 error_setg_errno(errp, -ret, "Can't update bitmap directory");
1006 goto fail;
1008 header_updated = true;
1011 if (!can_write(bs)) {
1012 g_slist_foreach(created_dirty_bitmaps, set_readonly_helper,
1013 (gpointer)true);
1016 g_slist_free(created_dirty_bitmaps);
1017 bitmap_list_free(bm_list);
1019 return header_updated;
1021 fail:
1022 g_slist_foreach(created_dirty_bitmaps, release_dirty_bitmap_helper, bs);
1023 g_slist_free(created_dirty_bitmaps);
1024 bitmap_list_free(bm_list);
1026 return false;
1030 static Qcow2BitmapInfoFlagsList *get_bitmap_info_flags(uint32_t flags)
1032 Qcow2BitmapInfoFlagsList *list = NULL;
1033 Qcow2BitmapInfoFlagsList **plist = &list;
1034 int i;
1036 static const struct {
1037 int bme; /* Bitmap directory entry flags */
1038 int info; /* The flags to report to the user */
1039 } map[] = {
1040 { BME_FLAG_IN_USE, QCOW2_BITMAP_INFO_FLAGS_IN_USE },
1041 { BME_FLAG_AUTO, QCOW2_BITMAP_INFO_FLAGS_AUTO },
1044 int map_size = ARRAY_SIZE(map);
1046 for (i = 0; i < map_size; ++i) {
1047 if (flags & map[i].bme) {
1048 Qcow2BitmapInfoFlagsList *entry =
1049 g_new0(Qcow2BitmapInfoFlagsList, 1);
1050 entry->value = map[i].info;
1051 *plist = entry;
1052 plist = &entry->next;
1053 flags &= ~map[i].bme;
1056 /* Check if the BME_* mapping above is complete */
1057 assert(!flags);
1059 return list;
1063 * qcow2_get_bitmap_info_list()
1064 * Returns a list of QCOW2 bitmap details.
1065 * In case of no bitmaps, the function returns NULL and
1066 * the @errp parameter is not set.
1067 * When bitmap information can not be obtained, the function returns
1068 * NULL and the @errp parameter is set.
1070 Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
1071 Error **errp)
1073 BDRVQcow2State *s = bs->opaque;
1074 Qcow2BitmapList *bm_list;
1075 Qcow2Bitmap *bm;
1076 Qcow2BitmapInfoList *list = NULL;
1077 Qcow2BitmapInfoList **plist = &list;
1079 if (s->nb_bitmaps == 0) {
1080 return NULL;
1083 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1084 s->bitmap_directory_size, errp);
1085 if (bm_list == NULL) {
1086 return NULL;
1089 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1090 Qcow2BitmapInfo *info = g_new0(Qcow2BitmapInfo, 1);
1091 Qcow2BitmapInfoList *obj = g_new0(Qcow2BitmapInfoList, 1);
1092 info->granularity = 1U << bm->granularity_bits;
1093 info->name = g_strdup(bm->name);
1094 info->flags = get_bitmap_info_flags(bm->flags & ~BME_RESERVED_FLAGS);
1095 obj->value = info;
1096 *plist = obj;
1097 plist = &obj->next;
1100 bitmap_list_free(bm_list);
1102 return list;
1105 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
1106 Error **errp)
1108 BDRVQcow2State *s = bs->opaque;
1109 Qcow2BitmapList *bm_list;
1110 Qcow2Bitmap *bm;
1111 GSList *ro_dirty_bitmaps = NULL;
1112 int ret = 0;
1114 if (header_updated != NULL) {
1115 *header_updated = false;
1118 if (s->nb_bitmaps == 0) {
1119 /* No bitmaps - nothing to do */
1120 return 0;
1123 if (!can_write(bs)) {
1124 error_setg(errp, "Can't write to the image on reopening bitmaps rw");
1125 return -EINVAL;
1128 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1129 s->bitmap_directory_size, errp);
1130 if (bm_list == NULL) {
1131 return -EINVAL;
1134 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1135 BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
1136 if (bitmap == NULL) {
1137 continue;
1140 if (!bdrv_dirty_bitmap_readonly(bitmap)) {
1141 error_setg(errp, "Bitmap %s was loaded prior to rw-reopen, but was "
1142 "not marked as readonly. This is a bug, something went "
1143 "wrong. All of the bitmaps may be corrupted", bm->name);
1144 ret = -EINVAL;
1145 goto out;
1148 bm->flags |= BME_FLAG_IN_USE;
1149 ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap);
1152 if (ro_dirty_bitmaps != NULL) {
1153 /* in_use flags must be updated */
1154 ret = update_ext_header_and_dir_in_place(bs, bm_list);
1155 if (ret < 0) {
1156 error_setg_errno(errp, -ret, "Can't update bitmap directory");
1157 goto out;
1159 if (header_updated != NULL) {
1160 *header_updated = true;
1162 g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false);
1165 out:
1166 g_slist_free(ro_dirty_bitmaps);
1167 bitmap_list_free(bm_list);
1169 return ret;
1172 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp)
1174 return qcow2_reopen_bitmaps_rw_hint(bs, NULL, errp);
1177 /* Checks to see if it's safe to resize bitmaps */
1178 int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp)
1180 BDRVQcow2State *s = bs->opaque;
1181 Qcow2BitmapList *bm_list;
1182 Qcow2Bitmap *bm;
1183 int ret = 0;
1185 if (s->nb_bitmaps == 0) {
1186 return 0;
1189 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1190 s->bitmap_directory_size, errp);
1191 if (bm_list == NULL) {
1192 return -EINVAL;
1195 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1196 BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
1197 if (bitmap == NULL) {
1199 * We rely on all bitmaps being in-memory to be able to resize them,
1200 * Otherwise, we'd need to resize them on disk explicitly
1202 error_setg(errp, "Cannot resize qcow2 with persistent bitmaps that "
1203 "were not loaded into memory");
1204 ret = -ENOTSUP;
1205 goto out;
1209 * The checks against readonly and busy are redundant, but certainly
1210 * do no harm. checks against inconsistent are crucial:
1212 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
1213 ret = -ENOTSUP;
1214 goto out;
1218 out:
1219 bitmap_list_free(bm_list);
1220 return ret;
1223 /* store_bitmap_data()
1224 * Store bitmap to image, filling bitmap table accordingly.
1226 static uint64_t *store_bitmap_data(BlockDriverState *bs,
1227 BdrvDirtyBitmap *bitmap,
1228 uint32_t *bitmap_table_size, Error **errp)
1230 int ret;
1231 BDRVQcow2State *s = bs->opaque;
1232 int64_t offset;
1233 uint64_t limit;
1234 uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
1235 const char *bm_name = bdrv_dirty_bitmap_name(bitmap);
1236 uint8_t *buf = NULL;
1237 BdrvDirtyBitmapIter *dbi;
1238 uint64_t *tb;
1239 uint64_t tb_size =
1240 size_to_clusters(s,
1241 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
1243 if (tb_size > BME_MAX_TABLE_SIZE ||
1244 tb_size * s->cluster_size > BME_MAX_PHYS_SIZE)
1246 error_setg(errp, "Bitmap '%s' is too big", bm_name);
1247 return NULL;
1250 tb = g_try_new0(uint64_t, tb_size);
1251 if (tb == NULL) {
1252 error_setg(errp, "No memory");
1253 return NULL;
1256 dbi = bdrv_dirty_iter_new(bitmap);
1257 buf = g_malloc(s->cluster_size);
1258 limit = bytes_covered_by_bitmap_cluster(s, bitmap);
1259 assert(DIV_ROUND_UP(bm_size, limit) == tb_size);
1261 while ((offset = bdrv_dirty_iter_next(dbi)) >= 0) {
1262 uint64_t cluster = offset / limit;
1263 uint64_t end, write_size;
1264 int64_t off;
1267 * We found the first dirty offset, but want to write out the
1268 * entire cluster of the bitmap that includes that offset,
1269 * including any leading zero bits.
1271 offset = QEMU_ALIGN_DOWN(offset, limit);
1272 end = MIN(bm_size, offset + limit);
1273 write_size = bdrv_dirty_bitmap_serialization_size(bitmap, offset,
1274 end - offset);
1275 assert(write_size <= s->cluster_size);
1277 off = qcow2_alloc_clusters(bs, s->cluster_size);
1278 if (off < 0) {
1279 error_setg_errno(errp, -off,
1280 "Failed to allocate clusters for bitmap '%s'",
1281 bm_name);
1282 goto fail;
1284 tb[cluster] = off;
1286 bdrv_dirty_bitmap_serialize_part(bitmap, buf, offset, end - offset);
1287 if (write_size < s->cluster_size) {
1288 memset(buf + write_size, 0, s->cluster_size - write_size);
1291 ret = qcow2_pre_write_overlap_check(bs, 0, off, s->cluster_size, false);
1292 if (ret < 0) {
1293 error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1294 goto fail;
1297 ret = bdrv_pwrite(bs->file, off, buf, s->cluster_size);
1298 if (ret < 0) {
1299 error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1300 bm_name);
1301 goto fail;
1304 if (end >= bm_size) {
1305 break;
1308 bdrv_set_dirty_iter(dbi, end);
1311 *bitmap_table_size = tb_size;
1312 g_free(buf);
1313 bdrv_dirty_iter_free(dbi);
1315 return tb;
1317 fail:
1318 clear_bitmap_table(bs, tb, tb_size);
1319 g_free(buf);
1320 bdrv_dirty_iter_free(dbi);
1321 g_free(tb);
1323 return NULL;
1326 /* store_bitmap()
1327 * Store bm->dirty_bitmap to qcow2.
1328 * Set bm->table_offset and bm->table_size accordingly.
1330 static int store_bitmap(BlockDriverState *bs, Qcow2Bitmap *bm, Error **errp)
1332 int ret;
1333 uint64_t *tb;
1334 int64_t tb_offset;
1335 uint32_t tb_size;
1336 BdrvDirtyBitmap *bitmap = bm->dirty_bitmap;
1337 const char *bm_name;
1339 assert(bitmap != NULL);
1341 bm_name = bdrv_dirty_bitmap_name(bitmap);
1343 tb = store_bitmap_data(bs, bitmap, &tb_size, errp);
1344 if (tb == NULL) {
1345 return -EINVAL;
1348 assert(tb_size <= BME_MAX_TABLE_SIZE);
1349 tb_offset = qcow2_alloc_clusters(bs, tb_size * sizeof(tb[0]));
1350 if (tb_offset < 0) {
1351 error_setg_errno(errp, -tb_offset,
1352 "Failed to allocate clusters for bitmap '%s'",
1353 bm_name);
1354 ret = tb_offset;
1355 goto fail;
1358 ret = qcow2_pre_write_overlap_check(bs, 0, tb_offset,
1359 tb_size * sizeof(tb[0]), false);
1360 if (ret < 0) {
1361 error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1362 goto fail;
1365 bitmap_table_to_be(tb, tb_size);
1366 ret = bdrv_pwrite(bs->file, tb_offset, tb, tb_size * sizeof(tb[0]));
1367 if (ret < 0) {
1368 error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1369 bm_name);
1370 goto fail;
1373 g_free(tb);
1375 bm->table.offset = tb_offset;
1376 bm->table.size = tb_size;
1378 return 0;
1380 fail:
1381 clear_bitmap_table(bs, tb, tb_size);
1383 if (tb_offset > 0) {
1384 qcow2_free_clusters(bs, tb_offset, tb_size * sizeof(tb[0]),
1385 QCOW2_DISCARD_OTHER);
1388 g_free(tb);
1390 return ret;
1393 static Qcow2Bitmap *find_bitmap_by_name(Qcow2BitmapList *bm_list,
1394 const char *name)
1396 Qcow2Bitmap *bm;
1398 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1399 if (strcmp(name, bm->name) == 0) {
1400 return bm;
1404 return NULL;
1407 int coroutine_fn qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs,
1408 const char *name,
1409 Error **errp)
1411 int ret;
1412 BDRVQcow2State *s = bs->opaque;
1413 Qcow2Bitmap *bm = NULL;
1414 Qcow2BitmapList *bm_list;
1416 if (s->nb_bitmaps == 0) {
1417 /* Absence of the bitmap is not an error: see explanation above
1418 * bdrv_remove_persistent_dirty_bitmap() definition. */
1419 return 0;
1422 qemu_co_mutex_lock(&s->lock);
1424 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1425 s->bitmap_directory_size, errp);
1426 if (bm_list == NULL) {
1427 ret = -EIO;
1428 goto out;
1431 bm = find_bitmap_by_name(bm_list, name);
1432 if (bm == NULL) {
1433 ret = -EINVAL;
1434 goto out;
1437 QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
1439 ret = update_ext_header_and_dir(bs, bm_list);
1440 if (ret < 0) {
1441 error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1442 goto out;
1445 free_bitmap_clusters(bs, &bm->table);
1447 out:
1448 qemu_co_mutex_unlock(&s->lock);
1450 bitmap_free(bm);
1451 bitmap_list_free(bm_list);
1453 return ret;
1456 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp)
1458 BdrvDirtyBitmap *bitmap;
1459 BDRVQcow2State *s = bs->opaque;
1460 uint32_t new_nb_bitmaps = s->nb_bitmaps;
1461 uint64_t new_dir_size = s->bitmap_directory_size;
1462 int ret;
1463 Qcow2BitmapList *bm_list;
1464 Qcow2Bitmap *bm;
1465 QSIMPLEQ_HEAD(, Qcow2BitmapTable) drop_tables;
1466 Qcow2BitmapTable *tb, *tb_next;
1468 if (!bdrv_has_changed_persistent_bitmaps(bs)) {
1469 /* nothing to do */
1470 return;
1473 if (!can_write(bs)) {
1474 error_setg(errp, "No write access");
1475 return;
1478 QSIMPLEQ_INIT(&drop_tables);
1480 if (s->nb_bitmaps == 0) {
1481 bm_list = bitmap_list_new();
1482 } else {
1483 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1484 s->bitmap_directory_size, errp);
1485 if (bm_list == NULL) {
1486 return;
1490 /* check constraints and names */
1491 for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1492 bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1494 const char *name = bdrv_dirty_bitmap_name(bitmap);
1495 uint32_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
1496 Qcow2Bitmap *bm;
1498 if (!bdrv_dirty_bitmap_get_persistence(bitmap) ||
1499 bdrv_dirty_bitmap_readonly(bitmap) ||
1500 bdrv_dirty_bitmap_inconsistent(bitmap)) {
1501 continue;
1504 if (check_constraints_on_bitmap(bs, name, granularity, errp) < 0) {
1505 error_prepend(errp, "Bitmap '%s' doesn't satisfy the constraints: ",
1506 name);
1507 goto fail;
1510 bm = find_bitmap_by_name(bm_list, name);
1511 if (bm == NULL) {
1512 if (++new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
1513 error_setg(errp, "Too many persistent bitmaps");
1514 goto fail;
1517 new_dir_size += calc_dir_entry_size(strlen(name), 0);
1518 if (new_dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
1519 error_setg(errp, "Bitmap directory is too large");
1520 goto fail;
1523 bm = g_new0(Qcow2Bitmap, 1);
1524 bm->name = g_strdup(name);
1525 QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry);
1526 } else {
1527 if (!(bm->flags & BME_FLAG_IN_USE)) {
1528 error_setg(errp, "Bitmap '%s' already exists in the image",
1529 name);
1530 goto fail;
1532 tb = g_memdup(&bm->table, sizeof(bm->table));
1533 bm->table.offset = 0;
1534 bm->table.size = 0;
1535 QSIMPLEQ_INSERT_TAIL(&drop_tables, tb, entry);
1537 bm->flags = bdrv_dirty_bitmap_enabled(bitmap) ? BME_FLAG_AUTO : 0;
1538 bm->granularity_bits = ctz32(bdrv_dirty_bitmap_granularity(bitmap));
1539 bm->dirty_bitmap = bitmap;
1542 /* allocate clusters and store bitmaps */
1543 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1544 if (bm->dirty_bitmap == NULL) {
1545 continue;
1548 ret = store_bitmap(bs, bm, errp);
1549 if (ret < 0) {
1550 goto fail;
1554 ret = update_ext_header_and_dir(bs, bm_list);
1555 if (ret < 0) {
1556 error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1557 goto fail;
1560 /* Bitmap directory was successfully updated, so, old data can be dropped.
1561 * TODO it is better to reuse these clusters */
1562 QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1563 free_bitmap_clusters(bs, tb);
1564 g_free(tb);
1567 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1568 /* For safety, we remove bitmap after storing.
1569 * We may be here in two cases:
1570 * 1. bdrv_close. It's ok to drop bitmap.
1571 * 2. inactivation. It means migration without 'dirty-bitmaps'
1572 * capability, so bitmaps are not marked with
1573 * BdrvDirtyBitmap.migration flags. It's not bad to drop them too,
1574 * and reload on invalidation.
1576 if (bm->dirty_bitmap == NULL) {
1577 continue;
1580 bdrv_release_dirty_bitmap(bs, bm->dirty_bitmap);
1583 bitmap_list_free(bm_list);
1584 return;
1586 fail:
1587 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1588 if (bm->dirty_bitmap == NULL || bm->table.offset == 0) {
1589 continue;
1592 free_bitmap_clusters(bs, &bm->table);
1595 QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1596 g_free(tb);
1599 bitmap_list_free(bm_list);
1602 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp)
1604 BdrvDirtyBitmap *bitmap;
1605 Error *local_err = NULL;
1607 qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
1608 if (local_err != NULL) {
1609 error_propagate(errp, local_err);
1610 return -EINVAL;
1613 for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1614 bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1616 if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
1617 bdrv_dirty_bitmap_set_readonly(bitmap, true);
1621 return 0;
1624 bool coroutine_fn qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
1625 const char *name,
1626 uint32_t granularity,
1627 Error **errp)
1629 BDRVQcow2State *s = bs->opaque;
1630 bool found;
1631 Qcow2BitmapList *bm_list;
1633 if (s->qcow_version < 3) {
1634 /* Without autoclear_features, we would always have to assume
1635 * that a program without persistent dirty bitmap support has
1636 * accessed this qcow2 file when opening it, and would thus
1637 * have to drop all dirty bitmaps (defeating their purpose).
1639 error_setg(errp, "Cannot store dirty bitmaps in qcow2 v2 files");
1640 goto fail;
1643 if (check_constraints_on_bitmap(bs, name, granularity, errp) != 0) {
1644 goto fail;
1647 if (s->nb_bitmaps == 0) {
1648 return true;
1651 if (s->nb_bitmaps >= QCOW2_MAX_BITMAPS) {
1652 error_setg(errp,
1653 "Maximum number of persistent bitmaps is already reached");
1654 goto fail;
1657 if (s->bitmap_directory_size + calc_dir_entry_size(strlen(name), 0) >
1658 QCOW2_MAX_BITMAP_DIRECTORY_SIZE)
1660 error_setg(errp, "Not enough space in the bitmap directory");
1661 goto fail;
1664 qemu_co_mutex_lock(&s->lock);
1665 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1666 s->bitmap_directory_size, errp);
1667 qemu_co_mutex_unlock(&s->lock);
1668 if (bm_list == NULL) {
1669 goto fail;
1672 found = find_bitmap_by_name(bm_list, name);
1673 bitmap_list_free(bm_list);
1674 if (found) {
1675 error_setg(errp, "Bitmap with the same name is already stored");
1676 goto fail;
1679 return true;
1681 fail:
1682 error_prepend(errp, "Can't make bitmap '%s' persistent in '%s': ",
1683 name, bdrv_get_device_or_node_name(bs));
1684 return false;