Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ 2dedf83e

History | View | Annotate | Download (30 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 f7d0fe02 Kevin Wolf
static int 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 3b88e52b Kevin Wolf
static int write_refcount_block(BDRVQcowState *s)
38 3b88e52b Kevin Wolf
{
39 3b88e52b Kevin Wolf
    size_t size = s->cluster_size;
40 3b88e52b Kevin Wolf
41 3b88e52b Kevin Wolf
    if (s->refcount_block_cache_offset == 0) {
42 3b88e52b Kevin Wolf
        return 0;
43 3b88e52b Kevin Wolf
    }
44 3b88e52b Kevin Wolf
45 3b88e52b Kevin Wolf
    if (bdrv_pwrite(s->hd, s->refcount_block_cache_offset,
46 3b88e52b Kevin Wolf
            s->refcount_block_cache, size) != size)
47 3b88e52b Kevin Wolf
    {
48 3b88e52b Kevin Wolf
        return -EIO;
49 3b88e52b Kevin Wolf
    }
50 3b88e52b Kevin Wolf
51 3b88e52b Kevin Wolf
    return 0;
52 3b88e52b Kevin Wolf
}
53 3b88e52b Kevin Wolf
54 f7d0fe02 Kevin Wolf
/*********************************************************/
55 f7d0fe02 Kevin Wolf
/* refcount handling */
56 f7d0fe02 Kevin Wolf
57 ed6ccf0f Kevin Wolf
int qcow2_refcount_init(BlockDriverState *bs)
58 f7d0fe02 Kevin Wolf
{
59 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
60 f7d0fe02 Kevin Wolf
    int ret, refcount_table_size2, i;
61 f7d0fe02 Kevin Wolf
62 f7d0fe02 Kevin Wolf
    s->refcount_block_cache = qemu_malloc(s->cluster_size);
63 f7d0fe02 Kevin Wolf
    refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
64 f7d0fe02 Kevin Wolf
    s->refcount_table = qemu_malloc(refcount_table_size2);
65 f7d0fe02 Kevin Wolf
    if (s->refcount_table_size > 0) {
66 f7d0fe02 Kevin Wolf
        ret = bdrv_pread(s->hd, s->refcount_table_offset,
67 f7d0fe02 Kevin Wolf
                         s->refcount_table, refcount_table_size2);
68 f7d0fe02 Kevin Wolf
        if (ret != refcount_table_size2)
69 f7d0fe02 Kevin Wolf
            goto fail;
70 f7d0fe02 Kevin Wolf
        for(i = 0; i < s->refcount_table_size; i++)
71 f7d0fe02 Kevin Wolf
            be64_to_cpus(&s->refcount_table[i]);
72 f7d0fe02 Kevin Wolf
    }
73 f7d0fe02 Kevin Wolf
    return 0;
74 f7d0fe02 Kevin Wolf
 fail:
75 f7d0fe02 Kevin Wolf
    return -ENOMEM;
76 f7d0fe02 Kevin Wolf
}
77 f7d0fe02 Kevin Wolf
78 ed6ccf0f Kevin Wolf
void qcow2_refcount_close(BlockDriverState *bs)
79 f7d0fe02 Kevin Wolf
{
80 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
81 f7d0fe02 Kevin Wolf
    qemu_free(s->refcount_block_cache);
82 f7d0fe02 Kevin Wolf
    qemu_free(s->refcount_table);
83 f7d0fe02 Kevin Wolf
}
84 f7d0fe02 Kevin Wolf
85 f7d0fe02 Kevin Wolf
86 f7d0fe02 Kevin Wolf
static int load_refcount_block(BlockDriverState *bs,
87 f7d0fe02 Kevin Wolf
                               int64_t refcount_block_offset)
88 f7d0fe02 Kevin Wolf
{
89 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
90 f7d0fe02 Kevin Wolf
    int ret;
91 3b88e52b Kevin Wolf
92 3b88e52b Kevin Wolf
    if (cache_refcount_updates) {
93 3b88e52b Kevin Wolf
        write_refcount_block(s);
94 3b88e52b Kevin Wolf
    }
95 3b88e52b Kevin Wolf
96 f7d0fe02 Kevin Wolf
    ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
97 f7d0fe02 Kevin Wolf
                     s->cluster_size);
98 f7d0fe02 Kevin Wolf
    if (ret != s->cluster_size)
99 f7d0fe02 Kevin Wolf
        return -EIO;
100 f7d0fe02 Kevin Wolf
    s->refcount_block_cache_offset = refcount_block_offset;
101 f7d0fe02 Kevin Wolf
    return 0;
102 f7d0fe02 Kevin Wolf
}
103 f7d0fe02 Kevin Wolf
104 f7d0fe02 Kevin Wolf
static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
105 f7d0fe02 Kevin Wolf
{
106 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
107 f7d0fe02 Kevin Wolf
    int refcount_table_index, block_index;
108 f7d0fe02 Kevin Wolf
    int64_t refcount_block_offset;
109 f7d0fe02 Kevin Wolf
110 f7d0fe02 Kevin Wolf
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
111 f7d0fe02 Kevin Wolf
    if (refcount_table_index >= s->refcount_table_size)
112 f7d0fe02 Kevin Wolf
        return 0;
113 f7d0fe02 Kevin Wolf
    refcount_block_offset = s->refcount_table[refcount_table_index];
114 f7d0fe02 Kevin Wolf
    if (!refcount_block_offset)
115 f7d0fe02 Kevin Wolf
        return 0;
116 f7d0fe02 Kevin Wolf
    if (refcount_block_offset != s->refcount_block_cache_offset) {
117 f7d0fe02 Kevin Wolf
        /* better than nothing: return allocated if read error */
118 f7d0fe02 Kevin Wolf
        if (load_refcount_block(bs, refcount_block_offset) < 0)
119 f7d0fe02 Kevin Wolf
            return 1;
120 f7d0fe02 Kevin Wolf
    }
121 f7d0fe02 Kevin Wolf
    block_index = cluster_index &
122 f7d0fe02 Kevin Wolf
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
123 f7d0fe02 Kevin Wolf
    return be16_to_cpu(s->refcount_block_cache[block_index]);
124 f7d0fe02 Kevin Wolf
}
125 f7d0fe02 Kevin Wolf
126 f7d0fe02 Kevin Wolf
static int grow_refcount_table(BlockDriverState *bs, int min_size)
127 f7d0fe02 Kevin Wolf
{
128 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
129 f7d0fe02 Kevin Wolf
    int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
130 f7d0fe02 Kevin Wolf
    uint64_t *new_table;
131 f7d0fe02 Kevin Wolf
    int64_t table_offset;
132 f7d0fe02 Kevin Wolf
    uint8_t data[12];
133 f7d0fe02 Kevin Wolf
    int old_table_size;
134 f7d0fe02 Kevin Wolf
    int64_t old_table_offset;
135 f7d0fe02 Kevin Wolf
136 f7d0fe02 Kevin Wolf
    if (min_size <= s->refcount_table_size)
137 f7d0fe02 Kevin Wolf
        return 0;
138 f7d0fe02 Kevin Wolf
    /* compute new table size */
139 f7d0fe02 Kevin Wolf
    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
140 f7d0fe02 Kevin Wolf
    for(;;) {
141 f7d0fe02 Kevin Wolf
        if (refcount_table_clusters == 0) {
142 f7d0fe02 Kevin Wolf
            refcount_table_clusters = 1;
143 f7d0fe02 Kevin Wolf
        } else {
144 f7d0fe02 Kevin Wolf
            refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
145 f7d0fe02 Kevin Wolf
        }
146 f7d0fe02 Kevin Wolf
        new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
147 f7d0fe02 Kevin Wolf
        if (min_size <= new_table_size)
148 f7d0fe02 Kevin Wolf
            break;
149 f7d0fe02 Kevin Wolf
    }
150 f7d0fe02 Kevin Wolf
#ifdef DEBUG_ALLOC2
151 f7d0fe02 Kevin Wolf
    printf("grow_refcount_table from %d to %d\n",
152 f7d0fe02 Kevin Wolf
           s->refcount_table_size,
153 f7d0fe02 Kevin Wolf
           new_table_size);
154 f7d0fe02 Kevin Wolf
#endif
155 f7d0fe02 Kevin Wolf
    new_table_size2 = new_table_size * sizeof(uint64_t);
156 f7d0fe02 Kevin Wolf
    new_table = qemu_mallocz(new_table_size2);
157 f7d0fe02 Kevin Wolf
    memcpy(new_table, s->refcount_table,
158 f7d0fe02 Kevin Wolf
           s->refcount_table_size * sizeof(uint64_t));
159 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++)
160 f7d0fe02 Kevin Wolf
        cpu_to_be64s(&new_table[i]);
