Statistics
| Branch: | Revision:

root / block / qcow.c @ 5614c188

History | View | Annotate | Download (30 kB)

1 ea2384d3 bellard
/*
2 ea2384d3 bellard
 * Block driver for the QCOW format
3 5fafdf24 ths
 *
4 83f64091 bellard
 * Copyright (c) 2004-2006 Fabrice Bellard
5 5fafdf24 ths
 *
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 faf07963 pbrook
#include "qemu-common.h"
25 ea2384d3 bellard
#include "block_int.h"
26 5efa9d5a Anthony Liguori
#include "module.h"
27 28d34b82 bellard
#include <zlib.h>
28 ea2384d3 bellard
#include "aes.h"
29 ea2384d3 bellard
30 ea2384d3 bellard
/**************************************************************/
31 ea2384d3 bellard
/* QEMU COW block driver with compression and encryption support */
32 ea2384d3 bellard
33 ea2384d3 bellard
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
34 ea2384d3 bellard
#define QCOW_VERSION 1
35 ea2384d3 bellard
36 ea2384d3 bellard
#define QCOW_CRYPT_NONE 0
37 ea2384d3 bellard
#define QCOW_CRYPT_AES  1
38 ea2384d3 bellard
39 ea2384d3 bellard
#define QCOW_OFLAG_COMPRESSED (1LL << 63)
40 ea2384d3 bellard
41 ea2384d3 bellard
typedef struct QCowHeader {
42 ea2384d3 bellard
    uint32_t magic;
43 ea2384d3 bellard
    uint32_t version;
44 ea2384d3 bellard
    uint64_t backing_file_offset;
45 ea2384d3 bellard
    uint32_t backing_file_size;
46 ea2384d3 bellard
    uint32_t mtime;
47 ea2384d3 bellard
    uint64_t size; /* in bytes */
48 ea2384d3 bellard
    uint8_t cluster_bits;
49 ea2384d3 bellard
    uint8_t l2_bits;
50 ea2384d3 bellard
    uint32_t crypt_method;
51 ea2384d3 bellard
    uint64_t l1_table_offset;
52 ea2384d3 bellard
} QCowHeader;
53 ea2384d3 bellard
54 ea2384d3 bellard
#define L2_CACHE_SIZE 16
55 ea2384d3 bellard
56 ea2384d3 bellard
typedef struct BDRVQcowState {
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 66f82cee Kevin Wolf
static int decompress_cluster(BlockDriverState *bs, 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 3b46e624 ths
84 712e7874 bellard
    if (buf_size >= sizeof(QCowHeader) &&
85 712e7874 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
86 5fafdf24 ths
        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 66f82cee Kevin Wolf
static int qcow_open(BlockDriverState *bs, int flags)
93 ea2384d3 bellard
{
94 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
95 66f82cee Kevin Wolf
    int len, i, shift;
96 ea2384d3 bellard
    QCowHeader header;
97 83f64091 bellard
98 66f82cee Kevin Wolf
    if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
99 ea2384d3 bellard
        goto fail;
100 ea2384d3 bellard
    be32_to_cpus(&header.magic);
101 ea2384d3 bellard
    be32_to_cpus(&header.version);
102 ea2384d3 bellard
    be64_to_cpus(&header.backing_file_offset);
103 ea2384d3 bellard
    be32_to_cpus(&header.backing_file_size);
104 ea2384d3 bellard
    be32_to_cpus(&header.mtime);
105 ea2384d3 bellard
    be64_to_cpus(&header.size);
106 ea2384d3 bellard
    be32_to_cpus(&header.crypt_method);
107 ea2384d3 bellard
    be64_to_cpus(&header.l1_table_offset);
108 3b46e624 ths
109 ea2384d3 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
110 ea2384d3 bellard
        goto fail;
111 ea2384d3 bellard
    if (header.size <= 1 || header.cluster_bits < 9)
112 ea2384d3 bellard
        goto fail;
113 ea2384d3 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
114 ea2384d3 bellard
        goto fail;
115 ea2384d3 bellard
    s->crypt_method_header = header.crypt_method;
116 ea2384d3 bellard
    if (s->crypt_method_header)
117 ea2384d3 bellard
        bs->encrypted = 1;
118 ea2384d3 bellard
    s->cluster_bits = header.cluster_bits;
119 ea2384d3 bellard
    s->cluster_size = 1 << s->cluster_bits;
120 ea2384d3 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
121 ea2384d3 bellard
    s->l2_bits = header.l2_bits;
122 ea2384d3 bellard
    s->l2_size = 1 << s->l2_bits;
123 ea2384d3 bellard
    bs->total_sectors = header.size / 512;
124 ea2384d3 bellard
    s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
125 ea2384d3 bellard
126 ea2384d3 bellard
    /* read the level 1 table */
127 ea2384d3 bellard
    shift = s->cluster_bits + s->l2_bits;
128 ea2384d3 bellard
    s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
129 ea2384d3 bellard
130 ea2384d3 bellard
    s->l1_table_offset = header.l1_table_offset;
131 ea2384d3 bellard
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
132 ea2384d3 bellard
    if (!s->l1_table)
133 ea2384d3 bellard
        goto fail;
134 66f82cee Kevin Wolf
    if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
135 ea2384d3 bellard
        s->l1_size * sizeof(uint64_t))
136 ea2384d3 bellard
        goto fail;
137 ea2384d3 bellard
    for(i = 0;i < s->l1_size; i++) {
138 ea2384d3 bellard
        be64_to_cpus(&s->l1_table[i]);
139 ea2384d3 bellard
    }
140 ea2384d3 bellard
    /* alloc L2 cache */
141 ea2384d3 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
142 ea2384d3 bellard
    if (!s->l2_cache)
143 ea2384d3 bellard
        goto fail;
144 ea2384d3 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
145 ea2384d3 bellard
    if (!s->cluster_cache)
146 ea2384d3 bellard
        goto fail;
147 ea2384d3 bellard
    s->cluster_data = qemu_malloc(s->cluster_size);
148 ea2384d3 bellard
    if (!s->cluster_data)
149 ea2384d3 bellard
        goto fail;
150 ea2384d3 bellard
    s->cluster_cache_offset = -1;
151 3b46e624 ths
152 ea2384d3 bellard
    /* read the backing file name */
153 ea2384d3 bellard
    if (header.backing_file_offset != 0) {
154 ea2384d3 bellard
        len = header.backing_file_size;
155 ea2384d3 bellard
        if (len > 1023)
156 ea2384d3 bellard
            len = 1023;
157 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
158 ea2384d3 bellard
            goto fail;
159 ea2384d3 bellard
        bs->backing_file[len] = '\0';
160 ea2384d3 bellard
    }
161 ea2384d3 bellard
    return 0;
162 ea2384d3 bellard
163 ea2384d3 bellard
 fail:
164 ea2384d3 bellard
    qemu_free(s->l1_table);
165 ea2384d3 bellard
    qemu_free(s->l2_cache);
166 ea2384d3 bellard
    qemu_free(s->cluster_cache);
167 ea2384d3 bellard
    qemu_free(s->cluster_data);
168 ea2384d3 bellard
    return -1;
169 ea2384d3 bellard
}
170 ea2384d3 bellard
171 ea2384d3 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
172 ea2384d3 bellard
{
173 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
174 ea2384d3 bellard
    uint8_t keybuf[16];
175 ea2384d3 bellard
    int len, i;
176 3b46e624 ths
177 ea2384d3 bellard
    memset(keybuf, 0, 16);
178 ea2384d3 bellard
    len = strlen(key);
179 ea2384d3 bellard
    if (len > 16)
180 ea2384d3 bellard
        len = 16;
181 ea2384d3 bellard
    /* XXX: we could compress the chars to 7 bits to increase
182 ea2384d3 bellard
       entropy */
183 ea2384d3 bellard
    for(i = 0;i < len;i++) {
184 ea2384d3 bellard
        keybuf[i] = key[i];
185 ea2384d3 bellard
    }
186 ea2384d3 bellard
    s->crypt_method = s->crypt_method_header;
187 ea2384d3 bellard
188 ea2384d3 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
189 ea2384d3 bellard
        return -1;
190 ea2384d3 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
191 ea2384d3 bellard
        return -1;
192 ea2384d3 bellard
#if 0
193 ea2384d3 bellard
    /* test */
194 ea2384d3 bellard
    {
195 ea2384d3 bellard
        uint8_t in[16];
196 ea2384d3 bellard
        uint8_t out[16];
197 ea2384d3 bellard
        uint8_t tmp[16];
198 ea2384d3 bellard
        for(i=0;i<16;i++)
199 ea2384d3 bellard
            in[i] = i;
200 ea2384d3 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
201 ea2384d3 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
202 ea2384d3 bellard
        for(i = 0; i < 16; i++)
203 ea2384d3 bellard
            printf(" %02x", tmp[i]);
204 ea2384d3 bellard
        printf("\n");
205 ea2384d3 bellard
        for(i = 0; i < 16; i++)
206 ea2384d3 bellard
            printf(" %02x", out[i]);
207 ea2384d3 bellard
        printf("\n");
208 ea2384d3 bellard
    }
209 ea2384d3 bellard
#endif
210 ea2384d3 bellard
    return 0;
211 ea2384d3 bellard
}
212 ea2384d3 bellard
213 ea2384d3 bellard
/* The crypt function is compatible with the linux cryptoloop
214 ea2384d3 bellard
   algorithm for < 4 GB images. NOTE: out_buf == in_buf is
215 ea2384d3 bellard
   supported */
216 ea2384d3 bellard
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
217 ea2384d3 bellard
                            uint8_t *out_buf, const uint8_t *in_buf,
218 ea2384d3 bellard
                            int nb_sectors, int enc,
219 ea2384d3 bellard
                            const AES_KEY *key)
220 ea2384d3 bellard
{
221 ea2384d3 bellard
    union {
222 ea2384d3 bellard
        uint64_t ll[2];
223 ea2384d3 bellard
        uint8_t b[16];
224 ea2384d3 bellard
    } ivec;
225 ea2384d3 bellard
    int i;
226 ea2384d3 bellard
227 ea2384d3 bellard
    for(i = 0; i < nb_sectors; i++) {
228 ea2384d3 bellard
        ivec.ll[0] = cpu_to_le64(sector_num);
229 ea2384d3 bellard
        ivec.ll[1] = 0;
230 5fafdf24 ths
        AES_cbc_encrypt(in_buf, out_buf, 512, key,
231 ea2384d3 bellard
                        ivec.b, enc);
232 ea2384d3 bellard
        sector_num++;
233 ea2384d3 bellard
        in_buf += 512;
234 ea2384d3 bellard
        out_buf += 512;
235 ea2384d3 bellard
    }
236 ea2384d3 bellard
}
237 ea2384d3 bellard
238 ea2384d3 bellard
/* 'allocate' is:
239 ea2384d3 bellard
 *
240 ea2384d3 bellard
 * 0 to not allocate.
241 ea2384d3 bellard
 *
242 ea2384d3 bellard
 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
243 ea2384d3 bellard
 * 'n_end')
244 ea2384d3 bellard
 *
245 ea2384d3 bellard
 * 2 to allocate a compressed cluster of size
246 ea2384d3 bellard
 * 'compressed_size'. 'compressed_size' must be > 0 and <
247 5fafdf24 ths
 * cluster_size
248 ea2384d3 bellard
 *
249 ea2384d3 bellard
 * return 0 if not allocated.
250 ea2384d3 bellard
 */
251 ea2384d3 bellard
static uint64_t get_cluster_offset(BlockDriverState *bs,
252 ea2384d3 bellard
                                   uint64_t offset, int allocate,
253 ea2384d3 bellard
                                   int compressed_size,
254 ea2384d3 bellard
                                   int n_start, int n_end)
255 ea2384d3 bellard
{
256 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
257 ea2384d3 bellard
    int min_index, i, j, l1_index, l2_index;
258 ea2384d3 bellard
    uint64_t l2_offset, *l2_table, cluster_offset, tmp;
259 ea2384d3 bellard
    uint32_t min_count;
260 ea2384d3 bellard
    int new_l2_table;
261 3b46e624 ths
262 ea2384d3 bellard
    l1_index = offset >> (s->l2_bits + s->cluster_bits);
263 ea2384d3 bellard
    l2_offset = s->l1_table[l1_index];
264 ea2384d3 bellard
    new_l2_table = 0;
265 ea2384d3 bellard
    if (!l2_offset) {
266 ea2384d3 bellard
        if (!allocate)
267 ea2384d3 bellard
            return 0;
268 ea2384d3 bellard
        /* allocate a new l2 entry */
269 66f82cee Kevin Wolf
        l2_offset = bdrv_getlength(bs->file);
270 ea2384d3 bellard
        /* round to cluster size */
271 ea2384d3 bellard
        l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
272 ea2384d3 bellard
        /* update the L1 entry */
273 ea2384d3 bellard
        s->l1_table[l1_index] = l2_offset;
274 ea2384d3 bellard
        tmp = cpu_to_be64(l2_offset);
275 5e5557d9 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file,
276 5e5557d9 Kevin Wolf
                s->l1_table_offset + l1_index * sizeof(tmp),
277 5e5557d9 Kevin Wolf
                &tmp, sizeof(tmp)) < 0)
278 ea2384d3 bellard
            return 0;
279 ea2384d3 bellard
        new_l2_table = 1;
280 ea2384d3 bellard
    }
