Statistics
| Branch: | Revision:

root / block-qcow.c @ 83f64091

History | View | Annotate | Download (28.7 kB)

1 ea2384d3 bellard
/*
2 ea2384d3 bellard
 * Block driver for the QCOW format
3 ea2384d3 bellard
 * 
4 83f64091 bellard
 * Copyright (c) 2004-2006 Fabrice Bellard
5 ea2384d3 bellard
 * 
6 ea2384d3 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 ea2384d3 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 ea2384d3 bellard
 * in the Software without restriction, including without limitation the rights
9 ea2384d3 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ea2384d3 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 ea2384d3 bellard
 * furnished to do so, subject to the following conditions:
12 ea2384d3 bellard
 *
13 ea2384d3 bellard
 * The above copyright notice and this permission notice shall be included in
14 ea2384d3 bellard
 * all copies or substantial portions of the Software.
15 ea2384d3 bellard
 *
16 ea2384d3 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ea2384d3 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ea2384d3 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 ea2384d3 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ea2384d3 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ea2384d3 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 ea2384d3 bellard
 * THE SOFTWARE.
23 ea2384d3 bellard
 */
24 ea2384d3 bellard
#include "vl.h"
25 ea2384d3 bellard
#include "block_int.h"
26 28d34b82 bellard
#include <zlib.h>
27 ea2384d3 bellard
#include "aes.h"
28 ea2384d3 bellard
29 ea2384d3 bellard
/**************************************************************/
30 ea2384d3 bellard
/* QEMU COW block driver with compression and encryption support */
31 ea2384d3 bellard
32 ea2384d3 bellard
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
33 ea2384d3 bellard
#define QCOW_VERSION 1
34 ea2384d3 bellard
35 ea2384d3 bellard
#define QCOW_CRYPT_NONE 0
36 ea2384d3 bellard
#define QCOW_CRYPT_AES  1
37 ea2384d3 bellard
38 ea2384d3 bellard
#define QCOW_OFLAG_COMPRESSED (1LL << 63)
39 ea2384d3 bellard
40 ea2384d3 bellard
typedef struct QCowHeader {
41 ea2384d3 bellard
    uint32_t magic;
42 ea2384d3 bellard
    uint32_t version;
43 ea2384d3 bellard
    uint64_t backing_file_offset;
44 ea2384d3 bellard
    uint32_t backing_file_size;
45 ea2384d3 bellard
    uint32_t mtime;
46 ea2384d3 bellard
    uint64_t size; /* in bytes */
47 ea2384d3 bellard
    uint8_t cluster_bits;
48 ea2384d3 bellard
    uint8_t l2_bits;
49 ea2384d3 bellard
    uint32_t crypt_method;
50 ea2384d3 bellard
    uint64_t l1_table_offset;
51 ea2384d3 bellard
} QCowHeader;
52 ea2384d3 bellard
53 ea2384d3 bellard
#define L2_CACHE_SIZE 16
54 ea2384d3 bellard
55 ea2384d3 bellard
typedef struct BDRVQcowState {
56 83f64091 bellard
    BlockDriverState *hd;
57 ea2384d3 bellard
    int cluster_bits;
58 ea2384d3 bellard
    int cluster_size;
59 ea2384d3 bellard
    int cluster_sectors;
60 ea2384d3 bellard
    int l2_bits;
61 ea2384d3 bellard
    int l2_size;
62 ea2384d3 bellard
    int l1_size;
63 ea2384d3 bellard
    uint64_t cluster_offset_mask;
64 ea2384d3 bellard
    uint64_t l1_table_offset;
65 ea2384d3 bellard
    uint64_t *l1_table;
66 ea2384d3 bellard
    uint64_t *l2_cache;
67 ea2384d3 bellard
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
68 ea2384d3 bellard
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
69 ea2384d3 bellard
    uint8_t *cluster_cache;
70 ea2384d3 bellard
    uint8_t *cluster_data;
71 ea2384d3 bellard
    uint64_t cluster_cache_offset;
72 ea2384d3 bellard
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
73 ea2384d3 bellard
    uint32_t crypt_method_header;
74 ea2384d3 bellard
    AES_KEY aes_encrypt_key;
75 ea2384d3 bellard
    AES_KEY aes_decrypt_key;
76 ea2384d3 bellard
} BDRVQcowState;
77 ea2384d3 bellard
78 ea2384d3 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
79 ea2384d3 bellard
80 ea2384d3 bellard
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
81 ea2384d3 bellard
{
82 ea2384d3 bellard
    const QCowHeader *cow_header = (const void *)buf;
83 712e7874 bellard
    
84 712e7874 bellard
    if (buf_size >= sizeof(QCowHeader) &&
85 712e7874 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
86 ea2384d3 bellard
        be32_to_cpu(cow_header->version) == QCOW_VERSION) 
87 ea2384d3 bellard
        return 100;
88 ea2384d3 bellard
    else
89 ea2384d3 bellard
        return 0;
90 ea2384d3 bellard
}
91 ea2384d3 bellard
92 83f64091 bellard
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
93 ea2384d3 bellard
{
94 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
95 83f64091 bellard
    int len, i, shift, ret;
96 ea2384d3 bellard
    QCowHeader header;
97 83f64091 bellard
98 83f64091 bellard
    ret = bdrv_file_open(&s->hd, filename, flags);
99 83f64091 bellard
    if (ret < 0)
100 83f64091 bellard
        return ret;
101 83f64091 bellard
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
102 ea2384d3 bellard
        goto fail;
103 ea2384d3 bellard
    be32_to_cpus(&header.magic);
104 ea2384d3 bellard
    be32_to_cpus(&header.version);
105 ea2384d3 bellard
    be64_to_cpus(&header.backing_file_offset);
106 ea2384d3 bellard
    be32_to_cpus(&header.backing_file_size);
107 ea2384d3 bellard
    be32_to_cpus(&header.mtime);
108 ea2384d3 bellard
    be64_to_cpus(&header.size);
109 ea2384d3 bellard
    be32_to_cpus(&header.crypt_method);
110 ea2384d3 bellard
    be64_to_cpus(&header.l1_table_offset);
111 ea2384d3 bellard
    
112 ea2384d3 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
113 ea2384d3 bellard
        goto fail;
114 ea2384d3 bellard
    if (header.size <= 1 || header.cluster_bits < 9)
115 ea2384d3 bellard
        goto fail;
116 ea2384d3 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
117 ea2384d3 bellard
        goto fail;
118 ea2384d3 bellard
    s->crypt_method_header = header.crypt_method;
119 ea2384d3 bellard
    if (s->crypt_method_header)
120 ea2384d3 bellard
        bs->encrypted = 1;
121 ea2384d3 bellard
    s->cluster_bits = header.cluster_bits;
122 ea2384d3 bellard
    s->cluster_size = 1 << s->cluster_bits;
123 ea2384d3 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
124 ea2384d3 bellard
    s->l2_bits = header.l2_bits;
125 ea2384d3 bellard
    s->l2_size = 1 << s->l2_bits;
126 ea2384d3 bellard
    bs->total_sectors = header.size / 512;
127 ea2384d3 bellard
    s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
128 ea2384d3 bellard
129 ea2384d3 bellard
    /* read the level 1 table */
130 ea2384d3 bellard
    shift = s->cluster_bits + s->l2_bits;
131 ea2384d3 bellard
    s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
132 ea2384d3 bellard
133 ea2384d3 bellard
    s->l1_table_offset = header.l1_table_offset;
134 ea2384d3 bellard
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
135 ea2384d3 bellard
    if (!s->l1_table)
136 ea2384d3 bellard
        goto fail;
137 83f64091 bellard
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != 
138 ea2384d3 bellard
        s->l1_size * sizeof(uint64_t))
