Statistics
| Branch: | Revision:

root / block / qcow2.c @ 1d36e3aa

History | View | Annotate | Download (37.1 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 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
128 f965509c aliguori
            break;
129 f965509c aliguori
130 9b80ddf3 aliguori
        default:
131 9b80ddf3 aliguori
            /* unknown magic -- just skip it */
132 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
133 9b80ddf3 aliguori
            break;
134 9b80ddf3 aliguori
        }
135 9b80ddf3 aliguori
    }
136 9b80ddf3 aliguori
137 9b80ddf3 aliguori
    return 0;
138 9b80ddf3 aliguori
}
139 9b80ddf3 aliguori
140 9b80ddf3 aliguori
141 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 d191d12d Stefan Weil
    if (header.cluster_bits < MIN_CLUSTER_BITS ||
170 73c632ed Kevin Wolf
        header.cluster_bits > MAX_CLUSTER_BITS)
171 585f8587 bellard
        goto fail;
172 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
173 585f8587 bellard
        goto fail;
174 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
175 585f8587 bellard
    if (s->crypt_method_header)
176 585f8587 bellard
        bs->encrypted = 1;
177 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
178 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
179 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
180 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
181 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
182 585f8587 bellard
    bs->total_sectors = header.size / 512;
183 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
184 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
185 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
186 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
187 5fafdf24 ths
    s->refcount_table_size =
188 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
189 585f8587 bellard
190 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
191 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
192 585f8587 bellard
193 585f8587 bellard
    /* read the level 1 table */
194 585f8587 bellard
    s->l1_size = header.l1_size;
195 585f8587 bellard
    shift = s->cluster_bits + s->l2_bits;
196 585f8587 bellard
    s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
197 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
198 585f8587 bellard
       header.size bytes */
199 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
200 585f8587 bellard
        goto fail;
201 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
202 d191d12d Stefan Weil
    if (s->l1_size > 0) {
203 d191d12d Stefan Weil
        s->l1_table = qemu_mallocz(
204 d191d12d Stefan Weil
            align_offset(s->l1_size * sizeof(uint64_t), 512));
205 d191d12d Stefan Weil
        if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
206 d191d12d Stefan Weil
            s->l1_size * sizeof(uint64_t))
207 d191d12d Stefan Weil
            goto fail;
208 d191d12d Stefan Weil
        for(i = 0;i < s->l1_size; i++) {
209 d191d12d Stefan Weil
            be64_to_cpus(&s->l1_table[i]);
210 d191d12d Stefan Weil
        }
211 585f8587 bellard
    }
212 585f8587 bellard
    /* alloc L2 cache */
213 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
214 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
215 585f8587 bellard
    /* one more sector for decompressed data alignment */
216 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
217 095a9c58 aliguori
                                  + 512);
218 585f8587 bellard
    s->cluster_cache_offset = -1;
219 3b46e624 ths
220 ed6ccf0f Kevin Wolf
    if (qcow2_refcount_init(bs) < 0)
221 585f8587 bellard
        goto fail;
222 585f8587 bellard
223 72cf2d4f Blue Swirl
    QLIST_INIT(&s->cluster_allocs);
224 f214978a Kevin Wolf
225 9b80ddf3 aliguori
    /* read qcow2 extensions */
226 9b80ddf3 aliguori
    if (header.backing_file_offset)
227 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
228 9b80ddf3 aliguori
    else
229 9b80ddf3 aliguori
        ext_end = s->cluster_size;
230 9b80ddf3 aliguori
    if (qcow_read_extensions(bs, sizeof(header), ext_end))
231 9b80ddf3 aliguori
        goto fail;
232 9b80ddf3 aliguori
233 585f8587 bellard
    /* read the backing file name */
234 585f8587 bellard
    if (header.backing_file_offset != 0) {
235 585f8587 bellard
        len = header.backing_file_size;
236 585f8587 bellard
        if (len > 1023)
237 585f8587 bellard
            len = 1023;
238 585f8587 bellard
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
239 585f8587 bellard
            goto fail;
240 585f8587 bellard
        bs->backing_file[len] = '\0';
241 585f8587 bellard
    }
242 ed6ccf0f Kevin Wolf
    if (qcow2_read_snapshots(bs) < 0)
243 585f8587 bellard
        goto fail;
244 585f8587 bellard
245 585f8587 bellard
#ifdef DEBUG_ALLOC
246 14899cdf Filip Navara
    qcow2_check_refcounts(bs);
247 585f8587 bellard
#endif
248 585f8587 bellard
    return 0;
249 585f8587 bellard
250 585f8587 bellard
 fail:
251 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
252 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
253 585f8587 bellard
    qemu_free(s->l1_table);
254 585f8587 bellard
    qemu_free(s->l2_cache);
255 585f8587 bellard
    qemu_free(s->cluster_cache);
256 585f8587 bellard
    qemu_free(s->cluster_data);
257 585f8587 bellard
    bdrv_delete(s->hd);
258 585f8587 bellard
    return -1;
259 585f8587 bellard
}
260 585f8587 bellard
261 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
262 585f8587 bellard
{
263 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
264 585f8587 bellard
    uint8_t keybuf[16];
265 585f8587 bellard
    int len, i;
266 3b46e624 ths
267 585f8587 bellard
    memset(keybuf, 0, 16);
268 585f8587 bellard
    len = strlen(key);
269 585f8587 bellard
    if (len > 16)
270 585f8587 bellard
        len = 16;
271 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
272 585f8587 bellard
       entropy */
273 585f8587 bellard
    for(i = 0;i < len;i++) {
274 585f8587 bellard
        keybuf[i] = key[i];
275 585f8587 bellard
    }
276 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
277 585f8587 bellard
278 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
279 585f8587 bellard
        return -1;
280 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
281 585f8587 bellard
        return -1;
282 585f8587 bellard
#if 0
283 585f8587 bellard
    /* test */
284 585f8587 bellard
    {
285 585f8587 bellard
        uint8_t in[16];
286 585f8587 bellard
        uint8_t out[16];
287 585f8587 bellard
        uint8_t tmp[16];
288 585f8587 bellard
        for(i=0;i<16;i++)
289 585f8587 bellard
            in[i] = i;
290 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
291 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
292 585f8587 bellard
        for(i = 0; i < 16; i++)
293 585f8587 bellard
            printf(" %02x", tmp[i]);
294 585f8587 bellard
        printf("\n");
295 585f8587 bellard
        for(i = 0; i < 16; i++)
296 585f8587 bellard
            printf(" %02x", out[i]);
297 585f8587 bellard
        printf("\n");
298 585f8587 bellard
    }
299 585f8587 bellard
#endif
300 585f8587 bellard
    return 0;
301 585f8587 bellard
}
302 585f8587 bellard
303 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
304 585f8587 bellard
                             int nb_sectors, int *pnum)
305 585f8587 bellard
{
306 585f8587 bellard
    uint64_t cluster_offset;
307 585f8587 bellard
308 095a9c58 aliguori
    *pnum = nb_sectors;
309 ed6ccf0f Kevin Wolf
    cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum);
310 095a9c58 aliguori
311 585f8587 bellard
    return (cluster_offset != 0);
312 585f8587 bellard
}
313 585f8587 bellard
314 a9465922 bellard
/* handle reading after the end of the backing file */
315 ed6ccf0f Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs,
316 45aba42f Kevin Wolf
                  int64_t sector_num, uint8_t *buf, int nb_sectors)
