Statistics
| Branch: | Revision:

root / block-qcow2.c @ 5fafdf24

History | View | Annotate | Download (71.5 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 585f8587 bellard
#include "vl.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 5fafdf24 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 5fafdf24 ths
49 585f8587 bellard
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50 585f8587 bellard
#define QCOW_VERSION 2
51 585f8587 bellard
52 585f8587 bellard
#define QCOW_CRYPT_NONE 0
53 585f8587 bellard
#define QCOW_CRYPT_AES  1
54 585f8587 bellard
55 585f8587 bellard
/* indicate that the refcount of the referenced cluster is exactly one. */
56 585f8587 bellard
#define QCOW_OFLAG_COPIED     (1LL << 63)
57 585f8587 bellard
/* indicate that the cluster is compressed (they never have the copied flag) */
58 585f8587 bellard
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
59 585f8587 bellard
60 585f8587 bellard
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
61 585f8587 bellard
62 585f8587 bellard
#ifndef offsetof
63 585f8587 bellard
#define offsetof(type, field) ((size_t) &((type *)0)->field)
64 585f8587 bellard
#endif
65 585f8587 bellard
66 585f8587 bellard
typedef struct QCowHeader {
67 585f8587 bellard
    uint32_t magic;
68 585f8587 bellard
    uint32_t version;
69 585f8587 bellard
    uint64_t backing_file_offset;
70 585f8587 bellard
    uint32_t backing_file_size;
71 585f8587 bellard
    uint32_t cluster_bits;
72 585f8587 bellard
    uint64_t size; /* in bytes */
73 585f8587 bellard
    uint32_t crypt_method;
74 585f8587 bellard
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
75 585f8587 bellard
    uint64_t l1_table_offset;
76 585f8587 bellard
    uint64_t refcount_table_offset;
77 585f8587 bellard
    uint32_t refcount_table_clusters;
78 585f8587 bellard
    uint32_t nb_snapshots;
79 585f8587 bellard
    uint64_t snapshots_offset;
80 585f8587 bellard
} QCowHeader;
81 585f8587 bellard
82 585f8587 bellard
typedef struct __attribute__((packed)) QCowSnapshotHeader {
83 585f8587 bellard
    /* header is 8 byte aligned */
84 585f8587 bellard
    uint64_t l1_table_offset;
85 585f8587 bellard
86 585f8587 bellard
    uint32_t l1_size;
87 585f8587 bellard
    uint16_t id_str_size;
88 585f8587 bellard
    uint16_t name_size;
89 585f8587 bellard
90 585f8587 bellard
    uint32_t date_sec;
91 585f8587 bellard
    uint32_t date_nsec;
92 585f8587 bellard
93 585f8587 bellard
    uint64_t vm_clock_nsec;
94 585f8587 bellard
95 585f8587 bellard
    uint32_t vm_state_size;
96 585f8587 bellard
    uint32_t extra_data_size; /* for extension */
97 585f8587 bellard
    /* extra data follows */
98 585f8587 bellard
    /* id_str follows */
99 585f8587 bellard
    /* name follows  */
100 585f8587 bellard
} QCowSnapshotHeader;
101 585f8587 bellard
102 585f8587 bellard
#define L2_CACHE_SIZE 16
103 585f8587 bellard
104 585f8587 bellard
typedef struct QCowSnapshot {
105 585f8587 bellard
    uint64_t l1_table_offset;
106 585f8587 bellard
    uint32_t l1_size;
107 585f8587 bellard
    char *id_str;
108 585f8587 bellard
    char *name;
109 585f8587 bellard
    uint32_t vm_state_size;
110 585f8587 bellard
    uint32_t date_sec;
111 585f8587 bellard
    uint32_t date_nsec;
112 585f8587 bellard
    uint64_t vm_clock_nsec;
113 585f8587 bellard
} QCowSnapshot;
114 585f8587 bellard
115 585f8587 bellard
typedef struct BDRVQcowState {
116 585f8587 bellard
    BlockDriverState *hd;
117 585f8587 bellard
    int cluster_bits;
118 585f8587 bellard
    int cluster_size;
119 585f8587 bellard
    int cluster_sectors;
120 585f8587 bellard
    int l2_bits;
121 585f8587 bellard
    int l2_size;
122 585f8587 bellard
    int l1_size;
123 585f8587 bellard
    int l1_vm_state_index;
124 585f8587 bellard
    int csize_shift;
125 585f8587 bellard
    int csize_mask;
126 585f8587 bellard
    uint64_t cluster_offset_mask;
127 585f8587 bellard
    uint64_t l1_table_offset;
128 585f8587 bellard
    uint64_t *l1_table;
129 585f8587 bellard
    uint64_t *l2_cache;
130 585f8587 bellard
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
131 585f8587 bellard
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
132 585f8587 bellard
    uint8_t *cluster_cache;
133 585f8587 bellard
    uint8_t *cluster_data;
134 585f8587 bellard
    uint64_t cluster_cache_offset;
135 585f8587 bellard
136 585f8587 bellard
    uint64_t *refcount_table;
137 585f8587 bellard
    uint64_t refcount_table_offset;
138 585f8587 bellard
    uint32_t refcount_table_size;
139 585f8587 bellard
    uint64_t refcount_block_cache_offset;
140 585f8587 bellard
    uint16_t *refcount_block_cache;
141 585f8587 bellard
    int64_t free_cluster_index;
142 585f8587 bellard
    int64_t free_byte_offset;
143 585f8587 bellard
144 585f8587 bellard
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
145 585f8587 bellard
    uint32_t crypt_method_header;
146 585f8587 bellard
    AES_KEY aes_encrypt_key;
147 585f8587 bellard
    AES_KEY aes_decrypt_key;
148 585f8587 bellard
    uint64_t snapshots_offset;
149 585f8587 bellard
    int snapshots_size;
150 585f8587 bellard
    int nb_snapshots;
151 585f8587 bellard
    QCowSnapshot *snapshots;
152 585f8587 bellard
} BDRVQcowState;
153 585f8587 bellard
154 585f8587 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
155 5fafdf24 ths
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
156 585f8587 bellard
                     uint8_t *buf, int nb_sectors);
157 585f8587 bellard
static int qcow_read_snapshots(BlockDriverState *bs);
158 585f8587 bellard
static void qcow_free_snapshots(BlockDriverState *bs);
159 585f8587 bellard
static int refcount_init(BlockDriverState *bs);
160 585f8587 bellard
static void refcount_close(BlockDriverState *bs);
161 585f8587 bellard
static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
162 5fafdf24 ths
static int update_cluster_refcount(BlockDriverState *bs,
163 585f8587 bellard
                                   int64_t cluster_index,
164 585f8587 bellard
                                   int addend);
165 5fafdf24 ths
static void update_refcount(BlockDriverState *bs,
166 5fafdf24 ths
                            int64_t offset, int64_t length,
167 585f8587 bellard
                            int addend);
168 585f8587 bellard
static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
169 585f8587 bellard
static int64_t alloc_bytes(BlockDriverState *bs, int size);
170 5fafdf24 ths
static void free_clusters(BlockDriverState *bs,
171 585f8587 bellard
                          int64_t offset, int64_t size);
172 585f8587 bellard
#ifdef DEBUG_ALLOC
173 585f8587 bellard
static void check_refcounts(BlockDriverState *bs);
174 585f8587 bellard
#endif
175 585f8587 bellard
176 585f8587 bellard
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
177 585f8587 bellard
{
178 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
179 5fafdf24 ths
   
180 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
181 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
182 5fafdf24 ths
        be32_to_cpu(cow_header->version) == QCOW_VERSION)
183 585f8587 bellard
        return 100;
184 585f8587 bellard
    else
185 585f8587 bellard
        return 0;
186 585f8587 bellard
}
187 585f8587 bellard
188 585f8587 bellard
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
189 585f8587 bellard
{
190 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
191 585f8587 bellard
    int len, i, shift, ret;
192 585f8587 bellard
    QCowHeader header;
193 585f8587 bellard
194 585f8587 bellard
    ret = bdrv_file_open(&s->hd, filename, flags);
195 585f8587 bellard
    if (ret < 0)
196 585f8587 bellard
        return ret;
197 585f8587 bellard
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
198 585f8587 bellard
        goto fail;
199 585f8587 bellard
    be32_to_cpus(&header.magic);
200 585f8587 bellard
    be32_to_cpus(&header.version);
201 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
202 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
203 585f8587 bellard
    be64_to_cpus(&header.size);
204 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
205 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
206 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
207 585f8587 bellard
    be32_to_cpus(&header.l1_size);
208 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
209 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
210 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
211 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
212 5fafdf24 ths
   
213 585f8587 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
214 585f8587 bellard
        goto fail;
215 5fafdf24 ths
    if (header.size <= 1 ||
216 5fafdf24 ths
        header.cluster_bits < 9 ||
217 585f8587 bellard
        header.cluster_bits > 16)
218 585f8587 bellard
        goto fail;
219 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
220 585f8587 bellard
        goto fail;
221 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
222 585f8587 bellard
    if (s->crypt_method_header)
223 585f8587 bellard
        bs->encrypted = 1;
224 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
225 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
226 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
227 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
228 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
229 585f8587 bellard
    bs->total_sectors = header.size / 512;
230 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
231 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
232 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
233 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
234 5fafdf24 ths
    s->refcount_table_size =
235 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
236 585f8587 bellard
237 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
238 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
239 585f8587 bellard
240 585f8587 bellard
    /* read the level 1 table */
241 585f8587 bellard
    s->l1_size = header.l1_size;
242 585f8587 bellard
    shift = s->cluster_bits + s->l2_bits;
243 585f8587 bellard
    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
244 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
245 585f8587 bellard
       header.size bytes */
246 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
247 585f8587 bellard
        goto fail;
248 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
249 585f8587 bellard
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
250 585f8587 bellard
    if (!s->l1_table)
251 585f8587 bellard
        goto fail;
252 5fafdf24 ths
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
253 585f8587 bellard
        s->l1_size * sizeof(uint64_t))
254 585f8587 bellard
        goto fail;
255 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
256 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
257 585f8587 bellard
    }
258 585f8587 bellard
    /* alloc L2 cache */
259 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
260 585f8587 bellard
    if (!s->l2_cache)
261 585f8587 bellard
        goto fail;
262 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
263 585f8587 bellard
    if (!s->cluster_cache)
264 585f8587 bellard
        goto fail;
265 585f8587 bellard
    /* one more sector for decompressed data alignment */
266 585f8587 bellard
    s->cluster_data = qemu_malloc(s->cluster_size + 512);
267 585f8587 bellard
    if (!s->cluster_data)
268 585f8587 bellard
        goto fail;
269 585f8587 bellard
    s->cluster_cache_offset = -1;
270 5fafdf24 ths
   
271 585f8587 bellard
    if (refcount_init(bs) < 0)
272 585f8587 bellard
        goto fail;
273 585f8587 bellard
274 585f8587 bellard
    /* read the backing file name */
275 585f8587 bellard
    if (header.backing_file_offset != 0) {
276 585f8587 bellard
        len = header.backing_file_size;
277 585f8587 bellard
        if (len > 1023)
278 585f8587 bellard
            len = 1023;
279 585f8587 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
280 585f8587 bellard
            goto fail;
281 585f8587 bellard
        bs->backing_file[len] = '\0';
282 585f8587 bellard
    }
283 585f8587 bellard
    if (qcow_read_snapshots(bs) < 0)
284 585f8587 bellard
        goto fail;
285 585f8587 bellard
286 585f8587 bellard
#ifdef DEBUG_ALLOC
287 585f8587 bellard
    check_refcounts(bs);
288 585f8587 bellard
#endif
289 585f8587 bellard
    return 0;
290 585f8587 bellard
291 585f8587 bellard
 fail:
292 585f8587 bellard
    qcow_free_snapshots(bs);
293 585f8587 bellard
    refcount_close(bs);
294 585f8587 bellard
    qemu_free(s->l1_table);
295 585f8587 bellard
    qemu_free(s->l2_cache);
296 585f8587 bellard
    qemu_free(s->cluster_cache);
297 585f8587 bellard
    qemu_free(s->cluster_data);
298 585f8587 bellard
    bdrv_delete(s->hd);
299 585f8587 bellard
    return -1;
300 585f8587 bellard
}
301 585f8587 bellard
302 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
303 585f8587 bellard
{
304 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
305 585f8587 bellard
    uint8_t keybuf[16];
306 585f8587 bellard
    int len, i;
307 5fafdf24 ths
   
308 585f8587 bellard
    memset(keybuf, 0, 16);
309 585f8587 bellard
    len = strlen(key);
310 585f8587 bellard
    if (len > 16)
311 585f8587 bellard
        len = 16;
312 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
313 585f8587 bellard
       entropy */
314 585f8587 bellard
    for(i = 0;i < len;i++) {
315 585f8587 bellard
        keybuf[i] = key[i];
316 585f8587 bellard
    }
317 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
318 585f8587 bellard
319 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
320 585f8587 bellard
        return -1;
321 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
322 585f8587 bellard
        return -1;
323 585f8587 bellard
#if 0
324 585f8587 bellard
    /* test */
325 585f8587 bellard
    {
326 585f8587 bellard
        uint8_t in[16];
327 585f8587 bellard
        uint8_t out[16];
328 585f8587 bellard
        uint8_t tmp[16];
329 585f8587 bellard
        for(i=0;i<16;i++)
330 585f8587 bellard
            in[i] = i;
331 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
332 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
333 585f8587 bellard
        for(i = 0; i < 16; i++)
334 585f8587 bellard
            printf(" %02x", tmp[i]);
335 585f8587 bellard
        printf("\n");
336 585f8587 bellard
        for(i = 0; i < 16; i++)
337 585f8587 bellard
            printf(" %02x", out[i]);
338 585f8587 bellard
        printf("\n");
339 585f8587 bellard
    }
340 585f8587 bellard
#endif
341 585f8587 bellard
    return 0;
342 585f8587 bellard
}
343 585f8587 bellard
344 585f8587 bellard
/* The crypt function is compatible with the linux cryptoloop
345 585f8587 bellard
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
346 585f8587 bellard
   supported */
347 585f8587 bellard
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
348 585f8587 bellard
                            uint8_t *out_buf, const uint8_t *in_buf,
349 585f8587 bellard
                            int nb_sectors, int enc,
350 585f8587 bellard
                            const AES_KEY *key)
