Statistics
| Branch: | Revision:

root / block / qcow.c @ 1e5b9d2f

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

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

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