Statistics
| Branch: | Revision:

root / block / qcow2.c @ 99cce9fa

History | View | Annotate | Download (40.5 kB)

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

36 585f8587 bellard
  - Support for multiple incremental snapshots.
37 585f8587 bellard
  - Memory management by reference counts.
38 585f8587 bellard
  - Clusters which have a reference count of one have the bit
39 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
40 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
41 585f8587 bellard
    in the cluster offsets.
42 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
43 3b46e624 ths
    snapshots.
44 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
45 585f8587 bellard
    (could be backported to QCOW).
46 585f8587 bellard
  - L2 tables have always a size of one cluster.
47 585f8587 bellard
*/
48 585f8587 bellard
49 9b80ddf3 aliguori
50 9b80ddf3 aliguori
typedef struct {
51 9b80ddf3 aliguori
    uint32_t magic;
52 9b80ddf3 aliguori
    uint32_t len;
53 9b80ddf3 aliguori
} QCowExtension;
54 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_END 0
55 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
56 9b80ddf3 aliguori
57 7c80ab3f Jes Sorensen
static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
58 585f8587 bellard
{
59 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
60 3b46e624 ths
61 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
62 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
63 e8cdcec1 Kevin Wolf
        be32_to_cpu(cow_header->version) >= QCOW_VERSION)
64 585f8587 bellard
        return 100;
65 585f8587 bellard
    else
66 585f8587 bellard
        return 0;
67 585f8587 bellard
}
68 585f8587 bellard
69 9b80ddf3 aliguori
70 9b80ddf3 aliguori
/* 
71 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
72 9b80ddf3 aliguori
 * start reading from start_offset
73 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
74 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
75 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
76 9b80ddf3 aliguori
 */
77 7c80ab3f Jes Sorensen
static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
78 7c80ab3f Jes Sorensen
                                 uint64_t end_offset)
79 9b80ddf3 aliguori
{
80 9b80ddf3 aliguori
    QCowExtension ext;
81 9b80ddf3 aliguori
    uint64_t offset;
82 9b80ddf3 aliguori
83 9b80ddf3 aliguori
#ifdef DEBUG_EXT
84 7c80ab3f Jes Sorensen
    printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
85 9b80ddf3 aliguori
#endif
86 9b80ddf3 aliguori
    offset = start_offset;
87 9b80ddf3 aliguori
    while (offset < end_offset) {
88 9b80ddf3 aliguori
89 9b80ddf3 aliguori
#ifdef DEBUG_EXT
90 9b80ddf3 aliguori
        /* Sanity check */
91 9b80ddf3 aliguori
        if (offset > s->cluster_size)
92 7c80ab3f Jes Sorensen
            printf("qcow2_read_extension: suspicious offset %lu\n", offset);
93 9b80ddf3 aliguori
94 9b80ddf3 aliguori
        printf("attemting to read extended header in offset %lu\n", offset);
95 9b80ddf3 aliguori
#endif
96 9b80ddf3 aliguori
97 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
98 7c80ab3f Jes Sorensen
            fprintf(stderr, "qcow2_read_extension: ERROR: "
99 0bfcd599 Blue Swirl
                    "pread fail from offset %" PRIu64 "\n",
100 0bfcd599 Blue Swirl
                    offset);
101 9b80ddf3 aliguori
            return 1;
102 9b80ddf3 aliguori
        }
103 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
104 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
105 9b80ddf3 aliguori
        offset += sizeof(ext);
106 9b80ddf3 aliguori
#ifdef DEBUG_EXT
107 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
108 9b80ddf3 aliguori
#endif
109 9b80ddf3 aliguori
        switch (ext.magic) {
110 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_END:
111 9b80ddf3 aliguori
            return 0;
112 f965509c aliguori
113 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_BACKING_FORMAT:
114 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
115 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
116 4c978075 aliguori
                        " (>=%zu)\n",
117 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
118 f965509c aliguori
                return 2;
119 f965509c aliguori
            }
120 66f82cee Kevin Wolf
            if (bdrv_pread(bs->file, offset , bs->backing_format,
121 f965509c aliguori
                           ext.len) != ext.len)
122 f965509c aliguori
                return 3;
123 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
124 f965509c aliguori
#ifdef DEBUG_EXT
125 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
126 f965509c aliguori
#endif
127 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
128 f965509c aliguori
            break;
129 f965509c aliguori
130 9b80ddf3 aliguori
        default:
131 9b80ddf3 aliguori
            /* unknown magic -- just skip it */
132 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
133 9b80ddf3 aliguori
            break;
134 9b80ddf3 aliguori
        }
135 9b80ddf3 aliguori
    }
136 9b80ddf3 aliguori
137 9b80ddf3 aliguori
    return 0;
138 9b80ddf3 aliguori
}
139 9b80ddf3 aliguori
140 9b80ddf3 aliguori
141 7c80ab3f Jes Sorensen
static int qcow2_open(BlockDriverState *bs, int flags)
142 585f8587 bellard
{
143 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
144 6d85a57e Jes Sorensen
    int len, i, ret = 0;
145 585f8587 bellard
    QCowHeader header;
146 9b80ddf3 aliguori
    uint64_t ext_end;
147 29c1a730 Kevin Wolf
    bool writethrough;
148 585f8587 bellard
149 6d85a57e Jes Sorensen
    ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
150 6d85a57e Jes Sorensen
    if (ret < 0) {
151 585f8587 bellard
        goto fail;
152 6d85a57e Jes Sorensen
    }
153 585f8587 bellard
    be32_to_cpus(&header.magic);
154 585f8587 bellard
    be32_to_cpus(&header.version);
155 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
156 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
157 585f8587 bellard
    be64_to_cpus(&header.size);
158 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
159 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
160 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
161 585f8587 bellard
    be32_to_cpus(&header.l1_size);
162 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
163 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
164 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
165 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
166 3b46e624 ths
167 e8cdcec1 Kevin Wolf
    if (header.magic != QCOW_MAGIC) {
168 6d85a57e Jes Sorensen
        ret = -EINVAL;
169 585f8587 bellard
        goto fail;
170 6d85a57e Jes Sorensen
    }
171 e8cdcec1 Kevin Wolf
    if (header.version != QCOW_VERSION) {
172 e8cdcec1 Kevin Wolf
        char version[64];
173 e8cdcec1 Kevin Wolf
        snprintf(version, sizeof(version), "QCOW version %d", header.version);
174 e8cdcec1 Kevin Wolf
        qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
175 e8cdcec1 Kevin Wolf
            bs->device_name, "qcow2", version);
176 e8cdcec1 Kevin Wolf
        ret = -ENOTSUP;
177 e8cdcec1 Kevin Wolf
        goto fail;
178 e8cdcec1 Kevin Wolf
    }
179 d191d12d Stefan Weil
    if (header.cluster_bits < MIN_CLUSTER_BITS ||
180 6d85a57e Jes Sorensen
        header.cluster_bits > MAX_CLUSTER_BITS) {
181 6d85a57e Jes Sorensen
        ret = -EINVAL;
182 585f8587 bellard
        goto fail;
183 6d85a57e Jes Sorensen
    }
184 6d85a57e Jes Sorensen
    if (header.crypt_method > QCOW_CRYPT_AES) {
185 6d85a57e Jes Sorensen
        ret = -EINVAL;
186 585f8587 bellard
        goto fail;
187 6d85a57e Jes Sorensen
    }
188 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
189 6d85a57e Jes Sorensen
    if (s->crypt_method_header) {
190 585f8587 bellard
        bs->encrypted = 1;
191 6d85a57e Jes Sorensen
    }
192 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
193 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
194 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
195 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
196 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
197 585f8587 bellard
    bs->total_sectors = header.size / 512;
198 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
199 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
200 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
201 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
202 5fafdf24 ths
    s->refcount_table_size =
203 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
204 585f8587 bellard
205 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
206 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
207 585f8587 bellard
208 585f8587 bellard
    /* read the level 1 table */
209 585f8587 bellard
    s->l1_size = header.l1_size;
210 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = size_to_l1(s, header.size);
211 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
212 585f8587 bellard
       header.size bytes */
213 6d85a57e Jes Sorensen
    if (s->l1_size < s->l1_vm_state_index) {
214 6d85a57e Jes Sorensen
        ret = -EINVAL;
215 585f8587 bellard
        goto fail;
216 6d85a57e Jes Sorensen
    }
217 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
218 d191d12d Stefan Weil
    if (s->l1_size > 0) {
219 d191d12d Stefan Weil
        s->l1_table = qemu_mallocz(
220 d191d12d Stefan Weil
            align_offset(s->l1_size * sizeof(uint64_t), 512));
221 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
222 6d85a57e Jes Sorensen
                         s->l1_size * sizeof(uint64_t));
223 6d85a57e Jes Sorensen
        if (ret < 0) {
224 d191d12d Stefan Weil
            goto fail;
225 6d85a57e Jes Sorensen
        }
226 d191d12d Stefan Weil
        for(i = 0;i < s->l1_size; i++) {
227 d191d12d Stefan Weil
            be64_to_cpus(&s->l1_table[i]);
228 d191d12d Stefan Weil
        }
229 585f8587 bellard
    }
230 29c1a730 Kevin Wolf
231 29c1a730 Kevin Wolf
    /* alloc L2 table/refcount block cache */
232 a6599793 Christoph Hellwig
    writethrough = ((flags & BDRV_O_CACHE_WB) == 0);
233 29c1a730 Kevin Wolf
    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE, writethrough);
234 29c1a730 Kevin Wolf
    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE,
235 29c1a730 Kevin Wolf
        writethrough);
