Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 50717e94

History | View | Annotate | Download (39.8 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 737e150e Paolo Bonzini
#include "block/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 a3548077 Kevin Wolf
        uint64_t table_clusters =
305 a3548077 Kevin Wolf
            size_to_clusters(s, table_size * sizeof(uint64_t));
306 92dcb59f Kevin Wolf
        blocks_clusters = 1 +
307 92dcb59f Kevin Wolf
            ((table_clusters + refcount_block_clusters - 1)
308 92dcb59f Kevin Wolf
            / refcount_block_clusters);
309 92dcb59f Kevin Wolf
        uint64_t meta_clusters = table_clusters + blocks_clusters;
310 92dcb59f Kevin Wolf
311 92dcb59f Kevin Wolf
        last_table_size = table_size;
312 92dcb59f Kevin Wolf
        table_size = next_refcount_table_size(s, blocks_used +
313 92dcb59f Kevin Wolf
            ((meta_clusters + refcount_block_clusters - 1)
314 92dcb59f Kevin Wolf
            / refcount_block_clusters));
315 92dcb59f Kevin Wolf
316 92dcb59f Kevin Wolf
    } while (last_table_size != table_size);
317 92dcb59f Kevin Wolf
318 92dcb59f Kevin Wolf
#ifdef DEBUG_ALLOC2
319 92dcb59f Kevin Wolf
    fprintf(stderr, "qcow2: Grow refcount table %" PRId32 " => %" PRId64 "\n",
320 92dcb59f Kevin Wolf
        s->refcount_table_size, table_size);
321 92dcb59f Kevin Wolf
#endif
322 92dcb59f Kevin Wolf
323 92dcb59f Kevin Wolf
    /* Create the new refcount table and blocks */
324 92dcb59f Kevin Wolf
    uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
325 92dcb59f Kevin Wolf
        s->cluster_size;
326 92dcb59f Kevin Wolf
    uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
327 7267c094 Anthony Liguori
    uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size);
328 7267c094 Anthony Liguori
    uint64_t *new_table = g_malloc0(table_size * sizeof(uint64_t));
329 92dcb59f Kevin Wolf
330 92dcb59f Kevin Wolf
    assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
331 92dcb59f Kevin Wolf
332 92dcb59f Kevin Wolf
    /* Fill the new refcount table */
333 f7d0fe02 Kevin Wolf
    memcpy(new_table, s->refcount_table,
334 92dcb59f Kevin Wolf
        s->refcount_table_size * sizeof(uint64_t));
335 92dcb59f Kevin Wolf
    new_table[refcount_table_index] = new_block;
336 92dcb59f Kevin Wolf
337 92dcb59f Kevin Wolf
    int i;
338 92dcb59f Kevin Wolf
    for (i = 0; i < blocks_clusters; i++) {
339 92dcb59f Kevin Wolf
        new_table[blocks_used + i] = meta_offset + (i * s->cluster_size);
340 92dcb59f Kevin Wolf
    }
341 92dcb59f Kevin Wolf
342 92dcb59f Kevin Wolf
    /* Fill the refcount blocks */
343 92dcb59f Kevin Wolf
    uint64_t table_clusters = size_to_clusters(s, table_size * sizeof(uint64_t));
344 92dcb59f Kevin Wolf
    int block = 0;
345 92dcb59f Kevin Wolf
    for (i = 0; i < table_clusters + blocks_clusters; i++) {
346 92dcb59f Kevin Wolf
        new_blocks[block++] = cpu_to_be16(1);
347 92dcb59f Kevin Wolf
    }
348 92dcb59f Kevin Wolf
349 92dcb59f Kevin Wolf
    /* Write refcount blocks to disk */
350 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
351 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, meta_offset, new_blocks,
352 92dcb59f Kevin Wolf
        blocks_clusters * s->cluster_size);
353 7267c094 Anthony Liguori
    g_free(new_blocks);
354 92dcb59f Kevin Wolf
    if (ret < 0) {
355 92dcb59f Kevin Wolf
        goto fail_table;
356 92dcb59f Kevin Wolf
    }
357 92dcb59f Kevin Wolf
358 92dcb59f Kevin Wolf
    /* Write refcount table to disk */
359 92dcb59f Kevin Wolf
    for(i = 0; i < table_size; i++) {
360 92dcb59f Kevin Wolf
        cpu_to_be64s(&new_table[i]);
361 92dcb59f Kevin Wolf
    }
362 92dcb59f Kevin Wolf
363 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
364 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, table_offset, new_table,
365 92dcb59f Kevin Wolf
        table_size * sizeof(uint64_t));
366 92dcb59f Kevin Wolf
    if (ret < 0) {
367 92dcb59f Kevin Wolf
        goto fail_table;
368 92dcb59f Kevin Wolf
    }
369 92dcb59f Kevin Wolf
370 92dcb59f Kevin Wolf
    for(i = 0; i < table_size; i++) {
371 87267753 Zhi Yong Wu
        be64_to_cpus(&new_table[i]);
372 92dcb59f Kevin Wolf
    }
373 f7d0fe02 Kevin Wolf
374 92dcb59f Kevin Wolf
    /* Hook up the new refcount table in the qcow2 header */
375 92dcb59f Kevin Wolf
    uint8_t data[12];
376 f7d0fe02 Kevin Wolf
    cpu_to_be64w((uint64_t*)data, table_offset);
377 92dcb59f Kevin Wolf
    cpu_to_be32w((uint32_t*)(data + 8), table_clusters);
378 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
379 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, refcount_table_offset),
380 92dcb59f Kevin Wolf
        data, sizeof(data));
381 92dcb59f Kevin Wolf
    if (ret < 0) {
382 92dcb59f Kevin Wolf
        goto fail_table;
383 f2b7c8b3 Kevin Wolf
    }
384 f2b7c8b3 Kevin Wolf
385 92dcb59f Kevin Wolf
    /* And switch it in memory */
386 92dcb59f Kevin Wolf
    uint64_t old_table_offset = s->refcount_table_offset;
387 92dcb59f Kevin Wolf
    uint64_t old_table_size = s->refcount_table_size;
388 92dcb59f Kevin Wolf
389 7267c094 Anthony Liguori
    g_free(s->refcount_table);
390 f7d0fe02 Kevin Wolf
    s->refcount_table = new_table;
391 92dcb59f Kevin Wolf
    s->refcount_table_size = table_size;
392 f7d0fe02 Kevin Wolf
    s->refcount_table_offset = table_offset;
393 f7d0fe02 Kevin Wolf
394 92dcb59f Kevin Wolf
    /* Free old table. Remember, we must not change free_cluster_index */
395 92dcb59f Kevin Wolf
    uint64_t old_free_cluster_index = s->free_cluster_index;
396 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
397 92dcb59f Kevin Wolf
    s->free_cluster_index = old_free_cluster_index;
398 f7d0fe02 Kevin Wolf
399 29c1a730 Kevin Wolf
    ret = load_refcount_block(bs, new_block, (void**) refcount_block);
400 92dcb59f Kevin Wolf
    if (ret < 0) {
401 29c1a730 Kevin Wolf
        return ret;
402 f7d0fe02 Kevin Wolf
    }
403 f7d0fe02 Kevin Wolf
404 2795ecf6 Kevin Wolf
    return 0;
405 f7d0fe02 Kevin Wolf
406 92dcb59f Kevin Wolf
fail_table:
407 7267c094 Anthony Liguori
    g_free(new_table);
408 92dcb59f Kevin Wolf
fail_block:
409 29c1a730 Kevin Wolf
    if (*refcount_block != NULL) {
410 29c1a730 Kevin Wolf
        qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block);
