block-qcow2: keep highest allocated byte (Uri Lublin)
[qemu.git] / block-qcow2.c
blobe016e8f004ee972ecc1e76b3a4fdb8c07313d5dc
1 /*
2 * Block driver for the QCOW version 2 format
4 * Copyright (c) 2004-2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include <zlib.h>
27 #include "aes.h"
28 #include <assert.h>
31 Differences with QCOW:
33 - Support for multiple incremental snapshots.
34 - Memory management by reference counts.
35 - Clusters which have a reference count of one have the bit
36 QCOW_OFLAG_COPIED to optimize write performance.
37 - Size of compressed clusters is stored in sectors to reduce bit usage
38 in the cluster offsets.
39 - Support for storing additional data (such as the VM state) in the
40 snapshots.
41 - If a backing store is used, the cluster size is not constrained
42 (could be backported to QCOW).
43 - L2 tables have always a size of one cluster.
46 //#define DEBUG_ALLOC
47 //#define DEBUG_ALLOC2
49 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50 #define QCOW_VERSION 2
52 #define QCOW_CRYPT_NONE 0
53 #define QCOW_CRYPT_AES 1
55 #define QCOW_MAX_CRYPT_CLUSTERS 32
57 /* indicate that the refcount of the referenced cluster is exactly one. */
58 #define QCOW_OFLAG_COPIED (1LL << 63)
59 /* indicate that the cluster is compressed (they never have the copied flag) */
60 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
62 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
64 typedef struct QCowHeader {
65 uint32_t magic;
66 uint32_t version;
67 uint64_t backing_file_offset;
68 uint32_t backing_file_size;
69 uint32_t cluster_bits;
70 uint64_t size; /* in bytes */
71 uint32_t crypt_method;
72 uint32_t l1_size; /* XXX: save number of clusters instead ? */
73 uint64_t l1_table_offset;
74 uint64_t refcount_table_offset;
75 uint32_t refcount_table_clusters;
76 uint32_t nb_snapshots;
77 uint64_t snapshots_offset;
78 } QCowHeader;
80 typedef struct __attribute__((packed)) QCowSnapshotHeader {
81 /* header is 8 byte aligned */
82 uint64_t l1_table_offset;
84 uint32_t l1_size;
85 uint16_t id_str_size;
86 uint16_t name_size;
88 uint32_t date_sec;
89 uint32_t date_nsec;
91 uint64_t vm_clock_nsec;
93 uint32_t vm_state_size;
94 uint32_t extra_data_size; /* for extension */
95 /* extra data follows */
96 /* id_str follows */
97 /* name follows */
98 } QCowSnapshotHeader;
100 #define L2_CACHE_SIZE 16
102 typedef struct QCowSnapshot {
103 uint64_t l1_table_offset;
104 uint32_t l1_size;
105 char *id_str;
106 char *name;
107 uint32_t vm_state_size;
108 uint32_t date_sec;
109 uint32_t date_nsec;
110 uint64_t vm_clock_nsec;
111 } QCowSnapshot;
113 typedef struct BDRVQcowState {
114 BlockDriverState *hd;
115 int cluster_bits;
116 int cluster_size;
117 int cluster_sectors;
118 int l2_bits;
119 int l2_size;
120 int l1_size;
121 int l1_vm_state_index;
122 int csize_shift;
123 int csize_mask;
124 uint64_t cluster_offset_mask;
125 uint64_t l1_table_offset;
126 uint64_t *l1_table;
127 uint64_t *l2_cache;
128 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
129 uint32_t l2_cache_counts[L2_CACHE_SIZE];
130 uint8_t *cluster_cache;
131 uint8_t *cluster_data;
132 uint64_t cluster_cache_offset;
134 uint64_t *refcount_table;
135 uint64_t refcount_table_offset;
136 uint32_t refcount_table_size;
137 uint64_t refcount_block_cache_offset;
138 uint16_t *refcount_block_cache;
139 int64_t free_cluster_index;
140 int64_t free_byte_offset;
142 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
143 uint32_t crypt_method_header;
144 AES_KEY aes_encrypt_key;
145 AES_KEY aes_decrypt_key;
147 int64_t highest_alloc; /* highest cluester allocated (in clusters) */
149 uint64_t snapshots_offset;
150 int snapshots_size;
151 int nb_snapshots;
152 QCowSnapshot *snapshots;
153 } BDRVQcowState;
155 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
156 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
157 uint8_t *buf, int nb_sectors);
158 static int qcow_read_snapshots(BlockDriverState *bs);
159 static void qcow_free_snapshots(BlockDriverState *bs);
160 static int refcount_init(BlockDriverState *bs);
161 static void refcount_close(BlockDriverState *bs);
162 static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
163 static int update_cluster_refcount(BlockDriverState *bs,
164 int64_t cluster_index,
165 int addend);
166 static void update_refcount(BlockDriverState *bs,
167 int64_t offset, int64_t length,
168 int addend);
169 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
170 static int64_t alloc_bytes(BlockDriverState *bs, int size);
171 static void free_clusters(BlockDriverState *bs,
172 int64_t offset, int64_t size);
173 #ifdef DEBUG_ALLOC
174 static void check_refcounts(BlockDriverState *bs);
175 #endif
176 static void scan_refcount(BlockDriverState *bs, int64_t *high);
179 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
181 const QCowHeader *cow_header = (const void *)buf;
183 if (buf_size >= sizeof(QCowHeader) &&
184 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
185 be32_to_cpu(cow_header->version) == QCOW_VERSION)
186 return 100;
187 else
188 return 0;
191 static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
193 BDRVQcowState *s = bs->opaque;
194 int len, i, shift, ret;
195 QCowHeader header;
197 /* Performance is terrible right now with cache=writethrough due mainly
198 * to reference count updates. If the user does not explicitly specify
199 * a caching type, force to writeback caching.
201 if ((flags & BDRV_O_CACHE_DEF)) {
202 flags |= BDRV_O_CACHE_WB;
203 flags &= ~BDRV_O_CACHE_DEF;
205 ret = bdrv_file_open(&s->hd, filename, flags);
206 if (ret < 0)
207 return ret;
208 if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
209 goto fail;
210 be32_to_cpus(&header.magic);
211 be32_to_cpus(&header.version);
212 be64_to_cpus(&header.backing_file_offset);
213 be32_to_cpus(&header.backing_file_size);
214 be64_to_cpus(&header.size);
215 be32_to_cpus(&header.cluster_bits);
216 be32_to_cpus(&header.crypt_method);
217 be64_to_cpus(&header.l1_table_offset);
218 be32_to_cpus(&header.l1_size);
219 be64_to_cpus(&header.refcount_table_offset);
220 be32_to_cpus(&header.refcount_table_clusters);
221 be64_to_cpus(&header.snapshots_offset);
222 be32_to_cpus(&header.nb_snapshots);
224 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
225 goto fail;
226 if (header.size <= 1 ||
227 header.cluster_bits < 9 ||
228 header.cluster_bits > 16)
229 goto fail;
230 if (header.crypt_method > QCOW_CRYPT_AES)
231 goto fail;
232 s->crypt_method_header = header.crypt_method;
233 if (s->crypt_method_header)
234 bs->encrypted = 1;
235 s->cluster_bits = header.cluster_bits;
236 s->cluster_size = 1 << s->cluster_bits;
237 s->cluster_sectors = 1 << (s->cluster_bits - 9);
238 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
239 s->l2_size = 1 << s->l2_bits;
240 bs->total_sectors = header.size / 512;
241 s->csize_shift = (62 - (s->cluster_bits - 8));
242 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
243 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
244 s->refcount_table_offset = header.refcount_table_offset;
245 s->refcount_table_size =
246 header.refcount_table_clusters << (s->cluster_bits - 3);
248 s->snapshots_offset = header.snapshots_offset;
249 s->nb_snapshots = header.nb_snapshots;
251 /* read the level 1 table */
252 s->l1_size = header.l1_size;
253 shift = s->cluster_bits + s->l2_bits;
254 s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
255 /* the L1 table must contain at least enough entries to put
256 header.size bytes */
257 if (s->l1_size < s->l1_vm_state_index)
258 goto fail;
259 s->l1_table_offset = header.l1_table_offset;
260 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
261 if (!s->l1_table)
262 goto fail;
263 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
264 s->l1_size * sizeof(uint64_t))
265 goto fail;
266 for(i = 0;i < s->l1_size; i++) {
267 be64_to_cpus(&s->l1_table[i]);
269 /* alloc L2 cache */
270 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
271 if (!s->l2_cache)
272 goto fail;
273 s->cluster_cache = qemu_malloc(s->cluster_size);
274 if (!s->cluster_cache)
275 goto fail;
276 /* one more sector for decompressed data alignment */
277 s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
278 + 512);
279 if (!s->cluster_data)
280 goto fail;
281 s->cluster_cache_offset = -1;
283 if (refcount_init(bs) < 0)
284 goto fail;
286 scan_refcount(bs, &s->highest_alloc);
288 /* read the backing file name */
289 if (header.backing_file_offset != 0) {
290 len = header.backing_file_size;
291 if (len > 1023)
292 len = 1023;
293 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
294 goto fail;
295 bs->backing_file[len] = '\0';
297 if (qcow_read_snapshots(bs) < 0)
298 goto fail;
300 #ifdef DEBUG_ALLOC
301 check_refcounts(bs);
302 #endif
303 return 0;
305 fail:
306 qcow_free_snapshots(bs);
307 refcount_close(bs);
308 qemu_free(s->l1_table);
309 qemu_free(s->l2_cache);
310 qemu_free(s->cluster_cache);
311 qemu_free(s->cluster_data);
312 bdrv_delete(s->hd);
313 return -1;
316 static int qcow_set_key(BlockDriverState *bs, const char *key)
318 BDRVQcowState *s = bs->opaque;
319 uint8_t keybuf[16];
320 int len, i;
322 memset(keybuf, 0, 16);
323 len = strlen(key);
324 if (len > 16)
325 len = 16;
326 /* XXX: we could compress the chars to 7 bits to increase
327 entropy */
328 for(i = 0;i < len;i++) {
329 keybuf[i] = key[i];
331 s->crypt_method = s->crypt_method_header;
333 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
334 return -1;
335 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
336 return -1;
337 #if 0
338 /* test */
340 uint8_t in[16];
341 uint8_t out[16];
342 uint8_t tmp[16];
343 for(i=0;i<16;i++)
344 in[i] = i;
345 AES_encrypt(in, tmp, &s->aes_encrypt_key);
346 AES_decrypt(tmp, out, &s->aes_decrypt_key);
347 for(i = 0; i < 16; i++)
348 printf(" %02x", tmp[i]);
349 printf("\n");
350 for(i = 0; i < 16; i++)
351 printf(" %02x", out[i]);
352 printf("\n");
354 #endif
355 return 0;
358 /* The crypt function is compatible with the linux cryptoloop
359 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
360 supported */
361 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
362 uint8_t *out_buf, const uint8_t *in_buf,
363 int nb_sectors, int enc,
364 const AES_KEY *key)
366 union {
367 uint64_t ll[2];
368 uint8_t b[16];
369 } ivec;
370 int i;
372 for(i = 0; i < nb_sectors; i++) {
373 ivec.ll[0] = cpu_to_le64(sector_num);
374 ivec.ll[1] = 0;
375 AES_cbc_encrypt(in_buf, out_buf, 512, key,
376 ivec.b, enc);
377 sector_num++;
378 in_buf += 512;
379 out_buf += 512;
383 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
384 uint64_t cluster_offset, int n_start, int n_end)
386 BDRVQcowState *s = bs->opaque;
387 int n, ret;
389 n = n_end - n_start;
390 if (n <= 0)
391 return 0;
392 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
393 if (ret < 0)
394 return ret;
395 if (s->crypt_method) {
396 encrypt_sectors(s, start_sect + n_start,
397 s->cluster_data,
398 s->cluster_data, n, 1,
399 &s->aes_encrypt_key);
401 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
402 s->cluster_data, n);
403 if (ret < 0)
404 return ret;
405 return 0;
408 static void l2_cache_reset(BlockDriverState *bs)
410 BDRVQcowState *s = bs->opaque;
412 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
413 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
414 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
417 static inline int l2_cache_new_entry(BlockDriverState *bs)
419 BDRVQcowState *s = bs->opaque;
420 uint32_t min_count;
421 int min_index, i;
423 /* find a new entry in the least used one */
424 min_index = 0;
425 min_count = 0xffffffff;
426 for(i = 0; i < L2_CACHE_SIZE; i++) {
427 if (s->l2_cache_counts[i] < min_count) {
428 min_count = s->l2_cache_counts[i];
429 min_index = i;
432 return min_index;
435 static int64_t align_offset(int64_t offset, int n)
437 offset = (offset + n - 1) & ~(n - 1);
438 return offset;
441 static int grow_l1_table(BlockDriverState *bs, int min_size)
443 BDRVQcowState *s = bs->opaque;
444 int new_l1_size, new_l1_size2, ret, i;
445 uint64_t *new_l1_table;
446 uint64_t new_l1_table_offset;
447 uint8_t data[12];
449 new_l1_size = s->l1_size;
450 if (min_size <= new_l1_size)
451 return 0;
452 while (min_size > new_l1_size) {
453 new_l1_size = (new_l1_size * 3 + 1) / 2;
455 #ifdef DEBUG_ALLOC2
456 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
457 #endif
459 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
460 new_l1_table = qemu_mallocz(new_l1_size2);
461 if (!new_l1_table)
462 return -ENOMEM;
463 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
465 /* write new table (align to cluster) */
466 new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
468 for(i = 0; i < s->l1_size; i++)
469 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
470 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
471 if (ret != new_l1_size2)
472 goto fail;
473 for(i = 0; i < s->l1_size; i++)
474 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
476 /* set new table */
477 cpu_to_be32w((uint32_t*)data, new_l1_size);
478 cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
479 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
480 sizeof(data)) != sizeof(data))
481 goto fail;
482 qemu_free(s->l1_table);
483 free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
484 s->l1_table_offset = new_l1_table_offset;
485 s->l1_table = new_l1_table;
486 s->l1_size = new_l1_size;
487 return 0;
488 fail:
489 qemu_free(s->l1_table);
490 return -EIO;
494 * seek_l2_table
496 * seek l2_offset in the l2_cache table
497 * if not found, return NULL,
498 * if found,
499 * increments the l2 cache hit count of the entry,
500 * if counter overflow, divide by two all counters
501 * return the pointer to the l2 cache entry
505 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
507 int i, j;
509 for(i = 0; i < L2_CACHE_SIZE; i++) {
510 if (l2_offset == s->l2_cache_offsets[i]) {
511 /* increment the hit count */
512 if (++s->l2_cache_counts[i] == 0xffffffff) {
513 for(j = 0; j < L2_CACHE_SIZE; j++) {
514 s->l2_cache_counts[j] >>= 1;
517 return s->l2_cache + (i << s->l2_bits);
520 return NULL;
524 * l2_load
526 * Loads a L2 table into memory. If the table is in the cache, the cache
527 * is used; otherwise the L2 table is loaded from the image file.
529 * Returns a pointer to the L2 table on success, or NULL if the read from
530 * the image file failed.
533 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
535 BDRVQcowState *s = bs->opaque;
536 int min_index;
537 uint64_t *l2_table;
539 /* seek if the table for the given offset is in the cache */
541 l2_table = seek_l2_table(s, l2_offset);
542 if (l2_table != NULL)
543 return l2_table;
545 /* not found: load a new entry in the least used one */
547 min_index = l2_cache_new_entry(bs);
548 l2_table = s->l2_cache + (min_index << s->l2_bits);
549 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
550 s->l2_size * sizeof(uint64_t))
551 return NULL;
552 s->l2_cache_offsets[min_index] = l2_offset;
553 s->l2_cache_counts[min_index] = 1;
555 return l2_table;
559 * l2_allocate
561 * Allocate a new l2 entry in the file. If l1_index points to an already
562 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
563 * table) copy the contents of the old L2 table into the newly allocated one.
564 * Otherwise the new table is initialized with zeros.
568 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
570 BDRVQcowState *s = bs->opaque;
571 int min_index;
572 uint64_t old_l2_offset, tmp;
573 uint64_t *l2_table, l2_offset;
575 old_l2_offset = s->l1_table[l1_index];
577 /* allocate a new l2 entry */
579 l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
581 /* update the L1 entry */
583 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
585 tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
586 if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
587 &tmp, sizeof(tmp)) != sizeof(tmp))
588 return NULL;
590 /* allocate a new entry in the l2 cache */
592 min_index = l2_cache_new_entry(bs);
593 l2_table = s->l2_cache + (min_index << s->l2_bits);
595 if (old_l2_offset == 0) {
596 /* if there was no old l2 table, clear the new table */
597 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
598 } else {
599 /* if there was an old l2 table, read it from the disk */
600 if (bdrv_pread(s->hd, old_l2_offset,
601 l2_table, s->l2_size * sizeof(uint64_t)) !=
602 s->l2_size * sizeof(uint64_t))
603 return NULL;
605 /* write the l2 table to the file */
606 if (bdrv_pwrite(s->hd, l2_offset,
607 l2_table, s->l2_size * sizeof(uint64_t)) !=
608 s->l2_size * sizeof(uint64_t))
609 return NULL;
611 /* update the l2 cache entry */
613 s->l2_cache_offsets[min_index] = l2_offset;
614 s->l2_cache_counts[min_index] = 1;
616 return l2_table;
619 static int size_to_clusters(BDRVQcowState *s, int64_t size)
621 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
624 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
625 uint64_t *l2_table, uint64_t start, uint64_t mask)
627 int i;
628 uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
630 if (!offset)
631 return 0;
633 for (i = start; i < start + nb_clusters; i++)
634 if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
635 break;
637 return (i - start);
640 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
642 int i = 0;
644 while(nb_clusters-- && l2_table[i] == 0)
645 i++;
647 return i;
651 * get_cluster_offset
653 * For a given offset of the disk image, return cluster offset in
654 * qcow2 file.
656 * on entry, *num is the number of contiguous clusters we'd like to
657 * access following offset.
659 * on exit, *num is the number of contiguous clusters we can read.
661 * Return 1, if the offset is found
662 * Return 0, otherwise.
666 static uint64_t get_cluster_offset(BlockDriverState *bs,
667 uint64_t offset, int *num)
669 BDRVQcowState *s = bs->opaque;
670 int l1_index, l2_index;
671 uint64_t l2_offset, *l2_table, cluster_offset;
672 int l1_bits, c;
673 int index_in_cluster, nb_available, nb_needed, nb_clusters;
675 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
676 nb_needed = *num + index_in_cluster;
678 l1_bits = s->l2_bits + s->cluster_bits;
680 /* compute how many bytes there are between the offset and
681 * the end of the l1 entry
684 nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
686 /* compute the number of available sectors */
688 nb_available = (nb_available >> 9) + index_in_cluster;
690 cluster_offset = 0;
692 /* seek the the l2 offset in the l1 table */
694 l1_index = offset >> l1_bits;
695 if (l1_index >= s->l1_size)
696 goto out;
698 l2_offset = s->l1_table[l1_index];
700 /* seek the l2 table of the given l2 offset */
702 if (!l2_offset)
703 goto out;
705 /* load the l2 table in memory */
707 l2_offset &= ~QCOW_OFLAG_COPIED;
708 l2_table = l2_load(bs, l2_offset);
709 if (l2_table == NULL)
710 return 0;
712 /* find the cluster offset for the given disk offset */
714 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
715 cluster_offset = be64_to_cpu(l2_table[l2_index]);
716 nb_clusters = size_to_clusters(s, nb_needed << 9);
718 if (!cluster_offset) {
719 /* how many empty clusters ? */
720 c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
721 } else {
722 /* how many allocated clusters ? */
723 c = count_contiguous_clusters(nb_clusters, s->cluster_size,
724 &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
727 nb_available = (c * s->cluster_sectors);
728 out:
729 if (nb_available > nb_needed)
730 nb_available = nb_needed;
732 *num = nb_available - index_in_cluster;
734 return cluster_offset & ~QCOW_OFLAG_COPIED;
738 * free_any_clusters
740 * free clusters according to its type: compressed or not
744 static void free_any_clusters(BlockDriverState *bs,
745 uint64_t cluster_offset, int nb_clusters)
747 BDRVQcowState *s = bs->opaque;
749 /* free the cluster */
751 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
752 int nb_csectors;
753 nb_csectors = ((cluster_offset >> s->csize_shift) &
754 s->csize_mask) + 1;
755 free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
756 nb_csectors * 512);
757 return;
760 free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
762 return;
766 * get_cluster_table
768 * for a given disk offset, load (and allocate if needed)
769 * the l2 table.
771 * the l2 table offset in the qcow2 file and the cluster index
772 * in the l2 table are given to the caller.
776 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
777 uint64_t **new_l2_table,
778 uint64_t *new_l2_offset,
779 int *new_l2_index)
781 BDRVQcowState *s = bs->opaque;
782 int l1_index, l2_index, ret;
783 uint64_t l2_offset, *l2_table;
785 /* seek the the l2 offset in the l1 table */
787 l1_index = offset >> (s->l2_bits + s->cluster_bits);
788 if (l1_index >= s->l1_size) {
789 ret = grow_l1_table(bs, l1_index + 1);
790 if (ret < 0)
791 return 0;
793 l2_offset = s->l1_table[l1_index];
795 /* seek the l2 table of the given l2 offset */
797 if (l2_offset & QCOW_OFLAG_COPIED) {
798 /* load the l2 table in memory */
799 l2_offset &= ~QCOW_OFLAG_COPIED;
800 l2_table = l2_load(bs, l2_offset);
801 if (l2_table == NULL)
802 return 0;
803 } else {
804 if (l2_offset)
805 free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
806 l2_table = l2_allocate(bs, l1_index);
807 if (l2_table == NULL)
808 return 0;
809 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
812 /* find the cluster offset for the given disk offset */
814 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
816 *new_l2_table = l2_table;
817 *new_l2_offset = l2_offset;
818 *new_l2_index = l2_index;
820 return 1;
824 * alloc_compressed_cluster_offset
826 * For a given offset of the disk image, return cluster offset in
827 * qcow2 file.
829 * If the offset is not found, allocate a new compressed cluster.
831 * Return the cluster offset if successful,
832 * Return 0, otherwise.
836 static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
837 uint64_t offset,
838 int compressed_size)
840 BDRVQcowState *s = bs->opaque;
841 int l2_index, ret;
842 uint64_t l2_offset, *l2_table, cluster_offset;
843 int nb_csectors;
845 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
846 if (ret == 0)
847 return 0;
849 cluster_offset = be64_to_cpu(l2_table[l2_index]);
850 if (cluster_offset & QCOW_OFLAG_COPIED)
851 return cluster_offset & ~QCOW_OFLAG_COPIED;
853 if (cluster_offset)
854 free_any_clusters(bs, cluster_offset, 1);
856 cluster_offset = alloc_bytes(bs, compressed_size);
857 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
858 (cluster_offset >> 9);
860 cluster_offset |= QCOW_OFLAG_COMPRESSED |
861 ((uint64_t)nb_csectors << s->csize_shift);
863 /* update L2 table */
865 /* compressed clusters never have the copied flag */
867 l2_table[l2_index] = cpu_to_be64(cluster_offset);
868 if (bdrv_pwrite(s->hd,
869 l2_offset + l2_index * sizeof(uint64_t),
870 l2_table + l2_index,
871 sizeof(uint64_t)) != sizeof(uint64_t))
872 return 0;
874 return cluster_offset;
877 typedef struct QCowL2Meta
879 uint64_t offset;
880 int n_start;
881 int nb_available;
882 int nb_clusters;
883 } QCowL2Meta;
885 static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
886 QCowL2Meta *m)
888 BDRVQcowState *s = bs->opaque;
889 int i, j = 0, l2_index, ret;
890 uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
892 if (m->nb_clusters == 0)
893 return 0;
895 if (!(old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t))))
896 return -ENOMEM;
898 /* copy content of unmodified sectors */
899 start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
900 if (m->n_start) {
901 ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
902 if (ret < 0)
903 goto err;
906 if (m->nb_available & (s->cluster_sectors - 1)) {
907 uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
908 ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
909 m->nb_available - end, s->cluster_sectors);
910 if (ret < 0)
911 goto err;
914 ret = -EIO;
915 /* update L2 table */
916 if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
917 goto err;
919 for (i = 0; i < m->nb_clusters; i++) {
920 if(l2_table[l2_index + i] != 0)
921 old_cluster[j++] = l2_table[l2_index + i];
923 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
924 (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
927 if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),
928 l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=
929 m->nb_clusters * sizeof(uint64_t))
930 goto err;
932 for (i = 0; i < j; i++)
933 free_any_clusters(bs, old_cluster[i], 1);
935 ret = 0;
936 err:
937 qemu_free(old_cluster);
938 return ret;
942 * alloc_cluster_offset
944 * For a given offset of the disk image, return cluster offset in
945 * qcow2 file.
947 * If the offset is not found, allocate a new cluster.
949 * Return the cluster offset if successful,
950 * Return 0, otherwise.
954 static uint64_t alloc_cluster_offset(BlockDriverState *bs,
955 uint64_t offset,
956 int n_start, int n_end,
957 int *num, QCowL2Meta *m)
959 BDRVQcowState *s = bs->opaque;
960 int l2_index, ret;
961 uint64_t l2_offset, *l2_table, cluster_offset;
962 int nb_clusters, i = 0;
964 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
965 if (ret == 0)
966 return 0;
968 nb_clusters = size_to_clusters(s, n_end << 9);
970 nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
972 cluster_offset = be64_to_cpu(l2_table[l2_index]);
974 /* We keep all QCOW_OFLAG_COPIED clusters */
976 if (cluster_offset & QCOW_OFLAG_COPIED) {
977 nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
978 &l2_table[l2_index], 0, 0);
980 cluster_offset &= ~QCOW_OFLAG_COPIED;
981 m->nb_clusters = 0;
983 goto out;
986 /* for the moment, multiple compressed clusters are not managed */
988 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
989 nb_clusters = 1;
991 /* how many available clusters ? */
993 while (i < nb_clusters) {
994 i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
995 &l2_table[l2_index], i, 0);
997 if(be64_to_cpu(l2_table[l2_index + i]))
998 break;
1000 i += count_contiguous_free_clusters(nb_clusters - i,
1001 &l2_table[l2_index + i]);
1003 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
1005 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
1006 (cluster_offset & QCOW_OFLAG_COMPRESSED))
1007 break;
1009 nb_clusters = i;
1011 /* allocate a new cluster */
1013 cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
1015 /* save info needed for meta data update */
1016 m->offset = offset;
1017 m->n_start = n_start;
1018 m->nb_clusters = nb_clusters;
1020 out:
1021 m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
1023 *num = m->nb_available - n_start;
1025 return cluster_offset;
1028 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
1029 int nb_sectors, int *pnum)
1031 uint64_t cluster_offset;
1033 *pnum = nb_sectors;
1034 cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
1036 return (cluster_offset != 0);
1039 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1040 const uint8_t *buf, int buf_size)
1042 z_stream strm1, *strm = &strm1;
1043 int ret, out_len;
1045 memset(strm, 0, sizeof(*strm));
1047 strm->next_in = (uint8_t *)buf;
1048 strm->avail_in = buf_size;
1049 strm->next_out = out_buf;
1050 strm->avail_out = out_buf_size;
1052 ret = inflateInit2(strm, -12);
1053 if (ret != Z_OK)
1054 return -1;
1055 ret = inflate(strm, Z_FINISH);
1056 out_len = strm->next_out - out_buf;
1057 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1058 out_len != out_buf_size) {
1059 inflateEnd(strm);
1060 return -1;
1062 inflateEnd(strm);
1063 return 0;
1066 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1068 int ret, csize, nb_csectors, sector_offset;
1069 uint64_t coffset;
1071 coffset = cluster_offset & s->cluster_offset_mask;
1072 if (s->cluster_cache_offset != coffset) {
1073 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1074 sector_offset = coffset & 511;
1075 csize = nb_csectors * 512 - sector_offset;
1076 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1077 if (ret < 0) {
1078 return -1;
1080 if (decompress_buffer(s->cluster_cache, s->cluster_size,
1081 s->cluster_data + sector_offset, csize) < 0) {
1082 return -1;
1084 s->cluster_cache_offset = coffset;
1086 return 0;
1089 /* handle reading after the end of the backing file */
1090 static int backing_read1(BlockDriverState *bs,
1091 int64_t sector_num, uint8_t *buf, int nb_sectors)
1093 int n1;
1094 if ((sector_num + nb_sectors) <= bs->total_sectors)
1095 return nb_sectors;
1096 if (sector_num >= bs->total_sectors)
1097 n1 = 0;
1098 else
1099 n1 = bs->total_sectors - sector_num;
1100 memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1101 return n1;
1104 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
1105 uint8_t *buf, int nb_sectors)
1107 BDRVQcowState *s = bs->opaque;
1108 int ret, index_in_cluster, n, n1;
1109 uint64_t cluster_offset;
1111 while (nb_sectors > 0) {
1112 n = nb_sectors;
1113 cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
1114 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1115 if (!cluster_offset) {
1116 if (bs->backing_hd) {
1117 /* read from the base image */
1118 n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1119 if (n1 > 0) {
1120 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1121 if (ret < 0)
1122 return -1;
1124 } else {
1125 memset(buf, 0, 512 * n);
1127 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1128 if (decompress_cluster(s, cluster_offset) < 0)
1129 return -1;
1130 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1131 } else {
1132 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1133 if (ret != n * 512)
1134 return -1;
1135 if (s->crypt_method) {
1136 encrypt_sectors(s, sector_num, buf, buf, n, 0,
1137 &s->aes_decrypt_key);
1140 nb_sectors -= n;
1141 sector_num += n;
1142 buf += n * 512;
1144 return 0;
1147 static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1148 const uint8_t *buf, int nb_sectors)
1150 BDRVQcowState *s = bs->opaque;
1151 int ret, index_in_cluster, n;
1152 uint64_t cluster_offset;
1153 int n_end;
1154 QCowL2Meta l2meta;
1156 while (nb_sectors > 0) {
1157 index_in_cluster = sector_num & (s->cluster_sectors - 1);
1158 n_end = index_in_cluster + nb_sectors;
1159 if (s->crypt_method &&
1160 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1161 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1162 cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1163 index_in_cluster,
1164 n_end, &n, &l2meta);
1165 if (!cluster_offset)
1166 return -1;
1167 if (s->crypt_method) {
1168 encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1169 &s->aes_encrypt_key);
1170 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1171 s->cluster_data, n * 512);
1172 } else {
1173 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1175 if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) {
1176 free_any_clusters(bs, cluster_offset, l2meta.nb_clusters);
1177 return -1;
1179 nb_sectors -= n;
1180 sector_num += n;
1181 buf += n * 512;
1183 s->cluster_cache_offset = -1; /* disable compressed cache */
1184 return 0;
1187 typedef struct QCowAIOCB {
1188 BlockDriverAIOCB common;
1189 int64_t sector_num;
1190 uint8_t *buf;
1191 int nb_sectors;
1192 int n;
1193 uint64_t cluster_offset;
1194 uint8_t *cluster_data;
1195 BlockDriverAIOCB *hd_aiocb;
1196 QEMUBH *bh;
1197 QCowL2Meta l2meta;
1198 } QCowAIOCB;
1200 static void qcow_aio_read_cb(void *opaque, int ret);
1201 static void qcow_aio_read_bh(void *opaque)
1203 QCowAIOCB *acb = opaque;
1204 qemu_bh_delete(acb->bh);
1205 acb->bh = NULL;
1206 qcow_aio_read_cb(opaque, 0);
1209 static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
1211 if (acb->bh)
1212 return -EIO;
1214 acb->bh = qemu_bh_new(cb, acb);
1215 if (!acb->bh)
1216 return -EIO;
1218 qemu_bh_schedule(acb->bh);
1220 return 0;
1223 static void qcow_aio_read_cb(void *opaque, int ret)
1225 QCowAIOCB *acb = opaque;
1226 BlockDriverState *bs = acb->common.bs;
1227 BDRVQcowState *s = bs->opaque;
1228 int index_in_cluster, n1;
1230 acb->hd_aiocb = NULL;
1231 if (ret < 0) {
1232 fail:
1233 acb->common.cb(acb->common.opaque, ret);
1234 qemu_aio_release(acb);
1235 return;
1238 /* post process the read buffer */
1239 if (!acb->cluster_offset) {
1240 /* nothing to do */
1241 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1242 /* nothing to do */
1243 } else {
1244 if (s->crypt_method) {
1245 encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1246 acb->n, 0,
1247 &s->aes_decrypt_key);
1251 acb->nb_sectors -= acb->n;
1252 acb->sector_num += acb->n;
1253 acb->buf += acb->n * 512;
1255 if (acb->nb_sectors == 0) {
1256 /* request completed */
1257 acb->common.cb(acb->common.opaque, 0);
1258 qemu_aio_release(acb);
1259 return;
1262 /* prepare next AIO request */
1263 acb->n = acb->nb_sectors;
1264 acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
1265 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1267 if (!acb->cluster_offset) {
1268 if (bs->backing_hd) {
1269 /* read from the base image */
1270 n1 = backing_read1(bs->backing_hd, acb->sector_num,
1271 acb->buf, acb->n);
1272 if (n1 > 0) {
1273 acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
1274 acb->buf, acb->n, qcow_aio_read_cb, acb);
1275 if (acb->hd_aiocb == NULL)
1276 goto fail;
1277 } else {
1278 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1279 if (ret < 0)
1280 goto fail;
1282 } else {
1283 /* Note: in this case, no need to wait */
1284 memset(acb->buf, 0, 512 * acb->n);
1285 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1286 if (ret < 0)
1287 goto fail;
1289 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1290 /* add AIO support for compressed blocks ? */
1291 if (decompress_cluster(s, acb->cluster_offset) < 0)
1292 goto fail;
1293 memcpy(acb->buf,
1294 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1295 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1296 if (ret < 0)
1297 goto fail;
1298 } else {
1299 if ((acb->cluster_offset & 511) != 0) {
1300 ret = -EIO;
1301 goto fail;
1303 acb->hd_aiocb = bdrv_aio_read(s->hd,
1304 (acb->cluster_offset >> 9) + index_in_cluster,
1305 acb->buf, acb->n, qcow_aio_read_cb, acb);
1306 if (acb->hd_aiocb == NULL)
1307 goto fail;
1311 static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1312 int64_t sector_num, uint8_t *buf, int nb_sectors,
1313 BlockDriverCompletionFunc *cb, void *opaque)
1315 QCowAIOCB *acb;
1317 acb = qemu_aio_get(bs, cb, opaque);
1318 if (!acb)
1319 return NULL;
1320 acb->hd_aiocb = NULL;
1321 acb->sector_num = sector_num;
1322 acb->buf = buf;
1323 acb->nb_sectors = nb_sectors;
1324 acb->n = 0;
1325 acb->cluster_offset = 0;
1326 acb->l2meta.nb_clusters = 0;
1327 return acb;
1330 static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1331 int64_t sector_num, uint8_t *buf, int nb_sectors,
1332 BlockDriverCompletionFunc *cb, void *opaque)
1334 QCowAIOCB *acb;
1336 acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1337 if (!acb)
1338 return NULL;
1340 qcow_aio_read_cb(acb, 0);
1341 return &acb->common;
1344 static void qcow_aio_write_cb(void *opaque, int ret)
1346 QCowAIOCB *acb = opaque;
1347 BlockDriverState *bs = acb->common.bs;
1348 BDRVQcowState *s = bs->opaque;
1349 int index_in_cluster;
1350 const uint8_t *src_buf;
1351 int n_end;
1353 acb->hd_aiocb = NULL;
1355 if (ret < 0) {
1356 fail:
1357 acb->common.cb(acb->common.opaque, ret);
1358 qemu_aio_release(acb);
1359 return;
1362 if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
1363 free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
1364 goto fail;
1367 acb->nb_sectors -= acb->n;
1368 acb->sector_num += acb->n;
1369 acb->buf += acb->n * 512;
1371 if (acb->nb_sectors == 0) {
1372 /* request completed */
1373 acb->common.cb(acb->common.opaque, 0);
1374 qemu_aio_release(acb);
1375 return;
1378 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1379 n_end = index_in_cluster + acb->nb_sectors;
1380 if (s->crypt_method &&
1381 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1382 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1384 acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
1385 index_in_cluster,
1386 n_end, &acb->n, &acb->l2meta);
1387 if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
1388 ret = -EIO;
1389 goto fail;
1391 if (s->crypt_method) {
1392 if (!acb->cluster_data) {
1393 acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1394 s->cluster_size);
1395 if (!acb->cluster_data) {
1396 ret = -ENOMEM;
1397 goto fail;
1400 encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
1401 acb->n, 1, &s->aes_encrypt_key);
1402 src_buf = acb->cluster_data;
1403 } else {
1404 src_buf = acb->buf;
1406 acb->hd_aiocb = bdrv_aio_write(s->hd,
1407 (acb->cluster_offset >> 9) + index_in_cluster,
1408 src_buf, acb->n,
1409 qcow_aio_write_cb, acb);
1410 if (acb->hd_aiocb == NULL)
1411 goto fail;
1414 static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1415 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1416 BlockDriverCompletionFunc *cb, void *opaque)
1418 BDRVQcowState *s = bs->opaque;
1419 QCowAIOCB *acb;
1421 s->cluster_cache_offset = -1; /* disable compressed cache */
1423 acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1424 if (!acb)
1425 return NULL;
1427 qcow_aio_write_cb(acb, 0);
1428 return &acb->common;
1431 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1433 QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1434 if (acb->hd_aiocb)
1435 bdrv_aio_cancel(acb->hd_aiocb);
1436 qemu_aio_release(acb);
1439 static void qcow_close(BlockDriverState *bs)
1441 BDRVQcowState *s = bs->opaque;
1442 qemu_free(s->l1_table);
1443 qemu_free(s->l2_cache);
1444 qemu_free(s->cluster_cache);
1445 qemu_free(s->cluster_data);
1446 refcount_close(bs);
1447 bdrv_delete(s->hd);
1450 /* XXX: use std qcow open function ? */
1451 typedef struct QCowCreateState {
1452 int cluster_size;
1453 int cluster_bits;
1454 uint16_t *refcount_block;
1455 uint64_t *refcount_table;
1456 int64_t l1_table_offset;
1457 int64_t refcount_table_offset;
1458 int64_t refcount_block_offset;
1459 } QCowCreateState;
1461 static void create_refcount_update(QCowCreateState *s,
1462 int64_t offset, int64_t size)
1464 int refcount;
1465 int64_t start, last, cluster_offset;
1466 uint16_t *p;
1468 start = offset & ~(s->cluster_size - 1);
1469 last = (offset + size - 1) & ~(s->cluster_size - 1);
1470 for(cluster_offset = start; cluster_offset <= last;
1471 cluster_offset += s->cluster_size) {
1472 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1473 refcount = be16_to_cpu(*p);
1474 refcount++;
1475 *p = cpu_to_be16(refcount);
1479 static int qcow_create(const char *filename, int64_t total_size,
1480 const char *backing_file, int flags)
1482 int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1483 QCowHeader header;
1484 uint64_t tmp, offset;
1485 QCowCreateState s1, *s = &s1;
1487 memset(s, 0, sizeof(*s));
1489 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1490 if (fd < 0)
1491 return -1;
1492 memset(&header, 0, sizeof(header));
1493 header.magic = cpu_to_be32(QCOW_MAGIC);
1494 header.version = cpu_to_be32(QCOW_VERSION);
1495 header.size = cpu_to_be64(total_size * 512);
1496 header_size = sizeof(header);
1497 backing_filename_len = 0;
1498 if (backing_file) {
1499 header.backing_file_offset = cpu_to_be64(header_size);
1500 backing_filename_len = strlen(backing_file);
1501 header.backing_file_size = cpu_to_be32(backing_filename_len);
1502 header_size += backing_filename_len;
1504 s->cluster_bits = 12; /* 4 KB clusters */
1505 s->cluster_size = 1 << s->cluster_bits;
1506 header.cluster_bits = cpu_to_be32(s->cluster_bits);
1507 header_size = (header_size + 7) & ~7;
1508 if (flags & BLOCK_FLAG_ENCRYPT) {
1509 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1510 } else {
1511 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1513 l2_bits = s->cluster_bits - 3;
1514 shift = s->cluster_bits + l2_bits;
1515 l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1516 offset = align_offset(header_size, s->cluster_size);
1517 s->l1_table_offset = offset;
1518 header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1519 header.l1_size = cpu_to_be32(l1_size);
1520 offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1522 s->refcount_table = qemu_mallocz(s->cluster_size);
1523 if (!s->refcount_table)
1524 goto fail;
1525 s->refcount_block = qemu_mallocz(s->cluster_size);
1526 if (!s->refcount_block)
1527 goto fail;
1529 s->refcount_table_offset = offset;
1530 header.refcount_table_offset = cpu_to_be64(offset);
1531 header.refcount_table_clusters = cpu_to_be32(1);
1532 offset += s->cluster_size;
1534 s->refcount_table[0] = cpu_to_be64(offset);
1535 s->refcount_block_offset = offset;
1536 offset += s->cluster_size;
1538 /* update refcounts */
1539 create_refcount_update(s, 0, header_size);
1540 create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1541 create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1542 create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
1544 /* write all the data */
1545 write(fd, &header, sizeof(header));
1546 if (backing_file) {
1547 write(fd, backing_file, backing_filename_len);
1549 lseek(fd, s->l1_table_offset, SEEK_SET);
1550 tmp = 0;
1551 for(i = 0;i < l1_size; i++) {
1552 write(fd, &tmp, sizeof(tmp));
1554 lseek(fd, s->refcount_table_offset, SEEK_SET);
1555 write(fd, s->refcount_table, s->cluster_size);
1557 lseek(fd, s->refcount_block_offset, SEEK_SET);
1558 write(fd, s->refcount_block, s->cluster_size);
1560 qemu_free(s->refcount_table);
1561 qemu_free(s->refcount_block);
1562 close(fd);
1563 return 0;
1564 fail:
1565 qemu_free(s->refcount_table);
1566 qemu_free(s->refcount_block);
1567 close(fd);
1568 return -ENOMEM;
1571 static int qcow_make_empty(BlockDriverState *bs)
1573 #if 0
1574 /* XXX: not correct */
1575 BDRVQcowState *s = bs->opaque;
1576 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1577 int ret;
1579 memset(s->l1_table, 0, l1_length);
1580 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1581 return -1;
1582 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1583 if (ret < 0)
1584 return ret;
1586 l2_cache_reset(bs);
1587 #endif
1588 return 0;
1591 /* XXX: put compressed sectors first, then all the cluster aligned
1592 tables to avoid losing bytes in alignment */
1593 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1594 const uint8_t *buf, int nb_sectors)
1596 BDRVQcowState *s = bs->opaque;
1597 z_stream strm;
1598 int ret, out_len;
1599 uint8_t *out_buf;
1600 uint64_t cluster_offset;
1602 if (nb_sectors == 0) {
1603 /* align end of file to a sector boundary to ease reading with
1604 sector based I/Os */
1605 cluster_offset = bdrv_getlength(s->hd);
1606 cluster_offset = (cluster_offset + 511) & ~511;
1607 bdrv_truncate(s->hd, cluster_offset);
1608 return 0;
1611 if (nb_sectors != s->cluster_sectors)
1612 return -EINVAL;
1614 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1615 if (!out_buf)
1616 return -ENOMEM;
1618 /* best compression, small window, no zlib header */
1619 memset(&strm, 0, sizeof(strm));
1620 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1621 Z_DEFLATED, -12,
1622 9, Z_DEFAULT_STRATEGY);
1623 if (ret != 0) {
1624 qemu_free(out_buf);
1625 return -1;
1628 strm.avail_in = s->cluster_size;
1629 strm.next_in = (uint8_t *)buf;
1630 strm.avail_out = s->cluster_size;
1631 strm.next_out = out_buf;
1633 ret = deflate(&strm, Z_FINISH);
1634 if (ret != Z_STREAM_END && ret != Z_OK) {
1635 qemu_free(out_buf);
1636 deflateEnd(&strm);
1637 return -1;
1639 out_len = strm.next_out - out_buf;
1641 deflateEnd(&strm);
1643 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1644 /* could not compress: write normal cluster */
1645 qcow_write(bs, sector_num, buf, s->cluster_sectors);
1646 } else {
1647 cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1648 out_len);
1649 if (!cluster_offset)
1650 return -1;
1651 cluster_offset &= s->cluster_offset_mask;
1652 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1653 qemu_free(out_buf);
1654 return -1;
1658 qemu_free(out_buf);
1659 return 0;
1662 static void qcow_flush(BlockDriverState *bs)
1664 BDRVQcowState *s = bs->opaque;
1665 bdrv_flush(s->hd);
1668 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1670 BDRVQcowState *s = bs->opaque;
1671 bdi->cluster_size = s->cluster_size;
1672 bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1673 (s->cluster_bits + s->l2_bits);
1674 return 0;
1677 /*********************************************************/
1678 /* snapshot support */
1680 /* update the refcounts of snapshots and the copied flag */
1681 static int update_snapshot_refcount(BlockDriverState *bs,
1682 int64_t l1_table_offset,
1683 int l1_size,
1684 int addend)
1686 BDRVQcowState *s = bs->opaque;
1687 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1688 int64_t old_offset, old_l2_offset;
1689 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1691 l2_cache_reset(bs);
1693 l2_table = NULL;
1694 l1_table = NULL;
1695 l1_size2 = l1_size * sizeof(uint64_t);
1696 l1_allocated = 0;
1697 if (l1_table_offset != s->l1_table_offset) {
1698 l1_table = qemu_malloc(l1_size2);
1699 if (!l1_table)
1700 goto fail;
1701 l1_allocated = 1;
1702 if (bdrv_pread(s->hd, l1_table_offset,
1703 l1_table, l1_size2) != l1_size2)
1704 goto fail;
1705 for(i = 0;i < l1_size; i++)
1706 be64_to_cpus(&l1_table[i]);
1707 } else {
1708 assert(l1_size == s->l1_size);
1709 l1_table = s->l1_table;
1710 l1_allocated = 0;
1713 l2_size = s->l2_size * sizeof(uint64_t);
1714 l2_table = qemu_malloc(l2_size);
1715 if (!l2_table)
1716 goto fail;
1717 l1_modified = 0;
1718 for(i = 0; i < l1_size; i++) {
1719 l2_offset = l1_table[i];
1720 if (l2_offset) {
1721 old_l2_offset = l2_offset;
1722 l2_offset &= ~QCOW_OFLAG_COPIED;
1723 l2_modified = 0;
1724 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1725 goto fail;
1726 for(j = 0; j < s->l2_size; j++) {
1727 offset = be64_to_cpu(l2_table[j]);
1728 if (offset != 0) {
1729 old_offset = offset;
1730 offset &= ~QCOW_OFLAG_COPIED;
1731 if (offset & QCOW_OFLAG_COMPRESSED) {
1732 nb_csectors = ((offset >> s->csize_shift) &
1733 s->csize_mask) + 1;
1734 if (addend != 0)
1735 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1736 nb_csectors * 512, addend);
1737 /* compressed clusters are never modified */
1738 refcount = 2;
1739 } else {
1740 if (addend != 0) {
1741 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1742 } else {
1743 refcount = get_refcount(bs, offset >> s->cluster_bits);
1747 if (refcount == 1) {
1748 offset |= QCOW_OFLAG_COPIED;
1750 if (offset != old_offset) {
1751 l2_table[j] = cpu_to_be64(offset);
1752 l2_modified = 1;
1756 if (l2_modified) {
1757 if (bdrv_pwrite(s->hd,
1758 l2_offset, l2_table, l2_size) != l2_size)
1759 goto fail;
1762 if (addend != 0) {
1763 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1764 } else {
1765 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1767 if (refcount == 1) {
1768 l2_offset |= QCOW_OFLAG_COPIED;
1770 if (l2_offset != old_l2_offset) {
1771 l1_table[i] = l2_offset;
1772 l1_modified = 1;
1776 if (l1_modified) {
1777 for(i = 0; i < l1_size; i++)
1778 cpu_to_be64s(&l1_table[i]);
1779 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1780 l1_size2) != l1_size2)
1781 goto fail;
1782 for(i = 0; i < l1_size; i++)
1783 be64_to_cpus(&l1_table[i]);
1785 if (l1_allocated)
1786 qemu_free(l1_table);
1787 qemu_free(l2_table);
1788 return 0;
1789 fail:
1790 if (l1_allocated)
1791 qemu_free(l1_table);
1792 qemu_free(l2_table);
1793 return -EIO;
1796 static void qcow_free_snapshots(BlockDriverState *bs)
1798 BDRVQcowState *s = bs->opaque;
1799 int i;
1801 for(i = 0; i < s->nb_snapshots; i++) {
1802 qemu_free(s->snapshots[i].name);
1803 qemu_free(s->snapshots[i].id_str);
1805 qemu_free(s->snapshots);
1806 s->snapshots = NULL;
1807 s->nb_snapshots = 0;
1810 static int qcow_read_snapshots(BlockDriverState *bs)
1812 BDRVQcowState *s = bs->opaque;
1813 QCowSnapshotHeader h;
1814 QCowSnapshot *sn;
1815 int i, id_str_size, name_size;
1816 int64_t offset;
1817 uint32_t extra_data_size;
1819 if (!s->nb_snapshots) {
1820 s->snapshots = NULL;
1821 s->snapshots_size = 0;
1822 return 0;
1825 offset = s->snapshots_offset;
1826 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1827 if (!s->snapshots)
1828 goto fail;
1829 for(i = 0; i < s->nb_snapshots; i++) {
1830 offset = align_offset(offset, 8);
1831 if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1832 goto fail;
1833 offset += sizeof(h);
1834 sn = s->snapshots + i;
1835 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1836 sn->l1_size = be32_to_cpu(h.l1_size);
1837 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1838 sn->date_sec = be32_to_cpu(h.date_sec);
1839 sn->date_nsec = be32_to_cpu(h.date_nsec);
1840 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1841 extra_data_size = be32_to_cpu(h.extra_data_size);
1843 id_str_size = be16_to_cpu(h.id_str_size);
1844 name_size = be16_to_cpu(h.name_size);
1846 offset += extra_data_size;
1848 sn->id_str = qemu_malloc(id_str_size + 1);
1849 if (!sn->id_str)
1850 goto fail;
1851 if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1852 goto fail;
1853 offset += id_str_size;
1854 sn->id_str[id_str_size] = '\0';
1856 sn->name = qemu_malloc(name_size + 1);
1857 if (!sn->name)
1858 goto fail;
1859 if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1860 goto fail;
1861 offset += name_size;
1862 sn->name[name_size] = '\0';
1864 s->snapshots_size = offset - s->snapshots_offset;
1865 return 0;
1866 fail:
1867 qcow_free_snapshots(bs);
1868 return -1;
1871 /* add at the end of the file a new list of snapshots */
1872 static int qcow_write_snapshots(BlockDriverState *bs)
1874 BDRVQcowState *s = bs->opaque;
1875 QCowSnapshot *sn;
1876 QCowSnapshotHeader h;
1877 int i, name_size, id_str_size, snapshots_size;
1878 uint64_t data64;
1879 uint32_t data32;
1880 int64_t offset, snapshots_offset;
1882 /* compute the size of the snapshots */
1883 offset = 0;
1884 for(i = 0; i < s->nb_snapshots; i++) {
1885 sn = s->snapshots + i;
1886 offset = align_offset(offset, 8);
1887 offset += sizeof(h);
1888 offset += strlen(sn->id_str);
1889 offset += strlen(sn->name);
1891 snapshots_size = offset;
1893 snapshots_offset = alloc_clusters(bs, snapshots_size);
1894 offset = snapshots_offset;
1896 for(i = 0; i < s->nb_snapshots; i++) {
1897 sn = s->snapshots + i;
1898 memset(&h, 0, sizeof(h));
1899 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1900 h.l1_size = cpu_to_be32(sn->l1_size);
1901 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1902 h.date_sec = cpu_to_be32(sn->date_sec);
1903 h.date_nsec = cpu_to_be32(sn->date_nsec);
1904 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1906 id_str_size = strlen(sn->id_str);
1907 name_size = strlen(sn->name);
1908 h.id_str_size = cpu_to_be16(id_str_size);
1909 h.name_size = cpu_to_be16(name_size);
1910 offset = align_offset(offset, 8);
1911 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1912 goto fail;
1913 offset += sizeof(h);
1914 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1915 goto fail;
1916 offset += id_str_size;
1917 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1918 goto fail;
1919 offset += name_size;
1922 /* update the various header fields */
1923 data64 = cpu_to_be64(snapshots_offset);
1924 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1925 &data64, sizeof(data64)) != sizeof(data64))
1926 goto fail;
1927 data32 = cpu_to_be32(s->nb_snapshots);
1928 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1929 &data32, sizeof(data32)) != sizeof(data32))
1930 goto fail;
1932 /* free the old snapshot table */
1933 free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1934 s->snapshots_offset = snapshots_offset;
1935 s->snapshots_size = snapshots_size;
1936 return 0;
1937 fail:
1938 return -1;
1941 static void find_new_snapshot_id(BlockDriverState *bs,
1942 char *id_str, int id_str_size)
1944 BDRVQcowState *s = bs->opaque;
1945 QCowSnapshot *sn;
1946 int i, id, id_max = 0;
1948 for(i = 0; i < s->nb_snapshots; i++) {
1949 sn = s->snapshots + i;
1950 id = strtoul(sn->id_str, NULL, 10);
1951 if (id > id_max)
1952 id_max = id;
1954 snprintf(id_str, id_str_size, "%d", id_max + 1);
1957 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1959 BDRVQcowState *s = bs->opaque;
1960 int i;
1962 for(i = 0; i < s->nb_snapshots; i++) {
1963 if (!strcmp(s->snapshots[i].id_str, id_str))
1964 return i;
1966 return -1;
1969 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1971 BDRVQcowState *s = bs->opaque;
1972 int i, ret;
1974 ret = find_snapshot_by_id(bs, name);
1975 if (ret >= 0)
1976 return ret;
1977 for(i = 0; i < s->nb_snapshots; i++) {
1978 if (!strcmp(s->snapshots[i].name, name))
1979 return i;
1981 return -1;
1984 /* if no id is provided, a new one is constructed */
1985 static int qcow_snapshot_create(BlockDriverState *bs,
1986 QEMUSnapshotInfo *sn_info)
1988 BDRVQcowState *s = bs->opaque;
1989 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1990 int i, ret;
1991 uint64_t *l1_table = NULL;
1993 memset(sn, 0, sizeof(*sn));
1995 if (sn_info->id_str[0] == '\0') {
1996 /* compute a new id */
1997 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
2000 /* check that the ID is unique */
2001 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
2002 return -ENOENT;
2004 sn->id_str = qemu_strdup(sn_info->id_str);
2005 if (!sn->id_str)
2006 goto fail;
2007 sn->name = qemu_strdup(sn_info->name);
2008 if (!sn->name)
2009 goto fail;
2010 sn->vm_state_size = sn_info->vm_state_size;
2011 sn->date_sec = sn_info->date_sec;
2012 sn->date_nsec = sn_info->date_nsec;
2013 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
2015 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
2016 if (ret < 0)
2017 goto fail;
2019 /* create the L1 table of the snapshot */
2020 sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
2021 sn->l1_size = s->l1_size;
2023 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
2024 if (!l1_table)
2025 goto fail;
2026 for(i = 0; i < s->l1_size; i++) {
2027 l1_table[i] = cpu_to_be64(s->l1_table[i]);
2029 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
2030 l1_table, s->l1_size * sizeof(uint64_t)) !=
2031 (s->l1_size * sizeof(uint64_t)))
2032 goto fail;
2033 qemu_free(l1_table);
2034 l1_table = NULL;
2036 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
2037 if (!snapshots1)
2038 goto fail;
2039 if (s->snapshots) {
2040 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
2041 qemu_free(s->snapshots);
2043 s->snapshots = snapshots1;
2044 s->snapshots[s->nb_snapshots++] = *sn;
2046 if (qcow_write_snapshots(bs) < 0)
2047 goto fail;
2048 #ifdef DEBUG_ALLOC
2049 check_refcounts(bs);
2050 #endif
2051 return 0;
2052 fail:
2053 qemu_free(sn->name);
2054 qemu_free(l1_table);
2055 return -1;
2058 /* copy the snapshot 'snapshot_name' into the current disk image */
2059 static int qcow_snapshot_goto(BlockDriverState *bs,
2060 const char *snapshot_id)
2062 BDRVQcowState *s = bs->opaque;
2063 QCowSnapshot *sn;
2064 int i, snapshot_index, l1_size2;
2066 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2067 if (snapshot_index < 0)
2068 return -ENOENT;
2069 sn = &s->snapshots[snapshot_index];
2071 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
2072 goto fail;
2074 if (grow_l1_table(bs, sn->l1_size) < 0)
2075 goto fail;
2077 s->l1_size = sn->l1_size;
2078 l1_size2 = s->l1_size * sizeof(uint64_t);
2079 /* copy the snapshot l1 table to the current l1 table */
2080 if (bdrv_pread(s->hd, sn->l1_table_offset,
2081 s->l1_table, l1_size2) != l1_size2)
2082 goto fail;
2083 if (bdrv_pwrite(s->hd, s->l1_table_offset,
2084 s->l1_table, l1_size2) != l1_size2)
2085 goto fail;
2086 for(i = 0;i < s->l1_size; i++) {
2087 be64_to_cpus(&s->l1_table[i]);
2090 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2091 goto fail;
2093 #ifdef DEBUG_ALLOC
2094 check_refcounts(bs);
2095 #endif
2096 return 0;
2097 fail:
2098 return -EIO;
2101 static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2103 BDRVQcowState *s = bs->opaque;
2104 QCowSnapshot *sn;
2105 int snapshot_index, ret;
2107 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2108 if (snapshot_index < 0)
2109 return -ENOENT;
2110 sn = &s->snapshots[snapshot_index];
2112 ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2113 if (ret < 0)
2114 return ret;
2115 /* must update the copied flag on the current cluster offsets */
2116 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2117 if (ret < 0)
2118 return ret;
2119 free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2121 qemu_free(sn->id_str);
2122 qemu_free(sn->name);
2123 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2124 s->nb_snapshots--;
2125 ret = qcow_write_snapshots(bs);
2126 if (ret < 0) {
2127 /* XXX: restore snapshot if error ? */
2128 return ret;
2130 #ifdef DEBUG_ALLOC
2131 check_refcounts(bs);
2132 #endif
2133 return 0;
2136 static int qcow_snapshot_list(BlockDriverState *bs,
2137 QEMUSnapshotInfo **psn_tab)
2139 BDRVQcowState *s = bs->opaque;
2140 QEMUSnapshotInfo *sn_tab, *sn_info;
2141 QCowSnapshot *sn;
2142 int i;
2144 sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2145 if (!sn_tab)
2146 goto fail;
2147 for(i = 0; i < s->nb_snapshots; i++) {
2148 sn_info = sn_tab + i;
2149 sn = s->snapshots + i;
2150 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2151 sn->id_str);
2152 pstrcpy(sn_info->name, sizeof(sn_info->name),
2153 sn->name);
2154 sn_info->vm_state_size = sn->vm_state_size;
2155 sn_info->date_sec = sn->date_sec;
2156 sn_info->date_nsec = sn->date_nsec;
2157 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2159 *psn_tab = sn_tab;
2160 return s->nb_snapshots;
2161 fail:
2162 qemu_free(sn_tab);
2163 *psn_tab = NULL;
2164 return -ENOMEM;
2167 /*********************************************************/
2168 /* refcount handling */
2170 static int refcount_init(BlockDriverState *bs)
2172 BDRVQcowState *s = bs->opaque;
2173 int ret, refcount_table_size2, i;
2175 s->refcount_block_cache = qemu_malloc(s->cluster_size);
2176 if (!s->refcount_block_cache)
2177 goto fail;
2178 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2179 s->refcount_table = qemu_malloc(refcount_table_size2);
2180 if (!s->refcount_table)
2181 goto fail;
2182 if (s->refcount_table_size > 0) {
2183 ret = bdrv_pread(s->hd, s->refcount_table_offset,
2184 s->refcount_table, refcount_table_size2);
2185 if (ret != refcount_table_size2)
2186 goto fail;
2187 for(i = 0; i < s->refcount_table_size; i++)
2188 be64_to_cpus(&s->refcount_table[i]);
2190 return 0;
2191 fail:
2192 return -ENOMEM;
2195 static void refcount_close(BlockDriverState *bs)
2197 BDRVQcowState *s = bs->opaque;
2198 qemu_free(s->refcount_block_cache);
2199 qemu_free(s->refcount_table);
2203 static int load_refcount_block(BlockDriverState *bs,
2204 int64_t refcount_block_offset)
2206 BDRVQcowState *s = bs->opaque;
2207 int ret;
2208 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
2209 s->cluster_size);
2210 if (ret != s->cluster_size)
2211 return -EIO;
2212 s->refcount_block_cache_offset = refcount_block_offset;
2213 return 0;
2216 static void scan_refcount(BlockDriverState *bs, int64_t *high)
2218 BDRVQcowState *s = bs->opaque;
2219 int64_t refcnt_index, cluster_index, cluster_end, h = 0;
2221 for (refcnt_index=0; refcnt_index < s->refcount_table_size; refcnt_index++){
2222 if (s->refcount_table[refcnt_index] == 0) {
2223 continue;
2225 cluster_index = refcnt_index << (s->cluster_bits - REFCOUNT_SHIFT);
2226 cluster_end = (refcnt_index + 1) << (s->cluster_bits - REFCOUNT_SHIFT);
2227 for ( ; cluster_index < cluster_end; cluster_index++) {
2228 if (get_refcount(bs, cluster_index) == 0)
2229 /* do nothing -- reserved for free counting */;
2230 else
2231 h = cluster_index;
2235 if (high)
2236 *high = (h+1);
2239 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2241 BDRVQcowState *s = bs->opaque;
2242 int refcount_table_index, block_index;
2243 int64_t refcount_block_offset;
2245 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2246 if (refcount_table_index >= s->refcount_table_size)
2247 return 0;
2248 refcount_block_offset = s->refcount_table[refcount_table_index];
2249 if (!refcount_block_offset)
2250 return 0;
2251 if (refcount_block_offset != s->refcount_block_cache_offset) {
2252 /* better than nothing: return allocated if read error */
2253 if (load_refcount_block(bs, refcount_block_offset) < 0)
2254 return 1;
2256 block_index = cluster_index &
2257 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2258 return be16_to_cpu(s->refcount_block_cache[block_index]);
2261 /* return < 0 if error */
2262 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2264 BDRVQcowState *s = bs->opaque;
2265 int i, nb_clusters;
2267 nb_clusters = size_to_clusters(s, size);
2268 retry:
2269 for(i = 0; i < nb_clusters; i++) {
2270 int64_t i = s->free_cluster_index++;
2271 if (get_refcount(bs, i) != 0)
2272 goto retry;
2274 #ifdef DEBUG_ALLOC2
2275 printf("alloc_clusters: size=%lld -> %lld\n",
2276 size,
2277 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2278 #endif
2280 if (s->highest_alloc < s->free_cluster_index)
2281 s->highest_alloc = s->free_cluster_index;
2283 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2286 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2288 int64_t offset;
2290 offset = alloc_clusters_noref(bs, size);
2291 update_refcount(bs, offset, size, 1);
2292 return offset;
2295 /* only used to allocate compressed sectors. We try to allocate
2296 contiguous sectors. size must be <= cluster_size */
2297 static int64_t alloc_bytes(BlockDriverState *bs, int size)
2299 BDRVQcowState *s = bs->opaque;
2300 int64_t offset, cluster_offset;
2301 int free_in_cluster;
2303 assert(size > 0 && size <= s->cluster_size);
2304 if (s->free_byte_offset == 0) {
2305 s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2307 redo:
2308 free_in_cluster = s->cluster_size -
2309 (s->free_byte_offset & (s->cluster_size - 1));
2310 if (size <= free_in_cluster) {
2311 /* enough space in current cluster */
2312 offset = s->free_byte_offset;
2313 s->free_byte_offset += size;
2314 free_in_cluster -= size;
2315 if (free_in_cluster == 0)
2316 s->free_byte_offset = 0;
2317 if ((offset & (s->cluster_size - 1)) != 0)
2318 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2319 } else {
2320 offset = alloc_clusters(bs, s->cluster_size);
2321 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2322 if ((cluster_offset + s->cluster_size) == offset) {
2323 /* we are lucky: contiguous data */
2324 offset = s->free_byte_offset;
2325 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2326 s->free_byte_offset += size;
2327 } else {
2328 s->free_byte_offset = offset;
2329 goto redo;
2332 return offset;
2335 static void free_clusters(BlockDriverState *bs,
2336 int64_t offset, int64_t size)
2338 update_refcount(bs, offset, size, -1);
2341 static int grow_refcount_table(BlockDriverState *bs, int min_size)
2343 BDRVQcowState *s = bs->opaque;
2344 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2345 uint64_t *new_table;
2346 int64_t table_offset;
2347 uint8_t data[12];
2348 int old_table_size;
2349 int64_t old_table_offset;
2351 if (min_size <= s->refcount_table_size)
2352 return 0;
2353 /* compute new table size */
2354 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2355 for(;;) {
2356 if (refcount_table_clusters == 0) {
2357 refcount_table_clusters = 1;
2358 } else {
2359 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2361 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2362 if (min_size <= new_table_size)
2363 break;
2365 #ifdef DEBUG_ALLOC2
2366 printf("grow_refcount_table from %d to %d\n",
2367 s->refcount_table_size,
2368 new_table_size);
2369 #endif
2370 new_table_size2 = new_table_size * sizeof(uint64_t);
2371 new_table = qemu_mallocz(new_table_size2);
2372 if (!new_table)
2373 return -ENOMEM;
2374 memcpy(new_table, s->refcount_table,
2375 s->refcount_table_size * sizeof(uint64_t));
2376 for(i = 0; i < s->refcount_table_size; i++)
2377 cpu_to_be64s(&new_table[i]);
2378 /* Note: we cannot update the refcount now to avoid recursion */
2379 table_offset = alloc_clusters_noref(bs, new_table_size2);
2380 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
2381 if (ret != new_table_size2)
2382 goto fail;
2383 for(i = 0; i < s->refcount_table_size; i++)
2384 be64_to_cpus(&new_table[i]);
2386 cpu_to_be64w((uint64_t*)data, table_offset);
2387 cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
2388 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2389 data, sizeof(data)) != sizeof(data))
2390 goto fail;
2391 qemu_free(s->refcount_table);
2392 old_table_offset = s->refcount_table_offset;
2393 old_table_size = s->refcount_table_size;
2394 s->refcount_table = new_table;
2395 s->refcount_table_size = new_table_size;
2396 s->refcount_table_offset = table_offset;
2398 update_refcount(bs, table_offset, new_table_size2, 1);
2399 free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
2400 return 0;
2401 fail:
2402 free_clusters(bs, table_offset, new_table_size2);
2403 qemu_free(new_table);
2404 return -EIO;
2407 /* addend must be 1 or -1 */
2408 /* XXX: cache several refcount block clusters ? */
2409 static int update_cluster_refcount(BlockDriverState *bs,
2410 int64_t cluster_index,
2411 int addend)
2413 BDRVQcowState *s = bs->opaque;
2414 int64_t offset, refcount_block_offset;
2415 int ret, refcount_table_index, block_index, refcount;
2416 uint64_t data64;
2418 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2419 if (refcount_table_index >= s->refcount_table_size) {
2420 if (addend < 0)
2421 return -EINVAL;
2422 ret = grow_refcount_table(bs, refcount_table_index + 1);
2423 if (ret < 0)
2424 return ret;
2426 refcount_block_offset = s->refcount_table[refcount_table_index];
2427 if (!refcount_block_offset) {
2428 if (addend < 0)
2429 return -EINVAL;
2430 /* create a new refcount block */
2431 /* Note: we cannot update the refcount now to avoid recursion */
2432 offset = alloc_clusters_noref(bs, s->cluster_size);
2433 memset(s->refcount_block_cache, 0, s->cluster_size);
2434 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2435 if (ret != s->cluster_size)
2436 return -EINVAL;
2437 s->refcount_table[refcount_table_index] = offset;
2438 data64 = cpu_to_be64(offset);
2439 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2440 refcount_table_index * sizeof(uint64_t),
2441 &data64, sizeof(data64));
2442 if (ret != sizeof(data64))
2443 return -EINVAL;
2445 refcount_block_offset = offset;
2446 s->refcount_block_cache_offset = offset;
2447 update_refcount(bs, offset, s->cluster_size, 1);
2448 } else {
2449 if (refcount_block_offset != s->refcount_block_cache_offset) {
2450 if (load_refcount_block(bs, refcount_block_offset) < 0)
2451 return -EIO;
2454 /* we can update the count and save it */
2455 block_index = cluster_index &
2456 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2457 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2458 refcount += addend;
2459 if (refcount < 0 || refcount > 0xffff)
2460 return -EINVAL;
2461 if (refcount == 0 && cluster_index < s->free_cluster_index) {
2462 s->free_cluster_index = cluster_index;
2464 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2465 if (bdrv_pwrite(s->hd,
2466 refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2467 &s->refcount_block_cache[block_index], 2) != 2)
2468 return -EIO;
2469 return refcount;
2472 static void update_refcount(BlockDriverState *bs,
2473 int64_t offset, int64_t length,
2474 int addend)
2476 BDRVQcowState *s = bs->opaque;
2477 int64_t start, last, cluster_offset;
2479 #ifdef DEBUG_ALLOC2
2480 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2481 offset, length, addend);
2482 #endif
2483 if (length <= 0)
2484 return;
2485 start = offset & ~(s->cluster_size - 1);
2486 last = (offset + length - 1) & ~(s->cluster_size - 1);
2487 for(cluster_offset = start; cluster_offset <= last;
2488 cluster_offset += s->cluster_size) {
2489 update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2493 #ifdef DEBUG_ALLOC
2494 static void inc_refcounts(BlockDriverState *bs,
2495 uint16_t *refcount_table,
2496 int refcount_table_size,
2497 int64_t offset, int64_t size)
2499 BDRVQcowState *s = bs->opaque;
2500 int64_t start, last, cluster_offset;
2501 int k;
2503 if (size <= 0)
2504 return;
2506 start = offset & ~(s->cluster_size - 1);
2507 last = (offset + size - 1) & ~(s->cluster_size - 1);
2508 for(cluster_offset = start; cluster_offset <= last;
2509 cluster_offset += s->cluster_size) {
2510 k = cluster_offset >> s->cluster_bits;
2511 if (k < 0 || k >= refcount_table_size) {
2512 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2513 } else {
2514 if (++refcount_table[k] == 0) {
2515 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2521 static int check_refcounts_l1(BlockDriverState *bs,
2522 uint16_t *refcount_table,
2523 int refcount_table_size,
2524 int64_t l1_table_offset, int l1_size,
2525 int check_copied)
2527 BDRVQcowState *s = bs->opaque;
2528 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2529 int l2_size, i, j, nb_csectors, refcount;
2531 l2_table = NULL;
2532 l1_size2 = l1_size * sizeof(uint64_t);
2534 inc_refcounts(bs, refcount_table, refcount_table_size,
2535 l1_table_offset, l1_size2);
2537 l1_table = qemu_malloc(l1_size2);
2538 if (!l1_table)
2539 goto fail;
2540 if (bdrv_pread(s->hd, l1_table_offset,
2541 l1_table, l1_size2) != l1_size2)
2542 goto fail;
2543 for(i = 0;i < l1_size; i++)
2544 be64_to_cpus(&l1_table[i]);
2546 l2_size = s->l2_size * sizeof(uint64_t);
2547 l2_table = qemu_malloc(l2_size);
2548 if (!l2_table)
2549 goto fail;
2550 for(i = 0; i < l1_size; i++) {
2551 l2_offset = l1_table[i];
2552 if (l2_offset) {
2553 if (check_copied) {
2554 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2555 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2556 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2557 l2_offset, refcount);
2560 l2_offset &= ~QCOW_OFLAG_COPIED;
2561 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2562 goto fail;
2563 for(j = 0; j < s->l2_size; j++) {
2564 offset = be64_to_cpu(l2_table[j]);
2565 if (offset != 0) {
2566 if (offset & QCOW_OFLAG_COMPRESSED) {
2567 if (offset & QCOW_OFLAG_COPIED) {
2568 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2569 offset >> s->cluster_bits);
2570 offset &= ~QCOW_OFLAG_COPIED;
2572 nb_csectors = ((offset >> s->csize_shift) &
2573 s->csize_mask) + 1;
2574 offset &= s->cluster_offset_mask;
2575 inc_refcounts(bs, refcount_table,
2576 refcount_table_size,
2577 offset & ~511, nb_csectors * 512);
2578 } else {
2579 if (check_copied) {
2580 refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2581 if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2582 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2583 offset, refcount);
2586 offset &= ~QCOW_OFLAG_COPIED;
2587 inc_refcounts(bs, refcount_table,
2588 refcount_table_size,
2589 offset, s->cluster_size);
2593 inc_refcounts(bs, refcount_table,
2594 refcount_table_size,
2595 l2_offset,
2596 s->cluster_size);
2599 qemu_free(l1_table);
2600 qemu_free(l2_table);
2601 return 0;
2602 fail:
2603 printf("ERROR: I/O error in check_refcounts_l1\n");
2604 qemu_free(l1_table);
2605 qemu_free(l2_table);
2606 return -EIO;
2609 static void check_refcounts(BlockDriverState *bs)
2611 BDRVQcowState *s = bs->opaque;
2612 int64_t size;
2613 int nb_clusters, refcount1, refcount2, i;
2614 QCowSnapshot *sn;
2615 uint16_t *refcount_table;
2617 size = bdrv_getlength(s->hd);
2618 nb_clusters = size_to_clusters(s, size);
2619 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2621 /* header */
2622 inc_refcounts(bs, refcount_table, nb_clusters,
2623 0, s->cluster_size);
2625 check_refcounts_l1(bs, refcount_table, nb_clusters,
2626 s->l1_table_offset, s->l1_size, 1);
2628 /* snapshots */
2629 for(i = 0; i < s->nb_snapshots; i++) {
2630 sn = s->snapshots + i;
2631 check_refcounts_l1(bs, refcount_table, nb_clusters,
2632 sn->l1_table_offset, sn->l1_size, 0);
2634 inc_refcounts(bs, refcount_table, nb_clusters,
2635 s->snapshots_offset, s->snapshots_size);
2637 /* refcount data */
2638 inc_refcounts(bs, refcount_table, nb_clusters,
2639 s->refcount_table_offset,
2640 s->refcount_table_size * sizeof(uint64_t));
2641 for(i = 0; i < s->refcount_table_size; i++) {
2642 int64_t offset;
2643 offset = s->refcount_table[i];
2644 if (offset != 0) {
2645 inc_refcounts(bs, refcount_table, nb_clusters,
2646 offset, s->cluster_size);
2650 /* compare ref counts */
2651 for(i = 0; i < nb_clusters; i++) {
2652 refcount1 = get_refcount(bs, i);
2653 refcount2 = refcount_table[i];
2654 if (refcount1 != refcount2)
2655 printf("ERROR cluster %d refcount=%d reference=%d\n",
2656 i, refcount1, refcount2);
2659 qemu_free(refcount_table);
2662 #if 0
2663 static void dump_refcounts(BlockDriverState *bs)
2665 BDRVQcowState *s = bs->opaque;
2666 int64_t nb_clusters, k, k1, size;
2667 int refcount;
2669 size = bdrv_getlength(s->hd);
2670 nb_clusters = size_to_clusters(s, size);
2671 for(k = 0; k < nb_clusters;) {
2672 k1 = k;
2673 refcount = get_refcount(bs, k);
2674 k++;
2675 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2676 k++;
2677 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2680 #endif
2681 #endif
2683 BlockDriver bdrv_qcow2 = {
2684 "qcow2",
2685 sizeof(BDRVQcowState),
2686 qcow_probe,
2687 qcow_open,
2688 NULL,
2689 NULL,
2690 qcow_close,
2691 qcow_create,
2692 qcow_flush,
2693 qcow_is_allocated,
2694 qcow_set_key,
2695 qcow_make_empty,
2697 .bdrv_aio_read = qcow_aio_read,
2698 .bdrv_aio_write = qcow_aio_write,
2699 .bdrv_aio_cancel = qcow_aio_cancel,
2700 .aiocb_size = sizeof(QCowAIOCB),
2701 .bdrv_write_compressed = qcow_write_compressed,
2703 .bdrv_snapshot_create = qcow_snapshot_create,
2704 .bdrv_snapshot_goto = qcow_snapshot_goto,
2705 .bdrv_snapshot_delete = qcow_snapshot_delete,
2706 .bdrv_snapshot_list = qcow_snapshot_list,
2707 .bdrv_get_info = qcow_get_info,