236 29c1a730 Kevin Wolf
237 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
238 585f8587 bellard
    /* one more sector for decompressed data alignment */
239 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
240 095a9c58 aliguori
                                  + 512);
241 585f8587 bellard
    s->cluster_cache_offset = -1;
242 3b46e624 ths
243 6d85a57e Jes Sorensen
    ret = qcow2_refcount_init(bs);
244 6d85a57e Jes Sorensen
    if (ret != 0) {
245 585f8587 bellard
        goto fail;
246 6d85a57e Jes Sorensen
    }
247 585f8587 bellard
248 72cf2d4f Blue Swirl
    QLIST_INIT(&s->cluster_allocs);
249 f214978a Kevin Wolf
250 9b80ddf3 aliguori
    /* read qcow2 extensions */
251 6d85a57e Jes Sorensen
    if (header.backing_file_offset) {
252 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
253 6d85a57e Jes Sorensen
    } else {
254 9b80ddf3 aliguori
        ext_end = s->cluster_size;
255 6d85a57e Jes Sorensen
    }
256 6d85a57e Jes Sorensen
    if (qcow2_read_extensions(bs, sizeof(header), ext_end)) {
257 6d85a57e Jes Sorensen
        ret = -EINVAL;
258 9b80ddf3 aliguori
        goto fail;
259 6d85a57e Jes Sorensen
    }
260 9b80ddf3 aliguori
261 585f8587 bellard
    /* read the backing file name */
262 585f8587 bellard
    if (header.backing_file_offset != 0) {
263 585f8587 bellard
        len = header.backing_file_size;
264 6d85a57e Jes Sorensen
        if (len > 1023) {
265 585f8587 bellard
            len = 1023;
266 6d85a57e Jes Sorensen
        }
267 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, header.backing_file_offset,
268 6d85a57e Jes Sorensen
                         bs->backing_file, len);
269 6d85a57e Jes Sorensen
        if (ret < 0) {
270 585f8587 bellard
            goto fail;
271 6d85a57e Jes Sorensen
        }
272 585f8587 bellard
        bs->backing_file[len] = '\0';
273 585f8587 bellard
    }
274 6d85a57e Jes Sorensen
    if (qcow2_read_snapshots(bs) < 0) {
275 6d85a57e Jes Sorensen
        ret = -EINVAL;
276 585f8587 bellard
        goto fail;
277 6d85a57e Jes Sorensen
    }
278 585f8587 bellard
279 585f8587 bellard
#ifdef DEBUG_ALLOC
280 14899cdf Filip Navara
    qcow2_check_refcounts(bs);
281 585f8587 bellard
#endif
282 6d85a57e Jes Sorensen
    return ret;
283 585f8587 bellard
284 585f8587 bellard
 fail:
285 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
286 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
287 585f8587 bellard
    qemu_free(s->l1_table);
288 29c1a730 Kevin Wolf
    if (s->l2_table_cache) {
289 29c1a730 Kevin Wolf
        qcow2_cache_destroy(bs, s->l2_table_cache);
290 29c1a730 Kevin Wolf
    }
291 585f8587 bellard
    qemu_free(s->cluster_cache);
292 585f8587 bellard
    qemu_free(s->cluster_data);
293 6d85a57e Jes Sorensen
    return ret;
294 585f8587 bellard
}
295 585f8587 bellard
296 7c80ab3f Jes Sorensen
static int qcow2_set_key(BlockDriverState *bs, const char *key)
297 585f8587 bellard
{
298 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
299 585f8587 bellard
    uint8_t keybuf[16];
300 585f8587 bellard
    int len, i;
301 3b46e624 ths
302 585f8587 bellard
    memset(keybuf, 0, 16);
303 585f8587 bellard
    len = strlen(key);
304 585f8587 bellard
    if (len > 16)
305 585f8587 bellard
        len = 16;
306 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
307 585f8587 bellard
       entropy */
308 585f8587 bellard
    for(i = 0;i < len;i++) {
309 585f8587 bellard
        keybuf[i] = key[i];
310 585f8587 bellard
    }
311 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
312 585f8587 bellard
313 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
314 585f8587 bellard
        return -1;
315 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
316 585f8587 bellard
        return -1;
317 585f8587 bellard
#if 0
318 585f8587 bellard
    /* test */
319 585f8587 bellard
    {
320 585f8587 bellard
        uint8_t in[16];
321 585f8587 bellard
        uint8_t out[16];
322 585f8587 bellard
        uint8_t tmp[16];
323 585f8587 bellard
        for(i=0;i<16;i++)
324 585f8587 bellard
            in[i] = i;
325 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
326 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
327 585f8587 bellard
        for(i = 0; i < 16; i++)
328 585f8587 bellard
            printf(" %02x", tmp[i]);
329 585f8587 bellard
        printf("\n");
330 585f8587 bellard
        for(i = 0; i < 16; i++)
331 585f8587 bellard
            printf(" %02x", out[i]);
332 585f8587 bellard
        printf("\n");
333 585f8587 bellard
    }
334 585f8587 bellard
#endif
335 585f8587 bellard
    return 0;
336 585f8587 bellard
}
337 585f8587 bellard
338 7c80ab3f Jes Sorensen
static int qcow2_is_allocated(BlockDriverState *bs, int64_t sector_num,
339 7c80ab3f Jes Sorensen
                              int nb_sectors, int *pnum)
340 585f8587 bellard
{
341 585f8587 bellard
    uint64_t cluster_offset;
342 1c46efaa Kevin Wolf
    int ret;
343 585f8587 bellard
344 095a9c58 aliguori
    *pnum = nb_sectors;
345 1c46efaa Kevin Wolf
    /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
346 1c46efaa Kevin Wolf
     * pass them on today */
347 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
348 1c46efaa Kevin Wolf
    if (ret < 0) {
349 1c46efaa Kevin Wolf
        *pnum = 0;
350 1c46efaa Kevin Wolf
    }
351 095a9c58 aliguori
352 585f8587 bellard
    return (cluster_offset != 0);
353 585f8587 bellard
}
354 585f8587 bellard
355 a9465922 bellard
/* handle reading after the end of the backing file */
356 bd28f835 Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
357 bd28f835 Kevin Wolf
                  int64_t sector_num, int nb_sectors)
358 a9465922 bellard
{
359 a9465922 bellard
    int n1;
360 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
361 a9465922 bellard
        return nb_sectors;
362 a9465922 bellard
    if (sector_num >= bs->total_sectors)
363 a9465922 bellard
        n1 = 0;
364 a9465922 bellard
    else
365 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
366 bd28f835 Kevin Wolf
367 e0d9c6f9 Chunqiang Tang
    qemu_iovec_memset_skip(qiov, 0, 512 * (nb_sectors - n1), 512 * n1);
368 bd28f835 Kevin Wolf
369 a9465922 bellard
    return n1;
370 a9465922 bellard
}
371 a9465922 bellard
372 ce1a14dc pbrook
typedef struct QCowAIOCB {
373 ce1a14dc pbrook
    BlockDriverAIOCB common;
374 585f8587 bellard
    int64_t sector_num;
375 f141eafe aliguori
    QEMUIOVector *qiov;
376 7b88e48b Christoph Hellwig
    int remaining_sectors;
377 7b88e48b Christoph Hellwig
    int cur_nr_sectors;        /* number of sectors in current iteration */
378 bd28f835 Kevin Wolf
    uint64_t bytes_done;
379 585f8587 bellard
    uint64_t cluster_offset;
380 5fafdf24 ths
    uint8_t *cluster_data;
381 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
382 c87c0672 aliguori
    QEMUIOVector hd_qiov;
383 1490791f aliguori
    QEMUBH *bh;
384 e976c6a1 aliguori
    QCowL2Meta l2meta;
385 72cf2d4f Blue Swirl
    QLIST_ENTRY(QCowAIOCB) next_depend;
386 585f8587 bellard
} QCowAIOCB;
387 585f8587 bellard
388 7c80ab3f Jes Sorensen
static void qcow2_aio_cancel(BlockDriverAIOCB *blockacb)
389 c16b5a2c Christoph Hellwig
{
390 b666d239 Kevin Wolf
    QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
391 c16b5a2c Christoph Hellwig
    if (acb->hd_aiocb)
392 c16b5a2c Christoph Hellwig
        bdrv_aio_cancel(acb->hd_aiocb);
393 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
394 c16b5a2c Christoph Hellwig
}
395 c16b5a2c Christoph Hellwig
396 7c80ab3f Jes Sorensen
static AIOPool qcow2_aio_pool = {
397 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(QCowAIOCB),
398 7c80ab3f Jes Sorensen
    .cancel             = qcow2_aio_cancel,
399 c16b5a2c Christoph Hellwig
};
400 c16b5a2c Christoph Hellwig
401 7c80ab3f Jes Sorensen
static void qcow2_aio_read_cb(void *opaque, int ret);
402 7c80ab3f Jes Sorensen
static void qcow2_aio_read_bh(void *opaque)
403 1490791f aliguori
{
404 1490791f aliguori
    QCowAIOCB *acb = opaque;
405 1490791f aliguori
    qemu_bh_delete(acb->bh);
406 1490791f aliguori
    acb->bh = NULL;
407 7c80ab3f Jes Sorensen
    qcow2_aio_read_cb(opaque, 0);
408 1490791f aliguori
}
409 1490791f aliguori
410 7c80ab3f Jes Sorensen
static int qcow2_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
411 a32ef786 aliguori
{
412 a32ef786 aliguori
    if (acb->bh)
413 a32ef786 aliguori
        return -EIO;
414 a32ef786 aliguori
415 a32ef786 aliguori
    acb->bh = qemu_bh_new(cb, acb);
416 a32ef786 aliguori
    if (!acb->bh)
417 a32ef786 aliguori
        return -EIO;
418 a32ef786 aliguori
419 a32ef786 aliguori
    qemu_bh_schedule(acb->bh);
420 a32ef786 aliguori
421 a32ef786 aliguori
    return 0;
422 a32ef786 aliguori
}
423 a32ef786 aliguori
424 7c80ab3f Jes Sorensen
static void qcow2_aio_read_cb(void *opaque, int ret)
425 585f8587 bellard
{
426 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
427 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
428 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
429 a9465922 bellard
    int index_in_cluster, n1;
430 585f8587 bellard
431 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
432 f141eafe aliguori
    if (ret < 0)
433 f141eafe aliguori
        goto done;
434 585f8587 bellard
435 585f8587 bellard
    /* post process the read buffer */
436 ce1a14dc pbrook
    if (!acb->cluster_offset) {
437 585f8587 bellard
        /* nothing to do */
438 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
439 585f8587 bellard
        /* nothing to do */
440 585f8587 bellard
    } else {
441 585f8587 bellard
        if (s->crypt_method) {
442 bd28f835 Kevin Wolf
            qcow2_encrypt_sectors(s, acb->sector_num,  acb->cluster_data,
443 bd28f835 Kevin Wolf
                acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key);
444 bd28f835 Kevin Wolf
            qemu_iovec_reset(&acb->hd_qiov);
445 bd28f835 Kevin Wolf
            qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
446 bd28f835 Kevin Wolf
                acb->cur_nr_sectors * 512);
447 bd28f835 Kevin Wolf
            qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data,
448 bd28f835 Kevin Wolf
                512 * acb->cur_nr_sectors);
449 585f8587 bellard
        }
