Change order of metadata update to prevent loosing guest data because of unexpected...
[qemu] / block-qcow2.c
1 /*
2  * Block driver for the QCOW version 2 format
3  *
4  * Copyright (c) 2004-2006 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include <zlib.h>
27 #include "aes.h"
28 #include <assert.h>
29
30 /*
31   Differences with QCOW:
32
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.
44 */
45
46 //#define DEBUG_ALLOC
47 //#define DEBUG_ALLOC2
48
49 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50 #define QCOW_VERSION 2
51
52 #define QCOW_CRYPT_NONE 0
53 #define QCOW_CRYPT_AES  1
54
55 #define QCOW_MAX_CRYPT_CLUSTERS 32
56
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)
61
62 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
63
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;
79
80 typedef struct __attribute__((packed)) QCowSnapshotHeader {
81     /* header is 8 byte aligned */
82     uint64_t l1_table_offset;
83
84     uint32_t l1_size;
85     uint16_t id_str_size;
86     uint16_t name_size;
87
88     uint32_t date_sec;
89     uint32_t date_nsec;
90
91     uint64_t vm_clock_nsec;
92
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;
99
100 #define L2_CACHE_SIZE 16
101
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;
112
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;
133
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;
141
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;
146     uint64_t snapshots_offset;
147     int snapshots_size;
148     int nb_snapshots;
149     QCowSnapshot *snapshots;
150 } BDRVQcowState;
151
152 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
153 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
154                      uint8_t *buf, int nb_sectors);
155 static int qcow_read_snapshots(BlockDriverState *bs);
156 static void qcow_free_snapshots(BlockDriverState *bs);
157 static int refcount_init(BlockDriverState *bs);
158 static void refcount_close(BlockDriverState *bs);
159 static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
160 static int update_cluster_refcount(BlockDriverState *bs,
161                                    int64_t cluster_index,
162                                    int addend);
163 static void update_refcount(BlockDriverState *bs,
164                             int64_t offset, int64_t length,
165                             int addend);
166 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
167 static int64_t alloc_bytes(BlockDriverState *bs, int size);
168 static void free_clusters(BlockDriverState *bs,
169                           int64_t offset, int64_t size);
170 #ifdef DEBUG_ALLOC
171 static void check_refcounts(BlockDriverState *bs);
172 #endif
173
174 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
175 {
176     const QCowHeader *cow_header = (const void *)buf;
177
178     if (buf_size >= sizeof(QCowHeader) &&
179         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
180         be32_to_cpu(cow_header->version) == QCOW_VERSION)
181         return 100;
182     else
183         return 0;
184 }
185
186 static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
187 {
188     BDRVQcowState *s = bs->opaque;
189     int len, i, shift, ret;
190     QCowHeader header;
191
192     ret = bdrv_file_open(&s->hd, filename, flags);
193     if (ret < 0)
194         return ret;
195     if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
196         goto fail;
197     be32_to_cpus(&header.magic);
198     be32_to_cpus(&header.version);
199     be64_to_cpus(&header.backing_file_offset);
200     be32_to_cpus(&header.backing_file_size);
201     be64_to_cpus(&header.size);
202     be32_to_cpus(&header.cluster_bits);
203     be32_to_cpus(&header.crypt_method);
204     be64_to_cpus(&header.l1_table_offset);
205     be32_to_cpus(&header.l1_size);
206     be64_to_cpus(&header.refcount_table_offset);
207     be32_to_cpus(&header.refcount_table_clusters);
208     be64_to_cpus(&header.snapshots_offset);
209     be32_to_cpus(&header.nb_snapshots);
210
211     if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
212         goto fail;
213     if (header.size <= 1 ||
214         header.cluster_bits < 9 ||
215         header.cluster_bits > 16)
216         goto fail;
217     if (header.crypt_method > QCOW_CRYPT_AES)
218         goto fail;
219     s->crypt_method_header = header.crypt_method;
220     if (s->crypt_method_header)
221         bs->encrypted = 1;
222     s->cluster_bits = header.cluster_bits;
223     s->cluster_size = 1 << s->cluster_bits;
224     s->cluster_sectors = 1 << (s->cluster_bits - 9);
225     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
226     s->l2_size = 1 << s->l2_bits;
227     bs->total_sectors = header.size / 512;
228     s->csize_shift = (62 - (s->cluster_bits - 8));
229     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
230     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
231     s->refcount_table_offset = header.refcount_table_offset;
232     s->refcount_table_size =
233         header.refcount_table_clusters << (s->cluster_bits - 3);
234
235     s->snapshots_offset = header.snapshots_offset;
236     s->nb_snapshots = header.nb_snapshots;
237
238     /* read the level 1 table */
239     s->l1_size = header.l1_size;
240     shift = s->cluster_bits + s->l2_bits;
241     s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
242     /* the L1 table must contain at least enough entries to put
243        header.size bytes */
244     if (s->l1_size < s->l1_vm_state_index)
245         goto fail;
246     s->l1_table_offset = header.l1_table_offset;
247     s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
248     if (!s->l1_table)
249         goto fail;
250     if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
251         s->l1_size * sizeof(uint64_t))
252         goto fail;
253     for(i = 0;i < s->l1_size; i++) {
254         be64_to_cpus(&s->l1_table[i]);
255     }
256     /* alloc L2 cache */
257     s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
258     if (!s->l2_cache)
259         goto fail;
260     s->cluster_cache = qemu_malloc(s->cluster_size);
261     if (!s->cluster_cache)
262         goto fail;
263     /* one more sector for decompressed data alignment */
264     s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
265                                   + 512);
266     if (!s->cluster_data)
267         goto fail;
268     s->cluster_cache_offset = -1;
269
270     if (refcount_init(bs) < 0)
271         goto fail;
272
273     /* read the backing file name */
274     if (header.backing_file_offset != 0) {
275         len = header.backing_file_size;
276         if (len > 1023)
277             len = 1023;
278         if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
279             goto fail;
280         bs->backing_file[len] = '\0';
281     }
282     if (qcow_read_snapshots(bs) < 0)
283         goto fail;
284
285 #ifdef DEBUG_ALLOC
286     check_refcounts(bs);
287 #endif
288     return 0;
289
290  fail:
291     qcow_free_snapshots(bs);
292     refcount_close(bs);
293     qemu_free(s->l1_table);
294     qemu_free(s->l2_cache);
295     qemu_free(s->cluster_cache);
296     qemu_free(s->cluster_data);
297     bdrv_delete(s->hd);
298     return -1;
299 }
300
301 static int qcow_set_key(BlockDriverState *bs, const char *key)
302 {
303     BDRVQcowState *s = bs->opaque;
304     uint8_t keybuf[16];
305     int len, i;
306
307     memset(keybuf, 0, 16);
308     len = strlen(key);
309     if (len > 16)
310         len = 16;
311     /* XXX: we could compress the chars to 7 bits to increase
312        entropy */
313     for(i = 0;i < len;i++) {
314         keybuf[i] = key[i];
315     }
316     s->crypt_method = s->crypt_method_header;
317
318     if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
319         return -1;
320     if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
321         return -1;
322 #if 0
323     /* test */
324     {
325         uint8_t in[16];
326         uint8_t out[16];
327         uint8_t tmp[16];
328         for(i=0;i<16;i++)
329             in[i] = i;
330         AES_encrypt(in, tmp, &s->aes_encrypt_key);
331         AES_decrypt(tmp, out, &s->aes_decrypt_key);
332         for(i = 0; i < 16; i++)
333             printf(" %02x", tmp[i]);
334         printf("\n");
335         for(i = 0; i < 16; i++)
336             printf(" %02x", out[i]);
337         printf("\n");
338     }
339 #endif
340     return 0;
341 }
342
343 /* The crypt function is compatible with the linux cryptoloop
344    algorithm for < 4 GB images. NOTE: out_buf == in_buf is
345    supported */
346 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
347                             uint8_t *out_buf, const uint8_t *in_buf,
348                             int nb_sectors, int enc,
349                             const AES_KEY *key)
350 {
351     union {
352         uint64_t ll[2];
353         uint8_t b[16];
354     } ivec;
355     int i;
356
357     for(i = 0; i < nb_sectors; i++) {
358         ivec.ll[0] = cpu_to_le64(sector_num);
359         ivec.ll[1] = 0;
360         AES_cbc_encrypt(in_buf, out_buf, 512, key,
361                         ivec.b, enc);
362         sector_num++;
363         in_buf += 512;
364         out_buf += 512;
365     }
366 }
367
368 static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
369                         uint64_t cluster_offset, int n_start, int n_end)
370 {
371     BDRVQcowState *s = bs->opaque;
372     int n, ret;
373
374     n = n_end - n_start;
375     if (n <= 0)
376         return 0;
377     ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
378     if (ret < 0)
379         return ret;
380     if (s->crypt_method) {
381         encrypt_sectors(s, start_sect + n_start,
382                         s->cluster_data,
383                         s->cluster_data, n, 1,
384                         &s->aes_encrypt_key);
385     }
386     ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
387                      s->cluster_data, n);
388     if (ret < 0)
389         return ret;
390     return 0;
391 }
392
393 static void l2_cache_reset(BlockDriverState *bs)
394 {
395     BDRVQcowState *s = bs->opaque;
396
397     memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
398     memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
399     memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
400 }
401
402 static inline int l2_cache_new_entry(BlockDriverState *bs)
403 {
404     BDRVQcowState *s = bs->opaque;
405     uint32_t min_count;
406     int min_index, i;
407
408     /* find a new entry in the least used one */
409     min_index = 0;
410     min_count = 0xffffffff;
411     for(i = 0; i < L2_CACHE_SIZE; i++) {
412         if (s->l2_cache_counts[i] < min_count) {
413             min_count = s->l2_cache_counts[i];
414             min_index = i;
415         }
416     }
417     return min_index;
418 }
419
420 static int64_t align_offset(int64_t offset, int n)
421 {
422     offset = (offset + n - 1) & ~(n - 1);
423     return offset;
424 }
425
426 static int grow_l1_table(BlockDriverState *bs, int min_size)
427 {
428     BDRVQcowState *s = bs->opaque;
429     int new_l1_size, new_l1_size2, ret, i;
430     uint64_t *new_l1_table;
431     uint64_t new_l1_table_offset;
432     uint8_t data[12];
433
434     new_l1_size = s->l1_size;
435     if (min_size <= new_l1_size)
436         return 0;
437     while (min_size > new_l1_size) {
438         new_l1_size = (new_l1_size * 3 + 1) / 2;
439     }
440 #ifdef DEBUG_ALLOC2
441     printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
442 #endif
443
444     new_l1_size2 = sizeof(uint64_t) * new_l1_size;
445     new_l1_table = qemu_mallocz(new_l1_size2);
446     if (!new_l1_table)
447         return -ENOMEM;
448     memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
449
450     /* write new table (align to cluster) */
451     new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
452
453     for(i = 0; i < s->l1_size; i++)
454         new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
455     ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
456     if (ret != new_l1_size2)
457         goto fail;
458     for(i = 0; i < s->l1_size; i++)
459         new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
460
461     /* set new table */
462     cpu_to_be32w((uint32_t*)data, new_l1_size);
463     cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
464     if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
465                 sizeof(data)) != sizeof(data))
466         goto fail;
467     qemu_free(s->l1_table);
468     free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
469     s->l1_table_offset = new_l1_table_offset;
470     s->l1_table = new_l1_table;
471     s->l1_size = new_l1_size;
472     return 0;
473  fail:
474     qemu_free(s->l1_table);
475     return -EIO;
476 }
477
478 /*
479  * seek_l2_table
480  *
481  * seek l2_offset in the l2_cache table
482  * if not found, return NULL,
483  * if found,
484  *   increments the l2 cache hit count of the entry,
485  *   if counter overflow, divide by two all counters
486  *   return the pointer to the l2 cache entry
487  *
488  */
489
490 static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
491 {
492     int i, j;
493
494     for(i = 0; i < L2_CACHE_SIZE; i++) {
495         if (l2_offset == s->l2_cache_offsets[i]) {
496             /* increment the hit count */
497             if (++s->l2_cache_counts[i] == 0xffffffff) {
498                 for(j = 0; j < L2_CACHE_SIZE; j++) {
499                     s->l2_cache_counts[j] >>= 1;
500                 }
501             }
502             return s->l2_cache + (i << s->l2_bits);
503         }
504     }
505     return NULL;
506 }
507
508 /*
509  * l2_load
510  *
511  * Loads a L2 table into memory. If the table is in the cache, the cache
512  * is used; otherwise the L2 table is loaded from the image file.
513  *
514  * Returns a pointer to the L2 table on success, or NULL if the read from
515  * the image file failed.
516  */
517
518 static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
519 {
520     BDRVQcowState *s = bs->opaque;
521     int min_index;
522     uint64_t *l2_table;
523
524     /* seek if the table for the given offset is in the cache */
525
526     l2_table = seek_l2_table(s, l2_offset);
527     if (l2_table != NULL)
528         return l2_table;
529
530     /* not found: load a new entry in the least used one */
531
532     min_index = l2_cache_new_entry(bs);
533     l2_table = s->l2_cache + (min_index << s->l2_bits);
534     if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
535         s->l2_size * sizeof(uint64_t))
536         return NULL;
537     s->l2_cache_offsets[min_index] = l2_offset;
538     s->l2_cache_counts[min_index] = 1;
539
540     return l2_table;
541 }
542
543 /*
544  * l2_allocate
545  *
546  * Allocate a new l2 entry in the file. If l1_index points to an already
547  * used entry in the L2 table (i.e. we are doing a copy on write for the L2
548  * table) copy the contents of the old L2 table into the newly allocated one.
549  * Otherwise the new table is initialized with zeros.
550  *
551  */
552
553 static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
554 {
555     BDRVQcowState *s = bs->opaque;
556     int min_index;
557     uint64_t old_l2_offset, tmp;
558     uint64_t *l2_table, l2_offset;
559
560     old_l2_offset = s->l1_table[l1_index];
561
562     /* allocate a new l2 entry */
563
564     l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
565
566     /* update the L1 entry */
567
568     s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
569
570     tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
571     if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
572                     &tmp, sizeof(tmp)) != sizeof(tmp))
573         return NULL;
574
575     /* allocate a new entry in the l2 cache */
576
577     min_index = l2_cache_new_entry(bs);
578     l2_table = s->l2_cache + (min_index << s->l2_bits);
579
580     if (old_l2_offset == 0) {
581         /* if there was no old l2 table, clear the new table */
582         memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
583     } else {
584         /* if there was an old l2 table, read it from the disk */
585         if (bdrv_pread(s->hd, old_l2_offset,
586                        l2_table, s->l2_size * sizeof(uint64_t)) !=
587             s->l2_size * sizeof(uint64_t))
588             return NULL;
589     }
590     /* write the l2 table to the file */
591     if (bdrv_pwrite(s->hd, l2_offset,
592                     l2_table, s->l2_size * sizeof(uint64_t)) !=
593         s->l2_size * sizeof(uint64_t))
594         return NULL;
595
596     /* update the l2 cache entry */
597
598     s->l2_cache_offsets[min_index] = l2_offset;
599     s->l2_cache_counts[min_index] = 1;
600
601     return l2_table;
602 }
603
604 static int size_to_clusters(BDRVQcowState *s, int64_t size)
605 {
606     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
607 }
608
609 static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
610         uint64_t *l2_table, uint64_t mask)
611 {
612     int i;
613     uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
614
615     for (i = 0; i < nb_clusters; i++)
616         if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
617             break;
618
619         return i;
620 }
621
622 static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
623 {
624     int i = 0;
625
626     while(nb_clusters-- && l2_table[i] == 0)
627         i++;
628
629     return i;
630 }
631
632 /*
633  * get_cluster_offset
634  *
635  * For a given offset of the disk image, return cluster offset in
636  * qcow2 file.
637  *
638  * on entry, *num is the number of contiguous clusters we'd like to
639  * access following offset.
640  *
641  * on exit, *num is the number of contiguous clusters we can read.
642  *
643  * Return 1, if the offset is found
644  * Return 0, otherwise.
645  *
646  */
647
648 static uint64_t get_cluster_offset(BlockDriverState *bs,
649                                    uint64_t offset, int *num)
650 {
651     BDRVQcowState *s = bs->opaque;
652     int l1_index, l2_index;
653     uint64_t l2_offset, *l2_table, cluster_offset;
654     int l1_bits, c;
655     int index_in_cluster, nb_available, nb_needed, nb_clusters;
656
657     index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
658     nb_needed = *num + index_in_cluster;
659
660     l1_bits = s->l2_bits + s->cluster_bits;
661
662     /* compute how many bytes there are between the offset and
663      * the end of the l1 entry
664      */
665
666     nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
667
668     /* compute the number of available sectors */
669
670     nb_available = (nb_available >> 9) + index_in_cluster;
671
672     cluster_offset = 0;
673
674     /* seek the the l2 offset in the l1 table */
675
676     l1_index = offset >> l1_bits;
677     if (l1_index >= s->l1_size)
678         goto out;
679
680     l2_offset = s->l1_table[l1_index];
681
682     /* seek the l2 table of the given l2 offset */
683
684     if (!l2_offset)
685         goto out;
686
687     /* load the l2 table in memory */
688
689     l2_offset &= ~QCOW_OFLAG_COPIED;
690     l2_table = l2_load(bs, l2_offset);
691     if (l2_table == NULL)
692         return 0;
693
694     /* find the cluster offset for the given disk offset */
695
696     l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
697     cluster_offset = be64_to_cpu(l2_table[l2_index]);
698     nb_clusters = size_to_clusters(s, nb_needed << 9);
699
700     if (!cluster_offset) {
701         /* how many empty clusters ? */
702         c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
703     } else {
704         /* how many allocated clusters ? */
705         c = count_contiguous_clusters(nb_clusters, s->cluster_size,
706                 &l2_table[l2_index], QCOW_OFLAG_COPIED);
707     }
708
709    nb_available = (c * s->cluster_sectors);
710 out:
711     if (nb_available > nb_needed)
712         nb_available = nb_needed;
713
714     *num = nb_available - index_in_cluster;
715
716     return cluster_offset & ~QCOW_OFLAG_COPIED;
717 }
718
719 /*
720  * free_any_clusters
721  *
722  * free clusters according to its type: compressed or not
723  *
724  */
725
726 static void free_any_clusters(BlockDriverState *bs,
727                               uint64_t cluster_offset, int nb_clusters)
728 {
729     BDRVQcowState *s = bs->opaque;
730
731     /* free the cluster */
732
733     if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
734         int nb_csectors;
735         nb_csectors = ((cluster_offset >> s->csize_shift) &
736                        s->csize_mask) + 1;
737         free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
738                       nb_csectors * 512);
739         return;
740     }
741
742     free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
743
744     return;
745 }
746
747 /*
748  * get_cluster_table
749  *
750  * for a given disk offset, load (and allocate if needed)
751  * the l2 table.
752  *
753  * the l2 table offset in the qcow2 file and the cluster index
754  * in the l2 table are given to the caller.
755  *
756  */
757
758 static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
759                              uint64_t **new_l2_table,
760                              uint64_t *new_l2_offset,
761                              int *new_l2_index)
762 {
763     BDRVQcowState *s = bs->opaque;
764     int l1_index, l2_index, ret;
765     uint64_t l2_offset, *l2_table;
766
767     /* seek the the l2 offset in the l1 table */
768
769     l1_index = offset >> (s->l2_bits + s->cluster_bits);
770     if (l1_index >= s->l1_size) {
771         ret = grow_l1_table(bs, l1_index + 1);
772         if (ret < 0)
773             return 0;
774     }
775     l2_offset = s->l1_table[l1_index];
776
777     /* seek the l2 table of the given l2 offset */
778
779     if (l2_offset & QCOW_OFLAG_COPIED) {
780         /* load the l2 table in memory */
781         l2_offset &= ~QCOW_OFLAG_COPIED;
782         l2_table = l2_load(bs, l2_offset);
783         if (l2_table == NULL)
784             return 0;
785     } else {
786         if (l2_offset)
787             free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
788         l2_table = l2_allocate(bs, l1_index);
789         if (l2_table == NULL)
790             return 0;
791         l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
792     }
793
794     /* find the cluster offset for the given disk offset */
795
796     l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
797
798     *new_l2_table = l2_table;
799     *new_l2_offset = l2_offset;
800     *new_l2_index = l2_index;
801
802     return 1;
803 }
804
805 /*
806  * alloc_compressed_cluster_offset
807  *
808  * For a given offset of the disk image, return cluster offset in
809  * qcow2 file.
810  *
811  * If the offset is not found, allocate a new compressed cluster.
812  *
813  * Return the cluster offset if successful,
814  * Return 0, otherwise.
815  *
816  */
817
818 static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
819                                                 uint64_t offset,
820                                                 int compressed_size)
821 {
822     BDRVQcowState *s = bs->opaque;
823     int l2_index, ret;
824     uint64_t l2_offset, *l2_table, cluster_offset;
825     int nb_csectors;
826
827     ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
828     if (ret == 0)
829         return 0;
830
831     cluster_offset = be64_to_cpu(l2_table[l2_index]);
832     if (cluster_offset & QCOW_OFLAG_COPIED)
833         return cluster_offset & ~QCOW_OFLAG_COPIED;
834
835     if (cluster_offset)
836         free_any_clusters(bs, cluster_offset, 1);
837
838     cluster_offset = alloc_bytes(bs, compressed_size);
839     nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
840                   (cluster_offset >> 9);
841
842     cluster_offset |= QCOW_OFLAG_COMPRESSED |
843                       ((uint64_t)nb_csectors << s->csize_shift);
844
845     /* update L2 table */
846
847     /* compressed clusters never have the copied flag */
848
849     l2_table[l2_index] = cpu_to_be64(cluster_offset);
850     if (bdrv_pwrite(s->hd,
851                     l2_offset + l2_index * sizeof(uint64_t),
852                     l2_table + l2_index,
853                     sizeof(uint64_t)) != sizeof(uint64_t))
854         return 0;
855
856     return cluster_offset;
857 }
858
859 typedef struct QCowL2Meta
860 {
861     uint64_t offset;
862     int n_start;
863     int nb_available;
864     int nb_clusters;
865 } QCowL2Meta;
866
867 static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
868         QCowL2Meta *m)
869 {
870     BDRVQcowState *s = bs->opaque;
871     int i, j = 0, l2_index, ret;
872     uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
873
874     if (m->nb_clusters == 0)
875         return 0;
876
877     if (!(old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t))))
878         return -ENOMEM;
879
880     /* copy content of unmodified sectors */
881     start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
882     if (m->n_start) {
883         ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
884         if (ret < 0)
885             goto err;
886     }
887
888     if (m->nb_available & (s->cluster_sectors - 1)) {
889         uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
890         ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
891                 m->nb_available - end, s->cluster_sectors);
892         if (ret < 0)
893             goto err;
894     }
895
896     ret = -EIO;
897     /* update L2 table */
898     if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
899         goto err;
900
901     for (i = 0; i < m->nb_clusters; i++) {
902         if(l2_table[l2_index + i] != 0)
903             old_cluster[j++] = l2_table[l2_index + i];
904
905         l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
906                     (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
907      }
908
909     if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),
910                 l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=
911             m->nb_clusters * sizeof(uint64_t))
912         goto err;
913
914     for (i = 0; i < j; i++)
915         free_any_clusters(bs, old_cluster[i], 1);
916
917     ret = 0;
918 err:
919     qemu_free(old_cluster);
920     return ret;
921  }
922
923 /*
924  * alloc_cluster_offset
925  *
926  * For a given offset of the disk image, return cluster offset in
927  * qcow2 file.
928  *
929  * If the offset is not found, allocate a new cluster.
930  *
931  * Return the cluster offset if successful,
932  * Return 0, otherwise.
933  *
934  */
935
936 static uint64_t alloc_cluster_offset(BlockDriverState *bs,
937                                      uint64_t offset,
938                                      int n_start, int n_end,
939                                      int *num, QCowL2Meta *m)
940 {
941     BDRVQcowState *s = bs->opaque;
942     int l2_index, ret;
943     uint64_t l2_offset, *l2_table, cluster_offset;
944     int nb_clusters, i = 0;
945
946     ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
947     if (ret == 0)
948         return 0;
949
950     nb_clusters = size_to_clusters(s, n_end << 9);
951
952     nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
953
954     cluster_offset = be64_to_cpu(l2_table[l2_index]);
955
956     /* We keep all QCOW_OFLAG_COPIED clusters */
957
958     if (cluster_offset & QCOW_OFLAG_COPIED) {
959         nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
960                 &l2_table[l2_index], 0);
961
962         cluster_offset &= ~QCOW_OFLAG_COPIED;
963         m->nb_clusters = 0;
964
965         goto out;
966     }
967
968     /* for the moment, multiple compressed clusters are not managed */
969
970     if (cluster_offset & QCOW_OFLAG_COMPRESSED)
971         nb_clusters = 1;
972
973     /* how many available clusters ? */
974
975     while (i < nb_clusters) {
976         i += count_contiguous_free_clusters(nb_clusters - i,
977                 &l2_table[l2_index + i]);
978
979         cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
980
981         if ((cluster_offset & QCOW_OFLAG_COPIED) ||
982                 (cluster_offset & QCOW_OFLAG_COMPRESSED))
983             break;
984
985         i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
986                 &l2_table[l2_index + i], 0);
987
988         if(be64_to_cpu(l2_table[l2_index + i]))
989             break;
990     }
991     nb_clusters = i;
992
993     /* allocate a new cluster */
994
995     cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
996
997     /* save info needed for meta data update */
998     m->offset = offset;
999     m->n_start = n_start;
1000     m->nb_clusters = nb_clusters;
1001
1002 out:
1003     m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
1004
1005     *num = m->nb_available - n_start;
1006
1007     return cluster_offset;
1008 }
1009
1010 static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
1011                              int nb_sectors, int *pnum)
1012 {
1013     uint64_t cluster_offset;
1014
1015     *pnum = nb_sectors;
1016     cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
1017
1018     return (cluster_offset != 0);
1019 }
1020
1021 static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1022                              const uint8_t *buf, int buf_size)
1023 {
1024     z_stream strm1, *strm = &strm1;
1025     int ret, out_len;
1026
1027     memset(strm, 0, sizeof(*strm));
1028
1029     strm->next_in = (uint8_t *)buf;
1030     strm->avail_in = buf_size;
1031     strm->next_out = out_buf;
1032     strm->avail_out = out_buf_size;
1033
1034     ret = inflateInit2(strm, -12);
1035     if (ret != Z_OK)
1036         return -1;
1037     ret = inflate(strm, Z_FINISH);
1038     out_len = strm->next_out - out_buf;
1039     if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1040         out_len != out_buf_size) {
1041         inflateEnd(strm);
1042         return -1;
1043     }
1044     inflateEnd(strm);
1045     return 0;
1046 }
1047
1048 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1049 {
1050     int ret, csize, nb_csectors, sector_offset;
1051     uint64_t coffset;
1052
1053     coffset = cluster_offset & s->cluster_offset_mask;
1054     if (s->cluster_cache_offset != coffset) {
1055         nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1056         sector_offset = coffset & 511;
1057         csize = nb_csectors * 512 - sector_offset;
1058         ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1059         if (ret < 0) {
1060             return -1;
1061         }
1062         if (decompress_buffer(s->cluster_cache, s->cluster_size,
1063                               s->cluster_data + sector_offset, csize) < 0) {
1064             return -1;
1065         }
1066         s->cluster_cache_offset = coffset;
1067     }
1068     return 0;
1069 }
1070
1071 /* handle reading after the end of the backing file */
1072 static int backing_read1(BlockDriverState *bs,
1073                          int64_t sector_num, uint8_t *buf, int nb_sectors)
1074 {
1075     int n1;
1076     if ((sector_num + nb_sectors) <= bs->total_sectors)
1077         return nb_sectors;
1078     if (sector_num >= bs->total_sectors)
1079         n1 = 0;
1080     else
1081         n1 = bs->total_sectors - sector_num;
1082     memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1083     return n1;
1084 }
1085
1086 static int qcow_read(BlockDriverState *bs, int64_t sector_num,
1087                      uint8_t *buf, int nb_sectors)
1088 {
1089     BDRVQcowState *s = bs->opaque;
1090     int ret, index_in_cluster, n, n1;
1091     uint64_t cluster_offset;
1092
1093     while (nb_sectors > 0) {
1094         n = nb_sectors;
1095         cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
1096         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1097         if (!cluster_offset) {
1098             if (bs->backing_hd) {
1099                 /* read from the base image */
1100                 n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1101                 if (n1 > 0) {
1102                     ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1103                     if (ret < 0)
1104                         return -1;
1105                 }
1106             } else {
1107                 memset(buf, 0, 512 * n);
1108             }
1109         } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1110             if (decompress_cluster(s, cluster_offset) < 0)
1111                 return -1;
1112             memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1113         } else {
1114             ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1115             if (ret != n * 512)
1116                 return -1;
1117             if (s->crypt_method) {
1118                 encrypt_sectors(s, sector_num, buf, buf, n, 0,
1119                                 &s->aes_decrypt_key);
1120             }
1121         }
1122         nb_sectors -= n;
1123         sector_num += n;
1124         buf += n * 512;
1125     }
1126     return 0;
1127 }
1128
1129 static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1130                      const uint8_t *buf, int nb_sectors)
1131 {
1132     BDRVQcowState *s = bs->opaque;
1133     int ret, index_in_cluster, n;
1134     uint64_t cluster_offset;
1135     int n_end;
1136     QCowL2Meta l2meta;
1137
1138     while (nb_sectors > 0) {
1139         index_in_cluster = sector_num & (s->cluster_sectors - 1);
1140         n_end = index_in_cluster + nb_sectors;
1141         if (s->crypt_method &&
1142             n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1143             n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1144         cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1145                                               index_in_cluster,
1146                                               n_end, &n, &l2meta);
1147         if (!cluster_offset)
1148             return -1;
1149         if (s->crypt_method) {
1150             encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1151                             &s->aes_encrypt_key);
1152             ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1153                               s->cluster_data, n * 512);
1154         } else {
1155             ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1156         }
1157         if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) {
1158             free_any_clusters(bs, cluster_offset, l2meta.nb_clusters);
1159             return -1;
1160         }
1161         nb_sectors -= n;
1162         sector_num += n;
1163         buf += n * 512;
1164     }
1165     s->cluster_cache_offset = -1; /* disable compressed cache */
1166     return 0;
1167 }
1168
1169 typedef struct QCowAIOCB {
1170     BlockDriverAIOCB common;
1171     int64_t sector_num;
1172     uint8_t *buf;
1173     int nb_sectors;
1174     int n;
1175     uint64_t cluster_offset;
1176     uint8_t *cluster_data;
1177     BlockDriverAIOCB *hd_aiocb;
1178     QEMUBH *bh;
1179     QCowL2Meta l2meta;
1180 } QCowAIOCB;
1181
1182 static void qcow_aio_read_cb(void *opaque, int ret);
1183 static void qcow_aio_read_bh(void *opaque)
1184 {
1185     QCowAIOCB *acb = opaque;
1186     qemu_bh_delete(acb->bh);
1187     acb->bh = NULL;
1188     qcow_aio_read_cb(opaque, 0);
1189 }
1190
1191 static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
1192 {
1193     if (acb->bh)
1194         return -EIO;
1195
1196     acb->bh = qemu_bh_new(cb, acb);
1197     if (!acb->bh)
1198         return -EIO;
1199
1200     qemu_bh_schedule(acb->bh);
1201
1202     return 0;
1203 }
1204
1205 static void qcow_aio_read_cb(void *opaque, int ret)
1206 {
1207     QCowAIOCB *acb = opaque;
1208     BlockDriverState *bs = acb->common.bs;
1209     BDRVQcowState *s = bs->opaque;
1210     int index_in_cluster, n1;
1211
1212     acb->hd_aiocb = NULL;
1213     if (ret < 0) {
1214 fail:
1215         acb->common.cb(acb->common.opaque, ret);
1216         qemu_aio_release(acb);
1217         return;
1218     }
1219
1220     /* post process the read buffer */
1221     if (!acb->cluster_offset) {
1222         /* nothing to do */
1223     } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1224         /* nothing to do */
1225     } else {
1226         if (s->crypt_method) {
1227             encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1228                             acb->n, 0,
1229                             &s->aes_decrypt_key);
1230         }
1231     }
1232
1233     acb->nb_sectors -= acb->n;
1234     acb->sector_num += acb->n;
1235     acb->buf += acb->n * 512;
1236
1237     if (acb->nb_sectors == 0) {
1238         /* request completed */
1239         acb->common.cb(acb->common.opaque, 0);
1240         qemu_aio_release(acb);
1241         return;
1242     }
1243
1244     /* prepare next AIO request */
1245     acb->n = acb->nb_sectors;
1246     acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
1247     index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1248
1249     if (!acb->cluster_offset) {
1250         if (bs->backing_hd) {
1251             /* read from the base image */
1252             n1 = backing_read1(bs->backing_hd, acb->sector_num,
1253                                acb->buf, acb->n);
1254             if (n1 > 0) {
1255                 acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
1256                                     acb->buf, acb->n, qcow_aio_read_cb, acb);
1257                 if (acb->hd_aiocb == NULL)
1258                     goto fail;
1259             } else {
1260                 ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1261                 if (ret < 0)
1262                     goto fail;
1263             }
1264         } else {
1265             /* Note: in this case, no need to wait */
1266             memset(acb->buf, 0, 512 * acb->n);
1267             ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1268             if (ret < 0)
1269                 goto fail;
1270         }
1271     } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1272         /* add AIO support for compressed blocks ? */
1273         if (decompress_cluster(s, acb->cluster_offset) < 0)
1274             goto fail;
1275         memcpy(acb->buf,
1276                s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1277         ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1278         if (ret < 0)
1279             goto fail;
1280     } else {
1281         if ((acb->cluster_offset & 511) != 0) {
1282             ret = -EIO;
1283             goto fail;
1284         }
1285         acb->hd_aiocb = bdrv_aio_read(s->hd,
1286                             (acb->cluster_offset >> 9) + index_in_cluster,
1287                             acb->buf, acb->n, qcow_aio_read_cb, acb);
1288         if (acb->hd_aiocb == NULL)
1289             goto fail;
1290     }
1291 }
1292
1293 static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1294         int64_t sector_num, uint8_t *buf, int nb_sectors,
1295         BlockDriverCompletionFunc *cb, void *opaque)
1296 {
1297     QCowAIOCB *acb;
1298
1299     acb = qemu_aio_get(bs, cb, opaque);
1300     if (!acb)
1301         return NULL;
1302     acb->hd_aiocb = NULL;
1303     acb->sector_num = sector_num;
1304     acb->buf = buf;
1305     acb->nb_sectors = nb_sectors;
1306     acb->n = 0;
1307     acb->cluster_offset = 0;
1308     acb->l2meta.nb_clusters = 0;
1309     return acb;
1310 }
1311
1312 static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1313         int64_t sector_num, uint8_t *buf, int nb_sectors,
1314         BlockDriverCompletionFunc *cb, void *opaque)
1315 {
1316     QCowAIOCB *acb;
1317
1318     acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1319     if (!acb)
1320         return NULL;
1321
1322     qcow_aio_read_cb(acb, 0);
1323     return &acb->common;
1324 }
1325
1326 static void qcow_aio_write_cb(void *opaque, int ret)
1327 {
1328     QCowAIOCB *acb = opaque;
1329     BlockDriverState *bs = acb->common.bs;
1330     BDRVQcowState *s = bs->opaque;
1331     int index_in_cluster;
1332     const uint8_t *src_buf;
1333     int n_end;
1334
1335     acb->hd_aiocb = NULL;
1336
1337     if (ret < 0) {
1338     fail:
1339         acb->common.cb(acb->common.opaque, ret);
1340         qemu_aio_release(acb);
1341         return;
1342     }
1343
1344     if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
1345         free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
1346         goto fail;
1347     }
1348
1349     acb->nb_sectors -= acb->n;
1350     acb->sector_num += acb->n;
1351     acb->buf += acb->n * 512;
1352
1353     if (acb->nb_sectors == 0) {
1354         /* request completed */
1355         acb->common.cb(acb->common.opaque, 0);
1356         qemu_aio_release(acb);
1357         return;
1358     }
1359
1360     index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1361     n_end = index_in_cluster + acb->nb_sectors;
1362     if (s->crypt_method &&
1363         n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1364         n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1365
1366     acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
1367                                           index_in_cluster,
1368                                           n_end, &acb->n, &acb->l2meta);
1369     if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
1370         ret = -EIO;
1371         goto fail;
1372     }
1373     if (s->crypt_method) {
1374         if (!acb->cluster_data) {
1375             acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1376                                              s->cluster_size);
1377             if (!acb->cluster_data) {
1378                 ret = -ENOMEM;
1379                 goto fail;
1380             }
1381         }
1382         encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
1383                         acb->n, 1, &s->aes_encrypt_key);
1384         src_buf = acb->cluster_data;
1385     } else {
1386         src_buf = acb->buf;
1387     }
1388     acb->hd_aiocb = bdrv_aio_write(s->hd,
1389                                    (acb->cluster_offset >> 9) + index_in_cluster,
1390                                    src_buf, acb->n,
1391                                    qcow_aio_write_cb, acb);
1392     if (acb->hd_aiocb == NULL)
1393         goto fail;
1394 }
1395
1396 static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1397         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1398         BlockDriverCompletionFunc *cb, void *opaque)
1399 {
1400     BDRVQcowState *s = bs->opaque;
1401     QCowAIOCB *acb;
1402
1403     s->cluster_cache_offset = -1; /* disable compressed cache */
1404
1405     acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1406     if (!acb)
1407         return NULL;
1408
1409     qcow_aio_write_cb(acb, 0);
1410     return &acb->common;
1411 }
1412
1413 static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1414 {
1415     QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1416     if (acb->hd_aiocb)
1417         bdrv_aio_cancel(acb->hd_aiocb);
1418     qemu_aio_release(acb);
1419 }
1420
1421 static void qcow_close(BlockDriverState *bs)
1422 {
1423     BDRVQcowState *s = bs->opaque;
1424     qemu_free(s->l1_table);
1425     qemu_free(s->l2_cache);
1426     qemu_free(s->cluster_cache);
1427     qemu_free(s->cluster_data);
1428     refcount_close(bs);
1429     bdrv_delete(s->hd);
1430 }
1431
1432 /* XXX: use std qcow open function ? */
1433 typedef struct QCowCreateState {
1434     int cluster_size;
1435     int cluster_bits;
1436     uint16_t *refcount_block;
1437     uint64_t *refcount_table;
1438     int64_t l1_table_offset;
1439     int64_t refcount_table_offset;
1440     int64_t refcount_block_offset;
1441 } QCowCreateState;
1442
1443 static void create_refcount_update(QCowCreateState *s,
1444                                    int64_t offset, int64_t size)
1445 {
1446     int refcount;
1447     int64_t start, last, cluster_offset;
1448     uint16_t *p;
1449
1450     start = offset & ~(s->cluster_size - 1);
1451     last = (offset + size - 1)  & ~(s->cluster_size - 1);
1452     for(cluster_offset = start; cluster_offset <= last;
1453         cluster_offset += s->cluster_size) {
1454         p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1455         refcount = be16_to_cpu(*p);
1456         refcount++;
1457         *p = cpu_to_be16(refcount);
1458     }
1459 }
1460
1461 static int qcow_create(const char *filename, int64_t total_size,
1462                       const char *backing_file, int flags)
1463 {
1464     int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1465     QCowHeader header;
1466     uint64_t tmp, offset;
1467     QCowCreateState s1, *s = &s1;
1468
1469     memset(s, 0, sizeof(*s));
1470
1471     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1472     if (fd < 0)
1473         return -1;
1474     memset(&header, 0, sizeof(header));
1475     header.magic = cpu_to_be32(QCOW_MAGIC);
1476     header.version = cpu_to_be32(QCOW_VERSION);
1477     header.size = cpu_to_be64(total_size * 512);
1478     header_size = sizeof(header);
1479     backing_filename_len = 0;
1480     if (backing_file) {
1481         header.backing_file_offset = cpu_to_be64(header_size);
1482         backing_filename_len = strlen(backing_file);
1483         header.backing_file_size = cpu_to_be32(backing_filename_len);
1484         header_size += backing_filename_len;
1485     }
1486     s->cluster_bits = 12;  /* 4 KB clusters */
1487     s->cluster_size = 1 << s->cluster_bits;
1488     header.cluster_bits = cpu_to_be32(s->cluster_bits);
1489     header_size = (header_size + 7) & ~7;
1490     if (flags & BLOCK_FLAG_ENCRYPT) {
1491         header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1492     } else {
1493         header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1494     }
1495     l2_bits = s->cluster_bits - 3;
1496     shift = s->cluster_bits + l2_bits;
1497     l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1498     offset = align_offset(header_size, s->cluster_size);
1499     s->l1_table_offset = offset;
1500     header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1501     header.l1_size = cpu_to_be32(l1_size);
1502     offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1503
1504     s->refcount_table = qemu_mallocz(s->cluster_size);
1505     if (!s->refcount_table)
1506         goto fail;
1507     s->refcount_block = qemu_mallocz(s->cluster_size);
1508     if (!s->refcount_block)
1509         goto fail;
1510
1511     s->refcount_table_offset = offset;
1512     header.refcount_table_offset = cpu_to_be64(offset);
1513     header.refcount_table_clusters = cpu_to_be32(1);
1514     offset += s->cluster_size;
1515
1516     s->refcount_table[0] = cpu_to_be64(offset);
1517     s->refcount_block_offset = offset;
1518     offset += s->cluster_size;
1519
1520     /* update refcounts */
1521     create_refcount_update(s, 0, header_size);
1522     create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1523     create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1524     create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
1525
1526     /* write all the data */
1527     write(fd, &header, sizeof(header));
1528     if (backing_file) {
1529         write(fd, backing_file, backing_filename_len);
1530     }
1531     lseek(fd, s->l1_table_offset, SEEK_SET);
1532     tmp = 0;
1533     for(i = 0;i < l1_size; i++) {
1534         write(fd, &tmp, sizeof(tmp));
1535     }
1536     lseek(fd, s->refcount_table_offset, SEEK_SET);
1537     write(fd, s->refcount_table, s->cluster_size);
1538
1539     lseek(fd, s->refcount_block_offset, SEEK_SET);
1540     write(fd, s->refcount_block, s->cluster_size);
1541
1542     qemu_free(s->refcount_table);
1543     qemu_free(s->refcount_block);
1544     close(fd);
1545     return 0;
1546  fail:
1547     qemu_free(s->refcount_table);
1548     qemu_free(s->refcount_block);
1549     close(fd);
1550     return -ENOMEM;
1551 }
1552
1553 static int qcow_make_empty(BlockDriverState *bs)
1554 {
1555 #if 0
1556     /* XXX: not correct */
1557     BDRVQcowState *s = bs->opaque;
1558     uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1559     int ret;
1560
1561     memset(s->l1_table, 0, l1_length);
1562     if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1563         return -1;
1564     ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1565     if (ret < 0)
1566         return ret;
1567
1568     l2_cache_reset(bs);
1569 #endif
1570     return 0;
1571 }
1572
1573 /* XXX: put compressed sectors first, then all the cluster aligned
1574    tables to avoid losing bytes in alignment */
1575 static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1576                                  const uint8_t *buf, int nb_sectors)
1577 {
1578     BDRVQcowState *s = bs->opaque;
1579     z_stream strm;
1580     int ret, out_len;
1581     uint8_t *out_buf;
1582     uint64_t cluster_offset;
1583
1584     if (nb_sectors == 0) {
1585         /* align end of file to a sector boundary to ease reading with
1586            sector based I/Os */
1587         cluster_offset = bdrv_getlength(s->hd);
1588         cluster_offset = (cluster_offset + 511) & ~511;
1589         bdrv_truncate(s->hd, cluster_offset);
1590         return 0;
1591     }
1592
1593     if (nb_sectors != s->cluster_sectors)
1594         return -EINVAL;
1595
1596     out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1597     if (!out_buf)
1598         return -ENOMEM;
1599
1600     /* best compression, small window, no zlib header */
1601     memset(&strm, 0, sizeof(strm));
1602     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1603                        Z_DEFLATED, -12,
1604                        9, Z_DEFAULT_STRATEGY);
1605     if (ret != 0) {
1606         qemu_free(out_buf);
1607         return -1;
1608     }
1609
1610     strm.avail_in = s->cluster_size;
1611     strm.next_in = (uint8_t *)buf;
1612     strm.avail_out = s->cluster_size;
1613     strm.next_out = out_buf;
1614
1615     ret = deflate(&strm, Z_FINISH);
1616     if (ret != Z_STREAM_END && ret != Z_OK) {
1617         qemu_free(out_buf);
1618         deflateEnd(&strm);
1619         return -1;
1620     }
1621     out_len = strm.next_out - out_buf;
1622
1623     deflateEnd(&strm);
1624
1625     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1626         /* could not compress: write normal cluster */
1627         qcow_write(bs, sector_num, buf, s->cluster_sectors);
1628     } else {
1629         cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1630                                               out_len);
1631         if (!cluster_offset)
1632             return -1;
1633         cluster_offset &= s->cluster_offset_mask;
1634         if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1635             qemu_free(out_buf);
1636             return -1;
1637         }
1638     }
1639
1640     qemu_free(out_buf);
1641     return 0;
1642 }
1643
1644 static void qcow_flush(BlockDriverState *bs)
1645 {
1646     BDRVQcowState *s = bs->opaque;
1647     bdrv_flush(s->hd);
1648 }
1649
1650 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1651 {
1652     BDRVQcowState *s = bs->opaque;
1653     bdi->cluster_size = s->cluster_size;
1654     bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1655         (s->cluster_bits + s->l2_bits);
1656     return 0;
1657 }
1658
1659 /*********************************************************/
1660 /* snapshot support */
1661
1662 /* update the refcounts of snapshots and the copied flag */
1663 static int update_snapshot_refcount(BlockDriverState *bs,
1664                                     int64_t l1_table_offset,
1665                                     int l1_size,
1666                                     int addend)
1667 {
1668     BDRVQcowState *s = bs->opaque;
1669     uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1670     int64_t old_offset, old_l2_offset;
1671     int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1672
1673     l2_cache_reset(bs);
1674
1675     l2_table = NULL;
1676     l1_table = NULL;
1677     l1_size2 = l1_size * sizeof(uint64_t);
1678     l1_allocated = 0;
1679     if (l1_table_offset != s->l1_table_offset) {
1680         l1_table = qemu_malloc(l1_size2);
1681         if (!l1_table)
1682             goto fail;
1683         l1_allocated = 1;
1684         if (bdrv_pread(s->hd, l1_table_offset,
1685                        l1_table, l1_size2) != l1_size2)
1686             goto fail;
1687         for(i = 0;i < l1_size; i++)
1688             be64_to_cpus(&l1_table[i]);
1689     } else {
1690         assert(l1_size == s->l1_size);
1691         l1_table = s->l1_table;
1692         l1_allocated = 0;
1693     }
1694
1695     l2_size = s->l2_size * sizeof(uint64_t);
1696     l2_table = qemu_malloc(l2_size);
1697     if (!l2_table)
1698         goto fail;
1699     l1_modified = 0;
1700     for(i = 0; i < l1_size; i++) {
1701         l2_offset = l1_table[i];
1702         if (l2_offset) {
1703             old_l2_offset = l2_offset;
1704             l2_offset &= ~QCOW_OFLAG_COPIED;
1705             l2_modified = 0;
1706             if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1707                 goto fail;
1708             for(j = 0; j < s->l2_size; j++) {
1709                 offset = be64_to_cpu(l2_table[j]);
1710                 if (offset != 0) {
1711                     old_offset = offset;
1712                     offset &= ~QCOW_OFLAG_COPIED;
1713                     if (offset & QCOW_OFLAG_COMPRESSED) {
1714                         nb_csectors = ((offset >> s->csize_shift) &
1715                                        s->csize_mask) + 1;
1716                         if (addend != 0)
1717                             update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1718                                             nb_csectors * 512, addend);
1719                         /* compressed clusters are never modified */
1720                         refcount = 2;
1721                     } else {
1722                         if (addend != 0) {
1723                             refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1724                         } else {
1725                             refcount = get_refcount(bs, offset >> s->cluster_bits);
1726                         }
1727                     }
1728
1729                     if (refcount == 1) {
1730                         offset |= QCOW_OFLAG_COPIED;
1731                     }
1732                     if (offset != old_offset) {
1733                         l2_table[j] = cpu_to_be64(offset);
1734                         l2_modified = 1;
1735                     }
1736                 }
1737             }
1738             if (l2_modified) {
1739                 if (bdrv_pwrite(s->hd,
1740                                 l2_offset, l2_table, l2_size) != l2_size)
1741                     goto fail;
1742             }
1743
1744             if (addend != 0) {
1745                 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1746             } else {
1747                 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1748             }
1749             if (refcount == 1) {
1750                 l2_offset |= QCOW_OFLAG_COPIED;
1751             }
1752             if (l2_offset != old_l2_offset) {
1753                 l1_table[i] = l2_offset;
1754                 l1_modified = 1;
1755             }
1756         }
1757     }
1758     if (l1_modified) {
1759         for(i = 0; i < l1_size; i++)
1760             cpu_to_be64s(&l1_table[i]);
1761         if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1762                         l1_size2) != l1_size2)
1763             goto fail;
1764         for(i = 0; i < l1_size; i++)
1765             be64_to_cpus(&l1_table[i]);
1766     }
1767     if (l1_allocated)
1768         qemu_free(l1_table);
1769     qemu_free(l2_table);
1770     return 0;
1771  fail:
1772     if (l1_allocated)
1773         qemu_free(l1_table);
1774     qemu_free(l2_table);
1775     return -EIO;
1776 }
1777
1778 static void qcow_free_snapshots(BlockDriverState *bs)
1779 {
1780     BDRVQcowState *s = bs->opaque;
1781     int i;
1782
1783     for(i = 0; i < s->nb_snapshots; i++) {
1784         qemu_free(s->snapshots[i].name);
1785         qemu_free(s->snapshots[i].id_str);
1786     }
1787     qemu_free(s->snapshots);
1788     s->snapshots = NULL;
1789     s->nb_snapshots = 0;
1790 }
1791
1792 static int qcow_read_snapshots(BlockDriverState *bs)
1793 {
1794     BDRVQcowState *s = bs->opaque;
1795     QCowSnapshotHeader h;
1796     QCowSnapshot *sn;
1797     int i, id_str_size, name_size;
1798     int64_t offset;
1799     uint32_t extra_data_size;
1800
1801     offset = s->snapshots_offset;
1802     s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1803     if (!s->snapshots)
1804         goto fail;
1805     for(i = 0; i < s->nb_snapshots; i++) {
1806         offset = align_offset(offset, 8);
1807         if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1808             goto fail;
1809         offset += sizeof(h);
1810         sn = s->snapshots + i;
1811         sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1812         sn->l1_size = be32_to_cpu(h.l1_size);
1813         sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1814         sn->date_sec = be32_to_cpu(h.date_sec);
1815         sn->date_nsec = be32_to_cpu(h.date_nsec);
1816         sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1817         extra_data_size = be32_to_cpu(h.extra_data_size);
1818
1819         id_str_size = be16_to_cpu(h.id_str_size);
1820         name_size = be16_to_cpu(h.name_size);
1821
1822         offset += extra_data_size;
1823
1824         sn->id_str = qemu_malloc(id_str_size + 1);
1825         if (!sn->id_str)
1826             goto fail;
1827         if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1828             goto fail;
1829         offset += id_str_size;
1830         sn->id_str[id_str_size] = '\0';
1831
1832         sn->name = qemu_malloc(name_size + 1);
1833         if (!sn->name)
1834             goto fail;
1835         if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1836             goto fail;
1837         offset += name_size;
1838         sn->name[name_size] = '\0';
1839     }
1840     s->snapshots_size = offset - s->snapshots_offset;
1841     return 0;
1842  fail:
1843     qcow_free_snapshots(bs);
1844     return -1;
1845 }
1846
1847 /* add at the end of the file a new list of snapshots */
1848 static int qcow_write_snapshots(BlockDriverState *bs)
1849 {
1850     BDRVQcowState *s = bs->opaque;
1851     QCowSnapshot *sn;
1852     QCowSnapshotHeader h;
1853     int i, name_size, id_str_size, snapshots_size;
1854     uint64_t data64;
1855     uint32_t data32;
1856     int64_t offset, snapshots_offset;
1857
1858     /* compute the size of the snapshots */
1859     offset = 0;
1860     for(i = 0; i < s->nb_snapshots; i++) {
1861         sn = s->snapshots + i;
1862         offset = align_offset(offset, 8);
1863         offset += sizeof(h);
1864         offset += strlen(sn->id_str);
1865         offset += strlen(sn->name);
1866     }
1867     snapshots_size = offset;
1868
1869     snapshots_offset = alloc_clusters(bs, snapshots_size);
1870     offset = snapshots_offset;
1871
1872     for(i = 0; i < s->nb_snapshots; i++) {
1873         sn = s->snapshots + i;
1874         memset(&h, 0, sizeof(h));
1875         h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1876         h.l1_size = cpu_to_be32(sn->l1_size);
1877         h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1878         h.date_sec = cpu_to_be32(sn->date_sec);
1879         h.date_nsec = cpu_to_be32(sn->date_nsec);
1880         h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1881
1882         id_str_size = strlen(sn->id_str);
1883         name_size = strlen(sn->name);
1884         h.id_str_size = cpu_to_be16(id_str_size);
1885         h.name_size = cpu_to_be16(name_size);
1886         offset = align_offset(offset, 8);
1887         if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1888             goto fail;
1889         offset += sizeof(h);
1890         if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1891             goto fail;
1892         offset += id_str_size;
1893         if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1894             goto fail;
1895         offset += name_size;
1896     }
1897
1898     /* update the various header fields */
1899     data64 = cpu_to_be64(snapshots_offset);
1900     if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1901                     &data64, sizeof(data64)) != sizeof(data64))
1902         goto fail;
1903     data32 = cpu_to_be32(s->nb_snapshots);
1904     if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1905                     &data32, sizeof(data32)) != sizeof(data32))
1906         goto fail;
1907
1908     /* free the old snapshot table */
1909     free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1910     s->snapshots_offset = snapshots_offset;
1911     s->snapshots_size = snapshots_size;
1912     return 0;
1913  fail:
1914     return -1;
1915 }
1916
1917 static void find_new_snapshot_id(BlockDriverState *bs,
1918                                  char *id_str, int id_str_size)
1919 {
1920     BDRVQcowState *s = bs->opaque;
1921     QCowSnapshot *sn;
1922     int i, id, id_max = 0;
1923
1924     for(i = 0; i < s->nb_snapshots; i++) {
1925         sn = s->snapshots + i;
1926         id = strtoul(sn->id_str, NULL, 10);
1927         if (id > id_max)
1928             id_max = id;
1929     }
1930     snprintf(id_str, id_str_size, "%d", id_max + 1);
1931 }
1932
1933 static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1934 {
1935     BDRVQcowState *s = bs->opaque;
1936     int i;
1937
1938     for(i = 0; i < s->nb_snapshots; i++) {
1939         if (!strcmp(s->snapshots[i].id_str, id_str))
1940             return i;
1941     }
1942     return -1;
1943 }
1944
1945 static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1946 {
1947     BDRVQcowState *s = bs->opaque;
1948     int i, ret;
1949
1950     ret = find_snapshot_by_id(bs, name);
1951     if (ret >= 0)
1952         return ret;
1953     for(i = 0; i < s->nb_snapshots; i++) {
1954         if (!strcmp(s->snapshots[i].name, name))
1955             return i;
1956     }
1957     return -1;
1958 }
1959
1960 /* if no id is provided, a new one is constructed */
1961 static int qcow_snapshot_create(BlockDriverState *bs,
1962                                 QEMUSnapshotInfo *sn_info)
1963 {
1964     BDRVQcowState *s = bs->opaque;
1965     QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1966     int i, ret;
1967     uint64_t *l1_table = NULL;
1968
1969     memset(sn, 0, sizeof(*sn));
1970
1971     if (sn_info->id_str[0] == '\0') {
1972         /* compute a new id */
1973         find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
1974     }
1975
1976     /* check that the ID is unique */
1977     if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
1978         return -ENOENT;
1979
1980     sn->id_str = qemu_strdup(sn_info->id_str);
1981     if (!sn->id_str)
1982         goto fail;
1983     sn->name = qemu_strdup(sn_info->name);
1984     if (!sn->name)
1985         goto fail;
1986     sn->vm_state_size = sn_info->vm_state_size;
1987     sn->date_sec = sn_info->date_sec;
1988     sn->date_nsec = sn_info->date_nsec;
1989     sn->vm_clock_nsec = sn_info->vm_clock_nsec;
1990
1991     ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
1992     if (ret < 0)
1993         goto fail;
1994
1995     /* create the L1 table of the snapshot */
1996     sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
1997     sn->l1_size = s->l1_size;
1998
1999     l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
2000     if (!l1_table)
2001         goto fail;
2002     for(i = 0; i < s->l1_size; i++) {
2003         l1_table[i] = cpu_to_be64(s->l1_table[i]);
2004     }
2005     if (bdrv_pwrite(s->hd, sn->l1_table_offset,
2006                     l1_table, s->l1_size * sizeof(uint64_t)) !=
2007         (s->l1_size * sizeof(uint64_t)))
2008         goto fail;
2009     qemu_free(l1_table);
2010     l1_table = NULL;
2011
2012     snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
2013     if (!snapshots1)
2014         goto fail;
2015     memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
2016     s->snapshots = snapshots1;
2017     s->snapshots[s->nb_snapshots++] = *sn;
2018
2019     if (qcow_write_snapshots(bs) < 0)
2020         goto fail;
2021 #ifdef DEBUG_ALLOC
2022     check_refcounts(bs);
2023 #endif
2024     return 0;
2025  fail:
2026     qemu_free(sn->name);
2027     qemu_free(l1_table);
2028     return -1;
2029 }
2030
2031 /* copy the snapshot 'snapshot_name' into the current disk image */
2032 static int qcow_snapshot_goto(BlockDriverState *bs,
2033                               const char *snapshot_id)
2034 {
2035     BDRVQcowState *s = bs->opaque;
2036     QCowSnapshot *sn;
2037     int i, snapshot_index, l1_size2;
2038
2039     snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2040     if (snapshot_index < 0)
2041         return -ENOENT;
2042     sn = &s->snapshots[snapshot_index];
2043
2044     if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
2045         goto fail;
2046
2047     if (grow_l1_table(bs, sn->l1_size) < 0)
2048         goto fail;
2049
2050     s->l1_size = sn->l1_size;
2051     l1_size2 = s->l1_size * sizeof(uint64_t);
2052     /* copy the snapshot l1 table to the current l1 table */
2053     if (bdrv_pread(s->hd, sn->l1_table_offset,
2054                    s->l1_table, l1_size2) != l1_size2)
2055         goto fail;
2056     if (bdrv_pwrite(s->hd, s->l1_table_offset,
2057                     s->l1_table, l1_size2) != l1_size2)
2058         goto fail;
2059     for(i = 0;i < s->l1_size; i++) {
2060         be64_to_cpus(&s->l1_table[i]);
2061     }
2062
2063     if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2064         goto fail;
2065
2066 #ifdef DEBUG_ALLOC
2067     check_refcounts(bs);
2068 #endif
2069     return 0;
2070  fail:
2071     return -EIO;
2072 }
2073
2074 static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2075 {
2076     BDRVQcowState *s = bs->opaque;
2077     QCowSnapshot *sn;
2078     int snapshot_index, ret;
2079
2080     snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2081     if (snapshot_index < 0)
2082         return -ENOENT;
2083     sn = &s->snapshots[snapshot_index];
2084
2085     ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2086     if (ret < 0)
2087         return ret;
2088     /* must update the copied flag on the current cluster offsets */
2089     ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2090     if (ret < 0)
2091         return ret;
2092     free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2093
2094     qemu_free(sn->id_str);
2095     qemu_free(sn->name);
2096     memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2097     s->nb_snapshots--;
2098     ret = qcow_write_snapshots(bs);
2099     if (ret < 0) {
2100         /* XXX: restore snapshot if error ? */
2101         return ret;
2102     }
2103 #ifdef DEBUG_ALLOC
2104     check_refcounts(bs);
2105 #endif
2106     return 0;
2107 }
2108
2109 static int qcow_snapshot_list(BlockDriverState *bs,
2110                               QEMUSnapshotInfo **psn_tab)
2111 {
2112     BDRVQcowState *s = bs->opaque;
2113     QEMUSnapshotInfo *sn_tab, *sn_info;
2114     QCowSnapshot *sn;
2115     int i;
2116
2117     sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2118     if (!sn_tab)
2119         goto fail;
2120     for(i = 0; i < s->nb_snapshots; i++) {
2121         sn_info = sn_tab + i;
2122         sn = s->snapshots + i;
2123         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2124                 sn->id_str);
2125         pstrcpy(sn_info->name, sizeof(sn_info->name),
2126                 sn->name);
2127         sn_info->vm_state_size = sn->vm_state_size;
2128         sn_info->date_sec = sn->date_sec;
2129         sn_info->date_nsec = sn->date_nsec;
2130         sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2131     }
2132     *psn_tab = sn_tab;
2133     return s->nb_snapshots;
2134  fail:
2135     qemu_free(sn_tab);
2136     *psn_tab = NULL;
2137     return -ENOMEM;
2138 }
2139
2140 /*********************************************************/
2141 /* refcount handling */
2142
2143 static int refcount_init(BlockDriverState *bs)
2144 {
2145     BDRVQcowState *s = bs->opaque;
2146     int ret, refcount_table_size2, i;
2147
2148     s->refcount_block_cache = qemu_malloc(s->cluster_size);
2149     if (!s->refcount_block_cache)
2150         goto fail;
2151     refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2152     s->refcount_table = qemu_malloc(refcount_table_size2);
2153     if (!s->refcount_table)
2154         goto fail;
2155     if (s->refcount_table_size > 0) {
2156         ret = bdrv_pread(s->hd, s->refcount_table_offset,
2157                          s->refcount_table, refcount_table_size2);
2158         if (ret != refcount_table_size2)
2159             goto fail;
2160         for(i = 0; i < s->refcount_table_size; i++)
2161             be64_to_cpus(&s->refcount_table[i]);
2162     }
2163     return 0;
2164  fail:
2165     return -ENOMEM;
2166 }
2167
2168 static void refcount_close(BlockDriverState *bs)
2169 {
2170     BDRVQcowState *s = bs->opaque;
2171     qemu_free(s->refcount_block_cache);
2172     qemu_free(s->refcount_table);
2173 }
2174
2175
2176 static int load_refcount_block(BlockDriverState *bs,
2177                                int64_t refcount_block_offset)
2178 {
2179     BDRVQcowState *s = bs->opaque;
2180     int ret;
2181     ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
2182                      s->cluster_size);
2183     if (ret != s->cluster_size)
2184         return -EIO;
2185     s->refcount_block_cache_offset = refcount_block_offset;
2186     return 0;
2187 }
2188
2189 static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2190 {
2191     BDRVQcowState *s = bs->opaque;
2192     int refcount_table_index, block_index;
2193     int64_t refcount_block_offset;
2194
2195     refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2196     if (refcount_table_index >= s->refcount_table_size)
2197         return 0;
2198     refcount_block_offset = s->refcount_table[refcount_table_index];
2199     if (!refcount_block_offset)
2200         return 0;
2201     if (refcount_block_offset != s->refcount_block_cache_offset) {
2202         /* better than nothing: return allocated if read error */
2203         if (load_refcount_block(bs, refcount_block_offset) < 0)
2204             return 1;
2205     }
2206     block_index = cluster_index &
2207         ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2208     return be16_to_cpu(s->refcount_block_cache[block_index]);
2209 }
2210
2211 /* return < 0 if error */
2212 static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2213 {
2214     BDRVQcowState *s = bs->opaque;
2215     int i, nb_clusters;
2216
2217     nb_clusters = size_to_clusters(s, size);
2218 retry:
2219     for(i = 0; i < nb_clusters; i++) {
2220         int64_t i = s->free_cluster_index++;
2221         if (get_refcount(bs, i) != 0)
2222             goto retry;
2223     }
2224 #ifdef DEBUG_ALLOC2
2225     printf("alloc_clusters: size=%lld -> %lld\n",
2226             size,
2227             (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2228 #endif
2229     return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2230 }
2231
2232 static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2233 {
2234     int64_t offset;
2235
2236     offset = alloc_clusters_noref(bs, size);
2237     update_refcount(bs, offset, size, 1);
2238     return offset;
2239 }
2240
2241 /* only used to allocate compressed sectors. We try to allocate
2242    contiguous sectors. size must be <= cluster_size */
2243 static int64_t alloc_bytes(BlockDriverState *bs, int size)
2244 {
2245     BDRVQcowState *s = bs->opaque;
2246     int64_t offset, cluster_offset;
2247     int free_in_cluster;
2248
2249     assert(size > 0 && size <= s->cluster_size);
2250     if (s->free_byte_offset == 0) {
2251         s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2252     }
2253  redo:
2254     free_in_cluster = s->cluster_size -
2255         (s->free_byte_offset & (s->cluster_size - 1));
2256     if (size <= free_in_cluster) {
2257         /* enough space in current cluster */
2258         offset = s->free_byte_offset;
2259         s->free_byte_offset += size;
2260         free_in_cluster -= size;
2261         if (free_in_cluster == 0)
2262             s->free_byte_offset = 0;
2263         if ((offset & (s->cluster_size - 1)) != 0)
2264             update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2265     } else {
2266         offset = alloc_clusters(bs, s->cluster_size);
2267         cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2268         if ((cluster_offset + s->cluster_size) == offset) {
2269             /* we are lucky: contiguous data */
2270             offset = s->free_byte_offset;
2271             update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2272             s->free_byte_offset += size;
2273         } else {
2274             s->free_byte_offset = offset;
2275             goto redo;
2276         }
2277     }
2278     return offset;
2279 }
2280
2281 static void free_clusters(BlockDriverState *bs,
2282                           int64_t offset, int64_t size)
2283 {
2284     update_refcount(bs, offset, size, -1);
2285 }
2286
2287 static int grow_refcount_table(BlockDriverState *bs, int min_size)
2288 {
2289     BDRVQcowState *s = bs->opaque;
2290     int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2291     uint64_t *new_table;
2292     int64_t table_offset;
2293     uint8_t data[12];
2294     int old_table_size;
2295     int64_t old_table_offset;
2296
2297     if (min_size <= s->refcount_table_size)
2298         return 0;
2299     /* compute new table size */
2300     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2301     for(;;) {
2302         if (refcount_table_clusters == 0) {
2303             refcount_table_clusters = 1;
2304         } else {
2305             refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2306         }
2307         new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2308         if (min_size <= new_table_size)
2309             break;
2310     }
2311 #ifdef DEBUG_ALLOC2
2312     printf("grow_refcount_table from %d to %d\n",
2313            s->refcount_table_size,
2314            new_table_size);
2315 #endif
2316     new_table_size2 = new_table_size * sizeof(uint64_t);
2317     new_table = qemu_mallocz(new_table_size2);
2318     if (!new_table)
2319         return -ENOMEM;
2320     memcpy(new_table, s->refcount_table,
2321            s->refcount_table_size * sizeof(uint64_t));
2322     for(i = 0; i < s->refcount_table_size; i++)
2323         cpu_to_be64s(&new_table[i]);
2324     /* Note: we cannot update the refcount now to avoid recursion */
2325     table_offset = alloc_clusters_noref(bs, new_table_size2);
2326     ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
2327     if (ret != new_table_size2)
2328         goto fail;
2329     for(i = 0; i < s->refcount_table_size; i++)
2330         be64_to_cpus(&new_table[i]);
2331
2332     cpu_to_be64w((uint64_t*)data, table_offset);
2333     cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
2334     if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2335                     data, sizeof(data)) != sizeof(data))
2336         goto fail;
2337     qemu_free(s->refcount_table);
2338     old_table_offset = s->refcount_table_offset;
2339     old_table_size = s->refcount_table_size;
2340     s->refcount_table = new_table;
2341     s->refcount_table_size = new_table_size;
2342     s->refcount_table_offset = table_offset;
2343
2344     update_refcount(bs, table_offset, new_table_size2, 1);
2345     free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
2346     return 0;
2347  fail:
2348     free_clusters(bs, table_offset, new_table_size2);
2349     qemu_free(new_table);
2350     return -EIO;
2351 }
2352
2353 /* addend must be 1 or -1 */
2354 /* XXX: cache several refcount block clusters ? */
2355 static int update_cluster_refcount(BlockDriverState *bs,
2356                                    int64_t cluster_index,
2357                                    int addend)
2358 {
2359     BDRVQcowState *s = bs->opaque;
2360     int64_t offset, refcount_block_offset;
2361     int ret, refcount_table_index, block_index, refcount;
2362     uint64_t data64;
2363
2364     refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2365     if (refcount_table_index >= s->refcount_table_size) {
2366         if (addend < 0)
2367             return -EINVAL;
2368         ret = grow_refcount_table(bs, refcount_table_index + 1);
2369         if (ret < 0)
2370             return ret;
2371     }
2372     refcount_block_offset = s->refcount_table[refcount_table_index];
2373     if (!refcount_block_offset) {
2374         if (addend < 0)
2375             return -EINVAL;
2376         /* create a new refcount block */
2377         /* Note: we cannot update the refcount now to avoid recursion */
2378         offset = alloc_clusters_noref(bs, s->cluster_size);
2379         memset(s->refcount_block_cache, 0, s->cluster_size);
2380         ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2381         if (ret != s->cluster_size)
2382             return -EINVAL;
2383         s->refcount_table[refcount_table_index] = offset;
2384         data64 = cpu_to_be64(offset);
2385         ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2386                           refcount_table_index * sizeof(uint64_t),
2387                           &data64, sizeof(data64));
2388         if (ret != sizeof(data64))
2389             return -EINVAL;
2390
2391         refcount_block_offset = offset;
2392         s->refcount_block_cache_offset = offset;
2393         update_refcount(bs, offset, s->cluster_size, 1);
2394     } else {
2395         if (refcount_block_offset != s->refcount_block_cache_offset) {
2396             if (load_refcount_block(bs, refcount_block_offset) < 0)
2397                 return -EIO;
2398         }
2399     }
2400     /* we can update the count and save it */
2401     block_index = cluster_index &
2402         ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2403     refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2404     refcount += addend;
2405     if (refcount < 0 || refcount > 0xffff)
2406         return -EINVAL;
2407     if (refcount == 0 && cluster_index < s->free_cluster_index) {
2408         s->free_cluster_index = cluster_index;
2409     }
2410     s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2411     if (bdrv_pwrite(s->hd,
2412                     refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2413                     &s->refcount_block_cache[block_index], 2) != 2)
2414         return -EIO;
2415     return refcount;
2416 }
2417
2418 static void update_refcount(BlockDriverState *bs,
2419                             int64_t offset, int64_t length,
2420                             int addend)
2421 {
2422     BDRVQcowState *s = bs->opaque;
2423     int64_t start, last, cluster_offset;
2424
2425 #ifdef DEBUG_ALLOC2
2426     printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2427            offset, length, addend);
2428 #endif
2429     if (length <= 0)
2430         return;
2431     start = offset & ~(s->cluster_size - 1);
2432     last = (offset + length - 1) & ~(s->cluster_size - 1);
2433     for(cluster_offset = start; cluster_offset <= last;
2434         cluster_offset += s->cluster_size) {
2435         update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2436     }
2437 }
2438
2439 #ifdef DEBUG_ALLOC
2440 static void inc_refcounts(BlockDriverState *bs,
2441                           uint16_t *refcount_table,
2442                           int refcount_table_size,
2443                           int64_t offset, int64_t size)
2444 {
2445     BDRVQcowState *s = bs->opaque;
2446     int64_t start, last, cluster_offset;
2447     int k;
2448
2449     if (size <= 0)
2450         return;
2451
2452     start = offset & ~(s->cluster_size - 1);
2453     last = (offset + size - 1) & ~(s->cluster_size - 1);
2454     for(cluster_offset = start; cluster_offset <= last;
2455         cluster_offset += s->cluster_size) {
2456         k = cluster_offset >> s->cluster_bits;
2457         if (k < 0 || k >= refcount_table_size) {
2458             printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2459         } else {
2460             if (++refcount_table[k] == 0) {
2461                 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2462             }
2463         }
2464     }
2465 }
2466
2467 static int check_refcounts_l1(BlockDriverState *bs,
2468                               uint16_t *refcount_table,
2469                               int refcount_table_size,
2470                               int64_t l1_table_offset, int l1_size,
2471                               int check_copied)
2472 {
2473     BDRVQcowState *s = bs->opaque;
2474     uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2475     int l2_size, i, j, nb_csectors, refcount;
2476
2477     l2_table = NULL;
2478     l1_size2 = l1_size * sizeof(uint64_t);
2479
2480     inc_refcounts(bs, refcount_table, refcount_table_size,
2481                   l1_table_offset, l1_size2);
2482
2483     l1_table = qemu_malloc(l1_size2);
2484     if (!l1_table)
2485         goto fail;
2486     if (bdrv_pread(s->hd, l1_table_offset,
2487                    l1_table, l1_size2) != l1_size2)
2488         goto fail;
2489     for(i = 0;i < l1_size; i++)
2490         be64_to_cpus(&l1_table[i]);
2491
2492     l2_size = s->l2_size * sizeof(uint64_t);
2493     l2_table = qemu_malloc(l2_size);
2494     if (!l2_table)
2495         goto fail;
2496     for(i = 0; i < l1_size; i++) {
2497         l2_offset = l1_table[i];
2498         if (l2_offset) {
2499             if (check_copied) {
2500                 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2501                 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2502                     printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2503                            l2_offset, refcount);
2504                 }
2505             }
2506             l2_offset &= ~QCOW_OFLAG_COPIED;
2507             if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2508                 goto fail;
2509             for(j = 0; j < s->l2_size; j++) {
2510                 offset = be64_to_cpu(l2_table[j]);
2511                 if (offset != 0) {
2512                     if (offset & QCOW_OFLAG_COMPRESSED) {
2513                         if (offset & QCOW_OFLAG_COPIED) {
2514                             printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2515                                    offset >> s->cluster_bits);
2516                             offset &= ~QCOW_OFLAG_COPIED;
2517                         }
2518                         nb_csectors = ((offset >> s->csize_shift) &
2519                                        s->csize_mask) + 1;
2520                         offset &= s->cluster_offset_mask;
2521                         inc_refcounts(bs, refcount_table,
2522                                       refcount_table_size,
2523                                       offset & ~511, nb_csectors * 512);
2524                     } else {
2525                         if (check_copied) {
2526                             refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2527                             if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2528                                 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2529                                        offset, refcount);
2530                             }
2531                         }
2532                         offset &= ~QCOW_OFLAG_COPIED;
2533                         inc_refcounts(bs, refcount_table,
2534                                       refcount_table_size,
2535                                       offset, s->cluster_size);
2536                     }
2537                 }
2538             }
2539             inc_refcounts(bs, refcount_table,
2540                           refcount_table_size,
2541                           l2_offset,
2542                           s->cluster_size);
2543         }
2544     }
2545     qemu_free(l1_table);
2546     qemu_free(l2_table);
2547     return 0;
2548  fail:
2549     printf("ERROR: I/O error in check_refcounts_l1\n");
2550     qemu_free(l1_table);
2551     qemu_free(l2_table);
2552     return -EIO;
2553 }
2554
2555 static void check_refcounts(BlockDriverState *bs)
2556 {
2557     BDRVQcowState *s = bs->opaque;
2558     int64_t size;
2559     int nb_clusters, refcount1, refcount2, i;
2560     QCowSnapshot *sn;
2561     uint16_t *refcount_table;
2562
2563     size = bdrv_getlength(s->hd);
2564     nb_clusters = size_to_clusters(s, size);
2565     refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2566
2567     /* header */
2568     inc_refcounts(bs, refcount_table, nb_clusters,
2569                   0, s->cluster_size);
2570
2571     check_refcounts_l1(bs, refcount_table, nb_clusters,
2572                        s->l1_table_offset, s->l1_size, 1);
2573
2574     /* snapshots */
2575     for(i = 0; i < s->nb_snapshots; i++) {
2576         sn = s->snapshots + i;
2577         check_refcounts_l1(bs, refcount_table, nb_clusters,
2578                            sn->l1_table_offset, sn->l1_size, 0);
2579     }
2580     inc_refcounts(bs, refcount_table, nb_clusters,
2581                   s->snapshots_offset, s->snapshots_size);
2582
2583     /* refcount data */
2584     inc_refcounts(bs, refcount_table, nb_clusters,
2585                   s->refcount_table_offset,
2586                   s->refcount_table_size * sizeof(uint64_t));
2587     for(i = 0; i < s->refcount_table_size; i++) {
2588         int64_t offset;
2589         offset = s->refcount_table[i];
2590         if (offset != 0) {
2591             inc_refcounts(bs, refcount_table, nb_clusters,
2592                           offset, s->cluster_size);
2593         }
2594     }
2595
2596     /* compare ref counts */
2597     for(i = 0; i < nb_clusters; i++) {
2598         refcount1 = get_refcount(bs, i);
2599         refcount2 = refcount_table[i];
2600         if (refcount1 != refcount2)
2601             printf("ERROR cluster %d refcount=%d reference=%d\n",
2602                    i, refcount1, refcount2);
2603     }
2604
2605     qemu_free(refcount_table);
2606 }
2607
2608 #if 0
2609 static void dump_refcounts(BlockDriverState *bs)
2610 {
2611     BDRVQcowState *s = bs->opaque;
2612     int64_t nb_clusters, k, k1, size;
2613     int refcount;
2614
2615     size = bdrv_getlength(s->hd);
2616     nb_clusters = size_to_clusters(s, size);
2617     for(k = 0; k < nb_clusters;) {
2618         k1 = k;
2619         refcount = get_refcount(bs, k);
2620         k++;
2621         while (k < nb_clusters && get_refcount(bs, k) == refcount)
2622             k++;
2623         printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2624     }
2625 }
2626 #endif
2627 #endif
2628
2629 BlockDriver bdrv_qcow2 = {
2630     "qcow2",
2631     sizeof(BDRVQcowState),
2632     qcow_probe,
2633     qcow_open,
2634     NULL,
2635     NULL,
2636     qcow_close,
2637     qcow_create,
2638     qcow_flush,
2639     qcow_is_allocated,
2640     qcow_set_key,
2641     qcow_make_empty,
2642
2643     .bdrv_aio_read = qcow_aio_read,
2644     .bdrv_aio_write = qcow_aio_write,
2645     .bdrv_aio_cancel = qcow_aio_cancel,
2646     .aiocb_size = sizeof(QCowAIOCB),
2647     .bdrv_write_compressed = qcow_write_compressed,
2648
2649     .bdrv_snapshot_create = qcow_snapshot_create,
2650     .bdrv_snapshot_goto = qcow_snapshot_goto,
2651     .bdrv_snapshot_delete = qcow_snapshot_delete,
2652     .bdrv_snapshot_list = qcow_snapshot_list,
2653     .bdrv_get_info = qcow_get_info,
2654 };