Statistics
| Branch: | Revision:

root / block / qcow2-refcount.c @ db3a964f

History | View | Annotate | Download (29.9 kB)

1 f7d0fe02 Kevin Wolf
/*
2 f7d0fe02 Kevin Wolf
 * Block driver for the QCOW version 2 format
3 f7d0fe02 Kevin Wolf
 *
4 f7d0fe02 Kevin Wolf
 * Copyright (c) 2004-2006 Fabrice Bellard
5 f7d0fe02 Kevin Wolf
 *
6 f7d0fe02 Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 f7d0fe02 Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 f7d0fe02 Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 f7d0fe02 Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 f7d0fe02 Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 f7d0fe02 Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 f7d0fe02 Kevin Wolf
 *
13 f7d0fe02 Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 f7d0fe02 Kevin Wolf
 * all copies or substantial portions of the Software.
15 f7d0fe02 Kevin Wolf
 *
16 f7d0fe02 Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 f7d0fe02 Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 f7d0fe02 Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 f7d0fe02 Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 f7d0fe02 Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 f7d0fe02 Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 f7d0fe02 Kevin Wolf
 * THE SOFTWARE.
23 f7d0fe02 Kevin Wolf
 */
24 f7d0fe02 Kevin Wolf
25 f7d0fe02 Kevin Wolf
#include "qemu-common.h"
26 f7d0fe02 Kevin Wolf
#include "block_int.h"
27 f7d0fe02 Kevin Wolf
#include "block/qcow2.h"
28 f7d0fe02 Kevin Wolf
29 f7d0fe02 Kevin Wolf
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size);
30 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 f7d0fe02 Kevin Wolf
    }
439 f7d0fe02 Kevin Wolf
 redo:
440 f7d0fe02 Kevin Wolf
    free_in_cluster = s->cluster_size -
441 f7d0fe02 Kevin Wolf
        (s->free_byte_offset & (s->cluster_size - 1));
442 f7d0fe02 Kevin Wolf
    if (size <= free_in_cluster) {
443 f7d0fe02 Kevin Wolf
        /* enough space in current cluster */
444 f7d0fe02 Kevin Wolf
        offset = s->free_byte_offset;
445 f7d0fe02 Kevin Wolf
        s->free_byte_offset += size;
446 f7d0fe02 Kevin Wolf
        free_in_cluster -= size;
447 f7d0fe02 Kevin Wolf
        if (free_in_cluster == 0)
448 f7d0fe02 Kevin Wolf
            s->free_byte_offset = 0;
449 f7d0fe02 Kevin Wolf
        if ((offset & (s->cluster_size - 1)) != 0)
450 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
451 f7d0fe02 Kevin Wolf
    } else {
452 ed6ccf0f Kevin Wolf
        offset = qcow2_alloc_clusters(bs, s->cluster_size);
453 f7d0fe02 Kevin Wolf
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
454 f7d0fe02 Kevin Wolf
        if ((cluster_offset + s->cluster_size) == offset) {
455 f7d0fe02 Kevin Wolf
            /* we are lucky: contiguous data */
456 f7d0fe02 Kevin Wolf
            offset = s->free_byte_offset;
457 f7d0fe02 Kevin Wolf
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
458 f7d0fe02 Kevin Wolf
            s->free_byte_offset += size;
459 f7d0fe02 Kevin Wolf
        } else {
460 f7d0fe02 Kevin Wolf
            s->free_byte_offset = offset;
461 f7d0fe02 Kevin Wolf
            goto redo;
462 f7d0fe02 Kevin Wolf
        }
463 f7d0fe02 Kevin Wolf
    }
464 f7d0fe02 Kevin Wolf
    return offset;
465 f7d0fe02 Kevin Wolf
}
466 f7d0fe02 Kevin Wolf
467 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
468 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
469 f7d0fe02 Kevin Wolf
{
470 db3a964f Kevin Wolf
    int ret;
471 db3a964f Kevin Wolf
472 db3a964f Kevin Wolf
    ret = update_refcount(bs, offset, size, -1);
473 db3a964f Kevin Wolf
    if (ret < 0) {
474 db3a964f Kevin Wolf
        fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
475 db3a964f Kevin Wolf
        abort();
476 db3a964f Kevin Wolf
    }
477 f7d0fe02 Kevin Wolf
}
478 f7d0fe02 Kevin Wolf
479 45aba42f Kevin Wolf
/*
480 45aba42f Kevin Wolf
 * free_any_clusters
481 45aba42f Kevin Wolf
 *
482 45aba42f Kevin Wolf
 * free clusters according to its type: compressed or not
483 45aba42f Kevin Wolf
 *
484 45aba42f Kevin Wolf
 */