450 585f8587 bellard
    }
451 585f8587 bellard
452 7b88e48b Christoph Hellwig
    acb->remaining_sectors -= acb->cur_nr_sectors;
453 7b88e48b Christoph Hellwig
    acb->sector_num += acb->cur_nr_sectors;
454 bd28f835 Kevin Wolf
    acb->bytes_done += acb->cur_nr_sectors * 512;
455 585f8587 bellard
456 7b88e48b Christoph Hellwig
    if (acb->remaining_sectors == 0) {
457 585f8587 bellard
        /* request completed */
458 f141eafe aliguori
        ret = 0;
459 f141eafe aliguori
        goto done;
460 585f8587 bellard
    }
461 3b46e624 ths
462 585f8587 bellard
    /* prepare next AIO request */
463 7b88e48b Christoph Hellwig
    acb->cur_nr_sectors = acb->remaining_sectors;
464 bd28f835 Kevin Wolf
    if (s->crypt_method) {
465 bd28f835 Kevin Wolf
        acb->cur_nr_sectors = MIN(acb->cur_nr_sectors,
466 bd28f835 Kevin Wolf
            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
467 bd28f835 Kevin Wolf
    }
468 bd28f835 Kevin Wolf
469 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9,
470 1c46efaa Kevin Wolf
        &acb->cur_nr_sectors, &acb->cluster_offset);
471 1c46efaa Kevin Wolf
    if (ret < 0) {
472 1c46efaa Kevin Wolf
        goto done;
473 1c46efaa Kevin Wolf
    }
474 1c46efaa Kevin Wolf
475 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
476 ce1a14dc pbrook
477 bd28f835 Kevin Wolf
    qemu_iovec_reset(&acb->hd_qiov);
478 bd28f835 Kevin Wolf
    qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
479 bd28f835 Kevin Wolf
        acb->cur_nr_sectors * 512);
480 bd28f835 Kevin Wolf
481 ce1a14dc pbrook
    if (!acb->cluster_offset) {
482 bd28f835 Kevin Wolf
483 585f8587 bellard
        if (bs->backing_hd) {
484 585f8587 bellard
            /* read from the base image */
485 bd28f835 Kevin Wolf
            n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov,
486 bd28f835 Kevin Wolf
                acb->sector_num, acb->cur_nr_sectors);
487 a9465922 bellard
            if (n1 > 0) {
488 66f82cee Kevin Wolf
                BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
489 c87c0672 aliguori
                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
490 e0d9c6f9 Chunqiang Tang
                                    &acb->hd_qiov, n1, qcow2_aio_read_cb, acb);
491 3ab4c7e9 Kevin Wolf
                if (acb->hd_aiocb == NULL) {
492 3ab4c7e9 Kevin Wolf
                    ret = -EIO;
493 f141eafe aliguori
                    goto done;
494 3ab4c7e9 Kevin Wolf
                }
495 a9465922 bellard
            } else {
496 7c80ab3f Jes Sorensen
                ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
497 a32ef786 aliguori
                if (ret < 0)
498 f141eafe aliguori
                    goto done;
499 a9465922 bellard
            }
500 585f8587 bellard
        } else {
501 585f8587 bellard
            /* Note: in this case, no need to wait */
502 bd28f835 Kevin Wolf
            qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors);
503 7c80ab3f Jes Sorensen
            ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
504 a32ef786 aliguori
            if (ret < 0)
505 f141eafe aliguori
                goto done;
506 585f8587 bellard
        }
507 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
508 585f8587 bellard
        /* add AIO support for compressed blocks ? */
509 8af36488 Kevin Wolf
        ret = qcow2_decompress_cluster(bs, acb->cluster_offset);
510 8af36488 Kevin Wolf
        if (ret < 0) {
511 f141eafe aliguori
            goto done;
512 8af36488 Kevin Wolf
        }
513 bd28f835 Kevin Wolf
514 bd28f835 Kevin Wolf
        qemu_iovec_from_buffer(&acb->hd_qiov,
515 bd28f835 Kevin Wolf
            s->cluster_cache + index_in_cluster * 512,
516 bd28f835 Kevin Wolf
            512 * acb->cur_nr_sectors);
517 bd28f835 Kevin Wolf
518 7c80ab3f Jes Sorensen
        ret = qcow2_schedule_bh(qcow2_aio_read_bh, acb);
519 a32ef786 aliguori
        if (ret < 0)
520 f141eafe aliguori
            goto done;
521 585f8587 bellard
    } else {
522 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
523 585f8587 bellard
            ret = -EIO;
524 f141eafe aliguori
            goto done;
525 585f8587 bellard
        }
526 c87c0672 aliguori
527 bd28f835 Kevin Wolf
        if (s->crypt_method) {
528 bd28f835 Kevin Wolf
            /*
529 bd28f835 Kevin Wolf
             * For encrypted images, read everything into a temporary
530 bd28f835 Kevin Wolf
             * contiguous buffer on which the AES functions can work.
531 bd28f835 Kevin Wolf
             */
532 bd28f835 Kevin Wolf
            if (!acb->cluster_data) {
533 bd28f835 Kevin Wolf
                acb->cluster_data =
534 bd28f835 Kevin Wolf
                    qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
535 bd28f835 Kevin Wolf
            }
536 bd28f835 Kevin Wolf
537 bd28f835 Kevin Wolf
            assert(acb->cur_nr_sectors <=
538 bd28f835 Kevin Wolf
                QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
539 bd28f835 Kevin Wolf
            qemu_iovec_reset(&acb->hd_qiov);
540 bd28f835 Kevin Wolf
            qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
541 bd28f835 Kevin Wolf
                512 * acb->cur_nr_sectors);
542 bd28f835 Kevin Wolf
        }
543 bd28f835 Kevin Wolf
544 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
545 66f82cee Kevin Wolf
        acb->hd_aiocb = bdrv_aio_readv(bs->file,
546 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
547 7b88e48b Christoph Hellwig
                            &acb->hd_qiov, acb->cur_nr_sectors,
548 7c80ab3f Jes Sorensen
                            qcow2_aio_read_cb, acb);
549 171e3d6b Kevin Wolf
        if (acb->hd_aiocb == NULL) {
550 171e3d6b Kevin Wolf
            ret = -EIO;
551 f141eafe aliguori
            goto done;
552 171e3d6b Kevin Wolf
        }
553 f141eafe aliguori
    }
554 f141eafe aliguori
555 f141eafe aliguori
    return;
556 f141eafe aliguori
done:
557 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
558 bd28f835 Kevin Wolf
    qemu_iovec_destroy(&acb->hd_qiov);
559 f141eafe aliguori
    qemu_aio_release(acb);
560 585f8587 bellard
}
561 585f8587 bellard
562 7c80ab3f Jes Sorensen
static QCowAIOCB *qcow2_aio_setup(BlockDriverState *bs, int64_t sector_num,
563 7c80ab3f Jes Sorensen
                                  QEMUIOVector *qiov, int nb_sectors,
564 7c80ab3f Jes Sorensen
                                  BlockDriverCompletionFunc *cb,
565 7c80ab3f Jes Sorensen
                                  void *opaque, int is_write)
