ia64/xen-unstable

view tools/blktap/drivers/block-qcow2.c @ 19297:12c19447f6ea

blktap: Fix compile warnings in block-qcow2.c

Signed-off-by: Yosuke Iwamatsu <y-iwamatsu@ab.jp.nec.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Mar 09 09:48:45 2009 +0000 (2009-03-09)
parents 7f573cb76db4
children
line source
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 */
25 #include <zlib.h>
26 #include "aes.h"
27 #include <assert.h>
28 #include <stdint.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "tapdisk.h"
35 #include "tapaio.h"
36 #include "bswap.h"
37 #include "blk.h"
39 #define USE_AIO
41 #define qemu_malloc malloc
42 #define qemu_mallocz(size) calloc(1, size)
43 #define qemu_free free
45 #ifndef O_BINARY
46 #define O_BINARY 0
47 #endif
49 /* *BSD has no O_LARGEFILE */
50 #ifndef O_LARGEFILE
51 #define O_LARGEFILE 0
52 #endif
54 #define BLOCK_FLAG_ENCRYPT 1
56 /*
57 Differences with QCOW:
59 - Support for multiple incremental snapshots.
60 - Memory management by reference counts.
61 - Clusters which have a reference count of one have the bit
62 QCOW_OFLAG_COPIED to optimize write performance.
63 - Size of compressed clusters is stored in sectors to reduce bit usage
64 in the cluster offsets.
65 - Support for storing additional data (such as the VM state) in the
66 snapshots.
67 - If a backing store is used, the cluster size is not constrained
68 (could be backported to QCOW).
69 - L2 tables have always a size of one cluster.
70 */
72 //#define DEBUG_ALLOC
73 //#define DEBUG_ALLOC2
75 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
76 #define QCOW_VERSION 2
78 #define QCOW_CRYPT_NONE 0
79 #define QCOW_CRYPT_AES 1
81 /* indicate that the refcount of the referenced cluster is exactly one. */
82 #define QCOW_OFLAG_COPIED (1LL << 63)
83 /* indicate that the cluster is compressed (they never have the copied flag) */
84 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
86 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
88 #ifndef offsetof
89 #define offsetof(type, field) ((size_t) &((type *)0)->field)
90 #endif
92 typedef struct QCowHeader {
93 uint32_t magic;
94 uint32_t version;
95 uint64_t backing_file_offset;
96 uint32_t backing_file_size;
97 uint32_t cluster_bits;
98 uint64_t size; /* in bytes */
100 uint32_t crypt_method;
101 uint32_t l1_size; /* XXX: save number of clusters instead ? */
102 uint64_t l1_table_offset;
103 uint64_t refcount_table_offset;
104 uint32_t refcount_table_clusters;
105 uint32_t nb_snapshots;
106 uint64_t snapshots_offset;
107 } QCowHeader;
109 typedef struct __attribute__((packed)) QCowSnapshotHeader {
110 /* header is 8 byte aligned */
111 uint64_t l1_table_offset;
113 uint32_t l1_size;
114 uint16_t id_str_size;
115 uint16_t name_size;
117 uint32_t date_sec;
118 uint32_t date_nsec;
120 uint64_t vm_clock_nsec;
122 uint32_t vm_state_size;
123 uint32_t extra_data_size; /* for extension */
124 /* extra data follows */
125 /* id_str follows */
126 /* name follows */
127 } QCowSnapshotHeader;
129 #define L2_CACHE_SIZE 16
131 typedef struct QCowSnapshot {
132 uint64_t l1_table_offset;
133 uint32_t l1_size;
134 char *id_str;
135 char *name;
136 uint32_t vm_state_size;
137 uint32_t date_sec;
138 uint32_t date_nsec;
139 uint64_t vm_clock_nsec;
140 } QCowSnapshot;
142 typedef struct BDRVQcowState {
144 /* blktap additions */
145 int fd;
146 int poll_pipe[2]; /* dummy fd for polling on */
147 char* name;
148 int encrypted;
149 char backing_file[1024];
150 struct disk_driver* backing_hd;
152 int64_t total_sectors;
154 tap_aio_context_t async;
156 /* Original qemu variables */
157 int cluster_bits;
158 int cluster_size;
159 int cluster_sectors;
160 int l2_bits;
161 int l2_size;
162 int l1_size;
163 int l1_vm_state_index;
164 int csize_shift;
165 int csize_mask;
166 uint64_t cluster_offset_mask;
167 uint64_t l1_table_offset;
168 uint64_t *l1_table;
169 uint64_t *l2_cache;
170 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
171 uint32_t l2_cache_counts[L2_CACHE_SIZE];
172 uint8_t *cluster_cache;
173 uint8_t *cluster_data;
174 uint64_t cluster_cache_offset;
176 uint64_t *refcount_table;
177 uint64_t refcount_table_offset;
178 uint32_t refcount_table_size;
179 uint64_t refcount_block_cache_offset;
180 uint16_t *refcount_block_cache;
181 int64_t free_cluster_index;
182 int64_t free_byte_offset;
184 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
185 uint32_t crypt_method_header;
186 AES_KEY aes_encrypt_key;
187 AES_KEY aes_decrypt_key;
188 uint64_t snapshots_offset;
189 int snapshots_size;
190 int nb_snapshots;
191 QCowSnapshot *snapshots;
192 } BDRVQcowState;
194 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
195 static int qcow_read(struct disk_driver *bs, uint64_t sector_num,
196 uint8_t *buf, int nb_sectors);
198 static int qcow_read_snapshots(struct disk_driver *bs);
199 static void qcow_free_snapshots(struct disk_driver *bs);
201 static int refcount_init(struct disk_driver *bs);
202 static void refcount_close(struct disk_driver *bs);
203 static int get_refcount(struct disk_driver *bs, int64_t cluster_index);
204 static int update_cluster_refcount(struct disk_driver *bs,
205 int64_t cluster_index,
206 int addend);
207 static void update_refcount(struct disk_driver *bs,
208 int64_t offset, int64_t length,
209 int addend);
210 static int64_t alloc_clusters(struct disk_driver *bs, int64_t size);
211 static int64_t alloc_bytes(struct disk_driver *bs, int size);
212 static void free_clusters(struct disk_driver *bs,
213 int64_t offset, int64_t size);
214 #ifdef DEBUG_ALLOC
215 static void check_refcounts(struct disk_driver *bs);
216 #endif
218 static int qcow_sync_read(struct disk_driver *dd, uint64_t sector,
219 int nb_sectors, char *buf, td_callback_t cb,
220 int id, void *prv);
222 /**
223 * Read with byte offsets
224 */
225 static int bdrv_pread(int fd, int64_t offset, void *buf, int count)
226 {
227 int ret;
229 if (lseek(fd, offset, SEEK_SET) == -1) {
230 DPRINTF("bdrv_pread failed seek (%#"PRIx64").\n", offset);
231 return -1;
232 }
234 ret = read(fd, buf, count);
235 if (ret < 0) {
236 if (lseek(fd, 0, SEEK_END) >= offset) {
237 DPRINTF("bdrv_pread read failed (%#"PRIx64", END = %#"PRIx64").\n",
238 offset, lseek(fd, 0, SEEK_END));
239 return -1;
240 }
242 /* Read beyond end of file. Reading zeros. */
243 memset(buf, 0, count);
244 ret = count;
245 } else if (ret < count) {
246 /* Read beyond end of file. Filling up with zeros. */
247 memset(buf + ret, 0, count - ret);
248 ret = count;
249 }
250 return ret;
251 }
253 /**
254 * Write with byte offsets
255 */
256 static int bdrv_pwrite(int fd, int64_t offset, const void *buf, int count)
257 {
258 if (lseek(fd, offset, SEEK_SET) == -1) {
259 DPRINTF("bdrv_pwrite failed seek (%#"PRIx64").\n", offset);
260 return -1;
261 }
263 return write(fd, buf, count);
264 }
267 /**
268 * Read with sector offsets
269 */
270 static int bdrv_read(int fd, int64_t offset, void *buf, int count)
271 {
272 return bdrv_pread(fd, 512 * offset, buf, 512 * count);
273 }
275 /**
276 * Write with sector offsets
277 */
278 static int bdrv_write(int fd, int64_t offset, const void *buf, int count)
279 {
280 return bdrv_pwrite(fd, 512 * offset, buf, count);
281 }
284 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
285 {
286 const QCowHeader *cow_header = (const void *)buf;
288 if (buf_size >= sizeof(QCowHeader) &&
289 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
290 be32_to_cpu(cow_header->version) == QCOW_VERSION)
291 return 100;
292 else
293 return 0;
294 }
296 static int qcow_open(struct disk_driver *bs, const char *filename, td_flag_t flags)
297 {
298 BDRVQcowState *s = bs->private;
299 int len, i, shift, ret, max_aio_reqs;
300 QCowHeader header;
302 int fd, o_flags;
304 o_flags = O_LARGEFILE | ((flags == TD_RDONLY) ? O_RDONLY : O_RDWR);
306 DPRINTF("Opening %s\n", filename);
307 fd = open(filename, o_flags);
308 if (fd < 0) {
309 DPRINTF("Unable to open %s (%d)\n", filename, 0 - errno);
310 return -1;
311 }
313 s->fd = fd;
314 if (asprintf(&s->name,"%s", filename) == -1) {
315 close(fd);
316 return -1;
317 }
319 ret = read(fd, &header, sizeof(header));
320 if (ret != sizeof(header)) {
321 DPRINTF(" ret = %d, errno = %d\n", ret, errno);
322 goto fail;
323 }
325 be32_to_cpus(&header.magic);
326 be32_to_cpus(&header.version);
327 be64_to_cpus(&header.backing_file_offset);
328 be32_to_cpus(&header.backing_file_size);
329 be64_to_cpus(&header.size);
330 be32_to_cpus(&header.cluster_bits);
331 be32_to_cpus(&header.crypt_method);
332 be64_to_cpus(&header.l1_table_offset);
333 be32_to_cpus(&header.l1_size);
334 be64_to_cpus(&header.refcount_table_offset);
335 be32_to_cpus(&header.refcount_table_clusters);
336 be64_to_cpus(&header.snapshots_offset);
337 be32_to_cpus(&header.nb_snapshots);
339 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
340 goto fail;
342 if (header.size <= 1 ||
343 header.cluster_bits < 9 ||
344 header.cluster_bits > 16)
345 goto fail;
347 s->crypt_method = 0;
348 if (header.crypt_method > QCOW_CRYPT_AES)
349 goto fail;
350 s->crypt_method_header = header.crypt_method;
351 if (s->crypt_method_header)
352 s->encrypted = 1;
353 s->cluster_bits = header.cluster_bits;
354 s->cluster_size = 1 << s->cluster_bits;
355 s->cluster_sectors = 1 << (s->cluster_bits - 9);
356 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
357 s->l2_size = 1 << s->l2_bits;
358 s->total_sectors = header.size / 512;
359 s->csize_shift = (62 - (s->cluster_bits - 8));
360 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
361 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
362 s->refcount_table_offset = header.refcount_table_offset;
363 s->refcount_table_size =
364 header.refcount_table_clusters << (s->cluster_bits - 3);
366 s->snapshots_offset = header.snapshots_offset;
367 s->nb_snapshots = header.nb_snapshots;
369 // DPRINTF("-- cluster_bits/size/sectors = %d/%d/%d\n",
370 // s->cluster_bits, s->cluster_size, s->cluster_sectors);
371 // DPRINTF("-- l2_bits/sizes = %d/%d\n",
372 // s->l2_bits, s->l2_size);
374 /* Set sector size and number */
375 bs->td_state->sector_size = 512;
376 bs->td_state->size = header.size / 512;
377 bs->td_state->info = 0;
379 /* read the level 1 table */
380 s->l1_size = header.l1_size;
381 shift = s->cluster_bits + s->l2_bits;
382 s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
383 /* the L1 table must contain at least enough entries to put
384 header.size bytes */
385 if (s->l1_size < s->l1_vm_state_index) {
386 DPRINTF("L1 table tooo small\n");
387 goto fail;
388 }
389 s->l1_table_offset = header.l1_table_offset;
391 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
392 if (!s->l1_table)
393 goto fail;
396 if (lseek(fd, s->l1_table_offset, SEEK_SET) == -1)
397 goto fail;
399 if (read(fd, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
400 s->l1_size * sizeof(uint64_t)) {
402 DPRINTF("Could not read L1 table\n");
403 goto fail;
404 }
406 for(i = 0;i < s->l1_size; i++) {
407 be64_to_cpus(&s->l1_table[i]);
408 }
409 /* alloc L2 cache */
410 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
411 if (!s->l2_cache)
412 goto fail;
413 s->cluster_cache = qemu_malloc(s->cluster_size);
414 if (!s->cluster_cache)
415 goto fail;
416 /* one more sector for decompressed data alignment */
417 s->cluster_data = qemu_malloc(s->cluster_size + 512);
418 if (!s->cluster_data)
419 goto fail;
420 s->cluster_cache_offset = -1;
422 if (refcount_init(bs) < 0)
423 goto fail;
425 /* read the backing file name */
426 s->backing_file[0] = '\0';
427 if (header.backing_file_offset != 0) {
428 len = header.backing_file_size;
429 if (len > 1023)
430 len = 1023;
432 if (lseek(fd, header.backing_file_offset, SEEK_SET) == -1) {
433 DPRINTF("Could not lseek to %#"PRIx64"\n", header.backing_file_offset);
434 goto fail;
435 }
437 if (read(fd, s->backing_file, len) != len) {
438 DPRINTF("Could not read %#x bytes from %#"PRIx64": %s\n",
439 len, header.backing_file_offset,
440 strerror(errno));
441 goto fail;
442 }
444 s->backing_file[len] = '\0';
445 }
447 #if 0
448 s->backing_hd = NULL;
449 if (qcow_read_snapshots(bs) < 0) {
450 DPRINTF("Could not read backing files\n");
451 goto fail;
452 }
453 #endif
455 #ifdef DEBUG_ALLOC
456 check_refcounts(bs);
457 #endif
459 /* Initialize fds */
460 for(i = 0; i < MAX_IOFD; i++)
461 bs->io_fd[i] = 0;
463 #ifdef USE_AIO
464 /* Initialize AIO */
466 /* A segment (i.e. a page) can span multiple clusters */
467 max_aio_reqs = ((getpagesize() / s->cluster_size) + 1) *
468 MAX_SEGMENTS_PER_REQ * MAX_REQUESTS;
470 if (tap_aio_init(&s->async, bs->td_state->size, max_aio_reqs)) {
471 DPRINTF("Unable to initialise AIO state\n");
472 tap_aio_free(&s->async);
473 goto fail;
474 }
476 bs->io_fd[0] = s->async.aio_ctx.pollfd;
477 #else
478 /* Synchronous IO */
479 if (pipe(s->poll_pipe))
480 goto fail;
482 bs->io_fd[0] = s->poll_pipe[0];
483 #endif
485 return 0;
487 fail:
488 DPRINTF("qcow_open failed\n");
490 #ifdef USE_AIO
491 tap_aio_free(&s->async);
492 #endif
494 qcow_free_snapshots(bs);
495 refcount_close(bs);
496 qemu_free(s->l1_table);
497 qemu_free(s->l2_cache);
498 qemu_free(s->cluster_cache);
499 qemu_free(s->cluster_data);
500 close(fd);
501 return -1;
502 }
504 static int qcow_set_key(struct disk_driver *bs, const char *key)
505 {
506 BDRVQcowState *s = bs->private;
507 uint8_t keybuf[16];
508 int len, i;
510 memset(keybuf, 0, 16);
511 len = strlen(key);
512 if (len > 16)
513 len = 16;
514 /* XXX: we could compress the chars to 7 bits to increase
515 entropy */
516 for(i = 0;i < len;i++) {
517 keybuf[i] = key[i];
518 }
519 s->crypt_method = s->crypt_method_header;
521 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
522 return -1;
523 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
524 return -1;
525 #if 0
526 /* test */
527 {
528 uint8_t in[16];
529 uint8_t out[16];
530 uint8_t tmp[16];
531 for(i=0;i<16;i++)
532 in[i] = i;
533 AES_encrypt(in, tmp, &s->aes_encrypt_key);
534 AES_decrypt(tmp, out, &s->aes_decrypt_key);
535 for(i = 0; i < 16; i++)
536 printf(" %02x", tmp[i]);
537 printf("\n");
538 for(i = 0; i < 16; i++)
539 printf(" %02x", out[i]);
540 printf("\n");
541 }
542 #endif
543 return 0;
544 }
546 /* The crypt function is compatible with the linux cryptoloop
547 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
548 supported */
549 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
550 uint8_t *out_buf, const uint8_t *in_buf,
551 int nb_sectors, int enc,
552 const AES_KEY *key)
553 {
554 union {
555 uint64_t ll[2];
556 uint8_t b[16];
557 } ivec;
558 int i;
560 for(i = 0; i < nb_sectors; i++) {
561 ivec.ll[0] = cpu_to_le64(sector_num);
562 ivec.ll[1] = 0;
563 AES_cbc_encrypt(in_buf, out_buf, 512, key,
564 ivec.b, enc);
565 sector_num++;
566 in_buf += 512;
567 out_buf += 512;
568 }
569 }
571 static int copy_sectors(struct disk_driver *bs, uint64_t start_sect,
572 uint64_t cluster_offset, int n_start, int n_end)
573 {
574 BDRVQcowState *s = bs->private;
575 int n, ret;
577 n = n_end - n_start;
578 if (n <= 0)
579 return 0;
581 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
583 if (ret < 0)
584 return ret;
585 if (s->crypt_method) {
586 encrypt_sectors(s, start_sect + n_start,
587 s->cluster_data,
588 s->cluster_data, n, 1,
589 &s->aes_encrypt_key);
590 }
593 ret = bdrv_pwrite(s->fd, cluster_offset + 512*n_start, s->cluster_data, n*512);
595 if (ret < 0)
596 return ret;
597 return 0;
598 }
600 static void l2_cache_reset(struct disk_driver *bs)
601 {
602 BDRVQcowState *s = bs->private;
604 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
605 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
606 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
607 }
609 static inline int l2_cache_new_entry(struct disk_driver *bs)
610 {
611 BDRVQcowState *s = bs->private;
612 uint32_t min_count;
613 int min_index, i;
615 /* find a new entry in the least used one */
616 min_index = 0;
617 min_count = 0xffffffff;
618 for(i = 0; i < L2_CACHE_SIZE; i++) {
619 if (s->l2_cache_counts[i] < min_count) {
620 min_count = s->l2_cache_counts[i];
621 min_index = i;
622 }
623 }
624 return min_index;
625 }
627 static int64_t align_offset(int64_t offset, int n)
628 {
629 offset = (offset + n - 1) & ~(n - 1);
630 return offset;
631 }
633 static int grow_l1_table(struct disk_driver *bs, int min_size)
634 {
635 BDRVQcowState *s = bs->private;
636 int new_l1_size, new_l1_size2, ret, i;
637 uint64_t *new_l1_table;
638 uint64_t new_l1_table_offset;
639 uint64_t data64;
640 uint32_t data32;
642 new_l1_size = s->l1_size;
643 if (min_size <= new_l1_size)
644 return 0;
645 while (min_size > new_l1_size) {
646 new_l1_size = (new_l1_size * 3 + 1) / 2;
647 }
649 #ifdef DEBUG_ALLOC2
650 DPRINTF("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
651 #endif
653 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
654 new_l1_table = qemu_mallocz(new_l1_size2);
655 if (!new_l1_table)
656 return -ENOMEM;
657 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
659 /* write new table (align to cluster) */
660 new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
662 for(i = 0; i < s->l1_size; i++)
663 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
666 if (lseek(s->fd, new_l1_table_offset, SEEK_SET) == -1)
667 goto fail;
669 ret = write(s->fd, new_l1_table, new_l1_size2);
670 if (ret != new_l1_size2)
671 goto fail;
674 for(i = 0; i < s->l1_size; i++)
675 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
677 /* set new table */
678 data64 = cpu_to_be64(new_l1_table_offset);
680 if (lseek(s->fd, offsetof(QCowHeader, l1_table_offset), SEEK_SET) == -1)
681 goto fail;
683 if (write(s->fd, &data64, sizeof(data64)) != sizeof(data64))
684 goto fail;
686 data32 = cpu_to_be32(new_l1_size);
688 if (bdrv_pwrite(s->fd, offsetof(QCowHeader, l1_size),
689 &data32, sizeof(data32)) != sizeof(data32))
690 goto fail;
691 qemu_free(s->l1_table);
692 free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
693 s->l1_table_offset = new_l1_table_offset;
694 s->l1_table = new_l1_table;
695 s->l1_size = new_l1_size;
696 return 0;
697 fail:
698 qemu_free(s->l1_table);
699 return -EIO;
700 }
702 /* 'allocate' is:
703 *
704 * 0 not to allocate.
705 *
706 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
707 * 'n_end')
708 *
709 * 2 to allocate a compressed cluster of size
710 * 'compressed_size'. 'compressed_size' must be > 0 and <
711 * cluster_size
712 *
713 * return 0 if not allocated.
714 */
715 static uint64_t get_cluster_offset(struct disk_driver *bs,
716 uint64_t offset, int allocate,
717 int compressed_size,
718 int n_start, int n_end)
719 {
720 BDRVQcowState *s = bs->private;
721 int min_index, i, j, l1_index, l2_index, ret;
722 uint64_t l2_offset, *l2_table, cluster_offset, tmp, old_l2_offset;
724 l1_index = offset >> (s->l2_bits + s->cluster_bits);
725 if (l1_index >= s->l1_size) {
726 /* outside l1 table is allowed: we grow the table if needed */
727 if (!allocate)
728 return 0;
730 if (grow_l1_table(bs, l1_index + 1) < 0) {
731 DPRINTF("Could not grow L1 table");
732 return 0;
733 }
734 }
736 l2_offset = s->l1_table[l1_index];
737 if (!l2_offset) {
738 if (!allocate)
739 return 0;
741 l2_allocate:
742 old_l2_offset = l2_offset;
743 /* allocate a new l2 entry */
744 l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
746 /* update the L1 entry */
747 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
748 tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
749 if (bdrv_pwrite(s->fd, s->l1_table_offset + l1_index * sizeof(tmp),
750 &tmp, sizeof(tmp)) != sizeof(tmp))
751 return 0;
752 min_index = l2_cache_new_entry(bs);
753 l2_table = s->l2_cache + (min_index << s->l2_bits);
755 if (old_l2_offset == 0) {
756 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
757 } else {
758 if (bdrv_pread(s->fd, old_l2_offset,
759 l2_table, s->l2_size * sizeof(uint64_t)) !=
760 s->l2_size * sizeof(uint64_t))
761 return 0;
762 }
763 if (bdrv_pwrite(s->fd, l2_offset,
764 l2_table, s->l2_size * sizeof(uint64_t)) !=
765 s->l2_size * sizeof(uint64_t))
766 return 0;
767 } else {
768 if (!(l2_offset & QCOW_OFLAG_COPIED)) {
769 if (allocate) {
770 free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
771 goto l2_allocate;
772 }
773 } else {
774 l2_offset &= ~QCOW_OFLAG_COPIED;
775 }
776 for(i = 0; i < L2_CACHE_SIZE; i++) {
777 if (l2_offset == s->l2_cache_offsets[i]) {
778 /* increment the hit count */
779 if (++s->l2_cache_counts[i] == 0xffffffff) {
780 for(j = 0; j < L2_CACHE_SIZE; j++) {
781 s->l2_cache_counts[j] >>= 1;
782 }
783 }
784 l2_table = s->l2_cache + (i << s->l2_bits);
785 goto found;
786 }
787 }
788 /* not found: load a new entry in the least used one */
789 min_index = l2_cache_new_entry(bs);
790 l2_table = s->l2_cache + (min_index << s->l2_bits);
792 if (bdrv_pread(s->fd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
793 s->l2_size * sizeof(uint64_t))
794 {
795 DPRINTF("Could not read L2 table");
796 return 0;
797 }
798 }
799 s->l2_cache_offsets[min_index] = l2_offset;
800 s->l2_cache_counts[min_index] = 1;
801 found:
802 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
804 cluster_offset = be64_to_cpu(l2_table[l2_index]);
805 if (!cluster_offset) {
806 if (!allocate) {
807 return cluster_offset;
808 }
809 } else if (!(cluster_offset & QCOW_OFLAG_COPIED)) {
810 if (!allocate)
811 return cluster_offset;
812 /* free the cluster */
813 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
814 int nb_csectors;
815 nb_csectors = ((cluster_offset >> s->csize_shift) &
816 s->csize_mask) + 1;
817 free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
818 nb_csectors * 512);
819 } else {
820 free_clusters(bs, cluster_offset, s->cluster_size);
821 }
822 } else {
823 cluster_offset &= ~QCOW_OFLAG_COPIED;
824 return cluster_offset;
825 }
826 if (allocate == 1) {
827 /* allocate a new cluster */
828 cluster_offset = alloc_clusters(bs, s->cluster_size);
830 /* we must initialize the cluster content which won't be
831 written */
832 if ((n_end - n_start) < s->cluster_sectors) {
833 uint64_t start_sect;
835 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
836 ret = copy_sectors(bs, start_sect,
837 cluster_offset, 0, n_start);
838 if (ret < 0)
839 return 0;
840 ret = copy_sectors(bs, start_sect,
841 cluster_offset, n_end, s->cluster_sectors);
842 if (ret < 0)
843 return 0;
844 }
845 tmp = cpu_to_be64(cluster_offset | QCOW_OFLAG_COPIED);
846 } else {
847 int nb_csectors;
848 cluster_offset = alloc_bytes(bs, compressed_size);
849 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
850 (cluster_offset >> 9);
851 cluster_offset |= QCOW_OFLAG_COMPRESSED |
852 ((uint64_t)nb_csectors << s->csize_shift);
853 /* compressed clusters never have the copied flag */
854 tmp = cpu_to_be64(cluster_offset);
855 }
856 /* update L2 table */
857 l2_table[l2_index] = tmp;
859 if (bdrv_pwrite(s->fd, l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
860 return 0;
861 return cluster_offset;
862 }
864 static int qcow_is_allocated(struct disk_driver *bs, int64_t sector_num,
865 int nb_sectors, int *pnum)
866 {
867 BDRVQcowState *s = bs->private;
868 int index_in_cluster, n;
869 uint64_t cluster_offset;
871 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
872 index_in_cluster = sector_num & (s->cluster_sectors - 1);
873 n = s->cluster_sectors - index_in_cluster;
874 if (n > nb_sectors)
875 n = nb_sectors;
876 *pnum = n;
877 return (cluster_offset != 0);
878 }
880 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
881 const uint8_t *buf, int buf_size)
882 {
883 z_stream strm1, *strm = &strm1;
884 int ret, out_len;
886 memset(strm, 0, sizeof(*strm));
888 strm->next_in = (uint8_t *)buf;
889 strm->avail_in = buf_size;
890 strm->next_out = out_buf;
891 strm->avail_out = out_buf_size;
893 ret = inflateInit2(strm, -12);
894 if (ret != Z_OK)
895 return -1;
896 ret = inflate(strm, Z_FINISH);
897 out_len = strm->next_out - out_buf;
898 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
899 out_len != out_buf_size) {
900 inflateEnd(strm);
901 return -1;
902 }
903 inflateEnd(strm);
904 return 0;
905 }
907 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
908 {
909 int ret, csize, nb_csectors, sector_offset;
910 uint64_t coffset;
912 coffset = cluster_offset & s->cluster_offset_mask;
913 if (s->cluster_cache_offset != coffset) {
914 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
915 sector_offset = coffset & 511;
916 csize = nb_csectors * 512 - sector_offset;
917 ret = bdrv_read(s->fd, coffset >> 9, s->cluster_data, nb_csectors);
918 if (ret < 0) {
919 return -1;
920 }
921 if (decompress_buffer(s->cluster_cache, s->cluster_size,
922 s->cluster_data + sector_offset, csize) < 0) {
923 return -1;
924 }
925 s->cluster_cache_offset = coffset;
926 }
927 return 0;
928 }
930 /* handle reading after the end of the backing file */
931 static int backing_read1(struct disk_driver *bs,
932 int64_t sector_num, uint8_t *buf, int nb_sectors)
933 {
934 int n1;
935 BDRVQcowState* s = bs->private;
937 if ((sector_num + nb_sectors) <= s->total_sectors)
938 return nb_sectors;
939 if (sector_num >= s->total_sectors)
940 n1 = 0;
941 else
942 n1 = s->total_sectors - sector_num;
943 memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
944 return n1;
945 }
947 /**
948 * Reads a number of sectors from the image (synchronous)
949 */
950 static int qcow_read(struct disk_driver *bs, uint64_t sector_num,
951 uint8_t *buf, int nb_sectors)
952 {
953 BDRVQcowState *s = bs->private;
954 int ret, index_in_cluster, n, n1;
955 uint64_t cluster_offset;
957 while (nb_sectors > 0) {
958 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
959 index_in_cluster = sector_num & (s->cluster_sectors - 1);
960 n = s->cluster_sectors - index_in_cluster;
961 if (n > nb_sectors)
962 n = nb_sectors;
963 if (!cluster_offset) {
965 if (bs->next) {
967 /* Read from backing file */
968 struct disk_driver *parent = bs->next;
970 ret = qcow_sync_read(parent, sector_num,
971 nb_sectors, (char*) buf, NULL, 0, NULL);
973 #if 0
974 /* read from the base image */
975 n1 = backing_read1(s->backing_hd, sector_num, buf, n);
976 if (n1 > 0) {
977 ret = bdrv_read(((BDRVQcowState*) s->backing_hd)->fd, sector_num, buf, n1);
978 if (ret < 0) {
979 DPRINTF("read from backing file failed: ret = %d; errno = %d\n", ret, errno);
980 return -1;
981 }
982 }
983 #endif
984 } else {
985 memset(buf, 0, 512 * n);
986 }
987 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
988 if (decompress_cluster(s, cluster_offset) < 0) {
989 DPRINTF("read/decompression failed: errno = %d\n", errno);
990 return -1;
991 }
992 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
993 } else {
994 ret = bdrv_pread(s->fd, cluster_offset + index_in_cluster * 512, buf, n * 512);
995 if (ret != n * 512) {
996 DPRINTF("read failed: ret = %d != n * 512 = %d; errno = %d\n", ret, n * 512, errno);
997 DPRINTF(" cluster_offset = %"PRIx64", index = %d; sector_num = %"PRId64"", cluster_offset, index_in_cluster, sector_num);
998 return -1;
999 }
1001 if (s->crypt_method) {
1002 encrypt_sectors(s, sector_num, buf, buf, n, 0,
1003 &s->aes_decrypt_key);
1006 nb_sectors -= n;
1007 sector_num += n;
1008 buf += n * 512;
1010 return 0;
1013 /**
1014 * Writes a number of sectors to the image (synchronous)
1015 */
1016 static int qcow_write(struct disk_driver *bs, uint64_t sector_num,
1017 const uint8_t *buf, int nb_sectors)
1019 BDRVQcowState *s = bs->private;
1020 int ret, index_in_cluster, n;
1021 uint64_t cluster_offset;
1023 while (nb_sectors > 0) {
1024 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1025 n = s->cluster_sectors - index_in_cluster;
1026 if (n > nb_sectors)
1027 n = nb_sectors;
1028 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
1029 index_in_cluster,
1030 index_in_cluster + n);
1031 if (!cluster_offset) {
1032 DPRINTF("qcow_write: cluster_offset == 0\n");
1033 DPRINTF(" index = %d; sector_num = %"PRId64"\n",
1034 index_in_cluster, sector_num);
1035 return -1;
1038 if (s->crypt_method) {
1039 encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1040 &s->aes_encrypt_key);
1041 ret = bdrv_pwrite(s->fd, cluster_offset + index_in_cluster * 512,
1042 s->cluster_data, n * 512);
1043 } else {
1044 ret = bdrv_pwrite(s->fd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1046 if (ret != n * 512) {
1047 DPRINTF("write failed: ret = %d != n * 512 = %d; errno = %d\n", ret, n * 512, errno);
1048 DPRINTF(" cluster_offset = %"PRIx64", index = %d; sector_num = %"PRId64"\n", cluster_offset, index_in_cluster, sector_num);
1049 return -1;
1052 nb_sectors -= n;
1053 sector_num += n;
1054 buf += n * 512;
1056 s->cluster_cache_offset = -1; /* disable compressed cache */
1057 return 0;
1062 #ifdef USE_AIO
1064 /*
1065 * QCOW2 specific AIO functions
1066 */
1068 static int qcow_queue_read(struct disk_driver *bs, uint64_t sector,
1069 int nb_sectors, char *buf, td_callback_t cb,
1070 int id, void *private)
1072 BDRVQcowState *s = bs->private;
1073 int i, index_in_cluster, n, ret;
1074 int rsp = 0;
1075 uint64_t cluster_offset;
1077 /*Check we can get a lock*/
1078 for (i = 0; i < nb_sectors; i++)
1079 if (!tap_aio_can_lock(&s->async, sector + i))
1080 return cb(bs, -EBUSY, sector, nb_sectors, id, private);
1082 while (nb_sectors > 0) {
1084 cluster_offset = get_cluster_offset(bs, sector << 9, 0, 0, 0, 0);
1086 index_in_cluster = sector & (s->cluster_sectors - 1);
1087 n = s->cluster_sectors - index_in_cluster;
1088 if (n > nb_sectors)
1089 n = nb_sectors;
1091 if (s->async.iocb_free_count == 0 || !tap_aio_lock(&s->async, sector))
1092 return cb(bs, -EBUSY, sector, nb_sectors, id, private);
1094 if (!cluster_offset) {
1096 /* The requested sector is not allocated */
1097 tap_aio_unlock(&s->async, sector);
1098 ret = cb(bs, BLK_NOT_ALLOCATED,
1099 sector, n, id, private);
1100 if (ret == -EBUSY) {
1101 /* mark remainder of request
1102 * as busy and try again later */
1103 return cb(bs, -EBUSY, sector + n,
1104 nb_sectors - n, id, private);
1105 } else {
1106 rsp += ret;
1109 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1111 /* sync read for compressed clusters */
1112 tap_aio_unlock(&s->async, sector);
1113 if (decompress_cluster(s, cluster_offset) < 0) {
1114 rsp += cb(bs, -EIO, sector, nb_sectors, id, private);
1115 goto done;
1117 memcpy(buf, s->cluster_cache + index_in_cluster * 512,
1118 512 * n);
1119 rsp += cb(bs, 0, sector, n, id, private);
1121 } else {
1123 /* async read */
1124 tap_aio_read(&s->async, s->fd, n * 512,
1125 (cluster_offset + index_in_cluster * 512),
1126 buf, cb, id, sector, private);
1129 /* Prepare for next sector to read */
1130 nb_sectors -= n;
1131 sector += n;
1132 buf += n * 512;
1135 done:
1136 return rsp;
1140 static int qcow_queue_write(struct disk_driver *bs, uint64_t sector,
1141 int nb_sectors, char *buf, td_callback_t cb,
1142 int id, void *private)
1144 BDRVQcowState *s = bs->private;
1145 int i, n, index_in_cluster;
1146 uint64_t cluster_offset;
1147 const uint8_t *src_buf;
1150 /*Check we can get a lock*/
1151 for (i = 0; i < nb_sectors; i++)
1152 if (!tap_aio_can_lock(&s->async, sector + i))
1153 return cb(bs, -EBUSY, sector, nb_sectors, id, private);
1156 while (nb_sectors > 0) {
1158 index_in_cluster = sector & (s->cluster_sectors - 1);
1159 n = s->cluster_sectors - index_in_cluster;
1160 if (n > nb_sectors)
1161 n = nb_sectors;
1163 if (s->async.iocb_free_count == 0 || !tap_aio_lock(&s->async, sector))
1164 return cb(bs, -EBUSY, sector, nb_sectors, id, private);
1167 cluster_offset = get_cluster_offset(bs, sector << 9, 1, 0,
1168 index_in_cluster,
1169 index_in_cluster+n);
1171 if (!cluster_offset) {
1172 DPRINTF("Ooops, no write cluster offset!\n");
1173 tap_aio_unlock(&s->async, sector);
1174 return cb(bs, -EIO, sector, nb_sectors, id, private);
1178 // TODO Encryption
1180 tap_aio_write(&s->async, s->fd, n * 512,
1181 (cluster_offset + index_in_cluster*512),
1182 buf, cb, id, sector, private);
1184 /* Prepare for next sector to write */
1185 nb_sectors -= n;
1186 sector += n;
1187 buf += n * 512;
1191 s->cluster_cache_offset = -1; /* disable compressed cache */
1193 return 0;
1197 #endif /* USE_AIO */
1200 static int qcow_close(struct disk_driver *bs)
1202 BDRVQcowState *s = bs->private;
1204 #ifdef USE_AIO
1205 io_destroy(s->async.aio_ctx.aio_ctx);
1206 tap_aio_free(&s->async);
1207 #else
1208 close(s->poll_pipe[0]);
1209 close(s->poll_pipe[1]);
1210 #endif
1212 qemu_free(s->l1_table);
1213 qemu_free(s->l2_cache);
1214 qemu_free(s->cluster_cache);
1215 qemu_free(s->cluster_data);
1216 refcount_close(bs);
1217 return close(s->fd);
1220 /* XXX: use std qcow open function ? */
1221 typedef struct QCowCreateState {
1222 int cluster_size;
1223 int cluster_bits;
1224 uint16_t *refcount_block;
1225 uint64_t *refcount_table;
1226 int64_t l1_table_offset;
1227 int64_t refcount_table_offset;
1228 int64_t refcount_block_offset;
1229 } QCowCreateState;
1231 static void create_refcount_update(QCowCreateState *s,
1232 int64_t offset, int64_t size)
1234 int refcount;
1235 int64_t start, last, cluster_offset;
1236 uint16_t *p;
1238 start = offset & ~(s->cluster_size - 1);
1239 last = (offset + size - 1) & ~(s->cluster_size - 1);
1240 for(cluster_offset = start; cluster_offset <= last;
1241 cluster_offset += s->cluster_size) {
1242 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1243 refcount = be16_to_cpu(*p);
1244 refcount++;
1245 *p = cpu_to_be16(refcount);
1249 static int qcow_submit(struct disk_driver *bs)
1251 struct BDRVQcowState *s = (struct BDRVQcowState*) bs->private;
1253 fsync(s->fd);
1254 return tap_aio_submit(&s->async);
1258 /*********************************************************/
1259 /* snapshot support */
1262 static void qcow_free_snapshots(struct disk_driver *bs)
1264 BDRVQcowState *s = bs->private;
1265 int i;
1267 for(i = 0; i < s->nb_snapshots; i++) {
1268 qemu_free(s->snapshots[i].name);
1269 qemu_free(s->snapshots[i].id_str);
1271 qemu_free(s->snapshots);
1272 s->snapshots = NULL;
1273 s->nb_snapshots = 0;
1276 static int qcow_read_snapshots(struct disk_driver *bs)
1278 BDRVQcowState *s = bs->private;
1279 QCowSnapshotHeader h;
1280 QCowSnapshot *sn;
1281 int i, id_str_size, name_size;
1282 int64_t offset;
1283 uint32_t extra_data_size;
1285 offset = s->snapshots_offset;
1286 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1287 if (!s->snapshots)
1288 goto fail;
1289 for(i = 0; i < s->nb_snapshots; i++) {
1290 offset = align_offset(offset, 8);
1291 if (bdrv_pread(s->fd, offset, &h, sizeof(h)) != sizeof(h))
1292 goto fail;
1293 offset += sizeof(h);
1294 sn = s->snapshots + i;
1295 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1296 sn->l1_size = be32_to_cpu(h.l1_size);
1297 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1298 sn->date_sec = be32_to_cpu(h.date_sec);
1299 sn->date_nsec = be32_to_cpu(h.date_nsec);
1300 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1301 extra_data_size = be32_to_cpu(h.extra_data_size);
1303 id_str_size = be16_to_cpu(h.id_str_size);
1304 name_size = be16_to_cpu(h.name_size);
1306 offset += extra_data_size;
1308 sn->id_str = qemu_malloc(id_str_size + 1);
1309 if (!sn->id_str)
1310 goto fail;
1311 if (bdrv_pread(s->fd, offset, sn->id_str, id_str_size) != id_str_size)
1312 goto fail;
1313 offset += id_str_size;
1314 sn->id_str[id_str_size] = '\0';
1316 sn->name = qemu_malloc(name_size + 1);
1317 if (!sn->name)
1318 goto fail;
1319 if (bdrv_pread(s->fd, offset, sn->name, name_size) != name_size)
1320 goto fail;
1321 offset += name_size;
1322 sn->name[name_size] = '\0';
1324 s->snapshots_size = offset - s->snapshots_offset;
1325 return 0;
1326 fail:
1327 qcow_free_snapshots(bs);
1328 return -1;
1332 /*********************************************************/
1333 /* refcount handling */
1335 static int refcount_init(struct disk_driver *bs)
1337 BDRVQcowState *s = bs->private;
1338 int ret, refcount_table_size2, i;
1340 s->refcount_block_cache = qemu_malloc(s->cluster_size);
1341 if (!s->refcount_block_cache)
1342 goto fail;
1343 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
1344 s->refcount_table = qemu_malloc(refcount_table_size2);
1345 if (!s->refcount_table)
1346 goto fail;
1347 if (s->refcount_table_size > 0) {
1348 ret = bdrv_pread(s->fd, s->refcount_table_offset,
1349 s->refcount_table, refcount_table_size2);
1350 if (ret != refcount_table_size2)
1351 goto fail;
1352 for(i = 0; i < s->refcount_table_size; i++)
1353 be64_to_cpus(&s->refcount_table[i]);
1355 return 0;
1356 fail:
1357 return -ENOMEM;
1360 static void refcount_close(struct disk_driver *bs)
1362 BDRVQcowState *s = bs->private;
1363 qemu_free(s->refcount_block_cache);
1364 qemu_free(s->refcount_table);
1368 static int load_refcount_block(struct disk_driver *bs,
1369 int64_t refcount_block_offset)
1371 BDRVQcowState *s = bs->private;
1372 int ret;
1373 ret = bdrv_pread(s->fd, refcount_block_offset, s->refcount_block_cache,
1374 s->cluster_size);
1375 if (ret != s->cluster_size)
1376 return -EIO;
1377 s->refcount_block_cache_offset = refcount_block_offset;
1378 return 0;
1381 static int get_refcount(struct disk_driver *bs, int64_t cluster_index)
1383 BDRVQcowState *s = bs->private;
1384 int refcount_table_index, block_index;
1385 int64_t refcount_block_offset;
1387 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
1388 if (refcount_table_index >= s->refcount_table_size)
1389 return 0;
1390 refcount_block_offset = s->refcount_table[refcount_table_index];
1391 if (!refcount_block_offset)
1392 return 0;
1393 if (refcount_block_offset != s->refcount_block_cache_offset) {
1394 /* better than nothing: return allocated if read error */
1395 if (load_refcount_block(bs, refcount_block_offset) < 0)
1396 return 1;
1398 block_index = cluster_index &
1399 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
1400 return be16_to_cpu(s->refcount_block_cache[block_index]);
1403 /* return < 0 if error */
1404 static int64_t alloc_clusters_noref(struct disk_driver *bs, int64_t size)
1406 BDRVQcowState *s = bs->private;
1407 int i, nb_clusters;
1409 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
1410 for(;;) {
1411 if (get_refcount(bs, s->free_cluster_index) == 0) {
1412 s->free_cluster_index++;
1413 for(i = 1; i < nb_clusters; i++) {
1414 if (get_refcount(bs, s->free_cluster_index) != 0)
1415 goto not_found;
1416 s->free_cluster_index++;
1419 #ifdef DEBUG_ALLOC2
1420 DPRINTF("alloc_clusters: size=%ld -> %ld\n",
1421 size,
1422 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
1423 #endif
1425 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
1426 } else {
1427 not_found:
1428 s->free_cluster_index++;
1433 static int64_t alloc_clusters(struct disk_driver *bs, int64_t size)
1435 int64_t offset;
1437 offset = alloc_clusters_noref(bs, size);
1438 update_refcount(bs, offset, size, 1);
1439 return offset;
1442 /* only used to allocate compressed sectors. We try to allocate
1443 contiguous sectors. size must be <= cluster_size */
1444 static int64_t alloc_bytes(struct disk_driver *bs, int size)
1446 BDRVQcowState *s = bs->private;
1447 int64_t offset, cluster_offset;
1448 int free_in_cluster;
1450 assert(size > 0 && size <= s->cluster_size);
1451 if (s->free_byte_offset == 0) {
1452 s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
1454 redo:
1455 free_in_cluster = s->cluster_size -
1456 (s->free_byte_offset & (s->cluster_size - 1));
1457 if (size <= free_in_cluster) {
1458 /* enough space in current cluster */
1459 offset = s->free_byte_offset;
1460 s->free_byte_offset += size;
1461 free_in_cluster -= size;
1462 if (free_in_cluster == 0)
1463 s->free_byte_offset = 0;
1464 if ((offset & (s->cluster_size - 1)) != 0)
1465 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
1466 } else {
1467 offset = alloc_clusters(bs, s->cluster_size);
1468 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
1469 if ((cluster_offset + s->cluster_size) == offset) {
1470 /* we are lucky: contiguous data */
1471 offset = s->free_byte_offset;
1472 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
1473 s->free_byte_offset += size;
1474 } else {
1475 s->free_byte_offset = offset;
1476 goto redo;
1479 return offset;
1482 static void free_clusters(struct disk_driver *bs,
1483 int64_t offset, int64_t size)
1485 update_refcount(bs, offset, size, -1);
1488 static int grow_refcount_table(struct disk_driver *bs, int min_size)
1490 BDRVQcowState *s = bs->private;
1491 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
1492 uint64_t *new_table;
1493 int64_t table_offset;
1494 uint64_t data64;
1495 uint32_t data32;
1496 int old_table_size;
1497 int64_t old_table_offset;
1499 if (min_size <= s->refcount_table_size)
1500 return 0;
1502 /* compute new table size */
1503 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1504 for(;;) {
1505 if (refcount_table_clusters == 0) {
1506 refcount_table_clusters = 1;
1507 } else {
1508 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
1510 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
1511 if (min_size <= new_table_size)
1512 break;
1515 #ifdef DEBUG_ALLOC2
1516 printf("grow_refcount_table from %d to %d\n",
1517 s->refcount_table_size,
1518 new_table_size);
1519 #endif
1520 new_table_size2 = new_table_size * sizeof(uint64_t);
1521 new_table = qemu_mallocz(new_table_size2);
1522 if (!new_table)
1523 return -ENOMEM;
1524 memcpy(new_table, s->refcount_table,
1525 s->refcount_table_size * sizeof(uint64_t));
1526 for(i = 0; i < s->refcount_table_size; i++)
1527 cpu_to_be64s(&new_table[i]);
1528 /* Note: we cannot update the refcount now to avoid recursion */
1529 table_offset = alloc_clusters_noref(bs, new_table_size2);
1530 ret = bdrv_pwrite(s->fd, table_offset, new_table, new_table_size2);
1531 if (ret != new_table_size2)
1532 goto fail;
1533 for(i = 0; i < s->refcount_table_size; i++)
1534 be64_to_cpus(&new_table[i]);
1536 data64 = cpu_to_be64(table_offset);
1537 if (bdrv_pwrite(s->fd, offsetof(QCowHeader, refcount_table_offset),
1538 &data64, sizeof(data64)) != sizeof(data64))
1539 goto fail;
1540 data32 = cpu_to_be32(refcount_table_clusters);
1541 if (bdrv_pwrite(s->fd, offsetof(QCowHeader, refcount_table_clusters),
1542 &data32, sizeof(data32)) != sizeof(data32))
1543 goto fail;
1544 qemu_free(s->refcount_table);
1545 old_table_offset = s->refcount_table_offset;
1546 old_table_size = s->refcount_table_size;
1547 s->refcount_table = new_table;
1548 s->refcount_table_size = new_table_size;
1549 s->refcount_table_offset = table_offset;
1551 update_refcount(bs, table_offset, new_table_size2, 1);
1552 free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
1553 return 0;
1554 fail:
1555 free_clusters(bs, table_offset, new_table_size2);
1556 qemu_free(new_table);
1557 return -EIO;
1560 /* addend must be 1 or -1 */
1561 /* XXX: cache several refcount block clusters ? */
1562 static int update_cluster_refcount(struct disk_driver *bs,
1563 int64_t cluster_index,
1564 int addend)
1566 BDRVQcowState *s = bs->private;
1567 int64_t offset, refcount_block_offset;
1568 int ret, refcount_table_index, block_index, refcount;
1569 uint64_t data64;
1571 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
1572 if (refcount_table_index >= s->refcount_table_size) {
1573 if (addend < 0)
1574 return -EINVAL;
1575 ret = grow_refcount_table(bs, refcount_table_index + 1);
1576 if (ret < 0)
1577 return ret;
1579 refcount_block_offset = s->refcount_table[refcount_table_index];
1580 if (!refcount_block_offset) {
1581 if (addend < 0)
1582 return -EINVAL;
1583 /* create a new refcount block */
1584 /* Note: we cannot update the refcount now to avoid recursion */
1585 offset = alloc_clusters_noref(bs, s->cluster_size);
1586 memset(s->refcount_block_cache, 0, s->cluster_size);
1587 ret = bdrv_pwrite(s->fd, offset, s->refcount_block_cache, s->cluster_size);
1588 if (ret != s->cluster_size)
1589 return -EINVAL;
1590 s->refcount_table[refcount_table_index] = offset;
1591 data64 = cpu_to_be64(offset);
1592 ret = bdrv_pwrite(s->fd, s->refcount_table_offset +
1593 refcount_table_index * sizeof(uint64_t),
1594 &data64, sizeof(data64));
1595 if (ret != sizeof(data64))
1596 return -EINVAL;
1598 refcount_block_offset = offset;
1599 s->refcount_block_cache_offset = offset;
1600 update_refcount(bs, offset, s->cluster_size, 1);
1601 } else {
1602 if (refcount_block_offset != s->refcount_block_cache_offset) {
1603 if (load_refcount_block(bs, refcount_block_offset) < 0)
1604 return -EIO;
1607 /* we can update the count and save it */
1608 block_index = cluster_index &
1609 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
1610 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
1611 refcount += addend;
1612 if (refcount < 0 || refcount > 0xffff)
1613 return -EINVAL;
1614 if (refcount == 0 && cluster_index < s->free_cluster_index) {
1615 s->free_cluster_index = cluster_index;
1617 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
1618 if (bdrv_pwrite(s->fd,
1619 refcount_block_offset + (block_index << REFCOUNT_SHIFT),
1620 &s->refcount_block_cache[block_index], 2) != 2)
1621 return -EIO;
1622 return refcount;
1625 static void update_refcount(struct disk_driver *bs,
1626 int64_t offset, int64_t length,
1627 int addend)
1629 BDRVQcowState *s = bs->private;
1630 int64_t start, last, cluster_offset;
1632 #ifdef DEBUG_ALLOC2
1633 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
1634 offset, length, addend);
1635 #endif
1636 if (length <= 0)
1637 return;
1638 start = offset & ~(s->cluster_size - 1);
1639 last = (offset + length - 1) & ~(s->cluster_size - 1);
1640 for(cluster_offset = start; cluster_offset <= last;
1641 cluster_offset += s->cluster_size) {
1642 update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
1646 #ifdef DEBUG_ALLOC
1647 static void inc_refcounts(struct disk_driver *bs,
1648 uint16_t *refcount_table,
1649 int refcount_table_size,
1650 int64_t offset, int64_t size)
1652 BDRVQcowState *s = bs->private;
1653 int64_t start, last, cluster_offset;
1654 int k;
1656 if (size <= 0)
1657 return;
1659 start = offset & ~(s->cluster_size - 1);
1660 last = (offset + size - 1) & ~(s->cluster_size - 1);
1661 for(cluster_offset = start; cluster_offset <= last;
1662 cluster_offset += s->cluster_size) {
1663 k = cluster_offset >> s->cluster_bits;
1664 if (k < 0 || k >= refcount_table_size) {
1665 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
1666 } else {
1667 if (++refcount_table[k] == 0) {
1668 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
1674 static int check_refcounts_l1(struct disk_driver *bs,
1675 uint16_t *refcount_table,
1676 int refcount_table_size,
1677 int64_t l1_table_offset, int l1_size,
1678 int check_copied)
1680 BDRVQcowState *s = bs->private;
1681 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
1682 int l2_size, i, j, nb_csectors, refcount;
1684 l2_table = NULL;
1685 l1_size2 = l1_size * sizeof(uint64_t);
1687 inc_refcounts(bs, refcount_table, refcount_table_size,
1688 l1_table_offset, l1_size2);
1690 l1_table = qemu_malloc(l1_size2);
1691 if (!l1_table)
1692 goto fail;
1693 if (bdrv_pread(s->fd, l1_table_offset,
1694 l1_table, l1_size2) != l1_size2)
1695 goto fail;
1696 for(i = 0;i < l1_size; i++)
1697 be64_to_cpus(&l1_table[i]);
1699 l2_size = s->l2_size * sizeof(uint64_t);
1700 l2_table = qemu_malloc(l2_size);
1701 if (!l2_table)
1702 goto fail;
1703 for(i = 0; i < l1_size; i++) {
1704 l2_offset = l1_table[i];
1705 if (l2_offset) {
1706 if (check_copied) {
1707 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
1708 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
1709 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
1710 l2_offset, refcount);
1713 l2_offset &= ~QCOW_OFLAG_COPIED;
1714 if (bdrv_pread(s->fd, l2_offset, l2_table, l2_size) != l2_size)
1715 goto fail;
1716 for(j = 0; j < s->l2_size; j++) {
1717 offset = be64_to_cpu(l2_table[j]);
1718 if (offset != 0) {
1719 if (offset & QCOW_OFLAG_COMPRESSED) {
1720 if (offset & QCOW_OFLAG_COPIED) {
1721 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
1722 offset >> s->cluster_bits);
1723 offset &= ~QCOW_OFLAG_COPIED;
1725 nb_csectors = ((offset >> s->csize_shift) &
1726 s->csize_mask) + 1;
1727 offset &= s->cluster_offset_mask;
1728 inc_refcounts(bs, refcount_table,
1729 refcount_table_size,
1730 offset & ~511, nb_csectors * 512);
1731 } else {
1732 if (check_copied) {
1733 refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
1734 if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
1735 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
1736 offset, refcount);
1739 offset &= ~QCOW_OFLAG_COPIED;
1740 inc_refcounts(bs, refcount_table,
1741 refcount_table_size,
1742 offset, s->cluster_size);
1746 inc_refcounts(bs, refcount_table,
1747 refcount_table_size,
1748 l2_offset,
1749 s->cluster_size);
1752 qemu_free(l1_table);
1753 qemu_free(l2_table);
1754 return 0;
1755 fail:
1756 printf("ERROR: I/O error in check_refcounts_l1\n");
1757 qemu_free(l1_table);
1758 qemu_free(l2_table);
1759 return -EIO;
1762 static void check_refcounts(struct disk_driver *bs)
1764 BDRVQcowState *s = bs->private;
1765 int64_t size;
1766 int nb_clusters, refcount1, refcount2, i;
1767 QCowSnapshot *sn;
1768 uint16_t *refcount_table;
1770 size = bdrv_getlength(s->fd);
1771 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
1772 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
1774 /* header */
1775 inc_refcounts(bs, refcount_table, nb_clusters,
1776 0, s->cluster_size);
1778 check_refcounts_l1(bs, refcount_table, nb_clusters,
1779 s->l1_table_offset, s->l1_size, 1);
1781 /* snapshots */
1782 for(i = 0; i < s->nb_snapshots; i++) {
1783 sn = s->snapshots + i;
1784 check_refcounts_l1(bs, refcount_table, nb_clusters,
1785 sn->l1_table_offset, sn->l1_size, 0);
1787 inc_refcounts(bs, refcount_table, nb_clusters,
1788 s->snapshots_offset, s->snapshots_size);
1790 /* refcount data */
1791 inc_refcounts(bs, refcount_table, nb_clusters,
1792 s->refcount_table_offset,
1793 s->refcount_table_size * sizeof(uint64_t));
1795 for(i = 0; i < s->refcount_table_size; i++) {
1796 int64_t offset;
1797 offset = s->refcount_table[i];
1798 if (offset != 0) {
1799 inc_refcounts(bs, refcount_table, nb_clusters,
1800 offset, s->cluster_size);
1804 /* compare ref counts */
1805 for(i = 0; i < nb_clusters; i++) {
1806 refcount1 = get_refcount(bs, i);
1807 refcount2 = refcount_table[i];
1808 if (refcount1 != refcount2)
1809 printf("ERROR cluster %d refcount=%d reference=%d\n",
1810 i, refcount1, refcount2);
1813 qemu_free(refcount_table);
1815 #endif
1818 /**
1819 * Wrapper for synchronous read.
1820 * This function is called when not using AIO at all (#undef USE_AIO) or
1821 * for accessing the backing file.
1822 */
1823 static int qcow_sync_read(struct disk_driver *dd, uint64_t sector,
1824 int nb_sectors, char *buf, td_callback_t cb,
1825 int id, void *prv)
1827 int ret = qcow_read(dd, sector, (uint8_t*) buf, nb_sectors);
1829 if (cb != NULL) {
1830 return cb(dd, (ret < 0) ? ret : 0, sector, nb_sectors, id, prv);
1831 } else {
1832 return ret;
1836 #ifndef USE_AIO
1837 /**
1838 * Wrapper for synchronous write
1839 */
1840 static int qcow_sync_write(struct disk_driver *dd, uint64_t sector,
1841 int nb_sectors, char *buf, td_callback_t cb,
1842 int id, void *prv)
1844 int ret = qcow_write(dd, sector, (uint8_t*) buf, nb_sectors);
1846 return cb(dd, (ret < 0) ? ret : 0, sector, nb_sectors, id, prv);
1848 #endif
1852 #ifndef USE_AIO
1854 static int qcow_do_callbacks(struct disk_driver *dd, int sid)
1856 return 1;
1859 #else
1861 static int qcow_do_callbacks(struct disk_driver *dd, int sid)
1863 int ret, i, nr_events, rsp = 0,*ptr;
1864 struct io_event *ep;
1865 struct BDRVQcowState *prv = (struct BDRVQcowState*)dd->private;
1867 if (sid > MAX_IOFD) return 1;
1869 nr_events = tap_aio_get_events(&prv->async.aio_ctx);
1871 repeat:
1872 for (ep = prv->async.aio_events, i = nr_events; i-- > 0; ep++) {
1873 struct iocb *io = ep->obj;
1874 struct pending_aio *pio;
1876 pio = &prv->async.pending_aio[(long)io->data];
1878 tap_aio_unlock(&prv->async, pio->sector);
1880 if (prv->crypt_method)
1881 encrypt_sectors(prv, pio->sector,
1882 (unsigned char *)pio->buf,
1883 (unsigned char *)pio->buf,
1884 pio->nb_sectors, 0,
1885 &prv->aes_decrypt_key);
1887 rsp += pio->cb(dd, ep->res == io->u.c.nbytes ? 0 : 1,
1888 pio->sector, pio->nb_sectors,
1889 pio->id, pio->private);
1891 prv->async.iocb_free[prv->async.iocb_free_count++] = io;
1894 if (nr_events) {
1895 nr_events = tap_aio_more_events(&prv->async.aio_ctx);
1896 goto repeat;
1899 tap_aio_continue(&prv->async.aio_ctx);
1901 return rsp;
1904 #endif
1906 static int get_filesize(char *filename, uint64_t *size, struct stat *st)
1908 int fd;
1909 QCowHeader header;
1911 /*Set to the backing file size*/
1912 fd = open(filename, O_RDONLY);
1913 if (fd < 0)
1914 return -1;
1915 if (read(fd, &header, sizeof(header)) < sizeof(header)) {
1916 close(fd);
1917 return -1;
1919 close(fd);
1921 be32_to_cpus(&header.magic);
1922 be32_to_cpus(&header.version);
1923 be64_to_cpus(&header.size);
1924 if (header.magic == QCOW_MAGIC && header.version == QCOW_VERSION) {
1925 *size = header.size >> SECTOR_SHIFT;
1926 return 0;
1929 if(S_ISBLK(st->st_mode)) {
1930 fd = open(filename, O_RDONLY);
1931 if (fd < 0)
1932 return -1;
1933 if (blk_getimagesize(fd, size) != 0) {
1934 close(fd);
1935 return -1;
1937 close(fd);
1938 } else *size = (st->st_size >> SECTOR_SHIFT);
1939 return 0;
1942 /**
1943 * @return
1944 * 0 if parent id successfully retrieved;
1945 * TD_NO_PARENT if no parent exists;
1946 * -errno on error
1947 */
1948 static int qcow_get_parent_id(struct disk_driver *dd, struct disk_id *id)
1950 struct BDRVQcowState* s = (struct BDRVQcowState*) dd->private;
1952 if (s->backing_file[0] == '\0')
1953 return TD_NO_PARENT;
1955 id->name = strdup(s->backing_file);
1956 id->drivertype = DISK_TYPE_AIO;
1958 return 0;
1961 static int qcow_validate_parent(struct disk_driver *child,
1962 struct disk_driver *parent, td_flag_t flags)
1964 struct stat stats;
1965 uint64_t psize, csize;
1967 if (stat(parent->name, &stats))
1968 return -EINVAL;
1969 if (get_filesize(parent->name, &psize, &stats))
1970 return -EINVAL;
1972 if (stat(child->name, &stats))
1973 return -EINVAL;
1974 if (get_filesize(child->name, &csize, &stats))
1975 return -EINVAL;
1977 if (csize != psize)
1978 return -EINVAL;
1980 return 0;
1983 int qcow2_create(const char *filename, uint64_t total_size,
1984 const char *backing_file, int flags)
1986 int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1987 int ret = 0;
1988 QCowHeader header;
1989 uint64_t tmp, offset;
1990 QCowCreateState s1, *s = &s1;
1992 memset(s, 0, sizeof(*s));
1994 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1995 if (fd < 0)
1996 return -1;
1997 memset(&header, 0, sizeof(header));
1998 header.magic = cpu_to_be32(QCOW_MAGIC);
1999 header.version = cpu_to_be32(QCOW_VERSION);
2000 header.size = cpu_to_be64(total_size * 512);
2001 header_size = sizeof(header);
2002 backing_filename_len = 0;
2003 if (backing_file) {
2004 header.backing_file_offset = cpu_to_be64(header_size);
2005 backing_filename_len = strlen(backing_file);
2006 header.backing_file_size = cpu_to_be32(backing_filename_len);
2007 header_size += backing_filename_len;
2009 s->cluster_bits = 12; /* 4 KB clusters */
2010 s->cluster_size = 1 << s->cluster_bits;
2011 header.cluster_bits = cpu_to_be32(s->cluster_bits);
2012 header_size = (header_size + 7) & ~7;
2013 if (flags & BLOCK_FLAG_ENCRYPT) {
2014 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
2015 } else {
2016 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
2018 l2_bits = s->cluster_bits - 3;
2019 shift = s->cluster_bits + l2_bits;
2020 l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
2021 offset = align_offset(header_size, s->cluster_size);
2022 s->l1_table_offset = offset;
2023 header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
2024 header.l1_size = cpu_to_be32(l1_size);
2025 offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
2027 s->refcount_table = qemu_mallocz(s->cluster_size);
2028 s->refcount_block = qemu_mallocz(s->cluster_size);
2030 s->refcount_table_offset = offset;
2031 header.refcount_table_offset = cpu_to_be64(offset);
2032 header.refcount_table_clusters = cpu_to_be32(1);
2033 offset += s->cluster_size;
2035 s->refcount_table[0] = cpu_to_be64(offset);
2036 s->refcount_block_offset = offset;
2037 offset += s->cluster_size;
2039 /* update refcounts */
2040 create_refcount_update(s, 0, header_size);
2041 create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
2042 create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
2043 create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
2045 /* write all the data */
2046 ret = write(fd, &header, sizeof(header));
2047 if (ret < 0)
2048 goto out;
2049 if (backing_file) {
2050 ret = write(fd, backing_file, backing_filename_len);
2051 if (ret < 0)
2052 goto out;
2054 lseek(fd, s->l1_table_offset, SEEK_SET);
2055 tmp = 0;
2056 for(i = 0;i < l1_size; i++) {
2057 ret = write(fd, &tmp, sizeof(tmp));
2058 if (ret < 0)
2059 goto out;
2061 lseek(fd, s->refcount_table_offset, SEEK_SET);
2062 ret = write(fd, s->refcount_table, s->cluster_size);
2063 if (ret < 0)
2064 goto out;
2066 lseek(fd, s->refcount_block_offset, SEEK_SET);
2067 ret = write(fd, s->refcount_block, s->cluster_size);
2068 if (ret < 0)
2069 goto out;
2070 ret = 0;
2072 out:
2073 qemu_free(s->refcount_table);
2074 qemu_free(s->refcount_block);
2075 close(fd);
2076 return ret;
2081 struct tap_disk tapdisk_qcow2 = {
2082 "qcow2",
2083 sizeof(BDRVQcowState),
2084 qcow_open,
2085 #ifdef USE_AIO
2086 qcow_queue_read,
2087 qcow_queue_write,
2088 #else
2089 qcow_sync_read,
2090 qcow_sync_write,
2091 #endif
2092 qcow_submit,
2093 qcow_close,
2094 qcow_do_callbacks,
2095 qcow_get_parent_id,
2096 qcow_validate_parent
2097 };