Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 206e6d85

History | View | Annotate | Download (39.7 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 7267c094 Anthony Liguori
    s->refcount_table = g_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 7267c094 Anthony Liguori
    g_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 76dc9e0c Kevin Wolf
            s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
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 7267c094 Anthony Liguori
    uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size);
327 7267c094 Anthony Liguori
    uint64_t *new_table = g_malloc0(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 7267c094 Anthony Liguori
    g_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 87267753 Zhi Yong Wu
        be64_to_cpus(&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 7267c094 Anthony Liguori
    g_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 2795ecf6 Kevin Wolf
    return 0;
404 f7d0fe02 Kevin Wolf
405 92dcb59f Kevin Wolf
fail_table:
406 7267c094 Anthony Liguori
    g_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 35ee5e39 Frediano Ziglio
    fprintf(stderr, "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 35ee5e39 Frediano Ziglio
    fprintf(stderr, "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 256900b1 Kevin Wolf
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
586 256900b1 Kevin Wolf
    int nb_clusters)
587 256900b1 Kevin Wolf
{
588 256900b1 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
589 256900b1 Kevin Wolf
    uint64_t cluster_index;
590 f24423bd Kevin Wolf
    uint64_t old_free_cluster_index;
591 256900b1 Kevin Wolf
    int i, refcount, ret;
592 256900b1 Kevin Wolf
593 256900b1 Kevin Wolf
    /* Check how many clusters there are free */
594 256900b1 Kevin Wolf
    cluster_index = offset >> s->cluster_bits;
595 256900b1 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
596 256900b1 Kevin Wolf
        refcount = get_refcount(bs, cluster_index++);
597 256900b1 Kevin Wolf
598 256900b1 Kevin Wolf
        if (refcount < 0) {
599 256900b1 Kevin Wolf
            return refcount;
600 256900b1 Kevin Wolf
        } else if (refcount != 0) {
601 256900b1 Kevin Wolf
            break;
602 256900b1 Kevin Wolf
        }
603 256900b1 Kevin Wolf
    }
604 256900b1 Kevin Wolf
605 256900b1 Kevin Wolf
    /* And then allocate them */
606 f24423bd Kevin Wolf
    old_free_cluster_index = s->free_cluster_index;
607 f24423bd Kevin Wolf
    s->free_cluster_index = cluster_index + i;
608 f24423bd Kevin Wolf
609 256900b1 Kevin Wolf
    ret = update_refcount(bs, offset, i << s->cluster_bits, 1);
610 256900b1 Kevin Wolf
    if (ret < 0) {
611 256900b1 Kevin Wolf
        return ret;
612 256900b1 Kevin Wolf
    }
613 256900b1 Kevin Wolf
614 f24423bd Kevin Wolf
    s->free_cluster_index = old_free_cluster_index;
615 f24423bd Kevin Wolf
616 256900b1 Kevin Wolf
    return i;
617 256900b1 Kevin Wolf
}
618 256900b1 Kevin Wolf
619 f7d0fe02 Kevin Wolf
/* only used to allocate compressed sectors. We try to allocate
620 f7d0fe02 Kevin Wolf
   contiguous sectors. size must be <= cluster_size */
621 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
622 f7d0fe02 Kevin Wolf
{
623 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
624 f7d0fe02 Kevin Wolf
    int64_t offset, cluster_offset;
625 f7d0fe02 Kevin Wolf
    int free_in_cluster;
626 f7d0fe02 Kevin Wolf
627 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
628 f7d0fe02 Kevin Wolf
    assert(size > 0 && size <= s->cluster_size);
629 f7d0fe02 Kevin Wolf
    if (s->free_byte_offset == 0) {
630 206e6d85 Stefan Hajnoczi
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
631 206e6d85 Stefan Hajnoczi
        if (offset < 0) {
632 206e6d85 Stefan Hajnoczi
            return offset;
633 5d757b56 Kevin Wolf
        }
634 206e6d85 Stefan Hajnoczi
        s->free_byte_offset = offset;
635 f7d0fe02 Kevin Wolf
    }
636 f7d0fe02 Kevin Wolf
 redo:
637 f7d0fe02 Kevin Wolf
    free_in_cluster = s->cluster_size -
638 f7d0fe02 Kevin Wolf
        (s->free_byte_offset & (s->cluster_size - 1));
639 f7d0fe02 Kevin Wolf
    if (size <= free_in_cluster) {
640 f7d0fe02 Kevin Wolf
        /* enough space in current cluster */
641 f7d0fe02 Kevin Wolf
        offset = s->free_byte_offset;
642 f7d0fe02 Kevin Wolf
        s->free_byte_offset += size;
643 f7d0fe02 Kevin Wolf
        free_in_cluster -= size;
644 f7d0fe02 Kevin Wolf
        if (free_in_cluster == 0)
645 f7d0fe02 Kevin Wolf
            s->free_byte_offset = 0;
646 f7d0fe02 Kevin Wolf
        if ((offset & (s->cluster_size - 1)) != 0)
647 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
648 f7d0fe02 Kevin Wolf
    } else {
649 ed6ccf0f Kevin Wolf
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
650 5d757b56 Kevin Wolf
        if (offset < 0) {
651 5d757b56 Kevin Wolf
            return offset;
652 5d757b56 Kevin Wolf
        }
653 f7d0fe02 Kevin Wolf
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
654 f7d0fe02 Kevin Wolf
        if ((cluster_offset + s->cluster_size) == offset) {
655 f7d0fe02 Kevin Wolf
            /* we are lucky: contiguous data */
656 f7d0fe02 Kevin Wolf
            offset = s->free_byte_offset;
657 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
658 f7d0fe02 Kevin Wolf
            s->free_byte_offset += size;
659 f7d0fe02 Kevin Wolf
        } else {
660 f7d0fe02 Kevin Wolf
            s->free_byte_offset = offset;
661 f7d0fe02 Kevin Wolf
            goto redo;
662 f7d0fe02 Kevin Wolf
        }
663 f7d0fe02 Kevin Wolf
    }
664 29216ed1 Kevin Wolf
665 29216ed1 Kevin Wolf
    bdrv_flush(bs->file);
666 f7d0fe02 Kevin Wolf
    return offset;
667 f7d0fe02 Kevin Wolf
}
668 f7d0fe02 Kevin Wolf
669 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
670 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
671 f7d0fe02 Kevin Wolf
{
672 db3a964f Kevin Wolf
    int ret;
673 db3a964f Kevin Wolf
674 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
675 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, -1);
676 db3a964f Kevin Wolf
    if (ret < 0) {
677 db3a964f Kevin Wolf
        fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
678 003fad6e Kevin Wolf
        /* TODO Remember the clusters to free them later and avoid leaking */
679 db3a964f Kevin Wolf
    }
680 f7d0fe02 Kevin Wolf
}
681 f7d0fe02 Kevin Wolf
682 45aba42f Kevin Wolf
/*
683 c7a4c37a Kevin Wolf
 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
684 c7a4c37a Kevin Wolf
 * normal cluster, compressed cluster, etc.)
685 45aba42f Kevin Wolf
 */
686 ed6ccf0f Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs,
687 c7a4c37a Kevin Wolf
    uint64_t l2_entry, int nb_clusters)