566 585f8587 bellard
{
567 ce1a14dc pbrook
    QCowAIOCB *acb;
568 ce1a14dc pbrook
569 7c80ab3f Jes Sorensen
    acb = qemu_aio_get(&qcow2_aio_pool, bs, cb, opaque);
570 ce1a14dc pbrook
    if (!acb)
571 ce1a14dc pbrook
        return NULL;
572 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
573 ce1a14dc pbrook
    acb->sector_num = sector_num;
574 f141eafe aliguori
    acb->qiov = qiov;
575 bd28f835 Kevin Wolf
576 6f5f060b Kevin Wolf
    qemu_iovec_init(&acb->hd_qiov, qiov->niov);
577 bd28f835 Kevin Wolf
578 bd28f835 Kevin Wolf
    acb->bytes_done = 0;
579 7b88e48b Christoph Hellwig
    acb->remaining_sectors = nb_sectors;
580 7b88e48b Christoph Hellwig
    acb->cur_nr_sectors = 0;
581 ce1a14dc pbrook
    acb->cluster_offset = 0;
582 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
583 72cf2d4f Blue Swirl
    QLIST_INIT(&acb->l2meta.dependent_requests);
584 ce1a14dc pbrook
    return acb;
585 ce1a14dc pbrook
}
586 ce1a14dc pbrook
587 7c80ab3f Jes Sorensen
static BlockDriverAIOCB *qcow2_aio_readv(BlockDriverState *bs,
588 7c80ab3f Jes Sorensen
                                         int64_t sector_num,
589 7c80ab3f Jes Sorensen
                                         QEMUIOVector *qiov, int nb_sectors,
590 7c80ab3f Jes Sorensen
                                         BlockDriverCompletionFunc *cb,
591 7c80ab3f Jes Sorensen
                                         void *opaque)
592 ce1a14dc pbrook
{
593 ce1a14dc pbrook
    QCowAIOCB *acb;
594 ce1a14dc pbrook
595 7c80ab3f Jes Sorensen
    acb = qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
596 ce1a14dc pbrook
    if (!acb)
597 ce1a14dc pbrook
        return NULL;
598 585f8587 bellard
599 7c80ab3f Jes Sorensen
    qcow2_aio_read_cb(acb, 0);
600 ce1a14dc pbrook
    return &acb->common;
601 585f8587 bellard
}
602 585f8587 bellard
603 7c80ab3f Jes Sorensen
static void qcow2_aio_write_cb(void *opaque, int ret);
604 f214978a Kevin Wolf
605 f214978a Kevin Wolf
static void run_dependent_requests(QCowL2Meta *m)
606 f214978a Kevin Wolf
{
607 f214978a Kevin Wolf
    QCowAIOCB *req;
608 f214978a Kevin Wolf
    QCowAIOCB *next;
609 f214978a Kevin Wolf
610 f214978a Kevin Wolf
    /* Take the request off the list of running requests */
611 f214978a Kevin Wolf
    if (m->nb_clusters != 0) {
612 72cf2d4f Blue Swirl
        QLIST_REMOVE(m, next_in_flight);
613 f214978a Kevin Wolf
    }
614 f214978a Kevin Wolf
615 d4c146f0 Stefan Hajnoczi
    /* Restart all dependent requests */
616 d4c146f0 Stefan Hajnoczi
    QLIST_FOREACH_SAFE(req, &m->dependent_requests, next_depend, next) {
617 7c80ab3f Jes Sorensen
        qcow2_aio_write_cb(req, 0);
618 f214978a Kevin Wolf
    }
619 f214978a Kevin Wolf
620 f214978a Kevin Wolf
    /* Empty the list for the next part of the request */
621 72cf2d4f Blue Swirl
    QLIST_INIT(&m->dependent_requests);
622 f214978a Kevin Wolf
}
623 f214978a Kevin Wolf
624 7c80ab3f Jes Sorensen
static void qcow2_aio_write_cb(void *opaque, int ret)
625 585f8587 bellard
{
626 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
627 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
628 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
629 585f8587 bellard
    int index_in_cluster;
630 095a9c58 aliguori
    int n_end;
631 ce1a14dc pbrook
632 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
633 ce1a14dc pbrook
634 f214978a Kevin Wolf
    if (ret >= 0) {
635 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta);
636 f214978a Kevin Wolf
    }
637 f214978a Kevin Wolf
638 f214978a Kevin Wolf
    run_dependent_requests(&acb->l2meta);
639 f214978a Kevin Wolf
640 f141eafe aliguori
    if (ret < 0)
641 f141eafe aliguori
        goto done;
642 585f8587 bellard
643 7b88e48b Christoph Hellwig
    acb->remaining_sectors -= acb->cur_nr_sectors;
644 7b88e48b Christoph Hellwig
    acb->sector_num += acb->cur_nr_sectors;
645 6f5f060b Kevin Wolf
    acb->bytes_done += acb->cur_nr_sectors * 512;
646 585f8587 bellard
647 7b88e48b Christoph Hellwig
    if (acb->remaining_sectors == 0) {
648 585f8587 bellard
        /* request completed */
649 f141eafe aliguori
        ret = 0;
650 f141eafe aliguori
        goto done;
651 585f8587 bellard
    }
652 3b46e624 ths
653 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
654 7b88e48b Christoph Hellwig
    n_end = index_in_cluster + acb->remaining_sectors;
655 095a9c58 aliguori
    if (s->crypt_method &&
656 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
657 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
658 095a9c58 aliguori
659 148da7ea Kevin Wolf
    ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
660 7b88e48b Christoph Hellwig
        index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta);
661 148da7ea Kevin Wolf
    if (ret < 0) {
662 148da7ea Kevin Wolf
        goto done;
663 148da7ea Kevin Wolf
    }
664 148da7ea Kevin Wolf
665 148da7ea Kevin Wolf
    acb->cluster_offset = acb->l2meta.cluster_offset;
666 f214978a Kevin Wolf
667 f214978a Kevin Wolf
    /* Need to wait for another request? If so, we are done for now. */
668 148da7ea Kevin Wolf
    if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) {
669 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests,
670 f214978a Kevin Wolf
            acb, next_depend);
671 f214978a Kevin Wolf
        return;
672 f214978a Kevin Wolf
    }
673 f214978a Kevin Wolf
674 148da7ea Kevin Wolf
    assert((acb->cluster_offset & 511) == 0);
675 148da7ea Kevin Wolf
676 6f5f060b Kevin Wolf
    qemu_iovec_reset(&acb->hd_qiov);
677 6f5f060b Kevin Wolf
    qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
678 6f5f060b Kevin Wolf
        acb->cur_nr_sectors * 512);
679 6f5f060b Kevin Wolf
680 585f8587 bellard
    if (s->crypt_method) {
681 ce1a14dc pbrook
        if (!acb->cluster_data) {
682 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
683 095a9c58 aliguori
                                             s->cluster_size);
684 585f8587 bellard
        }
685 6f5f060b Kevin Wolf
686 6f5f060b Kevin Wolf
        assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
687 6f5f060b Kevin Wolf
        qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data);
688 6f5f060b Kevin Wolf
689 6f5f060b Kevin Wolf
        qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
690 6f5f060b Kevin Wolf
            acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key);
691 6f5f060b Kevin Wolf
692 6f5f060b Kevin Wolf
        qemu_iovec_reset(&acb->hd_qiov);
693 6f5f060b Kevin Wolf
        qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
694 6f5f060b Kevin Wolf
            acb->cur_nr_sectors * 512);
695 585f8587 bellard
    }
696 6f5f060b Kevin Wolf
697 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
698 66f82cee Kevin Wolf
    acb->hd_aiocb = bdrv_aio_writev(bs->file,
699 c87c0672 aliguori
                                    (acb->cluster_offset >> 9) + index_in_cluster,
700 7b88e48b Christoph Hellwig
                                    &acb->hd_qiov, acb->cur_nr_sectors,
701 7c80ab3f Jes Sorensen
                                    qcow2_aio_write_cb, acb);
702 171e3d6b Kevin Wolf
    if (acb->hd_aiocb == NULL) {
703 171e3d6b Kevin Wolf
        ret = -EIO;
704 c644db3d Kevin Wolf
        goto fail;
705 171e3d6b Kevin Wolf
    }
706 f141eafe aliguori
707 f141eafe aliguori
    return;
708 f141eafe aliguori
709 c644db3d Kevin Wolf
fail:
710 c644db3d Kevin Wolf
    if (acb->l2meta.nb_clusters != 0) {
711 c644db3d Kevin Wolf
        QLIST_REMOVE(&acb->l2meta, next_in_flight);
712 c644db3d Kevin Wolf
    }
713 f141eafe aliguori
done:
714 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
715 6f5f060b Kevin Wolf
    qemu_iovec_destroy(&acb->hd_qiov);
716 f141eafe aliguori
    qemu_aio_release(acb);
717 585f8587 bellard
}
718 585f8587 bellard
719 7c80ab3f Jes Sorensen
static BlockDriverAIOCB *qcow2_aio_writev(BlockDriverState *bs,
720 7c80ab3f Jes Sorensen
                                          int64_t sector_num,
721 7c80ab3f Jes Sorensen
                                          QEMUIOVector *qiov, int nb_sectors,
722 7c80ab3f Jes Sorensen
                                          BlockDriverCompletionFunc *cb,
723 7c80ab3f Jes Sorensen
                                          void *opaque)
724 585f8587 bellard
{
725 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
726 ce1a14dc pbrook
    QCowAIOCB *acb;
727 3b46e624 ths
728 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
729 585f8587 bellard
730 7c80ab3f Jes Sorensen
    acb = qcow2_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
731 ce1a14dc pbrook
    if (!acb)
732 ce1a14dc pbrook
        return NULL;
733 3b46e624 ths
734 7c80ab3f Jes Sorensen
    qcow2_aio_write_cb(acb, 0);
735 ce1a14dc pbrook
    return &acb->common;
736 585f8587 bellard
}
737 585f8587 bellard
738 7c80ab3f Jes Sorensen
static void qcow2_close(BlockDriverState *bs)
739 585f8587 bellard
{
740 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
741 585f8587 bellard
    qemu_free(s->l1_table);
742 29c1a730 Kevin Wolf
743 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->l2_table_cache);
744 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->refcount_block_cache);
745 29c1a730 Kevin Wolf
746 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->l2_table_cache);
747 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->refcount_block_cache);
748 29c1a730 Kevin Wolf
749 585f8587 bellard
    qemu_free(s->cluster_cache);
