blob: f4f6cd6ad020e63a6166d3389720ecfb2de30f48 [file] [log] [blame]
Kevin Wolf45aba422009-05-28 16:07:05 +02001/*
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
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Kevin Wolf45aba422009-05-28 16:07:05 +020026#include <zlib.h>
27
Markus Armbrustere2c1c342022-12-21 14:35:49 +010028#include "block/block-io.h"
Alberto Garciac9a442e2018-03-06 18:14:08 +020029#include "qapi/error.h"
Michael S. Tsirkin0d8c41d2018-05-03 22:50:20 +030030#include "qcow2.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010031#include "qemu/bswap.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000032#include "qemu/memalign.h"
Kevin Wolf3cce16f2012-03-01 18:36:21 +010033#include "trace.h"
Kevin Wolf45aba422009-05-28 16:07:05 +020034
Paolo Bonzinia1b4ecf2022-10-13 14:36:59 +020035int coroutine_fn qcow2_shrink_l1_table(BlockDriverState *bs,
36 uint64_t exact_size)
Pavel Butsykin46b732c2017-09-18 15:42:29 +030037{
38 BDRVQcow2State *s = bs->opaque;
39 int new_l1_size, i, ret;
40
41 if (exact_size >= s->l1_size) {
42 return 0;
43 }
44
45 new_l1_size = exact_size;
46
47#ifdef DEBUG_ALLOC2
48 fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_size);
49#endif
50
Paolo Bonzini17362392023-06-01 13:51:45 +020051 BLKDBG_CO_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE);
Alberto Faria38505e22022-10-13 14:37:06 +020052 ret = bdrv_co_pwrite_zeroes(bs->file,
53 s->l1_table_offset + new_l1_size * L1E_SIZE,
54 (s->l1_size - new_l1_size) * L1E_SIZE, 0);
Pavel Butsykin46b732c2017-09-18 15:42:29 +030055 if (ret < 0) {
56 goto fail;
57 }
58
Alberto Faria38505e22022-10-13 14:37:06 +020059 ret = bdrv_co_flush(bs->file->bs);
Pavel Butsykin46b732c2017-09-18 15:42:29 +030060 if (ret < 0) {
61 goto fail;
62 }
63
Paolo Bonzini17362392023-06-01 13:51:45 +020064 BLKDBG_CO_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS);
Pavel Butsykin46b732c2017-09-18 15:42:29 +030065 for (i = s->l1_size - 1; i > new_l1_size - 1; i--) {
66 if ((s->l1_table[i] & L1E_OFFSET_MASK) == 0) {
67 continue;
68 }
69 qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK,
70 s->cluster_size, QCOW2_DISCARD_ALWAYS);
71 s->l1_table[i] = 0;
72 }
73 return 0;
74
75fail:
76 /*
77 * If the write in the l1_table failed the image may contain a partially
78 * overwritten l1_table. In this case it would be better to clear the
79 * l1_table in memory to avoid possible image corruption.
80 */
81 memset(s->l1_table + new_l1_size, 0,
Alberto Garcia02b1ecf2020-08-28 13:08:28 +020082 (s->l1_size - new_l1_size) * L1E_SIZE);
Pavel Butsykin46b732c2017-09-18 15:42:29 +030083 return ret;
84}
85
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +020086int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
87 bool exact_size)
Kevin Wolf45aba422009-05-28 16:07:05 +020088{
Kevin Wolfff991292015-09-07 17:12:56 +020089 BDRVQcow2State *s = bs->opaque;
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +020090 int new_l1_size2, ret, i;
Kevin Wolf45aba422009-05-28 16:07:05 +020091 uint64_t *new_l1_table;
Max Reitzfda74f82013-09-30 17:57:21 +020092 int64_t old_l1_table_offset, old_l1_size;
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +020093 int64_t new_l1_table_offset, new_l1_size;
Kevin Wolf45aba422009-05-28 16:07:05 +020094 uint8_t data[12];
95
Stefan Hajnoczi72893752010-10-18 16:53:53 +010096 if (min_size <= s->l1_size)
Kevin Wolf45aba422009-05-28 16:07:05 +020097 return 0;
Stefan Hajnoczi72893752010-10-18 16:53:53 +010098
Max Reitzb93f9952014-04-29 19:03:14 +020099 /* Do a sanity check on min_size before trying to calculate new_l1_size
100 * (this prevents overflows during the while loop for the calculation of
101 * new_l1_size) */
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200102 if (min_size > INT_MAX / L1E_SIZE) {
Max Reitzb93f9952014-04-29 19:03:14 +0200103 return -EFBIG;
104 }
105
Stefan Hajnoczi72893752010-10-18 16:53:53 +0100106 if (exact_size) {
107 new_l1_size = min_size;
108 } else {
109 /* Bump size up to reduce the number of times we have to grow */
110 new_l1_size = s->l1_size;
111 if (new_l1_size == 0) {
112 new_l1_size = 1;
113 }
114 while (min_size > new_l1_size) {
Marc-André Lureau21cf3e12017-06-22 13:04:16 +0200115 new_l1_size = DIV_ROUND_UP(new_l1_size * 3, 2);
Stefan Hajnoczi72893752010-10-18 16:53:53 +0100116 }
Stefan Weild191d122009-10-26 16:11:16 +0100117 }
Stefan Hajnoczi72893752010-10-18 16:53:53 +0100118
Max Reitz84c26522016-06-15 17:36:30 +0200119 QEMU_BUILD_BUG_ON(QCOW_MAX_L1_SIZE > INT_MAX);
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200120 if (new_l1_size > QCOW_MAX_L1_SIZE / L1E_SIZE) {
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +0200121 return -EFBIG;
122 }
123
Kevin Wolf45aba422009-05-28 16:07:05 +0200124#ifdef DEBUG_ALLOC2
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +0200125 fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n",
126 s->l1_size, new_l1_size);
Kevin Wolf45aba422009-05-28 16:07:05 +0200127#endif
128
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200129 new_l1_size2 = L1E_SIZE * new_l1_size;
Alberto Garciaef97d602020-01-18 20:09:26 +0100130 new_l1_table = qemu_try_blockalign(bs->file->bs, new_l1_size2);
Kevin Wolfde828152014-05-20 17:12:47 +0200131 if (new_l1_table == NULL) {
132 return -ENOMEM;
133 }
Alberto Garciaef97d602020-01-18 20:09:26 +0100134 memset(new_l1_table, 0, new_l1_size2);
Kevin Wolfde828152014-05-20 17:12:47 +0200135
Stefan Hajnoczi0647d472016-09-13 09:56:27 +0100136 if (s->l1_size) {
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200137 memcpy(new_l1_table, s->l1_table, s->l1_size * L1E_SIZE);
Stefan Hajnoczi0647d472016-09-13 09:56:27 +0100138 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200139
140 /* write new table (align to cluster) */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200141 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE);
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200142 new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
Kevin Wolf5d757b52010-01-20 15:04:01 +0100143 if (new_l1_table_offset < 0) {
Kevin Wolfde828152014-05-20 17:12:47 +0200144 qemu_vfree(new_l1_table);
Kevin Wolf5d757b52010-01-20 15:04:01 +0100145 return new_l1_table_offset;
146 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100147
148 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
149 if (ret < 0) {
Kevin Wolf80fa3342011-06-01 10:50:00 +0200150 goto fail;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100151 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200152
Max Reitzcf939802013-08-30 14:34:26 +0200153 /* the L1 position has not yet been updated, so these clusters must
154 * indeed be completely free */
Max Reitz231bb262013-10-10 11:09:23 +0200155 ret = qcow2_pre_write_overlap_check(bs, 0, new_l1_table_offset,
Kevin Wolf966b0002019-01-15 20:39:06 +0100156 new_l1_size2, false);
Max Reitzcf939802013-08-30 14:34:26 +0200157 if (ret < 0) {
158 goto fail;
159 }
160
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200161 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
Kevin Wolf45aba422009-05-28 16:07:05 +0200162 for(i = 0; i < s->l1_size; i++)
163 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
Alberto Faria32cc71d2022-06-09 16:27:36 +0100164 ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_size2,
165 new_l1_table, 0);
Kevin Wolf8b3b7202010-06-16 17:44:35 +0200166 if (ret < 0)
Kevin Wolf45aba422009-05-28 16:07:05 +0200167 goto fail;
168 for(i = 0; i < s->l1_size; i++)
169 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
170
171 /* set new table */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200172 BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
Peter Maydellf1f7a1d2016-06-16 17:06:17 +0100173 stl_be_p(data, new_l1_size);
Peter Maydelle4ef9f42013-11-05 16:38:36 +0000174 stq_be_p(data + 4, new_l1_table_offset);
Alberto Faria32cc71d2022-06-09 16:27:36 +0100175 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size),
176 sizeof(data), data, 0);
Kevin Wolf8b3b7202010-06-16 17:44:35 +0200177 if (ret < 0) {
Kevin Wolf45aba422009-05-28 16:07:05 +0200178 goto fail;
Kevin Wolffb8fa772010-01-20 15:02:58 +0100179 }
Kevin Wolfde828152014-05-20 17:12:47 +0200180 qemu_vfree(s->l1_table);
Max Reitzfda74f82013-09-30 17:57:21 +0200181 old_l1_table_offset = s->l1_table_offset;
Kevin Wolf45aba422009-05-28 16:07:05 +0200182 s->l1_table_offset = new_l1_table_offset;
183 s->l1_table = new_l1_table;
Max Reitzfda74f82013-09-30 17:57:21 +0200184 old_l1_size = s->l1_size;
Kevin Wolf45aba422009-05-28 16:07:05 +0200185 s->l1_size = new_l1_size;
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200186 qcow2_free_clusters(bs, old_l1_table_offset, old_l1_size * L1E_SIZE,
Max Reitzfda74f82013-09-30 17:57:21 +0200187 QCOW2_DISCARD_OTHER);
Kevin Wolf45aba422009-05-28 16:07:05 +0200188 return 0;
189 fail:
Kevin Wolfde828152014-05-20 17:12:47 +0200190 qemu_vfree(new_l1_table);
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200191 qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2,
192 QCOW2_DISCARD_OTHER);
Kevin Wolf8b3b7202010-06-16 17:44:35 +0200193 return ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200194}
195
Kevin Wolf45aba422009-05-28 16:07:05 +0200196/*
197 * l2_load
198 *
Alberto Garciae2b57132018-02-05 16:33:15 +0200199 * @bs: The BlockDriverState
200 * @offset: A guest offset, used to calculate what slice of the L2
201 * table to load.
202 * @l2_offset: Offset to the L2 table in the image file.
203 * @l2_slice: Location to store the pointer to the L2 slice.
Kevin Wolf45aba422009-05-28 16:07:05 +0200204 *
Alberto Garciae2b57132018-02-05 16:33:15 +0200205 * Loads a L2 slice into memory (L2 slices are the parts of L2 tables
206 * that are loaded by the qcow2 cache). If the slice is in the cache,
207 * the cache is used; otherwise the L2 slice is loaded from the image
208 * file.
Kevin Wolf45aba422009-05-28 16:07:05 +0200209 */
Alberto Garciae2b57132018-02-05 16:33:15 +0200210static int l2_load(BlockDriverState *bs, uint64_t offset,
211 uint64_t l2_offset, uint64_t **l2_slice)
Kevin Wolf45aba422009-05-28 16:07:05 +0200212{
Kevin Wolfff991292015-09-07 17:12:56 +0200213 BDRVQcow2State *s = bs->opaque;
Alberto Garciac8fd8552020-07-10 18:12:54 +0200214 int start_of_slice = l2_entry_size(s) *
Alberto Garciae2b57132018-02-05 16:33:15 +0200215 (offset_to_l2_index(s, offset) - offset_to_l2_slice_index(s, offset));
Kevin Wolf45aba422009-05-28 16:07:05 +0200216
Alberto Garciae2b57132018-02-05 16:33:15 +0200217 return qcow2_cache_get(bs, s->l2_table_cache, l2_offset + start_of_slice,
218 (void **)l2_slice);
Kevin Wolf45aba422009-05-28 16:07:05 +0200219}
220
221/*
Alberto Garciada86f8c2020-01-18 20:09:28 +0100222 * Writes an L1 entry to disk (note that depending on the alignment
223 * requirements this function may write more that just one entry in
224 * order to prevent bdrv_pwrite from performing a read-modify-write)
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200225 */
Max Reitze23e4002013-08-30 14:34:28 +0200226int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200227{
Kevin Wolfff991292015-09-07 17:12:56 +0200228 BDRVQcow2State *s = bs->opaque;
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200229 int l1_start_index;
Kevin Wolff7defcb2010-03-23 17:28:22 +0100230 int i, ret;
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200231 int bufsize = MAX(L1E_SIZE,
Alberto Garciada86f8c2020-01-18 20:09:28 +0100232 MIN(bs->file->bs->bl.request_alignment, s->cluster_size));
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200233 int nentries = bufsize / L1E_SIZE;
Alberto Garciada86f8c2020-01-18 20:09:28 +0100234 g_autofree uint64_t *buf = g_try_new0(uint64_t, nentries);
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200235
Alberto Garciada86f8c2020-01-18 20:09:28 +0100236 if (buf == NULL) {
237 return -ENOMEM;
238 }
239
240 l1_start_index = QEMU_ALIGN_DOWN(l1_index, nentries);
241 for (i = 0; i < MIN(nentries, s->l1_size - l1_start_index); i++) {
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200242 buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]);
243 }
244
Max Reitz231bb262013-10-10 11:09:23 +0200245 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
Alberto Garciac508c732020-09-28 18:23:33 +0200246 s->l1_table_offset + L1E_SIZE * l1_start_index, bufsize, false);
Max Reitzcf939802013-08-30 14:34:26 +0200247 if (ret < 0) {
248 return ret;
249 }
250
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200251 BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200252 ret = bdrv_pwrite_sync(bs->file,
Alberto Garciac508c732020-09-28 18:23:33 +0200253 s->l1_table_offset + L1E_SIZE * l1_start_index,
Alberto Faria32cc71d2022-06-09 16:27:36 +0100254 bufsize, buf, 0);
Kevin Wolff7defcb2010-03-23 17:28:22 +0100255 if (ret < 0) {
256 return ret;
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200257 }
258
259 return 0;
260}
261
262/*
Kevin Wolf45aba422009-05-28 16:07:05 +0200263 * l2_allocate
264 *
265 * Allocate a new l2 entry in the file. If l1_index points to an already
266 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
267 * table) copy the contents of the old L2 table into the newly allocated one.
268 * Otherwise the new table is initialized with zeros.
269 *
270 */
271
Alberto Garcia38619462018-02-05 16:33:17 +0200272static int l2_allocate(BlockDriverState *bs, int l1_index)
Kevin Wolf45aba422009-05-28 16:07:05 +0200273{
Kevin Wolfff991292015-09-07 17:12:56 +0200274 BDRVQcow2State *s = bs->opaque;
Kevin Wolf6583e3c2009-06-16 11:31:28 +0200275 uint64_t old_l2_offset;
Alberto Garcia38619462018-02-05 16:33:17 +0200276 uint64_t *l2_slice = NULL;
277 unsigned slice, slice_size2, n_slices;
Kevin Wolff4f0d392010-02-02 15:20:57 +0100278 int64_t l2_offset;
Kevin Wolfc46e1162010-03-23 17:41:24 +0100279 int ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200280
281 old_l2_offset = s->l1_table[l1_index];
282
Kevin Wolf3cce16f2012-03-01 18:36:21 +0100283 trace_qcow2_l2_allocate(bs, l1_index);
284
Kevin Wolf45aba422009-05-28 16:07:05 +0200285 /* allocate a new l2 entry */
286
Alberto Garciac8fd8552020-07-10 18:12:54 +0200287 l2_offset = qcow2_alloc_clusters(bs, s->l2_size * l2_entry_size(s));
Kevin Wolf5d757b52010-01-20 15:04:01 +0100288 if (l2_offset < 0) {
Max Reitzbe0b7422013-09-25 16:37:20 +0200289 ret = l2_offset;
290 goto fail;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100291 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100292
Alberto Garciac1c43992019-02-08 17:44:53 +0200293 /* The offset must fit in the offset field of the L1 table entry */
294 assert((l2_offset & L1E_OFFSET_MASK) == l2_offset);
295
Alberto Garcia98839752017-11-03 16:18:51 +0200296 /* If we're allocating the table at offset 0 then something is wrong */
297 if (l2_offset == 0) {
298 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
299 "allocation of L2 table at offset 0");
300 ret = -EIO;
301 goto fail;
302 }
303
Kevin Wolf29c1a732011-01-10 17:17:28 +0100304 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
305 if (ret < 0) {
306 goto fail;
307 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200308
Kevin Wolf45aba422009-05-28 16:07:05 +0200309 /* allocate a new entry in the l2 cache */
310
Alberto Garciac8fd8552020-07-10 18:12:54 +0200311 slice_size2 = s->l2_slice_size * l2_entry_size(s);
Alberto Garcia38619462018-02-05 16:33:17 +0200312 n_slices = s->cluster_size / slice_size2;
313
Kevin Wolf3cce16f2012-03-01 18:36:21 +0100314 trace_qcow2_l2_allocate_get_empty(bs, l1_index);
Alberto Garcia38619462018-02-05 16:33:17 +0200315 for (slice = 0; slice < n_slices; slice++) {
Alberto Garcia6580bb02018-02-05 16:33:16 +0200316 ret = qcow2_cache_get_empty(bs, s->l2_table_cache,
Alberto Garcia38619462018-02-05 16:33:17 +0200317 l2_offset + slice * slice_size2,
318 (void **) &l2_slice);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100319 if (ret < 0) {
320 goto fail;
321 }
322
Alberto Garcia6580bb02018-02-05 16:33:16 +0200323 if ((old_l2_offset & L1E_OFFSET_MASK) == 0) {
Alberto Garcia38619462018-02-05 16:33:17 +0200324 /* if there was no old l2 table, clear the new slice */
325 memset(l2_slice, 0, slice_size2);
Alberto Garcia6580bb02018-02-05 16:33:16 +0200326 } else {
Alberto Garcia38619462018-02-05 16:33:17 +0200327 uint64_t *old_slice;
328 uint64_t old_l2_slice_offset =
329 (old_l2_offset & L1E_OFFSET_MASK) + slice * slice_size2;
Alberto Garcia6580bb02018-02-05 16:33:16 +0200330
Alberto Garcia38619462018-02-05 16:33:17 +0200331 /* if there was an old l2 table, read a slice from the disk */
Alberto Garcia6580bb02018-02-05 16:33:16 +0200332 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
Alberto Garcia38619462018-02-05 16:33:17 +0200333 ret = qcow2_cache_get(bs, s->l2_table_cache, old_l2_slice_offset,
334 (void **) &old_slice);
Alberto Garcia6580bb02018-02-05 16:33:16 +0200335 if (ret < 0) {
336 goto fail;
337 }
338
Alberto Garcia38619462018-02-05 16:33:17 +0200339 memcpy(l2_slice, old_slice, slice_size2);
Alberto Garcia6580bb02018-02-05 16:33:16 +0200340
Alberto Garcia38619462018-02-05 16:33:17 +0200341 qcow2_cache_put(s->l2_table_cache, (void **) &old_slice);
Alberto Garcia6580bb02018-02-05 16:33:16 +0200342 }
343
Alberto Garcia38619462018-02-05 16:33:17 +0200344 /* write the l2 slice to the file */
Alberto Garcia6580bb02018-02-05 16:33:16 +0200345 BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
346
347 trace_qcow2_l2_allocate_write_l2(bs, l1_index);
Alberto Garcia38619462018-02-05 16:33:17 +0200348 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
349 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf45aba422009-05-28 16:07:05 +0200350 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100351
Kevin Wolf29c1a732011-01-10 17:17:28 +0100352 ret = qcow2_cache_flush(bs, s->l2_table_cache);
Kevin Wolfc46e1162010-03-23 17:41:24 +0100353 if (ret < 0) {
Kevin Wolf175e1152010-05-12 16:23:26 +0200354 goto fail;
355 }
356
357 /* update the L1 entry */
Kevin Wolf3cce16f2012-03-01 18:36:21 +0100358 trace_qcow2_l2_allocate_write_l1(bs, l1_index);
Kevin Wolf175e1152010-05-12 16:23:26 +0200359 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
Max Reitze23e4002013-08-30 14:34:28 +0200360 ret = qcow2_write_l1_entry(bs, l1_index);
Kevin Wolf175e1152010-05-12 16:23:26 +0200361 if (ret < 0) {
362 goto fail;
Kevin Wolfc46e1162010-03-23 17:41:24 +0100363 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200364
Kevin Wolf3cce16f2012-03-01 18:36:21 +0100365 trace_qcow2_l2_allocate_done(bs, l1_index, 0);
Kevin Wolfc46e1162010-03-23 17:41:24 +0100366 return 0;
Kevin Wolf175e1152010-05-12 16:23:26 +0200367
368fail:
Kevin Wolf3cce16f2012-03-01 18:36:21 +0100369 trace_qcow2_l2_allocate_done(bs, l1_index, ret);
Alberto Garcia38619462018-02-05 16:33:17 +0200370 if (l2_slice != NULL) {
371 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Max Reitz8585afd2013-09-25 16:37:18 +0200372 }
Kevin Wolf68dba0b2010-06-07 16:43:22 +0200373 s->l1_table[l1_index] = old_l2_offset;
Max Reitze3b21ef2013-09-25 16:37:19 +0200374 if (l2_offset > 0) {
Alberto Garciac8fd8552020-07-10 18:12:54 +0200375 qcow2_free_clusters(bs, l2_offset, s->l2_size * l2_entry_size(s),
Max Reitze3b21ef2013-09-25 16:37:19 +0200376 QCOW2_DISCARD_ALWAYS);
377 }
Kevin Wolf175e1152010-05-12 16:23:26 +0200378 return ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200379}
380
Kevin Wolf2bfcc4a2012-03-15 16:37:40 +0100381/*
Alberto Garcia70d1cba2020-07-10 18:12:57 +0200382 * For a given L2 entry, count the number of contiguous subclusters of
383 * the same type starting from @sc_from. Compressed clusters are
384 * treated as if they were divided into subclusters of size
385 * s->subcluster_size.
386 *
387 * Return the number of contiguous subclusters and set @type to the
388 * subcluster type.
389 *
390 * If the L2 entry is invalid return -errno and set @type to
391 * QCOW2_SUBCLUSTER_INVALID.
392 */
Alberto Garcia70d1cba2020-07-10 18:12:57 +0200393static int qcow2_get_subcluster_range_type(BlockDriverState *bs,
394 uint64_t l2_entry,
395 uint64_t l2_bitmap,
396 unsigned sc_from,
397 QCow2SubclusterType *type)
398{
399 BDRVQcow2State *s = bs->opaque;
400 uint32_t val;
401
402 *type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_from);
403
404 if (*type == QCOW2_SUBCLUSTER_INVALID) {
405 return -EINVAL;
406 } else if (!has_subclusters(s) || *type == QCOW2_SUBCLUSTER_COMPRESSED) {
407 return s->subclusters_per_cluster - sc_from;
408 }
409
410 switch (*type) {
411 case QCOW2_SUBCLUSTER_NORMAL:
412 val = l2_bitmap | QCOW_OFLAG_SUB_ALLOC_RANGE(0, sc_from);
413 return cto32(val) - sc_from;
414
415 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
416 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
417 val = (l2_bitmap | QCOW_OFLAG_SUB_ZERO_RANGE(0, sc_from)) >> 32;
418 return cto32(val) - sc_from;
419
420 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
421 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
422 val = ((l2_bitmap >> 32) | l2_bitmap)
423 & ~QCOW_OFLAG_SUB_ALLOC_RANGE(0, sc_from);
424 return ctz32(val) - sc_from;
425
426 default:
427 g_assert_not_reached();
428 }
429}
430
431/*
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200432 * Return the number of contiguous subclusters of the exact same type
433 * in a given L2 slice, starting from cluster @l2_index, subcluster
434 * @sc_index. Allocated subclusters are required to be contiguous in
435 * the image file.
436 * At most @nb_clusters are checked (note that this means clusters,
437 * not subclusters).
438 * Compressed clusters are always processed one by one but for the
439 * purpose of this count they are treated as if they were divided into
440 * subclusters of size s->subcluster_size.
441 * On failure return -errno and update @l2_index to point to the
442 * invalid entry.
Kevin Wolf2bfcc4a2012-03-15 16:37:40 +0100443 */
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200444static int count_contiguous_subclusters(BlockDriverState *bs, int nb_clusters,
445 unsigned sc_index, uint64_t *l2_slice,
446 unsigned *l2_index)
Kevin Wolf45aba422009-05-28 16:07:05 +0200447{
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200448 BDRVQcow2State *s = bs->opaque;
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200449 int i, count = 0;
450 bool check_offset = false;
451 uint64_t expected_offset = 0;
452 QCow2SubclusterType expected_type = QCOW2_SUBCLUSTER_NORMAL, type;
Kevin Wolf45aba422009-05-28 16:07:05 +0200453
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200454 assert(*l2_index + nb_clusters <= s->l2_slice_size);
Max Reitz15684a42013-09-27 12:14:15 +0200455
Kevin Wolf61653002013-09-27 13:36:11 +0200456 for (i = 0; i < nb_clusters; i++) {
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200457 unsigned first_sc = (i == 0) ? sc_index : 0;
458 uint64_t l2_entry = get_l2_entry(s, l2_slice, *l2_index + i);
459 uint64_t l2_bitmap = get_l2_bitmap(s, l2_slice, *l2_index + i);
460 int ret = qcow2_get_subcluster_range_type(bs, l2_entry, l2_bitmap,
461 first_sc, &type);
462 if (ret < 0) {
463 *l2_index += i; /* Point to the invalid entry */
464 return -EIO;
465 }
466 if (i == 0) {
467 if (type == QCOW2_SUBCLUSTER_COMPRESSED) {
468 /* Compressed clusters are always processed one by one */
469 return ret;
470 }
471 expected_type = type;
472 expected_offset = l2_entry & L2E_OFFSET_MASK;
473 check_offset = (type == QCOW2_SUBCLUSTER_NORMAL ||
474 type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
475 type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC);
476 } else if (type != expected_type) {
477 break;
478 } else if (check_offset) {
479 expected_offset += s->cluster_size;
480 if (expected_offset != (l2_entry & L2E_OFFSET_MASK)) {
481 break;
482 }
483 }
484 count += ret;
485 /* Stop if there are type changes before the end of the cluster */
486 if (first_sc + ret < s->subclusters_per_cluster) {
Kevin Wolf45aba422009-05-28 16:07:05 +0200487 break;
Kevin Wolf2bfcc4a2012-03-15 16:37:40 +0100488 }
489 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200490
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200491 return count;
Kevin Wolf45aba422009-05-28 16:07:05 +0200492}
493
Kevin Wolf7b1fb722023-02-03 16:21:49 +0100494static int coroutine_fn GRAPH_RDLOCK
495do_perform_cow_read(BlockDriverState *bs, uint64_t src_cluster_offset,
496 unsigned offset_in_cluster, QEMUIOVector *qiov)
Kevin Wolf45aba422009-05-28 16:07:05 +0200497{
Kevin Wolfaaa4d202016-06-01 15:21:05 +0200498 int ret;
Kevin Wolf1b9f1492011-09-19 11:26:48 +0200499
Alberto Garcia86b862c2017-06-19 16:40:07 +0300500 if (qiov->size == 0) {
Alberto Garcia99450c62017-06-19 16:40:04 +0300501 return 0;
502 }
503
Paolo Bonzini17362392023-06-01 13:51:45 +0200504 BLKDBG_CO_EVENT(bs->file, BLKDBG_COW_READ);
Stefan Hajnocziaef4acb2011-11-30 12:23:41 +0000505
Max Reitzdba28552014-03-10 23:44:07 +0100506 if (!bs->drv) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300507 return -ENOMEDIUM;
Max Reitzdba28552014-03-10 23:44:07 +0100508 }
509
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300510 /*
511 * We never deal with requests that don't satisfy
512 * bdrv_check_qiov_request(), and aligning requests to clusters never
513 * breaks this condition. So, do some assertions before calling
514 * bs->drv->bdrv_co_preadv_part() which has int64_t arguments.
515 */
516 assert(src_cluster_offset <= INT64_MAX);
517 assert(src_cluster_offset + offset_in_cluster <= INT64_MAX);
Hanna Reitze7e588d2021-10-11 17:50:31 +0200518 /* Cast qiov->size to uint64_t to silence a compiler warning on -m32 */
519 assert((uint64_t)qiov->size <= INT64_MAX);
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300520 bdrv_check_qiov_request(src_cluster_offset + offset_in_cluster, qiov->size,
521 qiov, 0, &error_abort);
522 /*
523 * Call .bdrv_co_readv() directly instead of using the public block-layer
Stefan Hajnocziaef4acb2011-11-30 12:23:41 +0000524 * interface. This avoids double I/O throttling and request tracking,
525 * which can lead to deadlock when block layer copy-on-read is enabled.
526 */
Vladimir Sementsov-Ogievskiydf893d22019-06-04 19:15:13 +0300527 ret = bs->drv->bdrv_co_preadv_part(bs,
528 src_cluster_offset + offset_in_cluster,
529 qiov->size, qiov, 0, 0);
Kevin Wolf1b9f1492011-09-19 11:26:48 +0200530 if (ret < 0) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300531 return ret;
Kevin Wolf1b9f1492011-09-19 11:26:48 +0200532 }
533
Alberto Garcia672f0f22017-06-19 16:40:05 +0300534 return 0;
535}
536
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100537static int coroutine_fn GRAPH_RDLOCK
538do_perform_cow_write(BlockDriverState *bs, uint64_t cluster_offset,
539 unsigned offset_in_cluster, QEMUIOVector *qiov)
Alberto Garcia672f0f22017-06-19 16:40:05 +0300540{
Kevin Wolf966b0002019-01-15 20:39:06 +0100541 BDRVQcow2State *s = bs->opaque;
Alberto Garcia672f0f22017-06-19 16:40:05 +0300542 int ret;
543
Alberto Garcia86b862c2017-06-19 16:40:07 +0300544 if (qiov->size == 0) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300545 return 0;
546 }
547
Max Reitz231bb262013-10-10 11:09:23 +0200548 ret = qcow2_pre_write_overlap_check(bs, 0,
Kevin Wolf966b0002019-01-15 20:39:06 +0100549 cluster_offset + offset_in_cluster, qiov->size, true);
Max Reitzcf939802013-08-30 14:34:26 +0200550 if (ret < 0) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300551 return ret;
Max Reitzcf939802013-08-30 14:34:26 +0200552 }
553
Paolo Bonzini17362392023-06-01 13:51:45 +0200554 BLKDBG_CO_EVENT(bs->file, BLKDBG_COW_WRITE);
Kevin Wolf966b0002019-01-15 20:39:06 +0100555 ret = bdrv_co_pwritev(s->data_file, cluster_offset + offset_in_cluster,
Alberto Garcia86b862c2017-06-19 16:40:07 +0300556 qiov->size, qiov, 0);
Kevin Wolf1b9f1492011-09-19 11:26:48 +0200557 if (ret < 0) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300558 return ret;
Kevin Wolf1b9f1492011-09-19 11:26:48 +0200559 }
560
Alberto Garcia672f0f22017-06-19 16:40:05 +0300561 return 0;
Kevin Wolf45aba422009-05-28 16:07:05 +0200562}
563
564
565/*
Alberto Garcia388e5812020-07-10 18:12:44 +0200566 * get_host_offset
Kevin Wolf45aba422009-05-28 16:07:05 +0200567 *
Alberto Garcia388e5812020-07-10 18:12:44 +0200568 * For a given offset of the virtual disk find the equivalent host
569 * offset in the qcow2 file and store it in *host_offset. Neither
570 * offset needs to be aligned to a cluster boundary.
571 *
572 * If the cluster is unallocated then *host_offset will be 0.
Vladimir Sementsov-Ogievskiy9a3978a2021-09-14 15:24:46 +0300573 * If the cluster is compressed then *host_offset will contain the l2 entry.
Kevin Wolf45aba422009-05-28 16:07:05 +0200574 *
Kevin Wolfecfe1862016-05-31 16:13:07 +0200575 * On entry, *bytes is the maximum number of contiguous bytes starting at
576 * offset that we are interested in.
Kevin Wolf45aba422009-05-28 16:07:05 +0200577 *
Kevin Wolfecfe1862016-05-31 16:13:07 +0200578 * On exit, *bytes is the number of bytes starting at offset that have the same
Alberto Garcia10dabdc52020-07-10 18:13:00 +0200579 * subcluster type and (if applicable) are stored contiguously in the image
580 * file. The subcluster type is stored in *subcluster_type.
581 * Compressed clusters are always processed one by one.
Kevin Wolf45aba422009-05-28 16:07:05 +0200582 *
Alberto Garciaca4a0bb2020-07-10 18:12:59 +0200583 * Returns 0 on success, -errno in error cases.
Kevin Wolf45aba422009-05-28 16:07:05 +0200584 */
Alberto Garcia388e5812020-07-10 18:12:44 +0200585int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
Alberto Garciaca4a0bb2020-07-10 18:12:59 +0200586 unsigned int *bytes, uint64_t *host_offset,
Alberto Garcia10dabdc52020-07-10 18:13:00 +0200587 QCow2SubclusterType *subcluster_type)
Kevin Wolf45aba422009-05-28 16:07:05 +0200588{
Kevin Wolfff991292015-09-07 17:12:56 +0200589 BDRVQcow2State *s = bs->opaque;
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200590 unsigned int l2_index, sc_index;
591 uint64_t l1_index, l2_offset, *l2_slice, l2_entry, l2_bitmap;
592 int sc;
Max Reitzc834cba2016-06-20 16:26:23 +0200593 unsigned int offset_in_cluster;
594 uint64_t bytes_available, bytes_needed, nb_clusters;
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200595 QCow2SubclusterType type;
Kevin Wolf55c17e92010-05-21 18:25:20 +0200596 int ret;
Kevin Wolfb2f65d62016-05-31 16:41:09 +0200597
598 offset_in_cluster = offset_into_cluster(s, offset);
Kevin Wolfecfe1862016-05-31 16:13:07 +0200599 bytes_needed = (uint64_t) *bytes + offset_in_cluster;
Kevin Wolf45aba422009-05-28 16:07:05 +0200600
Kevin Wolfb2f65d62016-05-31 16:41:09 +0200601 /* compute how many bytes there are between the start of the cluster
Alberto Garciafd630032018-02-05 16:33:20 +0200602 * containing offset and the end of the l2 slice that contains
603 * the entry pointing to it */
604 bytes_available =
605 ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
606 << s->cluster_bits;
Kevin Wolf45aba422009-05-28 16:07:05 +0200607
Kevin Wolfb2f65d62016-05-31 16:41:09 +0200608 if (bytes_needed > bytes_available) {
609 bytes_needed = bytes_available;
Kevin Wolf45aba422009-05-28 16:07:05 +0200610 }
611
Alberto Garcia388e5812020-07-10 18:12:44 +0200612 *host_offset = 0;
Kevin Wolf45aba422009-05-28 16:07:05 +0200613
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100614 /* seek to the l2 offset in the l1 table */
Kevin Wolf45aba422009-05-28 16:07:05 +0200615
Alberto Garcia05b5b6e2018-02-05 16:33:12 +0200616 l1_index = offset_to_l1_index(s, offset);
Kevin Wolf68d000a2012-03-14 19:15:03 +0100617 if (l1_index >= s->l1_size) {
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200618 type = QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
Kevin Wolf45aba422009-05-28 16:07:05 +0200619 goto out;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100620 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200621
Kevin Wolf68d000a2012-03-14 19:15:03 +0100622 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
623 if (!l2_offset) {
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200624 type = QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
Kevin Wolf45aba422009-05-28 16:07:05 +0200625 goto out;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100626 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200627
Max Reitza97c67e2014-09-05 16:07:18 +0200628 if (offset_into_cluster(s, l2_offset)) {
629 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
630 " unaligned (L1 index: %#" PRIx64 ")",
631 l2_offset, l1_index);
632 return -EIO;
633 }
634
Alberto Garciafd630032018-02-05 16:33:20 +0200635 /* load the l2 slice in memory */
Kevin Wolf45aba422009-05-28 16:07:05 +0200636
Alberto Garciafd630032018-02-05 16:33:20 +0200637 ret = l2_load(bs, offset, l2_offset, &l2_slice);
Kevin Wolf55c17e92010-05-21 18:25:20 +0200638 if (ret < 0) {
639 return ret;
Kevin Wolf1c46efa2010-05-21 17:59:36 +0200640 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200641
642 /* find the cluster offset for the given disk offset */
643
Alberto Garciafd630032018-02-05 16:33:20 +0200644 l2_index = offset_to_l2_slice_index(s, offset);
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200645 sc_index = offset_to_sc_index(s, offset);
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200646 l2_entry = get_l2_entry(s, l2_slice, l2_index);
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200647 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
Max Reitzb6d36de2015-09-14 16:39:47 +0200648
Kevin Wolfb2f65d62016-05-31 16:41:09 +0200649 nb_clusters = size_to_clusters(s, bytes_needed);
Max Reitzc834cba2016-06-20 16:26:23 +0200650 /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
651 * integers; the minimum cluster size is 512, so this assertion is always
652 * true */
653 assert(nb_clusters <= INT_MAX);
Kevin Wolf45aba422009-05-28 16:07:05 +0200654
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200655 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
656 if (s->qcow_version < 3 && (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
657 type == QCOW2_SUBCLUSTER_ZERO_ALLOC)) {
Eric Blakefdfab372017-05-06 19:05:46 -0500658 qcow2_signal_corruption(bs, true, -1, -1, "Zero cluster entry found"
659 " in pre-v3 image (L2 offset: %#" PRIx64
660 ", L2 index: %#x)", l2_offset, l2_index);
661 ret = -EIO;
662 goto fail;
663 }
Eric Blake3ef95212017-05-06 19:05:45 -0500664 switch (type) {
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200665 case QCOW2_SUBCLUSTER_INVALID:
666 break; /* This is handled by count_contiguous_subclusters() below */
667 case QCOW2_SUBCLUSTER_COMPRESSED:
Kevin Wolf966b0002019-01-15 20:39:06 +0100668 if (has_data_file(bs)) {
669 qcow2_signal_corruption(bs, true, -1, -1, "Compressed cluster "
670 "entry found in image with external data "
671 "file (L2 offset: %#" PRIx64 ", L2 index: "
672 "%#x)", l2_offset, l2_index);
673 ret = -EIO;
674 goto fail;
675 }
Vladimir Sementsov-Ogievskiy9a3978a2021-09-14 15:24:46 +0300676 *host_offset = l2_entry;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100677 break;
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200678 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
679 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
Kevin Wolf68d000a2012-03-14 19:15:03 +0100680 break;
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200681 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
682 case QCOW2_SUBCLUSTER_NORMAL:
683 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC: {
Alberto Garcia388e5812020-07-10 18:12:44 +0200684 uint64_t host_cluster_offset = l2_entry & L2E_OFFSET_MASK;
685 *host_offset = host_cluster_offset + offset_in_cluster;
Alberto Garcia388e5812020-07-10 18:12:44 +0200686 if (offset_into_cluster(s, host_cluster_offset)) {
Eric Blakefdfab372017-05-06 19:05:46 -0500687 qcow2_signal_corruption(bs, true, -1, -1,
688 "Cluster allocation offset %#"
Max Reitza97c67e2014-09-05 16:07:18 +0200689 PRIx64 " unaligned (L2 offset: %#" PRIx64
Alberto Garcia388e5812020-07-10 18:12:44 +0200690 ", L2 index: %#x)", host_cluster_offset,
Max Reitza97c67e2014-09-05 16:07:18 +0200691 l2_offset, l2_index);
692 ret = -EIO;
693 goto fail;
694 }
Alberto Garcia388e5812020-07-10 18:12:44 +0200695 if (has_data_file(bs) && *host_offset != offset) {
Kevin Wolf966b0002019-01-15 20:39:06 +0100696 qcow2_signal_corruption(bs, true, -1, -1,
697 "External data file host cluster offset %#"
698 PRIx64 " does not match guest cluster "
699 "offset: %#" PRIx64
Alberto Garcia388e5812020-07-10 18:12:44 +0200700 ", L2 index: %#x)", host_cluster_offset,
Kevin Wolf966b0002019-01-15 20:39:06 +0100701 offset - offset_in_cluster, l2_index);
702 ret = -EIO;
703 goto fail;
704 }
Kevin Wolf68d000a2012-03-14 19:15:03 +0100705 break;
Alberto Garcia388e5812020-07-10 18:12:44 +0200706 }
Kevin Wolf1417d7e2012-06-15 13:43:18 +0200707 default:
708 abort();
Kevin Wolf45aba422009-05-28 16:07:05 +0200709 }
710
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200711 sc = count_contiguous_subclusters(bs, nb_clusters, sc_index,
712 l2_slice, &l2_index);
713 if (sc < 0) {
714 qcow2_signal_corruption(bs, true, -1, -1, "Invalid cluster entry found "
715 " (L2 offset: %#" PRIx64 ", L2 index: %#x)",
716 l2_offset, l2_index);
717 ret = -EIO;
718 goto fail;
719 }
Alberto Garciafd630032018-02-05 16:33:20 +0200720 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100721
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200722 bytes_available = ((int64_t)sc + sc_index) << s->subcluster_bits;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100723
Kevin Wolf45aba422009-05-28 16:07:05 +0200724out:
Kevin Wolfb2f65d62016-05-31 16:41:09 +0200725 if (bytes_available > bytes_needed) {
726 bytes_available = bytes_needed;
727 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200728
Max Reitzc834cba2016-06-20 16:26:23 +0200729 /* bytes_available <= bytes_needed <= *bytes + offset_in_cluster;
730 * subtracting offset_in_cluster will therefore definitely yield something
731 * not exceeding UINT_MAX */
732 assert(bytes_available - offset_in_cluster <= UINT_MAX);
Kevin Wolfecfe1862016-05-31 16:13:07 +0200733 *bytes = bytes_available - offset_in_cluster;
Kevin Wolf45aba422009-05-28 16:07:05 +0200734
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200735 *subcluster_type = type;
Alberto Garciaca4a0bb2020-07-10 18:12:59 +0200736
737 return 0;
Max Reitza97c67e2014-09-05 16:07:18 +0200738
739fail:
Alberto Garciafd630032018-02-05 16:33:20 +0200740 qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
Max Reitza97c67e2014-09-05 16:07:18 +0200741 return ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200742}
743
744/*
745 * get_cluster_table
746 *
747 * for a given disk offset, load (and allocate if needed)
Alberto Garciac03bfc52018-02-05 16:33:19 +0200748 * the appropriate slice of its l2 table.
Kevin Wolf45aba422009-05-28 16:07:05 +0200749 *
Alberto Garciac03bfc52018-02-05 16:33:19 +0200750 * the cluster index in the l2 slice is given to the caller.
Kevin Wolf45aba422009-05-28 16:07:05 +0200751 *
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100752 * Returns 0 on success, -errno in failure case
Kevin Wolf45aba422009-05-28 16:07:05 +0200753 */
Kevin Wolf45aba422009-05-28 16:07:05 +0200754static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
Alberto Garciac03bfc52018-02-05 16:33:19 +0200755 uint64_t **new_l2_slice,
Kevin Wolf45aba422009-05-28 16:07:05 +0200756 int *new_l2_index)
757{
Kevin Wolfff991292015-09-07 17:12:56 +0200758 BDRVQcow2State *s = bs->opaque;
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +0200759 unsigned int l2_index;
760 uint64_t l1_index, l2_offset;
Alberto Garciac03bfc52018-02-05 16:33:19 +0200761 uint64_t *l2_slice = NULL;
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200762 int ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200763
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +0100764 /* seek to the l2 offset in the l1 table */
Kevin Wolf45aba422009-05-28 16:07:05 +0200765
Alberto Garcia05b5b6e2018-02-05 16:33:12 +0200766 l1_index = offset_to_l1_index(s, offset);
Kevin Wolf45aba422009-05-28 16:07:05 +0200767 if (l1_index >= s->l1_size) {
Stefan Hajnoczi72893752010-10-18 16:53:53 +0100768 ret = qcow2_grow_l1_table(bs, l1_index + 1, false);
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100769 if (ret < 0) {
770 return ret;
771 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200772 }
Kevin Wolf8e37f682012-02-23 15:40:55 +0100773
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +0200774 assert(l1_index < s->l1_size);
Kevin Wolf8e37f682012-02-23 15:40:55 +0100775 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
Max Reitza97c67e2014-09-05 16:07:18 +0200776 if (offset_into_cluster(s, l2_offset)) {
777 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#" PRIx64
778 " unaligned (L1 index: %#" PRIx64 ")",
779 l2_offset, l1_index);
780 return -EIO;
781 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200782
Alberto Garcia05f9ee462018-02-05 16:33:18 +0200783 if (!(s->l1_table[l1_index] & QCOW_OFLAG_COPIED)) {
Kevin Wolf16fde5f2011-02-09 17:36:19 +0100784 /* First allocate a new L2 table (and do COW if needed) */
Alberto Garcia38619462018-02-05 16:33:17 +0200785 ret = l2_allocate(bs, l1_index);
Kevin Wolfc46e1162010-03-23 17:41:24 +0100786 if (ret < 0) {
787 return ret;
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100788 }
Kevin Wolf16fde5f2011-02-09 17:36:19 +0100789
790 /* Then decrease the refcount of the old table */
791 if (l2_offset) {
Alberto Garciac8fd8552020-07-10 18:12:54 +0200792 qcow2_free_clusters(bs, l2_offset, s->l2_size * l2_entry_size(s),
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200793 QCOW2_DISCARD_OTHER);
Kevin Wolf16fde5f2011-02-09 17:36:19 +0100794 }
Alberto Garcia38619462018-02-05 16:33:17 +0200795
796 /* Get the offset of the newly-allocated l2 table */
797 l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
798 assert(offset_into_cluster(s, l2_offset) == 0);
Alberto Garcia05f9ee462018-02-05 16:33:18 +0200799 }
800
Alberto Garciac03bfc52018-02-05 16:33:19 +0200801 /* load the l2 slice in memory */
802 ret = l2_load(bs, offset, l2_offset, &l2_slice);
Alberto Garcia05f9ee462018-02-05 16:33:18 +0200803 if (ret < 0) {
804 return ret;
Kevin Wolf45aba422009-05-28 16:07:05 +0200805 }
806
807 /* find the cluster offset for the given disk offset */
808
Alberto Garciac03bfc52018-02-05 16:33:19 +0200809 l2_index = offset_to_l2_slice_index(s, offset);
Kevin Wolf45aba422009-05-28 16:07:05 +0200810
Alberto Garciac03bfc52018-02-05 16:33:19 +0200811 *new_l2_slice = l2_slice;
Kevin Wolf45aba422009-05-28 16:07:05 +0200812 *new_l2_index = l2_index;
813
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100814 return 0;
Kevin Wolf45aba422009-05-28 16:07:05 +0200815}
816
817/*
818 * alloc_compressed_cluster_offset
819 *
Kevin Wolf77e023f2019-02-27 10:26:24 +0100820 * For a given offset on the virtual disk, allocate a new compressed cluster
821 * and put the host offset of the cluster into *host_offset. If a cluster is
822 * already allocated at the offset, return an error.
Kevin Wolf45aba422009-05-28 16:07:05 +0200823 *
Kevin Wolf77e023f2019-02-27 10:26:24 +0100824 * Return 0 on success and -errno in error cases
Kevin Wolf45aba422009-05-28 16:07:05 +0200825 */
Paolo Bonzini70bacc42023-06-01 13:51:43 +0200826int coroutine_fn GRAPH_RDLOCK
827qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, uint64_t offset,
828 int compressed_size, uint64_t *host_offset)
Kevin Wolf45aba422009-05-28 16:07:05 +0200829{
Kevin Wolfff991292015-09-07 17:12:56 +0200830 BDRVQcow2State *s = bs->opaque;
Kevin Wolf45aba422009-05-28 16:07:05 +0200831 int l2_index, ret;
Alberto Garciae4e72542018-02-05 16:33:32 +0200832 uint64_t *l2_slice;
Kevin Wolff4f0d392010-02-02 15:20:57 +0100833 int64_t cluster_offset;
Kevin Wolf45aba422009-05-28 16:07:05 +0200834 int nb_csectors;
835
Kevin Wolf966b0002019-01-15 20:39:06 +0100836 if (has_data_file(bs)) {
837 return 0;
838 }
839
Alberto Garciae4e72542018-02-05 16:33:32 +0200840 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100841 if (ret < 0) {
Kevin Wolf77e023f2019-02-27 10:26:24 +0100842 return ret;
Kevin Wolf1e3e8f12010-01-20 15:03:00 +0100843 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200844
Kevin Wolfb0b68622012-03-15 17:20:11 +0100845 /* Compression can't overwrite anything. Fail if the cluster was already
846 * allocated. */
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200847 cluster_offset = get_l2_entry(s, l2_slice, l2_index);
Kevin Wolfb0b68622012-03-15 17:20:11 +0100848 if (cluster_offset & L2E_OFFSET_MASK) {
Alberto Garciae4e72542018-02-05 16:33:32 +0200849 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf77e023f2019-02-27 10:26:24 +0100850 return -EIO;
Kevin Wolf8f1efd02011-10-18 17:12:44 +0200851 }
Kevin Wolf45aba422009-05-28 16:07:05 +0200852
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200853 cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
Kevin Wolf5d757b52010-01-20 15:04:01 +0100854 if (cluster_offset < 0) {
Alberto Garciae4e72542018-02-05 16:33:32 +0200855 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf77e023f2019-02-27 10:26:24 +0100856 return cluster_offset;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100857 }
858
Alberto Garciab6c24692019-05-10 19:22:54 +0300859 nb_csectors =
860 (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
861 (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
Kevin Wolf45aba422009-05-28 16:07:05 +0200862
Alberto Garcia3a75a872020-01-13 17:11:46 +0100863 /* The offset and size must fit in their fields of the L2 table entry */
864 assert((cluster_offset & s->cluster_offset_mask) == cluster_offset);
865 assert((nb_csectors & s->csize_mask) == nb_csectors);
866
Kevin Wolf45aba422009-05-28 16:07:05 +0200867 cluster_offset |= QCOW_OFLAG_COMPRESSED |
868 ((uint64_t)nb_csectors << s->csize_shift);
869
870 /* update L2 table */
871
872 /* compressed clusters never have the copied flag */
873
Paolo Bonzini17362392023-06-01 13:51:45 +0200874 BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
Alberto Garciae4e72542018-02-05 16:33:32 +0200875 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200876 set_l2_entry(s, l2_slice, l2_index, cluster_offset);
Alberto Garciaff4cdec2020-07-10 18:13:08 +0200877 if (has_subclusters(s)) {
878 set_l2_bitmap(s, l2_slice, l2_index, 0);
879 }
Alberto Garciae4e72542018-02-05 16:33:32 +0200880 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf4c1612d2009-06-16 11:31:29 +0200881
Kevin Wolf77e023f2019-02-27 10:26:24 +0100882 *host_offset = cluster_offset & s->cluster_offset_mask;
883 return 0;
Kevin Wolf4c1612d2009-06-16 11:31:29 +0200884}
885
Kevin Wolf7b1fb722023-02-03 16:21:49 +0100886static int coroutine_fn GRAPH_RDLOCK
887perform_cow(BlockDriverState *bs, QCowL2Meta *m)
Kevin Wolf593fb832012-12-07 18:08:43 +0100888{
Kevin Wolfff991292015-09-07 17:12:56 +0200889 BDRVQcow2State *s = bs->opaque;
Alberto Garcia99450c62017-06-19 16:40:04 +0300890 Qcow2COWRegion *start = &m->cow_start;
891 Qcow2COWRegion *end = &m->cow_end;
Alberto Garcia672f0f22017-06-19 16:40:05 +0300892 unsigned buffer_size;
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300893 unsigned data_bytes = end->offset - (start->offset + start->nb_bytes);
894 bool merge_reads;
Alberto Garcia672f0f22017-06-19 16:40:05 +0300895 uint8_t *start_buffer, *end_buffer;
Alberto Garcia86b862c2017-06-19 16:40:07 +0300896 QEMUIOVector qiov;
Kevin Wolf593fb832012-12-07 18:08:43 +0100897 int ret;
898
Alberto Garcia672f0f22017-06-19 16:40:05 +0300899 assert(start->nb_bytes <= UINT_MAX - end->nb_bytes);
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300900 assert(start->nb_bytes + end->nb_bytes <= UINT_MAX - data_bytes);
901 assert(start->offset + start->nb_bytes <= end->offset);
Alberto Garcia672f0f22017-06-19 16:40:05 +0300902
Anton Nefedovc8bb23c2019-05-16 17:27:49 +0300903 if ((start->nb_bytes == 0 && end->nb_bytes == 0) || m->skip_cow) {
Kevin Wolf593fb832012-12-07 18:08:43 +0100904 return 0;
905 }
906
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300907 /* If we have to read both the start and end COW regions and the
908 * middle region is not too large then perform just one read
909 * operation */
910 merge_reads = start->nb_bytes && end->nb_bytes && data_bytes <= 16384;
911 if (merge_reads) {
912 buffer_size = start->nb_bytes + data_bytes + end->nb_bytes;
913 } else {
914 /* If we have to do two reads, add some padding in the middle
915 * if necessary to make sure that the end region is optimally
916 * aligned. */
917 size_t align = bdrv_opt_mem_align(bs);
918 assert(align > 0 && align <= UINT_MAX);
919 assert(QEMU_ALIGN_UP(start->nb_bytes, align) <=
920 UINT_MAX - end->nb_bytes);
921 buffer_size = QEMU_ALIGN_UP(start->nb_bytes, align) + end->nb_bytes;
922 }
923
924 /* Reserve a buffer large enough to store all the data that we're
925 * going to read */
Alberto Garcia672f0f22017-06-19 16:40:05 +0300926 start_buffer = qemu_try_blockalign(bs, buffer_size);
927 if (start_buffer == NULL) {
928 return -ENOMEM;
929 }
930 /* The part of the buffer where the end region is located */
931 end_buffer = start_buffer + buffer_size - end->nb_bytes;
932
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300933 qemu_iovec_init(&qiov, 2 + (m->data_qiov ?
934 qemu_iovec_subvec_niov(m->data_qiov,
935 m->data_qiov_offset,
936 data_bytes)
937 : 0));
Alberto Garcia86b862c2017-06-19 16:40:07 +0300938
Kevin Wolf593fb832012-12-07 18:08:43 +0100939 qemu_co_mutex_unlock(&s->lock);
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300940 /* First we read the existing data from both COW regions. We
941 * either read the whole region in one go, or the start and end
942 * regions separately. */
943 if (merge_reads) {
Alberto Garcia86b862c2017-06-19 16:40:07 +0300944 qemu_iovec_add(&qiov, start_buffer, buffer_size);
945 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300946 } else {
Alberto Garcia86b862c2017-06-19 16:40:07 +0300947 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
948 ret = do_perform_cow_read(bs, m->offset, start->offset, &qiov);
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300949 if (ret < 0) {
950 goto fail;
951 }
Kevin Wolf593fb832012-12-07 18:08:43 +0100952
Alberto Garcia86b862c2017-06-19 16:40:07 +0300953 qemu_iovec_reset(&qiov);
954 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
955 ret = do_perform_cow_read(bs, m->offset, end->offset, &qiov);
Alberto Garciab3cf1c72017-06-19 16:40:06 +0300956 }
Alberto Garcia672f0f22017-06-19 16:40:05 +0300957 if (ret < 0) {
958 goto fail;
959 }
Alberto Garcia99450c62017-06-19 16:40:04 +0300960
Alberto Garcia672f0f22017-06-19 16:40:05 +0300961 /* Encrypt the data if necessary before writing it */
962 if (bs->encrypted) {
Maxim Levitsky603fbd02019-09-15 23:36:54 +0300963 ret = qcow2_co_encrypt(bs,
964 m->alloc_offset + start->offset,
965 m->offset + start->offset,
966 start_buffer, start->nb_bytes);
967 if (ret < 0) {
968 goto fail;
969 }
970
971 ret = qcow2_co_encrypt(bs,
972 m->alloc_offset + end->offset,
973 m->offset + end->offset,
974 end_buffer, end->nb_bytes);
975 if (ret < 0) {
Alberto Garcia672f0f22017-06-19 16:40:05 +0300976 goto fail;
977 }
978 }
979
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300980 /* And now we can write everything. If we have the guest data we
981 * can write everything in one single operation */
982 if (m->data_qiov) {
983 qemu_iovec_reset(&qiov);
984 if (start->nb_bytes) {
985 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
986 }
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300987 qemu_iovec_concat(&qiov, m->data_qiov, m->data_qiov_offset, data_bytes);
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300988 if (end->nb_bytes) {
989 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
990 }
991 /* NOTE: we have a write_aio blkdebug event here followed by
992 * a cow_write one in do_perform_cow_write(), but there's only
993 * one single I/O operation */
Paolo Bonzini17362392023-06-01 13:51:45 +0200994 BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300995 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
996 } else {
997 /* If there's no guest data then write both COW regions separately */
998 qemu_iovec_reset(&qiov);
999 qemu_iovec_add(&qiov, start_buffer, start->nb_bytes);
1000 ret = do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov);
1001 if (ret < 0) {
1002 goto fail;
1003 }
1004
1005 qemu_iovec_reset(&qiov);
1006 qemu_iovec_add(&qiov, end_buffer, end->nb_bytes);
1007 ret = do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov);
Alberto Garcia672f0f22017-06-19 16:40:05 +03001008 }
1009
Alberto Garcia99450c62017-06-19 16:40:04 +03001010fail:
1011 qemu_co_mutex_lock(&s->lock);
1012
Kevin Wolf593fb832012-12-07 18:08:43 +01001013 /*
1014 * Before we update the L2 table to actually point to the new cluster, we
1015 * need to be sure that the refcounts have been increased and COW was
1016 * handled.
1017 */
Alberto Garcia99450c62017-06-19 16:40:04 +03001018 if (ret == 0) {
1019 qcow2_cache_depends_on_flush(s->l2_table_cache);
1020 }
Kevin Wolf593fb832012-12-07 18:08:43 +01001021
Alberto Garcia672f0f22017-06-19 16:40:05 +03001022 qemu_vfree(start_buffer);
Alberto Garcia86b862c2017-06-19 16:40:07 +03001023 qemu_iovec_destroy(&qiov);
Alberto Garcia99450c62017-06-19 16:40:04 +03001024 return ret;
Kevin Wolf593fb832012-12-07 18:08:43 +01001025}
1026
Paolo Bonzini050ed2e2022-09-22 10:49:12 +02001027int coroutine_fn qcow2_alloc_cluster_link_l2(BlockDriverState *bs,
1028 QCowL2Meta *m)
Kevin Wolf45aba422009-05-28 16:07:05 +02001029{
Kevin Wolfff991292015-09-07 17:12:56 +02001030 BDRVQcow2State *s = bs->opaque;
Kevin Wolf45aba422009-05-28 16:07:05 +02001031 int i, j = 0, l2_index, ret;
Alberto Garciaa002c0b2018-02-05 16:33:21 +02001032 uint64_t *old_cluster, *l2_slice;
Kevin Wolf250196f2012-03-02 14:10:54 +01001033 uint64_t cluster_offset = m->alloc_offset;
Kevin Wolf45aba422009-05-28 16:07:05 +02001034
Kevin Wolf3cce16f2012-03-01 18:36:21 +01001035 trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters);
Kevin Wolff50f88b2012-12-07 18:08:46 +01001036 assert(m->nb_clusters > 0);
Kevin Wolf45aba422009-05-28 16:07:05 +02001037
Markus Armbruster5839e532014-08-19 10:31:08 +02001038 old_cluster = g_try_new(uint64_t, m->nb_clusters);
Kevin Wolfde828152014-05-20 17:12:47 +02001039 if (old_cluster == NULL) {
1040 ret = -ENOMEM;
1041 goto err;
1042 }
Kevin Wolf45aba422009-05-28 16:07:05 +02001043
1044 /* copy content of unmodified sectors */
Alberto Garcia99450c62017-06-19 16:40:04 +03001045 ret = perform_cow(bs, m);
Kevin Wolf593fb832012-12-07 18:08:43 +01001046 if (ret < 0) {
1047 goto err;
Kevin Wolf45aba422009-05-28 16:07:05 +02001048 }
1049
Kevin Wolf593fb832012-12-07 18:08:43 +01001050 /* Update L2 table. */
Kevin Wolf74c45102013-03-15 10:35:08 +01001051 if (s->use_lazy_refcounts) {
Kevin Wolf280d3732012-12-07 18:08:47 +01001052 qcow2_mark_dirty(bs);
1053 }
Stefan Hajnoczibfe80432012-07-27 09:05:22 +01001054 if (qcow2_need_accurate_refcounts(s)) {
1055 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1056 s->refcount_block_cache);
1057 }
Kevin Wolf280d3732012-12-07 18:08:47 +01001058
Alberto Garciaa002c0b2018-02-05 16:33:21 +02001059 ret = get_cluster_table(bs, m->offset, &l2_slice, &l2_index);
Kevin Wolf1e3e8f12010-01-20 15:03:00 +01001060 if (ret < 0) {
Kevin Wolf45aba422009-05-28 16:07:05 +02001061 goto err;
Kevin Wolf1e3e8f12010-01-20 15:03:00 +01001062 }
Alberto Garciaa002c0b2018-02-05 16:33:21 +02001063 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
Kevin Wolf45aba422009-05-28 16:07:05 +02001064
Alberto Garciaa002c0b2018-02-05 16:33:21 +02001065 assert(l2_index + m->nb_clusters <= s->l2_slice_size);
Alberto Garcia3441ad42020-10-07 18:13:23 +02001066 assert(m->cow_end.offset + m->cow_end.nb_bytes <=
1067 m->nb_clusters << s->cluster_bits);
Kevin Wolf45aba422009-05-28 16:07:05 +02001068 for (i = 0; i < m->nb_clusters; i++) {
Tuguoyi348fcc42020-08-05 09:22:58 +00001069 uint64_t offset = cluster_offset + ((uint64_t)i << s->cluster_bits);
Kevin Wolf45aba422009-05-28 16:07:05 +02001070 /* if two concurrent writes happen to the same unallocated cluster
Kevin Wolfaaa4d202016-06-01 15:21:05 +02001071 * each write allocates separate cluster and writes data concurrently.
1072 * The first one to complete updates l2 table with pointer to its
1073 * cluster the second one has to do RMW (which is done above by
1074 * perform_cow()), update l2 table with its cluster pointer and free
1075 * old cluster. This is what this loop does */
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001076 if (get_l2_entry(s, l2_slice, l2_index + i) != 0) {
1077 old_cluster[j++] = get_l2_entry(s, l2_slice, l2_index + i);
Kevin Wolfaaa4d202016-06-01 15:21:05 +02001078 }
Kevin Wolf45aba422009-05-28 16:07:05 +02001079
Alberto Garcia3a75a872020-01-13 17:11:46 +01001080 /* The offset must fit in the offset field of the L2 table entry */
1081 assert((offset & L2E_OFFSET_MASK) == offset);
1082
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001083 set_l2_entry(s, l2_slice, l2_index + i, offset | QCOW_OFLAG_COPIED);
Alberto Garciaaca00cd2020-07-10 18:13:07 +02001084
1085 /* Update bitmap with the subclusters that were just written */
Alberto Garcia40dee942020-07-10 18:13:12 +02001086 if (has_subclusters(s) && !m->prealloc) {
Alberto Garciaaca00cd2020-07-10 18:13:07 +02001087 uint64_t l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1088 unsigned written_from = m->cow_start.offset;
Alberto Garcia3441ad42020-10-07 18:13:23 +02001089 unsigned written_to = m->cow_end.offset + m->cow_end.nb_bytes;
Alberto Garciaaca00cd2020-07-10 18:13:07 +02001090 int first_sc, last_sc;
1091 /* Narrow written_from and written_to down to the current cluster */
1092 written_from = MAX(written_from, i << s->cluster_bits);
1093 written_to = MIN(written_to, (i + 1) << s->cluster_bits);
1094 assert(written_from < written_to);
1095 first_sc = offset_to_sc_index(s, written_from);
1096 last_sc = offset_to_sc_index(s, written_to - 1);
1097 l2_bitmap |= QCOW_OFLAG_SUB_ALLOC_RANGE(first_sc, last_sc + 1);
1098 l2_bitmap &= ~QCOW_OFLAG_SUB_ZERO_RANGE(first_sc, last_sc + 1);
1099 set_l2_bitmap(s, l2_slice, l2_index + i, l2_bitmap);
1100 }
Kevin Wolf45aba422009-05-28 16:07:05 +02001101 }
1102
Kevin Wolf9f8e6682010-09-17 17:02:09 +02001103
Alberto Garciaa002c0b2018-02-05 16:33:21 +02001104 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf45aba422009-05-28 16:07:05 +02001105
Kevin Wolf7ec5e6a2010-09-01 12:40:52 +02001106 /*
1107 * If this was a COW, we need to decrease the refcount of the old cluster.
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02001108 *
1109 * Don't discard clusters that reach a refcount of 0 (e.g. compressed
1110 * clusters), the next write will reuse them anyway.
Kevin Wolf7ec5e6a2010-09-01 12:40:52 +02001111 */
Max Reitz564a6b62017-05-04 01:11:18 +02001112 if (!m->keep_old_clusters && j != 0) {
Kevin Wolf7ec5e6a2010-09-01 12:40:52 +02001113 for (i = 0; i < j; i++) {
Alberto Garcia3fec2372020-09-08 16:08:28 +02001114 qcow2_free_any_cluster(bs, old_cluster[i], QCOW2_DISCARD_NEVER);
Kevin Wolf7ec5e6a2010-09-01 12:40:52 +02001115 }
1116 }
Kevin Wolf45aba422009-05-28 16:07:05 +02001117
1118 ret = 0;
1119err:
Anthony Liguori7267c092011-08-20 22:09:37 -05001120 g_free(old_cluster);
Kevin Wolf45aba422009-05-28 16:07:05 +02001121 return ret;
1122 }
1123
Kevin Wolf8b24cd12018-06-28 17:05:45 +02001124/**
1125 * Frees the allocated clusters because the request failed and they won't
1126 * actually be linked.
1127 */
Paolo Bonzinia39bae42023-03-09 09:44:55 +01001128void coroutine_fn qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)
Kevin Wolf8b24cd12018-06-28 17:05:45 +02001129{
1130 BDRVQcow2State *s = bs->opaque;
Max Reitz3ede9352020-02-25 15:31:28 +01001131 if (!has_data_file(bs) && !m->keep_old_clusters) {
Kevin Wolfc3b66582020-02-11 10:48:59 +01001132 qcow2_free_clusters(bs, m->alloc_offset,
1133 m->nb_clusters << s->cluster_bits,
1134 QCOW2_DISCARD_NEVER);
1135 }
Kevin Wolf8b24cd12018-06-28 17:05:45 +02001136}
1137
Kevin Wolf45aba422009-05-28 16:07:05 +02001138/*
Alberto Garcia8f91d692020-07-10 18:12:45 +02001139 * For a given write request, create a new QCowL2Meta structure, add
Alberto Garcia57538c82020-07-10 18:12:47 +02001140 * it to @m and the BDRVQcow2State.cluster_allocs list. If the write
1141 * request does not need copy-on-write or changes to the L2 metadata
1142 * then this function does nothing.
Alberto Garcia8f91d692020-07-10 18:12:45 +02001143 *
1144 * @host_cluster_offset points to the beginning of the first cluster.
1145 *
1146 * @guest_offset and @bytes indicate the offset and length of the
1147 * request.
1148 *
Alberto Garcia57538c82020-07-10 18:12:47 +02001149 * @l2_slice contains the L2 entries of all clusters involved in this
1150 * write request.
1151 *
Alberto Garcia8f91d692020-07-10 18:12:45 +02001152 * If @keep_old is true it means that the clusters were already
1153 * allocated and will be overwritten. If false then the clusters are
1154 * new and we have to decrease the reference count of the old ones.
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001155 *
1156 * Returns 0 on success, -errno on failure.
Alberto Garcia8f91d692020-07-10 18:12:45 +02001157 */
Paolo Bonzinia39bae42023-03-09 09:44:55 +01001158static int coroutine_fn calculate_l2_meta(BlockDriverState *bs,
1159 uint64_t host_cluster_offset,
1160 uint64_t guest_offset, unsigned bytes,
1161 uint64_t *l2_slice, QCowL2Meta **m,
1162 bool keep_old)
Alberto Garcia8f91d692020-07-10 18:12:45 +02001163{
1164 BDRVQcow2State *s = bs->opaque;
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001165 int sc_index, l2_index = offset_to_l2_slice_index(s, guest_offset);
1166 uint64_t l2_entry, l2_bitmap;
Alberto Garcia57538c82020-07-10 18:12:47 +02001167 unsigned cow_start_from, cow_end_to;
Alberto Garcia8f91d692020-07-10 18:12:45 +02001168 unsigned cow_start_to = offset_into_cluster(s, guest_offset);
1169 unsigned cow_end_from = cow_start_to + bytes;
Alberto Garcia8f91d692020-07-10 18:12:45 +02001170 unsigned nb_clusters = size_to_clusters(s, cow_end_from);
1171 QCowL2Meta *old_m = *m;
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001172 QCow2SubclusterType type;
1173 int i;
1174 bool skip_cow = keep_old;
Alberto Garcia57538c82020-07-10 18:12:47 +02001175
1176 assert(nb_clusters <= s->l2_slice_size - l2_index);
1177
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001178 /* Check the type of all affected subclusters */
1179 for (i = 0; i < nb_clusters; i++) {
1180 l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
1181 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1182 if (skip_cow) {
1183 unsigned write_from = MAX(cow_start_to, i << s->cluster_bits);
1184 unsigned write_to = MIN(cow_end_from, (i + 1) << s->cluster_bits);
1185 int first_sc = offset_to_sc_index(s, write_from);
1186 int last_sc = offset_to_sc_index(s, write_to - 1);
1187 int cnt = qcow2_get_subcluster_range_type(bs, l2_entry, l2_bitmap,
1188 first_sc, &type);
1189 /* Is any of the subclusters of type != QCOW2_SUBCLUSTER_NORMAL ? */
1190 if (type != QCOW2_SUBCLUSTER_NORMAL || first_sc + cnt <= last_sc) {
1191 skip_cow = false;
Alberto Garcia57538c82020-07-10 18:12:47 +02001192 }
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001193 } else {
1194 /* If we can't skip the cow we can still look for invalid entries */
1195 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, 0);
Alberto Garcia57538c82020-07-10 18:12:47 +02001196 }
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001197 if (type == QCOW2_SUBCLUSTER_INVALID) {
1198 int l1_index = offset_to_l1_index(s, guest_offset);
1199 uint64_t l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
1200 qcow2_signal_corruption(bs, true, -1, -1, "Invalid cluster "
1201 "entry found (L2 offset: %#" PRIx64
1202 ", L2 index: %#x)",
1203 l2_offset, l2_index + i);
1204 return -EIO;
Alberto Garcia57538c82020-07-10 18:12:47 +02001205 }
1206 }
1207
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001208 if (skip_cow) {
1209 return 0;
1210 }
1211
Alberto Garcia57538c82020-07-10 18:12:47 +02001212 /* Get the L2 entry of the first cluster */
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001213 l2_entry = get_l2_entry(s, l2_slice, l2_index);
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001214 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
1215 sc_index = offset_to_sc_index(s, guest_offset);
1216 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001217
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001218 if (!keep_old) {
1219 switch (type) {
1220 case QCOW2_SUBCLUSTER_COMPRESSED:
1221 cow_start_from = 0;
1222 break;
1223 case QCOW2_SUBCLUSTER_NORMAL:
1224 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1225 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1226 if (has_subclusters(s)) {
1227 /* Skip all leading zero and unallocated subclusters */
1228 uint32_t alloc_bitmap = l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC;
1229 cow_start_from =
1230 MIN(sc_index, ctz32(alloc_bitmap)) << s->subcluster_bits;
1231 } else {
1232 cow_start_from = 0;
1233 }
1234 break;
1235 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
1236 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
1237 cow_start_from = sc_index << s->subcluster_bits;
1238 break;
1239 default:
1240 g_assert_not_reached();
1241 }
Alberto Garcia57538c82020-07-10 18:12:47 +02001242 } else {
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001243 switch (type) {
1244 case QCOW2_SUBCLUSTER_NORMAL:
1245 cow_start_from = cow_start_to;
1246 break;
1247 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1248 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1249 cow_start_from = sc_index << s->subcluster_bits;
1250 break;
1251 default:
1252 g_assert_not_reached();
1253 }
Alberto Garcia57538c82020-07-10 18:12:47 +02001254 }
1255
1256 /* Get the L2 entry of the last cluster */
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001257 l2_index += nb_clusters - 1;
1258 l2_entry = get_l2_entry(s, l2_slice, l2_index);
1259 l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
1260 sc_index = offset_to_sc_index(s, guest_offset + bytes - 1);
1261 type = qcow2_get_subcluster_type(bs, l2_entry, l2_bitmap, sc_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001262
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001263 if (!keep_old) {
1264 switch (type) {
1265 case QCOW2_SUBCLUSTER_COMPRESSED:
1266 cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);
1267 break;
1268 case QCOW2_SUBCLUSTER_NORMAL:
1269 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1270 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1271 cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);
1272 if (has_subclusters(s)) {
1273 /* Skip all trailing zero and unallocated subclusters */
1274 uint32_t alloc_bitmap = l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC;
1275 cow_end_to -=
1276 MIN(s->subclusters_per_cluster - sc_index - 1,
1277 clz32(alloc_bitmap)) << s->subcluster_bits;
1278 }
1279 break;
1280 case QCOW2_SUBCLUSTER_ZERO_PLAIN:
1281 case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
1282 cow_end_to = ROUND_UP(cow_end_from, s->subcluster_size);
1283 break;
1284 default:
1285 g_assert_not_reached();
1286 }
Alberto Garcia57538c82020-07-10 18:12:47 +02001287 } else {
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001288 switch (type) {
1289 case QCOW2_SUBCLUSTER_NORMAL:
1290 cow_end_to = cow_end_from;
1291 break;
1292 case QCOW2_SUBCLUSTER_ZERO_ALLOC:
1293 case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
1294 cow_end_to = ROUND_UP(cow_end_from, s->subcluster_size);
1295 break;
1296 default:
1297 g_assert_not_reached();
1298 }
Alberto Garcia57538c82020-07-10 18:12:47 +02001299 }
Alberto Garcia8f91d692020-07-10 18:12:45 +02001300
1301 *m = g_malloc0(sizeof(**m));
1302 **m = (QCowL2Meta) {
1303 .next = old_m,
1304
1305 .alloc_offset = host_cluster_offset,
1306 .offset = start_of_cluster(s, guest_offset),
1307 .nb_clusters = nb_clusters,
1308
1309 .keep_old_clusters = keep_old,
1310
1311 .cow_start = {
1312 .offset = cow_start_from,
1313 .nb_bytes = cow_start_to - cow_start_from,
1314 },
1315 .cow_end = {
1316 .offset = cow_end_from,
1317 .nb_bytes = cow_end_to - cow_end_from,
1318 },
1319 };
1320
1321 qemu_co_queue_init(&(*m)->dependent_requests);
1322 QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001323
1324 return 0;
Alberto Garcia8f91d692020-07-10 18:12:45 +02001325}
1326
Alberto Garcia57538c82020-07-10 18:12:47 +02001327/*
1328 * Returns true if writing to the cluster pointed to by @l2_entry
1329 * requires a new allocation (that is, if the cluster is unallocated
1330 * or has refcount > 1 and therefore cannot be written in-place).
1331 */
1332static bool cluster_needs_new_alloc(BlockDriverState *bs, uint64_t l2_entry)
Alberto Garciac1587d82020-07-10 18:12:46 +02001333{
1334 switch (qcow2_get_cluster_type(bs, l2_entry)) {
1335 case QCOW2_CLUSTER_NORMAL:
Alberto Garcia57538c82020-07-10 18:12:47 +02001336 case QCOW2_CLUSTER_ZERO_ALLOC:
Alberto Garciac1587d82020-07-10 18:12:46 +02001337 if (l2_entry & QCOW_OFLAG_COPIED) {
1338 return false;
1339 }
Thomas Huthb9be6fa2020-09-08 09:00:28 +02001340 /* fallthrough */
Alberto Garciac1587d82020-07-10 18:12:46 +02001341 case QCOW2_CLUSTER_UNALLOCATED:
1342 case QCOW2_CLUSTER_COMPRESSED:
1343 case QCOW2_CLUSTER_ZERO_PLAIN:
Alberto Garciac1587d82020-07-10 18:12:46 +02001344 return true;
1345 default:
1346 abort();
1347 }
1348}
1349
Alberto Garcia8f91d692020-07-10 18:12:45 +02001350/*
Alberto Garcia57538c82020-07-10 18:12:47 +02001351 * Returns the number of contiguous clusters that can be written to
1352 * using one single write request, starting from @l2_index.
1353 * At most @nb_clusters are checked.
1354 *
1355 * If @new_alloc is true this counts clusters that are either
1356 * unallocated, or allocated but with refcount > 1 (so they need to be
1357 * newly allocated and COWed).
1358 *
1359 * If @new_alloc is false this counts clusters that are already
1360 * allocated and can be overwritten in-place (this includes clusters
1361 * of type QCOW2_CLUSTER_ZERO_ALLOC).
Kevin Wolfbf319ec2012-03-02 19:27:53 +01001362 */
Alberto Garcia57538c82020-07-10 18:12:47 +02001363static int count_single_write_clusters(BlockDriverState *bs, int nb_clusters,
1364 uint64_t *l2_slice, int l2_index,
1365 bool new_alloc)
Kevin Wolfbf319ec2012-03-02 19:27:53 +01001366{
Alberto Garcia57538c82020-07-10 18:12:47 +02001367 BDRVQcow2State *s = bs->opaque;
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001368 uint64_t l2_entry = get_l2_entry(s, l2_slice, l2_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001369 uint64_t expected_offset = l2_entry & L2E_OFFSET_MASK;
Kevin Wolf143550a2012-03-27 13:17:22 +02001370 int i;
Kevin Wolfbf319ec2012-03-02 19:27:53 +01001371
Kevin Wolf143550a2012-03-27 13:17:22 +02001372 for (i = 0; i < nb_clusters; i++) {
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001373 l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
Alberto Garcia57538c82020-07-10 18:12:47 +02001374 if (cluster_needs_new_alloc(bs, l2_entry) != new_alloc) {
Kevin Wolfbf319ec2012-03-02 19:27:53 +01001375 break;
1376 }
Alberto Garcia57538c82020-07-10 18:12:47 +02001377 if (!new_alloc) {
1378 if (expected_offset != (l2_entry & L2E_OFFSET_MASK)) {
1379 break;
1380 }
1381 expected_offset += s->cluster_size;
1382 }
Kevin Wolfbf319ec2012-03-02 19:27:53 +01001383 }
1384
1385 assert(i <= nb_clusters);
1386 return i;
1387}
1388
1389/*
Kevin Wolf226c3c22012-12-07 18:08:49 +01001390 * Check if there already is an AIO write request in flight which allocates
1391 * the same cluster. In this case we need to wait until the previous
1392 * request has completed and updated the L2 table accordingly.
Kevin Wolf65eb2e32013-03-26 17:49:58 +01001393 *
1394 * Returns:
1395 * 0 if there was no dependency. *cur_bytes indicates the number of
1396 * bytes from guest_offset that can be read before the next
1397 * dependency must be processed (or the request is complete)
1398 *
1399 * -EAGAIN if we had to wait for another request, previously gathered
1400 * information on cluster allocation may be invalid now. The caller
1401 * must start over anyway, so consider *cur_bytes undefined.
Kevin Wolf250196f2012-03-02 14:10:54 +01001402 */
Paolo Bonzini050ed2e2022-09-22 10:49:12 +02001403static int coroutine_fn handle_dependencies(BlockDriverState *bs,
1404 uint64_t guest_offset,
1405 uint64_t *cur_bytes, QCowL2Meta **m)
Kevin Wolf250196f2012-03-02 14:10:54 +01001406{
Kevin Wolfff991292015-09-07 17:12:56 +02001407 BDRVQcow2State *s = bs->opaque;
Kevin Wolf250196f2012-03-02 14:10:54 +01001408 QCowL2Meta *old_alloc;
Kevin Wolf65eb2e32013-03-26 17:49:58 +01001409 uint64_t bytes = *cur_bytes;
Kevin Wolf250196f2012-03-02 14:10:54 +01001410
Kevin Wolf250196f2012-03-02 14:10:54 +01001411 QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) {
1412
Kevin Wolf65eb2e32013-03-26 17:49:58 +01001413 uint64_t start = guest_offset;
1414 uint64_t end = start + bytes;
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001415 uint64_t old_start = start_of_cluster(s, l2meta_cow_start(old_alloc));
1416 uint64_t old_end = ROUND_UP(l2meta_cow_end(old_alloc), s->cluster_size);
Kevin Wolf250196f2012-03-02 14:10:54 +01001417
Kevin Wolfd9d74f42013-03-26 17:49:57 +01001418 if (end <= old_start || start >= old_end) {
Kevin Wolf250196f2012-03-02 14:10:54 +01001419 /* No intersection */
Vladimir Sementsov-Ogievskiy6d207d32021-08-24 13:15:16 +03001420 continue;
1421 }
1422
Vladimir Sementsov-Ogievskiyff812c52021-08-24 13:15:17 +03001423 if (old_alloc->keep_old_clusters &&
1424 (end <= l2meta_cow_start(old_alloc) ||
1425 start >= l2meta_cow_end(old_alloc)))
1426 {
1427 /*
1428 * Clusters intersect but COW areas don't. And cluster itself is
1429 * already allocated. So, there is no actual conflict.
1430 */
1431 continue;
1432 }
1433
Vladimir Sementsov-Ogievskiy6d207d32021-08-24 13:15:16 +03001434 /* Conflict */
1435
1436 if (start < old_start) {
1437 /* Stop at the start of a running allocation */
1438 bytes = old_start - start;
Kevin Wolf250196f2012-03-02 14:10:54 +01001439 } else {
Vladimir Sementsov-Ogievskiy6d207d32021-08-24 13:15:16 +03001440 bytes = 0;
1441 }
Kevin Wolf250196f2012-03-02 14:10:54 +01001442
Vladimir Sementsov-Ogievskiy6d207d32021-08-24 13:15:16 +03001443 /*
1444 * Stop if an l2meta already exists. After yielding, it wouldn't
1445 * be valid any more, so we'd have to clean up the old L2Metas
1446 * and deal with requests depending on them before starting to
1447 * gather new ones. Not worth the trouble.
1448 */
1449 if (bytes == 0 && *m) {
1450 *cur_bytes = 0;
1451 return 0;
1452 }
Kevin Wolfecdd5332013-03-27 11:43:49 +01001453
Vladimir Sementsov-Ogievskiy6d207d32021-08-24 13:15:16 +03001454 if (bytes == 0) {
1455 /*
1456 * Wait for the dependency to complete. We need to recheck
1457 * the free/allocated clusters when we continue.
1458 */
1459 qemu_co_queue_wait(&old_alloc->dependent_requests, &s->lock);
1460 return -EAGAIN;
Kevin Wolf250196f2012-03-02 14:10:54 +01001461 }
1462 }
1463
Kevin Wolf65eb2e32013-03-26 17:49:58 +01001464 /* Make sure that existing clusters and new allocations are only used up to
1465 * the next dependency if we shortened the request above */
1466 *cur_bytes = bytes;
Kevin Wolf250196f2012-03-02 14:10:54 +01001467
Kevin Wolf226c3c22012-12-07 18:08:49 +01001468 return 0;
1469}
1470
1471/*
Alberto Garcia57538c82020-07-10 18:12:47 +02001472 * Checks how many already allocated clusters that don't require a new
1473 * allocation there are at the given guest_offset (up to *bytes).
1474 * If *host_offset is not INV_OFFSET, only physically contiguous clusters
1475 * beginning at this host offset are counted.
Kevin Wolf0af729e2013-03-26 17:50:05 +01001476 *
Kevin Wolf411d62b2013-03-26 17:50:09 +01001477 * Note that guest_offset may not be cluster aligned. In this case, the
1478 * returned *host_offset points to exact byte referenced by guest_offset and
1479 * therefore isn't cluster aligned as well.
Kevin Wolf0af729e2013-03-26 17:50:05 +01001480 *
1481 * Returns:
1482 * 0: if no allocated clusters are available at the given offset.
1483 * *bytes is normally unchanged. It is set to 0 if the cluster
Alberto Garcia57538c82020-07-10 18:12:47 +02001484 * is allocated and can be overwritten in-place but doesn't have
1485 * the right physical offset.
Kevin Wolf0af729e2013-03-26 17:50:05 +01001486 *
Alberto Garcia57538c82020-07-10 18:12:47 +02001487 * 1: if allocated clusters that can be overwritten in place are
1488 * available at the requested offset. *bytes may have decreased
1489 * and describes the length of the area that can be written to.
Kevin Wolf0af729e2013-03-26 17:50:05 +01001490 *
1491 * -errno: in error cases
Kevin Wolf0af729e2013-03-26 17:50:05 +01001492 */
Paolo Bonzinia1b4ecf2022-10-13 14:36:59 +02001493static int coroutine_fn handle_copied(BlockDriverState *bs,
1494 uint64_t guest_offset, uint64_t *host_offset, uint64_t *bytes,
1495 QCowL2Meta **m)
Kevin Wolf0af729e2013-03-26 17:50:05 +01001496{
Kevin Wolfff991292015-09-07 17:12:56 +02001497 BDRVQcow2State *s = bs->opaque;
Kevin Wolf0af729e2013-03-26 17:50:05 +01001498 int l2_index;
Alberto Garcia57538c82020-07-10 18:12:47 +02001499 uint64_t l2_entry, cluster_offset;
Alberto Garciacde91762018-02-05 16:33:22 +02001500 uint64_t *l2_slice;
Max Reitzb6d36de2015-09-14 16:39:47 +02001501 uint64_t nb_clusters;
Kevin Wolfc53ede92013-03-26 17:50:07 +01001502 unsigned int keep_clusters;
Alberto Garciaa3f1afb2015-05-11 15:54:58 +03001503 int ret;
Kevin Wolf0af729e2013-03-26 17:50:05 +01001504
1505 trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
1506 *bytes);
Kevin Wolf0af729e2013-03-26 17:50:05 +01001507
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001508 assert(*host_offset == INV_OFFSET || offset_into_cluster(s, guest_offset)
1509 == offset_into_cluster(s, *host_offset));
Kevin Wolf411d62b2013-03-26 17:50:09 +01001510
Kevin Wolfacb04672013-03-26 17:50:06 +01001511 /*
Alberto Garciacde91762018-02-05 16:33:22 +02001512 * Calculate the number of clusters to look for. We stop at L2 slice
Kevin Wolfacb04672013-03-26 17:50:06 +01001513 * boundaries to keep things simple.
1514 */
1515 nb_clusters =
1516 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1517
Alberto Garciacde91762018-02-05 16:33:22 +02001518 l2_index = offset_to_l2_slice_index(s, guest_offset);
1519 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001520 /* Limit total byte count to BDRV_REQUEST_MAX_BYTES */
1521 nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);
Kevin Wolfacb04672013-03-26 17:50:06 +01001522
Kevin Wolf0af729e2013-03-26 17:50:05 +01001523 /* Find L2 entry for the first involved cluster */
Alberto Garciacde91762018-02-05 16:33:22 +02001524 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
Kevin Wolf0af729e2013-03-26 17:50:05 +01001525 if (ret < 0) {
1526 return ret;
1527 }
1528
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02001529 l2_entry = get_l2_entry(s, l2_slice, l2_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001530 cluster_offset = l2_entry & L2E_OFFSET_MASK;
Kevin Wolf0af729e2013-03-26 17:50:05 +01001531
Alberto Garcia57538c82020-07-10 18:12:47 +02001532 if (!cluster_needs_new_alloc(bs, l2_entry)) {
1533 if (offset_into_cluster(s, cluster_offset)) {
1534 qcow2_signal_corruption(bs, true, -1, -1, "%s cluster offset "
1535 "%#" PRIx64 " unaligned (guest offset: %#"
1536 PRIx64 ")", l2_entry & QCOW_OFLAG_ZERO ?
1537 "Preallocated zero" : "Data",
1538 cluster_offset, guest_offset);
Max Reitza97c67e2014-09-05 16:07:18 +02001539 ret = -EIO;
1540 goto out;
1541 }
1542
Alberto Garcia57538c82020-07-10 18:12:47 +02001543 /* If a specific host_offset is required, check it */
1544 if (*host_offset != INV_OFFSET && cluster_offset != *host_offset) {
Kevin Wolfe62daaf2013-03-26 17:50:08 +01001545 *bytes = 0;
1546 ret = 0;
1547 goto out;
1548 }
1549
Kevin Wolf0af729e2013-03-26 17:50:05 +01001550 /* We keep all QCOW_OFLAG_COPIED clusters */
Alberto Garcia57538c82020-07-10 18:12:47 +02001551 keep_clusters = count_single_write_clusters(bs, nb_clusters, l2_slice,
1552 l2_index, false);
Kevin Wolfc53ede92013-03-26 17:50:07 +01001553 assert(keep_clusters <= nb_clusters);
1554
1555 *bytes = MIN(*bytes,
1556 keep_clusters * s->cluster_size
1557 - offset_into_cluster(s, guest_offset));
Alberto Garcia57538c82020-07-10 18:12:47 +02001558 assert(*bytes != 0);
1559
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001560 ret = calculate_l2_meta(bs, cluster_offset, guest_offset,
1561 *bytes, l2_slice, m, true);
1562 if (ret < 0) {
1563 goto out;
1564 }
Kevin Wolf0af729e2013-03-26 17:50:05 +01001565
1566 ret = 1;
1567 } else {
Kevin Wolf0af729e2013-03-26 17:50:05 +01001568 ret = 0;
1569 }
1570
Kevin Wolf0af729e2013-03-26 17:50:05 +01001571 /* Cleanup */
Kevin Wolfe62daaf2013-03-26 17:50:08 +01001572out:
Alberto Garciacde91762018-02-05 16:33:22 +02001573 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf0af729e2013-03-26 17:50:05 +01001574
Kevin Wolfe62daaf2013-03-26 17:50:08 +01001575 /* Only return a host offset if we actually made progress. Otherwise we
1576 * would make requirements for handle_alloc() that it can't fulfill */
Max Reitza97c67e2014-09-05 16:07:18 +02001577 if (ret > 0) {
Alberto Garcia57538c82020-07-10 18:12:47 +02001578 *host_offset = cluster_offset + offset_into_cluster(s, guest_offset);
Kevin Wolfe62daaf2013-03-26 17:50:08 +01001579 }
1580
Kevin Wolf0af729e2013-03-26 17:50:05 +01001581 return ret;
1582}
1583
1584/*
Kevin Wolf226c3c22012-12-07 18:08:49 +01001585 * Allocates new clusters for the given guest_offset.
1586 *
1587 * At most *nb_clusters are allocated, and on return *nb_clusters is updated to
1588 * contain the number of clusters that have been allocated and are contiguous
1589 * in the image file.
1590 *
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001591 * If *host_offset is not INV_OFFSET, it specifies the offset in the image file
1592 * at which the new clusters must start. *nb_clusters can be 0 on return in
1593 * this case if the cluster at host_offset is already in use. If *host_offset
1594 * is INV_OFFSET, the clusters can be allocated anywhere in the image file.
Kevin Wolf226c3c22012-12-07 18:08:49 +01001595 *
1596 * *host_offset is updated to contain the offset into the image file at which
1597 * the first allocated cluster starts.
1598 *
1599 * Return 0 on success and -errno in error cases. -EAGAIN means that the
1600 * function has been waiting for another request and the allocation must be
1601 * restarted, but the whole request should not be failed.
1602 */
Paolo Bonzinia39bae42023-03-09 09:44:55 +01001603static int coroutine_fn do_alloc_cluster_offset(BlockDriverState *bs,
1604 uint64_t guest_offset,
1605 uint64_t *host_offset,
1606 uint64_t *nb_clusters)
Kevin Wolf226c3c22012-12-07 18:08:49 +01001607{
Kevin Wolfff991292015-09-07 17:12:56 +02001608 BDRVQcow2State *s = bs->opaque;
Kevin Wolf226c3c22012-12-07 18:08:49 +01001609
1610 trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset,
1611 *host_offset, *nb_clusters);
1612
Kevin Wolf966b0002019-01-15 20:39:06 +01001613 if (has_data_file(bs)) {
1614 assert(*host_offset == INV_OFFSET ||
1615 *host_offset == start_of_cluster(s, guest_offset));
1616 *host_offset = start_of_cluster(s, guest_offset);
1617 return 0;
1618 }
1619
Kevin Wolf250196f2012-03-02 14:10:54 +01001620 /* Allocate new clusters */
1621 trace_qcow2_cluster_alloc_phys(qemu_coroutine_self());
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001622 if (*host_offset == INV_OFFSET) {
Kevin Wolfdf021792012-05-24 12:56:32 +02001623 int64_t cluster_offset =
1624 qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size);
1625 if (cluster_offset < 0) {
1626 return cluster_offset;
1627 }
1628 *host_offset = cluster_offset;
1629 return 0;
Kevin Wolf250196f2012-03-02 14:10:54 +01001630 } else {
Max Reitzb6d36de2015-09-14 16:39:47 +02001631 int64_t ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters);
Kevin Wolfdf021792012-05-24 12:56:32 +02001632 if (ret < 0) {
1633 return ret;
1634 }
1635 *nb_clusters = ret;
1636 return 0;
Kevin Wolf250196f2012-03-02 14:10:54 +01001637 }
Kevin Wolf250196f2012-03-02 14:10:54 +01001638}
1639
1640/*
Alberto Garcia57538c82020-07-10 18:12:47 +02001641 * Allocates new clusters for an area that is either still unallocated or
1642 * cannot be overwritten in-place. If *host_offset is not INV_OFFSET,
1643 * clusters are only allocated if the new allocation can match the specified
1644 * host offset.
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001645 *
Kevin Wolf411d62b2013-03-26 17:50:09 +01001646 * Note that guest_offset may not be cluster aligned. In this case, the
1647 * returned *host_offset points to exact byte referenced by guest_offset and
1648 * therefore isn't cluster aligned as well.
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001649 *
1650 * Returns:
1651 * 0: if no clusters could be allocated. *bytes is set to 0,
1652 * *host_offset is left unchanged.
1653 *
1654 * 1: if new clusters were allocated. *bytes may be decreased if the
1655 * new allocation doesn't cover all of the requested area.
1656 * *host_offset is updated to contain the host offset of the first
1657 * newly allocated cluster.
1658 *
1659 * -errno: in error cases
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001660 */
Paolo Bonzinia1b4ecf2022-10-13 14:36:59 +02001661static int coroutine_fn handle_alloc(BlockDriverState *bs,
1662 uint64_t guest_offset, uint64_t *host_offset, uint64_t *bytes,
1663 QCowL2Meta **m)
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001664{
Kevin Wolfff991292015-09-07 17:12:56 +02001665 BDRVQcow2State *s = bs->opaque;
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001666 int l2_index;
Alberto Garcia6d99a342018-02-05 16:33:23 +02001667 uint64_t *l2_slice;
Max Reitzb6d36de2015-09-14 16:39:47 +02001668 uint64_t nb_clusters;
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001669 int ret;
1670
Alberto Garcia57538c82020-07-10 18:12:47 +02001671 uint64_t alloc_cluster_offset;
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001672
1673 trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,
1674 *bytes);
1675 assert(*bytes > 0);
1676
Kevin Wolff5bc6352013-03-26 17:50:01 +01001677 /*
Alberto Garcia6d99a342018-02-05 16:33:23 +02001678 * Calculate the number of clusters to look for. We stop at L2 slice
Kevin Wolff5bc6352013-03-26 17:50:01 +01001679 * boundaries to keep things simple.
1680 */
Kevin Wolfc37f4cd2013-03-26 17:50:03 +01001681 nb_clusters =
1682 size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes);
1683
Alberto Garcia6d99a342018-02-05 16:33:23 +02001684 l2_index = offset_to_l2_slice_index(s, guest_offset);
1685 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
Alberto Garcia57538c82020-07-10 18:12:47 +02001686 /* Limit total allocation byte count to BDRV_REQUEST_MAX_BYTES */
1687 nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);
Max Reitzd1b9d192019-10-10 12:08:57 +02001688
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001689 /* Find L2 entry for the first involved cluster */
Alberto Garcia6d99a342018-02-05 16:33:23 +02001690 ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001691 if (ret < 0) {
1692 return ret;
1693 }
1694
Alberto Garcia57538c82020-07-10 18:12:47 +02001695 nb_clusters = count_single_write_clusters(bs, nb_clusters,
1696 l2_slice, l2_index, true);
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001697
Kevin Wolfecdd5332013-03-27 11:43:49 +01001698 /* This function is only called when there were no non-COW clusters, so if
1699 * we can't find any unallocated or COW clusters either, something is
1700 * wrong with our code. */
1701 assert(nb_clusters > 0);
1702
Alberto Garcia57538c82020-07-10 18:12:47 +02001703 /* Allocate at a given offset in the image file */
1704 alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :
1705 start_of_cluster(s, *host_offset);
1706 ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,
1707 &nb_clusters);
1708 if (ret < 0) {
1709 goto out;
Max Reitz564a6b62017-05-04 01:11:18 +02001710 }
1711
Alberto Garcia57538c82020-07-10 18:12:47 +02001712 /* Can't extend contiguous allocation */
1713 if (nb_clusters == 0) {
1714 *bytes = 0;
1715 ret = 0;
1716 goto out;
Max Reitzff52aab2014-08-07 22:47:53 +02001717 }
1718
Alberto Garcia57538c82020-07-10 18:12:47 +02001719 assert(alloc_cluster_offset != INV_OFFSET);
1720
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001721 /*
1722 * Save info needed for meta data update.
1723 *
Kevin Wolf85567392016-06-01 14:08:56 +02001724 * requested_bytes: Number of bytes from the start of the first
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001725 * newly allocated cluster to the end of the (possibly shortened
1726 * before) write request.
1727 *
Kevin Wolf85567392016-06-01 14:08:56 +02001728 * avail_bytes: Number of bytes from the start of the first
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001729 * newly allocated to the end of the last newly allocated cluster.
1730 *
Kevin Wolf85567392016-06-01 14:08:56 +02001731 * nb_bytes: The number of bytes from the start of the first
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001732 * newly allocated cluster to the end of the area that the write
1733 * request actually writes to (excluding COW at the end)
1734 */
Kevin Wolf85567392016-06-01 14:08:56 +02001735 uint64_t requested_bytes = *bytes + offset_into_cluster(s, guest_offset);
Max Reitzd1b9d192019-10-10 12:08:57 +02001736 int avail_bytes = nb_clusters << s->cluster_bits;
Kevin Wolf85567392016-06-01 14:08:56 +02001737 int nb_bytes = MIN(requested_bytes, avail_bytes);
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001738
Kevin Wolf411d62b2013-03-26 17:50:09 +01001739 *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset);
Kevin Wolf85567392016-06-01 14:08:56 +02001740 *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));
Kevin Wolf83baa9a2013-03-26 17:50:04 +01001741 assert(*bytes != 0);
1742
Alberto Garciad53ec3d2020-07-10 18:13:02 +02001743 ret = calculate_l2_meta(bs, alloc_cluster_offset, guest_offset, *bytes,
1744 l2_slice, m, false);
1745 if (ret < 0) {
1746 goto out;
1747 }
Alberto Garcia8f91d692020-07-10 18:12:45 +02001748
Alberto Garcia57538c82020-07-10 18:12:47 +02001749 ret = 1;
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001750
Alberto Garcia57538c82020-07-10 18:12:47 +02001751out:
1752 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001753 return ret;
1754}
1755
1756/*
Alberto Garcia2b60c5b2020-09-03 18:37:49 +02001757 * For a given area on the virtual disk defined by @offset and @bytes,
1758 * find the corresponding area on the qcow2 image, allocating new
1759 * clusters (or subclusters) if necessary. The result can span a
1760 * combination of allocated and previously unallocated clusters.
Kevin Wolf45aba422009-05-28 16:07:05 +02001761 *
Alberto Garciabfd09892020-09-11 16:09:42 +02001762 * Note that offset may not be cluster aligned. In this case, the returned
1763 * *host_offset points to exact byte referenced by offset and therefore
1764 * isn't cluster aligned as well.
1765 *
Alberto Garcia2b60c5b2020-09-03 18:37:49 +02001766 * On return, @host_offset is set to the beginning of the requested
1767 * area. This area is guaranteed to be contiguous on the qcow2 file
1768 * but it can be smaller than initially requested. In this case @bytes
1769 * is updated with the actual size.
Kevin Wolf45aba422009-05-28 16:07:05 +02001770 *
Alberto Garcia2b60c5b2020-09-03 18:37:49 +02001771 * If any clusters or subclusters were allocated then @m contains a
1772 * list with the information of all the affected regions. Note that
1773 * this can happen regardless of whether this function succeeds or
1774 * not. The caller is responsible for updating the L2 metadata of the
1775 * allocated clusters (on success) or freeing them (on failure), and
1776 * for clearing the contents of @m afterwards in both cases.
Kevin Wolf148da7e2010-01-20 15:03:01 +01001777 *
Kevin Wolf68d100e2011-06-30 17:42:09 +02001778 * If the request conflicts with another write request in flight, the coroutine
1779 * is queued and will be reentered when the dependency has completed.
Kevin Wolf148da7e2010-01-20 15:03:01 +01001780 *
1781 * Return 0 on success and -errno in error cases
Kevin Wolf45aba422009-05-28 16:07:05 +02001782 */
Paolo Bonzini050ed2e2022-09-22 10:49:12 +02001783int coroutine_fn qcow2_alloc_host_offset(BlockDriverState *bs, uint64_t offset,
1784 unsigned int *bytes,
1785 uint64_t *host_offset,
1786 QCowL2Meta **m)
Kevin Wolf45aba422009-05-28 16:07:05 +02001787{
Kevin Wolfff991292015-09-07 17:12:56 +02001788 BDRVQcow2State *s = bs->opaque;
Kevin Wolf710c2492013-03-26 17:50:10 +01001789 uint64_t start, remaining;
Kevin Wolf250196f2012-03-02 14:10:54 +01001790 uint64_t cluster_offset;
Kevin Wolf65eb2e32013-03-26 17:49:58 +01001791 uint64_t cur_bytes;
Kevin Wolf710c2492013-03-26 17:50:10 +01001792 int ret;
Kevin Wolf45aba422009-05-28 16:07:05 +02001793
Kevin Wolfd46a0bb2016-06-01 16:55:05 +02001794 trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, *bytes);
Kevin Wolf710c2492013-03-26 17:50:10 +01001795
Kevin Wolf72424112012-04-24 16:10:56 +02001796again:
Hu Tao16f05872014-01-26 11:12:37 +08001797 start = offset;
Kevin Wolfd46a0bb2016-06-01 16:55:05 +02001798 remaining = *bytes;
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001799 cluster_offset = INV_OFFSET;
1800 *host_offset = INV_OFFSET;
Kevin Wolfecdd5332013-03-27 11:43:49 +01001801 cur_bytes = 0;
1802 *m = NULL;
Kevin Wolf0af729e2013-03-26 17:50:05 +01001803
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001804 while (true) {
Kevin Wolfecdd5332013-03-27 11:43:49 +01001805
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001806 if (*host_offset == INV_OFFSET && cluster_offset != INV_OFFSET) {
Alberto Garciabfd09892020-09-11 16:09:42 +02001807 *host_offset = cluster_offset;
Kevin Wolfecdd5332013-03-27 11:43:49 +01001808 }
1809
1810 assert(remaining >= cur_bytes);
1811
1812 start += cur_bytes;
1813 remaining -= cur_bytes;
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001814
1815 if (cluster_offset != INV_OFFSET) {
1816 cluster_offset += cur_bytes;
1817 }
Kevin Wolfecdd5332013-03-27 11:43:49 +01001818
1819 if (remaining == 0) {
1820 break;
1821 }
1822
1823 cur_bytes = remaining;
1824
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001825 /*
1826 * Now start gathering as many contiguous clusters as possible:
1827 *
1828 * 1. Check for overlaps with in-flight allocations
1829 *
1830 * a) Overlap not in the first cluster -> shorten this request and
1831 * let the caller handle the rest in its next loop iteration.
1832 *
1833 * b) Real overlaps of two requests. Yield and restart the search
1834 * for contiguous clusters (the situation could have changed
1835 * while we were sleeping)
1836 *
1837 * c) TODO: Request starts in the same cluster as the in-flight
1838 * allocation ends. Shorten the COW of the in-fight allocation,
1839 * set cluster_offset to write to the same cluster and set up
1840 * the right synchronisation between the in-flight request and
1841 * the new one.
1842 */
Kevin Wolfecdd5332013-03-27 11:43:49 +01001843 ret = handle_dependencies(bs, start, &cur_bytes, m);
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001844 if (ret == -EAGAIN) {
Kevin Wolfecdd5332013-03-27 11:43:49 +01001845 /* Currently handle_dependencies() doesn't yield if we already had
1846 * an allocation. If it did, we would have to clean up the L2Meta
1847 * structs before starting over. */
1848 assert(*m == NULL);
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001849 goto again;
1850 } else if (ret < 0) {
1851 return ret;
Kevin Wolfecdd5332013-03-27 11:43:49 +01001852 } else if (cur_bytes == 0) {
1853 break;
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001854 } else {
1855 /* handle_dependencies() may have decreased cur_bytes (shortened
1856 * the allocations below) so that the next dependency is processed
1857 * correctly during the next loop iteration. */
Kevin Wolf710c2492013-03-26 17:50:10 +01001858 }
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001859
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001860 /*
1861 * 2. Count contiguous COPIED clusters.
1862 */
1863 ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m);
1864 if (ret < 0) {
1865 return ret;
1866 } else if (ret) {
Kevin Wolfecdd5332013-03-27 11:43:49 +01001867 continue;
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001868 } else if (cur_bytes == 0) {
1869 break;
1870 }
1871
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001872 /*
1873 * 3. If the request still hasn't completed, allocate new clusters,
1874 * considering any cluster_offset of steps 1c or 2.
1875 */
1876 ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m);
1877 if (ret < 0) {
1878 return ret;
1879 } else if (ret) {
Kevin Wolfecdd5332013-03-27 11:43:49 +01001880 continue;
Kevin Wolf2c3b32d2013-03-26 17:50:12 +01001881 } else {
1882 assert(cur_bytes == 0);
1883 break;
1884 }
Kevin Wolff5bc6352013-03-26 17:50:01 +01001885 }
Kevin Wolf10f0ed82013-03-26 17:50:00 +01001886
Kevin Wolfd46a0bb2016-06-01 16:55:05 +02001887 *bytes -= remaining;
1888 assert(*bytes > 0);
Kevin Wolfc6d619c2019-01-18 14:40:36 +01001889 assert(*host_offset != INV_OFFSET);
Alberto Garciabfd09892020-09-11 16:09:42 +02001890 assert(offset_into_cluster(s, *host_offset) ==
1891 offset_into_cluster(s, offset));
Kevin Wolf45aba422009-05-28 16:07:05 +02001892
Kevin Wolf148da7e2010-01-20 15:03:01 +01001893 return 0;
Kevin Wolf45aba422009-05-28 16:07:05 +02001894}
1895
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001896/*
1897 * This discards as many clusters of nb_clusters as possible at once (i.e.
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001898 * all clusters in the same L2 slice) and returns the number of discarded
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001899 * clusters.
1900 */
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001901static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset,
1902 uint64_t nb_clusters,
1903 enum qcow2_discard_type type, bool full_discard)
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001904{
Kevin Wolfff991292015-09-07 17:12:56 +02001905 BDRVQcow2State *s = bs->opaque;
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001906 uint64_t *l2_slice;
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001907 int l2_index;
1908 int ret;
1909 int i;
1910
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001911 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001912 if (ret < 0) {
1913 return ret;
1914 }
1915
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001916 /* Limit nb_clusters to one L2 slice */
1917 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
Max Reitzb6d36de2015-09-14 16:39:47 +02001918 assert(nb_clusters <= INT_MAX);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001919
1920 for (i = 0; i < nb_clusters; i++) {
Alberto Garciaa68cd702020-07-10 18:13:05 +02001921 uint64_t old_l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
1922 uint64_t old_l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
1923 uint64_t new_l2_entry = old_l2_entry;
1924 uint64_t new_l2_bitmap = old_l2_bitmap;
1925 QCow2ClusterType cluster_type =
1926 qcow2_get_cluster_type(bs, old_l2_entry);
Jean-Louis Dupond42a28902023-06-05 10:45:24 +02001927 bool keep_reference = (cluster_type != QCOW2_CLUSTER_COMPRESSED) &&
1928 !full_discard &&
1929 (s->discard_no_unref &&
1930 type == QCOW2_DISCARD_REQUEST);
Kevin Wolfa71835a2014-02-08 14:38:33 +01001931
1932 /*
Alberto Garciaa68cd702020-07-10 18:13:05 +02001933 * If full_discard is true, the cluster should not read back as zeroes,
1934 * but rather fall through to the backing file.
1935 *
Max Reitz808c4b62014-10-24 15:57:30 +02001936 * If full_discard is false, make sure that a discarded area reads back
1937 * as zeroes for v3 images (we cannot do it for v2 without actually
1938 * writing a zero-filled buffer). We can skip the operation if the
1939 * cluster is already marked as zero, or if it's unallocated and we
1940 * don't have a backing file.
Kevin Wolfa71835a2014-02-08 14:38:33 +01001941 *
Eric Blake237d78f2017-10-11 22:47:03 -05001942 * TODO We might want to use bdrv_block_status(bs) here, but we're
Kevin Wolfa71835a2014-02-08 14:38:33 +01001943 * holding s->lock, so that doesn't work today.
1944 */
Alberto Garciaa68cd702020-07-10 18:13:05 +02001945 if (full_discard) {
1946 new_l2_entry = new_l2_bitmap = 0;
1947 } else if (bs->backing || qcow2_cluster_is_allocated(cluster_type)) {
1948 if (has_subclusters(s)) {
Jean-Louis Dupond42a28902023-06-05 10:45:24 +02001949 if (keep_reference) {
1950 new_l2_entry = old_l2_entry;
1951 } else {
1952 new_l2_entry = 0;
1953 }
Alberto Garciaa68cd702020-07-10 18:13:05 +02001954 new_l2_bitmap = QCOW_L2_BITMAP_ALL_ZEROES;
1955 } else {
Jean-Louis Dupond42a28902023-06-05 10:45:24 +02001956 if (s->qcow_version >= 3) {
1957 if (keep_reference) {
1958 new_l2_entry |= QCOW_OFLAG_ZERO;
1959 } else {
1960 new_l2_entry = QCOW_OFLAG_ZERO;
1961 }
1962 } else {
1963 new_l2_entry = 0;
1964 }
Eric Blakebbd995d2017-05-06 19:05:42 -05001965 }
Alberto Garciaa68cd702020-07-10 18:13:05 +02001966 }
Kevin Wolfa71835a2014-02-08 14:38:33 +01001967
Alberto Garciaa68cd702020-07-10 18:13:05 +02001968 if (old_l2_entry == new_l2_entry && old_l2_bitmap == new_l2_bitmap) {
1969 continue;
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001970 }
1971
1972 /* First remove L2 entries */
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001973 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
Alberto Garciaa68cd702020-07-10 18:13:05 +02001974 set_l2_entry(s, l2_slice, l2_index + i, new_l2_entry);
1975 if (has_subclusters(s)) {
1976 set_l2_bitmap(s, l2_slice, l2_index + i, new_l2_bitmap);
Kevin Wolfa71835a2014-02-08 14:38:33 +01001977 }
Jean-Louis Dupond42a28902023-06-05 10:45:24 +02001978 if (!keep_reference) {
1979 /* Then decrease the refcount */
1980 qcow2_free_any_cluster(bs, old_l2_entry, type);
1981 } else if (s->discard_passthrough[type] &&
1982 (cluster_type == QCOW2_CLUSTER_NORMAL ||
1983 cluster_type == QCOW2_CLUSTER_ZERO_ALLOC)) {
1984 /* If we keep the reference, pass on the discard still */
1985 bdrv_pdiscard(s->data_file, old_l2_entry & L2E_OFFSET_MASK,
1986 s->cluster_size);
1987 }
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001988 }
1989
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02001990 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001991
1992 return nb_clusters;
1993}
1994
Eric Blaked2cb36a2017-05-06 19:05:52 -05001995int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
1996 uint64_t bytes, enum qcow2_discard_type type,
1997 bool full_discard)
Kevin Wolf5ea929e2011-01-26 16:56:48 +01001998{
Kevin Wolfff991292015-09-07 17:12:56 +02001999 BDRVQcow2State *s = bs->opaque;
Eric Blaked2cb36a2017-05-06 19:05:52 -05002000 uint64_t end_offset = offset + bytes;
Max Reitzb6d36de2015-09-14 16:39:47 +02002001 uint64_t nb_clusters;
Eric Blaked2cb36a2017-05-06 19:05:52 -05002002 int64_t cleared;
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002003 int ret;
2004
Eric Blakef10ee132017-05-06 19:05:51 -05002005 /* Caller must pass aligned values, except at image end */
Eric Blake0c1bd462017-03-31 13:53:55 -05002006 assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
Eric Blakef10ee132017-05-06 19:05:51 -05002007 assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
2008 end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002009
Eric Blaked2cb36a2017-05-06 19:05:52 -05002010 nb_clusters = size_to_clusters(s, bytes);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002011
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002012 s->cache_discards = true;
2013
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02002014 /* Each L2 slice is handled by its own loop iteration */
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002015 while (nb_clusters > 0) {
Alberto Garcia21ab3ad2018-02-05 16:33:24 +02002016 cleared = discard_in_l2_slice(bs, offset, nb_clusters, type,
2017 full_discard);
Eric Blaked2cb36a2017-05-06 19:05:52 -05002018 if (cleared < 0) {
2019 ret = cleared;
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002020 goto fail;
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002021 }
2022
Eric Blaked2cb36a2017-05-06 19:05:52 -05002023 nb_clusters -= cleared;
2024 offset += (cleared * s->cluster_size);
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002025 }
2026
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002027 ret = 0;
2028fail:
2029 s->cache_discards = false;
2030 qcow2_process_discards(bs, ret);
2031
2032 return ret;
Kevin Wolf5ea929e2011-01-26 16:56:48 +01002033}
Kevin Wolf621f0582012-03-20 15:12:58 +01002034
2035/*
2036 * This zeroes as many clusters of nb_clusters as possible at once (i.e.
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002037 * all clusters in the same L2 slice) and returns the number of zeroed
Kevin Wolf621f0582012-03-20 15:12:58 +01002038 * clusters.
2039 */
Paolo Bonzini70bacc42023-06-01 13:51:43 +02002040static int coroutine_fn
2041zero_in_l2_slice(BlockDriverState *bs, uint64_t offset,
2042 uint64_t nb_clusters, int flags)
Kevin Wolf621f0582012-03-20 15:12:58 +01002043{
Kevin Wolfff991292015-09-07 17:12:56 +02002044 BDRVQcow2State *s = bs->opaque;
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002045 uint64_t *l2_slice;
Kevin Wolf621f0582012-03-20 15:12:58 +01002046 int l2_index;
2047 int ret;
2048 int i;
2049
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002050 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
Kevin Wolf621f0582012-03-20 15:12:58 +01002051 if (ret < 0) {
2052 return ret;
2053 }
2054
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002055 /* Limit nb_clusters to one L2 slice */
2056 nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);
Max Reitzb6d36de2015-09-14 16:39:47 +02002057 assert(nb_clusters <= INT_MAX);
Kevin Wolf621f0582012-03-20 15:12:58 +01002058
2059 for (i = 0; i < nb_clusters; i++) {
Alberto Garcia205fa502020-07-10 18:13:04 +02002060 uint64_t old_l2_entry = get_l2_entry(s, l2_slice, l2_index + i);
2061 uint64_t old_l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index + i);
2062 QCow2ClusterType type = qcow2_get_cluster_type(bs, old_l2_entry);
2063 bool unmap = (type == QCOW2_CLUSTER_COMPRESSED) ||
2064 ((flags & BDRV_REQ_MAY_UNMAP) && qcow2_cluster_is_allocated(type));
2065 uint64_t new_l2_entry = unmap ? 0 : old_l2_entry;
2066 uint64_t new_l2_bitmap = old_l2_bitmap;
Kevin Wolf621f0582012-03-20 15:12:58 +01002067
Alberto Garcia205fa502020-07-10 18:13:04 +02002068 if (has_subclusters(s)) {
2069 new_l2_bitmap = QCOW_L2_BITMAP_ALL_ZEROES;
2070 } else {
2071 new_l2_entry |= QCOW_OFLAG_ZERO;
2072 }
Kevin Wolf621f0582012-03-20 15:12:58 +01002073
Alberto Garcia205fa502020-07-10 18:13:04 +02002074 if (old_l2_entry == new_l2_entry && old_l2_bitmap == new_l2_bitmap) {
Eric Blake06cc5e22017-05-06 19:05:47 -05002075 continue;
2076 }
2077
Maxim Levitskyc8bf9a92020-11-24 10:28:15 +01002078 /* First update L2 entries */
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002079 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
Alberto Garcia205fa502020-07-10 18:13:04 +02002080 set_l2_entry(s, l2_slice, l2_index + i, new_l2_entry);
2081 if (has_subclusters(s)) {
2082 set_l2_bitmap(s, l2_slice, l2_index + i, new_l2_bitmap);
Kevin Wolf621f0582012-03-20 15:12:58 +01002083 }
Maxim Levitskyc8bf9a92020-11-24 10:28:15 +01002084
2085 /* Then decrease the refcount */
2086 if (unmap) {
2087 qcow2_free_any_cluster(bs, old_l2_entry, QCOW2_DISCARD_REQUEST);
2088 }
Kevin Wolf621f0582012-03-20 15:12:58 +01002089 }
2090
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002091 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf621f0582012-03-20 15:12:58 +01002092
2093 return nb_clusters;
2094}
2095
Paolo Bonzinia39bae42023-03-09 09:44:55 +01002096static int coroutine_fn
2097zero_l2_subclusters(BlockDriverState *bs, uint64_t offset,
2098 unsigned nb_subclusters)
Alberto Garciaa6841a22020-07-10 18:13:10 +02002099{
2100 BDRVQcow2State *s = bs->opaque;
2101 uint64_t *l2_slice;
2102 uint64_t old_l2_bitmap, l2_bitmap;
2103 int l2_index, ret, sc = offset_to_sc_index(s, offset);
2104
2105 /* For full clusters use zero_in_l2_slice() instead */
2106 assert(nb_subclusters > 0 && nb_subclusters < s->subclusters_per_cluster);
2107 assert(sc + nb_subclusters <= s->subclusters_per_cluster);
2108 assert(offset_into_subcluster(s, offset) == 0);
2109
2110 ret = get_cluster_table(bs, offset, &l2_slice, &l2_index);
2111 if (ret < 0) {
2112 return ret;
2113 }
2114
2115 switch (qcow2_get_cluster_type(bs, get_l2_entry(s, l2_slice, l2_index))) {
2116 case QCOW2_CLUSTER_COMPRESSED:
2117 ret = -ENOTSUP; /* We cannot partially zeroize compressed clusters */
2118 goto out;
2119 case QCOW2_CLUSTER_NORMAL:
2120 case QCOW2_CLUSTER_UNALLOCATED:
2121 break;
2122 default:
2123 g_assert_not_reached();
2124 }
2125
2126 old_l2_bitmap = l2_bitmap = get_l2_bitmap(s, l2_slice, l2_index);
2127
2128 l2_bitmap |= QCOW_OFLAG_SUB_ZERO_RANGE(sc, sc + nb_subclusters);
2129 l2_bitmap &= ~QCOW_OFLAG_SUB_ALLOC_RANGE(sc, sc + nb_subclusters);
2130
2131 if (old_l2_bitmap != l2_bitmap) {
2132 set_l2_bitmap(s, l2_slice, l2_index, l2_bitmap);
2133 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
2134 }
2135
2136 ret = 0;
2137out:
2138 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
2139
2140 return ret;
2141}
2142
Paolo Bonzini050ed2e2022-09-22 10:49:12 +02002143int coroutine_fn qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset,
2144 uint64_t bytes, int flags)
Kevin Wolf621f0582012-03-20 15:12:58 +01002145{
Kevin Wolfff991292015-09-07 17:12:56 +02002146 BDRVQcow2State *s = bs->opaque;
Eric Blaked2cb36a2017-05-06 19:05:52 -05002147 uint64_t end_offset = offset + bytes;
Max Reitzb6d36de2015-09-14 16:39:47 +02002148 uint64_t nb_clusters;
Alberto Garciaa6841a22020-07-10 18:13:10 +02002149 unsigned head, tail;
Eric Blaked2cb36a2017-05-06 19:05:52 -05002150 int64_t cleared;
Kevin Wolf621f0582012-03-20 15:12:58 +01002151 int ret;
2152
Kevin Wolf6c3944d2019-02-22 14:29:38 +01002153 /* If we have to stay in sync with an external data file, zero out
2154 * s->data_file first. */
2155 if (data_file_is_raw(bs)) {
2156 assert(has_data_file(bs));
2157 ret = bdrv_co_pwrite_zeroes(s->data_file, offset, bytes, flags);
2158 if (ret < 0) {
2159 return ret;
2160 }
2161 }
2162
Eric Blakef10ee132017-05-06 19:05:51 -05002163 /* Caller must pass aligned values, except at image end */
Alberto Garciaa6841a22020-07-10 18:13:10 +02002164 assert(offset_into_subcluster(s, offset) == 0);
2165 assert(offset_into_subcluster(s, end_offset) == 0 ||
Kevin Wolff01643f2020-04-24 14:54:42 +02002166 end_offset >= bs->total_sectors << BDRV_SECTOR_BITS);
Eric Blakef10ee132017-05-06 19:05:51 -05002167
Kevin Wolf61b30432020-07-21 15:55:19 +02002168 /*
2169 * The zero flag is only supported by version 3 and newer. However, if we
2170 * have no backing file, we can resort to discard in version 2.
2171 */
Kevin Wolf621f0582012-03-20 15:12:58 +01002172 if (s->qcow_version < 3) {
Kevin Wolf61b30432020-07-21 15:55:19 +02002173 if (!bs->backing) {
2174 return qcow2_cluster_discard(bs, offset, bytes,
2175 QCOW2_DISCARD_REQUEST, false);
2176 }
Kevin Wolf621f0582012-03-20 15:12:58 +01002177 return -ENOTSUP;
2178 }
2179
Alberto Garciaa6841a22020-07-10 18:13:10 +02002180 head = MIN(end_offset, ROUND_UP(offset, s->cluster_size)) - offset;
2181 offset += head;
2182
2183 tail = (end_offset >= bs->total_sectors << BDRV_SECTOR_BITS) ? 0 :
2184 end_offset - MAX(offset, start_of_cluster(s, end_offset));
2185 end_offset -= tail;
Kevin Wolf621f0582012-03-20 15:12:58 +01002186
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002187 s->cache_discards = true;
2188
Alberto Garciaa6841a22020-07-10 18:13:10 +02002189 if (head) {
2190 ret = zero_l2_subclusters(bs, offset - head,
2191 size_to_subclusters(s, head));
2192 if (ret < 0) {
2193 goto fail;
2194 }
2195 }
2196
2197 /* Each L2 slice is handled by its own loop iteration */
2198 nb_clusters = size_to_clusters(s, end_offset - offset);
2199
Kevin Wolf621f0582012-03-20 15:12:58 +01002200 while (nb_clusters > 0) {
Alberto Garciaa9a9f8f2018-02-05 16:33:25 +02002201 cleared = zero_in_l2_slice(bs, offset, nb_clusters, flags);
Eric Blaked2cb36a2017-05-06 19:05:52 -05002202 if (cleared < 0) {
2203 ret = cleared;
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002204 goto fail;
Kevin Wolf621f0582012-03-20 15:12:58 +01002205 }
2206
Eric Blaked2cb36a2017-05-06 19:05:52 -05002207 nb_clusters -= cleared;
2208 offset += (cleared * s->cluster_size);
Kevin Wolf621f0582012-03-20 15:12:58 +01002209 }
2210
Alberto Garciaa6841a22020-07-10 18:13:10 +02002211 if (tail) {
2212 ret = zero_l2_subclusters(bs, end_offset, size_to_subclusters(s, tail));
2213 if (ret < 0) {
2214 goto fail;
2215 }
2216 }
2217
Kevin Wolf0b919fa2013-06-19 13:44:20 +02002218 ret = 0;
2219fail:
2220 s->cache_discards = false;
2221 qcow2_process_discards(bs, ret);
2222
2223 return ret;
Kevin Wolf621f0582012-03-20 15:12:58 +01002224}
Max Reitz32b64442013-09-03 10:09:52 +02002225
2226/*
2227 * Expands all zero clusters in a specific L1 table (or deallocates them, for
2228 * non-backed non-pre-allocated zero clusters).
2229 *
Max Reitz4057a2b2014-10-27 11:12:53 +01002230 * l1_entries and *visited_l1_entries are used to keep track of progress for
2231 * status_cb(). l1_entries contains the total number of L1 entries and
2232 * *visited_l1_entries counts all visited L1 entries.
Max Reitz32b64442013-09-03 10:09:52 +02002233 */
2234static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
Max Reitzecf58772014-10-27 11:12:55 +01002235 int l1_size, int64_t *visited_l1_entries,
Max Reitz4057a2b2014-10-27 11:12:53 +01002236 int64_t l1_entries,
Max Reitz8b139762015-07-27 17:51:32 +02002237 BlockDriverAmendStatusCB *status_cb,
2238 void *cb_opaque)
Max Reitz32b64442013-09-03 10:09:52 +02002239{
Kevin Wolfff991292015-09-07 17:12:56 +02002240 BDRVQcow2State *s = bs->opaque;
Max Reitz32b64442013-09-03 10:09:52 +02002241 bool is_active_l1 = (l1_table == s->l1_table);
Alberto Garcia415184f2018-02-05 16:33:30 +02002242 uint64_t *l2_slice = NULL;
2243 unsigned slice, slice_size2, n_slices;
Max Reitz32b64442013-09-03 10:09:52 +02002244 int ret;
2245 int i, j;
2246
Alberto Garcia7bbb5922020-07-10 18:13:15 +02002247 /* qcow2_downgrade() is not allowed in images with subclusters */
2248 assert(!has_subclusters(s));
2249
Alberto Garciac8fd8552020-07-10 18:12:54 +02002250 slice_size2 = s->l2_slice_size * l2_entry_size(s);
Alberto Garcia415184f2018-02-05 16:33:30 +02002251 n_slices = s->cluster_size / slice_size2;
2252
Max Reitz32b64442013-09-03 10:09:52 +02002253 if (!is_active_l1) {
2254 /* inactive L2 tables require a buffer to be stored in when loading
2255 * them from disk */
Alberto Garcia415184f2018-02-05 16:33:30 +02002256 l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
2257 if (l2_slice == NULL) {
Kevin Wolfde828152014-05-20 17:12:47 +02002258 return -ENOMEM;
2259 }
Max Reitz32b64442013-09-03 10:09:52 +02002260 }
2261
2262 for (i = 0; i < l1_size; i++) {
2263 uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
Max Reitz0e065282015-02-10 15:28:48 -05002264 uint64_t l2_refcount;
Max Reitz32b64442013-09-03 10:09:52 +02002265
2266 if (!l2_offset) {
2267 /* unallocated */
Max Reitz4057a2b2014-10-27 11:12:53 +01002268 (*visited_l1_entries)++;
2269 if (status_cb) {
Max Reitz8b139762015-07-27 17:51:32 +02002270 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
Max Reitz4057a2b2014-10-27 11:12:53 +01002271 }
Max Reitz32b64442013-09-03 10:09:52 +02002272 continue;
2273 }
2274
Max Reitz8dd93d92015-01-19 15:49:03 -05002275 if (offset_into_cluster(s, l2_offset)) {
2276 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
2277 PRIx64 " unaligned (L1 index: %#x)",
2278 l2_offset, i);
2279 ret = -EIO;
2280 goto fail;
2281 }
2282
Alberto Garcia9b765482018-02-05 16:33:28 +02002283 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
2284 &l2_refcount);
2285 if (ret < 0) {
2286 goto fail;
2287 }
2288
Alberto Garcia415184f2018-02-05 16:33:30 +02002289 for (slice = 0; slice < n_slices; slice++) {
2290 uint64_t slice_offset = l2_offset + slice * slice_size2;
2291 bool l2_dirty = false;
Alberto Garcia226494f2018-02-05 16:33:29 +02002292 if (is_active_l1) {
2293 /* get active L2 tables from cache */
Alberto Garcia415184f2018-02-05 16:33:30 +02002294 ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
2295 (void **)&l2_slice);
Alberto Garcia226494f2018-02-05 16:33:29 +02002296 } else {
2297 /* load inactive L2 tables from disk */
Alberto Faria32cc71d2022-06-09 16:27:36 +01002298 ret = bdrv_pread(bs->file, slice_offset, slice_size2,
2299 l2_slice, 0);
Alberto Garcia226494f2018-02-05 16:33:29 +02002300 }
2301 if (ret < 0) {
2302 goto fail;
Max Reitz32b64442013-09-03 10:09:52 +02002303 }
2304
Alberto Garcia415184f2018-02-05 16:33:30 +02002305 for (j = 0; j < s->l2_slice_size; j++) {
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02002306 uint64_t l2_entry = get_l2_entry(s, l2_slice, j);
Alberto Garcia226494f2018-02-05 16:33:29 +02002307 int64_t offset = l2_entry & L2E_OFFSET_MASK;
2308 QCow2ClusterType cluster_type =
Kevin Wolf808c2bb2019-01-17 18:42:40 +01002309 qcow2_get_cluster_type(bs, l2_entry);
Alberto Garcia226494f2018-02-05 16:33:29 +02002310
2311 if (cluster_type != QCOW2_CLUSTER_ZERO_PLAIN &&
2312 cluster_type != QCOW2_CLUSTER_ZERO_ALLOC) {
Max Reitz32b64442013-09-03 10:09:52 +02002313 continue;
2314 }
2315
Alberto Garcia226494f2018-02-05 16:33:29 +02002316 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2317 if (!bs->backing) {
Alberto Garcia7bbb5922020-07-10 18:13:15 +02002318 /*
2319 * not backed; therefore we can simply deallocate the
2320 * cluster. No need to call set_l2_bitmap(), this
2321 * function doesn't support images with subclusters.
2322 */
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02002323 set_l2_entry(s, l2_slice, j, 0);
Alberto Garcia226494f2018-02-05 16:33:29 +02002324 l2_dirty = true;
2325 continue;
2326 }
Max Reitzecf58772014-10-27 11:12:55 +01002327
Alberto Garcia226494f2018-02-05 16:33:29 +02002328 offset = qcow2_alloc_clusters(bs, s->cluster_size);
2329 if (offset < 0) {
2330 ret = offset;
Max Reitzecf58772014-10-27 11:12:55 +01002331 goto fail;
2332 }
Alberto Garcia226494f2018-02-05 16:33:29 +02002333
Alberto Garcia3a75a872020-01-13 17:11:46 +01002334 /* The offset must fit in the offset field */
2335 assert((offset & L2E_OFFSET_MASK) == offset);
2336
Alberto Garcia226494f2018-02-05 16:33:29 +02002337 if (l2_refcount > 1) {
2338 /* For shared L2 tables, set the refcount accordingly
2339 * (it is already 1 and needs to be l2_refcount) */
2340 ret = qcow2_update_cluster_refcount(
2341 bs, offset >> s->cluster_bits,
2342 refcount_diff(1, l2_refcount), false,
2343 QCOW2_DISCARD_OTHER);
2344 if (ret < 0) {
2345 qcow2_free_clusters(bs, offset, s->cluster_size,
2346 QCOW2_DISCARD_OTHER);
2347 goto fail;
2348 }
2349 }
Max Reitzecf58772014-10-27 11:12:55 +01002350 }
Alberto Garcia226494f2018-02-05 16:33:29 +02002351
2352 if (offset_into_cluster(s, offset)) {
Alberto Garcia415184f2018-02-05 16:33:30 +02002353 int l2_index = slice * s->l2_slice_size + j;
Alberto Garcia226494f2018-02-05 16:33:29 +02002354 qcow2_signal_corruption(
2355 bs, true, -1, -1,
2356 "Cluster allocation offset "
2357 "%#" PRIx64 " unaligned (L2 offset: %#"
2358 PRIx64 ", L2 index: %#x)", offset,
Alberto Garcia415184f2018-02-05 16:33:30 +02002359 l2_offset, l2_index);
Alberto Garcia226494f2018-02-05 16:33:29 +02002360 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2361 qcow2_free_clusters(bs, offset, s->cluster_size,
2362 QCOW2_DISCARD_ALWAYS);
2363 }
2364 ret = -EIO;
2365 goto fail;
2366 }
2367
2368 ret = qcow2_pre_write_overlap_check(bs, 0, offset,
Kevin Wolf966b0002019-01-15 20:39:06 +01002369 s->cluster_size, true);
Alberto Garcia226494f2018-02-05 16:33:29 +02002370 if (ret < 0) {
2371 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2372 qcow2_free_clusters(bs, offset, s->cluster_size,
2373 QCOW2_DISCARD_ALWAYS);
2374 }
2375 goto fail;
2376 }
2377
Kevin Wolf966b0002019-01-15 20:39:06 +01002378 ret = bdrv_pwrite_zeroes(s->data_file, offset,
2379 s->cluster_size, 0);
Alberto Garcia226494f2018-02-05 16:33:29 +02002380 if (ret < 0) {
2381 if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
2382 qcow2_free_clusters(bs, offset, s->cluster_size,
2383 QCOW2_DISCARD_ALWAYS);
2384 }
2385 goto fail;
2386 }
2387
2388 if (l2_refcount == 1) {
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02002389 set_l2_entry(s, l2_slice, j, offset | QCOW_OFLAG_COPIED);
Alberto Garcia226494f2018-02-05 16:33:29 +02002390 } else {
Alberto Garcia12c6aeb2020-07-10 18:12:48 +02002391 set_l2_entry(s, l2_slice, j, offset);
Alberto Garcia226494f2018-02-05 16:33:29 +02002392 }
Alberto Garcia7bbb5922020-07-10 18:13:15 +02002393 /*
2394 * No need to call set_l2_bitmap() after set_l2_entry() because
2395 * this function doesn't support images with subclusters.
2396 */
Alberto Garcia226494f2018-02-05 16:33:29 +02002397 l2_dirty = true;
Max Reitz32b64442013-09-03 10:09:52 +02002398 }
2399
Alberto Garcia226494f2018-02-05 16:33:29 +02002400 if (is_active_l1) {
2401 if (l2_dirty) {
Alberto Garcia415184f2018-02-05 16:33:30 +02002402 qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
Alberto Garcia226494f2018-02-05 16:33:29 +02002403 qcow2_cache_depends_on_flush(s->l2_table_cache);
Max Reitz8dd93d92015-01-19 15:49:03 -05002404 }
Alberto Garcia415184f2018-02-05 16:33:30 +02002405 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Max Reitzecf58772014-10-27 11:12:55 +01002406 } else {
Alberto Garcia226494f2018-02-05 16:33:29 +02002407 if (l2_dirty) {
2408 ret = qcow2_pre_write_overlap_check(
2409 bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
Kevin Wolf966b0002019-01-15 20:39:06 +01002410 slice_offset, slice_size2, false);
Alberto Garcia226494f2018-02-05 16:33:29 +02002411 if (ret < 0) {
2412 goto fail;
2413 }
Max Reitz32b64442013-09-03 10:09:52 +02002414
Alberto Faria32cc71d2022-06-09 16:27:36 +01002415 ret = bdrv_pwrite(bs->file, slice_offset, slice_size2,
2416 l2_slice, 0);
Alberto Garcia226494f2018-02-05 16:33:29 +02002417 if (ret < 0) {
2418 goto fail;
2419 }
Max Reitz32b64442013-09-03 10:09:52 +02002420 }
2421 }
2422 }
Max Reitz4057a2b2014-10-27 11:12:53 +01002423
2424 (*visited_l1_entries)++;
2425 if (status_cb) {
Max Reitz8b139762015-07-27 17:51:32 +02002426 status_cb(bs, *visited_l1_entries, l1_entries, cb_opaque);
Max Reitz4057a2b2014-10-27 11:12:53 +01002427 }
Max Reitz32b64442013-09-03 10:09:52 +02002428 }
2429
2430 ret = 0;
2431
2432fail:
Alberto Garcia415184f2018-02-05 16:33:30 +02002433 if (l2_slice) {
Max Reitz32b64442013-09-03 10:09:52 +02002434 if (!is_active_l1) {
Alberto Garcia415184f2018-02-05 16:33:30 +02002435 qemu_vfree(l2_slice);
Max Reitz32b64442013-09-03 10:09:52 +02002436 } else {
Alberto Garcia415184f2018-02-05 16:33:30 +02002437 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Max Reitz32b64442013-09-03 10:09:52 +02002438 }
2439 }
2440 return ret;
2441}
2442
2443/*
2444 * For backed images, expands all zero clusters on the image. For non-backed
2445 * images, deallocates all non-pre-allocated zero clusters (and claims the
2446 * allocation for pre-allocated ones). This is important for downgrading to a
2447 * qcow2 version which doesn't yet support metadata zero clusters.
2448 */
Max Reitz4057a2b2014-10-27 11:12:53 +01002449int qcow2_expand_zero_clusters(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +02002450 BlockDriverAmendStatusCB *status_cb,
2451 void *cb_opaque)
Max Reitz32b64442013-09-03 10:09:52 +02002452{
Kevin Wolfff991292015-09-07 17:12:56 +02002453 BDRVQcow2State *s = bs->opaque;
Max Reitz32b64442013-09-03 10:09:52 +02002454 uint64_t *l1_table = NULL;
Max Reitz4057a2b2014-10-27 11:12:53 +01002455 int64_t l1_entries = 0, visited_l1_entries = 0;
Max Reitz32b64442013-09-03 10:09:52 +02002456 int ret;
2457 int i, j;
2458
Max Reitz4057a2b2014-10-27 11:12:53 +01002459 if (status_cb) {
2460 l1_entries = s->l1_size;
2461 for (i = 0; i < s->nb_snapshots; i++) {
2462 l1_entries += s->snapshots[i].l1_size;
2463 }
2464 }
2465
Max Reitz32b64442013-09-03 10:09:52 +02002466 ret = expand_zero_clusters_in_l1(bs, s->l1_table, s->l1_size,
Max Reitz4057a2b2014-10-27 11:12:53 +01002467 &visited_l1_entries, l1_entries,
Max Reitz8b139762015-07-27 17:51:32 +02002468 status_cb, cb_opaque);
Max Reitz32b64442013-09-03 10:09:52 +02002469 if (ret < 0) {
2470 goto fail;
2471 }
2472
2473 /* Inactive L1 tables may point to active L2 tables - therefore it is
2474 * necessary to flush the L2 table cache before trying to access the L2
2475 * tables pointed to by inactive L1 entries (else we might try to expand
2476 * zero clusters that have already been expanded); furthermore, it is also
2477 * necessary to empty the L2 table cache, since it may contain tables which
2478 * are now going to be modified directly on disk, bypassing the cache.
2479 * qcow2_cache_empty() does both for us. */
2480 ret = qcow2_cache_empty(bs, s->l2_table_cache);
2481 if (ret < 0) {
2482 goto fail;
2483 }
2484
2485 for (i = 0; i < s->nb_snapshots; i++) {
Alberto Garciac9a442e2018-03-06 18:14:08 +02002486 int l1_size2;
2487 uint64_t *new_l1_table;
2488 Error *local_err = NULL;
Max Reitz32b64442013-09-03 10:09:52 +02002489
Alberto Garciac9a442e2018-03-06 18:14:08 +02002490 ret = qcow2_validate_table(bs, s->snapshots[i].l1_table_offset,
Alberto Garcia02b1ecf2020-08-28 13:08:28 +02002491 s->snapshots[i].l1_size, L1E_SIZE,
Alberto Garciac9a442e2018-03-06 18:14:08 +02002492 QCOW_MAX_L1_SIZE, "Snapshot L1 table",
2493 &local_err);
2494 if (ret < 0) {
2495 error_report_err(local_err);
2496 goto fail;
2497 }
2498
Alberto Garcia02b1ecf2020-08-28 13:08:28 +02002499 l1_size2 = s->snapshots[i].l1_size * L1E_SIZE;
Alberto Garciac9a442e2018-03-06 18:14:08 +02002500 new_l1_table = g_try_realloc(l1_table, l1_size2);
Alberto Garciade7269d2018-02-09 16:42:22 +02002501
2502 if (!new_l1_table) {
2503 ret = -ENOMEM;
2504 goto fail;
2505 }
2506
2507 l1_table = new_l1_table;
Max Reitz32b64442013-09-03 10:09:52 +02002508
Alberto Faria32cc71d2022-06-09 16:27:36 +01002509 ret = bdrv_pread(bs->file, s->snapshots[i].l1_table_offset, l1_size2,
2510 l1_table, 0);
Max Reitz32b64442013-09-03 10:09:52 +02002511 if (ret < 0) {
2512 goto fail;
2513 }
2514
2515 for (j = 0; j < s->snapshots[i].l1_size; j++) {
2516 be64_to_cpus(&l1_table[j]);
2517 }
2518
2519 ret = expand_zero_clusters_in_l1(bs, l1_table, s->snapshots[i].l1_size,
Max Reitz4057a2b2014-10-27 11:12:53 +01002520 &visited_l1_entries, l1_entries,
Max Reitz8b139762015-07-27 17:51:32 +02002521 status_cb, cb_opaque);
Max Reitz32b64442013-09-03 10:09:52 +02002522 if (ret < 0) {
2523 goto fail;
2524 }
2525 }
2526
2527 ret = 0;
2528
2529fail:
Max Reitz32b64442013-09-03 10:09:52 +02002530 g_free(l1_table);
2531 return ret;
2532}
Vladimir Sementsov-Ogievskiya6e09842021-09-14 15:24:47 +03002533
2534void qcow2_parse_compressed_l2_entry(BlockDriverState *bs, uint64_t l2_entry,
2535 uint64_t *coffset, int *csize)
2536{
2537 BDRVQcow2State *s = bs->opaque;
2538 int nb_csectors;
2539
2540 assert(qcow2_get_cluster_type(bs, l2_entry) == QCOW2_CLUSTER_COMPRESSED);
2541
2542 *coffset = l2_entry & s->cluster_offset_mask;
2543
2544 nb_csectors = ((l2_entry >> s->csize_shift) & s->csize_mask) + 1;
2545 *csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
2546 (*coffset & (QCOW2_COMPRESSED_SECTOR_SIZE - 1));
2547}