688 45aba42f Kevin Wolf
{
689 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
690 45aba42f Kevin Wolf
691 c7a4c37a Kevin Wolf
    switch (qcow2_get_cluster_type(l2_entry)) {
692 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_COMPRESSED:
693 c7a4c37a Kevin Wolf
        {
694 c7a4c37a Kevin Wolf
            int nb_csectors;
695 c7a4c37a Kevin Wolf
            nb_csectors = ((l2_entry >> s->csize_shift) &
696 c7a4c37a Kevin Wolf
                           s->csize_mask) + 1;
697 c7a4c37a Kevin Wolf
            qcow2_free_clusters(bs,
698 c7a4c37a Kevin Wolf
                (l2_entry & s->cluster_offset_mask) & ~511,
699 c7a4c37a Kevin Wolf
                nb_csectors * 512);
700 c7a4c37a Kevin Wolf
        }
701 c7a4c37a Kevin Wolf
        break;
702 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_NORMAL:
703 c7a4c37a Kevin Wolf
        qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
704 c7a4c37a Kevin Wolf
                            nb_clusters << s->cluster_bits);
705 c7a4c37a Kevin Wolf
        break;
706 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_UNALLOCATED:
707 6377af48 Kevin Wolf
    case QCOW2_CLUSTER_ZERO:
708 c7a4c37a Kevin Wolf
        break;
709 c7a4c37a Kevin Wolf
    default:
710 c7a4c37a Kevin Wolf
        abort();
711 45aba42f Kevin Wolf
    }