411 3b88e52b Kevin Wolf
    }
412 29c1a730 Kevin Wolf
    return ret;
413 9923e05e Kevin Wolf
}
414 9923e05e Kevin Wolf
415 f7d0fe02 Kevin Wolf
/* XXX: cache several refcount block clusters ? */
416 db3a964f Kevin Wolf
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
417 db3a964f Kevin Wolf
    int64_t offset, int64_t length, int addend)
418 f7d0fe02 Kevin Wolf
{
419 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
420 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
421 29c1a730 Kevin Wolf
    uint16_t *refcount_block = NULL;
422 29c1a730 Kevin Wolf
    int64_t old_table_index = -1;
423 09508d13 Kevin Wolf
    int ret;
424 f7d0fe02 Kevin Wolf
425 f7d0fe02 Kevin Wolf
#ifdef DEBUG_ALLOC2
426 35ee5e39 Frediano Ziglio
    fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
427 f7d0fe02 Kevin Wolf
           offset, length, addend);
428 f7d0fe02 Kevin Wolf
#endif
429 7322afe7 Kevin Wolf
    if (length < 0) {
430 f7d0fe02 Kevin Wolf
        return -EINVAL;
431 7322afe7 Kevin Wolf
    } else if (length == 0) {
432 7322afe7 Kevin Wolf
        return 0;
433 7322afe7 Kevin Wolf
    }
434 7322afe7 Kevin Wolf
435 29c1a730 Kevin Wolf
    if (addend < 0) {
436 29c1a730 Kevin Wolf
        qcow2_cache_set_dependency(bs, s->refcount_block_cache,
437 29c1a730 Kevin Wolf
            s->l2_table_cache);
438 29c1a730 Kevin Wolf
    }
439 29c1a730 Kevin Wolf
440 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
441 f7d0fe02 Kevin Wolf
    last = (offset + length - 1) & ~(s->cluster_size - 1);
442 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
443 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size)
444 f7d0fe02 Kevin Wolf
    {
445 f7d0fe02 Kevin Wolf
        int block_index, refcount;
446 f7d0fe02 Kevin Wolf
        int64_t cluster_index = cluster_offset >> s->cluster_bits;
447 29c1a730 Kevin Wolf
        int64_t table_index =
448 29c1a730 Kevin Wolf
            cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
449 f7d0fe02 Kevin Wolf
450 29c1a730 Kevin Wolf
        /* Load the refcount block and allocate it if needed */
451 29c1a730 Kevin Wolf
        if (table_index != old_table_index) {
452 29c1a730 Kevin Wolf
            if (refcount_block) {
453 29c1a730 Kevin Wolf
                ret = qcow2_cache_put(bs, s->refcount_block_cache,
454 29c1a730 Kevin Wolf
                    (void**) &refcount_block);
455 29c1a730 Kevin Wolf
                if (ret < 0) {
456 29c1a730 Kevin Wolf
                    goto fail;
457 29c1a730 Kevin Wolf
                }
458 29c1a730 Kevin Wolf
            }
459 9923e05e Kevin Wolf
460 29c1a730 Kevin Wolf
            ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
461 ed0df867 Kevin Wolf
            if (ret < 0) {
462 29c1a730 Kevin Wolf
                goto fail;
463 f7d0fe02 Kevin Wolf
            }
464 f7d0fe02 Kevin Wolf
        }
465 29c1a730 Kevin Wolf
        old_table_index = table_index;
466 f7d0fe02 Kevin Wolf
467 29c1a730 Kevin Wolf
        qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
468 f7d0fe02 Kevin Wolf
469 f7d0fe02 Kevin Wolf
        /* we can update the count and save it */
470 f7d0fe02 Kevin Wolf
        block_index = cluster_index &
471 f7d0fe02 Kevin Wolf
            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
472 f7d0fe02 Kevin Wolf
473 29c1a730 Kevin Wolf
        refcount = be16_to_cpu(refcount_block[block_index]);
474 f7d0fe02 Kevin Wolf
        refcount += addend;
475 09508d13 Kevin Wolf
        if (refcount < 0 || refcount > 0xffff) {
476 09508d13 Kevin Wolf
            ret = -EINVAL;
477 09508d13 Kevin Wolf
            goto fail;
478 09508d13 Kevin Wolf
        }
479 f7d0fe02 Kevin Wolf
        if (refcount == 0 && cluster_index < s->free_cluster_index) {
480 f7d0fe02 Kevin Wolf
            s->free_cluster_index = cluster_index;
481 f7d0fe02 Kevin Wolf
        }
482 29c1a730 Kevin Wolf
        refcount_block[block_index] = cpu_to_be16(refcount);
483 f7d0fe02 Kevin Wolf
    }
484 f7d0fe02 Kevin Wolf
485 09508d13 Kevin Wolf
    ret = 0;
486 09508d13 Kevin Wolf
fail:
487 f7d0fe02 Kevin Wolf
    /* Write last changed block to disk */
488 29c1a730 Kevin Wolf
    if (refcount_block) {
489 ed0df867 Kevin Wolf
        int wret;
490 29c1a730 Kevin Wolf
        wret = qcow2_cache_put(bs, s->refcount_block_cache,
491 29c1a730 Kevin Wolf
            (void**) &refcount_block);
492 ed0df867 Kevin Wolf
        if (wret < 0) {
493 ed0df867 Kevin Wolf
            return ret < 0 ? ret : wret;
494 f7d0fe02 Kevin Wolf
        }
495 f7d0fe02 Kevin Wolf
    }
496 f7d0fe02 Kevin Wolf
497 09508d13 Kevin Wolf
    /*
498 09508d13 Kevin Wolf
     * Try do undo any updates if an error is returned (This may succeed in
499 09508d13 Kevin Wolf
     * some cases like ENOSPC for allocating a new refcount block)
500 09508d13 Kevin Wolf
     */
501 09508d13 Kevin Wolf
    if (ret < 0) {
502 09508d13 Kevin Wolf
        int dummy;
503 09508d13 Kevin Wolf
        dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
504 83e3f76c Blue Swirl
        (void)dummy;
505 09508d13 Kevin Wolf
    }
506 09508d13 Kevin Wolf
507 09508d13 Kevin Wolf
    return ret;
508 f7d0fe02 Kevin Wolf
}
509 f7d0fe02 Kevin Wolf
510 018faafd Kevin Wolf
/*
511 018faafd Kevin Wolf
 * Increases or decreases the refcount of a given cluster by one.
512 018faafd Kevin Wolf
 * addend must be 1 or -1.
513 018faafd Kevin Wolf
 *
514 018faafd Kevin Wolf
 * If the return value is non-negative, it is the new refcount of the cluster.
515 018faafd Kevin Wolf
 * If it is negative, it is -errno and indicates an error.
516 018faafd Kevin Wolf
 */
517 f7d0fe02 Kevin Wolf
static int update_cluster_refcount(BlockDriverState *bs,
518 f7d0fe02 Kevin Wolf
                                   int64_t cluster_index,
519 f7d0fe02 Kevin Wolf
                                   int addend)
