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