351 585f8587 bellard
{
352 585f8587 bellard
    union {
353 585f8587 bellard
        uint64_t ll[2];
354 585f8587 bellard
        uint8_t b[16];
355 585f8587 bellard
    } ivec;
356 585f8587 bellard
    int i;
357 585f8587 bellard
358 585f8587 bellard
    for(i = 0; i < nb_sectors; i++) {
359 585f8587 bellard
        ivec.ll[0] = cpu_to_le64(sector_num);
360 585f8587 bellard
        ivec.ll[1] = 0;
361 5fafdf24 ths
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
362 585f8587 bellard
                        ivec.b, enc);
363 585f8587 bellard
        sector_num++;
364 585f8587 bellard
        in_buf += 512;
365 585f8587 bellard
        out_buf += 512;
366 585f8587 bellard
    }
367 585f8587 bellard
}
368 585f8587 bellard
369 585f8587 bellard
static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
370 585f8587 bellard
                        uint64_t cluster_offset, int n_start, int n_end)
371 585f8587 bellard
{
372 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
373 585f8587 bellard
    int n, ret;
374 585f8587 bellard
375 585f8587 bellard
    n = n_end - n_start;
376 585f8587 bellard
    if (n <= 0)
377 585f8587 bellard
        return 0;
378 585f8587 bellard
    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
379 585f8587 bellard
    if (ret < 0)
380 585f8587 bellard
        return ret;
381 585f8587 bellard
    if (s->crypt_method) {
382 5fafdf24 ths
        encrypt_sectors(s, start_sect + n_start,
383 5fafdf24 ths
                        s->cluster_data,
384 585f8587 bellard
                        s->cluster_data, n, 1,
385 585f8587 bellard
                        &s->aes_encrypt_key);
386 585f8587 bellard
    }
387 5fafdf24 ths
    ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
388 585f8587 bellard
                     s->cluster_data, n);
389 585f8587 bellard
    if (ret < 0)
390 585f8587 bellard
        return ret;
391 585f8587 bellard
    return 0;
392 585f8587 bellard
}
393 585f8587 bellard
394 585f8587 bellard
static void l2_cache_reset(BlockDriverState *bs)
395 585f8587 bellard
{
396 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
397 585f8587 bellard
398 585f8587 bellard
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
399 585f8587 bellard
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
400 585f8587 bellard
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
401 585f8587 bellard
}
402 585f8587 bellard
403 585f8587 bellard
static inline int l2_cache_new_entry(BlockDriverState *bs)
404 585f8587 bellard
{
405 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
406 585f8587 bellard
    uint32_t min_count;
407 585f8587 bellard
    int min_index, i;
408 585f8587 bellard
409 585f8587 bellard
    /* find a new entry in the least used one */
410 585f8587 bellard
    min_index = 0;
411 585f8587 bellard
    min_count = 0xffffffff;
412 585f8587 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
413 585f8587 bellard
        if (s->l2_cache_counts[i] < min_count) {
414 585f8587 bellard
            min_count = s->l2_cache_counts[i];
415 585f8587 bellard
            min_index = i;
416 585f8587 bellard
        }
417 585f8587 bellard
    }
418 585f8587 bellard
    return min_index;
419 585f8587 bellard
}
420 585f8587 bellard
421 585f8587 bellard
static int64_t align_offset(int64_t offset, int n)
422 585f8587 bellard
{
423 585f8587 bellard
    offset = (offset + n - 1) & ~(n - 1);
424 585f8587 bellard
    return offset;
425 585f8587 bellard
}
426 585f8587 bellard
427 585f8587 bellard
static int grow_l1_table(BlockDriverState *bs, int min_size)
428 585f8587 bellard
{
429 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
430 585f8587 bellard
    int new_l1_size, new_l1_size2, ret, i;
431 585f8587 bellard
    uint64_t *new_l1_table;
432 585f8587 bellard
    uint64_t new_l1_table_offset;
433 585f8587 bellard
    uint64_t data64;
434 585f8587 bellard
    uint32_t data32;
435 585f8587 bellard
436 585f8587 bellard
    new_l1_size = s->l1_size;
437 585f8587 bellard
    if (min_size <= new_l1_size)
438 585f8587 bellard
        return 0;
439 585f8587 bellard
    while (min_size > new_l1_size) {
440 585f8587 bellard
        new_l1_size = (new_l1_size * 3 + 1) / 2;
441 585f8587 bellard
    }
442 585f8587 bellard
#ifdef DEBUG_ALLOC2
443 585f8587 bellard
    printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
444 585f8587 bellard
#endif
445 585f8587 bellard
446 585f8587 bellard
    new_l1_size2 = sizeof(uint64_t) * new_l1_size;
447 585f8587 bellard
    new_l1_table = qemu_mallocz(new_l1_size2);
448 585f8587 bellard
    if (!new_l1_table)
449 585f8587 bellard
        return -ENOMEM;
450 585f8587 bellard
    memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
451 585f8587 bellard
452 585f8587 bellard
    /* write new table (align to cluster) */
453 585f8587 bellard
    new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
454 5fafdf24 ths
   
455 585f8587 bellard
    for(i = 0; i < s->l1_size; i++)
456 585f8587 bellard
        new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
457 585f8587 bellard
    ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
458 585f8587 bellard
    if (ret != new_l1_size2)
459 585f8587 bellard
        goto fail;
460 585f8587 bellard
    for(i = 0; i < s->l1_size; i++)
461 585f8587 bellard
        new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
462 5fafdf24 ths
   
463 585f8587 bellard
    /* set new table */
464 585f8587 bellard
    data64 = cpu_to_be64(new_l1_table_offset);
465 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_table_offset),
466 585f8587 bellard
                    &data64, sizeof(data64)) != sizeof(data64))
467 585f8587 bellard
        goto fail;
468 585f8587 bellard
    data32 = cpu_to_be32(new_l1_size);
469 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size),
470 585f8587 bellard
                    &data32, sizeof(data32)) != sizeof(data32))
471 585f8587 bellard
        goto fail;
472 585f8587 bellard
    qemu_free(s->l1_table);
473 585f8587 bellard
    free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
474 585f8587 bellard
    s->l1_table_offset = new_l1_table_offset;
475 585f8587 bellard
    s->l1_table = new_l1_table;
476 585f8587 bellard
    s->l1_size = new_l1_size;
477 585f8587 bellard
    return 0;
478 585f8587 bellard
 fail:
479 585f8587 bellard
    qemu_free(s->l1_table);
480 585f8587 bellard
    return -EIO;
481 585f8587 bellard
}
482 585f8587 bellard
483 585f8587 bellard
/* 'allocate' is:
484 585f8587 bellard
 *
485 585f8587 bellard
 * 0 not to allocate.
486 585f8587 bellard
 *
487 585f8587 bellard
 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
488 585f8587 bellard
 * 'n_end')
489 585f8587 bellard
 *
490 585f8587 bellard
 * 2 to allocate a compressed cluster of size
491 585f8587 bellard
 * 'compressed_size'. 'compressed_size' must be > 0 and <
492 5fafdf24 ths
 * cluster_size
493 585f8587 bellard
 *
494 585f8587 bellard
 * return 0 if not allocated.
495 585f8587 bellard
 */
496 585f8587 bellard
static uint64_t get_cluster_offset(BlockDriverState *bs,
497 585f8587 bellard
                                   uint64_t offset, int allocate,
498 585f8587 bellard
                                   int compressed_size,
499 585f8587 bellard
                                   int n_start, int n_end)
500 585f8587 bellard
{
501 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
502 585f8587 bellard
    int min_index, i, j, l1_index, l2_index, ret;
503 585f8587 bellard
    uint64_t l2_offset, *l2_table, cluster_offset, tmp, old_l2_offset;
504 5fafdf24 ths
   
505 585f8587 bellard
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
506 585f8587 bellard
    if (l1_index >= s->l1_size) {
507 585f8587 bellard
        /* outside l1 table is allowed: we grow the table if needed */
508 585f8587 bellard
        if (!allocate)
509 585f8587 bellard
            return 0;
510 585f8587 bellard
        if (grow_l1_table(bs, l1_index + 1) < 0)
511 585f8587 bellard
            return 0;
512 585f8587 bellard
    }
513 585f8587 bellard
    l2_offset = s->l1_table[l1_index];
514 585f8587 bellard
    if (!l2_offset) {
515 585f8587 bellard
        if (!allocate)
516 585f8587 bellard
            return 0;
517 585f8587 bellard
    l2_allocate:
518 585f8587 bellard
        old_l2_offset = l2_offset;
519 585f8587 bellard
        /* allocate a new l2 entry */
520 585f8587 bellard
        l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
521 585f8587 bellard
        /* update the L1 entry */
522 585f8587 bellard
        s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
523 585f8587 bellard
        tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
524 5fafdf24 ths
        if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
525 585f8587 bellard
                        &tmp, sizeof(tmp)) != sizeof(tmp))
526 585f8587 bellard
            return 0;
527 585f8587 bellard
        min_index = l2_cache_new_entry(bs);
528 585f8587 bellard
        l2_table = s->l2_cache + (min_index << s->l2_bits);
529 585f8587 bellard
530 585f8587 bellard
        if (old_l2_offset == 0) {
531 585f8587 bellard
            memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
532 585f8587 bellard
        } else {
533 5fafdf24 ths
            if (bdrv_pread(s->hd, old_l2_offset,
534 585f8587 bellard
                           l2_table, s->l2_size * sizeof(uint64_t)) !=
535 585f8587 bellard
                s->l2_size * sizeof(uint64_t))
536 585f8587 bellard
                return 0;
537 585f8587 bellard
        }
538 5fafdf24 ths
        if (bdrv_pwrite(s->hd, l2_offset,
539 585f8587 bellard
                        l2_table, s->l2_size * sizeof(uint64_t)) !=
540 585f8587 bellard
            s->l2_size * sizeof(uint64_t))
541 585f8587 bellard
            return 0;
542 585f8587 bellard
    } else {
543 585f8587 bellard
        if (!(l2_offset & QCOW_OFLAG_COPIED)) {
544 585f8587 bellard
            if (allocate) {
545 585f8587 bellard
                free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
546 585f8587 bellard
                goto l2_allocate;
547 585f8587 bellard
            }
548 585f8587 bellard
        } else {
549 585f8587 bellard
            l2_offset &= ~QCOW_OFLAG_COPIED;
550 585f8587 bellard
        }
551 585f8587 bellard
        for(i = 0; i < L2_CACHE_SIZE; i++) {
552 585f8587 bellard
            if (l2_offset == s->l2_cache_offsets[i]) {
553 585f8587 bellard
                /* increment the hit count */
554 585f8587 bellard
                if (++s->l2_cache_counts[i] == 0xffffffff) {
555 585f8587 bellard
                    for(j = 0; j < L2_CACHE_SIZE; j++) {
556 585f8587 bellard
                        s->l2_cache_counts[j] >>= 1;
557 585f8587 bellard
                    }
558 585f8587 bellard
                }
559 585f8587 bellard
                l2_table = s->l2_cache + (i << s->l2_bits);
560 585f8587 bellard
                goto found;
561 585f8587 bellard
            }
562 585f8587 bellard
        }
563 585f8587 bellard
        /* not found: load a new entry in the least used one */
564 585f8587 bellard
        min_index = l2_cache_new_entry(bs);
565 585f8587 bellard
        l2_table = s->l2_cache + (min_index << s->l2_bits);
566 5fafdf24 ths
        if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
567 585f8587 bellard
            s->l2_size * sizeof(uint64_t))
568 585f8587 bellard
            return 0;
569 585f8587 bellard
    }
570 585f8587 bellard
    s->l2_cache_offsets[min_index] = l2_offset;
571 585f8587 bellard
    s->l2_cache_counts[min_index] = 1;
572 585f8587 bellard
 found:
573 585f8587 bellard
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
574 585f8587 bellard
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
575 585f8587 bellard
    if (!cluster_offset) {
576 585f8587 bellard
        if (!allocate)
577 585f8587 bellard
            return cluster_offset;
578 585f8587 bellard
    } else if (!(cluster_offset & QCOW_OFLAG_COPIED)) {
579 585f8587 bellard
        if (!allocate)
580 585f8587 bellard
            return cluster_offset;
581 585f8587 bellard
        /* free the cluster */
582 585f8587 bellard
        if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
583 585f8587 bellard
            int nb_csectors;
584 5fafdf24 ths
            nb_csectors = ((cluster_offset >> s->csize_shift) &
585 585f8587 bellard
                           s->csize_mask) + 1;
586 585f8587 bellard
            free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
587 585f8587 bellard
                          nb_csectors * 512);
588 585f8587 bellard
        } else {
589 585f8587 bellard
            free_clusters(bs, cluster_offset, s->cluster_size);
590 585f8587 bellard
        }
591 585f8587 bellard
    } else {
592 585f8587 bellard
        cluster_offset &= ~QCOW_OFLAG_COPIED;
593 585f8587 bellard
        return cluster_offset;
594 585f8587 bellard
    }
595 585f8587 bellard
    if (allocate == 1) {
596 585f8587 bellard
        /* allocate a new cluster */
597 585f8587 bellard
        cluster_offset = alloc_clusters(bs, s->cluster_size);
598 585f8587 bellard
599 585f8587 bellard
        /* we must initialize the cluster content which won't be
600 585f8587 bellard
           written */
601 585f8587 bellard
        if ((n_end - n_start) < s->cluster_sectors) {
602 585f8587 bellard
            uint64_t start_sect;
603 5fafdf24 ths
           
604 585f8587 bellard
            start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
605 585f8587 bellard
            ret = copy_sectors(bs, start_sect,
606 585f8587 bellard
                               cluster_offset, 0, n_start);
607 585f8587 bellard
            if (ret < 0)
608 585f8587 bellard
                return 0;
609 585f8587 bellard
            ret = copy_sectors(bs, start_sect,
610 585f8587 bellard
                               cluster_offset, n_end, s->cluster_sectors);
611 585f8587 bellard
            if (ret < 0)
612 585f8587 bellard
                return 0;
613 585f8587 bellard
        }
614 585f8587 bellard
        tmp = cpu_to_be64(cluster_offset | QCOW_OFLAG_COPIED);
615 585f8587 bellard
    } else {
616 585f8587 bellard
        int nb_csectors;
617 585f8587 bellard
        cluster_offset = alloc_bytes(bs, compressed_size);
618 5fafdf24 ths
        nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
619 585f8587 bellard
            (cluster_offset >> 9);
620 5fafdf24 ths
        cluster_offset |= QCOW_OFLAG_COMPRESSED |
621 585f8587 bellard
            ((uint64_t)nb_csectors << s->csize_shift);
622 585f8587 bellard
        /* compressed clusters never have the copied flag */
623 585f8587 bellard
        tmp = cpu_to_be64(cluster_offset);
624 585f8587 bellard
    }
