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