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