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