281 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
282 ea2384d3 bellard
        if (l2_offset == s->l2_cache_offsets[i]) {
283 ea2384d3 bellard
            /* increment the hit count */
284 ea2384d3 bellard
            if (++s->l2_cache_counts[i] == 0xffffffff) {
285 ea2384d3 bellard
                for(j = 0; j < L2_CACHE_SIZE; j++) {
286 ea2384d3 bellard
                    s->l2_cache_counts[j] >>= 1;
287 ea2384d3 bellard
                }
288 ea2384d3 bellard
            }
289 ea2384d3 bellard
            l2_table = s->l2_cache + (i << s->l2_bits);
290 ea2384d3 bellard
            goto found;
291 ea2384d3 bellard
        }
292 ea2384d3 bellard
    }
293 ea2384d3 bellard
    /* not found: load a new entry in the least used one */
294 ea2384d3 bellard
    min_index = 0;
295 ea2384d3 bellard
    min_count = 0xffffffff;
296 ea2384d3 bellard
    for(i = 0; i < L2_CACHE_SIZE; i++) {
297 ea2384d3 bellard
        if (s->l2_cache_counts[i] < min_count) {
298 ea2384d3 bellard
            min_count = s->l2_cache_counts[i];
299 ea2384d3 bellard
            min_index = i;
300 ea2384d3 bellard
        }
301 ea2384d3 bellard
    }