520 f7d0fe02 Kevin Wolf
{
521 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
522 f7d0fe02 Kevin Wolf
    int ret;
523 f7d0fe02 Kevin Wolf
524 f7d0fe02 Kevin Wolf
    ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
525 f7d0fe02 Kevin Wolf
    if (ret < 0) {
526 f7d0fe02 Kevin Wolf
        return ret;
527 f7d0fe02 Kevin Wolf
    }
528 f7d0fe02 Kevin Wolf
529 1c4c2814 Kevin Wolf
    bdrv_flush(bs->file);
530 1c4c2814 Kevin Wolf
531 f7d0fe02 Kevin Wolf
    return get_refcount(bs, cluster_index);
532 f7d0fe02 Kevin Wolf
}
533 f7d0fe02 Kevin Wolf
534 f7d0fe02 Kevin Wolf
535 f7d0fe02 Kevin Wolf
536 f7d0fe02 Kevin Wolf
/*********************************************************/
537 f7d0fe02 Kevin Wolf
/* cluster allocation functions */
538 f7d0fe02 Kevin Wolf
539 f7d0fe02 Kevin Wolf
540 f7d0fe02 Kevin Wolf
541 f7d0fe02 Kevin Wolf
/* return < 0 if error */
542 f7d0fe02 Kevin Wolf
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
543 f7d0fe02 Kevin Wolf
{
544 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
545 2eaa8f63 Kevin Wolf
    int i, nb_clusters, refcount;
546 f7d0fe02 Kevin Wolf
547 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
548 f7d0fe02 Kevin Wolf
retry:
549 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
550 508e0893 Stefan Hajnoczi
        int64_t next_cluster_index = s->free_cluster_index++;
551 2eaa8f63 Kevin Wolf
        refcount = get_refcount(bs, next_cluster_index);
552 2eaa8f63 Kevin Wolf
553 2eaa8f63 Kevin Wolf
        if (refcount < 0) {
554 2eaa8f63 Kevin Wolf
            return refcount;
555 2eaa8f63 Kevin Wolf
        } else if (refcount != 0) {
556 f7d0fe02 Kevin Wolf
            goto retry;
557 2eaa8f63 Kevin Wolf
        }
558 f7d0fe02 Kevin Wolf
    }
559 f7d0fe02 Kevin Wolf
#ifdef DEBUG_ALLOC2
560 35ee5e39 Frediano Ziglio
    fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
561 f7d0fe02 Kevin Wolf
            size,
562 f7d0fe02 Kevin Wolf
            (s->free_cluster_index - nb_clusters) << s->cluster_bits);
563 f7d0fe02 Kevin Wolf
#endif
564 f7d0fe02 Kevin Wolf
    return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
565 f7d0fe02 Kevin Wolf
}
566 f7d0fe02 Kevin Wolf
567 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
568 f7d0fe02 Kevin Wolf
{
569 f7d0fe02 Kevin Wolf
    int64_t offset;
570 db3a964f Kevin Wolf
    int ret;
571 f7d0fe02 Kevin Wolf
572 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
573 f7d0fe02 Kevin Wolf
    offset = alloc_clusters_noref(bs, size);
574 2eaa8f63 Kevin Wolf
    if (offset < 0) {
575 2eaa8f63 Kevin Wolf
        return offset;
576 2eaa8f63 Kevin Wolf
    }
577 2eaa8f63 Kevin Wolf
578 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, 1);
579 db3a964f Kevin Wolf
    if (ret < 0) {
580 db3a964f Kevin Wolf
        return ret;
581 db3a964f Kevin Wolf
    }
582 1c4c2814 Kevin Wolf
583 f7d0fe02 Kevin Wolf
    return offset;
584 f7d0fe02 Kevin Wolf
}
585 f7d0fe02 Kevin Wolf
586 256900b1 Kevin Wolf
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
587 256900b1 Kevin Wolf
    int nb_clusters)
588 256900b1 Kevin Wolf
{
589 256900b1 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
590 256900b1 Kevin Wolf
    uint64_t cluster_index;
591 f24423bd Kevin Wolf
    uint64_t old_free_cluster_index;
592 256900b1 Kevin Wolf
    int i, refcount, ret;
593 256900b1 Kevin Wolf
594 256900b1 Kevin Wolf
    /* Check how many clusters there are free */
595 256900b1 Kevin Wolf
    cluster_index = offset >> s->cluster_bits;
596 256900b1 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
597 256900b1 Kevin Wolf
        refcount = get_refcount(bs, cluster_index++);
598 256900b1 Kevin Wolf
599 256900b1 Kevin Wolf
        if (refcount < 0) {
600 256900b1 Kevin Wolf
            return refcount;
601 256900b1 Kevin Wolf
        } else if (refcount != 0) {
602 256900b1 Kevin Wolf
            break;
603 256900b1 Kevin Wolf
        }
604 256900b1 Kevin Wolf
    }
605 256900b1 Kevin Wolf
606 256900b1 Kevin Wolf
    /* And then allocate them */
607 f24423bd Kevin Wolf
    old_free_cluster_index = s->free_cluster_index;
608 f24423bd Kevin Wolf
    s->free_cluster_index = cluster_index + i;
609 f24423bd Kevin Wolf
610 256900b1 Kevin Wolf
    ret = update_refcount(bs, offset, i << s->cluster_bits, 1);
611 256900b1 Kevin Wolf
    if (ret < 0) {
612 256900b1 Kevin Wolf
        return ret;
613 256900b1 Kevin Wolf
    }
614 256900b1 Kevin Wolf
615 f24423bd Kevin Wolf
    s->free_cluster_index = old_free_cluster_index;
616 f24423bd Kevin Wolf
617 256900b1 Kevin Wolf
    return i;
618 256900b1 Kevin Wolf
}
619 256900b1 Kevin Wolf
620 f7d0fe02 Kevin Wolf
/* only used to allocate compressed sectors. We try to allocate
621 f7d0fe02 Kevin Wolf
   contiguous sectors. size must be <= cluster_size */
622 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
623 f7d0fe02 Kevin Wolf
{
624 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
625 f7d0fe02 Kevin Wolf
    int64_t offset, cluster_offset;
626 f7d0fe02 Kevin Wolf
    int free_in_cluster;
627 f7d0fe02 Kevin Wolf
628 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
629 f7d0fe02 Kevin Wolf
    assert(size > 0 && size <= s->cluster_size);
630 f7d0fe02 Kevin Wolf
    if (s->free_byte_offset == 0) {
631 206e6d85 Stefan Hajnoczi
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
632 206e6d85 Stefan Hajnoczi
        if (offset < 0) {
633 206e6d85 Stefan Hajnoczi
            return offset;
634 5d757b56 Kevin Wolf
        }
635 206e6d85 Stefan Hajnoczi
        s->free_byte_offset = offset;
636 f7d0fe02 Kevin Wolf
    }
637 f7d0fe02 Kevin Wolf
 redo:
638 f7d0fe02 Kevin Wolf
    free_in_cluster = s->cluster_size -
639 f7d0fe02 Kevin Wolf
        (s->free_byte_offset & (s->cluster_size - 1));
640 f7d0fe02 Kevin Wolf
    if (size <= free_in_cluster) {
641 f7d0fe02 Kevin Wolf
        /* enough space in current cluster */
642 f7d0fe02 Kevin Wolf
        offset = s->free_byte_offset;
643 f7d0fe02 Kevin Wolf
        s->free_byte_offset += size;
644 f7d0fe02 Kevin Wolf
        free_in_cluster -= size;
645 f7d0fe02 Kevin Wolf
        if (free_in_cluster == 0)
646 f7d0fe02 Kevin Wolf
            s->free_byte_offset = 0;
647 f7d0fe02 Kevin Wolf
        if ((offset & (s->cluster_size - 1)) != 0)
648 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
649 f7d0fe02 Kevin Wolf
    } else {
650 ed6ccf0f Kevin Wolf
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
651 5d757b56 Kevin Wolf
        if (offset < 0) {
652 5d757b56 Kevin Wolf
            return offset;
653 5d757b56 Kevin Wolf
        }
654 f7d0fe02 Kevin Wolf
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
655 f7d0fe02 Kevin Wolf
        if ((cluster_offset + s->cluster_size) == offset) {
656 f7d0fe02 Kevin Wolf
            /* we are lucky: contiguous data */
657 f7d0fe02 Kevin Wolf
            offset = s->free_byte_offset;
658 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
659 f7d0fe02 Kevin Wolf
            s->free_byte_offset += size;
660 f7d0fe02 Kevin Wolf
        } else {
661 f7d0fe02 Kevin Wolf
            s->free_byte_offset = offset;
662 f7d0fe02 Kevin Wolf
            goto redo;
663 f7d0fe02 Kevin Wolf
        }
664 f7d0fe02 Kevin Wolf
    }
665 29216ed1 Kevin Wolf
666 29216ed1 Kevin Wolf
    bdrv_flush(bs->file);
667 f7d0fe02 Kevin Wolf
    return offset;
668 f7d0fe02 Kevin Wolf
}
669 f7d0fe02 Kevin Wolf
670 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
671 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
672 f7d0fe02 Kevin Wolf
{
673 db3a964f Kevin Wolf
    int ret;
674 db3a964f Kevin Wolf
675 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
676 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, -1);
677 db3a964f Kevin Wolf
    if (ret < 0) {
678 db3a964f Kevin Wolf
        fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
679 003fad6e Kevin Wolf
        /* TODO Remember the clusters to free them later and avoid leaking */
680 db3a964f Kevin Wolf
    }
