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