]>
Commit | Line | Data |
---|---|---|
585f8587 FB |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
5fafdf24 | 3 | * |
585f8587 | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
5fafdf24 | 5 | * |
585f8587 FB |
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 | */ | |
e688df6b | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
2714f13d | 26 | |
609f45ea | 27 | #include "block/qdict.h" |
23588797 | 28 | #include "sysemu/block-backend.h" |
db725815 | 29 | #include "qemu/main-loop.h" |
1de7afc9 | 30 | #include "qemu/module.h" |
0d8c41da | 31 | #include "qcow2.h" |
1de7afc9 | 32 | #include "qemu/error-report.h" |
e688df6b | 33 | #include "qapi/error.h" |
9af23989 | 34 | #include "qapi/qapi-events-block-core.h" |
6b673957 MA |
35 | #include "qapi/qmp/qdict.h" |
36 | #include "qapi/qmp/qstring.h" | |
3cce16f4 | 37 | #include "trace.h" |
1bd0e2d1 | 38 | #include "qemu/option_int.h" |
f348b6d1 | 39 | #include "qemu/cutils.h" |
58369e22 | 40 | #include "qemu/bswap.h" |
b76b4f60 KW |
41 | #include "qapi/qobject-input-visitor.h" |
42 | #include "qapi/qapi-visit-block-core.h" | |
0d8c41da | 43 | #include "crypto.h" |
d710cf57 | 44 | #include "block/aio_task.h" |
585f8587 FB |
45 | |
46 | /* | |
47 | Differences with QCOW: | |
48 | ||
49 | - Support for multiple incremental snapshots. | |
50 | - Memory management by reference counts. | |
51 | - Clusters which have a reference count of one have the bit | |
52 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 53 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
54 | in the cluster offsets. |
55 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 56 | snapshots. |
585f8587 FB |
57 | - If a backing store is used, the cluster size is not constrained |
58 | (could be backported to QCOW). | |
59 | - L2 tables have always a size of one cluster. | |
60 | */ | |
61 | ||
9b80ddf3 AL |
62 | |
63 | typedef struct { | |
64 | uint32_t magic; | |
65 | uint32_t len; | |
c4217f64 | 66 | } QEMU_PACKED QCowExtension; |
21d82ac9 | 67 | |
7c80ab3f JS |
68 | #define QCOW2_EXT_MAGIC_END 0 |
69 | #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA | |
cfcc4c62 | 70 | #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 |
4652b8f3 | 71 | #define QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77 |
88ddffae | 72 | #define QCOW2_EXT_MAGIC_BITMAPS 0x23852875 |
93c24936 | 73 | #define QCOW2_EXT_MAGIC_DATA_FILE 0x44415441 |
9b80ddf3 | 74 | |
c3c10f72 VSO |
75 | static int coroutine_fn |
76 | qcow2_co_preadv_compressed(BlockDriverState *bs, | |
77 | uint64_t file_cluster_offset, | |
78 | uint64_t offset, | |
79 | uint64_t bytes, | |
df893d25 VSO |
80 | QEMUIOVector *qiov, |
81 | size_t qiov_offset); | |
c3c10f72 | 82 | |
7c80ab3f | 83 | static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) |
585f8587 FB |
84 | { |
85 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 86 | |
585f8587 FB |
87 | if (buf_size >= sizeof(QCowHeader) && |
88 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
6744cbab | 89 | be32_to_cpu(cow_header->version) >= 2) |
585f8587 FB |
90 | return 100; |
91 | else | |
92 | return 0; | |
93 | } | |
94 | ||
9b80ddf3 | 95 | |
4652b8f3 DB |
96 | static ssize_t qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset, |
97 | uint8_t *buf, size_t buflen, | |
98 | void *opaque, Error **errp) | |
99 | { | |
100 | BlockDriverState *bs = opaque; | |
101 | BDRVQcow2State *s = bs->opaque; | |
102 | ssize_t ret; | |
103 | ||
104 | if ((offset + buflen) > s->crypto_header.length) { | |
105 | error_setg(errp, "Request for data outside of extension header"); | |
106 | return -1; | |
107 | } | |
108 | ||
109 | ret = bdrv_pread(bs->file, | |
110 | s->crypto_header.offset + offset, buf, buflen); | |
111 | if (ret < 0) { | |
112 | error_setg_errno(errp, -ret, "Could not read encryption header"); | |
113 | return -1; | |
114 | } | |
115 | return ret; | |
116 | } | |
117 | ||
118 | ||
119 | static ssize_t qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, | |
120 | void *opaque, Error **errp) | |
121 | { | |
122 | BlockDriverState *bs = opaque; | |
123 | BDRVQcow2State *s = bs->opaque; | |
124 | int64_t ret; | |
125 | int64_t clusterlen; | |
126 | ||
127 | ret = qcow2_alloc_clusters(bs, headerlen); | |
128 | if (ret < 0) { | |
129 | error_setg_errno(errp, -ret, | |
130 | "Cannot allocate cluster for LUKS header size %zu", | |
131 | headerlen); | |
132 | return -1; | |
133 | } | |
134 | ||
135 | s->crypto_header.length = headerlen; | |
136 | s->crypto_header.offset = ret; | |
137 | ||
087ab8e7 DB |
138 | /* |
139 | * Zero fill all space in cluster so it has predictable | |
140 | * content, as we may not initialize some regions of the | |
141 | * header (eg only 1 out of 8 key slots will be initialized) | |
142 | */ | |
4652b8f3 | 143 | clusterlen = size_to_clusters(s, headerlen) * s->cluster_size; |
966b000f | 144 | assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen, false) == 0); |
4652b8f3 | 145 | ret = bdrv_pwrite_zeroes(bs->file, |
087ab8e7 DB |
146 | ret, |
147 | clusterlen, 0); | |
4652b8f3 DB |
148 | if (ret < 0) { |
149 | error_setg_errno(errp, -ret, "Could not zero fill encryption header"); | |
150 | return -1; | |
151 | } | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
156 | ||
157 | static ssize_t qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset, | |
158 | const uint8_t *buf, size_t buflen, | |
159 | void *opaque, Error **errp) | |
160 | { | |
161 | BlockDriverState *bs = opaque; | |
162 | BDRVQcow2State *s = bs->opaque; | |
163 | ssize_t ret; | |
164 | ||
165 | if ((offset + buflen) > s->crypto_header.length) { | |
166 | error_setg(errp, "Request for data outside of extension header"); | |
167 | return -1; | |
168 | } | |
169 | ||
170 | ret = bdrv_pwrite(bs->file, | |
171 | s->crypto_header.offset + offset, buf, buflen); | |
172 | if (ret < 0) { | |
173 | error_setg_errno(errp, -ret, "Could not read encryption header"); | |
174 | return -1; | |
175 | } | |
176 | return ret; | |
177 | } | |
178 | ||
179 | ||
9b80ddf3 AL |
180 | /* |
181 | * read qcow2 extension and fill bs | |
182 | * start reading from start_offset | |
183 | * finish reading upon magic of value 0 or when end_offset reached | |
184 | * unknown magic is skipped (future extension this version knows nothing about) | |
185 | * return 0 upon success, non-0 otherwise | |
186 | */ | |
7c80ab3f | 187 | static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
3ef6c40a | 188 | uint64_t end_offset, void **p_feature_table, |
88ddffae VSO |
189 | int flags, bool *need_update_header, |
190 | Error **errp) | |
9b80ddf3 | 191 | { |
ff99129a | 192 | BDRVQcow2State *s = bs->opaque; |
9b80ddf3 AL |
193 | QCowExtension ext; |
194 | uint64_t offset; | |
75bab85c | 195 | int ret; |
88ddffae VSO |
196 | Qcow2BitmapHeaderExt bitmaps_ext; |
197 | ||
198 | if (need_update_header != NULL) { | |
199 | *need_update_header = false; | |
200 | } | |
9b80ddf3 AL |
201 | |
202 | #ifdef DEBUG_EXT | |
7c80ab3f | 203 | printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); |
9b80ddf3 AL |
204 | #endif |
205 | offset = start_offset; | |
206 | while (offset < end_offset) { | |
207 | ||
208 | #ifdef DEBUG_EXT | |
209 | /* Sanity check */ | |
210 | if (offset > s->cluster_size) | |
7c80ab3f | 211 | printf("qcow2_read_extension: suspicious offset %lu\n", offset); |
9b80ddf3 | 212 | |
9b2260cb | 213 | printf("attempting to read extended header in offset %lu\n", offset); |
9b80ddf3 AL |
214 | #endif |
215 | ||
cf2ab8fc | 216 | ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext)); |
3ef6c40a HR |
217 | if (ret < 0) { |
218 | error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: " | |
219 | "pread fail from offset %" PRIu64, offset); | |
9b80ddf3 AL |
220 | return 1; |
221 | } | |
3b698f52 PM |
222 | ext.magic = be32_to_cpu(ext.magic); |
223 | ext.len = be32_to_cpu(ext.len); | |
9b80ddf3 AL |
224 | offset += sizeof(ext); |
225 | #ifdef DEBUG_EXT | |
226 | printf("ext.magic = 0x%x\n", ext.magic); | |
227 | #endif | |
2ebafc85 | 228 | if (offset > end_offset || ext.len > end_offset - offset) { |
3ef6c40a | 229 | error_setg(errp, "Header extension too large"); |
64ca6aee KW |
230 | return -EINVAL; |
231 | } | |
232 | ||
9b80ddf3 | 233 | switch (ext.magic) { |
7c80ab3f | 234 | case QCOW2_EXT_MAGIC_END: |
9b80ddf3 | 235 | return 0; |
f965509c | 236 | |
7c80ab3f | 237 | case QCOW2_EXT_MAGIC_BACKING_FORMAT: |
f965509c | 238 | if (ext.len >= sizeof(bs->backing_format)) { |
521b2b5d HR |
239 | error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32 |
240 | " too large (>=%zu)", ext.len, | |
241 | sizeof(bs->backing_format)); | |
f965509c AL |
242 | return 2; |
243 | } | |
cf2ab8fc | 244 | ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len); |
3ef6c40a HR |
245 | if (ret < 0) { |
246 | error_setg_errno(errp, -ret, "ERROR: ext_backing_format: " | |
247 | "Could not read format name"); | |
f965509c | 248 | return 3; |
3ef6c40a | 249 | } |
f965509c | 250 | bs->backing_format[ext.len] = '\0'; |
e4603fe1 | 251 | s->image_backing_format = g_strdup(bs->backing_format); |
f965509c AL |
252 | #ifdef DEBUG_EXT |
253 | printf("Qcow2: Got format extension %s\n", bs->backing_format); | |
254 | #endif | |
f965509c AL |
255 | break; |
256 | ||
cfcc4c62 KW |
257 | case QCOW2_EXT_MAGIC_FEATURE_TABLE: |
258 | if (p_feature_table != NULL) { | |
259 | void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); | |
cf2ab8fc | 260 | ret = bdrv_pread(bs->file, offset , feature_table, ext.len); |
cfcc4c62 | 261 | if (ret < 0) { |
3ef6c40a HR |
262 | error_setg_errno(errp, -ret, "ERROR: ext_feature_table: " |
263 | "Could not read table"); | |
cfcc4c62 KW |
264 | return ret; |
265 | } | |
266 | ||
267 | *p_feature_table = feature_table; | |
268 | } | |
269 | break; | |
270 | ||
4652b8f3 DB |
271 | case QCOW2_EXT_MAGIC_CRYPTO_HEADER: { |
272 | unsigned int cflags = 0; | |
273 | if (s->crypt_method_header != QCOW_CRYPT_LUKS) { | |
274 | error_setg(errp, "CRYPTO header extension only " | |
275 | "expected with LUKS encryption method"); | |
276 | return -EINVAL; | |
277 | } | |
278 | if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) { | |
279 | error_setg(errp, "CRYPTO header extension size %u, " | |
280 | "but expected size %zu", ext.len, | |
281 | sizeof(Qcow2CryptoHeaderExtension)); | |
282 | return -EINVAL; | |
283 | } | |
284 | ||
285 | ret = bdrv_pread(bs->file, offset, &s->crypto_header, ext.len); | |
286 | if (ret < 0) { | |
287 | error_setg_errno(errp, -ret, | |
288 | "Unable to read CRYPTO header extension"); | |
289 | return ret; | |
290 | } | |
3b698f52 PM |
291 | s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset); |
292 | s->crypto_header.length = be64_to_cpu(s->crypto_header.length); | |
4652b8f3 DB |
293 | |
294 | if ((s->crypto_header.offset % s->cluster_size) != 0) { | |
295 | error_setg(errp, "Encryption header offset '%" PRIu64 "' is " | |
296 | "not a multiple of cluster size '%u'", | |
297 | s->crypto_header.offset, s->cluster_size); | |
298 | return -EINVAL; | |
299 | } | |
300 | ||
301 | if (flags & BDRV_O_NO_IO) { | |
302 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; | |
303 | } | |
1cd9a787 | 304 | s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.", |
4652b8f3 | 305 | qcow2_crypto_hdr_read_func, |
8ac0f15f | 306 | bs, cflags, QCOW2_MAX_THREADS, errp); |
4652b8f3 DB |
307 | if (!s->crypto) { |
308 | return -EINVAL; | |
309 | } | |
310 | } break; | |
311 | ||
88ddffae VSO |
312 | case QCOW2_EXT_MAGIC_BITMAPS: |
313 | if (ext.len != sizeof(bitmaps_ext)) { | |
314 | error_setg_errno(errp, -ret, "bitmaps_ext: " | |
315 | "Invalid extension length"); | |
316 | return -EINVAL; | |
317 | } | |
318 | ||
319 | if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) { | |
c9ceb3ec HR |
320 | if (s->qcow_version < 3) { |
321 | /* Let's be a bit more specific */ | |
322 | warn_report("This qcow2 v2 image contains bitmaps, but " | |
323 | "they may have been modified by a program " | |
324 | "without persistent bitmap support; so now " | |
325 | "they must all be considered inconsistent"); | |
326 | } else { | |
327 | warn_report("a program lacking bitmap support " | |
328 | "modified this file, so all bitmaps are now " | |
329 | "considered inconsistent"); | |
330 | } | |
55d527a9 AF |
331 | error_printf("Some clusters may be leaked, " |
332 | "run 'qemu-img check -r' on the image " | |
88ddffae VSO |
333 | "file to fix."); |
334 | if (need_update_header != NULL) { | |
335 | /* Updating is needed to drop invalid bitmap extension. */ | |
336 | *need_update_header = true; | |
337 | } | |
338 | break; | |
339 | } | |
340 | ||
341 | ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len); | |
342 | if (ret < 0) { | |
343 | error_setg_errno(errp, -ret, "bitmaps_ext: " | |
344 | "Could not read ext header"); | |
345 | return ret; | |
346 | } | |
347 | ||
348 | if (bitmaps_ext.reserved32 != 0) { | |
349 | error_setg_errno(errp, -ret, "bitmaps_ext: " | |
350 | "Reserved field is not zero"); | |
351 | return -EINVAL; | |
352 | } | |
353 | ||
3b698f52 PM |
354 | bitmaps_ext.nb_bitmaps = be32_to_cpu(bitmaps_ext.nb_bitmaps); |
355 | bitmaps_ext.bitmap_directory_size = | |
356 | be64_to_cpu(bitmaps_ext.bitmap_directory_size); | |
357 | bitmaps_ext.bitmap_directory_offset = | |
358 | be64_to_cpu(bitmaps_ext.bitmap_directory_offset); | |
88ddffae VSO |
359 | |
360 | if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) { | |
361 | error_setg(errp, | |
362 | "bitmaps_ext: Image has %" PRIu32 " bitmaps, " | |
363 | "exceeding the QEMU supported maximum of %d", | |
364 | bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS); | |
365 | return -EINVAL; | |
366 | } | |
367 | ||
368 | if (bitmaps_ext.nb_bitmaps == 0) { | |
369 | error_setg(errp, "found bitmaps extension with zero bitmaps"); | |
370 | return -EINVAL; | |
371 | } | |
372 | ||
74e60fb5 | 373 | if (offset_into_cluster(s, bitmaps_ext.bitmap_directory_offset)) { |
88ddffae VSO |
374 | error_setg(errp, "bitmaps_ext: " |
375 | "invalid bitmap directory offset"); | |
376 | return -EINVAL; | |
377 | } | |
378 | ||
379 | if (bitmaps_ext.bitmap_directory_size > | |
380 | QCOW2_MAX_BITMAP_DIRECTORY_SIZE) { | |
381 | error_setg(errp, "bitmaps_ext: " | |
382 | "bitmap directory size (%" PRIu64 ") exceeds " | |
383 | "the maximum supported size (%d)", | |
384 | bitmaps_ext.bitmap_directory_size, | |
385 | QCOW2_MAX_BITMAP_DIRECTORY_SIZE); | |
386 | return -EINVAL; | |
387 | } | |
388 | ||
389 | s->nb_bitmaps = bitmaps_ext.nb_bitmaps; | |
390 | s->bitmap_directory_offset = | |
391 | bitmaps_ext.bitmap_directory_offset; | |
392 | s->bitmap_directory_size = | |
393 | bitmaps_ext.bitmap_directory_size; | |
394 | ||
395 | #ifdef DEBUG_EXT | |
396 | printf("Qcow2: Got bitmaps extension: " | |
397 | "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n", | |
398 | s->bitmap_directory_offset, s->nb_bitmaps); | |
399 | #endif | |
400 | break; | |
401 | ||
9b890bdc KW |
402 | case QCOW2_EXT_MAGIC_DATA_FILE: |
403 | { | |
404 | s->image_data_file = g_malloc0(ext.len + 1); | |
405 | ret = bdrv_pread(bs->file, offset, s->image_data_file, ext.len); | |
406 | if (ret < 0) { | |
407 | error_setg_errno(errp, -ret, | |
408 | "ERROR: Could not read data file name"); | |
409 | return ret; | |
410 | } | |
411 | #ifdef DEBUG_EXT | |
412 | printf("Qcow2: Got external data file %s\n", s->image_data_file); | |
413 | #endif | |
414 | break; | |
415 | } | |
416 | ||
9b80ddf3 | 417 | default: |
75bab85c | 418 | /* unknown magic - save it in case we need to rewrite the header */ |
4096974e EB |
419 | /* If you add a new feature, make sure to also update the fast |
420 | * path of qcow2_make_empty() to deal with it. */ | |
75bab85c KW |
421 | { |
422 | Qcow2UnknownHeaderExtension *uext; | |
423 | ||
424 | uext = g_malloc0(sizeof(*uext) + ext.len); | |
425 | uext->magic = ext.magic; | |
426 | uext->len = ext.len; | |
427 | QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); | |
428 | ||
cf2ab8fc | 429 | ret = bdrv_pread(bs->file, offset , uext->data, uext->len); |
75bab85c | 430 | if (ret < 0) { |
3ef6c40a HR |
431 | error_setg_errno(errp, -ret, "ERROR: unknown extension: " |
432 | "Could not read data"); | |
75bab85c KW |
433 | return ret; |
434 | } | |
75bab85c | 435 | } |
9b80ddf3 AL |
436 | break; |
437 | } | |
fd29b4bb KW |
438 | |
439 | offset += ((ext.len + 7) & ~7); | |
9b80ddf3 AL |
440 | } |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
75bab85c KW |
445 | static void cleanup_unknown_header_ext(BlockDriverState *bs) |
446 | { | |
ff99129a | 447 | BDRVQcow2State *s = bs->opaque; |
75bab85c KW |
448 | Qcow2UnknownHeaderExtension *uext, *next; |
449 | ||
450 | QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { | |
451 | QLIST_REMOVE(uext, next); | |
452 | g_free(uext); | |
453 | } | |
454 | } | |
9b80ddf3 | 455 | |
a55448b3 HR |
456 | static void report_unsupported_feature(Error **errp, Qcow2Feature *table, |
457 | uint64_t mask) | |
cfcc4c62 | 458 | { |
7cdca2e2 | 459 | g_autoptr(GString) features = g_string_sized_new(60); |
12ac6d3d | 460 | |
cfcc4c62 KW |
461 | while (table && table->name[0] != '\0') { |
462 | if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { | |
12ac6d3d | 463 | if (mask & (1ULL << table->bit)) { |
7cdca2e2 AG |
464 | if (features->len > 0) { |
465 | g_string_append(features, ", "); | |
466 | } | |
467 | g_string_append_printf(features, "%.46s", table->name); | |
12ac6d3d | 468 | mask &= ~(1ULL << table->bit); |
cfcc4c62 KW |
469 | } |
470 | } | |
471 | table++; | |
472 | } | |
473 | ||
474 | if (mask) { | |
7cdca2e2 AG |
475 | if (features->len > 0) { |
476 | g_string_append(features, ", "); | |
477 | } | |
478 | g_string_append_printf(features, | |
479 | "Unknown incompatible feature: %" PRIx64, mask); | |
cfcc4c62 | 480 | } |
12ac6d3d | 481 | |
7cdca2e2 | 482 | error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str); |
cfcc4c62 KW |
483 | } |
484 | ||
bfe8043e SH |
485 | /* |
486 | * Sets the dirty bit and flushes afterwards if necessary. | |
487 | * | |
488 | * The incompatible_features bit is only set if the image file header was | |
489 | * updated successfully. Therefore it is not required to check the return | |
490 | * value of this function. | |
491 | */ | |
280d3735 | 492 | int qcow2_mark_dirty(BlockDriverState *bs) |
bfe8043e | 493 | { |
ff99129a | 494 | BDRVQcow2State *s = bs->opaque; |
bfe8043e SH |
495 | uint64_t val; |
496 | int ret; | |
497 | ||
498 | assert(s->qcow_version >= 3); | |
499 | ||
500 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
501 | return 0; /* already dirty */ | |
502 | } | |
503 | ||
504 | val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); | |
d9ca2ea2 | 505 | ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), |
bfe8043e SH |
506 | &val, sizeof(val)); |
507 | if (ret < 0) { | |
508 | return ret; | |
509 | } | |
9a4f4c31 | 510 | ret = bdrv_flush(bs->file->bs); |
bfe8043e SH |
511 | if (ret < 0) { |
512 | return ret; | |
513 | } | |
514 | ||
515 | /* Only treat image as dirty if the header was updated successfully */ | |
516 | s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; | |
517 | return 0; | |
518 | } | |
519 | ||
c61d0004 SH |
520 | /* |
521 | * Clears the dirty bit and flushes before if necessary. Only call this | |
522 | * function when there are no pending requests, it does not guard against | |
523 | * concurrent requests dirtying the image. | |
524 | */ | |
525 | static int qcow2_mark_clean(BlockDriverState *bs) | |
526 | { | |
ff99129a | 527 | BDRVQcow2State *s = bs->opaque; |
c61d0004 SH |
528 | |
529 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
4c2e5f8f KW |
530 | int ret; |
531 | ||
532 | s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; | |
533 | ||
8b220eb7 | 534 | ret = qcow2_flush_caches(bs); |
c61d0004 SH |
535 | if (ret < 0) { |
536 | return ret; | |
537 | } | |
538 | ||
c61d0004 SH |
539 | return qcow2_update_header(bs); |
540 | } | |
541 | return 0; | |
542 | } | |
543 | ||
69c98726 HR |
544 | /* |
545 | * Marks the image as corrupt. | |
546 | */ | |
547 | int qcow2_mark_corrupt(BlockDriverState *bs) | |
548 | { | |
ff99129a | 549 | BDRVQcow2State *s = bs->opaque; |
69c98726 HR |
550 | |
551 | s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT; | |
552 | return qcow2_update_header(bs); | |
553 | } | |
554 | ||
555 | /* | |
556 | * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes | |
557 | * before if necessary. | |
558 | */ | |
559 | int qcow2_mark_consistent(BlockDriverState *bs) | |
560 | { | |
ff99129a | 561 | BDRVQcow2State *s = bs->opaque; |
69c98726 HR |
562 | |
563 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { | |
8b220eb7 | 564 | int ret = qcow2_flush_caches(bs); |
69c98726 HR |
565 | if (ret < 0) { |
566 | return ret; | |
567 | } | |
568 | ||
569 | s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT; | |
570 | return qcow2_update_header(bs); | |
571 | } | |
572 | return 0; | |
573 | } | |
574 | ||
8bc584fe HR |
575 | static void qcow2_add_check_result(BdrvCheckResult *out, |
576 | const BdrvCheckResult *src, | |
577 | bool set_allocation_info) | |
578 | { | |
579 | out->corruptions += src->corruptions; | |
580 | out->leaks += src->leaks; | |
581 | out->check_errors += src->check_errors; | |
582 | out->corruptions_fixed += src->corruptions_fixed; | |
583 | out->leaks_fixed += src->leaks_fixed; | |
584 | ||
585 | if (set_allocation_info) { | |
586 | out->image_end_offset = src->image_end_offset; | |
587 | out->bfi = src->bfi; | |
588 | } | |
589 | } | |
590 | ||
2fd61638 PB |
591 | static int coroutine_fn qcow2_co_check_locked(BlockDriverState *bs, |
592 | BdrvCheckResult *result, | |
593 | BdrvCheckMode fix) | |
acbe5982 | 594 | { |
8bc584fe HR |
595 | BdrvCheckResult snapshot_res = {}; |
596 | BdrvCheckResult refcount_res = {}; | |
597 | int ret; | |
598 | ||
599 | memset(result, 0, sizeof(*result)); | |
600 | ||
601 | ret = qcow2_check_read_snapshot_table(bs, &snapshot_res, fix); | |
8bc584fe | 602 | if (ret < 0) { |
fe446b5d | 603 | qcow2_add_check_result(result, &snapshot_res, false); |
8bc584fe HR |
604 | return ret; |
605 | } | |
606 | ||
607 | ret = qcow2_check_refcounts(bs, &refcount_res, fix); | |
608 | qcow2_add_check_result(result, &refcount_res, true); | |
fe446b5d HR |
609 | if (ret < 0) { |
610 | qcow2_add_check_result(result, &snapshot_res, false); | |
611 | return ret; | |
612 | } | |
613 | ||
614 | ret = qcow2_check_fix_snapshot_table(bs, &snapshot_res, fix); | |
615 | qcow2_add_check_result(result, &snapshot_res, false); | |
acbe5982 SH |
616 | if (ret < 0) { |
617 | return ret; | |
618 | } | |
619 | ||
620 | if (fix && result->check_errors == 0 && result->corruptions == 0) { | |
24530f3e HR |
621 | ret = qcow2_mark_clean(bs); |
622 | if (ret < 0) { | |
623 | return ret; | |
624 | } | |
625 | return qcow2_mark_consistent(bs); | |
acbe5982 SH |
626 | } |
627 | return ret; | |
628 | } | |
629 | ||
2fd61638 PB |
630 | static int coroutine_fn qcow2_co_check(BlockDriverState *bs, |
631 | BdrvCheckResult *result, | |
632 | BdrvCheckMode fix) | |
633 | { | |
634 | BDRVQcow2State *s = bs->opaque; | |
635 | int ret; | |
636 | ||
637 | qemu_co_mutex_lock(&s->lock); | |
638 | ret = qcow2_co_check_locked(bs, result, fix); | |
639 | qemu_co_mutex_unlock(&s->lock); | |
640 | return ret; | |
641 | } | |
642 | ||
0cf0e598 AG |
643 | int qcow2_validate_table(BlockDriverState *bs, uint64_t offset, |
644 | uint64_t entries, size_t entry_len, | |
645 | int64_t max_size_bytes, const char *table_name, | |
646 | Error **errp) | |
8c7de283 | 647 | { |
ff99129a | 648 | BDRVQcow2State *s = bs->opaque; |
8c7de283 | 649 | |
0cf0e598 AG |
650 | if (entries > max_size_bytes / entry_len) { |
651 | error_setg(errp, "%s too large", table_name); | |
652 | return -EFBIG; | |
8c7de283 KW |
653 | } |
654 | ||
0cf0e598 AG |
655 | /* Use signed INT64_MAX as the maximum even for uint64_t header fields, |
656 | * because values will be passed to qemu functions taking int64_t. */ | |
657 | if ((INT64_MAX - entries * entry_len < offset) || | |
658 | (offset_into_cluster(s, offset) != 0)) { | |
659 | error_setg(errp, "%s offset invalid", table_name); | |
8c7de283 KW |
660 | return -EINVAL; |
661 | } | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
8a2ce0bc AG |
666 | static const char *const mutable_opts[] = { |
667 | QCOW2_OPT_LAZY_REFCOUNTS, | |
668 | QCOW2_OPT_DISCARD_REQUEST, | |
669 | QCOW2_OPT_DISCARD_SNAPSHOT, | |
670 | QCOW2_OPT_DISCARD_OTHER, | |
671 | QCOW2_OPT_OVERLAP, | |
672 | QCOW2_OPT_OVERLAP_TEMPLATE, | |
673 | QCOW2_OPT_OVERLAP_MAIN_HEADER, | |
674 | QCOW2_OPT_OVERLAP_ACTIVE_L1, | |
675 | QCOW2_OPT_OVERLAP_ACTIVE_L2, | |
676 | QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, | |
677 | QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, | |
678 | QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, | |
679 | QCOW2_OPT_OVERLAP_INACTIVE_L1, | |
680 | QCOW2_OPT_OVERLAP_INACTIVE_L2, | |
681 | QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, | |
682 | QCOW2_OPT_CACHE_SIZE, | |
683 | QCOW2_OPT_L2_CACHE_SIZE, | |
684 | QCOW2_OPT_L2_CACHE_ENTRY_SIZE, | |
685 | QCOW2_OPT_REFCOUNT_CACHE_SIZE, | |
686 | QCOW2_OPT_CACHE_CLEAN_INTERVAL, | |
687 | NULL | |
688 | }; | |
689 | ||
74c4510a KW |
690 | static QemuOptsList qcow2_runtime_opts = { |
691 | .name = "qcow2", | |
692 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), | |
693 | .desc = { | |
694 | { | |
64aa99d3 | 695 | .name = QCOW2_OPT_LAZY_REFCOUNTS, |
74c4510a KW |
696 | .type = QEMU_OPT_BOOL, |
697 | .help = "Postpone refcount updates", | |
698 | }, | |
67af674e KW |
699 | { |
700 | .name = QCOW2_OPT_DISCARD_REQUEST, | |
701 | .type = QEMU_OPT_BOOL, | |
702 | .help = "Pass guest discard requests to the layer below", | |
703 | }, | |
704 | { | |
705 | .name = QCOW2_OPT_DISCARD_SNAPSHOT, | |
706 | .type = QEMU_OPT_BOOL, | |
707 | .help = "Generate discard requests when snapshot related space " | |
708 | "is freed", | |
709 | }, | |
710 | { | |
711 | .name = QCOW2_OPT_DISCARD_OTHER, | |
712 | .type = QEMU_OPT_BOOL, | |
713 | .help = "Generate discard requests when other clusters are freed", | |
714 | }, | |
05de7e86 HR |
715 | { |
716 | .name = QCOW2_OPT_OVERLAP, | |
717 | .type = QEMU_OPT_STRING, | |
718 | .help = "Selects which overlap checks to perform from a range of " | |
719 | "templates (none, constant, cached, all)", | |
720 | }, | |
ee42b5ce HR |
721 | { |
722 | .name = QCOW2_OPT_OVERLAP_TEMPLATE, | |
723 | .type = QEMU_OPT_STRING, | |
724 | .help = "Selects which overlap checks to perform from a range of " | |
725 | "templates (none, constant, cached, all)", | |
726 | }, | |
05de7e86 HR |
727 | { |
728 | .name = QCOW2_OPT_OVERLAP_MAIN_HEADER, | |
729 | .type = QEMU_OPT_BOOL, | |
730 | .help = "Check for unintended writes into the main qcow2 header", | |
731 | }, | |
732 | { | |
733 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L1, | |
734 | .type = QEMU_OPT_BOOL, | |
735 | .help = "Check for unintended writes into the active L1 table", | |
736 | }, | |
737 | { | |
738 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L2, | |
739 | .type = QEMU_OPT_BOOL, | |
740 | .help = "Check for unintended writes into an active L2 table", | |
741 | }, | |
742 | { | |
743 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, | |
744 | .type = QEMU_OPT_BOOL, | |
745 | .help = "Check for unintended writes into the refcount table", | |
746 | }, | |
747 | { | |
748 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, | |
749 | .type = QEMU_OPT_BOOL, | |
750 | .help = "Check for unintended writes into a refcount block", | |
751 | }, | |
752 | { | |
753 | .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, | |
754 | .type = QEMU_OPT_BOOL, | |
755 | .help = "Check for unintended writes into the snapshot table", | |
756 | }, | |
757 | { | |
758 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L1, | |
759 | .type = QEMU_OPT_BOOL, | |
760 | .help = "Check for unintended writes into an inactive L1 table", | |
761 | }, | |
762 | { | |
763 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L2, | |
764 | .type = QEMU_OPT_BOOL, | |
765 | .help = "Check for unintended writes into an inactive L2 table", | |
766 | }, | |
0e4e4318 VSO |
767 | { |
768 | .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, | |
769 | .type = QEMU_OPT_BOOL, | |
770 | .help = "Check for unintended writes into the bitmap directory", | |
771 | }, | |
6c1c8d5d HR |
772 | { |
773 | .name = QCOW2_OPT_CACHE_SIZE, | |
774 | .type = QEMU_OPT_SIZE, | |
775 | .help = "Maximum combined metadata (L2 tables and refcount blocks) " | |
776 | "cache size", | |
777 | }, | |
778 | { | |
779 | .name = QCOW2_OPT_L2_CACHE_SIZE, | |
780 | .type = QEMU_OPT_SIZE, | |
781 | .help = "Maximum L2 table cache size", | |
782 | }, | |
1221fe6f AG |
783 | { |
784 | .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE, | |
785 | .type = QEMU_OPT_SIZE, | |
786 | .help = "Size of each entry in the L2 cache", | |
787 | }, | |
6c1c8d5d HR |
788 | { |
789 | .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE, | |
790 | .type = QEMU_OPT_SIZE, | |
791 | .help = "Maximum refcount block cache size", | |
792 | }, | |
279621c0 AG |
793 | { |
794 | .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL, | |
795 | .type = QEMU_OPT_NUMBER, | |
796 | .help = "Clean unused cache entries after this time (in seconds)", | |
797 | }, | |
4652b8f3 DB |
798 | BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.", |
799 | "ID of secret providing qcow2 AES key or LUKS passphrase"), | |
74c4510a KW |
800 | { /* end of list */ } |
801 | }, | |
802 | }; | |
803 | ||
4092e99d | 804 | static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { |
0e4e4318 VSO |
805 | [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER, |
806 | [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1, | |
807 | [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2, | |
808 | [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, | |
809 | [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, | |
810 | [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, | |
811 | [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1, | |
812 | [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, | |
813 | [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY, | |
4092e99d HR |
814 | }; |
815 | ||
279621c0 AG |
816 | static void cache_clean_timer_cb(void *opaque) |
817 | { | |
818 | BlockDriverState *bs = opaque; | |
ff99129a | 819 | BDRVQcow2State *s = bs->opaque; |
b2f68bff AG |
820 | qcow2_cache_clean_unused(s->l2_table_cache); |
821 | qcow2_cache_clean_unused(s->refcount_block_cache); | |
279621c0 AG |
822 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + |
823 | (int64_t) s->cache_clean_interval * 1000); | |
824 | } | |
825 | ||
826 | static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context) | |
827 | { | |
ff99129a | 828 | BDRVQcow2State *s = bs->opaque; |
279621c0 AG |
829 | if (s->cache_clean_interval > 0) { |
830 | s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL, | |
831 | SCALE_MS, cache_clean_timer_cb, | |
832 | bs); | |
833 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
834 | (int64_t) s->cache_clean_interval * 1000); | |
835 | } | |
836 | } | |
837 | ||
838 | static void cache_clean_timer_del(BlockDriverState *bs) | |
839 | { | |
ff99129a | 840 | BDRVQcow2State *s = bs->opaque; |
279621c0 AG |
841 | if (s->cache_clean_timer) { |
842 | timer_del(s->cache_clean_timer); | |
843 | timer_free(s->cache_clean_timer); | |
844 | s->cache_clean_timer = NULL; | |
845 | } | |
846 | } | |
847 | ||
848 | static void qcow2_detach_aio_context(BlockDriverState *bs) | |
849 | { | |
850 | cache_clean_timer_del(bs); | |
851 | } | |
852 | ||
853 | static void qcow2_attach_aio_context(BlockDriverState *bs, | |
854 | AioContext *new_context) | |
855 | { | |
856 | cache_clean_timer_init(bs, new_context); | |
857 | } | |
858 | ||
bc85ef26 HR |
859 | static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts, |
860 | uint64_t *l2_cache_size, | |
1221fe6f | 861 | uint64_t *l2_cache_entry_size, |
6c1c8d5d HR |
862 | uint64_t *refcount_cache_size, Error **errp) |
863 | { | |
ff99129a | 864 | BDRVQcow2State *s = bs->opaque; |
b749562d | 865 | uint64_t combined_cache_size, l2_cache_max_setting; |
6c1c8d5d | 866 | bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set; |
af39bd0d | 867 | bool l2_cache_entry_size_set; |
7af5eea9 | 868 | int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size; |
b749562d | 869 | uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
b70d0820 AG |
870 | uint64_t max_l2_entries = DIV_ROUND_UP(virtual_disk_size, s->cluster_size); |
871 | /* An L2 table is always one cluster in size so the max cache size | |
872 | * should be a multiple of the cluster size. */ | |
873 | uint64_t max_l2_cache = ROUND_UP(max_l2_entries * sizeof(uint64_t), | |
874 | s->cluster_size); | |
6c1c8d5d HR |
875 | |
876 | combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE); | |
877 | l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE); | |
878 | refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
af39bd0d | 879 | l2_cache_entry_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE); |
6c1c8d5d HR |
880 | |
881 | combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0); | |
b749562d LB |
882 | l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, |
883 | DEFAULT_L2_CACHE_MAX_SIZE); | |
6c1c8d5d HR |
884 | *refcount_cache_size = qemu_opt_get_size(opts, |
885 | QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0); | |
886 | ||
1221fe6f AG |
887 | *l2_cache_entry_size = qemu_opt_get_size( |
888 | opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size); | |
889 | ||
b749562d LB |
890 | *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting); |
891 | ||
6c1c8d5d HR |
892 | if (combined_cache_size_set) { |
893 | if (l2_cache_size_set && refcount_cache_size_set) { | |
894 | error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE | |
895 | " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set " | |
308999e9 | 896 | "at the same time"); |
6c1c8d5d | 897 | return; |
b749562d LB |
898 | } else if (l2_cache_size_set && |
899 | (l2_cache_max_setting > combined_cache_size)) { | |
6c1c8d5d HR |
900 | error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed " |
901 | QCOW2_OPT_CACHE_SIZE); | |
902 | return; | |
903 | } else if (*refcount_cache_size > combined_cache_size) { | |
904 | error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed " | |
905 | QCOW2_OPT_CACHE_SIZE); | |
906 | return; | |
907 | } | |
908 | ||
909 | if (l2_cache_size_set) { | |
910 | *refcount_cache_size = combined_cache_size - *l2_cache_size; | |
911 | } else if (refcount_cache_size_set) { | |
912 | *l2_cache_size = combined_cache_size - *refcount_cache_size; | |
913 | } else { | |
52253998 AG |
914 | /* Assign as much memory as possible to the L2 cache, and |
915 | * use the remainder for the refcount cache */ | |
916 | if (combined_cache_size >= max_l2_cache + min_refcount_cache) { | |
917 | *l2_cache_size = max_l2_cache; | |
918 | *refcount_cache_size = combined_cache_size - *l2_cache_size; | |
919 | } else { | |
920 | *refcount_cache_size = | |
921 | MIN(combined_cache_size, min_refcount_cache); | |
922 | *l2_cache_size = combined_cache_size - *refcount_cache_size; | |
923 | } | |
6c1c8d5d | 924 | } |
6c1c8d5d | 925 | } |
af39bd0d AG |
926 | |
927 | /* | |
928 | * If the L2 cache is not enough to cover the whole disk then | |
929 | * default to 4KB entries. Smaller entries reduce the cost of | |
930 | * loads and evictions and increase I/O performance. | |
931 | */ | |
932 | if (*l2_cache_size < max_l2_cache && !l2_cache_entry_size_set) { | |
933 | *l2_cache_entry_size = MIN(s->cluster_size, 4096); | |
934 | } | |
935 | ||
657ada52 LB |
936 | /* l2_cache_size and refcount_cache_size are ensured to have at least |
937 | * their minimum values in qcow2_update_options_prepare() */ | |
1221fe6f AG |
938 | |
939 | if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) || | |
940 | *l2_cache_entry_size > s->cluster_size || | |
941 | !is_power_of_2(*l2_cache_entry_size)) { | |
942 | error_setg(errp, "L2 cache entry size must be a power of two " | |
943 | "between %d and the cluster size (%d)", | |
944 | 1 << MIN_CLUSTER_BITS, s->cluster_size); | |
945 | return; | |
946 | } | |
6c1c8d5d HR |
947 | } |
948 | ||
ee55b173 KW |
949 | typedef struct Qcow2ReopenState { |
950 | Qcow2Cache *l2_table_cache; | |
951 | Qcow2Cache *refcount_block_cache; | |
3c2e511a | 952 | int l2_slice_size; /* Number of entries in a slice of the L2 table */ |
ee55b173 KW |
953 | bool use_lazy_refcounts; |
954 | int overlap_check; | |
955 | bool discard_passthrough[QCOW2_DISCARD_MAX]; | |
956 | uint64_t cache_clean_interval; | |
b25b387f | 957 | QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */ |
ee55b173 KW |
958 | } Qcow2ReopenState; |
959 | ||
960 | static int qcow2_update_options_prepare(BlockDriverState *bs, | |
961 | Qcow2ReopenState *r, | |
962 | QDict *options, int flags, | |
963 | Error **errp) | |
4c75d1a1 KW |
964 | { |
965 | BDRVQcow2State *s = bs->opaque; | |
94edf3fb | 966 | QemuOpts *opts = NULL; |
4c75d1a1 KW |
967 | const char *opt_overlap_check, *opt_overlap_check_template; |
968 | int overlap_check_template = 0; | |
1221fe6f | 969 | uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size; |
4c75d1a1 | 970 | int i; |
b25b387f DB |
971 | const char *encryptfmt; |
972 | QDict *encryptopts = NULL; | |
94edf3fb | 973 | Error *local_err = NULL; |
4c75d1a1 KW |
974 | int ret; |
975 | ||
b25b387f DB |
976 | qdict_extract_subqdict(options, &encryptopts, "encrypt."); |
977 | encryptfmt = qdict_get_try_str(encryptopts, "format"); | |
978 | ||
94edf3fb KW |
979 | opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort); |
980 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
981 | if (local_err) { | |
982 | error_propagate(errp, local_err); | |
983 | ret = -EINVAL; | |
984 | goto fail; | |
985 | } | |
986 | ||
987 | /* get L2 table/refcount block cache size from command line options */ | |
1221fe6f AG |
988 | read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size, |
989 | &refcount_cache_size, &local_err); | |
94edf3fb KW |
990 | if (local_err) { |
991 | error_propagate(errp, local_err); | |
992 | ret = -EINVAL; | |
993 | goto fail; | |
994 | } | |
995 | ||
1221fe6f | 996 | l2_cache_size /= l2_cache_entry_size; |
94edf3fb KW |
997 | if (l2_cache_size < MIN_L2_CACHE_SIZE) { |
998 | l2_cache_size = MIN_L2_CACHE_SIZE; | |
999 | } | |
1000 | if (l2_cache_size > INT_MAX) { | |
1001 | error_setg(errp, "L2 cache size too big"); | |
1002 | ret = -EINVAL; | |
1003 | goto fail; | |
1004 | } | |
1005 | ||
1006 | refcount_cache_size /= s->cluster_size; | |
1007 | if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) { | |
1008 | refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE; | |
1009 | } | |
1010 | if (refcount_cache_size > INT_MAX) { | |
1011 | error_setg(errp, "Refcount cache size too big"); | |
1012 | ret = -EINVAL; | |
1013 | goto fail; | |
1014 | } | |
1015 | ||
5b0959a7 KW |
1016 | /* alloc new L2 table/refcount block cache, flush old one */ |
1017 | if (s->l2_table_cache) { | |
1018 | ret = qcow2_cache_flush(bs, s->l2_table_cache); | |
1019 | if (ret) { | |
1020 | error_setg_errno(errp, -ret, "Failed to flush the L2 table cache"); | |
1021 | goto fail; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | if (s->refcount_block_cache) { | |
1026 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
1027 | if (ret) { | |
1028 | error_setg_errno(errp, -ret, | |
1029 | "Failed to flush the refcount block cache"); | |
1030 | goto fail; | |
1031 | } | |
1032 | } | |
1033 | ||
1221fe6f AG |
1034 | r->l2_slice_size = l2_cache_entry_size / sizeof(uint64_t); |
1035 | r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size, | |
1036 | l2_cache_entry_size); | |
1037 | r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size, | |
1038 | s->cluster_size); | |
ee55b173 | 1039 | if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) { |
94edf3fb KW |
1040 | error_setg(errp, "Could not allocate metadata caches"); |
1041 | ret = -ENOMEM; | |
1042 | goto fail; | |
1043 | } | |
1044 | ||
1045 | /* New interval for cache cleanup timer */ | |
ee55b173 | 1046 | r->cache_clean_interval = |
5b0959a7 | 1047 | qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL, |
e957b50b | 1048 | DEFAULT_CACHE_CLEAN_INTERVAL); |
91203f08 AG |
1049 | #ifndef CONFIG_LINUX |
1050 | if (r->cache_clean_interval != 0) { | |
1051 | error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL | |
1052 | " not supported on this host"); | |
1053 | ret = -EINVAL; | |
1054 | goto fail; | |
1055 | } | |
1056 | #endif | |
ee55b173 | 1057 | if (r->cache_clean_interval > UINT_MAX) { |
94edf3fb KW |
1058 | error_setg(errp, "Cache clean interval too big"); |
1059 | ret = -EINVAL; | |
1060 | goto fail; | |
1061 | } | |
94edf3fb | 1062 | |
5b0959a7 | 1063 | /* lazy-refcounts; flush if going from enabled to disabled */ |
ee55b173 | 1064 | r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, |
4c75d1a1 | 1065 | (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); |
ee55b173 | 1066 | if (r->use_lazy_refcounts && s->qcow_version < 3) { |
007dbc39 KW |
1067 | error_setg(errp, "Lazy refcounts require a qcow2 image with at least " |
1068 | "qemu 1.1 compatibility level"); | |
1069 | ret = -EINVAL; | |
1070 | goto fail; | |
1071 | } | |
4c75d1a1 | 1072 | |
5b0959a7 KW |
1073 | if (s->use_lazy_refcounts && !r->use_lazy_refcounts) { |
1074 | ret = qcow2_mark_clean(bs); | |
1075 | if (ret < 0) { | |
1076 | error_setg_errno(errp, -ret, "Failed to disable lazy refcounts"); | |
1077 | goto fail; | |
1078 | } | |
1079 | } | |
1080 | ||
007dbc39 | 1081 | /* Overlap check options */ |
4c75d1a1 KW |
1082 | opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP); |
1083 | opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE); | |
1084 | if (opt_overlap_check_template && opt_overlap_check && | |
1085 | strcmp(opt_overlap_check_template, opt_overlap_check)) | |
1086 | { | |
1087 | error_setg(errp, "Conflicting values for qcow2 options '" | |
1088 | QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE | |
1089 | "' ('%s')", opt_overlap_check, opt_overlap_check_template); | |
1090 | ret = -EINVAL; | |
1091 | goto fail; | |
1092 | } | |
1093 | if (!opt_overlap_check) { | |
1094 | opt_overlap_check = opt_overlap_check_template ?: "cached"; | |
1095 | } | |
1096 | ||
1097 | if (!strcmp(opt_overlap_check, "none")) { | |
1098 | overlap_check_template = 0; | |
1099 | } else if (!strcmp(opt_overlap_check, "constant")) { | |
1100 | overlap_check_template = QCOW2_OL_CONSTANT; | |
1101 | } else if (!strcmp(opt_overlap_check, "cached")) { | |
1102 | overlap_check_template = QCOW2_OL_CACHED; | |
1103 | } else if (!strcmp(opt_overlap_check, "all")) { | |
1104 | overlap_check_template = QCOW2_OL_ALL; | |
1105 | } else { | |
1106 | error_setg(errp, "Unsupported value '%s' for qcow2 option " | |
1107 | "'overlap-check'. Allowed are any of the following: " | |
1108 | "none, constant, cached, all", opt_overlap_check); | |
1109 | ret = -EINVAL; | |
1110 | goto fail; | |
1111 | } | |
1112 | ||
ee55b173 | 1113 | r->overlap_check = 0; |
4c75d1a1 KW |
1114 | for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) { |
1115 | /* overlap-check defines a template bitmask, but every flag may be | |
1116 | * overwritten through the associated boolean option */ | |
ee55b173 | 1117 | r->overlap_check |= |
4c75d1a1 KW |
1118 | qemu_opt_get_bool(opts, overlap_bool_option_names[i], |
1119 | overlap_check_template & (1 << i)) << i; | |
1120 | } | |
1121 | ||
ee55b173 KW |
1122 | r->discard_passthrough[QCOW2_DISCARD_NEVER] = false; |
1123 | r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; | |
1124 | r->discard_passthrough[QCOW2_DISCARD_REQUEST] = | |
007dbc39 KW |
1125 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, |
1126 | flags & BDRV_O_UNMAP); | |
ee55b173 | 1127 | r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = |
007dbc39 | 1128 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); |
ee55b173 | 1129 | r->discard_passthrough[QCOW2_DISCARD_OTHER] = |
007dbc39 KW |
1130 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); |
1131 | ||
b25b387f DB |
1132 | switch (s->crypt_method_header) { |
1133 | case QCOW_CRYPT_NONE: | |
1134 | if (encryptfmt) { | |
1135 | error_setg(errp, "No encryption in image header, but options " | |
1136 | "specified format '%s'", encryptfmt); | |
1137 | ret = -EINVAL; | |
1138 | goto fail; | |
1139 | } | |
1140 | break; | |
1141 | ||
1142 | case QCOW_CRYPT_AES: | |
1143 | if (encryptfmt && !g_str_equal(encryptfmt, "aes")) { | |
1144 | error_setg(errp, | |
1145 | "Header reported 'aes' encryption format but " | |
1146 | "options specify '%s'", encryptfmt); | |
1147 | ret = -EINVAL; | |
1148 | goto fail; | |
1149 | } | |
796d3239 MA |
1150 | qdict_put_str(encryptopts, "format", "qcow"); |
1151 | r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp); | |
b25b387f DB |
1152 | break; |
1153 | ||
4652b8f3 DB |
1154 | case QCOW_CRYPT_LUKS: |
1155 | if (encryptfmt && !g_str_equal(encryptfmt, "luks")) { | |
1156 | error_setg(errp, | |
1157 | "Header reported 'luks' encryption format but " | |
1158 | "options specify '%s'", encryptfmt); | |
1159 | ret = -EINVAL; | |
1160 | goto fail; | |
1161 | } | |
796d3239 MA |
1162 | qdict_put_str(encryptopts, "format", "luks"); |
1163 | r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp); | |
4652b8f3 DB |
1164 | break; |
1165 | ||
b25b387f DB |
1166 | default: |
1167 | error_setg(errp, "Unsupported encryption method %d", | |
1168 | s->crypt_method_header); | |
1169 | break; | |
1170 | } | |
1171 | if (s->crypt_method_header != QCOW_CRYPT_NONE && !r->crypto_opts) { | |
1172 | ret = -EINVAL; | |
1173 | goto fail; | |
1174 | } | |
1175 | ||
4c75d1a1 KW |
1176 | ret = 0; |
1177 | fail: | |
cb3e7f08 | 1178 | qobject_unref(encryptopts); |
94edf3fb KW |
1179 | qemu_opts_del(opts); |
1180 | opts = NULL; | |
ee55b173 KW |
1181 | return ret; |
1182 | } | |
1183 | ||
1184 | static void qcow2_update_options_commit(BlockDriverState *bs, | |
1185 | Qcow2ReopenState *r) | |
1186 | { | |
1187 | BDRVQcow2State *s = bs->opaque; | |
1188 | int i; | |
1189 | ||
5b0959a7 | 1190 | if (s->l2_table_cache) { |
e64d4072 | 1191 | qcow2_cache_destroy(s->l2_table_cache); |
5b0959a7 KW |
1192 | } |
1193 | if (s->refcount_block_cache) { | |
e64d4072 | 1194 | qcow2_cache_destroy(s->refcount_block_cache); |
5b0959a7 | 1195 | } |
ee55b173 KW |
1196 | s->l2_table_cache = r->l2_table_cache; |
1197 | s->refcount_block_cache = r->refcount_block_cache; | |
3c2e511a | 1198 | s->l2_slice_size = r->l2_slice_size; |
ee55b173 KW |
1199 | |
1200 | s->overlap_check = r->overlap_check; | |
1201 | s->use_lazy_refcounts = r->use_lazy_refcounts; | |
1202 | ||
1203 | for (i = 0; i < QCOW2_DISCARD_MAX; i++) { | |
1204 | s->discard_passthrough[i] = r->discard_passthrough[i]; | |
1205 | } | |
1206 | ||
5b0959a7 KW |
1207 | if (s->cache_clean_interval != r->cache_clean_interval) { |
1208 | cache_clean_timer_del(bs); | |
1209 | s->cache_clean_interval = r->cache_clean_interval; | |
1210 | cache_clean_timer_init(bs, bdrv_get_aio_context(bs)); | |
1211 | } | |
b25b387f DB |
1212 | |
1213 | qapi_free_QCryptoBlockOpenOptions(s->crypto_opts); | |
1214 | s->crypto_opts = r->crypto_opts; | |
ee55b173 KW |
1215 | } |
1216 | ||
1217 | static void qcow2_update_options_abort(BlockDriverState *bs, | |
1218 | Qcow2ReopenState *r) | |
1219 | { | |
1220 | if (r->l2_table_cache) { | |
e64d4072 | 1221 | qcow2_cache_destroy(r->l2_table_cache); |
ee55b173 KW |
1222 | } |
1223 | if (r->refcount_block_cache) { | |
e64d4072 | 1224 | qcow2_cache_destroy(r->refcount_block_cache); |
ee55b173 | 1225 | } |
b25b387f | 1226 | qapi_free_QCryptoBlockOpenOptions(r->crypto_opts); |
ee55b173 KW |
1227 | } |
1228 | ||
1229 | static int qcow2_update_options(BlockDriverState *bs, QDict *options, | |
1230 | int flags, Error **errp) | |
1231 | { | |
1232 | Qcow2ReopenState r = {}; | |
1233 | int ret; | |
1234 | ||
1235 | ret = qcow2_update_options_prepare(bs, &r, options, flags, errp); | |
1236 | if (ret >= 0) { | |
1237 | qcow2_update_options_commit(bs, &r); | |
1238 | } else { | |
1239 | qcow2_update_options_abort(bs, &r); | |
1240 | } | |
94edf3fb | 1241 | |
4c75d1a1 KW |
1242 | return ret; |
1243 | } | |
1244 | ||
1fafcd93 PB |
1245 | /* Called with s->lock held. */ |
1246 | static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, | |
1247 | int flags, Error **errp) | |
585f8587 | 1248 | { |
ff99129a | 1249 | BDRVQcow2State *s = bs->opaque; |
6d33e8e7 KW |
1250 | unsigned int len, i; |
1251 | int ret = 0; | |
585f8587 | 1252 | QCowHeader header; |
74c4510a | 1253 | Error *local_err = NULL; |
9b80ddf3 | 1254 | uint64_t ext_end; |
2cf7cfa1 | 1255 | uint64_t l1_vm_state_index; |
88ddffae | 1256 | bool update_header = false; |
585f8587 | 1257 | |
cf2ab8fc | 1258 | ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); |
6d85a57e | 1259 | if (ret < 0) { |
3ef6c40a | 1260 | error_setg_errno(errp, -ret, "Could not read qcow2 header"); |
585f8587 | 1261 | goto fail; |
6d85a57e | 1262 | } |
3b698f52 PM |
1263 | header.magic = be32_to_cpu(header.magic); |
1264 | header.version = be32_to_cpu(header.version); | |
1265 | header.backing_file_offset = be64_to_cpu(header.backing_file_offset); | |
1266 | header.backing_file_size = be32_to_cpu(header.backing_file_size); | |
1267 | header.size = be64_to_cpu(header.size); | |
1268 | header.cluster_bits = be32_to_cpu(header.cluster_bits); | |
1269 | header.crypt_method = be32_to_cpu(header.crypt_method); | |
1270 | header.l1_table_offset = be64_to_cpu(header.l1_table_offset); | |
1271 | header.l1_size = be32_to_cpu(header.l1_size); | |
1272 | header.refcount_table_offset = be64_to_cpu(header.refcount_table_offset); | |
1273 | header.refcount_table_clusters = | |
1274 | be32_to_cpu(header.refcount_table_clusters); | |
1275 | header.snapshots_offset = be64_to_cpu(header.snapshots_offset); | |
1276 | header.nb_snapshots = be32_to_cpu(header.nb_snapshots); | |
3b46e624 | 1277 | |
e8cdcec1 | 1278 | if (header.magic != QCOW_MAGIC) { |
3ef6c40a | 1279 | error_setg(errp, "Image is not in qcow2 format"); |
76abe407 | 1280 | ret = -EINVAL; |
585f8587 | 1281 | goto fail; |
6d85a57e | 1282 | } |
6744cbab | 1283 | if (header.version < 2 || header.version > 3) { |
a55448b3 | 1284 | error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version); |
6744cbab KW |
1285 | ret = -ENOTSUP; |
1286 | goto fail; | |
1287 | } | |
1288 | ||
1289 | s->qcow_version = header.version; | |
1290 | ||
24342f2c KW |
1291 | /* Initialise cluster size */ |
1292 | if (header.cluster_bits < MIN_CLUSTER_BITS || | |
1293 | header.cluster_bits > MAX_CLUSTER_BITS) { | |
521b2b5d HR |
1294 | error_setg(errp, "Unsupported cluster size: 2^%" PRIu32, |
1295 | header.cluster_bits); | |
24342f2c KW |
1296 | ret = -EINVAL; |
1297 | goto fail; | |
1298 | } | |
1299 | ||
1300 | s->cluster_bits = header.cluster_bits; | |
1301 | s->cluster_size = 1 << s->cluster_bits; | |
24342f2c | 1302 | |
6744cbab KW |
1303 | /* Initialise version 3 header fields */ |
1304 | if (header.version == 2) { | |
1305 | header.incompatible_features = 0; | |
1306 | header.compatible_features = 0; | |
1307 | header.autoclear_features = 0; | |
1308 | header.refcount_order = 4; | |
1309 | header.header_length = 72; | |
1310 | } else { | |
3b698f52 PM |
1311 | header.incompatible_features = |
1312 | be64_to_cpu(header.incompatible_features); | |
1313 | header.compatible_features = be64_to_cpu(header.compatible_features); | |
1314 | header.autoclear_features = be64_to_cpu(header.autoclear_features); | |
1315 | header.refcount_order = be32_to_cpu(header.refcount_order); | |
1316 | header.header_length = be32_to_cpu(header.header_length); | |
24342f2c KW |
1317 | |
1318 | if (header.header_length < 104) { | |
1319 | error_setg(errp, "qcow2 header too short"); | |
1320 | ret = -EINVAL; | |
1321 | goto fail; | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | if (header.header_length > s->cluster_size) { | |
1326 | error_setg(errp, "qcow2 header exceeds cluster size"); | |
1327 | ret = -EINVAL; | |
1328 | goto fail; | |
6744cbab KW |
1329 | } |
1330 | ||
1331 | if (header.header_length > sizeof(header)) { | |
1332 | s->unknown_header_fields_size = header.header_length - sizeof(header); | |
1333 | s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); | |
cf2ab8fc | 1334 | ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, |
6744cbab KW |
1335 | s->unknown_header_fields_size); |
1336 | if (ret < 0) { | |
3ef6c40a HR |
1337 | error_setg_errno(errp, -ret, "Could not read unknown qcow2 header " |
1338 | "fields"); | |
6744cbab KW |
1339 | goto fail; |
1340 | } | |
1341 | } | |
1342 | ||
a1b3955c KW |
1343 | if (header.backing_file_offset > s->cluster_size) { |
1344 | error_setg(errp, "Invalid backing file offset"); | |
1345 | ret = -EINVAL; | |
1346 | goto fail; | |
1347 | } | |
1348 | ||
cfcc4c62 KW |
1349 | if (header.backing_file_offset) { |
1350 | ext_end = header.backing_file_offset; | |
1351 | } else { | |
1352 | ext_end = 1 << header.cluster_bits; | |
1353 | } | |
1354 | ||
6744cbab KW |
1355 | /* Handle feature bits */ |
1356 | s->incompatible_features = header.incompatible_features; | |
1357 | s->compatible_features = header.compatible_features; | |
1358 | s->autoclear_features = header.autoclear_features; | |
1359 | ||
c61d0004 | 1360 | if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { |
cfcc4c62 KW |
1361 | void *feature_table = NULL; |
1362 | qcow2_read_extensions(bs, header.header_length, ext_end, | |
88ddffae | 1363 | &feature_table, flags, NULL, NULL); |
a55448b3 | 1364 | report_unsupported_feature(errp, feature_table, |
c61d0004 SH |
1365 | s->incompatible_features & |
1366 | ~QCOW2_INCOMPAT_MASK); | |
6744cbab | 1367 | ret = -ENOTSUP; |
c5a33ee9 | 1368 | g_free(feature_table); |
6744cbab KW |
1369 | goto fail; |
1370 | } | |
1371 | ||
69c98726 HR |
1372 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { |
1373 | /* Corrupt images may not be written to unless they are being repaired | |
1374 | */ | |
1375 | if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { | |
3ef6c40a HR |
1376 | error_setg(errp, "qcow2: Image is corrupt; cannot be opened " |
1377 | "read/write"); | |
69c98726 HR |
1378 | ret = -EACCES; |
1379 | goto fail; | |
1380 | } | |
1381 | } | |
1382 | ||
6744cbab | 1383 | /* Check support for various header values */ |
b72faf9f HR |
1384 | if (header.refcount_order > 6) { |
1385 | error_setg(errp, "Reference count entry width too large; may not " | |
1386 | "exceed 64 bits"); | |
1387 | ret = -EINVAL; | |
e8cdcec1 KW |
1388 | goto fail; |
1389 | } | |
b6481f37 | 1390 | s->refcount_order = header.refcount_order; |
346a53df HR |
1391 | s->refcount_bits = 1 << s->refcount_order; |
1392 | s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1); | |
1393 | s->refcount_max += s->refcount_max - 1; | |
6744cbab | 1394 | |
585f8587 | 1395 | s->crypt_method_header = header.crypt_method; |
6d85a57e | 1396 | if (s->crypt_method_header) { |
e6ff69bf DB |
1397 | if (bdrv_uses_whitelist() && |
1398 | s->crypt_method_header == QCOW_CRYPT_AES) { | |
8c0dcbc4 DB |
1399 | error_setg(errp, |
1400 | "Use of AES-CBC encrypted qcow2 images is no longer " | |
1401 | "supported in system emulators"); | |
1402 | error_append_hint(errp, | |
1403 | "You can use 'qemu-img convert' to convert your " | |
1404 | "image to an alternative supported format, such " | |
1405 | "as unencrypted qcow2, or raw with the LUKS " | |
1406 | "format instead.\n"); | |
1407 | ret = -ENOSYS; | |
1408 | goto fail; | |
e6ff69bf DB |
1409 | } |
1410 | ||
4652b8f3 DB |
1411 | if (s->crypt_method_header == QCOW_CRYPT_AES) { |
1412 | s->crypt_physical_offset = false; | |
1413 | } else { | |
1414 | /* Assuming LUKS and any future crypt methods we | |
1415 | * add will all use physical offsets, due to the | |
1416 | * fact that the alternative is insecure... */ | |
1417 | s->crypt_physical_offset = true; | |
1418 | } | |
1419 | ||
54115412 | 1420 | bs->encrypted = true; |
6d85a57e | 1421 | } |
24342f2c | 1422 | |
585f8587 FB |
1423 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
1424 | s->l2_size = 1 << s->l2_bits; | |
1d13d654 HR |
1425 | /* 2^(s->refcount_order - 3) is the refcount width in bytes */ |
1426 | s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3); | |
1427 | s->refcount_block_size = 1 << s->refcount_block_bits; | |
bd016b91 | 1428 | bs->total_sectors = header.size / BDRV_SECTOR_SIZE; |
585f8587 FB |
1429 | s->csize_shift = (62 - (s->cluster_bits - 8)); |
1430 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
1431 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
5dab2fad | 1432 | |
585f8587 | 1433 | s->refcount_table_offset = header.refcount_table_offset; |
5fafdf24 | 1434 | s->refcount_table_size = |
585f8587 FB |
1435 | header.refcount_table_clusters << (s->cluster_bits - 3); |
1436 | ||
951053a9 AG |
1437 | if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) { |
1438 | error_setg(errp, "Image does not contain a reference count table"); | |
1439 | ret = -EINVAL; | |
1440 | goto fail; | |
1441 | } | |
1442 | ||
0cf0e598 AG |
1443 | ret = qcow2_validate_table(bs, s->refcount_table_offset, |
1444 | header.refcount_table_clusters, | |
1445 | s->cluster_size, QCOW_MAX_REFTABLE_SIZE, | |
1446 | "Reference count table", errp); | |
8c7de283 | 1447 | if (ret < 0) { |
8c7de283 KW |
1448 | goto fail; |
1449 | } | |
1450 | ||
8bc584fe HR |
1451 | if (!(flags & BDRV_O_CHECK)) { |
1452 | /* | |
1453 | * The total size in bytes of the snapshot table is checked in | |
1454 | * qcow2_read_snapshots() because the size of each snapshot is | |
1455 | * variable and we don't know it yet. | |
1456 | * Here we only check the offset and number of snapshots. | |
1457 | */ | |
1458 | ret = qcow2_validate_table(bs, header.snapshots_offset, | |
1459 | header.nb_snapshots, | |
1460 | sizeof(QCowSnapshotHeader), | |
1461 | sizeof(QCowSnapshotHeader) * | |
1462 | QCOW_MAX_SNAPSHOTS, | |
1463 | "Snapshot table", errp); | |
1464 | if (ret < 0) { | |
1465 | goto fail; | |
1466 | } | |
ce48f2f4 KW |
1467 | } |
1468 | ||
585f8587 | 1469 | /* read the level 1 table */ |
0cf0e598 AG |
1470 | ret = qcow2_validate_table(bs, header.l1_table_offset, |
1471 | header.l1_size, sizeof(uint64_t), | |
1472 | QCOW_MAX_L1_SIZE, "Active L1 table", errp); | |
1473 | if (ret < 0) { | |
2d51c32c KW |
1474 | goto fail; |
1475 | } | |
585f8587 | 1476 | s->l1_size = header.l1_size; |
0cf0e598 | 1477 | s->l1_table_offset = header.l1_table_offset; |
2cf7cfa1 KW |
1478 | |
1479 | l1_vm_state_index = size_to_l1(s, header.size); | |
1480 | if (l1_vm_state_index > INT_MAX) { | |
3ef6c40a | 1481 | error_setg(errp, "Image is too big"); |
2cf7cfa1 KW |
1482 | ret = -EFBIG; |
1483 | goto fail; | |
1484 | } | |
1485 | s->l1_vm_state_index = l1_vm_state_index; | |
1486 | ||
585f8587 FB |
1487 | /* the L1 table must contain at least enough entries to put |
1488 | header.size bytes */ | |
6d85a57e | 1489 | if (s->l1_size < s->l1_vm_state_index) { |
3ef6c40a | 1490 | error_setg(errp, "L1 table is too small"); |
6d85a57e | 1491 | ret = -EINVAL; |
585f8587 | 1492 | goto fail; |
6d85a57e | 1493 | } |
2d51c32c | 1494 | |
d191d12d | 1495 | if (s->l1_size > 0) { |
9a4f4c31 | 1496 | s->l1_table = qemu_try_blockalign(bs->file->bs, |
ef97d608 | 1497 | s->l1_size * sizeof(uint64_t)); |
de82815d KW |
1498 | if (s->l1_table == NULL) { |
1499 | error_setg(errp, "Could not allocate L1 table"); | |
1500 | ret = -ENOMEM; | |
1501 | goto fail; | |
1502 | } | |
cf2ab8fc | 1503 | ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, |
6d85a57e JS |
1504 | s->l1_size * sizeof(uint64_t)); |
1505 | if (ret < 0) { | |
3ef6c40a | 1506 | error_setg_errno(errp, -ret, "Could not read L1 table"); |
d191d12d | 1507 | goto fail; |
6d85a57e | 1508 | } |
d191d12d | 1509 | for(i = 0;i < s->l1_size; i++) { |
3b698f52 | 1510 | s->l1_table[i] = be64_to_cpu(s->l1_table[i]); |
d191d12d | 1511 | } |
585f8587 | 1512 | } |
29c1a730 | 1513 | |
94edf3fb KW |
1514 | /* Parse driver-specific options */ |
1515 | ret = qcow2_update_options(bs, options, flags, errp); | |
90efa0ea KW |
1516 | if (ret < 0) { |
1517 | goto fail; | |
1518 | } | |
1519 | ||
06d9260f | 1520 | s->flags = flags; |
3b46e624 | 1521 | |
6d85a57e JS |
1522 | ret = qcow2_refcount_init(bs); |
1523 | if (ret != 0) { | |
3ef6c40a | 1524 | error_setg_errno(errp, -ret, "Could not initialize refcount handling"); |
585f8587 | 1525 | goto fail; |
6d85a57e | 1526 | } |
585f8587 | 1527 | |
72cf2d4f | 1528 | QLIST_INIT(&s->cluster_allocs); |
0b919fae | 1529 | QTAILQ_INIT(&s->discards); |
f214978a | 1530 | |
9b80ddf3 | 1531 | /* read qcow2 extensions */ |
3ef6c40a | 1532 | if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL, |
88ddffae | 1533 | flags, &update_header, &local_err)) { |
3ef6c40a | 1534 | error_propagate(errp, local_err); |
6d85a57e | 1535 | ret = -EINVAL; |
9b80ddf3 | 1536 | goto fail; |
6d85a57e | 1537 | } |
9b80ddf3 | 1538 | |
0e8c08be KW |
1539 | /* Open external data file */ |
1540 | s->data_file = bdrv_open_child(NULL, options, "data-file", bs, &child_file, | |
1541 | true, &local_err); | |
1542 | if (local_err) { | |
1543 | error_propagate(errp, local_err); | |
1544 | ret = -EINVAL; | |
1545 | goto fail; | |
1546 | } | |
1547 | ||
1548 | if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) { | |
9b890bdc KW |
1549 | if (!s->data_file && s->image_data_file) { |
1550 | s->data_file = bdrv_open_child(s->image_data_file, options, | |
1551 | "data-file", bs, &child_file, | |
1552 | false, errp); | |
1553 | if (!s->data_file) { | |
1554 | ret = -EINVAL; | |
1555 | goto fail; | |
1556 | } | |
1557 | } | |
0e8c08be KW |
1558 | if (!s->data_file) { |
1559 | error_setg(errp, "'data-file' is required for this image"); | |
1560 | ret = -EINVAL; | |
1561 | goto fail; | |
1562 | } | |
1563 | } else { | |
1564 | if (s->data_file) { | |
1565 | error_setg(errp, "'data-file' can only be set for images with an " | |
1566 | "external data file"); | |
1567 | ret = -EINVAL; | |
1568 | goto fail; | |
6c3944dc KW |
1569 | } |
1570 | ||
1571 | s->data_file = bs->file; | |
1572 | ||
1573 | if (data_file_is_raw(bs)) { | |
1574 | error_setg(errp, "data-file-raw requires a data file"); | |
1575 | ret = -EINVAL; | |
1576 | goto fail; | |
0e8c08be KW |
1577 | } |
1578 | } | |
93c24936 | 1579 | |
4652b8f3 DB |
1580 | /* qcow2_read_extension may have set up the crypto context |
1581 | * if the crypt method needs a header region, some methods | |
1582 | * don't need header extensions, so must check here | |
1583 | */ | |
1584 | if (s->crypt_method_header && !s->crypto) { | |
1585 | if (s->crypt_method_header == QCOW_CRYPT_AES) { | |
1586 | unsigned int cflags = 0; | |
1587 | if (flags & BDRV_O_NO_IO) { | |
1588 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; | |
1589 | } | |
1cd9a787 | 1590 | s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.", |
8ac0f15f VSO |
1591 | NULL, NULL, cflags, |
1592 | QCOW2_MAX_THREADS, errp); | |
4652b8f3 DB |
1593 | if (!s->crypto) { |
1594 | ret = -EINVAL; | |
1595 | goto fail; | |
1596 | } | |
1597 | } else if (!(flags & BDRV_O_NO_IO)) { | |
1598 | error_setg(errp, "Missing CRYPTO header for crypt method %d", | |
1599 | s->crypt_method_header); | |
b25b387f DB |
1600 | ret = -EINVAL; |
1601 | goto fail; | |
1602 | } | |
1603 | } | |
1604 | ||
585f8587 FB |
1605 | /* read the backing file name */ |
1606 | if (header.backing_file_offset != 0) { | |
1607 | len = header.backing_file_size; | |
9a29e18f | 1608 | if (len > MIN(1023, s->cluster_size - header.backing_file_offset) || |
e729fa6a | 1609 | len >= sizeof(bs->backing_file)) { |
6d33e8e7 KW |
1610 | error_setg(errp, "Backing file name too long"); |
1611 | ret = -EINVAL; | |
1612 | goto fail; | |
6d85a57e | 1613 | } |
cf2ab8fc | 1614 | ret = bdrv_pread(bs->file, header.backing_file_offset, |
998c2019 | 1615 | bs->auto_backing_file, len); |
6d85a57e | 1616 | if (ret < 0) { |
3ef6c40a | 1617 | error_setg_errno(errp, -ret, "Could not read backing file name"); |
585f8587 | 1618 | goto fail; |
6d85a57e | 1619 | } |
998c2019 HR |
1620 | bs->auto_backing_file[len] = '\0'; |
1621 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), | |
1622 | bs->auto_backing_file); | |
1623 | s->image_backing_file = g_strdup(bs->auto_backing_file); | |
585f8587 | 1624 | } |
42deb29f | 1625 | |
8bc584fe HR |
1626 | /* |
1627 | * Internal snapshots; skip reading them in check mode, because | |
1628 | * we do not need them then, and we do not want to abort because | |
1629 | * of a broken table. | |
1630 | */ | |
1631 | if (!(flags & BDRV_O_CHECK)) { | |
1632 | s->snapshots_offset = header.snapshots_offset; | |
1633 | s->nb_snapshots = header.nb_snapshots; | |
11b128f4 | 1634 | |
8bc584fe HR |
1635 | ret = qcow2_read_snapshots(bs, errp); |
1636 | if (ret < 0) { | |
1637 | goto fail; | |
1638 | } | |
6d85a57e | 1639 | } |
585f8587 | 1640 | |
af7b708d | 1641 | /* Clear unknown autoclear feature bits */ |
88ddffae | 1642 | update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK; |
d1258dd0 VSO |
1643 | update_header = |
1644 | update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE); | |
1645 | if (update_header) { | |
88ddffae | 1646 | s->autoclear_features &= QCOW2_AUTOCLEAR_MASK; |
d1258dd0 VSO |
1647 | } |
1648 | ||
9c98f145 VSO |
1649 | /* == Handle persistent dirty bitmaps == |
1650 | * | |
1651 | * We want load dirty bitmaps in three cases: | |
1652 | * | |
1653 | * 1. Normal open of the disk in active mode, not related to invalidation | |
1654 | * after migration. | |
1655 | * | |
1656 | * 2. Invalidation of the target vm after pre-copy phase of migration, if | |
1657 | * bitmaps are _not_ migrating through migration channel, i.e. | |
1658 | * 'dirty-bitmaps' capability is disabled. | |
1659 | * | |
1660 | * 3. Invalidation of source vm after failed or canceled migration. | |
1661 | * This is a very interesting case. There are two possible types of | |
1662 | * bitmaps: | |
1663 | * | |
1664 | * A. Stored on inactivation and removed. They should be loaded from the | |
1665 | * image. | |
1666 | * | |
1667 | * B. Not stored: not-persistent bitmaps and bitmaps, migrated through | |
1668 | * the migration channel (with dirty-bitmaps capability). | |
1669 | * | |
1670 | * On the other hand, there are two possible sub-cases: | |
1671 | * | |
1672 | * 3.1 disk was changed by somebody else while were inactive. In this | |
1673 | * case all in-RAM dirty bitmaps (both persistent and not) are | |
1674 | * definitely invalid. And we don't have any method to determine | |
1675 | * this. | |
1676 | * | |
1677 | * Simple and safe thing is to just drop all the bitmaps of type B on | |
1678 | * inactivation. But in this case we lose bitmaps in valid 4.2 case. | |
1679 | * | |
1680 | * On the other hand, resuming source vm, if disk was already changed | |
1681 | * is a bad thing anyway: not only bitmaps, the whole vm state is | |
1682 | * out of sync with disk. | |
1683 | * | |
1684 | * This means, that user or management tool, who for some reason | |
1685 | * decided to resume source vm, after disk was already changed by | |
1686 | * target vm, should at least drop all dirty bitmaps by hand. | |
1687 | * | |
1688 | * So, we can ignore this case for now, but TODO: "generation" | |
1689 | * extension for qcow2, to determine, that image was changed after | |
1690 | * last inactivation. And if it is changed, we will drop (or at least | |
1691 | * mark as 'invalid' all the bitmaps of type B, both persistent | |
1692 | * and not). | |
1693 | * | |
1694 | * 3.2 disk was _not_ changed while were inactive. Bitmaps may be saved | |
1695 | * to disk ('dirty-bitmaps' capability disabled), or not saved | |
1696 | * ('dirty-bitmaps' capability enabled), but we don't need to care | |
1697 | * of: let's load bitmaps as always: stored bitmaps will be loaded, | |
1698 | * and not stored has flag IN_USE=1 in the image and will be skipped | |
1699 | * on loading. | |
1700 | * | |
1701 | * One remaining possible case when we don't want load bitmaps: | |
1702 | * | |
1703 | * 4. Open disk in inactive mode in target vm (bitmaps are migrating or | |
1704 | * will be loaded on invalidation, no needs try loading them before) | |
1705 | */ | |
1706 | ||
1707 | if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) { | |
1708 | /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */ | |
1709 | bool header_updated = qcow2_load_dirty_bitmaps(bs, &local_err); | |
66be5c3e T |
1710 | if (local_err != NULL) { |
1711 | error_propagate(errp, local_err); | |
1712 | ret = -EINVAL; | |
1713 | goto fail; | |
1714 | } | |
9c98f145 VSO |
1715 | |
1716 | update_header = update_header && !header_updated; | |
d1258dd0 | 1717 | } |
d1258dd0 VSO |
1718 | |
1719 | if (update_header) { | |
af7b708d SH |
1720 | ret = qcow2_update_header(bs); |
1721 | if (ret < 0) { | |
3ef6c40a | 1722 | error_setg_errno(errp, -ret, "Could not update qcow2 header"); |
af7b708d SH |
1723 | goto fail; |
1724 | } | |
1725 | } | |
1726 | ||
3b650816 KW |
1727 | bs->supported_zero_flags = header.version >= 3 ? |
1728 | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK : 0; | |
68d100e9 | 1729 | |
c61d0004 | 1730 | /* Repair image if dirty */ |
04c01a5c | 1731 | if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only && |
058f8f16 | 1732 | (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { |
c61d0004 SH |
1733 | BdrvCheckResult result = {0}; |
1734 | ||
2fd61638 PB |
1735 | ret = qcow2_co_check_locked(bs, &result, |
1736 | BDRV_FIX_ERRORS | BDRV_FIX_LEAKS); | |
791fff50 HR |
1737 | if (ret < 0 || result.check_errors) { |
1738 | if (ret >= 0) { | |
1739 | ret = -EIO; | |
1740 | } | |
3ef6c40a | 1741 | error_setg_errno(errp, -ret, "Could not repair dirty image"); |
c61d0004 SH |
1742 | goto fail; |
1743 | } | |
1744 | } | |
1745 | ||
585f8587 | 1746 | #ifdef DEBUG_ALLOC |
6cbc3031 PH |
1747 | { |
1748 | BdrvCheckResult result = {0}; | |
b35278f7 | 1749 | qcow2_check_refcounts(bs, &result, 0); |
6cbc3031 | 1750 | } |
585f8587 | 1751 | #endif |
ceb029cd | 1752 | |
6f13a316 | 1753 | qemu_co_queue_init(&s->thread_task_queue); |
ceb029cd | 1754 | |
6d85a57e | 1755 | return ret; |
585f8587 FB |
1756 | |
1757 | fail: | |
9b890bdc | 1758 | g_free(s->image_data_file); |
0e8c08be KW |
1759 | if (has_data_file(bs)) { |
1760 | bdrv_unref_child(bs, s->data_file); | |
1761 | } | |
6744cbab | 1762 | g_free(s->unknown_header_fields); |
75bab85c | 1763 | cleanup_unknown_header_ext(bs); |
ed6ccf0f KW |
1764 | qcow2_free_snapshots(bs); |
1765 | qcow2_refcount_close(bs); | |
de82815d | 1766 | qemu_vfree(s->l1_table); |
cf93980e HR |
1767 | /* else pre-write overlap checks in cache_destroy may crash */ |
1768 | s->l1_table = NULL; | |
279621c0 | 1769 | cache_clean_timer_del(bs); |
29c1a730 | 1770 | if (s->l2_table_cache) { |
e64d4072 | 1771 | qcow2_cache_destroy(s->l2_table_cache); |
29c1a730 | 1772 | } |
c5a33ee9 | 1773 | if (s->refcount_block_cache) { |
e64d4072 | 1774 | qcow2_cache_destroy(s->refcount_block_cache); |
c5a33ee9 | 1775 | } |
b25b387f DB |
1776 | qcrypto_block_free(s->crypto); |
1777 | qapi_free_QCryptoBlockOpenOptions(s->crypto_opts); | |
6d85a57e | 1778 | return ret; |
585f8587 FB |
1779 | } |
1780 | ||
1fafcd93 PB |
1781 | typedef struct QCow2OpenCo { |
1782 | BlockDriverState *bs; | |
1783 | QDict *options; | |
1784 | int flags; | |
1785 | Error **errp; | |
1786 | int ret; | |
1787 | } QCow2OpenCo; | |
1788 | ||
1789 | static void coroutine_fn qcow2_open_entry(void *opaque) | |
1790 | { | |
1791 | QCow2OpenCo *qoc = opaque; | |
1792 | BDRVQcow2State *s = qoc->bs->opaque; | |
1793 | ||
1794 | qemu_co_mutex_lock(&s->lock); | |
1795 | qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, qoc->errp); | |
1796 | qemu_co_mutex_unlock(&s->lock); | |
1797 | } | |
1798 | ||
4e4bf5c4 KW |
1799 | static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, |
1800 | Error **errp) | |
1801 | { | |
1fafcd93 PB |
1802 | BDRVQcow2State *s = bs->opaque; |
1803 | QCow2OpenCo qoc = { | |
1804 | .bs = bs, | |
1805 | .options = options, | |
1806 | .flags = flags, | |
1807 | .errp = errp, | |
1808 | .ret = -EINPROGRESS | |
1809 | }; | |
1810 | ||
4e4bf5c4 KW |
1811 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, |
1812 | false, errp); | |
1813 | if (!bs->file) { | |
1814 | return -EINVAL; | |
1815 | } | |
1816 | ||
1fafcd93 PB |
1817 | /* Initialise locks */ |
1818 | qemu_co_mutex_init(&s->lock); | |
1819 | ||
1820 | if (qemu_in_coroutine()) { | |
1821 | /* From bdrv_co_create. */ | |
1822 | qcow2_open_entry(&qoc); | |
1823 | } else { | |
4720cbee | 1824 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
1fafcd93 PB |
1825 | qemu_coroutine_enter(qemu_coroutine_create(qcow2_open_entry, &qoc)); |
1826 | BDRV_POLL_WHILE(bs, qoc.ret == -EINPROGRESS); | |
1827 | } | |
1828 | return qoc.ret; | |
4e4bf5c4 KW |
1829 | } |
1830 | ||
3baca891 | 1831 | static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) |
d34682cd | 1832 | { |
ff99129a | 1833 | BDRVQcow2State *s = bs->opaque; |
d34682cd | 1834 | |
a84178cc EB |
1835 | if (bs->encrypted) { |
1836 | /* Encryption works on a sector granularity */ | |
6f8f015c | 1837 | bs->bl.request_alignment = qcrypto_block_get_sector_size(s->crypto); |
a84178cc | 1838 | } |
cf081fca | 1839 | bs->bl.pwrite_zeroes_alignment = s->cluster_size; |
ecdbead6 | 1840 | bs->bl.pdiscard_alignment = s->cluster_size; |
d34682cd KW |
1841 | } |
1842 | ||
21d82ac9 JC |
1843 | static int qcow2_reopen_prepare(BDRVReopenState *state, |
1844 | BlockReopenQueue *queue, Error **errp) | |
1845 | { | |
5b0959a7 | 1846 | Qcow2ReopenState *r; |
4c2e5f8f KW |
1847 | int ret; |
1848 | ||
5b0959a7 KW |
1849 | r = g_new0(Qcow2ReopenState, 1); |
1850 | state->opaque = r; | |
1851 | ||
1852 | ret = qcow2_update_options_prepare(state->bs, r, state->options, | |
1853 | state->flags, errp); | |
1854 | if (ret < 0) { | |
1855 | goto fail; | |
1856 | } | |
1857 | ||
1858 | /* We need to write out any unwritten data if we reopen read-only. */ | |
4c2e5f8f | 1859 | if ((state->flags & BDRV_O_RDWR) == 0) { |
169b8793 VSO |
1860 | ret = qcow2_reopen_bitmaps_ro(state->bs, errp); |
1861 | if (ret < 0) { | |
1862 | goto fail; | |
1863 | } | |
1864 | ||
4c2e5f8f KW |
1865 | ret = bdrv_flush(state->bs); |
1866 | if (ret < 0) { | |
5b0959a7 | 1867 | goto fail; |
4c2e5f8f KW |
1868 | } |
1869 | ||
1870 | ret = qcow2_mark_clean(state->bs); | |
1871 | if (ret < 0) { | |
5b0959a7 | 1872 | goto fail; |
4c2e5f8f KW |
1873 | } |
1874 | } | |
1875 | ||
21d82ac9 | 1876 | return 0; |
5b0959a7 KW |
1877 | |
1878 | fail: | |
1879 | qcow2_update_options_abort(state->bs, r); | |
1880 | g_free(r); | |
1881 | return ret; | |
1882 | } | |
1883 | ||
1884 | static void qcow2_reopen_commit(BDRVReopenState *state) | |
1885 | { | |
1886 | qcow2_update_options_commit(state->bs, state->opaque); | |
65eb7c85 PK |
1887 | g_free(state->opaque); |
1888 | } | |
1889 | ||
1890 | static void qcow2_reopen_commit_post(BDRVReopenState *state) | |
1891 | { | |
4dd09f62 VSO |
1892 | if (state->flags & BDRV_O_RDWR) { |
1893 | Error *local_err = NULL; | |
1894 | ||
1895 | if (qcow2_reopen_bitmaps_rw(state->bs, &local_err) < 0) { | |
1896 | /* | |
1897 | * This is not fatal, bitmaps just left read-only, so all following | |
1898 | * writes will fail. User can remove read-only bitmaps to unblock | |
1899 | * writes or retry reopen. | |
1900 | */ | |
1901 | error_reportf_err(local_err, | |
1902 | "%s: Failed to make dirty bitmaps writable: ", | |
1903 | bdrv_get_node_name(state->bs)); | |
1904 | } | |
1905 | } | |
5b0959a7 KW |
1906 | } |
1907 | ||
1908 | static void qcow2_reopen_abort(BDRVReopenState *state) | |
1909 | { | |
1910 | qcow2_update_options_abort(state->bs, state->opaque); | |
1911 | g_free(state->opaque); | |
21d82ac9 JC |
1912 | } |
1913 | ||
5365f44d KW |
1914 | static void qcow2_join_options(QDict *options, QDict *old_options) |
1915 | { | |
1916 | bool has_new_overlap_template = | |
1917 | qdict_haskey(options, QCOW2_OPT_OVERLAP) || | |
1918 | qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE); | |
1919 | bool has_new_total_cache_size = | |
1920 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE); | |
1921 | bool has_all_cache_options; | |
1922 | ||
1923 | /* New overlap template overrides all old overlap options */ | |
1924 | if (has_new_overlap_template) { | |
1925 | qdict_del(old_options, QCOW2_OPT_OVERLAP); | |
1926 | qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE); | |
1927 | qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER); | |
1928 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1); | |
1929 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2); | |
1930 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE); | |
1931 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK); | |
1932 | qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE); | |
1933 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1); | |
1934 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2); | |
1935 | } | |
1936 | ||
1937 | /* New total cache size overrides all old options */ | |
1938 | if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) { | |
1939 | qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE); | |
1940 | qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
1941 | } | |
1942 | ||
1943 | qdict_join(options, old_options, false); | |
1944 | ||
1945 | /* | |
1946 | * If after merging all cache size options are set, an old total size is | |
1947 | * overwritten. Do keep all options, however, if all three are new. The | |
1948 | * resulting error message is what we want to happen. | |
1949 | */ | |
1950 | has_all_cache_options = | |
1951 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) || | |
1952 | qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) || | |
1953 | qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
1954 | ||
1955 | if (has_all_cache_options && !has_new_total_cache_size) { | |
1956 | qdict_del(options, QCOW2_OPT_CACHE_SIZE); | |
1957 | } | |
1958 | } | |
1959 | ||
a320fb04 EB |
1960 | static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs, |
1961 | bool want_zero, | |
1962 | int64_t offset, int64_t count, | |
1963 | int64_t *pnum, int64_t *map, | |
1964 | BlockDriverState **file) | |
585f8587 | 1965 | { |
ff99129a | 1966 | BDRVQcow2State *s = bs->opaque; |
585f8587 | 1967 | uint64_t cluster_offset; |
ecfe1863 | 1968 | unsigned int bytes; |
74e60fb5 | 1969 | int ret, status = 0; |
585f8587 | 1970 | |
5e978550 KW |
1971 | qemu_co_mutex_lock(&s->lock); |
1972 | ||
69f47505 VSO |
1973 | if (!s->metadata_preallocation_checked) { |
1974 | ret = qcow2_detect_metadata_preallocation(bs); | |
1975 | s->metadata_preallocation = (ret == 1); | |
1976 | s->metadata_preallocation_checked = true; | |
1977 | } | |
1978 | ||
a320fb04 | 1979 | bytes = MIN(INT_MAX, count); |
a320fb04 | 1980 | ret = qcow2_get_cluster_offset(bs, offset, &bytes, &cluster_offset); |
f8a2e5e3 | 1981 | qemu_co_mutex_unlock(&s->lock); |
1c46efaa | 1982 | if (ret < 0) { |
d663640c | 1983 | return ret; |
1c46efaa | 1984 | } |
095a9c58 | 1985 | |
a320fb04 | 1986 | *pnum = bytes; |
ecfe1863 | 1987 | |
37be1403 | 1988 | if ((ret == QCOW2_CLUSTER_NORMAL || ret == QCOW2_CLUSTER_ZERO_ALLOC) && |
b25b387f | 1989 | !s->crypto) { |
74e60fb5 | 1990 | *map = cluster_offset | offset_into_cluster(s, offset); |
37be1403 | 1991 | *file = s->data_file->bs; |
a320fb04 | 1992 | status |= BDRV_BLOCK_OFFSET_VALID; |
4bc74be9 | 1993 | } |
fdfab37d | 1994 | if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) { |
4bc74be9 PB |
1995 | status |= BDRV_BLOCK_ZERO; |
1996 | } else if (ret != QCOW2_CLUSTER_UNALLOCATED) { | |
1997 | status |= BDRV_BLOCK_DATA; | |
1998 | } | |
69f47505 VSO |
1999 | if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) && |
2000 | (status & BDRV_BLOCK_OFFSET_VALID)) | |
2001 | { | |
2002 | status |= BDRV_BLOCK_RECURSE; | |
2003 | } | |
4bc74be9 | 2004 | return status; |
585f8587 FB |
2005 | } |
2006 | ||
fd9fcd37 FZ |
2007 | static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs, |
2008 | QCowL2Meta **pl2meta, | |
2009 | bool link_l2) | |
2010 | { | |
2011 | int ret = 0; | |
2012 | QCowL2Meta *l2meta = *pl2meta; | |
2013 | ||
2014 | while (l2meta != NULL) { | |
2015 | QCowL2Meta *next; | |
2016 | ||
354d930d | 2017 | if (link_l2) { |
fd9fcd37 FZ |
2018 | ret = qcow2_alloc_cluster_link_l2(bs, l2meta); |
2019 | if (ret) { | |
2020 | goto out; | |
2021 | } | |
8b24cd14 KW |
2022 | } else { |
2023 | qcow2_alloc_cluster_abort(bs, l2meta); | |
fd9fcd37 FZ |
2024 | } |
2025 | ||
2026 | /* Take the request off the list of running requests */ | |
2027 | if (l2meta->nb_clusters != 0) { | |
2028 | QLIST_REMOVE(l2meta, next_in_flight); | |
2029 | } | |
2030 | ||
2031 | qemu_co_queue_restart_all(&l2meta->dependent_requests); | |
2032 | ||
2033 | next = l2meta->next; | |
2034 | g_free(l2meta); | |
2035 | l2meta = next; | |
2036 | } | |
2037 | out: | |
2038 | *pl2meta = l2meta; | |
2039 | return ret; | |
2040 | } | |
2041 | ||
88f468e5 VSO |
2042 | static coroutine_fn int |
2043 | qcow2_co_preadv_encrypted(BlockDriverState *bs, | |
2044 | uint64_t file_cluster_offset, | |
2045 | uint64_t offset, | |
2046 | uint64_t bytes, | |
2047 | QEMUIOVector *qiov, | |
2048 | uint64_t qiov_offset) | |
2049 | { | |
2050 | int ret; | |
2051 | BDRVQcow2State *s = bs->opaque; | |
2052 | uint8_t *buf; | |
2053 | ||
2054 | assert(bs->encrypted && s->crypto); | |
2055 | assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); | |
2056 | ||
2057 | /* | |
2058 | * For encrypted images, read everything into a temporary | |
2059 | * contiguous buffer on which the AES functions can work. | |
2060 | * Also, decryption in a separate buffer is better as it | |
2061 | * prevents the guest from learning information about the | |
2062 | * encrypted nature of the virtual disk. | |
2063 | */ | |
2064 | ||
2065 | buf = qemu_try_blockalign(s->data_file->bs, bytes); | |
2066 | if (buf == NULL) { | |
2067 | return -ENOMEM; | |
2068 | } | |
2069 | ||
2070 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
2071 | ret = bdrv_co_pread(s->data_file, | |
2072 | file_cluster_offset + offset_into_cluster(s, offset), | |
2073 | bytes, buf, 0); | |
2074 | if (ret < 0) { | |
2075 | goto fail; | |
2076 | } | |
2077 | ||
88f468e5 VSO |
2078 | if (qcow2_co_decrypt(bs, |
2079 | file_cluster_offset + offset_into_cluster(s, offset), | |
2080 | offset, buf, bytes) < 0) | |
2081 | { | |
2082 | ret = -EIO; | |
2083 | goto fail; | |
2084 | } | |
2085 | qemu_iovec_from_buf(qiov, qiov_offset, buf, bytes); | |
2086 | ||
2087 | fail: | |
2088 | qemu_vfree(buf); | |
2089 | ||
2090 | return ret; | |
2091 | } | |
2092 | ||
d710cf57 VSO |
2093 | typedef struct Qcow2AioTask { |
2094 | AioTask task; | |
2095 | ||
2096 | BlockDriverState *bs; | |
2097 | QCow2ClusterType cluster_type; /* only for read */ | |
2098 | uint64_t file_cluster_offset; | |
2099 | uint64_t offset; | |
2100 | uint64_t bytes; | |
2101 | QEMUIOVector *qiov; | |
2102 | uint64_t qiov_offset; | |
2103 | QCowL2Meta *l2meta; /* only for write */ | |
2104 | } Qcow2AioTask; | |
2105 | ||
2106 | static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task); | |
2107 | static coroutine_fn int qcow2_add_task(BlockDriverState *bs, | |
2108 | AioTaskPool *pool, | |
2109 | AioTaskFunc func, | |
2110 | QCow2ClusterType cluster_type, | |
2111 | uint64_t file_cluster_offset, | |
2112 | uint64_t offset, | |
2113 | uint64_t bytes, | |
2114 | QEMUIOVector *qiov, | |
2115 | size_t qiov_offset, | |
2116 | QCowL2Meta *l2meta) | |
2117 | { | |
2118 | Qcow2AioTask local_task; | |
2119 | Qcow2AioTask *task = pool ? g_new(Qcow2AioTask, 1) : &local_task; | |
2120 | ||
2121 | *task = (Qcow2AioTask) { | |
2122 | .task.func = func, | |
2123 | .bs = bs, | |
2124 | .cluster_type = cluster_type, | |
2125 | .qiov = qiov, | |
2126 | .file_cluster_offset = file_cluster_offset, | |
2127 | .offset = offset, | |
2128 | .bytes = bytes, | |
2129 | .qiov_offset = qiov_offset, | |
2130 | .l2meta = l2meta, | |
2131 | }; | |
2132 | ||
2133 | trace_qcow2_add_task(qemu_coroutine_self(), bs, pool, | |
2134 | func == qcow2_co_preadv_task_entry ? "read" : "write", | |
2135 | cluster_type, file_cluster_offset, offset, bytes, | |
2136 | qiov, qiov_offset); | |
2137 | ||
2138 | if (!pool) { | |
2139 | return func(&task->task); | |
2140 | } | |
2141 | ||
2142 | aio_task_pool_start_task(pool, &task->task); | |
2143 | ||
2144 | return 0; | |
2145 | } | |
2146 | ||
88f468e5 VSO |
2147 | static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs, |
2148 | QCow2ClusterType cluster_type, | |
2149 | uint64_t file_cluster_offset, | |
2150 | uint64_t offset, uint64_t bytes, | |
2151 | QEMUIOVector *qiov, | |
2152 | size_t qiov_offset) | |
2153 | { | |
2154 | BDRVQcow2State *s = bs->opaque; | |
2155 | int offset_in_cluster = offset_into_cluster(s, offset); | |
2156 | ||
2157 | switch (cluster_type) { | |
2158 | case QCOW2_CLUSTER_ZERO_PLAIN: | |
2159 | case QCOW2_CLUSTER_ZERO_ALLOC: | |
2160 | /* Both zero types are handled in qcow2_co_preadv_part */ | |
2161 | g_assert_not_reached(); | |
2162 | ||
2163 | case QCOW2_CLUSTER_UNALLOCATED: | |
2164 | assert(bs->backing); /* otherwise handled in qcow2_co_preadv_part */ | |
2165 | ||
2166 | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); | |
2167 | return bdrv_co_preadv_part(bs->backing, offset, bytes, | |
2168 | qiov, qiov_offset, 0); | |
2169 | ||
2170 | case QCOW2_CLUSTER_COMPRESSED: | |
2171 | return qcow2_co_preadv_compressed(bs, file_cluster_offset, | |
2172 | offset, bytes, qiov, qiov_offset); | |
2173 | ||
2174 | case QCOW2_CLUSTER_NORMAL: | |
344ffea9 | 2175 | assert(offset_into_cluster(s, file_cluster_offset) == 0); |
88f468e5 VSO |
2176 | if (bs->encrypted) { |
2177 | return qcow2_co_preadv_encrypted(bs, file_cluster_offset, | |
2178 | offset, bytes, qiov, qiov_offset); | |
2179 | } | |
2180 | ||
2181 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
2182 | return bdrv_co_preadv_part(s->data_file, | |
2183 | file_cluster_offset + offset_in_cluster, | |
2184 | bytes, qiov, qiov_offset, 0); | |
2185 | ||
2186 | default: | |
2187 | g_assert_not_reached(); | |
2188 | } | |
2189 | ||
2190 | g_assert_not_reached(); | |
2191 | } | |
2192 | ||
d710cf57 VSO |
2193 | static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task) |
2194 | { | |
2195 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); | |
2196 | ||
2197 | assert(!t->l2meta); | |
2198 | ||
2199 | return qcow2_co_preadv_task(t->bs, t->cluster_type, t->file_cluster_offset, | |
2200 | t->offset, t->bytes, t->qiov, t->qiov_offset); | |
2201 | } | |
2202 | ||
df893d25 VSO |
2203 | static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs, |
2204 | uint64_t offset, uint64_t bytes, | |
2205 | QEMUIOVector *qiov, | |
2206 | size_t qiov_offset, int flags) | |
585f8587 | 2207 | { |
ff99129a | 2208 | BDRVQcow2State *s = bs->opaque; |
d710cf57 | 2209 | int ret = 0; |
ecfe1863 | 2210 | unsigned int cur_bytes; /* number of bytes in current iteration */ |
c2bdd990 | 2211 | uint64_t cluster_offset = 0; |
d710cf57 | 2212 | AioTaskPool *aio = NULL; |
585f8587 | 2213 | |
d710cf57 | 2214 | while (bytes != 0 && aio_task_pool_status(aio) == 0) { |
5ebaa27e | 2215 | /* prepare next request */ |
ecfe1863 | 2216 | cur_bytes = MIN(bytes, INT_MAX); |
b25b387f | 2217 | if (s->crypto) { |
ecfe1863 KW |
2218 | cur_bytes = MIN(cur_bytes, |
2219 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); | |
585f8587 | 2220 | } |
5ebaa27e | 2221 | |
f24196d3 | 2222 | qemu_co_mutex_lock(&s->lock); |
ecfe1863 | 2223 | ret = qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_offset); |
f24196d3 | 2224 | qemu_co_mutex_unlock(&s->lock); |
8af36488 | 2225 | if (ret < 0) { |
d710cf57 | 2226 | goto out; |
8af36488 | 2227 | } |
bd28f835 | 2228 | |
88f468e5 VSO |
2229 | if (ret == QCOW2_CLUSTER_ZERO_PLAIN || |
2230 | ret == QCOW2_CLUSTER_ZERO_ALLOC || | |
2231 | (ret == QCOW2_CLUSTER_UNALLOCATED && !bs->backing)) | |
2232 | { | |
df893d25 | 2233 | qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); |
88f468e5 | 2234 | } else { |
d710cf57 VSO |
2235 | if (!aio && cur_bytes != bytes) { |
2236 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); | |
2237 | } | |
2238 | ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, ret, | |
2239 | cluster_offset, offset, cur_bytes, | |
2240 | qiov, qiov_offset, NULL); | |
5ebaa27e | 2241 | if (ret < 0) { |
d710cf57 | 2242 | goto out; |
5ebaa27e | 2243 | } |
faf575c1 | 2244 | } |
f141eafe | 2245 | |
ecfe1863 KW |
2246 | bytes -= cur_bytes; |
2247 | offset += cur_bytes; | |
df893d25 | 2248 | qiov_offset += cur_bytes; |
5ebaa27e | 2249 | } |
68d100e9 | 2250 | |
d710cf57 VSO |
2251 | out: |
2252 | if (aio) { | |
2253 | aio_task_pool_wait_all(aio); | |
2254 | if (ret == 0) { | |
2255 | ret = aio_task_pool_status(aio); | |
2256 | } | |
2257 | g_free(aio); | |
2258 | } | |
2259 | ||
2260 | return ret; | |
585f8587 FB |
2261 | } |
2262 | ||
ee22a9d8 AG |
2263 | /* Check if it's possible to merge a write request with the writing of |
2264 | * the data from the COW regions */ | |
2265 | static bool merge_cow(uint64_t offset, unsigned bytes, | |
5396234b VSO |
2266 | QEMUIOVector *qiov, size_t qiov_offset, |
2267 | QCowL2Meta *l2meta) | |
ee22a9d8 AG |
2268 | { |
2269 | QCowL2Meta *m; | |
2270 | ||
2271 | for (m = l2meta; m != NULL; m = m->next) { | |
2272 | /* If both COW regions are empty then there's nothing to merge */ | |
2273 | if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) { | |
2274 | continue; | |
2275 | } | |
2276 | ||
c8bb23cb AN |
2277 | /* If COW regions are handled already, skip this too */ |
2278 | if (m->skip_cow) { | |
2279 | continue; | |
2280 | } | |
2281 | ||
ee22a9d8 AG |
2282 | /* The data (middle) region must be immediately after the |
2283 | * start region */ | |
2284 | if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) { | |
2285 | continue; | |
2286 | } | |
2287 | ||
2288 | /* The end region must be immediately after the data (middle) | |
2289 | * region */ | |
2290 | if (m->offset + m->cow_end.offset != offset + bytes) { | |
2291 | continue; | |
2292 | } | |
2293 | ||
2294 | /* Make sure that adding both COW regions to the QEMUIOVector | |
2295 | * does not exceed IOV_MAX */ | |
5396234b | 2296 | if (qemu_iovec_subvec_niov(qiov, qiov_offset, bytes) > IOV_MAX - 2) { |
ee22a9d8 AG |
2297 | continue; |
2298 | } | |
2299 | ||
5396234b VSO |
2300 | m->data_qiov = qiov; |
2301 | m->data_qiov_offset = qiov_offset; | |
ee22a9d8 AG |
2302 | return true; |
2303 | } | |
2304 | ||
2305 | return false; | |
2306 | } | |
2307 | ||
c8bb23cb AN |
2308 | static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t bytes) |
2309 | { | |
2310 | int64_t nr; | |
2311 | return !bytes || | |
170d3bd3 AS |
2312 | (!bdrv_is_allocated_above(bs, NULL, false, offset, bytes, &nr) && |
2313 | nr == bytes); | |
c8bb23cb AN |
2314 | } |
2315 | ||
2316 | static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m) | |
2317 | { | |
2318 | /* | |
2319 | * This check is designed for optimization shortcut so it must be | |
2320 | * efficient. | |
2321 | * Instead of is_zero(), use is_unallocated() as it is faster (but not | |
2322 | * as accurate and can result in false negatives). | |
2323 | */ | |
2324 | return is_unallocated(bs, m->offset + m->cow_start.offset, | |
2325 | m->cow_start.nb_bytes) && | |
2326 | is_unallocated(bs, m->offset + m->cow_end.offset, | |
2327 | m->cow_end.nb_bytes); | |
2328 | } | |
2329 | ||
2330 | static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) | |
2331 | { | |
2332 | BDRVQcow2State *s = bs->opaque; | |
2333 | QCowL2Meta *m; | |
2334 | ||
2335 | if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) { | |
2336 | return 0; | |
2337 | } | |
2338 | ||
2339 | if (bs->encrypted) { | |
2340 | return 0; | |
2341 | } | |
2342 | ||
2343 | for (m = l2meta; m != NULL; m = m->next) { | |
2344 | int ret; | |
2345 | ||
2346 | if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) { | |
2347 | continue; | |
2348 | } | |
2349 | ||
2350 | if (!is_zero_cow(bs, m)) { | |
2351 | continue; | |
2352 | } | |
2353 | ||
2354 | /* | |
2355 | * instead of writing zero COW buffers, | |
2356 | * efficiently zero out the whole clusters | |
2357 | */ | |
2358 | ||
2359 | ret = qcow2_pre_write_overlap_check(bs, 0, m->alloc_offset, | |
2360 | m->nb_clusters * s->cluster_size, | |
2361 | true); | |
2362 | if (ret < 0) { | |
2363 | return ret; | |
2364 | } | |
2365 | ||
2366 | BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE); | |
2367 | ret = bdrv_co_pwrite_zeroes(s->data_file, m->alloc_offset, | |
2368 | m->nb_clusters * s->cluster_size, | |
2369 | BDRV_REQ_NO_FALLBACK); | |
2370 | if (ret < 0) { | |
2371 | if (ret != -ENOTSUP && ret != -EAGAIN) { | |
2372 | return ret; | |
2373 | } | |
2374 | continue; | |
2375 | } | |
2376 | ||
2377 | trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters); | |
2378 | m->skip_cow = true; | |
2379 | } | |
2380 | return 0; | |
2381 | } | |
2382 | ||
6aa7a263 VSO |
2383 | /* |
2384 | * qcow2_co_pwritev_task | |
2385 | * Called with s->lock unlocked | |
2386 | * l2meta - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must | |
2387 | * not use it somehow after qcow2_co_pwritev_task() call | |
2388 | */ | |
2389 | static coroutine_fn int qcow2_co_pwritev_task(BlockDriverState *bs, | |
2390 | uint64_t file_cluster_offset, | |
2391 | uint64_t offset, uint64_t bytes, | |
2392 | QEMUIOVector *qiov, | |
2393 | uint64_t qiov_offset, | |
2394 | QCowL2Meta *l2meta) | |
2395 | { | |
2396 | int ret; | |
2397 | BDRVQcow2State *s = bs->opaque; | |
2398 | void *crypt_buf = NULL; | |
2399 | int offset_in_cluster = offset_into_cluster(s, offset); | |
2400 | QEMUIOVector encrypted_qiov; | |
2401 | ||
2402 | if (bs->encrypted) { | |
2403 | assert(s->crypto); | |
2404 | assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); | |
2405 | crypt_buf = qemu_try_blockalign(bs->file->bs, bytes); | |
2406 | if (crypt_buf == NULL) { | |
2407 | ret = -ENOMEM; | |
2408 | goto out_unlocked; | |
2409 | } | |
2410 | qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes); | |
2411 | ||
2412 | if (qcow2_co_encrypt(bs, file_cluster_offset + offset_in_cluster, | |
2413 | offset, crypt_buf, bytes) < 0) | |
2414 | { | |
2415 | ret = -EIO; | |
2416 | goto out_unlocked; | |
2417 | } | |
2418 | ||
2419 | qemu_iovec_init_buf(&encrypted_qiov, crypt_buf, bytes); | |
2420 | qiov = &encrypted_qiov; | |
2421 | qiov_offset = 0; | |
2422 | } | |
2423 | ||
2424 | /* Try to efficiently initialize the physical space with zeroes */ | |
2425 | ret = handle_alloc_space(bs, l2meta); | |
2426 | if (ret < 0) { | |
2427 | goto out_unlocked; | |
2428 | } | |
2429 | ||
2430 | /* | |
2431 | * If we need to do COW, check if it's possible to merge the | |
2432 | * writing of the guest data together with that of the COW regions. | |
2433 | * If it's not possible (or not necessary) then write the | |
2434 | * guest data now. | |
2435 | */ | |
2436 | if (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) { | |
2437 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); | |
2438 | trace_qcow2_writev_data(qemu_coroutine_self(), | |
2439 | file_cluster_offset + offset_in_cluster); | |
2440 | ret = bdrv_co_pwritev_part(s->data_file, | |
2441 | file_cluster_offset + offset_in_cluster, | |
2442 | bytes, qiov, qiov_offset, 0); | |
2443 | if (ret < 0) { | |
2444 | goto out_unlocked; | |
2445 | } | |
2446 | } | |
2447 | ||
2448 | qemu_co_mutex_lock(&s->lock); | |
2449 | ||
2450 | ret = qcow2_handle_l2meta(bs, &l2meta, true); | |
2451 | goto out_locked; | |
2452 | ||
2453 | out_unlocked: | |
2454 | qemu_co_mutex_lock(&s->lock); | |
2455 | ||
2456 | out_locked: | |
2457 | qcow2_handle_l2meta(bs, &l2meta, false); | |
2458 | qemu_co_mutex_unlock(&s->lock); | |
2459 | ||
2460 | qemu_vfree(crypt_buf); | |
2461 | ||
2462 | return ret; | |
2463 | } | |
2464 | ||
d710cf57 VSO |
2465 | static coroutine_fn int qcow2_co_pwritev_task_entry(AioTask *task) |
2466 | { | |
2467 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); | |
2468 | ||
2469 | assert(!t->cluster_type); | |
2470 | ||
2471 | return qcow2_co_pwritev_task(t->bs, t->file_cluster_offset, | |
2472 | t->offset, t->bytes, t->qiov, t->qiov_offset, | |
2473 | t->l2meta); | |
2474 | } | |
2475 | ||
5396234b VSO |
2476 | static coroutine_fn int qcow2_co_pwritev_part( |
2477 | BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
2478 | QEMUIOVector *qiov, size_t qiov_offset, int flags) | |
585f8587 | 2479 | { |
ff99129a | 2480 | BDRVQcow2State *s = bs->opaque; |
d46a0bb2 | 2481 | int offset_in_cluster; |
68d100e9 | 2482 | int ret; |
d46a0bb2 | 2483 | unsigned int cur_bytes; /* number of sectors in current iteration */ |
c2bdd990 | 2484 | uint64_t cluster_offset; |
8d2497c3 | 2485 | QCowL2Meta *l2meta = NULL; |
d710cf57 | 2486 | AioTaskPool *aio = NULL; |
c2271403 | 2487 | |
d46a0bb2 | 2488 | trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes); |
3cce16f4 | 2489 | |
d710cf57 | 2490 | while (bytes != 0 && aio_task_pool_status(aio) == 0) { |
3fc48d09 | 2491 | |
f50f88b9 | 2492 | l2meta = NULL; |
cf5c1a23 | 2493 | |
3cce16f4 | 2494 | trace_qcow2_writev_start_part(qemu_coroutine_self()); |
d46a0bb2 KW |
2495 | offset_in_cluster = offset_into_cluster(s, offset); |
2496 | cur_bytes = MIN(bytes, INT_MAX); | |
2497 | if (bs->encrypted) { | |
2498 | cur_bytes = MIN(cur_bytes, | |
2499 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size | |
2500 | - offset_in_cluster); | |
5ebaa27e | 2501 | } |
095a9c58 | 2502 | |
6aa7a263 VSO |
2503 | qemu_co_mutex_lock(&s->lock); |
2504 | ||
d46a0bb2 KW |
2505 | ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes, |
2506 | &cluster_offset, &l2meta); | |
5ebaa27e | 2507 | if (ret < 0) { |
5447c3a0 | 2508 | goto out_locked; |
5ebaa27e | 2509 | } |
148da7ea | 2510 | |
344ffea9 | 2511 | assert(offset_into_cluster(s, cluster_offset) == 0); |
148da7ea | 2512 | |
5447c3a0 VSO |
2513 | ret = qcow2_pre_write_overlap_check(bs, 0, |
2514 | cluster_offset + offset_in_cluster, | |
2515 | cur_bytes, true); | |
2516 | if (ret < 0) { | |
2517 | goto out_locked; | |
2518 | } | |
2519 | ||
2520 | qemu_co_mutex_unlock(&s->lock); | |
2521 | ||
d710cf57 VSO |
2522 | if (!aio && cur_bytes != bytes) { |
2523 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); | |
2524 | } | |
2525 | ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0, | |
2526 | cluster_offset, offset, cur_bytes, | |
2527 | qiov, qiov_offset, l2meta); | |
6aa7a263 | 2528 | l2meta = NULL; /* l2meta is consumed by qcow2_co_pwritev_task() */ |
c8bb23cb | 2529 | if (ret < 0) { |
6aa7a263 | 2530 | goto fail_nometa; |
f50f88b9 | 2531 | } |
0fa9131a | 2532 | |
d46a0bb2 KW |
2533 | bytes -= cur_bytes; |
2534 | offset += cur_bytes; | |
6aa7a263 | 2535 | qiov_offset += cur_bytes; |
d46a0bb2 | 2536 | trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes); |
5ebaa27e | 2537 | } |
3fc48d09 | 2538 | ret = 0; |
faf575c1 | 2539 | |
5447c3a0 VSO |
2540 | qemu_co_mutex_lock(&s->lock); |
2541 | ||
2542 | out_locked: | |
fd9fcd37 | 2543 | qcow2_handle_l2meta(bs, &l2meta, false); |
0fa9131a | 2544 | |
a8c57408 PB |
2545 | qemu_co_mutex_unlock(&s->lock); |
2546 | ||
6aa7a263 | 2547 | fail_nometa: |
d710cf57 VSO |
2548 | if (aio) { |
2549 | aio_task_pool_wait_all(aio); | |
2550 | if (ret == 0) { | |
2551 | ret = aio_task_pool_status(aio); | |
2552 | } | |
2553 | g_free(aio); | |
2554 | } | |
2555 | ||
3cce16f4 | 2556 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
42496d62 | 2557 | |
68d100e9 | 2558 | return ret; |
585f8587 FB |
2559 | } |
2560 | ||
ec6d8912 KW |
2561 | static int qcow2_inactivate(BlockDriverState *bs) |
2562 | { | |
2563 | BDRVQcow2State *s = bs->opaque; | |
2564 | int ret, result = 0; | |
5f72826e | 2565 | Error *local_err = NULL; |
ec6d8912 | 2566 | |
644ddbb7 | 2567 | qcow2_store_persistent_dirty_bitmaps(bs, true, &local_err); |
83a8c775 PB |
2568 | if (local_err != NULL) { |
2569 | result = -EINVAL; | |
132adb68 VSO |
2570 | error_reportf_err(local_err, "Lost persistent bitmaps during " |
2571 | "inactivation of node '%s': ", | |
2572 | bdrv_get_device_or_node_name(bs)); | |
83a8c775 PB |
2573 | } |
2574 | ||
ec6d8912 KW |
2575 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
2576 | if (ret) { | |
2577 | result = ret; | |
2578 | error_report("Failed to flush the L2 table cache: %s", | |
2579 | strerror(-ret)); | |
2580 | } | |
2581 | ||
2582 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
2583 | if (ret) { | |
2584 | result = ret; | |
2585 | error_report("Failed to flush the refcount block cache: %s", | |
2586 | strerror(-ret)); | |
2587 | } | |
2588 | ||
2589 | if (result == 0) { | |
2590 | qcow2_mark_clean(bs); | |
2591 | } | |
2592 | ||
2593 | return result; | |
2594 | } | |
2595 | ||
7c80ab3f | 2596 | static void qcow2_close(BlockDriverState *bs) |
585f8587 | 2597 | { |
ff99129a | 2598 | BDRVQcow2State *s = bs->opaque; |
de82815d | 2599 | qemu_vfree(s->l1_table); |
cf93980e HR |
2600 | /* else pre-write overlap checks in cache_destroy may crash */ |
2601 | s->l1_table = NULL; | |
29c1a730 | 2602 | |
140fd5a6 | 2603 | if (!(s->flags & BDRV_O_INACTIVE)) { |
ec6d8912 | 2604 | qcow2_inactivate(bs); |
27eb6c09 | 2605 | } |
c61d0004 | 2606 | |
279621c0 | 2607 | cache_clean_timer_del(bs); |
e64d4072 AG |
2608 | qcow2_cache_destroy(s->l2_table_cache); |
2609 | qcow2_cache_destroy(s->refcount_block_cache); | |
29c1a730 | 2610 | |
b25b387f DB |
2611 | qcrypto_block_free(s->crypto); |
2612 | s->crypto = NULL; | |
f6fa64f6 | 2613 | |
6744cbab | 2614 | g_free(s->unknown_header_fields); |
75bab85c | 2615 | cleanup_unknown_header_ext(bs); |
6744cbab | 2616 | |
9b890bdc | 2617 | g_free(s->image_data_file); |
e4603fe1 KW |
2618 | g_free(s->image_backing_file); |
2619 | g_free(s->image_backing_format); | |
2620 | ||
0e8c08be KW |
2621 | if (has_data_file(bs)) { |
2622 | bdrv_unref_child(bs, s->data_file); | |
2623 | } | |
2624 | ||
ed6ccf0f | 2625 | qcow2_refcount_close(bs); |
28c1202b | 2626 | qcow2_free_snapshots(bs); |
585f8587 FB |
2627 | } |
2628 | ||
2b148f39 PB |
2629 | static void coroutine_fn qcow2_co_invalidate_cache(BlockDriverState *bs, |
2630 | Error **errp) | |
06d9260f | 2631 | { |
ff99129a | 2632 | BDRVQcow2State *s = bs->opaque; |
06d9260f | 2633 | int flags = s->flags; |
b25b387f | 2634 | QCryptoBlock *crypto = NULL; |
acdfb480 | 2635 | QDict *options; |
5a8a30db KW |
2636 | Error *local_err = NULL; |
2637 | int ret; | |
06d9260f AL |
2638 | |
2639 | /* | |
2640 | * Backing files are read-only which makes all of their metadata immutable, | |
2641 | * that means we don't have to worry about reopening them here. | |
2642 | */ | |
2643 | ||
b25b387f DB |
2644 | crypto = s->crypto; |
2645 | s->crypto = NULL; | |
06d9260f AL |
2646 | |
2647 | qcow2_close(bs); | |
2648 | ||
ff99129a | 2649 | memset(s, 0, sizeof(BDRVQcow2State)); |
d475e5ac | 2650 | options = qdict_clone_shallow(bs->options); |
5a8a30db | 2651 | |
140fd5a6 | 2652 | flags &= ~BDRV_O_INACTIVE; |
2b148f39 | 2653 | qemu_co_mutex_lock(&s->lock); |
4e4bf5c4 | 2654 | ret = qcow2_do_open(bs, options, flags, &local_err); |
2b148f39 | 2655 | qemu_co_mutex_unlock(&s->lock); |
cb3e7f08 | 2656 | qobject_unref(options); |
5a8a30db | 2657 | if (local_err) { |
4b576648 MA |
2658 | error_propagate_prepend(errp, local_err, |
2659 | "Could not reopen qcow2 layer: "); | |
191fb11b | 2660 | bs->drv = NULL; |
5a8a30db KW |
2661 | return; |
2662 | } else if (ret < 0) { | |
2663 | error_setg_errno(errp, -ret, "Could not reopen qcow2 layer"); | |
191fb11b | 2664 | bs->drv = NULL; |
5a8a30db KW |
2665 | return; |
2666 | } | |
acdfb480 | 2667 | |
b25b387f | 2668 | s->crypto = crypto; |
06d9260f AL |
2669 | } |
2670 | ||
e24e49e6 KW |
2671 | static size_t header_ext_add(char *buf, uint32_t magic, const void *s, |
2672 | size_t len, size_t buflen) | |
2673 | { | |
2674 | QCowExtension *ext_backing_fmt = (QCowExtension*) buf; | |
2675 | size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); | |
2676 | ||
2677 | if (buflen < ext_len) { | |
2678 | return -ENOSPC; | |
2679 | } | |
2680 | ||
2681 | *ext_backing_fmt = (QCowExtension) { | |
2682 | .magic = cpu_to_be32(magic), | |
2683 | .len = cpu_to_be32(len), | |
2684 | }; | |
0647d47c SH |
2685 | |
2686 | if (len) { | |
2687 | memcpy(buf + sizeof(QCowExtension), s, len); | |
2688 | } | |
e24e49e6 KW |
2689 | |
2690 | return ext_len; | |
2691 | } | |
2692 | ||
756e6736 | 2693 | /* |
e24e49e6 KW |
2694 | * Updates the qcow2 header, including the variable length parts of it, i.e. |
2695 | * the backing file name and all extensions. qcow2 was not designed to allow | |
2696 | * such changes, so if we run out of space (we can only use the first cluster) | |
2697 | * this function may fail. | |
756e6736 KW |
2698 | * |
2699 | * Returns 0 on success, -errno in error cases. | |
2700 | */ | |
e24e49e6 | 2701 | int qcow2_update_header(BlockDriverState *bs) |
756e6736 | 2702 | { |
ff99129a | 2703 | BDRVQcow2State *s = bs->opaque; |
e24e49e6 KW |
2704 | QCowHeader *header; |
2705 | char *buf; | |
2706 | size_t buflen = s->cluster_size; | |
756e6736 | 2707 | int ret; |
e24e49e6 KW |
2708 | uint64_t total_size; |
2709 | uint32_t refcount_table_clusters; | |
6744cbab | 2710 | size_t header_length; |
75bab85c | 2711 | Qcow2UnknownHeaderExtension *uext; |
756e6736 | 2712 | |
e24e49e6 | 2713 | buf = qemu_blockalign(bs, buflen); |
756e6736 | 2714 | |
e24e49e6 KW |
2715 | /* Header structure */ |
2716 | header = (QCowHeader*) buf; | |
756e6736 | 2717 | |
e24e49e6 KW |
2718 | if (buflen < sizeof(*header)) { |
2719 | ret = -ENOSPC; | |
2720 | goto fail; | |
756e6736 KW |
2721 | } |
2722 | ||
6744cbab | 2723 | header_length = sizeof(*header) + s->unknown_header_fields_size; |
e24e49e6 KW |
2724 | total_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
2725 | refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); | |
2726 | ||
2727 | *header = (QCowHeader) { | |
6744cbab | 2728 | /* Version 2 fields */ |
e24e49e6 | 2729 | .magic = cpu_to_be32(QCOW_MAGIC), |
6744cbab | 2730 | .version = cpu_to_be32(s->qcow_version), |
e24e49e6 KW |
2731 | .backing_file_offset = 0, |
2732 | .backing_file_size = 0, | |
2733 | .cluster_bits = cpu_to_be32(s->cluster_bits), | |
2734 | .size = cpu_to_be64(total_size), | |
2735 | .crypt_method = cpu_to_be32(s->crypt_method_header), | |
2736 | .l1_size = cpu_to_be32(s->l1_size), | |
2737 | .l1_table_offset = cpu_to_be64(s->l1_table_offset), | |
2738 | .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), | |
2739 | .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), | |
2740 | .nb_snapshots = cpu_to_be32(s->nb_snapshots), | |
2741 | .snapshots_offset = cpu_to_be64(s->snapshots_offset), | |
6744cbab KW |
2742 | |
2743 | /* Version 3 fields */ | |
2744 | .incompatible_features = cpu_to_be64(s->incompatible_features), | |
2745 | .compatible_features = cpu_to_be64(s->compatible_features), | |
2746 | .autoclear_features = cpu_to_be64(s->autoclear_features), | |
b6481f37 | 2747 | .refcount_order = cpu_to_be32(s->refcount_order), |
6744cbab | 2748 | .header_length = cpu_to_be32(header_length), |
e24e49e6 | 2749 | }; |
756e6736 | 2750 | |
6744cbab KW |
2751 | /* For older versions, write a shorter header */ |
2752 | switch (s->qcow_version) { | |
2753 | case 2: | |
2754 | ret = offsetof(QCowHeader, incompatible_features); | |
2755 | break; | |
2756 | case 3: | |
2757 | ret = sizeof(*header); | |
2758 | break; | |
2759 | default: | |
b6c14762 JM |
2760 | ret = -EINVAL; |
2761 | goto fail; | |
6744cbab KW |
2762 | } |
2763 | ||
2764 | buf += ret; | |
2765 | buflen -= ret; | |
2766 | memset(buf, 0, buflen); | |
2767 | ||
2768 | /* Preserve any unknown field in the header */ | |
2769 | if (s->unknown_header_fields_size) { | |
2770 | if (buflen < s->unknown_header_fields_size) { | |
2771 | ret = -ENOSPC; | |
2772 | goto fail; | |
2773 | } | |
2774 | ||
2775 | memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); | |
2776 | buf += s->unknown_header_fields_size; | |
2777 | buflen -= s->unknown_header_fields_size; | |
2778 | } | |
756e6736 | 2779 | |
e24e49e6 | 2780 | /* Backing file format header extension */ |
e4603fe1 | 2781 | if (s->image_backing_format) { |
e24e49e6 | 2782 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, |
e4603fe1 KW |
2783 | s->image_backing_format, |
2784 | strlen(s->image_backing_format), | |
e24e49e6 KW |
2785 | buflen); |
2786 | if (ret < 0) { | |
2787 | goto fail; | |
756e6736 KW |
2788 | } |
2789 | ||
e24e49e6 KW |
2790 | buf += ret; |
2791 | buflen -= ret; | |
756e6736 KW |
2792 | } |
2793 | ||
9b890bdc KW |
2794 | /* External data file header extension */ |
2795 | if (has_data_file(bs) && s->image_data_file) { | |
2796 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE, | |
2797 | s->image_data_file, strlen(s->image_data_file), | |
2798 | buflen); | |
2799 | if (ret < 0) { | |
2800 | goto fail; | |
2801 | } | |
2802 | ||
2803 | buf += ret; | |
2804 | buflen -= ret; | |
2805 | } | |
2806 | ||
4652b8f3 DB |
2807 | /* Full disk encryption header pointer extension */ |
2808 | if (s->crypto_header.offset != 0) { | |
3b698f52 PM |
2809 | s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset); |
2810 | s->crypto_header.length = cpu_to_be64(s->crypto_header.length); | |
4652b8f3 DB |
2811 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER, |
2812 | &s->crypto_header, sizeof(s->crypto_header), | |
2813 | buflen); | |
3b698f52 PM |
2814 | s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset); |
2815 | s->crypto_header.length = be64_to_cpu(s->crypto_header.length); | |
4652b8f3 DB |
2816 | if (ret < 0) { |
2817 | goto fail; | |
2818 | } | |
2819 | buf += ret; | |
2820 | buflen -= ret; | |
2821 | } | |
2822 | ||
cfcc4c62 | 2823 | /* Feature table */ |
1a4828c7 KW |
2824 | if (s->qcow_version >= 3) { |
2825 | Qcow2Feature features[] = { | |
2826 | { | |
2827 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
2828 | .bit = QCOW2_INCOMPAT_DIRTY_BITNR, | |
2829 | .name = "dirty bit", | |
2830 | }, | |
2831 | { | |
2832 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
2833 | .bit = QCOW2_INCOMPAT_CORRUPT_BITNR, | |
2834 | .name = "corrupt bit", | |
2835 | }, | |
93c24936 KW |
2836 | { |
2837 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
2838 | .bit = QCOW2_INCOMPAT_DATA_FILE_BITNR, | |
2839 | .name = "external data file", | |
2840 | }, | |
1a4828c7 KW |
2841 | { |
2842 | .type = QCOW2_FEAT_TYPE_COMPATIBLE, | |
2843 | .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
2844 | .name = "lazy refcounts", | |
2845 | }, | |
2846 | }; | |
2847 | ||
2848 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, | |
2849 | features, sizeof(features), buflen); | |
2850 | if (ret < 0) { | |
2851 | goto fail; | |
2852 | } | |
2853 | buf += ret; | |
2854 | buflen -= ret; | |
cfcc4c62 | 2855 | } |
cfcc4c62 | 2856 | |
88ddffae VSO |
2857 | /* Bitmap extension */ |
2858 | if (s->nb_bitmaps > 0) { | |
2859 | Qcow2BitmapHeaderExt bitmaps_header = { | |
2860 | .nb_bitmaps = cpu_to_be32(s->nb_bitmaps), | |
2861 | .bitmap_directory_size = | |
2862 | cpu_to_be64(s->bitmap_directory_size), | |
2863 | .bitmap_directory_offset = | |
2864 | cpu_to_be64(s->bitmap_directory_offset) | |
2865 | }; | |
2866 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS, | |
2867 | &bitmaps_header, sizeof(bitmaps_header), | |
2868 | buflen); | |
2869 | if (ret < 0) { | |
2870 | goto fail; | |
2871 | } | |
2872 | buf += ret; | |
2873 | buflen -= ret; | |
2874 | } | |
2875 | ||
75bab85c KW |
2876 | /* Keep unknown header extensions */ |
2877 | QLIST_FOREACH(uext, &s->unknown_header_ext, next) { | |
2878 | ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); | |
2879 | if (ret < 0) { | |
2880 | goto fail; | |
2881 | } | |
2882 | ||
2883 | buf += ret; | |
2884 | buflen -= ret; | |
2885 | } | |
2886 | ||
e24e49e6 KW |
2887 | /* End of header extensions */ |
2888 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); | |
756e6736 KW |
2889 | if (ret < 0) { |
2890 | goto fail; | |
2891 | } | |
2892 | ||
e24e49e6 KW |
2893 | buf += ret; |
2894 | buflen -= ret; | |
756e6736 | 2895 | |
e24e49e6 | 2896 | /* Backing file name */ |
e4603fe1 KW |
2897 | if (s->image_backing_file) { |
2898 | size_t backing_file_len = strlen(s->image_backing_file); | |
e24e49e6 KW |
2899 | |
2900 | if (buflen < backing_file_len) { | |
2901 | ret = -ENOSPC; | |
2902 | goto fail; | |
2903 | } | |
2904 | ||
00ea1881 | 2905 | /* Using strncpy is ok here, since buf is not NUL-terminated. */ |
e4603fe1 | 2906 | strncpy(buf, s->image_backing_file, buflen); |
e24e49e6 KW |
2907 | |
2908 | header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); | |
2909 | header->backing_file_size = cpu_to_be32(backing_file_len); | |
756e6736 KW |
2910 | } |
2911 | ||
e24e49e6 | 2912 | /* Write the new header */ |
d9ca2ea2 | 2913 | ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); |
756e6736 KW |
2914 | if (ret < 0) { |
2915 | goto fail; | |
2916 | } | |
2917 | ||
2918 | ret = 0; | |
2919 | fail: | |
e24e49e6 | 2920 | qemu_vfree(header); |
756e6736 KW |
2921 | return ret; |
2922 | } | |
2923 | ||
2924 | static int qcow2_change_backing_file(BlockDriverState *bs, | |
2925 | const char *backing_file, const char *backing_fmt) | |
2926 | { | |
ff99129a | 2927 | BDRVQcow2State *s = bs->opaque; |
e4603fe1 | 2928 | |
6c3944dc KW |
2929 | /* Adding a backing file means that the external data file alone won't be |
2930 | * enough to make sense of the content */ | |
2931 | if (backing_file && data_file_is_raw(bs)) { | |
2932 | return -EINVAL; | |
2933 | } | |
2934 | ||
4e876bcf HR |
2935 | if (backing_file && strlen(backing_file) > 1023) { |
2936 | return -EINVAL; | |
2937 | } | |
2938 | ||
998c2019 HR |
2939 | pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file), |
2940 | backing_file ?: ""); | |
e24e49e6 KW |
2941 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
2942 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
2943 | ||
e4603fe1 KW |
2944 | g_free(s->image_backing_file); |
2945 | g_free(s->image_backing_format); | |
2946 | ||
2947 | s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL; | |
2948 | s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL; | |
2949 | ||
e24e49e6 | 2950 | return qcow2_update_header(bs); |
756e6736 KW |
2951 | } |
2952 | ||
4652b8f3 DB |
2953 | static int qcow2_crypt_method_from_format(const char *encryptfmt) |
2954 | { | |
2955 | if (g_str_equal(encryptfmt, "luks")) { | |
2956 | return QCOW_CRYPT_LUKS; | |
2957 | } else if (g_str_equal(encryptfmt, "aes")) { | |
2958 | return QCOW_CRYPT_AES; | |
2959 | } else { | |
2960 | return -EINVAL; | |
2961 | } | |
2962 | } | |
b25b387f | 2963 | |
60900b7b KW |
2964 | static int qcow2_set_up_encryption(BlockDriverState *bs, |
2965 | QCryptoBlockCreateOptions *cryptoopts, | |
2966 | Error **errp) | |
2967 | { | |
2968 | BDRVQcow2State *s = bs->opaque; | |
2969 | QCryptoBlock *crypto = NULL; | |
2970 | int fmt, ret; | |
2971 | ||
2972 | switch (cryptoopts->format) { | |
2973 | case Q_CRYPTO_BLOCK_FORMAT_LUKS: | |
2974 | fmt = QCOW_CRYPT_LUKS; | |
2975 | break; | |
2976 | case Q_CRYPTO_BLOCK_FORMAT_QCOW: | |
2977 | fmt = QCOW_CRYPT_AES; | |
2978 | break; | |
2979 | default: | |
2980 | error_setg(errp, "Crypto format not supported in qcow2"); | |
2981 | return -EINVAL; | |
b25b387f | 2982 | } |
60900b7b | 2983 | |
4652b8f3 | 2984 | s->crypt_method_header = fmt; |
b25b387f | 2985 | |
1cd9a787 | 2986 | crypto = qcrypto_block_create(cryptoopts, "encrypt.", |
4652b8f3 DB |
2987 | qcow2_crypto_hdr_init_func, |
2988 | qcow2_crypto_hdr_write_func, | |
b25b387f DB |
2989 | bs, errp); |
2990 | if (!crypto) { | |
60900b7b | 2991 | return -EINVAL; |
b25b387f DB |
2992 | } |
2993 | ||
2994 | ret = qcow2_update_header(bs); | |
2995 | if (ret < 0) { | |
2996 | error_setg_errno(errp, -ret, "Could not write encryption header"); | |
2997 | goto out; | |
2998 | } | |
2999 | ||
60900b7b | 3000 | ret = 0; |
b25b387f | 3001 | out: |
b25b387f | 3002 | qcrypto_block_free(crypto); |
b25b387f DB |
3003 | return ret; |
3004 | } | |
3005 | ||
7bc45dc1 HR |
3006 | /** |
3007 | * Preallocates metadata structures for data clusters between @offset (in the | |
3008 | * guest disk) and @new_length (which is thus generally the new guest disk | |
3009 | * size). | |
3010 | * | |
3011 | * Returns: 0 on success, -errno on failure. | |
3012 | */ | |
47e86b86 | 3013 | static int coroutine_fn preallocate_co(BlockDriverState *bs, uint64_t offset, |
718c0fce KW |
3014 | uint64_t new_length, PreallocMode mode, |
3015 | Error **errp) | |
a35e1c17 | 3016 | { |
93e32b3e | 3017 | BDRVQcow2State *s = bs->opaque; |
d46a0bb2 | 3018 | uint64_t bytes; |
060bee89 | 3019 | uint64_t host_offset = 0; |
718c0fce | 3020 | int64_t file_length; |
d46a0bb2 | 3021 | unsigned int cur_bytes; |
148da7ea | 3022 | int ret; |
f50f88b9 | 3023 | QCowL2Meta *meta; |
a35e1c17 | 3024 | |
7bc45dc1 HR |
3025 | assert(offset <= new_length); |
3026 | bytes = new_length - offset; | |
a35e1c17 | 3027 | |
d46a0bb2 | 3028 | while (bytes) { |
f29fbf7c | 3029 | cur_bytes = MIN(bytes, QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size)); |
d46a0bb2 | 3030 | ret = qcow2_alloc_cluster_offset(bs, offset, &cur_bytes, |
060bee89 | 3031 | &host_offset, &meta); |
148da7ea | 3032 | if (ret < 0) { |
360bd074 | 3033 | error_setg_errno(errp, -ret, "Allocating clusters failed"); |
47e86b86 | 3034 | return ret; |
a35e1c17 KW |
3035 | } |
3036 | ||
c792707f SH |
3037 | while (meta) { |
3038 | QCowL2Meta *next = meta->next; | |
3039 | ||
7c2bbf4a HT |
3040 | ret = qcow2_alloc_cluster_link_l2(bs, meta); |
3041 | if (ret < 0) { | |
360bd074 | 3042 | error_setg_errno(errp, -ret, "Mapping clusters failed"); |
7c2bbf4a HT |
3043 | qcow2_free_any_clusters(bs, meta->alloc_offset, |
3044 | meta->nb_clusters, QCOW2_DISCARD_NEVER); | |
47e86b86 | 3045 | return ret; |
7c2bbf4a HT |
3046 | } |
3047 | ||
3048 | /* There are no dependent requests, but we need to remove our | |
3049 | * request from the list of in-flight requests */ | |
4e95314e | 3050 | QLIST_REMOVE(meta, next_in_flight); |
c792707f SH |
3051 | |
3052 | g_free(meta); | |
3053 | meta = next; | |
f50f88b9 | 3054 | } |
f214978a | 3055 | |
a35e1c17 KW |
3056 | /* TODO Preallocate data if requested */ |
3057 | ||
d46a0bb2 KW |
3058 | bytes -= cur_bytes; |
3059 | offset += cur_bytes; | |
a35e1c17 KW |
3060 | } |
3061 | ||
3062 | /* | |
3063 | * It is expected that the image file is large enough to actually contain | |
3064 | * all of the allocated clusters (otherwise we get failing reads after | |
3065 | * EOF). Extend the image to the last allocated sector. | |
3066 | */ | |
718c0fce KW |
3067 | file_length = bdrv_getlength(s->data_file->bs); |
3068 | if (file_length < 0) { | |
3069 | error_setg_errno(errp, -file_length, "Could not get file size"); | |
3070 | return file_length; | |
3071 | } | |
3072 | ||
3073 | if (host_offset + cur_bytes > file_length) { | |
3074 | if (mode == PREALLOC_MODE_METADATA) { | |
3075 | mode = PREALLOC_MODE_OFF; | |
3076 | } | |
c80d8b06 HR |
3077 | ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false, |
3078 | mode, errp); | |
19dbcbf7 | 3079 | if (ret < 0) { |
47e86b86 | 3080 | return ret; |
19dbcbf7 | 3081 | } |
a35e1c17 KW |
3082 | } |
3083 | ||
47e86b86 | 3084 | return 0; |
a35e1c17 KW |
3085 | } |
3086 | ||
7c5bcc42 SH |
3087 | /* qcow2_refcount_metadata_size: |
3088 | * @clusters: number of clusters to refcount (including data and L1/L2 tables) | |
3089 | * @cluster_size: size of a cluster, in bytes | |
3090 | * @refcount_order: refcount bits power-of-2 exponent | |
12cc30a8 HR |
3091 | * @generous_increase: allow for the refcount table to be 1.5x as large as it |
3092 | * needs to be | |
7c5bcc42 SH |
3093 | * |
3094 | * Returns: Number of bytes required for refcount blocks and table metadata. | |
3095 | */ | |
12cc30a8 HR |
3096 | int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size, |
3097 | int refcount_order, bool generous_increase, | |
3098 | uint64_t *refblock_count) | |
7c5bcc42 SH |
3099 | { |
3100 | /* | |
3101 | * Every host cluster is reference-counted, including metadata (even | |
3102 | * refcount metadata is recursively included). | |
3103 | * | |
3104 | * An accurate formula for the size of refcount metadata size is difficult | |
3105 | * to derive. An easier method of calculation is finding the fixed point | |
3106 | * where no further refcount blocks or table clusters are required to | |
3107 | * reference count every cluster. | |
3108 | */ | |
3109 | int64_t blocks_per_table_cluster = cluster_size / sizeof(uint64_t); | |
3110 | int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order); | |
3111 | int64_t table = 0; /* number of refcount table clusters */ | |
3112 | int64_t blocks = 0; /* number of refcount block clusters */ | |
3113 | int64_t last; | |
3114 | int64_t n = 0; | |
3115 | ||
3116 | do { | |
3117 | last = n; | |
3118 | blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block); | |
3119 | table = DIV_ROUND_UP(blocks, blocks_per_table_cluster); | |
3120 | n = clusters + blocks + table; | |
12cc30a8 HR |
3121 | |
3122 | if (n == last && generous_increase) { | |
3123 | clusters += DIV_ROUND_UP(table, 2); | |
3124 | n = 0; /* force another loop */ | |
3125 | generous_increase = false; | |
3126 | } | |
7c5bcc42 SH |
3127 | } while (n != last); |
3128 | ||
12cc30a8 HR |
3129 | if (refblock_count) { |
3130 | *refblock_count = blocks; | |
3131 | } | |
3132 | ||
7c5bcc42 SH |
3133 | return (blocks + table) * cluster_size; |
3134 | } | |
3135 | ||
95c67e3b SH |
3136 | /** |
3137 | * qcow2_calc_prealloc_size: | |
3138 | * @total_size: virtual disk size in bytes | |
3139 | * @cluster_size: cluster size in bytes | |
3140 | * @refcount_order: refcount bits power-of-2 exponent | |
3141 | * | |
3142 | * Returns: Total number of bytes required for the fully allocated image | |
3143 | * (including metadata). | |
3144 | */ | |
3145 | static int64_t qcow2_calc_prealloc_size(int64_t total_size, | |
3146 | size_t cluster_size, | |
3147 | int refcount_order) | |
3148 | { | |
95c67e3b | 3149 | int64_t meta_size = 0; |
7c5bcc42 | 3150 | uint64_t nl1e, nl2e; |
9e029689 | 3151 | int64_t aligned_total_size = ROUND_UP(total_size, cluster_size); |
95c67e3b SH |
3152 | |
3153 | /* header: 1 cluster */ | |
3154 | meta_size += cluster_size; | |
3155 | ||
3156 | /* total size of L2 tables */ | |
3157 | nl2e = aligned_total_size / cluster_size; | |
9e029689 | 3158 | nl2e = ROUND_UP(nl2e, cluster_size / sizeof(uint64_t)); |
95c67e3b SH |
3159 | meta_size += nl2e * sizeof(uint64_t); |
3160 | ||
3161 | /* total size of L1 tables */ | |
3162 | nl1e = nl2e * sizeof(uint64_t) / cluster_size; | |
9e029689 | 3163 | nl1e = ROUND_UP(nl1e, cluster_size / sizeof(uint64_t)); |
95c67e3b SH |
3164 | meta_size += nl1e * sizeof(uint64_t); |
3165 | ||
7c5bcc42 SH |
3166 | /* total size of refcount table and blocks */ |
3167 | meta_size += qcow2_refcount_metadata_size( | |
3168 | (meta_size + aligned_total_size) / cluster_size, | |
12cc30a8 | 3169 | cluster_size, refcount_order, false, NULL); |
95c67e3b SH |
3170 | |
3171 | return meta_size + aligned_total_size; | |
3172 | } | |
3173 | ||
29ca9e45 | 3174 | static bool validate_cluster_size(size_t cluster_size, Error **errp) |
a9420734 | 3175 | { |
29ca9e45 | 3176 | int cluster_bits = ctz32(cluster_size); |
a9420734 KW |
3177 | if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || |
3178 | (1 << cluster_bits) != cluster_size) | |
3179 | { | |
3ef6c40a HR |
3180 | error_setg(errp, "Cluster size must be a power of two between %d and " |
3181 | "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); | |
29ca9e45 KW |
3182 | return false; |
3183 | } | |
3184 | return true; | |
3185 | } | |
3186 | ||
3187 | static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, Error **errp) | |
3188 | { | |
3189 | size_t cluster_size; | |
3190 | ||
3191 | cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, | |
3192 | DEFAULT_CLUSTER_SIZE); | |
3193 | if (!validate_cluster_size(cluster_size, errp)) { | |
0eb4a8c1 SH |
3194 | return 0; |
3195 | } | |
3196 | return cluster_size; | |
3197 | } | |
3198 | ||
3199 | static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp) | |
3200 | { | |
3201 | char *buf; | |
3202 | int ret; | |
3203 | ||
3204 | buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL); | |
3205 | if (!buf) { | |
3206 | ret = 3; /* default */ | |
3207 | } else if (!strcmp(buf, "0.10")) { | |
3208 | ret = 2; | |
3209 | } else if (!strcmp(buf, "1.1")) { | |
3210 | ret = 3; | |
3211 | } else { | |
3212 | error_setg(errp, "Invalid compatibility level: '%s'", buf); | |
3213 | ret = -EINVAL; | |
3214 | } | |
3215 | g_free(buf); | |
3216 | return ret; | |
3217 | } | |
3218 | ||
3219 | static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version, | |
3220 | Error **errp) | |
3221 | { | |
3222 | uint64_t refcount_bits; | |
3223 | ||
3224 | refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16); | |
3225 | if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) { | |
3226 | error_setg(errp, "Refcount width must be a power of two and may not " | |
3227 | "exceed 64 bits"); | |
3228 | return 0; | |
3229 | } | |
3230 | ||
3231 | if (version < 3 && refcount_bits != 16) { | |
3232 | error_setg(errp, "Different refcount widths than 16 bits require " | |
3233 | "compatibility level 1.1 or above (use compat=1.1 or " | |
3234 | "greater)"); | |
3235 | return 0; | |
a9420734 KW |
3236 | } |
3237 | ||
0eb4a8c1 SH |
3238 | return refcount_bits; |
3239 | } | |
3240 | ||
c274393a | 3241 | static int coroutine_fn |
60900b7b | 3242 | qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) |
0eb4a8c1 | 3243 | { |
29ca9e45 | 3244 | BlockdevCreateOptionsQcow2 *qcow2_opts; |
0eb4a8c1 SH |
3245 | QDict *options; |
3246 | ||
a9420734 KW |
3247 | /* |
3248 | * Open the image file and write a minimal qcow2 header. | |
3249 | * | |
3250 | * We keep things simple and start with a zero-sized image. We also | |
3251 | * do without refcount blocks or a L1 table for now. We'll fix the | |
3252 | * inconsistency later. | |
3253 | * | |
3254 | * We do need a refcount table because growing the refcount table means | |
3255 | * allocating two new refcount blocks - the seconds of which would be at | |
3256 | * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file | |
3257 | * size for any qcow2 image. | |
3258 | */ | |
e1d74bc6 KW |
3259 | BlockBackend *blk = NULL; |
3260 | BlockDriverState *bs = NULL; | |
dcc98687 | 3261 | BlockDriverState *data_bs = NULL; |
f8413b3c | 3262 | QCowHeader *header; |
29ca9e45 KW |
3263 | size_t cluster_size; |
3264 | int version; | |
3265 | int refcount_order; | |
b106ad91 | 3266 | uint64_t* refcount_table; |
3ef6c40a | 3267 | Error *local_err = NULL; |
a9420734 KW |
3268 | int ret; |
3269 | ||
29ca9e45 KW |
3270 | assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2); |
3271 | qcow2_opts = &create_options->u.qcow2; | |
3272 | ||
e1d74bc6 KW |
3273 | bs = bdrv_open_blockdev_ref(qcow2_opts->file, errp); |
3274 | if (bs == NULL) { | |
3275 | return -EIO; | |
3276 | } | |
3277 | ||
3278 | /* Validate options and set default values */ | |
29ca9e45 | 3279 | if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) { |
3afea402 AG |
3280 | error_setg(errp, "Image size must be a multiple of %u bytes", |
3281 | (unsigned) BDRV_SECTOR_SIZE); | |
29ca9e45 KW |
3282 | ret = -EINVAL; |
3283 | goto out; | |
3284 | } | |
3285 | ||
3286 | if (qcow2_opts->has_version) { | |
3287 | switch (qcow2_opts->version) { | |
3288 | case BLOCKDEV_QCOW2_VERSION_V2: | |
3289 | version = 2; | |
3290 | break; | |
3291 | case BLOCKDEV_QCOW2_VERSION_V3: | |
3292 | version = 3; | |
3293 | break; | |
3294 | default: | |
3295 | g_assert_not_reached(); | |
3296 | } | |
3297 | } else { | |
3298 | version = 3; | |
3299 | } | |
3300 | ||
3301 | if (qcow2_opts->has_cluster_size) { | |
3302 | cluster_size = qcow2_opts->cluster_size; | |
3303 | } else { | |
3304 | cluster_size = DEFAULT_CLUSTER_SIZE; | |
3305 | } | |
3306 | ||
3307 | if (!validate_cluster_size(cluster_size, errp)) { | |
e1d74bc6 KW |
3308 | ret = -EINVAL; |
3309 | goto out; | |
29ca9e45 KW |
3310 | } |
3311 | ||
3312 | if (!qcow2_opts->has_preallocation) { | |
3313 | qcow2_opts->preallocation = PREALLOC_MODE_OFF; | |
3314 | } | |
3315 | if (qcow2_opts->has_backing_file && | |
3316 | qcow2_opts->preallocation != PREALLOC_MODE_OFF) | |
3317 | { | |
3318 | error_setg(errp, "Backing file and preallocation cannot be used at " | |
3319 | "the same time"); | |
e1d74bc6 KW |
3320 | ret = -EINVAL; |
3321 | goto out; | |
29ca9e45 KW |
3322 | } |
3323 | if (qcow2_opts->has_backing_fmt && !qcow2_opts->has_backing_file) { | |
3324 | error_setg(errp, "Backing format cannot be used without backing file"); | |
e1d74bc6 KW |
3325 | ret = -EINVAL; |
3326 | goto out; | |
29ca9e45 KW |
3327 | } |
3328 | ||
3329 | if (!qcow2_opts->has_lazy_refcounts) { | |
3330 | qcow2_opts->lazy_refcounts = false; | |
3331 | } | |
3332 | if (version < 3 && qcow2_opts->lazy_refcounts) { | |
3333 | error_setg(errp, "Lazy refcounts only supported with compatibility " | |
b76b4f60 | 3334 | "level 1.1 and above (use version=v3 or greater)"); |
e1d74bc6 KW |
3335 | ret = -EINVAL; |
3336 | goto out; | |
29ca9e45 KW |
3337 | } |
3338 | ||
3339 | if (!qcow2_opts->has_refcount_bits) { | |
3340 | qcow2_opts->refcount_bits = 16; | |
3341 | } | |
3342 | if (qcow2_opts->refcount_bits > 64 || | |
3343 | !is_power_of_2(qcow2_opts->refcount_bits)) | |
3344 | { | |
3345 | error_setg(errp, "Refcount width must be a power of two and may not " | |
3346 | "exceed 64 bits"); | |
e1d74bc6 KW |
3347 | ret = -EINVAL; |
3348 | goto out; | |
29ca9e45 KW |
3349 | } |
3350 | if (version < 3 && qcow2_opts->refcount_bits != 16) { | |
3351 | error_setg(errp, "Different refcount widths than 16 bits require " | |
b76b4f60 | 3352 | "compatibility level 1.1 or above (use version=v3 or " |
29ca9e45 | 3353 | "greater)"); |
e1d74bc6 KW |
3354 | ret = -EINVAL; |
3355 | goto out; | |
29ca9e45 KW |
3356 | } |
3357 | refcount_order = ctz32(qcow2_opts->refcount_bits); | |
3358 | ||
6c3944dc KW |
3359 | if (qcow2_opts->data_file_raw && !qcow2_opts->data_file) { |
3360 | error_setg(errp, "data-file-raw requires data-file"); | |
3361 | ret = -EINVAL; | |
3362 | goto out; | |
3363 | } | |
3364 | if (qcow2_opts->data_file_raw && qcow2_opts->has_backing_file) { | |
3365 | error_setg(errp, "Backing file and data-file-raw cannot be used at " | |
3366 | "the same time"); | |
3367 | ret = -EINVAL; | |
3368 | goto out; | |
3369 | } | |
3370 | ||
dcc98687 KW |
3371 | if (qcow2_opts->data_file) { |
3372 | if (version < 3) { | |
3373 | error_setg(errp, "External data files are only supported with " | |
3374 | "compatibility level 1.1 and above (use version=v3 or " | |
3375 | "greater)"); | |
3376 | ret = -EINVAL; | |
3377 | goto out; | |
3378 | } | |
3379 | data_bs = bdrv_open_blockdev_ref(qcow2_opts->data_file, errp); | |
a0cf8363 | 3380 | if (data_bs == NULL) { |
dcc98687 KW |
3381 | ret = -EIO; |
3382 | goto out; | |
3383 | } | |
3384 | } | |
29ca9e45 KW |
3385 | |
3386 | /* Create BlockBackend to write to the image */ | |
d861ab3a KW |
3387 | blk = blk_new(bdrv_get_aio_context(bs), |
3388 | BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); | |
cbf2b7c4 | 3389 | ret = blk_insert_bs(blk, bs, errp); |
a9420734 | 3390 | if (ret < 0) { |
cbf2b7c4 | 3391 | goto out; |
a9420734 | 3392 | } |
23588797 KW |
3393 | blk_set_allow_write_beyond_eof(blk, true); |
3394 | ||
a9420734 | 3395 | /* Write the header */ |
f8413b3c KW |
3396 | QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header)); |
3397 | header = g_malloc0(cluster_size); | |
3398 | *header = (QCowHeader) { | |
3399 | .magic = cpu_to_be32(QCOW_MAGIC), | |
3400 | .version = cpu_to_be32(version), | |
0eb4a8c1 | 3401 | .cluster_bits = cpu_to_be32(ctz32(cluster_size)), |
f8413b3c KW |
3402 | .size = cpu_to_be64(0), |
3403 | .l1_table_offset = cpu_to_be64(0), | |
3404 | .l1_size = cpu_to_be32(0), | |
3405 | .refcount_table_offset = cpu_to_be64(cluster_size), | |
3406 | .refcount_table_clusters = cpu_to_be32(1), | |
bd4b167f | 3407 | .refcount_order = cpu_to_be32(refcount_order), |
f8413b3c KW |
3408 | .header_length = cpu_to_be32(sizeof(*header)), |
3409 | }; | |
a9420734 | 3410 | |
b25b387f DB |
3411 | /* We'll update this to correct value later */ |
3412 | header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
a9420734 | 3413 | |
29ca9e45 | 3414 | if (qcow2_opts->lazy_refcounts) { |
f8413b3c | 3415 | header->compatible_features |= |
bfe8043e SH |
3416 | cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); |
3417 | } | |
dcc98687 KW |
3418 | if (data_bs) { |
3419 | header->incompatible_features |= | |
3420 | cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE); | |
3421 | } | |
6c3944dc KW |
3422 | if (qcow2_opts->data_file_raw) { |
3423 | header->autoclear_features |= | |
3424 | cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW); | |
3425 | } | |
bfe8043e | 3426 | |
8341f00d | 3427 | ret = blk_pwrite(blk, 0, header, cluster_size, 0); |
f8413b3c | 3428 | g_free(header); |
a9420734 | 3429 | if (ret < 0) { |
3ef6c40a | 3430 | error_setg_errno(errp, -ret, "Could not write qcow2 header"); |
a9420734 KW |
3431 | goto out; |
3432 | } | |
3433 | ||
b106ad91 KW |
3434 | /* Write a refcount table with one refcount block */ |
3435 | refcount_table = g_malloc0(2 * cluster_size); | |
3436 | refcount_table[0] = cpu_to_be64(2 * cluster_size); | |
8341f00d | 3437 | ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size, 0); |
7267c094 | 3438 | g_free(refcount_table); |
a9420734 KW |
3439 | |
3440 | if (ret < 0) { | |
3ef6c40a | 3441 | error_setg_errno(errp, -ret, "Could not write refcount table"); |
a9420734 KW |
3442 | goto out; |
3443 | } | |
3444 | ||
23588797 KW |
3445 | blk_unref(blk); |
3446 | blk = NULL; | |
a9420734 KW |
3447 | |
3448 | /* | |
3449 | * And now open the image and make it consistent first (i.e. increase the | |
3450 | * refcount of the cluster that is occupied by the header and the refcount | |
3451 | * table) | |
3452 | */ | |
e6641719 | 3453 | options = qdict_new(); |
46f5ac20 | 3454 | qdict_put_str(options, "driver", "qcow2"); |
cbf2b7c4 | 3455 | qdict_put_str(options, "file", bs->node_name); |
dcc98687 KW |
3456 | if (data_bs) { |
3457 | qdict_put_str(options, "data-file", data_bs->node_name); | |
3458 | } | |
cbf2b7c4 | 3459 | blk = blk_new_open(NULL, NULL, options, |
55880601 KW |
3460 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH, |
3461 | &local_err); | |
23588797 | 3462 | if (blk == NULL) { |
3ef6c40a | 3463 | error_propagate(errp, local_err); |
23588797 | 3464 | ret = -EIO; |
a9420734 KW |
3465 | goto out; |
3466 | } | |
3467 | ||
23588797 | 3468 | ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size); |
a9420734 | 3469 | if (ret < 0) { |
3ef6c40a HR |
3470 | error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " |
3471 | "header and refcount table"); | |
a9420734 KW |
3472 | goto out; |
3473 | ||
3474 | } else if (ret != 0) { | |
3475 | error_report("Huh, first cluster in empty image is already in use?"); | |
3476 | abort(); | |
3477 | } | |
3478 | ||
9b890bdc KW |
3479 | /* Set the external data file if necessary */ |
3480 | if (data_bs) { | |
3481 | BDRVQcow2State *s = blk_bs(blk)->opaque; | |
3482 | s->image_data_file = g_strdup(data_bs->filename); | |
3483 | } | |
3484 | ||
b527c9b3 | 3485 | /* Create a full header (including things like feature table) */ |
23588797 | 3486 | ret = qcow2_update_header(blk_bs(blk)); |
b527c9b3 KW |
3487 | if (ret < 0) { |
3488 | error_setg_errno(errp, -ret, "Could not update qcow2 header"); | |
3489 | goto out; | |
3490 | } | |
3491 | ||
a9420734 | 3492 | /* Okay, now that we have a valid image, let's give it the right size */ |
c80d8b06 HR |
3493 | ret = blk_truncate(blk, qcow2_opts->size, false, qcow2_opts->preallocation, |
3494 | errp); | |
a9420734 | 3495 | if (ret < 0) { |
ed3d2ec9 | 3496 | error_prepend(errp, "Could not resize image: "); |
a9420734 KW |
3497 | goto out; |
3498 | } | |
3499 | ||
3500 | /* Want a backing file? There you go.*/ | |
29ca9e45 KW |
3501 | if (qcow2_opts->has_backing_file) { |
3502 | const char *backing_format = NULL; | |
3503 | ||
3504 | if (qcow2_opts->has_backing_fmt) { | |
3505 | backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt); | |
3506 | } | |
3507 | ||
3508 | ret = bdrv_change_backing_file(blk_bs(blk), qcow2_opts->backing_file, | |
3509 | backing_format); | |
a9420734 | 3510 | if (ret < 0) { |
3ef6c40a | 3511 | error_setg_errno(errp, -ret, "Could not assign backing file '%s' " |
29ca9e45 KW |
3512 | "with format '%s'", qcow2_opts->backing_file, |
3513 | backing_format); | |
a9420734 KW |
3514 | goto out; |
3515 | } | |
3516 | } | |
3517 | ||
b25b387f | 3518 | /* Want encryption? There you go. */ |
60900b7b KW |
3519 | if (qcow2_opts->has_encrypt) { |
3520 | ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp); | |
b25b387f DB |
3521 | if (ret < 0) { |
3522 | goto out; | |
3523 | } | |
3524 | } | |
3525 | ||
23588797 KW |
3526 | blk_unref(blk); |
3527 | blk = NULL; | |
ba2ab2f2 | 3528 | |
b25b387f DB |
3529 | /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning. |
3530 | * Using BDRV_O_NO_IO, since encryption is now setup we don't want to | |
3531 | * have to setup decryption context. We're not doing any I/O on the top | |
3532 | * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does | |
3533 | * not have effect. | |
3534 | */ | |
e6641719 | 3535 | options = qdict_new(); |
46f5ac20 | 3536 | qdict_put_str(options, "driver", "qcow2"); |
cbf2b7c4 | 3537 | qdict_put_str(options, "file", bs->node_name); |
dcc98687 KW |
3538 | if (data_bs) { |
3539 | qdict_put_str(options, "data-file", data_bs->node_name); | |
3540 | } | |
cbf2b7c4 | 3541 | blk = blk_new_open(NULL, NULL, options, |
b25b387f DB |
3542 | BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO, |
3543 | &local_err); | |
23588797 | 3544 | if (blk == NULL) { |
ba2ab2f2 | 3545 | error_propagate(errp, local_err); |
23588797 | 3546 | ret = -EIO; |
ba2ab2f2 HR |
3547 | goto out; |
3548 | } | |
3549 | ||
a9420734 KW |
3550 | ret = 0; |
3551 | out: | |
e1d74bc6 KW |
3552 | blk_unref(blk); |
3553 | bdrv_unref(bs); | |
dcc98687 | 3554 | bdrv_unref(data_bs); |
a9420734 KW |
3555 | return ret; |
3556 | } | |
de5f3f40 | 3557 | |
efc75e2a SH |
3558 | static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opts, |
3559 | Error **errp) | |
de5f3f40 | 3560 | { |
b76b4f60 | 3561 | BlockdevCreateOptions *create_options = NULL; |
92adf9db | 3562 | QDict *qdict; |
b76b4f60 | 3563 | Visitor *v; |
cbf2b7c4 | 3564 | BlockDriverState *bs = NULL; |
9b890bdc | 3565 | BlockDriverState *data_bs = NULL; |
3ef6c40a | 3566 | Error *local_err = NULL; |
b76b4f60 | 3567 | const char *val; |
3ef6c40a | 3568 | int ret; |
de5f3f40 | 3569 | |
b76b4f60 KW |
3570 | /* Only the keyval visitor supports the dotted syntax needed for |
3571 | * encryption, so go through a QDict before getting a QAPI type. Ignore | |
3572 | * options meant for the protocol layer so that the visitor doesn't | |
3573 | * complain. */ | |
3574 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts, | |
3575 | true); | |
3576 | ||
3577 | /* Handle encryption options */ | |
3578 | val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT); | |
3579 | if (val && !strcmp(val, "on")) { | |
3580 | qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow"); | |
3581 | } else if (val && !strcmp(val, "off")) { | |
3582 | qdict_del(qdict, BLOCK_OPT_ENCRYPT); | |
3583 | } | |
3584 | ||
3585 | val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT); | |
3586 | if (val && !strcmp(val, "aes")) { | |
3587 | qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow"); | |
3588 | } | |
3589 | ||
3590 | /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into | |
3591 | * version=v2/v3 below. */ | |
3592 | val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL); | |
3593 | if (val && !strcmp(val, "0.10")) { | |
3594 | qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2"); | |
3595 | } else if (val && !strcmp(val, "1.1")) { | |
3596 | qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3"); | |
3597 | } | |
3598 | ||
3599 | /* Change legacy command line options into QMP ones */ | |
3600 | static const QDictRenames opt_renames[] = { | |
3601 | { BLOCK_OPT_BACKING_FILE, "backing-file" }, | |
3602 | { BLOCK_OPT_BACKING_FMT, "backing-fmt" }, | |
3603 | { BLOCK_OPT_CLUSTER_SIZE, "cluster-size" }, | |
3604 | { BLOCK_OPT_LAZY_REFCOUNTS, "lazy-refcounts" }, | |
3605 | { BLOCK_OPT_REFCOUNT_BITS, "refcount-bits" }, | |
3606 | { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT }, | |
3607 | { BLOCK_OPT_COMPAT_LEVEL, "version" }, | |
6c3944dc | 3608 | { BLOCK_OPT_DATA_FILE_RAW, "data-file-raw" }, |
b76b4f60 KW |
3609 | { NULL, NULL }, |
3610 | }; | |
3611 | ||
3612 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { | |
29ca9e45 KW |
3613 | ret = -EINVAL; |
3614 | goto finish; | |
3615 | } | |
60900b7b | 3616 | |
b76b4f60 KW |
3617 | /* Create and open the file (protocol layer) */ |
3618 | ret = bdrv_create_file(filename, opts, errp); | |
3619 | if (ret < 0) { | |
0eb4a8c1 SH |
3620 | goto finish; |
3621 | } | |
b76b4f60 KW |
3622 | |
3623 | bs = bdrv_open(filename, NULL, NULL, | |
3624 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
3625 | if (bs == NULL) { | |
3626 | ret = -EIO; | |
1bd0e2d1 CL |
3627 | goto finish; |
3628 | } | |
0eb4a8c1 | 3629 | |
9b890bdc KW |
3630 | /* Create and open an external data file (protocol layer) */ |
3631 | val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE); | |
3632 | if (val) { | |
3633 | ret = bdrv_create_file(val, opts, errp); | |
3634 | if (ret < 0) { | |
3635 | goto finish; | |
3636 | } | |
3637 | ||
3638 | data_bs = bdrv_open(val, NULL, NULL, | |
3639 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, | |
3640 | errp); | |
3641 | if (data_bs == NULL) { | |
3642 | ret = -EIO; | |
3643 | goto finish; | |
3644 | } | |
3645 | ||
3646 | qdict_del(qdict, BLOCK_OPT_DATA_FILE); | |
3647 | qdict_put_str(qdict, "data-file", data_bs->node_name); | |
3648 | } | |
3649 | ||
b76b4f60 KW |
3650 | /* Set 'driver' and 'node' options */ |
3651 | qdict_put_str(qdict, "driver", "qcow2"); | |
3652 | qdict_put_str(qdict, "file", bs->node_name); | |
3653 | ||
3654 | /* Now get the QAPI type BlockdevCreateOptions */ | |
af91062e MA |
3655 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
3656 | if (!v) { | |
1bd0e2d1 CL |
3657 | ret = -EINVAL; |
3658 | goto finish; | |
3659 | } | |
3660 | ||
b76b4f60 KW |
3661 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); |
3662 | visit_free(v); | |
de5f3f40 | 3663 | |
0eb4a8c1 SH |
3664 | if (local_err) { |
3665 | error_propagate(errp, local_err); | |
bd4b167f HR |
3666 | ret = -EINVAL; |
3667 | goto finish; | |
3668 | } | |
3669 | ||
b76b4f60 KW |
3670 | /* Silently round up size */ |
3671 | create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size, | |
3672 | BDRV_SECTOR_SIZE); | |
cbf2b7c4 KW |
3673 | |
3674 | /* Create the qcow2 image (format layer) */ | |
b76b4f60 | 3675 | ret = qcow2_co_create(create_options, errp); |
cbf2b7c4 KW |
3676 | if (ret < 0) { |
3677 | goto finish; | |
3678 | } | |
1bd0e2d1 | 3679 | |
b76b4f60 | 3680 | ret = 0; |
1bd0e2d1 | 3681 | finish: |
cb3e7f08 | 3682 | qobject_unref(qdict); |
cbf2b7c4 | 3683 | bdrv_unref(bs); |
9b890bdc | 3684 | bdrv_unref(data_bs); |
b76b4f60 | 3685 | qapi_free_BlockdevCreateOptions(create_options); |
3ef6c40a | 3686 | return ret; |
de5f3f40 KW |
3687 | } |
3688 | ||
2928abce | 3689 | |
f06f6b66 | 3690 | static bool is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes) |
2928abce | 3691 | { |
31826642 EB |
3692 | int64_t nr; |
3693 | int res; | |
f06f6b66 EB |
3694 | |
3695 | /* Clamp to image length, before checking status of underlying sectors */ | |
8cbf74b2 EB |
3696 | if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) { |
3697 | bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset; | |
fbaa6bb3 EB |
3698 | } |
3699 | ||
f06f6b66 | 3700 | if (!bytes) { |
ebb718a5 EB |
3701 | return true; |
3702 | } | |
8cbf74b2 | 3703 | res = bdrv_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL); |
31826642 | 3704 | return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == bytes; |
2928abce DL |
3705 | } |
3706 | ||
5544b59f | 3707 | static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 3708 | int64_t offset, int bytes, BdrvRequestFlags flags) |
621f0589 KW |
3709 | { |
3710 | int ret; | |
ff99129a | 3711 | BDRVQcow2State *s = bs->opaque; |
621f0589 | 3712 | |
5544b59f | 3713 | uint32_t head = offset % s->cluster_size; |
f5a5ca79 | 3714 | uint32_t tail = (offset + bytes) % s->cluster_size; |
2928abce | 3715 | |
f5a5ca79 MP |
3716 | trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes); |
3717 | if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) { | |
fbaa6bb3 EB |
3718 | tail = 0; |
3719 | } | |
5a64e942 | 3720 | |
ebb718a5 | 3721 | if (head || tail) { |
ebb718a5 | 3722 | uint64_t off; |
ecfe1863 | 3723 | unsigned int nr; |
2928abce | 3724 | |
f5a5ca79 | 3725 | assert(head + bytes <= s->cluster_size); |
2928abce | 3726 | |
ebb718a5 | 3727 | /* check whether remainder of cluster already reads as zero */ |
f06f6b66 EB |
3728 | if (!(is_zero(bs, offset - head, head) && |
3729 | is_zero(bs, offset + bytes, | |
3730 | tail ? s->cluster_size - tail : 0))) { | |
2928abce DL |
3731 | return -ENOTSUP; |
3732 | } | |
3733 | ||
2928abce DL |
3734 | qemu_co_mutex_lock(&s->lock); |
3735 | /* We can have new write after previous check */ | |
f06f6b66 | 3736 | offset = QEMU_ALIGN_DOWN(offset, s->cluster_size); |
f5a5ca79 | 3737 | bytes = s->cluster_size; |
ecfe1863 | 3738 | nr = s->cluster_size; |
5544b59f | 3739 | ret = qcow2_get_cluster_offset(bs, offset, &nr, &off); |
fdfab37d EB |
3740 | if (ret != QCOW2_CLUSTER_UNALLOCATED && |
3741 | ret != QCOW2_CLUSTER_ZERO_PLAIN && | |
3742 | ret != QCOW2_CLUSTER_ZERO_ALLOC) { | |
2928abce DL |
3743 | qemu_co_mutex_unlock(&s->lock); |
3744 | return -ENOTSUP; | |
3745 | } | |
3746 | } else { | |
3747 | qemu_co_mutex_lock(&s->lock); | |
621f0589 KW |
3748 | } |
3749 | ||
f5a5ca79 | 3750 | trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes); |
5a64e942 | 3751 | |
621f0589 | 3752 | /* Whatever is left can use real zero clusters */ |
f5a5ca79 | 3753 | ret = qcow2_cluster_zeroize(bs, offset, bytes, flags); |
621f0589 KW |
3754 | qemu_co_mutex_unlock(&s->lock); |
3755 | ||
3756 | return ret; | |
3757 | } | |
3758 | ||
82e8a788 | 3759 | static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs, |
f5a5ca79 | 3760 | int64_t offset, int bytes) |
5ea929e3 | 3761 | { |
6db39ae2 | 3762 | int ret; |
ff99129a | 3763 | BDRVQcow2State *s = bs->opaque; |
6db39ae2 | 3764 | |
f5a5ca79 MP |
3765 | if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) { |
3766 | assert(bytes < s->cluster_size); | |
048c5fd1 EB |
3767 | /* Ignore partial clusters, except for the special case of the |
3768 | * complete partial cluster at the end of an unaligned file */ | |
3769 | if (!QEMU_IS_ALIGNED(offset, s->cluster_size) || | |
f5a5ca79 | 3770 | offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) { |
048c5fd1 EB |
3771 | return -ENOTSUP; |
3772 | } | |
49228d1e EB |
3773 | } |
3774 | ||
6db39ae2 | 3775 | qemu_co_mutex_lock(&s->lock); |
f5a5ca79 | 3776 | ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST, |
d2cb36af | 3777 | false); |
6db39ae2 PB |
3778 | qemu_co_mutex_unlock(&s->lock); |
3779 | return ret; | |
5ea929e3 KW |
3780 | } |
3781 | ||
fd9fcd37 FZ |
3782 | static int coroutine_fn |
3783 | qcow2_co_copy_range_from(BlockDriverState *bs, | |
3784 | BdrvChild *src, uint64_t src_offset, | |
3785 | BdrvChild *dst, uint64_t dst_offset, | |
67b51fb9 VSO |
3786 | uint64_t bytes, BdrvRequestFlags read_flags, |
3787 | BdrvRequestFlags write_flags) | |
fd9fcd37 FZ |
3788 | { |
3789 | BDRVQcow2State *s = bs->opaque; | |
3790 | int ret; | |
3791 | unsigned int cur_bytes; /* number of bytes in current iteration */ | |
3792 | BdrvChild *child = NULL; | |
67b51fb9 | 3793 | BdrvRequestFlags cur_write_flags; |
fd9fcd37 FZ |
3794 | |
3795 | assert(!bs->encrypted); | |
3796 | qemu_co_mutex_lock(&s->lock); | |
3797 | ||
3798 | while (bytes != 0) { | |
3799 | uint64_t copy_offset = 0; | |
3800 | /* prepare next request */ | |
3801 | cur_bytes = MIN(bytes, INT_MAX); | |
67b51fb9 | 3802 | cur_write_flags = write_flags; |
fd9fcd37 FZ |
3803 | |
3804 | ret = qcow2_get_cluster_offset(bs, src_offset, &cur_bytes, ©_offset); | |
3805 | if (ret < 0) { | |
3806 | goto out; | |
3807 | } | |
3808 | ||
3809 | switch (ret) { | |
3810 | case QCOW2_CLUSTER_UNALLOCATED: | |
3811 | if (bs->backing && bs->backing->bs) { | |
3812 | int64_t backing_length = bdrv_getlength(bs->backing->bs); | |
3813 | if (src_offset >= backing_length) { | |
67b51fb9 | 3814 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
fd9fcd37 FZ |
3815 | } else { |
3816 | child = bs->backing; | |
3817 | cur_bytes = MIN(cur_bytes, backing_length - src_offset); | |
3818 | copy_offset = src_offset; | |
3819 | } | |
3820 | } else { | |
67b51fb9 | 3821 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
fd9fcd37 FZ |
3822 | } |
3823 | break; | |
3824 | ||
3825 | case QCOW2_CLUSTER_ZERO_PLAIN: | |
3826 | case QCOW2_CLUSTER_ZERO_ALLOC: | |
67b51fb9 | 3827 | cur_write_flags |= BDRV_REQ_ZERO_WRITE; |
fd9fcd37 FZ |
3828 | break; |
3829 | ||
3830 | case QCOW2_CLUSTER_COMPRESSED: | |
3831 | ret = -ENOTSUP; | |
3832 | goto out; | |
fd9fcd37 FZ |
3833 | |
3834 | case QCOW2_CLUSTER_NORMAL: | |
966b000f | 3835 | child = s->data_file; |
fd9fcd37 | 3836 | copy_offset += offset_into_cluster(s, src_offset); |
fd9fcd37 FZ |
3837 | break; |
3838 | ||
3839 | default: | |
3840 | abort(); | |
3841 | } | |
3842 | qemu_co_mutex_unlock(&s->lock); | |
3843 | ret = bdrv_co_copy_range_from(child, | |
3844 | copy_offset, | |
3845 | dst, dst_offset, | |
67b51fb9 | 3846 | cur_bytes, read_flags, cur_write_flags); |
fd9fcd37 FZ |
3847 | qemu_co_mutex_lock(&s->lock); |
3848 | if (ret < 0) { | |
3849 | goto out; | |
3850 | } | |
3851 | ||
3852 | bytes -= cur_bytes; | |
3853 | src_offset += cur_bytes; | |
3854 | dst_offset += cur_bytes; | |
3855 | } | |
3856 | ret = 0; | |
3857 | ||
3858 | out: | |
3859 | qemu_co_mutex_unlock(&s->lock); | |
3860 | return ret; | |
3861 | } | |
3862 | ||
3863 | static int coroutine_fn | |
3864 | qcow2_co_copy_range_to(BlockDriverState *bs, | |
3865 | BdrvChild *src, uint64_t src_offset, | |
3866 | BdrvChild *dst, uint64_t dst_offset, | |
67b51fb9 VSO |
3867 | uint64_t bytes, BdrvRequestFlags read_flags, |
3868 | BdrvRequestFlags write_flags) | |
fd9fcd37 FZ |
3869 | { |
3870 | BDRVQcow2State *s = bs->opaque; | |
3871 | int offset_in_cluster; | |
3872 | int ret; | |
3873 | unsigned int cur_bytes; /* number of sectors in current iteration */ | |
3874 | uint64_t cluster_offset; | |
fd9fcd37 FZ |
3875 | QCowL2Meta *l2meta = NULL; |
3876 | ||
3877 | assert(!bs->encrypted); | |
fd9fcd37 FZ |
3878 | |
3879 | qemu_co_mutex_lock(&s->lock); | |
3880 | ||
3881 | while (bytes != 0) { | |
3882 | ||
3883 | l2meta = NULL; | |
3884 | ||
3885 | offset_in_cluster = offset_into_cluster(s, dst_offset); | |
3886 | cur_bytes = MIN(bytes, INT_MAX); | |
3887 | ||
3888 | /* TODO: | |
3889 | * If src->bs == dst->bs, we could simply copy by incrementing | |
3890 | * the refcnt, without copying user data. | |
3891 | * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */ | |
3892 | ret = qcow2_alloc_cluster_offset(bs, dst_offset, &cur_bytes, | |
3893 | &cluster_offset, &l2meta); | |
3894 | if (ret < 0) { | |
3895 | goto fail; | |
3896 | } | |
3897 | ||
344ffea9 | 3898 | assert(offset_into_cluster(s, cluster_offset) == 0); |
fd9fcd37 FZ |
3899 | |
3900 | ret = qcow2_pre_write_overlap_check(bs, 0, | |
966b000f | 3901 | cluster_offset + offset_in_cluster, cur_bytes, true); |
fd9fcd37 FZ |
3902 | if (ret < 0) { |
3903 | goto fail; | |
3904 | } | |
3905 | ||
3906 | qemu_co_mutex_unlock(&s->lock); | |
3907 | ret = bdrv_co_copy_range_to(src, src_offset, | |
966b000f | 3908 | s->data_file, |
fd9fcd37 | 3909 | cluster_offset + offset_in_cluster, |
67b51fb9 | 3910 | cur_bytes, read_flags, write_flags); |
fd9fcd37 FZ |
3911 | qemu_co_mutex_lock(&s->lock); |
3912 | if (ret < 0) { | |
3913 | goto fail; | |
3914 | } | |
3915 | ||
3916 | ret = qcow2_handle_l2meta(bs, &l2meta, true); | |
3917 | if (ret) { | |
3918 | goto fail; | |
3919 | } | |
3920 | ||
3921 | bytes -= cur_bytes; | |
e06f4639 | 3922 | src_offset += cur_bytes; |
fd9fcd37 FZ |
3923 | dst_offset += cur_bytes; |
3924 | } | |
3925 | ret = 0; | |
3926 | ||
3927 | fail: | |
3928 | qcow2_handle_l2meta(bs, &l2meta, false); | |
3929 | ||
3930 | qemu_co_mutex_unlock(&s->lock); | |
3931 | ||
fd9fcd37 FZ |
3932 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
3933 | ||
3934 | return ret; | |
3935 | } | |
3936 | ||
061ca8a3 | 3937 | static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset, |
c80d8b06 HR |
3938 | bool exact, PreallocMode prealloc, |
3939 | Error **errp) | |
419b19d9 | 3940 | { |
ff99129a | 3941 | BDRVQcow2State *s = bs->opaque; |
95b98f34 | 3942 | uint64_t old_length; |
2cf7cfa1 KW |
3943 | int64_t new_l1_size; |
3944 | int ret; | |
45b4949c | 3945 | QDict *options; |
419b19d9 | 3946 | |
772d1f97 HR |
3947 | if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA && |
3948 | prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL) | |
3949 | { | |
8243ccb7 | 3950 | error_setg(errp, "Unsupported preallocation mode '%s'", |
977c736f | 3951 | PreallocMode_str(prealloc)); |
8243ccb7 HR |
3952 | return -ENOTSUP; |
3953 | } | |
3954 | ||
3afea402 AG |
3955 | if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) { |
3956 | error_setg(errp, "The new size must be a multiple of %u", | |
3957 | (unsigned) BDRV_SECTOR_SIZE); | |
419b19d9 SH |
3958 | return -EINVAL; |
3959 | } | |
3960 | ||
061ca8a3 KW |
3961 | qemu_co_mutex_lock(&s->lock); |
3962 | ||
419b19d9 SH |
3963 | /* cannot proceed if image has snapshots */ |
3964 | if (s->nb_snapshots) { | |
4bff28b8 | 3965 | error_setg(errp, "Can't resize an image which has snapshots"); |
061ca8a3 KW |
3966 | ret = -ENOTSUP; |
3967 | goto fail; | |
419b19d9 SH |
3968 | } |
3969 | ||
88ddffae | 3970 | /* cannot proceed if image has bitmaps */ |
d19c6b36 | 3971 | if (qcow2_truncate_bitmaps_check(bs, errp)) { |
061ca8a3 KW |
3972 | ret = -ENOTSUP; |
3973 | goto fail; | |
88ddffae VSO |
3974 | } |
3975 | ||
bd016b91 | 3976 | old_length = bs->total_sectors * BDRV_SECTOR_SIZE; |
46b732cd | 3977 | new_l1_size = size_to_l1(s, offset); |
95b98f34 | 3978 | |
95b98f34 | 3979 | if (offset < old_length) { |
163bc39d | 3980 | int64_t last_cluster, old_file_size; |
46b732cd PB |
3981 | if (prealloc != PREALLOC_MODE_OFF) { |
3982 | error_setg(errp, | |
3983 | "Preallocation can't be used for shrinking an image"); | |
061ca8a3 KW |
3984 | ret = -EINVAL; |
3985 | goto fail; | |
46b732cd | 3986 | } |
419b19d9 | 3987 | |
46b732cd PB |
3988 | ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size), |
3989 | old_length - ROUND_UP(offset, | |
3990 | s->cluster_size), | |
3991 | QCOW2_DISCARD_ALWAYS, true); | |
3992 | if (ret < 0) { | |
3993 | error_setg_errno(errp, -ret, "Failed to discard cropped clusters"); | |
061ca8a3 | 3994 | goto fail; |
46b732cd PB |
3995 | } |
3996 | ||
3997 | ret = qcow2_shrink_l1_table(bs, new_l1_size); | |
3998 | if (ret < 0) { | |
3999 | error_setg_errno(errp, -ret, | |
4000 | "Failed to reduce the number of L2 tables"); | |
061ca8a3 | 4001 | goto fail; |
46b732cd PB |
4002 | } |
4003 | ||
4004 | ret = qcow2_shrink_reftable(bs); | |
4005 | if (ret < 0) { | |
4006 | error_setg_errno(errp, -ret, | |
4007 | "Failed to discard unused refblocks"); | |
061ca8a3 | 4008 | goto fail; |
46b732cd | 4009 | } |
163bc39d PB |
4010 | |
4011 | old_file_size = bdrv_getlength(bs->file->bs); | |
4012 | if (old_file_size < 0) { | |
4013 | error_setg_errno(errp, -old_file_size, | |
4014 | "Failed to inquire current file length"); | |
061ca8a3 KW |
4015 | ret = old_file_size; |
4016 | goto fail; | |
163bc39d PB |
4017 | } |
4018 | last_cluster = qcow2_get_last_cluster(bs, old_file_size); | |
4019 | if (last_cluster < 0) { | |
4020 | error_setg_errno(errp, -last_cluster, | |
4021 | "Failed to find the last cluster"); | |
061ca8a3 KW |
4022 | ret = last_cluster; |
4023 | goto fail; | |
163bc39d PB |
4024 | } |
4025 | if ((last_cluster + 1) * s->cluster_size < old_file_size) { | |
233521b1 HR |
4026 | Error *local_err = NULL; |
4027 | ||
e61a28a9 HR |
4028 | /* |
4029 | * Do not pass @exact here: It will not help the user if | |
4030 | * we get an error here just because they wanted to shrink | |
4031 | * their qcow2 image (on a block device) with qemu-img. | |
4032 | * (And on the qcow2 layer, the @exact requirement is | |
4033 | * always fulfilled, so there is no need to pass it on.) | |
4034 | */ | |
061ca8a3 | 4035 | bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size, |
c80d8b06 | 4036 | false, PREALLOC_MODE_OFF, &local_err); |
233521b1 HR |
4037 | if (local_err) { |
4038 | warn_reportf_err(local_err, | |
4039 | "Failed to truncate the tail of the image: "); | |
163bc39d PB |
4040 | } |
4041 | } | |
46b732cd PB |
4042 | } else { |
4043 | ret = qcow2_grow_l1_table(bs, new_l1_size, true); | |
4044 | if (ret < 0) { | |
4045 | error_setg_errno(errp, -ret, "Failed to grow the L1 table"); | |
061ca8a3 | 4046 | goto fail; |
46b732cd | 4047 | } |
419b19d9 SH |
4048 | } |
4049 | ||
95b98f34 HR |
4050 | switch (prealloc) { |
4051 | case PREALLOC_MODE_OFF: | |
718c0fce | 4052 | if (has_data_file(bs)) { |
e61a28a9 HR |
4053 | /* |
4054 | * If the caller wants an exact resize, the external data | |
4055 | * file should be resized to the exact target size, too, | |
4056 | * so we pass @exact here. | |
4057 | */ | |
4058 | ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, errp); | |
718c0fce KW |
4059 | if (ret < 0) { |
4060 | goto fail; | |
4061 | } | |
4062 | } | |
95b98f34 HR |
4063 | break; |
4064 | ||
4065 | case PREALLOC_MODE_METADATA: | |
718c0fce | 4066 | ret = preallocate_co(bs, old_length, offset, prealloc, errp); |
95b98f34 | 4067 | if (ret < 0) { |
061ca8a3 | 4068 | goto fail; |
95b98f34 HR |
4069 | } |
4070 | break; | |
4071 | ||
772d1f97 HR |
4072 | case PREALLOC_MODE_FALLOC: |
4073 | case PREALLOC_MODE_FULL: | |
4074 | { | |
4075 | int64_t allocation_start, host_offset, guest_offset; | |
4076 | int64_t clusters_allocated; | |
4077 | int64_t old_file_size, new_file_size; | |
4078 | uint64_t nb_new_data_clusters, nb_new_l2_tables; | |
4079 | ||
966b000f KW |
4080 | /* With a data file, preallocation means just allocating the metadata |
4081 | * and forwarding the truncate request to the data file */ | |
4082 | if (has_data_file(bs)) { | |
718c0fce | 4083 | ret = preallocate_co(bs, old_length, offset, prealloc, errp); |
966b000f | 4084 | if (ret < 0) { |
966b000f KW |
4085 | goto fail; |
4086 | } | |
4087 | break; | |
4088 | } | |
4089 | ||
772d1f97 HR |
4090 | old_file_size = bdrv_getlength(bs->file->bs); |
4091 | if (old_file_size < 0) { | |
4092 | error_setg_errno(errp, -old_file_size, | |
4093 | "Failed to inquire current file length"); | |
061ca8a3 KW |
4094 | ret = old_file_size; |
4095 | goto fail; | |
772d1f97 | 4096 | } |
e400ad1e | 4097 | old_file_size = ROUND_UP(old_file_size, s->cluster_size); |
772d1f97 HR |
4098 | |
4099 | nb_new_data_clusters = DIV_ROUND_UP(offset - old_length, | |
4100 | s->cluster_size); | |
4101 | ||
4102 | /* This is an overestimation; we will not actually allocate space for | |
4103 | * these in the file but just make sure the new refcount structures are | |
4104 | * able to cover them so we will not have to allocate new refblocks | |
4105 | * while entering the data blocks in the potentially new L2 tables. | |
4106 | * (We do not actually care where the L2 tables are placed. Maybe they | |
4107 | * are already allocated or they can be placed somewhere before | |
4108 | * @old_file_size. It does not matter because they will be fully | |
4109 | * allocated automatically, so they do not need to be covered by the | |
4110 | * preallocation. All that matters is that we will not have to allocate | |
4111 | * new refcount structures for them.) */ | |
4112 | nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters, | |
4113 | s->cluster_size / sizeof(uint64_t)); | |
4114 | /* The cluster range may not be aligned to L2 boundaries, so add one L2 | |
4115 | * table for a potential head/tail */ | |
4116 | nb_new_l2_tables++; | |
4117 | ||
4118 | allocation_start = qcow2_refcount_area(bs, old_file_size, | |
4119 | nb_new_data_clusters + | |
4120 | nb_new_l2_tables, | |
4121 | true, 0, 0); | |
4122 | if (allocation_start < 0) { | |
4123 | error_setg_errno(errp, -allocation_start, | |
4124 | "Failed to resize refcount structures"); | |
061ca8a3 KW |
4125 | ret = allocation_start; |
4126 | goto fail; | |
772d1f97 HR |
4127 | } |
4128 | ||
4129 | clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start, | |
4130 | nb_new_data_clusters); | |
4131 | if (clusters_allocated < 0) { | |
4132 | error_setg_errno(errp, -clusters_allocated, | |
4133 | "Failed to allocate data clusters"); | |
061ca8a3 KW |
4134 | ret = clusters_allocated; |
4135 | goto fail; | |
772d1f97 HR |
4136 | } |
4137 | ||
4138 | assert(clusters_allocated == nb_new_data_clusters); | |
4139 | ||
4140 | /* Allocate the data area */ | |
4141 | new_file_size = allocation_start + | |
4142 | nb_new_data_clusters * s->cluster_size; | |
e61a28a9 | 4143 | /* Image file grows, so @exact does not matter */ |
c80d8b06 | 4144 | ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, errp); |
772d1f97 HR |
4145 | if (ret < 0) { |
4146 | error_prepend(errp, "Failed to resize underlying file: "); | |
4147 | qcow2_free_clusters(bs, allocation_start, | |
4148 | nb_new_data_clusters * s->cluster_size, | |
4149 | QCOW2_DISCARD_OTHER); | |
061ca8a3 | 4150 | goto fail; |
772d1f97 HR |
4151 | } |
4152 | ||
4153 | /* Create the necessary L2 entries */ | |
4154 | host_offset = allocation_start; | |
4155 | guest_offset = old_length; | |
4156 | while (nb_new_data_clusters) { | |
13bec229 AG |
4157 | int64_t nb_clusters = MIN( |
4158 | nb_new_data_clusters, | |
4159 | s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset)); | |
772d1f97 HR |
4160 | QCowL2Meta allocation = { |
4161 | .offset = guest_offset, | |
4162 | .alloc_offset = host_offset, | |
4163 | .nb_clusters = nb_clusters, | |
4164 | }; | |
4165 | qemu_co_queue_init(&allocation.dependent_requests); | |
4166 | ||
4167 | ret = qcow2_alloc_cluster_link_l2(bs, &allocation); | |
4168 | if (ret < 0) { | |
4169 | error_setg_errno(errp, -ret, "Failed to update L2 tables"); | |
4170 | qcow2_free_clusters(bs, host_offset, | |
4171 | nb_new_data_clusters * s->cluster_size, | |
4172 | QCOW2_DISCARD_OTHER); | |
061ca8a3 | 4173 | goto fail; |
772d1f97 HR |
4174 | } |
4175 | ||
4176 | guest_offset += nb_clusters * s->cluster_size; | |
4177 | host_offset += nb_clusters * s->cluster_size; | |
4178 | nb_new_data_clusters -= nb_clusters; | |
4179 | } | |
4180 | break; | |
4181 | } | |
4182 | ||
95b98f34 HR |
4183 | default: |
4184 | g_assert_not_reached(); | |
4185 | } | |
4186 | ||
4187 | if (prealloc != PREALLOC_MODE_OFF) { | |
4188 | /* Flush metadata before actually changing the image size */ | |
061ca8a3 | 4189 | ret = qcow2_write_caches(bs); |
95b98f34 HR |
4190 | if (ret < 0) { |
4191 | error_setg_errno(errp, -ret, | |
4192 | "Failed to flush the preallocated area to disk"); | |
061ca8a3 | 4193 | goto fail; |
95b98f34 HR |
4194 | } |
4195 | } | |
4196 | ||
45b4949c LB |
4197 | bs->total_sectors = offset / BDRV_SECTOR_SIZE; |
4198 | ||
419b19d9 SH |
4199 | /* write updated header.size */ |
4200 | offset = cpu_to_be64(offset); | |
d9ca2ea2 | 4201 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |
8b3b7206 | 4202 | &offset, sizeof(uint64_t)); |
419b19d9 | 4203 | if (ret < 0) { |
f59adb32 | 4204 | error_setg_errno(errp, -ret, "Failed to update the image size"); |
061ca8a3 | 4205 | goto fail; |
419b19d9 SH |
4206 | } |
4207 | ||
4208 | s->l1_vm_state_index = new_l1_size; | |
45b4949c LB |
4209 | |
4210 | /* Update cache sizes */ | |
4211 | options = qdict_clone_shallow(bs->options); | |
4212 | ret = qcow2_update_options(bs, options, s->flags, errp); | |
4213 | qobject_unref(options); | |
4214 | if (ret < 0) { | |
4215 | goto fail; | |
4216 | } | |
061ca8a3 KW |
4217 | ret = 0; |
4218 | fail: | |
4219 | qemu_co_mutex_unlock(&s->lock); | |
4220 | return ret; | |
419b19d9 SH |
4221 | } |
4222 | ||
fcccefc5 | 4223 | static coroutine_fn int |
0d483dce | 4224 | qcow2_co_pwritev_compressed_task(BlockDriverState *bs, |
5396234b VSO |
4225 | uint64_t offset, uint64_t bytes, |
4226 | QEMUIOVector *qiov, size_t qiov_offset) | |
20d97356 | 4227 | { |
ff99129a | 4228 | BDRVQcow2State *s = bs->opaque; |
2714f13d | 4229 | int ret; |
e1f4a37a | 4230 | ssize_t out_len; |
fcccefc5 | 4231 | uint8_t *buf, *out_buf; |
77e023ff | 4232 | uint64_t cluster_offset; |
20d97356 | 4233 | |
0d483dce AS |
4234 | assert(bytes == s->cluster_size || (bytes < s->cluster_size && |
4235 | (offset + bytes == bs->total_sectors << BDRV_SECTOR_BITS))); | |
3e3b838f | 4236 | |
a2c0ca6f | 4237 | buf = qemu_blockalign(bs, s->cluster_size); |
0d483dce | 4238 | if (bytes < s->cluster_size) { |
a2c0ca6f PB |
4239 | /* Zero-pad last write if image size is not cluster aligned */ |
4240 | memset(buf + bytes, 0, s->cluster_size - bytes); | |
f4d38bef | 4241 | } |
5396234b | 4242 | qemu_iovec_to_buf(qiov, qiov_offset, buf, bytes); |
20d97356 | 4243 | |
ebf7bba0 | 4244 | out_buf = g_malloc(s->cluster_size); |
20d97356 | 4245 | |
6994fd78 VSO |
4246 | out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1, |
4247 | buf, s->cluster_size); | |
e1f4a37a | 4248 | if (out_len == -ENOMEM) { |
20d97356 | 4249 | /* could not compress: write normal cluster */ |
5396234b | 4250 | ret = qcow2_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset, 0); |
8f1efd00 KW |
4251 | if (ret < 0) { |
4252 | goto fail; | |
4253 | } | |
fcccefc5 | 4254 | goto success; |
e1f4a37a AG |
4255 | } else if (out_len < 0) { |
4256 | ret = -EINVAL; | |
4257 | goto fail; | |
fcccefc5 | 4258 | } |
cf93980e | 4259 | |
fcccefc5 | 4260 | qemu_co_mutex_lock(&s->lock); |
77e023ff KW |
4261 | ret = qcow2_alloc_compressed_cluster_offset(bs, offset, out_len, |
4262 | &cluster_offset); | |
4263 | if (ret < 0) { | |
fcccefc5 | 4264 | qemu_co_mutex_unlock(&s->lock); |
fcccefc5 PB |
4265 | goto fail; |
4266 | } | |
cf93980e | 4267 | |
966b000f | 4268 | ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len, true); |
fcccefc5 PB |
4269 | qemu_co_mutex_unlock(&s->lock); |
4270 | if (ret < 0) { | |
4271 | goto fail; | |
20d97356 BS |
4272 | } |
4273 | ||
966b000f | 4274 | BLKDBG_EVENT(s->data_file, BLKDBG_WRITE_COMPRESSED); |
b00cb15b | 4275 | ret = bdrv_co_pwrite(s->data_file, cluster_offset, out_len, out_buf, 0); |
fcccefc5 PB |
4276 | if (ret < 0) { |
4277 | goto fail; | |
4278 | } | |
4279 | success: | |
8f1efd00 KW |
4280 | ret = 0; |
4281 | fail: | |
fcccefc5 | 4282 | qemu_vfree(buf); |
7267c094 | 4283 | g_free(out_buf); |
8f1efd00 | 4284 | return ret; |
20d97356 BS |
4285 | } |
4286 | ||
0d483dce AS |
4287 | static coroutine_fn int qcow2_co_pwritev_compressed_task_entry(AioTask *task) |
4288 | { | |
4289 | Qcow2AioTask *t = container_of(task, Qcow2AioTask, task); | |
4290 | ||
4291 | assert(!t->cluster_type && !t->l2meta); | |
4292 | ||
4293 | return qcow2_co_pwritev_compressed_task(t->bs, t->offset, t->bytes, t->qiov, | |
4294 | t->qiov_offset); | |
4295 | } | |
4296 | ||
4297 | /* | |
4298 | * XXX: put compressed sectors first, then all the cluster aligned | |
4299 | * tables to avoid losing bytes in alignment | |
4300 | */ | |
4301 | static coroutine_fn int | |
4302 | qcow2_co_pwritev_compressed_part(BlockDriverState *bs, | |
4303 | uint64_t offset, uint64_t bytes, | |
4304 | QEMUIOVector *qiov, size_t qiov_offset) | |
4305 | { | |
4306 | BDRVQcow2State *s = bs->opaque; | |
4307 | AioTaskPool *aio = NULL; | |
4308 | int ret = 0; | |
4309 | ||
4310 | if (has_data_file(bs)) { | |
4311 | return -ENOTSUP; | |
4312 | } | |
4313 | ||
4314 | if (bytes == 0) { | |
4315 | /* | |
4316 | * align end of file to a sector boundary to ease reading with | |
4317 | * sector based I/Os | |
4318 | */ | |
4319 | int64_t len = bdrv_getlength(bs->file->bs); | |
4320 | if (len < 0) { | |
4321 | return len; | |
4322 | } | |
4323 | return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, NULL); | |
4324 | } | |
4325 | ||
4326 | if (offset_into_cluster(s, offset)) { | |
4327 | return -EINVAL; | |
4328 | } | |
4329 | ||
4330 | while (bytes && aio_task_pool_status(aio) == 0) { | |
4331 | uint64_t chunk_size = MIN(bytes, s->cluster_size); | |
4332 | ||
4333 | if (!aio && chunk_size != bytes) { | |
4334 | aio = aio_task_pool_new(QCOW2_MAX_WORKERS); | |
4335 | } | |
4336 | ||
4337 | ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_compressed_task_entry, | |
4338 | 0, 0, offset, chunk_size, qiov, qiov_offset, NULL); | |
4339 | if (ret < 0) { | |
4340 | break; | |
4341 | } | |
4342 | qiov_offset += chunk_size; | |
4343 | offset += chunk_size; | |
4344 | bytes -= chunk_size; | |
4345 | } | |
4346 | ||
4347 | if (aio) { | |
4348 | aio_task_pool_wait_all(aio); | |
4349 | if (ret == 0) { | |
4350 | ret = aio_task_pool_status(aio); | |
4351 | } | |
4352 | g_free(aio); | |
4353 | } | |
4354 | ||
4355 | return ret; | |
4356 | } | |
4357 | ||
c3c10f72 VSO |
4358 | static int coroutine_fn |
4359 | qcow2_co_preadv_compressed(BlockDriverState *bs, | |
4360 | uint64_t file_cluster_offset, | |
4361 | uint64_t offset, | |
4362 | uint64_t bytes, | |
df893d25 VSO |
4363 | QEMUIOVector *qiov, |
4364 | size_t qiov_offset) | |
f4b3e2a9 VSO |
4365 | { |
4366 | BDRVQcow2State *s = bs->opaque; | |
c3c10f72 | 4367 | int ret = 0, csize, nb_csectors; |
f4b3e2a9 | 4368 | uint64_t coffset; |
c3c10f72 | 4369 | uint8_t *buf, *out_buf; |
c3c10f72 | 4370 | int offset_in_cluster = offset_into_cluster(s, offset); |
f4b3e2a9 | 4371 | |
c3c10f72 VSO |
4372 | coffset = file_cluster_offset & s->cluster_offset_mask; |
4373 | nb_csectors = ((file_cluster_offset >> s->csize_shift) & s->csize_mask) + 1; | |
b6c24694 AG |
4374 | csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE - |
4375 | (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK); | |
f4b3e2a9 | 4376 | |
c3c10f72 VSO |
4377 | buf = g_try_malloc(csize); |
4378 | if (!buf) { | |
4379 | return -ENOMEM; | |
4380 | } | |
f4b3e2a9 | 4381 | |
c3c10f72 | 4382 | out_buf = qemu_blockalign(bs, s->cluster_size); |
c068a1cd | 4383 | |
c3c10f72 | 4384 | BLKDBG_EVENT(bs->file, BLKDBG_READ_COMPRESSED); |
b00cb15b | 4385 | ret = bdrv_co_pread(bs->file, coffset, csize, buf, 0); |
c3c10f72 VSO |
4386 | if (ret < 0) { |
4387 | goto fail; | |
f4b3e2a9 | 4388 | } |
c3c10f72 | 4389 | |
e23c9d7a | 4390 | if (qcow2_co_decompress(bs, out_buf, s->cluster_size, buf, csize) < 0) { |
c3c10f72 VSO |
4391 | ret = -EIO; |
4392 | goto fail; | |
4393 | } | |
4394 | ||
df893d25 | 4395 | qemu_iovec_from_buf(qiov, qiov_offset, out_buf + offset_in_cluster, bytes); |
c3c10f72 VSO |
4396 | |
4397 | fail: | |
4398 | qemu_vfree(out_buf); | |
4399 | g_free(buf); | |
4400 | ||
4401 | return ret; | |
f4b3e2a9 VSO |
4402 | } |
4403 | ||
94054183 HR |
4404 | static int make_completely_empty(BlockDriverState *bs) |
4405 | { | |
ff99129a | 4406 | BDRVQcow2State *s = bs->opaque; |
ed3d2ec9 | 4407 | Error *local_err = NULL; |
94054183 HR |
4408 | int ret, l1_clusters; |
4409 | int64_t offset; | |
4410 | uint64_t *new_reftable = NULL; | |
4411 | uint64_t rt_entry, l1_size2; | |
4412 | struct { | |
4413 | uint64_t l1_offset; | |
4414 | uint64_t reftable_offset; | |
4415 | uint32_t reftable_clusters; | |
4416 | } QEMU_PACKED l1_ofs_rt_ofs_cls; | |
4417 | ||
4418 | ret = qcow2_cache_empty(bs, s->l2_table_cache); | |
4419 | if (ret < 0) { | |
4420 | goto fail; | |
4421 | } | |
4422 | ||
4423 | ret = qcow2_cache_empty(bs, s->refcount_block_cache); | |
4424 | if (ret < 0) { | |
4425 | goto fail; | |
4426 | } | |
4427 | ||
4428 | /* Refcounts will be broken utterly */ | |
4429 | ret = qcow2_mark_dirty(bs); | |
4430 | if (ret < 0) { | |
4431 | goto fail; | |
4432 | } | |
4433 | ||
4434 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); | |
4435 | ||
4436 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t)); | |
4437 | l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t); | |
4438 | ||
4439 | /* After this call, neither the in-memory nor the on-disk refcount | |
4440 | * information accurately describe the actual references */ | |
4441 | ||
720ff280 | 4442 | ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset, |
74021bc4 | 4443 | l1_clusters * s->cluster_size, 0); |
94054183 HR |
4444 | if (ret < 0) { |
4445 | goto fail_broken_refcounts; | |
4446 | } | |
4447 | memset(s->l1_table, 0, l1_size2); | |
4448 | ||
4449 | BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE); | |
4450 | ||
4451 | /* Overwrite enough clusters at the beginning of the sectors to place | |
4452 | * the refcount table, a refcount block and the L1 table in; this may | |
4453 | * overwrite parts of the existing refcount and L1 table, which is not | |
4454 | * an issue because the dirty flag is set, complete data loss is in fact | |
4455 | * desired and partial data loss is consequently fine as well */ | |
720ff280 | 4456 | ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size, |
74021bc4 | 4457 | (2 + l1_clusters) * s->cluster_size, 0); |
94054183 HR |
4458 | /* This call (even if it failed overall) may have overwritten on-disk |
4459 | * refcount structures; in that case, the in-memory refcount information | |
4460 | * will probably differ from the on-disk information which makes the BDS | |
4461 | * unusable */ | |
4462 | if (ret < 0) { | |
4463 | goto fail_broken_refcounts; | |
4464 | } | |
4465 | ||
4466 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); | |
4467 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE); | |
4468 | ||
4469 | /* "Create" an empty reftable (one cluster) directly after the image | |
4470 | * header and an empty L1 table three clusters after the image header; | |
4471 | * the cluster between those two will be used as the first refblock */ | |
f1f7a1dd PM |
4472 | l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size); |
4473 | l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size); | |
4474 | l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1); | |
d9ca2ea2 | 4475 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset), |
94054183 HR |
4476 | &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls)); |
4477 | if (ret < 0) { | |
4478 | goto fail_broken_refcounts; | |
4479 | } | |
4480 | ||
4481 | s->l1_table_offset = 3 * s->cluster_size; | |
4482 | ||
4483 | new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t)); | |
4484 | if (!new_reftable) { | |
4485 | ret = -ENOMEM; | |
4486 | goto fail_broken_refcounts; | |
4487 | } | |
4488 | ||
4489 | s->refcount_table_offset = s->cluster_size; | |
4490 | s->refcount_table_size = s->cluster_size / sizeof(uint64_t); | |
7061a078 | 4491 | s->max_refcount_table_index = 0; |
94054183 HR |
4492 | |
4493 | g_free(s->refcount_table); | |
4494 | s->refcount_table = new_reftable; | |
4495 | new_reftable = NULL; | |
4496 | ||
4497 | /* Now the in-memory refcount information again corresponds to the on-disk | |
4498 | * information (reftable is empty and no refblocks (the refblock cache is | |
4499 | * empty)); however, this means some clusters (e.g. the image header) are | |
4500 | * referenced, but not refcounted, but the normal qcow2 code assumes that | |
4501 | * the in-memory information is always correct */ | |
4502 | ||
4503 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); | |
4504 | ||
4505 | /* Enter the first refblock into the reftable */ | |
4506 | rt_entry = cpu_to_be64(2 * s->cluster_size); | |
d9ca2ea2 | 4507 | ret = bdrv_pwrite_sync(bs->file, s->cluster_size, |
94054183 HR |
4508 | &rt_entry, sizeof(rt_entry)); |
4509 | if (ret < 0) { | |
4510 | goto fail_broken_refcounts; | |
4511 | } | |
4512 | s->refcount_table[0] = 2 * s->cluster_size; | |
4513 | ||
4514 | s->free_cluster_index = 0; | |
4515 | assert(3 + l1_clusters <= s->refcount_block_size); | |
4516 | offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2); | |
4517 | if (offset < 0) { | |
4518 | ret = offset; | |
4519 | goto fail_broken_refcounts; | |
4520 | } else if (offset > 0) { | |
4521 | error_report("First cluster in emptied image is in use"); | |
4522 | abort(); | |
4523 | } | |
4524 | ||
4525 | /* Now finally the in-memory information corresponds to the on-disk | |
4526 | * structures and is correct */ | |
4527 | ret = qcow2_mark_clean(bs); | |
4528 | if (ret < 0) { | |
4529 | goto fail; | |
4530 | } | |
4531 | ||
c80d8b06 | 4532 | ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false, |
7ea37c30 | 4533 | PREALLOC_MODE_OFF, &local_err); |
94054183 | 4534 | if (ret < 0) { |
ed3d2ec9 | 4535 | error_report_err(local_err); |
94054183 HR |
4536 | goto fail; |
4537 | } | |
4538 | ||
4539 | return 0; | |
4540 | ||
4541 | fail_broken_refcounts: | |
4542 | /* The BDS is unusable at this point. If we wanted to make it usable, we | |
4543 | * would have to call qcow2_refcount_close(), qcow2_refcount_init(), | |
4544 | * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init() | |
4545 | * again. However, because the functions which could have caused this error | |
4546 | * path to be taken are used by those functions as well, it's very likely | |
4547 | * that that sequence will fail as well. Therefore, just eject the BDS. */ | |
4548 | bs->drv = NULL; | |
4549 | ||
4550 | fail: | |
4551 | g_free(new_reftable); | |
4552 | return ret; | |
4553 | } | |
4554 | ||
491d27e2 HR |
4555 | static int qcow2_make_empty(BlockDriverState *bs) |
4556 | { | |
ff99129a | 4557 | BDRVQcow2State *s = bs->opaque; |
d2cb36af EB |
4558 | uint64_t offset, end_offset; |
4559 | int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size); | |
94054183 HR |
4560 | int l1_clusters, ret = 0; |
4561 | ||
4562 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t)); | |
4563 | ||
4096974e | 4564 | if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps && |
f0603329 | 4565 | 3 + l1_clusters <= s->refcount_block_size && |
db04524f KW |
4566 | s->crypt_method_header != QCOW_CRYPT_LUKS && |
4567 | !has_data_file(bs)) { | |
4096974e EB |
4568 | /* The following function only works for qcow2 v3 images (it |
4569 | * requires the dirty flag) and only as long as there are no | |
4570 | * features that reserve extra clusters (such as snapshots, | |
4571 | * LUKS header, or persistent bitmaps), because it completely | |
4572 | * empties the image. Furthermore, the L1 table and three | |
4573 | * additional clusters (image header, refcount table, one | |
db04524f KW |
4574 | * refcount block) have to fit inside one refcount block. It |
4575 | * only resets the image file, i.e. does not work with an | |
4576 | * external data file. */ | |
94054183 HR |
4577 | return make_completely_empty(bs); |
4578 | } | |
491d27e2 | 4579 | |
94054183 HR |
4580 | /* This fallback code simply discards every active cluster; this is slow, |
4581 | * but works in all cases */ | |
d2cb36af EB |
4582 | end_offset = bs->total_sectors * BDRV_SECTOR_SIZE; |
4583 | for (offset = 0; offset < end_offset; offset += step) { | |
491d27e2 HR |
4584 | /* As this function is generally used after committing an external |
4585 | * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the | |
4586 | * default action for this kind of discard is to pass the discard, | |
4587 | * which will ideally result in an actually smaller image file, as | |
4588 | * is probably desired. */ | |
d2cb36af EB |
4589 | ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset), |
4590 | QCOW2_DISCARD_SNAPSHOT, true); | |
491d27e2 HR |
4591 | if (ret < 0) { |
4592 | break; | |
4593 | } | |
4594 | } | |
4595 | ||
4596 | return ret; | |
4597 | } | |
4598 | ||
a968168c | 4599 | static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) |
20d97356 | 4600 | { |
ff99129a | 4601 | BDRVQcow2State *s = bs->opaque; |
29c1a730 KW |
4602 | int ret; |
4603 | ||
8b94ff85 | 4604 | qemu_co_mutex_lock(&s->lock); |
8b220eb7 | 4605 | ret = qcow2_write_caches(bs); |
8b94ff85 | 4606 | qemu_co_mutex_unlock(&s->lock); |
29c1a730 | 4607 | |
8b220eb7 | 4608 | return ret; |
eb489bb1 KW |
4609 | } |
4610 | ||
c501c352 SH |
4611 | static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs, |
4612 | Error **errp) | |
4613 | { | |
4614 | Error *local_err = NULL; | |
4615 | BlockMeasureInfo *info; | |
4616 | uint64_t required = 0; /* bytes that contribute to required size */ | |
4617 | uint64_t virtual_size; /* disk size as seen by guest */ | |
4618 | uint64_t refcount_bits; | |
4619 | uint64_t l2_tables; | |
61914f89 | 4620 | uint64_t luks_payload_size = 0; |
c501c352 SH |
4621 | size_t cluster_size; |
4622 | int version; | |
4623 | char *optstr; | |
4624 | PreallocMode prealloc; | |
4625 | bool has_backing_file; | |
61914f89 | 4626 | bool has_luks; |
c501c352 SH |
4627 | |
4628 | /* Parse image creation options */ | |
4629 | cluster_size = qcow2_opt_get_cluster_size_del(opts, &local_err); | |
4630 | if (local_err) { | |
4631 | goto err; | |
4632 | } | |
4633 | ||
4634 | version = qcow2_opt_get_version_del(opts, &local_err); | |
4635 | if (local_err) { | |
4636 | goto err; | |
4637 | } | |
4638 | ||
4639 | refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err); | |
4640 | if (local_err) { | |
4641 | goto err; | |
4642 | } | |
4643 | ||
4644 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); | |
f7abe0ec | 4645 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr, |
06c60b6c | 4646 | PREALLOC_MODE_OFF, &local_err); |
c501c352 SH |
4647 | g_free(optstr); |
4648 | if (local_err) { | |
4649 | goto err; | |
4650 | } | |
4651 | ||
4652 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); | |
4653 | has_backing_file = !!optstr; | |
4654 | g_free(optstr); | |
4655 | ||
61914f89 SH |
4656 | optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
4657 | has_luks = optstr && strcmp(optstr, "luks") == 0; | |
4658 | g_free(optstr); | |
4659 | ||
4660 | if (has_luks) { | |
6d49d3a8 SH |
4661 | g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL; |
4662 | QDict *opts_qdict; | |
4663 | QDict *cryptoopts; | |
61914f89 SH |
4664 | size_t headerlen; |
4665 | ||
6d49d3a8 SH |
4666 | opts_qdict = qemu_opts_to_qdict(opts, NULL); |
4667 | qdict_extract_subqdict(opts_qdict, &cryptoopts, "encrypt."); | |
4668 | qobject_unref(opts_qdict); | |
4669 | ||
4670 | qdict_put_str(cryptoopts, "format", "luks"); | |
4671 | ||
4672 | create_opts = block_crypto_create_opts_init(cryptoopts, errp); | |
4673 | qobject_unref(cryptoopts); | |
4674 | if (!create_opts) { | |
4675 | goto err; | |
4676 | } | |
4677 | ||
4678 | if (!qcrypto_block_calculate_payload_offset(create_opts, | |
4679 | "encrypt.", | |
4680 | &headerlen, | |
4681 | &local_err)) { | |
61914f89 SH |
4682 | goto err; |
4683 | } | |
4684 | ||
4685 | luks_payload_size = ROUND_UP(headerlen, cluster_size); | |
4686 | } | |
4687 | ||
9e029689 AG |
4688 | virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); |
4689 | virtual_size = ROUND_UP(virtual_size, cluster_size); | |
c501c352 SH |
4690 | |
4691 | /* Check that virtual disk size is valid */ | |
4692 | l2_tables = DIV_ROUND_UP(virtual_size / cluster_size, | |
4693 | cluster_size / sizeof(uint64_t)); | |
4694 | if (l2_tables * sizeof(uint64_t) > QCOW_MAX_L1_SIZE) { | |
4695 | error_setg(&local_err, "The image size is too large " | |
4696 | "(try using a larger cluster size)"); | |
4697 | goto err; | |
4698 | } | |
4699 | ||
4700 | /* Account for input image */ | |
4701 | if (in_bs) { | |
4702 | int64_t ssize = bdrv_getlength(in_bs); | |
4703 | if (ssize < 0) { | |
4704 | error_setg_errno(&local_err, -ssize, | |
4705 | "Unable to get image virtual_size"); | |
4706 | goto err; | |
4707 | } | |
4708 | ||
9e029689 | 4709 | virtual_size = ROUND_UP(ssize, cluster_size); |
c501c352 SH |
4710 | |
4711 | if (has_backing_file) { | |
4712 | /* We don't how much of the backing chain is shared by the input | |
4713 | * image and the new image file. In the worst case the new image's | |
4714 | * backing file has nothing in common with the input image. Be | |
4715 | * conservative and assume all clusters need to be written. | |
4716 | */ | |
4717 | required = virtual_size; | |
4718 | } else { | |
b85ee453 | 4719 | int64_t offset; |
31826642 | 4720 | int64_t pnum = 0; |
c501c352 | 4721 | |
31826642 EB |
4722 | for (offset = 0; offset < ssize; offset += pnum) { |
4723 | int ret; | |
c501c352 | 4724 | |
31826642 EB |
4725 | ret = bdrv_block_status_above(in_bs, NULL, offset, |
4726 | ssize - offset, &pnum, NULL, | |
4727 | NULL); | |
c501c352 SH |
4728 | if (ret < 0) { |
4729 | error_setg_errno(&local_err, -ret, | |
4730 | "Unable to get block status"); | |
4731 | goto err; | |
4732 | } | |
4733 | ||
4734 | if (ret & BDRV_BLOCK_ZERO) { | |
4735 | /* Skip zero regions (safe with no backing file) */ | |
4736 | } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) == | |
4737 | (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) { | |
4738 | /* Extend pnum to end of cluster for next iteration */ | |
31826642 | 4739 | pnum = ROUND_UP(offset + pnum, cluster_size) - offset; |
c501c352 SH |
4740 | |
4741 | /* Count clusters we've seen */ | |
31826642 | 4742 | required += offset % cluster_size + pnum; |
c501c352 SH |
4743 | } |
4744 | } | |
4745 | } | |
4746 | } | |
4747 | ||
4748 | /* Take into account preallocation. Nothing special is needed for | |
4749 | * PREALLOC_MODE_METADATA since metadata is always counted. | |
4750 | */ | |
4751 | if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) { | |
4752 | required = virtual_size; | |
4753 | } | |
4754 | ||
4755 | info = g_new(BlockMeasureInfo, 1); | |
4756 | info->fully_allocated = | |
4757 | qcow2_calc_prealloc_size(virtual_size, cluster_size, | |
61914f89 | 4758 | ctz32(refcount_bits)) + luks_payload_size; |
c501c352 SH |
4759 | |
4760 | /* Remove data clusters that are not required. This overestimates the | |
4761 | * required size because metadata needed for the fully allocated file is | |
4762 | * still counted. | |
4763 | */ | |
4764 | info->required = info->fully_allocated - virtual_size + required; | |
4765 | return info; | |
4766 | ||
4767 | err: | |
4768 | error_propagate(errp, local_err); | |
4769 | return NULL; | |
4770 | } | |
4771 | ||
7c80ab3f | 4772 | static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
20d97356 | 4773 | { |
ff99129a | 4774 | BDRVQcow2State *s = bs->opaque; |
95de6d70 | 4775 | bdi->unallocated_blocks_are_zero = true; |
20d97356 | 4776 | bdi->cluster_size = s->cluster_size; |
7c80ab3f | 4777 | bdi->vm_state_offset = qcow2_vm_state_offset(s); |
20d97356 BS |
4778 | return 0; |
4779 | } | |
4780 | ||
1bf6e9ca AS |
4781 | static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs, |
4782 | Error **errp) | |
37764dfb | 4783 | { |
ff99129a | 4784 | BDRVQcow2State *s = bs->opaque; |
0a12f6f8 DB |
4785 | ImageInfoSpecific *spec_info; |
4786 | QCryptoBlockInfo *encrypt_info = NULL; | |
1bf6e9ca | 4787 | Error *local_err = NULL; |
37764dfb | 4788 | |
0a12f6f8 | 4789 | if (s->crypto != NULL) { |
1bf6e9ca AS |
4790 | encrypt_info = qcrypto_block_get_info(s->crypto, &local_err); |
4791 | if (local_err) { | |
4792 | error_propagate(errp, local_err); | |
4793 | return NULL; | |
4794 | } | |
0a12f6f8 DB |
4795 | } |
4796 | ||
4797 | spec_info = g_new(ImageInfoSpecific, 1); | |
37764dfb | 4798 | *spec_info = (ImageInfoSpecific){ |
6a8f9661 | 4799 | .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2, |
b8968c87 | 4800 | .u.qcow2.data = g_new0(ImageInfoSpecificQCow2, 1), |
37764dfb HR |
4801 | }; |
4802 | if (s->qcow_version == 2) { | |
32bafa8f | 4803 | *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){ |
0709c5a1 HR |
4804 | .compat = g_strdup("0.10"), |
4805 | .refcount_bits = s->refcount_bits, | |
37764dfb HR |
4806 | }; |
4807 | } else if (s->qcow_version == 3) { | |
b8968c87 AS |
4808 | Qcow2BitmapInfoList *bitmaps; |
4809 | bitmaps = qcow2_get_bitmap_info_list(bs, &local_err); | |
4810 | if (local_err) { | |
4811 | error_propagate(errp, local_err); | |
4812 | qapi_free_ImageInfoSpecific(spec_info); | |
4813 | return NULL; | |
4814 | } | |
32bafa8f | 4815 | *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){ |
37764dfb HR |
4816 | .compat = g_strdup("1.1"), |
4817 | .lazy_refcounts = s->compatible_features & | |
4818 | QCOW2_COMPAT_LAZY_REFCOUNTS, | |
4819 | .has_lazy_refcounts = true, | |
9009b196 HR |
4820 | .corrupt = s->incompatible_features & |
4821 | QCOW2_INCOMPAT_CORRUPT, | |
4822 | .has_corrupt = true, | |
0709c5a1 | 4823 | .refcount_bits = s->refcount_bits, |
b8968c87 AS |
4824 | .has_bitmaps = !!bitmaps, |
4825 | .bitmaps = bitmaps, | |
9b890bdc KW |
4826 | .has_data_file = !!s->image_data_file, |
4827 | .data_file = g_strdup(s->image_data_file), | |
6c3944dc KW |
4828 | .has_data_file_raw = has_data_file(bs), |
4829 | .data_file_raw = data_file_is_raw(bs), | |
37764dfb | 4830 | }; |
b1fc8f93 DL |
4831 | } else { |
4832 | /* if this assertion fails, this probably means a new version was | |
4833 | * added without having it covered here */ | |
4834 | assert(false); | |
37764dfb HR |
4835 | } |
4836 | ||
0a12f6f8 DB |
4837 | if (encrypt_info) { |
4838 | ImageInfoSpecificQCow2Encryption *qencrypt = | |
4839 | g_new(ImageInfoSpecificQCow2Encryption, 1); | |
4840 | switch (encrypt_info->format) { | |
4841 | case Q_CRYPTO_BLOCK_FORMAT_QCOW: | |
4842 | qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES; | |
0a12f6f8 DB |
4843 | break; |
4844 | case Q_CRYPTO_BLOCK_FORMAT_LUKS: | |
4845 | qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS; | |
4846 | qencrypt->u.luks = encrypt_info->u.luks; | |
4847 | break; | |
4848 | default: | |
4849 | abort(); | |
4850 | } | |
4851 | /* Since we did shallow copy above, erase any pointers | |
4852 | * in the original info */ | |
4853 | memset(&encrypt_info->u, 0, sizeof(encrypt_info->u)); | |
4854 | qapi_free_QCryptoBlockInfo(encrypt_info); | |
4855 | ||
4856 | spec_info->u.qcow2.data->has_encrypt = true; | |
4857 | spec_info->u.qcow2.data->encrypt = qencrypt; | |
4858 | } | |
4859 | ||
37764dfb HR |
4860 | return spec_info; |
4861 | } | |
4862 | ||
38841dcd HR |
4863 | static int qcow2_has_zero_init(BlockDriverState *bs) |
4864 | { | |
4865 | BDRVQcow2State *s = bs->opaque; | |
4866 | bool preallocated; | |
4867 | ||
4868 | if (qemu_in_coroutine()) { | |
4869 | qemu_co_mutex_lock(&s->lock); | |
4870 | } | |
4871 | /* | |
4872 | * Check preallocation status: Preallocated images have all L2 | |
4873 | * tables allocated, nonpreallocated images have none. It is | |
4874 | * therefore enough to check the first one. | |
4875 | */ | |
4876 | preallocated = s->l1_size > 0 && s->l1_table[0] != 0; | |
4877 | if (qemu_in_coroutine()) { | |
4878 | qemu_co_mutex_unlock(&s->lock); | |
4879 | } | |
4880 | ||
4881 | if (!preallocated) { | |
4882 | return 1; | |
4883 | } else if (bs->encrypted) { | |
4884 | return 0; | |
4885 | } else { | |
4886 | return bdrv_has_zero_init(s->data_file->bs); | |
4887 | } | |
4888 | } | |
4889 | ||
cf8074b3 KW |
4890 | static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
4891 | int64_t pos) | |
20d97356 | 4892 | { |
ff99129a | 4893 | BDRVQcow2State *s = bs->opaque; |
20d97356 | 4894 | |
66f82cee | 4895 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
5396234b VSO |
4896 | return bs->drv->bdrv_co_pwritev_part(bs, qcow2_vm_state_offset(s) + pos, |
4897 | qiov->size, qiov, 0, 0); | |
20d97356 BS |
4898 | } |
4899 | ||
5ddda0b8 KW |
4900 | static int qcow2_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
4901 | int64_t pos) | |
20d97356 | 4902 | { |
ff99129a | 4903 | BDRVQcow2State *s = bs->opaque; |
20d97356 | 4904 | |
66f82cee | 4905 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
df893d25 VSO |
4906 | return bs->drv->bdrv_co_preadv_part(bs, qcow2_vm_state_offset(s) + pos, |
4907 | qiov->size, qiov, 0, 0); | |
20d97356 BS |
4908 | } |
4909 | ||
9296b3ed HR |
4910 | /* |
4911 | * Downgrades an image's version. To achieve this, any incompatible features | |
4912 | * have to be removed. | |
4913 | */ | |
4057a2b2 | 4914 | static int qcow2_downgrade(BlockDriverState *bs, int target_version, |
d1402b50 HR |
4915 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, |
4916 | Error **errp) | |
9296b3ed | 4917 | { |
ff99129a | 4918 | BDRVQcow2State *s = bs->opaque; |
9296b3ed HR |
4919 | int current_version = s->qcow_version; |
4920 | int ret; | |
4921 | ||
d1402b50 HR |
4922 | /* This is qcow2_downgrade(), not qcow2_upgrade() */ |
4923 | assert(target_version < current_version); | |
4924 | ||
4925 | /* There are no other versions (now) that you can downgrade to */ | |
4926 | assert(target_version == 2); | |
9296b3ed HR |
4927 | |
4928 | if (s->refcount_order != 4) { | |
d1402b50 | 4929 | error_setg(errp, "compat=0.10 requires refcount_bits=16"); |
9296b3ed HR |
4930 | return -ENOTSUP; |
4931 | } | |
4932 | ||
966b000f KW |
4933 | if (has_data_file(bs)) { |
4934 | error_setg(errp, "Cannot downgrade an image with a data file"); | |
4935 | return -ENOTSUP; | |
4936 | } | |
4937 | ||
9296b3ed HR |
4938 | /* clear incompatible features */ |
4939 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
4940 | ret = qcow2_mark_clean(bs); | |
4941 | if (ret < 0) { | |
d1402b50 | 4942 | error_setg_errno(errp, -ret, "Failed to make the image clean"); |
9296b3ed HR |
4943 | return ret; |
4944 | } | |
4945 | } | |
4946 | ||
4947 | /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in | |
4948 | * the first place; if that happens nonetheless, returning -ENOTSUP is the | |
4949 | * best thing to do anyway */ | |
4950 | ||
4951 | if (s->incompatible_features) { | |
d1402b50 HR |
4952 | error_setg(errp, "Cannot downgrade an image with incompatible features " |
4953 | "%#" PRIx64 " set", s->incompatible_features); | |
9296b3ed HR |
4954 | return -ENOTSUP; |
4955 | } | |
4956 | ||
4957 | /* since we can ignore compatible features, we can set them to 0 as well */ | |
4958 | s->compatible_features = 0; | |
4959 | /* if lazy refcounts have been used, they have already been fixed through | |
4960 | * clearing the dirty flag */ | |
4961 | ||
4962 | /* clearing autoclear features is trivial */ | |
4963 | s->autoclear_features = 0; | |
4964 | ||
8b13976d | 4965 | ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque); |
9296b3ed | 4966 | if (ret < 0) { |
d1402b50 | 4967 | error_setg_errno(errp, -ret, "Failed to turn zero into data clusters"); |
9296b3ed HR |
4968 | return ret; |
4969 | } | |
4970 | ||
4971 | s->qcow_version = target_version; | |
4972 | ret = qcow2_update_header(bs); | |
4973 | if (ret < 0) { | |
4974 | s->qcow_version = current_version; | |
d1402b50 | 4975 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
9296b3ed HR |
4976 | return ret; |
4977 | } | |
4978 | return 0; | |
4979 | } | |
4980 | ||
722efb0c HR |
4981 | /* |
4982 | * Upgrades an image's version. While newer versions encompass all | |
4983 | * features of older versions, some things may have to be presented | |
4984 | * differently. | |
4985 | */ | |
4986 | static int qcow2_upgrade(BlockDriverState *bs, int target_version, | |
4987 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque, | |
4988 | Error **errp) | |
4989 | { | |
4990 | BDRVQcow2State *s = bs->opaque; | |
0a85af35 | 4991 | bool need_snapshot_update; |
722efb0c | 4992 | int current_version = s->qcow_version; |
0a85af35 | 4993 | int i; |
722efb0c HR |
4994 | int ret; |
4995 | ||
4996 | /* This is qcow2_upgrade(), not qcow2_downgrade() */ | |
4997 | assert(target_version > current_version); | |
4998 | ||
4999 | /* There are no other versions (yet) that you can upgrade to */ | |
5000 | assert(target_version == 3); | |
5001 | ||
0a85af35 HR |
5002 | status_cb(bs, 0, 2, cb_opaque); |
5003 | ||
5004 | /* | |
5005 | * In v2, snapshots do not need to have extra data. v3 requires | |
5006 | * the 64-bit VM state size and the virtual disk size to be | |
5007 | * present. | |
5008 | * qcow2_write_snapshots() will always write the list in the | |
5009 | * v3-compliant format. | |
5010 | */ | |
5011 | need_snapshot_update = false; | |
5012 | for (i = 0; i < s->nb_snapshots; i++) { | |
5013 | if (s->snapshots[i].extra_data_size < | |
5014 | sizeof_field(QCowSnapshotExtraData, vm_state_size_large) + | |
5015 | sizeof_field(QCowSnapshotExtraData, disk_size)) | |
5016 | { | |
5017 | need_snapshot_update = true; | |
5018 | break; | |
5019 | } | |
5020 | } | |
5021 | if (need_snapshot_update) { | |
5022 | ret = qcow2_write_snapshots(bs); | |
5023 | if (ret < 0) { | |
5024 | error_setg_errno(errp, -ret, "Failed to update the snapshot table"); | |
5025 | return ret; | |
5026 | } | |
5027 | } | |
5028 | status_cb(bs, 1, 2, cb_opaque); | |
722efb0c HR |
5029 | |
5030 | s->qcow_version = target_version; | |
5031 | ret = qcow2_update_header(bs); | |
5032 | if (ret < 0) { | |
5033 | s->qcow_version = current_version; | |
5034 | error_setg_errno(errp, -ret, "Failed to update the image header"); | |
5035 | return ret; | |
5036 | } | |
0a85af35 | 5037 | status_cb(bs, 2, 2, cb_opaque); |
722efb0c HR |
5038 | |
5039 | return 0; | |
5040 | } | |
5041 | ||
c293a809 HR |
5042 | typedef enum Qcow2AmendOperation { |
5043 | /* This is the value Qcow2AmendHelperCBInfo::last_operation will be | |
5044 | * statically initialized to so that the helper CB can discern the first | |
5045 | * invocation from an operation change */ | |
5046 | QCOW2_NO_OPERATION = 0, | |
5047 | ||
722efb0c | 5048 | QCOW2_UPGRADING, |
61ce55fc | 5049 | QCOW2_CHANGING_REFCOUNT_ORDER, |
c293a809 HR |
5050 | QCOW2_DOWNGRADING, |
5051 | } Qcow2AmendOperation; | |
5052 | ||
5053 | typedef struct Qcow2AmendHelperCBInfo { | |
5054 | /* The code coordinating the amend operations should only modify | |
5055 | * these four fields; the rest will be managed by the CB */ | |
5056 | BlockDriverAmendStatusCB *original_status_cb; | |
5057 | void *original_cb_opaque; | |
5058 | ||
5059 | Qcow2AmendOperation current_operation; | |
5060 | ||
5061 | /* Total number of operations to perform (only set once) */ | |
5062 | int total_operations; | |
5063 | ||
5064 | /* The following fields are managed by the CB */ | |
5065 | ||
5066 | /* Number of operations completed */ | |
5067 | int operations_completed; | |
5068 | ||
5069 | /* Cumulative offset of all completed operations */ | |
5070 | int64_t offset_completed; | |
5071 | ||
5072 | Qcow2AmendOperation last_operation; | |
5073 | int64_t last_work_size; | |
5074 | } Qcow2AmendHelperCBInfo; | |
5075 | ||
5076 | static void qcow2_amend_helper_cb(BlockDriverState *bs, | |
5077 | int64_t operation_offset, | |
5078 | int64_t operation_work_size, void *opaque) | |
5079 | { | |
5080 | Qcow2AmendHelperCBInfo *info = opaque; | |
5081 | int64_t current_work_size; | |
5082 | int64_t projected_work_size; | |
5083 | ||
5084 | if (info->current_operation != info->last_operation) { | |
5085 | if (info->last_operation != QCOW2_NO_OPERATION) { | |
5086 | info->offset_completed += info->last_work_size; | |
5087 | info->operations_completed++; | |
5088 | } | |
5089 | ||
5090 | info->last_operation = info->current_operation; | |
5091 | } | |
5092 | ||
5093 | assert(info->total_operations > 0); | |
5094 | assert(info->operations_completed < info->total_operations); | |
5095 | ||
5096 | info->last_work_size = operation_work_size; | |
5097 | ||
5098 | current_work_size = info->offset_completed + operation_work_size; | |
5099 | ||
5100 | /* current_work_size is the total work size for (operations_completed + 1) | |
5101 | * operations (which includes this one), so multiply it by the number of | |
5102 | * operations not covered and divide it by the number of operations | |
5103 | * covered to get a projection for the operations not covered */ | |
5104 | projected_work_size = current_work_size * (info->total_operations - | |
5105 | info->operations_completed - 1) | |
5106 | / (info->operations_completed + 1); | |
5107 | ||
5108 | info->original_status_cb(bs, info->offset_completed + operation_offset, | |
5109 | current_work_size + projected_work_size, | |
5110 | info->original_cb_opaque); | |
5111 | } | |
5112 | ||
77485434 | 5113 | static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts, |
8b13976d | 5114 | BlockDriverAmendStatusCB *status_cb, |
d1402b50 HR |
5115 | void *cb_opaque, |
5116 | Error **errp) | |
9296b3ed | 5117 | { |
ff99129a | 5118 | BDRVQcow2State *s = bs->opaque; |
9296b3ed HR |
5119 | int old_version = s->qcow_version, new_version = old_version; |
5120 | uint64_t new_size = 0; | |
9b890bdc | 5121 | const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL; |
9296b3ed | 5122 | bool lazy_refcounts = s->use_lazy_refcounts; |
6c3944dc | 5123 | bool data_file_raw = data_file_is_raw(bs); |
1bd0e2d1 CL |
5124 | const char *compat = NULL; |
5125 | uint64_t cluster_size = s->cluster_size; | |
5126 | bool encrypt; | |
4652b8f3 | 5127 | int encformat; |
61ce55fc | 5128 | int refcount_bits = s->refcount_bits; |
9296b3ed | 5129 | int ret; |
1bd0e2d1 | 5130 | QemuOptDesc *desc = opts->list->desc; |
c293a809 | 5131 | Qcow2AmendHelperCBInfo helper_cb_info; |
9296b3ed | 5132 | |
1bd0e2d1 CL |
5133 | while (desc && desc->name) { |
5134 | if (!qemu_opt_find(opts, desc->name)) { | |
9296b3ed | 5135 | /* only change explicitly defined options */ |
1bd0e2d1 | 5136 | desc++; |
9296b3ed HR |
5137 | continue; |
5138 | } | |
5139 | ||
8a17b83c HR |
5140 | if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) { |
5141 | compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL); | |
1bd0e2d1 | 5142 | if (!compat) { |
9296b3ed | 5143 | /* preserve default */ |
f7077c98 | 5144 | } else if (!strcmp(compat, "0.10") || !strcmp(compat, "v2")) { |
9296b3ed | 5145 | new_version = 2; |
f7077c98 | 5146 | } else if (!strcmp(compat, "1.1") || !strcmp(compat, "v3")) { |
9296b3ed HR |
5147 | new_version = 3; |
5148 | } else { | |
d1402b50 | 5149 | error_setg(errp, "Unknown compatibility level %s", compat); |
9296b3ed HR |
5150 | return -EINVAL; |
5151 | } | |
8a17b83c | 5152 | } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) { |
d1402b50 | 5153 | error_setg(errp, "Cannot change preallocation mode"); |
9296b3ed | 5154 | return -ENOTSUP; |
8a17b83c HR |
5155 | } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) { |
5156 | new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); | |
5157 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) { | |
5158 | backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); | |
5159 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) { | |
5160 | backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); | |
5161 | } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) { | |
5162 | encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, | |
b25b387f | 5163 | !!s->crypto); |
f6fa64f6 | 5164 | |
b25b387f | 5165 | if (encrypt != !!s->crypto) { |
d1402b50 HR |
5166 | error_setg(errp, |
5167 | "Changing the encryption flag is not supported"); | |
9296b3ed HR |
5168 | return -ENOTSUP; |
5169 | } | |
4652b8f3 DB |
5170 | } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT_FORMAT)) { |
5171 | encformat = qcow2_crypt_method_from_format( | |
5172 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT)); | |
5173 | ||
5174 | if (encformat != s->crypt_method_header) { | |
d1402b50 HR |
5175 | error_setg(errp, |
5176 | "Changing the encryption format is not supported"); | |
4652b8f3 DB |
5177 | return -ENOTSUP; |
5178 | } | |
f66afbe2 | 5179 | } else if (g_str_has_prefix(desc->name, "encrypt.")) { |
d1402b50 HR |
5180 | error_setg(errp, |
5181 | "Changing the encryption parameters is not supported"); | |
f66afbe2 | 5182 | return -ENOTSUP; |
8a17b83c HR |
5183 | } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) { |
5184 | cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, | |
1bd0e2d1 CL |
5185 | cluster_size); |
5186 | if (cluster_size != s->cluster_size) { | |
d1402b50 | 5187 | error_setg(errp, "Changing the cluster size is not supported"); |
9296b3ed HR |
5188 | return -ENOTSUP; |
5189 | } | |
8a17b83c HR |
5190 | } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) { |
5191 | lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS, | |
1bd0e2d1 | 5192 | lazy_refcounts); |
06d05fa7 | 5193 | } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) { |
61ce55fc HR |
5194 | refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS, |
5195 | refcount_bits); | |
5196 | ||
5197 | if (refcount_bits <= 0 || refcount_bits > 64 || | |
5198 | !is_power_of_2(refcount_bits)) | |
5199 | { | |
d1402b50 HR |
5200 | error_setg(errp, "Refcount width must be a power of two and " |
5201 | "may not exceed 64 bits"); | |
61ce55fc HR |
5202 | return -EINVAL; |
5203 | } | |
9b890bdc KW |
5204 | } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) { |
5205 | data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE); | |
5206 | if (data_file && !has_data_file(bs)) { | |
5207 | error_setg(errp, "data-file can only be set for images that " | |
5208 | "use an external data file"); | |
5209 | return -EINVAL; | |
5210 | } | |
6c3944dc KW |
5211 | } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE_RAW)) { |
5212 | data_file_raw = qemu_opt_get_bool(opts, BLOCK_OPT_DATA_FILE_RAW, | |
5213 | data_file_raw); | |
5214 | if (data_file_raw && !data_file_is_raw(bs)) { | |
5215 | error_setg(errp, "data-file-raw cannot be set on existing " | |
5216 | "images"); | |
5217 | return -EINVAL; | |
5218 | } | |
9296b3ed | 5219 | } else { |
164e0f89 | 5220 | /* if this point is reached, this probably means a new option was |
9296b3ed | 5221 | * added without having it covered here */ |
164e0f89 | 5222 | abort(); |
9296b3ed | 5223 | } |
1bd0e2d1 CL |
5224 | |
5225 | desc++; | |
9296b3ed HR |
5226 | } |
5227 | ||
c293a809 HR |
5228 | helper_cb_info = (Qcow2AmendHelperCBInfo){ |
5229 | .original_status_cb = status_cb, | |
5230 | .original_cb_opaque = cb_opaque, | |
722efb0c | 5231 | .total_operations = (new_version != old_version) |
61ce55fc | 5232 | + (s->refcount_bits != refcount_bits) |
c293a809 HR |
5233 | }; |
5234 | ||
1038bbb8 HR |
5235 | /* Upgrade first (some features may require compat=1.1) */ |
5236 | if (new_version > old_version) { | |
722efb0c HR |
5237 | helper_cb_info.current_operation = QCOW2_UPGRADING; |
5238 | ret = qcow2_upgrade(bs, new_version, &qcow2_amend_helper_cb, | |
5239 | &helper_cb_info, errp); | |
1038bbb8 | 5240 | if (ret < 0) { |
1038bbb8 | 5241 | return ret; |
9296b3ed HR |
5242 | } |
5243 | } | |
5244 | ||
61ce55fc HR |
5245 | if (s->refcount_bits != refcount_bits) { |
5246 | int refcount_order = ctz32(refcount_bits); | |
61ce55fc HR |
5247 | |
5248 | if (new_version < 3 && refcount_bits != 16) { | |
d1402b50 HR |
5249 | error_setg(errp, "Refcount widths other than 16 bits require " |
5250 | "compatibility level 1.1 or above (use compat=1.1 or " | |
5251 | "greater)"); | |
61ce55fc HR |
5252 | return -EINVAL; |
5253 | } | |
5254 | ||
5255 | helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER; | |
5256 | ret = qcow2_change_refcount_order(bs, refcount_order, | |
5257 | &qcow2_amend_helper_cb, | |
d1402b50 | 5258 | &helper_cb_info, errp); |
61ce55fc | 5259 | if (ret < 0) { |
61ce55fc HR |
5260 | return ret; |
5261 | } | |
5262 | } | |
5263 | ||
6c3944dc KW |
5264 | /* data-file-raw blocks backing files, so clear it first if requested */ |
5265 | if (data_file_raw) { | |
5266 | s->autoclear_features |= QCOW2_AUTOCLEAR_DATA_FILE_RAW; | |
5267 | } else { | |
5268 | s->autoclear_features &= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW; | |
5269 | } | |
5270 | ||
9b890bdc KW |
5271 | if (data_file) { |
5272 | g_free(s->image_data_file); | |
5273 | s->image_data_file = *data_file ? g_strdup(data_file) : NULL; | |
5274 | } | |
5275 | ||
5276 | ret = qcow2_update_header(bs); | |
5277 | if (ret < 0) { | |
5278 | error_setg_errno(errp, -ret, "Failed to update the image header"); | |
5279 | return ret; | |
5280 | } | |
5281 | ||
9296b3ed | 5282 | if (backing_file || backing_format) { |
e4603fe1 KW |
5283 | ret = qcow2_change_backing_file(bs, |
5284 | backing_file ?: s->image_backing_file, | |
5285 | backing_format ?: s->image_backing_format); | |
9296b3ed | 5286 | if (ret < 0) { |
d1402b50 | 5287 | error_setg_errno(errp, -ret, "Failed to change the backing file"); |
9296b3ed HR |
5288 | return ret; |
5289 | } | |
5290 | } | |
5291 | ||
5292 | if (s->use_lazy_refcounts != lazy_refcounts) { | |
5293 | if (lazy_refcounts) { | |
1038bbb8 | 5294 | if (new_version < 3) { |
d1402b50 HR |
5295 | error_setg(errp, "Lazy refcounts only supported with " |
5296 | "compatibility level 1.1 and above (use compat=1.1 " | |
5297 | "or greater)"); | |
9296b3ed HR |
5298 | return -EINVAL; |
5299 | } | |
5300 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; | |
5301 | ret = qcow2_update_header(bs); | |
5302 | if (ret < 0) { | |
5303 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; | |
d1402b50 | 5304 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
9296b3ed HR |
5305 | return ret; |
5306 | } | |
5307 | s->use_lazy_refcounts = true; | |
5308 | } else { | |
5309 | /* make image clean first */ | |
5310 | ret = qcow2_mark_clean(bs); | |
5311 | if (ret < 0) { | |
d1402b50 | 5312 | error_setg_errno(errp, -ret, "Failed to make the image clean"); |
9296b3ed HR |
5313 | return ret; |
5314 | } | |
5315 | /* now disallow lazy refcounts */ | |
5316 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; | |
5317 | ret = qcow2_update_header(bs); | |
5318 | if (ret < 0) { | |
5319 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; | |
d1402b50 | 5320 | error_setg_errno(errp, -ret, "Failed to update the image header"); |
9296b3ed HR |
5321 | return ret; |
5322 | } | |
5323 | s->use_lazy_refcounts = false; | |
5324 | } | |
5325 | } | |
5326 | ||
5327 | if (new_size) { | |
d861ab3a KW |
5328 | BlockBackend *blk = blk_new(bdrv_get_aio_context(bs), |
5329 | BLK_PERM_RESIZE, BLK_PERM_ALL); | |
d1402b50 | 5330 | ret = blk_insert_bs(blk, bs, errp); |
d7086422 | 5331 | if (ret < 0) { |
d7086422 KW |
5332 | blk_unref(blk); |
5333 | return ret; | |
5334 | } | |
5335 | ||
e8d04f92 HR |
5336 | /* |
5337 | * Amending image options should ensure that the image has | |
5338 | * exactly the given new values, so pass exact=true here. | |
5339 | */ | |
5340 | ret = blk_truncate(blk, new_size, true, PREALLOC_MODE_OFF, errp); | |
70b27f36 | 5341 | blk_unref(blk); |
9296b3ed HR |
5342 | if (ret < 0) { |
5343 | return ret; | |
5344 | } | |
5345 | } | |
5346 | ||
1038bbb8 HR |
5347 | /* Downgrade last (so unsupported features can be removed before) */ |
5348 | if (new_version < old_version) { | |
c293a809 HR |
5349 | helper_cb_info.current_operation = QCOW2_DOWNGRADING; |
5350 | ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb, | |
d1402b50 | 5351 | &helper_cb_info, errp); |
1038bbb8 HR |
5352 | if (ret < 0) { |
5353 | return ret; | |
5354 | } | |
5355 | } | |
5356 | ||
9296b3ed HR |
5357 | return 0; |
5358 | } | |
5359 | ||
85186ebd HR |
5360 | /* |
5361 | * If offset or size are negative, respectively, they will not be included in | |
5362 | * the BLOCK_IMAGE_CORRUPTED event emitted. | |
5363 | * fatal will be ignored for read-only BDS; corruptions found there will always | |
5364 | * be considered non-fatal. | |
5365 | */ | |
5366 | void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset, | |
5367 | int64_t size, const char *message_format, ...) | |
5368 | { | |
ff99129a | 5369 | BDRVQcow2State *s = bs->opaque; |
dc881b44 | 5370 | const char *node_name; |
85186ebd HR |
5371 | char *message; |
5372 | va_list ap; | |
5373 | ||
ddf3b47e | 5374 | fatal = fatal && bdrv_is_writable(bs); |
85186ebd HR |
5375 | |
5376 | if (s->signaled_corruption && | |
5377 | (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT))) | |
5378 | { | |
5379 | return; | |
5380 | } | |
5381 | ||
5382 | va_start(ap, message_format); | |
5383 | message = g_strdup_vprintf(message_format, ap); | |
5384 | va_end(ap); | |
5385 | ||
5386 | if (fatal) { | |
5387 | fprintf(stderr, "qcow2: Marking image as corrupt: %s; further " | |
5388 | "corruption events will be suppressed\n", message); | |
5389 | } else { | |
5390 | fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal " | |
5391 | "corruption events will be suppressed\n", message); | |
5392 | } | |
5393 | ||
dc881b44 AG |
5394 | node_name = bdrv_get_node_name(bs); |
5395 | qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs), | |
5396 | *node_name != '\0', node_name, | |
5397 | message, offset >= 0, offset, | |
5398 | size >= 0, size, | |
3ab72385 | 5399 | fatal); |
85186ebd HR |
5400 | g_free(message); |
5401 | ||
5402 | if (fatal) { | |
5403 | qcow2_mark_corrupt(bs); | |
5404 | bs->drv = NULL; /* make BDS unusable */ | |
5405 | } | |
5406 | ||
5407 | s->signaled_corruption = true; | |
5408 | } | |
5409 | ||
1bd0e2d1 CL |
5410 | static QemuOptsList qcow2_create_opts = { |
5411 | .name = "qcow2-create-opts", | |
5412 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head), | |
5413 | .desc = { | |
5414 | { | |
5415 | .name = BLOCK_OPT_SIZE, | |
5416 | .type = QEMU_OPT_SIZE, | |
5417 | .help = "Virtual disk size" | |
5418 | }, | |
5419 | { | |
5420 | .name = BLOCK_OPT_COMPAT_LEVEL, | |
5421 | .type = QEMU_OPT_STRING, | |
f7077c98 | 5422 | .help = "Compatibility level (v2 [0.10] or v3 [1.1])" |
1bd0e2d1 CL |
5423 | }, |
5424 | { | |
5425 | .name = BLOCK_OPT_BACKING_FILE, | |
5426 | .type = QEMU_OPT_STRING, | |
5427 | .help = "File name of a base image" | |
5428 | }, | |
5429 | { | |
5430 | .name = BLOCK_OPT_BACKING_FMT, | |
5431 | .type = QEMU_OPT_STRING, | |
5432 | .help = "Image format of the base image" | |
5433 | }, | |
9b890bdc KW |
5434 | { |
5435 | .name = BLOCK_OPT_DATA_FILE, | |
5436 | .type = QEMU_OPT_STRING, | |
5437 | .help = "File name of an external data file" | |
5438 | }, | |
6c3944dc KW |
5439 | { |
5440 | .name = BLOCK_OPT_DATA_FILE_RAW, | |
5441 | .type = QEMU_OPT_BOOL, | |
5442 | .help = "The external data file must stay valid as a raw image" | |
5443 | }, | |
1bd0e2d1 CL |
5444 | { |
5445 | .name = BLOCK_OPT_ENCRYPT, | |
5446 | .type = QEMU_OPT_BOOL, | |
0cb8d47b DB |
5447 | .help = "Encrypt the image with format 'aes'. (Deprecated " |
5448 | "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)", | |
5449 | }, | |
5450 | { | |
5451 | .name = BLOCK_OPT_ENCRYPT_FORMAT, | |
5452 | .type = QEMU_OPT_STRING, | |
4652b8f3 | 5453 | .help = "Encrypt the image, format choices: 'aes', 'luks'", |
1bd0e2d1 | 5454 | }, |
4652b8f3 DB |
5455 | BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.", |
5456 | "ID of secret providing qcow AES key or LUKS passphrase"), | |
5457 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."), | |
5458 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."), | |
5459 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."), | |
5460 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."), | |
5461 | BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."), | |
5462 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."), | |
1bd0e2d1 CL |
5463 | { |
5464 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
5465 | .type = QEMU_OPT_SIZE, | |
5466 | .help = "qcow2 cluster size", | |
5467 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE) | |
5468 | }, | |
5469 | { | |
5470 | .name = BLOCK_OPT_PREALLOC, | |
5471 | .type = QEMU_OPT_STRING, | |
0e4271b7 HT |
5472 | .help = "Preallocation mode (allowed values: off, metadata, " |
5473 | "falloc, full)" | |
1bd0e2d1 CL |
5474 | }, |
5475 | { | |
5476 | .name = BLOCK_OPT_LAZY_REFCOUNTS, | |
5477 | .type = QEMU_OPT_BOOL, | |
5478 | .help = "Postpone refcount updates", | |
5479 | .def_value_str = "off" | |
5480 | }, | |
06d05fa7 HR |
5481 | { |
5482 | .name = BLOCK_OPT_REFCOUNT_BITS, | |
5483 | .type = QEMU_OPT_NUMBER, | |
5484 | .help = "Width of a reference count entry in bits", | |
5485 | .def_value_str = "16" | |
5486 | }, | |
1bd0e2d1 CL |
5487 | { /* end of list */ } |
5488 | } | |
20d97356 BS |
5489 | }; |
5490 | ||
2654267c HR |
5491 | static const char *const qcow2_strong_runtime_opts[] = { |
5492 | "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET, | |
5493 | ||
5494 | NULL | |
5495 | }; | |
5496 | ||
5f535a94 | 5497 | BlockDriver bdrv_qcow2 = { |
7c80ab3f | 5498 | .format_name = "qcow2", |
ff99129a | 5499 | .instance_size = sizeof(BDRVQcow2State), |
7c80ab3f JS |
5500 | .bdrv_probe = qcow2_probe, |
5501 | .bdrv_open = qcow2_open, | |
5502 | .bdrv_close = qcow2_close, | |
21d82ac9 | 5503 | .bdrv_reopen_prepare = qcow2_reopen_prepare, |
5b0959a7 | 5504 | .bdrv_reopen_commit = qcow2_reopen_commit, |
65eb7c85 | 5505 | .bdrv_reopen_commit_post = qcow2_reopen_commit_post, |
5b0959a7 | 5506 | .bdrv_reopen_abort = qcow2_reopen_abort, |
5365f44d | 5507 | .bdrv_join_options = qcow2_join_options, |
862f215f | 5508 | .bdrv_child_perm = bdrv_format_default_perms, |
efc75e2a | 5509 | .bdrv_co_create_opts = qcow2_co_create_opts, |
b0292b85 | 5510 | .bdrv_co_create = qcow2_co_create, |
38841dcd | 5511 | .bdrv_has_zero_init = qcow2_has_zero_init, |
1dcaf527 | 5512 | .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1, |
a320fb04 | 5513 | .bdrv_co_block_status = qcow2_co_block_status, |
7c80ab3f | 5514 | |
df893d25 | 5515 | .bdrv_co_preadv_part = qcow2_co_preadv_part, |
5396234b | 5516 | .bdrv_co_pwritev_part = qcow2_co_pwritev_part, |
eb489bb1 | 5517 | .bdrv_co_flush_to_os = qcow2_co_flush_to_os, |
419b19d9 | 5518 | |
5544b59f | 5519 | .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes, |
82e8a788 | 5520 | .bdrv_co_pdiscard = qcow2_co_pdiscard, |
fd9fcd37 FZ |
5521 | .bdrv_co_copy_range_from = qcow2_co_copy_range_from, |
5522 | .bdrv_co_copy_range_to = qcow2_co_copy_range_to, | |
061ca8a3 | 5523 | .bdrv_co_truncate = qcow2_co_truncate, |
5396234b | 5524 | .bdrv_co_pwritev_compressed_part = qcow2_co_pwritev_compressed_part, |
491d27e2 | 5525 | .bdrv_make_empty = qcow2_make_empty, |
20d97356 BS |
5526 | |
5527 | .bdrv_snapshot_create = qcow2_snapshot_create, | |
5528 | .bdrv_snapshot_goto = qcow2_snapshot_goto, | |
5529 | .bdrv_snapshot_delete = qcow2_snapshot_delete, | |
5530 | .bdrv_snapshot_list = qcow2_snapshot_list, | |
1bd0e2d1 | 5531 | .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, |
c501c352 | 5532 | .bdrv_measure = qcow2_measure, |
1bd0e2d1 | 5533 | .bdrv_get_info = qcow2_get_info, |
37764dfb | 5534 | .bdrv_get_specific_info = qcow2_get_specific_info, |
20d97356 | 5535 | |
7c80ab3f JS |
5536 | .bdrv_save_vmstate = qcow2_save_vmstate, |
5537 | .bdrv_load_vmstate = qcow2_load_vmstate, | |
20d97356 | 5538 | |
8ee79e70 | 5539 | .supports_backing = true, |
20d97356 BS |
5540 | .bdrv_change_backing_file = qcow2_change_backing_file, |
5541 | ||
d34682cd | 5542 | .bdrv_refresh_limits = qcow2_refresh_limits, |
2b148f39 | 5543 | .bdrv_co_invalidate_cache = qcow2_co_invalidate_cache, |
ec6d8912 | 5544 | .bdrv_inactivate = qcow2_inactivate, |
06d9260f | 5545 | |
1bd0e2d1 | 5546 | .create_opts = &qcow2_create_opts, |
2654267c | 5547 | .strong_runtime_opts = qcow2_strong_runtime_opts, |
8a2ce0bc | 5548 | .mutable_opts = mutable_opts, |
2fd61638 | 5549 | .bdrv_co_check = qcow2_co_check, |
c282e1fd | 5550 | .bdrv_amend_options = qcow2_amend_options, |
279621c0 AG |
5551 | |
5552 | .bdrv_detach_aio_context = qcow2_detach_aio_context, | |
5553 | .bdrv_attach_aio_context = qcow2_attach_aio_context, | |
1b6b0562 | 5554 | |
d2c3080e VSO |
5555 | .bdrv_co_can_store_new_dirty_bitmap = qcow2_co_can_store_new_dirty_bitmap, |
5556 | .bdrv_co_remove_persistent_dirty_bitmap = | |
5557 | qcow2_co_remove_persistent_dirty_bitmap, | |
20d97356 BS |
5558 | }; |
5559 | ||
5efa9d5a AL |
5560 | static void bdrv_qcow2_init(void) |
5561 | { | |
5562 | bdrv_register(&bdrv_qcow2); | |
5563 | } | |
5564 | ||
5565 | block_init(bdrv_qcow2_init); |