750 585f8587 bellard
    qemu_free(s->cluster_data);
751 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
752 585f8587 bellard
}
753 585f8587 bellard
754 756e6736 Kevin Wolf
/*
755 756e6736 Kevin Wolf
 * Updates the variable length parts of the qcow2 header, i.e. the backing file
756 756e6736 Kevin Wolf
 * name and all extensions. qcow2 was not designed to allow such changes, so if
757 756e6736 Kevin Wolf
 * we run out of space (we can only use the first cluster) this function may
758 756e6736 Kevin Wolf
 * fail.
759 756e6736 Kevin Wolf
 *
760 756e6736 Kevin Wolf
 * Returns 0 on success, -errno in error cases.
761 756e6736 Kevin Wolf
 */
762 756e6736 Kevin Wolf
static int qcow2_update_ext_header(BlockDriverState *bs,
763 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
764 756e6736 Kevin Wolf
{
765 756e6736 Kevin Wolf
    size_t backing_file_len = 0;
766 756e6736 Kevin Wolf
    size_t backing_fmt_len = 0;
767 756e6736 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
768 756e6736 Kevin Wolf
    QCowExtension ext_backing_fmt = {0, 0};
769 756e6736 Kevin Wolf
    int ret;
770 756e6736 Kevin Wolf
771 756e6736 Kevin Wolf
    /* Backing file format doesn't make sense without a backing file */
772 756e6736 Kevin Wolf
    if (backing_fmt && !backing_file) {
773 756e6736 Kevin Wolf
        return -EINVAL;
774 756e6736 Kevin Wolf
    }
775 756e6736 Kevin Wolf
776 756e6736 Kevin Wolf
    /* Prepare the backing file format extension if needed */
777 756e6736 Kevin Wolf
    if (backing_fmt) {
778 756e6736 Kevin Wolf
        ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt));
779 7c80ab3f Jes Sorensen
        ext_backing_fmt.magic = cpu_to_be32(QCOW2_EXT_MAGIC_BACKING_FORMAT);
780 756e6736 Kevin Wolf
        backing_fmt_len = ((sizeof(ext_backing_fmt)
781 756e6736 Kevin Wolf
            + strlen(backing_fmt) + 7) & ~7);
782 756e6736 Kevin Wolf
    }
783 756e6736 Kevin Wolf
784 756e6736 Kevin Wolf
    /* Check if we can fit the new header into the first cluster */
785 756e6736 Kevin Wolf
    if (backing_file) {
786 756e6736 Kevin Wolf
        backing_file_len = strlen(backing_file);
787 756e6736 Kevin Wolf
    }
788 756e6736 Kevin Wolf
789 756e6736 Kevin Wolf
    size_t header_size = sizeof(QCowHeader) + backing_file_len
790 756e6736 Kevin Wolf
        + backing_fmt_len;
791 756e6736 Kevin Wolf
792 756e6736 Kevin Wolf
    if (header_size > s->cluster_size) {
793 756e6736 Kevin Wolf
        return -ENOSPC;
794 756e6736 Kevin Wolf
    }
795 756e6736 Kevin Wolf
796 756e6736 Kevin Wolf
    /* Rewrite backing file name and qcow2 extensions */
797 756e6736 Kevin Wolf
    size_t ext_size = header_size - sizeof(QCowHeader);
798 756e6736 Kevin Wolf
    uint8_t buf[ext_size];
799 756e6736 Kevin Wolf
    size_t offset = 0;
800 756e6736 Kevin Wolf
    size_t backing_file_offset = 0;
801 756e6736 Kevin Wolf
802 756e6736 Kevin Wolf
    if (backing_file) {
803 756e6736 Kevin Wolf
        if (backing_fmt) {
804 756e6736 Kevin Wolf
            int padding = backing_fmt_len -
805 756e6736 Kevin Wolf
                (sizeof(ext_backing_fmt) + strlen(backing_fmt));
806 756e6736 Kevin Wolf
807 756e6736 Kevin Wolf
            memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
808 756e6736 Kevin Wolf
            offset += sizeof(ext_backing_fmt);
809 756e6736 Kevin Wolf
810 756e6736 Kevin Wolf
            memcpy(buf + offset, backing_fmt, strlen(backing_fmt));
811 756e6736 Kevin Wolf
            offset += strlen(backing_fmt);
812 756e6736 Kevin Wolf
813 756e6736 Kevin Wolf
            memset(buf + offset, 0, padding);
814 756e6736 Kevin Wolf
            offset += padding;
815 756e6736 Kevin Wolf
        }
816 756e6736 Kevin Wolf
817 756e6736 Kevin Wolf
        memcpy(buf + offset, backing_file, backing_file_len);
818 756e6736 Kevin Wolf
        backing_file_offset = sizeof(QCowHeader) + offset;
819 756e6736 Kevin Wolf
    }
820 756e6736 Kevin Wolf
821 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size);
822 756e6736 Kevin Wolf
    if (ret < 0) {
823 756e6736 Kevin Wolf
        goto fail;
824 756e6736 Kevin Wolf
    }
825 756e6736 Kevin Wolf
826 756e6736 Kevin Wolf
    /* Update header fields */
827 756e6736 Kevin Wolf
    uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset);
828 756e6736 Kevin Wolf
    uint32_t be_backing_file_size = cpu_to_be32(backing_file_len);
829 756e6736 Kevin Wolf
830 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset),
831 756e6736 Kevin Wolf
        &be_backing_file_offset, sizeof(uint64_t));
832 756e6736 Kevin Wolf
    if (ret < 0) {
833 756e6736 Kevin Wolf
        goto fail;
834 756e6736 Kevin Wolf
    }
835 756e6736 Kevin Wolf
836 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size),
837 756e6736 Kevin Wolf
        &be_backing_file_size, sizeof(uint32_t));
838 756e6736 Kevin Wolf
    if (ret < 0) {
839 756e6736 Kevin Wolf
        goto fail;
840 756e6736 Kevin Wolf
    }
841 756e6736 Kevin Wolf
842 756e6736 Kevin Wolf
    ret = 0;
843 756e6736 Kevin Wolf
fail:
844 756e6736 Kevin Wolf
    return ret;
845 756e6736 Kevin Wolf
}
846 756e6736 Kevin Wolf
847 756e6736 Kevin Wolf
static int qcow2_change_backing_file(BlockDriverState *bs,
848 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
849 756e6736 Kevin Wolf
{
850 756e6736 Kevin Wolf
    return qcow2_update_ext_header(bs, backing_file, backing_fmt);
851 756e6736 Kevin Wolf
}
852 756e6736 Kevin Wolf
853 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
854 a35e1c17 Kevin Wolf
{
855 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
856 a35e1c17 Kevin Wolf
    uint64_t offset;
857 a35e1c17 Kevin Wolf
    int num;
858 148da7ea Kevin Wolf
    int ret;
859 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
860 a35e1c17 Kevin Wolf
861 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
862 a35e1c17 Kevin Wolf
    offset = 0;
863 72cf2d4f Blue Swirl
    QLIST_INIT(&meta.dependent_requests);
864 148da7ea Kevin Wolf
    meta.cluster_offset = 0;
865 a35e1c17 Kevin Wolf
866 a35e1c17 Kevin Wolf
    while (nb_sectors) {
867 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
868 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
869 148da7ea Kevin Wolf
        if (ret < 0) {
870 19dbcbf7 Kevin Wolf
            return ret;
871 a35e1c17 Kevin Wolf
        }
872 a35e1c17 Kevin Wolf
873 19dbcbf7 Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &meta);
874 19dbcbf7 Kevin Wolf
        if (ret < 0) {
875 148da7ea Kevin Wolf
            qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
876 19dbcbf7 Kevin Wolf
            return ret;
877 a35e1c17 Kevin Wolf
        }
878 a35e1c17 Kevin Wolf
879 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
880 f214978a Kevin Wolf
         * from the list of in-flight requests */
881 f214978a Kevin Wolf
        run_dependent_requests(&meta);
882 f214978a Kevin Wolf
883 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
884 a35e1c17 Kevin Wolf
885 a35e1c17 Kevin Wolf
        nb_sectors -= num;
886 a35e1c17 Kevin Wolf
        offset += num << 9;
887 a35e1c17 Kevin Wolf
    }
888 a35e1c17 Kevin Wolf
889 a35e1c17 Kevin Wolf
    /*
890 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
891 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
892 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
893 a35e1c17 Kevin Wolf
     */
894 148da7ea Kevin Wolf
    if (meta.cluster_offset != 0) {
895 ea80b906 Kevin Wolf
        uint8_t buf[512];
896 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
897 19dbcbf7 Kevin Wolf
        ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
898 19dbcbf7 Kevin Wolf
        if (ret < 0) {
899 19dbcbf7 Kevin Wolf
            return ret;
900 19dbcbf7 Kevin Wolf
        }
901 a35e1c17 Kevin Wolf
    }
902 a35e1c17 Kevin Wolf
903 a35e1c17 Kevin Wolf
    return 0;
904 a35e1c17 Kevin Wolf
}
905 a35e1c17 Kevin Wolf
906 7c80ab3f Jes Sorensen
static int qcow2_create2(const char *filename, int64_t total_size,
907 7c80ab3f Jes Sorensen
                         const char *backing_file, const char *backing_format,
908 7c80ab3f Jes Sorensen
                         int flags, size_t cluster_size, int prealloc,
909 7c80ab3f Jes Sorensen
                         QEMUOptionParameter *options)
