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