302 ea2384d3 bellard
    l2_table = s->l2_cache + (min_index << s->l2_bits);
303 ea2384d3 bellard
    if (new_l2_table) {
304 ea2384d3 bellard
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
305 5e5557d9 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
306 5e5557d9 Kevin Wolf
                s->l2_size * sizeof(uint64_t)) < 0)
307 ea2384d3 bellard
            return 0;
308 ea2384d3 bellard
    } else {
309 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
310 ea2384d3 bellard
            s->l2_size * sizeof(uint64_t))
311 ea2384d3 bellard
            return 0;
312 ea2384d3 bellard
    }
313 ea2384d3 bellard
    s->l2_cache_offsets[min_index] = l2_offset;
314 ea2384d3 bellard
    s->l2_cache_counts[min_index] = 1;
315 ea2384d3 bellard
 found:
316 ea2384d3 bellard
    l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
317 ea2384d3 bellard
    cluster_offset = be64_to_cpu(l2_table[l2_index]);
318 5fafdf24 ths
    if (!cluster_offset ||
319 ea2384d3 bellard
        ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
320 ea2384d3 bellard
        if (!allocate)
321 ea2384d3 bellard
            return 0;
322 ea2384d3 bellard
        /* allocate a new cluster */
323 ea2384d3 bellard
        if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
324 ea2384d3 bellard
            (n_end - n_start) < s->cluster_sectors) {
325 ea2384d3 bellard
            /* if the cluster is already compressed, we must
326 ea2384d3 bellard
               decompress it in the case it is not completely
327 ea2384d3 bellard
               overwritten */
328 66f82cee Kevin Wolf
            if (decompress_cluster(bs, cluster_offset) < 0)
329 ea2384d3 bellard
                return 0;
330 66f82cee Kevin Wolf
            cluster_offset = bdrv_getlength(bs->file);
331 5fafdf24 ths
            cluster_offset = (cluster_offset + s->cluster_size - 1) &
332 ea2384d3 bellard
                ~(s->cluster_size - 1);
333 ea2384d3 bellard
            /* write the cluster content */
334 66f82cee Kevin Wolf
            if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
335 ea2384d3 bellard
                s->cluster_size)
336 ea2384d3 bellard
                return -1;
337 ea2384d3 bellard
        } else {
338 66f82cee Kevin Wolf
            cluster_offset = bdrv_getlength(bs->file);
339 7f48fa1f aliguori
            if (allocate == 1) {
340 7f48fa1f aliguori
                /* round to cluster size */
341 7f48fa1f aliguori
                cluster_offset = (cluster_offset + s->cluster_size - 1) &
342 7f48fa1f aliguori
                    ~(s->cluster_size - 1);
343 66f82cee Kevin Wolf
                bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
344 7f48fa1f aliguori
                /* if encrypted, we must initialize the cluster
345 7f48fa1f aliguori
                   content which won't be written */
346 7f48fa1f aliguori
                if (s->crypt_method &&
347 7f48fa1f aliguori
                    (n_end - n_start) < s->cluster_sectors) {
348 7f48fa1f aliguori
                    uint64_t start_sect;
349 7f48fa1f aliguori
                    start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
350 7f48fa1f aliguori
                    memset(s->cluster_data + 512, 0x00, 512);
351 7f48fa1f aliguori
                    for(i = 0; i < s->cluster_sectors; i++) {
352 7f48fa1f aliguori
                        if (i < n_start || i >= n_end) {
353 7f48fa1f aliguori
                            encrypt_sectors(s, start_sect + i,
354 7f48fa1f aliguori
                                            s->cluster_data,
355 7f48fa1f aliguori
                                            s->cluster_data + 512, 1, 1,
356 7f48fa1f aliguori
                                            &s->aes_encrypt_key);
357 66f82cee Kevin Wolf
                            if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
358 7f48fa1f aliguori
                                            s->cluster_data, 512) != 512)
359 7f48fa1f aliguori
                                return -1;
360 7f48fa1f aliguori
                        }
361 ea2384d3 bellard
                    }
362 ea2384d3 bellard
                }
363 7f48fa1f aliguori
            } else if (allocate == 2) {
364 7f48fa1f aliguori
                cluster_offset |= QCOW_OFLAG_COMPRESSED |
365 7f48fa1f aliguori
                    (uint64_t)compressed_size << (63 - s->cluster_bits);
366 ea2384d3 bellard
            }
367 ea2384d3 bellard
        }
368 ea2384d3 bellard
        /* update L2 table */
369 ea2384d3 bellard
        tmp = cpu_to_be64(cluster_offset);
370 ea2384d3 bellard
        l2_table[l2_index] = tmp;
371 5e5557d9 Kevin Wolf
        if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
