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