Statistics
| Branch: | Revision:

root / block / qcow2.c @ 2000cbc5

History | View | Annotate | Download (32.3 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 9b80ddf3 aliguori
48 9b80ddf3 aliguori
typedef struct {
49 9b80ddf3 aliguori
    uint32_t magic;
50 9b80ddf3 aliguori
    uint32_t len;
51 9b80ddf3 aliguori
} QCowExtension;
52 9b80ddf3 aliguori
#define  QCOW_EXT_MAGIC_END 0
53 f965509c aliguori
#define  QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
54 9b80ddf3 aliguori
55 9b80ddf3 aliguori
56 585f8587 bellard
57 585f8587 bellard
static int qcow_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 5fafdf24 ths
        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 9b80ddf3 aliguori
static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset,
78 9b80ddf3 aliguori
                                uint64_t end_offset)
79 9b80ddf3 aliguori
{
80 9b80ddf3 aliguori
    BDRVQcowState *s = bs->opaque;
81 9b80ddf3 aliguori
    QCowExtension ext;
82 9b80ddf3 aliguori
    uint64_t offset;
83 9b80ddf3 aliguori
84 9b80ddf3 aliguori
#ifdef DEBUG_EXT
85 9b80ddf3 aliguori
    printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
86 9b80ddf3 aliguori
#endif
87 9b80ddf3 aliguori
    offset = start_offset;
88 9b80ddf3 aliguori
    while (offset < end_offset) {
89 9b80ddf3 aliguori
90 9b80ddf3 aliguori
#ifdef DEBUG_EXT
91 9b80ddf3 aliguori
        /* Sanity check */
92 9b80ddf3 aliguori
        if (offset > s->cluster_size)
93 9b80ddf3 aliguori
            printf("qcow_handle_extension: suspicious offset %lu\n", offset);
94 9b80ddf3 aliguori
95 9b80ddf3 aliguori
        printf("attemting to read extended header in offset %lu\n", offset);
96 9b80ddf3 aliguori
#endif
97 9b80ddf3 aliguori
98 9b80ddf3 aliguori
        if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) {
99 4c978075 aliguori
            fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
100 4c978075 aliguori
                    (unsigned long long)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 9b80ddf3 aliguori
        case QCOW_EXT_MAGIC_END:
111 9b80ddf3 aliguori
            return 0;
112 f965509c aliguori
113 f965509c aliguori
        case QCOW_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 f965509c aliguori
            if (bdrv_pread(s->hd, 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 f965509c aliguori
            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 9b80ddf3 aliguori
            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 585f8587 bellard
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
142 585f8587 bellard
{
143 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
144 585f8587 bellard
    int len, i, shift, ret;
145 585f8587 bellard
    QCowHeader header;
146 9b80ddf3 aliguori
    uint64_t ext_end;
147 585f8587 bellard
148 b5eff355 aurel32
    ret = bdrv_file_open(&s->hd, filename, flags);
149 585f8587 bellard
    if (ret < 0)
150 585f8587 bellard
        return ret;
151 585f8587 bellard
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
152 585f8587 bellard
        goto fail;
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 585f8587 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
168 585f8587 bellard
        goto fail;
169 5fafdf24 ths
    if (header.size <= 1 ||
170 73c632ed Kevin Wolf
        header.cluster_bits < MIN_CLUSTER_BITS ||
171 73c632ed Kevin Wolf
        header.cluster_bits > MAX_CLUSTER_BITS)
172 585f8587 bellard
        goto fail;
173 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
174 585f8587 bellard
        goto fail;
175 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
176 585f8587 bellard
    if (s->crypt_method_header)
177 585f8587 bellard
        bs->encrypted = 1;
178 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
179 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
180 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
181 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
182 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
183 585f8587 bellard
    bs->total_sectors = header.size / 512;
184 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
185 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
186 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
187 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
188 5fafdf24 ths
    s->refcount_table_size =
189 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
190 585f8587 bellard
191 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
192 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
193 585f8587 bellard
194 585f8587 bellard
    /* read the level 1 table */
195 585f8587 bellard
    s->l1_size = header.l1_size;
196 585f8587 bellard
    shift = s->cluster_bits + s->l2_bits;
197 585f8587 bellard
    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
198 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
199 585f8587 bellard
       header.size bytes */
200 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
201 585f8587 bellard
        goto fail;
202 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
203 3f6a3ee5 Kevin Wolf
    s->l1_table = qemu_mallocz(
204 3f6a3ee5 Kevin Wolf
        align_offset(s->l1_size * sizeof(uint64_t), 512));
205 5fafdf24 ths
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
206 585f8587 bellard
        s->l1_size * sizeof(uint64_t))
207 585f8587 bellard
        goto fail;
208 585f8587 bellard
    for(i = 0;i < s->l1_size; i++) {
209 585f8587 bellard
        be64_to_cpus(&s->l1_table[i]);
210 585f8587 bellard
    }
211 585f8587 bellard
    /* alloc L2 cache */
212 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
213 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
214 585f8587 bellard
    /* one more sector for decompressed data alignment */
215 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
216 095a9c58 aliguori
                                  + 512);
217 585f8587 bellard
    s->cluster_cache_offset = -1;
218 3b46e624 ths
219 ed6ccf0f Kevin Wolf
    if (qcow2_refcount_init(bs) < 0)
220 585f8587 bellard
        goto fail;
221 585f8587 bellard
222 9b80ddf3 aliguori
    /* read qcow2 extensions */
223 9b80ddf3 aliguori
    if (header.backing_file_offset)
224 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
225 9b80ddf3 aliguori
    else
226 9b80ddf3 aliguori
        ext_end = s->cluster_size;
227 9b80ddf3 aliguori
    if (qcow_read_extensions(bs, sizeof(header), ext_end))
228 9b80ddf3 aliguori
        goto fail;
229 9b80ddf3 aliguori
230 585f8587 bellard
    /* read the backing file name */
231 585f8587 bellard
    if (header.backing_file_offset != 0) {
232 585f8587 bellard
        len = header.backing_file_size;
233 585f8587 bellard
        if (len > 1023)
234 585f8587 bellard
            len = 1023;
235 585f8587 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
236 585f8587 bellard
            goto fail;
237 585f8587 bellard
        bs->backing_file[len] = '\0';
238 585f8587 bellard
    }
239 ed6ccf0f Kevin Wolf
    if (qcow2_read_snapshots(bs) < 0)
240 585f8587 bellard
        goto fail;
241 585f8587 bellard
242 585f8587 bellard
#ifdef DEBUG_ALLOC
243 14899cdf Filip Navara
    qcow2_check_refcounts(bs);
244 585f8587 bellard
#endif
245 585f8587 bellard
    return 0;
246 585f8587 bellard
247 585f8587 bellard
 fail:
248 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
249 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
250 585f8587 bellard
    qemu_free(s->l1_table);
251 585f8587 bellard
    qemu_free(s->l2_cache);
252 585f8587 bellard
    qemu_free(s->cluster_cache);
253 585f8587 bellard
    qemu_free(s->cluster_data);
254 585f8587 bellard
    bdrv_delete(s->hd);
255 585f8587 bellard
    return -1;
256 585f8587 bellard
}
257 585f8587 bellard
258 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
259 585f8587 bellard
{
260 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
261 585f8587 bellard
    uint8_t keybuf[16];
262 585f8587 bellard
    int len, i;
263 3b46e624 ths
264 585f8587 bellard
    memset(keybuf, 0, 16);
265 585f8587 bellard
    len = strlen(key);
266 585f8587 bellard
    if (len > 16)
267 585f8587 bellard
        len = 16;
268 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
269 585f8587 bellard
       entropy */
270 585f8587 bellard
    for(i = 0;i < len;i++) {
271 585f8587 bellard
        keybuf[i] = key[i];
272 585f8587 bellard
    }
273 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
274 585f8587 bellard
275 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
276 585f8587 bellard
        return -1;
277 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
278 585f8587 bellard
        return -1;
279 585f8587 bellard
#if 0
280 585f8587 bellard
    /* test */
281 585f8587 bellard
    {
282 585f8587 bellard
        uint8_t in[16];
283 585f8587 bellard
        uint8_t out[16];
284 585f8587 bellard
        uint8_t tmp[16];
285 585f8587 bellard
        for(i=0;i<16;i++)
286 585f8587 bellard
            in[i] = i;
287 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
288 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
289 585f8587 bellard
        for(i = 0; i < 16; i++)
290 585f8587 bellard
            printf(" %02x", tmp[i]);
291 585f8587 bellard
        printf("\n");
292 585f8587 bellard
        for(i = 0; i < 16; i++)
293 585f8587 bellard
            printf(" %02x", out[i]);
294 585f8587 bellard
        printf("\n");
295 585f8587 bellard
    }
296 585f8587 bellard
#endif
297 585f8587 bellard
    return 0;
298 585f8587 bellard
}
299 585f8587 bellard
300 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
301 585f8587 bellard
                             int nb_sectors, int *pnum)
302 585f8587 bellard
{
303 585f8587 bellard
    uint64_t cluster_offset;
304 585f8587 bellard
305 095a9c58 aliguori
    *pnum = nb_sectors;
306 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum);
307 095a9c58 aliguori
308 585f8587 bellard
    return (cluster_offset != 0);
309 585f8587 bellard
}
310 585f8587 bellard
311 a9465922 bellard
/* handle reading after the end of the backing file */
312 ed6ccf0f Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs,
313 45aba42f Kevin Wolf
                  int64_t sector_num, uint8_t *buf, int nb_sectors)
