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