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