Statistics
| Branch: | Revision:

root / block-qcow2.c @ 178e08a5

History | View | Annotate | Download (84.7 kB)

1 585f8587 bellard
/*
2 585f8587 bellard
 * Block driver for the QCOW version 2 format
3 5fafdf24 ths
 *
4 585f8587 bellard
 * Copyright (c) 2004-2006 Fabrice Bellard
5 5fafdf24 ths
 *
6 585f8587 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 585f8587 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 585f8587 bellard
 * in the Software without restriction, including without limitation the rights
9 585f8587 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 585f8587 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 585f8587 bellard
 * furnished to do so, subject to the following conditions:
12 585f8587 bellard
 *
13 585f8587 bellard
 * The above copyright notice and this permission notice shall be included in
14 585f8587 bellard
 * all copies or substantial portions of the Software.
15 585f8587 bellard
 *
16 585f8587 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 585f8587 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 585f8587 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 585f8587 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 585f8587 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 585f8587 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 585f8587 bellard
 * THE SOFTWARE.
23 585f8587 bellard
 */
24 faf07963 pbrook
#include "qemu-common.h"
25 585f8587 bellard
#include "block_int.h"
26 585f8587 bellard
#include <zlib.h>
27 585f8587 bellard
#include "aes.h"
28 585f8587 bellard
#include <assert.h>
29 585f8587 bellard
30 585f8587 bellard
/*
31 585f8587 bellard
  Differences with QCOW:
32 585f8587 bellard

33 585f8587 bellard
  - Support for multiple incremental snapshots.
34 585f8587 bellard
  - Memory management by reference counts.
35 585f8587 bellard
  - Clusters which have a reference count of one have the bit
36 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
37 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
38 585f8587 bellard
    in the cluster offsets.
39 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
40 3b46e624 ths
    snapshots.
41 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
42 585f8587 bellard
    (could be backported to QCOW).
43 585f8587 bellard
  - L2 tables have always a size of one cluster.
44 585f8587 bellard
*/
45 585f8587 bellard
46 585f8587 bellard
//#define DEBUG_ALLOC
47 585f8587 bellard
//#define DEBUG_ALLOC2
48 9b80ddf3 aliguori
//#define DEBUG_EXT
49 5fafdf24 ths
50 585f8587 bellard
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
51 585f8587 bellard
#define QCOW_VERSION 2
52 585f8587 bellard
53 585f8587 bellard
#define QCOW_CRYPT_NONE 0
54 585f8587 bellard
#define QCOW_CRYPT_AES  1
55 585f8587 bellard
56 095a9c58 aliguori
#define QCOW_MAX_CRYPT_CLUSTERS 32
57 095a9c58 aliguori
58 585f8587 bellard
/* indicate that the refcount of the referenced cluster is exactly one. */
59 585f8587 bellard
#define QCOW_OFLAG_COPIED     (1LL << 63)
60 585f8587 bellard
/* indicate that the cluster is compressed (they never have the copied flag) */
61 585f8587 bellard
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
62 585f8587 bellard
63 585f8587 bellard
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
64 585f8587 bellard
65 585f8587 bellard
typedef struct QCowHeader {
66 585f8587 bellard
    uint32_t magic;
67 585f8587 bellard
    uint32_t version;
68 585f8587 bellard
    uint64_t backing_file_offset;
69 585f8587 bellard
    uint32_t backing_file_size;
70 585f8587 bellard
    uint32_t cluster_bits;
71 585f8587 bellard
    uint64_t size; /* in bytes */
72 585f8587 bellard
    uint32_t crypt_method;
73 585f8587 bellard
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
74 585f8587 bellard
    uint64_t l1_table_offset;
75 585f8587 bellard
    uint64_t refcount_table_offset;
76 585f8587 bellard
    uint32_t refcount_table_clusters;
77 585f8587 bellard
    uint32_t nb_snapshots;
78 585f8587 bellard
    uint64_t snapshots_offset;
79 585f8587 bellard
} QCowHeader;
80 585f8587 bellard
81 9b80ddf3 aliguori
82 9b80ddf3 aliguori
typedef struct {
83 9b80ddf3 aliguori
    uint32_t magic;
84 9b80ddf3 aliguori
    uint32_t len;
85 9b80ddf3 aliguori
} QCowExtension;
86 9b80ddf3 aliguori
#define  QCOW_EXT_MAGIC_END 0
87 f965509c aliguori
#define  QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
88 9b80ddf3 aliguori
89 9b80ddf3 aliguori
90 585f8587 bellard
typedef struct __attribute__((packed)) QCowSnapshotHeader {
91 585f8587 bellard
    /* header is 8 byte aligned */
92 585f8587 bellard
    uint64_t l1_table_offset;
93 585f8587 bellard
94 585f8587 bellard
    uint32_t l1_size;
95 585f8587 bellard
    uint16_t id_str_size;
96 585f8587 bellard
    uint16_t name_size;
97 585f8587 bellard
98 585f8587 bellard
    uint32_t date_sec;
99 585f8587 bellard
    uint32_t date_nsec;
100 585f8587 bellard
101 585f8587 bellard
    uint64_t vm_clock_nsec;
102 585f8587 bellard
103 585f8587 bellard
    uint32_t vm_state_size;
104 585f8587 bellard
    uint32_t extra_data_size; /* for extension */
105 585f8587 bellard
    /* extra data follows */
106 585f8587 bellard
    /* id_str follows */
107 585f8587 bellard
    /* name follows  */
108 585f8587 bellard
} QCowSnapshotHeader;
109 585f8587 bellard
110 585f8587 bellard
#define L2_CACHE_SIZE 16
111 585f8587 bellard
112 585f8587 bellard
typedef struct QCowSnapshot {
113 585f8587 bellard
    uint64_t l1_table_offset;
114 585f8587 bellard
    uint32_t l1_size;
115 585f8587 bellard
    char *id_str;
116 585f8587 bellard
    char *name;
117 585f8587 bellard
    uint32_t vm_state_size;
118 585f8587 bellard
    uint32_t date_sec;
119 585f8587 bellard
    uint32_t date_nsec;
120 585f8587 bellard
    uint64_t vm_clock_nsec;
121 585f8587 bellard
} QCowSnapshot;
122 585f8587 bellard
123 585f8587 bellard
typedef struct BDRVQcowState {
124 585f8587 bellard
    BlockDriverState *hd;
125 585f8587 bellard
    int cluster_bits;
126 585f8587 bellard
    int cluster_size;
127 585f8587 bellard
    int cluster_sectors;
128 585f8587 bellard
    int l2_bits;
129 585f8587 bellard
    int l2_size;
130 585f8587 bellard
    int l1_size;
131 585f8587 bellard
    int l1_vm_state_index;
132 585f8587 bellard
    int csize_shift;
133 585f8587 bellard
    int csize_mask;
134 585f8587 bellard
    uint64_t cluster_offset_mask;
135 585f8587 bellard
    uint64_t l1_table_offset;
136 585f8587 bellard
    uint64_t *l1_table;
137 585f8587 bellard
    uint64_t *l2_cache;
138 585f8587 bellard
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
139 585f8587 bellard
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
140 585f8587 bellard
    uint8_t *cluster_cache;
141 585f8587 bellard
    uint8_t *cluster_data;
142 585f8587 bellard
    uint64_t cluster_cache_offset;
143 585f8587 bellard
144 585f8587 bellard
    uint64_t *refcount_table;
145 585f8587 bellard
    uint64_t refcount_table_offset;
146 585f8587 bellard
    uint32_t refcount_table_size;
147 585f8587 bellard
    uint64_t refcount_block_cache_offset;
148 585f8587 bellard
    uint16_t *refcount_block_cache;
149 585f8587 bellard
    int64_t free_cluster_index;
150 585f8587 bellard
    int64_t free_byte_offset;
151 585f8587 bellard
152 585f8587 bellard
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
153 585f8587 bellard
    uint32_t crypt_method_header;
154 585f8587 bellard
    AES_KEY aes_encrypt_key;
155 585f8587 bellard
    AES_KEY aes_decrypt_key;
156 585f8587 bellard
    uint64_t snapshots_offset;
157 585f8587 bellard
    int snapshots_size;
158 585f8587 bellard
    int nb_snapshots;
159 585f8587 bellard
    QCowSnapshot *snapshots;
160 585f8587 bellard
} BDRVQcowState;
161 585f8587 bellard
162 585f8587 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
163 5fafdf24 ths
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
164 585f8587 bellard
                     uint8_t *buf, int nb_sectors);
165 585f8587 bellard
static int qcow_read_snapshots(BlockDriverState *bs);
166 585f8587 bellard
static void qcow_free_snapshots(BlockDriverState *bs);
167 585f8587 bellard
static int refcount_init(BlockDriverState *bs);
168 585f8587 bellard
static void refcount_close(BlockDriverState *bs);
169 585f8587 bellard
static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
170 5fafdf24 ths
static int update_cluster_refcount(BlockDriverState *bs,
171 585f8587 bellard
                                   int64_t cluster_index,
172 585f8587 bellard
                                   int addend);
173 5fafdf24 ths
static void update_refcount(BlockDriverState *bs,
174 5fafdf24 ths
                            int64_t offset, int64_t length,
175 585f8587 bellard
                            int addend);
176 585f8587 bellard
static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
177 585f8587 bellard
static int64_t alloc_bytes(BlockDriverState *bs, int size);
178 5fafdf24 ths
static void free_clusters(BlockDriverState *bs,
179 585f8587 bellard
                          int64_t offset, int64_t size);
180 585f8587 bellard
#ifdef DEBUG_ALLOC
181 585f8587 bellard
static void check_refcounts(BlockDriverState *bs);
182 585f8587 bellard
#endif
183 585f8587 bellard
184 585f8587 bellard
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
185 585f8587 bellard
{
186 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
187 3b46e624 ths
188 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
189 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
190 5fafdf24 ths
        be32_to_cpu(cow_header->version) == QCOW_VERSION)
191 585f8587 bellard
        return 100;
192 585f8587 bellard
    else
193 585f8587 bellard
        return 0;
194 585f8587 bellard
}
195 585f8587 bellard
196 9b80ddf3 aliguori
197 9b80ddf3 aliguori
/* 
198 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
199 9b80ddf3 aliguori
 * start reading from start_offset
200 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
201 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
202 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
203 9b80ddf3 aliguori
 */
204 9b80ddf3 aliguori
static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset,
205 9b80ddf3 aliguori
                                uint64_t end_offset)
206 9b80ddf3 aliguori
{
207 9b80ddf3 aliguori
    BDRVQcowState *s = bs->opaque;
208 9b80ddf3 aliguori
    QCowExtension ext;
209 9b80ddf3 aliguori
    uint64_t offset;
210 9b80ddf3 aliguori
211 9b80ddf3 aliguori
#ifdef DEBUG_EXT
212 9b80ddf3 aliguori
    printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
213 9b80ddf3 aliguori
#endif
214 9b80ddf3 aliguori
    offset = start_offset;
215 9b80ddf3 aliguori
    while (offset < end_offset) {
216 9b80ddf3 aliguori
217 9b80ddf3 aliguori
#ifdef DEBUG_EXT
218 9b80ddf3 aliguori
        /* Sanity check */
219 9b80ddf3 aliguori
        if (offset > s->cluster_size)
220 9b80ddf3 aliguori
            printf("qcow_handle_extension: suspicious offset %lu\n", offset);
221 9b80ddf3 aliguori
222 9b80ddf3 aliguori
        printf("attemting to read extended header in offset %lu\n", offset);
223 9b80ddf3 aliguori
#endif
224 9b80ddf3 aliguori
225 9b80ddf3 aliguori
        if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) {
226 4c978075 aliguori
            fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
227 4c978075 aliguori
                    (unsigned long long)offset);
228 9b80ddf3 aliguori
            return 1;
229 9b80ddf3 aliguori
        }
230 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
231 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
232 9b80ddf3 aliguori
        offset += sizeof(ext);
233 9b80ddf3 aliguori
#ifdef DEBUG_EXT
234 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
235 9b80ddf3 aliguori
#endif
236 9b80ddf3 aliguori
        switch (ext.magic) {
237 9b80ddf3 aliguori
        case QCOW_EXT_MAGIC_END:
238 9b80ddf3 aliguori
            return 0;
239 f965509c aliguori
240 f965509c aliguori
        case QCOW_EXT_MAGIC_BACKING_FORMAT:
241 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
242 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
243 4c978075 aliguori
                        " (>=%zu)\n",
244 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
245 f965509c aliguori
                return 2;
246 f965509c aliguori
            }
247 f965509c aliguori
            if (bdrv_pread(s->hd, offset , bs->backing_format,
248 f965509c aliguori
                           ext.len) != ext.len)
249 f965509c aliguori
                return 3;
250 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
251 f965509c aliguori
#ifdef DEBUG_EXT
252 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
253 f965509c aliguori
#endif
254 f965509c aliguori
            offset += ((ext.len + 7) & ~7);
255 f965509c aliguori
            break;
256 f965509c aliguori
257 9b80ddf3 aliguori
        default:
258 9b80ddf3 aliguori
            /* unknown magic -- just skip it */
259 9b80ddf3 aliguori
            offset += ((ext.len + 7) & ~7);
260 9b80ddf3 aliguori
            break;
261 9b80ddf3 aliguori
        }
262 9b80ddf3 aliguori
    }
263 9b80ddf3 aliguori
264 9b80ddf3 aliguori
    return 0;
265 9b80ddf3 aliguori
}
266 9b80ddf3 aliguori
267 9b80ddf3 aliguori
268 585f8587 bellard
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
269 585f8587 bellard
{
270 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
271 585f8587 bellard
    int len, i, shift, ret;
272 585f8587 bellard
    QCowHeader header;
273 9b80ddf3 aliguori
    uint64_t ext_end;
274 585f8587 bellard
275 4dc822d7 aliguori
    /* Performance is terrible right now with cache=writethrough due mainly
276 4dc822d7 aliguori
     * to reference count updates.  If the user does not explicitly specify
277 4dc822d7 aliguori
     * a caching type, force to writeback caching.
278 4dc822d7 aliguori
     */
279 4dc822d7 aliguori
    if ((flags & BDRV_O_CACHE_DEF)) {
280 4dc822d7 aliguori
        flags |= BDRV_O_CACHE_WB;
281 4dc822d7 aliguori
        flags &= ~BDRV_O_CACHE_DEF;
282 4dc822d7 aliguori
    }
283 b5eff355 aurel32
    ret = bdrv_file_open(&s->hd, filename, flags);
284 585f8587 bellard
    if (ret < 0)
285 585f8587 bellard
        return ret;
286 585f8587 bellard
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
287 585f8587 bellard
        goto fail;
288 585f8587 bellard
    be32_to_cpus(&header.magic);
289 585f8587 bellard
    be32_to_cpus(&header.version);
290 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
291 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
292 585f8587 bellard
    be64_to_cpus(&header.size);
293 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
294 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
295 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
296 585f8587 bellard
    be32_to_cpus(&header.l1_size);
297 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
298 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
299 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
300 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
301 3b46e624 ths
302 585f8587 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
303 585f8587 bellard
        goto fail;
304 5fafdf24 ths
    if (header.size <= 1 ||
305 5fafdf24 ths
        header.cluster_bits < 9 ||
306 585f8587 bellard
        header.cluster_bits > 16)
307 585f8587 bellard
        goto fail;
308 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
309 585f8587 bellard
        goto fail;
310 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
311 585f8587 bellard
    if (s->crypt_method_header)
312 585f8587 bellard
        bs->encrypted = 1;
313 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
314 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
315 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
316 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
317 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
318 585f8587 bellard
    bs->total_sectors = header.size / 512;
319 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
320 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
321 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
322 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
323 5fafdf24 ths
    s->refcount_table_size =
324 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
325 585f8587 bellard
326 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
327 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
328 585f8587 bellard
329 585f8587 bellard
    /* read the level 1 table */
330 585f8587 bellard
    s->l1_size = header.l1_size;
331 585f8587 bellard
    shift = s->cluster_bits + s->l2_bits;
332 585f8587 bellard
    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
333 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
334 585f8587 bellard
       header.size bytes */
335 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
336 585f8587 bellard
        goto fail;
337 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
338 585f8587 bellard
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
339 5fafdf24 ths
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
340 585f8587 bellard
        s->l1_size * sizeof(uint64_t))
341 585f8587 bellard
        goto fail;
342 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
343 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
344 585f8587 bellard
    }
345 585f8587 bellard
    /* alloc L2 cache */
346 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
347 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
348 585f8587 bellard
    /* one more sector for decompressed data alignment */
349 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
350 095a9c58 aliguori
                                  + 512);
351 585f8587 bellard
    s->cluster_cache_offset = -1;
352 3b46e624 ths
353 585f8587 bellard
    if (refcount_init(bs) < 0)
354 585f8587 bellard
        goto fail;
355 585f8587 bellard
356 9b80ddf3 aliguori
    /* read qcow2 extensions */
357 9b80ddf3 aliguori
    if (header.backing_file_offset)
358 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
359 9b80ddf3 aliguori
    else
360 9b80ddf3 aliguori
        ext_end = s->cluster_size;
361 9b80ddf3 aliguori
    if (qcow_read_extensions(bs, sizeof(header), ext_end))
362 9b80ddf3 aliguori
        goto fail;
363 9b80ddf3 aliguori
364 585f8587 bellard
    /* read the backing file name */
365 585f8587 bellard
    if (header.backing_file_offset != 0) {
366 585f8587 bellard
        len = header.backing_file_size;
367 585f8587 bellard
        if (len > 1023)
368 585f8587 bellard
            len = 1023;
369 585f8587 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
370 585f8587 bellard
            goto fail;
371 585f8587 bellard
        bs->backing_file[len] = '\0';
372 585f8587 bellard
    }
373 585f8587 bellard
    if (qcow_read_snapshots(bs) < 0)
374 585f8587 bellard
        goto fail;
375 585f8587 bellard
376 585f8587 bellard
#ifdef DEBUG_ALLOC
377 585f8587 bellard
    check_refcounts(bs);