161 f7d0fe02 Kevin Wolf
    /* Note: we cannot update the refcount now to avoid recursion */
162 f7d0fe02 Kevin Wolf
    table_offset = alloc_clusters_noref(bs, new_table_size2);
163 f7d0fe02 Kevin Wolf
    ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
164 f7d0fe02 Kevin Wolf
    if (ret != new_table_size2)
165 f7d0fe02 Kevin Wolf
        goto fail;
166 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++)
167 f7d0fe02 Kevin Wolf
        be64_to_cpus(&new_table[i]);
168 f7d0fe02 Kevin Wolf
169 f7d0fe02 Kevin Wolf
    cpu_to_be64w((uint64_t*)data, table_offset);
170 f7d0fe02 Kevin Wolf
    cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
171 f2b7c8b3 Kevin Wolf
    ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
172 f2b7c8b3 Kevin Wolf
                    data, sizeof(data));
173 f2b7c8b3 Kevin Wolf
    if (ret != sizeof(data)) {
174 f7d0fe02 Kevin Wolf
        goto fail;
175 f2b7c8b3 Kevin Wolf
    }
176 f2b7c8b3 Kevin Wolf
177 f7d0fe02 Kevin Wolf
    qemu_free(s->refcount_table);
178 f7d0fe02 Kevin Wolf
    old_table_offset = s->refcount_table_offset;
179 f7d0fe02 Kevin Wolf
    old_table_size = s->refcount_table_size;
180 f7d0fe02 Kevin Wolf
    s->refcount_table = new_table;
181 f7d0fe02 Kevin Wolf
    s->refcount_table_size = new_table_size;
182 f7d0fe02 Kevin Wolf
    s->refcount_table_offset = table_offset;
183 f7d0fe02 Kevin Wolf
184 f7d0fe02 Kevin Wolf
    update_refcount(bs, table_offset, new_table_size2, 1);
185 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
186 f7d0fe02 Kevin Wolf
    return 0;
187 f7d0fe02 Kevin Wolf
 fail:
188 f7d0fe02 Kevin Wolf
    qemu_free(new_table);
189 f2b7c8b3 Kevin Wolf
    return ret < 0 ? ret : -EIO;
190 f7d0fe02 Kevin Wolf
}
191 f7d0fe02 Kevin Wolf
192 f7d0fe02 Kevin Wolf
193 f7d0fe02 Kevin Wolf
static int64_t alloc_refcount_block(BlockDriverState *bs, int64_t cluster_index)
194 f7d0fe02 Kevin Wolf
{
195 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
196 f7d0fe02 Kevin Wolf
    int64_t offset, refcount_block_offset;
197 80ee15a6 Kevin Wolf
    unsigned int refcount_table_index;
198 80ee15a6 Kevin Wolf
    int ret;
199 f7d0fe02 Kevin Wolf
    uint64_t data64;
200 3b88e52b Kevin Wolf
    int cache = cache_refcount_updates;
201 f7d0fe02 Kevin Wolf
202 f7d0fe02 Kevin Wolf
    /* Find L1 index and grow refcount table if needed */
203 f7d0fe02 Kevin Wolf
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
204 f7d0fe02 Kevin Wolf
    if (refcount_table_index >= s->refcount_table_size) {
205 f7d0fe02 Kevin Wolf
        ret = grow_refcount_table(bs, refcount_table_index + 1);
206 f7d0fe02 Kevin Wolf
        if (ret < 0)
207 f7d0fe02 Kevin Wolf
            return ret;
208 f7d0fe02 Kevin Wolf
    }
209 f7d0fe02 Kevin Wolf
210 f7d0fe02 Kevin Wolf
    /* Load or allocate the refcount block */
211 f7d0fe02 Kevin Wolf
    refcount_block_offset = s->refcount_table[refcount_table_index];
212 f7d0fe02 Kevin Wolf
    if (!refcount_block_offset) {
213 3b88e52b Kevin Wolf
        if (cache_refcount_updates) {
214 3b88e52b Kevin Wolf
            write_refcount_block(s);
215 3b88e52b Kevin Wolf
            cache_refcount_updates = 0;
216 3b88e52b Kevin Wolf
        }
217 f7d0fe02 Kevin Wolf
        /* create a new refcount block */
218 f7d0fe02 Kevin Wolf
        /* Note: we cannot update the refcount now to avoid recursion */
219 f7d0fe02 Kevin Wolf
        offset = alloc_clusters_noref(bs, s->cluster_size);
220 f7d0fe02 Kevin Wolf
        memset(s->refcount_block_cache, 0, s->cluster_size);
221 f7d0fe02 Kevin Wolf
        ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
222 f7d0fe02 Kevin Wolf
        if (ret != s->cluster_size)
223 f7d0fe02 Kevin Wolf
            return -EINVAL;
224 f7d0fe02 Kevin Wolf
        s->refcount_table[refcount_table_index] = offset;
225 f7d0fe02 Kevin Wolf
        data64 = cpu_to_be64(offset);
226 f7d0fe02 Kevin Wolf
        ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
227 f7d0fe02 Kevin Wolf
                          refcount_table_index * sizeof(uint64_t),
228 f7d0fe02 Kevin Wolf
                          &data64, sizeof(data64));
229 f7d0fe02 Kevin Wolf
        if (ret != sizeof(data64))
230 f7d0fe02 Kevin Wolf
            return -EINVAL;
231 f7d0fe02 Kevin Wolf
232 f7d0fe02 Kevin Wolf
        refcount_block_offset = offset;
233 f7d0fe02 Kevin Wolf
        s->refcount_block_cache_offset = offset;
234 f7d0fe02 Kevin Wolf
        update_refcount(bs, offset, s->cluster_size, 1);
235 3b88e52b Kevin Wolf
        cache_refcount_updates = cache;
236 f7d0fe02 Kevin Wolf
    } else {
237 f7d0fe02 Kevin Wolf
        if (refcount_block_offset != s->refcount_block_cache_offset) {
238 f7d0fe02 Kevin Wolf
            if (load_refcount_block(bs, refcount_block_offset) < 0)
239 f7d0fe02 Kevin Wolf
                return -EIO;
240 f7d0fe02 Kevin Wolf
        }
241 f7d0fe02 Kevin Wolf
    }
