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