314 a9465922 bellard
{
315 a9465922 bellard
    int n1;
316 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
317 a9465922 bellard
        return nb_sectors;
318 a9465922 bellard
    if (sector_num >= bs->total_sectors)
319 a9465922 bellard
        n1 = 0;
320 a9465922 bellard
    else
321 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
322 a9465922 bellard
    memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
323 a9465922 bellard
    return n1;
324 a9465922 bellard
}
325 a9465922 bellard
326 ce1a14dc pbrook
typedef struct QCowAIOCB {
327 ce1a14dc pbrook
    BlockDriverAIOCB common;
328 585f8587 bellard
    int64_t sector_num;
329 f141eafe aliguori
    QEMUIOVector *qiov;
330 585f8587 bellard
    uint8_t *buf;
331 f141eafe aliguori
    void *orig_buf;
332 585f8587 bellard
    int nb_sectors;
333 585f8587 bellard
    int n;
334 585f8587 bellard
    uint64_t cluster_offset;
335 5fafdf24 ths
    uint8_t *cluster_data;
336 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
337 c87c0672 aliguori
    struct iovec hd_iov;
338 c87c0672 aliguori
    QEMUIOVector hd_qiov;
339 1490791f aliguori
    QEMUBH *bh;
340 e976c6a1 aliguori
    QCowL2Meta l2meta;
341 585f8587 bellard
} QCowAIOCB;
342 585f8587 bellard
343 c16b5a2c Christoph Hellwig
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
344 c16b5a2c Christoph Hellwig
{
345 c16b5a2c Christoph Hellwig
    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
346 c16b5a2c Christoph Hellwig
    if (acb->hd_aiocb)
347 c16b5a2c Christoph Hellwig
        bdrv_aio_cancel(acb->hd_aiocb);
348 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
349 c16b5a2c Christoph Hellwig
}
350 c16b5a2c Christoph Hellwig
351 c16b5a2c Christoph Hellwig
static AIOPool qcow_aio_pool = {
352 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(QCowAIOCB),
353 c16b5a2c Christoph Hellwig
    .cancel             = qcow_aio_cancel,
354 c16b5a2c Christoph Hellwig
};
355 c16b5a2c Christoph Hellwig
356 1490791f aliguori
static void qcow_aio_read_cb(void *opaque, int ret);
357 1490791f aliguori
static void qcow_aio_read_bh(void *opaque)
358 1490791f aliguori
{
359 1490791f aliguori
    QCowAIOCB *acb = opaque;
360 1490791f aliguori
    qemu_bh_delete(acb->bh);
361 1490791f aliguori
    acb->bh = NULL;
362 1490791f aliguori
    qcow_aio_read_cb(opaque, 0);
363 1490791f aliguori
}
364 1490791f aliguori
365 a32ef786 aliguori
static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
366 a32ef786 aliguori
{
367 a32ef786 aliguori
    if (acb->bh)
368 a32ef786 aliguori
        return -EIO;
369 a32ef786 aliguori
370 a32ef786 aliguori
    acb->bh = qemu_bh_new(cb, acb);
371 a32ef786 aliguori
    if (!acb->bh)
372 a32ef786 aliguori
        return -EIO;
373 a32ef786 aliguori
374 a32ef786 aliguori
    qemu_bh_schedule(acb->bh);
375 a32ef786 aliguori
376 a32ef786 aliguori
    return 0;
377 a32ef786 aliguori
}
378 a32ef786 aliguori
379 585f8587 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
380 585f8587 bellard
{
381 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
382 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
383 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
384 a9465922 bellard
    int index_in_cluster, n1;
385 585f8587 bellard
386 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
387 f141eafe aliguori
    if (ret < 0)
388 f141eafe aliguori
        goto done;
389 585f8587 bellard
390 585f8587 bellard
    /* post process the read buffer */
391 ce1a14dc pbrook
    if (!acb->cluster_offset) {
392 585f8587 bellard
        /* nothing to do */
393 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
394 585f8587 bellard
        /* nothing to do */
395 585f8587 bellard
    } else {
396 585f8587 bellard
        if (s->crypt_method) {
397 ed6ccf0f Kevin Wolf
            qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
398 5fafdf24 ths
                            acb->n, 0,
399 585f8587 bellard
                            &s->aes_decrypt_key);
400 585f8587 bellard
        }
401 585f8587 bellard
    }
