blob: 1c63ac244ac3aafd04b02f6f84af0c9ee91e537b [file] [log] [blame]
Kevin Wolff7d0fe02009-05-28 16:07:04 +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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Kevin Wolff7d0fe02009-05-28 16:07:04 +020027#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010028#include "block/block_int.h"
Michael S. Tsirkin0d8c41d2018-05-03 22:50:20 +030029#include "qcow2.h"
Max Reitza40f1c22013-08-30 14:34:25 +020030#include "qemu/range.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010031#include "qemu/bswap.h"
Pavel Butsykin46b732c2017-09-18 15:42:29 +030032#include "qemu/cutils.h"
Kevin Wolff7d0fe02009-05-28 16:07:04 +020033
Eric Blake77d6a212018-11-13 17:03:18 -060034static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size,
35 uint64_t max);
Kevin Wolf92dcb592010-02-23 16:40:53 +010036static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
Max Reitz0e065282015-02-10 15:28:48 -050037 int64_t offset, int64_t length, uint64_t addend,
Max Reitz2aabe7c2015-02-10 15:28:47 -050038 bool decrease, enum qcow2_discard_type type);
Kevin Wolff7d0fe02009-05-28 16:07:04 +020039
Max Reitz59c0cb72015-02-10 15:28:51 -050040static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index);
41static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index);
42static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index);
43static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index);
Max Reitz7453c962015-02-10 15:28:50 -050044static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index);
Max Reitz59c0cb72015-02-10 15:28:51 -050045static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index);
46static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index);
Max Reitz7453c962015-02-10 15:28:50 -050047
Max Reitz59c0cb72015-02-10 15:28:51 -050048static void set_refcount_ro0(void *refcount_array, uint64_t index,
49 uint64_t value);
50static void set_refcount_ro1(void *refcount_array, uint64_t index,
51 uint64_t value);
52static void set_refcount_ro2(void *refcount_array, uint64_t index,
53 uint64_t value);
54static void set_refcount_ro3(void *refcount_array, uint64_t index,
55 uint64_t value);
Max Reitz7453c962015-02-10 15:28:50 -050056static void set_refcount_ro4(void *refcount_array, uint64_t index,
57 uint64_t value);
Max Reitz59c0cb72015-02-10 15:28:51 -050058static void set_refcount_ro5(void *refcount_array, uint64_t index,
59 uint64_t value);
60static void set_refcount_ro6(void *refcount_array, uint64_t index,
61 uint64_t value);
62
63
64static Qcow2GetRefcountFunc *const get_refcount_funcs[] = {
65 &get_refcount_ro0,
66 &get_refcount_ro1,
67 &get_refcount_ro2,
68 &get_refcount_ro3,
69 &get_refcount_ro4,
70 &get_refcount_ro5,
71 &get_refcount_ro6
72};
73
74static Qcow2SetRefcountFunc *const set_refcount_funcs[] = {
75 &set_refcount_ro0,
76 &set_refcount_ro1,
77 &set_refcount_ro2,
78 &set_refcount_ro3,
79 &set_refcount_ro4,
80 &set_refcount_ro5,
81 &set_refcount_ro6
82};
Max Reitz7453c962015-02-10 15:28:50 -050083
Kevin Wolf3b88e522009-06-26 20:19:38 +020084
Kevin Wolff7d0fe02009-05-28 16:07:04 +020085/*********************************************************/
86/* refcount handling */
87
Alberto Garcia7061a072017-02-01 14:38:28 +020088static void update_max_refcount_table_index(BDRVQcow2State *s)
89{
90 unsigned i = s->refcount_table_size - 1;
91 while (i > 0 && (s->refcount_table[i] & REFT_OFFSET_MASK) == 0) {
92 i--;
93 }
94 /* Set s->max_refcount_table_index to the index of the last used entry */
95 s->max_refcount_table_index = i;
96}
97
Kevin Wolfed6ccf02009-05-28 16:07:07 +020098int qcow2_refcount_init(BlockDriverState *bs)
Kevin Wolff7d0fe02009-05-28 16:07:04 +020099{
Kevin Wolfff991292015-09-07 17:12:56 +0200100 BDRVQcow2State *s = bs->opaque;
Kevin Wolf5dab2fa2014-03-26 13:05:43 +0100101 unsigned int refcount_table_size2, i;
102 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200103
Max Reitz59c0cb72015-02-10 15:28:51 -0500104 assert(s->refcount_order >= 0 && s->refcount_order <= 6);
105
106 s->get_refcount = get_refcount_funcs[s->refcount_order];
107 s->set_refcount = set_refcount_funcs[s->refcount_order];
Max Reitz7453c962015-02-10 15:28:50 -0500108
Kevin Wolf5dab2fa2014-03-26 13:05:43 +0100109 assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t));
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200110 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
Kevin Wolfde828152014-05-20 17:12:47 +0200111 s->refcount_table = g_try_malloc(refcount_table_size2);
112
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200113 if (s->refcount_table_size > 0) {
Kevin Wolfde828152014-05-20 17:12:47 +0200114 if (s->refcount_table == NULL) {
Max Reitz8fcffa92014-05-29 00:19:54 +0200115 ret = -ENOMEM;
Kevin Wolfde828152014-05-20 17:12:47 +0200116 goto fail;
117 }
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200118 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200119 ret = bdrv_pread(bs->file, s->refcount_table_offset,
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200120 s->refcount_table, refcount_table_size2);
Max Reitz8fcffa92014-05-29 00:19:54 +0200121 if (ret < 0) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200122 goto fail;
Max Reitz8fcffa92014-05-29 00:19:54 +0200123 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200124 for(i = 0; i < s->refcount_table_size; i++)
125 be64_to_cpus(&s->refcount_table[i]);
Alberto Garcia7061a072017-02-01 14:38:28 +0200126 update_max_refcount_table_index(s);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200127 }
128 return 0;
129 fail:
Max Reitz8fcffa92014-05-29 00:19:54 +0200130 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200131}
132
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200133void qcow2_refcount_close(BlockDriverState *bs)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200134{
Kevin Wolfff991292015-09-07 17:12:56 +0200135 BDRVQcow2State *s = bs->opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500136 g_free(s->refcount_table);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200137}
138
139
Max Reitz59c0cb72015-02-10 15:28:51 -0500140static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index)
141{
142 return (((const uint8_t *)refcount_array)[index / 8] >> (index % 8)) & 0x1;
143}
144
145static void set_refcount_ro0(void *refcount_array, uint64_t index,
146 uint64_t value)
147{
148 assert(!(value >> 1));
149 ((uint8_t *)refcount_array)[index / 8] &= ~(0x1 << (index % 8));
150 ((uint8_t *)refcount_array)[index / 8] |= value << (index % 8);
151}
152
153static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index)
154{
155 return (((const uint8_t *)refcount_array)[index / 4] >> (2 * (index % 4)))
156 & 0x3;
157}
158
159static void set_refcount_ro1(void *refcount_array, uint64_t index,
160 uint64_t value)
161{
162 assert(!(value >> 2));
163 ((uint8_t *)refcount_array)[index / 4] &= ~(0x3 << (2 * (index % 4)));
164 ((uint8_t *)refcount_array)[index / 4] |= value << (2 * (index % 4));
165}
166
167static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index)
168{
169 return (((const uint8_t *)refcount_array)[index / 2] >> (4 * (index % 2)))
170 & 0xf;
171}
172
173static void set_refcount_ro2(void *refcount_array, uint64_t index,
174 uint64_t value)
175{
176 assert(!(value >> 4));
177 ((uint8_t *)refcount_array)[index / 2] &= ~(0xf << (4 * (index % 2)));
178 ((uint8_t *)refcount_array)[index / 2] |= value << (4 * (index % 2));
179}
180
181static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index)
182{
183 return ((const uint8_t *)refcount_array)[index];
184}
185
186static void set_refcount_ro3(void *refcount_array, uint64_t index,
187 uint64_t value)
188{
189 assert(!(value >> 8));
190 ((uint8_t *)refcount_array)[index] = value;
191}
192
Max Reitz7453c962015-02-10 15:28:50 -0500193static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index)
194{
195 return be16_to_cpu(((const uint16_t *)refcount_array)[index]);
196}
197
198static void set_refcount_ro4(void *refcount_array, uint64_t index,
199 uint64_t value)
200{
201 assert(!(value >> 16));
202 ((uint16_t *)refcount_array)[index] = cpu_to_be16(value);
203}
204
Max Reitz59c0cb72015-02-10 15:28:51 -0500205static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index)
206{
207 return be32_to_cpu(((const uint32_t *)refcount_array)[index]);
208}
209
210static void set_refcount_ro5(void *refcount_array, uint64_t index,
211 uint64_t value)
212{
213 assert(!(value >> 32));
214 ((uint32_t *)refcount_array)[index] = cpu_to_be32(value);
215}
216
217static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index)
218{
219 return be64_to_cpu(((const uint64_t *)refcount_array)[index]);
220}
221
222static void set_refcount_ro6(void *refcount_array, uint64_t index,
223 uint64_t value)
224{
225 ((uint64_t *)refcount_array)[index] = cpu_to_be64(value);
226}
227
Max Reitz7453c962015-02-10 15:28:50 -0500228
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200229static int load_refcount_block(BlockDriverState *bs,
Kevin Wolf29c1a732011-01-10 17:17:28 +0100230 int64_t refcount_block_offset,
231 void **refcount_block)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200232{
Kevin Wolfff991292015-09-07 17:12:56 +0200233 BDRVQcow2State *s = bs->opaque;
Kevin Wolf3b88e522009-06-26 20:19:38 +0200234
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200235 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD);
Eduardo Habkost9be38592016-06-13 18:57:58 -0300236 return qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
237 refcount_block);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200238}
239
Kevin Wolf018faaf2010-06-04 11:16:11 +0200240/*
Max Reitz7324c102015-02-10 15:28:46 -0500241 * Retrieves the refcount of the cluster given by its index and stores it in
242 * *refcount. Returns 0 on success and -errno on failure.
Kevin Wolf018faaf2010-06-04 11:16:11 +0200243 */
Max Reitz7324c102015-02-10 15:28:46 -0500244int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
Max Reitz0e065282015-02-10 15:28:48 -0500245 uint64_t *refcount)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200246{
Kevin Wolfff991292015-09-07 17:12:56 +0200247 BDRVQcow2State *s = bs->opaque;
Kevin Wolfdb8a31d2014-03-26 13:05:49 +0100248 uint64_t refcount_table_index, block_index;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200249 int64_t refcount_block_offset;
Kevin Wolf018faaf2010-06-04 11:16:11 +0200250 int ret;
Max Reitz7453c962015-02-10 15:28:50 -0500251 void *refcount_block;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200252
Max Reitz17bd5f42014-09-03 00:25:07 +0200253 refcount_table_index = cluster_index >> s->refcount_block_bits;
Max Reitz7324c102015-02-10 15:28:46 -0500254 if (refcount_table_index >= s->refcount_table_size) {
255 *refcount = 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200256 return 0;
Max Reitz7324c102015-02-10 15:28:46 -0500257 }
Max Reitz26d49c42014-03-07 23:10:12 +0100258 refcount_block_offset =
259 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
Max Reitz7324c102015-02-10 15:28:46 -0500260 if (!refcount_block_offset) {
261 *refcount = 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200262 return 0;
Max Reitz7324c102015-02-10 15:28:46 -0500263 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100264
Max Reitza97c67e2014-09-05 16:07:18 +0200265 if (offset_into_cluster(s, refcount_block_offset)) {
266 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#" PRIx64
267 " unaligned (reftable index: %#" PRIx64 ")",
268 refcount_block_offset, refcount_table_index);
269 return -EIO;
270 }
271
Kevin Wolf29c1a732011-01-10 17:17:28 +0100272 ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset,
Max Reitz7453c962015-02-10 15:28:50 -0500273 &refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100274 if (ret < 0) {
275 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200276 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100277
Max Reitz17bd5f42014-09-03 00:25:07 +0200278 block_index = cluster_index & (s->refcount_block_size - 1);
Max Reitz7453c962015-02-10 15:28:50 -0500279 *refcount = s->get_refcount(refcount_block, block_index);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100280
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200281 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100282
Max Reitz7324c102015-02-10 15:28:46 -0500283 return 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200284}
285
Kevin Wolf92dcb592010-02-23 16:40:53 +0100286/* Checks if two offsets are described by the same refcount block */
Kevin Wolfff991292015-09-07 17:12:56 +0200287static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a,
Kevin Wolf92dcb592010-02-23 16:40:53 +0100288 uint64_t offset_b)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200289{
Max Reitz17bd5f42014-09-03 00:25:07 +0200290 uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
291 uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200292
Kevin Wolf92dcb592010-02-23 16:40:53 +0100293 return (block_a == block_b);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200294}
295
Kevin Wolf92dcb592010-02-23 16:40:53 +0100296/*
297 * Loads a refcount block. If it doesn't exist yet, it is allocated first
298 * (including growing the refcount table if needed).
299 *
Kevin Wolf29c1a732011-01-10 17:17:28 +0100300 * Returns 0 on success or -errno in error case
Kevin Wolf92dcb592010-02-23 16:40:53 +0100301 */
Kevin Wolf29c1a732011-01-10 17:17:28 +0100302static int alloc_refcount_block(BlockDriverState *bs,
Max Reitz7453c962015-02-10 15:28:50 -0500303 int64_t cluster_index, void **refcount_block)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200304{
Kevin Wolfff991292015-09-07 17:12:56 +0200305 BDRVQcow2State *s = bs->opaque;
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200306 unsigned int refcount_table_index;
Max Reitz12cc30a2017-06-13 22:21:03 +0200307 int64_t ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200308
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200309 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
Kevin Wolf82522782010-03-15 17:38:05 +0100310
Kevin Wolf92dcb592010-02-23 16:40:53 +0100311 /* Find the refcount block for the given cluster */
Max Reitz17bd5f42014-09-03 00:25:07 +0200312 refcount_table_index = cluster_index >> s->refcount_block_bits;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100313
314 if (refcount_table_index < s->refcount_table_size) {
315
316 uint64_t refcount_block_offset =
Kevin Wolf76dc9e02012-03-16 14:09:08 +0100317 s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100318
319 /* If it's already there, we're done */
320 if (refcount_block_offset) {
Max Reitza97c67e2014-09-05 16:07:18 +0200321 if (offset_into_cluster(s, refcount_block_offset)) {
322 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
323 PRIx64 " unaligned (reftable index: "
324 "%#x)", refcount_block_offset,
325 refcount_table_index);
326 return -EIO;
327 }
328
Kevin Wolf29c1a732011-01-10 17:17:28 +0100329 return load_refcount_block(bs, refcount_block_offset,
Max Reitz7453c962015-02-10 15:28:50 -0500330 refcount_block);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100331 }
332 }
333
334 /*
335 * If we came here, we need to allocate something. Something is at least
336 * a cluster for the new refcount block. It may also include a new refcount
337 * table if the old refcount table is too small.
338 *
339 * Note that allocating clusters here needs some special care:
340 *
341 * - We can't use the normal qcow2_alloc_clusters(), it would try to
342 * increase the refcount and very likely we would end up with an endless
343 * recursion. Instead we must place the refcount blocks in a way that
344 * they can describe them themselves.
345 *
346 * - We need to consider that at this point we are inside update_refcounts
Kevin Wolfb106ad92014-03-28 18:06:31 +0100347 * and potentially doing an initial refcount increase. This means that
348 * some clusters have already been allocated by the caller, but their
349 * refcount isn't accurate yet. If we allocate clusters for metadata, we
350 * need to return -EAGAIN to signal the caller that it needs to restart
351 * the search for free clusters.
Kevin Wolf92dcb592010-02-23 16:40:53 +0100352 *
353 * - alloc_clusters_noref and qcow2_free_clusters may load a different
354 * refcount block into the cache
355 */
356
Kevin Wolf29c1a732011-01-10 17:17:28 +0100357 *refcount_block = NULL;
358
359 /* We write to the refcount table, so we might depend on L2 tables */
Stefan Hajnoczi99919232013-03-04 15:02:30 +0100360 ret = qcow2_cache_flush(bs, s->l2_table_cache);
361 if (ret < 0) {
362 return ret;
363 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200364
Kevin Wolf92dcb592010-02-23 16:40:53 +0100365 /* Allocate the refcount block itself and mark it as used */
Eric Blake77d6a212018-11-13 17:03:18 -0600366 int64_t new_block = alloc_clusters_noref(bs, s->cluster_size, INT64_MAX);
Kevin Wolf2eaa8f62010-06-04 11:22:39 +0200367 if (new_block < 0) {
368 return new_block;
369 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200370
Alberto Garcia6bf45d52017-11-03 16:18:50 +0200371 /* If we're allocating the block at offset 0 then something is wrong */
372 if (new_block == 0) {
373 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
374 "allocation of refcount block at offset 0");
375 return -EIO;
376 }
377
Kevin Wolf92dcb592010-02-23 16:40:53 +0100378#ifdef DEBUG_ALLOC2
379 fprintf(stderr, "qcow2: Allocate refcount block %d for %" PRIx64
380 " at %" PRIx64 "\n",
381 refcount_table_index, cluster_index << s->cluster_bits, new_block);
382#endif
383
384 if (in_same_refcount_block(s, new_block, cluster_index << s->cluster_bits)) {
Kevin Wolf25408c02010-05-28 12:05:45 +0200385 /* Zero the new refcount block before updating it */
Kevin Wolf29c1a732011-01-10 17:17:28 +0100386 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
Max Reitz7453c962015-02-10 15:28:50 -0500387 refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100388 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200389 goto fail;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100390 }
391
392 memset(*refcount_block, 0, s->cluster_size);
Kevin Wolf25408c02010-05-28 12:05:45 +0200393
Kevin Wolf92dcb592010-02-23 16:40:53 +0100394 /* The block describes itself, need to update the cache */
395 int block_index = (new_block >> s->cluster_bits) &
Max Reitz17bd5f42014-09-03 00:25:07 +0200396 (s->refcount_block_size - 1);
Max Reitz7453c962015-02-10 15:28:50 -0500397 s->set_refcount(*refcount_block, block_index, 1);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200398 } else {
Kevin Wolf92dcb592010-02-23 16:40:53 +0100399 /* Described somewhere else. This can recurse at most twice before we
400 * arrive at a block that describes itself. */
Max Reitz2aabe7c2015-02-10 15:28:47 -0500401 ret = update_refcount(bs, new_block, s->cluster_size, 1, false,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200402 QCOW2_DISCARD_NEVER);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100403 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200404 goto fail;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200405 }
Kevin Wolf25408c02010-05-28 12:05:45 +0200406
Stefan Hajnoczi99919232013-03-04 15:02:30 +0100407 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
408 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200409 goto fail;
Stefan Hajnoczi99919232013-03-04 15:02:30 +0100410 }
Kevin Wolf1c4c2812010-09-17 16:36:58 +0200411
Kevin Wolf25408c02010-05-28 12:05:45 +0200412 /* Initialize the new refcount block only after updating its refcount,
413 * update_refcount uses the refcount cache itself */
Kevin Wolf29c1a732011-01-10 17:17:28 +0100414 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block,
Max Reitz7453c962015-02-10 15:28:50 -0500415 refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100416 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200417 goto fail;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100418 }
419
420 memset(*refcount_block, 0, s->cluster_size);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200421 }
422
Kevin Wolf92dcb592010-02-23 16:40:53 +0100423 /* Now the new refcount block needs to be written to disk */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200424 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
Alberto Garcia2d135ee2018-02-05 16:33:06 +0200425 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100426 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100427 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200428 goto fail;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100429 }
430
431 /* If the refcount table is big enough, just hook the block up there */
432 if (refcount_table_index < s->refcount_table_size) {
433 uint64_t data64 = cpu_to_be64(new_block);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200434 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200435 ret = bdrv_pwrite_sync(bs->file,
Kevin Wolf92dcb592010-02-23 16:40:53 +0100436 s->refcount_table_offset + refcount_table_index * sizeof(uint64_t),
437 &data64, sizeof(data64));
438 if (ret < 0) {
Max Reitz60c48a22017-06-13 22:21:04 +0200439 goto fail;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100440 }
441
442 s->refcount_table[refcount_table_index] = new_block;
Alberto Garcia7061a072017-02-01 14:38:28 +0200443 /* If there's a hole in s->refcount_table then it can happen
444 * that refcount_table_index < s->max_refcount_table_index */
445 s->max_refcount_table_index =
446 MAX(s->max_refcount_table_index, refcount_table_index);
Kevin Wolfb106ad92014-03-28 18:06:31 +0100447
448 /* The new refcount block may be where the caller intended to put its
449 * data, so let it restart the search. */
450 return -EAGAIN;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100451 }
452
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200453 qcow2_cache_put(s->refcount_block_cache, refcount_block);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100454
455 /*
456 * If we come here, we need to grow the refcount table. Again, a new
457 * refcount table needs some space and we can't simply allocate to avoid
458 * endless recursion.
459 *
460 * Therefore let's grab new refcount blocks at the end of the image, which
461 * will describe themselves and the new refcount table. This way we can
462 * reference them only in the new table and do the switch to the new
463 * refcount table at once without producing an inconsistent state in
464 * between.
465 */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200466 BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
Kevin Wolf82522782010-03-15 17:38:05 +0100467
Max Reitz14a58a42015-02-10 15:02:31 -0500468 /* Calculate the number of refcount blocks needed so far; this will be the
469 * basis for calculating the index of the first cluster used for the
470 * self-describing refcount structures which we are about to create.
471 *
472 * Because we reached this point, there cannot be any refcount entries for
473 * cluster_index or higher indices yet. However, because new_block has been
474 * allocated to describe that cluster (and it will assume this role later
475 * on), we cannot use that index; also, new_block may actually have a higher
476 * cluster index than cluster_index, so it needs to be taken into account
477 * here (and 1 needs to be added to its value because that cluster is used).
478 */
479 uint64_t blocks_used = DIV_ROUND_UP(MAX(cluster_index + 1,
480 (new_block >> s->cluster_bits) + 1),
481 s->refcount_block_size);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100482
Kevin Wolf92dcb592010-02-23 16:40:53 +0100483 /* Create the new refcount table and blocks */
Max Reitz17bd5f42014-09-03 00:25:07 +0200484 uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
Kevin Wolf92dcb592010-02-23 16:40:53 +0100485 s->cluster_size;
Kevin Wolfde828152014-05-20 17:12:47 +0200486
Max Reitz12cc30a2017-06-13 22:21:03 +0200487 ret = qcow2_refcount_area(bs, meta_offset, 0, false,
488 refcount_table_index, new_block);
489 if (ret < 0) {
490 return ret;
491 }
492
493 ret = load_refcount_block(bs, new_block, refcount_block);
494 if (ret < 0) {
495 return ret;
496 }
497
498 /* If we were trying to do the initial refcount update for some cluster
499 * allocation, we might have used the same clusters to store newly
500 * allocated metadata. Make the caller search some new space. */
501 return -EAGAIN;
502
Max Reitz60c48a22017-06-13 22:21:04 +0200503fail:
Max Reitz12cc30a2017-06-13 22:21:03 +0200504 if (*refcount_block != NULL) {
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200505 qcow2_cache_put(s->refcount_block_cache, refcount_block);
Max Reitz12cc30a2017-06-13 22:21:03 +0200506 }
507 return ret;
508}
509
510/*
511 * Starting at @start_offset, this function creates new self-covering refcount
512 * structures: A new refcount table and refcount blocks which cover all of
513 * themselves, and a number of @additional_clusters beyond their end.
514 * @start_offset must be at the end of the image file, that is, there must be
515 * only empty space beyond it.
516 * If @exact_size is false, the refcount table will have 50 % more entries than
517 * necessary so it will not need to grow again soon.
518 * If @new_refblock_offset is not zero, it contains the offset of a refcount
519 * block that should be entered into the new refcount table at index
520 * @new_refblock_index.
521 *
522 * Returns: The offset after the new refcount structures (i.e. where the
523 * @additional_clusters may be placed) on success, -errno on error.
524 */
Max Reitz772d1f92017-06-13 22:21:05 +0200525int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t start_offset,
526 uint64_t additional_clusters, bool exact_size,
527 int new_refblock_index,
528 uint64_t new_refblock_offset)
Max Reitz12cc30a2017-06-13 22:21:03 +0200529{
530 BDRVQcow2State *s = bs->opaque;
531 uint64_t total_refblock_count_u64, additional_refblock_count;
532 int total_refblock_count, table_size, area_reftable_index, table_clusters;
533 int i;
534 uint64_t table_offset, block_offset, end_offset;
535 int ret;
536 uint64_t *new_table;
537
538 assert(!(start_offset % s->cluster_size));
539
540 qcow2_refcount_metadata_size(start_offset / s->cluster_size +
541 additional_clusters,
542 s->cluster_size, s->refcount_order,
543 !exact_size, &total_refblock_count_u64);
544 if (total_refblock_count_u64 > QCOW_MAX_REFTABLE_SIZE) {
545 return -EFBIG;
546 }
547 total_refblock_count = total_refblock_count_u64;
548
549 /* Index in the refcount table of the first refcount block to cover the area
550 * of refcount structures we are about to create; we know that
551 * @total_refblock_count can cover @start_offset, so this will definitely
552 * fit into an int. */
553 area_reftable_index = (start_offset / s->cluster_size) /
554 s->refcount_block_size;
555
556 if (exact_size) {
557 table_size = total_refblock_count;
558 } else {
559 table_size = total_refblock_count +
560 DIV_ROUND_UP(total_refblock_count, 2);
561 }
562 /* The qcow2 file can only store the reftable size in number of clusters */
563 table_size = ROUND_UP(table_size, s->cluster_size / sizeof(uint64_t));
564 table_clusters = (table_size * sizeof(uint64_t)) / s->cluster_size;
565
566 if (table_size > QCOW_MAX_REFTABLE_SIZE) {
567 return -EFBIG;
568 }
569
570 new_table = g_try_new0(uint64_t, table_size);
571
572 assert(table_size > 0);
573 if (new_table == NULL) {
Kevin Wolfde828152014-05-20 17:12:47 +0200574 ret = -ENOMEM;
Max Reitz12cc30a2017-06-13 22:21:03 +0200575 goto fail;
Kevin Wolfde828152014-05-20 17:12:47 +0200576 }
Kevin Wolf92dcb592010-02-23 16:40:53 +0100577
Kevin Wolf92dcb592010-02-23 16:40:53 +0100578 /* Fill the new refcount table */
Max Reitz12cc30a2017-06-13 22:21:03 +0200579 if (table_size > s->max_refcount_table_index) {
580 /* We're actually growing the reftable */
581 memcpy(new_table, s->refcount_table,
582 (s->max_refcount_table_index + 1) * sizeof(uint64_t));
583 } else {
584 /* Improbable case: We're shrinking the reftable. However, the caller
585 * has assured us that there is only empty space beyond @start_offset,
586 * so we can simply drop all of the refblocks that won't fit into the
587 * new reftable. */
588 memcpy(new_table, s->refcount_table, table_size * sizeof(uint64_t));
Kevin Wolf92dcb592010-02-23 16:40:53 +0100589 }
590
Max Reitz12cc30a2017-06-13 22:21:03 +0200591 if (new_refblock_offset) {
592 assert(new_refblock_index < total_refblock_count);
593 new_table[new_refblock_index] = new_refblock_offset;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100594 }
595
Max Reitz12cc30a2017-06-13 22:21:03 +0200596 /* Count how many new refblocks we have to create */
597 additional_refblock_count = 0;
598 for (i = area_reftable_index; i < total_refblock_count; i++) {
599 if (!new_table[i]) {
600 additional_refblock_count++;
601 }
602 }
603
604 table_offset = start_offset + additional_refblock_count * s->cluster_size;
605 end_offset = table_offset + table_clusters * s->cluster_size;
606
607 /* Fill the refcount blocks, and create new ones, if necessary */
608 block_offset = start_offset;
609 for (i = area_reftable_index; i < total_refblock_count; i++) {
610 void *refblock_data;
611 uint64_t first_offset_covered;
612
613 /* Reuse an existing refblock if possible, create a new one otherwise */
614 if (new_table[i]) {
615 ret = qcow2_cache_get(bs, s->refcount_block_cache, new_table[i],
616 &refblock_data);
617 if (ret < 0) {
618 goto fail;
619 }
620 } else {
621 ret = qcow2_cache_get_empty(bs, s->refcount_block_cache,
622 block_offset, &refblock_data);
623 if (ret < 0) {
624 goto fail;
625 }
626 memset(refblock_data, 0, s->cluster_size);
Alberto Garcia2d135ee2018-02-05 16:33:06 +0200627 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
Max Reitz12cc30a2017-06-13 22:21:03 +0200628 refblock_data);
629
630 new_table[i] = block_offset;
631 block_offset += s->cluster_size;
632 }
633
634 /* First host offset covered by this refblock */
635 first_offset_covered = (uint64_t)i * s->refcount_block_size *
636 s->cluster_size;
637 if (first_offset_covered < end_offset) {
638 int j, end_index;
639
640 /* Set the refcount of all of the new refcount structures to 1 */
641
642 if (first_offset_covered < start_offset) {
643 assert(i == area_reftable_index);
644 j = (start_offset - first_offset_covered) / s->cluster_size;
645 assert(j < s->refcount_block_size);
646 } else {
647 j = 0;
648 }
649
650 end_index = MIN((end_offset - first_offset_covered) /
651 s->cluster_size,
652 s->refcount_block_size);
653
654 for (; j < end_index; j++) {
655 /* The caller guaranteed us this space would be empty */
656 assert(s->get_refcount(refblock_data, j) == 0);
657 s->set_refcount(refblock_data, j, 1);
658 }
659
Alberto Garcia2d135ee2018-02-05 16:33:06 +0200660 qcow2_cache_entry_mark_dirty(s->refcount_block_cache,
Max Reitz12cc30a2017-06-13 22:21:03 +0200661 refblock_data);
662 }
663
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200664 qcow2_cache_put(s->refcount_block_cache, &refblock_data);
Max Reitz12cc30a2017-06-13 22:21:03 +0200665 }
666
667 assert(block_offset == table_offset);
668
Kevin Wolf92dcb592010-02-23 16:40:53 +0100669 /* Write refcount blocks to disk */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200670 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS);
Max Reitz12cc30a2017-06-13 22:21:03 +0200671 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100672 if (ret < 0) {
Max Reitz12cc30a2017-06-13 22:21:03 +0200673 goto fail;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100674 }
675
676 /* Write refcount table to disk */
Max Reitz12cc30a2017-06-13 22:21:03 +0200677 for (i = 0; i < total_refblock_count; i++) {
Kevin Wolf92dcb592010-02-23 16:40:53 +0100678 cpu_to_be64s(&new_table[i]);
679 }
680
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200681 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200682 ret = bdrv_pwrite_sync(bs->file, table_offset, new_table,
Kevin Wolf92dcb592010-02-23 16:40:53 +0100683 table_size * sizeof(uint64_t));
684 if (ret < 0) {
Max Reitz12cc30a2017-06-13 22:21:03 +0200685 goto fail;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100686 }
687
Max Reitz12cc30a2017-06-13 22:21:03 +0200688 for (i = 0; i < total_refblock_count; i++) {
Zhi Yong Wu87267752012-04-28 15:38:08 +0800689 be64_to_cpus(&new_table[i]);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100690 }
691
692 /* Hook up the new refcount table in the qcow2 header */
John Snow95334232015-11-02 18:32:06 -0500693 struct QEMU_PACKED {
694 uint64_t d64;
695 uint32_t d32;
696 } data;
Peter Maydellf1f7a1d2016-06-16 17:06:17 +0100697 data.d64 = cpu_to_be64(table_offset);
698 data.d32 = cpu_to_be32(table_clusters);
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200699 BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE);
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200700 ret = bdrv_pwrite_sync(bs->file,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200701 offsetof(QCowHeader, refcount_table_offset),
John Snow95334232015-11-02 18:32:06 -0500702 &data, sizeof(data));
Kevin Wolf92dcb592010-02-23 16:40:53 +0100703 if (ret < 0) {
Max Reitz12cc30a2017-06-13 22:21:03 +0200704 goto fail;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100705 }
706
707 /* And switch it in memory */
708 uint64_t old_table_offset = s->refcount_table_offset;
709 uint64_t old_table_size = s->refcount_table_size;
710
Anthony Liguori7267c092011-08-20 22:09:37 -0500711 g_free(s->refcount_table);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100712 s->refcount_table = new_table;
713 s->refcount_table_size = table_size;
714 s->refcount_table_offset = table_offset;
Alberto Garcia7061a072017-02-01 14:38:28 +0200715 update_max_refcount_table_index(s);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100716
Kevin Wolfb106ad92014-03-28 18:06:31 +0100717 /* Free old table. */
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200718 qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
719 QCOW2_DISCARD_OTHER);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100720
Max Reitz12cc30a2017-06-13 22:21:03 +0200721 return end_offset;
Kevin Wolf92dcb592010-02-23 16:40:53 +0100722
Max Reitz12cc30a2017-06-13 22:21:03 +0200723fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500724 g_free(new_table);
Kevin Wolf92dcb592010-02-23 16:40:53 +0100725 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200726}
727
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200728void qcow2_process_discards(BlockDriverState *bs, int ret)
729{
Kevin Wolfff991292015-09-07 17:12:56 +0200730 BDRVQcow2State *s = bs->opaque;
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200731 Qcow2DiscardRegion *d, *next;
732
733 QTAILQ_FOREACH_SAFE(d, &s->discards, next, next) {
734 QTAILQ_REMOVE(&s->discards, d, next);
735
736 /* Discard is optional, ignore the return value */
737 if (ret >= 0) {
Fam Zheng0b9fd3f2018-07-10 14:31:17 +0800738 bdrv_pdiscard(bs->file, d->offset, d->bytes);
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200739 }
740
741 g_free(d);
742 }
743}
744
745static void update_refcount_discard(BlockDriverState *bs,
746 uint64_t offset, uint64_t length)
747{
Kevin Wolfff991292015-09-07 17:12:56 +0200748 BDRVQcow2State *s = bs->opaque;
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200749 Qcow2DiscardRegion *d, *p, *next;
750
751 QTAILQ_FOREACH(d, &s->discards, next) {
752 uint64_t new_start = MIN(offset, d->offset);
753 uint64_t new_end = MAX(offset + length, d->offset + d->bytes);
754
755 if (new_end - new_start <= length + d->bytes) {
756 /* There can't be any overlap, areas ending up here have no
757 * references any more and therefore shouldn't get freed another
758 * time. */
759 assert(d->bytes + length == new_end - new_start);
760 d->offset = new_start;
761 d->bytes = new_end - new_start;
762 goto found;
763 }
764 }
765
766 d = g_malloc(sizeof(*d));
767 *d = (Qcow2DiscardRegion) {
768 .bs = bs,
769 .offset = offset,
770 .bytes = length,
771 };
772 QTAILQ_INSERT_TAIL(&s->discards, d, next);
773
774found:
775 /* Merge discard requests if they are adjacent now */
776 QTAILQ_FOREACH_SAFE(p, &s->discards, next, next) {
777 if (p == d
778 || p->offset > d->offset + d->bytes
779 || d->offset > p->offset + p->bytes)
780 {
781 continue;
782 }
783
784 /* Still no overlap possible */
785 assert(p->offset == d->offset + d->bytes
786 || d->offset == p->offset + p->bytes);
787
788 QTAILQ_REMOVE(&s->discards, p, next);
789 d->offset = MIN(d->offset, p->offset);
790 d->bytes += p->bytes;
Zhang Haoyud8bb71b2014-10-11 16:35:43 +0800791 g_free(p);
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200792 }
793}
794
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200795/* XXX: cache several refcount block clusters ? */
Max Reitz2aabe7c2015-02-10 15:28:47 -0500796/* @addend is the absolute value of the addend; if @decrease is set, @addend
797 * will be subtracted from the current refcount, otherwise it will be added */
Kevin Wolfdb3a9642010-01-20 15:03:06 +0100798static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
Max Reitz2aabe7c2015-02-10 15:28:47 -0500799 int64_t offset,
800 int64_t length,
Max Reitz0e065282015-02-10 15:28:48 -0500801 uint64_t addend,
Max Reitz2aabe7c2015-02-10 15:28:47 -0500802 bool decrease,
803 enum qcow2_discard_type type)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200804{
Kevin Wolfff991292015-09-07 17:12:56 +0200805 BDRVQcow2State *s = bs->opaque;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200806 int64_t start, last, cluster_offset;
Max Reitz7453c962015-02-10 15:28:50 -0500807 void *refcount_block = NULL;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100808 int64_t old_table_index = -1;
Kevin Wolf09508d12010-01-20 15:03:04 +0100809 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200810
811#ifdef DEBUG_ALLOC2
Max Reitz2aabe7c2015-02-10 15:28:47 -0500812 fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64
Max Reitz0e065282015-02-10 15:28:48 -0500813 " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "",
Max Reitz2aabe7c2015-02-10 15:28:47 -0500814 addend);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200815#endif
Kevin Wolf7322afe2010-01-20 15:03:05 +0100816 if (length < 0) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200817 return -EINVAL;
Kevin Wolf7322afe2010-01-20 15:03:05 +0100818 } else if (length == 0) {
819 return 0;
820 }
821
Max Reitz2aabe7c2015-02-10 15:28:47 -0500822 if (decrease) {
Kevin Wolf29c1a732011-01-10 17:17:28 +0100823 qcow2_cache_set_dependency(bs, s->refcount_block_cache,
824 s->l2_table_cache);
825 }
826
Hu Taoac95acd2013-12-05 14:32:34 +0800827 start = start_of_cluster(s, offset);
828 last = start_of_cluster(s, offset + length - 1);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200829 for(cluster_offset = start; cluster_offset <= last;
830 cluster_offset += s->cluster_size)
831 {
Max Reitz2aabe7c2015-02-10 15:28:47 -0500832 int block_index;
Max Reitz0e065282015-02-10 15:28:48 -0500833 uint64_t refcount;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200834 int64_t cluster_index = cluster_offset >> s->cluster_bits;
Max Reitz17bd5f42014-09-03 00:25:07 +0200835 int64_t table_index = cluster_index >> s->refcount_block_bits;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200836
837 /* Load the refcount block and allocate it if needed */
Kevin Wolf29c1a732011-01-10 17:17:28 +0100838 if (table_index != old_table_index) {
839 if (refcount_block) {
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200840 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
Kevin Wolf29c1a732011-01-10 17:17:28 +0100841 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100842 ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
Alberto Garciaabf754f2018-03-21 15:38:52 +0200843 /* If the caller needs to restart the search for free clusters,
844 * try the same ones first to see if they're still free. */
845 if (ret == -EAGAIN) {
846 if (s->free_cluster_index > (start >> s->cluster_bits)) {
847 s->free_cluster_index = (start >> s->cluster_bits);
848 }
849 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100850 if (ret < 0) {
851 goto fail;
852 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200853 }
Kevin Wolf29c1a732011-01-10 17:17:28 +0100854 old_table_index = table_index;
855
Alberto Garcia2d135ee2018-02-05 16:33:06 +0200856 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200857
858 /* we can update the count and save it */
Max Reitz17bd5f42014-09-03 00:25:07 +0200859 block_index = cluster_index & (s->refcount_block_size - 1);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200860
Max Reitz7453c962015-02-10 15:28:50 -0500861 refcount = s->get_refcount(refcount_block, block_index);
Max Reitz0e065282015-02-10 15:28:48 -0500862 if (decrease ? (refcount - addend > refcount)
863 : (refcount + addend < refcount ||
864 refcount + addend > s->refcount_max))
Max Reitz2aabe7c2015-02-10 15:28:47 -0500865 {
Kevin Wolf09508d12010-01-20 15:03:04 +0100866 ret = -EINVAL;
867 goto fail;
868 }
Max Reitz2aabe7c2015-02-10 15:28:47 -0500869 if (decrease) {
870 refcount -= addend;
871 } else {
872 refcount += addend;
873 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200874 if (refcount == 0 && cluster_index < s->free_cluster_index) {
875 s->free_cluster_index = cluster_index;
876 }
Max Reitz7453c962015-02-10 15:28:50 -0500877 s->set_refcount(refcount_block, block_index, refcount);
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200878
Pavel Butsykinf71c08e2017-09-18 15:42:28 +0300879 if (refcount == 0) {
880 void *table;
881
Alberto Garcia6e6fa762018-02-05 16:33:11 +0200882 table = qcow2_cache_is_table_offset(s->refcount_block_cache,
Pavel Butsykinf71c08e2017-09-18 15:42:28 +0300883 offset);
884 if (table != NULL) {
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200885 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
Alberto Garcia77aadd72018-02-05 16:33:10 +0200886 qcow2_cache_discard(s->refcount_block_cache, table);
Pavel Butsykinf71c08e2017-09-18 15:42:28 +0300887 }
888
Alberto Garcia6e6fa762018-02-05 16:33:11 +0200889 table = qcow2_cache_is_table_offset(s->l2_table_cache, offset);
Pavel Butsykinf71c08e2017-09-18 15:42:28 +0300890 if (table != NULL) {
Alberto Garcia77aadd72018-02-05 16:33:10 +0200891 qcow2_cache_discard(s->l2_table_cache, table);
Pavel Butsykinf71c08e2017-09-18 15:42:28 +0300892 }
893
894 if (s->discard_passthrough[type]) {
895 update_refcount_discard(bs, cluster_offset, s->cluster_size);
896 }
Kevin Wolf67af6742013-06-19 13:44:19 +0200897 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200898 }
899
Kevin Wolf09508d12010-01-20 15:03:04 +0100900 ret = 0;
901fail:
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200902 if (!s->cache_discards) {
903 qcow2_process_discards(bs, ret);
904 }
905
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200906 /* Write last changed block to disk */
Kevin Wolf29c1a732011-01-10 17:17:28 +0100907 if (refcount_block) {
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200908 qcow2_cache_put(s->refcount_block_cache, &refcount_block);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200909 }
910
Kevin Wolf09508d12010-01-20 15:03:04 +0100911 /*
912 * Try do undo any updates if an error is returned (This may succeed in
913 * some cases like ENOSPC for allocating a new refcount block)
914 */
915 if (ret < 0) {
916 int dummy;
Max Reitz2aabe7c2015-02-10 15:28:47 -0500917 dummy = update_refcount(bs, offset, cluster_offset - offset, addend,
918 !decrease, QCOW2_DISCARD_NEVER);
Blue Swirl83e3f762010-10-13 18:38:07 +0000919 (void)dummy;
Kevin Wolf09508d12010-01-20 15:03:04 +0100920 }
921
922 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200923}
924
Kevin Wolf018faaf2010-06-04 11:16:11 +0200925/*
Max Reitz44751912014-10-27 11:12:54 +0100926 * Increases or decreases the refcount of a given cluster.
Kevin Wolf018faaf2010-06-04 11:16:11 +0200927 *
Max Reitz2aabe7c2015-02-10 15:28:47 -0500928 * @addend is the absolute value of the addend; if @decrease is set, @addend
929 * will be subtracted from the current refcount, otherwise it will be added.
930 *
Max Reitzc6e9d8a2015-02-10 15:28:45 -0500931 * On success 0 is returned; on failure -errno is returned.
Kevin Wolf018faaf2010-06-04 11:16:11 +0200932 */
Max Reitz32b64442013-09-03 10:09:52 +0200933int qcow2_update_cluster_refcount(BlockDriverState *bs,
934 int64_t cluster_index,
Max Reitz0e065282015-02-10 15:28:48 -0500935 uint64_t addend, bool decrease,
Max Reitz32b64442013-09-03 10:09:52 +0200936 enum qcow2_discard_type type)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200937{
Kevin Wolfff991292015-09-07 17:12:56 +0200938 BDRVQcow2State *s = bs->opaque;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200939 int ret;
940
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200941 ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend,
Max Reitz2aabe7c2015-02-10 15:28:47 -0500942 decrease, type);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200943 if (ret < 0) {
944 return ret;
945 }
946
Max Reitzc6e9d8a2015-02-10 15:28:45 -0500947 return 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200948}
949
950
951
952/*********************************************************/
953/* cluster allocation functions */
954
955
956
957/* return < 0 if error */
Eric Blake77d6a212018-11-13 17:03:18 -0600958static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size,
959 uint64_t max)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200960{
Kevin Wolfff991292015-09-07 17:12:56 +0200961 BDRVQcow2State *s = bs->opaque;
Max Reitz0e065282015-02-10 15:28:48 -0500962 uint64_t i, nb_clusters, refcount;
Max Reitz7324c102015-02-10 15:28:46 -0500963 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200964
Kevin Wolfecbda7a2015-05-06 13:21:51 +0200965 /* We can't allocate clusters if they may still be queued for discard. */
966 if (s->cache_discards) {
967 qcow2_process_discards(bs, 0);
968 }
969
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200970 nb_clusters = size_to_clusters(s, size);
971retry:
972 for(i = 0; i < nb_clusters; i++) {
Kevin Wolfbb572ae2014-03-26 13:05:51 +0100973 uint64_t next_cluster_index = s->free_cluster_index++;
Max Reitz7324c102015-02-10 15:28:46 -0500974 ret = qcow2_get_refcount(bs, next_cluster_index, &refcount);
Kevin Wolf2eaa8f62010-06-04 11:22:39 +0200975
Max Reitz7324c102015-02-10 15:28:46 -0500976 if (ret < 0) {
977 return ret;
Kevin Wolf2eaa8f62010-06-04 11:22:39 +0200978 } else if (refcount != 0) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200979 goto retry;
Kevin Wolf2eaa8f62010-06-04 11:22:39 +0200980 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200981 }
Max Reitz91f827d2014-04-29 19:03:11 +0200982
983 /* Make sure that all offsets in the "allocated" range are representable
Eric Blake77d6a212018-11-13 17:03:18 -0600984 * in the requested max */
Max Reitz65f33bc2014-05-04 05:31:40 +0200985 if (s->free_cluster_index > 0 &&
Eric Blake77d6a212018-11-13 17:03:18 -0600986 s->free_cluster_index - 1 > (max >> s->cluster_bits))
Max Reitz65f33bc2014-05-04 05:31:40 +0200987 {
Max Reitz91f827d2014-04-29 19:03:11 +0200988 return -EFBIG;
989 }
990
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200991#ifdef DEBUG_ALLOC2
Frediano Ziglio35ee5e32011-08-25 09:23:55 +0200992 fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n",
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200993 size,
994 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
995#endif
996 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
997}
998
Kevin Wolfbb572ae2014-03-26 13:05:51 +0100999int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001000{
1001 int64_t offset;
Kevin Wolfdb3a9642010-01-20 15:03:06 +01001002 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001003
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001004 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
Kevin Wolfb106ad92014-03-28 18:06:31 +01001005 do {
Eric Blake77d6a212018-11-13 17:03:18 -06001006 offset = alloc_clusters_noref(bs, size, QCOW_MAX_CLUSTER_OFFSET);
Kevin Wolfb106ad92014-03-28 18:06:31 +01001007 if (offset < 0) {
1008 return offset;
1009 }
Kevin Wolf2eaa8f62010-06-04 11:22:39 +02001010
Max Reitz2aabe7c2015-02-10 15:28:47 -05001011 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
Kevin Wolfb106ad92014-03-28 18:06:31 +01001012 } while (ret == -EAGAIN);
1013
Kevin Wolfdb3a9642010-01-20 15:03:06 +01001014 if (ret < 0) {
1015 return ret;
1016 }
Kevin Wolf1c4c2812010-09-17 16:36:58 +02001017
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001018 return offset;
1019}
1020
Max Reitzb6d36de2015-09-14 16:39:47 +02001021int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
1022 int64_t nb_clusters)
Kevin Wolf256900b2012-03-02 19:35:58 +01001023{
Kevin Wolfff991292015-09-07 17:12:56 +02001024 BDRVQcow2State *s = bs->opaque;
Max Reitz0e065282015-02-10 15:28:48 -05001025 uint64_t cluster_index, refcount;
Hu Tao33304ec2014-01-26 11:12:38 +08001026 uint64_t i;
Max Reitz7324c102015-02-10 15:28:46 -05001027 int ret;
Hu Tao33304ec2014-01-26 11:12:38 +08001028
1029 assert(nb_clusters >= 0);
1030 if (nb_clusters == 0) {
1031 return 0;
1032 }
Kevin Wolf256900b2012-03-02 19:35:58 +01001033
Kevin Wolfb106ad92014-03-28 18:06:31 +01001034 do {
1035 /* Check how many clusters there are free */
1036 cluster_index = offset >> s->cluster_bits;
1037 for(i = 0; i < nb_clusters; i++) {
Max Reitz7324c102015-02-10 15:28:46 -05001038 ret = qcow2_get_refcount(bs, cluster_index++, &refcount);
1039 if (ret < 0) {
1040 return ret;
Kevin Wolfb106ad92014-03-28 18:06:31 +01001041 } else if (refcount != 0) {
1042 break;
1043 }
Kevin Wolf256900b2012-03-02 19:35:58 +01001044 }
Kevin Wolf256900b2012-03-02 19:35:58 +01001045
Kevin Wolfb106ad92014-03-28 18:06:31 +01001046 /* And then allocate them */
Max Reitz2aabe7c2015-02-10 15:28:47 -05001047 ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false,
Kevin Wolfb106ad92014-03-28 18:06:31 +01001048 QCOW2_DISCARD_NEVER);
1049 } while (ret == -EAGAIN);
Kevin Wolff24423b2012-04-20 15:50:39 +02001050
Kevin Wolf256900b2012-03-02 19:35:58 +01001051 if (ret < 0) {
1052 return ret;
1053 }
1054
1055 return i;
1056}
1057
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001058/* only used to allocate compressed sectors. We try to allocate
1059 contiguous sectors. size must be <= cluster_size */
Kevin Wolfed6ccf02009-05-28 16:07:07 +02001060int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001061{
Kevin Wolfff991292015-09-07 17:12:56 +02001062 BDRVQcow2State *s = bs->opaque;
Max Reitz8c44dfb2015-02-06 09:39:16 -05001063 int64_t offset;
1064 size_t free_in_cluster;
1065 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001066
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001067 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001068 assert(size > 0 && size <= s->cluster_size);
Max Reitz8c44dfb2015-02-06 09:39:16 -05001069 assert(!s->free_byte_offset || offset_into_cluster(s, s->free_byte_offset));
1070
1071 offset = s->free_byte_offset;
1072
1073 if (offset) {
Max Reitz0e065282015-02-10 15:28:48 -05001074 uint64_t refcount;
Max Reitz7324c102015-02-10 15:28:46 -05001075 ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount);
1076 if (ret < 0) {
1077 return ret;
Kevin Wolf5d757b52010-01-20 15:04:01 +01001078 }
Max Reitz8c44dfb2015-02-06 09:39:16 -05001079
Max Reitz346a53d2015-02-10 15:28:43 -05001080 if (refcount == s->refcount_max) {
Max Reitz8c44dfb2015-02-06 09:39:16 -05001081 offset = 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001082 }
1083 }
Kevin Wolf29216ed2010-09-17 16:57:48 +02001084
Max Reitz8c44dfb2015-02-06 09:39:16 -05001085 free_in_cluster = s->cluster_size - offset_into_cluster(s, offset);
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001086 do {
1087 if (!offset || free_in_cluster < size) {
Eric Blake77d6a212018-11-13 17:03:18 -06001088 int64_t new_cluster;
1089
1090 new_cluster = alloc_clusters_noref(bs, s->cluster_size,
1091 MIN(s->cluster_offset_mask,
1092 QCOW_MAX_CLUSTER_OFFSET));
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001093 if (new_cluster < 0) {
1094 return new_cluster;
1095 }
1096
Alberto Garcia8aa34832017-11-03 16:18:52 +02001097 if (new_cluster == 0) {
1098 qcow2_signal_corruption(bs, true, -1, -1, "Preventing invalid "
1099 "allocation of compressed cluster "
1100 "at offset 0");
1101 return -EIO;
1102 }
1103
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001104 if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
1105 offset = new_cluster;
Max Reitz2ac01522015-09-11 18:47:51 +02001106 free_in_cluster = s->cluster_size;
1107 } else {
1108 free_in_cluster += s->cluster_size;
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001109 }
Max Reitz8c44dfb2015-02-06 09:39:16 -05001110 }
1111
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001112 assert(offset);
1113 ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
Max Reitz2ac01522015-09-11 18:47:51 +02001114 if (ret < 0) {
1115 offset = 0;
1116 }
Jindřich Makovička3e5feb62015-06-24 07:05:25 +02001117 } while (ret == -EAGAIN);
Max Reitz8c44dfb2015-02-06 09:39:16 -05001118 if (ret < 0) {
1119 return ret;
1120 }
1121
1122 /* The cluster refcount was incremented; refcount blocks must be flushed
1123 * before the caller's L2 table updates. */
Stefan Hajnoczic1f5baf2013-03-04 15:02:32 +01001124 qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
Max Reitz8c44dfb2015-02-06 09:39:16 -05001125
1126 s->free_byte_offset = offset + size;
1127 if (!offset_into_cluster(s, s->free_byte_offset)) {
1128 s->free_byte_offset = 0;
1129 }
1130
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001131 return offset;
1132}
1133
Kevin Wolfed6ccf02009-05-28 16:07:07 +02001134void qcow2_free_clusters(BlockDriverState *bs,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02001135 int64_t offset, int64_t size,
1136 enum qcow2_discard_type type)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001137{
Kevin Wolfdb3a9642010-01-20 15:03:06 +01001138 int ret;
1139
Kevin Wolf66f82ce2010-04-14 14:17:38 +02001140 BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE);
Max Reitz2aabe7c2015-02-10 15:28:47 -05001141 ret = update_refcount(bs, offset, size, 1, true, type);
Kevin Wolfdb3a9642010-01-20 15:03:06 +01001142 if (ret < 0) {
1143 fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret));
Kevin Wolf003fad62010-04-21 11:37:52 +02001144 /* TODO Remember the clusters to free them later and avoid leaking */
Kevin Wolfdb3a9642010-01-20 15:03:06 +01001145 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001146}
1147
Kevin Wolf45aba422009-05-28 16:07:05 +02001148/*
Kevin Wolfc7a4c372012-03-27 13:09:22 +02001149 * Free a cluster using its L2 entry (handles clusters of all types, e.g.
1150 * normal cluster, compressed cluster, etc.)
Kevin Wolf45aba422009-05-28 16:07:05 +02001151 */
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02001152void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
1153 int nb_clusters, enum qcow2_discard_type type)
Kevin Wolf45aba422009-05-28 16:07:05 +02001154{
Kevin Wolfff991292015-09-07 17:12:56 +02001155 BDRVQcow2State *s = bs->opaque;
Kevin Wolf45aba422009-05-28 16:07:05 +02001156
Kevin Wolfc7a4c372012-03-27 13:09:22 +02001157 switch (qcow2_get_cluster_type(l2_entry)) {
1158 case QCOW2_CLUSTER_COMPRESSED:
1159 {
1160 int nb_csectors;
1161 nb_csectors = ((l2_entry >> s->csize_shift) &
1162 s->csize_mask) + 1;
1163 qcow2_free_clusters(bs,
1164 (l2_entry & s->cluster_offset_mask) & ~511,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02001165 nb_csectors * 512, type);
Kevin Wolfc7a4c372012-03-27 13:09:22 +02001166 }
1167 break;
1168 case QCOW2_CLUSTER_NORMAL:
Eric Blakefdfab372017-05-06 19:05:46 -05001169 case QCOW2_CLUSTER_ZERO_ALLOC:
1170 if (offset_into_cluster(s, l2_entry & L2E_OFFSET_MASK)) {
1171 qcow2_signal_corruption(bs, false, -1, -1,
1172 "Cannot free unaligned cluster %#llx",
1173 l2_entry & L2E_OFFSET_MASK);
1174 } else {
1175 qcow2_free_clusters(bs, l2_entry & L2E_OFFSET_MASK,
1176 nb_clusters << s->cluster_bits, type);
Max Reitz8f730dd2013-10-09 10:44:28 +02001177 }
Kevin Wolfc7a4c372012-03-27 13:09:22 +02001178 break;
Eric Blakefdfab372017-05-06 19:05:46 -05001179 case QCOW2_CLUSTER_ZERO_PLAIN:
Kevin Wolfc7a4c372012-03-27 13:09:22 +02001180 case QCOW2_CLUSTER_UNALLOCATED:
1181 break;
1182 default:
1183 abort();
Kevin Wolf45aba422009-05-28 16:07:05 +02001184 }
Kevin Wolf45aba422009-05-28 16:07:05 +02001185}
1186
Paolo Bonzini8b220eb2018-03-01 17:36:14 +01001187int coroutine_fn qcow2_write_caches(BlockDriverState *bs)
1188{
1189 BDRVQcow2State *s = bs->opaque;
1190 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001191
Paolo Bonzini8b220eb2018-03-01 17:36:14 +01001192 ret = qcow2_cache_write(bs, s->l2_table_cache);
1193 if (ret < 0) {
1194 return ret;
1195 }
1196
1197 if (qcow2_need_accurate_refcounts(s)) {
1198 ret = qcow2_cache_write(bs, s->refcount_block_cache);
1199 if (ret < 0) {
1200 return ret;
1201 }
1202 }
1203
1204 return 0;
1205}
1206
1207int coroutine_fn qcow2_flush_caches(BlockDriverState *bs)
1208{
1209 int ret = qcow2_write_caches(bs);
1210 if (ret < 0) {
1211 return ret;
1212 }
1213
1214 return bdrv_flush(bs->file->bs);
1215}
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001216
1217/*********************************************************/
1218/* snapshots and image creation */
1219
1220
1221
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001222/* update the refcounts of snapshots and the copied flag */
Kevin Wolfed6ccf02009-05-28 16:07:07 +02001223int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1224 int64_t l1_table_offset, int l1_size, int addend)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001225{
Kevin Wolfff991292015-09-07 17:12:56 +02001226 BDRVQcow2State *s = bs->opaque;
Alberto Garcia83ad1652018-02-05 16:33:27 +02001227 uint64_t *l1_table, *l2_slice, l2_offset, entry, l1_size2, refcount;
Kevin Wolfde828152014-05-20 17:12:47 +02001228 bool l1_allocated = false;
Eric Blakeb32cbae2017-05-06 19:05:41 -05001229 int64_t old_entry, old_l2_offset;
Alberto Garcia83ad1652018-02-05 16:33:27 +02001230 unsigned slice, slice_size2, n_slices;
Max Reitz7324c102015-02-10 15:28:46 -05001231 int i, j, l1_modified = 0, nb_csectors;
Kevin Wolf29c1a732011-01-10 17:17:28 +01001232 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001233
Max Reitz2aabe7c2015-02-10 15:28:47 -05001234 assert(addend >= -1 && addend <= 1);
1235
Alberto Garcia83ad1652018-02-05 16:33:27 +02001236 l2_slice = NULL;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001237 l1_table = NULL;
1238 l1_size2 = l1_size * sizeof(uint64_t);
Alberto Garcia83ad1652018-02-05 16:33:27 +02001239 slice_size2 = s->l2_slice_size * sizeof(uint64_t);
1240 n_slices = s->cluster_size / slice_size2;
Kevin Wolf43a0cac2011-11-16 15:20:45 +01001241
Kevin Wolf0b919fa2013-06-19 13:44:20 +02001242 s->cache_discards = true;
1243
Kevin Wolf43a0cac2011-11-16 15:20:45 +01001244 /* WARNING: qcow2_snapshot_goto relies on this function not using the
1245 * l1_table_offset when it is the current s->l1_table_offset! Be careful
1246 * when changing this! */
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001247 if (l1_table_offset != s->l1_table_offset) {
Alberto Garcia9e029682018-02-15 15:10:08 +02001248 l1_table = g_try_malloc0(ROUND_UP(l1_size2, 512));
Kevin Wolfde828152014-05-20 17:12:47 +02001249 if (l1_size2 && l1_table == NULL) {
1250 ret = -ENOMEM;
1251 goto fail;
1252 }
1253 l1_allocated = true;
Kevin Wolfc2bc78b2013-04-05 12:51:31 +02001254
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001255 ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);
Kevin Wolfc2bc78b2013-04-05 12:51:31 +02001256 if (ret < 0) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001257 goto fail;
Kevin Wolf93913df2011-07-19 13:01:48 +02001258 }
1259
Eric Blakeb32cbae2017-05-06 19:05:41 -05001260 for (i = 0; i < l1_size; i++) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001261 be64_to_cpus(&l1_table[i]);
Eric Blakeb32cbae2017-05-06 19:05:41 -05001262 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001263 } else {
1264 assert(l1_size == s->l1_size);
1265 l1_table = s->l1_table;
Kevin Wolfde828152014-05-20 17:12:47 +02001266 l1_allocated = false;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001267 }
1268
Eric Blakeb32cbae2017-05-06 19:05:41 -05001269 for (i = 0; i < l1_size; i++) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001270 l2_offset = l1_table[i];
1271 if (l2_offset) {
1272 old_l2_offset = l2_offset;
Kevin Wolf8e37f682012-02-23 15:40:55 +01001273 l2_offset &= L1E_OFFSET_MASK;
Kevin Wolf29c1a732011-01-10 17:17:28 +01001274
Max Reitza97c67e2014-09-05 16:07:18 +02001275 if (offset_into_cluster(s, l2_offset)) {
1276 qcow2_signal_corruption(bs, true, -1, -1, "L2 table offset %#"
1277 PRIx64 " unaligned (L1 index: %#x)",
1278 l2_offset, i);
1279 ret = -EIO;
1280 goto fail;
1281 }
1282
Alberto Garcia83ad1652018-02-05 16:33:27 +02001283 for (slice = 0; slice < n_slices; slice++) {
Alberto Garciaca62dd52018-02-05 16:33:26 +02001284 ret = qcow2_cache_get(bs, s->l2_table_cache,
Alberto Garcia83ad1652018-02-05 16:33:27 +02001285 l2_offset + slice * slice_size2,
1286 (void **) &l2_slice);
Alberto Garciaca62dd52018-02-05 16:33:26 +02001287 if (ret < 0) {
1288 goto fail;
1289 }
Kevin Wolf29c1a732011-01-10 17:17:28 +01001290
Alberto Garcia83ad1652018-02-05 16:33:27 +02001291 for (j = 0; j < s->l2_slice_size; j++) {
Alberto Garciaca62dd52018-02-05 16:33:26 +02001292 uint64_t cluster_index;
1293 uint64_t offset;
Max Reitz8b81a7b2013-08-30 10:40:14 +02001294
Alberto Garcia83ad1652018-02-05 16:33:27 +02001295 entry = be64_to_cpu(l2_slice[j]);
Alberto Garciaca62dd52018-02-05 16:33:26 +02001296 old_entry = entry;
1297 entry &= ~QCOW_OFLAG_COPIED;
1298 offset = entry & L2E_OFFSET_MASK;
Max Reitz8b81a7b2013-08-30 10:40:14 +02001299
Alberto Garciaca62dd52018-02-05 16:33:26 +02001300 switch (qcow2_get_cluster_type(entry)) {
1301 case QCOW2_CLUSTER_COMPRESSED:
1302 nb_csectors = ((entry >> s->csize_shift) &
1303 s->csize_mask) + 1;
1304 if (addend != 0) {
1305 ret = update_refcount(
1306 bs, (entry & s->cluster_offset_mask) & ~511,
Max Reitz2aabe7c2015-02-10 15:28:47 -05001307 nb_csectors * 512, abs(addend), addend < 0,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02001308 QCOW2_DISCARD_SNAPSHOT);
Alberto Garciaca62dd52018-02-05 16:33:26 +02001309 if (ret < 0) {
1310 goto fail;
1311 }
1312 }
1313 /* compressed clusters are never modified */
1314 refcount = 2;
1315 break;
1316
1317 case QCOW2_CLUSTER_NORMAL:
1318 case QCOW2_CLUSTER_ZERO_ALLOC:
1319 if (offset_into_cluster(s, offset)) {
Alberto Garcia83ad1652018-02-05 16:33:27 +02001320 /* Here l2_index means table (not slice) index */
1321 int l2_index = slice * s->l2_slice_size + j;
Alberto Garciaca62dd52018-02-05 16:33:26 +02001322 qcow2_signal_corruption(
1323 bs, true, -1, -1, "Cluster "
1324 "allocation offset %#" PRIx64
1325 " unaligned (L2 offset: %#"
1326 PRIx64 ", L2 index: %#x)",
Alberto Garcia83ad1652018-02-05 16:33:27 +02001327 offset, l2_offset, l2_index);
Alberto Garciaca62dd52018-02-05 16:33:26 +02001328 ret = -EIO;
1329 goto fail;
1330 }
1331
1332 cluster_index = offset >> s->cluster_bits;
1333 assert(cluster_index);
1334 if (addend != 0) {
1335 ret = qcow2_update_cluster_refcount(
1336 bs, cluster_index, abs(addend), addend < 0,
1337 QCOW2_DISCARD_SNAPSHOT);
1338 if (ret < 0) {
1339 goto fail;
1340 }
1341 }
1342
1343 ret = qcow2_get_refcount(bs, cluster_index, &refcount);
Max Reitz7324c102015-02-10 15:28:46 -05001344 if (ret < 0) {
Kevin Wolf018faaf2010-06-04 11:16:11 +02001345 goto fail;
1346 }
Alberto Garciaca62dd52018-02-05 16:33:26 +02001347 break;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001348
Alberto Garciaca62dd52018-02-05 16:33:26 +02001349 case QCOW2_CLUSTER_ZERO_PLAIN:
1350 case QCOW2_CLUSTER_UNALLOCATED:
1351 refcount = 0;
1352 break;
1353
1354 default:
1355 abort();
Eric Blakebbd995d2017-05-06 19:05:42 -05001356 }
1357
Alberto Garciaca62dd52018-02-05 16:33:26 +02001358 if (refcount == 1) {
1359 entry |= QCOW_OFLAG_COPIED;
1360 }
1361 if (entry != old_entry) {
1362 if (addend > 0) {
1363 qcow2_cache_set_dependency(bs, s->l2_table_cache,
1364 s->refcount_block_cache);
Eric Blakebbd995d2017-05-06 19:05:42 -05001365 }
Alberto Garcia83ad1652018-02-05 16:33:27 +02001366 l2_slice[j] = cpu_to_be64(entry);
Alberto Garciaca62dd52018-02-05 16:33:26 +02001367 qcow2_cache_entry_mark_dirty(s->l2_table_cache,
Alberto Garcia83ad1652018-02-05 16:33:27 +02001368 l2_slice);
Eric Blakebbd995d2017-05-06 19:05:42 -05001369 }
Max Reitz8b81a7b2013-08-30 10:40:14 +02001370 }
1371
Alberto Garcia83ad1652018-02-05 16:33:27 +02001372 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001373 }
Kevin Wolf29c1a732011-01-10 17:17:28 +01001374
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001375 if (addend != 0) {
Max Reitzc6e9d8a2015-02-10 15:28:45 -05001376 ret = qcow2_update_cluster_refcount(bs, l2_offset >>
1377 s->cluster_bits,
Max Reitz2aabe7c2015-02-10 15:28:47 -05001378 abs(addend), addend < 0,
Max Reitzc6e9d8a2015-02-10 15:28:45 -05001379 QCOW2_DISCARD_SNAPSHOT);
1380 if (ret < 0) {
1381 goto fail;
1382 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001383 }
Max Reitz7324c102015-02-10 15:28:46 -05001384 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1385 &refcount);
1386 if (ret < 0) {
Kevin Wolf018faaf2010-06-04 11:16:11 +02001387 goto fail;
1388 } else if (refcount == 1) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001389 l2_offset |= QCOW_OFLAG_COPIED;
1390 }
1391 if (l2_offset != old_l2_offset) {
1392 l1_table[i] = l2_offset;
1393 l1_modified = 1;
1394 }
1395 }
1396 }
Kevin Wolf93913df2011-07-19 13:01:48 +02001397
Stefan Hajnoczi2154f242013-03-04 15:02:33 +01001398 ret = bdrv_flush(bs);
Kevin Wolf93913df2011-07-19 13:01:48 +02001399fail:
Alberto Garcia83ad1652018-02-05 16:33:27 +02001400 if (l2_slice) {
1401 qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
Kevin Wolf93913df2011-07-19 13:01:48 +02001402 }
1403
Kevin Wolf0b919fa2013-06-19 13:44:20 +02001404 s->cache_discards = false;
1405 qcow2_process_discards(bs, ret);
1406
Kevin Wolf43a0cac2011-11-16 15:20:45 +01001407 /* Update L1 only if it isn't deleted anyway (addend = -1) */
Kevin Wolfc2b6ff52013-04-05 12:57:10 +02001408 if (ret == 0 && addend >= 0 && l1_modified) {
1409 for (i = 0; i < l1_size; i++) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001410 cpu_to_be64s(&l1_table[i]);
Kevin Wolfc2b6ff52013-04-05 12:57:10 +02001411 }
1412
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001413 ret = bdrv_pwrite_sync(bs->file, l1_table_offset,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001414 l1_table, l1_size2);
Kevin Wolfc2b6ff52013-04-05 12:57:10 +02001415
1416 for (i = 0; i < l1_size; i++) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001417 be64_to_cpus(&l1_table[i]);
Kevin Wolfc2b6ff52013-04-05 12:57:10 +02001418 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001419 }
1420 if (l1_allocated)
Anthony Liguori7267c092011-08-20 22:09:37 -05001421 g_free(l1_table);
Kevin Wolf93913df2011-07-19 13:01:48 +02001422 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001423}
1424
1425
1426
1427
1428/*********************************************************/
1429/* refcount checking functions */
1430
1431
Kevin Wolfc2551b42015-12-01 15:16:49 +01001432static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries)
Max Reitz5fee1922015-02-10 15:28:49 -05001433{
1434 /* This assertion holds because there is no way we can address more than
1435 * 2^(64 - 9) clusters at once (with cluster size 512 = 2^9, and because
1436 * offsets have to be representable in bytes); due to every cluster
1437 * corresponding to one refcount entry, we are well below that limit */
1438 assert(entries < (UINT64_C(1) << (64 - 9)));
1439
1440 /* Thanks to the assertion this will not overflow, because
1441 * s->refcount_order < 7.
1442 * (note: x << s->refcount_order == x * s->refcount_bits) */
1443 return DIV_ROUND_UP(entries << s->refcount_order, 8);
1444}
1445
1446/**
1447 * Reallocates *array so that it can hold new_size entries. *size must contain
1448 * the current number of entries in *array. If the reallocation fails, *array
1449 * and *size will not be modified and -errno will be returned. If the
1450 * reallocation is successful, *array will be set to the new buffer, *size
1451 * will be set to new_size and 0 will be returned. The size of the reallocated
1452 * refcount array buffer will be aligned to a cluster boundary, and the newly
1453 * allocated area will be zeroed.
1454 */
Kevin Wolfff991292015-09-07 17:12:56 +02001455static int realloc_refcount_array(BDRVQcow2State *s, void **array,
Max Reitz5fee1922015-02-10 15:28:49 -05001456 int64_t *size, int64_t new_size)
1457{
Max Reitzb6d36de2015-09-14 16:39:47 +02001458 int64_t old_byte_size, new_byte_size;
Max Reitz7453c962015-02-10 15:28:50 -05001459 void *new_ptr;
Max Reitz5fee1922015-02-10 15:28:49 -05001460
1461 /* Round to clusters so the array can be directly written to disk */
1462 old_byte_size = size_to_clusters(s, refcount_array_byte_size(s, *size))
1463 * s->cluster_size;
1464 new_byte_size = size_to_clusters(s, refcount_array_byte_size(s, new_size))
1465 * s->cluster_size;
1466
1467 if (new_byte_size == old_byte_size) {
1468 *size = new_size;
1469 return 0;
1470 }
1471
1472 assert(new_byte_size > 0);
1473
Max Reitzb6d36de2015-09-14 16:39:47 +02001474 if (new_byte_size > SIZE_MAX) {
1475 return -ENOMEM;
1476 }
1477
Max Reitz5fee1922015-02-10 15:28:49 -05001478 new_ptr = g_try_realloc(*array, new_byte_size);
1479 if (!new_ptr) {
1480 return -ENOMEM;
1481 }
1482
1483 if (new_byte_size > old_byte_size) {
Max Reitzb6d36de2015-09-14 16:39:47 +02001484 memset((char *)new_ptr + old_byte_size, 0,
Max Reitz5fee1922015-02-10 15:28:49 -05001485 new_byte_size - old_byte_size);
1486 }
1487
1488 *array = new_ptr;
1489 *size = new_size;
1490
1491 return 0;
1492}
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001493
1494/*
1495 * Increases the refcount for a range of clusters in a given refcount table.
1496 * This is used to construct a temporary refcount table out of L1 and L2 tables
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +01001497 * which can be compared to the refcount table saved in the image.
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001498 *
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001499 * Modifies the number of errors in res.
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001500 */
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001501int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
1502 void **refcount_table,
1503 int64_t *refcount_table_size,
1504 int64_t offset, int64_t size)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001505{
Kevin Wolfff991292015-09-07 17:12:56 +02001506 BDRVQcow2State *s = bs->opaque;
Max Reitz7453c962015-02-10 15:28:50 -05001507 uint64_t start, last, cluster_offset, k, refcount;
Max Reitz5fee1922015-02-10 15:28:49 -05001508 int ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001509
Max Reitzfef4d3d2014-10-22 14:09:35 +02001510 if (size <= 0) {
1511 return 0;
1512 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001513
Hu Taoac95acd2013-12-05 14:32:34 +08001514 start = start_of_cluster(s, offset);
1515 last = start_of_cluster(s, offset + size - 1);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001516 for(cluster_offset = start; cluster_offset <= last;
1517 cluster_offset += s->cluster_size) {
1518 k = cluster_offset >> s->cluster_bits;
Max Reitz641bb632014-10-22 14:09:36 +02001519 if (k >= *refcount_table_size) {
Max Reitz5fee1922015-02-10 15:28:49 -05001520 ret = realloc_refcount_array(s, refcount_table,
1521 refcount_table_size, k + 1);
1522 if (ret < 0) {
Max Reitz641bb632014-10-22 14:09:36 +02001523 res->check_errors++;
Max Reitz5fee1922015-02-10 15:28:49 -05001524 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001525 }
Max Reitz641bb632014-10-22 14:09:36 +02001526 }
1527
Max Reitz7453c962015-02-10 15:28:50 -05001528 refcount = s->get_refcount(*refcount_table, k);
1529 if (refcount == s->refcount_max) {
Max Reitz641bb632014-10-22 14:09:36 +02001530 fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
1531 "\n", cluster_offset);
Max Reitz03bb78e2015-07-27 17:51:39 +02001532 fprintf(stderr, "Use qemu-img amend to increase the refcount entry "
1533 "width or qemu-img convert to create a clean copy if the "
1534 "image cannot be opened for writing\n");
Max Reitz641bb632014-10-22 14:09:36 +02001535 res->corruptions++;
Max Reitz7453c962015-02-10 15:28:50 -05001536 continue;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001537 }
Max Reitz7453c962015-02-10 15:28:50 -05001538 s->set_refcount(*refcount_table, k, refcount + 1);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001539 }
Max Reitzfef4d3d2014-10-22 14:09:35 +02001540
1541 return 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001542}
1543
Stefan Hajnoczi801f7042013-02-07 17:15:01 +01001544/* Flags for check_refcounts_l1() and check_refcounts_l2() */
1545enum {
Stefan Hajnoczifba31ba2013-02-07 17:15:02 +01001546 CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */
Stefan Hajnoczi801f7042013-02-07 17:15:01 +01001547};
1548
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001549/*
1550 * Increases the refcount in the given refcount table for the all clusters
1551 * referenced in the L2 table. While doing so, performs some checks on L2
1552 * entries.
1553 *
1554 * Returns the number of errors found by the checks or -errno if an internal
1555 * error occurred.
1556 */
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001557static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
Max Reitz7453c962015-02-10 15:28:50 -05001558 void **refcount_table,
1559 int64_t *refcount_table_size, int64_t l2_offset,
Max Reitzac5b7872017-11-10 21:37:59 +01001560 int flags, BdrvCheckMode fix)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001561{
Kevin Wolfff991292015-09-07 17:12:56 +02001562 BDRVQcow2State *s = bs->opaque;
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001563 uint64_t *l2_table, l2_entry;
Stefan Hajnoczifba31ba2013-02-07 17:15:02 +01001564 uint64_t next_contiguous_offset = 0;
Max Reitzad273902014-10-22 14:09:34 +02001565 int i, l2_size, nb_csectors, ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001566
1567 /* Read L2 table from disk */
1568 l2_size = s->l2_size * sizeof(uint64_t);
Anthony Liguori7267c092011-08-20 22:09:37 -05001569 l2_table = g_malloc(l2_size);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001570
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001571 ret = bdrv_pread(bs->file, l2_offset, l2_table, l2_size);
Max Reitzad273902014-10-22 14:09:34 +02001572 if (ret < 0) {
1573 fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n");
1574 res->check_errors++;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001575 goto fail;
Max Reitzad273902014-10-22 14:09:34 +02001576 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001577
1578 /* Do the actual checks */
1579 for(i = 0; i < s->l2_size; i++) {
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001580 l2_entry = be64_to_cpu(l2_table[i]);
1581
1582 switch (qcow2_get_cluster_type(l2_entry)) {
1583 case QCOW2_CLUSTER_COMPRESSED:
1584 /* Compressed clusters don't have QCOW_OFLAG_COPIED */
1585 if (l2_entry & QCOW_OFLAG_COPIED) {
Alberto Garcia74c44a52018-04-10 18:05:03 +02001586 fprintf(stderr, "ERROR: coffset=0x%" PRIx64 ": "
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001587 "copied flag must never be set for compressed "
Alberto Garcia74c44a52018-04-10 18:05:03 +02001588 "clusters\n", l2_entry & s->cluster_offset_mask);
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001589 l2_entry &= ~QCOW_OFLAG_COPIED;
1590 res->corruptions++;
1591 }
1592
1593 /* Mark cluster as used */
1594 nb_csectors = ((l2_entry >> s->csize_shift) &
1595 s->csize_mask) + 1;
1596 l2_entry &= s->cluster_offset_mask;
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001597 ret = qcow2_inc_refcounts_imrt(bs, res,
1598 refcount_table, refcount_table_size,
1599 l2_entry & ~511, nb_csectors * 512);
Max Reitzfef4d3d2014-10-22 14:09:35 +02001600 if (ret < 0) {
1601 goto fail;
1602 }
Stefan Hajnoczifba31ba2013-02-07 17:15:02 +01001603
1604 if (flags & CHECK_FRAG_INFO) {
1605 res->bfi.allocated_clusters++;
Stefan Hajnoczi4db35162013-02-07 17:15:05 +01001606 res->bfi.compressed_clusters++;
Stefan Hajnoczifba31ba2013-02-07 17:15:02 +01001607
1608 /* Compressed clusters are fragmented by nature. Since they
1609 * take up sub-sector space but we only have sector granularity
1610 * I/O we need to re-read the same sectors even for adjacent
1611 * compressed clusters.
1612 */
1613 res->bfi.fragmented_clusters++;
1614 }
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001615 break;
1616
Eric Blakefdfab372017-05-06 19:05:46 -05001617 case QCOW2_CLUSTER_ZERO_ALLOC:
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001618 case QCOW2_CLUSTER_NORMAL:
1619 {
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001620 uint64_t offset = l2_entry & L2E_OFFSET_MASK;
1621
Stefan Hajnoczifba31ba2013-02-07 17:15:02 +01001622 if (flags & CHECK_FRAG_INFO) {
1623 res->bfi.allocated_clusters++;
1624 if (next_contiguous_offset &&
1625 offset != next_contiguous_offset) {
1626 res->bfi.fragmented_clusters++;
1627 }
1628 next_contiguous_offset = offset + s->cluster_size;
1629 }
1630
Max Reitzac5b7872017-11-10 21:37:59 +01001631 /* Correct offsets are cluster aligned */
1632 if (offset_into_cluster(s, offset)) {
1633 if (qcow2_get_cluster_type(l2_entry) ==
1634 QCOW2_CLUSTER_ZERO_ALLOC)
1635 {
1636 fprintf(stderr, "%s offset=%" PRIx64 ": Preallocated zero "
1637 "cluster is not properly aligned; L2 entry "
1638 "corrupted.\n",
1639 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR",
1640 offset);
1641 if (fix & BDRV_FIX_ERRORS) {
1642 uint64_t l2e_offset =
1643 l2_offset + (uint64_t)i * sizeof(uint64_t);
1644
1645 l2_entry = QCOW_OFLAG_ZERO;
1646 l2_table[i] = cpu_to_be64(l2_entry);
1647 ret = qcow2_pre_write_overlap_check(bs,
1648 QCOW2_OL_ACTIVE_L2 | QCOW2_OL_INACTIVE_L2,
1649 l2e_offset, sizeof(uint64_t));
1650 if (ret < 0) {
1651 fprintf(stderr, "ERROR: Overlap check failed\n");
1652 res->check_errors++;
1653 /* Something is seriously wrong, so abort checking
1654 * this L2 table */
1655 goto fail;
1656 }
1657
1658 ret = bdrv_pwrite_sync(bs->file, l2e_offset,
1659 &l2_table[i], sizeof(uint64_t));
1660 if (ret < 0) {
1661 fprintf(stderr, "ERROR: Failed to overwrite L2 "
1662 "table entry: %s\n", strerror(-ret));
1663 res->check_errors++;
1664 /* Do not abort, continue checking the rest of this
1665 * L2 table's entries */
1666 } else {
1667 res->corruptions_fixed++;
1668 /* Skip marking the cluster as used
1669 * (it is unused now) */
1670 continue;
1671 }
1672 } else {
1673 res->corruptions++;
1674 }
1675 } else {
1676 fprintf(stderr, "ERROR offset=%" PRIx64 ": Data cluster is "
1677 "not properly aligned; L2 entry corrupted.\n", offset);
1678 res->corruptions++;
1679 }
1680 }
1681
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001682 /* Mark cluster as used */
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001683 ret = qcow2_inc_refcounts_imrt(bs, res,
1684 refcount_table, refcount_table_size,
1685 offset, s->cluster_size);
Max Reitzfef4d3d2014-10-22 14:09:35 +02001686 if (ret < 0) {
1687 goto fail;
1688 }
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001689 break;
1690 }
1691
Eric Blakefdfab372017-05-06 19:05:46 -05001692 case QCOW2_CLUSTER_ZERO_PLAIN:
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001693 case QCOW2_CLUSTER_UNALLOCATED:
1694 break;
1695
1696 default:
1697 abort();
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001698 }
1699 }
1700
Anthony Liguori7267c092011-08-20 22:09:37 -05001701 g_free(l2_table);
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001702 return 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001703
1704fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05001705 g_free(l2_table);
Max Reitzad273902014-10-22 14:09:34 +02001706 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001707}
1708
1709/*
1710 * Increases the refcount for the L1 table, its L2 tables and all referenced
1711 * clusters in the given refcount table. While doing so, performs some checks
1712 * on L1 and L2 entries.
1713 *
1714 * Returns the number of errors found by the checks or -errno if an internal
1715 * error occurred.
1716 */
1717static int check_refcounts_l1(BlockDriverState *bs,
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001718 BdrvCheckResult *res,
Max Reitz7453c962015-02-10 15:28:50 -05001719 void **refcount_table,
Max Reitz641bb632014-10-22 14:09:36 +02001720 int64_t *refcount_table_size,
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001721 int64_t l1_table_offset, int l1_size,
Max Reitzac5b7872017-11-10 21:37:59 +01001722 int flags, BdrvCheckMode fix)
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001723{
Kevin Wolfff991292015-09-07 17:12:56 +02001724 BDRVQcow2State *s = bs->opaque;
Max Reitzfef4d3d2014-10-22 14:09:35 +02001725 uint64_t *l1_table = NULL, l2_offset, l1_size2;
Max Reitz4f6ed882013-08-30 14:34:27 +02001726 int i, ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001727
1728 l1_size2 = l1_size * sizeof(uint64_t);
1729
1730 /* Mark L1 table as used */
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001731 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size,
1732 l1_table_offset, l1_size2);
Max Reitzfef4d3d2014-10-22 14:09:35 +02001733 if (ret < 0) {
1734 goto fail;
1735 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001736
1737 /* Read L1 table entries from disk */
Max Reitzfef4d3d2014-10-22 14:09:35 +02001738 if (l1_size2 > 0) {
Kevin Wolfde828152014-05-20 17:12:47 +02001739 l1_table = g_try_malloc(l1_size2);
1740 if (l1_table == NULL) {
1741 ret = -ENOMEM;
Max Reitzad273902014-10-22 14:09:34 +02001742 res->check_errors++;
Kevin Wolfde828152014-05-20 17:12:47 +02001743 goto fail;
1744 }
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001745 ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2);
Max Reitzad273902014-10-22 14:09:34 +02001746 if (ret < 0) {
1747 fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n");
1748 res->check_errors++;
Kevin Wolf702ef632009-11-27 17:35:55 +01001749 goto fail;
Max Reitzad273902014-10-22 14:09:34 +02001750 }
Kevin Wolf702ef632009-11-27 17:35:55 +01001751 for(i = 0;i < l1_size; i++)
1752 be64_to_cpus(&l1_table[i]);
1753 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001754
1755 /* Do the actual checks */
1756 for(i = 0; i < l1_size; i++) {
1757 l2_offset = l1_table[i];
1758 if (l2_offset) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001759 /* Mark L2 table as used */
Kevin Wolfafdf0ab2012-03-27 13:44:56 +02001760 l2_offset &= L1E_OFFSET_MASK;
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001761 ret = qcow2_inc_refcounts_imrt(bs, res,
1762 refcount_table, refcount_table_size,
1763 l2_offset, s->cluster_size);
Max Reitzfef4d3d2014-10-22 14:09:35 +02001764 if (ret < 0) {
1765 goto fail;
1766 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001767
1768 /* L2 tables are cluster aligned */
Hu Taoac95acd2013-12-05 14:32:34 +08001769 if (offset_into_cluster(s, l2_offset)) {
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001770 fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not "
1771 "cluster aligned; L1 entry corrupted\n", l2_offset);
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001772 res->corruptions++;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001773 }
1774
1775 /* Process and check L2 entries */
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001776 ret = check_refcounts_l2(bs, res, refcount_table,
Max Reitzac5b7872017-11-10 21:37:59 +01001777 refcount_table_size, l2_offset, flags,
1778 fix);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001779 if (ret < 0) {
1780 goto fail;
1781 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001782 }
1783 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001784 g_free(l1_table);
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001785 return 0;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001786
1787fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05001788 g_free(l1_table);
Max Reitzad273902014-10-22 14:09:34 +02001789 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001790}
1791
1792/*
Max Reitz4f6ed882013-08-30 14:34:27 +02001793 * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
1794 *
1795 * This function does not print an error message nor does it increment
Max Reitz44751912014-10-27 11:12:54 +01001796 * check_errors if qcow2_get_refcount fails (this is because such an error will
1797 * have been already detected and sufficiently signaled by the calling function
Max Reitz4f6ed882013-08-30 14:34:27 +02001798 * (qcow2_check_refcounts) by the time this function is called).
1799 */
Max Reitze23e4002013-08-30 14:34:28 +02001800static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
1801 BdrvCheckMode fix)
Max Reitz4f6ed882013-08-30 14:34:27 +02001802{
Kevin Wolfff991292015-09-07 17:12:56 +02001803 BDRVQcow2State *s = bs->opaque;
Max Reitz4f6ed882013-08-30 14:34:27 +02001804 uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
1805 int ret;
Max Reitz0e065282015-02-10 15:28:48 -05001806 uint64_t refcount;
Max Reitz4f6ed882013-08-30 14:34:27 +02001807 int i, j;
Max Reitz3cce51c2018-05-09 22:00:58 +02001808 bool repair;
1809
1810 if (fix & BDRV_FIX_ERRORS) {
1811 /* Always repair */
1812 repair = true;
1813 } else if (fix & BDRV_FIX_LEAKS) {
1814 /* Repair only if that seems safe: This function is always
1815 * called after the refcounts have been fixed, so the refcount
1816 * is accurate if that repair was successful */
1817 repair = !res->check_errors && !res->corruptions && !res->leaks;
1818 } else {
1819 repair = false;
1820 }
Max Reitz4f6ed882013-08-30 14:34:27 +02001821
1822 for (i = 0; i < s->l1_size; i++) {
1823 uint64_t l1_entry = s->l1_table[i];
1824 uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
Max Reitze23e4002013-08-30 14:34:28 +02001825 bool l2_dirty = false;
Max Reitz4f6ed882013-08-30 14:34:27 +02001826
1827 if (!l2_offset) {
1828 continue;
1829 }
1830
Max Reitz7324c102015-02-10 15:28:46 -05001831 ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits,
1832 &refcount);
1833 if (ret < 0) {
Max Reitz4f6ed882013-08-30 14:34:27 +02001834 /* don't print message nor increment check_errors */
1835 continue;
1836 }
1837 if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
Max Reitze23e4002013-08-30 14:34:28 +02001838 fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
Max Reitz0e065282015-02-10 15:28:48 -05001839 "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
Max Reitz3cce51c2018-05-09 22:00:58 +02001840 repair ? "Repairing" : "ERROR", i, l1_entry, refcount);
1841 if (repair) {
Max Reitze23e4002013-08-30 14:34:28 +02001842 s->l1_table[i] = refcount == 1
1843 ? l1_entry | QCOW_OFLAG_COPIED
1844 : l1_entry & ~QCOW_OFLAG_COPIED;
1845 ret = qcow2_write_l1_entry(bs, i);
1846 if (ret < 0) {
1847 res->check_errors++;
1848 goto fail;
1849 }
1850 res->corruptions_fixed++;
1851 } else {
1852 res->corruptions++;
1853 }
Max Reitz4f6ed882013-08-30 14:34:27 +02001854 }
1855
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02001856 ret = bdrv_pread(bs->file, l2_offset, l2_table,
Max Reitz4f6ed882013-08-30 14:34:27 +02001857 s->l2_size * sizeof(uint64_t));
1858 if (ret < 0) {
1859 fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
1860 strerror(-ret));
1861 res->check_errors++;
1862 goto fail;
1863 }
1864
1865 for (j = 0; j < s->l2_size; j++) {
1866 uint64_t l2_entry = be64_to_cpu(l2_table[j]);
1867 uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
Eric Blake3ef95212017-05-06 19:05:45 -05001868 QCow2ClusterType cluster_type = qcow2_get_cluster_type(l2_entry);
Max Reitz4f6ed882013-08-30 14:34:27 +02001869
Eric Blakefdfab372017-05-06 19:05:46 -05001870 if (cluster_type == QCOW2_CLUSTER_NORMAL ||
1871 cluster_type == QCOW2_CLUSTER_ZERO_ALLOC) {
Max Reitz7324c102015-02-10 15:28:46 -05001872 ret = qcow2_get_refcount(bs,
1873 data_offset >> s->cluster_bits,
1874 &refcount);
1875 if (ret < 0) {
Max Reitz4f6ed882013-08-30 14:34:27 +02001876 /* don't print message nor increment check_errors */
1877 continue;
1878 }
1879 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
Max Reitze23e4002013-08-30 14:34:28 +02001880 fprintf(stderr, "%s OFLAG_COPIED data cluster: "
Max Reitz0e065282015-02-10 15:28:48 -05001881 "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
Max Reitz3cce51c2018-05-09 22:00:58 +02001882 repair ? "Repairing" : "ERROR", l2_entry, refcount);
1883 if (repair) {
Max Reitze23e4002013-08-30 14:34:28 +02001884 l2_table[j] = cpu_to_be64(refcount == 1
1885 ? l2_entry | QCOW_OFLAG_COPIED
1886 : l2_entry & ~QCOW_OFLAG_COPIED);
1887 l2_dirty = true;
1888 res->corruptions_fixed++;
1889 } else {
1890 res->corruptions++;
1891 }
Max Reitz4f6ed882013-08-30 14:34:27 +02001892 }
1893 }
1894 }
Max Reitze23e4002013-08-30 14:34:28 +02001895
1896 if (l2_dirty) {
Max Reitz231bb262013-10-10 11:09:23 +02001897 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
1898 l2_offset, s->cluster_size);
Max Reitze23e4002013-08-30 14:34:28 +02001899 if (ret < 0) {
1900 fprintf(stderr, "ERROR: Could not write L2 table; metadata "
1901 "overlap check failed: %s\n", strerror(-ret));
1902 res->check_errors++;
1903 goto fail;
1904 }
1905
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02001906 ret = bdrv_pwrite(bs->file, l2_offset, l2_table,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001907 s->cluster_size);
Max Reitze23e4002013-08-30 14:34:28 +02001908 if (ret < 0) {
1909 fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
1910 strerror(-ret));
1911 res->check_errors++;
1912 goto fail;
1913 }
1914 }
Max Reitz4f6ed882013-08-30 14:34:27 +02001915 }
1916
1917 ret = 0;
1918
1919fail:
1920 qemu_vfree(l2_table);
1921 return ret;
1922}
1923
1924/*
Max Reitz6ca56bf2014-10-22 14:09:30 +02001925 * Checks consistency of refblocks and accounts for each refblock in
1926 * *refcount_table.
1927 */
1928static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
Max Reitzf307b252014-10-22 14:09:39 +02001929 BdrvCheckMode fix, bool *rebuild,
Max Reitz7453c962015-02-10 15:28:50 -05001930 void **refcount_table, int64_t *nb_clusters)
Max Reitz6ca56bf2014-10-22 14:09:30 +02001931{
Kevin Wolfff991292015-09-07 17:12:56 +02001932 BDRVQcow2State *s = bs->opaque;
Max Reitz001c1582014-10-22 14:09:38 +02001933 int64_t i, size;
Max Reitzfef4d3d2014-10-22 14:09:35 +02001934 int ret;
Max Reitz6ca56bf2014-10-22 14:09:30 +02001935
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001936 for(i = 0; i < s->refcount_table_size; i++) {
Kevin Wolf6882c8f2010-06-22 12:31:45 +02001937 uint64_t offset, cluster;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001938 offset = s->refcount_table[i];
Kevin Wolf6882c8f2010-06-22 12:31:45 +02001939 cluster = offset >> s->cluster_bits;
Kevin Wolf746c3cb2010-02-23 16:40:54 +01001940
1941 /* Refcount blocks are cluster aligned */
Hu Taoac95acd2013-12-05 14:32:34 +08001942 if (offset_into_cluster(s, offset)) {
Kevin Wolf166acf52012-05-11 18:18:36 +02001943 fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
Kevin Wolf746c3cb2010-02-23 16:40:54 +01001944 "cluster aligned; refcount table entry corrupted\n", i);
Kevin Wolf9ac228e2010-06-29 12:37:54 +02001945 res->corruptions++;
Max Reitzf307b252014-10-22 14:09:39 +02001946 *rebuild = true;
Kevin Wolf6882c8f2010-06-22 12:31:45 +02001947 continue;
1948 }
1949
Max Reitz6ca56bf2014-10-22 14:09:30 +02001950 if (cluster >= *nb_clusters) {
Max Reitz001c1582014-10-22 14:09:38 +02001951 fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
1952 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
1953
1954 if (fix & BDRV_FIX_ERRORS) {
Max Reitz5fee1922015-02-10 15:28:49 -05001955 int64_t new_nb_clusters;
Max Reitzed3d2ec2017-03-28 22:51:27 +02001956 Error *local_err = NULL;
Max Reitz001c1582014-10-22 14:09:38 +02001957
1958 if (offset > INT64_MAX - s->cluster_size) {
1959 ret = -EINVAL;
1960 goto resize_fail;
1961 }
1962
Max Reitzed3d2ec2017-03-28 22:51:27 +02001963 ret = bdrv_truncate(bs->file, offset + s->cluster_size,
Max Reitz7ea37c32017-06-13 22:20:53 +02001964 PREALLOC_MODE_OFF, &local_err);
Max Reitz001c1582014-10-22 14:09:38 +02001965 if (ret < 0) {
Max Reitzed3d2ec2017-03-28 22:51:27 +02001966 error_report_err(local_err);
Max Reitz001c1582014-10-22 14:09:38 +02001967 goto resize_fail;
1968 }
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02001969 size = bdrv_getlength(bs->file->bs);
Max Reitz001c1582014-10-22 14:09:38 +02001970 if (size < 0) {
1971 ret = size;
1972 goto resize_fail;
1973 }
1974
Max Reitz5fee1922015-02-10 15:28:49 -05001975 new_nb_clusters = size_to_clusters(s, size);
1976 assert(new_nb_clusters >= *nb_clusters);
Max Reitz001c1582014-10-22 14:09:38 +02001977
Max Reitz5fee1922015-02-10 15:28:49 -05001978 ret = realloc_refcount_array(s, refcount_table,
1979 nb_clusters, new_nb_clusters);
1980 if (ret < 0) {
Max Reitz001c1582014-10-22 14:09:38 +02001981 res->check_errors++;
Max Reitz5fee1922015-02-10 15:28:49 -05001982 return ret;
Max Reitz001c1582014-10-22 14:09:38 +02001983 }
Max Reitz001c1582014-10-22 14:09:38 +02001984
1985 if (cluster >= *nb_clusters) {
1986 ret = -EINVAL;
1987 goto resize_fail;
1988 }
1989
1990 res->corruptions_fixed++;
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001991 ret = qcow2_inc_refcounts_imrt(bs, res,
1992 refcount_table, nb_clusters,
1993 offset, s->cluster_size);
Max Reitz001c1582014-10-22 14:09:38 +02001994 if (ret < 0) {
1995 return ret;
1996 }
1997 /* No need to check whether the refcount is now greater than 1:
1998 * This area was just allocated and zeroed, so it can only be
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03001999 * exactly 1 after qcow2_inc_refcounts_imrt() */
Max Reitz001c1582014-10-22 14:09:38 +02002000 continue;
2001
2002resize_fail:
2003 res->corruptions++;
Max Reitzf307b252014-10-22 14:09:39 +02002004 *rebuild = true;
Max Reitz001c1582014-10-22 14:09:38 +02002005 fprintf(stderr, "ERROR could not resize image: %s\n",
2006 strerror(-ret));
2007 } else {
2008 res->corruptions++;
2009 }
Kevin Wolf6882c8f2010-06-22 12:31:45 +02002010 continue;
Kevin Wolf746c3cb2010-02-23 16:40:54 +01002011 }
2012
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002013 if (offset != 0) {
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03002014 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2015 offset, s->cluster_size);
Max Reitzfef4d3d2014-10-22 14:09:35 +02002016 if (ret < 0) {
2017 return ret;
2018 }
Max Reitz7453c962015-02-10 15:28:50 -05002019 if (s->get_refcount(*refcount_table, cluster) != 1) {
Max Reitzf307b252014-10-22 14:09:39 +02002020 fprintf(stderr, "ERROR refcount block %" PRId64
Max Reitz7453c962015-02-10 15:28:50 -05002021 " refcount=%" PRIu64 "\n", i,
2022 s->get_refcount(*refcount_table, cluster));
Max Reitzf307b252014-10-22 14:09:39 +02002023 res->corruptions++;
2024 *rebuild = true;
Kevin Wolf746c3cb2010-02-23 16:40:54 +01002025 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002026 }
2027 }
2028
Max Reitz6ca56bf2014-10-22 14:09:30 +02002029 return 0;
2030}
2031
2032/*
Max Reitz057a3fe2014-10-22 14:09:32 +02002033 * Calculates an in-memory refcount table.
2034 */
2035static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
Max Reitzf307b252014-10-22 14:09:39 +02002036 BdrvCheckMode fix, bool *rebuild,
Max Reitz7453c962015-02-10 15:28:50 -05002037 void **refcount_table, int64_t *nb_clusters)
Max Reitz057a3fe2014-10-22 14:09:32 +02002038{
Kevin Wolfff991292015-09-07 17:12:56 +02002039 BDRVQcow2State *s = bs->opaque;
Max Reitz057a3fe2014-10-22 14:09:32 +02002040 int64_t i;
2041 QCowSnapshot *sn;
2042 int ret;
2043
Max Reitz9696df22014-10-22 14:09:37 +02002044 if (!*refcount_table) {
Max Reitz5fee1922015-02-10 15:28:49 -05002045 int64_t old_size = 0;
2046 ret = realloc_refcount_array(s, refcount_table,
2047 &old_size, *nb_clusters);
2048 if (ret < 0) {
Max Reitz9696df22014-10-22 14:09:37 +02002049 res->check_errors++;
Max Reitz5fee1922015-02-10 15:28:49 -05002050 return ret;
Max Reitz9696df22014-10-22 14:09:37 +02002051 }
Max Reitz057a3fe2014-10-22 14:09:32 +02002052 }
2053
2054 /* header */
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03002055 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2056 0, s->cluster_size);
Max Reitzfef4d3d2014-10-22 14:09:35 +02002057 if (ret < 0) {
2058 return ret;
2059 }
Max Reitz057a3fe2014-10-22 14:09:32 +02002060
2061 /* current L1 table */
Max Reitz641bb632014-10-22 14:09:36 +02002062 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
Max Reitzac5b7872017-11-10 21:37:59 +01002063 s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO,
2064 fix);
Max Reitz057a3fe2014-10-22 14:09:32 +02002065 if (ret < 0) {
2066 return ret;
2067 }
2068
2069 /* snapshots */
2070 for (i = 0; i < s->nb_snapshots; i++) {
2071 sn = s->snapshots + i;
Alberto Garcia0c2ada82018-03-06 18:14:12 +02002072 if (offset_into_cluster(s, sn->l1_table_offset)) {
2073 fprintf(stderr, "ERROR snapshot %s (%s) l1_offset=%#" PRIx64 ": "
2074 "L1 table is not cluster aligned; snapshot table entry "
2075 "corrupted\n", sn->id_str, sn->name, sn->l1_table_offset);
2076 res->corruptions++;
2077 continue;
2078 }
2079 if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
2080 fprintf(stderr, "ERROR snapshot %s (%s) l1_size=%#" PRIx32 ": "
2081 "L1 table is too large; snapshot table entry corrupted\n",
2082 sn->id_str, sn->name, sn->l1_size);
2083 res->corruptions++;
2084 continue;
2085 }
Max Reitz641bb632014-10-22 14:09:36 +02002086 ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
Max Reitzac5b7872017-11-10 21:37:59 +01002087 sn->l1_table_offset, sn->l1_size, 0, fix);
Max Reitz057a3fe2014-10-22 14:09:32 +02002088 if (ret < 0) {
2089 return ret;
2090 }
2091 }
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03002092 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2093 s->snapshots_offset, s->snapshots_size);
Max Reitzfef4d3d2014-10-22 14:09:35 +02002094 if (ret < 0) {
2095 return ret;
2096 }
Max Reitz057a3fe2014-10-22 14:09:32 +02002097
2098 /* refcount data */
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03002099 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2100 s->refcount_table_offset,
2101 s->refcount_table_size * sizeof(uint64_t));
Max Reitzfef4d3d2014-10-22 14:09:35 +02002102 if (ret < 0) {
2103 return ret;
2104 }
Max Reitz057a3fe2014-10-22 14:09:32 +02002105
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +01002106 /* encryption */
2107 if (s->crypto_header.length) {
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +03002108 ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters,
2109 s->crypto_header.offset,
2110 s->crypto_header.length);
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +01002111 if (ret < 0) {
2112 return ret;
2113 }
2114 }
2115
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +03002116 /* bitmaps */
2117 ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clusters);
2118 if (ret < 0) {
2119 return ret;
2120 }
2121
Max Reitzf307b252014-10-22 14:09:39 +02002122 return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters);
Max Reitz057a3fe2014-10-22 14:09:32 +02002123}
2124
2125/*
Max Reitz6ca56bf2014-10-22 14:09:30 +02002126 * Compares the actual reference count for each cluster in the image against the
2127 * refcount as reported by the refcount structures on-disk.
2128 */
2129static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
Max Reitzf307b252014-10-22 14:09:39 +02002130 BdrvCheckMode fix, bool *rebuild,
2131 int64_t *highest_cluster,
Max Reitz7453c962015-02-10 15:28:50 -05002132 void *refcount_table, int64_t nb_clusters)
Max Reitz6ca56bf2014-10-22 14:09:30 +02002133{
Kevin Wolfff991292015-09-07 17:12:56 +02002134 BDRVQcow2State *s = bs->opaque;
Max Reitz6ca56bf2014-10-22 14:09:30 +02002135 int64_t i;
Max Reitz0e065282015-02-10 15:28:48 -05002136 uint64_t refcount1, refcount2;
Max Reitz7324c102015-02-10 15:28:46 -05002137 int ret;
Max Reitz6ca56bf2014-10-22 14:09:30 +02002138
2139 for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) {
Max Reitz7324c102015-02-10 15:28:46 -05002140 ret = qcow2_get_refcount(bs, i, &refcount1);
2141 if (ret < 0) {
Kevin Wolf166acf52012-05-11 18:18:36 +02002142 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
Max Reitz7324c102015-02-10 15:28:46 -05002143 i, strerror(-ret));
Kevin Wolf9ac228e2010-06-29 12:37:54 +02002144 res->check_errors++;
Kevin Wolff74550f2010-06-22 12:35:07 +02002145 continue;
Kevin Wolf018faaf2010-06-04 11:16:11 +02002146 }
2147
Max Reitz7453c962015-02-10 15:28:50 -05002148 refcount2 = s->get_refcount(refcount_table, i);
Federico Simoncellic6bb9ad2013-01-28 06:59:46 -05002149
2150 if (refcount1 > 0 || refcount2 > 0) {
Max Reitz6ca56bf2014-10-22 14:09:30 +02002151 *highest_cluster = i;
Federico Simoncellic6bb9ad2013-01-28 06:59:46 -05002152 }
2153
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002154 if (refcount1 != refcount2) {
Kevin Wolf166acf52012-05-11 18:18:36 +02002155 /* Check if we're allowed to fix the mismatch */
2156 int *num_fixed = NULL;
Max Reitzf307b252014-10-22 14:09:39 +02002157 if (refcount1 == 0) {
2158 *rebuild = true;
2159 } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
Kevin Wolf166acf52012-05-11 18:18:36 +02002160 num_fixed = &res->leaks_fixed;
2161 } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
2162 num_fixed = &res->corruptions_fixed;
2163 }
2164
Max Reitz0e065282015-02-10 15:28:48 -05002165 fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64
2166 " reference=%" PRIu64 "\n",
Kevin Wolf166acf52012-05-11 18:18:36 +02002167 num_fixed != NULL ? "Repairing" :
2168 refcount1 < refcount2 ? "ERROR" :
2169 "Leaked",
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002170 i, refcount1, refcount2);
Kevin Wolf166acf52012-05-11 18:18:36 +02002171
2172 if (num_fixed) {
2173 ret = update_refcount(bs, i << s->cluster_bits, 1,
Max Reitz2aabe7c2015-02-10 15:28:47 -05002174 refcount_diff(refcount1, refcount2),
2175 refcount1 > refcount2,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +02002176 QCOW2_DISCARD_ALWAYS);
Kevin Wolf166acf52012-05-11 18:18:36 +02002177 if (ret >= 0) {
2178 (*num_fixed)++;
2179 continue;
2180 }
2181 }
2182
2183 /* And if we couldn't, print an error */
Kevin Wolf9ac228e2010-06-29 12:37:54 +02002184 if (refcount1 < refcount2) {
2185 res->corruptions++;
2186 } else {
2187 res->leaks++;
2188 }
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002189 }
2190 }
Max Reitz6ca56bf2014-10-22 14:09:30 +02002191}
2192
2193/*
Max Reitzc7c06812014-10-22 14:09:40 +02002194 * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
2195 * the on-disk refcount structures.
2196 *
2197 * On input, *first_free_cluster tells where to start looking, and need not
2198 * actually be a free cluster; the returned offset will not be before that
2199 * cluster. On output, *first_free_cluster points to the first gap found, even
2200 * if that gap was too small to be used as the returned offset.
2201 *
2202 * Note that *first_free_cluster is a cluster index whereas the return value is
2203 * an offset.
2204 */
2205static int64_t alloc_clusters_imrt(BlockDriverState *bs,
2206 int cluster_count,
Max Reitz7453c962015-02-10 15:28:50 -05002207 void **refcount_table,
Max Reitzc7c06812014-10-22 14:09:40 +02002208 int64_t *imrt_nb_clusters,
2209 int64_t *first_free_cluster)
2210{
Kevin Wolfff991292015-09-07 17:12:56 +02002211 BDRVQcow2State *s = bs->opaque;
Max Reitzc7c06812014-10-22 14:09:40 +02002212 int64_t cluster = *first_free_cluster, i;
2213 bool first_gap = true;
2214 int contiguous_free_clusters;
Max Reitz5fee1922015-02-10 15:28:49 -05002215 int ret;
Max Reitzc7c06812014-10-22 14:09:40 +02002216
2217 /* Starting at *first_free_cluster, find a range of at least cluster_count
2218 * continuously free clusters */
2219 for (contiguous_free_clusters = 0;
2220 cluster < *imrt_nb_clusters &&
2221 contiguous_free_clusters < cluster_count;
2222 cluster++)
2223 {
Max Reitz7453c962015-02-10 15:28:50 -05002224 if (!s->get_refcount(*refcount_table, cluster)) {
Max Reitzc7c06812014-10-22 14:09:40 +02002225 contiguous_free_clusters++;
2226 if (first_gap) {
2227 /* If this is the first free cluster found, update
2228 * *first_free_cluster accordingly */
2229 *first_free_cluster = cluster;
2230 first_gap = false;
2231 }
2232 } else if (contiguous_free_clusters) {
2233 contiguous_free_clusters = 0;
2234 }
2235 }
2236
2237 /* If contiguous_free_clusters is greater than zero, it contains the number
2238 * of continuously free clusters until the current cluster; the first free
2239 * cluster in the current "gap" is therefore
2240 * cluster - contiguous_free_clusters */
2241
2242 /* If no such range could be found, grow the in-memory refcount table
2243 * accordingly to append free clusters at the end of the image */
2244 if (contiguous_free_clusters < cluster_count) {
Max Reitzc7c06812014-10-22 14:09:40 +02002245 /* contiguous_free_clusters clusters are already empty at the image end;
2246 * we need cluster_count clusters; therefore, we have to allocate
2247 * cluster_count - contiguous_free_clusters new clusters at the end of
2248 * the image (which is the current value of cluster; note that cluster
2249 * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
2250 * the image end) */
Max Reitz5fee1922015-02-10 15:28:49 -05002251 ret = realloc_refcount_array(s, refcount_table, imrt_nb_clusters,
2252 cluster + cluster_count
2253 - contiguous_free_clusters);
2254 if (ret < 0) {
2255 return ret;
Max Reitzc7c06812014-10-22 14:09:40 +02002256 }
Max Reitzc7c06812014-10-22 14:09:40 +02002257 }
2258
2259 /* Go back to the first free cluster */
2260 cluster -= contiguous_free_clusters;
2261 for (i = 0; i < cluster_count; i++) {
Max Reitz7453c962015-02-10 15:28:50 -05002262 s->set_refcount(*refcount_table, cluster + i, 1);
Max Reitzc7c06812014-10-22 14:09:40 +02002263 }
2264
2265 return cluster << s->cluster_bits;
2266}
2267
2268/*
2269 * Creates a new refcount structure based solely on the in-memory information
2270 * given through *refcount_table. All necessary allocations will be reflected
2271 * in that array.
2272 *
2273 * On success, the old refcount structure is leaked (it will be covered by the
2274 * new refcount structure).
2275 */
2276static int rebuild_refcount_structure(BlockDriverState *bs,
2277 BdrvCheckResult *res,
Max Reitz7453c962015-02-10 15:28:50 -05002278 void **refcount_table,
Max Reitzc7c06812014-10-22 14:09:40 +02002279 int64_t *nb_clusters)
2280{
Kevin Wolfff991292015-09-07 17:12:56 +02002281 BDRVQcow2State *s = bs->opaque;
Max Reitzc7c06812014-10-22 14:09:40 +02002282 int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
2283 int64_t refblock_offset, refblock_start, refblock_index;
2284 uint32_t reftable_size = 0;
2285 uint64_t *on_disk_reftable = NULL;
Max Reitz7453c962015-02-10 15:28:50 -05002286 void *on_disk_refblock;
2287 int ret = 0;
Max Reitzc7c06812014-10-22 14:09:40 +02002288 struct {
2289 uint64_t reftable_offset;
2290 uint32_t reftable_clusters;
2291 } QEMU_PACKED reftable_offset_and_clusters;
2292
2293 qcow2_cache_empty(bs, s->refcount_block_cache);
2294
2295write_refblocks:
2296 for (; cluster < *nb_clusters; cluster++) {
Max Reitz7453c962015-02-10 15:28:50 -05002297 if (!s->get_refcount(*refcount_table, cluster)) {
Max Reitzc7c06812014-10-22 14:09:40 +02002298 continue;
2299 }
2300
2301 refblock_index = cluster >> s->refcount_block_bits;
2302 refblock_start = refblock_index << s->refcount_block_bits;
2303
2304 /* Don't allocate a cluster in a refblock already written to disk */
2305 if (first_free_cluster < refblock_start) {
2306 first_free_cluster = refblock_start;
2307 }
2308 refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
2309 nb_clusters, &first_free_cluster);
2310 if (refblock_offset < 0) {
2311 fprintf(stderr, "ERROR allocating refblock: %s\n",
2312 strerror(-refblock_offset));
2313 res->check_errors++;
2314 ret = refblock_offset;
2315 goto fail;
2316 }
2317
2318 if (reftable_size <= refblock_index) {
2319 uint32_t old_reftable_size = reftable_size;
2320 uint64_t *new_on_disk_reftable;
2321
2322 reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t),
2323 s->cluster_size) / sizeof(uint64_t);
2324 new_on_disk_reftable = g_try_realloc(on_disk_reftable,
2325 reftable_size *
2326 sizeof(uint64_t));
2327 if (!new_on_disk_reftable) {
2328 res->check_errors++;
2329 ret = -ENOMEM;
2330 goto fail;
2331 }
2332 on_disk_reftable = new_on_disk_reftable;
2333
2334 memset(on_disk_reftable + old_reftable_size, 0,
2335 (reftable_size - old_reftable_size) * sizeof(uint64_t));
2336
2337 /* The offset we have for the reftable is now no longer valid;
2338 * this will leak that range, but we can easily fix that by running
2339 * a leak-fixing check after this rebuild operation */
2340 reftable_offset = -1;
Philippe Mathieu-Daudéf80ac752017-07-26 23:42:10 -03002341 } else {
2342 assert(on_disk_reftable);
Max Reitzc7c06812014-10-22 14:09:40 +02002343 }
2344 on_disk_reftable[refblock_index] = refblock_offset;
2345
2346 /* If this is apparently the last refblock (for now), try to squeeze the
2347 * reftable in */
2348 if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
2349 reftable_offset < 0)
2350 {
2351 uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
2352 sizeof(uint64_t));
2353 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2354 refcount_table, nb_clusters,
2355 &first_free_cluster);
2356 if (reftable_offset < 0) {
2357 fprintf(stderr, "ERROR allocating reftable: %s\n",
2358 strerror(-reftable_offset));
2359 res->check_errors++;
2360 ret = reftable_offset;
2361 goto fail;
2362 }
2363 }
2364
2365 ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
2366 s->cluster_size);
2367 if (ret < 0) {
2368 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2369 goto fail;
2370 }
2371
Max Reitz7453c962015-02-10 15:28:50 -05002372 /* The size of *refcount_table is always cluster-aligned, therefore the
2373 * write operation will not overflow */
2374 on_disk_refblock = (void *)((char *) *refcount_table +
2375 refblock_index * s->cluster_size);
Max Reitzc7c06812014-10-22 14:09:40 +02002376
Kevin Wolf18d51c42016-05-31 14:42:08 +02002377 ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE,
Max Reitz7453c962015-02-10 15:28:50 -05002378 on_disk_refblock, s->cluster_sectors);
Max Reitzc7c06812014-10-22 14:09:40 +02002379 if (ret < 0) {
2380 fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
2381 goto fail;
2382 }
2383
2384 /* Go to the end of this refblock */
2385 cluster = refblock_start + s->refcount_block_size - 1;
2386 }
2387
2388 if (reftable_offset < 0) {
2389 uint64_t post_refblock_start, reftable_clusters;
2390
2391 post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
2392 reftable_clusters = size_to_clusters(s,
2393 reftable_size * sizeof(uint64_t));
2394 /* Not pretty but simple */
2395 if (first_free_cluster < post_refblock_start) {
2396 first_free_cluster = post_refblock_start;
2397 }
2398 reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
2399 refcount_table, nb_clusters,
2400 &first_free_cluster);
2401 if (reftable_offset < 0) {
2402 fprintf(stderr, "ERROR allocating reftable: %s\n",
2403 strerror(-reftable_offset));
2404 res->check_errors++;
2405 ret = reftable_offset;
2406 goto fail;
2407 }
2408
2409 goto write_refblocks;
2410 }
2411
Max Reitzc7c06812014-10-22 14:09:40 +02002412 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2413 cpu_to_be64s(&on_disk_reftable[refblock_index]);
2414 }
2415
2416 ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
2417 reftable_size * sizeof(uint64_t));
2418 if (ret < 0) {
2419 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2420 goto fail;
2421 }
2422
2423 assert(reftable_size < INT_MAX / sizeof(uint64_t));
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02002424 ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable,
Max Reitzc7c06812014-10-22 14:09:40 +02002425 reftable_size * sizeof(uint64_t));
2426 if (ret < 0) {
2427 fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
2428 goto fail;
2429 }
2430
2431 /* Enter new reftable into the image header */
Peter Maydellf1f7a1d2016-06-16 17:06:17 +01002432 reftable_offset_and_clusters.reftable_offset = cpu_to_be64(reftable_offset);
2433 reftable_offset_and_clusters.reftable_clusters =
2434 cpu_to_be32(size_to_clusters(s, reftable_size * sizeof(uint64_t)));
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02002435 ret = bdrv_pwrite_sync(bs->file,
2436 offsetof(QCowHeader, refcount_table_offset),
Max Reitzc7c06812014-10-22 14:09:40 +02002437 &reftable_offset_and_clusters,
2438 sizeof(reftable_offset_and_clusters));
2439 if (ret < 0) {
2440 fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
2441 goto fail;
2442 }
2443
2444 for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
2445 be64_to_cpus(&on_disk_reftable[refblock_index]);
2446 }
2447 s->refcount_table = on_disk_reftable;
2448 s->refcount_table_offset = reftable_offset;
2449 s->refcount_table_size = reftable_size;
Alberto Garcia7061a072017-02-01 14:38:28 +02002450 update_max_refcount_table_index(s);
Max Reitzc7c06812014-10-22 14:09:40 +02002451
2452 return 0;
2453
2454fail:
2455 g_free(on_disk_reftable);
2456 return ret;
2457}
2458
2459/*
Max Reitz6ca56bf2014-10-22 14:09:30 +02002460 * Checks an image for refcount consistency.
2461 *
2462 * Returns 0 if no errors are found, the number of errors in case the image is
2463 * detected as corrupted, and -errno when an internal error occurred.
2464 */
2465int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
2466 BdrvCheckMode fix)
2467{
Kevin Wolfff991292015-09-07 17:12:56 +02002468 BDRVQcow2State *s = bs->opaque;
Max Reitzc7c06812014-10-22 14:09:40 +02002469 BdrvCheckResult pre_compare_res;
Max Reitz6ca56bf2014-10-22 14:09:30 +02002470 int64_t size, highest_cluster, nb_clusters;
Max Reitz7453c962015-02-10 15:28:50 -05002471 void *refcount_table = NULL;
Max Reitzf307b252014-10-22 14:09:39 +02002472 bool rebuild = false;
Max Reitz6ca56bf2014-10-22 14:09:30 +02002473 int ret;
2474
Kevin Wolf9a4f4c32015-06-16 14:19:22 +02002475 size = bdrv_getlength(bs->file->bs);
Max Reitz6ca56bf2014-10-22 14:09:30 +02002476 if (size < 0) {
2477 res->check_errors++;
2478 return size;
2479 }
2480
2481 nb_clusters = size_to_clusters(s, size);
2482 if (nb_clusters > INT_MAX) {
2483 res->check_errors++;
2484 return -EFBIG;
2485 }
2486
2487 res->bfi.total_clusters =
2488 size_to_clusters(s, bs->total_sectors * BDRV_SECTOR_SIZE);
2489
Max Reitzf307b252014-10-22 14:09:39 +02002490 ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table,
2491 &nb_clusters);
Max Reitz6ca56bf2014-10-22 14:09:30 +02002492 if (ret < 0) {
2493 goto fail;
2494 }
2495
Max Reitzc7c06812014-10-22 14:09:40 +02002496 /* In case we don't need to rebuild the refcount structure (but want to fix
2497 * something), this function is immediately called again, in which case the
2498 * result should be ignored */
2499 pre_compare_res = *res;
2500 compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
Max Reitz6ca56bf2014-10-22 14:09:30 +02002501 nb_clusters);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002502
Max Reitzc7c06812014-10-22 14:09:40 +02002503 if (rebuild && (fix & BDRV_FIX_ERRORS)) {
Max Reitz791230d2014-10-22 14:09:41 +02002504 BdrvCheckResult old_res = *res;
2505 int fresh_leaks = 0;
2506
Max Reitzc7c06812014-10-22 14:09:40 +02002507 fprintf(stderr, "Rebuilding refcount structure\n");
2508 ret = rebuild_refcount_structure(bs, res, &refcount_table,
2509 &nb_clusters);
2510 if (ret < 0) {
2511 goto fail;
2512 }
Max Reitz791230d2014-10-22 14:09:41 +02002513
2514 res->corruptions = 0;
2515 res->leaks = 0;
2516
2517 /* Because the old reftable has been exchanged for a new one the
2518 * references have to be recalculated */
2519 rebuild = false;
Max Reitz7453c962015-02-10 15:28:50 -05002520 memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters));
Max Reitz791230d2014-10-22 14:09:41 +02002521 ret = calculate_refcounts(bs, res, 0, &rebuild, &refcount_table,
2522 &nb_clusters);
2523 if (ret < 0) {
2524 goto fail;
2525 }
2526
2527 if (fix & BDRV_FIX_LEAKS) {
2528 /* The old refcount structures are now leaked, fix it; the result
2529 * can be ignored, aside from leaks which were introduced by
2530 * rebuild_refcount_structure() that could not be fixed */
2531 BdrvCheckResult saved_res = *res;
2532 *res = (BdrvCheckResult){ 0 };
2533
2534 compare_refcounts(bs, res, BDRV_FIX_LEAKS, &rebuild,
2535 &highest_cluster, refcount_table, nb_clusters);
2536 if (rebuild) {
2537 fprintf(stderr, "ERROR rebuilt refcount structure is still "
2538 "broken\n");
2539 }
2540
2541 /* Any leaks accounted for here were introduced by
2542 * rebuild_refcount_structure() because that function has created a
2543 * new refcount structure from scratch */
2544 fresh_leaks = res->leaks;
2545 *res = saved_res;
2546 }
2547
2548 if (res->corruptions < old_res.corruptions) {
2549 res->corruptions_fixed += old_res.corruptions - res->corruptions;
2550 }
2551 if (res->leaks < old_res.leaks) {
2552 res->leaks_fixed += old_res.leaks - res->leaks;
2553 }
2554 res->leaks += fresh_leaks;
Max Reitzc7c06812014-10-22 14:09:40 +02002555 } else if (fix) {
2556 if (rebuild) {
2557 fprintf(stderr, "ERROR need to rebuild refcount structures\n");
2558 res->check_errors++;
2559 ret = -EIO;
2560 goto fail;
2561 }
2562
2563 if (res->leaks || res->corruptions) {
2564 *res = pre_compare_res;
2565 compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
2566 refcount_table, nb_clusters);
2567 }
Max Reitzf307b252014-10-22 14:09:39 +02002568 }
2569
Max Reitz4f6ed882013-08-30 14:34:27 +02002570 /* check OFLAG_COPIED */
Max Reitze23e4002013-08-30 14:34:28 +02002571 ret = check_oflag_copied(bs, res, fix);
Max Reitz4f6ed882013-08-30 14:34:27 +02002572 if (ret < 0) {
2573 goto fail;
2574 }
2575
Federico Simoncellic6bb9ad2013-01-28 06:59:46 -05002576 res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
Kevin Wolf80fa3342011-06-01 10:50:00 +02002577 ret = 0;
2578
2579fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05002580 g_free(refcount_table);
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002581
Kevin Wolf80fa3342011-06-01 10:50:00 +02002582 return ret;
Kevin Wolff7d0fe02009-05-28 16:07:04 +02002583}
2584
Max Reitza40f1c22013-08-30 14:34:25 +02002585#define overlaps_with(ofs, sz) \
2586 ranges_overlap(offset, size, ofs, sz)
2587
2588/*
2589 * Checks if the given offset into the image file is actually free to use by
2590 * looking for overlaps with important metadata sections (L1/L2 tables etc.),
2591 * i.e. a sanity check without relying on the refcount tables.
2592 *
Max Reitz231bb262013-10-10 11:09:23 +02002593 * The ign parameter specifies what checks not to perform (being a bitmask of
2594 * QCow2MetadataOverlap values), i.e., what sections to ignore.
Max Reitza40f1c22013-08-30 14:34:25 +02002595 *
2596 * Returns:
2597 * - 0 if writing to this offset will not affect the mentioned metadata
2598 * - a positive QCow2MetadataOverlap value indicating one overlapping section
2599 * - a negative value (-errno) indicating an error while performing a check,
2600 * e.g. when bdrv_read failed on QCOW2_OL_INACTIVE_L2
2601 */
Max Reitz231bb262013-10-10 11:09:23 +02002602int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
Max Reitza40f1c22013-08-30 14:34:25 +02002603 int64_t size)
2604{
Kevin Wolfff991292015-09-07 17:12:56 +02002605 BDRVQcow2State *s = bs->opaque;
Max Reitz3e355392013-10-10 11:09:24 +02002606 int chk = s->overlap_check & ~ign;
Max Reitza40f1c22013-08-30 14:34:25 +02002607 int i, j;
2608
2609 if (!size) {
2610 return 0;
2611 }
2612
2613 if (chk & QCOW2_OL_MAIN_HEADER) {
2614 if (offset < s->cluster_size) {
2615 return QCOW2_OL_MAIN_HEADER;
2616 }
2617 }
2618
2619 /* align range to test to cluster boundaries */
Alberto Garcia9e029682018-02-15 15:10:08 +02002620 size = ROUND_UP(offset_into_cluster(s, offset) + size, s->cluster_size);
Max Reitza40f1c22013-08-30 14:34:25 +02002621 offset = start_of_cluster(s, offset);
2622
2623 if ((chk & QCOW2_OL_ACTIVE_L1) && s->l1_size) {
2624 if (overlaps_with(s->l1_table_offset, s->l1_size * sizeof(uint64_t))) {
2625 return QCOW2_OL_ACTIVE_L1;
2626 }
2627 }
2628
2629 if ((chk & QCOW2_OL_REFCOUNT_TABLE) && s->refcount_table_size) {
2630 if (overlaps_with(s->refcount_table_offset,
2631 s->refcount_table_size * sizeof(uint64_t))) {
2632 return QCOW2_OL_REFCOUNT_TABLE;
2633 }
2634 }
2635
2636 if ((chk & QCOW2_OL_SNAPSHOT_TABLE) && s->snapshots_size) {
2637 if (overlaps_with(s->snapshots_offset, s->snapshots_size)) {
2638 return QCOW2_OL_SNAPSHOT_TABLE;
2639 }
2640 }
2641
2642 if ((chk & QCOW2_OL_INACTIVE_L1) && s->snapshots) {
2643 for (i = 0; i < s->nb_snapshots; i++) {
2644 if (s->snapshots[i].l1_size &&
2645 overlaps_with(s->snapshots[i].l1_table_offset,
2646 s->snapshots[i].l1_size * sizeof(uint64_t))) {
2647 return QCOW2_OL_INACTIVE_L1;
2648 }
2649 }
2650 }
2651
2652 if ((chk & QCOW2_OL_ACTIVE_L2) && s->l1_table) {
2653 for (i = 0; i < s->l1_size; i++) {
2654 if ((s->l1_table[i] & L1E_OFFSET_MASK) &&
2655 overlaps_with(s->l1_table[i] & L1E_OFFSET_MASK,
2656 s->cluster_size)) {
2657 return QCOW2_OL_ACTIVE_L2;
2658 }
2659 }
2660 }
2661
2662 if ((chk & QCOW2_OL_REFCOUNT_BLOCK) && s->refcount_table) {
Alberto Garcia7061a072017-02-01 14:38:28 +02002663 unsigned last_entry = s->max_refcount_table_index;
2664 assert(last_entry < s->refcount_table_size);
2665 assert(last_entry + 1 == s->refcount_table_size ||
2666 (s->refcount_table[last_entry + 1] & REFT_OFFSET_MASK) == 0);
2667 for (i = 0; i <= last_entry; i++) {
Max Reitza40f1c22013-08-30 14:34:25 +02002668 if ((s->refcount_table[i] & REFT_OFFSET_MASK) &&
2669 overlaps_with(s->refcount_table[i] & REFT_OFFSET_MASK,
2670 s->cluster_size)) {
2671 return QCOW2_OL_REFCOUNT_BLOCK;
2672 }
2673 }
2674 }
2675
2676 if ((chk & QCOW2_OL_INACTIVE_L2) && s->snapshots) {
2677 for (i = 0; i < s->nb_snapshots; i++) {
2678 uint64_t l1_ofs = s->snapshots[i].l1_table_offset;
2679 uint32_t l1_sz = s->snapshots[i].l1_size;
Max Reitz998b9592013-10-09 10:42:56 +02002680 uint64_t l1_sz2 = l1_sz * sizeof(uint64_t);
Alberto Garciac7a9d812018-03-06 18:14:09 +02002681 uint64_t *l1;
Max Reitza40f1c22013-08-30 14:34:25 +02002682 int ret;
2683
Alberto Garciac7a9d812018-03-06 18:14:09 +02002684 ret = qcow2_validate_table(bs, l1_ofs, l1_sz, sizeof(uint64_t),
2685 QCOW_MAX_L1_SIZE, "", NULL);
2686 if (ret < 0) {
2687 return ret;
2688 }
2689
2690 l1 = g_try_malloc(l1_sz2);
2691
Kevin Wolfde828152014-05-20 17:12:47 +02002692 if (l1_sz2 && l1 == NULL) {
2693 return -ENOMEM;
2694 }
2695
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +02002696 ret = bdrv_pread(bs->file, l1_ofs, l1, l1_sz2);
Max Reitza40f1c22013-08-30 14:34:25 +02002697 if (ret < 0) {
2698 g_free(l1);
2699 return ret;
2700 }
2701
2702 for (j = 0; j < l1_sz; j++) {
Max Reitz1e242b52013-09-30 08:59:28 +02002703 uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK;
2704 if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) {
Max Reitza40f1c22013-08-30 14:34:25 +02002705 g_free(l1);
2706 return QCOW2_OL_INACTIVE_L2;
2707 }
2708 }
2709
2710 g_free(l1);
2711 }
2712 }
2713
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +03002714 if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
2715 (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
2716 {
2717 if (overlaps_with(s->bitmap_directory_offset,
2718 s->bitmap_directory_size))
2719 {
2720 return QCOW2_OL_BITMAP_DIRECTORY;
2721 }
2722 }
2723
Max Reitza40f1c22013-08-30 14:34:25 +02002724 return 0;
2725}
2726
2727static const char *metadata_ol_names[] = {
Liam Merwick7cb6d3c2018-11-05 21:38:39 +00002728 [QCOW2_OL_MAIN_HEADER_BITNR] = "qcow2_header",
2729 [QCOW2_OL_ACTIVE_L1_BITNR] = "active L1 table",
2730 [QCOW2_OL_ACTIVE_L2_BITNR] = "active L2 table",
2731 [QCOW2_OL_REFCOUNT_TABLE_BITNR] = "refcount table",
2732 [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = "refcount block",
2733 [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = "snapshot table",
2734 [QCOW2_OL_INACTIVE_L1_BITNR] = "inactive L1 table",
2735 [QCOW2_OL_INACTIVE_L2_BITNR] = "inactive L2 table",
2736 [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = "bitmap directory",
Max Reitza40f1c22013-08-30 14:34:25 +02002737};
Liam Merwick7cb6d3c2018-11-05 21:38:39 +00002738QEMU_BUILD_BUG_ON(QCOW2_OL_MAX_BITNR != ARRAY_SIZE(metadata_ol_names));
Max Reitza40f1c22013-08-30 14:34:25 +02002739
2740/*
2741 * First performs a check for metadata overlaps (through
2742 * qcow2_check_metadata_overlap); if that fails with a negative value (error
2743 * while performing a check), that value is returned. If an impending overlap
2744 * is detected, the BDS will be made unusable, the qcow2 file marked corrupt
2745 * and -EIO returned.
2746 *
2747 * Returns 0 if there were neither overlaps nor errors while checking for
2748 * overlaps; or a negative value (-errno) on error.
2749 */
Max Reitz231bb262013-10-10 11:09:23 +02002750int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
Max Reitza40f1c22013-08-30 14:34:25 +02002751 int64_t size)
2752{
Max Reitz231bb262013-10-10 11:09:23 +02002753 int ret = qcow2_check_metadata_overlap(bs, ign, offset, size);
Max Reitza40f1c22013-08-30 14:34:25 +02002754
2755 if (ret < 0) {
2756 return ret;
2757 } else if (ret > 0) {
Stefan Hajnoczi786a4ea2015-03-23 15:29:26 +00002758 int metadata_ol_bitnr = ctz32(ret);
Max Reitza40f1c22013-08-30 14:34:25 +02002759 assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
2760
Max Reitzadb43552014-09-05 16:07:17 +02002761 qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
2762 "write on metadata (overlaps with %s)",
2763 metadata_ol_names[metadata_ol_bitnr]);
Max Reitza40f1c22013-08-30 14:34:25 +02002764 return -EIO;
2765 }
2766
2767 return 0;
2768}
Max Reitz791c9a02015-07-27 17:51:37 +02002769
2770/* A pointer to a function of this type is given to walk_over_reftable(). That
2771 * function will create refblocks and pass them to a RefblockFinishOp once they
2772 * are completed (@refblock). @refblock_empty is set if the refblock is
2773 * completely empty.
2774 *
2775 * Along with the refblock, a corresponding reftable entry is passed, in the
2776 * reftable @reftable (which may be reallocated) at @reftable_index.
2777 *
2778 * @allocated should be set to true if a new cluster has been allocated.
2779 */
2780typedef int (RefblockFinishOp)(BlockDriverState *bs, uint64_t **reftable,
2781 uint64_t reftable_index, uint64_t *reftable_size,
2782 void *refblock, bool refblock_empty,
2783 bool *allocated, Error **errp);
2784
2785/**
2786 * This "operation" for walk_over_reftable() allocates the refblock on disk (if
2787 * it is not empty) and inserts its offset into the new reftable. The size of
2788 * this new reftable is increased as required.
2789 */
2790static int alloc_refblock(BlockDriverState *bs, uint64_t **reftable,
2791 uint64_t reftable_index, uint64_t *reftable_size,
2792 void *refblock, bool refblock_empty, bool *allocated,
2793 Error **errp)
2794{
2795 BDRVQcow2State *s = bs->opaque;
2796 int64_t offset;
2797
2798 if (!refblock_empty && reftable_index >= *reftable_size) {
2799 uint64_t *new_reftable;
2800 uint64_t new_reftable_size;
2801
2802 new_reftable_size = ROUND_UP(reftable_index + 1,
2803 s->cluster_size / sizeof(uint64_t));
2804 if (new_reftable_size > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
2805 error_setg(errp,
2806 "This operation would make the refcount table grow "
2807 "beyond the maximum size supported by QEMU, aborting");
2808 return -ENOTSUP;
2809 }
2810
2811 new_reftable = g_try_realloc(*reftable, new_reftable_size *
2812 sizeof(uint64_t));
2813 if (!new_reftable) {
2814 error_setg(errp, "Failed to increase reftable buffer size");
2815 return -ENOMEM;
2816 }
2817
2818 memset(new_reftable + *reftable_size, 0,
2819 (new_reftable_size - *reftable_size) * sizeof(uint64_t));
2820
2821 *reftable = new_reftable;
2822 *reftable_size = new_reftable_size;
2823 }
2824
2825 if (!refblock_empty && !(*reftable)[reftable_index]) {
2826 offset = qcow2_alloc_clusters(bs, s->cluster_size);
2827 if (offset < 0) {
2828 error_setg_errno(errp, -offset, "Failed to allocate refblock");
2829 return offset;
2830 }
2831 (*reftable)[reftable_index] = offset;
2832 *allocated = true;
2833 }
2834
2835 return 0;
2836}
2837
2838/**
2839 * This "operation" for walk_over_reftable() writes the refblock to disk at the
2840 * offset specified by the new reftable's entry. It does not modify the new
2841 * reftable or change any refcounts.
2842 */
2843static int flush_refblock(BlockDriverState *bs, uint64_t **reftable,
2844 uint64_t reftable_index, uint64_t *reftable_size,
2845 void *refblock, bool refblock_empty, bool *allocated,
2846 Error **errp)
2847{
2848 BDRVQcow2State *s = bs->opaque;
2849 int64_t offset;
2850 int ret;
2851
2852 if (reftable_index < *reftable_size && (*reftable)[reftable_index]) {
2853 offset = (*reftable)[reftable_index];
2854
2855 ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
2856 if (ret < 0) {
2857 error_setg_errno(errp, -ret, "Overlap check failed");
2858 return ret;
2859 }
2860
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02002861 ret = bdrv_pwrite(bs->file, offset, refblock, s->cluster_size);
Max Reitz791c9a02015-07-27 17:51:37 +02002862 if (ret < 0) {
2863 error_setg_errno(errp, -ret, "Failed to write refblock");
2864 return ret;
2865 }
2866 } else {
2867 assert(refblock_empty);
2868 }
2869
2870 return 0;
2871}
2872
2873/**
2874 * This function walks over the existing reftable and every referenced refblock;
2875 * if @new_set_refcount is non-NULL, it is called for every refcount entry to
2876 * create an equal new entry in the passed @new_refblock. Once that
2877 * @new_refblock is completely filled, @operation will be called.
2878 *
2879 * @status_cb and @cb_opaque are used for the amend operation's status callback.
2880 * @index is the index of the walk_over_reftable() calls and @total is the total
2881 * number of walk_over_reftable() calls per amend operation. Both are used for
2882 * calculating the parameters for the status callback.
2883 *
2884 * @allocated is set to true if a new cluster has been allocated.
2885 */
2886static int walk_over_reftable(BlockDriverState *bs, uint64_t **new_reftable,
2887 uint64_t *new_reftable_index,
2888 uint64_t *new_reftable_size,
2889 void *new_refblock, int new_refblock_size,
2890 int new_refcount_bits,
2891 RefblockFinishOp *operation, bool *allocated,
2892 Qcow2SetRefcountFunc *new_set_refcount,
2893 BlockDriverAmendStatusCB *status_cb,
2894 void *cb_opaque, int index, int total,
2895 Error **errp)
2896{
2897 BDRVQcow2State *s = bs->opaque;
2898 uint64_t reftable_index;
2899 bool new_refblock_empty = true;
2900 int refblock_index;
2901 int new_refblock_index = 0;
2902 int ret;
2903
2904 for (reftable_index = 0; reftable_index < s->refcount_table_size;
2905 reftable_index++)
2906 {
2907 uint64_t refblock_offset = s->refcount_table[reftable_index]
2908 & REFT_OFFSET_MASK;
2909
2910 status_cb(bs, (uint64_t)index * s->refcount_table_size + reftable_index,
2911 (uint64_t)total * s->refcount_table_size, cb_opaque);
2912
2913 if (refblock_offset) {
2914 void *refblock;
2915
2916 if (offset_into_cluster(s, refblock_offset)) {
2917 qcow2_signal_corruption(bs, true, -1, -1, "Refblock offset %#"
2918 PRIx64 " unaligned (reftable index: %#"
2919 PRIx64 ")", refblock_offset,
2920 reftable_index);
2921 error_setg(errp,
2922 "Image is corrupt (unaligned refblock offset)");
2923 return -EIO;
2924 }
2925
2926 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offset,
2927 &refblock);
2928 if (ret < 0) {
2929 error_setg_errno(errp, -ret, "Failed to retrieve refblock");
2930 return ret;
2931 }
2932
2933 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2934 refblock_index++)
2935 {
2936 uint64_t refcount;
2937
2938 if (new_refblock_index >= new_refblock_size) {
2939 /* new_refblock is now complete */
2940 ret = operation(bs, new_reftable, *new_reftable_index,
2941 new_reftable_size, new_refblock,
2942 new_refblock_empty, allocated, errp);
2943 if (ret < 0) {
Alberto Garcia2013c3d2018-02-05 16:33:07 +02002944 qcow2_cache_put(s->refcount_block_cache, &refblock);
Max Reitz791c9a02015-07-27 17:51:37 +02002945 return ret;
2946 }
2947
2948 (*new_reftable_index)++;
2949 new_refblock_index = 0;
2950 new_refblock_empty = true;
2951 }
2952
2953 refcount = s->get_refcount(refblock, refblock_index);
2954 if (new_refcount_bits < 64 && refcount >> new_refcount_bits) {
2955 uint64_t offset;
2956
Alberto Garcia2013c3d2018-02-05 16:33:07 +02002957 qcow2_cache_put(s->refcount_block_cache, &refblock);
Max Reitz791c9a02015-07-27 17:51:37 +02002958
2959 offset = ((reftable_index << s->refcount_block_bits)
2960 + refblock_index) << s->cluster_bits;
2961
2962 error_setg(errp, "Cannot decrease refcount entry width to "
2963 "%i bits: Cluster at offset %#" PRIx64 " has a "
2964 "refcount of %" PRIu64, new_refcount_bits,
2965 offset, refcount);
2966 return -EINVAL;
2967 }
2968
2969 if (new_set_refcount) {
2970 new_set_refcount(new_refblock, new_refblock_index++,
2971 refcount);
2972 } else {
2973 new_refblock_index++;
2974 }
2975 new_refblock_empty = new_refblock_empty && refcount == 0;
2976 }
2977
Alberto Garcia2013c3d2018-02-05 16:33:07 +02002978 qcow2_cache_put(s->refcount_block_cache, &refblock);
Max Reitz791c9a02015-07-27 17:51:37 +02002979 } else {
2980 /* No refblock means every refcount is 0 */
2981 for (refblock_index = 0; refblock_index < s->refcount_block_size;
2982 refblock_index++)
2983 {
2984 if (new_refblock_index >= new_refblock_size) {
2985 /* new_refblock is now complete */
2986 ret = operation(bs, new_reftable, *new_reftable_index,
2987 new_reftable_size, new_refblock,
2988 new_refblock_empty, allocated, errp);
2989 if (ret < 0) {
2990 return ret;
2991 }
2992
2993 (*new_reftable_index)++;
2994 new_refblock_index = 0;
2995 new_refblock_empty = true;
2996 }
2997
2998 if (new_set_refcount) {
2999 new_set_refcount(new_refblock, new_refblock_index++, 0);
3000 } else {
3001 new_refblock_index++;
3002 }
3003 }
3004 }
3005 }
3006
3007 if (new_refblock_index > 0) {
3008 /* Complete the potentially existing partially filled final refblock */
3009 if (new_set_refcount) {
3010 for (; new_refblock_index < new_refblock_size;
3011 new_refblock_index++)
3012 {
3013 new_set_refcount(new_refblock, new_refblock_index, 0);
3014 }
3015 }
3016
3017 ret = operation(bs, new_reftable, *new_reftable_index,
3018 new_reftable_size, new_refblock, new_refblock_empty,
3019 allocated, errp);
3020 if (ret < 0) {
3021 return ret;
3022 }
3023
3024 (*new_reftable_index)++;
3025 }
3026
3027 status_cb(bs, (uint64_t)(index + 1) * s->refcount_table_size,
3028 (uint64_t)total * s->refcount_table_size, cb_opaque);
3029
3030 return 0;
3031}
3032
3033int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
3034 BlockDriverAmendStatusCB *status_cb,
3035 void *cb_opaque, Error **errp)
3036{
3037 BDRVQcow2State *s = bs->opaque;
3038 Qcow2GetRefcountFunc *new_get_refcount;
3039 Qcow2SetRefcountFunc *new_set_refcount;
3040 void *new_refblock = qemu_blockalign(bs->file->bs, s->cluster_size);
3041 uint64_t *new_reftable = NULL, new_reftable_size = 0;
3042 uint64_t *old_reftable, old_reftable_size, old_reftable_offset;
3043 uint64_t new_reftable_index = 0;
3044 uint64_t i;
3045 int64_t new_reftable_offset = 0, allocated_reftable_size = 0;
3046 int new_refblock_size, new_refcount_bits = 1 << refcount_order;
3047 int old_refcount_order;
3048 int walk_index = 0;
3049 int ret;
3050 bool new_allocation;
3051
3052 assert(s->qcow_version >= 3);
3053 assert(refcount_order >= 0 && refcount_order <= 6);
3054
3055 /* see qcow2_open() */
3056 new_refblock_size = 1 << (s->cluster_bits - (refcount_order - 3));
3057
3058 new_get_refcount = get_refcount_funcs[refcount_order];
3059 new_set_refcount = set_refcount_funcs[refcount_order];
3060
3061
3062 do {
3063 int total_walks;
3064
3065 new_allocation = false;
3066
3067 /* At least we have to do this walk and the one which writes the
3068 * refblocks; also, at least we have to do this loop here at least
3069 * twice (normally), first to do the allocations, and second to
3070 * determine that everything is correctly allocated, this then makes
3071 * three walks in total */
3072 total_walks = MAX(walk_index + 2, 3);
3073
3074 /* First, allocate the structures so they are present in the refcount
3075 * structures */
3076 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
3077 &new_reftable_size, NULL, new_refblock_size,
3078 new_refcount_bits, &alloc_refblock,
3079 &new_allocation, NULL, status_cb, cb_opaque,
3080 walk_index++, total_walks, errp);
3081 if (ret < 0) {
3082 goto done;
3083 }
3084
3085 new_reftable_index = 0;
3086
3087 if (new_allocation) {
3088 if (new_reftable_offset) {
3089 qcow2_free_clusters(bs, new_reftable_offset,
3090 allocated_reftable_size * sizeof(uint64_t),
3091 QCOW2_DISCARD_NEVER);
3092 }
3093
3094 new_reftable_offset = qcow2_alloc_clusters(bs, new_reftable_size *
3095 sizeof(uint64_t));
3096 if (new_reftable_offset < 0) {
3097 error_setg_errno(errp, -new_reftable_offset,
3098 "Failed to allocate the new reftable");
3099 ret = new_reftable_offset;
3100 goto done;
3101 }
3102 allocated_reftable_size = new_reftable_size;
3103 }
3104 } while (new_allocation);
3105
3106 /* Second, write the new refblocks */
3107 ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
3108 &new_reftable_size, new_refblock,
3109 new_refblock_size, new_refcount_bits,
3110 &flush_refblock, &new_allocation, new_set_refcount,
3111 status_cb, cb_opaque, walk_index, walk_index + 1,
3112 errp);
3113 if (ret < 0) {
3114 goto done;
3115 }
3116 assert(!new_allocation);
3117
3118
3119 /* Write the new reftable */
3120 ret = qcow2_pre_write_overlap_check(bs, 0, new_reftable_offset,
3121 new_reftable_size * sizeof(uint64_t));
3122 if (ret < 0) {
3123 error_setg_errno(errp, -ret, "Overlap check failed");
3124 goto done;
3125 }
3126
3127 for (i = 0; i < new_reftable_size; i++) {
3128 cpu_to_be64s(&new_reftable[i]);
3129 }
3130
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +02003131 ret = bdrv_pwrite(bs->file, new_reftable_offset, new_reftable,
Max Reitz791c9a02015-07-27 17:51:37 +02003132 new_reftable_size * sizeof(uint64_t));
3133
3134 for (i = 0; i < new_reftable_size; i++) {
3135 be64_to_cpus(&new_reftable[i]);
3136 }
3137
3138 if (ret < 0) {
3139 error_setg_errno(errp, -ret, "Failed to write the new reftable");
3140 goto done;
3141 }
3142
3143
3144 /* Empty the refcount cache */
3145 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
3146 if (ret < 0) {
3147 error_setg_errno(errp, -ret, "Failed to flush the refblock cache");
3148 goto done;
3149 }
3150
3151 /* Update the image header to point to the new reftable; this only updates
3152 * the fields which are relevant to qcow2_update_header(); other fields
3153 * such as s->refcount_table or s->refcount_bits stay stale for now
3154 * (because we have to restore everything if qcow2_update_header() fails) */
3155 old_refcount_order = s->refcount_order;
3156 old_reftable_size = s->refcount_table_size;
3157 old_reftable_offset = s->refcount_table_offset;
3158
3159 s->refcount_order = refcount_order;
3160 s->refcount_table_size = new_reftable_size;
3161 s->refcount_table_offset = new_reftable_offset;
3162
3163 ret = qcow2_update_header(bs);
3164 if (ret < 0) {
3165 s->refcount_order = old_refcount_order;
3166 s->refcount_table_size = old_reftable_size;
3167 s->refcount_table_offset = old_reftable_offset;
3168 error_setg_errno(errp, -ret, "Failed to update the qcow2 header");
3169 goto done;
3170 }
3171
3172 /* Now update the rest of the in-memory information */
3173 old_reftable = s->refcount_table;
3174 s->refcount_table = new_reftable;
Alberto Garcia7061a072017-02-01 14:38:28 +02003175 update_max_refcount_table_index(s);
Max Reitz791c9a02015-07-27 17:51:37 +02003176
3177 s->refcount_bits = 1 << refcount_order;
3178 s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
3179 s->refcount_max += s->refcount_max - 1;
3180
3181 s->refcount_block_bits = s->cluster_bits - (refcount_order - 3);
3182 s->refcount_block_size = 1 << s->refcount_block_bits;
3183
3184 s->get_refcount = new_get_refcount;
3185 s->set_refcount = new_set_refcount;
3186
3187 /* For cleaning up all old refblocks and the old reftable below the "done"
3188 * label */
3189 new_reftable = old_reftable;
3190 new_reftable_size = old_reftable_size;
3191 new_reftable_offset = old_reftable_offset;
3192
3193done:
3194 if (new_reftable) {
3195 /* On success, new_reftable actually points to the old reftable (and
3196 * new_reftable_size is the old reftable's size); but that is just
3197 * fine */
3198 for (i = 0; i < new_reftable_size; i++) {
3199 uint64_t offset = new_reftable[i] & REFT_OFFSET_MASK;
3200 if (offset) {
3201 qcow2_free_clusters(bs, offset, s->cluster_size,
3202 QCOW2_DISCARD_OTHER);
3203 }
3204 }
3205 g_free(new_reftable);
3206
3207 if (new_reftable_offset > 0) {
3208 qcow2_free_clusters(bs, new_reftable_offset,
3209 new_reftable_size * sizeof(uint64_t),
3210 QCOW2_DISCARD_OTHER);
3211 }
3212 }
3213
3214 qemu_vfree(new_refblock);
3215 return ret;
3216}
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003217
Max Reitz23482f82017-11-10 21:31:10 +01003218static int64_t get_refblock_offset(BlockDriverState *bs, uint64_t offset)
3219{
3220 BDRVQcow2State *s = bs->opaque;
3221 uint32_t index = offset_to_reftable_index(s, offset);
3222 int64_t covering_refblock_offset = 0;
3223
3224 if (index < s->refcount_table_size) {
3225 covering_refblock_offset = s->refcount_table[index] & REFT_OFFSET_MASK;
3226 }
3227 if (!covering_refblock_offset) {
3228 qcow2_signal_corruption(bs, true, -1, -1, "Refblock at %#" PRIx64 " is "
3229 "not covered by the refcount structures",
3230 offset);
3231 return -EIO;
3232 }
3233
3234 return covering_refblock_offset;
3235}
3236
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003237static int qcow2_discard_refcount_block(BlockDriverState *bs,
3238 uint64_t discard_block_offs)
3239{
3240 BDRVQcow2State *s = bs->opaque;
Max Reitz23482f82017-11-10 21:31:10 +01003241 int64_t refblock_offs;
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003242 uint64_t cluster_index = discard_block_offs >> s->cluster_bits;
3243 uint32_t block_index = cluster_index & (s->refcount_block_size - 1);
3244 void *refblock;
3245 int ret;
3246
Max Reitz23482f82017-11-10 21:31:10 +01003247 refblock_offs = get_refblock_offset(bs, discard_block_offs);
3248 if (refblock_offs < 0) {
3249 return refblock_offs;
3250 }
3251
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003252 assert(discard_block_offs != 0);
3253
3254 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3255 &refblock);
3256 if (ret < 0) {
3257 return ret;
3258 }
3259
3260 if (s->get_refcount(refblock, block_index) != 1) {
3261 qcow2_signal_corruption(bs, true, -1, -1, "Invalid refcount:"
3262 " refblock offset %#" PRIx64
3263 ", reftable index %u"
3264 ", block offset %#" PRIx64
3265 ", refcount %#" PRIx64,
3266 refblock_offs,
3267 offset_to_reftable_index(s, discard_block_offs),
3268 discard_block_offs,
3269 s->get_refcount(refblock, block_index));
Alberto Garcia2013c3d2018-02-05 16:33:07 +02003270 qcow2_cache_put(s->refcount_block_cache, &refblock);
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003271 return -EINVAL;
3272 }
3273 s->set_refcount(refblock, block_index, 0);
3274
Alberto Garcia2d135ee2018-02-05 16:33:06 +02003275 qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refblock);
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003276
Alberto Garcia2013c3d2018-02-05 16:33:07 +02003277 qcow2_cache_put(s->refcount_block_cache, &refblock);
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003278
3279 if (cluster_index < s->free_cluster_index) {
3280 s->free_cluster_index = cluster_index;
3281 }
3282
Alberto Garcia6e6fa762018-02-05 16:33:11 +02003283 refblock = qcow2_cache_is_table_offset(s->refcount_block_cache,
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003284 discard_block_offs);
3285 if (refblock) {
3286 /* discard refblock from the cache if refblock is cached */
Alberto Garcia77aadd72018-02-05 16:33:10 +02003287 qcow2_cache_discard(s->refcount_block_cache, refblock);
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003288 }
3289 update_refcount_discard(bs, discard_block_offs, s->cluster_size);
3290
3291 return 0;
3292}
3293
3294int qcow2_shrink_reftable(BlockDriverState *bs)
3295{
3296 BDRVQcow2State *s = bs->opaque;
3297 uint64_t *reftable_tmp =
3298 g_malloc(s->refcount_table_size * sizeof(uint64_t));
3299 int i, ret;
3300
3301 for (i = 0; i < s->refcount_table_size; i++) {
3302 int64_t refblock_offs = s->refcount_table[i] & REFT_OFFSET_MASK;
3303 void *refblock;
3304 bool unused_block;
3305
3306 if (refblock_offs == 0) {
3307 reftable_tmp[i] = 0;
3308 continue;
3309 }
3310 ret = qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs,
3311 &refblock);
3312 if (ret < 0) {
3313 goto out;
3314 }
3315
3316 /* the refblock has own reference */
3317 if (i == offset_to_reftable_index(s, refblock_offs)) {
3318 uint64_t block_index = (refblock_offs >> s->cluster_bits) &
3319 (s->refcount_block_size - 1);
3320 uint64_t refcount = s->get_refcount(refblock, block_index);
3321
3322 s->set_refcount(refblock, block_index, 0);
3323
3324 unused_block = buffer_is_zero(refblock, s->cluster_size);
3325
3326 s->set_refcount(refblock, block_index, refcount);
3327 } else {
3328 unused_block = buffer_is_zero(refblock, s->cluster_size);
3329 }
Alberto Garcia2013c3d2018-02-05 16:33:07 +02003330 qcow2_cache_put(s->refcount_block_cache, &refblock);
Pavel Butsykin46b732c2017-09-18 15:42:29 +03003331
3332 reftable_tmp[i] = unused_block ? 0 : cpu_to_be64(s->refcount_table[i]);
3333 }
3334
3335 ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset, reftable_tmp,
3336 s->refcount_table_size * sizeof(uint64_t));
3337 /*
3338 * If the write in the reftable failed the image may contain a partially
3339 * overwritten reftable. In this case it would be better to clear the
3340 * reftable in memory to avoid possible image corruption.
3341 */
3342 for (i = 0; i < s->refcount_table_size; i++) {
3343 if (s->refcount_table[i] && !reftable_tmp[i]) {
3344 if (ret == 0) {
3345 ret = qcow2_discard_refcount_block(bs, s->refcount_table[i] &
3346 REFT_OFFSET_MASK);
3347 }
3348 s->refcount_table[i] = 0;
3349 }
3350 }
3351
3352 if (!s->cache_discards) {
3353 qcow2_process_discards(bs, ret);
3354 }
3355
3356out:
3357 g_free(reftable_tmp);
3358 return ret;
3359}
Pavel Butsykin163bc392017-09-29 15:16:13 +03003360
3361int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size)
3362{
3363 BDRVQcow2State *s = bs->opaque;
3364 int64_t i;
3365
3366 for (i = size_to_clusters(s, size) - 1; i >= 0; i--) {
3367 uint64_t refcount;
3368 int ret = qcow2_get_refcount(bs, i, &refcount);
3369 if (ret < 0) {
3370 fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
3371 i, strerror(-ret));
3372 return ret;
3373 }
3374 if (refcount > 0) {
3375 return i;
3376 }
3377 }
3378 qcow2_signal_corruption(bs, true, -1, -1,
3379 "There are no references in the refcount table.");
3380 return -EIO;
3381}