485 45aba42f Kevin Wolf
486 ed6ccf0f Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs,
487 45aba42f Kevin Wolf
    uint64_t cluster_offset, int nb_clusters)
488 45aba42f Kevin Wolf
{
489 45aba42f Kevin Wolf
    BDRVQcowState *s = bs->opaque;
490 45aba42f Kevin Wolf
491 45aba42f Kevin Wolf
    /* free the cluster */
492 45aba42f Kevin Wolf
493 45aba42f Kevin Wolf
    if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
494 45aba42f Kevin Wolf
        int nb_csectors;
495 45aba42f Kevin Wolf
        nb_csectors = ((cluster_offset >> s->csize_shift) &
496 45aba42f Kevin Wolf
                       s->csize_mask) + 1;
497 ed6ccf0f Kevin Wolf
        qcow2_free_clusters(bs,
498 ed6ccf0f Kevin Wolf
            (cluster_offset & s->cluster_offset_mask) & ~511,
499 ed6ccf0f Kevin Wolf
            nb_csectors * 512);
500 45aba42f Kevin Wolf
        return;
501 45aba42f Kevin Wolf
    }
502 45aba42f Kevin Wolf
503 ed6ccf0f Kevin Wolf
    qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
504 45aba42f Kevin Wolf
505 45aba42f Kevin Wolf
    return;
506 45aba42f Kevin Wolf
}
507 45aba42f Kevin Wolf
508 f7d0fe02 Kevin Wolf
509 f7d0fe02 Kevin Wolf
510 f7d0fe02 Kevin Wolf
/*********************************************************/
511 f7d0fe02 Kevin Wolf
/* snapshots and image creation */
512 f7d0fe02 Kevin Wolf
513 f7d0fe02 Kevin Wolf
514 f7d0fe02 Kevin Wolf
515 ed6ccf0f Kevin Wolf
void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
516 ed6ccf0f Kevin Wolf
    int64_t size)
517 f7d0fe02 Kevin Wolf
{
518 f7d0fe02 Kevin Wolf
    int refcount;
519 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
520 f7d0fe02 Kevin Wolf
    uint16_t *p;
521 f7d0fe02 Kevin Wolf
522 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
523 f7d0fe02 Kevin Wolf
    last = (offset + size - 1)  & ~(s->cluster_size - 1);
524 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
525 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
526 f7d0fe02 Kevin Wolf
        p = &s->refcount_block[cluster_offset >> s->cluster_bits];
527 f7d0fe02 Kevin Wolf
        refcount = be16_to_cpu(*p);
528 f7d0fe02 Kevin Wolf
        refcount++;
529 f7d0fe02 Kevin Wolf
        *p = cpu_to_be16(refcount);
530 f7d0fe02 Kevin Wolf
    }