139 ea2384d3 bellard
        goto fail;
140 ea2384d3 bellard
    for(i = 0;i < s->l1_size; i++) {
141 ea2384d3 bellard
        be64_to_cpus(&s->l1_table[i]);
142 ea2384d3 bellard
    }
143 ea2384d3 bellard
    /* alloc L2 cache */
144 ea2384d3 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
145 ea2384d3 bellard
    if (!s->l2_cache)
146 ea2384d3 bellard
        goto fail;
147 ea2384d3 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
148 ea2384d3 bellard
    if (!s->cluster_cache)
149 ea2384d3 bellard
        goto fail;
150 ea2384d3 bellard
    s->cluster_data = qemu_malloc(s->cluster_size);
151 ea2384d3 bellard
    if (!s->cluster_data)
152 ea2384d3 bellard
        goto fail;
153 ea2384d3 bellard
    s->cluster_cache_offset = -1;
154 ea2384d3 bellard
    
155 ea2384d3 bellard
    /* read the backing file name */
156 ea2384d3 bellard
    if (header.backing_file_offset != 0) {
157 ea2384d3 bellard
        len = header.backing_file_size;
158 ea2384d3 bellard
        if (len > 1023)
159 ea2384d3 bellard
            len = 1023;
160 83f64091 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
161 ea2384d3 bellard
            goto fail;
162 ea2384d3 bellard
        bs->backing_file[len] = '\0';
163 ea2384d3 bellard
    }
164 ea2384d3 bellard
    return 0;
165 ea2384d3 bellard
166 ea2384d3 bellard
 fail:
167 ea2384d3 bellard
    qemu_free(s->l1_table);
168 ea2384d3 bellard
    qemu_free(s->l2_cache);
169 ea2384d3 bellard
    qemu_free(s->cluster_cache);
170 ea2384d3 bellard
    qemu_free(s->cluster_data);
171 83f64091 bellard
    bdrv_delete(s->hd);
172 ea2384d3 bellard
    return -1;
173 ea2384d3 bellard
}
174 ea2384d3 bellard
175 ea2384d3 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
176 ea2384d3 bellard
{
177 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
178 ea2384d3 bellard
    uint8_t keybuf[16];
179 ea2384d3 bellard
    int len, i;
180 ea2384d3 bellard
    
181 ea2384d3 bellard
    memset(keybuf, 0, 16);
182 ea2384d3 bellard
    len = strlen(key);
183 ea2384d3 bellard
    if (len > 16)
184 ea2384d3 bellard
        len = 16;
185 ea2384d3 bellard
    /* XXX: we could compress the chars to 7 bits to increase
186 ea2384d3 bellard
       entropy */
187 ea2384d3 bellard
    for(i = 0;i < len;i++) {
188 ea2384d3 bellard
        keybuf[i] = key[i];
189 ea2384d3 bellard
    }
190 ea2384d3 bellard
    s->crypt_method = s->crypt_method_header;
191 ea2384d3 bellard
192 ea2384d3 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
193 ea2384d3 bellard
        return -1;
194 ea2384d3 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
195 ea2384d3 bellard
        return -1;
196 ea2384d3 bellard
#if 0
197 ea2384d3 bellard
    /* test */
198 ea2384d3 bellard
    {
199 ea2384d3 bellard
        uint8_t in[16];
200 ea2384d3 bellard
        uint8_t out[16];
201 ea2384d3 bellard
        uint8_t tmp[16];
202 ea2384d3 bellard
        for(i=0;i<16;i++)
203 ea2384d3 bellard
            in[i] = i;
204 ea2384d3 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
205 ea2384d3 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
206 ea2384d3 bellard
        for(i = 0; i < 16; i++)
207 ea2384d3 bellard
            printf(" %02x", tmp[i]);
208 ea2384d3 bellard
        printf("\n");
209 ea2384d3 bellard
        for(i = 0; i < 16; i++)
210 ea2384d3 bellard
            printf(" %02x", out[i]);
211 ea2384d3 bellard
        printf("\n");
212 ea2384d3 bellard
    }
213 ea2384d3 bellard
#endif
214 ea2384d3 bellard
    return 0;
215 ea2384d3 bellard
}
216 ea2384d3 bellard
217 ea2384d3 bellard
/* The crypt function is compatible with the linux cryptoloop
218 ea2384d3 bellard
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
219 ea2384d3 bellard
   supported */
220 ea2384d3 bellard
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
221 ea2384d3 bellard
                            uint8_t *out_buf, const uint8_t *in_buf,
222 ea2384d3 bellard
                            int nb_sectors, int enc,
223 ea2384d3 bellard
                            const AES_KEY *key)