712 45aba42f Kevin Wolf
}
713 45aba42f Kevin Wolf
714 f7d0fe02 Kevin Wolf
715 f7d0fe02 Kevin Wolf
716 f7d0fe02 Kevin Wolf
/*********************************************************/
717 f7d0fe02 Kevin Wolf
/* snapshots and image creation */
718 f7d0fe02 Kevin Wolf
719 f7d0fe02 Kevin Wolf
720 f7d0fe02 Kevin Wolf
721 f7d0fe02 Kevin Wolf
/* update the refcounts of snapshots and the copied flag */
722 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
723 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend)
724 f7d0fe02 Kevin Wolf
{
725 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
726 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
727 f7d0fe02 Kevin Wolf
    int64_t old_offset, old_l2_offset;
728 93913dfd Kevin Wolf
    int i, j, l1_modified = 0, nb_csectors, refcount;
729 29c1a730 Kevin Wolf
    int ret;
730 f7d0fe02 Kevin Wolf
731 f7d0fe02 Kevin Wolf
    l2_table = NULL;
732 f7d0fe02 Kevin Wolf
    l1_table = NULL;
733 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
734 43a0cac4 Kevin Wolf
735 43a0cac4 Kevin Wolf
    /* WARNING: qcow2_snapshot_goto relies on this function not using the
736 43a0cac4 Kevin Wolf
     * l1_table_offset when it is the current s->l1_table_offset! Be careful
737 43a0cac4 Kevin Wolf
     * when changing this! */
738 f7d0fe02 Kevin Wolf
    if (l1_table_offset != s->l1_table_offset) {
739 702ef63f Kevin Wolf
        if (l1_size2 != 0) {
740 7267c094 Anthony Liguori
            l1_table = g_malloc0(align_offset(l1_size2, 512));
741 702ef63f Kevin Wolf
        } else {
742 702ef63f Kevin Wolf
            l1_table = NULL;
743 702ef63f Kevin Wolf
        }
744 f7d0fe02 Kevin Wolf
        l1_allocated = 1;
745 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, l1_table_offset,
746 f7d0fe02 Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
747 93913dfd Kevin Wolf
        {
748 93913dfd Kevin Wolf
            ret = -EIO;
749 f7d0fe02 Kevin Wolf
            goto fail;
750 93913dfd Kevin Wolf
        }
751 93913dfd Kevin Wolf
752 f7d0fe02 Kevin Wolf
        for(i = 0;i < l1_size; i++)
753 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
754 f7d0fe02 Kevin Wolf
    } else {
755 f7d0fe02 Kevin Wolf
        assert(l1_size == s->l1_size);
756 f7d0fe02 Kevin Wolf
        l1_table = s->l1_table;
757 f7d0fe02 Kevin Wolf
        l1_allocated = 0;
758 f7d0fe02 Kevin Wolf
    }
759 f7d0fe02 Kevin Wolf
760 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
761 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
762 f7d0fe02 Kevin Wolf
        if (l2_offset) {
763 f7d0fe02 Kevin Wolf
            old_l2_offset = l2_offset;
764 8e37f681 Kevin Wolf
            l2_offset &= L1E_OFFSET_MASK;
765 29c1a730 Kevin Wolf
766 29c1a730 Kevin Wolf
            ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
767 29c1a730 Kevin Wolf
                (void**) &l2_table);
768 29c1a730 Kevin Wolf
            if (ret < 0) {
769 f7d0fe02 Kevin Wolf
                goto fail;
770 29c1a730 Kevin Wolf
            }
771 29c1a730 Kevin Wolf
772 f7d0fe02 Kevin Wolf
            for(j = 0; j < s->l2_size; j++) {
773 f7d0fe02 Kevin Wolf
                offset = be64_to_cpu(l2_table[j]);
774 f7d0fe02 Kevin Wolf
                if (offset != 0) {
775 f7d0fe02 Kevin Wolf
                    old_offset = offset;
776 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
777 f7d0fe02 Kevin Wolf
                    if (offset & QCOW_OFLAG_COMPRESSED) {
778 f7d0fe02 Kevin Wolf
                        nb_csectors = ((offset >> s->csize_shift) &
779 f7d0fe02 Kevin Wolf
                                       s->csize_mask) + 1;
780 db3a964f Kevin Wolf
                        if (addend != 0) {
781 db3a964f Kevin Wolf
                            int ret;
782 db3a964f Kevin Wolf
                            ret = update_refcount(bs,
783 db3a964f Kevin Wolf
                                (offset & s->cluster_offset_mask) & ~511,
784 db3a964f Kevin Wolf
                                nb_csectors * 512, addend);
785 db3a964f Kevin Wolf
                            if (ret < 0) {
786 db3a964f Kevin Wolf
                                goto fail;
787 db3a964f Kevin Wolf
                            }
788 1c4c2814 Kevin Wolf
789 1c4c2814 Kevin Wolf
                            /* TODO Flushing once for the whole function should
790 1c4c2814 Kevin Wolf
                             * be enough */
791 1c4c2814 Kevin Wolf
                            bdrv_flush(bs->file);
792 db3a964f Kevin Wolf
                        }
793 f7d0fe02 Kevin Wolf
                        /* compressed clusters are never modified */
794 f7d0fe02 Kevin Wolf
                        refcount = 2;
795 f7d0fe02 Kevin Wolf
                    } else {
796 8e37f681 Kevin Wolf
                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
797 f7d0fe02 Kevin Wolf
                        if (addend != 0) {
798 8e37f681 Kevin Wolf
                            refcount = update_cluster_refcount(bs, cluster_index, addend);
799 f7d0fe02 Kevin Wolf
                        } else {
800 8e37f681 Kevin Wolf
                            refcount = get_refcount(bs, cluster_index);
801 f7d0fe02 Kevin Wolf
                        }
802 018faafd Kevin Wolf
803 018faafd Kevin Wolf
                        if (refcount < 0) {
804 93913dfd Kevin Wolf
                            ret = -EIO;
805 018faafd Kevin Wolf
                            goto fail;
806 018faafd Kevin Wolf
                        }
807 f7d0fe02 Kevin Wolf
                    }
808 f7d0fe02 Kevin Wolf
809 f7d0fe02 Kevin Wolf
                    if (refcount == 1) {
810 f7d0fe02 Kevin Wolf
                        offset |= QCOW_OFLAG_COPIED;
811 f7d0fe02 Kevin Wolf
                    }
812 f7d0fe02 Kevin Wolf
                    if (offset != old_offset) {
813 29c1a730 Kevin Wolf
                        if (addend > 0) {
814 29c1a730 Kevin Wolf
                            qcow2_cache_set_dependency(bs, s->l2_table_cache,
815 29c1a730 Kevin Wolf
                                s->refcount_block_cache);
816 29c1a730 Kevin Wolf
                        }
817 f7d0fe02 Kevin Wolf
                        l2_table[j] = cpu_to_be64(offset);
818 29c1a730 Kevin Wolf
                        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
819 f7d0fe02 Kevin Wolf
                    }
820 f7d0fe02 Kevin Wolf
                }
821 f7d0fe02 Kevin Wolf
            }
822 29c1a730 Kevin Wolf
823 29c1a730 Kevin Wolf
            ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
824 29c1a730 Kevin Wolf
            if (ret < 0) {
825 29c1a730 Kevin Wolf
                goto fail;
826 f7d0fe02 Kevin Wolf
            }
827 f7d0fe02 Kevin Wolf
828 29c1a730 Kevin Wolf
829 f7d0fe02 Kevin Wolf
            if (addend != 0) {
830 f7d0fe02 Kevin Wolf
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
831 f7d0fe02 Kevin Wolf
            } else {
832 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
833 f7d0fe02 Kevin Wolf
            }
834 018faafd Kevin Wolf
            if (refcount < 0) {
835 93913dfd Kevin Wolf
                ret = -EIO;
836 018faafd Kevin Wolf
                goto fail;
837 018faafd Kevin Wolf
            } else if (refcount == 1) {
838 f7d0fe02 Kevin Wolf
                l2_offset |= QCOW_OFLAG_COPIED;
839 f7d0fe02 Kevin Wolf
            }
840 f7d0fe02 Kevin Wolf
            if (l2_offset != old_l2_offset) {
841 f7d0fe02 Kevin Wolf
                l1_table[i] = l2_offset;
842 f7d0fe02 Kevin Wolf
                l1_modified = 1;
843 f7d0fe02 Kevin Wolf
            }
844 f7d0fe02 Kevin Wolf
        }