531 f7d0fe02 Kevin Wolf
}
532 f7d0fe02 Kevin Wolf
533 f7d0fe02 Kevin Wolf
/* update the refcounts of snapshots and the copied flag */
534 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
535 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend)
536 f7d0fe02 Kevin Wolf
{
537 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
538 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
539 f7d0fe02 Kevin Wolf
    int64_t old_offset, old_l2_offset;
540 f7d0fe02 Kevin Wolf
    int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
541 f7d0fe02 Kevin Wolf
542 ed6ccf0f Kevin Wolf
    qcow2_l2_cache_reset(bs);
543 3b88e52b Kevin Wolf
    cache_refcount_updates = 1;
544 f7d0fe02 Kevin Wolf
545 f7d0fe02 Kevin Wolf
    l2_table = NULL;
546 f7d0fe02 Kevin Wolf
    l1_table = NULL;
547 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
548 f7d0fe02 Kevin Wolf
    if (l1_table_offset != s->l1_table_offset) {
549 702ef63f Kevin Wolf
        if (l1_size2 != 0) {
550 702ef63f Kevin Wolf
            l1_table = qemu_mallocz(align_offset(l1_size2, 512));
551 702ef63f Kevin Wolf
        } else {
552 702ef63f Kevin Wolf
            l1_table = NULL;
553 702ef63f Kevin Wolf
        }
554 f7d0fe02 Kevin Wolf
        l1_allocated = 1;
555 f7d0fe02 Kevin Wolf
        if (bdrv_pread(s->hd, l1_table_offset,
556 f7d0fe02 Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
557 f7d0fe02 Kevin Wolf
            goto fail;
558 f7d0fe02 Kevin Wolf
        for(i = 0;i < l1_size; i++)
559 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
560 f7d0fe02 Kevin Wolf
    } else {
561 f7d0fe02 Kevin Wolf
        assert(l1_size == s->l1_size);
562 f7d0fe02 Kevin Wolf
        l1_table = s->l1_table;
563 f7d0fe02 Kevin Wolf
        l1_allocated = 0;
564 f7d0fe02 Kevin Wolf
    }
565 f7d0fe02 Kevin Wolf
566 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
567 f7d0fe02 Kevin Wolf
    l2_table = qemu_malloc(l2_size);
568 f7d0fe02 Kevin Wolf
    l1_modified = 0;
569 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
570 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
571 f7d0fe02 Kevin Wolf
        if (l2_offset) {
572 f7d0fe02 Kevin Wolf
            old_l2_offset = l2_offset;
573 f7d0fe02 Kevin Wolf
            l2_offset &= ~QCOW_OFLAG_COPIED;
574 f7d0fe02 Kevin Wolf
            l2_modified = 0;
575 f7d0fe02 Kevin Wolf
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
576 f7d0fe02 Kevin Wolf
                goto fail;
577 f7d0fe02 Kevin Wolf
            for(j = 0; j < s->l2_size; j++) {
578 f7d0fe02 Kevin Wolf
                offset = be64_to_cpu(l2_table[j]);
579 f7d0fe02 Kevin Wolf
                if (offset != 0) {
580 f7d0fe02 Kevin Wolf
                    old_offset = offset;
581 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
582 f7d0fe02 Kevin Wolf
                    if (offset & QCOW_OFLAG_COMPRESSED) {
583 f7d0fe02 Kevin Wolf
                        nb_csectors = ((offset >> s->csize_shift) &
584 f7d0fe02 Kevin Wolf
                                       s->csize_mask) + 1;
585 db3a964f Kevin Wolf
                        if (addend != 0) {
586 db3a964f Kevin Wolf
                            int ret;
587 db3a964f Kevin Wolf
                            ret = update_refcount(bs,
588 db3a964f Kevin Wolf
                                (offset & s->cluster_offset_mask) & ~511,
589 db3a964f Kevin Wolf
                                nb_csectors * 512, addend);
590 db3a964f Kevin Wolf
                            if (ret < 0) {
591 db3a964f Kevin Wolf
                                goto fail;
592 db3a964f Kevin Wolf
                            }
593 db3a964f Kevin Wolf
                        }
594 f7d0fe02 Kevin Wolf
                        /* compressed clusters are never modified */
595 f7d0fe02 Kevin Wolf
                        refcount = 2;
596 f7d0fe02 Kevin Wolf
                    } else {
597 f7d0fe02 Kevin Wolf
                        if (addend != 0) {
598 f7d0fe02 Kevin Wolf
                            refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
599 f7d0fe02 Kevin Wolf
                        } else {
600 f7d0fe02 Kevin Wolf
                            refcount = get_refcount(bs, offset >> s->cluster_bits);
601 f7d0fe02 Kevin Wolf
                        }
602 f7d0fe02 Kevin Wolf
                    }
603 f7d0fe02 Kevin Wolf
604 f7d0fe02 Kevin Wolf
                    if (refcount == 1) {
605 f7d0fe02 Kevin Wolf
                        offset |= QCOW_OFLAG_COPIED;
606 f7d0fe02 Kevin Wolf
                    }
607 f7d0fe02 Kevin Wolf
                    if (offset != old_offset) {
608 f7d0fe02 Kevin Wolf
                        l2_table[j] = cpu_to_be64(offset);
609 f7d0fe02 Kevin Wolf
                        l2_modified = 1;
610 f7d0fe02 Kevin Wolf
                    }
611 f7d0fe02 Kevin Wolf
                }
612 f7d0fe02 Kevin Wolf
            }
613 f7d0fe02 Kevin Wolf
            if (l2_modified) {
614 f7d0fe02 Kevin Wolf
                if (bdrv_pwrite(s->hd,
615 f7d0fe02 Kevin Wolf
                                l2_offset, l2_table, l2_size) != l2_size)
616 f7d0fe02 Kevin Wolf
                    goto fail;
617 f7d0fe02 Kevin Wolf
            }
618 f7d0fe02 Kevin Wolf
619 f7d0fe02 Kevin Wolf
            if (addend != 0) {
620 f7d0fe02 Kevin Wolf
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
621 f7d0fe02 Kevin Wolf
            } else {
622 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
623 f7d0fe02 Kevin Wolf
            }
624 f7d0fe02 Kevin Wolf
            if (refcount == 1) {
625 f7d0fe02 Kevin Wolf
                l2_offset |= QCOW_OFLAG_COPIED;
626 f7d0fe02 Kevin Wolf
            }
627 f7d0fe02 Kevin Wolf
            if (l2_offset != old_l2_offset) {
628 f7d0fe02 Kevin Wolf
                l1_table[i] = l2_offset;
629 f7d0fe02 Kevin Wolf
                l1_modified = 1;
630 f7d0fe02 Kevin Wolf
            }
631 f7d0fe02 Kevin Wolf
        }
632 f7d0fe02 Kevin Wolf
    }
633 f7d0fe02 Kevin Wolf
    if (l1_modified) {
634 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
635 f7d0fe02 Kevin Wolf
            cpu_to_be64s(&l1_table[i]);
636 f7d0fe02 Kevin Wolf
        if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
637 f7d0fe02 Kevin Wolf
                        l1_size2) != l1_size2)
638 f7d0fe02 Kevin Wolf
            goto fail;
639 f7d0fe02 Kevin Wolf
        for(i = 0; i < l1_size; i++)
640 f7d0fe02 Kevin Wolf
            be64_to_cpus(&l1_table[i]);
641 f7d0fe02 Kevin Wolf
    }
