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