1585f8587Sbellard /* 2585f8587Sbellard * Block driver for the QCOW version 2 format 3585f8587Sbellard * 4585f8587Sbellard * Copyright (c) 2004-2006 Fabrice Bellard 5585f8587Sbellard * 6585f8587Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 7585f8587Sbellard * of this software and associated documentation files (the "Software"), to deal 8585f8587Sbellard * in the Software without restriction, including without limitation the rights 9585f8587Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10585f8587Sbellard * copies of the Software, and to permit persons to whom the Software is 11585f8587Sbellard * furnished to do so, subject to the following conditions: 12585f8587Sbellard * 13585f8587Sbellard * The above copyright notice and this permission notice shall be included in 14585f8587Sbellard * all copies or substantial portions of the Software. 15585f8587Sbellard * 16585f8587Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17585f8587Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18585f8587Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19585f8587Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20585f8587Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21585f8587Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22585f8587Sbellard * THE SOFTWARE. 23585f8587Sbellard */ 24faf07963Spbrook #include "qemu-common.h" 25737e150eSPaolo Bonzini #include "block/block_int.h" 261de7afc9SPaolo Bonzini #include "qemu/module.h" 27585f8587Sbellard #include <zlib.h> 28753d9b82SAurelien Jarno #include "qemu/aes.h" 29f7d0fe02SKevin Wolf #include "block/qcow2.h" 301de7afc9SPaolo Bonzini #include "qemu/error-report.h" 317b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 32acdfb480SKevin Wolf #include "qapi/qmp/qbool.h" 333cce16f4SKevin Wolf #include "trace.h" 34585f8587Sbellard 35585f8587Sbellard /* 36585f8587Sbellard Differences with QCOW: 37585f8587Sbellard 38585f8587Sbellard - Support for multiple incremental snapshots. 39585f8587Sbellard - Memory management by reference counts. 40585f8587Sbellard - Clusters which have a reference count of one have the bit 41585f8587Sbellard QCOW_OFLAG_COPIED to optimize write performance. 42585f8587Sbellard - Size of compressed clusters is stored in sectors to reduce bit usage 43585f8587Sbellard in the cluster offsets. 44585f8587Sbellard - Support for storing additional data (such as the VM state) in the 45585f8587Sbellard snapshots. 46585f8587Sbellard - If a backing store is used, the cluster size is not constrained 47585f8587Sbellard (could be backported to QCOW). 48585f8587Sbellard - L2 tables have always a size of one cluster. 49585f8587Sbellard */ 50585f8587Sbellard 519b80ddf3Saliguori 529b80ddf3Saliguori typedef struct { 539b80ddf3Saliguori uint32_t magic; 549b80ddf3Saliguori uint32_t len; 559b80ddf3Saliguori } QCowExtension; 5621d82ac9SJeff Cody 577c80ab3fSJes Sorensen #define QCOW2_EXT_MAGIC_END 0 587c80ab3fSJes Sorensen #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA 59cfcc4c62SKevin Wolf #define QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857 609b80ddf3Saliguori 617c80ab3fSJes Sorensen static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename) 62585f8587Sbellard { 63585f8587Sbellard const QCowHeader *cow_header = (const void *)buf; 64585f8587Sbellard 65585f8587Sbellard if (buf_size >= sizeof(QCowHeader) && 66585f8587Sbellard be32_to_cpu(cow_header->magic) == QCOW_MAGIC && 676744cbabSKevin Wolf be32_to_cpu(cow_header->version) >= 2) 68585f8587Sbellard return 100; 69585f8587Sbellard else 70585f8587Sbellard return 0; 71585f8587Sbellard } 72585f8587Sbellard 739b80ddf3Saliguori 749b80ddf3Saliguori /* 759b80ddf3Saliguori * read qcow2 extension and fill bs 769b80ddf3Saliguori * start reading from start_offset 779b80ddf3Saliguori * finish reading upon magic of value 0 or when end_offset reached 789b80ddf3Saliguori * unknown magic is skipped (future extension this version knows nothing about) 799b80ddf3Saliguori * return 0 upon success, non-0 otherwise 809b80ddf3Saliguori */ 817c80ab3fSJes Sorensen static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, 82cfcc4c62SKevin Wolf uint64_t end_offset, void **p_feature_table) 839b80ddf3Saliguori { 8475bab85cSKevin Wolf BDRVQcowState *s = bs->opaque; 859b80ddf3Saliguori QCowExtension ext; 869b80ddf3Saliguori uint64_t offset; 8775bab85cSKevin Wolf int ret; 889b80ddf3Saliguori 899b80ddf3Saliguori #ifdef DEBUG_EXT 907c80ab3fSJes Sorensen printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); 919b80ddf3Saliguori #endif 929b80ddf3Saliguori offset = start_offset; 939b80ddf3Saliguori while (offset < end_offset) { 949b80ddf3Saliguori 959b80ddf3Saliguori #ifdef DEBUG_EXT 969b80ddf3Saliguori /* Sanity check */ 979b80ddf3Saliguori if (offset > s->cluster_size) 987c80ab3fSJes Sorensen printf("qcow2_read_extension: suspicious offset %lu\n", offset); 999b80ddf3Saliguori 1009b2260cbSDong Xu Wang printf("attempting to read extended header in offset %lu\n", offset); 1019b80ddf3Saliguori #endif 1029b80ddf3Saliguori 10366f82ceeSKevin Wolf if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) { 1047c80ab3fSJes Sorensen fprintf(stderr, "qcow2_read_extension: ERROR: " 1050bfcd599SBlue Swirl "pread fail from offset %" PRIu64 "\n", 1060bfcd599SBlue Swirl offset); 1079b80ddf3Saliguori return 1; 1089b80ddf3Saliguori } 1099b80ddf3Saliguori be32_to_cpus(&ext.magic); 1109b80ddf3Saliguori be32_to_cpus(&ext.len); 1119b80ddf3Saliguori offset += sizeof(ext); 1129b80ddf3Saliguori #ifdef DEBUG_EXT 1139b80ddf3Saliguori printf("ext.magic = 0x%x\n", ext.magic); 1149b80ddf3Saliguori #endif 11564ca6aeeSKevin Wolf if (ext.len > end_offset - offset) { 11664ca6aeeSKevin Wolf error_report("Header extension too large"); 11764ca6aeeSKevin Wolf return -EINVAL; 11864ca6aeeSKevin Wolf } 11964ca6aeeSKevin Wolf 1209b80ddf3Saliguori switch (ext.magic) { 1217c80ab3fSJes Sorensen case QCOW2_EXT_MAGIC_END: 1229b80ddf3Saliguori return 0; 123f965509cSaliguori 1247c80ab3fSJes Sorensen case QCOW2_EXT_MAGIC_BACKING_FORMAT: 125f965509cSaliguori if (ext.len >= sizeof(bs->backing_format)) { 126f965509cSaliguori fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" 1274c978075Saliguori " (>=%zu)\n", 128f965509cSaliguori ext.len, sizeof(bs->backing_format)); 129f965509cSaliguori return 2; 130f965509cSaliguori } 13166f82ceeSKevin Wolf if (bdrv_pread(bs->file, offset , bs->backing_format, 132f965509cSaliguori ext.len) != ext.len) 133f965509cSaliguori return 3; 134f965509cSaliguori bs->backing_format[ext.len] = '\0'; 135f965509cSaliguori #ifdef DEBUG_EXT 136f965509cSaliguori printf("Qcow2: Got format extension %s\n", bs->backing_format); 137f965509cSaliguori #endif 138f965509cSaliguori break; 139f965509cSaliguori 140cfcc4c62SKevin Wolf case QCOW2_EXT_MAGIC_FEATURE_TABLE: 141cfcc4c62SKevin Wolf if (p_feature_table != NULL) { 142cfcc4c62SKevin Wolf void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); 143cfcc4c62SKevin Wolf ret = bdrv_pread(bs->file, offset , feature_table, ext.len); 144cfcc4c62SKevin Wolf if (ret < 0) { 145cfcc4c62SKevin Wolf return ret; 146cfcc4c62SKevin Wolf } 147cfcc4c62SKevin Wolf 148cfcc4c62SKevin Wolf *p_feature_table = feature_table; 149cfcc4c62SKevin Wolf } 150cfcc4c62SKevin Wolf break; 151cfcc4c62SKevin Wolf 1529b80ddf3Saliguori default: 15375bab85cSKevin Wolf /* unknown magic - save it in case we need to rewrite the header */ 15475bab85cSKevin Wolf { 15575bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext; 15675bab85cSKevin Wolf 15775bab85cSKevin Wolf uext = g_malloc0(sizeof(*uext) + ext.len); 15875bab85cSKevin Wolf uext->magic = ext.magic; 15975bab85cSKevin Wolf uext->len = ext.len; 16075bab85cSKevin Wolf QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); 16175bab85cSKevin Wolf 16275bab85cSKevin Wolf ret = bdrv_pread(bs->file, offset , uext->data, uext->len); 16375bab85cSKevin Wolf if (ret < 0) { 16475bab85cSKevin Wolf return ret; 16575bab85cSKevin Wolf } 16675bab85cSKevin Wolf } 1679b80ddf3Saliguori break; 1689b80ddf3Saliguori } 169fd29b4bbSKevin Wolf 170fd29b4bbSKevin Wolf offset += ((ext.len + 7) & ~7); 1719b80ddf3Saliguori } 1729b80ddf3Saliguori 1739b80ddf3Saliguori return 0; 1749b80ddf3Saliguori } 1759b80ddf3Saliguori 17675bab85cSKevin Wolf static void cleanup_unknown_header_ext(BlockDriverState *bs) 17775bab85cSKevin Wolf { 17875bab85cSKevin Wolf BDRVQcowState *s = bs->opaque; 17975bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext, *next; 18075bab85cSKevin Wolf 18175bab85cSKevin Wolf QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { 18275bab85cSKevin Wolf QLIST_REMOVE(uext, next); 18375bab85cSKevin Wolf g_free(uext); 18475bab85cSKevin Wolf } 18575bab85cSKevin Wolf } 1869b80ddf3Saliguori 187b9531b6eSStefan Weil static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState *bs, 188b9531b6eSStefan Weil const char *fmt, ...) 1896744cbabSKevin Wolf { 1906744cbabSKevin Wolf char msg[64]; 1916744cbabSKevin Wolf va_list ap; 1926744cbabSKevin Wolf 1936744cbabSKevin Wolf va_start(ap, fmt); 1946744cbabSKevin Wolf vsnprintf(msg, sizeof(msg), fmt, ap); 1956744cbabSKevin Wolf va_end(ap); 1966744cbabSKevin Wolf 1976744cbabSKevin Wolf qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, 1986744cbabSKevin Wolf bs->device_name, "qcow2", msg); 1996744cbabSKevin Wolf } 2006744cbabSKevin Wolf 201cfcc4c62SKevin Wolf static void report_unsupported_feature(BlockDriverState *bs, 202cfcc4c62SKevin Wolf Qcow2Feature *table, uint64_t mask) 203cfcc4c62SKevin Wolf { 204cfcc4c62SKevin Wolf while (table && table->name[0] != '\0') { 205cfcc4c62SKevin Wolf if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { 206cfcc4c62SKevin Wolf if (mask & (1 << table->bit)) { 207cfcc4c62SKevin Wolf report_unsupported(bs, "%.46s",table->name); 208cfcc4c62SKevin Wolf mask &= ~(1 << table->bit); 209cfcc4c62SKevin Wolf } 210cfcc4c62SKevin Wolf } 211cfcc4c62SKevin Wolf table++; 212cfcc4c62SKevin Wolf } 213cfcc4c62SKevin Wolf 214cfcc4c62SKevin Wolf if (mask) { 215cfcc4c62SKevin Wolf report_unsupported(bs, "Unknown incompatible feature: %" PRIx64, mask); 216cfcc4c62SKevin Wolf } 217cfcc4c62SKevin Wolf } 218cfcc4c62SKevin Wolf 219c61d0004SStefan Hajnoczi /* 220bfe8043eSStefan Hajnoczi * Sets the dirty bit and flushes afterwards if necessary. 221bfe8043eSStefan Hajnoczi * 222bfe8043eSStefan Hajnoczi * The incompatible_features bit is only set if the image file header was 223bfe8043eSStefan Hajnoczi * updated successfully. Therefore it is not required to check the return 224bfe8043eSStefan Hajnoczi * value of this function. 225bfe8043eSStefan Hajnoczi */ 226280d3735SKevin Wolf int qcow2_mark_dirty(BlockDriverState *bs) 227bfe8043eSStefan Hajnoczi { 228bfe8043eSStefan Hajnoczi BDRVQcowState *s = bs->opaque; 229bfe8043eSStefan Hajnoczi uint64_t val; 230bfe8043eSStefan Hajnoczi int ret; 231bfe8043eSStefan Hajnoczi 232bfe8043eSStefan Hajnoczi assert(s->qcow_version >= 3); 233bfe8043eSStefan Hajnoczi 234bfe8043eSStefan Hajnoczi if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 235bfe8043eSStefan Hajnoczi return 0; /* already dirty */ 236bfe8043eSStefan Hajnoczi } 237bfe8043eSStefan Hajnoczi 238bfe8043eSStefan Hajnoczi val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); 239bfe8043eSStefan Hajnoczi ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), 240bfe8043eSStefan Hajnoczi &val, sizeof(val)); 241bfe8043eSStefan Hajnoczi if (ret < 0) { 242bfe8043eSStefan Hajnoczi return ret; 243bfe8043eSStefan Hajnoczi } 244bfe8043eSStefan Hajnoczi ret = bdrv_flush(bs->file); 245bfe8043eSStefan Hajnoczi if (ret < 0) { 246bfe8043eSStefan Hajnoczi return ret; 247bfe8043eSStefan Hajnoczi } 248bfe8043eSStefan Hajnoczi 249bfe8043eSStefan Hajnoczi /* Only treat image as dirty if the header was updated successfully */ 250bfe8043eSStefan Hajnoczi s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; 251bfe8043eSStefan Hajnoczi return 0; 252bfe8043eSStefan Hajnoczi } 253bfe8043eSStefan Hajnoczi 254bfe8043eSStefan Hajnoczi /* 255c61d0004SStefan Hajnoczi * Clears the dirty bit and flushes before if necessary. Only call this 256c61d0004SStefan Hajnoczi * function when there are no pending requests, it does not guard against 257c61d0004SStefan Hajnoczi * concurrent requests dirtying the image. 258c61d0004SStefan Hajnoczi */ 259c61d0004SStefan Hajnoczi static int qcow2_mark_clean(BlockDriverState *bs) 260c61d0004SStefan Hajnoczi { 261c61d0004SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 262c61d0004SStefan Hajnoczi 263c61d0004SStefan Hajnoczi if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 264c61d0004SStefan Hajnoczi int ret = bdrv_flush(bs); 265c61d0004SStefan Hajnoczi if (ret < 0) { 266c61d0004SStefan Hajnoczi return ret; 267c61d0004SStefan Hajnoczi } 268c61d0004SStefan Hajnoczi 269c61d0004SStefan Hajnoczi s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; 270c61d0004SStefan Hajnoczi return qcow2_update_header(bs); 271c61d0004SStefan Hajnoczi } 272c61d0004SStefan Hajnoczi return 0; 273c61d0004SStefan Hajnoczi } 274c61d0004SStefan Hajnoczi 275acbe5982SStefan Hajnoczi static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, 276acbe5982SStefan Hajnoczi BdrvCheckMode fix) 277acbe5982SStefan Hajnoczi { 278acbe5982SStefan Hajnoczi int ret = qcow2_check_refcounts(bs, result, fix); 279acbe5982SStefan Hajnoczi if (ret < 0) { 280acbe5982SStefan Hajnoczi return ret; 281acbe5982SStefan Hajnoczi } 282acbe5982SStefan Hajnoczi 283acbe5982SStefan Hajnoczi if (fix && result->check_errors == 0 && result->corruptions == 0) { 284acbe5982SStefan Hajnoczi return qcow2_mark_clean(bs); 285acbe5982SStefan Hajnoczi } 286acbe5982SStefan Hajnoczi return ret; 287acbe5982SStefan Hajnoczi } 288acbe5982SStefan Hajnoczi 28974c4510aSKevin Wolf static QemuOptsList qcow2_runtime_opts = { 29074c4510aSKevin Wolf .name = "qcow2", 29174c4510aSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), 29274c4510aSKevin Wolf .desc = { 29374c4510aSKevin Wolf { 29464aa99d3SKevin Wolf .name = QCOW2_OPT_LAZY_REFCOUNTS, 29574c4510aSKevin Wolf .type = QEMU_OPT_BOOL, 29674c4510aSKevin Wolf .help = "Postpone refcount updates", 29774c4510aSKevin Wolf }, 29867af674eSKevin Wolf { 29967af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_REQUEST, 30067af674eSKevin Wolf .type = QEMU_OPT_BOOL, 30167af674eSKevin Wolf .help = "Pass guest discard requests to the layer below", 30267af674eSKevin Wolf }, 30367af674eSKevin Wolf { 30467af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_SNAPSHOT, 30567af674eSKevin Wolf .type = QEMU_OPT_BOOL, 30667af674eSKevin Wolf .help = "Generate discard requests when snapshot related space " 30767af674eSKevin Wolf "is freed", 30867af674eSKevin Wolf }, 30967af674eSKevin Wolf { 31067af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_OTHER, 31167af674eSKevin Wolf .type = QEMU_OPT_BOOL, 31267af674eSKevin Wolf .help = "Generate discard requests when other clusters are freed", 31367af674eSKevin Wolf }, 31474c4510aSKevin Wolf { /* end of list */ } 31574c4510aSKevin Wolf }, 31674c4510aSKevin Wolf }; 31774c4510aSKevin Wolf 3181a86938fSKevin Wolf static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) 319585f8587Sbellard { 320585f8587Sbellard BDRVQcowState *s = bs->opaque; 3216d85a57eSJes Sorensen int len, i, ret = 0; 322585f8587Sbellard QCowHeader header; 32374c4510aSKevin Wolf QemuOpts *opts; 32474c4510aSKevin Wolf Error *local_err = NULL; 3259b80ddf3Saliguori uint64_t ext_end; 3262cf7cfa1SKevin Wolf uint64_t l1_vm_state_index; 327585f8587Sbellard 3286d85a57eSJes Sorensen ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); 3296d85a57eSJes Sorensen if (ret < 0) { 330585f8587Sbellard goto fail; 3316d85a57eSJes Sorensen } 332585f8587Sbellard be32_to_cpus(&header.magic); 333585f8587Sbellard be32_to_cpus(&header.version); 334585f8587Sbellard be64_to_cpus(&header.backing_file_offset); 335585f8587Sbellard be32_to_cpus(&header.backing_file_size); 336585f8587Sbellard be64_to_cpus(&header.size); 337585f8587Sbellard be32_to_cpus(&header.cluster_bits); 338585f8587Sbellard be32_to_cpus(&header.crypt_method); 339585f8587Sbellard be64_to_cpus(&header.l1_table_offset); 340585f8587Sbellard be32_to_cpus(&header.l1_size); 341585f8587Sbellard be64_to_cpus(&header.refcount_table_offset); 342585f8587Sbellard be32_to_cpus(&header.refcount_table_clusters); 343585f8587Sbellard be64_to_cpus(&header.snapshots_offset); 344585f8587Sbellard be32_to_cpus(&header.nb_snapshots); 345585f8587Sbellard 346e8cdcec1SKevin Wolf if (header.magic != QCOW_MAGIC) { 34715bac0d5SStefan Weil ret = -EMEDIUMTYPE; 348585f8587Sbellard goto fail; 3496d85a57eSJes Sorensen } 3506744cbabSKevin Wolf if (header.version < 2 || header.version > 3) { 3516744cbabSKevin Wolf report_unsupported(bs, "QCOW version %d", header.version); 352e8cdcec1SKevin Wolf ret = -ENOTSUP; 353e8cdcec1SKevin Wolf goto fail; 354e8cdcec1SKevin Wolf } 3556744cbabSKevin Wolf 3566744cbabSKevin Wolf s->qcow_version = header.version; 3576744cbabSKevin Wolf 3586744cbabSKevin Wolf /* Initialise version 3 header fields */ 3596744cbabSKevin Wolf if (header.version == 2) { 3606744cbabSKevin Wolf header.incompatible_features = 0; 3616744cbabSKevin Wolf header.compatible_features = 0; 3626744cbabSKevin Wolf header.autoclear_features = 0; 3636744cbabSKevin Wolf header.refcount_order = 4; 3646744cbabSKevin Wolf header.header_length = 72; 3656744cbabSKevin Wolf } else { 3666744cbabSKevin Wolf be64_to_cpus(&header.incompatible_features); 3676744cbabSKevin Wolf be64_to_cpus(&header.compatible_features); 3686744cbabSKevin Wolf be64_to_cpus(&header.autoclear_features); 3696744cbabSKevin Wolf be32_to_cpus(&header.refcount_order); 3706744cbabSKevin Wolf be32_to_cpus(&header.header_length); 3716744cbabSKevin Wolf } 3726744cbabSKevin Wolf 3736744cbabSKevin Wolf if (header.header_length > sizeof(header)) { 3746744cbabSKevin Wolf s->unknown_header_fields_size = header.header_length - sizeof(header); 3756744cbabSKevin Wolf s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); 3766744cbabSKevin Wolf ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, 3776744cbabSKevin Wolf s->unknown_header_fields_size); 3786744cbabSKevin Wolf if (ret < 0) { 3796744cbabSKevin Wolf goto fail; 3806744cbabSKevin Wolf } 3816744cbabSKevin Wolf } 3826744cbabSKevin Wolf 383cfcc4c62SKevin Wolf if (header.backing_file_offset) { 384cfcc4c62SKevin Wolf ext_end = header.backing_file_offset; 385cfcc4c62SKevin Wolf } else { 386cfcc4c62SKevin Wolf ext_end = 1 << header.cluster_bits; 387cfcc4c62SKevin Wolf } 388cfcc4c62SKevin Wolf 3896744cbabSKevin Wolf /* Handle feature bits */ 3906744cbabSKevin Wolf s->incompatible_features = header.incompatible_features; 3916744cbabSKevin Wolf s->compatible_features = header.compatible_features; 3926744cbabSKevin Wolf s->autoclear_features = header.autoclear_features; 3936744cbabSKevin Wolf 394c61d0004SStefan Hajnoczi if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { 395cfcc4c62SKevin Wolf void *feature_table = NULL; 396cfcc4c62SKevin Wolf qcow2_read_extensions(bs, header.header_length, ext_end, 397cfcc4c62SKevin Wolf &feature_table); 398cfcc4c62SKevin Wolf report_unsupported_feature(bs, feature_table, 399c61d0004SStefan Hajnoczi s->incompatible_features & 400c61d0004SStefan Hajnoczi ~QCOW2_INCOMPAT_MASK); 4016744cbabSKevin Wolf ret = -ENOTSUP; 4026744cbabSKevin Wolf goto fail; 4036744cbabSKevin Wolf } 4046744cbabSKevin Wolf 4056744cbabSKevin Wolf /* Check support for various header values */ 4066744cbabSKevin Wolf if (header.refcount_order != 4) { 4076744cbabSKevin Wolf report_unsupported(bs, "%d bit reference counts", 4086744cbabSKevin Wolf 1 << header.refcount_order); 4096744cbabSKevin Wolf ret = -ENOTSUP; 4106744cbabSKevin Wolf goto fail; 4116744cbabSKevin Wolf } 4126744cbabSKevin Wolf 413d191d12dSStefan Weil if (header.cluster_bits < MIN_CLUSTER_BITS || 4146d85a57eSJes Sorensen header.cluster_bits > MAX_CLUSTER_BITS) { 4156d85a57eSJes Sorensen ret = -EINVAL; 416585f8587Sbellard goto fail; 4176d85a57eSJes Sorensen } 4186d85a57eSJes Sorensen if (header.crypt_method > QCOW_CRYPT_AES) { 4196d85a57eSJes Sorensen ret = -EINVAL; 420585f8587Sbellard goto fail; 4216d85a57eSJes Sorensen } 422585f8587Sbellard s->crypt_method_header = header.crypt_method; 4236d85a57eSJes Sorensen if (s->crypt_method_header) { 424585f8587Sbellard bs->encrypted = 1; 4256d85a57eSJes Sorensen } 426585f8587Sbellard s->cluster_bits = header.cluster_bits; 427585f8587Sbellard s->cluster_size = 1 << s->cluster_bits; 428585f8587Sbellard s->cluster_sectors = 1 << (s->cluster_bits - 9); 429585f8587Sbellard s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ 430585f8587Sbellard s->l2_size = 1 << s->l2_bits; 431585f8587Sbellard bs->total_sectors = header.size / 512; 432585f8587Sbellard s->csize_shift = (62 - (s->cluster_bits - 8)); 433585f8587Sbellard s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; 434585f8587Sbellard s->cluster_offset_mask = (1LL << s->csize_shift) - 1; 435585f8587Sbellard s->refcount_table_offset = header.refcount_table_offset; 436585f8587Sbellard s->refcount_table_size = 437585f8587Sbellard header.refcount_table_clusters << (s->cluster_bits - 3); 438585f8587Sbellard 439585f8587Sbellard s->snapshots_offset = header.snapshots_offset; 440585f8587Sbellard s->nb_snapshots = header.nb_snapshots; 441585f8587Sbellard 442585f8587Sbellard /* read the level 1 table */ 443585f8587Sbellard s->l1_size = header.l1_size; 4442cf7cfa1SKevin Wolf 4452cf7cfa1SKevin Wolf l1_vm_state_index = size_to_l1(s, header.size); 4462cf7cfa1SKevin Wolf if (l1_vm_state_index > INT_MAX) { 4472cf7cfa1SKevin Wolf ret = -EFBIG; 4482cf7cfa1SKevin Wolf goto fail; 4492cf7cfa1SKevin Wolf } 4502cf7cfa1SKevin Wolf s->l1_vm_state_index = l1_vm_state_index; 4512cf7cfa1SKevin Wolf 452585f8587Sbellard /* the L1 table must contain at least enough entries to put 453585f8587Sbellard header.size bytes */ 4546d85a57eSJes Sorensen if (s->l1_size < s->l1_vm_state_index) { 4556d85a57eSJes Sorensen ret = -EINVAL; 456585f8587Sbellard goto fail; 4576d85a57eSJes Sorensen } 458585f8587Sbellard s->l1_table_offset = header.l1_table_offset; 459d191d12dSStefan Weil if (s->l1_size > 0) { 4607267c094SAnthony Liguori s->l1_table = g_malloc0( 4613f6a3ee5SKevin Wolf align_offset(s->l1_size * sizeof(uint64_t), 512)); 4626d85a57eSJes Sorensen ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, 4636d85a57eSJes Sorensen s->l1_size * sizeof(uint64_t)); 4646d85a57eSJes Sorensen if (ret < 0) { 465585f8587Sbellard goto fail; 4666d85a57eSJes Sorensen } 467585f8587Sbellard for(i = 0;i < s->l1_size; i++) { 468585f8587Sbellard be64_to_cpus(&s->l1_table[i]); 469585f8587Sbellard } 470d191d12dSStefan Weil } 47129c1a730SKevin Wolf 47229c1a730SKevin Wolf /* alloc L2 table/refcount block cache */ 4736af4e9eaSPaolo Bonzini s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE); 4746af4e9eaSPaolo Bonzini s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE); 47529c1a730SKevin Wolf 4767267c094SAnthony Liguori s->cluster_cache = g_malloc(s->cluster_size); 477585f8587Sbellard /* one more sector for decompressed data alignment */ 478dea43a65SFrediano Ziglio s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size 479095a9c58Saliguori + 512); 480585f8587Sbellard s->cluster_cache_offset = -1; 48106d9260fSAnthony Liguori s->flags = flags; 482585f8587Sbellard 4836d85a57eSJes Sorensen ret = qcow2_refcount_init(bs); 4846d85a57eSJes Sorensen if (ret != 0) { 485585f8587Sbellard goto fail; 4866d85a57eSJes Sorensen } 487585f8587Sbellard 48872cf2d4fSBlue Swirl QLIST_INIT(&s->cluster_allocs); 4890b919faeSKevin Wolf QTAILQ_INIT(&s->discards); 490f214978aSKevin Wolf 4919b80ddf3Saliguori /* read qcow2 extensions */ 492cfcc4c62SKevin Wolf if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL)) { 4936d85a57eSJes Sorensen ret = -EINVAL; 4949b80ddf3Saliguori goto fail; 4956d85a57eSJes Sorensen } 4969b80ddf3Saliguori 497585f8587Sbellard /* read the backing file name */ 498585f8587Sbellard if (header.backing_file_offset != 0) { 499585f8587Sbellard len = header.backing_file_size; 5006d85a57eSJes Sorensen if (len > 1023) { 501585f8587Sbellard len = 1023; 5026d85a57eSJes Sorensen } 5036d85a57eSJes Sorensen ret = bdrv_pread(bs->file, header.backing_file_offset, 5046d85a57eSJes Sorensen bs->backing_file, len); 5056d85a57eSJes Sorensen if (ret < 0) { 506585f8587Sbellard goto fail; 5076d85a57eSJes Sorensen } 508585f8587Sbellard bs->backing_file[len] = '\0'; 509585f8587Sbellard } 51042deb29fSKevin Wolf 51142deb29fSKevin Wolf ret = qcow2_read_snapshots(bs); 51242deb29fSKevin Wolf if (ret < 0) { 513585f8587Sbellard goto fail; 5146d85a57eSJes Sorensen } 515585f8587Sbellard 516af7b708dSStefan Hajnoczi /* Clear unknown autoclear feature bits */ 517af7b708dSStefan Hajnoczi if (!bs->read_only && s->autoclear_features != 0) { 518af7b708dSStefan Hajnoczi s->autoclear_features = 0; 519af7b708dSStefan Hajnoczi ret = qcow2_update_header(bs); 520af7b708dSStefan Hajnoczi if (ret < 0) { 521af7b708dSStefan Hajnoczi goto fail; 522af7b708dSStefan Hajnoczi } 523af7b708dSStefan Hajnoczi } 524af7b708dSStefan Hajnoczi 52568d100e9SKevin Wolf /* Initialise locks */ 52668d100e9SKevin Wolf qemu_co_mutex_init(&s->lock); 52768d100e9SKevin Wolf 528c61d0004SStefan Hajnoczi /* Repair image if dirty */ 529058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && !bs->read_only && 530058f8f16SStefan Hajnoczi (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { 531c61d0004SStefan Hajnoczi BdrvCheckResult result = {0}; 532c61d0004SStefan Hajnoczi 533acbe5982SStefan Hajnoczi ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS); 534c61d0004SStefan Hajnoczi if (ret < 0) { 535c61d0004SStefan Hajnoczi goto fail; 536c61d0004SStefan Hajnoczi } 537c61d0004SStefan Hajnoczi } 538c61d0004SStefan Hajnoczi 53974c4510aSKevin Wolf /* Enable lazy_refcounts according to image and command line options */ 54074c4510aSKevin Wolf opts = qemu_opts_create_nofail(&qcow2_runtime_opts); 54174c4510aSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 54274c4510aSKevin Wolf if (error_is_set(&local_err)) { 54374c4510aSKevin Wolf qerror_report_err(local_err); 54474c4510aSKevin Wolf error_free(local_err); 54574c4510aSKevin Wolf ret = -EINVAL; 54674c4510aSKevin Wolf goto fail; 54774c4510aSKevin Wolf } 54874c4510aSKevin Wolf 549acdfb480SKevin Wolf s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, 55074c4510aSKevin Wolf (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); 55174c4510aSKevin Wolf 55267af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_NEVER] = false; 55367af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; 55467af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_REQUEST] = 55567af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, 55667af674eSKevin Wolf flags & BDRV_O_UNMAP); 55767af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = 55867af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); 55967af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_OTHER] = 56067af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); 56167af674eSKevin Wolf 56274c4510aSKevin Wolf qemu_opts_del(opts); 56374c4510aSKevin Wolf 56474c4510aSKevin Wolf if (s->use_lazy_refcounts && s->qcow_version < 3) { 56574c4510aSKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, "Lazy refcounts require " 56674c4510aSKevin Wolf "a qcow2 image with at least qemu 1.1 compatibility level"); 56774c4510aSKevin Wolf ret = -EINVAL; 56874c4510aSKevin Wolf goto fail; 56974c4510aSKevin Wolf } 57074c4510aSKevin Wolf 571585f8587Sbellard #ifdef DEBUG_ALLOC 5726cbc3031SPhilipp Hahn { 5736cbc3031SPhilipp Hahn BdrvCheckResult result = {0}; 574b35278f7SStefan Hajnoczi qcow2_check_refcounts(bs, &result, 0); 5756cbc3031SPhilipp Hahn } 576585f8587Sbellard #endif 5776d85a57eSJes Sorensen return ret; 578585f8587Sbellard 579585f8587Sbellard fail: 5806744cbabSKevin Wolf g_free(s->unknown_header_fields); 58175bab85cSKevin Wolf cleanup_unknown_header_ext(bs); 582ed6ccf0fSKevin Wolf qcow2_free_snapshots(bs); 583ed6ccf0fSKevin Wolf qcow2_refcount_close(bs); 5847267c094SAnthony Liguori g_free(s->l1_table); 58529c1a730SKevin Wolf if (s->l2_table_cache) { 58629c1a730SKevin Wolf qcow2_cache_destroy(bs, s->l2_table_cache); 58729c1a730SKevin Wolf } 5887267c094SAnthony Liguori g_free(s->cluster_cache); 589dea43a65SFrediano Ziglio qemu_vfree(s->cluster_data); 5906d85a57eSJes Sorensen return ret; 591585f8587Sbellard } 592585f8587Sbellard 5937c80ab3fSJes Sorensen static int qcow2_set_key(BlockDriverState *bs, const char *key) 594585f8587Sbellard { 595585f8587Sbellard BDRVQcowState *s = bs->opaque; 596585f8587Sbellard uint8_t keybuf[16]; 597585f8587Sbellard int len, i; 598585f8587Sbellard 599585f8587Sbellard memset(keybuf, 0, 16); 600585f8587Sbellard len = strlen(key); 601585f8587Sbellard if (len > 16) 602585f8587Sbellard len = 16; 603585f8587Sbellard /* XXX: we could compress the chars to 7 bits to increase 604585f8587Sbellard entropy */ 605585f8587Sbellard for(i = 0;i < len;i++) { 606585f8587Sbellard keybuf[i] = key[i]; 607585f8587Sbellard } 608585f8587Sbellard s->crypt_method = s->crypt_method_header; 609585f8587Sbellard 610585f8587Sbellard if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) 611585f8587Sbellard return -1; 612585f8587Sbellard if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) 613585f8587Sbellard return -1; 614585f8587Sbellard #if 0 615585f8587Sbellard /* test */ 616585f8587Sbellard { 617585f8587Sbellard uint8_t in[16]; 618585f8587Sbellard uint8_t out[16]; 619585f8587Sbellard uint8_t tmp[16]; 620585f8587Sbellard for(i=0;i<16;i++) 621585f8587Sbellard in[i] = i; 622585f8587Sbellard AES_encrypt(in, tmp, &s->aes_encrypt_key); 623585f8587Sbellard AES_decrypt(tmp, out, &s->aes_decrypt_key); 624585f8587Sbellard for(i = 0; i < 16; i++) 625585f8587Sbellard printf(" %02x", tmp[i]); 626585f8587Sbellard printf("\n"); 627585f8587Sbellard for(i = 0; i < 16; i++) 628585f8587Sbellard printf(" %02x", out[i]); 629585f8587Sbellard printf("\n"); 630585f8587Sbellard } 631585f8587Sbellard #endif 632585f8587Sbellard return 0; 633585f8587Sbellard } 634585f8587Sbellard 63521d82ac9SJeff Cody /* We have nothing to do for QCOW2 reopen, stubs just return 63621d82ac9SJeff Cody * success */ 63721d82ac9SJeff Cody static int qcow2_reopen_prepare(BDRVReopenState *state, 63821d82ac9SJeff Cody BlockReopenQueue *queue, Error **errp) 63921d82ac9SJeff Cody { 64021d82ac9SJeff Cody return 0; 64121d82ac9SJeff Cody } 64221d82ac9SJeff Cody 643f8a2e5e3SStefan Hajnoczi static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs, 644f8a2e5e3SStefan Hajnoczi int64_t sector_num, int nb_sectors, int *pnum) 645585f8587Sbellard { 646f8a2e5e3SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 647585f8587Sbellard uint64_t cluster_offset; 6481c46efaaSKevin Wolf int ret; 649585f8587Sbellard 650095a9c58Saliguori *pnum = nb_sectors; 651f8a2e5e3SStefan Hajnoczi /* FIXME We can get errors here, but the bdrv_co_is_allocated interface 652f8a2e5e3SStefan Hajnoczi * can't pass them on today */ 653f8a2e5e3SStefan Hajnoczi qemu_co_mutex_lock(&s->lock); 6541c46efaaSKevin Wolf ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); 655f8a2e5e3SStefan Hajnoczi qemu_co_mutex_unlock(&s->lock); 6561c46efaaSKevin Wolf if (ret < 0) { 6571c46efaaSKevin Wolf *pnum = 0; 6581c46efaaSKevin Wolf } 659095a9c58Saliguori 660381b487dSPaolo Bonzini return (cluster_offset != 0) || (ret == QCOW2_CLUSTER_ZERO); 661585f8587Sbellard } 662585f8587Sbellard 663a9465922Sbellard /* handle reading after the end of the backing file */ 664bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, 665bd28f835SKevin Wolf int64_t sector_num, int nb_sectors) 666a9465922Sbellard { 667a9465922Sbellard int n1; 668a9465922Sbellard if ((sector_num + nb_sectors) <= bs->total_sectors) 669a9465922Sbellard return nb_sectors; 670a9465922Sbellard if (sector_num >= bs->total_sectors) 671a9465922Sbellard n1 = 0; 672a9465922Sbellard else 673a9465922Sbellard n1 = bs->total_sectors - sector_num; 674bd28f835SKevin Wolf 6753d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1)); 676bd28f835SKevin Wolf 677a9465922Sbellard return n1; 678a9465922Sbellard } 679a9465922Sbellard 680a968168cSDong Xu Wang static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, 6813fc48d09SFrediano Ziglio int remaining_sectors, QEMUIOVector *qiov) 6821490791fSaliguori { 683585f8587Sbellard BDRVQcowState *s = bs->opaque; 684a9465922Sbellard int index_in_cluster, n1; 68568d100e9SKevin Wolf int ret; 686faf575c1SFrediano Ziglio int cur_nr_sectors; /* number of sectors in current iteration */ 687c2bdd990SFrediano Ziglio uint64_t cluster_offset = 0; 6883fc48d09SFrediano Ziglio uint64_t bytes_done = 0; 6893fc48d09SFrediano Ziglio QEMUIOVector hd_qiov; 6903fc48d09SFrediano Ziglio uint8_t *cluster_data = NULL; 691585f8587Sbellard 6923fc48d09SFrediano Ziglio qemu_iovec_init(&hd_qiov, qiov->niov); 6933fc48d09SFrediano Ziglio 6943fc48d09SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 6953fc48d09SFrediano Ziglio 6963fc48d09SFrediano Ziglio while (remaining_sectors != 0) { 697585f8587Sbellard 698faf575c1SFrediano Ziglio /* prepare next request */ 6993fc48d09SFrediano Ziglio cur_nr_sectors = remaining_sectors; 700bd28f835SKevin Wolf if (s->crypt_method) { 701faf575c1SFrediano Ziglio cur_nr_sectors = MIN(cur_nr_sectors, 702bd28f835SKevin Wolf QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 703bd28f835SKevin Wolf } 704bd28f835SKevin Wolf 7053fc48d09SFrediano Ziglio ret = qcow2_get_cluster_offset(bs, sector_num << 9, 706c2bdd990SFrediano Ziglio &cur_nr_sectors, &cluster_offset); 7071c46efaaSKevin Wolf if (ret < 0) { 7083fc48d09SFrediano Ziglio goto fail; 7091c46efaaSKevin Wolf } 7101c46efaaSKevin Wolf 7113fc48d09SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 712585f8587Sbellard 7133fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 7141b093c48SMichael Tokarev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 715faf575c1SFrediano Ziglio cur_nr_sectors * 512); 716bd28f835SKevin Wolf 71768d000a3SKevin Wolf switch (ret) { 71868d000a3SKevin Wolf case QCOW2_CLUSTER_UNALLOCATED: 719bd28f835SKevin Wolf 720585f8587Sbellard if (bs->backing_hd) { 721585f8587Sbellard /* read from the base image */ 7223fc48d09SFrediano Ziglio n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov, 7233fc48d09SFrediano Ziglio sector_num, cur_nr_sectors); 724a9465922Sbellard if (n1 > 0) { 72566f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); 72668d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 7273fc48d09SFrediano Ziglio ret = bdrv_co_readv(bs->backing_hd, sector_num, 7283fc48d09SFrediano Ziglio n1, &hd_qiov); 72968d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 73068d100e9SKevin Wolf if (ret < 0) { 7313fc48d09SFrediano Ziglio goto fail; 7323ab4c7e9SKevin Wolf } 7331490791fSaliguori } 734a9465922Sbellard } else { 735585f8587Sbellard /* Note: in this case, no need to wait */ 7363d9b4925SMichael Tokarev qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 7371490791fSaliguori } 73868d000a3SKevin Wolf break; 73968d000a3SKevin Wolf 7406377af48SKevin Wolf case QCOW2_CLUSTER_ZERO: 7413d9b4925SMichael Tokarev qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 7426377af48SKevin Wolf break; 7436377af48SKevin Wolf 74468d000a3SKevin Wolf case QCOW2_CLUSTER_COMPRESSED: 745585f8587Sbellard /* add AIO support for compressed blocks ? */ 746c2bdd990SFrediano Ziglio ret = qcow2_decompress_cluster(bs, cluster_offset); 7478af36488SKevin Wolf if (ret < 0) { 7483fc48d09SFrediano Ziglio goto fail; 7498af36488SKevin Wolf } 750bd28f835SKevin Wolf 75103396148SMichael Tokarev qemu_iovec_from_buf(&hd_qiov, 0, 752bd28f835SKevin Wolf s->cluster_cache + index_in_cluster * 512, 753faf575c1SFrediano Ziglio 512 * cur_nr_sectors); 75468d000a3SKevin Wolf break; 75568d000a3SKevin Wolf 75668d000a3SKevin Wolf case QCOW2_CLUSTER_NORMAL: 757c2bdd990SFrediano Ziglio if ((cluster_offset & 511) != 0) { 7583fc48d09SFrediano Ziglio ret = -EIO; 7593fc48d09SFrediano Ziglio goto fail; 760585f8587Sbellard } 761c87c0672Saliguori 762bd28f835SKevin Wolf if (s->crypt_method) { 763bd28f835SKevin Wolf /* 764bd28f835SKevin Wolf * For encrypted images, read everything into a temporary 765bd28f835SKevin Wolf * contiguous buffer on which the AES functions can work. 766bd28f835SKevin Wolf */ 7673fc48d09SFrediano Ziglio if (!cluster_data) { 7683fc48d09SFrediano Ziglio cluster_data = 769dea43a65SFrediano Ziglio qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 770bd28f835SKevin Wolf } 771bd28f835SKevin Wolf 772faf575c1SFrediano Ziglio assert(cur_nr_sectors <= 773bd28f835SKevin Wolf QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 7743fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 7753fc48d09SFrediano Ziglio qemu_iovec_add(&hd_qiov, cluster_data, 776faf575c1SFrediano Ziglio 512 * cur_nr_sectors); 777bd28f835SKevin Wolf } 778bd28f835SKevin Wolf 77966f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 78068d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 78168d100e9SKevin Wolf ret = bdrv_co_readv(bs->file, 782c2bdd990SFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 7833fc48d09SFrediano Ziglio cur_nr_sectors, &hd_qiov); 78468d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 78568d100e9SKevin Wolf if (ret < 0) { 7863fc48d09SFrediano Ziglio goto fail; 787585f8587Sbellard } 788faf575c1SFrediano Ziglio if (s->crypt_method) { 7893fc48d09SFrediano Ziglio qcow2_encrypt_sectors(s, sector_num, cluster_data, 7903fc48d09SFrediano Ziglio cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key); 79103396148SMichael Tokarev qemu_iovec_from_buf(qiov, bytes_done, 79203396148SMichael Tokarev cluster_data, 512 * cur_nr_sectors); 793171e3d6bSKevin Wolf } 79468d000a3SKevin Wolf break; 79568d000a3SKevin Wolf 79668d000a3SKevin Wolf default: 79768d000a3SKevin Wolf g_assert_not_reached(); 79868d000a3SKevin Wolf ret = -EIO; 79968d000a3SKevin Wolf goto fail; 800faf575c1SFrediano Ziglio } 801faf575c1SFrediano Ziglio 8023fc48d09SFrediano Ziglio remaining_sectors -= cur_nr_sectors; 8033fc48d09SFrediano Ziglio sector_num += cur_nr_sectors; 8043fc48d09SFrediano Ziglio bytes_done += cur_nr_sectors * 512; 8055ebaa27eSFrediano Ziglio } 8063fc48d09SFrediano Ziglio ret = 0; 807f141eafeSaliguori 8083fc48d09SFrediano Ziglio fail: 80968d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 81068d100e9SKevin Wolf 8113fc48d09SFrediano Ziglio qemu_iovec_destroy(&hd_qiov); 812dea43a65SFrediano Ziglio qemu_vfree(cluster_data); 81368d100e9SKevin Wolf 81468d100e9SKevin Wolf return ret; 815585f8587Sbellard } 816585f8587Sbellard 817a968168cSDong Xu Wang static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, 8183fc48d09SFrediano Ziglio int64_t sector_num, 8193fc48d09SFrediano Ziglio int remaining_sectors, 8203fc48d09SFrediano Ziglio QEMUIOVector *qiov) 821585f8587Sbellard { 822585f8587Sbellard BDRVQcowState *s = bs->opaque; 823585f8587Sbellard int index_in_cluster; 824095a9c58Saliguori int n_end; 82568d100e9SKevin Wolf int ret; 826faf575c1SFrediano Ziglio int cur_nr_sectors; /* number of sectors in current iteration */ 827c2bdd990SFrediano Ziglio uint64_t cluster_offset; 8283fc48d09SFrediano Ziglio QEMUIOVector hd_qiov; 8293fc48d09SFrediano Ziglio uint64_t bytes_done = 0; 8303fc48d09SFrediano Ziglio uint8_t *cluster_data = NULL; 8318d2497c3SKevin Wolf QCowL2Meta *l2meta = NULL; 832c2271403SFrediano Ziglio 8333cce16f4SKevin Wolf trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num, 8343cce16f4SKevin Wolf remaining_sectors); 8353cce16f4SKevin Wolf 8363fc48d09SFrediano Ziglio qemu_iovec_init(&hd_qiov, qiov->niov); 837585f8587Sbellard 8383fc48d09SFrediano Ziglio s->cluster_cache_offset = -1; /* disable compressed cache */ 8393fc48d09SFrediano Ziglio 8403fc48d09SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 8413fc48d09SFrediano Ziglio 8423fc48d09SFrediano Ziglio while (remaining_sectors != 0) { 8433fc48d09SFrediano Ziglio 844f50f88b9SKevin Wolf l2meta = NULL; 845cf5c1a23SKevin Wolf 8463cce16f4SKevin Wolf trace_qcow2_writev_start_part(qemu_coroutine_self()); 8473fc48d09SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 8483fc48d09SFrediano Ziglio n_end = index_in_cluster + remaining_sectors; 849095a9c58Saliguori if (s->crypt_method && 8505ebaa27eSFrediano Ziglio n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) { 851095a9c58Saliguori n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; 8525ebaa27eSFrediano Ziglio } 853095a9c58Saliguori 8543fc48d09SFrediano Ziglio ret = qcow2_alloc_cluster_offset(bs, sector_num << 9, 855f50f88b9SKevin Wolf index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta); 856148da7eaSKevin Wolf if (ret < 0) { 8573fc48d09SFrediano Ziglio goto fail; 858148da7eaSKevin Wolf } 859148da7eaSKevin Wolf 860c2bdd990SFrediano Ziglio assert((cluster_offset & 511) == 0); 861148da7eaSKevin Wolf 8623fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 8631b093c48SMichael Tokarev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 864faf575c1SFrediano Ziglio cur_nr_sectors * 512); 8656f5f060bSKevin Wolf 866585f8587Sbellard if (s->crypt_method) { 8673fc48d09SFrediano Ziglio if (!cluster_data) { 868dea43a65SFrediano Ziglio cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * 869095a9c58Saliguori s->cluster_size); 870585f8587Sbellard } 8716f5f060bSKevin Wolf 8723fc48d09SFrediano Ziglio assert(hd_qiov.size <= 8735ebaa27eSFrediano Ziglio QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 874d5e6b161SMichael Tokarev qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size); 8756f5f060bSKevin Wolf 8763fc48d09SFrediano Ziglio qcow2_encrypt_sectors(s, sector_num, cluster_data, 8773fc48d09SFrediano Ziglio cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key); 8786f5f060bSKevin Wolf 8793fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 8803fc48d09SFrediano Ziglio qemu_iovec_add(&hd_qiov, cluster_data, 881faf575c1SFrediano Ziglio cur_nr_sectors * 512); 882585f8587Sbellard } 8836f5f060bSKevin Wolf 88468d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 88567a7a0ebSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); 8863cce16f4SKevin Wolf trace_qcow2_writev_data(qemu_coroutine_self(), 8873cce16f4SKevin Wolf (cluster_offset >> 9) + index_in_cluster); 88868d100e9SKevin Wolf ret = bdrv_co_writev(bs->file, 889c2bdd990SFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 8903fc48d09SFrediano Ziglio cur_nr_sectors, &hd_qiov); 89168d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 89268d100e9SKevin Wolf if (ret < 0) { 8933fc48d09SFrediano Ziglio goto fail; 894171e3d6bSKevin Wolf } 895f141eafeSaliguori 89688c6588cSKevin Wolf while (l2meta != NULL) { 89788c6588cSKevin Wolf QCowL2Meta *next; 89888c6588cSKevin Wolf 899cf5c1a23SKevin Wolf ret = qcow2_alloc_cluster_link_l2(bs, l2meta); 900faf575c1SFrediano Ziglio if (ret < 0) { 9013fc48d09SFrediano Ziglio goto fail; 902faf575c1SFrediano Ziglio } 903faf575c1SFrediano Ziglio 9044e95314eSKevin Wolf /* Take the request off the list of running requests */ 9054e95314eSKevin Wolf if (l2meta->nb_clusters != 0) { 9064e95314eSKevin Wolf QLIST_REMOVE(l2meta, next_in_flight); 9074e95314eSKevin Wolf } 9084e95314eSKevin Wolf 9094e95314eSKevin Wolf qemu_co_queue_restart_all(&l2meta->dependent_requests); 9104e95314eSKevin Wolf 91188c6588cSKevin Wolf next = l2meta->next; 912cf5c1a23SKevin Wolf g_free(l2meta); 91388c6588cSKevin Wolf l2meta = next; 914f50f88b9SKevin Wolf } 9150fa9131aSKevin Wolf 9163fc48d09SFrediano Ziglio remaining_sectors -= cur_nr_sectors; 9173fc48d09SFrediano Ziglio sector_num += cur_nr_sectors; 9183fc48d09SFrediano Ziglio bytes_done += cur_nr_sectors * 512; 9193cce16f4SKevin Wolf trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors); 9205ebaa27eSFrediano Ziglio } 9213fc48d09SFrediano Ziglio ret = 0; 922faf575c1SFrediano Ziglio 9233fc48d09SFrediano Ziglio fail: 9244e95314eSKevin Wolf qemu_co_mutex_unlock(&s->lock); 9254e95314eSKevin Wolf 92688c6588cSKevin Wolf while (l2meta != NULL) { 92788c6588cSKevin Wolf QCowL2Meta *next; 92888c6588cSKevin Wolf 9294e95314eSKevin Wolf if (l2meta->nb_clusters != 0) { 9304e95314eSKevin Wolf QLIST_REMOVE(l2meta, next_in_flight); 9314e95314eSKevin Wolf } 9324e95314eSKevin Wolf qemu_co_queue_restart_all(&l2meta->dependent_requests); 93388c6588cSKevin Wolf 93488c6588cSKevin Wolf next = l2meta->next; 935cf5c1a23SKevin Wolf g_free(l2meta); 93688c6588cSKevin Wolf l2meta = next; 937cf5c1a23SKevin Wolf } 9380fa9131aSKevin Wolf 9393fc48d09SFrediano Ziglio qemu_iovec_destroy(&hd_qiov); 940dea43a65SFrediano Ziglio qemu_vfree(cluster_data); 9413cce16f4SKevin Wolf trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); 94242496d62SKevin Wolf 94368d100e9SKevin Wolf return ret; 944585f8587Sbellard } 945585f8587Sbellard 9467c80ab3fSJes Sorensen static void qcow2_close(BlockDriverState *bs) 947585f8587Sbellard { 948585f8587Sbellard BDRVQcowState *s = bs->opaque; 9497267c094SAnthony Liguori g_free(s->l1_table); 95029c1a730SKevin Wolf 95129c1a730SKevin Wolf qcow2_cache_flush(bs, s->l2_table_cache); 95229c1a730SKevin Wolf qcow2_cache_flush(bs, s->refcount_block_cache); 95329c1a730SKevin Wolf 954c61d0004SStefan Hajnoczi qcow2_mark_clean(bs); 955c61d0004SStefan Hajnoczi 95629c1a730SKevin Wolf qcow2_cache_destroy(bs, s->l2_table_cache); 95729c1a730SKevin Wolf qcow2_cache_destroy(bs, s->refcount_block_cache); 95829c1a730SKevin Wolf 9596744cbabSKevin Wolf g_free(s->unknown_header_fields); 96075bab85cSKevin Wolf cleanup_unknown_header_ext(bs); 9616744cbabSKevin Wolf 9627267c094SAnthony Liguori g_free(s->cluster_cache); 963dea43a65SFrediano Ziglio qemu_vfree(s->cluster_data); 964ed6ccf0fSKevin Wolf qcow2_refcount_close(bs); 96528c1202bSLi Zhi Hui qcow2_free_snapshots(bs); 966585f8587Sbellard } 967585f8587Sbellard 96806d9260fSAnthony Liguori static void qcow2_invalidate_cache(BlockDriverState *bs) 96906d9260fSAnthony Liguori { 97006d9260fSAnthony Liguori BDRVQcowState *s = bs->opaque; 97106d9260fSAnthony Liguori int flags = s->flags; 97206d9260fSAnthony Liguori AES_KEY aes_encrypt_key; 97306d9260fSAnthony Liguori AES_KEY aes_decrypt_key; 97406d9260fSAnthony Liguori uint32_t crypt_method = 0; 975acdfb480SKevin Wolf QDict *options; 97606d9260fSAnthony Liguori 97706d9260fSAnthony Liguori /* 97806d9260fSAnthony Liguori * Backing files are read-only which makes all of their metadata immutable, 97906d9260fSAnthony Liguori * that means we don't have to worry about reopening them here. 98006d9260fSAnthony Liguori */ 98106d9260fSAnthony Liguori 98206d9260fSAnthony Liguori if (s->crypt_method) { 98306d9260fSAnthony Liguori crypt_method = s->crypt_method; 98406d9260fSAnthony Liguori memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key)); 98506d9260fSAnthony Liguori memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key)); 98606d9260fSAnthony Liguori } 98706d9260fSAnthony Liguori 98806d9260fSAnthony Liguori qcow2_close(bs); 98906d9260fSAnthony Liguori 990acdfb480SKevin Wolf options = qdict_new(); 991acdfb480SKevin Wolf qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS, 992acdfb480SKevin Wolf qbool_from_int(s->use_lazy_refcounts)); 993acdfb480SKevin Wolf 99406d9260fSAnthony Liguori memset(s, 0, sizeof(BDRVQcowState)); 995acdfb480SKevin Wolf qcow2_open(bs, options, flags); 996acdfb480SKevin Wolf 997acdfb480SKevin Wolf QDECREF(options); 99806d9260fSAnthony Liguori 99906d9260fSAnthony Liguori if (crypt_method) { 100006d9260fSAnthony Liguori s->crypt_method = crypt_method; 100106d9260fSAnthony Liguori memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key)); 100206d9260fSAnthony Liguori memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key)); 100306d9260fSAnthony Liguori } 100406d9260fSAnthony Liguori } 100506d9260fSAnthony Liguori 1006e24e49e6SKevin Wolf static size_t header_ext_add(char *buf, uint32_t magic, const void *s, 1007e24e49e6SKevin Wolf size_t len, size_t buflen) 1008756e6736SKevin Wolf { 1009e24e49e6SKevin Wolf QCowExtension *ext_backing_fmt = (QCowExtension*) buf; 1010e24e49e6SKevin Wolf size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); 1011756e6736SKevin Wolf 1012e24e49e6SKevin Wolf if (buflen < ext_len) { 1013756e6736SKevin Wolf return -ENOSPC; 1014756e6736SKevin Wolf } 1015756e6736SKevin Wolf 1016e24e49e6SKevin Wolf *ext_backing_fmt = (QCowExtension) { 1017e24e49e6SKevin Wolf .magic = cpu_to_be32(magic), 1018e24e49e6SKevin Wolf .len = cpu_to_be32(len), 1019e24e49e6SKevin Wolf }; 1020e24e49e6SKevin Wolf memcpy(buf + sizeof(QCowExtension), s, len); 1021756e6736SKevin Wolf 1022e24e49e6SKevin Wolf return ext_len; 1023756e6736SKevin Wolf } 1024756e6736SKevin Wolf 1025e24e49e6SKevin Wolf /* 1026e24e49e6SKevin Wolf * Updates the qcow2 header, including the variable length parts of it, i.e. 1027e24e49e6SKevin Wolf * the backing file name and all extensions. qcow2 was not designed to allow 1028e24e49e6SKevin Wolf * such changes, so if we run out of space (we can only use the first cluster) 1029e24e49e6SKevin Wolf * this function may fail. 1030e24e49e6SKevin Wolf * 1031e24e49e6SKevin Wolf * Returns 0 on success, -errno in error cases. 1032e24e49e6SKevin Wolf */ 1033e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs) 1034e24e49e6SKevin Wolf { 1035e24e49e6SKevin Wolf BDRVQcowState *s = bs->opaque; 1036e24e49e6SKevin Wolf QCowHeader *header; 1037e24e49e6SKevin Wolf char *buf; 1038e24e49e6SKevin Wolf size_t buflen = s->cluster_size; 1039e24e49e6SKevin Wolf int ret; 1040e24e49e6SKevin Wolf uint64_t total_size; 1041e24e49e6SKevin Wolf uint32_t refcount_table_clusters; 10426744cbabSKevin Wolf size_t header_length; 104375bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext; 1044e24e49e6SKevin Wolf 1045e24e49e6SKevin Wolf buf = qemu_blockalign(bs, buflen); 1046e24e49e6SKevin Wolf 1047e24e49e6SKevin Wolf /* Header structure */ 1048e24e49e6SKevin Wolf header = (QCowHeader*) buf; 1049e24e49e6SKevin Wolf 1050e24e49e6SKevin Wolf if (buflen < sizeof(*header)) { 1051e24e49e6SKevin Wolf ret = -ENOSPC; 1052e24e49e6SKevin Wolf goto fail; 1053756e6736SKevin Wolf } 1054756e6736SKevin Wolf 10556744cbabSKevin Wolf header_length = sizeof(*header) + s->unknown_header_fields_size; 1056e24e49e6SKevin Wolf total_size = bs->total_sectors * BDRV_SECTOR_SIZE; 1057e24e49e6SKevin Wolf refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); 1058e24e49e6SKevin Wolf 1059e24e49e6SKevin Wolf *header = (QCowHeader) { 10606744cbabSKevin Wolf /* Version 2 fields */ 1061e24e49e6SKevin Wolf .magic = cpu_to_be32(QCOW_MAGIC), 10626744cbabSKevin Wolf .version = cpu_to_be32(s->qcow_version), 1063e24e49e6SKevin Wolf .backing_file_offset = 0, 1064e24e49e6SKevin Wolf .backing_file_size = 0, 1065e24e49e6SKevin Wolf .cluster_bits = cpu_to_be32(s->cluster_bits), 1066e24e49e6SKevin Wolf .size = cpu_to_be64(total_size), 1067e24e49e6SKevin Wolf .crypt_method = cpu_to_be32(s->crypt_method_header), 1068e24e49e6SKevin Wolf .l1_size = cpu_to_be32(s->l1_size), 1069e24e49e6SKevin Wolf .l1_table_offset = cpu_to_be64(s->l1_table_offset), 1070e24e49e6SKevin Wolf .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), 1071e24e49e6SKevin Wolf .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), 1072e24e49e6SKevin Wolf .nb_snapshots = cpu_to_be32(s->nb_snapshots), 1073e24e49e6SKevin Wolf .snapshots_offset = cpu_to_be64(s->snapshots_offset), 10746744cbabSKevin Wolf 10756744cbabSKevin Wolf /* Version 3 fields */ 10766744cbabSKevin Wolf .incompatible_features = cpu_to_be64(s->incompatible_features), 10776744cbabSKevin Wolf .compatible_features = cpu_to_be64(s->compatible_features), 10786744cbabSKevin Wolf .autoclear_features = cpu_to_be64(s->autoclear_features), 10796744cbabSKevin Wolf .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT), 10806744cbabSKevin Wolf .header_length = cpu_to_be32(header_length), 1081e24e49e6SKevin Wolf }; 1082e24e49e6SKevin Wolf 10836744cbabSKevin Wolf /* For older versions, write a shorter header */ 10846744cbabSKevin Wolf switch (s->qcow_version) { 10856744cbabSKevin Wolf case 2: 10866744cbabSKevin Wolf ret = offsetof(QCowHeader, incompatible_features); 10876744cbabSKevin Wolf break; 10886744cbabSKevin Wolf case 3: 10896744cbabSKevin Wolf ret = sizeof(*header); 10906744cbabSKevin Wolf break; 10916744cbabSKevin Wolf default: 1092b6c14762SJim Meyering ret = -EINVAL; 1093b6c14762SJim Meyering goto fail; 10946744cbabSKevin Wolf } 10956744cbabSKevin Wolf 10966744cbabSKevin Wolf buf += ret; 10976744cbabSKevin Wolf buflen -= ret; 10986744cbabSKevin Wolf memset(buf, 0, buflen); 10996744cbabSKevin Wolf 11006744cbabSKevin Wolf /* Preserve any unknown field in the header */ 11016744cbabSKevin Wolf if (s->unknown_header_fields_size) { 11026744cbabSKevin Wolf if (buflen < s->unknown_header_fields_size) { 11036744cbabSKevin Wolf ret = -ENOSPC; 11046744cbabSKevin Wolf goto fail; 11056744cbabSKevin Wolf } 11066744cbabSKevin Wolf 11076744cbabSKevin Wolf memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); 11086744cbabSKevin Wolf buf += s->unknown_header_fields_size; 11096744cbabSKevin Wolf buflen -= s->unknown_header_fields_size; 11106744cbabSKevin Wolf } 1111e24e49e6SKevin Wolf 1112e24e49e6SKevin Wolf /* Backing file format header extension */ 1113e24e49e6SKevin Wolf if (*bs->backing_format) { 1114e24e49e6SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, 1115e24e49e6SKevin Wolf bs->backing_format, strlen(bs->backing_format), 1116e24e49e6SKevin Wolf buflen); 1117756e6736SKevin Wolf if (ret < 0) { 1118756e6736SKevin Wolf goto fail; 1119756e6736SKevin Wolf } 1120756e6736SKevin Wolf 1121e24e49e6SKevin Wolf buf += ret; 1122e24e49e6SKevin Wolf buflen -= ret; 1123e24e49e6SKevin Wolf } 1124756e6736SKevin Wolf 1125cfcc4c62SKevin Wolf /* Feature table */ 1126cfcc4c62SKevin Wolf Qcow2Feature features[] = { 1127c61d0004SStefan Hajnoczi { 1128c61d0004SStefan Hajnoczi .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, 1129c61d0004SStefan Hajnoczi .bit = QCOW2_INCOMPAT_DIRTY_BITNR, 1130c61d0004SStefan Hajnoczi .name = "dirty bit", 1131c61d0004SStefan Hajnoczi }, 1132bfe8043eSStefan Hajnoczi { 1133bfe8043eSStefan Hajnoczi .type = QCOW2_FEAT_TYPE_COMPATIBLE, 1134bfe8043eSStefan Hajnoczi .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, 1135bfe8043eSStefan Hajnoczi .name = "lazy refcounts", 1136bfe8043eSStefan Hajnoczi }, 1137cfcc4c62SKevin Wolf }; 1138cfcc4c62SKevin Wolf 1139cfcc4c62SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, 1140cfcc4c62SKevin Wolf features, sizeof(features), buflen); 1141cfcc4c62SKevin Wolf if (ret < 0) { 1142cfcc4c62SKevin Wolf goto fail; 1143cfcc4c62SKevin Wolf } 1144cfcc4c62SKevin Wolf buf += ret; 1145cfcc4c62SKevin Wolf buflen -= ret; 1146cfcc4c62SKevin Wolf 114775bab85cSKevin Wolf /* Keep unknown header extensions */ 114875bab85cSKevin Wolf QLIST_FOREACH(uext, &s->unknown_header_ext, next) { 114975bab85cSKevin Wolf ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); 115075bab85cSKevin Wolf if (ret < 0) { 115175bab85cSKevin Wolf goto fail; 115275bab85cSKevin Wolf } 115375bab85cSKevin Wolf 115475bab85cSKevin Wolf buf += ret; 115575bab85cSKevin Wolf buflen -= ret; 115675bab85cSKevin Wolf } 115775bab85cSKevin Wolf 1158e24e49e6SKevin Wolf /* End of header extensions */ 1159e24e49e6SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); 1160756e6736SKevin Wolf if (ret < 0) { 1161756e6736SKevin Wolf goto fail; 1162756e6736SKevin Wolf } 1163756e6736SKevin Wolf 1164e24e49e6SKevin Wolf buf += ret; 1165e24e49e6SKevin Wolf buflen -= ret; 1166e24e49e6SKevin Wolf 1167e24e49e6SKevin Wolf /* Backing file name */ 1168e24e49e6SKevin Wolf if (*bs->backing_file) { 1169e24e49e6SKevin Wolf size_t backing_file_len = strlen(bs->backing_file); 1170e24e49e6SKevin Wolf 1171e24e49e6SKevin Wolf if (buflen < backing_file_len) { 1172e24e49e6SKevin Wolf ret = -ENOSPC; 1173e24e49e6SKevin Wolf goto fail; 1174e24e49e6SKevin Wolf } 1175e24e49e6SKevin Wolf 117600ea1881SJim Meyering /* Using strncpy is ok here, since buf is not NUL-terminated. */ 1177e24e49e6SKevin Wolf strncpy(buf, bs->backing_file, buflen); 1178e24e49e6SKevin Wolf 1179e24e49e6SKevin Wolf header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); 1180e24e49e6SKevin Wolf header->backing_file_size = cpu_to_be32(backing_file_len); 1181e24e49e6SKevin Wolf } 1182e24e49e6SKevin Wolf 1183e24e49e6SKevin Wolf /* Write the new header */ 1184e24e49e6SKevin Wolf ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); 1185756e6736SKevin Wolf if (ret < 0) { 1186756e6736SKevin Wolf goto fail; 1187756e6736SKevin Wolf } 1188756e6736SKevin Wolf 1189756e6736SKevin Wolf ret = 0; 1190756e6736SKevin Wolf fail: 1191e24e49e6SKevin Wolf qemu_vfree(header); 1192756e6736SKevin Wolf return ret; 1193756e6736SKevin Wolf } 1194756e6736SKevin Wolf 1195756e6736SKevin Wolf static int qcow2_change_backing_file(BlockDriverState *bs, 1196756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1197756e6736SKevin Wolf { 1198e24e49e6SKevin Wolf pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1199e24e49e6SKevin Wolf pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1200e24e49e6SKevin Wolf 1201e24e49e6SKevin Wolf return qcow2_update_header(bs); 1202756e6736SKevin Wolf } 1203756e6736SKevin Wolf 1204a35e1c17SKevin Wolf static int preallocate(BlockDriverState *bs) 1205a35e1c17SKevin Wolf { 1206a35e1c17SKevin Wolf uint64_t nb_sectors; 1207a35e1c17SKevin Wolf uint64_t offset; 1208060bee89SKevin Wolf uint64_t host_offset = 0; 1209a35e1c17SKevin Wolf int num; 1210148da7eaSKevin Wolf int ret; 1211f50f88b9SKevin Wolf QCowL2Meta *meta; 1212a35e1c17SKevin Wolf 1213a35e1c17SKevin Wolf nb_sectors = bdrv_getlength(bs) >> 9; 1214a35e1c17SKevin Wolf offset = 0; 1215a35e1c17SKevin Wolf 1216a35e1c17SKevin Wolf while (nb_sectors) { 1217a35e1c17SKevin Wolf num = MIN(nb_sectors, INT_MAX >> 9); 1218060bee89SKevin Wolf ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, 1219060bee89SKevin Wolf &host_offset, &meta); 1220148da7eaSKevin Wolf if (ret < 0) { 122119dbcbf7SKevin Wolf return ret; 1222a35e1c17SKevin Wolf } 1223a35e1c17SKevin Wolf 1224f50f88b9SKevin Wolf ret = qcow2_alloc_cluster_link_l2(bs, meta); 122519dbcbf7SKevin Wolf if (ret < 0) { 12266cfcb9b8SKevin Wolf qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, 12276cfcb9b8SKevin Wolf QCOW2_DISCARD_NEVER); 122819dbcbf7SKevin Wolf return ret; 1229a35e1c17SKevin Wolf } 1230a35e1c17SKevin Wolf 1231f214978aSKevin Wolf /* There are no dependent requests, but we need to remove our request 1232f214978aSKevin Wolf * from the list of in-flight requests */ 1233f50f88b9SKevin Wolf if (meta != NULL) { 12344e95314eSKevin Wolf QLIST_REMOVE(meta, next_in_flight); 1235f50f88b9SKevin Wolf } 1236f214978aSKevin Wolf 1237a35e1c17SKevin Wolf /* TODO Preallocate data if requested */ 1238a35e1c17SKevin Wolf 1239a35e1c17SKevin Wolf nb_sectors -= num; 1240a35e1c17SKevin Wolf offset += num << 9; 1241a35e1c17SKevin Wolf } 1242a35e1c17SKevin Wolf 1243a35e1c17SKevin Wolf /* 1244a35e1c17SKevin Wolf * It is expected that the image file is large enough to actually contain 1245a35e1c17SKevin Wolf * all of the allocated clusters (otherwise we get failing reads after 1246a35e1c17SKevin Wolf * EOF). Extend the image to the last allocated sector. 1247a35e1c17SKevin Wolf */ 1248060bee89SKevin Wolf if (host_offset != 0) { 1249ea80b906SKevin Wolf uint8_t buf[512]; 1250ea80b906SKevin Wolf memset(buf, 0, 512); 1251060bee89SKevin Wolf ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1); 125219dbcbf7SKevin Wolf if (ret < 0) { 125319dbcbf7SKevin Wolf return ret; 125419dbcbf7SKevin Wolf } 1255a35e1c17SKevin Wolf } 1256a35e1c17SKevin Wolf 1257a35e1c17SKevin Wolf return 0; 1258a35e1c17SKevin Wolf } 1259a35e1c17SKevin Wolf 12607c80ab3fSJes Sorensen static int qcow2_create2(const char *filename, int64_t total_size, 1261a9420734SKevin Wolf const char *backing_file, const char *backing_format, 1262a9420734SKevin Wolf int flags, size_t cluster_size, int prealloc, 12636744cbabSKevin Wolf QEMUOptionParameter *options, int version) 1264a9420734SKevin Wolf { 12659b2260cbSDong Xu Wang /* Calculate cluster_bits */ 1266a9420734SKevin Wolf int cluster_bits; 1267a9420734SKevin Wolf cluster_bits = ffs(cluster_size) - 1; 1268a9420734SKevin Wolf if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || 1269a9420734SKevin Wolf (1 << cluster_bits) != cluster_size) 1270a9420734SKevin Wolf { 1271a9420734SKevin Wolf error_report( 12726daf194dSMarkus Armbruster "Cluster size must be a power of two between %d and %dk", 1273a9420734SKevin Wolf 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); 1274a9420734SKevin Wolf return -EINVAL; 1275a9420734SKevin Wolf } 1276a9420734SKevin Wolf 1277a9420734SKevin Wolf /* 1278a9420734SKevin Wolf * Open the image file and write a minimal qcow2 header. 1279a9420734SKevin Wolf * 1280a9420734SKevin Wolf * We keep things simple and start with a zero-sized image. We also 1281a9420734SKevin Wolf * do without refcount blocks or a L1 table for now. We'll fix the 1282a9420734SKevin Wolf * inconsistency later. 1283a9420734SKevin Wolf * 1284a9420734SKevin Wolf * We do need a refcount table because growing the refcount table means 1285a9420734SKevin Wolf * allocating two new refcount blocks - the seconds of which would be at 1286a9420734SKevin Wolf * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file 1287a9420734SKevin Wolf * size for any qcow2 image. 1288a9420734SKevin Wolf */ 1289a9420734SKevin Wolf BlockDriverState* bs; 1290a9420734SKevin Wolf QCowHeader header; 1291a9420734SKevin Wolf uint8_t* refcount_table; 1292a9420734SKevin Wolf int ret; 1293a9420734SKevin Wolf 1294a9420734SKevin Wolf ret = bdrv_create_file(filename, options); 1295a9420734SKevin Wolf if (ret < 0) { 1296a9420734SKevin Wolf return ret; 1297a9420734SKevin Wolf } 1298a9420734SKevin Wolf 1299787e4a85SKevin Wolf ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR); 1300a9420734SKevin Wolf if (ret < 0) { 1301a9420734SKevin Wolf return ret; 1302a9420734SKevin Wolf } 1303a9420734SKevin Wolf 1304a9420734SKevin Wolf /* Write the header */ 1305a9420734SKevin Wolf memset(&header, 0, sizeof(header)); 1306a9420734SKevin Wolf header.magic = cpu_to_be32(QCOW_MAGIC); 13076744cbabSKevin Wolf header.version = cpu_to_be32(version); 1308a9420734SKevin Wolf header.cluster_bits = cpu_to_be32(cluster_bits); 1309a9420734SKevin Wolf header.size = cpu_to_be64(0); 1310a9420734SKevin Wolf header.l1_table_offset = cpu_to_be64(0); 1311a9420734SKevin Wolf header.l1_size = cpu_to_be32(0); 1312a9420734SKevin Wolf header.refcount_table_offset = cpu_to_be64(cluster_size); 1313a9420734SKevin Wolf header.refcount_table_clusters = cpu_to_be32(1); 13146744cbabSKevin Wolf header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT); 13156744cbabSKevin Wolf header.header_length = cpu_to_be32(sizeof(header)); 1316a9420734SKevin Wolf 1317a9420734SKevin Wolf if (flags & BLOCK_FLAG_ENCRYPT) { 1318a9420734SKevin Wolf header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); 1319a9420734SKevin Wolf } else { 1320a9420734SKevin Wolf header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); 1321a9420734SKevin Wolf } 1322a9420734SKevin Wolf 1323bfe8043eSStefan Hajnoczi if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { 1324bfe8043eSStefan Hajnoczi header.compatible_features |= 1325bfe8043eSStefan Hajnoczi cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); 1326bfe8043eSStefan Hajnoczi } 1327bfe8043eSStefan Hajnoczi 1328a9420734SKevin Wolf ret = bdrv_pwrite(bs, 0, &header, sizeof(header)); 1329a9420734SKevin Wolf if (ret < 0) { 1330a9420734SKevin Wolf goto out; 1331a9420734SKevin Wolf } 1332a9420734SKevin Wolf 1333a9420734SKevin Wolf /* Write an empty refcount table */ 13347267c094SAnthony Liguori refcount_table = g_malloc0(cluster_size); 1335a9420734SKevin Wolf ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size); 13367267c094SAnthony Liguori g_free(refcount_table); 1337a9420734SKevin Wolf 1338a9420734SKevin Wolf if (ret < 0) { 1339a9420734SKevin Wolf goto out; 1340a9420734SKevin Wolf } 1341a9420734SKevin Wolf 1342a9420734SKevin Wolf bdrv_close(bs); 1343a9420734SKevin Wolf 1344a9420734SKevin Wolf /* 1345a9420734SKevin Wolf * And now open the image and make it consistent first (i.e. increase the 1346a9420734SKevin Wolf * refcount of the cluster that is occupied by the header and the refcount 1347a9420734SKevin Wolf * table) 1348a9420734SKevin Wolf */ 1349a9420734SKevin Wolf BlockDriver* drv = bdrv_find_format("qcow2"); 1350a9420734SKevin Wolf assert(drv != NULL); 1351de9c0cecSKevin Wolf ret = bdrv_open(bs, filename, NULL, 1352e1a7107fSKevin Wolf BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv); 1353a9420734SKevin Wolf if (ret < 0) { 1354a9420734SKevin Wolf goto out; 1355a9420734SKevin Wolf } 1356a9420734SKevin Wolf 1357a9420734SKevin Wolf ret = qcow2_alloc_clusters(bs, 2 * cluster_size); 1358a9420734SKevin Wolf if (ret < 0) { 1359a9420734SKevin Wolf goto out; 1360a9420734SKevin Wolf 1361a9420734SKevin Wolf } else if (ret != 0) { 1362a9420734SKevin Wolf error_report("Huh, first cluster in empty image is already in use?"); 1363a9420734SKevin Wolf abort(); 1364a9420734SKevin Wolf } 1365a9420734SKevin Wolf 1366a9420734SKevin Wolf /* Okay, now that we have a valid image, let's give it the right size */ 1367a9420734SKevin Wolf ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE); 1368a9420734SKevin Wolf if (ret < 0) { 1369a9420734SKevin Wolf goto out; 1370a9420734SKevin Wolf } 1371a9420734SKevin Wolf 1372a9420734SKevin Wolf /* Want a backing file? There you go.*/ 1373a9420734SKevin Wolf if (backing_file) { 1374a9420734SKevin Wolf ret = bdrv_change_backing_file(bs, backing_file, backing_format); 1375a9420734SKevin Wolf if (ret < 0) { 1376a9420734SKevin Wolf goto out; 1377a9420734SKevin Wolf } 1378a9420734SKevin Wolf } 1379a9420734SKevin Wolf 1380a9420734SKevin Wolf /* And if we're supposed to preallocate metadata, do that now */ 1381a9420734SKevin Wolf if (prealloc) { 138215552c4aSZhi Yong Wu BDRVQcowState *s = bs->opaque; 138315552c4aSZhi Yong Wu qemu_co_mutex_lock(&s->lock); 1384a9420734SKevin Wolf ret = preallocate(bs); 138515552c4aSZhi Yong Wu qemu_co_mutex_unlock(&s->lock); 1386a9420734SKevin Wolf if (ret < 0) { 1387a9420734SKevin Wolf goto out; 1388a9420734SKevin Wolf } 1389a9420734SKevin Wolf } 1390a9420734SKevin Wolf 1391a9420734SKevin Wolf ret = 0; 1392a9420734SKevin Wolf out: 1393a9420734SKevin Wolf bdrv_delete(bs); 1394a9420734SKevin Wolf return ret; 1395a9420734SKevin Wolf } 1396de5f3f40SKevin Wolf 13977c80ab3fSJes Sorensen static int qcow2_create(const char *filename, QEMUOptionParameter *options) 1398de5f3f40SKevin Wolf { 1399de5f3f40SKevin Wolf const char *backing_file = NULL; 1400de5f3f40SKevin Wolf const char *backing_fmt = NULL; 1401de5f3f40SKevin Wolf uint64_t sectors = 0; 1402de5f3f40SKevin Wolf int flags = 0; 140399cce9faSKevin Wolf size_t cluster_size = DEFAULT_CLUSTER_SIZE; 1404de5f3f40SKevin Wolf int prealloc = 0; 14058ad1898cSKevin Wolf int version = 3; 1406de5f3f40SKevin Wolf 1407de5f3f40SKevin Wolf /* Read out options */ 1408de5f3f40SKevin Wolf while (options && options->name) { 1409de5f3f40SKevin Wolf if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 1410de5f3f40SKevin Wolf sectors = options->value.n / 512; 1411de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 1412de5f3f40SKevin Wolf backing_file = options->value.s; 1413de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 1414de5f3f40SKevin Wolf backing_fmt = options->value.s; 1415de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { 1416de5f3f40SKevin Wolf flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; 1417de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 1418de5f3f40SKevin Wolf if (options->value.n) { 1419de5f3f40SKevin Wolf cluster_size = options->value.n; 1420de5f3f40SKevin Wolf } 1421de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { 1422de5f3f40SKevin Wolf if (!options->value.s || !strcmp(options->value.s, "off")) { 1423de5f3f40SKevin Wolf prealloc = 0; 1424de5f3f40SKevin Wolf } else if (!strcmp(options->value.s, "metadata")) { 1425de5f3f40SKevin Wolf prealloc = 1; 1426de5f3f40SKevin Wolf } else { 1427de5f3f40SKevin Wolf fprintf(stderr, "Invalid preallocation mode: '%s'\n", 1428de5f3f40SKevin Wolf options->value.s); 1429de5f3f40SKevin Wolf return -EINVAL; 1430de5f3f40SKevin Wolf } 14316744cbabSKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) { 14326744cbabSKevin Wolf if (!options->value.s || !strcmp(options->value.s, "0.10")) { 14336744cbabSKevin Wolf version = 2; 14346744cbabSKevin Wolf } else if (!strcmp(options->value.s, "1.1")) { 14356744cbabSKevin Wolf version = 3; 14366744cbabSKevin Wolf } else { 14376744cbabSKevin Wolf fprintf(stderr, "Invalid compatibility level: '%s'\n", 14386744cbabSKevin Wolf options->value.s); 14396744cbabSKevin Wolf return -EINVAL; 14406744cbabSKevin Wolf } 1441bfe8043eSStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) { 1442bfe8043eSStefan Hajnoczi flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0; 1443de5f3f40SKevin Wolf } 1444de5f3f40SKevin Wolf options++; 1445de5f3f40SKevin Wolf } 1446de5f3f40SKevin Wolf 1447de5f3f40SKevin Wolf if (backing_file && prealloc) { 1448de5f3f40SKevin Wolf fprintf(stderr, "Backing file and preallocation cannot be used at " 1449de5f3f40SKevin Wolf "the same time\n"); 1450de5f3f40SKevin Wolf return -EINVAL; 1451de5f3f40SKevin Wolf } 1452de5f3f40SKevin Wolf 1453bfe8043eSStefan Hajnoczi if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) { 1454bfe8043eSStefan Hajnoczi fprintf(stderr, "Lazy refcounts only supported with compatibility " 1455bfe8043eSStefan Hajnoczi "level 1.1 and above (use compat=1.1 or greater)\n"); 1456bfe8043eSStefan Hajnoczi return -EINVAL; 1457bfe8043eSStefan Hajnoczi } 1458bfe8043eSStefan Hajnoczi 14597c80ab3fSJes Sorensen return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags, 14606744cbabSKevin Wolf cluster_size, prealloc, options, version); 1461de5f3f40SKevin Wolf } 1462de5f3f40SKevin Wolf 14637c80ab3fSJes Sorensen static int qcow2_make_empty(BlockDriverState *bs) 146420d97356SBlue Swirl { 146520d97356SBlue Swirl #if 0 146620d97356SBlue Swirl /* XXX: not correct */ 146720d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 146820d97356SBlue Swirl uint32_t l1_length = s->l1_size * sizeof(uint64_t); 146920d97356SBlue Swirl int ret; 147020d97356SBlue Swirl 147120d97356SBlue Swirl memset(s->l1_table, 0, l1_length); 147266f82ceeSKevin Wolf if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) 147320d97356SBlue Swirl return -1; 147466f82ceeSKevin Wolf ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length); 147520d97356SBlue Swirl if (ret < 0) 147620d97356SBlue Swirl return ret; 147720d97356SBlue Swirl 147820d97356SBlue Swirl l2_cache_reset(bs); 147920d97356SBlue Swirl #endif 148020d97356SBlue Swirl return 0; 148120d97356SBlue Swirl } 148220d97356SBlue Swirl 1483621f0589SKevin Wolf static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, 1484621f0589SKevin Wolf int64_t sector_num, int nb_sectors) 1485621f0589SKevin Wolf { 1486621f0589SKevin Wolf int ret; 1487621f0589SKevin Wolf BDRVQcowState *s = bs->opaque; 1488621f0589SKevin Wolf 1489621f0589SKevin Wolf /* Emulate misaligned zero writes */ 1490621f0589SKevin Wolf if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) { 1491621f0589SKevin Wolf return -ENOTSUP; 1492621f0589SKevin Wolf } 1493621f0589SKevin Wolf 1494621f0589SKevin Wolf /* Whatever is left can use real zero clusters */ 1495621f0589SKevin Wolf qemu_co_mutex_lock(&s->lock); 1496621f0589SKevin Wolf ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, 1497621f0589SKevin Wolf nb_sectors); 1498621f0589SKevin Wolf qemu_co_mutex_unlock(&s->lock); 1499621f0589SKevin Wolf 1500621f0589SKevin Wolf return ret; 1501621f0589SKevin Wolf } 1502621f0589SKevin Wolf 15036db39ae2SPaolo Bonzini static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, 15046db39ae2SPaolo Bonzini int64_t sector_num, int nb_sectors) 15055ea929e3SKevin Wolf { 15066db39ae2SPaolo Bonzini int ret; 15076db39ae2SPaolo Bonzini BDRVQcowState *s = bs->opaque; 15086db39ae2SPaolo Bonzini 15096db39ae2SPaolo Bonzini qemu_co_mutex_lock(&s->lock); 15106db39ae2SPaolo Bonzini ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS, 15115ea929e3SKevin Wolf nb_sectors); 15126db39ae2SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 15136db39ae2SPaolo Bonzini return ret; 15145ea929e3SKevin Wolf } 15155ea929e3SKevin Wolf 1516419b19d9SStefan Hajnoczi static int qcow2_truncate(BlockDriverState *bs, int64_t offset) 1517419b19d9SStefan Hajnoczi { 1518419b19d9SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 15192cf7cfa1SKevin Wolf int64_t new_l1_size; 15202cf7cfa1SKevin Wolf int ret; 1521419b19d9SStefan Hajnoczi 1522419b19d9SStefan Hajnoczi if (offset & 511) { 1523259b2173SKevin Wolf error_report("The new size must be a multiple of 512"); 1524419b19d9SStefan Hajnoczi return -EINVAL; 1525419b19d9SStefan Hajnoczi } 1526419b19d9SStefan Hajnoczi 1527419b19d9SStefan Hajnoczi /* cannot proceed if image has snapshots */ 1528419b19d9SStefan Hajnoczi if (s->nb_snapshots) { 1529259b2173SKevin Wolf error_report("Can't resize an image which has snapshots"); 1530419b19d9SStefan Hajnoczi return -ENOTSUP; 1531419b19d9SStefan Hajnoczi } 1532419b19d9SStefan Hajnoczi 1533419b19d9SStefan Hajnoczi /* shrinking is currently not supported */ 1534419b19d9SStefan Hajnoczi if (offset < bs->total_sectors * 512) { 1535259b2173SKevin Wolf error_report("qcow2 doesn't support shrinking images yet"); 1536419b19d9SStefan Hajnoczi return -ENOTSUP; 1537419b19d9SStefan Hajnoczi } 1538419b19d9SStefan Hajnoczi 1539419b19d9SStefan Hajnoczi new_l1_size = size_to_l1(s, offset); 154072893756SStefan Hajnoczi ret = qcow2_grow_l1_table(bs, new_l1_size, true); 1541419b19d9SStefan Hajnoczi if (ret < 0) { 1542419b19d9SStefan Hajnoczi return ret; 1543419b19d9SStefan Hajnoczi } 1544419b19d9SStefan Hajnoczi 1545419b19d9SStefan Hajnoczi /* write updated header.size */ 1546419b19d9SStefan Hajnoczi offset = cpu_to_be64(offset); 15478b3b7206SKevin Wolf ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), 1548419b19d9SStefan Hajnoczi &offset, sizeof(uint64_t)); 1549419b19d9SStefan Hajnoczi if (ret < 0) { 1550419b19d9SStefan Hajnoczi return ret; 1551419b19d9SStefan Hajnoczi } 1552419b19d9SStefan Hajnoczi 1553419b19d9SStefan Hajnoczi s->l1_vm_state_index = new_l1_size; 1554419b19d9SStefan Hajnoczi return 0; 1555419b19d9SStefan Hajnoczi } 1556419b19d9SStefan Hajnoczi 155720d97356SBlue Swirl /* XXX: put compressed sectors first, then all the cluster aligned 155820d97356SBlue Swirl tables to avoid losing bytes in alignment */ 15597c80ab3fSJes Sorensen static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, 156020d97356SBlue Swirl const uint8_t *buf, int nb_sectors) 156120d97356SBlue Swirl { 156220d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 156320d97356SBlue Swirl z_stream strm; 156420d97356SBlue Swirl int ret, out_len; 156520d97356SBlue Swirl uint8_t *out_buf; 156620d97356SBlue Swirl uint64_t cluster_offset; 156720d97356SBlue Swirl 156820d97356SBlue Swirl if (nb_sectors == 0) { 156920d97356SBlue Swirl /* align end of file to a sector boundary to ease reading with 157020d97356SBlue Swirl sector based I/Os */ 157166f82ceeSKevin Wolf cluster_offset = bdrv_getlength(bs->file); 157220d97356SBlue Swirl cluster_offset = (cluster_offset + 511) & ~511; 157366f82ceeSKevin Wolf bdrv_truncate(bs->file, cluster_offset); 157420d97356SBlue Swirl return 0; 157520d97356SBlue Swirl } 157620d97356SBlue Swirl 1577f4d38befSStefan Hajnoczi if (nb_sectors != s->cluster_sectors) { 1578f4d38befSStefan Hajnoczi ret = -EINVAL; 1579f4d38befSStefan Hajnoczi 1580f4d38befSStefan Hajnoczi /* Zero-pad last write if image size is not cluster aligned */ 1581f4d38befSStefan Hajnoczi if (sector_num + nb_sectors == bs->total_sectors && 1582f4d38befSStefan Hajnoczi nb_sectors < s->cluster_sectors) { 1583f4d38befSStefan Hajnoczi uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); 1584f4d38befSStefan Hajnoczi memset(pad_buf, 0, s->cluster_size); 1585f4d38befSStefan Hajnoczi memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); 1586f4d38befSStefan Hajnoczi ret = qcow2_write_compressed(bs, sector_num, 1587f4d38befSStefan Hajnoczi pad_buf, s->cluster_sectors); 1588f4d38befSStefan Hajnoczi qemu_vfree(pad_buf); 1589f4d38befSStefan Hajnoczi } 1590f4d38befSStefan Hajnoczi return ret; 1591f4d38befSStefan Hajnoczi } 159220d97356SBlue Swirl 15937267c094SAnthony Liguori out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); 159420d97356SBlue Swirl 159520d97356SBlue Swirl /* best compression, small window, no zlib header */ 159620d97356SBlue Swirl memset(&strm, 0, sizeof(strm)); 159720d97356SBlue Swirl ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, 159820d97356SBlue Swirl Z_DEFLATED, -12, 159920d97356SBlue Swirl 9, Z_DEFAULT_STRATEGY); 160020d97356SBlue Swirl if (ret != 0) { 16018f1efd00SKevin Wolf ret = -EINVAL; 16028f1efd00SKevin Wolf goto fail; 160320d97356SBlue Swirl } 160420d97356SBlue Swirl 160520d97356SBlue Swirl strm.avail_in = s->cluster_size; 160620d97356SBlue Swirl strm.next_in = (uint8_t *)buf; 160720d97356SBlue Swirl strm.avail_out = s->cluster_size; 160820d97356SBlue Swirl strm.next_out = out_buf; 160920d97356SBlue Swirl 161020d97356SBlue Swirl ret = deflate(&strm, Z_FINISH); 161120d97356SBlue Swirl if (ret != Z_STREAM_END && ret != Z_OK) { 161220d97356SBlue Swirl deflateEnd(&strm); 16138f1efd00SKevin Wolf ret = -EINVAL; 16148f1efd00SKevin Wolf goto fail; 161520d97356SBlue Swirl } 161620d97356SBlue Swirl out_len = strm.next_out - out_buf; 161720d97356SBlue Swirl 161820d97356SBlue Swirl deflateEnd(&strm); 161920d97356SBlue Swirl 162020d97356SBlue Swirl if (ret != Z_STREAM_END || out_len >= s->cluster_size) { 162120d97356SBlue Swirl /* could not compress: write normal cluster */ 16228f1efd00SKevin Wolf ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors); 16238f1efd00SKevin Wolf if (ret < 0) { 16248f1efd00SKevin Wolf goto fail; 16258f1efd00SKevin Wolf } 162620d97356SBlue Swirl } else { 162720d97356SBlue Swirl cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, 162820d97356SBlue Swirl sector_num << 9, out_len); 16298f1efd00SKevin Wolf if (!cluster_offset) { 16308f1efd00SKevin Wolf ret = -EIO; 16318f1efd00SKevin Wolf goto fail; 16328f1efd00SKevin Wolf } 163320d97356SBlue Swirl cluster_offset &= s->cluster_offset_mask; 163466f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); 16358f1efd00SKevin Wolf ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len); 16368f1efd00SKevin Wolf if (ret < 0) { 16378f1efd00SKevin Wolf goto fail; 163820d97356SBlue Swirl } 163920d97356SBlue Swirl } 164020d97356SBlue Swirl 16418f1efd00SKevin Wolf ret = 0; 16428f1efd00SKevin Wolf fail: 16437267c094SAnthony Liguori g_free(out_buf); 16448f1efd00SKevin Wolf return ret; 164520d97356SBlue Swirl } 164620d97356SBlue Swirl 1647a968168cSDong Xu Wang static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) 164820d97356SBlue Swirl { 164929c1a730SKevin Wolf BDRVQcowState *s = bs->opaque; 165029c1a730SKevin Wolf int ret; 165129c1a730SKevin Wolf 16528b94ff85SPaolo Bonzini qemu_co_mutex_lock(&s->lock); 165329c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->l2_table_cache); 165429c1a730SKevin Wolf if (ret < 0) { 1655c95de7e2SDong Xu Wang qemu_co_mutex_unlock(&s->lock); 16568b94ff85SPaolo Bonzini return ret; 165729c1a730SKevin Wolf } 165829c1a730SKevin Wolf 1659bfe8043eSStefan Hajnoczi if (qcow2_need_accurate_refcounts(s)) { 166029c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->refcount_block_cache); 166129c1a730SKevin Wolf if (ret < 0) { 1662c95de7e2SDong Xu Wang qemu_co_mutex_unlock(&s->lock); 16638b94ff85SPaolo Bonzini return ret; 166429c1a730SKevin Wolf } 1665bfe8043eSStefan Hajnoczi } 16668b94ff85SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 166729c1a730SKevin Wolf 1668eb489bb1SKevin Wolf return 0; 1669eb489bb1SKevin Wolf } 1670eb489bb1SKevin Wolf 16717c80ab3fSJes Sorensen static int64_t qcow2_vm_state_offset(BDRVQcowState *s) 167220d97356SBlue Swirl { 167320d97356SBlue Swirl return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); 167420d97356SBlue Swirl } 167520d97356SBlue Swirl 16767c80ab3fSJes Sorensen static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 167720d97356SBlue Swirl { 167820d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 167920d97356SBlue Swirl bdi->cluster_size = s->cluster_size; 16807c80ab3fSJes Sorensen bdi->vm_state_offset = qcow2_vm_state_offset(s); 168120d97356SBlue Swirl return 0; 168220d97356SBlue Swirl } 168320d97356SBlue Swirl 168420d97356SBlue Swirl #if 0 168520d97356SBlue Swirl static void dump_refcounts(BlockDriverState *bs) 168620d97356SBlue Swirl { 168720d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 168820d97356SBlue Swirl int64_t nb_clusters, k, k1, size; 168920d97356SBlue Swirl int refcount; 169020d97356SBlue Swirl 169166f82ceeSKevin Wolf size = bdrv_getlength(bs->file); 169220d97356SBlue Swirl nb_clusters = size_to_clusters(s, size); 169320d97356SBlue Swirl for(k = 0; k < nb_clusters;) { 169420d97356SBlue Swirl k1 = k; 169520d97356SBlue Swirl refcount = get_refcount(bs, k); 169620d97356SBlue Swirl k++; 169720d97356SBlue Swirl while (k < nb_clusters && get_refcount(bs, k) == refcount) 169820d97356SBlue Swirl k++; 16990bfcd599SBlue Swirl printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, 17000bfcd599SBlue Swirl k - k1); 170120d97356SBlue Swirl } 170220d97356SBlue Swirl } 170320d97356SBlue Swirl #endif 170420d97356SBlue Swirl 1705cf8074b3SKevin Wolf static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, 1706cf8074b3SKevin Wolf int64_t pos) 170720d97356SBlue Swirl { 170820d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 170920d97356SBlue Swirl int growable = bs->growable; 171020d97356SBlue Swirl int ret; 171120d97356SBlue Swirl 171266f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); 171320d97356SBlue Swirl bs->growable = 1; 17148d3b1a2dSKevin Wolf ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov); 171520d97356SBlue Swirl bs->growable = growable; 171620d97356SBlue Swirl 171720d97356SBlue Swirl return ret; 171820d97356SBlue Swirl } 171920d97356SBlue Swirl 17207c80ab3fSJes Sorensen static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf, 172120d97356SBlue Swirl int64_t pos, int size) 172220d97356SBlue Swirl { 172320d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 172420d97356SBlue Swirl int growable = bs->growable; 1725*0d51b4deSAsias He bool zero_beyond_eof = bs->zero_beyond_eof; 172620d97356SBlue Swirl int ret; 172720d97356SBlue Swirl 172866f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); 172920d97356SBlue Swirl bs->growable = 1; 1730*0d51b4deSAsias He bs->zero_beyond_eof = false; 17317c80ab3fSJes Sorensen ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size); 173220d97356SBlue Swirl bs->growable = growable; 1733*0d51b4deSAsias He bs->zero_beyond_eof = zero_beyond_eof; 173420d97356SBlue Swirl 173520d97356SBlue Swirl return ret; 173620d97356SBlue Swirl } 173720d97356SBlue Swirl 17387c80ab3fSJes Sorensen static QEMUOptionParameter qcow2_create_options[] = { 173920d97356SBlue Swirl { 174020d97356SBlue Swirl .name = BLOCK_OPT_SIZE, 174120d97356SBlue Swirl .type = OPT_SIZE, 174220d97356SBlue Swirl .help = "Virtual disk size" 174320d97356SBlue Swirl }, 174420d97356SBlue Swirl { 17456744cbabSKevin Wolf .name = BLOCK_OPT_COMPAT_LEVEL, 17466744cbabSKevin Wolf .type = OPT_STRING, 17476744cbabSKevin Wolf .help = "Compatibility level (0.10 or 1.1)" 17486744cbabSKevin Wolf }, 17496744cbabSKevin Wolf { 175020d97356SBlue Swirl .name = BLOCK_OPT_BACKING_FILE, 175120d97356SBlue Swirl .type = OPT_STRING, 175220d97356SBlue Swirl .help = "File name of a base image" 175320d97356SBlue Swirl }, 175420d97356SBlue Swirl { 175520d97356SBlue Swirl .name = BLOCK_OPT_BACKING_FMT, 175620d97356SBlue Swirl .type = OPT_STRING, 175720d97356SBlue Swirl .help = "Image format of the base image" 175820d97356SBlue Swirl }, 175920d97356SBlue Swirl { 176020d97356SBlue Swirl .name = BLOCK_OPT_ENCRYPT, 176120d97356SBlue Swirl .type = OPT_FLAG, 176220d97356SBlue Swirl .help = "Encrypt the image" 176320d97356SBlue Swirl }, 176420d97356SBlue Swirl { 176520d97356SBlue Swirl .name = BLOCK_OPT_CLUSTER_SIZE, 176620d97356SBlue Swirl .type = OPT_SIZE, 176799cce9faSKevin Wolf .help = "qcow2 cluster size", 176899cce9faSKevin Wolf .value = { .n = DEFAULT_CLUSTER_SIZE }, 176920d97356SBlue Swirl }, 177020d97356SBlue Swirl { 177120d97356SBlue Swirl .name = BLOCK_OPT_PREALLOC, 177220d97356SBlue Swirl .type = OPT_STRING, 177320d97356SBlue Swirl .help = "Preallocation mode (allowed values: off, metadata)" 177420d97356SBlue Swirl }, 1775bfe8043eSStefan Hajnoczi { 1776bfe8043eSStefan Hajnoczi .name = BLOCK_OPT_LAZY_REFCOUNTS, 1777bfe8043eSStefan Hajnoczi .type = OPT_FLAG, 1778bfe8043eSStefan Hajnoczi .help = "Postpone refcount updates", 1779bfe8043eSStefan Hajnoczi }, 178020d97356SBlue Swirl { NULL } 178120d97356SBlue Swirl }; 178220d97356SBlue Swirl 178320d97356SBlue Swirl static BlockDriver bdrv_qcow2 = { 178420d97356SBlue Swirl .format_name = "qcow2", 178520d97356SBlue Swirl .instance_size = sizeof(BDRVQcowState), 17867c80ab3fSJes Sorensen .bdrv_probe = qcow2_probe, 17877c80ab3fSJes Sorensen .bdrv_open = qcow2_open, 17887c80ab3fSJes Sorensen .bdrv_close = qcow2_close, 178921d82ac9SJeff Cody .bdrv_reopen_prepare = qcow2_reopen_prepare, 17907c80ab3fSJes Sorensen .bdrv_create = qcow2_create, 17913ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 1792f8a2e5e3SStefan Hajnoczi .bdrv_co_is_allocated = qcow2_co_is_allocated, 17937c80ab3fSJes Sorensen .bdrv_set_key = qcow2_set_key, 17947c80ab3fSJes Sorensen .bdrv_make_empty = qcow2_make_empty, 179520d97356SBlue Swirl 179668d100e9SKevin Wolf .bdrv_co_readv = qcow2_co_readv, 179768d100e9SKevin Wolf .bdrv_co_writev = qcow2_co_writev, 1798eb489bb1SKevin Wolf .bdrv_co_flush_to_os = qcow2_co_flush_to_os, 1799419b19d9SStefan Hajnoczi 1800621f0589SKevin Wolf .bdrv_co_write_zeroes = qcow2_co_write_zeroes, 18016db39ae2SPaolo Bonzini .bdrv_co_discard = qcow2_co_discard, 1802419b19d9SStefan Hajnoczi .bdrv_truncate = qcow2_truncate, 18037c80ab3fSJes Sorensen .bdrv_write_compressed = qcow2_write_compressed, 180420d97356SBlue Swirl 180520d97356SBlue Swirl .bdrv_snapshot_create = qcow2_snapshot_create, 180620d97356SBlue Swirl .bdrv_snapshot_goto = qcow2_snapshot_goto, 180720d97356SBlue Swirl .bdrv_snapshot_delete = qcow2_snapshot_delete, 180820d97356SBlue Swirl .bdrv_snapshot_list = qcow2_snapshot_list, 180951ef6727Sedison .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, 18107c80ab3fSJes Sorensen .bdrv_get_info = qcow2_get_info, 181120d97356SBlue Swirl 18127c80ab3fSJes Sorensen .bdrv_save_vmstate = qcow2_save_vmstate, 18137c80ab3fSJes Sorensen .bdrv_load_vmstate = qcow2_load_vmstate, 181420d97356SBlue Swirl 181520d97356SBlue Swirl .bdrv_change_backing_file = qcow2_change_backing_file, 181620d97356SBlue Swirl 181706d9260fSAnthony Liguori .bdrv_invalidate_cache = qcow2_invalidate_cache, 181806d9260fSAnthony Liguori 18197c80ab3fSJes Sorensen .create_options = qcow2_create_options, 18207c80ab3fSJes Sorensen .bdrv_check = qcow2_check, 182120d97356SBlue Swirl }; 182220d97356SBlue Swirl 18235efa9d5aSAnthony Liguori static void bdrv_qcow2_init(void) 18245efa9d5aSAnthony Liguori { 18255efa9d5aSAnthony Liguori bdrv_register(&bdrv_qcow2); 18265efa9d5aSAnthony Liguori } 18275efa9d5aSAnthony Liguori 18285efa9d5aSAnthony Liguori block_init(bdrv_qcow2_init); 1829