224 ea2384d3 bellard
{
225 ea2384d3 bellard
    union {
226 ea2384d3 bellard
        uint64_t ll[2];
227 ea2384d3 bellard
        uint8_t b[16];
228 ea2384d3 bellard
    } ivec;
229 ea2384d3 bellard
    int i;
230 ea2384d3 bellard
231 ea2384d3 bellard
    for(i = 0; i < nb_sectors; i++) {
232 ea2384d3 bellard
        ivec.ll[0] = cpu_to_le64(sector_num);
233 ea2384d3 bellard
        ivec.ll[1] = 0;
234 ea2384d3 bellard
        AES_cbc_encrypt(in_buf, out_buf, 512, key, 
235 ea2384d3 bellard
                        ivec.b, enc);
236 ea2384d3 bellard
        sector_num++;
237 ea2384d3 bellard
        in_buf += 512;
238 ea2384d3 bellard
        out_buf += 512;
239 ea2384d3 bellard
    }
240 ea2384d3 bellard
}
241 ea2384d3 bellard
242 ea2384d3 bellard
/* 'allocate' is:
243 ea2384d3 bellard
 *
244 ea2384d3 bellard
 * 0 to not allocate.
245 ea2384d3 bellard
 *
246 ea2384d3 bellard
 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
247 ea2384d3 bellard
 * 'n_end')
248 ea2384d3 bellard
 *
249 ea2384d3 bellard
 * 2 to allocate a compressed cluster of size
250 ea2384d3 bellard
 * 'compressed_size'. 'compressed_size' must be > 0 and <
251 ea2384d3 bellard
 * cluster_size 
252 ea2384d3 bellard
 *
253 ea2384d3 bellard
 * return 0 if not allocated.
254 ea2384d3 bellard
 */
255 ea2384d3 bellard
static uint64_t get_cluster_offset(BlockDriverState *bs,
256 ea2384d3 bellard
                                   uint64_t offset, int allocate,
257 ea2384d3 bellard
                                   int compressed_size,
258 ea2384d3 bellard
                                   int n_start, int n_end)
259 ea2384d3 bellard
{
260 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
261 ea2384d3 bellard
    int min_index, i, j, l1_index, l2_index;
262 ea2384d3 bellard
    uint64_t l2_offset, *l2_table, cluster_offset, tmp;
263 ea2384d3 bellard
    uint32_t min_count;
264 ea2384d3 bellard
    int new_l2_table;
265 ea2384d3 bellard
    
266 ea2384d3 bellard
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
267 ea2384d3 bellard
    l2_offset = s->l1_table[l1_index];
268 ea2384d3 bellard
    new_l2_table = 0;
269 ea2384d3 bellard
    if (!l2_offset) {
270 ea2384d3 bellard
        if (!allocate)
271 ea2384d3 bellard
            return 0;
272 ea2384d3 bellard
        /* allocate a new l2 entry */
273 83f64091 bellard
        l2_offset = bdrv_getlength(s->hd);
274 ea2384d3 bellard
        /* round to cluster size */
275 ea2384d3 bellard
        l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
276 ea2384d3 bellard
        /* update the L1 entry */
277 ea2384d3 bellard
        s->l1_table[l1_index] = l2_offset;
278 ea2384d3 bellard
        tmp = cpu_to_be64(l2_offset);
279 83f64091 bellard
        if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), 
280 83f64091 bellard
                        &tmp, sizeof(tmp)) != sizeof(tmp))
281 ea2384d3 bellard
            return 0;
282 ea2384d3 bellard
        new_l2_table = 1;
283 ea2384d3 bellard
    }
284 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
285 ea2384d3 bellard
        if (l2_offset == s->l2_cache_offsets[i]) {
286 ea2384d3 bellard
            /* increment the hit count */
287 ea2384d3 bellard
            if (++s->l2_cache_counts[i] == 0xffffffff) {
288 ea2384d3 bellard
                for(j = 0; j < L2_CACHE_SIZE; j++) {
289 ea2384d3 bellard
                    s->l2_cache_counts[j] >>= 1;
290 ea2384d3 bellard
                }
291 ea2384d3 bellard
            }
292 ea2384d3 bellard
            l2_table = s->l2_cache + (i << s->l2_bits);
293 ea2384d3 bellard
            goto found;
294 ea2384d3 bellard
        }
295 ea2384d3 bellard
    }
296 ea2384d3 bellard
    /* not found: load a new entry in the least used one */
297 ea2384d3 bellard
    min_index = 0;
298 ea2384d3 bellard
    min_count = 0xffffffff;
299 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
300 ea2384d3 bellard
        if (s->l2_cache_counts[i] < min_count) {
301 ea2384d3 bellard
            min_count = s->l2_cache_counts[i];
302 ea2384d3 bellard
            min_index = i;
303 ea2384d3 bellard
        }
304 ea2384d3 bellard
    }
305 ea2384d3 bellard
    l2_table = s->l2_cache + (min_index << s->l2_bits);
306 ea2384d3 bellard
    if (new_l2_table) {
307 ea2384d3 bellard
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
308 83f64091 bellard
        if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
309 ea2384d3 bellard
            s->l2_size * sizeof(uint64_t))
310 ea2384d3 bellard
            return 0;
311 ea2384d3 bellard
    } else {
312 83f64091 bellard
        if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != 
313 ea2384d3 bellard
            s->l2_size * sizeof(uint64_t))
314 ea2384d3 bellard
            return 0;
315 ea2384d3 bellard
    }
316 ea2384d3 bellard
    s->l2_cache_offsets[min_index] = l2_offset;
317 ea2384d3 bellard
    s->l2_cache_counts[min_index] = 1;
318 ea2384d3 bellard
 found:
319 ea2384d3 bellard
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
320 ea2384d3 bellard
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
321 ea2384d3 bellard
    if (!cluster_offset || 
322 ea2384d3 bellard
        ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
323 ea2384d3 bellard
        if (!allocate)
324 ea2384d3 bellard
            return 0;
325 ea2384d3 bellard
        /* allocate a new cluster */
326 ea2384d3 bellard
        if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
327 ea2384d3 bellard
            (n_end - n_start) < s->cluster_sectors) {
328 ea2384d3 bellard
            /* if the cluster is already compressed, we must
329 ea2384d3 bellard
               decompress it in the case it is not completely
330 ea2384d3 bellard
               overwritten */
331 ea2384d3 bellard
            if (decompress_cluster(s, cluster_offset) < 0)
332 ea2384d3 bellard
                return 0;
333 83f64091 bellard
            cluster_offset = bdrv_getlength(s->hd);
334 ea2384d3 bellard
            cluster_offset = (cluster_offset + s->cluster_size - 1) & 
335 ea2384d3 bellard
                ~(s->cluster_size - 1);
336 ea2384d3 bellard
            /* write the cluster content */
337 83f64091 bellard
            if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != 
338 ea2384d3 bellard
                s->cluster_size)
339 ea2384d3 bellard
                return -1;
