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