]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-refcount.c
qcow2: Allow updating no refcounts
[mirror_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);
30static int update_refcount(BlockDriverState *bs,
31 int64_t offset, int64_t length,
32 int addend);
33
3b88e52b
KW
34
35static int cache_refcount_updates = 0;
36
37static int write_refcount_block(BDRVQcowState *s)
38{
39 size_t size = s->cluster_size;
40
41 if (s->refcount_block_cache_offset == 0) {
42 return 0;
43 }
44
45 if (bdrv_pwrite(s->hd, s->refcount_block_cache_offset,
46 s->refcount_block_cache, size) != size)
47 {
48 return -EIO;
49 }
50
51 return 0;
52}
53
f7d0fe02
KW
54/*********************************************************/
55/* refcount handling */
56
ed6ccf0f 57int qcow2_refcount_init(BlockDriverState *bs)
f7d0fe02
KW
58{
59 BDRVQcowState *s = bs->opaque;
60 int ret, refcount_table_size2, i;
61
62 s->refcount_block_cache = qemu_malloc(s->cluster_size);
63 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
64 s->refcount_table = qemu_malloc(refcount_table_size2);
65 if (s->refcount_table_size > 0) {
66 ret = bdrv_pread(s->hd, s->refcount_table_offset,
67 s->refcount_table, refcount_table_size2);
68 if (ret != refcount_table_size2)
69 goto fail;
70 for(i = 0; i < s->refcount_table_size; i++)
71 be64_to_cpus(&s->refcount_table[i]);
72 }
73 return 0;
74 fail:
75 return -ENOMEM;
76}
77
ed6ccf0f 78void qcow2_refcount_close(BlockDriverState *bs)
f7d0fe02
KW
79{
80 BDRVQcowState *s = bs->opaque;
81 qemu_free(s->refcount_block_cache);
82 qemu_free(s->refcount_table);
83}
84
85
86static int load_refcount_block(BlockDriverState *bs,
87 int64_t refcount_block_offset)
88{
89 BDRVQcowState *s = bs->opaque;
90 int ret;
3b88e52b
KW
91
92 if (cache_refcount_updates) {
93 write_refcount_block(s);
94 }
95
f7d0fe02
KW
96 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
97 s->cluster_size);
98 if (ret != s->cluster_size)
99 return -EIO;
100 s->refcount_block_cache_offset = refcount_block_offset;
101 return 0;
102}
103
104static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
105{
106 BDRVQcowState *s = bs->opaque;
107 int refcount_table_index, block_index;
108 int64_t refcount_block_offset;
109
110 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
111 if (refcount_table_index >= s->refcount_table_size)
112 return 0;
113 refcount_block_offset = s->refcount_table[refcount_table_index];
114 if (!refcount_block_offset)
115 return 0;
116 if (refcount_block_offset != s->refcount_block_cache_offset) {
117 /* better than nothing: return allocated if read error */
118 if (load_refcount_block(bs, refcount_block_offset) < 0)
119 return 1;
120 }
121 block_index = cluster_index &
122 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
123 return be16_to_cpu(s->refcount_block_cache[block_index]);
124}
125
126static int grow_refcount_table(BlockDriverState *bs, int min_size)
127{
128 BDRVQcowState *s = bs->opaque;
129 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
130 uint64_t *new_table;
131 int64_t table_offset;
132 uint8_t data[12];
133 int old_table_size;
134 int64_t old_table_offset;
135
136 if (min_size <= s->refcount_table_size)
137 return 0;
138 /* compute new table size */
139 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
140 for(;;) {
141 if (refcount_table_clusters == 0) {
142 refcount_table_clusters = 1;
143 } else {
144 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
145 }
146 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
147 if (min_size <= new_table_size)
148 break;
149 }
150#ifdef DEBUG_ALLOC2
151 printf("grow_refcount_table from %d to %d\n",
152 s->refcount_table_size,
153 new_table_size);
154#endif
155 new_table_size2 = new_table_size * sizeof(uint64_t);
156 new_table = qemu_mallocz(new_table_size2);
157 memcpy(new_table, s->refcount_table,
158 s->refcount_table_size * sizeof(uint64_t));
159 for(i = 0; i < s->refcount_table_size; i++)
160 cpu_to_be64s(&new_table[i]);
161 /* Note: we cannot update the refcount now to avoid recursion */
162 table_offset = alloc_clusters_noref(bs, new_table_size2);
163 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
164 if (ret != new_table_size2)
165 goto fail;
166 for(i = 0; i < s->refcount_table_size; i++)
167 be64_to_cpus(&new_table[i]);
168
169 cpu_to_be64w((uint64_t*)data, table_offset);
170 cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
f2b7c8b3
KW
171 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
172 data, sizeof(data));
173 if (ret != sizeof(data)) {
f7d0fe02 174 goto fail;
f2b7c8b3
KW
175 }
176
f7d0fe02
KW
177 qemu_free(s->refcount_table);
178 old_table_offset = s->refcount_table_offset;
179 old_table_size = s->refcount_table_size;
180 s->refcount_table = new_table;
181 s->refcount_table_size = new_table_size;
182 s->refcount_table_offset = table_offset;
183
184 update_refcount(bs, table_offset, new_table_size2, 1);
ed6ccf0f 185 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
f7d0fe02
KW
186 return 0;
187 fail:
f7d0fe02 188 qemu_free(new_table);
f2b7c8b3 189 return ret < 0 ? ret : -EIO;
f7d0fe02
KW
190}
191
192
193static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
194{
195 BDRVQcowState *s = bs->opaque;
196 int64_t offset, refcount_block_offset;
80ee15a6
KW
197 unsigned int refcount_table_index;
198 int ret;
f7d0fe02 199 uint64_t data64;
3b88e52b 200 int cache = cache_refcount_updates;
f7d0fe02
KW
201
202 /* Find L1 index and grow refcount table if needed */
203 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
204 if (refcount_table_index >= s->refcount_table_size) {
205 ret = grow_refcount_table(bs, refcount_table_index + 1);
206 if (ret < 0)
207 return ret;
208 }
209
210 /* Load or allocate the refcount block */
211 refcount_block_offset = s->refcount_table[refcount_table_index];
212 if (!refcount_block_offset) {
3b88e52b
KW
213 if (cache_refcount_updates) {
214 write_refcount_block(s);
215 cache_refcount_updates = 0;
216 }
f7d0fe02
KW
217 /* create a new refcount block */
218 /* Note: we cannot update the refcount now to avoid recursion */
219 offset = alloc_clusters_noref(bs, s->cluster_size);
220 memset(s->refcount_block_cache, 0, s->cluster_size);
221 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
222 if (ret != s->cluster_size)
223 return -EINVAL;
224 s->refcount_table[refcount_table_index] = offset;
225 data64 = cpu_to_be64(offset);
226 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
227 refcount_table_index * sizeof(uint64_t),
228 &data64, sizeof(data64));
229 if (ret != sizeof(data64))
230 return -EINVAL;
231
232 refcount_block_offset = offset;
233 s->refcount_block_cache_offset = offset;
234 update_refcount(bs, offset, s->cluster_size, 1);
3b88e52b 235 cache_refcount_updates = cache;
f7d0fe02
KW
236 } else {
237 if (refcount_block_offset != s->refcount_block_cache_offset) {
238 if (load_refcount_block(bs, refcount_block_offset) < 0)
239 return -EIO;
240 }
241 }
242
243 return refcount_block_offset;
244}
245
9923e05e
KW
246#define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
247static int write_refcount_block_entries(BDRVQcowState *s,
248 int64_t refcount_block_offset, int first_index, int last_index)
249{
250 size_t size;
251
3b88e52b
KW
252 if (cache_refcount_updates) {
253 return 0;
254 }
255
9923e05e
KW
256 first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
257 last_index = (last_index + REFCOUNTS_PER_SECTOR)
258 & ~(REFCOUNTS_PER_SECTOR - 1);
259
260 size = (last_index - first_index) << REFCOUNT_SHIFT;
261 if (bdrv_pwrite(s->hd,
262 refcount_block_offset + (first_index << REFCOUNT_SHIFT),
263 &s->refcount_block_cache[first_index], size) != size)
264 {
265 return -EIO;
266 }
267
268 return 0;
269}
270
f7d0fe02
KW
271/* XXX: cache several refcount block clusters ? */
272static int update_refcount(BlockDriverState *bs,
273 int64_t offset, int64_t length,
274 int addend)
275{
276 BDRVQcowState *s = bs->opaque;
277 int64_t start, last, cluster_offset;
278 int64_t refcount_block_offset = 0;
279 int64_t table_index = -1, old_table_index;
280 int first_index = -1, last_index = -1;
09508d13 281 int ret;
f7d0fe02
KW
282
283#ifdef DEBUG_ALLOC2
0bf9e31a 284 printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
f7d0fe02
KW
285 offset, length, addend);
286#endif
7322afe7 287 if (length < 0) {
f7d0fe02 288 return -EINVAL;
7322afe7
KW
289 } else if (length == 0) {
290 return 0;
291 }
292
f7d0fe02
KW
293 start = offset & ~(s->cluster_size - 1);
294 last = (offset + length - 1) & ~(s->cluster_size - 1);
295 for(cluster_offset = start; cluster_offset <= last;
296 cluster_offset += s->cluster_size)
297 {
298 int block_index, refcount;
299 int64_t cluster_index = cluster_offset >> s->cluster_bits;
09508d13 300 int64_t new_block;
f7d0fe02
KW
301
302 /* Only write refcount block to disk when we are done with it */
303 old_table_index = table_index;
304 table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
305 if ((old_table_index >= 0) && (table_index != old_table_index)) {
9923e05e
KW
306
307 if (write_refcount_block_entries(s, refcount_block_offset,
308 first_index, last_index) < 0)
f7d0fe02
KW
309 {
310 return -EIO;
311 }
312
313 first_index = -1;
314 last_index = -1;
315 }
316
317 /* Load the refcount block and allocate it if needed */
09508d13
KW
318 new_block = alloc_refcount_block(bs, cluster_index);
319 if (new_block < 0) {
320 ret = new_block;
321 goto fail;
f7d0fe02 322 }
09508d13 323 refcount_block_offset = new_block;
f7d0fe02
KW
324
325 /* we can update the count and save it */
326 block_index = cluster_index &
327 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
328 if (first_index == -1 || block_index < first_index) {
329 first_index = block_index;
330 }
331 if (block_index > last_index) {
332 last_index = block_index;
333 }
334
335 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
336 refcount += addend;
09508d13
KW
337 if (refcount < 0 || refcount > 0xffff) {
338 ret = -EINVAL;
339 goto fail;
340 }
f7d0fe02
KW
341 if (refcount == 0 && cluster_index < s->free_cluster_index) {
342 s->free_cluster_index = cluster_index;
343 }
344 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
345 }
346
09508d13
KW
347 ret = 0;
348fail:
349
f7d0fe02
KW
350 /* Write last changed block to disk */
351 if (refcount_block_offset != 0) {
9923e05e
KW
352 if (write_refcount_block_entries(s, refcount_block_offset,
353 first_index, last_index) < 0)
f7d0fe02 354 {
09508d13 355 return ret < 0 ? ret : -EIO;
f7d0fe02
KW
356 }
357 }
358
09508d13
KW
359 /*
360 * Try do undo any updates if an error is returned (This may succeed in
361 * some cases like ENOSPC for allocating a new refcount block)
362 */
363 if (ret < 0) {
364 int dummy;
365 dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
366 }
367
368 return ret;
f7d0fe02
KW
369}
370
371/* addend must be 1 or -1 */
372static int update_cluster_refcount(BlockDriverState *bs,
373 int64_t cluster_index,
374 int addend)
375{
376 BDRVQcowState *s = bs->opaque;
377 int ret;
378
379 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
380 if (ret < 0) {
381 return ret;
382 }
383
384 return get_refcount(bs, cluster_index);
385}
386
387
388
389/*********************************************************/
390/* cluster allocation functions */
391
392
393
394/* return < 0 if error */
395static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
396{
397 BDRVQcowState *s = bs->opaque;
398 int i, nb_clusters;
399
400 nb_clusters = size_to_clusters(s, size);
401retry:
402 for(i = 0; i < nb_clusters; i++) {
403 int64_t i = s->free_cluster_index++;
404 if (get_refcount(bs, i) != 0)
405 goto retry;
406 }
407#ifdef DEBUG_ALLOC2
0bf9e31a 408 printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
f7d0fe02
KW
409 size,
410 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
411#endif
412 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
413}
414
ed6ccf0f 415int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
f7d0fe02
KW
416{
417 int64_t offset;
418
419 offset = alloc_clusters_noref(bs, size);
420 update_refcount(bs, offset, size, 1);
421 return offset;
422}
423
424/* only used to allocate compressed sectors. We try to allocate
425 contiguous sectors. size must be <= cluster_size */
ed6ccf0f 426int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
f7d0fe02
KW
427{
428 BDRVQcowState *s = bs->opaque;
429 int64_t offset, cluster_offset;
430 int free_in_cluster;
431
432 assert(size > 0 && size <= s->cluster_size);
433 if (s->free_byte_offset == 0) {
ed6ccf0f 434 s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
f7d0fe02
KW
435 }
436 redo:
437 free_in_cluster = s->cluster_size -
438 (s->free_byte_offset & (s->cluster_size - 1));
439 if (size <= free_in_cluster) {
440 /* enough space in current cluster */
441 offset = s->free_byte_offset;
442 s->free_byte_offset += size;
443 free_in_cluster -= size;
444 if (free_in_cluster == 0)
445 s->free_byte_offset = 0;
446 if ((offset & (s->cluster_size - 1)) != 0)
447 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
448 } else {
ed6ccf0f 449 offset = qcow2_alloc_clusters(bs, s->cluster_size);
f7d0fe02
KW
450 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
451 if ((cluster_offset + s->cluster_size) == offset) {
452 /* we are lucky: contiguous data */
453 offset = s->free_byte_offset;
454 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
455 s->free_byte_offset += size;
456 } else {
457 s->free_byte_offset = offset;
458 goto redo;
459 }
460 }
461 return offset;
462}
463
ed6ccf0f 464void qcow2_free_clusters(BlockDriverState *bs,
f7d0fe02
KW
465 int64_t offset, int64_t size)
466{
467 update_refcount(bs, offset, size, -1);
468}
469
45aba42f
KW
470/*
471 * free_any_clusters
472 *
473 * free clusters according to its type: compressed or not
474 *
475 */
476
ed6ccf0f 477void qcow2_free_any_clusters(BlockDriverState *bs,
45aba42f
KW
478 uint64_t cluster_offset, int nb_clusters)
479{
480 BDRVQcowState *s = bs->opaque;
481
482 /* free the cluster */
483
484 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
485 int nb_csectors;
486 nb_csectors = ((cluster_offset >> s->csize_shift) &
487 s->csize_mask) + 1;
ed6ccf0f
KW
488 qcow2_free_clusters(bs,
489 (cluster_offset & s->cluster_offset_mask) & ~511,
490 nb_csectors * 512);
45aba42f
KW
491 return;
492 }
493
ed6ccf0f 494 qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
45aba42f
KW
495
496 return;
497}
498
f7d0fe02
KW
499
500
501/*********************************************************/
502/* snapshots and image creation */
503
504
505
ed6ccf0f
KW
506void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
507 int64_t size)
f7d0fe02
KW
508{
509 int refcount;
510 int64_t start, last, cluster_offset;
511 uint16_t *p;
512
513 start = offset & ~(s->cluster_size - 1);
514 last = (offset + size - 1) & ~(s->cluster_size - 1);
515 for(cluster_offset = start; cluster_offset <= last;
516 cluster_offset += s->cluster_size) {
517 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
518 refcount = be16_to_cpu(*p);
519 refcount++;
520 *p = cpu_to_be16(refcount);
521 }
522}
523
524/* update the refcounts of snapshots and the copied flag */
ed6ccf0f
KW
525int qcow2_update_snapshot_refcount(BlockDriverState *bs,
526 int64_t l1_table_offset, int l1_size, int addend)
f7d0fe02
KW
527{
528 BDRVQcowState *s = bs->opaque;
529 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
530 int64_t old_offset, old_l2_offset;
531 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
532
ed6ccf0f 533 qcow2_l2_cache_reset(bs);
3b88e52b 534 cache_refcount_updates = 1;
f7d0fe02
KW
535
536 l2_table = NULL;
537 l1_table = NULL;
538 l1_size2 = l1_size * sizeof(uint64_t);
f7d0fe02 539 if (l1_table_offset != s->l1_table_offset) {
702ef63f
KW
540 if (l1_size2 != 0) {
541 l1_table = qemu_mallocz(align_offset(l1_size2, 512));
542 } else {
543 l1_table = NULL;
544 }
f7d0fe02
KW
545 l1_allocated = 1;
546 if (bdrv_pread(s->hd, l1_table_offset,
547 l1_table, l1_size2) != l1_size2)
548 goto fail;
549 for(i = 0;i < l1_size; i++)
550 be64_to_cpus(&l1_table[i]);
551 } else {
552 assert(l1_size == s->l1_size);
553 l1_table = s->l1_table;
554 l1_allocated = 0;
555 }
556
557 l2_size = s->l2_size * sizeof(uint64_t);
558 l2_table = qemu_malloc(l2_size);
559 l1_modified = 0;
560 for(i = 0; i < l1_size; i++) {
561 l2_offset = l1_table[i];
562 if (l2_offset) {
563 old_l2_offset = l2_offset;
564 l2_offset &= ~QCOW_OFLAG_COPIED;
565 l2_modified = 0;
566 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
567 goto fail;
568 for(j = 0; j < s->l2_size; j++) {
569 offset = be64_to_cpu(l2_table[j]);
570 if (offset != 0) {
571 old_offset = offset;
572 offset &= ~QCOW_OFLAG_COPIED;
573 if (offset & QCOW_OFLAG_COMPRESSED) {
574 nb_csectors = ((offset >> s->csize_shift) &
575 s->csize_mask) + 1;
576 if (addend != 0)
577 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
578 nb_csectors * 512, addend);
579 /* compressed clusters are never modified */
580 refcount = 2;
581 } else {
582 if (addend != 0) {
583 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
584 } else {
585 refcount = get_refcount(bs, offset >> s->cluster_bits);
586 }
587 }
588
589 if (refcount == 1) {
590 offset |= QCOW_OFLAG_COPIED;
591 }
592 if (offset != old_offset) {
593 l2_table[j] = cpu_to_be64(offset);
594 l2_modified = 1;
595 }
596 }
597 }
598 if (l2_modified) {
599 if (bdrv_pwrite(s->hd,
600 l2_offset, l2_table, l2_size) != l2_size)
601 goto fail;
602 }
603
604 if (addend != 0) {
605 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
606 } else {
607 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
608 }
609 if (refcount == 1) {
610 l2_offset |= QCOW_OFLAG_COPIED;
611 }
612 if (l2_offset != old_l2_offset) {
613 l1_table[i] = l2_offset;
614 l1_modified = 1;
615 }
616 }
617 }
618 if (l1_modified) {
619 for(i = 0; i < l1_size; i++)
620 cpu_to_be64s(&l1_table[i]);
621 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
622 l1_size2) != l1_size2)
623 goto fail;
624 for(i = 0; i < l1_size; i++)
625 be64_to_cpus(&l1_table[i]);
626 }
627 if (l1_allocated)
628 qemu_free(l1_table);
629 qemu_free(l2_table);
3b88e52b
KW
630 cache_refcount_updates = 0;
631 write_refcount_block(s);
f7d0fe02
KW
632 return 0;
633 fail:
634 if (l1_allocated)
635 qemu_free(l1_table);
636 qemu_free(l2_table);
3b88e52b
KW
637 cache_refcount_updates = 0;
638 write_refcount_block(s);
f7d0fe02
KW
639 return -EIO;
640}
641
642
643
644
645/*********************************************************/
646/* refcount checking functions */
647
648
649
650/*
651 * Increases the refcount for a range of clusters in a given refcount table.
652 * This is used to construct a temporary refcount table out of L1 and L2 tables
653 * which can be compared the the refcount table saved in the image.
654 *
655 * Returns the number of errors in the image that were found
656 */
657static int inc_refcounts(BlockDriverState *bs,
658 uint16_t *refcount_table,
659 int refcount_table_size,
660 int64_t offset, int64_t size)
661{
662 BDRVQcowState *s = bs->opaque;
663 int64_t start, last, cluster_offset;
664 int k;
665 int errors = 0;
666
667 if (size <= 0)
668 return 0;
669
670 start = offset & ~(s->cluster_size - 1);
671 last = (offset + size - 1) & ~(s->cluster_size - 1);
672 for(cluster_offset = start; cluster_offset <= last;
673 cluster_offset += s->cluster_size) {
674 k = cluster_offset >> s->cluster_bits;
675 if (k < 0 || k >= refcount_table_size) {
676 fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
677 cluster_offset);
678 errors++;
679 } else {
680 if (++refcount_table[k] == 0) {
681 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
682 "\n", cluster_offset);
683 errors++;
684 }
685 }
686 }
687
688 return errors;
689}
690
691/*
692 * Increases the refcount in the given refcount table for the all clusters
693 * referenced in the L2 table. While doing so, performs some checks on L2
694 * entries.
695 *
696 * Returns the number of errors found by the checks or -errno if an internal
697 * error occurred.
698 */
699static int check_refcounts_l2(BlockDriverState *bs,
700 uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
701 int check_copied)
702{
703 BDRVQcowState *s = bs->opaque;
704 uint64_t *l2_table, offset;
705 int i, l2_size, nb_csectors, refcount;
706 int errors = 0;
707
708 /* Read L2 table from disk */
709 l2_size = s->l2_size * sizeof(uint64_t);
710 l2_table = qemu_malloc(l2_size);
711
712 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
713 goto fail;
714
715 /* Do the actual checks */
716 for(i = 0; i < s->l2_size; i++) {
717 offset = be64_to_cpu(l2_table[i]);
718 if (offset != 0) {
719 if (offset & QCOW_OFLAG_COMPRESSED) {
720 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
721 if (offset & QCOW_OFLAG_COPIED) {
722 fprintf(stderr, "ERROR: cluster %" PRId64 ": "
723 "copied flag must never be set for compressed "
724 "clusters\n", offset >> s->cluster_bits);
725 offset &= ~QCOW_OFLAG_COPIED;
726 errors++;
727 }
728
729 /* Mark cluster as used */
730 nb_csectors = ((offset >> s->csize_shift) &
731 s->csize_mask) + 1;
732 offset &= s->cluster_offset_mask;
733 errors += inc_refcounts(bs, refcount_table,
734 refcount_table_size,
735 offset & ~511, nb_csectors * 512);
736 } else {
737 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
738 if (check_copied) {
739 uint64_t entry = offset;
740 offset &= ~QCOW_OFLAG_COPIED;
741 refcount = get_refcount(bs, offset >> s->cluster_bits);
742 if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
743 fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
744 PRIx64 " refcount=%d\n", entry, refcount);
745 errors++;
746 }
747 }
748
749 /* Mark cluster as used */
750 offset &= ~QCOW_OFLAG_COPIED;
751 errors += inc_refcounts(bs, refcount_table,
752 refcount_table_size,
753 offset, s->cluster_size);
754
755 /* Correct offsets are cluster aligned */
756 if (offset & (s->cluster_size - 1)) {
757 fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
758 "properly aligned; L2 entry corrupted.\n", offset);
759 errors++;
760 }
761 }
762 }
763 }
764
765 qemu_free(l2_table);
766 return errors;
767
768fail:
769 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
770 qemu_free(l2_table);
771 return -EIO;
772}
773
774/*
775 * Increases the refcount for the L1 table, its L2 tables and all referenced
776 * clusters in the given refcount table. While doing so, performs some checks
777 * on L1 and L2 entries.
778 *
779 * Returns the number of errors found by the checks or -errno if an internal
780 * error occurred.
781 */
782static int check_refcounts_l1(BlockDriverState *bs,
783 uint16_t *refcount_table,
784 int refcount_table_size,
785 int64_t l1_table_offset, int l1_size,
786 int check_copied)
787{
788 BDRVQcowState *s = bs->opaque;
789 uint64_t *l1_table, l2_offset, l1_size2;
790 int i, refcount, ret;
791 int errors = 0;
792
793 l1_size2 = l1_size * sizeof(uint64_t);
794
795 /* Mark L1 table as used */
796 errors += inc_refcounts(bs, refcount_table, refcount_table_size,
797 l1_table_offset, l1_size2);
798
799 /* Read L1 table entries from disk */
702ef63f
KW
800 if (l1_size2 == 0) {
801 l1_table = NULL;
802 } else {
803 l1_table = qemu_malloc(l1_size2);
804 if (bdrv_pread(s->hd, l1_table_offset,
805 l1_table, l1_size2) != l1_size2)
806 goto fail;
807 for(i = 0;i < l1_size; i++)
808 be64_to_cpus(&l1_table[i]);
809 }
f7d0fe02
KW
810
811 /* Do the actual checks */
812 for(i = 0; i < l1_size; i++) {
813 l2_offset = l1_table[i];
814 if (l2_offset) {
815 /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
816 if (check_copied) {
817 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
818 >> s->cluster_bits);
819 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
820 fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
821 " refcount=%d\n", l2_offset, refcount);
822 errors++;
823 }
824 }
825
826 /* Mark L2 table as used */
827 l2_offset &= ~QCOW_OFLAG_COPIED;
828 errors += inc_refcounts(bs, refcount_table,
829 refcount_table_size,
830 l2_offset,
831 s->cluster_size);
832
833 /* L2 tables are cluster aligned */
834 if (l2_offset & (s->cluster_size - 1)) {
835 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
836 "cluster aligned; L1 entry corrupted\n", l2_offset);
837 errors++;
838 }
839
840 /* Process and check L2 entries */
841 ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
842 l2_offset, check_copied);
843 if (ret < 0) {
844 goto fail;
845 }
846 errors += ret;
847 }
848 }
849 qemu_free(l1_table);
850 return errors;
851
852fail:
853 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
854 qemu_free(l1_table);
855 return -EIO;
856}
857
858/*
859 * Checks an image for refcount consistency.
860 *
861 * Returns 0 if no errors are found, the number of errors in case the image is
862 * detected as corrupted, and -errno when an internal error occured.
863 */
ed6ccf0f 864int qcow2_check_refcounts(BlockDriverState *bs)
f7d0fe02
KW
865{
866 BDRVQcowState *s = bs->opaque;
867 int64_t size;
868 int nb_clusters, refcount1, refcount2, i;
869 QCowSnapshot *sn;
870 uint16_t *refcount_table;
871 int ret, errors = 0;
872
873 size = bdrv_getlength(s->hd);
874 nb_clusters = size_to_clusters(s, size);
875 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
876
877 /* header */
878 errors += inc_refcounts(bs, refcount_table, nb_clusters,
879 0, s->cluster_size);
880
881 /* current L1 table */
882 ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
883 s->l1_table_offset, s->l1_size, 1);
884 if (ret < 0) {
885 return ret;
886 }
887 errors += ret;
888
889 /* snapshots */
890 for(i = 0; i < s->nb_snapshots; i++) {
891 sn = s->snapshots + i;
892 check_refcounts_l1(bs, refcount_table, nb_clusters,
893 sn->l1_table_offset, sn->l1_size, 0);
894 }
895 errors += inc_refcounts(bs, refcount_table, nb_clusters,
896 s->snapshots_offset, s->snapshots_size);
897
898 /* refcount data */
899 errors += inc_refcounts(bs, refcount_table, nb_clusters,
900 s->refcount_table_offset,
901 s->refcount_table_size * sizeof(uint64_t));
902 for(i = 0; i < s->refcount_table_size; i++) {
903 int64_t offset;
904 offset = s->refcount_table[i];
905 if (offset != 0) {
906 errors += inc_refcounts(bs, refcount_table, nb_clusters,
907 offset, s->cluster_size);
908 }
909 }
910
911 /* compare ref counts */
912 for(i = 0; i < nb_clusters; i++) {
913 refcount1 = get_refcount(bs, i);
914 refcount2 = refcount_table[i];
915 if (refcount1 != refcount2) {
916 fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
917 i, refcount1, refcount2);
918 errors++;
919 }
920 }
921
922 qemu_free(refcount_table);
923
924 return errors;
925}
926