642 f7d0fe02 Kevin Wolf
    if (l1_allocated)
643 f7d0fe02 Kevin Wolf
        qemu_free(l1_table);
644 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
645 3b88e52b Kevin Wolf
    cache_refcount_updates = 0;
646 3b88e52b Kevin Wolf
    write_refcount_block(s);
647 f7d0fe02 Kevin Wolf
    return 0;
648 f7d0fe02 Kevin Wolf
 fail:
649 f7d0fe02 Kevin Wolf
    if (l1_allocated)
650 f7d0fe02 Kevin Wolf
        qemu_free(l1_table);
651 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
652 3b88e52b Kevin Wolf
    cache_refcount_updates = 0;
653 3b88e52b Kevin Wolf
    write_refcount_block(s);
654 f7d0fe02 Kevin Wolf
    return -EIO;
655 f7d0fe02 Kevin Wolf
}
656 f7d0fe02 Kevin Wolf
657 f7d0fe02 Kevin Wolf
658 f7d0fe02 Kevin Wolf
659 f7d0fe02 Kevin Wolf
660 f7d0fe02 Kevin Wolf
/*********************************************************/
661 f7d0fe02 Kevin Wolf
/* refcount checking functions */
662 f7d0fe02 Kevin Wolf
663 f7d0fe02 Kevin Wolf
664 f7d0fe02 Kevin Wolf
665 f7d0fe02 Kevin Wolf
/*
666 f7d0fe02 Kevin Wolf
 * Increases the refcount for a range of clusters in a given refcount table.
667 f7d0fe02 Kevin Wolf
 * This is used to construct a temporary refcount table out of L1 and L2 tables
668 f7d0fe02 Kevin Wolf
 * which can be compared the the refcount table saved in the image.
669 f7d0fe02 Kevin Wolf
 *
670 f7d0fe02 Kevin Wolf
 * Returns the number of errors in the image that were found
671 f7d0fe02 Kevin Wolf
 */
672 f7d0fe02 Kevin Wolf
static int inc_refcounts(BlockDriverState *bs,
673 f7d0fe02 Kevin Wolf
                          uint16_t *refcount_table,
674 f7d0fe02 Kevin Wolf
                          int refcount_table_size,
675 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size)
676 f7d0fe02 Kevin Wolf
{
677 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
678 f7d0fe02 Kevin Wolf
    int64_t start, last, cluster_offset;
679 f7d0fe02 Kevin Wolf
    int k;
680 f7d0fe02 Kevin Wolf
    int errors = 0;
681 f7d0fe02 Kevin Wolf
682 f7d0fe02 Kevin Wolf
    if (size <= 0)
683 f7d0fe02 Kevin Wolf
        return 0;
684 f7d0fe02 Kevin Wolf
685 f7d0fe02 Kevin Wolf
    start = offset & ~(s->cluster_size - 1);
686 f7d0fe02 Kevin Wolf
    last = (offset + size - 1) & ~(s->cluster_size - 1);
687 f7d0fe02 Kevin Wolf
    for(cluster_offset = start; cluster_offset <= last;
688 f7d0fe02 Kevin Wolf
        cluster_offset += s->cluster_size) {
689 f7d0fe02 Kevin Wolf
        k = cluster_offset >> s->cluster_bits;
690 f7d0fe02 Kevin Wolf
        if (k < 0 || k >= refcount_table_size) {
691 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR: invalid cluster offset=0x%" PRIx64 "\n",
692 f7d0fe02 Kevin Wolf
                cluster_offset);
693 f7d0fe02 Kevin Wolf
            errors++;
694 f7d0fe02 Kevin Wolf
        } else {
695 f7d0fe02 Kevin Wolf
            if (++refcount_table[k] == 0) {
696 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
697 f7d0fe02 Kevin Wolf
                    "\n", cluster_offset);
698 f7d0fe02 Kevin Wolf
                errors++;
699 f7d0fe02 Kevin Wolf
            }
700 f7d0fe02 Kevin Wolf
        }