845 f7d0fe02 Kevin Wolf
    }
846 93913dfd Kevin Wolf
847 93913dfd Kevin Wolf
    ret = 0;
848 93913dfd Kevin Wolf
fail:
849 93913dfd Kevin Wolf
    if (l2_table) {
850 93913dfd Kevin Wolf
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
851 93913dfd Kevin Wolf
    }
852 93913dfd Kevin Wolf
853 43a0cac4 Kevin Wolf
    /* Update L1 only if it isn't deleted anyway (addend = -1) */
854 43a0cac4 Kevin Wolf
    if (addend >= 0 && l1_modified) {
855 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
856 f7d0fe02 Kevin Wolf
            cpu_to_be64s(&l1_table[i]);
857 8b3b7206 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file, l1_table_offset, l1_table,
858 8b3b7206 Kevin Wolf
                        l1_size2) < 0)
859 f7d0fe02 Kevin Wolf
            goto fail;
860 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
861 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
862 f7d0fe02 Kevin Wolf
    }
863 f7d0fe02 Kevin Wolf
    if (l1_allocated)
864 7267c094 Anthony Liguori
        g_free(l1_table);
865 93913dfd Kevin Wolf
    return ret;
866 f7d0fe02 Kevin Wolf
}
867 f7d0fe02 Kevin Wolf
868 f7d0fe02 Kevin Wolf
869 f7d0fe02 Kevin Wolf
870 f7d0fe02 Kevin Wolf
871 f7d0fe02 Kevin Wolf
/*********************************************************/
872 f7d0fe02 Kevin Wolf
/* refcount checking functions */
873 f7d0fe02 Kevin Wolf
874 f7d0fe02 Kevin Wolf
875 f7d0fe02 Kevin Wolf
876 f7d0fe02 Kevin Wolf
/*
877 f7d0fe02 Kevin Wolf
 * Increases the refcount for a range of clusters in a given refcount table.
878 f7d0fe02 Kevin Wolf
 * This is used to construct a temporary refcount table out of L1 and L2 tables
879 f7d0fe02 Kevin Wolf
 * which can be compared the the refcount table saved in the image.
880 f7d0fe02 Kevin Wolf
 *
881 9ac228e0 Kevin Wolf
 * Modifies the number of errors in res.
882 f7d0fe02 Kevin Wolf
 */
883 9ac228e0 Kevin Wolf
static void inc_refcounts(BlockDriverState *bs,
884 9ac228e0 Kevin Wolf
                          BdrvCheckResult *res,
885 f7d0fe02 Kevin Wolf
                          uint16_t *refcount_table,
886 f7d0fe02 Kevin Wolf
                          int refcount_table_size,
887 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
888 f7d0fe02 Kevin Wolf
{
889 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
890 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
891 f7d0fe02 Kevin Wolf
    int k;
892 f7d0fe02 Kevin Wolf
893 f7d0fe02 Kevin Wolf
    if (size <= 0)
894 9ac228e0 Kevin Wolf
        return;
895 f7d0fe02 Kevin Wolf
896 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
897 f7d0fe02 Kevin Wolf
    last = (offset + size - 1) & ~(s->cluster_size - 1);
898 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
899 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
900 f7d0fe02 Kevin Wolf
        k = cluster_offset >> s->cluster_bits;
901 9ac228e0 Kevin Wolf
        if (k < 0) {
902 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
903 f7d0fe02 Kevin Wolf
                cluster_offset);
904 9ac228e0 Kevin Wolf
            res->corruptions++;
905 9ac228e0 Kevin Wolf
        } else if (k >= refcount_table_size) {
906 9ac228e0 Kevin Wolf
            fprintf(stderr, "Warning: cluster offset=0x%" PRIx64 " is after "
907 9ac228e0 Kevin Wolf
                "the end of the image file, can't properly check refcounts.\n",
908 9ac228e0 Kevin Wolf
                cluster_offset);
909 9ac228e0 Kevin Wolf
            res->check_errors++;
910 f7d0fe02 Kevin Wolf
        } else {
911 f7d0fe02 Kevin Wolf
            if (++refcount_table[k] == 0) {
912 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
913 f7d0fe02 Kevin Wolf
                    "\n", cluster_offset);
914 9ac228e0 Kevin Wolf
                res->corruptions++;
915 f7d0fe02 Kevin Wolf
            }
916 f7d0fe02 Kevin Wolf
        }
