Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 93913dfd

History | View | Annotate | Download (38.4 kB)

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