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