242 f7d0fe02 Kevin Wolf
243 f7d0fe02 Kevin Wolf
    return refcount_block_offset;
244 f7d0fe02 Kevin Wolf
}
245 f7d0fe02 Kevin Wolf
246 9923e05e Kevin Wolf
#define REFCOUNTS_PER_SECTOR (512 >> REFCOUNT_SHIFT)
247 9923e05e Kevin Wolf
static int write_refcount_block_entries(BDRVQcowState *s,
248 9923e05e Kevin Wolf
    int64_t refcount_block_offset, int first_index, int last_index)
249 9923e05e Kevin Wolf
{
250 9923e05e Kevin Wolf
    size_t size;
251 9923e05e Kevin Wolf
252 3b88e52b Kevin Wolf
    if (cache_refcount_updates) {
253 3b88e52b Kevin Wolf
        return 0;
254 3b88e52b Kevin Wolf
    }
255 3b88e52b Kevin Wolf
256 9923e05e Kevin Wolf
    first_index &= ~(REFCOUNTS_PER_SECTOR - 1);
257 9923e05e Kevin Wolf
    last_index = (last_index + REFCOUNTS_PER_SECTOR)
258 9923e05e Kevin Wolf
        & ~(REFCOUNTS_PER_SECTOR - 1);
259 9923e05e Kevin Wolf
260 9923e05e Kevin Wolf
    size = (last_index - first_index) << REFCOUNT_SHIFT;
261 9923e05e Kevin Wolf
    if (bdrv_pwrite(s->hd,
262 9923e05e Kevin Wolf
        refcount_block_offset + (first_index << REFCOUNT_SHIFT),
263 9923e05e Kevin Wolf
        &s->refcount_block_cache[first_index], size) != size)
264 9923e05e Kevin Wolf
    {
265 9923e05e Kevin Wolf
        return -EIO;
266 9923e05e Kevin Wolf
    }
267 9923e05e Kevin Wolf
268 9923e05e Kevin Wolf
    return 0;
269 9923e05e Kevin Wolf
}
270 9923e05e Kevin Wolf
271 f7d0fe02 Kevin Wolf
/* XXX: cache several refcount block clusters ? */
272 db3a964f Kevin Wolf
static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
273 db3a964f Kevin Wolf
    int64_t offset, int64_t length, int addend)
274 f7d0fe02 Kevin Wolf
{
275 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
276 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
277 f7d0fe02 Kevin Wolf
    int64_t refcount_block_offset = 0;
278 f7d0fe02 Kevin Wolf
    int64_t table_index = -1, old_table_index;
279 f7d0fe02 Kevin Wolf
    int first_index = -1, last_index = -1;
280 09508d13 Kevin Wolf
    int ret;
281 f7d0fe02 Kevin Wolf
282 f7d0fe02 Kevin Wolf
#ifdef DEBUG_ALLOC2
283 0bf9e31a Blue Swirl
    printf("update_refcount: offset=%" PRId64 " size=%" PRId64 " addend=%d\n",
284 f7d0fe02 Kevin Wolf
           offset, length, addend);
285 f7d0fe02 Kevin Wolf
#endif
286 7322afe7 Kevin Wolf
    if (length < 0) {
287 f7d0fe02 Kevin Wolf
        return -EINVAL;
288 7322afe7 Kevin Wolf
    } else if (length == 0) {
289 7322afe7 Kevin Wolf
        return 0;
290 7322afe7 Kevin Wolf
    }
291 7322afe7 Kevin Wolf
292 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
293 f7d0fe02 Kevin Wolf
    last = (offset + length - 1) & ~(s->cluster_size - 1);
294 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
295 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size)
296 f7d0fe02 Kevin Wolf
    {
297 f7d0fe02 Kevin Wolf
        int block_index, refcount;
298 f7d0fe02 Kevin Wolf
        int64_t cluster_index = cluster_offset >> s->cluster_bits;
299 09508d13 Kevin Wolf
        int64_t new_block;
300 f7d0fe02 Kevin Wolf
301 f7d0fe02 Kevin Wolf
        /* Only write refcount block to disk when we are done with it */
302 f7d0fe02 Kevin Wolf
        old_table_index = table_index;
303 f7d0fe02 Kevin Wolf
        table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
304 f7d0fe02 Kevin Wolf
        if ((old_table_index >= 0) && (table_index != old_table_index)) {
305 9923e05e Kevin Wolf
306 9923e05e Kevin Wolf
            if (write_refcount_block_entries(s, refcount_block_offset,
307 9923e05e Kevin Wolf
                first_index, last_index) < 0)
308 f7d0fe02 Kevin Wolf
            {
309 f7d0fe02 Kevin Wolf
                return -EIO;
310 f7d0fe02 Kevin Wolf
            }
311 f7d0fe02 Kevin Wolf
312 f7d0fe02 Kevin Wolf
            first_index = -1;
313 f7d0fe02 Kevin Wolf
            last_index = -1;
314 f7d0fe02 Kevin Wolf
        }
315 f7d0fe02 Kevin Wolf
316 f7d0fe02 Kevin Wolf
        /* Load the refcount block and allocate it if needed */
317 09508d13 Kevin Wolf
        new_block = alloc_refcount_block(bs, cluster_index);
318 09508d13 Kevin Wolf
        if (new_block < 0) {
319 09508d13 Kevin Wolf
            ret = new_block;
320 09508d13 Kevin Wolf
            goto fail;
321 f7d0fe02 Kevin Wolf
        }
322 09508d13 Kevin Wolf
        refcount_block_offset = new_block;
323 f7d0fe02 Kevin Wolf
324 f7d0fe02 Kevin Wolf
        /* we can update the count and save it */
325 f7d0fe02 Kevin Wolf
        block_index = cluster_index &
326 f7d0fe02 Kevin Wolf
            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
327 f7d0fe02 Kevin Wolf
        if (first_index == -1 || block_index < first_index) {
328 f7d0fe02 Kevin Wolf
            first_index = block_index;
329 f7d0fe02 Kevin Wolf
        }
330 f7d0fe02 Kevin Wolf
        if (block_index > last_index) {
331 f7d0fe02 Kevin Wolf
            last_index = block_index;
332 f7d0fe02 Kevin Wolf
        }
333 f7d0fe02 Kevin Wolf
334 f7d0fe02 Kevin Wolf
        refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
335 f7d0fe02 Kevin Wolf
        refcount += addend;
336 09508d13 Kevin Wolf
        if (refcount < 0 || refcount > 0xffff) {
337 09508d13 Kevin Wolf
            ret = -EINVAL;
338 09508d13 Kevin Wolf
            goto fail;
339 09508d13 Kevin Wolf
        }
340 f7d0fe02 Kevin Wolf
        if (refcount == 0 && cluster_index < s->free_cluster_index) {
341 f7d0fe02 Kevin Wolf
            s->free_cluster_index = cluster_index;
342 f7d0fe02 Kevin Wolf
        }
343 f7d0fe02 Kevin Wolf
        s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
344 f7d0fe02 Kevin Wolf
    }
345 f7d0fe02 Kevin Wolf
346 09508d13 Kevin Wolf
    ret = 0;
347 09508d13 Kevin Wolf
fail:
348 09508d13 Kevin Wolf
349 f7d0fe02 Kevin Wolf
    /* Write last changed block to disk */