910 a9420734 Kevin Wolf
{
911 a9420734 Kevin Wolf
    /* Calulate cluster_bits */
912 a9420734 Kevin Wolf
    int cluster_bits;
913 a9420734 Kevin Wolf
    cluster_bits = ffs(cluster_size) - 1;
914 a9420734 Kevin Wolf
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
915 a9420734 Kevin Wolf
        (1 << cluster_bits) != cluster_size)
916 a9420734 Kevin Wolf
    {
917 a9420734 Kevin Wolf
        error_report(
918 a9420734 Kevin Wolf
            "Cluster size must be a power of two between %d and %dk\n",
919 a9420734 Kevin Wolf
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
920 a9420734 Kevin Wolf
        return -EINVAL;
921 a9420734 Kevin Wolf
    }
922 a9420734 Kevin Wolf
923 a9420734 Kevin Wolf
    /*
924 a9420734 Kevin Wolf
     * Open the image file and write a minimal qcow2 header.
925 a9420734 Kevin Wolf
     *
926 a9420734 Kevin Wolf
     * We keep things simple and start with a zero-sized image. We also
927 a9420734 Kevin Wolf
     * do without refcount blocks or a L1 table for now. We'll fix the
928 a9420734 Kevin Wolf
     * inconsistency later.
929 a9420734 Kevin Wolf
     *
930 a9420734 Kevin Wolf
     * We do need a refcount table because growing the refcount table means
931 a9420734 Kevin Wolf
     * allocating two new refcount blocks - the seconds of which would be at
932 a9420734 Kevin Wolf
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
933 a9420734 Kevin Wolf
     * size for any qcow2 image.
934 a9420734 Kevin Wolf
     */
935 a9420734 Kevin Wolf
    BlockDriverState* bs;
936 a9420734 Kevin Wolf
    QCowHeader header;
937 a9420734 Kevin Wolf
    uint8_t* refcount_table;
938 a9420734 Kevin Wolf
    int ret;
939 a9420734 Kevin Wolf
940 a9420734 Kevin Wolf
    ret = bdrv_create_file(filename, options);
941 a9420734 Kevin Wolf
    if (ret < 0) {
942 a9420734 Kevin Wolf
        return ret;
943 a9420734 Kevin Wolf
    }
944 a9420734 Kevin Wolf
945 a9420734 Kevin Wolf
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
946 a9420734 Kevin Wolf
    if (ret < 0) {
947 a9420734 Kevin Wolf
        return ret;
948 a9420734 Kevin Wolf
    }
949 a9420734 Kevin Wolf
950 a9420734 Kevin Wolf
    /* Write the header */
951 a9420734 Kevin Wolf
    memset(&header, 0, sizeof(header));
952 a9420734 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
953 a9420734 Kevin Wolf
    header.version = cpu_to_be32(QCOW_VERSION);
954 a9420734 Kevin Wolf
    header.cluster_bits = cpu_to_be32(cluster_bits);
955 a9420734 Kevin Wolf
    header.size = cpu_to_be64(0);
956 a9420734 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(0);
957 a9420734 Kevin Wolf
    header.l1_size = cpu_to_be32(0);
958 a9420734 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(cluster_size);
959 a9420734 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(1);
960 a9420734 Kevin Wolf
961 a9420734 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
962 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
963 a9420734 Kevin Wolf
    } else {
964 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
965 a9420734 Kevin Wolf
    }
966 a9420734 Kevin Wolf
967 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
968 a9420734 Kevin Wolf
    if (ret < 0) {
969 a9420734 Kevin Wolf
        goto out;
970 a9420734 Kevin Wolf
    }
971 a9420734 Kevin Wolf
972 a9420734 Kevin Wolf
    /* Write an empty refcount table */
973 a9420734 Kevin Wolf
    refcount_table = qemu_mallocz(cluster_size);
974 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
975 a9420734 Kevin Wolf
    qemu_free(refcount_table);
976 a9420734 Kevin Wolf
977 a9420734 Kevin Wolf
    if (ret < 0) {
978 a9420734 Kevin Wolf
        goto out;
979 a9420734 Kevin Wolf
    }
980 a9420734 Kevin Wolf
981 a9420734 Kevin Wolf
    bdrv_close(bs);
982 a9420734 Kevin Wolf
983 a9420734 Kevin Wolf
    /*
984 a9420734 Kevin Wolf
     * And now open the image and make it consistent first (i.e. increase the
985 a9420734 Kevin Wolf
     * refcount of the cluster that is occupied by the header and the refcount
986 a9420734 Kevin Wolf
     * table)
987 a9420734 Kevin Wolf
     */
988 a9420734 Kevin Wolf
    BlockDriver* drv = bdrv_find_format("qcow2");
989 a9420734 Kevin Wolf
    assert(drv != NULL);
990 e1a7107f Kevin Wolf
    ret = bdrv_open(bs, filename,
991 e1a7107f Kevin Wolf
        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
992 a9420734 Kevin Wolf
    if (ret < 0) {
993 a9420734 Kevin Wolf
        goto out;
994 a9420734 Kevin Wolf
    }
995 a9420734 Kevin Wolf
996 a9420734 Kevin Wolf
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
997 a9420734 Kevin Wolf
    if (ret < 0) {
998 a9420734 Kevin Wolf
        goto out;
999 a9420734 Kevin Wolf
1000 a9420734 Kevin Wolf
    } else if (ret != 0) {
1001 a9420734 Kevin Wolf
        error_report("Huh, first cluster in empty image is already in use?");
1002 a9420734 Kevin Wolf
        abort();
1003 a9420734 Kevin Wolf
    }
1004 a9420734 Kevin Wolf
1005 a9420734 Kevin Wolf
    /* Okay, now that we have a valid image, let's give it the right size */
1006 a9420734 Kevin Wolf
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1007 a9420734 Kevin Wolf
    if (ret < 0) {
1008 a9420734 Kevin Wolf
        goto out;
1009 a9420734 Kevin Wolf
    }
1010 a9420734 Kevin Wolf
1011 a9420734 Kevin Wolf
    /* Want a backing file? There you go.*/
1012 a9420734 Kevin Wolf
    if (backing_file) {
1013 a9420734 Kevin Wolf
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1014 a9420734 Kevin Wolf
        if (ret < 0) {
1015 a9420734 Kevin Wolf
            goto out;
1016 a9420734 Kevin Wolf
        }
1017 a9420734 Kevin Wolf
    }
1018 a9420734 Kevin Wolf
1019 a9420734 Kevin Wolf
    /* And if we're supposed to preallocate metadata, do that now */
1020 a9420734 Kevin Wolf
    if (prealloc) {
1021 a9420734 Kevin Wolf
        ret = preallocate(bs);
1022 a9420734 Kevin Wolf
        if (ret < 0) {
1023 a9420734 Kevin Wolf
            goto out;
1024 a9420734 Kevin Wolf
        }
1025 a9420734 Kevin Wolf
    }
1026 a9420734 Kevin Wolf
1027 a9420734 Kevin Wolf
    ret = 0;
1028 a9420734 Kevin Wolf
out:
1029 a9420734 Kevin Wolf
    bdrv_delete(bs);
1030 a9420734 Kevin Wolf
    return ret;
1031 a9420734 Kevin Wolf
}
1032 de5f3f40 Kevin Wolf
1033 7c80ab3f Jes Sorensen
static int qcow2_create(const char *filename, QEMUOptionParameter *options)
1034 de5f3f40 Kevin Wolf
{
1035 de5f3f40 Kevin Wolf
    const char *backing_file = NULL;
1036 de5f3f40 Kevin Wolf
    const char *backing_fmt = NULL;
1037 de5f3f40 Kevin Wolf
    uint64_t sectors = 0;
1038 de5f3f40 Kevin Wolf
    int flags = 0;
1039 99cce9fa Kevin Wolf
    size_t cluster_size = DEFAULT_CLUSTER_SIZE;
1040 de5f3f40 Kevin Wolf
    int prealloc = 0;
1041 de5f3f40 Kevin Wolf
1042 de5f3f40 Kevin Wolf
    /* Read out options */
1043 de5f3f40 Kevin Wolf
    while (options && options->name) {
1044 de5f3f40 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1045 de5f3f40 Kevin Wolf
            sectors = options->value.n / 512;
1046 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1047 de5f3f40 Kevin Wolf
            backing_file = options->value.s;
1048 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1049 de5f3f40 Kevin Wolf
            backing_fmt = options->value.s;
1050 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1051 de5f3f40 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1052 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1053 de5f3f40 Kevin Wolf
            if (options->value.n) {
1054 de5f3f40 Kevin Wolf
                cluster_size = options->value.n;
1055 de5f3f40 Kevin Wolf
            }
1056 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1057 de5f3f40 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1058 de5f3f40 Kevin Wolf
                prealloc = 0;
1059 de5f3f40 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
1060 de5f3f40 Kevin Wolf
                prealloc = 1;
1061 de5f3f40 Kevin Wolf
            } else {
1062 de5f3f40 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1063 de5f3f40 Kevin Wolf
                    options->value.s);
1064 de5f3f40 Kevin Wolf
                return -EINVAL;
1065 de5f3f40 Kevin Wolf
            }
1066 de5f3f40 Kevin Wolf
        }
1067 de5f3f40 Kevin Wolf
        options++;
1068 de5f3f40 Kevin Wolf
    }
1069 de5f3f40 Kevin Wolf
1070 de5f3f40 Kevin Wolf
    if (backing_file && prealloc) {
1071 de5f3f40 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1072 de5f3f40 Kevin Wolf
            "the same time\n");
1073 de5f3f40 Kevin Wolf
        return -EINVAL;
1074 de5f3f40 Kevin Wolf
    }