378 585f8587 bellard
#endif
379 585f8587 bellard
    return 0;
380 585f8587 bellard
381 585f8587 bellard
 fail:
382 585f8587 bellard
    qcow_free_snapshots(bs);
383 585f8587 bellard
    refcount_close(bs);
384 585f8587 bellard
    qemu_free(s->l1_table);
385 585f8587 bellard
    qemu_free(s->l2_cache);
386 585f8587 bellard
    qemu_free(s->cluster_cache);
387 585f8587 bellard
    qemu_free(s->cluster_data);
388 585f8587 bellard
    bdrv_delete(s->hd);
389 585f8587 bellard
    return -1;
390 585f8587 bellard
}
391 585f8587 bellard
392 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
393 585f8587 bellard
{
394 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
395 585f8587 bellard
    uint8_t keybuf[16];
396 585f8587 bellard
    int len, i;
397 3b46e624 ths
398 585f8587 bellard
    memset(keybuf, 0, 16);
399 585f8587 bellard
    len = strlen(key);
400 585f8587 bellard
    if (len > 16)
401 585f8587 bellard
        len = 16;
402 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
403 585f8587 bellard
       entropy */
404 585f8587 bellard
    for(i = 0;i < len;i++) {
405 585f8587 bellard
        keybuf[i] = key[i];
406 585f8587 bellard
    }
407 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
408 585f8587 bellard
409 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
410 585f8587 bellard
        return -1;
411 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
412 585f8587 bellard
        return -1;
413 585f8587 bellard
#if 0
414 585f8587 bellard
    /* test */
415 585f8587 bellard
    {
416 585f8587 bellard
        uint8_t in[16];
417 585f8587 bellard
        uint8_t out[16];
418 585f8587 bellard
        uint8_t tmp[16];
419 585f8587 bellard
        for(i=0;i<16;i++)
420 585f8587 bellard
            in[i] = i;
421 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
422 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
423 585f8587 bellard
        for(i = 0; i < 16; i++)
424 585f8587 bellard
            printf(" %02x", tmp[i]);
425 585f8587 bellard
        printf("\n");
426 585f8587 bellard
        for(i = 0; i < 16; i++)
427 585f8587 bellard
            printf(" %02x", out[i]);
428 585f8587 bellard
        printf("\n");
429 585f8587 bellard
    }
430 585f8587 bellard
#endif
431 585f8587 bellard
    return 0;
432 585f8587 bellard
}
433 585f8587 bellard
434 585f8587 bellard
/* The crypt function is compatible with the linux cryptoloop
435 585f8587 bellard
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
436 585f8587 bellard
   supported */
437 585f8587 bellard
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
438 585f8587 bellard
                            uint8_t *out_buf, const uint8_t *in_buf,
439 585f8587 bellard
                            int nb_sectors, int enc,
440 585f8587 bellard
                            const AES_KEY *key)
441 585f8587 bellard
{
442 585f8587 bellard
    union {
443 585f8587 bellard
        uint64_t ll[2];
444 585f8587 bellard
        uint8_t b[16];
445 585f8587 bellard
    } ivec;
446 585f8587 bellard
    int i;
447 585f8587 bellard
448 585f8587 bellard
    for(i = 0; i < nb_sectors; i++) {
449 585f8587 bellard
        ivec.ll[0] = cpu_to_le64(sector_num);
450 585f8587 bellard
        ivec.ll[1] = 0;
451 5fafdf24 ths
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
452 585f8587 bellard
                        ivec.b, enc);
453 585f8587 bellard
        sector_num++;
454 585f8587 bellard
        in_buf += 512;
455 585f8587 bellard
        out_buf += 512;
456 585f8587 bellard
    }
457 585f8587 bellard
}
458 585f8587 bellard
459 585f8587 bellard
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
460 585f8587 bellard
                        uint64_t cluster_offset, int n_start, int n_end)
461 585f8587 bellard
{
462 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
463 585f8587 bellard
    int n, ret;
464 585f8587 bellard
465 585f8587 bellard
    n = n_end - n_start;
466 585f8587 bellard
    if (n <= 0)
467 585f8587 bellard
        return 0;
468 585f8587 bellard
    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
469 585f8587 bellard
    if (ret < 0)
470 585f8587 bellard
        return ret;
471 585f8587 bellard
    if (s->crypt_method) {
472 5fafdf24 ths
        encrypt_sectors(s, start_sect + n_start,
473 5fafdf24 ths
                        s->cluster_data,
474 585f8587 bellard
                        s->cluster_data, n, 1,
475 585f8587 bellard
                        &s->aes_encrypt_key);
476 585f8587 bellard
    }
477 5fafdf24 ths
    ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
478 585f8587 bellard
                     s->cluster_data, n);
479 585f8587 bellard
    if (ret < 0)
480 585f8587 bellard
        return ret;
481 585f8587 bellard
    return 0;
482 585f8587 bellard
}
483 585f8587 bellard
484 585f8587 bellard
static void l2_cache_reset(BlockDriverState *bs)
485 585f8587 bellard
{
486 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
487 585f8587 bellard
488 585f8587 bellard
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
489 585f8587 bellard
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
490 585f8587 bellard
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
491 585f8587 bellard
}
492 585f8587 bellard
493 585f8587 bellard
static inline int l2_cache_new_entry(BlockDriverState *bs)
494 585f8587 bellard
{
495 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
496 585f8587 bellard
    uint32_t min_count;
497 585f8587 bellard
    int min_index, i;
498 585f8587 bellard
499 585f8587 bellard
    /* find a new entry in the least used one */
500 585f8587 bellard
    min_index = 0;
501 585f8587 bellard
    min_count = 0xffffffff;
502 585f8587 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
503 585f8587 bellard
        if (s->l2_cache_counts[i] < min_count) {
504 585f8587 bellard
            min_count = s->l2_cache_counts[i];
505 585f8587 bellard
            min_index = i;
506 585f8587 bellard
        }
507 585f8587 bellard
    }
508 585f8587 bellard
    return min_index;
509 585f8587 bellard
}
510 585f8587 bellard
511 585f8587 bellard
static int64_t align_offset(int64_t offset, int n)
512 585f8587 bellard
{
513 585f8587 bellard
    offset = (offset + n - 1) & ~(n - 1);
514 585f8587 bellard
    return offset;
515 585f8587 bellard
}
516 585f8587 bellard
517 585f8587 bellard
static int grow_l1_table(BlockDriverState *bs, int min_size)
518 585f8587 bellard
{
519 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
520 585f8587 bellard
    int new_l1_size, new_l1_size2, ret, i;
521 585f8587 bellard
    uint64_t *new_l1_table;
522 585f8587 bellard
    uint64_t new_l1_table_offset;
523 643e5399 aliguori
    uint8_t data[12];
524 585f8587 bellard
525 585f8587 bellard
    new_l1_size = s->l1_size;
526 585f8587 bellard
    if (min_size <= new_l1_size)
527 585f8587 bellard
        return 0;
528 585f8587 bellard
    while (min_size > new_l1_size) {
529 585f8587 bellard
        new_l1_size = (new_l1_size * 3 + 1) / 2;
530 585f8587 bellard
    }
531 585f8587 bellard
#ifdef DEBUG_ALLOC2
532 585f8587 bellard
    printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
533 585f8587 bellard
#endif
534 585f8587 bellard
535 585f8587 bellard
    new_l1_size2 = sizeof(uint64_t) * new_l1_size;
536 585f8587 bellard
    new_l1_table = qemu_mallocz(new_l1_size2);
537 585f8587 bellard
    memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
538 585f8587 bellard
539 585f8587 bellard
    /* write new table (align to cluster) */
540 585f8587 bellard
    new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
541 3b46e624 ths
542 585f8587 bellard
    for(i = 0; i < s->l1_size; i++)
543 585f8587 bellard
        new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
544 585f8587 bellard
    ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
545 585f8587 bellard
    if (ret != new_l1_size2)
546 585f8587 bellard
        goto fail;
547 585f8587 bellard
    for(i = 0; i < s->l1_size; i++)
548 585f8587 bellard
        new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
549 3b46e624 ths
550 585f8587 bellard
    /* set new table */
551 643e5399 aliguori
    cpu_to_be32w((uint32_t*)data, new_l1_size);
552 643e5399 aliguori
    cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
553 643e5399 aliguori
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size), data,
554 643e5399 aliguori
                sizeof(data)) != sizeof(data))
555 585f8587 bellard
        goto fail;
556 585f8587 bellard
    qemu_free(s->l1_table);
557 585f8587 bellard
    free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
558 585f8587 bellard
    s->l1_table_offset = new_l1_table_offset;
559 585f8587 bellard
    s->l1_table = new_l1_table;
560 585f8587 bellard
    s->l1_size = new_l1_size;
561 585f8587 bellard
    return 0;
562 585f8587 bellard
 fail:
563 585f8587 bellard
    qemu_free(s->l1_table);
564 585f8587 bellard
    return -EIO;
565 585f8587 bellard
}
566 585f8587 bellard
567 108534b9 aliguori
/*
568 108534b9 aliguori
 * seek_l2_table
569 585f8587 bellard
 *
570 108534b9 aliguori
 * seek l2_offset in the l2_cache table
571 108534b9 aliguori
 * if not found, return NULL,
572 108534b9 aliguori
 * if found,
573 108534b9 aliguori
 *   increments the l2 cache hit count of the entry,
574 108534b9 aliguori
 *   if counter overflow, divide by two all counters
575 108534b9 aliguori
 *   return the pointer to the l2 cache entry
576 585f8587 bellard
 *
577 108534b9 aliguori
 */
578 108534b9 aliguori
579 108534b9 aliguori
static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
580 108534b9 aliguori
{
581 108534b9 aliguori
    int i, j;
582 108534b9 aliguori
583 108534b9 aliguori
    for(i = 0; i < L2_CACHE_SIZE; i++) {
584 108534b9 aliguori
        if (l2_offset == s->l2_cache_offsets[i]) {
585 108534b9 aliguori
            /* increment the hit count */
586 108534b9 aliguori
            if (++s->l2_cache_counts[i] == 0xffffffff) {
587 108534b9 aliguori
                for(j = 0; j < L2_CACHE_SIZE; j++) {
588 108534b9 aliguori
                    s->l2_cache_counts[j] >>= 1;
589 108534b9 aliguori
                }
590 108534b9 aliguori
            }
591 108534b9 aliguori
            return s->l2_cache + (i << s->l2_bits);
592 108534b9 aliguori
        }
593 108534b9 aliguori
    }
594 108534b9 aliguori
    return NULL;
595 108534b9 aliguori
}
596 108534b9 aliguori
597 108534b9 aliguori
/*
598 108534b9 aliguori
 * l2_load
599 108534b9 aliguori
 *
600 108534b9 aliguori
 * Loads a L2 table into memory. If the table is in the cache, the cache
601 108534b9 aliguori
 * is used; otherwise the L2 table is loaded from the image file.
602 108534b9 aliguori
 *
603 108534b9 aliguori
 * Returns a pointer to the L2 table on success, or NULL if the read from
604 108534b9 aliguori
 * the image file failed.
605 108534b9 aliguori
 */
606 108534b9 aliguori
607 108534b9 aliguori
static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
608 108534b9 aliguori
{
609 108534b9 aliguori
    BDRVQcowState *s = bs->opaque;
610 108534b9 aliguori
    int min_index;
611 108534b9 aliguori
    uint64_t *l2_table;
612 108534b9 aliguori
613 108534b9 aliguori
    /* seek if the table for the given offset is in the cache */
614 108534b9 aliguori
615 108534b9 aliguori
    l2_table = seek_l2_table(s, l2_offset);
616 108534b9 aliguori
    if (l2_table != NULL)
617 108534b9 aliguori
        return l2_table;
618 108534b9 aliguori
619 108534b9 aliguori
    /* not found: load a new entry in the least used one */
620 108534b9 aliguori
621 108534b9 aliguori
    min_index = l2_cache_new_entry(bs);
622 108534b9 aliguori
    l2_table = s->l2_cache + (min_index << s->l2_bits);
623 108534b9 aliguori
    if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
624 108534b9 aliguori
        s->l2_size * sizeof(uint64_t))
625 108534b9 aliguori
        return NULL;
626 108534b9 aliguori
    s->l2_cache_offsets[min_index] = l2_offset;
627 108534b9 aliguori
    s->l2_cache_counts[min_index] = 1;
628 108534b9 aliguori
629 108534b9 aliguori
    return l2_table;
630 108534b9 aliguori
}
631 108534b9 aliguori
632 108534b9 aliguori
/*
633 108534b9 aliguori
 * l2_allocate
634 585f8587 bellard
 *
635 108534b9 aliguori
 * Allocate a new l2 entry in the file. If l1_index points to an already
636 108534b9 aliguori
 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
637 108534b9 aliguori
 * table) copy the contents of the old L2 table into the newly allocated one.
638 108534b9 aliguori
 * Otherwise the new table is initialized with zeros.
639 585f8587 bellard
 *
640 585f8587 bellard
 */
641 108534b9 aliguori
642 108534b9 aliguori
static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
643 108534b9 aliguori
{
644 108534b9 aliguori
    BDRVQcowState *s = bs->opaque;
645 108534b9 aliguori
    int min_index;
646 108534b9 aliguori
    uint64_t old_l2_offset, tmp;
647 108534b9 aliguori
    uint64_t *l2_table, l2_offset;
648 108534b9 aliguori
649 108534b9 aliguori
    old_l2_offset = s->l1_table[l1_index];
650 108534b9 aliguori
651 108534b9 aliguori
    /* allocate a new l2 entry */
652 108534b9 aliguori
653 108534b9 aliguori
    l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
654 108534b9 aliguori
655 108534b9 aliguori
    /* update the L1 entry */
656 108534b9 aliguori
657 108534b9 aliguori
    s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
658 108534b9 aliguori
659 108534b9 aliguori
    tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
660 108534b9 aliguori
    if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
661 108534b9 aliguori
                    &tmp, sizeof(tmp)) != sizeof(tmp))
662 108534b9 aliguori
        return NULL;
663 108534b9 aliguori
664 108534b9 aliguori
    /* allocate a new entry in the l2 cache */
665 108534b9 aliguori
666 108534b9 aliguori
    min_index = l2_cache_new_entry(bs);
667 108534b9 aliguori
    l2_table = s->l2_cache + (min_index << s->l2_bits);
668 108534b9 aliguori
669 108534b9 aliguori
    if (old_l2_offset == 0) {
670 108534b9 aliguori
        /* if there was no old l2 table, clear the new table */
671 108534b9 aliguori
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
672 108534b9 aliguori
    } else {
673 108534b9 aliguori
        /* if there was an old l2 table, read it from the disk */
674 108534b9 aliguori
        if (bdrv_pread(s->hd, old_l2_offset,
675 108534b9 aliguori
                       l2_table, s->l2_size * sizeof(uint64_t)) !=
676 108534b9 aliguori
            s->l2_size * sizeof(uint64_t))
677 108534b9 aliguori
            return NULL;
678 108534b9 aliguori
    }
679 108534b9 aliguori
    /* write the l2 table to the file */
680 108534b9 aliguori
    if (bdrv_pwrite(s->hd, l2_offset,
681 108534b9 aliguori
                    l2_table, s->l2_size * sizeof(uint64_t)) !=
682 108534b9 aliguori
        s->l2_size * sizeof(uint64_t))
683 108534b9 aliguori
        return NULL;
684 108534b9 aliguori
685 108534b9 aliguori
    /* update the l2 cache entry */
686 108534b9 aliguori
687 108534b9 aliguori
    s->l2_cache_offsets[min_index] = l2_offset;
688 108534b9 aliguori
    s->l2_cache_counts[min_index] = 1;
689 108534b9 aliguori
690 108534b9 aliguori
    return l2_table;
691 108534b9 aliguori
}
692 108534b9 aliguori
693 6db6c638 aliguori
static int size_to_clusters(BDRVQcowState *s, int64_t size)
694 6db6c638 aliguori
{
695 6db6c638 aliguori
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
696 6db6c638 aliguori
}
697 6db6c638 aliguori
698 6db6c638 aliguori
static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size,
699 ff4b91c2 aliguori
        uint64_t *l2_table, uint64_t start, uint64_t mask)
700 6db6c638 aliguori
{
701 6db6c638 aliguori
    int i;
702 6db6c638 aliguori
    uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask;
703 6db6c638 aliguori
704 ab5ccbd6 aliguori
    if (!offset)
705 ab5ccbd6 aliguori
        return 0;
706 ab5ccbd6 aliguori
707 ff4b91c2 aliguori
    for (i = start; i < start + nb_clusters; i++)
708 6db6c638 aliguori
        if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask))
709 6db6c638 aliguori
            break;
710 6db6c638 aliguori
711 ff4b91c2 aliguori
        return (i - start);
712 6db6c638 aliguori
}
713 6db6c638 aliguori
714 6db6c638 aliguori
static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table)
715 6db6c638 aliguori
{
716 6db6c638 aliguori
    int i = 0;
717 6db6c638 aliguori
718 6db6c638 aliguori
    while(nb_clusters-- && l2_table[i] == 0)
719 6db6c638 aliguori
        i++;
720 6db6c638 aliguori
721 6db6c638 aliguori
    return i;
722 6db6c638 aliguori
}
723 6db6c638 aliguori
724 05203524 aliguori
/*
725 05203524 aliguori
 * get_cluster_offset
726 05203524 aliguori
 *
727 05203524 aliguori
 * For a given offset of the disk image, return cluster offset in
728 05203524 aliguori
 * qcow2 file.
729 05203524 aliguori
 *
730 095a9c58 aliguori
 * on entry, *num is the number of contiguous clusters we'd like to
731 095a9c58 aliguori
 * access following offset.
732 095a9c58 aliguori
 *
733 095a9c58 aliguori
 * on exit, *num is the number of contiguous clusters we can read.
734 095a9c58 aliguori
 *
735 05203524 aliguori
 * Return 1, if the offset is found
736 05203524 aliguori
 * Return 0, otherwise.
737 05203524 aliguori
 *
738 05203524 aliguori
 */
