Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ edcdd562

History | View | Annotate | Download (39.2 kB)

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