1075 de5f3f40 Kevin Wolf
1076 7c80ab3f Jes Sorensen
    return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1077 7c80ab3f Jes Sorensen
                         cluster_size, prealloc, options);
1078 de5f3f40 Kevin Wolf
}
1079 de5f3f40 Kevin Wolf
1080 7c80ab3f Jes Sorensen
static int qcow2_make_empty(BlockDriverState *bs)
1081 20d97356 Blue Swirl
{
1082 20d97356 Blue Swirl
#if 0
1083 20d97356 Blue Swirl
    /* XXX: not correct */
1084 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1085 20d97356 Blue Swirl
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1086 20d97356 Blue Swirl
    int ret;
1087 20d97356 Blue Swirl

1088 20d97356 Blue Swirl
    memset(s->l1_table, 0, l1_length);
1089 66f82cee Kevin Wolf
    if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1090 20d97356 Blue Swirl
        return -1;
1091 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1092 20d97356 Blue Swirl
    if (ret < 0)
1093 20d97356 Blue Swirl
        return ret;
1094 20d97356 Blue Swirl

1095 20d97356 Blue Swirl
    l2_cache_reset(bs);
1096 20d97356 Blue Swirl
#endif
1097 20d97356 Blue Swirl
    return 0;
1098 20d97356 Blue Swirl
}
1099 20d97356 Blue Swirl
1100 5ea929e3 Kevin Wolf
static int qcow2_discard(BlockDriverState *bs, int64_t sector_num,
1101 5ea929e3 Kevin Wolf
    int nb_sectors)
1102 5ea929e3 Kevin Wolf
{
1103 5ea929e3 Kevin Wolf
    return qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1104 5ea929e3 Kevin Wolf
        nb_sectors);
1105 5ea929e3 Kevin Wolf
}
1106 5ea929e3 Kevin Wolf
1107 419b19d9 Stefan Hajnoczi
static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1108 419b19d9 Stefan Hajnoczi
{
1109 419b19d9 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
1110 419b19d9 Stefan Hajnoczi
    int ret, new_l1_size;
1111 419b19d9 Stefan Hajnoczi
1112 419b19d9 Stefan Hajnoczi
    if (offset & 511) {
1113 419b19d9 Stefan Hajnoczi
        return -EINVAL;
1114 419b19d9 Stefan Hajnoczi
    }
1115 419b19d9 Stefan Hajnoczi
1116 419b19d9 Stefan Hajnoczi
    /* cannot proceed if image has snapshots */
1117 419b19d9 Stefan Hajnoczi
    if (s->nb_snapshots) {
1118 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1119 419b19d9 Stefan Hajnoczi
    }
1120 419b19d9 Stefan Hajnoczi
1121 419b19d9 Stefan Hajnoczi
    /* shrinking is currently not supported */
1122 419b19d9 Stefan Hajnoczi
    if (offset < bs->total_sectors * 512) {
1123 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1124 419b19d9 Stefan Hajnoczi
    }
1125 419b19d9 Stefan Hajnoczi
1126 419b19d9 Stefan Hajnoczi
    new_l1_size = size_to_l1(s, offset);
1127 72893756 Stefan Hajnoczi
    ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1128 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1129 419b19d9 Stefan Hajnoczi
        return ret;
1130 419b19d9 Stefan Hajnoczi
    }
1131 419b19d9 Stefan Hajnoczi
1132 419b19d9 Stefan Hajnoczi
    /* write updated header.size */
1133 419b19d9 Stefan Hajnoczi
    offset = cpu_to_be64(offset);
1134 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1135 8b3b7206 Kevin Wolf
                           &offset, sizeof(uint64_t));
1136 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1137 419b19d9 Stefan Hajnoczi
        return ret;
1138 419b19d9 Stefan Hajnoczi
    }
1139 419b19d9 Stefan Hajnoczi
1140 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = new_l1_size;
1141 419b19d9 Stefan Hajnoczi
    return 0;
1142 419b19d9 Stefan Hajnoczi
}
1143 419b19d9 Stefan Hajnoczi
1144 20d97356 Blue Swirl
/* XXX: put compressed sectors first, then all the cluster aligned
1145 20d97356 Blue Swirl
   tables to avoid losing bytes in alignment */
1146 7c80ab3f Jes Sorensen
static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1147 7c80ab3f Jes Sorensen
                                  const uint8_t *buf, int nb_sectors)
1148 20d97356 Blue Swirl
{
1149 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1150 20d97356 Blue Swirl
    z_stream strm;
1151 20d97356 Blue Swirl
    int ret, out_len;
1152 20d97356 Blue Swirl
    uint8_t *out_buf;
1153 20d97356 Blue Swirl
    uint64_t cluster_offset;
1154 20d97356 Blue Swirl
1155 20d97356 Blue Swirl
    if (nb_sectors == 0) {
1156 20d97356 Blue Swirl
        /* align end of file to a sector boundary to ease reading with
1157 20d97356 Blue Swirl
           sector based I/Os */
1158 66f82cee Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
1159 20d97356 Blue Swirl
        cluster_offset = (cluster_offset + 511) & ~511;
1160 66f82cee Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset);
1161 20d97356 Blue Swirl
        return 0;
1162 20d97356 Blue Swirl
    }
1163 20d97356 Blue Swirl
1164 20d97356 Blue Swirl
    if (nb_sectors != s->cluster_sectors)
1165 20d97356 Blue Swirl
        return -EINVAL;
1166 20d97356 Blue Swirl
1167 20d97356 Blue Swirl
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1168 20d97356 Blue Swirl
1169 20d97356 Blue Swirl
    /* best compression, small window, no zlib header */
1170 20d97356 Blue Swirl
    memset(&strm, 0, sizeof(strm));
1171 20d97356 Blue Swirl
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1172 20d97356 Blue Swirl
                       Z_DEFLATED, -12,
1173 20d97356 Blue Swirl
                       9, Z_DEFAULT_STRATEGY);
1174 20d97356 Blue Swirl
    if (ret != 0) {
1175 20d97356 Blue Swirl
        qemu_free(out_buf);
1176 20d97356 Blue Swirl
        return -1;
1177 20d97356 Blue Swirl
    }
1178 20d97356 Blue Swirl
1179 20d97356 Blue Swirl
    strm.avail_in = s->cluster_size;
1180 20d97356 Blue Swirl
    strm.next_in = (uint8_t *)buf;
1181 20d97356 Blue Swirl
    strm.avail_out = s->cluster_size;
1182 20d97356 Blue Swirl
    strm.next_out = out_buf;
1183 20d97356 Blue Swirl
1184 20d97356 Blue Swirl
    ret = deflate(&strm, Z_FINISH);
1185 20d97356 Blue Swirl
    if (ret != Z_STREAM_END && ret != Z_OK) {
1186 20d97356 Blue Swirl
        qemu_free(out_buf);
1187 20d97356 Blue Swirl
        deflateEnd(&strm);
1188 20d97356 Blue Swirl
        return -1;
1189 20d97356 Blue Swirl
    }
1190 20d97356 Blue Swirl
    out_len = strm.next_out - out_buf;
1191 20d97356 Blue Swirl
1192 20d97356 Blue Swirl
    deflateEnd(&strm);
1193 20d97356 Blue Swirl
1194 20d97356 Blue Swirl
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1195 20d97356 Blue Swirl
        /* could not compress: write normal cluster */
1196 20d97356 Blue Swirl
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1197 20d97356 Blue Swirl
    } else {
1198 20d97356 Blue Swirl
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1199 20d97356 Blue Swirl
            sector_num << 9, out_len);
1200 20d97356 Blue Swirl
        if (!cluster_offset)
1201 20d97356 Blue Swirl
            return -1;
1202 20d97356 Blue Swirl
        cluster_offset &= s->cluster_offset_mask;
1203 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1204 66f82cee Kevin Wolf
        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
1205 20d97356 Blue Swirl
            qemu_free(out_buf);
1206 20d97356 Blue Swirl
            return -1;
1207 20d97356 Blue Swirl
        }
1208 20d97356 Blue Swirl
    }
1209 20d97356 Blue Swirl
1210 20d97356 Blue Swirl
    qemu_free(out_buf);
1211 20d97356 Blue Swirl
    return 0;
1212 20d97356 Blue Swirl
}
1213 20d97356 Blue Swirl
1214 7c80ab3f Jes Sorensen
static int qcow2_flush(BlockDriverState *bs)
1215 20d97356 Blue Swirl
{
1216 29c1a730 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1217 29c1a730 Kevin Wolf
    int ret;
1218 29c1a730 Kevin Wolf
1219 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
1220 29c1a730 Kevin Wolf
    if (ret < 0) {
1221 29c1a730 Kevin Wolf
        return ret;
1222 29c1a730 Kevin Wolf
    }
1223 29c1a730 Kevin Wolf
1224 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1225 29c1a730 Kevin Wolf
    if (ret < 0) {
1226 29c1a730 Kevin Wolf
        return ret;
1227 29c1a730 Kevin Wolf
    }
1228 29c1a730 Kevin Wolf
1229 205ef796 Kevin Wolf
    return bdrv_flush(bs->file);
1230 20d97356 Blue Swirl
}
1231 20d97356 Blue Swirl
1232 7c80ab3f Jes Sorensen
static BlockDriverAIOCB *qcow2_aio_flush(BlockDriverState *bs,
1233 7c80ab3f Jes Sorensen
                                         BlockDriverCompletionFunc *cb,
1234 7c80ab3f Jes Sorensen
                                         void *opaque)
