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