317 a9465922 bellard
{
318 a9465922 bellard
    int n1;
319 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
320 a9465922 bellard
        return nb_sectors;
321 a9465922 bellard
    if (sector_num >= bs->total_sectors)
322 a9465922 bellard
        n1 = 0;
323 a9465922 bellard
    else
324 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
325 a9465922 bellard
    memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
326 a9465922 bellard
    return n1;
327 a9465922 bellard
}
328 a9465922 bellard
329 ce1a14dc pbrook
typedef struct QCowAIOCB {
330 ce1a14dc pbrook
    BlockDriverAIOCB common;
331 585f8587 bellard
    int64_t sector_num;
332 f141eafe aliguori
    QEMUIOVector *qiov;
333 585f8587 bellard
    uint8_t *buf;
334 f141eafe aliguori
    void *orig_buf;
335 585f8587 bellard
    int nb_sectors;
336 585f8587 bellard
    int n;
337 585f8587 bellard
    uint64_t cluster_offset;
338 5fafdf24 ths
    uint8_t *cluster_data;
339 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
340 c87c0672 aliguori
    struct iovec hd_iov;
341 c87c0672 aliguori
    QEMUIOVector hd_qiov;
342 1490791f aliguori
    QEMUBH *bh;
343 e976c6a1 aliguori
    QCowL2Meta l2meta;
344 72cf2d4f Blue Swirl
    QLIST_ENTRY(QCowAIOCB) next_depend;
345 585f8587 bellard
} QCowAIOCB;
346 585f8587 bellard
347 c16b5a2c Christoph Hellwig
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
348 c16b5a2c Christoph Hellwig
{
349 c16b5a2c Christoph Hellwig
    QCowAIOCB *acb = (QCowAIOCB *)blockacb;
350 c16b5a2c Christoph Hellwig
    if (acb->hd_aiocb)
351 c16b5a2c Christoph Hellwig
        bdrv_aio_cancel(acb->hd_aiocb);
352 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
353 c16b5a2c Christoph Hellwig
}
354 c16b5a2c Christoph Hellwig
355 c16b5a2c Christoph Hellwig
static AIOPool qcow_aio_pool = {
356 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(QCowAIOCB),
357 c16b5a2c Christoph Hellwig
    .cancel             = qcow_aio_cancel,
358 c16b5a2c Christoph Hellwig
};
359 c16b5a2c Christoph Hellwig
360 1490791f aliguori
static void qcow_aio_read_cb(void *opaque, int ret);
361 1490791f aliguori
static void qcow_aio_read_bh(void *opaque)
362 1490791f aliguori
{
363 1490791f aliguori
    QCowAIOCB *acb = opaque;
364 1490791f aliguori
    qemu_bh_delete(acb->bh);
365 1490791f aliguori
    acb->bh = NULL;
366 1490791f aliguori
    qcow_aio_read_cb(opaque, 0);
367 1490791f aliguori
}
368 1490791f aliguori
369 a32ef786 aliguori
static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb)
370 a32ef786 aliguori
{
371 a32ef786 aliguori
    if (acb->bh)
372 a32ef786 aliguori
        return -EIO;
373 a32ef786 aliguori
374 a32ef786 aliguori
    acb->bh = qemu_bh_new(cb, acb);
375 a32ef786 aliguori
    if (!acb->bh)
376 a32ef786 aliguori
        return -EIO;
377 a32ef786 aliguori
378 a32ef786 aliguori
    qemu_bh_schedule(acb->bh);
379 a32ef786 aliguori
380 a32ef786 aliguori
    return 0;
381 a32ef786 aliguori
}
382 a32ef786 aliguori
383 585f8587 bellard
static void qcow_aio_read_cb(void *opaque, int ret)
384 585f8587 bellard
{
385 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
386 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
387 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
388 a9465922 bellard
    int index_in_cluster, n1;
389 585f8587 bellard
390 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
391 f141eafe aliguori
    if (ret < 0)
392 f141eafe aliguori
        goto done;
393 585f8587 bellard
394 585f8587 bellard
    /* post process the read buffer */
395 ce1a14dc pbrook
    if (!acb->cluster_offset) {
396 585f8587 bellard
        /* nothing to do */
397 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
398 585f8587 bellard
        /* nothing to do */
399 585f8587 bellard
    } else {
400 585f8587 bellard
        if (s->crypt_method) {
401 ed6ccf0f Kevin Wolf
            qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
402 5fafdf24 ths
                            acb->n, 0,
403 585f8587 bellard
                            &s->aes_decrypt_key);
404 585f8587 bellard
        }
405 585f8587 bellard
    }
406 585f8587 bellard
407 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
408 ce1a14dc pbrook
    acb->sector_num += acb->n;
409 ce1a14dc pbrook
    acb->buf += acb->n * 512;
410 585f8587 bellard
411 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
412 585f8587 bellard
        /* request completed */
413 f141eafe aliguori
        ret = 0;
414 f141eafe aliguori
        goto done;
415 585f8587 bellard
    }
416 3b46e624 ths
417 585f8587 bellard
    /* prepare next AIO request */
418 095a9c58 aliguori
    acb->n = acb->nb_sectors;
419 ed6ccf0f Kevin Wolf
    acb->cluster_offset =
420 ed6ccf0f Kevin Wolf
        qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
421 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
422 ce1a14dc pbrook
423 ce1a14dc pbrook
    if (!acb->cluster_offset) {
424 585f8587 bellard
        if (bs->backing_hd) {
425 585f8587 bellard
            /* read from the base image */
426 ed6ccf0f Kevin Wolf
            n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num,
427 ce1a14dc pbrook
                               acb->buf, acb->n);
428 a9465922 bellard
            if (n1 > 0) {
429 3f4cb3d3 blueswir1
                acb->hd_iov.iov_base = (void *)acb->buf;
430 c87c0672 aliguori
                acb->hd_iov.iov_len = acb->n * 512;
431 c87c0672 aliguori
                qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
432 c87c0672 aliguori
                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
433 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
434 c87c0672 aliguori
                                    qcow_aio_read_cb, acb);
435 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
436 f141eafe aliguori
                    goto done;
437 a9465922 bellard
            } else {
438 a32ef786 aliguori
                ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
439 a32ef786 aliguori
                if (ret < 0)
440 f141eafe aliguori
                    goto done;
441 a9465922 bellard
            }
442 585f8587 bellard
        } else {
443 585f8587 bellard
            /* Note: in this case, no need to wait */
444 ce1a14dc pbrook
            memset(acb->buf, 0, 512 * acb->n);
445 a32ef786 aliguori
            ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
446 a32ef786 aliguori
            if (ret < 0)
447 f141eafe aliguori
                goto done;
448 585f8587 bellard
        }
449 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
450 585f8587 bellard
        /* add AIO support for compressed blocks ? */
451 ed6ccf0f Kevin Wolf
        if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0)
452 f141eafe aliguori
            goto done;
453 5fafdf24 ths
        memcpy(acb->buf,
454 ce1a14dc pbrook
               s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
455 a32ef786 aliguori
        ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
456 a32ef786 aliguori
        if (ret < 0)
457 f141eafe aliguori
            goto done;
458 585f8587 bellard
    } else {
459 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
460 585f8587 bellard
            ret = -EIO;
461 f141eafe aliguori
            goto done;
462 585f8587 bellard
        }
463 c87c0672 aliguori
464 3f4cb3d3 blueswir1
        acb->hd_iov.iov_base = (void *)acb->buf;