350 f7d0fe02 Kevin Wolf
    if (refcount_block_offset != 0) {
351 9923e05e Kevin Wolf
        if (write_refcount_block_entries(s, refcount_block_offset,
352 9923e05e Kevin Wolf
            first_index, last_index) < 0)
353 f7d0fe02 Kevin Wolf
        {
354 09508d13 Kevin Wolf
            return ret < 0 ? ret : -EIO;
355 f7d0fe02 Kevin Wolf
        }
356 f7d0fe02 Kevin Wolf
    }
357 f7d0fe02 Kevin Wolf
358 09508d13 Kevin Wolf
    /*
359 09508d13 Kevin Wolf
     * Try do undo any updates if an error is returned (This may succeed in
360 09508d13 Kevin Wolf
     * some cases like ENOSPC for allocating a new refcount block)
361 09508d13 Kevin Wolf
     */
362 09508d13 Kevin Wolf
    if (ret < 0) {
363 09508d13 Kevin Wolf
        int dummy;
364 09508d13 Kevin Wolf
        dummy = update_refcount(bs, offset, cluster_offset - offset, -addend);
365 09508d13 Kevin Wolf
    }
366 09508d13 Kevin Wolf
367 09508d13 Kevin Wolf
    return ret;
368 f7d0fe02 Kevin Wolf
}
369 f7d0fe02 Kevin Wolf
370 f7d0fe02 Kevin Wolf
/* addend must be 1 or -1 */
371 f7d0fe02 Kevin Wolf
static int update_cluster_refcount(BlockDriverState *bs,
372 f7d0fe02 Kevin Wolf
                                   int64_t cluster_index,
373 f7d0fe02 Kevin Wolf
                                   int addend)
374 f7d0fe02 Kevin Wolf
{
375 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
376 f7d0fe02 Kevin Wolf
    int ret;
377 f7d0fe02 Kevin Wolf
378 f7d0fe02 Kevin Wolf
    ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend);
379 f7d0fe02 Kevin Wolf
    if (ret < 0) {
380 f7d0fe02 Kevin Wolf
        return ret;
381 f7d0fe02 Kevin Wolf
    }
382 f7d0fe02 Kevin Wolf
383 f7d0fe02 Kevin Wolf
    return get_refcount(bs, cluster_index);
384 f7d0fe02 Kevin Wolf
}
385 f7d0fe02 Kevin Wolf
386 f7d0fe02 Kevin Wolf
387 f7d0fe02 Kevin Wolf
388 f7d0fe02 Kevin Wolf
/*********************************************************/
389 f7d0fe02 Kevin Wolf
/* cluster allocation functions */
390 f7d0fe02 Kevin Wolf
391 f7d0fe02 Kevin Wolf
392 f7d0fe02 Kevin Wolf
393 f7d0fe02 Kevin Wolf
/* return < 0 if error */
394 f7d0fe02 Kevin Wolf
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
395 f7d0fe02 Kevin Wolf
{
396 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
397 f7d0fe02 Kevin Wolf
    int i, nb_clusters;
398 f7d0fe02 Kevin Wolf
399 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
400 f7d0fe02 Kevin Wolf
retry:
401 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
402 f7d0fe02 Kevin Wolf
        int64_t i = s->free_cluster_index++;
403 f7d0fe02 Kevin Wolf
        if (get_refcount(bs, i) != 0)
404 f7d0fe02 Kevin Wolf
            goto retry;
405 f7d0fe02 Kevin Wolf
    }
406 f7d0fe02 Kevin Wolf
#ifdef DEBUG_ALLOC2
407 0bf9e31a Blue Swirl
    printf("alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
408 f7d0fe02 Kevin Wolf
            size,
409 f7d0fe02 Kevin Wolf
            (s->free_cluster_index - nb_clusters) << s->cluster_bits);
410 f7d0fe02 Kevin Wolf
#endif
411 f7d0fe02 Kevin Wolf
    return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
412 f7d0fe02 Kevin Wolf
}
413 f7d0fe02 Kevin Wolf
414 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
415 f7d0fe02 Kevin Wolf
{
416 f7d0fe02 Kevin Wolf
    int64_t offset;
417 db3a964f Kevin Wolf
    int ret;
418 f7d0fe02 Kevin Wolf
419 f7d0fe02 Kevin Wolf
    offset = alloc_clusters_noref(bs, size);
420 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, 1);
421 db3a964f Kevin Wolf
    if (ret < 0) {
422 db3a964f Kevin Wolf
        return ret;
423 db3a964f Kevin Wolf
    }
424 f7d0fe02 Kevin Wolf
    return offset;
425 f7d0fe02 Kevin Wolf
}
426 f7d0fe02 Kevin Wolf
427 f7d0fe02 Kevin Wolf
/* only used to allocate compressed sectors. We try to allocate
428 f7d0fe02 Kevin Wolf
   contiguous sectors. size must be <= cluster_size */
429 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
430 f7d0fe02 Kevin Wolf
{
431 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
432 f7d0fe02 Kevin Wolf
    int64_t offset, cluster_offset;
433 f7d0fe02 Kevin Wolf
    int free_in_cluster;
434 f7d0fe02 Kevin Wolf
435 f7d0fe02 Kevin Wolf
    assert(size > 0 && size <= s->cluster_size);
436 f7d0fe02 Kevin Wolf
    if (s->free_byte_offset == 0) {
437 ed6ccf0f Kevin Wolf
        s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
438 5d757b56 Kevin Wolf
        if (s->free_byte_offset < 0) {
439 5d757b56 Kevin Wolf
            return s->free_byte_offset;
440 5d757b56 Kevin Wolf
        }
441 f7d0fe02 Kevin Wolf
    }
442 f7d0fe02 Kevin Wolf
 redo:
443 f7d0fe02 Kevin Wolf
    free_in_cluster = s->cluster_size -
444 f7d0fe02 Kevin Wolf
        (s->free_byte_offset & (s->cluster_size - 1));
445 f7d0fe02 Kevin Wolf
    if (size <= free_in_cluster) {
446 f7d0fe02 Kevin Wolf
        /* enough space in current cluster */
447 f7d0fe02 Kevin Wolf
        offset = s->free_byte_offset;
448 f7d0fe02 Kevin Wolf
        s->free_byte_offset += size;
449 f7d0fe02 Kevin Wolf
        free_in_cluster -= size;
450 f7d0fe02 Kevin Wolf
        if (free_in_cluster == 0)
451 f7d0fe02 Kevin Wolf
            s->free_byte_offset = 0;
452 f7d0fe02 Kevin Wolf
        if ((offset & (s->cluster_size - 1)) != 0)
453 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
454 f7d0fe02 Kevin Wolf
    } else {
455 ed6ccf0f Kevin Wolf
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
456 5d757b56 Kevin Wolf
        if (offset < 0) {
457 5d757b56 Kevin Wolf
            return offset;
458 5d757b56 Kevin Wolf
        }
459 f7d0fe02 Kevin Wolf
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
460 f7d0fe02 Kevin Wolf
        if ((cluster_offset + s->cluster_size) == offset) {
461 f7d0fe02 Kevin Wolf
            /* we are lucky: contiguous data */
462 f7d0fe02 Kevin Wolf
            offset = s->free_byte_offset;
463 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
464 f7d0fe02 Kevin Wolf
            s->free_byte_offset += size;
465 f7d0fe02 Kevin Wolf
        } else {
466 f7d0fe02 Kevin Wolf
            s->free_byte_offset = offset;
467 f7d0fe02 Kevin Wolf
            goto redo;
468 f7d0fe02 Kevin Wolf
        }
469 f7d0fe02 Kevin Wolf
    }
470 f7d0fe02 Kevin Wolf
    return offset;
471 f7d0fe02 Kevin Wolf
}
472 f7d0fe02 Kevin Wolf
473 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
474 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
475 f7d0fe02 Kevin Wolf
{
476 db3a964f Kevin Wolf
    int ret;
477 db3a964f Kevin Wolf
478 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, -1);
479 db3a964f Kevin Wolf
    if (ret < 0) {
480 db3a964f Kevin Wolf
        fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
481 db3a964f Kevin Wolf
        abort();
482 db3a964f Kevin Wolf
    }