739 05203524 aliguori
740 095a9c58 aliguori
static uint64_t get_cluster_offset(BlockDriverState *bs,
741 095a9c58 aliguori
                                   uint64_t offset, int *num)
742 05203524 aliguori
{
743 05203524 aliguori
    BDRVQcowState *s = bs->opaque;
744 05203524 aliguori
    int l1_index, l2_index;
745 6db6c638 aliguori
    uint64_t l2_offset, *l2_table, cluster_offset;
746 6db6c638 aliguori
    int l1_bits, c;
747 6db6c638 aliguori
    int index_in_cluster, nb_available, nb_needed, nb_clusters;
748 095a9c58 aliguori
749 095a9c58 aliguori
    index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
750 095a9c58 aliguori
    nb_needed = *num + index_in_cluster;
751 095a9c58 aliguori
752 095a9c58 aliguori
    l1_bits = s->l2_bits + s->cluster_bits;
753 095a9c58 aliguori
754 095a9c58 aliguori
    /* compute how many bytes there are between the offset and
755 6db6c638 aliguori
     * the end of the l1 entry
756 095a9c58 aliguori
     */
757 095a9c58 aliguori
758 095a9c58 aliguori
    nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
759 095a9c58 aliguori
760 095a9c58 aliguori
    /* compute the number of available sectors */
761 095a9c58 aliguori
762 095a9c58 aliguori
    nb_available = (nb_available >> 9) + index_in_cluster;
763 095a9c58 aliguori
764 f8de1660 aliguori
    if (nb_needed > nb_available) {
765 f8de1660 aliguori
        nb_needed = nb_available;
766 f8de1660 aliguori
    }
767 f8de1660 aliguori
768 095a9c58 aliguori
    cluster_offset = 0;
769 05203524 aliguori
770 05203524 aliguori
    /* seek the the l2 offset in the l1 table */
771 05203524 aliguori
772 095a9c58 aliguori
    l1_index = offset >> l1_bits;
773 05203524 aliguori
    if (l1_index >= s->l1_size)
774 095a9c58 aliguori
        goto out;
775 05203524 aliguori
776 05203524 aliguori
    l2_offset = s->l1_table[l1_index];
777 05203524 aliguori
778 05203524 aliguori
    /* seek the l2 table of the given l2 offset */
779 05203524 aliguori
780 05203524 aliguori
    if (!l2_offset)
781 095a9c58 aliguori
        goto out;
782 05203524 aliguori
783 05203524 aliguori
    /* load the l2 table in memory */
784 05203524 aliguori
785 05203524 aliguori
    l2_offset &= ~QCOW_OFLAG_COPIED;
786 05203524 aliguori
    l2_table = l2_load(bs, l2_offset);
787 05203524 aliguori
    if (l2_table == NULL)
788 768706a5 aliguori
        return 0;
789 05203524 aliguori
790 05203524 aliguori
    /* find the cluster offset for the given disk offset */
791 05203524 aliguori
792 05203524 aliguori
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
793 05203524 aliguori
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
794 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, nb_needed << 9);
795 095a9c58 aliguori
796 095a9c58 aliguori
    if (!cluster_offset) {
797 6db6c638 aliguori
        /* how many empty clusters ? */
798 6db6c638 aliguori
        c = count_contiguous_free_clusters(nb_clusters, &l2_table[l2_index]);
799 095a9c58 aliguori
    } else {
800 6db6c638 aliguori
        /* how many allocated clusters ? */
801 6db6c638 aliguori
        c = count_contiguous_clusters(nb_clusters, s->cluster_size,
802 ff4b91c2 aliguori
                &l2_table[l2_index], 0, QCOW_OFLAG_COPIED);
803 6db6c638 aliguori
    }
804 095a9c58 aliguori
805 6db6c638 aliguori
   nb_available = (c * s->cluster_sectors);
806 095a9c58 aliguori
out:
807 095a9c58 aliguori
    if (nb_available > nb_needed)
808 095a9c58 aliguori
        nb_available = nb_needed;
809 095a9c58 aliguori
810 095a9c58 aliguori
    *num = nb_available - index_in_cluster;
811 095a9c58 aliguori
812 6db6c638 aliguori
    return cluster_offset & ~QCOW_OFLAG_COPIED;
813 05203524 aliguori
}
814 05203524 aliguori
815 05203524 aliguori
/*
816 52d893ec aliguori
 * free_any_clusters
817 05203524 aliguori
 *
818 52d893ec aliguori
 * free clusters according to its type: compressed or not
819 05203524 aliguori
 *
820 52d893ec aliguori
 */
821 52d893ec aliguori
822 52d893ec aliguori
static void free_any_clusters(BlockDriverState *bs,
823 095a9c58 aliguori
                              uint64_t cluster_offset, int nb_clusters)
824 52d893ec aliguori
{
825 52d893ec aliguori
    BDRVQcowState *s = bs->opaque;
826 52d893ec aliguori
827 52d893ec aliguori
    /* free the cluster */
828 52d893ec aliguori
829 52d893ec aliguori
    if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
830 52d893ec aliguori
        int nb_csectors;
831 52d893ec aliguori
        nb_csectors = ((cluster_offset >> s->csize_shift) &
832 52d893ec aliguori
                       s->csize_mask) + 1;
833 52d893ec aliguori
        free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
834 52d893ec aliguori
                      nb_csectors * 512);
835 52d893ec aliguori
        return;
836 52d893ec aliguori
    }
837 52d893ec aliguori
838 095a9c58 aliguori
    free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
839 095a9c58 aliguori
840 095a9c58 aliguori
    return;
841 52d893ec aliguori
}
842 52d893ec aliguori
843 52d893ec aliguori
/*
844 52d893ec aliguori
 * get_cluster_table
845 05203524 aliguori
 *
846 52d893ec aliguori
 * for a given disk offset, load (and allocate if needed)
847 52d893ec aliguori
 * the l2 table.
848 52d893ec aliguori
 *
849 52d893ec aliguori
 * the l2 table offset in the qcow2 file and the cluster index
850 52d893ec aliguori
 * in the l2 table are given to the caller.
851 05203524 aliguori
 *
852 05203524 aliguori
 */
853 05203524 aliguori
854 52d893ec aliguori
static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
855 52d893ec aliguori
                             uint64_t **new_l2_table,
856 52d893ec aliguori
                             uint64_t *new_l2_offset,
857 52d893ec aliguori
                             int *new_l2_index)
858 585f8587 bellard
{
859 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
860 108534b9 aliguori
    int l1_index, l2_index, ret;
861 52d893ec aliguori
    uint64_t l2_offset, *l2_table;
862 108534b9 aliguori
863 108534b9 aliguori
    /* seek the the l2 offset in the l1 table */
864 3b46e624 ths
865 585f8587 bellard
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
866 585f8587 bellard
    if (l1_index >= s->l1_size) {
867 108534b9 aliguori
        ret = grow_l1_table(bs, l1_index + 1);
868 108534b9 aliguori
        if (ret < 0)
869 585f8587 bellard
            return 0;
870 585f8587 bellard
    }
871 585f8587 bellard
    l2_offset = s->l1_table[l1_index];
872 108534b9 aliguori
873 108534b9 aliguori
    /* seek the l2 table of the given l2 offset */
874 108534b9 aliguori
875 05203524 aliguori
    if (l2_offset & QCOW_OFLAG_COPIED) {
876 05203524 aliguori
        /* load the l2 table in memory */
877 05203524 aliguori
        l2_offset &= ~QCOW_OFLAG_COPIED;
878 05203524 aliguori
        l2_table = l2_load(bs, l2_offset);
879 05203524 aliguori
        if (l2_table == NULL)
880 585f8587 bellard
            return 0;
881 05203524 aliguori
    } else {
882 05203524 aliguori
        if (l2_offset)
883 05203524 aliguori
            free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
884 108534b9 aliguori
        l2_table = l2_allocate(bs, l1_index);
885 108534b9 aliguori
        if (l2_table == NULL)
886 585f8587 bellard
            return 0;
887 108534b9 aliguori
        l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
888 585f8587 bellard
    }
889 108534b9 aliguori
890 108534b9 aliguori
    /* find the cluster offset for the given disk offset */
891 108534b9 aliguori
892 585f8587 bellard
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
893 05203524 aliguori
894 52d893ec aliguori
    *new_l2_table = l2_table;
895 52d893ec aliguori
    *new_l2_offset = l2_offset;
896 52d893ec aliguori
    *new_l2_index = l2_index;
897 52d893ec aliguori
898 52d893ec aliguori
    return 1;
899 52d893ec aliguori
}
900 52d893ec aliguori
901 52d893ec aliguori
/*
902 52d893ec aliguori
 * alloc_compressed_cluster_offset
903 52d893ec aliguori
 *
904 52d893ec aliguori
 * For a given offset of the disk image, return cluster offset in
905 52d893ec aliguori
 * qcow2 file.
906 52d893ec aliguori
 *
907 52d893ec aliguori
 * If the offset is not found, allocate a new compressed cluster.
908 52d893ec aliguori
 *
909 52d893ec aliguori
 * Return the cluster offset if successful,
910 52d893ec aliguori
 * Return 0, otherwise.
911 52d893ec aliguori
 *
912 52d893ec aliguori
 */
913 52d893ec aliguori
914 52d893ec aliguori
static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
915 52d893ec aliguori
                                                uint64_t offset,
916 52d893ec aliguori
                                                int compressed_size)
917 52d893ec aliguori
{
918 52d893ec aliguori
    BDRVQcowState *s = bs->opaque;
919 52d893ec aliguori
    int l2_index, ret;
920 52d893ec aliguori
    uint64_t l2_offset, *l2_table, cluster_offset;
921 52d893ec aliguori
    int nb_csectors;
922 52d893ec aliguori
923 52d893ec aliguori
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
924 52d893ec aliguori
    if (ret == 0)
925 52d893ec aliguori
        return 0;
926 52d893ec aliguori
927 52d893ec aliguori
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
928 05203524 aliguori
    if (cluster_offset & QCOW_OFLAG_COPIED)
929 05203524 aliguori
        return cluster_offset & ~QCOW_OFLAG_COPIED;
930 05203524 aliguori
931 095a9c58 aliguori
    if (cluster_offset)
932 095a9c58 aliguori
        free_any_clusters(bs, cluster_offset, 1);
933 108534b9 aliguori
934 52d893ec aliguori
    cluster_offset = alloc_bytes(bs, compressed_size);
935 52d893ec aliguori
    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
936 52d893ec aliguori
                  (cluster_offset >> 9);
937 05203524 aliguori
938 52d893ec aliguori
    cluster_offset |= QCOW_OFLAG_COMPRESSED |
939 52d893ec aliguori
                      ((uint64_t)nb_csectors << s->csize_shift);
940 05203524 aliguori
941 52d893ec aliguori
    /* update L2 table */
942 05203524 aliguori
943 52d893ec aliguori
    /* compressed clusters never have the copied flag */
944 05203524 aliguori
945 52d893ec aliguori
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
946 52d893ec aliguori
    if (bdrv_pwrite(s->hd,
947 52d893ec aliguori
                    l2_offset + l2_index * sizeof(uint64_t),
948 52d893ec aliguori
                    l2_table + l2_index,
949 52d893ec aliguori
                    sizeof(uint64_t)) != sizeof(uint64_t))
950 52d893ec aliguori
        return 0;
951 05203524 aliguori
952 52d893ec aliguori
    return cluster_offset;
953 52d893ec aliguori
}
954 05203524 aliguori
955 e976c6a1 aliguori
typedef struct QCowL2Meta
956 e976c6a1 aliguori
{
957 e976c6a1 aliguori
    uint64_t offset;
958 e976c6a1 aliguori
    int n_start;
959 e976c6a1 aliguori
    int nb_available;
960 e976c6a1 aliguori
    int nb_clusters;
961 e976c6a1 aliguori
} QCowL2Meta;
962 e976c6a1 aliguori
963 e976c6a1 aliguori
static int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
964 e976c6a1 aliguori
        QCowL2Meta *m)
965 e976c6a1 aliguori
{
966 e976c6a1 aliguori
    BDRVQcowState *s = bs->opaque;
967 e976c6a1 aliguori
    int i, j = 0, l2_index, ret;
968 e976c6a1 aliguori
    uint64_t *old_cluster, start_sect, l2_offset, *l2_table;
969 e976c6a1 aliguori
970 e976c6a1 aliguori
    if (m->nb_clusters == 0)
971 e976c6a1 aliguori
        return 0;
972 e976c6a1 aliguori
973 3ec88e80 aliguori
    old_cluster = qemu_malloc(m->nb_clusters * sizeof(uint64_t));
974 e976c6a1 aliguori
975 e976c6a1 aliguori
    /* copy content of unmodified sectors */
976 e976c6a1 aliguori
    start_sect = (m->offset & ~(s->cluster_size - 1)) >> 9;
977 e976c6a1 aliguori
    if (m->n_start) {
978 e976c6a1 aliguori
        ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start);
979 e976c6a1 aliguori
        if (ret < 0)
980 e976c6a1 aliguori
            goto err;
981 e976c6a1 aliguori
    }
982 e976c6a1 aliguori
983 e976c6a1 aliguori
    if (m->nb_available & (s->cluster_sectors - 1)) {
984 e976c6a1 aliguori
        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
985 e976c6a1 aliguori
        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
986 e976c6a1 aliguori
                m->nb_available - end, s->cluster_sectors);
987 e976c6a1 aliguori
        if (ret < 0)
988 e976c6a1 aliguori
            goto err;
989 e976c6a1 aliguori
    }
990 e976c6a1 aliguori
991 e976c6a1 aliguori
    ret = -EIO;
992 e976c6a1 aliguori
    /* update L2 table */
993 e976c6a1 aliguori
    if (!get_cluster_table(bs, m->offset, &l2_table, &l2_offset, &l2_index))
994 e976c6a1 aliguori
        goto err;
995 e976c6a1 aliguori
996 e976c6a1 aliguori
    for (i = 0; i < m->nb_clusters; i++) {
997 e976c6a1 aliguori
        if(l2_table[l2_index + i] != 0)
998 e976c6a1 aliguori
            old_cluster[j++] = l2_table[l2_index + i];
999 e976c6a1 aliguori
1000 e976c6a1 aliguori
        l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
1001 e976c6a1 aliguori
                    (i << s->cluster_bits)) | QCOW_OFLAG_COPIED);
1002 e976c6a1 aliguori
     }
1003 e976c6a1 aliguori
1004 e976c6a1 aliguori
    if (bdrv_pwrite(s->hd, l2_offset + l2_index * sizeof(uint64_t),
1005 e976c6a1 aliguori
                l2_table + l2_index, m->nb_clusters * sizeof(uint64_t)) !=
1006 e976c6a1 aliguori
            m->nb_clusters * sizeof(uint64_t))
1007 e976c6a1 aliguori
        goto err;
1008 e976c6a1 aliguori
1009 e976c6a1 aliguori
    for (i = 0; i < j; i++)
1010 e976c6a1 aliguori
        free_any_clusters(bs, old_cluster[i], 1);
1011 e976c6a1 aliguori
1012 e976c6a1 aliguori
    ret = 0;
1013 e976c6a1 aliguori
err:
1014 e976c6a1 aliguori
    qemu_free(old_cluster);
1015 e976c6a1 aliguori
    return ret;
1016 e976c6a1 aliguori
 }
1017 e976c6a1 aliguori
1018 52d893ec aliguori
/*
1019 52d893ec aliguori
 * alloc_cluster_offset
1020 52d893ec aliguori
 *
1021 52d893ec aliguori
 * For a given offset of the disk image, return cluster offset in
1022 52d893ec aliguori
 * qcow2 file.
1023 52d893ec aliguori
 *
1024 52d893ec aliguori
 * If the offset is not found, allocate a new cluster.
1025 52d893ec aliguori
 *
1026 52d893ec aliguori
 * Return the cluster offset if successful,
1027 52d893ec aliguori
 * Return 0, otherwise.
1028 52d893ec aliguori
 *
1029 52d893ec aliguori
 */
1030 52d893ec aliguori
1031 52d893ec aliguori
static uint64_t alloc_cluster_offset(BlockDriverState *bs,
1032 52d893ec aliguori
                                     uint64_t offset,
1033 095a9c58 aliguori
                                     int n_start, int n_end,
1034 e976c6a1 aliguori
                                     int *num, QCowL2Meta *m)
1035 52d893ec aliguori
{
1036 52d893ec aliguori
    BDRVQcowState *s = bs->opaque;
1037 52d893ec aliguori
    int l2_index, ret;
1038 52d893ec aliguori
    uint64_t l2_offset, *l2_table, cluster_offset;
1039 e976c6a1 aliguori
    int nb_clusters, i = 0;
1040 52d893ec aliguori
1041 52d893ec aliguori
    ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
1042 52d893ec aliguori
    if (ret == 0)
1043 52d893ec aliguori
        return 0;
1044 52d893ec aliguori
1045 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, n_end << 9);
1046 6db6c638 aliguori
1047 e976c6a1 aliguori
    nb_clusters = MIN(nb_clusters, s->l2_size - l2_index);
1048 095a9c58 aliguori
1049 52d893ec aliguori
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
1050 52d893ec aliguori
1051 095a9c58 aliguori
    /* We keep all QCOW_OFLAG_COPIED clusters */
1052 095a9c58 aliguori
1053 095a9c58 aliguori
    if (cluster_offset & QCOW_OFLAG_COPIED) {
1054 6db6c638 aliguori
        nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size,
1055 ff4b91c2 aliguori
                &l2_table[l2_index], 0, 0);
1056 095a9c58 aliguori
1057 095a9c58 aliguori
        cluster_offset &= ~QCOW_OFLAG_COPIED;
1058 e976c6a1 aliguori
        m->nb_clusters = 0;
1059 095a9c58 aliguori
1060 095a9c58 aliguori
        goto out;
1061 095a9c58 aliguori
    }
1062 095a9c58 aliguori
1063 095a9c58 aliguori
    /* for the moment, multiple compressed clusters are not managed */
1064 095a9c58 aliguori
1065 095a9c58 aliguori
    if (cluster_offset & QCOW_OFLAG_COMPRESSED)
