]> git.proxmox.com Git - mirror_qemu.git/blob - block/qcow.c
qcow/qcow2: Allocate QCowAIOCB structure using stack
[mirror_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 #if 0
196 /* test */
197 {
198 uint8_t in[16];
199 uint8_t out[16];
200 uint8_t tmp[16];
201 for(i=0;i<16;i++)
202 in[i] = i;
203 AES_encrypt(in, tmp, &s->aes_encrypt_key);
204 AES_decrypt(tmp, out, &s->aes_decrypt_key);
205 for(i = 0; i < 16; i++)
206 printf(" %02x", tmp[i]);
207 printf("\n");
208 for(i = 0; i < 16; i++)
209 printf(" %02x", out[i]);
210 printf("\n");
211 }
212 #endif
213 return 0;
214 }
215
216 /* The crypt function is compatible with the linux cryptoloop
217 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
218 supported */
219 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
220 uint8_t *out_buf, const uint8_t *in_buf,
221 int nb_sectors, int enc,
222 const AES_KEY *key)
223 {
224 union {
225 uint64_t ll[2];
226 uint8_t b[16];
227 } ivec;
228 int i;
229
230 for(i = 0; i < nb_sectors; i++) {
231 ivec.ll[0] = cpu_to_le64(sector_num);
232 ivec.ll[1] = 0;
233 AES_cbc_encrypt(in_buf, out_buf, 512, key,
234 ivec.b, enc);
235 sector_num++;
236 in_buf += 512;
237 out_buf += 512;
238 }
239 }
240
241 /* 'allocate' is:
242 *
243 * 0 to not allocate.
244 *
245 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
246 * 'n_end')
247 *
248 * 2 to allocate a compressed cluster of size
249 * 'compressed_size'. 'compressed_size' must be > 0 and <
250 * cluster_size
251 *
252 * return 0 if not allocated.
253 */
254 static uint64_t get_cluster_offset(BlockDriverState *bs,
255 uint64_t offset, int allocate,
256 int compressed_size,
257 int n_start, int n_end)
258 {
259 BDRVQcowState *s = bs->opaque;
260 int min_index, i, j, l1_index, l2_index;
261 uint64_t l2_offset, *l2_table, cluster_offset, tmp;
262 uint32_t min_count;
263 int new_l2_table;
264
265 l1_index = offset >> (s->l2_bits + s->cluster_bits);
266 l2_offset = s->l1_table[l1_index];
267 new_l2_table = 0;
268 if (!l2_offset) {
269 if (!allocate)
270 return 0;
271 /* allocate a new l2 entry */
272 l2_offset = bdrv_getlength(bs->file);
273 /* round to cluster size */
274 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
275 /* update the L1 entry */
276 s->l1_table[l1_index] = l2_offset;
277 tmp = cpu_to_be64(l2_offset);
278 if (bdrv_pwrite_sync(bs->file,
279 s->l1_table_offset + l1_index * sizeof(tmp),
280 &tmp, sizeof(tmp)) < 0)
281 return 0;
282 new_l2_table = 1;
283 }
284 for(i = 0; i < L2_CACHE_SIZE; i++) {
285 if (l2_offset == s->l2_cache_offsets[i]) {
286 /* increment the hit count */
287 if (++s->l2_cache_counts[i] == 0xffffffff) {
288 for(j = 0; j < L2_CACHE_SIZE; j++) {
289 s->l2_cache_counts[j] >>= 1;
290 }
291 }
292 l2_table = s->l2_cache + (i << s->l2_bits);
293 goto found;
294 }
295 }
296 /* not found: load a new entry in the least used one */
297 min_index = 0;
298 min_count = 0xffffffff;
299 for(i = 0; i < L2_CACHE_SIZE; i++) {
300 if (s->l2_cache_counts[i] < min_count) {
301 min_count = s->l2_cache_counts[i];
302 min_index = i;
303 }
304 }
305 l2_table = s->l2_cache + (min_index << s->l2_bits);
306 if (new_l2_table) {
307 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
308 if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
309 s->l2_size * sizeof(uint64_t)) < 0)
310 return 0;
311 } else {
312 if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
313 s->l2_size * sizeof(uint64_t))
314 return 0;
315 }
316 s->l2_cache_offsets[min_index] = l2_offset;
317 s->l2_cache_counts[min_index] = 1;
318 found:
319 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
320 cluster_offset = be64_to_cpu(l2_table[l2_index]);
321 if (!cluster_offset ||
322 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
323 if (!allocate)
324 return 0;
325 /* allocate a new cluster */
326 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
327 (n_end - n_start) < s->cluster_sectors) {
328 /* if the cluster is already compressed, we must
329 decompress it in the case it is not completely
330 overwritten */
331 if (decompress_cluster(bs, cluster_offset) < 0)
332 return 0;
333 cluster_offset = bdrv_getlength(bs->file);
334 cluster_offset = (cluster_offset + s->cluster_size - 1) &
335 ~(s->cluster_size - 1);
336 /* write the cluster content */
337 if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
338 s->cluster_size)
339 return -1;
340 } else {
341 cluster_offset = bdrv_getlength(bs->file);
342 if (allocate == 1) {
343 /* round to cluster size */
344 cluster_offset = (cluster_offset + s->cluster_size - 1) &
345 ~(s->cluster_size - 1);
346 bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
347 /* if encrypted, we must initialize the cluster
348 content which won't be written */
349 if (s->crypt_method &&
350 (n_end - n_start) < s->cluster_sectors) {
351 uint64_t start_sect;
352 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
353 memset(s->cluster_data + 512, 0x00, 512);
354 for(i = 0; i < s->cluster_sectors; i++) {
355 if (i < n_start || i >= n_end) {
356 encrypt_sectors(s, start_sect + i,
357 s->cluster_data,
358 s->cluster_data + 512, 1, 1,
359 &s->aes_encrypt_key);
360 if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
361 s->cluster_data, 512) != 512)
362 return -1;
363 }
364 }
365 }
366 } else if (allocate == 2) {
367 cluster_offset |= QCOW_OFLAG_COMPRESSED |
368 (uint64_t)compressed_size << (63 - s->cluster_bits);
369 }
370 }
371 /* update L2 table */
372 tmp = cpu_to_be64(cluster_offset);
373 l2_table[l2_index] = tmp;
374 if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
375 &tmp, sizeof(tmp)) < 0)
376 return 0;
377 }
378 return cluster_offset;
379 }
380
381 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
382 int nb_sectors, int *pnum)
383 {
384 BDRVQcowState *s = bs->opaque;
385 int index_in_cluster, n;
386 uint64_t cluster_offset;
387
388 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
389 index_in_cluster = sector_num & (s->cluster_sectors - 1);
390 n = s->cluster_sectors - index_in_cluster;
391 if (n > nb_sectors)
392 n = nb_sectors;
393 *pnum = n;
394 return (cluster_offset != 0);
395 }
396
397 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
398 const uint8_t *buf, int buf_size)
399 {
400 z_stream strm1, *strm = &strm1;
401 int ret, out_len;
402
403 memset(strm, 0, sizeof(*strm));
404
405 strm->next_in = (uint8_t *)buf;
406 strm->avail_in = buf_size;
407 strm->next_out = out_buf;
408 strm->avail_out = out_buf_size;
409
410 ret = inflateInit2(strm, -12);
411 if (ret != Z_OK)
412 return -1;
413 ret = inflate(strm, Z_FINISH);
414 out_len = strm->next_out - out_buf;
415 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
416 out_len != out_buf_size) {
417 inflateEnd(strm);
418 return -1;
419 }
420 inflateEnd(strm);
421 return 0;
422 }
423
424 static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
425 {
426 BDRVQcowState *s = bs->opaque;
427 int ret, csize;
428 uint64_t coffset;
429
430 coffset = cluster_offset & s->cluster_offset_mask;
431 if (s->cluster_cache_offset != coffset) {
432 csize = cluster_offset >> (63 - s->cluster_bits);
433 csize &= (s->cluster_size - 1);
434 ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
435 if (ret != csize)
436 return -1;
437 if (decompress_buffer(s->cluster_cache, s->cluster_size,
438 s->cluster_data, csize) < 0) {
439 return -1;
440 }
441 s->cluster_cache_offset = coffset;
442 }
443 return 0;
444 }
445
446 #if 0
447
448 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
449 uint8_t *buf, int nb_sectors)
450 {
451 BDRVQcowState *s = bs->opaque;
452 int ret, index_in_cluster, n;
453 uint64_t cluster_offset;
454
455 while (nb_sectors > 0) {
456 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
457 index_in_cluster = sector_num & (s->cluster_sectors - 1);
458 n = s->cluster_sectors - index_in_cluster;
459 if (n > nb_sectors)
460 n = nb_sectors;
461 if (!cluster_offset) {
462 if (bs->backing_hd) {
463 /* read from the base image */
464 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
465 if (ret < 0)
466 return -1;
467 } else {
468 memset(buf, 0, 512 * n);
469 }
470 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
471 if (decompress_cluster(bs, cluster_offset) < 0)
472 return -1;
473 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
474 } else {
475 ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
476 if (ret != n * 512)
477 return -1;
478 if (s->crypt_method) {
479 encrypt_sectors(s, sector_num, buf, buf, n, 0,
480 &s->aes_decrypt_key);
481 }
482 }
483 nb_sectors -= n;
484 sector_num += n;
485 buf += n * 512;
486 }
487 return 0;
488 }
489 #endif
490
491 typedef struct QCowAIOCB {
492 BlockDriverAIOCB common;
493 int64_t sector_num;
494 QEMUIOVector *qiov;
495 uint8_t *buf;
496 void *orig_buf;
497 int nb_sectors;
498 int n;
499 uint64_t cluster_offset;
500 uint8_t *cluster_data;
501 struct iovec hd_iov;
502 bool is_write;
503 QEMUBH *bh;
504 QEMUIOVector hd_qiov;
505 BlockDriverAIOCB *hd_aiocb;
506 } QCowAIOCB;
507
508 static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
509 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
510 int is_write, QCowAIOCB *acb)
511 {
512 memset(acb, 0, sizeof(*acb));
513 acb->common.bs = bs;
514 acb->hd_aiocb = NULL;
515 acb->sector_num = sector_num;
516 acb->qiov = qiov;
517 acb->is_write = is_write;
518
519 if (qiov->niov > 1) {
520 acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
521 if (is_write)
522 qemu_iovec_to_buffer(qiov, acb->buf);
523 } else {
524 acb->buf = (uint8_t *)qiov->iov->iov_base;
525 }
526 acb->nb_sectors = nb_sectors;
527 acb->n = 0;
528 acb->cluster_offset = 0;
529 return acb;
530 }
531
532 static int qcow_aio_read_cb(QCowAIOCB *acb)
533 {
534 BlockDriverState *bs = acb->common.bs;
535 BDRVQcowState *s = bs->opaque;
536 int index_in_cluster;
537 int ret;
538
539 acb->hd_aiocb = NULL;
540
541 redo:
542 /* post process the read buffer */
543 if (!acb->cluster_offset) {
544 /* nothing to do */
545 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
546 /* nothing to do */
547 } else {
548 if (s->crypt_method) {
549 encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
550 acb->n, 0,
551 &s->aes_decrypt_key);
552 }
553 }
554
555 acb->nb_sectors -= acb->n;
556 acb->sector_num += acb->n;
557 acb->buf += acb->n * 512;
558
559 if (acb->nb_sectors == 0) {
560 /* request completed */
561 return 0;
562 }
563
564 /* prepare next AIO request */
565 acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
566 0, 0, 0, 0);
567 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
568 acb->n = s->cluster_sectors - index_in_cluster;
569 if (acb->n > acb->nb_sectors)
570 acb->n = acb->nb_sectors;
571
572 if (!acb->cluster_offset) {
573 if (bs->backing_hd) {
574 /* read from the base image */
575 acb->hd_iov.iov_base = (void *)acb->buf;
576 acb->hd_iov.iov_len = acb->n * 512;
577 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
578 qemu_co_mutex_unlock(&s->lock);
579 ret = bdrv_co_readv(bs->backing_hd, acb->sector_num,
580 acb->n, &acb->hd_qiov);
581 qemu_co_mutex_lock(&s->lock);
582 if (ret < 0) {
583 return -EIO;
584 }
585 } else {
586 /* Note: in this case, no need to wait */
587 memset(acb->buf, 0, 512 * acb->n);
588 goto redo;
589 }
590 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
591 /* add AIO support for compressed blocks ? */
592 if (decompress_cluster(bs, acb->cluster_offset) < 0) {
593 return -EIO;
594 }
595 memcpy(acb->buf,
596 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
597 goto redo;
598 } else {
599 if ((acb->cluster_offset & 511) != 0) {
600 return -EIO;
601 }
602 acb->hd_iov.iov_base = (void *)acb->buf;
603 acb->hd_iov.iov_len = acb->n * 512;
604 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
605 qemu_co_mutex_unlock(&s->lock);
606 ret = bdrv_co_readv(bs->file,
607 (acb->cluster_offset >> 9) + index_in_cluster,
608 acb->n, &acb->hd_qiov);
609 qemu_co_mutex_lock(&s->lock);
610 if (ret < 0) {
611 return ret;
612 }
613 }
614
615 return 1;
616 }
617
618 static int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
619 int nb_sectors, QEMUIOVector *qiov)
620 {
621 BDRVQcowState *s = bs->opaque;
622 QCowAIOCB acb;
623 int ret;
624
625 qcow_aio_setup(bs, sector_num, qiov, nb_sectors, 0, &acb);
626
627 qemu_co_mutex_lock(&s->lock);
628 do {
629 ret = qcow_aio_read_cb(&acb);
630 } while (ret > 0);
631 qemu_co_mutex_unlock(&s->lock);
632
633 if (acb.qiov->niov > 1) {
634 qemu_iovec_from_buffer(acb.qiov, acb.orig_buf, acb.qiov->size);
635 qemu_vfree(acb.orig_buf);
636 }
637
638 return ret;
639 }
640
641 static int qcow_aio_write_cb(QCowAIOCB *acb)
642 {
643 BlockDriverState *bs = acb->common.bs;
644 BDRVQcowState *s = bs->opaque;
645 int index_in_cluster;
646 uint64_t cluster_offset;
647 const uint8_t *src_buf;
648 int ret;
649
650 acb->hd_aiocb = NULL;
651
652 acb->nb_sectors -= acb->n;
653 acb->sector_num += acb->n;
654 acb->buf += acb->n * 512;
655
656 if (acb->nb_sectors == 0) {
657 /* request completed */
658 return 0;
659 }
660
661 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
662 acb->n = s->cluster_sectors - index_in_cluster;
663 if (acb->n > acb->nb_sectors)
664 acb->n = acb->nb_sectors;
665 cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
666 index_in_cluster,
667 index_in_cluster + acb->n);
668 if (!cluster_offset || (cluster_offset & 511) != 0) {
669 return -EIO;
670 }
671 if (s->crypt_method) {
672 if (!acb->cluster_data) {
673 acb->cluster_data = g_malloc0(s->cluster_size);
674 }
675 encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
676 acb->n, 1, &s->aes_encrypt_key);
677 src_buf = acb->cluster_data;
678 } else {
679 src_buf = acb->buf;
680 }
681
682 acb->hd_iov.iov_base = (void *)src_buf;
683 acb->hd_iov.iov_len = acb->n * 512;
684 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
685 qemu_co_mutex_unlock(&s->lock);
686 ret = bdrv_co_writev(bs->file,
687 (cluster_offset >> 9) + index_in_cluster,
688 acb->n, &acb->hd_qiov);
689 qemu_co_mutex_lock(&s->lock);
690 if (ret < 0) {
691 return ret;
692 }
693 return 1;
694 }
695
696 static int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
697 int nb_sectors, QEMUIOVector *qiov)
698 {
699 BDRVQcowState *s = bs->opaque;
700 QCowAIOCB acb;
701 int ret;
702
703 s->cluster_cache_offset = -1; /* disable compressed cache */
704
705 qcow_aio_setup(bs, sector_num, qiov, nb_sectors, 1, &acb);
706
707 qemu_co_mutex_lock(&s->lock);
708 do {
709 ret = qcow_aio_write_cb(&acb);
710 } while (ret > 0);
711 qemu_co_mutex_unlock(&s->lock);
712
713 if (acb.qiov->niov > 1) {
714 qemu_vfree(acb.orig_buf);
715 }
716
717 return ret;
718 }
719
720 static void qcow_close(BlockDriverState *bs)
721 {
722 BDRVQcowState *s = bs->opaque;
723 g_free(s->l1_table);
724 g_free(s->l2_cache);
725 g_free(s->cluster_cache);
726 g_free(s->cluster_data);
727 }
728
729 static int qcow_create(const char *filename, QEMUOptionParameter *options)
730 {
731 int fd, header_size, backing_filename_len, l1_size, i, shift;
732 QCowHeader header;
733 uint64_t tmp;
734 int64_t total_size = 0;
735 const char *backing_file = NULL;
736 int flags = 0;
737 int ret;
738
739 /* Read out options */
740 while (options && options->name) {
741 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
742 total_size = options->value.n / 512;
743 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
744 backing_file = options->value.s;
745 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
746 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
747 }
748 options++;
749 }
750
751 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
752 if (fd < 0)
753 return -errno;
754 memset(&header, 0, sizeof(header));
755 header.magic = cpu_to_be32(QCOW_MAGIC);
756 header.version = cpu_to_be32(QCOW_VERSION);
757 header.size = cpu_to_be64(total_size * 512);
758 header_size = sizeof(header);
759 backing_filename_len = 0;
760 if (backing_file) {
761 if (strcmp(backing_file, "fat:")) {
762 header.backing_file_offset = cpu_to_be64(header_size);
763 backing_filename_len = strlen(backing_file);
764 header.backing_file_size = cpu_to_be32(backing_filename_len);
765 header_size += backing_filename_len;
766 } else {
767 /* special backing file for vvfat */
768 backing_file = NULL;
769 }
770 header.cluster_bits = 9; /* 512 byte cluster to avoid copying
771 unmodifyed sectors */
772 header.l2_bits = 12; /* 32 KB L2 tables */
773 } else {
774 header.cluster_bits = 12; /* 4 KB clusters */
775 header.l2_bits = 9; /* 4 KB L2 tables */
776 }
777 header_size = (header_size + 7) & ~7;
778 shift = header.cluster_bits + header.l2_bits;
779 l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
780
781 header.l1_table_offset = cpu_to_be64(header_size);
782 if (flags & BLOCK_FLAG_ENCRYPT) {
783 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
784 } else {
785 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
786 }
787
788 /* write all the data */
789 ret = qemu_write_full(fd, &header, sizeof(header));
790 if (ret != sizeof(header)) {
791 ret = -errno;
792 goto exit;
793 }
794
795 if (backing_file) {
796 ret = qemu_write_full(fd, backing_file, backing_filename_len);
797 if (ret != backing_filename_len) {
798 ret = -errno;
799 goto exit;
800 }
801
802 }
803 lseek(fd, header_size, SEEK_SET);
804 tmp = 0;
805 for(i = 0;i < l1_size; i++) {
806 ret = qemu_write_full(fd, &tmp, sizeof(tmp));
807 if (ret != sizeof(tmp)) {
808 ret = -errno;
809 goto exit;
810 }
811 }
812
813 ret = 0;
814 exit:
815 close(fd);
816 return ret;
817 }
818
819 static int qcow_make_empty(BlockDriverState *bs)
820 {
821 BDRVQcowState *s = bs->opaque;
822 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
823 int ret;
824
825 memset(s->l1_table, 0, l1_length);
826 if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
827 l1_length) < 0)
828 return -1;
829 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
830 if (ret < 0)
831 return ret;
832
833 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
834 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
835 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
836
837 return 0;
838 }
839
840 /* XXX: put compressed sectors first, then all the cluster aligned
841 tables to avoid losing bytes in alignment */
842 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
843 const uint8_t *buf, int nb_sectors)
844 {
845 BDRVQcowState *s = bs->opaque;
846 z_stream strm;
847 int ret, out_len;
848 uint8_t *out_buf;
849 uint64_t cluster_offset;
850
851 if (nb_sectors != s->cluster_sectors)
852 return -EINVAL;
853
854 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
855 if (!out_buf)
856 return -1;
857
858 /* best compression, small window, no zlib header */
859 memset(&strm, 0, sizeof(strm));
860 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
861 Z_DEFLATED, -12,
862 9, Z_DEFAULT_STRATEGY);
863 if (ret != 0) {
864 g_free(out_buf);
865 return -1;
866 }
867
868 strm.avail_in = s->cluster_size;
869 strm.next_in = (uint8_t *)buf;
870 strm.avail_out = s->cluster_size;
871 strm.next_out = out_buf;
872
873 ret = deflate(&strm, Z_FINISH);
874 if (ret != Z_STREAM_END && ret != Z_OK) {
875 g_free(out_buf);
876 deflateEnd(&strm);
877 return -1;
878 }
879 out_len = strm.next_out - out_buf;
880
881 deflateEnd(&strm);
882
883 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
884 /* could not compress: write normal cluster */
885 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
886 } else {
887 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
888 out_len, 0, 0);
889 cluster_offset &= s->cluster_offset_mask;
890 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
891 g_free(out_buf);
892 return -1;
893 }
894 }
895
896 g_free(out_buf);
897 return 0;
898 }
899
900 static int qcow_flush(BlockDriverState *bs)
901 {
902 return bdrv_flush(bs->file);
903 }
904
905 static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
906 BlockDriverCompletionFunc *cb, void *opaque)
907 {
908 return bdrv_aio_flush(bs->file, cb, opaque);
909 }
910
911 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
912 {
913 BDRVQcowState *s = bs->opaque;
914 bdi->cluster_size = s->cluster_size;
915 return 0;
916 }
917
918
919 static QEMUOptionParameter qcow_create_options[] = {
920 {
921 .name = BLOCK_OPT_SIZE,
922 .type = OPT_SIZE,
923 .help = "Virtual disk size"
924 },
925 {
926 .name = BLOCK_OPT_BACKING_FILE,
927 .type = OPT_STRING,
928 .help = "File name of a base image"
929 },
930 {
931 .name = BLOCK_OPT_ENCRYPT,
932 .type = OPT_FLAG,
933 .help = "Encrypt the image"
934 },
935 { NULL }
936 };
937
938 static BlockDriver bdrv_qcow = {
939 .format_name = "qcow",
940 .instance_size = sizeof(BDRVQcowState),
941 .bdrv_probe = qcow_probe,
942 .bdrv_open = qcow_open,
943 .bdrv_close = qcow_close,
944 .bdrv_create = qcow_create,
945 .bdrv_flush = qcow_flush,
946 .bdrv_is_allocated = qcow_is_allocated,
947 .bdrv_set_key = qcow_set_key,
948 .bdrv_make_empty = qcow_make_empty,
949 .bdrv_co_readv = qcow_co_readv,
950 .bdrv_co_writev = qcow_co_writev,
951 .bdrv_aio_flush = qcow_aio_flush,
952 .bdrv_write_compressed = qcow_write_compressed,
953 .bdrv_get_info = qcow_get_info,
954
955 .create_options = qcow_create_options,
956 };
957
958 static void bdrv_qcow_init(void)
959 {
960 bdrv_register(&bdrv_qcow);
961 }
962
963 block_init(bdrv_qcow_init);