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