340 ea2384d3 bellard
        } else {
341 83f64091 bellard
            cluster_offset = bdrv_getlength(s->hd);
342 ea2384d3 bellard
            if (allocate == 1) {
343 ea2384d3 bellard
                /* round to cluster size */
344 ea2384d3 bellard
                cluster_offset = (cluster_offset + s->cluster_size - 1) & 
345 ea2384d3 bellard
                    ~(s->cluster_size - 1);
346 83f64091 bellard
                bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
347 ea2384d3 bellard
                /* if encrypted, we must initialize the cluster
348 ea2384d3 bellard
                   content which won't be written */
349 ea2384d3 bellard
                if (s->crypt_method && 
350 ea2384d3 bellard
                    (n_end - n_start) < s->cluster_sectors) {
351 ea2384d3 bellard
                    uint64_t start_sect;
352 ea2384d3 bellard
                    start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
353 ea2384d3 bellard
                    memset(s->cluster_data + 512, 0xaa, 512);
354 ea2384d3 bellard
                    for(i = 0; i < s->cluster_sectors; i++) {
355 ea2384d3 bellard
                        if (i < n_start || i >= n_end) {
356 ea2384d3 bellard
                            encrypt_sectors(s, start_sect + i, 
357 ea2384d3 bellard
                                            s->cluster_data, 
358 ea2384d3 bellard
                                            s->cluster_data + 512, 1, 1,
359 ea2384d3 bellard
                                            &s->aes_encrypt_key);
360 83f64091 bellard
                            if (bdrv_pwrite(s->hd, cluster_offset + i * 512, 
361 83f64091 bellard
                                            s->cluster_data, 512) != 512)
362 ea2384d3 bellard
                                return -1;
363 ea2384d3 bellard
                        }
364 ea2384d3 bellard
                    }
365 ea2384d3 bellard
                }
366 ea2384d3 bellard
            } else {
367 ea2384d3 bellard
                cluster_offset |= QCOW_OFLAG_COMPRESSED | 
368 ea2384d3 bellard
                    (uint64_t)compressed_size << (63 - s->cluster_bits);
369 ea2384d3 bellard
            }
370 ea2384d3 bellard
        }
371 ea2384d3 bellard
        /* update L2 table */
372 ea2384d3 bellard
        tmp = cpu_to_be64(cluster_offset);
373 ea2384d3 bellard
        l2_table[l2_index] = tmp;
374 83f64091 bellard
        if (bdrv_pwrite(s->hd, 
375 83f64091 bellard
                        l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
376 ea2384d3 bellard
            return 0;
377 ea2384d3 bellard
    }
378 ea2384d3 bellard
    return cluster_offset;
379 ea2384d3 bellard
}
380 ea2384d3 bellard
381 ea2384d3 bellard
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, 
382 ea2384d3 bellard
                             int nb_sectors, int *pnum)
383 ea2384d3 bellard
{
384 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
385 ea2384d3 bellard
    int index_in_cluster, n;
386 ea2384d3 bellard
    uint64_t cluster_offset;
387 ea2384d3 bellard
388 ea2384d3 bellard
    cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
389 ea2384d3 bellard
    index_in_cluster = sector_num & (s->cluster_sectors - 1);
390 ea2384d3 bellard
    n = s->cluster_sectors - index_in_cluster;
391 ea2384d3 bellard
    if (n > nb_sectors)
392 ea2384d3 bellard
        n = nb_sectors;
393 ea2384d3 bellard
    *pnum = n;
394 ea2384d3 bellard
    return (cluster_offset != 0);
395 ea2384d3 bellard
}
396 ea2384d3 bellard
397 ea2384d3 bellard
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
398 ea2384d3 bellard
                             const uint8_t *buf, int buf_size)
399 ea2384d3 bellard
{
400 ea2384d3 bellard
    z_stream strm1, *strm = &strm1;
401 ea2384d3 bellard
    int ret, out_len;
402 ea2384d3 bellard
403 ea2384d3 bellard
    memset(strm, 0, sizeof(*strm));
404 ea2384d3 bellard
405 ea2384d3 bellard
    strm->next_in = (uint8_t *)buf;
406 ea2384d3 bellard
    strm->avail_in = buf_size;
407 ea2384d3 bellard
    strm->next_out = out_buf;
408 ea2384d3 bellard
    strm->avail_out = out_buf_size;
409 ea2384d3 bellard
410 ea2384d3 bellard
    ret = inflateInit2(strm, -12);
411 ea2384d3 bellard
    if (ret != Z_OK)
412 ea2384d3 bellard
        return -1;
413 ea2384d3 bellard
    ret = inflate(strm, Z_FINISH);
414 ea2384d3 bellard
    out_len = strm->next_out - out_buf;
415 ea2384d3 bellard
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
416 ea2384d3 bellard
        out_len != out_buf_size) {
417 ea2384d3 bellard
        inflateEnd(strm);
418 ea2384d3 bellard
        return -1;
419 ea2384d3 bellard
    }
420 ea2384d3 bellard
    inflateEnd(strm);
421 ea2384d3 bellard
    return 0;
422 ea2384d3 bellard
}
423 ea2384d3 bellard
                              
424 ea2384d3 bellard
static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
425 ea2384d3 bellard
{
426 ea2384d3 bellard
    int ret, csize;
427 ea2384d3 bellard
    uint64_t coffset;
428 ea2384d3 bellard
429 ea2384d3 bellard
    coffset = cluster_offset & s->cluster_offset_mask;
430 ea2384d3 bellard
    if (s->cluster_cache_offset != coffset) {
431 ea2384d3 bellard
        csize = cluster_offset >> (63 - s->cluster_bits);
432 ea2384d3 bellard
        csize &= (s->cluster_size - 1);
433 83f64091 bellard
        ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize);
434 ea2384d3 bellard
        if (ret != csize) 
435 ea2384d3 bellard
            return -1;
436 ea2384d3 bellard
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
437 ea2384d3 bellard
                              s->cluster_data, csize) < 0) {
438 ea2384d3 bellard
            return -1;
439 ea2384d3 bellard
        }
440 ea2384d3 bellard
        s->cluster_cache_offset = coffset;
441 ea2384d3 bellard
    }
442 ea2384d3 bellard
    return 0;
443 ea2384d3 bellard
}
444 ea2384d3 bellard
445 83f64091 bellard
#if 0
446 83f64091 bellard

447 ea2384d3 bellard
static int qcow_read(BlockDriverState *bs, int64_t sector_num, 
448 ea2384d3 bellard
                     uint8_t *buf, int nb_sectors)
