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