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