402 585f8587 bellard
403 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
404 ce1a14dc pbrook
    acb->sector_num += acb->n;
405 ce1a14dc pbrook
    acb->buf += acb->n * 512;
406 585f8587 bellard
407 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
408 585f8587 bellard
        /* request completed */
409 f141eafe aliguori
        ret = 0;
410 f141eafe aliguori
        goto done;
411 585f8587 bellard
    }
412 3b46e624 ths
413 585f8587 bellard
    /* prepare next AIO request */
414 095a9c58 aliguori
    acb->n = acb->nb_sectors;
415 ed6ccf0f Kevin Wolf
    acb->cluster_offset =
416 ed6ccf0f Kevin Wolf
        qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
417 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
418 ce1a14dc pbrook
419 ce1a14dc pbrook
    if (!acb->cluster_offset) {
420 585f8587 bellard
        if (bs->backing_hd) {
421 585f8587 bellard
            /* read from the base image */
422 ed6ccf0f Kevin Wolf
            n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num,
423 ce1a14dc pbrook
                               acb->buf, acb->n);
424 a9465922 bellard
            if (n1 > 0) {
425 3f4cb3d3 blueswir1
                acb->hd_iov.iov_base = (void *)acb->buf;
426 c87c0672 aliguori
                acb->hd_iov.iov_len = acb->n * 512;
427 c87c0672 aliguori
                qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
428 c87c0672 aliguori
                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
429 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
430 c87c0672 aliguori
                                    qcow_aio_read_cb, acb);
431 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
432 f141eafe aliguori
                    goto done;
433 a9465922 bellard
            } else {
434 a32ef786 aliguori
                ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
435 a32ef786 aliguori
                if (ret < 0)
436 f141eafe aliguori
                    goto done;
437 a9465922 bellard
            }
438 585f8587 bellard
        } else {
439 585f8587 bellard
            /* Note: in this case, no need to wait */
440 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
441 a32ef786 aliguori
            ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
442 a32ef786 aliguori
            if (ret < 0)
443 f141eafe aliguori
                goto done;
444 585f8587 bellard
        }
445 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
446 585f8587 bellard
        /* add AIO support for compressed blocks ? */
447 ed6ccf0f Kevin Wolf
        if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0)
448 f141eafe aliguori
            goto done;
449 5fafdf24 ths
        memcpy(acb->buf,
450 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 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
    } else {
455 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
456 585f8587 bellard
            ret = -EIO;
457 f141eafe aliguori
            goto done;
458 585f8587 bellard
        }
459 c87c0672 aliguori
460 3f4cb3d3 blueswir1
        acb->hd_iov.iov_base = (void *)acb->buf;
461 c87c0672 aliguori
        acb->hd_iov.iov_len = acb->n * 512;
462 c87c0672 aliguori
        qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
463 c87c0672 aliguori
        acb->hd_aiocb = bdrv_aio_readv(s->hd,
464 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
465 c87c0672 aliguori
                            &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
466 ce1a14dc pbrook
        if (acb->hd_aiocb == NULL)
467 f141eafe aliguori
            goto done;
468 f141eafe aliguori
    }
469 f141eafe aliguori
470 f141eafe aliguori
    return;
471 f141eafe aliguori
done:
472 f141eafe aliguori
    if (acb->qiov->niov > 1) {
473 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
474 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
475 585f8587 bellard
    }
476 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
477 f141eafe aliguori
    qemu_aio_release(acb);