372 5e5557d9 Kevin Wolf
                &tmp, sizeof(tmp)) < 0)
373 ea2384d3 bellard
            return 0;
374 ea2384d3 bellard
    }
375 ea2384d3 bellard
    return cluster_offset;
376 ea2384d3 bellard
}
377 ea2384d3 bellard
378 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
379 ea2384d3 bellard
                             int nb_sectors, int *pnum)
380 ea2384d3 bellard
{
381 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
382 ea2384d3 bellard
    int index_in_cluster, n;
383 ea2384d3 bellard
    uint64_t cluster_offset;
384 ea2384d3 bellard
385 ea2384d3 bellard
    cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
386 ea2384d3 bellard
    index_in_cluster = sector_num & (s->cluster_sectors - 1);
387 ea2384d3 bellard
    n = s->cluster_sectors - index_in_cluster;
388 ea2384d3 bellard
    if (n > nb_sectors)
389 ea2384d3 bellard
        n = nb_sectors;
390 ea2384d3 bellard
    *pnum = n;
391 ea2384d3 bellard
    return (cluster_offset != 0);
392 ea2384d3 bellard
}
393 ea2384d3 bellard
394 ea2384d3 bellard
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
395 ea2384d3 bellard
                             const uint8_t *buf, int buf_size)
396 ea2384d3 bellard
{
397 ea2384d3 bellard
    z_stream strm1, *strm = &strm1;
398 ea2384d3 bellard
    int ret, out_len;
399 ea2384d3 bellard
400 ea2384d3 bellard
    memset(strm, 0, sizeof(*strm));
401 ea2384d3 bellard
402 ea2384d3 bellard
    strm->next_in = (uint8_t *)buf;
403 ea2384d3 bellard
    strm->avail_in = buf_size;
404 ea2384d3 bellard
    strm->next_out = out_buf;
405 ea2384d3 bellard
    strm->avail_out = out_buf_size;
406 ea2384d3 bellard
407 ea2384d3 bellard
    ret = inflateInit2(strm, -12);
408 ea2384d3 bellard
    if (ret != Z_OK)
409 ea2384d3 bellard
        return -1;
410 ea2384d3 bellard
    ret = inflate(strm, Z_FINISH);
411 ea2384d3 bellard
    out_len = strm->next_out - out_buf;
412 ea2384d3 bellard
    if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
413 ea2384d3 bellard
        out_len != out_buf_size) {
414 ea2384d3 bellard
        inflateEnd(strm);
415 ea2384d3 bellard
        return -1;
416 ea2384d3 bellard
    }
417 ea2384d3 bellard
    inflateEnd(strm);
418 ea2384d3 bellard
    return 0;
419 ea2384d3 bellard
}
420 3b46e624 ths
421 66f82cee Kevin Wolf
static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
422 ea2384d3 bellard
{
423 66f82cee Kevin Wolf
    BDRVQcowState *s = bs->opaque;
424 ea2384d3 bellard
    int ret, csize;
425 ea2384d3 bellard
    uint64_t coffset;
426 ea2384d3 bellard
427 ea2384d3 bellard
    coffset = cluster_offset & s->cluster_offset_mask;
428 ea2384d3 bellard
    if (s->cluster_cache_offset != coffset) {
429 ea2384d3 bellard
        csize = cluster_offset >> (63 - s->cluster_bits);
430 ea2384d3 bellard
        csize &= (s->cluster_size - 1);
431 66f82cee Kevin Wolf
        ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
432 5fafdf24 ths
        if (ret != csize)
433 ea2384d3 bellard
            return -1;
434 ea2384d3 bellard
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
435 ea2384d3 bellard
                              s->cluster_data, csize) < 0) {
436 ea2384d3 bellard
            return -1;
437 ea2384d3 bellard
        }
438 ea2384d3 bellard
        s->cluster_cache_offset = coffset;
439 ea2384d3 bellard
    }
440 ea2384d3 bellard
    return 0;
441 ea2384d3 bellard
}
442 ea2384d3 bellard
443 83f64091 bellard
#if 0
444 83f64091 bellard

445 5fafdf24 ths
static int qcow_read(BlockDriverState *bs, int64_t sector_num,
446 ea2384d3 bellard
                     uint8_t *buf, int nb_sectors)