1066 095a9c58 aliguori
        nb_clusters = 1;
1067 095a9c58 aliguori
1068 bc352085 aliguori
    /* how many available clusters ? */
1069 095a9c58 aliguori
1070 bc352085 aliguori
    while (i < nb_clusters) {
1071 ab5ccbd6 aliguori
        i += count_contiguous_clusters(nb_clusters - i, s->cluster_size,
1072 ff4b91c2 aliguori
                &l2_table[l2_index], i, 0);
1073 ab5ccbd6 aliguori
1074 ab5ccbd6 aliguori
        if(be64_to_cpu(l2_table[l2_index + i]))
1075 ab5ccbd6 aliguori
            break;
1076 ab5ccbd6 aliguori
1077 6db6c638 aliguori
        i += count_contiguous_free_clusters(nb_clusters - i,
1078 6db6c638 aliguori
                &l2_table[l2_index + i]);
1079 095a9c58 aliguori
1080 6db6c638 aliguori
        cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
1081 095a9c58 aliguori
1082 6db6c638 aliguori
        if ((cluster_offset & QCOW_OFLAG_COPIED) ||
1083 bc352085 aliguori
                (cluster_offset & QCOW_OFLAG_COMPRESSED))
1084 6db6c638 aliguori
            break;
1085 095a9c58 aliguori
    }
1086 bc352085 aliguori
    nb_clusters = i;
1087 05203524 aliguori
1088 05203524 aliguori
    /* allocate a new cluster */
1089 05203524 aliguori
1090 095a9c58 aliguori
    cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
1091 05203524 aliguori
1092 e976c6a1 aliguori
    /* save info needed for meta data update */
1093 e976c6a1 aliguori
    m->offset = offset;
1094 e976c6a1 aliguori
    m->n_start = n_start;
1095 e976c6a1 aliguori
    m->nb_clusters = nb_clusters;
1096 05203524 aliguori
1097 095a9c58 aliguori
out:
1098 e976c6a1 aliguori
    m->nb_available = MIN(nb_clusters << (s->cluster_bits - 9), n_end);
1099 e976c6a1 aliguori
1100 e976c6a1 aliguori
    *num = m->nb_available - n_start;
1101 095a9c58 aliguori
1102 585f8587 bellard
    return cluster_offset;
1103 585f8587 bellard
}
1104 585f8587 bellard
1105 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
1106 585f8587 bellard
                             int nb_sectors, int *pnum)
1107 585f8587 bellard
{
1108 585f8587 bellard
    uint64_t cluster_offset;
1109 585f8587 bellard
1110 095a9c58 aliguori
    *pnum = nb_sectors;
1111 095a9c58 aliguori
    cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
1112 095a9c58 aliguori
1113 585f8587 bellard
    return (cluster_offset != 0);
1114 585f8587 bellard
}
1115 585f8587 bellard
1116 585f8587 bellard
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1117 585f8587 bellard
                             const uint8_t *buf, int buf_size)
1118 585f8587 bellard
{
1119 585f8587 bellard
    z_stream strm1, *strm = &strm1;
1120 585f8587 bellard
    int ret, out_len;
1121 585f8587 bellard
1122 585f8587 bellard
    memset(strm, 0, sizeof(*strm));
1123 585f8587 bellard
1124 585f8587 bellard
    strm->next_in = (uint8_t *)buf;
1125 585f8587 bellard
    strm->avail_in = buf_size;
1126 585f8587 bellard
    strm->next_out = out_buf;
1127 585f8587 bellard
    strm->avail_out = out_buf_size;
1128 585f8587 bellard
1129 585f8587 bellard
    ret = inflateInit2(strm, -12);
1130 585f8587 bellard
    if (ret != Z_OK)
1131 585f8587 bellard
        return -1;
1132 585f8587 bellard
    ret = inflate(strm, Z_FINISH);
1133 585f8587 bellard
    out_len = strm->next_out - out_buf;
1134 585f8587 bellard
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1135 585f8587 bellard
        out_len != out_buf_size) {
1136 585f8587 bellard
        inflateEnd(strm);
1137 585f8587 bellard
        return -1;
1138 585f8587 bellard
    }
1139 585f8587 bellard
    inflateEnd(strm);
1140 585f8587 bellard
    return 0;
1141 585f8587 bellard
}
1142 3b46e624 ths
1143 585f8587 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1144 585f8587 bellard
{
1145 585f8587 bellard
    int ret, csize, nb_csectors, sector_offset;
1146 585f8587 bellard
    uint64_t coffset;
1147 585f8587 bellard
1148 585f8587 bellard
    coffset = cluster_offset & s->cluster_offset_mask;
1149 585f8587 bellard
    if (s->cluster_cache_offset != coffset) {
1150 585f8587 bellard
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1151 585f8587 bellard
        sector_offset = coffset & 511;
1152 585f8587 bellard
        csize = nb_csectors * 512 - sector_offset;
1153 585f8587 bellard
        ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1154 585f8587 bellard
        if (ret < 0) {
1155 585f8587 bellard
            return -1;
1156 585f8587 bellard
        }
1157 585f8587 bellard
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
1158 585f8587 bellard
                              s->cluster_data + sector_offset, csize) < 0) {
1159 585f8587 bellard
            return -1;
1160 585f8587 bellard
        }
1161 585f8587 bellard
        s->cluster_cache_offset = coffset;
1162 585f8587 bellard
    }
1163 585f8587 bellard
    return 0;
1164 585f8587 bellard
}
1165 585f8587 bellard
1166 a9465922 bellard
/* handle reading after the end of the backing file */
1167 5fafdf24 ths
static int backing_read1(BlockDriverState *bs,
1168 a9465922 bellard
                         int64_t sector_num, uint8_t *buf, int nb_sectors)
1169 a9465922 bellard
{
1170 a9465922 bellard
    int n1;
1171 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
1172 a9465922 bellard
        return nb_sectors;
1173 a9465922 bellard
    if (sector_num >= bs->total_sectors)
1174 a9465922 bellard
        n1 = 0;
1175 a9465922 bellard
    else
1176 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
1177 a9465922 bellard
    memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1178 a9465922 bellard
    return n1;
1179 a9465922 bellard
}
1180 a9465922 bellard
1181 5fafdf24 ths
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
1182 585f8587 bellard
                     uint8_t *buf, int nb_sectors)
1183 585f8587 bellard
{
1184 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1185 a9465922 bellard
    int ret, index_in_cluster, n, n1;
1186 585f8587 bellard
    uint64_t cluster_offset;
1187 3b46e624 ths
1188 585f8587 bellard
    while (nb_sectors > 0) {
1189 095a9c58 aliguori
        n = nb_sectors;
1190 095a9c58 aliguori
        cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
1191 585f8587 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
1192 585f8587 bellard
        if (!cluster_offset) {
1193 585f8587 bellard
            if (bs->backing_hd) {
1194 585f8587 bellard
                /* read from the base image */
1195 a9465922 bellard
                n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1196 a9465922 bellard
                if (n1 > 0) {
1197 a9465922 bellard
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1198 a9465922 bellard
                    if (ret < 0)
1199 a9465922 bellard
                        return -1;
1200 a9465922 bellard
                }
1201 585f8587 bellard
            } else {
1202 585f8587 bellard
                memset(buf, 0, 512 * n);
1203 585f8587 bellard
            }
1204 585f8587 bellard
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1205 585f8587 bellard
            if (decompress_cluster(s, cluster_offset) < 0)
1206 585f8587 bellard
                return -1;
1207 585f8587 bellard
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1208 585f8587 bellard
        } else {
1209 585f8587 bellard
            ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1210 5fafdf24 ths
            if (ret != n * 512)
1211 585f8587 bellard
                return -1;
1212 585f8587 bellard
            if (s->crypt_method) {
1213 5fafdf24 ths
                encrypt_sectors(s, sector_num, buf, buf, n, 0,
1214 585f8587 bellard
                                &s->aes_decrypt_key);
1215 585f8587 bellard
            }
1216 585f8587 bellard
        }
1217 585f8587 bellard
        nb_sectors -= n;
1218 585f8587 bellard
        sector_num += n;
1219 585f8587 bellard
        buf += n * 512;
1220 585f8587 bellard
    }
1221 585f8587 bellard
    return 0;
1222 585f8587 bellard
}
1223 585f8587 bellard
1224 5fafdf24 ths
static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1225 585f8587 bellard
                     const uint8_t *buf, int nb_sectors)
1226 585f8587 bellard
{
1227 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1228 585f8587 bellard
    int ret, index_in_cluster, n;
1229 585f8587 bellard
    uint64_t cluster_offset;
1230 095a9c58 aliguori
    int n_end;
1231 e976c6a1 aliguori
    QCowL2Meta l2meta;
1232 3b46e624 ths
1233 585f8587 bellard
    while (nb_sectors > 0) {
1234 585f8587 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
1235 095a9c58 aliguori
        n_end = index_in_cluster + nb_sectors;
1236 095a9c58 aliguori
        if (s->crypt_method &&
1237 095a9c58 aliguori
            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1238 095a9c58 aliguori
            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1239 52d893ec aliguori
        cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1240 05203524 aliguori
                                              index_in_cluster,
1241 e976c6a1 aliguori
                                              n_end, &n, &l2meta);
1242 585f8587 bellard
        if (!cluster_offset)
1243 585f8587 bellard
            return -1;
1244 585f8587 bellard
        if (s->crypt_method) {
1245 585f8587 bellard
            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1246 585f8587 bellard
                            &s->aes_encrypt_key);
1247 5fafdf24 ths
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1248 585f8587 bellard
                              s->cluster_data, n * 512);
1249 585f8587 bellard
        } else {
1250 585f8587 bellard
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1251 585f8587 bellard
        }
1252 e976c6a1 aliguori
        if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) {
1253 e976c6a1 aliguori
            free_any_clusters(bs, cluster_offset, l2meta.nb_clusters);
1254 585f8587 bellard
            return -1;
1255 e976c6a1 aliguori
        }
1256 585f8587 bellard
        nb_sectors -= n;
1257 585f8587 bellard
        sector_num += n;
1258 585f8587 bellard
        buf += n * 512;
1259 585f8587 bellard
    }
1260 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
1261 585f8587 bellard
    return 0;
1262 585f8587 bellard
}
1263 585f8587 bellard
1264 ce1a14dc pbrook
typedef struct QCowAIOCB {
1265 ce1a14dc pbrook
    BlockDriverAIOCB common;
1266 585f8587 bellard
    int64_t sector_num;
1267 585f8587 bellard
    uint8_t *buf;
1268 585f8587 bellard
    int nb_sectors;
1269 585f8587 bellard
    int n;
1270 585f8587 bellard
    uint64_t cluster_offset;
1271 5fafdf24 ths
    uint8_t *cluster_data;
1272 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
1273 1490791f aliguori
    QEMUBH *bh;
1274 e976c6a1 aliguori
    QCowL2Meta l2meta;
1275 585f8587 bellard
} QCowAIOCB;
1276 585f8587 bellard
1277 1490791f aliguori
static void qcow_aio_read_cb(void *opaque, int ret);
1278 1490791f aliguori
static void qcow_aio_read_bh(void *opaque)
1279 1490791f aliguori
{
1280 1490791f aliguori
    QCowAIOCB *acb = opaque;
1281 1490791f aliguori
    qemu_bh_delete(acb->bh);
1282 1490791f aliguori
    acb->bh = NULL;
1283 1490791f aliguori
    qcow_aio_read_cb(opaque, 0);
1284 1490791f aliguori
}
1285 1490791f aliguori
1286 a32ef786 aliguori
static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
1287 a32ef786 aliguori
{
1288 a32ef786 aliguori
    if (acb->bh)
1289 a32ef786 aliguori
        return -EIO;
1290 a32ef786 aliguori
1291 a32ef786 aliguori
    acb->bh = qemu_bh_new(cb, acb);
1292 a32ef786 aliguori
    if (!acb->bh)
1293 a32ef786 aliguori
        return -EIO;
1294 a32ef786 aliguori
1295 a32ef786 aliguori
    qemu_bh_schedule(acb->bh);
1296 a32ef786 aliguori
1297 a32ef786 aliguori
    return 0;
1298 a32ef786 aliguori
}
1299 a32ef786 aliguori
1300 585f8587 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
1301 585f8587 bellard
{
1302 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
1303 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1304 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1305 a9465922 bellard
    int index_in_cluster, n1;
1306 585f8587 bellard
1307 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
1308 585f8587 bellard
    if (ret < 0) {
1309 ac674887 aliguori
fail:
1310 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, ret);
1311 ce1a14dc pbrook
        qemu_aio_release(acb);
1312 585f8587 bellard
        return;
1313 585f8587 bellard
    }
1314 585f8587 bellard
1315 585f8587 bellard
    /* post process the read buffer */
1316 ce1a14dc pbrook
    if (!acb->cluster_offset) {
1317 585f8587 bellard
        /* nothing to do */
1318 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1319 585f8587 bellard
        /* nothing to do */
1320 585f8587 bellard
    } else {
1321 585f8587 bellard
        if (s->crypt_method) {
1322 5fafdf24 ths
            encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1323 5fafdf24 ths
                            acb->n, 0,
1324 585f8587 bellard
                            &s->aes_decrypt_key);
1325 585f8587 bellard
        }
1326 585f8587 bellard
    }
1327 585f8587 bellard
1328 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
1329 ce1a14dc pbrook
    acb->sector_num += acb->n;
1330 ce1a14dc pbrook
    acb->buf += acb->n * 512;
1331 585f8587 bellard
1332 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
1333 585f8587 bellard
        /* request completed */
1334 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
1335 ce1a14dc pbrook
        qemu_aio_release(acb);
1336 585f8587 bellard
        return;
1337 585f8587 bellard
    }
1338 3b46e624 ths
1339 585f8587 bellard
    /* prepare next AIO request */
1340 095a9c58 aliguori
    acb->n = acb->nb_sectors;
1341 095a9c58 aliguori
    acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
1342 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1343 ce1a14dc pbrook
1344 ce1a14dc pbrook
    if (!acb->cluster_offset) {
1345 585f8587 bellard
        if (bs->backing_hd) {
1346 585f8587 bellard
            /* read from the base image */
1347 5fafdf24 ths
            n1 = backing_read1(bs->backing_hd, acb->sector_num,
1348 ce1a14dc pbrook
                               acb->buf, acb->n);
1349 a9465922 bellard
            if (n1 > 0) {
1350 5fafdf24 ths
                acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
1351 ce1a14dc pbrook
                                    acb->buf, acb->n, qcow_aio_read_cb, acb);
1352 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
1353 a9465922 bellard
                    goto fail;
1354 a9465922 bellard
            } else {
1355 a32ef786 aliguori
                ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1356 a32ef786 aliguori
                if (ret < 0)
1357 ac674887 aliguori
                    goto fail;
1358 a9465922 bellard
            }
1359 585f8587 bellard
        } else {
1360 585f8587 bellard
            /* Note: in this case, no need to wait */
1361 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
1362 a32ef786 aliguori
            ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1363 a32ef786 aliguori
            if (ret < 0)
1364 ac674887 aliguori
                goto fail;
1365 585f8587 bellard
        }
1366 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
1367 585f8587 bellard
        /* add AIO support for compressed blocks ? */
1368 ce1a14dc pbrook
        if (decompress_cluster(s, acb->cluster_offset) < 0)
1369 585f8587 bellard
            goto fail;
1370 5fafdf24 ths
        memcpy(acb->buf,
1371 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1372 a32ef786 aliguori
        ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
1373 a32ef786 aliguori
        if (ret < 0)
1374 ac674887 aliguori
            goto fail;
1375 585f8587 bellard
    } else {
1376 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
1377 585f8587 bellard
            ret = -EIO;
1378 585f8587 bellard
            goto fail;
1379 585f8587 bellard
        }
1380 ce1a14dc pbrook
        acb->hd_aiocb = bdrv_aio_read(s->hd,
1381 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
1382 ce1a14dc pbrook
                            acb->buf, acb->n, qcow_aio_read_cb, acb);
1383 ce1a14dc pbrook
        if (acb->hd_aiocb == NULL)
1384 585f8587 bellard
            goto fail;
1385 585f8587 bellard
    }
1386 585f8587 bellard
}
1387 585f8587 bellard
1388 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1389 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1390 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1391 585f8587 bellard
{
1392 ce1a14dc pbrook
    QCowAIOCB *acb;
1393 ce1a14dc pbrook
1394 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1395 ce1a14dc pbrook
    if (!acb)
1396 ce1a14dc pbrook
        return NULL;
1397 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
1398 ce1a14dc pbrook
    acb->sector_num = sector_num;
1399 ce1a14dc pbrook
    acb->buf = buf;
1400 ce1a14dc pbrook
    acb->nb_sectors = nb_sectors;
1401 ce1a14dc pbrook
    acb->n = 0;
1402 ce1a14dc pbrook
    acb->cluster_offset = 0;
1403 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
1404 ce1a14dc pbrook
    return acb;
1405 ce1a14dc pbrook
}
1406 ce1a14dc pbrook
1407 ce1a14dc pbrook
static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1408 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1409 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1410 ce1a14dc pbrook
{
1411 ce1a14dc pbrook
    QCowAIOCB *acb;
1412 ce1a14dc pbrook
1413 ce1a14dc pbrook
    acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1414 ce1a14dc pbrook
    if (!acb)
1415 ce1a14dc pbrook
        return NULL;
1416 585f8587 bellard
1417 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
1418 ce1a14dc pbrook
    return &acb->common;
1419 585f8587 bellard
}
1420 585f8587 bellard
1421 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
1422 585f8587 bellard
{
1423 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
1424 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1425 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1426 585f8587 bellard
    int index_in_cluster;
1427 585f8587 bellard
    const uint8_t *src_buf;
1428 095a9c58 aliguori
    int n_end;
1429 ce1a14dc pbrook
1430 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
1431 ce1a14dc pbrook
1432 585f8587 bellard
    if (ret < 0) {
1433 585f8587 bellard
    fail:
1434 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, ret);
1435 ce1a14dc pbrook
        qemu_aio_release(acb);
1436 585f8587 bellard
        return;
1437 585f8587 bellard
    }
