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