]> git.proxmox.com Git - qemu.git/blame - block/qcow2-refcount.c
qcow2: Change l2_load to return 0/-errno
[qemu.git] / block / qcow2-refcount.c
CommitLineData
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"
26#include "block_int.h"
27#include "block/qcow2.h"
28
29static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
92dcb59f 30static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
f7d0fe02
KW
31 int64_t offset, int64_t length,
32 int addend);
33
3b88e52b
KW
34
35static int cache_refcount_updates = 0;
36
66f82cee 37static int write_refcount_block(BlockDriverState *bs)
3b88e52b 38{
66f82cee 39 BDRVQcowState *s = bs->opaque;
3b88e52b
KW
40 size_t size = s->cluster_size;
41
42 if (s->refcount_block_cache_offset == 0) {
43 return 0;
44 }
45
66f82cee
KW
46 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE);
47 if (bdrv_pwrite(bs->file, s->refcount_block_cache_offset,
3b88e52b
KW
48 s->refcount_block_cache, size) != size)
49 {
50 return -EIO;
51 }
52
53 return 0;
54}
55
f7d0fe02
KW
56/*********************************************************/
57/* refcount handling */
58
ed6ccf0f 59int qcow2_refcount_init(BlockDriverState *bs)
f7d0fe02
KW
60{
61 BDRVQcowState *s = bs->opaque;
62 int ret, refcount_table_size2, i;
63
64 s->refcount_block_cache = qemu_malloc(s->cluster_size);
65 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
66 s->refcount_table = qemu_malloc(refcount_table_size2);
67 if (s->refcount_table_size > 0) {
66f82cee
KW
68 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
69 ret = bdrv_pread(bs->file, s->refcount_table_offset,
f7d0fe02
KW
70 s->refcount_table, refcount_table_size2);
71 if (ret != refcount_table_size2)
72 goto fail;
73 for(i = 0; i < s->refcount_table_size; i++)
74 be64_to_cpus(&s->refcount_table[i]);
75 }
76 return 0;
77 fail:
78 return -ENOMEM;
79}
80
ed6ccf0f 81void qcow2_refcount_close(BlockDriverState *bs)
f7d0fe02
KW
82{
83 BDRVQcowState *s = bs->opaque;
84 qemu_free(s->refcount_block_cache);
85 qemu_free(s->refcount_table);
86}
87
88
89static int load_refcount_block(BlockDriverState *bs,
90 int64_t refcount_block_offset)
91{
92 BDRVQcowState *s = bs->opaque;
93 int ret;
3b88e52b
KW
94
95 if (cache_refcount_updates) {
66f82cee 96 write_refcount_block(bs);
3b88e52b
KW
97 }
98
66f82cee
KW
99 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
100 ret = bdrv_pread(bs->file, refcount_block_offset, s->refcount_block_cache,
f7d0fe02
KW
101 s->cluster_size);
102 if (ret != s->cluster_size)
103 return -EIO;
104 s->refcount_block_cache_offset = refcount_block_offset;
105 return 0;
106}
107
108static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
109{
110 BDRVQcowState *s = bs->opaque;
111 int refcount_table_index, block_index;
112 int64_t refcount_block_offset;
113
114 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
115 if (refcount_table_index >= s->refcount_table_size)
116 return 0;
117 refcount_block_offset = s->refcount_table[refcount_table_index];
118 if (!refcount_block_offset)
119 return 0;
120 if (refcount_block_offset != s->refcount_block_cache_offset) {
121 /* better than nothing: return allocated if read error */
122 if (load_refcount_block(bs, refcount_block_offset) < 0)
123 return 1;
124 }
125 block_index = cluster_index &
126 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
127 return be16_to_cpu(s->refcount_block_cache[block_index]);
128}
129
05121aed
KW
130/*
131 * Rounds the refcount table size up to avoid growing the table for each single
132 * refcount block that is allocated.
133 */
134static unsigned int next_refcount_table_size(BDRVQcowState *s,
135 unsigned int min_size)
136{
137 unsigned int min_clusters = (min_size >> (s->cluster_bits - 3)) + 1;
138 unsigned int refcount_table_clusters =
139 MAX(1, s->refcount_table_size >> (s->cluster_bits - 3));
140
141 while (min_clusters > refcount_table_clusters) {
142 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
143 }
144
145 return refcount_table_clusters << (s->cluster_bits - 3);
146}
147
92dcb59f
KW
148
149/* Checks if two offsets are described by the same refcount block */
150static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
151 uint64_t offset_b)
152{
153 uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
154 uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
155
156 return (block_a == block_b);
157}
158
159/*
160 * Loads a refcount block. If it doesn't exist yet, it is allocated first
161 * (including growing the refcount table if needed).
162 *
163 * Returns the offset of the refcount block on success or -errno in error case
164 */
165static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
f7d0fe02
KW
166{
167 BDRVQcowState *s = bs->opaque;
92dcb59f
KW
168 unsigned int refcount_table_index;
169 int ret;
170
66f82cee 171 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
8252278a 172
92dcb59f
KW
173 /* Find the refcount block for the given cluster */
174 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
175
176 if (refcount_table_index < s->refcount_table_size) {
177
178 uint64_t refcount_block_offset =
179 s->refcount_table[refcount_table_index];
180
181 /* If it's already there, we're done */
182 if (refcount_block_offset) {
183 if (refcount_block_offset != s->refcount_block_cache_offset) {
184 ret = load_refcount_block(bs, refcount_block_offset);
185 if (ret < 0) {
186 return ret;
187 }
188 }
189 return refcount_block_offset;
190 }
191 }
192
193 /*
194 * If we came here, we need to allocate something. Something is at least
195 * a cluster for the new refcount block. It may also include a new refcount
196 * table if the old refcount table is too small.
197 *
198 * Note that allocating clusters here needs some special care:
199 *
200 * - We can't use the normal qcow2_alloc_clusters(), it would try to
201 * increase the refcount and very likely we would end up with an endless
202 * recursion. Instead we must place the refcount blocks in a way that
203 * they can describe them themselves.
204 *
205 * - We need to consider that at this point we are inside update_refcounts
206 * and doing the initial refcount increase. This means that some clusters
207 * have already been allocated by the caller, but their refcount isn't
208 * accurate yet. free_cluster_index tells us where this allocation ends
209 * as long as we don't overwrite it by freeing clusters.
210 *
211 * - alloc_clusters_noref and qcow2_free_clusters may load a different
212 * refcount block into the cache
213 */
214
215 if (cache_refcount_updates) {
66f82cee 216 ret = write_refcount_block(bs);
92dcb59f
KW
217 if (ret < 0) {
218 return ret;
219 }
220 }
221
222 /* Allocate the refcount block itself and mark it as used */
223 uint64_t new_block = alloc_clusters_noref(bs, s->cluster_size);
224 memset(s->refcount_block_cache, 0, s->cluster_size);
225 s->refcount_block_cache_offset = new_block;
f7d0fe02 226
f7d0fe02 227#ifdef DEBUG_ALLOC2
92dcb59f
KW
228 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
229 " at %" PRIx64 "\n",
230 refcount_table_index, cluster_index << s->cluster_bits, new_block);
f7d0fe02 231#endif
92dcb59f
KW
232
233 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
234 /* The block describes itself, need to update the cache */
235 int block_index = (new_block >> s->cluster_bits) &
236 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
237 s->refcount_block_cache[block_index] = cpu_to_be16(1);
238 } else {
239 /* Described somewhere else. This can recurse at most twice before we
240 * arrive at a block that describes itself. */
241 ret = update_refcount(bs, new_block, s->cluster_size, 1);
242 if (ret < 0) {
243 goto fail_block;
244 }
245 }
246
247 /* Now the new refcount block needs to be written to disk */
66f82cee
KW
248 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
249 ret = bdrv_pwrite(bs->file, new_block, s->refcount_block_cache,
92dcb59f
KW
250 s->cluster_size);
251 if (ret < 0) {
252 goto fail_block;
253 }
254
255 /* If the refcount table is big enough, just hook the block up there */
256 if (refcount_table_index < s->refcount_table_size) {
257 uint64_t data64 = cpu_to_be64(new_block);
66f82cee
KW
258 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
259 ret = bdrv_pwrite(bs->file,
92dcb59f
KW
260 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
261 &data64, sizeof(data64));
262 if (ret < 0) {
263 goto fail_block;
264 }
265
266 s->refcount_table[refcount_table_index] = new_block;
267 return new_block;
268 }
269
270 /*
271 * If we come here, we need to grow the refcount table. Again, a new
272 * refcount table needs some space and we can't simply allocate to avoid
273 * endless recursion.
274 *
275 * Therefore let's grab new refcount blocks at the end of the image, which
276 * will describe themselves and the new refcount table. This way we can
277 * reference them only in the new table and do the switch to the new
278 * refcount table at once without producing an inconsistent state in
279 * between.
280 */
66f82cee 281 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
8252278a 282
92dcb59f
KW
283 /* Calculate the number of refcount blocks needed so far */
284 uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
285 uint64_t blocks_used = (s->free_cluster_index +
286 refcount_block_clusters - 1) / refcount_block_clusters;
287
288 /* And now we need at least one block more for the new metadata */
289 uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
290 uint64_t last_table_size;
291 uint64_t blocks_clusters;
292 do {
293 uint64_t table_clusters = size_to_clusters(s, table_size);
294 blocks_clusters = 1 +
295 ((table_clusters + refcount_block_clusters - 1)
296 / refcount_block_clusters);
297 uint64_t meta_clusters = table_clusters + blocks_clusters;
298
299 last_table_size = table_size;
300 table_size = next_refcount_table_size(s, blocks_used +
301 ((meta_clusters + refcount_block_clusters - 1)
302 / refcount_block_clusters));
303
304 } while (last_table_size != table_size);
305
306#ifdef DEBUG_ALLOC2
307 fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
308 s->refcount_table_size, table_size);
309#endif
310
311 /* Create the new refcount table and blocks */
312 uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
313 s->cluster_size;
314 uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
315 uint16_t *new_blocks = qemu_mallocz(blocks_clusters * s->cluster_size);
316 uint64_t *new_table = qemu_mallocz(table_size * sizeof(uint64_t));
317
318 assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
319
320 /* Fill the new refcount table */
f7d0fe02 321 memcpy(new_table, s->refcount_table,
92dcb59f
KW
322 s->refcount_table_size * sizeof(uint64_t));
323 new_table[refcount_table_index] = new_block;
324
325 int i;
326 for (i = 0; i < blocks_clusters; i++) {
327 new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
328 }
329
330 /* Fill the refcount blocks */
331 uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
332 int block = 0;
333 for (i = 0; i < table_clusters + blocks_clusters; i++) {
334 new_blocks[block++] = cpu_to_be16(1);
335 }
336
337 /* Write refcount blocks to disk */
66f82cee
KW
338 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
339 ret = bdrv_pwrite(bs->file, meta_offset, new_blocks,
92dcb59f
KW
340 blocks_clusters * s->cluster_size);
341 qemu_free(new_blocks);
342 if (ret < 0) {
343 goto fail_table;
344 }
345
346 /* Write refcount table to disk */
347 for(i = 0; i < table_size; i++) {
348 cpu_to_be64s(&new_table[i]);
349 }
350
66f82cee
KW
351 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
352 ret = bdrv_pwrite(bs->file, table_offset, new_table,
92dcb59f
KW
353 table_size * sizeof(uint64_t));
354 if (ret < 0) {
355 goto fail_table;
356 }
357
358 for(i = 0; i < table_size; i++) {
f7d0fe02 359 cpu_to_be64s(&new_table[i]);
92dcb59f 360 }
f7d0fe02 361
92dcb59f
KW
362 /* Hook up the new refcount table in the qcow2 header */
363 uint8_t data[12];
f7d0fe02 364 cpu_to_be64w((uint64_t*)data, table_offset);
92dcb59f 365 cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
66f82cee
KW
366 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
367 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, refcount_table_offset),
92dcb59f
KW
368 data, sizeof(data));
369 if (ret < 0) {
370 goto fail_table;
f2b7c8b3
KW
371 }
372
92dcb59f
KW
373 /* And switch it in memory */
374 uint64_t old_table_offset = s->refcount_table_offset;
375 uint64_t old_table_size = s->refcount_table_size;
376
f7d0fe02 377 qemu_free(s->refcount_table);
f7d0fe02 378 s->refcount_table = new_table;
92dcb59f 379 s->refcount_table_size = table_size;
f7d0fe02
KW
380 s->refcount_table_offset = table_offset;
381
92dcb59f
KW
382 /* Free old table. Remember, we must not change free_cluster_index */
383 uint64_t old_free_cluster_index = s->free_cluster_index;
ed6ccf0f 384 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
92dcb59f 385 s->free_cluster_index = old_free_cluster_index;
f7d0fe02 386
92dcb59f
KW
387 ret = load_refcount_block(bs, new_block);
388 if (ret < 0) {
389 goto fail_block;
f7d0fe02
KW
390 }
391
92dcb59f 392 return new_block;
f7d0fe02 393
92dcb59f
KW
394fail_table:
395 qemu_free(new_table);
396fail_block:
397 s->refcount_block_cache_offset = 0;
398 return ret;
f7d0fe02
KW
399}
400
9923e05e 401#define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
66f82cee 402static int write_refcount_block_entries(BlockDriverState *bs,
9923e05e
KW
403 int64_t refcount_block_offset, int first_index, int last_index)
404{
66f82cee 405 BDRVQcowState *s = bs->opaque;
9923e05e
KW
406 size_t size;
407
3b88e52b
KW
408 if (cache_refcount_updates) {
409 return 0;
410 }
411
9923e05e
KW
412 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
413 last_index = (last_index + REFCOUNTS_PER_SECTOR)
414 & ~(REFCOUNTS_PER_SECTOR - 1);
415
416 size = (last_index - first_index) << REFCOUNT_SHIFT;
66f82cee
KW
417 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
418 if (bdrv_pwrite(bs->file,
9923e05e
KW
419 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
420 &s->refcount_block_cache[first_index], size) != size)
421 {
422 return -EIO;
423 }
424
425 return 0;
426}
427
f7d0fe02 428/* XXX: cache several refcount block clusters ? */
db3a964f
KW
429static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
430 int64_t offset, int64_t length, int addend)
f7d0fe02
KW
431{
432 BDRVQcowState *s = bs->opaque;
433 int64_t start, last, cluster_offset;
434 int64_t refcount_block_offset = 0;
435 int64_t table_index = -1, old_table_index;
436 int first_index = -1, last_index = -1;
09508d13 437 int ret;
f7d0fe02
KW
438
439#ifdef DEBUG_ALLOC2
0bf9e31a 440 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
f7d0fe02
KW
441 offset, length, addend);
442#endif
7322afe7 443 if (length < 0) {
f7d0fe02 444 return -EINVAL;
7322afe7
KW
445 } else if (length == 0) {
446 return 0;
447 }
448
f7d0fe02
KW
449 start = offset & ~(s->cluster_size - 1);
450 last = (offset + length - 1) & ~(s->cluster_size - 1);
451 for(cluster_offset = start; cluster_offset <= last;
452 cluster_offset += s->cluster_size)
453 {
454 int block_index, refcount;
455 int64_t cluster_index = cluster_offset >> s->cluster_bits;
09508d13 456 int64_t new_block;
f7d0fe02
KW
457
458 /* Only write refcount block to disk when we are done with it */
459 old_table_index = table_index;
460 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
461 if ((old_table_index >= 0) && (table_index != old_table_index)) {
9923e05e 462
66f82cee 463 if (write_refcount_block_entries(bs, refcount_block_offset,
9923e05e 464 first_index, last_index) < 0)
f7d0fe02
KW
465 {
466 return -EIO;
467 }
468
469 first_index = -1;
470 last_index = -1;
471 }
472
473 /* Load the refcount block and allocate it if needed */
09508d13
KW
474 new_block = alloc_refcount_block(bs, cluster_index);
475 if (new_block < 0) {
476 ret = new_block;
477 goto fail;
f7d0fe02 478 }
09508d13 479 refcount_block_offset = new_block;
f7d0fe02
KW
480
481 /* we can update the count and save it */
482 block_index = cluster_index &
483 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
484 if (first_index == -1 || block_index < first_index) {
485 first_index = block_index;
486 }
487 if (block_index > last_index) {
488 last_index = block_index;
489 }
490
491 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
492 refcount += addend;
09508d13
KW
493 if (refcount < 0 || refcount > 0xffff) {
494 ret = -EINVAL;
495 goto fail;
496 }
f7d0fe02
KW
497 if (refcount == 0 && cluster_index < s->free_cluster_index) {
498 s->free_cluster_index = cluster_index;
499 }
500 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
501 }
502
09508d13
KW
503 ret = 0;
504fail:
505
f7d0fe02
KW
506 /* Write last changed block to disk */
507 if (refcount_block_offset != 0) {
66f82cee 508 if (write_refcount_block_entries(bs, refcount_block_offset,
9923e05e 509 first_index, last_index) < 0)
f7d0fe02 510 {
09508d13 511 return ret < 0 ? ret : -EIO;
f7d0fe02
KW
512 }
513 }
514
09508d13
KW
515 /*
516 * Try do undo any updates if an error is returned (This may succeed in
517 * some cases like ENOSPC for allocating a new refcount block)
518 */
519 if (ret < 0) {
520 int dummy;
521 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
522 }
523
524 return ret;
f7d0fe02
KW
525}
526
527/* addend must be 1 or -1 */
528static int update_cluster_refcount(BlockDriverState *bs,
529 int64_t cluster_index,
530 int addend)
531{
532 BDRVQcowState *s = bs->opaque;
533 int ret;
534
535 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
536 if (ret < 0) {
537 return ret;
538 }
539
540 return get_refcount(bs, cluster_index);
541}
542
543
544
545/*********************************************************/
546/* cluster allocation functions */
547
548
549
550/* return < 0 if error */
551static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
552{
553 BDRVQcowState *s = bs->opaque;
554 int i, nb_clusters;
555
556 nb_clusters = size_to_clusters(s, size);
557retry:
558 for(i = 0; i < nb_clusters; i++) {
508e0893
SH
559 int64_t next_cluster_index = s->free_cluster_index++;
560 if (get_refcount(bs, next_cluster_index) != 0)
f7d0fe02
KW
561 goto retry;
562 }
563#ifdef DEBUG_ALLOC2
0bf9e31a 564 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
f7d0fe02
KW
565 size,
566 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
567#endif
568 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
569}
570
ed6ccf0f 571int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
f7d0fe02
KW
572{
573 int64_t offset;
db3a964f 574 int ret;
f7d0fe02 575
66f82cee 576 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
f7d0fe02 577 offset = alloc_clusters_noref(bs, size);
db3a964f
KW
578 ret = update_refcount(bs, offset, size, 1);
579 if (ret < 0) {
580 return ret;
581 }
f7d0fe02
KW
582 return offset;
583}
584
585/* only used to allocate compressed sectors. We try to allocate
586 contiguous sectors. size must be <= cluster_size */
ed6ccf0f 587int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
f7d0fe02
KW
588{
589 BDRVQcowState *s = bs->opaque;
590 int64_t offset, cluster_offset;
591 int free_in_cluster;
592
66f82cee 593 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
f7d0fe02
KW
594 assert(size > 0 && size <= s->cluster_size);
595 if (s->free_byte_offset == 0) {
ed6ccf0f 596 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
5d757b56
KW
597 if (s->free_byte_offset < 0) {
598 return s->free_byte_offset;
599 }
f7d0fe02
KW
600 }
601 redo:
602 free_in_cluster = s->cluster_size -
603 (s->free_byte_offset & (s->cluster_size - 1));
604 if (size <= free_in_cluster) {
605 /* enough space in current cluster */
606 offset = s->free_byte_offset;
607 s->free_byte_offset += size;
608 free_in_cluster -= size;
609 if (free_in_cluster == 0)
610 s->free_byte_offset = 0;
611 if ((offset & (s->cluster_size - 1)) != 0)
612 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
613 } else {
ed6ccf0f 614 offset = qcow2_alloc_clusters(bs, s->cluster_size);
5d757b56
KW
615 if (offset < 0) {
616 return offset;
617 }
f7d0fe02
KW
618 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
619 if ((cluster_offset + s->cluster_size) == offset) {
620 /* we are lucky: contiguous data */
621 offset = s->free_byte_offset;
622 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
623 s->free_byte_offset += size;
624 } else {
625 s->free_byte_offset = offset;
626 goto redo;
627 }
628 }
629 return offset;
630}
631
ed6ccf0f 632void qcow2_free_clusters(BlockDriverState *bs,
f7d0fe02
KW
633 int64_t offset, int64_t size)
634{
db3a964f
KW
635 int ret;
636
66f82cee 637 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
db3a964f
KW
638 ret = update_refcount(bs, offset, size, -1);
639 if (ret < 0) {
640 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
003fad6e 641 /* TODO Remember the clusters to free them later and avoid leaking */
db3a964f 642 }
f7d0fe02
KW
643}
644
45aba42f
KW
645/*
646 * free_any_clusters
647 *
648 * free clusters according to its type: compressed or not
649 *
650 */
651
ed6ccf0f 652void qcow2_free_any_clusters(BlockDriverState *bs,
45aba42f
KW
653 uint64_t cluster_offset, int nb_clusters)
654{
655 BDRVQcowState *s = bs->opaque;
656
657 /* free the cluster */
658
659 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
660 int nb_csectors;
661 nb_csectors = ((cluster_offset >> s->csize_shift) &
662 s->csize_mask) + 1;
ed6ccf0f
KW
663 qcow2_free_clusters(bs,
664 (cluster_offset & s->cluster_offset_mask) & ~511,
665 nb_csectors * 512);
45aba42f
KW
666 return;
667 }
668
ed6ccf0f 669 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
45aba42f
KW
670
671 return;
672}
673
f7d0fe02
KW
674
675
676/*********************************************************/
677/* snapshots and image creation */
678
679
680
ed6ccf0f
KW
681void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
682 int64_t size)
f7d0fe02
KW
683{
684 int refcount;
685 int64_t start, last, cluster_offset;
686 uint16_t *p;
687
688 start = offset & ~(s->cluster_size - 1);
689 last = (offset + size - 1) & ~(s->cluster_size - 1);
690 for(cluster_offset = start; cluster_offset <= last;
691 cluster_offset += s->cluster_size) {
692 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
693 refcount = be16_to_cpu(*p);
694 refcount++;
695 *p = cpu_to_be16(refcount);
696 }
697}
698
699/* update the refcounts of snapshots and the copied flag */
ed6ccf0f
KW
700int qcow2_update_snapshot_refcount(BlockDriverState *bs,
701 int64_t l1_table_offset, int l1_size, int addend)
f7d0fe02
KW
702{
703 BDRVQcowState *s = bs->opaque;
704 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
705 int64_t old_offset, old_l2_offset;
706 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
707
ed6ccf0f 708 qcow2_l2_cache_reset(bs);
3b88e52b 709 cache_refcount_updates = 1;
f7d0fe02
KW
710
711 l2_table = NULL;
712 l1_table = NULL;
713 l1_size2 = l1_size * sizeof(uint64_t);
f7d0fe02 714 if (l1_table_offset != s->l1_table_offset) {
702ef63f
KW
715 if (l1_size2 != 0) {
716 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
717 } else {
718 l1_table = NULL;
719 }
f7d0fe02 720 l1_allocated = 1;
66f82cee 721 if (bdrv_pread(bs->file, l1_table_offset,
f7d0fe02
KW
722 l1_table, l1_size2) != l1_size2)
723 goto fail;
724 for(i = 0;i < l1_size; i++)
725 be64_to_cpus(&l1_table[i]);
726 } else {
727 assert(l1_size == s->l1_size);
728 l1_table = s->l1_table;
729 l1_allocated = 0;
730 }
731
732 l2_size = s->l2_size * sizeof(uint64_t);
733 l2_table = qemu_malloc(l2_size);
734 l1_modified = 0;
735 for(i = 0; i < l1_size; i++) {
736 l2_offset = l1_table[i];
737 if (l2_offset) {
738 old_l2_offset = l2_offset;
739 l2_offset &= ~QCOW_OFLAG_COPIED;
740 l2_modified = 0;
66f82cee 741 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
f7d0fe02
KW
742 goto fail;
743 for(j = 0; j < s->l2_size; j++) {
744 offset = be64_to_cpu(l2_table[j]);
745 if (offset != 0) {
746 old_offset = offset;
747 offset &= ~QCOW_OFLAG_COPIED;
748 if (offset & QCOW_OFLAG_COMPRESSED) {
749 nb_csectors = ((offset >> s->csize_shift) &
750 s->csize_mask) + 1;
db3a964f
KW
751 if (addend != 0) {
752 int ret;
753 ret = update_refcount(bs,
754 (offset & s->cluster_offset_mask) & ~511,
755 nb_csectors * 512, addend);
756 if (ret < 0) {
757 goto fail;
758 }
759 }
f7d0fe02
KW
760 /* compressed clusters are never modified */
761 refcount = 2;
762 } else {
763 if (addend != 0) {
764 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
765 } else {
766 refcount = get_refcount(bs, offset >> s->cluster_bits);
767 }
768 }
769
770 if (refcount == 1) {
771 offset |= QCOW_OFLAG_COPIED;
772 }
773 if (offset != old_offset) {
774 l2_table[j] = cpu_to_be64(offset);
775 l2_modified = 1;
776 }
777 }
778 }
779 if (l2_modified) {
66f82cee 780 if (bdrv_pwrite(bs->file,
f7d0fe02
KW
781 l2_offset, l2_table, l2_size) != l2_size)
782 goto fail;
783 }
784
785 if (addend != 0) {
786 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
787 } else {
788 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
789 }
790 if (refcount == 1) {
791 l2_offset |= QCOW_OFLAG_COPIED;
792 }
793 if (l2_offset != old_l2_offset) {
794 l1_table[i] = l2_offset;
795 l1_modified = 1;
796 }
797 }
798 }
799 if (l1_modified) {
800 for(i = 0; i < l1_size; i++)
801 cpu_to_be64s(&l1_table[i]);
66f82cee 802 if (bdrv_pwrite(bs->file, l1_table_offset, l1_table,
f7d0fe02
KW
803 l1_size2) != l1_size2)
804 goto fail;
805 for(i = 0; i < l1_size; i++)
806 be64_to_cpus(&l1_table[i]);
807 }
808 if (l1_allocated)
809 qemu_free(l1_table);
810 qemu_free(l2_table);
3b88e52b 811 cache_refcount_updates = 0;
66f82cee 812 write_refcount_block(bs);
f7d0fe02
KW
813 return 0;
814 fail:
815 if (l1_allocated)
816 qemu_free(l1_table);
817 qemu_free(l2_table);
3b88e52b 818 cache_refcount_updates = 0;
66f82cee 819 write_refcount_block(bs);
f7d0fe02
KW
820 return -EIO;
821}
822
823
824
825
826/*********************************************************/
827/* refcount checking functions */
828
829
830
831/*
832 * Increases the refcount for a range of clusters in a given refcount table.
833 * This is used to construct a temporary refcount table out of L1 and L2 tables
834 * which can be compared the the refcount table saved in the image.
835 *
836 * Returns the number of errors in the image that were found
837 */
838static int inc_refcounts(BlockDriverState *bs,
839 uint16_t *refcount_table,
840 int refcount_table_size,
841 int64_t offset, int64_t size)
842{
843 BDRVQcowState *s = bs->opaque;
844 int64_t start, last, cluster_offset;
845 int k;
846 int errors = 0;
847
848 if (size <= 0)
849 return 0;
850
851 start = offset & ~(s->cluster_size - 1);
852 last = (offset + size - 1) & ~(s->cluster_size - 1);
853 for(cluster_offset = start; cluster_offset <= last;
854 cluster_offset += s->cluster_size) {
855 k = cluster_offset >> s->cluster_bits;
856 if (k < 0 || k >= refcount_table_size) {
857 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
858 cluster_offset);
859 errors++;
860 } else {
861 if (++refcount_table[k] == 0) {
862 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
863 "\n", cluster_offset);
864 errors++;
865 }
866 }
867 }
868
869 return errors;
870}
871
872/*
873 * Increases the refcount in the given refcount table for the all clusters
874 * referenced in the L2 table. While doing so, performs some checks on L2
875 * entries.
876 *
877 * Returns the number of errors found by the checks or -errno if an internal
878 * error occurred.
879 */
880static int check_refcounts_l2(BlockDriverState *bs,
881 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
882 int check_copied)
883{
884 BDRVQcowState *s = bs->opaque;
885 uint64_t *l2_table, offset;
886 int i, l2_size, nb_csectors, refcount;
887 int errors = 0;
888
889 /* Read L2 table from disk */
890 l2_size = s->l2_size * sizeof(uint64_t);
891 l2_table = qemu_malloc(l2_size);
892
66f82cee 893 if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
f7d0fe02
KW
894 goto fail;
895
896 /* Do the actual checks */
897 for(i = 0; i < s->l2_size; i++) {
898 offset = be64_to_cpu(l2_table[i]);
899 if (offset != 0) {
900 if (offset & QCOW_OFLAG_COMPRESSED) {
901 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
902 if (offset & QCOW_OFLAG_COPIED) {
903 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
904 "copied flag must never be set for compressed "
905 "clusters\n", offset >> s->cluster_bits);
906 offset &= ~QCOW_OFLAG_COPIED;
907 errors++;
908 }
909
910 /* Mark cluster as used */
911 nb_csectors = ((offset >> s->csize_shift) &
912 s->csize_mask) + 1;
913 offset &= s->cluster_offset_mask;
914 errors += inc_refcounts(bs, refcount_table,
915 refcount_table_size,
916 offset & ~511, nb_csectors * 512);
917 } else {
918 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
919 if (check_copied) {
920 uint64_t entry = offset;
921 offset &= ~QCOW_OFLAG_COPIED;
922 refcount = get_refcount(bs, offset >> s->cluster_bits);
923 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
924 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
925 PRIx64 " refcount=%d\n", entry, refcount);
926 errors++;
927 }
928 }
929
930 /* Mark cluster as used */
931 offset &= ~QCOW_OFLAG_COPIED;
932 errors += inc_refcounts(bs, refcount_table,
933 refcount_table_size,
934 offset, s->cluster_size);
935
936 /* Correct offsets are cluster aligned */
937 if (offset & (s->cluster_size - 1)) {
938 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
939 "properly aligned; L2 entry corrupted.\n", offset);
940 errors++;
941 }
942 }
943 }
944 }
945
946 qemu_free(l2_table);
947 return errors;
948
949fail:
950 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
951 qemu_free(l2_table);
952 return -EIO;
953}
954
955/*
956 * Increases the refcount for the L1 table, its L2 tables and all referenced
957 * clusters in the given refcount table. While doing so, performs some checks
958 * on L1 and L2 entries.
959 *
960 * Returns the number of errors found by the checks or -errno if an internal
961 * error occurred.
962 */
963static int check_refcounts_l1(BlockDriverState *bs,
964 uint16_t *refcount_table,
965 int refcount_table_size,
966 int64_t l1_table_offset, int l1_size,
967 int check_copied)
968{
969 BDRVQcowState *s = bs->opaque;
970 uint64_t *l1_table, l2_offset, l1_size2;
971 int i, refcount, ret;
972 int errors = 0;
973
974 l1_size2 = l1_size * sizeof(uint64_t);
975
976 /* Mark L1 table as used */
977 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
978 l1_table_offset, l1_size2);
979
980 /* Read L1 table entries from disk */
702ef63f
KW
981 if (l1_size2 == 0) {
982 l1_table = NULL;
983 } else {
984 l1_table = qemu_malloc(l1_size2);
66f82cee 985 if (bdrv_pread(bs->file, l1_table_offset,
702ef63f
KW
986 l1_table, l1_size2) != l1_size2)
987 goto fail;
988 for(i = 0;i < l1_size; i++)
989 be64_to_cpus(&l1_table[i]);
990 }
f7d0fe02
KW
991
992 /* Do the actual checks */
993 for(i = 0; i < l1_size; i++) {
994 l2_offset = l1_table[i];
995 if (l2_offset) {
996 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
997 if (check_copied) {
998 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
999 >> s->cluster_bits);
1000 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1001 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1002 " refcount=%d\n", l2_offset, refcount);
1003 errors++;
1004 }
1005 }
1006
1007 /* Mark L2 table as used */
1008 l2_offset &= ~QCOW_OFLAG_COPIED;
1009 errors += inc_refcounts(bs, refcount_table,
1010 refcount_table_size,
1011 l2_offset,
1012 s->cluster_size);
1013
1014 /* L2 tables are cluster aligned */
1015 if (l2_offset & (s->cluster_size - 1)) {
1016 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1017 "cluster aligned; L1 entry corrupted\n", l2_offset);
1018 errors++;
1019 }
1020
1021 /* Process and check L2 entries */
1022 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
1023 l2_offset, check_copied);
1024 if (ret < 0) {
1025 goto fail;
1026 }
1027 errors += ret;
1028 }
1029 }
1030 qemu_free(l1_table);
1031 return errors;
1032
1033fail:
1034 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1035 qemu_free(l1_table);
1036 return -EIO;
1037}
1038
1039/*
1040 * Checks an image for refcount consistency.
1041 *
1042 * Returns 0 if no errors are found, the number of errors in case the image is
1043 * detected as corrupted, and -errno when an internal error occured.
1044 */
ed6ccf0f 1045int qcow2_check_refcounts(BlockDriverState *bs)
f7d0fe02
KW
1046{
1047 BDRVQcowState *s = bs->opaque;
1048 int64_t size;
1049 int nb_clusters, refcount1, refcount2, i;
1050 QCowSnapshot *sn;
1051 uint16_t *refcount_table;
1052 int ret, errors = 0;
1053
66f82cee 1054 size = bdrv_getlength(bs->file);
f7d0fe02
KW
1055 nb_clusters = size_to_clusters(s, size);
1056 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
1057
1058 /* header */
1059 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1060 0, s->cluster_size);
1061
1062 /* current L1 table */
1063 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
1064 s->l1_table_offset, s->l1_size, 1);
1065 if (ret < 0) {
1066 return ret;
1067 }
1068 errors += ret;
1069
1070 /* snapshots */
1071 for(i = 0; i < s->nb_snapshots; i++) {
1072 sn = s->snapshots + i;
1073 check_refcounts_l1(bs, refcount_table, nb_clusters,
1074 sn->l1_table_offset, sn->l1_size, 0);
1075 }
1076 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1077 s->snapshots_offset, s->snapshots_size);
1078
1079 /* refcount data */
1080 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1081 s->refcount_table_offset,
1082 s->refcount_table_size * sizeof(uint64_t));
1083 for(i = 0; i < s->refcount_table_size; i++) {
1084 int64_t offset;
1085 offset = s->refcount_table[i];
746c3cb5
KW
1086
1087 /* Refcount blocks are cluster aligned */
1088 if (offset & (s->cluster_size - 1)) {
1089 fprintf(stderr, "ERROR refcount block %d is not "
1090 "cluster aligned; refcount table entry corrupted\n", i);
1091 errors++;
1092 }
1093
f7d0fe02
KW
1094 if (offset != 0) {
1095 errors += inc_refcounts(bs, refcount_table, nb_clusters,
1096 offset, s->cluster_size);
746c3cb5
KW
1097 if (refcount_table[offset / s->cluster_size] != 1) {
1098 fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
1099 i, refcount_table[offset / s->cluster_size]);
1100 }
f7d0fe02
KW
1101 }
1102 }
1103
1104 /* compare ref counts */
1105 for(i = 0; i < nb_clusters; i++) {
1106 refcount1 = get_refcount(bs, i);
1107 refcount2 = refcount_table[i];
1108 if (refcount1 != refcount2) {
1109 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
1110 i, refcount1, refcount2);
1111 errors++;
1112 }
1113 }
1114
1115 qemu_free(refcount_table);
1116
1117 return errors;
1118}
1119