625 585f8587 bellard
    /* update L2 table */
626 585f8587 bellard
    l2_table[l2_index] = tmp;
627 5fafdf24 ths
    if (bdrv_pwrite(s->hd,
628 585f8587 bellard
                    l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
629 585f8587 bellard
        return 0;
630 585f8587 bellard
    return cluster_offset;
631 585f8587 bellard
}
632 585f8587 bellard
633 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
634 585f8587 bellard
                             int nb_sectors, int *pnum)
635 585f8587 bellard
{
636 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
637 585f8587 bellard
    int index_in_cluster, n;
638 585f8587 bellard
    uint64_t cluster_offset;
639 585f8587 bellard
640 585f8587 bellard
    cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
641 585f8587 bellard
    index_in_cluster = sector_num & (s->cluster_sectors - 1);
642 585f8587 bellard
    n = s->cluster_sectors - index_in_cluster;
643 585f8587 bellard
    if (n > nb_sectors)
644 585f8587 bellard
        n = nb_sectors;
645 585f8587 bellard
    *pnum = n;
646 585f8587 bellard
    return (cluster_offset != 0);
647 585f8587 bellard
}
648 585f8587 bellard
649 585f8587 bellard
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
650 585f8587 bellard
                             const uint8_t *buf, int buf_size)
651 585f8587 bellard
{
652 585f8587 bellard
    z_stream strm1, *strm = &strm1;
653 585f8587 bellard
    int ret, out_len;
654 585f8587 bellard
655 585f8587 bellard
    memset(strm, 0, sizeof(*strm));
656 585f8587 bellard
657 585f8587 bellard
    strm->next_in = (uint8_t *)buf;
658 585f8587 bellard
    strm->avail_in = buf_size;
659 585f8587 bellard
    strm->next_out = out_buf;
660 585f8587 bellard
    strm->avail_out = out_buf_size;
661 585f8587 bellard
662 585f8587 bellard
    ret = inflateInit2(strm, -12);
663 585f8587 bellard
    if (ret != Z_OK)
664 585f8587 bellard
        return -1;
665 585f8587 bellard
    ret = inflate(strm, Z_FINISH);
666 585f8587 bellard
    out_len = strm->next_out - out_buf;
667 585f8587 bellard
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
668 585f8587 bellard
        out_len != out_buf_size) {
669 585f8587 bellard
        inflateEnd(strm);
670 585f8587 bellard
        return -1;
671 585f8587 bellard
    }
672 585f8587 bellard
    inflateEnd(strm);
673 585f8587 bellard
    return 0;
674 585f8587 bellard
}
675 5fafdf24 ths
                             
676 585f8587 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
677 585f8587 bellard
{
678 585f8587 bellard
    int ret, csize, nb_csectors, sector_offset;
679 585f8587 bellard
    uint64_t coffset;
680 585f8587 bellard
681 585f8587 bellard
    coffset = cluster_offset & s->cluster_offset_mask;
682 585f8587 bellard
    if (s->cluster_cache_offset != coffset) {
683 585f8587 bellard
        nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
684 585f8587 bellard
        sector_offset = coffset & 511;
685 585f8587 bellard
        csize = nb_csectors * 512 - sector_offset;
686 585f8587 bellard
        ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
687 585f8587 bellard
        if (ret < 0) {
688 585f8587 bellard
            return -1;
689 585f8587 bellard
        }
690 585f8587 bellard
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
691 585f8587 bellard
                              s->cluster_data + sector_offset, csize) < 0) {
692 585f8587 bellard
            return -1;
693 585f8587 bellard
        }
694 585f8587 bellard
        s->cluster_cache_offset = coffset;
695 585f8587 bellard
    }
696 585f8587 bellard
    return 0;
697 585f8587 bellard
}
698 585f8587 bellard
699 a9465922 bellard
/* handle reading after the end of the backing file */
700 5fafdf24 ths
static int backing_read1(BlockDriverState *bs,
701 a9465922 bellard
                         int64_t sector_num, uint8_t *buf, int nb_sectors)
702 a9465922 bellard
{
703 a9465922 bellard
    int n1;
704 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
705 a9465922 bellard
        return nb_sectors;
706 a9465922 bellard
    if (sector_num >= bs->total_sectors)
707 a9465922 bellard
        n1 = 0;
708 a9465922 bellard
    else
709 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
710 a9465922 bellard
    memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
711 a9465922 bellard
    return n1;
712 a9465922 bellard
}
713 a9465922 bellard
714 5fafdf24 ths
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
715 585f8587 bellard
                     uint8_t *buf, int nb_sectors)
716 585f8587 bellard
{
717 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
718 a9465922 bellard
    int ret, index_in_cluster, n, n1;
719 585f8587 bellard
    uint64_t cluster_offset;
720 5fafdf24 ths
   
721 585f8587 bellard
    while (nb_sectors > 0) {
722 585f8587 bellard
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
723 585f8587 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
724 585f8587 bellard
        n = s->cluster_sectors - index_in_cluster;
725 585f8587 bellard
        if (n > nb_sectors)
726 585f8587 bellard
            n = nb_sectors;
727 585f8587 bellard
        if (!cluster_offset) {
728 585f8587 bellard
            if (bs->backing_hd) {
729 585f8587 bellard
                /* read from the base image */
730 a9465922 bellard
                n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
731 a9465922 bellard
                if (n1 > 0) {
732 a9465922 bellard
                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
733 a9465922 bellard
                    if (ret < 0)
734 a9465922 bellard
                        return -1;
735 a9465922 bellard
                }
736 585f8587 bellard
            } else {
737 585f8587 bellard
                memset(buf, 0, 512 * n);
738 585f8587 bellard
            }
739 585f8587 bellard
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
740 585f8587 bellard
            if (decompress_cluster(s, cluster_offset) < 0)
741 585f8587 bellard
                return -1;
742 585f8587 bellard
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
743 585f8587 bellard
        } else {
744 585f8587 bellard
            ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
745 5fafdf24 ths
            if (ret != n * 512)
746 585f8587 bellard
                return -1;
747 585f8587 bellard
            if (s->crypt_method) {
748 5fafdf24 ths
                encrypt_sectors(s, sector_num, buf, buf, n, 0,
749 585f8587 bellard
                                &s->aes_decrypt_key);
750 585f8587 bellard
            }
751 585f8587 bellard
        }
752 585f8587 bellard
        nb_sectors -= n;
753 585f8587 bellard
        sector_num += n;
754 585f8587 bellard
        buf += n * 512;
755 585f8587 bellard
    }
756 585f8587 bellard
    return 0;
757 585f8587 bellard
}
758 585f8587 bellard
759 5fafdf24 ths
static int qcow_write(BlockDriverState *bs, int64_t sector_num,
760 585f8587 bellard
                     const uint8_t *buf, int nb_sectors)
761 585f8587 bellard
{
762 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
763 585f8587 bellard
    int ret, index_in_cluster, n;
764 585f8587 bellard
    uint64_t cluster_offset;
765 5fafdf24 ths
   
766 585f8587 bellard
    while (nb_sectors > 0) {
767 585f8587 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
768 585f8587 bellard
        n = s->cluster_sectors - index_in_cluster;
769 585f8587 bellard
        if (n > nb_sectors)
770 585f8587 bellard
            n = nb_sectors;
771 5fafdf24 ths
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
772 5fafdf24 ths
                                            index_in_cluster,
773 585f8587 bellard
                                            index_in_cluster + n);
774 585f8587 bellard
        if (!cluster_offset)
775 585f8587 bellard
            return -1;
776 585f8587 bellard
        if (s->crypt_method) {
777 585f8587 bellard
            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
778 585f8587 bellard
                            &s->aes_encrypt_key);
779 5fafdf24 ths
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
780 585f8587 bellard
                              s->cluster_data, n * 512);
781 585f8587 bellard
        } else {
782 585f8587 bellard
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
783 585f8587 bellard
        }
784 5fafdf24 ths
        if (ret != n * 512)
785 585f8587 bellard
            return -1;
786 585f8587 bellard
        nb_sectors -= n;
787 585f8587 bellard
        sector_num += n;
788 585f8587 bellard
        buf += n * 512;
789 585f8587 bellard
    }
790 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
791 585f8587 bellard
    return 0;
792 585f8587 bellard
}
793 585f8587 bellard
794 ce1a14dc pbrook
typedef struct QCowAIOCB {
795 ce1a14dc pbrook
    BlockDriverAIOCB common;
796 585f8587 bellard
    int64_t sector_num;
797 585f8587 bellard
    uint8_t *buf;
798 585f8587 bellard
    int nb_sectors;
799 585f8587 bellard
    int n;
800 585f8587 bellard
    uint64_t cluster_offset;
801 5fafdf24 ths
    uint8_t *cluster_data;
802 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
803 585f8587 bellard
} QCowAIOCB;
804 585f8587 bellard
805 585f8587 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
806 585f8587 bellard
{
807 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
808 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
809 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
810 a9465922 bellard
    int index_in_cluster, n1;
811 585f8587 bellard
812 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
813 585f8587 bellard
    if (ret < 0) {
814 585f8587 bellard
    fail:
815 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, ret);
816 ce1a14dc pbrook
        qemu_aio_release(acb);
817 585f8587 bellard
        return;
818 585f8587 bellard
    }
819 585f8587 bellard
820 585f8587 bellard
 redo:
821 585f8587 bellard
    /* post process the read buffer */
822 ce1a14dc pbrook
    if (!acb->cluster_offset) {
823 585f8587 bellard
        /* nothing to do */
824 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
825 585f8587 bellard
        /* nothing to do */
826 585f8587 bellard
    } else {
827 585f8587 bellard
        if (s->crypt_method) {
828 5fafdf24 ths
            encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
829 5fafdf24 ths
                            acb->n, 0,
830 585f8587 bellard
                            &s->aes_decrypt_key);
831 585f8587 bellard
        }
832 585f8587 bellard
    }
833 585f8587 bellard
834 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
835 ce1a14dc pbrook
    acb->sector_num += acb->n;
836 ce1a14dc pbrook
    acb->buf += acb->n * 512;
837 585f8587 bellard
838 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
839 585f8587 bellard
        /* request completed */
840 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
841 ce1a14dc pbrook
        qemu_aio_release(acb);
842 585f8587 bellard
        return;
843 585f8587 bellard
    }
844 5fafdf24 ths
   
845 585f8587 bellard
    /* prepare next AIO request */
846 5fafdf24 ths
    acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
847 ce1a14dc pbrook
                                             0, 0, 0, 0);
848 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
849 ce1a14dc pbrook
    acb->n = s->cluster_sectors - index_in_cluster;
850 ce1a14dc pbrook
    if (acb->n > acb->nb_sectors)
851 ce1a14dc pbrook
        acb->n = acb->nb_sectors;
852 ce1a14dc pbrook
853 ce1a14dc pbrook
    if (!acb->cluster_offset) {
854 585f8587 bellard
        if (bs->backing_hd) {
855 585f8587 bellard
            /* read from the base image */
856 5fafdf24 ths
            n1 = backing_read1(bs->backing_hd, acb->sector_num,
857 ce1a14dc pbrook
                               acb->buf, acb->n);
858 a9465922 bellard
            if (n1 > 0) {
859 5fafdf24 ths
                acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
860 ce1a14dc pbrook
                                    acb->buf, acb->n, qcow_aio_read_cb, acb);
861 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
862 a9465922 bellard
                    goto fail;
863 a9465922 bellard
            } else {
864 a9465922 bellard
                goto redo;
865 a9465922 bellard
            }
866 585f8587 bellard
        } else {
867 585f8587 bellard
            /* Note: in this case, no need to wait */
868 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
869 585f8587 bellard
            goto redo;
870 585f8587 bellard
        }
871 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
872 585f8587 bellard
        /* add AIO support for compressed blocks ? */
873 ce1a14dc pbrook
        if (decompress_cluster(s, acb->cluster_offset) < 0)
874 585f8587 bellard
            goto fail;
875 5fafdf24 ths
        memcpy(acb->buf,
876 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
877 585f8587 bellard
        goto redo;
878 585f8587 bellard
    } else {
879 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
880 585f8587 bellard
            ret = -EIO;
881 585f8587 bellard
            goto fail;
882 585f8587 bellard
        }
883 ce1a14dc pbrook
        acb->hd_aiocb = bdrv_aio_read(s->hd,
884 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
885 ce1a14dc pbrook
                            acb->buf, acb->n, qcow_aio_read_cb, acb);
886 ce1a14dc pbrook
        if (acb->hd_aiocb == NULL)
887 585f8587 bellard
            goto fail;
888 585f8587 bellard
    }
889 585f8587 bellard
}
890 585f8587 bellard
891 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
892 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
893 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
894 585f8587 bellard
{
895 ce1a14dc pbrook
    QCowAIOCB *acb;
896 ce1a14dc pbrook
897 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
898 ce1a14dc pbrook
    if (!acb)
899 ce1a14dc pbrook
        return NULL;
900 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
901 ce1a14dc pbrook
    acb->sector_num = sector_num;
902 ce1a14dc pbrook
    acb->buf = buf;
903 ce1a14dc pbrook
    acb->nb_sectors = nb_sectors;
904 ce1a14dc pbrook
    acb->n = 0;
905 ce1a14dc pbrook
    acb->cluster_offset = 0;
906 ce1a14dc pbrook
    return acb;
907 ce1a14dc pbrook
}
908 ce1a14dc pbrook
909 ce1a14dc pbrook
static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
910 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
911 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
912 ce1a14dc pbrook
{
913 ce1a14dc pbrook
    QCowAIOCB *acb;
914 ce1a14dc pbrook
915 ce1a14dc pbrook
    acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
916 ce1a14dc pbrook
    if (!acb)
917 ce1a14dc pbrook
        return NULL;
918 585f8587 bellard
919 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
920 ce1a14dc pbrook
    return &acb->common;
921 585f8587 bellard
}
922 585f8587 bellard
923 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
924 585f8587 bellard
{
925 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
926 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
927 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
928 585f8587 bellard
    int index_in_cluster;
929 585f8587 bellard
    uint64_t cluster_offset;
930 585f8587 bellard
    const uint8_t *src_buf;
931 ce1a14dc pbrook
932 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
933 ce1a14dc pbrook
934 585f8587 bellard
    if (ret < 0) {
935 585f8587 bellard
    fail:
936 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, ret);
937 ce1a14dc pbrook
        qemu_aio_release(acb);
938 585f8587 bellard
        return;
939 585f8587 bellard
    }
