]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-cluster.c
qcow2: Tighten cluster_offset alignment assertions
[mirror_qemu.git] / block / qcow2-cluster.c
CommitLineData
45aba42f
KW
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
80c71a24 25#include "qemu/osdep.h"
45aba42f
KW
26#include <zlib.h>
27
c9a442e4 28#include "qapi/error.h"
0d8c41da 29#include "qcow2.h"
58369e22 30#include "qemu/bswap.h"
3cce16f4 31#include "trace.h"
45aba42f 32
46b732cd
PB
33int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t exact_size)
34{
35 BDRVQcow2State *s = bs->opaque;
36 int new_l1_size, i, ret;
37
38 if (exact_size >= s->l1_size) {
39 return 0;
40 }
41
42 new_l1_size = exact_size;
43
44#ifdef DEBUG_ALLOC2
45 fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
46#endif
47
48 BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE);
49 ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset +
50 new_l1_size * sizeof(uint64_t),
51 (s->l1_size - new_l1_size) * sizeof(uint64_t), 0);
52 if (ret < 0) {
53 goto fail;
54 }
55
56 ret = bdrv_flush(bs->file->bs);
57 if (ret < 0) {
58 goto fail;
59 }
60
61 BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS);
62 for (i = s->l1_size - 1; i > new_l1_size - 1; i--) {
63 if ((s->l1_table[i] & L1E_OFFSET_MASK) == 0) {
64 continue;
65 }
66 qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK,
67 s->cluster_size, QCOW2_DISCARD_ALWAYS);
68 s->l1_table[i] = 0;
69 }
70 return 0;
71
72fail:
73 /*
74 * If the write in the l1_table failed the image may contain a partially
75 * overwritten l1_table. In this case it would be better to clear the
76 * l1_table in memory to avoid possible image corruption.
77 */
78 memset(s->l1_table + new_l1_size, 0,
79 (s->l1_size - new_l1_size) * sizeof(uint64_t));
80 return ret;
81}
82
2cf7cfa1
KW
83int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
84 bool exact_size)
45aba42f 85{
ff99129a 86 BDRVQcow2State *s = bs->opaque;
2cf7cfa1 87 int new_l1_size2, ret, i;
45aba42f 88 uint64_t *new_l1_table;
fda74f82 89 int64_t old_l1_table_offset, old_l1_size;
2cf7cfa1 90 int64_t new_l1_table_offset, new_l1_size;
45aba42f
KW
91 uint8_t data[12];
92
72893756 93 if (min_size <= s->l1_size)
45aba42f 94 return 0;
72893756 95
b93f9950
HR
96 /* Do a sanity check on min_size before trying to calculate new_l1_size
97 * (this prevents overflows during the while loop for the calculation of
98 * new_l1_size) */
99 if (min_size > INT_MAX / sizeof(uint64_t)) {
100 return -EFBIG;
101 }
102
72893756
SH
103 if (exact_size) {
104 new_l1_size = min_size;
105 } else {
106 /* Bump size up to reduce the number of times we have to grow */
107 new_l1_size = s->l1_size;
108 if (new_l1_size == 0) {
109 new_l1_size = 1;
110 }
111 while (min_size > new_l1_size) {
21cf3e12 112 new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
72893756 113 }
45aba42f 114 }
72893756 115
84c26520
HR
116 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX);
117 if (new_l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
2cf7cfa1
KW
118 return -EFBIG;
119 }
120
45aba42f 121#ifdef DEBUG_ALLOC2
2cf7cfa1
KW
122 fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
123 s->l1_size, new_l1_size);
45aba42f
KW
124#endif
125
126 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
ef97d608 127 new_l1_table = qemu_try_blockalign(bs->file->bs, new_l1_size2);
de82815d
KW
128 if (new_l1_table == NULL) {
129 return -ENOMEM;
130 }
ef97d608 131 memset(new_l1_table, 0, new_l1_size2);
de82815d 132
0647d47c
SH
133 if (s->l1_size) {
134 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
135 }
45aba42f
KW
136
137 /* write new table (align to cluster) */
66f82cee 138 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
ed6ccf0f 139 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
5d757b56 140 if (new_l1_table_offset < 0) {
de82815d 141 qemu_vfree(new_l1_table);
5d757b56
KW
142 return new_l1_table_offset;
143 }
29c1a730
KW
144
145 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
146 if (ret < 0) {
80fa3341 147 goto fail;
29c1a730 148 }
45aba42f 149
cf93980e
HR
150 /* the L1 position has not yet been updated, so these clusters must
151 * indeed be completely free */
231bb267 152 ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
966b000f 153 new_l1_size2, false);
cf93980e
HR
154 if (ret < 0) {
155 goto fail;
156 }
157
66f82cee 158 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
45aba42f
KW
159 for(i = 0; i < s->l1_size; i++)
160 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
d9ca2ea2 161 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset,
9a4f4c31 162 new_l1_table, new_l1_size2);
8b3b7206 163 if (ret < 0)
45aba42f
KW
164 goto fail;
165 for(i = 0; i < s->l1_size; i++)
166 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
167
168 /* set new table */
66f82cee 169 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
f1f7a1dd 170 stl_be_p(data, new_l1_size);
e4ef9f46 171 stq_be_p(data + 4, new_l1_table_offset);
d9ca2ea2 172 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
9a4f4c31 173 data, sizeof(data));
8b3b7206 174 if (ret < 0) {
45aba42f 175 goto fail;
fb8fa77c 176 }
de82815d 177 qemu_vfree(s->l1_table);
fda74f82 178 old_l1_table_offset = s->l1_table_offset;
45aba42f
KW
179 s->l1_table_offset = new_l1_table_offset;
180 s->l1_table = new_l1_table;
fda74f82 181 old_l1_size = s->l1_size;
45aba42f 182 s->l1_size = new_l1_size;
fda74f82
HR
183 qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * sizeof(uint64_t),
184 QCOW2_DISCARD_OTHER);
45aba42f
KW
185 return 0;
186 fail:
de82815d 187 qemu_vfree(new_l1_table);
6cfcb9b8
KW
188 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
189 QCOW2_DISCARD_OTHER);
8b3b7206 190 return ret;
45aba42f
KW
191}
192
45aba42f
KW
193/*
194 * l2_load
195 *
e2b5713e
AG
196 * @bs: The BlockDriverState
197 * @offset: A guest offset, used to calculate what slice of the L2
198 * table to load.
199 * @l2_offset: Offset to the L2 table in the image file.
200 * @l2_slice: Location to store the pointer to the L2 slice.
45aba42f 201 *
e2b5713e
AG
202 * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
203 * that are loaded by the qcow2 cache). If the slice is in the cache,
204 * the cache is used; otherwise the L2 slice is loaded from the image
205 * file.
45aba42f 206 */
e2b5713e
AG
207static int l2_load(BlockDriverState *bs, uint64_t offset,
208 uint64_t l2_offset, uint64_t **l2_slice)
45aba42f 209{
ff99129a 210 BDRVQcow2State *s = bs->opaque;
e2b5713e
AG
211 int start_of_slice = sizeof(uint64_t) *
212 (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
45aba42f 213
e2b5713e
AG
214 return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
215 (void **)l2_slice);
45aba42f
KW
216}
217
6583e3c7
KW
218/*
219 * Writes one sector of the L1 table to the disk (can't update single entries
220 * and we really don't want bdrv_pread to perform a read-modify-write)
221 */
222#define L1_ENTRIES_PER_SECTOR (512 / 8)
e23e400e 223int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
6583e3c7 224{
ff99129a 225 BDRVQcow2State *s = bs->opaque;
a1391444 226 uint64_t buf[L1_ENTRIES_PER_SECTOR] = { 0 };
6583e3c7 227 int l1_start_index;
f7defcb6 228 int i, ret;
6583e3c7
KW
229
230 l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1);
a1391444
HR
231 for (i = 0; i < L1_ENTRIES_PER_SECTOR && l1_start_index + i < s->l1_size;
232 i++)
233 {
6583e3c7
KW
234 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
235 }
236
231bb267 237 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
966b000f 238 s->l1_table_offset + 8 * l1_start_index, sizeof(buf), false);
cf93980e
HR
239 if (ret < 0) {
240 return ret;
241 }
242
66f82cee 243 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
d9ca2ea2 244 ret = bdrv_pwrite_sync(bs->file,
9a4f4c31
KW
245 s->l1_table_offset + 8 * l1_start_index,
246 buf, sizeof(buf));
f7defcb6
KW
247 if (ret < 0) {
248 return ret;
6583e3c7
KW
249 }
250
251 return 0;
252}
253
45aba42f
KW
254/*
255 * l2_allocate
256 *
257 * Allocate a new l2 entry in the file. If l1_index points to an already
258 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
259 * table) copy the contents of the old L2 table into the newly allocated one.
260 * Otherwise the new table is initialized with zeros.
261 *
262 */
263
3861946a 264static int l2_allocate(BlockDriverState *bs, int l1_index)
45aba42f 265{
ff99129a 266 BDRVQcow2State *s = bs->opaque;
6583e3c7 267 uint64_t old_l2_offset;
3861946a
AG
268 uint64_t *l2_slice = NULL;
269 unsigned slice, slice_size2, n_slices;
f4f0d391 270 int64_t l2_offset;
c46e1167 271 int ret;
45aba42f
KW
272
273 old_l2_offset = s->l1_table[l1_index];
274
3cce16f4
KW
275 trace_qcow2_l2_allocate(bs, l1_index);
276
45aba42f
KW
277 /* allocate a new l2 entry */
278
ed6ccf0f 279 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
5d757b56 280 if (l2_offset < 0) {
be0b742e
HR
281 ret = l2_offset;
282 goto fail;
5d757b56 283 }
29c1a730 284
c1c43990
AG
285 /* The offset must fit in the offset field of the L1 table entry */
286 assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
287
98839750
AG
288 /* If we're allocating the table at offset 0 then something is wrong */
289 if (l2_offset == 0) {
290 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
291 "allocation of L2 table at offset 0");
292 ret = -EIO;
293 goto fail;
294 }
295
29c1a730
KW
296 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
297 if (ret < 0) {
298 goto fail;
299 }
45aba42f 300
45aba42f
KW
301 /* allocate a new entry in the l2 cache */
302
3861946a
AG
303 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
304 n_slices = s->cluster_size / slice_size2;
305
3cce16f4 306 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
3861946a 307 for (slice = 0; slice < n_slices; slice++) {
6580bb09 308 ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
3861946a
AG
309 l2_offset + slice * slice_size2,
310 (void **) &l2_slice);
6580bb09
AG
311 if (ret < 0) {
312 goto fail;
313 }
29c1a730 314
6580bb09 315 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
3861946a
AG
316 /* if there was no old l2 table, clear the new slice */
317 memset(l2_slice, 0, slice_size2);
6580bb09 318 } else {
3861946a
AG
319 uint64_t *old_slice;
320 uint64_t old_l2_slice_offset =
321 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
29c1a730 322
3861946a 323 /* if there was an old l2 table, read a slice from the disk */
6580bb09 324 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
3861946a
AG
325 ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
326 (void **) &old_slice);
6580bb09
AG
327 if (ret < 0) {
328 goto fail;
329 }
330
3861946a 331 memcpy(l2_slice, old_slice, slice_size2);
6580bb09 332
3861946a 333 qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
29c1a730
KW
334 }
335
3861946a 336 /* write the l2 slice to the file */
6580bb09 337 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
29c1a730 338
6580bb09 339 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
3861946a
AG
340 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
341 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
45aba42f 342 }
29c1a730 343
29c1a730 344 ret = qcow2_cache_flush(bs, s->l2_table_cache);
c46e1167 345 if (ret < 0) {
175e1152
KW
346 goto fail;
347 }
348
349 /* update the L1 entry */
3cce16f4 350 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
175e1152 351 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
e23e400e 352 ret = qcow2_write_l1_entry(bs, l1_index);
175e1152
KW
353 if (ret < 0) {
354 goto fail;
c46e1167 355 }
45aba42f 356
3cce16f4 357 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
c46e1167 358 return 0;
175e1152
KW
359
360fail:
3cce16f4 361 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
3861946a
AG
362 if (l2_slice != NULL) {
363 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
8585afd8 364 }
68dba0bf 365 s->l1_table[l1_index] = old_l2_offset;
e3b21ef9
HR
366 if (l2_offset > 0) {
367 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
368 QCOW2_DISCARD_ALWAYS);
369 }
175e1152 370 return ret;
45aba42f
KW
371}
372
2bfcc4a0 373/*
13f893c4 374 * Checks how many clusters in a given L2 slice are contiguous in the image
2bfcc4a0
KW
375 * file. As soon as one of the flags in the bitmask stop_flags changes compared
376 * to the first cluster, the search is stopped and the cluster is not counted
377 * as contiguous. (This allows it, for example, to stop at the first compressed
378 * cluster which may require a different handling)
379 */
808c2bb4
KW
380static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters,
381 int cluster_size, uint64_t *l2_slice, uint64_t stop_flags)
45aba42f
KW
382{
383 int i;
3ef95218 384 QCow2ClusterType first_cluster_type;
78a52ad5 385 uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
13f893c4 386 uint64_t first_entry = be64_to_cpu(l2_slice[0]);
15684a47 387 uint64_t offset = first_entry & mask;
45aba42f 388
b8c8353a
KW
389 first_cluster_type = qcow2_get_cluster_type(bs, first_entry);
390 if (first_cluster_type == QCOW2_CLUSTER_UNALLOCATED) {
45aba42f 391 return 0;
564a6b69 392 }
45aba42f 393
564a6b69 394 /* must be allocated */
564a6b69 395 assert(first_cluster_type == QCOW2_CLUSTER_NORMAL ||
fdfab37d 396 first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC);
15684a47 397
61653008 398 for (i = 0; i < nb_clusters; i++) {
13f893c4 399 uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask;
2bfcc4a0 400 if (offset + (uint64_t) i * cluster_size != l2_entry) {
45aba42f 401 break;
2bfcc4a0
KW
402 }
403 }
45aba42f 404
7d37435b 405 return i;
45aba42f
KW
406}
407
4341df8a
EB
408/*
409 * Checks how many consecutive unallocated clusters in a given L2
c26f10ba 410 * slice have the same cluster type.
4341df8a 411 */
808c2bb4
KW
412static int count_contiguous_clusters_unallocated(BlockDriverState *bs,
413 int nb_clusters,
c26f10ba 414 uint64_t *l2_slice,
3ef95218 415 QCow2ClusterType wanted_type)
45aba42f 416{
2bfcc4a0
KW
417 int i;
418
fdfab37d 419 assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN ||
4341df8a 420 wanted_type == QCOW2_CLUSTER_UNALLOCATED);
2bfcc4a0 421 for (i = 0; i < nb_clusters; i++) {
c26f10ba 422 uint64_t entry = be64_to_cpu(l2_slice[i]);
808c2bb4 423 QCow2ClusterType type = qcow2_get_cluster_type(bs, entry);
45aba42f 424
fdfab37d 425 if (type != wanted_type) {
2bfcc4a0
KW
426 break;
427 }
428 }
45aba42f
KW
429
430 return i;
431}
432
672f0f2c
AG
433static int coroutine_fn do_perform_cow_read(BlockDriverState *bs,
434 uint64_t src_cluster_offset,
435 unsigned offset_in_cluster,
86b862c4 436 QEMUIOVector *qiov)
45aba42f 437{
aaa4d20b 438 int ret;
1b9f1491 439
86b862c4 440 if (qiov->size == 0) {
99450c6f
AG
441 return 0;
442 }
443
66f82cee 444 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
aef4acb6 445
dba28555 446 if (!bs->drv) {
672f0f2c 447 return -ENOMEDIUM;
dba28555
HR
448 }
449
aef4acb6
SH
450 /* Call .bdrv_co_readv() directly instead of using the public block-layer
451 * interface. This avoids double I/O throttling and request tracking,
452 * which can lead to deadlock when block layer copy-on-read is enabled.
453 */
df893d25
VSO
454 ret = bs->drv->bdrv_co_preadv_part(bs,
455 src_cluster_offset + offset_in_cluster,
456 qiov->size, qiov, 0, 0);
1b9f1491 457 if (ret < 0) {
672f0f2c 458 return ret;
1b9f1491
KW
459 }
460
672f0f2c
AG
461 return 0;
462}
463
672f0f2c
AG
464static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
465 uint64_t cluster_offset,
466 unsigned offset_in_cluster,
86b862c4 467 QEMUIOVector *qiov)
672f0f2c 468{
966b000f 469 BDRVQcow2State *s = bs->opaque;
672f0f2c
AG
470 int ret;
471
86b862c4 472 if (qiov->size == 0) {
672f0f2c
AG
473 return 0;
474 }
475
231bb267 476 ret = qcow2_pre_write_overlap_check(bs, 0,
966b000f 477 cluster_offset + offset_in_cluster, qiov->size, true);
cf93980e 478 if (ret < 0) {
672f0f2c 479 return ret;
cf93980e
HR
480 }
481
66f82cee 482 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
966b000f 483 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
86b862c4 484 qiov->size, qiov, 0);
1b9f1491 485 if (ret < 0) {
672f0f2c 486 return ret;
1b9f1491
KW
487 }
488
672f0f2c 489 return 0;
45aba42f
KW
490}
491
492
493/*
494 * get_cluster_offset
495 *
ecfe1863
KW
496 * For a given offset of the virtual disk, find the cluster type and offset in
497 * the qcow2 file. The offset is stored in *cluster_offset.
45aba42f 498 *
ecfe1863
KW
499 * On entry, *bytes is the maximum number of contiguous bytes starting at
500 * offset that we are interested in.
45aba42f 501 *
ecfe1863
KW
502 * On exit, *bytes is the number of bytes starting at offset that have the same
503 * cluster type and (if applicable) are stored contiguously in the image file.
504 * Compressed clusters are always returned one by one.
45aba42f 505 *
68d000a3
KW
506 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
507 * cases.
45aba42f 508 */
1c46efaa 509int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
ecfe1863 510 unsigned int *bytes, uint64_t *cluster_offset)
45aba42f 511{
ff99129a 512 BDRVQcow2State *s = bs->opaque;
2cf7cfa1 513 unsigned int l2_index;
fd630039
AG
514 uint64_t l1_index, l2_offset, *l2_slice;
515 int c;
c834cba9
HR
516 unsigned int offset_in_cluster;
517 uint64_t bytes_available, bytes_needed, nb_clusters;
3ef95218 518 QCow2ClusterType type;
55c17e98 519 int ret;
45aba42f 520
b2f65d6b 521 offset_in_cluster = offset_into_cluster(s, offset);
ecfe1863 522 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
45aba42f 523
b2f65d6b 524 /* compute how many bytes there are between the start of the cluster
fd630039
AG
525 * containing offset and the end of the l2 slice that contains
526 * the entry pointing to it */
527 bytes_available =
528 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
529 << s->cluster_bits;
45aba42f 530
b2f65d6b
KW
531 if (bytes_needed > bytes_available) {
532 bytes_needed = bytes_available;
45aba42f
KW
533 }
534
1c46efaa 535 *cluster_offset = 0;
45aba42f 536
b6af0975 537 /* seek to the l2 offset in the l1 table */
45aba42f 538
05b5b6ee 539 l1_index = offset_to_l1_index(s, offset);
68d000a3 540 if (l1_index >= s->l1_size) {
3ef95218 541 type = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 542 goto out;
68d000a3 543 }
45aba42f 544
68d000a3
KW
545 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
546 if (!l2_offset) {
3ef95218 547 type = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 548 goto out;
68d000a3 549 }
45aba42f 550
a97c67ee
HR
551 if (offset_into_cluster(s, l2_offset)) {
552 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
553 " unaligned (L1 index: %#" PRIx64 ")",
554 l2_offset, l1_index);
555 return -EIO;
556 }
557
fd630039 558 /* load the l2 slice in memory */
45aba42f 559
fd630039 560 ret = l2_load(bs, offset, l2_offset, &l2_slice);
55c17e98
KW
561 if (ret < 0) {
562 return ret;
1c46efaa 563 }
45aba42f
KW
564
565 /* find the cluster offset for the given disk offset */
566
fd630039
AG
567 l2_index = offset_to_l2_slice_index(s, offset);
568 *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
b6d36def 569
b2f65d6b 570 nb_clusters = size_to_clusters(s, bytes_needed);
c834cba9
HR
571 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
572 * integers; the minimum cluster size is 512, so this assertion is always
573 * true */
574 assert(nb_clusters <= INT_MAX);
45aba42f 575
808c2bb4 576 type = qcow2_get_cluster_type(bs, *cluster_offset);
fdfab37d
EB
577 if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
578 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
579 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
580 " in pre-v3 image (L2 offset: %#" PRIx64
581 ", L2 index: %#x)", l2_offset, l2_index);
582 ret = -EIO;
583 goto fail;
584 }
3ef95218 585 switch (type) {
68d000a3 586 case QCOW2_CLUSTER_COMPRESSED:
966b000f
KW
587 if (has_data_file(bs)) {
588 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
589 "entry found in image with external data "
590 "file (L2 offset: %#" PRIx64 ", L2 index: "
591 "%#x)", l2_offset, l2_index);
592 ret = -EIO;
593 goto fail;
594 }
68d000a3
KW
595 /* Compressed clusters can only be processed one by one */
596 c = 1;
597 *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
598 break;
fdfab37d 599 case QCOW2_CLUSTER_ZERO_PLAIN:
68d000a3 600 case QCOW2_CLUSTER_UNALLOCATED:
45aba42f 601 /* how many empty clusters ? */
808c2bb4 602 c = count_contiguous_clusters_unallocated(bs, nb_clusters,
fd630039 603 &l2_slice[l2_index], type);
68d000a3
KW
604 *cluster_offset = 0;
605 break;
fdfab37d 606 case QCOW2_CLUSTER_ZERO_ALLOC:
68d000a3 607 case QCOW2_CLUSTER_NORMAL:
45aba42f 608 /* how many allocated clusters ? */
808c2bb4 609 c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
fd630039 610 &l2_slice[l2_index], QCOW_OFLAG_ZERO);
68d000a3 611 *cluster_offset &= L2E_OFFSET_MASK;
a97c67ee 612 if (offset_into_cluster(s, *cluster_offset)) {
fdfab37d
EB
613 qcow2_signal_corruption(bs, true, -1, -1,
614 "Cluster allocation offset %#"
a97c67ee
HR
615 PRIx64 " unaligned (L2 offset: %#" PRIx64
616 ", L2 index: %#x)", *cluster_offset,
617 l2_offset, l2_index);
618 ret = -EIO;
619 goto fail;
620 }
966b000f
KW
621 if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
622 {
623 qcow2_signal_corruption(bs, true, -1, -1,
624 "External data file host cluster offset %#"
625 PRIx64 " does not match guest cluster "
626 "offset: %#" PRIx64
627 ", L2 index: %#x)", *cluster_offset,
628 offset - offset_in_cluster, l2_index);
629 ret = -EIO;
630 goto fail;
631 }
68d000a3 632 break;
1417d7e4
KW
633 default:
634 abort();
45aba42f
KW
635 }
636
fd630039 637 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
29c1a730 638
c834cba9 639 bytes_available = (int64_t)c * s->cluster_size;
68d000a3 640
45aba42f 641out:
b2f65d6b
KW
642 if (bytes_available > bytes_needed) {
643 bytes_available = bytes_needed;
644 }
45aba42f 645
c834cba9
HR
646 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
647 * subtracting offset_in_cluster will therefore definitely yield something
648 * not exceeding UINT_MAX */
649 assert(bytes_available - offset_in_cluster <= UINT_MAX);
ecfe1863 650 *bytes = bytes_available - offset_in_cluster;
45aba42f 651
3ef95218 652 return type;
a97c67ee
HR
653
654fail:
fd630039 655 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
a97c67ee 656 return ret;
45aba42f
KW
657}
658
659/*
660 * get_cluster_table
661 *
662 * for a given disk offset, load (and allocate if needed)
c03bfc5b 663 * the appropriate slice of its l2 table.
45aba42f 664 *
c03bfc5b 665 * the cluster index in the l2 slice is given to the caller.
45aba42f 666 *
1e3e8f1a 667 * Returns 0 on success, -errno in failure case
45aba42f 668 */
45aba42f 669static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
c03bfc5b 670 uint64_t **new_l2_slice,
45aba42f
KW
671 int *new_l2_index)
672{
ff99129a 673 BDRVQcow2State *s = bs->opaque;
2cf7cfa1
KW
674 unsigned int l2_index;
675 uint64_t l1_index, l2_offset;
c03bfc5b 676 uint64_t *l2_slice = NULL;
80ee15a6 677 int ret;
45aba42f 678
b6af0975 679 /* seek to the l2 offset in the l1 table */
45aba42f 680
05b5b6ee 681 l1_index = offset_to_l1_index(s, offset);
45aba42f 682 if (l1_index >= s->l1_size) {
72893756 683 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
1e3e8f1a
KW
684 if (ret < 0) {
685 return ret;
686 }
45aba42f 687 }
8e37f681 688
2cf7cfa1 689 assert(l1_index < s->l1_size);
8e37f681 690 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
a97c67ee
HR
691 if (offset_into_cluster(s, l2_offset)) {
692 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
693 " unaligned (L1 index: %#" PRIx64 ")",
694 l2_offset, l1_index);
695 return -EIO;
696 }
45aba42f 697
05f9ee46 698 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
16fde5f2 699 /* First allocate a new L2 table (and do COW if needed) */
3861946a 700 ret = l2_allocate(bs, l1_index);
c46e1167
KW
701 if (ret < 0) {
702 return ret;
1e3e8f1a 703 }
16fde5f2
KW
704
705 /* Then decrease the refcount of the old table */
706 if (l2_offset) {
6cfcb9b8
KW
707 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
708 QCOW2_DISCARD_OTHER);
16fde5f2 709 }
3861946a
AG
710
711 /* Get the offset of the newly-allocated l2 table */
712 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
713 assert(offset_into_cluster(s, l2_offset) == 0);
05f9ee46
AG
714 }
715
c03bfc5b
AG
716 /* load the l2 slice in memory */
717 ret = l2_load(bs, offset, l2_offset, &l2_slice);
05f9ee46
AG
718 if (ret < 0) {
719 return ret;
45aba42f
KW
720 }
721
722 /* find the cluster offset for the given disk offset */
723
c03bfc5b 724 l2_index = offset_to_l2_slice_index(s, offset);
45aba42f 725
c03bfc5b 726 *new_l2_slice = l2_slice;
45aba42f
KW
727 *new_l2_index = l2_index;
728
1e3e8f1a 729 return 0;
45aba42f
KW
730}
731
732/*
733 * alloc_compressed_cluster_offset
734 *
77e023ff
KW
735 * For a given offset on the virtual disk, allocate a new compressed cluster
736 * and put the host offset of the cluster into *host_offset. If a cluster is
737 * already allocated at the offset, return an error.
45aba42f 738 *
77e023ff 739 * Return 0 on success and -errno in error cases
45aba42f 740 */
77e023ff
KW
741int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
742 uint64_t offset,
743 int compressed_size,
744 uint64_t *host_offset)
45aba42f 745{
ff99129a 746 BDRVQcow2State *s = bs->opaque;
45aba42f 747 int l2_index, ret;
e4e72548 748 uint64_t *l2_slice;
f4f0d391 749 int64_t cluster_offset;
45aba42f
KW
750 int nb_csectors;
751
966b000f
KW
752 if (has_data_file(bs)) {
753 return 0;
754 }
755
e4e72548 756 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1e3e8f1a 757 if (ret < 0) {
77e023ff 758 return ret;
1e3e8f1a 759 }
45aba42f 760
b0b6862e
KW
761 /* Compression can't overwrite anything. Fail if the cluster was already
762 * allocated. */
e4e72548 763 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
b0b6862e 764 if (cluster_offset & L2E_OFFSET_MASK) {
e4e72548 765 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
77e023ff 766 return -EIO;
8f1efd00 767 }
45aba42f 768
ed6ccf0f 769 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
5d757b56 770 if (cluster_offset < 0) {
e4e72548 771 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
77e023ff 772 return cluster_offset;
5d757b56
KW
773 }
774
b6c24694
AG
775 nb_csectors =
776 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
777 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
45aba42f 778
3a75a870
AG
779 /* The offset and size must fit in their fields of the L2 table entry */
780 assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
781 assert((nb_csectors & s->csize_mask) == nb_csectors);
782
45aba42f
KW
783 cluster_offset |= QCOW_OFLAG_COMPRESSED |
784 ((uint64_t)nb_csectors << s->csize_shift);
785
786 /* update L2 table */
787
788 /* compressed clusters never have the copied flag */
789
66f82cee 790 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
e4e72548
AG
791 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
792 l2_slice[l2_index] = cpu_to_be64(cluster_offset);
793 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
4c1612d9 794
77e023ff
KW
795 *host_offset = cluster_offset & s->cluster_offset_mask;
796 return 0;
4c1612d9
KW
797}
798
99450c6f 799static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
593fb83c 800{
ff99129a 801 BDRVQcow2State *s = bs->opaque;
99450c6f
AG
802 Qcow2COWRegion *start = &m->cow_start;
803 Qcow2COWRegion *end = &m->cow_end;
672f0f2c 804 unsigned buffer_size;
b3cf1c7c
AG
805 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
806 bool merge_reads;
672f0f2c 807 uint8_t *start_buffer, *end_buffer;
86b862c4 808 QEMUIOVector qiov;
593fb83c
KW
809 int ret;
810
672f0f2c 811 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
b3cf1c7c
AG
812 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
813 assert(start->offset + start->nb_bytes <= end->offset);
672f0f2c 814
c8bb23cb 815 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
593fb83c
KW
816 return 0;
817 }
818
b3cf1c7c
AG
819 /* If we have to read both the start and end COW regions and the
820 * middle region is not too large then perform just one read
821 * operation */
822 merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
823 if (merge_reads) {
824 buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
825 } else {
826 /* If we have to do two reads, add some padding in the middle
827 * if necessary to make sure that the end region is optimally
828 * aligned. */
829 size_t align = bdrv_opt_mem_align(bs);
830 assert(align > 0 && align <= UINT_MAX);
831 assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
832 UINT_MAX - end->nb_bytes);
833 buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
834 }
835
836 /* Reserve a buffer large enough to store all the data that we're
837 * going to read */
672f0f2c
AG
838 start_buffer = qemu_try_blockalign(bs, buffer_size);
839 if (start_buffer == NULL) {
840 return -ENOMEM;
841 }
842 /* The part of the buffer where the end region is located */
843 end_buffer = start_buffer + buffer_size - end->nb_bytes;
844
5396234b
VSO
845 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
846 qemu_iovec_subvec_niov(m->data_qiov,
847 m->data_qiov_offset,
848 data_bytes)
849 : 0));
86b862c4 850
593fb83c 851 qemu_co_mutex_unlock(&s->lock);
b3cf1c7c
AG
852 /* First we read the existing data from both COW regions. We
853 * either read the whole region in one go, or the start and end
854 * regions separately. */
855 if (merge_reads) {
86b862c4
AG
856 qemu_iovec_add(&qiov, start_buffer, buffer_size);
857 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
b3cf1c7c 858 } else {
86b862c4
AG
859 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
860 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
b3cf1c7c
AG
861 if (ret < 0) {
862 goto fail;
863 }
672f0f2c 864
86b862c4
AG
865 qemu_iovec_reset(&qiov);
866 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
867 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
b3cf1c7c 868 }
593fb83c 869 if (ret < 0) {
99450c6f 870 goto fail;
593fb83c
KW
871 }
872
672f0f2c
AG
873 /* Encrypt the data if necessary before writing it */
874 if (bs->encrypted) {
603fbd07
ML
875 ret = qcow2_co_encrypt(bs,
876 m->alloc_offset + start->offset,
877 m->offset + start->offset,
878 start_buffer, start->nb_bytes);
879 if (ret < 0) {
880 goto fail;
881 }
882
883 ret = qcow2_co_encrypt(bs,
884 m->alloc_offset + end->offset,
885 m->offset + end->offset,
886 end_buffer, end->nb_bytes);
887 if (ret < 0) {
672f0f2c
AG
888 goto fail;
889 }
890 }
891
ee22a9d8
AG
892 /* And now we can write everything. If we have the guest data we
893 * can write everything in one single operation */
894 if (m->data_qiov) {
895 qemu_iovec_reset(&qiov);
896 if (start->nb_bytes) {
897 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
898 }
5396234b 899 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
ee22a9d8
AG
900 if (end->nb_bytes) {
901 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
902 }
903 /* NOTE: we have a write_aio blkdebug event here followed by
904 * a cow_write one in do_perform_cow_write(), but there's only
905 * one single I/O operation */
906 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
907 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
908 } else {
909 /* If there's no guest data then write both COW regions separately */
910 qemu_iovec_reset(&qiov);
911 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
912 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
913 if (ret < 0) {
914 goto fail;
915 }
916
917 qemu_iovec_reset(&qiov);
918 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
919 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
672f0f2c 920 }
99450c6f
AG
921
922fail:
923 qemu_co_mutex_lock(&s->lock);
924
593fb83c
KW
925 /*
926 * Before we update the L2 table to actually point to the new cluster, we
927 * need to be sure that the refcounts have been increased and COW was
928 * handled.
929 */
99450c6f
AG
930 if (ret == 0) {
931 qcow2_cache_depends_on_flush(s->l2_table_cache);
932 }
593fb83c 933
672f0f2c 934 qemu_vfree(start_buffer);
86b862c4 935 qemu_iovec_destroy(&qiov);
99450c6f 936 return ret;
593fb83c
KW
937}
938
148da7ea 939int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
45aba42f 940{
ff99129a 941 BDRVQcow2State *s = bs->opaque;
45aba42f 942 int i, j = 0, l2_index, ret;
a002c0b0 943 uint64_t *old_cluster, *l2_slice;
250196f1 944 uint64_t cluster_offset = m->alloc_offset;
45aba42f 945
3cce16f4 946 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
f50f88b9 947 assert(m->nb_clusters > 0);
45aba42f 948
5839e53b 949 old_cluster = g_try_new(uint64_t, m->nb_clusters);
de82815d
KW
950 if (old_cluster == NULL) {
951 ret = -ENOMEM;
952 goto err;
953 }
45aba42f
KW
954
955 /* copy content of unmodified sectors */
99450c6f 956 ret = perform_cow(bs, m);
593fb83c
KW
957 if (ret < 0) {
958 goto err;
29c1a730
KW
959 }
960
593fb83c 961 /* Update L2 table. */
74c4510a 962 if (s->use_lazy_refcounts) {
280d3735
KW
963 qcow2_mark_dirty(bs);
964 }
bfe8043e
SH
965 if (qcow2_need_accurate_refcounts(s)) {
966 qcow2_cache_set_dependency(bs, s->l2_table_cache,
967 s->refcount_block_cache);
968 }
280d3735 969
a002c0b0 970 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
1e3e8f1a 971 if (ret < 0) {
45aba42f 972 goto err;
1e3e8f1a 973 }
a002c0b0 974 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
45aba42f 975
a002c0b0 976 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
45aba42f 977 for (i = 0; i < m->nb_clusters; i++) {
3a75a870 978 uint64_t offset = cluster_offset + (i << s->cluster_bits);
45aba42f 979 /* if two concurrent writes happen to the same unallocated cluster
aaa4d20b
KW
980 * each write allocates separate cluster and writes data concurrently.
981 * The first one to complete updates l2 table with pointer to its
982 * cluster the second one has to do RMW (which is done above by
983 * perform_cow()), update l2 table with its cluster pointer and free
984 * old cluster. This is what this loop does */
a002c0b0
AG
985 if (l2_slice[l2_index + i] != 0) {
986 old_cluster[j++] = l2_slice[l2_index + i];
aaa4d20b 987 }
45aba42f 988
3a75a870
AG
989 /* The offset must fit in the offset field of the L2 table entry */
990 assert((offset & L2E_OFFSET_MASK) == offset);
991
992 l2_slice[l2_index + i] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
45aba42f
KW
993 }
994
9f8e668e 995
a002c0b0 996 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
45aba42f 997
7ec5e6a4
KW
998 /*
999 * If this was a COW, we need to decrease the refcount of the old cluster.
6cfcb9b8
KW
1000 *
1001 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
1002 * clusters), the next write will reuse them anyway.
7ec5e6a4 1003 */
564a6b69 1004 if (!m->keep_old_clusters && j != 0) {
7ec5e6a4 1005 for (i = 0; i < j; i++) {
6cfcb9b8
KW
1006 qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1,
1007 QCOW2_DISCARD_NEVER);
7ec5e6a4
KW
1008 }
1009 }
45aba42f
KW
1010
1011 ret = 0;
1012err:
7267c094 1013 g_free(old_cluster);
45aba42f
KW
1014 return ret;
1015 }
1016
8b24cd14
KW
1017/**
1018 * Frees the allocated clusters because the request failed and they won't
1019 * actually be linked.
1020 */
1021void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
1022{
1023 BDRVQcow2State *s = bs->opaque;
1024 qcow2_free_clusters(bs, m->alloc_offset, m->nb_clusters << s->cluster_bits,
1025 QCOW2_DISCARD_NEVER);
1026}
1027
bf319ece
KW
1028/*
1029 * Returns the number of contiguous clusters that can be used for an allocating
1030 * write, but require COW to be performed (this includes yet unallocated space,
1031 * which must copy from the backing file)
1032 */
808c2bb4 1033static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,
dd32c881 1034 uint64_t *l2_slice, int l2_index)
bf319ece 1035{
143550a8 1036 int i;
bf319ece 1037
143550a8 1038 for (i = 0; i < nb_clusters; i++) {
dd32c881 1039 uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
808c2bb4 1040 QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
143550a8
KW
1041
1042 switch(cluster_type) {
1043 case QCOW2_CLUSTER_NORMAL:
1044 if (l2_entry & QCOW_OFLAG_COPIED) {
1045 goto out;
1046 }
bf319ece 1047 break;
143550a8
KW
1048 case QCOW2_CLUSTER_UNALLOCATED:
1049 case QCOW2_CLUSTER_COMPRESSED:
fdfab37d
EB
1050 case QCOW2_CLUSTER_ZERO_PLAIN:
1051 case QCOW2_CLUSTER_ZERO_ALLOC:
bf319ece 1052 break;
143550a8
KW
1053 default:
1054 abort();
1055 }
bf319ece
KW
1056 }
1057
143550a8 1058out:
bf319ece
KW
1059 assert(i <= nb_clusters);
1060 return i;
1061}
1062
250196f1 1063/*
226c3c26
KW
1064 * Check if there already is an AIO write request in flight which allocates
1065 * the same cluster. In this case we need to wait until the previous
1066 * request has completed and updated the L2 table accordingly.
65eb2e35
KW
1067 *
1068 * Returns:
1069 * 0 if there was no dependency. *cur_bytes indicates the number of
1070 * bytes from guest_offset that can be read before the next
1071 * dependency must be processed (or the request is complete)
1072 *
1073 * -EAGAIN if we had to wait for another request, previously gathered
1074 * information on cluster allocation may be invalid now. The caller
1075 * must start over anyway, so consider *cur_bytes undefined.
250196f1 1076 */
226c3c26 1077static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
ecdd5333 1078 uint64_t *cur_bytes, QCowL2Meta **m)
250196f1 1079{
ff99129a 1080 BDRVQcow2State *s = bs->opaque;
250196f1 1081 QCowL2Meta *old_alloc;
65eb2e35 1082 uint64_t bytes = *cur_bytes;
250196f1 1083
250196f1
KW
1084 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1085
65eb2e35
KW
1086 uint64_t start = guest_offset;
1087 uint64_t end = start + bytes;
1088 uint64_t old_start = l2meta_cow_start(old_alloc);
1089 uint64_t old_end = l2meta_cow_end(old_alloc);
250196f1 1090
d9d74f41 1091 if (end <= old_start || start >= old_end) {
250196f1
KW
1092 /* No intersection */
1093 } else {
1094 if (start < old_start) {
1095 /* Stop at the start of a running allocation */
65eb2e35 1096 bytes = old_start - start;
250196f1 1097 } else {
65eb2e35 1098 bytes = 0;
250196f1
KW
1099 }
1100
ecdd5333
KW
1101 /* Stop if already an l2meta exists. After yielding, it wouldn't
1102 * be valid any more, so we'd have to clean up the old L2Metas
1103 * and deal with requests depending on them before starting to
1104 * gather new ones. Not worth the trouble. */
1105 if (bytes == 0 && *m) {
1106 *cur_bytes = 0;
1107 return 0;
1108 }
1109
65eb2e35 1110 if (bytes == 0) {
250196f1
KW
1111 /* Wait for the dependency to complete. We need to recheck
1112 * the free/allocated clusters when we continue. */
1ace7cea 1113 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
250196f1
KW
1114 return -EAGAIN;
1115 }
1116 }
1117 }
1118
65eb2e35
KW
1119 /* Make sure that existing clusters and new allocations are only used up to
1120 * the next dependency if we shortened the request above */
1121 *cur_bytes = bytes;
250196f1 1122
226c3c26
KW
1123 return 0;
1124}
1125
0af729ec
KW
1126/*
1127 * Checks how many already allocated clusters that don't require a copy on
c6d619cc
KW
1128 * write there are at the given guest_offset (up to *bytes). If *host_offset is
1129 * not INV_OFFSET, only physically contiguous clusters beginning at this host
1130 * offset are counted.
0af729ec 1131 *
411d62b0
KW
1132 * Note that guest_offset may not be cluster aligned. In this case, the
1133 * returned *host_offset points to exact byte referenced by guest_offset and
1134 * therefore isn't cluster aligned as well.
0af729ec
KW
1135 *
1136 * Returns:
1137 * 0: if no allocated clusters are available at the given offset.
1138 * *bytes is normally unchanged. It is set to 0 if the cluster
1139 * is allocated and doesn't need COW, but doesn't have the right
1140 * physical offset.
1141 *
1142 * 1: if allocated clusters that don't require a COW are available at
1143 * the requested offset. *bytes may have decreased and describes
1144 * the length of the area that can be written to.
1145 *
1146 * -errno: in error cases
0af729ec
KW
1147 */
1148static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
c53ede9f 1149 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
0af729ec 1150{
ff99129a 1151 BDRVQcow2State *s = bs->opaque;
0af729ec
KW
1152 int l2_index;
1153 uint64_t cluster_offset;
cde91766 1154 uint64_t *l2_slice;
b6d36def 1155 uint64_t nb_clusters;
c53ede9f 1156 unsigned int keep_clusters;
a3f1afb4 1157 int ret;
0af729ec
KW
1158
1159 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1160 *bytes);
0af729ec 1161
c6d619cc
KW
1162 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1163 == offset_into_cluster(s, *host_offset));
411d62b0 1164
acb0467f 1165 /*
cde91766 1166 * Calculate the number of clusters to look for. We stop at L2 slice
acb0467f
KW
1167 * boundaries to keep things simple.
1168 */
1169 nb_clusters =
1170 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1171
cde91766
AG
1172 l2_index = offset_to_l2_slice_index(s, guest_offset);
1173 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1174 assert(nb_clusters <= INT_MAX);
acb0467f 1175
0af729ec 1176 /* Find L2 entry for the first involved cluster */
cde91766 1177 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
0af729ec
KW
1178 if (ret < 0) {
1179 return ret;
1180 }
1181
cde91766 1182 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
0af729ec
KW
1183
1184 /* Check how many clusters are already allocated and don't need COW */
808c2bb4 1185 if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL
0af729ec
KW
1186 && (cluster_offset & QCOW_OFLAG_COPIED))
1187 {
e62daaf6
KW
1188 /* If a specific host_offset is required, check it */
1189 bool offset_matches =
1190 (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
1191
a97c67ee
HR
1192 if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {
1193 qcow2_signal_corruption(bs, true, -1, -1, "Data cluster offset "
1194 "%#llx unaligned (guest offset: %#" PRIx64
1195 ")", cluster_offset & L2E_OFFSET_MASK,
1196 guest_offset);
1197 ret = -EIO;
1198 goto out;
1199 }
1200
c6d619cc 1201 if (*host_offset != INV_OFFSET && !offset_matches) {
e62daaf6
KW
1202 *bytes = 0;
1203 ret = 0;
1204 goto out;
1205 }
1206
0af729ec 1207 /* We keep all QCOW_OFLAG_COPIED clusters */
c53ede9f 1208 keep_clusters =
808c2bb4 1209 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
cde91766 1210 &l2_slice[l2_index],
0af729ec 1211 QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
c53ede9f
KW
1212 assert(keep_clusters <= nb_clusters);
1213
1214 *bytes = MIN(*bytes,
1215 keep_clusters * s->cluster_size
1216 - offset_into_cluster(s, guest_offset));
0af729ec
KW
1217
1218 ret = 1;
1219 } else {
0af729ec
KW
1220 ret = 0;
1221 }
1222
0af729ec 1223 /* Cleanup */
e62daaf6 1224out:
cde91766 1225 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
0af729ec 1226
e62daaf6
KW
1227 /* Only return a host offset if we actually made progress. Otherwise we
1228 * would make requirements for handle_alloc() that it can't fulfill */
a97c67ee 1229 if (ret > 0) {
411d62b0
KW
1230 *host_offset = (cluster_offset & L2E_OFFSET_MASK)
1231 + offset_into_cluster(s, guest_offset);
e62daaf6
KW
1232 }
1233
0af729ec
KW
1234 return ret;
1235}
1236
226c3c26
KW
1237/*
1238 * Allocates new clusters for the given guest_offset.
1239 *
1240 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1241 * contain the number of clusters that have been allocated and are contiguous
1242 * in the image file.
1243 *
c6d619cc
KW
1244 * If *host_offset is not INV_OFFSET, it specifies the offset in the image file
1245 * at which the new clusters must start. *nb_clusters can be 0 on return in
1246 * this case if the cluster at host_offset is already in use. If *host_offset
1247 * is INV_OFFSET, the clusters can be allocated anywhere in the image file.
226c3c26
KW
1248 *
1249 * *host_offset is updated to contain the offset into the image file at which
1250 * the first allocated cluster starts.
1251 *
1252 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1253 * function has been waiting for another request and the allocation must be
1254 * restarted, but the whole request should not be failed.
1255 */
1256static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
b6d36def 1257 uint64_t *host_offset, uint64_t *nb_clusters)
226c3c26 1258{
ff99129a 1259 BDRVQcow2State *s = bs->opaque;
226c3c26
KW
1260
1261 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1262 *host_offset, *nb_clusters);
1263
966b000f
KW
1264 if (has_data_file(bs)) {
1265 assert(*host_offset == INV_OFFSET ||
1266 *host_offset == start_of_cluster(s, guest_offset));
1267 *host_offset = start_of_cluster(s, guest_offset);
1268 return 0;
1269 }
1270
250196f1
KW
1271 /* Allocate new clusters */
1272 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
c6d619cc 1273 if (*host_offset == INV_OFFSET) {
df021791
KW
1274 int64_t cluster_offset =
1275 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1276 if (cluster_offset < 0) {
1277 return cluster_offset;
1278 }
1279 *host_offset = cluster_offset;
1280 return 0;
250196f1 1281 } else {
b6d36def 1282 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
df021791
KW
1283 if (ret < 0) {
1284 return ret;
1285 }
1286 *nb_clusters = ret;
1287 return 0;
250196f1 1288 }
250196f1
KW
1289}
1290
10f0ed8b
KW
1291/*
1292 * Allocates new clusters for an area that either is yet unallocated or needs a
c6d619cc
KW
1293 * copy on write. If *host_offset is not INV_OFFSET, clusters are only
1294 * allocated if the new allocation can match the specified host offset.
10f0ed8b 1295 *
411d62b0
KW
1296 * Note that guest_offset may not be cluster aligned. In this case, the
1297 * returned *host_offset points to exact byte referenced by guest_offset and
1298 * therefore isn't cluster aligned as well.
10f0ed8b
KW
1299 *
1300 * Returns:
1301 * 0: if no clusters could be allocated. *bytes is set to 0,
1302 * *host_offset is left unchanged.
1303 *
1304 * 1: if new clusters were allocated. *bytes may be decreased if the
1305 * new allocation doesn't cover all of the requested area.
1306 * *host_offset is updated to contain the host offset of the first
1307 * newly allocated cluster.
1308 *
1309 * -errno: in error cases
10f0ed8b
KW
1310 */
1311static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
c37f4cd7 1312 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
10f0ed8b 1313{
ff99129a 1314 BDRVQcow2State *s = bs->opaque;
10f0ed8b 1315 int l2_index;
6d99a344 1316 uint64_t *l2_slice;
10f0ed8b 1317 uint64_t entry;
b6d36def 1318 uint64_t nb_clusters;
10f0ed8b 1319 int ret;
564a6b69 1320 bool keep_old_clusters = false;
10f0ed8b 1321
c6d619cc 1322 uint64_t alloc_cluster_offset = INV_OFFSET;
10f0ed8b
KW
1323
1324 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1325 *bytes);
1326 assert(*bytes > 0);
1327
f5bc6350 1328 /*
6d99a344 1329 * Calculate the number of clusters to look for. We stop at L2 slice
f5bc6350
KW
1330 * boundaries to keep things simple.
1331 */
c37f4cd7
KW
1332 nb_clusters =
1333 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1334
6d99a344
AG
1335 l2_index = offset_to_l2_slice_index(s, guest_offset);
1336 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1337 assert(nb_clusters <= INT_MAX);
f5bc6350 1338
d1b9d19f
HR
1339 /* Limit total allocation byte count to INT_MAX */
1340 nb_clusters = MIN(nb_clusters, INT_MAX >> s->cluster_bits);
1341
10f0ed8b 1342 /* Find L2 entry for the first involved cluster */
6d99a344 1343 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
10f0ed8b
KW
1344 if (ret < 0) {
1345 return ret;
1346 }
1347
6d99a344 1348 entry = be64_to_cpu(l2_slice[l2_index]);
bf3d78ae 1349 nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
10f0ed8b 1350
ecdd5333
KW
1351 /* This function is only called when there were no non-COW clusters, so if
1352 * we can't find any unallocated or COW clusters either, something is
1353 * wrong with our code. */
1354 assert(nb_clusters > 0);
1355
808c2bb4 1356 if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&
fdfab37d 1357 (entry & QCOW_OFLAG_COPIED) &&
c6d619cc 1358 (*host_offset == INV_OFFSET ||
564a6b69
HR
1359 start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))
1360 {
93bbaf03
HR
1361 int preallocated_nb_clusters;
1362
1363 if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {
1364 qcow2_signal_corruption(bs, true, -1, -1, "Preallocated zero "
1365 "cluster offset %#llx unaligned (guest "
1366 "offset: %#" PRIx64 ")",
1367 entry & L2E_OFFSET_MASK, guest_offset);
1368 ret = -EIO;
1369 goto fail;
1370 }
1371
564a6b69
HR
1372 /* Try to reuse preallocated zero clusters; contiguous normal clusters
1373 * would be fine, too, but count_cow_clusters() above has limited
1374 * nb_clusters already to a range of COW clusters */
93bbaf03 1375 preallocated_nb_clusters =
808c2bb4 1376 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
6d99a344 1377 &l2_slice[l2_index], QCOW_OFLAG_COPIED);
564a6b69 1378 assert(preallocated_nb_clusters > 0);
10f0ed8b 1379
564a6b69
HR
1380 nb_clusters = preallocated_nb_clusters;
1381 alloc_cluster_offset = entry & L2E_OFFSET_MASK;
10f0ed8b 1382
564a6b69
HR
1383 /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()
1384 * should not free them. */
1385 keep_old_clusters = true;
10f0ed8b
KW
1386 }
1387
6d99a344 1388 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
564a6b69 1389
c6d619cc 1390 if (alloc_cluster_offset == INV_OFFSET) {
564a6b69 1391 /* Allocate, if necessary at a given offset in the image file */
c6d619cc
KW
1392 alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1393 start_of_cluster(s, *host_offset);
564a6b69
HR
1394 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1395 &nb_clusters);
1396 if (ret < 0) {
1397 goto fail;
1398 }
1399
1400 /* Can't extend contiguous allocation */
1401 if (nb_clusters == 0) {
1402 *bytes = 0;
1403 return 0;
1404 }
1405
c6d619cc 1406 assert(alloc_cluster_offset != INV_OFFSET);
ff52aab2
HR
1407 }
1408
83baa9a4
KW
1409 /*
1410 * Save info needed for meta data update.
1411 *
85567393 1412 * requested_bytes: Number of bytes from the start of the first
83baa9a4
KW
1413 * newly allocated cluster to the end of the (possibly shortened
1414 * before) write request.
1415 *
85567393 1416 * avail_bytes: Number of bytes from the start of the first
83baa9a4
KW
1417 * newly allocated to the end of the last newly allocated cluster.
1418 *
85567393 1419 * nb_bytes: The number of bytes from the start of the first
83baa9a4
KW
1420 * newly allocated cluster to the end of the area that the write
1421 * request actually writes to (excluding COW at the end)
1422 */
85567393 1423 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
d1b9d19f 1424 int avail_bytes = nb_clusters << s->cluster_bits;
85567393 1425 int nb_bytes = MIN(requested_bytes, avail_bytes);
88c6588c 1426 QCowL2Meta *old_m = *m;
83baa9a4 1427
83baa9a4
KW
1428 *m = g_malloc0(sizeof(**m));
1429
1430 **m = (QCowL2Meta) {
88c6588c
KW
1431 .next = old_m,
1432
411d62b0 1433 .alloc_offset = alloc_cluster_offset,
83baa9a4
KW
1434 .offset = start_of_cluster(s, guest_offset),
1435 .nb_clusters = nb_clusters,
83baa9a4 1436
564a6b69
HR
1437 .keep_old_clusters = keep_old_clusters,
1438
83baa9a4
KW
1439 .cow_start = {
1440 .offset = 0,
85567393 1441 .nb_bytes = offset_into_cluster(s, guest_offset),
83baa9a4
KW
1442 },
1443 .cow_end = {
85567393
KW
1444 .offset = nb_bytes,
1445 .nb_bytes = avail_bytes - nb_bytes,
83baa9a4
KW
1446 },
1447 };
1448 qemu_co_queue_init(&(*m)->dependent_requests);
1449 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
1450
411d62b0 1451 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
85567393 1452 *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
83baa9a4
KW
1453 assert(*bytes != 0);
1454
10f0ed8b
KW
1455 return 1;
1456
1457fail:
1458 if (*m && (*m)->nb_clusters > 0) {
1459 QLIST_REMOVE(*m, next_in_flight);
1460 }
1461 return ret;
1462}
1463
45aba42f
KW
1464/*
1465 * alloc_cluster_offset
1466 *
250196f1
KW
1467 * For a given offset on the virtual disk, find the cluster offset in qcow2
1468 * file. If the offset is not found, allocate a new cluster.
45aba42f 1469 *
250196f1 1470 * If the cluster was already allocated, m->nb_clusters is set to 0 and
a7912369 1471 * other fields in m are meaningless.
148da7ea
KW
1472 *
1473 * If the cluster is newly allocated, m->nb_clusters is set to the number of
68d100e9
KW
1474 * contiguous clusters that have been allocated. In this case, the other
1475 * fields of m are valid and contain information about the first allocated
1476 * cluster.
45aba42f 1477 *
68d100e9
KW
1478 * If the request conflicts with another write request in flight, the coroutine
1479 * is queued and will be reentered when the dependency has completed.
148da7ea
KW
1480 *
1481 * Return 0 on success and -errno in error cases
45aba42f 1482 */
f4f0d391 1483int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
d46a0bb2
KW
1484 unsigned int *bytes, uint64_t *host_offset,
1485 QCowL2Meta **m)
45aba42f 1486{
ff99129a 1487 BDRVQcow2State *s = bs->opaque;
710c2496 1488 uint64_t start, remaining;
250196f1 1489 uint64_t cluster_offset;
65eb2e35 1490 uint64_t cur_bytes;
710c2496 1491 int ret;
45aba42f 1492
d46a0bb2 1493 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
710c2496 1494
72424114 1495again:
16f0587e 1496 start = offset;
d46a0bb2 1497 remaining = *bytes;
c6d619cc
KW
1498 cluster_offset = INV_OFFSET;
1499 *host_offset = INV_OFFSET;
ecdd5333
KW
1500 cur_bytes = 0;
1501 *m = NULL;
0af729ec 1502
2c3b32d2 1503 while (true) {
ecdd5333 1504
c6d619cc 1505 if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
ecdd5333
KW
1506 *host_offset = start_of_cluster(s, cluster_offset);
1507 }
1508
1509 assert(remaining >= cur_bytes);
1510
1511 start += cur_bytes;
1512 remaining -= cur_bytes;
c6d619cc
KW
1513
1514 if (cluster_offset != INV_OFFSET) {
1515 cluster_offset += cur_bytes;
1516 }
ecdd5333
KW
1517
1518 if (remaining == 0) {
1519 break;
1520 }
1521
1522 cur_bytes = remaining;
1523
2c3b32d2
KW
1524 /*
1525 * Now start gathering as many contiguous clusters as possible:
1526 *
1527 * 1. Check for overlaps with in-flight allocations
1528 *
1529 * a) Overlap not in the first cluster -> shorten this request and
1530 * let the caller handle the rest in its next loop iteration.
1531 *
1532 * b) Real overlaps of two requests. Yield and restart the search
1533 * for contiguous clusters (the situation could have changed
1534 * while we were sleeping)
1535 *
1536 * c) TODO: Request starts in the same cluster as the in-flight
1537 * allocation ends. Shorten the COW of the in-fight allocation,
1538 * set cluster_offset to write to the same cluster and set up
1539 * the right synchronisation between the in-flight request and
1540 * the new one.
1541 */
ecdd5333 1542 ret = handle_dependencies(bs, start, &cur_bytes, m);
2c3b32d2 1543 if (ret == -EAGAIN) {
ecdd5333
KW
1544 /* Currently handle_dependencies() doesn't yield if we already had
1545 * an allocation. If it did, we would have to clean up the L2Meta
1546 * structs before starting over. */
1547 assert(*m == NULL);
2c3b32d2
KW
1548 goto again;
1549 } else if (ret < 0) {
1550 return ret;
ecdd5333
KW
1551 } else if (cur_bytes == 0) {
1552 break;
2c3b32d2
KW
1553 } else {
1554 /* handle_dependencies() may have decreased cur_bytes (shortened
1555 * the allocations below) so that the next dependency is processed
1556 * correctly during the next loop iteration. */
0af729ec 1557 }
710c2496 1558
2c3b32d2
KW
1559 /*
1560 * 2. Count contiguous COPIED clusters.
1561 */
1562 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1563 if (ret < 0) {
1564 return ret;
1565 } else if (ret) {
ecdd5333 1566 continue;
2c3b32d2
KW
1567 } else if (cur_bytes == 0) {
1568 break;
1569 }
060bee89 1570
2c3b32d2
KW
1571 /*
1572 * 3. If the request still hasn't completed, allocate new clusters,
1573 * considering any cluster_offset of steps 1c or 2.
1574 */
1575 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1576 if (ret < 0) {
1577 return ret;
1578 } else if (ret) {
ecdd5333 1579 continue;
2c3b32d2
KW
1580 } else {
1581 assert(cur_bytes == 0);
1582 break;
1583 }
f5bc6350 1584 }
10f0ed8b 1585
d46a0bb2
KW
1586 *bytes -= remaining;
1587 assert(*bytes > 0);
c6d619cc 1588 assert(*host_offset != INV_OFFSET);
45aba42f 1589
148da7ea 1590 return 0;
45aba42f
KW
1591}
1592
5ea929e3
KW
1593/*
1594 * This discards as many clusters of nb_clusters as possible at once (i.e.
21ab3add 1595 * all clusters in the same L2 slice) and returns the number of discarded
5ea929e3
KW
1596 * clusters.
1597 */
21ab3add
AG
1598static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1599 uint64_t nb_clusters,
1600 enum qcow2_discard_type type, bool full_discard)
5ea929e3 1601{
ff99129a 1602 BDRVQcow2State *s = bs->opaque;
21ab3add 1603 uint64_t *l2_slice;
5ea929e3
KW
1604 int l2_index;
1605 int ret;
1606 int i;
1607
21ab3add 1608 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
5ea929e3
KW
1609 if (ret < 0) {
1610 return ret;
1611 }
1612
21ab3add
AG
1613 /* Limit nb_clusters to one L2 slice */
1614 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1615 assert(nb_clusters <= INT_MAX);
5ea929e3
KW
1616
1617 for (i = 0; i < nb_clusters; i++) {
c883db0d 1618 uint64_t old_l2_entry;
5ea929e3 1619
21ab3add 1620 old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
a71835a0
KW
1621
1622 /*
808c4b6f
HR
1623 * If full_discard is false, make sure that a discarded area reads back
1624 * as zeroes for v3 images (we cannot do it for v2 without actually
1625 * writing a zero-filled buffer). We can skip the operation if the
1626 * cluster is already marked as zero, or if it's unallocated and we
1627 * don't have a backing file.
a71835a0 1628 *
237d78f8 1629 * TODO We might want to use bdrv_block_status(bs) here, but we're
a71835a0 1630 * holding s->lock, so that doesn't work today.
808c4b6f
HR
1631 *
1632 * If full_discard is true, the sector should not read back as zeroes,
1633 * but rather fall through to the backing file.
a71835a0 1634 */
808c2bb4 1635 switch (qcow2_get_cluster_type(bs, old_l2_entry)) {
bbd995d8
EB
1636 case QCOW2_CLUSTER_UNALLOCATED:
1637 if (full_discard || !bs->backing) {
1638 continue;
1639 }
1640 break;
1641
fdfab37d
EB
1642 case QCOW2_CLUSTER_ZERO_PLAIN:
1643 if (!full_discard) {
bbd995d8
EB
1644 continue;
1645 }
1646 break;
1647
fdfab37d 1648 case QCOW2_CLUSTER_ZERO_ALLOC:
bbd995d8
EB
1649 case QCOW2_CLUSTER_NORMAL:
1650 case QCOW2_CLUSTER_COMPRESSED:
1651 break;
1652
1653 default:
1654 abort();
5ea929e3
KW
1655 }
1656
1657 /* First remove L2 entries */
21ab3add 1658 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
808c4b6f 1659 if (!full_discard && s->qcow_version >= 3) {
21ab3add 1660 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
a71835a0 1661 } else {
21ab3add 1662 l2_slice[l2_index + i] = cpu_to_be64(0);
a71835a0 1663 }
5ea929e3
KW
1664
1665 /* Then decrease the refcount */
c883db0d 1666 qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
5ea929e3
KW
1667 }
1668
21ab3add 1669 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
5ea929e3
KW
1670
1671 return nb_clusters;
1672}
1673
d2cb36af
EB
1674int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1675 uint64_t bytes, enum qcow2_discard_type type,
1676 bool full_discard)
5ea929e3 1677{
ff99129a 1678 BDRVQcow2State *s = bs->opaque;
d2cb36af 1679 uint64_t end_offset = offset + bytes;
b6d36def 1680 uint64_t nb_clusters;
d2cb36af 1681 int64_t cleared;
5ea929e3
KW
1682 int ret;
1683
f10ee139 1684 /* Caller must pass aligned values, except at image end */
0c1bd469 1685 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
f10ee139
EB
1686 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1687 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
5ea929e3 1688
d2cb36af 1689 nb_clusters = size_to_clusters(s, bytes);
5ea929e3 1690
0b919fae
KW
1691 s->cache_discards = true;
1692
21ab3add 1693 /* Each L2 slice is handled by its own loop iteration */
5ea929e3 1694 while (nb_clusters > 0) {
21ab3add
AG
1695 cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
1696 full_discard);
d2cb36af
EB
1697 if (cleared < 0) {
1698 ret = cleared;
0b919fae 1699 goto fail;
5ea929e3
KW
1700 }
1701
d2cb36af
EB
1702 nb_clusters -= cleared;
1703 offset += (cleared * s->cluster_size);
5ea929e3
KW
1704 }
1705
0b919fae
KW
1706 ret = 0;
1707fail:
1708 s->cache_discards = false;
1709 qcow2_process_discards(bs, ret);
1710
1711 return ret;
5ea929e3 1712}
621f0589
KW
1713
1714/*
1715 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
a9a9f8f0 1716 * all clusters in the same L2 slice) and returns the number of zeroed
621f0589
KW
1717 * clusters.
1718 */
a9a9f8f0
AG
1719static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1720 uint64_t nb_clusters, int flags)
621f0589 1721{
ff99129a 1722 BDRVQcow2State *s = bs->opaque;
a9a9f8f0 1723 uint64_t *l2_slice;
621f0589
KW
1724 int l2_index;
1725 int ret;
1726 int i;
06cc5e2b 1727 bool unmap = !!(flags & BDRV_REQ_MAY_UNMAP);
621f0589 1728
a9a9f8f0 1729 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
621f0589
KW
1730 if (ret < 0) {
1731 return ret;
1732 }
1733
a9a9f8f0
AG
1734 /* Limit nb_clusters to one L2 slice */
1735 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1736 assert(nb_clusters <= INT_MAX);
621f0589
KW
1737
1738 for (i = 0; i < nb_clusters; i++) {
1739 uint64_t old_offset;
06cc5e2b 1740 QCow2ClusterType cluster_type;
621f0589 1741
a9a9f8f0 1742 old_offset = be64_to_cpu(l2_slice[l2_index + i]);
621f0589 1743
06cc5e2b
EB
1744 /*
1745 * Minimize L2 changes if the cluster already reads back as
1746 * zeroes with correct allocation.
1747 */
808c2bb4 1748 cluster_type = qcow2_get_cluster_type(bs, old_offset);
06cc5e2b
EB
1749 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN ||
1750 (cluster_type == QCOW2_CLUSTER_ZERO_ALLOC && !unmap)) {
1751 continue;
1752 }
1753
a9a9f8f0 1754 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
06cc5e2b 1755 if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) {
a9a9f8f0 1756 l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
6cfcb9b8 1757 qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
621f0589 1758 } else {
a9a9f8f0 1759 l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO);
621f0589
KW
1760 }
1761 }
1762
a9a9f8f0 1763 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
621f0589
KW
1764
1765 return nb_clusters;
1766}
1767
d2cb36af
EB
1768int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
1769 uint64_t bytes, int flags)
621f0589 1770{
ff99129a 1771 BDRVQcow2State *s = bs->opaque;
d2cb36af 1772 uint64_t end_offset = offset + bytes;
b6d36def 1773 uint64_t nb_clusters;
d2cb36af 1774 int64_t cleared;
621f0589
KW
1775 int ret;
1776
6c3944dc
KW
1777 /* If we have to stay in sync with an external data file, zero out
1778 * s->data_file first. */
1779 if (data_file_is_raw(bs)) {
1780 assert(has_data_file(bs));
1781 ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
1782 if (ret < 0) {
1783 return ret;
1784 }
1785 }
1786
f10ee139
EB
1787 /* Caller must pass aligned values, except at image end */
1788 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
1789 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
1790 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
1791
621f0589
KW
1792 /* The zero flag is only supported by version 3 and newer */
1793 if (s->qcow_version < 3) {
1794 return -ENOTSUP;
1795 }
1796
a9a9f8f0 1797 /* Each L2 slice is handled by its own loop iteration */
d2cb36af 1798 nb_clusters = size_to_clusters(s, bytes);
621f0589 1799
0b919fae
KW
1800 s->cache_discards = true;
1801
621f0589 1802 while (nb_clusters > 0) {
a9a9f8f0 1803 cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
d2cb36af
EB
1804 if (cleared < 0) {
1805 ret = cleared;
0b919fae 1806 goto fail;
621f0589
KW
1807 }
1808
d2cb36af
EB
1809 nb_clusters -= cleared;
1810 offset += (cleared * s->cluster_size);
621f0589
KW
1811 }
1812
0b919fae
KW
1813 ret = 0;
1814fail:
1815 s->cache_discards = false;
1816 qcow2_process_discards(bs, ret);
1817
1818 return ret;
621f0589 1819}
32b6444d
HR
1820
1821/*
1822 * Expands all zero clusters in a specific L1 table (or deallocates them, for
1823 * non-backed non-pre-allocated zero clusters).
1824 *
4057a2b2
HR
1825 * l1_entries and *visited_l1_entries are used to keep track of progress for
1826 * status_cb(). l1_entries contains the total number of L1 entries and
1827 * *visited_l1_entries counts all visited L1 entries.
32b6444d
HR
1828 */
1829static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
ecf58777 1830 int l1_size, int64_t *visited_l1_entries,
4057a2b2 1831 int64_t l1_entries,
8b13976d
HR
1832 BlockDriverAmendStatusCB *status_cb,
1833 void *cb_opaque)
32b6444d 1834{
ff99129a 1835 BDRVQcow2State *s = bs->opaque;
32b6444d 1836 bool is_active_l1 = (l1_table == s->l1_table);
415184f5
AG
1837 uint64_t *l2_slice = NULL;
1838 unsigned slice, slice_size2, n_slices;
32b6444d
HR
1839 int ret;
1840 int i, j;
1841
415184f5
AG
1842 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1843 n_slices = s->cluster_size / slice_size2;
1844
32b6444d
HR
1845 if (!is_active_l1) {
1846 /* inactive L2 tables require a buffer to be stored in when loading
1847 * them from disk */
415184f5
AG
1848 l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
1849 if (l2_slice == NULL) {
de82815d
KW
1850 return -ENOMEM;
1851 }
32b6444d
HR
1852 }
1853
1854 for (i = 0; i < l1_size; i++) {
1855 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
0e06528e 1856 uint64_t l2_refcount;
32b6444d
HR
1857
1858 if (!l2_offset) {
1859 /* unallocated */
4057a2b2
HR
1860 (*visited_l1_entries)++;
1861 if (status_cb) {
8b13976d 1862 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
4057a2b2 1863 }
32b6444d
HR
1864 continue;
1865 }
1866
8dd93d93
HR
1867 if (offset_into_cluster(s, l2_offset)) {
1868 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1869 PRIx64 " unaligned (L1 index: %#x)",
1870 l2_offset, i);
1871 ret = -EIO;
1872 goto fail;
1873 }
1874
9b765486
AG
1875 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1876 &l2_refcount);
1877 if (ret < 0) {
1878 goto fail;
1879 }
1880
415184f5
AG
1881 for (slice = 0; slice < n_slices; slice++) {
1882 uint64_t slice_offset = l2_offset + slice * slice_size2;
1883 bool l2_dirty = false;
226494ff
AG
1884 if (is_active_l1) {
1885 /* get active L2 tables from cache */
415184f5
AG
1886 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
1887 (void **)&l2_slice);
226494ff
AG
1888 } else {
1889 /* load inactive L2 tables from disk */
415184f5 1890 ret = bdrv_pread(bs->file, slice_offset, l2_slice, slice_size2);
226494ff
AG
1891 }
1892 if (ret < 0) {
1893 goto fail;
32b6444d
HR
1894 }
1895
415184f5
AG
1896 for (j = 0; j < s->l2_slice_size; j++) {
1897 uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
226494ff
AG
1898 int64_t offset = l2_entry & L2E_OFFSET_MASK;
1899 QCow2ClusterType cluster_type =
808c2bb4 1900 qcow2_get_cluster_type(bs, l2_entry);
226494ff
AG
1901
1902 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
1903 cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
32b6444d
HR
1904 continue;
1905 }
1906
226494ff
AG
1907 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1908 if (!bs->backing) {
1909 /* not backed; therefore we can simply deallocate the
1910 * cluster */
415184f5 1911 l2_slice[j] = 0;
226494ff
AG
1912 l2_dirty = true;
1913 continue;
1914 }
1915
1916 offset = qcow2_alloc_clusters(bs, s->cluster_size);
1917 if (offset < 0) {
1918 ret = offset;
1919 goto fail;
1920 }
ecf58777 1921
3a75a870
AG
1922 /* The offset must fit in the offset field */
1923 assert((offset & L2E_OFFSET_MASK) == offset);
1924
226494ff
AG
1925 if (l2_refcount > 1) {
1926 /* For shared L2 tables, set the refcount accordingly
1927 * (it is already 1 and needs to be l2_refcount) */
1928 ret = qcow2_update_cluster_refcount(
1929 bs, offset >> s->cluster_bits,
2aabe7c7 1930 refcount_diff(1, l2_refcount), false,
ecf58777 1931 QCOW2_DISCARD_OTHER);
226494ff
AG
1932 if (ret < 0) {
1933 qcow2_free_clusters(bs, offset, s->cluster_size,
1934 QCOW2_DISCARD_OTHER);
1935 goto fail;
1936 }
ecf58777
HR
1937 }
1938 }
32b6444d 1939
226494ff 1940 if (offset_into_cluster(s, offset)) {
415184f5 1941 int l2_index = slice * s->l2_slice_size + j;
226494ff
AG
1942 qcow2_signal_corruption(
1943 bs, true, -1, -1,
1944 "Cluster allocation offset "
1945 "%#" PRIx64 " unaligned (L2 offset: %#"
1946 PRIx64 ", L2 index: %#x)", offset,
415184f5 1947 l2_offset, l2_index);
226494ff
AG
1948 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1949 qcow2_free_clusters(bs, offset, s->cluster_size,
1950 QCOW2_DISCARD_ALWAYS);
1951 }
1952 ret = -EIO;
1953 goto fail;
8dd93d93 1954 }
8dd93d93 1955
226494ff 1956 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
966b000f 1957 s->cluster_size, true);
226494ff
AG
1958 if (ret < 0) {
1959 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1960 qcow2_free_clusters(bs, offset, s->cluster_size,
1961 QCOW2_DISCARD_ALWAYS);
1962 }
1963 goto fail;
320c7066 1964 }
32b6444d 1965
966b000f
KW
1966 ret = bdrv_pwrite_zeroes(s->data_file, offset,
1967 s->cluster_size, 0);
226494ff
AG
1968 if (ret < 0) {
1969 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1970 qcow2_free_clusters(bs, offset, s->cluster_size,
1971 QCOW2_DISCARD_ALWAYS);
1972 }
1973 goto fail;
320c7066 1974 }
32b6444d 1975
226494ff 1976 if (l2_refcount == 1) {
415184f5 1977 l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
226494ff 1978 } else {
415184f5 1979 l2_slice[j] = cpu_to_be64(offset);
226494ff
AG
1980 }
1981 l2_dirty = true;
e390cf5a 1982 }
32b6444d 1983
226494ff
AG
1984 if (is_active_l1) {
1985 if (l2_dirty) {
415184f5 1986 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
226494ff 1987 qcow2_cache_depends_on_flush(s->l2_table_cache);
32b6444d 1988 }
415184f5 1989 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
226494ff
AG
1990 } else {
1991 if (l2_dirty) {
1992 ret = qcow2_pre_write_overlap_check(
1993 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
966b000f 1994 slice_offset, slice_size2, false);
226494ff
AG
1995 if (ret < 0) {
1996 goto fail;
1997 }
32b6444d 1998
415184f5
AG
1999 ret = bdrv_pwrite(bs->file, slice_offset,
2000 l2_slice, slice_size2);
226494ff
AG
2001 if (ret < 0) {
2002 goto fail;
2003 }
32b6444d
HR
2004 }
2005 }
2006 }
4057a2b2
HR
2007
2008 (*visited_l1_entries)++;
2009 if (status_cb) {
8b13976d 2010 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
4057a2b2 2011 }
32b6444d
HR
2012 }
2013
2014 ret = 0;
2015
2016fail:
415184f5 2017 if (l2_slice) {
32b6444d 2018 if (!is_active_l1) {
415184f5 2019 qemu_vfree(l2_slice);
32b6444d 2020 } else {
415184f5 2021 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
32b6444d
HR
2022 }
2023 }
2024 return ret;
2025}
2026
2027/*
2028 * For backed images, expands all zero clusters on the image. For non-backed
2029 * images, deallocates all non-pre-allocated zero clusters (and claims the
2030 * allocation for pre-allocated ones). This is important for downgrading to a
2031 * qcow2 version which doesn't yet support metadata zero clusters.
2032 */
4057a2b2 2033int qcow2_expand_zero_clusters(BlockDriverState *bs,
8b13976d
HR
2034 BlockDriverAmendStatusCB *status_cb,
2035 void *cb_opaque)
32b6444d 2036{
ff99129a 2037 BDRVQcow2State *s = bs->opaque;
32b6444d 2038 uint64_t *l1_table = NULL;
4057a2b2 2039 int64_t l1_entries = 0, visited_l1_entries = 0;
32b6444d
HR
2040 int ret;
2041 int i, j;
2042
4057a2b2
HR
2043 if (status_cb) {
2044 l1_entries = s->l1_size;
2045 for (i = 0; i < s->nb_snapshots; i++) {
2046 l1_entries += s->snapshots[i].l1_size;
2047 }
2048 }
2049
32b6444d 2050 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
4057a2b2 2051 &visited_l1_entries, l1_entries,
8b13976d 2052 status_cb, cb_opaque);
32b6444d
HR
2053 if (ret < 0) {
2054 goto fail;
2055 }
2056
2057 /* Inactive L1 tables may point to active L2 tables - therefore it is
2058 * necessary to flush the L2 table cache before trying to access the L2
2059 * tables pointed to by inactive L1 entries (else we might try to expand
2060 * zero clusters that have already been expanded); furthermore, it is also
2061 * necessary to empty the L2 table cache, since it may contain tables which
2062 * are now going to be modified directly on disk, bypassing the cache.
2063 * qcow2_cache_empty() does both for us. */
2064 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2065 if (ret < 0) {
2066 goto fail;
2067 }
2068
2069 for (i = 0; i < s->nb_snapshots; i++) {
c9a442e4
AG
2070 int l1_size2;
2071 uint64_t *new_l1_table;
2072 Error *local_err = NULL;
2073
2074 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2075 s->snapshots[i].l1_size, sizeof(uint64_t),
2076 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2077 &local_err);
2078 if (ret < 0) {
2079 error_report_err(local_err);
2080 goto fail;
2081 }
32b6444d 2082
c9a442e4
AG
2083 l1_size2 = s->snapshots[i].l1_size * sizeof(uint64_t);
2084 new_l1_table = g_try_realloc(l1_table, l1_size2);
de7269d2
AG
2085
2086 if (!new_l1_table) {
2087 ret = -ENOMEM;
2088 goto fail;
2089 }
2090
2091 l1_table = new_l1_table;
32b6444d 2092
c9a442e4
AG
2093 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset,
2094 l1_table, l1_size2);
32b6444d
HR
2095 if (ret < 0) {
2096 goto fail;
2097 }
2098
2099 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2100 be64_to_cpus(&l1_table[j]);
2101 }
2102
2103 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
4057a2b2 2104 &visited_l1_entries, l1_entries,
8b13976d 2105 status_cb, cb_opaque);
32b6444d
HR
2106 if (ret < 0) {
2107 goto fail;
2108 }
2109 }
2110
2111 ret = 0;
2112
2113fail:
32b6444d
HR
2114 g_free(l1_table);
2115 return ret;
2116}