Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 60beb341

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