1438 585f8587 bellard
1439 e976c6a1 aliguori
    if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
1440 e976c6a1 aliguori
        free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
1441 e976c6a1 aliguori
        goto fail;
1442 e976c6a1 aliguori
    }
1443 e976c6a1 aliguori
1444 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
1445 ce1a14dc pbrook
    acb->sector_num += acb->n;
1446 ce1a14dc pbrook
    acb->buf += acb->n * 512;
1447 585f8587 bellard
1448 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
1449 585f8587 bellard
        /* request completed */
1450 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
1451 ce1a14dc pbrook
        qemu_aio_release(acb);
1452 585f8587 bellard
        return;
1453 585f8587 bellard
    }
1454 3b46e624 ths
1455 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
1456 095a9c58 aliguori
    n_end = index_in_cluster + acb->nb_sectors;
1457 095a9c58 aliguori
    if (s->crypt_method &&
1458 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1459 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1460 095a9c58 aliguori
1461 e976c6a1 aliguori
    acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
1462 05203524 aliguori
                                          index_in_cluster,
1463 e976c6a1 aliguori
                                          n_end, &acb->n, &acb->l2meta);
1464 e976c6a1 aliguori
    if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
1465 585f8587 bellard
        ret = -EIO;
1466 585f8587 bellard
        goto fail;
1467 585f8587 bellard
    }
1468 585f8587 bellard
    if (s->crypt_method) {
1469 ce1a14dc pbrook
        if (!acb->cluster_data) {
1470 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1471 095a9c58 aliguori
                                             s->cluster_size);
1472 585f8587 bellard
        }
1473 5fafdf24 ths
        encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
1474 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
1475 ce1a14dc pbrook
        src_buf = acb->cluster_data;
1476 585f8587 bellard
    } else {
1477 ce1a14dc pbrook
        src_buf = acb->buf;
1478 585f8587 bellard
    }
1479 ce1a14dc pbrook
    acb->hd_aiocb = bdrv_aio_write(s->hd,
1480 e976c6a1 aliguori
                                   (acb->cluster_offset >> 9) + index_in_cluster,
1481 5fafdf24 ths
                                   src_buf, acb->n,
1482 ce1a14dc pbrook
                                   qcow_aio_write_cb, acb);
1483 ce1a14dc pbrook
    if (acb->hd_aiocb == NULL)
1484 585f8587 bellard
        goto fail;
1485 585f8587 bellard
}
1486 585f8587 bellard
1487 ce1a14dc pbrook
static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1488 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
1489 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1490 585f8587 bellard
{
1491 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1492 ce1a14dc pbrook
    QCowAIOCB *acb;
1493 3b46e624 ths
1494 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
1495 585f8587 bellard
1496 ce1a14dc pbrook
    acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1497 ce1a14dc pbrook
    if (!acb)
1498 ce1a14dc pbrook
        return NULL;
1499 3b46e624 ths
1500 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
1501 ce1a14dc pbrook
    return &acb->common;
1502 585f8587 bellard
}
1503 585f8587 bellard
1504 ce1a14dc pbrook
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1505 585f8587 bellard
{
1506 ce1a14dc pbrook
    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1507 ce1a14dc pbrook
    if (acb->hd_aiocb)
1508 ce1a14dc pbrook
        bdrv_aio_cancel(acb->hd_aiocb);
1509 ce1a14dc pbrook
    qemu_aio_release(acb);
1510 585f8587 bellard
}
1511 585f8587 bellard
1512 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
1513 585f8587 bellard
{
1514 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1515 585f8587 bellard
    qemu_free(s->l1_table);
1516 585f8587 bellard
    qemu_free(s->l2_cache);
1517 585f8587 bellard
    qemu_free(s->cluster_cache);
1518 585f8587 bellard
    qemu_free(s->cluster_data);
1519 585f8587 bellard
    refcount_close(bs);
1520 585f8587 bellard
    bdrv_delete(s->hd);
1521 585f8587 bellard
}
1522 585f8587 bellard
1523 585f8587 bellard
/* XXX: use std qcow open function ? */
1524 585f8587 bellard
typedef struct QCowCreateState {
1525 585f8587 bellard
    int cluster_size;
1526 585f8587 bellard
    int cluster_bits;
1527 585f8587 bellard
    uint16_t *refcount_block;
1528 585f8587 bellard
    uint64_t *refcount_table;
1529 585f8587 bellard
    int64_t l1_table_offset;
1530 585f8587 bellard
    int64_t refcount_table_offset;
1531 585f8587 bellard
    int64_t refcount_block_offset;
1532 585f8587 bellard
} QCowCreateState;
1533 585f8587 bellard
1534 585f8587 bellard
static void create_refcount_update(QCowCreateState *s,
1535 585f8587 bellard
                                   int64_t offset, int64_t size)
1536 585f8587 bellard
{
1537 585f8587 bellard
    int refcount;
1538 585f8587 bellard
    int64_t start, last, cluster_offset;
1539 585f8587 bellard
    uint16_t *p;
1540 585f8587 bellard
1541 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
1542 585f8587 bellard
    last = (offset + size - 1)  & ~(s->cluster_size - 1);
1543 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
1544 585f8587 bellard
        cluster_offset += s->cluster_size) {
1545 585f8587 bellard
        p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1546 585f8587 bellard
        refcount = be16_to_cpu(*p);
1547 585f8587 bellard
        refcount++;
1548 585f8587 bellard
        *p = cpu_to_be16(refcount);
1549 585f8587 bellard
    }
1550 585f8587 bellard
}
1551 585f8587 bellard
1552 f965509c aliguori
static int qcow_create2(const char *filename, int64_t total_size,
1553 f965509c aliguori
                        const char *backing_file, const char *backing_format,
1554 f965509c aliguori
                        int flags)
1555 585f8587 bellard
{
1556 f965509c aliguori
1557 585f8587 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1558 2d2431f0 aliguori
    int ref_clusters, backing_format_len = 0;
1559 585f8587 bellard
    QCowHeader header;
1560 585f8587 bellard
    uint64_t tmp, offset;
1561 585f8587 bellard
    QCowCreateState s1, *s = &s1;
1562 f965509c aliguori
    QCowExtension ext_bf = {0, 0};
1563 f965509c aliguori
1564 3b46e624 ths
1565 585f8587 bellard
    memset(s, 0, sizeof(*s));
1566 585f8587 bellard
1567 585f8587 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1568 585f8587 bellard
    if (fd < 0)
1569 585f8587 bellard
        return -1;
1570 585f8587 bellard
    memset(&header, 0, sizeof(header));
1571 585f8587 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
1572 585f8587 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
1573 585f8587 bellard
    header.size = cpu_to_be64(total_size * 512);
1574 585f8587 bellard
    header_size = sizeof(header);
1575 585f8587 bellard
    backing_filename_len = 0;
1576 585f8587 bellard
    if (backing_file) {
1577 f965509c aliguori
        if (backing_format) {
1578 f965509c aliguori
            ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
1579 f965509c aliguori
            backing_format_len = strlen(backing_format);
1580 f965509c aliguori
            ext_bf.len = (backing_format_len + 7) & ~7;
1581 f965509c aliguori
            header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7);
1582 f965509c aliguori
        }
1583 585f8587 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
1584 585f8587 bellard
        backing_filename_len = strlen(backing_file);
1585 585f8587 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
1586 585f8587 bellard
        header_size += backing_filename_len;
1587 585f8587 bellard
    }
1588 585f8587 bellard
    s->cluster_bits = 12;  /* 4 KB clusters */
1589 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
1590 585f8587 bellard
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
1591 585f8587 bellard
    header_size = (header_size + 7) & ~7;
1592 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT) {
1593 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1594 585f8587 bellard
    } else {
1595 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1596 585f8587 bellard
    }
1597 585f8587 bellard
    l2_bits = s->cluster_bits - 3;
1598 585f8587 bellard
    shift = s->cluster_bits + l2_bits;
1599 585f8587 bellard
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1600 585f8587 bellard
    offset = align_offset(header_size, s->cluster_size);
1601 585f8587 bellard
    s->l1_table_offset = offset;
1602 585f8587 bellard
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1603 585f8587 bellard
    header.l1_size = cpu_to_be32(l1_size);
1604 15e6690a bellard
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1605 585f8587 bellard
1606 585f8587 bellard
    s->refcount_table = qemu_mallocz(s->cluster_size);
1607 3b46e624 ths
1608 585f8587 bellard
    s->refcount_table_offset = offset;
1609 585f8587 bellard
    header.refcount_table_offset = cpu_to_be64(offset);
1610 585f8587 bellard
    header.refcount_table_clusters = cpu_to_be32(1);
1611 585f8587 bellard
    offset += s->cluster_size;
1612 585f8587 bellard
    s->refcount_block_offset = offset;
1613 2d2431f0 aliguori
1614 2d2431f0 aliguori
    /* count how many refcount blocks needed */
1615 2d2431f0 aliguori
    tmp = offset >> s->cluster_bits;
1616 2d2431f0 aliguori
    ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
1617 2d2431f0 aliguori
    for (i=0; i < ref_clusters; i++) {
1618 2d2431f0 aliguori
        s->refcount_table[i] = cpu_to_be64(offset);
1619 2d2431f0 aliguori
        offset += s->cluster_size;
1620 2d2431f0 aliguori
    }
1621 2d2431f0 aliguori
1622 2d2431f0 aliguori
    s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
1623 585f8587 bellard
1624 585f8587 bellard
    /* update refcounts */
1625 585f8587 bellard
    create_refcount_update(s, 0, header_size);
1626 15e6690a bellard
    create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1627 585f8587 bellard
    create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1628 2d2431f0 aliguori
    create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size);
1629 3b46e624 ths
1630 585f8587 bellard
    /* write all the data */
1631 585f8587 bellard
    write(fd, &header, sizeof(header));
1632 585f8587 bellard
    if (backing_file) {
1633 f965509c aliguori
        if (backing_format_len) {
1634 f965509c aliguori
            char zero[16];
1635 f965509c aliguori
            int d = ext_bf.len - backing_format_len;
1636 f965509c aliguori
1637 f965509c aliguori
            memset(zero, 0, sizeof(zero));
1638 f965509c aliguori
            cpu_to_be32s(&ext_bf.magic);
1639 f965509c aliguori
            cpu_to_be32s(&ext_bf.len);
1640 f965509c aliguori
            write(fd, &ext_bf, sizeof(ext_bf));
1641 f965509c aliguori
            write(fd, backing_format, backing_format_len);
1642 f965509c aliguori
            if (d>0) {
1643 f965509c aliguori
                write(fd, zero, d);
1644 f965509c aliguori
            }
1645 f965509c aliguori
        }
1646 585f8587 bellard
        write(fd, backing_file, backing_filename_len);
1647 585f8587 bellard
    }
1648 585f8587 bellard
    lseek(fd, s->l1_table_offset, SEEK_SET);
1649 585f8587 bellard
    tmp = 0;
1650 585f8587 bellard
    for(i = 0;i < l1_size; i++) {
1651 585f8587 bellard
        write(fd, &tmp, sizeof(tmp));
1652 585f8587 bellard
    }
1653 585f8587 bellard
    lseek(fd, s->refcount_table_offset, SEEK_SET);
1654 585f8587 bellard
    write(fd, s->refcount_table, s->cluster_size);
1655 3b46e624 ths
1656 585f8587 bellard
    lseek(fd, s->refcount_block_offset, SEEK_SET);
1657 2d2431f0 aliguori
    write(fd, s->refcount_block, ref_clusters * s->cluster_size);
1658 585f8587 bellard
1659 585f8587 bellard
    qemu_free(s->refcount_table);
1660 585f8587 bellard
    qemu_free(s->refcount_block);
1661 585f8587 bellard
    close(fd);
1662 585f8587 bellard
    return 0;
1663 585f8587 bellard
}
1664 585f8587 bellard
1665 f965509c aliguori
static int qcow_create(const char *filename, int64_t total_size,
1666 f965509c aliguori
                       const char *backing_file, int flags)
1667 f965509c aliguori
{
1668 f965509c aliguori
    return qcow_create2(filename, total_size, backing_file, NULL, flags);
1669 f965509c aliguori
}
1670 f965509c aliguori
1671 585f8587 bellard
static int qcow_make_empty(BlockDriverState *bs)
1672 585f8587 bellard
{
1673 585f8587 bellard
#if 0
1674 585f8587 bellard
    /* XXX: not correct */
1675 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1676 585f8587 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1677 585f8587 bellard
    int ret;
1678 585f8587 bellard

1679 585f8587 bellard
    memset(s->l1_table, 0, l1_length);
1680 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1681 ac674887 aliguori
        return -1;
1682 585f8587 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1683 585f8587 bellard
    if (ret < 0)
1684 585f8587 bellard
        return ret;
1685 3b46e624 ths

1686 585f8587 bellard
    l2_cache_reset(bs);
1687 585f8587 bellard
#endif
1688 585f8587 bellard
    return 0;
1689 585f8587 bellard
}
1690 585f8587 bellard
1691 585f8587 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
1692 585f8587 bellard
   tables to avoid losing bytes in alignment */
1693 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1694 585f8587 bellard
                                 const uint8_t *buf, int nb_sectors)
1695 585f8587 bellard
{
1696 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1697 585f8587 bellard
    z_stream strm;
1698 585f8587 bellard
    int ret, out_len;
1699 585f8587 bellard
    uint8_t *out_buf;
1700 585f8587 bellard
    uint64_t cluster_offset;
1701 585f8587 bellard
1702 585f8587 bellard
    if (nb_sectors == 0) {
1703 585f8587 bellard
        /* align end of file to a sector boundary to ease reading with
1704 585f8587 bellard
           sector based I/Os */
1705 585f8587 bellard
        cluster_offset = bdrv_getlength(s->hd);
1706 585f8587 bellard
        cluster_offset = (cluster_offset + 511) & ~511;
1707 585f8587 bellard
        bdrv_truncate(s->hd, cluster_offset);
1708 585f8587 bellard
        return 0;
1709 585f8587 bellard
    }
1710 585f8587 bellard
1711 585f8587 bellard
    if (nb_sectors != s->cluster_sectors)
1712 585f8587 bellard
        return -EINVAL;
1713 585f8587 bellard
1714 585f8587 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1715 585f8587 bellard
1716 585f8587 bellard
    /* best compression, small window, no zlib header */
1717 585f8587 bellard
    memset(&strm, 0, sizeof(strm));
1718 585f8587 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1719 5fafdf24 ths
                       Z_DEFLATED, -12,
1720 585f8587 bellard
                       9, Z_DEFAULT_STRATEGY);
1721 585f8587 bellard
    if (ret != 0) {
1722 585f8587 bellard
        qemu_free(out_buf);
1723 585f8587 bellard
        return -1;
1724 585f8587 bellard
    }
1725 585f8587 bellard
1726 585f8587 bellard
    strm.avail_in = s->cluster_size;
1727 585f8587 bellard
    strm.next_in = (uint8_t *)buf;
1728 585f8587 bellard
    strm.avail_out = s->cluster_size;
1729 585f8587 bellard
    strm.next_out = out_buf;
1730 585f8587 bellard
1731 585f8587 bellard
    ret = deflate(&strm, Z_FINISH);
1732 585f8587 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
1733 585f8587 bellard
        qemu_free(out_buf);
1734 585f8587 bellard
        deflateEnd(&strm);
1735 585f8587 bellard
        return -1;
1736 585f8587 bellard
    }
1737 585f8587 bellard
    out_len = strm.next_out - out_buf;
1738 585f8587 bellard
1739 585f8587 bellard
    deflateEnd(&strm);
1740 585f8587 bellard
1741 585f8587 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1742 585f8587 bellard
        /* could not compress: write normal cluster */
1743 585f8587 bellard
        qcow_write(bs, sector_num, buf, s->cluster_sectors);
1744 585f8587 bellard
    } else {
1745 52d893ec aliguori
        cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1746 52d893ec aliguori
                                              out_len);
1747 52d893ec aliguori
        if (!cluster_offset)
1748 52d893ec aliguori
            return -1;
1749 585f8587 bellard
        cluster_offset &= s->cluster_offset_mask;
1750 585f8587 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1751 585f8587 bellard
            qemu_free(out_buf);
1752 585f8587 bellard
            return -1;
1753 585f8587 bellard
        }
1754 585f8587 bellard
    }
1755 3b46e624 ths
1756 585f8587 bellard
    qemu_free(out_buf);
1757 585f8587 bellard
    return 0;
1758 585f8587 bellard
}
1759 585f8587 bellard
1760 585f8587 bellard
static void qcow_flush(BlockDriverState *bs)
1761 585f8587 bellard
{
1762 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1763 585f8587 bellard
    bdrv_flush(s->hd);
1764 585f8587 bellard
}
1765 585f8587 bellard
1766 585f8587 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1767 585f8587 bellard
{
1768 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1769 585f8587 bellard
    bdi->cluster_size = s->cluster_size;
1770 5fafdf24 ths
    bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1771 585f8587 bellard
        (s->cluster_bits + s->l2_bits);
1772 585f8587 bellard
    return 0;
1773 585f8587 bellard
}
1774 585f8587 bellard
1775 585f8587 bellard
/*********************************************************/
1776 585f8587 bellard
/* snapshot support */
1777 585f8587 bellard
1778 585f8587 bellard
/* update the refcounts of snapshots and the copied flag */
1779 5fafdf24 ths
static int update_snapshot_refcount(BlockDriverState *bs,
1780 585f8587 bellard
                                    int64_t l1_table_offset,
1781 585f8587 bellard
                                    int l1_size,
1782 585f8587 bellard
                                    int addend)