917 f7d0fe02 Kevin Wolf
    }
918 f7d0fe02 Kevin Wolf
}
919 f7d0fe02 Kevin Wolf
920 f7d0fe02 Kevin Wolf
/*
921 f7d0fe02 Kevin Wolf
 * Increases the refcount in the given refcount table for the all clusters
922 f7d0fe02 Kevin Wolf
 * referenced in the L2 table. While doing so, performs some checks on L2
923 f7d0fe02 Kevin Wolf
 * entries.
924 f7d0fe02 Kevin Wolf
 *
925 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
926 f7d0fe02 Kevin Wolf
 * error occurred.
927 f7d0fe02 Kevin Wolf
 */
928 9ac228e0 Kevin Wolf
static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
929 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
930 f7d0fe02 Kevin Wolf
    int check_copied)
931 f7d0fe02 Kevin Wolf
{
932 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
933 afdf0abe Kevin Wolf
    uint64_t *l2_table, l2_entry;
934 f7d0fe02 Kevin Wolf
    int i, l2_size, nb_csectors, refcount;
935 f7d0fe02 Kevin Wolf
936 f7d0fe02 Kevin Wolf
    /* Read L2 table from disk */
937 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
938 7267c094 Anthony Liguori
    l2_table = g_malloc(l2_size);
939 f7d0fe02 Kevin Wolf
940 66f82cee Kevin Wolf
    if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
941 f7d0fe02 Kevin Wolf
        goto fail;
942 f7d0fe02 Kevin Wolf
943 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
944 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->l2_size; i++) {
945 afdf0abe Kevin Wolf
        l2_entry = be64_to_cpu(l2_table[i]);
946 afdf0abe Kevin Wolf
947 afdf0abe Kevin Wolf
        switch (qcow2_get_cluster_type(l2_entry)) {
948 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_COMPRESSED:
949 afdf0abe Kevin Wolf
            /* Compressed clusters don't have QCOW_OFLAG_COPIED */
950 afdf0abe Kevin Wolf
            if (l2_entry & QCOW_OFLAG_COPIED) {
951 afdf0abe Kevin Wolf
                fprintf(stderr, "ERROR: cluster %" PRId64 ": "
952 afdf0abe Kevin Wolf
                    "copied flag must never be set for compressed "
953 afdf0abe Kevin Wolf
                    "clusters\n", l2_entry >> s->cluster_bits);
954 afdf0abe Kevin Wolf
                l2_entry &= ~QCOW_OFLAG_COPIED;
955 afdf0abe Kevin Wolf
                res->corruptions++;
956 afdf0abe Kevin Wolf
            }
957 f7d0fe02 Kevin Wolf
958 afdf0abe Kevin Wolf
            /* Mark cluster as used */
959 afdf0abe Kevin Wolf
            nb_csectors = ((l2_entry >> s->csize_shift) &
960 afdf0abe Kevin Wolf
                           s->csize_mask) + 1;
961 afdf0abe Kevin Wolf
            l2_entry &= s->cluster_offset_mask;
962 afdf0abe Kevin Wolf
            inc_refcounts(bs, res, refcount_table, refcount_table_size,
963 afdf0abe Kevin Wolf
                l2_entry & ~511, nb_csectors * 512);
964 afdf0abe Kevin Wolf
            break;
965 f7d0fe02 Kevin Wolf
966 6377af48 Kevin Wolf
        case QCOW2_CLUSTER_ZERO:
967 6377af48 Kevin Wolf
            if ((l2_entry & L2E_OFFSET_MASK) == 0) {
968 6377af48 Kevin Wolf
                break;
969 6377af48 Kevin Wolf
            }
970 6377af48 Kevin Wolf
            /* fall through */
971 6377af48 Kevin Wolf
972 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_NORMAL:
973 afdf0abe Kevin Wolf
        {
974 afdf0abe Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
975 afdf0abe Kevin Wolf
            uint64_t offset = l2_entry & L2E_OFFSET_MASK;
976 f7d0fe02 Kevin Wolf
977 afdf0abe Kevin Wolf
            if (check_copied) {
978 afdf0abe Kevin Wolf
                refcount = get_refcount(bs, offset >> s->cluster_bits);
979 afdf0abe Kevin Wolf
                if (refcount < 0) {
980 afdf0abe Kevin Wolf
                    fprintf(stderr, "Can't get refcount for offset %"
981 afdf0abe Kevin Wolf
                        PRIx64 ": %s\n", l2_entry, strerror(-refcount));
982 afdf0abe Kevin Wolf
                    goto fail;
983 afdf0abe Kevin Wolf
                }
984 afdf0abe Kevin Wolf
                if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
985 afdf0abe Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
986 afdf0abe Kevin Wolf
                        PRIx64 " refcount=%d\n", l2_entry, refcount);
987 9ac228e0 Kevin Wolf
                    res->corruptions++;
988 f7d0fe02 Kevin Wolf
                }
989 f7d0fe02 Kevin Wolf
            }
990 afdf0abe Kevin Wolf
991 afdf0abe Kevin Wolf
            /* Mark cluster as used */
992 afdf0abe Kevin Wolf
            inc_refcounts(bs, res, refcount_table,refcount_table_size,
993 afdf0abe Kevin Wolf
                offset, s->cluster_size);
994 afdf0abe Kevin Wolf
995 afdf0abe Kevin Wolf
            /* Correct offsets are cluster aligned */
996 afdf0abe Kevin Wolf
            if (offset & (s->cluster_size - 1)) {
997 afdf0abe Kevin Wolf
                fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
998 afdf0abe Kevin Wolf
                    "properly aligned; L2 entry corrupted.\n", offset);
999 afdf0abe Kevin Wolf
                res->corruptions++;
1000 afdf0abe Kevin Wolf
            }
