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