1783 585f8587 bellard
{
1784 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1785 585f8587 bellard
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1786 585f8587 bellard
    int64_t old_offset, old_l2_offset;
1787 585f8587 bellard
    int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1788 3b46e624 ths
1789 585f8587 bellard
    l2_cache_reset(bs);
1790 585f8587 bellard
1791 585f8587 bellard
    l2_table = NULL;
1792 585f8587 bellard
    l1_table = NULL;
1793 585f8587 bellard
    l1_size2 = l1_size * sizeof(uint64_t);
1794 585f8587 bellard
    l1_allocated = 0;
1795 585f8587 bellard
    if (l1_table_offset != s->l1_table_offset) {
1796 585f8587 bellard
        l1_table = qemu_malloc(l1_size2);
1797 585f8587 bellard
        l1_allocated = 1;
1798 5fafdf24 ths
        if (bdrv_pread(s->hd, l1_table_offset,
1799 585f8587 bellard
                       l1_table, l1_size2) != l1_size2)
1800 585f8587 bellard
            goto fail;
1801 585f8587 bellard
        for(i = 0;i < l1_size; i++)
1802 585f8587 bellard
            be64_to_cpus(&l1_table[i]);
1803 585f8587 bellard
    } else {
1804 585f8587 bellard
        assert(l1_size == s->l1_size);
1805 585f8587 bellard
        l1_table = s->l1_table;
1806 585f8587 bellard
        l1_allocated = 0;
1807 585f8587 bellard
    }
1808 3b46e624 ths
1809 585f8587 bellard
    l2_size = s->l2_size * sizeof(uint64_t);
1810 585f8587 bellard
    l2_table = qemu_malloc(l2_size);
1811 585f8587 bellard
    l1_modified = 0;
1812 585f8587 bellard
    for(i = 0; i < l1_size; i++) {
1813 585f8587 bellard
        l2_offset = l1_table[i];
1814 585f8587 bellard
        if (l2_offset) {
1815 585f8587 bellard
            old_l2_offset = l2_offset;
1816 585f8587 bellard
            l2_offset &= ~QCOW_OFLAG_COPIED;
1817 585f8587 bellard
            l2_modified = 0;
1818 585f8587 bellard
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1819 585f8587 bellard
                goto fail;
1820 585f8587 bellard
            for(j = 0; j < s->l2_size; j++) {
1821 585f8587 bellard
                offset = be64_to_cpu(l2_table[j]);
1822 585f8587 bellard
                if (offset != 0) {
1823 585f8587 bellard
                    old_offset = offset;
1824 585f8587 bellard
                    offset &= ~QCOW_OFLAG_COPIED;
1825 585f8587 bellard
                    if (offset & QCOW_OFLAG_COMPRESSED) {
1826 5fafdf24 ths
                        nb_csectors = ((offset >> s->csize_shift) &
1827 585f8587 bellard
                                       s->csize_mask) + 1;
1828 585f8587 bellard
                        if (addend != 0)
1829 585f8587 bellard
                            update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1830 585f8587 bellard
                                            nb_csectors * 512, addend);
1831 585f8587 bellard
                        /* compressed clusters are never modified */
1832 5fafdf24 ths
                        refcount = 2;
1833 585f8587 bellard
                    } else {
1834 585f8587 bellard
                        if (addend != 0) {
1835 585f8587 bellard
                            refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1836 585f8587 bellard
                        } else {
1837 585f8587 bellard
                            refcount = get_refcount(bs, offset >> s->cluster_bits);
1838 585f8587 bellard
                        }
1839 585f8587 bellard
                    }
1840 585f8587 bellard
1841 585f8587 bellard
                    if (refcount == 1) {
1842 585f8587 bellard
                        offset |= QCOW_OFLAG_COPIED;
1843 585f8587 bellard
                    }
1844 585f8587 bellard
                    if (offset != old_offset) {
1845 585f8587 bellard
                        l2_table[j] = cpu_to_be64(offset);
1846 585f8587 bellard
                        l2_modified = 1;
1847 585f8587 bellard
                    }
1848 585f8587 bellard
                }
1849 585f8587 bellard
            }
1850 585f8587 bellard
            if (l2_modified) {
1851 5fafdf24 ths
                if (bdrv_pwrite(s->hd,
1852 585f8587 bellard
                                l2_offset, l2_table, l2_size) != l2_size)
1853 585f8587 bellard
                    goto fail;
1854 585f8587 bellard
            }
1855 585f8587 bellard
1856 585f8587 bellard
            if (addend != 0) {
1857 585f8587 bellard
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1858 585f8587 bellard
            } else {
1859 585f8587 bellard
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1860 585f8587 bellard
            }
1861 585f8587 bellard
            if (refcount == 1) {
1862 585f8587 bellard
                l2_offset |= QCOW_OFLAG_COPIED;
1863 585f8587 bellard
            }
1864 585f8587 bellard
            if (l2_offset != old_l2_offset) {
1865 585f8587 bellard
                l1_table[i] = l2_offset;
1866 585f8587 bellard
                l1_modified = 1;
1867 585f8587 bellard
            }
1868 585f8587 bellard
        }
1869 585f8587 bellard
    }
1870 585f8587 bellard
    if (l1_modified) {
1871 585f8587 bellard
        for(i = 0; i < l1_size; i++)
1872 585f8587 bellard
            cpu_to_be64s(&l1_table[i]);
1873 5fafdf24 ths
        if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1874 585f8587 bellard
                        l1_size2) != l1_size2)
1875 585f8587 bellard
            goto fail;
1876 585f8587 bellard
        for(i = 0; i < l1_size; i++)
1877 585f8587 bellard
            be64_to_cpus(&l1_table[i]);
1878 585f8587 bellard
    }
1879 585f8587 bellard
    if (l1_allocated)
1880 585f8587 bellard
        qemu_free(l1_table);
1881 585f8587 bellard
    qemu_free(l2_table);
1882 585f8587 bellard
    return 0;
1883 585f8587 bellard
 fail:
1884 585f8587 bellard
    if (l1_allocated)
1885 585f8587 bellard
        qemu_free(l1_table);
1886 585f8587 bellard
    qemu_free(l2_table);
1887 585f8587 bellard
    return -EIO;
1888 585f8587 bellard
}
1889 585f8587 bellard
1890 585f8587 bellard
static void qcow_free_snapshots(BlockDriverState *bs)
1891 585f8587 bellard
{
1892 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1893 585f8587 bellard
    int i;
1894 585f8587 bellard
1895 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1896 585f8587 bellard
        qemu_free(s->snapshots[i].name);
1897 585f8587 bellard
        qemu_free(s->snapshots[i].id_str);
1898 585f8587 bellard
    }
1899 585f8587 bellard
    qemu_free(s->snapshots);
1900 585f8587 bellard
    s->snapshots = NULL;
1901 585f8587 bellard
    s->nb_snapshots = 0;
1902 585f8587 bellard
}
1903 585f8587 bellard
1904 585f8587 bellard
static int qcow_read_snapshots(BlockDriverState *bs)
1905 585f8587 bellard
{
1906 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1907 585f8587 bellard
    QCowSnapshotHeader h;
1908 585f8587 bellard
    QCowSnapshot *sn;
1909 585f8587 bellard
    int i, id_str_size, name_size;
1910 585f8587 bellard
    int64_t offset;
1911 585f8587 bellard
    uint32_t extra_data_size;
1912 585f8587 bellard
1913 63c75dcd malc
    if (!s->nb_snapshots) {
1914 63c75dcd malc
        s->snapshots = NULL;
1915 63c75dcd malc
        s->snapshots_size = 0;
1916 63c75dcd malc
        return 0;
1917 63c75dcd malc
    }
1918 63c75dcd malc
1919 585f8587 bellard
    offset = s->snapshots_offset;
1920 585f8587 bellard
    s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1921 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1922 585f8587 bellard
        offset = align_offset(offset, 8);
1923 585f8587 bellard
        if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1924 585f8587 bellard
            goto fail;
1925 585f8587 bellard
        offset += sizeof(h);
1926 585f8587 bellard
        sn = s->snapshots + i;
1927 585f8587 bellard
        sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1928 585f8587 bellard
        sn->l1_size = be32_to_cpu(h.l1_size);
1929 585f8587 bellard
        sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1930 585f8587 bellard
        sn->date_sec = be32_to_cpu(h.date_sec);
1931 585f8587 bellard
        sn->date_nsec = be32_to_cpu(h.date_nsec);
1932 585f8587 bellard
        sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1933 585f8587 bellard
        extra_data_size = be32_to_cpu(h.extra_data_size);
1934 585f8587 bellard
1935 585f8587 bellard
        id_str_size = be16_to_cpu(h.id_str_size);
1936 585f8587 bellard
        name_size = be16_to_cpu(h.name_size);
1937 585f8587 bellard
1938 585f8587 bellard
        offset += extra_data_size;
1939 585f8587 bellard
1940 585f8587 bellard
        sn->id_str = qemu_malloc(id_str_size + 1);
1941 585f8587 bellard
        if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1942 585f8587 bellard
            goto fail;
1943 585f8587 bellard
        offset += id_str_size;
1944 585f8587 bellard
        sn->id_str[id_str_size] = '\0';
1945 585f8587 bellard
1946 585f8587 bellard
        sn->name = qemu_malloc(name_size + 1);
1947 585f8587 bellard
        if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1948 585f8587 bellard
            goto fail;
1949 585f8587 bellard
        offset += name_size;
1950 585f8587 bellard
        sn->name[name_size] = '\0';
1951 585f8587 bellard
    }
1952 585f8587 bellard
    s->snapshots_size = offset - s->snapshots_offset;
1953 585f8587 bellard
    return 0;
1954 585f8587 bellard
 fail:
1955 585f8587 bellard
    qcow_free_snapshots(bs);
1956 585f8587 bellard
    return -1;
1957 585f8587 bellard
}
1958 585f8587 bellard
1959 585f8587 bellard
/* add at the end of the file a new list of snapshots */
1960 585f8587 bellard
static int qcow_write_snapshots(BlockDriverState *bs)
1961 585f8587 bellard
{
1962 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1963 585f8587 bellard
    QCowSnapshot *sn;
1964 585f8587 bellard
    QCowSnapshotHeader h;
1965 585f8587 bellard
    int i, name_size, id_str_size, snapshots_size;
1966 585f8587 bellard
    uint64_t data64;
1967 585f8587 bellard
    uint32_t data32;
1968 585f8587 bellard
    int64_t offset, snapshots_offset;
1969 585f8587 bellard
1970 585f8587 bellard
    /* compute the size of the snapshots */
1971 585f8587 bellard
    offset = 0;
1972 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1973 585f8587 bellard
        sn = s->snapshots + i;
1974 585f8587 bellard
        offset = align_offset(offset, 8);
1975 585f8587 bellard
        offset += sizeof(h);
1976 585f8587 bellard
        offset += strlen(sn->id_str);
1977 585f8587 bellard
        offset += strlen(sn->name);
1978 585f8587 bellard
    }
1979 585f8587 bellard
    snapshots_size = offset;
1980 585f8587 bellard
1981 585f8587 bellard
    snapshots_offset = alloc_clusters(bs, snapshots_size);
1982 585f8587 bellard
    offset = snapshots_offset;
1983 3b46e624 ths
1984 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1985 585f8587 bellard
        sn = s->snapshots + i;
1986 585f8587 bellard
        memset(&h, 0, sizeof(h));
1987 585f8587 bellard
        h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1988 585f8587 bellard
        h.l1_size = cpu_to_be32(sn->l1_size);
1989 585f8587 bellard
        h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1990 585f8587 bellard
        h.date_sec = cpu_to_be32(sn->date_sec);
1991 585f8587 bellard
        h.date_nsec = cpu_to_be32(sn->date_nsec);
1992 585f8587 bellard
        h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1993 3b46e624 ths
1994 585f8587 bellard
        id_str_size = strlen(sn->id_str);
1995 585f8587 bellard
        name_size = strlen(sn->name);
1996 585f8587 bellard
        h.id_str_size = cpu_to_be16(id_str_size);
1997 585f8587 bellard
        h.name_size = cpu_to_be16(name_size);
1998 585f8587 bellard
        offset = align_offset(offset, 8);
1999 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
2000 585f8587 bellard
            goto fail;
2001 585f8587 bellard
        offset += sizeof(h);
2002 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
2003 585f8587 bellard
            goto fail;
2004 585f8587 bellard
        offset += id_str_size;
2005 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
2006 585f8587 bellard
            goto fail;
2007 585f8587 bellard
        offset += name_size;
2008 585f8587 bellard
    }
2009 585f8587 bellard
2010 585f8587 bellard
    /* update the various header fields */
2011 585f8587 bellard
    data64 = cpu_to_be64(snapshots_offset);
2012 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
2013 585f8587 bellard
                    &data64, sizeof(data64)) != sizeof(data64))
2014 585f8587 bellard
        goto fail;
2015 585f8587 bellard
    data32 = cpu_to_be32(s->nb_snapshots);
2016 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
2017 585f8587 bellard
                    &data32, sizeof(data32)) != sizeof(data32))
2018 585f8587 bellard
        goto fail;
2019 585f8587 bellard
2020 585f8587 bellard
    /* free the old snapshot table */
2021 585f8587 bellard
    free_clusters(bs, s->snapshots_offset, s->snapshots_size);
2022 585f8587 bellard
    s->snapshots_offset = snapshots_offset;
2023 585f8587 bellard
    s->snapshots_size = snapshots_size;
2024 585f8587 bellard
    return 0;
2025 585f8587 bellard
 fail:
2026 585f8587 bellard
    return -1;
2027 585f8587 bellard
}
2028 585f8587 bellard
2029 585f8587 bellard
static void find_new_snapshot_id(BlockDriverState *bs,
2030 585f8587 bellard
                                 char *id_str, int id_str_size)
2031 585f8587 bellard
{
2032 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2033 585f8587 bellard
    QCowSnapshot *sn;
2034 585f8587 bellard
    int i, id, id_max = 0;
2035 585f8587 bellard
2036 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2037 585f8587 bellard
        sn = s->snapshots + i;
2038 585f8587 bellard
        id = strtoul(sn->id_str, NULL, 10);
2039 585f8587 bellard
        if (id > id_max)
2040 585f8587 bellard
            id_max = id;
2041 585f8587 bellard
    }
2042 585f8587 bellard
    snprintf(id_str, id_str_size, "%d", id_max + 1);
2043 585f8587 bellard
}
2044 585f8587 bellard
2045 585f8587 bellard
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
2046 585f8587 bellard
{
2047 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2048 585f8587 bellard
    int i;
2049 585f8587 bellard
2050 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2051 585f8587 bellard
        if (!strcmp(s->snapshots[i].id_str, id_str))
2052 585f8587 bellard
            return i;
2053 585f8587 bellard
    }
2054 585f8587 bellard
    return -1;
2055 585f8587 bellard
}
2056 585f8587 bellard
2057 585f8587 bellard
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
2058 585f8587 bellard
{
2059 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2060 585f8587 bellard
    int i, ret;
2061 3b46e624 ths
2062 585f8587 bellard
    ret = find_snapshot_by_id(bs, name);
2063 585f8587 bellard
    if (ret >= 0)
2064 585f8587 bellard
        return ret;
2065 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2066 585f8587 bellard
        if (!strcmp(s->snapshots[i].name, name))
2067 585f8587 bellard
            return i;
2068 585f8587 bellard
    }
2069 585f8587 bellard
    return -1;
2070 585f8587 bellard
}
2071 585f8587 bellard
2072 585f8587 bellard
/* if no id is provided, a new one is constructed */
2073 5fafdf24 ths
static int qcow_snapshot_create(BlockDriverState *bs,
2074 585f8587 bellard
                                QEMUSnapshotInfo *sn_info)
2075 585f8587 bellard
{
2076 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2077 585f8587 bellard
    QCowSnapshot *snapshots1, sn1, *sn = &sn1;
2078 585f8587 bellard
    int i, ret;
2079 585f8587 bellard
    uint64_t *l1_table = NULL;
2080 3b46e624 ths
2081 585f8587 bellard
    memset(sn, 0, sizeof(*sn));
2082 585f8587 bellard
2083 585f8587 bellard
    if (sn_info->id_str[0] == '\0') {
2084 585f8587 bellard
        /* compute a new id */
2085 585f8587 bellard
        find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
2086 585f8587 bellard
    }
2087 585f8587 bellard
2088 585f8587 bellard
    /* check that the ID is unique */
2089 585f8587 bellard
    if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
2090 585f8587 bellard
        return -ENOENT;
2091 585f8587 bellard
2092 585f8587 bellard
    sn->id_str = qemu_strdup(sn_info->id_str);
2093 585f8587 bellard
    if (!sn->id_str)
2094 585f8587 bellard
        goto fail;
2095 585f8587 bellard
    sn->name = qemu_strdup(sn_info->name);
2096 585f8587 bellard
    if (!sn->name)
2097 585f8587 bellard
        goto fail;
2098 585f8587 bellard
    sn->vm_state_size = sn_info->vm_state_size;
2099 585f8587 bellard
    sn->date_sec = sn_info->date_sec;
2100 585f8587 bellard
    sn->date_nsec = sn_info->date_nsec;
2101 585f8587 bellard
    sn->vm_clock_nsec = sn_info->vm_clock_nsec;
2102 585f8587 bellard
2103 585f8587 bellard
    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
2104 585f8587 bellard
    if (ret < 0)
2105 585f8587 bellard
        goto fail;
2106 585f8587 bellard
2107 585f8587 bellard
    /* create the L1 table of the snapshot */
2108 585f8587 bellard
    sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
2109 585f8587 bellard
    sn->l1_size = s->l1_size;
2110 585f8587 bellard
2111 585f8587 bellard
    l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
2112 585f8587 bellard
    for(i = 0; i < s->l1_size; i++) {
2113 585f8587 bellard
        l1_table[i] = cpu_to_be64(s->l1_table[i]);
2114 585f8587 bellard
    }
2115 585f8587 bellard
    if (bdrv_pwrite(s->hd, sn->l1_table_offset,
2116 5fafdf24 ths
                    l1_table, s->l1_size * sizeof(uint64_t)) !=
2117 585f8587 bellard
        (s->l1_size * sizeof(uint64_t)))
2118 585f8587 bellard
        goto fail;
2119 585f8587 bellard
    qemu_free(l1_table);
2120 585f8587 bellard
    l1_table = NULL;
2121 585f8587 bellard
2122 585f8587 bellard
    snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
2123 63c75dcd malc
    if (s->snapshots) {
2124 63c75dcd malc
        memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
2125 63c75dcd malc
        qemu_free(s->snapshots);
2126 63c75dcd malc
    }
2127 585f8587 bellard
    s->snapshots = snapshots1;
2128 585f8587 bellard
    s->snapshots[s->nb_snapshots++] = *sn;
2129 585f8587 bellard
2130 585f8587 bellard
    if (qcow_write_snapshots(bs) < 0)
2131 585f8587 bellard
        goto fail;
2132 585f8587 bellard
#ifdef DEBUG_ALLOC
2133 585f8587 bellard
    check_refcounts(bs);
2134 585f8587 bellard
#endif
2135 585f8587 bellard
    return 0;
2136 585f8587 bellard
 fail:
2137 585f8587 bellard
    qemu_free(sn->name);
2138 585f8587 bellard
    qemu_free(l1_table);
2139 585f8587 bellard
    return -1;
2140 585f8587 bellard
}
2141 585f8587 bellard
2142 585f8587 bellard
/* copy the snapshot 'snapshot_name' into the current disk image */
2143 5fafdf24 ths
static int qcow_snapshot_goto(BlockDriverState *bs,
2144 585f8587 bellard
                              const char *snapshot_id)
