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