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