]> git.proxmox.com Git - qemu.git/blob - block/qcow2.c
551b3c2f82ddfe31dbd013772599552c83f231c4
[qemu.git] / block / qcow2.c
1 /*
2 * Block driver for the QCOW version 2 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 #include "block/qcow2.h"
30 #include "qemu-error.h"
31
32 /*
33 Differences with QCOW:
34
35 - Support for multiple incremental snapshots.
36 - Memory management by reference counts.
37 - Clusters which have a reference count of one have the bit
38 QCOW_OFLAG_COPIED to optimize write performance.
39 - Size of compressed clusters is stored in sectors to reduce bit usage
40 in the cluster offsets.
41 - Support for storing additional data (such as the VM state) in the
42 snapshots.
43 - If a backing store is used, the cluster size is not constrained
44 (could be backported to QCOW).
45 - L2 tables have always a size of one cluster.
46 */
47
48
49 typedef struct {
50 uint32_t magic;
51 uint32_t len;
52 } QCowExtension;
53 #define QCOW2_EXT_MAGIC_END 0
54 #define QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
55
56 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
57 {
58 const QCowHeader *cow_header = (const void *)buf;
59
60 if (buf_size >= sizeof(QCowHeader) &&
61 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
62 be32_to_cpu(cow_header->version) == QCOW_VERSION)
63 return 100;
64 else
65 return 0;
66 }
67
68
69 /*
70 * read qcow2 extension and fill bs
71 * start reading from start_offset
72 * finish reading upon magic of value 0 or when end_offset reached
73 * unknown magic is skipped (future extension this version knows nothing about)
74 * return 0 upon success, non-0 otherwise
75 */
76 static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
77 uint64_t end_offset)
78 {
79 QCowExtension ext;
80 uint64_t offset;
81
82 #ifdef DEBUG_EXT
83 printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
84 #endif
85 offset = start_offset;
86 while (offset < end_offset) {
87
88 #ifdef DEBUG_EXT
89 /* Sanity check */
90 if (offset > s->cluster_size)
91 printf("qcow2_read_extension: suspicious offset %lu\n", offset);
92
93 printf("attemting to read extended header in offset %lu\n", offset);
94 #endif
95
96 if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
97 fprintf(stderr, "qcow2_read_extension: ERROR: "
98 "pread fail from offset %" PRIu64 "\n",
99 offset);
100 return 1;
101 }
102 be32_to_cpus(&ext.magic);
103 be32_to_cpus(&ext.len);
104 offset += sizeof(ext);
105 #ifdef DEBUG_EXT
106 printf("ext.magic = 0x%x\n", ext.magic);
107 #endif
108 switch (ext.magic) {
109 case QCOW2_EXT_MAGIC_END:
110 return 0;
111
112 case QCOW2_EXT_MAGIC_BACKING_FORMAT:
113 if (ext.len >= sizeof(bs->backing_format)) {
114 fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
115 " (>=%zu)\n",
116 ext.len, sizeof(bs->backing_format));
117 return 2;
118 }
119 if (bdrv_pread(bs->file, offset , bs->backing_format,
120 ext.len) != ext.len)
121 return 3;
122 bs->backing_format[ext.len] = '\0';
123 #ifdef DEBUG_EXT
124 printf("Qcow2: Got format extension %s\n", bs->backing_format);
125 #endif
126 offset = ((offset + ext.len + 7) & ~7);
127 break;
128
129 default:
130 /* unknown magic -- just skip it */
131 offset = ((offset + ext.len + 7) & ~7);
132 break;
133 }
134 }
135
136 return 0;
137 }
138
139
140 static int qcow2_open(BlockDriverState *bs, int flags)
141 {
142 BDRVQcowState *s = bs->opaque;
143 int len, i, ret = 0;
144 QCowHeader header;
145 uint64_t ext_end;
146 bool writethrough;
147
148 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
149 if (ret < 0) {
150 goto fail;
151 }
152 be32_to_cpus(&header.magic);
153 be32_to_cpus(&header.version);
154 be64_to_cpus(&header.backing_file_offset);
155 be32_to_cpus(&header.backing_file_size);
156 be64_to_cpus(&header.size);
157 be32_to_cpus(&header.cluster_bits);
158 be32_to_cpus(&header.crypt_method);
159 be64_to_cpus(&header.l1_table_offset);
160 be32_to_cpus(&header.l1_size);
161 be64_to_cpus(&header.refcount_table_offset);
162 be32_to_cpus(&header.refcount_table_clusters);
163 be64_to_cpus(&header.snapshots_offset);
164 be32_to_cpus(&header.nb_snapshots);
165
166 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) {
167 ret = -EINVAL;
168 goto fail;
169 }
170 if (header.cluster_bits < MIN_CLUSTER_BITS ||
171 header.cluster_bits > MAX_CLUSTER_BITS) {
172 ret = -EINVAL;
173 goto fail;
174 }
175 if (header.crypt_method > QCOW_CRYPT_AES) {
176 ret = -EINVAL;
177 goto fail;
178 }
179 s->crypt_method_header = header.crypt_method;
180 if (s->crypt_method_header) {
181 bs->encrypted = 1;
182 }
183 s->cluster_bits = header.cluster_bits;
184 s->cluster_size = 1 << s->cluster_bits;
185 s->cluster_sectors = 1 << (s->cluster_bits - 9);
186 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
187 s->l2_size = 1 << s->l2_bits;
188 bs->total_sectors = header.size / 512;
189 s->csize_shift = (62 - (s->cluster_bits - 8));
190 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
191 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
192 s->refcount_table_offset = header.refcount_table_offset;
193 s->refcount_table_size =
194 header.refcount_table_clusters << (s->cluster_bits - 3);
195
196 s->snapshots_offset = header.snapshots_offset;
197 s->nb_snapshots = header.nb_snapshots;
198
199 /* read the level 1 table */
200 s->l1_size = header.l1_size;
201 s->l1_vm_state_index = size_to_l1(s, header.size);
202 /* the L1 table must contain at least enough entries to put
203 header.size bytes */
204 if (s->l1_size < s->l1_vm_state_index) {
205 ret = -EINVAL;
206 goto fail;
207 }
208 s->l1_table_offset = header.l1_table_offset;
209 if (s->l1_size > 0) {
210 s->l1_table = qemu_mallocz(
211 align_offset(s->l1_size * sizeof(uint64_t), 512));
212 ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
213 s->l1_size * sizeof(uint64_t));
214 if (ret < 0) {
215 goto fail;
216 }
217 for(i = 0;i < s->l1_size; i++) {
218 be64_to_cpus(&s->l1_table[i]);
219 }
220 }
221
222 /* alloc L2 table/refcount block cache */
223 writethrough = ((flags & BDRV_O_CACHE_MASK) == 0);
224 s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE, writethrough);
225 s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE,
226 writethrough);
227
228 s->cluster_cache = qemu_malloc(s->cluster_size);
229 /* one more sector for decompressed data alignment */
230 s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
231 + 512);
232 s->cluster_cache_offset = -1;
233
234 ret = qcow2_refcount_init(bs);
235 if (ret != 0) {
236 goto fail;
237 }
238
239 QLIST_INIT(&s->cluster_allocs);
240
241 /* read qcow2 extensions */
242 if (header.backing_file_offset) {
243 ext_end = header.backing_file_offset;
244 } else {
245 ext_end = s->cluster_size;
246 }
247 if (qcow2_read_extensions(bs, sizeof(header), ext_end)) {
248 ret = -EINVAL;
249 goto fail;
250 }
251
252 /* read the backing file name */
253 if (header.backing_file_offset != 0) {
254 len = header.backing_file_size;
255 if (len > 1023) {
256 len = 1023;
257 }
258 ret = bdrv_pread(bs->file, header.backing_file_offset,
259 bs->backing_file, len);
260 if (ret < 0) {
261 goto fail;
262 }
263 bs->backing_file[len] = '\0';
264 }
265 if (qcow2_read_snapshots(bs) < 0) {
266 ret = -EINVAL;
267 goto fail;
268 }
269
270 #ifdef DEBUG_ALLOC
271 qcow2_check_refcounts(bs);
272 #endif
273 return ret;
274
275 fail:
276 qcow2_free_snapshots(bs);
277 qcow2_refcount_close(bs);
278 qemu_free(s->l1_table);
279 if (s->l2_table_cache) {
280 qcow2_cache_destroy(bs, s->l2_table_cache);
281 }
282 qemu_free(s->cluster_cache);
283 qemu_free(s->cluster_data);
284 return ret;
285 }
286
287 static int qcow2_set_key(BlockDriverState *bs, const char *key)
288 {
289 BDRVQcowState *s = bs->opaque;
290 uint8_t keybuf[16];
291 int len, i;
292
293 memset(keybuf, 0, 16);
294 len = strlen(key);
295 if (len > 16)
296 len = 16;
297 /* XXX: we could compress the chars to 7 bits to increase
298 entropy */
299 for(i = 0;i < len;i++) {
300 keybuf[i] = key[i];
301 }
302 s->crypt_method = s->crypt_method_header;
303
304 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
305 return -1;
306 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
307 return -1;
308 #if 0
309 /* test */
310 {
311 uint8_t in[16];
312 uint8_t out[16];
313 uint8_t tmp[16];
314 for(i=0;i<16;i++)
315 in[i] = i;
316 AES_encrypt(in, tmp, &s->aes_encrypt_key);
317 AES_decrypt(tmp, out, &s->aes_decrypt_key);
318 for(i = 0; i < 16; i++)
319 printf(" %02x", tmp[i]);
320 printf("\n");
321 for(i = 0; i < 16; i++)
322 printf(" %02x", out[i]);
323 printf("\n");
324 }
325 #endif
326 return 0;
327 }
328
329 static int qcow2_is_allocated(BlockDriverState *bs, int64_t sector_num,
330 int nb_sectors, int *pnum)
331 {
332 uint64_t cluster_offset;
333 int ret;
334
335 *pnum = nb_sectors;
336 /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
337 * pass them on today */
338 ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
339 if (ret < 0) {
340 *pnum = 0;
341 }
342
343 return (cluster_offset != 0);
344 }
345
346 /* handle reading after the end of the backing file */
347 int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
348 int64_t sector_num, int nb_sectors)
349 {
350 int n1;
351 if ((sector_num + nb_sectors) <= bs->total_sectors)
352 return nb_sectors;
353 if (sector_num >= bs->total_sectors)
354 n1 = 0;
355 else
356 n1 = bs->total_sectors - sector_num;
357
358 qemu_iovec_memset_skip(qiov, 0, 512 * (nb_sectors - n1), 512 * n1);
359
360 return n1;
361 }
362
363 typedef struct QCowAIOCB {
364 BlockDriverAIOCB common;
365 int64_t sector_num;
366 QEMUIOVector *qiov;
367 int remaining_sectors;
368 int cur_nr_sectors; /* number of sectors in current iteration */
369 uint64_t bytes_done;
370 uint64_t cluster_offset;
371 uint8_t *cluster_data;
372 BlockDriverAIOCB *hd_aiocb;
373 QEMUIOVector hd_qiov;
374 QEMUBH *bh;
375 QCowL2Meta l2meta;
376 QLIST_ENTRY(QCowAIOCB) next_depend;
377 } QCowAIOCB;
378
379 static void qcow2_aio_cancel(BlockDriverAIOCB *blockacb)
380 {
381 QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
382 if (acb->hd_aiocb)
383 bdrv_aio_cancel(acb->hd_aiocb);
384 qemu_aio_release(acb);
385 }
386
387 static AIOPool qcow2_aio_pool = {
388 .aiocb_size = sizeof(QCowAIOCB),
389 .cancel = qcow2_aio_cancel,
390 };
391
392 static void qcow2_aio_read_cb(void *opaque, int ret);
393 static void qcow2_aio_read_bh(void *opaque)
394 {
395 QCowAIOCB *acb = opaque;
396 qemu_bh_delete(acb->bh);
397 acb->bh = NULL;
398 qcow2_aio_read_cb(opaque, 0);
399 }
400
401 static int qcow2_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
402 {
403 if (acb->bh)
404 return -EIO;
405
406 acb->bh = qemu_bh_new(cb, acb);
407 if (!acb->bh)
408 return -EIO;
409
410 qemu_bh_schedule(acb->bh);
411
412 return 0;
413 }
414
415 static void qcow2_aio_read_cb(void *opaque, int ret)
416 {
417 QCowAIOCB *acb = opaque;
418 BlockDriverState *bs = acb->common.bs;
419 BDRVQcowState *s = bs->opaque;
420 int index_in_cluster, n1;
421
422 acb->hd_aiocb = NULL;
423 if (ret < 0)
424 goto done;
425
426 /* post process the read buffer */
427 if (!acb->cluster_offset) {
428 /* nothing to do */
429 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
430 /* nothing to do */
431 } else {
432 if (s->crypt_method) {
433 qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
434 acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key);
435 qemu_iovec_reset(&acb->hd_qiov);
436 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
437 acb->cur_nr_sectors * 512);
438 qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data,
439 512 * acb->cur_nr_sectors);
440 }
441 }
442
443 acb->remaining_sectors -= acb->cur_nr_sectors;
444 acb->sector_num += acb->cur_nr_sectors;
445 acb->bytes_done += acb->cur_nr_sectors * 512;
446
447 if (acb->remaining_sectors == 0) {
448 /* request completed */
449 ret = 0;
450 goto done;
451 }
452
453 /* prepare next AIO request */
454 acb->cur_nr_sectors = acb->remaining_sectors;
455 if (s->crypt_method) {
456 acb->cur_nr_sectors = MIN(acb->cur_nr_sectors,
457 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
458 }
459
460 ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9,
461 &acb->cur_nr_sectors, &acb->cluster_offset);
462 if (ret < 0) {
463 goto done;
464 }
465
466 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
467
468 qemu_iovec_reset(&acb->hd_qiov);
469 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
470 acb->cur_nr_sectors * 512);
471
472 if (!acb->cluster_offset) {
473
474 if (bs->backing_hd) {
475 /* read from the base image */
476 n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov,
477 acb->sector_num, acb->cur_nr_sectors);
478 if (n1 > 0) {
479 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
480 acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
481 &acb->hd_qiov, n1, qcow2_aio_read_cb, acb);
482 if (acb->hd_aiocb == NULL) {
483 ret = -EIO;
484 goto done;
485 }
486 } else {
487 ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
488 if (ret < 0)
489 goto done;
490 }
491 } else {
492 /* Note: in this case, no need to wait */
493 qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors);
494 ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
495 if (ret < 0)
496 goto done;
497 }
498 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
499 /* add AIO support for compressed blocks ? */
500 ret = qcow2_decompress_cluster(bs, acb->cluster_offset);
501 if (ret < 0) {
502 goto done;
503 }
504
505 qemu_iovec_from_buffer(&acb->hd_qiov,
506 s->cluster_cache + index_in_cluster * 512,
507 512 * acb->cur_nr_sectors);
508
509 ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
510 if (ret < 0)
511 goto done;
512 } else {
513 if ((acb->cluster_offset & 511) != 0) {
514 ret = -EIO;
515 goto done;
516 }
517
518 if (s->crypt_method) {
519 /*
520 * For encrypted images, read everything into a temporary
521 * contiguous buffer on which the AES functions can work.
522 */
523 if (!acb->cluster_data) {
524 acb->cluster_data =
525 qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
526 }
527
528 assert(acb->cur_nr_sectors <=
529 QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
530 qemu_iovec_reset(&acb->hd_qiov);
531 qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
532 512 * acb->cur_nr_sectors);
533 }
534
535 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
536 acb->hd_aiocb = bdrv_aio_readv(bs->file,
537 (acb->cluster_offset >> 9) + index_in_cluster,
538 &acb->hd_qiov, acb->cur_nr_sectors,
539 qcow2_aio_read_cb, acb);
540 if (acb->hd_aiocb == NULL) {
541 ret = -EIO;
542 goto done;
543 }
544 }
545
546 return;
547 done:
548 acb->common.cb(acb->common.opaque, ret);
549 qemu_iovec_destroy(&acb->hd_qiov);
550 qemu_aio_release(acb);
551 }
552
553 static QCowAIOCB *qcow2_aio_setup(BlockDriverState *bs, int64_t sector_num,
554 QEMUIOVector *qiov, int nb_sectors,
555 BlockDriverCompletionFunc *cb,
556 void *opaque, int is_write)
557 {
558 QCowAIOCB *acb;
559
560 acb = qemu_aio_get(&qcow2_aio_pool, bs, cb, opaque);
561 if (!acb)
562 return NULL;
563 acb->hd_aiocb = NULL;
564 acb->sector_num = sector_num;
565 acb->qiov = qiov;
566
567 qemu_iovec_init(&acb->hd_qiov, qiov->niov);
568
569 acb->bytes_done = 0;
570 acb->remaining_sectors = nb_sectors;
571 acb->cur_nr_sectors = 0;
572 acb->cluster_offset = 0;
573 acb->l2meta.nb_clusters = 0;
574 QLIST_INIT(&acb->l2meta.dependent_requests);
575 return acb;
576 }
577
578 static BlockDriverAIOCB *qcow2_aio_readv(BlockDriverState *bs,
579 int64_t sector_num,
580 QEMUIOVector *qiov, int nb_sectors,
581 BlockDriverCompletionFunc *cb,
582 void *opaque)
583 {
584 QCowAIOCB *acb;
585
586 acb = qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
587 if (!acb)
588 return NULL;
589
590 qcow2_aio_read_cb(acb, 0);
591 return &acb->common;
592 }
593
594 static void qcow2_aio_write_cb(void *opaque, int ret);
595
596 static void run_dependent_requests(QCowL2Meta *m)
597 {
598 QCowAIOCB *req;
599 QCowAIOCB *next;
600
601 /* Take the request off the list of running requests */
602 if (m->nb_clusters != 0) {
603 QLIST_REMOVE(m, next_in_flight);
604 }
605
606 /* Restart all dependent requests */
607 QLIST_FOREACH_SAFE(req, &m->dependent_requests, next_depend, next) {
608 qcow2_aio_write_cb(req, 0);
609 }
610
611 /* Empty the list for the next part of the request */
612 QLIST_INIT(&m->dependent_requests);
613 }
614
615 static void qcow2_aio_write_cb(void *opaque, int ret)
616 {
617 QCowAIOCB *acb = opaque;
618 BlockDriverState *bs = acb->common.bs;
619 BDRVQcowState *s = bs->opaque;
620 int index_in_cluster;
621 int n_end;
622
623 acb->hd_aiocb = NULL;
624
625 if (ret >= 0) {
626 ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta);
627 }
628
629 run_dependent_requests(&acb->l2meta);
630
631 if (ret < 0)
632 goto done;
633
634 acb->remaining_sectors -= acb->cur_nr_sectors;
635 acb->sector_num += acb->cur_nr_sectors;
636 acb->bytes_done += acb->cur_nr_sectors * 512;
637
638 if (acb->remaining_sectors == 0) {
639 /* request completed */
640 ret = 0;
641 goto done;
642 }
643
644 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
645 n_end = index_in_cluster + acb->remaining_sectors;
646 if (s->crypt_method &&
647 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
648 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
649
650 ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
651 index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta);
652 if (ret < 0) {
653 goto done;
654 }
655
656 acb->cluster_offset = acb->l2meta.cluster_offset;
657
658 /* Need to wait for another request? If so, we are done for now. */
659 if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) {
660 QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests,
661 acb, next_depend);
662 return;
663 }
664
665 assert((acb->cluster_offset & 511) == 0);
666
667 qemu_iovec_reset(&acb->hd_qiov);
668 qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
669 acb->cur_nr_sectors * 512);
670
671 if (s->crypt_method) {
672 if (!acb->cluster_data) {
673 acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
674 s->cluster_size);
675 }
676
677 assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
678 qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data);
679
680 qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
681 acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key);
682
683 qemu_iovec_reset(&acb->hd_qiov);
684 qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
685 acb->cur_nr_sectors * 512);
686 }
687
688 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
689 acb->hd_aiocb = bdrv_aio_writev(bs->file,
690 (acb->cluster_offset >> 9) + index_in_cluster,
691 &acb->hd_qiov, acb->cur_nr_sectors,
692 qcow2_aio_write_cb, acb);
693 if (acb->hd_aiocb == NULL) {
694 ret = -EIO;
695 goto fail;
696 }
697
698 return;
699
700 fail:
701 if (acb->l2meta.nb_clusters != 0) {
702 QLIST_REMOVE(&acb->l2meta, next_in_flight);
703 }
704 done:
705 acb->common.cb(acb->common.opaque, ret);
706 qemu_iovec_destroy(&acb->hd_qiov);
707 qemu_aio_release(acb);
708 }
709
710 static BlockDriverAIOCB *qcow2_aio_writev(BlockDriverState *bs,
711 int64_t sector_num,
712 QEMUIOVector *qiov, int nb_sectors,
713 BlockDriverCompletionFunc *cb,
714 void *opaque)
715 {
716 BDRVQcowState *s = bs->opaque;
717 QCowAIOCB *acb;
718
719 s->cluster_cache_offset = -1; /* disable compressed cache */
720
721 acb = qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
722 if (!acb)
723 return NULL;
724
725 qcow2_aio_write_cb(acb, 0);
726 return &acb->common;
727 }
728
729 static void qcow2_close(BlockDriverState *bs)
730 {
731 BDRVQcowState *s = bs->opaque;
732 qemu_free(s->l1_table);
733
734 qcow2_cache_flush(bs, s->l2_table_cache);
735 qcow2_cache_flush(bs, s->refcount_block_cache);
736
737 qcow2_cache_destroy(bs, s->l2_table_cache);
738 qcow2_cache_destroy(bs, s->refcount_block_cache);
739
740 qemu_free(s->cluster_cache);
741 qemu_free(s->cluster_data);
742 qcow2_refcount_close(bs);
743 }
744
745 /*
746 * Updates the variable length parts of the qcow2 header, i.e. the backing file
747 * name and all extensions. qcow2 was not designed to allow such changes, so if
748 * we run out of space (we can only use the first cluster) this function may
749 * fail.
750 *
751 * Returns 0 on success, -errno in error cases.
752 */
753 static int qcow2_update_ext_header(BlockDriverState *bs,
754 const char *backing_file, const char *backing_fmt)
755 {
756 size_t backing_file_len = 0;
757 size_t backing_fmt_len = 0;
758 BDRVQcowState *s = bs->opaque;
759 QCowExtension ext_backing_fmt = {0, 0};
760 int ret;
761
762 /* Backing file format doesn't make sense without a backing file */
763 if (backing_fmt && !backing_file) {
764 return -EINVAL;
765 }
766
767 /* Prepare the backing file format extension if needed */
768 if (backing_fmt) {
769 ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt));
770 ext_backing_fmt.magic = cpu_to_be32(QCOW2_EXT_MAGIC_BACKING_FORMAT);
771 backing_fmt_len = ((sizeof(ext_backing_fmt)
772 + strlen(backing_fmt) + 7) & ~7);
773 }
774
775 /* Check if we can fit the new header into the first cluster */
776 if (backing_file) {
777 backing_file_len = strlen(backing_file);
778 }
779
780 size_t header_size = sizeof(QCowHeader) + backing_file_len
781 + backing_fmt_len;
782
783 if (header_size > s->cluster_size) {
784 return -ENOSPC;
785 }
786
787 /* Rewrite backing file name and qcow2 extensions */
788 size_t ext_size = header_size - sizeof(QCowHeader);
789 uint8_t buf[ext_size];
790 size_t offset = 0;
791 size_t backing_file_offset = 0;
792
793 if (backing_file) {
794 if (backing_fmt) {
795 int padding = backing_fmt_len -
796 (sizeof(ext_backing_fmt) + strlen(backing_fmt));
797
798 memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
799 offset += sizeof(ext_backing_fmt);
800
801 memcpy(buf + offset, backing_fmt, strlen(backing_fmt));
802 offset += strlen(backing_fmt);
803
804 memset(buf + offset, 0, padding);
805 offset += padding;
806 }
807
808 memcpy(buf + offset, backing_file, backing_file_len);
809 backing_file_offset = sizeof(QCowHeader) + offset;
810 }
811
812 ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size);
813 if (ret < 0) {
814 goto fail;
815 }
816
817 /* Update header fields */
818 uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset);
819 uint32_t be_backing_file_size = cpu_to_be32(backing_file_len);
820
821 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset),
822 &be_backing_file_offset, sizeof(uint64_t));
823 if (ret < 0) {
824 goto fail;
825 }
826
827 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size),
828 &be_backing_file_size, sizeof(uint32_t));
829 if (ret < 0) {
830 goto fail;
831 }
832
833 ret = 0;
834 fail:
835 return ret;
836 }
837
838 static int qcow2_change_backing_file(BlockDriverState *bs,
839 const char *backing_file, const char *backing_fmt)
840 {
841 return qcow2_update_ext_header(bs, backing_file, backing_fmt);
842 }
843
844 static int preallocate(BlockDriverState *bs)
845 {
846 uint64_t nb_sectors;
847 uint64_t offset;
848 int num;
849 int ret;
850 QCowL2Meta meta;
851
852 nb_sectors = bdrv_getlength(bs) >> 9;
853 offset = 0;
854 QLIST_INIT(&meta.dependent_requests);
855 meta.cluster_offset = 0;
856
857 while (nb_sectors) {
858 num = MIN(nb_sectors, INT_MAX >> 9);
859 ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
860 if (ret < 0) {
861 return ret;
862 }
863
864 ret = qcow2_alloc_cluster_link_l2(bs, &meta);
865 if (ret < 0) {
866 qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
867 return ret;
868 }
869
870 /* There are no dependent requests, but we need to remove our request
871 * from the list of in-flight requests */
872 run_dependent_requests(&meta);
873
874 /* TODO Preallocate data if requested */
875
876 nb_sectors -= num;
877 offset += num << 9;
878 }
879
880 /*
881 * It is expected that the image file is large enough to actually contain
882 * all of the allocated clusters (otherwise we get failing reads after
883 * EOF). Extend the image to the last allocated sector.
884 */
885 if (meta.cluster_offset != 0) {
886 uint8_t buf[512];
887 memset(buf, 0, 512);
888 ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
889 if (ret < 0) {
890 return ret;
891 }
892 }
893
894 return 0;
895 }
896
897 static int qcow2_create2(const char *filename, int64_t total_size,
898 const char *backing_file, const char *backing_format,
899 int flags, size_t cluster_size, int prealloc,
900 QEMUOptionParameter *options)
901 {
902 /* Calulate cluster_bits */
903 int cluster_bits;
904 cluster_bits = ffs(cluster_size) - 1;
905 if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
906 (1 << cluster_bits) != cluster_size)
907 {
908 error_report(
909 "Cluster size must be a power of two between %d and %dk\n",
910 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
911 return -EINVAL;
912 }
913
914 /*
915 * Open the image file and write a minimal qcow2 header.
916 *
917 * We keep things simple and start with a zero-sized image. We also
918 * do without refcount blocks or a L1 table for now. We'll fix the
919 * inconsistency later.
920 *
921 * We do need a refcount table because growing the refcount table means
922 * allocating two new refcount blocks - the seconds of which would be at
923 * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
924 * size for any qcow2 image.
925 */
926 BlockDriverState* bs;
927 QCowHeader header;
928 uint8_t* refcount_table;
929 int ret;
930
931 ret = bdrv_create_file(filename, options);
932 if (ret < 0) {
933 return ret;
934 }
935
936 ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
937 if (ret < 0) {
938 return ret;
939 }
940
941 /* Write the header */
942 memset(&header, 0, sizeof(header));
943 header.magic = cpu_to_be32(QCOW_MAGIC);
944 header.version = cpu_to_be32(QCOW_VERSION);
945 header.cluster_bits = cpu_to_be32(cluster_bits);
946 header.size = cpu_to_be64(0);
947 header.l1_table_offset = cpu_to_be64(0);
948 header.l1_size = cpu_to_be32(0);
949 header.refcount_table_offset = cpu_to_be64(cluster_size);
950 header.refcount_table_clusters = cpu_to_be32(1);
951
952 if (flags & BLOCK_FLAG_ENCRYPT) {
953 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
954 } else {
955 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
956 }
957
958 ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
959 if (ret < 0) {
960 goto out;
961 }
962
963 /* Write an empty refcount table */
964 refcount_table = qemu_mallocz(cluster_size);
965 ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
966 qemu_free(refcount_table);
967
968 if (ret < 0) {
969 goto out;
970 }
971
972 bdrv_close(bs);
973
974 /*
975 * And now open the image and make it consistent first (i.e. increase the
976 * refcount of the cluster that is occupied by the header and the refcount
977 * table)
978 */
979 BlockDriver* drv = bdrv_find_format("qcow2");
980 assert(drv != NULL);
981 ret = bdrv_open(bs, filename,
982 BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
983 if (ret < 0) {
984 goto out;
985 }
986
987 ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
988 if (ret < 0) {
989 goto out;
990
991 } else if (ret != 0) {
992 error_report("Huh, first cluster in empty image is already in use?");
993 abort();
994 }
995
996 /* Okay, now that we have a valid image, let's give it the right size */
997 ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
998 if (ret < 0) {
999 goto out;
1000 }
1001
1002 /* Want a backing file? There you go.*/
1003 if (backing_file) {
1004 ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1005 if (ret < 0) {
1006 goto out;
1007 }
1008 }
1009
1010 /* And if we're supposed to preallocate metadata, do that now */
1011 if (prealloc) {
1012 ret = preallocate(bs);
1013 if (ret < 0) {
1014 goto out;
1015 }
1016 }
1017
1018 ret = 0;
1019 out:
1020 bdrv_delete(bs);
1021 return ret;
1022 }
1023
1024 static int qcow2_create(const char *filename, QEMUOptionParameter *options)
1025 {
1026 const char *backing_file = NULL;
1027 const char *backing_fmt = NULL;
1028 uint64_t sectors = 0;
1029 int flags = 0;
1030 size_t cluster_size = 65536;
1031 int prealloc = 0;
1032
1033 /* Read out options */
1034 while (options && options->name) {
1035 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1036 sectors = options->value.n / 512;
1037 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1038 backing_file = options->value.s;
1039 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1040 backing_fmt = options->value.s;
1041 } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1042 flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1043 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1044 if (options->value.n) {
1045 cluster_size = options->value.n;
1046 }
1047 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1048 if (!options->value.s || !strcmp(options->value.s, "off")) {
1049 prealloc = 0;
1050 } else if (!strcmp(options->value.s, "metadata")) {
1051 prealloc = 1;
1052 } else {
1053 fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1054 options->value.s);
1055 return -EINVAL;
1056 }
1057 }
1058 options++;
1059 }
1060
1061 if (backing_file && prealloc) {
1062 fprintf(stderr, "Backing file and preallocation cannot be used at "
1063 "the same time\n");
1064 return -EINVAL;
1065 }
1066
1067 return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1068 cluster_size, prealloc, options);
1069 }
1070
1071 static int qcow2_make_empty(BlockDriverState *bs)
1072 {
1073 #if 0
1074 /* XXX: not correct */
1075 BDRVQcowState *s = bs->opaque;
1076 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1077 int ret;
1078
1079 memset(s->l1_table, 0, l1_length);
1080 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1081 return -1;
1082 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1083 if (ret < 0)
1084 return ret;
1085
1086 l2_cache_reset(bs);
1087 #endif
1088 return 0;
1089 }
1090
1091 static int qcow2_discard(BlockDriverState *bs, int64_t sector_num,
1092 int nb_sectors)
1093 {
1094 return qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1095 nb_sectors);
1096 }
1097
1098 static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1099 {
1100 BDRVQcowState *s = bs->opaque;
1101 int ret, new_l1_size;
1102
1103 if (offset & 511) {
1104 return -EINVAL;
1105 }
1106
1107 /* cannot proceed if image has snapshots */
1108 if (s->nb_snapshots) {
1109 return -ENOTSUP;
1110 }
1111
1112 /* shrinking is currently not supported */
1113 if (offset < bs->total_sectors * 512) {
1114 return -ENOTSUP;
1115 }
1116
1117 new_l1_size = size_to_l1(s, offset);
1118 ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1119 if (ret < 0) {
1120 return ret;
1121 }
1122
1123 /* write updated header.size */
1124 offset = cpu_to_be64(offset);
1125 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1126 &offset, sizeof(uint64_t));
1127 if (ret < 0) {
1128 return ret;
1129 }
1130
1131 s->l1_vm_state_index = new_l1_size;
1132 return 0;
1133 }
1134
1135 /* XXX: put compressed sectors first, then all the cluster aligned
1136 tables to avoid losing bytes in alignment */
1137 static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1138 const uint8_t *buf, int nb_sectors)
1139 {
1140 BDRVQcowState *s = bs->opaque;
1141 z_stream strm;
1142 int ret, out_len;
1143 uint8_t *out_buf;
1144 uint64_t cluster_offset;
1145
1146 if (nb_sectors == 0) {
1147 /* align end of file to a sector boundary to ease reading with
1148 sector based I/Os */
1149 cluster_offset = bdrv_getlength(bs->file);
1150 cluster_offset = (cluster_offset + 511) & ~511;
1151 bdrv_truncate(bs->file, cluster_offset);
1152 return 0;
1153 }
1154
1155 if (nb_sectors != s->cluster_sectors)
1156 return -EINVAL;
1157
1158 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1159
1160 /* best compression, small window, no zlib header */
1161 memset(&strm, 0, sizeof(strm));
1162 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1163 Z_DEFLATED, -12,
1164 9, Z_DEFAULT_STRATEGY);
1165 if (ret != 0) {
1166 qemu_free(out_buf);
1167 return -1;
1168 }
1169
1170 strm.avail_in = s->cluster_size;
1171 strm.next_in = (uint8_t *)buf;
1172 strm.avail_out = s->cluster_size;
1173 strm.next_out = out_buf;
1174
1175 ret = deflate(&strm, Z_FINISH);
1176 if (ret != Z_STREAM_END && ret != Z_OK) {
1177 qemu_free(out_buf);
1178 deflateEnd(&strm);
1179 return -1;
1180 }
1181 out_len = strm.next_out - out_buf;
1182
1183 deflateEnd(&strm);
1184
1185 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1186 /* could not compress: write normal cluster */
1187 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1188 } else {
1189 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1190 sector_num << 9, out_len);
1191 if (!cluster_offset)
1192 return -1;
1193 cluster_offset &= s->cluster_offset_mask;
1194 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1195 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
1196 qemu_free(out_buf);
1197 return -1;
1198 }
1199 }
1200
1201 qemu_free(out_buf);
1202 return 0;
1203 }
1204
1205 static int qcow2_flush(BlockDriverState *bs)
1206 {
1207 BDRVQcowState *s = bs->opaque;
1208 int ret;
1209
1210 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1211 if (ret < 0) {
1212 return ret;
1213 }
1214
1215 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1216 if (ret < 0) {
1217 return ret;
1218 }
1219
1220 return bdrv_flush(bs->file);
1221 }
1222
1223 static BlockDriverAIOCB *qcow2_aio_flush(BlockDriverState *bs,
1224 BlockDriverCompletionFunc *cb,
1225 void *opaque)
1226 {
1227 BDRVQcowState *s = bs->opaque;
1228 int ret;
1229
1230 ret = qcow2_cache_flush(bs, s->l2_table_cache);
1231 if (ret < 0) {
1232 return NULL;
1233 }
1234
1235 ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1236 if (ret < 0) {
1237 return NULL;
1238 }
1239
1240 return bdrv_aio_flush(bs->file, cb, opaque);
1241 }
1242
1243 static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
1244 {
1245 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1246 }
1247
1248 static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1249 {
1250 BDRVQcowState *s = bs->opaque;
1251 bdi->cluster_size = s->cluster_size;
1252 bdi->vm_state_offset = qcow2_vm_state_offset(s);
1253 return 0;
1254 }
1255
1256
1257 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result)
1258 {
1259 return qcow2_check_refcounts(bs, result);
1260 }
1261
1262 #if 0
1263 static void dump_refcounts(BlockDriverState *bs)
1264 {
1265 BDRVQcowState *s = bs->opaque;
1266 int64_t nb_clusters, k, k1, size;
1267 int refcount;
1268
1269 size = bdrv_getlength(bs->file);
1270 nb_clusters = size_to_clusters(s, size);
1271 for(k = 0; k < nb_clusters;) {
1272 k1 = k;
1273 refcount = get_refcount(bs, k);
1274 k++;
1275 while (k < nb_clusters && get_refcount(bs, k) == refcount)
1276 k++;
1277 printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1278 k - k1);
1279 }
1280 }
1281 #endif
1282
1283 static int qcow2_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1284 int64_t pos, int size)
1285 {
1286 BDRVQcowState *s = bs->opaque;
1287 int growable = bs->growable;
1288 int ret;
1289
1290 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1291 bs->growable = 1;
1292 ret = bdrv_pwrite(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1293 bs->growable = growable;
1294
1295 return ret;
1296 }
1297
1298 static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1299 int64_t pos, int size)
1300 {
1301 BDRVQcowState *s = bs->opaque;
1302 int growable = bs->growable;
1303 int ret;
1304
1305 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1306 bs->growable = 1;
1307 ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1308 bs->growable = growable;
1309
1310 return ret;
1311 }
1312
1313 static QEMUOptionParameter qcow2_create_options[] = {
1314 {
1315 .name = BLOCK_OPT_SIZE,
1316 .type = OPT_SIZE,
1317 .help = "Virtual disk size"
1318 },
1319 {
1320 .name = BLOCK_OPT_BACKING_FILE,
1321 .type = OPT_STRING,
1322 .help = "File name of a base image"
1323 },
1324 {
1325 .name = BLOCK_OPT_BACKING_FMT,
1326 .type = OPT_STRING,
1327 .help = "Image format of the base image"
1328 },
1329 {
1330 .name = BLOCK_OPT_ENCRYPT,
1331 .type = OPT_FLAG,
1332 .help = "Encrypt the image"
1333 },
1334 {
1335 .name = BLOCK_OPT_CLUSTER_SIZE,
1336 .type = OPT_SIZE,
1337 .help = "qcow2 cluster size"
1338 },
1339 {
1340 .name = BLOCK_OPT_PREALLOC,
1341 .type = OPT_STRING,
1342 .help = "Preallocation mode (allowed values: off, metadata)"
1343 },
1344 { NULL }
1345 };
1346
1347 static BlockDriver bdrv_qcow2 = {
1348 .format_name = "qcow2",
1349 .instance_size = sizeof(BDRVQcowState),
1350 .bdrv_probe = qcow2_probe,
1351 .bdrv_open = qcow2_open,
1352 .bdrv_close = qcow2_close,
1353 .bdrv_create = qcow2_create,
1354 .bdrv_flush = qcow2_flush,
1355 .bdrv_is_allocated = qcow2_is_allocated,
1356 .bdrv_set_key = qcow2_set_key,
1357 .bdrv_make_empty = qcow2_make_empty,
1358
1359 .bdrv_aio_readv = qcow2_aio_readv,
1360 .bdrv_aio_writev = qcow2_aio_writev,
1361 .bdrv_aio_flush = qcow2_aio_flush,
1362
1363 .bdrv_discard = qcow2_discard,
1364 .bdrv_truncate = qcow2_truncate,
1365 .bdrv_write_compressed = qcow2_write_compressed,
1366
1367 .bdrv_snapshot_create = qcow2_snapshot_create,
1368 .bdrv_snapshot_goto = qcow2_snapshot_goto,
1369 .bdrv_snapshot_delete = qcow2_snapshot_delete,
1370 .bdrv_snapshot_list = qcow2_snapshot_list,
1371 .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp,
1372 .bdrv_get_info = qcow2_get_info,
1373
1374 .bdrv_save_vmstate = qcow2_save_vmstate,
1375 .bdrv_load_vmstate = qcow2_load_vmstate,
1376
1377 .bdrv_change_backing_file = qcow2_change_backing_file,
1378
1379 .create_options = qcow2_create_options,
1380 .bdrv_check = qcow2_check,
1381 };
1382
1383 static void bdrv_qcow2_init(void)
1384 {
1385 bdrv_register(&bdrv_qcow2);
1386 }
1387
1388 block_init(bdrv_qcow2_init);