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