1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * Block driver for the QCOW format 3019d6b8fSAnthony Liguori * 4019d6b8fSAnthony Liguori * Copyright (c) 2004-2006 Fabrice Bellard 5019d6b8fSAnthony Liguori * 6019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 7019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 8019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 9019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 11019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 12019d6b8fSAnthony Liguori * 13019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 14019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 15019d6b8fSAnthony Liguori * 16019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22019d6b8fSAnthony Liguori * THE SOFTWARE. 23019d6b8fSAnthony Liguori */ 2480c71a24SPeter Maydell #include "qemu/osdep.h" 25da34e65cSMarkus Armbruster #include "qapi/error.h" 26019d6b8fSAnthony Liguori #include "qemu-common.h" 27e6ff69bfSDaniel P. Berrange #include "qemu/error-report.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 296af40160SKevin Wolf #include "sysemu/block-backend.h" 301de7afc9SPaolo Bonzini #include "qemu/module.h" 3158369e22SPaolo Bonzini #include "qemu/bswap.h" 32019d6b8fSAnthony Liguori #include <zlib.h> 33cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 34d85f4222SDaniel P. Berrange #include "qapi/qmp/qstring.h" 35d85f4222SDaniel P. Berrange #include "crypto/block.h" 36795c40b8SJuan Quintela #include "migration/blocker.h" 37d85f4222SDaniel P. Berrange #include "block/crypto.h" 38019d6b8fSAnthony Liguori 39019d6b8fSAnthony Liguori /**************************************************************/ 40019d6b8fSAnthony Liguori /* QEMU COW block driver with compression and encryption support */ 41019d6b8fSAnthony Liguori 42019d6b8fSAnthony Liguori #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) 43019d6b8fSAnthony Liguori #define QCOW_VERSION 1 44019d6b8fSAnthony Liguori 45019d6b8fSAnthony Liguori #define QCOW_CRYPT_NONE 0 46019d6b8fSAnthony Liguori #define QCOW_CRYPT_AES 1 47019d6b8fSAnthony Liguori 48019d6b8fSAnthony Liguori #define QCOW_OFLAG_COMPRESSED (1LL << 63) 49019d6b8fSAnthony Liguori 50019d6b8fSAnthony Liguori typedef struct QCowHeader { 51019d6b8fSAnthony Liguori uint32_t magic; 52019d6b8fSAnthony Liguori uint32_t version; 53019d6b8fSAnthony Liguori uint64_t backing_file_offset; 54019d6b8fSAnthony Liguori uint32_t backing_file_size; 55019d6b8fSAnthony Liguori uint32_t mtime; 56019d6b8fSAnthony Liguori uint64_t size; /* in bytes */ 57019d6b8fSAnthony Liguori uint8_t cluster_bits; 58019d6b8fSAnthony Liguori uint8_t l2_bits; 59ea54feffSKevin Wolf uint16_t padding; 60019d6b8fSAnthony Liguori uint32_t crypt_method; 61019d6b8fSAnthony Liguori uint64_t l1_table_offset; 62ea54feffSKevin Wolf } QEMU_PACKED QCowHeader; 63019d6b8fSAnthony Liguori 64019d6b8fSAnthony Liguori #define L2_CACHE_SIZE 16 65019d6b8fSAnthony Liguori 66019d6b8fSAnthony Liguori typedef struct BDRVQcowState { 67019d6b8fSAnthony Liguori int cluster_bits; 68019d6b8fSAnthony Liguori int cluster_size; 69019d6b8fSAnthony Liguori int cluster_sectors; 70019d6b8fSAnthony Liguori int l2_bits; 71019d6b8fSAnthony Liguori int l2_size; 7246485de0SKevin Wolf unsigned int l1_size; 73019d6b8fSAnthony Liguori uint64_t cluster_offset_mask; 74019d6b8fSAnthony Liguori uint64_t l1_table_offset; 75019d6b8fSAnthony Liguori uint64_t *l1_table; 76019d6b8fSAnthony Liguori uint64_t *l2_cache; 77019d6b8fSAnthony Liguori uint64_t l2_cache_offsets[L2_CACHE_SIZE]; 78019d6b8fSAnthony Liguori uint32_t l2_cache_counts[L2_CACHE_SIZE]; 79019d6b8fSAnthony Liguori uint8_t *cluster_cache; 80019d6b8fSAnthony Liguori uint8_t *cluster_data; 81019d6b8fSAnthony Liguori uint64_t cluster_cache_offset; 82d85f4222SDaniel P. Berrange QCryptoBlock *crypto; /* Disk encryption format driver */ 83019d6b8fSAnthony Liguori uint32_t crypt_method_header; 8452b8eb60SKevin Wolf CoMutex lock; 85fd9f102cSKevin Wolf Error *migration_blocker; 86019d6b8fSAnthony Liguori } BDRVQcowState; 87019d6b8fSAnthony Liguori 8866f82ceeSKevin Wolf static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); 89019d6b8fSAnthony Liguori 90019d6b8fSAnthony Liguori static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) 91019d6b8fSAnthony Liguori { 92019d6b8fSAnthony Liguori const QCowHeader *cow_header = (const void *)buf; 93019d6b8fSAnthony Liguori 94019d6b8fSAnthony Liguori if (buf_size >= sizeof(QCowHeader) && 95019d6b8fSAnthony Liguori be32_to_cpu(cow_header->magic) == QCOW_MAGIC && 96019d6b8fSAnthony Liguori be32_to_cpu(cow_header->version) == QCOW_VERSION) 97019d6b8fSAnthony Liguori return 100; 98019d6b8fSAnthony Liguori else 99019d6b8fSAnthony Liguori return 0; 100019d6b8fSAnthony Liguori } 101019d6b8fSAnthony Liguori 102d85f4222SDaniel P. Berrange static QemuOptsList qcow_runtime_opts = { 103d85f4222SDaniel P. Berrange .name = "qcow", 104d85f4222SDaniel P. Berrange .head = QTAILQ_HEAD_INITIALIZER(qcow_runtime_opts.head), 105d85f4222SDaniel P. Berrange .desc = { 106d85f4222SDaniel P. Berrange BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."), 107d85f4222SDaniel P. Berrange { /* end of list */ } 108d85f4222SDaniel P. Berrange }, 109d85f4222SDaniel P. Berrange }; 110d85f4222SDaniel P. Berrange 111015a1036SMax Reitz static int qcow_open(BlockDriverState *bs, QDict *options, int flags, 112015a1036SMax Reitz Error **errp) 113019d6b8fSAnthony Liguori { 114019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 115d66e5ceeSKevin Wolf unsigned int len, i, shift; 116d66e5ceeSKevin Wolf int ret; 117019d6b8fSAnthony Liguori QCowHeader header; 118fe44dc91SAshijeet Acharya Error *local_err = NULL; 119d85f4222SDaniel P. Berrange QCryptoBlockOpenOptions *crypto_opts = NULL; 120d85f4222SDaniel P. Berrange unsigned int cflags = 0; 121d85f4222SDaniel P. Berrange QDict *encryptopts = NULL; 122d85f4222SDaniel P. Berrange const char *encryptfmt; 123d85f4222SDaniel P. Berrange 124d85f4222SDaniel P. Berrange qdict_extract_subqdict(options, &encryptopts, "encrypt."); 125d85f4222SDaniel P. Berrange encryptfmt = qdict_get_try_str(encryptopts, "format"); 126019d6b8fSAnthony Liguori 1274e4bf5c4SKevin Wolf bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, 1284e4bf5c4SKevin Wolf false, errp); 1294e4bf5c4SKevin Wolf if (!bs->file) { 130d85f4222SDaniel P. Berrange ret = -EINVAL; 131d85f4222SDaniel P. Berrange goto fail; 1324e4bf5c4SKevin Wolf } 1334e4bf5c4SKevin Wolf 134cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); 13584b0ec02SLi Zhi Hui if (ret < 0) { 136019d6b8fSAnthony Liguori goto fail; 13784b0ec02SLi Zhi Hui } 138019d6b8fSAnthony Liguori be32_to_cpus(&header.magic); 139019d6b8fSAnthony Liguori be32_to_cpus(&header.version); 140019d6b8fSAnthony Liguori be64_to_cpus(&header.backing_file_offset); 141019d6b8fSAnthony Liguori be32_to_cpus(&header.backing_file_size); 142019d6b8fSAnthony Liguori be32_to_cpus(&header.mtime); 143019d6b8fSAnthony Liguori be64_to_cpus(&header.size); 144019d6b8fSAnthony Liguori be32_to_cpus(&header.crypt_method); 145019d6b8fSAnthony Liguori be64_to_cpus(&header.l1_table_offset); 146019d6b8fSAnthony Liguori 14784b0ec02SLi Zhi Hui if (header.magic != QCOW_MAGIC) { 14876abe407SPaolo Bonzini error_setg(errp, "Image not in qcow format"); 14976abe407SPaolo Bonzini ret = -EINVAL; 150019d6b8fSAnthony Liguori goto fail; 15184b0ec02SLi Zhi Hui } 15284b0ec02SLi Zhi Hui if (header.version != QCOW_VERSION) { 153a55448b3SMax Reitz error_setg(errp, "Unsupported qcow version %" PRIu32, header.version); 15484b0ec02SLi Zhi Hui ret = -ENOTSUP; 155019d6b8fSAnthony Liguori goto fail; 15684b0ec02SLi Zhi Hui } 15784b0ec02SLi Zhi Hui 1587159a45bSKevin Wolf if (header.size <= 1) { 1597159a45bSKevin Wolf error_setg(errp, "Image size is too small (must be at least 2 bytes)"); 16084b0ec02SLi Zhi Hui ret = -EINVAL; 161019d6b8fSAnthony Liguori goto fail; 16284b0ec02SLi Zhi Hui } 1637159a45bSKevin Wolf if (header.cluster_bits < 9 || header.cluster_bits > 16) { 1647159a45bSKevin Wolf error_setg(errp, "Cluster size must be between 512 and 64k"); 1657159a45bSKevin Wolf ret = -EINVAL; 1667159a45bSKevin Wolf goto fail; 1677159a45bSKevin Wolf } 1687159a45bSKevin Wolf 16942eb5817SKevin Wolf /* l2_bits specifies number of entries; storing a uint64_t in each entry, 17042eb5817SKevin Wolf * so bytes = num_entries << 3. */ 17142eb5817SKevin Wolf if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) { 17242eb5817SKevin Wolf error_setg(errp, "L2 table size must be between 512 and 64k"); 17342eb5817SKevin Wolf ret = -EINVAL; 17442eb5817SKevin Wolf goto fail; 17542eb5817SKevin Wolf } 17642eb5817SKevin Wolf 177019d6b8fSAnthony Liguori s->crypt_method_header = header.crypt_method; 17884b0ec02SLi Zhi Hui if (s->crypt_method_header) { 179e6ff69bfSDaniel P. Berrange if (bdrv_uses_whitelist() && 180e6ff69bfSDaniel P. Berrange s->crypt_method_header == QCOW_CRYPT_AES) { 1818c0dcbc4SDaniel P. Berrange error_setg(errp, 1828c0dcbc4SDaniel P. Berrange "Use of AES-CBC encrypted qcow images is no longer " 1838c0dcbc4SDaniel P. Berrange "supported in system emulators"); 1848c0dcbc4SDaniel P. Berrange error_append_hint(errp, 1858c0dcbc4SDaniel P. Berrange "You can use 'qemu-img convert' to convert your " 1868c0dcbc4SDaniel P. Berrange "image to an alternative supported format, such " 1878c0dcbc4SDaniel P. Berrange "as unencrypted qcow, or raw with the LUKS " 1888c0dcbc4SDaniel P. Berrange "format instead.\n"); 1898c0dcbc4SDaniel P. Berrange ret = -ENOSYS; 1908c0dcbc4SDaniel P. Berrange goto fail; 191e6ff69bfSDaniel P. Berrange } 192d85f4222SDaniel P. Berrange if (s->crypt_method_header == QCOW_CRYPT_AES) { 193d85f4222SDaniel P. Berrange if (encryptfmt && !g_str_equal(encryptfmt, "aes")) { 194d85f4222SDaniel P. Berrange error_setg(errp, 195d85f4222SDaniel P. Berrange "Header reported 'aes' encryption format but " 196d85f4222SDaniel P. Berrange "options specify '%s'", encryptfmt); 197d85f4222SDaniel P. Berrange ret = -EINVAL; 198d85f4222SDaniel P. Berrange goto fail; 199d85f4222SDaniel P. Berrange } 200d85f4222SDaniel P. Berrange qdict_del(encryptopts, "format"); 201d85f4222SDaniel P. Berrange crypto_opts = block_crypto_open_opts_init( 202d85f4222SDaniel P. Berrange Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); 203d85f4222SDaniel P. Berrange if (!crypto_opts) { 204d85f4222SDaniel P. Berrange ret = -EINVAL; 205d85f4222SDaniel P. Berrange goto fail; 206d85f4222SDaniel P. Berrange } 207e6ff69bfSDaniel P. Berrange 208d85f4222SDaniel P. Berrange if (flags & BDRV_O_NO_IO) { 209d85f4222SDaniel P. Berrange cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; 210d85f4222SDaniel P. Berrange } 2111cd9a787SDaniel P. Berrange s->crypto = qcrypto_block_open(crypto_opts, "encrypt.", 2121cd9a787SDaniel P. Berrange NULL, NULL, cflags, errp); 213d85f4222SDaniel P. Berrange if (!s->crypto) { 214d85f4222SDaniel P. Berrange ret = -EINVAL; 215d85f4222SDaniel P. Berrange goto fail; 216d85f4222SDaniel P. Berrange } 217d85f4222SDaniel P. Berrange } else { 218d85f4222SDaniel P. Berrange error_setg(errp, "invalid encryption method in qcow header"); 219d85f4222SDaniel P. Berrange ret = -EINVAL; 220d85f4222SDaniel P. Berrange goto fail; 221d85f4222SDaniel P. Berrange } 22254115412SEric Blake bs->encrypted = true; 223c01c214bSDaniel P. Berrange } else { 224c01c214bSDaniel P. Berrange if (encryptfmt) { 225c01c214bSDaniel P. Berrange error_setg(errp, "No encryption in image header, but options " 226c01c214bSDaniel P. Berrange "specified format '%s'", encryptfmt); 227c01c214bSDaniel P. Berrange ret = -EINVAL; 228c01c214bSDaniel P. Berrange goto fail; 229c01c214bSDaniel P. Berrange } 23084b0ec02SLi Zhi Hui } 231019d6b8fSAnthony Liguori s->cluster_bits = header.cluster_bits; 232019d6b8fSAnthony Liguori s->cluster_size = 1 << s->cluster_bits; 233019d6b8fSAnthony Liguori s->cluster_sectors = 1 << (s->cluster_bits - 9); 234019d6b8fSAnthony Liguori s->l2_bits = header.l2_bits; 235019d6b8fSAnthony Liguori s->l2_size = 1 << s->l2_bits; 236019d6b8fSAnthony Liguori bs->total_sectors = header.size / 512; 237019d6b8fSAnthony Liguori s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; 238019d6b8fSAnthony Liguori 239019d6b8fSAnthony Liguori /* read the level 1 table */ 240019d6b8fSAnthony Liguori shift = s->cluster_bits + s->l2_bits; 24146485de0SKevin Wolf if (header.size > UINT64_MAX - (1LL << shift)) { 24246485de0SKevin Wolf error_setg(errp, "Image too large"); 24346485de0SKevin Wolf ret = -EINVAL; 24446485de0SKevin Wolf goto fail; 24546485de0SKevin Wolf } else { 24646485de0SKevin Wolf uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift; 24746485de0SKevin Wolf if (l1_size > INT_MAX / sizeof(uint64_t)) { 24846485de0SKevin Wolf error_setg(errp, "Image too large"); 24946485de0SKevin Wolf ret = -EINVAL; 25046485de0SKevin Wolf goto fail; 25146485de0SKevin Wolf } 25246485de0SKevin Wolf s->l1_size = l1_size; 25346485de0SKevin Wolf } 254019d6b8fSAnthony Liguori 255019d6b8fSAnthony Liguori s->l1_table_offset = header.l1_table_offset; 2565839e53bSMarkus Armbruster s->l1_table = g_try_new(uint64_t, s->l1_size); 2570df93305SKevin Wolf if (s->l1_table == NULL) { 2580df93305SKevin Wolf error_setg(errp, "Could not allocate memory for L1 table"); 2590df93305SKevin Wolf ret = -ENOMEM; 2600df93305SKevin Wolf goto fail; 2610df93305SKevin Wolf } 26284b0ec02SLi Zhi Hui 263cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, 26484b0ec02SLi Zhi Hui s->l1_size * sizeof(uint64_t)); 26584b0ec02SLi Zhi Hui if (ret < 0) { 266019d6b8fSAnthony Liguori goto fail; 26784b0ec02SLi Zhi Hui } 26884b0ec02SLi Zhi Hui 269019d6b8fSAnthony Liguori for(i = 0;i < s->l1_size; i++) { 270019d6b8fSAnthony Liguori be64_to_cpus(&s->l1_table[i]); 271019d6b8fSAnthony Liguori } 2720df93305SKevin Wolf 2730df93305SKevin Wolf /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */ 2740df93305SKevin Wolf s->l2_cache = 2759a4f4c31SKevin Wolf qemu_try_blockalign(bs->file->bs, 2760df93305SKevin Wolf s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); 2770df93305SKevin Wolf if (s->l2_cache == NULL) { 2780df93305SKevin Wolf error_setg(errp, "Could not allocate L2 table cache"); 2790df93305SKevin Wolf ret = -ENOMEM; 2800df93305SKevin Wolf goto fail; 2810df93305SKevin Wolf } 2827267c094SAnthony Liguori s->cluster_cache = g_malloc(s->cluster_size); 2837267c094SAnthony Liguori s->cluster_data = g_malloc(s->cluster_size); 284019d6b8fSAnthony Liguori s->cluster_cache_offset = -1; 285019d6b8fSAnthony Liguori 286019d6b8fSAnthony Liguori /* read the backing file name */ 287019d6b8fSAnthony Liguori if (header.backing_file_offset != 0) { 288019d6b8fSAnthony Liguori len = header.backing_file_size; 289e729fa6aSJeff Cody if (len > 1023 || len >= sizeof(bs->backing_file)) { 290d66e5ceeSKevin Wolf error_setg(errp, "Backing file name too long"); 291d66e5ceeSKevin Wolf ret = -EINVAL; 292d66e5ceeSKevin Wolf goto fail; 29384b0ec02SLi Zhi Hui } 294cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, header.backing_file_offset, 29584b0ec02SLi Zhi Hui bs->backing_file, len); 29684b0ec02SLi Zhi Hui if (ret < 0) { 297019d6b8fSAnthony Liguori goto fail; 29884b0ec02SLi Zhi Hui } 299019d6b8fSAnthony Liguori bs->backing_file[len] = '\0'; 300019d6b8fSAnthony Liguori } 301de33b1f3SScott Wood 302fd9f102cSKevin Wolf /* Disable migration when qcow images are used */ 30381e5f78aSAlberto Garcia error_setg(&s->migration_blocker, "The qcow format used by node '%s' " 30481e5f78aSAlberto Garcia "does not support live migration", 30581e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs)); 306fe44dc91SAshijeet Acharya ret = migrate_add_blocker(s->migration_blocker, &local_err); 307fe44dc91SAshijeet Acharya if (local_err) { 308fe44dc91SAshijeet Acharya error_propagate(errp, local_err); 309fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 310fe44dc91SAshijeet Acharya goto fail; 311fe44dc91SAshijeet Acharya } 312fd9f102cSKevin Wolf 313d85f4222SDaniel P. Berrange QDECREF(encryptopts); 314d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockOpenOptions(crypto_opts); 315de33b1f3SScott Wood qemu_co_mutex_init(&s->lock); 316019d6b8fSAnthony Liguori return 0; 317019d6b8fSAnthony Liguori 318019d6b8fSAnthony Liguori fail: 3197267c094SAnthony Liguori g_free(s->l1_table); 3200df93305SKevin Wolf qemu_vfree(s->l2_cache); 3217267c094SAnthony Liguori g_free(s->cluster_cache); 3227267c094SAnthony Liguori g_free(s->cluster_data); 323d85f4222SDaniel P. Berrange qcrypto_block_free(s->crypto); 324d85f4222SDaniel P. Berrange QDECREF(encryptopts); 325d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockOpenOptions(crypto_opts); 32684b0ec02SLi Zhi Hui return ret; 327019d6b8fSAnthony Liguori } 328019d6b8fSAnthony Liguori 329d177692eSJeff Cody 330d177692eSJeff Cody /* We have nothing to do for QCOW reopen, stubs just return 331d177692eSJeff Cody * success */ 332d177692eSJeff Cody static int qcow_reopen_prepare(BDRVReopenState *state, 333d177692eSJeff Cody BlockReopenQueue *queue, Error **errp) 334d177692eSJeff Cody { 335d177692eSJeff Cody return 0; 336d177692eSJeff Cody } 337d177692eSJeff Cody 338019d6b8fSAnthony Liguori 339019d6b8fSAnthony Liguori /* 'allocate' is: 340019d6b8fSAnthony Liguori * 341019d6b8fSAnthony Liguori * 0 to not allocate. 342019d6b8fSAnthony Liguori * 343019d6b8fSAnthony Liguori * 1 to allocate a normal cluster (for sector indexes 'n_start' to 344019d6b8fSAnthony Liguori * 'n_end') 345019d6b8fSAnthony Liguori * 346019d6b8fSAnthony Liguori * 2 to allocate a compressed cluster of size 347019d6b8fSAnthony Liguori * 'compressed_size'. 'compressed_size' must be > 0 and < 348019d6b8fSAnthony Liguori * cluster_size 349019d6b8fSAnthony Liguori * 350019d6b8fSAnthony Liguori * return 0 if not allocated. 351019d6b8fSAnthony Liguori */ 352019d6b8fSAnthony Liguori static uint64_t get_cluster_offset(BlockDriverState *bs, 353019d6b8fSAnthony Liguori uint64_t offset, int allocate, 354019d6b8fSAnthony Liguori int compressed_size, 355019d6b8fSAnthony Liguori int n_start, int n_end) 356019d6b8fSAnthony Liguori { 357019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 358019d6b8fSAnthony Liguori int min_index, i, j, l1_index, l2_index; 359019d6b8fSAnthony Liguori uint64_t l2_offset, *l2_table, cluster_offset, tmp; 360019d6b8fSAnthony Liguori uint32_t min_count; 361019d6b8fSAnthony Liguori int new_l2_table; 362019d6b8fSAnthony Liguori 363019d6b8fSAnthony Liguori l1_index = offset >> (s->l2_bits + s->cluster_bits); 364019d6b8fSAnthony Liguori l2_offset = s->l1_table[l1_index]; 365019d6b8fSAnthony Liguori new_l2_table = 0; 366019d6b8fSAnthony Liguori if (!l2_offset) { 367019d6b8fSAnthony Liguori if (!allocate) 368019d6b8fSAnthony Liguori return 0; 369019d6b8fSAnthony Liguori /* allocate a new l2 entry */ 3709a4f4c31SKevin Wolf l2_offset = bdrv_getlength(bs->file->bs); 371019d6b8fSAnthony Liguori /* round to cluster size */ 372019d6b8fSAnthony Liguori l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); 373019d6b8fSAnthony Liguori /* update the L1 entry */ 374019d6b8fSAnthony Liguori s->l1_table[l1_index] = l2_offset; 375019d6b8fSAnthony Liguori tmp = cpu_to_be64(l2_offset); 376d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, 3775e5557d9SKevin Wolf s->l1_table_offset + l1_index * sizeof(tmp), 3785e5557d9SKevin Wolf &tmp, sizeof(tmp)) < 0) 379019d6b8fSAnthony Liguori return 0; 380019d6b8fSAnthony Liguori new_l2_table = 1; 381019d6b8fSAnthony Liguori } 382019d6b8fSAnthony Liguori for(i = 0; i < L2_CACHE_SIZE; i++) { 383019d6b8fSAnthony Liguori if (l2_offset == s->l2_cache_offsets[i]) { 384019d6b8fSAnthony Liguori /* increment the hit count */ 385019d6b8fSAnthony Liguori if (++s->l2_cache_counts[i] == 0xffffffff) { 386019d6b8fSAnthony Liguori for(j = 0; j < L2_CACHE_SIZE; j++) { 387019d6b8fSAnthony Liguori s->l2_cache_counts[j] >>= 1; 388019d6b8fSAnthony Liguori } 389019d6b8fSAnthony Liguori } 390019d6b8fSAnthony Liguori l2_table = s->l2_cache + (i << s->l2_bits); 391019d6b8fSAnthony Liguori goto found; 392019d6b8fSAnthony Liguori } 393019d6b8fSAnthony Liguori } 394019d6b8fSAnthony Liguori /* not found: load a new entry in the least used one */ 395019d6b8fSAnthony Liguori min_index = 0; 396019d6b8fSAnthony Liguori min_count = 0xffffffff; 397019d6b8fSAnthony Liguori for(i = 0; i < L2_CACHE_SIZE; i++) { 398019d6b8fSAnthony Liguori if (s->l2_cache_counts[i] < min_count) { 399019d6b8fSAnthony Liguori min_count = s->l2_cache_counts[i]; 400019d6b8fSAnthony Liguori min_index = i; 401019d6b8fSAnthony Liguori } 402019d6b8fSAnthony Liguori } 403019d6b8fSAnthony Liguori l2_table = s->l2_cache + (min_index << s->l2_bits); 404019d6b8fSAnthony Liguori if (new_l2_table) { 405019d6b8fSAnthony Liguori memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); 406d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table, 4075e5557d9SKevin Wolf s->l2_size * sizeof(uint64_t)) < 0) 408019d6b8fSAnthony Liguori return 0; 409019d6b8fSAnthony Liguori } else { 410cf2ab8fcSKevin Wolf if (bdrv_pread(bs->file, l2_offset, l2_table, 4119a4f4c31SKevin Wolf s->l2_size * sizeof(uint64_t)) != 412019d6b8fSAnthony Liguori s->l2_size * sizeof(uint64_t)) 413019d6b8fSAnthony Liguori return 0; 414019d6b8fSAnthony Liguori } 415019d6b8fSAnthony Liguori s->l2_cache_offsets[min_index] = l2_offset; 416019d6b8fSAnthony Liguori s->l2_cache_counts[min_index] = 1; 417019d6b8fSAnthony Liguori found: 418019d6b8fSAnthony Liguori l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); 419019d6b8fSAnthony Liguori cluster_offset = be64_to_cpu(l2_table[l2_index]); 420019d6b8fSAnthony Liguori if (!cluster_offset || 421019d6b8fSAnthony Liguori ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { 422019d6b8fSAnthony Liguori if (!allocate) 423019d6b8fSAnthony Liguori return 0; 424019d6b8fSAnthony Liguori /* allocate a new cluster */ 425019d6b8fSAnthony Liguori if ((cluster_offset & QCOW_OFLAG_COMPRESSED) && 426019d6b8fSAnthony Liguori (n_end - n_start) < s->cluster_sectors) { 427019d6b8fSAnthony Liguori /* if the cluster is already compressed, we must 428019d6b8fSAnthony Liguori decompress it in the case it is not completely 429019d6b8fSAnthony Liguori overwritten */ 43066f82ceeSKevin Wolf if (decompress_cluster(bs, cluster_offset) < 0) 431019d6b8fSAnthony Liguori return 0; 4329a4f4c31SKevin Wolf cluster_offset = bdrv_getlength(bs->file->bs); 433019d6b8fSAnthony Liguori cluster_offset = (cluster_offset + s->cluster_size - 1) & 434019d6b8fSAnthony Liguori ~(s->cluster_size - 1); 435019d6b8fSAnthony Liguori /* write the cluster content */ 436d9ca2ea2SKevin Wolf if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, 4379a4f4c31SKevin Wolf s->cluster_size) != 438019d6b8fSAnthony Liguori s->cluster_size) 439019d6b8fSAnthony Liguori return -1; 440019d6b8fSAnthony Liguori } else { 4419a4f4c31SKevin Wolf cluster_offset = bdrv_getlength(bs->file->bs); 442019d6b8fSAnthony Liguori if (allocate == 1) { 443019d6b8fSAnthony Liguori /* round to cluster size */ 444019d6b8fSAnthony Liguori cluster_offset = (cluster_offset + s->cluster_size - 1) & 445019d6b8fSAnthony Liguori ~(s->cluster_size - 1); 4467ea37c30SMax Reitz bdrv_truncate(bs->file, cluster_offset + s->cluster_size, 4477ea37c30SMax Reitz PREALLOC_MODE_OFF, NULL); 448019d6b8fSAnthony Liguori /* if encrypted, we must initialize the cluster 449019d6b8fSAnthony Liguori content which won't be written */ 4508336aafaSDaniel P. Berrange if (bs->encrypted && 451019d6b8fSAnthony Liguori (n_end - n_start) < s->cluster_sectors) { 452019d6b8fSAnthony Liguori uint64_t start_sect; 453d85f4222SDaniel P. Berrange assert(s->crypto); 454019d6b8fSAnthony Liguori start_sect = (offset & ~(s->cluster_size - 1)) >> 9; 455019d6b8fSAnthony Liguori for(i = 0; i < s->cluster_sectors; i++) { 456019d6b8fSAnthony Liguori if (i < n_start || i >= n_end) { 4571fad1f94SDaniel P. Berrange memset(s->cluster_data, 0x00, 512); 458d85f4222SDaniel P. Berrange if (qcrypto_block_encrypt(s->crypto, start_sect + i, 459d85f4222SDaniel P. Berrange s->cluster_data, 460d85f4222SDaniel P. Berrange BDRV_SECTOR_SIZE, 461*c3a8fe33SAlberto Garcia NULL) < 0) { 462f6fa64f6SDaniel P. Berrange errno = EIO; 463f6fa64f6SDaniel P. Berrange return -1; 464f6fa64f6SDaniel P. Berrange } 465d9ca2ea2SKevin Wolf if (bdrv_pwrite(bs->file, 4669a4f4c31SKevin Wolf cluster_offset + i * 512, 467019d6b8fSAnthony Liguori s->cluster_data, 512) != 512) 468019d6b8fSAnthony Liguori return -1; 469019d6b8fSAnthony Liguori } 470019d6b8fSAnthony Liguori } 471019d6b8fSAnthony Liguori } 472019d6b8fSAnthony Liguori } else if (allocate == 2) { 473019d6b8fSAnthony Liguori cluster_offset |= QCOW_OFLAG_COMPRESSED | 474019d6b8fSAnthony Liguori (uint64_t)compressed_size << (63 - s->cluster_bits); 475019d6b8fSAnthony Liguori } 476019d6b8fSAnthony Liguori } 477019d6b8fSAnthony Liguori /* update L2 table */ 478019d6b8fSAnthony Liguori tmp = cpu_to_be64(cluster_offset); 479019d6b8fSAnthony Liguori l2_table[l2_index] = tmp; 480d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp), 4815e5557d9SKevin Wolf &tmp, sizeof(tmp)) < 0) 482019d6b8fSAnthony Liguori return 0; 483019d6b8fSAnthony Liguori } 484019d6b8fSAnthony Liguori return cluster_offset; 485019d6b8fSAnthony Liguori } 486019d6b8fSAnthony Liguori 487b6b8a333SPaolo Bonzini static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs, 48867a0fd2aSFam Zheng int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file) 489019d6b8fSAnthony Liguori { 490019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 491019d6b8fSAnthony Liguori int index_in_cluster, n; 492019d6b8fSAnthony Liguori uint64_t cluster_offset; 493019d6b8fSAnthony Liguori 494f8a2e5e3SStefan Hajnoczi qemu_co_mutex_lock(&s->lock); 495019d6b8fSAnthony Liguori cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); 496f8a2e5e3SStefan Hajnoczi qemu_co_mutex_unlock(&s->lock); 497019d6b8fSAnthony Liguori index_in_cluster = sector_num & (s->cluster_sectors - 1); 498019d6b8fSAnthony Liguori n = s->cluster_sectors - index_in_cluster; 499019d6b8fSAnthony Liguori if (n > nb_sectors) 500019d6b8fSAnthony Liguori n = nb_sectors; 501019d6b8fSAnthony Liguori *pnum = n; 5024bc74be9SPaolo Bonzini if (!cluster_offset) { 5034bc74be9SPaolo Bonzini return 0; 5044bc74be9SPaolo Bonzini } 505d85f4222SDaniel P. Berrange if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypto) { 5064bc74be9SPaolo Bonzini return BDRV_BLOCK_DATA; 5074bc74be9SPaolo Bonzini } 5084bc74be9SPaolo Bonzini cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); 5093064bf6fSFam Zheng *file = bs->file->bs; 5104bc74be9SPaolo Bonzini return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset; 511019d6b8fSAnthony Liguori } 512019d6b8fSAnthony Liguori 513019d6b8fSAnthony Liguori static int decompress_buffer(uint8_t *out_buf, int out_buf_size, 514019d6b8fSAnthony Liguori const uint8_t *buf, int buf_size) 515019d6b8fSAnthony Liguori { 516019d6b8fSAnthony Liguori z_stream strm1, *strm = &strm1; 517019d6b8fSAnthony Liguori int ret, out_len; 518019d6b8fSAnthony Liguori 519019d6b8fSAnthony Liguori memset(strm, 0, sizeof(*strm)); 520019d6b8fSAnthony Liguori 521019d6b8fSAnthony Liguori strm->next_in = (uint8_t *)buf; 522019d6b8fSAnthony Liguori strm->avail_in = buf_size; 523019d6b8fSAnthony Liguori strm->next_out = out_buf; 524019d6b8fSAnthony Liguori strm->avail_out = out_buf_size; 525019d6b8fSAnthony Liguori 526019d6b8fSAnthony Liguori ret = inflateInit2(strm, -12); 527019d6b8fSAnthony Liguori if (ret != Z_OK) 528019d6b8fSAnthony Liguori return -1; 529019d6b8fSAnthony Liguori ret = inflate(strm, Z_FINISH); 530019d6b8fSAnthony Liguori out_len = strm->next_out - out_buf; 531019d6b8fSAnthony Liguori if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || 532019d6b8fSAnthony Liguori out_len != out_buf_size) { 533019d6b8fSAnthony Liguori inflateEnd(strm); 534019d6b8fSAnthony Liguori return -1; 535019d6b8fSAnthony Liguori } 536019d6b8fSAnthony Liguori inflateEnd(strm); 537019d6b8fSAnthony Liguori return 0; 538019d6b8fSAnthony Liguori } 539019d6b8fSAnthony Liguori 54066f82ceeSKevin Wolf static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) 541019d6b8fSAnthony Liguori { 54266f82ceeSKevin Wolf BDRVQcowState *s = bs->opaque; 543019d6b8fSAnthony Liguori int ret, csize; 544019d6b8fSAnthony Liguori uint64_t coffset; 545019d6b8fSAnthony Liguori 546019d6b8fSAnthony Liguori coffset = cluster_offset & s->cluster_offset_mask; 547019d6b8fSAnthony Liguori if (s->cluster_cache_offset != coffset) { 548019d6b8fSAnthony Liguori csize = cluster_offset >> (63 - s->cluster_bits); 549019d6b8fSAnthony Liguori csize &= (s->cluster_size - 1); 550cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize); 551019d6b8fSAnthony Liguori if (ret != csize) 552019d6b8fSAnthony Liguori return -1; 553019d6b8fSAnthony Liguori if (decompress_buffer(s->cluster_cache, s->cluster_size, 554019d6b8fSAnthony Liguori s->cluster_data, csize) < 0) { 555019d6b8fSAnthony Liguori return -1; 556019d6b8fSAnthony Liguori } 557019d6b8fSAnthony Liguori s->cluster_cache_offset = coffset; 558019d6b8fSAnthony Liguori } 559019d6b8fSAnthony Liguori return 0; 560019d6b8fSAnthony Liguori } 561019d6b8fSAnthony Liguori 562a968168cSDong Xu Wang static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num, 56327deebe8SFrediano Ziglio int nb_sectors, QEMUIOVector *qiov) 564ad53089bSChristoph Hellwig { 565019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 566019d6b8fSAnthony Liguori int index_in_cluster; 56727deebe8SFrediano Ziglio int ret = 0, n; 56843ca85b5SFrediano Ziglio uint64_t cluster_offset; 569430bbaaaSFrediano Ziglio struct iovec hd_iov; 570430bbaaaSFrediano Ziglio QEMUIOVector hd_qiov; 57127deebe8SFrediano Ziglio uint8_t *buf; 57227deebe8SFrediano Ziglio void *orig_buf; 573019d6b8fSAnthony Liguori 57427deebe8SFrediano Ziglio if (qiov->niov > 1) { 5750df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size); 5760df93305SKevin Wolf if (buf == NULL) { 5770df93305SKevin Wolf return -ENOMEM; 5780df93305SKevin Wolf } 57927deebe8SFrediano Ziglio } else { 58027deebe8SFrediano Ziglio orig_buf = NULL; 58127deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base; 582019d6b8fSAnthony Liguori } 583019d6b8fSAnthony Liguori 58427deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 58527deebe8SFrediano Ziglio 58627deebe8SFrediano Ziglio while (nb_sectors != 0) { 58743ca85b5SFrediano Ziglio /* prepare next request */ 58827deebe8SFrediano Ziglio cluster_offset = get_cluster_offset(bs, sector_num << 9, 589019d6b8fSAnthony Liguori 0, 0, 0, 0); 59027deebe8SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 591430bbaaaSFrediano Ziglio n = s->cluster_sectors - index_in_cluster; 59227deebe8SFrediano Ziglio if (n > nb_sectors) { 59327deebe8SFrediano Ziglio n = nb_sectors; 594430bbaaaSFrediano Ziglio } 595019d6b8fSAnthony Liguori 596430bbaaaSFrediano Ziglio if (!cluster_offset) { 597760e0063SKevin Wolf if (bs->backing) { 598019d6b8fSAnthony Liguori /* read from the base image */ 59927deebe8SFrediano Ziglio hd_iov.iov_base = (void *)buf; 600430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 601430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 60252b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 60328b04a8fSKevin Wolf ret = bdrv_co_readv(bs->backing, sector_num, n, &hd_qiov); 60452b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 60552b8eb60SKevin Wolf if (ret < 0) { 60627deebe8SFrediano Ziglio goto fail; 6075614c188SStefan Weil } 608019d6b8fSAnthony Liguori } else { 609019d6b8fSAnthony Liguori /* Note: in this case, no need to wait */ 61027deebe8SFrediano Ziglio memset(buf, 0, 512 * n); 611019d6b8fSAnthony Liguori } 612430bbaaaSFrediano Ziglio } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { 613019d6b8fSAnthony Liguori /* add AIO support for compressed blocks ? */ 614430bbaaaSFrediano Ziglio if (decompress_cluster(bs, cluster_offset) < 0) { 61527deebe8SFrediano Ziglio goto fail; 6165614c188SStefan Weil } 61727deebe8SFrediano Ziglio memcpy(buf, 618430bbaaaSFrediano Ziglio s->cluster_cache + index_in_cluster * 512, 512 * n); 619019d6b8fSAnthony Liguori } else { 620430bbaaaSFrediano Ziglio if ((cluster_offset & 511) != 0) { 62127deebe8SFrediano Ziglio goto fail; 622019d6b8fSAnthony Liguori } 62327deebe8SFrediano Ziglio hd_iov.iov_base = (void *)buf; 624430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 625430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 62652b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 62728b04a8fSKevin Wolf ret = bdrv_co_readv(bs->file, 628430bbaaaSFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 629430bbaaaSFrediano Ziglio n, &hd_qiov); 63052b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 63152b8eb60SKevin Wolf if (ret < 0) { 63227deebe8SFrediano Ziglio break; 633019d6b8fSAnthony Liguori } 6348336aafaSDaniel P. Berrange if (bs->encrypted) { 635d85f4222SDaniel P. Berrange assert(s->crypto); 636d85f4222SDaniel P. Berrange if (qcrypto_block_decrypt(s->crypto, sector_num, buf, 637*c3a8fe33SAlberto Garcia n * BDRV_SECTOR_SIZE, NULL) < 0) { 638f6fa64f6SDaniel P. Berrange goto fail; 639f6fa64f6SDaniel P. Berrange } 64043ca85b5SFrediano Ziglio } 64143ca85b5SFrediano Ziglio } 64227deebe8SFrediano Ziglio ret = 0; 64343ca85b5SFrediano Ziglio 64427deebe8SFrediano Ziglio nb_sectors -= n; 64527deebe8SFrediano Ziglio sector_num += n; 64627deebe8SFrediano Ziglio buf += n * 512; 64752b8eb60SKevin Wolf } 648019d6b8fSAnthony Liguori 64927deebe8SFrediano Ziglio done: 65052b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 65152b8eb60SKevin Wolf 65227deebe8SFrediano Ziglio if (qiov->niov > 1) { 65303396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size); 65427deebe8SFrediano Ziglio qemu_vfree(orig_buf); 655019d6b8fSAnthony Liguori } 65652b8eb60SKevin Wolf 65752b8eb60SKevin Wolf return ret; 65827deebe8SFrediano Ziglio 65927deebe8SFrediano Ziglio fail: 66027deebe8SFrediano Ziglio ret = -EIO; 66127deebe8SFrediano Ziglio goto done; 662019d6b8fSAnthony Liguori } 663019d6b8fSAnthony Liguori 664a968168cSDong Xu Wang static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num, 66527deebe8SFrediano Ziglio int nb_sectors, QEMUIOVector *qiov) 666019d6b8fSAnthony Liguori { 667019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 668019d6b8fSAnthony Liguori int index_in_cluster; 669019d6b8fSAnthony Liguori uint64_t cluster_offset; 67027deebe8SFrediano Ziglio int ret = 0, n; 671430bbaaaSFrediano Ziglio struct iovec hd_iov; 672430bbaaaSFrediano Ziglio QEMUIOVector hd_qiov; 67327deebe8SFrediano Ziglio uint8_t *buf; 67427deebe8SFrediano Ziglio void *orig_buf; 675019d6b8fSAnthony Liguori 67627deebe8SFrediano Ziglio s->cluster_cache_offset = -1; /* disable compressed cache */ 67727deebe8SFrediano Ziglio 6781fad1f94SDaniel P. Berrange /* We must always copy the iov when encrypting, so we 6791fad1f94SDaniel P. Berrange * don't modify the original data buffer during encryption */ 6801fad1f94SDaniel P. Berrange if (bs->encrypted || qiov->niov > 1) { 6810df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size); 6820df93305SKevin Wolf if (buf == NULL) { 6830df93305SKevin Wolf return -ENOMEM; 6840df93305SKevin Wolf } 685d5e6b161SMichael Tokarev qemu_iovec_to_buf(qiov, 0, buf, qiov->size); 68627deebe8SFrediano Ziglio } else { 68727deebe8SFrediano Ziglio orig_buf = NULL; 68827deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base; 689019d6b8fSAnthony Liguori } 690019d6b8fSAnthony Liguori 69127deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 69227deebe8SFrediano Ziglio 69327deebe8SFrediano Ziglio while (nb_sectors != 0) { 69427deebe8SFrediano Ziglio 69527deebe8SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 696430bbaaaSFrediano Ziglio n = s->cluster_sectors - index_in_cluster; 69727deebe8SFrediano Ziglio if (n > nb_sectors) { 69827deebe8SFrediano Ziglio n = nb_sectors; 699430bbaaaSFrediano Ziglio } 70027deebe8SFrediano Ziglio cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, 701019d6b8fSAnthony Liguori index_in_cluster, 702430bbaaaSFrediano Ziglio index_in_cluster + n); 703019d6b8fSAnthony Liguori if (!cluster_offset || (cluster_offset & 511) != 0) { 70427deebe8SFrediano Ziglio ret = -EIO; 70527deebe8SFrediano Ziglio break; 706019d6b8fSAnthony Liguori } 7078336aafaSDaniel P. Berrange if (bs->encrypted) { 708d85f4222SDaniel P. Berrange assert(s->crypto); 709d85f4222SDaniel P. Berrange if (qcrypto_block_encrypt(s->crypto, sector_num, buf, 710*c3a8fe33SAlberto Garcia n * BDRV_SECTOR_SIZE, NULL) < 0) { 711f6fa64f6SDaniel P. Berrange ret = -EIO; 712f6fa64f6SDaniel P. Berrange break; 713f6fa64f6SDaniel P. Berrange } 714019d6b8fSAnthony Liguori } 715019d6b8fSAnthony Liguori 7161fad1f94SDaniel P. Berrange hd_iov.iov_base = (void *)buf; 717430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 718430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 71952b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 72025ec177dSKevin Wolf ret = bdrv_co_writev(bs->file, 721019d6b8fSAnthony Liguori (cluster_offset >> 9) + index_in_cluster, 722430bbaaaSFrediano Ziglio n, &hd_qiov); 72352b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 72452b8eb60SKevin Wolf if (ret < 0) { 72527deebe8SFrediano Ziglio break; 7265614c188SStefan Weil } 72727deebe8SFrediano Ziglio ret = 0; 72843ca85b5SFrediano Ziglio 72927deebe8SFrediano Ziglio nb_sectors -= n; 73027deebe8SFrediano Ziglio sector_num += n; 73127deebe8SFrediano Ziglio buf += n * 512; 732019d6b8fSAnthony Liguori } 73352b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 734019d6b8fSAnthony Liguori 73527deebe8SFrediano Ziglio qemu_vfree(orig_buf); 736b11a24deSKevin Wolf 73752b8eb60SKevin Wolf return ret; 738019d6b8fSAnthony Liguori } 739019d6b8fSAnthony Liguori 740019d6b8fSAnthony Liguori static void qcow_close(BlockDriverState *bs) 741019d6b8fSAnthony Liguori { 742019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 743fd9f102cSKevin Wolf 744d85f4222SDaniel P. Berrange qcrypto_block_free(s->crypto); 745d85f4222SDaniel P. Berrange s->crypto = NULL; 7467267c094SAnthony Liguori g_free(s->l1_table); 7470df93305SKevin Wolf qemu_vfree(s->l2_cache); 7487267c094SAnthony Liguori g_free(s->cluster_cache); 7497267c094SAnthony Liguori g_free(s->cluster_data); 750fd9f102cSKevin Wolf 751fd9f102cSKevin Wolf migrate_del_blocker(s->migration_blocker); 752fd9f102cSKevin Wolf error_free(s->migration_blocker); 753019d6b8fSAnthony Liguori } 754019d6b8fSAnthony Liguori 75516d12159SChunyan Liu static int qcow_create(const char *filename, QemuOpts *opts, Error **errp) 756019d6b8fSAnthony Liguori { 7572b16c9ffSLi Zhi Hui int header_size, backing_filename_len, l1_size, shift, i; 758019d6b8fSAnthony Liguori QCowHeader header; 7592b16c9ffSLi Zhi Hui uint8_t *tmp; 7600e7e1989SKevin Wolf int64_t total_size = 0; 76116d12159SChunyan Liu char *backing_file = NULL; 76234b5d2c6SMax Reitz Error *local_err = NULL; 7633e1a8134SKirill A. Shutemov int ret; 7646af40160SKevin Wolf BlockBackend *qcow_blk; 7650696ae2cSDaniel P. Berrange char *encryptfmt = NULL; 766d85f4222SDaniel P. Berrange QDict *options; 767d85f4222SDaniel P. Berrange QDict *encryptopts = NULL; 768d85f4222SDaniel P. Berrange QCryptoBlockCreateOptions *crypto_opts = NULL; 769d85f4222SDaniel P. Berrange QCryptoBlock *crypto = NULL; 7700e7e1989SKevin Wolf 7710e7e1989SKevin Wolf /* Read out options */ 772180e9526SHu Tao total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 773c2eb918eSHu Tao BDRV_SECTOR_SIZE); 7746aa837f7SDaniel P. Berrange if (total_size == 0) { 7756aa837f7SDaniel P. Berrange error_setg(errp, "Image size is too small, cannot be zero length"); 7766aa837f7SDaniel P. Berrange ret = -EINVAL; 7776aa837f7SDaniel P. Berrange goto cleanup; 7786aa837f7SDaniel P. Berrange } 7796aa837f7SDaniel P. Berrange 78016d12159SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 7810cb8d47bSDaniel P. Berrange encryptfmt = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); 7820cb8d47bSDaniel P. Berrange if (encryptfmt) { 7830cb8d47bSDaniel P. Berrange if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) { 7840cb8d47bSDaniel P. Berrange error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and " 7850cb8d47bSDaniel P. Berrange BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive"); 7860cb8d47bSDaniel P. Berrange ret = -EINVAL; 7870cb8d47bSDaniel P. Berrange goto cleanup; 7880cb8d47bSDaniel P. Berrange } 7890cb8d47bSDaniel P. Berrange } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { 7900696ae2cSDaniel P. Berrange encryptfmt = g_strdup("aes"); 7910e7e1989SKevin Wolf } 792019d6b8fSAnthony Liguori 793c282e1fdSChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 7942b16c9ffSLi Zhi Hui if (ret < 0) { 795b6d5066dSPaolo Bonzini error_propagate(errp, local_err); 79616d12159SChunyan Liu goto cleanup; 7972b16c9ffSLi Zhi Hui } 7982b16c9ffSLi Zhi Hui 799efaa7c4eSMax Reitz qcow_blk = blk_new_open(filename, NULL, NULL, 80055880601SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 80155880601SKevin Wolf &local_err); 8026af40160SKevin Wolf if (qcow_blk == NULL) { 803b6d5066dSPaolo Bonzini error_propagate(errp, local_err); 8046af40160SKevin Wolf ret = -EIO; 80516d12159SChunyan Liu goto cleanup; 8062b16c9ffSLi Zhi Hui } 8072b16c9ffSLi Zhi Hui 8086af40160SKevin Wolf blk_set_allow_write_beyond_eof(qcow_blk, true); 8096af40160SKevin Wolf 8103a691c50SMax Reitz ret = blk_truncate(qcow_blk, 0, PREALLOC_MODE_OFF, errp); 8112b16c9ffSLi Zhi Hui if (ret < 0) { 8122b16c9ffSLi Zhi Hui goto exit; 8132b16c9ffSLi Zhi Hui } 8142b16c9ffSLi Zhi Hui 815019d6b8fSAnthony Liguori memset(&header, 0, sizeof(header)); 816019d6b8fSAnthony Liguori header.magic = cpu_to_be32(QCOW_MAGIC); 817019d6b8fSAnthony Liguori header.version = cpu_to_be32(QCOW_VERSION); 818180e9526SHu Tao header.size = cpu_to_be64(total_size); 819019d6b8fSAnthony Liguori header_size = sizeof(header); 820019d6b8fSAnthony Liguori backing_filename_len = 0; 821019d6b8fSAnthony Liguori if (backing_file) { 822019d6b8fSAnthony Liguori if (strcmp(backing_file, "fat:")) { 823019d6b8fSAnthony Liguori header.backing_file_offset = cpu_to_be64(header_size); 824019d6b8fSAnthony Liguori backing_filename_len = strlen(backing_file); 825019d6b8fSAnthony Liguori header.backing_file_size = cpu_to_be32(backing_filename_len); 826019d6b8fSAnthony Liguori header_size += backing_filename_len; 827019d6b8fSAnthony Liguori } else { 828019d6b8fSAnthony Liguori /* special backing file for vvfat */ 829272545cfSPeter Maydell g_free(backing_file); 830019d6b8fSAnthony Liguori backing_file = NULL; 831019d6b8fSAnthony Liguori } 832019d6b8fSAnthony Liguori header.cluster_bits = 9; /* 512 byte cluster to avoid copying 833dc6fb73dSDeepak Kathayat unmodified sectors */ 834019d6b8fSAnthony Liguori header.l2_bits = 12; /* 32 KB L2 tables */ 835019d6b8fSAnthony Liguori } else { 836019d6b8fSAnthony Liguori header.cluster_bits = 12; /* 4 KB clusters */ 837019d6b8fSAnthony Liguori header.l2_bits = 9; /* 4 KB L2 tables */ 838019d6b8fSAnthony Liguori } 839019d6b8fSAnthony Liguori header_size = (header_size + 7) & ~7; 840019d6b8fSAnthony Liguori shift = header.cluster_bits + header.l2_bits; 841180e9526SHu Tao l1_size = (total_size + (1LL << shift) - 1) >> shift; 842019d6b8fSAnthony Liguori 843019d6b8fSAnthony Liguori header.l1_table_offset = cpu_to_be64(header_size); 844d85f4222SDaniel P. Berrange 845d85f4222SDaniel P. Berrange options = qemu_opts_to_qdict(opts, NULL); 846d85f4222SDaniel P. Berrange qdict_extract_subqdict(options, &encryptopts, "encrypt."); 847d85f4222SDaniel P. Berrange QDECREF(options); 8480cb8d47bSDaniel P. Berrange if (encryptfmt) { 8490cb8d47bSDaniel P. Berrange if (!g_str_equal(encryptfmt, "aes")) { 8500cb8d47bSDaniel P. Berrange error_setg(errp, "Unknown encryption format '%s', expected 'aes'", 8510cb8d47bSDaniel P. Berrange encryptfmt); 8520cb8d47bSDaniel P. Berrange ret = -EINVAL; 8530cb8d47bSDaniel P. Berrange goto exit; 8540cb8d47bSDaniel P. Berrange } 855019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); 856d85f4222SDaniel P. Berrange 857d85f4222SDaniel P. Berrange crypto_opts = block_crypto_create_opts_init( 858d85f4222SDaniel P. Berrange Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); 859d85f4222SDaniel P. Berrange if (!crypto_opts) { 860d85f4222SDaniel P. Berrange ret = -EINVAL; 861d85f4222SDaniel P. Berrange goto exit; 862d85f4222SDaniel P. Berrange } 863d85f4222SDaniel P. Berrange 8641cd9a787SDaniel P. Berrange crypto = qcrypto_block_create(crypto_opts, "encrypt.", 8651cd9a787SDaniel P. Berrange NULL, NULL, NULL, errp); 866d85f4222SDaniel P. Berrange if (!crypto) { 867d85f4222SDaniel P. Berrange ret = -EINVAL; 868d85f4222SDaniel P. Berrange goto exit; 869d85f4222SDaniel P. Berrange } 870019d6b8fSAnthony Liguori } else { 871019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); 872019d6b8fSAnthony Liguori } 873019d6b8fSAnthony Liguori 874019d6b8fSAnthony Liguori /* write all the data */ 8758341f00dSEric Blake ret = blk_pwrite(qcow_blk, 0, &header, sizeof(header), 0); 8763e1a8134SKirill A. Shutemov if (ret != sizeof(header)) { 8773e1a8134SKirill A. Shutemov goto exit; 8783e1a8134SKirill A. Shutemov } 8793e1a8134SKirill A. Shutemov 880019d6b8fSAnthony Liguori if (backing_file) { 8816af40160SKevin Wolf ret = blk_pwrite(qcow_blk, sizeof(header), 8828341f00dSEric Blake backing_file, backing_filename_len, 0); 8833e1a8134SKirill A. Shutemov if (ret != backing_filename_len) { 8843e1a8134SKirill A. Shutemov goto exit; 885019d6b8fSAnthony Liguori } 8863e1a8134SKirill A. Shutemov } 8873e1a8134SKirill A. Shutemov 8882b16c9ffSLi Zhi Hui tmp = g_malloc0(BDRV_SECTOR_SIZE); 889d737b78cSLaurent Vivier for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE); 890d737b78cSLaurent Vivier i++) { 8918341f00dSEric Blake ret = blk_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i, 8928341f00dSEric Blake tmp, BDRV_SECTOR_SIZE, 0); 8932b16c9ffSLi Zhi Hui if (ret != BDRV_SECTOR_SIZE) { 8942b16c9ffSLi Zhi Hui g_free(tmp); 8952b16c9ffSLi Zhi Hui goto exit; 8962b16c9ffSLi Zhi Hui } 8972b16c9ffSLi Zhi Hui } 8982b16c9ffSLi Zhi Hui 8992b16c9ffSLi Zhi Hui g_free(tmp); 9003e1a8134SKirill A. Shutemov ret = 0; 9013e1a8134SKirill A. Shutemov exit: 9026af40160SKevin Wolf blk_unref(qcow_blk); 90316d12159SChunyan Liu cleanup: 904d85f4222SDaniel P. Berrange QDECREF(encryptopts); 9050696ae2cSDaniel P. Berrange g_free(encryptfmt); 906d85f4222SDaniel P. Berrange qcrypto_block_free(crypto); 907d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockCreateOptions(crypto_opts); 90816d12159SChunyan Liu g_free(backing_file); 9093e1a8134SKirill A. Shutemov return ret; 910019d6b8fSAnthony Liguori } 911019d6b8fSAnthony Liguori 912019d6b8fSAnthony Liguori static int qcow_make_empty(BlockDriverState *bs) 913019d6b8fSAnthony Liguori { 914019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 915019d6b8fSAnthony Liguori uint32_t l1_length = s->l1_size * sizeof(uint64_t); 916019d6b8fSAnthony Liguori int ret; 917019d6b8fSAnthony Liguori 918019d6b8fSAnthony Liguori memset(s->l1_table, 0, l1_length); 919d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table, 9205e5557d9SKevin Wolf l1_length) < 0) 921019d6b8fSAnthony Liguori return -1; 9227ea37c30SMax Reitz ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, 9237ea37c30SMax Reitz PREALLOC_MODE_OFF, NULL); 924019d6b8fSAnthony Liguori if (ret < 0) 925019d6b8fSAnthony Liguori return ret; 926019d6b8fSAnthony Liguori 927019d6b8fSAnthony Liguori memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); 928019d6b8fSAnthony Liguori memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); 929019d6b8fSAnthony Liguori memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); 930019d6b8fSAnthony Liguori 931019d6b8fSAnthony Liguori return 0; 932019d6b8fSAnthony Liguori } 933019d6b8fSAnthony Liguori 934019d6b8fSAnthony Liguori /* XXX: put compressed sectors first, then all the cluster aligned 935019d6b8fSAnthony Liguori tables to avoid losing bytes in alignment */ 936f2b95a12SPavel Butsykin static coroutine_fn int 937f2b95a12SPavel Butsykin qcow_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, 938f2b95a12SPavel Butsykin uint64_t bytes, QEMUIOVector *qiov) 939019d6b8fSAnthony Liguori { 940019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 941f2b95a12SPavel Butsykin QEMUIOVector hd_qiov; 942f2b95a12SPavel Butsykin struct iovec iov; 943019d6b8fSAnthony Liguori z_stream strm; 944019d6b8fSAnthony Liguori int ret, out_len; 945f2b95a12SPavel Butsykin uint8_t *buf, *out_buf; 946019d6b8fSAnthony Liguori uint64_t cluster_offset; 947019d6b8fSAnthony Liguori 948f2b95a12SPavel Butsykin buf = qemu_blockalign(bs, s->cluster_size); 949655923dfSPavel Butsykin if (bytes != s->cluster_size) { 950655923dfSPavel Butsykin if (bytes > s->cluster_size || 951655923dfSPavel Butsykin offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS) 952655923dfSPavel Butsykin { 953655923dfSPavel Butsykin qemu_vfree(buf); 954655923dfSPavel Butsykin return -EINVAL; 955655923dfSPavel Butsykin } 956655923dfSPavel Butsykin /* Zero-pad last write if image size is not cluster aligned */ 957655923dfSPavel Butsykin memset(buf + bytes, 0, s->cluster_size - bytes); 958655923dfSPavel Butsykin } 959f2b95a12SPavel Butsykin qemu_iovec_to_buf(qiov, 0, buf, qiov->size); 960019d6b8fSAnthony Liguori 961ebf7bba0SVladimir Sementsov-Ogievskiy out_buf = g_malloc(s->cluster_size); 962019d6b8fSAnthony Liguori 963019d6b8fSAnthony Liguori /* best compression, small window, no zlib header */ 964019d6b8fSAnthony Liguori memset(&strm, 0, sizeof(strm)); 965019d6b8fSAnthony Liguori ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, 966019d6b8fSAnthony Liguori Z_DEFLATED, -12, 967019d6b8fSAnthony Liguori 9, Z_DEFAULT_STRATEGY); 968019d6b8fSAnthony Liguori if (ret != 0) { 96964ebe71aSKevin Wolf ret = -EINVAL; 97064ebe71aSKevin Wolf goto fail; 971019d6b8fSAnthony Liguori } 972019d6b8fSAnthony Liguori 973019d6b8fSAnthony Liguori strm.avail_in = s->cluster_size; 974019d6b8fSAnthony Liguori strm.next_in = (uint8_t *)buf; 975019d6b8fSAnthony Liguori strm.avail_out = s->cluster_size; 976019d6b8fSAnthony Liguori strm.next_out = out_buf; 977019d6b8fSAnthony Liguori 978019d6b8fSAnthony Liguori ret = deflate(&strm, Z_FINISH); 979019d6b8fSAnthony Liguori if (ret != Z_STREAM_END && ret != Z_OK) { 980019d6b8fSAnthony Liguori deflateEnd(&strm); 98164ebe71aSKevin Wolf ret = -EINVAL; 98264ebe71aSKevin Wolf goto fail; 983019d6b8fSAnthony Liguori } 984019d6b8fSAnthony Liguori out_len = strm.next_out - out_buf; 985019d6b8fSAnthony Liguori 986019d6b8fSAnthony Liguori deflateEnd(&strm); 987019d6b8fSAnthony Liguori 988019d6b8fSAnthony Liguori if (ret != Z_STREAM_END || out_len >= s->cluster_size) { 989019d6b8fSAnthony Liguori /* could not compress: write normal cluster */ 990f2b95a12SPavel Butsykin ret = qcow_co_writev(bs, offset >> BDRV_SECTOR_BITS, 991f2b95a12SPavel Butsykin bytes >> BDRV_SECTOR_BITS, qiov); 99264ebe71aSKevin Wolf if (ret < 0) { 99364ebe71aSKevin Wolf goto fail; 99464ebe71aSKevin Wolf } 995f2b95a12SPavel Butsykin goto success; 996f2b95a12SPavel Butsykin } 997f2b95a12SPavel Butsykin qemu_co_mutex_lock(&s->lock); 998f2b95a12SPavel Butsykin cluster_offset = get_cluster_offset(bs, offset, 2, out_len, 0, 0); 999f2b95a12SPavel Butsykin qemu_co_mutex_unlock(&s->lock); 100064ebe71aSKevin Wolf if (cluster_offset == 0) { 100164ebe71aSKevin Wolf ret = -EIO; 100264ebe71aSKevin Wolf goto fail; 100364ebe71aSKevin Wolf } 1004019d6b8fSAnthony Liguori cluster_offset &= s->cluster_offset_mask; 1005f2b95a12SPavel Butsykin 1006f2b95a12SPavel Butsykin iov = (struct iovec) { 1007f2b95a12SPavel Butsykin .iov_base = out_buf, 1008f2b95a12SPavel Butsykin .iov_len = out_len, 1009f2b95a12SPavel Butsykin }; 1010f2b95a12SPavel Butsykin qemu_iovec_init_external(&hd_qiov, &iov, 1); 1011f2b95a12SPavel Butsykin ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0); 101264ebe71aSKevin Wolf if (ret < 0) { 101364ebe71aSKevin Wolf goto fail; 1014019d6b8fSAnthony Liguori } 1015f2b95a12SPavel Butsykin success: 101664ebe71aSKevin Wolf ret = 0; 101764ebe71aSKevin Wolf fail: 1018f2b95a12SPavel Butsykin qemu_vfree(buf); 10197267c094SAnthony Liguori g_free(out_buf); 102064ebe71aSKevin Wolf return ret; 1021019d6b8fSAnthony Liguori } 1022019d6b8fSAnthony Liguori 1023019d6b8fSAnthony Liguori static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1024019d6b8fSAnthony Liguori { 1025019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 1026019d6b8fSAnthony Liguori bdi->cluster_size = s->cluster_size; 1027019d6b8fSAnthony Liguori return 0; 1028019d6b8fSAnthony Liguori } 1029019d6b8fSAnthony Liguori 103016d12159SChunyan Liu static QemuOptsList qcow_create_opts = { 103116d12159SChunyan Liu .name = "qcow-create-opts", 103216d12159SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head), 103316d12159SChunyan Liu .desc = { 1034db08adf5SKevin Wolf { 1035db08adf5SKevin Wolf .name = BLOCK_OPT_SIZE, 103616d12159SChunyan Liu .type = QEMU_OPT_SIZE, 1037db08adf5SKevin Wolf .help = "Virtual disk size" 1038db08adf5SKevin Wolf }, 1039db08adf5SKevin Wolf { 1040db08adf5SKevin Wolf .name = BLOCK_OPT_BACKING_FILE, 104116d12159SChunyan Liu .type = QEMU_OPT_STRING, 1042db08adf5SKevin Wolf .help = "File name of a base image" 1043db08adf5SKevin Wolf }, 1044db08adf5SKevin Wolf { 1045db08adf5SKevin Wolf .name = BLOCK_OPT_ENCRYPT, 104616d12159SChunyan Liu .type = QEMU_OPT_BOOL, 10470cb8d47bSDaniel P. Berrange .help = "Encrypt the image with format 'aes'. (Deprecated " 10480cb8d47bSDaniel P. Berrange "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)", 10490cb8d47bSDaniel P. Berrange }, 10500cb8d47bSDaniel P. Berrange { 10510cb8d47bSDaniel P. Berrange .name = BLOCK_OPT_ENCRYPT_FORMAT, 10520cb8d47bSDaniel P. Berrange .type = QEMU_OPT_STRING, 10530cb8d47bSDaniel P. Berrange .help = "Encrypt the image, format choices: 'aes'", 1054db08adf5SKevin Wolf }, 1055d85f4222SDaniel P. Berrange BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."), 105616d12159SChunyan Liu { /* end of list */ } 105716d12159SChunyan Liu } 10580e7e1989SKevin Wolf }; 10590e7e1989SKevin Wolf 1060019d6b8fSAnthony Liguori static BlockDriver bdrv_qcow = { 1061019d6b8fSAnthony Liguori .format_name = "qcow", 1062019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVQcowState), 1063019d6b8fSAnthony Liguori .bdrv_probe = qcow_probe, 1064019d6b8fSAnthony Liguori .bdrv_open = qcow_open, 1065019d6b8fSAnthony Liguori .bdrv_close = qcow_close, 1066862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1067d177692eSJeff Cody .bdrv_reopen_prepare = qcow_reopen_prepare, 1068c282e1fdSChunyan Liu .bdrv_create = qcow_create, 10693ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 10708ee79e70SKevin Wolf .supports_backing = true, 1071c68b89acSKevin Wolf 107252b8eb60SKevin Wolf .bdrv_co_readv = qcow_co_readv, 107352b8eb60SKevin Wolf .bdrv_co_writev = qcow_co_writev, 1074b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = qcow_co_get_block_status, 1075c68b89acSKevin Wolf 1076c68b89acSKevin Wolf .bdrv_make_empty = qcow_make_empty, 1077f2b95a12SPavel Butsykin .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed, 1078019d6b8fSAnthony Liguori .bdrv_get_info = qcow_get_info, 10790e7e1989SKevin Wolf 108016d12159SChunyan Liu .create_opts = &qcow_create_opts, 1081019d6b8fSAnthony Liguori }; 1082019d6b8fSAnthony Liguori 1083019d6b8fSAnthony Liguori static void bdrv_qcow_init(void) 1084019d6b8fSAnthony Liguori { 1085019d6b8fSAnthony Liguori bdrv_register(&bdrv_qcow); 1086019d6b8fSAnthony Liguori } 1087019d6b8fSAnthony Liguori 1088019d6b8fSAnthony Liguori block_init(bdrv_qcow_init); 1089