483 f7d0fe02 Kevin Wolf
}
484 f7d0fe02 Kevin Wolf
485 45aba42f Kevin Wolf
/*
486 45aba42f Kevin Wolf
 * free_any_clusters
487 45aba42f Kevin Wolf
 *
488 45aba42f Kevin Wolf
 * free clusters according to its type: compressed or not
489 45aba42f Kevin Wolf
 *
490 45aba42f Kevin Wolf
 */
491 45aba42f Kevin Wolf
492 ed6ccf0f Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs,
493 45aba42f Kevin Wolf
    uint64_t cluster_offset, int nb_clusters)
494 45aba42f Kevin Wolf
{
495 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
496 45aba42f Kevin Wolf
497 45aba42f Kevin Wolf
    /* free the cluster */
498 45aba42f Kevin Wolf
499 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
500 45aba42f Kevin Wolf
        int nb_csectors;
501 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) &
502 45aba42f Kevin Wolf
                       s->csize_mask) + 1;
503 ed6ccf0f Kevin Wolf
        qcow2_free_clusters(bs,
504 ed6ccf0f Kevin Wolf
            (cluster_offset & s->cluster_offset_mask) & ~511,
505 ed6ccf0f Kevin Wolf
            nb_csectors * 512);
506 45aba42f Kevin Wolf
        return;
507 45aba42f Kevin Wolf
    }
508 45aba42f Kevin Wolf
509 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
510 45aba42f Kevin Wolf
511 45aba42f Kevin Wolf
    return;
512 45aba42f Kevin Wolf
}
513 45aba42f Kevin Wolf
514 f7d0fe02 Kevin Wolf
515 f7d0fe02 Kevin Wolf
516 f7d0fe02 Kevin Wolf
/*********************************************************/
517 f7d0fe02 Kevin Wolf
/* snapshots and image creation */
518 f7d0fe02 Kevin Wolf
519 f7d0fe02 Kevin Wolf
520 f7d0fe02 Kevin Wolf
521 ed6ccf0f Kevin Wolf
void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
522 ed6ccf0f Kevin Wolf
    int64_t size)
523 f7d0fe02 Kevin Wolf
{
524 f7d0fe02 Kevin Wolf
    int refcount;
525 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
526 f7d0fe02 Kevin Wolf
    uint16_t *p;
527 f7d0fe02 Kevin Wolf
528 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
529 f7d0fe02 Kevin Wolf
    last = (offset + size - 1)  & ~(s->cluster_size - 1);
530 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
531 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
532 f7d0fe02 Kevin Wolf
        p = &s->refcount_block[cluster_offset >> s->cluster_bits];
533 f7d0fe02 Kevin Wolf
        refcount = be16_to_cpu(*p);
534 f7d0fe02 Kevin Wolf
        refcount++;
535 f7d0fe02 Kevin Wolf
        *p = cpu_to_be16(refcount);
536 f7d0fe02 Kevin Wolf
    }
537 f7d0fe02 Kevin Wolf
}
538 f7d0fe02 Kevin Wolf
539 f7d0fe02 Kevin Wolf
/* update the refcounts of snapshots and the copied flag */
540 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
541 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend)
542 f7d0fe02 Kevin Wolf
{
543 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
544 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
545 f7d0fe02 Kevin Wolf
    int64_t old_offset, old_l2_offset;
546 f7d0fe02 Kevin Wolf
    int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
547 f7d0fe02 Kevin Wolf
548 ed6ccf0f Kevin Wolf
    qcow2_l2_cache_reset(bs);
549 3b88e52b Kevin Wolf
    cache_refcount_updates = 1;
550 f7d0fe02 Kevin Wolf
551 f7d0fe02 Kevin Wolf
    l2_table = NULL;
552 f7d0fe02 Kevin Wolf
    l1_table = NULL;
553 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
554 f7d0fe02 Kevin Wolf
    if (l1_table_offset != s->l1_table_offset) {
555 702ef63f Kevin Wolf
        if (l1_size2 != 0) {
556 702ef63f Kevin Wolf
            l1_table = qemu_mallocz(align_offset(l1_size2, 512));
557 702ef63f Kevin Wolf
        } else {
558 702ef63f Kevin Wolf
            l1_table = NULL;
559 702ef63f Kevin Wolf
        }
560 f7d0fe02 Kevin Wolf
        l1_allocated = 1;
561 f7d0fe02 Kevin Wolf
        if (bdrv_pread(s->hd, l1_table_offset,
562 f7d0fe02 Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
563 f7d0fe02 Kevin Wolf
            goto fail;
564 f7d0fe02 Kevin Wolf
        for(i = 0;i < l1_size; i++)
565 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
566 f7d0fe02 Kevin Wolf
    } else {
567 f7d0fe02 Kevin Wolf
        assert(l1_size == s->l1_size);
568 f7d0fe02 Kevin Wolf
        l1_table = s->l1_table;
569 f7d0fe02 Kevin Wolf
        l1_allocated = 0;
570 f7d0fe02 Kevin Wolf
    }
571 f7d0fe02 Kevin Wolf
572 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
573 f7d0fe02 Kevin Wolf
    l2_table = qemu_malloc(l2_size);
574 f7d0fe02 Kevin Wolf
    l1_modified = 0;
575 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
576 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
577 f7d0fe02 Kevin Wolf
        if (l2_offset) {
578 f7d0fe02 Kevin Wolf
            old_l2_offset = l2_offset;
579 f7d0fe02 Kevin Wolf
            l2_offset &= ~QCOW_OFLAG_COPIED;
580 f7d0fe02 Kevin Wolf
            l2_modified = 0;
581 f7d0fe02 Kevin Wolf
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
582 f7d0fe02 Kevin Wolf
                goto fail;
583 f7d0fe02 Kevin Wolf
            for(j = 0; j < s->l2_size; j++) {
584 f7d0fe02 Kevin Wolf
                offset = be64_to_cpu(l2_table[j]);
585 f7d0fe02 Kevin Wolf
                if (offset != 0) {
586 f7d0fe02 Kevin Wolf
                    old_offset = offset;
587 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
588 f7d0fe02 Kevin Wolf
                    if (offset & QCOW_OFLAG_COMPRESSED) {
589 f7d0fe02 Kevin Wolf
                        nb_csectors = ((offset >> s->csize_shift) &
590 f7d0fe02 Kevin Wolf
                                       s->csize_mask) + 1;
591 db3a964f Kevin Wolf
                        if (addend != 0) {
592 db3a964f Kevin Wolf
                            int ret;
593 db3a964f Kevin Wolf
                            ret = update_refcount(bs,
594 db3a964f Kevin Wolf
                                (offset & s->cluster_offset_mask) & ~511,
595 db3a964f Kevin Wolf
                                nb_csectors * 512, addend);
596 db3a964f Kevin Wolf
                            if (ret < 0) {
597 db3a964f Kevin Wolf
                                goto fail;
598 db3a964f Kevin Wolf
                            }
599 db3a964f Kevin Wolf
                        }
600 f7d0fe02 Kevin Wolf
                        /* compressed clusters are never modified */
601 f7d0fe02 Kevin Wolf
                        refcount = 2;
602 f7d0fe02 Kevin Wolf
                    } else {
603 f7d0fe02 Kevin Wolf
                        if (addend != 0) {
604 f7d0fe02 Kevin Wolf
                            refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
605 f7d0fe02 Kevin Wolf
                        } else {
606 f7d0fe02 Kevin Wolf
                            refcount = get_refcount(bs, offset >> s->cluster_bits);
607 f7d0fe02 Kevin Wolf
                        }
608 f7d0fe02 Kevin Wolf
                    }
609 f7d0fe02 Kevin Wolf
610 f7d0fe02 Kevin Wolf
                    if (refcount == 1) {
611 f7d0fe02 Kevin Wolf
                        offset |= QCOW_OFLAG_COPIED;
612 f7d0fe02 Kevin Wolf
                    }
613 f7d0fe02 Kevin Wolf
                    if (offset != old_offset) {
614 f7d0fe02 Kevin Wolf
                        l2_table[j] = cpu_to_be64(offset);
615 f7d0fe02 Kevin Wolf
                        l2_modified = 1;
616 f7d0fe02 Kevin Wolf
                    }
617 f7d0fe02 Kevin Wolf
                }
618 f7d0fe02 Kevin Wolf
            }
619 f7d0fe02 Kevin Wolf
            if (l2_modified) {
620 f7d0fe02 Kevin Wolf
                if (bdrv_pwrite(s->hd,
621 f7d0fe02 Kevin Wolf
                                l2_offset, l2_table, l2_size) != l2_size)
622 f7d0fe02 Kevin Wolf
                    goto fail;
623 f7d0fe02 Kevin Wolf
            }
624 f7d0fe02 Kevin Wolf
625 f7d0fe02 Kevin Wolf
            if (addend != 0) {
626 f7d0fe02 Kevin Wolf
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
627 f7d0fe02 Kevin Wolf
            } else {
628 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
629 f7d0fe02 Kevin Wolf
            }
630 f7d0fe02 Kevin Wolf
            if (refcount == 1) {
631 f7d0fe02 Kevin Wolf
                l2_offset |= QCOW_OFLAG_COPIED;
632 f7d0fe02 Kevin Wolf
            }
633 f7d0fe02 Kevin Wolf
            if (l2_offset != old_l2_offset) {
634 f7d0fe02 Kevin Wolf
                l1_table[i] = l2_offset;
635 f7d0fe02 Kevin Wolf
                l1_modified = 1;
636 f7d0fe02 Kevin Wolf
            }
637 f7d0fe02 Kevin Wolf
        }
638 f7d0fe02 Kevin Wolf
    }
639 f7d0fe02 Kevin Wolf
    if (l1_modified) {
640 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
641 f7d0fe02 Kevin Wolf
            cpu_to_be64s(&l1_table[i]);
642 f7d0fe02 Kevin Wolf
        if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
643 f7d0fe02 Kevin Wolf
                        l1_size2) != l1_size2)
644 f7d0fe02 Kevin Wolf
            goto fail;
645 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
646 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
647 f7d0fe02 Kevin Wolf
    }
