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