Statistics
| Branch: | Revision:

root / block / qcow2.c @ c142442b

History | View | Annotate | Download (30.2 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 585f8587 bellard
31 585f8587 bellard
/*
32 585f8587 bellard
  Differences with QCOW:
33 585f8587 bellard

34 585f8587 bellard
  - Support for multiple incremental snapshots.
35 585f8587 bellard
  - Memory management by reference counts.
36 585f8587 bellard
  - Clusters which have a reference count of one have the bit
37 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
38 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
39 585f8587 bellard
    in the cluster offsets.
40 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
41 3b46e624 ths
    snapshots.
42 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
43 585f8587 bellard
    (could be backported to QCOW).
44 585f8587 bellard
  - L2 tables have always a size of one cluster.
45 585f8587 bellard
*/
46 585f8587 bellard
47 585f8587 bellard
//#define DEBUG_ALLOC
48 585f8587 bellard
//#define DEBUG_ALLOC2
49 9b80ddf3 aliguori
//#define DEBUG_EXT
50 5fafdf24 ths
51 9b80ddf3 aliguori
52 9b80ddf3 aliguori
typedef struct {
53 9b80ddf3 aliguori
    uint32_t magic;
54 9b80ddf3 aliguori
    uint32_t len;
55 9b80ddf3 aliguori
} QCowExtension;
56 9b80ddf3 aliguori
#define  QCOW_EXT_MAGIC_END 0
57 f965509c aliguori
#define  QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
58 9b80ddf3 aliguori
59 9b80ddf3 aliguori
60 585f8587 bellard
61 585f8587 bellard
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
62 585f8587 bellard
{
63 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
64 3b46e624 ths
65 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
66 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
67 5fafdf24 ths
        be32_to_cpu(cow_header->version) == QCOW_VERSION)
68 585f8587 bellard
        return 100;
69 585f8587 bellard
    else
70 585f8587 bellard
        return 0;
71 585f8587 bellard
}
72 585f8587 bellard
73 9b80ddf3 aliguori
74 9b80ddf3 aliguori
/* 
75 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
76 9b80ddf3 aliguori
 * start reading from start_offset
77 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
78 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
79 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
80 9b80ddf3 aliguori
 */
81 9b80ddf3 aliguori
static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset,
82 9b80ddf3 aliguori
                                uint64_t end_offset)
83 9b80ddf3 aliguori
{
84 9b80ddf3 aliguori
    BDRVQcowState *s = bs->opaque;
85 9b80ddf3 aliguori
    QCowExtension ext;
86 9b80ddf3 aliguori
    uint64_t offset;
87 9b80ddf3 aliguori
88 9b80ddf3 aliguori
#ifdef DEBUG_EXT
89 9b80ddf3 aliguori
    printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
90 9b80ddf3 aliguori
#endif
91 9b80ddf3 aliguori
    offset = start_offset;
92 9b80ddf3 aliguori
    while (offset < end_offset) {
93 9b80ddf3 aliguori
94 9b80ddf3 aliguori
#ifdef DEBUG_EXT
95 9b80ddf3 aliguori
        /* Sanity check */
96 9b80ddf3 aliguori
        if (offset > s->cluster_size)
97 9b80ddf3 aliguori
            printf("qcow_handle_extension: suspicious offset %lu\n", offset);
98 9b80ddf3 aliguori
99 9b80ddf3 aliguori
        printf("attemting to read extended header in offset %lu\n", offset);
100 9b80ddf3 aliguori
#endif
101 9b80ddf3 aliguori
102 9b80ddf3 aliguori
        if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) {
103 4c978075 aliguori
            fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
104 4c978075 aliguori
                    (unsigned long long)offset);
105 9b80ddf3 aliguori
            return 1;
106 9b80ddf3 aliguori
        }
107 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
108 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
109 9b80ddf3 aliguori
        offset += sizeof(ext);
110 9b80ddf3 aliguori
#ifdef DEBUG_EXT
111 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
112 9b80ddf3 aliguori
#endif
113 9b80ddf3 aliguori
        switch (ext.magic) {
114 9b80ddf3 aliguori
        case QCOW_EXT_MAGIC_END:
115 9b80ddf3 aliguori
            return 0;
116 f965509c aliguori
117 f965509c aliguori
        case QCOW_EXT_MAGIC_BACKING_FORMAT:
118 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
119 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
120 4c978075 aliguori
                        " (>=%zu)\n",
121 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
122 f965509c aliguori
                return 2;
123 f965509c aliguori
            }
124 f965509c aliguori
            if (bdrv_pread(s->hd, offset , bs->backing_format,
125 f965509c aliguori
                           ext.len) != ext.len)
126 f965509c aliguori
                return 3;
127 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
128 f965509c aliguori
#ifdef DEBUG_EXT
129 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
130 f965509c aliguori
#endif
131 f965509c aliguori
            offset += ((ext.len + 7) & ~7);
132 f965509c aliguori
            break;
133 f965509c aliguori
134 9b80ddf3 aliguori
        default:
135 9b80ddf3 aliguori
            /* unknown magic -- just skip it */
136 9b80ddf3 aliguori
            offset += ((ext.len + 7) & ~7);
137 9b80ddf3 aliguori
            break;
138 9b80ddf3 aliguori
        }
139 9b80ddf3 aliguori
    }
140 9b80ddf3 aliguori
141 9b80ddf3 aliguori
    return 0;