648 f7d0fe02 Kevin Wolf
    if (l1_allocated)
649 f7d0fe02 Kevin Wolf
        qemu_free(l1_table);
650 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
651 3b88e52b Kevin Wolf
    cache_refcount_updates = 0;
652 3b88e52b Kevin Wolf
    write_refcount_block(s);
653 f7d0fe02 Kevin Wolf
    return 0;
654 f7d0fe02 Kevin Wolf
 fail:
655 f7d0fe02 Kevin Wolf
    if (l1_allocated)
656 f7d0fe02 Kevin Wolf
        qemu_free(l1_table);
657 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
658 3b88e52b Kevin Wolf
    cache_refcount_updates = 0;
659 3b88e52b Kevin Wolf
    write_refcount_block(s);
660 f7d0fe02 Kevin Wolf
    return -EIO;
661 f7d0fe02 Kevin Wolf
}
662 f7d0fe02 Kevin Wolf
663 f7d0fe02 Kevin Wolf
664 f7d0fe02 Kevin Wolf
665 f7d0fe02 Kevin Wolf
666 f7d0fe02 Kevin Wolf
/*********************************************************/
667 f7d0fe02 Kevin Wolf
/* refcount checking functions */
668 f7d0fe02 Kevin Wolf
669 f7d0fe02 Kevin Wolf
670 f7d0fe02 Kevin Wolf
671 f7d0fe02 Kevin Wolf
/*
672 f7d0fe02 Kevin Wolf
 * Increases the refcount for a range of clusters in a given refcount table.
673 f7d0fe02 Kevin Wolf
 * This is used to construct a temporary refcount table out of L1 and L2 tables
674 f7d0fe02 Kevin Wolf
 * which can be compared the the refcount table saved in the image.
675 f7d0fe02 Kevin Wolf
 *
676 f7d0fe02 Kevin Wolf
 * Returns the number of errors in the image that were found
677 f7d0fe02 Kevin Wolf
 */
678 f7d0fe02 Kevin Wolf
static int inc_refcounts(BlockDriverState *bs,
679 f7d0fe02 Kevin Wolf
                          uint16_t *refcount_table,
680 f7d0fe02 Kevin Wolf
                          int refcount_table_size,
681 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
682 f7d0fe02 Kevin Wolf
{
683 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
684 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
685 f7d0fe02 Kevin Wolf
    int k;
686 f7d0fe02 Kevin Wolf
    int errors = 0;
687 f7d0fe02 Kevin Wolf
688 f7d0fe02 Kevin Wolf
    if (size <= 0)
689 f7d0fe02 Kevin Wolf
        return 0;
690 f7d0fe02 Kevin Wolf
691 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
692 f7d0fe02 Kevin Wolf
    last = (offset + size - 1) & ~(s->cluster_size - 1);
693 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
694 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
695 f7d0fe02 Kevin Wolf
        k = cluster_offset >> s->cluster_bits;
696 f7d0fe02 Kevin Wolf
        if (k < 0 || k >= refcount_table_size) {
697 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
698 f7d0fe02 Kevin Wolf
                cluster_offset);
699 f7d0fe02 Kevin Wolf
            errors++;
700 f7d0fe02 Kevin Wolf
        } else {
701 f7d0fe02 Kevin Wolf
            if (++refcount_table[k] == 0) {
702 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
703 f7d0fe02 Kevin Wolf
                    "\n", cluster_offset);
704 f7d0fe02 Kevin Wolf
                errors++;
705 f7d0fe02 Kevin Wolf
            }
706 f7d0fe02 Kevin Wolf
        }
707 f7d0fe02 Kevin Wolf
    }
708 f7d0fe02 Kevin Wolf
709 f7d0fe02 Kevin Wolf
    return errors;
