]>
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" | |
30 | ||
31 | #include "block/block_int.h" | |
32 | #include "block/qcow2.h" | |
33 | ||
34 | /* NOTICE: BME here means Bitmaps Extension and used as a namespace for | |
35 | * _internal_ constants. Please do not use this _internal_ abbreviation for | |
36 | * other needs and/or outside of this file. */ | |
37 | ||
38 | /* Bitmap directory entry constraints */ | |
39 | #define BME_MAX_TABLE_SIZE 0x8000000 | |
40 | #define BME_MAX_PHYS_SIZE 0x20000000 /* restrict BdrvDirtyBitmap size in RAM */ | |
41 | #define BME_MAX_GRANULARITY_BITS 31 | |
42 | #define BME_MIN_GRANULARITY_BITS 9 | |
43 | #define BME_MAX_NAME_SIZE 1023 | |
44 | ||
45 | /* Bitmap directory entry flags */ | |
46 | #define BME_RESERVED_FLAGS 0xfffffffcU | |
d1258dd0 VSO |
47 | #define BME_FLAG_IN_USE (1U << 0) |
48 | #define BME_FLAG_AUTO (1U << 1) | |
88ddffae VSO |
49 | |
50 | /* bits [1, 8] U [56, 63] are reserved */ | |
51 | #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001feULL | |
52 | #define BME_TABLE_ENTRY_OFFSET_MASK 0x00fffffffffffe00ULL | |
53 | #define BME_TABLE_ENTRY_FLAG_ALL_ONES (1ULL << 0) | |
54 | ||
55 | typedef struct QEMU_PACKED Qcow2BitmapDirEntry { | |
56 | /* header is 8 byte aligned */ | |
57 | uint64_t bitmap_table_offset; | |
58 | ||
59 | uint32_t bitmap_table_size; | |
60 | uint32_t flags; | |
61 | ||
62 | uint8_t type; | |
63 | uint8_t granularity_bits; | |
64 | uint16_t name_size; | |
65 | uint32_t extra_data_size; | |
66 | /* extra data follows */ | |
67 | /* name follows */ | |
68 | } Qcow2BitmapDirEntry; | |
69 | ||
70 | typedef struct Qcow2BitmapTable { | |
71 | uint64_t offset; | |
72 | uint32_t size; /* number of 64bit entries */ | |
73 | QSIMPLEQ_ENTRY(Qcow2BitmapTable) entry; | |
74 | } Qcow2BitmapTable; | |
75 | ||
76 | typedef struct Qcow2Bitmap { | |
77 | Qcow2BitmapTable table; | |
78 | uint32_t flags; | |
79 | uint8_t granularity_bits; | |
80 | char *name; | |
81 | ||
82 | QSIMPLEQ_ENTRY(Qcow2Bitmap) entry; | |
83 | } Qcow2Bitmap; | |
84 | typedef QSIMPLEQ_HEAD(Qcow2BitmapList, Qcow2Bitmap) Qcow2BitmapList; | |
85 | ||
86 | typedef enum BitmapType { | |
87 | BT_DIRTY_TRACKING_BITMAP = 1 | |
88 | } BitmapType; | |
89 | ||
d1258dd0 VSO |
90 | static inline bool can_write(BlockDriverState *bs) |
91 | { | |
92 | return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE); | |
93 | } | |
94 | ||
95 | static int update_header_sync(BlockDriverState *bs) | |
96 | { | |
97 | int ret; | |
98 | ||
99 | ret = qcow2_update_header(bs); | |
100 | if (ret < 0) { | |
101 | return ret; | |
102 | } | |
103 | ||
104 | return bdrv_flush(bs); | |
105 | } | |
106 | ||
88ddffae VSO |
107 | static int check_table_entry(uint64_t entry, int cluster_size) |
108 | { | |
109 | uint64_t offset; | |
110 | ||
111 | if (entry & BME_TABLE_ENTRY_RESERVED_MASK) { | |
112 | return -EINVAL; | |
113 | } | |
114 | ||
115 | offset = entry & BME_TABLE_ENTRY_OFFSET_MASK; | |
116 | if (offset != 0) { | |
117 | /* if offset specified, bit 0 is reserved */ | |
118 | if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) { | |
119 | return -EINVAL; | |
120 | } | |
121 | ||
122 | if (offset % cluster_size != 0) { | |
123 | return -EINVAL; | |
124 | } | |
125 | } | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
130 | static int bitmap_table_load(BlockDriverState *bs, Qcow2BitmapTable *tb, | |
131 | uint64_t **bitmap_table) | |
132 | { | |
133 | int ret; | |
134 | BDRVQcow2State *s = bs->opaque; | |
135 | uint32_t i; | |
136 | uint64_t *table; | |
137 | ||
138 | assert(tb->size != 0); | |
139 | table = g_try_new(uint64_t, tb->size); | |
140 | if (table == NULL) { | |
141 | return -ENOMEM; | |
142 | } | |
143 | ||
144 | assert(tb->size <= BME_MAX_TABLE_SIZE); | |
145 | ret = bdrv_pread(bs->file, tb->offset, | |
146 | table, tb->size * sizeof(uint64_t)); | |
147 | if (ret < 0) { | |
148 | goto fail; | |
149 | } | |
150 | ||
151 | for (i = 0; i < tb->size; ++i) { | |
152 | be64_to_cpus(&table[i]); | |
153 | ret = check_table_entry(table[i], s->cluster_size); | |
154 | if (ret < 0) { | |
155 | goto fail; | |
156 | } | |
157 | } | |
158 | ||
159 | *bitmap_table = table; | |
160 | return 0; | |
161 | ||
162 | fail: | |
163 | g_free(table); | |
164 | ||
165 | return ret; | |
166 | } | |
167 | ||
d1258dd0 VSO |
168 | /* This function returns the number of disk sectors covered by a single qcow2 |
169 | * cluster of bitmap data. */ | |
170 | static uint64_t sectors_covered_by_bitmap_cluster(const BDRVQcow2State *s, | |
171 | const BdrvDirtyBitmap *bitmap) | |
172 | { | |
173 | uint32_t sector_granularity = | |
174 | bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS; | |
175 | ||
176 | return (uint64_t)sector_granularity * (s->cluster_size << 3); | |
177 | } | |
178 | ||
179 | /* load_bitmap_data | |
180 | * @bitmap_table entries must satisfy specification constraints. | |
181 | * @bitmap must be cleared */ | |
182 | static int load_bitmap_data(BlockDriverState *bs, | |
183 | const uint64_t *bitmap_table, | |
184 | uint32_t bitmap_table_size, | |
185 | BdrvDirtyBitmap *bitmap) | |
186 | { | |
187 | int ret = 0; | |
188 | BDRVQcow2State *s = bs->opaque; | |
189 | uint64_t sector, sbc; | |
190 | uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap); | |
191 | uint8_t *buf = NULL; | |
192 | uint64_t i, tab_size = | |
193 | size_to_clusters(s, | |
194 | bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size)); | |
195 | ||
196 | if (tab_size != bitmap_table_size || tab_size > BME_MAX_TABLE_SIZE) { | |
197 | return -EINVAL; | |
198 | } | |
199 | ||
200 | buf = g_malloc(s->cluster_size); | |
201 | sbc = sectors_covered_by_bitmap_cluster(s, bitmap); | |
202 | for (i = 0, sector = 0; i < tab_size; ++i, sector += sbc) { | |
203 | uint64_t count = MIN(bm_size - sector, sbc); | |
204 | uint64_t entry = bitmap_table[i]; | |
205 | uint64_t offset = entry & BME_TABLE_ENTRY_OFFSET_MASK; | |
206 | ||
207 | assert(check_table_entry(entry, s->cluster_size) == 0); | |
208 | ||
209 | if (offset == 0) { | |
210 | if (entry & BME_TABLE_ENTRY_FLAG_ALL_ONES) { | |
211 | bdrv_dirty_bitmap_deserialize_ones(bitmap, sector, count, | |
212 | false); | |
213 | } else { | |
214 | /* No need to deserialize zeros because the dirty bitmap is | |
215 | * already cleared */ | |
216 | } | |
217 | } else { | |
218 | ret = bdrv_pread(bs->file, offset, buf, s->cluster_size); | |
219 | if (ret < 0) { | |
220 | goto finish; | |
221 | } | |
222 | bdrv_dirty_bitmap_deserialize_part(bitmap, buf, sector, count, | |
223 | false); | |
224 | } | |
225 | } | |
226 | ret = 0; | |
227 | ||
228 | bdrv_dirty_bitmap_deserialize_finish(bitmap); | |
229 | ||
230 | finish: | |
231 | g_free(buf); | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs, | |
237 | Qcow2Bitmap *bm, Error **errp) | |
238 | { | |
239 | int ret; | |
240 | uint64_t *bitmap_table = NULL; | |
241 | uint32_t granularity; | |
242 | BdrvDirtyBitmap *bitmap = NULL; | |
243 | ||
244 | if (bm->flags & BME_FLAG_IN_USE) { | |
245 | error_setg(errp, "Bitmap '%s' is in use", bm->name); | |
246 | goto fail; | |
247 | } | |
248 | ||
249 | ret = bitmap_table_load(bs, &bm->table, &bitmap_table); | |
250 | if (ret < 0) { | |
251 | error_setg_errno(errp, -ret, | |
252 | "Could not read bitmap_table table from image for " | |
253 | "bitmap '%s'", bm->name); | |
254 | goto fail; | |
255 | } | |
256 | ||
257 | granularity = 1U << bm->granularity_bits; | |
258 | bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp); | |
259 | if (bitmap == NULL) { | |
260 | goto fail; | |
261 | } | |
262 | ||
263 | ret = load_bitmap_data(bs, bitmap_table, bm->table.size, bitmap); | |
264 | if (ret < 0) { | |
265 | error_setg_errno(errp, -ret, "Could not read bitmap '%s' from image", | |
266 | bm->name); | |
267 | goto fail; | |
268 | } | |
269 | ||
270 | g_free(bitmap_table); | |
271 | return bitmap; | |
272 | ||
273 | fail: | |
274 | g_free(bitmap_table); | |
275 | if (bitmap != NULL) { | |
276 | bdrv_release_dirty_bitmap(bs, bitmap); | |
277 | } | |
278 | ||
279 | return NULL; | |
280 | } | |
281 | ||
88ddffae VSO |
282 | /* |
283 | * Bitmap List | |
284 | */ | |
285 | ||
286 | /* | |
287 | * Bitmap List private functions | |
288 | * Only Bitmap List knows about bitmap directory structure in Qcow2. | |
289 | */ | |
290 | ||
291 | static inline void bitmap_dir_entry_to_cpu(Qcow2BitmapDirEntry *entry) | |
292 | { | |
293 | be64_to_cpus(&entry->bitmap_table_offset); | |
294 | be32_to_cpus(&entry->bitmap_table_size); | |
295 | be32_to_cpus(&entry->flags); | |
296 | be16_to_cpus(&entry->name_size); | |
297 | be32_to_cpus(&entry->extra_data_size); | |
298 | } | |
299 | ||
d1258dd0 VSO |
300 | static inline void bitmap_dir_entry_to_be(Qcow2BitmapDirEntry *entry) |
301 | { | |
302 | cpu_to_be64s(&entry->bitmap_table_offset); | |
303 | cpu_to_be32s(&entry->bitmap_table_size); | |
304 | cpu_to_be32s(&entry->flags); | |
305 | cpu_to_be16s(&entry->name_size); | |
306 | cpu_to_be32s(&entry->extra_data_size); | |
307 | } | |
308 | ||
88ddffae VSO |
309 | static inline int calc_dir_entry_size(size_t name_size, size_t extra_data_size) |
310 | { | |
311 | return align_offset(sizeof(Qcow2BitmapDirEntry) + | |
312 | name_size + extra_data_size, 8); | |
313 | } | |
314 | ||
315 | static inline int dir_entry_size(Qcow2BitmapDirEntry *entry) | |
316 | { | |
317 | return calc_dir_entry_size(entry->name_size, entry->extra_data_size); | |
318 | } | |
319 | ||
320 | static inline const char *dir_entry_name_field(Qcow2BitmapDirEntry *entry) | |
321 | { | |
322 | return (const char *)(entry + 1) + entry->extra_data_size; | |
323 | } | |
324 | ||
325 | static inline char *dir_entry_copy_name(Qcow2BitmapDirEntry *entry) | |
326 | { | |
327 | const char *name_field = dir_entry_name_field(entry); | |
328 | return g_strndup(name_field, entry->name_size); | |
329 | } | |
330 | ||
331 | static inline Qcow2BitmapDirEntry *next_dir_entry(Qcow2BitmapDirEntry *entry) | |
332 | { | |
333 | return (Qcow2BitmapDirEntry *)((uint8_t *)entry + dir_entry_size(entry)); | |
334 | } | |
335 | ||
336 | static int check_dir_entry(BlockDriverState *bs, Qcow2BitmapDirEntry *entry) | |
337 | { | |
338 | BDRVQcow2State *s = bs->opaque; | |
339 | uint64_t phys_bitmap_bytes; | |
340 | int64_t len; | |
341 | ||
342 | bool fail = (entry->bitmap_table_size == 0) || | |
343 | (entry->bitmap_table_offset == 0) || | |
344 | (entry->bitmap_table_offset % s->cluster_size) || | |
345 | (entry->bitmap_table_size > BME_MAX_TABLE_SIZE) || | |
346 | (entry->granularity_bits > BME_MAX_GRANULARITY_BITS) || | |
347 | (entry->granularity_bits < BME_MIN_GRANULARITY_BITS) || | |
348 | (entry->flags & BME_RESERVED_FLAGS) || | |
349 | (entry->name_size > BME_MAX_NAME_SIZE) || | |
350 | (entry->type != BT_DIRTY_TRACKING_BITMAP); | |
351 | ||
352 | if (fail) { | |
353 | return -EINVAL; | |
354 | } | |
355 | ||
356 | phys_bitmap_bytes = (uint64_t)entry->bitmap_table_size * s->cluster_size; | |
357 | len = bdrv_getlength(bs); | |
358 | ||
359 | if (len < 0) { | |
360 | return len; | |
361 | } | |
362 | ||
363 | fail = (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) || | |
364 | (len > ((phys_bitmap_bytes * 8) << entry->granularity_bits)); | |
365 | ||
366 | return fail ? -EINVAL : 0; | |
367 | } | |
368 | ||
d1258dd0 VSO |
369 | static inline void bitmap_directory_to_be(uint8_t *dir, size_t size) |
370 | { | |
371 | uint8_t *end = dir + size; | |
372 | while (dir < end) { | |
373 | Qcow2BitmapDirEntry *e = (Qcow2BitmapDirEntry *)dir; | |
374 | dir += dir_entry_size(e); | |
375 | ||
376 | bitmap_dir_entry_to_be(e); | |
377 | } | |
378 | } | |
379 | ||
88ddffae VSO |
380 | /* |
381 | * Bitmap List public functions | |
382 | */ | |
383 | ||
384 | static void bitmap_free(Qcow2Bitmap *bm) | |
385 | { | |
386 | g_free(bm->name); | |
387 | g_free(bm); | |
388 | } | |
389 | ||
390 | static void bitmap_list_free(Qcow2BitmapList *bm_list) | |
391 | { | |
392 | Qcow2Bitmap *bm; | |
393 | ||
394 | if (bm_list == NULL) { | |
395 | return; | |
396 | } | |
397 | ||
398 | while ((bm = QSIMPLEQ_FIRST(bm_list)) != NULL) { | |
399 | QSIMPLEQ_REMOVE_HEAD(bm_list, entry); | |
400 | bitmap_free(bm); | |
401 | } | |
402 | ||
403 | g_free(bm_list); | |
404 | } | |
405 | ||
406 | static Qcow2BitmapList *bitmap_list_new(void) | |
407 | { | |
408 | Qcow2BitmapList *bm_list = g_new(Qcow2BitmapList, 1); | |
409 | QSIMPLEQ_INIT(bm_list); | |
410 | ||
411 | return bm_list; | |
412 | } | |
413 | ||
d1258dd0 VSO |
414 | static uint32_t bitmap_list_count(Qcow2BitmapList *bm_list) |
415 | { | |
416 | Qcow2Bitmap *bm; | |
417 | uint32_t nb_bitmaps = 0; | |
418 | ||
419 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
420 | nb_bitmaps++; | |
421 | } | |
422 | ||
423 | return nb_bitmaps; | |
424 | } | |
425 | ||
88ddffae VSO |
426 | /* bitmap_list_load |
427 | * Get bitmap list from qcow2 image. Actually reads bitmap directory, | |
428 | * checks it and convert to bitmap list. | |
429 | */ | |
430 | static Qcow2BitmapList *bitmap_list_load(BlockDriverState *bs, uint64_t offset, | |
431 | uint64_t size, Error **errp) | |
432 | { | |
433 | int ret; | |
434 | BDRVQcow2State *s = bs->opaque; | |
435 | uint8_t *dir, *dir_end; | |
436 | Qcow2BitmapDirEntry *e; | |
437 | uint32_t nb_dir_entries = 0; | |
438 | Qcow2BitmapList *bm_list = NULL; | |
439 | ||
440 | if (size == 0) { | |
441 | error_setg(errp, "Requested bitmap directory size is zero"); | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | if (size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) { | |
446 | error_setg(errp, "Requested bitmap directory size is too big"); | |
447 | return NULL; | |
448 | } | |
449 | ||
450 | dir = g_try_malloc(size); | |
451 | if (dir == NULL) { | |
452 | error_setg(errp, "Failed to allocate space for bitmap directory"); | |
453 | return NULL; | |
454 | } | |
455 | dir_end = dir + size; | |
456 | ||
457 | ret = bdrv_pread(bs->file, offset, dir, size); | |
458 | if (ret < 0) { | |
459 | error_setg_errno(errp, -ret, "Failed to read bitmap directory"); | |
460 | goto fail; | |
461 | } | |
462 | ||
463 | bm_list = bitmap_list_new(); | |
464 | for (e = (Qcow2BitmapDirEntry *)dir; | |
465 | e < (Qcow2BitmapDirEntry *)dir_end; | |
466 | e = next_dir_entry(e)) | |
467 | { | |
468 | Qcow2Bitmap *bm; | |
469 | ||
470 | if ((uint8_t *)(e + 1) > dir_end) { | |
471 | goto broken_dir; | |
472 | } | |
473 | ||
474 | if (++nb_dir_entries > s->nb_bitmaps) { | |
475 | error_setg(errp, "More bitmaps found than specified in header" | |
476 | " extension"); | |
477 | goto fail; | |
478 | } | |
479 | bitmap_dir_entry_to_cpu(e); | |
480 | ||
481 | if ((uint8_t *)next_dir_entry(e) > dir_end) { | |
482 | goto broken_dir; | |
483 | } | |
484 | ||
485 | if (e->extra_data_size != 0) { | |
486 | error_setg(errp, "Bitmap extra data is not supported"); | |
487 | goto fail; | |
488 | } | |
489 | ||
490 | ret = check_dir_entry(bs, e); | |
491 | if (ret < 0) { | |
492 | error_setg(errp, "Bitmap '%.*s' doesn't satisfy the constraints", | |
493 | e->name_size, dir_entry_name_field(e)); | |
494 | goto fail; | |
495 | } | |
496 | ||
497 | bm = g_new(Qcow2Bitmap, 1); | |
498 | bm->table.offset = e->bitmap_table_offset; | |
499 | bm->table.size = e->bitmap_table_size; | |
500 | bm->flags = e->flags; | |
501 | bm->granularity_bits = e->granularity_bits; | |
502 | bm->name = dir_entry_copy_name(e); | |
503 | QSIMPLEQ_INSERT_TAIL(bm_list, bm, entry); | |
504 | } | |
505 | ||
506 | if (nb_dir_entries != s->nb_bitmaps) { | |
507 | error_setg(errp, "Less bitmaps found than specified in header" | |
508 | " extension"); | |
509 | goto fail; | |
510 | } | |
511 | ||
512 | if ((uint8_t *)e != dir_end) { | |
513 | goto broken_dir; | |
514 | } | |
515 | ||
516 | g_free(dir); | |
517 | return bm_list; | |
518 | ||
519 | broken_dir: | |
520 | ret = -EINVAL; | |
521 | error_setg(errp, "Broken bitmap directory"); | |
522 | ||
523 | fail: | |
524 | g_free(dir); | |
525 | bitmap_list_free(bm_list); | |
526 | ||
527 | return NULL; | |
528 | } | |
529 | ||
530 | int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res, | |
531 | void **refcount_table, | |
532 | int64_t *refcount_table_size) | |
533 | { | |
534 | int ret; | |
535 | BDRVQcow2State *s = bs->opaque; | |
536 | Qcow2BitmapList *bm_list; | |
537 | Qcow2Bitmap *bm; | |
538 | ||
539 | if (s->nb_bitmaps == 0) { | |
540 | return 0; | |
541 | } | |
542 | ||
543 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size, | |
544 | s->bitmap_directory_offset, | |
545 | s->bitmap_directory_size); | |
546 | if (ret < 0) { | |
547 | return ret; | |
548 | } | |
549 | ||
550 | bm_list = bitmap_list_load(bs, s->bitmap_directory_offset, | |
551 | s->bitmap_directory_size, NULL); | |
552 | if (bm_list == NULL) { | |
553 | res->corruptions++; | |
554 | return -EINVAL; | |
555 | } | |
556 | ||
557 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
558 | uint64_t *bitmap_table = NULL; | |
559 | int i; | |
560 | ||
561 | ret = qcow2_inc_refcounts_imrt(bs, res, | |
562 | refcount_table, refcount_table_size, | |
563 | bm->table.offset, | |
564 | bm->table.size * sizeof(uint64_t)); | |
565 | if (ret < 0) { | |
566 | goto out; | |
567 | } | |
568 | ||
569 | ret = bitmap_table_load(bs, &bm->table, &bitmap_table); | |
570 | if (ret < 0) { | |
571 | res->corruptions++; | |
572 | goto out; | |
573 | } | |
574 | ||
575 | for (i = 0; i < bm->table.size; ++i) { | |
576 | uint64_t entry = bitmap_table[i]; | |
577 | uint64_t offset = entry & BME_TABLE_ENTRY_OFFSET_MASK; | |
578 | ||
579 | if (check_table_entry(entry, s->cluster_size) < 0) { | |
580 | res->corruptions++; | |
581 | continue; | |
582 | } | |
583 | ||
584 | if (offset == 0) { | |
585 | continue; | |
586 | } | |
587 | ||
588 | ret = qcow2_inc_refcounts_imrt(bs, res, | |
589 | refcount_table, refcount_table_size, | |
590 | offset, s->cluster_size); | |
591 | if (ret < 0) { | |
592 | g_free(bitmap_table); | |
593 | goto out; | |
594 | } | |
595 | } | |
596 | ||
597 | g_free(bitmap_table); | |
598 | } | |
599 | ||
600 | out: | |
601 | bitmap_list_free(bm_list); | |
602 | ||
603 | return ret; | |
604 | } | |
d1258dd0 VSO |
605 | |
606 | /* bitmap_list_store | |
607 | * Store bitmap list to qcow2 image as a bitmap directory. | |
608 | * Everything is checked. | |
609 | */ | |
610 | static int bitmap_list_store(BlockDriverState *bs, Qcow2BitmapList *bm_list, | |
611 | uint64_t *offset, uint64_t *size, bool in_place) | |
612 | { | |
613 | int ret; | |
614 | uint8_t *dir; | |
615 | int64_t dir_offset = 0; | |
616 | uint64_t dir_size = 0; | |
617 | Qcow2Bitmap *bm; | |
618 | Qcow2BitmapDirEntry *e; | |
619 | ||
620 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
621 | dir_size += calc_dir_entry_size(strlen(bm->name), 0); | |
622 | } | |
623 | ||
624 | if (dir_size == 0 || dir_size > QCOW2_MAX_BITMAP_DIRECTORY_SIZE) { | |
625 | return -EINVAL; | |
626 | } | |
627 | ||
628 | if (in_place) { | |
629 | if (*size != dir_size || *offset == 0) { | |
630 | return -EINVAL; | |
631 | } | |
632 | ||
633 | dir_offset = *offset; | |
634 | } | |
635 | ||
636 | dir = g_try_malloc(dir_size); | |
637 | if (dir == NULL) { | |
638 | return -ENOMEM; | |
639 | } | |
640 | ||
641 | e = (Qcow2BitmapDirEntry *)dir; | |
642 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
643 | e->bitmap_table_offset = bm->table.offset; | |
644 | e->bitmap_table_size = bm->table.size; | |
645 | e->flags = bm->flags; | |
646 | e->type = BT_DIRTY_TRACKING_BITMAP; | |
647 | e->granularity_bits = bm->granularity_bits; | |
648 | e->name_size = strlen(bm->name); | |
649 | e->extra_data_size = 0; | |
650 | memcpy(e + 1, bm->name, e->name_size); | |
651 | ||
652 | if (check_dir_entry(bs, e) < 0) { | |
653 | ret = -EINVAL; | |
654 | goto fail; | |
655 | } | |
656 | ||
657 | e = next_dir_entry(e); | |
658 | } | |
659 | ||
660 | bitmap_directory_to_be(dir, dir_size); | |
661 | ||
662 | if (!in_place) { | |
663 | dir_offset = qcow2_alloc_clusters(bs, dir_size); | |
664 | if (dir_offset < 0) { | |
665 | ret = dir_offset; | |
666 | goto fail; | |
667 | } | |
668 | } | |
669 | ||
670 | ret = qcow2_pre_write_overlap_check(bs, 0, dir_offset, dir_size); | |
671 | if (ret < 0) { | |
672 | goto fail; | |
673 | } | |
674 | ||
675 | ret = bdrv_pwrite(bs->file, dir_offset, dir, dir_size); | |
676 | if (ret < 0) { | |
677 | goto fail; | |
678 | } | |
679 | ||
680 | g_free(dir); | |
681 | ||
682 | if (!in_place) { | |
683 | *size = dir_size; | |
684 | *offset = dir_offset; | |
685 | } | |
686 | ||
687 | return 0; | |
688 | ||
689 | fail: | |
690 | g_free(dir); | |
691 | ||
692 | if (!in_place && dir_offset > 0) { | |
693 | qcow2_free_clusters(bs, dir_offset, dir_size, QCOW2_DISCARD_OTHER); | |
694 | } | |
695 | ||
696 | return ret; | |
697 | } | |
698 | ||
699 | /* | |
700 | * Bitmap List end | |
701 | */ | |
702 | ||
703 | static int update_ext_header_and_dir_in_place(BlockDriverState *bs, | |
704 | Qcow2BitmapList *bm_list) | |
705 | { | |
706 | BDRVQcow2State *s = bs->opaque; | |
707 | int ret; | |
708 | ||
709 | if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS) || | |
710 | bm_list == NULL || QSIMPLEQ_EMPTY(bm_list) || | |
711 | bitmap_list_count(bm_list) != s->nb_bitmaps) | |
712 | { | |
713 | return -EINVAL; | |
714 | } | |
715 | ||
716 | s->autoclear_features &= ~(uint64_t)QCOW2_AUTOCLEAR_BITMAPS; | |
717 | ret = update_header_sync(bs); | |
718 | if (ret < 0) { | |
719 | /* Two variants are possible here: | |
720 | * 1. Autoclear flag is dropped, all bitmaps will be lost. | |
721 | * 2. Autoclear flag is not dropped, old state is left. | |
722 | */ | |
723 | return ret; | |
724 | } | |
725 | ||
726 | /* autoclear bit is not set, so we can safely update bitmap directory */ | |
727 | ||
728 | ret = bitmap_list_store(bs, bm_list, &s->bitmap_directory_offset, | |
729 | &s->bitmap_directory_size, true); | |
730 | if (ret < 0) { | |
731 | /* autoclear bit is cleared, so all leaked clusters would be removed on | |
732 | * qemu-img check */ | |
733 | return ret; | |
734 | } | |
735 | ||
736 | ret = update_header_sync(bs); | |
737 | if (ret < 0) { | |
738 | /* autoclear bit is cleared, so all leaked clusters would be removed on | |
739 | * qemu-img check */ | |
740 | return ret; | |
741 | } | |
742 | ||
743 | s->autoclear_features |= QCOW2_AUTOCLEAR_BITMAPS; | |
744 | return update_header_sync(bs); | |
745 | /* If final update_header_sync() fails, two variants are possible: | |
746 | * 1. Autoclear flag is not set, all bitmaps will be lost. | |
747 | * 2. Autoclear flag is set, header and directory are successfully updated. | |
748 | */ | |
749 | } | |
750 | ||
751 | /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */ | |
752 | static void release_dirty_bitmap_helper(gpointer bitmap, | |
753 | gpointer bs) | |
754 | { | |
755 | bdrv_release_dirty_bitmap(bs, bitmap); | |
756 | } | |
757 | ||
758 | /* for g_slist_foreach for GSList of BdrvDirtyBitmap* elements */ | |
759 | static void set_readonly_helper(gpointer bitmap, gpointer value) | |
760 | { | |
761 | bdrv_dirty_bitmap_set_readonly(bitmap, (bool)value); | |
762 | } | |
763 | ||
764 | /* qcow2_load_autoloading_dirty_bitmaps() | |
765 | * Return value is a hint for caller: true means that the Qcow2 header was | |
766 | * updated. (false doesn't mean that the header should be updated by the | |
767 | * caller, it just means that updating was not needed or the image cannot be | |
768 | * written to). | |
769 | * On failure the function returns false. | |
770 | */ | |
771 | bool qcow2_load_autoloading_dirty_bitmaps(BlockDriverState *bs, Error **errp) | |
772 | { | |
773 | BDRVQcow2State *s = bs->opaque; | |
774 | Qcow2BitmapList *bm_list; | |
775 | Qcow2Bitmap *bm; | |
776 | GSList *created_dirty_bitmaps = NULL; | |
777 | bool header_updated = false; | |
778 | ||
779 | if (s->nb_bitmaps == 0) { | |
780 | /* No bitmaps - nothing to do */ | |
781 | return false; | |
782 | } | |
783 | ||
784 | bm_list = bitmap_list_load(bs, s->bitmap_directory_offset, | |
785 | s->bitmap_directory_size, errp); | |
786 | if (bm_list == NULL) { | |
787 | return false; | |
788 | } | |
789 | ||
790 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
791 | if ((bm->flags & BME_FLAG_AUTO) && !(bm->flags & BME_FLAG_IN_USE)) { | |
792 | BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp); | |
793 | if (bitmap == NULL) { | |
794 | goto fail; | |
795 | } | |
a0319aac VSO |
796 | |
797 | bdrv_dirty_bitmap_set_autoload(bitmap, true); | |
d1258dd0 VSO |
798 | bm->flags |= BME_FLAG_IN_USE; |
799 | created_dirty_bitmaps = | |
800 | g_slist_append(created_dirty_bitmaps, bitmap); | |
801 | } | |
802 | } | |
803 | ||
804 | if (created_dirty_bitmaps != NULL) { | |
805 | if (can_write(bs)) { | |
806 | /* in_use flags must be updated */ | |
807 | int ret = update_ext_header_and_dir_in_place(bs, bm_list); | |
808 | if (ret < 0) { | |
809 | error_setg_errno(errp, -ret, "Can't update bitmap directory"); | |
810 | goto fail; | |
811 | } | |
812 | header_updated = true; | |
813 | } else { | |
814 | g_slist_foreach(created_dirty_bitmaps, set_readonly_helper, | |
815 | (gpointer)true); | |
816 | } | |
817 | } | |
818 | ||
819 | g_slist_free(created_dirty_bitmaps); | |
820 | bitmap_list_free(bm_list); | |
821 | ||
822 | return header_updated; | |
823 | ||
824 | fail: | |
825 | g_slist_foreach(created_dirty_bitmaps, release_dirty_bitmap_helper, bs); | |
826 | g_slist_free(created_dirty_bitmaps); | |
827 | bitmap_list_free(bm_list); | |
828 | ||
829 | return false; | |
830 | } | |
1b6b0562 VSO |
831 | |
832 | int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp) | |
833 | { | |
834 | BDRVQcow2State *s = bs->opaque; | |
835 | Qcow2BitmapList *bm_list; | |
836 | Qcow2Bitmap *bm; | |
837 | GSList *ro_dirty_bitmaps = NULL; | |
838 | int ret = 0; | |
839 | ||
840 | if (s->nb_bitmaps == 0) { | |
841 | /* No bitmaps - nothing to do */ | |
842 | return 0; | |
843 | } | |
844 | ||
845 | if (!can_write(bs)) { | |
846 | error_setg(errp, "Can't write to the image on reopening bitmaps rw"); | |
847 | return -EINVAL; | |
848 | } | |
849 | ||
850 | bm_list = bitmap_list_load(bs, s->bitmap_directory_offset, | |
851 | s->bitmap_directory_size, errp); | |
852 | if (bm_list == NULL) { | |
853 | return -EINVAL; | |
854 | } | |
855 | ||
856 | QSIMPLEQ_FOREACH(bm, bm_list, entry) { | |
857 | if (!(bm->flags & BME_FLAG_IN_USE)) { | |
858 | BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name); | |
859 | if (bitmap == NULL) { | |
860 | continue; | |
861 | } | |
862 | ||
863 | if (!bdrv_dirty_bitmap_readonly(bitmap)) { | |
864 | error_setg(errp, "Bitmap %s is not readonly but not marked" | |
865 | "'IN_USE' in the image. Something went wrong," | |
866 | "all the bitmaps may be corrupted", bm->name); | |
867 | ret = -EINVAL; | |
868 | goto out; | |
869 | } | |
870 | ||
871 | bm->flags |= BME_FLAG_IN_USE; | |
872 | ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap); | |
873 | } | |
874 | } | |
875 | ||
876 | if (ro_dirty_bitmaps != NULL) { | |
877 | /* in_use flags must be updated */ | |
878 | ret = update_ext_header_and_dir_in_place(bs, bm_list); | |
879 | if (ret < 0) { | |
880 | error_setg_errno(errp, -ret, "Can't update bitmap directory"); | |
881 | goto out; | |
882 | } | |
883 | g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false); | |
884 | } | |
885 | ||
886 | out: | |
887 | g_slist_free(ro_dirty_bitmaps); | |
888 | bitmap_list_free(bm_list); | |
889 | ||
890 | return ret; | |
891 | } |