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