]>
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> |
753d9b82 | 28 | #include "qemu/aes.h" |
f7d0fe02 | 29 | #include "block/qcow2.h" |
1de7afc9 | 30 | #include "qemu/error-report.h" |
7b1b5d19 | 31 | #include "qapi/qmp/qerror.h" |
acdfb480 | 32 | #include "qapi/qmp/qbool.h" |
3cce16f4 | 33 | #include "trace.h" |
585f8587 FB |
34 | |
35 | /* | |
36 | Differences with QCOW: | |
37 | ||
38 | - Support for multiple incremental snapshots. | |
39 | - Memory management by reference counts. | |
40 | - Clusters which have a reference count of one have the bit | |
41 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 42 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
43 | in the cluster offsets. |
44 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 45 | snapshots. |
585f8587 FB |
46 | - If a backing store is used, the cluster size is not constrained |
47 | (could be backported to QCOW). | |
48 | - L2 tables have always a size of one cluster. | |
49 | */ | |
50 | ||
9b80ddf3 AL |
51 | |
52 | typedef struct { | |
53 | uint32_t magic; | |
54 | uint32_t len; | |
55 | } QCowExtension; | |
21d82ac9 | 56 | |
7c80ab3f JS |
57 | #define QCOW2_EXT_MAGIC_END 0 |
58 | #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA | |
cfcc4c62 | 59 | #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 |
9b80ddf3 | 60 | |
7c80ab3f | 61 | static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) |
585f8587 FB |
62 | { |
63 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 64 | |
585f8587 FB |
65 | if (buf_size >= sizeof(QCowHeader) && |
66 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
6744cbab | 67 | be32_to_cpu(cow_header->version) >= 2) |
585f8587 FB |
68 | return 100; |
69 | else | |
70 | return 0; | |
71 | } | |
72 | ||
9b80ddf3 AL |
73 | |
74 | /* | |
75 | * read qcow2 extension and fill bs | |
76 | * start reading from start_offset | |
77 | * finish reading upon magic of value 0 or when end_offset reached | |
78 | * unknown magic is skipped (future extension this version knows nothing about) | |
79 | * return 0 upon success, non-0 otherwise | |
80 | */ | |
7c80ab3f | 81 | static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
cfcc4c62 | 82 | uint64_t end_offset, void **p_feature_table) |
9b80ddf3 | 83 | { |
75bab85c | 84 | BDRVQcowState *s = bs->opaque; |
9b80ddf3 AL |
85 | QCowExtension ext; |
86 | uint64_t offset; | |
75bab85c | 87 | int ret; |
9b80ddf3 AL |
88 | |
89 | #ifdef DEBUG_EXT | |
7c80ab3f | 90 | printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); |
9b80ddf3 AL |
91 | #endif |
92 | offset = start_offset; | |
93 | while (offset < end_offset) { | |
94 | ||
95 | #ifdef DEBUG_EXT | |
96 | /* Sanity check */ | |
97 | if (offset > s->cluster_size) | |
7c80ab3f | 98 | printf("qcow2_read_extension: suspicious offset %lu\n", offset); |
9b80ddf3 | 99 | |
9b2260cb | 100 | printf("attempting to read extended header in offset %lu\n", offset); |
9b80ddf3 AL |
101 | #endif |
102 | ||
66f82cee | 103 | if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
7c80ab3f | 104 | fprintf(stderr, "qcow2_read_extension: ERROR: " |
0bfcd599 BS |
105 | "pread fail from offset %" PRIu64 "\n", |
106 | offset); | |
9b80ddf3 AL |
107 | return 1; |
108 | } | |
109 | be32_to_cpus(&ext.magic); | |
110 | be32_to_cpus(&ext.len); | |
111 | offset += sizeof(ext); | |
112 | #ifdef DEBUG_EXT | |
113 | printf("ext.magic = 0x%x\n", ext.magic); | |
114 | #endif | |
64ca6aee KW |
115 | if (ext.len > end_offset - offset) { |
116 | error_report("Header extension too large"); | |
117 | return -EINVAL; | |
118 | } | |
119 | ||
9b80ddf3 | 120 | switch (ext.magic) { |
7c80ab3f | 121 | case QCOW2_EXT_MAGIC_END: |
9b80ddf3 | 122 | return 0; |
f965509c | 123 | |
7c80ab3f | 124 | case QCOW2_EXT_MAGIC_BACKING_FORMAT: |
f965509c AL |
125 | if (ext.len >= sizeof(bs->backing_format)) { |
126 | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" | |
4c978075 | 127 | " (>=%zu)\n", |
f965509c AL |
128 | ext.len, sizeof(bs->backing_format)); |
129 | return 2; | |
130 | } | |
66f82cee | 131 | if (bdrv_pread(bs->file, offset , bs->backing_format, |
f965509c AL |
132 | ext.len) != ext.len) |
133 | return 3; | |
134 | bs->backing_format[ext.len] = '\0'; | |
135 | #ifdef DEBUG_EXT | |
136 | printf("Qcow2: Got format extension %s\n", bs->backing_format); | |
137 | #endif | |
f965509c AL |
138 | break; |
139 | ||
cfcc4c62 KW |
140 | case QCOW2_EXT_MAGIC_FEATURE_TABLE: |
141 | if (p_feature_table != NULL) { | |
142 | void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); | |
143 | ret = bdrv_pread(bs->file, offset , feature_table, ext.len); | |
144 | if (ret < 0) { | |
145 | return ret; | |
146 | } | |
147 | ||
148 | *p_feature_table = feature_table; | |
149 | } | |
150 | break; | |
151 | ||
9b80ddf3 | 152 | default: |
75bab85c KW |
153 | /* unknown magic - save it in case we need to rewrite the header */ |
154 | { | |
155 | Qcow2UnknownHeaderExtension *uext; | |
156 | ||
157 | uext = g_malloc0(sizeof(*uext) + ext.len); | |
158 | uext->magic = ext.magic; | |
159 | uext->len = ext.len; | |
160 | QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); | |
161 | ||
162 | ret = bdrv_pread(bs->file, offset , uext->data, uext->len); | |
163 | if (ret < 0) { | |
164 | return ret; | |
165 | } | |
75bab85c | 166 | } |
9b80ddf3 AL |
167 | break; |
168 | } | |
fd29b4bb KW |
169 | |
170 | offset += ((ext.len + 7) & ~7); | |
9b80ddf3 AL |
171 | } |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
75bab85c KW |
176 | static void cleanup_unknown_header_ext(BlockDriverState *bs) |
177 | { | |
178 | BDRVQcowState *s = bs->opaque; | |
179 | Qcow2UnknownHeaderExtension *uext, *next; | |
180 | ||
181 | QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { | |
182 | QLIST_REMOVE(uext, next); | |
183 | g_free(uext); | |
184 | } | |
185 | } | |
9b80ddf3 | 186 | |
b9531b6e SW |
187 | static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState *bs, |
188 | const char *fmt, ...) | |
6744cbab KW |
189 | { |
190 | char msg[64]; | |
191 | va_list ap; | |
192 | ||
193 | va_start(ap, fmt); | |
194 | vsnprintf(msg, sizeof(msg), fmt, ap); | |
195 | va_end(ap); | |
196 | ||
197 | qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, | |
198 | bs->device_name, "qcow2", msg); | |
199 | } | |
200 | ||
cfcc4c62 KW |
201 | static void report_unsupported_feature(BlockDriverState *bs, |
202 | Qcow2Feature *table, uint64_t mask) | |
203 | { | |
204 | while (table && table->name[0] != '\0') { | |
205 | if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { | |
206 | if (mask & (1 << table->bit)) { | |
207 | report_unsupported(bs, "%.46s",table->name); | |
208 | mask &= ~(1 << table->bit); | |
209 | } | |
210 | } | |
211 | table++; | |
212 | } | |
213 | ||
214 | if (mask) { | |
215 | report_unsupported(bs, "Unknown incompatible feature: %" PRIx64, mask); | |
216 | } | |
217 | } | |
218 | ||
bfe8043e SH |
219 | /* |
220 | * Sets the dirty bit and flushes afterwards if necessary. | |
221 | * | |
222 | * The incompatible_features bit is only set if the image file header was | |
223 | * updated successfully. Therefore it is not required to check the return | |
224 | * value of this function. | |
225 | */ | |
280d3735 | 226 | int qcow2_mark_dirty(BlockDriverState *bs) |
bfe8043e SH |
227 | { |
228 | BDRVQcowState *s = bs->opaque; | |
229 | uint64_t val; | |
230 | int ret; | |
231 | ||
232 | assert(s->qcow_version >= 3); | |
233 | ||
234 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
235 | return 0; /* already dirty */ | |
236 | } | |
237 | ||
238 | val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); | |
239 | ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), | |
240 | &val, sizeof(val)); | |
241 | if (ret < 0) { | |
242 | return ret; | |
243 | } | |
244 | ret = bdrv_flush(bs->file); | |
245 | if (ret < 0) { | |
246 | return ret; | |
247 | } | |
248 | ||
249 | /* Only treat image as dirty if the header was updated successfully */ | |
250 | s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; | |
251 | return 0; | |
252 | } | |
253 | ||
c61d0004 SH |
254 | /* |
255 | * Clears the dirty bit and flushes before if necessary. Only call this | |
256 | * function when there are no pending requests, it does not guard against | |
257 | * concurrent requests dirtying the image. | |
258 | */ | |
259 | static int qcow2_mark_clean(BlockDriverState *bs) | |
260 | { | |
261 | BDRVQcowState *s = bs->opaque; | |
262 | ||
263 | if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { | |
264 | int ret = bdrv_flush(bs); | |
265 | if (ret < 0) { | |
266 | return ret; | |
267 | } | |
268 | ||
269 | s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; | |
270 | return qcow2_update_header(bs); | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
acbe5982 SH |
275 | static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, |
276 | BdrvCheckMode fix) | |
277 | { | |
278 | int ret = qcow2_check_refcounts(bs, result, fix); | |
279 | if (ret < 0) { | |
280 | return ret; | |
281 | } | |
282 | ||
283 | if (fix && result->check_errors == 0 && result->corruptions == 0) { | |
284 | return qcow2_mark_clean(bs); | |
285 | } | |
286 | return ret; | |
287 | } | |
288 | ||
74c4510a KW |
289 | static QemuOptsList qcow2_runtime_opts = { |
290 | .name = "qcow2", | |
291 | .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), | |
292 | .desc = { | |
293 | { | |
294 | .name = "lazy_refcounts", | |
295 | .type = QEMU_OPT_BOOL, | |
296 | .help = "Postpone refcount updates", | |
297 | }, | |
67af674e KW |
298 | { |
299 | .name = QCOW2_OPT_DISCARD_REQUEST, | |
300 | .type = QEMU_OPT_BOOL, | |
301 | .help = "Pass guest discard requests to the layer below", | |
302 | }, | |
303 | { | |
304 | .name = QCOW2_OPT_DISCARD_SNAPSHOT, | |
305 | .type = QEMU_OPT_BOOL, | |
306 | .help = "Generate discard requests when snapshot related space " | |
307 | "is freed", | |
308 | }, | |
309 | { | |
310 | .name = QCOW2_OPT_DISCARD_OTHER, | |
311 | .type = QEMU_OPT_BOOL, | |
312 | .help = "Generate discard requests when other clusters are freed", | |
313 | }, | |
74c4510a KW |
314 | { /* end of list */ } |
315 | }, | |
316 | }; | |
317 | ||
1a86938f | 318 | static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) |
585f8587 FB |
319 | { |
320 | BDRVQcowState *s = bs->opaque; | |
6d85a57e | 321 | int len, i, ret = 0; |
585f8587 | 322 | QCowHeader header; |
74c4510a KW |
323 | QemuOpts *opts; |
324 | Error *local_err = NULL; | |
9b80ddf3 | 325 | uint64_t ext_end; |
2cf7cfa1 | 326 | uint64_t l1_vm_state_index; |
585f8587 | 327 | |
6d85a57e JS |
328 | ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); |
329 | if (ret < 0) { | |
585f8587 | 330 | goto fail; |
6d85a57e | 331 | } |
585f8587 FB |
332 | be32_to_cpus(&header.magic); |
333 | be32_to_cpus(&header.version); | |
334 | be64_to_cpus(&header.backing_file_offset); | |
335 | be32_to_cpus(&header.backing_file_size); | |
336 | be64_to_cpus(&header.size); | |
337 | be32_to_cpus(&header.cluster_bits); | |
338 | be32_to_cpus(&header.crypt_method); | |
339 | be64_to_cpus(&header.l1_table_offset); | |
340 | be32_to_cpus(&header.l1_size); | |
341 | be64_to_cpus(&header.refcount_table_offset); | |
342 | be32_to_cpus(&header.refcount_table_clusters); | |
343 | be64_to_cpus(&header.snapshots_offset); | |
344 | be32_to_cpus(&header.nb_snapshots); | |
3b46e624 | 345 | |
e8cdcec1 | 346 | if (header.magic != QCOW_MAGIC) { |
15bac0d5 | 347 | ret = -EMEDIUMTYPE; |
585f8587 | 348 | goto fail; |
6d85a57e | 349 | } |
6744cbab KW |
350 | if (header.version < 2 || header.version > 3) { |
351 | report_unsupported(bs, "QCOW version %d", header.version); | |
352 | ret = -ENOTSUP; | |
353 | goto fail; | |
354 | } | |
355 | ||
356 | s->qcow_version = header.version; | |
357 | ||
358 | /* Initialise version 3 header fields */ | |
359 | if (header.version == 2) { | |
360 | header.incompatible_features = 0; | |
361 | header.compatible_features = 0; | |
362 | header.autoclear_features = 0; | |
363 | header.refcount_order = 4; | |
364 | header.header_length = 72; | |
365 | } else { | |
366 | be64_to_cpus(&header.incompatible_features); | |
367 | be64_to_cpus(&header.compatible_features); | |
368 | be64_to_cpus(&header.autoclear_features); | |
369 | be32_to_cpus(&header.refcount_order); | |
370 | be32_to_cpus(&header.header_length); | |
371 | } | |
372 | ||
373 | if (header.header_length > sizeof(header)) { | |
374 | s->unknown_header_fields_size = header.header_length - sizeof(header); | |
375 | s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); | |
376 | ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, | |
377 | s->unknown_header_fields_size); | |
378 | if (ret < 0) { | |
379 | goto fail; | |
380 | } | |
381 | } | |
382 | ||
cfcc4c62 KW |
383 | if (header.backing_file_offset) { |
384 | ext_end = header.backing_file_offset; | |
385 | } else { | |
386 | ext_end = 1 << header.cluster_bits; | |
387 | } | |
388 | ||
6744cbab KW |
389 | /* Handle feature bits */ |
390 | s->incompatible_features = header.incompatible_features; | |
391 | s->compatible_features = header.compatible_features; | |
392 | s->autoclear_features = header.autoclear_features; | |
393 | ||
c61d0004 | 394 | if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { |
cfcc4c62 KW |
395 | void *feature_table = NULL; |
396 | qcow2_read_extensions(bs, header.header_length, ext_end, | |
397 | &feature_table); | |
398 | report_unsupported_feature(bs, feature_table, | |
c61d0004 SH |
399 | s->incompatible_features & |
400 | ~QCOW2_INCOMPAT_MASK); | |
6744cbab KW |
401 | ret = -ENOTSUP; |
402 | goto fail; | |
403 | } | |
404 | ||
6744cbab KW |
405 | /* Check support for various header values */ |
406 | if (header.refcount_order != 4) { | |
407 | report_unsupported(bs, "%d bit reference counts", | |
408 | 1 << header.refcount_order); | |
e8cdcec1 KW |
409 | ret = -ENOTSUP; |
410 | goto fail; | |
411 | } | |
6744cbab | 412 | |
d191d12d | 413 | if (header.cluster_bits < MIN_CLUSTER_BITS || |
6d85a57e JS |
414 | header.cluster_bits > MAX_CLUSTER_BITS) { |
415 | ret = -EINVAL; | |
585f8587 | 416 | goto fail; |
6d85a57e JS |
417 | } |
418 | if (header.crypt_method > QCOW_CRYPT_AES) { | |
419 | ret = -EINVAL; | |
585f8587 | 420 | goto fail; |
6d85a57e | 421 | } |
585f8587 | 422 | s->crypt_method_header = header.crypt_method; |
6d85a57e | 423 | if (s->crypt_method_header) { |
585f8587 | 424 | bs->encrypted = 1; |
6d85a57e | 425 | } |
585f8587 FB |
426 | s->cluster_bits = header.cluster_bits; |
427 | s->cluster_size = 1 << s->cluster_bits; | |
428 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
429 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ | |
430 | s->l2_size = 1 << s->l2_bits; | |
431 | bs->total_sectors = header.size / 512; | |
432 | s->csize_shift = (62 - (s->cluster_bits - 8)); | |
433 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
434 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
435 | s->refcount_table_offset = header.refcount_table_offset; | |
5fafdf24 | 436 | s->refcount_table_size = |
585f8587 FB |
437 | header.refcount_table_clusters << (s->cluster_bits - 3); |
438 | ||
439 | s->snapshots_offset = header.snapshots_offset; | |
440 | s->nb_snapshots = header.nb_snapshots; | |
441 | ||
442 | /* read the level 1 table */ | |
443 | s->l1_size = header.l1_size; | |
2cf7cfa1 KW |
444 | |
445 | l1_vm_state_index = size_to_l1(s, header.size); | |
446 | if (l1_vm_state_index > INT_MAX) { | |
447 | ret = -EFBIG; | |
448 | goto fail; | |
449 | } | |
450 | s->l1_vm_state_index = l1_vm_state_index; | |
451 | ||
585f8587 FB |
452 | /* the L1 table must contain at least enough entries to put |
453 | header.size bytes */ | |
6d85a57e JS |
454 | if (s->l1_size < s->l1_vm_state_index) { |
455 | ret = -EINVAL; | |
585f8587 | 456 | goto fail; |
6d85a57e | 457 | } |
585f8587 | 458 | s->l1_table_offset = header.l1_table_offset; |
d191d12d | 459 | if (s->l1_size > 0) { |
7267c094 | 460 | s->l1_table = g_malloc0( |
d191d12d | 461 | align_offset(s->l1_size * sizeof(uint64_t), 512)); |
6d85a57e JS |
462 | ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, |
463 | s->l1_size * sizeof(uint64_t)); | |
464 | if (ret < 0) { | |
d191d12d | 465 | goto fail; |
6d85a57e | 466 | } |
d191d12d SW |
467 | for(i = 0;i < s->l1_size; i++) { |
468 | be64_to_cpus(&s->l1_table[i]); | |
469 | } | |
585f8587 | 470 | } |
29c1a730 KW |
471 | |
472 | /* alloc L2 table/refcount block cache */ | |
6af4e9ea PB |
473 | s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE); |
474 | s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE); | |
29c1a730 | 475 | |
7267c094 | 476 | s->cluster_cache = g_malloc(s->cluster_size); |
585f8587 | 477 | /* one more sector for decompressed data alignment */ |
dea43a65 | 478 | s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
095a9c58 | 479 | + 512); |
585f8587 | 480 | s->cluster_cache_offset = -1; |
06d9260f | 481 | s->flags = flags; |
3b46e624 | 482 | |
6d85a57e JS |
483 | ret = qcow2_refcount_init(bs); |
484 | if (ret != 0) { | |
585f8587 | 485 | goto fail; |
6d85a57e | 486 | } |
585f8587 | 487 | |
72cf2d4f | 488 | QLIST_INIT(&s->cluster_allocs); |
0b919fae | 489 | QTAILQ_INIT(&s->discards); |
f214978a | 490 | |
9b80ddf3 | 491 | /* read qcow2 extensions */ |
cfcc4c62 | 492 | if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL)) { |
6d85a57e | 493 | ret = -EINVAL; |
9b80ddf3 | 494 | goto fail; |
6d85a57e | 495 | } |
9b80ddf3 | 496 | |
585f8587 FB |
497 | /* read the backing file name */ |
498 | if (header.backing_file_offset != 0) { | |
499 | len = header.backing_file_size; | |
6d85a57e | 500 | if (len > 1023) { |
585f8587 | 501 | len = 1023; |
6d85a57e JS |
502 | } |
503 | ret = bdrv_pread(bs->file, header.backing_file_offset, | |
504 | bs->backing_file, len); | |
505 | if (ret < 0) { | |
585f8587 | 506 | goto fail; |
6d85a57e | 507 | } |
585f8587 FB |
508 | bs->backing_file[len] = '\0'; |
509 | } | |
42deb29f KW |
510 | |
511 | ret = qcow2_read_snapshots(bs); | |
512 | if (ret < 0) { | |
585f8587 | 513 | goto fail; |
6d85a57e | 514 | } |
585f8587 | 515 | |
af7b708d SH |
516 | /* Clear unknown autoclear feature bits */ |
517 | if (!bs->read_only && s->autoclear_features != 0) { | |
518 | s->autoclear_features = 0; | |
519 | ret = qcow2_update_header(bs); | |
520 | if (ret < 0) { | |
521 | goto fail; | |
522 | } | |
523 | } | |
524 | ||
68d100e9 KW |
525 | /* Initialise locks */ |
526 | qemu_co_mutex_init(&s->lock); | |
527 | ||
c61d0004 | 528 | /* Repair image if dirty */ |
058f8f16 SH |
529 | if (!(flags & BDRV_O_CHECK) && !bs->read_only && |
530 | (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { | |
c61d0004 SH |
531 | BdrvCheckResult result = {0}; |
532 | ||
acbe5982 | 533 | ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS); |
c61d0004 SH |
534 | if (ret < 0) { |
535 | goto fail; | |
536 | } | |
537 | } | |
538 | ||
74c4510a KW |
539 | /* Enable lazy_refcounts according to image and command line options */ |
540 | opts = qemu_opts_create_nofail(&qcow2_runtime_opts); | |
541 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
542 | if (error_is_set(&local_err)) { | |
543 | qerror_report_err(local_err); | |
544 | error_free(local_err); | |
545 | ret = -EINVAL; | |
546 | goto fail; | |
547 | } | |
548 | ||
acdfb480 | 549 | s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, |
74c4510a KW |
550 | (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); |
551 | ||
67af674e KW |
552 | s->discard_passthrough[QCOW2_DISCARD_NEVER] = false; |
553 | s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; | |
554 | s->discard_passthrough[QCOW2_DISCARD_REQUEST] = | |
555 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, | |
556 | flags & BDRV_O_UNMAP); | |
557 | s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = | |
558 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); | |
559 | s->discard_passthrough[QCOW2_DISCARD_OTHER] = | |
560 | qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); | |
561 | ||
74c4510a KW |
562 | qemu_opts_del(opts); |
563 | ||
564 | if (s->use_lazy_refcounts && s->qcow_version < 3) { | |
565 | qerror_report(ERROR_CLASS_GENERIC_ERROR, "Lazy refcounts require " | |
566 | "a qcow2 image with at least qemu 1.1 compatibility level"); | |
567 | ret = -EINVAL; | |
568 | goto fail; | |
569 | } | |
570 | ||
585f8587 | 571 | #ifdef DEBUG_ALLOC |
6cbc3031 PH |
572 | { |
573 | BdrvCheckResult result = {0}; | |
b35278f7 | 574 | qcow2_check_refcounts(bs, &result, 0); |
6cbc3031 | 575 | } |
585f8587 | 576 | #endif |
6d85a57e | 577 | return ret; |
585f8587 FB |
578 | |
579 | fail: | |
6744cbab | 580 | g_free(s->unknown_header_fields); |
75bab85c | 581 | cleanup_unknown_header_ext(bs); |
ed6ccf0f KW |
582 | qcow2_free_snapshots(bs); |
583 | qcow2_refcount_close(bs); | |
7267c094 | 584 | g_free(s->l1_table); |
29c1a730 KW |
585 | if (s->l2_table_cache) { |
586 | qcow2_cache_destroy(bs, s->l2_table_cache); | |
587 | } | |
7267c094 | 588 | g_free(s->cluster_cache); |
dea43a65 | 589 | qemu_vfree(s->cluster_data); |
6d85a57e | 590 | return ret; |
585f8587 FB |
591 | } |
592 | ||
7c80ab3f | 593 | static int qcow2_set_key(BlockDriverState *bs, const char *key) |
585f8587 FB |
594 | { |
595 | BDRVQcowState *s = bs->opaque; | |
596 | uint8_t keybuf[16]; | |
597 | int len, i; | |
3b46e624 | 598 | |
585f8587 FB |
599 | memset(keybuf, 0, 16); |
600 | len = strlen(key); | |
601 | if (len > 16) | |
602 | len = 16; | |
603 | /* XXX: we could compress the chars to 7 bits to increase | |
604 | entropy */ | |
605 | for(i = 0;i < len;i++) { | |
606 | keybuf[i] = key[i]; | |
607 | } | |
608 | s->crypt_method = s->crypt_method_header; | |
609 | ||
610 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) | |
611 | return -1; | |
612 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) | |
613 | return -1; | |
614 | #if 0 | |
615 | /* test */ | |
616 | { | |
617 | uint8_t in[16]; | |
618 | uint8_t out[16]; | |
619 | uint8_t tmp[16]; | |
620 | for(i=0;i<16;i++) | |
621 | in[i] = i; | |
622 | AES_encrypt(in, tmp, &s->aes_encrypt_key); | |
623 | AES_decrypt(tmp, out, &s->aes_decrypt_key); | |
624 | for(i = 0; i < 16; i++) | |
625 | printf(" %02x", tmp[i]); | |
626 | printf("\n"); | |
627 | for(i = 0; i < 16; i++) | |
628 | printf(" %02x", out[i]); | |
629 | printf("\n"); | |
630 | } | |
631 | #endif | |
632 | return 0; | |
633 | } | |
634 | ||
21d82ac9 JC |
635 | /* We have nothing to do for QCOW2 reopen, stubs just return |
636 | * success */ | |
637 | static int qcow2_reopen_prepare(BDRVReopenState *state, | |
638 | BlockReopenQueue *queue, Error **errp) | |
639 | { | |
640 | return 0; | |
641 | } | |
642 | ||
f8a2e5e3 SH |
643 | static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs, |
644 | int64_t sector_num, int nb_sectors, int *pnum) | |
585f8587 | 645 | { |
f8a2e5e3 | 646 | BDRVQcowState *s = bs->opaque; |
585f8587 | 647 | uint64_t cluster_offset; |
1c46efaa | 648 | int ret; |
585f8587 | 649 | |
095a9c58 | 650 | *pnum = nb_sectors; |
f8a2e5e3 SH |
651 | /* FIXME We can get errors here, but the bdrv_co_is_allocated interface |
652 | * can't pass them on today */ | |
653 | qemu_co_mutex_lock(&s->lock); | |
1c46efaa | 654 | ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); |
f8a2e5e3 | 655 | qemu_co_mutex_unlock(&s->lock); |
1c46efaa KW |
656 | if (ret < 0) { |
657 | *pnum = 0; | |
658 | } | |
095a9c58 | 659 | |
381b487d | 660 | return (cluster_offset != 0) || (ret == QCOW2_CLUSTER_ZERO); |
585f8587 FB |
661 | } |
662 | ||
a9465922 | 663 | /* handle reading after the end of the backing file */ |
bd28f835 KW |
664 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
665 | int64_t sector_num, int nb_sectors) | |
a9465922 FB |
666 | { |
667 | int n1; | |
668 | if ((sector_num + nb_sectors) <= bs->total_sectors) | |
669 | return nb_sectors; | |
670 | if (sector_num >= bs->total_sectors) | |
671 | n1 = 0; | |
672 | else | |
673 | n1 = bs->total_sectors - sector_num; | |
bd28f835 | 674 | |
3d9b4925 | 675 | qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1)); |
bd28f835 | 676 | |
a9465922 FB |
677 | return n1; |
678 | } | |
679 | ||
a968168c | 680 | static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, |
3fc48d09 | 681 | int remaining_sectors, QEMUIOVector *qiov) |
585f8587 | 682 | { |
585f8587 | 683 | BDRVQcowState *s = bs->opaque; |
a9465922 | 684 | int index_in_cluster, n1; |
68d100e9 | 685 | int ret; |
faf575c1 | 686 | int cur_nr_sectors; /* number of sectors in current iteration */ |
c2bdd990 | 687 | uint64_t cluster_offset = 0; |
3fc48d09 FZ |
688 | uint64_t bytes_done = 0; |
689 | QEMUIOVector hd_qiov; | |
690 | uint8_t *cluster_data = NULL; | |
585f8587 | 691 | |
3fc48d09 FZ |
692 | qemu_iovec_init(&hd_qiov, qiov->niov); |
693 | ||
694 | qemu_co_mutex_lock(&s->lock); | |
695 | ||
696 | while (remaining_sectors != 0) { | |
bd28f835 | 697 | |
5ebaa27e | 698 | /* prepare next request */ |
3fc48d09 | 699 | cur_nr_sectors = remaining_sectors; |
5ebaa27e FZ |
700 | if (s->crypt_method) { |
701 | cur_nr_sectors = MIN(cur_nr_sectors, | |
702 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
585f8587 | 703 | } |
5ebaa27e | 704 | |
3fc48d09 | 705 | ret = qcow2_get_cluster_offset(bs, sector_num << 9, |
5ebaa27e | 706 | &cur_nr_sectors, &cluster_offset); |
8af36488 | 707 | if (ret < 0) { |
3fc48d09 | 708 | goto fail; |
8af36488 | 709 | } |
bd28f835 | 710 | |
3fc48d09 | 711 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
c87c0672 | 712 | |
3fc48d09 | 713 | qemu_iovec_reset(&hd_qiov); |
1b093c48 | 714 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, |
5ebaa27e FZ |
715 | cur_nr_sectors * 512); |
716 | ||
68d000a3 KW |
717 | switch (ret) { |
718 | case QCOW2_CLUSTER_UNALLOCATED: | |
5ebaa27e FZ |
719 | |
720 | if (bs->backing_hd) { | |
721 | /* read from the base image */ | |
3fc48d09 FZ |
722 | n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov, |
723 | sector_num, cur_nr_sectors); | |
5ebaa27e FZ |
724 | if (n1 > 0) { |
725 | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); | |
726 | qemu_co_mutex_unlock(&s->lock); | |
3fc48d09 FZ |
727 | ret = bdrv_co_readv(bs->backing_hd, sector_num, |
728 | n1, &hd_qiov); | |
5ebaa27e FZ |
729 | qemu_co_mutex_lock(&s->lock); |
730 | if (ret < 0) { | |
3fc48d09 | 731 | goto fail; |
5ebaa27e FZ |
732 | } |
733 | } | |
734 | } else { | |
735 | /* Note: in this case, no need to wait */ | |
3d9b4925 | 736 | qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); |
5ebaa27e | 737 | } |
68d000a3 KW |
738 | break; |
739 | ||
6377af48 | 740 | case QCOW2_CLUSTER_ZERO: |
3d9b4925 | 741 | qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); |
6377af48 KW |
742 | break; |
743 | ||
68d000a3 | 744 | case QCOW2_CLUSTER_COMPRESSED: |
5ebaa27e FZ |
745 | /* add AIO support for compressed blocks ? */ |
746 | ret = qcow2_decompress_cluster(bs, cluster_offset); | |
747 | if (ret < 0) { | |
3fc48d09 | 748 | goto fail; |
bd28f835 KW |
749 | } |
750 | ||
03396148 | 751 | qemu_iovec_from_buf(&hd_qiov, 0, |
5ebaa27e | 752 | s->cluster_cache + index_in_cluster * 512, |
faf575c1 | 753 | 512 * cur_nr_sectors); |
68d000a3 KW |
754 | break; |
755 | ||
756 | case QCOW2_CLUSTER_NORMAL: | |
5ebaa27e | 757 | if ((cluster_offset & 511) != 0) { |
3fc48d09 FZ |
758 | ret = -EIO; |
759 | goto fail; | |
5ebaa27e | 760 | } |
bd28f835 | 761 | |
5ebaa27e FZ |
762 | if (s->crypt_method) { |
763 | /* | |
764 | * For encrypted images, read everything into a temporary | |
765 | * contiguous buffer on which the AES functions can work. | |
766 | */ | |
3fc48d09 FZ |
767 | if (!cluster_data) { |
768 | cluster_data = | |
dea43a65 | 769 | qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
5ebaa27e FZ |
770 | } |
771 | ||
772 | assert(cur_nr_sectors <= | |
773 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
3fc48d09 FZ |
774 | qemu_iovec_reset(&hd_qiov); |
775 | qemu_iovec_add(&hd_qiov, cluster_data, | |
5ebaa27e FZ |
776 | 512 * cur_nr_sectors); |
777 | } | |
778 | ||
779 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); | |
780 | qemu_co_mutex_unlock(&s->lock); | |
781 | ret = bdrv_co_readv(bs->file, | |
782 | (cluster_offset >> 9) + index_in_cluster, | |
3fc48d09 | 783 | cur_nr_sectors, &hd_qiov); |
5ebaa27e FZ |
784 | qemu_co_mutex_lock(&s->lock); |
785 | if (ret < 0) { | |
3fc48d09 | 786 | goto fail; |
5ebaa27e FZ |
787 | } |
788 | if (s->crypt_method) { | |
3fc48d09 FZ |
789 | qcow2_encrypt_sectors(s, sector_num, cluster_data, |
790 | cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key); | |
03396148 MT |
791 | qemu_iovec_from_buf(qiov, bytes_done, |
792 | cluster_data, 512 * cur_nr_sectors); | |
5ebaa27e | 793 | } |
68d000a3 KW |
794 | break; |
795 | ||
796 | default: | |
797 | g_assert_not_reached(); | |
798 | ret = -EIO; | |
799 | goto fail; | |
faf575c1 | 800 | } |
f141eafe | 801 | |
3fc48d09 FZ |
802 | remaining_sectors -= cur_nr_sectors; |
803 | sector_num += cur_nr_sectors; | |
804 | bytes_done += cur_nr_sectors * 512; | |
5ebaa27e | 805 | } |
3fc48d09 | 806 | ret = 0; |
faf575c1 | 807 | |
3fc48d09 | 808 | fail: |
68d100e9 | 809 | qemu_co_mutex_unlock(&s->lock); |
42496d62 | 810 | |
3fc48d09 | 811 | qemu_iovec_destroy(&hd_qiov); |
dea43a65 | 812 | qemu_vfree(cluster_data); |
68d100e9 KW |
813 | |
814 | return ret; | |
585f8587 FB |
815 | } |
816 | ||
a968168c | 817 | static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, |
3fc48d09 FZ |
818 | int64_t sector_num, |
819 | int remaining_sectors, | |
820 | QEMUIOVector *qiov) | |
585f8587 | 821 | { |
585f8587 | 822 | BDRVQcowState *s = bs->opaque; |
585f8587 | 823 | int index_in_cluster; |
095a9c58 | 824 | int n_end; |
68d100e9 | 825 | int ret; |
faf575c1 | 826 | int cur_nr_sectors; /* number of sectors in current iteration */ |
c2bdd990 | 827 | uint64_t cluster_offset; |
3fc48d09 FZ |
828 | QEMUIOVector hd_qiov; |
829 | uint64_t bytes_done = 0; | |
830 | uint8_t *cluster_data = NULL; | |
8d2497c3 | 831 | QCowL2Meta *l2meta = NULL; |
c2271403 | 832 | |
3cce16f4 KW |
833 | trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num, |
834 | remaining_sectors); | |
835 | ||
3fc48d09 FZ |
836 | qemu_iovec_init(&hd_qiov, qiov->niov); |
837 | ||
838 | s->cluster_cache_offset = -1; /* disable compressed cache */ | |
3b46e624 | 839 | |
3fc48d09 FZ |
840 | qemu_co_mutex_lock(&s->lock); |
841 | ||
842 | while (remaining_sectors != 0) { | |
843 | ||
f50f88b9 | 844 | l2meta = NULL; |
cf5c1a23 | 845 | |
3cce16f4 | 846 | trace_qcow2_writev_start_part(qemu_coroutine_self()); |
3fc48d09 FZ |
847 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
848 | n_end = index_in_cluster + remaining_sectors; | |
5ebaa27e FZ |
849 | if (s->crypt_method && |
850 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) { | |
851 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; | |
852 | } | |
095a9c58 | 853 | |
3fc48d09 | 854 | ret = qcow2_alloc_cluster_offset(bs, sector_num << 9, |
f50f88b9 | 855 | index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta); |
5ebaa27e | 856 | if (ret < 0) { |
3fc48d09 | 857 | goto fail; |
5ebaa27e | 858 | } |
148da7ea | 859 | |
5ebaa27e | 860 | assert((cluster_offset & 511) == 0); |
148da7ea | 861 | |
3fc48d09 | 862 | qemu_iovec_reset(&hd_qiov); |
1b093c48 | 863 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, |
5ebaa27e | 864 | cur_nr_sectors * 512); |
6f5f060b | 865 | |
5ebaa27e | 866 | if (s->crypt_method) { |
3fc48d09 | 867 | if (!cluster_data) { |
dea43a65 | 868 | cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * |
5ebaa27e FZ |
869 | s->cluster_size); |
870 | } | |
6f5f060b | 871 | |
3fc48d09 | 872 | assert(hd_qiov.size <= |
5ebaa27e | 873 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
d5e6b161 | 874 | qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size); |
6f5f060b | 875 | |
3fc48d09 FZ |
876 | qcow2_encrypt_sectors(s, sector_num, cluster_data, |
877 | cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key); | |
6f5f060b | 878 | |
3fc48d09 FZ |
879 | qemu_iovec_reset(&hd_qiov); |
880 | qemu_iovec_add(&hd_qiov, cluster_data, | |
5ebaa27e FZ |
881 | cur_nr_sectors * 512); |
882 | } | |
6f5f060b | 883 | |
5ebaa27e | 884 | qemu_co_mutex_unlock(&s->lock); |
67a7a0eb | 885 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
3cce16f4 KW |
886 | trace_qcow2_writev_data(qemu_coroutine_self(), |
887 | (cluster_offset >> 9) + index_in_cluster); | |
5ebaa27e FZ |
888 | ret = bdrv_co_writev(bs->file, |
889 | (cluster_offset >> 9) + index_in_cluster, | |
3fc48d09 | 890 | cur_nr_sectors, &hd_qiov); |
5ebaa27e FZ |
891 | qemu_co_mutex_lock(&s->lock); |
892 | if (ret < 0) { | |
3fc48d09 | 893 | goto fail; |
5ebaa27e | 894 | } |
f141eafe | 895 | |
88c6588c KW |
896 | while (l2meta != NULL) { |
897 | QCowL2Meta *next; | |
898 | ||
f50f88b9 KW |
899 | ret = qcow2_alloc_cluster_link_l2(bs, l2meta); |
900 | if (ret < 0) { | |
901 | goto fail; | |
902 | } | |
faf575c1 | 903 | |
4e95314e KW |
904 | /* Take the request off the list of running requests */ |
905 | if (l2meta->nb_clusters != 0) { | |
906 | QLIST_REMOVE(l2meta, next_in_flight); | |
907 | } | |
908 | ||
4e95314e | 909 | qemu_co_queue_restart_all(&l2meta->dependent_requests); |
4e95314e | 910 | |
88c6588c | 911 | next = l2meta->next; |
f50f88b9 | 912 | g_free(l2meta); |
88c6588c | 913 | l2meta = next; |
f50f88b9 | 914 | } |
0fa9131a | 915 | |
3fc48d09 FZ |
916 | remaining_sectors -= cur_nr_sectors; |
917 | sector_num += cur_nr_sectors; | |
918 | bytes_done += cur_nr_sectors * 512; | |
3cce16f4 | 919 | trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors); |
5ebaa27e | 920 | } |
3fc48d09 | 921 | ret = 0; |
faf575c1 | 922 | |
3fc48d09 | 923 | fail: |
4e95314e KW |
924 | qemu_co_mutex_unlock(&s->lock); |
925 | ||
88c6588c KW |
926 | while (l2meta != NULL) { |
927 | QCowL2Meta *next; | |
928 | ||
4e95314e KW |
929 | if (l2meta->nb_clusters != 0) { |
930 | QLIST_REMOVE(l2meta, next_in_flight); | |
931 | } | |
932 | qemu_co_queue_restart_all(&l2meta->dependent_requests); | |
88c6588c KW |
933 | |
934 | next = l2meta->next; | |
cf5c1a23 | 935 | g_free(l2meta); |
88c6588c | 936 | l2meta = next; |
cf5c1a23 | 937 | } |
0fa9131a | 938 | |
3fc48d09 | 939 | qemu_iovec_destroy(&hd_qiov); |
dea43a65 | 940 | qemu_vfree(cluster_data); |
3cce16f4 | 941 | trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); |
42496d62 | 942 | |
68d100e9 | 943 | return ret; |
585f8587 FB |
944 | } |
945 | ||
7c80ab3f | 946 | static void qcow2_close(BlockDriverState *bs) |
585f8587 FB |
947 | { |
948 | BDRVQcowState *s = bs->opaque; | |
7267c094 | 949 | g_free(s->l1_table); |
29c1a730 KW |
950 | |
951 | qcow2_cache_flush(bs, s->l2_table_cache); | |
952 | qcow2_cache_flush(bs, s->refcount_block_cache); | |
953 | ||
c61d0004 SH |
954 | qcow2_mark_clean(bs); |
955 | ||
29c1a730 KW |
956 | qcow2_cache_destroy(bs, s->l2_table_cache); |
957 | qcow2_cache_destroy(bs, s->refcount_block_cache); | |
958 | ||
6744cbab | 959 | g_free(s->unknown_header_fields); |
75bab85c | 960 | cleanup_unknown_header_ext(bs); |
6744cbab | 961 | |
7267c094 | 962 | g_free(s->cluster_cache); |
dea43a65 | 963 | qemu_vfree(s->cluster_data); |
ed6ccf0f | 964 | qcow2_refcount_close(bs); |
28c1202b | 965 | qcow2_free_snapshots(bs); |
585f8587 FB |
966 | } |
967 | ||
06d9260f AL |
968 | static void qcow2_invalidate_cache(BlockDriverState *bs) |
969 | { | |
970 | BDRVQcowState *s = bs->opaque; | |
971 | int flags = s->flags; | |
972 | AES_KEY aes_encrypt_key; | |
973 | AES_KEY aes_decrypt_key; | |
974 | uint32_t crypt_method = 0; | |
acdfb480 | 975 | QDict *options; |
06d9260f AL |
976 | |
977 | /* | |
978 | * Backing files are read-only which makes all of their metadata immutable, | |
979 | * that means we don't have to worry about reopening them here. | |
980 | */ | |
981 | ||
982 | if (s->crypt_method) { | |
983 | crypt_method = s->crypt_method; | |
984 | memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key)); | |
985 | memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key)); | |
986 | } | |
987 | ||
988 | qcow2_close(bs); | |
989 | ||
acdfb480 KW |
990 | options = qdict_new(); |
991 | qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS, | |
992 | qbool_from_int(s->use_lazy_refcounts)); | |
993 | ||
06d9260f | 994 | memset(s, 0, sizeof(BDRVQcowState)); |
acdfb480 KW |
995 | qcow2_open(bs, options, flags); |
996 | ||
997 | QDECREF(options); | |
06d9260f AL |
998 | |
999 | if (crypt_method) { | |
1000 | s->crypt_method = crypt_method; | |
1001 | memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key)); | |
1002 | memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key)); | |
1003 | } | |
1004 | } | |
1005 | ||
e24e49e6 KW |
1006 | static size_t header_ext_add(char *buf, uint32_t magic, const void *s, |
1007 | size_t len, size_t buflen) | |
1008 | { | |
1009 | QCowExtension *ext_backing_fmt = (QCowExtension*) buf; | |
1010 | size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); | |
1011 | ||
1012 | if (buflen < ext_len) { | |
1013 | return -ENOSPC; | |
1014 | } | |
1015 | ||
1016 | *ext_backing_fmt = (QCowExtension) { | |
1017 | .magic = cpu_to_be32(magic), | |
1018 | .len = cpu_to_be32(len), | |
1019 | }; | |
1020 | memcpy(buf + sizeof(QCowExtension), s, len); | |
1021 | ||
1022 | return ext_len; | |
1023 | } | |
1024 | ||
756e6736 | 1025 | /* |
e24e49e6 KW |
1026 | * Updates the qcow2 header, including the variable length parts of it, i.e. |
1027 | * the backing file name and all extensions. qcow2 was not designed to allow | |
1028 | * such changes, so if we run out of space (we can only use the first cluster) | |
1029 | * this function may fail. | |
756e6736 KW |
1030 | * |
1031 | * Returns 0 on success, -errno in error cases. | |
1032 | */ | |
e24e49e6 | 1033 | int qcow2_update_header(BlockDriverState *bs) |
756e6736 | 1034 | { |
756e6736 | 1035 | BDRVQcowState *s = bs->opaque; |
e24e49e6 KW |
1036 | QCowHeader *header; |
1037 | char *buf; | |
1038 | size_t buflen = s->cluster_size; | |
756e6736 | 1039 | int ret; |
e24e49e6 KW |
1040 | uint64_t total_size; |
1041 | uint32_t refcount_table_clusters; | |
6744cbab | 1042 | size_t header_length; |
75bab85c | 1043 | Qcow2UnknownHeaderExtension *uext; |
756e6736 | 1044 | |
e24e49e6 | 1045 | buf = qemu_blockalign(bs, buflen); |
756e6736 | 1046 | |
e24e49e6 KW |
1047 | /* Header structure */ |
1048 | header = (QCowHeader*) buf; | |
756e6736 | 1049 | |
e24e49e6 KW |
1050 | if (buflen < sizeof(*header)) { |
1051 | ret = -ENOSPC; | |
1052 | goto fail; | |
756e6736 KW |
1053 | } |
1054 | ||
6744cbab | 1055 | header_length = sizeof(*header) + s->unknown_header_fields_size; |
e24e49e6 KW |
1056 | total_size = bs->total_sectors * BDRV_SECTOR_SIZE; |
1057 | refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); | |
1058 | ||
1059 | *header = (QCowHeader) { | |
6744cbab | 1060 | /* Version 2 fields */ |
e24e49e6 | 1061 | .magic = cpu_to_be32(QCOW_MAGIC), |
6744cbab | 1062 | .version = cpu_to_be32(s->qcow_version), |
e24e49e6 KW |
1063 | .backing_file_offset = 0, |
1064 | .backing_file_size = 0, | |
1065 | .cluster_bits = cpu_to_be32(s->cluster_bits), | |
1066 | .size = cpu_to_be64(total_size), | |
1067 | .crypt_method = cpu_to_be32(s->crypt_method_header), | |
1068 | .l1_size = cpu_to_be32(s->l1_size), | |
1069 | .l1_table_offset = cpu_to_be64(s->l1_table_offset), | |
1070 | .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), | |
1071 | .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), | |
1072 | .nb_snapshots = cpu_to_be32(s->nb_snapshots), | |
1073 | .snapshots_offset = cpu_to_be64(s->snapshots_offset), | |
6744cbab KW |
1074 | |
1075 | /* Version 3 fields */ | |
1076 | .incompatible_features = cpu_to_be64(s->incompatible_features), | |
1077 | .compatible_features = cpu_to_be64(s->compatible_features), | |
1078 | .autoclear_features = cpu_to_be64(s->autoclear_features), | |
1079 | .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT), | |
1080 | .header_length = cpu_to_be32(header_length), | |
e24e49e6 | 1081 | }; |
756e6736 | 1082 | |
6744cbab KW |
1083 | /* For older versions, write a shorter header */ |
1084 | switch (s->qcow_version) { | |
1085 | case 2: | |
1086 | ret = offsetof(QCowHeader, incompatible_features); | |
1087 | break; | |
1088 | case 3: | |
1089 | ret = sizeof(*header); | |
1090 | break; | |
1091 | default: | |
b6c14762 JM |
1092 | ret = -EINVAL; |
1093 | goto fail; | |
6744cbab KW |
1094 | } |
1095 | ||
1096 | buf += ret; | |
1097 | buflen -= ret; | |
1098 | memset(buf, 0, buflen); | |
1099 | ||
1100 | /* Preserve any unknown field in the header */ | |
1101 | if (s->unknown_header_fields_size) { | |
1102 | if (buflen < s->unknown_header_fields_size) { | |
1103 | ret = -ENOSPC; | |
1104 | goto fail; | |
1105 | } | |
1106 | ||
1107 | memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); | |
1108 | buf += s->unknown_header_fields_size; | |
1109 | buflen -= s->unknown_header_fields_size; | |
1110 | } | |
756e6736 | 1111 | |
e24e49e6 KW |
1112 | /* Backing file format header extension */ |
1113 | if (*bs->backing_format) { | |
1114 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, | |
1115 | bs->backing_format, strlen(bs->backing_format), | |
1116 | buflen); | |
1117 | if (ret < 0) { | |
1118 | goto fail; | |
756e6736 KW |
1119 | } |
1120 | ||
e24e49e6 KW |
1121 | buf += ret; |
1122 | buflen -= ret; | |
756e6736 KW |
1123 | } |
1124 | ||
cfcc4c62 KW |
1125 | /* Feature table */ |
1126 | Qcow2Feature features[] = { | |
c61d0004 SH |
1127 | { |
1128 | .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, | |
1129 | .bit = QCOW2_INCOMPAT_DIRTY_BITNR, | |
1130 | .name = "dirty bit", | |
1131 | }, | |
bfe8043e SH |
1132 | { |
1133 | .type = QCOW2_FEAT_TYPE_COMPATIBLE, | |
1134 | .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, | |
1135 | .name = "lazy refcounts", | |
1136 | }, | |
cfcc4c62 KW |
1137 | }; |
1138 | ||
1139 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, | |
1140 | features, sizeof(features), buflen); | |
1141 | if (ret < 0) { | |
1142 | goto fail; | |
1143 | } | |
1144 | buf += ret; | |
1145 | buflen -= ret; | |
1146 | ||
75bab85c KW |
1147 | /* Keep unknown header extensions */ |
1148 | QLIST_FOREACH(uext, &s->unknown_header_ext, next) { | |
1149 | ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); | |
1150 | if (ret < 0) { | |
1151 | goto fail; | |
1152 | } | |
1153 | ||
1154 | buf += ret; | |
1155 | buflen -= ret; | |
1156 | } | |
1157 | ||
e24e49e6 KW |
1158 | /* End of header extensions */ |
1159 | ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); | |
756e6736 KW |
1160 | if (ret < 0) { |
1161 | goto fail; | |
1162 | } | |
1163 | ||
e24e49e6 KW |
1164 | buf += ret; |
1165 | buflen -= ret; | |
756e6736 | 1166 | |
e24e49e6 KW |
1167 | /* Backing file name */ |
1168 | if (*bs->backing_file) { | |
1169 | size_t backing_file_len = strlen(bs->backing_file); | |
1170 | ||
1171 | if (buflen < backing_file_len) { | |
1172 | ret = -ENOSPC; | |
1173 | goto fail; | |
1174 | } | |
1175 | ||
00ea1881 | 1176 | /* Using strncpy is ok here, since buf is not NUL-terminated. */ |
e24e49e6 KW |
1177 | strncpy(buf, bs->backing_file, buflen); |
1178 | ||
1179 | header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); | |
1180 | header->backing_file_size = cpu_to_be32(backing_file_len); | |
756e6736 KW |
1181 | } |
1182 | ||
e24e49e6 KW |
1183 | /* Write the new header */ |
1184 | ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); | |
756e6736 KW |
1185 | if (ret < 0) { |
1186 | goto fail; | |
1187 | } | |
1188 | ||
1189 | ret = 0; | |
1190 | fail: | |
e24e49e6 | 1191 | qemu_vfree(header); |
756e6736 KW |
1192 | return ret; |
1193 | } | |
1194 | ||
1195 | static int qcow2_change_backing_file(BlockDriverState *bs, | |
1196 | const char *backing_file, const char *backing_fmt) | |
1197 | { | |
e24e49e6 KW |
1198 | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
1199 | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); | |
1200 | ||
1201 | return qcow2_update_header(bs); | |
756e6736 KW |
1202 | } |
1203 | ||
a35e1c17 KW |
1204 | static int preallocate(BlockDriverState *bs) |
1205 | { | |
a35e1c17 KW |
1206 | uint64_t nb_sectors; |
1207 | uint64_t offset; | |
060bee89 | 1208 | uint64_t host_offset = 0; |
a35e1c17 | 1209 | int num; |
148da7ea | 1210 | int ret; |
f50f88b9 | 1211 | QCowL2Meta *meta; |
a35e1c17 KW |
1212 | |
1213 | nb_sectors = bdrv_getlength(bs) >> 9; | |
1214 | offset = 0; | |
1215 | ||
1216 | while (nb_sectors) { | |
1217 | num = MIN(nb_sectors, INT_MAX >> 9); | |
060bee89 KW |
1218 | ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, |
1219 | &host_offset, &meta); | |
148da7ea | 1220 | if (ret < 0) { |
19dbcbf7 | 1221 | return ret; |
a35e1c17 KW |
1222 | } |
1223 | ||
f50f88b9 | 1224 | ret = qcow2_alloc_cluster_link_l2(bs, meta); |
19dbcbf7 | 1225 | if (ret < 0) { |
6cfcb9b8 KW |
1226 | qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, |
1227 | QCOW2_DISCARD_NEVER); | |
19dbcbf7 | 1228 | return ret; |
a35e1c17 KW |
1229 | } |
1230 | ||
f214978a KW |
1231 | /* There are no dependent requests, but we need to remove our request |
1232 | * from the list of in-flight requests */ | |
f50f88b9 | 1233 | if (meta != NULL) { |
4e95314e | 1234 | QLIST_REMOVE(meta, next_in_flight); |
f50f88b9 | 1235 | } |
f214978a | 1236 | |
a35e1c17 KW |
1237 | /* TODO Preallocate data if requested */ |
1238 | ||
1239 | nb_sectors -= num; | |
1240 | offset += num << 9; | |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * It is expected that the image file is large enough to actually contain | |
1245 | * all of the allocated clusters (otherwise we get failing reads after | |
1246 | * EOF). Extend the image to the last allocated sector. | |
1247 | */ | |
060bee89 | 1248 | if (host_offset != 0) { |
ea80b906 KW |
1249 | uint8_t buf[512]; |
1250 | memset(buf, 0, 512); | |
060bee89 | 1251 | ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1); |
19dbcbf7 KW |
1252 | if (ret < 0) { |
1253 | return ret; | |
1254 | } | |
a35e1c17 KW |
1255 | } |
1256 | ||
1257 | return 0; | |
1258 | } | |
1259 | ||
7c80ab3f JS |
1260 | static int qcow2_create2(const char *filename, int64_t total_size, |
1261 | const char *backing_file, const char *backing_format, | |
1262 | int flags, size_t cluster_size, int prealloc, | |
6744cbab | 1263 | QEMUOptionParameter *options, int version) |
a9420734 | 1264 | { |
9b2260cb | 1265 | /* Calculate cluster_bits */ |
a9420734 KW |
1266 | int cluster_bits; |
1267 | cluster_bits = ffs(cluster_size) - 1; | |
1268 | if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || | |
1269 | (1 << cluster_bits) != cluster_size) | |
1270 | { | |
1271 | error_report( | |
6daf194d | 1272 | "Cluster size must be a power of two between %d and %dk", |
a9420734 KW |
1273 | 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); |
1274 | return -EINVAL; | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * Open the image file and write a minimal qcow2 header. | |
1279 | * | |
1280 | * We keep things simple and start with a zero-sized image. We also | |
1281 | * do without refcount blocks or a L1 table for now. We'll fix the | |
1282 | * inconsistency later. | |
1283 | * | |
1284 | * We do need a refcount table because growing the refcount table means | |
1285 | * allocating two new refcount blocks - the seconds of which would be at | |
1286 | * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file | |
1287 | * size for any qcow2 image. | |
1288 | */ | |
1289 | BlockDriverState* bs; | |
1290 | QCowHeader header; | |
1291 | uint8_t* refcount_table; | |
1292 | int ret; | |
1293 | ||
1294 | ret = bdrv_create_file(filename, options); | |
1295 | if (ret < 0) { | |
1296 | return ret; | |
1297 | } | |
1298 | ||
787e4a85 | 1299 | ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR); |
a9420734 KW |
1300 | if (ret < 0) { |
1301 | return ret; | |
1302 | } | |
1303 | ||
1304 | /* Write the header */ | |
1305 | memset(&header, 0, sizeof(header)); | |
1306 | header.magic = cpu_to_be32(QCOW_MAGIC); | |
6744cbab | 1307 | header.version = cpu_to_be32(version); |
a9420734 KW |
1308 | header.cluster_bits = cpu_to_be32(cluster_bits); |
1309 | header.size = cpu_to_be64(0); | |
1310 | header.l1_table_offset = cpu_to_be64(0); | |
1311 | header.l1_size = cpu_to_be32(0); | |
1312 | header.refcount_table_offset = cpu_to_be64(cluster_size); | |
1313 | header.refcount_table_clusters = cpu_to_be32(1); | |
6744cbab KW |
1314 | header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT); |
1315 | header.header_length = cpu_to_be32(sizeof(header)); | |
a9420734 KW |
1316 | |
1317 | if (flags & BLOCK_FLAG_ENCRYPT) { | |
1318 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); | |
1319 | } else { | |
1320 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
1321 | } | |
1322 | ||
bfe8043e SH |
1323 | if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { |
1324 | header.compatible_features |= | |
1325 | cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); | |
1326 | } | |
1327 | ||
a9420734 KW |
1328 | ret = bdrv_pwrite(bs, 0, &header, sizeof(header)); |
1329 | if (ret < 0) { | |
1330 | goto out; | |
1331 | } | |
1332 | ||
1333 | /* Write an empty refcount table */ | |
7267c094 | 1334 | refcount_table = g_malloc0(cluster_size); |
a9420734 | 1335 | ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size); |
7267c094 | 1336 | g_free(refcount_table); |
a9420734 KW |
1337 | |
1338 | if (ret < 0) { | |
1339 | goto out; | |
1340 | } | |
1341 | ||
1342 | bdrv_close(bs); | |
1343 | ||
1344 | /* | |
1345 | * And now open the image and make it consistent first (i.e. increase the | |
1346 | * refcount of the cluster that is occupied by the header and the refcount | |
1347 | * table) | |
1348 | */ | |
1349 | BlockDriver* drv = bdrv_find_format("qcow2"); | |
1350 | assert(drv != NULL); | |
de9c0cec | 1351 | ret = bdrv_open(bs, filename, NULL, |
e1a7107f | 1352 | BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv); |
a9420734 KW |
1353 | if (ret < 0) { |
1354 | goto out; | |
1355 | } | |
1356 | ||
1357 | ret = qcow2_alloc_clusters(bs, 2 * cluster_size); | |
1358 | if (ret < 0) { | |
1359 | goto out; | |
1360 | ||
1361 | } else if (ret != 0) { | |
1362 | error_report("Huh, first cluster in empty image is already in use?"); | |
1363 | abort(); | |
1364 | } | |
1365 | ||
1366 | /* Okay, now that we have a valid image, let's give it the right size */ | |
1367 | ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE); | |
1368 | if (ret < 0) { | |
1369 | goto out; | |
1370 | } | |
1371 | ||
1372 | /* Want a backing file? There you go.*/ | |
1373 | if (backing_file) { | |
1374 | ret = bdrv_change_backing_file(bs, backing_file, backing_format); | |
1375 | if (ret < 0) { | |
1376 | goto out; | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | /* And if we're supposed to preallocate metadata, do that now */ | |
1381 | if (prealloc) { | |
15552c4a ZYW |
1382 | BDRVQcowState *s = bs->opaque; |
1383 | qemu_co_mutex_lock(&s->lock); | |
a9420734 | 1384 | ret = preallocate(bs); |
15552c4a | 1385 | qemu_co_mutex_unlock(&s->lock); |
a9420734 KW |
1386 | if (ret < 0) { |
1387 | goto out; | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | ret = 0; | |
1392 | out: | |
1393 | bdrv_delete(bs); | |
1394 | return ret; | |
1395 | } | |
de5f3f40 | 1396 | |
7c80ab3f | 1397 | static int qcow2_create(const char *filename, QEMUOptionParameter *options) |
de5f3f40 KW |
1398 | { |
1399 | const char *backing_file = NULL; | |
1400 | const char *backing_fmt = NULL; | |
1401 | uint64_t sectors = 0; | |
1402 | int flags = 0; | |
99cce9fa | 1403 | size_t cluster_size = DEFAULT_CLUSTER_SIZE; |
de5f3f40 | 1404 | int prealloc = 0; |
6744cbab | 1405 | int version = 2; |
de5f3f40 KW |
1406 | |
1407 | /* Read out options */ | |
1408 | while (options && options->name) { | |
1409 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
1410 | sectors = options->value.n / 512; | |
1411 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
1412 | backing_file = options->value.s; | |
1413 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { | |
1414 | backing_fmt = options->value.s; | |
1415 | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { | |
1416 | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; | |
1417 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { | |
1418 | if (options->value.n) { | |
1419 | cluster_size = options->value.n; | |
1420 | } | |
1421 | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { | |
1422 | if (!options->value.s || !strcmp(options->value.s, "off")) { | |
1423 | prealloc = 0; | |
1424 | } else if (!strcmp(options->value.s, "metadata")) { | |
1425 | prealloc = 1; | |
1426 | } else { | |
1427 | fprintf(stderr, "Invalid preallocation mode: '%s'\n", | |
1428 | options->value.s); | |
1429 | return -EINVAL; | |
1430 | } | |
6744cbab KW |
1431 | } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) { |
1432 | if (!options->value.s || !strcmp(options->value.s, "0.10")) { | |
1433 | version = 2; | |
1434 | } else if (!strcmp(options->value.s, "1.1")) { | |
1435 | version = 3; | |
1436 | } else { | |
1437 | fprintf(stderr, "Invalid compatibility level: '%s'\n", | |
1438 | options->value.s); | |
1439 | return -EINVAL; | |
1440 | } | |
bfe8043e SH |
1441 | } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) { |
1442 | flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0; | |
de5f3f40 KW |
1443 | } |
1444 | options++; | |
1445 | } | |
1446 | ||
1447 | if (backing_file && prealloc) { | |
1448 | fprintf(stderr, "Backing file and preallocation cannot be used at " | |
1449 | "the same time\n"); | |
1450 | return -EINVAL; | |
1451 | } | |
1452 | ||
bfe8043e SH |
1453 | if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) { |
1454 | fprintf(stderr, "Lazy refcounts only supported with compatibility " | |
1455 | "level 1.1 and above (use compat=1.1 or greater)\n"); | |
1456 | return -EINVAL; | |
1457 | } | |
1458 | ||
7c80ab3f | 1459 | return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags, |
6744cbab | 1460 | cluster_size, prealloc, options, version); |
de5f3f40 KW |
1461 | } |
1462 | ||
7c80ab3f | 1463 | static int qcow2_make_empty(BlockDriverState *bs) |
20d97356 BS |
1464 | { |
1465 | #if 0 | |
1466 | /* XXX: not correct */ | |
1467 | BDRVQcowState *s = bs->opaque; | |
1468 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); | |
1469 | int ret; | |
1470 | ||
1471 | memset(s->l1_table, 0, l1_length); | |
66f82cee | 1472 | if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) |
20d97356 | 1473 | return -1; |
66f82cee | 1474 | ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length); |
20d97356 BS |
1475 | if (ret < 0) |
1476 | return ret; | |
1477 | ||
1478 | l2_cache_reset(bs); | |
1479 | #endif | |
1480 | return 0; | |
1481 | } | |
1482 | ||
621f0589 KW |
1483 | static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, |
1484 | int64_t sector_num, int nb_sectors) | |
1485 | { | |
1486 | int ret; | |
1487 | BDRVQcowState *s = bs->opaque; | |
1488 | ||
1489 | /* Emulate misaligned zero writes */ | |
1490 | if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) { | |
1491 | return -ENOTSUP; | |
1492 | } | |
1493 | ||
1494 | /* Whatever is left can use real zero clusters */ | |
1495 | qemu_co_mutex_lock(&s->lock); | |
1496 | ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, | |
1497 | nb_sectors); | |
1498 | qemu_co_mutex_unlock(&s->lock); | |
1499 | ||
1500 | return ret; | |
1501 | } | |
1502 | ||
6db39ae2 PB |
1503 | static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, |
1504 | int64_t sector_num, int nb_sectors) | |
5ea929e3 | 1505 | { |
6db39ae2 PB |
1506 | int ret; |
1507 | BDRVQcowState *s = bs->opaque; | |
1508 | ||
1509 | qemu_co_mutex_lock(&s->lock); | |
1510 | ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS, | |
5ea929e3 | 1511 | nb_sectors); |
6db39ae2 PB |
1512 | qemu_co_mutex_unlock(&s->lock); |
1513 | return ret; | |
5ea929e3 KW |
1514 | } |
1515 | ||
419b19d9 SH |
1516 | static int qcow2_truncate(BlockDriverState *bs, int64_t offset) |
1517 | { | |
1518 | BDRVQcowState *s = bs->opaque; | |
2cf7cfa1 KW |
1519 | int64_t new_l1_size; |
1520 | int ret; | |
419b19d9 SH |
1521 | |
1522 | if (offset & 511) { | |
259b2173 | 1523 | error_report("The new size must be a multiple of 512"); |
419b19d9 SH |
1524 | return -EINVAL; |
1525 | } | |
1526 | ||
1527 | /* cannot proceed if image has snapshots */ | |
1528 | if (s->nb_snapshots) { | |
259b2173 | 1529 | error_report("Can't resize an image which has snapshots"); |
419b19d9 SH |
1530 | return -ENOTSUP; |
1531 | } | |
1532 | ||
1533 | /* shrinking is currently not supported */ | |
1534 | if (offset < bs->total_sectors * 512) { | |
259b2173 | 1535 | error_report("qcow2 doesn't support shrinking images yet"); |
419b19d9 SH |
1536 | return -ENOTSUP; |
1537 | } | |
1538 | ||
1539 | new_l1_size = size_to_l1(s, offset); | |
72893756 | 1540 | ret = qcow2_grow_l1_table(bs, new_l1_size, true); |
419b19d9 SH |
1541 | if (ret < 0) { |
1542 | return ret; | |
1543 | } | |
1544 | ||
1545 | /* write updated header.size */ | |
1546 | offset = cpu_to_be64(offset); | |
8b3b7206 KW |
1547 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |
1548 | &offset, sizeof(uint64_t)); | |
419b19d9 SH |
1549 | if (ret < 0) { |
1550 | return ret; | |
1551 | } | |
1552 | ||
1553 | s->l1_vm_state_index = new_l1_size; | |
1554 | return 0; | |
1555 | } | |
1556 | ||
20d97356 BS |
1557 | /* XXX: put compressed sectors first, then all the cluster aligned |
1558 | tables to avoid losing bytes in alignment */ | |
7c80ab3f JS |
1559 | static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, |
1560 | const uint8_t *buf, int nb_sectors) | |
20d97356 BS |
1561 | { |
1562 | BDRVQcowState *s = bs->opaque; | |
1563 | z_stream strm; | |
1564 | int ret, out_len; | |
1565 | uint8_t *out_buf; | |
1566 | uint64_t cluster_offset; | |
1567 | ||
1568 | if (nb_sectors == 0) { | |
1569 | /* align end of file to a sector boundary to ease reading with | |
1570 | sector based I/Os */ | |
66f82cee | 1571 | cluster_offset = bdrv_getlength(bs->file); |
20d97356 | 1572 | cluster_offset = (cluster_offset + 511) & ~511; |
66f82cee | 1573 | bdrv_truncate(bs->file, cluster_offset); |
20d97356 BS |
1574 | return 0; |
1575 | } | |
1576 | ||
f4d38bef SH |
1577 | if (nb_sectors != s->cluster_sectors) { |
1578 | ret = -EINVAL; | |
1579 | ||
1580 | /* Zero-pad last write if image size is not cluster aligned */ | |
1581 | if (sector_num + nb_sectors == bs->total_sectors && | |
1582 | nb_sectors < s->cluster_sectors) { | |
1583 | uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); | |
1584 | memset(pad_buf, 0, s->cluster_size); | |
1585 | memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); | |
1586 | ret = qcow2_write_compressed(bs, sector_num, | |
1587 | pad_buf, s->cluster_sectors); | |
1588 | qemu_vfree(pad_buf); | |
1589 | } | |
1590 | return ret; | |
1591 | } | |
20d97356 | 1592 | |
7267c094 | 1593 | out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
20d97356 BS |
1594 | |
1595 | /* best compression, small window, no zlib header */ | |
1596 | memset(&strm, 0, sizeof(strm)); | |
1597 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
1598 | Z_DEFLATED, -12, | |
1599 | 9, Z_DEFAULT_STRATEGY); | |
1600 | if (ret != 0) { | |
8f1efd00 KW |
1601 | ret = -EINVAL; |
1602 | goto fail; | |
20d97356 BS |
1603 | } |
1604 | ||
1605 | strm.avail_in = s->cluster_size; | |
1606 | strm.next_in = (uint8_t *)buf; | |
1607 | strm.avail_out = s->cluster_size; | |
1608 | strm.next_out = out_buf; | |
1609 | ||
1610 | ret = deflate(&strm, Z_FINISH); | |
1611 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
20d97356 | 1612 | deflateEnd(&strm); |
8f1efd00 KW |
1613 | ret = -EINVAL; |
1614 | goto fail; | |
20d97356 BS |
1615 | } |
1616 | out_len = strm.next_out - out_buf; | |
1617 | ||
1618 | deflateEnd(&strm); | |
1619 | ||
1620 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
1621 | /* could not compress: write normal cluster */ | |
8f1efd00 KW |
1622 | ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
1623 | if (ret < 0) { | |
1624 | goto fail; | |
1625 | } | |
20d97356 BS |
1626 | } else { |
1627 | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, | |
1628 | sector_num << 9, out_len); | |
8f1efd00 KW |
1629 | if (!cluster_offset) { |
1630 | ret = -EIO; | |
1631 | goto fail; | |
1632 | } | |
20d97356 | 1633 | cluster_offset &= s->cluster_offset_mask; |
66f82cee | 1634 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); |
8f1efd00 KW |
1635 | ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len); |
1636 | if (ret < 0) { | |
1637 | goto fail; | |
20d97356 BS |
1638 | } |
1639 | } | |
1640 | ||
8f1efd00 KW |
1641 | ret = 0; |
1642 | fail: | |
7267c094 | 1643 | g_free(out_buf); |
8f1efd00 | 1644 | return ret; |
20d97356 BS |
1645 | } |
1646 | ||
a968168c | 1647 | static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) |
20d97356 | 1648 | { |
29c1a730 KW |
1649 | BDRVQcowState *s = bs->opaque; |
1650 | int ret; | |
1651 | ||
8b94ff85 | 1652 | qemu_co_mutex_lock(&s->lock); |
29c1a730 KW |
1653 | ret = qcow2_cache_flush(bs, s->l2_table_cache); |
1654 | if (ret < 0) { | |
c95de7e2 | 1655 | qemu_co_mutex_unlock(&s->lock); |
8b94ff85 | 1656 | return ret; |
29c1a730 KW |
1657 | } |
1658 | ||
bfe8043e SH |
1659 | if (qcow2_need_accurate_refcounts(s)) { |
1660 | ret = qcow2_cache_flush(bs, s->refcount_block_cache); | |
1661 | if (ret < 0) { | |
1662 | qemu_co_mutex_unlock(&s->lock); | |
1663 | return ret; | |
1664 | } | |
29c1a730 | 1665 | } |
8b94ff85 | 1666 | qemu_co_mutex_unlock(&s->lock); |
29c1a730 | 1667 | |
eb489bb1 KW |
1668 | return 0; |
1669 | } | |
1670 | ||
7c80ab3f | 1671 | static int64_t qcow2_vm_state_offset(BDRVQcowState *s) |
20d97356 BS |
1672 | { |
1673 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); | |
1674 | } | |
1675 | ||
7c80ab3f | 1676 | static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
20d97356 BS |
1677 | { |
1678 | BDRVQcowState *s = bs->opaque; | |
1679 | bdi->cluster_size = s->cluster_size; | |
7c80ab3f | 1680 | bdi->vm_state_offset = qcow2_vm_state_offset(s); |
20d97356 BS |
1681 | return 0; |
1682 | } | |
1683 | ||
20d97356 BS |
1684 | #if 0 |
1685 | static void dump_refcounts(BlockDriverState *bs) | |
1686 | { | |
1687 | BDRVQcowState *s = bs->opaque; | |
1688 | int64_t nb_clusters, k, k1, size; | |
1689 | int refcount; | |
1690 | ||
66f82cee | 1691 | size = bdrv_getlength(bs->file); |
20d97356 BS |
1692 | nb_clusters = size_to_clusters(s, size); |
1693 | for(k = 0; k < nb_clusters;) { | |
1694 | k1 = k; | |
1695 | refcount = get_refcount(bs, k); | |
1696 | k++; | |
1697 | while (k < nb_clusters && get_refcount(bs, k) == refcount) | |
1698 | k++; | |
0bfcd599 BS |
1699 | printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, |
1700 | k - k1); | |
20d97356 BS |
1701 | } |
1702 | } | |
1703 | #endif | |
1704 | ||
cf8074b3 KW |
1705 | static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, |
1706 | int64_t pos) | |
20d97356 BS |
1707 | { |
1708 | BDRVQcowState *s = bs->opaque; | |
1709 | int growable = bs->growable; | |
1710 | int ret; | |
1711 | ||
66f82cee | 1712 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
20d97356 | 1713 | bs->growable = 1; |
8d3b1a2d | 1714 | ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov); |
20d97356 BS |
1715 | bs->growable = growable; |
1716 | ||
1717 | return ret; | |
1718 | } | |
1719 | ||
7c80ab3f JS |
1720 | static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
1721 | int64_t pos, int size) | |
20d97356 BS |
1722 | { |
1723 | BDRVQcowState *s = bs->opaque; | |
1724 | int growable = bs->growable; | |
1725 | int ret; | |
1726 | ||
66f82cee | 1727 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
20d97356 | 1728 | bs->growable = 1; |
7c80ab3f | 1729 | ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size); |
20d97356 BS |
1730 | bs->growable = growable; |
1731 | ||
1732 | return ret; | |
1733 | } | |
1734 | ||
7c80ab3f | 1735 | static QEMUOptionParameter qcow2_create_options[] = { |
20d97356 BS |
1736 | { |
1737 | .name = BLOCK_OPT_SIZE, | |
1738 | .type = OPT_SIZE, | |
1739 | .help = "Virtual disk size" | |
1740 | }, | |
6744cbab KW |
1741 | { |
1742 | .name = BLOCK_OPT_COMPAT_LEVEL, | |
1743 | .type = OPT_STRING, | |
1744 | .help = "Compatibility level (0.10 or 1.1)" | |
1745 | }, | |
20d97356 BS |
1746 | { |
1747 | .name = BLOCK_OPT_BACKING_FILE, | |
1748 | .type = OPT_STRING, | |
1749 | .help = "File name of a base image" | |
1750 | }, | |
1751 | { | |
1752 | .name = BLOCK_OPT_BACKING_FMT, | |
1753 | .type = OPT_STRING, | |
1754 | .help = "Image format of the base image" | |
1755 | }, | |
1756 | { | |
1757 | .name = BLOCK_OPT_ENCRYPT, | |
1758 | .type = OPT_FLAG, | |
1759 | .help = "Encrypt the image" | |
1760 | }, | |
1761 | { | |
1762 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1763 | .type = OPT_SIZE, | |
99cce9fa KW |
1764 | .help = "qcow2 cluster size", |
1765 | .value = { .n = DEFAULT_CLUSTER_SIZE }, | |
20d97356 BS |
1766 | }, |
1767 | { | |
1768 | .name = BLOCK_OPT_PREALLOC, | |
1769 | .type = OPT_STRING, | |
1770 | .help = "Preallocation mode (allowed values: off, metadata)" | |
1771 | }, | |
bfe8043e SH |
1772 | { |
1773 | .name = BLOCK_OPT_LAZY_REFCOUNTS, | |
1774 | .type = OPT_FLAG, | |
1775 | .help = "Postpone refcount updates", | |
1776 | }, | |
20d97356 BS |
1777 | { NULL } |
1778 | }; | |
1779 | ||
1780 | static BlockDriver bdrv_qcow2 = { | |
7c80ab3f JS |
1781 | .format_name = "qcow2", |
1782 | .instance_size = sizeof(BDRVQcowState), | |
1783 | .bdrv_probe = qcow2_probe, | |
1784 | .bdrv_open = qcow2_open, | |
1785 | .bdrv_close = qcow2_close, | |
21d82ac9 | 1786 | .bdrv_reopen_prepare = qcow2_reopen_prepare, |
7c80ab3f | 1787 | .bdrv_create = qcow2_create, |
f8a2e5e3 | 1788 | .bdrv_co_is_allocated = qcow2_co_is_allocated, |
7c80ab3f JS |
1789 | .bdrv_set_key = qcow2_set_key, |
1790 | .bdrv_make_empty = qcow2_make_empty, | |
1791 | ||
c68b89ac KW |
1792 | .bdrv_co_readv = qcow2_co_readv, |
1793 | .bdrv_co_writev = qcow2_co_writev, | |
eb489bb1 | 1794 | .bdrv_co_flush_to_os = qcow2_co_flush_to_os, |
419b19d9 | 1795 | |
621f0589 | 1796 | .bdrv_co_write_zeroes = qcow2_co_write_zeroes, |
6db39ae2 | 1797 | .bdrv_co_discard = qcow2_co_discard, |
419b19d9 | 1798 | .bdrv_truncate = qcow2_truncate, |
7c80ab3f | 1799 | .bdrv_write_compressed = qcow2_write_compressed, |
20d97356 BS |
1800 | |
1801 | .bdrv_snapshot_create = qcow2_snapshot_create, | |
1802 | .bdrv_snapshot_goto = qcow2_snapshot_goto, | |
1803 | .bdrv_snapshot_delete = qcow2_snapshot_delete, | |
1804 | .bdrv_snapshot_list = qcow2_snapshot_list, | |
51ef6727 | 1805 | .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, |
7c80ab3f | 1806 | .bdrv_get_info = qcow2_get_info, |
20d97356 | 1807 | |
7c80ab3f JS |
1808 | .bdrv_save_vmstate = qcow2_save_vmstate, |
1809 | .bdrv_load_vmstate = qcow2_load_vmstate, | |
20d97356 BS |
1810 | |
1811 | .bdrv_change_backing_file = qcow2_change_backing_file, | |
1812 | ||
06d9260f AL |
1813 | .bdrv_invalidate_cache = qcow2_invalidate_cache, |
1814 | ||
7c80ab3f JS |
1815 | .create_options = qcow2_create_options, |
1816 | .bdrv_check = qcow2_check, | |
20d97356 BS |
1817 | }; |
1818 | ||
5efa9d5a AL |
1819 | static void bdrv_qcow2_init(void) |
1820 | { | |
1821 | bdrv_register(&bdrv_qcow2); | |
1822 | } | |
1823 | ||
1824 | block_init(bdrv_qcow2_init); |