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