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