710 f7d0fe02 Kevin Wolf
}
711 f7d0fe02 Kevin Wolf
712 f7d0fe02 Kevin Wolf
/*
713 f7d0fe02 Kevin Wolf
 * Increases the refcount in the given refcount table for the all clusters
714 f7d0fe02 Kevin Wolf
 * referenced in the L2 table. While doing so, performs some checks on L2
715 f7d0fe02 Kevin Wolf
 * entries.
716 f7d0fe02 Kevin Wolf
 *
717 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
718 f7d0fe02 Kevin Wolf
 * error occurred.
719 f7d0fe02 Kevin Wolf
 */
720 f7d0fe02 Kevin Wolf
static int check_refcounts_l2(BlockDriverState *bs,
721 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
722 f7d0fe02 Kevin Wolf
    int check_copied)
723 f7d0fe02 Kevin Wolf
{
724 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
725 f7d0fe02 Kevin Wolf
    uint64_t *l2_table, offset;
726 f7d0fe02 Kevin Wolf
    int i, l2_size, nb_csectors, refcount;
727 f7d0fe02 Kevin Wolf
    int errors = 0;
728 f7d0fe02 Kevin Wolf
729 f7d0fe02 Kevin Wolf
    /* Read L2 table from disk */
730 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
731 f7d0fe02 Kevin Wolf
    l2_table = qemu_malloc(l2_size);
732 f7d0fe02 Kevin Wolf
733 f7d0fe02 Kevin Wolf
    if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
734 f7d0fe02 Kevin Wolf
        goto fail;
735 f7d0fe02 Kevin Wolf
736 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
737 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->l2_size; i++) {
738 f7d0fe02 Kevin Wolf
        offset = be64_to_cpu(l2_table[i]);
739 f7d0fe02 Kevin Wolf
        if (offset != 0) {
740 f7d0fe02 Kevin Wolf
            if (offset & QCOW_OFLAG_COMPRESSED) {
741 f7d0fe02 Kevin Wolf
                /* Compressed clusters don't have QCOW_OFLAG_COPIED */
742 f7d0fe02 Kevin Wolf
                if (offset & QCOW_OFLAG_COPIED) {
743 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR: cluster %" PRId64 ": "
744 f7d0fe02 Kevin Wolf
                        "copied flag must never be set for compressed "
745 f7d0fe02 Kevin Wolf
                        "clusters\n", offset >> s->cluster_bits);
746 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
747 f7d0fe02 Kevin Wolf
                    errors++;
748 f7d0fe02 Kevin Wolf
                }
749 f7d0fe02 Kevin Wolf
750 f7d0fe02 Kevin Wolf
                /* Mark cluster as used */
751 f7d0fe02 Kevin Wolf
                nb_csectors = ((offset >> s->csize_shift) &
752 f7d0fe02 Kevin Wolf
                               s->csize_mask) + 1;
753 f7d0fe02 Kevin Wolf
                offset &= s->cluster_offset_mask;
754 f7d0fe02 Kevin Wolf
                errors += inc_refcounts(bs, refcount_table,
755 f7d0fe02 Kevin Wolf
                              refcount_table_size,
756 f7d0fe02 Kevin Wolf
                              offset & ~511, nb_csectors * 512);
757 f7d0fe02 Kevin Wolf
            } else {
758 f7d0fe02 Kevin Wolf
                /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
759 f7d0fe02 Kevin Wolf
                if (check_copied) {
760 f7d0fe02 Kevin Wolf
                    uint64_t entry = offset;
761 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
762 f7d0fe02 Kevin Wolf
                    refcount = get_refcount(bs, offset >> s->cluster_bits);
763 f7d0fe02 Kevin Wolf
                    if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
764 f7d0fe02 Kevin Wolf
                        fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
765 f7d0fe02 Kevin Wolf
                            PRIx64 " refcount=%d\n", entry, refcount);
766 f7d0fe02 Kevin Wolf
                        errors++;
767 f7d0fe02 Kevin Wolf
                    }
768 f7d0fe02 Kevin Wolf
                }
769 f7d0fe02 Kevin Wolf
770 f7d0fe02 Kevin Wolf
                /* Mark cluster as used */
771 f7d0fe02 Kevin Wolf
                offset &= ~QCOW_OFLAG_COPIED;
772 f7d0fe02 Kevin Wolf
                errors += inc_refcounts(bs, refcount_table,
773 f7d0fe02 Kevin Wolf
                              refcount_table_size,
774 f7d0fe02 Kevin Wolf
                              offset, s->cluster_size);
775 f7d0fe02 Kevin Wolf
776 f7d0fe02 Kevin Wolf
                /* Correct offsets are cluster aligned */
777 f7d0fe02 Kevin Wolf
                if (offset & (s->cluster_size - 1)) {
778 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
779 f7d0fe02 Kevin Wolf
                        "properly aligned; L2 entry corrupted.\n", offset);
780 f7d0fe02 Kevin Wolf
                    errors++;
781 f7d0fe02 Kevin Wolf
                }
782 f7d0fe02 Kevin Wolf
            }
783 f7d0fe02 Kevin Wolf
        }
784 f7d0fe02 Kevin Wolf
    }
785 f7d0fe02 Kevin Wolf
786 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
787 f7d0fe02 Kevin Wolf
    return errors;
788 f7d0fe02 Kevin Wolf
789 f7d0fe02 Kevin Wolf
fail:
790 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
791 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
792 f7d0fe02 Kevin Wolf
    return -EIO;
793 f7d0fe02 Kevin Wolf
}
794 f7d0fe02 Kevin Wolf
795 f7d0fe02 Kevin Wolf
/*
796 f7d0fe02 Kevin Wolf
 * Increases the refcount for the L1 table, its L2 tables and all referenced
797 f7d0fe02 Kevin Wolf
 * clusters in the given refcount table. While doing so, performs some checks
798 f7d0fe02 Kevin Wolf
 * on L1 and L2 entries.
799 f7d0fe02 Kevin Wolf
 *
800 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
801 f7d0fe02 Kevin Wolf
 * error occurred.
802 f7d0fe02 Kevin Wolf
 */
803 f7d0fe02 Kevin Wolf
static int check_refcounts_l1(BlockDriverState *bs,
804 f7d0fe02 Kevin Wolf
                              uint16_t *refcount_table,
805 f7d0fe02 Kevin Wolf
                              int refcount_table_size,
806 f7d0fe02 Kevin Wolf
                              int64_t l1_table_offset, int l1_size,
807 f7d0fe02 Kevin Wolf
                              int check_copied)
808 f7d0fe02 Kevin Wolf
{
809 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
810 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, l2_offset, l1_size2;
811 f7d0fe02 Kevin Wolf
    int i, refcount, ret;
812 f7d0fe02 Kevin Wolf
    int errors = 0;
813 f7d0fe02 Kevin Wolf
814 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
815 f7d0fe02 Kevin Wolf
816 f7d0fe02 Kevin Wolf
    /* Mark L1 table as used */
817 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, refcount_table_size,
818 f7d0fe02 Kevin Wolf
                  l1_table_offset, l1_size2);
819 f7d0fe02 Kevin Wolf
820 f7d0fe02 Kevin Wolf
    /* Read L1 table entries from disk */