1235 20d97356 Blue Swirl
{
1236 29c1a730 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1237 29c1a730 Kevin Wolf
    int ret;
1238 29c1a730 Kevin Wolf
1239 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
1240 29c1a730 Kevin Wolf
    if (ret < 0) {
1241 29c1a730 Kevin Wolf
        return NULL;
1242 29c1a730 Kevin Wolf
    }
1243 29c1a730 Kevin Wolf
1244 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1245 29c1a730 Kevin Wolf
    if (ret < 0) {
1246 29c1a730 Kevin Wolf
        return NULL;
1247 29c1a730 Kevin Wolf
    }
1248 29c1a730 Kevin Wolf
1249 66f82cee Kevin Wolf
    return bdrv_aio_flush(bs->file, cb, opaque);
1250 20d97356 Blue Swirl
}
1251 20d97356 Blue Swirl
1252 7c80ab3f Jes Sorensen
static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
1253 20d97356 Blue Swirl
{
1254 20d97356 Blue Swirl
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1255 20d97356 Blue Swirl
}
1256 20d97356 Blue Swirl
1257 7c80ab3f Jes Sorensen
static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1258 20d97356 Blue Swirl
{
1259 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1260 20d97356 Blue Swirl
    bdi->cluster_size = s->cluster_size;
1261 7c80ab3f Jes Sorensen
    bdi->vm_state_offset = qcow2_vm_state_offset(s);
1262 20d97356 Blue Swirl
    return 0;
1263 20d97356 Blue Swirl
}
1264 20d97356 Blue Swirl
1265 20d97356 Blue Swirl
1266 7c80ab3f Jes Sorensen
static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result)
1267 20d97356 Blue Swirl
{
1268 9ac228e0 Kevin Wolf
    return qcow2_check_refcounts(bs, result);
1269 20d97356 Blue Swirl
}
1270 20d97356 Blue Swirl
1271 20d97356 Blue Swirl
#if 0
1272 20d97356 Blue Swirl
static void dump_refcounts(BlockDriverState *bs)
1273 20d97356 Blue Swirl
{
1274 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1275 20d97356 Blue Swirl
    int64_t nb_clusters, k, k1, size;
1276 20d97356 Blue Swirl
    int refcount;
1277 20d97356 Blue Swirl

1278 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1279 20d97356 Blue Swirl
    nb_clusters = size_to_clusters(s, size);
1280 20d97356 Blue Swirl
    for(k = 0; k < nb_clusters;) {
1281 20d97356 Blue Swirl
        k1 = k;
1282 20d97356 Blue Swirl
        refcount = get_refcount(bs, k);
1283 20d97356 Blue Swirl
        k++;
1284 20d97356 Blue Swirl
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1285 20d97356 Blue Swirl
            k++;
1286 0bfcd599 Blue Swirl
        printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1287 0bfcd599 Blue Swirl
               k - k1);
1288 20d97356 Blue Swirl
    }
1289 20d97356 Blue Swirl
}
1290 20d97356 Blue Swirl
#endif
1291 20d97356 Blue Swirl
1292 7c80ab3f Jes Sorensen
static int qcow2_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1293 7c80ab3f Jes Sorensen
                              int64_t pos, int size)
1294 20d97356 Blue Swirl
{
1295 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1296 20d97356 Blue Swirl
    int growable = bs->growable;
1297 20d97356 Blue Swirl
    int ret;
1298 20d97356 Blue Swirl
1299 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1300 20d97356 Blue Swirl
    bs->growable = 1;
1301 7c80ab3f Jes Sorensen
    ret = bdrv_pwrite(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1302 20d97356 Blue Swirl
    bs->growable = growable;
1303 20d97356 Blue Swirl
1304 20d97356 Blue Swirl
    return ret;
1305 20d97356 Blue Swirl
}
1306 20d97356 Blue Swirl
1307 7c80ab3f Jes Sorensen
static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1308 7c80ab3f Jes Sorensen
                              int64_t pos, int size)
1309 20d97356 Blue Swirl
{
1310 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1311 20d97356 Blue Swirl
    int growable = bs->growable;
1312 20d97356 Blue Swirl
    int ret;
1313 20d97356 Blue Swirl
1314 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1315 20d97356 Blue Swirl
    bs->growable = 1;
1316 7c80ab3f Jes Sorensen
    ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1317 20d97356 Blue Swirl
    bs->growable = growable;
1318 20d97356 Blue Swirl
1319 20d97356 Blue Swirl
    return ret;
1320 20d97356 Blue Swirl
}
1321 20d97356 Blue Swirl
1322 7c80ab3f Jes Sorensen
static QEMUOptionParameter qcow2_create_options[] = {
1323 20d97356 Blue Swirl
    {
1324 20d97356 Blue Swirl
        .name = BLOCK_OPT_SIZE,
1325 20d97356 Blue Swirl
        .type = OPT_SIZE,
1326 20d97356 Blue Swirl
        .help = "Virtual disk size"
1327 20d97356 Blue Swirl
    },
1328 20d97356 Blue Swirl
    {
1329 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FILE,
1330 20d97356 Blue Swirl
        .type = OPT_STRING,
1331 20d97356 Blue Swirl
        .help = "File name of a base image"
1332 20d97356 Blue Swirl
    },
1333 20d97356 Blue Swirl
    {
1334 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FMT,
1335 20d97356 Blue Swirl
        .type = OPT_STRING,
1336 20d97356 Blue Swirl
        .help = "Image format of the base image"
1337 20d97356 Blue Swirl
    },
1338 20d97356 Blue Swirl
    {
1339 20d97356 Blue Swirl
        .name = BLOCK_OPT_ENCRYPT,
1340 20d97356 Blue Swirl
        .type = OPT_FLAG,
1341 20d97356 Blue Swirl
        .help = "Encrypt the image"
1342 20d97356 Blue Swirl
    },
1343 20d97356 Blue Swirl
    {
1344 20d97356 Blue Swirl
        .name = BLOCK_OPT_CLUSTER_SIZE,
1345 20d97356 Blue Swirl
        .type = OPT_SIZE,
1346 99cce9fa Kevin Wolf
        .help = "qcow2 cluster size",
1347 99cce9fa Kevin Wolf
        .value = { .n = DEFAULT_CLUSTER_SIZE },
1348 20d97356 Blue Swirl
    },
1349 20d97356 Blue Swirl
    {
1350 20d97356 Blue Swirl
        .name = BLOCK_OPT_PREALLOC,
1351 20d97356 Blue Swirl
        .type = OPT_STRING,
1352 20d97356 Blue Swirl
        .help = "Preallocation mode (allowed values: off, metadata)"
1353 20d97356 Blue Swirl
    },
1354 20d97356 Blue Swirl
    { NULL }
1355 20d97356 Blue Swirl
};
1356 20d97356 Blue Swirl
1357 20d97356 Blue Swirl
static BlockDriver bdrv_qcow2 = {
1358 7c80ab3f Jes Sorensen
    .format_name        = "qcow2",
1359 7c80ab3f Jes Sorensen
    .instance_size      = sizeof(BDRVQcowState),
1360 7c80ab3f Jes Sorensen
    .bdrv_probe         = qcow2_probe,
1361 7c80ab3f Jes Sorensen
    .bdrv_open          = qcow2_open,
1362 7c80ab3f Jes Sorensen
    .bdrv_close         = qcow2_close,
1363 7c80ab3f Jes Sorensen
    .bdrv_create        = qcow2_create,
1364 7c80ab3f Jes Sorensen
    .bdrv_flush         = qcow2_flush,
1365 7c80ab3f Jes Sorensen
    .bdrv_is_allocated  = qcow2_is_allocated,
1366 7c80ab3f Jes Sorensen
    .bdrv_set_key       = qcow2_set_key,
1367 7c80ab3f Jes Sorensen
    .bdrv_make_empty    = qcow2_make_empty,
1368 7c80ab3f Jes Sorensen
1369 7c80ab3f Jes Sorensen
    .bdrv_aio_readv     = qcow2_aio_readv,
1370 7c80ab3f Jes Sorensen
    .bdrv_aio_writev    = qcow2_aio_writev,
1371 7c80ab3f Jes Sorensen
    .bdrv_aio_flush     = qcow2_aio_flush,
1372 419b19d9 Stefan Hajnoczi
1373 5ea929e3 Kevin Wolf
    .bdrv_discard           = qcow2_discard,
1374 419b19d9 Stefan Hajnoczi
    .bdrv_truncate          = qcow2_truncate,
1375 7c80ab3f Jes Sorensen
    .bdrv_write_compressed  = qcow2_write_compressed,
1376 20d97356 Blue Swirl
1377 20d97356 Blue Swirl
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1378 20d97356 Blue Swirl
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1379 20d97356 Blue Swirl
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1380 20d97356 Blue Swirl
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1381 51ef6727 edison
    .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
1382 7c80ab3f Jes Sorensen
    .bdrv_get_info      = qcow2_get_info,
1383 20d97356 Blue Swirl
1384 7c80ab3f Jes Sorensen
    .bdrv_save_vmstate    = qcow2_save_vmstate,
1385 7c80ab3f Jes Sorensen
    .bdrv_load_vmstate    = qcow2_load_vmstate,
1386 20d97356 Blue Swirl
1387 20d97356 Blue Swirl
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1388 20d97356 Blue Swirl
1389 7c80ab3f Jes Sorensen
    .create_options = qcow2_create_options,
1390 7c80ab3f Jes Sorensen
    .bdrv_check = qcow2_check,
1391 20d97356 Blue Swirl
};
1392 20d97356 Blue Swirl
1393 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1394 5efa9d5a Anthony Liguori
{
1395 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1396 5efa9d5a Anthony Liguori
}
1397 5efa9d5a Anthony Liguori
1398 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);