465 c87c0672 aliguori
        acb->hd_iov.iov_len = acb->n * 512;
466 c87c0672 aliguori
        qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
467 c87c0672 aliguori
        acb->hd_aiocb = bdrv_aio_readv(s->hd,
468 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
469 c87c0672 aliguori
                            &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
470 ce1a14dc pbrook
        if (acb->hd_aiocb == NULL)
471 f141eafe aliguori
            goto done;
472 f141eafe aliguori
    }
473 f141eafe aliguori
474 f141eafe aliguori
    return;
475 f141eafe aliguori
done:
476 f141eafe aliguori
    if (acb->qiov->niov > 1) {
477 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
478 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
479 585f8587 bellard
    }
480 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
481 f141eafe aliguori
    qemu_aio_release(acb);
482 585f8587 bellard
}
483 585f8587 bellard
484 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
485 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
486 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque, int is_write)
487 585f8587 bellard
{
488 ce1a14dc pbrook
    QCowAIOCB *acb;
489 ce1a14dc pbrook
490 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
491 ce1a14dc pbrook
    if (!acb)
492 ce1a14dc pbrook
        return NULL;
493 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
494 ce1a14dc pbrook
    acb->sector_num = sector_num;
495 f141eafe aliguori
    acb->qiov = qiov;
496 f141eafe aliguori
    if (qiov->niov > 1) {
497 e268ca52 aliguori
        acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size);
498 f141eafe aliguori
        if (is_write)
499 f141eafe aliguori
            qemu_iovec_to_buffer(qiov, acb->buf);
500 3f4cb3d3 blueswir1
    } else {
501 3f4cb3d3 blueswir1
        acb->buf = (uint8_t *)qiov->iov->iov_base;
502 3f4cb3d3 blueswir1
    }
503 ce1a14dc pbrook
    acb->nb_sectors = nb_sectors;
504 ce1a14dc pbrook
    acb->n = 0;
505 ce1a14dc pbrook
    acb->cluster_offset = 0;
506 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
507 72cf2d4f Blue Swirl
    QLIST_INIT(&acb->l2meta.dependent_requests);
508 ce1a14dc pbrook
    return acb;
509 ce1a14dc pbrook
}
510 ce1a14dc pbrook
511 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
512 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
513 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
514 ce1a14dc pbrook
{
515 ce1a14dc pbrook
    QCowAIOCB *acb;
516 ce1a14dc pbrook
517 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
518 ce1a14dc pbrook
    if (!acb)
519 ce1a14dc pbrook
        return NULL;
520 585f8587 bellard
521 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
522 ce1a14dc pbrook
    return &acb->common;
523 585f8587 bellard
}
524 585f8587 bellard
525 f214978a Kevin Wolf
static void qcow_aio_write_cb(void *opaque, int ret);
526 f214978a Kevin Wolf
527 f214978a Kevin Wolf
static void run_dependent_requests(QCowL2Meta *m)
528 f214978a Kevin Wolf
{
529 f214978a Kevin Wolf
    QCowAIOCB *req;
530 f214978a Kevin Wolf
    QCowAIOCB *next;
531 f214978a Kevin Wolf
532 f214978a Kevin Wolf
    /* Take the request off the list of running requests */
533 f214978a Kevin Wolf
    if (m->nb_clusters != 0) {
534 72cf2d4f Blue Swirl
        QLIST_REMOVE(m, next_in_flight);
535 f214978a Kevin Wolf
    }
536 f214978a Kevin Wolf
537 f214978a Kevin Wolf
    /*
538 f214978a Kevin Wolf
     * Restart all dependent requests.
539 72cf2d4f Blue Swirl
     * Can't use QLIST_FOREACH here - the next link might not be the same
540 f214978a Kevin Wolf
     * any more after the callback  (request could depend on a different
541 f214978a Kevin Wolf
     * request now)
542 f214978a Kevin Wolf
     */
543 f214978a Kevin Wolf
    for (req = m->dependent_requests.lh_first; req != NULL; req = next) {
544 f214978a Kevin Wolf
        next = req->next_depend.le_next;
545 f214978a Kevin Wolf
        qcow_aio_write_cb(req, 0);
546 f214978a Kevin Wolf
    }
547 f214978a Kevin Wolf
548 f214978a Kevin Wolf
    /* Empty the list for the next part of the request */
549 72cf2d4f Blue Swirl
    QLIST_INIT(&m->dependent_requests);
550 f214978a Kevin Wolf
}
551 f214978a Kevin Wolf
552 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
553 585f8587 bellard
{
554 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
555 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
556 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
557 585f8587 bellard
    int index_in_cluster;
558 585f8587 bellard
    const uint8_t *src_buf;
559 095a9c58 aliguori
    int n_end;
560 ce1a14dc pbrook
561 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
562 ce1a14dc pbrook
563 f214978a Kevin Wolf
    if (ret >= 0) {
564 f214978a Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta);
565 f214978a Kevin Wolf
    }
566 f214978a Kevin Wolf
567 f214978a Kevin Wolf
    run_dependent_requests(&acb->l2meta);
568 f214978a Kevin Wolf
569 f141eafe aliguori
    if (ret < 0)
570 f141eafe aliguori
        goto done;
571 585f8587 bellard
572 ce1a14dc pbrook
    acb->nb_sectors -= acb->n;
573 ce1a14dc pbrook
    acb->sector_num += acb->n;
574 ce1a14dc pbrook
    acb->buf += acb->n * 512;
575 585f8587 bellard
576 ce1a14dc pbrook
    if (acb->nb_sectors == 0) {
577 585f8587 bellard
        /* request completed */
578 f141eafe aliguori
        ret = 0;
579 f141eafe aliguori
        goto done;
580 585f8587 bellard
    }
581 3b46e624 ths
582 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
583 095a9c58 aliguori
    n_end = index_in_cluster + acb->nb_sectors;
584 095a9c58 aliguori
    if (s->crypt_method &&
585 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
586 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
587 095a9c58 aliguori
588 ed6ccf0f Kevin Wolf
    acb->cluster_offset = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
589 05203524 aliguori
                                          index_in_cluster,
590 e976c6a1 aliguori
                                          n_end, &acb->n, &acb->l2meta);
591 f214978a Kevin Wolf
592 f214978a Kevin Wolf
    /* Need to wait for another request? If so, we are done for now. */
593 f214978a Kevin Wolf
    if (!acb->cluster_offset && acb->l2meta.depends_on != NULL) {
594 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests,
595 f214978a Kevin Wolf
            acb, next_depend);
596 f214978a Kevin Wolf
        return;
597 f214978a Kevin Wolf
    }
598 f214978a Kevin Wolf
599 e976c6a1 aliguori
    if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) {
600 585f8587 bellard
        ret = -EIO;
601 f141eafe aliguori
        goto done;
602 585f8587 bellard
    }
603 585f8587 bellard
    if (s->crypt_method) {
604 ce1a14dc pbrook
        if (!acb->cluster_data) {
605 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
606 095a9c58 aliguori
                                             s->cluster_size);
607 585f8587 bellard
        }
608 ed6ccf0f Kevin Wolf
        qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
609 ce1a14dc pbrook
                        acb->n, 1, &s->aes_encrypt_key);
610 ce1a14dc pbrook
        src_buf = acb->cluster_data;
611 585f8587 bellard
    } else {
612 ce1a14dc pbrook
        src_buf = acb->buf;
613 585f8587 bellard
    }
