]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow.c
block: Use g_new() & friends where that makes obvious sense
[mirror_qemu.git] / block / qcow.c
CommitLineData
ea2384d3
FB
1/*
2 * Block driver for the QCOW format
5fafdf24 3 *
83f64091 4 * Copyright (c) 2004-2006 Fabrice Bellard
5fafdf24 5 *
ea2384d3
FB
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 */
faf07963 24#include "qemu-common.h"
737e150e 25#include "block/block_int.h"
1de7afc9 26#include "qemu/module.h"
28d34b82 27#include <zlib.h>
753d9b82 28#include "qemu/aes.h"
caf71f86 29#include "migration/migration.h"
ea2384d3
FB
30
31/**************************************************************/
32/* QEMU COW block driver with compression and encryption support */
33
34#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35#define QCOW_VERSION 1
36
37#define QCOW_CRYPT_NONE 0
38#define QCOW_CRYPT_AES 1
39
40#define QCOW_OFLAG_COMPRESSED (1LL << 63)
41
42typedef struct QCowHeader {
43 uint32_t magic;
44 uint32_t version;
45 uint64_t backing_file_offset;
46 uint32_t backing_file_size;
47 uint32_t mtime;
48 uint64_t size; /* in bytes */
49 uint8_t cluster_bits;
50 uint8_t l2_bits;
ea54feff 51 uint16_t padding;
ea2384d3
FB
52 uint32_t crypt_method;
53 uint64_t l1_table_offset;
ea54feff 54} QEMU_PACKED QCowHeader;
ea2384d3
FB
55
56#define L2_CACHE_SIZE 16
57
58typedef struct BDRVQcowState {
ea2384d3
FB
59 int cluster_bits;
60 int cluster_size;
61 int cluster_sectors;
62 int l2_bits;
63 int l2_size;
46485de0 64 unsigned int l1_size;
ea2384d3
FB
65 uint64_t cluster_offset_mask;
66 uint64_t l1_table_offset;
67 uint64_t *l1_table;
68 uint64_t *l2_cache;
69 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
70 uint32_t l2_cache_counts[L2_CACHE_SIZE];
71 uint8_t *cluster_cache;
72 uint8_t *cluster_data;
73 uint64_t cluster_cache_offset;
74 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
75 uint32_t crypt_method_header;
76 AES_KEY aes_encrypt_key;
77 AES_KEY aes_decrypt_key;
52b8eb60 78 CoMutex lock;
fd9f102c 79 Error *migration_blocker;
ea2384d3
FB
80} BDRVQcowState;
81
66f82cee 82static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
ea2384d3
FB
83
84static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
85{
86 const QCowHeader *cow_header = (const void *)buf;
3b46e624 87
712e7874
FB
88 if (buf_size >= sizeof(QCowHeader) &&
89 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
5fafdf24 90 be32_to_cpu(cow_header->version) == QCOW_VERSION)
ea2384d3
FB
91 return 100;
92 else
93 return 0;
94}
95
015a1036
HR
96static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
97 Error **errp)
ea2384d3
FB
98{
99 BDRVQcowState *s = bs->opaque;
d66e5cee
KW
100 unsigned int len, i, shift;
101 int ret;
ea2384d3 102 QCowHeader header;
83f64091 103
84b0ec02
LZH
104 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
105 if (ret < 0) {
ea2384d3 106 goto fail;
84b0ec02 107 }
ea2384d3
FB
108 be32_to_cpus(&header.magic);
109 be32_to_cpus(&header.version);
110 be64_to_cpus(&header.backing_file_offset);
111 be32_to_cpus(&header.backing_file_size);
112 be32_to_cpus(&header.mtime);
113 be64_to_cpus(&header.size);
114 be32_to_cpus(&header.crypt_method);
115 be64_to_cpus(&header.l1_table_offset);
3b46e624 116
84b0ec02 117 if (header.magic != QCOW_MAGIC) {
76abe407
PB
118 error_setg(errp, "Image not in qcow format");
119 ret = -EINVAL;
84b0ec02
LZH
120 goto fail;
121 }
122 if (header.version != QCOW_VERSION) {
123 char version[64];
521b2b5d
HR
124 snprintf(version, sizeof(version), "QCOW version %" PRIu32,
125 header.version);
b6d5066d
PB
126 error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
127 bs->device_name, "qcow", version);
84b0ec02 128 ret = -ENOTSUP;
ea2384d3 129 goto fail;
84b0ec02
LZH
130 }
131
7159a45b
KW
132 if (header.size <= 1) {
133 error_setg(errp, "Image size is too small (must be at least 2 bytes)");
84b0ec02 134 ret = -EINVAL;
ea2384d3 135 goto fail;
84b0ec02 136 }
7159a45b
KW
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
42eb5817
KW
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
84b0ec02 151 if (header.crypt_method > QCOW_CRYPT_AES) {
b6d5066d 152 error_setg(errp, "invalid encryption method in qcow header");
84b0ec02 153 ret = -EINVAL;
ea2384d3 154 goto fail;
84b0ec02 155 }
ea2384d3 156 s->crypt_method_header = header.crypt_method;
84b0ec02 157 if (s->crypt_method_header) {
ea2384d3 158 bs->encrypted = 1;
84b0ec02 159 }
ea2384d3
FB
160 s->cluster_bits = header.cluster_bits;
161 s->cluster_size = 1 << s->cluster_bits;
162 s->cluster_sectors = 1 << (s->cluster_bits - 9);
163 s->l2_bits = header.l2_bits;
164 s->l2_size = 1 << s->l2_bits;
165 bs->total_sectors = header.size / 512;
166 s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
167
168 /* read the level 1 table */
169 shift = s->cluster_bits + s->l2_bits;
46485de0
KW
170 if (header.size > UINT64_MAX - (1LL << shift)) {
171 error_setg(errp, "Image too large");
172 ret = -EINVAL;
173 goto fail;
174 } else {
175 uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
176 if (l1_size > INT_MAX / sizeof(uint64_t)) {
177 error_setg(errp, "Image too large");
178 ret = -EINVAL;
179 goto fail;
180 }
181 s->l1_size = l1_size;
182 }
ea2384d3
FB
183
184 s->l1_table_offset = header.l1_table_offset;
5839e53b 185 s->l1_table = g_try_new(uint64_t, s->l1_size);
0df93305
KW
186 if (s->l1_table == NULL) {
187 error_setg(errp, "Could not allocate memory for L1 table");
188 ret = -ENOMEM;
189 goto fail;
190 }
84b0ec02
LZH
191
192 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
193 s->l1_size * sizeof(uint64_t));
194 if (ret < 0) {
ea2384d3 195 goto fail;
84b0ec02
LZH
196 }
197
ea2384d3
FB
198 for(i = 0;i < s->l1_size; i++) {
199 be64_to_cpus(&s->l1_table[i]);
200 }
0df93305
KW
201
202 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
203 s->l2_cache =
204 qemu_try_blockalign(bs->file,
205 s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
206 if (s->l2_cache == NULL) {
207 error_setg(errp, "Could not allocate L2 table cache");
208 ret = -ENOMEM;
209 goto fail;
210 }
7267c094 211 s->cluster_cache = g_malloc(s->cluster_size);
7267c094 212 s->cluster_data = g_malloc(s->cluster_size);
ea2384d3 213 s->cluster_cache_offset = -1;
3b46e624 214
ea2384d3
FB
215 /* read the backing file name */
216 if (header.backing_file_offset != 0) {
217 len = header.backing_file_size;
84b0ec02 218 if (len > 1023) {
d66e5cee
KW
219 error_setg(errp, "Backing file name too long");
220 ret = -EINVAL;
221 goto fail;
84b0ec02
LZH
222 }
223 ret = bdrv_pread(bs->file, header.backing_file_offset,
224 bs->backing_file, len);
225 if (ret < 0) {
ea2384d3 226 goto fail;
84b0ec02 227 }
ea2384d3
FB
228 bs->backing_file[len] = '\0';
229 }
de33b1f3 230
fd9f102c
KW
231 /* Disable migration when qcow images are used */
232 error_set(&s->migration_blocker,
233 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
234 "qcow", bs->device_name, "live migration");
235 migrate_add_blocker(s->migration_blocker);
236
de33b1f3 237 qemu_co_mutex_init(&s->lock);
ea2384d3
FB
238 return 0;
239
240 fail:
7267c094 241 g_free(s->l1_table);
0df93305 242 qemu_vfree(s->l2_cache);
7267c094
AL
243 g_free(s->cluster_cache);
244 g_free(s->cluster_data);
84b0ec02 245 return ret;
ea2384d3
FB
246}
247
d177692e
JC
248
249/* We have nothing to do for QCOW reopen, stubs just return
250 * success */
251static int qcow_reopen_prepare(BDRVReopenState *state,
252 BlockReopenQueue *queue, Error **errp)
253{
254 return 0;
255}
256
ea2384d3
FB
257static int qcow_set_key(BlockDriverState *bs, const char *key)
258{
259 BDRVQcowState *s = bs->opaque;
260 uint8_t keybuf[16];
261 int len, i;
3b46e624 262
ea2384d3
FB
263 memset(keybuf, 0, 16);
264 len = strlen(key);
265 if (len > 16)
266 len = 16;
267 /* XXX: we could compress the chars to 7 bits to increase
268 entropy */
269 for(i = 0;i < len;i++) {
270 keybuf[i] = key[i];
271 }
272 s->crypt_method = s->crypt_method_header;
273
274 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
275 return -1;
276 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
277 return -1;
ea2384d3
FB
278 return 0;
279}
280
281/* The crypt function is compatible with the linux cryptoloop
282 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
283 supported */
284static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
285 uint8_t *out_buf, const uint8_t *in_buf,
286 int nb_sectors, int enc,
287 const AES_KEY *key)
288{
289 union {
290 uint64_t ll[2];
291 uint8_t b[16];
292 } ivec;
293 int i;
294
295 for(i = 0; i < nb_sectors; i++) {
296 ivec.ll[0] = cpu_to_le64(sector_num);
297 ivec.ll[1] = 0;
5fafdf24 298 AES_cbc_encrypt(in_buf, out_buf, 512, key,
ea2384d3
FB
299 ivec.b, enc);
300 sector_num++;
301 in_buf += 512;
302 out_buf += 512;
303 }
304}
305
306/* 'allocate' is:
307 *
308 * 0 to not allocate.
309 *
310 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
311 * 'n_end')
312 *
313 * 2 to allocate a compressed cluster of size
314 * 'compressed_size'. 'compressed_size' must be > 0 and <
5fafdf24 315 * cluster_size
ea2384d3
FB
316 *
317 * return 0 if not allocated.
318 */
319static uint64_t get_cluster_offset(BlockDriverState *bs,
320 uint64_t offset, int allocate,
321 int compressed_size,
322 int n_start, int n_end)
323{
324 BDRVQcowState *s = bs->opaque;
325 int min_index, i, j, l1_index, l2_index;
326 uint64_t l2_offset, *l2_table, cluster_offset, tmp;
327 uint32_t min_count;
328 int new_l2_table;
3b46e624 329
ea2384d3
FB
330 l1_index = offset >> (s->l2_bits + s->cluster_bits);
331 l2_offset = s->l1_table[l1_index];
332 new_l2_table = 0;
333 if (!l2_offset) {
334 if (!allocate)
335 return 0;
336 /* allocate a new l2 entry */
66f82cee 337 l2_offset = bdrv_getlength(bs->file);
ea2384d3
FB
338 /* round to cluster size */
339 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
340 /* update the L1 entry */
341 s->l1_table[l1_index] = l2_offset;
342 tmp = cpu_to_be64(l2_offset);
5e5557d9
KW
343 if (bdrv_pwrite_sync(bs->file,
344 s->l1_table_offset + l1_index * sizeof(tmp),
345 &tmp, sizeof(tmp)) < 0)
ea2384d3
FB
346 return 0;
347 new_l2_table = 1;
348 }
349 for(i = 0; i < L2_CACHE_SIZE; i++) {
350 if (l2_offset == s->l2_cache_offsets[i]) {
351 /* increment the hit count */
352 if (++s->l2_cache_counts[i] == 0xffffffff) {
353 for(j = 0; j < L2_CACHE_SIZE; j++) {
354 s->l2_cache_counts[j] >>= 1;
355 }
356 }
357 l2_table = s->l2_cache + (i << s->l2_bits);
358 goto found;
359 }
360 }
361 /* not found: load a new entry in the least used one */
362 min_index = 0;
363 min_count = 0xffffffff;
364 for(i = 0; i < L2_CACHE_SIZE; i++) {
365 if (s->l2_cache_counts[i] < min_count) {
366 min_count = s->l2_cache_counts[i];
367 min_index = i;
368 }
369 }
370 l2_table = s->l2_cache + (min_index << s->l2_bits);
ea2384d3
FB
371 if (new_l2_table) {
372 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
5e5557d9
KW
373 if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
374 s->l2_size * sizeof(uint64_t)) < 0)
ea2384d3
FB
375 return 0;
376 } else {
66f82cee 377 if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
ea2384d3
FB
378 s->l2_size * sizeof(uint64_t))
379 return 0;
380 }
381 s->l2_cache_offsets[min_index] = l2_offset;
382 s->l2_cache_counts[min_index] = 1;
383 found:
384 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
385 cluster_offset = be64_to_cpu(l2_table[l2_index]);
5fafdf24 386 if (!cluster_offset ||
ea2384d3
FB
387 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
388 if (!allocate)
389 return 0;
390 /* allocate a new cluster */
391 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
392 (n_end - n_start) < s->cluster_sectors) {
393 /* if the cluster is already compressed, we must
394 decompress it in the case it is not completely
395 overwritten */
66f82cee 396 if (decompress_cluster(bs, cluster_offset) < 0)
ea2384d3 397 return 0;
66f82cee 398 cluster_offset = bdrv_getlength(bs->file);
5fafdf24 399 cluster_offset = (cluster_offset + s->cluster_size - 1) &
ea2384d3
FB
400 ~(s->cluster_size - 1);
401 /* write the cluster content */
66f82cee 402 if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
ea2384d3
FB
403 s->cluster_size)
404 return -1;
405 } else {
66f82cee 406 cluster_offset = bdrv_getlength(bs->file);
7f48fa1f
AL
407 if (allocate == 1) {
408 /* round to cluster size */
409 cluster_offset = (cluster_offset + s->cluster_size - 1) &
410 ~(s->cluster_size - 1);
66f82cee 411 bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
7f48fa1f
AL
412 /* if encrypted, we must initialize the cluster
413 content which won't be written */
414 if (s->crypt_method &&
415 (n_end - n_start) < s->cluster_sectors) {
416 uint64_t start_sect;
417 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
418 memset(s->cluster_data + 512, 0x00, 512);
419 for(i = 0; i < s->cluster_sectors; i++) {
420 if (i < n_start || i >= n_end) {
421 encrypt_sectors(s, start_sect + i,
422 s->cluster_data,
423 s->cluster_data + 512, 1, 1,
424 &s->aes_encrypt_key);
66f82cee 425 if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
7f48fa1f
AL
426 s->cluster_data, 512) != 512)
427 return -1;
428 }
ea2384d3
FB
429 }
430 }
7f48fa1f
AL
431 } else if (allocate == 2) {
432 cluster_offset |= QCOW_OFLAG_COMPRESSED |
433 (uint64_t)compressed_size << (63 - s->cluster_bits);
ea2384d3
FB
434 }
435 }
436 /* update L2 table */
437 tmp = cpu_to_be64(cluster_offset);
438 l2_table[l2_index] = tmp;
5e5557d9
KW
439 if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
440 &tmp, sizeof(tmp)) < 0)
ea2384d3
FB
441 return 0;
442 }
443 return cluster_offset;
444}
445
b6b8a333 446static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
f8a2e5e3 447 int64_t sector_num, int nb_sectors, int *pnum)
ea2384d3
FB
448{
449 BDRVQcowState *s = bs->opaque;
450 int index_in_cluster, n;
451 uint64_t cluster_offset;
452
f8a2e5e3 453 qemu_co_mutex_lock(&s->lock);
ea2384d3 454 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
f8a2e5e3 455 qemu_co_mutex_unlock(&s->lock);
ea2384d3
FB
456 index_in_cluster = sector_num & (s->cluster_sectors - 1);
457 n = s->cluster_sectors - index_in_cluster;
458 if (n > nb_sectors)
459 n = nb_sectors;
460 *pnum = n;
4bc74be9
PB
461 if (!cluster_offset) {
462 return 0;
463 }
464 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypt_method) {
465 return BDRV_BLOCK_DATA;
466 }
467 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
468 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset;
ea2384d3
FB
469}
470
471static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
472 const uint8_t *buf, int buf_size)
473{
474 z_stream strm1, *strm = &strm1;
475 int ret, out_len;
476
477 memset(strm, 0, sizeof(*strm));
478
479 strm->next_in = (uint8_t *)buf;
480 strm->avail_in = buf_size;
481 strm->next_out = out_buf;
482 strm->avail_out = out_buf_size;
483
484 ret = inflateInit2(strm, -12);
485 if (ret != Z_OK)
486 return -1;
487 ret = inflate(strm, Z_FINISH);
488 out_len = strm->next_out - out_buf;
489 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
490 out_len != out_buf_size) {
491 inflateEnd(strm);
492 return -1;
493 }
494 inflateEnd(strm);
495 return 0;
496}
3b46e624 497
66f82cee 498static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
ea2384d3 499{
66f82cee 500 BDRVQcowState *s = bs->opaque;
ea2384d3
FB
501 int ret, csize;
502 uint64_t coffset;
503
504 coffset = cluster_offset & s->cluster_offset_mask;
505 if (s->cluster_cache_offset != coffset) {
506 csize = cluster_offset >> (63 - s->cluster_bits);
507 csize &= (s->cluster_size - 1);
66f82cee 508 ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
5fafdf24 509 if (ret != csize)
ea2384d3
FB
510 return -1;
511 if (decompress_buffer(s->cluster_cache, s->cluster_size,
512 s->cluster_data, csize) < 0) {
513 return -1;
514 }
515 s->cluster_cache_offset = coffset;
516 }
517 return 0;
518}
519
a968168c 520static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
27deebe8 521 int nb_sectors, QEMUIOVector *qiov)
83f64091 522{
83f64091 523 BDRVQcowState *s = bs->opaque;
83f64091 524 int index_in_cluster;
27deebe8 525 int ret = 0, n;
43ca85b5 526 uint64_t cluster_offset;
430bbaaa
FZ
527 struct iovec hd_iov;
528 QEMUIOVector hd_qiov;
27deebe8
FZ
529 uint8_t *buf;
530 void *orig_buf;
83f64091 531
27deebe8 532 if (qiov->niov > 1) {
0df93305
KW
533 buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
534 if (buf == NULL) {
535 return -ENOMEM;
536 }
27deebe8
FZ
537 } else {
538 orig_buf = NULL;
539 buf = (uint8_t *)qiov->iov->iov_base;
83f64091 540 }
3b46e624 541
27deebe8
FZ
542 qemu_co_mutex_lock(&s->lock);
543
544 while (nb_sectors != 0) {
545 /* prepare next request */
546 cluster_offset = get_cluster_offset(bs, sector_num << 9,
547 0, 0, 0, 0);
548 index_in_cluster = sector_num & (s->cluster_sectors - 1);
549 n = s->cluster_sectors - index_in_cluster;
550 if (n > nb_sectors) {
551 n = nb_sectors;
552 }
ce1a14dc 553
27deebe8
FZ
554 if (!cluster_offset) {
555 if (bs->backing_hd) {
556 /* read from the base image */
557 hd_iov.iov_base = (void *)buf;
558 hd_iov.iov_len = n * 512;
559 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
560 qemu_co_mutex_unlock(&s->lock);
561 ret = bdrv_co_readv(bs->backing_hd, sector_num,
562 n, &hd_qiov);
563 qemu_co_mutex_lock(&s->lock);
564 if (ret < 0) {
565 goto fail;
566 }
567 } else {
568 /* Note: in this case, no need to wait */
569 memset(buf, 0, 512 * n);
570 }
571 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
572 /* add AIO support for compressed blocks ? */
573 if (decompress_cluster(bs, cluster_offset) < 0) {
574 goto fail;
575 }
576 memcpy(buf,
577 s->cluster_cache + index_in_cluster * 512, 512 * n);
578 } else {
579 if ((cluster_offset & 511) != 0) {
580 goto fail;
581 }
582 hd_iov.iov_base = (void *)buf;
430bbaaa
FZ
583 hd_iov.iov_len = n * 512;
584 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
52b8eb60 585 qemu_co_mutex_unlock(&s->lock);
27deebe8
FZ
586 ret = bdrv_co_readv(bs->file,
587 (cluster_offset >> 9) + index_in_cluster,
430bbaaa 588 n, &hd_qiov);
52b8eb60
KW
589 qemu_co_mutex_lock(&s->lock);
590 if (ret < 0) {
27deebe8
FZ
591 break;
592 }
593 if (s->crypt_method) {
594 encrypt_sectors(s, sector_num, buf, buf,
595 n, 0,
596 &s->aes_decrypt_key);
5614c188 597 }
5614c188 598 }
27deebe8 599 ret = 0;
f141eafe 600
27deebe8
FZ
601 nb_sectors -= n;
602 sector_num += n;
603 buf += n * 512;
43ca85b5
FZ
604 }
605
27deebe8 606done:
52b8eb60
KW
607 qemu_co_mutex_unlock(&s->lock);
608
27deebe8 609 if (qiov->niov > 1) {
03396148 610 qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size);
27deebe8 611 qemu_vfree(orig_buf);
b11a24de
KW
612 }
613
52b8eb60 614 return ret;
27deebe8
FZ
615
616fail:
617 ret = -EIO;
618 goto done;
83f64091
FB
619}
620
a968168c 621static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
27deebe8 622 int nb_sectors, QEMUIOVector *qiov)
83f64091 623{
83f64091 624 BDRVQcowState *s = bs->opaque;
83f64091
FB
625 int index_in_cluster;
626 uint64_t cluster_offset;
627 const uint8_t *src_buf;
27deebe8 628 int ret = 0, n;
430bbaaa
FZ
629 uint8_t *cluster_data = NULL;
630 struct iovec hd_iov;
631 QEMUIOVector hd_qiov;
27deebe8
FZ
632 uint8_t *buf;
633 void *orig_buf;
ce1a14dc 634
27deebe8 635 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 636
27deebe8 637 if (qiov->niov > 1) {
0df93305
KW
638 buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
639 if (buf == NULL) {
640 return -ENOMEM;
641 }
d5e6b161 642 qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
83f64091 643 } else {
27deebe8
FZ
644 orig_buf = NULL;
645 buf = (uint8_t *)qiov->iov->iov_base;
83f64091 646 }
c87c0672 647
52b8eb60 648 qemu_co_mutex_lock(&s->lock);
43ca85b5 649
27deebe8 650 while (nb_sectors != 0) {
83f64091 651
27deebe8
FZ
652 index_in_cluster = sector_num & (s->cluster_sectors - 1);
653 n = s->cluster_sectors - index_in_cluster;
654 if (n > nb_sectors) {
655 n = nb_sectors;
656 }
657 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
658 index_in_cluster,
659 index_in_cluster + n);
660 if (!cluster_offset || (cluster_offset & 511) != 0) {
661 ret = -EIO;
662 break;
663 }
664 if (s->crypt_method) {
665 if (!cluster_data) {
666 cluster_data = g_malloc0(s->cluster_size);
667 }
668 encrypt_sectors(s, sector_num, cluster_data, buf,
669 n, 1, &s->aes_encrypt_key);
670 src_buf = cluster_data;
671 } else {
672 src_buf = buf;
673 }
83f64091 674
27deebe8
FZ
675 hd_iov.iov_base = (void *)src_buf;
676 hd_iov.iov_len = n * 512;
677 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
678 qemu_co_mutex_unlock(&s->lock);
679 ret = bdrv_co_writev(bs->file,
680 (cluster_offset >> 9) + index_in_cluster,
681 n, &hd_qiov);
682 qemu_co_mutex_lock(&s->lock);
683 if (ret < 0) {
684 break;
685 }
686 ret = 0;
ad53089b 687
27deebe8
FZ
688 nb_sectors -= n;
689 sector_num += n;
690 buf += n * 512;
691 }
52b8eb60 692 qemu_co_mutex_unlock(&s->lock);
3b46e624 693
27deebe8
FZ
694 if (qiov->niov > 1) {
695 qemu_vfree(orig_buf);
b11a24de 696 }
add8d262 697 g_free(cluster_data);
b11a24de 698
52b8eb60 699 return ret;
83f64091
FB
700}
701
e2731add 702static void qcow_close(BlockDriverState *bs)
ea2384d3
FB
703{
704 BDRVQcowState *s = bs->opaque;
fd9f102c 705
7267c094 706 g_free(s->l1_table);
0df93305 707 qemu_vfree(s->l2_cache);
7267c094
AL
708 g_free(s->cluster_cache);
709 g_free(s->cluster_data);
fd9f102c
KW
710
711 migrate_del_blocker(s->migration_blocker);
712 error_free(s->migration_blocker);
ea2384d3
FB
713}
714
16d12159 715static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
ea2384d3 716{
2b16c9ff 717 int header_size, backing_filename_len, l1_size, shift, i;
ea2384d3 718 QCowHeader header;
2b16c9ff 719 uint8_t *tmp;
0e7e1989 720 int64_t total_size = 0;
16d12159 721 char *backing_file = NULL;
0e7e1989 722 int flags = 0;
34b5d2c6 723 Error *local_err = NULL;
3e1a8134 724 int ret;
2b16c9ff 725 BlockDriverState *qcow_bs;
0e7e1989
KW
726
727 /* Read out options */
16d12159
CL
728 total_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / 512;
729 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
730 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
731 flags |= BLOCK_FLAG_ENCRYPT;
0e7e1989 732 }
ea2384d3 733
c282e1fd 734 ret = bdrv_create_file(filename, opts, &local_err);
2b16c9ff 735 if (ret < 0) {
b6d5066d 736 error_propagate(errp, local_err);
16d12159 737 goto cleanup;
2b16c9ff
LZH
738 }
739
2e40134b
HR
740 qcow_bs = NULL;
741 ret = bdrv_open(&qcow_bs, filename, NULL, NULL,
742 BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL, &local_err);
2b16c9ff 743 if (ret < 0) {
b6d5066d 744 error_propagate(errp, local_err);
16d12159 745 goto cleanup;
2b16c9ff
LZH
746 }
747
748 ret = bdrv_truncate(qcow_bs, 0);
749 if (ret < 0) {
750 goto exit;
751 }
752
ea2384d3
FB
753 memset(&header, 0, sizeof(header));
754 header.magic = cpu_to_be32(QCOW_MAGIC);
755 header.version = cpu_to_be32(QCOW_VERSION);
756 header.size = cpu_to_be64(total_size * 512);
757 header_size = sizeof(header);
758 backing_filename_len = 0;
759 if (backing_file) {
7852e5da
AJ
760 if (strcmp(backing_file, "fat:")) {
761 header.backing_file_offset = cpu_to_be64(header_size);
762 backing_filename_len = strlen(backing_file);
763 header.backing_file_size = cpu_to_be32(backing_filename_len);
764 header_size += backing_filename_len;
765 } else {
766 /* special backing file for vvfat */
767 backing_file = NULL;
768 }
ea2384d3 769 header.cluster_bits = 9; /* 512 byte cluster to avoid copying
dc6fb73d 770 unmodified sectors */
ea2384d3
FB
771 header.l2_bits = 12; /* 32 KB L2 tables */
772 } else {
773 header.cluster_bits = 12; /* 4 KB clusters */
774 header.l2_bits = 9; /* 4 KB L2 tables */
775 }
776 header_size = (header_size + 7) & ~7;
777 shift = header.cluster_bits + header.l2_bits;
778 l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
779
780 header.l1_table_offset = cpu_to_be64(header_size);
ec36ba14 781 if (flags & BLOCK_FLAG_ENCRYPT) {
ea2384d3
FB
782 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
783 } else {
784 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
785 }
3b46e624 786
ea2384d3 787 /* write all the data */
2b16c9ff 788 ret = bdrv_pwrite(qcow_bs, 0, &header, sizeof(header));
3e1a8134 789 if (ret != sizeof(header)) {
3e1a8134
KS
790 goto exit;
791 }
792
ea2384d3 793 if (backing_file) {
2b16c9ff
LZH
794 ret = bdrv_pwrite(qcow_bs, sizeof(header),
795 backing_file, backing_filename_len);
3e1a8134 796 if (ret != backing_filename_len) {
3e1a8134
KS
797 goto exit;
798 }
ea2384d3 799 }
2b16c9ff
LZH
800
801 tmp = g_malloc0(BDRV_SECTOR_SIZE);
802 for (i = 0; i < ((sizeof(uint64_t)*l1_size + BDRV_SECTOR_SIZE - 1)/
803 BDRV_SECTOR_SIZE); i++) {
804 ret = bdrv_pwrite(qcow_bs, header_size +
805 BDRV_SECTOR_SIZE*i, tmp, BDRV_SECTOR_SIZE);
806 if (ret != BDRV_SECTOR_SIZE) {
807 g_free(tmp);
3e1a8134
KS
808 goto exit;
809 }
ea2384d3 810 }
3e1a8134 811
2b16c9ff 812 g_free(tmp);
3e1a8134
KS
813 ret = 0;
814exit:
4f6fd349 815 bdrv_unref(qcow_bs);
16d12159
CL
816cleanup:
817 g_free(backing_file);
3e1a8134 818 return ret;
ea2384d3
FB
819}
820
c47c33b0 821static int qcow_make_empty(BlockDriverState *bs)
95389c86
FB
822{
823 BDRVQcowState *s = bs->opaque;
824 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
83f64091 825 int ret;
95389c86
FB
826
827 memset(s->l1_table, 0, l1_length);
5e5557d9
KW
828 if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
829 l1_length) < 0)
830 return -1;
66f82cee 831 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
83f64091
FB
832 if (ret < 0)
833 return ret;
95389c86
FB
834
835 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
836 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
837 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
838
839 return 0;
840}
841
ea2384d3
FB
842/* XXX: put compressed sectors first, then all the cluster aligned
843 tables to avoid losing bytes in alignment */
5fafdf24 844static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
c47c33b0 845 const uint8_t *buf, int nb_sectors)
ea2384d3
FB
846{
847 BDRVQcowState *s = bs->opaque;
848 z_stream strm;
849 int ret, out_len;
850 uint8_t *out_buf;
851 uint64_t cluster_offset;
852
16b3c5cd
SH
853 if (nb_sectors != s->cluster_sectors) {
854 ret = -EINVAL;
855
856 /* Zero-pad last write if image size is not cluster aligned */
857 if (sector_num + nb_sectors == bs->total_sectors &&
858 nb_sectors < s->cluster_sectors) {
859 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
860 memset(pad_buf, 0, s->cluster_size);
861 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
862 ret = qcow_write_compressed(bs, sector_num,
863 pad_buf, s->cluster_sectors);
864 qemu_vfree(pad_buf);
865 }
866 return ret;
867 }
ea2384d3 868
7267c094 869 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
ea2384d3
FB
870
871 /* best compression, small window, no zlib header */
872 memset(&strm, 0, sizeof(strm));
873 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
5fafdf24 874 Z_DEFLATED, -12,
ea2384d3
FB
875 9, Z_DEFAULT_STRATEGY);
876 if (ret != 0) {
64ebe71a
KW
877 ret = -EINVAL;
878 goto fail;
ea2384d3
FB
879 }
880
881 strm.avail_in = s->cluster_size;
882 strm.next_in = (uint8_t *)buf;
883 strm.avail_out = s->cluster_size;
884 strm.next_out = out_buf;
885
886 ret = deflate(&strm, Z_FINISH);
887 if (ret != Z_STREAM_END && ret != Z_OK) {
ea2384d3 888 deflateEnd(&strm);
64ebe71a
KW
889 ret = -EINVAL;
890 goto fail;
ea2384d3
FB
891 }
892 out_len = strm.next_out - out_buf;
893
894 deflateEnd(&strm);
895
896 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
897 /* could not compress: write normal cluster */
64ebe71a
KW
898 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
899 if (ret < 0) {
900 goto fail;
901 }
ea2384d3 902 } else {
5fafdf24 903 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
ea2384d3 904 out_len, 0, 0);
64ebe71a
KW
905 if (cluster_offset == 0) {
906 ret = -EIO;
907 goto fail;
908 }
909
ea2384d3 910 cluster_offset &= s->cluster_offset_mask;
64ebe71a
KW
911 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
912 if (ret < 0) {
913 goto fail;
ea2384d3
FB
914 }
915 }
3b46e624 916
64ebe71a
KW
917 ret = 0;
918fail:
7267c094 919 g_free(out_buf);
64ebe71a 920 return ret;
ea2384d3
FB
921}
922
c47c33b0
FB
923static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
924{
925 BDRVQcowState *s = bs->opaque;
926 bdi->cluster_size = s->cluster_size;
927 return 0;
928}
929
16d12159
CL
930static QemuOptsList qcow_create_opts = {
931 .name = "qcow-create-opts",
932 .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
933 .desc = {
934 {
935 .name = BLOCK_OPT_SIZE,
936 .type = QEMU_OPT_SIZE,
937 .help = "Virtual disk size"
938 },
939 {
940 .name = BLOCK_OPT_BACKING_FILE,
941 .type = QEMU_OPT_STRING,
942 .help = "File name of a base image"
943 },
944 {
945 .name = BLOCK_OPT_ENCRYPT,
946 .type = QEMU_OPT_BOOL,
947 .help = "Encrypt the image",
948 .def_value_str = "off"
949 },
950 { /* end of list */ }
951 }
0e7e1989
KW
952};
953
5efa9d5a 954static BlockDriver bdrv_qcow = {
e60f469c
AJ
955 .format_name = "qcow",
956 .instance_size = sizeof(BDRVQcowState),
957 .bdrv_probe = qcow_probe,
958 .bdrv_open = qcow_open,
959 .bdrv_close = qcow_close,
16d12159 960 .bdrv_reopen_prepare = qcow_reopen_prepare,
c282e1fd 961 .bdrv_create = qcow_create,
3ac21627 962 .bdrv_has_zero_init = bdrv_has_zero_init_1,
8ee79e70 963 .supports_backing = true,
c68b89ac
KW
964
965 .bdrv_co_readv = qcow_co_readv,
966 .bdrv_co_writev = qcow_co_writev,
b6b8a333 967 .bdrv_co_get_block_status = qcow_co_get_block_status,
c68b89ac
KW
968
969 .bdrv_set_key = qcow_set_key,
970 .bdrv_make_empty = qcow_make_empty,
971 .bdrv_write_compressed = qcow_write_compressed,
972 .bdrv_get_info = qcow_get_info,
0e7e1989 973
16d12159 974 .create_opts = &qcow_create_opts,
ea2384d3 975};
5efa9d5a
AL
976
977static void bdrv_qcow_init(void)
978{
979 bdrv_register(&bdrv_qcow);
980}
981
982block_init(bdrv_qcow_init);