940 585f8587 bellard
941 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
942 ce1a14dc pbrook
    acb->sector_num += acb->n;
943 ce1a14dc pbrook
    acb->buf += acb->n * 512;
944 585f8587 bellard
945 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
946 585f8587 bellard
        /* request completed */
947 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
948 ce1a14dc pbrook
        qemu_aio_release(acb);
949 585f8587 bellard
        return;
950 585f8587 bellard
    }
951 5fafdf24 ths
   
952 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
953 ce1a14dc pbrook
    acb->n = s->cluster_sectors - index_in_cluster;
954 ce1a14dc pbrook
    if (acb->n > acb->nb_sectors)
955 ce1a14dc pbrook
        acb->n = acb->nb_sectors;
956 5fafdf24 ths
    cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
957 5fafdf24 ths
                                        index_in_cluster,
958 ce1a14dc pbrook
                                        index_in_cluster + acb->n);
959 585f8587 bellard
    if (!cluster_offset || (cluster_offset & 511) != 0) {
960 585f8587 bellard
        ret = -EIO;
961 585f8587 bellard
        goto fail;
962 585f8587 bellard
    }
963 585f8587 bellard
    if (s->crypt_method) {
964 ce1a14dc pbrook
        if (!acb->cluster_data) {
965 ce1a14dc pbrook
            acb->cluster_data = qemu_mallocz(s->cluster_size);
966 ce1a14dc pbrook
            if (!acb->cluster_data) {
967 585f8587 bellard
                ret = -ENOMEM;
968 585f8587 bellard
                goto fail;
969 585f8587 bellard
            }
970 585f8587 bellard
        }
971 5fafdf24 ths
        encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
972 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
973 ce1a14dc pbrook
        src_buf = acb->cluster_data;
974 585f8587 bellard
    } else {
975 ce1a14dc pbrook
        src_buf = acb->buf;
976 585f8587 bellard
    }
977 ce1a14dc pbrook
    acb->hd_aiocb = bdrv_aio_write(s->hd,
978 5fafdf24 ths
                                   (cluster_offset >> 9) + index_in_cluster,
979 5fafdf24 ths
                                   src_buf, acb->n,
980 ce1a14dc pbrook
                                   qcow_aio_write_cb, acb);
981 ce1a14dc pbrook
    if (acb->hd_aiocb == NULL)
982 585f8587 bellard
        goto fail;
983 585f8587 bellard
}
984 585f8587 bellard
985 ce1a14dc pbrook
static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
986 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
987 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
988 585f8587 bellard
{
989 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
990 ce1a14dc pbrook
    QCowAIOCB *acb;
991 5fafdf24 ths
   
992 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
993 585f8587 bellard
994 ce1a14dc pbrook
    acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
995 ce1a14dc pbrook
    if (!acb)
996 ce1a14dc pbrook
        return NULL;
997 5fafdf24 ths
   
998 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
999 ce1a14dc pbrook
    return &acb->common;
1000 585f8587 bellard
}
1001 585f8587 bellard
1002 ce1a14dc pbrook
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
1003 585f8587 bellard
{
1004 ce1a14dc pbrook
    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1005 ce1a14dc pbrook
    if (acb->hd_aiocb)
1006 ce1a14dc pbrook
        bdrv_aio_cancel(acb->hd_aiocb);
1007 ce1a14dc pbrook
    qemu_aio_release(acb);
1008 585f8587 bellard
}
1009 585f8587 bellard
1010 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
1011 585f8587 bellard
{
1012 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1013 585f8587 bellard
    qemu_free(s->l1_table);
1014 585f8587 bellard
    qemu_free(s->l2_cache);
1015 585f8587 bellard
    qemu_free(s->cluster_cache);
1016 585f8587 bellard
    qemu_free(s->cluster_data);
1017 585f8587 bellard
    refcount_close(bs);
1018 585f8587 bellard
    bdrv_delete(s->hd);
1019 585f8587 bellard
}
1020 585f8587 bellard
1021 585f8587 bellard
/* XXX: use std qcow open function ? */
1022 585f8587 bellard
typedef struct QCowCreateState {
1023 585f8587 bellard
    int cluster_size;
1024 585f8587 bellard
    int cluster_bits;
1025 585f8587 bellard
    uint16_t *refcount_block;
1026 585f8587 bellard
    uint64_t *refcount_table;
1027 585f8587 bellard
    int64_t l1_table_offset;
1028 585f8587 bellard
    int64_t refcount_table_offset;
1029 585f8587 bellard
    int64_t refcount_block_offset;
1030 585f8587 bellard
} QCowCreateState;
1031 585f8587 bellard
1032 585f8587 bellard
static void create_refcount_update(QCowCreateState *s,
1033 585f8587 bellard
                                   int64_t offset, int64_t size)
1034 585f8587 bellard
{
1035 585f8587 bellard
    int refcount;
1036 585f8587 bellard
    int64_t start, last, cluster_offset;
1037 585f8587 bellard
    uint16_t *p;
1038 585f8587 bellard
1039 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
1040 585f8587 bellard
    last = (offset + size - 1)  & ~(s->cluster_size - 1);
1041 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
1042 585f8587 bellard
        cluster_offset += s->cluster_size) {
1043 585f8587 bellard
        p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1044 585f8587 bellard
        refcount = be16_to_cpu(*p);
1045 585f8587 bellard
        refcount++;
1046 585f8587 bellard
        *p = cpu_to_be16(refcount);
1047 585f8587 bellard
    }
1048 585f8587 bellard
}
1049 585f8587 bellard
1050 585f8587 bellard
static int qcow_create(const char *filename, int64_t total_size,
1051 585f8587 bellard
                      const char *backing_file, int flags)
1052 585f8587 bellard
{
1053 585f8587 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1054 585f8587 bellard
    QCowHeader header;
1055 585f8587 bellard
    uint64_t tmp, offset;
1056 585f8587 bellard
    QCowCreateState s1, *s = &s1;
1057 5fafdf24 ths
   
1058 585f8587 bellard
    memset(s, 0, sizeof(*s));
1059 585f8587 bellard
1060 585f8587 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1061 585f8587 bellard
    if (fd < 0)
1062 585f8587 bellard
        return -1;
1063 585f8587 bellard
    memset(&header, 0, sizeof(header));
1064 585f8587 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
1065 585f8587 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
1066 585f8587 bellard
    header.size = cpu_to_be64(total_size * 512);
1067 585f8587 bellard
    header_size = sizeof(header);
1068 585f8587 bellard
    backing_filename_len = 0;
1069 585f8587 bellard
    if (backing_file) {
1070 585f8587 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
1071 585f8587 bellard
        backing_filename_len = strlen(backing_file);
1072 585f8587 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
1073 585f8587 bellard
        header_size += backing_filename_len;
1074 585f8587 bellard
    }
1075 585f8587 bellard
    s->cluster_bits = 12;  /* 4 KB clusters */
1076 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
1077 585f8587 bellard
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
1078 585f8587 bellard
    header_size = (header_size + 7) & ~7;
1079 585f8587 bellard
    if (flags) {
1080 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1081 585f8587 bellard
    } else {
1082 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1083 585f8587 bellard
    }
1084 585f8587 bellard
    l2_bits = s->cluster_bits - 3;
1085 585f8587 bellard
    shift = s->cluster_bits + l2_bits;
1086 585f8587 bellard
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1087 585f8587 bellard
    offset = align_offset(header_size, s->cluster_size);
1088 585f8587 bellard
    s->l1_table_offset = offset;
1089 585f8587 bellard
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1090 585f8587 bellard
    header.l1_size = cpu_to_be32(l1_size);
1091 15e6690a bellard
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
1092 585f8587 bellard
1093 585f8587 bellard
    s->refcount_table = qemu_mallocz(s->cluster_size);
1094 585f8587 bellard
    if (!s->refcount_table)
1095 585f8587 bellard
        goto fail;
1096 585f8587 bellard
    s->refcount_block = qemu_mallocz(s->cluster_size);
1097 585f8587 bellard
    if (!s->refcount_block)
1098 585f8587 bellard
        goto fail;
1099 5fafdf24 ths
   
1100 585f8587 bellard
    s->refcount_table_offset = offset;
1101 585f8587 bellard
    header.refcount_table_offset = cpu_to_be64(offset);
1102 585f8587 bellard
    header.refcount_table_clusters = cpu_to_be32(1);
1103 585f8587 bellard
    offset += s->cluster_size;
1104 585f8587 bellard
1105 585f8587 bellard
    s->refcount_table[0] = cpu_to_be64(offset);
1106 585f8587 bellard
    s->refcount_block_offset = offset;
1107 585f8587 bellard
    offset += s->cluster_size;
1108 585f8587 bellard
1109 585f8587 bellard
    /* update refcounts */
1110 585f8587 bellard
    create_refcount_update(s, 0, header_size);
1111 15e6690a bellard
    create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
1112 585f8587 bellard
    create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1113 585f8587 bellard
    create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
1114 5fafdf24 ths
   
1115 585f8587 bellard
    /* write all the data */
1116 585f8587 bellard
    write(fd, &header, sizeof(header));
1117 585f8587 bellard
    if (backing_file) {
1118 585f8587 bellard
        write(fd, backing_file, backing_filename_len);
1119 585f8587 bellard
    }
1120 585f8587 bellard
    lseek(fd, s->l1_table_offset, SEEK_SET);
1121 585f8587 bellard
    tmp = 0;
1122 585f8587 bellard
    for(i = 0;i < l1_size; i++) {
1123 585f8587 bellard
        write(fd, &tmp, sizeof(tmp));
1124 585f8587 bellard
    }
1125 585f8587 bellard
    lseek(fd, s->refcount_table_offset, SEEK_SET);
1126 585f8587 bellard
    write(fd, s->refcount_table, s->cluster_size);
1127 5fafdf24 ths
   
1128 585f8587 bellard
    lseek(fd, s->refcount_block_offset, SEEK_SET);
1129 585f8587 bellard
    write(fd, s->refcount_block, s->cluster_size);
1130 585f8587 bellard
1131 585f8587 bellard
    qemu_free(s->refcount_table);
1132 585f8587 bellard
    qemu_free(s->refcount_block);
1133 585f8587 bellard
    close(fd);
1134 585f8587 bellard
    return 0;
1135 585f8587 bellard
 fail:
1136 585f8587 bellard
    qemu_free(s->refcount_table);
1137 585f8587 bellard
    qemu_free(s->refcount_block);
1138 585f8587 bellard
    close(fd);
1139 585f8587 bellard
    return -ENOMEM;
1140 585f8587 bellard
}
1141 585f8587 bellard
1142 585f8587 bellard
static int qcow_make_empty(BlockDriverState *bs)
1143 585f8587 bellard
{
1144 585f8587 bellard
#if 0
1145 585f8587 bellard
    /* XXX: not correct */
1146 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1147 585f8587 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1148 585f8587 bellard
    int ret;
1149 585f8587 bellard

1150 585f8587 bellard
    memset(s->l1_table, 0, l1_length);
1151 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1152 585f8587 bellard
        return -1;
1153 585f8587 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1154 585f8587 bellard
    if (ret < 0)
1155 585f8587 bellard
        return ret;
1156 5fafdf24 ths
   
1157 585f8587 bellard
    l2_cache_reset(bs);
1158 585f8587 bellard
#endif
1159 585f8587 bellard
    return 0;
1160 585f8587 bellard
}
1161 585f8587 bellard
1162 585f8587 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
1163 585f8587 bellard
   tables to avoid losing bytes in alignment */
1164 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1165 585f8587 bellard
                                 const uint8_t *buf, int nb_sectors)
1166 585f8587 bellard
{
1167 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1168 585f8587 bellard
    z_stream strm;
1169 585f8587 bellard
    int ret, out_len;
1170 585f8587 bellard
    uint8_t *out_buf;
1171 585f8587 bellard
    uint64_t cluster_offset;
1172 585f8587 bellard
1173 585f8587 bellard
    if (nb_sectors == 0) {
1174 585f8587 bellard
        /* align end of file to a sector boundary to ease reading with
1175 585f8587 bellard
           sector based I/Os */
1176 585f8587 bellard
        cluster_offset = bdrv_getlength(s->hd);
1177 585f8587 bellard
        cluster_offset = (cluster_offset + 511) & ~511;
1178 585f8587 bellard
        bdrv_truncate(s->hd, cluster_offset);
1179 585f8587 bellard
        return 0;
1180 585f8587 bellard
    }
1181 585f8587 bellard
1182 585f8587 bellard
    if (nb_sectors != s->cluster_sectors)
1183 585f8587 bellard
        return -EINVAL;
1184 585f8587 bellard
1185 585f8587 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1186 585f8587 bellard
    if (!out_buf)
1187 585f8587 bellard
        return -ENOMEM;
1188 585f8587 bellard
1189 585f8587 bellard
    /* best compression, small window, no zlib header */
1190 585f8587 bellard
    memset(&strm, 0, sizeof(strm));
1191 585f8587 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1192 5fafdf24 ths
                       Z_DEFLATED, -12,
1193 585f8587 bellard
                       9, Z_DEFAULT_STRATEGY);
1194 585f8587 bellard
    if (ret != 0) {
1195 585f8587 bellard
        qemu_free(out_buf);
1196 585f8587 bellard
        return -1;
1197 585f8587 bellard
    }
1198 585f8587 bellard
1199 585f8587 bellard
    strm.avail_in = s->cluster_size;
1200 585f8587 bellard
    strm.next_in = (uint8_t *)buf;
1201 585f8587 bellard
    strm.avail_out = s->cluster_size;
1202 585f8587 bellard
    strm.next_out = out_buf;
1203 585f8587 bellard
1204 585f8587 bellard
    ret = deflate(&strm, Z_FINISH);
1205 585f8587 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
1206 585f8587 bellard
        qemu_free(out_buf);
1207 585f8587 bellard
        deflateEnd(&strm);