701 f7d0fe02 Kevin Wolf
    }
702 f7d0fe02 Kevin Wolf
703 f7d0fe02 Kevin Wolf
    return errors;
704 f7d0fe02 Kevin Wolf
}
705 f7d0fe02 Kevin Wolf
706 f7d0fe02 Kevin Wolf
/*
707 f7d0fe02 Kevin Wolf
 * Increases the refcount in the given refcount table for the all clusters
708 f7d0fe02 Kevin Wolf
 * referenced in the L2 table. While doing so, performs some checks on L2
709 f7d0fe02 Kevin Wolf
 * entries.
710 f7d0fe02 Kevin Wolf
 *
711 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
712 f7d0fe02 Kevin Wolf
 * error occurred.
713 f7d0fe02 Kevin Wolf
 */
714 f7d0fe02 Kevin Wolf
static int check_refcounts_l2(BlockDriverState *bs,
715 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table, int refcount_table_size, int64_t l2_offset,
716 f7d0fe02 Kevin Wolf
    int check_copied)
717 f7d0fe02 Kevin Wolf
{
718 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
719 f7d0fe02 Kevin Wolf
    uint64_t *l2_table, offset;
720 f7d0fe02 Kevin Wolf
    int i, l2_size, nb_csectors, refcount;
721 f7d0fe02 Kevin Wolf
    int errors = 0;
722 f7d0fe02 Kevin Wolf
723 f7d0fe02 Kevin Wolf
    /* Read L2 table from disk */
724 f7d0fe02 Kevin Wolf
    l2_size = s->l2_size * sizeof(uint64_t);
725 f7d0fe02 Kevin Wolf
    l2_table = qemu_malloc(l2_size);
726 f7d0fe02 Kevin Wolf
727 f7d0fe02 Kevin Wolf
    if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
728 f7d0fe02 Kevin Wolf
        goto fail;
729 f7d0fe02 Kevin Wolf
730 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
731 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->l2_size; i++) {
732 f7d0fe02 Kevin Wolf
        offset = be64_to_cpu(l2_table[i]);
733 f7d0fe02 Kevin Wolf
        if (offset != 0) {
734 f7d0fe02 Kevin Wolf
            if (offset & QCOW_OFLAG_COMPRESSED) {
735 f7d0fe02 Kevin Wolf
                /* Compressed clusters don't have QCOW_OFLAG_COPIED */
736 f7d0fe02 Kevin Wolf
                if (offset & QCOW_OFLAG_COPIED) {
737 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR: cluster %" PRId64 ": "
738 f7d0fe02 Kevin Wolf
                        "copied flag must never be set for compressed "
739 f7d0fe02 Kevin Wolf
                        "clusters\n", offset >> s->cluster_bits);
740 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
741 f7d0fe02 Kevin Wolf
                    errors++;
742 f7d0fe02 Kevin Wolf
                }
743 f7d0fe02 Kevin Wolf
744 f7d0fe02 Kevin Wolf
                /* Mark cluster as used */
745 f7d0fe02 Kevin Wolf
                nb_csectors = ((offset >> s->csize_shift) &
746 f7d0fe02 Kevin Wolf
                               s->csize_mask) + 1;
747 f7d0fe02 Kevin Wolf
                offset &= s->cluster_offset_mask;
748 f7d0fe02 Kevin Wolf
                errors += inc_refcounts(bs, refcount_table,
749 f7d0fe02 Kevin Wolf
                              refcount_table_size,
750 f7d0fe02 Kevin Wolf
                              offset & ~511, nb_csectors * 512);
751 f7d0fe02 Kevin Wolf
            } else {
752 f7d0fe02 Kevin Wolf
                /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
753 f7d0fe02 Kevin Wolf
                if (check_copied) {
754 f7d0fe02 Kevin Wolf
                    uint64_t entry = offset;
755 f7d0fe02 Kevin Wolf
                    offset &= ~QCOW_OFLAG_COPIED;
756 f7d0fe02 Kevin Wolf
                    refcount = get_refcount(bs, offset >> s->cluster_bits);
757 f7d0fe02 Kevin Wolf
                    if ((refcount == 1) != ((entry & QCOW_OFLAG_COPIED) != 0)) {
758 f7d0fe02 Kevin Wolf
                        fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
759 f7d0fe02 Kevin Wolf
                            PRIx64 " refcount=%d\n", entry, refcount);
760 f7d0fe02 Kevin Wolf
                        errors++;
761 f7d0fe02 Kevin Wolf
                    }
762 f7d0fe02 Kevin Wolf
                }
763 f7d0fe02 Kevin Wolf
764 f7d0fe02 Kevin Wolf
                /* Mark cluster as used */
765 f7d0fe02 Kevin Wolf
                offset &= ~QCOW_OFLAG_COPIED;
766 f7d0fe02 Kevin Wolf
                errors += inc_refcounts(bs, refcount_table,
767 f7d0fe02 Kevin Wolf
                              refcount_table_size,
768 f7d0fe02 Kevin Wolf
                              offset, s->cluster_size);
769 f7d0fe02 Kevin Wolf
770 f7d0fe02 Kevin Wolf
                /* Correct offsets are cluster aligned */
771 f7d0fe02 Kevin Wolf
                if (offset & (s->cluster_size - 1)) {
772 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR offset=%" PRIx64 ": Cluster is not "
773 f7d0fe02 Kevin Wolf
                        "properly aligned; L2 entry corrupted.\n", offset);
774 f7d0fe02 Kevin Wolf
                    errors++;
775 f7d0fe02 Kevin Wolf
                }