478 585f8587 bellard
}
479 585f8587 bellard
480 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
481 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
482 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque, int is_write)
483 585f8587 bellard
{
484 ce1a14dc pbrook
    QCowAIOCB *acb;
485 ce1a14dc pbrook
486 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
487 ce1a14dc pbrook
    if (!acb)
488 ce1a14dc pbrook
        return NULL;
489 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
490 ce1a14dc pbrook
    acb->sector_num = sector_num;
491 f141eafe aliguori
    acb->qiov = qiov;
492 f141eafe aliguori
    if (qiov->niov > 1) {
493 e268ca52 aliguori
        acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
494 f141eafe aliguori
        if (is_write)
495 f141eafe aliguori
            qemu_iovec_to_buffer(qiov, acb->buf);
496 3f4cb3d3 blueswir1
    } else {
497 3f4cb3d3 blueswir1
        acb->buf = (uint8_t *)qiov->iov->iov_base;
498 3f4cb3d3 blueswir1
    }
499 ce1a14dc pbrook
    acb->nb_sectors = nb_sectors;
500 ce1a14dc pbrook
    acb->n = 0;
501 ce1a14dc pbrook
    acb->cluster_offset = 0;
502 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
503 ce1a14dc pbrook
    return acb;
504 ce1a14dc pbrook
}
505 ce1a14dc pbrook
506 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
507 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
508 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
509 ce1a14dc pbrook
{
510 ce1a14dc pbrook
    QCowAIOCB *acb;
511 ce1a14dc pbrook
512 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
513 ce1a14dc pbrook
    if (!acb)
514 ce1a14dc pbrook
        return NULL;
515 585f8587 bellard
516 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
517 ce1a14dc pbrook
    return &acb->common;
518 585f8587 bellard
}
519 585f8587 bellard
520 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
521 585f8587 bellard
{
522 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
523 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
524 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
525 585f8587 bellard
    int index_in_cluster;
526 585f8587 bellard
    const uint8_t *src_buf;
527 095a9c58 aliguori
    int n_end;
528 ce1a14dc pbrook
529 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
530 ce1a14dc pbrook
531 f141eafe aliguori
    if (ret < 0)
532 f141eafe aliguori
        goto done;
533 585f8587 bellard
534 ed6ccf0f Kevin Wolf
    if (qcow2_alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) {
535 ed6ccf0f Kevin Wolf
        qcow2_free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters);
536 f141eafe aliguori
        goto done;
537 e976c6a1 aliguori
    }
538 e976c6a1 aliguori
539 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
540 ce1a14dc pbrook
    acb->sector_num += acb->n;
541 ce1a14dc pbrook
    acb->buf += acb->n * 512;
542 585f8587 bellard
543 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
544 585f8587 bellard
        /* request completed */
545 f141eafe aliguori
        ret = 0;
546 f141eafe aliguori
        goto done;
547 585f8587 bellard
    }
548 3b46e624 ths
549 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
550 095a9c58 aliguori
    n_end = index_in_cluster + acb->nb_sectors;
551 095a9c58 aliguori
    if (s->crypt_method &&
552 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
553 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
554 095a9c58 aliguori
555 ed6ccf0f Kevin Wolf
    acb->cluster_offset = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
556 05203524 aliguori
                                          index_in_cluster,
557 e976c6a1 aliguori
                                          n_end, &acb->n, &acb->l2meta);
558 e976c6a1 aliguori
    if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
559 585f8587 bellard
        ret = -EIO;
560 f141eafe aliguori
        goto done;
561 585f8587 bellard
    }
562 585f8587 bellard
    if (s->crypt_method) {
563 ce1a14dc pbrook
        if (!acb->cluster_data) {
564 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
565 095a9c58 aliguori
                                             s->cluster_size);
566 585f8587 bellard
        }
567 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
568 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
569 ce1a14dc pbrook
        src_buf = acb->cluster_data;
570 585f8587 bellard
    } else {
571 ce1a14dc pbrook
        src_buf = acb->buf;
572 585f8587 bellard
    }
573 c87c0672 aliguori
    acb->hd_iov.iov_base = (void *)src_buf;
574 c87c0672 aliguori
    acb->hd_iov.iov_len = acb->n * 512;
575 c87c0672 aliguori
    qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
576 c87c0672 aliguori
    acb->hd_aiocb = bdrv_aio_writev(s->hd,
577 c87c0672 aliguori
                                    (acb->cluster_offset >> 9) + index_in_cluster,
578 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
579 c87c0672 aliguori
                                    qcow_aio_write_cb, acb);
580 ce1a14dc pbrook
    if (acb->hd_aiocb == NULL)
581 f141eafe aliguori
        goto done;
582 f141eafe aliguori
583 f141eafe aliguori
    return;
584 f141eafe aliguori
585 f141eafe aliguori
done:
586 f141eafe aliguori
    if (acb->qiov->niov > 1)
587 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
588 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
589 f141eafe aliguori
    qemu_aio_release(acb);
590 585f8587 bellard
}
591 585f8587 bellard
592 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
593 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
594 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
595 585f8587 bellard
{
596 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
597 ce1a14dc pbrook
    QCowAIOCB *acb;
598 3b46e624 ths
599 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
600 585f8587 bellard
601 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
602 ce1a14dc pbrook
    if (!acb)
603 ce1a14dc pbrook
        return NULL;
604 3b46e624 ths
605 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
606 ce1a14dc pbrook
    return &acb->common;
607 585f8587 bellard
}
608 585f8587 bellard
609 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
610 585f8587 bellard
{
611 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
612 585f8587 bellard
    qemu_free(s->l1_table);
613 585f8587 bellard
    qemu_free(s->l2_cache);
614 585f8587 bellard
    qemu_free(s->cluster_cache);
615 585f8587 bellard
    qemu_free(s->cluster_data);
616 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
617 585f8587 bellard
    bdrv_delete(s->hd);
618 585f8587 bellard
}
619 585f8587 bellard
620 73c632ed Kevin Wolf
static int get_bits_from_size(size_t size)
621 73c632ed Kevin Wolf
{
622 73c632ed Kevin Wolf
    int res = 0;
623 73c632ed Kevin Wolf
624 73c632ed Kevin Wolf
    if (size == 0) {
625 73c632ed Kevin Wolf
        return -1;
626 73c632ed Kevin Wolf
    }
627 73c632ed Kevin Wolf
628 73c632ed Kevin Wolf
    while (size != 1) {
629 73c632ed Kevin Wolf
        /* Not a power of two */
630 73c632ed Kevin Wolf
        if (size & 1) {
631 73c632ed Kevin Wolf
            return -1;
632 73c632ed Kevin Wolf
        }
633 73c632ed Kevin Wolf
634 73c632ed Kevin Wolf
        size >>= 1;
635 73c632ed Kevin Wolf
        res++;
636 73c632ed Kevin Wolf
    }
637 73c632ed Kevin Wolf
638 73c632ed Kevin Wolf
    return res;
639 73c632ed Kevin Wolf
}
640 73c632ed Kevin Wolf
641 a35e1c17 Kevin Wolf
642 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
643 a35e1c17 Kevin Wolf
{
644 a35e1c17 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
645 2000cbc5 Blue Swirl
    uint64_t cluster_offset = 0;
646 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
647 a35e1c17 Kevin Wolf
    uint64_t offset;
648 a35e1c17 Kevin Wolf
    int num;
649 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
650 a35e1c17 Kevin Wolf
651 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
652 a35e1c17 Kevin Wolf
    offset = 0;
653 a35e1c17 Kevin Wolf
654 a35e1c17 Kevin Wolf
    while (nb_sectors) {
655 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
656 a35e1c17 Kevin Wolf
        cluster_offset = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
657 a35e1c17 Kevin Wolf
            &meta);
658 a35e1c17 Kevin Wolf
659 a35e1c17 Kevin Wolf
        if (cluster_offset == 0) {
660 a35e1c17 Kevin Wolf
            return -1;
661 a35e1c17 Kevin Wolf
        }
662 a35e1c17 Kevin Wolf
663 a35e1c17 Kevin Wolf
        if (qcow2_alloc_cluster_link_l2(bs, cluster_offset, &meta) < 0) {
664 a35e1c17 Kevin Wolf
            qcow2_free_any_clusters(bs, cluster_offset, meta.nb_clusters);
665 a35e1c17 Kevin Wolf
            return -1;
666 a35e1c17 Kevin Wolf
        }
667 a35e1c17 Kevin Wolf
668 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
669 a35e1c17 Kevin Wolf
670 a35e1c17 Kevin Wolf
        nb_sectors -= num;
671 a35e1c17 Kevin Wolf
        offset += num << 9;
672 a35e1c17 Kevin Wolf
    }
