Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 33b1db1c

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