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