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