]> git.proxmox.com Git - qemu.git/blob - block/qcow.c
Merge remote-tracking branch 'aneesh/for-upstream-6' into staging
[qemu.git] / block / qcow.c
1 /*
2 * Block driver for the QCOW format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
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 */
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include "module.h"
27 #include <zlib.h>
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
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;
53
54 #define L2_CACHE_SIZE 16
55
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;
78
79 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
80
81 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
82 {
83 const QCowHeader *cow_header = (const void *)buf;
84
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;
91 }
92
93 static int qcow_open(BlockDriverState *bs, int flags)
94 {
95 BDRVQcowState *s = bs->opaque;
96 int len, i, shift;
97 QCowHeader header;
98
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);
109
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;
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]);
140 }
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;
152
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';
161 }
162
163 qemu_co_mutex_init(&s->lock);
164 return 0;
165
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;
172 }
173
174 static int qcow_set_key(BlockDriverState *bs, const char *key)
175 {
176 BDRVQcowState *s = bs->opaque;
177 uint8_t keybuf[16];
178 int len, i;
179
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;
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 */
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)
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;
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;
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 <
232 * cluster_size
233 *
234 * return 0 if not allocated.
235 */
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)
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;
246
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;
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);
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;
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]);
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;
345 }
346 }
347 }
348 } else if (allocate == 2) {
349 cluster_offset |= QCOW_OFLAG_COMPRESSED |
350 (uint64_t)compressed_size << (63 - s->cluster_bits);
351 }
352 }
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;
359 }
360 return cluster_offset;
361 }
362
363 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
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
379 static 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 }
405
406 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
407 {
408 BDRVQcowState *s = bs->opaque;
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);
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;
422 }
423 s->cluster_cache_offset = coffset;
424 }
425 return 0;
426 }
427
428 static int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
429 int nb_sectors, QEMUIOVector *qiov)
430 {
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;
439
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;
445 }
446
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 }
458
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;
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;
497 }
498 if (s->crypt_method) {
499 encrypt_sectors(s, sector_num, buf, buf,
500 n, 0,
501 &s->aes_decrypt_key);
502 }
503 }
504 ret = 0;
505
506 nb_sectors -= n;
507 sector_num += n;
508 buf += n * 512;
509 }
510
511 done:
512 qemu_co_mutex_unlock(&s->lock);
513
514 if (qiov->niov > 1) {
515 qemu_iovec_from_buffer(qiov, orig_buf, qiov->size);
516 qemu_vfree(orig_buf);
517 }
518
519 return ret;
520
521 fail:
522 ret = -EIO;
523 goto done;
524 }
525
526 static int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
527 int nb_sectors, QEMUIOVector *qiov)
528 {
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;
539
540 s->cluster_cache_offset = -1; /* disable compressed cache */
541
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;
548 }
549
550 qemu_co_mutex_lock(&s->lock);
551
552 while (nb_sectors != 0) {
553
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 }
576
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;
589
590 nb_sectors -= n;
591 sector_num += n;
592 buf += n * 512;
593 }
594 qemu_co_mutex_unlock(&s->lock);
595
596 if (qiov->niov > 1) {
597 qemu_vfree(orig_buf);
598 }
599 g_free(cluster_data);
600
601 return ret;
602 }
603
604 static void qcow_close(BlockDriverState *bs)
605 {
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);
611 }
612
613 static int qcow_create(const char *filename, QEMUOptionParameter *options)
614 {
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;
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 }
634
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;
653 }
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);
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);
670 }
671
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;
677 }
678
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;
684 }
685
686 }
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;
694 }
695 }
696
697 ret = 0;
698 exit:
699 close(fd);
700 return ret;
701 }
702
703 static int qcow_make_empty(BlockDriverState *bs)
704 {
705 BDRVQcowState *s = bs->opaque;
706 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
707 int ret;
708
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;
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
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)
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
735 if (nb_sectors != s->cluster_sectors)
736 return -EINVAL;
737
738 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
739 if (!out_buf)
740 return -1;
741
742 /* best compression, small window, no zlib header */
743 memset(&strm, 0, sizeof(strm));
744 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
745 Z_DEFLATED, -12,
746 9, Z_DEFAULT_STRATEGY);
747 if (ret != 0) {
748 g_free(out_buf);
749 return -1;
750 }
751
752 strm.avail_in = s->cluster_size;
753 strm.next_in = (uint8_t *)buf;
754 strm.avail_out = s->cluster_size;
755 strm.next_out = out_buf;
756
757 ret = deflate(&strm, Z_FINISH);
758 if (ret != Z_STREAM_END && ret != Z_OK) {
759 g_free(out_buf);
760 deflateEnd(&strm);
761 return -1;
762 }
763 out_len = strm.next_out - out_buf;
764
765 deflateEnd(&strm);
766
767 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
768 /* could not compress: write normal cluster */
769 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
770 } else {
771 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
772 out_len, 0, 0);
773 cluster_offset &= s->cluster_offset_mask;
774 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
775 g_free(out_buf);
776 return -1;
777 }
778 }
779
780 g_free(out_buf);
781 return 0;
782 }
783
784 static int qcow_flush(BlockDriverState *bs)
785 {
786 return bdrv_flush(bs->file);
787 }
788
789 static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
790 BlockDriverCompletionFunc *cb, void *opaque)
791 {
792 return bdrv_aio_flush(bs->file, cb, opaque);
793 }
794
795 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
796 {
797 BDRVQcowState *s = bs->opaque;
798 bdi->cluster_size = s->cluster_size;
799 return 0;
800 }
801
802
803 static QEMUOptionParameter qcow_create_options[] = {
804 {
805 .name = BLOCK_OPT_SIZE,
806 .type = OPT_SIZE,
807 .help = "Virtual disk size"
808 },
809 {
810 .name = BLOCK_OPT_BACKING_FILE,
811 .type = OPT_STRING,
812 .help = "File name of a base image"
813 },
814 {
815 .name = BLOCK_OPT_ENCRYPT,
816 .type = OPT_FLAG,
817 .help = "Encrypt the image"
818 },
819 { NULL }
820 };
821
822 static BlockDriver bdrv_qcow = {
823 .format_name = "qcow",
824 .instance_size = sizeof(BDRVQcowState),
825 .bdrv_probe = qcow_probe,
826 .bdrv_open = qcow_open,
827 .bdrv_close = qcow_close,
828 .bdrv_create = qcow_create,
829 .bdrv_flush = qcow_flush,
830 .bdrv_is_allocated = qcow_is_allocated,
831 .bdrv_set_key = qcow_set_key,
832 .bdrv_make_empty = qcow_make_empty,
833 .bdrv_co_readv = qcow_co_readv,
834 .bdrv_co_writev = qcow_co_writev,
835 .bdrv_aio_flush = qcow_aio_flush,
836 .bdrv_write_compressed = qcow_write_compressed,
837 .bdrv_get_info = qcow_get_info,
838
839 .create_options = qcow_create_options,
840 };
841
842 static void bdrv_qcow_init(void)
843 {
844 bdrv_register(&bdrv_qcow);
845 }
846
847 block_init(bdrv_qcow_init);