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