ac97: don't override the pci subsystem id
[qemu.git] / block / qcow.c
blob35e21eb6b3661ce9f3efdb3aab123e7a3084f688
1 /*
2 * Block driver for the QCOW format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include "module.h"
27 #include <zlib.h>
28 #include "aes.h"
30 /**************************************************************/
31 /* QEMU COW block driver with compression and encryption support */
33 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
34 #define QCOW_VERSION 1
36 #define QCOW_CRYPT_NONE 0
37 #define QCOW_CRYPT_AES 1
39 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
41 typedef 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;
54 #define L2_CACHE_SIZE 16
56 typedef struct BDRVQcowState {
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;
76 CoMutex lock;
77 } BDRVQcowState;
79 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
81 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
83 const QCowHeader *cow_header = (const void *)buf;
85 if (buf_size >= sizeof(QCowHeader) &&
86 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
87 be32_to_cpu(cow_header->version) == QCOW_VERSION)
88 return 100;
89 else
90 return 0;
93 static int qcow_open(BlockDriverState *bs, int flags)
95 BDRVQcowState *s = bs->opaque;
96 int len, i, shift;
97 QCowHeader header;
99 if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
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);
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;
127 /* read the level 1 table */
128 shift = s->cluster_bits + s->l2_bits;
129 s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
131 s->l1_table_offset = header.l1_table_offset;
132 s->l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
133 if (!s->l1_table)
134 goto fail;
135 if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
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]);
141 /* alloc L2 cache */
142 s->l2_cache = g_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
143 if (!s->l2_cache)
144 goto fail;
145 s->cluster_cache = g_malloc(s->cluster_size);
146 if (!s->cluster_cache)
147 goto fail;
148 s->cluster_data = g_malloc(s->cluster_size);
149 if (!s->cluster_data)
150 goto fail;
151 s->cluster_cache_offset = -1;
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;
158 if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
159 goto fail;
160 bs->backing_file[len] = '\0';
163 qemu_co_mutex_init(&s->lock);
164 return 0;
166 fail:
167 g_free(s->l1_table);
168 g_free(s->l2_cache);
169 g_free(s->cluster_cache);
170 g_free(s->cluster_data);
171 return -1;
174 static int qcow_set_key(BlockDriverState *bs, const char *key)
176 BDRVQcowState *s = bs->opaque;
177 uint8_t keybuf[16];
178 int len, i;
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];
189 s->crypt_method = s->crypt_method_header;
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;
195 return 0;
198 /* The crypt function is compatible with the linux cryptoloop
199 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
200 supported */
201 static 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)
206 union {
207 uint64_t ll[2];
208 uint8_t b[16];
209 } ivec;
210 int i;
212 for(i = 0; i < nb_sectors; i++) {
213 ivec.ll[0] = cpu_to_le64(sector_num);
214 ivec.ll[1] = 0;
215 AES_cbc_encrypt(in_buf, out_buf, 512, key,
216 ivec.b, enc);
217 sector_num++;
218 in_buf += 512;
219 out_buf += 512;
223 /* 'allocate' is:
225 * 0 to not allocate.
227 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
228 * 'n_end')
230 * 2 to allocate a compressed cluster of size
231 * 'compressed_size'. 'compressed_size' must be > 0 and <
232 * cluster_size
234 * return 0 if not allocated.
236 static uint64_t get_cluster_offset(BlockDriverState *bs,
237 uint64_t offset, int allocate,
238 int compressed_size,
239 int n_start, int n_end)
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;
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 */
254 l2_offset = bdrv_getlength(bs->file);
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);
260 if (bdrv_pwrite_sync(bs->file,
261 s->l1_table_offset + l1_index * sizeof(tmp),
262 &tmp, sizeof(tmp)) < 0)
263 return 0;
264 new_l2_table = 1;
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;
274 l2_table = s->l2_cache + (i << s->l2_bits);
275 goto found;
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;
287 l2_table = s->l2_cache + (min_index << s->l2_bits);
288 if (new_l2_table) {
289 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
290 if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
291 s->l2_size * sizeof(uint64_t)) < 0)
292 return 0;
293 } else {
294 if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
295 s->l2_size * sizeof(uint64_t))
296 return 0;
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]);
303 if (!cluster_offset ||
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 */
313 if (decompress_cluster(bs, cluster_offset) < 0)
314 return 0;
315 cluster_offset = bdrv_getlength(bs->file);
316 cluster_offset = (cluster_offset + s->cluster_size - 1) &
317 ~(s->cluster_size - 1);
318 /* write the cluster content */
319 if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
320 s->cluster_size)
321 return -1;
322 } else {
323 cluster_offset = bdrv_getlength(bs->file);
324 if (allocate == 1) {
325 /* round to cluster size */
326 cluster_offset = (cluster_offset + s->cluster_size - 1) &
327 ~(s->cluster_size - 1);
328 bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
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);
342 if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
343 s->cluster_data, 512) != 512)
344 return -1;
348 } else if (allocate == 2) {
349 cluster_offset |= QCOW_OFLAG_COMPRESSED |
350 (uint64_t)compressed_size << (63 - s->cluster_bits);
353 /* update L2 table */
354 tmp = cpu_to_be64(cluster_offset);
355 l2_table[l2_index] = tmp;
356 if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
357 &tmp, sizeof(tmp)) < 0)
358 return 0;
360 return cluster_offset;
363 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
364 int nb_sectors, int *pnum)
366 BDRVQcowState *s = bs->opaque;
367 int index_in_cluster, n;
368 uint64_t cluster_offset;
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);
379 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
380 const uint8_t *buf, int buf_size)
382 z_stream strm1, *strm = &strm1;
383 int ret, out_len;
385 memset(strm, 0, sizeof(*strm));
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;
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;
402 inflateEnd(strm);
403 return 0;
406 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
408 BDRVQcowState *s = bs->opaque;
409 int ret, csize;
410 uint64_t coffset;
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);
416 ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
417 if (ret != csize)
418 return -1;
419 if (decompress_buffer(s->cluster_cache, s->cluster_size,
420 s->cluster_data, csize) < 0) {
421 return -1;
423 s->cluster_cache_offset = coffset;
425 return 0;
428 static int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
429 int nb_sectors, QEMUIOVector *qiov)
431 BDRVQcowState *s = bs->opaque;
432 int index_in_cluster;
433 int ret = 0, n;
434 uint64_t cluster_offset;
435 struct iovec hd_iov;
436 QEMUIOVector hd_qiov;
437 uint8_t *buf;
438 void *orig_buf;
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;
447 qemu_co_mutex_lock(&s->lock);
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;
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;
472 } else {
473 /* Note: in this case, no need to wait */
474 memset(buf, 0, 512 * n);
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;
481 memcpy(buf,
482 s->cluster_cache + index_in_cluster * 512, 512 * n);
483 } else {
484 if ((cluster_offset & 511) != 0) {
485 goto fail;
487 hd_iov.iov_base = (void *)buf;
488 hd_iov.iov_len = n * 512;
489 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
490 qemu_co_mutex_unlock(&s->lock);
491 ret = bdrv_co_readv(bs->file,
492 (cluster_offset >> 9) + index_in_cluster,
493 n, &hd_qiov);
494 qemu_co_mutex_lock(&s->lock);
495 if (ret < 0) {
496 break;
498 if (s->crypt_method) {
499 encrypt_sectors(s, sector_num, buf, buf,
500 n, 0,
501 &s->aes_decrypt_key);
504 ret = 0;
506 nb_sectors -= n;
507 sector_num += n;
508 buf += n * 512;
511 done:
512 qemu_co_mutex_unlock(&s->lock);
514 if (qiov->niov > 1) {
515 qemu_iovec_from_buffer(qiov, orig_buf, qiov->size);
516 qemu_vfree(orig_buf);
519 return ret;
521 fail:
522 ret = -EIO;
523 goto done;
526 static int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
527 int nb_sectors, QEMUIOVector *qiov)
529 BDRVQcowState *s = bs->opaque;
530 int index_in_cluster;
531 uint64_t cluster_offset;
532 const uint8_t *src_buf;
533 int ret = 0, n;
534 uint8_t *cluster_data = NULL;
535 struct iovec hd_iov;
536 QEMUIOVector hd_qiov;
537 uint8_t *buf;
538 void *orig_buf;
540 s->cluster_cache_offset = -1; /* disable compressed cache */
542 if (qiov->niov > 1) {
543 buf = orig_buf = qemu_blockalign(bs, qiov->size);
544 qemu_iovec_to_buffer(qiov, buf);
545 } else {
546 orig_buf = NULL;
547 buf = (uint8_t *)qiov->iov->iov_base;
550 qemu_co_mutex_lock(&s->lock);
552 while (nb_sectors != 0) {
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;
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;
566 if (s->crypt_method) {
567 if (!cluster_data) {
568 cluster_data = g_malloc0(s->cluster_size);
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;
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;
588 ret = 0;
590 nb_sectors -= n;
591 sector_num += n;
592 buf += n * 512;
594 qemu_co_mutex_unlock(&s->lock);
596 if (qiov->niov > 1) {
597 qemu_vfree(orig_buf);
599 g_free(cluster_data);
601 return ret;
604 static void qcow_close(BlockDriverState *bs)
606 BDRVQcowState *s = bs->opaque;
607 g_free(s->l1_table);
608 g_free(s->l2_cache);
609 g_free(s->cluster_cache);
610 g_free(s->cluster_data);
613 static int qcow_create(const char *filename, QEMUOptionParameter *options)
615 int fd, header_size, backing_filename_len, l1_size, i, shift;
616 QCowHeader header;
617 uint64_t tmp;
618 int64_t total_size = 0;
619 const char *backing_file = NULL;
620 int flags = 0;
621 int ret;
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;
632 options++;
635 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
636 if (fd < 0)
637 return -errno;
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) {
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;
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 */
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;
665 header.l1_table_offset = cpu_to_be64(header_size);
666 if (flags & BLOCK_FLAG_ENCRYPT) {
667 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
668 } else {
669 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
672 /* write all the data */
673 ret = qemu_write_full(fd, &header, sizeof(header));
674 if (ret != sizeof(header)) {
675 ret = -errno;
676 goto exit;
679 if (backing_file) {
680 ret = qemu_write_full(fd, backing_file, backing_filename_len);
681 if (ret != backing_filename_len) {
682 ret = -errno;
683 goto exit;
687 lseek(fd, header_size, SEEK_SET);
688 tmp = 0;
689 for(i = 0;i < l1_size; i++) {
690 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
691 if (ret != sizeof(tmp)) {
692 ret = -errno;
693 goto exit;
697 ret = 0;
698 exit:
699 close(fd);
700 return ret;
703 static int qcow_make_empty(BlockDriverState *bs)
705 BDRVQcowState *s = bs->opaque;
706 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
707 int ret;
709 memset(s->l1_table, 0, l1_length);
710 if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
711 l1_length) < 0)
712 return -1;
713 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
714 if (ret < 0)
715 return ret;
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));
721 return 0;
724 /* XXX: put compressed sectors first, then all the cluster aligned
725 tables to avoid losing bytes in alignment */
726 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
727 const uint8_t *buf, int nb_sectors)
729 BDRVQcowState *s = bs->opaque;
730 z_stream strm;
731 int ret, out_len;
732 uint8_t *out_buf;
733 uint64_t cluster_offset;
735 if (nb_sectors != s->cluster_sectors)
736 return -EINVAL;
738 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
740 /* best compression, small window, no zlib header */
741 memset(&strm, 0, sizeof(strm));
742 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
743 Z_DEFLATED, -12,
744 9, Z_DEFAULT_STRATEGY);
745 if (ret != 0) {
746 ret = -EINVAL;
747 goto fail;
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;
755 ret = deflate(&strm, Z_FINISH);
756 if (ret != Z_STREAM_END && ret != Z_OK) {
757 deflateEnd(&strm);
758 ret = -EINVAL;
759 goto fail;
761 out_len = strm.next_out - out_buf;
763 deflateEnd(&strm);
765 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
766 /* could not compress: write normal cluster */
767 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
768 if (ret < 0) {
769 goto fail;
771 } else {
772 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
773 out_len, 0, 0);
774 if (cluster_offset == 0) {
775 ret = -EIO;
776 goto fail;
779 cluster_offset &= s->cluster_offset_mask;
780 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
781 if (ret < 0) {
782 goto fail;
786 ret = 0;
787 fail:
788 g_free(out_buf);
789 return ret;
792 static coroutine_fn int qcow_co_flush(BlockDriverState *bs)
794 return bdrv_co_flush(bs->file);
797 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
799 BDRVQcowState *s = bs->opaque;
800 bdi->cluster_size = s->cluster_size;
801 return 0;
805 static QEMUOptionParameter qcow_create_options[] = {
807 .name = BLOCK_OPT_SIZE,
808 .type = OPT_SIZE,
809 .help = "Virtual disk size"
812 .name = BLOCK_OPT_BACKING_FILE,
813 .type = OPT_STRING,
814 .help = "File name of a base image"
817 .name = BLOCK_OPT_ENCRYPT,
818 .type = OPT_FLAG,
819 .help = "Encrypt the image"
821 { NULL }
824 static BlockDriver bdrv_qcow = {
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,
831 .bdrv_is_allocated = qcow_is_allocated,
832 .bdrv_set_key = qcow_set_key,
833 .bdrv_make_empty = qcow_make_empty,
834 .bdrv_co_readv = qcow_co_readv,
835 .bdrv_co_writev = qcow_co_writev,
836 .bdrv_co_flush = qcow_co_flush,
837 .bdrv_write_compressed = qcow_write_compressed,
838 .bdrv_get_info = qcow_get_info,
840 .create_options = qcow_create_options,
843 static void bdrv_qcow_init(void)
845 bdrv_register(&bdrv_qcow);
848 block_init(bdrv_qcow_init);