Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 6c6ea921

History | View | Annotate | Download (36.8 kB)

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