virtio-gpu: fix crashes upon warm reboot with vga mode
[qemu/ar7.git] / block / qcow2-bitmap.c
blobba978ad2aacc1312d9b75116673527c593545b5d
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 "block/block_int.h"
33 #include "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
48 #endif
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;
65 uint32_t flags;
67 uint8_t type;
68 uint8_t granularity_bits;
69 uint16_t name_size;
70 uint32_t extra_data_size;
71 /* extra data follows */
72 /* name follows */
73 } Qcow2BitmapDirEntry;
75 typedef struct Qcow2BitmapTable {
76 uint64_t offset;
77 uint32_t size; /* number of 64bit entries */
78 QSIMPLEQ_ENTRY(Qcow2BitmapTable) entry;
79 } Qcow2BitmapTable;
80 typedef QSIMPLEQ_HEAD(Qcow2BitmapTableList, Qcow2BitmapTable)
81 Qcow2BitmapTableList;
83 typedef struct Qcow2Bitmap {
84 Qcow2BitmapTable table;
85 uint32_t flags;
86 uint8_t granularity_bits;
87 char *name;
89 BdrvDirtyBitmap *dirty_bitmap;
91 QSIMPLEQ_ENTRY(Qcow2Bitmap) entry;
92 } Qcow2Bitmap;
93 typedef QSIMPLEQ_HEAD(Qcow2BitmapList, Qcow2Bitmap) Qcow2BitmapList;
95 typedef enum BitmapType {
96 BT_DIRTY_TRACKING_BITMAP = 1
97 } BitmapType;
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)
106 int ret;
108 ret = qcow2_update_header(bs);
109 if (ret < 0) {
110 return ret;
113 return bdrv_flush(bs->file->bs);
116 static inline void bitmap_table_to_be(uint64_t *bitmap_table, size_t size)
118 size_t i;
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)
127 uint64_t offset;
129 if (entry & BME_TABLE_ENTRY_RESERVED_MASK) {
130 return -EINVAL;
133 offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
134 if (offset != 0) {
135 /* if offset specified, bit 0 is reserved */
136 if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
137 return -EINVAL;
140 if (offset % cluster_size != 0) {
141 return -EINVAL;
145 return 0;
148 static int check_constraints_on_bitmap(BlockDriverState *bs,
149 const char *name,
150 uint32_t granularity,
151 Error **errp)
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);
160 if (len < 0) {
161 error_setg_errno(errp, -len, "Failed to get size of '%s'",
162 bdrv_get_device_or_node_name(bs));
163 return len;
166 if (granularity_bits > BME_MAX_GRANULARITY_BITS) {
167 error_setg(errp, "Granularity exceeds maximum (%llu bytes)",
168 1ULL << BME_MAX_GRANULARITY_BITS);
169 return -EINVAL;
171 if (granularity_bits < BME_MIN_GRANULARITY_BITS) {
172 error_setg(errp, "Granularity is under minimum (%llu bytes)",
173 1ULL << BME_MIN_GRANULARITY_BITS);
174 return -EINVAL;
177 if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
178 (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
179 granularity_bits))
181 error_setg(errp, "Too much space will be occupied by the bitmap. "
182 "Use larger granularity");
183 return -EINVAL;
186 if (strlen(name) > BME_MAX_NAME_SIZE) {
187 error_setg(errp, "Name length exceeds maximum (%u characters)",
188 BME_MAX_NAME_SIZE);
189 return -EINVAL;
192 return 0;
195 static void clear_bitmap_table(BlockDriverState *bs, uint64_t *bitmap_table,
196 uint32_t bitmap_table_size)
198 BDRVQcow2State *s = bs->opaque;
199 int i;
201 for (i = 0; i < bitmap_table_size; ++i) {
202 uint64_t addr = bitmap_table[i] & BME_TABLE_ENTRY_OFFSET_MASK;
203 if (!addr) {
204 continue;
207 qcow2_free_clusters(bs, addr, s->cluster_size, QCOW2_DISCARD_OTHER);
208 bitmap_table[i] = 0;
212 static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb,
213 uint64_t **bitmap_table)
215 int ret;
216 BDRVQcow2State *s = bs->opaque;
217 uint32_t i;
218 uint64_t *table;
220 assert(tb->size != 0);
221 table = g_try_new(uint64_t, tb->size);
222 if (table == NULL) {
223 return -ENOMEM;
226 assert(tb->size <= BME_MAX_TABLE_SIZE);
227 ret = bdrv_pread(bs->file, tb->offset,
228 table, tb->size * sizeof(uint64_t));
229 if (ret < 0) {
230 goto fail;
233 for (i = 0; i < tb->size; ++i) {
234 be64_to_cpus(&table[i]);
235 ret = check_table_entry(table[i], s->cluster_size);
236 if (ret < 0) {
237 goto fail;
241 *bitmap_table = table;
242 return 0;
244 fail:
245 g_free(table);
247 return ret;
250 static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
252 int ret;
253 uint64_t *bitmap_table;
255 ret = bitmap_table_load(bs, tb, &bitmap_table);
256 if (ret < 0) {
257 return ret;
260 clear_bitmap_table(bs, bitmap_table, tb->size);
261 qcow2_free_clusters(bs, tb->offset, tb->size * sizeof(uint64_t),
262 QCOW2_DISCARD_OTHER);
263 g_free(bitmap_table);
265 tb->offset = 0;
266 tb->size = 0;
268 return 0;
271 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
272 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State *s,
273 const BdrvDirtyBitmap *bitmap)
275 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
276 uint64_t limit = granularity * (s->cluster_size << 3);
278 assert(QEMU_IS_ALIGNED(limit,
279 bdrv_dirty_bitmap_serialization_align(bitmap)));
280 return limit;
283 /* load_bitmap_data
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)
291 int ret = 0;
292 BDRVQcow2State *s = bs->opaque;
293 uint64_t offset, limit;
294 uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
295 uint8_t *buf = NULL;
296 uint64_t i, tab_size =
297 size_to_clusters(s,
298 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
300 if (tab_size != bitmap_table_size || tab_size > BME_MAX_TABLE_SIZE) {
301 return -EINVAL;
304 buf = g_malloc(s->cluster_size);
305 limit = bytes_covered_by_bitmap_cluster(s, bitmap);
306 for (i = 0, offset = 0; i < tab_size; ++i, offset += limit) {
307 uint64_t count = MIN(bm_size - offset, limit);
308 uint64_t entry = bitmap_table[i];
309 uint64_t data_offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
311 assert(check_table_entry(entry, s->cluster_size) == 0);
313 if (data_offset == 0) {
314 if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
315 bdrv_dirty_bitmap_deserialize_ones(bitmap, offset, count,
316 false);
317 } else {
318 /* No need to deserialize zeros because the dirty bitmap is
319 * already cleared */
321 } else {
322 ret = bdrv_pread(bs->file, data_offset, buf, s->cluster_size);
323 if (ret < 0) {
324 goto finish;
326 bdrv_dirty_bitmap_deserialize_part(bitmap, buf, offset, count,
327 false);
330 ret = 0;
332 bdrv_dirty_bitmap_deserialize_finish(bitmap);
334 finish:
335 g_free(buf);
337 return ret;
340 static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs,
341 Qcow2Bitmap *bm, Error **errp)
343 int ret;
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);
350 goto fail;
353 ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
354 if (ret < 0) {
355 error_setg_errno(errp, -ret,
356 "Could not read bitmap_table table from image for "
357 "bitmap '%s'", bm->name);
358 goto fail;
361 granularity = 1U << bm->granularity_bits;
362 bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
363 if (bitmap == NULL) {
364 goto fail;
367 ret = load_bitmap_data(bs, bitmap_table, bm->table.size, bitmap);
368 if (ret < 0) {
369 error_setg_errno(errp, -ret, "Could not read bitmap '%s' from image",
370 bm->name);
371 goto fail;
374 g_free(bitmap_table);
375 return bitmap;
377 fail:
378 g_free(bitmap_table);
379 if (bitmap != NULL) {
380 bdrv_release_dirty_bitmap(bs, bitmap);
383 return NULL;
387 * Bitmap List
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 int size = sizeof(Qcow2BitmapDirEntry) + name_size + extra_data_size;
416 return ROUND_UP(size, 8);
419 static inline int dir_entry_size(Qcow2BitmapDirEntry *entry)
421 return calc_dir_entry_size(entry->name_size, entry->extra_data_size);
424 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry *entry)
426 return (const char *)(entry + 1) + entry->extra_data_size;
429 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry *entry)
431 const char *name_field = dir_entry_name_field(entry);
432 return g_strndup(name_field, entry->name_size);
435 static inline Qcow2BitmapDirEntry *next_dir_entry(Qcow2BitmapDirEntry *entry)
437 return (Qcow2BitmapDirEntry *)((uint8_t *)entry + dir_entry_size(entry));
440 static int check_dir_entry(BlockDriverState *bs, Qcow2BitmapDirEntry *entry)
442 BDRVQcow2State *s = bs->opaque;
443 uint64_t phys_bitmap_bytes;
444 int64_t len;
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);
456 if (fail) {
457 return -EINVAL;
460 phys_bitmap_bytes = (uint64_t)entry->bitmap_table_size * s->cluster_size;
461 len = bdrv_getlength(bs);
463 if (len < 0) {
464 return len;
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;
476 while (dir < end) {
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)
490 if (bm == NULL) {
491 return;
494 g_free(bm->name);
495 g_free(bm);
498 static void bitmap_list_free(Qcow2BitmapList *bm_list)
500 Qcow2Bitmap *bm;
502 if (bm_list == NULL) {
503 return;
506 while ((bm = QSIMPLEQ_FIRST(bm_list)) != NULL) {
507 QSIMPLEQ_REMOVE_HEAD(bm_list, entry);
508 bitmap_free(bm);
511 g_free(bm_list);
514 static Qcow2BitmapList *bitmap_list_new(void)
516 Qcow2BitmapList *bm_list = g_new(Qcow2BitmapList, 1);
517 QSIMPLEQ_INIT(bm_list);
519 return bm_list;
522 static uint32_t bitmap_list_count(Qcow2BitmapList *bm_list)
524 Qcow2Bitmap *bm;
525 uint32_t nb_bitmaps = 0;
527 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
528 nb_bitmaps++;
531 return nb_bitmaps;
534 /* bitmap_list_load
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)
541 int ret;
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;
548 if (size == 0) {
549 error_setg(errp, "Requested bitmap directory size is zero");
550 return NULL;
553 if (size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
554 error_setg(errp, "Requested bitmap directory size is too big");
555 return NULL;
558 dir = g_try_malloc(size);
559 if (dir == NULL) {
560 error_setg(errp, "Failed to allocate space for bitmap directory");
561 return NULL;
563 dir_end = dir + size;
565 ret = bdrv_pread(bs->file, offset, dir, size);
566 if (ret < 0) {
567 error_setg_errno(errp, -ret, "Failed to read bitmap directory");
568 goto fail;
571 bm_list = bitmap_list_new();
572 for (e = (Qcow2BitmapDirEntry *)dir;
573 e < (Qcow2BitmapDirEntry *)dir_end;
574 e = next_dir_entry(e))
576 Qcow2Bitmap *bm;
578 if ((uint8_t *)(e + 1) > dir_end) {
579 goto broken_dir;
582 if (++nb_dir_entries > s->nb_bitmaps) {
583 error_setg(errp, "More bitmaps found than specified in header"
584 " extension");
585 goto fail;
587 bitmap_dir_entry_to_cpu(e);
589 if ((uint8_t *)next_dir_entry(e) > dir_end) {
590 goto broken_dir;
593 if (e->extra_data_size != 0) {
594 error_setg(errp, "Bitmap extra data is not supported");
595 goto fail;
598 ret = check_dir_entry(bs, e);
599 if (ret < 0) {
600 error_setg(errp, "Bitmap '%.*s' doesn't satisfy the constraints",
601 e->name_size, dir_entry_name_field(e));
602 goto fail;
605 bm = g_new0(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"
616 " extension");
617 goto fail;
620 if ((uint8_t *)e != dir_end) {
621 goto broken_dir;
624 g_free(dir);
625 return bm_list;
627 broken_dir:
628 ret = -EINVAL;
629 error_setg(errp, "Broken bitmap directory");
631 fail:
632 g_free(dir);
633 bitmap_list_free(bm_list);
635 return NULL;
638 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
639 void **refcount_table,
640 int64_t *refcount_table_size)
642 int ret;
643 BDRVQcow2State *s = bs->opaque;
644 Qcow2BitmapList *bm_list;
645 Qcow2Bitmap *bm;
647 if (s->nb_bitmaps == 0) {
648 return 0;
651 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
652 s->bitmap_directory_offset,
653 s->bitmap_directory_size);
654 if (ret < 0) {
655 return ret;
658 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
659 s->bitmap_directory_size, NULL);
660 if (bm_list == NULL) {
661 res->corruptions++;
662 return -EINVAL;
665 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
666 uint64_t *bitmap_table = NULL;
667 int i;
669 ret = qcow2_inc_refcounts_imrt(bs, res,
670 refcount_table, refcount_table_size,
671 bm->table.offset,
672 bm->table.size * sizeof(uint64_t));
673 if (ret < 0) {
674 goto out;
677 ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
678 if (ret < 0) {
679 res->corruptions++;
680 goto out;
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) {
688 res->corruptions++;
689 continue;
692 if (offset == 0) {
693 continue;
696 ret = qcow2_inc_refcounts_imrt(bs, res,
697 refcount_table, refcount_table_size,
698 offset, s->cluster_size);
699 if (ret < 0) {
700 g_free(bitmap_table);
701 goto out;
705 g_free(bitmap_table);
708 out:
709 bitmap_list_free(bm_list);
711 return ret;
714 /* bitmap_list_store
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)
721 int ret;
722 uint8_t *dir;
723 int64_t dir_offset = 0;
724 uint64_t dir_size = 0;
725 Qcow2Bitmap *bm;
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) {
733 return -EINVAL;
736 if (in_place) {
737 if (*size != dir_size || *offset == 0) {
738 return -EINVAL;
741 dir_offset = *offset;
744 dir = g_try_malloc(dir_size);
745 if (dir == NULL) {
746 return -ENOMEM;
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) {
761 ret = -EINVAL;
762 goto fail;
765 e = next_dir_entry(e);
768 bitmap_directory_to_be(dir, dir_size);
770 if (!in_place) {
771 dir_offset = qcow2_alloc_clusters(bs, dir_size);
772 if (dir_offset < 0) {
773 ret = dir_offset;
774 goto fail;
778 /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
779 * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
780 * directory in-place (actually, turn-off the extension), which is checked
781 * in qcow2_check_metadata_overlap() */
782 ret = qcow2_pre_write_overlap_check(
783 bs, in_place ? QCOW2_OL_BITMAP_DIRECTORY : 0, dir_offset, dir_size);
784 if (ret < 0) {
785 goto fail;
788 ret = bdrv_pwrite(bs->file, dir_offset, dir, dir_size);
789 if (ret < 0) {
790 goto fail;
793 g_free(dir);
795 if (!in_place) {
796 *size = dir_size;
797 *offset = dir_offset;
800 return 0;
802 fail:
803 g_free(dir);
805 if (!in_place && dir_offset > 0) {
806 qcow2_free_clusters(bs, dir_offset, dir_size, QCOW2_DISCARD_OTHER);
809 return ret;
813 * Bitmap List end
816 static int update_ext_header_and_dir_in_place(BlockDriverState *bs,
817 Qcow2BitmapList *bm_list)
819 BDRVQcow2State *s = bs->opaque;
820 int ret;
822 if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS) ||
823 bm_list == NULL || QSIMPLEQ_EMPTY(bm_list) ||
824 bitmap_list_count(bm_list) != s->nb_bitmaps)
826 return -EINVAL;
829 s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
830 ret = update_header_sync(bs);
831 if (ret < 0) {
832 /* Two variants are possible here:
833 * 1. Autoclear flag is dropped, all bitmaps will be lost.
834 * 2. Autoclear flag is not dropped, old state is left.
836 return ret;
839 /* autoclear bit is not set, so we can safely update bitmap directory */
841 ret = bitmap_list_store(bs, bm_list, &s->bitmap_directory_offset,
842 &s->bitmap_directory_size, true);
843 if (ret < 0) {
844 /* autoclear bit is cleared, so all leaked clusters would be removed on
845 * qemu-img check */
846 return ret;
849 ret = update_header_sync(bs);
850 if (ret < 0) {
851 /* autoclear bit is cleared, so all leaked clusters would be removed on
852 * qemu-img check */
853 return ret;
856 s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
857 return update_header_sync(bs);
858 /* If final update_header_sync() fails, two variants are possible:
859 * 1. Autoclear flag is not set, all bitmaps will be lost.
860 * 2. Autoclear flag is set, header and directory are successfully updated.
864 static int update_ext_header_and_dir(BlockDriverState *bs,
865 Qcow2BitmapList *bm_list)
867 BDRVQcow2State *s = bs->opaque;
868 int ret;
869 uint64_t new_offset = 0;
870 uint64_t new_size = 0;
871 uint32_t new_nb_bitmaps = 0;
872 uint64_t old_offset = s->bitmap_directory_offset;
873 uint64_t old_size = s->bitmap_directory_size;
874 uint32_t old_nb_bitmaps = s->nb_bitmaps;
875 uint64_t old_autocl = s->autoclear_features;
877 if (bm_list != NULL && !QSIMPLEQ_EMPTY(bm_list)) {
878 new_nb_bitmaps = bitmap_list_count(bm_list);
880 if (new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
881 return -EINVAL;
884 ret = bitmap_list_store(bs, bm_list, &new_offset, &new_size, false);
885 if (ret < 0) {
886 return ret;
889 ret = qcow2_flush_caches(bs);
890 if (ret < 0) {
891 goto fail;
894 s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
895 } else {
896 s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
899 s->bitmap_directory_offset = new_offset;
900 s->bitmap_directory_size = new_size;
901 s->nb_bitmaps = new_nb_bitmaps;
903 ret = update_header_sync(bs);
904 if (ret < 0) {
905 goto fail;
908 if (old_size > 0) {
909 qcow2_free_clusters(bs, old_offset, old_size, QCOW2_DISCARD_OTHER);
912 return 0;
914 fail:
915 if (new_offset > 0) {
916 qcow2_free_clusters(bs, new_offset, new_size, QCOW2_DISCARD_OTHER);
919 s->bitmap_directory_offset = old_offset;
920 s->bitmap_directory_size = old_size;
921 s->nb_bitmaps = old_nb_bitmaps;
922 s->autoclear_features = old_autocl;
924 return ret;
927 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
928 static void release_dirty_bitmap_helper(gpointer bitmap,
929 gpointer bs)
931 bdrv_release_dirty_bitmap(bs, bitmap);
934 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
935 static void set_readonly_helper(gpointer bitmap, gpointer value)
937 bdrv_dirty_bitmap_set_readonly(bitmap, (bool)value);
940 /* qcow2_load_dirty_bitmaps()
941 * Return value is a hint for caller: true means that the Qcow2 header was
942 * updated. (false doesn't mean that the header should be updated by the
943 * caller, it just means that updating was not needed or the image cannot be
944 * written to).
945 * On failure the function returns false.
947 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
949 BDRVQcow2State *s = bs->opaque;
950 Qcow2BitmapList *bm_list;
951 Qcow2Bitmap *bm;
952 GSList *created_dirty_bitmaps = NULL;
953 bool header_updated = false;
955 if (s->nb_bitmaps == 0) {
956 /* No bitmaps - nothing to do */
957 return false;
960 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
961 s->bitmap_directory_size, errp);
962 if (bm_list == NULL) {
963 return false;
966 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
967 if (!(bm->flags & BME_FLAG_IN_USE)) {
968 BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp);
969 if (bitmap == NULL) {
970 goto fail;
973 if (!(bm->flags & BME_FLAG_AUTO)) {
974 bdrv_disable_dirty_bitmap(bitmap);
976 bdrv_dirty_bitmap_set_persistance(bitmap, true);
977 bm->flags |= BME_FLAG_IN_USE;
978 created_dirty_bitmaps =
979 g_slist_append(created_dirty_bitmaps, bitmap);
983 if (created_dirty_bitmaps != NULL) {
984 if (can_write(bs)) {
985 /* in_use flags must be updated */
986 int ret = update_ext_header_and_dir_in_place(bs, bm_list);
987 if (ret < 0) {
988 error_setg_errno(errp, -ret, "Can't update bitmap directory");
989 goto fail;
991 header_updated = true;
992 } else {
993 g_slist_foreach(created_dirty_bitmaps, set_readonly_helper,
994 (gpointer)true);
998 g_slist_free(created_dirty_bitmaps);
999 bitmap_list_free(bm_list);
1001 return header_updated;
1003 fail:
1004 g_slist_foreach(created_dirty_bitmaps, release_dirty_bitmap_helper, bs);
1005 g_slist_free(created_dirty_bitmaps);
1006 bitmap_list_free(bm_list);
1008 return false;
1011 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
1012 Error **errp)
1014 BDRVQcow2State *s = bs->opaque;
1015 Qcow2BitmapList *bm_list;
1016 Qcow2Bitmap *bm;
1017 GSList *ro_dirty_bitmaps = NULL;
1018 int ret = 0;
1020 if (header_updated != NULL) {
1021 *header_updated = false;
1024 if (s->nb_bitmaps == 0) {
1025 /* No bitmaps - nothing to do */
1026 return 0;
1029 if (!can_write(bs)) {
1030 error_setg(errp, "Can't write to the image on reopening bitmaps rw");
1031 return -EINVAL;
1034 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1035 s->bitmap_directory_size, errp);
1036 if (bm_list == NULL) {
1037 return -EINVAL;
1040 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1041 if (!(bm->flags & BME_FLAG_IN_USE)) {
1042 BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
1043 if (bitmap == NULL) {
1044 continue;
1047 if (!bdrv_dirty_bitmap_readonly(bitmap)) {
1048 error_setg(errp, "Bitmap %s is not readonly but not marked"
1049 "'IN_USE' in the image. Something went wrong,"
1050 "all the bitmaps may be corrupted", bm->name);
1051 ret = -EINVAL;
1052 goto out;
1055 bm->flags |= BME_FLAG_IN_USE;
1056 ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap);
1060 if (ro_dirty_bitmaps != NULL) {
1061 /* in_use flags must be updated */
1062 ret = update_ext_header_and_dir_in_place(bs, bm_list);
1063 if (ret < 0) {
1064 error_setg_errno(errp, -ret, "Can't update bitmap directory");
1065 goto out;
1067 if (header_updated != NULL) {
1068 *header_updated = true;
1070 g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false);
1073 out:
1074 g_slist_free(ro_dirty_bitmaps);
1075 bitmap_list_free(bm_list);
1077 return ret;
1080 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp)
1082 return qcow2_reopen_bitmaps_rw_hint(bs, NULL, errp);
1085 /* store_bitmap_data()
1086 * Store bitmap to image, filling bitmap table accordingly.
1088 static uint64_t *store_bitmap_data(BlockDriverState *bs,
1089 BdrvDirtyBitmap *bitmap,
1090 uint32_t *bitmap_table_size, Error **errp)
1092 int ret;
1093 BDRVQcow2State *s = bs->opaque;
1094 int64_t offset;
1095 uint64_t limit;
1096 uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
1097 const char *bm_name = bdrv_dirty_bitmap_name(bitmap);
1098 uint8_t *buf = NULL;
1099 BdrvDirtyBitmapIter *dbi;
1100 uint64_t *tb;
1101 uint64_t tb_size =
1102 size_to_clusters(s,
1103 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
1105 if (tb_size > BME_MAX_TABLE_SIZE ||
1106 tb_size * s->cluster_size > BME_MAX_PHYS_SIZE)
1108 error_setg(errp, "Bitmap '%s' is too big", bm_name);
1109 return NULL;
1112 tb = g_try_new0(uint64_t, tb_size);
1113 if (tb == NULL) {
1114 error_setg(errp, "No memory");
1115 return NULL;
1118 dbi = bdrv_dirty_iter_new(bitmap);
1119 buf = g_malloc(s->cluster_size);
1120 limit = bytes_covered_by_bitmap_cluster(s, bitmap);
1121 assert(DIV_ROUND_UP(bm_size, limit) == tb_size);
1123 while ((offset = bdrv_dirty_iter_next(dbi)) >= 0) {
1124 uint64_t cluster = offset / limit;
1125 uint64_t end, write_size;
1126 int64_t off;
1129 * We found the first dirty offset, but want to write out the
1130 * entire cluster of the bitmap that includes that offset,
1131 * including any leading zero bits.
1133 offset = QEMU_ALIGN_DOWN(offset, limit);
1134 end = MIN(bm_size, offset + limit);
1135 write_size = bdrv_dirty_bitmap_serialization_size(bitmap, offset,
1136 end - offset);
1137 assert(write_size <= s->cluster_size);
1139 off = qcow2_alloc_clusters(bs, s->cluster_size);
1140 if (off < 0) {
1141 error_setg_errno(errp, -off,
1142 "Failed to allocate clusters for bitmap '%s'",
1143 bm_name);
1144 goto fail;
1146 tb[cluster] = off;
1148 bdrv_dirty_bitmap_serialize_part(bitmap, buf, offset, end - offset);
1149 if (write_size < s->cluster_size) {
1150 memset(buf + write_size, 0, s->cluster_size - write_size);
1153 ret = qcow2_pre_write_overlap_check(bs, 0, off, s->cluster_size);
1154 if (ret < 0) {
1155 error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1156 goto fail;
1159 ret = bdrv_pwrite(bs->file, off, buf, s->cluster_size);
1160 if (ret < 0) {
1161 error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1162 bm_name);
1163 goto fail;
1166 if (end >= bm_size) {
1167 break;
1170 bdrv_set_dirty_iter(dbi, end);
1173 *bitmap_table_size = tb_size;
1174 g_free(buf);
1175 bdrv_dirty_iter_free(dbi);
1177 return tb;
1179 fail:
1180 clear_bitmap_table(bs, tb, tb_size);
1181 g_free(buf);
1182 bdrv_dirty_iter_free(dbi);
1183 g_free(tb);
1185 return NULL;
1188 /* store_bitmap()
1189 * Store bm->dirty_bitmap to qcow2.
1190 * Set bm->table_offset and bm->table_size accordingly.
1192 static int store_bitmap(BlockDriverState *bs, Qcow2Bitmap *bm, Error **errp)
1194 int ret;
1195 uint64_t *tb;
1196 int64_t tb_offset;
1197 uint32_t tb_size;
1198 BdrvDirtyBitmap *bitmap = bm->dirty_bitmap;
1199 const char *bm_name;
1201 assert(bitmap != NULL);
1203 bm_name = bdrv_dirty_bitmap_name(bitmap);
1205 tb = store_bitmap_data(bs, bitmap, &tb_size, errp);
1206 if (tb == NULL) {
1207 return -EINVAL;
1210 assert(tb_size <= BME_MAX_TABLE_SIZE);
1211 tb_offset = qcow2_alloc_clusters(bs, tb_size * sizeof(tb[0]));
1212 if (tb_offset < 0) {
1213 error_setg_errno(errp, -tb_offset,
1214 "Failed to allocate clusters for bitmap '%s'",
1215 bm_name);
1216 ret = tb_offset;
1217 goto fail;
1220 ret = qcow2_pre_write_overlap_check(bs, 0, tb_offset,
1221 tb_size * sizeof(tb[0]));
1222 if (ret < 0) {
1223 error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1224 goto fail;
1227 bitmap_table_to_be(tb, tb_size);
1228 ret = bdrv_pwrite(bs->file, tb_offset, tb, tb_size * sizeof(tb[0]));
1229 if (ret < 0) {
1230 error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1231 bm_name);
1232 goto fail;
1235 g_free(tb);
1237 bm->table.offset = tb_offset;
1238 bm->table.size = tb_size;
1240 return 0;
1242 fail:
1243 clear_bitmap_table(bs, tb, tb_size);
1245 if (tb_offset > 0) {
1246 qcow2_free_clusters(bs, tb_offset, tb_size * sizeof(tb[0]),
1247 QCOW2_DISCARD_OTHER);
1250 g_free(tb);
1252 return ret;
1255 static Qcow2Bitmap *find_bitmap_by_name(Qcow2BitmapList *bm_list,
1256 const char *name)
1258 Qcow2Bitmap *bm;
1260 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1261 if (strcmp(name, bm->name) == 0) {
1262 return bm;
1266 return NULL;
1269 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
1270 const char *name,
1271 Error **errp)
1273 int ret;
1274 BDRVQcow2State *s = bs->opaque;
1275 Qcow2Bitmap *bm;
1276 Qcow2BitmapList *bm_list;
1278 if (s->nb_bitmaps == 0) {
1279 /* Absence of the bitmap is not an error: see explanation above
1280 * bdrv_remove_persistent_dirty_bitmap() definition. */
1281 return;
1284 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1285 s->bitmap_directory_size, errp);
1286 if (bm_list == NULL) {
1287 return;
1290 bm = find_bitmap_by_name(bm_list, name);
1291 if (bm == NULL) {
1292 goto fail;
1295 QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
1297 ret = update_ext_header_and_dir(bs, bm_list);
1298 if (ret < 0) {
1299 error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1300 goto fail;
1303 free_bitmap_clusters(bs, &bm->table);
1305 fail:
1306 bitmap_free(bm);
1307 bitmap_list_free(bm_list);
1310 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp)
1312 BdrvDirtyBitmap *bitmap;
1313 BDRVQcow2State *s = bs->opaque;
1314 uint32_t new_nb_bitmaps = s->nb_bitmaps;
1315 uint64_t new_dir_size = s->bitmap_directory_size;
1316 int ret;
1317 Qcow2BitmapList *bm_list;
1318 Qcow2Bitmap *bm;
1319 Qcow2BitmapTableList drop_tables;
1320 Qcow2BitmapTable *tb, *tb_next;
1322 if (!bdrv_has_changed_persistent_bitmaps(bs)) {
1323 /* nothing to do */
1324 return;
1327 if (!can_write(bs)) {
1328 error_setg(errp, "No write access");
1329 return;
1332 QSIMPLEQ_INIT(&drop_tables);
1334 if (s->nb_bitmaps == 0) {
1335 bm_list = bitmap_list_new();
1336 } else {
1337 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1338 s->bitmap_directory_size, errp);
1339 if (bm_list == NULL) {
1340 return;
1344 /* check constraints and names */
1345 for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1346 bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1348 const char *name = bdrv_dirty_bitmap_name(bitmap);
1349 uint32_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
1350 Qcow2Bitmap *bm;
1352 if (!bdrv_dirty_bitmap_get_persistance(bitmap) ||
1353 bdrv_dirty_bitmap_readonly(bitmap))
1355 continue;
1358 if (check_constraints_on_bitmap(bs, name, granularity, errp) < 0) {
1359 error_prepend(errp, "Bitmap '%s' doesn't satisfy the constraints: ",
1360 name);
1361 goto fail;
1364 bm = find_bitmap_by_name(bm_list, name);
1365 if (bm == NULL) {
1366 if (++new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
1367 error_setg(errp, "Too many persistent bitmaps");
1368 goto fail;
1371 new_dir_size += calc_dir_entry_size(strlen(name), 0);
1372 if (new_dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
1373 error_setg(errp, "Bitmap directory is too large");
1374 goto fail;
1377 bm = g_new0(Qcow2Bitmap, 1);
1378 bm->name = g_strdup(name);
1379 QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry);
1380 } else {
1381 if (!(bm->flags & BME_FLAG_IN_USE)) {
1382 error_setg(errp, "Bitmap '%s' already exists in the image",
1383 name);
1384 goto fail;
1386 tb = g_memdup(&bm->table, sizeof(bm->table));
1387 bm->table.offset = 0;
1388 bm->table.size = 0;
1389 QSIMPLEQ_INSERT_TAIL(&drop_tables, tb, entry);
1391 bm->flags = bdrv_dirty_bitmap_enabled(bitmap) ? BME_FLAG_AUTO : 0;
1392 bm->granularity_bits = ctz32(bdrv_dirty_bitmap_granularity(bitmap));
1393 bm->dirty_bitmap = bitmap;
1396 /* allocate clusters and store bitmaps */
1397 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1398 if (bm->dirty_bitmap == NULL) {
1399 continue;
1402 ret = store_bitmap(bs, bm, errp);
1403 if (ret < 0) {
1404 goto fail;
1408 ret = update_ext_header_and_dir(bs, bm_list);
1409 if (ret < 0) {
1410 error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1411 goto fail;
1414 /* Bitmap directory was successfully updated, so, old data can be dropped.
1415 * TODO it is better to reuse these clusters */
1416 QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1417 free_bitmap_clusters(bs, tb);
1418 g_free(tb);
1421 bitmap_list_free(bm_list);
1422 return;
1424 fail:
1425 QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1426 if (bm->dirty_bitmap == NULL || bm->table.offset == 0) {
1427 continue;
1430 free_bitmap_clusters(bs, &bm->table);
1433 QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1434 g_free(tb);
1437 bitmap_list_free(bm_list);
1440 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp)
1442 BdrvDirtyBitmap *bitmap;
1443 Error *local_err = NULL;
1445 qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
1446 if (local_err != NULL) {
1447 error_propagate(errp, local_err);
1448 return -EINVAL;
1451 for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1452 bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1454 if (bdrv_dirty_bitmap_get_persistance(bitmap)) {
1455 bdrv_dirty_bitmap_set_readonly(bitmap, true);
1459 return 0;
1462 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState *bs,
1463 const char *name,
1464 uint32_t granularity,
1465 Error **errp)
1467 BDRVQcow2State *s = bs->opaque;
1468 bool found;
1469 Qcow2BitmapList *bm_list;
1471 if (s->qcow_version < 3) {
1472 /* Without autoclear_features, we would always have to assume
1473 * that a program without persistent dirty bitmap support has
1474 * accessed this qcow2 file when opening it, and would thus
1475 * have to drop all dirty bitmaps (defeating their purpose).
1477 error_setg(errp, "Cannot store dirty bitmaps in qcow2 v2 files");
1478 goto fail;
1481 if (check_constraints_on_bitmap(bs, name, granularity, errp) != 0) {
1482 goto fail;
1485 if (s->nb_bitmaps == 0) {
1486 return true;
1489 if (s->nb_bitmaps >= QCOW2_MAX_BITMAPS) {
1490 error_setg(errp,
1491 "Maximum number of persistent bitmaps is already reached");
1492 goto fail;
1495 if (s->bitmap_directory_size + calc_dir_entry_size(strlen(name), 0) >
1496 QCOW2_MAX_BITMAP_DIRECTORY_SIZE)
1498 error_setg(errp, "Not enough space in the bitmap directory");
1499 goto fail;
1502 bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1503 s->bitmap_directory_size, errp);
1504 if (bm_list == NULL) {
1505 goto fail;
1508 found = find_bitmap_by_name(bm_list, name);
1509 bitmap_list_free(bm_list);
1510 if (found) {
1511 error_setg(errp, "Bitmap with the same name is already stored");
1512 goto fail;
1515 return true;
1517 fail:
1518 error_prepend(errp, "Can't make bitmap '%s' persistent in '%s': ",
1519 name, bdrv_get_device_or_node_name(bs));
1520 return false;