447 ea2384d3 bellard
{
448 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
449 ea2384d3 bellard
    int ret, index_in_cluster, n;
450 ea2384d3 bellard
    uint64_t cluster_offset;
451 3b46e624 ths

452 ea2384d3 bellard
    while (nb_sectors > 0) {
453 ea2384d3 bellard
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
454 ea2384d3 bellard
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
455 ea2384d3 bellard
        n = s->cluster_sectors - index_in_cluster;
456 ea2384d3 bellard
        if (n > nb_sectors)
457 ea2384d3 bellard
            n = nb_sectors;
458 ea2384d3 bellard
        if (!cluster_offset) {
459 83f64091 bellard
            if (bs->backing_hd) {
460 83f64091 bellard
                /* read from the base image */
461 83f64091 bellard
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
462 83f64091 bellard
                if (ret < 0)
463 83f64091 bellard
                    return -1;
464 83f64091 bellard
            } else {
465 83f64091 bellard
                memset(buf, 0, 512 * n);
466 83f64091 bellard
            }
467 ea2384d3 bellard
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
468 66f82cee Kevin Wolf
            if (decompress_cluster(bs, cluster_offset) < 0)
469 ea2384d3 bellard
                return -1;
470 ea2384d3 bellard
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
471 ea2384d3 bellard
        } else {
472 66f82cee Kevin Wolf
            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
473 5fafdf24 ths
            if (ret != n * 512)
474 ea2384d3 bellard
                return -1;
475 ea2384d3 bellard
            if (s->crypt_method) {
476 5fafdf24 ths
                encrypt_sectors(s, sector_num, buf, buf, n, 0,
477 ea2384d3 bellard
                                &s->aes_decrypt_key);
478 ea2384d3 bellard
            }
479 ea2384d3 bellard
        }
480 ea2384d3 bellard
        nb_sectors -= n;
481 ea2384d3 bellard
        sector_num += n;
482 ea2384d3 bellard
        buf += n * 512;
483 ea2384d3 bellard
    }
484 ea2384d3 bellard
    return 0;
485 ea2384d3 bellard
}
486 83f64091 bellard
#endif
487 ea2384d3 bellard
488 ce1a14dc pbrook
typedef struct QCowAIOCB {
489 ce1a14dc pbrook
    BlockDriverAIOCB common;
490 83f64091 bellard
    int64_t sector_num;
491 f141eafe aliguori
    QEMUIOVector *qiov;
492 83f64091 bellard
    uint8_t *buf;
493 f141eafe aliguori
    void *orig_buf;
494 83f64091 bellard
    int nb_sectors;
495 83f64091 bellard
    int n;
496 83f64091 bellard
    uint64_t cluster_offset;
497 5fafdf24 ths
    uint8_t *cluster_data;
498 c87c0672 aliguori
    struct iovec hd_iov;
499 c87c0672 aliguori
    QEMUIOVector hd_qiov;
500 83f64091 bellard
    BlockDriverAIOCB *hd_aiocb;
501 83f64091 bellard
} QCowAIOCB;
502 83f64091 bellard
503 c16b5a2c Christoph Hellwig
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
504 c16b5a2c Christoph Hellwig
{
505 b666d239 Kevin Wolf
    QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
506 c16b5a2c Christoph Hellwig
    if (acb->hd_aiocb)
507 c16b5a2c Christoph Hellwig
        bdrv_aio_cancel(acb->hd_aiocb);
508 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
509 c16b5a2c Christoph Hellwig
}
510 c16b5a2c Christoph Hellwig
511 c16b5a2c Christoph Hellwig
static AIOPool qcow_aio_pool = {
512 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(QCowAIOCB),
513 c16b5a2c Christoph Hellwig
    .cancel             = qcow_aio_cancel,
514 c16b5a2c Christoph Hellwig
};
515 ad53089b Christoph Hellwig
516 ad53089b Christoph Hellwig
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
517 ad53089b Christoph Hellwig
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
518 ad53089b Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque, int is_write)
519 ad53089b Christoph Hellwig
{
520 ad53089b Christoph Hellwig
    QCowAIOCB *acb;
521 ad53089b Christoph Hellwig
522 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
523 ad53089b Christoph Hellwig
    if (!acb)
524 ad53089b Christoph Hellwig
        return NULL;
525 ad53089b Christoph Hellwig
    acb->hd_aiocb = NULL;
526 ad53089b Christoph Hellwig
    acb->sector_num = sector_num;
527 ad53089b Christoph Hellwig
    acb->qiov = qiov;
528 ad53089b Christoph Hellwig
    if (qiov->niov > 1) {
529 ad53089b Christoph Hellwig
        acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
530 ad53089b Christoph Hellwig
        if (is_write)
531 ad53089b Christoph Hellwig
            qemu_iovec_to_buffer(qiov, acb->buf);
532 ad53089b Christoph Hellwig
    } else {
533 ad53089b Christoph Hellwig
        acb->buf = (uint8_t *)qiov->iov->iov_base;
534 ad53089b Christoph Hellwig
    }
535 ad53089b Christoph Hellwig
    acb->nb_sectors = nb_sectors;
536 ad53089b Christoph Hellwig
    acb->n = 0;
537 ad53089b Christoph Hellwig
    acb->cluster_offset = 0;
538 ad53089b Christoph Hellwig
    return acb;
539 ad53089b Christoph Hellwig
}
540 ad53089b Christoph Hellwig
541 83f64091 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
542 83f64091 bellard
{
543 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
544 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
545 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
546 83f64091 bellard
    int index_in_cluster;
547 83f64091 bellard
548 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
549 f141eafe aliguori
    if (ret < 0)
550 f141eafe aliguori
        goto done;
551 83f64091 bellard
552 83f64091 bellard
 redo:
553 83f64091 bellard
    /* post process the read buffer */
554 ce1a14dc pbrook
    if (!acb->cluster_offset) {
555 83f64091 bellard
        /* nothing to do */
556 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
557 83f64091 bellard
        /* nothing to do */
558 83f64091 bellard
    } else {
559 83f64091 bellard
        if (s->crypt_method) {
560 5fafdf24 ths
            encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
561 5fafdf24 ths
                            acb->n, 0,
562 83f64091 bellard
                            &s->aes_decrypt_key);
563 83f64091 bellard
        }
564 83f64091 bellard
    }
565 83f64091 bellard
566 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
567 ce1a14dc pbrook
    acb->sector_num += acb->n;
568 ce1a14dc pbrook
    acb->buf += acb->n * 512;
569 83f64091 bellard
570 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
571 83f64091 bellard
        /* request completed */
572 f141eafe aliguori
        ret = 0;
573 f141eafe aliguori
        goto done;
574 83f64091 bellard
    }
575 3b46e624 ths
576 83f64091 bellard
    /* prepare next AIO request */
577 5fafdf24 ths
    acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9,
578 ce1a14dc pbrook
                                             0, 0, 0, 0);
579 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
580 ce1a14dc pbrook
    acb->n = s->cluster_sectors - index_in_cluster;
581 ce1a14dc pbrook
    if (acb->n > acb->nb_sectors)
582 ce1a14dc pbrook
        acb->n = acb->nb_sectors;