673 a35e1c17 Kevin Wolf
674 a35e1c17 Kevin Wolf
    /*
675 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
676 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
677 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
678 a35e1c17 Kevin Wolf
     */
679 a35e1c17 Kevin Wolf
    if (cluster_offset != 0) {
680 a35e1c17 Kevin Wolf
        bdrv_truncate(s->hd, cluster_offset + (num <<  9));
681 a35e1c17 Kevin Wolf
    }
682 a35e1c17 Kevin Wolf
683 a35e1c17 Kevin Wolf
    return 0;
684 a35e1c17 Kevin Wolf
}
685 a35e1c17 Kevin Wolf
686 f965509c aliguori
static int qcow_create2(const char *filename, int64_t total_size,
687 f965509c aliguori
                        const char *backing_file, const char *backing_format,
688 a35e1c17 Kevin Wolf
                        int flags, size_t cluster_size, int prealloc)
689 585f8587 bellard
{
690 f965509c aliguori
691 585f8587 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
692 2d2431f0 aliguori
    int ref_clusters, backing_format_len = 0;
693 585f8587 bellard
    QCowHeader header;
694 585f8587 bellard
    uint64_t tmp, offset;
695 585f8587 bellard
    QCowCreateState s1, *s = &s1;
696 f965509c aliguori
    QCowExtension ext_bf = {0, 0};
697 f965509c aliguori
698 3b46e624 ths
699 585f8587 bellard
    memset(s, 0, sizeof(*s));
700 585f8587 bellard
701 585f8587 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
702 585f8587 bellard
    if (fd < 0)
703 585f8587 bellard
        return -1;
704 585f8587 bellard
    memset(&header, 0, sizeof(header));
705 585f8587 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
706 585f8587 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
707 585f8587 bellard
    header.size = cpu_to_be64(total_size * 512);
708 585f8587 bellard
    header_size = sizeof(header);
709 585f8587 bellard
    backing_filename_len = 0;
710 585f8587 bellard
    if (backing_file) {
711 f965509c aliguori
        if (backing_format) {
712 f965509c aliguori
            ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
713 f965509c aliguori
            backing_format_len = strlen(backing_format);
714 f965509c aliguori
            ext_bf.len = (backing_format_len + 7) & ~7;
715 f965509c aliguori
            header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7);
716 f965509c aliguori
        }
717 585f8587 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
718 585f8587 bellard
        backing_filename_len = strlen(backing_file);
719 585f8587 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
720 585f8587 bellard
        header_size += backing_filename_len;
721 585f8587 bellard
    }
722 73c632ed Kevin Wolf
723 73c632ed Kevin Wolf
    /* Cluster size */
724 73c632ed Kevin Wolf
    s->cluster_bits = get_bits_from_size(cluster_size);
725 73c632ed Kevin Wolf
    if (s->cluster_bits < MIN_CLUSTER_BITS ||
726 73c632ed Kevin Wolf
        s->cluster_bits > MAX_CLUSTER_BITS)
727 73c632ed Kevin Wolf
    {
728 73c632ed Kevin Wolf
        fprintf(stderr, "Cluster size must be a power of two between "
729 73c632ed Kevin Wolf
            "%d and %dk\n",
730 73c632ed Kevin Wolf
            1 << MIN_CLUSTER_BITS,
731 73c632ed Kevin Wolf
            1 << (MAX_CLUSTER_BITS - 10));
732 73c632ed Kevin Wolf
        return -EINVAL;
733 73c632ed Kevin Wolf
    }
734 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
735 73c632ed Kevin Wolf
736 585f8587 bellard
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
737 585f8587 bellard
    header_size = (header_size + 7) & ~7;
738 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT) {
739 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
740 585f8587 bellard
    } else {
741 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
742 585f8587 bellard
    }
743 585f8587 bellard
    l2_bits = s->cluster_bits - 3;
744 585f8587 bellard
    shift = s->cluster_bits + l2_bits;
745 585f8587 bellard
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
746 585f8587 bellard
    offset = align_offset(header_size, s->cluster_size);
747 585f8587 bellard
    s->l1_table_offset = offset;
748 585f8587 bellard
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
749 585f8587 bellard
    header.l1_size = cpu_to_be32(l1_size);
750 15e6690a bellard
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
751 585f8587 bellard
752 585f8587 bellard
    s->refcount_table = qemu_mallocz(s->cluster_size);
753 3b46e624 ths
754 585f8587 bellard
    s->refcount_table_offset = offset;
755 585f8587 bellard
    header.refcount_table_offset = cpu_to_be64(offset);
