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