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