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