1208 585f8587 bellard
        return -1;
1209 585f8587 bellard
    }
1210 585f8587 bellard
    out_len = strm.next_out - out_buf;
1211 585f8587 bellard
1212 585f8587 bellard
    deflateEnd(&strm);
1213 585f8587 bellard
1214 585f8587 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1215 585f8587 bellard
        /* could not compress: write normal cluster */
1216 585f8587 bellard
        qcow_write(bs, sector_num, buf, s->cluster_sectors);
1217 585f8587 bellard
    } else {
1218 5fafdf24 ths
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
1219 585f8587 bellard
                                            out_len, 0, 0);
1220 585f8587 bellard
        cluster_offset &= s->cluster_offset_mask;
1221 585f8587 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1222 585f8587 bellard
            qemu_free(out_buf);
1223 585f8587 bellard
            return -1;
1224 585f8587 bellard
        }
1225 585f8587 bellard
    }
1226 5fafdf24 ths
   
1227 585f8587 bellard
    qemu_free(out_buf);
1228 585f8587 bellard
    return 0;
1229 585f8587 bellard
}
1230 585f8587 bellard
1231 585f8587 bellard
static void qcow_flush(BlockDriverState *bs)
1232 585f8587 bellard
{
1233 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1234 585f8587 bellard
    bdrv_flush(s->hd);
1235 585f8587 bellard
}
1236 585f8587 bellard
1237 585f8587 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1238 585f8587 bellard
{
1239 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1240 585f8587 bellard
    bdi->cluster_size = s->cluster_size;
1241 5fafdf24 ths
    bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1242 585f8587 bellard
        (s->cluster_bits + s->l2_bits);
1243 585f8587 bellard
    return 0;
1244 585f8587 bellard
}
1245 585f8587 bellard
1246 585f8587 bellard
/*********************************************************/
1247 585f8587 bellard
/* snapshot support */
1248 585f8587 bellard
1249 585f8587 bellard
/* update the refcounts of snapshots and the copied flag */
1250 5fafdf24 ths
static int update_snapshot_refcount(BlockDriverState *bs,
1251 585f8587 bellard
                                    int64_t l1_table_offset,
1252 585f8587 bellard
                                    int l1_size,
1253 585f8587 bellard
                                    int addend)
1254 585f8587 bellard
{
1255 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1256 585f8587 bellard
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1257 585f8587 bellard
    int64_t old_offset, old_l2_offset;
1258 585f8587 bellard
    int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
1259 5fafdf24 ths
   
1260 585f8587 bellard
    l2_cache_reset(bs);
1261 585f8587 bellard
1262 585f8587 bellard
    l2_table = NULL;
1263 585f8587 bellard
    l1_table = NULL;
1264 585f8587 bellard
    l1_size2 = l1_size * sizeof(uint64_t);
1265 585f8587 bellard
    l1_allocated = 0;
1266 585f8587 bellard
    if (l1_table_offset != s->l1_table_offset) {
1267 585f8587 bellard
        l1_table = qemu_malloc(l1_size2);
1268 585f8587 bellard
        if (!l1_table)
1269 585f8587 bellard
            goto fail;
1270 585f8587 bellard
        l1_allocated = 1;
1271 5fafdf24 ths
        if (bdrv_pread(s->hd, l1_table_offset,
1272 585f8587 bellard
                       l1_table, l1_size2) != l1_size2)
1273 585f8587 bellard
            goto fail;
1274 585f8587 bellard
        for(i = 0;i < l1_size; i++)
1275 585f8587 bellard
            be64_to_cpus(&l1_table[i]);
1276 585f8587 bellard
    } else {
1277 585f8587 bellard
        assert(l1_size == s->l1_size);
1278 585f8587 bellard
        l1_table = s->l1_table;
1279 585f8587 bellard
        l1_allocated = 0;
1280 585f8587 bellard
    }
1281 5fafdf24 ths
   
1282 585f8587 bellard
    l2_size = s->l2_size * sizeof(uint64_t);
1283 585f8587 bellard
    l2_table = qemu_malloc(l2_size);
1284 585f8587 bellard
    if (!l2_table)
1285 585f8587 bellard
        goto fail;
1286 585f8587 bellard
    l1_modified = 0;
1287 585f8587 bellard
    for(i = 0; i < l1_size; i++) {
1288 585f8587 bellard
        l2_offset = l1_table[i];
1289 585f8587 bellard
        if (l2_offset) {
1290 585f8587 bellard
            old_l2_offset = l2_offset;
1291 585f8587 bellard
            l2_offset &= ~QCOW_OFLAG_COPIED;
1292 585f8587 bellard
            l2_modified = 0;
1293 585f8587 bellard
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1294 585f8587 bellard
                goto fail;
1295 585f8587 bellard
            for(j = 0; j < s->l2_size; j++) {
1296 585f8587 bellard
                offset = be64_to_cpu(l2_table[j]);
1297 585f8587 bellard
                if (offset != 0) {
1298 585f8587 bellard
                    old_offset = offset;
1299 585f8587 bellard
                    offset &= ~QCOW_OFLAG_COPIED;
1300 585f8587 bellard
                    if (offset & QCOW_OFLAG_COMPRESSED) {
1301 5fafdf24 ths
                        nb_csectors = ((offset >> s->csize_shift) &
1302 585f8587 bellard
                                       s->csize_mask) + 1;
1303 585f8587 bellard
                        if (addend != 0)
1304 585f8587 bellard
                            update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1305 585f8587 bellard
                                            nb_csectors * 512, addend);
1306 585f8587 bellard
                        /* compressed clusters are never modified */
1307 5fafdf24 ths
                        refcount = 2;
1308 585f8587 bellard
                    } else {
1309 585f8587 bellard
                        if (addend != 0) {
1310 585f8587 bellard
                            refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1311 585f8587 bellard
                        } else {
1312 585f8587 bellard
                            refcount = get_refcount(bs, offset >> s->cluster_bits);
1313 585f8587 bellard
                        }
1314 585f8587 bellard
                    }
1315 585f8587 bellard
1316 585f8587 bellard
                    if (refcount == 1) {
1317 585f8587 bellard
                        offset |= QCOW_OFLAG_COPIED;
1318 585f8587 bellard
                    }
1319 585f8587 bellard
                    if (offset != old_offset) {
1320 585f8587 bellard
                        l2_table[j] = cpu_to_be64(offset);
1321 585f8587 bellard
                        l2_modified = 1;
1322 585f8587 bellard
                    }
1323 585f8587 bellard
                }
1324 585f8587 bellard
            }
1325 585f8587 bellard
            if (l2_modified) {
1326 5fafdf24 ths
                if (bdrv_pwrite(s->hd,
1327 585f8587 bellard
                                l2_offset, l2_table, l2_size) != l2_size)
1328 585f8587 bellard
                    goto fail;
1329 585f8587 bellard
            }
1330 585f8587 bellard
1331 585f8587 bellard
            if (addend != 0) {
1332 585f8587 bellard
                refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1333 585f8587 bellard
            } else {
1334 585f8587 bellard
                refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1335 585f8587 bellard
            }
1336 585f8587 bellard
            if (refcount == 1) {
1337 585f8587 bellard
                l2_offset |= QCOW_OFLAG_COPIED;
1338 585f8587 bellard
            }
1339 585f8587 bellard
            if (l2_offset != old_l2_offset) {
1340 585f8587 bellard
                l1_table[i] = l2_offset;
1341 585f8587 bellard
                l1_modified = 1;
1342 585f8587 bellard
            }
1343 585f8587 bellard
        }
1344 585f8587 bellard
    }
1345 585f8587 bellard
    if (l1_modified) {
1346 585f8587 bellard
        for(i = 0; i < l1_size; i++)
1347 585f8587 bellard
            cpu_to_be64s(&l1_table[i]);
1348 5fafdf24 ths
        if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
1349 585f8587 bellard
                        l1_size2) != l1_size2)
1350 585f8587 bellard
            goto fail;
1351 585f8587 bellard
        for(i = 0; i < l1_size; i++)
1352 585f8587 bellard
            be64_to_cpus(&l1_table[i]);
1353 585f8587 bellard
    }
1354 585f8587 bellard
    if (l1_allocated)
1355 585f8587 bellard
        qemu_free(l1_table);
1356 585f8587 bellard
    qemu_free(l2_table);
1357 585f8587 bellard
    return 0;
1358 585f8587 bellard
 fail:
1359 585f8587 bellard
    if (l1_allocated)
1360 585f8587 bellard
        qemu_free(l1_table);
1361 585f8587 bellard
    qemu_free(l2_table);
1362 585f8587 bellard
    return -EIO;
1363 585f8587 bellard
}
1364 585f8587 bellard
1365 585f8587 bellard
static void qcow_free_snapshots(BlockDriverState *bs)
1366 585f8587 bellard
{
1367 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1368 585f8587 bellard
    int i;
1369 585f8587 bellard
1370 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1371 585f8587 bellard
        qemu_free(s->snapshots[i].name);
1372 585f8587 bellard
        qemu_free(s->snapshots[i].id_str);
1373 585f8587 bellard
    }
1374 585f8587 bellard
    qemu_free(s->snapshots);
1375 585f8587 bellard
    s->snapshots = NULL;
1376 585f8587 bellard
    s->nb_snapshots = 0;
1377 585f8587 bellard
}
1378 585f8587 bellard
1379 585f8587 bellard
static int qcow_read_snapshots(BlockDriverState *bs)
1380 585f8587 bellard
{
1381 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1382 585f8587 bellard
    QCowSnapshotHeader h;
1383 585f8587 bellard
    QCowSnapshot *sn;
1384 585f8587 bellard
    int i, id_str_size, name_size;
1385 585f8587 bellard
    int64_t offset;
1386 585f8587 bellard
    uint32_t extra_data_size;
1387 585f8587 bellard
1388 585f8587 bellard
    offset = s->snapshots_offset;
1389 585f8587 bellard
    s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1390 585f8587 bellard
    if (!s->snapshots)
1391 585f8587 bellard
        goto fail;
1392 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1393 585f8587 bellard
        offset = align_offset(offset, 8);
1394 585f8587 bellard
        if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1395 585f8587 bellard
            goto fail;
1396 585f8587 bellard
        offset += sizeof(h);
1397 585f8587 bellard
        sn = s->snapshots + i;
1398 585f8587 bellard
        sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1399 585f8587 bellard
        sn->l1_size = be32_to_cpu(h.l1_size);
1400 585f8587 bellard
        sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1401 585f8587 bellard
        sn->date_sec = be32_to_cpu(h.date_sec);
1402 585f8587 bellard
        sn->date_nsec = be32_to_cpu(h.date_nsec);
1403 585f8587 bellard
        sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1404 585f8587 bellard
        extra_data_size = be32_to_cpu(h.extra_data_size);
1405 585f8587 bellard
1406 585f8587 bellard
        id_str_size = be16_to_cpu(h.id_str_size);
1407 585f8587 bellard
        name_size = be16_to_cpu(h.name_size);
1408 585f8587 bellard
1409 585f8587 bellard
        offset += extra_data_size;
1410 585f8587 bellard
1411 585f8587 bellard
        sn->id_str = qemu_malloc(id_str_size + 1);
1412 585f8587 bellard
        if (!sn->id_str)
1413 585f8587 bellard
            goto fail;
1414 585f8587 bellard
        if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1415 585f8587 bellard
            goto fail;
1416 585f8587 bellard
        offset += id_str_size;
1417 585f8587 bellard
        sn->id_str[id_str_size] = '\0';
1418 585f8587 bellard
1419 585f8587 bellard
        sn->name = qemu_malloc(name_size + 1);
1420 585f8587 bellard
        if (!sn->name)
1421 585f8587 bellard
            goto fail;
1422 585f8587 bellard
        if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1423 585f8587 bellard
            goto fail;
1424 585f8587 bellard
        offset += name_size;
1425 585f8587 bellard
        sn->name[name_size] = '\0';
1426 585f8587 bellard
    }
1427 585f8587 bellard
    s->snapshots_size = offset - s->snapshots_offset;
1428 585f8587 bellard
    return 0;
1429 585f8587 bellard
 fail:
1430 585f8587 bellard
    qcow_free_snapshots(bs);
1431 585f8587 bellard
    return -1;
1432 585f8587 bellard
}
1433 585f8587 bellard
1434 585f8587 bellard
/* add at the end of the file a new list of snapshots */
1435 585f8587 bellard
static int qcow_write_snapshots(BlockDriverState *bs)
1436 585f8587 bellard
{
1437 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1438 585f8587 bellard
    QCowSnapshot *sn;
1439 585f8587 bellard
    QCowSnapshotHeader h;
1440 585f8587 bellard
    int i, name_size, id_str_size, snapshots_size;
1441 585f8587 bellard
    uint64_t data64;
1442 585f8587 bellard
    uint32_t data32;
1443 585f8587 bellard
    int64_t offset, snapshots_offset;
1444 585f8587 bellard
1445 585f8587 bellard
    /* compute the size of the snapshots */
1446 585f8587 bellard
    offset = 0;
1447 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1448 585f8587 bellard
        sn = s->snapshots + i;
1449 585f8587 bellard
        offset = align_offset(offset, 8);
1450 585f8587 bellard
        offset += sizeof(h);
1451 585f8587 bellard
        offset += strlen(sn->id_str);
1452 585f8587 bellard
        offset += strlen(sn->name);
1453 585f8587 bellard
    }
1454 585f8587 bellard
    snapshots_size = offset;
1455 585f8587 bellard
1456 585f8587 bellard
    snapshots_offset = alloc_clusters(bs, snapshots_size);
1457 585f8587 bellard
    offset = snapshots_offset;
1458 5fafdf24 ths
   
1459 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1460 585f8587 bellard
        sn = s->snapshots + i;
1461 585f8587 bellard
        memset(&h, 0, sizeof(h));
1462 585f8587 bellard
        h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1463 585f8587 bellard
        h.l1_size = cpu_to_be32(sn->l1_size);
1464 585f8587 bellard
        h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1465 585f8587 bellard
        h.date_sec = cpu_to_be32(sn->date_sec);
1466 585f8587 bellard
        h.date_nsec = cpu_to_be32(sn->date_nsec);
1467 585f8587 bellard
        h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
1468 5fafdf24 ths
       
1469 585f8587 bellard
        id_str_size = strlen(sn->id_str);
1470 585f8587 bellard
        name_size = strlen(sn->name);