756 585f8587 bellard
    header.refcount_table_clusters = cpu_to_be32(1);
757 585f8587 bellard
    offset += s->cluster_size;
758 585f8587 bellard
    s->refcount_block_offset = offset;
759 2d2431f0 aliguori
760 2d2431f0 aliguori
    /* count how many refcount blocks needed */
761 2d2431f0 aliguori
    tmp = offset >> s->cluster_bits;
762 2d2431f0 aliguori
    ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
763 2d2431f0 aliguori
    for (i=0; i < ref_clusters; i++) {
764 2d2431f0 aliguori
        s->refcount_table[i] = cpu_to_be64(offset);
765 2d2431f0 aliguori
        offset += s->cluster_size;
766 2d2431f0 aliguori
    }
767 2d2431f0 aliguori
768 2d2431f0 aliguori
    s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
769 585f8587 bellard
770 585f8587 bellard
    /* update refcounts */
771 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, 0, header_size);
772 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->l1_table_offset,
773 ed6ccf0f Kevin Wolf
        l1_size * sizeof(uint64_t));
774 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
775 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_block_offset,
776 ed6ccf0f Kevin Wolf
        ref_clusters * s->cluster_size);
777 3b46e624 ths
778 585f8587 bellard
    /* write all the data */
779 585f8587 bellard
    write(fd, &header, sizeof(header));
780 585f8587 bellard
    if (backing_file) {
781 f965509c aliguori
        if (backing_format_len) {
782 f965509c aliguori
            char zero[16];
783 f965509c aliguori
            int d = ext_bf.len - backing_format_len;
784 f965509c aliguori
785 f965509c aliguori
            memset(zero, 0, sizeof(zero));
786 f965509c aliguori
            cpu_to_be32s(&ext_bf.magic);
787 f965509c aliguori
            cpu_to_be32s(&ext_bf.len);
788 f965509c aliguori
            write(fd, &ext_bf, sizeof(ext_bf));
789 f965509c aliguori
            write(fd, backing_format, backing_format_len);
790 f965509c aliguori
            if (d>0) {
791 f965509c aliguori
                write(fd, zero, d);
792 f965509c aliguori
            }
793 f965509c aliguori
        }
794 585f8587 bellard
        write(fd, backing_file, backing_filename_len);
795 585f8587 bellard
    }
796 585f8587 bellard
    lseek(fd, s->l1_table_offset, SEEK_SET);
797 585f8587 bellard
    tmp = 0;
798 585f8587 bellard
    for(i = 0;i < l1_size; i++) {
799 585f8587 bellard
        write(fd, &tmp, sizeof(tmp));
800 585f8587 bellard
    }
801 585f8587 bellard
    lseek(fd, s->refcount_table_offset, SEEK_SET);
802 585f8587 bellard
    write(fd, s->refcount_table, s->cluster_size);
803 3b46e624 ths
804 585f8587 bellard
    lseek(fd, s->refcount_block_offset, SEEK_SET);
805 2d2431f0 aliguori
    write(fd, s->refcount_block, ref_clusters * s->cluster_size);
806 585f8587 bellard
807 585f8587 bellard
    qemu_free(s->refcount_table);
808 585f8587 bellard
    qemu_free(s->refcount_block);
809 585f8587 bellard
    close(fd);
810 a35e1c17 Kevin Wolf
811 a35e1c17 Kevin Wolf
    /* Preallocate metadata */
812 a35e1c17 Kevin Wolf
    if (prealloc) {
813 a35e1c17 Kevin Wolf
        BlockDriverState *bs;
814 a35e1c17 Kevin Wolf
        bs = bdrv_new("");
815 a35e1c17 Kevin Wolf
        bdrv_open(bs, filename, BDRV_O_CACHE_WB);
816 a35e1c17 Kevin Wolf
        preallocate(bs);
817 a35e1c17 Kevin Wolf
        bdrv_close(bs);
818 a35e1c17 Kevin Wolf
    }
819 a35e1c17 Kevin Wolf
820 585f8587 bellard
    return 0;
821 585f8587 bellard
}
822 585f8587 bellard
823 0e7e1989 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
824 0e7e1989 Kevin Wolf
{
825 0e7e1989 Kevin Wolf
    const char *backing_file = NULL;
826 0e7e1989 Kevin Wolf
    const char *backing_fmt = NULL;
827 0e7e1989 Kevin Wolf
    uint64_t sectors = 0;
828 0e7e1989 Kevin Wolf
    int flags = 0;
829 9ccb258e Kevin Wolf
    size_t cluster_size = 65536;
830 a35e1c17 Kevin Wolf
    int prealloc = 0;
831 0e7e1989 Kevin Wolf
832 0e7e1989 Kevin Wolf
    /* Read out options */
833 0e7e1989 Kevin Wolf
    while (options && options->name) {
834 0e7e1989 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
835 0e7e1989 Kevin Wolf
            sectors = options->value.n / 512;
836 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
837 0e7e1989 Kevin Wolf
            backing_file = options->value.s;
838 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
839 0e7e1989 Kevin Wolf
            backing_fmt = options->value.s;
840 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
841 0e7e1989 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
842 73c632ed Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
843 73c632ed Kevin Wolf
            if (options->value.n) {
844 73c632ed Kevin Wolf
                cluster_size = options->value.n;
845 73c632ed Kevin Wolf
            }
846 a35e1c17 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
847 a35e1c17 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
848 a35e1c17 Kevin Wolf
                prealloc = 0;
849 a35e1c17 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
850 a35e1c17 Kevin Wolf
                prealloc = 1;
851 a35e1c17 Kevin Wolf
            } else {
852 a35e1c17 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
853 a35e1c17 Kevin Wolf
                    options->value.s);
854 a35e1c17 Kevin Wolf
                return -EINVAL;
855 a35e1c17 Kevin Wolf
            }
856 0e7e1989 Kevin Wolf
        }
857 0e7e1989 Kevin Wolf
        options++;
858 0e7e1989 Kevin Wolf
    }
859 0e7e1989 Kevin Wolf
860 a35e1c17 Kevin Wolf
    if (backing_file && prealloc) {
861 a35e1c17 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
862 a35e1c17 Kevin Wolf
            "the same time\n");