449 ea2384d3 bellard
{
450 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
451 ea2384d3 bellard
    int ret, index_in_cluster, n;
452 ea2384d3 bellard
    uint64_t cluster_offset;
453 ea2384d3 bellard
    
454 ea2384d3 bellard
    while (nb_sectors > 0) {
455 ea2384d3 bellard
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
456 ea2384d3 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
457 ea2384d3 bellard
        n = s->cluster_sectors - index_in_cluster;
458 ea2384d3 bellard
        if (n > nb_sectors)
459 ea2384d3 bellard
            n = nb_sectors;
460 ea2384d3 bellard
        if (!cluster_offset) {
461 83f64091 bellard
            if (bs->backing_hd) {
462 83f64091 bellard
                /* read from the base image */
463 83f64091 bellard
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
464 83f64091 bellard
                if (ret < 0)
465 83f64091 bellard
                    return -1;
466 83f64091 bellard
            } else {
467 83f64091 bellard
                memset(buf, 0, 512 * n);
468 83f64091 bellard
            }
469 ea2384d3 bellard
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
470 ea2384d3 bellard
            if (decompress_cluster(s, cluster_offset) < 0)
471 ea2384d3 bellard
                return -1;
472 ea2384d3 bellard
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
473 ea2384d3 bellard
        } else {
474 83f64091 bellard
            ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
475 ea2384d3 bellard
            if (ret != n * 512) 
476 ea2384d3 bellard
                return -1;
477 ea2384d3 bellard
            if (s->crypt_method) {
478 ea2384d3 bellard
                encrypt_sectors(s, sector_num, buf, buf, n, 0, 
479 ea2384d3 bellard
                                &s->aes_decrypt_key);
480 ea2384d3 bellard
            }
481 ea2384d3 bellard
        }
482 ea2384d3 bellard
        nb_sectors -= n;
483 ea2384d3 bellard
        sector_num += n;
484 ea2384d3 bellard
        buf += n * 512;
485 ea2384d3 bellard
    }
486 ea2384d3 bellard
    return 0;
487 ea2384d3 bellard
}
488 83f64091 bellard
#endif
489 ea2384d3 bellard
490 ea2384d3 bellard
static int qcow_write(BlockDriverState *bs, int64_t sector_num, 
491 ea2384d3 bellard
                     const uint8_t *buf, int nb_sectors)
492 ea2384d3 bellard
{
493 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
494 ea2384d3 bellard
    int ret, index_in_cluster, n;
495 ea2384d3 bellard
    uint64_t cluster_offset;
496 ea2384d3 bellard
    
497 ea2384d3 bellard
    while (nb_sectors > 0) {
498 ea2384d3 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
499 ea2384d3 bellard
        n = s->cluster_sectors - index_in_cluster;
500 ea2384d3 bellard
        if (n > nb_sectors)
501 ea2384d3 bellard
            n = nb_sectors;
502 ea2384d3 bellard
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, 
503 ea2384d3 bellard
                                            index_in_cluster, 
504 ea2384d3 bellard
                                            index_in_cluster + n);
505 ea2384d3 bellard
        if (!cluster_offset)
506 ea2384d3 bellard
            return -1;
507 ea2384d3 bellard
        if (s->crypt_method) {
508 ea2384d3 bellard
            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
509 ea2384d3 bellard
                            &s->aes_encrypt_key);
510 83f64091 bellard
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, 
511 83f64091 bellard
                              s->cluster_data, n * 512);
512 ea2384d3 bellard
        } else {
513 83f64091 bellard
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
514 ea2384d3 bellard
        }
515 ea2384d3 bellard
        if (ret != n * 512) 
516 ea2384d3 bellard
            return -1;
517 ea2384d3 bellard
        nb_sectors -= n;
518 ea2384d3 bellard
        sector_num += n;
519 ea2384d3 bellard
        buf += n * 512;
520 ea2384d3 bellard
    }
521 ea2384d3 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
522 ea2384d3 bellard
    return 0;
523 ea2384d3 bellard
}
524 ea2384d3 bellard
525 83f64091 bellard
typedef struct {
526 83f64091 bellard
    int64_t sector_num;
527 83f64091 bellard
    uint8_t *buf;
528 83f64091 bellard
    int nb_sectors;
529 83f64091 bellard
    int n;
530 83f64091 bellard
    uint64_t cluster_offset;
531 83f64091 bellard
    uint8_t *cluster_data; 
532 83f64091 bellard
    BlockDriverAIOCB *hd_aiocb;
533 83f64091 bellard
    BlockDriverAIOCB *backing_hd_aiocb;
534 83f64091 bellard
} QCowAIOCB;
535 83f64091 bellard
536 83f64091 bellard
static void qcow_aio_delete(BlockDriverAIOCB *acb);
537 83f64091 bellard
538 83f64091 bellard
static int qcow_aio_new(BlockDriverAIOCB *acb)
539 83f64091 bellard
{
540 83f64091 bellard
    BlockDriverState *bs = acb->bs;
541 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
542 83f64091 bellard
    QCowAIOCB *acb1;
543 83f64091 bellard
    acb1 = qemu_mallocz(sizeof(QCowAIOCB));
544 83f64091 bellard
    if (!acb1)
545 83f64091 bellard
        return -1;
546 83f64091 bellard
    acb->opaque = acb1;
547 83f64091 bellard
    acb1->hd_aiocb = bdrv_aio_new(s->hd);
548 83f64091 bellard
    if (!acb1->hd_aiocb)
549 83f64091 bellard
        goto fail;
550 83f64091 bellard
    if (bs->backing_hd) {
551 83f64091 bellard
        acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd);
552 83f64091 bellard
        if (!acb1->backing_hd_aiocb)
553 83f64091 bellard
            goto fail;
554 83f64091 bellard
    }
555 83f64091 bellard
    return 0;
556 83f64091 bellard
 fail:
557 83f64091 bellard
    qcow_aio_delete(acb);
558 83f64091 bellard
    return -1;
