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