2145 585f8587 bellard
{
2146 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2147 585f8587 bellard
    QCowSnapshot *sn;
2148 585f8587 bellard
    int i, snapshot_index, l1_size2;
2149 585f8587 bellard
2150 585f8587 bellard
    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2151 585f8587 bellard
    if (snapshot_index < 0)
2152 585f8587 bellard
        return -ENOENT;
2153 585f8587 bellard
    sn = &s->snapshots[snapshot_index];
2154 585f8587 bellard
2155 585f8587 bellard
    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
2156 585f8587 bellard
        goto fail;
2157 585f8587 bellard
2158 585f8587 bellard
    if (grow_l1_table(bs, sn->l1_size) < 0)
2159 585f8587 bellard
        goto fail;
2160 585f8587 bellard
2161 585f8587 bellard
    s->l1_size = sn->l1_size;
2162 585f8587 bellard
    l1_size2 = s->l1_size * sizeof(uint64_t);
2163 585f8587 bellard
    /* copy the snapshot l1 table to the current l1 table */
2164 5fafdf24 ths
    if (bdrv_pread(s->hd, sn->l1_table_offset,
2165 585f8587 bellard
                   s->l1_table, l1_size2) != l1_size2)
2166 585f8587 bellard
        goto fail;
2167 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset,
2168 585f8587 bellard
                    s->l1_table, l1_size2) != l1_size2)
2169 585f8587 bellard
        goto fail;
2170 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
2171 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
2172 585f8587 bellard
    }
2173 585f8587 bellard
2174 585f8587 bellard
    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2175 585f8587 bellard
        goto fail;
2176 585f8587 bellard
2177 585f8587 bellard
#ifdef DEBUG_ALLOC
2178 585f8587 bellard
    check_refcounts(bs);
2179 585f8587 bellard
#endif
2180 585f8587 bellard
    return 0;
2181 585f8587 bellard
 fail:
2182 585f8587 bellard
    return -EIO;
2183 585f8587 bellard
}
2184 585f8587 bellard
2185 585f8587 bellard
static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2186 585f8587 bellard
{
2187 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2188 585f8587 bellard
    QCowSnapshot *sn;
2189 585f8587 bellard
    int snapshot_index, ret;
2190 3b46e624 ths
2191 585f8587 bellard
    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2192 585f8587 bellard
    if (snapshot_index < 0)
2193 585f8587 bellard
        return -ENOENT;
2194 585f8587 bellard
    sn = &s->snapshots[snapshot_index];
2195 585f8587 bellard
2196 585f8587 bellard
    ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2197 585f8587 bellard
    if (ret < 0)
2198 585f8587 bellard
        return ret;
2199 585f8587 bellard
    /* must update the copied flag on the current cluster offsets */
2200 585f8587 bellard
    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2201 585f8587 bellard
    if (ret < 0)
2202 585f8587 bellard
        return ret;
2203 585f8587 bellard
    free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2204 585f8587 bellard
2205 585f8587 bellard
    qemu_free(sn->id_str);
2206 585f8587 bellard
    qemu_free(sn->name);
2207 585f8587 bellard
    memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2208 585f8587 bellard
    s->nb_snapshots--;
2209 585f8587 bellard
    ret = qcow_write_snapshots(bs);
2210 585f8587 bellard
    if (ret < 0) {
2211 585f8587 bellard
        /* XXX: restore snapshot if error ? */
2212 585f8587 bellard
        return ret;
2213 585f8587 bellard
    }
2214 585f8587 bellard
#ifdef DEBUG_ALLOC
2215 585f8587 bellard
    check_refcounts(bs);
2216 585f8587 bellard
#endif
2217 585f8587 bellard
    return 0;
2218 585f8587 bellard
}
2219 585f8587 bellard
2220 5fafdf24 ths
static int qcow_snapshot_list(BlockDriverState *bs,
2221 585f8587 bellard
                              QEMUSnapshotInfo **psn_tab)
2222 585f8587 bellard
{
2223 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2224 585f8587 bellard
    QEMUSnapshotInfo *sn_tab, *sn_info;
2225 585f8587 bellard
    QCowSnapshot *sn;
2226 585f8587 bellard
    int i;
2227 585f8587 bellard
2228 585f8587 bellard
    sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2229 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2230 585f8587 bellard
        sn_info = sn_tab + i;
2231 585f8587 bellard
        sn = s->snapshots + i;
2232 585f8587 bellard
        pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2233 585f8587 bellard
                sn->id_str);
2234 585f8587 bellard
        pstrcpy(sn_info->name, sizeof(sn_info->name),
2235 585f8587 bellard
                sn->name);
2236 585f8587 bellard
        sn_info->vm_state_size = sn->vm_state_size;
2237 585f8587 bellard
        sn_info->date_sec = sn->date_sec;
2238 585f8587 bellard
        sn_info->date_nsec = sn->date_nsec;
2239 585f8587 bellard
        sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2240 585f8587 bellard
    }
2241 585f8587 bellard
    *psn_tab = sn_tab;
2242 585f8587 bellard
    return s->nb_snapshots;
2243 585f8587 bellard
}
2244 585f8587 bellard
2245 585f8587 bellard
/*********************************************************/
2246 585f8587 bellard
/* refcount handling */
2247 585f8587 bellard
2248 585f8587 bellard
static int refcount_init(BlockDriverState *bs)
2249 585f8587 bellard
{
2250 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2251 585f8587 bellard
    int ret, refcount_table_size2, i;
2252 3b46e624 ths
2253 585f8587 bellard
    s->refcount_block_cache = qemu_malloc(s->cluster_size);
2254 585f8587 bellard
    refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2255 585f8587 bellard
    s->refcount_table = qemu_malloc(refcount_table_size2);
2256 585f8587 bellard
    if (s->refcount_table_size > 0) {
2257 585f8587 bellard
        ret = bdrv_pread(s->hd, s->refcount_table_offset,
2258 585f8587 bellard
                         s->refcount_table, refcount_table_size2);
2259 585f8587 bellard
        if (ret != refcount_table_size2)
2260 585f8587 bellard
            goto fail;
2261 585f8587 bellard
        for(i = 0; i < s->refcount_table_size; i++)
2262 585f8587 bellard
            be64_to_cpus(&s->refcount_table[i]);
2263 585f8587 bellard
    }
2264 585f8587 bellard
    return 0;
2265 585f8587 bellard
 fail:
2266 585f8587 bellard
    return -ENOMEM;
2267 585f8587 bellard
}
2268 585f8587 bellard
2269 585f8587 bellard
static void refcount_close(BlockDriverState *bs)
2270 585f8587 bellard
{
2271 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2272 585f8587 bellard
    qemu_free(s->refcount_block_cache);
2273 585f8587 bellard
    qemu_free(s->refcount_table);
2274 585f8587 bellard
}
2275 585f8587 bellard
2276 585f8587 bellard
2277 5fafdf24 ths
static int load_refcount_block(BlockDriverState *bs,
2278 585f8587 bellard
                               int64_t refcount_block_offset)
2279 585f8587 bellard
{
2280 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2281 585f8587 bellard
    int ret;
2282 5fafdf24 ths
    ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
2283 585f8587 bellard
                     s->cluster_size);
2284 585f8587 bellard
    if (ret != s->cluster_size)
2285 585f8587 bellard
        return -EIO;
2286 585f8587 bellard
    s->refcount_block_cache_offset = refcount_block_offset;
2287 585f8587 bellard
    return 0;
2288 585f8587 bellard
}
2289 585f8587 bellard
2290 585f8587 bellard
static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2291 585f8587 bellard
{
2292 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2293 585f8587 bellard
    int refcount_table_index, block_index;
2294 585f8587 bellard
    int64_t refcount_block_offset;
2295 585f8587 bellard
2296 585f8587 bellard
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2297 585f8587 bellard
    if (refcount_table_index >= s->refcount_table_size)
2298 585f8587 bellard
        return 0;
2299 585f8587 bellard
    refcount_block_offset = s->refcount_table[refcount_table_index];
2300 585f8587 bellard
    if (!refcount_block_offset)
2301 585f8587 bellard
        return 0;
2302 585f8587 bellard
    if (refcount_block_offset != s->refcount_block_cache_offset) {
2303 585f8587 bellard
        /* better than nothing: return allocated if read error */
2304 585f8587 bellard
        if (load_refcount_block(bs, refcount_block_offset) < 0)
2305 585f8587 bellard
            return 1;
2306 585f8587 bellard
    }
2307 5fafdf24 ths
    block_index = cluster_index &
2308 585f8587 bellard
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2309 585f8587 bellard
    return be16_to_cpu(s->refcount_block_cache[block_index]);
2310 585f8587 bellard
}
2311 585f8587 bellard
2312 585f8587 bellard
/* return < 0 if error */
2313 585f8587 bellard
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2314 585f8587 bellard
{
2315 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2316 585f8587 bellard
    int i, nb_clusters;
2317 585f8587 bellard
2318 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
2319 6db6c638 aliguori
retry:
2320 6db6c638 aliguori
    for(i = 0; i < nb_clusters; i++) {
2321 6db6c638 aliguori
        int64_t i = s->free_cluster_index++;
2322 6db6c638 aliguori
        if (get_refcount(bs, i) != 0)
2323 6db6c638 aliguori
            goto retry;
2324 6db6c638 aliguori
    }
2325 585f8587 bellard
#ifdef DEBUG_ALLOC2
2326 6db6c638 aliguori
    printf("alloc_clusters: size=%lld -> %lld\n",
2327 6db6c638 aliguori
            size,
2328 6db6c638 aliguori
            (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2329 585f8587 bellard
#endif
2330 6db6c638 aliguori
    return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2331 585f8587 bellard
}
2332 585f8587 bellard
2333 585f8587 bellard
static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2334 585f8587 bellard
{
2335 585f8587 bellard
    int64_t offset;
2336 585f8587 bellard
2337 585f8587 bellard
    offset = alloc_clusters_noref(bs, size);
2338 585f8587 bellard
    update_refcount(bs, offset, size, 1);
2339 585f8587 bellard
    return offset;
2340 585f8587 bellard
}
2341 585f8587 bellard
2342 585f8587 bellard
/* only used to allocate compressed sectors. We try to allocate
2343 585f8587 bellard
   contiguous sectors. size must be <= cluster_size */
2344 585f8587 bellard
static int64_t alloc_bytes(BlockDriverState *bs, int size)
2345 585f8587 bellard
{
2346 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2347 585f8587 bellard
    int64_t offset, cluster_offset;
2348 585f8587 bellard
    int free_in_cluster;
2349 3b46e624 ths
2350 585f8587 bellard
    assert(size > 0 && size <= s->cluster_size);
2351 585f8587 bellard
    if (s->free_byte_offset == 0) {
2352 585f8587 bellard
        s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2353 585f8587 bellard
    }
2354 585f8587 bellard
 redo:
2355 5fafdf24 ths
    free_in_cluster = s->cluster_size -
2356 585f8587 bellard
        (s->free_byte_offset & (s->cluster_size - 1));
2357 585f8587 bellard
    if (size <= free_in_cluster) {
2358 585f8587 bellard
        /* enough space in current cluster */
2359 585f8587 bellard
        offset = s->free_byte_offset;
2360 585f8587 bellard
        s->free_byte_offset += size;
2361 585f8587 bellard
        free_in_cluster -= size;
2362 585f8587 bellard
        if (free_in_cluster == 0)
2363 585f8587 bellard
            s->free_byte_offset = 0;
2364 585f8587 bellard
        if ((offset & (s->cluster_size - 1)) != 0)
2365 585f8587 bellard
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2366 585f8587 bellard
    } else {
2367 585f8587 bellard
        offset = alloc_clusters(bs, s->cluster_size);
2368 585f8587 bellard
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2369 585f8587 bellard
        if ((cluster_offset + s->cluster_size) == offset) {
2370 585f8587 bellard
            /* we are lucky: contiguous data */
2371 585f8587 bellard
            offset = s->free_byte_offset;
2372 585f8587 bellard
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2373 585f8587 bellard
            s->free_byte_offset += size;
2374 585f8587 bellard
        } else {
2375 585f8587 bellard
            s->free_byte_offset = offset;
2376 585f8587 bellard
            goto redo;
2377 585f8587 bellard
        }
2378 585f8587 bellard
    }
2379 585f8587 bellard
    return offset;
2380 585f8587 bellard
}
2381 585f8587 bellard
2382 5fafdf24 ths
static void free_clusters(BlockDriverState *bs,
2383 585f8587 bellard
                          int64_t offset, int64_t size)
2384 585f8587 bellard
{
2385 585f8587 bellard
    update_refcount(bs, offset, size, -1);
2386 585f8587 bellard
}
2387 585f8587 bellard
2388 585f8587 bellard
static int grow_refcount_table(BlockDriverState *bs, int min_size)
2389 585f8587 bellard
{
2390 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2391 585f8587 bellard
    int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2392 585f8587 bellard
    uint64_t *new_table;
2393 585f8587 bellard
    int64_t table_offset;
2394 643e5399 aliguori
    uint8_t data[12];
2395 23be50f1 ths
    int old_table_size;
2396 23be50f1 ths
    int64_t old_table_offset;
2397 585f8587 bellard
2398 585f8587 bellard
    if (min_size <= s->refcount_table_size)
2399 585f8587 bellard
        return 0;
2400 585f8587 bellard
    /* compute new table size */
2401 585f8587 bellard
    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2402 585f8587 bellard
    for(;;) {
2403 585f8587 bellard
        if (refcount_table_clusters == 0) {
2404 585f8587 bellard
            refcount_table_clusters = 1;
2405 585f8587 bellard
        } else {
2406 585f8587 bellard
            refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2407 585f8587 bellard
        }
2408 585f8587 bellard
        new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2409 585f8587 bellard
        if (min_size <= new_table_size)
2410 585f8587 bellard
            break;
2411 585f8587 bellard
    }
2412 15e6690a bellard
#ifdef DEBUG_ALLOC2
2413 15e6690a bellard
    printf("grow_refcount_table from %d to %d\n",
2414 15e6690a bellard
           s->refcount_table_size,
2415 15e6690a bellard
           new_table_size);
2416 15e6690a bellard
#endif
2417 585f8587 bellard
    new_table_size2 = new_table_size * sizeof(uint64_t);
2418 585f8587 bellard
    new_table = qemu_mallocz(new_table_size2);
2419 5fafdf24 ths
    memcpy(new_table, s->refcount_table,
2420 585f8587 bellard
           s->refcount_table_size * sizeof(uint64_t));
2421 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++)
2422 585f8587 bellard
        cpu_to_be64s(&new_table[i]);
2423 585f8587 bellard
    /* Note: we cannot update the refcount now to avoid recursion */
2424 585f8587 bellard
    table_offset = alloc_clusters_noref(bs, new_table_size2);
2425 585f8587 bellard
    ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
2426 5fafdf24 ths
    if (ret != new_table_size2)
2427 585f8587 bellard
        goto fail;
2428 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++)
2429 585f8587 bellard
        be64_to_cpus(&new_table[i]);
2430 585f8587 bellard
2431 643e5399 aliguori
    cpu_to_be64w((uint64_t*)data, table_offset);
2432 643e5399 aliguori
    cpu_to_be32w((uint32_t*)(data + 8), refcount_table_clusters);
2433 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2434 643e5399 aliguori
                    data, sizeof(data)) != sizeof(data))
2435 585f8587 bellard
        goto fail;
2436 585f8587 bellard
    qemu_free(s->refcount_table);
2437 23be50f1 ths
    old_table_offset = s->refcount_table_offset;
2438 23be50f1 ths
    old_table_size = s->refcount_table_size;
2439 585f8587 bellard
    s->refcount_table = new_table;
2440 585f8587 bellard
    s->refcount_table_size = new_table_size;
2441 a4080ece ths
    s->refcount_table_offset = table_offset;
2442 585f8587 bellard
2443 585f8587 bellard
    update_refcount(bs, table_offset, new_table_size2, 1);
2444 23be50f1 ths
    free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
2445 585f8587 bellard
    return 0;
2446 585f8587 bellard
 fail:
2447 585f8587 bellard
    free_clusters(bs, table_offset, new_table_size2);
2448 585f8587 bellard
    qemu_free(new_table);
2449 585f8587 bellard
    return -EIO;
2450 585f8587 bellard
}
2451 585f8587 bellard
2452 585f8587 bellard
/* addend must be 1 or -1 */
2453 585f8587 bellard
/* XXX: cache several refcount block clusters ? */
2454 5fafdf24 ths
static int update_cluster_refcount(BlockDriverState *bs,
2455 585f8587 bellard
                                   int64_t cluster_index,
2456 585f8587 bellard
                                   int addend)
