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