614 c87c0672 aliguori
    acb->hd_iov.iov_base = (void *)src_buf;
615 c87c0672 aliguori
    acb->hd_iov.iov_len = acb->n * 512;
616 c87c0672 aliguori
    qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
617 c87c0672 aliguori
    acb->hd_aiocb = bdrv_aio_writev(s->hd,
618 c87c0672 aliguori
                                    (acb->cluster_offset >> 9) + index_in_cluster,
619 c87c0672 aliguori
                                    &acb->hd_qiov, acb->n,
620 c87c0672 aliguori
                                    qcow_aio_write_cb, acb);
621 ce1a14dc pbrook
    if (acb->hd_aiocb == NULL)
622 f141eafe aliguori
        goto done;
623 f141eafe aliguori
624 f141eafe aliguori
    return;
625 f141eafe aliguori
626 f141eafe aliguori
done:
627 f141eafe aliguori
    if (acb->qiov->niov > 1)
628 f141eafe aliguori
        qemu_vfree(acb->orig_buf);
629 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
630 f141eafe aliguori
    qemu_aio_release(acb);
631 585f8587 bellard
}
632 585f8587 bellard
633 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
634 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
635 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
636 585f8587 bellard
{
637 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
638 ce1a14dc pbrook
    QCowAIOCB *acb;
639 3b46e624 ths
640 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
641 585f8587 bellard
642 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
643 ce1a14dc pbrook
    if (!acb)
644 ce1a14dc pbrook
        return NULL;
645 3b46e624 ths
646 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
647 ce1a14dc pbrook
    return &acb->common;
648 585f8587 bellard
}
649 585f8587 bellard
650 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
651 585f8587 bellard
{
652 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
653 585f8587 bellard
    qemu_free(s->l1_table);
654 585f8587 bellard
    qemu_free(s->l2_cache);
655 585f8587 bellard
    qemu_free(s->cluster_cache);
656 585f8587 bellard
    qemu_free(s->cluster_data);
657 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
658 585f8587 bellard
    bdrv_delete(s->hd);
659 585f8587 bellard
}
660 585f8587 bellard
661 756e6736 Kevin Wolf
/*
662 756e6736 Kevin Wolf
 * Updates the variable length parts of the qcow2 header, i.e. the backing file
663 756e6736 Kevin Wolf
 * name and all extensions. qcow2 was not designed to allow such changes, so if
664 756e6736 Kevin Wolf
 * we run out of space (we can only use the first cluster) this function may
665 756e6736 Kevin Wolf
 * fail.
666 756e6736 Kevin Wolf
 *
667 756e6736 Kevin Wolf
 * Returns 0 on success, -errno in error cases.
668 756e6736 Kevin Wolf
 */
669 756e6736 Kevin Wolf
static int qcow2_update_ext_header(BlockDriverState *bs,
670 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
671 756e6736 Kevin Wolf
{
672 756e6736 Kevin Wolf
    size_t backing_file_len = 0;
673 756e6736 Kevin Wolf
    size_t backing_fmt_len = 0;
674 756e6736 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
675 756e6736 Kevin Wolf
    QCowExtension ext_backing_fmt = {0, 0};
676 756e6736 Kevin Wolf
    int ret;
677 756e6736 Kevin Wolf
678 756e6736 Kevin Wolf
    /* Backing file format doesn't make sense without a backing file */
679 756e6736 Kevin Wolf
    if (backing_fmt && !backing_file) {
680 756e6736 Kevin Wolf
        return -EINVAL;
681 756e6736 Kevin Wolf
    }
682 756e6736 Kevin Wolf
683 756e6736 Kevin Wolf
    /* Prepare the backing file format extension if needed */
684 756e6736 Kevin Wolf
    if (backing_fmt) {
685 756e6736 Kevin Wolf
        ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt));
686 756e6736 Kevin Wolf
        ext_backing_fmt.magic = cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT);
687 756e6736 Kevin Wolf
        backing_fmt_len = ((sizeof(ext_backing_fmt)
688 756e6736 Kevin Wolf
            + strlen(backing_fmt) + 7) & ~7);
689 756e6736 Kevin Wolf
    }
690 756e6736 Kevin Wolf
691 756e6736 Kevin Wolf
    /* Check if we can fit the new header into the first cluster */
692 756e6736 Kevin Wolf
    if (backing_file) {
693 756e6736 Kevin Wolf
        backing_file_len = strlen(backing_file);
694 756e6736 Kevin Wolf
    }
695 756e6736 Kevin Wolf
696 756e6736 Kevin Wolf
    size_t header_size = sizeof(QCowHeader) + backing_file_len
697 756e6736 Kevin Wolf
        + backing_fmt_len;
698 756e6736 Kevin Wolf
699 756e6736 Kevin Wolf
    if (header_size > s->cluster_size) {
700 756e6736 Kevin Wolf
        return -ENOSPC;
701 756e6736 Kevin Wolf
    }
702 756e6736 Kevin Wolf
703 756e6736 Kevin Wolf
    /* Rewrite backing file name and qcow2 extensions */
704 756e6736 Kevin Wolf
    size_t ext_size = header_size - sizeof(QCowHeader);
705 756e6736 Kevin Wolf
    uint8_t buf[ext_size];
706 756e6736 Kevin Wolf
    size_t offset = 0;
707 756e6736 Kevin Wolf
    size_t backing_file_offset = 0;
708 756e6736 Kevin Wolf
709 756e6736 Kevin Wolf
    if (backing_file) {
710 756e6736 Kevin Wolf
        if (backing_fmt) {
711 756e6736 Kevin Wolf
            int padding = backing_fmt_len -
712 756e6736 Kevin Wolf
                (sizeof(ext_backing_fmt) + strlen(backing_fmt));
713 756e6736 Kevin Wolf
714 756e6736 Kevin Wolf
            memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
715 756e6736 Kevin Wolf
            offset += sizeof(ext_backing_fmt);
716 756e6736 Kevin Wolf
717 756e6736 Kevin Wolf
            memcpy(buf + offset, backing_fmt, strlen(backing_fmt));
718 756e6736 Kevin Wolf
            offset += strlen(backing_fmt);
719 756e6736 Kevin Wolf
720 756e6736 Kevin Wolf
            memset(buf + offset, 0, padding);
721 756e6736 Kevin Wolf
            offset += padding;
722 756e6736 Kevin Wolf
        }
723 756e6736 Kevin Wolf
724 756e6736 Kevin Wolf
        memcpy(buf + offset, backing_file, backing_file_len);
725 756e6736 Kevin Wolf
        backing_file_offset = sizeof(QCowHeader) + offset;
726 756e6736 Kevin Wolf
    }
727 756e6736 Kevin Wolf
728 756e6736 Kevin Wolf
    ret = bdrv_pwrite(s->hd, sizeof(QCowHeader), buf, ext_size);
729 756e6736 Kevin Wolf
    if (ret < 0) {
730 756e6736 Kevin Wolf
        goto fail;
731 756e6736 Kevin Wolf
    }
732 756e6736 Kevin Wolf
733 756e6736 Kevin Wolf
    /* Update header fields */
734 756e6736 Kevin Wolf
    uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset);
735 756e6736 Kevin Wolf
    uint32_t be_backing_file_size = cpu_to_be32(backing_file_len);
736 756e6736 Kevin Wolf
737 756e6736 Kevin Wolf
    ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_offset),
738 756e6736 Kevin Wolf
        &be_backing_file_offset, sizeof(uint64_t));