1001 afdf0abe Kevin Wolf
            break;
1002 afdf0abe Kevin Wolf
        }
1003 afdf0abe Kevin Wolf
1004 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_UNALLOCATED:
1005 afdf0abe Kevin Wolf
            break;
1006 afdf0abe Kevin Wolf
1007 afdf0abe Kevin Wolf
        default:
1008 afdf0abe Kevin Wolf
            abort();
1009 f7d0fe02 Kevin Wolf
        }
1010 f7d0fe02 Kevin Wolf
    }
1011 f7d0fe02 Kevin Wolf
1012 7267c094 Anthony Liguori
    g_free(l2_table);
1013 9ac228e0 Kevin Wolf
    return 0;
1014 f7d0fe02 Kevin Wolf
1015 f7d0fe02 Kevin Wolf
fail:
1016 9ac228e0 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1017 7267c094 Anthony Liguori
    g_free(l2_table);
1018 f7d0fe02 Kevin Wolf
    return -EIO;
1019 f7d0fe02 Kevin Wolf
}
1020 f7d0fe02 Kevin Wolf
1021 f7d0fe02 Kevin Wolf
/*
1022 f7d0fe02 Kevin Wolf
 * Increases the refcount for the L1 table, its L2 tables and all referenced
1023 f7d0fe02 Kevin Wolf
 * clusters in the given refcount table. While doing so, performs some checks
1024 f7d0fe02 Kevin Wolf
 * on L1 and L2 entries.
1025 f7d0fe02 Kevin Wolf
 *
1026 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
1027 f7d0fe02 Kevin Wolf
 * error occurred.
1028 f7d0fe02 Kevin Wolf
 */
1029 f7d0fe02 Kevin Wolf
static int check_refcounts_l1(BlockDriverState *bs,
1030 9ac228e0 Kevin Wolf
                              BdrvCheckResult *res,
1031 f7d0fe02 Kevin Wolf
                              uint16_t *refcount_table,
1032 f7d0fe02 Kevin Wolf
                              int refcount_table_size,
1033 f7d0fe02 Kevin Wolf
                              int64_t l1_table_offset, int l1_size,
1034 f7d0fe02 Kevin Wolf
                              int check_copied)
1035 f7d0fe02 Kevin Wolf
{
1036 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1037 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, l2_offset, l1_size2;
1038 f7d0fe02 Kevin Wolf
    int i, refcount, ret;
1039 f7d0fe02 Kevin Wolf
1040 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
1041 f7d0fe02 Kevin Wolf
1042 f7d0fe02 Kevin Wolf
    /* Mark L1 table as used */
1043 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, refcount_table_size,
1044 9ac228e0 Kevin Wolf
        l1_table_offset, l1_size2);
1045 f7d0fe02 Kevin Wolf
1046 f7d0fe02 Kevin Wolf
    /* Read L1 table entries from disk */
1047 702ef63f Kevin Wolf
    if (l1_size2 == 0) {
1048 702ef63f Kevin Wolf
        l1_table = NULL;
1049 702ef63f Kevin Wolf
    } else {
1050 7267c094 Anthony Liguori
        l1_table = g_malloc(l1_size2);
1051 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, l1_table_offset,
1052 702ef63f Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
1053 702ef63f Kevin Wolf
            goto fail;
1054 702ef63f Kevin Wolf
        for(i = 0;i < l1_size; i++)
1055 702ef63f Kevin Wolf
            be64_to_cpus(&l1_table[i]);
1056 702ef63f Kevin Wolf
    }
1057 f7d0fe02 Kevin Wolf
1058 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
1059 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
1060 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
1061 f7d0fe02 Kevin Wolf
        if (l2_offset) {
1062 f7d0fe02 Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1063 f7d0fe02 Kevin Wolf
            if (check_copied) {
1064 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
1065 f7d0fe02 Kevin Wolf
                    >> s->cluster_bits);
1066 018faafd Kevin Wolf
                if (refcount < 0) {
1067 018faafd Kevin Wolf
                    fprintf(stderr, "Can't get refcount for l2_offset %"
1068 018faafd Kevin Wolf
                        PRIx64 ": %s\n", l2_offset, strerror(-refcount));
1069 9ac228e0 Kevin Wolf
                    goto fail;
1070 018faafd Kevin Wolf
                }
1071 f7d0fe02 Kevin Wolf
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1072 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1073 f7d0fe02 Kevin Wolf
                        " refcount=%d\n", l2_offset, refcount);
1074 9ac228e0 Kevin Wolf
                    res->corruptions++;
1075 f7d0fe02 Kevin Wolf
                }
1076 f7d0fe02 Kevin Wolf
            }
1077 f7d0fe02 Kevin Wolf
1078 f7d0fe02 Kevin Wolf
            /* Mark L2 table as used */
1079 afdf0abe Kevin Wolf
            l2_offset &= L1E_OFFSET_MASK;