821 702ef63f Kevin Wolf
    if (l1_size2 == 0) {
822 702ef63f Kevin Wolf
        l1_table = NULL;
823 702ef63f Kevin Wolf
    } else {
824 702ef63f Kevin Wolf
        l1_table = qemu_malloc(l1_size2);
825 702ef63f Kevin Wolf
        if (bdrv_pread(s->hd, l1_table_offset,
826 702ef63f Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
827 702ef63f Kevin Wolf
            goto fail;
828 702ef63f Kevin Wolf
        for(i = 0;i < l1_size; i++)
829 702ef63f Kevin Wolf
            be64_to_cpus(&l1_table[i]);
830 702ef63f Kevin Wolf
    }
831 f7d0fe02 Kevin Wolf
832 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
833 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
834 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
835 f7d0fe02 Kevin Wolf
        if (l2_offset) {
836 f7d0fe02 Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
837 f7d0fe02 Kevin Wolf
            if (check_copied) {
838 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
839 f7d0fe02 Kevin Wolf
                    >> s->cluster_bits);
840 f7d0fe02 Kevin Wolf
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
841 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
842 f7d0fe02 Kevin Wolf
                        " refcount=%d\n", l2_offset, refcount);
843 f7d0fe02 Kevin Wolf
                    errors++;
844 f7d0fe02 Kevin Wolf
                }
845 f7d0fe02 Kevin Wolf
            }
846 f7d0fe02 Kevin Wolf
847 f7d0fe02 Kevin Wolf
            /* Mark L2 table as used */
848 f7d0fe02 Kevin Wolf
            l2_offset &= ~QCOW_OFLAG_COPIED;
849 f7d0fe02 Kevin Wolf
            errors += inc_refcounts(bs, refcount_table,
850 f7d0fe02 Kevin Wolf
                          refcount_table_size,
851 f7d0fe02 Kevin Wolf
                          l2_offset,
852 f7d0fe02 Kevin Wolf
                          s->cluster_size);
853 f7d0fe02 Kevin Wolf
854 f7d0fe02 Kevin Wolf
            /* L2 tables are cluster aligned */
855 f7d0fe02 Kevin Wolf
            if (l2_offset & (s->cluster_size - 1)) {
856 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
857 f7d0fe02 Kevin Wolf
                    "cluster aligned; L1 entry corrupted\n", l2_offset);
858 f7d0fe02 Kevin Wolf
                errors++;
859 f7d0fe02 Kevin Wolf
            }
860 f7d0fe02 Kevin Wolf
861 f7d0fe02 Kevin Wolf
            /* Process and check L2 entries */
862 f7d0fe02 Kevin Wolf
            ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
863 f7d0fe02 Kevin Wolf
                l2_offset, check_copied);
864 f7d0fe02 Kevin Wolf
            if (ret < 0) {
865 f7d0fe02 Kevin Wolf
                goto fail;
866 f7d0fe02 Kevin Wolf
            }
867 f7d0fe02 Kevin Wolf
            errors += ret;
868 f7d0fe02 Kevin Wolf
        }
869 f7d0fe02 Kevin Wolf
    }
870 f7d0fe02 Kevin Wolf
    qemu_free(l1_table);
871 f7d0fe02 Kevin Wolf
    return errors;
872 f7d0fe02 Kevin Wolf
873 f7d0fe02 Kevin Wolf
fail:
874 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
875 f7d0fe02 Kevin Wolf
    qemu_free(l1_table);
876 f7d0fe02 Kevin Wolf
    return -EIO;
877 f7d0fe02 Kevin Wolf
}
878 f7d0fe02 Kevin Wolf
879 f7d0fe02 Kevin Wolf
/*
880 f7d0fe02 Kevin Wolf
 * Checks an image for refcount consistency.
881 f7d0fe02 Kevin Wolf
 *
882 f7d0fe02 Kevin Wolf
 * Returns 0 if no errors are found, the number of errors in case the image is
883 f7d0fe02 Kevin Wolf
 * detected as corrupted, and -errno when an internal error occured.
884 f7d0fe02 Kevin Wolf
 */
885 ed6ccf0f Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs)
886 f7d0fe02 Kevin Wolf
{
887 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
888 f7d0fe02 Kevin Wolf
    int64_t size;
889 f7d0fe02 Kevin Wolf
    int nb_clusters, refcount1, refcount2, i;
890 f7d0fe02 Kevin Wolf
    QCowSnapshot *sn;
891 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table;
892 f7d0fe02 Kevin Wolf
    int ret, errors = 0;
893 f7d0fe02 Kevin Wolf
894 f7d0fe02 Kevin Wolf
    size = bdrv_getlength(s->hd);
895 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
896 f7d0fe02 Kevin Wolf
    refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
897 f7d0fe02 Kevin Wolf
898 f7d0fe02 Kevin Wolf
    /* header */
899 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
900 f7d0fe02 Kevin Wolf
                  0, s->cluster_size);
901 f7d0fe02 Kevin Wolf
902 f7d0fe02 Kevin Wolf
    /* current L1 table */
903 f7d0fe02 Kevin Wolf
    ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
904 f7d0fe02 Kevin Wolf
                       s->l1_table_offset, s->l1_size, 1);
905 f7d0fe02 Kevin Wolf
    if (ret < 0) {
906 f7d0fe02 Kevin Wolf
        return ret;
907 f7d0fe02 Kevin Wolf
    }
908 f7d0fe02 Kevin Wolf
    errors += ret;
909 f7d0fe02 Kevin Wolf
910 f7d0fe02 Kevin Wolf
    /* snapshots */
911 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->nb_snapshots; i++) {
912 f7d0fe02 Kevin Wolf
        sn = s->snapshots + i;
913 f7d0fe02 Kevin Wolf
        check_refcounts_l1(bs, refcount_table, nb_clusters,
914 f7d0fe02 Kevin Wolf
                           sn->l1_table_offset, sn->l1_size, 0);
915 f7d0fe02 Kevin Wolf
    }
916 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
917 f7d0fe02 Kevin Wolf
                  s->snapshots_offset, s->snapshots_size);
918 f7d0fe02 Kevin Wolf
919 f7d0fe02 Kevin Wolf
    /* refcount data */
920 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
921 f7d0fe02 Kevin Wolf
                  s->refcount_table_offset,
922 f7d0fe02 Kevin Wolf
                  s->refcount_table_size * sizeof(uint64_t));
923 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++) {
924 f7d0fe02 Kevin Wolf
        int64_t offset;
925 f7d0fe02 Kevin Wolf
        offset = s->refcount_table[i];
926 f7d0fe02 Kevin Wolf
        if (offset != 0) {
927 f7d0fe02 Kevin Wolf
            errors += inc_refcounts(bs, refcount_table, nb_clusters,
928 f7d0fe02 Kevin Wolf
                          offset, s->cluster_size);
929 f7d0fe02 Kevin Wolf
        }
930 f7d0fe02 Kevin Wolf
    }
931 f7d0fe02 Kevin Wolf
932 f7d0fe02 Kevin Wolf
    /* compare ref counts */
933 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
934 f7d0fe02 Kevin Wolf
        refcount1 = get_refcount(bs, i);
935 f7d0fe02 Kevin Wolf
        refcount2 = refcount_table[i];
936 f7d0fe02 Kevin Wolf
        if (refcount1 != refcount2) {
937 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
938 f7d0fe02 Kevin Wolf
                   i, refcount1, refcount2);
939 f7d0fe02 Kevin Wolf
            errors++;
940 f7d0fe02 Kevin Wolf
        }
941 f7d0fe02 Kevin Wolf
    }
942 f7d0fe02 Kevin Wolf
943 f7d0fe02 Kevin Wolf
    qemu_free(refcount_table);
944 f7d0fe02 Kevin Wolf
945 f7d0fe02 Kevin Wolf
    return errors;
946 f7d0fe02 Kevin Wolf
}