]>
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 | ||
25 | #include <zlib.h> | |
26 | ||
27 | #include "qemu-common.h" | |
737e150e | 28 | #include "block/block_int.h" |
45aba42f | 29 | #include "block/qcow2.h" |
3cce16f4 | 30 | #include "trace.h" |
45aba42f | 31 | |
2cf7cfa1 KW |
32 | int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, |
33 | bool exact_size) | |
45aba42f KW |
34 | { |
35 | BDRVQcowState *s = bs->opaque; | |
2cf7cfa1 | 36 | int new_l1_size2, ret, i; |
45aba42f | 37 | uint64_t *new_l1_table; |
2cf7cfa1 | 38 | int64_t new_l1_table_offset, new_l1_size; |
45aba42f KW |
39 | uint8_t data[12]; |
40 | ||
72893756 | 41 | if (min_size <= s->l1_size) |
45aba42f | 42 | return 0; |
72893756 SH |
43 | |
44 | if (exact_size) { | |
45 | new_l1_size = min_size; | |
46 | } else { | |
47 | /* Bump size up to reduce the number of times we have to grow */ | |
48 | new_l1_size = s->l1_size; | |
49 | if (new_l1_size == 0) { | |
50 | new_l1_size = 1; | |
51 | } | |
52 | while (min_size > new_l1_size) { | |
53 | new_l1_size = (new_l1_size * 3 + 1) / 2; | |
54 | } | |
45aba42f | 55 | } |
72893756 | 56 | |
2cf7cfa1 KW |
57 | if (new_l1_size > INT_MAX) { |
58 | return -EFBIG; | |
59 | } | |
60 | ||
45aba42f | 61 | #ifdef DEBUG_ALLOC2 |
2cf7cfa1 KW |
62 | fprintf(stderr, "grow l1_table from %d to %" PRId64 "\n", |
63 | s->l1_size, new_l1_size); | |
45aba42f KW |
64 | #endif |
65 | ||
66 | new_l1_size2 = sizeof(uint64_t) * new_l1_size; | |
7267c094 | 67 | new_l1_table = g_malloc0(align_offset(new_l1_size2, 512)); |
45aba42f KW |
68 | memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t)); |
69 | ||
70 | /* write new table (align to cluster) */ | |
66f82cee | 71 | BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ALLOC_TABLE); |
ed6ccf0f | 72 | new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2); |
5d757b56 | 73 | if (new_l1_table_offset < 0) { |
7267c094 | 74 | g_free(new_l1_table); |
5d757b56 KW |
75 | return new_l1_table_offset; |
76 | } | |
29c1a730 KW |
77 | |
78 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
79 | if (ret < 0) { | |
80fa3341 | 80 | goto fail; |
29c1a730 | 81 | } |
45aba42f | 82 | |
66f82cee | 83 | BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE); |
45aba42f KW |
84 | for(i = 0; i < s->l1_size; i++) |
85 | new_l1_table[i] = cpu_to_be64(new_l1_table[i]); | |
8b3b7206 KW |
86 | ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2); |
87 | if (ret < 0) | |
45aba42f KW |
88 | goto fail; |
89 | for(i = 0; i < s->l1_size; i++) | |
90 | new_l1_table[i] = be64_to_cpu(new_l1_table[i]); | |
91 | ||
92 | /* set new table */ | |
66f82cee | 93 | BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE); |
45aba42f | 94 | cpu_to_be32w((uint32_t*)data, new_l1_size); |
653df36b | 95 | cpu_to_be64wu((uint64_t*)(data + 4), new_l1_table_offset); |
8b3b7206 KW |
96 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data)); |
97 | if (ret < 0) { | |
45aba42f | 98 | goto fail; |
fb8fa77c | 99 | } |
7267c094 | 100 | g_free(s->l1_table); |
ed6ccf0f | 101 | qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t)); |
45aba42f KW |
102 | s->l1_table_offset = new_l1_table_offset; |
103 | s->l1_table = new_l1_table; | |
104 | s->l1_size = new_l1_size; | |
105 | return 0; | |
106 | fail: | |
7267c094 | 107 | g_free(new_l1_table); |
fb8fa77c | 108 | qcow2_free_clusters(bs, new_l1_table_offset, new_l1_size2); |
8b3b7206 | 109 | return ret; |
45aba42f KW |
110 | } |
111 | ||
45aba42f KW |
112 | /* |
113 | * l2_load | |
114 | * | |
115 | * Loads a L2 table into memory. If the table is in the cache, the cache | |
116 | * is used; otherwise the L2 table is loaded from the image file. | |
117 | * | |
118 | * Returns a pointer to the L2 table on success, or NULL if the read from | |
119 | * the image file failed. | |
120 | */ | |
121 | ||
55c17e98 KW |
122 | static int l2_load(BlockDriverState *bs, uint64_t l2_offset, |
123 | uint64_t **l2_table) | |
45aba42f KW |
124 | { |
125 | BDRVQcowState *s = bs->opaque; | |
55c17e98 | 126 | int ret; |
45aba42f | 127 | |
29c1a730 | 128 | ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset, (void**) l2_table); |
45aba42f | 129 | |
29c1a730 | 130 | return ret; |
45aba42f KW |
131 | } |
132 | ||
6583e3c7 KW |
133 | /* |
134 | * Writes one sector of the L1 table to the disk (can't update single entries | |
135 | * and we really don't want bdrv_pread to perform a read-modify-write) | |
136 | */ | |
137 | #define L1_ENTRIES_PER_SECTOR (512 / 8) | |
66f82cee | 138 | static int write_l1_entry(BlockDriverState *bs, int l1_index) |
6583e3c7 | 139 | { |
66f82cee | 140 | BDRVQcowState *s = bs->opaque; |
6583e3c7 KW |
141 | uint64_t buf[L1_ENTRIES_PER_SECTOR]; |
142 | int l1_start_index; | |
f7defcb6 | 143 | int i, ret; |
6583e3c7 KW |
144 | |
145 | l1_start_index = l1_index & ~(L1_ENTRIES_PER_SECTOR - 1); | |
146 | for (i = 0; i < L1_ENTRIES_PER_SECTOR; i++) { | |
147 | buf[i] = cpu_to_be64(s->l1_table[l1_start_index + i]); | |
148 | } | |
149 | ||
66f82cee | 150 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); |
8b3b7206 | 151 | ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index, |
f7defcb6 KW |
152 | buf, sizeof(buf)); |
153 | if (ret < 0) { | |
154 | return ret; | |
6583e3c7 KW |
155 | } |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
45aba42f KW |
160 | /* |
161 | * l2_allocate | |
162 | * | |
163 | * Allocate a new l2 entry in the file. If l1_index points to an already | |
164 | * used entry in the L2 table (i.e. we are doing a copy on write for the L2 | |
165 | * table) copy the contents of the old L2 table into the newly allocated one. | |
166 | * Otherwise the new table is initialized with zeros. | |
167 | * | |
168 | */ | |
169 | ||
c46e1167 | 170 | static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) |
45aba42f KW |
171 | { |
172 | BDRVQcowState *s = bs->opaque; | |
6583e3c7 | 173 | uint64_t old_l2_offset; |
f4f0d391 KW |
174 | uint64_t *l2_table; |
175 | int64_t l2_offset; | |
c46e1167 | 176 | int ret; |
45aba42f KW |
177 | |
178 | old_l2_offset = s->l1_table[l1_index]; | |
179 | ||
3cce16f4 KW |
180 | trace_qcow2_l2_allocate(bs, l1_index); |
181 | ||
45aba42f KW |
182 | /* allocate a new l2 entry */ |
183 | ||
ed6ccf0f | 184 | l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t)); |
5d757b56 | 185 | if (l2_offset < 0) { |
c46e1167 | 186 | return l2_offset; |
5d757b56 | 187 | } |
29c1a730 KW |
188 | |
189 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
190 | if (ret < 0) { | |
191 | goto fail; | |
192 | } | |
45aba42f | 193 | |
45aba42f KW |
194 | /* allocate a new entry in the l2 cache */ |
195 | ||
3cce16f4 | 196 | trace_qcow2_l2_allocate_get_empty(bs, l1_index); |
29c1a730 KW |
197 | ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table); |
198 | if (ret < 0) { | |
199 | return ret; | |
200 | } | |
201 | ||
202 | l2_table = *table; | |
45aba42f | 203 | |
8e37f681 | 204 | if ((old_l2_offset & L1E_OFFSET_MASK) == 0) { |
45aba42f KW |
205 | /* if there was no old l2 table, clear the new table */ |
206 | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); | |
207 | } else { | |
29c1a730 KW |
208 | uint64_t* old_table; |
209 | ||
45aba42f | 210 | /* if there was an old l2 table, read it from the disk */ |
66f82cee | 211 | BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ); |
8e37f681 KW |
212 | ret = qcow2_cache_get(bs, s->l2_table_cache, |
213 | old_l2_offset & L1E_OFFSET_MASK, | |
29c1a730 KW |
214 | (void**) &old_table); |
215 | if (ret < 0) { | |
216 | goto fail; | |
217 | } | |
218 | ||
219 | memcpy(l2_table, old_table, s->cluster_size); | |
220 | ||
221 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &old_table); | |
c46e1167 | 222 | if (ret < 0) { |
175e1152 | 223 | goto fail; |
c46e1167 | 224 | } |
45aba42f | 225 | } |
29c1a730 | 226 | |
45aba42f | 227 | /* write the l2 table to the file */ |
66f82cee | 228 | BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE); |
29c1a730 | 229 | |
3cce16f4 | 230 | trace_qcow2_l2_allocate_write_l2(bs, l1_index); |
29c1a730 KW |
231 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); |
232 | ret = qcow2_cache_flush(bs, s->l2_table_cache); | |
c46e1167 | 233 | if (ret < 0) { |
175e1152 KW |
234 | goto fail; |
235 | } | |
236 | ||
237 | /* update the L1 entry */ | |
3cce16f4 | 238 | trace_qcow2_l2_allocate_write_l1(bs, l1_index); |
175e1152 KW |
239 | s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED; |
240 | ret = write_l1_entry(bs, l1_index); | |
241 | if (ret < 0) { | |
242 | goto fail; | |
c46e1167 | 243 | } |
45aba42f | 244 | |
c46e1167 | 245 | *table = l2_table; |
3cce16f4 | 246 | trace_qcow2_l2_allocate_done(bs, l1_index, 0); |
c46e1167 | 247 | return 0; |
175e1152 KW |
248 | |
249 | fail: | |
3cce16f4 | 250 | trace_qcow2_l2_allocate_done(bs, l1_index, ret); |
29c1a730 | 251 | qcow2_cache_put(bs, s->l2_table_cache, (void**) table); |
68dba0bf | 252 | s->l1_table[l1_index] = old_l2_offset; |
175e1152 | 253 | return ret; |
45aba42f KW |
254 | } |
255 | ||
2bfcc4a0 KW |
256 | /* |
257 | * Checks how many clusters in a given L2 table are contiguous in the image | |
258 | * file. As soon as one of the flags in the bitmask stop_flags changes compared | |
259 | * to the first cluster, the search is stopped and the cluster is not counted | |
260 | * as contiguous. (This allows it, for example, to stop at the first compressed | |
261 | * cluster which may require a different handling) | |
262 | */ | |
45aba42f | 263 | static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, |
2bfcc4a0 | 264 | uint64_t *l2_table, uint64_t start, uint64_t stop_flags) |
45aba42f KW |
265 | { |
266 | int i; | |
2bfcc4a0 KW |
267 | uint64_t mask = stop_flags | L2E_OFFSET_MASK; |
268 | uint64_t offset = be64_to_cpu(l2_table[0]) & mask; | |
45aba42f KW |
269 | |
270 | if (!offset) | |
271 | return 0; | |
272 | ||
2bfcc4a0 KW |
273 | for (i = start; i < start + nb_clusters; i++) { |
274 | uint64_t l2_entry = be64_to_cpu(l2_table[i]) & mask; | |
275 | if (offset + (uint64_t) i * cluster_size != l2_entry) { | |
45aba42f | 276 | break; |
2bfcc4a0 KW |
277 | } |
278 | } | |
45aba42f KW |
279 | |
280 | return (i - start); | |
281 | } | |
282 | ||
283 | static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) | |
284 | { | |
2bfcc4a0 KW |
285 | int i; |
286 | ||
287 | for (i = 0; i < nb_clusters; i++) { | |
288 | int type = qcow2_get_cluster_type(be64_to_cpu(l2_table[i])); | |
45aba42f | 289 | |
2bfcc4a0 KW |
290 | if (type != QCOW2_CLUSTER_UNALLOCATED) { |
291 | break; | |
292 | } | |
293 | } | |
45aba42f KW |
294 | |
295 | return i; | |
296 | } | |
297 | ||
298 | /* The crypt function is compatible with the linux cryptoloop | |
299 | algorithm for < 4 GB images. NOTE: out_buf == in_buf is | |
300 | supported */ | |
ed6ccf0f KW |
301 | void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
302 | uint8_t *out_buf, const uint8_t *in_buf, | |
303 | int nb_sectors, int enc, | |
304 | const AES_KEY *key) | |
45aba42f KW |
305 | { |
306 | union { | |
307 | uint64_t ll[2]; | |
308 | uint8_t b[16]; | |
309 | } ivec; | |
310 | int i; | |
311 | ||
312 | for(i = 0; i < nb_sectors; i++) { | |
313 | ivec.ll[0] = cpu_to_le64(sector_num); | |
314 | ivec.ll[1] = 0; | |
315 | AES_cbc_encrypt(in_buf, out_buf, 512, key, | |
316 | ivec.b, enc); | |
317 | sector_num++; | |
318 | in_buf += 512; | |
319 | out_buf += 512; | |
320 | } | |
321 | } | |
322 | ||
aef4acb6 SH |
323 | static int coroutine_fn copy_sectors(BlockDriverState *bs, |
324 | uint64_t start_sect, | |
325 | uint64_t cluster_offset, | |
326 | int n_start, int n_end) | |
45aba42f KW |
327 | { |
328 | BDRVQcowState *s = bs->opaque; | |
aef4acb6 SH |
329 | QEMUIOVector qiov; |
330 | struct iovec iov; | |
45aba42f | 331 | int n, ret; |
1b9f1491 KW |
332 | |
333 | /* | |
334 | * If this is the last cluster and it is only partially used, we must only | |
335 | * copy until the end of the image, or bdrv_check_request will fail for the | |
336 | * bdrv_read/write calls below. | |
337 | */ | |
338 | if (start_sect + n_end > bs->total_sectors) { | |
339 | n_end = bs->total_sectors - start_sect; | |
340 | } | |
45aba42f KW |
341 | |
342 | n = n_end - n_start; | |
1b9f1491 | 343 | if (n <= 0) { |
45aba42f | 344 | return 0; |
1b9f1491 KW |
345 | } |
346 | ||
aef4acb6 SH |
347 | iov.iov_len = n * BDRV_SECTOR_SIZE; |
348 | iov.iov_base = qemu_blockalign(bs, iov.iov_len); | |
349 | ||
350 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1b9f1491 | 351 | |
66f82cee | 352 | BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); |
aef4acb6 SH |
353 | |
354 | /* Call .bdrv_co_readv() directly instead of using the public block-layer | |
355 | * interface. This avoids double I/O throttling and request tracking, | |
356 | * which can lead to deadlock when block layer copy-on-read is enabled. | |
357 | */ | |
358 | ret = bs->drv->bdrv_co_readv(bs, start_sect + n_start, n, &qiov); | |
1b9f1491 KW |
359 | if (ret < 0) { |
360 | goto out; | |
361 | } | |
362 | ||
45aba42f | 363 | if (s->crypt_method) { |
ed6ccf0f | 364 | qcow2_encrypt_sectors(s, start_sect + n_start, |
aef4acb6 | 365 | iov.iov_base, iov.iov_base, n, 1, |
45aba42f KW |
366 | &s->aes_encrypt_key); |
367 | } | |
1b9f1491 | 368 | |
66f82cee | 369 | BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); |
aef4acb6 | 370 | ret = bdrv_co_writev(bs->file, (cluster_offset >> 9) + n_start, n, &qiov); |
1b9f1491 KW |
371 | if (ret < 0) { |
372 | goto out; | |
373 | } | |
374 | ||
375 | ret = 0; | |
376 | out: | |
aef4acb6 | 377 | qemu_vfree(iov.iov_base); |
1b9f1491 | 378 | return ret; |
45aba42f KW |
379 | } |
380 | ||
381 | ||
382 | /* | |
383 | * get_cluster_offset | |
384 | * | |
1c46efaa KW |
385 | * For a given offset of the disk image, find the cluster offset in |
386 | * qcow2 file. The offset is stored in *cluster_offset. | |
45aba42f | 387 | * |
d57237f2 | 388 | * on entry, *num is the number of contiguous sectors we'd like to |
45aba42f KW |
389 | * access following offset. |
390 | * | |
d57237f2 | 391 | * on exit, *num is the number of contiguous sectors we can read. |
45aba42f | 392 | * |
68d000a3 KW |
393 | * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error |
394 | * cases. | |
45aba42f | 395 | */ |
1c46efaa KW |
396 | int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, |
397 | int *num, uint64_t *cluster_offset) | |
45aba42f KW |
398 | { |
399 | BDRVQcowState *s = bs->opaque; | |
2cf7cfa1 KW |
400 | unsigned int l2_index; |
401 | uint64_t l1_index, l2_offset, *l2_table; | |
45aba42f | 402 | int l1_bits, c; |
80ee15a6 KW |
403 | unsigned int index_in_cluster, nb_clusters; |
404 | uint64_t nb_available, nb_needed; | |
55c17e98 | 405 | int ret; |
45aba42f KW |
406 | |
407 | index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1); | |
408 | nb_needed = *num + index_in_cluster; | |
409 | ||
410 | l1_bits = s->l2_bits + s->cluster_bits; | |
411 | ||
412 | /* compute how many bytes there are between the offset and | |
413 | * the end of the l1 entry | |
414 | */ | |
415 | ||
80ee15a6 | 416 | nb_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1)); |
45aba42f KW |
417 | |
418 | /* compute the number of available sectors */ | |
419 | ||
420 | nb_available = (nb_available >> 9) + index_in_cluster; | |
421 | ||
422 | if (nb_needed > nb_available) { | |
423 | nb_needed = nb_available; | |
424 | } | |
425 | ||
1c46efaa | 426 | *cluster_offset = 0; |
45aba42f KW |
427 | |
428 | /* seek the the l2 offset in the l1 table */ | |
429 | ||
430 | l1_index = offset >> l1_bits; | |
68d000a3 KW |
431 | if (l1_index >= s->l1_size) { |
432 | ret = QCOW2_CLUSTER_UNALLOCATED; | |
45aba42f | 433 | goto out; |
68d000a3 | 434 | } |
45aba42f | 435 | |
68d000a3 KW |
436 | l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; |
437 | if (!l2_offset) { | |
438 | ret = QCOW2_CLUSTER_UNALLOCATED; | |
45aba42f | 439 | goto out; |
68d000a3 | 440 | } |
45aba42f KW |
441 | |
442 | /* load the l2 table in memory */ | |
443 | ||
55c17e98 KW |
444 | ret = l2_load(bs, l2_offset, &l2_table); |
445 | if (ret < 0) { | |
446 | return ret; | |
1c46efaa | 447 | } |
45aba42f KW |
448 | |
449 | /* find the cluster offset for the given disk offset */ | |
450 | ||
451 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); | |
1c46efaa | 452 | *cluster_offset = be64_to_cpu(l2_table[l2_index]); |
45aba42f KW |
453 | nb_clusters = size_to_clusters(s, nb_needed << 9); |
454 | ||
68d000a3 KW |
455 | ret = qcow2_get_cluster_type(*cluster_offset); |
456 | switch (ret) { | |
457 | case QCOW2_CLUSTER_COMPRESSED: | |
458 | /* Compressed clusters can only be processed one by one */ | |
459 | c = 1; | |
460 | *cluster_offset &= L2E_COMPRESSED_OFFSET_SIZE_MASK; | |
461 | break; | |
6377af48 | 462 | case QCOW2_CLUSTER_ZERO: |
381b487d PB |
463 | if (s->qcow_version < 3) { |
464 | return -EIO; | |
465 | } | |
6377af48 KW |
466 | c = count_contiguous_clusters(nb_clusters, s->cluster_size, |
467 | &l2_table[l2_index], 0, | |
468 | QCOW_OFLAG_COMPRESSED | QCOW_OFLAG_ZERO); | |
469 | *cluster_offset = 0; | |
470 | break; | |
68d000a3 | 471 | case QCOW2_CLUSTER_UNALLOCATED: |
45aba42f KW |
472 | /* how many empty clusters ? */ |
473 | c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]); | |
68d000a3 KW |
474 | *cluster_offset = 0; |
475 | break; | |
476 | case QCOW2_CLUSTER_NORMAL: | |
45aba42f KW |
477 | /* how many allocated clusters ? */ |
478 | c = count_contiguous_clusters(nb_clusters, s->cluster_size, | |
6377af48 KW |
479 | &l2_table[l2_index], 0, |
480 | QCOW_OFLAG_COMPRESSED | QCOW_OFLAG_ZERO); | |
68d000a3 KW |
481 | *cluster_offset &= L2E_OFFSET_MASK; |
482 | break; | |
1417d7e4 KW |
483 | default: |
484 | abort(); | |
45aba42f KW |
485 | } |
486 | ||
29c1a730 KW |
487 | qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
488 | ||
68d000a3 KW |
489 | nb_available = (c * s->cluster_sectors); |
490 | ||
45aba42f KW |
491 | out: |
492 | if (nb_available > nb_needed) | |
493 | nb_available = nb_needed; | |
494 | ||
495 | *num = nb_available - index_in_cluster; | |
496 | ||
68d000a3 | 497 | return ret; |
45aba42f KW |
498 | } |
499 | ||
500 | /* | |
501 | * get_cluster_table | |
502 | * | |
503 | * for a given disk offset, load (and allocate if needed) | |
504 | * the l2 table. | |
505 | * | |
506 | * the l2 table offset in the qcow2 file and the cluster index | |
507 | * in the l2 table are given to the caller. | |
508 | * | |
1e3e8f1a | 509 | * Returns 0 on success, -errno in failure case |
45aba42f | 510 | */ |
45aba42f KW |
511 | static int get_cluster_table(BlockDriverState *bs, uint64_t offset, |
512 | uint64_t **new_l2_table, | |
45aba42f KW |
513 | int *new_l2_index) |
514 | { | |
515 | BDRVQcowState *s = bs->opaque; | |
2cf7cfa1 KW |
516 | unsigned int l2_index; |
517 | uint64_t l1_index, l2_offset; | |
c46e1167 | 518 | uint64_t *l2_table = NULL; |
80ee15a6 | 519 | int ret; |
45aba42f KW |
520 | |
521 | /* seek the the l2 offset in the l1 table */ | |
522 | ||
523 | l1_index = offset >> (s->l2_bits + s->cluster_bits); | |
524 | if (l1_index >= s->l1_size) { | |
72893756 | 525 | ret = qcow2_grow_l1_table(bs, l1_index + 1, false); |
1e3e8f1a KW |
526 | if (ret < 0) { |
527 | return ret; | |
528 | } | |
45aba42f | 529 | } |
8e37f681 | 530 | |
2cf7cfa1 | 531 | assert(l1_index < s->l1_size); |
8e37f681 | 532 | l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK; |
45aba42f KW |
533 | |
534 | /* seek the l2 table of the given l2 offset */ | |
535 | ||
8e37f681 | 536 | if (s->l1_table[l1_index] & QCOW_OFLAG_COPIED) { |
45aba42f | 537 | /* load the l2 table in memory */ |
55c17e98 KW |
538 | ret = l2_load(bs, l2_offset, &l2_table); |
539 | if (ret < 0) { | |
540 | return ret; | |
1e3e8f1a | 541 | } |
45aba42f | 542 | } else { |
16fde5f2 | 543 | /* First allocate a new L2 table (and do COW if needed) */ |
c46e1167 KW |
544 | ret = l2_allocate(bs, l1_index, &l2_table); |
545 | if (ret < 0) { | |
546 | return ret; | |
1e3e8f1a | 547 | } |
16fde5f2 KW |
548 | |
549 | /* Then decrease the refcount of the old table */ | |
550 | if (l2_offset) { | |
551 | qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t)); | |
552 | } | |
45aba42f KW |
553 | } |
554 | ||
555 | /* find the cluster offset for the given disk offset */ | |
556 | ||
557 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); | |
558 | ||
559 | *new_l2_table = l2_table; | |
45aba42f KW |
560 | *new_l2_index = l2_index; |
561 | ||
1e3e8f1a | 562 | return 0; |
45aba42f KW |
563 | } |
564 | ||
565 | /* | |
566 | * alloc_compressed_cluster_offset | |
567 | * | |
568 | * For a given offset of the disk image, return cluster offset in | |
569 | * qcow2 file. | |
570 | * | |
571 | * If the offset is not found, allocate a new compressed cluster. | |
572 | * | |
573 | * Return the cluster offset if successful, | |
574 | * Return 0, otherwise. | |
575 | * | |
576 | */ | |
577 | ||
ed6ccf0f KW |
578 | uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, |
579 | uint64_t offset, | |
580 | int compressed_size) | |
45aba42f KW |
581 | { |
582 | BDRVQcowState *s = bs->opaque; | |
583 | int l2_index, ret; | |
3948d1d4 | 584 | uint64_t *l2_table; |
f4f0d391 | 585 | int64_t cluster_offset; |
45aba42f KW |
586 | int nb_csectors; |
587 | ||
3948d1d4 | 588 | ret = get_cluster_table(bs, offset, &l2_table, &l2_index); |
1e3e8f1a | 589 | if (ret < 0) { |
45aba42f | 590 | return 0; |
1e3e8f1a | 591 | } |
45aba42f | 592 | |
b0b6862e KW |
593 | /* Compression can't overwrite anything. Fail if the cluster was already |
594 | * allocated. */ | |
45aba42f | 595 | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
b0b6862e | 596 | if (cluster_offset & L2E_OFFSET_MASK) { |
8f1efd00 KW |
597 | qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
598 | return 0; | |
599 | } | |
45aba42f | 600 | |
ed6ccf0f | 601 | cluster_offset = qcow2_alloc_bytes(bs, compressed_size); |
5d757b56 | 602 | if (cluster_offset < 0) { |
29c1a730 | 603 | qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
5d757b56 KW |
604 | return 0; |
605 | } | |
606 | ||
45aba42f KW |
607 | nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) - |
608 | (cluster_offset >> 9); | |
609 | ||
610 | cluster_offset |= QCOW_OFLAG_COMPRESSED | | |
611 | ((uint64_t)nb_csectors << s->csize_shift); | |
612 | ||
613 | /* update L2 table */ | |
614 | ||
615 | /* compressed clusters never have the copied flag */ | |
616 | ||
66f82cee | 617 | BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED); |
29c1a730 | 618 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); |
45aba42f | 619 | l2_table[l2_index] = cpu_to_be64(cluster_offset); |
29c1a730 | 620 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
79a31189 | 621 | if (ret < 0) { |
29c1a730 | 622 | return 0; |
4c1612d9 KW |
623 | } |
624 | ||
29c1a730 | 625 | return cluster_offset; |
4c1612d9 KW |
626 | } |
627 | ||
593fb83c KW |
628 | static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion *r) |
629 | { | |
630 | BDRVQcowState *s = bs->opaque; | |
631 | int ret; | |
632 | ||
633 | if (r->nb_sectors == 0) { | |
634 | return 0; | |
635 | } | |
636 | ||
637 | qemu_co_mutex_unlock(&s->lock); | |
638 | ret = copy_sectors(bs, m->offset / BDRV_SECTOR_SIZE, m->alloc_offset, | |
639 | r->offset / BDRV_SECTOR_SIZE, | |
640 | r->offset / BDRV_SECTOR_SIZE + r->nb_sectors); | |
641 | qemu_co_mutex_lock(&s->lock); | |
642 | ||
643 | if (ret < 0) { | |
644 | return ret; | |
645 | } | |
646 | ||
647 | /* | |
648 | * Before we update the L2 table to actually point to the new cluster, we | |
649 | * need to be sure that the refcounts have been increased and COW was | |
650 | * handled. | |
651 | */ | |
652 | qcow2_cache_depends_on_flush(s->l2_table_cache); | |
653 | ||
654 | return 0; | |
655 | } | |
656 | ||
148da7ea | 657 | int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) |
45aba42f KW |
658 | { |
659 | BDRVQcowState *s = bs->opaque; | |
660 | int i, j = 0, l2_index, ret; | |
593fb83c | 661 | uint64_t *old_cluster, *l2_table; |
250196f1 | 662 | uint64_t cluster_offset = m->alloc_offset; |
45aba42f | 663 | |
3cce16f4 | 664 | trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters); |
f50f88b9 | 665 | assert(m->nb_clusters > 0); |
45aba42f | 666 | |
7267c094 | 667 | old_cluster = g_malloc(m->nb_clusters * sizeof(uint64_t)); |
45aba42f KW |
668 | |
669 | /* copy content of unmodified sectors */ | |
593fb83c KW |
670 | ret = perform_cow(bs, m, &m->cow_start); |
671 | if (ret < 0) { | |
672 | goto err; | |
45aba42f KW |
673 | } |
674 | ||
593fb83c KW |
675 | ret = perform_cow(bs, m, &m->cow_end); |
676 | if (ret < 0) { | |
677 | goto err; | |
29c1a730 KW |
678 | } |
679 | ||
593fb83c | 680 | /* Update L2 table. */ |
74c4510a | 681 | if (s->use_lazy_refcounts) { |
280d3735 KW |
682 | qcow2_mark_dirty(bs); |
683 | } | |
bfe8043e SH |
684 | if (qcow2_need_accurate_refcounts(s)) { |
685 | qcow2_cache_set_dependency(bs, s->l2_table_cache, | |
686 | s->refcount_block_cache); | |
687 | } | |
280d3735 | 688 | |
3948d1d4 | 689 | ret = get_cluster_table(bs, m->offset, &l2_table, &l2_index); |
1e3e8f1a | 690 | if (ret < 0) { |
45aba42f | 691 | goto err; |
1e3e8f1a | 692 | } |
29c1a730 | 693 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); |
45aba42f KW |
694 | |
695 | for (i = 0; i < m->nb_clusters; i++) { | |
696 | /* if two concurrent writes happen to the same unallocated cluster | |
697 | * each write allocates separate cluster and writes data concurrently. | |
698 | * The first one to complete updates l2 table with pointer to its | |
699 | * cluster the second one has to do RMW (which is done above by | |
700 | * copy_sectors()), update l2 table with its cluster pointer and free | |
701 | * old cluster. This is what this loop does */ | |
702 | if(l2_table[l2_index + i] != 0) | |
703 | old_cluster[j++] = l2_table[l2_index + i]; | |
704 | ||
705 | l2_table[l2_index + i] = cpu_to_be64((cluster_offset + | |
706 | (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); | |
707 | } | |
708 | ||
9f8e668e | 709 | |
29c1a730 | 710 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
c835d00f | 711 | if (ret < 0) { |
45aba42f | 712 | goto err; |
4c1612d9 | 713 | } |
45aba42f | 714 | |
7ec5e6a4 KW |
715 | /* |
716 | * If this was a COW, we need to decrease the refcount of the old cluster. | |
717 | * Also flush bs->file to get the right order for L2 and refcount update. | |
718 | */ | |
719 | if (j != 0) { | |
7ec5e6a4 | 720 | for (i = 0; i < j; i++) { |
8e37f681 | 721 | qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1); |
7ec5e6a4 KW |
722 | } |
723 | } | |
45aba42f KW |
724 | |
725 | ret = 0; | |
726 | err: | |
7267c094 | 727 | g_free(old_cluster); |
45aba42f KW |
728 | return ret; |
729 | } | |
730 | ||
bf319ece KW |
731 | /* |
732 | * Returns the number of contiguous clusters that can be used for an allocating | |
733 | * write, but require COW to be performed (this includes yet unallocated space, | |
734 | * which must copy from the backing file) | |
735 | */ | |
736 | static int count_cow_clusters(BDRVQcowState *s, int nb_clusters, | |
737 | uint64_t *l2_table, int l2_index) | |
738 | { | |
143550a8 | 739 | int i; |
bf319ece | 740 | |
143550a8 KW |
741 | for (i = 0; i < nb_clusters; i++) { |
742 | uint64_t l2_entry = be64_to_cpu(l2_table[l2_index + i]); | |
743 | int cluster_type = qcow2_get_cluster_type(l2_entry); | |
744 | ||
745 | switch(cluster_type) { | |
746 | case QCOW2_CLUSTER_NORMAL: | |
747 | if (l2_entry & QCOW_OFLAG_COPIED) { | |
748 | goto out; | |
749 | } | |
bf319ece | 750 | break; |
143550a8 KW |
751 | case QCOW2_CLUSTER_UNALLOCATED: |
752 | case QCOW2_CLUSTER_COMPRESSED: | |
6377af48 | 753 | case QCOW2_CLUSTER_ZERO: |
bf319ece | 754 | break; |
143550a8 KW |
755 | default: |
756 | abort(); | |
757 | } | |
bf319ece KW |
758 | } |
759 | ||
143550a8 | 760 | out: |
bf319ece KW |
761 | assert(i <= nb_clusters); |
762 | return i; | |
763 | } | |
764 | ||
250196f1 | 765 | /* |
226c3c26 KW |
766 | * Check if there already is an AIO write request in flight which allocates |
767 | * the same cluster. In this case we need to wait until the previous | |
768 | * request has completed and updated the L2 table accordingly. | |
65eb2e35 KW |
769 | * |
770 | * Returns: | |
771 | * 0 if there was no dependency. *cur_bytes indicates the number of | |
772 | * bytes from guest_offset that can be read before the next | |
773 | * dependency must be processed (or the request is complete) | |
774 | * | |
775 | * -EAGAIN if we had to wait for another request, previously gathered | |
776 | * information on cluster allocation may be invalid now. The caller | |
777 | * must start over anyway, so consider *cur_bytes undefined. | |
250196f1 | 778 | */ |
226c3c26 | 779 | static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset, |
ecdd5333 | 780 | uint64_t *cur_bytes, QCowL2Meta **m) |
250196f1 KW |
781 | { |
782 | BDRVQcowState *s = bs->opaque; | |
250196f1 | 783 | QCowL2Meta *old_alloc; |
65eb2e35 | 784 | uint64_t bytes = *cur_bytes; |
250196f1 | 785 | |
250196f1 KW |
786 | QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) { |
787 | ||
65eb2e35 KW |
788 | uint64_t start = guest_offset; |
789 | uint64_t end = start + bytes; | |
790 | uint64_t old_start = l2meta_cow_start(old_alloc); | |
791 | uint64_t old_end = l2meta_cow_end(old_alloc); | |
250196f1 | 792 | |
d9d74f41 | 793 | if (end <= old_start || start >= old_end) { |
250196f1 KW |
794 | /* No intersection */ |
795 | } else { | |
796 | if (start < old_start) { | |
797 | /* Stop at the start of a running allocation */ | |
65eb2e35 | 798 | bytes = old_start - start; |
250196f1 | 799 | } else { |
65eb2e35 | 800 | bytes = 0; |
250196f1 KW |
801 | } |
802 | ||
ecdd5333 KW |
803 | /* Stop if already an l2meta exists. After yielding, it wouldn't |
804 | * be valid any more, so we'd have to clean up the old L2Metas | |
805 | * and deal with requests depending on them before starting to | |
806 | * gather new ones. Not worth the trouble. */ | |
807 | if (bytes == 0 && *m) { | |
808 | *cur_bytes = 0; | |
809 | return 0; | |
810 | } | |
811 | ||
65eb2e35 | 812 | if (bytes == 0) { |
250196f1 KW |
813 | /* Wait for the dependency to complete. We need to recheck |
814 | * the free/allocated clusters when we continue. */ | |
815 | qemu_co_mutex_unlock(&s->lock); | |
816 | qemu_co_queue_wait(&old_alloc->dependent_requests); | |
817 | qemu_co_mutex_lock(&s->lock); | |
818 | return -EAGAIN; | |
819 | } | |
820 | } | |
821 | } | |
822 | ||
65eb2e35 KW |
823 | /* Make sure that existing clusters and new allocations are only used up to |
824 | * the next dependency if we shortened the request above */ | |
825 | *cur_bytes = bytes; | |
250196f1 | 826 | |
226c3c26 KW |
827 | return 0; |
828 | } | |
829 | ||
0af729ec KW |
830 | /* |
831 | * Checks how many already allocated clusters that don't require a copy on | |
832 | * write there are at the given guest_offset (up to *bytes). If | |
833 | * *host_offset is not zero, only physically contiguous clusters beginning at | |
834 | * this host offset are counted. | |
835 | * | |
411d62b0 KW |
836 | * Note that guest_offset may not be cluster aligned. In this case, the |
837 | * returned *host_offset points to exact byte referenced by guest_offset and | |
838 | * therefore isn't cluster aligned as well. | |
0af729ec KW |
839 | * |
840 | * Returns: | |
841 | * 0: if no allocated clusters are available at the given offset. | |
842 | * *bytes is normally unchanged. It is set to 0 if the cluster | |
843 | * is allocated and doesn't need COW, but doesn't have the right | |
844 | * physical offset. | |
845 | * | |
846 | * 1: if allocated clusters that don't require a COW are available at | |
847 | * the requested offset. *bytes may have decreased and describes | |
848 | * the length of the area that can be written to. | |
849 | * | |
850 | * -errno: in error cases | |
0af729ec KW |
851 | */ |
852 | static int handle_copied(BlockDriverState *bs, uint64_t guest_offset, | |
c53ede9f | 853 | uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m) |
0af729ec KW |
854 | { |
855 | BDRVQcowState *s = bs->opaque; | |
856 | int l2_index; | |
857 | uint64_t cluster_offset; | |
858 | uint64_t *l2_table; | |
acb0467f | 859 | unsigned int nb_clusters; |
c53ede9f | 860 | unsigned int keep_clusters; |
0af729ec KW |
861 | int ret, pret; |
862 | ||
863 | trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset, | |
864 | *bytes); | |
0af729ec | 865 | |
411d62b0 KW |
866 | assert(*host_offset == 0 || offset_into_cluster(s, guest_offset) |
867 | == offset_into_cluster(s, *host_offset)); | |
868 | ||
acb0467f KW |
869 | /* |
870 | * Calculate the number of clusters to look for. We stop at L2 table | |
871 | * boundaries to keep things simple. | |
872 | */ | |
873 | nb_clusters = | |
874 | size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes); | |
875 | ||
876 | l2_index = offset_to_l2_index(s, guest_offset); | |
877 | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); | |
878 | ||
0af729ec KW |
879 | /* Find L2 entry for the first involved cluster */ |
880 | ret = get_cluster_table(bs, guest_offset, &l2_table, &l2_index); | |
881 | if (ret < 0) { | |
882 | return ret; | |
883 | } | |
884 | ||
885 | cluster_offset = be64_to_cpu(l2_table[l2_index]); | |
886 | ||
887 | /* Check how many clusters are already allocated and don't need COW */ | |
888 | if (qcow2_get_cluster_type(cluster_offset) == QCOW2_CLUSTER_NORMAL | |
889 | && (cluster_offset & QCOW_OFLAG_COPIED)) | |
890 | { | |
e62daaf6 KW |
891 | /* If a specific host_offset is required, check it */ |
892 | bool offset_matches = | |
893 | (cluster_offset & L2E_OFFSET_MASK) == *host_offset; | |
894 | ||
895 | if (*host_offset != 0 && !offset_matches) { | |
896 | *bytes = 0; | |
897 | ret = 0; | |
898 | goto out; | |
899 | } | |
900 | ||
0af729ec | 901 | /* We keep all QCOW_OFLAG_COPIED clusters */ |
c53ede9f | 902 | keep_clusters = |
acb0467f | 903 | count_contiguous_clusters(nb_clusters, s->cluster_size, |
0af729ec KW |
904 | &l2_table[l2_index], 0, |
905 | QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO); | |
c53ede9f KW |
906 | assert(keep_clusters <= nb_clusters); |
907 | ||
908 | *bytes = MIN(*bytes, | |
909 | keep_clusters * s->cluster_size | |
910 | - offset_into_cluster(s, guest_offset)); | |
0af729ec KW |
911 | |
912 | ret = 1; | |
913 | } else { | |
0af729ec KW |
914 | ret = 0; |
915 | } | |
916 | ||
0af729ec | 917 | /* Cleanup */ |
e62daaf6 | 918 | out: |
0af729ec KW |
919 | pret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
920 | if (pret < 0) { | |
921 | return pret; | |
922 | } | |
923 | ||
e62daaf6 KW |
924 | /* Only return a host offset if we actually made progress. Otherwise we |
925 | * would make requirements for handle_alloc() that it can't fulfill */ | |
926 | if (ret) { | |
411d62b0 KW |
927 | *host_offset = (cluster_offset & L2E_OFFSET_MASK) |
928 | + offset_into_cluster(s, guest_offset); | |
e62daaf6 KW |
929 | } |
930 | ||
0af729ec KW |
931 | return ret; |
932 | } | |
933 | ||
226c3c26 KW |
934 | /* |
935 | * Allocates new clusters for the given guest_offset. | |
936 | * | |
937 | * At most *nb_clusters are allocated, and on return *nb_clusters is updated to | |
938 | * contain the number of clusters that have been allocated and are contiguous | |
939 | * in the image file. | |
940 | * | |
941 | * If *host_offset is non-zero, it specifies the offset in the image file at | |
942 | * which the new clusters must start. *nb_clusters can be 0 on return in this | |
943 | * case if the cluster at host_offset is already in use. If *host_offset is | |
944 | * zero, the clusters can be allocated anywhere in the image file. | |
945 | * | |
946 | * *host_offset is updated to contain the offset into the image file at which | |
947 | * the first allocated cluster starts. | |
948 | * | |
949 | * Return 0 on success and -errno in error cases. -EAGAIN means that the | |
950 | * function has been waiting for another request and the allocation must be | |
951 | * restarted, but the whole request should not be failed. | |
952 | */ | |
953 | static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset, | |
954 | uint64_t *host_offset, unsigned int *nb_clusters) | |
955 | { | |
956 | BDRVQcowState *s = bs->opaque; | |
226c3c26 KW |
957 | |
958 | trace_qcow2_do_alloc_clusters_offset(qemu_coroutine_self(), guest_offset, | |
959 | *host_offset, *nb_clusters); | |
960 | ||
250196f1 KW |
961 | /* Allocate new clusters */ |
962 | trace_qcow2_cluster_alloc_phys(qemu_coroutine_self()); | |
963 | if (*host_offset == 0) { | |
df021791 KW |
964 | int64_t cluster_offset = |
965 | qcow2_alloc_clusters(bs, *nb_clusters * s->cluster_size); | |
966 | if (cluster_offset < 0) { | |
967 | return cluster_offset; | |
968 | } | |
969 | *host_offset = cluster_offset; | |
970 | return 0; | |
250196f1 | 971 | } else { |
17a71e58 | 972 | int ret = qcow2_alloc_clusters_at(bs, *host_offset, *nb_clusters); |
df021791 KW |
973 | if (ret < 0) { |
974 | return ret; | |
975 | } | |
976 | *nb_clusters = ret; | |
977 | return 0; | |
250196f1 | 978 | } |
250196f1 KW |
979 | } |
980 | ||
10f0ed8b KW |
981 | /* |
982 | * Allocates new clusters for an area that either is yet unallocated or needs a | |
983 | * copy on write. If *host_offset is non-zero, clusters are only allocated if | |
984 | * the new allocation can match the specified host offset. | |
985 | * | |
411d62b0 KW |
986 | * Note that guest_offset may not be cluster aligned. In this case, the |
987 | * returned *host_offset points to exact byte referenced by guest_offset and | |
988 | * therefore isn't cluster aligned as well. | |
10f0ed8b KW |
989 | * |
990 | * Returns: | |
991 | * 0: if no clusters could be allocated. *bytes is set to 0, | |
992 | * *host_offset is left unchanged. | |
993 | * | |
994 | * 1: if new clusters were allocated. *bytes may be decreased if the | |
995 | * new allocation doesn't cover all of the requested area. | |
996 | * *host_offset is updated to contain the host offset of the first | |
997 | * newly allocated cluster. | |
998 | * | |
999 | * -errno: in error cases | |
10f0ed8b KW |
1000 | */ |
1001 | static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset, | |
c37f4cd7 | 1002 | uint64_t *host_offset, uint64_t *bytes, QCowL2Meta **m) |
10f0ed8b KW |
1003 | { |
1004 | BDRVQcowState *s = bs->opaque; | |
1005 | int l2_index; | |
1006 | uint64_t *l2_table; | |
1007 | uint64_t entry; | |
f5bc6350 | 1008 | unsigned int nb_clusters; |
10f0ed8b KW |
1009 | int ret; |
1010 | ||
10f0ed8b | 1011 | uint64_t alloc_cluster_offset; |
10f0ed8b KW |
1012 | |
1013 | trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset, | |
1014 | *bytes); | |
1015 | assert(*bytes > 0); | |
1016 | ||
f5bc6350 KW |
1017 | /* |
1018 | * Calculate the number of clusters to look for. We stop at L2 table | |
1019 | * boundaries to keep things simple. | |
1020 | */ | |
c37f4cd7 KW |
1021 | nb_clusters = |
1022 | size_to_clusters(s, offset_into_cluster(s, guest_offset) + *bytes); | |
1023 | ||
f5bc6350 | 1024 | l2_index = offset_to_l2_index(s, guest_offset); |
c37f4cd7 | 1025 | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); |
f5bc6350 | 1026 | |
10f0ed8b KW |
1027 | /* Find L2 entry for the first involved cluster */ |
1028 | ret = get_cluster_table(bs, guest_offset, &l2_table, &l2_index); | |
1029 | if (ret < 0) { | |
1030 | return ret; | |
1031 | } | |
1032 | ||
3b8e2e26 | 1033 | entry = be64_to_cpu(l2_table[l2_index]); |
10f0ed8b KW |
1034 | |
1035 | /* For the moment, overwrite compressed clusters one by one */ | |
1036 | if (entry & QCOW_OFLAG_COMPRESSED) { | |
1037 | nb_clusters = 1; | |
1038 | } else { | |
3b8e2e26 | 1039 | nb_clusters = count_cow_clusters(s, nb_clusters, l2_table, l2_index); |
10f0ed8b KW |
1040 | } |
1041 | ||
ecdd5333 KW |
1042 | /* This function is only called when there were no non-COW clusters, so if |
1043 | * we can't find any unallocated or COW clusters either, something is | |
1044 | * wrong with our code. */ | |
1045 | assert(nb_clusters > 0); | |
1046 | ||
10f0ed8b KW |
1047 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); |
1048 | if (ret < 0) { | |
1049 | return ret; | |
1050 | } | |
1051 | ||
10f0ed8b | 1052 | /* Allocate, if necessary at a given offset in the image file */ |
411d62b0 | 1053 | alloc_cluster_offset = start_of_cluster(s, *host_offset); |
83baa9a4 | 1054 | ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset, |
10f0ed8b KW |
1055 | &nb_clusters); |
1056 | if (ret < 0) { | |
1057 | goto fail; | |
1058 | } | |
1059 | ||
83baa9a4 KW |
1060 | /* Can't extend contiguous allocation */ |
1061 | if (nb_clusters == 0) { | |
10f0ed8b KW |
1062 | *bytes = 0; |
1063 | return 0; | |
1064 | } | |
1065 | ||
83baa9a4 KW |
1066 | /* |
1067 | * Save info needed for meta data update. | |
1068 | * | |
1069 | * requested_sectors: Number of sectors from the start of the first | |
1070 | * newly allocated cluster to the end of the (possibly shortened | |
1071 | * before) write request. | |
1072 | * | |
1073 | * avail_sectors: Number of sectors from the start of the first | |
1074 | * newly allocated to the end of the last newly allocated cluster. | |
1075 | * | |
1076 | * nb_sectors: The number of sectors from the start of the first | |
1077 | * newly allocated cluster to the end of the area that the write | |
1078 | * request actually writes to (excluding COW at the end) | |
1079 | */ | |
1080 | int requested_sectors = | |
1081 | (*bytes + offset_into_cluster(s, guest_offset)) | |
1082 | >> BDRV_SECTOR_BITS; | |
1083 | int avail_sectors = nb_clusters | |
1084 | << (s->cluster_bits - BDRV_SECTOR_BITS); | |
1085 | int alloc_n_start = offset_into_cluster(s, guest_offset) | |
1086 | >> BDRV_SECTOR_BITS; | |
1087 | int nb_sectors = MIN(requested_sectors, avail_sectors); | |
88c6588c | 1088 | QCowL2Meta *old_m = *m; |
83baa9a4 | 1089 | |
83baa9a4 KW |
1090 | *m = g_malloc0(sizeof(**m)); |
1091 | ||
1092 | **m = (QCowL2Meta) { | |
88c6588c KW |
1093 | .next = old_m, |
1094 | ||
411d62b0 | 1095 | .alloc_offset = alloc_cluster_offset, |
83baa9a4 KW |
1096 | .offset = start_of_cluster(s, guest_offset), |
1097 | .nb_clusters = nb_clusters, | |
1098 | .nb_available = nb_sectors, | |
1099 | ||
1100 | .cow_start = { | |
1101 | .offset = 0, | |
1102 | .nb_sectors = alloc_n_start, | |
1103 | }, | |
1104 | .cow_end = { | |
1105 | .offset = nb_sectors * BDRV_SECTOR_SIZE, | |
1106 | .nb_sectors = avail_sectors - nb_sectors, | |
1107 | }, | |
1108 | }; | |
1109 | qemu_co_queue_init(&(*m)->dependent_requests); | |
1110 | QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight); | |
1111 | ||
411d62b0 | 1112 | *host_offset = alloc_cluster_offset + offset_into_cluster(s, guest_offset); |
83baa9a4 KW |
1113 | *bytes = MIN(*bytes, (nb_sectors * BDRV_SECTOR_SIZE) |
1114 | - offset_into_cluster(s, guest_offset)); | |
1115 | assert(*bytes != 0); | |
1116 | ||
10f0ed8b KW |
1117 | return 1; |
1118 | ||
1119 | fail: | |
1120 | if (*m && (*m)->nb_clusters > 0) { | |
1121 | QLIST_REMOVE(*m, next_in_flight); | |
1122 | } | |
1123 | return ret; | |
1124 | } | |
1125 | ||
45aba42f KW |
1126 | /* |
1127 | * alloc_cluster_offset | |
1128 | * | |
250196f1 KW |
1129 | * For a given offset on the virtual disk, find the cluster offset in qcow2 |
1130 | * file. If the offset is not found, allocate a new cluster. | |
45aba42f | 1131 | * |
250196f1 | 1132 | * If the cluster was already allocated, m->nb_clusters is set to 0 and |
a7912369 | 1133 | * other fields in m are meaningless. |
148da7ea KW |
1134 | * |
1135 | * If the cluster is newly allocated, m->nb_clusters is set to the number of | |
68d100e9 KW |
1136 | * contiguous clusters that have been allocated. In this case, the other |
1137 | * fields of m are valid and contain information about the first allocated | |
1138 | * cluster. | |
45aba42f | 1139 | * |
68d100e9 KW |
1140 | * If the request conflicts with another write request in flight, the coroutine |
1141 | * is queued and will be reentered when the dependency has completed. | |
148da7ea KW |
1142 | * |
1143 | * Return 0 on success and -errno in error cases | |
45aba42f | 1144 | */ |
f4f0d391 | 1145 | int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, |
f50f88b9 | 1146 | int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m) |
45aba42f KW |
1147 | { |
1148 | BDRVQcowState *s = bs->opaque; | |
710c2496 | 1149 | uint64_t start, remaining; |
250196f1 | 1150 | uint64_t cluster_offset; |
65eb2e35 | 1151 | uint64_t cur_bytes; |
710c2496 | 1152 | int ret; |
45aba42f | 1153 | |
3cce16f4 KW |
1154 | trace_qcow2_alloc_clusters_offset(qemu_coroutine_self(), offset, |
1155 | n_start, n_end); | |
1156 | ||
710c2496 KW |
1157 | assert(n_start * BDRV_SECTOR_SIZE == offset_into_cluster(s, offset)); |
1158 | offset = start_of_cluster(s, offset); | |
1159 | ||
72424114 | 1160 | again: |
710c2496 KW |
1161 | start = offset + (n_start << BDRV_SECTOR_BITS); |
1162 | remaining = (n_end - n_start) << BDRV_SECTOR_BITS; | |
0af729ec KW |
1163 | cluster_offset = 0; |
1164 | *host_offset = 0; | |
ecdd5333 KW |
1165 | cur_bytes = 0; |
1166 | *m = NULL; | |
0af729ec | 1167 | |
2c3b32d2 | 1168 | while (true) { |
ecdd5333 KW |
1169 | |
1170 | if (!*host_offset) { | |
1171 | *host_offset = start_of_cluster(s, cluster_offset); | |
1172 | } | |
1173 | ||
1174 | assert(remaining >= cur_bytes); | |
1175 | ||
1176 | start += cur_bytes; | |
1177 | remaining -= cur_bytes; | |
1178 | cluster_offset += cur_bytes; | |
1179 | ||
1180 | if (remaining == 0) { | |
1181 | break; | |
1182 | } | |
1183 | ||
1184 | cur_bytes = remaining; | |
1185 | ||
2c3b32d2 KW |
1186 | /* |
1187 | * Now start gathering as many contiguous clusters as possible: | |
1188 | * | |
1189 | * 1. Check for overlaps with in-flight allocations | |
1190 | * | |
1191 | * a) Overlap not in the first cluster -> shorten this request and | |
1192 | * let the caller handle the rest in its next loop iteration. | |
1193 | * | |
1194 | * b) Real overlaps of two requests. Yield and restart the search | |
1195 | * for contiguous clusters (the situation could have changed | |
1196 | * while we were sleeping) | |
1197 | * | |
1198 | * c) TODO: Request starts in the same cluster as the in-flight | |
1199 | * allocation ends. Shorten the COW of the in-fight allocation, | |
1200 | * set cluster_offset to write to the same cluster and set up | |
1201 | * the right synchronisation between the in-flight request and | |
1202 | * the new one. | |
1203 | */ | |
ecdd5333 | 1204 | ret = handle_dependencies(bs, start, &cur_bytes, m); |
2c3b32d2 | 1205 | if (ret == -EAGAIN) { |
ecdd5333 KW |
1206 | /* Currently handle_dependencies() doesn't yield if we already had |
1207 | * an allocation. If it did, we would have to clean up the L2Meta | |
1208 | * structs before starting over. */ | |
1209 | assert(*m == NULL); | |
2c3b32d2 KW |
1210 | goto again; |
1211 | } else if (ret < 0) { | |
1212 | return ret; | |
ecdd5333 KW |
1213 | } else if (cur_bytes == 0) { |
1214 | break; | |
2c3b32d2 KW |
1215 | } else { |
1216 | /* handle_dependencies() may have decreased cur_bytes (shortened | |
1217 | * the allocations below) so that the next dependency is processed | |
1218 | * correctly during the next loop iteration. */ | |
0af729ec | 1219 | } |
710c2496 | 1220 | |
2c3b32d2 KW |
1221 | /* |
1222 | * 2. Count contiguous COPIED clusters. | |
1223 | */ | |
1224 | ret = handle_copied(bs, start, &cluster_offset, &cur_bytes, m); | |
1225 | if (ret < 0) { | |
1226 | return ret; | |
1227 | } else if (ret) { | |
ecdd5333 | 1228 | continue; |
2c3b32d2 KW |
1229 | } else if (cur_bytes == 0) { |
1230 | break; | |
1231 | } | |
060bee89 | 1232 | |
2c3b32d2 KW |
1233 | /* |
1234 | * 3. If the request still hasn't completed, allocate new clusters, | |
1235 | * considering any cluster_offset of steps 1c or 2. | |
1236 | */ | |
1237 | ret = handle_alloc(bs, start, &cluster_offset, &cur_bytes, m); | |
1238 | if (ret < 0) { | |
1239 | return ret; | |
1240 | } else if (ret) { | |
ecdd5333 | 1241 | continue; |
2c3b32d2 KW |
1242 | } else { |
1243 | assert(cur_bytes == 0); | |
1244 | break; | |
1245 | } | |
f5bc6350 | 1246 | } |
10f0ed8b | 1247 | |
710c2496 KW |
1248 | *num = (n_end - n_start) - (remaining >> BDRV_SECTOR_BITS); |
1249 | assert(*num > 0); | |
1250 | assert(*host_offset != 0); | |
45aba42f | 1251 | |
148da7ea | 1252 | return 0; |
45aba42f KW |
1253 | } |
1254 | ||
1255 | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, | |
1256 | const uint8_t *buf, int buf_size) | |
1257 | { | |
1258 | z_stream strm1, *strm = &strm1; | |
1259 | int ret, out_len; | |
1260 | ||
1261 | memset(strm, 0, sizeof(*strm)); | |
1262 | ||
1263 | strm->next_in = (uint8_t *)buf; | |
1264 | strm->avail_in = buf_size; | |
1265 | strm->next_out = out_buf; | |
1266 | strm->avail_out = out_buf_size; | |
1267 | ||
1268 | ret = inflateInit2(strm, -12); | |
1269 | if (ret != Z_OK) | |
1270 | return -1; | |
1271 | ret = inflate(strm, Z_FINISH); | |
1272 | out_len = strm->next_out - out_buf; | |
1273 | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || | |
1274 | out_len != out_buf_size) { | |
1275 | inflateEnd(strm); | |
1276 | return -1; | |
1277 | } | |
1278 | inflateEnd(strm); | |
1279 | return 0; | |
1280 | } | |
1281 | ||
66f82cee | 1282 | int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) |
45aba42f | 1283 | { |
66f82cee | 1284 | BDRVQcowState *s = bs->opaque; |
45aba42f KW |
1285 | int ret, csize, nb_csectors, sector_offset; |
1286 | uint64_t coffset; | |
1287 | ||
1288 | coffset = cluster_offset & s->cluster_offset_mask; | |
1289 | if (s->cluster_cache_offset != coffset) { | |
1290 | nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1; | |
1291 | sector_offset = coffset & 511; | |
1292 | csize = nb_csectors * 512 - sector_offset; | |
66f82cee KW |
1293 | BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED); |
1294 | ret = bdrv_read(bs->file, coffset >> 9, s->cluster_data, nb_csectors); | |
45aba42f | 1295 | if (ret < 0) { |
8af36488 | 1296 | return ret; |
45aba42f KW |
1297 | } |
1298 | if (decompress_buffer(s->cluster_cache, s->cluster_size, | |
1299 | s->cluster_data + sector_offset, csize) < 0) { | |
8af36488 | 1300 | return -EIO; |
45aba42f KW |
1301 | } |
1302 | s->cluster_cache_offset = coffset; | |
1303 | } | |
1304 | return 0; | |
1305 | } | |
5ea929e3 KW |
1306 | |
1307 | /* | |
1308 | * This discards as many clusters of nb_clusters as possible at once (i.e. | |
1309 | * all clusters in the same L2 table) and returns the number of discarded | |
1310 | * clusters. | |
1311 | */ | |
1312 | static int discard_single_l2(BlockDriverState *bs, uint64_t offset, | |
1313 | unsigned int nb_clusters) | |
1314 | { | |
1315 | BDRVQcowState *s = bs->opaque; | |
3948d1d4 | 1316 | uint64_t *l2_table; |
5ea929e3 KW |
1317 | int l2_index; |
1318 | int ret; | |
1319 | int i; | |
1320 | ||
3948d1d4 | 1321 | ret = get_cluster_table(bs, offset, &l2_table, &l2_index); |
5ea929e3 KW |
1322 | if (ret < 0) { |
1323 | return ret; | |
1324 | } | |
1325 | ||
1326 | /* Limit nb_clusters to one L2 table */ | |
1327 | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); | |
1328 | ||
1329 | for (i = 0; i < nb_clusters; i++) { | |
1330 | uint64_t old_offset; | |
1331 | ||
1332 | old_offset = be64_to_cpu(l2_table[l2_index + i]); | |
8e37f681 | 1333 | if ((old_offset & L2E_OFFSET_MASK) == 0) { |
5ea929e3 KW |
1334 | continue; |
1335 | } | |
1336 | ||
1337 | /* First remove L2 entries */ | |
1338 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); | |
1339 | l2_table[l2_index + i] = cpu_to_be64(0); | |
1340 | ||
1341 | /* Then decrease the refcount */ | |
1342 | qcow2_free_any_clusters(bs, old_offset, 1); | |
1343 | } | |
1344 | ||
1345 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); | |
1346 | if (ret < 0) { | |
1347 | return ret; | |
1348 | } | |
1349 | ||
1350 | return nb_clusters; | |
1351 | } | |
1352 | ||
1353 | int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, | |
1354 | int nb_sectors) | |
1355 | { | |
1356 | BDRVQcowState *s = bs->opaque; | |
1357 | uint64_t end_offset; | |
1358 | unsigned int nb_clusters; | |
1359 | int ret; | |
1360 | ||
1361 | end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS); | |
1362 | ||
1363 | /* Round start up and end down */ | |
1364 | offset = align_offset(offset, s->cluster_size); | |
1365 | end_offset &= ~(s->cluster_size - 1); | |
1366 | ||
1367 | if (offset > end_offset) { | |
1368 | return 0; | |
1369 | } | |
1370 | ||
1371 | nb_clusters = size_to_clusters(s, end_offset - offset); | |
1372 | ||
1373 | /* Each L2 table is handled by its own loop iteration */ | |
1374 | while (nb_clusters > 0) { | |
1375 | ret = discard_single_l2(bs, offset, nb_clusters); | |
1376 | if (ret < 0) { | |
1377 | return ret; | |
1378 | } | |
1379 | ||
1380 | nb_clusters -= ret; | |
1381 | offset += (ret * s->cluster_size); | |
1382 | } | |
1383 | ||
1384 | return 0; | |
1385 | } | |
621f0589 KW |
1386 | |
1387 | /* | |
1388 | * This zeroes as many clusters of nb_clusters as possible at once (i.e. | |
1389 | * all clusters in the same L2 table) and returns the number of zeroed | |
1390 | * clusters. | |
1391 | */ | |
1392 | static int zero_single_l2(BlockDriverState *bs, uint64_t offset, | |
1393 | unsigned int nb_clusters) | |
1394 | { | |
1395 | BDRVQcowState *s = bs->opaque; | |
1396 | uint64_t *l2_table; | |
1397 | int l2_index; | |
1398 | int ret; | |
1399 | int i; | |
1400 | ||
1401 | ret = get_cluster_table(bs, offset, &l2_table, &l2_index); | |
1402 | if (ret < 0) { | |
1403 | return ret; | |
1404 | } | |
1405 | ||
1406 | /* Limit nb_clusters to one L2 table */ | |
1407 | nb_clusters = MIN(nb_clusters, s->l2_size - l2_index); | |
1408 | ||
1409 | for (i = 0; i < nb_clusters; i++) { | |
1410 | uint64_t old_offset; | |
1411 | ||
1412 | old_offset = be64_to_cpu(l2_table[l2_index + i]); | |
1413 | ||
1414 | /* Update L2 entries */ | |
1415 | qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); | |
1416 | if (old_offset & QCOW_OFLAG_COMPRESSED) { | |
1417 | l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO); | |
1418 | qcow2_free_any_clusters(bs, old_offset, 1); | |
1419 | } else { | |
1420 | l2_table[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO); | |
1421 | } | |
1422 | } | |
1423 | ||
1424 | ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table); | |
1425 | if (ret < 0) { | |
1426 | return ret; | |
1427 | } | |
1428 | ||
1429 | return nb_clusters; | |
1430 | } | |
1431 | ||
1432 | int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors) | |
1433 | { | |
1434 | BDRVQcowState *s = bs->opaque; | |
1435 | unsigned int nb_clusters; | |
1436 | int ret; | |
1437 | ||
1438 | /* The zero flag is only supported by version 3 and newer */ | |
1439 | if (s->qcow_version < 3) { | |
1440 | return -ENOTSUP; | |
1441 | } | |
1442 | ||
1443 | /* Each L2 table is handled by its own loop iteration */ | |
1444 | nb_clusters = size_to_clusters(s, nb_sectors << BDRV_SECTOR_BITS); | |
1445 | ||
1446 | while (nb_clusters > 0) { | |
1447 | ret = zero_single_l2(bs, offset, nb_clusters); | |
1448 | if (ret < 0) { | |
1449 | return ret; | |
1450 | } | |
1451 | ||
1452 | nb_clusters -= ret; | |
1453 | offset += (ret * s->cluster_size); | |
1454 | } | |
1455 | ||
1456 | return 0; | |
1457 | } |