1471 585f8587 bellard
        h.id_str_size = cpu_to_be16(id_str_size);
1472 585f8587 bellard
        h.name_size = cpu_to_be16(name_size);
1473 585f8587 bellard
        offset = align_offset(offset, 8);
1474 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1475 585f8587 bellard
            goto fail;
1476 585f8587 bellard
        offset += sizeof(h);
1477 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1478 585f8587 bellard
            goto fail;
1479 585f8587 bellard
        offset += id_str_size;
1480 585f8587 bellard
        if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1481 585f8587 bellard
            goto fail;
1482 585f8587 bellard
        offset += name_size;
1483 585f8587 bellard
    }
1484 585f8587 bellard
1485 585f8587 bellard
    /* update the various header fields */
1486 585f8587 bellard
    data64 = cpu_to_be64(snapshots_offset);
1487 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1488 585f8587 bellard
                    &data64, sizeof(data64)) != sizeof(data64))
1489 585f8587 bellard
        goto fail;
1490 585f8587 bellard
    data32 = cpu_to_be32(s->nb_snapshots);
1491 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1492 585f8587 bellard
                    &data32, sizeof(data32)) != sizeof(data32))
1493 585f8587 bellard
        goto fail;
1494 585f8587 bellard
1495 585f8587 bellard
    /* free the old snapshot table */
1496 585f8587 bellard
    free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1497 585f8587 bellard
    s->snapshots_offset = snapshots_offset;
1498 585f8587 bellard
    s->snapshots_size = snapshots_size;
1499 585f8587 bellard
    return 0;
1500 585f8587 bellard
 fail:
1501 585f8587 bellard
    return -1;
1502 585f8587 bellard
}
1503 585f8587 bellard
1504 585f8587 bellard
static void find_new_snapshot_id(BlockDriverState *bs,
1505 585f8587 bellard
                                 char *id_str, int id_str_size)
1506 585f8587 bellard
{
1507 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1508 585f8587 bellard
    QCowSnapshot *sn;
1509 585f8587 bellard
    int i, id, id_max = 0;
1510 585f8587 bellard
1511 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1512 585f8587 bellard
        sn = s->snapshots + i;
1513 585f8587 bellard
        id = strtoul(sn->id_str, NULL, 10);
1514 585f8587 bellard
        if (id > id_max)
1515 585f8587 bellard
            id_max = id;
1516 585f8587 bellard
    }
1517 585f8587 bellard
    snprintf(id_str, id_str_size, "%d", id_max + 1);
1518 585f8587 bellard
}
1519 585f8587 bellard
1520 585f8587 bellard
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1521 585f8587 bellard
{
1522 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1523 585f8587 bellard
    int i;
1524 585f8587 bellard
1525 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1526 585f8587 bellard
        if (!strcmp(s->snapshots[i].id_str, id_str))
1527 585f8587 bellard
            return i;
1528 585f8587 bellard
    }
1529 585f8587 bellard
    return -1;
1530 585f8587 bellard
}
1531 585f8587 bellard
1532 585f8587 bellard
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1533 585f8587 bellard
{
1534 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1535 585f8587 bellard
    int i, ret;
1536 5fafdf24 ths
   
1537 585f8587 bellard
    ret = find_snapshot_by_id(bs, name);
1538 585f8587 bellard
    if (ret >= 0)
1539 585f8587 bellard
        return ret;
1540 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1541 585f8587 bellard
        if (!strcmp(s->snapshots[i].name, name))
1542 585f8587 bellard
            return i;
1543 585f8587 bellard
    }
1544 585f8587 bellard
    return -1;
1545 585f8587 bellard
}
1546 585f8587 bellard
1547 585f8587 bellard
/* if no id is provided, a new one is constructed */
1548 5fafdf24 ths
static int qcow_snapshot_create(BlockDriverState *bs,
1549 585f8587 bellard
                                QEMUSnapshotInfo *sn_info)
1550 585f8587 bellard
{
1551 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1552 585f8587 bellard
    QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1553 585f8587 bellard
    int i, ret;
1554 585f8587 bellard
    uint64_t *l1_table = NULL;
1555 5fafdf24 ths
   
1556 585f8587 bellard
    memset(sn, 0, sizeof(*sn));
1557 585f8587 bellard
1558 585f8587 bellard
    if (sn_info->id_str[0] == '\0') {
1559 585f8587 bellard
        /* compute a new id */
1560 585f8587 bellard
        find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
1561 585f8587 bellard
    }
1562 585f8587 bellard
1563 585f8587 bellard
    /* check that the ID is unique */
1564 585f8587 bellard
    if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
1565 585f8587 bellard
        return -ENOENT;
1566 585f8587 bellard
1567 585f8587 bellard
    sn->id_str = qemu_strdup(sn_info->id_str);
1568 585f8587 bellard
    if (!sn->id_str)
1569 585f8587 bellard
        goto fail;
1570 585f8587 bellard
    sn->name = qemu_strdup(sn_info->name);
1571 585f8587 bellard
    if (!sn->name)
1572 585f8587 bellard
        goto fail;
1573 585f8587 bellard
    sn->vm_state_size = sn_info->vm_state_size;
1574 585f8587 bellard
    sn->date_sec = sn_info->date_sec;
1575 585f8587 bellard
    sn->date_nsec = sn_info->date_nsec;
1576 585f8587 bellard
    sn->vm_clock_nsec = sn_info->vm_clock_nsec;
1577 585f8587 bellard
1578 585f8587 bellard
    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
1579 585f8587 bellard
    if (ret < 0)
1580 585f8587 bellard
        goto fail;
1581 585f8587 bellard
1582 585f8587 bellard
    /* create the L1 table of the snapshot */
1583 585f8587 bellard
    sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
1584 585f8587 bellard
    sn->l1_size = s->l1_size;
1585 585f8587 bellard
1586 585f8587 bellard
    l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
1587 585f8587 bellard
    if (!l1_table)
1588 585f8587 bellard
        goto fail;
1589 585f8587 bellard
    for(i = 0; i < s->l1_size; i++) {
1590 585f8587 bellard
        l1_table[i] = cpu_to_be64(s->l1_table[i]);
1591 585f8587 bellard
    }
1592 585f8587 bellard
    if (bdrv_pwrite(s->hd, sn->l1_table_offset,
1593 5fafdf24 ths
                    l1_table, s->l1_size * sizeof(uint64_t)) !=
1594 585f8587 bellard
        (s->l1_size * sizeof(uint64_t)))
1595 585f8587 bellard
        goto fail;
1596 585f8587 bellard
    qemu_free(l1_table);
1597 585f8587 bellard
    l1_table = NULL;
1598 585f8587 bellard
1599 585f8587 bellard
    snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
1600 585f8587 bellard
    if (!snapshots1)
1601 585f8587 bellard
        goto fail;
1602 585f8587 bellard
    memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
1603 585f8587 bellard
    s->snapshots = snapshots1;
1604 585f8587 bellard
    s->snapshots[s->nb_snapshots++] = *sn;
1605 585f8587 bellard
1606 585f8587 bellard
    if (qcow_write_snapshots(bs) < 0)
1607 585f8587 bellard
        goto fail;
1608 585f8587 bellard
#ifdef DEBUG_ALLOC
1609 585f8587 bellard
    check_refcounts(bs);
1610 585f8587 bellard
#endif
1611 585f8587 bellard
    return 0;
1612 585f8587 bellard
 fail:
1613 585f8587 bellard
    qemu_free(sn->name);
1614 585f8587 bellard
    qemu_free(l1_table);
1615 585f8587 bellard
    return -1;
1616 585f8587 bellard
}
1617 585f8587 bellard
1618 585f8587 bellard
/* copy the snapshot 'snapshot_name' into the current disk image */
1619 5fafdf24 ths
static int qcow_snapshot_goto(BlockDriverState *bs,
1620 585f8587 bellard
                              const char *snapshot_id)
1621 585f8587 bellard
{
1622 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1623 585f8587 bellard
    QCowSnapshot *sn;
1624 585f8587 bellard
    int i, snapshot_index, l1_size2;
1625 585f8587 bellard
1626 585f8587 bellard
    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
1627 585f8587 bellard
    if (snapshot_index < 0)
1628 585f8587 bellard
        return -ENOENT;
1629 585f8587 bellard
    sn = &s->snapshots[snapshot_index];
1630 585f8587 bellard
1631 585f8587 bellard
    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
1632 585f8587 bellard
        goto fail;
1633 585f8587 bellard
1634 585f8587 bellard
    if (grow_l1_table(bs, sn->l1_size) < 0)
1635 585f8587 bellard
        goto fail;
1636 585f8587 bellard
1637 585f8587 bellard
    s->l1_size = sn->l1_size;
1638 585f8587 bellard
    l1_size2 = s->l1_size * sizeof(uint64_t);
1639 585f8587 bellard
    /* copy the snapshot l1 table to the current l1 table */
1640 5fafdf24 ths
    if (bdrv_pread(s->hd, sn->l1_table_offset,
1641 585f8587 bellard
                   s->l1_table, l1_size2) != l1_size2)
1642 585f8587 bellard
        goto fail;
1643 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset,
1644 585f8587 bellard
                    s->l1_table, l1_size2) != l1_size2)
1645 585f8587 bellard
        goto fail;
1646 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
1647 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
1648 585f8587 bellard
    }
1649 585f8587 bellard
1650 585f8587 bellard
    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
1651 585f8587 bellard
        goto fail;
1652 585f8587 bellard
1653 585f8587 bellard
#ifdef DEBUG_ALLOC
1654 585f8587 bellard
    check_refcounts(bs);
1655 585f8587 bellard
#endif
1656 585f8587 bellard
    return 0;
1657 585f8587 bellard
 fail:
1658 585f8587 bellard
    return -EIO;
1659 585f8587 bellard
}
1660 585f8587 bellard
1661 585f8587 bellard
static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1662 585f8587 bellard
{
1663 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1664 585f8587 bellard
    QCowSnapshot *sn;
1665 585f8587 bellard
    int snapshot_index, ret;
1666 5fafdf24 ths
   
1667 585f8587 bellard
    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
1668 585f8587 bellard
    if (snapshot_index < 0)
1669 585f8587 bellard
        return -ENOENT;
1670 585f8587 bellard
    sn = &s->snapshots[snapshot_index];
1671 585f8587 bellard
1672 585f8587 bellard
    ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
1673 585f8587 bellard
    if (ret < 0)
1674 585f8587 bellard
        return ret;
1675 585f8587 bellard
    /* must update the copied flag on the current cluster offsets */
1676 585f8587 bellard
    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
1677 585f8587 bellard
    if (ret < 0)
1678 585f8587 bellard
        return ret;
1679 585f8587 bellard
    free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
1680 585f8587 bellard
1681 585f8587 bellard
    qemu_free(sn->id_str);
1682 585f8587 bellard
    qemu_free(sn->name);
1683 585f8587 bellard
    memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
1684 585f8587 bellard
    s->nb_snapshots--;
1685 585f8587 bellard
    ret = qcow_write_snapshots(bs);
1686 585f8587 bellard
    if (ret < 0) {
1687 585f8587 bellard
        /* XXX: restore snapshot if error ? */
1688 585f8587 bellard
        return ret;
1689 585f8587 bellard
    }
1690 585f8587 bellard
#ifdef DEBUG_ALLOC
1691 585f8587 bellard
    check_refcounts(bs);
1692 585f8587 bellard
#endif
1693 585f8587 bellard
    return 0;
1694 585f8587 bellard
}
1695 585f8587 bellard
1696 5fafdf24 ths
static int qcow_snapshot_list(BlockDriverState *bs,
1697 585f8587 bellard
                              QEMUSnapshotInfo **psn_tab)
1698 585f8587 bellard
{
1699 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1700 585f8587 bellard
    QEMUSnapshotInfo *sn_tab, *sn_info;
1701 585f8587 bellard
    QCowSnapshot *sn;
1702 585f8587 bellard
    int i;
1703 585f8587 bellard
1704 585f8587 bellard
    sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
1705 585f8587 bellard
    if (!sn_tab)
1706 585f8587 bellard
        goto fail;
1707 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
1708 585f8587 bellard
        sn_info = sn_tab + i;
1709 585f8587 bellard
        sn = s->snapshots + i;
1710 585f8587 bellard
        pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
1711 585f8587 bellard
                sn->id_str);
1712 585f8587 bellard
        pstrcpy(sn_info->name, sizeof(sn_info->name),
1713 585f8587 bellard
                sn->name);
1714 585f8587 bellard
        sn_info->vm_state_size = sn->vm_state_size;
1715 585f8587 bellard
        sn_info->date_sec = sn->date_sec;
1716 585f8587 bellard
        sn_info->date_nsec = sn->date_nsec;
1717 585f8587 bellard
        sn_info->vm_clock_nsec = sn->vm_clock_nsec;
1718 585f8587 bellard
    }
1719 585f8587 bellard
    *psn_tab = sn_tab;
1720 585f8587 bellard
    return s->nb_snapshots;
1721 585f8587 bellard
 fail:
1722 585f8587 bellard
    qemu_free(sn_tab);
1723 585f8587 bellard
    *psn_tab = NULL;
1724 585f8587 bellard
    return -ENOMEM;
1725 585f8587 bellard
}
1726 585f8587 bellard
1727 585f8587 bellard
/*********************************************************/
1728 585f8587 bellard
/* refcount handling */
1729 585f8587 bellard
1730 585f8587 bellard
static int refcount_init(BlockDriverState *bs)
1731 585f8587 bellard
{
1732 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1733 585f8587 bellard
    int ret, refcount_table_size2, i;
1734 5fafdf24 ths
   
1735 585f8587 bellard
    s->refcount_block_cache = qemu_malloc(s->cluster_size);
1736 585f8587 bellard
    if (!s->refcount_block_cache)
1737 585f8587 bellard
        goto fail;
1738 585f8587 bellard
    refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
1739 585f8587 bellard
    s->refcount_table = qemu_malloc(refcount_table_size2);
1740 585f8587 bellard
    if (!s->refcount_table)
1741 585f8587 bellard
        goto fail;
1742 585f8587 bellard
    if (s->refcount_table_size > 0) {
1743 585f8587 bellard
        ret = bdrv_pread(s->hd, s->refcount_table_offset,
1744 585f8587 bellard
                         s->refcount_table, refcount_table_size2);
1745 585f8587 bellard
        if (ret != refcount_table_size2)
1746 585f8587 bellard
            goto fail;
1747 585f8587 bellard
        for(i = 0; i < s->refcount_table_size; i++)
1748 585f8587 bellard
            be64_to_cpus(&s->refcount_table[i]);
1749 585f8587 bellard
    }
1750 585f8587 bellard
    return 0;
1751 585f8587 bellard
 fail:
1752 585f8587 bellard
    return -ENOMEM;
1753 585f8587 bellard
}
1754 585f8587 bellard
1755 585f8587 bellard
static void refcount_close(BlockDriverState *bs)
1756 585f8587 bellard
{
1757 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1758 585f8587 bellard
    qemu_free(s->refcount_block_cache);
1759 585f8587 bellard
    qemu_free(s->refcount_table);
1760 585f8587 bellard
}
1761 585f8587 bellard
1762 585f8587 bellard
1763 5fafdf24 ths
static int load_refcount_block(BlockDriverState *bs,
1764 585f8587 bellard
                               int64_t refcount_block_offset)
