bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for the QCOW version 2 format |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 585f858 | 2006-08-05 21:14:20 +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 | */ |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 25 | #include "block_int.h" |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 26 | #include "module.h" |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 27 | #include <zlib.h> |
| 28 | #include "aes.h" |
Kevin Wolf | f7d0fe0 | 2009-05-28 16:07:04 +0200 | [diff] [blame] | 29 | #include "block/qcow2.h" |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | Differences with QCOW: |
| 33 | |
| 34 | - Support for multiple incremental snapshots. |
| 35 | - Memory management by reference counts. |
| 36 | - Clusters which have a reference count of one have the bit |
| 37 | QCOW_OFLAG_COPIED to optimize write performance. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 38 | - Size of compressed clusters is stored in sectors to reduce bit usage |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 39 | in the cluster offsets. |
| 40 | - Support for storing additional data (such as the VM state) in the |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 41 | snapshots. |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 42 | - If a backing store is used, the cluster size is not constrained |
| 43 | (could be backported to QCOW). |
| 44 | - L2 tables have always a size of one cluster. |
| 45 | */ |
| 46 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 47 | |
| 48 | typedef struct { |
| 49 | uint32_t magic; |
| 50 | uint32_t len; |
| 51 | } QCowExtension; |
| 52 | #define QCOW_EXT_MAGIC_END 0 |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 53 | #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 54 | |
| 55 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 56 | |
| 57 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
| 58 | { |
| 59 | const QCowHeader *cow_header = (const void *)buf; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 60 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 61 | if (buf_size >= sizeof(QCowHeader) && |
| 62 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 63 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 64 | return 100; |
| 65 | else |
| 66 | return 0; |
| 67 | } |
| 68 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * read qcow2 extension and fill bs |
| 72 | * start reading from start_offset |
| 73 | * finish reading upon magic of value 0 or when end_offset reached |
| 74 | * unknown magic is skipped (future extension this version knows nothing about) |
| 75 | * return 0 upon success, non-0 otherwise |
| 76 | */ |
| 77 | static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
| 78 | uint64_t end_offset) |
| 79 | { |
| 80 | BDRVQcowState *s = bs->opaque; |
| 81 | QCowExtension ext; |
| 82 | uint64_t offset; |
| 83 | |
| 84 | #ifdef DEBUG_EXT |
| 85 | printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); |
| 86 | #endif |
| 87 | offset = start_offset; |
| 88 | while (offset < end_offset) { |
| 89 | |
| 90 | #ifdef DEBUG_EXT |
| 91 | /* Sanity check */ |
| 92 | if (offset > s->cluster_size) |
| 93 | printf("qcow_handle_extension: suspicious offset %lu\n", offset); |
| 94 | |
| 95 | printf("attemting to read extended header in offset %lu\n", offset); |
| 96 | #endif |
| 97 | |
| 98 | if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
aliguori | 4c97807 | 2009-03-29 01:31:56 +0000 | [diff] [blame] | 99 | fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n", |
| 100 | (unsigned long long)offset); |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 101 | return 1; |
| 102 | } |
| 103 | be32_to_cpus(&ext.magic); |
| 104 | be32_to_cpus(&ext.len); |
| 105 | offset += sizeof(ext); |
| 106 | #ifdef DEBUG_EXT |
| 107 | printf("ext.magic = 0x%x\n", ext.magic); |
| 108 | #endif |
| 109 | switch (ext.magic) { |
| 110 | case QCOW_EXT_MAGIC_END: |
| 111 | return 0; |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 112 | |
| 113 | case QCOW_EXT_MAGIC_BACKING_FORMAT: |
| 114 | if (ext.len >= sizeof(bs->backing_format)) { |
| 115 | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" |
aliguori | 4c97807 | 2009-03-29 01:31:56 +0000 | [diff] [blame] | 116 | " (>=%zu)\n", |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 117 | ext.len, sizeof(bs->backing_format)); |
| 118 | return 2; |
| 119 | } |
| 120 | if (bdrv_pread(s->hd, offset , bs->backing_format, |
| 121 | ext.len) != ext.len) |
| 122 | return 3; |
| 123 | bs->backing_format[ext.len] = '\0'; |
| 124 | #ifdef DEBUG_EXT |
| 125 | printf("Qcow2: Got format extension %s\n", bs->backing_format); |
| 126 | #endif |
| 127 | offset += ((ext.len + 7) & ~7); |
| 128 | break; |
| 129 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 130 | default: |
| 131 | /* unknown magic -- just skip it */ |
| 132 | offset += ((ext.len + 7) & ~7); |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 141 | static int qcow_open(BlockDriverState *bs, const char *filename, int flags) |
| 142 | { |
| 143 | BDRVQcowState *s = bs->opaque; |
| 144 | int len, i, shift, ret; |
| 145 | QCowHeader header; |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 146 | uint64_t ext_end; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 147 | |
aurel32 | b5eff35 | 2008-03-11 23:30:22 +0000 | [diff] [blame] | 148 | ret = bdrv_file_open(&s->hd, filename, flags); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) |
| 152 | goto fail; |
| 153 | be32_to_cpus(&header.magic); |
| 154 | be32_to_cpus(&header.version); |
| 155 | be64_to_cpus(&header.backing_file_offset); |
| 156 | be32_to_cpus(&header.backing_file_size); |
| 157 | be64_to_cpus(&header.size); |
| 158 | be32_to_cpus(&header.cluster_bits); |
| 159 | be32_to_cpus(&header.crypt_method); |
| 160 | be64_to_cpus(&header.l1_table_offset); |
| 161 | be32_to_cpus(&header.l1_size); |
| 162 | be64_to_cpus(&header.refcount_table_offset); |
| 163 | be32_to_cpus(&header.refcount_table_clusters); |
| 164 | be64_to_cpus(&header.snapshots_offset); |
| 165 | be32_to_cpus(&header.nb_snapshots); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 166 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 167 | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
| 168 | goto fail; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 169 | if (header.size <= 1 || |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 170 | header.cluster_bits < MIN_CLUSTER_BITS || |
| 171 | header.cluster_bits > MAX_CLUSTER_BITS) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 172 | goto fail; |
| 173 | if (header.crypt_method > QCOW_CRYPT_AES) |
| 174 | goto fail; |
| 175 | s->crypt_method_header = header.crypt_method; |
| 176 | if (s->crypt_method_header) |
| 177 | bs->encrypted = 1; |
| 178 | s->cluster_bits = header.cluster_bits; |
| 179 | s->cluster_size = 1 << s->cluster_bits; |
| 180 | s->cluster_sectors = 1 << (s->cluster_bits - 9); |
| 181 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
| 182 | s->l2_size = 1 << s->l2_bits; |
| 183 | bs->total_sectors = header.size / 512; |
| 184 | s->csize_shift = (62 - (s->cluster_bits - 8)); |
| 185 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; |
| 186 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; |
| 187 | s->refcount_table_offset = header.refcount_table_offset; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 188 | s->refcount_table_size = |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 189 | header.refcount_table_clusters << (s->cluster_bits - 3); |
| 190 | |
| 191 | s->snapshots_offset = header.snapshots_offset; |
| 192 | s->nb_snapshots = header.nb_snapshots; |
| 193 | |
| 194 | /* read the level 1 table */ |
| 195 | s->l1_size = header.l1_size; |
| 196 | shift = s->cluster_bits + s->l2_bits; |
| 197 | s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; |
| 198 | /* the L1 table must contain at least enough entries to put |
| 199 | header.size bytes */ |
| 200 | if (s->l1_size < s->l1_vm_state_index) |
| 201 | goto fail; |
| 202 | s->l1_table_offset = header.l1_table_offset; |
Kevin Wolf | 3f6a3ee | 2009-07-07 18:09:42 +0200 | [diff] [blame] | 203 | s->l1_table = qemu_mallocz( |
| 204 | align_offset(s->l1_size * sizeof(uint64_t), 512)); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 205 | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 206 | s->l1_size * sizeof(uint64_t)) |
| 207 | goto fail; |
| 208 | for(i = 0;i < s->l1_size; i++) { |
| 209 | be64_to_cpus(&s->l1_table[i]); |
| 210 | } |
| 211 | /* alloc L2 cache */ |
| 212 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 213 | s->cluster_cache = qemu_malloc(s->cluster_size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 214 | /* one more sector for decompressed data alignment */ |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 215 | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
| 216 | + 512); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 217 | s->cluster_cache_offset = -1; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 218 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 219 | if (qcow2_refcount_init(bs) < 0) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 220 | goto fail; |
| 221 | |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 222 | LIST_INIT(&s->cluster_allocs); |
| 223 | |
aliguori | 9b80ddf | 2009-03-28 17:55:06 +0000 | [diff] [blame] | 224 | /* read qcow2 extensions */ |
| 225 | if (header.backing_file_offset) |
| 226 | ext_end = header.backing_file_offset; |
| 227 | else |
| 228 | ext_end = s->cluster_size; |
| 229 | if (qcow_read_extensions(bs, sizeof(header), ext_end)) |
| 230 | goto fail; |
| 231 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 232 | /* read the backing file name */ |
| 233 | if (header.backing_file_offset != 0) { |
| 234 | len = header.backing_file_size; |
| 235 | if (len > 1023) |
| 236 | len = 1023; |
| 237 | if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) |
| 238 | goto fail; |
| 239 | bs->backing_file[len] = '\0'; |
| 240 | } |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 241 | if (qcow2_read_snapshots(bs) < 0) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 242 | goto fail; |
| 243 | |
| 244 | #ifdef DEBUG_ALLOC |
Filip Navara | 14899cd | 2009-06-23 19:17:30 -0500 | [diff] [blame] | 245 | qcow2_check_refcounts(bs); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 246 | #endif |
| 247 | return 0; |
| 248 | |
| 249 | fail: |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 250 | qcow2_free_snapshots(bs); |
| 251 | qcow2_refcount_close(bs); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 252 | qemu_free(s->l1_table); |
| 253 | qemu_free(s->l2_cache); |
| 254 | qemu_free(s->cluster_cache); |
| 255 | qemu_free(s->cluster_data); |
| 256 | bdrv_delete(s->hd); |
| 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | static int qcow_set_key(BlockDriverState *bs, const char *key) |
| 261 | { |
| 262 | BDRVQcowState *s = bs->opaque; |
| 263 | uint8_t keybuf[16]; |
| 264 | int len, i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 265 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 266 | memset(keybuf, 0, 16); |
| 267 | len = strlen(key); |
| 268 | if (len > 16) |
| 269 | len = 16; |
| 270 | /* XXX: we could compress the chars to 7 bits to increase |
| 271 | entropy */ |
| 272 | for(i = 0;i < len;i++) { |
| 273 | keybuf[i] = key[i]; |
| 274 | } |
| 275 | s->crypt_method = s->crypt_method_header; |
| 276 | |
| 277 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
| 278 | return -1; |
| 279 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
| 280 | return -1; |
| 281 | #if 0 |
| 282 | /* test */ |
| 283 | { |
| 284 | uint8_t in[16]; |
| 285 | uint8_t out[16]; |
| 286 | uint8_t tmp[16]; |
| 287 | for(i=0;i<16;i++) |
| 288 | in[i] = i; |
| 289 | AES_encrypt(in, tmp, &s->aes_encrypt_key); |
| 290 | AES_decrypt(tmp, out, &s->aes_decrypt_key); |
| 291 | for(i = 0; i < 16; i++) |
| 292 | printf(" %02x", tmp[i]); |
| 293 | printf("\n"); |
| 294 | for(i = 0; i < 16; i++) |
| 295 | printf(" %02x", out[i]); |
| 296 | printf("\n"); |
| 297 | } |
| 298 | #endif |
| 299 | return 0; |
| 300 | } |
| 301 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 302 | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 303 | int nb_sectors, int *pnum) |
| 304 | { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 305 | uint64_t cluster_offset; |
| 306 | |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 307 | *pnum = nb_sectors; |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 308 | cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum); |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 309 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 310 | return (cluster_offset != 0); |
| 311 | } |
| 312 | |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 313 | /* handle reading after the end of the backing file */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 314 | int qcow2_backing_read1(BlockDriverState *bs, |
Kevin Wolf | 45aba42 | 2009-05-28 16:07:05 +0200 | [diff] [blame] | 315 | int64_t sector_num, uint8_t *buf, int nb_sectors) |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 316 | { |
| 317 | int n1; |
| 318 | if ((sector_num + nb_sectors) <= bs->total_sectors) |
| 319 | return nb_sectors; |
| 320 | if (sector_num >= bs->total_sectors) |
| 321 | n1 = 0; |
| 322 | else |
| 323 | n1 = bs->total_sectors - sector_num; |
| 324 | memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1)); |
| 325 | return n1; |
| 326 | } |
| 327 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 328 | typedef struct QCowAIOCB { |
| 329 | BlockDriverAIOCB common; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 330 | int64_t sector_num; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 331 | QEMUIOVector *qiov; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 332 | uint8_t *buf; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 333 | void *orig_buf; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 334 | int nb_sectors; |
| 335 | int n; |
| 336 | uint64_t cluster_offset; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 337 | uint8_t *cluster_data; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 338 | BlockDriverAIOCB *hd_aiocb; |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 339 | struct iovec hd_iov; |
| 340 | QEMUIOVector hd_qiov; |
aliguori | 1490791 | 2008-10-31 17:28:00 +0000 | [diff] [blame] | 341 | QEMUBH *bh; |
aliguori | e976c6a | 2008-12-02 20:14:05 +0000 | [diff] [blame] | 342 | QCowL2Meta l2meta; |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 343 | LIST_ENTRY(QCowAIOCB) next_depend; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 344 | } QCowAIOCB; |
| 345 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 346 | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
| 347 | { |
| 348 | QCowAIOCB *acb = (QCowAIOCB *)blockacb; |
| 349 | if (acb->hd_aiocb) |
| 350 | bdrv_aio_cancel(acb->hd_aiocb); |
| 351 | qemu_aio_release(acb); |
| 352 | } |
| 353 | |
| 354 | static AIOPool qcow_aio_pool = { |
| 355 | .aiocb_size = sizeof(QCowAIOCB), |
| 356 | .cancel = qcow_aio_cancel, |
| 357 | }; |
| 358 | |
aliguori | 1490791 | 2008-10-31 17:28:00 +0000 | [diff] [blame] | 359 | static void qcow_aio_read_cb(void *opaque, int ret); |
| 360 | static void qcow_aio_read_bh(void *opaque) |
| 361 | { |
| 362 | QCowAIOCB *acb = opaque; |
| 363 | qemu_bh_delete(acb->bh); |
| 364 | acb->bh = NULL; |
| 365 | qcow_aio_read_cb(opaque, 0); |
| 366 | } |
| 367 | |
aliguori | a32ef78 | 2008-12-02 20:08:04 +0000 | [diff] [blame] | 368 | static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
| 369 | { |
| 370 | if (acb->bh) |
| 371 | return -EIO; |
| 372 | |
| 373 | acb->bh = qemu_bh_new(cb, acb); |
| 374 | if (!acb->bh) |
| 375 | return -EIO; |
| 376 | |
| 377 | qemu_bh_schedule(acb->bh); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 382 | static void qcow_aio_read_cb(void *opaque, int ret) |
| 383 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 384 | QCowAIOCB *acb = opaque; |
| 385 | BlockDriverState *bs = acb->common.bs; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 386 | BDRVQcowState *s = bs->opaque; |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 387 | int index_in_cluster, n1; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 388 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 389 | acb->hd_aiocb = NULL; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 390 | if (ret < 0) |
| 391 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 392 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 393 | /* post process the read buffer */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 394 | if (!acb->cluster_offset) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 395 | /* nothing to do */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 396 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 397 | /* nothing to do */ |
| 398 | } else { |
| 399 | if (s->crypt_method) { |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 400 | qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 401 | acb->n, 0, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 402 | &s->aes_decrypt_key); |
| 403 | } |
| 404 | } |
| 405 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 406 | acb->nb_sectors -= acb->n; |
| 407 | acb->sector_num += acb->n; |
| 408 | acb->buf += acb->n * 512; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 409 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 410 | if (acb->nb_sectors == 0) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 411 | /* request completed */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 412 | ret = 0; |
| 413 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 414 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 415 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 416 | /* prepare next AIO request */ |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 417 | acb->n = acb->nb_sectors; |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 418 | acb->cluster_offset = |
| 419 | qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 420 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 421 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 422 | if (!acb->cluster_offset) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 423 | if (bs->backing_hd) { |
| 424 | /* read from the base image */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 425 | n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 426 | acb->buf, acb->n); |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 427 | if (n1 > 0) { |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 428 | acb->hd_iov.iov_base = (void *)acb->buf; |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 429 | acb->hd_iov.iov_len = acb->n * 512; |
| 430 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
| 431 | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, |
| 432 | &acb->hd_qiov, acb->n, |
| 433 | qcow_aio_read_cb, acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 434 | if (acb->hd_aiocb == NULL) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 435 | goto done; |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 436 | } else { |
aliguori | a32ef78 | 2008-12-02 20:08:04 +0000 | [diff] [blame] | 437 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
| 438 | if (ret < 0) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 439 | goto done; |
bellard | a946592 | 2006-08-06 13:34:04 +0000 | [diff] [blame] | 440 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 441 | } else { |
| 442 | /* Note: in this case, no need to wait */ |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 443 | memset(acb->buf, 0, 512 * acb->n); |
aliguori | a32ef78 | 2008-12-02 20:08:04 +0000 | [diff] [blame] | 444 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
| 445 | if (ret < 0) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 446 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 447 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 448 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 449 | /* add AIO support for compressed blocks ? */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 450 | if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 451 | goto done; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 452 | memcpy(acb->buf, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 453 | s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
aliguori | a32ef78 | 2008-12-02 20:08:04 +0000 | [diff] [blame] | 454 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
| 455 | if (ret < 0) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 456 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 457 | } else { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 458 | if ((acb->cluster_offset & 511) != 0) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 459 | ret = -EIO; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 460 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 461 | } |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 462 | |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 463 | acb->hd_iov.iov_base = (void *)acb->buf; |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 464 | acb->hd_iov.iov_len = acb->n * 512; |
| 465 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
| 466 | acb->hd_aiocb = bdrv_aio_readv(s->hd, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 467 | (acb->cluster_offset >> 9) + index_in_cluster, |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 468 | &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 469 | if (acb->hd_aiocb == NULL) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 470 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 471 | } |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 472 | |
| 473 | return; |
| 474 | done: |
| 475 | if (acb->qiov->niov > 1) { |
| 476 | qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size); |
| 477 | qemu_vfree(acb->orig_buf); |
| 478 | } |
| 479 | acb->common.cb(acb->common.opaque, ret); |
| 480 | qemu_aio_release(acb); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 481 | } |
| 482 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 483 | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 484 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
| 485 | BlockDriverCompletionFunc *cb, void *opaque, int is_write) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 486 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 487 | QCowAIOCB *acb; |
| 488 | |
Christoph Hellwig | c16b5a2 | 2009-05-25 12:37:32 +0200 | [diff] [blame] | 489 | acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 490 | if (!acb) |
| 491 | return NULL; |
| 492 | acb->hd_aiocb = NULL; |
| 493 | acb->sector_num = sector_num; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 494 | acb->qiov = qiov; |
| 495 | if (qiov->niov > 1) { |
aliguori | e268ca5 | 2009-04-22 20:20:00 +0000 | [diff] [blame] | 496 | acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size); |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 497 | if (is_write) |
| 498 | qemu_iovec_to_buffer(qiov, acb->buf); |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 499 | } else { |
| 500 | acb->buf = (uint8_t *)qiov->iov->iov_base; |
| 501 | } |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 502 | acb->nb_sectors = nb_sectors; |
| 503 | acb->n = 0; |
| 504 | acb->cluster_offset = 0; |
aliguori | e976c6a | 2008-12-02 20:14:05 +0000 | [diff] [blame] | 505 | acb->l2meta.nb_clusters = 0; |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 506 | LIST_INIT(&acb->l2meta.dependent_requests); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 507 | return acb; |
| 508 | } |
| 509 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 510 | static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs, |
| 511 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 512 | BlockDriverCompletionFunc *cb, void *opaque) |
| 513 | { |
| 514 | QCowAIOCB *acb; |
| 515 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 516 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 517 | if (!acb) |
| 518 | return NULL; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 519 | |
| 520 | qcow_aio_read_cb(acb, 0); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 521 | return &acb->common; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 524 | static void qcow_aio_write_cb(void *opaque, int ret); |
| 525 | |
| 526 | static void run_dependent_requests(QCowL2Meta *m) |
| 527 | { |
| 528 | QCowAIOCB *req; |
| 529 | QCowAIOCB *next; |
| 530 | |
| 531 | /* Take the request off the list of running requests */ |
| 532 | if (m->nb_clusters != 0) { |
| 533 | LIST_REMOVE(m, next_in_flight); |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Restart all dependent requests. |
| 538 | * Can't use LIST_FOREACH here - the next link might not be the same |
| 539 | * any more after the callback (request could depend on a different |
| 540 | * request now) |
| 541 | */ |
| 542 | for (req = m->dependent_requests.lh_first; req != NULL; req = next) { |
| 543 | next = req->next_depend.le_next; |
| 544 | qcow_aio_write_cb(req, 0); |
| 545 | } |
| 546 | |
| 547 | /* Empty the list for the next part of the request */ |
| 548 | LIST_INIT(&m->dependent_requests); |
| 549 | } |
| 550 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 551 | static void qcow_aio_write_cb(void *opaque, int ret) |
| 552 | { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 553 | QCowAIOCB *acb = opaque; |
| 554 | BlockDriverState *bs = acb->common.bs; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 555 | BDRVQcowState *s = bs->opaque; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 556 | int index_in_cluster; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 557 | const uint8_t *src_buf; |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 558 | int n_end; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 559 | |
| 560 | acb->hd_aiocb = NULL; |
| 561 | |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 562 | if (ret >= 0) { |
| 563 | ret = qcow2_alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta); |
| 564 | } |
| 565 | |
| 566 | run_dependent_requests(&acb->l2meta); |
| 567 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 568 | if (ret < 0) |
| 569 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 570 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 571 | acb->nb_sectors -= acb->n; |
| 572 | acb->sector_num += acb->n; |
| 573 | acb->buf += acb->n * 512; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 574 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 575 | if (acb->nb_sectors == 0) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 576 | /* request completed */ |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 577 | ret = 0; |
| 578 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 579 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 580 | |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 581 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 582 | n_end = index_in_cluster + acb->nb_sectors; |
| 583 | if (s->crypt_method && |
| 584 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) |
| 585 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; |
| 586 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 587 | acb->cluster_offset = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9, |
aliguori | 0520352 | 2008-08-14 18:08:21 +0000 | [diff] [blame] | 588 | index_in_cluster, |
aliguori | e976c6a | 2008-12-02 20:14:05 +0000 | [diff] [blame] | 589 | n_end, &acb->n, &acb->l2meta); |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 590 | |
| 591 | /* Need to wait for another request? If so, we are done for now. */ |
| 592 | if (!acb->cluster_offset && acb->l2meta.depends_on != NULL) { |
| 593 | LIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests, |
| 594 | acb, next_depend); |
| 595 | return; |
| 596 | } |
| 597 | |
aliguori | e976c6a | 2008-12-02 20:14:05 +0000 | [diff] [blame] | 598 | if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 599 | ret = -EIO; |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 600 | goto done; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 601 | } |
| 602 | if (s->crypt_method) { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 603 | if (!acb->cluster_data) { |
aliguori | 095a9c5 | 2008-08-14 18:10:28 +0000 | [diff] [blame] | 604 | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
| 605 | s->cluster_size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 606 | } |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 607 | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 608 | acb->n, 1, &s->aes_encrypt_key); |
| 609 | src_buf = acb->cluster_data; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 610 | } else { |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 611 | src_buf = acb->buf; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 612 | } |
aliguori | c87c067 | 2009-04-07 18:43:20 +0000 | [diff] [blame] | 613 | acb->hd_iov.iov_base = (void *)src_buf; |
| 614 | acb->hd_iov.iov_len = acb->n * 512; |
| 615 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
| 616 | acb->hd_aiocb = bdrv_aio_writev(s->hd, |
| 617 | (acb->cluster_offset >> 9) + index_in_cluster, |
| 618 | &acb->hd_qiov, acb->n, |
| 619 | qcow_aio_write_cb, acb); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 620 | if (acb->hd_aiocb == NULL) |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 621 | goto done; |
| 622 | |
| 623 | return; |
| 624 | |
| 625 | done: |
| 626 | if (acb->qiov->niov > 1) |
| 627 | qemu_vfree(acb->orig_buf); |
| 628 | acb->common.cb(acb->common.opaque, ret); |
| 629 | qemu_aio_release(acb); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 630 | } |
| 631 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 632 | static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs, |
| 633 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 634 | BlockDriverCompletionFunc *cb, void *opaque) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 635 | { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 636 | BDRVQcowState *s = bs->opaque; |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 637 | QCowAIOCB *acb; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 638 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 639 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
| 640 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 641 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 642 | if (!acb) |
| 643 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 644 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 645 | qcow_aio_write_cb(acb, 0); |
pbrook | ce1a14d | 2006-08-07 02:38:06 +0000 | [diff] [blame] | 646 | return &acb->common; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 647 | } |
| 648 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 649 | static void qcow_close(BlockDriverState *bs) |
| 650 | { |
| 651 | BDRVQcowState *s = bs->opaque; |
| 652 | qemu_free(s->l1_table); |
| 653 | qemu_free(s->l2_cache); |
| 654 | qemu_free(s->cluster_cache); |
| 655 | qemu_free(s->cluster_data); |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 656 | qcow2_refcount_close(bs); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 657 | bdrv_delete(s->hd); |
| 658 | } |
| 659 | |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 660 | static int get_bits_from_size(size_t size) |
| 661 | { |
| 662 | int res = 0; |
| 663 | |
| 664 | if (size == 0) { |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | while (size != 1) { |
| 669 | /* Not a power of two */ |
| 670 | if (size & 1) { |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | size >>= 1; |
| 675 | res++; |
| 676 | } |
| 677 | |
| 678 | return res; |
| 679 | } |
| 680 | |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 681 | |
| 682 | static int preallocate(BlockDriverState *bs) |
| 683 | { |
| 684 | BDRVQcowState *s = bs->opaque; |
Blue Swirl | 2000cbc | 2009-08-29 16:37:26 +0300 | [diff] [blame] | 685 | uint64_t cluster_offset = 0; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 686 | uint64_t nb_sectors; |
| 687 | uint64_t offset; |
| 688 | int num; |
| 689 | QCowL2Meta meta; |
| 690 | |
| 691 | nb_sectors = bdrv_getlength(bs) >> 9; |
| 692 | offset = 0; |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 693 | LIST_INIT(&meta.dependent_requests); |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 694 | |
| 695 | while (nb_sectors) { |
| 696 | num = MIN(nb_sectors, INT_MAX >> 9); |
| 697 | cluster_offset = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, |
| 698 | &meta); |
| 699 | |
| 700 | if (cluster_offset == 0) { |
| 701 | return -1; |
| 702 | } |
| 703 | |
| 704 | if (qcow2_alloc_cluster_link_l2(bs, cluster_offset, &meta) < 0) { |
| 705 | qcow2_free_any_clusters(bs, cluster_offset, meta.nb_clusters); |
| 706 | return -1; |
| 707 | } |
| 708 | |
Kevin Wolf | f214978 | 2009-08-31 16:48:49 +0200 | [diff] [blame] | 709 | /* There are no dependent requests, but we need to remove our request |
| 710 | * from the list of in-flight requests */ |
| 711 | run_dependent_requests(&meta); |
| 712 | |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 713 | /* TODO Preallocate data if requested */ |
| 714 | |
| 715 | nb_sectors -= num; |
| 716 | offset += num << 9; |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * It is expected that the image file is large enough to actually contain |
| 721 | * all of the allocated clusters (otherwise we get failing reads after |
| 722 | * EOF). Extend the image to the last allocated sector. |
| 723 | */ |
| 724 | if (cluster_offset != 0) { |
Kevin Wolf | ea80b90 | 2009-08-31 12:26:57 +0200 | [diff] [blame] | 725 | uint8_t buf[512]; |
| 726 | memset(buf, 0, 512); |
| 727 | bdrv_write(s->hd, (cluster_offset >> 9) + num - 1, buf, 1); |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 733 | static int qcow_create2(const char *filename, int64_t total_size, |
| 734 | const char *backing_file, const char *backing_format, |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 735 | int flags, size_t cluster_size, int prealloc) |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 736 | { |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 737 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 738 | int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits; |
aliguori | 2d2431f | 2009-04-05 17:40:58 +0000 | [diff] [blame] | 739 | int ref_clusters, backing_format_len = 0; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 740 | QCowHeader header; |
| 741 | uint64_t tmp, offset; |
| 742 | QCowCreateState s1, *s = &s1; |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 743 | QCowExtension ext_bf = {0, 0}; |
| 744 | |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 745 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 746 | memset(s, 0, sizeof(*s)); |
| 747 | |
| 748 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
| 749 | if (fd < 0) |
| 750 | return -1; |
| 751 | memset(&header, 0, sizeof(header)); |
| 752 | header.magic = cpu_to_be32(QCOW_MAGIC); |
| 753 | header.version = cpu_to_be32(QCOW_VERSION); |
| 754 | header.size = cpu_to_be64(total_size * 512); |
| 755 | header_size = sizeof(header); |
| 756 | backing_filename_len = 0; |
| 757 | if (backing_file) { |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 758 | if (backing_format) { |
| 759 | ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT; |
| 760 | backing_format_len = strlen(backing_format); |
| 761 | ext_bf.len = (backing_format_len + 7) & ~7; |
| 762 | header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7); |
| 763 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 764 | header.backing_file_offset = cpu_to_be64(header_size); |
| 765 | backing_filename_len = strlen(backing_file); |
| 766 | header.backing_file_size = cpu_to_be32(backing_filename_len); |
| 767 | header_size += backing_filename_len; |
| 768 | } |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 769 | |
| 770 | /* Cluster size */ |
| 771 | s->cluster_bits = get_bits_from_size(cluster_size); |
| 772 | if (s->cluster_bits < MIN_CLUSTER_BITS || |
| 773 | s->cluster_bits > MAX_CLUSTER_BITS) |
| 774 | { |
| 775 | fprintf(stderr, "Cluster size must be a power of two between " |
| 776 | "%d and %dk\n", |
| 777 | 1 << MIN_CLUSTER_BITS, |
| 778 | 1 << (MAX_CLUSTER_BITS - 10)); |
| 779 | return -EINVAL; |
| 780 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 781 | s->cluster_size = 1 << s->cluster_bits; |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 782 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 783 | header.cluster_bits = cpu_to_be32(s->cluster_bits); |
| 784 | header_size = (header_size + 7) & ~7; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 785 | if (flags & BLOCK_FLAG_ENCRYPT) { |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 786 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
| 787 | } else { |
| 788 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
| 789 | } |
| 790 | l2_bits = s->cluster_bits - 3; |
| 791 | shift = s->cluster_bits + l2_bits; |
| 792 | l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); |
| 793 | offset = align_offset(header_size, s->cluster_size); |
| 794 | s->l1_table_offset = offset; |
| 795 | header.l1_table_offset = cpu_to_be64(s->l1_table_offset); |
| 796 | header.l1_size = cpu_to_be32(l1_size); |
bellard | 15e6690 | 2006-08-05 22:24:28 +0000 | [diff] [blame] | 797 | offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 798 | |
| 799 | s->refcount_table = qemu_mallocz(s->cluster_size); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 800 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 801 | s->refcount_table_offset = offset; |
| 802 | header.refcount_table_offset = cpu_to_be64(offset); |
| 803 | header.refcount_table_clusters = cpu_to_be32(1); |
| 804 | offset += s->cluster_size; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 805 | s->refcount_block_offset = offset; |
aliguori | 2d2431f | 2009-04-05 17:40:58 +0000 | [diff] [blame] | 806 | |
| 807 | /* count how many refcount blocks needed */ |
| 808 | tmp = offset >> s->cluster_bits; |
| 809 | ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1; |
| 810 | for (i=0; i < ref_clusters; i++) { |
| 811 | s->refcount_table[i] = cpu_to_be64(offset); |
| 812 | offset += s->cluster_size; |
| 813 | } |
| 814 | |
| 815 | s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 816 | |
| 817 | /* update refcounts */ |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 818 | qcow2_create_refcount_update(s, 0, header_size); |
| 819 | qcow2_create_refcount_update(s, s->l1_table_offset, |
| 820 | l1_size * sizeof(uint64_t)); |
| 821 | qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size); |
| 822 | qcow2_create_refcount_update(s, s->refcount_block_offset, |
| 823 | ref_clusters * s->cluster_size); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 824 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 825 | /* write all the data */ |
| 826 | write(fd, &header, sizeof(header)); |
| 827 | if (backing_file) { |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 828 | if (backing_format_len) { |
| 829 | char zero[16]; |
| 830 | int d = ext_bf.len - backing_format_len; |
| 831 | |
| 832 | memset(zero, 0, sizeof(zero)); |
| 833 | cpu_to_be32s(&ext_bf.magic); |
| 834 | cpu_to_be32s(&ext_bf.len); |
| 835 | write(fd, &ext_bf, sizeof(ext_bf)); |
| 836 | write(fd, backing_format, backing_format_len); |
| 837 | if (d>0) { |
| 838 | write(fd, zero, d); |
| 839 | } |
| 840 | } |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 841 | write(fd, backing_file, backing_filename_len); |
| 842 | } |
| 843 | lseek(fd, s->l1_table_offset, SEEK_SET); |
| 844 | tmp = 0; |
| 845 | for(i = 0;i < l1_size; i++) { |
| 846 | write(fd, &tmp, sizeof(tmp)); |
| 847 | } |
| 848 | lseek(fd, s->refcount_table_offset, SEEK_SET); |
| 849 | write(fd, s->refcount_table, s->cluster_size); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 850 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 851 | lseek(fd, s->refcount_block_offset, SEEK_SET); |
aliguori | 2d2431f | 2009-04-05 17:40:58 +0000 | [diff] [blame] | 852 | write(fd, s->refcount_block, ref_clusters * s->cluster_size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 853 | |
| 854 | qemu_free(s->refcount_table); |
| 855 | qemu_free(s->refcount_block); |
| 856 | close(fd); |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 857 | |
| 858 | /* Preallocate metadata */ |
| 859 | if (prealloc) { |
| 860 | BlockDriverState *bs; |
| 861 | bs = bdrv_new(""); |
| 862 | bdrv_open(bs, filename, BDRV_O_CACHE_WB); |
| 863 | preallocate(bs); |
| 864 | bdrv_close(bs); |
| 865 | } |
| 866 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 867 | return 0; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 870 | static int qcow_create(const char *filename, QEMUOptionParameter *options) |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 871 | { |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 872 | const char *backing_file = NULL; |
| 873 | const char *backing_fmt = NULL; |
| 874 | uint64_t sectors = 0; |
| 875 | int flags = 0; |
Kevin Wolf | 9ccb258 | 2009-06-16 12:53:25 +0200 | [diff] [blame] | 876 | size_t cluster_size = 65536; |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 877 | int prealloc = 0; |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 878 | |
| 879 | /* Read out options */ |
| 880 | while (options && options->name) { |
| 881 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { |
| 882 | sectors = options->value.n / 512; |
| 883 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
| 884 | backing_file = options->value.s; |
| 885 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { |
| 886 | backing_fmt = options->value.s; |
| 887 | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { |
| 888 | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 889 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
| 890 | if (options->value.n) { |
| 891 | cluster_size = options->value.n; |
| 892 | } |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 893 | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { |
| 894 | if (!options->value.s || !strcmp(options->value.s, "off")) { |
| 895 | prealloc = 0; |
| 896 | } else if (!strcmp(options->value.s, "metadata")) { |
| 897 | prealloc = 1; |
| 898 | } else { |
| 899 | fprintf(stderr, "Invalid preallocation mode: '%s'\n", |
| 900 | options->value.s); |
| 901 | return -EINVAL; |
| 902 | } |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 903 | } |
| 904 | options++; |
| 905 | } |
| 906 | |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 907 | if (backing_file && prealloc) { |
| 908 | fprintf(stderr, "Backing file and preallocation cannot be used at " |
| 909 | "the same time\n"); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
Kevin Wolf | 73c632e | 2009-05-19 17:57:21 +0200 | [diff] [blame] | 913 | return qcow_create2(filename, sectors, backing_file, backing_fmt, flags, |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 914 | cluster_size, prealloc); |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 915 | } |
| 916 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 917 | static int qcow_make_empty(BlockDriverState *bs) |
| 918 | { |
| 919 | #if 0 |
| 920 | /* XXX: not correct */ |
| 921 | BDRVQcowState *s = bs->opaque; |
| 922 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); |
| 923 | int ret; |
| 924 | |
| 925 | memset(s->l1_table, 0, l1_length); |
| 926 | if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) |
aliguori | ac67488 | 2008-12-02 20:06:51 +0000 | [diff] [blame] | 927 | return -1; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 928 | ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); |
| 929 | if (ret < 0) |
| 930 | return ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 931 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 932 | l2_cache_reset(bs); |
| 933 | #endif |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | /* XXX: put compressed sectors first, then all the cluster aligned |
| 938 | tables to avoid losing bytes in alignment */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 939 | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 940 | const uint8_t *buf, int nb_sectors) |
| 941 | { |
| 942 | BDRVQcowState *s = bs->opaque; |
| 943 | z_stream strm; |
| 944 | int ret, out_len; |
| 945 | uint8_t *out_buf; |
| 946 | uint64_t cluster_offset; |
| 947 | |
| 948 | if (nb_sectors == 0) { |
| 949 | /* align end of file to a sector boundary to ease reading with |
| 950 | sector based I/Os */ |
| 951 | cluster_offset = bdrv_getlength(s->hd); |
| 952 | cluster_offset = (cluster_offset + 511) & ~511; |
| 953 | bdrv_truncate(s->hd, cluster_offset); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | if (nb_sectors != s->cluster_sectors) |
| 958 | return -EINVAL; |
| 959 | |
| 960 | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 961 | |
| 962 | /* best compression, small window, no zlib header */ |
| 963 | memset(&strm, 0, sizeof(strm)); |
| 964 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 965 | Z_DEFLATED, -12, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 966 | 9, Z_DEFAULT_STRATEGY); |
| 967 | if (ret != 0) { |
| 968 | qemu_free(out_buf); |
| 969 | return -1; |
| 970 | } |
| 971 | |
| 972 | strm.avail_in = s->cluster_size; |
| 973 | strm.next_in = (uint8_t *)buf; |
| 974 | strm.avail_out = s->cluster_size; |
| 975 | strm.next_out = out_buf; |
| 976 | |
| 977 | ret = deflate(&strm, Z_FINISH); |
| 978 | if (ret != Z_STREAM_END && ret != Z_OK) { |
| 979 | qemu_free(out_buf); |
| 980 | deflateEnd(&strm); |
| 981 | return -1; |
| 982 | } |
| 983 | out_len = strm.next_out - out_buf; |
| 984 | |
| 985 | deflateEnd(&strm); |
| 986 | |
| 987 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { |
| 988 | /* could not compress: write normal cluster */ |
Kevin Wolf | ade4067 | 2009-05-25 11:38:59 +0200 | [diff] [blame] | 989 | bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 990 | } else { |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 991 | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, |
| 992 | sector_num << 9, out_len); |
aliguori | 52d893e | 2008-08-14 18:09:32 +0000 | [diff] [blame] | 993 | if (!cluster_offset) |
| 994 | return -1; |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 995 | cluster_offset &= s->cluster_offset_mask; |
| 996 | if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { |
| 997 | qemu_free(out_buf); |
| 998 | return -1; |
| 999 | } |
| 1000 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1001 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1002 | qemu_free(out_buf); |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | static void qcow_flush(BlockDriverState *bs) |
| 1007 | { |
| 1008 | BDRVQcowState *s = bs->opaque; |
| 1009 | bdrv_flush(s->hd); |
| 1010 | } |
| 1011 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1012 | static int64_t qcow_vm_state_offset(BDRVQcowState *s) |
| 1013 | { |
| 1014 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); |
| 1015 | } |
| 1016 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1017 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
| 1018 | { |
| 1019 | BDRVQcowState *s = bs->opaque; |
| 1020 | bdi->cluster_size = s->cluster_size; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1021 | bdi->vm_state_offset = qcow_vm_state_offset(s); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1025 | |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1026 | static int qcow_check(BlockDriverState *bs) |
| 1027 | { |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1028 | return qcow2_check_refcounts(bs); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | #if 0 |
| 1032 | static void dump_refcounts(BlockDriverState *bs) |
| 1033 | { |
| 1034 | BDRVQcowState *s = bs->opaque; |
| 1035 | int64_t nb_clusters, k, k1, size; |
| 1036 | int refcount; |
| 1037 | |
| 1038 | size = bdrv_getlength(s->hd); |
aliguori | 6db6c63 | 2008-12-02 20:11:27 +0000 | [diff] [blame] | 1039 | nb_clusters = size_to_clusters(s, size); |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1040 | for(k = 0; k < nb_clusters;) { |
| 1041 | k1 = k; |
| 1042 | refcount = get_refcount(bs, k); |
| 1043 | k++; |
| 1044 | while (k < nb_clusters && get_refcount(bs, k) == refcount) |
| 1045 | k++; |
| 1046 | printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1); |
| 1047 | } |
| 1048 | } |
| 1049 | #endif |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1050 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1051 | static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1052 | int64_t pos, int size) |
| 1053 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1054 | BDRVQcowState *s = bs->opaque; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1055 | int growable = bs->growable; |
| 1056 | |
| 1057 | bs->growable = 1; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1058 | bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1059 | bs->growable = growable; |
| 1060 | |
| 1061 | return size; |
| 1062 | } |
| 1063 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1064 | static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1065 | int64_t pos, int size) |
| 1066 | { |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1067 | BDRVQcowState *s = bs->opaque; |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1068 | int growable = bs->growable; |
| 1069 | int ret; |
| 1070 | |
| 1071 | bs->growable = 1; |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1072 | ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1073 | bs->growable = growable; |
| 1074 | |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1078 | static QEMUOptionParameter qcow_create_options[] = { |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 1079 | { |
| 1080 | .name = BLOCK_OPT_SIZE, |
| 1081 | .type = OPT_SIZE, |
| 1082 | .help = "Virtual disk size" |
| 1083 | }, |
| 1084 | { |
| 1085 | .name = BLOCK_OPT_BACKING_FILE, |
| 1086 | .type = OPT_STRING, |
| 1087 | .help = "File name of a base image" |
| 1088 | }, |
| 1089 | { |
| 1090 | .name = BLOCK_OPT_BACKING_FMT, |
| 1091 | .type = OPT_STRING, |
| 1092 | .help = "Image format of the base image" |
| 1093 | }, |
| 1094 | { |
| 1095 | .name = BLOCK_OPT_ENCRYPT, |
| 1096 | .type = OPT_FLAG, |
| 1097 | .help = "Encrypt the image" |
| 1098 | }, |
| 1099 | { |
| 1100 | .name = BLOCK_OPT_CLUSTER_SIZE, |
| 1101 | .type = OPT_SIZE, |
| 1102 | .help = "qcow2 cluster size" |
| 1103 | }, |
Kevin Wolf | a35e1c1 | 2009-08-17 15:50:10 +0200 | [diff] [blame] | 1104 | { |
| 1105 | .name = BLOCK_OPT_PREALLOC, |
| 1106 | .type = OPT_STRING, |
| 1107 | .help = "Preallocation mode (allowed values: off, metadata)" |
| 1108 | }, |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1109 | { NULL } |
| 1110 | }; |
| 1111 | |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1112 | static BlockDriver bdrv_qcow2 = { |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1113 | .format_name = "qcow2", |
| 1114 | .instance_size = sizeof(BDRVQcowState), |
| 1115 | .bdrv_probe = qcow_probe, |
| 1116 | .bdrv_open = qcow_open, |
| 1117 | .bdrv_close = qcow_close, |
| 1118 | .bdrv_create = qcow_create, |
| 1119 | .bdrv_flush = qcow_flush, |
| 1120 | .bdrv_is_allocated = qcow_is_allocated, |
| 1121 | .bdrv_set_key = qcow_set_key, |
| 1122 | .bdrv_make_empty = qcow_make_empty, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1123 | |
aliguori | f141eaf | 2009-04-07 18:43:24 +0000 | [diff] [blame] | 1124 | .bdrv_aio_readv = qcow_aio_readv, |
| 1125 | .bdrv_aio_writev = qcow_aio_writev, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1126 | .bdrv_write_compressed = qcow_write_compressed, |
| 1127 | |
Kevin Wolf | ed6ccf0 | 2009-05-28 16:07:07 +0200 | [diff] [blame] | 1128 | .bdrv_snapshot_create = qcow2_snapshot_create, |
| 1129 | .bdrv_snapshot_goto = qcow2_snapshot_goto, |
| 1130 | .bdrv_snapshot_delete = qcow2_snapshot_delete, |
| 1131 | .bdrv_snapshot_list = qcow2_snapshot_list, |
aurel32 | e60f469 | 2009-03-07 22:00:29 +0000 | [diff] [blame] | 1132 | .bdrv_get_info = qcow_get_info, |
aliguori | f965509 | 2009-03-28 17:55:14 +0000 | [diff] [blame] | 1133 | |
Christoph Hellwig | 45566e9 | 2009-07-10 23:11:57 +0200 | [diff] [blame] | 1134 | .bdrv_save_vmstate = qcow_save_vmstate, |
| 1135 | .bdrv_load_vmstate = qcow_load_vmstate, |
aliguori | 178e08a | 2009-04-05 19:10:55 +0000 | [diff] [blame] | 1136 | |
Kevin Wolf | 0e7e198 | 2009-05-18 16:42:10 +0200 | [diff] [blame] | 1137 | .create_options = qcow_create_options, |
aliguori | e97fc19 | 2009-04-21 23:11:50 +0000 | [diff] [blame] | 1138 | .bdrv_check = qcow_check, |
bellard | 585f858 | 2006-08-05 21:14:20 +0000 | [diff] [blame] | 1139 | }; |
Anthony Liguori | 5efa9d5 | 2009-05-09 17:03:42 -0500 | [diff] [blame] | 1140 | |
| 1141 | static void bdrv_qcow2_init(void) |
| 1142 | { |
| 1143 | bdrv_register(&bdrv_qcow2); |
| 1144 | } |
| 1145 | |
| 1146 | block_init(bdrv_qcow2_init); |