739 756e6736 Kevin Wolf
    if (ret < 0) {
740 756e6736 Kevin Wolf
        goto fail;
741 756e6736 Kevin Wolf
    }
742 756e6736 Kevin Wolf
743 756e6736 Kevin Wolf
    ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_size),
744 756e6736 Kevin Wolf
        &be_backing_file_size, sizeof(uint32_t));
745 756e6736 Kevin Wolf
    if (ret < 0) {
746 756e6736 Kevin Wolf
        goto fail;
747 756e6736 Kevin Wolf
    }
748 756e6736 Kevin Wolf
749 756e6736 Kevin Wolf
    ret = 0;
750 756e6736 Kevin Wolf
fail:
751 756e6736 Kevin Wolf
    return ret;
752 756e6736 Kevin Wolf
}
753 756e6736 Kevin Wolf
754 756e6736 Kevin Wolf
static int qcow2_change_backing_file(BlockDriverState *bs,
755 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
756 756e6736 Kevin Wolf
{
757 756e6736 Kevin Wolf
    return qcow2_update_ext_header(bs, backing_file, backing_fmt);
758 756e6736 Kevin Wolf
}
759 756e6736 Kevin Wolf
760 73c632ed Kevin Wolf
static int get_bits_from_size(size_t size)
761 73c632ed Kevin Wolf
{
762 73c632ed Kevin Wolf
    int res = 0;
763 73c632ed Kevin Wolf
764 73c632ed Kevin Wolf
    if (size == 0) {
765 73c632ed Kevin Wolf
        return -1;
766 73c632ed Kevin Wolf
    }
767 73c632ed Kevin Wolf
768 73c632ed Kevin Wolf
    while (size != 1) {
769 73c632ed Kevin Wolf
        /* Not a power of two */
770 73c632ed Kevin Wolf
        if (size & 1) {
771 73c632ed Kevin Wolf
            return -1;
772 73c632ed Kevin Wolf
        }
773 73c632ed Kevin Wolf
774 73c632ed Kevin Wolf
        size >>= 1;
775 73c632ed Kevin Wolf
        res++;
776 73c632ed Kevin Wolf
    }
777 73c632ed Kevin Wolf
778 73c632ed Kevin Wolf
    return res;
779 73c632ed Kevin Wolf
}
780 73c632ed Kevin Wolf
781 a35e1c17 Kevin Wolf
782 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
783 a35e1c17 Kevin Wolf
{
784 a35e1c17 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
785 2000cbc5 Blue Swirl
    uint64_t cluster_offset = 0;
786 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
787 a35e1c17 Kevin Wolf
    uint64_t offset;
788 a35e1c17 Kevin Wolf
    int num;
789 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
790 a35e1c17 Kevin Wolf
791 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
792 a35e1c17 Kevin Wolf
    offset = 0;
793 72cf2d4f Blue Swirl
    QLIST_INIT(&meta.dependent_requests);
794 a35e1c17 Kevin Wolf
795 a35e1c17 Kevin Wolf
    while (nb_sectors) {
796 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
797 a35e1c17 Kevin Wolf
        cluster_offset = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
798 a35e1c17 Kevin Wolf
            &meta);
799 a35e1c17 Kevin Wolf
800 a35e1c17 Kevin Wolf
        if (cluster_offset == 0) {
801 a35e1c17 Kevin Wolf
            return -1;
802 a35e1c17 Kevin Wolf
        }
803 a35e1c17 Kevin Wolf
804 a35e1c17 Kevin Wolf
        if (qcow2_alloc_cluster_link_l2(bs, cluster_offset, &meta) < 0) {
805 a35e1c17 Kevin Wolf
            qcow2_free_any_clusters(bs, cluster_offset, meta.nb_clusters);
806 a35e1c17 Kevin Wolf
            return -1;
807 a35e1c17 Kevin Wolf
        }
808 a35e1c17 Kevin Wolf
809 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
810 f214978a Kevin Wolf
         * from the list of in-flight requests */
811 f214978a Kevin Wolf
        run_dependent_requests(&meta);
812 f214978a Kevin Wolf
813 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
814 a35e1c17 Kevin Wolf
815 a35e1c17 Kevin Wolf
        nb_sectors -= num;
816 a35e1c17 Kevin Wolf
        offset += num << 9;
817 a35e1c17 Kevin Wolf
    }
818 a35e1c17 Kevin Wolf
819 a35e1c17 Kevin Wolf
    /*
820 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
821 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
822 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
823 a35e1c17 Kevin Wolf
     */
824 a35e1c17 Kevin Wolf
    if (cluster_offset != 0) {
825 ea80b906 Kevin Wolf
        uint8_t buf[512];
826 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
827 ea80b906 Kevin Wolf
        bdrv_write(s->hd, (cluster_offset >> 9) + num - 1, buf, 1);
828 a35e1c17 Kevin Wolf
    }
829 a35e1c17 Kevin Wolf
830 a35e1c17 Kevin Wolf
    return 0;
831 a35e1c17 Kevin Wolf
}
832 a35e1c17 Kevin Wolf
833 f965509c aliguori
static int qcow_create2(const char *filename, int64_t total_size,
834 f965509c aliguori
                        const char *backing_file, const char *backing_format,
835 a35e1c17 Kevin Wolf
                        int flags, size_t cluster_size, int prealloc)
836 585f8587 bellard
{
837 f965509c aliguori
838 585f8587 bellard
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
839 2d2431f0 aliguori
    int ref_clusters, backing_format_len = 0;
840 e1c7f0e3 Kevin Wolf
    int rounded_ext_bf_len = 0;
841 585f8587 bellard
    QCowHeader header;
842 585f8587 bellard
    uint64_t tmp, offset;
843 585f8587 bellard
    QCowCreateState s1, *s = &s1;
844 f965509c aliguori
    QCowExtension ext_bf = {0, 0};
845 f965509c aliguori
846 3b46e624 ths
847 585f8587 bellard
    memset(s, 0, sizeof(*s));
848 585f8587 bellard
849 585f8587 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
850 585f8587 bellard
    if (fd < 0)
851 585f8587 bellard
        return -1;
852 585f8587 bellard
    memset(&header, 0, sizeof(header));
853 585f8587 bellard
    header.magic = cpu_to_be32(QCOW_MAGIC);
854 585f8587 bellard
    header.version = cpu_to_be32(QCOW_VERSION);
855 585f8587 bellard
    header.size = cpu_to_be64(total_size * 512);
856 585f8587 bellard
    header_size = sizeof(header);
857 585f8587 bellard
    backing_filename_len = 0;
858 585f8587 bellard
    if (backing_file) {
859 f965509c aliguori
        if (backing_format) {
860 f965509c aliguori
            ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
861 f965509c aliguori
            backing_format_len = strlen(backing_format);
862 e1c7f0e3 Kevin Wolf
            ext_bf.len = backing_format_len;
863 e1c7f0e3 Kevin Wolf
            rounded_ext_bf_len = (sizeof(ext_bf) + ext_bf.len + 7) & ~7;
864 e1c7f0e3 Kevin Wolf
            header_size += rounded_ext_bf_len;
865 f965509c aliguori
        }
866 585f8587 bellard
        header.backing_file_offset = cpu_to_be64(header_size);
867 585f8587 bellard
        backing_filename_len = strlen(backing_file);
868 585f8587 bellard
        header.backing_file_size = cpu_to_be32(backing_filename_len);
869 585f8587 bellard
        header_size += backing_filename_len;
870 585f8587 bellard
    }
871 73c632ed Kevin Wolf
872 73c632ed Kevin Wolf
    /* Cluster size */
873 73c632ed Kevin Wolf
    s->cluster_bits = get_bits_from_size(cluster_size);
874 73c632ed Kevin Wolf
    if (s->cluster_bits < MIN_CLUSTER_BITS ||
875 73c632ed Kevin Wolf
        s->cluster_bits > MAX_CLUSTER_BITS)
876 73c632ed Kevin Wolf
    {
877 73c632ed Kevin Wolf
        fprintf(stderr, "Cluster size must be a power of two between "
878 73c632ed Kevin Wolf
            "%d and %dk\n",
879 73c632ed Kevin Wolf
            1 << MIN_CLUSTER_BITS,
880 73c632ed Kevin Wolf
            1 << (MAX_CLUSTER_BITS - 10));
881 73c632ed Kevin Wolf
        return -EINVAL;
882 73c632ed Kevin Wolf
    }
883 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
884 73c632ed Kevin Wolf
885 585f8587 bellard
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
886 585f8587 bellard
    header_size = (header_size + 7) & ~7;
887 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT) {
888 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
889 585f8587 bellard
    } else {
890 585f8587 bellard
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
891 585f8587 bellard
    }
