]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2-cluster.c
Merge remote-tracking branch 'remotes/pmaydell/tags/pull-docs-20200306' into staging
[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 218/*
da86f8cb
AG
219 * Writes an L1 entry to disk (note that depending on the alignment
220 * requirements this function may write more that just one entry in
221 * order to prevent bdrv_pwrite from performing a read-modify-write)
6583e3c7 222 */
e23e400e 223int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
6583e3c7 224{
ff99129a 225 BDRVQcow2State *s = bs->opaque;
6583e3c7 226 int l1_start_index;
f7defcb6 227 int i, ret;
da86f8cb
AG
228 int bufsize = MAX(sizeof(uint64_t),
229 MIN(bs->file->bs->bl.request_alignment, s->cluster_size));
230 int nentries = bufsize / sizeof(uint64_t);
231 g_autofree uint64_t *buf = g_try_new0(uint64_t, nentries);
6583e3c7 232
da86f8cb
AG
233 if (buf == NULL) {
234 return -ENOMEM;
235 }
236
237 l1_start_index = QEMU_ALIGN_DOWN(l1_index, nentries);
238 for (i = 0; i < MIN(nentries, s->l1_size - l1_start_index); i++) {
6583e3c7
KW
239 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
240 }
241
231bb267 242 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
da86f8cb 243 s->l1_table_offset + 8 * l1_start_index, bufsize, false);
cf93980e
HR
244 if (ret < 0) {
245 return ret;
246 }
247
66f82cee 248 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
d9ca2ea2 249 ret = bdrv_pwrite_sync(bs->file,
9a4f4c31 250 s->l1_table_offset + 8 * l1_start_index,
da86f8cb 251 buf, bufsize);
f7defcb6
KW
252 if (ret < 0) {
253 return ret;
6583e3c7
KW
254 }
255
256 return 0;
257}
258
45aba42f
KW
259/*
260 * l2_allocate
261 *
262 * Allocate a new l2 entry in the file. If l1_index points to an already
263 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
264 * table) copy the contents of the old L2 table into the newly allocated one.
265 * Otherwise the new table is initialized with zeros.
266 *
267 */
268
3861946a 269static int l2_allocate(BlockDriverState *bs, int l1_index)
45aba42f 270{
ff99129a 271 BDRVQcow2State *s = bs->opaque;
6583e3c7 272 uint64_t old_l2_offset;
3861946a
AG
273 uint64_t *l2_slice = NULL;
274 unsigned slice, slice_size2, n_slices;
f4f0d391 275 int64_t l2_offset;
c46e1167 276 int ret;
45aba42f
KW
277
278 old_l2_offset = s->l1_table[l1_index];
279
3cce16f4
KW
280 trace_qcow2_l2_allocate(bs, l1_index);
281
45aba42f
KW
282 /* allocate a new l2 entry */
283
ed6ccf0f 284 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
5d757b56 285 if (l2_offset < 0) {
be0b742e
HR
286 ret = l2_offset;
287 goto fail;
5d757b56 288 }
29c1a730 289
c1c43990
AG
290 /* The offset must fit in the offset field of the L1 table entry */
291 assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
292
98839750
AG
293 /* If we're allocating the table at offset 0 then something is wrong */
294 if (l2_offset == 0) {
295 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
296 "allocation of L2 table at offset 0");
297 ret = -EIO;
298 goto fail;
299 }
300
29c1a730
KW
301 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
302 if (ret < 0) {
303 goto fail;
304 }
45aba42f 305
45aba42f
KW
306 /* allocate a new entry in the l2 cache */
307
3861946a
AG
308 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
309 n_slices = s->cluster_size / slice_size2;
310
3cce16f4 311 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
3861946a 312 for (slice = 0; slice < n_slices; slice++) {
6580bb09 313 ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
3861946a
AG
314 l2_offset + slice * slice_size2,
315 (void **) &l2_slice);
6580bb09
AG
316 if (ret < 0) {
317 goto fail;
318 }
29c1a730 319
6580bb09 320 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
3861946a
AG
321 /* if there was no old l2 table, clear the new slice */
322 memset(l2_slice, 0, slice_size2);
6580bb09 323 } else {
3861946a
AG
324 uint64_t *old_slice;
325 uint64_t old_l2_slice_offset =
326 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
29c1a730 327
3861946a 328 /* if there was an old l2 table, read a slice from the disk */
6580bb09 329 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
3861946a
AG
330 ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
331 (void **) &old_slice);
6580bb09
AG
332 if (ret < 0) {
333 goto fail;
334 }
335
3861946a 336 memcpy(l2_slice, old_slice, slice_size2);
6580bb09 337
3861946a 338 qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
29c1a730
KW
339 }
340
3861946a 341 /* write the l2 slice to the file */
6580bb09 342 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
29c1a730 343
6580bb09 344 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
3861946a
AG
345 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
346 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
45aba42f 347 }
29c1a730 348
29c1a730 349 ret = qcow2_cache_flush(bs, s->l2_table_cache);
c46e1167 350 if (ret < 0) {
175e1152
KW
351 goto fail;
352 }
353
354 /* update the L1 entry */
3cce16f4 355 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
175e1152 356 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
e23e400e 357 ret = qcow2_write_l1_entry(bs, l1_index);
175e1152
KW
358 if (ret < 0) {
359 goto fail;
c46e1167 360 }
45aba42f 361
3cce16f4 362 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
c46e1167 363 return 0;
175e1152
KW
364
365fail:
3cce16f4 366 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
3861946a
AG
367 if (l2_slice != NULL) {
368 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
8585afd8 369 }
68dba0bf 370 s->l1_table[l1_index] = old_l2_offset;
e3b21ef9
HR
371 if (l2_offset > 0) {
372 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
373 QCOW2_DISCARD_ALWAYS);
374 }
175e1152 375 return ret;
45aba42f
KW
376}
377
2bfcc4a0 378/*
13f893c4 379 * Checks how many clusters in a given L2 slice are contiguous in the image
2bfcc4a0
KW
380 * file. As soon as one of the flags in the bitmask stop_flags changes compared
381 * to the first cluster, the search is stopped and the cluster is not counted
382 * as contiguous. (This allows it, for example, to stop at the first compressed
383 * cluster which may require a different handling)
384 */
808c2bb4
KW
385static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters,
386 int cluster_size, uint64_t *l2_slice, uint64_t stop_flags)
45aba42f
KW
387{
388 int i;
3ef95218 389 QCow2ClusterType first_cluster_type;
78a52ad5 390 uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED;
13f893c4 391 uint64_t first_entry = be64_to_cpu(l2_slice[0]);
15684a47 392 uint64_t offset = first_entry & mask;
45aba42f 393
b8c8353a
KW
394 first_cluster_type = qcow2_get_cluster_type(bs, first_entry);
395 if (first_cluster_type == QCOW2_CLUSTER_UNALLOCATED) {
45aba42f 396 return 0;
564a6b69 397 }
45aba42f 398
564a6b69 399 /* must be allocated */
564a6b69 400 assert(first_cluster_type == QCOW2_CLUSTER_NORMAL ||
fdfab37d 401 first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC);
15684a47 402
61653008 403 for (i = 0; i < nb_clusters; i++) {
13f893c4 404 uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask;
2bfcc4a0 405 if (offset + (uint64_t) i * cluster_size != l2_entry) {
45aba42f 406 break;
2bfcc4a0
KW
407 }
408 }
45aba42f 409
7d37435b 410 return i;
45aba42f
KW
411}
412
4341df8a
EB
413/*
414 * Checks how many consecutive unallocated clusters in a given L2
c26f10ba 415 * slice have the same cluster type.
4341df8a 416 */
808c2bb4
KW
417static int count_contiguous_clusters_unallocated(BlockDriverState *bs,
418 int nb_clusters,
c26f10ba 419 uint64_t *l2_slice,
3ef95218 420 QCow2ClusterType wanted_type)
45aba42f 421{
2bfcc4a0
KW
422 int i;
423
fdfab37d 424 assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN ||
4341df8a 425 wanted_type == QCOW2_CLUSTER_UNALLOCATED);
2bfcc4a0 426 for (i = 0; i < nb_clusters; i++) {
c26f10ba 427 uint64_t entry = be64_to_cpu(l2_slice[i]);
808c2bb4 428 QCow2ClusterType type = qcow2_get_cluster_type(bs, entry);
45aba42f 429
fdfab37d 430 if (type != wanted_type) {
2bfcc4a0
KW
431 break;
432 }
433 }
45aba42f
KW
434
435 return i;
436}
437
672f0f2c
AG
438static int coroutine_fn do_perform_cow_read(BlockDriverState *bs,
439 uint64_t src_cluster_offset,
440 unsigned offset_in_cluster,
86b862c4 441 QEMUIOVector *qiov)
45aba42f 442{
aaa4d20b 443 int ret;
1b9f1491 444
86b862c4 445 if (qiov->size == 0) {
99450c6f
AG
446 return 0;
447 }
448
66f82cee 449 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
aef4acb6 450
dba28555 451 if (!bs->drv) {
672f0f2c 452 return -ENOMEDIUM;
dba28555
HR
453 }
454
aef4acb6
SH
455 /* Call .bdrv_co_readv() directly instead of using the public block-layer
456 * interface. This avoids double I/O throttling and request tracking,
457 * which can lead to deadlock when block layer copy-on-read is enabled.
458 */
df893d25
VSO
459 ret = bs->drv->bdrv_co_preadv_part(bs,
460 src_cluster_offset + offset_in_cluster,
461 qiov->size, qiov, 0, 0);
1b9f1491 462 if (ret < 0) {
672f0f2c 463 return ret;
1b9f1491
KW
464 }
465
672f0f2c
AG
466 return 0;
467}
468
672f0f2c
AG
469static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
470 uint64_t cluster_offset,
471 unsigned offset_in_cluster,
86b862c4 472 QEMUIOVector *qiov)
672f0f2c 473{
966b000f 474 BDRVQcow2State *s = bs->opaque;
672f0f2c
AG
475 int ret;
476
86b862c4 477 if (qiov->size == 0) {
672f0f2c
AG
478 return 0;
479 }
480
231bb267 481 ret = qcow2_pre_write_overlap_check(bs, 0,
966b000f 482 cluster_offset + offset_in_cluster, qiov->size, true);
cf93980e 483 if (ret < 0) {
672f0f2c 484 return ret;
cf93980e
HR
485 }
486
66f82cee 487 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
966b000f 488 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
86b862c4 489 qiov->size, qiov, 0);
1b9f1491 490 if (ret < 0) {
672f0f2c 491 return ret;
1b9f1491
KW
492 }
493
672f0f2c 494 return 0;
45aba42f
KW
495}
496
497
498/*
499 * get_cluster_offset
500 *
ecfe1863
KW
501 * For a given offset of the virtual disk, find the cluster type and offset in
502 * the qcow2 file. The offset is stored in *cluster_offset.
45aba42f 503 *
ecfe1863
KW
504 * On entry, *bytes is the maximum number of contiguous bytes starting at
505 * offset that we are interested in.
45aba42f 506 *
ecfe1863
KW
507 * On exit, *bytes is the number of bytes starting at offset that have the same
508 * cluster type and (if applicable) are stored contiguously in the image file.
509 * Compressed clusters are always returned one by one.
45aba42f 510 *
68d000a3
KW
511 * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
512 * cases.
45aba42f 513 */
1c46efaa 514int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
ecfe1863 515 unsigned int *bytes, uint64_t *cluster_offset)
45aba42f 516{
ff99129a 517 BDRVQcow2State *s = bs->opaque;
2cf7cfa1 518 unsigned int l2_index;
fd630039
AG
519 uint64_t l1_index, l2_offset, *l2_slice;
520 int c;
c834cba9
HR
521 unsigned int offset_in_cluster;
522 uint64_t bytes_available, bytes_needed, nb_clusters;
3ef95218 523 QCow2ClusterType type;
55c17e98 524 int ret;
45aba42f 525
b2f65d6b 526 offset_in_cluster = offset_into_cluster(s, offset);
ecfe1863 527 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
45aba42f 528
b2f65d6b 529 /* compute how many bytes there are between the start of the cluster
fd630039
AG
530 * containing offset and the end of the l2 slice that contains
531 * the entry pointing to it */
532 bytes_available =
533 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
534 << s->cluster_bits;
45aba42f 535
b2f65d6b
KW
536 if (bytes_needed > bytes_available) {
537 bytes_needed = bytes_available;
45aba42f
KW
538 }
539
1c46efaa 540 *cluster_offset = 0;
45aba42f 541
b6af0975 542 /* seek to the l2 offset in the l1 table */
45aba42f 543
05b5b6ee 544 l1_index = offset_to_l1_index(s, offset);
68d000a3 545 if (l1_index >= s->l1_size) {
3ef95218 546 type = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 547 goto out;
68d000a3 548 }
45aba42f 549
68d000a3
KW
550 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
551 if (!l2_offset) {
3ef95218 552 type = QCOW2_CLUSTER_UNALLOCATED;
45aba42f 553 goto out;
68d000a3 554 }
45aba42f 555
a97c67ee
HR
556 if (offset_into_cluster(s, l2_offset)) {
557 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
558 " unaligned (L1 index: %#" PRIx64 ")",
559 l2_offset, l1_index);
560 return -EIO;
561 }
562
fd630039 563 /* load the l2 slice in memory */
45aba42f 564
fd630039 565 ret = l2_load(bs, offset, l2_offset, &l2_slice);
55c17e98
KW
566 if (ret < 0) {
567 return ret;
1c46efaa 568 }
45aba42f
KW
569
570 /* find the cluster offset for the given disk offset */
571
fd630039
AG
572 l2_index = offset_to_l2_slice_index(s, offset);
573 *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
b6d36def 574
b2f65d6b 575 nb_clusters = size_to_clusters(s, bytes_needed);
c834cba9
HR
576 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
577 * integers; the minimum cluster size is 512, so this assertion is always
578 * true */
579 assert(nb_clusters <= INT_MAX);
45aba42f 580
808c2bb4 581 type = qcow2_get_cluster_type(bs, *cluster_offset);
fdfab37d
EB
582 if (s->qcow_version < 3 && (type == QCOW2_CLUSTER_ZERO_PLAIN ||
583 type == QCOW2_CLUSTER_ZERO_ALLOC)) {
584 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
585 " in pre-v3 image (L2 offset: %#" PRIx64
586 ", L2 index: %#x)", l2_offset, l2_index);
587 ret = -EIO;
588 goto fail;
589 }
3ef95218 590 switch (type) {
68d000a3 591 case QCOW2_CLUSTER_COMPRESSED:
966b000f
KW
592 if (has_data_file(bs)) {
593 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
594 "entry found in image with external data "
595 "file (L2 offset: %#" PRIx64 ", L2 index: "
596 "%#x)", l2_offset, l2_index);
597 ret = -EIO;
598 goto fail;
599 }
68d000a3
KW
600 /* Compressed clusters can only be processed one by one */
601 c = 1;
602 *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK;
603 break;
fdfab37d 604 case QCOW2_CLUSTER_ZERO_PLAIN:
68d000a3 605 case QCOW2_CLUSTER_UNALLOCATED:
45aba42f 606 /* how many empty clusters ? */
808c2bb4 607 c = count_contiguous_clusters_unallocated(bs, nb_clusters,
fd630039 608 &l2_slice[l2_index], type);
68d000a3
KW
609 *cluster_offset = 0;
610 break;
fdfab37d 611 case QCOW2_CLUSTER_ZERO_ALLOC:
68d000a3 612 case QCOW2_CLUSTER_NORMAL:
45aba42f 613 /* how many allocated clusters ? */
808c2bb4 614 c = count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
fd630039 615 &l2_slice[l2_index], QCOW_OFLAG_ZERO);
68d000a3 616 *cluster_offset &= L2E_OFFSET_MASK;
a97c67ee 617 if (offset_into_cluster(s, *cluster_offset)) {
fdfab37d
EB
618 qcow2_signal_corruption(bs, true, -1, -1,
619 "Cluster allocation offset %#"
a97c67ee
HR
620 PRIx64 " unaligned (L2 offset: %#" PRIx64
621 ", L2 index: %#x)", *cluster_offset,
622 l2_offset, l2_index);
623 ret = -EIO;
624 goto fail;
625 }
966b000f
KW
626 if (has_data_file(bs) && *cluster_offset != offset - offset_in_cluster)
627 {
628 qcow2_signal_corruption(bs, true, -1, -1,
629 "External data file host cluster offset %#"
630 PRIx64 " does not match guest cluster "
631 "offset: %#" PRIx64
632 ", L2 index: %#x)", *cluster_offset,
633 offset - offset_in_cluster, l2_index);
634 ret = -EIO;
635 goto fail;
636 }
68d000a3 637 break;
1417d7e4
KW
638 default:
639 abort();
45aba42f
KW
640 }
641
fd630039 642 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
29c1a730 643
c834cba9 644 bytes_available = (int64_t)c * s->cluster_size;
68d000a3 645
45aba42f 646out:
b2f65d6b
KW
647 if (bytes_available > bytes_needed) {
648 bytes_available = bytes_needed;
649 }
45aba42f 650
c834cba9
HR
651 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
652 * subtracting offset_in_cluster will therefore definitely yield something
653 * not exceeding UINT_MAX */
654 assert(bytes_available - offset_in_cluster <= UINT_MAX);
ecfe1863 655 *bytes = bytes_available - offset_in_cluster;
45aba42f 656
3ef95218 657 return type;
a97c67ee
HR
658
659fail:
fd630039 660 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
a97c67ee 661 return ret;
45aba42f
KW
662}
663
664/*
665 * get_cluster_table
666 *
667 * for a given disk offset, load (and allocate if needed)
c03bfc5b 668 * the appropriate slice of its l2 table.
45aba42f 669 *
c03bfc5b 670 * the cluster index in the l2 slice is given to the caller.
45aba42f 671 *
1e3e8f1a 672 * Returns 0 on success, -errno in failure case
45aba42f 673 */
45aba42f 674static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
c03bfc5b 675 uint64_t **new_l2_slice,
45aba42f
KW
676 int *new_l2_index)
677{
ff99129a 678 BDRVQcow2State *s = bs->opaque;
2cf7cfa1
KW
679 unsigned int l2_index;
680 uint64_t l1_index, l2_offset;
c03bfc5b 681 uint64_t *l2_slice = NULL;
80ee15a6 682 int ret;
45aba42f 683
b6af0975 684 /* seek to the l2 offset in the l1 table */
45aba42f 685
05b5b6ee 686 l1_index = offset_to_l1_index(s, offset);
45aba42f 687 if (l1_index >= s->l1_size) {
72893756 688 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
1e3e8f1a
KW
689 if (ret < 0) {
690 return ret;
691 }
45aba42f 692 }
8e37f681 693
2cf7cfa1 694 assert(l1_index < s->l1_size);
8e37f681 695 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
a97c67ee
HR
696 if (offset_into_cluster(s, l2_offset)) {
697 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
698 " unaligned (L1 index: %#" PRIx64 ")",
699 l2_offset, l1_index);
700 return -EIO;
701 }
45aba42f 702
05f9ee46 703 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
16fde5f2 704 /* First allocate a new L2 table (and do COW if needed) */
3861946a 705 ret = l2_allocate(bs, l1_index);
c46e1167
KW
706 if (ret < 0) {
707 return ret;
1e3e8f1a 708 }
16fde5f2
KW
709
710 /* Then decrease the refcount of the old table */
711 if (l2_offset) {
6cfcb9b8
KW
712 qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
713 QCOW2_DISCARD_OTHER);
16fde5f2 714 }
3861946a
AG
715
716 /* Get the offset of the newly-allocated l2 table */
717 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
718 assert(offset_into_cluster(s, l2_offset) == 0);
05f9ee46
AG
719 }
720
c03bfc5b
AG
721 /* load the l2 slice in memory */
722 ret = l2_load(bs, offset, l2_offset, &l2_slice);
05f9ee46
AG
723 if (ret < 0) {
724 return ret;
45aba42f
KW
725 }
726
727 /* find the cluster offset for the given disk offset */
728
c03bfc5b 729 l2_index = offset_to_l2_slice_index(s, offset);
45aba42f 730
c03bfc5b 731 *new_l2_slice = l2_slice;
45aba42f
KW
732 *new_l2_index = l2_index;
733
1e3e8f1a 734 return 0;
45aba42f
KW
735}
736
737/*
738 * alloc_compressed_cluster_offset
739 *
77e023ff
KW
740 * For a given offset on the virtual disk, allocate a new compressed cluster
741 * and put the host offset of the cluster into *host_offset. If a cluster is
742 * already allocated at the offset, return an error.
45aba42f 743 *
77e023ff 744 * Return 0 on success and -errno in error cases
45aba42f 745 */
77e023ff
KW
746int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
747 uint64_t offset,
748 int compressed_size,
749 uint64_t *host_offset)
45aba42f 750{
ff99129a 751 BDRVQcow2State *s = bs->opaque;
45aba42f 752 int l2_index, ret;
e4e72548 753 uint64_t *l2_slice;
f4f0d391 754 int64_t cluster_offset;
45aba42f
KW
755 int nb_csectors;
756
966b000f
KW
757 if (has_data_file(bs)) {
758 return 0;
759 }
760
e4e72548 761 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
1e3e8f1a 762 if (ret < 0) {
77e023ff 763 return ret;
1e3e8f1a 764 }
45aba42f 765
b0b6862e
KW
766 /* Compression can't overwrite anything. Fail if the cluster was already
767 * allocated. */
e4e72548 768 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
b0b6862e 769 if (cluster_offset & L2E_OFFSET_MASK) {
e4e72548 770 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
77e023ff 771 return -EIO;
8f1efd00 772 }
45aba42f 773
ed6ccf0f 774 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
5d757b56 775 if (cluster_offset < 0) {
e4e72548 776 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
77e023ff 777 return cluster_offset;
5d757b56
KW
778 }
779
b6c24694
AG
780 nb_csectors =
781 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
782 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
45aba42f 783
3a75a870
AG
784 /* The offset and size must fit in their fields of the L2 table entry */
785 assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
786 assert((nb_csectors & s->csize_mask) == nb_csectors);
787
45aba42f
KW
788 cluster_offset |= QCOW_OFLAG_COMPRESSED |
789 ((uint64_t)nb_csectors << s->csize_shift);
790
791 /* update L2 table */
792
793 /* compressed clusters never have the copied flag */
794
66f82cee 795 BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
e4e72548
AG
796 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
797 l2_slice[l2_index] = cpu_to_be64(cluster_offset);
798 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
4c1612d9 799
77e023ff
KW
800 *host_offset = cluster_offset & s->cluster_offset_mask;
801 return 0;
4c1612d9
KW
802}
803
99450c6f 804static int perform_cow(BlockDriverState *bs, QCowL2Meta *m)
593fb83c 805{
ff99129a 806 BDRVQcow2State *s = bs->opaque;
99450c6f
AG
807 Qcow2COWRegion *start = &m->cow_start;
808 Qcow2COWRegion *end = &m->cow_end;
672f0f2c 809 unsigned buffer_size;
b3cf1c7c
AG
810 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
811 bool merge_reads;
672f0f2c 812 uint8_t *start_buffer, *end_buffer;
86b862c4 813 QEMUIOVector qiov;
593fb83c
KW
814 int ret;
815
672f0f2c 816 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
b3cf1c7c
AG
817 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
818 assert(start->offset + start->nb_bytes <= end->offset);
672f0f2c 819
c8bb23cb 820 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
593fb83c
KW
821 return 0;
822 }
823
b3cf1c7c
AG
824 /* If we have to read both the start and end COW regions and the
825 * middle region is not too large then perform just one read
826 * operation */
827 merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
828 if (merge_reads) {
829 buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
830 } else {
831 /* If we have to do two reads, add some padding in the middle
832 * if necessary to make sure that the end region is optimally
833 * aligned. */
834 size_t align = bdrv_opt_mem_align(bs);
835 assert(align > 0 && align <= UINT_MAX);
836 assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
837 UINT_MAX - end->nb_bytes);
838 buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
839 }
840
841 /* Reserve a buffer large enough to store all the data that we're
842 * going to read */
672f0f2c
AG
843 start_buffer = qemu_try_blockalign(bs, buffer_size);
844 if (start_buffer == NULL) {
845 return -ENOMEM;
846 }
847 /* The part of the buffer where the end region is located */
848 end_buffer = start_buffer + buffer_size - end->nb_bytes;
849
5396234b
VSO
850 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
851 qemu_iovec_subvec_niov(m->data_qiov,
852 m->data_qiov_offset,
853 data_bytes)
854 : 0));
86b862c4 855
593fb83c 856 qemu_co_mutex_unlock(&s->lock);
b3cf1c7c
AG
857 /* First we read the existing data from both COW regions. We
858 * either read the whole region in one go, or the start and end
859 * regions separately. */
860 if (merge_reads) {
86b862c4
AG
861 qemu_iovec_add(&qiov, start_buffer, buffer_size);
862 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
b3cf1c7c 863 } else {
86b862c4
AG
864 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
865 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
b3cf1c7c
AG
866 if (ret < 0) {
867 goto fail;
868 }
672f0f2c 869
86b862c4
AG
870 qemu_iovec_reset(&qiov);
871 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
872 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
b3cf1c7c 873 }
593fb83c 874 if (ret < 0) {
99450c6f 875 goto fail;
593fb83c
KW
876 }
877
672f0f2c
AG
878 /* Encrypt the data if necessary before writing it */
879 if (bs->encrypted) {
603fbd07
ML
880 ret = qcow2_co_encrypt(bs,
881 m->alloc_offset + start->offset,
882 m->offset + start->offset,
883 start_buffer, start->nb_bytes);
884 if (ret < 0) {
885 goto fail;
886 }
887
888 ret = qcow2_co_encrypt(bs,
889 m->alloc_offset + end->offset,
890 m->offset + end->offset,
891 end_buffer, end->nb_bytes);
892 if (ret < 0) {
672f0f2c
AG
893 goto fail;
894 }
895 }
896
ee22a9d8
AG
897 /* And now we can write everything. If we have the guest data we
898 * can write everything in one single operation */
899 if (m->data_qiov) {
900 qemu_iovec_reset(&qiov);
901 if (start->nb_bytes) {
902 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
903 }
5396234b 904 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
ee22a9d8
AG
905 if (end->nb_bytes) {
906 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
907 }
908 /* NOTE: we have a write_aio blkdebug event here followed by
909 * a cow_write one in do_perform_cow_write(), but there's only
910 * one single I/O operation */
911 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
912 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
913 } else {
914 /* If there's no guest data then write both COW regions separately */
915 qemu_iovec_reset(&qiov);
916 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
917 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
918 if (ret < 0) {
919 goto fail;
920 }
921
922 qemu_iovec_reset(&qiov);
923 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
924 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
672f0f2c 925 }
99450c6f
AG
926
927fail:
928 qemu_co_mutex_lock(&s->lock);
929
593fb83c
KW
930 /*
931 * Before we update the L2 table to actually point to the new cluster, we
932 * need to be sure that the refcounts have been increased and COW was
933 * handled.
934 */
99450c6f
AG
935 if (ret == 0) {
936 qcow2_cache_depends_on_flush(s->l2_table_cache);
937 }
593fb83c 938
672f0f2c 939 qemu_vfree(start_buffer);
86b862c4 940 qemu_iovec_destroy(&qiov);
99450c6f 941 return ret;
593fb83c
KW
942}
943
148da7ea 944int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
45aba42f 945{
ff99129a 946 BDRVQcow2State *s = bs->opaque;
45aba42f 947 int i, j = 0, l2_index, ret;
a002c0b0 948 uint64_t *old_cluster, *l2_slice;
250196f1 949 uint64_t cluster_offset = m->alloc_offset;
45aba42f 950
3cce16f4 951 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
f50f88b9 952 assert(m->nb_clusters > 0);
45aba42f 953
5839e53b 954 old_cluster = g_try_new(uint64_t, m->nb_clusters);
de82815d
KW
955 if (old_cluster == NULL) {
956 ret = -ENOMEM;
957 goto err;
958 }
45aba42f
KW
959
960 /* copy content of unmodified sectors */
99450c6f 961 ret = perform_cow(bs, m);
593fb83c
KW
962 if (ret < 0) {
963 goto err;
29c1a730
KW
964 }
965
593fb83c 966 /* Update L2 table. */
74c4510a 967 if (s->use_lazy_refcounts) {
280d3735
KW
968 qcow2_mark_dirty(bs);
969 }
bfe8043e
SH
970 if (qcow2_need_accurate_refcounts(s)) {
971 qcow2_cache_set_dependency(bs, s->l2_table_cache,
972 s->refcount_block_cache);
973 }
280d3735 974
a002c0b0 975 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
1e3e8f1a 976 if (ret < 0) {
45aba42f 977 goto err;
1e3e8f1a 978 }
a002c0b0 979 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
45aba42f 980
a002c0b0 981 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
45aba42f 982 for (i = 0; i < m->nb_clusters; i++) {
3a75a870 983 uint64_t offset = cluster_offset + (i << s->cluster_bits);
45aba42f 984 /* if two concurrent writes happen to the same unallocated cluster
aaa4d20b
KW
985 * each write allocates separate cluster and writes data concurrently.
986 * The first one to complete updates l2 table with pointer to its
987 * cluster the second one has to do RMW (which is done above by
988 * perform_cow()), update l2 table with its cluster pointer and free
989 * old cluster. This is what this loop does */
a002c0b0
AG
990 if (l2_slice[l2_index + i] != 0) {
991 old_cluster[j++] = l2_slice[l2_index + i];
aaa4d20b 992 }
45aba42f 993
3a75a870
AG
994 /* The offset must fit in the offset field of the L2 table entry */
995 assert((offset & L2E_OFFSET_MASK) == offset);
996
997 l2_slice[l2_index + i] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
45aba42f
KW
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;
c3b6658c
KW
1029 if (!has_data_file(bs)) {
1030 qcow2_free_clusters(bs, m->alloc_offset,
1031 m->nb_clusters << s->cluster_bits,
1032 QCOW2_DISCARD_NEVER);
1033 }
8b24cd14
KW
1034}
1035
bf319ece
KW
1036/*
1037 * Returns the number of contiguous clusters that can be used for an allocating
1038 * write, but require COW to be performed (this includes yet unallocated space,
1039 * which must copy from the backing file)
1040 */
808c2bb4 1041static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,
dd32c881 1042 uint64_t *l2_slice, int l2_index)
bf319ece 1043{
143550a8 1044 int i;
bf319ece 1045
143550a8 1046 for (i = 0; i < nb_clusters; i++) {
dd32c881 1047 uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);
808c2bb4 1048 QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry);
143550a8
KW
1049
1050 switch(cluster_type) {
1051 case QCOW2_CLUSTER_NORMAL:
1052 if (l2_entry & QCOW_OFLAG_COPIED) {
1053 goto out;
1054 }
bf319ece 1055 break;
143550a8
KW
1056 case QCOW2_CLUSTER_UNALLOCATED:
1057 case QCOW2_CLUSTER_COMPRESSED:
fdfab37d
EB
1058 case QCOW2_CLUSTER_ZERO_PLAIN:
1059 case QCOW2_CLUSTER_ZERO_ALLOC:
bf319ece 1060 break;
143550a8
KW
1061 default:
1062 abort();
1063 }
bf319ece
KW
1064 }
1065
143550a8 1066out:
bf319ece
KW
1067 assert(i <= nb_clusters);
1068 return i;
1069}
1070
250196f1 1071/*
226c3c26
KW
1072 * Check if there already is an AIO write request in flight which allocates
1073 * the same cluster. In this case we need to wait until the previous
1074 * request has completed and updated the L2 table accordingly.
65eb2e35
KW
1075 *
1076 * Returns:
1077 * 0 if there was no dependency. *cur_bytes indicates the number of
1078 * bytes from guest_offset that can be read before the next
1079 * dependency must be processed (or the request is complete)
1080 *
1081 * -EAGAIN if we had to wait for another request, previously gathered
1082 * information on cluster allocation may be invalid now. The caller
1083 * must start over anyway, so consider *cur_bytes undefined.
250196f1 1084 */
226c3c26 1085static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,
ecdd5333 1086 uint64_t *cur_bytes, QCowL2Meta **m)
250196f1 1087{
ff99129a 1088 BDRVQcow2State *s = bs->opaque;
250196f1 1089 QCowL2Meta *old_alloc;
65eb2e35 1090 uint64_t bytes = *cur_bytes;
250196f1 1091
250196f1
KW
1092 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1093
65eb2e35
KW
1094 uint64_t start = guest_offset;
1095 uint64_t end = start + bytes;
1096 uint64_t old_start = l2meta_cow_start(old_alloc);
1097 uint64_t old_end = l2meta_cow_end(old_alloc);
250196f1 1098
d9d74f41 1099 if (end <= old_start || start >= old_end) {
250196f1
KW
1100 /* No intersection */
1101 } else {
1102 if (start < old_start) {
1103 /* Stop at the start of a running allocation */
65eb2e35 1104 bytes = old_start - start;
250196f1 1105 } else {
65eb2e35 1106 bytes = 0;
250196f1
KW
1107 }
1108
ecdd5333
KW
1109 /* Stop if already an l2meta exists. After yielding, it wouldn't
1110 * be valid any more, so we'd have to clean up the old L2Metas
1111 * and deal with requests depending on them before starting to
1112 * gather new ones. Not worth the trouble. */
1113 if (bytes == 0 && *m) {
1114 *cur_bytes = 0;
1115 return 0;
1116 }
1117
65eb2e35 1118 if (bytes == 0) {
250196f1
KW
1119 /* Wait for the dependency to complete. We need to recheck
1120 * the free/allocated clusters when we continue. */
1ace7cea 1121 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
250196f1
KW
1122 return -EAGAIN;
1123 }
1124 }
1125 }
1126
65eb2e35
KW
1127 /* Make sure that existing clusters and new allocations are only used up to
1128 * the next dependency if we shortened the request above */
1129 *cur_bytes = bytes;
250196f1 1130
226c3c26
KW
1131 return 0;
1132}
1133
0af729ec
KW
1134/*
1135 * Checks how many already allocated clusters that don't require a copy on
c6d619cc
KW
1136 * write there are at the given guest_offset (up to *bytes). If *host_offset is
1137 * not INV_OFFSET, only physically contiguous clusters beginning at this host
1138 * offset are counted.
0af729ec 1139 *
411d62b0
KW
1140 * Note that guest_offset may not be cluster aligned. In this case, the
1141 * returned *host_offset points to exact byte referenced by guest_offset and
1142 * therefore isn't cluster aligned as well.
0af729ec
KW
1143 *
1144 * Returns:
1145 * 0: if no allocated clusters are available at the given offset.
1146 * *bytes is normally unchanged. It is set to 0 if the cluster
1147 * is allocated and doesn't need COW, but doesn't have the right
1148 * physical offset.
1149 *
1150 * 1: if allocated clusters that don't require a COW are available at
1151 * the requested offset. *bytes may have decreased and describes
1152 * the length of the area that can be written to.
1153 *
1154 * -errno: in error cases
0af729ec
KW
1155 */
1156static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
c53ede9f 1157 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
0af729ec 1158{
ff99129a 1159 BDRVQcow2State *s = bs->opaque;
0af729ec
KW
1160 int l2_index;
1161 uint64_t cluster_offset;
cde91766 1162 uint64_t *l2_slice;
b6d36def 1163 uint64_t nb_clusters;
c53ede9f 1164 unsigned int keep_clusters;
a3f1afb4 1165 int ret;
0af729ec
KW
1166
1167 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1168 *bytes);
0af729ec 1169
c6d619cc
KW
1170 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1171 == offset_into_cluster(s, *host_offset));
411d62b0 1172
acb0467f 1173 /*
cde91766 1174 * Calculate the number of clusters to look for. We stop at L2 slice
acb0467f
KW
1175 * boundaries to keep things simple.
1176 */
1177 nb_clusters =
1178 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1179
cde91766
AG
1180 l2_index = offset_to_l2_slice_index(s, guest_offset);
1181 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1182 assert(nb_clusters <= INT_MAX);
acb0467f 1183
0af729ec 1184 /* Find L2 entry for the first involved cluster */
cde91766 1185 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
0af729ec
KW
1186 if (ret < 0) {
1187 return ret;
1188 }
1189
cde91766 1190 cluster_offset = be64_to_cpu(l2_slice[l2_index]);
0af729ec
KW
1191
1192 /* Check how many clusters are already allocated and don't need COW */
808c2bb4 1193 if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL
0af729ec
KW
1194 && (cluster_offset & QCOW_OFLAG_COPIED))
1195 {
e62daaf6
KW
1196 /* If a specific host_offset is required, check it */
1197 bool offset_matches =
1198 (cluster_offset & L2E_OFFSET_MASK) == *host_offset;
1199
a97c67ee
HR
1200 if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {
1201 qcow2_signal_corruption(bs, true, -1, -1, "Data cluster offset "
1202 "%#llx unaligned (guest offset: %#" PRIx64
1203 ")", cluster_offset & L2E_OFFSET_MASK,
1204 guest_offset);
1205 ret = -EIO;
1206 goto out;
1207 }
1208
c6d619cc 1209 if (*host_offset != INV_OFFSET && !offset_matches) {
e62daaf6
KW
1210 *bytes = 0;
1211 ret = 0;
1212 goto out;
1213 }
1214
0af729ec 1215 /* We keep all QCOW_OFLAG_COPIED clusters */
c53ede9f 1216 keep_clusters =
808c2bb4 1217 count_contiguous_clusters(bs, nb_clusters, s->cluster_size,
cde91766 1218 &l2_slice[l2_index],
0af729ec 1219 QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);
c53ede9f
KW
1220 assert(keep_clusters <= nb_clusters);
1221
1222 *bytes = MIN(*bytes,
1223 keep_clusters * s->cluster_size
1224 - offset_into_cluster(s, guest_offset));
0af729ec
KW
1225
1226 ret = 1;
1227 } else {
0af729ec
KW
1228 ret = 0;
1229 }
1230
0af729ec 1231 /* Cleanup */
e62daaf6 1232out:
cde91766 1233 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
0af729ec 1234
e62daaf6
KW
1235 /* Only return a host offset if we actually made progress. Otherwise we
1236 * would make requirements for handle_alloc() that it can't fulfill */
a97c67ee 1237 if (ret > 0) {
411d62b0
KW
1238 *host_offset = (cluster_offset & L2E_OFFSET_MASK)
1239 + offset_into_cluster(s, guest_offset);
e62daaf6
KW
1240 }
1241
0af729ec
KW
1242 return ret;
1243}
1244
226c3c26
KW
1245/*
1246 * Allocates new clusters for the given guest_offset.
1247 *
1248 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1249 * contain the number of clusters that have been allocated and are contiguous
1250 * in the image file.
1251 *
c6d619cc
KW
1252 * If *host_offset is not INV_OFFSET, it specifies the offset in the image file
1253 * at which the new clusters must start. *nb_clusters can be 0 on return in
1254 * this case if the cluster at host_offset is already in use. If *host_offset
1255 * is INV_OFFSET, the clusters can be allocated anywhere in the image file.
226c3c26
KW
1256 *
1257 * *host_offset is updated to contain the offset into the image file at which
1258 * the first allocated cluster starts.
1259 *
1260 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1261 * function has been waiting for another request and the allocation must be
1262 * restarted, but the whole request should not be failed.
1263 */
1264static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,
b6d36def 1265 uint64_t *host_offset, uint64_t *nb_clusters)
226c3c26 1266{
ff99129a 1267 BDRVQcow2State *s = bs->opaque;
226c3c26
KW
1268
1269 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1270 *host_offset, *nb_clusters);
1271
966b000f
KW
1272 if (has_data_file(bs)) {
1273 assert(*host_offset == INV_OFFSET ||
1274 *host_offset == start_of_cluster(s, guest_offset));
1275 *host_offset = start_of_cluster(s, guest_offset);
1276 return 0;
1277 }
1278
250196f1
KW
1279 /* Allocate new clusters */
1280 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
c6d619cc 1281 if (*host_offset == INV_OFFSET) {
df021791
KW
1282 int64_t cluster_offset =
1283 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1284 if (cluster_offset < 0) {
1285 return cluster_offset;
1286 }
1287 *host_offset = cluster_offset;
1288 return 0;
250196f1 1289 } else {
b6d36def 1290 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
df021791
KW
1291 if (ret < 0) {
1292 return ret;
1293 }
1294 *nb_clusters = ret;
1295 return 0;
250196f1 1296 }
250196f1
KW
1297}
1298
10f0ed8b
KW
1299/*
1300 * Allocates new clusters for an area that either is yet unallocated or needs a
c6d619cc
KW
1301 * copy on write. If *host_offset is not INV_OFFSET, clusters are only
1302 * allocated if the new allocation can match the specified host offset.
10f0ed8b 1303 *
411d62b0
KW
1304 * Note that guest_offset may not be cluster aligned. In this case, the
1305 * returned *host_offset points to exact byte referenced by guest_offset and
1306 * therefore isn't cluster aligned as well.
10f0ed8b
KW
1307 *
1308 * Returns:
1309 * 0: if no clusters could be allocated. *bytes is set to 0,
1310 * *host_offset is left unchanged.
1311 *
1312 * 1: if new clusters were allocated. *bytes may be decreased if the
1313 * new allocation doesn't cover all of the requested area.
1314 * *host_offset is updated to contain the host offset of the first
1315 * newly allocated cluster.
1316 *
1317 * -errno: in error cases
10f0ed8b
KW
1318 */
1319static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
c37f4cd7 1320 uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m)
10f0ed8b 1321{
ff99129a 1322 BDRVQcow2State *s = bs->opaque;
10f0ed8b 1323 int l2_index;
6d99a344 1324 uint64_t *l2_slice;
10f0ed8b 1325 uint64_t entry;
b6d36def 1326 uint64_t nb_clusters;
10f0ed8b 1327 int ret;
564a6b69 1328 bool keep_old_clusters = false;
10f0ed8b 1329
c6d619cc 1330 uint64_t alloc_cluster_offset = INV_OFFSET;
10f0ed8b
KW
1331
1332 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1333 *bytes);
1334 assert(*bytes > 0);
1335
f5bc6350 1336 /*
6d99a344 1337 * Calculate the number of clusters to look for. We stop at L2 slice
f5bc6350
KW
1338 * boundaries to keep things simple.
1339 */
c37f4cd7
KW
1340 nb_clusters =
1341 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1342
6d99a344
AG
1343 l2_index = offset_to_l2_slice_index(s, guest_offset);
1344 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
b6d36def 1345 assert(nb_clusters <= INT_MAX);
f5bc6350 1346
d1b9d19f
HR
1347 /* Limit total allocation byte count to INT_MAX */
1348 nb_clusters = MIN(nb_clusters, INT_MAX >> s->cluster_bits);
1349
10f0ed8b 1350 /* Find L2 entry for the first involved cluster */
6d99a344 1351 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
10f0ed8b
KW
1352 if (ret < 0) {
1353 return ret;
1354 }
1355
6d99a344 1356 entry = be64_to_cpu(l2_slice[l2_index]);
bf3d78ae 1357 nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);
10f0ed8b 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 1431 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
d1b9d19f 1432 int avail_bytes = nb_clusters << s->cluster_bits;
85567393 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
3a75a870
AG
1930 /* The offset must fit in the offset field */
1931 assert((offset & L2E_OFFSET_MASK) == offset);
1932
226494ff
AG
1933 if (l2_refcount > 1) {
1934 /* For shared L2 tables, set the refcount accordingly
1935 * (it is already 1 and needs to be l2_refcount) */
1936 ret = qcow2_update_cluster_refcount(
1937 bs, offset >> s->cluster_bits,
2aabe7c7 1938 refcount_diff(1, l2_refcount), false,
ecf58777 1939 QCOW2_DISCARD_OTHER);
226494ff
AG
1940 if (ret < 0) {
1941 qcow2_free_clusters(bs, offset, s->cluster_size,
1942 QCOW2_DISCARD_OTHER);
1943 goto fail;
1944 }
ecf58777
HR
1945 }
1946 }
32b6444d 1947
226494ff 1948 if (offset_into_cluster(s, offset)) {
415184f5 1949 int l2_index = slice * s->l2_slice_size + j;
226494ff
AG
1950 qcow2_signal_corruption(
1951 bs, true, -1, -1,
1952 "Cluster allocation offset "
1953 "%#" PRIx64 " unaligned (L2 offset: %#"
1954 PRIx64 ", L2 index: %#x)", offset,
415184f5 1955 l2_offset, l2_index);
226494ff
AG
1956 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1957 qcow2_free_clusters(bs, offset, s->cluster_size,
1958 QCOW2_DISCARD_ALWAYS);
1959 }
1960 ret = -EIO;
1961 goto fail;
8dd93d93 1962 }
8dd93d93 1963
226494ff 1964 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
966b000f 1965 s->cluster_size, true);
226494ff
AG
1966 if (ret < 0) {
1967 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1968 qcow2_free_clusters(bs, offset, s->cluster_size,
1969 QCOW2_DISCARD_ALWAYS);
1970 }
1971 goto fail;
320c7066 1972 }
32b6444d 1973
966b000f
KW
1974 ret = bdrv_pwrite_zeroes(s->data_file, offset,
1975 s->cluster_size, 0);
226494ff
AG
1976 if (ret < 0) {
1977 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
1978 qcow2_free_clusters(bs, offset, s->cluster_size,
1979 QCOW2_DISCARD_ALWAYS);
1980 }
1981 goto fail;
320c7066 1982 }
32b6444d 1983
226494ff 1984 if (l2_refcount == 1) {
415184f5 1985 l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
226494ff 1986 } else {
415184f5 1987 l2_slice[j] = cpu_to_be64(offset);
226494ff
AG
1988 }
1989 l2_dirty = true;
e390cf5a 1990 }
32b6444d 1991
226494ff
AG
1992 if (is_active_l1) {
1993 if (l2_dirty) {
415184f5 1994 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
226494ff 1995 qcow2_cache_depends_on_flush(s->l2_table_cache);
32b6444d 1996 }
415184f5 1997 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
226494ff
AG
1998 } else {
1999 if (l2_dirty) {
2000 ret = qcow2_pre_write_overlap_check(
2001 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
966b000f 2002 slice_offset, slice_size2, false);
226494ff
AG
2003 if (ret < 0) {
2004 goto fail;
2005 }
32b6444d 2006
415184f5
AG
2007 ret = bdrv_pwrite(bs->file, slice_offset,
2008 l2_slice, slice_size2);
226494ff
AG
2009 if (ret < 0) {
2010 goto fail;
2011 }
32b6444d
HR
2012 }
2013 }
2014 }
4057a2b2
HR
2015
2016 (*visited_l1_entries)++;
2017 if (status_cb) {
8b13976d 2018 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
4057a2b2 2019 }
32b6444d
HR
2020 }
2021
2022 ret = 0;
2023
2024fail:
415184f5 2025 if (l2_slice) {
32b6444d 2026 if (!is_active_l1) {
415184f5 2027 qemu_vfree(l2_slice);
32b6444d 2028 } else {
415184f5 2029 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
32b6444d
HR
2030 }
2031 }
2032 return ret;
2033}
2034
2035/*
2036 * For backed images, expands all zero clusters on the image. For non-backed
2037 * images, deallocates all non-pre-allocated zero clusters (and claims the
2038 * allocation for pre-allocated ones). This is important for downgrading to a
2039 * qcow2 version which doesn't yet support metadata zero clusters.
2040 */
4057a2b2 2041int qcow2_expand_zero_clusters(BlockDriverState *bs,
8b13976d
HR
2042 BlockDriverAmendStatusCB *status_cb,
2043 void *cb_opaque)
32b6444d 2044{
ff99129a 2045 BDRVQcow2State *s = bs->opaque;
32b6444d 2046 uint64_t *l1_table = NULL;
4057a2b2 2047 int64_t l1_entries = 0, visited_l1_entries = 0;
32b6444d
HR
2048 int ret;
2049 int i, j;
2050
4057a2b2
HR
2051 if (status_cb) {
2052 l1_entries = s->l1_size;
2053 for (i = 0; i < s->nb_snapshots; i++) {
2054 l1_entries += s->snapshots[i].l1_size;
2055 }
2056 }
2057
32b6444d 2058 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
4057a2b2 2059 &visited_l1_entries, l1_entries,
8b13976d 2060 status_cb, cb_opaque);
32b6444d
HR
2061 if (ret < 0) {
2062 goto fail;
2063 }
2064
2065 /* Inactive L1 tables may point to active L2 tables - therefore it is
2066 * necessary to flush the L2 table cache before trying to access the L2
2067 * tables pointed to by inactive L1 entries (else we might try to expand
2068 * zero clusters that have already been expanded); furthermore, it is also
2069 * necessary to empty the L2 table cache, since it may contain tables which
2070 * are now going to be modified directly on disk, bypassing the cache.
2071 * qcow2_cache_empty() does both for us. */
2072 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2073 if (ret < 0) {
2074 goto fail;
2075 }
2076
2077 for (i = 0; i < s->nb_snapshots; i++) {
c9a442e4
AG
2078 int l1_size2;
2079 uint64_t *new_l1_table;
2080 Error *local_err = NULL;
2081
2082 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
2083 s->snapshots[i].l1_size, sizeof(uint64_t),
2084 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2085 &local_err);
2086 if (ret < 0) {
2087 error_report_err(local_err);
2088 goto fail;
2089 }
32b6444d 2090
c9a442e4
AG
2091 l1_size2 = s->snapshots[i].l1_size * sizeof(uint64_t);
2092 new_l1_table = g_try_realloc(l1_table, l1_size2);
de7269d2
AG
2093
2094 if (!new_l1_table) {
2095 ret = -ENOMEM;
2096 goto fail;
2097 }
2098
2099 l1_table = new_l1_table;
32b6444d 2100
c9a442e4
AG
2101 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset,
2102 l1_table, l1_size2);
32b6444d
HR
2103 if (ret < 0) {
2104 goto fail;
2105 }
2106
2107 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2108 be64_to_cpus(&l1_table[j]);
2109 }
2110
2111 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
4057a2b2 2112 &visited_l1_entries, l1_entries,
8b13976d 2113 status_cb, cb_opaque);
32b6444d
HR
2114 if (ret < 0) {
2115 goto fail;
2116 }
2117 }
2118
2119 ret = 0;
2120
2121fail:
32b6444d
HR
2122 g_free(l1_table);
2123 return ret;
2124}