776 f7d0fe02 Kevin Wolf
            }
777 f7d0fe02 Kevin Wolf
        }
778 f7d0fe02 Kevin Wolf
    }
779 f7d0fe02 Kevin Wolf
780 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
781 f7d0fe02 Kevin Wolf
    return errors;
782 f7d0fe02 Kevin Wolf
783 f7d0fe02 Kevin Wolf
fail:
784 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
785 f7d0fe02 Kevin Wolf
    qemu_free(l2_table);
786 f7d0fe02 Kevin Wolf
    return -EIO;
787 f7d0fe02 Kevin Wolf
}
788 f7d0fe02 Kevin Wolf
789 f7d0fe02 Kevin Wolf
/*
790 f7d0fe02 Kevin Wolf
 * Increases the refcount for the L1 table, its L2 tables and all referenced
791 f7d0fe02 Kevin Wolf
 * clusters in the given refcount table. While doing so, performs some checks
792 f7d0fe02 Kevin Wolf
 * on L1 and L2 entries.
793 f7d0fe02 Kevin Wolf
 *
794 f7d0fe02 Kevin Wolf
 * Returns the number of errors found by the checks or -errno if an internal
795 f7d0fe02 Kevin Wolf
 * error occurred.
796 f7d0fe02 Kevin Wolf
 */
797 f7d0fe02 Kevin Wolf
static int check_refcounts_l1(BlockDriverState *bs,
798 f7d0fe02 Kevin Wolf
                              uint16_t *refcount_table,
799 f7d0fe02 Kevin Wolf
                              int refcount_table_size,
800 f7d0fe02 Kevin Wolf
                              int64_t l1_table_offset, int l1_size,
801 f7d0fe02 Kevin Wolf
                              int check_copied)
802 f7d0fe02 Kevin Wolf
{
803 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
804 f7d0fe02 Kevin Wolf
    uint64_t *l1_table, l2_offset, l1_size2;
805 f7d0fe02 Kevin Wolf
    int i, refcount, ret;
806 f7d0fe02 Kevin Wolf
    int errors = 0;
807 f7d0fe02 Kevin Wolf
808 f7d0fe02 Kevin Wolf
    l1_size2 = l1_size * sizeof(uint64_t);
809 f7d0fe02 Kevin Wolf
810 f7d0fe02 Kevin Wolf
    /* Mark L1 table as used */
811 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, refcount_table_size,
812 f7d0fe02 Kevin Wolf
                  l1_table_offset, l1_size2);
813 f7d0fe02 Kevin Wolf
814 f7d0fe02 Kevin Wolf
    /* Read L1 table entries from disk */