142 9b80ddf3 aliguori
}
143 9b80ddf3 aliguori
144 9b80ddf3 aliguori
145 585f8587 bellard
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
146 585f8587 bellard
{
147 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
148 585f8587 bellard
    int len, i, shift, ret;
149 585f8587 bellard
    QCowHeader header;
150 9b80ddf3 aliguori
    uint64_t ext_end;
151 585f8587 bellard
152 4dc822d7 aliguori
    /* Performance is terrible right now with cache=writethrough due mainly
153 4dc822d7 aliguori
     * to reference count updates.  If the user does not explicitly specify
154 4dc822d7 aliguori
     * a caching type, force to writeback caching.
155 4dc822d7 aliguori
     */
156 4dc822d7 aliguori
    if ((flags & BDRV_O_CACHE_DEF)) {
157 4dc822d7 aliguori
        flags |= BDRV_O_CACHE_WB;
158 4dc822d7 aliguori
        flags &= ~BDRV_O_CACHE_DEF;
159 4dc822d7 aliguori
    }
160 b5eff355 aurel32
    ret = bdrv_file_open(&s->hd, filename, flags);
161 585f8587 bellard
    if (ret < 0)
162 585f8587 bellard
        return ret;
163 585f8587 bellard
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
164 585f8587 bellard
        goto fail;
165 585f8587 bellard
    be32_to_cpus(&header.magic);
166 585f8587 bellard
    be32_to_cpus(&header.version);
167 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
168 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
169 585f8587 bellard
    be64_to_cpus(&header.size);
170 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
171 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
172 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
173 585f8587 bellard
    be32_to_cpus(&header.l1_size);
174 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
175 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
176 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
177 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
178 3b46e624 ths
179 585f8587 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
180 585f8587 bellard
        goto fail;
181 5fafdf24 ths
    if (header.size <= 1 ||
182 73c632ed Kevin Wolf
        header.cluster_bits < MIN_CLUSTER_BITS ||
183 73c632ed Kevin Wolf
        header.cluster_bits > MAX_CLUSTER_BITS)
184 585f8587 bellard
        goto fail;
185 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
186 585f8587 bellard
        goto fail;
187 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
188 585f8587 bellard
    if (s->crypt_method_header)
189 585f8587 bellard
        bs->encrypted = 1;
190 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
191 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
192 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
193 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
194 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
195 585f8587 bellard
    bs->total_sectors = header.size / 512;
196 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
197 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
198 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
199 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
200 5fafdf24 ths
    s->refcount_table_size =
201 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
202 585f8587 bellard
203 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
204 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
205 585f8587 bellard
206 585f8587 bellard
    /* read the level 1 table */
207 585f8587 bellard
    s->l1_size = header.l1_size;
208 585f8587 bellard
    shift = s->cluster_bits + s->l2_bits;
209 585f8587 bellard
    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
210 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
211 585f8587 bellard
       header.size bytes */
212 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
213 585f8587 bellard
        goto fail;
214 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
215 585f8587 bellard
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
216 5fafdf24 ths
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
217 585f8587 bellard
        s->l1_size * sizeof(uint64_t))
218 585f8587 bellard
        goto fail;
219 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
220 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
221 585f8587 bellard
    }
222 585f8587 bellard
    /* alloc L2 cache */
223 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
224 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
225 585f8587 bellard
    /* one more sector for decompressed data alignment */
226 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
227 095a9c58 aliguori
                                  + 512);
228 585f8587 bellard
    s->cluster_cache_offset = -1;
229 3b46e624 ths
230 585f8587 bellard
    if (refcount_init(bs) < 0)
231 585f8587 bellard
        goto fail;
232 585f8587 bellard
233 9b80ddf3 aliguori
    /* read qcow2 extensions */
234 9b80ddf3 aliguori
    if (header.backing_file_offset)
235 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
236 9b80ddf3 aliguori
    else
237 9b80ddf3 aliguori
        ext_end = s->cluster_size;
238 9b80ddf3 aliguori
    if (qcow_read_extensions(bs, sizeof(header), ext_end))
239 9b80ddf3 aliguori
        goto fail;
240 9b80ddf3 aliguori
241 585f8587 bellard
    /* read the backing file name */
242 585f8587 bellard
    if (header.backing_file_offset != 0) {
243 585f8587 bellard
        len = header.backing_file_size;
244 585f8587 bellard
        if (len > 1023)
245 585f8587 bellard
            len = 1023;
246 585f8587 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
247 585f8587 bellard
            goto fail;
248 585f8587 bellard
        bs->backing_file[len] = '\0';
249 585f8587 bellard
    }
250 585f8587 bellard
    if (qcow_read_snapshots(bs) < 0)
251 585f8587 bellard
        goto fail;
252 585f8587 bellard
253 585f8587 bellard
#ifdef DEBUG_ALLOC
254 585f8587 bellard
    check_refcounts(bs);
255 585f8587 bellard
#endif
256 585f8587 bellard
    return 0;
257 585f8587 bellard
258 585f8587 bellard
 fail:
259 585f8587 bellard
    qcow_free_snapshots(bs);
260 585f8587 bellard
    refcount_close(bs);
261 585f8587 bellard
    qemu_free(s->l1_table);
262 585f8587 bellard
    qemu_free(s->l2_cache);
263 585f8587 bellard
    qemu_free(s->cluster_cache);
264 585f8587 bellard
    qemu_free(s->cluster_data);
265 585f8587 bellard
    bdrv_delete(s->hd);
266 585f8587 bellard
    return -1;
267 585f8587 bellard
}
268 585f8587 bellard
269 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
270 585f8587 bellard
{
271 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
272 585f8587 bellard
    uint8_t keybuf[16];
273 585f8587 bellard
    int len, i;
274 3b46e624 ths
275 585f8587 bellard
    memset(keybuf, 0, 16);
276 585f8587 bellard
    len = strlen(key);
277 585f8587 bellard
    if (len > 16)
278 585f8587 bellard
        len = 16;
279 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
280 585f8587 bellard
       entropy */
281 585f8587 bellard
    for(i = 0;i < len;i++) {
282 585f8587 bellard
        keybuf[i] = key[i];
283 585f8587 bellard
    }
284 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
285 585f8587 bellard
286 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
287 585f8587 bellard
        return -1;
288 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
289 585f8587 bellard
        return -1;
290 585f8587 bellard
#if 0
291 585f8587 bellard
    /* test */
292 585f8587 bellard
    {
293 585f8587 bellard
        uint8_t in[16];
294 585f8587 bellard
        uint8_t out[16];
295 585f8587 bellard
        uint8_t tmp[16];
296 585f8587 bellard
        for(i=0;i<16;i++)
297 585f8587 bellard
            in[i] = i;
298 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
299 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
300 585f8587 bellard
        for(i = 0; i < 16; i++)
301 585f8587 bellard
            printf(" %02x", tmp[i]);
302 585f8587 bellard
        printf("\n");
303 585f8587 bellard
        for(i = 0; i < 16; i++)
304 585f8587 bellard
            printf(" %02x", out[i]);
305 585f8587 bellard
        printf("\n");
306 585f8587 bellard
    }
307 585f8587 bellard
#endif
308 585f8587 bellard
    return 0;
309 585f8587 bellard
}
310 585f8587 bellard
311 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
312 585f8587 bellard
                             int nb_sectors, int *pnum)