559 83f64091 bellard
}
560 83f64091 bellard
561 83f64091 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
562 83f64091 bellard
{
563 83f64091 bellard
    BlockDriverAIOCB *acb = opaque;
564 83f64091 bellard
    BlockDriverState *bs = acb->bs;
565 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
566 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
567 83f64091 bellard
    int index_in_cluster;
568 83f64091 bellard
569 83f64091 bellard
    if (ret < 0) {
570 83f64091 bellard
    fail:
571 83f64091 bellard
        acb->cb(acb->cb_opaque, ret);
572 83f64091 bellard
        return;
573 83f64091 bellard
    }
574 83f64091 bellard
575 83f64091 bellard
 redo:
576 83f64091 bellard
    /* post process the read buffer */
577 83f64091 bellard
    if (!acb1->cluster_offset) {
578 83f64091 bellard
        /* nothing to do */
579 83f64091 bellard
    } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) {
580 83f64091 bellard
        /* nothing to do */
581 83f64091 bellard
    } else {
582 83f64091 bellard
        if (s->crypt_method) {
583 83f64091 bellard
            encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, 
584 83f64091 bellard
                            acb1->n, 0, 
585 83f64091 bellard
                            &s->aes_decrypt_key);
586 83f64091 bellard
        }
587 83f64091 bellard
    }
588 83f64091 bellard
589 83f64091 bellard
    acb1->nb_sectors -= acb1->n;
590 83f64091 bellard
    acb1->sector_num += acb1->n;
591 83f64091 bellard
    acb1->buf += acb1->n * 512;
592 83f64091 bellard
593 83f64091 bellard
    if (acb1->nb_sectors == 0) {
594 83f64091 bellard
        /* request completed */
595 83f64091 bellard
        acb->cb(acb->cb_opaque, 0);
596 83f64091 bellard
        return;
597 83f64091 bellard
    }
598 83f64091 bellard
    
599 83f64091 bellard
    /* prepare next AIO request */
600 83f64091 bellard
    acb1->cluster_offset = get_cluster_offset(bs, 
601 83f64091 bellard
                                              acb1->sector_num << 9, 
602 83f64091 bellard
                                              0, 0, 0, 0);
603 83f64091 bellard
    index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1);
604 83f64091 bellard
    acb1->n = s->cluster_sectors - index_in_cluster;
605 83f64091 bellard
    if (acb1->n > acb1->nb_sectors)
606 83f64091 bellard
        acb1->n = acb1->nb_sectors;
607 83f64091 bellard
608 83f64091 bellard
    if (!acb1->cluster_offset) {
609 83f64091 bellard
        if (bs->backing_hd) {
610 83f64091 bellard
            /* read from the base image */
611 83f64091 bellard
            ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, 
612 83f64091 bellard
                                acb1->buf, acb1->n, qcow_aio_read_cb, acb);
613 83f64091 bellard
            if (ret < 0)
614 83f64091 bellard
                goto fail;
615 83f64091 bellard
        } else {
616 83f64091 bellard
            /* Note: in this case, no need to wait */
617 83f64091 bellard
            memset(acb1->buf, 0, 512 * acb1->n);
618 83f64091 bellard
            goto redo;
619 83f64091 bellard
        }
620 83f64091 bellard
    } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) {
621 83f64091 bellard
        /* add AIO support for compressed blocks ? */
622 83f64091 bellard
        if (decompress_cluster(s, acb1->cluster_offset) < 0)
623 83f64091 bellard
            goto fail;
624 83f64091 bellard
        memcpy(acb1->buf, 
625 83f64091 bellard
               s->cluster_cache + index_in_cluster * 512, 512 * acb1->n);
626 83f64091 bellard
        goto redo;
627 83f64091 bellard
    } else {
628 83f64091 bellard
        if ((acb1->cluster_offset & 511) != 0) {
629 83f64091 bellard
            ret = -EIO;
630 83f64091 bellard
            goto fail;
631 83f64091 bellard
        }
632 83f64091 bellard
        ret = bdrv_aio_read(acb1->hd_aiocb, 
633 83f64091 bellard
                            (acb1->cluster_offset >> 9) + index_in_cluster, 
634 83f64091 bellard
                            acb1->buf, acb1->n, qcow_aio_read_cb, acb);
635 83f64091 bellard
        if (ret < 0)
636 83f64091 bellard
            goto fail;
637 83f64091 bellard
    }
638 83f64091 bellard
}
639 83f64091 bellard
640 83f64091 bellard
static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, 
641 83f64091 bellard
                         uint8_t *buf, int nb_sectors)
642 83f64091 bellard
{
643 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
644 83f64091 bellard
    
645 83f64091 bellard
    acb1->sector_num = sector_num;
646 83f64091 bellard
    acb1->buf = buf;
647 83f64091 bellard
    acb1->nb_sectors = nb_sectors;
648 83f64091 bellard
    acb1->n = 0;
649 83f64091 bellard
    acb1->cluster_offset = 0;    
650 83f64091 bellard
651 83f64091 bellard
    qcow_aio_read_cb(acb, 0);
652 83f64091 bellard
}
653 83f64091 bellard
654 83f64091 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
655 83f64091 bellard
{
656 83f64091 bellard
    BlockDriverAIOCB *acb = opaque;
657 83f64091 bellard
    BlockDriverState *bs = acb->bs;
658 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
659 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
660 83f64091 bellard
    int index_in_cluster;
661 83f64091 bellard
    uint64_t cluster_offset;
662 83f64091 bellard
    const uint8_t *src_buf;
663 83f64091 bellard
    
664 83f64091 bellard
    if (ret < 0) {
665 83f64091 bellard
    fail:
666 83f64091 bellard
        acb->cb(acb->cb_opaque, ret);
667 83f64091 bellard
        return;
668 83f64091 bellard
    }
669 83f64091 bellard
670 83f64091 bellard
    acb1->nb_sectors -= acb1->n;
671 83f64091 bellard
    acb1->sector_num += acb1->n;
672 83f64091 bellard
    acb1->buf += acb1->n * 512;
673 83f64091 bellard
674 83f64091 bellard
    if (acb1->nb_sectors == 0) {
675 83f64091 bellard
        /* request completed */
676 83f64091 bellard
        acb->cb(acb->cb_opaque, 0);
677 83f64091 bellard
        return;
678 83f64091 bellard
    }
679 83f64091 bellard
    
680 83f64091 bellard
    index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1);
681 83f64091 bellard
    acb1->n = s->cluster_sectors - index_in_cluster;
682 83f64091 bellard
    if (acb1->n > acb1->nb_sectors)
683 83f64091 bellard
        acb1->n = acb1->nb_sectors;
684 83f64091 bellard
    cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, 
685 83f64091 bellard
                                        index_in_cluster, 
686 83f64091 bellard
                                        index_in_cluster + acb1->n);
687 83f64091 bellard
    if (!cluster_offset || (cluster_offset & 511) != 0) {
688 83f64091 bellard
        ret = -EIO;
689 83f64091 bellard
        goto fail;
690 83f64091 bellard
    }
