Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ afbaa7b4

History | View | Annotate | Download (39.2 kB)

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