681 f7d0fe02 Kevin Wolf
}
682 f7d0fe02 Kevin Wolf
683 45aba42f Kevin Wolf
/*
684 c7a4c37a Kevin Wolf
 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
685 c7a4c37a Kevin Wolf
 * normal cluster, compressed cluster, etc.)
686 45aba42f Kevin Wolf
 */
687 ed6ccf0f Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs,
688 c7a4c37a Kevin Wolf
    uint64_t l2_entry, int nb_clusters)
689 45aba42f Kevin Wolf
{
690 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
691 45aba42f Kevin Wolf
692 c7a4c37a Kevin Wolf
    switch (qcow2_get_cluster_type(l2_entry)) {
693 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_COMPRESSED:
694 c7a4c37a Kevin Wolf
        {
695 c7a4c37a Kevin Wolf
            int nb_csectors;
696 c7a4c37a Kevin Wolf
            nb_csectors = ((l2_entry >> s->csize_shift) &
697 c7a4c37a Kevin Wolf
                           s->csize_mask) + 1;
698 c7a4c37a Kevin Wolf
            qcow2_free_clusters(bs,
699 c7a4c37a Kevin Wolf
                (l2_entry & s->cluster_offset_mask) & ~511,
700 c7a4c37a Kevin Wolf
                nb_csectors * 512);
701 c7a4c37a Kevin Wolf
        }
702 c7a4c37a Kevin Wolf
        break;
703 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_NORMAL:
704 c7a4c37a Kevin Wolf
        qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
705 c7a4c37a Kevin Wolf
                            nb_clusters << s->cluster_bits);
706 c7a4c37a Kevin Wolf
        break;
707 c7a4c37a Kevin Wolf
    case QCOW2_CLUSTER_UNALLOCATED:
708 6377af48 Kevin Wolf
    case QCOW2_CLUSTER_ZERO:
709 c7a4c37a Kevin Wolf
        break;
710 c7a4c37a Kevin Wolf
    default:
711 c7a4c37a Kevin Wolf
        abort();
712 45aba42f Kevin Wolf
    }