2457 585f8587 bellard
{
2458 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2459 585f8587 bellard
    int64_t offset, refcount_block_offset;
2460 585f8587 bellard
    int ret, refcount_table_index, block_index, refcount;
2461 585f8587 bellard
    uint64_t data64;
2462 585f8587 bellard
2463 585f8587 bellard
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2464 585f8587 bellard
    if (refcount_table_index >= s->refcount_table_size) {
2465 585f8587 bellard
        if (addend < 0)
2466 585f8587 bellard
            return -EINVAL;
2467 585f8587 bellard
        ret = grow_refcount_table(bs, refcount_table_index + 1);
2468 585f8587 bellard
        if (ret < 0)
2469 585f8587 bellard
            return ret;
2470 585f8587 bellard
    }
2471 585f8587 bellard
    refcount_block_offset = s->refcount_table[refcount_table_index];
2472 585f8587 bellard
    if (!refcount_block_offset) {
2473 585f8587 bellard
        if (addend < 0)
2474 585f8587 bellard
            return -EINVAL;
2475 585f8587 bellard
        /* create a new refcount block */
2476 585f8587 bellard
        /* Note: we cannot update the refcount now to avoid recursion */
2477 585f8587 bellard
        offset = alloc_clusters_noref(bs, s->cluster_size);
2478 585f8587 bellard
        memset(s->refcount_block_cache, 0, s->cluster_size);
2479 585f8587 bellard
        ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2480 585f8587 bellard
        if (ret != s->cluster_size)
2481 585f8587 bellard
            return -EINVAL;
2482 585f8587 bellard
        s->refcount_table[refcount_table_index] = offset;
2483 585f8587 bellard
        data64 = cpu_to_be64(offset);
2484 5fafdf24 ths
        ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2485 5fafdf24 ths
                          refcount_table_index * sizeof(uint64_t),
2486 585f8587 bellard
                          &data64, sizeof(data64));
2487 585f8587 bellard
        if (ret != sizeof(data64))
2488 585f8587 bellard
            return -EINVAL;
2489 585f8587 bellard
2490 585f8587 bellard
        refcount_block_offset = offset;
2491 585f8587 bellard
        s->refcount_block_cache_offset = offset;
2492 585f8587 bellard
        update_refcount(bs, offset, s->cluster_size, 1);
2493 585f8587 bellard
    } else {
2494 585f8587 bellard
        if (refcount_block_offset != s->refcount_block_cache_offset) {
2495 585f8587 bellard
            if (load_refcount_block(bs, refcount_block_offset) < 0)
2496 585f8587 bellard
                return -EIO;
2497 585f8587 bellard
        }
2498 585f8587 bellard
    }
2499 585f8587 bellard
    /* we can update the count and save it */
2500 5fafdf24 ths
    block_index = cluster_index &
2501 585f8587 bellard
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2502 585f8587 bellard
    refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2503 585f8587 bellard
    refcount += addend;
2504 585f8587 bellard
    if (refcount < 0 || refcount > 0xffff)
2505 585f8587 bellard
        return -EINVAL;
2506 585f8587 bellard
    if (refcount == 0 && cluster_index < s->free_cluster_index) {
2507 585f8587 bellard
        s->free_cluster_index = cluster_index;
2508 585f8587 bellard
    }
2509 585f8587 bellard
    s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2510 5fafdf24 ths
    if (bdrv_pwrite(s->hd,
2511 5fafdf24 ths
                    refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2512 585f8587 bellard
                    &s->refcount_block_cache[block_index], 2) != 2)
2513 585f8587 bellard
        return -EIO;
2514 585f8587 bellard
    return refcount;
2515 585f8587 bellard
}
2516 585f8587 bellard
2517 5fafdf24 ths
static void update_refcount(BlockDriverState *bs,
2518 5fafdf24 ths
                            int64_t offset, int64_t length,
2519 585f8587 bellard
                            int addend)
2520 585f8587 bellard
{
2521 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2522 585f8587 bellard
    int64_t start, last, cluster_offset;
2523 585f8587 bellard
2524 585f8587 bellard
#ifdef DEBUG_ALLOC2
2525 5fafdf24 ths
    printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2526 585f8587 bellard
           offset, length, addend);
2527 585f8587 bellard
#endif
2528 585f8587 bellard
    if (length <= 0)
2529 585f8587 bellard
        return;
2530 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
2531 585f8587 bellard
    last = (offset + length - 1) & ~(s->cluster_size - 1);
2532 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
2533 585f8587 bellard
        cluster_offset += s->cluster_size) {
2534 585f8587 bellard
        update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2535 585f8587 bellard
    }
2536 585f8587 bellard
}
2537 585f8587 bellard
2538 585f8587 bellard
#ifdef DEBUG_ALLOC
2539 5fafdf24 ths
static void inc_refcounts(BlockDriverState *bs,
2540 5fafdf24 ths
                          uint16_t *refcount_table,
2541 585f8587 bellard
                          int refcount_table_size,
2542 585f8587 bellard
                          int64_t offset, int64_t size)
2543 585f8587 bellard
{
2544 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2545 585f8587 bellard
    int64_t start, last, cluster_offset;
2546 585f8587 bellard
    int k;
2547 3b46e624 ths
2548 585f8587 bellard
    if (size <= 0)
2549 585f8587 bellard
        return;
2550 585f8587 bellard
2551 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
2552 585f8587 bellard
    last = (offset + size - 1) & ~(s->cluster_size - 1);
2553 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
2554 585f8587 bellard
        cluster_offset += s->cluster_size) {
2555 585f8587 bellard
        k = cluster_offset >> s->cluster_bits;
2556 585f8587 bellard
        if (k < 0 || k >= refcount_table_size) {
2557 585f8587 bellard
            printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2558 585f8587 bellard
        } else {
2559 585f8587 bellard
            if (++refcount_table[k] == 0) {
2560 585f8587 bellard
                printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2561 585f8587 bellard
            }
2562 585f8587 bellard
        }
2563 585f8587 bellard
    }
2564 585f8587 bellard
}
2565 585f8587 bellard
2566 5fafdf24 ths
static int check_refcounts_l1(BlockDriverState *bs,
2567 5fafdf24 ths
                              uint16_t *refcount_table,
2568 585f8587 bellard
                              int refcount_table_size,
2569 585f8587 bellard
                              int64_t l1_table_offset, int l1_size,
2570 585f8587 bellard
                              int check_copied)
2571 585f8587 bellard
{
2572 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2573 585f8587 bellard
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2574 585f8587 bellard
    int l2_size, i, j, nb_csectors, refcount;
2575 585f8587 bellard
2576 585f8587 bellard
    l2_table = NULL;
2577 585f8587 bellard
    l1_size2 = l1_size * sizeof(uint64_t);
2578 585f8587 bellard
2579 585f8587 bellard
    inc_refcounts(bs, refcount_table, refcount_table_size,
2580 585f8587 bellard
                  l1_table_offset, l1_size2);
2581 585f8587 bellard
2582 585f8587 bellard
    l1_table = qemu_malloc(l1_size2);
2583 5fafdf24 ths
    if (bdrv_pread(s->hd, l1_table_offset,
2584 585f8587 bellard
                   l1_table, l1_size2) != l1_size2)
2585 585f8587 bellard
        goto fail;
2586 585f8587 bellard
    for(i = 0;i < l1_size; i++)
2587 585f8587 bellard
        be64_to_cpus(&l1_table[i]);
2588 3b46e624 ths
2589 585f8587 bellard
    l2_size = s->l2_size * sizeof(uint64_t);
2590 585f8587 bellard
    l2_table = qemu_malloc(l2_size);
2591 585f8587 bellard
    for(i = 0; i < l1_size; i++) {
2592 585f8587 bellard
        l2_offset = l1_table[i];
2593 585f8587 bellard
        if (l2_offset) {
2594 585f8587 bellard
            if (check_copied) {
2595 585f8587 bellard
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2596 585f8587 bellard
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2597 585f8587 bellard
                    printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2598 585f8587 bellard
                           l2_offset, refcount);
2599 585f8587 bellard
                }
2600 585f8587 bellard
            }
2601 585f8587 bellard
            l2_offset &= ~QCOW_OFLAG_COPIED;
2602 585f8587 bellard
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2603 585f8587 bellard
                goto fail;
2604 585f8587 bellard
            for(j = 0; j < s->l2_size; j++) {
2605 585f8587 bellard
                offset = be64_to_cpu(l2_table[j]);
2606 585f8587 bellard
                if (offset != 0) {
2607 585f8587 bellard
                    if (offset & QCOW_OFLAG_COMPRESSED) {
2608 585f8587 bellard
                        if (offset & QCOW_OFLAG_COPIED) {
2609 585f8587 bellard
                            printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2610 585f8587 bellard
                                   offset >> s->cluster_bits);
2611 585f8587 bellard
                            offset &= ~QCOW_OFLAG_COPIED;
2612 585f8587 bellard
                        }
2613 5fafdf24 ths
                        nb_csectors = ((offset >> s->csize_shift) &
2614 585f8587 bellard
                                       s->csize_mask) + 1;
2615 585f8587 bellard
                        offset &= s->cluster_offset_mask;
2616 5fafdf24 ths
                        inc_refcounts(bs, refcount_table,
2617 585f8587 bellard
                                      refcount_table_size,
2618 585f8587 bellard
                                      offset & ~511, nb_csectors * 512);
2619 585f8587 bellard
                    } else {
2620 585f8587 bellard
                        if (check_copied) {
2621 585f8587 bellard
                            refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2622 585f8587 bellard
                            if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2623 585f8587 bellard
                                printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2624 585f8587 bellard
                                       offset, refcount);
2625 585f8587 bellard
                            }
2626 585f8587 bellard
                        }
2627 585f8587 bellard
                        offset &= ~QCOW_OFLAG_COPIED;
2628 5fafdf24 ths
                        inc_refcounts(bs, refcount_table,
2629 585f8587 bellard
                                      refcount_table_size,
2630 585f8587 bellard
                                      offset, s->cluster_size);
2631 585f8587 bellard
                    }
2632 585f8587 bellard
                }
2633 585f8587 bellard
            }
2634 5fafdf24 ths
            inc_refcounts(bs, refcount_table,
2635 585f8587 bellard
                          refcount_table_size,
2636 585f8587 bellard
                          l2_offset,
2637 585f8587 bellard
                          s->cluster_size);
2638 585f8587 bellard
        }
2639 585f8587 bellard
    }
2640 585f8587 bellard
    qemu_free(l1_table);
2641 585f8587 bellard
    qemu_free(l2_table);
2642 585f8587 bellard
    return 0;
2643 585f8587 bellard
 fail:
2644 585f8587 bellard
    printf("ERROR: I/O error in check_refcounts_l1\n");
2645 585f8587 bellard
    qemu_free(l1_table);
2646 585f8587 bellard
    qemu_free(l2_table);
2647 585f8587 bellard
    return -EIO;
2648 585f8587 bellard
}
2649 585f8587 bellard
2650 585f8587 bellard
static void check_refcounts(BlockDriverState *bs)
2651 585f8587 bellard
{
2652 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2653 585f8587 bellard
    int64_t size;
2654 585f8587 bellard
    int nb_clusters, refcount1, refcount2, i;
2655 585f8587 bellard
    QCowSnapshot *sn;
2656 585f8587 bellard
    uint16_t *refcount_table;
2657 585f8587 bellard
2658 585f8587 bellard
    size = bdrv_getlength(s->hd);
2659 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
2660 585f8587 bellard
    refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2661 15e6690a bellard
2662 585f8587 bellard
    /* header */
2663 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2664 585f8587 bellard
                  0, s->cluster_size);
2665 3b46e624 ths
2666 585f8587 bellard
    check_refcounts_l1(bs, refcount_table, nb_clusters,
2667 585f8587 bellard
                       s->l1_table_offset, s->l1_size, 1);
2668 585f8587 bellard
2669 585f8587 bellard
    /* snapshots */
2670 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2671 585f8587 bellard
        sn = s->snapshots + i;
2672 585f8587 bellard
        check_refcounts_l1(bs, refcount_table, nb_clusters,
2673 585f8587 bellard
                           sn->l1_table_offset, sn->l1_size, 0);
2674 585f8587 bellard
    }
2675 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2676 585f8587 bellard
                  s->snapshots_offset, s->snapshots_size);
2677 585f8587 bellard
2678 585f8587 bellard
    /* refcount data */
2679 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2680 5fafdf24 ths
                  s->refcount_table_offset,
2681 585f8587 bellard
                  s->refcount_table_size * sizeof(uint64_t));
2682 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++) {
2683 585f8587 bellard
        int64_t offset;
2684 585f8587 bellard
        offset = s->refcount_table[i];
2685 585f8587 bellard
        if (offset != 0) {
2686 585f8587 bellard
            inc_refcounts(bs, refcount_table, nb_clusters,
2687 585f8587 bellard
                          offset, s->cluster_size);
2688 585f8587 bellard
        }
2689 585f8587 bellard
    }
2690 585f8587 bellard
2691 585f8587 bellard
    /* compare ref counts */
2692 585f8587 bellard
    for(i = 0; i < nb_clusters; i++) {
2693 585f8587 bellard
        refcount1 = get_refcount(bs, i);
2694 585f8587 bellard
        refcount2 = refcount_table[i];
2695 585f8587 bellard
        if (refcount1 != refcount2)
2696 585f8587 bellard
            printf("ERROR cluster %d refcount=%d reference=%d\n",
2697 585f8587 bellard
                   i, refcount1, refcount2);
2698 585f8587 bellard
    }
2699 585f8587 bellard
2700 585f8587 bellard
    qemu_free(refcount_table);
2701 585f8587 bellard
}
2702 585f8587 bellard
2703 585f8587 bellard
#if 0
2704 585f8587 bellard
static void dump_refcounts(BlockDriverState *bs)
2705 585f8587 bellard
{
2706 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2707 585f8587 bellard
    int64_t nb_clusters, k, k1, size;
2708 585f8587 bellard
    int refcount;
2709 585f8587 bellard

2710 585f8587 bellard
    size = bdrv_getlength(s->hd);
2711 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
2712 585f8587 bellard
    for(k = 0; k < nb_clusters;) {
2713 585f8587 bellard
        k1 = k;
2714 585f8587 bellard
        refcount = get_refcount(bs, k);
2715 585f8587 bellard
        k++;
2716 585f8587 bellard
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
2717 585f8587 bellard
            k++;
2718 585f8587 bellard
        printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2719 585f8587 bellard
    }
2720 585f8587 bellard
}
2721 585f8587 bellard
#endif
2722 585f8587 bellard
#endif
2723 585f8587 bellard
2724 178e08a5 aliguori
static int qcow_put_buffer(BlockDriverState *bs, const uint8_t *buf,
2725 178e08a5 aliguori
                           int64_t pos, int size)
2726 178e08a5 aliguori
{
2727 178e08a5 aliguori
    int growable = bs->growable;
2728 178e08a5 aliguori
2729 178e08a5 aliguori
    bs->growable = 1;
2730 178e08a5 aliguori
    bdrv_pwrite(bs, pos, buf, size);
2731 178e08a5 aliguori
    bs->growable = growable;
2732 178e08a5 aliguori
2733 178e08a5 aliguori
    return size;
2734 178e08a5 aliguori
}
2735 178e08a5 aliguori
2736 178e08a5 aliguori
static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf,
2737 178e08a5 aliguori
                           int64_t pos, int size)
2738 178e08a5 aliguori
{
2739 178e08a5 aliguori
    int growable = bs->growable;
2740 178e08a5 aliguori
    int ret;
2741 178e08a5 aliguori
2742 178e08a5 aliguori
    bs->growable = 1;
2743 178e08a5 aliguori
    ret = bdrv_pread(bs, pos, buf, size);
2744 178e08a5 aliguori
    bs->growable = growable;
2745 178e08a5 aliguori
2746 178e08a5 aliguori
    return ret;
2747 178e08a5 aliguori
}
2748 178e08a5 aliguori
2749 585f8587 bellard
BlockDriver bdrv_qcow2 = {
2750 e60f469c aurel32
    .format_name        = "qcow2",
2751 e60f469c aurel32
    .instance_size        = sizeof(BDRVQcowState),
2752 e60f469c aurel32
    .bdrv_probe                = qcow_probe,
2753 e60f469c aurel32
    .bdrv_open                = qcow_open,
2754 e60f469c aurel32
    .bdrv_close                = qcow_close,
2755 e60f469c aurel32
    .bdrv_create        = qcow_create,
2756 e60f469c aurel32
    .bdrv_flush                = qcow_flush,
2757 e60f469c aurel32
    .bdrv_is_allocated        = qcow_is_allocated,
2758 e60f469c aurel32
    .bdrv_set_key        = qcow_set_key,
2759 e60f469c aurel32
    .bdrv_make_empty        = qcow_make_empty,
2760 e60f469c aurel32
2761 e60f469c aurel32
    .bdrv_aio_read        = qcow_aio_read,
2762 e60f469c aurel32
    .bdrv_aio_write        = qcow_aio_write,
2763 e60f469c aurel32
    .bdrv_aio_cancel        = qcow_aio_cancel,
2764 e60f469c aurel32
    .aiocb_size                = sizeof(QCowAIOCB),
2765 585f8587 bellard
    .bdrv_write_compressed = qcow_write_compressed,
2766 585f8587 bellard
2767 585f8587 bellard
    .bdrv_snapshot_create = qcow_snapshot_create,
2768 e60f469c aurel32
    .bdrv_snapshot_goto        = qcow_snapshot_goto,
2769 585f8587 bellard
    .bdrv_snapshot_delete = qcow_snapshot_delete,
2770 e60f469c aurel32
    .bdrv_snapshot_list        = qcow_snapshot_list,
2771 e60f469c aurel32
    .bdrv_get_info        = qcow_get_info,
2772 f965509c aliguori
2773 178e08a5 aliguori
    .bdrv_put_buffer    = qcow_put_buffer,
2774 178e08a5 aliguori
    .bdrv_get_buffer    = qcow_get_buffer,
2775 178e08a5 aliguori
2776 f965509c aliguori
    .bdrv_create2 = qcow_create2,
2777 585f8587 bellard
};