583 ce1a14dc pbrook
584 ce1a14dc pbrook
    if (!acb->cluster_offset) {
585 83f64091 bellard
        if (bs->backing_hd) {
586 83f64091 bellard
            /* read from the base image */
587 3f4cb3d3 blueswir1
            acb->hd_iov.iov_base = (void *)acb->buf;
588 c87c0672 aliguori
            acb->hd_iov.iov_len = acb->n * 512;
589 c87c0672 aliguori
            qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
590 c87c0672 aliguori
            acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
591 c87c0672 aliguori
                &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
592 5614c188 Stefan Weil
            if (acb->hd_aiocb == NULL) {
593 5614c188 Stefan Weil
                ret = -EIO;
594 f141eafe aliguori
                goto done;
595 5614c188 Stefan Weil
            }
596 83f64091 bellard
        } else {
597 83f64091 bellard
            /* Note: in this case, no need to wait */
598 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
599 83f64091 bellard
            goto redo;
600 83f64091 bellard
        }
601 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
602 83f64091 bellard
        /* add AIO support for compressed blocks ? */
603 5614c188 Stefan Weil
        if (decompress_cluster(bs, acb->cluster_offset) < 0) {
604 5614c188 Stefan Weil
            ret = -EIO;
605 f141eafe aliguori
            goto done;
606 5614c188 Stefan Weil
        }
607 5fafdf24 ths
        memcpy(acb->buf,
608 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
609 83f64091 bellard
        goto redo;
610 83f64091 bellard
    } else {
611 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
612 83f64091 bellard
            ret = -EIO;
613 f141eafe aliguori
            goto done;
614 83f64091 bellard
        }
615 3f4cb3d3 blueswir1
        acb->hd_iov.iov_base = (void *)acb->buf;
616 c87c0672 aliguori
        acb->hd_iov.iov_len = acb->n * 512;
617 c87c0672 aliguori
        qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
618 66f82cee Kevin Wolf
        acb->hd_aiocb = bdrv_aio_readv(bs->file,
619 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
620 c87c0672 aliguori
                            &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
621 5614c188 Stefan Weil
        if (acb->hd_aiocb == NULL) {
622 5614c188 Stefan Weil
            ret = -EIO;
623 f141eafe aliguori
            goto done;
624 5614c188 Stefan Weil
        }
625 f141eafe aliguori
    }
626 f141eafe aliguori
627 f141eafe aliguori
    return;
628 f141eafe aliguori
629 f141eafe aliguori
done:
630 f141eafe aliguori
    if (acb->qiov->niov > 1) {
631 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
632 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
633 83f64091 bellard
    }
634 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
635 f141eafe aliguori
    qemu_aio_release(acb);
636 83f64091 bellard
}
637 83f64091 bellard
638 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
639 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
640 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
641 83f64091 bellard
{
642 ce1a14dc pbrook
    QCowAIOCB *acb;
643 ce1a14dc pbrook
644 ad53089b Christoph Hellwig
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
645 ce1a14dc pbrook
    if (!acb)
646 ce1a14dc pbrook
        return NULL;
647 83f64091 bellard
648 83f64091 bellard
    qcow_aio_read_cb(acb, 0);
649 ce1a14dc pbrook
    return &acb->common;
650 83f64091 bellard
}
651 83f64091 bellard
652 83f64091 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
653 83f64091 bellard
{
654 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
655 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
656 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
657 83f64091 bellard
    int index_in_cluster;
658 83f64091 bellard
    uint64_t cluster_offset;
659 83f64091 bellard
    const uint8_t *src_buf;
660 ce1a14dc pbrook
661 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
662 ce1a14dc pbrook
663 f141eafe aliguori
    if (ret < 0)
664 f141eafe aliguori
        goto done;
665 83f64091 bellard
666 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
667 ce1a14dc pbrook
    acb->sector_num += acb->n;
668 ce1a14dc pbrook
    acb->buf += acb->n * 512;
669 83f64091 bellard
670 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
671 83f64091 bellard
        /* request completed */
672 f141eafe aliguori
        ret = 0;
673 f141eafe aliguori
        goto done;
674 83f64091 bellard
    }
675 3b46e624 ths
676 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
677 ce1a14dc pbrook
    acb->n = s->cluster_sectors - index_in_cluster;
678 ce1a14dc pbrook
    if (acb->n > acb->nb_sectors)
679 ce1a14dc pbrook
        acb->n = acb->nb_sectors;
680 5fafdf24 ths
    cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0,
681 5fafdf24 ths
                                        index_in_cluster,
682 ce1a14dc pbrook
                                        index_in_cluster + acb->n);
683 83f64091 bellard
    if (!cluster_offset || (cluster_offset & 511) != 0) {
684 83f64091 bellard
        ret = -EIO;
685 f141eafe aliguori
        goto done;
686 83f64091 bellard
    }
687 83f64091 bellard
    if (s->crypt_method) {
688 ce1a14dc pbrook
        if (!acb->cluster_data) {
689 ce1a14dc pbrook
            acb->cluster_data = qemu_mallocz(s->cluster_size);
690 ce1a14dc pbrook
            if (!acb->cluster_data) {
691 83f64091 bellard
                ret = -ENOMEM;
692 f141eafe aliguori
                goto done;
693 83f64091 bellard
            }
694 83f64091 bellard
        }
695 5fafdf24 ths
        encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
696 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
697 ce1a14dc pbrook
        src_buf = acb->cluster_data;
698 83f64091 bellard
    } else {
699 ce1a14dc pbrook
        src_buf = acb->buf;
700 83f64091 bellard
    }
701 c87c0672 aliguori
702 c87c0672 aliguori
    acb->hd_iov.iov_base = (void *)src_buf;
703 c87c0672 aliguori
    acb->hd_iov.iov_len = acb->n * 512;
704 c87c0672 aliguori
    qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
705 66f82cee Kevin Wolf
    acb->hd_aiocb = bdrv_aio_writev(bs->file,
706 c87c0672 aliguori
                                    (cluster_offset >> 9) + index_in_cluster,
707 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
708 c87c0672 aliguori
                                    qcow_aio_write_cb, acb);
709 5614c188 Stefan Weil
    if (acb->hd_aiocb == NULL) {
710 5614c188 Stefan Weil
        ret = -EIO;
711 f141eafe aliguori
        goto done;
712 5614c188 Stefan Weil
    }
713 f141eafe aliguori
    return;
714 f141eafe aliguori
715 f141eafe aliguori
done:
716 f141eafe aliguori
    if (acb->qiov->niov > 1)
717 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
718 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
719 f141eafe aliguori
    qemu_aio_release(acb);
720 83f64091 bellard
}
721 83f64091 bellard
722 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
723 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
724 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
725 83f64091 bellard
{
726 83f64091 bellard
    BDRVQcowState *s = bs->opaque;
727 ce1a14dc pbrook
    QCowAIOCB *acb;
728 3b46e624 ths
729 83f64091 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
730 83f64091 bellard
731 c53ffce9 Kevin Wolf
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
732 ce1a14dc pbrook
    if (!acb)
733 ce1a14dc pbrook
        return NULL;
734 ad53089b Christoph Hellwig
735 3b46e624 ths
736 83f64091 bellard
    qcow_aio_write_cb(acb, 0);
737 ce1a14dc pbrook
    return &acb->common;
738 83f64091 bellard
}
739 83f64091 bellard
740 e2731add bellard
static void qcow_close(BlockDriverState *bs)
741 ea2384d3 bellard
{
742 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
743 ea2384d3 bellard
    qemu_free(s->l1_table);
744 ea2384d3 bellard
    qemu_free(s->l2_cache);
745 ea2384d3 bellard
    qemu_free(s->cluster_cache);
746 ea2384d3 bellard
    qemu_free(s->cluster_data);
747 ea2384d3 bellard
}
748 ea2384d3 bellard
749 0e7e1989 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
750 ea2384d3 bellard
{
751 ea2384d3 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift;
752 ea2384d3 bellard
    QCowHeader header;
753 ea2384d3 bellard
    uint64_t tmp;
754 0e7e1989 Kevin Wolf
    int64_t total_size = 0;
755 0e7e1989 Kevin Wolf
    const char *backing_file = NULL;
756 0e7e1989 Kevin Wolf
    int flags = 0;
757 3e1a8134 Kirill A. Shutemov
    int ret;
758 0e7e1989 Kevin Wolf
759 0e7e1989 Kevin Wolf
    /* Read out options */
760 0e7e1989 Kevin Wolf
    while (options && options->name) {
761 0e7e1989 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
762 0e7e1989 Kevin Wolf
            total_size = options->value.n / 512;
763 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
764 0e7e1989 Kevin Wolf
            backing_file = options->value.s;
765 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
766 0e7e1989 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
767 0e7e1989 Kevin Wolf
        }
768 0e7e1989 Kevin Wolf
        options++;
769 0e7e1989 Kevin Wolf
    }
