]>
Commit | Line | Data |
---|---|---|
f7d0fe02 KW |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
3 | * | |
4 | * Copyright (c) 2004-2006 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu-common.h" | |
737e150e | 26 | #include "block/block_int.h" |
f7d0fe02 | 27 | #include "block/qcow2.h" |
a40f1c2a HR |
28 | #include "qemu/range.h" |
29 | #include "qapi/qmp/types.h" | |
f7d0fe02 KW |
30 | |
31 | static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size); | |
92dcb59f | 32 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
f7d0fe02 | 33 | int64_t offset, int64_t length, |
6cfcb9b8 | 34 | int addend, enum qcow2_discard_type type); |
f7d0fe02 | 35 | |
3b88e52b | 36 | |
f7d0fe02 KW |
37 | /*********************************************************/ |
38 | /* refcount handling */ | |
39 | ||
ed6ccf0f | 40 | int qcow2_refcount_init(BlockDriverState *bs) |
f7d0fe02 KW |
41 | { |
42 | BDRVQcowState *s = bs->opaque; | |
5dab2fad KW |
43 | unsigned int refcount_table_size2, i; |
44 | int ret; | |
f7d0fe02 | 45 | |
5dab2fad | 46 | assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t)); |
f7d0fe02 | 47 | refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t); |
7267c094 | 48 | s->refcount_table = g_malloc(refcount_table_size2); |
f7d0fe02 | 49 | if (s->refcount_table_size > 0) { |
66f82cee KW |
50 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD); |
51 | ret = bdrv_pread(bs->file, s->refcount_table_offset, | |
f7d0fe02 KW |
52 | s->refcount_table, refcount_table_size2); |
53 | if (ret != refcount_table_size2) | |
54 | goto fail; | |
55 | for(i = 0; i < s->refcount_table_size; i++) | |
56 | be64_to_cpus(&s->refcount_table[i]); | |
57 | } | |
58 | return 0; | |
59 | fail: | |
60 | return -ENOMEM; | |
61 | } | |
62 | ||
ed6ccf0f | 63 | void qcow2_refcount_close(BlockDriverState *bs) |
f7d0fe02 KW |
64 | { |
65 | BDRVQcowState *s = bs->opaque; | |
7267c094 | 66 | g_free(s->refcount_table); |
f7d0fe02 KW |
67 | } |
68 | ||
69 | ||
70 | static int load_refcount_block(BlockDriverState *bs, | |
29c1a730 KW |
71 | int64_t refcount_block_offset, |
72 | void **refcount_block) | |
f7d0fe02 KW |
73 | { |
74 | BDRVQcowState *s = bs->opaque; | |
75 | int ret; | |
3b88e52b | 76 | |
66f82cee | 77 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD); |
29c1a730 KW |
78 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, |
79 | refcount_block); | |
e14e8ba5 | 80 | |
29c1a730 | 81 | return ret; |
f7d0fe02 KW |
82 | } |
83 | ||
018faafd KW |
84 | /* |
85 | * Returns the refcount of the cluster given by its index. Any non-negative | |
86 | * return value is the refcount of the cluster, negative values are -errno | |
87 | * and indicate an error. | |
88 | */ | |
f7d0fe02 KW |
89 | static int get_refcount(BlockDriverState *bs, int64_t cluster_index) |
90 | { | |
91 | BDRVQcowState *s = bs->opaque; | |
92 | int refcount_table_index, block_index; | |
93 | int64_t refcount_block_offset; | |
018faafd | 94 | int ret; |
29c1a730 KW |
95 | uint16_t *refcount_block; |
96 | uint16_t refcount; | |
f7d0fe02 KW |
97 | |
98 | refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT); | |
99 | if (refcount_table_index >= s->refcount_table_size) | |
100 | return 0; | |
26d49c46 HR |
101 | refcount_block_offset = |
102 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; | |
f7d0fe02 KW |
103 | if (!refcount_block_offset) |
104 | return 0; | |
29c1a730 KW |
105 | |
106 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, | |
107 | (void**) &refcount_block); | |
108 | if (ret < 0) { | |
109 | return ret; | |
f7d0fe02 | 110 | } |
29c1a730 | 111 | |
f7d0fe02 KW |
112 | block_index = cluster_index & |
113 | ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); | |
29c1a730 KW |
114 | refcount = be16_to_cpu(refcount_block[block_index]); |
115 | ||
116 | ret = qcow2_cache_put(bs, s->refcount_block_cache, | |
117 | (void**) &refcount_block); | |
118 | if (ret < 0) { | |
119 | return ret; | |
120 | } | |
121 | ||
122 | return refcount; | |
f7d0fe02 KW |
123 | } |
124 | ||
05121aed KW |
125 | /* |
126 | * Rounds the refcount table size up to avoid growing the table for each single | |
127 | * refcount block that is allocated. | |
128 | */ | |
129 | static unsigned int next_refcount_table_size(BDRVQcowState *s, | |
130 | unsigned int min_size) | |
131 | { | |
132 | unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1; | |
133 | unsigned int refcount_table_clusters = | |
134 | MAX(1, s->refcount_table_size >> (s->cluster_bits - 3)); | |
135 | ||
136 | while (min_clusters > refcount_table_clusters) { | |
137 | refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2; | |
138 | } | |
139 | ||
140 | return refcount_table_clusters << (s->cluster_bits - 3); | |
141 | } | |
142 | ||
92dcb59f KW |
143 | |
144 | /* Checks if two offsets are described by the same refcount block */ | |
145 | static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a, | |
146 | uint64_t offset_b) | |
147 | { | |
148 | uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT); | |
149 | uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT); | |
150 | ||
151 | return (block_a == block_b); | |
152 | } | |
153 | ||
154 | /* | |
155 | * Loads a refcount block. If it doesn't exist yet, it is allocated first | |
156 | * (including growing the refcount table if needed). | |
157 | * | |
29c1a730 | 158 | * Returns 0 on success or -errno in error case |
92dcb59f | 159 | */ |
29c1a730 KW |
160 | static int alloc_refcount_block(BlockDriverState *bs, |
161 | int64_t cluster_index, uint16_t **refcount_block) | |
f7d0fe02 KW |
162 | { |
163 | BDRVQcowState *s = bs->opaque; | |
92dcb59f KW |
164 | unsigned int refcount_table_index; |
165 | int ret; | |
166 | ||
66f82cee | 167 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); |
8252278a | 168 | |
92dcb59f KW |
169 | /* Find the refcount block for the given cluster */ |
170 | refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT); | |
171 | ||
172 | if (refcount_table_index < s->refcount_table_size) { | |
173 | ||
174 | uint64_t refcount_block_offset = | |
76dc9e0c | 175 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; |
92dcb59f KW |
176 | |
177 | /* If it's already there, we're done */ | |
178 | if (refcount_block_offset) { | |
29c1a730 KW |
179 | return load_refcount_block(bs, refcount_block_offset, |
180 | (void**) refcount_block); | |
92dcb59f KW |
181 | } |
182 | } | |
183 | ||
184 | /* | |
185 | * If we came here, we need to allocate something. Something is at least | |
186 | * a cluster for the new refcount block. It may also include a new refcount | |
187 | * table if the old refcount table is too small. | |
188 | * | |
189 | * Note that allocating clusters here needs some special care: | |
190 | * | |
191 | * - We can't use the normal qcow2_alloc_clusters(), it would try to | |
192 | * increase the refcount and very likely we would end up with an endless | |
193 | * recursion. Instead we must place the refcount blocks in a way that | |
194 | * they can describe them themselves. | |
195 | * | |
196 | * - We need to consider that at this point we are inside update_refcounts | |
b106ad91 KW |
197 | * and potentially doing an initial refcount increase. This means that |
198 | * some clusters have already been allocated by the caller, but their | |
199 | * refcount isn't accurate yet. If we allocate clusters for metadata, we | |
200 | * need to return -EAGAIN to signal the caller that it needs to restart | |
201 | * the search for free clusters. | |
92dcb59f KW |
202 | * |
203 | * - alloc_clusters_noref and qcow2_free_clusters may load a different | |
204 | * refcount block into the cache | |
205 | */ | |
206 | ||
29c1a730 KW |
207 | *refcount_block = NULL; |
208 | ||
209 | /* We write to the refcount table, so we might depend on L2 tables */ | |
9991923b SH |
210 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
211 | if (ret < 0) { | |
212 | return ret; | |
213 | } | |
92dcb59f KW |
214 | |
215 | /* Allocate the refcount block itself and mark it as used */ | |
2eaa8f63 KW |
216 | int64_t new_block = alloc_clusters_noref(bs, s->cluster_size); |
217 | if (new_block < 0) { | |
218 | return new_block; | |
219 | } | |
f7d0fe02 | 220 | |
f7d0fe02 | 221 | #ifdef DEBUG_ALLOC2 |
92dcb59f KW |
222 | fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64 |
223 | " at %" PRIx64 "\n", | |
224 | refcount_table_index, cluster_index << s->cluster_bits, new_block); | |
f7d0fe02 | 225 | #endif |
92dcb59f KW |
226 | |
227 | if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) { | |
25408c09 | 228 | /* Zero the new refcount block before updating it */ |
29c1a730 KW |
229 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
230 | (void**) refcount_block); | |
231 | if (ret < 0) { | |
232 | goto fail_block; | |
233 | } | |
234 | ||
235 | memset(*refcount_block, 0, s->cluster_size); | |
25408c09 | 236 | |
92dcb59f KW |
237 | /* The block describes itself, need to update the cache */ |
238 | int block_index = (new_block >> s->cluster_bits) & | |
239 | ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); | |
29c1a730 | 240 | (*refcount_block)[block_index] = cpu_to_be16(1); |
92dcb59f KW |
241 | } else { |
242 | /* Described somewhere else. This can recurse at most twice before we | |
243 | * arrive at a block that describes itself. */ | |
6cfcb9b8 KW |
244 | ret = update_refcount(bs, new_block, s->cluster_size, 1, |
245 | QCOW2_DISCARD_NEVER); | |
92dcb59f KW |
246 | if (ret < 0) { |
247 | goto fail_block; | |
248 | } | |
25408c09 | 249 | |
9991923b SH |
250 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
251 | if (ret < 0) { | |
252 | goto fail_block; | |
253 | } | |
1c4c2814 | 254 | |
25408c09 KW |
255 | /* Initialize the new refcount block only after updating its refcount, |
256 | * update_refcount uses the refcount cache itself */ | |
29c1a730 KW |
257 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
258 | (void**) refcount_block); | |
259 | if (ret < 0) { | |
260 | goto fail_block; | |
261 | } | |
262 | ||
263 | memset(*refcount_block, 0, s->cluster_size); | |
92dcb59f KW |
264 | } |
265 | ||
266 | /* Now the new refcount block needs to be written to disk */ | |
66f82cee | 267 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE); |
29c1a730 KW |
268 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block); |
269 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
92dcb59f KW |
270 | if (ret < 0) { |
271 | goto fail_block; | |
272 | } | |
273 | ||
274 | /* If the refcount table is big enough, just hook the block up there */ | |
275 | if (refcount_table_index < s->refcount_table_size) { | |
276 | uint64_t data64 = cpu_to_be64(new_block); | |
66f82cee | 277 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP); |
8b3b7206 | 278 | ret = bdrv_pwrite_sync(bs->file, |
92dcb59f KW |
279 | s->refcount_table_offset + refcount_table_index * sizeof(uint64_t), |
280 | &data64, sizeof(data64)); | |
281 | if (ret < 0) { | |
282 | goto fail_block; | |
283 | } | |
284 | ||
285 | s->refcount_table[refcount_table_index] = new_block; | |
b106ad91 KW |
286 | |
287 | /* The new refcount block may be where the caller intended to put its | |
288 | * data, so let it restart the search. */ | |
289 | return -EAGAIN; | |
29c1a730 KW |
290 | } |
291 | ||
292 | ret = qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block); | |
293 | if (ret < 0) { | |
294 | goto fail_block; | |
92dcb59f KW |
295 | } |
296 | ||
297 | /* | |
298 | * If we come here, we need to grow the refcount table. Again, a new | |
299 | * refcount table needs some space and we can't simply allocate to avoid | |
300 | * endless recursion. | |
301 | * | |
302 | * Therefore let's grab new refcount blocks at the end of the image, which | |
303 | * will describe themselves and the new refcount table. This way we can | |
304 | * reference them only in the new table and do the switch to the new | |
305 | * refcount table at once without producing an inconsistent state in | |
306 | * between. | |
307 | */ | |
66f82cee | 308 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW); |
8252278a | 309 | |
92dcb59f KW |
310 | /* Calculate the number of refcount blocks needed so far */ |
311 | uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT); | |
b106ad91 | 312 | uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters); |
92dcb59f KW |
313 | |
314 | /* And now we need at least one block more for the new metadata */ | |
315 | uint64_t table_size = next_refcount_table_size(s, blocks_used + 1); | |
316 | uint64_t last_table_size; | |
317 | uint64_t blocks_clusters; | |
318 | do { | |
a3548077 KW |
319 | uint64_t table_clusters = |
320 | size_to_clusters(s, table_size * sizeof(uint64_t)); | |
92dcb59f KW |
321 | blocks_clusters = 1 + |
322 | ((table_clusters + refcount_block_clusters - 1) | |
323 | / refcount_block_clusters); | |
324 | uint64_t meta_clusters = table_clusters + blocks_clusters; | |
325 | ||
326 | last_table_size = table_size; | |
327 | table_size = next_refcount_table_size(s, blocks_used + | |
328 | ((meta_clusters + refcount_block_clusters - 1) | |
329 | / refcount_block_clusters)); | |
330 | ||
331 | } while (last_table_size != table_size); | |
332 | ||
333 | #ifdef DEBUG_ALLOC2 | |
334 | fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n", | |
335 | s->refcount_table_size, table_size); | |
336 | #endif | |
337 | ||
338 | /* Create the new refcount table and blocks */ | |
339 | uint64_t meta_offset = (blocks_used * refcount_block_clusters) * | |
340 | s->cluster_size; | |
341 | uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size; | |
7267c094 AL |
342 | uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size); |
343 | uint64_t *new_table = g_malloc0(table_size * sizeof(uint64_t)); | |
92dcb59f | 344 | |
92dcb59f | 345 | /* Fill the new refcount table */ |
f7d0fe02 | 346 | memcpy(new_table, s->refcount_table, |
92dcb59f KW |
347 | s->refcount_table_size * sizeof(uint64_t)); |
348 | new_table[refcount_table_index] = new_block; | |
349 | ||
350 | int i; | |
351 | for (i = 0; i < blocks_clusters; i++) { | |
352 | new_table[blocks_used + i] = meta_offset + (i * s->cluster_size); | |
353 | } | |
354 | ||
355 | /* Fill the refcount blocks */ | |
356 | uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t)); | |
357 | int block = 0; | |
358 | for (i = 0; i < table_clusters + blocks_clusters; i++) { | |
359 | new_blocks[block++] = cpu_to_be16(1); | |
360 | } | |
361 | ||
362 | /* Write refcount blocks to disk */ | |
66f82cee | 363 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS); |
8b3b7206 | 364 | ret = bdrv_pwrite_sync(bs->file, meta_offset, new_blocks, |
92dcb59f | 365 | blocks_clusters * s->cluster_size); |
7267c094 | 366 | g_free(new_blocks); |
92dcb59f KW |
367 | if (ret < 0) { |
368 | goto fail_table; | |
369 | } | |
370 | ||
371 | /* Write refcount table to disk */ | |
372 | for(i = 0; i < table_size; i++) { | |
373 | cpu_to_be64s(&new_table[i]); | |
374 | } | |
375 | ||
66f82cee | 376 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE); |
8b3b7206 | 377 | ret = bdrv_pwrite_sync(bs->file, table_offset, new_table, |
92dcb59f KW |
378 | table_size * sizeof(uint64_t)); |
379 | if (ret < 0) { | |
380 | goto fail_table; | |
381 | } | |
382 | ||
383 | for(i = 0; i < table_size; i++) { | |
87267753 | 384 | be64_to_cpus(&new_table[i]); |
92dcb59f | 385 | } |
f7d0fe02 | 386 | |
92dcb59f KW |
387 | /* Hook up the new refcount table in the qcow2 header */ |
388 | uint8_t data[12]; | |
f7d0fe02 | 389 | cpu_to_be64w((uint64_t*)data, table_offset); |
92dcb59f | 390 | cpu_to_be32w((uint32_t*)(data + 8), table_clusters); |
66f82cee | 391 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE); |
8b3b7206 | 392 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, refcount_table_offset), |
92dcb59f KW |
393 | data, sizeof(data)); |
394 | if (ret < 0) { | |
395 | goto fail_table; | |
f2b7c8b3 KW |
396 | } |
397 | ||
92dcb59f KW |
398 | /* And switch it in memory */ |
399 | uint64_t old_table_offset = s->refcount_table_offset; | |
400 | uint64_t old_table_size = s->refcount_table_size; | |
401 | ||
7267c094 | 402 | g_free(s->refcount_table); |
f7d0fe02 | 403 | s->refcount_table = new_table; |
92dcb59f | 404 | s->refcount_table_size = table_size; |
f7d0fe02 KW |
405 | s->refcount_table_offset = table_offset; |
406 | ||
b106ad91 | 407 | /* Free old table. */ |
6cfcb9b8 KW |
408 | qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t), |
409 | QCOW2_DISCARD_OTHER); | |
f7d0fe02 | 410 | |
29c1a730 | 411 | ret = load_refcount_block(bs, new_block, (void**) refcount_block); |
92dcb59f | 412 | if (ret < 0) { |
29c1a730 | 413 | return ret; |
f7d0fe02 KW |
414 | } |
415 | ||
b106ad91 KW |
416 | /* If we were trying to do the initial refcount update for some cluster |
417 | * allocation, we might have used the same clusters to store newly | |
418 | * allocated metadata. Make the caller search some new space. */ | |
419 | return -EAGAIN; | |
f7d0fe02 | 420 | |
92dcb59f | 421 | fail_table: |
7267c094 | 422 | g_free(new_table); |
92dcb59f | 423 | fail_block: |
29c1a730 KW |
424 | if (*refcount_block != NULL) { |
425 | qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block); | |
3b88e52b | 426 | } |
29c1a730 | 427 | return ret; |
9923e05e KW |
428 | } |
429 | ||
0b919fae KW |
430 | void qcow2_process_discards(BlockDriverState *bs, int ret) |
431 | { | |
432 | BDRVQcowState *s = bs->opaque; | |
433 | Qcow2DiscardRegion *d, *next; | |
434 | ||
435 | QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) { | |
436 | QTAILQ_REMOVE(&s->discards, d, next); | |
437 | ||
438 | /* Discard is optional, ignore the return value */ | |
439 | if (ret >= 0) { | |
440 | bdrv_discard(bs->file, | |
441 | d->offset >> BDRV_SECTOR_BITS, | |
442 | d->bytes >> BDRV_SECTOR_BITS); | |
443 | } | |
444 | ||
445 | g_free(d); | |
446 | } | |
447 | } | |
448 | ||
449 | static void update_refcount_discard(BlockDriverState *bs, | |
450 | uint64_t offset, uint64_t length) | |
451 | { | |
452 | BDRVQcowState *s = bs->opaque; | |
453 | Qcow2DiscardRegion *d, *p, *next; | |
454 | ||
455 | QTAILQ_FOREACH(d, &s->discards, next) { | |
456 | uint64_t new_start = MIN(offset, d->offset); | |
457 | uint64_t new_end = MAX(offset + length, d->offset + d->bytes); | |
458 | ||
459 | if (new_end - new_start <= length + d->bytes) { | |
460 | /* There can't be any overlap, areas ending up here have no | |
461 | * references any more and therefore shouldn't get freed another | |
462 | * time. */ | |
463 | assert(d->bytes + length == new_end - new_start); | |
464 | d->offset = new_start; | |
465 | d->bytes = new_end - new_start; | |
466 | goto found; | |
467 | } | |
468 | } | |
469 | ||
470 | d = g_malloc(sizeof(*d)); | |
471 | *d = (Qcow2DiscardRegion) { | |
472 | .bs = bs, | |
473 | .offset = offset, | |
474 | .bytes = length, | |
475 | }; | |
476 | QTAILQ_INSERT_TAIL(&s->discards, d, next); | |
477 | ||
478 | found: | |
479 | /* Merge discard requests if they are adjacent now */ | |
480 | QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) { | |
481 | if (p == d | |
482 | || p->offset > d->offset + d->bytes | |
483 | || d->offset > p->offset + p->bytes) | |
484 | { | |
485 | continue; | |
486 | } | |
487 | ||
488 | /* Still no overlap possible */ | |
489 | assert(p->offset == d->offset + d->bytes | |
490 | || d->offset == p->offset + p->bytes); | |
491 | ||
492 | QTAILQ_REMOVE(&s->discards, p, next); | |
493 | d->offset = MIN(d->offset, p->offset); | |
494 | d->bytes += p->bytes; | |
495 | } | |
496 | } | |
497 | ||
f7d0fe02 | 498 | /* XXX: cache several refcount block clusters ? */ |
db3a964f | 499 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
6cfcb9b8 | 500 | int64_t offset, int64_t length, int addend, enum qcow2_discard_type type) |
f7d0fe02 KW |
501 | { |
502 | BDRVQcowState *s = bs->opaque; | |
503 | int64_t start, last, cluster_offset; | |
29c1a730 KW |
504 | uint16_t *refcount_block = NULL; |
505 | int64_t old_table_index = -1; | |
09508d13 | 506 | int ret; |
f7d0fe02 KW |
507 | |
508 | #ifdef DEBUG_ALLOC2 | |
35ee5e39 | 509 | fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n", |
f7d0fe02 KW |
510 | offset, length, addend); |
511 | #endif | |
7322afe7 | 512 | if (length < 0) { |
f7d0fe02 | 513 | return -EINVAL; |
7322afe7 KW |
514 | } else if (length == 0) { |
515 | return 0; | |
516 | } | |
517 | ||
29c1a730 KW |
518 | if (addend < 0) { |
519 | qcow2_cache_set_dependency(bs, s->refcount_block_cache, | |
520 | s->l2_table_cache); | |
521 | } | |
522 | ||
ac95acdb HT |
523 | start = start_of_cluster(s, offset); |
524 | last = start_of_cluster(s, offset + length - 1); | |
f7d0fe02 KW |
525 | for(cluster_offset = start; cluster_offset <= last; |
526 | cluster_offset += s->cluster_size) | |
527 | { | |
528 | int block_index, refcount; | |
529 | int64_t cluster_index = cluster_offset >> s->cluster_bits; | |
29c1a730 KW |
530 | int64_t table_index = |
531 | cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT); | |
f7d0fe02 | 532 | |
29c1a730 KW |
533 | /* Load the refcount block and allocate it if needed */ |
534 | if (table_index != old_table_index) { | |
535 | if (refcount_block) { | |
536 | ret = qcow2_cache_put(bs, s->refcount_block_cache, | |
537 | (void**) &refcount_block); | |
538 | if (ret < 0) { | |
539 | goto fail; | |
540 | } | |
541 | } | |
9923e05e | 542 | |
29c1a730 | 543 | ret = alloc_refcount_block(bs, cluster_index, &refcount_block); |
ed0df867 | 544 | if (ret < 0) { |
29c1a730 | 545 | goto fail; |
f7d0fe02 | 546 | } |
f7d0fe02 | 547 | } |
29c1a730 | 548 | old_table_index = table_index; |
f7d0fe02 | 549 | |
29c1a730 | 550 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block); |
f7d0fe02 KW |
551 | |
552 | /* we can update the count and save it */ | |
553 | block_index = cluster_index & | |
554 | ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); | |
f7d0fe02 | 555 | |
29c1a730 | 556 | refcount = be16_to_cpu(refcount_block[block_index]); |
f7d0fe02 | 557 | refcount += addend; |
09508d13 KW |
558 | if (refcount < 0 || refcount > 0xffff) { |
559 | ret = -EINVAL; | |
560 | goto fail; | |
561 | } | |
f7d0fe02 KW |
562 | if (refcount == 0 && cluster_index < s->free_cluster_index) { |
563 | s->free_cluster_index = cluster_index; | |
564 | } | |
29c1a730 | 565 | refcount_block[block_index] = cpu_to_be16(refcount); |
0b919fae | 566 | |
67af674e | 567 | if (refcount == 0 && s->discard_passthrough[type]) { |
0b919fae | 568 | update_refcount_discard(bs, cluster_offset, s->cluster_size); |
67af674e | 569 | } |
f7d0fe02 KW |
570 | } |
571 | ||
09508d13 KW |
572 | ret = 0; |
573 | fail: | |
0b919fae KW |
574 | if (!s->cache_discards) { |
575 | qcow2_process_discards(bs, ret); | |
576 | } | |
577 | ||
f7d0fe02 | 578 | /* Write last changed block to disk */ |
29c1a730 | 579 | if (refcount_block) { |
ed0df867 | 580 | int wret; |
29c1a730 KW |
581 | wret = qcow2_cache_put(bs, s->refcount_block_cache, |
582 | (void**) &refcount_block); | |
ed0df867 KW |
583 | if (wret < 0) { |
584 | return ret < 0 ? ret : wret; | |
f7d0fe02 KW |
585 | } |
586 | } | |
587 | ||
09508d13 KW |
588 | /* |
589 | * Try do undo any updates if an error is returned (This may succeed in | |
590 | * some cases like ENOSPC for allocating a new refcount block) | |
591 | */ | |
592 | if (ret < 0) { | |
593 | int dummy; | |
6cfcb9b8 KW |
594 | dummy = update_refcount(bs, offset, cluster_offset - offset, -addend, |
595 | QCOW2_DISCARD_NEVER); | |
83e3f76c | 596 | (void)dummy; |
09508d13 KW |
597 | } |
598 | ||
599 | return ret; | |
f7d0fe02 KW |
600 | } |
601 | ||
018faafd KW |
602 | /* |
603 | * Increases or decreases the refcount of a given cluster by one. | |
604 | * addend must be 1 or -1. | |
605 | * | |
606 | * If the return value is non-negative, it is the new refcount of the cluster. | |
607 | * If it is negative, it is -errno and indicates an error. | |
608 | */ | |
32b6444d HR |
609 | int qcow2_update_cluster_refcount(BlockDriverState *bs, |
610 | int64_t cluster_index, | |
611 | int addend, | |
612 | enum qcow2_discard_type type) | |
f7d0fe02 KW |
613 | { |
614 | BDRVQcowState *s = bs->opaque; | |
615 | int ret; | |
616 | ||
6cfcb9b8 KW |
617 | ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend, |
618 | type); | |
f7d0fe02 KW |
619 | if (ret < 0) { |
620 | return ret; | |
621 | } | |
622 | ||
623 | return get_refcount(bs, cluster_index); | |
624 | } | |
625 | ||
626 | ||
627 | ||
628 | /*********************************************************/ | |
629 | /* cluster allocation functions */ | |
630 | ||
631 | ||
632 | ||
633 | /* return < 0 if error */ | |
634 | static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size) | |
635 | { | |
636 | BDRVQcowState *s = bs->opaque; | |
2eaa8f63 | 637 | int i, nb_clusters, refcount; |
f7d0fe02 KW |
638 | |
639 | nb_clusters = size_to_clusters(s, size); | |
640 | retry: | |
641 | for(i = 0; i < nb_clusters; i++) { | |
508e0893 | 642 | int64_t next_cluster_index = s->free_cluster_index++; |
2eaa8f63 KW |
643 | refcount = get_refcount(bs, next_cluster_index); |
644 | ||
645 | if (refcount < 0) { | |
646 | return refcount; | |
647 | } else if (refcount != 0) { | |
f7d0fe02 | 648 | goto retry; |
2eaa8f63 | 649 | } |
f7d0fe02 KW |
650 | } |
651 | #ifdef DEBUG_ALLOC2 | |
35ee5e39 | 652 | fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n", |
f7d0fe02 KW |
653 | size, |
654 | (s->free_cluster_index - nb_clusters) << s->cluster_bits); | |
655 | #endif | |
656 | return (s->free_cluster_index - nb_clusters) << s->cluster_bits; | |
657 | } | |
658 | ||
ed6ccf0f | 659 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size) |
f7d0fe02 KW |
660 | { |
661 | int64_t offset; | |
db3a964f | 662 | int ret; |
f7d0fe02 | 663 | |
66f82cee | 664 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC); |
b106ad91 KW |
665 | do { |
666 | offset = alloc_clusters_noref(bs, size); | |
667 | if (offset < 0) { | |
668 | return offset; | |
669 | } | |
670 | ||
671 | ret = update_refcount(bs, offset, size, 1, QCOW2_DISCARD_NEVER); | |
672 | } while (ret == -EAGAIN); | |
2eaa8f63 | 673 | |
db3a964f KW |
674 | if (ret < 0) { |
675 | return ret; | |
676 | } | |
1c4c2814 | 677 | |
f7d0fe02 KW |
678 | return offset; |
679 | } | |
680 | ||
256900b1 KW |
681 | int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
682 | int nb_clusters) | |
683 | { | |
684 | BDRVQcowState *s = bs->opaque; | |
685 | uint64_t cluster_index; | |
33304ec9 HT |
686 | uint64_t i; |
687 | int refcount, ret; | |
688 | ||
689 | assert(nb_clusters >= 0); | |
690 | if (nb_clusters == 0) { | |
691 | return 0; | |
692 | } | |
256900b1 | 693 | |
b106ad91 KW |
694 | do { |
695 | /* Check how many clusters there are free */ | |
696 | cluster_index = offset >> s->cluster_bits; | |
697 | for(i = 0; i < nb_clusters; i++) { | |
698 | refcount = get_refcount(bs, cluster_index++); | |
699 | ||
700 | if (refcount < 0) { | |
701 | return refcount; | |
702 | } else if (refcount != 0) { | |
703 | break; | |
704 | } | |
256900b1 | 705 | } |
256900b1 | 706 | |
b106ad91 KW |
707 | /* And then allocate them */ |
708 | ret = update_refcount(bs, offset, i << s->cluster_bits, 1, | |
709 | QCOW2_DISCARD_NEVER); | |
710 | } while (ret == -EAGAIN); | |
f24423bd | 711 | |
256900b1 KW |
712 | if (ret < 0) { |
713 | return ret; | |
714 | } | |
715 | ||
716 | return i; | |
717 | } | |
718 | ||
f7d0fe02 KW |
719 | /* only used to allocate compressed sectors. We try to allocate |
720 | contiguous sectors. size must be <= cluster_size */ | |
ed6ccf0f | 721 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size) |
f7d0fe02 KW |
722 | { |
723 | BDRVQcowState *s = bs->opaque; | |
724 | int64_t offset, cluster_offset; | |
725 | int free_in_cluster; | |
726 | ||
66f82cee | 727 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES); |
f7d0fe02 KW |
728 | assert(size > 0 && size <= s->cluster_size); |
729 | if (s->free_byte_offset == 0) { | |
206e6d85 SH |
730 | offset = qcow2_alloc_clusters(bs, s->cluster_size); |
731 | if (offset < 0) { | |
732 | return offset; | |
5d757b56 | 733 | } |
206e6d85 | 734 | s->free_byte_offset = offset; |
f7d0fe02 KW |
735 | } |
736 | redo: | |
737 | free_in_cluster = s->cluster_size - | |
ac95acdb | 738 | offset_into_cluster(s, s->free_byte_offset); |
f7d0fe02 KW |
739 | if (size <= free_in_cluster) { |
740 | /* enough space in current cluster */ | |
741 | offset = s->free_byte_offset; | |
742 | s->free_byte_offset += size; | |
743 | free_in_cluster -= size; | |
744 | if (free_in_cluster == 0) | |
745 | s->free_byte_offset = 0; | |
ac95acdb | 746 | if (offset_into_cluster(s, offset) != 0) |
32b6444d HR |
747 | qcow2_update_cluster_refcount(bs, offset >> s->cluster_bits, 1, |
748 | QCOW2_DISCARD_NEVER); | |
f7d0fe02 | 749 | } else { |
ed6ccf0f | 750 | offset = qcow2_alloc_clusters(bs, s->cluster_size); |
5d757b56 KW |
751 | if (offset < 0) { |
752 | return offset; | |
753 | } | |
ac95acdb | 754 | cluster_offset = start_of_cluster(s, s->free_byte_offset); |
f7d0fe02 KW |
755 | if ((cluster_offset + s->cluster_size) == offset) { |
756 | /* we are lucky: contiguous data */ | |
757 | offset = s->free_byte_offset; | |
32b6444d HR |
758 | qcow2_update_cluster_refcount(bs, offset >> s->cluster_bits, 1, |
759 | QCOW2_DISCARD_NEVER); | |
f7d0fe02 KW |
760 | s->free_byte_offset += size; |
761 | } else { | |
762 | s->free_byte_offset = offset; | |
763 | goto redo; | |
764 | } | |
765 | } | |
29216ed1 | 766 | |
c1f5bafd | 767 | /* The cluster refcount was incremented, either by qcow2_alloc_clusters() |
32b6444d HR |
768 | * or explicitly by qcow2_update_cluster_refcount(). Refcount blocks must |
769 | * be flushed before the caller's L2 table updates. | |
c1f5bafd SH |
770 | */ |
771 | qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); | |
f7d0fe02 KW |
772 | return offset; |
773 | } | |
774 | ||
ed6ccf0f | 775 | void qcow2_free_clusters(BlockDriverState *bs, |
6cfcb9b8 KW |
776 | int64_t offset, int64_t size, |
777 | enum qcow2_discard_type type) | |
f7d0fe02 | 778 | { |
db3a964f KW |
779 | int ret; |
780 | ||
66f82cee | 781 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE); |
6cfcb9b8 | 782 | ret = update_refcount(bs, offset, size, -1, type); |
db3a964f KW |
783 | if (ret < 0) { |
784 | fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret)); | |
003fad6e | 785 | /* TODO Remember the clusters to free them later and avoid leaking */ |
db3a964f | 786 | } |
f7d0fe02 KW |
787 | } |
788 | ||
45aba42f | 789 | /* |
c7a4c37a KW |
790 | * Free a cluster using its L2 entry (handles clusters of all types, e.g. |
791 | * normal cluster, compressed cluster, etc.) | |
45aba42f | 792 | */ |
6cfcb9b8 KW |
793 | void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry, |
794 | int nb_clusters, enum qcow2_discard_type type) | |
45aba42f KW |
795 | { |
796 | BDRVQcowState *s = bs->opaque; | |
797 | ||
c7a4c37a KW |
798 | switch (qcow2_get_cluster_type(l2_entry)) { |
799 | case QCOW2_CLUSTER_COMPRESSED: | |
800 | { | |
801 | int nb_csectors; | |
802 | nb_csectors = ((l2_entry >> s->csize_shift) & | |
803 | s->csize_mask) + 1; | |
804 | qcow2_free_clusters(bs, | |
805 | (l2_entry & s->cluster_offset_mask) & ~511, | |
6cfcb9b8 | 806 | nb_csectors * 512, type); |
c7a4c37a KW |
807 | } |
808 | break; | |
809 | case QCOW2_CLUSTER_NORMAL: | |
8f730dd2 HR |
810 | case QCOW2_CLUSTER_ZERO: |
811 | if (l2_entry & L2E_OFFSET_MASK) { | |
812 | qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK, | |
813 | nb_clusters << s->cluster_bits, type); | |
814 | } | |
c7a4c37a KW |
815 | break; |
816 | case QCOW2_CLUSTER_UNALLOCATED: | |
817 | break; | |
818 | default: | |
819 | abort(); | |
45aba42f | 820 | } |
45aba42f KW |
821 | } |
822 | ||
f7d0fe02 KW |
823 | |
824 | ||
825 | /*********************************************************/ | |
826 | /* snapshots and image creation */ | |
827 | ||
828 | ||
829 | ||
f7d0fe02 | 830 | /* update the refcounts of snapshots and the copied flag */ |
ed6ccf0f KW |
831 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
832 | int64_t l1_table_offset, int l1_size, int addend) | |
f7d0fe02 KW |
833 | { |
834 | BDRVQcowState *s = bs->opaque; | |
835 | uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated; | |
836 | int64_t old_offset, old_l2_offset; | |
93913dfd | 837 | int i, j, l1_modified = 0, nb_csectors, refcount; |
29c1a730 | 838 | int ret; |
f7d0fe02 KW |
839 | |
840 | l2_table = NULL; | |
841 | l1_table = NULL; | |
842 | l1_size2 = l1_size * sizeof(uint64_t); | |
43a0cac4 | 843 | |
0b919fae KW |
844 | s->cache_discards = true; |
845 | ||
43a0cac4 KW |
846 | /* WARNING: qcow2_snapshot_goto relies on this function not using the |
847 | * l1_table_offset when it is the current s->l1_table_offset! Be careful | |
848 | * when changing this! */ | |
f7d0fe02 | 849 | if (l1_table_offset != s->l1_table_offset) { |
6528499f | 850 | l1_table = g_malloc0(align_offset(l1_size2, 512)); |
f7d0fe02 | 851 | l1_allocated = 1; |
c2bc78b6 KW |
852 | |
853 | ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2); | |
854 | if (ret < 0) { | |
f7d0fe02 | 855 | goto fail; |
93913dfd KW |
856 | } |
857 | ||
f7d0fe02 KW |
858 | for(i = 0;i < l1_size; i++) |
859 | be64_to_cpus(&l1_table[i]); | |
860 | } else { | |
861 | assert(l1_size == s->l1_size); | |
862 | l1_table = s->l1_table; | |
863 | l1_allocated = 0; | |
864 | } | |
865 | ||
f7d0fe02 KW |
866 | for(i = 0; i < l1_size; i++) { |
867 | l2_offset = l1_table[i]; | |
868 | if (l2_offset) { | |
869 | old_l2_offset = l2_offset; | |
8e37f681 | 870 | l2_offset &= L1E_OFFSET_MASK; |
29c1a730 KW |
871 | |
872 | ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, | |
873 | (void**) &l2_table); | |
874 | if (ret < 0) { | |
f7d0fe02 | 875 | goto fail; |
29c1a730 KW |
876 | } |
877 | ||
f7d0fe02 | 878 | for(j = 0; j < s->l2_size; j++) { |
8b81a7b6 HR |
879 | uint64_t cluster_index; |
880 | ||
f7d0fe02 | 881 | offset = be64_to_cpu(l2_table[j]); |
8b81a7b6 HR |
882 | old_offset = offset; |
883 | offset &= ~QCOW_OFLAG_COPIED; | |
884 | ||
885 | switch (qcow2_get_cluster_type(offset)) { | |
886 | case QCOW2_CLUSTER_COMPRESSED: | |
f7d0fe02 KW |
887 | nb_csectors = ((offset >> s->csize_shift) & |
888 | s->csize_mask) + 1; | |
db3a964f | 889 | if (addend != 0) { |
db3a964f KW |
890 | ret = update_refcount(bs, |
891 | (offset & s->cluster_offset_mask) & ~511, | |
6cfcb9b8 KW |
892 | nb_csectors * 512, addend, |
893 | QCOW2_DISCARD_SNAPSHOT); | |
db3a964f KW |
894 | if (ret < 0) { |
895 | goto fail; | |
896 | } | |
897 | } | |
f7d0fe02 KW |
898 | /* compressed clusters are never modified */ |
899 | refcount = 2; | |
8b81a7b6 HR |
900 | break; |
901 | ||
902 | case QCOW2_CLUSTER_NORMAL: | |
903 | case QCOW2_CLUSTER_ZERO: | |
904 | cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits; | |
905 | if (!cluster_index) { | |
906 | /* unallocated */ | |
907 | refcount = 0; | |
908 | break; | |
909 | } | |
f7d0fe02 | 910 | if (addend != 0) { |
32b6444d HR |
911 | refcount = qcow2_update_cluster_refcount(bs, |
912 | cluster_index, addend, | |
913 | QCOW2_DISCARD_SNAPSHOT); | |
f7d0fe02 | 914 | } else { |
8e37f681 | 915 | refcount = get_refcount(bs, cluster_index); |
f7d0fe02 | 916 | } |
018faafd KW |
917 | |
918 | if (refcount < 0) { | |
c2bc78b6 | 919 | ret = refcount; |
018faafd KW |
920 | goto fail; |
921 | } | |
8b81a7b6 | 922 | break; |
f7d0fe02 | 923 | |
8b81a7b6 HR |
924 | case QCOW2_CLUSTER_UNALLOCATED: |
925 | refcount = 0; | |
926 | break; | |
927 | ||
928 | default: | |
929 | abort(); | |
930 | } | |
931 | ||
932 | if (refcount == 1) { | |
933 | offset |= QCOW_OFLAG_COPIED; | |
934 | } | |
935 | if (offset != old_offset) { | |
936 | if (addend > 0) { | |
937 | qcow2_cache_set_dependency(bs, s->l2_table_cache, | |
938 | s->refcount_block_cache); | |
f7d0fe02 | 939 | } |
8b81a7b6 HR |
940 | l2_table[j] = cpu_to_be64(offset); |
941 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); | |
f7d0fe02 KW |
942 | } |
943 | } | |
29c1a730 KW |
944 | |
945 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); | |
946 | if (ret < 0) { | |
947 | goto fail; | |
f7d0fe02 KW |
948 | } |
949 | ||
29c1a730 | 950 | |
f7d0fe02 | 951 | if (addend != 0) { |
32b6444d HR |
952 | refcount = qcow2_update_cluster_refcount(bs, l2_offset >> |
953 | s->cluster_bits, addend, QCOW2_DISCARD_SNAPSHOT); | |
f7d0fe02 KW |
954 | } else { |
955 | refcount = get_refcount(bs, l2_offset >> s->cluster_bits); | |
956 | } | |
018faafd | 957 | if (refcount < 0) { |
c2bc78b6 | 958 | ret = refcount; |
018faafd KW |
959 | goto fail; |
960 | } else if (refcount == 1) { | |
f7d0fe02 KW |
961 | l2_offset |= QCOW_OFLAG_COPIED; |
962 | } | |
963 | if (l2_offset != old_l2_offset) { | |
964 | l1_table[i] = l2_offset; | |
965 | l1_modified = 1; | |
966 | } | |
967 | } | |
968 | } | |
93913dfd | 969 | |
2154f24e | 970 | ret = bdrv_flush(bs); |
93913dfd KW |
971 | fail: |
972 | if (l2_table) { | |
973 | qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); | |
974 | } | |
975 | ||
0b919fae KW |
976 | s->cache_discards = false; |
977 | qcow2_process_discards(bs, ret); | |
978 | ||
43a0cac4 | 979 | /* Update L1 only if it isn't deleted anyway (addend = -1) */ |
c2b6ff51 KW |
980 | if (ret == 0 && addend >= 0 && l1_modified) { |
981 | for (i = 0; i < l1_size; i++) { | |
f7d0fe02 | 982 | cpu_to_be64s(&l1_table[i]); |
c2b6ff51 KW |
983 | } |
984 | ||
985 | ret = bdrv_pwrite_sync(bs->file, l1_table_offset, l1_table, l1_size2); | |
986 | ||
987 | for (i = 0; i < l1_size; i++) { | |
f7d0fe02 | 988 | be64_to_cpus(&l1_table[i]); |
c2b6ff51 | 989 | } |
f7d0fe02 KW |
990 | } |
991 | if (l1_allocated) | |
7267c094 | 992 | g_free(l1_table); |
93913dfd | 993 | return ret; |
f7d0fe02 KW |
994 | } |
995 | ||
996 | ||
997 | ||
998 | ||
999 | /*********************************************************/ | |
1000 | /* refcount checking functions */ | |
1001 | ||
1002 | ||
1003 | ||
1004 | /* | |
1005 | * Increases the refcount for a range of clusters in a given refcount table. | |
1006 | * This is used to construct a temporary refcount table out of L1 and L2 tables | |
1007 | * which can be compared the the refcount table saved in the image. | |
1008 | * | |
9ac228e0 | 1009 | * Modifies the number of errors in res. |
f7d0fe02 | 1010 | */ |
9ac228e0 KW |
1011 | static void inc_refcounts(BlockDriverState *bs, |
1012 | BdrvCheckResult *res, | |
f7d0fe02 KW |
1013 | uint16_t *refcount_table, |
1014 | int refcount_table_size, | |
1015 | int64_t offset, int64_t size) | |
1016 | { | |
1017 | BDRVQcowState *s = bs->opaque; | |
1018 | int64_t start, last, cluster_offset; | |
1019 | int k; | |
f7d0fe02 KW |
1020 | |
1021 | if (size <= 0) | |
9ac228e0 | 1022 | return; |
f7d0fe02 | 1023 | |
ac95acdb HT |
1024 | start = start_of_cluster(s, offset); |
1025 | last = start_of_cluster(s, offset + size - 1); | |
f7d0fe02 KW |
1026 | for(cluster_offset = start; cluster_offset <= last; |
1027 | cluster_offset += s->cluster_size) { | |
1028 | k = cluster_offset >> s->cluster_bits; | |
9ac228e0 | 1029 | if (k < 0) { |
f7d0fe02 KW |
1030 | fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n", |
1031 | cluster_offset); | |
9ac228e0 KW |
1032 | res->corruptions++; |
1033 | } else if (k >= refcount_table_size) { | |
1034 | fprintf(stderr, "Warning: cluster offset=0x%" PRIx64 " is after " | |
1035 | "the end of the image file, can't properly check refcounts.\n", | |
1036 | cluster_offset); | |
1037 | res->check_errors++; | |
f7d0fe02 KW |
1038 | } else { |
1039 | if (++refcount_table[k] == 0) { | |
1040 | fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64 | |
1041 | "\n", cluster_offset); | |
9ac228e0 | 1042 | res->corruptions++; |
f7d0fe02 KW |
1043 | } |
1044 | } | |
1045 | } | |
f7d0fe02 KW |
1046 | } |
1047 | ||
801f7044 SH |
1048 | /* Flags for check_refcounts_l1() and check_refcounts_l2() */ |
1049 | enum { | |
fba31bae | 1050 | CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */ |
801f7044 SH |
1051 | }; |
1052 | ||
f7d0fe02 KW |
1053 | /* |
1054 | * Increases the refcount in the given refcount table for the all clusters | |
1055 | * referenced in the L2 table. While doing so, performs some checks on L2 | |
1056 | * entries. | |
1057 | * | |
1058 | * Returns the number of errors found by the checks or -errno if an internal | |
1059 | * error occurred. | |
1060 | */ | |
9ac228e0 | 1061 | static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, |
f7d0fe02 | 1062 | uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset, |
801f7044 | 1063 | int flags) |
f7d0fe02 KW |
1064 | { |
1065 | BDRVQcowState *s = bs->opaque; | |
afdf0abe | 1066 | uint64_t *l2_table, l2_entry; |
fba31bae | 1067 | uint64_t next_contiguous_offset = 0; |
4f6ed88c | 1068 | int i, l2_size, nb_csectors; |
f7d0fe02 KW |
1069 | |
1070 | /* Read L2 table from disk */ | |
1071 | l2_size = s->l2_size * sizeof(uint64_t); | |
7267c094 | 1072 | l2_table = g_malloc(l2_size); |
f7d0fe02 | 1073 | |
66f82cee | 1074 | if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size) |
f7d0fe02 KW |
1075 | goto fail; |
1076 | ||
1077 | /* Do the actual checks */ | |
1078 | for(i = 0; i < s->l2_size; i++) { | |
afdf0abe KW |
1079 | l2_entry = be64_to_cpu(l2_table[i]); |
1080 | ||
1081 | switch (qcow2_get_cluster_type(l2_entry)) { | |
1082 | case QCOW2_CLUSTER_COMPRESSED: | |
1083 | /* Compressed clusters don't have QCOW_OFLAG_COPIED */ | |
1084 | if (l2_entry & QCOW_OFLAG_COPIED) { | |
1085 | fprintf(stderr, "ERROR: cluster %" PRId64 ": " | |
1086 | "copied flag must never be set for compressed " | |
1087 | "clusters\n", l2_entry >> s->cluster_bits); | |
1088 | l2_entry &= ~QCOW_OFLAG_COPIED; | |
1089 | res->corruptions++; | |
1090 | } | |
f7d0fe02 | 1091 | |
afdf0abe KW |
1092 | /* Mark cluster as used */ |
1093 | nb_csectors = ((l2_entry >> s->csize_shift) & | |
1094 | s->csize_mask) + 1; | |
1095 | l2_entry &= s->cluster_offset_mask; | |
1096 | inc_refcounts(bs, res, refcount_table, refcount_table_size, | |
1097 | l2_entry & ~511, nb_csectors * 512); | |
fba31bae SH |
1098 | |
1099 | if (flags & CHECK_FRAG_INFO) { | |
1100 | res->bfi.allocated_clusters++; | |
4db35162 | 1101 | res->bfi.compressed_clusters++; |
fba31bae SH |
1102 | |
1103 | /* Compressed clusters are fragmented by nature. Since they | |
1104 | * take up sub-sector space but we only have sector granularity | |
1105 | * I/O we need to re-read the same sectors even for adjacent | |
1106 | * compressed clusters. | |
1107 | */ | |
1108 | res->bfi.fragmented_clusters++; | |
1109 | } | |
afdf0abe | 1110 | break; |
f7d0fe02 | 1111 | |
6377af48 KW |
1112 | case QCOW2_CLUSTER_ZERO: |
1113 | if ((l2_entry & L2E_OFFSET_MASK) == 0) { | |
1114 | break; | |
1115 | } | |
1116 | /* fall through */ | |
1117 | ||
afdf0abe KW |
1118 | case QCOW2_CLUSTER_NORMAL: |
1119 | { | |
afdf0abe | 1120 | uint64_t offset = l2_entry & L2E_OFFSET_MASK; |
f7d0fe02 | 1121 | |
fba31bae SH |
1122 | if (flags & CHECK_FRAG_INFO) { |
1123 | res->bfi.allocated_clusters++; | |
1124 | if (next_contiguous_offset && | |
1125 | offset != next_contiguous_offset) { | |
1126 | res->bfi.fragmented_clusters++; | |
1127 | } | |
1128 | next_contiguous_offset = offset + s->cluster_size; | |
1129 | } | |
1130 | ||
afdf0abe KW |
1131 | /* Mark cluster as used */ |
1132 | inc_refcounts(bs, res, refcount_table,refcount_table_size, | |
1133 | offset, s->cluster_size); | |
1134 | ||
1135 | /* Correct offsets are cluster aligned */ | |
ac95acdb | 1136 | if (offset_into_cluster(s, offset)) { |
afdf0abe KW |
1137 | fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not " |
1138 | "properly aligned; L2 entry corrupted.\n", offset); | |
1139 | res->corruptions++; | |
1140 | } | |
1141 | break; | |
1142 | } | |
1143 | ||
1144 | case QCOW2_CLUSTER_UNALLOCATED: | |
1145 | break; | |
1146 | ||
1147 | default: | |
1148 | abort(); | |
f7d0fe02 KW |
1149 | } |
1150 | } | |
1151 | ||
7267c094 | 1152 | g_free(l2_table); |
9ac228e0 | 1153 | return 0; |
f7d0fe02 KW |
1154 | |
1155 | fail: | |
9ac228e0 | 1156 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n"); |
7267c094 | 1157 | g_free(l2_table); |
f7d0fe02 KW |
1158 | return -EIO; |
1159 | } | |
1160 | ||
1161 | /* | |
1162 | * Increases the refcount for the L1 table, its L2 tables and all referenced | |
1163 | * clusters in the given refcount table. While doing so, performs some checks | |
1164 | * on L1 and L2 entries. | |
1165 | * | |
1166 | * Returns the number of errors found by the checks or -errno if an internal | |
1167 | * error occurred. | |
1168 | */ | |
1169 | static int check_refcounts_l1(BlockDriverState *bs, | |
9ac228e0 | 1170 | BdrvCheckResult *res, |
f7d0fe02 KW |
1171 | uint16_t *refcount_table, |
1172 | int refcount_table_size, | |
1173 | int64_t l1_table_offset, int l1_size, | |
801f7044 | 1174 | int flags) |
f7d0fe02 KW |
1175 | { |
1176 | BDRVQcowState *s = bs->opaque; | |
1177 | uint64_t *l1_table, l2_offset, l1_size2; | |
4f6ed88c | 1178 | int i, ret; |
f7d0fe02 KW |
1179 | |
1180 | l1_size2 = l1_size * sizeof(uint64_t); | |
1181 | ||
1182 | /* Mark L1 table as used */ | |
9ac228e0 KW |
1183 | inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1184 | l1_table_offset, l1_size2); | |
f7d0fe02 KW |
1185 | |
1186 | /* Read L1 table entries from disk */ | |
702ef63f KW |
1187 | if (l1_size2 == 0) { |
1188 | l1_table = NULL; | |
1189 | } else { | |
7267c094 | 1190 | l1_table = g_malloc(l1_size2); |
66f82cee | 1191 | if (bdrv_pread(bs->file, l1_table_offset, |
702ef63f KW |
1192 | l1_table, l1_size2) != l1_size2) |
1193 | goto fail; | |
1194 | for(i = 0;i < l1_size; i++) | |
1195 | be64_to_cpus(&l1_table[i]); | |
1196 | } | |
f7d0fe02 KW |
1197 | |
1198 | /* Do the actual checks */ | |
1199 | for(i = 0; i < l1_size; i++) { | |
1200 | l2_offset = l1_table[i]; | |
1201 | if (l2_offset) { | |
f7d0fe02 | 1202 | /* Mark L2 table as used */ |
afdf0abe | 1203 | l2_offset &= L1E_OFFSET_MASK; |
9ac228e0 KW |
1204 | inc_refcounts(bs, res, refcount_table, refcount_table_size, |
1205 | l2_offset, s->cluster_size); | |
f7d0fe02 KW |
1206 | |
1207 | /* L2 tables are cluster aligned */ | |
ac95acdb | 1208 | if (offset_into_cluster(s, l2_offset)) { |
f7d0fe02 KW |
1209 | fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not " |
1210 | "cluster aligned; L1 entry corrupted\n", l2_offset); | |
9ac228e0 | 1211 | res->corruptions++; |
f7d0fe02 KW |
1212 | } |
1213 | ||
1214 | /* Process and check L2 entries */ | |
9ac228e0 | 1215 | ret = check_refcounts_l2(bs, res, refcount_table, |
801f7044 | 1216 | refcount_table_size, l2_offset, flags); |
f7d0fe02 KW |
1217 | if (ret < 0) { |
1218 | goto fail; | |
1219 | } | |
f7d0fe02 KW |
1220 | } |
1221 | } | |
7267c094 | 1222 | g_free(l1_table); |
9ac228e0 | 1223 | return 0; |
f7d0fe02 KW |
1224 | |
1225 | fail: | |
1226 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n"); | |
9ac228e0 | 1227 | res->check_errors++; |
7267c094 | 1228 | g_free(l1_table); |
f7d0fe02 KW |
1229 | return -EIO; |
1230 | } | |
1231 | ||
4f6ed88c HR |
1232 | /* |
1233 | * Checks the OFLAG_COPIED flag for all L1 and L2 entries. | |
1234 | * | |
1235 | * This function does not print an error message nor does it increment | |
1236 | * check_errors if get_refcount fails (this is because such an error will have | |
1237 | * been already detected and sufficiently signaled by the calling function | |
1238 | * (qcow2_check_refcounts) by the time this function is called). | |
1239 | */ | |
e23e400e HR |
1240 | static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res, |
1241 | BdrvCheckMode fix) | |
4f6ed88c HR |
1242 | { |
1243 | BDRVQcowState *s = bs->opaque; | |
1244 | uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size); | |
1245 | int ret; | |
1246 | int refcount; | |
1247 | int i, j; | |
1248 | ||
1249 | for (i = 0; i < s->l1_size; i++) { | |
1250 | uint64_t l1_entry = s->l1_table[i]; | |
1251 | uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK; | |
e23e400e | 1252 | bool l2_dirty = false; |
4f6ed88c HR |
1253 | |
1254 | if (!l2_offset) { | |
1255 | continue; | |
1256 | } | |
1257 | ||
1258 | refcount = get_refcount(bs, l2_offset >> s->cluster_bits); | |
1259 | if (refcount < 0) { | |
1260 | /* don't print message nor increment check_errors */ | |
1261 | continue; | |
1262 | } | |
1263 | if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) { | |
e23e400e | 1264 | fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d " |
4f6ed88c | 1265 | "l1_entry=%" PRIx64 " refcount=%d\n", |
e23e400e HR |
1266 | fix & BDRV_FIX_ERRORS ? "Repairing" : |
1267 | "ERROR", | |
4f6ed88c | 1268 | i, l1_entry, refcount); |
e23e400e HR |
1269 | if (fix & BDRV_FIX_ERRORS) { |
1270 | s->l1_table[i] = refcount == 1 | |
1271 | ? l1_entry | QCOW_OFLAG_COPIED | |
1272 | : l1_entry & ~QCOW_OFLAG_COPIED; | |
1273 | ret = qcow2_write_l1_entry(bs, i); | |
1274 | if (ret < 0) { | |
1275 | res->check_errors++; | |
1276 | goto fail; | |
1277 | } | |
1278 | res->corruptions_fixed++; | |
1279 | } else { | |
1280 | res->corruptions++; | |
1281 | } | |
4f6ed88c HR |
1282 | } |
1283 | ||
1284 | ret = bdrv_pread(bs->file, l2_offset, l2_table, | |
1285 | s->l2_size * sizeof(uint64_t)); | |
1286 | if (ret < 0) { | |
1287 | fprintf(stderr, "ERROR: Could not read L2 table: %s\n", | |
1288 | strerror(-ret)); | |
1289 | res->check_errors++; | |
1290 | goto fail; | |
1291 | } | |
1292 | ||
1293 | for (j = 0; j < s->l2_size; j++) { | |
1294 | uint64_t l2_entry = be64_to_cpu(l2_table[j]); | |
1295 | uint64_t data_offset = l2_entry & L2E_OFFSET_MASK; | |
1296 | int cluster_type = qcow2_get_cluster_type(l2_entry); | |
1297 | ||
1298 | if ((cluster_type == QCOW2_CLUSTER_NORMAL) || | |
1299 | ((cluster_type == QCOW2_CLUSTER_ZERO) && (data_offset != 0))) { | |
1300 | refcount = get_refcount(bs, data_offset >> s->cluster_bits); | |
1301 | if (refcount < 0) { | |
1302 | /* don't print message nor increment check_errors */ | |
1303 | continue; | |
1304 | } | |
1305 | if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) { | |
e23e400e | 1306 | fprintf(stderr, "%s OFLAG_COPIED data cluster: " |
4f6ed88c | 1307 | "l2_entry=%" PRIx64 " refcount=%d\n", |
e23e400e HR |
1308 | fix & BDRV_FIX_ERRORS ? "Repairing" : |
1309 | "ERROR", | |
4f6ed88c | 1310 | l2_entry, refcount); |
e23e400e HR |
1311 | if (fix & BDRV_FIX_ERRORS) { |
1312 | l2_table[j] = cpu_to_be64(refcount == 1 | |
1313 | ? l2_entry | QCOW_OFLAG_COPIED | |
1314 | : l2_entry & ~QCOW_OFLAG_COPIED); | |
1315 | l2_dirty = true; | |
1316 | res->corruptions_fixed++; | |
1317 | } else { | |
1318 | res->corruptions++; | |
1319 | } | |
4f6ed88c HR |
1320 | } |
1321 | } | |
1322 | } | |
e23e400e HR |
1323 | |
1324 | if (l2_dirty) { | |
231bb267 HR |
1325 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, |
1326 | l2_offset, s->cluster_size); | |
e23e400e HR |
1327 | if (ret < 0) { |
1328 | fprintf(stderr, "ERROR: Could not write L2 table; metadata " | |
1329 | "overlap check failed: %s\n", strerror(-ret)); | |
1330 | res->check_errors++; | |
1331 | goto fail; | |
1332 | } | |
1333 | ||
1334 | ret = bdrv_pwrite(bs->file, l2_offset, l2_table, s->cluster_size); | |
1335 | if (ret < 0) { | |
1336 | fprintf(stderr, "ERROR: Could not write L2 table: %s\n", | |
1337 | strerror(-ret)); | |
1338 | res->check_errors++; | |
1339 | goto fail; | |
1340 | } | |
1341 | } | |
4f6ed88c HR |
1342 | } |
1343 | ||
1344 | ret = 0; | |
1345 | ||
1346 | fail: | |
1347 | qemu_vfree(l2_table); | |
1348 | return ret; | |
1349 | } | |
1350 | ||
afa50193 HR |
1351 | /* |
1352 | * Writes one sector of the refcount table to the disk | |
1353 | */ | |
1354 | #define RT_ENTRIES_PER_SECTOR (512 / sizeof(uint64_t)) | |
1355 | static int write_reftable_entry(BlockDriverState *bs, int rt_index) | |
1356 | { | |
1357 | BDRVQcowState *s = bs->opaque; | |
1358 | uint64_t buf[RT_ENTRIES_PER_SECTOR]; | |
1359 | int rt_start_index; | |
1360 | int i, ret; | |
1361 | ||
1362 | rt_start_index = rt_index & ~(RT_ENTRIES_PER_SECTOR - 1); | |
1363 | for (i = 0; i < RT_ENTRIES_PER_SECTOR; i++) { | |
1364 | buf[i] = cpu_to_be64(s->refcount_table[rt_start_index + i]); | |
1365 | } | |
1366 | ||
231bb267 | 1367 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_TABLE, |
afa50193 HR |
1368 | s->refcount_table_offset + rt_start_index * sizeof(uint64_t), |
1369 | sizeof(buf)); | |
1370 | if (ret < 0) { | |
1371 | return ret; | |
1372 | } | |
1373 | ||
1374 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE); | |
1375 | ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset + | |
1376 | rt_start_index * sizeof(uint64_t), buf, sizeof(buf)); | |
1377 | if (ret < 0) { | |
1378 | return ret; | |
1379 | } | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * Allocates a new cluster for the given refcount block (represented by its | |
1386 | * offset in the image file) and copies the current content there. This function | |
1387 | * does _not_ decrement the reference count for the currently occupied cluster. | |
1388 | * | |
1389 | * This function prints an informative message to stderr on error (and returns | |
8a15b813 | 1390 | * -errno); on success, the offset of the newly allocated cluster is returned. |
afa50193 HR |
1391 | */ |
1392 | static int64_t realloc_refcount_block(BlockDriverState *bs, int reftable_index, | |
1393 | uint64_t offset) | |
1394 | { | |
1395 | BDRVQcowState *s = bs->opaque; | |
1396 | int64_t new_offset = 0; | |
1397 | void *refcount_block = NULL; | |
1398 | int ret; | |
1399 | ||
1400 | /* allocate new refcount block */ | |
1401 | new_offset = qcow2_alloc_clusters(bs, s->cluster_size); | |
1402 | if (new_offset < 0) { | |
1403 | fprintf(stderr, "Could not allocate new cluster: %s\n", | |
1404 | strerror(-new_offset)); | |
1405 | ret = new_offset; | |
a134d90f | 1406 | goto done; |
afa50193 HR |
1407 | } |
1408 | ||
1409 | /* fetch current refcount block content */ | |
1410 | ret = qcow2_cache_get(bs, s->refcount_block_cache, offset, &refcount_block); | |
1411 | if (ret < 0) { | |
1412 | fprintf(stderr, "Could not fetch refcount block: %s\n", strerror(-ret)); | |
a134d90f | 1413 | goto fail_free_cluster; |
afa50193 HR |
1414 | } |
1415 | ||
1416 | /* new block has not yet been entered into refcount table, therefore it is | |
1417 | * no refcount block yet (regarding this check) */ | |
231bb267 | 1418 | ret = qcow2_pre_write_overlap_check(bs, 0, new_offset, s->cluster_size); |
afa50193 HR |
1419 | if (ret < 0) { |
1420 | fprintf(stderr, "Could not write refcount block; metadata overlap " | |
1421 | "check failed: %s\n", strerror(-ret)); | |
1422 | /* the image will be marked corrupt, so don't even attempt on freeing | |
1423 | * the cluster */ | |
a134d90f | 1424 | goto done; |
afa50193 HR |
1425 | } |
1426 | ||
1427 | /* write to new block */ | |
1428 | ret = bdrv_write(bs->file, new_offset / BDRV_SECTOR_SIZE, refcount_block, | |
1429 | s->cluster_sectors); | |
1430 | if (ret < 0) { | |
1431 | fprintf(stderr, "Could not write refcount block: %s\n", strerror(-ret)); | |
a134d90f | 1432 | goto fail_free_cluster; |
afa50193 HR |
1433 | } |
1434 | ||
1435 | /* update refcount table */ | |
ac95acdb | 1436 | assert(!offset_into_cluster(s, new_offset)); |
afa50193 HR |
1437 | s->refcount_table[reftable_index] = new_offset; |
1438 | ret = write_reftable_entry(bs, reftable_index); | |
1439 | if (ret < 0) { | |
1440 | fprintf(stderr, "Could not update refcount table: %s\n", | |
1441 | strerror(-ret)); | |
a134d90f | 1442 | goto fail_free_cluster; |
afa50193 HR |
1443 | } |
1444 | ||
a134d90f HR |
1445 | goto done; |
1446 | ||
1447 | fail_free_cluster: | |
1448 | qcow2_free_clusters(bs, new_offset, s->cluster_size, QCOW2_DISCARD_OTHER); | |
1449 | ||
1450 | done: | |
afa50193 | 1451 | if (refcount_block) { |
a134d90f HR |
1452 | /* This should never fail, as it would only do so if the given refcount |
1453 | * block cannot be found in the cache. As this is impossible as long as | |
1454 | * there are no bugs, assert the success. */ | |
1455 | int tmp = qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block); | |
1456 | assert(tmp == 0); | |
afa50193 | 1457 | } |
a134d90f | 1458 | |
afa50193 HR |
1459 | if (ret < 0) { |
1460 | return ret; | |
1461 | } | |
a134d90f | 1462 | |
afa50193 HR |
1463 | return new_offset; |
1464 | } | |
1465 | ||
f7d0fe02 KW |
1466 | /* |
1467 | * Checks an image for refcount consistency. | |
1468 | * | |
1469 | * Returns 0 if no errors are found, the number of errors in case the image is | |
a1c7273b | 1470 | * detected as corrupted, and -errno when an internal error occurred. |
f7d0fe02 | 1471 | */ |
166acf54 KW |
1472 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
1473 | BdrvCheckMode fix) | |
f7d0fe02 KW |
1474 | { |
1475 | BDRVQcowState *s = bs->opaque; | |
c6bb9ad1 | 1476 | int64_t size, i, highest_cluster; |
166acf54 | 1477 | int nb_clusters, refcount1, refcount2; |
f7d0fe02 KW |
1478 | QCowSnapshot *sn; |
1479 | uint16_t *refcount_table; | |
9ac228e0 | 1480 | int ret; |
f7d0fe02 | 1481 | |
66f82cee | 1482 | size = bdrv_getlength(bs->file); |
f7d0fe02 | 1483 | nb_clusters = size_to_clusters(s, size); |
7267c094 | 1484 | refcount_table = g_malloc0(nb_clusters * sizeof(uint16_t)); |
f7d0fe02 | 1485 | |
c349ca4b KW |
1486 | res->bfi.total_clusters = |
1487 | size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE); | |
1488 | ||
f7d0fe02 | 1489 | /* header */ |
9ac228e0 KW |
1490 | inc_refcounts(bs, res, refcount_table, nb_clusters, |
1491 | 0, s->cluster_size); | |
f7d0fe02 KW |
1492 | |
1493 | /* current L1 table */ | |
9ac228e0 | 1494 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
db074901 | 1495 | s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO); |
f7d0fe02 | 1496 | if (ret < 0) { |
80fa3341 | 1497 | goto fail; |
f7d0fe02 | 1498 | } |
f7d0fe02 KW |
1499 | |
1500 | /* snapshots */ | |
1501 | for(i = 0; i < s->nb_snapshots; i++) { | |
1502 | sn = s->snapshots + i; | |
9ac228e0 KW |
1503 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
1504 | sn->l1_table_offset, sn->l1_size, 0); | |
1505 | if (ret < 0) { | |
80fa3341 | 1506 | goto fail; |
9ac228e0 | 1507 | } |
f7d0fe02 | 1508 | } |
9ac228e0 KW |
1509 | inc_refcounts(bs, res, refcount_table, nb_clusters, |
1510 | s->snapshots_offset, s->snapshots_size); | |
f7d0fe02 KW |
1511 | |
1512 | /* refcount data */ | |
9ac228e0 KW |
1513 | inc_refcounts(bs, res, refcount_table, nb_clusters, |
1514 | s->refcount_table_offset, | |
1515 | s->refcount_table_size * sizeof(uint64_t)); | |
1516 | ||
f7d0fe02 | 1517 | for(i = 0; i < s->refcount_table_size; i++) { |
6882c8fa | 1518 | uint64_t offset, cluster; |
f7d0fe02 | 1519 | offset = s->refcount_table[i]; |
6882c8fa | 1520 | cluster = offset >> s->cluster_bits; |
746c3cb5 KW |
1521 | |
1522 | /* Refcount blocks are cluster aligned */ | |
ac95acdb | 1523 | if (offset_into_cluster(s, offset)) { |
166acf54 | 1524 | fprintf(stderr, "ERROR refcount block %" PRId64 " is not " |
746c3cb5 | 1525 | "cluster aligned; refcount table entry corrupted\n", i); |
9ac228e0 | 1526 | res->corruptions++; |
6882c8fa KW |
1527 | continue; |
1528 | } | |
1529 | ||
1530 | if (cluster >= nb_clusters) { | |
166acf54 KW |
1531 | fprintf(stderr, "ERROR refcount block %" PRId64 |
1532 | " is outside image\n", i); | |
9ac228e0 | 1533 | res->corruptions++; |
6882c8fa | 1534 | continue; |
746c3cb5 KW |
1535 | } |
1536 | ||
f7d0fe02 | 1537 | if (offset != 0) { |
9ac228e0 KW |
1538 | inc_refcounts(bs, res, refcount_table, nb_clusters, |
1539 | offset, s->cluster_size); | |
6882c8fa | 1540 | if (refcount_table[cluster] != 1) { |
afa50193 | 1541 | fprintf(stderr, "%s refcount block %" PRId64 |
166acf54 | 1542 | " refcount=%d\n", |
afa50193 HR |
1543 | fix & BDRV_FIX_ERRORS ? "Repairing" : |
1544 | "ERROR", | |
6882c8fa | 1545 | i, refcount_table[cluster]); |
afa50193 HR |
1546 | |
1547 | if (fix & BDRV_FIX_ERRORS) { | |
1548 | int64_t new_offset; | |
1549 | ||
1550 | new_offset = realloc_refcount_block(bs, i, offset); | |
1551 | if (new_offset < 0) { | |
1552 | res->corruptions++; | |
1553 | continue; | |
1554 | } | |
1555 | ||
1556 | /* update refcounts */ | |
1557 | if ((new_offset >> s->cluster_bits) >= nb_clusters) { | |
1558 | /* increase refcount_table size if necessary */ | |
1559 | int old_nb_clusters = nb_clusters; | |
1560 | nb_clusters = (new_offset >> s->cluster_bits) + 1; | |
1561 | refcount_table = g_realloc(refcount_table, | |
1562 | nb_clusters * sizeof(uint16_t)); | |
1563 | memset(&refcount_table[old_nb_clusters], 0, (nb_clusters | |
1564 | - old_nb_clusters) * sizeof(uint16_t)); | |
1565 | } | |
1566 | refcount_table[cluster]--; | |
1567 | inc_refcounts(bs, res, refcount_table, nb_clusters, | |
1568 | new_offset, s->cluster_size); | |
1569 | ||
1570 | res->corruptions_fixed++; | |
1571 | } else { | |
1572 | res->corruptions++; | |
1573 | } | |
746c3cb5 | 1574 | } |
f7d0fe02 KW |
1575 | } |
1576 | } | |
1577 | ||
1578 | /* compare ref counts */ | |
c6bb9ad1 | 1579 | for (i = 0, highest_cluster = 0; i < nb_clusters; i++) { |
f7d0fe02 | 1580 | refcount1 = get_refcount(bs, i); |
018faafd | 1581 | if (refcount1 < 0) { |
166acf54 | 1582 | fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n", |
018faafd | 1583 | i, strerror(-refcount1)); |
9ac228e0 | 1584 | res->check_errors++; |
f74550fd | 1585 | continue; |
018faafd KW |
1586 | } |
1587 | ||
f7d0fe02 | 1588 | refcount2 = refcount_table[i]; |
c6bb9ad1 FS |
1589 | |
1590 | if (refcount1 > 0 || refcount2 > 0) { | |
1591 | highest_cluster = i; | |
1592 | } | |
1593 | ||
f7d0fe02 | 1594 | if (refcount1 != refcount2) { |
166acf54 KW |
1595 | |
1596 | /* Check if we're allowed to fix the mismatch */ | |
1597 | int *num_fixed = NULL; | |
1598 | if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) { | |
1599 | num_fixed = &res->leaks_fixed; | |
1600 | } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) { | |
1601 | num_fixed = &res->corruptions_fixed; | |
1602 | } | |
1603 | ||
1604 | fprintf(stderr, "%s cluster %" PRId64 " refcount=%d reference=%d\n", | |
1605 | num_fixed != NULL ? "Repairing" : | |
1606 | refcount1 < refcount2 ? "ERROR" : | |
1607 | "Leaked", | |
f7d0fe02 | 1608 | i, refcount1, refcount2); |
166acf54 KW |
1609 | |
1610 | if (num_fixed) { | |
1611 | ret = update_refcount(bs, i << s->cluster_bits, 1, | |
6cfcb9b8 KW |
1612 | refcount2 - refcount1, |
1613 | QCOW2_DISCARD_ALWAYS); | |
166acf54 KW |
1614 | if (ret >= 0) { |
1615 | (*num_fixed)++; | |
1616 | continue; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | /* And if we couldn't, print an error */ | |
9ac228e0 KW |
1621 | if (refcount1 < refcount2) { |
1622 | res->corruptions++; | |
1623 | } else { | |
1624 | res->leaks++; | |
1625 | } | |
f7d0fe02 KW |
1626 | } |
1627 | } | |
1628 | ||
4f6ed88c | 1629 | /* check OFLAG_COPIED */ |
e23e400e | 1630 | ret = check_oflag_copied(bs, res, fix); |
4f6ed88c HR |
1631 | if (ret < 0) { |
1632 | goto fail; | |
1633 | } | |
1634 | ||
c6bb9ad1 | 1635 | res->image_end_offset = (highest_cluster + 1) * s->cluster_size; |
80fa3341 KW |
1636 | ret = 0; |
1637 | ||
1638 | fail: | |
7267c094 | 1639 | g_free(refcount_table); |
f7d0fe02 | 1640 | |
80fa3341 | 1641 | return ret; |
f7d0fe02 KW |
1642 | } |
1643 | ||
a40f1c2a HR |
1644 | #define overlaps_with(ofs, sz) \ |
1645 | ranges_overlap(offset, size, ofs, sz) | |
1646 | ||
1647 | /* | |
1648 | * Checks if the given offset into the image file is actually free to use by | |
1649 | * looking for overlaps with important metadata sections (L1/L2 tables etc.), | |
1650 | * i.e. a sanity check without relying on the refcount tables. | |
1651 | * | |
231bb267 HR |
1652 | * The ign parameter specifies what checks not to perform (being a bitmask of |
1653 | * QCow2MetadataOverlap values), i.e., what sections to ignore. | |
a40f1c2a HR |
1654 | * |
1655 | * Returns: | |
1656 | * - 0 if writing to this offset will not affect the mentioned metadata | |
1657 | * - a positive QCow2MetadataOverlap value indicating one overlapping section | |
1658 | * - a negative value (-errno) indicating an error while performing a check, | |
1659 | * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2 | |
1660 | */ | |
231bb267 | 1661 | int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a HR |
1662 | int64_t size) |
1663 | { | |
1664 | BDRVQcowState *s = bs->opaque; | |
3e355390 | 1665 | int chk = s->overlap_check & ~ign; |
a40f1c2a HR |
1666 | int i, j; |
1667 | ||
1668 | if (!size) { | |
1669 | return 0; | |
1670 | } | |
1671 | ||
1672 | if (chk & QCOW2_OL_MAIN_HEADER) { | |
1673 | if (offset < s->cluster_size) { | |
1674 | return QCOW2_OL_MAIN_HEADER; | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | /* align range to test to cluster boundaries */ | |
1679 | size = align_offset(offset_into_cluster(s, offset) + size, s->cluster_size); | |
1680 | offset = start_of_cluster(s, offset); | |
1681 | ||
1682 | if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) { | |
1683 | if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) { | |
1684 | return QCOW2_OL_ACTIVE_L1; | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) { | |
1689 | if (overlaps_with(s->refcount_table_offset, | |
1690 | s->refcount_table_size * sizeof(uint64_t))) { | |
1691 | return QCOW2_OL_REFCOUNT_TABLE; | |
1692 | } | |
1693 | } | |
1694 | ||
1695 | if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) { | |
1696 | if (overlaps_with(s->snapshots_offset, s->snapshots_size)) { | |
1697 | return QCOW2_OL_SNAPSHOT_TABLE; | |
1698 | } | |
1699 | } | |
1700 | ||
1701 | if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) { | |
1702 | for (i = 0; i < s->nb_snapshots; i++) { | |
1703 | if (s->snapshots[i].l1_size && | |
1704 | overlaps_with(s->snapshots[i].l1_table_offset, | |
1705 | s->snapshots[i].l1_size * sizeof(uint64_t))) { | |
1706 | return QCOW2_OL_INACTIVE_L1; | |
1707 | } | |
1708 | } | |
1709 | } | |
1710 | ||
1711 | if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) { | |
1712 | for (i = 0; i < s->l1_size; i++) { | |
1713 | if ((s->l1_table[i] & L1E_OFFSET_MASK) && | |
1714 | overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK, | |
1715 | s->cluster_size)) { | |
1716 | return QCOW2_OL_ACTIVE_L2; | |
1717 | } | |
1718 | } | |
1719 | } | |
1720 | ||
1721 | if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) { | |
1722 | for (i = 0; i < s->refcount_table_size; i++) { | |
1723 | if ((s->refcount_table[i] & REFT_OFFSET_MASK) && | |
1724 | overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK, | |
1725 | s->cluster_size)) { | |
1726 | return QCOW2_OL_REFCOUNT_BLOCK; | |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) { | |
1732 | for (i = 0; i < s->nb_snapshots; i++) { | |
1733 | uint64_t l1_ofs = s->snapshots[i].l1_table_offset; | |
1734 | uint32_t l1_sz = s->snapshots[i].l1_size; | |
998b959c HR |
1735 | uint64_t l1_sz2 = l1_sz * sizeof(uint64_t); |
1736 | uint64_t *l1 = g_malloc(l1_sz2); | |
a40f1c2a HR |
1737 | int ret; |
1738 | ||
998b959c | 1739 | ret = bdrv_pread(bs->file, l1_ofs, l1, l1_sz2); |
a40f1c2a HR |
1740 | if (ret < 0) { |
1741 | g_free(l1); | |
1742 | return ret; | |
1743 | } | |
1744 | ||
1745 | for (j = 0; j < l1_sz; j++) { | |
1e242b55 HR |
1746 | uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK; |
1747 | if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) { | |
a40f1c2a HR |
1748 | g_free(l1); |
1749 | return QCOW2_OL_INACTIVE_L2; | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | g_free(l1); | |
1754 | } | |
1755 | } | |
1756 | ||
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | static const char *metadata_ol_names[] = { | |
1761 | [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header", | |
1762 | [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table", | |
1763 | [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table", | |
1764 | [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table", | |
1765 | [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block", | |
1766 | [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table", | |
1767 | [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table", | |
1768 | [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table", | |
1769 | }; | |
1770 | ||
1771 | /* | |
1772 | * First performs a check for metadata overlaps (through | |
1773 | * qcow2_check_metadata_overlap); if that fails with a negative value (error | |
1774 | * while performing a check), that value is returned. If an impending overlap | |
1775 | * is detected, the BDS will be made unusable, the qcow2 file marked corrupt | |
1776 | * and -EIO returned. | |
1777 | * | |
1778 | * Returns 0 if there were neither overlaps nor errors while checking for | |
1779 | * overlaps; or a negative value (-errno) on error. | |
1780 | */ | |
231bb267 | 1781 | int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset, |
a40f1c2a HR |
1782 | int64_t size) |
1783 | { | |
231bb267 | 1784 | int ret = qcow2_check_metadata_overlap(bs, ign, offset, size); |
a40f1c2a HR |
1785 | |
1786 | if (ret < 0) { | |
1787 | return ret; | |
1788 | } else if (ret > 0) { | |
1789 | int metadata_ol_bitnr = ffs(ret) - 1; | |
1790 | char *message; | |
1791 | QObject *data; | |
1792 | ||
1793 | assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR); | |
1794 | ||
1795 | fprintf(stderr, "qcow2: Preventing invalid write on metadata (overlaps " | |
1796 | "with %s); image marked as corrupt.\n", | |
1797 | metadata_ol_names[metadata_ol_bitnr]); | |
1798 | message = g_strdup_printf("Prevented %s overwrite", | |
1799 | metadata_ol_names[metadata_ol_bitnr]); | |
1800 | data = qobject_from_jsonf("{ 'device': %s, 'msg': %s, 'offset': %" | |
1801 | PRId64 ", 'size': %" PRId64 " }", bs->device_name, message, | |
1802 | offset, size); | |
1803 | monitor_protocol_event(QEVENT_BLOCK_IMAGE_CORRUPTED, data); | |
1804 | g_free(message); | |
1805 | qobject_decref(data); | |
1806 | ||
1807 | qcow2_mark_corrupt(bs); | |
1808 | bs->drv = NULL; /* make BDS unusable */ | |
1809 | return -EIO; | |
1810 | } | |
1811 | ||
1812 | return 0; | |
1813 | } |