1080 9ac228e0 Kevin Wolf
            inc_refcounts(bs, res, refcount_table, refcount_table_size,
1081 9ac228e0 Kevin Wolf
                l2_offset, s->cluster_size);
1082 f7d0fe02 Kevin Wolf
1083 f7d0fe02 Kevin Wolf
            /* L2 tables are cluster aligned */
1084 f7d0fe02 Kevin Wolf
            if (l2_offset & (s->cluster_size - 1)) {
1085 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1086 f7d0fe02 Kevin Wolf
                    "cluster aligned; L1 entry corrupted\n", l2_offset);
1087 9ac228e0 Kevin Wolf
                res->corruptions++;
1088 f7d0fe02 Kevin Wolf
            }
1089 f7d0fe02 Kevin Wolf
1090 f7d0fe02 Kevin Wolf
            /* Process and check L2 entries */
1091 9ac228e0 Kevin Wolf
            ret = check_refcounts_l2(bs, res, refcount_table,
1092 9ac228e0 Kevin Wolf
                refcount_table_size, l2_offset, check_copied);
1093 f7d0fe02 Kevin Wolf
            if (ret < 0) {
1094 f7d0fe02 Kevin Wolf
                goto fail;
1095 f7d0fe02 Kevin Wolf
            }
1096 f7d0fe02 Kevin Wolf
        }
1097 f7d0fe02 Kevin Wolf
    }
1098 7267c094 Anthony Liguori
    g_free(l1_table);
1099 9ac228e0 Kevin Wolf
    return 0;
1100 f7d0fe02 Kevin Wolf
1101 f7d0fe02 Kevin Wolf
fail:
1102 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1103 9ac228e0 Kevin Wolf
    res->check_errors++;
1104 7267c094 Anthony Liguori
    g_free(l1_table);
1105 f7d0fe02 Kevin Wolf
    return -EIO;
1106 f7d0fe02 Kevin Wolf
}
1107 f7d0fe02 Kevin Wolf
1108 f7d0fe02 Kevin Wolf
/*
1109 f7d0fe02 Kevin Wolf
 * Checks an image for refcount consistency.
1110 f7d0fe02 Kevin Wolf
 *
1111 f7d0fe02 Kevin Wolf
 * Returns 0 if no errors are found, the number of errors in case the image is
1112 a1c7273b Stefan Weil
 * detected as corrupted, and -errno when an internal error occurred.
1113 f7d0fe02 Kevin Wolf
 */
1114 166acf54 Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1115 166acf54 Kevin Wolf
                          BdrvCheckMode fix)
1116 f7d0fe02 Kevin Wolf
{
1117 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1118 166acf54 Kevin Wolf
    int64_t size, i;
1119 166acf54 Kevin Wolf
    int nb_clusters, refcount1, refcount2;
1120 f7d0fe02 Kevin Wolf
    QCowSnapshot *sn;
1121 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table;
1122 9ac228e0 Kevin Wolf
    int ret;
1123 f7d0fe02 Kevin Wolf
1124 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1125 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
1126 7267c094 Anthony Liguori
    refcount_table = g_malloc0(nb_clusters * sizeof(uint16_t));
1127 f7d0fe02 Kevin Wolf
1128 f7d0fe02 Kevin Wolf
    /* header */
1129 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1130 9ac228e0 Kevin Wolf
        0, s->cluster_size);
1131 f7d0fe02 Kevin Wolf
1132 f7d0fe02 Kevin Wolf
    /* current L1 table */
1133 9ac228e0 Kevin Wolf
    ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1134 f7d0fe02 Kevin Wolf
                       s->l1_table_offset, s->l1_size, 1);
1135 f7d0fe02 Kevin Wolf
    if (ret < 0) {
1136 80fa3341 Kevin Wolf
        goto fail;
1137 f7d0fe02 Kevin Wolf
    }
1138 f7d0fe02 Kevin Wolf
1139 f7d0fe02 Kevin Wolf
    /* snapshots */
1140 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->nb_snapshots; i++) {
1141 f7d0fe02 Kevin Wolf
        sn = s->snapshots + i;
1142 9ac228e0 Kevin Wolf
        ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1143 9ac228e0 Kevin Wolf
            sn->l1_table_offset, sn->l1_size, 0);
1144 9ac228e0 Kevin Wolf
        if (ret < 0) {
1145 80fa3341 Kevin Wolf
            goto fail;
1146 9ac228e0 Kevin Wolf
        }
1147 f7d0fe02 Kevin Wolf
    }
1148 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1149 9ac228e0 Kevin Wolf
        s->snapshots_offset, s->snapshots_size);
1150 f7d0fe02 Kevin Wolf
1151 f7d0fe02 Kevin Wolf
    /* refcount data */
1152 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1153 9ac228e0 Kevin Wolf
        s->refcount_table_offset,
1154 9ac228e0 Kevin Wolf
        s->refcount_table_size * sizeof(uint64_t));
1155 9ac228e0 Kevin Wolf
1156 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++) {
1157 6882c8fa Kevin Wolf
        uint64_t offset, cluster;
1158 f7d0fe02 Kevin Wolf
        offset = s->refcount_table[i];
1159 6882c8fa Kevin Wolf
        cluster = offset >> s->cluster_bits;
1160 746c3cb5 Kevin Wolf
1161 746c3cb5 Kevin Wolf
        /* Refcount blocks are cluster aligned */
1162 746c3cb5 Kevin Wolf
        if (offset & (s->cluster_size - 1)) {
1163 166acf54 Kevin Wolf
            fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
1164 746c3cb5 Kevin Wolf
                "cluster aligned; refcount table entry corrupted\n", i);
1165 9ac228e0 Kevin Wolf
            res->corruptions++;
1166 6882c8fa Kevin Wolf
            continue;
1167 6882c8fa Kevin Wolf
        }
