]>
Commit | Line | Data |
---|---|---|
f7d0fe02 KW |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
3 | * | |
4 | * Copyright (c) 2004-2006 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu-common.h" | |
737e150e | 26 | #include "block/block_int.h" |
f7d0fe02 | 27 | #include "block/qcow2.h" |
a40f1c2a | 28 | #include "qemu/range.h" |
f7d0fe02 | 29 | |
bb572aef | 30 | static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size); |
92dcb59f | 31 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
0e06528e | 32 | int64_t offset, int64_t length, uint64_t addend, |
2aabe7c7 | 33 | bool decrease, enum qcow2_discard_type type); |
f7d0fe02 | 34 | |
59c0cb78 HR |
35 | static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index); |
36 | static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index); | |
37 | static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index); | |
38 | static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index); | |
7453c96b | 39 | static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index); |
59c0cb78 HR |
40 | static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index); |
41 | static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index); | |
7453c96b | 42 | |
59c0cb78 HR |
43 | static void set_refcount_ro0(void *refcount_array, uint64_t index, |
44 | uint64_t value); | |
45 | static void set_refcount_ro1(void *refcount_array, uint64_t index, | |
46 | uint64_t value); | |
47 | static void set_refcount_ro2(void *refcount_array, uint64_t index, | |
48 | uint64_t value); | |
49 | static void set_refcount_ro3(void *refcount_array, uint64_t index, | |
50 | uint64_t value); | |
7453c96b HR |
51 | static void set_refcount_ro4(void *refcount_array, uint64_t index, |
52 | uint64_t value); | |
59c0cb78 HR |
53 | static void set_refcount_ro5(void *refcount_array, uint64_t index, |
54 | uint64_t value); | |
55 | static void set_refcount_ro6(void *refcount_array, uint64_t index, | |
56 | uint64_t value); | |
57 | ||
58 | ||
59 | static Qcow2GetRefcountFunc *const get_refcount_funcs[] = { | |
60 | &get_refcount_ro0, | |
61 | &get_refcount_ro1, | |
62 | &get_refcount_ro2, | |
63 | &get_refcount_ro3, | |
64 | &get_refcount_ro4, | |
65 | &get_refcount_ro5, | |
66 | &get_refcount_ro6 | |
67 | }; | |
68 | ||
69 | static Qcow2SetRefcountFunc *const set_refcount_funcs[] = { | |
70 | &set_refcount_ro0, | |
71 | &set_refcount_ro1, | |
72 | &set_refcount_ro2, | |
73 | &set_refcount_ro3, | |
74 | &set_refcount_ro4, | |
75 | &set_refcount_ro5, | |
76 | &set_refcount_ro6 | |
77 | }; | |
7453c96b | 78 | |
3b88e52b | 79 | |
f7d0fe02 KW |
80 | /*********************************************************/ |
81 | /* refcount handling */ | |
82 | ||
ed6ccf0f | 83 | int qcow2_refcount_init(BlockDriverState *bs) |
f7d0fe02 | 84 | { |
ff99129a | 85 | BDRVQcow2State *s = bs->opaque; |
5dab2fad KW |
86 | unsigned int refcount_table_size2, i; |
87 | int ret; | |
f7d0fe02 | 88 | |
59c0cb78 HR |
89 | assert(s->refcount_order >= 0 && s->refcount_order <= 6); |
90 | ||
91 | s->get_refcount = get_refcount_funcs[s->refcount_order]; | |
92 | s->set_refcount = set_refcount_funcs[s->refcount_order]; | |
7453c96b | 93 | |
5dab2fad | 94 | assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t)); |
f7d0fe02 | 95 | refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t); |
de82815d KW |
96 | s->refcount_table = g_try_malloc(refcount_table_size2); |
97 | ||
f7d0fe02 | 98 | if (s->refcount_table_size > 0) { |
de82815d | 99 | if (s->refcount_table == NULL) { |
8fcffa98 | 100 | ret = -ENOMEM; |
de82815d KW |
101 | goto fail; |
102 | } | |
66f82cee | 103 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD); |
9a4f4c31 | 104 | ret = bdrv_pread(bs->file->bs, s->refcount_table_offset, |
f7d0fe02 | 105 | s->refcount_table, refcount_table_size2); |
8fcffa98 | 106 | if (ret < 0) { |
f7d0fe02 | 107 | goto fail; |
8fcffa98 | 108 | } |
f7d0fe02 KW |
109 | for(i = 0; i < s->refcount_table_size; i++) |
110 | be64_to_cpus(&s->refcount_table[i]); | |
111 | } | |
112 | return 0; | |
113 | fail: | |
8fcffa98 | 114 | return ret; |
f7d0fe02 KW |
115 | } |
116 | ||
ed6ccf0f | 117 | void qcow2_refcount_close(BlockDriverState *bs) |
f7d0fe02 | 118 | { |
ff99129a | 119 | BDRVQcow2State *s = bs->opaque; |
7267c094 | 120 | g_free(s->refcount_table); |
f7d0fe02 KW |
121 | } |
122 | ||
123 | ||
59c0cb78 HR |
124 | static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index) |
125 | { | |
126 | return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1; | |
127 | } | |
128 | ||
129 | static void set_refcount_ro0(void *refcount_array, uint64_t index, | |
130 | uint64_t value) | |
131 | { | |
132 | assert(!(value >> 1)); | |
133 | ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8)); | |
134 | ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8); | |
135 | } | |
136 | ||
137 | static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index) | |
138 | { | |
139 | return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4))) | |
140 | & 0x3; | |
141 | } | |
142 | ||
143 | static void set_refcount_ro1(void *refcount_array, uint64_t index, | |
144 | uint64_t value) | |
145 | { | |
146 | assert(!(value >> 2)); | |
147 | ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4))); | |
148 | ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4)); | |
149 | } | |
150 | ||
151 | static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index) | |
152 | { | |
153 | return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2))) | |
154 | & 0xf; | |
155 | } | |
156 | ||
157 | static void set_refcount_ro2(void *refcount_array, uint64_t index, | |
158 | uint64_t value) | |
159 | { | |
160 | assert(!(value >> 4)); | |
161 | ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2))); | |
162 | ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2)); | |
163 | } | |
164 | ||
165 | static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index) | |
166 | { | |
167 | return ((const uint8_t *)refcount_array)[index]; | |
168 | } | |
169 | ||
170 | static void set_refcount_ro3(void *refcount_array, uint64_t index, | |
171 | uint64_t value) | |
172 | { | |
173 | assert(!(value >> 8)); | |
174 | ((uint8_t *)refcount_array)[index] = value; | |
175 | } | |
176 | ||
7453c96b HR |
177 | static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index) |
178 | { | |
179 | return be16_to_cpu(((const uint16_t *)refcount_array)[index]); | |
180 | } | |
181 | ||
182 | static void set_refcount_ro4(void *refcount_array, uint64_t index, | |
183 | uint64_t value) | |
184 | { | |
185 | assert(!(value >> 16)); | |
186 | ((uint16_t *)refcount_array)[index] = cpu_to_be16(value); | |
187 | } | |
188 | ||
59c0cb78 HR |
189 | static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index) |
190 | { | |
191 | return be32_to_cpu(((const uint32_t *)refcount_array)[index]); | |
192 | } | |
193 | ||
194 | static void set_refcount_ro5(void *refcount_array, uint64_t index, | |
195 | uint64_t value) | |
196 | { | |
197 | assert(!(value >> 32)); | |
198 | ((uint32_t *)refcount_array)[index] = cpu_to_be32(value); | |
199 | } | |
200 | ||
201 | static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index) | |
202 | { | |
203 | return be64_to_cpu(((const uint64_t *)refcount_array)[index]); | |
204 | } | |
205 | ||
206 | static void set_refcount_ro6(void *refcount_array, uint64_t index, | |
207 | uint64_t value) | |
208 | { | |
209 | ((uint64_t *)refcount_array)[index] = cpu_to_be64(value); | |
210 | } | |
211 | ||
7453c96b | 212 | |
f7d0fe02 | 213 | static int load_refcount_block(BlockDriverState *bs, |
29c1a730 KW |
214 | int64_t refcount_block_offset, |
215 | void **refcount_block) | |
f7d0fe02 | 216 | { |
ff99129a | 217 | BDRVQcow2State *s = bs->opaque; |
f7d0fe02 | 218 | int ret; |
3b88e52b | 219 | |
66f82cee | 220 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD); |
29c1a730 KW |
221 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, |
222 | refcount_block); | |
e14e8ba5 | 223 | |
29c1a730 | 224 | return ret; |
f7d0fe02 KW |
225 | } |
226 | ||
018faafd | 227 | /* |
7324c10f HR |
228 | * Retrieves the refcount of the cluster given by its index and stores it in |
229 | * *refcount. Returns 0 on success and -errno on failure. | |
018faafd | 230 | */ |
7324c10f | 231 | int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index, |
0e06528e | 232 | uint64_t *refcount) |
f7d0fe02 | 233 | { |
ff99129a | 234 | BDRVQcow2State *s = bs->opaque; |
db8a31d1 | 235 | uint64_t refcount_table_index, block_index; |
f7d0fe02 | 236 | int64_t refcount_block_offset; |
018faafd | 237 | int ret; |
7453c96b | 238 | void *refcount_block; |
f7d0fe02 | 239 | |
17bd5f47 | 240 | refcount_table_index = cluster_index >> s->refcount_block_bits; |
7324c10f HR |
241 | if (refcount_table_index >= s->refcount_table_size) { |
242 | *refcount = 0; | |
f7d0fe02 | 243 | return 0; |
7324c10f | 244 | } |
26d49c46 HR |
245 | refcount_block_offset = |
246 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; | |
7324c10f HR |
247 | if (!refcount_block_offset) { |
248 | *refcount = 0; | |
f7d0fe02 | 249 | return 0; |
7324c10f | 250 | } |
29c1a730 | 251 | |
a97c67ee HR |
252 | if (offset_into_cluster(s, refcount_block_offset)) { |
253 | qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64 | |
254 | " unaligned (reftable index: %#" PRIx64 ")", | |
255 | refcount_block_offset, refcount_table_index); | |
256 | return -EIO; | |
257 | } | |
258 | ||
29c1a730 | 259 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, |
7453c96b | 260 | &refcount_block); |
29c1a730 KW |
261 | if (ret < 0) { |
262 | return ret; | |
f7d0fe02 | 263 | } |
29c1a730 | 264 | |
17bd5f47 | 265 | block_index = cluster_index & (s->refcount_block_size - 1); |
7453c96b | 266 | *refcount = s->get_refcount(refcount_block, block_index); |
29c1a730 | 267 | |
a3f1afb4 | 268 | qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block); |
29c1a730 | 269 | |
7324c10f | 270 | return 0; |
f7d0fe02 KW |
271 | } |
272 | ||
05121aed KW |
273 | /* |
274 | * Rounds the refcount table size up to avoid growing the table for each single | |
275 | * refcount block that is allocated. | |
276 | */ | |
ff99129a | 277 | static unsigned int next_refcount_table_size(BDRVQcow2State *s, |
05121aed KW |
278 | unsigned int min_size) |
279 | { | |
280 | unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1; | |
281 | unsigned int refcount_table_clusters = | |
282 | MAX(1, s->refcount_table_size >> (s->cluster_bits - 3)); | |
283 | ||
284 | while (min_clusters > refcount_table_clusters) { | |
285 | refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2; | |
286 | } | |
287 | ||
288 | return refcount_table_clusters << (s->cluster_bits - 3); | |
289 | } | |
290 | ||
92dcb59f KW |
291 | |
292 | /* Checks if two offsets are described by the same refcount block */ | |
ff99129a | 293 | static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a, |
92dcb59f KW |
294 | uint64_t offset_b) |
295 | { | |
17bd5f47 HR |
296 | uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits); |
297 | uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits); | |
92dcb59f KW |
298 | |
299 | return (block_a == block_b); | |
300 | } | |
301 | ||
302 | /* | |
303 | * Loads a refcount block. If it doesn't exist yet, it is allocated first | |
304 | * (including growing the refcount table if needed). | |
305 | * | |
29c1a730 | 306 | * Returns 0 on success or -errno in error case |
92dcb59f | 307 | */ |
29c1a730 | 308 | static int alloc_refcount_block(BlockDriverState *bs, |
7453c96b | 309 | int64_t cluster_index, void **refcount_block) |
f7d0fe02 | 310 | { |
ff99129a | 311 | BDRVQcow2State *s = bs->opaque; |
92dcb59f KW |
312 | unsigned int refcount_table_index; |
313 | int ret; | |
314 | ||
66f82cee | 315 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); |
8252278a | 316 | |
92dcb59f | 317 | /* Find the refcount block for the given cluster */ |
17bd5f47 | 318 | refcount_table_index = cluster_index >> s->refcount_block_bits; |
92dcb59f KW |
319 | |
320 | if (refcount_table_index < s->refcount_table_size) { | |
321 | ||
322 | uint64_t refcount_block_offset = | |
76dc9e0c | 323 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; |
92dcb59f KW |
324 | |
325 | /* If it's already there, we're done */ | |
326 | if (refcount_block_offset) { | |
a97c67ee HR |
327 | if (offset_into_cluster(s, refcount_block_offset)) { |
328 | qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" | |
329 | PRIx64 " unaligned (reftable index: " | |
330 | "%#x)", refcount_block_offset, | |
331 | refcount_table_index); | |
332 | return -EIO; | |
333 | } | |
334 | ||
29c1a730 | 335 | return load_refcount_block(bs, refcount_block_offset, |
7453c96b | 336 | refcount_block); |
92dcb59f KW |
337 | } |
338 | } | |
339 | ||
340 | /* | |
341 | * If we came here, we need to allocate something. Something is at least | |
342 | * a cluster for the new refcount block. It may also include a new refcount | |
343 | * table if the old refcount table is too small. | |
344 | * | |
345 | * Note that allocating clusters here needs some special care: | |
346 | * | |
347 | * - We can't use the normal qcow2_alloc_clusters(), it would try to | |
348 | * increase the refcount and very likely we would end up with an endless | |
349 | * recursion. Instead we must place the refcount blocks in a way that | |
350 | * they can describe them themselves. | |
351 | * | |
352 | * - We need to consider that at this point we are inside update_refcounts | |
b106ad91 KW |
353 | * and potentially doing an initial refcount increase. This means that |
354 | * some clusters have already been allocated by the caller, but their | |
355 | * refcount isn't accurate yet. If we allocate clusters for metadata, we | |
356 | * need to return -EAGAIN to signal the caller that it needs to restart | |
357 | * the search for free clusters. | |
92dcb59f KW |
358 | * |
359 | * - alloc_clusters_noref and qcow2_free_clusters may load a different | |
360 | * refcount block into the cache | |
361 | */ | |
362 | ||
29c1a730 KW |
363 | *refcount_block = NULL; |
364 | ||
365 | /* We write to the refcount table, so we might depend on L2 tables */ | |
9991923b SH |
366 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
367 | if (ret < 0) { | |
368 | return ret; | |
369 | } | |
92dcb59f KW |
370 | |
371 | /* Allocate the refcount block itself and mark it as used */ | |
2eaa8f63 KW |
372 | int64_t new_block = alloc_clusters_noref(bs, s->cluster_size); |
373 | if (new_block < 0) { | |
374 | return new_block; | |
375 | } | |
f7d0fe02 | 376 | |
f7d0fe02 | 377 | #ifdef DEBUG_ALLOC2 |
92dcb59f KW |
378 | fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64 |
379 | " at %" PRIx64 "\n", | |
380 | refcount_table_index, cluster_index << s->cluster_bits, new_block); | |
f7d0fe02 | 381 | #endif |
92dcb59f KW |
382 | |
383 | if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) { | |
25408c09 | 384 | /* Zero the new refcount block before updating it */ |
29c1a730 | 385 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
7453c96b | 386 | refcount_block); |
29c1a730 KW |
387 | if (ret < 0) { |
388 | goto fail_block; | |
389 | } | |
390 | ||
391 | memset(*refcount_block, 0, s->cluster_size); | |
25408c09 | 392 | |
92dcb59f KW |
393 | /* The block describes itself, need to update the cache */ |
394 | int block_index = (new_block >> s->cluster_bits) & | |
17bd5f47 | 395 | (s->refcount_block_size - 1); |
7453c96b | 396 | s->set_refcount(*refcount_block, block_index, 1); |
92dcb59f KW |
397 | } else { |
398 | /* Described somewhere else. This can recurse at most twice before we | |
399 | * arrive at a block that describes itself. */ | |
2aabe7c7 | 400 | ret = update_refcount(bs, new_block, s->cluster_size, 1, false, |
6cfcb9b8 | 401 | QCOW2_DISCARD_NEVER); |
92dcb59f KW |
402 | if (ret < 0) { |
403 | goto fail_block; | |
404 | } | |
25408c09 | 405 | |
9991923b SH |
406 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
407 | if (ret < 0) { | |
408 | goto fail_block; | |
409 | } | |
1c4c2814 | 410 | |
25408c09 KW |
411 | /* Initialize the new refcount block only after updating its refcount, |
412 | * update_refcount uses the refcount cache itself */ | |
29c1a730 | 413 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
7453c96b | 414 | refcount_block); |
29c1a730 KW |
415 | if (ret < 0) { |
416 | goto fail_block; | |
417 | } | |
418 | ||
419 | memset(*refcount_block, 0, s->cluster_size); | |
92dcb59f KW |
420 | } |
421 | ||
422 | /* Now the new refcount block needs to be written to disk */ | |
66f82cee | 423 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE); |
72e80b89 | 424 | qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, *refcount_block); |
29c1a730 | 425 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
92dcb59f KW |
426 | if (ret < 0) { |
427 | goto fail_block; | |
428 | } | |
429 | ||
430 | /* If the refcount table is big enough, just hook the block up there */ | |
431 | if (refcount_table_index < s->refcount_table_size) { | |
432 | uint64_t data64 = cpu_to_be64(new_block); | |
66f82cee | 433 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP); |
9a4f4c31 | 434 | ret = bdrv_pwrite_sync(bs->file->bs, |
92dcb59f KW |
435 | s->refcount_table_offset + refcount_table_index * sizeof(uint64_t), |
436 | &data64, sizeof(data64)); | |
437 | if (ret < 0) { | |
438 | goto fail_block; | |
439 | } | |
440 | ||
441 | s->refcount_table[refcount_table_index] = new_block; | |
b106ad91 KW |
442 | |
443 | /* The new refcount block may be where the caller intended to put its | |
444 | * data, so let it restart the search. */ | |
445 | return -EAGAIN; | |
29c1a730 KW |
446 | } |
447 | ||
a3f1afb4 | 448 | qcow2_cache_put(bs, s->refcount_block_cache, refcount_block); |
92dcb59f KW |
449 | |
450 | /* | |
451 | * If we come here, we need to grow the refcount table. Again, a new | |
452 | * refcount table needs some space and we can't simply allocate to avoid | |
453 | * endless recursion. | |
454 | * | |
455 | * Therefore let's grab new refcount blocks at the end of the image, which | |
456 | * will describe themselves and the new refcount table. This way we can | |
457 | * reference them only in the new table and do the switch to the new | |
458 | * refcount table at once without producing an inconsistent state in | |
459 | * between. | |
460 | */ | |
66f82cee | 461 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW); |
8252278a | 462 | |
14a58a4e HR |
463 | /* Calculate the number of refcount blocks needed so far; this will be the |
464 | * basis for calculating the index of the first cluster used for the | |
465 | * self-describing refcount structures which we are about to create. | |
466 | * | |
467 | * Because we reached this point, there cannot be any refcount entries for | |
468 | * cluster_index or higher indices yet. However, because new_block has been | |
469 | * allocated to describe that cluster (and it will assume this role later | |
470 | * on), we cannot use that index; also, new_block may actually have a higher | |
471 | * cluster index than cluster_index, so it needs to be taken into account | |
472 | * here (and 1 needs to be added to its value because that cluster is used). | |
473 | */ | |
474 | uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1, | |
475 | (new_block >> s->cluster_bits) + 1), | |
476 | s->refcount_block_size); | |
92dcb59f | 477 | |
2b5d5953 KW |
478 | if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) { |
479 | return -EFBIG; | |
480 | } | |
481 | ||
92dcb59f KW |
482 | /* And now we need at least one block more for the new metadata */ |
483 | uint64_t table_size = next_refcount_table_size(s, blocks_used + 1); | |
484 | uint64_t last_table_size; | |
485 | uint64_t blocks_clusters; | |
486 | do { | |
a3548077 KW |
487 | uint64_t table_clusters = |
488 | size_to_clusters(s, table_size * sizeof(uint64_t)); | |
92dcb59f | 489 | blocks_clusters = 1 + |
17bd5f47 HR |
490 | ((table_clusters + s->refcount_block_size - 1) |
491 | / s->refcount_block_size); | |
92dcb59f KW |
492 | uint64_t meta_clusters = table_clusters + blocks_clusters; |
493 | ||
494 | last_table_size = table_size; | |
495 | table_size = next_refcount_table_size(s, blocks_used + | |
17bd5f47 HR |
496 | ((meta_clusters + s->refcount_block_size - 1) |
497 | / s->refcount_block_size)); | |
92dcb59f KW |
498 | |
499 | } while (last_table_size != table_size); | |
500 | ||
501 | #ifdef DEBUG_ALLOC2 | |
502 | fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n", | |
503 | s->refcount_table_size, table_size); | |
504 | #endif | |
505 | ||
506 | /* Create the new refcount table and blocks */ | |
17bd5f47 | 507 | uint64_t meta_offset = (blocks_used * s->refcount_block_size) * |
92dcb59f KW |
508 | s->cluster_size; |
509 | uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size; | |
5839e53b | 510 | uint64_t *new_table = g_try_new0(uint64_t, table_size); |
7453c96b | 511 | void *new_blocks = g_try_malloc0(blocks_clusters * s->cluster_size); |
de82815d KW |
512 | |
513 | assert(table_size > 0 && blocks_clusters > 0); | |
514 | if (new_table == NULL || new_blocks == NULL) { | |
515 | ret = -ENOMEM; | |
516 | goto fail_table; | |
517 | } | |
92dcb59f | 518 | |
92dcb59f | 519 | /* Fill the new refcount table */ |
f7d0fe02 | 520 | memcpy(new_table, s->refcount_table, |
92dcb59f KW |
521 | s->refcount_table_size * sizeof(uint64_t)); |
522 | new_table[refcount_table_index] = new_block; | |
523 | ||
524 | int i; | |
525 | for (i = 0; i < blocks_clusters; i++) { | |
526 | new_table[blocks_used + i] = meta_offset + (i * s->cluster_size); | |
527 | } | |
528 | ||
529 | /* Fill the refcount blocks */ | |
530 | uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t)); | |
531 | int block = 0; | |
532 | for (i = 0; i < table_clusters + blocks_clusters; i++) { | |
7453c96b | 533 | s->set_refcount(new_blocks, block++, 1); |
92dcb59f KW |
534 | } |
535 | ||
536 | /* Write refcount blocks to disk */ | |
66f82cee | 537 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS); |
9a4f4c31 | 538 | ret = bdrv_pwrite_sync(bs->file->bs, meta_offset, new_blocks, |
92dcb59f | 539 | blocks_clusters * s->cluster_size); |
7267c094 | 540 | g_free(new_blocks); |
39ba3bf6 | 541 | new_blocks = NULL; |
92dcb59f KW |
542 | if (ret < 0) { |
543 | goto fail_table; | |
544 | } | |
545 | ||
546 | /* Write refcount table to disk */ | |
547 | for(i = 0; i < table_size; i++) { | |
548 | cpu_to_be64s(&new_table[i]); | |
549 | } | |
550 | ||
66f82cee | 551 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE); |
9a4f4c31 | 552 | ret = bdrv_pwrite_sync(bs->file->bs, table_offset, new_table, |
92dcb59f KW |
553 | table_size * sizeof(uint64_t)); |
554 | if (ret < 0) { | |
555 | goto fail_table; | |
556 | } | |
557 | ||
558 | for(i = 0; i < table_size; i++) { | |
87267753 | 559 | be64_to_cpus(&new_table[i]); |
92dcb59f | 560 | } |
f7d0fe02 | 561 | |
92dcb59f | 562 | /* Hook up the new refcount table in the qcow2 header */ |
95334230 JS |
563 | struct QEMU_PACKED { |
564 | uint64_t d64; | |
565 | uint32_t d32; | |
566 | } data; | |
567 | cpu_to_be64w(&data.d64, table_offset); | |
568 | cpu_to_be32w(&data.d32, table_clusters); | |
66f82cee | 569 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE); |
9a4f4c31 KW |
570 | ret = bdrv_pwrite_sync(bs->file->bs, |
571 | offsetof(QCowHeader, refcount_table_offset), | |
95334230 | 572 | &data, sizeof(data)); |
92dcb59f KW |
573 | if (ret < 0) { |
574 | goto fail_table; | |
f2b7c8b3 KW |
575 | } |
576 | ||
92dcb59f KW |
577 | /* And switch it in memory */ |
578 | uint64_t old_table_offset = s->refcount_table_offset; | |
579 | uint64_t old_table_size = s->refcount_table_size; | |
580 | ||
7267c094 | 581 | g_free(s->refcount_table); |
f7d0fe02 | 582 | s->refcount_table = new_table; |
92dcb59f | 583 | s->refcount_table_size = table_size; |
f7d0fe02 KW |
584 | s->refcount_table_offset = table_offset; |
585 | ||
b106ad91 | 586 | /* Free old table. */ |
6cfcb9b8 KW |
587 | qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t), |
588 | QCOW2_DISCARD_OTHER); | |
f7d0fe02 | 589 | |
7453c96b | 590 | ret = load_refcount_block(bs, new_block, refcount_block); |
92dcb59f | 591 | if (ret < 0) { |
29c1a730 | 592 | return ret; |
f7d0fe02 KW |
593 | } |
594 | ||
b106ad91 KW |
595 | /* If we were trying to do the initial refcount update for some cluster |
596 | * allocation, we might have used the same clusters to store newly | |
597 | * allocated metadata. Make the caller search some new space. */ | |
598 | return -EAGAIN; | |
f7d0fe02 | 599 | |
92dcb59f | 600 | fail_table: |
de82815d | 601 | g_free(new_blocks); |
7267c094 | 602 | g_free(new_table); |
92dcb59f | 603 | fail_block: |
29c1a730 | 604 | if (*refcount_block != NULL) { |
7453c96b | 605 | qcow2_cache_put(bs, s->refcount_block_cache, refcount_block); |
3b88e52b | 606 | } |
29c1a730 | 607 | return ret; |
9923e05e KW |
608 | } |
609 | ||
0b919fae KW |
610 | void qcow2_process_discards(BlockDriverState *bs, int ret) |
611 | { | |
ff99129a | 612 | BDRVQcow2State *s = bs->opaque; |
0b919fae KW |
613 | Qcow2DiscardRegion *d, *next; |
614 | ||
615 | QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) { | |
616 | QTAILQ_REMOVE(&s->discards, d, next); | |
617 | ||
618 | /* Discard is optional, ignore the return value */ | |
619 | if (ret >= 0) { | |
9a4f4c31 | 620 | bdrv_discard(bs->file->bs, |
0b919fae KW |
621 | d->offset >> BDRV_SECTOR_BITS, |
622 | d->bytes >> BDRV_SECTOR_BITS); | |
623 | } | |
624 | ||
625 | g_free(d); | |
626 | } | |
627 | } | |
628 | ||
629 | static void update_refcount_discard(BlockDriverState *bs, | |
630 | uint64_t offset, uint64_t length) | |
631 | { | |
ff99129a | 632 | BDRVQcow2State *s = bs->opaque; |
0b919fae KW |
633 | Qcow2DiscardRegion *d, *p, *next; |
634 | ||
635 | QTAILQ_FOREACH(d, &s->discards, next) { | |
636 | uint64_t new_start = MIN(offset, d->offset); | |
637 | uint64_t new_end = MAX(offset + length, d->offset + d->bytes); | |
638 | ||
639 | if (new_end - new_start <= length + d->bytes) { | |
640 | /* There can't be any overlap, areas ending up here have no | |
641 | * references any more and therefore shouldn't get freed another | |
642 | * time. */ | |
643 | assert(d->bytes + length == new_end - new_start); | |
644 | d->offset = new_start; | |
645 | d->bytes = new_end - new_start; | |
646 | goto found; | |
647 | } | |
648 | } | |
649 | ||
650 | d = g_malloc(sizeof(*d)); | |
651 | *d = (Qcow2DiscardRegion) { | |
652 | .bs = bs, | |
653 | .offset = offset, | |
654 | .bytes = length, | |
655 | }; | |
656 | QTAILQ_INSERT_TAIL(&s->discards, d, next); | |
657 | ||
658 | found: | |
659 | /* Merge discard requests if they are adjacent now */ | |
660 | QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) { | |
661 | if (p == d | |
662 | || p->offset > d->offset + d->bytes | |
663 | || d->offset > p->offset + p->bytes) | |
664 | { | |
665 | continue; | |
666 | } | |
667 | ||
668 | /* Still no overlap possible */ | |
669 | assert(p->offset == d->offset + d->bytes | |
670 | || d->offset == p->offset + p->bytes); | |
671 | ||
672 | QTAILQ_REMOVE(&s->discards, p, next); | |
673 | d->offset = MIN(d->offset, p->offset); | |
674 | d->bytes += p->bytes; | |
d8bb71b6 | 675 | g_free(p); |
0b919fae KW |
676 | } |
677 | } | |
678 | ||
f7d0fe02 | 679 | /* XXX: cache several refcount block clusters ? */ |
2aabe7c7 HR |
680 | /* @addend is the absolute value of the addend; if @decrease is set, @addend |
681 | * will be subtracted from the current refcount, otherwise it will be added */ | |
db3a964f | 682 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
2aabe7c7 HR |
683 | int64_t offset, |
684 | int64_t length, | |
0e06528e | 685 | uint64_t addend, |
2aabe7c7 HR |
686 | bool decrease, |
687 | enum qcow2_discard_type type) | |
f7d0fe02 | 688 | { |
ff99129a | 689 | BDRVQcow2State *s = bs->opaque; |
f7d0fe02 | 690 | int64_t start, last, cluster_offset; |
7453c96b | 691 | void *refcount_block = NULL; |
29c1a730 | 692 | int64_t old_table_index = -1; |
09508d13 | 693 | int ret; |
f7d0fe02 KW |
694 | |
695 | #ifdef DEBUG_ALLOC2 | |
2aabe7c7 | 696 | fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64 |
0e06528e | 697 | " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "", |
2aabe7c7 | 698 | addend); |
f7d0fe02 | 699 | #endif |
7322afe7 | 700 | if (length < 0) { |
f7d0fe02 | 701 | return -EINVAL; |
7322afe7 KW |
702 | } else if (length == 0) { |
703 | return 0; | |
704 | } | |
705 | ||
2aabe7c7 | 706 | if (decrease) { |
29c1a730 KW |
707 | qcow2_cache_set_dependency(bs, s->refcount_block_cache, |
708 | s->l2_table_cache); | |
709 | } | |
710 | ||
ac95acdb HT |
711 | start = start_of_cluster(s, offset); |
712 | last = start_of_cluster(s, offset + length - 1); | |
f7d0fe02 KW |
713 | for(cluster_offset = start; cluster_offset <= last; |
714 | cluster_offset += s->cluster_size) | |
715 | { | |
2aabe7c7 | 716 | int block_index; |
0e06528e | 717 | uint64_t refcount; |
f7d0fe02 | 718 | int64_t cluster_index = cluster_offset >> s->cluster_bits; |
17bd5f47 | 719 | int64_t table_index = cluster_index >> s->refcount_block_bits; |
f7d0fe02 | 720 | |
29c1a730 KW |
721 | /* Load the refcount block and allocate it if needed */ |
722 | if (table_index != old_table_index) { | |
723 | if (refcount_block) { | |
a3f1afb4 | 724 | qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block); |
29c1a730 | 725 | } |
29c1a730 | 726 | ret = alloc_refcount_block(bs, cluster_index, &refcount_block); |
ed0df867 | 727 | if (ret < 0) { |
29c1a730 | 728 | goto fail; |
f7d0fe02 | 729 | } |
f7d0fe02 | 730 | } |
29c1a730 | 731 | old_table_index = table_index; |
f7d0fe02 | 732 | |
72e80b89 AG |
733 | qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, |
734 | refcount_block); | |
f7d0fe02 KW |
735 | |
736 | /* we can update the count and save it */ | |
17bd5f47 | 737 | block_index = cluster_index & (s->refcount_block_size - 1); |
f7d0fe02 | 738 | |
7453c96b | 739 | refcount = s->get_refcount(refcount_block, block_index); |
0e06528e HR |
740 | if (decrease ? (refcount - addend > refcount) |
741 | : (refcount + addend < refcount || | |
742 | refcount + addend > s->refcount_max)) | |
2aabe7c7 | 743 | { |
09508d13 KW |
744 | ret = -EINVAL; |
745 | goto fail; | |
746 | } | |
2aabe7c7 HR |
747 | if (decrease) { |
748 | refcount -= addend; | |
749 | } else { | |
750 | refcount += addend; | |
751 | } | |
f7d0fe02 KW |
752 | if (refcount == 0 && cluster_index < s->free_cluster_index) { |
753 | s->free_cluster_index = cluster_index; | |
754 | } | |
7453c96b | 755 | s->set_refcount(refcount_block, block_index, refcount); |
0b919fae | 756 | |
67af674e | 757 | if (refcount == 0 && s->discard_passthrough[type]) { |
0b919fae | 758 | update_refcount_discard(bs, cluster_offset, s->cluster_size); |
67af674e | 759 | } |
f7d0fe02 KW |
760 | } |
761 | ||
09508d13 KW |
762 | ret = 0; |
763 | fail: | |
0b919fae KW |
764 | if (!s->cache_discards) { |
765 | qcow2_process_discards(bs, ret); | |
766 | } | |
767 | ||
f7d0fe02 | 768 | /* Write last changed block to disk */ |
29c1a730 | 769 | if (refcount_block) { |
a3f1afb4 | 770 | qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block); |
f7d0fe02 KW |
771 | } |
772 | ||
09508d13 KW |
773 | /* |
774 | * Try do undo any updates if an error is returned (This may succeed in | |
775 | * some cases like ENOSPC for allocating a new refcount block) | |
776 | */ | |
777 | if (ret < 0) { | |
778 | int dummy; | |
2aabe7c7 HR |
779 | dummy = update_refcount(bs, offset, cluster_offset - offset, addend, |
780 | !decrease, QCOW2_DISCARD_NEVER); | |
83e3f76c | 781 | (void)dummy; |
09508d13 KW |
782 | } |
783 | ||
784 | return ret; | |
f7d0fe02 KW |
785 | } |
786 | ||
018faafd | 787 | /* |
44751917 | 788 | * Increases or decreases the refcount of a given cluster. |
018faafd | 789 | * |
2aabe7c7 HR |
790 | * @addend is the absolute value of the addend; if @decrease is set, @addend |
791 | * will be subtracted from the current refcount, otherwise it will be added. | |
792 | * | |
c6e9d8ae | 793 | * On success 0 is returned; on failure -errno is returned. |
018faafd | 794 | */ |
32b6444d HR |
795 | int qcow2_update_cluster_refcount(BlockDriverState *bs, |
796 | int64_t cluster_index, | |
0e06528e | 797 | uint64_t addend, bool decrease, |
32b6444d | 798 | enum qcow2_discard_type type) |
f7d0fe02 | 799 | { |
ff99129a | 800 | BDRVQcow2State *s = bs->opaque; |
f7d0fe02 KW |
801 | int ret; |
802 | ||
6cfcb9b8 | 803 | ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend, |
2aabe7c7 | 804 | decrease, type); |
f7d0fe02 KW |
805 | if (ret < 0) { |
806 | return ret; | |
807 | } | |
808 | ||
c6e9d8ae | 809 | return 0; |
f7d0fe02 KW |
810 | } |
811 | ||
812 | ||
813 | ||
814 | /*********************************************************/ | |
815 | /* cluster allocation functions */ | |
816 | ||
817 | ||
818 | ||
819 | /* return < 0 if error */ | |
bb572aef | 820 | static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size) |
f7d0fe02 | 821 | { |
ff99129a | 822 | BDRVQcow2State *s = bs->opaque; |
0e06528e | 823 | uint64_t i, nb_clusters, refcount; |
7324c10f | 824 | int ret; |
f7d0fe02 | 825 | |
ecbda7a2 KW |
826 | /* We can't allocate clusters if they may still be queued for discard. */ |
827 | if (s->cache_discards) { | |
828 | qcow2_process_discards(bs, 0); | |
829 | } | |
830 | ||
f7d0fe02 KW |
831 | nb_clusters = size_to_clusters(s, size); |
832 | retry: | |
833 | for(i = 0; i < nb_clusters; i++) { | |
bb572aef | 834 | uint64_t next_cluster_index = s->free_cluster_index++; |
7324c10f | 835 | ret = qcow2_get_refcount(bs, next_cluster_index, &refcount); |
2eaa8f63 | 836 | |
7324c10f HR |
837 | if (ret < 0) { |
838 | return ret; | |
2eaa8f63 | 839 | } else if (refcount != 0) { |
f7d0fe02 | 840 | goto retry; |
2eaa8f63 | 841 | } |
f7d0fe02 | 842 | } |
91f827dc HR |
843 | |
844 | /* Make sure that all offsets in the "allocated" range are representable | |
845 | * in an int64_t */ | |
65f33bc0 HR |
846 | if (s->free_cluster_index > 0 && |
847 | s->free_cluster_index - 1 > (INT64_MAX >> s->cluster_bits)) | |
848 | { | |
91f827dc HR |
849 | return -EFBIG; |
850 | } | |
851 | ||
f7d0fe02 | 852 | #ifdef DEBUG_ALLOC2 |
35ee5e39 | 853 | fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n", |
f7d0fe02 KW |
854 | size, |
855 | (s->free_cluster_index - nb_clusters) << s->cluster_bits); | |
856 | #endif | |
857 | return (s->free_cluster_index - nb_clusters) << s->cluster_bits; | |
858 | } | |
859 | ||
bb572aef | 860 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size) |
f7d0fe02 KW |
861 | { |
862 | int64_t offset; | |
db3a964f | 863 | int ret; |
f7d0fe02 | 864 | |
66f82cee | 865 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC); |
b106ad91 KW |
866 | do { |
867 | offset = alloc_clusters_noref(bs, size); | |
868 | if (offset < 0) { | |
869 | return offset; | |
870 | } | |
871 | ||
2aabe7c7 | 872 | ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER); |
b106ad91 | 873 | } while (ret == -EAGAIN); |
2eaa8f63 | 874 | |
db3a964f KW |
875 | if (ret < 0) { |
876 | return ret; | |
877 | } | |
1c4c2814 | 878 | |
f7d0fe02 KW |
879 | return offset; |
880 | } | |
881 | ||
b6d36def HR |
882 | int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
883 | int64_t nb_clusters) | |
256900b1 | 884 | { |
ff99129a | 885 | BDRVQcow2State *s = bs->opaque; |
0e06528e | 886 | uint64_t cluster_index, refcount; |
33304ec9 | 887 | uint64_t i; |
7324c10f | 888 | int ret; |
33304ec9 HT |
889 | |
890 | assert(nb_clusters >= 0); | |
891 | if (nb_clusters == 0) { | |
892 | return 0; | |
893 | } | |
256900b1 | 894 | |
b106ad91 KW |
895 | do { |
896 | /* Check how many clusters there are free */ | |
897 | cluster_index = offset >> s->cluster_bits; | |
898 | for(i = 0; i < nb_clusters; i++) { | |
7324c10f HR |
899 | ret = qcow2_get_refcount(bs, cluster_index++, &refcount); |
900 | if (ret < 0) { | |
901 | return ret; | |
b106ad91 KW |
902 | } else if (refcount != 0) { |
903 | break; | |
904 | } | |
256900b1 | 905 | } |
256900b1 | 906 | |
b106ad91 | 907 | /* And then allocate them */ |
2aabe7c7 | 908 | ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false, |
b106ad91 KW |
909 | QCOW2_DISCARD_NEVER); |
910 | } while (ret == -EAGAIN); | |
f24423bd | 911 | |
256900b1 KW |
912 | if (ret < 0) { |
913 | return ret; | |
914 | } | |
915 | ||
916 | return i; | |
917 | } | |
918 | ||
f7d0fe02 KW |
919 | /* only used to allocate compressed sectors. We try to allocate |
920 | contiguous sectors. size must be <= cluster_size */ | |
ed6ccf0f | 921 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size) |
f7d0fe02 | 922 | { |
ff99129a | 923 | BDRVQcow2State *s = bs->opaque; |
8c44dfbc HR |
924 | int64_t offset; |
925 | size_t free_in_cluster; | |
926 | int ret; | |
f7d0fe02 | 927 | |
66f82cee | 928 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES); |
f7d0fe02 | 929 | assert(size > 0 && size <= s->cluster_size); |
8c44dfbc HR |
930 | assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset)); |
931 | ||
932 | offset = s->free_byte_offset; | |
933 | ||
934 | if (offset) { | |
0e06528e | 935 | uint64_t refcount; |
7324c10f HR |
936 | ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount); |
937 | if (ret < 0) { | |
938 | return ret; | |
5d757b56 | 939 | } |
8c44dfbc | 940 | |
346a53df | 941 | if (refcount == s->refcount_max) { |
8c44dfbc | 942 | offset = 0; |
5d757b56 | 943 | } |
8c44dfbc HR |
944 | } |
945 | ||
946 | free_in_cluster = s->cluster_size - offset_into_cluster(s, offset); | |
3e5feb62 JM |
947 | do { |
948 | if (!offset || free_in_cluster < size) { | |
949 | int64_t new_cluster = alloc_clusters_noref(bs, s->cluster_size); | |
950 | if (new_cluster < 0) { | |
951 | return new_cluster; | |
952 | } | |
8c44dfbc | 953 | |
3e5feb62 JM |
954 | if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) { |
955 | offset = new_cluster; | |
2ac01520 HR |
956 | free_in_cluster = s->cluster_size; |
957 | } else { | |
958 | free_in_cluster += s->cluster_size; | |
3e5feb62 | 959 | } |
f7d0fe02 | 960 | } |
29216ed1 | 961 | |
3e5feb62 JM |
962 | assert(offset); |
963 | ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER); | |
2ac01520 HR |
964 | if (ret < 0) { |
965 | offset = 0; | |
966 | } | |
3e5feb62 | 967 | } while (ret == -EAGAIN); |
8c44dfbc HR |
968 | if (ret < 0) { |
969 | return ret; | |
970 | } | |
971 | ||
972 | /* The cluster refcount was incremented; refcount blocks must be flushed | |
973 | * before the caller's L2 table updates. */ | |
c1f5bafd | 974 | qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); |
8c44dfbc HR |
975 | |
976 | s->free_byte_offset = offset + size; | |
977 | if (!offset_into_cluster(s, s->free_byte_offset)) { | |
978 | s->free_byte_offset = 0; | |
979 | } | |
980 | ||
f7d0fe02 KW |
981 | return offset; |
982 | } | |
983 | ||
ed6ccf0f | 984 | void qcow2_free_clusters(BlockDriverState *bs, |
6cfcb9b8 KW |
985 | int64_t offset, int64_t size, |
986 | enum qcow2_discard_type type) | |
f7d0fe02 | 987 | { |
db3a964f KW |
988 | int ret; |
989 | ||
66f82cee | 990 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE); |
2aabe7c7 | 991 | ret = update_refcount(bs, offset, size, 1, true, type); |
db3a964f KW |
992 | if (ret < 0) { |
993 | fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret)); | |
003fad6e | 994 | /* TODO Remember the clusters to free them later and avoid leaking */ |
db3a964f | 995 | } |
f7d0fe02 KW |
996 | } |
997 | ||
45aba42f | 998 | /* |
c7a4c37a KW |
999 | * Free a cluster using its L2 entry (handles clusters of all types, e.g. |
1000 | * normal cluster, compressed cluster, etc.) | |
45aba42f | 1001 | */ |
6cfcb9b8 KW |
1002 | void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry, |
1003 | int nb_clusters, enum qcow2_discard_type type) | |
45aba42f | 1004 | { |
ff99129a | 1005 | BDRVQcow2State *s = bs->opaque; |
45aba42f | 1006 | |
c7a4c37a KW |
1007 | switch (qcow2_get_cluster_type(l2_entry)) { |
1008 | case QCOW2_CLUSTER_COMPRESSED: | |
1009 | { | |
1010 | int nb_csectors; | |
1011 | nb_csectors = ((l2_entry >> s->csize_shift) & | |
1012 | s->csize_mask) + 1; | |
1013 | qcow2_free_clusters(bs, | |
1014 | (l2_entry & s->cluster_offset_mask) & ~511, | |
6cfcb9b8 | 1015 | nb_csectors * 512, type); |
c7a4c37a KW |
1016 | } |
1017 | break; | |
1018 | case QCOW2_CLUSTER_NORMAL: | |
8f730dd2 HR |
1019 | case QCOW2_CLUSTER_ZERO: |
1020 | if (l2_entry & L2E_OFFSET_MASK) { | |
a97c67ee HR |
1021 | if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) { |
1022 | qcow2_signal_corruption(bs, false, -1, -1, | |
1023 | "Cannot free unaligned cluster %#llx", | |
1024 | l2_entry & L2E_OFFSET_MASK); | |
1025 | } else { | |
1026 | qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK, | |
1027 | nb_clusters << s->cluster_bits, type); | |
1028 | } | |
8f730dd2 | 1029 | } |
c7a4c37a KW |
1030 | break; |
1031 | case QCOW2_CLUSTER_UNALLOCATED: | |
1032 | break; | |
1033 | default: | |
1034 | abort(); | |
45aba42f | 1035 | } |
45aba42f KW |
1036 | } |
1037 | ||
f7d0fe02 KW |
1038 | |
1039 | ||
1040 | /*********************************************************/ | |
1041 | /* snapshots and image creation */ | |
1042 | ||
1043 | ||
1044 | ||
f7d0fe02 | 1045 | /* update the refcounts of snapshots and the copied flag */ |
ed6ccf0f KW |
1046 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
1047 | int64_t l1_table_offset, int l1_size, int addend) | |
f7d0fe02 | 1048 | { |
ff99129a | 1049 | BDRVQcow2State *s = bs->opaque; |
0e06528e | 1050 | uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, refcount; |
de82815d | 1051 | bool l1_allocated = false; |
f7d0fe02 | 1052 | int64_t old_offset, old_l2_offset; |
7324c10f | 1053 | int i, j, l1_modified = 0, nb_csectors; |
29c1a730 | 1054 | int ret; |
f7d0fe02 | 1055 | |
2aabe7c7 HR |
1056 | assert(addend >= -1 && addend <= 1); |
1057 | ||
f7d0fe02 KW |
1058 | l2_table = NULL; |
1059 | l1_table = NULL; | |
1060 | l1_size2 = l1_size * sizeof(uint64_t); | |
43a0cac4 | 1061 | |
0b919fae KW |
1062 | s->cache_discards = true; |
1063 | ||
43a0cac4 KW |
1064 | /* WARNING: qcow2_snapshot_goto relies on this function not using the |
1065 | * l1_table_offset when it is the current s->l1_table_offset! Be careful | |
1066 | * when changing this! */ | |
f7d0fe02 | 1067 | if (l1_table_offset != s->l1_table_offset) { |
de82815d KW |
1068 | l1_table = g_try_malloc0(align_offset(l1_size2, 512)); |
1069 | if (l1_size2 && l1_table == NULL) { | |
1070 | ret = -ENOMEM; | |
1071 | goto fail; | |
1072 | } | |
1073 | l1_allocated = true; | |
c2bc78b6 | 1074 | |
9a4f4c31 | 1075 | ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2); |
c2bc78b6 | 1076 | if (ret < 0) { |
f7d0fe02 | 1077 | goto fail; |
93913dfd KW |
1078 | } |
1079 | ||
f7d0fe02 KW |
1080 | for(i = 0;i < l1_size; i++) |
1081 | be64_to_cpus(&l1_table[i]); | |
1082 | } else { | |
1083 | assert(l1_size == s->l1_size); | |
1084 | l1_table = s->l1_table; | |
de82815d | 1085 | l1_allocated = false; |
f7d0fe02 KW |
1086 | } |
1087 | ||
f7d0fe02 KW |
1088 | for(i = 0; i < l1_size; i++) { |
1089 | l2_offset = l1_table[i]; | |
1090 | if (l2_offset) { | |
1091 | old_l2_offset = l2_offset; | |
8e37f681 | 1092 | l2_offset &= L1E_OFFSET_MASK; |
29c1a730 | 1093 | |
a97c67ee HR |
1094 | if (offset_into_cluster(s, l2_offset)) { |
1095 | qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" | |
1096 | PRIx64 " unaligned (L1 index: %#x)", | |
1097 | l2_offset, i); | |
1098 | ret = -EIO; | |
1099 | goto fail; | |
1100 | } | |
1101 | ||
29c1a730 KW |
1102 | ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, |
1103 | (void**) &l2_table); | |
1104 | if (ret < 0) { | |
f7d0fe02 | 1105 | goto fail; |
29c1a730 KW |
1106 | } |
1107 | ||
f7d0fe02 | 1108 | for(j = 0; j < s->l2_size; j++) { |
8b81a7b6 HR |
1109 | uint64_t cluster_index; |
1110 | ||
f7d0fe02 | 1111 | offset = be64_to_cpu(l2_table[j]); |
8b81a7b6 HR |
1112 | old_offset = offset; |
1113 | offset &= ~QCOW_OFLAG_COPIED; | |
1114 | ||
1115 | switch (qcow2_get_cluster_type(offset)) { | |
1116 | case QCOW2_CLUSTER_COMPRESSED: | |
f7d0fe02 KW |
1117 | nb_csectors = ((offset >> s->csize_shift) & |
1118 | s->csize_mask) + 1; | |
db3a964f | 1119 | if (addend != 0) { |
db3a964f KW |
1120 | ret = update_refcount(bs, |
1121 | (offset & s->cluster_offset_mask) & ~511, | |
2aabe7c7 | 1122 | nb_csectors * 512, abs(addend), addend < 0, |
6cfcb9b8 | 1123 | QCOW2_DISCARD_SNAPSHOT); |
db3a964f KW |
1124 | if (ret < 0) { |
1125 | goto fail; | |
1126 | } | |
1127 | } | |
f7d0fe02 KW |
1128 | /* compressed clusters are never modified */ |
1129 | refcount = 2; | |
8b81a7b6 HR |
1130 | break; |
1131 | ||
1132 | case QCOW2_CLUSTER_NORMAL: | |
1133 | case QCOW2_CLUSTER_ZERO: | |
a97c67ee HR |
1134 | if (offset_into_cluster(s, offset & L2E_OFFSET_MASK)) { |
1135 | qcow2_signal_corruption(bs, true, -1, -1, "Data " | |
1136 | "cluster offset %#llx " | |
1137 | "unaligned (L2 offset: %#" | |
1138 | PRIx64 ", L2 index: %#x)", | |
1139 | offset & L2E_OFFSET_MASK, | |
1140 | l2_offset, j); | |
1141 | ret = -EIO; | |
1142 | goto fail; | |
1143 | } | |
1144 | ||
8b81a7b6 HR |
1145 | cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits; |
1146 | if (!cluster_index) { | |
1147 | /* unallocated */ | |
1148 | refcount = 0; | |
1149 | break; | |
1150 | } | |
f7d0fe02 | 1151 | if (addend != 0) { |
c6e9d8ae | 1152 | ret = qcow2_update_cluster_refcount(bs, |
2aabe7c7 | 1153 | cluster_index, abs(addend), addend < 0, |
32b6444d | 1154 | QCOW2_DISCARD_SNAPSHOT); |
c6e9d8ae HR |
1155 | if (ret < 0) { |
1156 | goto fail; | |
1157 | } | |
f7d0fe02 | 1158 | } |
018faafd | 1159 | |
7324c10f HR |
1160 | ret = qcow2_get_refcount(bs, cluster_index, &refcount); |
1161 | if (ret < 0) { | |
018faafd KW |
1162 | goto fail; |
1163 | } | |
8b81a7b6 | 1164 | break; |
f7d0fe02 | 1165 | |
8b81a7b6 HR |
1166 | case QCOW2_CLUSTER_UNALLOCATED: |
1167 | refcount = 0; | |
1168 | break; | |
1169 | ||
1170 | default: | |
1171 | abort(); | |
1172 | } | |
1173 | ||
1174 | if (refcount == 1) { | |
1175 | offset |= QCOW_OFLAG_COPIED; | |
1176 | } | |
1177 | if (offset != old_offset) { | |
1178 | if (addend > 0) { | |
1179 | qcow2_cache_set_dependency(bs, s->l2_table_cache, | |
1180 | s->refcount_block_cache); | |
f7d0fe02 | 1181 | } |
8b81a7b6 | 1182 | l2_table[j] = cpu_to_be64(offset); |
72e80b89 AG |
1183 | qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, |
1184 | l2_table); | |
f7d0fe02 KW |
1185 | } |
1186 | } | |
29c1a730 | 1187 | |
a3f1afb4 | 1188 | qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table); |
29c1a730 | 1189 | |
f7d0fe02 | 1190 | if (addend != 0) { |
c6e9d8ae HR |
1191 | ret = qcow2_update_cluster_refcount(bs, l2_offset >> |
1192 | s->cluster_bits, | |
2aabe7c7 | 1193 | abs(addend), addend < 0, |
c6e9d8ae HR |
1194 | QCOW2_DISCARD_SNAPSHOT); |
1195 | if (ret < 0) { | |
1196 | goto fail; | |
1197 | } | |
f7d0fe02 | 1198 | } |
7324c10f HR |
1199 | ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits, |
1200 | &refcount); | |
1201 | if (ret < 0) { | |
018faafd KW |
1202 | goto fail; |
1203 | } else if (refcount == 1) { | |
f7d0fe02 KW |
1204 | l2_offset |= QCOW_OFLAG_COPIED; |
1205 | } | |
1206 | if (l2_offset != old_l2_offset) { | |
1207 | l1_table[i] = l2_offset; | |
1208 | l1_modified = 1; | |
1209 | } | |
1210 | } | |
1211 | } | |
93913dfd | 1212 | |
2154f24e | 1213 | ret = bdrv_flush(bs); |
93913dfd KW |
1214 | fail: |
1215 | if (l2_table) { | |
1216 | qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); | |
1217 | } | |
1218 | ||
0b919fae KW |
1219 | s->cache_discards = false; |
1220 | qcow2_process_discards(bs, ret); | |
1221 | ||
43a0cac4 | 1222 | /* Update L1 only if it isn't deleted anyway (addend = -1) */ |
c2b6ff51 KW |
1223 | if (ret == 0 && addend >= 0 && l1_modified) { |
1224 | for (i = 0; i < l1_size; i++) { | |
f7d0fe02 | 1225 | cpu_to_be64s(&l1_table[i]); |
c2b6ff51 KW |
1226 | } |
1227 | ||
9a4f4c31 KW |
1228 | ret = bdrv_pwrite_sync(bs->file->bs, l1_table_offset, |
1229 | l1_table, l1_size2); | |
c2b6ff51 KW |
1230 | |
1231 | for (i = 0; i < l1_size; i++) { | |
f7d0fe02 | 1232 | be64_to_cpus(&l1_table[i]); |
c2b6ff51 | 1233 | } |
f7d0fe02 KW |
1234 | } |
1235 | if (l1_allocated) | |
7267c094 | 1236 | g_free(l1_table); |
93913dfd | 1237 | return ret; |
f7d0fe02 KW |
1238 | } |
1239 | ||
1240 | ||
1241 | ||
1242 | ||
1243 | /*********************************************************/ | |
1244 | /* refcount checking functions */ | |
1245 | ||
1246 | ||
c2551b47 | 1247 | static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries) |
5fee192e HR |
1248 | { |
1249 | /* This assertion holds because there is no way we can address more than | |
1250 | * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because | |
1251 | * offsets have to be representable in bytes); due to every cluster | |
1252 | * corresponding to one refcount entry, we are well below that limit */ | |
1253 | assert(entries < (UINT64_C(1) << (64 - 9))); | |
1254 | ||
1255 | /* Thanks to the assertion this will not overflow, because | |
1256 | * s->refcount_order < 7. | |
1257 | * (note: x << s->refcount_order == x * s->refcount_bits) */ | |
1258 | return DIV_ROUND_UP(entries << s->refcount_order, 8); | |
1259 | } | |
1260 | ||
1261 | /** | |
1262 | * Reallocates *array so that it can hold new_size entries. *size must contain | |
1263 | * the current number of entries in *array. If the reallocation fails, *array | |
1264 | * and *size will not be modified and -errno will be returned. If the | |
1265 | * reallocation is successful, *array will be set to the new buffer, *size | |
1266 | * will be set to new_size and 0 will be returned. The size of the reallocated | |
1267 | * refcount array buffer will be aligned to a cluster boundary, and the newly | |
1268 | * allocated area will be zeroed. | |
1269 | */ | |
ff99129a | 1270 | static int realloc_refcount_array(BDRVQcow2State *s, void **array, |
5fee192e HR |
1271 | int64_t *size, int64_t new_size) |
1272 | { | |
b6d36def | 1273 | int64_t old_byte_size, new_byte_size; |
7453c96b | 1274 | void *new_ptr; |
5fee192e HR |
1275 | |
1276 | /* Round to clusters so the array can be directly written to disk */ | |
1277 | old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size)) | |
1278 | * s->cluster_size; | |
1279 | new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size)) | |
1280 | * s->cluster_size; | |
1281 | ||
1282 | if (new_byte_size == old_byte_size) { | |
1283 | *size = new_size; | |
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | assert(new_byte_size > 0); | |
1288 | ||
b6d36def HR |
1289 | if (new_byte_size > SIZE_MAX) { |
1290 | return -ENOMEM; | |
1291 | } | |
1292 | ||
5fee192e HR |
1293 | new_ptr = g_try_realloc(*array, new_byte_size); |
1294 | if (!new_ptr) { | |
1295 | return -ENOMEM; | |
1296 | } | |
1297 | ||
1298 | if (new_byte_size > old_byte_size) { | |
b6d36def | 1299 | memset((char *)new_ptr + old_byte_size, 0, |
5fee192e HR |
1300 | new_byte_size - old_byte_size); |
1301 | } | |
1302 | ||
1303 | *array = new_ptr; | |
1304 | *size = new_size; | |
1305 | ||
1306 | return 0; | |
1307 | } | |
f7d0fe02 KW |
1308 | |
1309 | /* | |
1310 | * Increases the refcount for a range of clusters in a given refcount table. | |
1311 | * This is used to construct a temporary refcount table out of L1 and L2 tables | |
b6af0975 | 1312 | * which can be compared to the refcount table saved in the image. |
f7d0fe02 | 1313 | * |
9ac228e0 | 1314 | * Modifies the number of errors in res. |
f7d0fe02 | 1315 | */ |
fef4d3d5 HR |
1316 | static int inc_refcounts(BlockDriverState *bs, |
1317 | BdrvCheckResult *res, | |
7453c96b | 1318 | void **refcount_table, |
641bb63c | 1319 | int64_t *refcount_table_size, |
fef4d3d5 | 1320 | int64_t offset, int64_t size) |
f7d0fe02 | 1321 | { |
ff99129a | 1322 | BDRVQcow2State *s = bs->opaque; |
7453c96b | 1323 | uint64_t start, last, cluster_offset, k, refcount; |
5fee192e | 1324 | int ret; |
f7d0fe02 | 1325 | |
fef4d3d5 HR |
1326 | if (size <= 0) { |
1327 | return 0; | |
1328 | } | |
f7d0fe02 | 1329 | |
ac95acdb HT |
1330 | start = start_of_cluster(s, offset); |
1331 | last = start_of_cluster(s, offset + size - 1); | |
f7d0fe02 KW |
1332 | for(cluster_offset = start; cluster_offset <= last; |
1333 | cluster_offset += s->cluster_size) { | |
1334 | k = cluster_offset >> s->cluster_bits; | |
641bb63c | 1335 | if (k >= *refcount_table_size) { |
5fee192e HR |
1336 | ret = realloc_refcount_array(s, refcount_table, |
1337 | refcount_table_size, k + 1); | |
1338 | if (ret < 0) { | |
641bb63c | 1339 | res->check_errors++; |
5fee192e | 1340 | return ret; |
f7d0fe02 | 1341 | } |
641bb63c HR |
1342 | } |
1343 | ||
7453c96b HR |
1344 | refcount = s->get_refcount(*refcount_table, k); |
1345 | if (refcount == s->refcount_max) { | |
641bb63c HR |
1346 | fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64 |
1347 | "\n", cluster_offset); | |
03bb78ed HR |
1348 | fprintf(stderr, "Use qemu-img amend to increase the refcount entry " |
1349 | "width or qemu-img convert to create a clean copy if the " | |
1350 | "image cannot be opened for writing\n"); | |
641bb63c | 1351 | res->corruptions++; |
7453c96b | 1352 | continue; |
f7d0fe02 | 1353 | } |
7453c96b | 1354 | s->set_refcount(*refcount_table, k, refcount + 1); |
f7d0fe02 | 1355 | } |
fef4d3d5 HR |
1356 | |
1357 | return 0; | |
f7d0fe02 KW |
1358 | } |
1359 | ||
801f7044 SH |
1360 | /* Flags for check_refcounts_l1() and check_refcounts_l2() */ |
1361 | enum { | |
fba31bae | 1362 | CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */ |
801f7044 SH |
1363 | }; |
1364 | ||
f7d0fe02 KW |
1365 | /* |
1366 | * Increases the refcount in the given refcount table for the all clusters | |
1367 | * referenced in the L2 table. While doing so, performs some checks on L2 | |
1368 | * entries. | |
1369 | * | |
1370 | * Returns the number of errors found by the checks or -errno if an internal | |
1371 | * error occurred. | |
1372 | */ | |
9ac228e0 | 1373 | static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, |
7453c96b HR |
1374 | void **refcount_table, |
1375 | int64_t *refcount_table_size, int64_t l2_offset, | |
1376 | int flags) | |
f7d0fe02 | 1377 | { |
ff99129a | 1378 | BDRVQcow2State *s = bs->opaque; |
afdf0abe | 1379 | uint64_t *l2_table, l2_entry; |
fba31bae | 1380 | uint64_t next_contiguous_offset = 0; |
ad27390c | 1381 | int i, l2_size, nb_csectors, ret; |
f7d0fe02 KW |
1382 | |
1383 | /* Read L2 table from disk */ | |
1384 | l2_size = s->l2_size * sizeof(uint64_t); | |
7267c094 | 1385 | l2_table = g_malloc(l2_size); |
f7d0fe02 | 1386 | |
9a4f4c31 | 1387 | ret = bdrv_pread(bs->file->bs, l2_offset, l2_table, l2_size); |
ad27390c HR |
1388 | if (ret < 0) { |
1389 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n"); | |
1390 | res->check_errors++; | |
f7d0fe02 | 1391 | goto fail; |
ad27390c | 1392 | } |
f7d0fe02 KW |
1393 | |
1394 | /* Do the actual checks */ | |
1395 | for(i = 0; i < s->l2_size; i++) { | |
afdf0abe KW |
1396 | l2_entry = be64_to_cpu(l2_table[i]); |
1397 | ||
1398 | switch (qcow2_get_cluster_type(l2_entry)) { | |
1399 | case QCOW2_CLUSTER_COMPRESSED: | |
1400 | /* Compressed clusters don't have QCOW_OFLAG_COPIED */ | |
1401 | if (l2_entry & QCOW_OFLAG_COPIED) { | |
1402 | fprintf(stderr, "ERROR: cluster %" PRId64 ": " | |
1403 | "copied flag must never be set for compressed " | |
1404 | "clusters\n", l2_entry >> s->cluster_bits); | |
1405 | l2_entry &= ~QCOW_OFLAG_COPIED; | |
1406 | res->corruptions++; | |
1407 | } | |
f7d0fe02 | 1408 | |
afdf0abe KW |
1409 | /* Mark cluster as used */ |
1410 | nb_csectors = ((l2_entry >> s->csize_shift) & | |
1411 | s->csize_mask) + 1; | |
1412 | l2_entry &= s->cluster_offset_mask; | |
fef4d3d5 HR |
1413 | ret = inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1414 | l2_entry & ~511, nb_csectors * 512); | |
1415 | if (ret < 0) { | |
1416 | goto fail; | |
1417 | } | |
fba31bae SH |
1418 | |
1419 | if (flags & CHECK_FRAG_INFO) { | |
1420 | res->bfi.allocated_clusters++; | |
4db35162 | 1421 | res->bfi.compressed_clusters++; |
fba31bae SH |
1422 | |
1423 | /* Compressed clusters are fragmented by nature. Since they | |
1424 | * take up sub-sector space but we only have sector granularity | |
1425 | * I/O we need to re-read the same sectors even for adjacent | |
1426 | * compressed clusters. | |
1427 | */ | |
1428 | res->bfi.fragmented_clusters++; | |
1429 | } | |
afdf0abe | 1430 | break; |
f7d0fe02 | 1431 | |
6377af48 KW |
1432 | case QCOW2_CLUSTER_ZERO: |
1433 | if ((l2_entry & L2E_OFFSET_MASK) == 0) { | |
1434 | break; | |
1435 | } | |
1436 | /* fall through */ | |
1437 | ||
afdf0abe KW |
1438 | case QCOW2_CLUSTER_NORMAL: |
1439 | { | |
afdf0abe | 1440 | uint64_t offset = l2_entry & L2E_OFFSET_MASK; |
f7d0fe02 | 1441 | |
fba31bae SH |
1442 | if (flags & CHECK_FRAG_INFO) { |
1443 | res->bfi.allocated_clusters++; | |
1444 | if (next_contiguous_offset && | |
1445 | offset != next_contiguous_offset) { | |
1446 | res->bfi.fragmented_clusters++; | |
1447 | } | |
1448 | next_contiguous_offset = offset + s->cluster_size; | |
1449 | } | |
1450 | ||
afdf0abe | 1451 | /* Mark cluster as used */ |
fef4d3d5 HR |
1452 | ret = inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1453 | offset, s->cluster_size); | |
1454 | if (ret < 0) { | |
1455 | goto fail; | |
1456 | } | |
afdf0abe KW |
1457 | |
1458 | /* Correct offsets are cluster aligned */ | |
ac95acdb | 1459 | if (offset_into_cluster(s, offset)) { |
afdf0abe KW |
1460 | fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not " |
1461 | "properly aligned; L2 entry corrupted.\n", offset); | |
1462 | res->corruptions++; | |
1463 | } | |
1464 | break; | |
1465 | } | |
1466 | ||
1467 | case QCOW2_CLUSTER_UNALLOCATED: | |
1468 | break; | |
1469 | ||
1470 | default: | |
1471 | abort(); | |
f7d0fe02 KW |
1472 | } |
1473 | } | |
1474 | ||
7267c094 | 1475 | g_free(l2_table); |
9ac228e0 | 1476 | return 0; |
f7d0fe02 KW |
1477 | |
1478 | fail: | |
7267c094 | 1479 | g_free(l2_table); |
ad27390c | 1480 | return ret; |
f7d0fe02 KW |
1481 | } |
1482 | ||
1483 | /* | |
1484 | * Increases the refcount for the L1 table, its L2 tables and all referenced | |
1485 | * clusters in the given refcount table. While doing so, performs some checks | |
1486 | * on L1 and L2 entries. | |
1487 | * | |
1488 | * Returns the number of errors found by the checks or -errno if an internal | |
1489 | * error occurred. | |
1490 | */ | |
1491 | static int check_refcounts_l1(BlockDriverState *bs, | |
9ac228e0 | 1492 | BdrvCheckResult *res, |
7453c96b | 1493 | void **refcount_table, |
641bb63c | 1494 | int64_t *refcount_table_size, |
f7d0fe02 | 1495 | int64_t l1_table_offset, int l1_size, |
801f7044 | 1496 | int flags) |
f7d0fe02 | 1497 | { |
ff99129a | 1498 | BDRVQcow2State *s = bs->opaque; |
fef4d3d5 | 1499 | uint64_t *l1_table = NULL, l2_offset, l1_size2; |
4f6ed88c | 1500 | int i, ret; |
f7d0fe02 KW |
1501 | |
1502 | l1_size2 = l1_size * sizeof(uint64_t); | |
1503 | ||
1504 | /* Mark L1 table as used */ | |
fef4d3d5 HR |
1505 | ret = inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1506 | l1_table_offset, l1_size2); | |
1507 | if (ret < 0) { | |
1508 | goto fail; | |
1509 | } | |
f7d0fe02 KW |
1510 | |
1511 | /* Read L1 table entries from disk */ | |
fef4d3d5 | 1512 | if (l1_size2 > 0) { |
de82815d KW |
1513 | l1_table = g_try_malloc(l1_size2); |
1514 | if (l1_table == NULL) { | |
1515 | ret = -ENOMEM; | |
ad27390c | 1516 | res->check_errors++; |
de82815d KW |
1517 | goto fail; |
1518 | } | |
9a4f4c31 | 1519 | ret = bdrv_pread(bs->file->bs, l1_table_offset, l1_table, l1_size2); |
ad27390c HR |
1520 | if (ret < 0) { |
1521 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n"); | |
1522 | res->check_errors++; | |
702ef63f | 1523 | goto fail; |
ad27390c | 1524 | } |
702ef63f KW |
1525 | for(i = 0;i < l1_size; i++) |
1526 | be64_to_cpus(&l1_table[i]); | |
1527 | } | |
f7d0fe02 KW |
1528 | |
1529 | /* Do the actual checks */ | |
1530 | for(i = 0; i < l1_size; i++) { | |
1531 | l2_offset = l1_table[i]; | |
1532 | if (l2_offset) { | |
f7d0fe02 | 1533 | /* Mark L2 table as used */ |
afdf0abe | 1534 | l2_offset &= L1E_OFFSET_MASK; |
fef4d3d5 HR |
1535 | ret = inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1536 | l2_offset, s->cluster_size); | |
1537 | if (ret < 0) { | |
1538 | goto fail; | |
1539 | } | |
f7d0fe02 KW |
1540 | |
1541 | /* L2 tables are cluster aligned */ | |
ac95acdb | 1542 | if (offset_into_cluster(s, l2_offset)) { |
f7d0fe02 KW |
1543 | fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not " |
1544 | "cluster aligned; L1 entry corrupted\n", l2_offset); | |
9ac228e0 | 1545 | res->corruptions++; |
f7d0fe02 KW |
1546 | } |
1547 | ||
1548 | /* Process and check L2 entries */ | |
9ac228e0 | 1549 | ret = check_refcounts_l2(bs, res, refcount_table, |
801f7044 | 1550 | refcount_table_size, l2_offset, flags); |
f7d0fe02 KW |
1551 | if (ret < 0) { |
1552 | goto fail; | |
1553 | } | |
f7d0fe02 KW |
1554 | } |
1555 | } | |
7267c094 | 1556 | g_free(l1_table); |
9ac228e0 | 1557 | return 0; |
f7d0fe02 KW |
1558 | |
1559 | fail: | |
7267c094 | 1560 | g_free(l1_table); |
ad27390c | 1561 | return ret; |
f7d0fe02 KW |
1562 | } |
1563 | ||
4f6ed88c HR |
1564 | /* |
1565 | * Checks the OFLAG_COPIED flag for all L1 and L2 entries. | |
1566 | * | |
1567 | * This function does not print an error message nor does it increment | |
44751917 HR |
1568 | * check_errors if qcow2_get_refcount fails (this is because such an error will |
1569 | * have been already detected and sufficiently signaled by the calling function | |
4f6ed88c HR |
1570 | * (qcow2_check_refcounts) by the time this function is called). |
1571 | */ | |
e23e400e HR |
1572 | static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res, |
1573 | BdrvCheckMode fix) | |
4f6ed88c | 1574 | { |
ff99129a | 1575 | BDRVQcow2State *s = bs->opaque; |
4f6ed88c HR |
1576 | uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size); |
1577 | int ret; | |
0e06528e | 1578 | uint64_t refcount; |
4f6ed88c HR |
1579 | int i, j; |
1580 | ||
1581 | for (i = 0; i < s->l1_size; i++) { | |
1582 | uint64_t l1_entry = s->l1_table[i]; | |
1583 | uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK; | |
e23e400e | 1584 | bool l2_dirty = false; |
4f6ed88c HR |
1585 | |
1586 | if (!l2_offset) { | |
1587 | continue; | |
1588 | } | |
1589 | ||
7324c10f HR |
1590 | ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits, |
1591 | &refcount); | |
1592 | if (ret < 0) { | |
4f6ed88c HR |
1593 | /* don't print message nor increment check_errors */ |
1594 | continue; | |
1595 | } | |
1596 | if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) { | |
e23e400e | 1597 | fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d " |
0e06528e | 1598 | "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n", |
e23e400e HR |
1599 | fix & BDRV_FIX_ERRORS ? "Repairing" : |
1600 | "ERROR", | |
4f6ed88c | 1601 | i, l1_entry, refcount); |
e23e400e HR |
1602 | if (fix & BDRV_FIX_ERRORS) { |
1603 | s->l1_table[i] = refcount == 1 | |
1604 | ? l1_entry | QCOW_OFLAG_COPIED | |
1605 | : l1_entry & ~QCOW_OFLAG_COPIED; | |
1606 | ret = qcow2_write_l1_entry(bs, i); | |
1607 | if (ret < 0) { | |
1608 | res->check_errors++; | |
1609 | goto fail; | |
1610 | } | |
1611 | res->corruptions_fixed++; | |
1612 | } else { | |
1613 | res->corruptions++; | |
1614 | } | |
4f6ed88c HR |
1615 | } |
1616 | ||
9a4f4c31 | 1617 | ret = bdrv_pread(bs->file->bs, l2_offset, l2_table, |
4f6ed88c HR |
1618 | s->l2_size * sizeof(uint64_t)); |
1619 | if (ret < 0) { | |
1620 | fprintf(stderr, "ERROR: Could not read L2 table: %s\n", | |
1621 | strerror(-ret)); | |
1622 | res->check_errors++; | |
1623 | goto fail; | |
1624 | } | |
1625 | ||
1626 | for (j = 0; j < s->l2_size; j++) { | |
1627 | uint64_t l2_entry = be64_to_cpu(l2_table[j]); | |
1628 | uint64_t data_offset = l2_entry & L2E_OFFSET_MASK; | |
1629 | int cluster_type = qcow2_get_cluster_type(l2_entry); | |
1630 | ||
1631 | if ((cluster_type == QCOW2_CLUSTER_NORMAL) || | |
1632 | ((cluster_type == QCOW2_CLUSTER_ZERO) && (data_offset != 0))) { | |
7324c10f HR |
1633 | ret = qcow2_get_refcount(bs, |
1634 | data_offset >> s->cluster_bits, | |
1635 | &refcount); | |
1636 | if (ret < 0) { | |
4f6ed88c HR |
1637 | /* don't print message nor increment check_errors */ |
1638 | continue; | |
1639 | } | |
1640 | if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) { | |
e23e400e | 1641 | fprintf(stderr, "%s OFLAG_COPIED data cluster: " |
0e06528e | 1642 | "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n", |
e23e400e HR |
1643 | fix & BDRV_FIX_ERRORS ? "Repairing" : |
1644 | "ERROR", | |
4f6ed88c | 1645 | l2_entry, refcount); |
e23e400e HR |
1646 | if (fix & BDRV_FIX_ERRORS) { |
1647 | l2_table[j] = cpu_to_be64(refcount == 1 | |
1648 | ? l2_entry | QCOW_OFLAG_COPIED | |
1649 | : l2_entry & ~QCOW_OFLAG_COPIED); | |
1650 | l2_dirty = true; | |
1651 | res->corruptions_fixed++; | |
1652 | } else { | |
1653 | res->corruptions++; | |
1654 | } | |
4f6ed88c HR |
1655 | } |
1656 | } | |
1657 | } | |
e23e400e HR |
1658 | |
1659 | if (l2_dirty) { | |
231bb267 HR |
1660 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, |
1661 | l2_offset, s->cluster_size); | |
e23e400e HR |
1662 | if (ret < 0) { |
1663 | fprintf(stderr, "ERROR: Could not write L2 table; metadata " | |
1664 | "overlap check failed: %s\n", strerror(-ret)); | |
1665 | res->check_errors++; | |
1666 | goto fail; | |
1667 | } | |
1668 | ||
9a4f4c31 KW |
1669 | ret = bdrv_pwrite(bs->file->bs, l2_offset, l2_table, |
1670 | s->cluster_size); | |
e23e400e HR |
1671 | if (ret < 0) { |
1672 | fprintf(stderr, "ERROR: Could not write L2 table: %s\n", | |
1673 | strerror(-ret)); | |
1674 | res->check_errors++; | |
1675 | goto fail; | |
1676 | } | |
1677 | } | |
4f6ed88c HR |
1678 | } |
1679 | ||
1680 | ret = 0; | |
1681 | ||
1682 | fail: | |
1683 | qemu_vfree(l2_table); | |
1684 | return ret; | |
1685 | } | |
1686 | ||
6ca56bf5 HR |
1687 | /* |
1688 | * Checks consistency of refblocks and accounts for each refblock in | |
1689 | * *refcount_table. | |
1690 | */ | |
1691 | static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res, | |
f307b255 | 1692 | BdrvCheckMode fix, bool *rebuild, |
7453c96b | 1693 | void **refcount_table, int64_t *nb_clusters) |
6ca56bf5 | 1694 | { |
ff99129a | 1695 | BDRVQcow2State *s = bs->opaque; |
001c158d | 1696 | int64_t i, size; |
fef4d3d5 | 1697 | int ret; |
6ca56bf5 | 1698 | |
f7d0fe02 | 1699 | for(i = 0; i < s->refcount_table_size; i++) { |
6882c8fa | 1700 | uint64_t offset, cluster; |
f7d0fe02 | 1701 | offset = s->refcount_table[i]; |
6882c8fa | 1702 | cluster = offset >> s->cluster_bits; |
746c3cb5 KW |
1703 | |
1704 | /* Refcount blocks are cluster aligned */ | |
ac95acdb | 1705 | if (offset_into_cluster(s, offset)) { |
166acf54 | 1706 | fprintf(stderr, "ERROR refcount block %" PRId64 " is not " |
746c3cb5 | 1707 | "cluster aligned; refcount table entry corrupted\n", i); |
9ac228e0 | 1708 | res->corruptions++; |
f307b255 | 1709 | *rebuild = true; |
6882c8fa KW |
1710 | continue; |
1711 | } | |
1712 | ||
6ca56bf5 | 1713 | if (cluster >= *nb_clusters) { |
001c158d HR |
1714 | fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n", |
1715 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i); | |
1716 | ||
1717 | if (fix & BDRV_FIX_ERRORS) { | |
5fee192e | 1718 | int64_t new_nb_clusters; |
001c158d HR |
1719 | |
1720 | if (offset > INT64_MAX - s->cluster_size) { | |
1721 | ret = -EINVAL; | |
1722 | goto resize_fail; | |
1723 | } | |
1724 | ||
9a4f4c31 | 1725 | ret = bdrv_truncate(bs->file->bs, offset + s->cluster_size); |
001c158d HR |
1726 | if (ret < 0) { |
1727 | goto resize_fail; | |
1728 | } | |
9a4f4c31 | 1729 | size = bdrv_getlength(bs->file->bs); |
001c158d HR |
1730 | if (size < 0) { |
1731 | ret = size; | |
1732 | goto resize_fail; | |
1733 | } | |
1734 | ||
5fee192e HR |
1735 | new_nb_clusters = size_to_clusters(s, size); |
1736 | assert(new_nb_clusters >= *nb_clusters); | |
001c158d | 1737 | |
5fee192e HR |
1738 | ret = realloc_refcount_array(s, refcount_table, |
1739 | nb_clusters, new_nb_clusters); | |
1740 | if (ret < 0) { | |
001c158d | 1741 | res->check_errors++; |
5fee192e | 1742 | return ret; |
001c158d | 1743 | } |
001c158d HR |
1744 | |
1745 | if (cluster >= *nb_clusters) { | |
1746 | ret = -EINVAL; | |
1747 | goto resize_fail; | |
1748 | } | |
1749 | ||
1750 | res->corruptions_fixed++; | |
1751 | ret = inc_refcounts(bs, res, refcount_table, nb_clusters, | |
1752 | offset, s->cluster_size); | |
1753 | if (ret < 0) { | |
1754 | return ret; | |
1755 | } | |
1756 | /* No need to check whether the refcount is now greater than 1: | |
1757 | * This area was just allocated and zeroed, so it can only be | |
1758 | * exactly 1 after inc_refcounts() */ | |
1759 | continue; | |
1760 | ||
1761 | resize_fail: | |
1762 | res->corruptions++; | |
f307b255 | 1763 | *rebuild = true; |
001c158d HR |
1764 | fprintf(stderr, "ERROR could not resize image: %s\n", |
1765 | strerror(-ret)); | |
1766 | } else { | |
1767 | res->corruptions++; | |
1768 | } | |
6882c8fa | 1769 | continue; |
746c3cb5 KW |
1770 | } |
1771 | ||
f7d0fe02 | 1772 | if (offset != 0) { |
641bb63c | 1773 | ret = inc_refcounts(bs, res, refcount_table, nb_clusters, |
fef4d3d5 HR |
1774 | offset, s->cluster_size); |
1775 | if (ret < 0) { | |
1776 | return ret; | |
1777 | } | |
7453c96b | 1778 | if (s->get_refcount(*refcount_table, cluster) != 1) { |
f307b255 | 1779 | fprintf(stderr, "ERROR refcount block %" PRId64 |
7453c96b HR |
1780 | " refcount=%" PRIu64 "\n", i, |
1781 | s->get_refcount(*refcount_table, cluster)); | |
f307b255 HR |
1782 | res->corruptions++; |
1783 | *rebuild = true; | |
746c3cb5 | 1784 | } |
f7d0fe02 KW |
1785 | } |
1786 | } | |
1787 | ||
6ca56bf5 HR |
1788 | return 0; |
1789 | } | |
1790 | ||
057a3fe5 HR |
1791 | /* |
1792 | * Calculates an in-memory refcount table. | |
1793 | */ | |
1794 | static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res, | |
f307b255 | 1795 | BdrvCheckMode fix, bool *rebuild, |
7453c96b | 1796 | void **refcount_table, int64_t *nb_clusters) |
057a3fe5 | 1797 | { |
ff99129a | 1798 | BDRVQcow2State *s = bs->opaque; |
057a3fe5 HR |
1799 | int64_t i; |
1800 | QCowSnapshot *sn; | |
1801 | int ret; | |
1802 | ||
9696df21 | 1803 | if (!*refcount_table) { |
5fee192e HR |
1804 | int64_t old_size = 0; |
1805 | ret = realloc_refcount_array(s, refcount_table, | |
1806 | &old_size, *nb_clusters); | |
1807 | if (ret < 0) { | |
9696df21 | 1808 | res->check_errors++; |
5fee192e | 1809 | return ret; |
9696df21 | 1810 | } |
057a3fe5 HR |
1811 | } |
1812 | ||
1813 | /* header */ | |
641bb63c | 1814 | ret = inc_refcounts(bs, res, refcount_table, nb_clusters, |
fef4d3d5 HR |
1815 | 0, s->cluster_size); |
1816 | if (ret < 0) { | |
1817 | return ret; | |
1818 | } | |
057a3fe5 HR |
1819 | |
1820 | /* current L1 table */ | |
641bb63c | 1821 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
057a3fe5 HR |
1822 | s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO); |
1823 | if (ret < 0) { | |
1824 | return ret; | |
1825 | } | |
1826 | ||
1827 | /* snapshots */ | |
1828 | for (i = 0; i < s->nb_snapshots; i++) { | |
1829 | sn = s->snapshots + i; | |
641bb63c | 1830 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
fef4d3d5 | 1831 | sn->l1_table_offset, sn->l1_size, 0); |
057a3fe5 HR |
1832 | if (ret < 0) { |
1833 | return ret; | |
1834 | } | |
1835 | } | |
641bb63c | 1836 | ret = inc_refcounts(bs, res, refcount_table, nb_clusters, |
fef4d3d5 HR |
1837 | s->snapshots_offset, s->snapshots_size); |
1838 | if (ret < 0) { | |
1839 | return ret; | |
1840 | } | |
057a3fe5 HR |
1841 | |
1842 | /* refcount data */ | |
641bb63c | 1843 | ret = inc_refcounts(bs, res, refcount_table, nb_clusters, |
fef4d3d5 HR |
1844 | s->refcount_table_offset, |
1845 | s->refcount_table_size * sizeof(uint64_t)); | |
1846 | if (ret < 0) { | |
1847 | return ret; | |
1848 | } | |
057a3fe5 | 1849 | |
f307b255 | 1850 | return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters); |
057a3fe5 HR |
1851 | } |
1852 | ||
6ca56bf5 HR |
1853 | /* |
1854 | * Compares the actual reference count for each cluster in the image against the | |
1855 | * refcount as reported by the refcount structures on-disk. | |
1856 | */ | |
1857 | static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res, | |
f307b255 HR |
1858 | BdrvCheckMode fix, bool *rebuild, |
1859 | int64_t *highest_cluster, | |
7453c96b | 1860 | void *refcount_table, int64_t nb_clusters) |
6ca56bf5 | 1861 | { |
ff99129a | 1862 | BDRVQcow2State *s = bs->opaque; |
6ca56bf5 | 1863 | int64_t i; |
0e06528e | 1864 | uint64_t refcount1, refcount2; |
7324c10f | 1865 | int ret; |
6ca56bf5 HR |
1866 | |
1867 | for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) { | |
7324c10f HR |
1868 | ret = qcow2_get_refcount(bs, i, &refcount1); |
1869 | if (ret < 0) { | |
166acf54 | 1870 | fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n", |
7324c10f | 1871 | i, strerror(-ret)); |
9ac228e0 | 1872 | res->check_errors++; |
f74550fd | 1873 | continue; |
018faafd KW |
1874 | } |
1875 | ||
7453c96b | 1876 | refcount2 = s->get_refcount(refcount_table, i); |
c6bb9ad1 FS |
1877 | |
1878 | if (refcount1 > 0 || refcount2 > 0) { | |
6ca56bf5 | 1879 | *highest_cluster = i; |
c6bb9ad1 FS |
1880 | } |
1881 | ||
f7d0fe02 | 1882 | if (refcount1 != refcount2) { |
166acf54 KW |
1883 | /* Check if we're allowed to fix the mismatch */ |
1884 | int *num_fixed = NULL; | |
f307b255 HR |
1885 | if (refcount1 == 0) { |
1886 | *rebuild = true; | |
1887 | } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) { | |
166acf54 KW |
1888 | num_fixed = &res->leaks_fixed; |
1889 | } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) { | |
1890 | num_fixed = &res->corruptions_fixed; | |
1891 | } | |
1892 | ||
0e06528e HR |
1893 | fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64 |
1894 | " reference=%" PRIu64 "\n", | |
166acf54 KW |
1895 | num_fixed != NULL ? "Repairing" : |
1896 | refcount1 < refcount2 ? "ERROR" : | |
1897 | "Leaked", | |
f7d0fe02 | 1898 | i, refcount1, refcount2); |
166acf54 KW |
1899 | |
1900 | if (num_fixed) { | |
1901 | ret = update_refcount(bs, i << s->cluster_bits, 1, | |
2aabe7c7 HR |
1902 | refcount_diff(refcount1, refcount2), |
1903 | refcount1 > refcount2, | |
6cfcb9b8 | 1904 | QCOW2_DISCARD_ALWAYS); |
166acf54 KW |
1905 | if (ret >= 0) { |
1906 | (*num_fixed)++; | |
1907 | continue; | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | /* And if we couldn't, print an error */ | |
9ac228e0 KW |
1912 | if (refcount1 < refcount2) { |
1913 | res->corruptions++; | |
1914 | } else { | |
1915 | res->leaks++; | |
1916 | } | |
f7d0fe02 KW |
1917 | } |
1918 | } | |
6ca56bf5 HR |
1919 | } |
1920 | ||
c7c0681b HR |
1921 | /* |
1922 | * Allocates clusters using an in-memory refcount table (IMRT) in contrast to | |
1923 | * the on-disk refcount structures. | |
1924 | * | |
1925 | * On input, *first_free_cluster tells where to start looking, and need not | |
1926 | * actually be a free cluster; the returned offset will not be before that | |
1927 | * cluster. On output, *first_free_cluster points to the first gap found, even | |
1928 | * if that gap was too small to be used as the returned offset. | |
1929 | * | |
1930 | * Note that *first_free_cluster is a cluster index whereas the return value is | |
1931 | * an offset. | |
1932 | */ | |
1933 | static int64_t alloc_clusters_imrt(BlockDriverState *bs, | |
1934 | int cluster_count, | |
7453c96b | 1935 | void **refcount_table, |
c7c0681b HR |
1936 | int64_t *imrt_nb_clusters, |
1937 | int64_t *first_free_cluster) | |
1938 | { | |
ff99129a | 1939 | BDRVQcow2State *s = bs->opaque; |
c7c0681b HR |
1940 | int64_t cluster = *first_free_cluster, i; |
1941 | bool first_gap = true; | |
1942 | int contiguous_free_clusters; | |
5fee192e | 1943 | int ret; |
c7c0681b HR |
1944 | |
1945 | /* Starting at *first_free_cluster, find a range of at least cluster_count | |
1946 | * continuously free clusters */ | |
1947 | for (contiguous_free_clusters = 0; | |
1948 | cluster < *imrt_nb_clusters && | |
1949 | contiguous_free_clusters < cluster_count; | |
1950 | cluster++) | |
1951 | { | |
7453c96b | 1952 | if (!s->get_refcount(*refcount_table, cluster)) { |
c7c0681b HR |
1953 | contiguous_free_clusters++; |
1954 | if (first_gap) { | |
1955 | /* If this is the first free cluster found, update | |
1956 | * *first_free_cluster accordingly */ | |
1957 | *first_free_cluster = cluster; | |
1958 | first_gap = false; | |
1959 | } | |
1960 | } else if (contiguous_free_clusters) { | |
1961 | contiguous_free_clusters = 0; | |
1962 | } | |
1963 | } | |
1964 | ||
1965 | /* If contiguous_free_clusters is greater than zero, it contains the number | |
1966 | * of continuously free clusters until the current cluster; the first free | |
1967 | * cluster in the current "gap" is therefore | |
1968 | * cluster - contiguous_free_clusters */ | |
1969 | ||
1970 | /* If no such range could be found, grow the in-memory refcount table | |
1971 | * accordingly to append free clusters at the end of the image */ | |
1972 | if (contiguous_free_clusters < cluster_count) { | |
c7c0681b HR |
1973 | /* contiguous_free_clusters clusters are already empty at the image end; |
1974 | * we need cluster_count clusters; therefore, we have to allocate | |
1975 | * cluster_count - contiguous_free_clusters new clusters at the end of | |
1976 | * the image (which is the current value of cluster; note that cluster | |
1977 | * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond | |
1978 | * the image end) */ | |
5fee192e HR |
1979 | ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters, |
1980 | cluster + cluster_count | |
1981 | - contiguous_free_clusters); | |
1982 | if (ret < 0) { | |
1983 | return ret; | |
c7c0681b | 1984 | } |
c7c0681b HR |
1985 | } |
1986 | ||
1987 | /* Go back to the first free cluster */ | |
1988 | cluster -= contiguous_free_clusters; | |
1989 | for (i = 0; i < cluster_count; i++) { | |
7453c96b | 1990 | s->set_refcount(*refcount_table, cluster + i, 1); |
c7c0681b HR |
1991 | } |
1992 | ||
1993 | return cluster << s->cluster_bits; | |
1994 | } | |
1995 | ||
1996 | /* | |
1997 | * Creates a new refcount structure based solely on the in-memory information | |
1998 | * given through *refcount_table. All necessary allocations will be reflected | |
1999 | * in that array. | |
2000 | * | |
2001 | * On success, the old refcount structure is leaked (it will be covered by the | |
2002 | * new refcount structure). | |
2003 | */ | |
2004 | static int rebuild_refcount_structure(BlockDriverState *bs, | |
2005 | BdrvCheckResult *res, | |
7453c96b | 2006 | void **refcount_table, |
c7c0681b HR |
2007 | int64_t *nb_clusters) |
2008 | { | |
ff99129a | 2009 | BDRVQcow2State *s = bs->opaque; |
c7c0681b HR |
2010 | int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0; |
2011 | int64_t refblock_offset, refblock_start, refblock_index; | |
2012 | uint32_t reftable_size = 0; | |
2013 | uint64_t *on_disk_reftable = NULL; | |
7453c96b HR |
2014 | void *on_disk_refblock; |
2015 | int ret = 0; | |
c7c0681b HR |
2016 | struct { |
2017 | uint64_t reftable_offset; | |
2018 | uint32_t reftable_clusters; | |
2019 | } QEMU_PACKED reftable_offset_and_clusters; | |
2020 | ||
2021 | qcow2_cache_empty(bs, s->refcount_block_cache); | |
2022 | ||
2023 | write_refblocks: | |
2024 | for (; cluster < *nb_clusters; cluster++) { | |
7453c96b | 2025 | if (!s->get_refcount(*refcount_table, cluster)) { |
c7c0681b HR |
2026 | continue; |
2027 | } | |
2028 | ||
2029 | refblock_index = cluster >> s->refcount_block_bits; | |
2030 | refblock_start = refblock_index << s->refcount_block_bits; | |
2031 | ||
2032 | /* Don't allocate a cluster in a refblock already written to disk */ | |
2033 | if (first_free_cluster < refblock_start) { | |
2034 | first_free_cluster = refblock_start; | |
2035 | } | |
2036 | refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table, | |
2037 | nb_clusters, &first_free_cluster); | |
2038 | if (refblock_offset < 0) { | |
2039 | fprintf(stderr, "ERROR allocating refblock: %s\n", | |
2040 | strerror(-refblock_offset)); | |
2041 | res->check_errors++; | |
2042 | ret = refblock_offset; | |
2043 | goto fail; | |
2044 | } | |
2045 | ||
2046 | if (reftable_size <= refblock_index) { | |
2047 | uint32_t old_reftable_size = reftable_size; | |
2048 | uint64_t *new_on_disk_reftable; | |
2049 | ||
2050 | reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t), | |
2051 | s->cluster_size) / sizeof(uint64_t); | |
2052 | new_on_disk_reftable = g_try_realloc(on_disk_reftable, | |
2053 | reftable_size * | |
2054 | sizeof(uint64_t)); | |
2055 | if (!new_on_disk_reftable) { | |
2056 | res->check_errors++; | |
2057 | ret = -ENOMEM; | |
2058 | goto fail; | |
2059 | } | |
2060 | on_disk_reftable = new_on_disk_reftable; | |
2061 | ||
2062 | memset(on_disk_reftable + old_reftable_size, 0, | |
2063 | (reftable_size - old_reftable_size) * sizeof(uint64_t)); | |
2064 | ||
2065 | /* The offset we have for the reftable is now no longer valid; | |
2066 | * this will leak that range, but we can easily fix that by running | |
2067 | * a leak-fixing check after this rebuild operation */ | |
2068 | reftable_offset = -1; | |
2069 | } | |
2070 | on_disk_reftable[refblock_index] = refblock_offset; | |
2071 | ||
2072 | /* If this is apparently the last refblock (for now), try to squeeze the | |
2073 | * reftable in */ | |
2074 | if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits && | |
2075 | reftable_offset < 0) | |
2076 | { | |
2077 | uint64_t reftable_clusters = size_to_clusters(s, reftable_size * | |
2078 | sizeof(uint64_t)); | |
2079 | reftable_offset = alloc_clusters_imrt(bs, reftable_clusters, | |
2080 | refcount_table, nb_clusters, | |
2081 | &first_free_cluster); | |
2082 | if (reftable_offset < 0) { | |
2083 | fprintf(stderr, "ERROR allocating reftable: %s\n", | |
2084 | strerror(-reftable_offset)); | |
2085 | res->check_errors++; | |
2086 | ret = reftable_offset; | |
2087 | goto fail; | |
2088 | } | |
2089 | } | |
2090 | ||
2091 | ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset, | |
2092 | s->cluster_size); | |
2093 | if (ret < 0) { | |
2094 | fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret)); | |
2095 | goto fail; | |
2096 | } | |
2097 | ||
7453c96b HR |
2098 | /* The size of *refcount_table is always cluster-aligned, therefore the |
2099 | * write operation will not overflow */ | |
2100 | on_disk_refblock = (void *)((char *) *refcount_table + | |
2101 | refblock_index * s->cluster_size); | |
c7c0681b | 2102 | |
9a4f4c31 | 2103 | ret = bdrv_write(bs->file->bs, refblock_offset / BDRV_SECTOR_SIZE, |
7453c96b | 2104 | on_disk_refblock, s->cluster_sectors); |
c7c0681b HR |
2105 | if (ret < 0) { |
2106 | fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret)); | |
2107 | goto fail; | |
2108 | } | |
2109 | ||
2110 | /* Go to the end of this refblock */ | |
2111 | cluster = refblock_start + s->refcount_block_size - 1; | |
2112 | } | |
2113 | ||
2114 | if (reftable_offset < 0) { | |
2115 | uint64_t post_refblock_start, reftable_clusters; | |
2116 | ||
2117 | post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size); | |
2118 | reftable_clusters = size_to_clusters(s, | |
2119 | reftable_size * sizeof(uint64_t)); | |
2120 | /* Not pretty but simple */ | |
2121 | if (first_free_cluster < post_refblock_start) { | |
2122 | first_free_cluster = post_refblock_start; | |
2123 | } | |
2124 | reftable_offset = alloc_clusters_imrt(bs, reftable_clusters, | |
2125 | refcount_table, nb_clusters, | |
2126 | &first_free_cluster); | |
2127 | if (reftable_offset < 0) { | |
2128 | fprintf(stderr, "ERROR allocating reftable: %s\n", | |
2129 | strerror(-reftable_offset)); | |
2130 | res->check_errors++; | |
2131 | ret = reftable_offset; | |
2132 | goto fail; | |
2133 | } | |
2134 | ||
2135 | goto write_refblocks; | |
2136 | } | |
2137 | ||
2138 | assert(on_disk_reftable); | |
2139 | ||
2140 | for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) { | |
2141 | cpu_to_be64s(&on_disk_reftable[refblock_index]); | |
2142 | } | |
2143 | ||
2144 | ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset, | |
2145 | reftable_size * sizeof(uint64_t)); | |
2146 | if (ret < 0) { | |
2147 | fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret)); | |
2148 | goto fail; | |
2149 | } | |
2150 | ||
2151 | assert(reftable_size < INT_MAX / sizeof(uint64_t)); | |
9a4f4c31 | 2152 | ret = bdrv_pwrite(bs->file->bs, reftable_offset, on_disk_reftable, |
c7c0681b HR |
2153 | reftable_size * sizeof(uint64_t)); |
2154 | if (ret < 0) { | |
2155 | fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret)); | |
2156 | goto fail; | |
2157 | } | |
2158 | ||
2159 | /* Enter new reftable into the image header */ | |
2160 | cpu_to_be64w(&reftable_offset_and_clusters.reftable_offset, | |
2161 | reftable_offset); | |
2162 | cpu_to_be32w(&reftable_offset_and_clusters.reftable_clusters, | |
2163 | size_to_clusters(s, reftable_size * sizeof(uint64_t))); | |
9a4f4c31 KW |
2164 | ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, |
2165 | refcount_table_offset), | |
c7c0681b HR |
2166 | &reftable_offset_and_clusters, |
2167 | sizeof(reftable_offset_and_clusters)); | |
2168 | if (ret < 0) { | |
2169 | fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret)); | |
2170 | goto fail; | |
2171 | } | |
2172 | ||
2173 | for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) { | |
2174 | be64_to_cpus(&on_disk_reftable[refblock_index]); | |
2175 | } | |
2176 | s->refcount_table = on_disk_reftable; | |
2177 | s->refcount_table_offset = reftable_offset; | |
2178 | s->refcount_table_size = reftable_size; | |
2179 | ||
2180 | return 0; | |
2181 | ||
2182 | fail: | |
2183 | g_free(on_disk_reftable); | |
2184 | return ret; | |
2185 | } | |
2186 | ||
6ca56bf5 HR |
2187 | /* |
2188 | * Checks an image for refcount consistency. | |
2189 | * | |
2190 | * Returns 0 if no errors are found, the number of errors in case the image is | |
2191 | * detected as corrupted, and -errno when an internal error occurred. | |
2192 | */ | |
2193 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, | |
2194 | BdrvCheckMode fix) | |
2195 | { | |
ff99129a | 2196 | BDRVQcow2State *s = bs->opaque; |
c7c0681b | 2197 | BdrvCheckResult pre_compare_res; |
6ca56bf5 | 2198 | int64_t size, highest_cluster, nb_clusters; |
7453c96b | 2199 | void *refcount_table = NULL; |
f307b255 | 2200 | bool rebuild = false; |
6ca56bf5 HR |
2201 | int ret; |
2202 | ||
9a4f4c31 | 2203 | size = bdrv_getlength(bs->file->bs); |
6ca56bf5 HR |
2204 | if (size < 0) { |
2205 | res->check_errors++; | |
2206 | return size; | |
2207 | } | |
2208 | ||
2209 | nb_clusters = size_to_clusters(s, size); | |
2210 | if (nb_clusters > INT_MAX) { | |
2211 | res->check_errors++; | |
2212 | return -EFBIG; | |
2213 | } | |
2214 | ||
2215 | res->bfi.total_clusters = | |
2216 | size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE); | |
2217 | ||
f307b255 HR |
2218 | ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table, |
2219 | &nb_clusters); | |
6ca56bf5 HR |
2220 | if (ret < 0) { |
2221 | goto fail; | |
2222 | } | |
2223 | ||
c7c0681b HR |
2224 | /* In case we don't need to rebuild the refcount structure (but want to fix |
2225 | * something), this function is immediately called again, in which case the | |
2226 | * result should be ignored */ | |
2227 | pre_compare_res = *res; | |
2228 | compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table, | |
6ca56bf5 | 2229 | nb_clusters); |
f7d0fe02 | 2230 | |
c7c0681b | 2231 | if (rebuild && (fix & BDRV_FIX_ERRORS)) { |
791230d8 HR |
2232 | BdrvCheckResult old_res = *res; |
2233 | int fresh_leaks = 0; | |
2234 | ||
c7c0681b HR |
2235 | fprintf(stderr, "Rebuilding refcount structure\n"); |
2236 | ret = rebuild_refcount_structure(bs, res, &refcount_table, | |
2237 | &nb_clusters); | |
2238 | if (ret < 0) { | |
2239 | goto fail; | |
2240 | } | |
791230d8 HR |
2241 | |
2242 | res->corruptions = 0; | |
2243 | res->leaks = 0; | |
2244 | ||
2245 | /* Because the old reftable has been exchanged for a new one the | |
2246 | * references have to be recalculated */ | |
2247 | rebuild = false; | |
7453c96b | 2248 | memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters)); |
791230d8 HR |
2249 | ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table, |
2250 | &nb_clusters); | |
2251 | if (ret < 0) { | |
2252 | goto fail; | |
2253 | } | |
2254 | ||
2255 | if (fix & BDRV_FIX_LEAKS) { | |
2256 | /* The old refcount structures are now leaked, fix it; the result | |
2257 | * can be ignored, aside from leaks which were introduced by | |
2258 | * rebuild_refcount_structure() that could not be fixed */ | |
2259 | BdrvCheckResult saved_res = *res; | |
2260 | *res = (BdrvCheckResult){ 0 }; | |
2261 | ||
2262 | compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild, | |
2263 | &highest_cluster, refcount_table, nb_clusters); | |
2264 | if (rebuild) { | |
2265 | fprintf(stderr, "ERROR rebuilt refcount structure is still " | |
2266 | "broken\n"); | |
2267 | } | |
2268 | ||
2269 | /* Any leaks accounted for here were introduced by | |
2270 | * rebuild_refcount_structure() because that function has created a | |
2271 | * new refcount structure from scratch */ | |
2272 | fresh_leaks = res->leaks; | |
2273 | *res = saved_res; | |
2274 | } | |
2275 | ||
2276 | if (res->corruptions < old_res.corruptions) { | |
2277 | res->corruptions_fixed += old_res.corruptions - res->corruptions; | |
2278 | } | |
2279 | if (res->leaks < old_res.leaks) { | |
2280 | res->leaks_fixed += old_res.leaks - res->leaks; | |
2281 | } | |
2282 | res->leaks += fresh_leaks; | |
c7c0681b HR |
2283 | } else if (fix) { |
2284 | if (rebuild) { | |
2285 | fprintf(stderr, "ERROR need to rebuild refcount structures\n"); | |
2286 | res->check_errors++; | |
2287 | ret = -EIO; | |
2288 | goto fail; | |
2289 | } | |
2290 | ||
2291 | if (res->leaks || res->corruptions) { | |
2292 | *res = pre_compare_res; | |
2293 | compare_refcounts(bs, res, fix, &rebuild, &highest_cluster, | |
2294 | refcount_table, nb_clusters); | |
2295 | } | |
f307b255 HR |
2296 | } |
2297 | ||
4f6ed88c | 2298 | /* check OFLAG_COPIED */ |
e23e400e | 2299 | ret = check_oflag_copied(bs, res, fix); |
4f6ed88c HR |
2300 | if (ret < 0) { |
2301 | goto fail; | |
2302 | } | |
2303 | ||
c6bb9ad1 | 2304 | res->image_end_offset = (highest_cluster + 1) * s->cluster_size; |
80fa3341 KW |
2305 | ret = 0; |
2306 | ||
2307 | fail: | |
7267c094 | 2308 | g_free(refcount_table); |
f7d0fe02 | 2309 | |
80fa3341 | 2310 | return ret; |
f7d0fe02 KW |
2311 | } |
2312 | ||
a40f1c2a HR |
2313 | #define overlaps_with(ofs, sz) \ |
2314 | ranges_overlap(offset, size, ofs, sz) | |
2315 | ||
2316 | /* | |
2317 | * Checks if the given offset into the image file is actually free to use by | |
2318 | * looking for overlaps with important metadata sections (L1/L2 tables etc.), | |
2319 | * i.e. a sanity check without relying on the refcount tables. | |
2320 | * | |
231bb267 HR |
2321 | * The ign parameter specifies what checks not to perform (being a bitmask of |
2322 | * QCow2MetadataOverlap values), i.e., what sections to ignore. | |
a40f1c2a HR |
2323 | * |
2324 | * Returns: | |
2325 | * - 0 if writing to this offset will not affect the mentioned metadata | |
2326 | * - a positive QCow2MetadataOverlap value indicating one overlapping section | |
2327 | * - a negative value (-errno) indicating an error while performing a check, | |
2328 | * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2 | |
2329 | */ | |
231bb267 | 2330 | int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a HR |
2331 | int64_t size) |
2332 | { | |
ff99129a | 2333 | BDRVQcow2State *s = bs->opaque; |
3e355390 | 2334 | int chk = s->overlap_check & ~ign; |
a40f1c2a HR |
2335 | int i, j; |
2336 | ||
2337 | if (!size) { | |
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | if (chk & QCOW2_OL_MAIN_HEADER) { | |
2342 | if (offset < s->cluster_size) { | |
2343 | return QCOW2_OL_MAIN_HEADER; | |
2344 | } | |
2345 | } | |
2346 | ||
2347 | /* align range to test to cluster boundaries */ | |
2348 | size = align_offset(offset_into_cluster(s, offset) + size, s->cluster_size); | |
2349 | offset = start_of_cluster(s, offset); | |
2350 | ||
2351 | if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) { | |
2352 | if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) { | |
2353 | return QCOW2_OL_ACTIVE_L1; | |
2354 | } | |
2355 | } | |
2356 | ||
2357 | if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) { | |
2358 | if (overlaps_with(s->refcount_table_offset, | |
2359 | s->refcount_table_size * sizeof(uint64_t))) { | |
2360 | return QCOW2_OL_REFCOUNT_TABLE; | |
2361 | } | |
2362 | } | |
2363 | ||
2364 | if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) { | |
2365 | if (overlaps_with(s->snapshots_offset, s->snapshots_size)) { | |
2366 | return QCOW2_OL_SNAPSHOT_TABLE; | |
2367 | } | |
2368 | } | |
2369 | ||
2370 | if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) { | |
2371 | for (i = 0; i < s->nb_snapshots; i++) { | |
2372 | if (s->snapshots[i].l1_size && | |
2373 | overlaps_with(s->snapshots[i].l1_table_offset, | |
2374 | s->snapshots[i].l1_size * sizeof(uint64_t))) { | |
2375 | return QCOW2_OL_INACTIVE_L1; | |
2376 | } | |
2377 | } | |
2378 | } | |
2379 | ||
2380 | if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) { | |
2381 | for (i = 0; i < s->l1_size; i++) { | |
2382 | if ((s->l1_table[i] & L1E_OFFSET_MASK) && | |
2383 | overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK, | |
2384 | s->cluster_size)) { | |
2385 | return QCOW2_OL_ACTIVE_L2; | |
2386 | } | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) { | |
2391 | for (i = 0; i < s->refcount_table_size; i++) { | |
2392 | if ((s->refcount_table[i] & REFT_OFFSET_MASK) && | |
2393 | overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK, | |
2394 | s->cluster_size)) { | |
2395 | return QCOW2_OL_REFCOUNT_BLOCK; | |
2396 | } | |
2397 | } | |
2398 | } | |
2399 | ||
2400 | if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) { | |
2401 | for (i = 0; i < s->nb_snapshots; i++) { | |
2402 | uint64_t l1_ofs = s->snapshots[i].l1_table_offset; | |
2403 | uint32_t l1_sz = s->snapshots[i].l1_size; | |
998b959c | 2404 | uint64_t l1_sz2 = l1_sz * sizeof(uint64_t); |
de82815d | 2405 | uint64_t *l1 = g_try_malloc(l1_sz2); |
a40f1c2a HR |
2406 | int ret; |
2407 | ||
de82815d KW |
2408 | if (l1_sz2 && l1 == NULL) { |
2409 | return -ENOMEM; | |
2410 | } | |
2411 | ||
9a4f4c31 | 2412 | ret = bdrv_pread(bs->file->bs, l1_ofs, l1, l1_sz2); |
a40f1c2a HR |
2413 | if (ret < 0) { |
2414 | g_free(l1); | |
2415 | return ret; | |
2416 | } | |
2417 | ||
2418 | for (j = 0; j < l1_sz; j++) { | |
1e242b55 HR |
2419 | uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK; |
2420 | if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) { | |
a40f1c2a HR |
2421 | g_free(l1); |
2422 | return QCOW2_OL_INACTIVE_L2; | |
2423 | } | |
2424 | } | |
2425 | ||
2426 | g_free(l1); | |
2427 | } | |
2428 | } | |
2429 | ||
2430 | return 0; | |
2431 | } | |
2432 | ||
2433 | static const char *metadata_ol_names[] = { | |
2434 | [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header", | |
2435 | [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table", | |
2436 | [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table", | |
2437 | [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table", | |
2438 | [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block", | |
2439 | [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table", | |
2440 | [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table", | |
2441 | [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table", | |
2442 | }; | |
2443 | ||
2444 | /* | |
2445 | * First performs a check for metadata overlaps (through | |
2446 | * qcow2_check_metadata_overlap); if that fails with a negative value (error | |
2447 | * while performing a check), that value is returned. If an impending overlap | |
2448 | * is detected, the BDS will be made unusable, the qcow2 file marked corrupt | |
2449 | * and -EIO returned. | |
2450 | * | |
2451 | * Returns 0 if there were neither overlaps nor errors while checking for | |
2452 | * overlaps; or a negative value (-errno) on error. | |
2453 | */ | |
231bb267 | 2454 | int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a HR |
2455 | int64_t size) |
2456 | { | |
231bb267 | 2457 | int ret = qcow2_check_metadata_overlap(bs, ign, offset, size); |
a40f1c2a HR |
2458 | |
2459 | if (ret < 0) { | |
2460 | return ret; | |
2461 | } else if (ret > 0) { | |
786a4ea8 | 2462 | int metadata_ol_bitnr = ctz32(ret); |
a40f1c2a HR |
2463 | assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR); |
2464 | ||
adb43552 HR |
2465 | qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid " |
2466 | "write on metadata (overlaps with %s)", | |
2467 | metadata_ol_names[metadata_ol_bitnr]); | |
a40f1c2a HR |
2468 | return -EIO; |
2469 | } | |
2470 | ||
2471 | return 0; | |
2472 | } | |
791c9a00 HR |
2473 | |
2474 | /* A pointer to a function of this type is given to walk_over_reftable(). That | |
2475 | * function will create refblocks and pass them to a RefblockFinishOp once they | |
2476 | * are completed (@refblock). @refblock_empty is set if the refblock is | |
2477 | * completely empty. | |
2478 | * | |
2479 | * Along with the refblock, a corresponding reftable entry is passed, in the | |
2480 | * reftable @reftable (which may be reallocated) at @reftable_index. | |
2481 | * | |
2482 | * @allocated should be set to true if a new cluster has been allocated. | |
2483 | */ | |
2484 | typedef int (RefblockFinishOp)(BlockDriverState *bs, uint64_t **reftable, | |
2485 | uint64_t reftable_index, uint64_t *reftable_size, | |
2486 | void *refblock, bool refblock_empty, | |
2487 | bool *allocated, Error **errp); | |
2488 | ||
2489 | /** | |
2490 | * This "operation" for walk_over_reftable() allocates the refblock on disk (if | |
2491 | * it is not empty) and inserts its offset into the new reftable. The size of | |
2492 | * this new reftable is increased as required. | |
2493 | */ | |
2494 | static int alloc_refblock(BlockDriverState *bs, uint64_t **reftable, | |
2495 | uint64_t reftable_index, uint64_t *reftable_size, | |
2496 | void *refblock, bool refblock_empty, bool *allocated, | |
2497 | Error **errp) | |
2498 | { | |
2499 | BDRVQcow2State *s = bs->opaque; | |
2500 | int64_t offset; | |
2501 | ||
2502 | if (!refblock_empty && reftable_index >= *reftable_size) { | |
2503 | uint64_t *new_reftable; | |
2504 | uint64_t new_reftable_size; | |
2505 | ||
2506 | new_reftable_size = ROUND_UP(reftable_index + 1, | |
2507 | s->cluster_size / sizeof(uint64_t)); | |
2508 | if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) { | |
2509 | error_setg(errp, | |
2510 | "This operation would make the refcount table grow " | |
2511 | "beyond the maximum size supported by QEMU, aborting"); | |
2512 | return -ENOTSUP; | |
2513 | } | |
2514 | ||
2515 | new_reftable = g_try_realloc(*reftable, new_reftable_size * | |
2516 | sizeof(uint64_t)); | |
2517 | if (!new_reftable) { | |
2518 | error_setg(errp, "Failed to increase reftable buffer size"); | |
2519 | return -ENOMEM; | |
2520 | } | |
2521 | ||
2522 | memset(new_reftable + *reftable_size, 0, | |
2523 | (new_reftable_size - *reftable_size) * sizeof(uint64_t)); | |
2524 | ||
2525 | *reftable = new_reftable; | |
2526 | *reftable_size = new_reftable_size; | |
2527 | } | |
2528 | ||
2529 | if (!refblock_empty && !(*reftable)[reftable_index]) { | |
2530 | offset = qcow2_alloc_clusters(bs, s->cluster_size); | |
2531 | if (offset < 0) { | |
2532 | error_setg_errno(errp, -offset, "Failed to allocate refblock"); | |
2533 | return offset; | |
2534 | } | |
2535 | (*reftable)[reftable_index] = offset; | |
2536 | *allocated = true; | |
2537 | } | |
2538 | ||
2539 | return 0; | |
2540 | } | |
2541 | ||
2542 | /** | |
2543 | * This "operation" for walk_over_reftable() writes the refblock to disk at the | |
2544 | * offset specified by the new reftable's entry. It does not modify the new | |
2545 | * reftable or change any refcounts. | |
2546 | */ | |
2547 | static int flush_refblock(BlockDriverState *bs, uint64_t **reftable, | |
2548 | uint64_t reftable_index, uint64_t *reftable_size, | |
2549 | void *refblock, bool refblock_empty, bool *allocated, | |
2550 | Error **errp) | |
2551 | { | |
2552 | BDRVQcow2State *s = bs->opaque; | |
2553 | int64_t offset; | |
2554 | int ret; | |
2555 | ||
2556 | if (reftable_index < *reftable_size && (*reftable)[reftable_index]) { | |
2557 | offset = (*reftable)[reftable_index]; | |
2558 | ||
2559 | ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size); | |
2560 | if (ret < 0) { | |
2561 | error_setg_errno(errp, -ret, "Overlap check failed"); | |
2562 | return ret; | |
2563 | } | |
2564 | ||
2565 | ret = bdrv_pwrite(bs->file->bs, offset, refblock, s->cluster_size); | |
2566 | if (ret < 0) { | |
2567 | error_setg_errno(errp, -ret, "Failed to write refblock"); | |
2568 | return ret; | |
2569 | } | |
2570 | } else { | |
2571 | assert(refblock_empty); | |
2572 | } | |
2573 | ||
2574 | return 0; | |
2575 | } | |
2576 | ||
2577 | /** | |
2578 | * This function walks over the existing reftable and every referenced refblock; | |
2579 | * if @new_set_refcount is non-NULL, it is called for every refcount entry to | |
2580 | * create an equal new entry in the passed @new_refblock. Once that | |
2581 | * @new_refblock is completely filled, @operation will be called. | |
2582 | * | |
2583 | * @status_cb and @cb_opaque are used for the amend operation's status callback. | |
2584 | * @index is the index of the walk_over_reftable() calls and @total is the total | |
2585 | * number of walk_over_reftable() calls per amend operation. Both are used for | |
2586 | * calculating the parameters for the status callback. | |
2587 | * | |
2588 | * @allocated is set to true if a new cluster has been allocated. | |
2589 | */ | |
2590 | static int walk_over_reftable(BlockDriverState *bs, uint64_t **new_reftable, | |
2591 | uint64_t *new_reftable_index, | |
2592 | uint64_t *new_reftable_size, | |
2593 | void *new_refblock, int new_refblock_size, | |
2594 | int new_refcount_bits, | |
2595 | RefblockFinishOp *operation, bool *allocated, | |
2596 | Qcow2SetRefcountFunc *new_set_refcount, | |
2597 | BlockDriverAmendStatusCB *status_cb, | |
2598 | void *cb_opaque, int index, int total, | |
2599 | Error **errp) | |
2600 | { | |
2601 | BDRVQcow2State *s = bs->opaque; | |
2602 | uint64_t reftable_index; | |
2603 | bool new_refblock_empty = true; | |
2604 | int refblock_index; | |
2605 | int new_refblock_index = 0; | |
2606 | int ret; | |
2607 | ||
2608 | for (reftable_index = 0; reftable_index < s->refcount_table_size; | |
2609 | reftable_index++) | |
2610 | { | |
2611 | uint64_t refblock_offset = s->refcount_table[reftable_index] | |
2612 | & REFT_OFFSET_MASK; | |
2613 | ||
2614 | status_cb(bs, (uint64_t)index * s->refcount_table_size + reftable_index, | |
2615 | (uint64_t)total * s->refcount_table_size, cb_opaque); | |
2616 | ||
2617 | if (refblock_offset) { | |
2618 | void *refblock; | |
2619 | ||
2620 | if (offset_into_cluster(s, refblock_offset)) { | |
2621 | qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" | |
2622 | PRIx64 " unaligned (reftable index: %#" | |
2623 | PRIx64 ")", refblock_offset, | |
2624 | reftable_index); | |
2625 | error_setg(errp, | |
2626 | "Image is corrupt (unaligned refblock offset)"); | |
2627 | return -EIO; | |
2628 | } | |
2629 | ||
2630 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offset, | |
2631 | &refblock); | |
2632 | if (ret < 0) { | |
2633 | error_setg_errno(errp, -ret, "Failed to retrieve refblock"); | |
2634 | return ret; | |
2635 | } | |
2636 | ||
2637 | for (refblock_index = 0; refblock_index < s->refcount_block_size; | |
2638 | refblock_index++) | |
2639 | { | |
2640 | uint64_t refcount; | |
2641 | ||
2642 | if (new_refblock_index >= new_refblock_size) { | |
2643 | /* new_refblock is now complete */ | |
2644 | ret = operation(bs, new_reftable, *new_reftable_index, | |
2645 | new_reftable_size, new_refblock, | |
2646 | new_refblock_empty, allocated, errp); | |
2647 | if (ret < 0) { | |
2648 | qcow2_cache_put(bs, s->refcount_block_cache, &refblock); | |
2649 | return ret; | |
2650 | } | |
2651 | ||
2652 | (*new_reftable_index)++; | |
2653 | new_refblock_index = 0; | |
2654 | new_refblock_empty = true; | |
2655 | } | |
2656 | ||
2657 | refcount = s->get_refcount(refblock, refblock_index); | |
2658 | if (new_refcount_bits < 64 && refcount >> new_refcount_bits) { | |
2659 | uint64_t offset; | |
2660 | ||
2661 | qcow2_cache_put(bs, s->refcount_block_cache, &refblock); | |
2662 | ||
2663 | offset = ((reftable_index << s->refcount_block_bits) | |
2664 | + refblock_index) << s->cluster_bits; | |
2665 | ||
2666 | error_setg(errp, "Cannot decrease refcount entry width to " | |
2667 | "%i bits: Cluster at offset %#" PRIx64 " has a " | |
2668 | "refcount of %" PRIu64, new_refcount_bits, | |
2669 | offset, refcount); | |
2670 | return -EINVAL; | |
2671 | } | |
2672 | ||
2673 | if (new_set_refcount) { | |
2674 | new_set_refcount(new_refblock, new_refblock_index++, | |
2675 | refcount); | |
2676 | } else { | |
2677 | new_refblock_index++; | |
2678 | } | |
2679 | new_refblock_empty = new_refblock_empty && refcount == 0; | |
2680 | } | |
2681 | ||
2682 | qcow2_cache_put(bs, s->refcount_block_cache, &refblock); | |
2683 | } else { | |
2684 | /* No refblock means every refcount is 0 */ | |
2685 | for (refblock_index = 0; refblock_index < s->refcount_block_size; | |
2686 | refblock_index++) | |
2687 | { | |
2688 | if (new_refblock_index >= new_refblock_size) { | |
2689 | /* new_refblock is now complete */ | |
2690 | ret = operation(bs, new_reftable, *new_reftable_index, | |
2691 | new_reftable_size, new_refblock, | |
2692 | new_refblock_empty, allocated, errp); | |
2693 | if (ret < 0) { | |
2694 | return ret; | |
2695 | } | |
2696 | ||
2697 | (*new_reftable_index)++; | |
2698 | new_refblock_index = 0; | |
2699 | new_refblock_empty = true; | |
2700 | } | |
2701 | ||
2702 | if (new_set_refcount) { | |
2703 | new_set_refcount(new_refblock, new_refblock_index++, 0); | |
2704 | } else { | |
2705 | new_refblock_index++; | |
2706 | } | |
2707 | } | |
2708 | } | |
2709 | } | |
2710 | ||
2711 | if (new_refblock_index > 0) { | |
2712 | /* Complete the potentially existing partially filled final refblock */ | |
2713 | if (new_set_refcount) { | |
2714 | for (; new_refblock_index < new_refblock_size; | |
2715 | new_refblock_index++) | |
2716 | { | |
2717 | new_set_refcount(new_refblock, new_refblock_index, 0); | |
2718 | } | |
2719 | } | |
2720 | ||
2721 | ret = operation(bs, new_reftable, *new_reftable_index, | |
2722 | new_reftable_size, new_refblock, new_refblock_empty, | |
2723 | allocated, errp); | |
2724 | if (ret < 0) { | |
2725 | return ret; | |
2726 | } | |
2727 | ||
2728 | (*new_reftable_index)++; | |
2729 | } | |
2730 | ||
2731 | status_cb(bs, (uint64_t)(index + 1) * s->refcount_table_size, | |
2732 | (uint64_t)total * s->refcount_table_size, cb_opaque); | |
2733 | ||
2734 | return 0; | |
2735 | } | |
2736 | ||
2737 | int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order, | |
2738 | BlockDriverAmendStatusCB *status_cb, | |
2739 | void *cb_opaque, Error **errp) | |
2740 | { | |
2741 | BDRVQcow2State *s = bs->opaque; | |
2742 | Qcow2GetRefcountFunc *new_get_refcount; | |
2743 | Qcow2SetRefcountFunc *new_set_refcount; | |
2744 | void *new_refblock = qemu_blockalign(bs->file->bs, s->cluster_size); | |
2745 | uint64_t *new_reftable = NULL, new_reftable_size = 0; | |
2746 | uint64_t *old_reftable, old_reftable_size, old_reftable_offset; | |
2747 | uint64_t new_reftable_index = 0; | |
2748 | uint64_t i; | |
2749 | int64_t new_reftable_offset = 0, allocated_reftable_size = 0; | |
2750 | int new_refblock_size, new_refcount_bits = 1 << refcount_order; | |
2751 | int old_refcount_order; | |
2752 | int walk_index = 0; | |
2753 | int ret; | |
2754 | bool new_allocation; | |
2755 | ||
2756 | assert(s->qcow_version >= 3); | |
2757 | assert(refcount_order >= 0 && refcount_order <= 6); | |
2758 | ||
2759 | /* see qcow2_open() */ | |
2760 | new_refblock_size = 1 << (s->cluster_bits - (refcount_order - 3)); | |
2761 | ||
2762 | new_get_refcount = get_refcount_funcs[refcount_order]; | |
2763 | new_set_refcount = set_refcount_funcs[refcount_order]; | |
2764 | ||
2765 | ||
2766 | do { | |
2767 | int total_walks; | |
2768 | ||
2769 | new_allocation = false; | |
2770 | ||
2771 | /* At least we have to do this walk and the one which writes the | |
2772 | * refblocks; also, at least we have to do this loop here at least | |
2773 | * twice (normally), first to do the allocations, and second to | |
2774 | * determine that everything is correctly allocated, this then makes | |
2775 | * three walks in total */ | |
2776 | total_walks = MAX(walk_index + 2, 3); | |
2777 | ||
2778 | /* First, allocate the structures so they are present in the refcount | |
2779 | * structures */ | |
2780 | ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index, | |
2781 | &new_reftable_size, NULL, new_refblock_size, | |
2782 | new_refcount_bits, &alloc_refblock, | |
2783 | &new_allocation, NULL, status_cb, cb_opaque, | |
2784 | walk_index++, total_walks, errp); | |
2785 | if (ret < 0) { | |
2786 | goto done; | |
2787 | } | |
2788 | ||
2789 | new_reftable_index = 0; | |
2790 | ||
2791 | if (new_allocation) { | |
2792 | if (new_reftable_offset) { | |
2793 | qcow2_free_clusters(bs, new_reftable_offset, | |
2794 | allocated_reftable_size * sizeof(uint64_t), | |
2795 | QCOW2_DISCARD_NEVER); | |
2796 | } | |
2797 | ||
2798 | new_reftable_offset = qcow2_alloc_clusters(bs, new_reftable_size * | |
2799 | sizeof(uint64_t)); | |
2800 | if (new_reftable_offset < 0) { | |
2801 | error_setg_errno(errp, -new_reftable_offset, | |
2802 | "Failed to allocate the new reftable"); | |
2803 | ret = new_reftable_offset; | |
2804 | goto done; | |
2805 | } | |
2806 | allocated_reftable_size = new_reftable_size; | |
2807 | } | |
2808 | } while (new_allocation); | |
2809 | ||
2810 | /* Second, write the new refblocks */ | |
2811 | ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index, | |
2812 | &new_reftable_size, new_refblock, | |
2813 | new_refblock_size, new_refcount_bits, | |
2814 | &flush_refblock, &new_allocation, new_set_refcount, | |
2815 | status_cb, cb_opaque, walk_index, walk_index + 1, | |
2816 | errp); | |
2817 | if (ret < 0) { | |
2818 | goto done; | |
2819 | } | |
2820 | assert(!new_allocation); | |
2821 | ||
2822 | ||
2823 | /* Write the new reftable */ | |
2824 | ret = qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset, | |
2825 | new_reftable_size * sizeof(uint64_t)); | |
2826 | if (ret < 0) { | |
2827 | error_setg_errno(errp, -ret, "Overlap check failed"); | |
2828 | goto done; | |
2829 | } | |
2830 | ||
2831 | for (i = 0; i < new_reftable_size; i++) { | |
2832 | cpu_to_be64s(&new_reftable[i]); | |
2833 | } | |
2834 | ||
2835 | ret = bdrv_pwrite(bs->file->bs, new_reftable_offset, new_reftable, | |
2836 | new_reftable_size * sizeof(uint64_t)); | |
2837 | ||
2838 | for (i = 0; i < new_reftable_size; i++) { | |
2839 | be64_to_cpus(&new_reftable[i]); | |
2840 | } | |
2841 | ||
2842 | if (ret < 0) { | |
2843 | error_setg_errno(errp, -ret, "Failed to write the new reftable"); | |
2844 | goto done; | |
2845 | } | |
2846 | ||
2847 | ||
2848 | /* Empty the refcount cache */ | |
2849 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
2850 | if (ret < 0) { | |
2851 | error_setg_errno(errp, -ret, "Failed to flush the refblock cache"); | |
2852 | goto done; | |
2853 | } | |
2854 | ||
2855 | /* Update the image header to point to the new reftable; this only updates | |
2856 | * the fields which are relevant to qcow2_update_header(); other fields | |
2857 | * such as s->refcount_table or s->refcount_bits stay stale for now | |
2858 | * (because we have to restore everything if qcow2_update_header() fails) */ | |
2859 | old_refcount_order = s->refcount_order; | |
2860 | old_reftable_size = s->refcount_table_size; | |
2861 | old_reftable_offset = s->refcount_table_offset; | |
2862 | ||
2863 | s->refcount_order = refcount_order; | |
2864 | s->refcount_table_size = new_reftable_size; | |
2865 | s->refcount_table_offset = new_reftable_offset; | |
2866 | ||
2867 | ret = qcow2_update_header(bs); | |
2868 | if (ret < 0) { | |
2869 | s->refcount_order = old_refcount_order; | |
2870 | s->refcount_table_size = old_reftable_size; | |
2871 | s->refcount_table_offset = old_reftable_offset; | |
2872 | error_setg_errno(errp, -ret, "Failed to update the qcow2 header"); | |
2873 | goto done; | |
2874 | } | |
2875 | ||
2876 | /* Now update the rest of the in-memory information */ | |
2877 | old_reftable = s->refcount_table; | |
2878 | s->refcount_table = new_reftable; | |
2879 | ||
2880 | s->refcount_bits = 1 << refcount_order; | |
2881 | s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1); | |
2882 | s->refcount_max += s->refcount_max - 1; | |
2883 | ||
2884 | s->refcount_block_bits = s->cluster_bits - (refcount_order - 3); | |
2885 | s->refcount_block_size = 1 << s->refcount_block_bits; | |
2886 | ||
2887 | s->get_refcount = new_get_refcount; | |
2888 | s->set_refcount = new_set_refcount; | |
2889 | ||
2890 | /* For cleaning up all old refblocks and the old reftable below the "done" | |
2891 | * label */ | |
2892 | new_reftable = old_reftable; | |
2893 | new_reftable_size = old_reftable_size; | |
2894 | new_reftable_offset = old_reftable_offset; | |
2895 | ||
2896 | done: | |
2897 | if (new_reftable) { | |
2898 | /* On success, new_reftable actually points to the old reftable (and | |
2899 | * new_reftable_size is the old reftable's size); but that is just | |
2900 | * fine */ | |
2901 | for (i = 0; i < new_reftable_size; i++) { | |
2902 | uint64_t offset = new_reftable[i] & REFT_OFFSET_MASK; | |
2903 | if (offset) { | |
2904 | qcow2_free_clusters(bs, offset, s->cluster_size, | |
2905 | QCOW2_DISCARD_OTHER); | |
2906 | } | |
2907 | } | |
2908 | g_free(new_reftable); | |
2909 | ||
2910 | if (new_reftable_offset > 0) { | |
2911 | qcow2_free_clusters(bs, new_reftable_offset, | |
2912 | new_reftable_size * sizeof(uint64_t), | |
2913 | QCOW2_DISCARD_OTHER); | |
2914 | } | |
2915 | } | |
2916 | ||
2917 | qemu_vfree(new_refblock); | |
2918 | return ret; | |
2919 | } |