9pfs: improve portability to older systems
[qemu/qmp-unstable.git] / block / qcow.c
blob4814ed0ced7d7d347719a0969931ef51a07a6a50
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"
29 #include "migration.h"
31 /**************************************************************/
32 /* QEMU COW block driver with compression and encryption support */
34 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35 #define QCOW_VERSION 1
37 #define QCOW_CRYPT_NONE 0
38 #define QCOW_CRYPT_AES 1
40 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
42 typedef 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;
55 #define L2_CACHE_SIZE 16
57 typedef struct BDRVQcowState {
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;
77 CoMutex lock;
78 Error *migration_blocker;
79 } BDRVQcowState;
81 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
83 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
85 const QCowHeader *cow_header = (const void *)buf;
87 if (buf_size >= sizeof(QCowHeader) &&
88 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
89 be32_to_cpu(cow_header->version) == QCOW_VERSION)
90 return 100;
91 else
92 return 0;
95 static int qcow_open(BlockDriverState *bs, int flags)
97 BDRVQcowState *s = bs->opaque;
98 int len, i, shift;
99 QCowHeader header;
101 if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
102 goto fail;
103 be32_to_cpus(&header.magic);
104 be32_to_cpus(&header.version);
105 be64_to_cpus(&header.backing_file_offset);
106 be32_to_cpus(&header.backing_file_size);
107 be32_to_cpus(&header.mtime);
108 be64_to_cpus(&header.size);
109 be32_to_cpus(&header.crypt_method);
110 be64_to_cpus(&header.l1_table_offset);
112 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
113 goto fail;
114 if (header.size <= 1 || header.cluster_bits < 9)
115 goto fail;
116 if (header.crypt_method > QCOW_CRYPT_AES)
117 goto fail;
118 s->crypt_method_header = header.crypt_method;
119 if (s->crypt_method_header)
120 bs->encrypted = 1;
121 s->cluster_bits = header.cluster_bits;
122 s->cluster_size = 1 << s->cluster_bits;
123 s->cluster_sectors = 1 << (s->cluster_bits - 9);
124 s->l2_bits = header.l2_bits;
125 s->l2_size = 1 << s->l2_bits;
126 bs->total_sectors = header.size / 512;
127 s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
129 /* read the level 1 table */
130 shift = s->cluster_bits + s->l2_bits;
131 s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
133 s->l1_table_offset = header.l1_table_offset;
134 s->l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
135 if (!s->l1_table)
136 goto fail;
137 if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
138 s->l1_size * sizeof(uint64_t))
139 goto fail;
140 for(i = 0;i < s->l1_size; i++) {
141 be64_to_cpus(&s->l1_table[i]);
143 /* alloc L2 cache */
144 s->l2_cache = g_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
145 if (!s->l2_cache)
146 goto fail;
147 s->cluster_cache = g_malloc(s->cluster_size);
148 if (!s->cluster_cache)
149 goto fail;
150 s->cluster_data = g_malloc(s->cluster_size);
151 if (!s->cluster_data)
152 goto fail;
153 s->cluster_cache_offset = -1;
155 /* read the backing file name */
156 if (header.backing_file_offset != 0) {
157 len = header.backing_file_size;
158 if (len > 1023)
159 len = 1023;
160 if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
161 goto fail;
162 bs->backing_file[len] = '\0';
165 /* Disable migration when qcow images are used */
166 error_set(&s->migration_blocker,
167 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
168 "qcow", bs->device_name, "live migration");
169 migrate_add_blocker(s->migration_blocker);
171 qemu_co_mutex_init(&s->lock);
172 return 0;
174 fail:
175 g_free(s->l1_table);
176 g_free(s->l2_cache);
177 g_free(s->cluster_cache);
178 g_free(s->cluster_data);
179 return -1;
182 static int qcow_set_key(BlockDriverState *bs, const char *key)
184 BDRVQcowState *s = bs->opaque;
185 uint8_t keybuf[16];
186 int len, i;
188 memset(keybuf, 0, 16);
189 len = strlen(key);
190 if (len > 16)
191 len = 16;
192 /* XXX: we could compress the chars to 7 bits to increase
193 entropy */
194 for(i = 0;i < len;i++) {
195 keybuf[i] = key[i];
197 s->crypt_method = s->crypt_method_header;
199 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
200 return -1;
201 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
202 return -1;
203 return 0;
206 /* The crypt function is compatible with the linux cryptoloop
207 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
208 supported */
209 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
210 uint8_t *out_buf, const uint8_t *in_buf,
211 int nb_sectors, int enc,
212 const AES_KEY *key)
214 union {
215 uint64_t ll[2];
216 uint8_t b[16];
217 } ivec;
218 int i;
220 for(i = 0; i < nb_sectors; i++) {
221 ivec.ll[0] = cpu_to_le64(sector_num);
222 ivec.ll[1] = 0;
223 AES_cbc_encrypt(in_buf, out_buf, 512, key,
224 ivec.b, enc);
225 sector_num++;
226 in_buf += 512;
227 out_buf += 512;
231 /* 'allocate' is:
233 * 0 to not allocate.
235 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
236 * 'n_end')
238 * 2 to allocate a compressed cluster of size
239 * 'compressed_size'. 'compressed_size' must be > 0 and <
240 * cluster_size
242 * return 0 if not allocated.
244 static uint64_t get_cluster_offset(BlockDriverState *bs,
245 uint64_t offset, int allocate,
246 int compressed_size,
247 int n_start, int n_end)
249 BDRVQcowState *s = bs->opaque;
250 int min_index, i, j, l1_index, l2_index;
251 uint64_t l2_offset, *l2_table, cluster_offset, tmp;
252 uint32_t min_count;
253 int new_l2_table;
255 l1_index = offset >> (s->l2_bits + s->cluster_bits);
256 l2_offset = s->l1_table[l1_index];
257 new_l2_table = 0;
258 if (!l2_offset) {
259 if (!allocate)
260 return 0;
261 /* allocate a new l2 entry */
262 l2_offset = bdrv_getlength(bs->file);
263 /* round to cluster size */
264 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
265 /* update the L1 entry */
266 s->l1_table[l1_index] = l2_offset;
267 tmp = cpu_to_be64(l2_offset);
268 if (bdrv_pwrite_sync(bs->file,
269 s->l1_table_offset + l1_index * sizeof(tmp),
270 &tmp, sizeof(tmp)) < 0)
271 return 0;
272 new_l2_table = 1;
274 for(i = 0; i < L2_CACHE_SIZE; i++) {
275 if (l2_offset == s->l2_cache_offsets[i]) {
276 /* increment the hit count */
277 if (++s->l2_cache_counts[i] == 0xffffffff) {
278 for(j = 0; j < L2_CACHE_SIZE; j++) {
279 s->l2_cache_counts[j] >>= 1;
282 l2_table = s->l2_cache + (i << s->l2_bits);
283 goto found;
286 /* not found: load a new entry in the least used one */
287 min_index = 0;
288 min_count = 0xffffffff;
289 for(i = 0; i < L2_CACHE_SIZE; i++) {
290 if (s->l2_cache_counts[i] < min_count) {
291 min_count = s->l2_cache_counts[i];
292 min_index = i;
295 l2_table = s->l2_cache + (min_index << s->l2_bits);
296 if (new_l2_table) {
297 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
298 if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
299 s->l2_size * sizeof(uint64_t)) < 0)
300 return 0;
301 } else {
302 if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
303 s->l2_size * sizeof(uint64_t))
304 return 0;
306 s->l2_cache_offsets[min_index] = l2_offset;
307 s->l2_cache_counts[min_index] = 1;
308 found:
309 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
310 cluster_offset = be64_to_cpu(l2_table[l2_index]);
311 if (!cluster_offset ||
312 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
313 if (!allocate)
314 return 0;
315 /* allocate a new cluster */
316 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
317 (n_end - n_start) < s->cluster_sectors) {
318 /* if the cluster is already compressed, we must
319 decompress it in the case it is not completely
320 overwritten */
321 if (decompress_cluster(bs, cluster_offset) < 0)
322 return 0;
323 cluster_offset = bdrv_getlength(bs->file);
324 cluster_offset = (cluster_offset + s->cluster_size - 1) &
325 ~(s->cluster_size - 1);
326 /* write the cluster content */
327 if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
328 s->cluster_size)
329 return -1;
330 } else {
331 cluster_offset = bdrv_getlength(bs->file);
332 if (allocate == 1) {
333 /* round to cluster size */
334 cluster_offset = (cluster_offset + s->cluster_size - 1) &
335 ~(s->cluster_size - 1);
336 bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
337 /* if encrypted, we must initialize the cluster
338 content which won't be written */
339 if (s->crypt_method &&
340 (n_end - n_start) < s->cluster_sectors) {
341 uint64_t start_sect;
342 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
343 memset(s->cluster_data + 512, 0x00, 512);
344 for(i = 0; i < s->cluster_sectors; i++) {
345 if (i < n_start || i >= n_end) {
346 encrypt_sectors(s, start_sect + i,
347 s->cluster_data,
348 s->cluster_data + 512, 1, 1,
349 &s->aes_encrypt_key);
350 if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
351 s->cluster_data, 512) != 512)
352 return -1;
356 } else if (allocate == 2) {
357 cluster_offset |= QCOW_OFLAG_COMPRESSED |
358 (uint64_t)compressed_size << (63 - s->cluster_bits);
361 /* update L2 table */
362 tmp = cpu_to_be64(cluster_offset);
363 l2_table[l2_index] = tmp;
364 if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
365 &tmp, sizeof(tmp)) < 0)
366 return 0;
368 return cluster_offset;
371 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
372 int nb_sectors, int *pnum)
374 BDRVQcowState *s = bs->opaque;
375 int index_in_cluster, n;
376 uint64_t cluster_offset;
378 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
379 index_in_cluster = sector_num & (s->cluster_sectors - 1);
380 n = s->cluster_sectors - index_in_cluster;
381 if (n > nb_sectors)
382 n = nb_sectors;
383 *pnum = n;
384 return (cluster_offset != 0);
387 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
388 const uint8_t *buf, int buf_size)
390 z_stream strm1, *strm = &strm1;
391 int ret, out_len;
393 memset(strm, 0, sizeof(*strm));
395 strm->next_in = (uint8_t *)buf;
396 strm->avail_in = buf_size;
397 strm->next_out = out_buf;
398 strm->avail_out = out_buf_size;
400 ret = inflateInit2(strm, -12);
401 if (ret != Z_OK)
402 return -1;
403 ret = inflate(strm, Z_FINISH);
404 out_len = strm->next_out - out_buf;
405 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
406 out_len != out_buf_size) {
407 inflateEnd(strm);
408 return -1;
410 inflateEnd(strm);
411 return 0;
414 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
416 BDRVQcowState *s = bs->opaque;
417 int ret, csize;
418 uint64_t coffset;
420 coffset = cluster_offset & s->cluster_offset_mask;
421 if (s->cluster_cache_offset != coffset) {
422 csize = cluster_offset >> (63 - s->cluster_bits);
423 csize &= (s->cluster_size - 1);
424 ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
425 if (ret != csize)
426 return -1;
427 if (decompress_buffer(s->cluster_cache, s->cluster_size,
428 s->cluster_data, csize) < 0) {
429 return -1;
431 s->cluster_cache_offset = coffset;
433 return 0;
436 static int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
437 int nb_sectors, QEMUIOVector *qiov)
439 BDRVQcowState *s = bs->opaque;
440 int index_in_cluster;
441 int ret = 0, n;
442 uint64_t cluster_offset;
443 struct iovec hd_iov;
444 QEMUIOVector hd_qiov;
445 uint8_t *buf;
446 void *orig_buf;
448 if (qiov->niov > 1) {
449 buf = orig_buf = qemu_blockalign(bs, qiov->size);
450 } else {
451 orig_buf = NULL;
452 buf = (uint8_t *)qiov->iov->iov_base;
455 qemu_co_mutex_lock(&s->lock);
457 while (nb_sectors != 0) {
458 /* prepare next request */
459 cluster_offset = get_cluster_offset(bs, sector_num << 9,
460 0, 0, 0, 0);
461 index_in_cluster = sector_num & (s->cluster_sectors - 1);
462 n = s->cluster_sectors - index_in_cluster;
463 if (n > nb_sectors) {
464 n = nb_sectors;
467 if (!cluster_offset) {
468 if (bs->backing_hd) {
469 /* read from the base image */
470 hd_iov.iov_base = (void *)buf;
471 hd_iov.iov_len = n * 512;
472 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
473 qemu_co_mutex_unlock(&s->lock);
474 ret = bdrv_co_readv(bs->backing_hd, sector_num,
475 n, &hd_qiov);
476 qemu_co_mutex_lock(&s->lock);
477 if (ret < 0) {
478 goto fail;
480 } else {
481 /* Note: in this case, no need to wait */
482 memset(buf, 0, 512 * n);
484 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
485 /* add AIO support for compressed blocks ? */
486 if (decompress_cluster(bs, cluster_offset) < 0) {
487 goto fail;
489 memcpy(buf,
490 s->cluster_cache + index_in_cluster * 512, 512 * n);
491 } else {
492 if ((cluster_offset & 511) != 0) {
493 goto fail;
495 hd_iov.iov_base = (void *)buf;
496 hd_iov.iov_len = n * 512;
497 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
498 qemu_co_mutex_unlock(&s->lock);
499 ret = bdrv_co_readv(bs->file,
500 (cluster_offset >> 9) + index_in_cluster,
501 n, &hd_qiov);
502 qemu_co_mutex_lock(&s->lock);
503 if (ret < 0) {
504 break;
506 if (s->crypt_method) {
507 encrypt_sectors(s, sector_num, buf, buf,
508 n, 0,
509 &s->aes_decrypt_key);
512 ret = 0;
514 nb_sectors -= n;
515 sector_num += n;
516 buf += n * 512;
519 done:
520 qemu_co_mutex_unlock(&s->lock);
522 if (qiov->niov > 1) {
523 qemu_iovec_from_buffer(qiov, orig_buf, qiov->size);
524 qemu_vfree(orig_buf);
527 return ret;
529 fail:
530 ret = -EIO;
531 goto done;
534 static int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
535 int nb_sectors, QEMUIOVector *qiov)
537 BDRVQcowState *s = bs->opaque;
538 int index_in_cluster;
539 uint64_t cluster_offset;
540 const uint8_t *src_buf;
541 int ret = 0, n;
542 uint8_t *cluster_data = NULL;
543 struct iovec hd_iov;
544 QEMUIOVector hd_qiov;
545 uint8_t *buf;
546 void *orig_buf;
548 s->cluster_cache_offset = -1; /* disable compressed cache */
550 if (qiov->niov > 1) {
551 buf = orig_buf = qemu_blockalign(bs, qiov->size);
552 qemu_iovec_to_buffer(qiov, buf);
553 } else {
554 orig_buf = NULL;
555 buf = (uint8_t *)qiov->iov->iov_base;
558 qemu_co_mutex_lock(&s->lock);
560 while (nb_sectors != 0) {
562 index_in_cluster = sector_num & (s->cluster_sectors - 1);
563 n = s->cluster_sectors - index_in_cluster;
564 if (n > nb_sectors) {
565 n = nb_sectors;
567 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
568 index_in_cluster,
569 index_in_cluster + n);
570 if (!cluster_offset || (cluster_offset & 511) != 0) {
571 ret = -EIO;
572 break;
574 if (s->crypt_method) {
575 if (!cluster_data) {
576 cluster_data = g_malloc0(s->cluster_size);
578 encrypt_sectors(s, sector_num, cluster_data, buf,
579 n, 1, &s->aes_encrypt_key);
580 src_buf = cluster_data;
581 } else {
582 src_buf = buf;
585 hd_iov.iov_base = (void *)src_buf;
586 hd_iov.iov_len = n * 512;
587 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
588 qemu_co_mutex_unlock(&s->lock);
589 ret = bdrv_co_writev(bs->file,
590 (cluster_offset >> 9) + index_in_cluster,
591 n, &hd_qiov);
592 qemu_co_mutex_lock(&s->lock);
593 if (ret < 0) {
594 break;
596 ret = 0;
598 nb_sectors -= n;
599 sector_num += n;
600 buf += n * 512;
602 qemu_co_mutex_unlock(&s->lock);
604 if (qiov->niov > 1) {
605 qemu_vfree(orig_buf);
607 g_free(cluster_data);
609 return ret;
612 static void qcow_close(BlockDriverState *bs)
614 BDRVQcowState *s = bs->opaque;
616 g_free(s->l1_table);
617 g_free(s->l2_cache);
618 g_free(s->cluster_cache);
619 g_free(s->cluster_data);
621 migrate_del_blocker(s->migration_blocker);
622 error_free(s->migration_blocker);
625 static int qcow_create(const char *filename, QEMUOptionParameter *options)
627 int fd, header_size, backing_filename_len, l1_size, i, shift;
628 QCowHeader header;
629 uint64_t tmp;
630 int64_t total_size = 0;
631 const char *backing_file = NULL;
632 int flags = 0;
633 int ret;
635 /* Read out options */
636 while (options && options->name) {
637 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
638 total_size = options->value.n / 512;
639 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
640 backing_file = options->value.s;
641 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
642 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
644 options++;
647 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
648 if (fd < 0)
649 return -errno;
650 memset(&header, 0, sizeof(header));
651 header.magic = cpu_to_be32(QCOW_MAGIC);
652 header.version = cpu_to_be32(QCOW_VERSION);
653 header.size = cpu_to_be64(total_size * 512);
654 header_size = sizeof(header);
655 backing_filename_len = 0;
656 if (backing_file) {
657 if (strcmp(backing_file, "fat:")) {
658 header.backing_file_offset = cpu_to_be64(header_size);
659 backing_filename_len = strlen(backing_file);
660 header.backing_file_size = cpu_to_be32(backing_filename_len);
661 header_size += backing_filename_len;
662 } else {
663 /* special backing file for vvfat */
664 backing_file = NULL;
666 header.cluster_bits = 9; /* 512 byte cluster to avoid copying
667 unmodifyed sectors */
668 header.l2_bits = 12; /* 32 KB L2 tables */
669 } else {
670 header.cluster_bits = 12; /* 4 KB clusters */
671 header.l2_bits = 9; /* 4 KB L2 tables */
673 header_size = (header_size + 7) & ~7;
674 shift = header.cluster_bits + header.l2_bits;
675 l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
677 header.l1_table_offset = cpu_to_be64(header_size);
678 if (flags & BLOCK_FLAG_ENCRYPT) {
679 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
680 } else {
681 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
684 /* write all the data */
685 ret = qemu_write_full(fd, &header, sizeof(header));
686 if (ret != sizeof(header)) {
687 ret = -errno;
688 goto exit;
691 if (backing_file) {
692 ret = qemu_write_full(fd, backing_file, backing_filename_len);
693 if (ret != backing_filename_len) {
694 ret = -errno;
695 goto exit;
699 lseek(fd, header_size, SEEK_SET);
700 tmp = 0;
701 for(i = 0;i < l1_size; i++) {
702 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
703 if (ret != sizeof(tmp)) {
704 ret = -errno;
705 goto exit;
709 ret = 0;
710 exit:
711 close(fd);
712 return ret;
715 static int qcow_make_empty(BlockDriverState *bs)
717 BDRVQcowState *s = bs->opaque;
718 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
719 int ret;
721 memset(s->l1_table, 0, l1_length);
722 if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
723 l1_length) < 0)
724 return -1;
725 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
726 if (ret < 0)
727 return ret;
729 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
730 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
731 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
733 return 0;
736 /* XXX: put compressed sectors first, then all the cluster aligned
737 tables to avoid losing bytes in alignment */
738 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
739 const uint8_t *buf, int nb_sectors)
741 BDRVQcowState *s = bs->opaque;
742 z_stream strm;
743 int ret, out_len;
744 uint8_t *out_buf;
745 uint64_t cluster_offset;
747 if (nb_sectors != s->cluster_sectors)
748 return -EINVAL;
750 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
752 /* best compression, small window, no zlib header */
753 memset(&strm, 0, sizeof(strm));
754 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
755 Z_DEFLATED, -12,
756 9, Z_DEFAULT_STRATEGY);
757 if (ret != 0) {
758 ret = -EINVAL;
759 goto fail;
762 strm.avail_in = s->cluster_size;
763 strm.next_in = (uint8_t *)buf;
764 strm.avail_out = s->cluster_size;
765 strm.next_out = out_buf;
767 ret = deflate(&strm, Z_FINISH);
768 if (ret != Z_STREAM_END && ret != Z_OK) {
769 deflateEnd(&strm);
770 ret = -EINVAL;
771 goto fail;
773 out_len = strm.next_out - out_buf;
775 deflateEnd(&strm);
777 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
778 /* could not compress: write normal cluster */
779 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
780 if (ret < 0) {
781 goto fail;
783 } else {
784 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
785 out_len, 0, 0);
786 if (cluster_offset == 0) {
787 ret = -EIO;
788 goto fail;
791 cluster_offset &= s->cluster_offset_mask;
792 ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
793 if (ret < 0) {
794 goto fail;
798 ret = 0;
799 fail:
800 g_free(out_buf);
801 return ret;
804 static coroutine_fn int qcow_co_flush(BlockDriverState *bs)
806 return bdrv_co_flush(bs->file);
809 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
811 BDRVQcowState *s = bs->opaque;
812 bdi->cluster_size = s->cluster_size;
813 return 0;
817 static QEMUOptionParameter qcow_create_options[] = {
819 .name = BLOCK_OPT_SIZE,
820 .type = OPT_SIZE,
821 .help = "Virtual disk size"
824 .name = BLOCK_OPT_BACKING_FILE,
825 .type = OPT_STRING,
826 .help = "File name of a base image"
829 .name = BLOCK_OPT_ENCRYPT,
830 .type = OPT_FLAG,
831 .help = "Encrypt the image"
833 { NULL }
836 static BlockDriver bdrv_qcow = {
837 .format_name = "qcow",
838 .instance_size = sizeof(BDRVQcowState),
839 .bdrv_probe = qcow_probe,
840 .bdrv_open = qcow_open,
841 .bdrv_close = qcow_close,
842 .bdrv_create = qcow_create,
844 .bdrv_co_readv = qcow_co_readv,
845 .bdrv_co_writev = qcow_co_writev,
846 .bdrv_co_flush_to_disk = qcow_co_flush,
847 .bdrv_is_allocated = qcow_is_allocated,
849 .bdrv_set_key = qcow_set_key,
850 .bdrv_make_empty = qcow_make_empty,
851 .bdrv_write_compressed = qcow_write_compressed,
852 .bdrv_get_info = qcow_get_info,
854 .create_options = qcow_create_options,
857 static void bdrv_qcow_init(void)
859 bdrv_register(&bdrv_qcow);
862 block_init(bdrv_qcow_init);