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