Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for the QCOW version 2 format |
| 3 | * |
| 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 27 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/block_int.h" |
Michael S. Tsirkin | 0d8c41d | 2018-05-03 22:50:20 +0300 | [diff] [blame] | 29 | #include "qcow2.h" |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 30 | #include "qemu/range.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 31 | #include "qemu/bswap.h" |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 32 | #include "qemu/cutils.h" |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 33 | |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 34 | static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size, |
| 35 | uint64_t max); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 36 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 37 | int64_t offset, int64_t length, uint64_t addend, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 38 | bool decrease, enum qcow2_discard_type type); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 39 | |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 40 | static uint64_t get_refcount_ro0(const void *refcount_array, uint64_t index); |
| 41 | static uint64_t get_refcount_ro1(const void *refcount_array, uint64_t index); |
| 42 | static uint64_t get_refcount_ro2(const void *refcount_array, uint64_t index); |
| 43 | static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index); |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 44 | static uint64_t get_refcount_ro4(const void *refcount_array, uint64_t index); |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 45 | static uint64_t get_refcount_ro5(const void *refcount_array, uint64_t index); |
| 46 | static uint64_t get_refcount_ro6(const void *refcount_array, uint64_t index); |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 47 | |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 48 | static void set_refcount_ro0(void *refcount_array, uint64_t index, |
| 49 | uint64_t value); |
| 50 | static void set_refcount_ro1(void *refcount_array, uint64_t index, |
| 51 | uint64_t value); |
| 52 | static void set_refcount_ro2(void *refcount_array, uint64_t index, |
| 53 | uint64_t value); |
| 54 | static void set_refcount_ro3(void *refcount_array, uint64_t index, |
| 55 | uint64_t value); |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 56 | static void set_refcount_ro4(void *refcount_array, uint64_t index, |
| 57 | uint64_t value); |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 58 | static void set_refcount_ro5(void *refcount_array, uint64_t index, |
| 59 | uint64_t value); |
| 60 | static void set_refcount_ro6(void *refcount_array, uint64_t index, |
| 61 | uint64_t value); |
| 62 | |
| 63 | |
| 64 | static 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 | |
| 74 | static 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 83 | |
Kevin Wolf | 3b88e52 | 2009-06-26 20:19:38 +0200 | [diff] [blame] | 84 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 85 | /*********************************************************/ |
| 86 | /* refcount handling */ |
| 87 | |
Alberto Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 88 | static 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 Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 98 | int qcow2_refcount_init(BlockDriverState *bs) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 99 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 100 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 5dab2fa | 2014-03-26 13:05:43 +0100 | [diff] [blame] | 101 | unsigned int refcount_table_size2, i; |
| 102 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 103 | |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 104 | 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 108 | |
Kevin Wolf | 5dab2fa | 2014-03-26 13:05:43 +0100 | [diff] [blame] | 109 | assert(s->refcount_table_size <= INT_MAX / sizeof(uint64_t)); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 110 | refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 111 | s->refcount_table = g_try_malloc(refcount_table_size2); |
| 112 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 113 | if (s->refcount_table_size > 0) { |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 114 | if (s->refcount_table == NULL) { |
Max Reitz | 8fcffa9 | 2014-05-29 00:19:54 +0200 | [diff] [blame] | 115 | ret = -ENOMEM; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 116 | goto fail; |
| 117 | } |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 118 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_LOAD); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 119 | ret = bdrv_pread(bs->file, s->refcount_table_offset, |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 120 | s->refcount_table, refcount_table_size2); |
Max Reitz | 8fcffa9 | 2014-05-29 00:19:54 +0200 | [diff] [blame] | 121 | if (ret < 0) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 122 | goto fail; |
Max Reitz | 8fcffa9 | 2014-05-29 00:19:54 +0200 | [diff] [blame] | 123 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 124 | for(i = 0; i < s->refcount_table_size; i++) |
| 125 | be64_to_cpus(&s->refcount_table[i]); |
Alberto Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 126 | update_max_refcount_table_index(s); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 127 | } |
| 128 | return 0; |
| 129 | fail: |
Max Reitz | 8fcffa9 | 2014-05-29 00:19:54 +0200 | [diff] [blame] | 130 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 133 | void qcow2_refcount_close(BlockDriverState *bs) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 134 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 135 | BDRVQcow2State *s = bs->opaque; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 136 | g_free(s->refcount_table); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
Max Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 140 | static 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 | |
| 145 | static 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 | |
| 153 | static 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 | |
| 159 | static 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 | |
| 167 | static 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 | |
| 173 | static 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 | |
| 181 | static uint64_t get_refcount_ro3(const void *refcount_array, uint64_t index) |
| 182 | { |
| 183 | return ((const uint8_t *)refcount_array)[index]; |
| 184 | } |
| 185 | |
| 186 | static 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 193 | static 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 | |
| 198 | static 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 Reitz | 59c0cb7 | 2015-02-10 15:28:51 -0500 | [diff] [blame] | 205 | static 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 | |
| 210 | static 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 | |
| 217 | static 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 | |
| 222 | static 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 228 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 229 | static int load_refcount_block(BlockDriverState *bs, |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 230 | int64_t refcount_block_offset, |
| 231 | void **refcount_block) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 232 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 233 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 3b88e52 | 2009-06-26 20:19:38 +0200 | [diff] [blame] | 234 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 235 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_LOAD); |
Eduardo Habkost | 9be3859 | 2016-06-13 18:57:58 -0300 | [diff] [blame] | 236 | return qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, |
| 237 | refcount_block); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 240 | /* |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 241 | * 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 Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 243 | */ |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 244 | int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index, |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 245 | uint64_t *refcount) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 246 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 247 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | db8a31d | 2014-03-26 13:05:49 +0100 | [diff] [blame] | 248 | uint64_t refcount_table_index, block_index; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 249 | int64_t refcount_block_offset; |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 250 | int ret; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 251 | void *refcount_block; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 252 | |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 253 | refcount_table_index = cluster_index >> s->refcount_block_bits; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 254 | if (refcount_table_index >= s->refcount_table_size) { |
| 255 | *refcount = 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 256 | return 0; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 257 | } |
Max Reitz | 26d49c4 | 2014-03-07 23:10:12 +0100 | [diff] [blame] | 258 | refcount_block_offset = |
| 259 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 260 | if (!refcount_block_offset) { |
| 261 | *refcount = 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 262 | return 0; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 263 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 264 | |
Max Reitz | a97c67e | 2014-09-05 16:07:18 +0200 | [diff] [blame] | 265 | 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 Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 272 | ret = qcow2_cache_get(bs, s->refcount_block_cache, refcount_block_offset, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 273 | &refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 274 | if (ret < 0) { |
| 275 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 276 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 277 | |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 278 | block_index = cluster_index & (s->refcount_block_size - 1); |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 279 | *refcount = s->get_refcount(refcount_block, block_index); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 280 | |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 281 | qcow2_cache_put(s->refcount_block_cache, &refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 282 | |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 283 | return 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 286 | /* Checks if two offsets are described by the same refcount block */ |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 287 | static int in_same_refcount_block(BDRVQcow2State *s, uint64_t offset_a, |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 288 | uint64_t offset_b) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 289 | { |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 290 | 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 292 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 293 | return (block_a == block_b); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 296 | /* |
| 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 Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 300 | * Returns 0 on success or -errno in error case |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 301 | */ |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 302 | static int alloc_refcount_block(BlockDriverState *bs, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 303 | int64_t cluster_index, void **refcount_block) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 304 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 305 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 306 | unsigned int refcount_table_index; |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 307 | int64_t ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 308 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 309 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); |
Kevin Wolf | 8252278 | 2010-03-15 17:38:05 +0100 | [diff] [blame] | 310 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 311 | /* Find the refcount block for the given cluster */ |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 312 | refcount_table_index = cluster_index >> s->refcount_block_bits; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 313 | |
| 314 | if (refcount_table_index < s->refcount_table_size) { |
| 315 | |
| 316 | uint64_t refcount_block_offset = |
Kevin Wolf | 76dc9e0 | 2012-03-16 14:09:08 +0100 | [diff] [blame] | 317 | s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 318 | |
| 319 | /* If it's already there, we're done */ |
| 320 | if (refcount_block_offset) { |
Max Reitz | a97c67e | 2014-09-05 16:07:18 +0200 | [diff] [blame] | 321 | 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 Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 329 | return load_refcount_block(bs, refcount_block_offset, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 330 | refcount_block); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 331 | } |
| 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 Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 347 | * 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 Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 352 | * |
| 353 | * - alloc_clusters_noref and qcow2_free_clusters may load a different |
| 354 | * refcount block into the cache |
| 355 | */ |
| 356 | |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 357 | *refcount_block = NULL; |
| 358 | |
| 359 | /* We write to the refcount table, so we might depend on L2 tables */ |
Stefan Hajnoczi | 9991923 | 2013-03-04 15:02:30 +0100 | [diff] [blame] | 360 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
| 361 | if (ret < 0) { |
| 362 | return ret; |
| 363 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 364 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 365 | /* Allocate the refcount block itself and mark it as used */ |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 366 | int64_t new_block = alloc_clusters_noref(bs, s->cluster_size, INT64_MAX); |
Kevin Wolf | 2eaa8f6 | 2010-06-04 11:22:39 +0200 | [diff] [blame] | 367 | if (new_block < 0) { |
| 368 | return new_block; |
| 369 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 370 | |
Alberto Garcia | 6bf45d5 | 2017-11-03 16:18:50 +0200 | [diff] [blame] | 371 | /* 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 Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 378 | #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 Wolf | 25408c0 | 2010-05-28 12:05:45 +0200 | [diff] [blame] | 385 | /* Zero the new refcount block before updating it */ |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 386 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 387 | refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 388 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 389 | goto fail; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | memset(*refcount_block, 0, s->cluster_size); |
Kevin Wolf | 25408c0 | 2010-05-28 12:05:45 +0200 | [diff] [blame] | 393 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 394 | /* The block describes itself, need to update the cache */ |
| 395 | int block_index = (new_block >> s->cluster_bits) & |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 396 | (s->refcount_block_size - 1); |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 397 | s->set_refcount(*refcount_block, block_index, 1); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 398 | } else { |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 399 | /* Described somewhere else. This can recurse at most twice before we |
| 400 | * arrive at a block that describes itself. */ |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 401 | ret = update_refcount(bs, new_block, s->cluster_size, 1, false, |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 402 | QCOW2_DISCARD_NEVER); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 403 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 404 | goto fail; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 405 | } |
Kevin Wolf | 25408c0 | 2010-05-28 12:05:45 +0200 | [diff] [blame] | 406 | |
Stefan Hajnoczi | 9991923 | 2013-03-04 15:02:30 +0100 | [diff] [blame] | 407 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
| 408 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 409 | goto fail; |
Stefan Hajnoczi | 9991923 | 2013-03-04 15:02:30 +0100 | [diff] [blame] | 410 | } |
Kevin Wolf | 1c4c281 | 2010-09-17 16:36:58 +0200 | [diff] [blame] | 411 | |
Kevin Wolf | 25408c0 | 2010-05-28 12:05:45 +0200 | [diff] [blame] | 412 | /* Initialize the new refcount block only after updating its refcount, |
| 413 | * update_refcount uses the refcount cache itself */ |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 414 | ret = qcow2_cache_get_empty(bs, s->refcount_block_cache, new_block, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 415 | refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 416 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 417 | goto fail; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | memset(*refcount_block, 0, s->cluster_size); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 423 | /* Now the new refcount block needs to be written to disk */ |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 424 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE); |
Alberto Garcia | 2d135ee | 2018-02-05 16:33:06 +0200 | [diff] [blame] | 425 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 426 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 427 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 428 | goto fail; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 429 | } |
| 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 Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 434 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_HOOKUP); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 435 | ret = bdrv_pwrite_sync(bs->file, |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 436 | s->refcount_table_offset + refcount_table_index * sizeof(uint64_t), |
| 437 | &data64, sizeof(data64)); |
| 438 | if (ret < 0) { |
Max Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 439 | goto fail; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | s->refcount_table[refcount_table_index] = new_block; |
Alberto Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 443 | /* 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 Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 447 | |
| 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 Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 453 | qcow2_cache_put(s->refcount_block_cache, refcount_block); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 454 | |
| 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 Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 466 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW); |
Kevin Wolf | 8252278 | 2010-03-15 17:38:05 +0100 | [diff] [blame] | 467 | |
Max Reitz | 14a58a4 | 2015-02-10 15:02:31 -0500 | [diff] [blame] | 468 | /* 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 Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 482 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 483 | /* Create the new refcount table and blocks */ |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 484 | uint64_t meta_offset = (blocks_used * s->refcount_block_size) * |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 485 | s->cluster_size; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 486 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 487 | 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 Reitz | 60c48a2 | 2017-06-13 22:21:04 +0200 | [diff] [blame] | 503 | fail: |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 504 | if (*refcount_block != NULL) { |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 505 | qcow2_cache_put(s->refcount_block_cache, refcount_block); |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 506 | } |
| 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 Reitz | 772d1f9 | 2017-06-13 22:21:05 +0200 | [diff] [blame] | 525 | int64_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 Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 529 | { |
| 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 Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 574 | ret = -ENOMEM; |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 575 | goto fail; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 576 | } |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 577 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 578 | /* Fill the new refcount table */ |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 579 | 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 Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 591 | if (new_refblock_offset) { |
| 592 | assert(new_refblock_index < total_refblock_count); |
| 593 | new_table[new_refblock_index] = new_refblock_offset; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 596 | /* 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 Garcia | 2d135ee | 2018-02-05 16:33:06 +0200 | [diff] [blame] | 627 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 628 | 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 Garcia | 2d135ee | 2018-02-05 16:33:06 +0200 | [diff] [blame] | 660 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 661 | refblock_data); |
| 662 | } |
| 663 | |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 664 | qcow2_cache_put(s->refcount_block_cache, &refblock_data); |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | assert(block_offset == table_offset); |
| 668 | |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 669 | /* Write refcount blocks to disk */ |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 670 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS); |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 671 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 672 | if (ret < 0) { |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 673 | goto fail; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* Write refcount table to disk */ |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 677 | for (i = 0; i < total_refblock_count; i++) { |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 678 | cpu_to_be64s(&new_table[i]); |
| 679 | } |
| 680 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 681 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 682 | ret = bdrv_pwrite_sync(bs->file, table_offset, new_table, |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 683 | table_size * sizeof(uint64_t)); |
| 684 | if (ret < 0) { |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 685 | goto fail; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 688 | for (i = 0; i < total_refblock_count; i++) { |
Zhi Yong Wu | 8726775 | 2012-04-28 15:38:08 +0800 | [diff] [blame] | 689 | be64_to_cpus(&new_table[i]); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* Hook up the new refcount table in the qcow2 header */ |
John Snow | 9533423 | 2015-11-02 18:32:06 -0500 | [diff] [blame] | 693 | struct QEMU_PACKED { |
| 694 | uint64_t d64; |
| 695 | uint32_t d32; |
| 696 | } data; |
Peter Maydell | f1f7a1d | 2016-06-16 17:06:17 +0100 | [diff] [blame] | 697 | data.d64 = cpu_to_be64(table_offset); |
| 698 | data.d32 = cpu_to_be32(table_clusters); |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 699 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 700 | ret = bdrv_pwrite_sync(bs->file, |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 701 | offsetof(QCowHeader, refcount_table_offset), |
John Snow | 9533423 | 2015-11-02 18:32:06 -0500 | [diff] [blame] | 702 | &data, sizeof(data)); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 703 | if (ret < 0) { |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 704 | goto fail; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 705 | } |
| 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 Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 711 | g_free(s->refcount_table); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 712 | s->refcount_table = new_table; |
| 713 | s->refcount_table_size = table_size; |
| 714 | s->refcount_table_offset = table_offset; |
Alberto Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 715 | update_max_refcount_table_index(s); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 716 | |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 717 | /* Free old table. */ |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 718 | qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t), |
| 719 | QCOW2_DISCARD_OTHER); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 720 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 721 | return end_offset; |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 722 | |
Max Reitz | 12cc30a | 2017-06-13 22:21:03 +0200 | [diff] [blame] | 723 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 724 | g_free(new_table); |
Kevin Wolf | 92dcb59 | 2010-02-23 16:40:53 +0100 | [diff] [blame] | 725 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 728 | void qcow2_process_discards(BlockDriverState *bs, int ret) |
| 729 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 730 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 731 | 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 Zheng | 0b9fd3f | 2018-07-10 14:31:17 +0800 | [diff] [blame] | 738 | bdrv_pdiscard(bs->file, d->offset, d->bytes); |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | g_free(d); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | static void update_refcount_discard(BlockDriverState *bs, |
| 746 | uint64_t offset, uint64_t length) |
| 747 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 748 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 749 | 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 | |
| 774 | found: |
| 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 Haoyu | d8bb71b | 2014-10-11 16:35:43 +0800 | [diff] [blame] | 791 | g_free(p); |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 795 | /* XXX: cache several refcount block clusters ? */ |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 796 | /* @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 Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 798 | static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 799 | int64_t offset, |
| 800 | int64_t length, |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 801 | uint64_t addend, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 802 | bool decrease, |
| 803 | enum qcow2_discard_type type) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 804 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 805 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 806 | int64_t start, last, cluster_offset; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 807 | void *refcount_block = NULL; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 808 | int64_t old_table_index = -1; |
Kevin Wolf | 09508d1 | 2010-01-20 15:03:04 +0100 | [diff] [blame] | 809 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 810 | |
| 811 | #ifdef DEBUG_ALLOC2 |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 812 | fprintf(stderr, "update_refcount: offset=%" PRId64 " size=%" PRId64 |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 813 | " addend=%s%" PRIu64 "\n", offset, length, decrease ? "-" : "", |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 814 | addend); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 815 | #endif |
Kevin Wolf | 7322afe | 2010-01-20 15:03:05 +0100 | [diff] [blame] | 816 | if (length < 0) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 817 | return -EINVAL; |
Kevin Wolf | 7322afe | 2010-01-20 15:03:05 +0100 | [diff] [blame] | 818 | } else if (length == 0) { |
| 819 | return 0; |
| 820 | } |
| 821 | |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 822 | if (decrease) { |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 823 | qcow2_cache_set_dependency(bs, s->refcount_block_cache, |
| 824 | s->l2_table_cache); |
| 825 | } |
| 826 | |
Hu Tao | ac95acd | 2013-12-05 14:32:34 +0800 | [diff] [blame] | 827 | start = start_of_cluster(s, offset); |
| 828 | last = start_of_cluster(s, offset + length - 1); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 829 | for(cluster_offset = start; cluster_offset <= last; |
| 830 | cluster_offset += s->cluster_size) |
| 831 | { |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 832 | int block_index; |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 833 | uint64_t refcount; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 834 | int64_t cluster_index = cluster_offset >> s->cluster_bits; |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 835 | int64_t table_index = cluster_index >> s->refcount_block_bits; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 836 | |
| 837 | /* Load the refcount block and allocate it if needed */ |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 838 | if (table_index != old_table_index) { |
| 839 | if (refcount_block) { |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 840 | qcow2_cache_put(s->refcount_block_cache, &refcount_block); |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 841 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 842 | ret = alloc_refcount_block(bs, cluster_index, &refcount_block); |
Alberto Garcia | abf754f | 2018-03-21 15:38:52 +0200 | [diff] [blame] | 843 | /* 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 Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 850 | if (ret < 0) { |
| 851 | goto fail; |
| 852 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 853 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 854 | old_table_index = table_index; |
| 855 | |
Alberto Garcia | 2d135ee | 2018-02-05 16:33:06 +0200 | [diff] [blame] | 856 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 857 | |
| 858 | /* we can update the count and save it */ |
Max Reitz | 17bd5f4 | 2014-09-03 00:25:07 +0200 | [diff] [blame] | 859 | block_index = cluster_index & (s->refcount_block_size - 1); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 860 | |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 861 | refcount = s->get_refcount(refcount_block, block_index); |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 862 | if (decrease ? (refcount - addend > refcount) |
| 863 | : (refcount + addend < refcount || |
| 864 | refcount + addend > s->refcount_max)) |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 865 | { |
Kevin Wolf | 09508d1 | 2010-01-20 15:03:04 +0100 | [diff] [blame] | 866 | ret = -EINVAL; |
| 867 | goto fail; |
| 868 | } |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 869 | if (decrease) { |
| 870 | refcount -= addend; |
| 871 | } else { |
| 872 | refcount += addend; |
| 873 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 874 | if (refcount == 0 && cluster_index < s->free_cluster_index) { |
| 875 | s->free_cluster_index = cluster_index; |
| 876 | } |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 877 | s->set_refcount(refcount_block, block_index, refcount); |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 878 | |
Pavel Butsykin | f71c08e | 2017-09-18 15:42:28 +0300 | [diff] [blame] | 879 | if (refcount == 0) { |
| 880 | void *table; |
| 881 | |
Alberto Garcia | 6e6fa76 | 2018-02-05 16:33:11 +0200 | [diff] [blame] | 882 | table = qcow2_cache_is_table_offset(s->refcount_block_cache, |
Pavel Butsykin | f71c08e | 2017-09-18 15:42:28 +0300 | [diff] [blame] | 883 | offset); |
| 884 | if (table != NULL) { |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 885 | qcow2_cache_put(s->refcount_block_cache, &refcount_block); |
Alberto Garcia | 77aadd7 | 2018-02-05 16:33:10 +0200 | [diff] [blame] | 886 | qcow2_cache_discard(s->refcount_block_cache, table); |
Pavel Butsykin | f71c08e | 2017-09-18 15:42:28 +0300 | [diff] [blame] | 887 | } |
| 888 | |
Alberto Garcia | 6e6fa76 | 2018-02-05 16:33:11 +0200 | [diff] [blame] | 889 | table = qcow2_cache_is_table_offset(s->l2_table_cache, offset); |
Pavel Butsykin | f71c08e | 2017-09-18 15:42:28 +0300 | [diff] [blame] | 890 | if (table != NULL) { |
Alberto Garcia | 77aadd7 | 2018-02-05 16:33:10 +0200 | [diff] [blame] | 891 | qcow2_cache_discard(s->l2_table_cache, table); |
Pavel Butsykin | f71c08e | 2017-09-18 15:42:28 +0300 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (s->discard_passthrough[type]) { |
| 895 | update_refcount_discard(bs, cluster_offset, s->cluster_size); |
| 896 | } |
Kevin Wolf | 67af674 | 2013-06-19 13:44:19 +0200 | [diff] [blame] | 897 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Kevin Wolf | 09508d1 | 2010-01-20 15:03:04 +0100 | [diff] [blame] | 900 | ret = 0; |
| 901 | fail: |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 902 | if (!s->cache_discards) { |
| 903 | qcow2_process_discards(bs, ret); |
| 904 | } |
| 905 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 906 | /* Write last changed block to disk */ |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 907 | if (refcount_block) { |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 908 | qcow2_cache_put(s->refcount_block_cache, &refcount_block); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Kevin Wolf | 09508d1 | 2010-01-20 15:03:04 +0100 | [diff] [blame] | 911 | /* |
| 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 Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 917 | dummy = update_refcount(bs, offset, cluster_offset - offset, addend, |
| 918 | !decrease, QCOW2_DISCARD_NEVER); |
Blue Swirl | 83e3f76 | 2010-10-13 18:38:07 +0000 | [diff] [blame] | 919 | (void)dummy; |
Kevin Wolf | 09508d1 | 2010-01-20 15:03:04 +0100 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 925 | /* |
Max Reitz | 4475191 | 2014-10-27 11:12:54 +0100 | [diff] [blame] | 926 | * Increases or decreases the refcount of a given cluster. |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 927 | * |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 928 | * @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 Reitz | c6e9d8a | 2015-02-10 15:28:45 -0500 | [diff] [blame] | 931 | * On success 0 is returned; on failure -errno is returned. |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 932 | */ |
Max Reitz | 32b6444 | 2013-09-03 10:09:52 +0200 | [diff] [blame] | 933 | int qcow2_update_cluster_refcount(BlockDriverState *bs, |
| 934 | int64_t cluster_index, |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 935 | uint64_t addend, bool decrease, |
Max Reitz | 32b6444 | 2013-09-03 10:09:52 +0200 | [diff] [blame] | 936 | enum qcow2_discard_type type) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 937 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 938 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 939 | int ret; |
| 940 | |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 941 | ret = update_refcount(bs, cluster_index << s->cluster_bits, 1, addend, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 942 | decrease, type); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 943 | if (ret < 0) { |
| 944 | return ret; |
| 945 | } |
| 946 | |
Max Reitz | c6e9d8a | 2015-02-10 15:28:45 -0500 | [diff] [blame] | 947 | return 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | |
| 951 | |
| 952 | /*********************************************************/ |
| 953 | /* cluster allocation functions */ |
| 954 | |
| 955 | |
| 956 | |
| 957 | /* return < 0 if error */ |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 958 | static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size, |
| 959 | uint64_t max) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 960 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 961 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 962 | uint64_t i, nb_clusters, refcount; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 963 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 964 | |
Kevin Wolf | ecbda7a | 2015-05-06 13:21:51 +0200 | [diff] [blame] | 965 | /* 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 970 | nb_clusters = size_to_clusters(s, size); |
| 971 | retry: |
| 972 | for(i = 0; i < nb_clusters; i++) { |
Kevin Wolf | bb572ae | 2014-03-26 13:05:51 +0100 | [diff] [blame] | 973 | uint64_t next_cluster_index = s->free_cluster_index++; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 974 | ret = qcow2_get_refcount(bs, next_cluster_index, &refcount); |
Kevin Wolf | 2eaa8f6 | 2010-06-04 11:22:39 +0200 | [diff] [blame] | 975 | |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 976 | if (ret < 0) { |
| 977 | return ret; |
Kevin Wolf | 2eaa8f6 | 2010-06-04 11:22:39 +0200 | [diff] [blame] | 978 | } else if (refcount != 0) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 979 | goto retry; |
Kevin Wolf | 2eaa8f6 | 2010-06-04 11:22:39 +0200 | [diff] [blame] | 980 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 981 | } |
Max Reitz | 91f827d | 2014-04-29 19:03:11 +0200 | [diff] [blame] | 982 | |
| 983 | /* Make sure that all offsets in the "allocated" range are representable |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 984 | * in the requested max */ |
Max Reitz | 65f33bc | 2014-05-04 05:31:40 +0200 | [diff] [blame] | 985 | if (s->free_cluster_index > 0 && |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 986 | s->free_cluster_index - 1 > (max >> s->cluster_bits)) |
Max Reitz | 65f33bc | 2014-05-04 05:31:40 +0200 | [diff] [blame] | 987 | { |
Max Reitz | 91f827d | 2014-04-29 19:03:11 +0200 | [diff] [blame] | 988 | return -EFBIG; |
| 989 | } |
| 990 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 991 | #ifdef DEBUG_ALLOC2 |
Frediano Ziglio | 35ee5e3 | 2011-08-25 09:23:55 +0200 | [diff] [blame] | 992 | fprintf(stderr, "alloc_clusters: size=%" PRId64 " -> %" PRId64 "\n", |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 993 | 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 Wolf | bb572ae | 2014-03-26 13:05:51 +0100 | [diff] [blame] | 999 | int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1000 | { |
| 1001 | int64_t offset; |
Kevin Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 1002 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1003 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 1004 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC); |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1005 | do { |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 1006 | offset = alloc_clusters_noref(bs, size, QCOW_MAX_CLUSTER_OFFSET); |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1007 | if (offset < 0) { |
| 1008 | return offset; |
| 1009 | } |
Kevin Wolf | 2eaa8f6 | 2010-06-04 11:22:39 +0200 | [diff] [blame] | 1010 | |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1011 | ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER); |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1012 | } while (ret == -EAGAIN); |
| 1013 | |
Kevin Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 1014 | if (ret < 0) { |
| 1015 | return ret; |
| 1016 | } |
Kevin Wolf | 1c4c281 | 2010-09-17 16:36:58 +0200 | [diff] [blame] | 1017 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1018 | return offset; |
| 1019 | } |
| 1020 | |
Max Reitz | b6d36de | 2015-09-14 16:39:47 +0200 | [diff] [blame] | 1021 | int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, |
| 1022 | int64_t nb_clusters) |
Kevin Wolf | 256900b | 2012-03-02 19:35:58 +0100 | [diff] [blame] | 1023 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1024 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 1025 | uint64_t cluster_index, refcount; |
Hu Tao | 33304ec | 2014-01-26 11:12:38 +0800 | [diff] [blame] | 1026 | uint64_t i; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1027 | int ret; |
Hu Tao | 33304ec | 2014-01-26 11:12:38 +0800 | [diff] [blame] | 1028 | |
| 1029 | assert(nb_clusters >= 0); |
| 1030 | if (nb_clusters == 0) { |
| 1031 | return 0; |
| 1032 | } |
Kevin Wolf | 256900b | 2012-03-02 19:35:58 +0100 | [diff] [blame] | 1033 | |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1034 | 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 Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1038 | ret = qcow2_get_refcount(bs, cluster_index++, &refcount); |
| 1039 | if (ret < 0) { |
| 1040 | return ret; |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1041 | } else if (refcount != 0) { |
| 1042 | break; |
| 1043 | } |
Kevin Wolf | 256900b | 2012-03-02 19:35:58 +0100 | [diff] [blame] | 1044 | } |
Kevin Wolf | 256900b | 2012-03-02 19:35:58 +0100 | [diff] [blame] | 1045 | |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1046 | /* And then allocate them */ |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1047 | ret = update_refcount(bs, offset, i << s->cluster_bits, 1, false, |
Kevin Wolf | b106ad9 | 2014-03-28 18:06:31 +0100 | [diff] [blame] | 1048 | QCOW2_DISCARD_NEVER); |
| 1049 | } while (ret == -EAGAIN); |
Kevin Wolf | f24423b | 2012-04-20 15:50:39 +0200 | [diff] [blame] | 1050 | |
Kevin Wolf | 256900b | 2012-03-02 19:35:58 +0100 | [diff] [blame] | 1051 | if (ret < 0) { |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | return i; |
| 1056 | } |
| 1057 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1058 | /* only used to allocate compressed sectors. We try to allocate |
| 1059 | contiguous sectors. size must be <= cluster_size */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1060 | int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1061 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1062 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1063 | int64_t offset; |
| 1064 | size_t free_in_cluster; |
| 1065 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1066 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 1067 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_BYTES); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1068 | assert(size > 0 && size <= s->cluster_size); |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1069 | 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 Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 1074 | uint64_t refcount; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1075 | ret = qcow2_get_refcount(bs, offset >> s->cluster_bits, &refcount); |
| 1076 | if (ret < 0) { |
| 1077 | return ret; |
Kevin Wolf | 5d757b5 | 2010-01-20 15:04:01 +0100 | [diff] [blame] | 1078 | } |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1079 | |
Max Reitz | 346a53d | 2015-02-10 15:28:43 -0500 | [diff] [blame] | 1080 | if (refcount == s->refcount_max) { |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1081 | offset = 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1082 | } |
| 1083 | } |
Kevin Wolf | 29216ed | 2010-09-17 16:57:48 +0200 | [diff] [blame] | 1084 | |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1085 | free_in_cluster = s->cluster_size - offset_into_cluster(s, offset); |
Jindřich Makovička | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1086 | do { |
| 1087 | if (!offset || free_in_cluster < size) { |
Eric Blake | 77d6a21 | 2018-11-13 17:03:18 -0600 | [diff] [blame] | 1088 | 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čka | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1093 | if (new_cluster < 0) { |
| 1094 | return new_cluster; |
| 1095 | } |
| 1096 | |
Alberto Garcia | 8aa3483 | 2017-11-03 16:18:52 +0200 | [diff] [blame] | 1097 | 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čka | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1104 | if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) { |
| 1105 | offset = new_cluster; |
Max Reitz | 2ac0152 | 2015-09-11 18:47:51 +0200 | [diff] [blame] | 1106 | free_in_cluster = s->cluster_size; |
| 1107 | } else { |
| 1108 | free_in_cluster += s->cluster_size; |
Jindřich Makovička | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1109 | } |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1110 | } |
| 1111 | |
Jindřich Makovička | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1112 | assert(offset); |
| 1113 | ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER); |
Max Reitz | 2ac0152 | 2015-09-11 18:47:51 +0200 | [diff] [blame] | 1114 | if (ret < 0) { |
| 1115 | offset = 0; |
| 1116 | } |
Jindřich Makovička | 3e5feb6 | 2015-06-24 07:05:25 +0200 | [diff] [blame] | 1117 | } while (ret == -EAGAIN); |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1118 | 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 Hajnoczi | c1f5baf | 2013-03-04 15:02:32 +0100 | [diff] [blame] | 1124 | qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); |
Max Reitz | 8c44dfb | 2015-02-06 09:39:16 -0500 | [diff] [blame] | 1125 | |
| 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1131 | return offset; |
| 1132 | } |
| 1133 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1134 | void qcow2_free_clusters(BlockDriverState *bs, |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 1135 | int64_t offset, int64_t size, |
| 1136 | enum qcow2_discard_type type) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1137 | { |
Kevin Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 1138 | int ret; |
| 1139 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 1140 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_FREE); |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1141 | ret = update_refcount(bs, offset, size, 1, true, type); |
Kevin Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 1142 | if (ret < 0) { |
| 1143 | fprintf(stderr, "qcow2_free_clusters failed: %s\n", strerror(-ret)); |
Kevin Wolf | 003fad6 | 2010-04-21 11:37:52 +0200 | [diff] [blame] | 1144 | /* TODO Remember the clusters to free them later and avoid leaking */ |
Kevin Wolf | db3a964 | 2010-01-20 15:03:06 +0100 | [diff] [blame] | 1145 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1148 | /* |
Kevin Wolf | c7a4c37 | 2012-03-27 13:09:22 +0200 | [diff] [blame] | 1149 | * Free a cluster using its L2 entry (handles clusters of all types, e.g. |
| 1150 | * normal cluster, compressed cluster, etc.) |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1151 | */ |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 1152 | void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry, |
| 1153 | int nb_clusters, enum qcow2_discard_type type) |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1154 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1155 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1156 | |
Kevin Wolf | c7a4c37 | 2012-03-27 13:09:22 +0200 | [diff] [blame] | 1157 | 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 Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 1165 | nb_csectors * 512, type); |
Kevin Wolf | c7a4c37 | 2012-03-27 13:09:22 +0200 | [diff] [blame] | 1166 | } |
| 1167 | break; |
| 1168 | case QCOW2_CLUSTER_NORMAL: |
Eric Blake | fdfab37 | 2017-05-06 19:05:46 -0500 | [diff] [blame] | 1169 | 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 Reitz | 8f730dd | 2013-10-09 10:44:28 +0200 | [diff] [blame] | 1177 | } |
Kevin Wolf | c7a4c37 | 2012-03-27 13:09:22 +0200 | [diff] [blame] | 1178 | break; |
Eric Blake | fdfab37 | 2017-05-06 19:05:46 -0500 | [diff] [blame] | 1179 | case QCOW2_CLUSTER_ZERO_PLAIN: |
Kevin Wolf | c7a4c37 | 2012-03-27 13:09:22 +0200 | [diff] [blame] | 1180 | case QCOW2_CLUSTER_UNALLOCATED: |
| 1181 | break; |
| 1182 | default: |
| 1183 | abort(); |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1184 | } |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 1187 | int coroutine_fn qcow2_write_caches(BlockDriverState *bs) |
| 1188 | { |
| 1189 | BDRVQcow2State *s = bs->opaque; |
| 1190 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1191 | |
Paolo Bonzini | 8b220eb | 2018-03-01 17:36:14 +0100 | [diff] [blame] | 1192 | 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 | |
| 1207 | int 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1216 | |
| 1217 | /*********************************************************/ |
| 1218 | /* snapshots and image creation */ |
| 1219 | |
| 1220 | |
| 1221 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1222 | /* update the refcounts of snapshots and the copied flag */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1223 | int qcow2_update_snapshot_refcount(BlockDriverState *bs, |
| 1224 | int64_t l1_table_offset, int l1_size, int addend) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1225 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1226 | BDRVQcow2State *s = bs->opaque; |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1227 | uint64_t *l1_table, *l2_slice, l2_offset, entry, l1_size2, refcount; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1228 | bool l1_allocated = false; |
Eric Blake | b32cbae | 2017-05-06 19:05:41 -0500 | [diff] [blame] | 1229 | int64_t old_entry, old_l2_offset; |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1230 | unsigned slice, slice_size2, n_slices; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1231 | int i, j, l1_modified = 0, nb_csectors; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1232 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1233 | |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1234 | assert(addend >= -1 && addend <= 1); |
| 1235 | |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1236 | l2_slice = NULL; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1237 | l1_table = NULL; |
| 1238 | l1_size2 = l1_size * sizeof(uint64_t); |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1239 | slice_size2 = s->l2_slice_size * sizeof(uint64_t); |
| 1240 | n_slices = s->cluster_size / slice_size2; |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 1241 | |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 1242 | s->cache_discards = true; |
| 1243 | |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 1244 | /* 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1247 | if (l1_table_offset != s->l1_table_offset) { |
Alberto Garcia | 9e02968 | 2018-02-15 15:10:08 +0200 | [diff] [blame] | 1248 | l1_table = g_try_malloc0(ROUND_UP(l1_size2, 512)); |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1249 | if (l1_size2 && l1_table == NULL) { |
| 1250 | ret = -ENOMEM; |
| 1251 | goto fail; |
| 1252 | } |
| 1253 | l1_allocated = true; |
Kevin Wolf | c2bc78b | 2013-04-05 12:51:31 +0200 | [diff] [blame] | 1254 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1255 | ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2); |
Kevin Wolf | c2bc78b | 2013-04-05 12:51:31 +0200 | [diff] [blame] | 1256 | if (ret < 0) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1257 | goto fail; |
Kevin Wolf | 93913df | 2011-07-19 13:01:48 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Eric Blake | b32cbae | 2017-05-06 19:05:41 -0500 | [diff] [blame] | 1260 | for (i = 0; i < l1_size; i++) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1261 | be64_to_cpus(&l1_table[i]); |
Eric Blake | b32cbae | 2017-05-06 19:05:41 -0500 | [diff] [blame] | 1262 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1263 | } else { |
| 1264 | assert(l1_size == s->l1_size); |
| 1265 | l1_table = s->l1_table; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1266 | l1_allocated = false; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
Eric Blake | b32cbae | 2017-05-06 19:05:41 -0500 | [diff] [blame] | 1269 | for (i = 0; i < l1_size; i++) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1270 | l2_offset = l1_table[i]; |
| 1271 | if (l2_offset) { |
| 1272 | old_l2_offset = l2_offset; |
Kevin Wolf | 8e37f68 | 2012-02-23 15:40:55 +0100 | [diff] [blame] | 1273 | l2_offset &= L1E_OFFSET_MASK; |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1274 | |
Max Reitz | a97c67e | 2014-09-05 16:07:18 +0200 | [diff] [blame] | 1275 | 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 Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1283 | for (slice = 0; slice < n_slices; slice++) { |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1284 | ret = qcow2_cache_get(bs, s->l2_table_cache, |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1285 | l2_offset + slice * slice_size2, |
| 1286 | (void **) &l2_slice); |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1287 | if (ret < 0) { |
| 1288 | goto fail; |
| 1289 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1290 | |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1291 | for (j = 0; j < s->l2_slice_size; j++) { |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1292 | uint64_t cluster_index; |
| 1293 | uint64_t offset; |
Max Reitz | 8b81a7b | 2013-08-30 10:40:14 +0200 | [diff] [blame] | 1294 | |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1295 | entry = be64_to_cpu(l2_slice[j]); |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1296 | old_entry = entry; |
| 1297 | entry &= ~QCOW_OFLAG_COPIED; |
| 1298 | offset = entry & L2E_OFFSET_MASK; |
Max Reitz | 8b81a7b | 2013-08-30 10:40:14 +0200 | [diff] [blame] | 1299 | |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1300 | 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 Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1307 | nb_csectors * 512, abs(addend), addend < 0, |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 1308 | QCOW2_DISCARD_SNAPSHOT); |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1309 | 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 Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1320 | /* Here l2_index means table (not slice) index */ |
| 1321 | int l2_index = slice * s->l2_slice_size + j; |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1322 | qcow2_signal_corruption( |
| 1323 | bs, true, -1, -1, "Cluster " |
| 1324 | "allocation offset %#" PRIx64 |
| 1325 | " unaligned (L2 offset: %#" |
| 1326 | PRIx64 ", L2 index: %#x)", |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1327 | offset, l2_offset, l2_index); |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1328 | 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 Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1344 | if (ret < 0) { |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 1345 | goto fail; |
| 1346 | } |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1347 | break; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1348 | |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1349 | case QCOW2_CLUSTER_ZERO_PLAIN: |
| 1350 | case QCOW2_CLUSTER_UNALLOCATED: |
| 1351 | refcount = 0; |
| 1352 | break; |
| 1353 | |
| 1354 | default: |
| 1355 | abort(); |
Eric Blake | bbd995d | 2017-05-06 19:05:42 -0500 | [diff] [blame] | 1356 | } |
| 1357 | |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1358 | 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 Blake | bbd995d | 2017-05-06 19:05:42 -0500 | [diff] [blame] | 1365 | } |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1366 | l2_slice[j] = cpu_to_be64(entry); |
Alberto Garcia | ca62dd5 | 2018-02-05 16:33:26 +0200 | [diff] [blame] | 1367 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1368 | l2_slice); |
Eric Blake | bbd995d | 2017-05-06 19:05:42 -0500 | [diff] [blame] | 1369 | } |
Max Reitz | 8b81a7b | 2013-08-30 10:40:14 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1372 | qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1373 | } |
Kevin Wolf | 29c1a73 | 2011-01-10 17:17:28 +0100 | [diff] [blame] | 1374 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1375 | if (addend != 0) { |
Max Reitz | c6e9d8a | 2015-02-10 15:28:45 -0500 | [diff] [blame] | 1376 | ret = qcow2_update_cluster_refcount(bs, l2_offset >> |
| 1377 | s->cluster_bits, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 1378 | abs(addend), addend < 0, |
Max Reitz | c6e9d8a | 2015-02-10 15:28:45 -0500 | [diff] [blame] | 1379 | QCOW2_DISCARD_SNAPSHOT); |
| 1380 | if (ret < 0) { |
| 1381 | goto fail; |
| 1382 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1383 | } |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1384 | ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits, |
| 1385 | &refcount); |
| 1386 | if (ret < 0) { |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 1387 | goto fail; |
| 1388 | } else if (refcount == 1) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1389 | 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 Wolf | 93913df | 2011-07-19 13:01:48 +0200 | [diff] [blame] | 1397 | |
Stefan Hajnoczi | 2154f24 | 2013-03-04 15:02:33 +0100 | [diff] [blame] | 1398 | ret = bdrv_flush(bs); |
Kevin Wolf | 93913df | 2011-07-19 13:01:48 +0200 | [diff] [blame] | 1399 | fail: |
Alberto Garcia | 83ad165 | 2018-02-05 16:33:27 +0200 | [diff] [blame] | 1400 | if (l2_slice) { |
| 1401 | qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); |
Kevin Wolf | 93913df | 2011-07-19 13:01:48 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Kevin Wolf | 0b919fa | 2013-06-19 13:44:20 +0200 | [diff] [blame] | 1404 | s->cache_discards = false; |
| 1405 | qcow2_process_discards(bs, ret); |
| 1406 | |
Kevin Wolf | 43a0cac | 2011-11-16 15:20:45 +0100 | [diff] [blame] | 1407 | /* Update L1 only if it isn't deleted anyway (addend = -1) */ |
Kevin Wolf | c2b6ff5 | 2013-04-05 12:57:10 +0200 | [diff] [blame] | 1408 | if (ret == 0 && addend >= 0 && l1_modified) { |
| 1409 | for (i = 0; i < l1_size; i++) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1410 | cpu_to_be64s(&l1_table[i]); |
Kevin Wolf | c2b6ff5 | 2013-04-05 12:57:10 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 1413 | ret = bdrv_pwrite_sync(bs->file, l1_table_offset, |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 1414 | l1_table, l1_size2); |
Kevin Wolf | c2b6ff5 | 2013-04-05 12:57:10 +0200 | [diff] [blame] | 1415 | |
| 1416 | for (i = 0; i < l1_size; i++) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1417 | be64_to_cpus(&l1_table[i]); |
Kevin Wolf | c2b6ff5 | 2013-04-05 12:57:10 +0200 | [diff] [blame] | 1418 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1419 | } |
| 1420 | if (l1_allocated) |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1421 | g_free(l1_table); |
Kevin Wolf | 93913df | 2011-07-19 13:01:48 +0200 | [diff] [blame] | 1422 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | /*********************************************************/ |
| 1429 | /* refcount checking functions */ |
| 1430 | |
| 1431 | |
Kevin Wolf | c2551b4 | 2015-12-01 15:16:49 +0100 | [diff] [blame] | 1432 | static uint64_t refcount_array_byte_size(BDRVQcow2State *s, uint64_t entries) |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1433 | { |
| 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 Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1455 | static int realloc_refcount_array(BDRVQcow2State *s, void **array, |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1456 | int64_t *size, int64_t new_size) |
| 1457 | { |
Max Reitz | b6d36de | 2015-09-14 16:39:47 +0200 | [diff] [blame] | 1458 | int64_t old_byte_size, new_byte_size; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1459 | void *new_ptr; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1460 | |
| 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 Reitz | b6d36de | 2015-09-14 16:39:47 +0200 | [diff] [blame] | 1474 | if (new_byte_size > SIZE_MAX) { |
| 1475 | return -ENOMEM; |
| 1476 | } |
| 1477 | |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1478 | 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 Reitz | b6d36de | 2015-09-14 16:39:47 +0200 | [diff] [blame] | 1484 | memset((char *)new_ptr + old_byte_size, 0, |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1485 | new_byte_size - old_byte_size); |
| 1486 | } |
| 1487 | |
| 1488 | *array = new_ptr; |
| 1489 | *size = new_size; |
| 1490 | |
| 1491 | return 0; |
| 1492 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1493 | |
| 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. Berrange | b6af097 | 2015-08-26 12:17:13 +0100 | [diff] [blame] | 1497 | * which can be compared to the refcount table saved in the image. |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1498 | * |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1499 | * Modifies the number of errors in res. |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1500 | */ |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1501 | int 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 Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1505 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1506 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1507 | uint64_t start, last, cluster_offset, k, refcount; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1508 | int ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1509 | |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1510 | if (size <= 0) { |
| 1511 | return 0; |
| 1512 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1513 | |
Hu Tao | ac95acd | 2013-12-05 14:32:34 +0800 | [diff] [blame] | 1514 | start = start_of_cluster(s, offset); |
| 1515 | last = start_of_cluster(s, offset + size - 1); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1516 | for(cluster_offset = start; cluster_offset <= last; |
| 1517 | cluster_offset += s->cluster_size) { |
| 1518 | k = cluster_offset >> s->cluster_bits; |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1519 | if (k >= *refcount_table_size) { |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1520 | ret = realloc_refcount_array(s, refcount_table, |
| 1521 | refcount_table_size, k + 1); |
| 1522 | if (ret < 0) { |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1523 | res->check_errors++; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1524 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1525 | } |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1528 | refcount = s->get_refcount(*refcount_table, k); |
| 1529 | if (refcount == s->refcount_max) { |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1530 | fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64 |
| 1531 | "\n", cluster_offset); |
Max Reitz | 03bb78e | 2015-07-27 17:51:39 +0200 | [diff] [blame] | 1532 | 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 Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1535 | res->corruptions++; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1536 | continue; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1537 | } |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1538 | s->set_refcount(*refcount_table, k, refcount + 1); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1539 | } |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1540 | |
| 1541 | return 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
Stefan Hajnoczi | 801f704 | 2013-02-07 17:15:01 +0100 | [diff] [blame] | 1544 | /* Flags for check_refcounts_l1() and check_refcounts_l2() */ |
| 1545 | enum { |
Stefan Hajnoczi | fba31ba | 2013-02-07 17:15:02 +0100 | [diff] [blame] | 1546 | CHECK_FRAG_INFO = 0x2, /* update BlockFragInfo counters */ |
Stefan Hajnoczi | 801f704 | 2013-02-07 17:15:01 +0100 | [diff] [blame] | 1547 | }; |
| 1548 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1549 | /* |
| 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 Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1557 | static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1558 | void **refcount_table, |
| 1559 | int64_t *refcount_table_size, int64_t l2_offset, |
Max Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 1560 | int flags, BdrvCheckMode fix) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1561 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1562 | BDRVQcow2State *s = bs->opaque; |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1563 | uint64_t *l2_table, l2_entry; |
Stefan Hajnoczi | fba31ba | 2013-02-07 17:15:02 +0100 | [diff] [blame] | 1564 | uint64_t next_contiguous_offset = 0; |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1565 | int i, l2_size, nb_csectors, ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1566 | |
| 1567 | /* Read L2 table from disk */ |
| 1568 | l2_size = s->l2_size * sizeof(uint64_t); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1569 | l2_table = g_malloc(l2_size); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1570 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1571 | ret = bdrv_pread(bs->file, l2_offset, l2_table, l2_size); |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1572 | if (ret < 0) { |
| 1573 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l2\n"); |
| 1574 | res->check_errors++; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1575 | goto fail; |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1576 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1577 | |
| 1578 | /* Do the actual checks */ |
| 1579 | for(i = 0; i < s->l2_size; i++) { |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1580 | 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 Garcia | 74c44a5 | 2018-04-10 18:05:03 +0200 | [diff] [blame] | 1586 | fprintf(stderr, "ERROR: coffset=0x%" PRIx64 ": " |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1587 | "copied flag must never be set for compressed " |
Alberto Garcia | 74c44a5 | 2018-04-10 18:05:03 +0200 | [diff] [blame] | 1588 | "clusters\n", l2_entry & s->cluster_offset_mask); |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1589 | 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-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1597 | ret = qcow2_inc_refcounts_imrt(bs, res, |
| 1598 | refcount_table, refcount_table_size, |
| 1599 | l2_entry & ~511, nb_csectors * 512); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1600 | if (ret < 0) { |
| 1601 | goto fail; |
| 1602 | } |
Stefan Hajnoczi | fba31ba | 2013-02-07 17:15:02 +0100 | [diff] [blame] | 1603 | |
| 1604 | if (flags & CHECK_FRAG_INFO) { |
| 1605 | res->bfi.allocated_clusters++; |
Stefan Hajnoczi | 4db3516 | 2013-02-07 17:15:05 +0100 | [diff] [blame] | 1606 | res->bfi.compressed_clusters++; |
Stefan Hajnoczi | fba31ba | 2013-02-07 17:15:02 +0100 | [diff] [blame] | 1607 | |
| 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 Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1615 | break; |
| 1616 | |
Eric Blake | fdfab37 | 2017-05-06 19:05:46 -0500 | [diff] [blame] | 1617 | case QCOW2_CLUSTER_ZERO_ALLOC: |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1618 | case QCOW2_CLUSTER_NORMAL: |
| 1619 | { |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1620 | uint64_t offset = l2_entry & L2E_OFFSET_MASK; |
| 1621 | |
Stefan Hajnoczi | fba31ba | 2013-02-07 17:15:02 +0100 | [diff] [blame] | 1622 | 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 Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 1631 | /* 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 Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1682 | /* Mark cluster as used */ |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1683 | ret = qcow2_inc_refcounts_imrt(bs, res, |
| 1684 | refcount_table, refcount_table_size, |
| 1685 | offset, s->cluster_size); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1686 | if (ret < 0) { |
| 1687 | goto fail; |
| 1688 | } |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1689 | break; |
| 1690 | } |
| 1691 | |
Eric Blake | fdfab37 | 2017-05-06 19:05:46 -0500 | [diff] [blame] | 1692 | case QCOW2_CLUSTER_ZERO_PLAIN: |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1693 | case QCOW2_CLUSTER_UNALLOCATED: |
| 1694 | break; |
| 1695 | |
| 1696 | default: |
| 1697 | abort(); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1701 | g_free(l2_table); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1702 | return 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1703 | |
| 1704 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1705 | g_free(l2_table); |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1706 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1707 | } |
| 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 | */ |
| 1717 | static int check_refcounts_l1(BlockDriverState *bs, |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1718 | BdrvCheckResult *res, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1719 | void **refcount_table, |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 1720 | int64_t *refcount_table_size, |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1721 | int64_t l1_table_offset, int l1_size, |
Max Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 1722 | int flags, BdrvCheckMode fix) |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1723 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1724 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1725 | uint64_t *l1_table = NULL, l2_offset, l1_size2; |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1726 | int i, ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1727 | |
| 1728 | l1_size2 = l1_size * sizeof(uint64_t); |
| 1729 | |
| 1730 | /* Mark L1 table as used */ |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1731 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size, |
| 1732 | l1_table_offset, l1_size2); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1733 | if (ret < 0) { |
| 1734 | goto fail; |
| 1735 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1736 | |
| 1737 | /* Read L1 table entries from disk */ |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1738 | if (l1_size2 > 0) { |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1739 | l1_table = g_try_malloc(l1_size2); |
| 1740 | if (l1_table == NULL) { |
| 1741 | ret = -ENOMEM; |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1742 | res->check_errors++; |
Kevin Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 1743 | goto fail; |
| 1744 | } |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1745 | ret = bdrv_pread(bs->file, l1_table_offset, l1_table, l1_size2); |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1746 | if (ret < 0) { |
| 1747 | fprintf(stderr, "ERROR: I/O error in check_refcounts_l1\n"); |
| 1748 | res->check_errors++; |
Kevin Wolf | 702ef63 | 2009-11-27 17:35:55 +0100 | [diff] [blame] | 1749 | goto fail; |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1750 | } |
Kevin Wolf | 702ef63 | 2009-11-27 17:35:55 +0100 | [diff] [blame] | 1751 | for(i = 0;i < l1_size; i++) |
| 1752 | be64_to_cpus(&l1_table[i]); |
| 1753 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1754 | |
| 1755 | /* Do the actual checks */ |
| 1756 | for(i = 0; i < l1_size; i++) { |
| 1757 | l2_offset = l1_table[i]; |
| 1758 | if (l2_offset) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1759 | /* Mark L2 table as used */ |
Kevin Wolf | afdf0ab | 2012-03-27 13:44:56 +0200 | [diff] [blame] | 1760 | l2_offset &= L1E_OFFSET_MASK; |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1761 | ret = qcow2_inc_refcounts_imrt(bs, res, |
| 1762 | refcount_table, refcount_table_size, |
| 1763 | l2_offset, s->cluster_size); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1764 | if (ret < 0) { |
| 1765 | goto fail; |
| 1766 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1767 | |
| 1768 | /* L2 tables are cluster aligned */ |
Hu Tao | ac95acd | 2013-12-05 14:32:34 +0800 | [diff] [blame] | 1769 | if (offset_into_cluster(s, l2_offset)) { |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1770 | fprintf(stderr, "ERROR l2_offset=%" PRIx64 ": Table is not " |
| 1771 | "cluster aligned; L1 entry corrupted\n", l2_offset); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1772 | res->corruptions++; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* Process and check L2 entries */ |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1776 | ret = check_refcounts_l2(bs, res, refcount_table, |
Max Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 1777 | refcount_table_size, l2_offset, flags, |
| 1778 | fix); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1779 | if (ret < 0) { |
| 1780 | goto fail; |
| 1781 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1782 | } |
| 1783 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1784 | g_free(l1_table); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1785 | return 0; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1786 | |
| 1787 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1788 | g_free(l1_table); |
Max Reitz | ad27390 | 2014-10-22 14:09:34 +0200 | [diff] [blame] | 1789 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | /* |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1793 | * 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 Reitz | 4475191 | 2014-10-27 11:12:54 +0100 | [diff] [blame] | 1796 | * 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 Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1798 | * (qcow2_check_refcounts) by the time this function is called). |
| 1799 | */ |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1800 | static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res, |
| 1801 | BdrvCheckMode fix) |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1802 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1803 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1804 | uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size); |
| 1805 | int ret; |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 1806 | uint64_t refcount; |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1807 | int i, j; |
Max Reitz | 3cce51c | 2018-05-09 22:00:58 +0200 | [diff] [blame] | 1808 | 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 Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1821 | |
| 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 Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1825 | bool l2_dirty = false; |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1826 | |
| 1827 | if (!l2_offset) { |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1831 | ret = qcow2_get_refcount(bs, l2_offset >> s->cluster_bits, |
| 1832 | &refcount); |
| 1833 | if (ret < 0) { |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1834 | /* don't print message nor increment check_errors */ |
| 1835 | continue; |
| 1836 | } |
| 1837 | if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) { |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1838 | fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d " |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 1839 | "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n", |
Max Reitz | 3cce51c | 2018-05-09 22:00:58 +0200 | [diff] [blame] | 1840 | repair ? "Repairing" : "ERROR", i, l1_entry, refcount); |
| 1841 | if (repair) { |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1842 | 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 Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 1856 | ret = bdrv_pread(bs->file, l2_offset, l2_table, |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1857 | 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 Blake | 3ef9521 | 2017-05-06 19:05:45 -0500 | [diff] [blame] | 1868 | QCow2ClusterType cluster_type = qcow2_get_cluster_type(l2_entry); |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1869 | |
Eric Blake | fdfab37 | 2017-05-06 19:05:46 -0500 | [diff] [blame] | 1870 | if (cluster_type == QCOW2_CLUSTER_NORMAL || |
| 1871 | cluster_type == QCOW2_CLUSTER_ZERO_ALLOC) { |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 1872 | ret = qcow2_get_refcount(bs, |
| 1873 | data_offset >> s->cluster_bits, |
| 1874 | &refcount); |
| 1875 | if (ret < 0) { |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1876 | /* don't print message nor increment check_errors */ |
| 1877 | continue; |
| 1878 | } |
| 1879 | if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) { |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1880 | fprintf(stderr, "%s OFLAG_COPIED data cluster: " |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 1881 | "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n", |
Max Reitz | 3cce51c | 2018-05-09 22:00:58 +0200 | [diff] [blame] | 1882 | repair ? "Repairing" : "ERROR", l2_entry, refcount); |
| 1883 | if (repair) { |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1884 | 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 Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | } |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1895 | |
| 1896 | if (l2_dirty) { |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 1897 | ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, |
| 1898 | l2_offset, s->cluster_size); |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1899 | 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 Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 1906 | ret = bdrv_pwrite(bs->file, l2_offset, l2_table, |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 1907 | s->cluster_size); |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 1908 | 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 Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | ret = 0; |
| 1918 | |
| 1919 | fail: |
| 1920 | qemu_vfree(l2_table); |
| 1921 | return ret; |
| 1922 | } |
| 1923 | |
| 1924 | /* |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 1925 | * Checks consistency of refblocks and accounts for each refblock in |
| 1926 | * *refcount_table. |
| 1927 | */ |
| 1928 | static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res, |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 1929 | BdrvCheckMode fix, bool *rebuild, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 1930 | void **refcount_table, int64_t *nb_clusters) |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 1931 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 1932 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1933 | int64_t i, size; |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 1934 | int ret; |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 1935 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1936 | for(i = 0; i < s->refcount_table_size; i++) { |
Kevin Wolf | 6882c8f | 2010-06-22 12:31:45 +0200 | [diff] [blame] | 1937 | uint64_t offset, cluster; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 1938 | offset = s->refcount_table[i]; |
Kevin Wolf | 6882c8f | 2010-06-22 12:31:45 +0200 | [diff] [blame] | 1939 | cluster = offset >> s->cluster_bits; |
Kevin Wolf | 746c3cb | 2010-02-23 16:40:54 +0100 | [diff] [blame] | 1940 | |
| 1941 | /* Refcount blocks are cluster aligned */ |
Hu Tao | ac95acd | 2013-12-05 14:32:34 +0800 | [diff] [blame] | 1942 | if (offset_into_cluster(s, offset)) { |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 1943 | fprintf(stderr, "ERROR refcount block %" PRId64 " is not " |
Kevin Wolf | 746c3cb | 2010-02-23 16:40:54 +0100 | [diff] [blame] | 1944 | "cluster aligned; refcount table entry corrupted\n", i); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 1945 | res->corruptions++; |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 1946 | *rebuild = true; |
Kevin Wolf | 6882c8f | 2010-06-22 12:31:45 +0200 | [diff] [blame] | 1947 | continue; |
| 1948 | } |
| 1949 | |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 1950 | if (cluster >= *nb_clusters) { |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1951 | 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 Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1955 | int64_t new_nb_clusters; |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 1956 | Error *local_err = NULL; |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1957 | |
| 1958 | if (offset > INT64_MAX - s->cluster_size) { |
| 1959 | ret = -EINVAL; |
| 1960 | goto resize_fail; |
| 1961 | } |
| 1962 | |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 1963 | ret = bdrv_truncate(bs->file, offset + s->cluster_size, |
Max Reitz | 7ea37c3 | 2017-06-13 22:20:53 +0200 | [diff] [blame] | 1964 | PREALLOC_MODE_OFF, &local_err); |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1965 | if (ret < 0) { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 1966 | error_report_err(local_err); |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1967 | goto resize_fail; |
| 1968 | } |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 1969 | size = bdrv_getlength(bs->file->bs); |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1970 | if (size < 0) { |
| 1971 | ret = size; |
| 1972 | goto resize_fail; |
| 1973 | } |
| 1974 | |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1975 | new_nb_clusters = size_to_clusters(s, size); |
| 1976 | assert(new_nb_clusters >= *nb_clusters); |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1977 | |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1978 | ret = realloc_refcount_array(s, refcount_table, |
| 1979 | nb_clusters, new_nb_clusters); |
| 1980 | if (ret < 0) { |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1981 | res->check_errors++; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 1982 | return ret; |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1983 | } |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1984 | |
| 1985 | if (cluster >= *nb_clusters) { |
| 1986 | ret = -EINVAL; |
| 1987 | goto resize_fail; |
| 1988 | } |
| 1989 | |
| 1990 | res->corruptions_fixed++; |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1991 | ret = qcow2_inc_refcounts_imrt(bs, res, |
| 1992 | refcount_table, nb_clusters, |
| 1993 | offset, s->cluster_size); |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 1994 | 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-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 1999 | * exactly 1 after qcow2_inc_refcounts_imrt() */ |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 2000 | continue; |
| 2001 | |
| 2002 | resize_fail: |
| 2003 | res->corruptions++; |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2004 | *rebuild = true; |
Max Reitz | 001c158 | 2014-10-22 14:09:38 +0200 | [diff] [blame] | 2005 | fprintf(stderr, "ERROR could not resize image: %s\n", |
| 2006 | strerror(-ret)); |
| 2007 | } else { |
| 2008 | res->corruptions++; |
| 2009 | } |
Kevin Wolf | 6882c8f | 2010-06-22 12:31:45 +0200 | [diff] [blame] | 2010 | continue; |
Kevin Wolf | 746c3cb | 2010-02-23 16:40:54 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2013 | if (offset != 0) { |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 2014 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters, |
| 2015 | offset, s->cluster_size); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 2016 | if (ret < 0) { |
| 2017 | return ret; |
| 2018 | } |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2019 | if (s->get_refcount(*refcount_table, cluster) != 1) { |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2020 | fprintf(stderr, "ERROR refcount block %" PRId64 |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2021 | " refcount=%" PRIu64 "\n", i, |
| 2022 | s->get_refcount(*refcount_table, cluster)); |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2023 | res->corruptions++; |
| 2024 | *rebuild = true; |
Kevin Wolf | 746c3cb | 2010-02-23 16:40:54 +0100 | [diff] [blame] | 2025 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2026 | } |
| 2027 | } |
| 2028 | |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | /* |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2033 | * Calculates an in-memory refcount table. |
| 2034 | */ |
| 2035 | static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2036 | BdrvCheckMode fix, bool *rebuild, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2037 | void **refcount_table, int64_t *nb_clusters) |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2038 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2039 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2040 | int64_t i; |
| 2041 | QCowSnapshot *sn; |
| 2042 | int ret; |
| 2043 | |
Max Reitz | 9696df2 | 2014-10-22 14:09:37 +0200 | [diff] [blame] | 2044 | if (!*refcount_table) { |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 2045 | int64_t old_size = 0; |
| 2046 | ret = realloc_refcount_array(s, refcount_table, |
| 2047 | &old_size, *nb_clusters); |
| 2048 | if (ret < 0) { |
Max Reitz | 9696df2 | 2014-10-22 14:09:37 +0200 | [diff] [blame] | 2049 | res->check_errors++; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 2050 | return ret; |
Max Reitz | 9696df2 | 2014-10-22 14:09:37 +0200 | [diff] [blame] | 2051 | } |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* header */ |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 2055 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters, |
| 2056 | 0, s->cluster_size); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 2057 | if (ret < 0) { |
| 2058 | return ret; |
| 2059 | } |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2060 | |
| 2061 | /* current L1 table */ |
Max Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 2062 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
Max Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 2063 | s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO, |
| 2064 | fix); |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2065 | 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 Garcia | 0c2ada8 | 2018-03-06 18:14:12 +0200 | [diff] [blame] | 2072 | 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 Reitz | 641bb63 | 2014-10-22 14:09:36 +0200 | [diff] [blame] | 2086 | ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, |
Max Reitz | ac5b787 | 2017-11-10 21:37:59 +0100 | [diff] [blame] | 2087 | sn->l1_table_offset, sn->l1_size, 0, fix); |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2088 | if (ret < 0) { |
| 2089 | return ret; |
| 2090 | } |
| 2091 | } |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 2092 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters, |
| 2093 | s->snapshots_offset, s->snapshots_size); |
Max Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 2094 | if (ret < 0) { |
| 2095 | return ret; |
| 2096 | } |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2097 | |
| 2098 | /* refcount data */ |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 2099 | 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 Reitz | fef4d3d | 2014-10-22 14:09:35 +0200 | [diff] [blame] | 2102 | if (ret < 0) { |
| 2103 | return ret; |
| 2104 | } |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2105 | |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 2106 | /* encryption */ |
| 2107 | if (s->crypto_header.length) { |
Vladimir Sementsov-Ogievskiy | 8a5bb1f | 2017-06-28 15:05:07 +0300 | [diff] [blame] | 2108 | ret = qcow2_inc_refcounts_imrt(bs, res, refcount_table, nb_clusters, |
| 2109 | s->crypto_header.offset, |
| 2110 | s->crypto_header.length); |
Daniel P. Berrange | 4652b8f | 2017-06-23 17:24:12 +0100 | [diff] [blame] | 2111 | if (ret < 0) { |
| 2112 | return ret; |
| 2113 | } |
| 2114 | } |
| 2115 | |
Vladimir Sementsov-Ogievskiy | 88ddffa | 2017-06-28 15:05:08 +0300 | [diff] [blame] | 2116 | /* bitmaps */ |
| 2117 | ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clusters); |
| 2118 | if (ret < 0) { |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2122 | return check_refblocks(bs, res, fix, rebuild, refcount_table, nb_clusters); |
Max Reitz | 057a3fe | 2014-10-22 14:09:32 +0200 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2126 | * Compares the actual reference count for each cluster in the image against the |
| 2127 | * refcount as reported by the refcount structures on-disk. |
| 2128 | */ |
| 2129 | static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2130 | BdrvCheckMode fix, bool *rebuild, |
| 2131 | int64_t *highest_cluster, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2132 | void *refcount_table, int64_t nb_clusters) |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2133 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2134 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2135 | int64_t i; |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 2136 | uint64_t refcount1, refcount2; |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 2137 | int ret; |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2138 | |
| 2139 | for (i = 0, *highest_cluster = 0; i < nb_clusters; i++) { |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 2140 | ret = qcow2_get_refcount(bs, i, &refcount1); |
| 2141 | if (ret < 0) { |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2142 | fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n", |
Max Reitz | 7324c10 | 2015-02-10 15:28:46 -0500 | [diff] [blame] | 2143 | i, strerror(-ret)); |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 2144 | res->check_errors++; |
Kevin Wolf | f74550f | 2010-06-22 12:35:07 +0200 | [diff] [blame] | 2145 | continue; |
Kevin Wolf | 018faaf | 2010-06-04 11:16:11 +0200 | [diff] [blame] | 2146 | } |
| 2147 | |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2148 | refcount2 = s->get_refcount(refcount_table, i); |
Federico Simoncelli | c6bb9ad | 2013-01-28 06:59:46 -0500 | [diff] [blame] | 2149 | |
| 2150 | if (refcount1 > 0 || refcount2 > 0) { |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2151 | *highest_cluster = i; |
Federico Simoncelli | c6bb9ad | 2013-01-28 06:59:46 -0500 | [diff] [blame] | 2152 | } |
| 2153 | |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2154 | if (refcount1 != refcount2) { |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2155 | /* Check if we're allowed to fix the mismatch */ |
| 2156 | int *num_fixed = NULL; |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2157 | if (refcount1 == 0) { |
| 2158 | *rebuild = true; |
| 2159 | } else if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) { |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2160 | num_fixed = &res->leaks_fixed; |
| 2161 | } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) { |
| 2162 | num_fixed = &res->corruptions_fixed; |
| 2163 | } |
| 2164 | |
Max Reitz | 0e06528 | 2015-02-10 15:28:48 -0500 | [diff] [blame] | 2165 | fprintf(stderr, "%s cluster %" PRId64 " refcount=%" PRIu64 |
| 2166 | " reference=%" PRIu64 "\n", |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2167 | num_fixed != NULL ? "Repairing" : |
| 2168 | refcount1 < refcount2 ? "ERROR" : |
| 2169 | "Leaked", |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2170 | i, refcount1, refcount2); |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2171 | |
| 2172 | if (num_fixed) { |
| 2173 | ret = update_refcount(bs, i << s->cluster_bits, 1, |
Max Reitz | 2aabe7c | 2015-02-10 15:28:47 -0500 | [diff] [blame] | 2174 | refcount_diff(refcount1, refcount2), |
| 2175 | refcount1 > refcount2, |
Kevin Wolf | 6cfcb9b | 2013-06-19 13:44:18 +0200 | [diff] [blame] | 2176 | QCOW2_DISCARD_ALWAYS); |
Kevin Wolf | 166acf5 | 2012-05-11 18:18:36 +0200 | [diff] [blame] | 2177 | if (ret >= 0) { |
| 2178 | (*num_fixed)++; |
| 2179 | continue; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | /* And if we couldn't, print an error */ |
Kevin Wolf | 9ac228e | 2010-06-29 12:37:54 +0200 | [diff] [blame] | 2184 | if (refcount1 < refcount2) { |
| 2185 | res->corruptions++; |
| 2186 | } else { |
| 2187 | res->leaks++; |
| 2188 | } |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2189 | } |
| 2190 | } |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | /* |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2194 | * 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 | */ |
| 2205 | static int64_t alloc_clusters_imrt(BlockDriverState *bs, |
| 2206 | int cluster_count, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2207 | void **refcount_table, |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2208 | int64_t *imrt_nb_clusters, |
| 2209 | int64_t *first_free_cluster) |
| 2210 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2211 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2212 | int64_t cluster = *first_free_cluster, i; |
| 2213 | bool first_gap = true; |
| 2214 | int contiguous_free_clusters; |
Max Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 2215 | int ret; |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2216 | |
| 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2224 | if (!s->get_refcount(*refcount_table, cluster)) { |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2225 | 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 Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2245 | /* 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 Reitz | 5fee192 | 2015-02-10 15:28:49 -0500 | [diff] [blame] | 2251 | 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 Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2256 | } |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* Go back to the first free cluster */ |
| 2260 | cluster -= contiguous_free_clusters; |
| 2261 | for (i = 0; i < cluster_count; i++) { |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2262 | s->set_refcount(*refcount_table, cluster + i, 1); |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2263 | } |
| 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 | */ |
| 2276 | static int rebuild_refcount_structure(BlockDriverState *bs, |
| 2277 | BdrvCheckResult *res, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2278 | void **refcount_table, |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2279 | int64_t *nb_clusters) |
| 2280 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2281 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2282 | 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2286 | void *on_disk_refblock; |
| 2287 | int ret = 0; |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2288 | 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 | |
| 2295 | write_refblocks: |
| 2296 | for (; cluster < *nb_clusters; cluster++) { |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2297 | if (!s->get_refcount(*refcount_table, cluster)) { |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2298 | 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é | f80ac75 | 2017-07-26 23:42:10 -0300 | [diff] [blame] | 2341 | } else { |
| 2342 | assert(on_disk_reftable); |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2343 | } |
| 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2372 | /* 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 Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2376 | |
Kevin Wolf | 18d51c4 | 2016-05-31 14:42:08 +0200 | [diff] [blame] | 2377 | ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE, |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2378 | on_disk_refblock, s->cluster_sectors); |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2379 | 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 Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2412 | 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 Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 2424 | ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable, |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2425 | 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 Maydell | f1f7a1d | 2016-06-16 17:06:17 +0100 | [diff] [blame] | 2432 | 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 Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 2435 | ret = bdrv_pwrite_sync(bs->file, |
| 2436 | offsetof(QCowHeader, refcount_table_offset), |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2437 | &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 Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 2450 | update_max_refcount_table_index(s); |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2451 | |
| 2452 | return 0; |
| 2453 | |
| 2454 | fail: |
| 2455 | g_free(on_disk_reftable); |
| 2456 | return ret; |
| 2457 | } |
| 2458 | |
| 2459 | /* |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2460 | * 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 | */ |
| 2465 | int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, |
| 2466 | BdrvCheckMode fix) |
| 2467 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2468 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2469 | BdrvCheckResult pre_compare_res; |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2470 | int64_t size, highest_cluster, nb_clusters; |
Max Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2471 | void *refcount_table = NULL; |
Max Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2472 | bool rebuild = false; |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2473 | int ret; |
| 2474 | |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 2475 | size = bdrv_getlength(bs->file->bs); |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2476 | 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 Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2490 | ret = calculate_refcounts(bs, res, fix, &rebuild, &refcount_table, |
| 2491 | &nb_clusters); |
Max Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2492 | if (ret < 0) { |
| 2493 | goto fail; |
| 2494 | } |
| 2495 | |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2496 | /* 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 Reitz | 6ca56bf | 2014-10-22 14:09:30 +0200 | [diff] [blame] | 2501 | nb_clusters); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2502 | |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2503 | if (rebuild && (fix & BDRV_FIX_ERRORS)) { |
Max Reitz | 791230d | 2014-10-22 14:09:41 +0200 | [diff] [blame] | 2504 | BdrvCheckResult old_res = *res; |
| 2505 | int fresh_leaks = 0; |
| 2506 | |
Max Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2507 | 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 Reitz | 791230d | 2014-10-22 14:09:41 +0200 | [diff] [blame] | 2513 | |
| 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 Reitz | 7453c96 | 2015-02-10 15:28:50 -0500 | [diff] [blame] | 2520 | memset(refcount_table, 0, refcount_array_byte_size(s, nb_clusters)); |
Max Reitz | 791230d | 2014-10-22 14:09:41 +0200 | [diff] [blame] | 2521 | 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 Reitz | c7c0681 | 2014-10-22 14:09:40 +0200 | [diff] [blame] | 2555 | } 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 Reitz | f307b25 | 2014-10-22 14:09:39 +0200 | [diff] [blame] | 2568 | } |
| 2569 | |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 2570 | /* check OFLAG_COPIED */ |
Max Reitz | e23e400 | 2013-08-30 14:34:28 +0200 | [diff] [blame] | 2571 | ret = check_oflag_copied(bs, res, fix); |
Max Reitz | 4f6ed88 | 2013-08-30 14:34:27 +0200 | [diff] [blame] | 2572 | if (ret < 0) { |
| 2573 | goto fail; |
| 2574 | } |
| 2575 | |
Federico Simoncelli | c6bb9ad | 2013-01-28 06:59:46 -0500 | [diff] [blame] | 2576 | res->image_end_offset = (highest_cluster + 1) * s->cluster_size; |
Kevin Wolf | 80fa334 | 2011-06-01 10:50:00 +0200 | [diff] [blame] | 2577 | ret = 0; |
| 2578 | |
| 2579 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2580 | g_free(refcount_table); |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2581 | |
Kevin Wolf | 80fa334 | 2011-06-01 10:50:00 +0200 | [diff] [blame] | 2582 | return ret; |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2585 | #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 Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 2593 | * The ign parameter specifies what checks not to perform (being a bitmask of |
| 2594 | * QCow2MetadataOverlap values), i.e., what sections to ignore. |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2595 | * |
| 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 Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 2602 | int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2603 | int64_t size) |
| 2604 | { |
Kevin Wolf | ff99129 | 2015-09-07 17:12:56 +0200 | [diff] [blame] | 2605 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 3e35539 | 2013-10-10 11:09:24 +0200 | [diff] [blame] | 2606 | int chk = s->overlap_check & ~ign; |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2607 | 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 Garcia | 9e02968 | 2018-02-15 15:10:08 +0200 | [diff] [blame] | 2620 | size = ROUND_UP(offset_into_cluster(s, offset) + size, s->cluster_size); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2621 | 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 Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 2663 | 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 Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2668 | 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 Reitz | 998b959 | 2013-10-09 10:42:56 +0200 | [diff] [blame] | 2680 | uint64_t l1_sz2 = l1_sz * sizeof(uint64_t); |
Alberto Garcia | c7a9d81 | 2018-03-06 18:14:09 +0200 | [diff] [blame] | 2681 | uint64_t *l1; |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2682 | int ret; |
| 2683 | |
Alberto Garcia | c7a9d81 | 2018-03-06 18:14:09 +0200 | [diff] [blame] | 2684 | 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 Wolf | de82815 | 2014-05-20 17:12:47 +0200 | [diff] [blame] | 2692 | if (l1_sz2 && l1 == NULL) { |
| 2693 | return -ENOMEM; |
| 2694 | } |
| 2695 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 2696 | ret = bdrv_pread(bs->file, l1_ofs, l1, l1_sz2); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2697 | if (ret < 0) { |
| 2698 | g_free(l1); |
| 2699 | return ret; |
| 2700 | } |
| 2701 | |
| 2702 | for (j = 0; j < l1_sz; j++) { |
Max Reitz | 1e242b5 | 2013-09-30 08:59:28 +0200 | [diff] [blame] | 2703 | uint64_t l2_ofs = be64_to_cpu(l1[j]) & L1E_OFFSET_MASK; |
| 2704 | if (l2_ofs && overlaps_with(l2_ofs, s->cluster_size)) { |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2705 | g_free(l1); |
| 2706 | return QCOW2_OL_INACTIVE_L2; |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | g_free(l1); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Vladimir Sementsov-Ogievskiy | 0e4e431 | 2018-07-05 18:15:15 +0300 | [diff] [blame] | 2714 | 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 Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2724 | return 0; |
| 2725 | } |
| 2726 | |
| 2727 | static const char *metadata_ol_names[] = { |
Liam Merwick | 7cb6d3c | 2018-11-05 21:38:39 +0000 | [diff] [blame] | 2728 | [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 Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2737 | }; |
Liam Merwick | 7cb6d3c | 2018-11-05 21:38:39 +0000 | [diff] [blame] | 2738 | QEMU_BUILD_BUG_ON(QCOW2_OL_MAX_BITNR != ARRAY_SIZE(metadata_ol_names)); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2739 | |
| 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 Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 2750 | int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset, |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2751 | int64_t size) |
| 2752 | { |
Max Reitz | 231bb26 | 2013-10-10 11:09:23 +0200 | [diff] [blame] | 2753 | int ret = qcow2_check_metadata_overlap(bs, ign, offset, size); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2754 | |
| 2755 | if (ret < 0) { |
| 2756 | return ret; |
| 2757 | } else if (ret > 0) { |
Stefan Hajnoczi | 786a4ea | 2015-03-23 15:29:26 +0000 | [diff] [blame] | 2758 | int metadata_ol_bitnr = ctz32(ret); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2759 | assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR); |
| 2760 | |
Max Reitz | adb4355 | 2014-09-05 16:07:17 +0200 | [diff] [blame] | 2761 | qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid " |
| 2762 | "write on metadata (overlaps with %s)", |
| 2763 | metadata_ol_names[metadata_ol_bitnr]); |
Max Reitz | a40f1c2 | 2013-08-30 14:34:25 +0200 | [diff] [blame] | 2764 | return -EIO; |
| 2765 | } |
| 2766 | |
| 2767 | return 0; |
| 2768 | } |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 2769 | |
| 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 | */ |
| 2780 | typedef 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 | */ |
| 2790 | static 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 | */ |
| 2843 | static 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 Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 2861 | ret = bdrv_pwrite(bs->file, offset, refblock, s->cluster_size); |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 2862 | 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 | */ |
| 2886 | static 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 Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 2944 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 2945 | 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 Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 2957 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 2958 | |
| 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 Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 2978 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 2979 | } 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 | |
| 3033 | int 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 Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 3131 | ret = bdrv_pwrite(bs->file, new_reftable_offset, new_reftable, |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 3132 | 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 Garcia | 7061a07 | 2017-02-01 14:38:28 +0200 | [diff] [blame] | 3175 | update_max_refcount_table_index(s); |
Max Reitz | 791c9a0 | 2015-07-27 17:51:37 +0200 | [diff] [blame] | 3176 | |
| 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 | |
| 3193 | done: |
| 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 Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3217 | |
Max Reitz | 23482f8 | 2017-11-10 21:31:10 +0100 | [diff] [blame] | 3218 | static 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 Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3237 | static int qcow2_discard_refcount_block(BlockDriverState *bs, |
| 3238 | uint64_t discard_block_offs) |
| 3239 | { |
| 3240 | BDRVQcow2State *s = bs->opaque; |
Max Reitz | 23482f8 | 2017-11-10 21:31:10 +0100 | [diff] [blame] | 3241 | int64_t refblock_offs; |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3242 | 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 Reitz | 23482f8 | 2017-11-10 21:31:10 +0100 | [diff] [blame] | 3247 | refblock_offs = get_refblock_offset(bs, discard_block_offs); |
| 3248 | if (refblock_offs < 0) { |
| 3249 | return refblock_offs; |
| 3250 | } |
| 3251 | |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3252 | 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 Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 3270 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3271 | return -EINVAL; |
| 3272 | } |
| 3273 | s->set_refcount(refblock, block_index, 0); |
| 3274 | |
Alberto Garcia | 2d135ee | 2018-02-05 16:33:06 +0200 | [diff] [blame] | 3275 | qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refblock); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3276 | |
Alberto Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 3277 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3278 | |
| 3279 | if (cluster_index < s->free_cluster_index) { |
| 3280 | s->free_cluster_index = cluster_index; |
| 3281 | } |
| 3282 | |
Alberto Garcia | 6e6fa76 | 2018-02-05 16:33:11 +0200 | [diff] [blame] | 3283 | refblock = qcow2_cache_is_table_offset(s->refcount_block_cache, |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3284 | discard_block_offs); |
| 3285 | if (refblock) { |
| 3286 | /* discard refblock from the cache if refblock is cached */ |
Alberto Garcia | 77aadd7 | 2018-02-05 16:33:10 +0200 | [diff] [blame] | 3287 | qcow2_cache_discard(s->refcount_block_cache, refblock); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3288 | } |
| 3289 | update_refcount_discard(bs, discard_block_offs, s->cluster_size); |
| 3290 | |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
| 3294 | int 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 Garcia | 2013c3d | 2018-02-05 16:33:07 +0200 | [diff] [blame] | 3330 | qcow2_cache_put(s->refcount_block_cache, &refblock); |
Pavel Butsykin | 46b732c | 2017-09-18 15:42:29 +0300 | [diff] [blame] | 3331 | |
| 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 | |
| 3356 | out: |
| 3357 | g_free(reftable_tmp); |
| 3358 | return ret; |
| 3359 | } |
Pavel Butsykin | 163bc39 | 2017-09-29 15:16:13 +0300 | [diff] [blame] | 3360 | |
| 3361 | int64_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 | } |