892 585f8587 bellard
    l2_bits = s->cluster_bits - 3;
893 585f8587 bellard
    shift = s->cluster_bits + l2_bits;
894 585f8587 bellard
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
895 585f8587 bellard
    offset = align_offset(header_size, s->cluster_size);
896 585f8587 bellard
    s->l1_table_offset = offset;
897 585f8587 bellard
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
898 585f8587 bellard
    header.l1_size = cpu_to_be32(l1_size);
899 15e6690a bellard
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
900 585f8587 bellard
901 585f8587 bellard
    s->refcount_table = qemu_mallocz(s->cluster_size);
902 3b46e624 ths
903 585f8587 bellard
    s->refcount_table_offset = offset;
904 585f8587 bellard
    header.refcount_table_offset = cpu_to_be64(offset);
905 585f8587 bellard
    header.refcount_table_clusters = cpu_to_be32(1);
906 585f8587 bellard
    offset += s->cluster_size;
907 585f8587 bellard
    s->refcount_block_offset = offset;
908 2d2431f0 aliguori
909 2d2431f0 aliguori
    /* count how many refcount blocks needed */
910 2d2431f0 aliguori
    tmp = offset >> s->cluster_bits;
911 2d2431f0 aliguori
    ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
912 2d2431f0 aliguori
    for (i=0; i < ref_clusters; i++) {
913 2d2431f0 aliguori
        s->refcount_table[i] = cpu_to_be64(offset);
914 2d2431f0 aliguori
        offset += s->cluster_size;
915 2d2431f0 aliguori
    }
916 2d2431f0 aliguori
917 2d2431f0 aliguori
    s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
918 585f8587 bellard
919 585f8587 bellard
    /* update refcounts */
920 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, 0, header_size);
921 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->l1_table_offset,
922 ed6ccf0f Kevin Wolf
        l1_size * sizeof(uint64_t));
923 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
924 ed6ccf0f Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_block_offset,
925 ed6ccf0f Kevin Wolf
        ref_clusters * s->cluster_size);
926 3b46e624 ths
927 585f8587 bellard
    /* write all the data */
928 585f8587 bellard
    write(fd, &header, sizeof(header));
929 585f8587 bellard
    if (backing_file) {
930 f965509c aliguori
        if (backing_format_len) {
931 f965509c aliguori
            char zero[16];
932 e1c7f0e3 Kevin Wolf
            int padding = rounded_ext_bf_len - (ext_bf.len + sizeof(ext_bf));
933 f965509c aliguori
934 f965509c aliguori
            memset(zero, 0, sizeof(zero));
935 f965509c aliguori
            cpu_to_be32s(&ext_bf.magic);
936 f965509c aliguori
            cpu_to_be32s(&ext_bf.len);
937 f965509c aliguori
            write(fd, &ext_bf, sizeof(ext_bf));
938 f965509c aliguori
            write(fd, backing_format, backing_format_len);
939 e1c7f0e3 Kevin Wolf
            if (padding > 0) {
940 e1c7f0e3 Kevin Wolf
                write(fd, zero, padding);
941 f965509c aliguori
            }
942 f965509c aliguori
        }
943 585f8587 bellard
        write(fd, backing_file, backing_filename_len);
944 585f8587 bellard
    }
945 585f8587 bellard
    lseek(fd, s->l1_table_offset, SEEK_SET);
946 585f8587 bellard
    tmp = 0;
947 585f8587 bellard
    for(i = 0;i < l1_size; i++) {
948 585f8587 bellard
        write(fd, &tmp, sizeof(tmp));
949 585f8587 bellard
    }
950 585f8587 bellard
    lseek(fd, s->refcount_table_offset, SEEK_SET);
951 585f8587 bellard
    write(fd, s->refcount_table, s->cluster_size);
952 3b46e624 ths
953 585f8587 bellard
    lseek(fd, s->refcount_block_offset, SEEK_SET);
954 2d2431f0 aliguori
    write(fd, s->refcount_block, ref_clusters * s->cluster_size);
955 585f8587 bellard
956 585f8587 bellard
    qemu_free(s->refcount_table);
957 585f8587 bellard
    qemu_free(s->refcount_block);
958 585f8587 bellard
    close(fd);
959 a35e1c17 Kevin Wolf
960 a35e1c17 Kevin Wolf
    /* Preallocate metadata */
961 a35e1c17 Kevin Wolf
    if (prealloc) {
962 a35e1c17 Kevin Wolf
        BlockDriverState *bs;
963 a35e1c17 Kevin Wolf
        bs = bdrv_new("");
964 a35e1c17 Kevin Wolf
        bdrv_open(bs, filename, BDRV_O_CACHE_WB);
965 a35e1c17 Kevin Wolf
        preallocate(bs);
966 a35e1c17 Kevin Wolf
        bdrv_close(bs);
967 a35e1c17 Kevin Wolf
    }
968 a35e1c17 Kevin Wolf
969 585f8587 bellard
    return 0;
970 585f8587 bellard
}
971 585f8587 bellard
972 0e7e1989 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
973 0e7e1989 Kevin Wolf
{
974 0e7e1989 Kevin Wolf
    const char *backing_file = NULL;
975 0e7e1989 Kevin Wolf
    const char *backing_fmt = NULL;
976 0e7e1989 Kevin Wolf
    uint64_t sectors = 0;
977 0e7e1989 Kevin Wolf
    int flags = 0;
978 9ccb258e Kevin Wolf
    size_t cluster_size = 65536;
979 a35e1c17 Kevin Wolf
    int prealloc = 0;
980 0e7e1989 Kevin Wolf
981 0e7e1989 Kevin Wolf
    /* Read out options */
982 0e7e1989 Kevin Wolf
    while (options && options->name) {
983 0e7e1989 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
984 0e7e1989 Kevin Wolf
            sectors = options->value.n / 512;
985 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
986 0e7e1989 Kevin Wolf
            backing_file = options->value.s;
987 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
988 0e7e1989 Kevin Wolf
            backing_fmt = options->value.s;
989 0e7e1989 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
990 0e7e1989 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
991 73c632ed Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
992 73c632ed Kevin Wolf
            if (options->value.n) {
993 73c632ed Kevin Wolf
                cluster_size = options->value.n;
994 73c632ed Kevin Wolf
            }
995 a35e1c17 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
996 a35e1c17 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
997 a35e1c17 Kevin Wolf
                prealloc = 0;
998 a35e1c17 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
999 a35e1c17 Kevin Wolf
                prealloc = 1;
1000 a35e1c17 Kevin Wolf
            } else {
1001 a35e1c17 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1002 a35e1c17 Kevin Wolf
                    options->value.s);
1003 a35e1c17 Kevin Wolf
                return -EINVAL;
1004 a35e1c17 Kevin Wolf
            }