313 585f8587 bellard
{
314 585f8587 bellard
    uint64_t cluster_offset;
315 585f8587 bellard
316 095a9c58 aliguori
    *pnum = nb_sectors;
317 095a9c58 aliguori
    cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
318 095a9c58 aliguori
319 585f8587 bellard
    return (cluster_offset != 0);
320 585f8587 bellard
}
321 585f8587 bellard
322 a9465922 bellard
/* handle reading after the end of the backing file */
323 45aba42f Kevin Wolf
int backing_read1(BlockDriverState *bs,
324 45aba42f Kevin Wolf
                  int64_t sector_num, uint8_t *buf, int nb_sectors)
325 a9465922 bellard
{
326 a9465922 bellard
    int n1;
327 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
328 a9465922 bellard
        return nb_sectors;
329 a9465922 bellard
    if (sector_num >= bs->total_sectors)
330 a9465922 bellard
        n1 = 0;
331 a9465922 bellard
    else
332 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
333 a9465922 bellard
    memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
334 a9465922 bellard
    return n1;
335 a9465922 bellard
}
336 a9465922 bellard
337 ce1a14dc pbrook
typedef struct QCowAIOCB {
338 ce1a14dc pbrook
    BlockDriverAIOCB common;
339 585f8587 bellard
    int64_t sector_num;
340 f141eafe aliguori
    QEMUIOVector *qiov;
341 585f8587 bellard
    uint8_t *buf;
342 f141eafe aliguori
    void *orig_buf;
343 585f8587 bellard
    int nb_sectors;
344 585f8587 bellard
    int n;
345 585f8587 bellard
    uint64_t cluster_offset;
346 5fafdf24 ths
    uint8_t *cluster_data;
347 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
348 c87c0672 aliguori
    struct iovec hd_iov;
349 c87c0672 aliguori
    QEMUIOVector hd_qiov;
350 1490791f aliguori
    QEMUBH *bh;
351 e976c6a1 aliguori
    QCowL2Meta l2meta;
352 585f8587 bellard
} QCowAIOCB;
353 585f8587 bellard
354 c16b5a2c Christoph Hellwig
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
355 c16b5a2c Christoph Hellwig
{
356 c16b5a2c Christoph Hellwig
    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
357 c16b5a2c Christoph Hellwig
    if (acb->hd_aiocb)
358 c16b5a2c Christoph Hellwig
        bdrv_aio_cancel(acb->hd_aiocb);
359 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
360 c16b5a2c Christoph Hellwig
}
361 c16b5a2c Christoph Hellwig
362 c16b5a2c Christoph Hellwig
static AIOPool qcow_aio_pool = {
363 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(QCowAIOCB),
364 c16b5a2c Christoph Hellwig
    .cancel             = qcow_aio_cancel,
365 c16b5a2c Christoph Hellwig
};
366 c16b5a2c Christoph Hellwig
367 1490791f aliguori
static void qcow_aio_read_cb(void *opaque, int ret);
368 1490791f aliguori
static void qcow_aio_read_bh(void *opaque)
369 1490791f aliguori
{
370 1490791f aliguori
    QCowAIOCB *acb = opaque;
371 1490791f aliguori
    qemu_bh_delete(acb->bh);
372 1490791f aliguori
    acb->bh = NULL;
373 1490791f aliguori
    qcow_aio_read_cb(opaque, 0);
374 1490791f aliguori
}
375 1490791f aliguori
376 a32ef786 aliguori
static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
377 a32ef786 aliguori
{
378 a32ef786 aliguori
    if (acb->bh)
379 a32ef786 aliguori
        return -EIO;
380 a32ef786 aliguori
381 a32ef786 aliguori
    acb->bh = qemu_bh_new(cb, acb);
382 a32ef786 aliguori
    if (!acb->bh)
383 a32ef786 aliguori
        return -EIO;
384 a32ef786 aliguori
385 a32ef786 aliguori
    qemu_bh_schedule(acb->bh);
386 a32ef786 aliguori
387 a32ef786 aliguori
    return 0;
388 a32ef786 aliguori
}
389 a32ef786 aliguori
390 585f8587 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
391 585f8587 bellard
{
392 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
393 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
394 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
395 a9465922 bellard
    int index_in_cluster, n1;
396 585f8587 bellard
397 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
398 f141eafe aliguori
    if (ret < 0)
399 f141eafe aliguori
        goto done;
400 585f8587 bellard
401 585f8587 bellard
    /* post process the read buffer */
402 ce1a14dc pbrook
    if (!acb->cluster_offset) {
403 585f8587 bellard
        /* nothing to do */
404 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
405 585f8587 bellard
        /* nothing to do */
406 585f8587 bellard
    } else {
407 585f8587 bellard
        if (s->crypt_method) {
408 5fafdf24 ths
            encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
409 5fafdf24 ths
                            acb->n, 0,
410 585f8587 bellard
                            &s->aes_decrypt_key);
411 585f8587 bellard
        }
412 585f8587 bellard
    }
413 585f8587 bellard
414 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
415 ce1a14dc pbrook
    acb->sector_num += acb->n;
416 ce1a14dc pbrook
    acb->buf += acb->n * 512;
417 585f8587 bellard
418 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
419 585f8587 bellard
        /* request completed */
420 f141eafe aliguori
        ret = 0;
421 f141eafe aliguori
        goto done;