863 a35e1c17 Kevin Wolf
        return -EINVAL;
864 a35e1c17 Kevin Wolf
    }
865 a35e1c17 Kevin Wolf
866 73c632ed Kevin Wolf
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
867 a35e1c17 Kevin Wolf
        cluster_size, prealloc);
868 f965509c aliguori
}
869 f965509c aliguori
870 585f8587 bellard
static int qcow_make_empty(BlockDriverState *bs)
871 585f8587 bellard
{
872 585f8587 bellard
#if 0
873 585f8587 bellard
    /* XXX: not correct */
874 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
875 585f8587 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
876 585f8587 bellard
    int ret;
877 585f8587 bellard

878 585f8587 bellard
    memset(s->l1_table, 0, l1_length);
879 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
880 ac674887 aliguori
        return -1;
881 585f8587 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
882 585f8587 bellard
    if (ret < 0)
883 585f8587 bellard
        return ret;
884 3b46e624 ths

885 585f8587 bellard
    l2_cache_reset(bs);
886 585f8587 bellard
#endif
887 585f8587 bellard
    return 0;
888 585f8587 bellard
}
889 585f8587 bellard
890 585f8587 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
891 585f8587 bellard
   tables to avoid losing bytes in alignment */
892 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
893 585f8587 bellard
                                 const uint8_t *buf, int nb_sectors)
894 585f8587 bellard
{
895 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
896 585f8587 bellard
    z_stream strm;
897 585f8587 bellard
    int ret, out_len;
898 585f8587 bellard
    uint8_t *out_buf;
899 585f8587 bellard
    uint64_t cluster_offset;
900 585f8587 bellard
901 585f8587 bellard
    if (nb_sectors == 0) {
902 585f8587 bellard
        /* align end of file to a sector boundary to ease reading with
903 585f8587 bellard
           sector based I/Os */
904 585f8587 bellard
        cluster_offset = bdrv_getlength(s->hd);
905 585f8587 bellard
        cluster_offset = (cluster_offset + 511) & ~511;
906 585f8587 bellard
        bdrv_truncate(s->hd, cluster_offset);
907 585f8587 bellard
        return 0;
908 585f8587 bellard
    }
909 585f8587 bellard
910 585f8587 bellard
    if (nb_sectors != s->cluster_sectors)
911 585f8587 bellard
        return -EINVAL;
912 585f8587 bellard
913 585f8587 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
914 585f8587 bellard
915 585f8587 bellard
    /* best compression, small window, no zlib header */
916 585f8587 bellard
    memset(&strm, 0, sizeof(strm));
917 585f8587 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
918 5fafdf24 ths
                       Z_DEFLATED, -12,
919 585f8587 bellard
                       9, Z_DEFAULT_STRATEGY);
920 585f8587 bellard
    if (ret != 0) {
921 585f8587 bellard
        qemu_free(out_buf);
922 585f8587 bellard
        return -1;
923 585f8587 bellard
    }
924 585f8587 bellard
925 585f8587 bellard
    strm.avail_in = s->cluster_size;
926 585f8587 bellard
    strm.next_in = (uint8_t *)buf;
927 585f8587 bellard
    strm.avail_out = s->cluster_size;
928 585f8587 bellard
    strm.next_out = out_buf;
929 585f8587 bellard
930 585f8587 bellard
    ret = deflate(&strm, Z_FINISH);
931 585f8587 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
932 585f8587 bellard
        qemu_free(out_buf);
933 585f8587 bellard
        deflateEnd(&strm);
934 585f8587 bellard
        return -1;
935 585f8587 bellard
    }
936 585f8587 bellard
    out_len = strm.next_out - out_buf;
937 585f8587 bellard
938 585f8587 bellard
    deflateEnd(&strm);
939 585f8587 bellard
940 585f8587 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
941 585f8587 bellard
        /* could not compress: write normal cluster */
942 ade40677 Kevin Wolf
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
943 585f8587 bellard
    } else {
944 ed6ccf0f Kevin Wolf
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
945 ed6ccf0f Kevin Wolf
            sector_num << 9, out_len);
946 52d893ec aliguori
        if (!cluster_offset)
947 52d893ec aliguori
            return -1;
948 585f8587 bellard
        cluster_offset &= s->cluster_offset_mask;
949 585f8587 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
950 585f8587 bellard
            qemu_free(out_buf);
951 585f8587 bellard
            return -1;
952 585f8587 bellard
        }
953 585f8587 bellard
    }
954 3b46e624 ths
955 585f8587 bellard
    qemu_free(out_buf);
956 585f8587 bellard
    return 0;
957 585f8587 bellard
}
958 585f8587 bellard
959 585f8587 bellard
static void qcow_flush(BlockDriverState *bs)
960 585f8587 bellard
{
961 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
962 585f8587 bellard
    bdrv_flush(s->hd);
963 585f8587 bellard
}
964 585f8587 bellard
965 45566e9c Christoph Hellwig
static int64_t qcow_vm_state_offset(BDRVQcowState *s)
966 45566e9c Christoph Hellwig
{
967 45566e9c Christoph Hellwig
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
968 45566e9c Christoph Hellwig
}
969 45566e9c Christoph Hellwig
970 585f8587 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
971 585f8587 bellard
{
972 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
973 585f8587 bellard
    bdi->cluster_size = s->cluster_size;
974 45566e9c Christoph Hellwig
    bdi->vm_state_offset = qcow_vm_state_offset(s);
975 585f8587 bellard
    return 0;
976 585f8587 bellard
}
977 585f8587 bellard
978 585f8587 bellard
979 e97fc193 aliguori
static int qcow_check(BlockDriverState *bs)
980 e97fc193 aliguori
{
981 ed6ccf0f Kevin Wolf
    return qcow2_check_refcounts(bs);
982 585f8587 bellard
}
983 585f8587 bellard
984 585f8587 bellard
#if 0
985 585f8587 bellard
static void dump_refcounts(BlockDriverState *bs)
986 585f8587 bellard
{
987 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
988 585f8587 bellard
    int64_t nb_clusters, k, k1, size;
989 585f8587 bellard
    int refcount;
990 585f8587 bellard

991 585f8587 bellard
    size = bdrv_getlength(s->hd);
992 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
993 585f8587 bellard
    for(k = 0; k < nb_clusters;) {
994 585f8587 bellard
        k1 = k;
995 585f8587 bellard
        refcount = get_refcount(bs, k);
996 585f8587 bellard
        k++;
997 585f8587 bellard
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
998 585f8587 bellard
            k++;
999 585f8587 bellard
        printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
1000 585f8587 bellard
    }
1001 585f8587 bellard
}
1002 585f8587 bellard
#endif
1003 585f8587 bellard
1004 45566e9c Christoph Hellwig
static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1005 178e08a5 aliguori
                           int64_t pos, int size)