1005 0e7e1989 Kevin Wolf
        }
1006 0e7e1989 Kevin Wolf
        options++;
1007 0e7e1989 Kevin Wolf
    }
1008 0e7e1989 Kevin Wolf
1009 a35e1c17 Kevin Wolf
    if (backing_file && prealloc) {
1010 a35e1c17 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1011 a35e1c17 Kevin Wolf
            "the same time\n");
1012 a35e1c17 Kevin Wolf
        return -EINVAL;
1013 a35e1c17 Kevin Wolf
    }
1014 a35e1c17 Kevin Wolf
1015 73c632ed Kevin Wolf
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
1016 a35e1c17 Kevin Wolf
        cluster_size, prealloc);
1017 f965509c aliguori
}
1018 f965509c aliguori
1019 585f8587 bellard
static int qcow_make_empty(BlockDriverState *bs)
1020 585f8587 bellard
{
1021 585f8587 bellard
#if 0
1022 585f8587 bellard
    /* XXX: not correct */
1023 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1024 585f8587 bellard
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1025 585f8587 bellard
    int ret;
1026 585f8587 bellard

1027 585f8587 bellard
    memset(s->l1_table, 0, l1_length);
1028 585f8587 bellard
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1029 ac674887 aliguori
        return -1;
1030 585f8587 bellard
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1031 585f8587 bellard
    if (ret < 0)
1032 585f8587 bellard
        return ret;
1033 3b46e624 ths

1034 585f8587 bellard
    l2_cache_reset(bs);
1035 585f8587 bellard
#endif
1036 585f8587 bellard
    return 0;
1037 585f8587 bellard
}
1038 585f8587 bellard
1039 585f8587 bellard
/* XXX: put compressed sectors first, then all the cluster aligned
1040 585f8587 bellard
   tables to avoid losing bytes in alignment */
1041 5fafdf24 ths
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1042 585f8587 bellard
                                 const uint8_t *buf, int nb_sectors)
1043 585f8587 bellard
{
1044 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1045 585f8587 bellard
    z_stream strm;
1046 585f8587 bellard
    int ret, out_len;
1047 585f8587 bellard
    uint8_t *out_buf;
1048 585f8587 bellard
    uint64_t cluster_offset;
1049 585f8587 bellard
1050 585f8587 bellard
    if (nb_sectors == 0) {
1051 585f8587 bellard
        /* align end of file to a sector boundary to ease reading with
1052 585f8587 bellard
           sector based I/Os */
1053 585f8587 bellard
        cluster_offset = bdrv_getlength(s->hd);
1054 585f8587 bellard
        cluster_offset = (cluster_offset + 511) & ~511;
1055 585f8587 bellard
        bdrv_truncate(s->hd, cluster_offset);
1056 585f8587 bellard
        return 0;
1057 585f8587 bellard
    }
1058 585f8587 bellard
1059 585f8587 bellard
    if (nb_sectors != s->cluster_sectors)
1060 585f8587 bellard
        return -EINVAL;
1061 585f8587 bellard
1062 585f8587 bellard
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1063 585f8587 bellard
1064 585f8587 bellard
    /* best compression, small window, no zlib header */
1065 585f8587 bellard
    memset(&strm, 0, sizeof(strm));
1066 585f8587 bellard
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1067 5fafdf24 ths
                       Z_DEFLATED, -12,
1068 585f8587 bellard
                       9, Z_DEFAULT_STRATEGY);
1069 585f8587 bellard
    if (ret != 0) {
1070 585f8587 bellard
        qemu_free(out_buf);
1071 585f8587 bellard
        return -1;
1072 585f8587 bellard
    }
1073 585f8587 bellard
1074 585f8587 bellard
    strm.avail_in = s->cluster_size;
1075 585f8587 bellard
    strm.next_in = (uint8_t *)buf;
1076 585f8587 bellard
    strm.avail_out = s->cluster_size;
1077 585f8587 bellard
    strm.next_out = out_buf;
1078 585f8587 bellard
1079 585f8587 bellard
    ret = deflate(&strm, Z_FINISH);
1080 585f8587 bellard
    if (ret != Z_STREAM_END && ret != Z_OK) {
1081 585f8587 bellard
        qemu_free(out_buf);
1082 585f8587 bellard
        deflateEnd(&strm);
1083 585f8587 bellard
        return -1;
1084 585f8587 bellard
    }
1085 585f8587 bellard
    out_len = strm.next_out - out_buf;
1086 585f8587 bellard
1087 585f8587 bellard
    deflateEnd(&strm);
1088 585f8587 bellard
1089 585f8587 bellard
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1090 585f8587 bellard
        /* could not compress: write normal cluster */
1091 ade40677 Kevin Wolf
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1092 585f8587 bellard
    } else {
1093 ed6ccf0f Kevin Wolf
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1094 ed6ccf0f Kevin Wolf
            sector_num << 9, out_len);
1095 52d893ec aliguori
        if (!cluster_offset)
1096 52d893ec aliguori
            return -1;
1097 585f8587 bellard
        cluster_offset &= s->cluster_offset_mask;
1098 585f8587 bellard
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1099 585f8587 bellard
            qemu_free(out_buf);
1100 585f8587 bellard
            return -1;
1101 585f8587 bellard
        }
1102 585f8587 bellard
    }
1103 3b46e624 ths
1104 585f8587 bellard
    qemu_free(out_buf);
1105 585f8587 bellard
    return 0;
1106 585f8587 bellard
}
1107 585f8587 bellard
1108 585f8587 bellard
static void qcow_flush(BlockDriverState *bs)
1109 585f8587 bellard
{
1110 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1111 585f8587 bellard
    bdrv_flush(s->hd);
1112 585f8587 bellard
}
1113 585f8587 bellard
1114 f8012c13 Kevin Wolf
static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
1115 f8012c13 Kevin Wolf
         BlockDriverCompletionFunc *cb, void *opaque)
