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