]>
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 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
737e150e | 25 | #include "block/block_int.h" |
1de7afc9 | 26 | #include "qemu/module.h" |
585f8587 | 27 | #include <zlib.h> |
f7d0fe02 | 28 | #include "block/qcow2.h" |
1de7afc9 | 29 | #include "qemu/error-report.h" |
7b1b5d19 | 30 | #include "qapi/qmp/qerror.h" |
acdfb480 | 31 | #include "qapi/qmp/qbool.h" |
ffeaac9b | 32 | #include "qapi/util.h" |
85186ebd HR |
33 | #include "qapi/qmp/types.h" |
34 | #include "qapi-event.h" | |
3cce16f4 | 35 | #include "trace.h" |
1bd0e2d1 | 36 | #include "qemu/option_int.h" |
585f8587 FB |
37 | |
38 | /* | |
39 | Differences with QCOW: | |
40 | ||
41 | - Support for multiple incremental snapshots. | |
42 | - Memory management by reference counts. | |
43 | - Clusters which have a reference count of one have the bit | |
44 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 45 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
46 | in the cluster offsets. |
47 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 48 | snapshots. |
585f8587 FB |
49 | - If a backing store is used, the cluster size is not constrained |
50 | (could be backported to QCOW). | |
51 | - L2 tables have always a size of one cluster. | |
52 | */ | |
53 | ||
9b80ddf3 AL |
54 | |
55 | typedef struct { | |
56 | uint32_t magic; | |
57 | uint32_t len; | |
c4217f64 | 58 | } QEMU_PACKED QCowExtension; |
21d82ac9 | 59 | |
7c80ab3f JS |
60 | #define QCOW2_EXT_MAGIC_END 0 |
61 | #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA | |
cfcc4c62 | 62 | #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 |
9b80ddf3 | 63 | |
7c80ab3f | 64 | static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) |
585f8587 FB |
65 | { |
66 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 67 | |
585f8587 FB |
68 | if (buf_size >= sizeof(QCowHeader) && |
69 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
6744cbab | 70 | be32_to_cpu(cow_header->version) >= 2) |
585f8587 FB |
71 | return 100; |
72 | else | |
73 | return 0; | |
74 | } | |
75 | ||
9b80ddf3 AL |
76 | |
77 | /* | |
78 | * read qcow2 extension and fill bs | |
79 | * start reading from start_offset | |
80 | * finish reading upon magic of value 0 or when end_offset reached | |
81 | * unknown magic is skipped (future extension this version knows nothing about) | |
82 | * return 0 upon success, non-0 otherwise | |
83 | */ | |
7c80ab3f | 84 | static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
3ef6c40a HR |
85 | uint64_t end_offset, void **p_feature_table, |
86 | Error **errp) | |
9b80ddf3 | 87 | { |
ff99129a | 88 | BDRVQcow2State *s = bs->opaque; |
9b80ddf3 AL |
89 | QCowExtension ext; |
90 | uint64_t offset; | |
75bab85c | 91 | int ret; |
9b80ddf3 AL |
92 | |
93 | #ifdef DEBUG_EXT | |
7c80ab3f | 94 | printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); |
9b80ddf3 AL |
95 | #endif |
96 | offset = start_offset; | |
97 | while (offset < end_offset) { | |
98 | ||
99 | #ifdef DEBUG_EXT | |
100 | /* Sanity check */ | |
101 | if (offset > s->cluster_size) | |
7c80ab3f | 102 | printf("qcow2_read_extension: suspicious offset %lu\n", offset); |
9b80ddf3 | 103 | |
9b2260cb | 104 | printf("attempting to read extended header in offset %lu\n", offset); |
9b80ddf3 AL |
105 | #endif |
106 | ||
9a4f4c31 | 107 | ret = bdrv_pread(bs->file->bs, offset, &ext, sizeof(ext)); |
3ef6c40a HR |
108 | if (ret < 0) { |
109 | error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: " | |
110 | "pread fail from offset %" PRIu64, offset); | |
9b80ddf3 AL |
111 | return 1; |
112 | } | |
113 | be32_to_cpus(&ext.magic); | |
114 | be32_to_cpus(&ext.len); | |
115 | offset += sizeof(ext); | |
116 | #ifdef DEBUG_EXT | |
117 | printf("ext.magic = 0x%x\n", ext.magic); | |
118 | #endif | |
2ebafc85 | 119 | if (offset > end_offset || ext.len > end_offset - offset) { |
3ef6c40a | 120 | error_setg(errp, "Header extension too large"); |
64ca6aee KW |
121 | return -EINVAL; |
122 | } | |
123 | ||
9b80ddf3 | 124 | switch (ext.magic) { |
7c80ab3f | 125 | case QCOW2_EXT_MAGIC_END: |
9b80ddf3 | 126 | return 0; |
f965509c | 127 | |
7c80ab3f | 128 | case QCOW2_EXT_MAGIC_BACKING_FORMAT: |
f965509c | 129 | if (ext.len >= sizeof(bs->backing_format)) { |
521b2b5d HR |
130 | error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32 |
131 | " too large (>=%zu)", ext.len, | |
132 | sizeof(bs->backing_format)); | |
f965509c AL |
133 | return 2; |
134 | } | |
9a4f4c31 | 135 | ret = bdrv_pread(bs->file->bs, offset, bs->backing_format, ext.len); |
3ef6c40a HR |
136 | if (ret < 0) { |
137 | error_setg_errno(errp, -ret, "ERROR: ext_backing_format: " | |
138 | "Could not read format name"); | |
f965509c | 139 | return 3; |
3ef6c40a | 140 | } |
f965509c | 141 | bs->backing_format[ext.len] = '\0'; |
e4603fe1 | 142 | s->image_backing_format = g_strdup(bs->backing_format); |
f965509c AL |
143 | #ifdef DEBUG_EXT |
144 | printf("Qcow2: Got format extension %s\n", bs->backing_format); | |
145 | #endif | |
f965509c AL |
146 | break; |
147 | ||
cfcc4c62 KW |
148 | case QCOW2_EXT_MAGIC_FEATURE_TABLE: |
149 | if (p_feature_table != NULL) { | |
150 | void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); | |
9a4f4c31 | 151 | ret = bdrv_pread(bs->file->bs, offset , feature_table, ext.len); |
cfcc4c62 | 152 | if (ret < 0) { |
3ef6c40a HR |
153 | error_setg_errno(errp, -ret, "ERROR: ext_feature_table: " |
154 | "Could not read table"); | |
cfcc4c62 KW |
155 | return ret; |
156 | } | |
157 | ||
158 | *p_feature_table = feature_table; | |
159 | } | |
160 | break; | |
161 | ||
9b80ddf3 | 162 | default: |
75bab85c KW |
163 | /* unknown magic - save it in case we need to rewrite the header */ |
164 | { | |
165 | Qcow2UnknownHeaderExtension *uext; | |
166 | ||
167 | uext = g_malloc0(sizeof(*uext) + ext.len); | |
168 | uext->magic = ext.magic; | |
169 | uext->len = ext.len; | |
170 | QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); | |
171 | ||
9a4f4c31 | 172 | ret = bdrv_pread(bs->file->bs, offset , uext->data, uext->len); |
75bab85c | 173 | if (ret < 0) { |
3ef6c40a HR |
174 | error_setg_errno(errp, -ret, "ERROR: unknown extension: " |
175 | "Could not read data"); | |
75bab85c KW |
176 | return ret; |
177 | } | |
75bab85c | 178 | } |
9b80ddf3 AL |
179 | break; |
180 | } | |
fd29b4bb KW |
181 | |
182 | offset += ((ext.len + 7) & ~7); | |
9b80ddf3 AL |
183 | } |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
75bab85c KW |
188 | static void cleanup_unknown_header_ext(BlockDriverState *bs) |
189 | { | |
ff99129a | 190 | BDRVQcow2State *s = bs->opaque; |
75bab85c KW |
191 | Qcow2UnknownHeaderExtension *uext, *next; |
192 | ||
193 | QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { | |
194 | QLIST_REMOVE(uext, next); | |
195 | g_free(uext); | |
196 | } | |
197 | } | |
9b80ddf3 | 198 | |
3ef6c40a HR |
199 | static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs, |
200 | Error **errp, const char *fmt, ...) | |
6744cbab KW |
201 | { |
202 | char msg[64]; | |
203 | va_list ap; | |
204 | ||
205 | va_start(ap, fmt); | |
206 | vsnprintf(msg, sizeof(msg), fmt, ap); | |
207 | va_end(ap); | |
208 | ||
c6bd8c70 MA |
209 | error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, |
210 | bdrv_get_device_or_node_name(bs), "qcow2", msg); | |
6744cbab KW |
211 | } |
212 | ||
cfcc4c62 | 213 | static void report_unsupported_feature(BlockDriverState *bs, |
3ef6c40a | 214 | Error **errp, Qcow2Feature *table, uint64_t mask) |
cfcc4c62 | 215 | { |
12ac6d3d KW |
216 | char *features = g_strdup(""); |
217 | char *old; | |
218 | ||
cfcc4c62 KW |
219 | while (table && table->name[0] != '\0') { |
220 | if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { | |
12ac6d3d KW |
221 | if (mask & (1ULL << table->bit)) { |
222 | old = features; | |
223 | features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "", | |
224 | table->name); | |
225 | g_free(old); | |
226 | mask &= ~(1ULL << table->bit); | |
cfcc4c62 KW |
227 | } |
228 | } | |
229 | table++; | |
230 | } | |
231 | ||
232 | if (mask) { | |
12ac6d3d KW |
233 | old = features; |
234 | features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64, | |
235 | old, *old ? ", " : "", mask); | |
236 | g_free(old); | |
cfcc4c62 | 237 | } |
12ac6d3d KW |
238 | |
239 | report_unsupported(bs, errp, "%s", features); | |
240 | g_free(features); | |
cfcc4c62 KW |
241 | } |
242 | ||
bfe8043e SH |
243 | /* |
244 | * Sets the dirty bit and flushes afterwards if necessary. | |
245 | * | |
246 | * The incompatible_features bit is only set if the image file header was | |
247 | * updated successfully. Therefore it is not required to check the return | |
248 | * value of this function. | |
249 | */ | |
280d3735 | 250 | int qcow2_mark_dirty(BlockDriverState *bs) |
bfe8043e | 251 | { |
ff99129a | 252 | BDRVQcow2State *s = bs->opaque; |
bfe8043e SH |
253 | uint64_t val; |
254 | int ret; | |
255 | ||
256 | assert(s->qcow_version >= 3); | |
257 | ||
258 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
259 | return 0; /* already dirty */ | |
260 | } | |
261 | ||
262 | val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); | |
9a4f4c31 | 263 | ret = bdrv_pwrite(bs->file->bs, offsetof(QCowHeader, incompatible_features), |
bfe8043e SH |
264 | &val, sizeof(val)); |
265 | if (ret < 0) { | |
266 | return ret; | |
267 | } | |
9a4f4c31 | 268 | ret = bdrv_flush(bs->file->bs); |
bfe8043e SH |
269 | if (ret < 0) { |
270 | return ret; | |
271 | } | |
272 | ||
273 | /* Only treat image as dirty if the header was updated successfully */ | |
274 | s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; | |
275 | return 0; | |
276 | } | |
277 | ||
c61d0004 SH |
278 | /* |
279 | * Clears the dirty bit and flushes before if necessary. Only call this | |
280 | * function when there are no pending requests, it does not guard against | |
281 | * concurrent requests dirtying the image. | |
282 | */ | |
283 | static int qcow2_mark_clean(BlockDriverState *bs) | |
284 | { | |
ff99129a | 285 | BDRVQcow2State *s = bs->opaque; |
c61d0004 SH |
286 | |
287 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
4c2e5f8f KW |
288 | int ret; |
289 | ||
290 | s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; | |
291 | ||
292 | ret = bdrv_flush(bs); | |
c61d0004 SH |
293 | if (ret < 0) { |
294 | return ret; | |
295 | } | |
296 | ||
c61d0004 SH |
297 | return qcow2_update_header(bs); |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
69c98726 HR |
302 | /* |
303 | * Marks the image as corrupt. | |
304 | */ | |
305 | int qcow2_mark_corrupt(BlockDriverState *bs) | |
306 | { | |
ff99129a | 307 | BDRVQcow2State *s = bs->opaque; |
69c98726 HR |
308 | |
309 | s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT; | |
310 | return qcow2_update_header(bs); | |
311 | } | |
312 | ||
313 | /* | |
314 | * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes | |
315 | * before if necessary. | |
316 | */ | |
317 | int qcow2_mark_consistent(BlockDriverState *bs) | |
318 | { | |
ff99129a | 319 | BDRVQcow2State *s = bs->opaque; |
69c98726 HR |
320 | |
321 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { | |
322 | int ret = bdrv_flush(bs); | |
323 | if (ret < 0) { | |
324 | return ret; | |
325 | } | |
326 | ||
327 | s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT; | |
328 | return qcow2_update_header(bs); | |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
acbe5982 SH |
333 | static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, |
334 | BdrvCheckMode fix) | |
335 | { | |
336 | int ret = qcow2_check_refcounts(bs, result, fix); | |
337 | if (ret < 0) { | |
338 | return ret; | |
339 | } | |
340 | ||
341 | if (fix && result->check_errors == 0 && result->corruptions == 0) { | |
24530f3e HR |
342 | ret = qcow2_mark_clean(bs); |
343 | if (ret < 0) { | |
344 | return ret; | |
345 | } | |
346 | return qcow2_mark_consistent(bs); | |
acbe5982 SH |
347 | } |
348 | return ret; | |
349 | } | |
350 | ||
8c7de283 KW |
351 | static int validate_table_offset(BlockDriverState *bs, uint64_t offset, |
352 | uint64_t entries, size_t entry_len) | |
353 | { | |
ff99129a | 354 | BDRVQcow2State *s = bs->opaque; |
8c7de283 KW |
355 | uint64_t size; |
356 | ||
357 | /* Use signed INT64_MAX as the maximum even for uint64_t header fields, | |
358 | * because values will be passed to qemu functions taking int64_t. */ | |
359 | if (entries > INT64_MAX / entry_len) { | |
360 | return -EINVAL; | |
361 | } | |
362 | ||
363 | size = entries * entry_len; | |
364 | ||
365 | if (INT64_MAX - size < offset) { | |
366 | return -EINVAL; | |
367 | } | |
368 | ||
369 | /* Tables must be cluster aligned */ | |
370 | if (offset & (s->cluster_size - 1)) { | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
74c4510a KW |
377 | static QemuOptsList qcow2_runtime_opts = { |
378 | .name = "qcow2", | |
379 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), | |
380 | .desc = { | |
381 | { | |
64aa99d3 | 382 | .name = QCOW2_OPT_LAZY_REFCOUNTS, |
74c4510a KW |
383 | .type = QEMU_OPT_BOOL, |
384 | .help = "Postpone refcount updates", | |
385 | }, | |
67af674e KW |
386 | { |
387 | .name = QCOW2_OPT_DISCARD_REQUEST, | |
388 | .type = QEMU_OPT_BOOL, | |
389 | .help = "Pass guest discard requests to the layer below", | |
390 | }, | |
391 | { | |
392 | .name = QCOW2_OPT_DISCARD_SNAPSHOT, | |
393 | .type = QEMU_OPT_BOOL, | |
394 | .help = "Generate discard requests when snapshot related space " | |
395 | "is freed", | |
396 | }, | |
397 | { | |
398 | .name = QCOW2_OPT_DISCARD_OTHER, | |
399 | .type = QEMU_OPT_BOOL, | |
400 | .help = "Generate discard requests when other clusters are freed", | |
401 | }, | |
05de7e86 HR |
402 | { |
403 | .name = QCOW2_OPT_OVERLAP, | |
404 | .type = QEMU_OPT_STRING, | |
405 | .help = "Selects which overlap checks to perform from a range of " | |
406 | "templates (none, constant, cached, all)", | |
407 | }, | |
ee42b5ce HR |
408 | { |
409 | .name = QCOW2_OPT_OVERLAP_TEMPLATE, | |
410 | .type = QEMU_OPT_STRING, | |
411 | .help = "Selects which overlap checks to perform from a range of " | |
412 | "templates (none, constant, cached, all)", | |
413 | }, | |
05de7e86 HR |
414 | { |
415 | .name = QCOW2_OPT_OVERLAP_MAIN_HEADER, | |
416 | .type = QEMU_OPT_BOOL, | |
417 | .help = "Check for unintended writes into the main qcow2 header", | |
418 | }, | |
419 | { | |
420 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L1, | |
421 | .type = QEMU_OPT_BOOL, | |
422 | .help = "Check for unintended writes into the active L1 table", | |
423 | }, | |
424 | { | |
425 | .name = QCOW2_OPT_OVERLAP_ACTIVE_L2, | |
426 | .type = QEMU_OPT_BOOL, | |
427 | .help = "Check for unintended writes into an active L2 table", | |
428 | }, | |
429 | { | |
430 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, | |
431 | .type = QEMU_OPT_BOOL, | |
432 | .help = "Check for unintended writes into the refcount table", | |
433 | }, | |
434 | { | |
435 | .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, | |
436 | .type = QEMU_OPT_BOOL, | |
437 | .help = "Check for unintended writes into a refcount block", | |
438 | }, | |
439 | { | |
440 | .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, | |
441 | .type = QEMU_OPT_BOOL, | |
442 | .help = "Check for unintended writes into the snapshot table", | |
443 | }, | |
444 | { | |
445 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L1, | |
446 | .type = QEMU_OPT_BOOL, | |
447 | .help = "Check for unintended writes into an inactive L1 table", | |
448 | }, | |
449 | { | |
450 | .name = QCOW2_OPT_OVERLAP_INACTIVE_L2, | |
451 | .type = QEMU_OPT_BOOL, | |
452 | .help = "Check for unintended writes into an inactive L2 table", | |
453 | }, | |
6c1c8d5d HR |
454 | { |
455 | .name = QCOW2_OPT_CACHE_SIZE, | |
456 | .type = QEMU_OPT_SIZE, | |
457 | .help = "Maximum combined metadata (L2 tables and refcount blocks) " | |
458 | "cache size", | |
459 | }, | |
460 | { | |
461 | .name = QCOW2_OPT_L2_CACHE_SIZE, | |
462 | .type = QEMU_OPT_SIZE, | |
463 | .help = "Maximum L2 table cache size", | |
464 | }, | |
465 | { | |
466 | .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE, | |
467 | .type = QEMU_OPT_SIZE, | |
468 | .help = "Maximum refcount block cache size", | |
469 | }, | |
279621c0 AG |
470 | { |
471 | .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL, | |
472 | .type = QEMU_OPT_NUMBER, | |
473 | .help = "Clean unused cache entries after this time (in seconds)", | |
474 | }, | |
74c4510a KW |
475 | { /* end of list */ } |
476 | }, | |
477 | }; | |
478 | ||
4092e99d HR |
479 | static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { |
480 | [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER, | |
481 | [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1, | |
482 | [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2, | |
483 | [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, | |
484 | [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, | |
485 | [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, | |
486 | [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1, | |
487 | [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, | |
488 | }; | |
489 | ||
279621c0 AG |
490 | static void cache_clean_timer_cb(void *opaque) |
491 | { | |
492 | BlockDriverState *bs = opaque; | |
ff99129a | 493 | BDRVQcow2State *s = bs->opaque; |
279621c0 AG |
494 | qcow2_cache_clean_unused(bs, s->l2_table_cache); |
495 | qcow2_cache_clean_unused(bs, s->refcount_block_cache); | |
496 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
497 | (int64_t) s->cache_clean_interval * 1000); | |
498 | } | |
499 | ||
500 | static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context) | |
501 | { | |
ff99129a | 502 | BDRVQcow2State *s = bs->opaque; |
279621c0 AG |
503 | if (s->cache_clean_interval > 0) { |
504 | s->cache_clean_timer = aio_timer_new(context, QEMU_CLOCK_VIRTUAL, | |
505 | SCALE_MS, cache_clean_timer_cb, | |
506 | bs); | |
507 | timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + | |
508 | (int64_t) s->cache_clean_interval * 1000); | |
509 | } | |
510 | } | |
511 | ||
512 | static void cache_clean_timer_del(BlockDriverState *bs) | |
513 | { | |
ff99129a | 514 | BDRVQcow2State *s = bs->opaque; |
279621c0 AG |
515 | if (s->cache_clean_timer) { |
516 | timer_del(s->cache_clean_timer); | |
517 | timer_free(s->cache_clean_timer); | |
518 | s->cache_clean_timer = NULL; | |
519 | } | |
520 | } | |
521 | ||
522 | static void qcow2_detach_aio_context(BlockDriverState *bs) | |
523 | { | |
524 | cache_clean_timer_del(bs); | |
525 | } | |
526 | ||
527 | static void qcow2_attach_aio_context(BlockDriverState *bs, | |
528 | AioContext *new_context) | |
529 | { | |
530 | cache_clean_timer_init(bs, new_context); | |
531 | } | |
532 | ||
bc85ef26 HR |
533 | static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts, |
534 | uint64_t *l2_cache_size, | |
6c1c8d5d HR |
535 | uint64_t *refcount_cache_size, Error **errp) |
536 | { | |
ff99129a | 537 | BDRVQcow2State *s = bs->opaque; |
6c1c8d5d HR |
538 | uint64_t combined_cache_size; |
539 | bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set; | |
540 | ||
541 | combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE); | |
542 | l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE); | |
543 | refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
544 | ||
545 | combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0); | |
546 | *l2_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE, 0); | |
547 | *refcount_cache_size = qemu_opt_get_size(opts, | |
548 | QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0); | |
549 | ||
550 | if (combined_cache_size_set) { | |
551 | if (l2_cache_size_set && refcount_cache_size_set) { | |
552 | error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE | |
553 | " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set " | |
554 | "the same time"); | |
555 | return; | |
556 | } else if (*l2_cache_size > combined_cache_size) { | |
557 | error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed " | |
558 | QCOW2_OPT_CACHE_SIZE); | |
559 | return; | |
560 | } else if (*refcount_cache_size > combined_cache_size) { | |
561 | error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed " | |
562 | QCOW2_OPT_CACHE_SIZE); | |
563 | return; | |
564 | } | |
565 | ||
566 | if (l2_cache_size_set) { | |
567 | *refcount_cache_size = combined_cache_size - *l2_cache_size; | |
568 | } else if (refcount_cache_size_set) { | |
569 | *l2_cache_size = combined_cache_size - *refcount_cache_size; | |
570 | } else { | |
571 | *refcount_cache_size = combined_cache_size | |
572 | / (DEFAULT_L2_REFCOUNT_SIZE_RATIO + 1); | |
573 | *l2_cache_size = combined_cache_size - *refcount_cache_size; | |
574 | } | |
575 | } else { | |
576 | if (!l2_cache_size_set && !refcount_cache_size_set) { | |
bc85ef26 HR |
577 | *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE, |
578 | (uint64_t)DEFAULT_L2_CACHE_CLUSTERS | |
579 | * s->cluster_size); | |
6c1c8d5d HR |
580 | *refcount_cache_size = *l2_cache_size |
581 | / DEFAULT_L2_REFCOUNT_SIZE_RATIO; | |
582 | } else if (!l2_cache_size_set) { | |
583 | *l2_cache_size = *refcount_cache_size | |
584 | * DEFAULT_L2_REFCOUNT_SIZE_RATIO; | |
585 | } else if (!refcount_cache_size_set) { | |
586 | *refcount_cache_size = *l2_cache_size | |
587 | / DEFAULT_L2_REFCOUNT_SIZE_RATIO; | |
588 | } | |
589 | } | |
590 | } | |
591 | ||
ee55b173 KW |
592 | typedef struct Qcow2ReopenState { |
593 | Qcow2Cache *l2_table_cache; | |
594 | Qcow2Cache *refcount_block_cache; | |
595 | bool use_lazy_refcounts; | |
596 | int overlap_check; | |
597 | bool discard_passthrough[QCOW2_DISCARD_MAX]; | |
598 | uint64_t cache_clean_interval; | |
599 | } Qcow2ReopenState; | |
600 | ||
601 | static int qcow2_update_options_prepare(BlockDriverState *bs, | |
602 | Qcow2ReopenState *r, | |
603 | QDict *options, int flags, | |
604 | Error **errp) | |
4c75d1a1 KW |
605 | { |
606 | BDRVQcow2State *s = bs->opaque; | |
94edf3fb | 607 | QemuOpts *opts = NULL; |
4c75d1a1 KW |
608 | const char *opt_overlap_check, *opt_overlap_check_template; |
609 | int overlap_check_template = 0; | |
94edf3fb | 610 | uint64_t l2_cache_size, refcount_cache_size; |
4c75d1a1 | 611 | int i; |
94edf3fb | 612 | Error *local_err = NULL; |
4c75d1a1 KW |
613 | int ret; |
614 | ||
94edf3fb KW |
615 | opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort); |
616 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
617 | if (local_err) { | |
618 | error_propagate(errp, local_err); | |
619 | ret = -EINVAL; | |
620 | goto fail; | |
621 | } | |
622 | ||
623 | /* get L2 table/refcount block cache size from command line options */ | |
624 | read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size, | |
625 | &local_err); | |
626 | if (local_err) { | |
627 | error_propagate(errp, local_err); | |
628 | ret = -EINVAL; | |
629 | goto fail; | |
630 | } | |
631 | ||
632 | l2_cache_size /= s->cluster_size; | |
633 | if (l2_cache_size < MIN_L2_CACHE_SIZE) { | |
634 | l2_cache_size = MIN_L2_CACHE_SIZE; | |
635 | } | |
636 | if (l2_cache_size > INT_MAX) { | |
637 | error_setg(errp, "L2 cache size too big"); | |
638 | ret = -EINVAL; | |
639 | goto fail; | |
640 | } | |
641 | ||
642 | refcount_cache_size /= s->cluster_size; | |
643 | if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) { | |
644 | refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE; | |
645 | } | |
646 | if (refcount_cache_size > INT_MAX) { | |
647 | error_setg(errp, "Refcount cache size too big"); | |
648 | ret = -EINVAL; | |
649 | goto fail; | |
650 | } | |
651 | ||
5b0959a7 KW |
652 | /* alloc new L2 table/refcount block cache, flush old one */ |
653 | if (s->l2_table_cache) { | |
654 | ret = qcow2_cache_flush(bs, s->l2_table_cache); | |
655 | if (ret) { | |
656 | error_setg_errno(errp, -ret, "Failed to flush the L2 table cache"); | |
657 | goto fail; | |
658 | } | |
659 | } | |
660 | ||
661 | if (s->refcount_block_cache) { | |
662 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
663 | if (ret) { | |
664 | error_setg_errno(errp, -ret, | |
665 | "Failed to flush the refcount block cache"); | |
666 | goto fail; | |
667 | } | |
668 | } | |
669 | ||
ee55b173 KW |
670 | r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size); |
671 | r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size); | |
672 | if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) { | |
94edf3fb KW |
673 | error_setg(errp, "Could not allocate metadata caches"); |
674 | ret = -ENOMEM; | |
675 | goto fail; | |
676 | } | |
677 | ||
678 | /* New interval for cache cleanup timer */ | |
ee55b173 | 679 | r->cache_clean_interval = |
5b0959a7 KW |
680 | qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL, |
681 | s->cache_clean_interval); | |
ee55b173 | 682 | if (r->cache_clean_interval > UINT_MAX) { |
94edf3fb KW |
683 | error_setg(errp, "Cache clean interval too big"); |
684 | ret = -EINVAL; | |
685 | goto fail; | |
686 | } | |
94edf3fb | 687 | |
5b0959a7 | 688 | /* lazy-refcounts; flush if going from enabled to disabled */ |
ee55b173 | 689 | r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, |
4c75d1a1 | 690 | (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); |
ee55b173 | 691 | if (r->use_lazy_refcounts && s->qcow_version < 3) { |
007dbc39 KW |
692 | error_setg(errp, "Lazy refcounts require a qcow2 image with at least " |
693 | "qemu 1.1 compatibility level"); | |
694 | ret = -EINVAL; | |
695 | goto fail; | |
696 | } | |
4c75d1a1 | 697 | |
5b0959a7 KW |
698 | if (s->use_lazy_refcounts && !r->use_lazy_refcounts) { |
699 | ret = qcow2_mark_clean(bs); | |
700 | if (ret < 0) { | |
701 | error_setg_errno(errp, -ret, "Failed to disable lazy refcounts"); | |
702 | goto fail; | |
703 | } | |
704 | } | |
705 | ||
007dbc39 | 706 | /* Overlap check options */ |
4c75d1a1 KW |
707 | opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP); |
708 | opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE); | |
709 | if (opt_overlap_check_template && opt_overlap_check && | |
710 | strcmp(opt_overlap_check_template, opt_overlap_check)) | |
711 | { | |
712 | error_setg(errp, "Conflicting values for qcow2 options '" | |
713 | QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE | |
714 | "' ('%s')", opt_overlap_check, opt_overlap_check_template); | |
715 | ret = -EINVAL; | |
716 | goto fail; | |
717 | } | |
718 | if (!opt_overlap_check) { | |
719 | opt_overlap_check = opt_overlap_check_template ?: "cached"; | |
720 | } | |
721 | ||
722 | if (!strcmp(opt_overlap_check, "none")) { | |
723 | overlap_check_template = 0; | |
724 | } else if (!strcmp(opt_overlap_check, "constant")) { | |
725 | overlap_check_template = QCOW2_OL_CONSTANT; | |
726 | } else if (!strcmp(opt_overlap_check, "cached")) { | |
727 | overlap_check_template = QCOW2_OL_CACHED; | |
728 | } else if (!strcmp(opt_overlap_check, "all")) { | |
729 | overlap_check_template = QCOW2_OL_ALL; | |
730 | } else { | |
731 | error_setg(errp, "Unsupported value '%s' for qcow2 option " | |
732 | "'overlap-check'. Allowed are any of the following: " | |
733 | "none, constant, cached, all", opt_overlap_check); | |
734 | ret = -EINVAL; | |
735 | goto fail; | |
736 | } | |
737 | ||
ee55b173 | 738 | r->overlap_check = 0; |
4c75d1a1 KW |
739 | for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) { |
740 | /* overlap-check defines a template bitmask, but every flag may be | |
741 | * overwritten through the associated boolean option */ | |
ee55b173 | 742 | r->overlap_check |= |
4c75d1a1 KW |
743 | qemu_opt_get_bool(opts, overlap_bool_option_names[i], |
744 | overlap_check_template & (1 << i)) << i; | |
745 | } | |
746 | ||
ee55b173 KW |
747 | r->discard_passthrough[QCOW2_DISCARD_NEVER] = false; |
748 | r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; | |
749 | r->discard_passthrough[QCOW2_DISCARD_REQUEST] = | |
007dbc39 KW |
750 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, |
751 | flags & BDRV_O_UNMAP); | |
ee55b173 | 752 | r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = |
007dbc39 | 753 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); |
ee55b173 | 754 | r->discard_passthrough[QCOW2_DISCARD_OTHER] = |
007dbc39 KW |
755 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); |
756 | ||
4c75d1a1 KW |
757 | ret = 0; |
758 | fail: | |
94edf3fb KW |
759 | qemu_opts_del(opts); |
760 | opts = NULL; | |
ee55b173 KW |
761 | return ret; |
762 | } | |
763 | ||
764 | static void qcow2_update_options_commit(BlockDriverState *bs, | |
765 | Qcow2ReopenState *r) | |
766 | { | |
767 | BDRVQcow2State *s = bs->opaque; | |
768 | int i; | |
769 | ||
5b0959a7 KW |
770 | if (s->l2_table_cache) { |
771 | qcow2_cache_destroy(bs, s->l2_table_cache); | |
772 | } | |
773 | if (s->refcount_block_cache) { | |
774 | qcow2_cache_destroy(bs, s->refcount_block_cache); | |
775 | } | |
ee55b173 KW |
776 | s->l2_table_cache = r->l2_table_cache; |
777 | s->refcount_block_cache = r->refcount_block_cache; | |
778 | ||
779 | s->overlap_check = r->overlap_check; | |
780 | s->use_lazy_refcounts = r->use_lazy_refcounts; | |
781 | ||
782 | for (i = 0; i < QCOW2_DISCARD_MAX; i++) { | |
783 | s->discard_passthrough[i] = r->discard_passthrough[i]; | |
784 | } | |
785 | ||
5b0959a7 KW |
786 | if (s->cache_clean_interval != r->cache_clean_interval) { |
787 | cache_clean_timer_del(bs); | |
788 | s->cache_clean_interval = r->cache_clean_interval; | |
789 | cache_clean_timer_init(bs, bdrv_get_aio_context(bs)); | |
790 | } | |
ee55b173 KW |
791 | } |
792 | ||
793 | static void qcow2_update_options_abort(BlockDriverState *bs, | |
794 | Qcow2ReopenState *r) | |
795 | { | |
796 | if (r->l2_table_cache) { | |
797 | qcow2_cache_destroy(bs, r->l2_table_cache); | |
798 | } | |
799 | if (r->refcount_block_cache) { | |
800 | qcow2_cache_destroy(bs, r->refcount_block_cache); | |
801 | } | |
802 | } | |
803 | ||
804 | static int qcow2_update_options(BlockDriverState *bs, QDict *options, | |
805 | int flags, Error **errp) | |
806 | { | |
807 | Qcow2ReopenState r = {}; | |
808 | int ret; | |
809 | ||
810 | ret = qcow2_update_options_prepare(bs, &r, options, flags, errp); | |
811 | if (ret >= 0) { | |
812 | qcow2_update_options_commit(bs, &r); | |
813 | } else { | |
814 | qcow2_update_options_abort(bs, &r); | |
815 | } | |
94edf3fb | 816 | |
4c75d1a1 KW |
817 | return ret; |
818 | } | |
819 | ||
015a1036 HR |
820 | static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, |
821 | Error **errp) | |
585f8587 | 822 | { |
ff99129a | 823 | BDRVQcow2State *s = bs->opaque; |
6d33e8e7 KW |
824 | unsigned int len, i; |
825 | int ret = 0; | |
585f8587 | 826 | QCowHeader header; |
74c4510a | 827 | Error *local_err = NULL; |
9b80ddf3 | 828 | uint64_t ext_end; |
2cf7cfa1 | 829 | uint64_t l1_vm_state_index; |
585f8587 | 830 | |
9a4f4c31 | 831 | ret = bdrv_pread(bs->file->bs, 0, &header, sizeof(header)); |
6d85a57e | 832 | if (ret < 0) { |
3ef6c40a | 833 | error_setg_errno(errp, -ret, "Could not read qcow2 header"); |
585f8587 | 834 | goto fail; |
6d85a57e | 835 | } |
585f8587 FB |
836 | be32_to_cpus(&header.magic); |
837 | be32_to_cpus(&header.version); | |
838 | be64_to_cpus(&header.backing_file_offset); | |
839 | be32_to_cpus(&header.backing_file_size); | |
840 | be64_to_cpus(&header.size); | |
841 | be32_to_cpus(&header.cluster_bits); | |
842 | be32_to_cpus(&header.crypt_method); | |
843 | be64_to_cpus(&header.l1_table_offset); | |
844 | be32_to_cpus(&header.l1_size); | |
845 | be64_to_cpus(&header.refcount_table_offset); | |
846 | be32_to_cpus(&header.refcount_table_clusters); | |
847 | be64_to_cpus(&header.snapshots_offset); | |
848 | be32_to_cpus(&header.nb_snapshots); | |
3b46e624 | 849 | |
e8cdcec1 | 850 | if (header.magic != QCOW_MAGIC) { |
3ef6c40a | 851 | error_setg(errp, "Image is not in qcow2 format"); |
76abe407 | 852 | ret = -EINVAL; |
585f8587 | 853 | goto fail; |
6d85a57e | 854 | } |
6744cbab | 855 | if (header.version < 2 || header.version > 3) { |
521b2b5d | 856 | report_unsupported(bs, errp, "QCOW version %" PRIu32, header.version); |
6744cbab KW |
857 | ret = -ENOTSUP; |
858 | goto fail; | |
859 | } | |
860 | ||
861 | s->qcow_version = header.version; | |
862 | ||
24342f2c KW |
863 | /* Initialise cluster size */ |
864 | if (header.cluster_bits < MIN_CLUSTER_BITS || | |
865 | header.cluster_bits > MAX_CLUSTER_BITS) { | |
521b2b5d HR |
866 | error_setg(errp, "Unsupported cluster size: 2^%" PRIu32, |
867 | header.cluster_bits); | |
24342f2c KW |
868 | ret = -EINVAL; |
869 | goto fail; | |
870 | } | |
871 | ||
872 | s->cluster_bits = header.cluster_bits; | |
873 | s->cluster_size = 1 << s->cluster_bits; | |
874 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
875 | ||
6744cbab KW |
876 | /* Initialise version 3 header fields */ |
877 | if (header.version == 2) { | |
878 | header.incompatible_features = 0; | |
879 | header.compatible_features = 0; | |
880 | header.autoclear_features = 0; | |
881 | header.refcount_order = 4; | |
882 | header.header_length = 72; | |
883 | } else { | |
884 | be64_to_cpus(&header.incompatible_features); | |
885 | be64_to_cpus(&header.compatible_features); | |
886 | be64_to_cpus(&header.autoclear_features); | |
887 | be32_to_cpus(&header.refcount_order); | |
888 | be32_to_cpus(&header.header_length); | |
24342f2c KW |
889 | |
890 | if (header.header_length < 104) { | |
891 | error_setg(errp, "qcow2 header too short"); | |
892 | ret = -EINVAL; | |
893 | goto fail; | |
894 | } | |
895 | } | |
896 | ||
897 | if (header.header_length > s->cluster_size) { | |
898 | error_setg(errp, "qcow2 header exceeds cluster size"); | |
899 | ret = -EINVAL; | |
900 | goto fail; | |
6744cbab KW |
901 | } |
902 | ||
903 | if (header.header_length > sizeof(header)) { | |
904 | s->unknown_header_fields_size = header.header_length - sizeof(header); | |
905 | s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); | |
9a4f4c31 | 906 | ret = bdrv_pread(bs->file->bs, sizeof(header), s->unknown_header_fields, |
6744cbab KW |
907 | s->unknown_header_fields_size); |
908 | if (ret < 0) { | |
3ef6c40a HR |
909 | error_setg_errno(errp, -ret, "Could not read unknown qcow2 header " |
910 | "fields"); | |
6744cbab KW |
911 | goto fail; |
912 | } | |
913 | } | |
914 | ||
a1b3955c KW |
915 | if (header.backing_file_offset > s->cluster_size) { |
916 | error_setg(errp, "Invalid backing file offset"); | |
917 | ret = -EINVAL; | |
918 | goto fail; | |
919 | } | |
920 | ||
cfcc4c62 KW |
921 | if (header.backing_file_offset) { |
922 | ext_end = header.backing_file_offset; | |
923 | } else { | |
924 | ext_end = 1 << header.cluster_bits; | |
925 | } | |
926 | ||
6744cbab KW |
927 | /* Handle feature bits */ |
928 | s->incompatible_features = header.incompatible_features; | |
929 | s->compatible_features = header.compatible_features; | |
930 | s->autoclear_features = header.autoclear_features; | |
931 | ||
c61d0004 | 932 | if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { |
cfcc4c62 KW |
933 | void *feature_table = NULL; |
934 | qcow2_read_extensions(bs, header.header_length, ext_end, | |
3ef6c40a HR |
935 | &feature_table, NULL); |
936 | report_unsupported_feature(bs, errp, feature_table, | |
c61d0004 SH |
937 | s->incompatible_features & |
938 | ~QCOW2_INCOMPAT_MASK); | |
6744cbab | 939 | ret = -ENOTSUP; |
c5a33ee9 | 940 | g_free(feature_table); |
6744cbab KW |
941 | goto fail; |
942 | } | |
943 | ||
69c98726 HR |
944 | if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { |
945 | /* Corrupt images may not be written to unless they are being repaired | |
946 | */ | |
947 | if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { | |
3ef6c40a HR |
948 | error_setg(errp, "qcow2: Image is corrupt; cannot be opened " |
949 | "read/write"); | |
69c98726 HR |
950 | ret = -EACCES; |
951 | goto fail; | |
952 | } | |
953 | } | |
954 | ||
6744cbab | 955 | /* Check support for various header values */ |
b72faf9f HR |
956 | if (header.refcount_order > 6) { |
957 | error_setg(errp, "Reference count entry width too large; may not " | |
958 | "exceed 64 bits"); | |
959 | ret = -EINVAL; | |
e8cdcec1 KW |
960 | goto fail; |
961 | } | |
b6481f37 | 962 | s->refcount_order = header.refcount_order; |
346a53df HR |
963 | s->refcount_bits = 1 << s->refcount_order; |
964 | s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1); | |
965 | s->refcount_max += s->refcount_max - 1; | |
6744cbab | 966 | |
6d85a57e | 967 | if (header.crypt_method > QCOW_CRYPT_AES) { |
521b2b5d | 968 | error_setg(errp, "Unsupported encryption method: %" PRIu32, |
3ef6c40a | 969 | header.crypt_method); |
6d85a57e | 970 | ret = -EINVAL; |
585f8587 | 971 | goto fail; |
6d85a57e | 972 | } |
f6fa64f6 DB |
973 | if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) { |
974 | error_setg(errp, "AES cipher not available"); | |
975 | ret = -EINVAL; | |
976 | goto fail; | |
977 | } | |
585f8587 | 978 | s->crypt_method_header = header.crypt_method; |
6d85a57e | 979 | if (s->crypt_method_header) { |
585f8587 | 980 | bs->encrypted = 1; |
6d85a57e | 981 | } |
24342f2c | 982 | |
585f8587 FB |
983 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
984 | s->l2_size = 1 << s->l2_bits; | |
1d13d654 HR |
985 | /* 2^(s->refcount_order - 3) is the refcount width in bytes */ |
986 | s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3); | |
987 | s->refcount_block_size = 1 << s->refcount_block_bits; | |
585f8587 FB |
988 | bs->total_sectors = header.size / 512; |
989 | s->csize_shift = (62 - (s->cluster_bits - 8)); | |
990 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
991 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
5dab2fad | 992 | |
585f8587 | 993 | s->refcount_table_offset = header.refcount_table_offset; |
5fafdf24 | 994 | s->refcount_table_size = |
585f8587 FB |
995 | header.refcount_table_clusters << (s->cluster_bits - 3); |
996 | ||
2b5d5953 | 997 | if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) { |
5dab2fad KW |
998 | error_setg(errp, "Reference count table too large"); |
999 | ret = -EINVAL; | |
1000 | goto fail; | |
1001 | } | |
1002 | ||
8c7de283 KW |
1003 | ret = validate_table_offset(bs, s->refcount_table_offset, |
1004 | s->refcount_table_size, sizeof(uint64_t)); | |
1005 | if (ret < 0) { | |
1006 | error_setg(errp, "Invalid reference count table offset"); | |
1007 | goto fail; | |
1008 | } | |
1009 | ||
ce48f2f4 KW |
1010 | /* Snapshot table offset/length */ |
1011 | if (header.nb_snapshots > QCOW_MAX_SNAPSHOTS) { | |
1012 | error_setg(errp, "Too many snapshots"); | |
1013 | ret = -EINVAL; | |
1014 | goto fail; | |
1015 | } | |
1016 | ||
1017 | ret = validate_table_offset(bs, header.snapshots_offset, | |
1018 | header.nb_snapshots, | |
1019 | sizeof(QCowSnapshotHeader)); | |
1020 | if (ret < 0) { | |
1021 | error_setg(errp, "Invalid snapshot table offset"); | |
1022 | goto fail; | |
1023 | } | |
1024 | ||
585f8587 | 1025 | /* read the level 1 table */ |
87b86e7e | 1026 | if (header.l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) { |
2d51c32c KW |
1027 | error_setg(errp, "Active L1 table too large"); |
1028 | ret = -EFBIG; | |
1029 | goto fail; | |
1030 | } | |
585f8587 | 1031 | s->l1_size = header.l1_size; |
2cf7cfa1 KW |
1032 | |
1033 | l1_vm_state_index = size_to_l1(s, header.size); | |
1034 | if (l1_vm_state_index > INT_MAX) { | |
3ef6c40a | 1035 | error_setg(errp, "Image is too big"); |
2cf7cfa1 KW |
1036 | ret = -EFBIG; |
1037 | goto fail; | |
1038 | } | |
1039 | s->l1_vm_state_index = l1_vm_state_index; | |
1040 | ||
585f8587 FB |
1041 | /* the L1 table must contain at least enough entries to put |
1042 | header.size bytes */ | |
6d85a57e | 1043 | if (s->l1_size < s->l1_vm_state_index) { |
3ef6c40a | 1044 | error_setg(errp, "L1 table is too small"); |
6d85a57e | 1045 | ret = -EINVAL; |
585f8587 | 1046 | goto fail; |
6d85a57e | 1047 | } |
2d51c32c KW |
1048 | |
1049 | ret = validate_table_offset(bs, header.l1_table_offset, | |
1050 | header.l1_size, sizeof(uint64_t)); | |
1051 | if (ret < 0) { | |
1052 | error_setg(errp, "Invalid L1 table offset"); | |
1053 | goto fail; | |
1054 | } | |
585f8587 | 1055 | s->l1_table_offset = header.l1_table_offset; |
2d51c32c KW |
1056 | |
1057 | ||
d191d12d | 1058 | if (s->l1_size > 0) { |
9a4f4c31 | 1059 | s->l1_table = qemu_try_blockalign(bs->file->bs, |
d191d12d | 1060 | align_offset(s->l1_size * sizeof(uint64_t), 512)); |
de82815d KW |
1061 | if (s->l1_table == NULL) { |
1062 | error_setg(errp, "Could not allocate L1 table"); | |
1063 | ret = -ENOMEM; | |
1064 | goto fail; | |
1065 | } | |
9a4f4c31 | 1066 | ret = bdrv_pread(bs->file->bs, s->l1_table_offset, s->l1_table, |
6d85a57e JS |
1067 | s->l1_size * sizeof(uint64_t)); |
1068 | if (ret < 0) { | |
3ef6c40a | 1069 | error_setg_errno(errp, -ret, "Could not read L1 table"); |
d191d12d | 1070 | goto fail; |
6d85a57e | 1071 | } |
d191d12d SW |
1072 | for(i = 0;i < s->l1_size; i++) { |
1073 | be64_to_cpus(&s->l1_table[i]); | |
1074 | } | |
585f8587 | 1075 | } |
29c1a730 | 1076 | |
94edf3fb KW |
1077 | /* Parse driver-specific options */ |
1078 | ret = qcow2_update_options(bs, options, flags, errp); | |
90efa0ea KW |
1079 | if (ret < 0) { |
1080 | goto fail; | |
1081 | } | |
1082 | ||
7267c094 | 1083 | s->cluster_cache = g_malloc(s->cluster_size); |
585f8587 | 1084 | /* one more sector for decompressed data alignment */ |
9a4f4c31 | 1085 | s->cluster_data = qemu_try_blockalign(bs->file->bs, QCOW_MAX_CRYPT_CLUSTERS |
de82815d KW |
1086 | * s->cluster_size + 512); |
1087 | if (s->cluster_data == NULL) { | |
1088 | error_setg(errp, "Could not allocate temporary cluster buffer"); | |
1089 | ret = -ENOMEM; | |
1090 | goto fail; | |
1091 | } | |
1092 | ||
585f8587 | 1093 | s->cluster_cache_offset = -1; |
06d9260f | 1094 | s->flags = flags; |
3b46e624 | 1095 | |
6d85a57e JS |
1096 | ret = qcow2_refcount_init(bs); |
1097 | if (ret != 0) { | |
3ef6c40a | 1098 | error_setg_errno(errp, -ret, "Could not initialize refcount handling"); |
585f8587 | 1099 | goto fail; |
6d85a57e | 1100 | } |
585f8587 | 1101 | |
72cf2d4f | 1102 | QLIST_INIT(&s->cluster_allocs); |
0b919fae | 1103 | QTAILQ_INIT(&s->discards); |
f214978a | 1104 | |
9b80ddf3 | 1105 | /* read qcow2 extensions */ |
3ef6c40a HR |
1106 | if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL, |
1107 | &local_err)) { | |
1108 | error_propagate(errp, local_err); | |
6d85a57e | 1109 | ret = -EINVAL; |
9b80ddf3 | 1110 | goto fail; |
6d85a57e | 1111 | } |
9b80ddf3 | 1112 | |
585f8587 FB |
1113 | /* read the backing file name */ |
1114 | if (header.backing_file_offset != 0) { | |
1115 | len = header.backing_file_size; | |
9a29e18f | 1116 | if (len > MIN(1023, s->cluster_size - header.backing_file_offset) || |
e729fa6a | 1117 | len >= sizeof(bs->backing_file)) { |
6d33e8e7 KW |
1118 | error_setg(errp, "Backing file name too long"); |
1119 | ret = -EINVAL; | |
1120 | goto fail; | |
6d85a57e | 1121 | } |
9a4f4c31 | 1122 | ret = bdrv_pread(bs->file->bs, header.backing_file_offset, |
6d85a57e JS |
1123 | bs->backing_file, len); |
1124 | if (ret < 0) { | |
3ef6c40a | 1125 | error_setg_errno(errp, -ret, "Could not read backing file name"); |
585f8587 | 1126 | goto fail; |
6d85a57e | 1127 | } |
585f8587 | 1128 | bs->backing_file[len] = '\0'; |
e4603fe1 | 1129 | s->image_backing_file = g_strdup(bs->backing_file); |
585f8587 | 1130 | } |
42deb29f | 1131 | |
11b128f4 KW |
1132 | /* Internal snapshots */ |
1133 | s->snapshots_offset = header.snapshots_offset; | |
1134 | s->nb_snapshots = header.nb_snapshots; | |
1135 | ||
42deb29f KW |
1136 | ret = qcow2_read_snapshots(bs); |
1137 | if (ret < 0) { | |
3ef6c40a | 1138 | error_setg_errno(errp, -ret, "Could not read snapshots"); |
585f8587 | 1139 | goto fail; |
6d85a57e | 1140 | } |
585f8587 | 1141 | |
af7b708d | 1142 | /* Clear unknown autoclear feature bits */ |
27eb6c09 | 1143 | if (!bs->read_only && !(flags & BDRV_O_INCOMING) && s->autoclear_features) { |
af7b708d SH |
1144 | s->autoclear_features = 0; |
1145 | ret = qcow2_update_header(bs); | |
1146 | if (ret < 0) { | |
3ef6c40a | 1147 | error_setg_errno(errp, -ret, "Could not update qcow2 header"); |
af7b708d SH |
1148 | goto fail; |
1149 | } | |
1150 | } | |
1151 | ||
68d100e9 KW |
1152 | /* Initialise locks */ |
1153 | qemu_co_mutex_init(&s->lock); | |
1154 | ||
c61d0004 | 1155 | /* Repair image if dirty */ |
27eb6c09 | 1156 | if (!(flags & (BDRV_O_CHECK | BDRV_O_INCOMING)) && !bs->read_only && |
058f8f16 | 1157 | (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { |
c61d0004 SH |
1158 | BdrvCheckResult result = {0}; |
1159 | ||
5b84106b | 1160 | ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS); |
c61d0004 | 1161 | if (ret < 0) { |
3ef6c40a | 1162 | error_setg_errno(errp, -ret, "Could not repair dirty image"); |
c61d0004 SH |
1163 | goto fail; |
1164 | } | |
1165 | } | |
1166 | ||
585f8587 | 1167 | #ifdef DEBUG_ALLOC |
6cbc3031 PH |
1168 | { |
1169 | BdrvCheckResult result = {0}; | |
b35278f7 | 1170 | qcow2_check_refcounts(bs, &result, 0); |
6cbc3031 | 1171 | } |
585f8587 | 1172 | #endif |
6d85a57e | 1173 | return ret; |
585f8587 FB |
1174 | |
1175 | fail: | |
6744cbab | 1176 | g_free(s->unknown_header_fields); |
75bab85c | 1177 | cleanup_unknown_header_ext(bs); |
ed6ccf0f KW |
1178 | qcow2_free_snapshots(bs); |
1179 | qcow2_refcount_close(bs); | |
de82815d | 1180 | qemu_vfree(s->l1_table); |
cf93980e HR |
1181 | /* else pre-write overlap checks in cache_destroy may crash */ |
1182 | s->l1_table = NULL; | |
279621c0 | 1183 | cache_clean_timer_del(bs); |
29c1a730 KW |
1184 | if (s->l2_table_cache) { |
1185 | qcow2_cache_destroy(bs, s->l2_table_cache); | |
1186 | } | |
c5a33ee9 PJ |
1187 | if (s->refcount_block_cache) { |
1188 | qcow2_cache_destroy(bs, s->refcount_block_cache); | |
1189 | } | |
7267c094 | 1190 | g_free(s->cluster_cache); |
dea43a65 | 1191 | qemu_vfree(s->cluster_data); |
6d85a57e | 1192 | return ret; |
585f8587 FB |
1193 | } |
1194 | ||
3baca891 | 1195 | static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) |
d34682cd | 1196 | { |
ff99129a | 1197 | BDRVQcow2State *s = bs->opaque; |
d34682cd KW |
1198 | |
1199 | bs->bl.write_zeroes_alignment = s->cluster_sectors; | |
d34682cd KW |
1200 | } |
1201 | ||
7c80ab3f | 1202 | static int qcow2_set_key(BlockDriverState *bs, const char *key) |
585f8587 | 1203 | { |
ff99129a | 1204 | BDRVQcow2State *s = bs->opaque; |
585f8587 FB |
1205 | uint8_t keybuf[16]; |
1206 | int len, i; | |
f6fa64f6 | 1207 | Error *err = NULL; |
3b46e624 | 1208 | |
585f8587 FB |
1209 | memset(keybuf, 0, 16); |
1210 | len = strlen(key); | |
1211 | if (len > 16) | |
1212 | len = 16; | |
1213 | /* XXX: we could compress the chars to 7 bits to increase | |
1214 | entropy */ | |
1215 | for(i = 0;i < len;i++) { | |
1216 | keybuf[i] = key[i]; | |
1217 | } | |
8336aafa | 1218 | assert(bs->encrypted); |
585f8587 | 1219 | |
f6fa64f6 DB |
1220 | qcrypto_cipher_free(s->cipher); |
1221 | s->cipher = qcrypto_cipher_new( | |
1222 | QCRYPTO_CIPHER_ALG_AES_128, | |
1223 | QCRYPTO_CIPHER_MODE_CBC, | |
1224 | keybuf, G_N_ELEMENTS(keybuf), | |
1225 | &err); | |
1226 | ||
1227 | if (!s->cipher) { | |
1228 | /* XXX would be nice if errors in this method could | |
1229 | * be properly propagate to the caller. Would need | |
1230 | * the bdrv_set_key() API signature to be fixed. */ | |
1231 | error_free(err); | |
585f8587 | 1232 | return -1; |
585f8587 | 1233 | } |
585f8587 FB |
1234 | return 0; |
1235 | } | |
1236 | ||
21d82ac9 JC |
1237 | static int qcow2_reopen_prepare(BDRVReopenState *state, |
1238 | BlockReopenQueue *queue, Error **errp) | |
1239 | { | |
5b0959a7 | 1240 | Qcow2ReopenState *r; |
4c2e5f8f KW |
1241 | int ret; |
1242 | ||
5b0959a7 KW |
1243 | r = g_new0(Qcow2ReopenState, 1); |
1244 | state->opaque = r; | |
1245 | ||
1246 | ret = qcow2_update_options_prepare(state->bs, r, state->options, | |
1247 | state->flags, errp); | |
1248 | if (ret < 0) { | |
1249 | goto fail; | |
1250 | } | |
1251 | ||
1252 | /* We need to write out any unwritten data if we reopen read-only. */ | |
4c2e5f8f KW |
1253 | if ((state->flags & BDRV_O_RDWR) == 0) { |
1254 | ret = bdrv_flush(state->bs); | |
1255 | if (ret < 0) { | |
5b0959a7 | 1256 | goto fail; |
4c2e5f8f KW |
1257 | } |
1258 | ||
1259 | ret = qcow2_mark_clean(state->bs); | |
1260 | if (ret < 0) { | |
5b0959a7 | 1261 | goto fail; |
4c2e5f8f KW |
1262 | } |
1263 | } | |
1264 | ||
21d82ac9 | 1265 | return 0; |
5b0959a7 KW |
1266 | |
1267 | fail: | |
1268 | qcow2_update_options_abort(state->bs, r); | |
1269 | g_free(r); | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | static void qcow2_reopen_commit(BDRVReopenState *state) | |
1274 | { | |
1275 | qcow2_update_options_commit(state->bs, state->opaque); | |
1276 | g_free(state->opaque); | |
1277 | } | |
1278 | ||
1279 | static void qcow2_reopen_abort(BDRVReopenState *state) | |
1280 | { | |
1281 | qcow2_update_options_abort(state->bs, state->opaque); | |
1282 | g_free(state->opaque); | |
21d82ac9 JC |
1283 | } |
1284 | ||
5365f44d KW |
1285 | static void qcow2_join_options(QDict *options, QDict *old_options) |
1286 | { | |
1287 | bool has_new_overlap_template = | |
1288 | qdict_haskey(options, QCOW2_OPT_OVERLAP) || | |
1289 | qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE); | |
1290 | bool has_new_total_cache_size = | |
1291 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE); | |
1292 | bool has_all_cache_options; | |
1293 | ||
1294 | /* New overlap template overrides all old overlap options */ | |
1295 | if (has_new_overlap_template) { | |
1296 | qdict_del(old_options, QCOW2_OPT_OVERLAP); | |
1297 | qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE); | |
1298 | qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER); | |
1299 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1); | |
1300 | qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2); | |
1301 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE); | |
1302 | qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK); | |
1303 | qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE); | |
1304 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1); | |
1305 | qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2); | |
1306 | } | |
1307 | ||
1308 | /* New total cache size overrides all old options */ | |
1309 | if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) { | |
1310 | qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE); | |
1311 | qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
1312 | } | |
1313 | ||
1314 | qdict_join(options, old_options, false); | |
1315 | ||
1316 | /* | |
1317 | * If after merging all cache size options are set, an old total size is | |
1318 | * overwritten. Do keep all options, however, if all three are new. The | |
1319 | * resulting error message is what we want to happen. | |
1320 | */ | |
1321 | has_all_cache_options = | |
1322 | qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) || | |
1323 | qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) || | |
1324 | qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE); | |
1325 | ||
1326 | if (has_all_cache_options && !has_new_total_cache_size) { | |
1327 | qdict_del(options, QCOW2_OPT_CACHE_SIZE); | |
1328 | } | |
1329 | } | |
1330 | ||
b6b8a333 | 1331 | static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs, |
f8a2e5e3 | 1332 | int64_t sector_num, int nb_sectors, int *pnum) |
585f8587 | 1333 | { |
ff99129a | 1334 | BDRVQcow2State *s = bs->opaque; |
585f8587 | 1335 | uint64_t cluster_offset; |
4bc74be9 PB |
1336 | int index_in_cluster, ret; |
1337 | int64_t status = 0; | |
585f8587 | 1338 | |
095a9c58 | 1339 | *pnum = nb_sectors; |
f8a2e5e3 | 1340 | qemu_co_mutex_lock(&s->lock); |
1c46efaa | 1341 | ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); |
f8a2e5e3 | 1342 | qemu_co_mutex_unlock(&s->lock); |
1c46efaa | 1343 | if (ret < 0) { |
d663640c | 1344 | return ret; |
1c46efaa | 1345 | } |
095a9c58 | 1346 | |
4bc74be9 | 1347 | if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED && |
f6fa64f6 | 1348 | !s->cipher) { |
4bc74be9 PB |
1349 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
1350 | cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); | |
1351 | status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset; | |
1352 | } | |
1353 | if (ret == QCOW2_CLUSTER_ZERO) { | |
1354 | status |= BDRV_BLOCK_ZERO; | |
1355 | } else if (ret != QCOW2_CLUSTER_UNALLOCATED) { | |
1356 | status |= BDRV_BLOCK_DATA; | |
1357 | } | |
1358 | return status; | |
585f8587 FB |
1359 | } |
1360 | ||
a9465922 | 1361 | /* handle reading after the end of the backing file */ |
bd28f835 KW |
1362 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
1363 | int64_t sector_num, int nb_sectors) | |
a9465922 FB |
1364 | { |
1365 | int n1; | |
1366 | if ((sector_num + nb_sectors) <= bs->total_sectors) | |
1367 | return nb_sectors; | |
1368 | if (sector_num >= bs->total_sectors) | |
1369 | n1 = 0; | |
1370 | else | |
1371 | n1 = bs->total_sectors - sector_num; | |
bd28f835 | 1372 | |
3d9b4925 | 1373 | qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1)); |
bd28f835 | 1374 | |
a9465922 FB |
1375 | return n1; |
1376 | } | |
1377 | ||
a968168c | 1378 | static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, |
3fc48d09 | 1379 | int remaining_sectors, QEMUIOVector *qiov) |
585f8587 | 1380 | { |
ff99129a | 1381 | BDRVQcow2State *s = bs->opaque; |
a9465922 | 1382 | int index_in_cluster, n1; |
68d100e9 | 1383 | int ret; |
faf575c1 | 1384 | int cur_nr_sectors; /* number of sectors in current iteration */ |
c2bdd990 | 1385 | uint64_t cluster_offset = 0; |
3fc48d09 FZ |
1386 | uint64_t bytes_done = 0; |
1387 | QEMUIOVector hd_qiov; | |
1388 | uint8_t *cluster_data = NULL; | |
585f8587 | 1389 | |
3fc48d09 FZ |
1390 | qemu_iovec_init(&hd_qiov, qiov->niov); |
1391 | ||
1392 | qemu_co_mutex_lock(&s->lock); | |
1393 | ||
1394 | while (remaining_sectors != 0) { | |
bd28f835 | 1395 | |
5ebaa27e | 1396 | /* prepare next request */ |
3fc48d09 | 1397 | cur_nr_sectors = remaining_sectors; |
f6fa64f6 | 1398 | if (s->cipher) { |
5ebaa27e FZ |
1399 | cur_nr_sectors = MIN(cur_nr_sectors, |
1400 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
585f8587 | 1401 | } |
5ebaa27e | 1402 | |
3fc48d09 | 1403 | ret = qcow2_get_cluster_offset(bs, sector_num << 9, |
5ebaa27e | 1404 | &cur_nr_sectors, &cluster_offset); |
8af36488 | 1405 | if (ret < 0) { |
3fc48d09 | 1406 | goto fail; |
8af36488 | 1407 | } |
bd28f835 | 1408 | |
3fc48d09 | 1409 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
c87c0672 | 1410 | |
3fc48d09 | 1411 | qemu_iovec_reset(&hd_qiov); |
1b093c48 | 1412 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, |
5ebaa27e FZ |
1413 | cur_nr_sectors * 512); |
1414 | ||
68d000a3 KW |
1415 | switch (ret) { |
1416 | case QCOW2_CLUSTER_UNALLOCATED: | |
5ebaa27e | 1417 | |
760e0063 | 1418 | if (bs->backing) { |
5ebaa27e | 1419 | /* read from the base image */ |
760e0063 | 1420 | n1 = qcow2_backing_read1(bs->backing->bs, &hd_qiov, |
3fc48d09 | 1421 | sector_num, cur_nr_sectors); |
5ebaa27e | 1422 | if (n1 > 0) { |
44deba5a KW |
1423 | QEMUIOVector local_qiov; |
1424 | ||
1425 | qemu_iovec_init(&local_qiov, hd_qiov.niov); | |
1426 | qemu_iovec_concat(&local_qiov, &hd_qiov, 0, | |
1427 | n1 * BDRV_SECTOR_SIZE); | |
1428 | ||
5ebaa27e FZ |
1429 | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
1430 | qemu_co_mutex_unlock(&s->lock); | |
760e0063 | 1431 | ret = bdrv_co_readv(bs->backing->bs, sector_num, |
44deba5a | 1432 | n1, &local_qiov); |
5ebaa27e | 1433 | qemu_co_mutex_lock(&s->lock); |
44deba5a KW |
1434 | |
1435 | qemu_iovec_destroy(&local_qiov); | |
1436 | ||
5ebaa27e | 1437 | if (ret < 0) { |
3fc48d09 | 1438 | goto fail; |
5ebaa27e FZ |
1439 | } |
1440 | } | |
1441 | } else { | |
1442 | /* Note: in this case, no need to wait */ | |
3d9b4925 | 1443 | qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); |
5ebaa27e | 1444 | } |
68d000a3 KW |
1445 | break; |
1446 | ||
6377af48 | 1447 | case QCOW2_CLUSTER_ZERO: |
3d9b4925 | 1448 | qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); |
6377af48 KW |
1449 | break; |
1450 | ||
68d000a3 | 1451 | case QCOW2_CLUSTER_COMPRESSED: |
5ebaa27e FZ |
1452 | /* add AIO support for compressed blocks ? */ |
1453 | ret = qcow2_decompress_cluster(bs, cluster_offset); | |
1454 | if (ret < 0) { | |
3fc48d09 | 1455 | goto fail; |
bd28f835 KW |
1456 | } |
1457 | ||
03396148 | 1458 | qemu_iovec_from_buf(&hd_qiov, 0, |
5ebaa27e | 1459 | s->cluster_cache + index_in_cluster * 512, |
faf575c1 | 1460 | 512 * cur_nr_sectors); |
68d000a3 KW |
1461 | break; |
1462 | ||
1463 | case QCOW2_CLUSTER_NORMAL: | |
5ebaa27e | 1464 | if ((cluster_offset & 511) != 0) { |
3fc48d09 FZ |
1465 | ret = -EIO; |
1466 | goto fail; | |
5ebaa27e | 1467 | } |
bd28f835 | 1468 | |
8336aafa | 1469 | if (bs->encrypted) { |
f6fa64f6 | 1470 | assert(s->cipher); |
8336aafa | 1471 | |
5ebaa27e FZ |
1472 | /* |
1473 | * For encrypted images, read everything into a temporary | |
1474 | * contiguous buffer on which the AES functions can work. | |
1475 | */ | |
3fc48d09 FZ |
1476 | if (!cluster_data) { |
1477 | cluster_data = | |
9a4f4c31 KW |
1478 | qemu_try_blockalign(bs->file->bs, |
1479 | QCOW_MAX_CRYPT_CLUSTERS | |
1480 | * s->cluster_size); | |
de82815d KW |
1481 | if (cluster_data == NULL) { |
1482 | ret = -ENOMEM; | |
1483 | goto fail; | |
1484 | } | |
5ebaa27e FZ |
1485 | } |
1486 | ||
1487 | assert(cur_nr_sectors <= | |
1488 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
3fc48d09 FZ |
1489 | qemu_iovec_reset(&hd_qiov); |
1490 | qemu_iovec_add(&hd_qiov, cluster_data, | |
5ebaa27e FZ |
1491 | 512 * cur_nr_sectors); |
1492 | } | |
1493 | ||
1494 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
1495 | qemu_co_mutex_unlock(&s->lock); | |
9a4f4c31 | 1496 | ret = bdrv_co_readv(bs->file->bs, |
5ebaa27e | 1497 | (cluster_offset >> 9) + index_in_cluster, |
3fc48d09 | 1498 | cur_nr_sectors, &hd_qiov); |
5ebaa27e FZ |
1499 | qemu_co_mutex_lock(&s->lock); |
1500 | if (ret < 0) { | |
3fc48d09 | 1501 | goto fail; |
5ebaa27e | 1502 | } |
8336aafa | 1503 | if (bs->encrypted) { |
f6fa64f6 DB |
1504 | assert(s->cipher); |
1505 | Error *err = NULL; | |
1506 | if (qcow2_encrypt_sectors(s, sector_num, cluster_data, | |
1507 | cluster_data, cur_nr_sectors, false, | |
1508 | &err) < 0) { | |
1509 | error_free(err); | |
1510 | ret = -EIO; | |
1511 | goto fail; | |
1512 | } | |
03396148 MT |
1513 | qemu_iovec_from_buf(qiov, bytes_done, |
1514 | cluster_data, 512 * cur_nr_sectors); | |
5ebaa27e | 1515 | } |
68d000a3 KW |
1516 | break; |
1517 | ||
1518 | default: | |
1519 | g_assert_not_reached(); | |
1520 | ret = -EIO; | |
1521 | goto fail; | |
faf575c1 | 1522 | } |
f141eafe | 1523 | |
3fc48d09 FZ |
1524 | remaining_sectors -= cur_nr_sectors; |
1525 | sector_num += cur_nr_sectors; | |
1526 | bytes_done += cur_nr_sectors * 512; | |
5ebaa27e | 1527 | } |
3fc48d09 | 1528 | ret = 0; |
faf575c1 | 1529 | |
3fc48d09 | 1530 | fail: |
68d100e9 | 1531 | qemu_co_mutex_unlock(&s->lock); |
42496d62 | 1532 | |
3fc48d09 | 1533 | qemu_iovec_destroy(&hd_qiov); |
dea43a65 | 1534 | qemu_vfree(cluster_data); |
68d100e9 KW |
1535 | |
1536 | return ret; | |
585f8587 FB |
1537 | } |
1538 | ||
a968168c | 1539 | static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, |
3fc48d09 FZ |
1540 | int64_t sector_num, |
1541 | int remaining_sectors, | |
1542 | QEMUIOVector *qiov) | |
585f8587 | 1543 | { |
ff99129a | 1544 | BDRVQcow2State *s = bs->opaque; |
585f8587 | 1545 | int index_in_cluster; |
68d100e9 | 1546 | int ret; |
faf575c1 | 1547 | int cur_nr_sectors; /* number of sectors in current iteration */ |
c2bdd990 | 1548 | uint64_t cluster_offset; |
3fc48d09 FZ |
1549 | QEMUIOVector hd_qiov; |
1550 | uint64_t bytes_done = 0; | |
1551 | uint8_t *cluster_data = NULL; | |
8d2497c3 | 1552 | QCowL2Meta *l2meta = NULL; |
c2271403 | 1553 | |
3cce16f4 KW |
1554 | trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num, |
1555 | remaining_sectors); | |
1556 | ||
3fc48d09 FZ |
1557 | qemu_iovec_init(&hd_qiov, qiov->niov); |
1558 | ||
1559 | s->cluster_cache_offset = -1; /* disable compressed cache */ | |
3b46e624 | 1560 | |
3fc48d09 FZ |
1561 | qemu_co_mutex_lock(&s->lock); |
1562 | ||
1563 | while (remaining_sectors != 0) { | |
1564 | ||
f50f88b9 | 1565 | l2meta = NULL; |
cf5c1a23 | 1566 | |
3cce16f4 | 1567 | trace_qcow2_writev_start_part(qemu_coroutine_self()); |
3fc48d09 | 1568 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
16f0587e | 1569 | cur_nr_sectors = remaining_sectors; |
8336aafa | 1570 | if (bs->encrypted && |
16f0587e HT |
1571 | cur_nr_sectors > |
1572 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) { | |
1573 | cur_nr_sectors = | |
1574 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster; | |
5ebaa27e | 1575 | } |
095a9c58 | 1576 | |
3fc48d09 | 1577 | ret = qcow2_alloc_cluster_offset(bs, sector_num << 9, |
16f0587e | 1578 | &cur_nr_sectors, &cluster_offset, &l2meta); |
5ebaa27e | 1579 | if (ret < 0) { |
3fc48d09 | 1580 | goto fail; |
5ebaa27e | 1581 | } |
148da7ea | 1582 | |
5ebaa27e | 1583 | assert((cluster_offset & 511) == 0); |
148da7ea | 1584 | |
3fc48d09 | 1585 | qemu_iovec_reset(&hd_qiov); |
1b093c48 | 1586 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, |
5ebaa27e | 1587 | cur_nr_sectors * 512); |
6f5f060b | 1588 | |
8336aafa | 1589 | if (bs->encrypted) { |
f6fa64f6 DB |
1590 | Error *err = NULL; |
1591 | assert(s->cipher); | |
3fc48d09 | 1592 | if (!cluster_data) { |
9a4f4c31 | 1593 | cluster_data = qemu_try_blockalign(bs->file->bs, |
de82815d KW |
1594 | QCOW_MAX_CRYPT_CLUSTERS |
1595 | * s->cluster_size); | |
1596 | if (cluster_data == NULL) { | |
1597 | ret = -ENOMEM; | |
1598 | goto fail; | |
1599 | } | |
5ebaa27e | 1600 | } |
6f5f060b | 1601 | |
3fc48d09 | 1602 | assert(hd_qiov.size <= |
5ebaa27e | 1603 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
d5e6b161 | 1604 | qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size); |
6f5f060b | 1605 | |
f6fa64f6 DB |
1606 | if (qcow2_encrypt_sectors(s, sector_num, cluster_data, |
1607 | cluster_data, cur_nr_sectors, | |
1608 | true, &err) < 0) { | |
1609 | error_free(err); | |
1610 | ret = -EIO; | |
1611 | goto fail; | |
1612 | } | |
6f5f060b | 1613 | |
3fc48d09 FZ |
1614 | qemu_iovec_reset(&hd_qiov); |
1615 | qemu_iovec_add(&hd_qiov, cluster_data, | |
5ebaa27e FZ |
1616 | cur_nr_sectors * 512); |
1617 | } | |
6f5f060b | 1618 | |
231bb267 | 1619 | ret = qcow2_pre_write_overlap_check(bs, 0, |
cf93980e HR |
1620 | cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE, |
1621 | cur_nr_sectors * BDRV_SECTOR_SIZE); | |
1622 | if (ret < 0) { | |
1623 | goto fail; | |
1624 | } | |
1625 | ||
5ebaa27e | 1626 | qemu_co_mutex_unlock(&s->lock); |
67a7a0eb | 1627 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
3cce16f4 KW |
1628 | trace_qcow2_writev_data(qemu_coroutine_self(), |
1629 | (cluster_offset >> 9) + index_in_cluster); | |
9a4f4c31 | 1630 | ret = bdrv_co_writev(bs->file->bs, |
5ebaa27e | 1631 | (cluster_offset >> 9) + index_in_cluster, |
3fc48d09 | 1632 | cur_nr_sectors, &hd_qiov); |
5ebaa27e FZ |
1633 | qemu_co_mutex_lock(&s->lock); |
1634 | if (ret < 0) { | |
3fc48d09 | 1635 | goto fail; |
5ebaa27e | 1636 | } |
f141eafe | 1637 | |
88c6588c KW |
1638 | while (l2meta != NULL) { |
1639 | QCowL2Meta *next; | |
1640 | ||
f50f88b9 KW |
1641 | ret = qcow2_alloc_cluster_link_l2(bs, l2meta); |
1642 | if (ret < 0) { | |
1643 | goto fail; | |
1644 | } | |
faf575c1 | 1645 | |
4e95314e KW |
1646 | /* Take the request off the list of running requests */ |
1647 | if (l2meta->nb_clusters != 0) { | |
1648 | QLIST_REMOVE(l2meta, next_in_flight); | |
1649 | } | |
1650 | ||
4e95314e | 1651 | qemu_co_queue_restart_all(&l2meta->dependent_requests); |
4e95314e | 1652 | |
88c6588c | 1653 | next = l2meta->next; |
f50f88b9 | 1654 | g_free(l2meta); |
88c6588c | 1655 | l2meta = next; |
f50f88b9 | 1656 | } |
0fa9131a | 1657 | |
3fc48d09 FZ |
1658 | remaining_sectors -= cur_nr_sectors; |
1659 | sector_num += cur_nr_sectors; | |
1660 | bytes_done += cur_nr_sectors * 512; | |
3cce16f4 | 1661 | trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors); |
5ebaa27e | 1662 | } |
3fc48d09 | 1663 | ret = 0; |
faf575c1 | 1664 | |
3fc48d09 | 1665 | fail: |
4e95314e KW |
1666 | qemu_co_mutex_unlock(&s->lock); |
1667 | ||
88c6588c KW |
1668 | while (l2meta != NULL) { |
1669 | QCowL2Meta *next; | |
1670 | ||
4e95314e KW |
1671 | if (l2meta->nb_clusters != 0) { |
1672 | QLIST_REMOVE(l2meta, next_in_flight); | |
1673 | } | |
1674 | qemu_co_queue_restart_all(&l2meta->dependent_requests); | |
88c6588c KW |
1675 | |
1676 | next = l2meta->next; | |
cf5c1a23 | 1677 | g_free(l2meta); |
88c6588c | 1678 | l2meta = next; |
cf5c1a23 | 1679 | } |
0fa9131a | 1680 | |
3fc48d09 | 1681 | qemu_iovec_destroy(&hd_qiov); |
dea43a65 | 1682 | qemu_vfree(cluster_data); |
3cce16f4 | 1683 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
42496d62 | 1684 | |
68d100e9 | 1685 | return ret; |
585f8587 FB |
1686 | } |
1687 | ||
7c80ab3f | 1688 | static void qcow2_close(BlockDriverState *bs) |
585f8587 | 1689 | { |
ff99129a | 1690 | BDRVQcow2State *s = bs->opaque; |
de82815d | 1691 | qemu_vfree(s->l1_table); |
cf93980e HR |
1692 | /* else pre-write overlap checks in cache_destroy may crash */ |
1693 | s->l1_table = NULL; | |
29c1a730 | 1694 | |
27eb6c09 | 1695 | if (!(bs->open_flags & BDRV_O_INCOMING)) { |
3b5e14c7 | 1696 | int ret1, ret2; |
29c1a730 | 1697 | |
3b5e14c7 HR |
1698 | ret1 = qcow2_cache_flush(bs, s->l2_table_cache); |
1699 | ret2 = qcow2_cache_flush(bs, s->refcount_block_cache); | |
1700 | ||
1701 | if (ret1) { | |
1702 | error_report("Failed to flush the L2 table cache: %s", | |
1703 | strerror(-ret1)); | |
1704 | } | |
1705 | if (ret2) { | |
1706 | error_report("Failed to flush the refcount block cache: %s", | |
1707 | strerror(-ret2)); | |
1708 | } | |
1709 | ||
1710 | if (!ret1 && !ret2) { | |
1711 | qcow2_mark_clean(bs); | |
1712 | } | |
27eb6c09 | 1713 | } |
c61d0004 | 1714 | |
279621c0 | 1715 | cache_clean_timer_del(bs); |
29c1a730 KW |
1716 | qcow2_cache_destroy(bs, s->l2_table_cache); |
1717 | qcow2_cache_destroy(bs, s->refcount_block_cache); | |
1718 | ||
f6fa64f6 DB |
1719 | qcrypto_cipher_free(s->cipher); |
1720 | s->cipher = NULL; | |
1721 | ||
6744cbab | 1722 | g_free(s->unknown_header_fields); |
75bab85c | 1723 | cleanup_unknown_header_ext(bs); |
6744cbab | 1724 | |
e4603fe1 KW |
1725 | g_free(s->image_backing_file); |
1726 | g_free(s->image_backing_format); | |
1727 | ||
7267c094 | 1728 | g_free(s->cluster_cache); |
dea43a65 | 1729 | qemu_vfree(s->cluster_data); |
ed6ccf0f | 1730 | qcow2_refcount_close(bs); |
28c1202b | 1731 | qcow2_free_snapshots(bs); |
585f8587 FB |
1732 | } |
1733 | ||
5a8a30db | 1734 | static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp) |
06d9260f | 1735 | { |
ff99129a | 1736 | BDRVQcow2State *s = bs->opaque; |
06d9260f | 1737 | int flags = s->flags; |
f6fa64f6 | 1738 | QCryptoCipher *cipher = NULL; |
acdfb480 | 1739 | QDict *options; |
5a8a30db KW |
1740 | Error *local_err = NULL; |
1741 | int ret; | |
06d9260f AL |
1742 | |
1743 | /* | |
1744 | * Backing files are read-only which makes all of their metadata immutable, | |
1745 | * that means we don't have to worry about reopening them here. | |
1746 | */ | |
1747 | ||
f6fa64f6 DB |
1748 | cipher = s->cipher; |
1749 | s->cipher = NULL; | |
06d9260f AL |
1750 | |
1751 | qcow2_close(bs); | |
1752 | ||
9a4f4c31 | 1753 | bdrv_invalidate_cache(bs->file->bs, &local_err); |
5a8a30db KW |
1754 | if (local_err) { |
1755 | error_propagate(errp, local_err); | |
1756 | return; | |
1757 | } | |
3456a8d1 | 1758 | |
ff99129a | 1759 | memset(s, 0, sizeof(BDRVQcow2State)); |
d475e5ac | 1760 | options = qdict_clone_shallow(bs->options); |
5a8a30db KW |
1761 | |
1762 | ret = qcow2_open(bs, options, flags, &local_err); | |
a1904e48 | 1763 | QDECREF(options); |
5a8a30db | 1764 | if (local_err) { |
e43bfd9c MA |
1765 | error_propagate(errp, local_err); |
1766 | error_prepend(errp, "Could not reopen qcow2 layer: "); | |
5a8a30db KW |
1767 | return; |
1768 | } else if (ret < 0) { | |
1769 | error_setg_errno(errp, -ret, "Could not reopen qcow2 layer"); | |
1770 | return; | |
1771 | } | |
acdfb480 | 1772 | |
f6fa64f6 | 1773 | s->cipher = cipher; |
06d9260f AL |
1774 | } |
1775 | ||
e24e49e6 KW |
1776 | static size_t header_ext_add(char *buf, uint32_t magic, const void *s, |
1777 | size_t len, size_t buflen) | |
1778 | { | |
1779 | QCowExtension *ext_backing_fmt = (QCowExtension*) buf; | |
1780 | size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); | |
1781 | ||
1782 | if (buflen < ext_len) { | |
1783 | return -ENOSPC; | |
1784 | } | |
1785 | ||
1786 | *ext_backing_fmt = (QCowExtension) { | |
1787 | .magic = cpu_to_be32(magic), | |
1788 | .len = cpu_to_be32(len), | |
1789 | }; | |
1790 | memcpy(buf + sizeof(QCowExtension), s, len); | |
1791 | ||
1792 | return ext_len; | |
1793 | } | |
1794 | ||
756e6736 | 1795 | /* |
e24e49e6 KW |
1796 | * Updates the qcow2 header, including the variable length parts of it, i.e. |
1797 | * the backing file name and all extensions. qcow2 was not designed to allow | |
1798 | * such changes, so if we run out of space (we can only use the first cluster) | |
1799 | * this function may fail. | |
756e6736 KW |
1800 | * |
1801 | * Returns 0 on success, -errno in error cases. | |
1802 | */ | |
e24e49e6 | 1803 | int qcow2_update_header(BlockDriverState *bs) |
756e6736 | 1804 | { |
ff99129a | 1805 | BDRVQcow2State *s = bs->opaque; |
e24e49e6 KW |
1806 | QCowHeader *header; |
1807 | char *buf; | |
1808 | size_t buflen = s->cluster_size; | |
756e6736 | 1809 | int ret; |
e24e49e6 KW |
1810 | uint64_t total_size; |
1811 | uint32_t refcount_table_clusters; | |
6744cbab | 1812 | size_t header_length; |
75bab85c | 1813 | Qcow2UnknownHeaderExtension *uext; |
756e6736 | 1814 | |
e24e49e6 | 1815 | buf = qemu_blockalign(bs, buflen); |
756e6736 | 1816 | |
e24e49e6 KW |
1817 | /* Header structure */ |
1818 | header = (QCowHeader*) buf; | |
756e6736 | 1819 | |
e24e49e6 KW |
1820 | if (buflen < sizeof(*header)) { |
1821 | ret = -ENOSPC; | |
1822 | goto fail; | |
756e6736 KW |
1823 | } |
1824 | ||
6744cbab | 1825 | header_length = sizeof(*header) + s->unknown_header_fields_size; |
e24e49e6 KW |
1826 | total_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
1827 | refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); | |
1828 | ||
1829 | *header = (QCowHeader) { | |
6744cbab | 1830 | /* Version 2 fields */ |
e24e49e6 | 1831 | .magic = cpu_to_be32(QCOW_MAGIC), |
6744cbab | 1832 | .version = cpu_to_be32(s->qcow_version), |
e24e49e6 KW |
1833 | .backing_file_offset = 0, |
1834 | .backing_file_size = 0, | |
1835 | .cluster_bits = cpu_to_be32(s->cluster_bits), | |
1836 | .size = cpu_to_be64(total_size), | |
1837 | .crypt_method = cpu_to_be32(s->crypt_method_header), | |
1838 | .l1_size = cpu_to_be32(s->l1_size), | |
1839 | .l1_table_offset = cpu_to_be64(s->l1_table_offset), | |
1840 | .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), | |
1841 | .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), | |
1842 | .nb_snapshots = cpu_to_be32(s->nb_snapshots), | |
1843 | .snapshots_offset = cpu_to_be64(s->snapshots_offset), | |
6744cbab KW |
1844 | |
1845 | /* Version 3 fields */ | |
1846 | .incompatible_features = cpu_to_be64(s->incompatible_features), | |
1847 | .compatible_features = cpu_to_be64(s->compatible_features), | |
1848 | .autoclear_features = cpu_to_be64(s->autoclear_features), | |
b6481f37 | 1849 | .refcount_order = cpu_to_be32(s->refcount_order), |
6744cbab | 1850 | .header_length = cpu_to_be32(header_length), |
e24e49e6 | 1851 | }; |
756e6736 | 1852 | |
6744cbab KW |
1853 | /* For older versions, write a shorter header */ |
1854 | switch (s->qcow_version) { | |
1855 | case 2: | |
1856 | ret = offsetof(QCowHeader, incompatible_features); | |
1857 | break; | |
1858 | case 3: | |
1859 | ret = sizeof(*header); | |
1860 | break; | |
1861 | default: | |
b6c14762 JM |
1862 | ret = -EINVAL; |
1863 | goto fail; | |
6744cbab KW |
1864 | } |
1865 | ||
1866 | buf += ret; | |
1867 | buflen -= ret; | |
1868 | memset(buf, 0, buflen); | |
1869 | ||
1870 | /* Preserve any unknown field in the header */ | |
1871 | if (s->unknown_header_fields_size) { | |
1872 | if (buflen < s->unknown_header_fields_size) { | |
1873 | ret = -ENOSPC; | |
1874 | goto fail; | |
1875 | } | |
1876 | ||
1877 | memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); | |
1878 | buf += s->unknown_header_fields_size; | |
1879 | buflen -= s->unknown_header_fields_size; | |
1880 | } | |
756e6736 | 1881 | |
e24e49e6 | 1882 | /* Backing file format header extension */ |
e4603fe1 | 1883 | if (s->image_backing_format) { |
e24e49e6 | 1884 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, |
e4603fe1 KW |
1885 | s->image_backing_format, |
1886 | strlen(s->image_backing_format), | |
e24e49e6 KW |
1887 | buflen); |
1888 | if (ret < 0) { | |
1889 | goto fail; | |
756e6736 KW |
1890 | } |
1891 | ||
e24e49e6 KW |
1892 | buf += ret; |
1893 | buflen -= ret; | |
756e6736 KW |
1894 | } |
1895 | ||
cfcc4c62 KW |
1896 | /* Feature table */ |
1897 | Qcow2Feature features[] = { | |
c61d0004 SH |
1898 | { |
1899 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
1900 | .bit = QCOW2_INCOMPAT_DIRTY_BITNR, | |
1901 | .name = "dirty bit", | |
1902 | }, | |
69c98726 HR |
1903 | { |
1904 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
1905 | .bit = QCOW2_INCOMPAT_CORRUPT_BITNR, | |
1906 | .name = "corrupt bit", | |
1907 | }, | |
bfe8043e SH |
1908 | { |
1909 | .type = QCOW2_FEAT_TYPE_COMPATIBLE, | |
1910 | .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
1911 | .name = "lazy refcounts", | |
1912 | }, | |
cfcc4c62 KW |
1913 | }; |
1914 | ||
1915 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, | |
1916 | features, sizeof(features), buflen); | |
1917 | if (ret < 0) { | |
1918 | goto fail; | |
1919 | } | |
1920 | buf += ret; | |
1921 | buflen -= ret; | |
1922 | ||
75bab85c KW |
1923 | /* Keep unknown header extensions */ |
1924 | QLIST_FOREACH(uext, &s->unknown_header_ext, next) { | |
1925 | ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); | |
1926 | if (ret < 0) { | |
1927 | goto fail; | |
1928 | } | |
1929 | ||
1930 | buf += ret; | |
1931 | buflen -= ret; | |
1932 | } | |
1933 | ||
e24e49e6 KW |
1934 | /* End of header extensions */ |
1935 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); | |
756e6736 KW |
1936 | if (ret < 0) { |
1937 | goto fail; | |
1938 | } | |
1939 | ||
e24e49e6 KW |
1940 | buf += ret; |
1941 | buflen -= ret; | |
756e6736 | 1942 | |
e24e49e6 | 1943 | /* Backing file name */ |
e4603fe1 KW |
1944 | if (s->image_backing_file) { |
1945 | size_t backing_file_len = strlen(s->image_backing_file); | |
e24e49e6 KW |
1946 | |
1947 | if (buflen < backing_file_len) { | |
1948 | ret = -ENOSPC; | |
1949 | goto fail; | |
1950 | } | |
1951 | ||
00ea1881 | 1952 | /* Using strncpy is ok here, since buf is not NUL-terminated. */ |
e4603fe1 | 1953 | strncpy(buf, s->image_backing_file, buflen); |
e24e49e6 KW |
1954 | |
1955 | header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); | |
1956 | header->backing_file_size = cpu_to_be32(backing_file_len); | |
756e6736 KW |
1957 | } |
1958 | ||
e24e49e6 | 1959 | /* Write the new header */ |
9a4f4c31 | 1960 | ret = bdrv_pwrite(bs->file->bs, 0, header, s->cluster_size); |
756e6736 KW |
1961 | if (ret < 0) { |
1962 | goto fail; | |
1963 | } | |
1964 | ||
1965 | ret = 0; | |
1966 | fail: | |
e24e49e6 | 1967 | qemu_vfree(header); |
756e6736 KW |
1968 | return ret; |
1969 | } | |
1970 | ||
1971 | static int qcow2_change_backing_file(BlockDriverState *bs, | |
1972 | const char *backing_file, const char *backing_fmt) | |
1973 | { | |
ff99129a | 1974 | BDRVQcow2State *s = bs->opaque; |
e4603fe1 | 1975 | |
e24e49e6 KW |
1976 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
1977 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
1978 | ||
e4603fe1 KW |
1979 | g_free(s->image_backing_file); |
1980 | g_free(s->image_backing_format); | |
1981 | ||
1982 | s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL; | |
1983 | s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL; | |
1984 | ||
e24e49e6 | 1985 | return qcow2_update_header(bs); |
756e6736 KW |
1986 | } |
1987 | ||
a35e1c17 KW |
1988 | static int preallocate(BlockDriverState *bs) |
1989 | { | |
a35e1c17 KW |
1990 | uint64_t nb_sectors; |
1991 | uint64_t offset; | |
060bee89 | 1992 | uint64_t host_offset = 0; |
a35e1c17 | 1993 | int num; |
148da7ea | 1994 | int ret; |
f50f88b9 | 1995 | QCowL2Meta *meta; |
a35e1c17 | 1996 | |
57322b78 | 1997 | nb_sectors = bdrv_nb_sectors(bs); |
a35e1c17 KW |
1998 | offset = 0; |
1999 | ||
2000 | while (nb_sectors) { | |
7c2bbf4a | 2001 | num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS); |
16f0587e | 2002 | ret = qcow2_alloc_cluster_offset(bs, offset, &num, |
060bee89 | 2003 | &host_offset, &meta); |
148da7ea | 2004 | if (ret < 0) { |
19dbcbf7 | 2005 | return ret; |
a35e1c17 KW |
2006 | } |
2007 | ||
c792707f SH |
2008 | while (meta) { |
2009 | QCowL2Meta *next = meta->next; | |
2010 | ||
7c2bbf4a HT |
2011 | ret = qcow2_alloc_cluster_link_l2(bs, meta); |
2012 | if (ret < 0) { | |
2013 | qcow2_free_any_clusters(bs, meta->alloc_offset, | |
2014 | meta->nb_clusters, QCOW2_DISCARD_NEVER); | |
2015 | return ret; | |
2016 | } | |
2017 | ||
2018 | /* There are no dependent requests, but we need to remove our | |
2019 | * request from the list of in-flight requests */ | |
4e95314e | 2020 | QLIST_REMOVE(meta, next_in_flight); |
c792707f SH |
2021 | |
2022 | g_free(meta); | |
2023 | meta = next; | |
f50f88b9 | 2024 | } |
f214978a | 2025 | |
a35e1c17 KW |
2026 | /* TODO Preallocate data if requested */ |
2027 | ||
2028 | nb_sectors -= num; | |
7c2bbf4a | 2029 | offset += num << BDRV_SECTOR_BITS; |
a35e1c17 KW |
2030 | } |
2031 | ||
2032 | /* | |
2033 | * It is expected that the image file is large enough to actually contain | |
2034 | * all of the allocated clusters (otherwise we get failing reads after | |
2035 | * EOF). Extend the image to the last allocated sector. | |
2036 | */ | |
060bee89 | 2037 | if (host_offset != 0) { |
7c2bbf4a HT |
2038 | uint8_t buf[BDRV_SECTOR_SIZE]; |
2039 | memset(buf, 0, BDRV_SECTOR_SIZE); | |
9a4f4c31 KW |
2040 | ret = bdrv_write(bs->file->bs, |
2041 | (host_offset >> BDRV_SECTOR_BITS) + num - 1, | |
7c2bbf4a | 2042 | buf, 1); |
19dbcbf7 KW |
2043 | if (ret < 0) { |
2044 | return ret; | |
2045 | } | |
a35e1c17 KW |
2046 | } |
2047 | ||
2048 | return 0; | |
2049 | } | |
2050 | ||
7c80ab3f JS |
2051 | static int qcow2_create2(const char *filename, int64_t total_size, |
2052 | const char *backing_file, const char *backing_format, | |
ffeaac9b | 2053 | int flags, size_t cluster_size, PreallocMode prealloc, |
bd4b167f | 2054 | QemuOpts *opts, int version, int refcount_order, |
3ef6c40a | 2055 | Error **errp) |
a9420734 | 2056 | { |
a9420734 | 2057 | int cluster_bits; |
e6641719 HR |
2058 | QDict *options; |
2059 | ||
2060 | /* Calculate cluster_bits */ | |
786a4ea8 | 2061 | cluster_bits = ctz32(cluster_size); |
a9420734 KW |
2062 | if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || |
2063 | (1 << cluster_bits) != cluster_size) | |
2064 | { | |
3ef6c40a HR |
2065 | error_setg(errp, "Cluster size must be a power of two between %d and " |
2066 | "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); | |
a9420734 KW |
2067 | return -EINVAL; |
2068 | } | |
2069 | ||
2070 | /* | |
2071 | * Open the image file and write a minimal qcow2 header. | |
2072 | * | |
2073 | * We keep things simple and start with a zero-sized image. We also | |
2074 | * do without refcount blocks or a L1 table for now. We'll fix the | |
2075 | * inconsistency later. | |
2076 | * | |
2077 | * We do need a refcount table because growing the refcount table means | |
2078 | * allocating two new refcount blocks - the seconds of which would be at | |
2079 | * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file | |
2080 | * size for any qcow2 image. | |
2081 | */ | |
2082 | BlockDriverState* bs; | |
f8413b3c | 2083 | QCowHeader *header; |
b106ad91 | 2084 | uint64_t* refcount_table; |
3ef6c40a | 2085 | Error *local_err = NULL; |
a9420734 KW |
2086 | int ret; |
2087 | ||
0e4271b7 | 2088 | if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) { |
bd4b167f HR |
2089 | /* Note: The following calculation does not need to be exact; if it is a |
2090 | * bit off, either some bytes will be "leaked" (which is fine) or we | |
2091 | * will need to increase the file size by some bytes (which is fine, | |
2092 | * too, as long as the bulk is allocated here). Therefore, using | |
2093 | * floating point arithmetic is fine. */ | |
0e4271b7 HT |
2094 | int64_t meta_size = 0; |
2095 | uint64_t nreftablee, nrefblocke, nl1e, nl2e; | |
2096 | int64_t aligned_total_size = align_offset(total_size, cluster_size); | |
bd4b167f HR |
2097 | int refblock_bits, refblock_size; |
2098 | /* refcount entry size in bytes */ | |
2099 | double rces = (1 << refcount_order) / 8.; | |
2100 | ||
2101 | /* see qcow2_open() */ | |
2102 | refblock_bits = cluster_bits - (refcount_order - 3); | |
2103 | refblock_size = 1 << refblock_bits; | |
0e4271b7 HT |
2104 | |
2105 | /* header: 1 cluster */ | |
2106 | meta_size += cluster_size; | |
2107 | ||
2108 | /* total size of L2 tables */ | |
2109 | nl2e = aligned_total_size / cluster_size; | |
2110 | nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t)); | |
2111 | meta_size += nl2e * sizeof(uint64_t); | |
2112 | ||
2113 | /* total size of L1 tables */ | |
2114 | nl1e = nl2e * sizeof(uint64_t) / cluster_size; | |
2115 | nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t)); | |
2116 | meta_size += nl1e * sizeof(uint64_t); | |
2117 | ||
2118 | /* total size of refcount blocks | |
2119 | * | |
2120 | * note: every host cluster is reference-counted, including metadata | |
2121 | * (even refcount blocks are recursively included). | |
2122 | * Let: | |
2123 | * a = total_size (this is the guest disk size) | |
2124 | * m = meta size not including refcount blocks and refcount tables | |
2125 | * c = cluster size | |
2126 | * y1 = number of refcount blocks entries | |
2127 | * y2 = meta size including everything | |
bd4b167f | 2128 | * rces = refcount entry size in bytes |
0e4271b7 HT |
2129 | * then, |
2130 | * y1 = (y2 + a)/c | |
bd4b167f | 2131 | * y2 = y1 * rces + y1 * rces * sizeof(u64) / c + m |
0e4271b7 | 2132 | * we can get y1: |
bd4b167f | 2133 | * y1 = (a + m) / (c - rces - rces * sizeof(u64) / c) |
0e4271b7 | 2134 | */ |
bd4b167f HR |
2135 | nrefblocke = (aligned_total_size + meta_size + cluster_size) |
2136 | / (cluster_size - rces - rces * sizeof(uint64_t) | |
2137 | / cluster_size); | |
2138 | meta_size += DIV_ROUND_UP(nrefblocke, refblock_size) * cluster_size; | |
0e4271b7 HT |
2139 | |
2140 | /* total size of refcount tables */ | |
bd4b167f | 2141 | nreftablee = nrefblocke / refblock_size; |
0e4271b7 HT |
2142 | nreftablee = align_offset(nreftablee, cluster_size / sizeof(uint64_t)); |
2143 | meta_size += nreftablee * sizeof(uint64_t); | |
2144 | ||
2145 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
39101f25 | 2146 | aligned_total_size + meta_size, &error_abort); |
f43e47db MA |
2147 | qemu_opt_set(opts, BLOCK_OPT_PREALLOC, PreallocMode_lookup[prealloc], |
2148 | &error_abort); | |
0e4271b7 HT |
2149 | } |
2150 | ||
c282e1fd | 2151 | ret = bdrv_create_file(filename, opts, &local_err); |
a9420734 | 2152 | if (ret < 0) { |
3ef6c40a | 2153 | error_propagate(errp, local_err); |
a9420734 KW |
2154 | return ret; |
2155 | } | |
2156 | ||
2e40134b HR |
2157 | bs = NULL; |
2158 | ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL, | |
6ebf9aa2 | 2159 | &local_err); |
a9420734 | 2160 | if (ret < 0) { |
3ef6c40a | 2161 | error_propagate(errp, local_err); |
a9420734 KW |
2162 | return ret; |
2163 | } | |
2164 | ||
2165 | /* Write the header */ | |
f8413b3c KW |
2166 | QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header)); |
2167 | header = g_malloc0(cluster_size); | |
2168 | *header = (QCowHeader) { | |
2169 | .magic = cpu_to_be32(QCOW_MAGIC), | |
2170 | .version = cpu_to_be32(version), | |
2171 | .cluster_bits = cpu_to_be32(cluster_bits), | |
2172 | .size = cpu_to_be64(0), | |
2173 | .l1_table_offset = cpu_to_be64(0), | |
2174 | .l1_size = cpu_to_be32(0), | |
2175 | .refcount_table_offset = cpu_to_be64(cluster_size), | |
2176 | .refcount_table_clusters = cpu_to_be32(1), | |
bd4b167f | 2177 | .refcount_order = cpu_to_be32(refcount_order), |
f8413b3c KW |
2178 | .header_length = cpu_to_be32(sizeof(*header)), |
2179 | }; | |
a9420734 KW |
2180 | |
2181 | if (flags & BLOCK_FLAG_ENCRYPT) { | |
f8413b3c | 2182 | header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
a9420734 | 2183 | } else { |
f8413b3c | 2184 | header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
a9420734 KW |
2185 | } |
2186 | ||
bfe8043e | 2187 | if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { |
f8413b3c | 2188 | header->compatible_features |= |
bfe8043e SH |
2189 | cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); |
2190 | } | |
2191 | ||
f8413b3c KW |
2192 | ret = bdrv_pwrite(bs, 0, header, cluster_size); |
2193 | g_free(header); | |
a9420734 | 2194 | if (ret < 0) { |
3ef6c40a | 2195 | error_setg_errno(errp, -ret, "Could not write qcow2 header"); |
a9420734 KW |
2196 | goto out; |
2197 | } | |
2198 | ||
b106ad91 KW |
2199 | /* Write a refcount table with one refcount block */ |
2200 | refcount_table = g_malloc0(2 * cluster_size); | |
2201 | refcount_table[0] = cpu_to_be64(2 * cluster_size); | |
2202 | ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size); | |
7267c094 | 2203 | g_free(refcount_table); |
a9420734 KW |
2204 | |
2205 | if (ret < 0) { | |
3ef6c40a | 2206 | error_setg_errno(errp, -ret, "Could not write refcount table"); |
a9420734 KW |
2207 | goto out; |
2208 | } | |
2209 | ||
f67503e5 HR |
2210 | bdrv_unref(bs); |
2211 | bs = NULL; | |
a9420734 KW |
2212 | |
2213 | /* | |
2214 | * And now open the image and make it consistent first (i.e. increase the | |
2215 | * refcount of the cluster that is occupied by the header and the refcount | |
2216 | * table) | |
2217 | */ | |
e6641719 HR |
2218 | options = qdict_new(); |
2219 | qdict_put(options, "driver", qstring_from_str("qcow2")); | |
2220 | ret = bdrv_open(&bs, filename, NULL, options, | |
ef810437 | 2221 | BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, |
6ebf9aa2 | 2222 | &local_err); |
a9420734 | 2223 | if (ret < 0) { |
3ef6c40a | 2224 | error_propagate(errp, local_err); |
a9420734 KW |
2225 | goto out; |
2226 | } | |
2227 | ||
b106ad91 | 2228 | ret = qcow2_alloc_clusters(bs, 3 * cluster_size); |
a9420734 | 2229 | if (ret < 0) { |
3ef6c40a HR |
2230 | error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " |
2231 | "header and refcount table"); | |
a9420734 KW |
2232 | goto out; |
2233 | ||
2234 | } else if (ret != 0) { | |
2235 | error_report("Huh, first cluster in empty image is already in use?"); | |
2236 | abort(); | |
2237 | } | |
2238 | ||
2239 | /* Okay, now that we have a valid image, let's give it the right size */ | |
180e9526 | 2240 | ret = bdrv_truncate(bs, total_size); |
a9420734 | 2241 | if (ret < 0) { |
3ef6c40a | 2242 | error_setg_errno(errp, -ret, "Could not resize image"); |
a9420734 KW |
2243 | goto out; |
2244 | } | |
2245 | ||
2246 | /* Want a backing file? There you go.*/ | |
2247 | if (backing_file) { | |
2248 | ret = bdrv_change_backing_file(bs, backing_file, backing_format); | |
2249 | if (ret < 0) { | |
3ef6c40a HR |
2250 | error_setg_errno(errp, -ret, "Could not assign backing file '%s' " |
2251 | "with format '%s'", backing_file, backing_format); | |
a9420734 KW |
2252 | goto out; |
2253 | } | |
2254 | } | |
2255 | ||
2256 | /* And if we're supposed to preallocate metadata, do that now */ | |
0e4271b7 | 2257 | if (prealloc != PREALLOC_MODE_OFF) { |
ff99129a | 2258 | BDRVQcow2State *s = bs->opaque; |
15552c4a | 2259 | qemu_co_mutex_lock(&s->lock); |
a9420734 | 2260 | ret = preallocate(bs); |
15552c4a | 2261 | qemu_co_mutex_unlock(&s->lock); |
a9420734 | 2262 | if (ret < 0) { |
3ef6c40a | 2263 | error_setg_errno(errp, -ret, "Could not preallocate metadata"); |
a9420734 KW |
2264 | goto out; |
2265 | } | |
2266 | } | |
2267 | ||
f67503e5 HR |
2268 | bdrv_unref(bs); |
2269 | bs = NULL; | |
ba2ab2f2 HR |
2270 | |
2271 | /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */ | |
e6641719 HR |
2272 | options = qdict_new(); |
2273 | qdict_put(options, "driver", qstring_from_str("qcow2")); | |
2274 | ret = bdrv_open(&bs, filename, NULL, options, | |
c9fbb99d | 2275 | BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, |
6ebf9aa2 | 2276 | &local_err); |
84d18f06 | 2277 | if (local_err) { |
ba2ab2f2 HR |
2278 | error_propagate(errp, local_err); |
2279 | goto out; | |
2280 | } | |
2281 | ||
a9420734 KW |
2282 | ret = 0; |
2283 | out: | |
f67503e5 HR |
2284 | if (bs) { |
2285 | bdrv_unref(bs); | |
2286 | } | |
a9420734 KW |
2287 | return ret; |
2288 | } | |
de5f3f40 | 2289 | |
1bd0e2d1 | 2290 | static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp) |
de5f3f40 | 2291 | { |
1bd0e2d1 CL |
2292 | char *backing_file = NULL; |
2293 | char *backing_fmt = NULL; | |
2294 | char *buf = NULL; | |
180e9526 | 2295 | uint64_t size = 0; |
de5f3f40 | 2296 | int flags = 0; |
99cce9fa | 2297 | size_t cluster_size = DEFAULT_CLUSTER_SIZE; |
ffeaac9b | 2298 | PreallocMode prealloc; |
8ad1898c | 2299 | int version = 3; |
bd4b167f HR |
2300 | uint64_t refcount_bits = 16; |
2301 | int refcount_order; | |
3ef6c40a HR |
2302 | Error *local_err = NULL; |
2303 | int ret; | |
de5f3f40 KW |
2304 | |
2305 | /* Read out options */ | |
180e9526 HT |
2306 | size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
2307 | BDRV_SECTOR_SIZE); | |
1bd0e2d1 CL |
2308 | backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
2309 | backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); | |
2310 | if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { | |
2311 | flags |= BLOCK_FLAG_ENCRYPT; | |
2312 | } | |
2313 | cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, | |
2314 | DEFAULT_CLUSTER_SIZE); | |
2315 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); | |
ffeaac9b | 2316 | prealloc = qapi_enum_parse(PreallocMode_lookup, buf, |
7fb1cf16 | 2317 | PREALLOC_MODE__MAX, PREALLOC_MODE_OFF, |
ffeaac9b HT |
2318 | &local_err); |
2319 | if (local_err) { | |
2320 | error_propagate(errp, local_err); | |
1bd0e2d1 CL |
2321 | ret = -EINVAL; |
2322 | goto finish; | |
2323 | } | |
2324 | g_free(buf); | |
2325 | buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL); | |
2326 | if (!buf) { | |
2327 | /* keep the default */ | |
2328 | } else if (!strcmp(buf, "0.10")) { | |
2329 | version = 2; | |
2330 | } else if (!strcmp(buf, "1.1")) { | |
2331 | version = 3; | |
2332 | } else { | |
2333 | error_setg(errp, "Invalid compatibility level: '%s'", buf); | |
2334 | ret = -EINVAL; | |
2335 | goto finish; | |
2336 | } | |
2337 | ||
2338 | if (qemu_opt_get_bool_del(opts, BLOCK_OPT_LAZY_REFCOUNTS, false)) { | |
2339 | flags |= BLOCK_FLAG_LAZY_REFCOUNTS; | |
de5f3f40 KW |
2340 | } |
2341 | ||
ffeaac9b | 2342 | if (backing_file && prealloc != PREALLOC_MODE_OFF) { |
3ef6c40a HR |
2343 | error_setg(errp, "Backing file and preallocation cannot be used at " |
2344 | "the same time"); | |
1bd0e2d1 CL |
2345 | ret = -EINVAL; |
2346 | goto finish; | |
de5f3f40 KW |
2347 | } |
2348 | ||
bfe8043e | 2349 | if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) { |
3ef6c40a HR |
2350 | error_setg(errp, "Lazy refcounts only supported with compatibility " |
2351 | "level 1.1 and above (use compat=1.1 or greater)"); | |
1bd0e2d1 CL |
2352 | ret = -EINVAL; |
2353 | goto finish; | |
bfe8043e SH |
2354 | } |
2355 | ||
06d05fa7 HR |
2356 | refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, |
2357 | refcount_bits); | |
2358 | if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) { | |
2359 | error_setg(errp, "Refcount width must be a power of two and may not " | |
2360 | "exceed 64 bits"); | |
2361 | ret = -EINVAL; | |
2362 | goto finish; | |
2363 | } | |
2364 | ||
bd4b167f HR |
2365 | if (version < 3 && refcount_bits != 16) { |
2366 | error_setg(errp, "Different refcount widths than 16 bits require " | |
2367 | "compatibility level 1.1 or above (use compat=1.1 or " | |
2368 | "greater)"); | |
2369 | ret = -EINVAL; | |
2370 | goto finish; | |
2371 | } | |
2372 | ||
786a4ea8 | 2373 | refcount_order = ctz32(refcount_bits); |
bd4b167f | 2374 | |
180e9526 | 2375 | ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags, |
bd4b167f HR |
2376 | cluster_size, prealloc, opts, version, refcount_order, |
2377 | &local_err); | |
84d18f06 | 2378 | if (local_err) { |
3ef6c40a HR |
2379 | error_propagate(errp, local_err); |
2380 | } | |
1bd0e2d1 CL |
2381 | |
2382 | finish: | |
2383 | g_free(backing_file); | |
2384 | g_free(backing_fmt); | |
2385 | g_free(buf); | |
3ef6c40a | 2386 | return ret; |
de5f3f40 KW |
2387 | } |
2388 | ||
621f0589 | 2389 | static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, |
aa7bfbff | 2390 | int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) |
621f0589 KW |
2391 | { |
2392 | int ret; | |
ff99129a | 2393 | BDRVQcow2State *s = bs->opaque; |
621f0589 KW |
2394 | |
2395 | /* Emulate misaligned zero writes */ | |
2396 | if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) { | |
2397 | return -ENOTSUP; | |
2398 | } | |
2399 | ||
2400 | /* Whatever is left can use real zero clusters */ | |
2401 | qemu_co_mutex_lock(&s->lock); | |
2402 | ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, | |
2403 | nb_sectors); | |
2404 | qemu_co_mutex_unlock(&s->lock); | |
2405 | ||
2406 | return ret; | |
2407 | } | |
2408 | ||
6db39ae2 PB |
2409 | static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, |
2410 | int64_t sector_num, int nb_sectors) | |
5ea929e3 | 2411 | { |
6db39ae2 | 2412 | int ret; |
ff99129a | 2413 | BDRVQcow2State *s = bs->opaque; |
6db39ae2 PB |
2414 | |
2415 | qemu_co_mutex_lock(&s->lock); | |
2416 | ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS, | |
808c4b6f | 2417 | nb_sectors, QCOW2_DISCARD_REQUEST, false); |
6db39ae2 PB |
2418 | qemu_co_mutex_unlock(&s->lock); |
2419 | return ret; | |
5ea929e3 KW |
2420 | } |
2421 | ||
419b19d9 SH |
2422 | static int qcow2_truncate(BlockDriverState *bs, int64_t offset) |
2423 | { | |
ff99129a | 2424 | BDRVQcow2State *s = bs->opaque; |
2cf7cfa1 KW |
2425 | int64_t new_l1_size; |
2426 | int ret; | |
419b19d9 SH |
2427 | |
2428 | if (offset & 511) { | |
259b2173 | 2429 | error_report("The new size must be a multiple of 512"); |
419b19d9 SH |
2430 | return -EINVAL; |
2431 | } | |
2432 | ||
2433 | /* cannot proceed if image has snapshots */ | |
2434 | if (s->nb_snapshots) { | |
259b2173 | 2435 | error_report("Can't resize an image which has snapshots"); |
419b19d9 SH |
2436 | return -ENOTSUP; |
2437 | } | |
2438 | ||
2439 | /* shrinking is currently not supported */ | |
2440 | if (offset < bs->total_sectors * 512) { | |
259b2173 | 2441 | error_report("qcow2 doesn't support shrinking images yet"); |
419b19d9 SH |
2442 | return -ENOTSUP; |
2443 | } | |
2444 | ||
2445 | new_l1_size = size_to_l1(s, offset); | |
72893756 | 2446 | ret = qcow2_grow_l1_table(bs, new_l1_size, true); |
419b19d9 SH |
2447 | if (ret < 0) { |
2448 | return ret; | |
2449 | } | |
2450 | ||
2451 | /* write updated header.size */ | |
2452 | offset = cpu_to_be64(offset); | |
9a4f4c31 | 2453 | ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, size), |
8b3b7206 | 2454 | &offset, sizeof(uint64_t)); |
419b19d9 SH |
2455 | if (ret < 0) { |
2456 | return ret; | |
2457 | } | |
2458 | ||
2459 | s->l1_vm_state_index = new_l1_size; | |
2460 | return 0; | |
2461 | } | |
2462 | ||
20d97356 BS |
2463 | /* XXX: put compressed sectors first, then all the cluster aligned |
2464 | tables to avoid losing bytes in alignment */ | |
7c80ab3f JS |
2465 | static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, |
2466 | const uint8_t *buf, int nb_sectors) | |
20d97356 | 2467 | { |
ff99129a | 2468 | BDRVQcow2State *s = bs->opaque; |
20d97356 BS |
2469 | z_stream strm; |
2470 | int ret, out_len; | |
2471 | uint8_t *out_buf; | |
2472 | uint64_t cluster_offset; | |
2473 | ||
2474 | if (nb_sectors == 0) { | |
2475 | /* align end of file to a sector boundary to ease reading with | |
2476 | sector based I/Os */ | |
9a4f4c31 KW |
2477 | cluster_offset = bdrv_getlength(bs->file->bs); |
2478 | return bdrv_truncate(bs->file->bs, cluster_offset); | |
20d97356 BS |
2479 | } |
2480 | ||
f4d38bef SH |
2481 | if (nb_sectors != s->cluster_sectors) { |
2482 | ret = -EINVAL; | |
2483 | ||
2484 | /* Zero-pad last write if image size is not cluster aligned */ | |
2485 | if (sector_num + nb_sectors == bs->total_sectors && | |
2486 | nb_sectors < s->cluster_sectors) { | |
2487 | uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); | |
2488 | memset(pad_buf, 0, s->cluster_size); | |
2489 | memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); | |
2490 | ret = qcow2_write_compressed(bs, sector_num, | |
2491 | pad_buf, s->cluster_sectors); | |
2492 | qemu_vfree(pad_buf); | |
2493 | } | |
2494 | return ret; | |
2495 | } | |
20d97356 | 2496 | |
7267c094 | 2497 | out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
20d97356 BS |
2498 | |
2499 | /* best compression, small window, no zlib header */ | |
2500 | memset(&strm, 0, sizeof(strm)); | |
2501 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
2502 | Z_DEFLATED, -12, | |
2503 | 9, Z_DEFAULT_STRATEGY); | |
2504 | if (ret != 0) { | |
8f1efd00 KW |
2505 | ret = -EINVAL; |
2506 | goto fail; | |
20d97356 BS |
2507 | } |
2508 | ||
2509 | strm.avail_in = s->cluster_size; | |
2510 | strm.next_in = (uint8_t *)buf; | |
2511 | strm.avail_out = s->cluster_size; | |
2512 | strm.next_out = out_buf; | |
2513 | ||
2514 | ret = deflate(&strm, Z_FINISH); | |
2515 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
20d97356 | 2516 | deflateEnd(&strm); |
8f1efd00 KW |
2517 | ret = -EINVAL; |
2518 | goto fail; | |
20d97356 BS |
2519 | } |
2520 | out_len = strm.next_out - out_buf; | |
2521 | ||
2522 | deflateEnd(&strm); | |
2523 | ||
2524 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
2525 | /* could not compress: write normal cluster */ | |
8f1efd00 KW |
2526 | ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
2527 | if (ret < 0) { | |
2528 | goto fail; | |
2529 | } | |
20d97356 BS |
2530 | } else { |
2531 | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, | |
2532 | sector_num << 9, out_len); | |
8f1efd00 KW |
2533 | if (!cluster_offset) { |
2534 | ret = -EIO; | |
2535 | goto fail; | |
2536 | } | |
20d97356 | 2537 | cluster_offset &= s->cluster_offset_mask; |
cf93980e | 2538 | |
231bb267 | 2539 | ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len); |
cf93980e HR |
2540 | if (ret < 0) { |
2541 | goto fail; | |
2542 | } | |
2543 | ||
66f82cee | 2544 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); |
9a4f4c31 | 2545 | ret = bdrv_pwrite(bs->file->bs, cluster_offset, out_buf, out_len); |
8f1efd00 KW |
2546 | if (ret < 0) { |
2547 | goto fail; | |
20d97356 BS |
2548 | } |
2549 | } | |
2550 | ||
8f1efd00 KW |
2551 | ret = 0; |
2552 | fail: | |
7267c094 | 2553 | g_free(out_buf); |
8f1efd00 | 2554 | return ret; |
20d97356 BS |
2555 | } |
2556 | ||
94054183 HR |
2557 | static int make_completely_empty(BlockDriverState *bs) |
2558 | { | |
ff99129a | 2559 | BDRVQcow2State *s = bs->opaque; |
94054183 HR |
2560 | int ret, l1_clusters; |
2561 | int64_t offset; | |
2562 | uint64_t *new_reftable = NULL; | |
2563 | uint64_t rt_entry, l1_size2; | |
2564 | struct { | |
2565 | uint64_t l1_offset; | |
2566 | uint64_t reftable_offset; | |
2567 | uint32_t reftable_clusters; | |
2568 | } QEMU_PACKED l1_ofs_rt_ofs_cls; | |
2569 | ||
2570 | ret = qcow2_cache_empty(bs, s->l2_table_cache); | |
2571 | if (ret < 0) { | |
2572 | goto fail; | |
2573 | } | |
2574 | ||
2575 | ret = qcow2_cache_empty(bs, s->refcount_block_cache); | |
2576 | if (ret < 0) { | |
2577 | goto fail; | |
2578 | } | |
2579 | ||
2580 | /* Refcounts will be broken utterly */ | |
2581 | ret = qcow2_mark_dirty(bs); | |
2582 | if (ret < 0) { | |
2583 | goto fail; | |
2584 | } | |
2585 | ||
2586 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); | |
2587 | ||
2588 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t)); | |
2589 | l1_size2 = (uint64_t)s->l1_size * sizeof(uint64_t); | |
2590 | ||
2591 | /* After this call, neither the in-memory nor the on-disk refcount | |
2592 | * information accurately describe the actual references */ | |
2593 | ||
9a4f4c31 | 2594 | ret = bdrv_write_zeroes(bs->file->bs, s->l1_table_offset / BDRV_SECTOR_SIZE, |
94054183 HR |
2595 | l1_clusters * s->cluster_sectors, 0); |
2596 | if (ret < 0) { | |
2597 | goto fail_broken_refcounts; | |
2598 | } | |
2599 | memset(s->l1_table, 0, l1_size2); | |
2600 | ||
2601 | BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE); | |
2602 | ||
2603 | /* Overwrite enough clusters at the beginning of the sectors to place | |
2604 | * the refcount table, a refcount block and the L1 table in; this may | |
2605 | * overwrite parts of the existing refcount and L1 table, which is not | |
2606 | * an issue because the dirty flag is set, complete data loss is in fact | |
2607 | * desired and partial data loss is consequently fine as well */ | |
9a4f4c31 | 2608 | ret = bdrv_write_zeroes(bs->file->bs, s->cluster_size / BDRV_SECTOR_SIZE, |
94054183 HR |
2609 | (2 + l1_clusters) * s->cluster_size / |
2610 | BDRV_SECTOR_SIZE, 0); | |
2611 | /* This call (even if it failed overall) may have overwritten on-disk | |
2612 | * refcount structures; in that case, the in-memory refcount information | |
2613 | * will probably differ from the on-disk information which makes the BDS | |
2614 | * unusable */ | |
2615 | if (ret < 0) { | |
2616 | goto fail_broken_refcounts; | |
2617 | } | |
2618 | ||
2619 | BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE); | |
2620 | BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE); | |
2621 | ||
2622 | /* "Create" an empty reftable (one cluster) directly after the image | |
2623 | * header and an empty L1 table three clusters after the image header; | |
2624 | * the cluster between those two will be used as the first refblock */ | |
2625 | cpu_to_be64w(&l1_ofs_rt_ofs_cls.l1_offset, 3 * s->cluster_size); | |
2626 | cpu_to_be64w(&l1_ofs_rt_ofs_cls.reftable_offset, s->cluster_size); | |
2627 | cpu_to_be32w(&l1_ofs_rt_ofs_cls.reftable_clusters, 1); | |
9a4f4c31 | 2628 | ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, l1_table_offset), |
94054183 HR |
2629 | &l1_ofs_rt_ofs_cls, sizeof(l1_ofs_rt_ofs_cls)); |
2630 | if (ret < 0) { | |
2631 | goto fail_broken_refcounts; | |
2632 | } | |
2633 | ||
2634 | s->l1_table_offset = 3 * s->cluster_size; | |
2635 | ||
2636 | new_reftable = g_try_new0(uint64_t, s->cluster_size / sizeof(uint64_t)); | |
2637 | if (!new_reftable) { | |
2638 | ret = -ENOMEM; | |
2639 | goto fail_broken_refcounts; | |
2640 | } | |
2641 | ||
2642 | s->refcount_table_offset = s->cluster_size; | |
2643 | s->refcount_table_size = s->cluster_size / sizeof(uint64_t); | |
2644 | ||
2645 | g_free(s->refcount_table); | |
2646 | s->refcount_table = new_reftable; | |
2647 | new_reftable = NULL; | |
2648 | ||
2649 | /* Now the in-memory refcount information again corresponds to the on-disk | |
2650 | * information (reftable is empty and no refblocks (the refblock cache is | |
2651 | * empty)); however, this means some clusters (e.g. the image header) are | |
2652 | * referenced, but not refcounted, but the normal qcow2 code assumes that | |
2653 | * the in-memory information is always correct */ | |
2654 | ||
2655 | BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC); | |
2656 | ||
2657 | /* Enter the first refblock into the reftable */ | |
2658 | rt_entry = cpu_to_be64(2 * s->cluster_size); | |
9a4f4c31 | 2659 | ret = bdrv_pwrite_sync(bs->file->bs, s->cluster_size, |
94054183 HR |
2660 | &rt_entry, sizeof(rt_entry)); |
2661 | if (ret < 0) { | |
2662 | goto fail_broken_refcounts; | |
2663 | } | |
2664 | s->refcount_table[0] = 2 * s->cluster_size; | |
2665 | ||
2666 | s->free_cluster_index = 0; | |
2667 | assert(3 + l1_clusters <= s->refcount_block_size); | |
2668 | offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2); | |
2669 | if (offset < 0) { | |
2670 | ret = offset; | |
2671 | goto fail_broken_refcounts; | |
2672 | } else if (offset > 0) { | |
2673 | error_report("First cluster in emptied image is in use"); | |
2674 | abort(); | |
2675 | } | |
2676 | ||
2677 | /* Now finally the in-memory information corresponds to the on-disk | |
2678 | * structures and is correct */ | |
2679 | ret = qcow2_mark_clean(bs); | |
2680 | if (ret < 0) { | |
2681 | goto fail; | |
2682 | } | |
2683 | ||
9a4f4c31 | 2684 | ret = bdrv_truncate(bs->file->bs, (3 + l1_clusters) * s->cluster_size); |
94054183 HR |
2685 | if (ret < 0) { |
2686 | goto fail; | |
2687 | } | |
2688 | ||
2689 | return 0; | |
2690 | ||
2691 | fail_broken_refcounts: | |
2692 | /* The BDS is unusable at this point. If we wanted to make it usable, we | |
2693 | * would have to call qcow2_refcount_close(), qcow2_refcount_init(), | |
2694 | * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init() | |
2695 | * again. However, because the functions which could have caused this error | |
2696 | * path to be taken are used by those functions as well, it's very likely | |
2697 | * that that sequence will fail as well. Therefore, just eject the BDS. */ | |
2698 | bs->drv = NULL; | |
2699 | ||
2700 | fail: | |
2701 | g_free(new_reftable); | |
2702 | return ret; | |
2703 | } | |
2704 | ||
491d27e2 HR |
2705 | static int qcow2_make_empty(BlockDriverState *bs) |
2706 | { | |
ff99129a | 2707 | BDRVQcow2State *s = bs->opaque; |
491d27e2 HR |
2708 | uint64_t start_sector; |
2709 | int sector_step = INT_MAX / BDRV_SECTOR_SIZE; | |
94054183 HR |
2710 | int l1_clusters, ret = 0; |
2711 | ||
2712 | l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t)); | |
2713 | ||
2714 | if (s->qcow_version >= 3 && !s->snapshots && | |
2715 | 3 + l1_clusters <= s->refcount_block_size) { | |
2716 | /* The following function only works for qcow2 v3 images (it requires | |
2717 | * the dirty flag) and only as long as there are no snapshots (because | |
2718 | * it completely empties the image). Furthermore, the L1 table and three | |
2719 | * additional clusters (image header, refcount table, one refcount | |
2720 | * block) have to fit inside one refcount block. */ | |
2721 | return make_completely_empty(bs); | |
2722 | } | |
491d27e2 | 2723 | |
94054183 HR |
2724 | /* This fallback code simply discards every active cluster; this is slow, |
2725 | * but works in all cases */ | |
491d27e2 HR |
2726 | for (start_sector = 0; start_sector < bs->total_sectors; |
2727 | start_sector += sector_step) | |
2728 | { | |
2729 | /* As this function is generally used after committing an external | |
2730 | * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the | |
2731 | * default action for this kind of discard is to pass the discard, | |
2732 | * which will ideally result in an actually smaller image file, as | |
2733 | * is probably desired. */ | |
2734 | ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE, | |
2735 | MIN(sector_step, | |
2736 | bs->total_sectors - start_sector), | |
2737 | QCOW2_DISCARD_SNAPSHOT, true); | |
2738 | if (ret < 0) { | |
2739 | break; | |
2740 | } | |
2741 | } | |
2742 | ||
2743 | return ret; | |
2744 | } | |
2745 | ||
a968168c | 2746 | static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) |
20d97356 | 2747 | { |
ff99129a | 2748 | BDRVQcow2State *s = bs->opaque; |
29c1a730 KW |
2749 | int ret; |
2750 | ||
8b94ff85 | 2751 | qemu_co_mutex_lock(&s->lock); |
29c1a730 KW |
2752 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
2753 | if (ret < 0) { | |
c95de7e2 | 2754 | qemu_co_mutex_unlock(&s->lock); |
8b94ff85 | 2755 | return ret; |
29c1a730 KW |
2756 | } |
2757 | ||
bfe8043e SH |
2758 | if (qcow2_need_accurate_refcounts(s)) { |
2759 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
2760 | if (ret < 0) { | |
2761 | qemu_co_mutex_unlock(&s->lock); | |
2762 | return ret; | |
2763 | } | |
29c1a730 | 2764 | } |
8b94ff85 | 2765 | qemu_co_mutex_unlock(&s->lock); |
29c1a730 | 2766 | |
eb489bb1 KW |
2767 | return 0; |
2768 | } | |
2769 | ||
7c80ab3f | 2770 | static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
20d97356 | 2771 | { |
ff99129a | 2772 | BDRVQcow2State *s = bs->opaque; |
95de6d70 PB |
2773 | bdi->unallocated_blocks_are_zero = true; |
2774 | bdi->can_write_zeroes_with_unmap = (s->qcow_version >= 3); | |
20d97356 | 2775 | bdi->cluster_size = s->cluster_size; |
7c80ab3f | 2776 | bdi->vm_state_offset = qcow2_vm_state_offset(s); |
20d97356 BS |
2777 | return 0; |
2778 | } | |
2779 | ||
37764dfb HR |
2780 | static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) |
2781 | { | |
ff99129a | 2782 | BDRVQcow2State *s = bs->opaque; |
37764dfb HR |
2783 | ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1); |
2784 | ||
2785 | *spec_info = (ImageInfoSpecific){ | |
6a8f9661 EB |
2786 | .type = IMAGE_INFO_SPECIFIC_KIND_QCOW2, |
2787 | .u.qcow2 = g_new(ImageInfoSpecificQCow2, 1), | |
37764dfb HR |
2788 | }; |
2789 | if (s->qcow_version == 2) { | |
6a8f9661 | 2790 | *spec_info->u.qcow2 = (ImageInfoSpecificQCow2){ |
0709c5a1 HR |
2791 | .compat = g_strdup("0.10"), |
2792 | .refcount_bits = s->refcount_bits, | |
37764dfb HR |
2793 | }; |
2794 | } else if (s->qcow_version == 3) { | |
6a8f9661 | 2795 | *spec_info->u.qcow2 = (ImageInfoSpecificQCow2){ |
37764dfb HR |
2796 | .compat = g_strdup("1.1"), |
2797 | .lazy_refcounts = s->compatible_features & | |
2798 | QCOW2_COMPAT_LAZY_REFCOUNTS, | |
2799 | .has_lazy_refcounts = true, | |
9009b196 HR |
2800 | .corrupt = s->incompatible_features & |
2801 | QCOW2_INCOMPAT_CORRUPT, | |
2802 | .has_corrupt = true, | |
0709c5a1 | 2803 | .refcount_bits = s->refcount_bits, |
37764dfb | 2804 | }; |
b1fc8f93 DL |
2805 | } else { |
2806 | /* if this assertion fails, this probably means a new version was | |
2807 | * added without having it covered here */ | |
2808 | assert(false); | |
37764dfb HR |
2809 | } |
2810 | ||
2811 | return spec_info; | |
2812 | } | |
2813 | ||
20d97356 BS |
2814 | #if 0 |
2815 | static void dump_refcounts(BlockDriverState *bs) | |
2816 | { | |
ff99129a | 2817 | BDRVQcow2State *s = bs->opaque; |
20d97356 BS |
2818 | int64_t nb_clusters, k, k1, size; |
2819 | int refcount; | |
2820 | ||
9a4f4c31 | 2821 | size = bdrv_getlength(bs->file->bs); |
20d97356 BS |
2822 | nb_clusters = size_to_clusters(s, size); |
2823 | for(k = 0; k < nb_clusters;) { | |
2824 | k1 = k; | |
2825 | refcount = get_refcount(bs, k); | |
2826 | k++; | |
2827 | while (k < nb_clusters && get_refcount(bs, k) == refcount) | |
2828 | k++; | |
0bfcd599 BS |
2829 | printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, |
2830 | k - k1); | |
20d97356 BS |
2831 | } |
2832 | } | |
2833 | #endif | |
2834 | ||
cf8074b3 KW |
2835 | static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
2836 | int64_t pos) | |
20d97356 | 2837 | { |
ff99129a | 2838 | BDRVQcow2State *s = bs->opaque; |
eedff66f | 2839 | int64_t total_sectors = bs->total_sectors; |
6e13610a | 2840 | bool zero_beyond_eof = bs->zero_beyond_eof; |
20d97356 BS |
2841 | int ret; |
2842 | ||
66f82cee | 2843 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
6e13610a | 2844 | bs->zero_beyond_eof = false; |
8d3b1a2d | 2845 | ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov); |
6e13610a | 2846 | bs->zero_beyond_eof = zero_beyond_eof; |
20d97356 | 2847 | |
eedff66f HR |
2848 | /* bdrv_co_do_writev will have increased the total_sectors value to include |
2849 | * the VM state - the VM state is however not an actual part of the block | |
2850 | * device, therefore, we need to restore the old value. */ | |
2851 | bs->total_sectors = total_sectors; | |
2852 | ||
20d97356 BS |
2853 | return ret; |
2854 | } | |
2855 | ||
7c80ab3f JS |
2856 | static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
2857 | int64_t pos, int size) | |
20d97356 | 2858 | { |
ff99129a | 2859 | BDRVQcow2State *s = bs->opaque; |
0d51b4de | 2860 | bool zero_beyond_eof = bs->zero_beyond_eof; |
20d97356 BS |
2861 | int ret; |
2862 | ||
66f82cee | 2863 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
0d51b4de | 2864 | bs->zero_beyond_eof = false; |
7c80ab3f | 2865 | ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size); |
0d51b4de | 2866 | bs->zero_beyond_eof = zero_beyond_eof; |
20d97356 BS |
2867 | |
2868 | return ret; | |
2869 | } | |
2870 | ||
9296b3ed HR |
2871 | /* |
2872 | * Downgrades an image's version. To achieve this, any incompatible features | |
2873 | * have to be removed. | |
2874 | */ | |
4057a2b2 | 2875 | static int qcow2_downgrade(BlockDriverState *bs, int target_version, |
8b13976d | 2876 | BlockDriverAmendStatusCB *status_cb, void *cb_opaque) |
9296b3ed | 2877 | { |
ff99129a | 2878 | BDRVQcow2State *s = bs->opaque; |
9296b3ed HR |
2879 | int current_version = s->qcow_version; |
2880 | int ret; | |
2881 | ||
2882 | if (target_version == current_version) { | |
2883 | return 0; | |
2884 | } else if (target_version > current_version) { | |
2885 | return -EINVAL; | |
2886 | } else if (target_version != 2) { | |
2887 | return -EINVAL; | |
2888 | } | |
2889 | ||
2890 | if (s->refcount_order != 4) { | |
61ce55fc | 2891 | error_report("compat=0.10 requires refcount_bits=16"); |
9296b3ed HR |
2892 | return -ENOTSUP; |
2893 | } | |
2894 | ||
2895 | /* clear incompatible features */ | |
2896 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
2897 | ret = qcow2_mark_clean(bs); | |
2898 | if (ret < 0) { | |
2899 | return ret; | |
2900 | } | |
2901 | } | |
2902 | ||
2903 | /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in | |
2904 | * the first place; if that happens nonetheless, returning -ENOTSUP is the | |
2905 | * best thing to do anyway */ | |
2906 | ||
2907 | if (s->incompatible_features) { | |
2908 | return -ENOTSUP; | |
2909 | } | |
2910 | ||
2911 | /* since we can ignore compatible features, we can set them to 0 as well */ | |
2912 | s->compatible_features = 0; | |
2913 | /* if lazy refcounts have been used, they have already been fixed through | |
2914 | * clearing the dirty flag */ | |
2915 | ||
2916 | /* clearing autoclear features is trivial */ | |
2917 | s->autoclear_features = 0; | |
2918 | ||
8b13976d | 2919 | ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque); |
9296b3ed HR |
2920 | if (ret < 0) { |
2921 | return ret; | |
2922 | } | |
2923 | ||
2924 | s->qcow_version = target_version; | |
2925 | ret = qcow2_update_header(bs); | |
2926 | if (ret < 0) { | |
2927 | s->qcow_version = current_version; | |
2928 | return ret; | |
2929 | } | |
2930 | return 0; | |
2931 | } | |
2932 | ||
c293a809 HR |
2933 | typedef enum Qcow2AmendOperation { |
2934 | /* This is the value Qcow2AmendHelperCBInfo::last_operation will be | |
2935 | * statically initialized to so that the helper CB can discern the first | |
2936 | * invocation from an operation change */ | |
2937 | QCOW2_NO_OPERATION = 0, | |
2938 | ||
61ce55fc | 2939 | QCOW2_CHANGING_REFCOUNT_ORDER, |
c293a809 HR |
2940 | QCOW2_DOWNGRADING, |
2941 | } Qcow2AmendOperation; | |
2942 | ||
2943 | typedef struct Qcow2AmendHelperCBInfo { | |
2944 | /* The code coordinating the amend operations should only modify | |
2945 | * these four fields; the rest will be managed by the CB */ | |
2946 | BlockDriverAmendStatusCB *original_status_cb; | |
2947 | void *original_cb_opaque; | |
2948 | ||
2949 | Qcow2AmendOperation current_operation; | |
2950 | ||
2951 | /* Total number of operations to perform (only set once) */ | |
2952 | int total_operations; | |
2953 | ||
2954 | /* The following fields are managed by the CB */ | |
2955 | ||
2956 | /* Number of operations completed */ | |
2957 | int operations_completed; | |
2958 | ||
2959 | /* Cumulative offset of all completed operations */ | |
2960 | int64_t offset_completed; | |
2961 | ||
2962 | Qcow2AmendOperation last_operation; | |
2963 | int64_t last_work_size; | |
2964 | } Qcow2AmendHelperCBInfo; | |
2965 | ||
2966 | static void qcow2_amend_helper_cb(BlockDriverState *bs, | |
2967 | int64_t operation_offset, | |
2968 | int64_t operation_work_size, void *opaque) | |
2969 | { | |
2970 | Qcow2AmendHelperCBInfo *info = opaque; | |
2971 | int64_t current_work_size; | |
2972 | int64_t projected_work_size; | |
2973 | ||
2974 | if (info->current_operation != info->last_operation) { | |
2975 | if (info->last_operation != QCOW2_NO_OPERATION) { | |
2976 | info->offset_completed += info->last_work_size; | |
2977 | info->operations_completed++; | |
2978 | } | |
2979 | ||
2980 | info->last_operation = info->current_operation; | |
2981 | } | |
2982 | ||
2983 | assert(info->total_operations > 0); | |
2984 | assert(info->operations_completed < info->total_operations); | |
2985 | ||
2986 | info->last_work_size = operation_work_size; | |
2987 | ||
2988 | current_work_size = info->offset_completed + operation_work_size; | |
2989 | ||
2990 | /* current_work_size is the total work size for (operations_completed + 1) | |
2991 | * operations (which includes this one), so multiply it by the number of | |
2992 | * operations not covered and divide it by the number of operations | |
2993 | * covered to get a projection for the operations not covered */ | |
2994 | projected_work_size = current_work_size * (info->total_operations - | |
2995 | info->operations_completed - 1) | |
2996 | / (info->operations_completed + 1); | |
2997 | ||
2998 | info->original_status_cb(bs, info->offset_completed + operation_offset, | |
2999 | current_work_size + projected_work_size, | |
3000 | info->original_cb_opaque); | |
3001 | } | |
3002 | ||
77485434 | 3003 | static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts, |
8b13976d HR |
3004 | BlockDriverAmendStatusCB *status_cb, |
3005 | void *cb_opaque) | |
9296b3ed | 3006 | { |
ff99129a | 3007 | BDRVQcow2State *s = bs->opaque; |
9296b3ed HR |
3008 | int old_version = s->qcow_version, new_version = old_version; |
3009 | uint64_t new_size = 0; | |
3010 | const char *backing_file = NULL, *backing_format = NULL; | |
3011 | bool lazy_refcounts = s->use_lazy_refcounts; | |
1bd0e2d1 CL |
3012 | const char *compat = NULL; |
3013 | uint64_t cluster_size = s->cluster_size; | |
3014 | bool encrypt; | |
61ce55fc | 3015 | int refcount_bits = s->refcount_bits; |
9296b3ed | 3016 | int ret; |
1bd0e2d1 | 3017 | QemuOptDesc *desc = opts->list->desc; |
c293a809 | 3018 | Qcow2AmendHelperCBInfo helper_cb_info; |
9296b3ed | 3019 | |
1bd0e2d1 CL |
3020 | while (desc && desc->name) { |
3021 | if (!qemu_opt_find(opts, desc->name)) { | |
9296b3ed | 3022 | /* only change explicitly defined options */ |
1bd0e2d1 | 3023 | desc++; |
9296b3ed HR |
3024 | continue; |
3025 | } | |
3026 | ||
8a17b83c HR |
3027 | if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) { |
3028 | compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL); | |
1bd0e2d1 | 3029 | if (!compat) { |
9296b3ed | 3030 | /* preserve default */ |
1bd0e2d1 | 3031 | } else if (!strcmp(compat, "0.10")) { |
9296b3ed | 3032 | new_version = 2; |
1bd0e2d1 | 3033 | } else if (!strcmp(compat, "1.1")) { |
9296b3ed HR |
3034 | new_version = 3; |
3035 | } else { | |
29d72431 | 3036 | error_report("Unknown compatibility level %s", compat); |
9296b3ed HR |
3037 | return -EINVAL; |
3038 | } | |
8a17b83c | 3039 | } else if (!strcmp(desc->name, BLOCK_OPT_PREALLOC)) { |
29d72431 | 3040 | error_report("Cannot change preallocation mode"); |
9296b3ed | 3041 | return -ENOTSUP; |
8a17b83c HR |
3042 | } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) { |
3043 | new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); | |
3044 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) { | |
3045 | backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); | |
3046 | } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) { | |
3047 | backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); | |
3048 | } else if (!strcmp(desc->name, BLOCK_OPT_ENCRYPT)) { | |
3049 | encrypt = qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, | |
f6fa64f6 DB |
3050 | !!s->cipher); |
3051 | ||
3052 | if (encrypt != !!s->cipher) { | |
29d72431 | 3053 | error_report("Changing the encryption flag is not supported"); |
9296b3ed HR |
3054 | return -ENOTSUP; |
3055 | } | |
8a17b83c HR |
3056 | } else if (!strcmp(desc->name, BLOCK_OPT_CLUSTER_SIZE)) { |
3057 | cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, | |
1bd0e2d1 CL |
3058 | cluster_size); |
3059 | if (cluster_size != s->cluster_size) { | |
29d72431 | 3060 | error_report("Changing the cluster size is not supported"); |
9296b3ed HR |
3061 | return -ENOTSUP; |
3062 | } | |
8a17b83c HR |
3063 | } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) { |
3064 | lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS, | |
1bd0e2d1 | 3065 | lazy_refcounts); |
06d05fa7 | 3066 | } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) { |
61ce55fc HR |
3067 | refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS, |
3068 | refcount_bits); | |
3069 | ||
3070 | if (refcount_bits <= 0 || refcount_bits > 64 || | |
3071 | !is_power_of_2(refcount_bits)) | |
3072 | { | |
3073 | error_report("Refcount width must be a power of two and may " | |
3074 | "not exceed 64 bits"); | |
3075 | return -EINVAL; | |
3076 | } | |
9296b3ed | 3077 | } else { |
164e0f89 | 3078 | /* if this point is reached, this probably means a new option was |
9296b3ed | 3079 | * added without having it covered here */ |
164e0f89 | 3080 | abort(); |
9296b3ed | 3081 | } |
1bd0e2d1 CL |
3082 | |
3083 | desc++; | |
9296b3ed HR |
3084 | } |
3085 | ||
c293a809 HR |
3086 | helper_cb_info = (Qcow2AmendHelperCBInfo){ |
3087 | .original_status_cb = status_cb, | |
3088 | .original_cb_opaque = cb_opaque, | |
3089 | .total_operations = (new_version < old_version) | |
61ce55fc | 3090 | + (s->refcount_bits != refcount_bits) |
c293a809 HR |
3091 | }; |
3092 | ||
1038bbb8 HR |
3093 | /* Upgrade first (some features may require compat=1.1) */ |
3094 | if (new_version > old_version) { | |
3095 | s->qcow_version = new_version; | |
3096 | ret = qcow2_update_header(bs); | |
3097 | if (ret < 0) { | |
3098 | s->qcow_version = old_version; | |
3099 | return ret; | |
9296b3ed HR |
3100 | } |
3101 | } | |
3102 | ||
61ce55fc HR |
3103 | if (s->refcount_bits != refcount_bits) { |
3104 | int refcount_order = ctz32(refcount_bits); | |
3105 | Error *local_error = NULL; | |
3106 | ||
3107 | if (new_version < 3 && refcount_bits != 16) { | |
3108 | error_report("Different refcount widths than 16 bits require " | |
3109 | "compatibility level 1.1 or above (use compat=1.1 or " | |
3110 | "greater)"); | |
3111 | return -EINVAL; | |
3112 | } | |
3113 | ||
3114 | helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER; | |
3115 | ret = qcow2_change_refcount_order(bs, refcount_order, | |
3116 | &qcow2_amend_helper_cb, | |
3117 | &helper_cb_info, &local_error); | |
3118 | if (ret < 0) { | |
3119 | error_report_err(local_error); | |
3120 | return ret; | |
3121 | } | |
3122 | } | |
3123 | ||
9296b3ed | 3124 | if (backing_file || backing_format) { |
e4603fe1 KW |
3125 | ret = qcow2_change_backing_file(bs, |
3126 | backing_file ?: s->image_backing_file, | |
3127 | backing_format ?: s->image_backing_format); | |
9296b3ed HR |
3128 | if (ret < 0) { |
3129 | return ret; | |
3130 | } | |
3131 | } | |
3132 | ||
3133 | if (s->use_lazy_refcounts != lazy_refcounts) { | |
3134 | if (lazy_refcounts) { | |
1038bbb8 | 3135 | if (new_version < 3) { |
29d72431 HR |
3136 | error_report("Lazy refcounts only supported with compatibility " |
3137 | "level 1.1 and above (use compat=1.1 or greater)"); | |
9296b3ed HR |
3138 | return -EINVAL; |
3139 | } | |
3140 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; | |
3141 | ret = qcow2_update_header(bs); | |
3142 | if (ret < 0) { | |
3143 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; | |
3144 | return ret; | |
3145 | } | |
3146 | s->use_lazy_refcounts = true; | |
3147 | } else { | |
3148 | /* make image clean first */ | |
3149 | ret = qcow2_mark_clean(bs); | |
3150 | if (ret < 0) { | |
3151 | return ret; | |
3152 | } | |
3153 | /* now disallow lazy refcounts */ | |
3154 | s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; | |
3155 | ret = qcow2_update_header(bs); | |
3156 | if (ret < 0) { | |
3157 | s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; | |
3158 | return ret; | |
3159 | } | |
3160 | s->use_lazy_refcounts = false; | |
3161 | } | |
3162 | } | |
3163 | ||
3164 | if (new_size) { | |
3165 | ret = bdrv_truncate(bs, new_size); | |
3166 | if (ret < 0) { | |
3167 | return ret; | |
3168 | } | |
3169 | } | |
3170 | ||
1038bbb8 HR |
3171 | /* Downgrade last (so unsupported features can be removed before) */ |
3172 | if (new_version < old_version) { | |
c293a809 HR |
3173 | helper_cb_info.current_operation = QCOW2_DOWNGRADING; |
3174 | ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb, | |
3175 | &helper_cb_info); | |
1038bbb8 HR |
3176 | if (ret < 0) { |
3177 | return ret; | |
3178 | } | |
3179 | } | |
3180 | ||
9296b3ed HR |
3181 | return 0; |
3182 | } | |
3183 | ||
85186ebd HR |
3184 | /* |
3185 | * If offset or size are negative, respectively, they will not be included in | |
3186 | * the BLOCK_IMAGE_CORRUPTED event emitted. | |
3187 | * fatal will be ignored for read-only BDS; corruptions found there will always | |
3188 | * be considered non-fatal. | |
3189 | */ | |
3190 | void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset, | |
3191 | int64_t size, const char *message_format, ...) | |
3192 | { | |
ff99129a | 3193 | BDRVQcow2State *s = bs->opaque; |
dc881b44 | 3194 | const char *node_name; |
85186ebd HR |
3195 | char *message; |
3196 | va_list ap; | |
3197 | ||
3198 | fatal = fatal && !bs->read_only; | |
3199 | ||
3200 | if (s->signaled_corruption && | |
3201 | (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT))) | |
3202 | { | |
3203 | return; | |
3204 | } | |
3205 | ||
3206 | va_start(ap, message_format); | |
3207 | message = g_strdup_vprintf(message_format, ap); | |
3208 | va_end(ap); | |
3209 | ||
3210 | if (fatal) { | |
3211 | fprintf(stderr, "qcow2: Marking image as corrupt: %s; further " | |
3212 | "corruption events will be suppressed\n", message); | |
3213 | } else { | |
3214 | fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal " | |
3215 | "corruption events will be suppressed\n", message); | |
3216 | } | |
3217 | ||
dc881b44 AG |
3218 | node_name = bdrv_get_node_name(bs); |
3219 | qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs), | |
3220 | *node_name != '\0', node_name, | |
3221 | message, offset >= 0, offset, | |
3222 | size >= 0, size, | |
85186ebd HR |
3223 | fatal, &error_abort); |
3224 | g_free(message); | |
3225 | ||
3226 | if (fatal) { | |
3227 | qcow2_mark_corrupt(bs); | |
3228 | bs->drv = NULL; /* make BDS unusable */ | |
3229 | } | |
3230 | ||
3231 | s->signaled_corruption = true; | |
3232 | } | |
3233 | ||
1bd0e2d1 CL |
3234 | static QemuOptsList qcow2_create_opts = { |
3235 | .name = "qcow2-create-opts", | |
3236 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head), | |
3237 | .desc = { | |
3238 | { | |
3239 | .name = BLOCK_OPT_SIZE, | |
3240 | .type = QEMU_OPT_SIZE, | |
3241 | .help = "Virtual disk size" | |
3242 | }, | |
3243 | { | |
3244 | .name = BLOCK_OPT_COMPAT_LEVEL, | |
3245 | .type = QEMU_OPT_STRING, | |
3246 | .help = "Compatibility level (0.10 or 1.1)" | |
3247 | }, | |
3248 | { | |
3249 | .name = BLOCK_OPT_BACKING_FILE, | |
3250 | .type = QEMU_OPT_STRING, | |
3251 | .help = "File name of a base image" | |
3252 | }, | |
3253 | { | |
3254 | .name = BLOCK_OPT_BACKING_FMT, | |
3255 | .type = QEMU_OPT_STRING, | |
3256 | .help = "Image format of the base image" | |
3257 | }, | |
3258 | { | |
3259 | .name = BLOCK_OPT_ENCRYPT, | |
3260 | .type = QEMU_OPT_BOOL, | |
3261 | .help = "Encrypt the image", | |
3262 | .def_value_str = "off" | |
3263 | }, | |
3264 | { | |
3265 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
3266 | .type = QEMU_OPT_SIZE, | |
3267 | .help = "qcow2 cluster size", | |
3268 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE) | |
3269 | }, | |
3270 | { | |
3271 | .name = BLOCK_OPT_PREALLOC, | |
3272 | .type = QEMU_OPT_STRING, | |
0e4271b7 HT |
3273 | .help = "Preallocation mode (allowed values: off, metadata, " |
3274 | "falloc, full)" | |
1bd0e2d1 CL |
3275 | }, |
3276 | { | |
3277 | .name = BLOCK_OPT_LAZY_REFCOUNTS, | |
3278 | .type = QEMU_OPT_BOOL, | |
3279 | .help = "Postpone refcount updates", | |
3280 | .def_value_str = "off" | |
3281 | }, | |
06d05fa7 HR |
3282 | { |
3283 | .name = BLOCK_OPT_REFCOUNT_BITS, | |
3284 | .type = QEMU_OPT_NUMBER, | |
3285 | .help = "Width of a reference count entry in bits", | |
3286 | .def_value_str = "16" | |
3287 | }, | |
1bd0e2d1 CL |
3288 | { /* end of list */ } |
3289 | } | |
20d97356 BS |
3290 | }; |
3291 | ||
5f535a94 | 3292 | BlockDriver bdrv_qcow2 = { |
7c80ab3f | 3293 | .format_name = "qcow2", |
ff99129a | 3294 | .instance_size = sizeof(BDRVQcow2State), |
7c80ab3f JS |
3295 | .bdrv_probe = qcow2_probe, |
3296 | .bdrv_open = qcow2_open, | |
3297 | .bdrv_close = qcow2_close, | |
21d82ac9 | 3298 | .bdrv_reopen_prepare = qcow2_reopen_prepare, |
5b0959a7 KW |
3299 | .bdrv_reopen_commit = qcow2_reopen_commit, |
3300 | .bdrv_reopen_abort = qcow2_reopen_abort, | |
5365f44d | 3301 | .bdrv_join_options = qcow2_join_options, |
c282e1fd | 3302 | .bdrv_create = qcow2_create, |
3ac21627 | 3303 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
b6b8a333 | 3304 | .bdrv_co_get_block_status = qcow2_co_get_block_status, |
7c80ab3f | 3305 | .bdrv_set_key = qcow2_set_key, |
7c80ab3f | 3306 | |
c68b89ac KW |
3307 | .bdrv_co_readv = qcow2_co_readv, |
3308 | .bdrv_co_writev = qcow2_co_writev, | |
eb489bb1 | 3309 | .bdrv_co_flush_to_os = qcow2_co_flush_to_os, |
419b19d9 | 3310 | |
621f0589 | 3311 | .bdrv_co_write_zeroes = qcow2_co_write_zeroes, |
6db39ae2 | 3312 | .bdrv_co_discard = qcow2_co_discard, |
419b19d9 | 3313 | .bdrv_truncate = qcow2_truncate, |
7c80ab3f | 3314 | .bdrv_write_compressed = qcow2_write_compressed, |
491d27e2 | 3315 | .bdrv_make_empty = qcow2_make_empty, |
20d97356 BS |
3316 | |
3317 | .bdrv_snapshot_create = qcow2_snapshot_create, | |
3318 | .bdrv_snapshot_goto = qcow2_snapshot_goto, | |
3319 | .bdrv_snapshot_delete = qcow2_snapshot_delete, | |
3320 | .bdrv_snapshot_list = qcow2_snapshot_list, | |
1bd0e2d1 CL |
3321 | .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, |
3322 | .bdrv_get_info = qcow2_get_info, | |
37764dfb | 3323 | .bdrv_get_specific_info = qcow2_get_specific_info, |
20d97356 | 3324 | |
7c80ab3f JS |
3325 | .bdrv_save_vmstate = qcow2_save_vmstate, |
3326 | .bdrv_load_vmstate = qcow2_load_vmstate, | |
20d97356 | 3327 | |
8ee79e70 | 3328 | .supports_backing = true, |
20d97356 BS |
3329 | .bdrv_change_backing_file = qcow2_change_backing_file, |
3330 | ||
d34682cd | 3331 | .bdrv_refresh_limits = qcow2_refresh_limits, |
06d9260f AL |
3332 | .bdrv_invalidate_cache = qcow2_invalidate_cache, |
3333 | ||
1bd0e2d1 CL |
3334 | .create_opts = &qcow2_create_opts, |
3335 | .bdrv_check = qcow2_check, | |
c282e1fd | 3336 | .bdrv_amend_options = qcow2_amend_options, |
279621c0 AG |
3337 | |
3338 | .bdrv_detach_aio_context = qcow2_detach_aio_context, | |
3339 | .bdrv_attach_aio_context = qcow2_attach_aio_context, | |
20d97356 BS |
3340 | }; |
3341 | ||
5efa9d5a AL |
3342 | static void bdrv_qcow2_init(void) |
3343 | { | |
3344 | bdrv_register(&bdrv_qcow2); | |
3345 | } | |
3346 | ||
3347 | block_init(bdrv_qcow2_init); |