1168 6882c8fa Kevin Wolf
1169 6882c8fa Kevin Wolf
        if (cluster >= nb_clusters) {
1170 166acf54 Kevin Wolf
            fprintf(stderr, "ERROR refcount block %" PRId64
1171 166acf54 Kevin Wolf
                    " is outside image\n", i);
1172 9ac228e0 Kevin Wolf
            res->corruptions++;
1173 6882c8fa Kevin Wolf
            continue;
1174 746c3cb5 Kevin Wolf
        }
1175 746c3cb5 Kevin Wolf
1176 f7d0fe02 Kevin Wolf
        if (offset != 0) {
1177 9ac228e0 Kevin Wolf
            inc_refcounts(bs, res, refcount_table, nb_clusters,
1178 9ac228e0 Kevin Wolf
                offset, s->cluster_size);
1179 6882c8fa Kevin Wolf
            if (refcount_table[cluster] != 1) {
1180 166acf54 Kevin Wolf
                fprintf(stderr, "ERROR refcount block %" PRId64
1181 166acf54 Kevin Wolf
                    " refcount=%d\n",
1182 6882c8fa Kevin Wolf
                    i, refcount_table[cluster]);
1183 9ac228e0 Kevin Wolf
                res->corruptions++;
1184 746c3cb5 Kevin Wolf
            }
1185 f7d0fe02 Kevin Wolf
        }
1186 f7d0fe02 Kevin Wolf
    }
1187 f7d0fe02 Kevin Wolf
1188 f7d0fe02 Kevin Wolf
    /* compare ref counts */
1189 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
1190 f7d0fe02 Kevin Wolf
        refcount1 = get_refcount(bs, i);
1191 018faafd Kevin Wolf
        if (refcount1 < 0) {
1192 166acf54 Kevin Wolf
            fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
1193 018faafd Kevin Wolf
                i, strerror(-refcount1));
1194 9ac228e0 Kevin Wolf
            res->check_errors++;
1195 f74550fd Kevin Wolf
            continue;
1196 018faafd Kevin Wolf
        }
1197 018faafd Kevin Wolf
1198 f7d0fe02 Kevin Wolf
        refcount2 = refcount_table[i];
1199 f7d0fe02 Kevin Wolf
        if (refcount1 != refcount2) {
1200 166acf54 Kevin Wolf
1201 166acf54 Kevin Wolf
            /* Check if we're allowed to fix the mismatch */
1202 166acf54 Kevin Wolf
            int *num_fixed = NULL;
1203 166acf54 Kevin Wolf
            if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
1204 166acf54 Kevin Wolf
                num_fixed = &res->leaks_fixed;
1205 166acf54 Kevin Wolf
            } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
1206 166acf54 Kevin Wolf
                num_fixed = &res->corruptions_fixed;
1207 166acf54 Kevin Wolf
            }
1208 166acf54 Kevin Wolf
1209 166acf54 Kevin Wolf
            fprintf(stderr, "%s cluster %" PRId64 " refcount=%d reference=%d\n",
1210 166acf54 Kevin Wolf
                   num_fixed != NULL     ? "Repairing" :
1211 166acf54 Kevin Wolf
                   refcount1 < refcount2 ? "ERROR" :
1212 166acf54 Kevin Wolf
                                           "Leaked",
1213 f7d0fe02 Kevin Wolf
                   i, refcount1, refcount2);
1214 166acf54 Kevin Wolf
1215 166acf54 Kevin Wolf
            if (num_fixed) {
1216 166acf54 Kevin Wolf
                ret = update_refcount(bs, i << s->cluster_bits, 1,
1217 166acf54 Kevin Wolf
                                      refcount2 - refcount1);
1218 166acf54 Kevin Wolf
                if (ret >= 0) {
1219 166acf54 Kevin Wolf
                    (*num_fixed)++;
1220 166acf54 Kevin Wolf
                    continue;
1221 166acf54 Kevin Wolf
                }
1222 166acf54 Kevin Wolf
            }
1223 166acf54 Kevin Wolf
1224 166acf54 Kevin Wolf
            /* And if we couldn't, print an error */
1225 9ac228e0 Kevin Wolf
            if (refcount1 < refcount2) {
1226 9ac228e0 Kevin Wolf
                res->corruptions++;
1227 9ac228e0 Kevin Wolf
            } else {
1228 9ac228e0 Kevin Wolf
                res->leaks++;
1229 9ac228e0 Kevin Wolf
            }
1230 f7d0fe02 Kevin Wolf
        }
1231 f7d0fe02 Kevin Wolf
    }
1232 f7d0fe02 Kevin Wolf
1233 80fa3341 Kevin Wolf
    ret = 0;
1234 80fa3341 Kevin Wolf
1235 80fa3341 Kevin Wolf
fail:
1236 7267c094 Anthony Liguori
    g_free(refcount_table);
1237 f7d0fe02 Kevin Wolf
1238 80fa3341 Kevin Wolf
    return ret;
1239 f7d0fe02 Kevin Wolf
}