422 585f8587 bellard
    }
423 3b46e624 ths
424 585f8587 bellard
    /* prepare next AIO request */
425 095a9c58 aliguori
    acb->n = acb->nb_sectors;
426 095a9c58 aliguori
    acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
427 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
428 ce1a14dc pbrook
429 ce1a14dc pbrook
    if (!acb->cluster_offset) {
430 585f8587 bellard
        if (bs->backing_hd) {
431 585f8587 bellard
            /* read from the base image */
432 5fafdf24 ths
            n1 = backing_read1(bs->backing_hd, acb->sector_num,
433 ce1a14dc pbrook
                               acb->buf, acb->n);
434 a9465922 bellard
            if (n1 > 0) {
435 3f4cb3d3 blueswir1
                acb->hd_iov.iov_base = (void *)acb->buf;
436 c87c0672 aliguori
                acb->hd_iov.iov_len = acb->n * 512;
437 c87c0672 aliguori
                qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
438 c87c0672 aliguori
                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
439 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
440 c87c0672 aliguori
                                    qcow_aio_read_cb, acb);
441 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
442 f141eafe aliguori
                    goto done;
443 a9465922 bellard
            } else {
444 a32ef786 aliguori
                ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
445 a32ef786 aliguori
                if (ret < 0)
446 f141eafe aliguori
                    goto done;
447 a9465922 bellard
            }
448 585f8587 bellard
        } else {
449 585f8587 bellard
            /* Note: in this case, no need to wait */
450 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
451 a32ef786 aliguori
            ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
452 a32ef786 aliguori
            if (ret < 0)
453 f141eafe aliguori
                goto done;
454 585f8587 bellard
        }
455 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
456 585f8587 bellard
        /* add AIO support for compressed blocks ? */
457 ce1a14dc pbrook
        if (decompress_cluster(s, acb->cluster_offset) < 0)
458 f141eafe aliguori
            goto done;
459 5fafdf24 ths
        memcpy(acb->buf,
460 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
461 a32ef786 aliguori
        ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
462 a32ef786 aliguori
        if (ret < 0)
463 f141eafe aliguori
            goto done;
464 585f8587 bellard
    } else {
465 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
466 585f8587 bellard
            ret = -EIO;
467 f141eafe aliguori
            goto done;
468 585f8587 bellard
        }
469 c87c0672 aliguori
470 3f4cb3d3 blueswir1
        acb->hd_iov.iov_base = (void *)acb->buf;
471 c87c0672 aliguori
        acb->hd_iov.iov_len = acb->n * 512;
472 c87c0672 aliguori
        qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
473 c87c0672 aliguori
        acb->hd_aiocb = bdrv_aio_readv(s->hd,
474 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
475 c87c0672 aliguori
                            &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
476 ce1a14dc pbrook
        if (acb->hd_aiocb == NULL)
477 f141eafe aliguori
            goto done;
478 f141eafe aliguori
    }
479 f141eafe aliguori
480 f141eafe aliguori
    return;
481 f141eafe aliguori
done:
482 f141eafe aliguori
    if (acb->qiov->niov > 1) {
483 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
484 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
485 585f8587 bellard
    }
486 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
487 f141eafe aliguori
    qemu_aio_release(acb);
488 585f8587 bellard
}
489 585f8587 bellard
490 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
491 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
492 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque, int is_write)
493 585f8587 bellard
{
494 ce1a14dc pbrook
    QCowAIOCB *acb;
495 ce1a14dc pbrook
496 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
497 ce1a14dc pbrook
    if (!acb)
498 ce1a14dc pbrook
        return NULL;
499 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
500 ce1a14dc pbrook
    acb->sector_num = sector_num;
501 f141eafe aliguori
    acb->qiov = qiov;
502 f141eafe aliguori
    if (qiov->niov > 1) {
503 e268ca52 aliguori
        acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
504 f141eafe aliguori
        if (is_write)
505 f141eafe aliguori
            qemu_iovec_to_buffer(qiov, acb->buf);
506 3f4cb3d3 blueswir1
    } else {
507 3f4cb3d3 blueswir1
        acb->buf = (uint8_t *)qiov->iov->iov_base;
508 3f4cb3d3 blueswir1
    }
509 ce1a14dc pbrook
    acb->nb_sectors = nb_sectors;
510 ce1a14dc pbrook
    acb->n = 0;
511 ce1a14dc pbrook
    acb->cluster_offset = 0;
512 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
513 ce1a14dc pbrook
    return acb;
514 ce1a14dc pbrook
}
515 ce1a14dc pbrook
516 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
517 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
518 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
519 ce1a14dc pbrook
{
520 ce1a14dc pbrook
    QCowAIOCB *acb;
521 ce1a14dc pbrook
522 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
523 ce1a14dc pbrook
    if (!acb)
524 ce1a14dc pbrook
        return NULL;
525 585f8587 bellard
526 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
527 ce1a14dc pbrook
    return &acb->common;
528 585f8587 bellard
}
529 585f8587 bellard
530 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
531 585f8587 bellard
{
532 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
533 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
534 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
535 585f8587 bellard
    int index_in_cluster;
536 585f8587 bellard
    const uint8_t *src_buf;
537 095a9c58 aliguori
    int n_end;
538 ce1a14dc pbrook
539 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
540 ce1a14dc pbrook
541 f141eafe aliguori
    if (ret < 0)
542 f141eafe aliguori
        goto done;
543 585f8587 bellard
544 e976c6a1 aliguori
    if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
545 e976c6a1 aliguori
        free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
546 f141eafe aliguori
        goto done;
547 e976c6a1 aliguori
    }
548 e976c6a1 aliguori
549 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
550 ce1a14dc pbrook
    acb->sector_num += acb->n;
551 ce1a14dc pbrook
    acb->buf += acb->n * 512;
552 585f8587 bellard
553 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
554 585f8587 bellard
        /* request completed */
555 f141eafe aliguori
        ret = 0;
556 f141eafe aliguori
        goto done;
557 585f8587 bellard
    }