815 702ef63f Kevin Wolf
    if (l1_size2 == 0) {
816 702ef63f Kevin Wolf
        l1_table = NULL;
817 702ef63f Kevin Wolf
    } else {
818 702ef63f Kevin Wolf
        l1_table = qemu_malloc(l1_size2);
819 702ef63f Kevin Wolf
        if (bdrv_pread(s->hd, l1_table_offset,
820 702ef63f Kevin Wolf
                       l1_table, l1_size2) != l1_size2)
821 702ef63f Kevin Wolf
            goto fail;
822 702ef63f Kevin Wolf
        for(i = 0;i < l1_size; i++)
823 702ef63f Kevin Wolf
            be64_to_cpus(&l1_table[i]);
824 702ef63f Kevin Wolf
    }
825 f7d0fe02 Kevin Wolf
826 f7d0fe02 Kevin Wolf
    /* Do the actual checks */
827 f7d0fe02 Kevin Wolf
    for(i = 0; i < l1_size; i++) {
828 f7d0fe02 Kevin Wolf
        l2_offset = l1_table[i];
829 f7d0fe02 Kevin Wolf
        if (l2_offset) {
830 f7d0fe02 Kevin Wolf
            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
831 f7d0fe02 Kevin Wolf
            if (check_copied) {
832 f7d0fe02 Kevin Wolf
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
833 f7d0fe02 Kevin Wolf
                    >> s->cluster_bits);
834 f7d0fe02 Kevin Wolf
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
835 f7d0fe02 Kevin Wolf
                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
836 f7d0fe02 Kevin Wolf
                        " refcount=%d\n", l2_offset, refcount);
837 f7d0fe02 Kevin Wolf
                    errors++;
838 f7d0fe02 Kevin Wolf
                }
839 f7d0fe02 Kevin Wolf
            }
840 f7d0fe02 Kevin Wolf
841 f7d0fe02 Kevin Wolf
            /* Mark L2 table as used */
842 f7d0fe02 Kevin Wolf
            l2_offset &= ~QCOW_OFLAG_COPIED;
843 f7d0fe02 Kevin Wolf
            errors += inc_refcounts(bs, refcount_table,
844 f7d0fe02 Kevin Wolf
                          refcount_table_size,
845 f7d0fe02 Kevin Wolf
                          l2_offset,
846 f7d0fe02 Kevin Wolf
                          s->cluster_size);
847 f7d0fe02 Kevin Wolf
848 f7d0fe02 Kevin Wolf
            /* L2 tables are cluster aligned */
849 f7d0fe02 Kevin Wolf
            if (l2_offset & (s->cluster_size - 1)) {
850 f7d0fe02 Kevin Wolf
                fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
851 f7d0fe02 Kevin Wolf
                    "cluster aligned; L1 entry corrupted\n", l2_offset);
852 f7d0fe02 Kevin Wolf
                errors++;
853 f7d0fe02 Kevin Wolf
            }
854 f7d0fe02 Kevin Wolf
855 f7d0fe02 Kevin Wolf
            /* Process and check L2 entries */
856 f7d0fe02 Kevin Wolf
            ret = check_refcounts_l2(bs, refcount_table, refcount_table_size,
857 f7d0fe02 Kevin Wolf
                l2_offset, check_copied);
858 f7d0fe02 Kevin Wolf
            if (ret < 0) {
859 f7d0fe02 Kevin Wolf
                goto fail;
860 f7d0fe02 Kevin Wolf
            }
861 f7d0fe02 Kevin Wolf
            errors += ret;
862 f7d0fe02 Kevin Wolf
        }
863 f7d0fe02 Kevin Wolf
    }
864 f7d0fe02 Kevin Wolf
    qemu_free(l1_table);
865 f7d0fe02 Kevin Wolf
    return errors;
866 f7d0fe02 Kevin Wolf
867 f7d0fe02 Kevin Wolf
fail:
868 f7d0fe02 Kevin Wolf
    fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
869 f7d0fe02 Kevin Wolf
    qemu_free(l1_table);
870 f7d0fe02 Kevin Wolf
    return -EIO;
871 f7d0fe02 Kevin Wolf
}
872 f7d0fe02 Kevin Wolf
873 f7d0fe02 Kevin Wolf
/*
874 f7d0fe02 Kevin Wolf
 * Checks an image for refcount consistency.
875 f7d0fe02 Kevin Wolf
 *
876 f7d0fe02 Kevin Wolf
 * Returns 0 if no errors are found, the number of errors in case the image is
877 f7d0fe02 Kevin Wolf
 * detected as corrupted, and -errno when an internal error occured.
878 f7d0fe02 Kevin Wolf
 */