1765 585f8587 bellard
{
1766 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1767 585f8587 bellard
    int ret;
1768 5fafdf24 ths
    ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
1769 585f8587 bellard
                     s->cluster_size);
1770 585f8587 bellard
    if (ret != s->cluster_size)
1771 585f8587 bellard
        return -EIO;
1772 585f8587 bellard
    s->refcount_block_cache_offset = refcount_block_offset;
1773 585f8587 bellard
    return 0;
1774 585f8587 bellard
}
1775 585f8587 bellard
1776 585f8587 bellard
static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
1777 585f8587 bellard
{
1778 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1779 585f8587 bellard
    int refcount_table_index, block_index;
1780 585f8587 bellard
    int64_t refcount_block_offset;
1781 585f8587 bellard
1782 585f8587 bellard
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
1783 585f8587 bellard
    if (refcount_table_index >= s->refcount_table_size)
1784 585f8587 bellard
        return 0;
1785 585f8587 bellard
    refcount_block_offset = s->refcount_table[refcount_table_index];
1786 585f8587 bellard
    if (!refcount_block_offset)
1787 585f8587 bellard
        return 0;
1788 585f8587 bellard
    if (refcount_block_offset != s->refcount_block_cache_offset) {
1789 585f8587 bellard
        /* better than nothing: return allocated if read error */
1790 585f8587 bellard
        if (load_refcount_block(bs, refcount_block_offset) < 0)
1791 585f8587 bellard
            return 1;
1792 585f8587 bellard
    }
1793 5fafdf24 ths
    block_index = cluster_index &
1794 585f8587 bellard
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
1795 585f8587 bellard
    return be16_to_cpu(s->refcount_block_cache[block_index]);
1796 585f8587 bellard
}
1797 585f8587 bellard
1798 585f8587 bellard
/* return < 0 if error */
1799 585f8587 bellard
static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
1800 585f8587 bellard
{
1801 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1802 585f8587 bellard
    int i, nb_clusters;
1803 585f8587 bellard
1804 585f8587 bellard
    nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
1805 585f8587 bellard
    for(;;) {
1806 585f8587 bellard
        if (get_refcount(bs, s->free_cluster_index) == 0) {
1807 585f8587 bellard
            s->free_cluster_index++;
1808 585f8587 bellard
            for(i = 1; i < nb_clusters; i++) {
1809 585f8587 bellard
                if (get_refcount(bs, s->free_cluster_index) != 0)
1810 585f8587 bellard
                    goto not_found;
1811 585f8587 bellard
                s->free_cluster_index++;
1812 585f8587 bellard
            }
1813 585f8587 bellard
#ifdef DEBUG_ALLOC2
1814 585f8587 bellard
            printf("alloc_clusters: size=%lld -> %lld\n",
1815 5fafdf24 ths
                   size,
1816 585f8587 bellard
                   (s->free_cluster_index - nb_clusters) << s->cluster_bits);
1817 585f8587 bellard
#endif
1818 585f8587 bellard
            return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
1819 585f8587 bellard
        } else {
1820 585f8587 bellard
        not_found:
1821 585f8587 bellard
            s->free_cluster_index++;
1822 585f8587 bellard
        }
1823 585f8587 bellard
    }
1824 585f8587 bellard
}
1825 585f8587 bellard
1826 585f8587 bellard
static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
1827 585f8587 bellard
{
1828 585f8587 bellard
    int64_t offset;
1829 585f8587 bellard
1830 585f8587 bellard
    offset = alloc_clusters_noref(bs, size);
1831 585f8587 bellard
    update_refcount(bs, offset, size, 1);
1832 585f8587 bellard
    return offset;
1833 585f8587 bellard
}
1834 585f8587 bellard
1835 585f8587 bellard
/* only used to allocate compressed sectors. We try to allocate
1836 585f8587 bellard
   contiguous sectors. size must be <= cluster_size */
1837 585f8587 bellard
static int64_t alloc_bytes(BlockDriverState *bs, int size)
1838 585f8587 bellard
{
1839 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1840 585f8587 bellard
    int64_t offset, cluster_offset;
1841 585f8587 bellard
    int free_in_cluster;
1842 5fafdf24 ths
   
1843 585f8587 bellard
    assert(size > 0 && size <= s->cluster_size);
1844 585f8587 bellard
    if (s->free_byte_offset == 0) {
1845 585f8587 bellard
        s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
1846 585f8587 bellard
    }
1847 585f8587 bellard
 redo:
1848 5fafdf24 ths
    free_in_cluster = s->cluster_size -
1849 585f8587 bellard
        (s->free_byte_offset & (s->cluster_size - 1));
1850 585f8587 bellard
    if (size <= free_in_cluster) {
1851 585f8587 bellard
        /* enough space in current cluster */
1852 585f8587 bellard
        offset = s->free_byte_offset;
1853 585f8587 bellard
        s->free_byte_offset += size;
1854 585f8587 bellard
        free_in_cluster -= size;
1855 585f8587 bellard
        if (free_in_cluster == 0)
1856 585f8587 bellard
            s->free_byte_offset = 0;
1857 585f8587 bellard
        if ((offset & (s->cluster_size - 1)) != 0)
1858 585f8587 bellard
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
1859 585f8587 bellard
    } else {
1860 585f8587 bellard
        offset = alloc_clusters(bs, s->cluster_size);
1861 585f8587 bellard
        cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
1862 585f8587 bellard
        if ((cluster_offset + s->cluster_size) == offset) {
1863 585f8587 bellard
            /* we are lucky: contiguous data */
1864 585f8587 bellard
            offset = s->free_byte_offset;
1865 585f8587 bellard
            update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
1866 585f8587 bellard
            s->free_byte_offset += size;
1867 585f8587 bellard
        } else {
1868 585f8587 bellard
            s->free_byte_offset = offset;
1869 585f8587 bellard
            goto redo;
1870 585f8587 bellard
        }
1871 585f8587 bellard
    }
1872 585f8587 bellard
    return offset;
1873 585f8587 bellard
}
1874 585f8587 bellard
1875 5fafdf24 ths
static void free_clusters(BlockDriverState *bs,
1876 585f8587 bellard
                          int64_t offset, int64_t size)
1877 585f8587 bellard
{
1878 585f8587 bellard
    update_refcount(bs, offset, size, -1);
1879 585f8587 bellard
}
1880 585f8587 bellard
1881 585f8587 bellard
static int grow_refcount_table(BlockDriverState *bs, int min_size)
1882 585f8587 bellard
{
1883 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1884 585f8587 bellard
    int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
1885 585f8587 bellard
    uint64_t *new_table;
1886 585f8587 bellard
    int64_t table_offset;
1887 585f8587 bellard
    uint64_t data64;
1888 585f8587 bellard
    uint32_t data32;
1889 23be50f1 ths
    int old_table_size;
1890 23be50f1 ths
    int64_t old_table_offset;
1891 585f8587 bellard
1892 585f8587 bellard
    if (min_size <= s->refcount_table_size)
1893 585f8587 bellard
        return 0;
1894 585f8587 bellard
    /* compute new table size */
1895 585f8587 bellard
    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
1896 585f8587 bellard
    for(;;) {
1897 585f8587 bellard
        if (refcount_table_clusters == 0) {
1898 585f8587 bellard
            refcount_table_clusters = 1;
1899 585f8587 bellard
        } else {
1900 585f8587 bellard
            refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
1901 585f8587 bellard
        }
1902 585f8587 bellard
        new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
1903 585f8587 bellard
        if (min_size <= new_table_size)
1904 585f8587 bellard
            break;
1905 585f8587 bellard
    }
1906 15e6690a bellard
#ifdef DEBUG_ALLOC2
1907 15e6690a bellard
    printf("grow_refcount_table from %d to %d\n",
1908 15e6690a bellard
           s->refcount_table_size,
1909 15e6690a bellard
           new_table_size);
1910 15e6690a bellard
#endif
1911 585f8587 bellard
    new_table_size2 = new_table_size * sizeof(uint64_t);
1912 585f8587 bellard
    new_table = qemu_mallocz(new_table_size2);
1913 585f8587 bellard
    if (!new_table)
1914 585f8587 bellard
        return -ENOMEM;
1915 5fafdf24 ths
    memcpy(new_table, s->refcount_table,
1916 585f8587 bellard
           s->refcount_table_size * sizeof(uint64_t));
1917 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++)
1918 585f8587 bellard
        cpu_to_be64s(&new_table[i]);
1919 585f8587 bellard
    /* Note: we cannot update the refcount now to avoid recursion */
1920 585f8587 bellard
    table_offset = alloc_clusters_noref(bs, new_table_size2);
1921 585f8587 bellard
    ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
1922 5fafdf24 ths
    if (ret != new_table_size2)
1923 585f8587 bellard
        goto fail;
1924 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++)
1925 585f8587 bellard
        be64_to_cpus(&new_table[i]);
1926 585f8587 bellard
1927 585f8587 bellard
    data64 = cpu_to_be64(table_offset);
1928 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
1929 585f8587 bellard
                    &data64, sizeof(data64)) != sizeof(data64))
1930 585f8587 bellard
        goto fail;
1931 585f8587 bellard
    data32 = cpu_to_be32(refcount_table_clusters);
1932 585f8587 bellard
    if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_clusters),
1933 585f8587 bellard
                    &data32, sizeof(data32)) != sizeof(data32))
1934 585f8587 bellard
        goto fail;
1935 585f8587 bellard
    qemu_free(s->refcount_table);
1936 23be50f1 ths
    old_table_offset = s->refcount_table_offset;
1937 23be50f1 ths
    old_table_size = s->refcount_table_size;
1938 585f8587 bellard
    s->refcount_table = new_table;
1939 585f8587 bellard
    s->refcount_table_size = new_table_size;
1940 a4080ece ths
    s->refcount_table_offset = table_offset;
1941 585f8587 bellard
1942 585f8587 bellard
    update_refcount(bs, table_offset, new_table_size2, 1);
1943 23be50f1 ths
    free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
1944 585f8587 bellard
    return 0;
1945 585f8587 bellard
 fail:
1946 585f8587 bellard
    free_clusters(bs, table_offset, new_table_size2);
1947 585f8587 bellard
    qemu_free(new_table);
1948 585f8587 bellard
    return -EIO;
1949 585f8587 bellard
}
1950 585f8587 bellard
1951 585f8587 bellard
/* addend must be 1 or -1 */
1952 585f8587 bellard
/* XXX: cache several refcount block clusters ? */
1953 5fafdf24 ths
static int update_cluster_refcount(BlockDriverState *bs,
1954 585f8587 bellard
                                   int64_t cluster_index,
1955 585f8587 bellard
                                   int addend)
1956 585f8587 bellard
{
1957 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1958 585f8587 bellard
    int64_t offset, refcount_block_offset;
1959 585f8587 bellard
    int ret, refcount_table_index, block_index, refcount;
1960 585f8587 bellard
    uint64_t data64;
1961 585f8587 bellard
1962 585f8587 bellard
    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
1963 585f8587 bellard
    if (refcount_table_index >= s->refcount_table_size) {
1964 585f8587 bellard
        if (addend < 0)
1965 585f8587 bellard
            return -EINVAL;
1966 585f8587 bellard
        ret = grow_refcount_table(bs, refcount_table_index + 1);
1967 585f8587 bellard
        if (ret < 0)
1968 585f8587 bellard
            return ret;
1969 585f8587 bellard
    }
1970 585f8587 bellard
    refcount_block_offset = s->refcount_table[refcount_table_index];
1971 585f8587 bellard
    if (!refcount_block_offset) {
1972 585f8587 bellard
        if (addend < 0)
1973 585f8587 bellard
            return -EINVAL;
1974 585f8587 bellard
        /* create a new refcount block */
1975 585f8587 bellard
        /* Note: we cannot update the refcount now to avoid recursion */
1976 585f8587 bellard
        offset = alloc_clusters_noref(bs, s->cluster_size);
1977 585f8587 bellard
        memset(s->refcount_block_cache, 0, s->cluster_size);
1978 585f8587 bellard
        ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
1979 585f8587 bellard
        if (ret != s->cluster_size)
1980 585f8587 bellard
            return -EINVAL;
1981 585f8587 bellard
        s->refcount_table[refcount_table_index] = offset;
1982 585f8587 bellard
        data64 = cpu_to_be64(offset);
1983 5fafdf24 ths
        ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
1984 5fafdf24 ths
                          refcount_table_index * sizeof(uint64_t),
1985 585f8587 bellard
                          &data64, sizeof(data64));
1986 585f8587 bellard
        if (ret != sizeof(data64))
1987 585f8587 bellard
            return -EINVAL;
1988 585f8587 bellard
1989 585f8587 bellard
        refcount_block_offset = offset;
1990 585f8587 bellard
        s->refcount_block_cache_offset = offset;
1991 585f8587 bellard
        update_refcount(bs, offset, s->cluster_size, 1);
1992 585f8587 bellard
    } else {
1993 585f8587 bellard
        if (refcount_block_offset != s->refcount_block_cache_offset) {
1994 585f8587 bellard
            if (load_refcount_block(bs, refcount_block_offset) < 0)
1995 585f8587 bellard
                return -EIO;
1996 585f8587 bellard
        }
1997 585f8587 bellard
    }
1998 585f8587 bellard
    /* we can update the count and save it */
1999 5fafdf24 ths
    block_index = cluster_index &