558 3b46e624 ths
559 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
560 095a9c58 aliguori
    n_end = index_in_cluster + acb->nb_sectors;
561 095a9c58 aliguori
    if (s->crypt_method &&
562 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
563 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
564 095a9c58 aliguori
565 e976c6a1 aliguori
    acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
566 05203524 aliguori
                                          index_in_cluster,
567 e976c6a1 aliguori
                                          n_end, &acb->n, &acb->l2meta);
568 e976c6a1 aliguori
    if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
569 585f8587 bellard
        ret = -EIO;
570 f141eafe aliguori
        goto done;
571 585f8587 bellard
    }
572 585f8587 bellard
    if (s->crypt_method) {
573 ce1a14dc pbrook
        if (!acb->cluster_data) {
574 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
575 095a9c58 aliguori
                                             s->cluster_size);
576 585f8587 bellard
        }
577 5fafdf24 ths
        encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
578 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
579 ce1a14dc pbrook
        src_buf = acb->cluster_data;
580 585f8587 bellard
    } else {
581 ce1a14dc pbrook
        src_buf = acb->buf;
582 585f8587 bellard
    }
583 c87c0672 aliguori
    acb->hd_iov.iov_base = (void *)src_buf;
584 c87c0672 aliguori
    acb->hd_iov.iov_len = acb->n * 512;
585 c87c0672 aliguori
    qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
586 c87c0672 aliguori
    acb->hd_aiocb = bdrv_aio_writev(s->hd,
587 c87c0672 aliguori
                                    (acb->cluster_offset >> 9) + index_in_cluster,
588 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
589 c87c0672 aliguori
                                    qcow_aio_write_cb, acb);
590 ce1a14dc pbrook
    if (acb->hd_aiocb == NULL)
591 f141eafe aliguori
        goto done;
592 f141eafe aliguori
593 f141eafe aliguori
    return;
594 f141eafe aliguori
595 f141eafe aliguori
done:
596 f141eafe aliguori
    if (acb->qiov->niov > 1)
597 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
598 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
599 f141eafe aliguori
    qemu_aio_release(acb);
600 585f8587 bellard
}
601 585f8587 bellard
602 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
603 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
604 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
605 585f8587 bellard
{
606 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
607 ce1a14dc pbrook
    QCowAIOCB *acb;
608 3b46e624 ths
609 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
610 585f8587 bellard
611 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
612 ce1a14dc pbrook
    if (!acb)
613 ce1a14dc pbrook
        return NULL;
614 3b46e624 ths
615 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
616 ce1a14dc pbrook
    return &acb->common;
617 585f8587 bellard
}
618 585f8587 bellard
619 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
620 585f8587 bellard
{
621 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
622 585f8587 bellard
    qemu_free(s->l1_table);
623 585f8587 bellard
    qemu_free(s->l2_cache);
624 585f8587 bellard
    qemu_free(s->cluster_cache);
625 585f8587 bellard
    qemu_free(s->cluster_data);
626 585f8587 bellard
    refcount_close(bs);
627 585f8587 bellard
    bdrv_delete(s->hd);
628 585f8587 bellard
}
629 585f8587 bellard
630 73c632ed Kevin Wolf
static int get_bits_from_size(size_t size)
631 73c632ed Kevin Wolf
{
632 73c632ed Kevin Wolf
    int res = 0;
633 73c632ed Kevin Wolf
634 73c632ed Kevin Wolf
    if (size == 0) {
635 73c632ed Kevin Wolf
        return -1;
636 73c632ed Kevin Wolf
    }
637 73c632ed Kevin Wolf
638 73c632ed Kevin Wolf
    while (size != 1) {
639 73c632ed Kevin Wolf
        /* Not a power of two */
640 73c632ed Kevin Wolf
        if (size & 1) {
641 73c632ed Kevin Wolf
            return -1;
642 73c632ed Kevin Wolf
        }
643 73c632ed Kevin Wolf
644 73c632ed Kevin Wolf
        size >>= 1;
645 73c632ed Kevin Wolf
        res++;
646 73c632ed Kevin Wolf
    }
647 73c632ed Kevin Wolf
648 73c632ed Kevin Wolf
    return res;
649 73c632ed Kevin Wolf
}
650 73c632ed Kevin Wolf
651 f965509c aliguori
static int qcow_create2(const char *filename, int64_t total_size,
652 f965509c aliguori
                        const char *backing_file, const char *backing_format,
653 73c632ed Kevin Wolf
                        int flags, size_t cluster_size)
654 585f8587 bellard
{
655 f965509c aliguori
656 585f8587 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
657 2d2431f0 aliguori
    int ref_clusters, backing_format_len = 0;
658 585f8587 bellard
    QCowHeader header;
659 585f8587 bellard
    uint64_t tmp, offset;
660 585f8587 bellard
    QCowCreateState s1, *s = &s1;
661 f965509c aliguori
    QCowExtension ext_bf = {0, 0};
662 f965509c aliguori
663 3b46e624 ths
664 585f8587 bellard
    memset(s, 0, sizeof(*s));
665 585f8587 bellard
666 585f8587 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
667 585f8587 bellard
    if (fd < 0)
668 585f8587 bellard
        return -1;
669 585f8587 bellard
    memset(&header, 0, sizeof(header));
670 585f8587 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
671 585f8587 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
672 585f8587 bellard
    header.size = cpu_to_be64(total_size * 512);
673 585f8587 bellard
    header_size = sizeof(header);
674 585f8587 bellard
    backing_filename_len = 0;
675 585f8587 bellard
    if (backing_file) {
676 f965509c aliguori
        if (backing_format) {
677 f965509c aliguori
            ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
678 f965509c aliguori
            backing_format_len = strlen(backing_format);
679 f965509c aliguori
            ext_bf.len = (backing_format_len + 7) & ~7;
680 f965509c aliguori
            header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7);
681 f965509c aliguori
        }
682 585f8587 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
683 585f8587 bellard
        backing_filename_len = strlen(backing_file);
684 585f8587 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
685 585f8587 bellard
        header_size += backing_filename_len;
686 585f8587 bellard
    }