713 45aba42f Kevin Wolf
}
714 45aba42f Kevin Wolf
715 f7d0fe02 Kevin Wolf
716 f7d0fe02 Kevin Wolf
717 f7d0fe02 Kevin Wolf
/*********************************************************/
718 f7d0fe02 Kevin Wolf
/* snapshots and image creation */
719 f7d0fe02 Kevin Wolf
720 f7d0fe02 Kevin Wolf
721 f7d0fe02 Kevin Wolf
722 f7d0fe02 Kevin Wolf
/* update the refcounts of snapshots and the copied flag */
723 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
724 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend)
725 f7d0fe02 Kevin Wolf
{
726 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
727 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
728 f7d0fe02 Kevin Wolf
    int64_t old_offset, old_l2_offset;
729 93913dfd Kevin Wolf
    int i, j, l1_modified = 0, nb_csectors, refcount;
730 29c1a730 Kevin Wolf
    int ret;
731 f7d0fe02 Kevin Wolf
732 f7d0fe02 Kevin Wolf
    l2_table = NULL;
733 f7d0fe02 Kevin Wolf
    l1_table = NULL;
734 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
735 43a0cac4 Kevin Wolf
736 43a0cac4 Kevin Wolf
    /* WARNING: qcow2_snapshot_goto relies on this function not using the
737 43a0cac4 Kevin Wolf
     * l1_table_offset when it is the current s->l1_table_offset! Be careful
738 43a0cac4 Kevin Wolf
     * when changing this! */
739 f7d0fe02 Kevin Wolf
    if (l1_table_offset != s->l1_table_offset) {
740 702ef63f Kevin Wolf
        if (l1_size2 != 0) {
741 7267c094 Anthony Liguori
            l1_table = g_malloc0(align_offset(l1_size2, 512));
742 702ef63f Kevin Wolf
        } else {
743 702ef63f Kevin Wolf
            l1_table = NULL;
744 702ef63f Kevin Wolf
        }
745 f7d0fe02 Kevin Wolf
        l1_allocated = 1;
746 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, l1_table_offset,
747 f7d0fe02 Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
748 93913dfd Kevin Wolf
        {
749 93913dfd Kevin Wolf
            ret = -EIO;
750 f7d0fe02 Kevin Wolf
            goto fail;
751 93913dfd Kevin Wolf
        }
752 93913dfd Kevin Wolf
753 f7d0fe02 Kevin Wolf
        for(i = 0;i < l1_size; i++)
754 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
755 f7d0fe02 Kevin Wolf
    } else {
756 f7d0fe02 Kevin Wolf
        assert(l1_size == s->l1_size);
757 f7d0fe02 Kevin Wolf
        l1_table = s->l1_table;
758 f7d0fe02 Kevin Wolf
        l1_allocated = 0;
759 f7d0fe02 Kevin Wolf
    }
760 f7d0fe02 Kevin Wolf
761 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
762 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
763 f7d0fe02 Kevin Wolf
        if (l2_offset) {
764 f7d0fe02 Kevin Wolf
            old_l2_offset = l2_offset;
765 8e37f681 Kevin Wolf
            l2_offset &= L1E_OFFSET_MASK;
766 29c1a730 Kevin Wolf
767 29c1a730 Kevin Wolf
            ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
768 29c1a730 Kevin Wolf
                (void**) &l2_table);
769 29c1a730 Kevin Wolf
            if (ret < 0) {
770 f7d0fe02 Kevin Wolf
                goto fail;
771 29c1a730 Kevin Wolf
            }
772 29c1a730 Kevin Wolf
773 f7d0fe02 Kevin Wolf
            for(j = 0; j < s->l2_size; j++) {
774 f7d0fe02 Kevin Wolf
                offset = be64_to_cpu(l2_table[j]);
775 f7d0fe02 Kevin Wolf
                if (offset != 0) {
776 f7d0fe02 Kevin Wolf
                    old_offset = offset;
777 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
778 f7d0fe02 Kevin Wolf
                    if (offset & QCOW_OFLAG_COMPRESSED) {
779 f7d0fe02 Kevin Wolf
                        nb_csectors = ((offset >> s->csize_shift) &
780 f7d0fe02 Kevin Wolf
                                       s->csize_mask) + 1;
781 db3a964f Kevin Wolf
                        if (addend != 0) {
782 db3a964f Kevin Wolf
                            int ret;
783 db3a964f Kevin Wolf
                            ret = update_refcount(bs,
784 db3a964f Kevin Wolf
                                (offset & s->cluster_offset_mask) & ~511,
785 db3a964f Kevin Wolf
                                nb_csectors * 512, addend);
786 db3a964f Kevin Wolf
                            if (ret < 0) {
787 db3a964f Kevin Wolf
                                goto fail;
788 db3a964f Kevin Wolf
                            }
789 1c4c2814 Kevin Wolf
790 1c4c2814 Kevin Wolf
                            /* TODO Flushing once for the whole function should
791 1c4c2814 Kevin Wolf
                             * be enough */
792 1c4c2814 Kevin Wolf
                            bdrv_flush(bs->file);
793 db3a964f Kevin Wolf
                        }
794 f7d0fe02 Kevin Wolf
                        /* compressed clusters are never modified */
795 f7d0fe02 Kevin Wolf
                        refcount = 2;
796 f7d0fe02 Kevin Wolf
                    } else {
797 8e37f681 Kevin Wolf
                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
798 f7d0fe02 Kevin Wolf
                        if (addend != 0) {
799 8e37f681 Kevin Wolf
                            refcount = update_cluster_refcount(bs, cluster_index, addend);
800 f7d0fe02 Kevin Wolf
                        } else {
801 8e37f681 Kevin Wolf
                            refcount = get_refcount(bs, cluster_index);
802 f7d0fe02 Kevin Wolf
                        }
803 018faafd Kevin Wolf
804 018faafd Kevin Wolf
                        if (refcount < 0) {
805 93913dfd Kevin Wolf
                            ret = -EIO;
806 018faafd Kevin Wolf
                            goto fail;
807 018faafd Kevin Wolf
                        }
808 f7d0fe02 Kevin Wolf
                    }
809 f7d0fe02 Kevin Wolf
810 f7d0fe02 Kevin Wolf
                    if (refcount == 1) {
811 f7d0fe02 Kevin Wolf
                        offset |= QCOW_OFLAG_COPIED;
812 f7d0fe02 Kevin Wolf
                    }
813 f7d0fe02 Kevin Wolf
                    if (offset != old_offset) {
814 29c1a730 Kevin Wolf
                        if (addend > 0) {
815 29c1a730 Kevin Wolf
                            qcow2_cache_set_dependency(bs, s->l2_table_cache,
816 29c1a730 Kevin Wolf
                                s->refcount_block_cache);
817 29c1a730 Kevin Wolf
                        }
818 f7d0fe02 Kevin Wolf
                        l2_table[j] = cpu_to_be64(offset);
819 29c1a730 Kevin Wolf
                        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
820 f7d0fe02 Kevin Wolf
                    }
821 f7d0fe02 Kevin Wolf
                }
822 f7d0fe02 Kevin Wolf
            }
823 29c1a730 Kevin Wolf
824 29c1a730 Kevin Wolf
            ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
825 29c1a730 Kevin Wolf
            if (ret < 0) {
826 29c1a730 Kevin Wolf
                goto fail;
827 f7d0fe02 Kevin Wolf
            }
828 f7d0fe02 Kevin Wolf
829 29c1a730 Kevin Wolf
830 f7d0fe02 Kevin Wolf
            if (addend != 0) {
831 f7d0fe02 Kevin Wolf
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
832 f7d0fe02 Kevin Wolf
            } else {
833 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
834 f7d0fe02 Kevin Wolf
            }
835 018faafd Kevin Wolf
            if (refcount < 0) {
836 93913dfd Kevin Wolf
                ret = -EIO;
837 018faafd Kevin Wolf
                goto fail;
838 018faafd Kevin Wolf
            } else if (refcount == 1) {
839 f7d0fe02 Kevin Wolf
                l2_offset |= QCOW_OFLAG_COPIED;
840 f7d0fe02 Kevin Wolf
            }
841 f7d0fe02 Kevin Wolf
            if (l2_offset != old_l2_offset) {
842 f7d0fe02 Kevin Wolf
                l1_table[i] = l2_offset;
843 f7d0fe02 Kevin Wolf
                l1_modified = 1;
844 f7d0fe02 Kevin Wolf
            }
845 f7d0fe02 Kevin Wolf
        }
846 f7d0fe02 Kevin Wolf
    }
847 93913dfd Kevin Wolf
848 93913dfd Kevin Wolf
    ret = 0;
849 93913dfd Kevin Wolf
fail:
850 93913dfd Kevin Wolf
    if (l2_table) {
851 93913dfd Kevin Wolf
        qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
852 93913dfd Kevin Wolf
    }
853 93913dfd Kevin Wolf
854 43a0cac4 Kevin Wolf
    /* Update L1 only if it isn't deleted anyway (addend = -1) */
855 43a0cac4 Kevin Wolf
    if (addend >= 0 && l1_modified) {
856 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
857 f7d0fe02 Kevin Wolf
            cpu_to_be64s(&l1_table[i]);
858 8b3b7206 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file, l1_table_offset, l1_table,
859 8b3b7206 Kevin Wolf
                        l1_size2) < 0)
860 f7d0fe02 Kevin Wolf
            goto fail;
861 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
862 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
863 f7d0fe02 Kevin Wolf
    }
864 f7d0fe02 Kevin Wolf
    if (l1_allocated)
865 7267c094 Anthony Liguori
        g_free(l1_table);
866 93913dfd Kevin Wolf
    return ret;
867 f7d0fe02 Kevin Wolf
}
868 f7d0fe02 Kevin Wolf
869 f7d0fe02 Kevin Wolf
870 f7d0fe02 Kevin Wolf
871 f7d0fe02 Kevin Wolf
872 f7d0fe02 Kevin Wolf
/*********************************************************/
873 f7d0fe02 Kevin Wolf
/* refcount checking functions */
874 f7d0fe02 Kevin Wolf
875 f7d0fe02 Kevin Wolf
876 f7d0fe02 Kevin Wolf
877 f7d0fe02 Kevin Wolf
/*
878 f7d0fe02 Kevin Wolf
 * Increases the refcount for a range of clusters in a given refcount table.
879 f7d0fe02 Kevin Wolf
 * This is used to construct a temporary refcount table out of L1 and L2 tables
880 f7d0fe02 Kevin Wolf
 * which can be compared the the refcount table saved in the image.
881 f7d0fe02 Kevin Wolf
 *
882 9ac228e0 Kevin Wolf
 * Modifies the number of errors in res.
883 f7d0fe02 Kevin Wolf
 */