879 ed6ccf0f Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs)
880 f7d0fe02 Kevin Wolf
{
881 f7d0fe02 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
882 f7d0fe02 Kevin Wolf
    int64_t size;
883 f7d0fe02 Kevin Wolf
    int nb_clusters, refcount1, refcount2, i;
884 f7d0fe02 Kevin Wolf
    QCowSnapshot *sn;
885 f7d0fe02 Kevin Wolf
    uint16_t *refcount_table;
886 f7d0fe02 Kevin Wolf
    int ret, errors = 0;
887 f7d0fe02 Kevin Wolf
888 f7d0fe02 Kevin Wolf
    size = bdrv_getlength(s->hd);
889 f7d0fe02 Kevin Wolf
    nb_clusters = size_to_clusters(s, size);
890 f7d0fe02 Kevin Wolf
    refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
891 f7d0fe02 Kevin Wolf
892 f7d0fe02 Kevin Wolf
    /* header */
893 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
894 f7d0fe02 Kevin Wolf
                  0, s->cluster_size);
895 f7d0fe02 Kevin Wolf
896 f7d0fe02 Kevin Wolf
    /* current L1 table */
897 f7d0fe02 Kevin Wolf
    ret = check_refcounts_l1(bs, refcount_table, nb_clusters,
898 f7d0fe02 Kevin Wolf
                       s->l1_table_offset, s->l1_size, 1);
899 f7d0fe02 Kevin Wolf
    if (ret < 0) {
900 f7d0fe02 Kevin Wolf
        return ret;
901 f7d0fe02 Kevin Wolf
    }
902 f7d0fe02 Kevin Wolf
    errors += ret;
903 f7d0fe02 Kevin Wolf
904 f7d0fe02 Kevin Wolf
    /* snapshots */
905 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->nb_snapshots; i++) {
906 f7d0fe02 Kevin Wolf
        sn = s->snapshots + i;
907 f7d0fe02 Kevin Wolf
        check_refcounts_l1(bs, refcount_table, nb_clusters,
908 f7d0fe02 Kevin Wolf
                           sn->l1_table_offset, sn->l1_size, 0);
909 f7d0fe02 Kevin Wolf
    }
910 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
911 f7d0fe02 Kevin Wolf
                  s->snapshots_offset, s->snapshots_size);
912 f7d0fe02 Kevin Wolf
913 f7d0fe02 Kevin Wolf
    /* refcount data */
914 f7d0fe02 Kevin Wolf
    errors += inc_refcounts(bs, refcount_table, nb_clusters,
915 f7d0fe02 Kevin Wolf
                  s->refcount_table_offset,
916 f7d0fe02 Kevin Wolf
                  s->refcount_table_size * sizeof(uint64_t));
917 f7d0fe02 Kevin Wolf
    for(i = 0; i < s->refcount_table_size; i++) {
918 f7d0fe02 Kevin Wolf
        int64_t offset;
919 f7d0fe02 Kevin Wolf
        offset = s->refcount_table[i];
920 f7d0fe02 Kevin Wolf
        if (offset != 0) {
921 f7d0fe02 Kevin Wolf
            errors += inc_refcounts(bs, refcount_table, nb_clusters,
922 f7d0fe02 Kevin Wolf
                          offset, s->cluster_size);
923 f7d0fe02 Kevin Wolf
        }
924 f7d0fe02 Kevin Wolf
    }
925 f7d0fe02 Kevin Wolf
926 f7d0fe02 Kevin Wolf
    /* compare ref counts */
927 f7d0fe02 Kevin Wolf
    for(i = 0; i < nb_clusters; i++) {
928 f7d0fe02 Kevin Wolf
        refcount1 = get_refcount(bs, i);
929 f7d0fe02 Kevin Wolf
        refcount2 = refcount_table[i];
930 f7d0fe02 Kevin Wolf
        if (refcount1 != refcount2) {
931 f7d0fe02 Kevin Wolf
            fprintf(stderr, "ERROR cluster %d refcount=%d reference=%d\n",
932 f7d0fe02 Kevin Wolf
                   i, refcount1, refcount2);
933 f7d0fe02 Kevin Wolf
            errors++;
934 f7d0fe02 Kevin Wolf
        }
935 f7d0fe02 Kevin Wolf
    }
936 f7d0fe02 Kevin Wolf
937 f7d0fe02 Kevin Wolf
    qemu_free(refcount_table);
938 f7d0fe02 Kevin Wolf
939 f7d0fe02 Kevin Wolf
    return errors;
940 f7d0fe02 Kevin Wolf
}