687 73c632ed Kevin Wolf
688 73c632ed Kevin Wolf
    /* Cluster size */
689 73c632ed Kevin Wolf
    s->cluster_bits = get_bits_from_size(cluster_size);
690 73c632ed Kevin Wolf
    if (s->cluster_bits < MIN_CLUSTER_BITS ||
691 73c632ed Kevin Wolf
        s->cluster_bits > MAX_CLUSTER_BITS)
692 73c632ed Kevin Wolf
    {
693 73c632ed Kevin Wolf
        fprintf(stderr, "Cluster size must be a power of two between "
694 73c632ed Kevin Wolf
            "%d and %dk\n",
695 73c632ed Kevin Wolf
            1 << MIN_CLUSTER_BITS,
696 73c632ed Kevin Wolf
            1 << (MAX_CLUSTER_BITS - 10));
697 73c632ed Kevin Wolf
        return -EINVAL;
698 73c632ed Kevin Wolf
    }
699 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
700 73c632ed Kevin Wolf
701 585f8587 bellard
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
702 585f8587 bellard
    header_size = (header_size + 7) & ~7;
703 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT) {
704 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
705 585f8587 bellard
    } else {
706 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
707 585f8587 bellard
    }
708 585f8587 bellard
    l2_bits = s->cluster_bits - 3;
709 585f8587 bellard
    shift = s->cluster_bits + l2_bits;
710 585f8587 bellard
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
711 585f8587 bellard
    offset = align_offset(header_size, s->cluster_size);
712 585f8587 bellard
    s->l1_table_offset = offset;
713 585f8587 bellard
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
714 585f8587 bellard
    header.l1_size = cpu_to_be32(l1_size);
715 15e6690a bellard
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
716 585f8587 bellard
717 585f8587 bellard
    s->refcount_table = qemu_mallocz(s->cluster_size);
718 3b46e624 ths
719 585f8587 bellard
    s->refcount_table_offset = offset;
720 585f8587 bellard
    header.refcount_table_offset = cpu_to_be64(offset);
721 585f8587 bellard
    header.refcount_table_clusters = cpu_to_be32(1);
722 585f8587 bellard
    offset += s->cluster_size;
723 585f8587 bellard
    s->refcount_block_offset = offset;
724 2d2431f0 aliguori
725 2d2431f0 aliguori
    /* count how many refcount blocks needed */
726 2d2431f0 aliguori
    tmp = offset >> s->cluster_bits;
727 2d2431f0 aliguori
    ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
728 2d2431f0 aliguori
    for (i=0; i < ref_clusters; i++) {
729 2d2431f0 aliguori
        s->refcount_table[i] = cpu_to_be64(offset);
730 2d2431f0 aliguori
        offset += s->cluster_size;
731 2d2431f0 aliguori
    }
732 2d2431f0 aliguori
733 2d2431f0 aliguori
    s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
734 585f8587 bellard
735 585f8587 bellard
    /* update refcounts */
736 585f8587 bellard
    create_refcount_update(s, 0, header_size);
737 15e6690a bellard
    create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
738 585f8587 bellard
    create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
739 2d2431f0 aliguori
    create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size);
740 3b46e624 ths
741 585f8587 bellard
    /* write all the data */
742 585f8587 bellard
    write(fd, &header, sizeof(header));
743 585f8587 bellard
    if (backing_file) {
744 f965509c aliguori
        if (backing_format_len) {
745 f965509c aliguori
            char zero[16];
746 f965509c aliguori
            int d = ext_bf.len - backing_format_len;
747 f965509c aliguori
748 f965509c aliguori
            memset(zero, 0, sizeof(zero));
749 f965509c aliguori
            cpu_to_be32s(&ext_bf.magic);
750 f965509c aliguori
            cpu_to_be32s(&ext_bf.len);
751 f965509c aliguori
            write(fd, &ext_bf, sizeof(ext_bf));
752 f965509c aliguori
            write(fd, backing_format, backing_format_len);
753 f965509c aliguori
            if (d>0) {
754 f965509c aliguori
                write(fd, zero, d);
755 f965509c aliguori
            }
756 f965509c aliguori
        }
757 585f8587 bellard
        write(fd, backing_file, backing_filename_len);
758 585f8587 bellard
    }
759 585f8587 bellard
    lseek(fd, s->l1_table_offset, SEEK_SET);
760 585f8587 bellard
    tmp = 0;
761 585f8587 bellard
    for(i = 0;i < l1_size; i++) {
762 585f8587 bellard
        write(fd, &tmp, sizeof(tmp));
763 585f8587 bellard
    }
764 585f8587 bellard
    lseek(fd, s->refcount_table_offset, SEEK_SET);
765 585f8587 bellard
    write(fd, s->refcount_table, s->cluster_size);
766 3b46e624 ths
767 585f8587 bellard
    lseek(fd, s->refcount_block_offset, SEEK_SET);
768 2d2431f0 aliguori
    write(fd, s->refcount_block, ref_clusters * s->cluster_size);
769 585f8587 bellard
770 585f8587 bellard
    qemu_free(s->refcount_table);
771 585f8587 bellard
    qemu_free(s->refcount_block);
772 585f8587 bellard
    close(fd);
773 585f8587 bellard
    return 0;
774 585f8587 bellard
}
775 585f8587 bellard
776 0e7e1989 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
777 0e7e1989 Kevin Wolf
{
778 0e7e1989 Kevin Wolf
    const char *backing_file = NULL;
779 0e7e1989 Kevin Wolf
    const char *backing_fmt = NULL;
780 0e7e1989 Kevin Wolf
    uint64_t sectors = 0;
781 0e7e1989 Kevin Wolf
    int flags = 0;
782 9ccb258e Kevin Wolf
    size_t cluster_size = 65536;
783 0e7e1989 Kevin Wolf
784 0e7e1989 Kevin Wolf
    /* Read out options */
785 0e7e1989 Kevin Wolf
    while (options && options->name) {
786 0e7e1989 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
787 0e7e1989 Kevin Wolf
            sectors = options->value.n / 512;
788 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
789 0e7e1989 Kevin Wolf
            backing_file = options->value.s;
790 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
791 0e7e1989 Kevin Wolf
            backing_fmt = options->value.s;
792 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
793 0e7e1989 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
794 73c632ed Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
795 73c632ed Kevin Wolf
            if (options->value.n) {
796 73c632ed Kevin Wolf
                cluster_size = options->value.n;
797 73c632ed Kevin Wolf
            }
798 0e7e1989 Kevin Wolf
        }
799 0e7e1989 Kevin Wolf
        options++;
800 0e7e1989 Kevin Wolf
    }
