bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for the QCOW format |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 26 | #include "qemu-common.h" |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 27 | #include "qemu/error-report.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/block_int.h" |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 29 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/module.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 31 | #include "qemu/bswap.h" |
bellard | 28d34b8 | 2004-09-29 21:30:43 +0000 | [diff] [blame] | 32 | #include <zlib.h> |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 33 | #include "qapi/qmp/qerror.h" |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 34 | #include "qapi/qmp/qstring.h" |
| 35 | #include "crypto/block.h" |
Juan Quintela | 795c40b | 2017-04-06 12:00:28 +0200 | [diff] [blame] | 36 | #include "migration/blocker.h" |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 37 | #include "block/crypto.h" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 38 | |
| 39 | /**************************************************************/ |
| 40 | /* QEMU COW block driver with compression and encryption support */ |
| 41 | |
| 42 | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
| 43 | #define QCOW_VERSION 1 |
| 44 | |
| 45 | #define QCOW_CRYPT_NONE 0 |
| 46 | #define QCOW_CRYPT_AES 1 |
| 47 | |
| 48 | #define QCOW_OFLAG_COMPRESSED (1LL << 63) |
| 49 | |
| 50 | typedef struct QCowHeader { |
| 51 | uint32_t magic; |
| 52 | uint32_t version; |
| 53 | uint64_t backing_file_offset; |
| 54 | uint32_t backing_file_size; |
| 55 | uint32_t mtime; |
| 56 | uint64_t size; /* in bytes */ |
| 57 | uint8_t cluster_bits; |
| 58 | uint8_t l2_bits; |
Kevin Wolf | ea54fef | 2014-05-07 16:56:10 +0200 | [diff] [blame] | 59 | uint16_t padding; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 60 | uint32_t crypt_method; |
| 61 | uint64_t l1_table_offset; |
Kevin Wolf | ea54fef | 2014-05-07 16:56:10 +0200 | [diff] [blame] | 62 | } QEMU_PACKED QCowHeader; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 63 | |
| 64 | #define L2_CACHE_SIZE 16 |
| 65 | |
| 66 | typedef struct BDRVQcowState { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 67 | int cluster_bits; |
| 68 | int cluster_size; |
| 69 | int cluster_sectors; |
| 70 | int l2_bits; |
| 71 | int l2_size; |
Kevin Wolf | 46485de | 2014-05-08 13:08:20 +0200 | [diff] [blame] | 72 | unsigned int l1_size; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 73 | uint64_t cluster_offset_mask; |
| 74 | uint64_t l1_table_offset; |
| 75 | uint64_t *l1_table; |
| 76 | uint64_t *l2_cache; |
| 77 | uint64_t l2_cache_offsets[L2_CACHE_SIZE]; |
| 78 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
| 79 | uint8_t *cluster_cache; |
| 80 | uint8_t *cluster_data; |
| 81 | uint64_t cluster_cache_offset; |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 82 | QCryptoBlock *crypto; /* Disk encryption format driver */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 83 | uint32_t crypt_method_header; |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 84 | CoMutex lock; |
Kevin Wolf | fd9f102 | 2011-11-22 16:44:45 +0100 | [diff] [blame] | 85 | Error *migration_blocker; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 86 | } BDRVQcowState; |
| 87 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 88 | static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 89 | |
| 90 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
| 91 | { |
| 92 | const QCowHeader *cow_header = (const void *)buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 93 | |
bellard | 712e787 | 2005-04-28 21:09:32 +0000 | [diff] [blame] | 94 | if (buf_size >= sizeof(QCowHeader) && |
| 95 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 96 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 97 | return 100; |
| 98 | else |
| 99 | return 0; |
| 100 | } |
| 101 | |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 102 | static QemuOptsList qcow_runtime_opts = { |
| 103 | .name = "qcow", |
| 104 | .head = QTAILQ_HEAD_INITIALIZER(qcow_runtime_opts.head), |
| 105 | .desc = { |
| 106 | BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."), |
| 107 | { /* end of list */ } |
| 108 | }, |
| 109 | }; |
| 110 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 111 | static int qcow_open(BlockDriverState *bs, QDict *options, int flags, |
| 112 | Error **errp) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 113 | { |
| 114 | BDRVQcowState *s = bs->opaque; |
Kevin Wolf | d66e5ce | 2014-05-08 13:35:09 +0200 | [diff] [blame] | 115 | unsigned int len, i, shift; |
| 116 | int ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 117 | QCowHeader header; |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 118 | Error *local_err = NULL; |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 119 | QCryptoBlockOpenOptions *crypto_opts = NULL; |
| 120 | unsigned int cflags = 0; |
| 121 | QDict *encryptopts = NULL; |
| 122 | const char *encryptfmt; |
| 123 | |
| 124 | qdict_extract_subqdict(options, &encryptopts, "encrypt."); |
| 125 | encryptfmt = qdict_get_try_str(encryptopts, "format"); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 126 | |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 127 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, |
| 128 | false, errp); |
| 129 | if (!bs->file) { |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 130 | ret = -EINVAL; |
| 131 | goto fail; |
Kevin Wolf | 4e4bf5c | 2016-12-16 18:52:37 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 134 | ret = bdrv_pread(bs->file, 0, &header, sizeof(header)); |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 135 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 136 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 137 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 138 | be32_to_cpus(&header.magic); |
| 139 | be32_to_cpus(&header.version); |
| 140 | be64_to_cpus(&header.backing_file_offset); |
| 141 | be32_to_cpus(&header.backing_file_size); |
| 142 | be32_to_cpus(&header.mtime); |
| 143 | be64_to_cpus(&header.size); |
| 144 | be32_to_cpus(&header.crypt_method); |
| 145 | be64_to_cpus(&header.l1_table_offset); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 146 | |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 147 | if (header.magic != QCOW_MAGIC) { |
Paolo Bonzini | 76abe40 | 2014-02-17 14:44:06 +0100 | [diff] [blame] | 148 | error_setg(errp, "Image not in qcow format"); |
| 149 | ret = -EINVAL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 150 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 151 | } |
| 152 | if (header.version != QCOW_VERSION) { |
Max Reitz | a55448b | 2016-03-16 19:54:33 +0100 | [diff] [blame] | 153 | error_setg(errp, "Unsupported qcow version %" PRIu32, header.version); |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 154 | ret = -ENOTSUP; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 155 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Kevin Wolf | 7159a45 | 2014-05-07 17:30:30 +0200 | [diff] [blame] | 158 | if (header.size <= 1) { |
| 159 | error_setg(errp, "Image size is too small (must be at least 2 bytes)"); |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 160 | ret = -EINVAL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 161 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 162 | } |
Kevin Wolf | 7159a45 | 2014-05-07 17:30:30 +0200 | [diff] [blame] | 163 | if (header.cluster_bits < 9 || header.cluster_bits > 16) { |
| 164 | error_setg(errp, "Cluster size must be between 512 and 64k"); |
| 165 | ret = -EINVAL; |
| 166 | goto fail; |
| 167 | } |
| 168 | |
Kevin Wolf | 42eb581 | 2014-05-15 16:10:11 +0200 | [diff] [blame] | 169 | /* l2_bits specifies number of entries; storing a uint64_t in each entry, |
| 170 | * so bytes = num_entries << 3. */ |
| 171 | if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) { |
| 172 | error_setg(errp, "L2 table size must be between 512 and 64k"); |
| 173 | ret = -EINVAL; |
| 174 | goto fail; |
| 175 | } |
| 176 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 177 | s->crypt_method_header = header.crypt_method; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 178 | if (s->crypt_method_header) { |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 179 | if (bdrv_uses_whitelist() && |
| 180 | s->crypt_method_header == QCOW_CRYPT_AES) { |
Daniel P. Berrange | 8c0dcbc | 2016-06-13 12:30:09 +0100 | [diff] [blame] | 181 | error_setg(errp, |
| 182 | "Use of AES-CBC encrypted qcow images is no longer " |
| 183 | "supported in system emulators"); |
| 184 | error_append_hint(errp, |
| 185 | "You can use 'qemu-img convert' to convert your " |
| 186 | "image to an alternative supported format, such " |
| 187 | "as unencrypted qcow, or raw with the LUKS " |
| 188 | "format instead.\n"); |
| 189 | ret = -ENOSYS; |
| 190 | goto fail; |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 191 | } |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 192 | if (s->crypt_method_header == QCOW_CRYPT_AES) { |
| 193 | if (encryptfmt && !g_str_equal(encryptfmt, "aes")) { |
| 194 | error_setg(errp, |
| 195 | "Header reported 'aes' encryption format but " |
| 196 | "options specify '%s'", encryptfmt); |
| 197 | ret = -EINVAL; |
| 198 | goto fail; |
| 199 | } |
| 200 | qdict_del(encryptopts, "format"); |
| 201 | crypto_opts = block_crypto_open_opts_init( |
| 202 | Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); |
| 203 | if (!crypto_opts) { |
| 204 | ret = -EINVAL; |
| 205 | goto fail; |
| 206 | } |
Daniel P. Berrange | e6ff69b | 2016-03-21 14:11:48 +0000 | [diff] [blame] | 207 | |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 208 | if (flags & BDRV_O_NO_IO) { |
| 209 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; |
| 210 | } |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 211 | s->crypto = qcrypto_block_open(crypto_opts, "encrypt.", |
| 212 | NULL, NULL, cflags, errp); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 213 | if (!s->crypto) { |
| 214 | ret = -EINVAL; |
| 215 | goto fail; |
| 216 | } |
| 217 | } else { |
| 218 | error_setg(errp, "invalid encryption method in qcow header"); |
| 219 | ret = -EINVAL; |
| 220 | goto fail; |
| 221 | } |
Eric Blake | 5411541 | 2016-06-23 16:37:26 -0600 | [diff] [blame] | 222 | bs->encrypted = true; |
Daniel P. Berrange | c01c214 | 2017-06-23 17:24:16 +0100 | [diff] [blame] | 223 | } else { |
| 224 | if (encryptfmt) { |
| 225 | error_setg(errp, "No encryption in image header, but options " |
| 226 | "specified format '%s'", encryptfmt); |
| 227 | ret = -EINVAL; |
| 228 | goto fail; |
| 229 | } |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 230 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 231 | s->cluster_bits = header.cluster_bits; |
| 232 | s->cluster_size = 1 << s->cluster_bits; |
| 233 | s->cluster_sectors = 1 << (s->cluster_bits - 9); |
| 234 | s->l2_bits = header.l2_bits; |
| 235 | s->l2_size = 1 << s->l2_bits; |
| 236 | bs->total_sectors = header.size / 512; |
| 237 | s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; |
| 238 | |
| 239 | /* read the level 1 table */ |
| 240 | shift = s->cluster_bits + s->l2_bits; |
Kevin Wolf | 46485de | 2014-05-08 13:08:20 +0200 | [diff] [blame] | 241 | if (header.size > UINT64_MAX - (1LL << shift)) { |
| 242 | error_setg(errp, "Image too large"); |
| 243 | ret = -EINVAL; |
| 244 | goto fail; |
| 245 | } else { |
| 246 | uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift; |
| 247 | if (l1_size > INT_MAX / sizeof(uint64_t)) { |
| 248 | error_setg(errp, "Image too large"); |
| 249 | ret = -EINVAL; |
| 250 | goto fail; |
| 251 | } |
| 252 | s->l1_size = l1_size; |
| 253 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 254 | |
| 255 | s->l1_table_offset = header.l1_table_offset; |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 256 | s->l1_table = g_try_new(uint64_t, s->l1_size); |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 257 | if (s->l1_table == NULL) { |
| 258 | error_setg(errp, "Could not allocate memory for L1 table"); |
| 259 | ret = -ENOMEM; |
| 260 | goto fail; |
| 261 | } |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 262 | |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 263 | ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 264 | s->l1_size * sizeof(uint64_t)); |
| 265 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 266 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 267 | } |
| 268 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 269 | for(i = 0;i < s->l1_size; i++) { |
| 270 | be64_to_cpus(&s->l1_table[i]); |
| 271 | } |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 272 | |
| 273 | /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */ |
| 274 | s->l2_cache = |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 275 | qemu_try_blockalign(bs->file->bs, |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 276 | s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
| 277 | if (s->l2_cache == NULL) { |
| 278 | error_setg(errp, "Could not allocate L2 table cache"); |
| 279 | ret = -ENOMEM; |
| 280 | goto fail; |
| 281 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 282 | s->cluster_cache = g_malloc(s->cluster_size); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 283 | s->cluster_data = g_malloc(s->cluster_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 284 | s->cluster_cache_offset = -1; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 285 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 286 | /* read the backing file name */ |
| 287 | if (header.backing_file_offset != 0) { |
| 288 | len = header.backing_file_size; |
Jeff Cody | e729fa6 | 2015-01-27 08:33:55 -0500 | [diff] [blame] | 289 | if (len > 1023 || len >= sizeof(bs->backing_file)) { |
Kevin Wolf | d66e5ce | 2014-05-08 13:35:09 +0200 | [diff] [blame] | 290 | error_setg(errp, "Backing file name too long"); |
| 291 | ret = -EINVAL; |
| 292 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 293 | } |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 294 | ret = bdrv_pread(bs->file, header.backing_file_offset, |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 295 | bs->backing_file, len); |
| 296 | if (ret < 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 297 | goto fail; |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 298 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 299 | bs->backing_file[len] = '\0'; |
| 300 | } |
Scott Wood | de33b1f | 2011-08-11 16:27:15 -0500 | [diff] [blame] | 301 | |
Kevin Wolf | fd9f102 | 2011-11-22 16:44:45 +0100 | [diff] [blame] | 302 | /* Disable migration when qcow images are used */ |
Alberto Garcia | 81e5f78 | 2015-04-08 12:29:19 +0300 | [diff] [blame] | 303 | error_setg(&s->migration_blocker, "The qcow format used by node '%s' " |
| 304 | "does not support live migration", |
| 305 | bdrv_get_device_or_node_name(bs)); |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 306 | ret = migrate_add_blocker(s->migration_blocker, &local_err); |
| 307 | if (local_err) { |
| 308 | error_propagate(errp, local_err); |
| 309 | error_free(s->migration_blocker); |
| 310 | goto fail; |
| 311 | } |
Kevin Wolf | fd9f102 | 2011-11-22 16:44:45 +0100 | [diff] [blame] | 312 | |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 313 | QDECREF(encryptopts); |
| 314 | qapi_free_QCryptoBlockOpenOptions(crypto_opts); |
Scott Wood | de33b1f | 2011-08-11 16:27:15 -0500 | [diff] [blame] | 315 | qemu_co_mutex_init(&s->lock); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 316 | return 0; |
| 317 | |
| 318 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 319 | g_free(s->l1_table); |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 320 | qemu_vfree(s->l2_cache); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 321 | g_free(s->cluster_cache); |
| 322 | g_free(s->cluster_data); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 323 | qcrypto_block_free(s->crypto); |
| 324 | QDECREF(encryptopts); |
| 325 | qapi_free_QCryptoBlockOpenOptions(crypto_opts); |
Li Zhi Hui | 84b0ec0 | 2011-12-15 18:14:00 +0800 | [diff] [blame] | 326 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Jeff Cody | d177692 | 2012-09-20 15:13:29 -0400 | [diff] [blame] | 329 | |
| 330 | /* We have nothing to do for QCOW reopen, stubs just return |
| 331 | * success */ |
| 332 | static int qcow_reopen_prepare(BDRVReopenState *state, |
| 333 | BlockReopenQueue *queue, Error **errp) |
| 334 | { |
| 335 | return 0; |
| 336 | } |
| 337 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 338 | |
| 339 | /* 'allocate' is: |
| 340 | * |
| 341 | * 0 to not allocate. |
| 342 | * |
| 343 | * 1 to allocate a normal cluster (for sector indexes 'n_start' to |
| 344 | * 'n_end') |
| 345 | * |
| 346 | * 2 to allocate a compressed cluster of size |
| 347 | * 'compressed_size'. 'compressed_size' must be > 0 and < |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 348 | * cluster_size |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 349 | * |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 350 | * return 0 if not allocated, 1 if *result is assigned, and negative |
| 351 | * errno on failure. |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 352 | */ |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 353 | static int get_cluster_offset(BlockDriverState *bs, |
| 354 | uint64_t offset, int allocate, |
| 355 | int compressed_size, |
| 356 | int n_start, int n_end, uint64_t *result) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 357 | { |
| 358 | BDRVQcowState *s = bs->opaque; |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 359 | int min_index, i, j, l1_index, l2_index, ret; |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 360 | int64_t l2_offset; |
| 361 | uint64_t *l2_table, cluster_offset, tmp; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 362 | uint32_t min_count; |
| 363 | int new_l2_table; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 364 | |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 365 | *result = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 366 | l1_index = offset >> (s->l2_bits + s->cluster_bits); |
| 367 | l2_offset = s->l1_table[l1_index]; |
| 368 | new_l2_table = 0; |
| 369 | if (!l2_offset) { |
| 370 | if (!allocate) |
| 371 | return 0; |
| 372 | /* allocate a new l2 entry */ |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 373 | l2_offset = bdrv_getlength(bs->file->bs); |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 374 | if (l2_offset < 0) { |
| 375 | return l2_offset; |
| 376 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 377 | /* round to cluster size */ |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 378 | l2_offset = QEMU_ALIGN_UP(l2_offset, s->cluster_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 379 | /* update the L1 entry */ |
| 380 | s->l1_table[l1_index] = l2_offset; |
| 381 | tmp = cpu_to_be64(l2_offset); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 382 | ret = bdrv_pwrite_sync(bs->file, |
| 383 | s->l1_table_offset + l1_index * sizeof(tmp), |
| 384 | &tmp, sizeof(tmp)); |
| 385 | if (ret < 0) { |
| 386 | return ret; |
| 387 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 388 | new_l2_table = 1; |
| 389 | } |
| 390 | for(i = 0; i < L2_CACHE_SIZE; i++) { |
| 391 | if (l2_offset == s->l2_cache_offsets[i]) { |
| 392 | /* increment the hit count */ |
| 393 | if (++s->l2_cache_counts[i] == 0xffffffff) { |
| 394 | for(j = 0; j < L2_CACHE_SIZE; j++) { |
| 395 | s->l2_cache_counts[j] >>= 1; |
| 396 | } |
| 397 | } |
| 398 | l2_table = s->l2_cache + (i << s->l2_bits); |
| 399 | goto found; |
| 400 | } |
| 401 | } |
| 402 | /* not found: load a new entry in the least used one */ |
| 403 | min_index = 0; |
| 404 | min_count = 0xffffffff; |
| 405 | for(i = 0; i < L2_CACHE_SIZE; i++) { |
| 406 | if (s->l2_cache_counts[i] < min_count) { |
| 407 | min_count = s->l2_cache_counts[i]; |
| 408 | min_index = i; |
| 409 | } |
| 410 | } |
| 411 | l2_table = s->l2_cache + (min_index << s->l2_bits); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 412 | if (new_l2_table) { |
| 413 | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 414 | ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table, |
| 415 | s->l2_size * sizeof(uint64_t)); |
| 416 | if (ret < 0) { |
| 417 | return ret; |
| 418 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 419 | } else { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 420 | ret = bdrv_pread(bs->file, l2_offset, l2_table, |
| 421 | s->l2_size * sizeof(uint64_t)); |
| 422 | if (ret < 0) { |
| 423 | return ret; |
| 424 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 425 | } |
| 426 | s->l2_cache_offsets[min_index] = l2_offset; |
| 427 | s->l2_cache_counts[min_index] = 1; |
| 428 | found: |
| 429 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); |
| 430 | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 431 | if (!cluster_offset || |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 432 | ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { |
| 433 | if (!allocate) |
| 434 | return 0; |
| 435 | /* allocate a new cluster */ |
| 436 | if ((cluster_offset & QCOW_OFLAG_COMPRESSED) && |
| 437 | (n_end - n_start) < s->cluster_sectors) { |
| 438 | /* if the cluster is already compressed, we must |
| 439 | decompress it in the case it is not completely |
| 440 | overwritten */ |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 441 | if (decompress_cluster(bs, cluster_offset) < 0) { |
| 442 | return -EIO; |
| 443 | } |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 444 | cluster_offset = bdrv_getlength(bs->file->bs); |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 445 | if ((int64_t) cluster_offset < 0) { |
| 446 | return cluster_offset; |
| 447 | } |
| 448 | cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 449 | /* write the cluster content */ |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 450 | ret = bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, |
| 451 | s->cluster_size); |
| 452 | if (ret < 0) { |
| 453 | return ret; |
| 454 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 455 | } else { |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 456 | cluster_offset = bdrv_getlength(bs->file->bs); |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 457 | if ((int64_t) cluster_offset < 0) { |
| 458 | return cluster_offset; |
| 459 | } |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 460 | if (allocate == 1) { |
| 461 | /* round to cluster size */ |
Eric Blake | d7a753a | 2017-08-09 15:38:06 -0500 | [diff] [blame] | 462 | cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size); |
| 463 | if (cluster_offset + s->cluster_size > INT64_MAX) { |
| 464 | return -E2BIG; |
| 465 | } |
| 466 | ret = bdrv_truncate(bs->file, cluster_offset + s->cluster_size, |
| 467 | PREALLOC_MODE_OFF, NULL); |
| 468 | if (ret < 0) { |
| 469 | return ret; |
| 470 | } |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 471 | /* if encrypted, we must initialize the cluster |
| 472 | content which won't be written */ |
Daniel P. Berrange | 8336aaf | 2015-05-12 17:09:18 +0100 | [diff] [blame] | 473 | if (bs->encrypted && |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 474 | (n_end - n_start) < s->cluster_sectors) { |
| 475 | uint64_t start_sect; |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 476 | assert(s->crypto); |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 477 | start_sect = (offset & ~(s->cluster_size - 1)) >> 9; |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 478 | for(i = 0; i < s->cluster_sectors; i++) { |
| 479 | if (i < n_start || i >= n_end) { |
Daniel P. Berrange | 1fad1f9 | 2017-06-23 17:24:07 +0100 | [diff] [blame] | 480 | memset(s->cluster_data, 0x00, 512); |
Daniel P. Berrange | 4609742 | 2017-09-27 13:53:39 +0100 | [diff] [blame] | 481 | if (qcrypto_block_encrypt(s->crypto, |
| 482 | (start_sect + i) * |
| 483 | BDRV_SECTOR_SIZE, |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 484 | s->cluster_data, |
| 485 | BDRV_SECTOR_SIZE, |
Alberto Garcia | c3a8fe3 | 2017-08-29 15:08:36 +0300 | [diff] [blame] | 486 | NULL) < 0) { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 487 | return -EIO; |
Daniel P. Berrange | f6fa64f | 2015-07-01 18:10:37 +0100 | [diff] [blame] | 488 | } |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 489 | ret = bdrv_pwrite(bs->file, |
| 490 | cluster_offset + i * 512, |
| 491 | s->cluster_data, 512); |
| 492 | if (ret < 0) { |
| 493 | return ret; |
| 494 | } |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 495 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
aliguori | 7f48fa1 | 2009-01-08 19:29:03 +0000 | [diff] [blame] | 498 | } else if (allocate == 2) { |
| 499 | cluster_offset |= QCOW_OFLAG_COMPRESSED | |
| 500 | (uint64_t)compressed_size << (63 - s->cluster_bits); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | /* update L2 table */ |
| 504 | tmp = cpu_to_be64(cluster_offset); |
| 505 | l2_table[l2_index] = tmp; |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 506 | ret = bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp), |
| 507 | &tmp, sizeof(tmp)); |
| 508 | if (ret < 0) { |
| 509 | return ret; |
| 510 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 511 | } |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 512 | *result = cluster_offset; |
| 513 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 516 | static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 517 | int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 518 | { |
| 519 | BDRVQcowState *s = bs->opaque; |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 520 | int index_in_cluster, n, ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 521 | uint64_t cluster_offset; |
| 522 | |
Stefan Hajnoczi | f8a2e5e | 2011-11-14 12:44:21 +0000 | [diff] [blame] | 523 | qemu_co_mutex_lock(&s->lock); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 524 | ret = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0, &cluster_offset); |
Stefan Hajnoczi | f8a2e5e | 2011-11-14 12:44:21 +0000 | [diff] [blame] | 525 | qemu_co_mutex_unlock(&s->lock); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 526 | if (ret < 0) { |
| 527 | return ret; |
| 528 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 529 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
| 530 | n = s->cluster_sectors - index_in_cluster; |
| 531 | if (n > nb_sectors) |
| 532 | n = nb_sectors; |
| 533 | *pnum = n; |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 534 | if (!cluster_offset) { |
| 535 | return 0; |
| 536 | } |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 537 | if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypto) { |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 538 | return BDRV_BLOCK_DATA; |
| 539 | } |
| 540 | cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS); |
Fam Zheng | 3064bf6 | 2016-01-26 11:58:49 +0800 | [diff] [blame] | 541 | *file = bs->file->bs; |
Paolo Bonzini | 4bc74be | 2013-09-04 19:00:30 +0200 | [diff] [blame] | 542 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, |
| 546 | const uint8_t *buf, int buf_size) |
| 547 | { |
| 548 | z_stream strm1, *strm = &strm1; |
| 549 | int ret, out_len; |
| 550 | |
| 551 | memset(strm, 0, sizeof(*strm)); |
| 552 | |
| 553 | strm->next_in = (uint8_t *)buf; |
| 554 | strm->avail_in = buf_size; |
| 555 | strm->next_out = out_buf; |
| 556 | strm->avail_out = out_buf_size; |
| 557 | |
| 558 | ret = inflateInit2(strm, -12); |
| 559 | if (ret != Z_OK) |
| 560 | return -1; |
| 561 | ret = inflate(strm, Z_FINISH); |
| 562 | out_len = strm->next_out - out_buf; |
| 563 | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || |
| 564 | out_len != out_buf_size) { |
| 565 | inflateEnd(strm); |
| 566 | return -1; |
| 567 | } |
| 568 | inflateEnd(strm); |
| 569 | return 0; |
| 570 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 571 | |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 572 | static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 573 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 574 | BDRVQcowState *s = bs->opaque; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 575 | int ret, csize; |
| 576 | uint64_t coffset; |
| 577 | |
| 578 | coffset = cluster_offset & s->cluster_offset_mask; |
| 579 | if (s->cluster_cache_offset != coffset) { |
| 580 | csize = cluster_offset >> (63 - s->cluster_bits); |
| 581 | csize &= (s->cluster_size - 1); |
Kevin Wolf | cf2ab8f | 2016-06-20 18:24:02 +0200 | [diff] [blame] | 582 | ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 583 | if (ret != csize) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 584 | return -1; |
| 585 | if (decompress_buffer(s->cluster_cache, s->cluster_size, |
| 586 | s->cluster_data, csize) < 0) { |
| 587 | return -1; |
| 588 | } |
| 589 | s->cluster_cache_offset = coffset; |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
Dong Xu Wang | a968168 | 2011-11-10 16:23:22 +0800 | [diff] [blame] | 594 | static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num, |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 595 | int nb_sectors, QEMUIOVector *qiov) |
| 596 | { |
| 597 | BDRVQcowState *s = bs->opaque; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 598 | int index_in_cluster; |
| 599 | int ret = 0, n; |
| 600 | uint64_t cluster_offset; |
| 601 | struct iovec hd_iov; |
| 602 | QEMUIOVector hd_qiov; |
| 603 | uint8_t *buf; |
| 604 | void *orig_buf; |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 605 | |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 606 | if (qiov->niov > 1) { |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 607 | buf = orig_buf = qemu_try_blockalign(bs, qiov->size); |
| 608 | if (buf == NULL) { |
| 609 | return -ENOMEM; |
| 610 | } |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 611 | } else { |
| 612 | orig_buf = NULL; |
| 613 | buf = (uint8_t *)qiov->iov->iov_base; |
| 614 | } |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 615 | |
| 616 | qemu_co_mutex_lock(&s->lock); |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 617 | |
| 618 | while (nb_sectors != 0) { |
| 619 | /* prepare next request */ |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 620 | ret = get_cluster_offset(bs, sector_num << 9, |
| 621 | 0, 0, 0, 0, &cluster_offset); |
| 622 | if (ret < 0) { |
| 623 | break; |
| 624 | } |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 625 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
| 626 | n = s->cluster_sectors - index_in_cluster; |
| 627 | if (n > nb_sectors) { |
| 628 | n = nb_sectors; |
| 629 | } |
| 630 | |
| 631 | if (!cluster_offset) { |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 632 | if (bs->backing) { |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 633 | /* read from the base image */ |
| 634 | hd_iov.iov_base = (void *)buf; |
| 635 | hd_iov.iov_len = n * 512; |
| 636 | qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); |
| 637 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 28b04a8 | 2016-05-24 17:21:22 +0200 | [diff] [blame] | 638 | ret = bdrv_co_readv(bs->backing, sector_num, n, &hd_qiov); |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 639 | qemu_co_mutex_lock(&s->lock); |
| 640 | if (ret < 0) { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 641 | break; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 642 | } |
| 643 | } else { |
| 644 | /* Note: in this case, no need to wait */ |
| 645 | memset(buf, 0, 512 * n); |
| 646 | } |
| 647 | } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
| 648 | /* add AIO support for compressed blocks ? */ |
| 649 | if (decompress_cluster(bs, cluster_offset) < 0) { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 650 | ret = -EIO; |
| 651 | break; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 652 | } |
| 653 | memcpy(buf, |
| 654 | s->cluster_cache + index_in_cluster * 512, 512 * n); |
| 655 | } else { |
| 656 | if ((cluster_offset & 511) != 0) { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 657 | ret = -EIO; |
| 658 | break; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 659 | } |
| 660 | hd_iov.iov_base = (void *)buf; |
| 661 | hd_iov.iov_len = n * 512; |
| 662 | qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); |
| 663 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 28b04a8 | 2016-05-24 17:21:22 +0200 | [diff] [blame] | 664 | ret = bdrv_co_readv(bs->file, |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 665 | (cluster_offset >> 9) + index_in_cluster, |
| 666 | n, &hd_qiov); |
| 667 | qemu_co_mutex_lock(&s->lock); |
| 668 | if (ret < 0) { |
| 669 | break; |
| 670 | } |
Daniel P. Berrange | 8336aaf | 2015-05-12 17:09:18 +0100 | [diff] [blame] | 671 | if (bs->encrypted) { |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 672 | assert(s->crypto); |
Daniel P. Berrange | 4609742 | 2017-09-27 13:53:39 +0100 | [diff] [blame] | 673 | if (qcrypto_block_decrypt(s->crypto, |
| 674 | sector_num * BDRV_SECTOR_SIZE, buf, |
Alberto Garcia | c3a8fe3 | 2017-08-29 15:08:36 +0300 | [diff] [blame] | 675 | n * BDRV_SECTOR_SIZE, NULL) < 0) { |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 676 | ret = -EIO; |
| 677 | break; |
Daniel P. Berrange | f6fa64f | 2015-07-01 18:10:37 +0100 | [diff] [blame] | 678 | } |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | ret = 0; |
| 682 | |
| 683 | nb_sectors -= n; |
| 684 | sector_num += n; |
| 685 | buf += n * 512; |
| 686 | } |
| 687 | |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 688 | qemu_co_mutex_unlock(&s->lock); |
| 689 | |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 690 | if (qiov->niov > 1) { |
Michael Tokarev | 0339614 | 2012-06-07 20:17:55 +0400 | [diff] [blame] | 691 | qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size); |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 692 | qemu_vfree(orig_buf); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 693 | } |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 694 | |
| 695 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Dong Xu Wang | a968168 | 2011-11-10 16:23:22 +0800 | [diff] [blame] | 698 | static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num, |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 699 | int nb_sectors, QEMUIOVector *qiov) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 700 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 701 | BDRVQcowState *s = bs->opaque; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 702 | int index_in_cluster; |
| 703 | uint64_t cluster_offset; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 704 | int ret = 0, n; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 705 | struct iovec hd_iov; |
| 706 | QEMUIOVector hd_qiov; |
| 707 | uint8_t *buf; |
| 708 | void *orig_buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 709 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 710 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
| 711 | |
Daniel P. Berrange | 1fad1f9 | 2017-06-23 17:24:07 +0100 | [diff] [blame] | 712 | /* We must always copy the iov when encrypting, so we |
| 713 | * don't modify the original data buffer during encryption */ |
| 714 | if (bs->encrypted || qiov->niov > 1) { |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 715 | buf = orig_buf = qemu_try_blockalign(bs, qiov->size); |
| 716 | if (buf == NULL) { |
| 717 | return -ENOMEM; |
| 718 | } |
Michael Tokarev | d5e6b16 | 2012-06-07 20:21:06 +0400 | [diff] [blame] | 719 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 720 | } else { |
| 721 | orig_buf = NULL; |
| 722 | buf = (uint8_t *)qiov->iov->iov_base; |
| 723 | } |
Christoph Hellwig | ad53089 | 2009-05-25 15:45:37 +0200 | [diff] [blame] | 724 | |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 725 | qemu_co_mutex_lock(&s->lock); |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 726 | |
| 727 | while (nb_sectors != 0) { |
| 728 | |
| 729 | index_in_cluster = sector_num & (s->cluster_sectors - 1); |
| 730 | n = s->cluster_sectors - index_in_cluster; |
| 731 | if (n > nb_sectors) { |
| 732 | n = nb_sectors; |
| 733 | } |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 734 | ret = get_cluster_offset(bs, sector_num << 9, 1, 0, |
| 735 | index_in_cluster, |
| 736 | index_in_cluster + n, &cluster_offset); |
| 737 | if (ret < 0) { |
| 738 | break; |
| 739 | } |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 740 | if (!cluster_offset || (cluster_offset & 511) != 0) { |
| 741 | ret = -EIO; |
| 742 | break; |
| 743 | } |
Daniel P. Berrange | 8336aaf | 2015-05-12 17:09:18 +0100 | [diff] [blame] | 744 | if (bs->encrypted) { |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 745 | assert(s->crypto); |
Daniel P. Berrange | 4609742 | 2017-09-27 13:53:39 +0100 | [diff] [blame] | 746 | if (qcrypto_block_encrypt(s->crypto, sector_num * BDRV_SECTOR_SIZE, |
| 747 | buf, n * BDRV_SECTOR_SIZE, NULL) < 0) { |
Daniel P. Berrange | f6fa64f | 2015-07-01 18:10:37 +0100 | [diff] [blame] | 748 | ret = -EIO; |
| 749 | break; |
| 750 | } |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 751 | } |
| 752 | |
Daniel P. Berrange | 1fad1f9 | 2017-06-23 17:24:07 +0100 | [diff] [blame] | 753 | hd_iov.iov_base = (void *)buf; |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 754 | hd_iov.iov_len = n * 512; |
| 755 | qemu_iovec_init_external(&hd_qiov, &hd_iov, 1); |
| 756 | qemu_co_mutex_unlock(&s->lock); |
Kevin Wolf | 25ec177 | 2016-05-24 17:21:22 +0200 | [diff] [blame] | 757 | ret = bdrv_co_writev(bs->file, |
Frediano Ziglio | 27deebe | 2011-08-23 15:21:11 +0200 | [diff] [blame] | 758 | (cluster_offset >> 9) + index_in_cluster, |
| 759 | n, &hd_qiov); |
| 760 | qemu_co_mutex_lock(&s->lock); |
| 761 | if (ret < 0) { |
| 762 | break; |
| 763 | } |
| 764 | ret = 0; |
| 765 | |
| 766 | nb_sectors -= n; |
| 767 | sector_num += n; |
| 768 | buf += n * 512; |
| 769 | } |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 770 | qemu_co_mutex_unlock(&s->lock); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 771 | |
Daniel P. Berrange | 1fad1f9 | 2017-06-23 17:24:07 +0100 | [diff] [blame] | 772 | qemu_vfree(orig_buf); |
Kevin Wolf | b11a24d | 2011-06-07 15:20:44 +0200 | [diff] [blame] | 773 | |
Kevin Wolf | 52b8eb6 | 2011-07-15 16:27:42 +0200 | [diff] [blame] | 774 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 775 | } |
| 776 | |
bellard | e2731ad | 2004-09-18 19:32:11 +0000 | [diff] [blame] | 777 | static void qcow_close(BlockDriverState *bs) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 778 | { |
| 779 | BDRVQcowState *s = bs->opaque; |
Kevin Wolf | fd9f102 | 2011-11-22 16:44:45 +0100 | [diff] [blame] | 780 | |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 781 | qcrypto_block_free(s->crypto); |
| 782 | s->crypto = NULL; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 783 | g_free(s->l1_table); |
Kevin Wolf | 0df9330 | 2014-05-20 13:36:05 +0200 | [diff] [blame] | 784 | qemu_vfree(s->l2_cache); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 785 | g_free(s->cluster_cache); |
| 786 | g_free(s->cluster_data); |
Kevin Wolf | fd9f102 | 2011-11-22 16:44:45 +0100 | [diff] [blame] | 787 | |
| 788 | migrate_del_blocker(s->migration_blocker); |
| 789 | error_free(s->migration_blocker); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 792 | static int qcow_create(const char *filename, QemuOpts *opts, Error **errp) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 793 | { |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 794 | int header_size, backing_filename_len, l1_size, shift, i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 795 | QCowHeader header; |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 796 | uint8_t *tmp; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 797 | int64_t total_size = 0; |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 798 | char *backing_file = NULL; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 799 | Error *local_err = NULL; |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 800 | int ret; |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 801 | BlockBackend *qcow_blk; |
Daniel P. Berrange | 0696ae2 | 2017-07-14 11:31:05 +0100 | [diff] [blame] | 802 | char *encryptfmt = NULL; |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 803 | QDict *options; |
| 804 | QDict *encryptopts = NULL; |
| 805 | QCryptoBlockCreateOptions *crypto_opts = NULL; |
| 806 | QCryptoBlock *crypto = NULL; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 807 | |
| 808 | /* Read out options */ |
Hu Tao | 180e952 | 2014-09-10 17:05:46 +0800 | [diff] [blame] | 809 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 810 | BDRV_SECTOR_SIZE); |
Daniel P. Berrange | 6aa837f | 2017-06-23 17:24:03 +0100 | [diff] [blame] | 811 | if (total_size == 0) { |
| 812 | error_setg(errp, "Image size is too small, cannot be zero length"); |
| 813 | ret = -EINVAL; |
| 814 | goto cleanup; |
| 815 | } |
| 816 | |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 817 | backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 818 | encryptfmt = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
| 819 | if (encryptfmt) { |
| 820 | if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) { |
| 821 | error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and " |
| 822 | BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive"); |
| 823 | ret = -EINVAL; |
| 824 | goto cleanup; |
| 825 | } |
| 826 | } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) { |
Daniel P. Berrange | 0696ae2 | 2017-07-14 11:31:05 +0100 | [diff] [blame] | 827 | encryptfmt = g_strdup("aes"); |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 828 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 829 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 830 | ret = bdrv_create_file(filename, opts, &local_err); |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 831 | if (ret < 0) { |
Paolo Bonzini | b6d5066 | 2014-02-17 14:43:58 +0100 | [diff] [blame] | 832 | error_propagate(errp, local_err); |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 833 | goto cleanup; |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 836 | qcow_blk = blk_new_open(filename, NULL, NULL, |
Kevin Wolf | 5588060 | 2017-02-17 15:07:38 +0100 | [diff] [blame] | 837 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, |
| 838 | &local_err); |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 839 | if (qcow_blk == NULL) { |
Paolo Bonzini | b6d5066 | 2014-02-17 14:43:58 +0100 | [diff] [blame] | 840 | error_propagate(errp, local_err); |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 841 | ret = -EIO; |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 842 | goto cleanup; |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 843 | } |
| 844 | |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 845 | blk_set_allow_write_beyond_eof(qcow_blk, true); |
| 846 | |
Max Reitz | 3a691c5 | 2017-06-13 22:20:54 +0200 | [diff] [blame] | 847 | ret = blk_truncate(qcow_blk, 0, PREALLOC_MODE_OFF, errp); |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 848 | if (ret < 0) { |
| 849 | goto exit; |
| 850 | } |
| 851 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 852 | memset(&header, 0, sizeof(header)); |
| 853 | header.magic = cpu_to_be32(QCOW_MAGIC); |
| 854 | header.version = cpu_to_be32(QCOW_VERSION); |
Hu Tao | 180e952 | 2014-09-10 17:05:46 +0800 | [diff] [blame] | 855 | header.size = cpu_to_be64(total_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 856 | header_size = sizeof(header); |
| 857 | backing_filename_len = 0; |
| 858 | if (backing_file) { |
aurel32 | 7852e5d | 2008-03-18 06:52:48 +0000 | [diff] [blame] | 859 | if (strcmp(backing_file, "fat:")) { |
| 860 | header.backing_file_offset = cpu_to_be64(header_size); |
| 861 | backing_filename_len = strlen(backing_file); |
| 862 | header.backing_file_size = cpu_to_be32(backing_filename_len); |
| 863 | header_size += backing_filename_len; |
| 864 | } else { |
| 865 | /* special backing file for vvfat */ |
Peter Maydell | 272545c | 2017-06-05 14:55:54 +0100 | [diff] [blame] | 866 | g_free(backing_file); |
aurel32 | 7852e5d | 2008-03-18 06:52:48 +0000 | [diff] [blame] | 867 | backing_file = NULL; |
| 868 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 869 | header.cluster_bits = 9; /* 512 byte cluster to avoid copying |
Deepak Kathayat | dc6fb73 | 2014-03-24 16:30:17 +0800 | [diff] [blame] | 870 | unmodified sectors */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 871 | header.l2_bits = 12; /* 32 KB L2 tables */ |
| 872 | } else { |
| 873 | header.cluster_bits = 12; /* 4 KB clusters */ |
| 874 | header.l2_bits = 9; /* 4 KB L2 tables */ |
| 875 | } |
| 876 | header_size = (header_size + 7) & ~7; |
| 877 | shift = header.cluster_bits + header.l2_bits; |
Hu Tao | 180e952 | 2014-09-10 17:05:46 +0800 | [diff] [blame] | 878 | l1_size = (total_size + (1LL << shift) - 1) >> shift; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 879 | |
| 880 | header.l1_table_offset = cpu_to_be64(header_size); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 881 | |
| 882 | options = qemu_opts_to_qdict(opts, NULL); |
| 883 | qdict_extract_subqdict(options, &encryptopts, "encrypt."); |
| 884 | QDECREF(options); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 885 | if (encryptfmt) { |
| 886 | if (!g_str_equal(encryptfmt, "aes")) { |
| 887 | error_setg(errp, "Unknown encryption format '%s', expected 'aes'", |
| 888 | encryptfmt); |
| 889 | ret = -EINVAL; |
| 890 | goto exit; |
| 891 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 892 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 893 | |
| 894 | crypto_opts = block_crypto_create_opts_init( |
| 895 | Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp); |
| 896 | if (!crypto_opts) { |
| 897 | ret = -EINVAL; |
| 898 | goto exit; |
| 899 | } |
| 900 | |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 901 | crypto = qcrypto_block_create(crypto_opts, "encrypt.", |
| 902 | NULL, NULL, NULL, errp); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 903 | if (!crypto) { |
| 904 | ret = -EINVAL; |
| 905 | goto exit; |
| 906 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 907 | } else { |
| 908 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
| 909 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 910 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 911 | /* write all the data */ |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 912 | ret = blk_pwrite(qcow_blk, 0, &header, sizeof(header), 0); |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 913 | if (ret != sizeof(header)) { |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 914 | goto exit; |
| 915 | } |
| 916 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 917 | if (backing_file) { |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 918 | ret = blk_pwrite(qcow_blk, sizeof(header), |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 919 | backing_file, backing_filename_len, 0); |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 920 | if (ret != backing_filename_len) { |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 921 | goto exit; |
| 922 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 923 | } |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 924 | |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 925 | tmp = g_malloc0(BDRV_SECTOR_SIZE); |
Laurent Vivier | d737b78 | 2016-05-31 18:35:52 +0200 | [diff] [blame] | 926 | for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE); |
| 927 | i++) { |
Eric Blake | 8341f00 | 2016-05-06 10:26:27 -0600 | [diff] [blame] | 928 | ret = blk_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i, |
| 929 | tmp, BDRV_SECTOR_SIZE, 0); |
Li Zhi Hui | 2b16c9f | 2011-11-21 15:40:39 +0800 | [diff] [blame] | 930 | if (ret != BDRV_SECTOR_SIZE) { |
| 931 | g_free(tmp); |
| 932 | goto exit; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | g_free(tmp); |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 937 | ret = 0; |
| 938 | exit: |
Kevin Wolf | 6af4016 | 2016-03-08 15:57:05 +0100 | [diff] [blame] | 939 | blk_unref(qcow_blk); |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 940 | cleanup: |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 941 | QDECREF(encryptopts); |
Daniel P. Berrange | 0696ae2 | 2017-07-14 11:31:05 +0100 | [diff] [blame] | 942 | g_free(encryptfmt); |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 943 | qcrypto_block_free(crypto); |
| 944 | qapi_free_QCryptoBlockCreateOptions(crypto_opts); |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 945 | g_free(backing_file); |
Kirill A. Shutemov | 3e1a813 | 2010-01-20 00:56:12 +0100 | [diff] [blame] | 946 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 947 | } |
| 948 | |
bellard | c47c33b | 2006-08-05 21:32:10 +0000 | [diff] [blame] | 949 | static int qcow_make_empty(BlockDriverState *bs) |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 950 | { |
| 951 | BDRVQcowState *s = bs->opaque; |
| 952 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 953 | int ret; |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 954 | |
| 955 | memset(s->l1_table, 0, l1_length); |
Kevin Wolf | d9ca2ea | 2016-06-20 20:09:15 +0200 | [diff] [blame] | 956 | if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table, |
Kevin Wolf | 5e5557d | 2010-06-18 16:11:53 +0200 | [diff] [blame] | 957 | l1_length) < 0) |
| 958 | return -1; |
Max Reitz | 7ea37c3 | 2017-06-13 22:20:53 +0200 | [diff] [blame] | 959 | ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, |
| 960 | PREALLOC_MODE_OFF, NULL); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 961 | if (ret < 0) |
| 962 | return ret; |
bellard | 95389c8 | 2005-12-18 18:28:15 +0000 | [diff] [blame] | 963 | |
| 964 | memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
| 965 | memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); |
| 966 | memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 971 | /* XXX: put compressed sectors first, then all the cluster aligned |
| 972 | tables to avoid losing bytes in alignment */ |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 973 | static coroutine_fn int |
| 974 | qcow_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, |
| 975 | uint64_t bytes, QEMUIOVector *qiov) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 976 | { |
| 977 | BDRVQcowState *s = bs->opaque; |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 978 | QEMUIOVector hd_qiov; |
| 979 | struct iovec iov; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 980 | z_stream strm; |
| 981 | int ret, out_len; |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 982 | uint8_t *buf, *out_buf; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 983 | uint64_t cluster_offset; |
| 984 | |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 985 | buf = qemu_blockalign(bs, s->cluster_size); |
Pavel Butsykin | 655923d | 2016-07-22 11:17:47 +0300 | [diff] [blame] | 986 | if (bytes != s->cluster_size) { |
| 987 | if (bytes > s->cluster_size || |
| 988 | offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS) |
| 989 | { |
| 990 | qemu_vfree(buf); |
| 991 | return -EINVAL; |
| 992 | } |
| 993 | /* Zero-pad last write if image size is not cluster aligned */ |
| 994 | memset(buf + bytes, 0, s->cluster_size - bytes); |
| 995 | } |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 996 | qemu_iovec_to_buf(qiov, 0, buf, qiov->size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 997 | |
Vladimir Sementsov-Ogievskiy | ebf7bba | 2016-07-14 19:59:25 +0300 | [diff] [blame] | 998 | out_buf = g_malloc(s->cluster_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 999 | |
| 1000 | /* best compression, small window, no zlib header */ |
| 1001 | memset(&strm, 0, sizeof(strm)); |
| 1002 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1003 | Z_DEFLATED, -12, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1004 | 9, Z_DEFAULT_STRATEGY); |
| 1005 | if (ret != 0) { |
Kevin Wolf | 64ebe71 | 2011-10-26 11:21:50 +0200 | [diff] [blame] | 1006 | ret = -EINVAL; |
| 1007 | goto fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | strm.avail_in = s->cluster_size; |
| 1011 | strm.next_in = (uint8_t *)buf; |
| 1012 | strm.avail_out = s->cluster_size; |
| 1013 | strm.next_out = out_buf; |
| 1014 | |
| 1015 | ret = deflate(&strm, Z_FINISH); |
| 1016 | if (ret != Z_STREAM_END && ret != Z_OK) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1017 | deflateEnd(&strm); |
Kevin Wolf | 64ebe71 | 2011-10-26 11:21:50 +0200 | [diff] [blame] | 1018 | ret = -EINVAL; |
| 1019 | goto fail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1020 | } |
| 1021 | out_len = strm.next_out - out_buf; |
| 1022 | |
| 1023 | deflateEnd(&strm); |
| 1024 | |
| 1025 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { |
| 1026 | /* could not compress: write normal cluster */ |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1027 | ret = qcow_co_writev(bs, offset >> BDRV_SECTOR_BITS, |
| 1028 | bytes >> BDRV_SECTOR_BITS, qiov); |
Kevin Wolf | 64ebe71 | 2011-10-26 11:21:50 +0200 | [diff] [blame] | 1029 | if (ret < 0) { |
| 1030 | goto fail; |
| 1031 | } |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1032 | goto success; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1033 | } |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1034 | qemu_co_mutex_lock(&s->lock); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 1035 | ret = get_cluster_offset(bs, offset, 2, out_len, 0, 0, &cluster_offset); |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1036 | qemu_co_mutex_unlock(&s->lock); |
Eric Blake | 56439e9 | 2017-08-09 15:38:05 -0500 | [diff] [blame] | 1037 | if (ret < 0) { |
| 1038 | goto fail; |
| 1039 | } |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1040 | if (cluster_offset == 0) { |
| 1041 | ret = -EIO; |
| 1042 | goto fail; |
| 1043 | } |
| 1044 | cluster_offset &= s->cluster_offset_mask; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1045 | |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1046 | iov = (struct iovec) { |
| 1047 | .iov_base = out_buf, |
| 1048 | .iov_len = out_len, |
| 1049 | }; |
| 1050 | qemu_iovec_init_external(&hd_qiov, &iov, 1); |
| 1051 | ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0); |
| 1052 | if (ret < 0) { |
| 1053 | goto fail; |
| 1054 | } |
| 1055 | success: |
Kevin Wolf | 64ebe71 | 2011-10-26 11:21:50 +0200 | [diff] [blame] | 1056 | ret = 0; |
| 1057 | fail: |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1058 | qemu_vfree(buf); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1059 | g_free(out_buf); |
Kevin Wolf | 64ebe71 | 2011-10-26 11:21:50 +0200 | [diff] [blame] | 1060 | return ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
bellard | c47c33b | 2006-08-05 21:32:10 +0000 | [diff] [blame] | 1063 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1064 | { |
| 1065 | BDRVQcowState *s = bs->opaque; |
| 1066 | bdi->cluster_size = s->cluster_size; |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 1070 | static QemuOptsList qcow_create_opts = { |
| 1071 | .name = "qcow-create-opts", |
| 1072 | .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head), |
| 1073 | .desc = { |
| 1074 | { |
| 1075 | .name = BLOCK_OPT_SIZE, |
| 1076 | .type = QEMU_OPT_SIZE, |
| 1077 | .help = "Virtual disk size" |
| 1078 | }, |
| 1079 | { |
| 1080 | .name = BLOCK_OPT_BACKING_FILE, |
| 1081 | .type = QEMU_OPT_STRING, |
| 1082 | .help = "File name of a base image" |
| 1083 | }, |
| 1084 | { |
| 1085 | .name = BLOCK_OPT_ENCRYPT, |
| 1086 | .type = QEMU_OPT_BOOL, |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 1087 | .help = "Encrypt the image with format 'aes'. (Deprecated " |
| 1088 | "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)", |
| 1089 | }, |
| 1090 | { |
| 1091 | .name = BLOCK_OPT_ENCRYPT_FORMAT, |
| 1092 | .type = QEMU_OPT_STRING, |
| 1093 | .help = "Encrypt the image, format choices: 'aes'", |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 1094 | }, |
Daniel P. Berrange | d85f422 | 2017-06-23 17:24:08 +0100 | [diff] [blame] | 1095 | BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."), |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 1096 | { /* end of list */ } |
| 1097 | } |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1098 | }; |
| 1099 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1100 | static BlockDriver bdrv_qcow = { |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1101 | .format_name = "qcow", |
| 1102 | .instance_size = sizeof(BDRVQcowState), |
| 1103 | .bdrv_probe = qcow_probe, |
| 1104 | .bdrv_open = qcow_open, |
| 1105 | .bdrv_close = qcow_close, |
Kevin Wolf | 862f215 | 2016-12-19 16:36:02 +0100 | [diff] [blame] | 1106 | .bdrv_child_perm = bdrv_format_default_perms, |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 1107 | .bdrv_reopen_prepare = qcow_reopen_prepare, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 1108 | .bdrv_create = qcow_create, |
Peter Lieven | 3ac2162 | 2013-06-28 12:47:42 +0200 | [diff] [blame] | 1109 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
Kevin Wolf | 8ee79e7 | 2014-06-04 15:09:35 +0200 | [diff] [blame] | 1110 | .supports_backing = true, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1111 | |
| 1112 | .bdrv_co_readv = qcow_co_readv, |
| 1113 | .bdrv_co_writev = qcow_co_writev, |
Paolo Bonzini | b6b8a33 | 2013-09-04 19:00:28 +0200 | [diff] [blame] | 1114 | .bdrv_co_get_block_status = qcow_co_get_block_status, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1115 | |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1116 | .bdrv_make_empty = qcow_make_empty, |
Pavel Butsykin | f2b95a1 | 2016-07-22 11:17:46 +0300 | [diff] [blame] | 1117 | .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 1118 | .bdrv_get_info = qcow_get_info, |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1119 | |
Chunyan Liu | 16d1215 | 2014-06-05 17:20:57 +0800 | [diff] [blame] | 1120 | .create_opts = &qcow_create_opts, |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1121 | }; |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1122 | |
| 1123 | static void bdrv_qcow_init(void) |
| 1124 | { |
| 1125 | bdrv_register(&bdrv_qcow); |
| 1126 | } |
| 1127 | |
| 1128 | block_init(bdrv_qcow_init); |