xref: /openbmc/qemu/block/qcow2-bitmap.c (revision b15c0f7d)
1 /*
2  * Bitmaps for the QCOW version 2 format
3  *
4  * Copyright (c) 2014-2017 Vladimir Sementsov-Ogievskiy
5  *
6  * This file is derived from qcow2-snapshot.c, original copyright:
7  * Copyright (c) 2004-2006 Fabrice Bellard
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
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.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu/cutils.h"
31 
32 #include "block/block_int.h"
33 #include "qcow2.h"
34 
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. */
38 
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
45 
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
49 
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)
54 
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)
59 
60 typedef struct QEMU_PACKED Qcow2BitmapDirEntry {
61     /* header is 8 byte aligned */
62     uint64_t bitmap_table_offset;
63 
64     uint32_t bitmap_table_size;
65     uint32_t flags;
66 
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;
74 
75 typedef struct Qcow2BitmapTable {
76     uint64_t offset;
77     uint32_t size; /* number of 64bit entries */
78     QSIMPLEQ_ENTRY(Qcow2BitmapTable) entry;
79 } Qcow2BitmapTable;
80 
81 typedef struct Qcow2Bitmap {
82     Qcow2BitmapTable table;
83     uint32_t flags;
84     uint8_t granularity_bits;
85     char *name;
86 
87     BdrvDirtyBitmap *dirty_bitmap;
88 
89     QSIMPLEQ_ENTRY(Qcow2Bitmap) entry;
90 } Qcow2Bitmap;
91 typedef QSIMPLEQ_HEAD(Qcow2BitmapList, Qcow2Bitmap) Qcow2BitmapList;
92 
93 typedef enum BitmapType {
94     BT_DIRTY_TRACKING_BITMAP = 1
95 } BitmapType;
96 
97 static inline bool can_write(BlockDriverState *bs)
98 {
99     return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
100 }
101 
102 static int update_header_sync(BlockDriverState *bs)
103 {
104     int ret;
105 
106     ret = qcow2_update_header(bs);
107     if (ret < 0) {
108         return ret;
109     }
110 
111     return bdrv_flush(bs->file->bs);
112 }
113 
114 static inline void bitmap_table_to_be(uint64_t *bitmap_table, size_t size)
115 {
116     size_t i;
117 
118     for (i = 0; i < size; ++i) {
119         bitmap_table[i] = cpu_to_be64(bitmap_table[i]);
120     }
121 }
122 
123 static int check_table_entry(uint64_t entry, int cluster_size)
124 {
125     uint64_t offset;
126 
127     if (entry & BME_TABLE_ENTRY_RESERVED_MASK) {
128         return -EINVAL;
129     }
130 
131     offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
132     if (offset != 0) {
133         /* if offset specified, bit 0 is reserved */
134         if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
135             return -EINVAL;
136         }
137 
138         if (offset % cluster_size != 0) {
139             return -EINVAL;
140         }
141     }
142 
143     return 0;
144 }
145 
146 static int check_constraints_on_bitmap(BlockDriverState *bs,
147                                        const char *name,
148                                        uint32_t granularity,
149                                        Error **errp)
150 {
151     BDRVQcow2State *s = bs->opaque;
152     int granularity_bits = ctz32(granularity);
153     int64_t len = bdrv_getlength(bs);
154 
155     assert(granularity > 0);
156     assert((granularity & (granularity - 1)) == 0);
157 
158     if (len < 0) {
159         error_setg_errno(errp, -len, "Failed to get size of '%s'",
160                          bdrv_get_device_or_node_name(bs));
161         return len;
162     }
163 
164     if (granularity_bits > BME_MAX_GRANULARITY_BITS) {
165         error_setg(errp, "Granularity exceeds maximum (%llu bytes)",
166                    1ULL << BME_MAX_GRANULARITY_BITS);
167         return -EINVAL;
168     }
169     if (granularity_bits < BME_MIN_GRANULARITY_BITS) {
170         error_setg(errp, "Granularity is under minimum (%llu bytes)",
171                    1ULL << BME_MIN_GRANULARITY_BITS);
172         return -EINVAL;
173     }
174 
175     if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
176         (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
177                granularity_bits))
178     {
179         error_setg(errp, "Too much space will be occupied by the bitmap. "
180                    "Use larger granularity");
181         return -EINVAL;
182     }
183 
184     if (strlen(name) > BME_MAX_NAME_SIZE) {
185         error_setg(errp, "Name length exceeds maximum (%u characters)",
186                    BME_MAX_NAME_SIZE);
187         return -EINVAL;
188     }
189 
190     return 0;
191 }
192 
193 static void clear_bitmap_table(BlockDriverState *bs, uint64_t *bitmap_table,
194                                uint32_t bitmap_table_size)
195 {
196     BDRVQcow2State *s = bs->opaque;
197     int i;
198 
199     for (i = 0; i < bitmap_table_size; ++i) {
200         uint64_t addr = bitmap_table[i] & BME_TABLE_ENTRY_OFFSET_MASK;
201         if (!addr) {
202             continue;
203         }
204 
205         qcow2_free_clusters(bs, addr, s->cluster_size, QCOW2_DISCARD_ALWAYS);
206         bitmap_table[i] = 0;
207     }
208 }
209 
210 static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb,
211                              uint64_t **bitmap_table)
212 {
213     int ret;
214     BDRVQcow2State *s = bs->opaque;
215     uint32_t i;
216     uint64_t *table;
217 
218     assert(tb->size != 0);
219     table = g_try_new(uint64_t, tb->size);
220     if (table == NULL) {
221         return -ENOMEM;
222     }
223 
224     assert(tb->size <= BME_MAX_TABLE_SIZE);
225     ret = bdrv_pread(bs->file, tb->offset,
226                      table, tb->size * sizeof(uint64_t));
227     if (ret < 0) {
228         goto fail;
229     }
230 
231     for (i = 0; i < tb->size; ++i) {
232         table[i] = be64_to_cpu(table[i]);
233         ret = check_table_entry(table[i], s->cluster_size);
234         if (ret < 0) {
235             goto fail;
236         }
237     }
238 
239     *bitmap_table = table;
240     return 0;
241 
242 fail:
243     g_free(table);
244 
245     return ret;
246 }
247 
248 static int free_bitmap_clusters(BlockDriverState *bs, Qcow2BitmapTable *tb)
249 {
250     int ret;
251     uint64_t *bitmap_table;
252 
253     ret = bitmap_table_load(bs, tb, &bitmap_table);
254     if (ret < 0) {
255         return ret;
256     }
257 
258     clear_bitmap_table(bs, bitmap_table, tb->size);
259     qcow2_free_clusters(bs, tb->offset, tb->size * sizeof(uint64_t),
260                         QCOW2_DISCARD_OTHER);
261     g_free(bitmap_table);
262 
263     tb->offset = 0;
264     tb->size = 0;
265 
266     return 0;
267 }
268 
269 /* Return the disk size covered by a single qcow2 cluster of bitmap data. */
270 static uint64_t bytes_covered_by_bitmap_cluster(const BDRVQcow2State *s,
271                                                 const BdrvDirtyBitmap *bitmap)
272 {
273     uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
274     uint64_t limit = granularity * (s->cluster_size << 3);
275 
276     assert(QEMU_IS_ALIGNED(limit,
277                            bdrv_dirty_bitmap_serialization_align(bitmap)));
278     return limit;
279 }
280 
281 /* load_bitmap_data
282  * @bitmap_table entries must satisfy specification constraints.
283  * @bitmap must be cleared */
284 static int load_bitmap_data(BlockDriverState *bs,
285                             const uint64_t *bitmap_table,
286                             uint32_t bitmap_table_size,
287                             BdrvDirtyBitmap *bitmap)
288 {
289     int ret = 0;
290     BDRVQcow2State *s = bs->opaque;
291     uint64_t offset, limit;
292     uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
293     uint8_t *buf = NULL;
294     uint64_t i, tab_size =
295             size_to_clusters(s,
296                 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
297 
298     if (tab_size != bitmap_table_size || tab_size > BME_MAX_TABLE_SIZE) {
299         return -EINVAL;
300     }
301 
302     buf = g_malloc(s->cluster_size);
303     limit = bytes_covered_by_bitmap_cluster(s, bitmap);
304     for (i = 0, offset = 0; i < tab_size; ++i, offset += limit) {
305         uint64_t count = MIN(bm_size - offset, limit);
306         uint64_t entry = bitmap_table[i];
307         uint64_t data_offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
308 
309         assert(check_table_entry(entry, s->cluster_size) == 0);
310 
311         if (data_offset == 0) {
312             if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) {
313                 bdrv_dirty_bitmap_deserialize_ones(bitmap, offset, count,
314                                                    false);
315             } else {
316                 /* No need to deserialize zeros because the dirty bitmap is
317                  * already cleared */
318             }
319         } else {
320             ret = bdrv_pread(bs->file, data_offset, buf, s->cluster_size);
321             if (ret < 0) {
322                 goto finish;
323             }
324             bdrv_dirty_bitmap_deserialize_part(bitmap, buf, offset, count,
325                                                false);
326         }
327     }
328     ret = 0;
329 
330     bdrv_dirty_bitmap_deserialize_finish(bitmap);
331 
332 finish:
333     g_free(buf);
334 
335     return ret;
336 }
337 
338 static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs,
339                                     Qcow2Bitmap *bm, Error **errp)
340 {
341     int ret;
342     uint64_t *bitmap_table = NULL;
343     uint32_t granularity;
344     BdrvDirtyBitmap *bitmap = NULL;
345 
346     granularity = 1U << bm->granularity_bits;
347     bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
348     if (bitmap == NULL) {
349         goto fail;
350     }
351 
352     if (bm->flags & BME_FLAG_IN_USE) {
353         /* Data is unusable, skip loading it */
354         return bitmap;
355     }
356 
357     ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
358     if (ret < 0) {
359         error_setg_errno(errp, -ret,
360                          "Could not read bitmap_table table from image for "
361                          "bitmap '%s'", bm->name);
362         goto fail;
363     }
364 
365     ret = load_bitmap_data(bs, bitmap_table, bm->table.size, bitmap);
366     if (ret < 0) {
367         error_setg_errno(errp, -ret, "Could not read bitmap '%s' from image",
368                          bm->name);
369         goto fail;
370     }
371 
372     g_free(bitmap_table);
373     return bitmap;
374 
375 fail:
376     g_free(bitmap_table);
377     if (bitmap != NULL) {
378         bdrv_release_dirty_bitmap(bs, bitmap);
379     }
380 
381     return NULL;
382 }
383 
384 /*
385  * Bitmap List
386  */
387 
388 /*
389  * Bitmap List private functions
390  * Only Bitmap List knows about bitmap directory structure in Qcow2.
391  */
392 
393 static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry *entry)
394 {
395     entry->bitmap_table_offset = be64_to_cpu(entry->bitmap_table_offset);
396     entry->bitmap_table_size = be32_to_cpu(entry->bitmap_table_size);
397     entry->flags = be32_to_cpu(entry->flags);
398     entry->name_size = be16_to_cpu(entry->name_size);
399     entry->extra_data_size = be32_to_cpu(entry->extra_data_size);
400 }
401 
402 static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry *entry)
403 {
404     entry->bitmap_table_offset = cpu_to_be64(entry->bitmap_table_offset);
405     entry->bitmap_table_size = cpu_to_be32(entry->bitmap_table_size);
406     entry->flags = cpu_to_be32(entry->flags);
407     entry->name_size = cpu_to_be16(entry->name_size);
408     entry->extra_data_size = cpu_to_be32(entry->extra_data_size);
409 }
410 
411 static inline int calc_dir_entry_size(size_t name_size, size_t extra_data_size)
412 {
413     int size = sizeof(Qcow2BitmapDirEntry) + name_size + extra_data_size;
414     return ROUND_UP(size, 8);
415 }
416 
417 static inline int dir_entry_size(Qcow2BitmapDirEntry *entry)
418 {
419     return calc_dir_entry_size(entry->name_size, entry->extra_data_size);
420 }
421 
422 static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry *entry)
423 {
424     return (const char *)(entry + 1) + entry->extra_data_size;
425 }
426 
427 static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry *entry)
428 {
429     const char *name_field = dir_entry_name_field(entry);
430     return g_strndup(name_field, entry->name_size);
431 }
432 
433 static inline Qcow2BitmapDirEntry *next_dir_entry(Qcow2BitmapDirEntry *entry)
434 {
435     return (Qcow2BitmapDirEntry *)((uint8_t *)entry + dir_entry_size(entry));
436 }
437 
438 static int check_dir_entry(BlockDriverState *bs, Qcow2BitmapDirEntry *entry)
439 {
440     BDRVQcow2State *s = bs->opaque;
441     uint64_t phys_bitmap_bytes;
442     int64_t len;
443 
444     bool fail = (entry->bitmap_table_size == 0) ||
445                 (entry->bitmap_table_offset == 0) ||
446                 (entry->bitmap_table_offset % s->cluster_size) ||
447                 (entry->bitmap_table_size > BME_MAX_TABLE_SIZE) ||
448                 (entry->granularity_bits > BME_MAX_GRANULARITY_BITS) ||
449                 (entry->granularity_bits < BME_MIN_GRANULARITY_BITS) ||
450                 (entry->flags & BME_RESERVED_FLAGS) ||
451                 (entry->name_size > BME_MAX_NAME_SIZE) ||
452                 (entry->type != BT_DIRTY_TRACKING_BITMAP);
453 
454     if (fail) {
455         return -EINVAL;
456     }
457 
458     phys_bitmap_bytes = (uint64_t)entry->bitmap_table_size * s->cluster_size;
459     len = bdrv_getlength(bs);
460 
461     if (len < 0) {
462         return len;
463     }
464 
465     if (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) {
466         return -EINVAL;
467     }
468 
469     if (!(entry->flags & BME_FLAG_IN_USE) &&
470         (len > ((phys_bitmap_bytes * 8) << entry->granularity_bits)))
471     {
472         /*
473          * We've loaded a valid bitmap (IN_USE not set) or we are going to
474          * store a valid bitmap, but the allocated bitmap table size is not
475          * enough to store this bitmap.
476          *
477          * Note, that it's OK to have an invalid bitmap with invalid size due
478          * to a bitmap that was not correctly saved after image resize.
479          */
480         return -EINVAL;
481     }
482 
483     return 0;
484 }
485 
486 static inline void bitmap_directory_to_be(uint8_t *dir, size_t size)
487 {
488     uint8_t *end = dir + size;
489     while (dir < end) {
490         Qcow2BitmapDirEntry *e = (Qcow2BitmapDirEntry *)dir;
491         dir += dir_entry_size(e);
492 
493         bitmap_dir_entry_to_be(e);
494     }
495 }
496 
497 /*
498  * Bitmap List public functions
499  */
500 
501 static void bitmap_free(Qcow2Bitmap *bm)
502 {
503     if (bm == NULL) {
504         return;
505     }
506 
507     g_free(bm->name);
508     g_free(bm);
509 }
510 
511 static void bitmap_list_free(Qcow2BitmapList *bm_list)
512 {
513     Qcow2Bitmap *bm;
514 
515     if (bm_list == NULL) {
516         return;
517     }
518 
519     while ((bm = QSIMPLEQ_FIRST(bm_list)) != NULL) {
520         QSIMPLEQ_REMOVE_HEAD(bm_list, entry);
521         bitmap_free(bm);
522     }
523 
524     g_free(bm_list);
525 }
526 
527 static Qcow2BitmapList *bitmap_list_new(void)
528 {
529     Qcow2BitmapList *bm_list = g_new(Qcow2BitmapList, 1);
530     QSIMPLEQ_INIT(bm_list);
531 
532     return bm_list;
533 }
534 
535 static uint32_t bitmap_list_count(Qcow2BitmapList *bm_list)
536 {
537     Qcow2Bitmap *bm;
538     uint32_t nb_bitmaps = 0;
539 
540     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
541         nb_bitmaps++;
542     }
543 
544     return nb_bitmaps;
545 }
546 
547 /* bitmap_list_load
548  * Get bitmap list from qcow2 image. Actually reads bitmap directory,
549  * checks it and convert to bitmap list.
550  */
551 static Qcow2BitmapList *bitmap_list_load(BlockDriverState *bs, uint64_t offset,
552                                          uint64_t size, Error **errp)
553 {
554     int ret;
555     BDRVQcow2State *s = bs->opaque;
556     uint8_t *dir, *dir_end;
557     Qcow2BitmapDirEntry *e;
558     uint32_t nb_dir_entries = 0;
559     Qcow2BitmapList *bm_list = NULL;
560 
561     if (size == 0) {
562         error_setg(errp, "Requested bitmap directory size is zero");
563         return NULL;
564     }
565 
566     if (size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
567         error_setg(errp, "Requested bitmap directory size is too big");
568         return NULL;
569     }
570 
571     dir = g_try_malloc(size);
572     if (dir == NULL) {
573         error_setg(errp, "Failed to allocate space for bitmap directory");
574         return NULL;
575     }
576     dir_end = dir + size;
577 
578     ret = bdrv_pread(bs->file, offset, dir, size);
579     if (ret < 0) {
580         error_setg_errno(errp, -ret, "Failed to read bitmap directory");
581         goto fail;
582     }
583 
584     bm_list = bitmap_list_new();
585     for (e = (Qcow2BitmapDirEntry *)dir;
586          e < (Qcow2BitmapDirEntry *)dir_end;
587          e = next_dir_entry(e))
588     {
589         Qcow2Bitmap *bm;
590 
591         if ((uint8_t *)(e + 1) > dir_end) {
592             goto broken_dir;
593         }
594 
595         if (++nb_dir_entries > s->nb_bitmaps) {
596             error_setg(errp, "More bitmaps found than specified in header"
597                        " extension");
598             goto fail;
599         }
600         bitmap_dir_entry_to_cpu(e);
601 
602         if ((uint8_t *)next_dir_entry(e) > dir_end) {
603             goto broken_dir;
604         }
605 
606         if (e->extra_data_size != 0) {
607             error_setg(errp, "Bitmap extra data is not supported");
608             goto fail;
609         }
610 
611         ret = check_dir_entry(bs, e);
612         if (ret < 0) {
613             error_setg(errp, "Bitmap '%.*s' doesn't satisfy the constraints",
614                        e->name_size, dir_entry_name_field(e));
615             goto fail;
616         }
617 
618         bm = g_new0(Qcow2Bitmap, 1);
619         bm->table.offset = e->bitmap_table_offset;
620         bm->table.size = e->bitmap_table_size;
621         bm->flags = e->flags;
622         bm->granularity_bits = e->granularity_bits;
623         bm->name = dir_entry_copy_name(e);
624         QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry);
625     }
626 
627     if (nb_dir_entries != s->nb_bitmaps) {
628         error_setg(errp, "Less bitmaps found than specified in header"
629                          " extension");
630         goto fail;
631     }
632 
633     if ((uint8_t *)e != dir_end) {
634         goto broken_dir;
635     }
636 
637     g_free(dir);
638     return bm_list;
639 
640 broken_dir:
641     ret = -EINVAL;
642     error_setg(errp, "Broken bitmap directory");
643 
644 fail:
645     g_free(dir);
646     bitmap_list_free(bm_list);
647 
648     return NULL;
649 }
650 
651 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
652                                   void **refcount_table,
653                                   int64_t *refcount_table_size)
654 {
655     int ret;
656     BDRVQcow2State *s = bs->opaque;
657     Qcow2BitmapList *bm_list;
658     Qcow2Bitmap *bm;
659 
660     if (s->nb_bitmaps == 0) {
661         return 0;
662     }
663 
664     ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
665                                    s->bitmap_directory_offset,
666                                    s->bitmap_directory_size);
667     if (ret < 0) {
668         return ret;
669     }
670 
671     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
672                                s->bitmap_directory_size, NULL);
673     if (bm_list == NULL) {
674         res->corruptions++;
675         return -EINVAL;
676     }
677 
678     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
679         uint64_t *bitmap_table = NULL;
680         int i;
681 
682         ret = qcow2_inc_refcounts_imrt(bs, res,
683                                        refcount_table, refcount_table_size,
684                                        bm->table.offset,
685                                        bm->table.size * sizeof(uint64_t));
686         if (ret < 0) {
687             goto out;
688         }
689 
690         ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
691         if (ret < 0) {
692             res->corruptions++;
693             goto out;
694         }
695 
696         for (i = 0; i < bm->table.size; ++i) {
697             uint64_t entry = bitmap_table[i];
698             uint64_t offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
699 
700             if (check_table_entry(entry, s->cluster_size) < 0) {
701                 res->corruptions++;
702                 continue;
703             }
704 
705             if (offset == 0) {
706                 continue;
707             }
708 
709             ret = qcow2_inc_refcounts_imrt(bs, res,
710                                            refcount_table, refcount_table_size,
711                                            offset, s->cluster_size);
712             if (ret < 0) {
713                 g_free(bitmap_table);
714                 goto out;
715             }
716         }
717 
718         g_free(bitmap_table);
719     }
720 
721 out:
722     bitmap_list_free(bm_list);
723 
724     return ret;
725 }
726 
727 /* bitmap_list_store
728  * Store bitmap list to qcow2 image as a bitmap directory.
729  * Everything is checked.
730  */
731 static int bitmap_list_store(BlockDriverState *bs, Qcow2BitmapList *bm_list,
732                              uint64_t *offset, uint64_t *size, bool in_place)
733 {
734     int ret;
735     uint8_t *dir;
736     int64_t dir_offset = 0;
737     uint64_t dir_size = 0;
738     Qcow2Bitmap *bm;
739     Qcow2BitmapDirEntry *e;
740 
741     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
742         dir_size += calc_dir_entry_size(strlen(bm->name), 0);
743     }
744 
745     if (dir_size == 0 || dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
746         return -EINVAL;
747     }
748 
749     if (in_place) {
750         if (*size != dir_size || *offset == 0) {
751             return -EINVAL;
752         }
753 
754         dir_offset = *offset;
755     }
756 
757     dir = g_try_malloc(dir_size);
758     if (dir == NULL) {
759         return -ENOMEM;
760     }
761 
762     e = (Qcow2BitmapDirEntry *)dir;
763     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
764         e->bitmap_table_offset = bm->table.offset;
765         e->bitmap_table_size = bm->table.size;
766         e->flags = bm->flags;
767         e->type = BT_DIRTY_TRACKING_BITMAP;
768         e->granularity_bits = bm->granularity_bits;
769         e->name_size = strlen(bm->name);
770         e->extra_data_size = 0;
771         memcpy(e + 1, bm->name, e->name_size);
772 
773         if (check_dir_entry(bs, e) < 0) {
774             ret = -EINVAL;
775             goto fail;
776         }
777 
778         e = next_dir_entry(e);
779     }
780 
781     bitmap_directory_to_be(dir, dir_size);
782 
783     if (!in_place) {
784         dir_offset = qcow2_alloc_clusters(bs, dir_size);
785         if (dir_offset < 0) {
786             ret = dir_offset;
787             goto fail;
788         }
789     }
790 
791     /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
792      * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
793      * directory in-place (actually, turn-off the extension), which is checked
794      * in qcow2_check_metadata_overlap() */
795     ret = qcow2_pre_write_overlap_check(
796             bs, in_place ? QCOW2_OL_BITMAP_DIRECTORY : 0, dir_offset, dir_size,
797             false);
798     if (ret < 0) {
799         goto fail;
800     }
801 
802     ret = bdrv_pwrite(bs->file, dir_offset, dir, dir_size);
803     if (ret < 0) {
804         goto fail;
805     }
806 
807     g_free(dir);
808 
809     if (!in_place) {
810         *size = dir_size;
811         *offset = dir_offset;
812     }
813 
814     return 0;
815 
816 fail:
817     g_free(dir);
818 
819     if (!in_place && dir_offset > 0) {
820         qcow2_free_clusters(bs, dir_offset, dir_size, QCOW2_DISCARD_OTHER);
821     }
822 
823     return ret;
824 }
825 
826 /*
827  * Bitmap List end
828  */
829 
830 static int update_ext_header_and_dir_in_place(BlockDriverState *bs,
831                                               Qcow2BitmapList *bm_list)
832 {
833     BDRVQcow2State *s = bs->opaque;
834     int ret;
835 
836     if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS) ||
837         bm_list == NULL || QSIMPLEQ_EMPTY(bm_list) ||
838         bitmap_list_count(bm_list) != s->nb_bitmaps)
839     {
840         return -EINVAL;
841     }
842 
843     s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
844     ret = update_header_sync(bs);
845     if (ret < 0) {
846         /* Two variants are possible here:
847          * 1. Autoclear flag is dropped, all bitmaps will be lost.
848          * 2. Autoclear flag is not dropped, old state is left.
849          */
850         return ret;
851     }
852 
853     /* autoclear bit is not set, so we can safely update bitmap directory */
854 
855     ret = bitmap_list_store(bs, bm_list, &s->bitmap_directory_offset,
856                             &s->bitmap_directory_size, true);
857     if (ret < 0) {
858         /* autoclear bit is cleared, so all leaked clusters would be removed on
859          * qemu-img check */
860         return ret;
861     }
862 
863     ret = update_header_sync(bs);
864     if (ret < 0) {
865         /* autoclear bit is cleared, so all leaked clusters would be removed on
866          * qemu-img check */
867         return ret;
868     }
869 
870     s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
871     return update_header_sync(bs);
872     /* If final update_header_sync() fails, two variants are possible:
873      * 1. Autoclear flag is not set, all bitmaps will be lost.
874      * 2. Autoclear flag is set, header and directory are successfully updated.
875      */
876 }
877 
878 static int update_ext_header_and_dir(BlockDriverState *bs,
879                                      Qcow2BitmapList *bm_list)
880 {
881     BDRVQcow2State *s = bs->opaque;
882     int ret;
883     uint64_t new_offset = 0;
884     uint64_t new_size = 0;
885     uint32_t new_nb_bitmaps = 0;
886     uint64_t old_offset = s->bitmap_directory_offset;
887     uint64_t old_size = s->bitmap_directory_size;
888     uint32_t old_nb_bitmaps = s->nb_bitmaps;
889     uint64_t old_autocl = s->autoclear_features;
890 
891     if (bm_list != NULL && !QSIMPLEQ_EMPTY(bm_list)) {
892         new_nb_bitmaps = bitmap_list_count(bm_list);
893 
894         if (new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
895             return -EINVAL;
896         }
897 
898         ret = bitmap_list_store(bs, bm_list, &new_offset, &new_size, false);
899         if (ret < 0) {
900             return ret;
901         }
902 
903         ret = qcow2_flush_caches(bs);
904         if (ret < 0) {
905             goto fail;
906         }
907 
908         s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS;
909     } else {
910         s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS;
911     }
912 
913     s->bitmap_directory_offset = new_offset;
914     s->bitmap_directory_size = new_size;
915     s->nb_bitmaps = new_nb_bitmaps;
916 
917     ret = update_header_sync(bs);
918     if (ret < 0) {
919         goto fail;
920     }
921 
922     if (old_size > 0) {
923         qcow2_free_clusters(bs, old_offset, old_size, QCOW2_DISCARD_OTHER);
924     }
925 
926     return 0;
927 
928 fail:
929     if (new_offset > 0) {
930         qcow2_free_clusters(bs, new_offset, new_size, QCOW2_DISCARD_OTHER);
931     }
932 
933     s->bitmap_directory_offset = old_offset;
934     s->bitmap_directory_size = old_size;
935     s->nb_bitmaps = old_nb_bitmaps;
936     s->autoclear_features = old_autocl;
937 
938     return ret;
939 }
940 
941 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
942 static void release_dirty_bitmap_helper(gpointer bitmap,
943                                         gpointer bs)
944 {
945     bdrv_release_dirty_bitmap(bs, bitmap);
946 }
947 
948 /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */
949 static void set_readonly_helper(gpointer bitmap, gpointer value)
950 {
951     bdrv_dirty_bitmap_set_readonly(bitmap, (bool)value);
952 }
953 
954 /* qcow2_load_dirty_bitmaps()
955  * Return value is a hint for caller: true means that the Qcow2 header was
956  * updated. (false doesn't mean that the header should be updated by the
957  * caller, it just means that updating was not needed or the image cannot be
958  * written to).
959  * On failure the function returns false.
960  */
961 bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
962 {
963     BDRVQcow2State *s = bs->opaque;
964     Qcow2BitmapList *bm_list;
965     Qcow2Bitmap *bm;
966     GSList *created_dirty_bitmaps = NULL;
967     bool header_updated = false;
968     bool needs_update = false;
969 
970     if (s->nb_bitmaps == 0) {
971         /* No bitmaps - nothing to do */
972         return false;
973     }
974 
975     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
976                                s->bitmap_directory_size, errp);
977     if (bm_list == NULL) {
978         return false;
979     }
980 
981     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
982         BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp);
983         if (bitmap == NULL) {
984             goto fail;
985         }
986 
987         bdrv_dirty_bitmap_set_persistence(bitmap, true);
988         if (bm->flags & BME_FLAG_IN_USE) {
989             bdrv_dirty_bitmap_set_inconsistent(bitmap);
990         } else {
991             /* NB: updated flags only get written if can_write(bs) is true. */
992             bm->flags |= BME_FLAG_IN_USE;
993             needs_update = true;
994         }
995         if (!(bm->flags & BME_FLAG_AUTO)) {
996             bdrv_disable_dirty_bitmap(bitmap);
997         }
998         created_dirty_bitmaps =
999             g_slist_append(created_dirty_bitmaps, bitmap);
1000     }
1001 
1002     if (needs_update && can_write(bs)) {
1003         /* in_use flags must be updated */
1004         int ret = update_ext_header_and_dir_in_place(bs, bm_list);
1005         if (ret < 0) {
1006             error_setg_errno(errp, -ret, "Can't update bitmap directory");
1007             goto fail;
1008         }
1009         header_updated = true;
1010     }
1011 
1012     if (!can_write(bs)) {
1013         g_slist_foreach(created_dirty_bitmaps, set_readonly_helper,
1014                         (gpointer)true);
1015     }
1016 
1017     g_slist_free(created_dirty_bitmaps);
1018     bitmap_list_free(bm_list);
1019 
1020     return header_updated;
1021 
1022 fail:
1023     g_slist_foreach(created_dirty_bitmaps, release_dirty_bitmap_helper, bs);
1024     g_slist_free(created_dirty_bitmaps);
1025     bitmap_list_free(bm_list);
1026 
1027     return false;
1028 }
1029 
1030 
1031 static Qcow2BitmapInfoFlagsList *get_bitmap_info_flags(uint32_t flags)
1032 {
1033     Qcow2BitmapInfoFlagsList *list = NULL;
1034     Qcow2BitmapInfoFlagsList **plist = &list;
1035     int i;
1036 
1037     static const struct {
1038         int bme;  /* Bitmap directory entry flags */
1039         int info; /* The flags to report to the user */
1040     } map[] = {
1041         { BME_FLAG_IN_USE, QCOW2_BITMAP_INFO_FLAGS_IN_USE },
1042         { BME_FLAG_AUTO,   QCOW2_BITMAP_INFO_FLAGS_AUTO },
1043     };
1044 
1045     int map_size = ARRAY_SIZE(map);
1046 
1047     for (i = 0; i < map_size; ++i) {
1048         if (flags & map[i].bme) {
1049             Qcow2BitmapInfoFlagsList *entry =
1050                 g_new0(Qcow2BitmapInfoFlagsList, 1);
1051             entry->value = map[i].info;
1052             *plist = entry;
1053             plist = &entry->next;
1054             flags &= ~map[i].bme;
1055         }
1056     }
1057     /* Check if the BME_* mapping above is complete */
1058     assert(!flags);
1059 
1060     return list;
1061 }
1062 
1063 /*
1064  * qcow2_get_bitmap_info_list()
1065  * Returns a list of QCOW2 bitmap details.
1066  * In case of no bitmaps, the function returns NULL and
1067  * the @errp parameter is not set.
1068  * When bitmap information can not be obtained, the function returns
1069  * NULL and the @errp parameter is set.
1070  */
1071 Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
1072                                                 Error **errp)
1073 {
1074     BDRVQcow2State *s = bs->opaque;
1075     Qcow2BitmapList *bm_list;
1076     Qcow2Bitmap *bm;
1077     Qcow2BitmapInfoList *list = NULL;
1078     Qcow2BitmapInfoList **plist = &list;
1079 
1080     if (s->nb_bitmaps == 0) {
1081         return NULL;
1082     }
1083 
1084     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1085                                s->bitmap_directory_size, errp);
1086     if (bm_list == NULL) {
1087         return NULL;
1088     }
1089 
1090     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1091         Qcow2BitmapInfo *info = g_new0(Qcow2BitmapInfo, 1);
1092         Qcow2BitmapInfoList *obj = g_new0(Qcow2BitmapInfoList, 1);
1093         info->granularity = 1U << bm->granularity_bits;
1094         info->name = g_strdup(bm->name);
1095         info->flags = get_bitmap_info_flags(bm->flags & ~BME_RESERVED_FLAGS);
1096         obj->value = info;
1097         *plist = obj;
1098         plist = &obj->next;
1099     }
1100 
1101     bitmap_list_free(bm_list);
1102 
1103     return list;
1104 }
1105 
1106 int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
1107                                  Error **errp)
1108 {
1109     BDRVQcow2State *s = bs->opaque;
1110     Qcow2BitmapList *bm_list;
1111     Qcow2Bitmap *bm;
1112     GSList *ro_dirty_bitmaps = NULL;
1113     int ret = 0;
1114 
1115     if (header_updated != NULL) {
1116         *header_updated = false;
1117     }
1118 
1119     if (s->nb_bitmaps == 0) {
1120         /* No bitmaps - nothing to do */
1121         return 0;
1122     }
1123 
1124     if (!can_write(bs)) {
1125         error_setg(errp, "Can't write to the image on reopening bitmaps rw");
1126         return -EINVAL;
1127     }
1128 
1129     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1130                                s->bitmap_directory_size, errp);
1131     if (bm_list == NULL) {
1132         return -EINVAL;
1133     }
1134 
1135     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1136         BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
1137         if (bitmap == NULL) {
1138             continue;
1139         }
1140 
1141         if (!bdrv_dirty_bitmap_readonly(bitmap)) {
1142             error_setg(errp, "Bitmap %s was loaded prior to rw-reopen, but was "
1143                        "not marked as readonly. This is a bug, something went "
1144                        "wrong. All of the bitmaps may be corrupted", bm->name);
1145             ret = -EINVAL;
1146             goto out;
1147         }
1148 
1149         bm->flags |= BME_FLAG_IN_USE;
1150         ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap);
1151     }
1152 
1153     if (ro_dirty_bitmaps != NULL) {
1154         /* in_use flags must be updated */
1155         ret = update_ext_header_and_dir_in_place(bs, bm_list);
1156         if (ret < 0) {
1157             error_setg_errno(errp, -ret, "Can't update bitmap directory");
1158             goto out;
1159         }
1160         if (header_updated != NULL) {
1161             *header_updated = true;
1162         }
1163         g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false);
1164     }
1165 
1166 out:
1167     g_slist_free(ro_dirty_bitmaps);
1168     bitmap_list_free(bm_list);
1169 
1170     return ret;
1171 }
1172 
1173 int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp)
1174 {
1175     return qcow2_reopen_bitmaps_rw_hint(bs, NULL, errp);
1176 }
1177 
1178 /* Checks to see if it's safe to resize bitmaps */
1179 int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp)
1180 {
1181     BDRVQcow2State *s = bs->opaque;
1182     Qcow2BitmapList *bm_list;
1183     Qcow2Bitmap *bm;
1184     int ret = 0;
1185 
1186     if (s->nb_bitmaps == 0) {
1187         return 0;
1188     }
1189 
1190     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1191                                s->bitmap_directory_size, errp);
1192     if (bm_list == NULL) {
1193         return -EINVAL;
1194     }
1195 
1196     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1197         BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
1198         if (bitmap == NULL) {
1199             /*
1200              * We rely on all bitmaps being in-memory to be able to resize them,
1201              * Otherwise, we'd need to resize them on disk explicitly
1202              */
1203             error_setg(errp, "Cannot resize qcow2 with persistent bitmaps that "
1204                        "were not loaded into memory");
1205             ret = -ENOTSUP;
1206             goto out;
1207         }
1208 
1209         /*
1210          * The checks against readonly and busy are redundant, but certainly
1211          * do no harm. checks against inconsistent are crucial:
1212          */
1213         if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
1214             ret = -ENOTSUP;
1215             goto out;
1216         }
1217     }
1218 
1219 out:
1220     bitmap_list_free(bm_list);
1221     return ret;
1222 }
1223 
1224 /* store_bitmap_data()
1225  * Store bitmap to image, filling bitmap table accordingly.
1226  */
1227 static uint64_t *store_bitmap_data(BlockDriverState *bs,
1228                                    BdrvDirtyBitmap *bitmap,
1229                                    uint32_t *bitmap_table_size, Error **errp)
1230 {
1231     int ret;
1232     BDRVQcow2State *s = bs->opaque;
1233     int64_t offset;
1234     uint64_t limit;
1235     uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
1236     const char *bm_name = bdrv_dirty_bitmap_name(bitmap);
1237     uint8_t *buf = NULL;
1238     BdrvDirtyBitmapIter *dbi;
1239     uint64_t *tb;
1240     uint64_t tb_size =
1241             size_to_clusters(s,
1242                 bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size));
1243 
1244     if (tb_size > BME_MAX_TABLE_SIZE ||
1245         tb_size * s->cluster_size > BME_MAX_PHYS_SIZE)
1246     {
1247         error_setg(errp, "Bitmap '%s' is too big", bm_name);
1248         return NULL;
1249     }
1250 
1251     tb = g_try_new0(uint64_t, tb_size);
1252     if (tb == NULL) {
1253         error_setg(errp, "No memory");
1254         return NULL;
1255     }
1256 
1257     dbi = bdrv_dirty_iter_new(bitmap);
1258     buf = g_malloc(s->cluster_size);
1259     limit = bytes_covered_by_bitmap_cluster(s, bitmap);
1260     assert(DIV_ROUND_UP(bm_size, limit) == tb_size);
1261 
1262     while ((offset = bdrv_dirty_iter_next(dbi)) >= 0) {
1263         uint64_t cluster = offset / limit;
1264         uint64_t end, write_size;
1265         int64_t off;
1266 
1267         /*
1268          * We found the first dirty offset, but want to write out the
1269          * entire cluster of the bitmap that includes that offset,
1270          * including any leading zero bits.
1271          */
1272         offset = QEMU_ALIGN_DOWN(offset, limit);
1273         end = MIN(bm_size, offset + limit);
1274         write_size = bdrv_dirty_bitmap_serialization_size(bitmap, offset,
1275                                                           end - offset);
1276         assert(write_size <= s->cluster_size);
1277 
1278         off = qcow2_alloc_clusters(bs, s->cluster_size);
1279         if (off < 0) {
1280             error_setg_errno(errp, -off,
1281                              "Failed to allocate clusters for bitmap '%s'",
1282                              bm_name);
1283             goto fail;
1284         }
1285         tb[cluster] = off;
1286 
1287         bdrv_dirty_bitmap_serialize_part(bitmap, buf, offset, end - offset);
1288         if (write_size < s->cluster_size) {
1289             memset(buf + write_size, 0, s->cluster_size - write_size);
1290         }
1291 
1292         ret = qcow2_pre_write_overlap_check(bs, 0, off, s->cluster_size, false);
1293         if (ret < 0) {
1294             error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1295             goto fail;
1296         }
1297 
1298         ret = bdrv_pwrite(bs->file, off, buf, s->cluster_size);
1299         if (ret < 0) {
1300             error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1301                              bm_name);
1302             goto fail;
1303         }
1304 
1305         if (end >= bm_size) {
1306             break;
1307         }
1308 
1309         bdrv_set_dirty_iter(dbi, end);
1310     }
1311 
1312     *bitmap_table_size = tb_size;
1313     g_free(buf);
1314     bdrv_dirty_iter_free(dbi);
1315 
1316     return tb;
1317 
1318 fail:
1319     clear_bitmap_table(bs, tb, tb_size);
1320     g_free(buf);
1321     bdrv_dirty_iter_free(dbi);
1322     g_free(tb);
1323 
1324     return NULL;
1325 }
1326 
1327 /* store_bitmap()
1328  * Store bm->dirty_bitmap to qcow2.
1329  * Set bm->table_offset and bm->table_size accordingly.
1330  */
1331 static int store_bitmap(BlockDriverState *bs, Qcow2Bitmap *bm, Error **errp)
1332 {
1333     int ret;
1334     uint64_t *tb;
1335     int64_t tb_offset;
1336     uint32_t tb_size;
1337     BdrvDirtyBitmap *bitmap = bm->dirty_bitmap;
1338     const char *bm_name;
1339 
1340     assert(bitmap != NULL);
1341 
1342     bm_name = bdrv_dirty_bitmap_name(bitmap);
1343 
1344     tb = store_bitmap_data(bs, bitmap, &tb_size, errp);
1345     if (tb == NULL) {
1346         return -EINVAL;
1347     }
1348 
1349     assert(tb_size <= BME_MAX_TABLE_SIZE);
1350     tb_offset = qcow2_alloc_clusters(bs, tb_size * sizeof(tb[0]));
1351     if (tb_offset < 0) {
1352         error_setg_errno(errp, -tb_offset,
1353                          "Failed to allocate clusters for bitmap '%s'",
1354                          bm_name);
1355         ret = tb_offset;
1356         goto fail;
1357     }
1358 
1359     ret = qcow2_pre_write_overlap_check(bs, 0, tb_offset,
1360                                         tb_size * sizeof(tb[0]), false);
1361     if (ret < 0) {
1362         error_setg_errno(errp, -ret, "Qcow2 overlap check failed");
1363         goto fail;
1364     }
1365 
1366     bitmap_table_to_be(tb, tb_size);
1367     ret = bdrv_pwrite(bs->file, tb_offset, tb, tb_size * sizeof(tb[0]));
1368     if (ret < 0) {
1369         error_setg_errno(errp, -ret, "Failed to write bitmap '%s' to file",
1370                          bm_name);
1371         goto fail;
1372     }
1373 
1374     g_free(tb);
1375 
1376     bm->table.offset = tb_offset;
1377     bm->table.size = tb_size;
1378 
1379     return 0;
1380 
1381 fail:
1382     clear_bitmap_table(bs, tb, tb_size);
1383 
1384     if (tb_offset > 0) {
1385         qcow2_free_clusters(bs, tb_offset, tb_size * sizeof(tb[0]),
1386                             QCOW2_DISCARD_OTHER);
1387     }
1388 
1389     g_free(tb);
1390 
1391     return ret;
1392 }
1393 
1394 static Qcow2Bitmap *find_bitmap_by_name(Qcow2BitmapList *bm_list,
1395                                         const char *name)
1396 {
1397     Qcow2Bitmap *bm;
1398 
1399     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1400         if (strcmp(name, bm->name) == 0) {
1401             return bm;
1402         }
1403     }
1404 
1405     return NULL;
1406 }
1407 
1408 void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,
1409                                           const char *name,
1410                                           Error **errp)
1411 {
1412     int ret;
1413     BDRVQcow2State *s = bs->opaque;
1414     Qcow2Bitmap *bm;
1415     Qcow2BitmapList *bm_list;
1416 
1417     if (s->nb_bitmaps == 0) {
1418         /* Absence of the bitmap is not an error: see explanation above
1419          * bdrv_remove_persistent_dirty_bitmap() definition. */
1420         return;
1421     }
1422 
1423     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1424                                s->bitmap_directory_size, errp);
1425     if (bm_list == NULL) {
1426         return;
1427     }
1428 
1429     bm = find_bitmap_by_name(bm_list, name);
1430     if (bm == NULL) {
1431         goto fail;
1432     }
1433 
1434     QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
1435 
1436     ret = update_ext_header_and_dir(bs, bm_list);
1437     if (ret < 0) {
1438         error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1439         goto fail;
1440     }
1441 
1442     free_bitmap_clusters(bs, &bm->table);
1443 
1444 fail:
1445     bitmap_free(bm);
1446     bitmap_list_free(bm_list);
1447 }
1448 
1449 void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp)
1450 {
1451     BdrvDirtyBitmap *bitmap;
1452     BDRVQcow2State *s = bs->opaque;
1453     uint32_t new_nb_bitmaps = s->nb_bitmaps;
1454     uint64_t new_dir_size = s->bitmap_directory_size;
1455     int ret;
1456     Qcow2BitmapList *bm_list;
1457     Qcow2Bitmap *bm;
1458     QSIMPLEQ_HEAD(, Qcow2BitmapTable) drop_tables;
1459     Qcow2BitmapTable *tb, *tb_next;
1460 
1461     if (!bdrv_has_changed_persistent_bitmaps(bs)) {
1462         /* nothing to do */
1463         return;
1464     }
1465 
1466     if (!can_write(bs)) {
1467         error_setg(errp, "No write access");
1468         return;
1469     }
1470 
1471     QSIMPLEQ_INIT(&drop_tables);
1472 
1473     if (s->nb_bitmaps == 0) {
1474         bm_list = bitmap_list_new();
1475     } else {
1476         bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1477                                    s->bitmap_directory_size, errp);
1478         if (bm_list == NULL) {
1479             return;
1480         }
1481     }
1482 
1483     /* check constraints and names */
1484     for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1485          bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1486     {
1487         const char *name = bdrv_dirty_bitmap_name(bitmap);
1488         uint32_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
1489         Qcow2Bitmap *bm;
1490 
1491         if (!bdrv_dirty_bitmap_get_persistence(bitmap) ||
1492             bdrv_dirty_bitmap_readonly(bitmap) ||
1493             bdrv_dirty_bitmap_inconsistent(bitmap)) {
1494             continue;
1495         }
1496 
1497         if (check_constraints_on_bitmap(bs, name, granularity, errp) < 0) {
1498             error_prepend(errp, "Bitmap '%s' doesn't satisfy the constraints: ",
1499                           name);
1500             goto fail;
1501         }
1502 
1503         bm = find_bitmap_by_name(bm_list, name);
1504         if (bm == NULL) {
1505             if (++new_nb_bitmaps > QCOW2_MAX_BITMAPS) {
1506                 error_setg(errp, "Too many persistent bitmaps");
1507                 goto fail;
1508             }
1509 
1510             new_dir_size += calc_dir_entry_size(strlen(name), 0);
1511             if (new_dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
1512                 error_setg(errp, "Bitmap directory is too large");
1513                 goto fail;
1514             }
1515 
1516             bm = g_new0(Qcow2Bitmap, 1);
1517             bm->name = g_strdup(name);
1518             QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry);
1519         } else {
1520             if (!(bm->flags & BME_FLAG_IN_USE)) {
1521                 error_setg(errp, "Bitmap '%s' already exists in the image",
1522                            name);
1523                 goto fail;
1524             }
1525             tb = g_memdup(&bm->table, sizeof(bm->table));
1526             bm->table.offset = 0;
1527             bm->table.size = 0;
1528             QSIMPLEQ_INSERT_TAIL(&drop_tables, tb, entry);
1529         }
1530         bm->flags = bdrv_dirty_bitmap_enabled(bitmap) ? BME_FLAG_AUTO : 0;
1531         bm->granularity_bits = ctz32(bdrv_dirty_bitmap_granularity(bitmap));
1532         bm->dirty_bitmap = bitmap;
1533     }
1534 
1535     /* allocate clusters and store bitmaps */
1536     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1537         if (bm->dirty_bitmap == NULL) {
1538             continue;
1539         }
1540 
1541         ret = store_bitmap(bs, bm, errp);
1542         if (ret < 0) {
1543             goto fail;
1544         }
1545     }
1546 
1547     ret = update_ext_header_and_dir(bs, bm_list);
1548     if (ret < 0) {
1549         error_setg_errno(errp, -ret, "Failed to update bitmap extension");
1550         goto fail;
1551     }
1552 
1553     /* Bitmap directory was successfully updated, so, old data can be dropped.
1554      * TODO it is better to reuse these clusters */
1555     QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1556         free_bitmap_clusters(bs, tb);
1557         g_free(tb);
1558     }
1559 
1560     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1561         /* For safety, we remove bitmap after storing.
1562          * We may be here in two cases:
1563          * 1. bdrv_close. It's ok to drop bitmap.
1564          * 2. inactivation. It means migration without 'dirty-bitmaps'
1565          *    capability, so bitmaps are not marked with
1566          *    BdrvDirtyBitmap.migration flags. It's not bad to drop them too,
1567          *    and reload on invalidation.
1568          */
1569         if (bm->dirty_bitmap == NULL) {
1570             continue;
1571         }
1572 
1573         bdrv_release_dirty_bitmap(bs, bm->dirty_bitmap);
1574     }
1575 
1576     bitmap_list_free(bm_list);
1577     return;
1578 
1579 fail:
1580     QSIMPLEQ_FOREACH(bm, bm_list, entry) {
1581         if (bm->dirty_bitmap == NULL || bm->table.offset == 0) {
1582             continue;
1583         }
1584 
1585         free_bitmap_clusters(bs, &bm->table);
1586     }
1587 
1588     QSIMPLEQ_FOREACH_SAFE(tb, &drop_tables, entry, tb_next) {
1589         g_free(tb);
1590     }
1591 
1592     bitmap_list_free(bm_list);
1593 }
1594 
1595 int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp)
1596 {
1597     BdrvDirtyBitmap *bitmap;
1598     Error *local_err = NULL;
1599 
1600     qcow2_store_persistent_dirty_bitmaps(bs, &local_err);
1601     if (local_err != NULL) {
1602         error_propagate(errp, local_err);
1603         return -EINVAL;
1604     }
1605 
1606     for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
1607          bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
1608     {
1609         if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
1610             bdrv_dirty_bitmap_set_readonly(bitmap, true);
1611         }
1612     }
1613 
1614     return 0;
1615 }
1616 
1617 bool qcow2_can_store_new_dirty_bitmap(BlockDriverState *bs,
1618                                       const char *name,
1619                                       uint32_t granularity,
1620                                       Error **errp)
1621 {
1622     BDRVQcow2State *s = bs->opaque;
1623     bool found;
1624     Qcow2BitmapList *bm_list;
1625 
1626     if (s->qcow_version < 3) {
1627         /* Without autoclear_features, we would always have to assume
1628          * that a program without persistent dirty bitmap support has
1629          * accessed this qcow2 file when opening it, and would thus
1630          * have to drop all dirty bitmaps (defeating their purpose).
1631          */
1632         error_setg(errp, "Cannot store dirty bitmaps in qcow2 v2 files");
1633         goto fail;
1634     }
1635 
1636     if (check_constraints_on_bitmap(bs, name, granularity, errp) != 0) {
1637         goto fail;
1638     }
1639 
1640     if (s->nb_bitmaps == 0) {
1641         return true;
1642     }
1643 
1644     if (s->nb_bitmaps >= QCOW2_MAX_BITMAPS) {
1645         error_setg(errp,
1646                    "Maximum number of persistent bitmaps is already reached");
1647         goto fail;
1648     }
1649 
1650     if (s->bitmap_directory_size + calc_dir_entry_size(strlen(name), 0) >
1651         QCOW2_MAX_BITMAP_DIRECTORY_SIZE)
1652     {
1653         error_setg(errp, "Not enough space in the bitmap directory");
1654         goto fail;
1655     }
1656 
1657     bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
1658                                s->bitmap_directory_size, errp);
1659     if (bm_list == NULL) {
1660         goto fail;
1661     }
1662 
1663     found = find_bitmap_by_name(bm_list, name);
1664     bitmap_list_free(bm_list);
1665     if (found) {
1666         error_setg(errp, "Bitmap with the same name is already stored");
1667         goto fail;
1668     }
1669 
1670     return true;
1671 
1672 fail:
1673     error_prepend(errp, "Can't make bitmap '%s' persistent in '%s': ",
1674                   name, bdrv_get_device_or_node_name(bs));
1675     return false;
1676 }
1677