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); 446*7ea37c30SMax Reitz bdrv_truncate(bs->file, cluster_offset + s->cluster_size, 447*7ea37c30SMax 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) { 457f6fa64f6SDaniel P. Berrange Error *err = NULL; 4581fad1f94SDaniel P. Berrange memset(s->cluster_data, 0x00, 512); 459d85f4222SDaniel P. Berrange if (qcrypto_block_encrypt(s->crypto, start_sect + i, 460d85f4222SDaniel P. Berrange s->cluster_data, 461d85f4222SDaniel P. Berrange BDRV_SECTOR_SIZE, 462d85f4222SDaniel P. Berrange &err) < 0) { 463f6fa64f6SDaniel P. Berrange error_free(err); 464f6fa64f6SDaniel P. Berrange errno = EIO; 465f6fa64f6SDaniel P. Berrange return -1; 466f6fa64f6SDaniel P. Berrange } 467d9ca2ea2SKevin Wolf if (bdrv_pwrite(bs->file, 4689a4f4c31SKevin Wolf cluster_offset + i * 512, 469019d6b8fSAnthony Liguori s->cluster_data, 512) != 512) 470019d6b8fSAnthony Liguori return -1; 471019d6b8fSAnthony Liguori } 472019d6b8fSAnthony Liguori } 473019d6b8fSAnthony Liguori } 474019d6b8fSAnthony Liguori } else if (allocate == 2) { 475019d6b8fSAnthony Liguori cluster_offset |= QCOW_OFLAG_COMPRESSED | 476019d6b8fSAnthony Liguori (uint64_t)compressed_size << (63 - s->cluster_bits); 477019d6b8fSAnthony Liguori } 478019d6b8fSAnthony Liguori } 479019d6b8fSAnthony Liguori /* update L2 table */ 480019d6b8fSAnthony Liguori tmp = cpu_to_be64(cluster_offset); 481019d6b8fSAnthony Liguori l2_table[l2_index] = tmp; 482d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp), 4835e5557d9SKevin Wolf &tmp, sizeof(tmp)) < 0) 484019d6b8fSAnthony Liguori return 0; 485019d6b8fSAnthony Liguori } 486019d6b8fSAnthony Liguori return cluster_offset; 487019d6b8fSAnthony Liguori } 488019d6b8fSAnthony Liguori 489b6b8a333SPaolo Bonzini static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs, 49067a0fd2aSFam Zheng int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file) 491019d6b8fSAnthony Liguori { 492019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 493019d6b8fSAnthony Liguori int index_in_cluster, n; 494019d6b8fSAnthony Liguori uint64_t cluster_offset; 495019d6b8fSAnthony Liguori 496f8a2e5e3SStefan Hajnoczi qemu_co_mutex_lock(&s->lock); 497019d6b8fSAnthony Liguori cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); 498f8a2e5e3SStefan Hajnoczi qemu_co_mutex_unlock(&s->lock); 499019d6b8fSAnthony Liguori index_in_cluster = sector_num & (s->cluster_sectors - 1); 500019d6b8fSAnthony Liguori n = s->cluster_sectors - index_in_cluster; 501019d6b8fSAnthony Liguori if (n > nb_sectors) 502019d6b8fSAnthony Liguori n = nb_sectors; 503019d6b8fSAnthony Liguori *pnum = n; 5044bc74be9SPaolo Bonzini if (!cluster_offset) { 5054bc74be9SPaolo Bonzini return 0; 5064bc74be9SPaolo Bonzini } 507d85f4222SDaniel P. Berrange if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypto) { 5084bc74be9SPaolo Bonzini return BDRV_BLOCK_DATA; 5094bc74be9SPaolo Bonzini } 5104bc74be9SPaolo Bonzini cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); 5113064bf6fSFam Zheng *file = bs->file->bs; 5124bc74be9SPaolo Bonzini return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset; 513019d6b8fSAnthony Liguori } 514019d6b8fSAnthony Liguori 515019d6b8fSAnthony Liguori static int decompress_buffer(uint8_t *out_buf, int out_buf_size, 516019d6b8fSAnthony Liguori const uint8_t *buf, int buf_size) 517019d6b8fSAnthony Liguori { 518019d6b8fSAnthony Liguori z_stream strm1, *strm = &strm1; 519019d6b8fSAnthony Liguori int ret, out_len; 520019d6b8fSAnthony Liguori 521019d6b8fSAnthony Liguori memset(strm, 0, sizeof(*strm)); 522019d6b8fSAnthony Liguori 523019d6b8fSAnthony Liguori strm->next_in = (uint8_t *)buf; 524019d6b8fSAnthony Liguori strm->avail_in = buf_size; 525019d6b8fSAnthony Liguori strm->next_out = out_buf; 526019d6b8fSAnthony Liguori strm->avail_out = out_buf_size; 527019d6b8fSAnthony Liguori 528019d6b8fSAnthony Liguori ret = inflateInit2(strm, -12); 529019d6b8fSAnthony Liguori if (ret != Z_OK) 530019d6b8fSAnthony Liguori return -1; 531019d6b8fSAnthony Liguori ret = inflate(strm, Z_FINISH); 532019d6b8fSAnthony Liguori out_len = strm->next_out - out_buf; 533019d6b8fSAnthony Liguori if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || 534019d6b8fSAnthony Liguori out_len != out_buf_size) { 535019d6b8fSAnthony Liguori inflateEnd(strm); 536019d6b8fSAnthony Liguori return -1; 537019d6b8fSAnthony Liguori } 538019d6b8fSAnthony Liguori inflateEnd(strm); 539019d6b8fSAnthony Liguori return 0; 540019d6b8fSAnthony Liguori } 541019d6b8fSAnthony Liguori 54266f82ceeSKevin Wolf static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) 543019d6b8fSAnthony Liguori { 54466f82ceeSKevin Wolf BDRVQcowState *s = bs->opaque; 545019d6b8fSAnthony Liguori int ret, csize; 546019d6b8fSAnthony Liguori uint64_t coffset; 547019d6b8fSAnthony Liguori 548019d6b8fSAnthony Liguori coffset = cluster_offset & s->cluster_offset_mask; 549019d6b8fSAnthony Liguori if (s->cluster_cache_offset != coffset) { 550019d6b8fSAnthony Liguori csize = cluster_offset >> (63 - s->cluster_bits); 551019d6b8fSAnthony Liguori csize &= (s->cluster_size - 1); 552cf2ab8fcSKevin Wolf ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize); 553019d6b8fSAnthony Liguori if (ret != csize) 554019d6b8fSAnthony Liguori return -1; 555019d6b8fSAnthony Liguori if (decompress_buffer(s->cluster_cache, s->cluster_size, 556019d6b8fSAnthony Liguori s->cluster_data, csize) < 0) { 557019d6b8fSAnthony Liguori return -1; 558019d6b8fSAnthony Liguori } 559019d6b8fSAnthony Liguori s->cluster_cache_offset = coffset; 560019d6b8fSAnthony Liguori } 561019d6b8fSAnthony Liguori return 0; 562019d6b8fSAnthony Liguori } 563019d6b8fSAnthony Liguori 564a968168cSDong Xu Wang static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num, 56527deebe8SFrediano Ziglio int nb_sectors, QEMUIOVector *qiov) 566ad53089bSChristoph Hellwig { 567019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 568019d6b8fSAnthony Liguori int index_in_cluster; 56927deebe8SFrediano Ziglio int ret = 0, n; 57043ca85b5SFrediano Ziglio uint64_t cluster_offset; 571430bbaaaSFrediano Ziglio struct iovec hd_iov; 572430bbaaaSFrediano Ziglio QEMUIOVector hd_qiov; 57327deebe8SFrediano Ziglio uint8_t *buf; 57427deebe8SFrediano Ziglio void *orig_buf; 575f6fa64f6SDaniel P. Berrange Error *err = NULL; 576019d6b8fSAnthony Liguori 57727deebe8SFrediano Ziglio if (qiov->niov > 1) { 5780df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size); 5790df93305SKevin Wolf if (buf == NULL) { 5800df93305SKevin Wolf return -ENOMEM; 5810df93305SKevin Wolf } 58227deebe8SFrediano Ziglio } else { 58327deebe8SFrediano Ziglio orig_buf = NULL; 58427deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base; 585019d6b8fSAnthony Liguori } 586019d6b8fSAnthony Liguori 58727deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 58827deebe8SFrediano Ziglio 58927deebe8SFrediano Ziglio while (nb_sectors != 0) { 59043ca85b5SFrediano Ziglio /* prepare next request */ 59127deebe8SFrediano Ziglio cluster_offset = get_cluster_offset(bs, sector_num << 9, 592019d6b8fSAnthony Liguori 0, 0, 0, 0); 59327deebe8SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 594430bbaaaSFrediano Ziglio n = s->cluster_sectors - index_in_cluster; 59527deebe8SFrediano Ziglio if (n > nb_sectors) { 59627deebe8SFrediano Ziglio n = nb_sectors; 597430bbaaaSFrediano Ziglio } 598019d6b8fSAnthony Liguori 599430bbaaaSFrediano Ziglio if (!cluster_offset) { 600760e0063SKevin Wolf if (bs->backing) { 601019d6b8fSAnthony Liguori /* read from the base image */ 60227deebe8SFrediano Ziglio hd_iov.iov_base = (void *)buf; 603430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 604430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 60552b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 60628b04a8fSKevin Wolf ret = bdrv_co_readv(bs->backing, sector_num, n, &hd_qiov); 60752b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 60852b8eb60SKevin Wolf if (ret < 0) { 60927deebe8SFrediano Ziglio goto fail; 6105614c188SStefan Weil } 611019d6b8fSAnthony Liguori } else { 612019d6b8fSAnthony Liguori /* Note: in this case, no need to wait */ 61327deebe8SFrediano Ziglio memset(buf, 0, 512 * n); 614019d6b8fSAnthony Liguori } 615430bbaaaSFrediano Ziglio } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { 616019d6b8fSAnthony Liguori /* add AIO support for compressed blocks ? */ 617430bbaaaSFrediano Ziglio if (decompress_cluster(bs, cluster_offset) < 0) { 61827deebe8SFrediano Ziglio goto fail; 6195614c188SStefan Weil } 62027deebe8SFrediano Ziglio memcpy(buf, 621430bbaaaSFrediano Ziglio s->cluster_cache + index_in_cluster * 512, 512 * n); 622019d6b8fSAnthony Liguori } else { 623430bbaaaSFrediano Ziglio if ((cluster_offset & 511) != 0) { 62427deebe8SFrediano Ziglio goto fail; 625019d6b8fSAnthony Liguori } 62627deebe8SFrediano Ziglio hd_iov.iov_base = (void *)buf; 627430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 628430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 62952b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 63028b04a8fSKevin Wolf ret = bdrv_co_readv(bs->file, 631430bbaaaSFrediano Ziglio (cluster_offset >> 9) + index_in_cluster, 632430bbaaaSFrediano Ziglio n, &hd_qiov); 63352b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 63452b8eb60SKevin Wolf if (ret < 0) { 63527deebe8SFrediano Ziglio break; 636019d6b8fSAnthony Liguori } 6378336aafaSDaniel P. Berrange if (bs->encrypted) { 638d85f4222SDaniel P. Berrange assert(s->crypto); 639d85f4222SDaniel P. Berrange if (qcrypto_block_decrypt(s->crypto, sector_num, buf, 640d85f4222SDaniel P. Berrange n * BDRV_SECTOR_SIZE, &err) < 0) { 641f6fa64f6SDaniel P. Berrange goto fail; 642f6fa64f6SDaniel P. Berrange } 64343ca85b5SFrediano Ziglio } 64443ca85b5SFrediano Ziglio } 64527deebe8SFrediano Ziglio ret = 0; 64643ca85b5SFrediano Ziglio 64727deebe8SFrediano Ziglio nb_sectors -= n; 64827deebe8SFrediano Ziglio sector_num += n; 64927deebe8SFrediano Ziglio buf += n * 512; 65052b8eb60SKevin Wolf } 651019d6b8fSAnthony Liguori 65227deebe8SFrediano Ziglio done: 65352b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 65452b8eb60SKevin Wolf 65527deebe8SFrediano Ziglio if (qiov->niov > 1) { 65603396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size); 65727deebe8SFrediano Ziglio qemu_vfree(orig_buf); 658019d6b8fSAnthony Liguori } 65952b8eb60SKevin Wolf 66052b8eb60SKevin Wolf return ret; 66127deebe8SFrediano Ziglio 66227deebe8SFrediano Ziglio fail: 663f6fa64f6SDaniel P. Berrange error_free(err); 66427deebe8SFrediano Ziglio ret = -EIO; 66527deebe8SFrediano Ziglio goto done; 666019d6b8fSAnthony Liguori } 667019d6b8fSAnthony Liguori 668a968168cSDong Xu Wang static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num, 66927deebe8SFrediano Ziglio int nb_sectors, QEMUIOVector *qiov) 670019d6b8fSAnthony Liguori { 671019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 672019d6b8fSAnthony Liguori int index_in_cluster; 673019d6b8fSAnthony Liguori uint64_t cluster_offset; 67427deebe8SFrediano Ziglio int ret = 0, n; 675430bbaaaSFrediano Ziglio struct iovec hd_iov; 676430bbaaaSFrediano Ziglio QEMUIOVector hd_qiov; 67727deebe8SFrediano Ziglio uint8_t *buf; 67827deebe8SFrediano Ziglio void *orig_buf; 679019d6b8fSAnthony Liguori 68027deebe8SFrediano Ziglio s->cluster_cache_offset = -1; /* disable compressed cache */ 68127deebe8SFrediano Ziglio 6821fad1f94SDaniel P. Berrange /* We must always copy the iov when encrypting, so we 6831fad1f94SDaniel P. Berrange * don't modify the original data buffer during encryption */ 6841fad1f94SDaniel P. Berrange if (bs->encrypted || qiov->niov > 1) { 6850df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size); 6860df93305SKevin Wolf if (buf == NULL) { 6870df93305SKevin Wolf return -ENOMEM; 6880df93305SKevin Wolf } 689d5e6b161SMichael Tokarev qemu_iovec_to_buf(qiov, 0, buf, qiov->size); 69027deebe8SFrediano Ziglio } else { 69127deebe8SFrediano Ziglio orig_buf = NULL; 69227deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base; 693019d6b8fSAnthony Liguori } 694019d6b8fSAnthony Liguori 69527deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock); 69627deebe8SFrediano Ziglio 69727deebe8SFrediano Ziglio while (nb_sectors != 0) { 69827deebe8SFrediano Ziglio 69927deebe8SFrediano Ziglio index_in_cluster = sector_num & (s->cluster_sectors - 1); 700430bbaaaSFrediano Ziglio n = s->cluster_sectors - index_in_cluster; 70127deebe8SFrediano Ziglio if (n > nb_sectors) { 70227deebe8SFrediano Ziglio n = nb_sectors; 703430bbaaaSFrediano Ziglio } 70427deebe8SFrediano Ziglio cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, 705019d6b8fSAnthony Liguori index_in_cluster, 706430bbaaaSFrediano Ziglio index_in_cluster + n); 707019d6b8fSAnthony Liguori if (!cluster_offset || (cluster_offset & 511) != 0) { 70827deebe8SFrediano Ziglio ret = -EIO; 70927deebe8SFrediano Ziglio break; 710019d6b8fSAnthony Liguori } 7118336aafaSDaniel P. Berrange if (bs->encrypted) { 712f6fa64f6SDaniel P. Berrange Error *err = NULL; 713d85f4222SDaniel P. Berrange assert(s->crypto); 714d85f4222SDaniel P. Berrange if (qcrypto_block_encrypt(s->crypto, sector_num, buf, 715d85f4222SDaniel P. Berrange n * BDRV_SECTOR_SIZE, &err) < 0) { 716f6fa64f6SDaniel P. Berrange error_free(err); 717f6fa64f6SDaniel P. Berrange ret = -EIO; 718f6fa64f6SDaniel P. Berrange break; 719f6fa64f6SDaniel P. Berrange } 720019d6b8fSAnthony Liguori } 721019d6b8fSAnthony Liguori 7221fad1f94SDaniel P. Berrange hd_iov.iov_base = (void *)buf; 723430bbaaaSFrediano Ziglio hd_iov.iov_len = n * 512; 724430bbaaaSFrediano Ziglio qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); 72552b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 72625ec177dSKevin Wolf ret = bdrv_co_writev(bs->file, 727019d6b8fSAnthony Liguori (cluster_offset >> 9) + index_in_cluster, 728430bbaaaSFrediano Ziglio n, &hd_qiov); 72952b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock); 73052b8eb60SKevin Wolf if (ret < 0) { 73127deebe8SFrediano Ziglio break; 7325614c188SStefan Weil } 73327deebe8SFrediano Ziglio ret = 0; 73443ca85b5SFrediano Ziglio 73527deebe8SFrediano Ziglio nb_sectors -= n; 73627deebe8SFrediano Ziglio sector_num += n; 73727deebe8SFrediano Ziglio buf += n * 512; 738019d6b8fSAnthony Liguori } 73952b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock); 740019d6b8fSAnthony Liguori 74127deebe8SFrediano Ziglio qemu_vfree(orig_buf); 742b11a24deSKevin Wolf 74352b8eb60SKevin Wolf return ret; 744019d6b8fSAnthony Liguori } 745019d6b8fSAnthony Liguori 746019d6b8fSAnthony Liguori static void qcow_close(BlockDriverState *bs) 747019d6b8fSAnthony Liguori { 748019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 749fd9f102cSKevin Wolf 750d85f4222SDaniel P. Berrange qcrypto_block_free(s->crypto); 751d85f4222SDaniel P. Berrange s->crypto = NULL; 7527267c094SAnthony Liguori g_free(s->l1_table); 7530df93305SKevin Wolf qemu_vfree(s->l2_cache); 7547267c094SAnthony Liguori g_free(s->cluster_cache); 7557267c094SAnthony Liguori g_free(s->cluster_data); 756fd9f102cSKevin Wolf 757fd9f102cSKevin Wolf migrate_del_blocker(s->migration_blocker); 758fd9f102cSKevin Wolf error_free(s->migration_blocker); 759019d6b8fSAnthony Liguori } 760019d6b8fSAnthony Liguori 76116d12159SChunyan Liu static int qcow_create(const char *filename, QemuOpts *opts, Error **errp) 762019d6b8fSAnthony Liguori { 7632b16c9ffSLi Zhi Hui int header_size, backing_filename_len, l1_size, shift, i; 764019d6b8fSAnthony Liguori QCowHeader header; 7652b16c9ffSLi Zhi Hui uint8_t *tmp; 7660e7e1989SKevin Wolf int64_t total_size = 0; 76716d12159SChunyan Liu char *backing_file = NULL; 76834b5d2c6SMax Reitz Error *local_err = NULL; 7693e1a8134SKirill A. Shutemov int ret; 7706af40160SKevin Wolf BlockBackend *qcow_blk; 7710cb8d47bSDaniel P. Berrange const char *encryptfmt = NULL; 772d85f4222SDaniel P. Berrange QDict *options; 773d85f4222SDaniel P. Berrange QDict *encryptopts = NULL; 774d85f4222SDaniel P. Berrange QCryptoBlockCreateOptions *crypto_opts = NULL; 775d85f4222SDaniel P. Berrange QCryptoBlock *crypto = NULL; 7760e7e1989SKevin Wolf 7770e7e1989SKevin Wolf /* Read out options */ 778180e9526SHu Tao total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 779c2eb918eSHu Tao BDRV_SECTOR_SIZE); 7806aa837f7SDaniel P. Berrange if (total_size == 0) { 7816aa837f7SDaniel P. Berrange error_setg(errp, "Image size is too small, cannot be zero length"); 7826aa837f7SDaniel P. Berrange ret = -EINVAL; 7836aa837f7SDaniel P. Berrange goto cleanup; 7846aa837f7SDaniel P. Berrange } 7856aa837f7SDaniel P. Berrange 78616d12159SChunyan Liu backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); 7870cb8d47bSDaniel P. Berrange encryptfmt = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); 7880cb8d47bSDaniel P. Berrange if (encryptfmt) { 7890cb8d47bSDaniel P. Berrange if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) { 7900cb8d47bSDaniel P. Berrange error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and " 7910cb8d47bSDaniel P. Berrange BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive"); 7920cb8d47bSDaniel P. Berrange ret = -EINVAL; 7930cb8d47bSDaniel P. Berrange goto cleanup; 7940cb8d47bSDaniel P. Berrange } 7950cb8d47bSDaniel P. Berrange } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { 7960cb8d47bSDaniel P. Berrange encryptfmt = "aes"; 7970e7e1989SKevin Wolf } 798019d6b8fSAnthony Liguori 799c282e1fdSChunyan Liu ret = bdrv_create_file(filename, opts, &local_err); 8002b16c9ffSLi Zhi Hui if (ret < 0) { 801b6d5066dSPaolo Bonzini error_propagate(errp, local_err); 80216d12159SChunyan Liu goto cleanup; 8032b16c9ffSLi Zhi Hui } 8042b16c9ffSLi Zhi Hui 805efaa7c4eSMax Reitz qcow_blk = blk_new_open(filename, NULL, NULL, 80655880601SKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, 80755880601SKevin Wolf &local_err); 8086af40160SKevin Wolf if (qcow_blk == NULL) { 809b6d5066dSPaolo Bonzini error_propagate(errp, local_err); 8106af40160SKevin Wolf ret = -EIO; 81116d12159SChunyan Liu goto cleanup; 8122b16c9ffSLi Zhi Hui } 8132b16c9ffSLi Zhi Hui 8146af40160SKevin Wolf blk_set_allow_write_beyond_eof(qcow_blk, true); 8156af40160SKevin Wolf 816ed3d2ec9SMax Reitz ret = blk_truncate(qcow_blk, 0, errp); 8172b16c9ffSLi Zhi Hui if (ret < 0) { 8182b16c9ffSLi Zhi Hui goto exit; 8192b16c9ffSLi Zhi Hui } 8202b16c9ffSLi Zhi Hui 821019d6b8fSAnthony Liguori memset(&header, 0, sizeof(header)); 822019d6b8fSAnthony Liguori header.magic = cpu_to_be32(QCOW_MAGIC); 823019d6b8fSAnthony Liguori header.version = cpu_to_be32(QCOW_VERSION); 824180e9526SHu Tao header.size = cpu_to_be64(total_size); 825019d6b8fSAnthony Liguori header_size = sizeof(header); 826019d6b8fSAnthony Liguori backing_filename_len = 0; 827019d6b8fSAnthony Liguori if (backing_file) { 828019d6b8fSAnthony Liguori if (strcmp(backing_file, "fat:")) { 829019d6b8fSAnthony Liguori header.backing_file_offset = cpu_to_be64(header_size); 830019d6b8fSAnthony Liguori backing_filename_len = strlen(backing_file); 831019d6b8fSAnthony Liguori header.backing_file_size = cpu_to_be32(backing_filename_len); 832019d6b8fSAnthony Liguori header_size += backing_filename_len; 833019d6b8fSAnthony Liguori } else { 834019d6b8fSAnthony Liguori /* special backing file for vvfat */ 835272545cfSPeter Maydell g_free(backing_file); 836019d6b8fSAnthony Liguori backing_file = NULL; 837019d6b8fSAnthony Liguori } 838019d6b8fSAnthony Liguori header.cluster_bits = 9; /* 512 byte cluster to avoid copying 839dc6fb73dSDeepak Kathayat unmodified sectors */ 840019d6b8fSAnthony Liguori header.l2_bits = 12; /* 32 KB L2 tables */ 841019d6b8fSAnthony Liguori } else { 842019d6b8fSAnthony Liguori header.cluster_bits = 12; /* 4 KB clusters */ 843019d6b8fSAnthony Liguori header.l2_bits = 9; /* 4 KB L2 tables */ 844019d6b8fSAnthony Liguori } 845019d6b8fSAnthony Liguori header_size = (header_size + 7) & ~7; 846019d6b8fSAnthony Liguori shift = header.cluster_bits + header.l2_bits; 847180e9526SHu Tao l1_size = (total_size + (1LL << shift) - 1) >> shift; 848019d6b8fSAnthony Liguori 849019d6b8fSAnthony Liguori header.l1_table_offset = cpu_to_be64(header_size); 850d85f4222SDaniel P. Berrange 851d85f4222SDaniel P. Berrange options = qemu_opts_to_qdict(opts, NULL); 852d85f4222SDaniel P. Berrange qdict_extract_subqdict(options, &encryptopts, "encrypt."); 853d85f4222SDaniel P. Berrange QDECREF(options); 8540cb8d47bSDaniel P. Berrange if (encryptfmt) { 8550cb8d47bSDaniel P. Berrange if (!g_str_equal(encryptfmt, "aes")) { 8560cb8d47bSDaniel P. Berrange error_setg(errp, "Unknown encryption format '%s', expected 'aes'", 8570cb8d47bSDaniel P. Berrange encryptfmt); 8580cb8d47bSDaniel P. Berrange ret = -EINVAL; 8590cb8d47bSDaniel P. Berrange goto exit; 8600cb8d47bSDaniel P. Berrange } 861019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); 862d85f4222SDaniel P. Berrange 863d85f4222SDaniel P. Berrange crypto_opts = block_crypto_create_opts_init( 864d85f4222SDaniel P. Berrange Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); 865d85f4222SDaniel P. Berrange if (!crypto_opts) { 866d85f4222SDaniel P. Berrange ret = -EINVAL; 867d85f4222SDaniel P. Berrange goto exit; 868d85f4222SDaniel P. Berrange } 869d85f4222SDaniel P. Berrange 8701cd9a787SDaniel P. Berrange crypto = qcrypto_block_create(crypto_opts, "encrypt.", 8711cd9a787SDaniel P. Berrange NULL, NULL, NULL, errp); 872d85f4222SDaniel P. Berrange if (!crypto) { 873d85f4222SDaniel P. Berrange ret = -EINVAL; 874d85f4222SDaniel P. Berrange goto exit; 875d85f4222SDaniel P. Berrange } 876019d6b8fSAnthony Liguori } else { 877019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); 878019d6b8fSAnthony Liguori } 879019d6b8fSAnthony Liguori 880019d6b8fSAnthony Liguori /* write all the data */ 8818341f00dSEric Blake ret = blk_pwrite(qcow_blk, 0, &header, sizeof(header), 0); 8823e1a8134SKirill A. Shutemov if (ret != sizeof(header)) { 8833e1a8134SKirill A. Shutemov goto exit; 8843e1a8134SKirill A. Shutemov } 8853e1a8134SKirill A. Shutemov 886019d6b8fSAnthony Liguori if (backing_file) { 8876af40160SKevin Wolf ret = blk_pwrite(qcow_blk, sizeof(header), 8888341f00dSEric Blake backing_file, backing_filename_len, 0); 8893e1a8134SKirill A. Shutemov if (ret != backing_filename_len) { 8903e1a8134SKirill A. Shutemov goto exit; 891019d6b8fSAnthony Liguori } 8923e1a8134SKirill A. Shutemov } 8933e1a8134SKirill A. Shutemov 8942b16c9ffSLi Zhi Hui tmp = g_malloc0(BDRV_SECTOR_SIZE); 895d737b78cSLaurent Vivier for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE); 896d737b78cSLaurent Vivier i++) { 8978341f00dSEric Blake ret = blk_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i, 8988341f00dSEric Blake tmp, BDRV_SECTOR_SIZE, 0); 8992b16c9ffSLi Zhi Hui if (ret != BDRV_SECTOR_SIZE) { 9002b16c9ffSLi Zhi Hui g_free(tmp); 9012b16c9ffSLi Zhi Hui goto exit; 9022b16c9ffSLi Zhi Hui } 9032b16c9ffSLi Zhi Hui } 9042b16c9ffSLi Zhi Hui 9052b16c9ffSLi Zhi Hui g_free(tmp); 9063e1a8134SKirill A. Shutemov ret = 0; 9073e1a8134SKirill A. Shutemov exit: 9086af40160SKevin Wolf blk_unref(qcow_blk); 90916d12159SChunyan Liu cleanup: 910d85f4222SDaniel P. Berrange QDECREF(encryptopts); 911d85f4222SDaniel P. Berrange qcrypto_block_free(crypto); 912d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockCreateOptions(crypto_opts); 91316d12159SChunyan Liu g_free(backing_file); 9143e1a8134SKirill A. Shutemov return ret; 915019d6b8fSAnthony Liguori } 916019d6b8fSAnthony Liguori 917019d6b8fSAnthony Liguori static int qcow_make_empty(BlockDriverState *bs) 918019d6b8fSAnthony Liguori { 919019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 920019d6b8fSAnthony Liguori uint32_t l1_length = s->l1_size * sizeof(uint64_t); 921019d6b8fSAnthony Liguori int ret; 922019d6b8fSAnthony Liguori 923019d6b8fSAnthony Liguori memset(s->l1_table, 0, l1_length); 924d9ca2ea2SKevin Wolf if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table, 9255e5557d9SKevin Wolf l1_length) < 0) 926019d6b8fSAnthony Liguori return -1; 927*7ea37c30SMax Reitz ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, 928*7ea37c30SMax Reitz PREALLOC_MODE_OFF, NULL); 929019d6b8fSAnthony Liguori if (ret < 0) 930019d6b8fSAnthony Liguori return ret; 931019d6b8fSAnthony Liguori 932019d6b8fSAnthony Liguori memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); 933019d6b8fSAnthony Liguori memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); 934019d6b8fSAnthony Liguori memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); 935019d6b8fSAnthony Liguori 936019d6b8fSAnthony Liguori return 0; 937019d6b8fSAnthony Liguori } 938019d6b8fSAnthony Liguori 939019d6b8fSAnthony Liguori /* XXX: put compressed sectors first, then all the cluster aligned 940019d6b8fSAnthony Liguori tables to avoid losing bytes in alignment */ 941f2b95a12SPavel Butsykin static coroutine_fn int 942f2b95a12SPavel Butsykin qcow_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, 943f2b95a12SPavel Butsykin uint64_t bytes, QEMUIOVector *qiov) 944019d6b8fSAnthony Liguori { 945019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 946f2b95a12SPavel Butsykin QEMUIOVector hd_qiov; 947f2b95a12SPavel Butsykin struct iovec iov; 948019d6b8fSAnthony Liguori z_stream strm; 949019d6b8fSAnthony Liguori int ret, out_len; 950f2b95a12SPavel Butsykin uint8_t *buf, *out_buf; 951019d6b8fSAnthony Liguori uint64_t cluster_offset; 952019d6b8fSAnthony Liguori 953f2b95a12SPavel Butsykin buf = qemu_blockalign(bs, s->cluster_size); 954655923dfSPavel Butsykin if (bytes != s->cluster_size) { 955655923dfSPavel Butsykin if (bytes > s->cluster_size || 956655923dfSPavel Butsykin offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS) 957655923dfSPavel Butsykin { 958655923dfSPavel Butsykin qemu_vfree(buf); 959655923dfSPavel Butsykin return -EINVAL; 960655923dfSPavel Butsykin } 961655923dfSPavel Butsykin /* Zero-pad last write if image size is not cluster aligned */ 962655923dfSPavel Butsykin memset(buf + bytes, 0, s->cluster_size - bytes); 963655923dfSPavel Butsykin } 964f2b95a12SPavel Butsykin qemu_iovec_to_buf(qiov, 0, buf, qiov->size); 965019d6b8fSAnthony Liguori 966ebf7bba0SVladimir Sementsov-Ogievskiy out_buf = g_malloc(s->cluster_size); 967019d6b8fSAnthony Liguori 968019d6b8fSAnthony Liguori /* best compression, small window, no zlib header */ 969019d6b8fSAnthony Liguori memset(&strm, 0, sizeof(strm)); 970019d6b8fSAnthony Liguori ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, 971019d6b8fSAnthony Liguori Z_DEFLATED, -12, 972019d6b8fSAnthony Liguori 9, Z_DEFAULT_STRATEGY); 973019d6b8fSAnthony Liguori if (ret != 0) { 97464ebe71aSKevin Wolf ret = -EINVAL; 97564ebe71aSKevin Wolf goto fail; 976019d6b8fSAnthony Liguori } 977019d6b8fSAnthony Liguori 978019d6b8fSAnthony Liguori strm.avail_in = s->cluster_size; 979019d6b8fSAnthony Liguori strm.next_in = (uint8_t *)buf; 980019d6b8fSAnthony Liguori strm.avail_out = s->cluster_size; 981019d6b8fSAnthony Liguori strm.next_out = out_buf; 982019d6b8fSAnthony Liguori 983019d6b8fSAnthony Liguori ret = deflate(&strm, Z_FINISH); 984019d6b8fSAnthony Liguori if (ret != Z_STREAM_END && ret != Z_OK) { 985019d6b8fSAnthony Liguori deflateEnd(&strm); 98664ebe71aSKevin Wolf ret = -EINVAL; 98764ebe71aSKevin Wolf goto fail; 988019d6b8fSAnthony Liguori } 989019d6b8fSAnthony Liguori out_len = strm.next_out - out_buf; 990019d6b8fSAnthony Liguori 991019d6b8fSAnthony Liguori deflateEnd(&strm); 992019d6b8fSAnthony Liguori 993019d6b8fSAnthony Liguori if (ret != Z_STREAM_END || out_len >= s->cluster_size) { 994019d6b8fSAnthony Liguori /* could not compress: write normal cluster */ 995f2b95a12SPavel Butsykin ret = qcow_co_writev(bs, offset >> BDRV_SECTOR_BITS, 996f2b95a12SPavel Butsykin bytes >> BDRV_SECTOR_BITS, qiov); 99764ebe71aSKevin Wolf if (ret < 0) { 99864ebe71aSKevin Wolf goto fail; 99964ebe71aSKevin Wolf } 1000f2b95a12SPavel Butsykin goto success; 1001f2b95a12SPavel Butsykin } 1002f2b95a12SPavel Butsykin qemu_co_mutex_lock(&s->lock); 1003f2b95a12SPavel Butsykin cluster_offset = get_cluster_offset(bs, offset, 2, out_len, 0, 0); 1004f2b95a12SPavel Butsykin qemu_co_mutex_unlock(&s->lock); 100564ebe71aSKevin Wolf if (cluster_offset == 0) { 100664ebe71aSKevin Wolf ret = -EIO; 100764ebe71aSKevin Wolf goto fail; 100864ebe71aSKevin Wolf } 1009019d6b8fSAnthony Liguori cluster_offset &= s->cluster_offset_mask; 1010f2b95a12SPavel Butsykin 1011f2b95a12SPavel Butsykin iov = (struct iovec) { 1012f2b95a12SPavel Butsykin .iov_base = out_buf, 1013f2b95a12SPavel Butsykin .iov_len = out_len, 1014f2b95a12SPavel Butsykin }; 1015f2b95a12SPavel Butsykin qemu_iovec_init_external(&hd_qiov, &iov, 1); 1016f2b95a12SPavel Butsykin ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0); 101764ebe71aSKevin Wolf if (ret < 0) { 101864ebe71aSKevin Wolf goto fail; 1019019d6b8fSAnthony Liguori } 1020f2b95a12SPavel Butsykin success: 102164ebe71aSKevin Wolf ret = 0; 102264ebe71aSKevin Wolf fail: 1023f2b95a12SPavel Butsykin qemu_vfree(buf); 10247267c094SAnthony Liguori g_free(out_buf); 102564ebe71aSKevin Wolf return ret; 1026019d6b8fSAnthony Liguori } 1027019d6b8fSAnthony Liguori 1028019d6b8fSAnthony Liguori static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1029019d6b8fSAnthony Liguori { 1030019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque; 1031019d6b8fSAnthony Liguori bdi->cluster_size = s->cluster_size; 1032019d6b8fSAnthony Liguori return 0; 1033019d6b8fSAnthony Liguori } 1034019d6b8fSAnthony Liguori 103516d12159SChunyan Liu static QemuOptsList qcow_create_opts = { 103616d12159SChunyan Liu .name = "qcow-create-opts", 103716d12159SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head), 103816d12159SChunyan Liu .desc = { 1039db08adf5SKevin Wolf { 1040db08adf5SKevin Wolf .name = BLOCK_OPT_SIZE, 104116d12159SChunyan Liu .type = QEMU_OPT_SIZE, 1042db08adf5SKevin Wolf .help = "Virtual disk size" 1043db08adf5SKevin Wolf }, 1044db08adf5SKevin Wolf { 1045db08adf5SKevin Wolf .name = BLOCK_OPT_BACKING_FILE, 104616d12159SChunyan Liu .type = QEMU_OPT_STRING, 1047db08adf5SKevin Wolf .help = "File name of a base image" 1048db08adf5SKevin Wolf }, 1049db08adf5SKevin Wolf { 1050db08adf5SKevin Wolf .name = BLOCK_OPT_ENCRYPT, 105116d12159SChunyan Liu .type = QEMU_OPT_BOOL, 10520cb8d47bSDaniel P. Berrange .help = "Encrypt the image with format 'aes'. (Deprecated " 10530cb8d47bSDaniel P. Berrange "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)", 10540cb8d47bSDaniel P. Berrange }, 10550cb8d47bSDaniel P. Berrange { 10560cb8d47bSDaniel P. Berrange .name = BLOCK_OPT_ENCRYPT_FORMAT, 10570cb8d47bSDaniel P. Berrange .type = QEMU_OPT_STRING, 10580cb8d47bSDaniel P. Berrange .help = "Encrypt the image, format choices: 'aes'", 1059db08adf5SKevin Wolf }, 1060d85f4222SDaniel P. Berrange BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."), 106116d12159SChunyan Liu { /* end of list */ } 106216d12159SChunyan Liu } 10630e7e1989SKevin Wolf }; 10640e7e1989SKevin Wolf 1065019d6b8fSAnthony Liguori static BlockDriver bdrv_qcow = { 1066019d6b8fSAnthony Liguori .format_name = "qcow", 1067019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVQcowState), 1068019d6b8fSAnthony Liguori .bdrv_probe = qcow_probe, 1069019d6b8fSAnthony Liguori .bdrv_open = qcow_open, 1070019d6b8fSAnthony Liguori .bdrv_close = qcow_close, 1071862f215fSKevin Wolf .bdrv_child_perm = bdrv_format_default_perms, 1072d177692eSJeff Cody .bdrv_reopen_prepare = qcow_reopen_prepare, 1073c282e1fdSChunyan Liu .bdrv_create = qcow_create, 10743ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1, 10758ee79e70SKevin Wolf .supports_backing = true, 1076c68b89acSKevin Wolf 107752b8eb60SKevin Wolf .bdrv_co_readv = qcow_co_readv, 107852b8eb60SKevin Wolf .bdrv_co_writev = qcow_co_writev, 1079b6b8a333SPaolo Bonzini .bdrv_co_get_block_status = qcow_co_get_block_status, 1080c68b89acSKevin Wolf 1081c68b89acSKevin Wolf .bdrv_make_empty = qcow_make_empty, 1082f2b95a12SPavel Butsykin .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed, 1083019d6b8fSAnthony Liguori .bdrv_get_info = qcow_get_info, 10840e7e1989SKevin Wolf 108516d12159SChunyan Liu .create_opts = &qcow_create_opts, 1086019d6b8fSAnthony Liguori }; 1087019d6b8fSAnthony Liguori 1088019d6b8fSAnthony Liguori static void bdrv_qcow_init(void) 1089019d6b8fSAnthony Liguori { 1090019d6b8fSAnthony Liguori bdrv_register(&bdrv_qcow); 1091019d6b8fSAnthony Liguori } 1092019d6b8fSAnthony Liguori 1093019d6b8fSAnthony Liguori block_init(bdrv_qcow_init); 1094