770 ea2384d3 bellard
771 83f64091 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
772 ea2384d3 bellard
    if (fd < 0)
773 98c2b2f4 Juan Quintela
        return -errno;
774 ea2384d3 bellard
    memset(&header, 0, sizeof(header));
775 ea2384d3 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
776 ea2384d3 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
777 ea2384d3 bellard
    header.size = cpu_to_be64(total_size * 512);
778 ea2384d3 bellard
    header_size = sizeof(header);
779 ea2384d3 bellard
    backing_filename_len = 0;
780 ea2384d3 bellard
    if (backing_file) {
781 7852e5da aurel32
        if (strcmp(backing_file, "fat:")) {
782 7852e5da aurel32
            header.backing_file_offset = cpu_to_be64(header_size);
783 7852e5da aurel32
            backing_filename_len = strlen(backing_file);
784 7852e5da aurel32
            header.backing_file_size = cpu_to_be32(backing_filename_len);
785 7852e5da aurel32
            header_size += backing_filename_len;
786 7852e5da aurel32
        } else {
787 7852e5da aurel32
            /* special backing file for vvfat */
788 7852e5da aurel32
            backing_file = NULL;
789 7852e5da aurel32
        }
790 ea2384d3 bellard
        header.cluster_bits = 9; /* 512 byte cluster to avoid copying
791 ea2384d3 bellard
                                    unmodifyed sectors */
792 ea2384d3 bellard
        header.l2_bits = 12; /* 32 KB L2 tables */
793 ea2384d3 bellard
    } else {
794 ea2384d3 bellard
        header.cluster_bits = 12; /* 4 KB clusters */
795 ea2384d3 bellard
        header.l2_bits = 9; /* 4 KB L2 tables */
796 ea2384d3 bellard
    }
797 ea2384d3 bellard
    header_size = (header_size + 7) & ~7;
798 ea2384d3 bellard
    shift = header.cluster_bits + header.l2_bits;
799 ea2384d3 bellard
    l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
800 ea2384d3 bellard
801 ea2384d3 bellard
    header.l1_table_offset = cpu_to_be64(header_size);
802 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT) {
803 ea2384d3 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
804 ea2384d3 bellard
    } else {
805 ea2384d3 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
806 ea2384d3 bellard
    }
807 3b46e624 ths
808 ea2384d3 bellard
    /* write all the data */
809 3e1a8134 Kirill A. Shutemov
    ret = qemu_write_full(fd, &header, sizeof(header));
810 3e1a8134 Kirill A. Shutemov
    if (ret != sizeof(header)) {
811 98c2b2f4 Juan Quintela
        ret = -errno;
812 3e1a8134 Kirill A. Shutemov
        goto exit;
813 3e1a8134 Kirill A. Shutemov
    }
814 3e1a8134 Kirill A. Shutemov
815 ea2384d3 bellard
    if (backing_file) {
816 3e1a8134 Kirill A. Shutemov
        ret = qemu_write_full(fd, backing_file, backing_filename_len);
817 3e1a8134 Kirill A. Shutemov
        if (ret != backing_filename_len) {
818 98c2b2f4 Juan Quintela
            ret = -errno;
819 3e1a8134 Kirill A. Shutemov
            goto exit;
820 3e1a8134 Kirill A. Shutemov
        }
821 3e1a8134 Kirill A. Shutemov
822 ea2384d3 bellard
    }
823 ea2384d3 bellard
    lseek(fd, header_size, SEEK_SET);
824 ea2384d3 bellard
    tmp = 0;
825 ea2384d3 bellard
    for(i = 0;i < l1_size; i++) {
826 3e1a8134 Kirill A. Shutemov
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
827 3e1a8134 Kirill A. Shutemov
        if (ret != sizeof(tmp)) {
828 98c2b2f4 Juan Quintela
            ret = -errno;
829 3e1a8134 Kirill A. Shutemov
            goto exit;
830 3e1a8134 Kirill A. Shutemov
        }
831 ea2384d3 bellard
    }
832 3e1a8134 Kirill A. Shutemov
833 3e1a8134 Kirill A. Shutemov
    ret = 0;
834 3e1a8134 Kirill A. Shutemov
exit:
835 ea2384d3 bellard
    close(fd);
836 3e1a8134 Kirill A. Shutemov
    return ret;
837 ea2384d3 bellard
}
838 ea2384d3 bellard
839 c47c33b0 bellard
static int qcow_make_empty(BlockDriverState *bs)
840 95389c86 bellard
{
841 95389c86 bellard
    BDRVQcowState *s = bs->opaque;
842 95389c86 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
843 83f64091 bellard
    int ret;
844 95389c86 bellard
845 95389c86 bellard
    memset(s->l1_table, 0, l1_length);
846 5e5557d9 Kevin Wolf
    if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
847 5e5557d9 Kevin Wolf
            l1_length) < 0)
848 5e5557d9 Kevin Wolf
        return -1;
849 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
850 83f64091 bellard
    if (ret < 0)
851 83f64091 bellard
        return ret;
852 95389c86 bellard
853 95389c86 bellard
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
854 95389c86 bellard
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
855 95389c86 bellard
    memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
856 95389c86 bellard
857 95389c86 bellard
    return 0;
858 95389c86 bellard
}
859 95389c86 bellard
860 ea2384d3 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
861 ea2384d3 bellard
   tables to avoid losing bytes in alignment */
862 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
863 c47c33b0 bellard
                                 const uint8_t *buf, int nb_sectors)
