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