801 0e7e1989 Kevin Wolf
802 73c632ed Kevin Wolf
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
803 73c632ed Kevin Wolf
        cluster_size);
804 f965509c aliguori
}
805 f965509c aliguori
806 585f8587 bellard
static int qcow_make_empty(BlockDriverState *bs)
807 585f8587 bellard
{
808 585f8587 bellard
#if 0
809 585f8587 bellard
    /* XXX: not correct */
810 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
811 585f8587 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
812 585f8587 bellard
    int ret;
813 585f8587 bellard

814 585f8587 bellard
    memset(s->l1_table, 0, l1_length);
815 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
816 ac674887 aliguori
        return -1;
817 585f8587 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
818 585f8587 bellard
    if (ret < 0)
819 585f8587 bellard
        return ret;
820 3b46e624 ths

821 585f8587 bellard
    l2_cache_reset(bs);
822 585f8587 bellard
#endif
823 585f8587 bellard
    return 0;
824 585f8587 bellard
}
825 585f8587 bellard
826 585f8587 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
827 585f8587 bellard
   tables to avoid losing bytes in alignment */
828 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
829 585f8587 bellard
                                 const uint8_t *buf, int nb_sectors)
830 585f8587 bellard
{
831 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
832 585f8587 bellard
    z_stream strm;
833 585f8587 bellard
    int ret, out_len;
834 585f8587 bellard
    uint8_t *out_buf;
835 585f8587 bellard
    uint64_t cluster_offset;
836 585f8587 bellard
837 585f8587 bellard
    if (nb_sectors == 0) {
838 585f8587 bellard
        /* align end of file to a sector boundary to ease reading with
839 585f8587 bellard
           sector based I/Os */
840 585f8587 bellard
        cluster_offset = bdrv_getlength(s->hd);
841 585f8587 bellard
        cluster_offset = (cluster_offset + 511) & ~511;
842 585f8587 bellard
        bdrv_truncate(s->hd, cluster_offset);
843 585f8587 bellard
        return 0;
844 585f8587 bellard
    }
845 585f8587 bellard
846 585f8587 bellard
    if (nb_sectors != s->cluster_sectors)
847 585f8587 bellard
        return -EINVAL;
848 585f8587 bellard
849 585f8587 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
850 585f8587 bellard
851 585f8587 bellard
    /* best compression, small window, no zlib header */
852 585f8587 bellard
    memset(&strm, 0, sizeof(strm));
853 585f8587 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
854 5fafdf24 ths
                       Z_DEFLATED, -12,
855 585f8587 bellard
                       9, Z_DEFAULT_STRATEGY);
856 585f8587 bellard
    if (ret != 0) {
857 585f8587 bellard
        qemu_free(out_buf);
858 585f8587 bellard
        return -1;
859 585f8587 bellard
    }
860 585f8587 bellard
861 585f8587 bellard
    strm.avail_in = s->cluster_size;
862 585f8587 bellard
    strm.next_in = (uint8_t *)buf;
863 585f8587 bellard
    strm.avail_out = s->cluster_size;
864 585f8587 bellard
    strm.next_out = out_buf;
865 585f8587 bellard
866 585f8587 bellard
    ret = deflate(&strm, Z_FINISH);
867 585f8587 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
868 585f8587 bellard
        qemu_free(out_buf);
869 585f8587 bellard
        deflateEnd(&strm);
870 585f8587 bellard
        return -1;
871 585f8587 bellard
    }
872 585f8587 bellard
    out_len = strm.next_out - out_buf;
873 585f8587 bellard
874 585f8587 bellard
    deflateEnd(&strm);
875 585f8587 bellard
876 585f8587 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
877 585f8587 bellard
        /* could not compress: write normal cluster */
878 ade40677 Kevin Wolf
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
879 585f8587 bellard
    } else {
880 52d893ec aliguori
        cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
881 52d893ec aliguori
                                              out_len);
882 52d893ec aliguori
        if (!cluster_offset)
883 52d893ec aliguori
            return -1;
884 585f8587 bellard
        cluster_offset &= s->cluster_offset_mask;
885 585f8587 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
886 585f8587 bellard
            qemu_free(out_buf);
887 585f8587 bellard
            return -1;
888 585f8587 bellard
        }
889 585f8587 bellard
    }
890 3b46e624 ths
891 585f8587 bellard
    qemu_free(out_buf);
892 585f8587 bellard
    return 0;
893 585f8587 bellard
}
894 585f8587 bellard
895 585f8587 bellard
static void qcow_flush(BlockDriverState *bs)
896 585f8587 bellard
{
897 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
898 585f8587 bellard
    bdrv_flush(s->hd);
899 585f8587 bellard
}
900 585f8587 bellard
901 585f8587 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
902 585f8587 bellard
{
903 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
904 585f8587 bellard
    bdi->cluster_size = s->cluster_size;
905 5fafdf24 ths
    bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
906 585f8587 bellard
        (s->cluster_bits + s->l2_bits);
907 585f8587 bellard
    return 0;
908 585f8587 bellard
}
909 585f8587 bellard
910 585f8587 bellard
911 e97fc193 aliguori
static int qcow_check(BlockDriverState *bs)
912 e97fc193 aliguori
{
913 e97fc193 aliguori
    return check_refcounts(bs);
914 585f8587 bellard
}
915 585f8587 bellard
916 585f8587 bellard
#if 0
917 585f8587 bellard
static void dump_refcounts(BlockDriverState *bs)
918 585f8587 bellard
{
919 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
920 585f8587 bellard
    int64_t nb_clusters, k, k1, size;
921 585f8587 bellard
    int refcount;
922 585f8587 bellard

923 585f8587 bellard
    size = bdrv_getlength(s->hd);
924 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
925 585f8587 bellard
    for(k = 0; k < nb_clusters;) {
926 585f8587 bellard
        k1 = k;
927 585f8587 bellard
        refcount = get_refcount(bs, k);
928 585f8587 bellard
        k++;
929 585f8587 bellard
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
930 585f8587 bellard
            k++;
931 585f8587 bellard
        printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
932 585f8587 bellard
    }
933 585f8587 bellard
}
934 585f8587 bellard
#endif
935 585f8587 bellard
936 178e08a5 aliguori
static int qcow_put_buffer(BlockDriverState *bs, const uint8_t *buf,
937 178e08a5 aliguori
                           int64_t pos, int size)