1116 f8012c13 Kevin Wolf
{
1117 f8012c13 Kevin Wolf
     BDRVQcowState *s = bs->opaque;
1118 f8012c13 Kevin Wolf
1119 f8012c13 Kevin Wolf
     return bdrv_aio_flush(s->hd, cb, opaque);
1120 f8012c13 Kevin Wolf
}
1121 f8012c13 Kevin Wolf
1122 45566e9c Christoph Hellwig
static int64_t qcow_vm_state_offset(BDRVQcowState *s)
1123 45566e9c Christoph Hellwig
{
1124 45566e9c Christoph Hellwig
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1125 45566e9c Christoph Hellwig
}
1126 45566e9c Christoph Hellwig
1127 585f8587 bellard
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1128 585f8587 bellard
{
1129 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1130 585f8587 bellard
    bdi->cluster_size = s->cluster_size;
1131 45566e9c Christoph Hellwig
    bdi->vm_state_offset = qcow_vm_state_offset(s);
1132 585f8587 bellard
    return 0;
1133 585f8587 bellard
}
1134 585f8587 bellard
1135 585f8587 bellard
1136 e97fc193 aliguori
static int qcow_check(BlockDriverState *bs)
1137 e97fc193 aliguori
{
1138 ed6ccf0f Kevin Wolf
    return qcow2_check_refcounts(bs);
1139 585f8587 bellard
}
1140 585f8587 bellard
1141 585f8587 bellard
#if 0
1142 585f8587 bellard
static void dump_refcounts(BlockDriverState *bs)
1143 585f8587 bellard
{
1144 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
1145 585f8587 bellard
    int64_t nb_clusters, k, k1, size;
1146 585f8587 bellard
    int refcount;
1147 585f8587 bellard

1148 585f8587 bellard
    size = bdrv_getlength(s->hd);
1149 6db6c638 aliguori
    nb_clusters = size_to_clusters(s, size);
1150 585f8587 bellard
    for(k = 0; k < nb_clusters;) {
1151 585f8587 bellard
        k1 = k;
1152 585f8587 bellard
        refcount = get_refcount(bs, k);
1153 585f8587 bellard
        k++;
1154 585f8587 bellard
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1155 585f8587 bellard
            k++;
1156 585f8587 bellard
        printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
1157 585f8587 bellard
    }
1158 585f8587 bellard
}
1159 585f8587 bellard
#endif
1160 585f8587 bellard
1161 45566e9c Christoph Hellwig
static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1162 178e08a5 aliguori
                           int64_t pos, int size)
1163 178e08a5 aliguori
{
1164 45566e9c Christoph Hellwig
    BDRVQcowState *s = bs->opaque;
1165 178e08a5 aliguori
    int growable = bs->growable;
1166 1d36e3aa Kevin Wolf
    int ret;
1167 178e08a5 aliguori
1168 178e08a5 aliguori
    bs->growable = 1;
1169 1d36e3aa Kevin Wolf
    ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
1170 178e08a5 aliguori
    bs->growable = growable;
1171 178e08a5 aliguori
1172 1d36e3aa Kevin Wolf
    return ret;
1173 178e08a5 aliguori
}
1174 178e08a5 aliguori
1175 45566e9c Christoph Hellwig
static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1176 178e08a5 aliguori
                           int64_t pos, int size)
1177 178e08a5 aliguori
{
1178 45566e9c Christoph Hellwig
    BDRVQcowState *s = bs->opaque;
1179 178e08a5 aliguori
    int growable = bs->growable;
1180 178e08a5 aliguori
    int ret;
1181 178e08a5 aliguori
1182 178e08a5 aliguori
    bs->growable = 1;
1183 45566e9c Christoph Hellwig
    ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
1184 178e08a5 aliguori
    bs->growable = growable;
1185 178e08a5 aliguori
1186 178e08a5 aliguori
    return ret;
1187 178e08a5 aliguori
}
1188 178e08a5 aliguori
1189 0e7e1989 Kevin Wolf
static QEMUOptionParameter qcow_create_options[] = {
1190 db08adf5 Kevin Wolf
    {
1191 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_SIZE,
1192 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
1193 db08adf5 Kevin Wolf
        .help = "Virtual disk size"
1194 db08adf5 Kevin Wolf
    },
1195 db08adf5 Kevin Wolf
    {
1196 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FILE,
1197 db08adf5 Kevin Wolf
        .type = OPT_STRING,
1198 db08adf5 Kevin Wolf
        .help = "File name of a base image"
1199 db08adf5 Kevin Wolf
    },
1200 db08adf5 Kevin Wolf
    {
1201 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_BACKING_FMT,
1202 db08adf5 Kevin Wolf
        .type = OPT_STRING,
1203 db08adf5 Kevin Wolf
        .help = "Image format of the base image"
1204 db08adf5 Kevin Wolf
    },
1205 db08adf5 Kevin Wolf
    {
1206 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_ENCRYPT,
1207 db08adf5 Kevin Wolf
        .type = OPT_FLAG,
1208 db08adf5 Kevin Wolf
        .help = "Encrypt the image"
1209 db08adf5 Kevin Wolf
    },
1210 db08adf5 Kevin Wolf
    {
1211 db08adf5 Kevin Wolf
        .name = BLOCK_OPT_CLUSTER_SIZE,
1212 db08adf5 Kevin Wolf
        .type = OPT_SIZE,
1213 db08adf5 Kevin Wolf
        .help = "qcow2 cluster size"
1214 db08adf5 Kevin Wolf
    },
1215 a35e1c17 Kevin Wolf
    {
1216 a35e1c17 Kevin Wolf
        .name = BLOCK_OPT_PREALLOC,
1217 a35e1c17 Kevin Wolf
        .type = OPT_STRING,
1218 a35e1c17 Kevin Wolf
        .help = "Preallocation mode (allowed values: off, metadata)"
1219 a35e1c17 Kevin Wolf
    },
1220 0e7e1989 Kevin Wolf
    { NULL }
1221 0e7e1989 Kevin Wolf
};
1222 0e7e1989 Kevin Wolf
1223 5efa9d5a Anthony Liguori
static BlockDriver bdrv_qcow2 = {
1224 e60f469c aurel32
    .format_name        = "qcow2",
1225 e60f469c aurel32
    .instance_size        = sizeof(BDRVQcowState),
1226 e60f469c aurel32
    .bdrv_probe                = qcow_probe,
1227 e60f469c aurel32
    .bdrv_open                = qcow_open,
1228 e60f469c aurel32
    .bdrv_close                = qcow_close,
1229 e60f469c aurel32
    .bdrv_create        = qcow_create,
1230 e60f469c aurel32
    .bdrv_flush                = qcow_flush,
1231 e60f469c aurel32
    .bdrv_is_allocated        = qcow_is_allocated,
1232 e60f469c aurel32
    .bdrv_set_key        = qcow_set_key,
1233 e60f469c aurel32
    .bdrv_make_empty        = qcow_make_empty,
1234 e60f469c aurel32
1235 72ecf02d Kevin Wolf
    .bdrv_aio_readv        = qcow_aio_readv,
1236 72ecf02d Kevin Wolf
    .bdrv_aio_writev        = qcow_aio_writev,
1237 f8012c13 Kevin Wolf
    .bdrv_aio_flush        = qcow_aio_flush,
1238 585f8587 bellard
    .bdrv_write_compressed = qcow_write_compressed,
1239 585f8587 bellard
1240 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1241 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1242 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1243 ed6ccf0f Kevin Wolf
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1244 e60f469c aurel32
    .bdrv_get_info        = qcow_get_info,
1245 f965509c aliguori
1246 45566e9c Christoph Hellwig
    .bdrv_save_vmstate    = qcow_save_vmstate,
1247 45566e9c Christoph Hellwig
    .bdrv_load_vmstate    = qcow_load_vmstate,
1248 178e08a5 aliguori
1249 756e6736 Kevin Wolf
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1250 756e6736 Kevin Wolf
1251 0e7e1989 Kevin Wolf
    .create_options = qcow_create_options,
1252 e97fc193 aliguori
    .bdrv_check = qcow_check,
1253 585f8587 bellard
};
1254 5efa9d5a Anthony Liguori
1255 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1256 5efa9d5a Anthony Liguori
{
1257 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1258 5efa9d5a Anthony Liguori
}
1259 5efa9d5a Anthony Liguori
1260 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);