691 83f64091 bellard
    if (s->crypt_method) {
692 83f64091 bellard
        if (!acb1->cluster_data) {
693 83f64091 bellard
            acb1->cluster_data = qemu_mallocz(s->cluster_size);
694 83f64091 bellard
            if (!acb1->cluster_data) {
695 83f64091 bellard
                ret = -ENOMEM;
696 83f64091 bellard
                goto fail;
697 83f64091 bellard
            }
698 83f64091 bellard
        }
699 83f64091 bellard
        encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, 
700 83f64091 bellard
                        acb1->n, 1, &s->aes_encrypt_key);
701 83f64091 bellard
        src_buf = acb1->cluster_data;
702 83f64091 bellard
    } else {
703 83f64091 bellard
        src_buf = acb1->buf;
704 83f64091 bellard
    }
705 83f64091 bellard
    ret = bdrv_aio_write(acb1->hd_aiocb, 
706 83f64091 bellard
                         (cluster_offset >> 9) + index_in_cluster, 
707 83f64091 bellard
                         src_buf, acb1->n, 
708 83f64091 bellard
                         qcow_aio_write_cb, acb);
709 83f64091 bellard
    if (ret < 0)
710 83f64091 bellard
        goto fail;
711 83f64091 bellard
}
712 83f64091 bellard
713 83f64091 bellard
static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, 
714 83f64091 bellard
                          const uint8_t *buf, int nb_sectors)
715 83f64091 bellard
{
716 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
717 83f64091 bellard
    BlockDriverState *bs = acb->bs;
718 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
719 83f64091 bellard
    
720 83f64091 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
721 83f64091 bellard
722 83f64091 bellard
    acb1->sector_num = sector_num;
723 83f64091 bellard
    acb1->buf = (uint8_t *)buf;
724 83f64091 bellard
    acb1->nb_sectors = nb_sectors;
725 83f64091 bellard
    acb1->n = 0;
726 83f64091 bellard
    
727 83f64091 bellard
    qcow_aio_write_cb(acb, 0);
728 83f64091 bellard
}
729 83f64091 bellard
730 83f64091 bellard
static void qcow_aio_cancel(BlockDriverAIOCB *acb)
731 83f64091 bellard
{
732 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
733 83f64091 bellard
    if (acb1->hd_aiocb)
734 83f64091 bellard
        bdrv_aio_cancel(acb1->hd_aiocb);
735 83f64091 bellard
    if (acb1->backing_hd_aiocb)
736 83f64091 bellard
        bdrv_aio_cancel(acb1->backing_hd_aiocb);
737 83f64091 bellard
}
738 83f64091 bellard
739 83f64091 bellard
static void qcow_aio_delete(BlockDriverAIOCB *acb)
740 83f64091 bellard
{
741 83f64091 bellard
    QCowAIOCB *acb1 = acb->opaque;
742 83f64091 bellard
    if (acb1->hd_aiocb)
743 83f64091 bellard
        bdrv_aio_delete(acb1->hd_aiocb);
744 83f64091 bellard
    if (acb1->backing_hd_aiocb)
745 83f64091 bellard
        bdrv_aio_delete(acb1->backing_hd_aiocb);
746 83f64091 bellard
    qemu_free(acb1->cluster_data);
747 83f64091 bellard
    qemu_free(acb1);
748 83f64091 bellard
}
749 83f64091 bellard
750 e2731add bellard
static void qcow_close(BlockDriverState *bs)
751 ea2384d3 bellard
{
752 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
753 ea2384d3 bellard
    qemu_free(s->l1_table);
754 ea2384d3 bellard
    qemu_free(s->l2_cache);
755 ea2384d3 bellard
    qemu_free(s->cluster_cache);
756 ea2384d3 bellard
    qemu_free(s->cluster_data);
757 83f64091 bellard
    bdrv_delete(s->hd);
758 ea2384d3 bellard
}
759 ea2384d3 bellard
760 ea2384d3 bellard
static int qcow_create(const char *filename, int64_t total_size,
761 ea2384d3 bellard
                      const char *backing_file, int flags)
762 ea2384d3 bellard
{
763 ea2384d3 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift;
764 ea2384d3 bellard
    QCowHeader header;
765 ea2384d3 bellard
    uint64_t tmp;
766 ea2384d3 bellard
767 83f64091 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
768 ea2384d3 bellard
    if (fd < 0)
769 ea2384d3 bellard
        return -1;
770 ea2384d3 bellard
    memset(&header, 0, sizeof(header));
771 ea2384d3 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
772 ea2384d3 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
773 ea2384d3 bellard
    header.size = cpu_to_be64(total_size * 512);
774 ea2384d3 bellard
    header_size = sizeof(header);
775 ea2384d3 bellard
    backing_filename_len = 0;
776 ea2384d3 bellard
    if (backing_file) {
777 83f64091 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
778 83f64091 bellard
        backing_filename_len = strlen(backing_file);
779 83f64091 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
780 83f64091 bellard
        header_size += backing_filename_len;
781 83f64091 bellard
        header.mtime = cpu_to_be32(0);
782 ea2384d3 bellard
        header.cluster_bits = 9; /* 512 byte cluster to avoid copying
783 ea2384d3 bellard
                                    unmodifyed sectors */
784 ea2384d3 bellard
        header.l2_bits = 12; /* 32 KB L2 tables */
785 ea2384d3 bellard
    } else {
786 ea2384d3 bellard
        header.cluster_bits = 12; /* 4 KB clusters */
787 ea2384d3 bellard
        header.l2_bits = 9; /* 4 KB L2 tables */
788 ea2384d3 bellard
    }
789 ea2384d3 bellard
    header_size = (header_size + 7) & ~7;
790 ea2384d3 bellard
    shift = header.cluster_bits + header.l2_bits;
791 ea2384d3 bellard
    l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
792 ea2384d3 bellard
793 ea2384d3 bellard
    header.l1_table_offset = cpu_to_be64(header_size);
794 ea2384d3 bellard
    if (flags) {
795 ea2384d3 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
796 ea2384d3 bellard
    } else {
797 ea2384d3 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
798 ea2384d3 bellard
    }
799 ea2384d3 bellard
    
800 ea2384d3 bellard
    /* write all the data */
801 ea2384d3 bellard
    write(fd, &header, sizeof(header));
802 ea2384d3 bellard
    if (backing_file) {
803 83f64091 bellard
        write(fd, backing_file, backing_filename_len);
804 ea2384d3 bellard
    }
805 ea2384d3 bellard
    lseek(fd, header_size, SEEK_SET);
806 ea2384d3 bellard
    tmp = 0;
807 ea2384d3 bellard
    for(i = 0;i < l1_size; i++) {
808 ea2384d3 bellard
        write(fd, &tmp, sizeof(tmp));
809 ea2384d3 bellard
    }
810 ea2384d3 bellard
    close(fd);
811 ea2384d3 bellard
    return 0;
812 ea2384d3 bellard
}
813 ea2384d3 bellard
814 95389c86 bellard
int qcow_make_empty(BlockDriverState *bs)
815 95389c86 bellard
{
816 95389c86 bellard
    BDRVQcowState *s = bs->opaque;
817 95389c86 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
818 83f64091 bellard
    int ret;
819 95389c86 bellard
820 95389c86 bellard
    memset(s->l1_table, 0, l1_length);
821 83f64091 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
822 95389c86 bellard
        return -1;
823 83f64091 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
824 83f64091 bellard
    if (ret < 0)
825 83f64091 bellard
        return ret;
826 95389c86 bellard
827 95389c86 bellard
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
828 95389c86 bellard
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
829 95389c86 bellard
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
830 95389c86 bellard
831 95389c86 bellard
    return 0;
832 95389c86 bellard
}
833 95389c86 bellard
834 ea2384d3 bellard
int qcow_get_cluster_size(BlockDriverState *bs)
835 ea2384d3 bellard
{
836 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
837 ea2384d3 bellard
    if (bs->drv != &bdrv_qcow)
838 ea2384d3 bellard
        return -1;
839 ea2384d3 bellard
    return s->cluster_size;
840 ea2384d3 bellard
}
841 ea2384d3 bellard
842 ea2384d3 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
843 ea2384d3 bellard
   tables to avoid losing bytes in alignment */