884 9ac228e0 Kevin Wolf
static void inc_refcounts(BlockDriverState *bs,
885 9ac228e0 Kevin Wolf
                          BdrvCheckResult *res,
886 f7d0fe02 Kevin Wolf
                          uint16_t *refcount_table,
887 f7d0fe02 Kevin Wolf
                          int refcount_table_size,
888 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
889 f7d0fe02 Kevin Wolf
{
890 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
891 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
892 f7d0fe02 Kevin Wolf
    int k;
893 f7d0fe02 Kevin Wolf
894 f7d0fe02 Kevin Wolf
    if (size <= 0)
895 9ac228e0 Kevin Wolf
        return;
896 f7d0fe02 Kevin Wolf
897 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
898 f7d0fe02 Kevin Wolf
    last = (offset + size - 1) & ~(s->cluster_size - 1);
899 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
900 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
901 f7d0fe02 Kevin Wolf
        k = cluster_offset >> s->cluster_bits;
902 9ac228e0 Kevin Wolf
        if (k < 0) {
903 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
904 f7d0fe02 Kevin Wolf
                cluster_offset);
905 9ac228e0 Kevin Wolf
            res->corruptions++;
906 9ac228e0 Kevin Wolf
        } else if (k >= refcount_table_size) {
907 9ac228e0 Kevin Wolf
            fprintf(stderr, "Warning: cluster offset=0x%" PRIx64 " is after "
908 9ac228e0 Kevin Wolf
                "the end of the image file, can't properly check refcounts.\n",
909 9ac228e0 Kevin Wolf
                cluster_offset);
910 9ac228e0 Kevin Wolf
            res->check_errors++;
911 f7d0fe02 Kevin Wolf
        } else {
912 f7d0fe02 Kevin Wolf
            if (++refcount_table[k] == 0) {
913 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
914 f7d0fe02 Kevin Wolf
                    "\n", cluster_offset);
915 9ac228e0 Kevin Wolf
                res->corruptions++;
916 f7d0fe02 Kevin Wolf
            }
917 f7d0fe02 Kevin Wolf
        }
918 f7d0fe02 Kevin Wolf
    }
919 f7d0fe02 Kevin Wolf
}
920 f7d0fe02 Kevin Wolf
921 f7d0fe02 Kevin Wolf
/*
922 f7d0fe02 Kevin Wolf
 * Increases the refcount in the given refcount table for the all clusters
923 f7d0fe02 Kevin Wolf
 * referenced in the L2 table. While doing so, performs some checks on L2
924 f7d0fe02 Kevin Wolf
 * entries.
925 f7d0fe02 Kevin Wolf
 *
926 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
927 f7d0fe02 Kevin Wolf
 * error occurred.
928 f7d0fe02 Kevin Wolf
 */
929 9ac228e0 Kevin Wolf
static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
930 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
931 f7d0fe02 Kevin Wolf
    int check_copied)
932 f7d0fe02 Kevin Wolf
{
933 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
934 afdf0abe Kevin Wolf
    uint64_t *l2_table, l2_entry;
935 f7d0fe02 Kevin Wolf
    int i, l2_size, nb_csectors, refcount;
936 f7d0fe02 Kevin Wolf
937 f7d0fe02 Kevin Wolf
    /* Read L2 table from disk */
938 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
939 7267c094 Anthony Liguori
    l2_table = g_malloc(l2_size);
940 f7d0fe02 Kevin Wolf
941 66f82cee Kevin Wolf
    if (bdrv_pread(bs->file, l2_offset, l2_table, l2_size) != l2_size)
942 f7d0fe02 Kevin Wolf
        goto fail;
943 f7d0fe02 Kevin Wolf
944 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
945 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->l2_size; i++) {
946 afdf0abe Kevin Wolf
        l2_entry = be64_to_cpu(l2_table[i]);
947 afdf0abe Kevin Wolf
948 afdf0abe Kevin Wolf
        switch (qcow2_get_cluster_type(l2_entry)) {
949 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_COMPRESSED:
950 afdf0abe Kevin Wolf
            /* Compressed clusters don't have QCOW_OFLAG_COPIED */
951 afdf0abe Kevin Wolf
            if (l2_entry & QCOW_OFLAG_COPIED) {
952 afdf0abe Kevin Wolf
                fprintf(stderr, "ERROR: cluster %" PRId64 ": "
953 afdf0abe Kevin Wolf
                    "copied flag must never be set for compressed "
954 afdf0abe Kevin Wolf
                    "clusters\n", l2_entry >> s->cluster_bits);
955 afdf0abe Kevin Wolf
                l2_entry &= ~QCOW_OFLAG_COPIED;
956 afdf0abe Kevin Wolf
                res->corruptions++;
957 afdf0abe Kevin Wolf
            }
958 f7d0fe02 Kevin Wolf
959 afdf0abe Kevin Wolf
            /* Mark cluster as used */
960 afdf0abe Kevin Wolf
            nb_csectors = ((l2_entry >> s->csize_shift) &
961 afdf0abe Kevin Wolf
                           s->csize_mask) + 1;
962 afdf0abe Kevin Wolf
            l2_entry &= s->cluster_offset_mask;
963 afdf0abe Kevin Wolf
            inc_refcounts(bs, res, refcount_table, refcount_table_size,
964 afdf0abe Kevin Wolf
                l2_entry & ~511, nb_csectors * 512);
965 afdf0abe Kevin Wolf
            break;
966 f7d0fe02 Kevin Wolf
967 6377af48 Kevin Wolf
        case QCOW2_CLUSTER_ZERO:
968 6377af48 Kevin Wolf
            if ((l2_entry & L2E_OFFSET_MASK) == 0) {
969 6377af48 Kevin Wolf
                break;
970 6377af48 Kevin Wolf
            }
971 6377af48 Kevin Wolf
            /* fall through */
972 6377af48 Kevin Wolf
973 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_NORMAL:
974 afdf0abe Kevin Wolf
        {
975 afdf0abe Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
976 afdf0abe Kevin Wolf
            uint64_t offset = l2_entry & L2E_OFFSET_MASK;
977 f7d0fe02 Kevin Wolf
978 afdf0abe Kevin Wolf
            if (check_copied) {
979 afdf0abe Kevin Wolf
                refcount = get_refcount(bs, offset >> s->cluster_bits);
980 afdf0abe Kevin Wolf
                if (refcount < 0) {
981 afdf0abe Kevin Wolf
                    fprintf(stderr, "Can't get refcount for offset %"
982 afdf0abe Kevin Wolf
                        PRIx64 ": %s\n", l2_entry, strerror(-refcount));
983 afdf0abe Kevin Wolf
                    goto fail;
984 afdf0abe Kevin Wolf
                }
985 afdf0abe Kevin Wolf
                if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
986 afdf0abe Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
987 afdf0abe Kevin Wolf
                        PRIx64 " refcount=%d\n", l2_entry, refcount);
988 9ac228e0 Kevin Wolf
                    res->corruptions++;
989 f7d0fe02 Kevin Wolf
                }
990 f7d0fe02 Kevin Wolf
            }
991 afdf0abe Kevin Wolf
992 afdf0abe Kevin Wolf
            /* Mark cluster as used */
993 afdf0abe Kevin Wolf
            inc_refcounts(bs, res, refcount_table,refcount_table_size,
994 afdf0abe Kevin Wolf
                offset, s->cluster_size);
995 afdf0abe Kevin Wolf
996 afdf0abe Kevin Wolf
            /* Correct offsets are cluster aligned */
997 afdf0abe Kevin Wolf
            if (offset & (s->cluster_size - 1)) {
998 afdf0abe Kevin Wolf
                fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
999 afdf0abe Kevin Wolf
                    "properly aligned; L2 entry corrupted.\n", offset);
1000 afdf0abe Kevin Wolf
                res->corruptions++;
1001 afdf0abe Kevin Wolf
            }
1002 afdf0abe Kevin Wolf
            break;
1003 afdf0abe Kevin Wolf
        }
1004 afdf0abe Kevin Wolf
1005 afdf0abe Kevin Wolf
        case QCOW2_CLUSTER_UNALLOCATED:
1006 afdf0abe Kevin Wolf
            break;
1007 afdf0abe Kevin Wolf
1008 afdf0abe Kevin Wolf
        default:
1009 afdf0abe Kevin Wolf
            abort();
1010 f7d0fe02 Kevin Wolf
        }
1011 f7d0fe02 Kevin Wolf
    }
