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