864 ea2384d3 bellard
{
865 ea2384d3 bellard
    BDRVQcowState *s = bs->opaque;
866 ea2384d3 bellard
    z_stream strm;
867 ea2384d3 bellard
    int ret, out_len;
868 ea2384d3 bellard
    uint8_t *out_buf;
869 ea2384d3 bellard
    uint64_t cluster_offset;
870 ea2384d3 bellard
871 c47c33b0 bellard
    if (nb_sectors != s->cluster_sectors)
872 c47c33b0 bellard
        return -EINVAL;
873 ea2384d3 bellard
874 ea2384d3 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
875 ea2384d3 bellard
    if (!out_buf)
876 ea2384d3 bellard
        return -1;
877 ea2384d3 bellard
878 ea2384d3 bellard
    /* best compression, small window, no zlib header */
879 ea2384d3 bellard
    memset(&strm, 0, sizeof(strm));
880 ea2384d3 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
881 5fafdf24 ths
                       Z_DEFLATED, -12,
882 ea2384d3 bellard
                       9, Z_DEFAULT_STRATEGY);
883 ea2384d3 bellard
    if (ret != 0) {
884 ea2384d3 bellard
        qemu_free(out_buf);
885 ea2384d3 bellard
        return -1;
886 ea2384d3 bellard
    }
887 ea2384d3 bellard
888 ea2384d3 bellard
    strm.avail_in = s->cluster_size;
889 ea2384d3 bellard
    strm.next_in = (uint8_t *)buf;
890 ea2384d3 bellard
    strm.avail_out = s->cluster_size;
891 ea2384d3 bellard
    strm.next_out = out_buf;
892 ea2384d3 bellard
893 ea2384d3 bellard
    ret = deflate(&strm, Z_FINISH);
894 ea2384d3 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
895 ea2384d3 bellard
        qemu_free(out_buf);
896 ea2384d3 bellard
        deflateEnd(&strm);
897 ea2384d3 bellard
        return -1;
898 ea2384d3 bellard
    }
899 ea2384d3 bellard
    out_len = strm.next_out - out_buf;
900 ea2384d3 bellard
901 ea2384d3 bellard
    deflateEnd(&strm);
902 ea2384d3 bellard
903 ea2384d3 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
904 ea2384d3 bellard
        /* could not compress: write normal cluster */
905 ade40677 Kevin Wolf
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
906 ea2384d3 bellard
    } else {
907 5fafdf24 ths
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
908 ea2384d3 bellard
                                            out_len, 0, 0);
909 ea2384d3 bellard
        cluster_offset &= s->cluster_offset_mask;
910 66f82cee Kevin Wolf
        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
911 ea2384d3 bellard
            qemu_free(out_buf);
912 ea2384d3 bellard
            return -1;
913 ea2384d3 bellard
        }
914 ea2384d3 bellard
    }
915 3b46e624 ths
916 ea2384d3 bellard
    qemu_free(out_buf);
917 ea2384d3 bellard
    return 0;
918 ea2384d3 bellard
}
919 ea2384d3 bellard
920 205ef796 Kevin Wolf
static int qcow_flush(BlockDriverState *bs)
921 7a6cba61 pbrook
{
922 205ef796 Kevin Wolf
    return bdrv_flush(bs->file);
923 7a6cba61 pbrook
}
924 7a6cba61 pbrook
925 f8012c13 Kevin Wolf
static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
926 f8012c13 Kevin Wolf
        BlockDriverCompletionFunc *cb, void *opaque)
927 f8012c13 Kevin Wolf
{
928 66f82cee Kevin Wolf
    return bdrv_aio_flush(bs->file, cb, opaque);
929 f8012c13 Kevin Wolf
}
930 f8012c13 Kevin Wolf
931 c47c33b0 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
932 c47c33b0 bellard
{
933 c47c33b0 bellard
    BDRVQcowState *s = bs->opaque;
934 c47c33b0 bellard
    bdi->cluster_size = s->cluster_size;
935 c47c33b0 bellard
    return 0;
936 c47c33b0 bellard
}
937 c47c33b0 bellard
938 0e7e1989 Kevin Wolf
939 0e7e1989 Kevin Wolf
static QEMUOptionParameter qcow_create_options[] = {
940 db08adf5 Kevin Wolf
    {
941 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_SIZE,
942 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
943 db08adf5 Kevin Wolf
        .help = "Virtual disk size"
944 db08adf5 Kevin Wolf
    },
945 db08adf5 Kevin Wolf
    {
946 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FILE,
947 db08adf5 Kevin Wolf
        .type = OPT_STRING,
948 db08adf5 Kevin Wolf
        .help = "File name of a base image"
949 db08adf5 Kevin Wolf
    },
950 db08adf5 Kevin Wolf
    {
951 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_ENCRYPT,
952 db08adf5 Kevin Wolf
        .type = OPT_FLAG,
953 db08adf5 Kevin Wolf
        .help = "Encrypt the image"
954 db08adf5 Kevin Wolf
    },
955 0e7e1989 Kevin Wolf
    { NULL }
956 0e7e1989 Kevin Wolf
};
957 0e7e1989 Kevin Wolf
958 5efa9d5a Anthony Liguori
static BlockDriver bdrv_qcow = {
959 e60f469c aurel32
    .format_name        = "qcow",
960 e60f469c aurel32
    .instance_size        = sizeof(BDRVQcowState),
961 e60f469c aurel32
    .bdrv_probe                = qcow_probe,
962 e60f469c aurel32
    .bdrv_open                = qcow_open,
963 e60f469c aurel32
    .bdrv_close                = qcow_close,
964 e60f469c aurel32
    .bdrv_create        = qcow_create,
965 e60f469c aurel32
    .bdrv_flush                = qcow_flush,
966 e60f469c aurel32
    .bdrv_is_allocated        = qcow_is_allocated,
967 e60f469c aurel32
    .bdrv_set_key        = qcow_set_key,
968 e60f469c aurel32
    .bdrv_make_empty        = qcow_make_empty,
969 f141eafe aliguori
    .bdrv_aio_readv        = qcow_aio_readv,
970 f141eafe aliguori
    .bdrv_aio_writev        = qcow_aio_writev,
971 f8012c13 Kevin Wolf
    .bdrv_aio_flush        = qcow_aio_flush,
972 c47c33b0 bellard
    .bdrv_write_compressed = qcow_write_compressed,
973 e60f469c aurel32
    .bdrv_get_info        = qcow_get_info,
974 0e7e1989 Kevin Wolf
975 0e7e1989 Kevin Wolf
    .create_options = qcow_create_options,
976 ea2384d3 bellard
};
977 5efa9d5a Anthony Liguori
978 5efa9d5a Anthony Liguori
static void bdrv_qcow_init(void)
979 5efa9d5a Anthony Liguori
{
980 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow);
981 5efa9d5a Anthony Liguori
}
982 5efa9d5a Anthony Liguori
983 5efa9d5a Anthony Liguori
block_init(bdrv_qcow_init);