1012 f7d0fe02 Kevin Wolf
1013 7267c094 Anthony Liguori
    g_free(l2_table);
1014 9ac228e0 Kevin Wolf
    return 0;
1015 f7d0fe02 Kevin Wolf
1016 f7d0fe02 Kevin Wolf
fail:
1017 9ac228e0 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1018 7267c094 Anthony Liguori
    g_free(l2_table);
1019 f7d0fe02 Kevin Wolf
    return -EIO;
1020 f7d0fe02 Kevin Wolf
}
1021 f7d0fe02 Kevin Wolf
1022 f7d0fe02 Kevin Wolf
/*
1023 f7d0fe02 Kevin Wolf
 * Increases the refcount for the L1 table, its L2 tables and all referenced
1024 f7d0fe02 Kevin Wolf
 * clusters in the given refcount table. While doing so, performs some checks
1025 f7d0fe02 Kevin Wolf
 * on L1 and L2 entries.
1026 f7d0fe02 Kevin Wolf
 *
1027 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
1028 f7d0fe02 Kevin Wolf
 * error occurred.
1029 f7d0fe02 Kevin Wolf
 */
1030 f7d0fe02 Kevin Wolf
static int check_refcounts_l1(BlockDriverState *bs,
1031 9ac228e0 Kevin Wolf
                              BdrvCheckResult *res,
1032 f7d0fe02 Kevin Wolf
                              uint16_t *refcount_table,
1033 f7d0fe02 Kevin Wolf
                              int refcount_table_size,
1034 f7d0fe02 Kevin Wolf
                              int64_t l1_table_offset, int l1_size,
1035 f7d0fe02 Kevin Wolf
                              int check_copied)
1036 f7d0fe02 Kevin Wolf
{
1037 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1038 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, l2_offset, l1_size2;
1039 f7d0fe02 Kevin Wolf
    int i, refcount, ret;
1040 f7d0fe02 Kevin Wolf
1041 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
1042 f7d0fe02 Kevin Wolf
1043 f7d0fe02 Kevin Wolf
    /* Mark L1 table as used */
1044 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, refcount_table_size,
1045 9ac228e0 Kevin Wolf
        l1_table_offset, l1_size2);
1046 f7d0fe02 Kevin Wolf
1047 f7d0fe02 Kevin Wolf
    /* Read L1 table entries from disk */
1048 702ef63f Kevin Wolf
    if (l1_size2 == 0) {
1049 702ef63f Kevin Wolf
        l1_table = NULL;
1050 702ef63f Kevin Wolf
    } else {
1051 7267c094 Anthony Liguori
        l1_table = g_malloc(l1_size2);
1052 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, l1_table_offset,
1053 702ef63f Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
1054 702ef63f Kevin Wolf
            goto fail;
1055 702ef63f Kevin Wolf
        for(i = 0;i < l1_size; i++)
1056 702ef63f Kevin Wolf
            be64_to_cpus(&l1_table[i]);
1057 702ef63f Kevin Wolf
    }
1058 f7d0fe02 Kevin Wolf
1059 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
1060 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
1061 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
1062 f7d0fe02 Kevin Wolf
        if (l2_offset) {
1063 f7d0fe02 Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
1064 f7d0fe02 Kevin Wolf
            if (check_copied) {
1065 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
1066 f7d0fe02 Kevin Wolf
                    >> s->cluster_bits);
1067 018faafd Kevin Wolf
                if (refcount < 0) {
1068 018faafd Kevin Wolf
                    fprintf(stderr, "Can't get refcount for l2_offset %"
1069 018faafd Kevin Wolf
                        PRIx64 ": %s\n", l2_offset, strerror(-refcount));
1070 9ac228e0 Kevin Wolf
                    goto fail;
1071 018faafd Kevin Wolf
                }
1072 f7d0fe02 Kevin Wolf
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1073 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
1074 f7d0fe02 Kevin Wolf
                        " refcount=%d\n", l2_offset, refcount);
1075 9ac228e0 Kevin Wolf
                    res->corruptions++;
1076 f7d0fe02 Kevin Wolf
                }
1077 f7d0fe02 Kevin Wolf
            }
1078 f7d0fe02 Kevin Wolf
1079 f7d0fe02 Kevin Wolf
            /* Mark L2 table as used */
1080 afdf0abe Kevin Wolf
            l2_offset &= L1E_OFFSET_MASK;
1081 9ac228e0 Kevin Wolf
            inc_refcounts(bs, res, refcount_table, refcount_table_size,
1082 9ac228e0 Kevin Wolf
                l2_offset, s->cluster_size);
1083 f7d0fe02 Kevin Wolf
1084 f7d0fe02 Kevin Wolf
            /* L2 tables are cluster aligned */
1085 f7d0fe02 Kevin Wolf
            if (l2_offset & (s->cluster_size - 1)) {
1086 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1087 f7d0fe02 Kevin Wolf
                    "cluster aligned; L1 entry corrupted\n", l2_offset);
1088 9ac228e0 Kevin Wolf
                res->corruptions++;
1089 f7d0fe02 Kevin Wolf
            }
1090 f7d0fe02 Kevin Wolf
1091 f7d0fe02 Kevin Wolf
            /* Process and check L2 entries */
1092 9ac228e0 Kevin Wolf
            ret = check_refcounts_l2(bs, res, refcount_table,
1093 9ac228e0 Kevin Wolf
                refcount_table_size, l2_offset, check_copied);
1094 f7d0fe02 Kevin Wolf
            if (ret < 0) {
1095 f7d0fe02 Kevin Wolf
                goto fail;
1096 f7d0fe02 Kevin Wolf
            }
1097 f7d0fe02 Kevin Wolf
        }
1098 f7d0fe02 Kevin Wolf
    }
1099 7267c094 Anthony Liguori
    g_free(l1_table);
1100 9ac228e0 Kevin Wolf
    return 0;
1101 f7d0fe02 Kevin Wolf
1102 f7d0fe02 Kevin Wolf
fail:
1103 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1104 9ac228e0 Kevin Wolf
    res->check_errors++;
1105 7267c094 Anthony Liguori
    g_free(l1_table);
1106 f7d0fe02 Kevin Wolf
    return -EIO;
1107 f7d0fe02 Kevin Wolf
}
1108 f7d0fe02 Kevin Wolf
1109 f7d0fe02 Kevin Wolf
/*
1110 f7d0fe02 Kevin Wolf
 * Checks an image for refcount consistency.
1111 f7d0fe02 Kevin Wolf
 *
1112 f7d0fe02 Kevin Wolf
 * Returns 0 if no errors are found, the number of errors in case the image is
1113 a1c7273b Stefan Weil
 * detected as corrupted, and -errno when an internal error occurred.
1114 f7d0fe02 Kevin Wolf
 */
1115 166acf54 Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
1116 166acf54 Kevin Wolf
                          BdrvCheckMode fix)
1117 f7d0fe02 Kevin Wolf
{
1118 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1119 166acf54 Kevin Wolf
    int64_t size, i;
1120 166acf54 Kevin Wolf
    int nb_clusters, refcount1, refcount2;
1121 f7d0fe02 Kevin Wolf
    QCowSnapshot *sn;
1122 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table;
1123 9ac228e0 Kevin Wolf
    int ret;
1124 f7d0fe02 Kevin Wolf
1125 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1126 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
1127 7267c094 Anthony Liguori
    refcount_table = g_malloc0(nb_clusters * sizeof(uint16_t));
1128 f7d0fe02 Kevin Wolf
1129 f7d0fe02 Kevin Wolf
    /* header */
1130 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1131 9ac228e0 Kevin Wolf
        0, s->cluster_size);