938 178e08a5 aliguori
{
939 178e08a5 aliguori
    int growable = bs->growable;
940 178e08a5 aliguori
941 178e08a5 aliguori
    bs->growable = 1;
942 178e08a5 aliguori
    bdrv_pwrite(bs, pos, buf, size);
943 178e08a5 aliguori
    bs->growable = growable;
944 178e08a5 aliguori
945 178e08a5 aliguori
    return size;
946 178e08a5 aliguori
}
947 178e08a5 aliguori
948 178e08a5 aliguori
static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf,
949 178e08a5 aliguori
                           int64_t pos, int size)
950 178e08a5 aliguori
{
951 178e08a5 aliguori
    int growable = bs->growable;
952 178e08a5 aliguori
    int ret;
953 178e08a5 aliguori
954 178e08a5 aliguori
    bs->growable = 1;
955 178e08a5 aliguori
    ret = bdrv_pread(bs, pos, buf, size);
956 178e08a5 aliguori
    bs->growable = growable;
957 178e08a5 aliguori
958 178e08a5 aliguori
    return ret;
959 178e08a5 aliguori
}
960 178e08a5 aliguori
961 0e7e1989 Kevin Wolf
static QEMUOptionParameter qcow_create_options[] = {
962 db08adf5 Kevin Wolf
    {
963 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_SIZE,
964 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
965 db08adf5 Kevin Wolf
        .help = "Virtual disk size"
966 db08adf5 Kevin Wolf
    },
967 db08adf5 Kevin Wolf
    {
968 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FILE,
969 db08adf5 Kevin Wolf
        .type = OPT_STRING,
970 db08adf5 Kevin Wolf
        .help = "File name of a base image"
971 db08adf5 Kevin Wolf
    },
972 db08adf5 Kevin Wolf
    {
973 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FMT,
974 db08adf5 Kevin Wolf
        .type = OPT_STRING,
975 db08adf5 Kevin Wolf
        .help = "Image format of the base image"
976 db08adf5 Kevin Wolf
    },
977 db08adf5 Kevin Wolf
    {
978 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_ENCRYPT,
979 db08adf5 Kevin Wolf
        .type = OPT_FLAG,
980 db08adf5 Kevin Wolf
        .help = "Encrypt the image"
981 db08adf5 Kevin Wolf
    },
982 db08adf5 Kevin Wolf
    {
983 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_CLUSTER_SIZE,
984 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
985 db08adf5 Kevin Wolf
        .help = "qcow2 cluster size"
986 db08adf5 Kevin Wolf
    },
987 0e7e1989 Kevin Wolf
    { NULL }
988 0e7e1989 Kevin Wolf
};
989 0e7e1989 Kevin Wolf
990 5efa9d5a Anthony Liguori
static BlockDriver bdrv_qcow2 = {
991 e60f469c aurel32
    .format_name        = "qcow2",
992 e60f469c aurel32
    .instance_size        = sizeof(BDRVQcowState),
993 e60f469c aurel32
    .bdrv_probe                = qcow_probe,
994 e60f469c aurel32
    .bdrv_open                = qcow_open,
995 e60f469c aurel32
    .bdrv_close                = qcow_close,
996 e60f469c aurel32
    .bdrv_create        = qcow_create,
997 e60f469c aurel32
    .bdrv_flush                = qcow_flush,
998 e60f469c aurel32
    .bdrv_is_allocated        = qcow_is_allocated,
999 e60f469c aurel32
    .bdrv_set_key        = qcow_set_key,
1000 e60f469c aurel32
    .bdrv_make_empty        = qcow_make_empty,
1001 e60f469c aurel32
1002 f141eafe aliguori
    .bdrv_aio_readv        = qcow_aio_readv,
1003 f141eafe aliguori
    .bdrv_aio_writev        = qcow_aio_writev,
1004 585f8587 bellard
    .bdrv_write_compressed = qcow_write_compressed,
1005 585f8587 bellard
1006 585f8587 bellard
    .bdrv_snapshot_create = qcow_snapshot_create,
1007 e60f469c aurel32
    .bdrv_snapshot_goto        = qcow_snapshot_goto,
1008 585f8587 bellard
    .bdrv_snapshot_delete = qcow_snapshot_delete,
1009 e60f469c aurel32
    .bdrv_snapshot_list        = qcow_snapshot_list,
1010 e60f469c aurel32
    .bdrv_get_info        = qcow_get_info,
1011 f965509c aliguori
1012 178e08a5 aliguori
    .bdrv_put_buffer    = qcow_put_buffer,
1013 178e08a5 aliguori
    .bdrv_get_buffer    = qcow_get_buffer,
1014 178e08a5 aliguori
1015 0e7e1989 Kevin Wolf
    .create_options = qcow_create_options,
1016 e97fc193 aliguori
    .bdrv_check = qcow_check,
1017 585f8587 bellard
};
1018 5efa9d5a Anthony Liguori
1019 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1020 5efa9d5a Anthony Liguori
{
1021 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1022 5efa9d5a Anthony Liguori
}
1023 5efa9d5a Anthony Liguori
1024 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);