844 ea2384d3 bellard
int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num, 
845 ea2384d3 bellard
                          const uint8_t *buf)
846 ea2384d3 bellard
{
847 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
848 ea2384d3 bellard
    z_stream strm;
849 ea2384d3 bellard
    int ret, out_len;
850 ea2384d3 bellard
    uint8_t *out_buf;
851 ea2384d3 bellard
    uint64_t cluster_offset;
852 ea2384d3 bellard
853 ea2384d3 bellard
    if (bs->drv != &bdrv_qcow)
854 ea2384d3 bellard
        return -1;
855 ea2384d3 bellard
856 ea2384d3 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
857 ea2384d3 bellard
    if (!out_buf)
858 ea2384d3 bellard
        return -1;
859 ea2384d3 bellard
860 ea2384d3 bellard
    /* best compression, small window, no zlib header */
861 ea2384d3 bellard
    memset(&strm, 0, sizeof(strm));
862 ea2384d3 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
863 ea2384d3 bellard
                       Z_DEFLATED, -12, 
864 ea2384d3 bellard
                       9, Z_DEFAULT_STRATEGY);
865 ea2384d3 bellard
    if (ret != 0) {
866 ea2384d3 bellard
        qemu_free(out_buf);
867 ea2384d3 bellard
        return -1;
868 ea2384d3 bellard
    }
869 ea2384d3 bellard
870 ea2384d3 bellard
    strm.avail_in = s->cluster_size;
871 ea2384d3 bellard
    strm.next_in = (uint8_t *)buf;
872 ea2384d3 bellard
    strm.avail_out = s->cluster_size;
873 ea2384d3 bellard
    strm.next_out = out_buf;
874 ea2384d3 bellard
875 ea2384d3 bellard
    ret = deflate(&strm, Z_FINISH);
876 ea2384d3 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
877 ea2384d3 bellard
        qemu_free(out_buf);
878 ea2384d3 bellard
        deflateEnd(&strm);
879 ea2384d3 bellard
        return -1;
880 ea2384d3 bellard
    }
881 ea2384d3 bellard
    out_len = strm.next_out - out_buf;
882 ea2384d3 bellard
883 ea2384d3 bellard
    deflateEnd(&strm);
884 ea2384d3 bellard
885 ea2384d3 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
886 ea2384d3 bellard
        /* could not compress: write normal cluster */
887 ea2384d3 bellard
        qcow_write(bs, sector_num, buf, s->cluster_sectors);
888 ea2384d3 bellard
    } else {
889 ea2384d3 bellard
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, 
890 ea2384d3 bellard
                                            out_len, 0, 0);
891 ea2384d3 bellard
        cluster_offset &= s->cluster_offset_mask;
892 83f64091 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
893 ea2384d3 bellard
            qemu_free(out_buf);
894 ea2384d3 bellard
            return -1;
895 ea2384d3 bellard
        }
896 ea2384d3 bellard
    }
897 ea2384d3 bellard
    
898 ea2384d3 bellard
    qemu_free(out_buf);
899 ea2384d3 bellard
    return 0;
900 ea2384d3 bellard
}
901 ea2384d3 bellard
902 7a6cba61 pbrook
static void qcow_flush(BlockDriverState *bs)
903 7a6cba61 pbrook
{
904 7a6cba61 pbrook
    BDRVQcowState *s = bs->opaque;
905 83f64091 bellard
    bdrv_flush(s->hd);
906 7a6cba61 pbrook
}
907 7a6cba61 pbrook
908 ea2384d3 bellard
BlockDriver bdrv_qcow = {
909 ea2384d3 bellard
    "qcow",
910 ea2384d3 bellard
    sizeof(BDRVQcowState),
911 ea2384d3 bellard
    qcow_probe,
912 ea2384d3 bellard
    qcow_open,
913 83f64091 bellard
    NULL,
914 83f64091 bellard
    NULL,
915 ea2384d3 bellard
    qcow_close,
916 ea2384d3 bellard
    qcow_create,
917 7a6cba61 pbrook
    qcow_flush,
918 ea2384d3 bellard
    qcow_is_allocated,
919 ea2384d3 bellard
    qcow_set_key,
920 83f64091 bellard
    qcow_make_empty,
921 83f64091 bellard
922 83f64091 bellard
    .bdrv_aio_new = qcow_aio_new,
923 83f64091 bellard
    .bdrv_aio_read = qcow_aio_read,
924 83f64091 bellard
    .bdrv_aio_write = qcow_aio_write,
925 83f64091 bellard
    .bdrv_aio_cancel = qcow_aio_cancel,
926 83f64091 bellard
    .bdrv_aio_delete = qcow_aio_delete,
927 ea2384d3 bellard
};
928 ea2384d3 bellard