1132 f7d0fe02 Kevin Wolf
1133 f7d0fe02 Kevin Wolf
    /* current L1 table */
1134 9ac228e0 Kevin Wolf
    ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1135 f7d0fe02 Kevin Wolf
                       s->l1_table_offset, s->l1_size, 1);
1136 f7d0fe02 Kevin Wolf
    if (ret < 0) {
1137 80fa3341 Kevin Wolf
        goto fail;
1138 f7d0fe02 Kevin Wolf
    }
1139 f7d0fe02 Kevin Wolf
1140 f7d0fe02 Kevin Wolf
    /* snapshots */
1141 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->nb_snapshots; i++) {
1142 f7d0fe02 Kevin Wolf
        sn = s->snapshots + i;
1143 9ac228e0 Kevin Wolf
        ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
1144 9ac228e0 Kevin Wolf
            sn->l1_table_offset, sn->l1_size, 0);
1145 9ac228e0 Kevin Wolf
        if (ret < 0) {
1146 80fa3341 Kevin Wolf
            goto fail;
1147 9ac228e0 Kevin Wolf
        }
1148 f7d0fe02 Kevin Wolf
    }
1149 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1150 9ac228e0 Kevin Wolf
        s->snapshots_offset, s->snapshots_size);
1151 f7d0fe02 Kevin Wolf
1152 f7d0fe02 Kevin Wolf
    /* refcount data */
1153 9ac228e0 Kevin Wolf
    inc_refcounts(bs, res, refcount_table, nb_clusters,
1154 9ac228e0 Kevin Wolf
        s->refcount_table_offset,
1155 9ac228e0 Kevin Wolf
        s->refcount_table_size * sizeof(uint64_t));
1156 9ac228e0 Kevin Wolf
1157 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++) {
1158 6882c8fa Kevin Wolf
        uint64_t offset, cluster;
1159 f7d0fe02 Kevin Wolf
        offset = s->refcount_table[i];
1160 6882c8fa Kevin Wolf
        cluster = offset >> s->cluster_bits;
1161 746c3cb5 Kevin Wolf
1162 746c3cb5 Kevin Wolf
        /* Refcount blocks are cluster aligned */
1163 746c3cb5 Kevin Wolf
        if (offset & (s->cluster_size - 1)) {
1164 166acf54 Kevin Wolf
            fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
1165 746c3cb5 Kevin Wolf
                "cluster aligned; refcount table entry corrupted\n", i);
1166 9ac228e0 Kevin Wolf
            res->corruptions++;
1167 6882c8fa Kevin Wolf
            continue;
1168 6882c8fa Kevin Wolf
        }
1169 6882c8fa Kevin Wolf
1170 6882c8fa Kevin Wolf
        if (cluster >= nb_clusters) {
1171 166acf54 Kevin Wolf
            fprintf(stderr, "ERROR refcount block %" PRId64
1172 166acf54 Kevin Wolf
                    " is outside image\n", i);
1173 9ac228e0 Kevin Wolf
            res->corruptions++;
1174 6882c8fa Kevin Wolf
            continue;
1175 746c3cb5 Kevin Wolf
        }
1176 746c3cb5 Kevin Wolf
1177 f7d0fe02 Kevin Wolf
        if (offset != 0) {
1178 9ac228e0 Kevin Wolf
            inc_refcounts(bs, res, refcount_table, nb_clusters,
1179 9ac228e0 Kevin Wolf
                offset, s->cluster_size);
1180 6882c8fa Kevin Wolf
            if (refcount_table[cluster] != 1) {
1181 166acf54 Kevin Wolf
                fprintf(stderr, "ERROR refcount block %" PRId64
1182 166acf54 Kevin Wolf
                    " refcount=%d\n",
1183 6882c8fa Kevin Wolf
                    i, refcount_table[cluster]);
1184 9ac228e0 Kevin Wolf
                res->corruptions++;
1185 746c3cb5 Kevin Wolf
            }
1186 f7d0fe02 Kevin Wolf
        }
1187 f7d0fe02 Kevin Wolf
    }
1188 f7d0fe02 Kevin Wolf
1189 f7d0fe02 Kevin Wolf
    /* compare ref counts */
1190 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
1191 f7d0fe02 Kevin Wolf
        refcount1 = get_refcount(bs, i);
1192 018faafd Kevin Wolf
        if (refcount1 < 0) {
1193 166acf54 Kevin Wolf
            fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
1194 018faafd Kevin Wolf
                i, strerror(-refcount1));
1195 9ac228e0 Kevin Wolf
            res->check_errors++;
1196 f74550fd Kevin Wolf
            continue;
1197 018faafd Kevin Wolf
        }
1198 018faafd Kevin Wolf
1199 f7d0fe02 Kevin Wolf
        refcount2 = refcount_table[i];
1200 f7d0fe02 Kevin Wolf
        if (refcount1 != refcount2) {
1201 166acf54 Kevin Wolf
1202 166acf54 Kevin Wolf
            /* Check if we're allowed to fix the mismatch */
1203 166acf54 Kevin Wolf
            int *num_fixed = NULL;
1204 166acf54 Kevin Wolf
            if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
1205 166acf54 Kevin Wolf
                num_fixed = &res->leaks_fixed;
1206 166acf54 Kevin Wolf
            } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
1207 166acf54 Kevin Wolf
                num_fixed = &res->corruptions_fixed;
1208 166acf54 Kevin Wolf
            }
1209 166acf54 Kevin Wolf
1210 166acf54 Kevin Wolf
            fprintf(stderr, "%s cluster %" PRId64 " refcount=%d reference=%d\n",
1211 166acf54 Kevin Wolf
                   num_fixed != NULL     ? "Repairing" :
1212 166acf54 Kevin Wolf
                   refcount1 < refcount2 ? "ERROR" :
1213 166acf54 Kevin Wolf
                                           "Leaked",
1214 f7d0fe02 Kevin Wolf
                   i, refcount1, refcount2);
1215 166acf54 Kevin Wolf
1216 166acf54 Kevin Wolf
            if (num_fixed) {
1217 166acf54 Kevin Wolf
                ret = update_refcount(bs, i << s->cluster_bits, 1,
1218 166acf54 Kevin Wolf
                                      refcount2 - refcount1);
1219 166acf54 Kevin Wolf
                if (ret >= 0) {
1220 166acf54 Kevin Wolf
                    (*num_fixed)++;
1221 166acf54 Kevin Wolf
                    continue;
1222 166acf54 Kevin Wolf
                }
1223 166acf54 Kevin Wolf
            }
1224 166acf54 Kevin Wolf
1225 166acf54 Kevin Wolf
            /* And if we couldn't, print an error */
1226 9ac228e0 Kevin Wolf
            if (refcount1 < refcount2) {
1227 9ac228e0 Kevin Wolf
                res->corruptions++;
1228 9ac228e0 Kevin Wolf
            } else {
1229 9ac228e0 Kevin Wolf
                res->leaks++;
1230 9ac228e0 Kevin Wolf
            }
1231 f7d0fe02 Kevin Wolf
        }
1232 f7d0fe02 Kevin Wolf
    }
1233 f7d0fe02 Kevin Wolf
1234 80fa3341 Kevin Wolf
    ret = 0;
1235 80fa3341 Kevin Wolf
1236 80fa3341 Kevin Wolf
fail:
1237 7267c094 Anthony Liguori
    g_free(refcount_table);
1238 f7d0fe02 Kevin Wolf
1239 80fa3341 Kevin Wolf
    return ret;
1240 f7d0fe02 Kevin Wolf
}