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; 55c4217f64SJeff Cody } QEMU_PACKED 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, 823ef6c40aSMax Reitz uint64_t end_offset, void **p_feature_table, 833ef6c40aSMax Reitz Error **errp) 849b80ddf3Saliguori { 8575bab85cSKevin Wolf BDRVQcowState *s = bs->opaque; 869b80ddf3Saliguori QCowExtension ext; 879b80ddf3Saliguori uint64_t offset; 8875bab85cSKevin Wolf int ret; 899b80ddf3Saliguori 909b80ddf3Saliguori #ifdef DEBUG_EXT 917c80ab3fSJes Sorensen printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); 929b80ddf3Saliguori #endif 939b80ddf3Saliguori offset = start_offset; 949b80ddf3Saliguori while (offset < end_offset) { 959b80ddf3Saliguori 969b80ddf3Saliguori #ifdef DEBUG_EXT 979b80ddf3Saliguori /* Sanity check */ 989b80ddf3Saliguori if (offset > s->cluster_size) 997c80ab3fSJes Sorensen printf("qcow2_read_extension: suspicious offset %lu\n", offset); 1009b80ddf3Saliguori 1019b2260cbSDong Xu Wang printf("attempting to read extended header in offset %lu\n", offset); 1029b80ddf3Saliguori #endif 1039b80ddf3Saliguori 1043ef6c40aSMax Reitz ret = bdrv_pread(bs->file, offset, &ext, sizeof(ext)); 1053ef6c40aSMax Reitz if (ret < 0) { 1063ef6c40aSMax Reitz error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: " 1073ef6c40aSMax Reitz "pread fail from offset %" PRIu64, offset); 1089b80ddf3Saliguori return 1; 1099b80ddf3Saliguori } 1109b80ddf3Saliguori be32_to_cpus(&ext.magic); 1119b80ddf3Saliguori be32_to_cpus(&ext.len); 1129b80ddf3Saliguori offset += sizeof(ext); 1139b80ddf3Saliguori #ifdef DEBUG_EXT 1149b80ddf3Saliguori printf("ext.magic = 0x%x\n", ext.magic); 1159b80ddf3Saliguori #endif 11664ca6aeeSKevin Wolf if (ext.len > end_offset - offset) { 1173ef6c40aSMax Reitz error_setg(errp, "Header extension too large"); 11864ca6aeeSKevin Wolf return -EINVAL; 11964ca6aeeSKevin Wolf } 12064ca6aeeSKevin Wolf 1219b80ddf3Saliguori switch (ext.magic) { 1227c80ab3fSJes Sorensen case QCOW2_EXT_MAGIC_END: 1239b80ddf3Saliguori return 0; 124f965509cSaliguori 1257c80ab3fSJes Sorensen case QCOW2_EXT_MAGIC_BACKING_FORMAT: 126f965509cSaliguori if (ext.len >= sizeof(bs->backing_format)) { 1273ef6c40aSMax Reitz error_setg(errp, "ERROR: ext_backing_format: len=%u too large" 1283ef6c40aSMax Reitz " (>=%zu)", ext.len, sizeof(bs->backing_format)); 129f965509cSaliguori return 2; 130f965509cSaliguori } 1313ef6c40aSMax Reitz ret = bdrv_pread(bs->file, offset, bs->backing_format, ext.len); 1323ef6c40aSMax Reitz if (ret < 0) { 1333ef6c40aSMax Reitz error_setg_errno(errp, -ret, "ERROR: ext_backing_format: " 1343ef6c40aSMax Reitz "Could not read format name"); 135f965509cSaliguori return 3; 1363ef6c40aSMax Reitz } 137f965509cSaliguori bs->backing_format[ext.len] = '\0'; 138f965509cSaliguori #ifdef DEBUG_EXT 139f965509cSaliguori printf("Qcow2: Got format extension %s\n", bs->backing_format); 140f965509cSaliguori #endif 141f965509cSaliguori break; 142f965509cSaliguori 143cfcc4c62SKevin Wolf case QCOW2_EXT_MAGIC_FEATURE_TABLE: 144cfcc4c62SKevin Wolf if (p_feature_table != NULL) { 145cfcc4c62SKevin Wolf void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature)); 146cfcc4c62SKevin Wolf ret = bdrv_pread(bs->file, offset , feature_table, ext.len); 147cfcc4c62SKevin Wolf if (ret < 0) { 1483ef6c40aSMax Reitz error_setg_errno(errp, -ret, "ERROR: ext_feature_table: " 1493ef6c40aSMax Reitz "Could not read table"); 150cfcc4c62SKevin Wolf return ret; 151cfcc4c62SKevin Wolf } 152cfcc4c62SKevin Wolf 153cfcc4c62SKevin Wolf *p_feature_table = feature_table; 154cfcc4c62SKevin Wolf } 155cfcc4c62SKevin Wolf break; 156cfcc4c62SKevin Wolf 1579b80ddf3Saliguori default: 15875bab85cSKevin Wolf /* unknown magic - save it in case we need to rewrite the header */ 15975bab85cSKevin Wolf { 16075bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext; 16175bab85cSKevin Wolf 16275bab85cSKevin Wolf uext = g_malloc0(sizeof(*uext) + ext.len); 16375bab85cSKevin Wolf uext->magic = ext.magic; 16475bab85cSKevin Wolf uext->len = ext.len; 16575bab85cSKevin Wolf QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next); 16675bab85cSKevin Wolf 16775bab85cSKevin Wolf ret = bdrv_pread(bs->file, offset , uext->data, uext->len); 16875bab85cSKevin Wolf if (ret < 0) { 1693ef6c40aSMax Reitz error_setg_errno(errp, -ret, "ERROR: unknown extension: " 1703ef6c40aSMax Reitz "Could not read data"); 17175bab85cSKevin Wolf return ret; 17275bab85cSKevin Wolf } 17375bab85cSKevin Wolf } 1749b80ddf3Saliguori break; 1759b80ddf3Saliguori } 176fd29b4bbSKevin Wolf 177fd29b4bbSKevin Wolf offset += ((ext.len + 7) & ~7); 1789b80ddf3Saliguori } 1799b80ddf3Saliguori 1809b80ddf3Saliguori return 0; 1819b80ddf3Saliguori } 1829b80ddf3Saliguori 18375bab85cSKevin Wolf static void cleanup_unknown_header_ext(BlockDriverState *bs) 18475bab85cSKevin Wolf { 18575bab85cSKevin Wolf BDRVQcowState *s = bs->opaque; 18675bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext, *next; 18775bab85cSKevin Wolf 18875bab85cSKevin Wolf QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) { 18975bab85cSKevin Wolf QLIST_REMOVE(uext, next); 19075bab85cSKevin Wolf g_free(uext); 19175bab85cSKevin Wolf } 19275bab85cSKevin Wolf } 1939b80ddf3Saliguori 1943ef6c40aSMax Reitz static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs, 1953ef6c40aSMax Reitz Error **errp, const char *fmt, ...) 1966744cbabSKevin Wolf { 1976744cbabSKevin Wolf char msg[64]; 1986744cbabSKevin Wolf va_list ap; 1996744cbabSKevin Wolf 2006744cbabSKevin Wolf va_start(ap, fmt); 2016744cbabSKevin Wolf vsnprintf(msg, sizeof(msg), fmt, ap); 2026744cbabSKevin Wolf va_end(ap); 2036744cbabSKevin Wolf 2043ef6c40aSMax Reitz error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, bs->device_name, "qcow2", 2053ef6c40aSMax Reitz msg); 2066744cbabSKevin Wolf } 2076744cbabSKevin Wolf 208cfcc4c62SKevin Wolf static void report_unsupported_feature(BlockDriverState *bs, 2093ef6c40aSMax Reitz Error **errp, Qcow2Feature *table, uint64_t mask) 210cfcc4c62SKevin Wolf { 211cfcc4c62SKevin Wolf while (table && table->name[0] != '\0') { 212cfcc4c62SKevin Wolf if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) { 213cfcc4c62SKevin Wolf if (mask & (1 << table->bit)) { 2143ef6c40aSMax Reitz report_unsupported(bs, errp, "%.46s", table->name); 215cfcc4c62SKevin Wolf mask &= ~(1 << table->bit); 216cfcc4c62SKevin Wolf } 217cfcc4c62SKevin Wolf } 218cfcc4c62SKevin Wolf table++; 219cfcc4c62SKevin Wolf } 220cfcc4c62SKevin Wolf 221cfcc4c62SKevin Wolf if (mask) { 2223ef6c40aSMax Reitz report_unsupported(bs, errp, "Unknown incompatible feature: %" PRIx64, 2233ef6c40aSMax Reitz mask); 224cfcc4c62SKevin Wolf } 225cfcc4c62SKevin Wolf } 226cfcc4c62SKevin Wolf 227c61d0004SStefan Hajnoczi /* 228bfe8043eSStefan Hajnoczi * Sets the dirty bit and flushes afterwards if necessary. 229bfe8043eSStefan Hajnoczi * 230bfe8043eSStefan Hajnoczi * The incompatible_features bit is only set if the image file header was 231bfe8043eSStefan Hajnoczi * updated successfully. Therefore it is not required to check the return 232bfe8043eSStefan Hajnoczi * value of this function. 233bfe8043eSStefan Hajnoczi */ 234280d3735SKevin Wolf int qcow2_mark_dirty(BlockDriverState *bs) 235bfe8043eSStefan Hajnoczi { 236bfe8043eSStefan Hajnoczi BDRVQcowState *s = bs->opaque; 237bfe8043eSStefan Hajnoczi uint64_t val; 238bfe8043eSStefan Hajnoczi int ret; 239bfe8043eSStefan Hajnoczi 240bfe8043eSStefan Hajnoczi assert(s->qcow_version >= 3); 241bfe8043eSStefan Hajnoczi 242bfe8043eSStefan Hajnoczi if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 243bfe8043eSStefan Hajnoczi return 0; /* already dirty */ 244bfe8043eSStefan Hajnoczi } 245bfe8043eSStefan Hajnoczi 246bfe8043eSStefan Hajnoczi val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY); 247bfe8043eSStefan Hajnoczi ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features), 248bfe8043eSStefan Hajnoczi &val, sizeof(val)); 249bfe8043eSStefan Hajnoczi if (ret < 0) { 250bfe8043eSStefan Hajnoczi return ret; 251bfe8043eSStefan Hajnoczi } 252bfe8043eSStefan Hajnoczi ret = bdrv_flush(bs->file); 253bfe8043eSStefan Hajnoczi if (ret < 0) { 254bfe8043eSStefan Hajnoczi return ret; 255bfe8043eSStefan Hajnoczi } 256bfe8043eSStefan Hajnoczi 257bfe8043eSStefan Hajnoczi /* Only treat image as dirty if the header was updated successfully */ 258bfe8043eSStefan Hajnoczi s->incompatible_features |= QCOW2_INCOMPAT_DIRTY; 259bfe8043eSStefan Hajnoczi return 0; 260bfe8043eSStefan Hajnoczi } 261bfe8043eSStefan Hajnoczi 262bfe8043eSStefan Hajnoczi /* 263c61d0004SStefan Hajnoczi * Clears the dirty bit and flushes before if necessary. Only call this 264c61d0004SStefan Hajnoczi * function when there are no pending requests, it does not guard against 265c61d0004SStefan Hajnoczi * concurrent requests dirtying the image. 266c61d0004SStefan Hajnoczi */ 267c61d0004SStefan Hajnoczi static int qcow2_mark_clean(BlockDriverState *bs) 268c61d0004SStefan Hajnoczi { 269c61d0004SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 270c61d0004SStefan Hajnoczi 271c61d0004SStefan Hajnoczi if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 272c61d0004SStefan Hajnoczi int ret = bdrv_flush(bs); 273c61d0004SStefan Hajnoczi if (ret < 0) { 274c61d0004SStefan Hajnoczi return ret; 275c61d0004SStefan Hajnoczi } 276c61d0004SStefan Hajnoczi 277c61d0004SStefan Hajnoczi s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY; 278c61d0004SStefan Hajnoczi return qcow2_update_header(bs); 279c61d0004SStefan Hajnoczi } 280c61d0004SStefan Hajnoczi return 0; 281c61d0004SStefan Hajnoczi } 282c61d0004SStefan Hajnoczi 28369c98726SMax Reitz /* 28469c98726SMax Reitz * Marks the image as corrupt. 28569c98726SMax Reitz */ 28669c98726SMax Reitz int qcow2_mark_corrupt(BlockDriverState *bs) 28769c98726SMax Reitz { 28869c98726SMax Reitz BDRVQcowState *s = bs->opaque; 28969c98726SMax Reitz 29069c98726SMax Reitz s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT; 29169c98726SMax Reitz return qcow2_update_header(bs); 29269c98726SMax Reitz } 29369c98726SMax Reitz 29469c98726SMax Reitz /* 29569c98726SMax Reitz * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes 29669c98726SMax Reitz * before if necessary. 29769c98726SMax Reitz */ 29869c98726SMax Reitz int qcow2_mark_consistent(BlockDriverState *bs) 29969c98726SMax Reitz { 30069c98726SMax Reitz BDRVQcowState *s = bs->opaque; 30169c98726SMax Reitz 30269c98726SMax Reitz if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { 30369c98726SMax Reitz int ret = bdrv_flush(bs); 30469c98726SMax Reitz if (ret < 0) { 30569c98726SMax Reitz return ret; 30669c98726SMax Reitz } 30769c98726SMax Reitz 30869c98726SMax Reitz s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT; 30969c98726SMax Reitz return qcow2_update_header(bs); 31069c98726SMax Reitz } 31169c98726SMax Reitz return 0; 31269c98726SMax Reitz } 31369c98726SMax Reitz 314acbe5982SStefan Hajnoczi static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, 315acbe5982SStefan Hajnoczi BdrvCheckMode fix) 316acbe5982SStefan Hajnoczi { 317acbe5982SStefan Hajnoczi int ret = qcow2_check_refcounts(bs, result, fix); 318acbe5982SStefan Hajnoczi if (ret < 0) { 319acbe5982SStefan Hajnoczi return ret; 320acbe5982SStefan Hajnoczi } 321acbe5982SStefan Hajnoczi 322acbe5982SStefan Hajnoczi if (fix && result->check_errors == 0 && result->corruptions == 0) { 32324530f3eSMax Reitz ret = qcow2_mark_clean(bs); 32424530f3eSMax Reitz if (ret < 0) { 32524530f3eSMax Reitz return ret; 32624530f3eSMax Reitz } 32724530f3eSMax Reitz return qcow2_mark_consistent(bs); 328acbe5982SStefan Hajnoczi } 329acbe5982SStefan Hajnoczi return ret; 330acbe5982SStefan Hajnoczi } 331acbe5982SStefan Hajnoczi 33274c4510aSKevin Wolf static QemuOptsList qcow2_runtime_opts = { 33374c4510aSKevin Wolf .name = "qcow2", 33474c4510aSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head), 33574c4510aSKevin Wolf .desc = { 33674c4510aSKevin Wolf { 33764aa99d3SKevin Wolf .name = QCOW2_OPT_LAZY_REFCOUNTS, 33874c4510aSKevin Wolf .type = QEMU_OPT_BOOL, 33974c4510aSKevin Wolf .help = "Postpone refcount updates", 34074c4510aSKevin Wolf }, 34167af674eSKevin Wolf { 34267af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_REQUEST, 34367af674eSKevin Wolf .type = QEMU_OPT_BOOL, 34467af674eSKevin Wolf .help = "Pass guest discard requests to the layer below", 34567af674eSKevin Wolf }, 34667af674eSKevin Wolf { 34767af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_SNAPSHOT, 34867af674eSKevin Wolf .type = QEMU_OPT_BOOL, 34967af674eSKevin Wolf .help = "Generate discard requests when snapshot related space " 35067af674eSKevin Wolf "is freed", 35167af674eSKevin Wolf }, 35267af674eSKevin Wolf { 35367af674eSKevin Wolf .name = QCOW2_OPT_DISCARD_OTHER, 35467af674eSKevin Wolf .type = QEMU_OPT_BOOL, 35567af674eSKevin Wolf .help = "Generate discard requests when other clusters are freed", 35667af674eSKevin Wolf }, 35705de7e86SMax Reitz { 35805de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP, 35905de7e86SMax Reitz .type = QEMU_OPT_STRING, 36005de7e86SMax Reitz .help = "Selects which overlap checks to perform from a range of " 36105de7e86SMax Reitz "templates (none, constant, cached, all)", 36205de7e86SMax Reitz }, 36305de7e86SMax Reitz { 36405de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_MAIN_HEADER, 36505de7e86SMax Reitz .type = QEMU_OPT_BOOL, 36605de7e86SMax Reitz .help = "Check for unintended writes into the main qcow2 header", 36705de7e86SMax Reitz }, 36805de7e86SMax Reitz { 36905de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_ACTIVE_L1, 37005de7e86SMax Reitz .type = QEMU_OPT_BOOL, 37105de7e86SMax Reitz .help = "Check for unintended writes into the active L1 table", 37205de7e86SMax Reitz }, 37305de7e86SMax Reitz { 37405de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_ACTIVE_L2, 37505de7e86SMax Reitz .type = QEMU_OPT_BOOL, 37605de7e86SMax Reitz .help = "Check for unintended writes into an active L2 table", 37705de7e86SMax Reitz }, 37805de7e86SMax Reitz { 37905de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, 38005de7e86SMax Reitz .type = QEMU_OPT_BOOL, 38105de7e86SMax Reitz .help = "Check for unintended writes into the refcount table", 38205de7e86SMax Reitz }, 38305de7e86SMax Reitz { 38405de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, 38505de7e86SMax Reitz .type = QEMU_OPT_BOOL, 38605de7e86SMax Reitz .help = "Check for unintended writes into a refcount block", 38705de7e86SMax Reitz }, 38805de7e86SMax Reitz { 38905de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, 39005de7e86SMax Reitz .type = QEMU_OPT_BOOL, 39105de7e86SMax Reitz .help = "Check for unintended writes into the snapshot table", 39205de7e86SMax Reitz }, 39305de7e86SMax Reitz { 39405de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_INACTIVE_L1, 39505de7e86SMax Reitz .type = QEMU_OPT_BOOL, 39605de7e86SMax Reitz .help = "Check for unintended writes into an inactive L1 table", 39705de7e86SMax Reitz }, 39805de7e86SMax Reitz { 39905de7e86SMax Reitz .name = QCOW2_OPT_OVERLAP_INACTIVE_L2, 40005de7e86SMax Reitz .type = QEMU_OPT_BOOL, 40105de7e86SMax Reitz .help = "Check for unintended writes into an inactive L2 table", 40205de7e86SMax Reitz }, 40374c4510aSKevin Wolf { /* end of list */ } 40474c4510aSKevin Wolf }, 40574c4510aSKevin Wolf }; 40674c4510aSKevin Wolf 4074092e99dSMax Reitz static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = { 4084092e99dSMax Reitz [QCOW2_OL_MAIN_HEADER_BITNR] = QCOW2_OPT_OVERLAP_MAIN_HEADER, 4094092e99dSMax Reitz [QCOW2_OL_ACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L1, 4104092e99dSMax Reitz [QCOW2_OL_ACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_ACTIVE_L2, 4114092e99dSMax Reitz [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE, 4124092e99dSMax Reitz [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK, 4134092e99dSMax Reitz [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE, 4144092e99dSMax Reitz [QCOW2_OL_INACTIVE_L1_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L1, 4154092e99dSMax Reitz [QCOW2_OL_INACTIVE_L2_BITNR] = QCOW2_OPT_OVERLAP_INACTIVE_L2, 4164092e99dSMax Reitz }; 4174092e99dSMax Reitz 418015a1036SMax Reitz static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, 419015a1036SMax Reitz Error **errp) 420585f8587Sbellard { 421585f8587Sbellard BDRVQcowState *s = bs->opaque; 4226d85a57eSJes Sorensen int len, i, ret = 0; 423585f8587Sbellard QCowHeader header; 42474c4510aSKevin Wolf QemuOpts *opts; 42574c4510aSKevin Wolf Error *local_err = NULL; 4269b80ddf3Saliguori uint64_t ext_end; 4272cf7cfa1SKevin Wolf uint64_t l1_vm_state_index; 4281fa5cc83SMax Reitz const char *opt_overlap_check; 4291fa5cc83SMax Reitz int overlap_check_template = 0; 430585f8587Sbellard 4316d85a57eSJes Sorensen ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); 4326d85a57eSJes Sorensen if (ret < 0) { 4333ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not read qcow2 header"); 434585f8587Sbellard goto fail; 4356d85a57eSJes Sorensen } 436585f8587Sbellard be32_to_cpus(&header.magic); 437585f8587Sbellard be32_to_cpus(&header.version); 438585f8587Sbellard be64_to_cpus(&header.backing_file_offset); 439585f8587Sbellard be32_to_cpus(&header.backing_file_size); 440585f8587Sbellard be64_to_cpus(&header.size); 441585f8587Sbellard be32_to_cpus(&header.cluster_bits); 442585f8587Sbellard be32_to_cpus(&header.crypt_method); 443585f8587Sbellard be64_to_cpus(&header.l1_table_offset); 444585f8587Sbellard be32_to_cpus(&header.l1_size); 445585f8587Sbellard be64_to_cpus(&header.refcount_table_offset); 446585f8587Sbellard be32_to_cpus(&header.refcount_table_clusters); 447585f8587Sbellard be64_to_cpus(&header.snapshots_offset); 448585f8587Sbellard be32_to_cpus(&header.nb_snapshots); 449585f8587Sbellard 450e8cdcec1SKevin Wolf if (header.magic != QCOW_MAGIC) { 4513ef6c40aSMax Reitz error_setg(errp, "Image is not in qcow2 format"); 45215bac0d5SStefan Weil ret = -EMEDIUMTYPE; 453585f8587Sbellard goto fail; 4546d85a57eSJes Sorensen } 4556744cbabSKevin Wolf if (header.version < 2 || header.version > 3) { 4563ef6c40aSMax Reitz report_unsupported(bs, errp, "QCOW version %d", header.version); 457e8cdcec1SKevin Wolf ret = -ENOTSUP; 458e8cdcec1SKevin Wolf goto fail; 459e8cdcec1SKevin Wolf } 4606744cbabSKevin Wolf 4616744cbabSKevin Wolf s->qcow_version = header.version; 4626744cbabSKevin Wolf 4636744cbabSKevin Wolf /* Initialise version 3 header fields */ 4646744cbabSKevin Wolf if (header.version == 2) { 4656744cbabSKevin Wolf header.incompatible_features = 0; 4666744cbabSKevin Wolf header.compatible_features = 0; 4676744cbabSKevin Wolf header.autoclear_features = 0; 4686744cbabSKevin Wolf header.refcount_order = 4; 4696744cbabSKevin Wolf header.header_length = 72; 4706744cbabSKevin Wolf } else { 4716744cbabSKevin Wolf be64_to_cpus(&header.incompatible_features); 4726744cbabSKevin Wolf be64_to_cpus(&header.compatible_features); 4736744cbabSKevin Wolf be64_to_cpus(&header.autoclear_features); 4746744cbabSKevin Wolf be32_to_cpus(&header.refcount_order); 4756744cbabSKevin Wolf be32_to_cpus(&header.header_length); 4766744cbabSKevin Wolf } 4776744cbabSKevin Wolf 4786744cbabSKevin Wolf if (header.header_length > sizeof(header)) { 4796744cbabSKevin Wolf s->unknown_header_fields_size = header.header_length - sizeof(header); 4806744cbabSKevin Wolf s->unknown_header_fields = g_malloc(s->unknown_header_fields_size); 4816744cbabSKevin Wolf ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields, 4826744cbabSKevin Wolf s->unknown_header_fields_size); 4836744cbabSKevin Wolf if (ret < 0) { 4843ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not read unknown qcow2 header " 4853ef6c40aSMax Reitz "fields"); 4866744cbabSKevin Wolf goto fail; 4876744cbabSKevin Wolf } 4886744cbabSKevin Wolf } 4896744cbabSKevin Wolf 490cfcc4c62SKevin Wolf if (header.backing_file_offset) { 491cfcc4c62SKevin Wolf ext_end = header.backing_file_offset; 492cfcc4c62SKevin Wolf } else { 493cfcc4c62SKevin Wolf ext_end = 1 << header.cluster_bits; 494cfcc4c62SKevin Wolf } 495cfcc4c62SKevin Wolf 4966744cbabSKevin Wolf /* Handle feature bits */ 4976744cbabSKevin Wolf s->incompatible_features = header.incompatible_features; 4986744cbabSKevin Wolf s->compatible_features = header.compatible_features; 4996744cbabSKevin Wolf s->autoclear_features = header.autoclear_features; 5006744cbabSKevin Wolf 501c61d0004SStefan Hajnoczi if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) { 502cfcc4c62SKevin Wolf void *feature_table = NULL; 503cfcc4c62SKevin Wolf qcow2_read_extensions(bs, header.header_length, ext_end, 5043ef6c40aSMax Reitz &feature_table, NULL); 5053ef6c40aSMax Reitz report_unsupported_feature(bs, errp, feature_table, 506c61d0004SStefan Hajnoczi s->incompatible_features & 507c61d0004SStefan Hajnoczi ~QCOW2_INCOMPAT_MASK); 5086744cbabSKevin Wolf ret = -ENOTSUP; 5096744cbabSKevin Wolf goto fail; 5106744cbabSKevin Wolf } 5116744cbabSKevin Wolf 51269c98726SMax Reitz if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) { 51369c98726SMax Reitz /* Corrupt images may not be written to unless they are being repaired 51469c98726SMax Reitz */ 51569c98726SMax Reitz if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { 5163ef6c40aSMax Reitz error_setg(errp, "qcow2: Image is corrupt; cannot be opened " 5173ef6c40aSMax Reitz "read/write"); 51869c98726SMax Reitz ret = -EACCES; 51969c98726SMax Reitz goto fail; 52069c98726SMax Reitz } 52169c98726SMax Reitz } 52269c98726SMax Reitz 5236744cbabSKevin Wolf /* Check support for various header values */ 5246744cbabSKevin Wolf if (header.refcount_order != 4) { 5253ef6c40aSMax Reitz report_unsupported(bs, errp, "%d bit reference counts", 5266744cbabSKevin Wolf 1 << header.refcount_order); 5276744cbabSKevin Wolf ret = -ENOTSUP; 5286744cbabSKevin Wolf goto fail; 5296744cbabSKevin Wolf } 530b6481f37SMax Reitz s->refcount_order = header.refcount_order; 5316744cbabSKevin Wolf 532d191d12dSStefan Weil if (header.cluster_bits < MIN_CLUSTER_BITS || 5336d85a57eSJes Sorensen header.cluster_bits > MAX_CLUSTER_BITS) { 5343ef6c40aSMax Reitz error_setg(errp, "Unsupported cluster size: 2^%i", header.cluster_bits); 5356d85a57eSJes Sorensen ret = -EINVAL; 536585f8587Sbellard goto fail; 5376d85a57eSJes Sorensen } 5386d85a57eSJes Sorensen if (header.crypt_method > QCOW_CRYPT_AES) { 5393ef6c40aSMax Reitz error_setg(errp, "Unsupported encryption method: %i", 5403ef6c40aSMax Reitz header.crypt_method); 5416d85a57eSJes Sorensen ret = -EINVAL; 542585f8587Sbellard goto fail; 5436d85a57eSJes Sorensen } 544585f8587Sbellard s->crypt_method_header = header.crypt_method; 5456d85a57eSJes Sorensen if (s->crypt_method_header) { 546585f8587Sbellard bs->encrypted = 1; 5476d85a57eSJes Sorensen } 548585f8587Sbellard s->cluster_bits = header.cluster_bits; 549585f8587Sbellard s->cluster_size = 1 << s->cluster_bits; 550585f8587Sbellard s->cluster_sectors = 1 << (s->cluster_bits - 9); 551585f8587Sbellard s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ 552585f8587Sbellard s->l2_size = 1 << s->l2_bits; 553585f8587Sbellard bs->total_sectors = header.size / 512; 554585f8587Sbellard s->csize_shift = (62 - (s->cluster_bits - 8)); 555585f8587Sbellard s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; 556585f8587Sbellard s->cluster_offset_mask = (1LL << s->csize_shift) - 1; 557585f8587Sbellard s->refcount_table_offset = header.refcount_table_offset; 558585f8587Sbellard s->refcount_table_size = 559585f8587Sbellard header.refcount_table_clusters << (s->cluster_bits - 3); 560585f8587Sbellard 561585f8587Sbellard s->snapshots_offset = header.snapshots_offset; 562585f8587Sbellard s->nb_snapshots = header.nb_snapshots; 563585f8587Sbellard 564585f8587Sbellard /* read the level 1 table */ 565585f8587Sbellard s->l1_size = header.l1_size; 5662cf7cfa1SKevin Wolf 5672cf7cfa1SKevin Wolf l1_vm_state_index = size_to_l1(s, header.size); 5682cf7cfa1SKevin Wolf if (l1_vm_state_index > INT_MAX) { 5693ef6c40aSMax Reitz error_setg(errp, "Image is too big"); 5702cf7cfa1SKevin Wolf ret = -EFBIG; 5712cf7cfa1SKevin Wolf goto fail; 5722cf7cfa1SKevin Wolf } 5732cf7cfa1SKevin Wolf s->l1_vm_state_index = l1_vm_state_index; 5742cf7cfa1SKevin Wolf 575585f8587Sbellard /* the L1 table must contain at least enough entries to put 576585f8587Sbellard header.size bytes */ 5776d85a57eSJes Sorensen if (s->l1_size < s->l1_vm_state_index) { 5783ef6c40aSMax Reitz error_setg(errp, "L1 table is too small"); 5796d85a57eSJes Sorensen ret = -EINVAL; 580585f8587Sbellard goto fail; 5816d85a57eSJes Sorensen } 582585f8587Sbellard s->l1_table_offset = header.l1_table_offset; 583d191d12dSStefan Weil if (s->l1_size > 0) { 5847267c094SAnthony Liguori s->l1_table = g_malloc0( 5853f6a3ee5SKevin Wolf align_offset(s->l1_size * sizeof(uint64_t), 512)); 5866d85a57eSJes Sorensen ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, 5876d85a57eSJes Sorensen s->l1_size * sizeof(uint64_t)); 5886d85a57eSJes Sorensen if (ret < 0) { 5893ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not read L1 table"); 590585f8587Sbellard goto fail; 5916d85a57eSJes Sorensen } 592585f8587Sbellard for(i = 0;i < s->l1_size; i++) { 593585f8587Sbellard be64_to_cpus(&s->l1_table[i]); 594585f8587Sbellard } 595d191d12dSStefan Weil } 59629c1a730SKevin Wolf 59729c1a730SKevin Wolf /* alloc L2 table/refcount block cache */ 5986af4e9eaSPaolo Bonzini s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE); 5996af4e9eaSPaolo Bonzini s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE); 60029c1a730SKevin Wolf 6017267c094SAnthony Liguori s->cluster_cache = g_malloc(s->cluster_size); 602585f8587Sbellard /* one more sector for decompressed data alignment */ 603dea43a65SFrediano Ziglio s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size 604095a9c58Saliguori + 512); 605585f8587Sbellard s->cluster_cache_offset = -1; 60606d9260fSAnthony Liguori s->flags = flags; 607585f8587Sbellard 6086d85a57eSJes Sorensen ret = qcow2_refcount_init(bs); 6096d85a57eSJes Sorensen if (ret != 0) { 6103ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not initialize refcount handling"); 611585f8587Sbellard goto fail; 6126d85a57eSJes Sorensen } 613585f8587Sbellard 61472cf2d4fSBlue Swirl QLIST_INIT(&s->cluster_allocs); 6150b919faeSKevin Wolf QTAILQ_INIT(&s->discards); 616f214978aSKevin Wolf 6179b80ddf3Saliguori /* read qcow2 extensions */ 6183ef6c40aSMax Reitz if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL, 6193ef6c40aSMax Reitz &local_err)) { 6203ef6c40aSMax Reitz error_propagate(errp, local_err); 6216d85a57eSJes Sorensen ret = -EINVAL; 6229b80ddf3Saliguori goto fail; 6236d85a57eSJes Sorensen } 6249b80ddf3Saliguori 625585f8587Sbellard /* read the backing file name */ 626585f8587Sbellard if (header.backing_file_offset != 0) { 627585f8587Sbellard len = header.backing_file_size; 6286d85a57eSJes Sorensen if (len > 1023) { 629585f8587Sbellard len = 1023; 6306d85a57eSJes Sorensen } 6316d85a57eSJes Sorensen ret = bdrv_pread(bs->file, header.backing_file_offset, 6326d85a57eSJes Sorensen bs->backing_file, len); 6336d85a57eSJes Sorensen if (ret < 0) { 6343ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not read backing file name"); 635585f8587Sbellard goto fail; 6366d85a57eSJes Sorensen } 637585f8587Sbellard bs->backing_file[len] = '\0'; 638585f8587Sbellard } 63942deb29fSKevin Wolf 64042deb29fSKevin Wolf ret = qcow2_read_snapshots(bs); 64142deb29fSKevin Wolf if (ret < 0) { 6423ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not read snapshots"); 643585f8587Sbellard goto fail; 6446d85a57eSJes Sorensen } 645585f8587Sbellard 646af7b708dSStefan Hajnoczi /* Clear unknown autoclear feature bits */ 647af7b708dSStefan Hajnoczi if (!bs->read_only && s->autoclear_features != 0) { 648af7b708dSStefan Hajnoczi s->autoclear_features = 0; 649af7b708dSStefan Hajnoczi ret = qcow2_update_header(bs); 650af7b708dSStefan Hajnoczi if (ret < 0) { 6513ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not update qcow2 header"); 652af7b708dSStefan Hajnoczi goto fail; 653af7b708dSStefan Hajnoczi } 654af7b708dSStefan Hajnoczi } 655af7b708dSStefan Hajnoczi 65668d100e9SKevin Wolf /* Initialise locks */ 65768d100e9SKevin Wolf qemu_co_mutex_init(&s->lock); 65868d100e9SKevin Wolf 659c61d0004SStefan Hajnoczi /* Repair image if dirty */ 660058f8f16SStefan Hajnoczi if (!(flags & BDRV_O_CHECK) && !bs->read_only && 661058f8f16SStefan Hajnoczi (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) { 662c61d0004SStefan Hajnoczi BdrvCheckResult result = {0}; 663c61d0004SStefan Hajnoczi 664acbe5982SStefan Hajnoczi ret = qcow2_check(bs, &result, BDRV_FIX_ERRORS); 665c61d0004SStefan Hajnoczi if (ret < 0) { 6663ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not repair dirty image"); 667c61d0004SStefan Hajnoczi goto fail; 668c61d0004SStefan Hajnoczi } 669c61d0004SStefan Hajnoczi } 670c61d0004SStefan Hajnoczi 67174c4510aSKevin Wolf /* Enable lazy_refcounts according to image and command line options */ 67274c4510aSKevin Wolf opts = qemu_opts_create_nofail(&qcow2_runtime_opts); 67374c4510aSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 67474c4510aSKevin Wolf if (error_is_set(&local_err)) { 6753ef6c40aSMax Reitz error_propagate(errp, local_err); 67674c4510aSKevin Wolf ret = -EINVAL; 67774c4510aSKevin Wolf goto fail; 67874c4510aSKevin Wolf } 67974c4510aSKevin Wolf 680acdfb480SKevin Wolf s->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS, 68174c4510aSKevin Wolf (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)); 68274c4510aSKevin Wolf 68367af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_NEVER] = false; 68467af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true; 68567af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_REQUEST] = 68667af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST, 68767af674eSKevin Wolf flags & BDRV_O_UNMAP); 68867af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] = 68967af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true); 69067af674eSKevin Wolf s->discard_passthrough[QCOW2_DISCARD_OTHER] = 69167af674eSKevin Wolf qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false); 69267af674eSKevin Wolf 6931fa5cc83SMax Reitz opt_overlap_check = qemu_opt_get(opts, "overlap-check") ?: "cached"; 6941fa5cc83SMax Reitz if (!strcmp(opt_overlap_check, "none")) { 6951fa5cc83SMax Reitz overlap_check_template = 0; 6961fa5cc83SMax Reitz } else if (!strcmp(opt_overlap_check, "constant")) { 6971fa5cc83SMax Reitz overlap_check_template = QCOW2_OL_CONSTANT; 6981fa5cc83SMax Reitz } else if (!strcmp(opt_overlap_check, "cached")) { 6991fa5cc83SMax Reitz overlap_check_template = QCOW2_OL_CACHED; 7001fa5cc83SMax Reitz } else if (!strcmp(opt_overlap_check, "all")) { 7011fa5cc83SMax Reitz overlap_check_template = QCOW2_OL_ALL; 7021fa5cc83SMax Reitz } else { 7031fa5cc83SMax Reitz error_setg(errp, "Unsupported value '%s' for qcow2 option " 7041fa5cc83SMax Reitz "'overlap-check'. Allowed are either of the following: " 7051fa5cc83SMax Reitz "none, constant, cached, all", opt_overlap_check); 7061fa5cc83SMax Reitz qemu_opts_del(opts); 7071fa5cc83SMax Reitz ret = -EINVAL; 7081fa5cc83SMax Reitz goto fail; 7091fa5cc83SMax Reitz } 7101fa5cc83SMax Reitz 7111fa5cc83SMax Reitz s->overlap_check = 0; 7121fa5cc83SMax Reitz for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) { 7131fa5cc83SMax Reitz /* overlap-check defines a template bitmask, but every flag may be 7141fa5cc83SMax Reitz * overwritten through the associated boolean option */ 7151fa5cc83SMax Reitz s->overlap_check |= 7161fa5cc83SMax Reitz qemu_opt_get_bool(opts, overlap_bool_option_names[i], 7171fa5cc83SMax Reitz overlap_check_template & (1 << i)) << i; 7181fa5cc83SMax Reitz } 7193e355390SMax Reitz 72074c4510aSKevin Wolf qemu_opts_del(opts); 72174c4510aSKevin Wolf 72274c4510aSKevin Wolf if (s->use_lazy_refcounts && s->qcow_version < 3) { 7233ef6c40aSMax Reitz error_setg(errp, "Lazy refcounts require a qcow2 image with at least " 7243ef6c40aSMax Reitz "qemu 1.1 compatibility level"); 72574c4510aSKevin Wolf ret = -EINVAL; 72674c4510aSKevin Wolf goto fail; 72774c4510aSKevin Wolf } 72874c4510aSKevin Wolf 729585f8587Sbellard #ifdef DEBUG_ALLOC 7306cbc3031SPhilipp Hahn { 7316cbc3031SPhilipp Hahn BdrvCheckResult result = {0}; 732b35278f7SStefan Hajnoczi qcow2_check_refcounts(bs, &result, 0); 7336cbc3031SPhilipp Hahn } 734585f8587Sbellard #endif 7356d85a57eSJes Sorensen return ret; 736585f8587Sbellard 737585f8587Sbellard fail: 7386744cbabSKevin Wolf g_free(s->unknown_header_fields); 73975bab85cSKevin Wolf cleanup_unknown_header_ext(bs); 740ed6ccf0fSKevin Wolf qcow2_free_snapshots(bs); 741ed6ccf0fSKevin Wolf qcow2_refcount_close(bs); 7427267c094SAnthony Liguori g_free(s->l1_table); 743cf93980eSMax Reitz /* else pre-write overlap checks in cache_destroy may crash */ 744cf93980eSMax Reitz s->l1_table = NULL; 74529c1a730SKevin Wolf if (s->l2_table_cache) { 74629c1a730SKevin Wolf qcow2_cache_destroy(bs, s->l2_table_cache); 74729c1a730SKevin Wolf } 7487267c094SAnthony Liguori g_free(s->cluster_cache); 749dea43a65SFrediano Ziglio qemu_vfree(s->cluster_data); 7506d85a57eSJes Sorensen return ret; 751585f8587Sbellard } 752585f8587Sbellard 7537c80ab3fSJes Sorensen static int qcow2_set_key(BlockDriverState *bs, const char *key) 754585f8587Sbellard { 755585f8587Sbellard BDRVQcowState *s = bs->opaque; 756585f8587Sbellard uint8_t keybuf[16]; 757585f8587Sbellard int len, i; 758585f8587Sbellard 759585f8587Sbellard memset(keybuf, 0, 16); 760585f8587Sbellard len = strlen(key); 761585f8587Sbellard if (len > 16) 762585f8587Sbellard len = 16; 763585f8587Sbellard /* XXX: we could compress the chars to 7 bits to increase 764585f8587Sbellard entropy */ 765585f8587Sbellard for(i = 0;i < len;i++) { 766585f8587Sbellard keybuf[i] = key[i]; 767585f8587Sbellard } 768585f8587Sbellard s->crypt_method = s->crypt_method_header; 769585f8587Sbellard 770585f8587Sbellard if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) 771585f8587Sbellard return -1; 772585f8587Sbellard if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) 773585f8587Sbellard return -1; 774585f8587Sbellard #if 0 775585f8587Sbellard /* test */ 776585f8587Sbellard { 777585f8587Sbellard uint8_t in[16]; 778585f8587Sbellard uint8_t out[16]; 779585f8587Sbellard uint8_t tmp[16]; 780585f8587Sbellard for(i=0;i<16;i++) 781585f8587Sbellard in[i] = i; 782585f8587Sbellard AES_encrypt(in, tmp, &s->aes_encrypt_key); 783585f8587Sbellard AES_decrypt(tmp, out, &s->aes_decrypt_key); 784585f8587Sbellard for(i = 0; i < 16; i++) 785585f8587Sbellard printf(" %02x", tmp[i]); 786585f8587Sbellard printf("\n"); 787585f8587Sbellard for(i = 0; i < 16; i++) 788585f8587Sbellard printf(" %02x", out[i]); 789585f8587Sbellard printf("\n"); 790585f8587Sbellard } 791585f8587Sbellard #endif 792585f8587Sbellard return 0; 793585f8587Sbellard } 794585f8587Sbellard 79521d82ac9SJeff Cody /* We have nothing to do for QCOW2 reopen, stubs just return 79621d82ac9SJeff Cody * success */ 79721d82ac9SJeff Cody static int qcow2_reopen_prepare(BDRVReopenState *state, 79821d82ac9SJeff Cody BlockReopenQueue *queue, Error **errp) 79921d82ac9SJeff Cody { 80021d82ac9SJeff Cody return 0; 80121d82ac9SJeff Cody } 80221d82ac9SJeff Cody 803b6b8a333SPaolo Bonzini static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs, 804f8a2e5e3SStefan Hajnoczi int64_t sector_num, int nb_sectors, int *pnum) 805585f8587Sbellard { 806f8a2e5e3SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 807585f8587Sbellard uint64_t cluster_offset; 8084bc74be9SPaolo Bonzini int index_in_cluster, ret; 8094bc74be9SPaolo Bonzini int64_t status = 0; 810585f8587Sbellard 811095a9c58Saliguori *pnum = nb_sectors; 812f8a2e5e3SStefan Hajnoczi qemu_co_mutex_lock(&s->lock); 8131c46efaaSKevin Wolf ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); 814f8a2e5e3SStefan Hajnoczi qemu_co_mutex_unlock(&s->lock); 8151c46efaaSKevin Wolf if (ret < 0) { 816d663640cSPaolo Bonzini return ret; 8171c46efaaSKevin Wolf } 818095a9c58Saliguori 8194bc74be9SPaolo Bonzini if (cluster_offset != 0 && ret != QCOW2_CLUSTER_COMPRESSED && 8204bc74be9SPaolo Bonzini !s->crypt_method) { 8214bc74be9SPaolo Bonzini index_in_cluster = sector_num & (s->cluster_sectors - 1); 8224bc74be9SPaolo Bonzini cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); 8234bc74be9SPaolo Bonzini status |= BDRV_BLOCK_OFFSET_VALID | cluster_offset; 8244bc74be9SPaolo Bonzini } 8254bc74be9SPaolo Bonzini if (ret == QCOW2_CLUSTER_ZERO) { 8264bc74be9SPaolo Bonzini status |= BDRV_BLOCK_ZERO; 8274bc74be9SPaolo Bonzini } else if (ret != QCOW2_CLUSTER_UNALLOCATED) { 8284bc74be9SPaolo Bonzini status |= BDRV_BLOCK_DATA; 8294bc74be9SPaolo Bonzini } 8304bc74be9SPaolo Bonzini return status; 831585f8587Sbellard } 832585f8587Sbellard 833a9465922Sbellard /* handle reading after the end of the backing file */ 834bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, 835bd28f835SKevin Wolf int64_t sector_num, int nb_sectors) 836a9465922Sbellard { 837a9465922Sbellard int n1; 838a9465922Sbellard if ((sector_num + nb_sectors) <= bs->total_sectors) 839a9465922Sbellard return nb_sectors; 840a9465922Sbellard if (sector_num >= bs->total_sectors) 841a9465922Sbellard n1 = 0; 842a9465922Sbellard else 843a9465922Sbellard n1 = bs->total_sectors - sector_num; 844bd28f835SKevin Wolf 8453d9b4925SMichael Tokarev qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1)); 846bd28f835SKevin Wolf 847a9465922Sbellard return n1; 848a9465922Sbellard } 849a9465922Sbellard 850a968168cSDong Xu Wang static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, 8513fc48d09SFrediano Ziglio int remaining_sectors, QEMUIOVector *qiov) 8521490791fSaliguori { 853585f8587Sbellard BDRVQcowState *s = bs->opaque; 854a9465922Sbellard int index_in_cluster, n1; 85568d100e9SKevin Wolf int ret; 856faf575c1SFrediano Ziglio int cur_nr_sectors; /* number of sectors in current iteration */ 857c2bdd990SFrediano Ziglio uint64_t cluster_offset = 0; 8583fc48d09SFrediano Ziglio uint64_t bytes_done = 0; 8593fc48d09SFrediano Ziglio QEMUIOVector hd_qiov; 8603fc48d09SFrediano Ziglio uint8_t *cluster_data = NULL; 861585f8587Sbellard 8623fc48d09SFrediano Ziglio qemu_iovec_init(&hd_qiov, qiov->niov); 8633fc48d09SFrediano Ziglio 8643fc48d09SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 8653fc48d09SFrediano Ziglio 8663fc48d09SFrediano Ziglio while (remaining_sectors != 0) { 867585f8587Sbellard 868faf575c1SFrediano Ziglio /* prepare next request */ 8693fc48d09SFrediano Ziglio cur_nr_sectors = remaining_sectors; 870bd28f835SKevin Wolf if (s->crypt_method) { 871faf575c1SFrediano Ziglio cur_nr_sectors = MIN(cur_nr_sectors, 872bd28f835SKevin Wolf QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 873bd28f835SKevin Wolf } 874bd28f835SKevin Wolf 8753fc48d09SFrediano Ziglio ret = qcow2_get_cluster_offset(bs, sector_num << 9, 876c2bdd990SFrediano Ziglio &cur_nr_sectors, &cluster_offset); 8771c46efaaSKevin Wolf if (ret < 0) { 8783fc48d09SFrediano Ziglio goto fail; 8791c46efaaSKevin Wolf } 8801c46efaaSKevin Wolf 8813fc48d09SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 882585f8587Sbellard 8833fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 8841b093c48SMichael Tokarev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 885faf575c1SFrediano Ziglio cur_nr_sectors * 512); 886bd28f835SKevin Wolf 88768d000a3SKevin Wolf switch (ret) { 88868d000a3SKevin Wolf case QCOW2_CLUSTER_UNALLOCATED: 889bd28f835SKevin Wolf 890585f8587Sbellard if (bs->backing_hd) { 891585f8587Sbellard /* read from the base image */ 8923fc48d09SFrediano Ziglio n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov, 8933fc48d09SFrediano Ziglio sector_num, cur_nr_sectors); 894a9465922Sbellard if (n1 > 0) { 89566f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); 89668d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 8973fc48d09SFrediano Ziglio ret = bdrv_co_readv(bs->backing_hd, sector_num, 8983fc48d09SFrediano Ziglio n1, &hd_qiov); 89968d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 90068d100e9SKevin Wolf if (ret < 0) { 9013fc48d09SFrediano Ziglio goto fail; 9023ab4c7e9SKevin Wolf } 9031490791fSaliguori } 904a9465922Sbellard } else { 905585f8587Sbellard /* Note: in this case, no need to wait */ 9063d9b4925SMichael Tokarev qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 9071490791fSaliguori } 90868d000a3SKevin Wolf break; 90968d000a3SKevin Wolf 9106377af48SKevin Wolf case QCOW2_CLUSTER_ZERO: 9113d9b4925SMichael Tokarev qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors); 9126377af48SKevin Wolf break; 9136377af48SKevin Wolf 91468d000a3SKevin Wolf case QCOW2_CLUSTER_COMPRESSED: 915585f8587Sbellard /* add AIO support for compressed blocks ? */ 916c2bdd990SFrediano Ziglio ret = qcow2_decompress_cluster(bs, cluster_offset); 9178af36488SKevin Wolf if (ret < 0) { 9183fc48d09SFrediano Ziglio goto fail; 9198af36488SKevin Wolf } 920bd28f835SKevin Wolf 92103396148SMichael Tokarev qemu_iovec_from_buf(&hd_qiov, 0, 922bd28f835SKevin Wolf s->cluster_cache + index_in_cluster * 512, 923faf575c1SFrediano Ziglio 512 * cur_nr_sectors); 92468d000a3SKevin Wolf break; 92568d000a3SKevin Wolf 92668d000a3SKevin Wolf case QCOW2_CLUSTER_NORMAL: 927c2bdd990SFrediano Ziglio if ((cluster_offset & 511) != 0) { 9283fc48d09SFrediano Ziglio ret = -EIO; 9293fc48d09SFrediano Ziglio goto fail; 930585f8587Sbellard } 931c87c0672Saliguori 932bd28f835SKevin Wolf if (s->crypt_method) { 933bd28f835SKevin Wolf /* 934bd28f835SKevin Wolf * For encrypted images, read everything into a temporary 935bd28f835SKevin Wolf * contiguous buffer on which the AES functions can work. 936bd28f835SKevin Wolf */ 9373fc48d09SFrediano Ziglio if (!cluster_data) { 9383fc48d09SFrediano Ziglio cluster_data = 939dea43a65SFrediano Ziglio qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 940bd28f835SKevin Wolf } 941bd28f835SKevin Wolf 942faf575c1SFrediano Ziglio assert(cur_nr_sectors <= 943bd28f835SKevin Wolf QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); 9443fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 9453fc48d09SFrediano Ziglio qemu_iovec_add(&hd_qiov, cluster_data, 946faf575c1SFrediano Ziglio 512 * cur_nr_sectors); 947bd28f835SKevin Wolf } 948bd28f835SKevin Wolf 94966f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 95068d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 95168d100e9SKevin Wolf ret = bdrv_co_readv(bs->file, 952c2bdd990SFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 9533fc48d09SFrediano Ziglio cur_nr_sectors, &hd_qiov); 95468d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 95568d100e9SKevin Wolf if (ret < 0) { 9563fc48d09SFrediano Ziglio goto fail; 957585f8587Sbellard } 958faf575c1SFrediano Ziglio if (s->crypt_method) { 9593fc48d09SFrediano Ziglio qcow2_encrypt_sectors(s, sector_num, cluster_data, 9603fc48d09SFrediano Ziglio cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key); 96103396148SMichael Tokarev qemu_iovec_from_buf(qiov, bytes_done, 96203396148SMichael Tokarev cluster_data, 512 * cur_nr_sectors); 963171e3d6bSKevin Wolf } 96468d000a3SKevin Wolf break; 96568d000a3SKevin Wolf 96668d000a3SKevin Wolf default: 96768d000a3SKevin Wolf g_assert_not_reached(); 96868d000a3SKevin Wolf ret = -EIO; 96968d000a3SKevin Wolf goto fail; 970faf575c1SFrediano Ziglio } 971faf575c1SFrediano Ziglio 9723fc48d09SFrediano Ziglio remaining_sectors -= cur_nr_sectors; 9733fc48d09SFrediano Ziglio sector_num += cur_nr_sectors; 9743fc48d09SFrediano Ziglio bytes_done += cur_nr_sectors * 512; 9755ebaa27eSFrediano Ziglio } 9763fc48d09SFrediano Ziglio ret = 0; 977f141eafeSaliguori 9783fc48d09SFrediano Ziglio fail: 97968d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 98068d100e9SKevin Wolf 9813fc48d09SFrediano Ziglio qemu_iovec_destroy(&hd_qiov); 982dea43a65SFrediano Ziglio qemu_vfree(cluster_data); 98368d100e9SKevin Wolf 98468d100e9SKevin Wolf return ret; 985585f8587Sbellard } 986585f8587Sbellard 987a968168cSDong Xu Wang static coroutine_fn int qcow2_co_writev(BlockDriverState *bs, 9883fc48d09SFrediano Ziglio int64_t sector_num, 9893fc48d09SFrediano Ziglio int remaining_sectors, 9903fc48d09SFrediano Ziglio QEMUIOVector *qiov) 991585f8587Sbellard { 992585f8587Sbellard BDRVQcowState *s = bs->opaque; 993585f8587Sbellard int index_in_cluster; 994095a9c58Saliguori int n_end; 99568d100e9SKevin Wolf int ret; 996faf575c1SFrediano Ziglio int cur_nr_sectors; /* number of sectors in current iteration */ 997c2bdd990SFrediano Ziglio uint64_t cluster_offset; 9983fc48d09SFrediano Ziglio QEMUIOVector hd_qiov; 9993fc48d09SFrediano Ziglio uint64_t bytes_done = 0; 10003fc48d09SFrediano Ziglio uint8_t *cluster_data = NULL; 10018d2497c3SKevin Wolf QCowL2Meta *l2meta = NULL; 1002c2271403SFrediano Ziglio 10033cce16f4SKevin Wolf trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num, 10043cce16f4SKevin Wolf remaining_sectors); 10053cce16f4SKevin Wolf 10063fc48d09SFrediano Ziglio qemu_iovec_init(&hd_qiov, qiov->niov); 1007585f8587Sbellard 10083fc48d09SFrediano Ziglio s->cluster_cache_offset = -1; /* disable compressed cache */ 10093fc48d09SFrediano Ziglio 10103fc48d09SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 10113fc48d09SFrediano Ziglio 10123fc48d09SFrediano Ziglio while (remaining_sectors != 0) { 10133fc48d09SFrediano Ziglio 1014f50f88b9SKevin Wolf l2meta = NULL; 1015cf5c1a23SKevin Wolf 10163cce16f4SKevin Wolf trace_qcow2_writev_start_part(qemu_coroutine_self()); 10173fc48d09SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 10183fc48d09SFrediano Ziglio n_end = index_in_cluster + remaining_sectors; 1019095a9c58Saliguori if (s->crypt_method && 10205ebaa27eSFrediano Ziglio n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) { 1021095a9c58Saliguori n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; 10225ebaa27eSFrediano Ziglio } 1023095a9c58Saliguori 10243fc48d09SFrediano Ziglio ret = qcow2_alloc_cluster_offset(bs, sector_num << 9, 1025f50f88b9SKevin Wolf index_in_cluster, n_end, &cur_nr_sectors, &cluster_offset, &l2meta); 1026148da7eaSKevin Wolf if (ret < 0) { 10273fc48d09SFrediano Ziglio goto fail; 1028148da7eaSKevin Wolf } 1029148da7eaSKevin Wolf 1030c2bdd990SFrediano Ziglio assert((cluster_offset & 511) == 0); 1031148da7eaSKevin Wolf 10323fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 10331b093c48SMichael Tokarev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, 1034faf575c1SFrediano Ziglio cur_nr_sectors * 512); 10356f5f060bSKevin Wolf 1036585f8587Sbellard if (s->crypt_method) { 10373fc48d09SFrediano Ziglio if (!cluster_data) { 1038dea43a65SFrediano Ziglio cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * 1039095a9c58Saliguori s->cluster_size); 1040585f8587Sbellard } 10416f5f060bSKevin Wolf 10423fc48d09SFrediano Ziglio assert(hd_qiov.size <= 10435ebaa27eSFrediano Ziglio QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); 1044d5e6b161SMichael Tokarev qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size); 10456f5f060bSKevin Wolf 10463fc48d09SFrediano Ziglio qcow2_encrypt_sectors(s, sector_num, cluster_data, 10473fc48d09SFrediano Ziglio cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key); 10486f5f060bSKevin Wolf 10493fc48d09SFrediano Ziglio qemu_iovec_reset(&hd_qiov); 10503fc48d09SFrediano Ziglio qemu_iovec_add(&hd_qiov, cluster_data, 1051faf575c1SFrediano Ziglio cur_nr_sectors * 512); 1052585f8587Sbellard } 10536f5f060bSKevin Wolf 1054231bb267SMax Reitz ret = qcow2_pre_write_overlap_check(bs, 0, 1055cf93980eSMax Reitz cluster_offset + index_in_cluster * BDRV_SECTOR_SIZE, 1056cf93980eSMax Reitz cur_nr_sectors * BDRV_SECTOR_SIZE); 1057cf93980eSMax Reitz if (ret < 0) { 1058cf93980eSMax Reitz goto fail; 1059cf93980eSMax Reitz } 1060cf93980eSMax Reitz 106168d100e9SKevin Wolf qemu_co_mutex_unlock(&s->lock); 106267a7a0ebSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); 10633cce16f4SKevin Wolf trace_qcow2_writev_data(qemu_coroutine_self(), 10643cce16f4SKevin Wolf (cluster_offset >> 9) + index_in_cluster); 106568d100e9SKevin Wolf ret = bdrv_co_writev(bs->file, 1066c2bdd990SFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 10673fc48d09SFrediano Ziglio cur_nr_sectors, &hd_qiov); 106868d100e9SKevin Wolf qemu_co_mutex_lock(&s->lock); 106968d100e9SKevin Wolf if (ret < 0) { 10703fc48d09SFrediano Ziglio goto fail; 1071171e3d6bSKevin Wolf } 1072f141eafeSaliguori 107388c6588cSKevin Wolf while (l2meta != NULL) { 107488c6588cSKevin Wolf QCowL2Meta *next; 107588c6588cSKevin Wolf 1076cf5c1a23SKevin Wolf ret = qcow2_alloc_cluster_link_l2(bs, l2meta); 1077faf575c1SFrediano Ziglio if (ret < 0) { 10783fc48d09SFrediano Ziglio goto fail; 1079faf575c1SFrediano Ziglio } 1080faf575c1SFrediano Ziglio 10814e95314eSKevin Wolf /* Take the request off the list of running requests */ 10824e95314eSKevin Wolf if (l2meta->nb_clusters != 0) { 10834e95314eSKevin Wolf QLIST_REMOVE(l2meta, next_in_flight); 10844e95314eSKevin Wolf } 10854e95314eSKevin Wolf 10864e95314eSKevin Wolf qemu_co_queue_restart_all(&l2meta->dependent_requests); 10874e95314eSKevin Wolf 108888c6588cSKevin Wolf next = l2meta->next; 1089cf5c1a23SKevin Wolf g_free(l2meta); 109088c6588cSKevin Wolf l2meta = next; 1091f50f88b9SKevin Wolf } 10920fa9131aSKevin Wolf 10933fc48d09SFrediano Ziglio remaining_sectors -= cur_nr_sectors; 10943fc48d09SFrediano Ziglio sector_num += cur_nr_sectors; 10953fc48d09SFrediano Ziglio bytes_done += cur_nr_sectors * 512; 10963cce16f4SKevin Wolf trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors); 10975ebaa27eSFrediano Ziglio } 10983fc48d09SFrediano Ziglio ret = 0; 1099faf575c1SFrediano Ziglio 11003fc48d09SFrediano Ziglio fail: 11014e95314eSKevin Wolf qemu_co_mutex_unlock(&s->lock); 11024e95314eSKevin Wolf 110388c6588cSKevin Wolf while (l2meta != NULL) { 110488c6588cSKevin Wolf QCowL2Meta *next; 110588c6588cSKevin Wolf 11064e95314eSKevin Wolf if (l2meta->nb_clusters != 0) { 11074e95314eSKevin Wolf QLIST_REMOVE(l2meta, next_in_flight); 11084e95314eSKevin Wolf } 11094e95314eSKevin Wolf qemu_co_queue_restart_all(&l2meta->dependent_requests); 111088c6588cSKevin Wolf 111188c6588cSKevin Wolf next = l2meta->next; 1112cf5c1a23SKevin Wolf g_free(l2meta); 111388c6588cSKevin Wolf l2meta = next; 1114cf5c1a23SKevin Wolf } 11150fa9131aSKevin Wolf 11163fc48d09SFrediano Ziglio qemu_iovec_destroy(&hd_qiov); 1117dea43a65SFrediano Ziglio qemu_vfree(cluster_data); 11183cce16f4SKevin Wolf trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); 111942496d62SKevin Wolf 112068d100e9SKevin Wolf return ret; 1121585f8587Sbellard } 1122585f8587Sbellard 11237c80ab3fSJes Sorensen static void qcow2_close(BlockDriverState *bs) 1124585f8587Sbellard { 1125585f8587Sbellard BDRVQcowState *s = bs->opaque; 11267267c094SAnthony Liguori g_free(s->l1_table); 1127cf93980eSMax Reitz /* else pre-write overlap checks in cache_destroy may crash */ 1128cf93980eSMax Reitz s->l1_table = NULL; 112929c1a730SKevin Wolf 113029c1a730SKevin Wolf qcow2_cache_flush(bs, s->l2_table_cache); 113129c1a730SKevin Wolf qcow2_cache_flush(bs, s->refcount_block_cache); 113229c1a730SKevin Wolf 1133c61d0004SStefan Hajnoczi qcow2_mark_clean(bs); 1134c61d0004SStefan Hajnoczi 113529c1a730SKevin Wolf qcow2_cache_destroy(bs, s->l2_table_cache); 113629c1a730SKevin Wolf qcow2_cache_destroy(bs, s->refcount_block_cache); 113729c1a730SKevin Wolf 11386744cbabSKevin Wolf g_free(s->unknown_header_fields); 113975bab85cSKevin Wolf cleanup_unknown_header_ext(bs); 11406744cbabSKevin Wolf 11417267c094SAnthony Liguori g_free(s->cluster_cache); 1142dea43a65SFrediano Ziglio qemu_vfree(s->cluster_data); 1143ed6ccf0fSKevin Wolf qcow2_refcount_close(bs); 114428c1202bSLi Zhi Hui qcow2_free_snapshots(bs); 1145585f8587Sbellard } 1146585f8587Sbellard 114706d9260fSAnthony Liguori static void qcow2_invalidate_cache(BlockDriverState *bs) 114806d9260fSAnthony Liguori { 114906d9260fSAnthony Liguori BDRVQcowState *s = bs->opaque; 115006d9260fSAnthony Liguori int flags = s->flags; 115106d9260fSAnthony Liguori AES_KEY aes_encrypt_key; 115206d9260fSAnthony Liguori AES_KEY aes_decrypt_key; 115306d9260fSAnthony Liguori uint32_t crypt_method = 0; 1154acdfb480SKevin Wolf QDict *options; 115506d9260fSAnthony Liguori 115606d9260fSAnthony Liguori /* 115706d9260fSAnthony Liguori * Backing files are read-only which makes all of their metadata immutable, 115806d9260fSAnthony Liguori * that means we don't have to worry about reopening them here. 115906d9260fSAnthony Liguori */ 116006d9260fSAnthony Liguori 116106d9260fSAnthony Liguori if (s->crypt_method) { 116206d9260fSAnthony Liguori crypt_method = s->crypt_method; 116306d9260fSAnthony Liguori memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key)); 116406d9260fSAnthony Liguori memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key)); 116506d9260fSAnthony Liguori } 116606d9260fSAnthony Liguori 116706d9260fSAnthony Liguori qcow2_close(bs); 116806d9260fSAnthony Liguori 1169acdfb480SKevin Wolf options = qdict_new(); 1170acdfb480SKevin Wolf qdict_put(options, QCOW2_OPT_LAZY_REFCOUNTS, 1171acdfb480SKevin Wolf qbool_from_int(s->use_lazy_refcounts)); 1172acdfb480SKevin Wolf 117306d9260fSAnthony Liguori memset(s, 0, sizeof(BDRVQcowState)); 1174015a1036SMax Reitz qcow2_open(bs, options, flags, NULL); 1175acdfb480SKevin Wolf 1176acdfb480SKevin Wolf QDECREF(options); 117706d9260fSAnthony Liguori 117806d9260fSAnthony Liguori if (crypt_method) { 117906d9260fSAnthony Liguori s->crypt_method = crypt_method; 118006d9260fSAnthony Liguori memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key)); 118106d9260fSAnthony Liguori memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key)); 118206d9260fSAnthony Liguori } 118306d9260fSAnthony Liguori } 118406d9260fSAnthony Liguori 1185e24e49e6SKevin Wolf static size_t header_ext_add(char *buf, uint32_t magic, const void *s, 1186e24e49e6SKevin Wolf size_t len, size_t buflen) 1187756e6736SKevin Wolf { 1188e24e49e6SKevin Wolf QCowExtension *ext_backing_fmt = (QCowExtension*) buf; 1189e24e49e6SKevin Wolf size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7); 1190756e6736SKevin Wolf 1191e24e49e6SKevin Wolf if (buflen < ext_len) { 1192756e6736SKevin Wolf return -ENOSPC; 1193756e6736SKevin Wolf } 1194756e6736SKevin Wolf 1195e24e49e6SKevin Wolf *ext_backing_fmt = (QCowExtension) { 1196e24e49e6SKevin Wolf .magic = cpu_to_be32(magic), 1197e24e49e6SKevin Wolf .len = cpu_to_be32(len), 1198e24e49e6SKevin Wolf }; 1199e24e49e6SKevin Wolf memcpy(buf + sizeof(QCowExtension), s, len); 1200756e6736SKevin Wolf 1201e24e49e6SKevin Wolf return ext_len; 1202756e6736SKevin Wolf } 1203756e6736SKevin Wolf 1204e24e49e6SKevin Wolf /* 1205e24e49e6SKevin Wolf * Updates the qcow2 header, including the variable length parts of it, i.e. 1206e24e49e6SKevin Wolf * the backing file name and all extensions. qcow2 was not designed to allow 1207e24e49e6SKevin Wolf * such changes, so if we run out of space (we can only use the first cluster) 1208e24e49e6SKevin Wolf * this function may fail. 1209e24e49e6SKevin Wolf * 1210e24e49e6SKevin Wolf * Returns 0 on success, -errno in error cases. 1211e24e49e6SKevin Wolf */ 1212e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs) 1213e24e49e6SKevin Wolf { 1214e24e49e6SKevin Wolf BDRVQcowState *s = bs->opaque; 1215e24e49e6SKevin Wolf QCowHeader *header; 1216e24e49e6SKevin Wolf char *buf; 1217e24e49e6SKevin Wolf size_t buflen = s->cluster_size; 1218e24e49e6SKevin Wolf int ret; 1219e24e49e6SKevin Wolf uint64_t total_size; 1220e24e49e6SKevin Wolf uint32_t refcount_table_clusters; 12216744cbabSKevin Wolf size_t header_length; 122275bab85cSKevin Wolf Qcow2UnknownHeaderExtension *uext; 1223e24e49e6SKevin Wolf 1224e24e49e6SKevin Wolf buf = qemu_blockalign(bs, buflen); 1225e24e49e6SKevin Wolf 1226e24e49e6SKevin Wolf /* Header structure */ 1227e24e49e6SKevin Wolf header = (QCowHeader*) buf; 1228e24e49e6SKevin Wolf 1229e24e49e6SKevin Wolf if (buflen < sizeof(*header)) { 1230e24e49e6SKevin Wolf ret = -ENOSPC; 1231e24e49e6SKevin Wolf goto fail; 1232756e6736SKevin Wolf } 1233756e6736SKevin Wolf 12346744cbabSKevin Wolf header_length = sizeof(*header) + s->unknown_header_fields_size; 1235e24e49e6SKevin Wolf total_size = bs->total_sectors * BDRV_SECTOR_SIZE; 1236e24e49e6SKevin Wolf refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3); 1237e24e49e6SKevin Wolf 1238e24e49e6SKevin Wolf *header = (QCowHeader) { 12396744cbabSKevin Wolf /* Version 2 fields */ 1240e24e49e6SKevin Wolf .magic = cpu_to_be32(QCOW_MAGIC), 12416744cbabSKevin Wolf .version = cpu_to_be32(s->qcow_version), 1242e24e49e6SKevin Wolf .backing_file_offset = 0, 1243e24e49e6SKevin Wolf .backing_file_size = 0, 1244e24e49e6SKevin Wolf .cluster_bits = cpu_to_be32(s->cluster_bits), 1245e24e49e6SKevin Wolf .size = cpu_to_be64(total_size), 1246e24e49e6SKevin Wolf .crypt_method = cpu_to_be32(s->crypt_method_header), 1247e24e49e6SKevin Wolf .l1_size = cpu_to_be32(s->l1_size), 1248e24e49e6SKevin Wolf .l1_table_offset = cpu_to_be64(s->l1_table_offset), 1249e24e49e6SKevin Wolf .refcount_table_offset = cpu_to_be64(s->refcount_table_offset), 1250e24e49e6SKevin Wolf .refcount_table_clusters = cpu_to_be32(refcount_table_clusters), 1251e24e49e6SKevin Wolf .nb_snapshots = cpu_to_be32(s->nb_snapshots), 1252e24e49e6SKevin Wolf .snapshots_offset = cpu_to_be64(s->snapshots_offset), 12536744cbabSKevin Wolf 12546744cbabSKevin Wolf /* Version 3 fields */ 12556744cbabSKevin Wolf .incompatible_features = cpu_to_be64(s->incompatible_features), 12566744cbabSKevin Wolf .compatible_features = cpu_to_be64(s->compatible_features), 12576744cbabSKevin Wolf .autoclear_features = cpu_to_be64(s->autoclear_features), 1258b6481f37SMax Reitz .refcount_order = cpu_to_be32(s->refcount_order), 12596744cbabSKevin Wolf .header_length = cpu_to_be32(header_length), 1260e24e49e6SKevin Wolf }; 1261e24e49e6SKevin Wolf 12626744cbabSKevin Wolf /* For older versions, write a shorter header */ 12636744cbabSKevin Wolf switch (s->qcow_version) { 12646744cbabSKevin Wolf case 2: 12656744cbabSKevin Wolf ret = offsetof(QCowHeader, incompatible_features); 12666744cbabSKevin Wolf break; 12676744cbabSKevin Wolf case 3: 12686744cbabSKevin Wolf ret = sizeof(*header); 12696744cbabSKevin Wolf break; 12706744cbabSKevin Wolf default: 1271b6c14762SJim Meyering ret = -EINVAL; 1272b6c14762SJim Meyering goto fail; 12736744cbabSKevin Wolf } 12746744cbabSKevin Wolf 12756744cbabSKevin Wolf buf += ret; 12766744cbabSKevin Wolf buflen -= ret; 12776744cbabSKevin Wolf memset(buf, 0, buflen); 12786744cbabSKevin Wolf 12796744cbabSKevin Wolf /* Preserve any unknown field in the header */ 12806744cbabSKevin Wolf if (s->unknown_header_fields_size) { 12816744cbabSKevin Wolf if (buflen < s->unknown_header_fields_size) { 12826744cbabSKevin Wolf ret = -ENOSPC; 12836744cbabSKevin Wolf goto fail; 12846744cbabSKevin Wolf } 12856744cbabSKevin Wolf 12866744cbabSKevin Wolf memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size); 12876744cbabSKevin Wolf buf += s->unknown_header_fields_size; 12886744cbabSKevin Wolf buflen -= s->unknown_header_fields_size; 12896744cbabSKevin Wolf } 1290e24e49e6SKevin Wolf 1291e24e49e6SKevin Wolf /* Backing file format header extension */ 1292e24e49e6SKevin Wolf if (*bs->backing_format) { 1293e24e49e6SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT, 1294e24e49e6SKevin Wolf bs->backing_format, strlen(bs->backing_format), 1295e24e49e6SKevin Wolf buflen); 1296756e6736SKevin Wolf if (ret < 0) { 1297756e6736SKevin Wolf goto fail; 1298756e6736SKevin Wolf } 1299756e6736SKevin Wolf 1300e24e49e6SKevin Wolf buf += ret; 1301e24e49e6SKevin Wolf buflen -= ret; 1302e24e49e6SKevin Wolf } 1303756e6736SKevin Wolf 1304cfcc4c62SKevin Wolf /* Feature table */ 1305cfcc4c62SKevin Wolf Qcow2Feature features[] = { 1306c61d0004SStefan Hajnoczi { 1307c61d0004SStefan Hajnoczi .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, 1308c61d0004SStefan Hajnoczi .bit = QCOW2_INCOMPAT_DIRTY_BITNR, 1309c61d0004SStefan Hajnoczi .name = "dirty bit", 1310c61d0004SStefan Hajnoczi }, 1311bfe8043eSStefan Hajnoczi { 131269c98726SMax Reitz .type = QCOW2_FEAT_TYPE_INCOMPATIBLE, 131369c98726SMax Reitz .bit = QCOW2_INCOMPAT_CORRUPT_BITNR, 131469c98726SMax Reitz .name = "corrupt bit", 131569c98726SMax Reitz }, 131669c98726SMax Reitz { 1317bfe8043eSStefan Hajnoczi .type = QCOW2_FEAT_TYPE_COMPATIBLE, 1318bfe8043eSStefan Hajnoczi .bit = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR, 1319bfe8043eSStefan Hajnoczi .name = "lazy refcounts", 1320bfe8043eSStefan Hajnoczi }, 1321cfcc4c62SKevin Wolf }; 1322cfcc4c62SKevin Wolf 1323cfcc4c62SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE, 1324cfcc4c62SKevin Wolf features, sizeof(features), buflen); 1325cfcc4c62SKevin Wolf if (ret < 0) { 1326cfcc4c62SKevin Wolf goto fail; 1327cfcc4c62SKevin Wolf } 1328cfcc4c62SKevin Wolf buf += ret; 1329cfcc4c62SKevin Wolf buflen -= ret; 1330cfcc4c62SKevin Wolf 133175bab85cSKevin Wolf /* Keep unknown header extensions */ 133275bab85cSKevin Wolf QLIST_FOREACH(uext, &s->unknown_header_ext, next) { 133375bab85cSKevin Wolf ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen); 133475bab85cSKevin Wolf if (ret < 0) { 133575bab85cSKevin Wolf goto fail; 133675bab85cSKevin Wolf } 133775bab85cSKevin Wolf 133875bab85cSKevin Wolf buf += ret; 133975bab85cSKevin Wolf buflen -= ret; 134075bab85cSKevin Wolf } 134175bab85cSKevin Wolf 1342e24e49e6SKevin Wolf /* End of header extensions */ 1343e24e49e6SKevin Wolf ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen); 1344756e6736SKevin Wolf if (ret < 0) { 1345756e6736SKevin Wolf goto fail; 1346756e6736SKevin Wolf } 1347756e6736SKevin Wolf 1348e24e49e6SKevin Wolf buf += ret; 1349e24e49e6SKevin Wolf buflen -= ret; 1350e24e49e6SKevin Wolf 1351e24e49e6SKevin Wolf /* Backing file name */ 1352e24e49e6SKevin Wolf if (*bs->backing_file) { 1353e24e49e6SKevin Wolf size_t backing_file_len = strlen(bs->backing_file); 1354e24e49e6SKevin Wolf 1355e24e49e6SKevin Wolf if (buflen < backing_file_len) { 1356e24e49e6SKevin Wolf ret = -ENOSPC; 1357e24e49e6SKevin Wolf goto fail; 1358e24e49e6SKevin Wolf } 1359e24e49e6SKevin Wolf 136000ea1881SJim Meyering /* Using strncpy is ok here, since buf is not NUL-terminated. */ 1361e24e49e6SKevin Wolf strncpy(buf, bs->backing_file, buflen); 1362e24e49e6SKevin Wolf 1363e24e49e6SKevin Wolf header->backing_file_offset = cpu_to_be64(buf - ((char*) header)); 1364e24e49e6SKevin Wolf header->backing_file_size = cpu_to_be32(backing_file_len); 1365e24e49e6SKevin Wolf } 1366e24e49e6SKevin Wolf 1367e24e49e6SKevin Wolf /* Write the new header */ 1368e24e49e6SKevin Wolf ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size); 1369756e6736SKevin Wolf if (ret < 0) { 1370756e6736SKevin Wolf goto fail; 1371756e6736SKevin Wolf } 1372756e6736SKevin Wolf 1373756e6736SKevin Wolf ret = 0; 1374756e6736SKevin Wolf fail: 1375e24e49e6SKevin Wolf qemu_vfree(header); 1376756e6736SKevin Wolf return ret; 1377756e6736SKevin Wolf } 1378756e6736SKevin Wolf 1379756e6736SKevin Wolf static int qcow2_change_backing_file(BlockDriverState *bs, 1380756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1381756e6736SKevin Wolf { 1382e24e49e6SKevin Wolf pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1383e24e49e6SKevin Wolf pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1384e24e49e6SKevin Wolf 1385e24e49e6SKevin Wolf return qcow2_update_header(bs); 1386756e6736SKevin Wolf } 1387756e6736SKevin Wolf 1388a35e1c17SKevin Wolf static int preallocate(BlockDriverState *bs) 1389a35e1c17SKevin Wolf { 1390a35e1c17SKevin Wolf uint64_t nb_sectors; 1391a35e1c17SKevin Wolf uint64_t offset; 1392060bee89SKevin Wolf uint64_t host_offset = 0; 1393a35e1c17SKevin Wolf int num; 1394148da7eaSKevin Wolf int ret; 1395f50f88b9SKevin Wolf QCowL2Meta *meta; 1396a35e1c17SKevin Wolf 1397a35e1c17SKevin Wolf nb_sectors = bdrv_getlength(bs) >> 9; 1398a35e1c17SKevin Wolf offset = 0; 1399a35e1c17SKevin Wolf 1400a35e1c17SKevin Wolf while (nb_sectors) { 1401a35e1c17SKevin Wolf num = MIN(nb_sectors, INT_MAX >> 9); 1402060bee89SKevin Wolf ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, 1403060bee89SKevin Wolf &host_offset, &meta); 1404148da7eaSKevin Wolf if (ret < 0) { 140519dbcbf7SKevin Wolf return ret; 1406a35e1c17SKevin Wolf } 1407a35e1c17SKevin Wolf 1408f50f88b9SKevin Wolf ret = qcow2_alloc_cluster_link_l2(bs, meta); 140919dbcbf7SKevin Wolf if (ret < 0) { 14106cfcb9b8SKevin Wolf qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, 14116cfcb9b8SKevin Wolf QCOW2_DISCARD_NEVER); 141219dbcbf7SKevin Wolf return ret; 1413a35e1c17SKevin Wolf } 1414a35e1c17SKevin Wolf 1415f214978aSKevin Wolf /* There are no dependent requests, but we need to remove our request 1416f214978aSKevin Wolf * from the list of in-flight requests */ 1417f50f88b9SKevin Wolf if (meta != NULL) { 14184e95314eSKevin Wolf QLIST_REMOVE(meta, next_in_flight); 1419f50f88b9SKevin Wolf } 1420f214978aSKevin Wolf 1421a35e1c17SKevin Wolf /* TODO Preallocate data if requested */ 1422a35e1c17SKevin Wolf 1423a35e1c17SKevin Wolf nb_sectors -= num; 1424a35e1c17SKevin Wolf offset += num << 9; 1425a35e1c17SKevin Wolf } 1426a35e1c17SKevin Wolf 1427a35e1c17SKevin Wolf /* 1428a35e1c17SKevin Wolf * It is expected that the image file is large enough to actually contain 1429a35e1c17SKevin Wolf * all of the allocated clusters (otherwise we get failing reads after 1430a35e1c17SKevin Wolf * EOF). Extend the image to the last allocated sector. 1431a35e1c17SKevin Wolf */ 1432060bee89SKevin Wolf if (host_offset != 0) { 1433ea80b906SKevin Wolf uint8_t buf[512]; 1434ea80b906SKevin Wolf memset(buf, 0, 512); 1435060bee89SKevin Wolf ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1); 143619dbcbf7SKevin Wolf if (ret < 0) { 143719dbcbf7SKevin Wolf return ret; 143819dbcbf7SKevin Wolf } 1439a35e1c17SKevin Wolf } 1440a35e1c17SKevin Wolf 1441a35e1c17SKevin Wolf return 0; 1442a35e1c17SKevin Wolf } 1443a35e1c17SKevin Wolf 14447c80ab3fSJes Sorensen static int qcow2_create2(const char *filename, int64_t total_size, 1445a9420734SKevin Wolf const char *backing_file, const char *backing_format, 1446a9420734SKevin Wolf int flags, size_t cluster_size, int prealloc, 14473ef6c40aSMax Reitz QEMUOptionParameter *options, int version, 14483ef6c40aSMax Reitz Error **errp) 1449a9420734SKevin Wolf { 14509b2260cbSDong Xu Wang /* Calculate cluster_bits */ 1451a9420734SKevin Wolf int cluster_bits; 1452a9420734SKevin Wolf cluster_bits = ffs(cluster_size) - 1; 1453a9420734SKevin Wolf if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || 1454a9420734SKevin Wolf (1 << cluster_bits) != cluster_size) 1455a9420734SKevin Wolf { 14563ef6c40aSMax Reitz error_setg(errp, "Cluster size must be a power of two between %d and " 14573ef6c40aSMax Reitz "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); 1458a9420734SKevin Wolf return -EINVAL; 1459a9420734SKevin Wolf } 1460a9420734SKevin Wolf 1461a9420734SKevin Wolf /* 1462a9420734SKevin Wolf * Open the image file and write a minimal qcow2 header. 1463a9420734SKevin Wolf * 1464a9420734SKevin Wolf * We keep things simple and start with a zero-sized image. We also 1465a9420734SKevin Wolf * do without refcount blocks or a L1 table for now. We'll fix the 1466a9420734SKevin Wolf * inconsistency later. 1467a9420734SKevin Wolf * 1468a9420734SKevin Wolf * We do need a refcount table because growing the refcount table means 1469a9420734SKevin Wolf * allocating two new refcount blocks - the seconds of which would be at 1470a9420734SKevin Wolf * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file 1471a9420734SKevin Wolf * size for any qcow2 image. 1472a9420734SKevin Wolf */ 1473a9420734SKevin Wolf BlockDriverState* bs; 1474a9420734SKevin Wolf QCowHeader header; 1475a9420734SKevin Wolf uint8_t* refcount_table; 14763ef6c40aSMax Reitz Error *local_err = NULL; 1477a9420734SKevin Wolf int ret; 1478a9420734SKevin Wolf 14793ef6c40aSMax Reitz ret = bdrv_create_file(filename, options, &local_err); 1480a9420734SKevin Wolf if (ret < 0) { 14813ef6c40aSMax Reitz error_propagate(errp, local_err); 1482a9420734SKevin Wolf return ret; 1483a9420734SKevin Wolf } 1484a9420734SKevin Wolf 14853ef6c40aSMax Reitz ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err); 1486a9420734SKevin Wolf if (ret < 0) { 14873ef6c40aSMax Reitz error_propagate(errp, local_err); 1488a9420734SKevin Wolf return ret; 1489a9420734SKevin Wolf } 1490a9420734SKevin Wolf 1491a9420734SKevin Wolf /* Write the header */ 1492a9420734SKevin Wolf memset(&header, 0, sizeof(header)); 1493a9420734SKevin Wolf header.magic = cpu_to_be32(QCOW_MAGIC); 14946744cbabSKevin Wolf header.version = cpu_to_be32(version); 1495a9420734SKevin Wolf header.cluster_bits = cpu_to_be32(cluster_bits); 1496a9420734SKevin Wolf header.size = cpu_to_be64(0); 1497a9420734SKevin Wolf header.l1_table_offset = cpu_to_be64(0); 1498a9420734SKevin Wolf header.l1_size = cpu_to_be32(0); 1499a9420734SKevin Wolf header.refcount_table_offset = cpu_to_be64(cluster_size); 1500a9420734SKevin Wolf header.refcount_table_clusters = cpu_to_be32(1); 15016744cbabSKevin Wolf header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT); 15026744cbabSKevin Wolf header.header_length = cpu_to_be32(sizeof(header)); 1503a9420734SKevin Wolf 1504a9420734SKevin Wolf if (flags & BLOCK_FLAG_ENCRYPT) { 1505a9420734SKevin Wolf header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); 1506a9420734SKevin Wolf } else { 1507a9420734SKevin Wolf header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); 1508a9420734SKevin Wolf } 1509a9420734SKevin Wolf 1510bfe8043eSStefan Hajnoczi if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { 1511bfe8043eSStefan Hajnoczi header.compatible_features |= 1512bfe8043eSStefan Hajnoczi cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); 1513bfe8043eSStefan Hajnoczi } 1514bfe8043eSStefan Hajnoczi 1515a9420734SKevin Wolf ret = bdrv_pwrite(bs, 0, &header, sizeof(header)); 1516a9420734SKevin Wolf if (ret < 0) { 15173ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not write qcow2 header"); 1518a9420734SKevin Wolf goto out; 1519a9420734SKevin Wolf } 1520a9420734SKevin Wolf 1521a9420734SKevin Wolf /* Write an empty refcount table */ 15227267c094SAnthony Liguori refcount_table = g_malloc0(cluster_size); 1523a9420734SKevin Wolf ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size); 15247267c094SAnthony Liguori g_free(refcount_table); 1525a9420734SKevin Wolf 1526a9420734SKevin Wolf if (ret < 0) { 15273ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not write refcount table"); 1528a9420734SKevin Wolf goto out; 1529a9420734SKevin Wolf } 1530a9420734SKevin Wolf 1531a9420734SKevin Wolf bdrv_close(bs); 1532a9420734SKevin Wolf 1533a9420734SKevin Wolf /* 1534a9420734SKevin Wolf * And now open the image and make it consistent first (i.e. increase the 1535a9420734SKevin Wolf * refcount of the cluster that is occupied by the header and the refcount 1536a9420734SKevin Wolf * table) 1537a9420734SKevin Wolf */ 1538a9420734SKevin Wolf BlockDriver* drv = bdrv_find_format("qcow2"); 1539a9420734SKevin Wolf assert(drv != NULL); 1540de9c0cecSKevin Wolf ret = bdrv_open(bs, filename, NULL, 15413ef6c40aSMax Reitz BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, &local_err); 1542a9420734SKevin Wolf if (ret < 0) { 15433ef6c40aSMax Reitz error_propagate(errp, local_err); 1544a9420734SKevin Wolf goto out; 1545a9420734SKevin Wolf } 1546a9420734SKevin Wolf 1547a9420734SKevin Wolf ret = qcow2_alloc_clusters(bs, 2 * cluster_size); 1548a9420734SKevin Wolf if (ret < 0) { 15493ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " 15503ef6c40aSMax Reitz "header and refcount table"); 1551a9420734SKevin Wolf goto out; 1552a9420734SKevin Wolf 1553a9420734SKevin Wolf } else if (ret != 0) { 1554a9420734SKevin Wolf error_report("Huh, first cluster in empty image is already in use?"); 1555a9420734SKevin Wolf abort(); 1556a9420734SKevin Wolf } 1557a9420734SKevin Wolf 1558a9420734SKevin Wolf /* Okay, now that we have a valid image, let's give it the right size */ 1559a9420734SKevin Wolf ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE); 1560a9420734SKevin Wolf if (ret < 0) { 15613ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not resize image"); 1562a9420734SKevin Wolf goto out; 1563a9420734SKevin Wolf } 1564a9420734SKevin Wolf 1565a9420734SKevin Wolf /* Want a backing file? There you go.*/ 1566a9420734SKevin Wolf if (backing_file) { 1567a9420734SKevin Wolf ret = bdrv_change_backing_file(bs, backing_file, backing_format); 1568a9420734SKevin Wolf if (ret < 0) { 15693ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not assign backing file '%s' " 15703ef6c40aSMax Reitz "with format '%s'", backing_file, backing_format); 1571a9420734SKevin Wolf goto out; 1572a9420734SKevin Wolf } 1573a9420734SKevin Wolf } 1574a9420734SKevin Wolf 1575a9420734SKevin Wolf /* And if we're supposed to preallocate metadata, do that now */ 1576a9420734SKevin Wolf if (prealloc) { 157715552c4aSZhi Yong Wu BDRVQcowState *s = bs->opaque; 157815552c4aSZhi Yong Wu qemu_co_mutex_lock(&s->lock); 1579a9420734SKevin Wolf ret = preallocate(bs); 158015552c4aSZhi Yong Wu qemu_co_mutex_unlock(&s->lock); 1581a9420734SKevin Wolf if (ret < 0) { 15823ef6c40aSMax Reitz error_setg_errno(errp, -ret, "Could not preallocate metadata"); 1583a9420734SKevin Wolf goto out; 1584a9420734SKevin Wolf } 1585a9420734SKevin Wolf } 1586a9420734SKevin Wolf 1587ba2ab2f2SMax Reitz bdrv_close(bs); 1588ba2ab2f2SMax Reitz 1589ba2ab2f2SMax Reitz /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */ 1590ba2ab2f2SMax Reitz ret = bdrv_open(bs, filename, NULL, 1591*c9fbb99dSKevin Wolf BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, 1592*c9fbb99dSKevin Wolf drv, &local_err); 1593ba2ab2f2SMax Reitz if (error_is_set(&local_err)) { 1594ba2ab2f2SMax Reitz error_propagate(errp, local_err); 1595ba2ab2f2SMax Reitz goto out; 1596ba2ab2f2SMax Reitz } 1597ba2ab2f2SMax Reitz 1598a9420734SKevin Wolf ret = 0; 1599a9420734SKevin Wolf out: 16004f6fd349SFam Zheng bdrv_unref(bs); 1601a9420734SKevin Wolf return ret; 1602a9420734SKevin Wolf } 1603de5f3f40SKevin Wolf 1604d5124c00SMax Reitz static int qcow2_create(const char *filename, QEMUOptionParameter *options, 1605d5124c00SMax Reitz Error **errp) 1606de5f3f40SKevin Wolf { 1607de5f3f40SKevin Wolf const char *backing_file = NULL; 1608de5f3f40SKevin Wolf const char *backing_fmt = NULL; 1609de5f3f40SKevin Wolf uint64_t sectors = 0; 1610de5f3f40SKevin Wolf int flags = 0; 161199cce9faSKevin Wolf size_t cluster_size = DEFAULT_CLUSTER_SIZE; 1612de5f3f40SKevin Wolf int prealloc = 0; 16138ad1898cSKevin Wolf int version = 3; 16143ef6c40aSMax Reitz Error *local_err = NULL; 16153ef6c40aSMax Reitz int ret; 1616de5f3f40SKevin Wolf 1617de5f3f40SKevin Wolf /* Read out options */ 1618de5f3f40SKevin Wolf while (options && options->name) { 1619de5f3f40SKevin Wolf if (!strcmp(options->name, BLOCK_OPT_SIZE)) { 1620de5f3f40SKevin Wolf sectors = options->value.n / 512; 1621de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { 1622de5f3f40SKevin Wolf backing_file = options->value.s; 1623de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { 1624de5f3f40SKevin Wolf backing_fmt = options->value.s; 1625de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { 1626de5f3f40SKevin Wolf flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; 1627de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { 1628de5f3f40SKevin Wolf if (options->value.n) { 1629de5f3f40SKevin Wolf cluster_size = options->value.n; 1630de5f3f40SKevin Wolf } 1631de5f3f40SKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { 1632de5f3f40SKevin Wolf if (!options->value.s || !strcmp(options->value.s, "off")) { 1633de5f3f40SKevin Wolf prealloc = 0; 1634de5f3f40SKevin Wolf } else if (!strcmp(options->value.s, "metadata")) { 1635de5f3f40SKevin Wolf prealloc = 1; 1636de5f3f40SKevin Wolf } else { 16373ef6c40aSMax Reitz error_setg(errp, "Invalid preallocation mode: '%s'", 1638de5f3f40SKevin Wolf options->value.s); 1639de5f3f40SKevin Wolf return -EINVAL; 1640de5f3f40SKevin Wolf } 16416744cbabSKevin Wolf } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) { 16429117b477SKevin Wolf if (!options->value.s) { 16439117b477SKevin Wolf /* keep the default */ 16449117b477SKevin Wolf } else if (!strcmp(options->value.s, "0.10")) { 16456744cbabSKevin Wolf version = 2; 16466744cbabSKevin Wolf } else if (!strcmp(options->value.s, "1.1")) { 16476744cbabSKevin Wolf version = 3; 16486744cbabSKevin Wolf } else { 16493ef6c40aSMax Reitz error_setg(errp, "Invalid compatibility level: '%s'", 16506744cbabSKevin Wolf options->value.s); 16516744cbabSKevin Wolf return -EINVAL; 16526744cbabSKevin Wolf } 1653bfe8043eSStefan Hajnoczi } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) { 1654bfe8043eSStefan Hajnoczi flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0; 1655de5f3f40SKevin Wolf } 1656de5f3f40SKevin Wolf options++; 1657de5f3f40SKevin Wolf } 1658de5f3f40SKevin Wolf 1659de5f3f40SKevin Wolf if (backing_file && prealloc) { 16603ef6c40aSMax Reitz error_setg(errp, "Backing file and preallocation cannot be used at " 16613ef6c40aSMax Reitz "the same time"); 1662de5f3f40SKevin Wolf return -EINVAL; 1663de5f3f40SKevin Wolf } 1664de5f3f40SKevin Wolf 1665bfe8043eSStefan Hajnoczi if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) { 16663ef6c40aSMax Reitz error_setg(errp, "Lazy refcounts only supported with compatibility " 16673ef6c40aSMax Reitz "level 1.1 and above (use compat=1.1 or greater)"); 1668bfe8043eSStefan Hajnoczi return -EINVAL; 1669bfe8043eSStefan Hajnoczi } 1670bfe8043eSStefan Hajnoczi 16713ef6c40aSMax Reitz ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags, 16723ef6c40aSMax Reitz cluster_size, prealloc, options, version, &local_err); 16733ef6c40aSMax Reitz if (error_is_set(&local_err)) { 16743ef6c40aSMax Reitz error_propagate(errp, local_err); 16753ef6c40aSMax Reitz } 16763ef6c40aSMax Reitz return ret; 1677de5f3f40SKevin Wolf } 1678de5f3f40SKevin Wolf 16797c80ab3fSJes Sorensen static int qcow2_make_empty(BlockDriverState *bs) 168020d97356SBlue Swirl { 168120d97356SBlue Swirl #if 0 168220d97356SBlue Swirl /* XXX: not correct */ 168320d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 168420d97356SBlue Swirl uint32_t l1_length = s->l1_size * sizeof(uint64_t); 168520d97356SBlue Swirl int ret; 168620d97356SBlue Swirl 168720d97356SBlue Swirl memset(s->l1_table, 0, l1_length); 168866f82ceeSKevin Wolf if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) 168920d97356SBlue Swirl return -1; 169066f82ceeSKevin Wolf ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length); 169120d97356SBlue Swirl if (ret < 0) 169220d97356SBlue Swirl return ret; 169320d97356SBlue Swirl 169420d97356SBlue Swirl l2_cache_reset(bs); 169520d97356SBlue Swirl #endif 169620d97356SBlue Swirl return 0; 169720d97356SBlue Swirl } 169820d97356SBlue Swirl 1699621f0589SKevin Wolf static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, 1700aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 1701621f0589SKevin Wolf { 1702621f0589SKevin Wolf int ret; 1703621f0589SKevin Wolf BDRVQcowState *s = bs->opaque; 1704621f0589SKevin Wolf 1705621f0589SKevin Wolf /* Emulate misaligned zero writes */ 1706621f0589SKevin Wolf if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) { 1707621f0589SKevin Wolf return -ENOTSUP; 1708621f0589SKevin Wolf } 1709621f0589SKevin Wolf 1710621f0589SKevin Wolf /* Whatever is left can use real zero clusters */ 1711621f0589SKevin Wolf qemu_co_mutex_lock(&s->lock); 1712621f0589SKevin Wolf ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, 1713621f0589SKevin Wolf nb_sectors); 1714621f0589SKevin Wolf qemu_co_mutex_unlock(&s->lock); 1715621f0589SKevin Wolf 1716621f0589SKevin Wolf return ret; 1717621f0589SKevin Wolf } 1718621f0589SKevin Wolf 17196db39ae2SPaolo Bonzini static coroutine_fn int qcow2_co_discard(BlockDriverState *bs, 17206db39ae2SPaolo Bonzini int64_t sector_num, int nb_sectors) 17215ea929e3SKevin Wolf { 17226db39ae2SPaolo Bonzini int ret; 17236db39ae2SPaolo Bonzini BDRVQcowState *s = bs->opaque; 17246db39ae2SPaolo Bonzini 17256db39ae2SPaolo Bonzini qemu_co_mutex_lock(&s->lock); 17266db39ae2SPaolo Bonzini ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS, 1727670df5e3SKevin Wolf nb_sectors, QCOW2_DISCARD_REQUEST); 17286db39ae2SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 17296db39ae2SPaolo Bonzini return ret; 17305ea929e3SKevin Wolf } 17315ea929e3SKevin Wolf 1732419b19d9SStefan Hajnoczi static int qcow2_truncate(BlockDriverState *bs, int64_t offset) 1733419b19d9SStefan Hajnoczi { 1734419b19d9SStefan Hajnoczi BDRVQcowState *s = bs->opaque; 17352cf7cfa1SKevin Wolf int64_t new_l1_size; 17362cf7cfa1SKevin Wolf int ret; 1737419b19d9SStefan Hajnoczi 1738419b19d9SStefan Hajnoczi if (offset & 511) { 1739259b2173SKevin Wolf error_report("The new size must be a multiple of 512"); 1740419b19d9SStefan Hajnoczi return -EINVAL; 1741419b19d9SStefan Hajnoczi } 1742419b19d9SStefan Hajnoczi 1743419b19d9SStefan Hajnoczi /* cannot proceed if image has snapshots */ 1744419b19d9SStefan Hajnoczi if (s->nb_snapshots) { 1745259b2173SKevin Wolf error_report("Can't resize an image which has snapshots"); 1746419b19d9SStefan Hajnoczi return -ENOTSUP; 1747419b19d9SStefan Hajnoczi } 1748419b19d9SStefan Hajnoczi 1749419b19d9SStefan Hajnoczi /* shrinking is currently not supported */ 1750419b19d9SStefan Hajnoczi if (offset < bs->total_sectors * 512) { 1751259b2173SKevin Wolf error_report("qcow2 doesn't support shrinking images yet"); 1752419b19d9SStefan Hajnoczi return -ENOTSUP; 1753419b19d9SStefan Hajnoczi } 1754419b19d9SStefan Hajnoczi 1755419b19d9SStefan Hajnoczi new_l1_size = size_to_l1(s, offset); 175672893756SStefan Hajnoczi ret = qcow2_grow_l1_table(bs, new_l1_size, true); 1757419b19d9SStefan Hajnoczi if (ret < 0) { 1758419b19d9SStefan Hajnoczi return ret; 1759419b19d9SStefan Hajnoczi } 1760419b19d9SStefan Hajnoczi 1761419b19d9SStefan Hajnoczi /* write updated header.size */ 1762419b19d9SStefan Hajnoczi offset = cpu_to_be64(offset); 17638b3b7206SKevin Wolf ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), 1764419b19d9SStefan Hajnoczi &offset, sizeof(uint64_t)); 1765419b19d9SStefan Hajnoczi if (ret < 0) { 1766419b19d9SStefan Hajnoczi return ret; 1767419b19d9SStefan Hajnoczi } 1768419b19d9SStefan Hajnoczi 1769419b19d9SStefan Hajnoczi s->l1_vm_state_index = new_l1_size; 1770419b19d9SStefan Hajnoczi return 0; 1771419b19d9SStefan Hajnoczi } 1772419b19d9SStefan Hajnoczi 177320d97356SBlue Swirl /* XXX: put compressed sectors first, then all the cluster aligned 177420d97356SBlue Swirl tables to avoid losing bytes in alignment */ 17757c80ab3fSJes Sorensen static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num, 177620d97356SBlue Swirl const uint8_t *buf, int nb_sectors) 177720d97356SBlue Swirl { 177820d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 177920d97356SBlue Swirl z_stream strm; 178020d97356SBlue Swirl int ret, out_len; 178120d97356SBlue Swirl uint8_t *out_buf; 178220d97356SBlue Swirl uint64_t cluster_offset; 178320d97356SBlue Swirl 178420d97356SBlue Swirl if (nb_sectors == 0) { 178520d97356SBlue Swirl /* align end of file to a sector boundary to ease reading with 178620d97356SBlue Swirl sector based I/Os */ 178766f82ceeSKevin Wolf cluster_offset = bdrv_getlength(bs->file); 178820d97356SBlue Swirl cluster_offset = (cluster_offset + 511) & ~511; 178966f82ceeSKevin Wolf bdrv_truncate(bs->file, cluster_offset); 179020d97356SBlue Swirl return 0; 179120d97356SBlue Swirl } 179220d97356SBlue Swirl 1793f4d38befSStefan Hajnoczi if (nb_sectors != s->cluster_sectors) { 1794f4d38befSStefan Hajnoczi ret = -EINVAL; 1795f4d38befSStefan Hajnoczi 1796f4d38befSStefan Hajnoczi /* Zero-pad last write if image size is not cluster aligned */ 1797f4d38befSStefan Hajnoczi if (sector_num + nb_sectors == bs->total_sectors && 1798f4d38befSStefan Hajnoczi nb_sectors < s->cluster_sectors) { 1799f4d38befSStefan Hajnoczi uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size); 1800f4d38befSStefan Hajnoczi memset(pad_buf, 0, s->cluster_size); 1801f4d38befSStefan Hajnoczi memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE); 1802f4d38befSStefan Hajnoczi ret = qcow2_write_compressed(bs, sector_num, 1803f4d38befSStefan Hajnoczi pad_buf, s->cluster_sectors); 1804f4d38befSStefan Hajnoczi qemu_vfree(pad_buf); 1805f4d38befSStefan Hajnoczi } 1806f4d38befSStefan Hajnoczi return ret; 1807f4d38befSStefan Hajnoczi } 180820d97356SBlue Swirl 18097267c094SAnthony Liguori out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); 181020d97356SBlue Swirl 181120d97356SBlue Swirl /* best compression, small window, no zlib header */ 181220d97356SBlue Swirl memset(&strm, 0, sizeof(strm)); 181320d97356SBlue Swirl ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, 181420d97356SBlue Swirl Z_DEFLATED, -12, 181520d97356SBlue Swirl 9, Z_DEFAULT_STRATEGY); 181620d97356SBlue Swirl if (ret != 0) { 18178f1efd00SKevin Wolf ret = -EINVAL; 18188f1efd00SKevin Wolf goto fail; 181920d97356SBlue Swirl } 182020d97356SBlue Swirl 182120d97356SBlue Swirl strm.avail_in = s->cluster_size; 182220d97356SBlue Swirl strm.next_in = (uint8_t *)buf; 182320d97356SBlue Swirl strm.avail_out = s->cluster_size; 182420d97356SBlue Swirl strm.next_out = out_buf; 182520d97356SBlue Swirl 182620d97356SBlue Swirl ret = deflate(&strm, Z_FINISH); 182720d97356SBlue Swirl if (ret != Z_STREAM_END && ret != Z_OK) { 182820d97356SBlue Swirl deflateEnd(&strm); 18298f1efd00SKevin Wolf ret = -EINVAL; 18308f1efd00SKevin Wolf goto fail; 183120d97356SBlue Swirl } 183220d97356SBlue Swirl out_len = strm.next_out - out_buf; 183320d97356SBlue Swirl 183420d97356SBlue Swirl deflateEnd(&strm); 183520d97356SBlue Swirl 183620d97356SBlue Swirl if (ret != Z_STREAM_END || out_len >= s->cluster_size) { 183720d97356SBlue Swirl /* could not compress: write normal cluster */ 18388f1efd00SKevin Wolf ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors); 18398f1efd00SKevin Wolf if (ret < 0) { 18408f1efd00SKevin Wolf goto fail; 18418f1efd00SKevin Wolf } 184220d97356SBlue Swirl } else { 184320d97356SBlue Swirl cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, 184420d97356SBlue Swirl sector_num << 9, out_len); 18458f1efd00SKevin Wolf if (!cluster_offset) { 18468f1efd00SKevin Wolf ret = -EIO; 18478f1efd00SKevin Wolf goto fail; 18488f1efd00SKevin Wolf } 184920d97356SBlue Swirl cluster_offset &= s->cluster_offset_mask; 1850cf93980eSMax Reitz 1851231bb267SMax Reitz ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len); 1852cf93980eSMax Reitz if (ret < 0) { 1853cf93980eSMax Reitz goto fail; 1854cf93980eSMax Reitz } 1855cf93980eSMax Reitz 185666f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); 18578f1efd00SKevin Wolf ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len); 18588f1efd00SKevin Wolf if (ret < 0) { 18598f1efd00SKevin Wolf goto fail; 186020d97356SBlue Swirl } 186120d97356SBlue Swirl } 186220d97356SBlue Swirl 18638f1efd00SKevin Wolf ret = 0; 18648f1efd00SKevin Wolf fail: 18657267c094SAnthony Liguori g_free(out_buf); 18668f1efd00SKevin Wolf return ret; 186720d97356SBlue Swirl } 186820d97356SBlue Swirl 1869a968168cSDong Xu Wang static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs) 187020d97356SBlue Swirl { 187129c1a730SKevin Wolf BDRVQcowState *s = bs->opaque; 187229c1a730SKevin Wolf int ret; 187329c1a730SKevin Wolf 18748b94ff85SPaolo Bonzini qemu_co_mutex_lock(&s->lock); 187529c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->l2_table_cache); 187629c1a730SKevin Wolf if (ret < 0) { 1877c95de7e2SDong Xu Wang qemu_co_mutex_unlock(&s->lock); 18788b94ff85SPaolo Bonzini return ret; 187929c1a730SKevin Wolf } 188029c1a730SKevin Wolf 1881bfe8043eSStefan Hajnoczi if (qcow2_need_accurate_refcounts(s)) { 188229c1a730SKevin Wolf ret = qcow2_cache_flush(bs, s->refcount_block_cache); 188329c1a730SKevin Wolf if (ret < 0) { 1884c95de7e2SDong Xu Wang qemu_co_mutex_unlock(&s->lock); 18858b94ff85SPaolo Bonzini return ret; 188629c1a730SKevin Wolf } 1887bfe8043eSStefan Hajnoczi } 18888b94ff85SPaolo Bonzini qemu_co_mutex_unlock(&s->lock); 188929c1a730SKevin Wolf 1890eb489bb1SKevin Wolf return 0; 1891eb489bb1SKevin Wolf } 1892eb489bb1SKevin Wolf 18937c80ab3fSJes Sorensen static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 189420d97356SBlue Swirl { 189520d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 189620d97356SBlue Swirl bdi->cluster_size = s->cluster_size; 18977c80ab3fSJes Sorensen bdi->vm_state_offset = qcow2_vm_state_offset(s); 189820d97356SBlue Swirl return 0; 189920d97356SBlue Swirl } 190020d97356SBlue Swirl 190137764dfbSMax Reitz static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs) 190237764dfbSMax Reitz { 190337764dfbSMax Reitz BDRVQcowState *s = bs->opaque; 190437764dfbSMax Reitz ImageInfoSpecific *spec_info = g_new(ImageInfoSpecific, 1); 190537764dfbSMax Reitz 190637764dfbSMax Reitz *spec_info = (ImageInfoSpecific){ 190737764dfbSMax Reitz .kind = IMAGE_INFO_SPECIFIC_KIND_QCOW2, 190837764dfbSMax Reitz { 190937764dfbSMax Reitz .qcow2 = g_new(ImageInfoSpecificQCow2, 1), 191037764dfbSMax Reitz }, 191137764dfbSMax Reitz }; 191237764dfbSMax Reitz if (s->qcow_version == 2) { 191337764dfbSMax Reitz *spec_info->qcow2 = (ImageInfoSpecificQCow2){ 191437764dfbSMax Reitz .compat = g_strdup("0.10"), 191537764dfbSMax Reitz }; 191637764dfbSMax Reitz } else if (s->qcow_version == 3) { 191737764dfbSMax Reitz *spec_info->qcow2 = (ImageInfoSpecificQCow2){ 191837764dfbSMax Reitz .compat = g_strdup("1.1"), 191937764dfbSMax Reitz .lazy_refcounts = s->compatible_features & 192037764dfbSMax Reitz QCOW2_COMPAT_LAZY_REFCOUNTS, 192137764dfbSMax Reitz .has_lazy_refcounts = true, 192237764dfbSMax Reitz }; 192337764dfbSMax Reitz } 192437764dfbSMax Reitz 192537764dfbSMax Reitz return spec_info; 192637764dfbSMax Reitz } 192737764dfbSMax Reitz 192820d97356SBlue Swirl #if 0 192920d97356SBlue Swirl static void dump_refcounts(BlockDriverState *bs) 193020d97356SBlue Swirl { 193120d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 193220d97356SBlue Swirl int64_t nb_clusters, k, k1, size; 193320d97356SBlue Swirl int refcount; 193420d97356SBlue Swirl 193566f82ceeSKevin Wolf size = bdrv_getlength(bs->file); 193620d97356SBlue Swirl nb_clusters = size_to_clusters(s, size); 193720d97356SBlue Swirl for(k = 0; k < nb_clusters;) { 193820d97356SBlue Swirl k1 = k; 193920d97356SBlue Swirl refcount = get_refcount(bs, k); 194020d97356SBlue Swirl k++; 194120d97356SBlue Swirl while (k < nb_clusters && get_refcount(bs, k) == refcount) 194220d97356SBlue Swirl k++; 19430bfcd599SBlue Swirl printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, 19440bfcd599SBlue Swirl k - k1); 194520d97356SBlue Swirl } 194620d97356SBlue Swirl } 194720d97356SBlue Swirl #endif 194820d97356SBlue Swirl 1949cf8074b3SKevin Wolf static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, 1950cf8074b3SKevin Wolf int64_t pos) 195120d97356SBlue Swirl { 195220d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 1953eedff66fSMax Reitz int64_t total_sectors = bs->total_sectors; 195420d97356SBlue Swirl int growable = bs->growable; 19556e13610aSMax Reitz bool zero_beyond_eof = bs->zero_beyond_eof; 195620d97356SBlue Swirl int ret; 195720d97356SBlue Swirl 195866f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); 195920d97356SBlue Swirl bs->growable = 1; 19606e13610aSMax Reitz bs->zero_beyond_eof = false; 19618d3b1a2dSKevin Wolf ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov); 196220d97356SBlue Swirl bs->growable = growable; 19636e13610aSMax Reitz bs->zero_beyond_eof = zero_beyond_eof; 196420d97356SBlue Swirl 1965eedff66fSMax Reitz /* bdrv_co_do_writev will have increased the total_sectors value to include 1966eedff66fSMax Reitz * the VM state - the VM state is however not an actual part of the block 1967eedff66fSMax Reitz * device, therefore, we need to restore the old value. */ 1968eedff66fSMax Reitz bs->total_sectors = total_sectors; 1969eedff66fSMax Reitz 197020d97356SBlue Swirl return ret; 197120d97356SBlue Swirl } 197220d97356SBlue Swirl 19737c80ab3fSJes Sorensen static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf, 197420d97356SBlue Swirl int64_t pos, int size) 197520d97356SBlue Swirl { 197620d97356SBlue Swirl BDRVQcowState *s = bs->opaque; 197720d97356SBlue Swirl int growable = bs->growable; 19780d51b4deSAsias He bool zero_beyond_eof = bs->zero_beyond_eof; 197920d97356SBlue Swirl int ret; 198020d97356SBlue Swirl 198166f82ceeSKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); 198220d97356SBlue Swirl bs->growable = 1; 19830d51b4deSAsias He bs->zero_beyond_eof = false; 19847c80ab3fSJes Sorensen ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size); 198520d97356SBlue Swirl bs->growable = growable; 19860d51b4deSAsias He bs->zero_beyond_eof = zero_beyond_eof; 198720d97356SBlue Swirl 198820d97356SBlue Swirl return ret; 198920d97356SBlue Swirl } 199020d97356SBlue Swirl 19919296b3edSMax Reitz /* 19929296b3edSMax Reitz * Downgrades an image's version. To achieve this, any incompatible features 19939296b3edSMax Reitz * have to be removed. 19949296b3edSMax Reitz */ 19959296b3edSMax Reitz static int qcow2_downgrade(BlockDriverState *bs, int target_version) 19969296b3edSMax Reitz { 19979296b3edSMax Reitz BDRVQcowState *s = bs->opaque; 19989296b3edSMax Reitz int current_version = s->qcow_version; 19999296b3edSMax Reitz int ret; 20009296b3edSMax Reitz 20019296b3edSMax Reitz if (target_version == current_version) { 20029296b3edSMax Reitz return 0; 20039296b3edSMax Reitz } else if (target_version > current_version) { 20049296b3edSMax Reitz return -EINVAL; 20059296b3edSMax Reitz } else if (target_version != 2) { 20069296b3edSMax Reitz return -EINVAL; 20079296b3edSMax Reitz } 20089296b3edSMax Reitz 20099296b3edSMax Reitz if (s->refcount_order != 4) { 20109296b3edSMax Reitz /* we would have to convert the image to a refcount_order == 4 image 20119296b3edSMax Reitz * here; however, since qemu (at the time of writing this) does not 20129296b3edSMax Reitz * support anything different than 4 anyway, there is no point in doing 20139296b3edSMax Reitz * so right now; however, we should error out (if qemu supports this in 20149296b3edSMax Reitz * the future and this code has not been adapted) */ 20159296b3edSMax Reitz error_report("qcow2_downgrade: Image refcount orders other than 4 are " 20169296b3edSMax Reitz "currently not supported."); 20179296b3edSMax Reitz return -ENOTSUP; 20189296b3edSMax Reitz } 20199296b3edSMax Reitz 20209296b3edSMax Reitz /* clear incompatible features */ 20219296b3edSMax Reitz if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) { 20229296b3edSMax Reitz ret = qcow2_mark_clean(bs); 20239296b3edSMax Reitz if (ret < 0) { 20249296b3edSMax Reitz return ret; 20259296b3edSMax Reitz } 20269296b3edSMax Reitz } 20279296b3edSMax Reitz 20289296b3edSMax Reitz /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in 20299296b3edSMax Reitz * the first place; if that happens nonetheless, returning -ENOTSUP is the 20309296b3edSMax Reitz * best thing to do anyway */ 20319296b3edSMax Reitz 20329296b3edSMax Reitz if (s->incompatible_features) { 20339296b3edSMax Reitz return -ENOTSUP; 20349296b3edSMax Reitz } 20359296b3edSMax Reitz 20369296b3edSMax Reitz /* since we can ignore compatible features, we can set them to 0 as well */ 20379296b3edSMax Reitz s->compatible_features = 0; 20389296b3edSMax Reitz /* if lazy refcounts have been used, they have already been fixed through 20399296b3edSMax Reitz * clearing the dirty flag */ 20409296b3edSMax Reitz 20419296b3edSMax Reitz /* clearing autoclear features is trivial */ 20429296b3edSMax Reitz s->autoclear_features = 0; 20439296b3edSMax Reitz 20449296b3edSMax Reitz ret = qcow2_expand_zero_clusters(bs); 20459296b3edSMax Reitz if (ret < 0) { 20469296b3edSMax Reitz return ret; 20479296b3edSMax Reitz } 20489296b3edSMax Reitz 20499296b3edSMax Reitz s->qcow_version = target_version; 20509296b3edSMax Reitz ret = qcow2_update_header(bs); 20519296b3edSMax Reitz if (ret < 0) { 20529296b3edSMax Reitz s->qcow_version = current_version; 20539296b3edSMax Reitz return ret; 20549296b3edSMax Reitz } 20559296b3edSMax Reitz return 0; 20569296b3edSMax Reitz } 20579296b3edSMax Reitz 20589296b3edSMax Reitz static int qcow2_amend_options(BlockDriverState *bs, 20599296b3edSMax Reitz QEMUOptionParameter *options) 20609296b3edSMax Reitz { 20619296b3edSMax Reitz BDRVQcowState *s = bs->opaque; 20629296b3edSMax Reitz int old_version = s->qcow_version, new_version = old_version; 20639296b3edSMax Reitz uint64_t new_size = 0; 20649296b3edSMax Reitz const char *backing_file = NULL, *backing_format = NULL; 20659296b3edSMax Reitz bool lazy_refcounts = s->use_lazy_refcounts; 20669296b3edSMax Reitz int ret; 20679296b3edSMax Reitz int i; 20689296b3edSMax Reitz 20699296b3edSMax Reitz for (i = 0; options[i].name; i++) 20709296b3edSMax Reitz { 20719296b3edSMax Reitz if (!options[i].assigned) { 20729296b3edSMax Reitz /* only change explicitly defined options */ 20739296b3edSMax Reitz continue; 20749296b3edSMax Reitz } 20759296b3edSMax Reitz 20769296b3edSMax Reitz if (!strcmp(options[i].name, "compat")) { 20779296b3edSMax Reitz if (!options[i].value.s) { 20789296b3edSMax Reitz /* preserve default */ 20799296b3edSMax Reitz } else if (!strcmp(options[i].value.s, "0.10")) { 20809296b3edSMax Reitz new_version = 2; 20819296b3edSMax Reitz } else if (!strcmp(options[i].value.s, "1.1")) { 20829296b3edSMax Reitz new_version = 3; 20839296b3edSMax Reitz } else { 20849296b3edSMax Reitz fprintf(stderr, "Unknown compatibility level %s.\n", 20859296b3edSMax Reitz options[i].value.s); 20869296b3edSMax Reitz return -EINVAL; 20879296b3edSMax Reitz } 20889296b3edSMax Reitz } else if (!strcmp(options[i].name, "preallocation")) { 20899296b3edSMax Reitz fprintf(stderr, "Cannot change preallocation mode.\n"); 20909296b3edSMax Reitz return -ENOTSUP; 20919296b3edSMax Reitz } else if (!strcmp(options[i].name, "size")) { 20929296b3edSMax Reitz new_size = options[i].value.n; 20939296b3edSMax Reitz } else if (!strcmp(options[i].name, "backing_file")) { 20949296b3edSMax Reitz backing_file = options[i].value.s; 20959296b3edSMax Reitz } else if (!strcmp(options[i].name, "backing_fmt")) { 20969296b3edSMax Reitz backing_format = options[i].value.s; 20979296b3edSMax Reitz } else if (!strcmp(options[i].name, "encryption")) { 20989296b3edSMax Reitz if ((options[i].value.n != !!s->crypt_method)) { 20999296b3edSMax Reitz fprintf(stderr, "Changing the encryption flag is not " 21009296b3edSMax Reitz "supported.\n"); 21019296b3edSMax Reitz return -ENOTSUP; 21029296b3edSMax Reitz } 21039296b3edSMax Reitz } else if (!strcmp(options[i].name, "cluster_size")) { 21049296b3edSMax Reitz if (options[i].value.n != s->cluster_size) { 21059296b3edSMax Reitz fprintf(stderr, "Changing the cluster size is not " 21069296b3edSMax Reitz "supported.\n"); 21079296b3edSMax Reitz return -ENOTSUP; 21089296b3edSMax Reitz } 21099296b3edSMax Reitz } else if (!strcmp(options[i].name, "lazy_refcounts")) { 21109296b3edSMax Reitz lazy_refcounts = options[i].value.n; 21119296b3edSMax Reitz } else { 21129296b3edSMax Reitz /* if this assertion fails, this probably means a new option was 21139296b3edSMax Reitz * added without having it covered here */ 21149296b3edSMax Reitz assert(false); 21159296b3edSMax Reitz } 21169296b3edSMax Reitz } 21179296b3edSMax Reitz 21189296b3edSMax Reitz if (new_version != old_version) { 21199296b3edSMax Reitz if (new_version > old_version) { 21209296b3edSMax Reitz /* Upgrade */ 21219296b3edSMax Reitz s->qcow_version = new_version; 21229296b3edSMax Reitz ret = qcow2_update_header(bs); 21239296b3edSMax Reitz if (ret < 0) { 21249296b3edSMax Reitz s->qcow_version = old_version; 21259296b3edSMax Reitz return ret; 21269296b3edSMax Reitz } 21279296b3edSMax Reitz } else { 21289296b3edSMax Reitz ret = qcow2_downgrade(bs, new_version); 21299296b3edSMax Reitz if (ret < 0) { 21309296b3edSMax Reitz return ret; 21319296b3edSMax Reitz } 21329296b3edSMax Reitz } 21339296b3edSMax Reitz } 21349296b3edSMax Reitz 21359296b3edSMax Reitz if (backing_file || backing_format) { 21369296b3edSMax Reitz ret = qcow2_change_backing_file(bs, backing_file ?: bs->backing_file, 21379296b3edSMax Reitz backing_format ?: bs->backing_format); 21389296b3edSMax Reitz if (ret < 0) { 21399296b3edSMax Reitz return ret; 21409296b3edSMax Reitz } 21419296b3edSMax Reitz } 21429296b3edSMax Reitz 21439296b3edSMax Reitz if (s->use_lazy_refcounts != lazy_refcounts) { 21449296b3edSMax Reitz if (lazy_refcounts) { 21459296b3edSMax Reitz if (s->qcow_version < 3) { 21469296b3edSMax Reitz fprintf(stderr, "Lazy refcounts only supported with compatibility " 21479296b3edSMax Reitz "level 1.1 and above (use compat=1.1 or greater)\n"); 21489296b3edSMax Reitz return -EINVAL; 21499296b3edSMax Reitz } 21509296b3edSMax Reitz s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; 21519296b3edSMax Reitz ret = qcow2_update_header(bs); 21529296b3edSMax Reitz if (ret < 0) { 21539296b3edSMax Reitz s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; 21549296b3edSMax Reitz return ret; 21559296b3edSMax Reitz } 21569296b3edSMax Reitz s->use_lazy_refcounts = true; 21579296b3edSMax Reitz } else { 21589296b3edSMax Reitz /* make image clean first */ 21599296b3edSMax Reitz ret = qcow2_mark_clean(bs); 21609296b3edSMax Reitz if (ret < 0) { 21619296b3edSMax Reitz return ret; 21629296b3edSMax Reitz } 21639296b3edSMax Reitz /* now disallow lazy refcounts */ 21649296b3edSMax Reitz s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS; 21659296b3edSMax Reitz ret = qcow2_update_header(bs); 21669296b3edSMax Reitz if (ret < 0) { 21679296b3edSMax Reitz s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS; 21689296b3edSMax Reitz return ret; 21699296b3edSMax Reitz } 21709296b3edSMax Reitz s->use_lazy_refcounts = false; 21719296b3edSMax Reitz } 21729296b3edSMax Reitz } 21739296b3edSMax Reitz 21749296b3edSMax Reitz if (new_size) { 21759296b3edSMax Reitz ret = bdrv_truncate(bs, new_size); 21769296b3edSMax Reitz if (ret < 0) { 21779296b3edSMax Reitz return ret; 21789296b3edSMax Reitz } 21799296b3edSMax Reitz } 21809296b3edSMax Reitz 21819296b3edSMax Reitz return 0; 21829296b3edSMax Reitz } 21839296b3edSMax Reitz 21847c80ab3fSJes Sorensen static QEMUOptionParameter qcow2_create_options[] = { 218520d97356SBlue Swirl { 218620d97356SBlue Swirl .name = BLOCK_OPT_SIZE, 218720d97356SBlue Swirl .type = OPT_SIZE, 218820d97356SBlue Swirl .help = "Virtual disk size" 218920d97356SBlue Swirl }, 219020d97356SBlue Swirl { 21916744cbabSKevin Wolf .name = BLOCK_OPT_COMPAT_LEVEL, 21926744cbabSKevin Wolf .type = OPT_STRING, 21936744cbabSKevin Wolf .help = "Compatibility level (0.10 or 1.1)" 21946744cbabSKevin Wolf }, 21956744cbabSKevin Wolf { 219620d97356SBlue Swirl .name = BLOCK_OPT_BACKING_FILE, 219720d97356SBlue Swirl .type = OPT_STRING, 219820d97356SBlue Swirl .help = "File name of a base image" 219920d97356SBlue Swirl }, 220020d97356SBlue Swirl { 220120d97356SBlue Swirl .name = BLOCK_OPT_BACKING_FMT, 220220d97356SBlue Swirl .type = OPT_STRING, 220320d97356SBlue Swirl .help = "Image format of the base image" 220420d97356SBlue Swirl }, 220520d97356SBlue Swirl { 220620d97356SBlue Swirl .name = BLOCK_OPT_ENCRYPT, 220720d97356SBlue Swirl .type = OPT_FLAG, 220820d97356SBlue Swirl .help = "Encrypt the image" 220920d97356SBlue Swirl }, 221020d97356SBlue Swirl { 221120d97356SBlue Swirl .name = BLOCK_OPT_CLUSTER_SIZE, 221220d97356SBlue Swirl .type = OPT_SIZE, 221399cce9faSKevin Wolf .help = "qcow2 cluster size", 221499cce9faSKevin Wolf .value = { .n = DEFAULT_CLUSTER_SIZE }, 221520d97356SBlue Swirl }, 221620d97356SBlue Swirl { 221720d97356SBlue Swirl .name = BLOCK_OPT_PREALLOC, 221820d97356SBlue Swirl .type = OPT_STRING, 221920d97356SBlue Swirl .help = "Preallocation mode (allowed values: off, metadata)" 222020d97356SBlue Swirl }, 2221bfe8043eSStefan Hajnoczi { 2222bfe8043eSStefan Hajnoczi .name = BLOCK_OPT_LAZY_REFCOUNTS, 2223bfe8043eSStefan Hajnoczi .type = OPT_FLAG, 2224bfe8043eSStefan Hajnoczi .help = "Postpone refcount updates", 2225bfe8043eSStefan Hajnoczi }, 222620d97356SBlue Swirl { NULL } 222720d97356SBlue Swirl }; 222820d97356SBlue Swirl 222920d97356SBlue Swirl static BlockDriver bdrv_qcow2 = { 223020d97356SBlue Swirl .format_name = "qcow2", 223120d97356SBlue Swirl .instance_size = sizeof(BDRVQcowState), 22327c80ab3fSJes Sorensen .bdrv_probe = qcow2_probe, 22337c80ab3fSJes Sorensen .bdrv_open = qcow2_open, 22347c80ab3fSJes Sorensen .bdrv_close = qcow2_close, 223521d82ac9SJeff Cody .bdrv_reopen_prepare = qcow2_reopen_prepare, 22367c80ab3fSJes Sorensen .bdrv_create = qcow2_create, 22373ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 2238b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = qcow2_co_get_block_status, 22397c80ab3fSJes Sorensen .bdrv_set_key = qcow2_set_key, 22407c80ab3fSJes Sorensen .bdrv_make_empty = qcow2_make_empty, 224120d97356SBlue Swirl 224268d100e9SKevin Wolf .bdrv_co_readv = qcow2_co_readv, 224368d100e9SKevin Wolf .bdrv_co_writev = qcow2_co_writev, 2244eb489bb1SKevin Wolf .bdrv_co_flush_to_os = qcow2_co_flush_to_os, 2245419b19d9SStefan Hajnoczi 2246621f0589SKevin Wolf .bdrv_co_write_zeroes = qcow2_co_write_zeroes, 22476db39ae2SPaolo Bonzini .bdrv_co_discard = qcow2_co_discard, 2248419b19d9SStefan Hajnoczi .bdrv_truncate = qcow2_truncate, 22497c80ab3fSJes Sorensen .bdrv_write_compressed = qcow2_write_compressed, 225020d97356SBlue Swirl 225120d97356SBlue Swirl .bdrv_snapshot_create = qcow2_snapshot_create, 225220d97356SBlue Swirl .bdrv_snapshot_goto = qcow2_snapshot_goto, 225320d97356SBlue Swirl .bdrv_snapshot_delete = qcow2_snapshot_delete, 225420d97356SBlue Swirl .bdrv_snapshot_list = qcow2_snapshot_list, 225551ef6727Sedison .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, 22567c80ab3fSJes Sorensen .bdrv_get_info = qcow2_get_info, 225737764dfbSMax Reitz .bdrv_get_specific_info = qcow2_get_specific_info, 225820d97356SBlue Swirl 22597c80ab3fSJes Sorensen .bdrv_save_vmstate = qcow2_save_vmstate, 22607c80ab3fSJes Sorensen .bdrv_load_vmstate = qcow2_load_vmstate, 226120d97356SBlue Swirl 226220d97356SBlue Swirl .bdrv_change_backing_file = qcow2_change_backing_file, 226320d97356SBlue Swirl 226406d9260fSAnthony Liguori .bdrv_invalidate_cache = qcow2_invalidate_cache, 226506d9260fSAnthony Liguori 22667c80ab3fSJes Sorensen .create_options = qcow2_create_options, 22677c80ab3fSJes Sorensen .bdrv_check = qcow2_check, 22689296b3edSMax Reitz .bdrv_amend_options = qcow2_amend_options, 226920d97356SBlue Swirl }; 227020d97356SBlue Swirl 22715efa9d5aSAnthony Liguori static void bdrv_qcow2_init(void) 22725efa9d5aSAnthony Liguori { 22735efa9d5aSAnthony Liguori bdrv_register(&bdrv_qcow2); 22745efa9d5aSAnthony Liguori } 22755efa9d5aSAnthony Liguori 22765efa9d5aSAnthony Liguori block_init(bdrv_qcow2_init); 2277