2000 585f8587 bellard
        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2001 585f8587 bellard
    refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2002 585f8587 bellard
    refcount += addend;
2003 585f8587 bellard
    if (refcount < 0 || refcount > 0xffff)
2004 585f8587 bellard
        return -EINVAL;
2005 585f8587 bellard
    if (refcount == 0 && cluster_index < s->free_cluster_index) {
2006 585f8587 bellard
        s->free_cluster_index = cluster_index;
2007 585f8587 bellard
    }
2008 585f8587 bellard
    s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
2009 5fafdf24 ths
    if (bdrv_pwrite(s->hd,
2010 5fafdf24 ths
                    refcount_block_offset + (block_index << REFCOUNT_SHIFT),
2011 585f8587 bellard
                    &s->refcount_block_cache[block_index], 2) != 2)
2012 585f8587 bellard
        return -EIO;
2013 585f8587 bellard
    return refcount;
2014 585f8587 bellard
}
2015 585f8587 bellard
2016 5fafdf24 ths
static void update_refcount(BlockDriverState *bs,
2017 5fafdf24 ths
                            int64_t offset, int64_t length,
2018 585f8587 bellard
                            int addend)
2019 585f8587 bellard
{
2020 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2021 585f8587 bellard
    int64_t start, last, cluster_offset;
2022 585f8587 bellard
2023 585f8587 bellard
#ifdef DEBUG_ALLOC2
2024 5fafdf24 ths
    printf("update_refcount: offset=%lld size=%lld addend=%d\n",
2025 585f8587 bellard
           offset, length, addend);
2026 585f8587 bellard
#endif
2027 585f8587 bellard
    if (length <= 0)
2028 585f8587 bellard
        return;
2029 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
2030 585f8587 bellard
    last = (offset + length - 1) & ~(s->cluster_size - 1);
2031 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
2032 585f8587 bellard
        cluster_offset += s->cluster_size) {
2033 585f8587 bellard
        update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2034 585f8587 bellard
    }
2035 585f8587 bellard
}
2036 585f8587 bellard
2037 585f8587 bellard
#ifdef DEBUG_ALLOC
2038 5fafdf24 ths
static void inc_refcounts(BlockDriverState *bs,
2039 5fafdf24 ths
                          uint16_t *refcount_table,
2040 585f8587 bellard
                          int refcount_table_size,
2041 585f8587 bellard
                          int64_t offset, int64_t size)
2042 585f8587 bellard
{
2043 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2044 585f8587 bellard
    int64_t start, last, cluster_offset;
2045 585f8587 bellard
    int k;
2046 5fafdf24 ths
   
2047 585f8587 bellard
    if (size <= 0)
2048 585f8587 bellard
        return;
2049 585f8587 bellard
2050 585f8587 bellard
    start = offset & ~(s->cluster_size - 1);
2051 585f8587 bellard
    last = (offset + size - 1) & ~(s->cluster_size - 1);
2052 5fafdf24 ths
    for(cluster_offset = start; cluster_offset <= last;
2053 585f8587 bellard
        cluster_offset += s->cluster_size) {
2054 585f8587 bellard
        k = cluster_offset >> s->cluster_bits;
2055 585f8587 bellard
        if (k < 0 || k >= refcount_table_size) {
2056 585f8587 bellard
            printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2057 585f8587 bellard
        } else {
2058 585f8587 bellard
            if (++refcount_table[k] == 0) {
2059 585f8587 bellard
                printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2060 585f8587 bellard
            }
2061 585f8587 bellard
        }
2062 585f8587 bellard
    }
2063 585f8587 bellard
}
2064 585f8587 bellard
2065 5fafdf24 ths
static int check_refcounts_l1(BlockDriverState *bs,
2066 5fafdf24 ths
                              uint16_t *refcount_table,
2067 585f8587 bellard
                              int refcount_table_size,
2068 585f8587 bellard
                              int64_t l1_table_offset, int l1_size,
2069 585f8587 bellard
                              int check_copied)
2070 585f8587 bellard
{
2071 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2072 585f8587 bellard
    uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2073 585f8587 bellard
    int l2_size, i, j, nb_csectors, refcount;
2074 585f8587 bellard
2075 585f8587 bellard
    l2_table = NULL;
2076 585f8587 bellard
    l1_size2 = l1_size * sizeof(uint64_t);
2077 585f8587 bellard
2078 585f8587 bellard
    inc_refcounts(bs, refcount_table, refcount_table_size,
2079 585f8587 bellard
                  l1_table_offset, l1_size2);
2080 585f8587 bellard
2081 585f8587 bellard
    l1_table = qemu_malloc(l1_size2);
2082 585f8587 bellard
    if (!l1_table)
2083 585f8587 bellard
        goto fail;
2084 5fafdf24 ths
    if (bdrv_pread(s->hd, l1_table_offset,
2085 585f8587 bellard
                   l1_table, l1_size2) != l1_size2)
2086 585f8587 bellard
        goto fail;
2087 585f8587 bellard
    for(i = 0;i < l1_size; i++)
2088 585f8587 bellard
        be64_to_cpus(&l1_table[i]);
2089 5fafdf24 ths
   
2090 585f8587 bellard
    l2_size = s->l2_size * sizeof(uint64_t);
2091 585f8587 bellard
    l2_table = qemu_malloc(l2_size);
2092 585f8587 bellard
    if (!l2_table)
2093 585f8587 bellard
        goto fail;
2094 585f8587 bellard
    for(i = 0; i < l1_size; i++) {
2095 585f8587 bellard
        l2_offset = l1_table[i];
2096 585f8587 bellard
        if (l2_offset) {
2097 585f8587 bellard
            if (check_copied) {
2098 585f8587 bellard
                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2099 585f8587 bellard
                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2100 585f8587 bellard
                    printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2101 585f8587 bellard
                           l2_offset, refcount);
2102 585f8587 bellard
                }
2103 585f8587 bellard
            }
2104 585f8587 bellard
            l2_offset &= ~QCOW_OFLAG_COPIED;
2105 585f8587 bellard
            if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2106 585f8587 bellard
                goto fail;
2107 585f8587 bellard
            for(j = 0; j < s->l2_size; j++) {
2108 585f8587 bellard
                offset = be64_to_cpu(l2_table[j]);
2109 585f8587 bellard
                if (offset != 0) {
2110 585f8587 bellard
                    if (offset & QCOW_OFLAG_COMPRESSED) {
2111 585f8587 bellard
                        if (offset & QCOW_OFLAG_COPIED) {
2112 585f8587 bellard
                            printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2113 585f8587 bellard
                                   offset >> s->cluster_bits);
2114 585f8587 bellard
                            offset &= ~QCOW_OFLAG_COPIED;
2115 585f8587 bellard
                        }
2116 5fafdf24 ths
                        nb_csectors = ((offset >> s->csize_shift) &
2117 585f8587 bellard
                                       s->csize_mask) + 1;
2118 585f8587 bellard
                        offset &= s->cluster_offset_mask;
2119 5fafdf24 ths
                        inc_refcounts(bs, refcount_table,
2120 585f8587 bellard
                                      refcount_table_size,
2121 585f8587 bellard
                                      offset & ~511, nb_csectors * 512);
2122 585f8587 bellard
                    } else {
2123 585f8587 bellard
                        if (check_copied) {
2124 585f8587 bellard
                            refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2125 585f8587 bellard
                            if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2126 585f8587 bellard
                                printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2127 585f8587 bellard
                                       offset, refcount);
2128 585f8587 bellard
                            }
2129 585f8587 bellard
                        }
2130 585f8587 bellard
                        offset &= ~QCOW_OFLAG_COPIED;
2131 5fafdf24 ths
                        inc_refcounts(bs, refcount_table,
2132 585f8587 bellard
                                      refcount_table_size,
2133 585f8587 bellard
                                      offset, s->cluster_size);
2134 585f8587 bellard
                    }
2135 585f8587 bellard
                }
2136 585f8587 bellard
            }
2137 5fafdf24 ths
            inc_refcounts(bs, refcount_table,
2138 585f8587 bellard
                          refcount_table_size,
2139 585f8587 bellard
                          l2_offset,
2140 585f8587 bellard
                          s->cluster_size);
2141 585f8587 bellard
        }
2142 585f8587 bellard
    }
2143 585f8587 bellard
    qemu_free(l1_table);
2144 585f8587 bellard
    qemu_free(l2_table);
2145 585f8587 bellard
    return 0;
2146 585f8587 bellard
 fail:
2147 585f8587 bellard
    printf("ERROR: I/O error in check_refcounts_l1\n");
2148 585f8587 bellard
    qemu_free(l1_table);
2149 585f8587 bellard
    qemu_free(l2_table);
2150 585f8587 bellard
    return -EIO;
2151 585f8587 bellard
}
2152 585f8587 bellard
2153 585f8587 bellard
static void check_refcounts(BlockDriverState *bs)
2154 585f8587 bellard
{
2155 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2156 585f8587 bellard
    int64_t size;
2157 585f8587 bellard
    int nb_clusters, refcount1, refcount2, i;
2158 585f8587 bellard
    QCowSnapshot *sn;
2159 585f8587 bellard
    uint16_t *refcount_table;
2160 585f8587 bellard
2161 585f8587 bellard
    size = bdrv_getlength(s->hd);
2162 585f8587 bellard
    nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2163 585f8587 bellard
    refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
2164 15e6690a bellard
2165 585f8587 bellard
    /* header */
2166 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2167 585f8587 bellard
                  0, s->cluster_size);
2168 5fafdf24 ths
   
2169 585f8587 bellard
    check_refcounts_l1(bs, refcount_table, nb_clusters,
2170 585f8587 bellard
                       s->l1_table_offset, s->l1_size, 1);
2171 585f8587 bellard
2172 585f8587 bellard
    /* snapshots */
2173 585f8587 bellard
    for(i = 0; i < s->nb_snapshots; i++) {
2174 585f8587 bellard
        sn = s->snapshots + i;
2175 585f8587 bellard
        check_refcounts_l1(bs, refcount_table, nb_clusters,
2176 585f8587 bellard
                           sn->l1_table_offset, sn->l1_size, 0);
2177 585f8587 bellard
    }
2178 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2179 585f8587 bellard
                  s->snapshots_offset, s->snapshots_size);
2180 585f8587 bellard
2181 585f8587 bellard
    /* refcount data */
2182 585f8587 bellard
    inc_refcounts(bs, refcount_table, nb_clusters,
2183 5fafdf24 ths
                  s->refcount_table_offset,
2184 585f8587 bellard
                  s->refcount_table_size * sizeof(uint64_t));
2185 585f8587 bellard
    for(i = 0; i < s->refcount_table_size; i++) {
2186 585f8587 bellard
        int64_t offset;
2187 585f8587 bellard
        offset = s->refcount_table[i];
2188 585f8587 bellard
        if (offset != 0) {
2189 585f8587 bellard
            inc_refcounts(bs, refcount_table, nb_clusters,
2190 585f8587 bellard
                          offset, s->cluster_size);
2191 585f8587 bellard
        }
2192 585f8587 bellard
    }
2193 585f8587 bellard
2194 585f8587 bellard
    /* compare ref counts */
2195 585f8587 bellard
    for(i = 0; i < nb_clusters; i++) {
2196 585f8587 bellard
        refcount1 = get_refcount(bs, i);
2197 585f8587 bellard
        refcount2 = refcount_table[i];
2198 585f8587 bellard
        if (refcount1 != refcount2)
2199 585f8587 bellard
            printf("ERROR cluster %d refcount=%d reference=%d\n",
2200 585f8587 bellard
                   i, refcount1, refcount2);
2201 585f8587 bellard
    }
2202 585f8587 bellard
2203 585f8587 bellard
    qemu_free(refcount_table);
2204 585f8587 bellard
}
2205 585f8587 bellard
2206 585f8587 bellard
#if 0
2207 585f8587 bellard
static void dump_refcounts(BlockDriverState *bs)
2208 585f8587 bellard
{
2209 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
2210 585f8587 bellard
    int64_t nb_clusters, k, k1, size;
2211 585f8587 bellard
    int refcount;
2212 585f8587 bellard

2213 585f8587 bellard
    size = bdrv_getlength(s->hd);
2214 585f8587 bellard
    nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2215 585f8587 bellard
    for(k = 0; k < nb_clusters;) {
2216 585f8587 bellard
        k1 = k;
2217 585f8587 bellard
        refcount = get_refcount(bs, k);
2218 585f8587 bellard
        k++;
2219 585f8587 bellard
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
2220 585f8587 bellard
            k++;
2221 585f8587 bellard
        printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2222 585f8587 bellard
    }
2223 585f8587 bellard
}
2224 585f8587 bellard
#endif
2225 585f8587 bellard
#endif
2226 585f8587 bellard
2227 585f8587 bellard
BlockDriver bdrv_qcow2 = {
2228 585f8587 bellard
    "qcow2",
2229 585f8587 bellard
    sizeof(BDRVQcowState),
2230 585f8587 bellard
    qcow_probe,
2231 585f8587 bellard
    qcow_open,
2232 585f8587 bellard
    NULL,
2233 585f8587 bellard
    NULL,
2234 585f8587 bellard
    qcow_close,
2235 585f8587 bellard
    qcow_create,
2236 585f8587 bellard
    qcow_flush,
2237 585f8587 bellard
    qcow_is_allocated,
2238 585f8587 bellard
    qcow_set_key,
2239 585f8587 bellard
    qcow_make_empty,
2240 585f8587 bellard
2241 585f8587 bellard
    .bdrv_aio_read = qcow_aio_read,
2242 585f8587 bellard
    .bdrv_aio_write = qcow_aio_write,
2243 585f8587 bellard
    .bdrv_aio_cancel = qcow_aio_cancel,
2244 ce1a14dc pbrook
    .aiocb_size = sizeof(QCowAIOCB),
2245 585f8587 bellard
    .bdrv_write_compressed = qcow_write_compressed,
2246 585f8587 bellard
2247 585f8587 bellard
    .bdrv_snapshot_create = qcow_snapshot_create,
2248 585f8587 bellard
    .bdrv_snapshot_goto = qcow_snapshot_goto,
2249 585f8587 bellard
    .bdrv_snapshot_delete = qcow_snapshot_delete,
2250 585f8587 bellard
    .bdrv_snapshot_list = qcow_snapshot_list,
2251 585f8587 bellard
    .bdrv_get_info = qcow_get_info,
2252 585f8587 bellard
};