1006 178e08a5 aliguori
{
1007 45566e9c Christoph Hellwig
    BDRVQcowState *s = bs->opaque;
1008 178e08a5 aliguori
    int growable = bs->growable;
1009 178e08a5 aliguori
1010 178e08a5 aliguori
    bs->growable = 1;
1011 45566e9c Christoph Hellwig
    bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
1012 178e08a5 aliguori
    bs->growable = growable;
1013 178e08a5 aliguori
1014 178e08a5 aliguori
    return size;
1015 178e08a5 aliguori
}
1016 178e08a5 aliguori
1017 45566e9c Christoph Hellwig
static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1018 178e08a5 aliguori
                           int64_t pos, int size)
1019 178e08a5 aliguori
{
1020 45566e9c Christoph Hellwig
    BDRVQcowState *s = bs->opaque;
1021 178e08a5 aliguori
    int growable = bs->growable;
1022 178e08a5 aliguori
    int ret;
1023 178e08a5 aliguori
1024 178e08a5 aliguori
    bs->growable = 1;
1025 45566e9c Christoph Hellwig
    ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
1026 178e08a5 aliguori
    bs->growable = growable;
1027 178e08a5 aliguori
1028 178e08a5 aliguori
    return ret;
1029 178e08a5 aliguori
}
1030 178e08a5 aliguori
1031 0e7e1989 Kevin Wolf
static QEMUOptionParameter qcow_create_options[] = {
1032 db08adf5 Kevin Wolf
    {
1033 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_SIZE,
1034 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
1035 db08adf5 Kevin Wolf
        .help = "Virtual disk size"
1036 db08adf5 Kevin Wolf
    },
1037 db08adf5 Kevin Wolf
    {
1038 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FILE,
1039 db08adf5 Kevin Wolf
        .type = OPT_STRING,
1040 db08adf5 Kevin Wolf
        .help = "File name of a base image"
1041 db08adf5 Kevin Wolf
    },
1042 db08adf5 Kevin Wolf
    {
1043 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FMT,
1044 db08adf5 Kevin Wolf
        .type = OPT_STRING,
1045 db08adf5 Kevin Wolf
        .help = "Image format of the base image"
1046 db08adf5 Kevin Wolf
    },
1047 db08adf5 Kevin Wolf
    {
1048 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_ENCRYPT,
1049 db08adf5 Kevin Wolf
        .type = OPT_FLAG,
1050 db08adf5 Kevin Wolf
        .help = "Encrypt the image"
1051 db08adf5 Kevin Wolf
    },
1052 db08adf5 Kevin Wolf
    {
1053 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_CLUSTER_SIZE,
1054 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
1055 db08adf5 Kevin Wolf
        .help = "qcow2 cluster size"
1056 db08adf5 Kevin Wolf
    },
1057 a35e1c17 Kevin Wolf
    {
1058 a35e1c17 Kevin Wolf
        .name = BLOCK_OPT_PREALLOC,
1059 a35e1c17 Kevin Wolf
        .type = OPT_STRING,
1060 a35e1c17 Kevin Wolf
        .help = "Preallocation mode (allowed values: off, metadata)"
1061 a35e1c17 Kevin Wolf
    },
1062 0e7e1989 Kevin Wolf
    { NULL }
1063 0e7e1989 Kevin Wolf
};
1064 0e7e1989 Kevin Wolf
1065 5efa9d5a Anthony Liguori
static BlockDriver bdrv_qcow2 = {
1066 e60f469c aurel32
    .format_name        = "qcow2",
1067 e60f469c aurel32
    .instance_size        = sizeof(BDRVQcowState),
1068 e60f469c aurel32
    .bdrv_probe                = qcow_probe,
1069 e60f469c aurel32
    .bdrv_open                = qcow_open,
1070 e60f469c aurel32
    .bdrv_close                = qcow_close,
1071 e60f469c aurel32
    .bdrv_create        = qcow_create,
1072 e60f469c aurel32
    .bdrv_flush                = qcow_flush,
1073 e60f469c aurel32
    .bdrv_is_allocated        = qcow_is_allocated,
1074 e60f469c aurel32
    .bdrv_set_key        = qcow_set_key,
1075 e60f469c aurel32
    .bdrv_make_empty        = qcow_make_empty,
1076 e60f469c aurel32
1077 f141eafe aliguori
    .bdrv_aio_readv        = qcow_aio_readv,
1078 f141eafe aliguori
    .bdrv_aio_writev        = qcow_aio_writev,
1079 585f8587 bellard
    .bdrv_write_compressed = qcow_write_compressed,
1080 585f8587 bellard
1081 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1082 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1083 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1084 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1085 e60f469c aurel32
    .bdrv_get_info        = qcow_get_info,
1086 f965509c aliguori
1087 45566e9c Christoph Hellwig
    .bdrv_save_vmstate    = qcow_save_vmstate,
1088 45566e9c Christoph Hellwig
    .bdrv_load_vmstate    = qcow_load_vmstate,
1089 178e08a5 aliguori
1090 0e7e1989 Kevin Wolf
    .create_options = qcow_create_options,
1091 e97fc193 aliguori
    .bdrv_check = qcow_check,
1092 585f8587 bellard
};
1093 5efa9d5a Anthony Liguori
1094 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1095 5efa9d5a Anthony Liguori
{
1096 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1097 5efa9d5a Anthony Liguori
}
1098 5efa9d5a Anthony Liguori
1099 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);