Statistics
| Branch: | Revision:

root / block / qcow2.c @ a9420734

History | View | Annotate | Download (44.8 kB)

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

35 585f8587 bellard
  - Support for multiple incremental snapshots.
36 585f8587 bellard
  - Memory management by reference counts.
37 585f8587 bellard
  - Clusters which have a reference count of one have the bit
38 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
39 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
40 585f8587 bellard
    in the cluster offsets.
41 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
42 3b46e624 ths
    snapshots.
43 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
44 585f8587 bellard
    (could be backported to QCOW).
45 585f8587 bellard
  - L2 tables have always a size of one cluster.
46 585f8587 bellard
*/
47 585f8587 bellard
48 9b80ddf3 aliguori
49 9b80ddf3 aliguori
typedef struct {
50 9b80ddf3 aliguori
    uint32_t magic;
51 9b80ddf3 aliguori
    uint32_t len;
52 9b80ddf3 aliguori
} QCowExtension;
53 9b80ddf3 aliguori
#define  QCOW_EXT_MAGIC_END 0
54 f965509c aliguori
#define  QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
55 9b80ddf3 aliguori
56 585f8587 bellard
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
57 585f8587 bellard
{
58 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
59 3b46e624 ths
60 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
61 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
62 5fafdf24 ths
        be32_to_cpu(cow_header->version) == QCOW_VERSION)
63 585f8587 bellard
        return 100;
64 585f8587 bellard
    else
65 585f8587 bellard
        return 0;
66 585f8587 bellard
}
67 585f8587 bellard
68 9b80ddf3 aliguori
69 9b80ddf3 aliguori
/* 
70 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
71 9b80ddf3 aliguori
 * start reading from start_offset
72 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
73 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
74 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
75 9b80ddf3 aliguori
 */
76 9b80ddf3 aliguori
static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset,
77 9b80ddf3 aliguori
                                uint64_t end_offset)
78 9b80ddf3 aliguori
{
79 9b80ddf3 aliguori
    QCowExtension ext;
80 9b80ddf3 aliguori
    uint64_t offset;
81 9b80ddf3 aliguori
82 9b80ddf3 aliguori
#ifdef DEBUG_EXT
83 9b80ddf3 aliguori
    printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
84 9b80ddf3 aliguori
#endif
85 9b80ddf3 aliguori
    offset = start_offset;
86 9b80ddf3 aliguori
    while (offset < end_offset) {
87 9b80ddf3 aliguori
88 9b80ddf3 aliguori
#ifdef DEBUG_EXT
89 9b80ddf3 aliguori
        /* Sanity check */
90 9b80ddf3 aliguori
        if (offset > s->cluster_size)
91 9b80ddf3 aliguori
            printf("qcow_handle_extension: suspicious offset %lu\n", offset);
92 9b80ddf3 aliguori
93 9b80ddf3 aliguori
        printf("attemting to read extended header in offset %lu\n", offset);
94 9b80ddf3 aliguori
#endif
95 9b80ddf3 aliguori
96 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
97 0bfcd599 Blue Swirl
            fprintf(stderr, "qcow_handle_extension: ERROR: "
98 0bfcd599 Blue Swirl
                    "pread fail from offset %" PRIu64 "\n",
99 0bfcd599 Blue Swirl
                    offset);
100 9b80ddf3 aliguori
            return 1;
101 9b80ddf3 aliguori
        }
102 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
103 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
104 9b80ddf3 aliguori
        offset += sizeof(ext);
105 9b80ddf3 aliguori
#ifdef DEBUG_EXT
106 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
107 9b80ddf3 aliguori
#endif
108 9b80ddf3 aliguori
        switch (ext.magic) {
109 9b80ddf3 aliguori
        case QCOW_EXT_MAGIC_END:
110 9b80ddf3 aliguori
            return 0;
111 f965509c aliguori
112 f965509c aliguori
        case QCOW_EXT_MAGIC_BACKING_FORMAT:
113 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
114 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
115 4c978075 aliguori
                        " (>=%zu)\n",
116 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
117 f965509c aliguori
                return 2;
118 f965509c aliguori
            }
119 66f82cee Kevin Wolf
            if (bdrv_pread(bs->file, offset , bs->backing_format,
120 f965509c aliguori
                           ext.len) != ext.len)
121 f965509c aliguori
                return 3;
122 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
123 f965509c aliguori
#ifdef DEBUG_EXT
124 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
125 f965509c aliguori
#endif
126 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
127 f965509c aliguori
            break;
128 f965509c aliguori
129 9b80ddf3 aliguori
        default:
130 9b80ddf3 aliguori
            /* unknown magic -- just skip it */
131 e1c7f0e3 Kevin Wolf
            offset = ((offset + ext.len + 7) & ~7);
132 9b80ddf3 aliguori
            break;
133 9b80ddf3 aliguori
        }
134 9b80ddf3 aliguori
    }
135 9b80ddf3 aliguori
136 9b80ddf3 aliguori
    return 0;
137 9b80ddf3 aliguori
}
138 9b80ddf3 aliguori
139 9b80ddf3 aliguori
140 66f82cee Kevin Wolf
static int qcow_open(BlockDriverState *bs, int flags)
141 585f8587 bellard
{
142 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
143 419b19d9 Stefan Hajnoczi
    int len, i;
144 585f8587 bellard
    QCowHeader header;
145 9b80ddf3 aliguori
    uint64_t ext_end;
146 585f8587 bellard
147 66f82cee Kevin Wolf
    if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
148 585f8587 bellard
        goto fail;
149 585f8587 bellard
    be32_to_cpus(&header.magic);
150 585f8587 bellard
    be32_to_cpus(&header.version);
151 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
152 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
153 585f8587 bellard
    be64_to_cpus(&header.size);
154 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
155 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
156 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
157 585f8587 bellard
    be32_to_cpus(&header.l1_size);
158 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
159 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
160 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
161 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
162 3b46e624 ths
163 585f8587 bellard
    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
164 585f8587 bellard
        goto fail;
165 d191d12d Stefan Weil
    if (header.cluster_bits < MIN_CLUSTER_BITS ||
166 73c632ed Kevin Wolf
        header.cluster_bits > MAX_CLUSTER_BITS)
167 585f8587 bellard
        goto fail;
168 585f8587 bellard
    if (header.crypt_method > QCOW_CRYPT_AES)
169 585f8587 bellard
        goto fail;
170 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
171 585f8587 bellard
    if (s->crypt_method_header)
172 585f8587 bellard
        bs->encrypted = 1;
173 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
174 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
175 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
176 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
177 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
178 585f8587 bellard
    bs->total_sectors = header.size / 512;
179 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
180 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
181 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
182 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
183 5fafdf24 ths
    s->refcount_table_size =
184 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
185 585f8587 bellard
186 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
187 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
188 585f8587 bellard
189 585f8587 bellard
    /* read the level 1 table */
190 585f8587 bellard
    s->l1_size = header.l1_size;
191 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = size_to_l1(s, header.size);
192 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
193 585f8587 bellard
       header.size bytes */
194 585f8587 bellard
    if (s->l1_size < s->l1_vm_state_index)
195 585f8587 bellard
        goto fail;
196 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
197 d191d12d Stefan Weil
    if (s->l1_size > 0) {
198 d191d12d Stefan Weil
        s->l1_table = qemu_mallocz(
199 d191d12d Stefan Weil
            align_offset(s->l1_size * sizeof(uint64_t), 512));
200 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
201 d191d12d Stefan Weil
            s->l1_size * sizeof(uint64_t))
202 d191d12d Stefan Weil
            goto fail;
203 d191d12d Stefan Weil
        for(i = 0;i < s->l1_size; i++) {
204 d191d12d Stefan Weil
            be64_to_cpus(&s->l1_table[i]);
205 d191d12d Stefan Weil
        }
206 585f8587 bellard
    }
207 585f8587 bellard
    /* alloc L2 cache */
208 585f8587 bellard
    s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
209 585f8587 bellard
    s->cluster_cache = qemu_malloc(s->cluster_size);
210 585f8587 bellard
    /* one more sector for decompressed data alignment */
211 095a9c58 aliguori
    s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
212 095a9c58 aliguori
                                  + 512);
213 585f8587 bellard
    s->cluster_cache_offset = -1;
214 3b46e624 ths
215 ed6ccf0f Kevin Wolf
    if (qcow2_refcount_init(bs) < 0)
216 585f8587 bellard
        goto fail;
217 585f8587 bellard
218 72cf2d4f Blue Swirl
    QLIST_INIT(&s->cluster_allocs);
219 f214978a Kevin Wolf
220 9b80ddf3 aliguori
    /* read qcow2 extensions */
221 9b80ddf3 aliguori
    if (header.backing_file_offset)
222 9b80ddf3 aliguori
        ext_end = header.backing_file_offset;
223 9b80ddf3 aliguori
    else
224 9b80ddf3 aliguori
        ext_end = s->cluster_size;
225 9b80ddf3 aliguori
    if (qcow_read_extensions(bs, sizeof(header), ext_end))
226 9b80ddf3 aliguori
        goto fail;
227 9b80ddf3 aliguori
228 585f8587 bellard
    /* read the backing file name */
229 585f8587 bellard
    if (header.backing_file_offset != 0) {
230 585f8587 bellard
        len = header.backing_file_size;
231 585f8587 bellard
        if (len > 1023)
232 585f8587 bellard
            len = 1023;
233 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
234 585f8587 bellard
            goto fail;
235 585f8587 bellard
        bs->backing_file[len] = '\0';
236 585f8587 bellard
    }
237 ed6ccf0f Kevin Wolf
    if (qcow2_read_snapshots(bs) < 0)
238 585f8587 bellard
        goto fail;
239 585f8587 bellard
240 585f8587 bellard
#ifdef DEBUG_ALLOC
241 14899cdf Filip Navara
    qcow2_check_refcounts(bs);
242 585f8587 bellard
#endif
243 585f8587 bellard
    return 0;
244 585f8587 bellard
245 585f8587 bellard
 fail:
246 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
247 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
248 585f8587 bellard
    qemu_free(s->l1_table);
249 585f8587 bellard
    qemu_free(s->l2_cache);
250 585f8587 bellard
    qemu_free(s->cluster_cache);
251 585f8587 bellard
    qemu_free(s->cluster_data);
252 585f8587 bellard
    return -1;
253 585f8587 bellard
}
254 585f8587 bellard
255 585f8587 bellard
static int qcow_set_key(BlockDriverState *bs, const char *key)
256 585f8587 bellard
{
257 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
258 585f8587 bellard
    uint8_t keybuf[16];
259 585f8587 bellard
    int len, i;
260 3b46e624 ths
261 585f8587 bellard
    memset(keybuf, 0, 16);
262 585f8587 bellard
    len = strlen(key);
263 585f8587 bellard
    if (len > 16)
264 585f8587 bellard
        len = 16;
265 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
266 585f8587 bellard
       entropy */
267 585f8587 bellard
    for(i = 0;i < len;i++) {
268 585f8587 bellard
        keybuf[i] = key[i];
269 585f8587 bellard
    }
270 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
271 585f8587 bellard
272 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
273 585f8587 bellard
        return -1;
274 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
275 585f8587 bellard
        return -1;
276 585f8587 bellard
#if 0
277 585f8587 bellard
    /* test */
278 585f8587 bellard
    {
279 585f8587 bellard
        uint8_t in[16];
280 585f8587 bellard
        uint8_t out[16];
281 585f8587 bellard
        uint8_t tmp[16];
282 585f8587 bellard
        for(i=0;i<16;i++)
283 585f8587 bellard
            in[i] = i;
284 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
285 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
286 585f8587 bellard
        for(i = 0; i < 16; i++)
287 585f8587 bellard
            printf(" %02x", tmp[i]);
288 585f8587 bellard
        printf("\n");
289 585f8587 bellard
        for(i = 0; i < 16; i++)
290 585f8587 bellard
            printf(" %02x", out[i]);
291 585f8587 bellard
        printf("\n");
292 585f8587 bellard
    }
293 585f8587 bellard
#endif
294 585f8587 bellard
    return 0;
295 585f8587 bellard
}
296 585f8587 bellard
297 5fafdf24 ths
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
298 585f8587 bellard
                             int nb_sectors, int *pnum)
299 585f8587 bellard
{
300 585f8587 bellard
    uint64_t cluster_offset;
301 1c46efaa Kevin Wolf
    int ret;
302 585f8587 bellard
303 095a9c58 aliguori
    *pnum = nb_sectors;
304 1c46efaa Kevin Wolf
    /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
305 1c46efaa Kevin Wolf
     * pass them on today */
306 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
307 1c46efaa Kevin Wolf
    if (ret < 0) {
308 1c46efaa Kevin Wolf
        *pnum = 0;
309 1c46efaa Kevin Wolf
    }
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 bd28f835 Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
316 bd28f835 Kevin Wolf
                  int64_t sector_num, 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 bd28f835 Kevin Wolf
326 bd28f835 Kevin Wolf
    qemu_iovec_memset(qiov, 0, 512 * (nb_sectors - n1));
327 bd28f835 Kevin Wolf
328 a9465922 bellard
    return n1;
329 a9465922 bellard
}
330 a9465922 bellard
331 ce1a14dc pbrook
typedef struct QCowAIOCB {
332 ce1a14dc pbrook
    BlockDriverAIOCB common;
333 585f8587 bellard
    int64_t sector_num;
334 f141eafe aliguori
    QEMUIOVector *qiov;
335 7b88e48b Christoph Hellwig
    int remaining_sectors;
336 7b88e48b Christoph Hellwig
    int cur_nr_sectors;        /* number of sectors in current iteration */
337 bd28f835 Kevin Wolf
    uint64_t bytes_done;
338 585f8587 bellard
    uint64_t cluster_offset;
339 5fafdf24 ths
    uint8_t *cluster_data;
340 585f8587 bellard
    BlockDriverAIOCB *hd_aiocb;
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 b666d239 Kevin Wolf
    QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common);
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 bd28f835 Kevin Wolf
            qcow2_encrypt_sectors(s, acb->sector_num,  acb->cluster_data,
402 bd28f835 Kevin Wolf
                acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key);
403 bd28f835 Kevin Wolf
            qemu_iovec_reset(&acb->hd_qiov);
404 bd28f835 Kevin Wolf
            qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
405 bd28f835 Kevin Wolf
                acb->cur_nr_sectors * 512);
406 bd28f835 Kevin Wolf
            qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data,
407 bd28f835 Kevin Wolf
                512 * acb->cur_nr_sectors);
408 585f8587 bellard
        }
409 585f8587 bellard
    }
410 585f8587 bellard
411 7b88e48b Christoph Hellwig
    acb->remaining_sectors -= acb->cur_nr_sectors;
412 7b88e48b Christoph Hellwig
    acb->sector_num += acb->cur_nr_sectors;
413 bd28f835 Kevin Wolf
    acb->bytes_done += acb->cur_nr_sectors * 512;
414 585f8587 bellard
415 7b88e48b Christoph Hellwig
    if (acb->remaining_sectors == 0) {
416 585f8587 bellard
        /* request completed */
417 f141eafe aliguori
        ret = 0;
418 f141eafe aliguori
        goto done;
419 585f8587 bellard
    }
420 3b46e624 ths
421 585f8587 bellard
    /* prepare next AIO request */
422 7b88e48b Christoph Hellwig
    acb->cur_nr_sectors = acb->remaining_sectors;
423 bd28f835 Kevin Wolf
    if (s->crypt_method) {
424 bd28f835 Kevin Wolf
        acb->cur_nr_sectors = MIN(acb->cur_nr_sectors,
425 bd28f835 Kevin Wolf
            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
426 bd28f835 Kevin Wolf
    }
427 bd28f835 Kevin Wolf
428 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9,
429 1c46efaa Kevin Wolf
        &acb->cur_nr_sectors, &acb->cluster_offset);
430 1c46efaa Kevin Wolf
    if (ret < 0) {
431 1c46efaa Kevin Wolf
        goto done;
432 1c46efaa Kevin Wolf
    }
433 1c46efaa Kevin Wolf
434 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
435 ce1a14dc pbrook
436 bd28f835 Kevin Wolf
    qemu_iovec_reset(&acb->hd_qiov);
437 bd28f835 Kevin Wolf
    qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
438 bd28f835 Kevin Wolf
        acb->cur_nr_sectors * 512);
439 bd28f835 Kevin Wolf
440 ce1a14dc pbrook
    if (!acb->cluster_offset) {
441 bd28f835 Kevin Wolf
442 585f8587 bellard
        if (bs->backing_hd) {
443 585f8587 bellard
            /* read from the base image */
444 bd28f835 Kevin Wolf
            n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov,
445 bd28f835 Kevin Wolf
                acb->sector_num, acb->cur_nr_sectors);
446 a9465922 bellard
            if (n1 > 0) {
447 66f82cee Kevin Wolf
                BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
448 c87c0672 aliguori
                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
449 7b88e48b Christoph Hellwig
                                    &acb->hd_qiov, acb->cur_nr_sectors,
450 c87c0672 aliguori
                                    qcow_aio_read_cb, acb);
451 ce1a14dc pbrook
                if (acb->hd_aiocb == NULL)
452 f141eafe aliguori
                    goto done;
453 a9465922 bellard
            } else {
454 a32ef786 aliguori
                ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
455 a32ef786 aliguori
                if (ret < 0)
456 f141eafe aliguori
                    goto done;
457 a9465922 bellard
            }
458 585f8587 bellard
        } else {
459 585f8587 bellard
            /* Note: in this case, no need to wait */
460 bd28f835 Kevin Wolf
            qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors);
461 a32ef786 aliguori
            ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
462 a32ef786 aliguori
            if (ret < 0)
463 f141eafe aliguori
                goto done;
464 585f8587 bellard
        }
465 ce1a14dc pbrook
    } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
466 585f8587 bellard
        /* add AIO support for compressed blocks ? */
467 66f82cee Kevin Wolf
        if (qcow2_decompress_cluster(bs, acb->cluster_offset) < 0)
468 f141eafe aliguori
            goto done;
469 bd28f835 Kevin Wolf
470 bd28f835 Kevin Wolf
        qemu_iovec_from_buffer(&acb->hd_qiov,
471 bd28f835 Kevin Wolf
            s->cluster_cache + index_in_cluster * 512,
472 bd28f835 Kevin Wolf
            512 * acb->cur_nr_sectors);
473 bd28f835 Kevin Wolf
474 a32ef786 aliguori
        ret = qcow_schedule_bh(qcow_aio_read_bh, acb);
475 a32ef786 aliguori
        if (ret < 0)
476 f141eafe aliguori
            goto done;
477 585f8587 bellard
    } else {
478 ce1a14dc pbrook
        if ((acb->cluster_offset & 511) != 0) {
479 585f8587 bellard
            ret = -EIO;
480 f141eafe aliguori
            goto done;
481 585f8587 bellard
        }
482 c87c0672 aliguori
483 bd28f835 Kevin Wolf
        if (s->crypt_method) {
484 bd28f835 Kevin Wolf
            /*
485 bd28f835 Kevin Wolf
             * For encrypted images, read everything into a temporary
486 bd28f835 Kevin Wolf
             * contiguous buffer on which the AES functions can work.
487 bd28f835 Kevin Wolf
             */
488 bd28f835 Kevin Wolf
            if (!acb->cluster_data) {
489 bd28f835 Kevin Wolf
                acb->cluster_data =
490 bd28f835 Kevin Wolf
                    qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
491 bd28f835 Kevin Wolf
            }
492 bd28f835 Kevin Wolf
493 bd28f835 Kevin Wolf
            assert(acb->cur_nr_sectors <=
494 bd28f835 Kevin Wolf
                QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
495 bd28f835 Kevin Wolf
            qemu_iovec_reset(&acb->hd_qiov);
496 bd28f835 Kevin Wolf
            qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
497 bd28f835 Kevin Wolf
                512 * acb->cur_nr_sectors);
498 bd28f835 Kevin Wolf
        }
499 bd28f835 Kevin Wolf
500 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
501 66f82cee Kevin Wolf
        acb->hd_aiocb = bdrv_aio_readv(bs->file,
502 5fafdf24 ths
                            (acb->cluster_offset >> 9) + index_in_cluster,
503 7b88e48b Christoph Hellwig
                            &acb->hd_qiov, acb->cur_nr_sectors,
504 7b88e48b Christoph Hellwig
                            qcow_aio_read_cb, acb);
505 171e3d6b Kevin Wolf
        if (acb->hd_aiocb == NULL) {
506 171e3d6b Kevin Wolf
            ret = -EIO;
507 f141eafe aliguori
            goto done;
508 171e3d6b Kevin Wolf
        }
509 f141eafe aliguori
    }
510 f141eafe aliguori
511 f141eafe aliguori
    return;
512 f141eafe aliguori
done:
513 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
514 bd28f835 Kevin Wolf
    qemu_iovec_destroy(&acb->hd_qiov);
515 f141eafe aliguori
    qemu_aio_release(acb);
516 585f8587 bellard
}
517 585f8587 bellard
518 ce1a14dc pbrook
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
519 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
520 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque, int is_write)
521 585f8587 bellard
{
522 ce1a14dc pbrook
    QCowAIOCB *acb;
523 ce1a14dc pbrook
524 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
525 ce1a14dc pbrook
    if (!acb)
526 ce1a14dc pbrook
        return NULL;
527 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
528 ce1a14dc pbrook
    acb->sector_num = sector_num;
529 f141eafe aliguori
    acb->qiov = qiov;
530 bd28f835 Kevin Wolf
531 6f5f060b Kevin Wolf
    qemu_iovec_init(&acb->hd_qiov, qiov->niov);
532 bd28f835 Kevin Wolf
533 bd28f835 Kevin Wolf
    acb->bytes_done = 0;
534 7b88e48b Christoph Hellwig
    acb->remaining_sectors = nb_sectors;
535 7b88e48b Christoph Hellwig
    acb->cur_nr_sectors = 0;
536 ce1a14dc pbrook
    acb->cluster_offset = 0;
537 e976c6a1 aliguori
    acb->l2meta.nb_clusters = 0;
538 72cf2d4f Blue Swirl
    QLIST_INIT(&acb->l2meta.dependent_requests);
539 ce1a14dc pbrook
    return acb;
540 ce1a14dc pbrook
}
541 ce1a14dc pbrook
542 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
543 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
544 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
545 ce1a14dc pbrook
{
546 ce1a14dc pbrook
    QCowAIOCB *acb;
547 ce1a14dc pbrook
548 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
549 ce1a14dc pbrook
    if (!acb)
550 ce1a14dc pbrook
        return NULL;
551 585f8587 bellard
552 585f8587 bellard
    qcow_aio_read_cb(acb, 0);
553 ce1a14dc pbrook
    return &acb->common;
554 585f8587 bellard
}
555 585f8587 bellard
556 f214978a Kevin Wolf
static void qcow_aio_write_cb(void *opaque, int ret);
557 f214978a Kevin Wolf
558 f214978a Kevin Wolf
static void run_dependent_requests(QCowL2Meta *m)
559 f214978a Kevin Wolf
{
560 f214978a Kevin Wolf
    QCowAIOCB *req;
561 f214978a Kevin Wolf
    QCowAIOCB *next;
562 f214978a Kevin Wolf
563 f214978a Kevin Wolf
    /* Take the request off the list of running requests */
564 f214978a Kevin Wolf
    if (m->nb_clusters != 0) {
565 72cf2d4f Blue Swirl
        QLIST_REMOVE(m, next_in_flight);
566 f214978a Kevin Wolf
    }
567 f214978a Kevin Wolf
568 d4c146f0 Stefan Hajnoczi
    /* Restart all dependent requests */
569 d4c146f0 Stefan Hajnoczi
    QLIST_FOREACH_SAFE(req, &m->dependent_requests, next_depend, next) {
570 f214978a Kevin Wolf
        qcow_aio_write_cb(req, 0);
571 f214978a Kevin Wolf
    }
572 f214978a Kevin Wolf
573 f214978a Kevin Wolf
    /* Empty the list for the next part of the request */
574 72cf2d4f Blue Swirl
    QLIST_INIT(&m->dependent_requests);
575 f214978a Kevin Wolf
}
576 f214978a Kevin Wolf
577 585f8587 bellard
static void qcow_aio_write_cb(void *opaque, int ret)
578 585f8587 bellard
{
579 ce1a14dc pbrook
    QCowAIOCB *acb = opaque;
580 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
581 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
582 585f8587 bellard
    int index_in_cluster;
583 095a9c58 aliguori
    int n_end;
584 ce1a14dc pbrook
585 ce1a14dc pbrook
    acb->hd_aiocb = NULL;
586 ce1a14dc pbrook
587 f214978a Kevin Wolf
    if (ret >= 0) {
588 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta);
589 f214978a Kevin Wolf
    }
590 f214978a Kevin Wolf
591 f214978a Kevin Wolf
    run_dependent_requests(&acb->l2meta);
592 f214978a Kevin Wolf
593 f141eafe aliguori
    if (ret < 0)
594 f141eafe aliguori
        goto done;
595 585f8587 bellard
596 7b88e48b Christoph Hellwig
    acb->remaining_sectors -= acb->cur_nr_sectors;
597 7b88e48b Christoph Hellwig
    acb->sector_num += acb->cur_nr_sectors;
598 6f5f060b Kevin Wolf
    acb->bytes_done += acb->cur_nr_sectors * 512;
599 585f8587 bellard
600 7b88e48b Christoph Hellwig
    if (acb->remaining_sectors == 0) {
601 585f8587 bellard
        /* request completed */
602 f141eafe aliguori
        ret = 0;
603 f141eafe aliguori
        goto done;
604 585f8587 bellard
    }
605 3b46e624 ths
606 ce1a14dc pbrook
    index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
607 7b88e48b Christoph Hellwig
    n_end = index_in_cluster + acb->remaining_sectors;
608 095a9c58 aliguori
    if (s->crypt_method &&
609 095a9c58 aliguori
        n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
610 095a9c58 aliguori
        n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
611 095a9c58 aliguori
612 148da7ea Kevin Wolf
    ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
613 7b88e48b Christoph Hellwig
        index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta);
614 148da7ea Kevin Wolf
    if (ret < 0) {
615 148da7ea Kevin Wolf
        goto done;
616 148da7ea Kevin Wolf
    }
617 148da7ea Kevin Wolf
618 148da7ea Kevin Wolf
    acb->cluster_offset = acb->l2meta.cluster_offset;
619 f214978a Kevin Wolf
620 f214978a Kevin Wolf
    /* Need to wait for another request? If so, we are done for now. */
621 148da7ea Kevin Wolf
    if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) {
622 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests,
623 f214978a Kevin Wolf
            acb, next_depend);
624 f214978a Kevin Wolf
        return;
625 f214978a Kevin Wolf
    }
626 f214978a Kevin Wolf
627 148da7ea Kevin Wolf
    assert((acb->cluster_offset & 511) == 0);
628 148da7ea Kevin Wolf
629 6f5f060b Kevin Wolf
    qemu_iovec_reset(&acb->hd_qiov);
630 6f5f060b Kevin Wolf
    qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done,
631 6f5f060b Kevin Wolf
        acb->cur_nr_sectors * 512);
632 6f5f060b Kevin Wolf
633 585f8587 bellard
    if (s->crypt_method) {
634 ce1a14dc pbrook
        if (!acb->cluster_data) {
635 095a9c58 aliguori
            acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
636 095a9c58 aliguori
                                             s->cluster_size);
637 585f8587 bellard
        }
638 6f5f060b Kevin Wolf
639 6f5f060b Kevin Wolf
        assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
640 6f5f060b Kevin Wolf
        qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data);
641 6f5f060b Kevin Wolf
642 6f5f060b Kevin Wolf
        qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data,
643 6f5f060b Kevin Wolf
            acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key);
644 6f5f060b Kevin Wolf
645 6f5f060b Kevin Wolf
        qemu_iovec_reset(&acb->hd_qiov);
646 6f5f060b Kevin Wolf
        qemu_iovec_add(&acb->hd_qiov, acb->cluster_data,
647 6f5f060b Kevin Wolf
            acb->cur_nr_sectors * 512);
648 585f8587 bellard
    }
649 6f5f060b Kevin Wolf
650 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
651 66f82cee Kevin Wolf
    acb->hd_aiocb = bdrv_aio_writev(bs->file,
652 c87c0672 aliguori
                                    (acb->cluster_offset >> 9) + index_in_cluster,
653 7b88e48b Christoph Hellwig
                                    &acb->hd_qiov, acb->cur_nr_sectors,
654 c87c0672 aliguori
                                    qcow_aio_write_cb, acb);
655 171e3d6b Kevin Wolf
    if (acb->hd_aiocb == NULL) {
656 171e3d6b Kevin Wolf
        ret = -EIO;
657 c644db3d Kevin Wolf
        goto fail;
658 171e3d6b Kevin Wolf
    }
659 f141eafe aliguori
660 f141eafe aliguori
    return;
661 f141eafe aliguori
662 c644db3d Kevin Wolf
fail:
663 c644db3d Kevin Wolf
    if (acb->l2meta.nb_clusters != 0) {
664 c644db3d Kevin Wolf
        QLIST_REMOVE(&acb->l2meta, next_in_flight);
665 c644db3d Kevin Wolf
    }
666 f141eafe aliguori
done:
667 f141eafe aliguori
    acb->common.cb(acb->common.opaque, ret);
668 6f5f060b Kevin Wolf
    qemu_iovec_destroy(&acb->hd_qiov);
669 f141eafe aliguori
    qemu_aio_release(acb);
670 585f8587 bellard
}
671 585f8587 bellard
672 f141eafe aliguori
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
673 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
674 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
675 585f8587 bellard
{
676 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
677 ce1a14dc pbrook
    QCowAIOCB *acb;
678 3b46e624 ths
679 585f8587 bellard
    s->cluster_cache_offset = -1; /* disable compressed cache */
680 585f8587 bellard
681 f141eafe aliguori
    acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
682 ce1a14dc pbrook
    if (!acb)
683 ce1a14dc pbrook
        return NULL;
684 3b46e624 ths
685 585f8587 bellard
    qcow_aio_write_cb(acb, 0);
686 ce1a14dc pbrook
    return &acb->common;
687 585f8587 bellard
}
688 585f8587 bellard
689 585f8587 bellard
static void qcow_close(BlockDriverState *bs)
690 585f8587 bellard
{
691 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
692 585f8587 bellard
    qemu_free(s->l1_table);
693 585f8587 bellard
    qemu_free(s->l2_cache);
694 585f8587 bellard
    qemu_free(s->cluster_cache);
695 585f8587 bellard
    qemu_free(s->cluster_data);
696 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
697 585f8587 bellard
}
698 585f8587 bellard
699 756e6736 Kevin Wolf
/*
700 756e6736 Kevin Wolf
 * Updates the variable length parts of the qcow2 header, i.e. the backing file
701 756e6736 Kevin Wolf
 * name and all extensions. qcow2 was not designed to allow such changes, so if
702 756e6736 Kevin Wolf
 * we run out of space (we can only use the first cluster) this function may
703 756e6736 Kevin Wolf
 * fail.
704 756e6736 Kevin Wolf
 *
705 756e6736 Kevin Wolf
 * Returns 0 on success, -errno in error cases.
706 756e6736 Kevin Wolf
 */
707 756e6736 Kevin Wolf
static int qcow2_update_ext_header(BlockDriverState *bs,
708 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
709 756e6736 Kevin Wolf
{
710 756e6736 Kevin Wolf
    size_t backing_file_len = 0;
711 756e6736 Kevin Wolf
    size_t backing_fmt_len = 0;
712 756e6736 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
713 756e6736 Kevin Wolf
    QCowExtension ext_backing_fmt = {0, 0};
714 756e6736 Kevin Wolf
    int ret;
715 756e6736 Kevin Wolf
716 756e6736 Kevin Wolf
    /* Backing file format doesn't make sense without a backing file */
717 756e6736 Kevin Wolf
    if (backing_fmt && !backing_file) {
718 756e6736 Kevin Wolf
        return -EINVAL;
719 756e6736 Kevin Wolf
    }
720 756e6736 Kevin Wolf
721 756e6736 Kevin Wolf
    /* Prepare the backing file format extension if needed */
722 756e6736 Kevin Wolf
    if (backing_fmt) {
723 756e6736 Kevin Wolf
        ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt));
724 756e6736 Kevin Wolf
        ext_backing_fmt.magic = cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT);
725 756e6736 Kevin Wolf
        backing_fmt_len = ((sizeof(ext_backing_fmt)
726 756e6736 Kevin Wolf
            + strlen(backing_fmt) + 7) & ~7);
727 756e6736 Kevin Wolf
    }
728 756e6736 Kevin Wolf
729 756e6736 Kevin Wolf
    /* Check if we can fit the new header into the first cluster */
730 756e6736 Kevin Wolf
    if (backing_file) {
731 756e6736 Kevin Wolf
        backing_file_len = strlen(backing_file);
732 756e6736 Kevin Wolf
    }
733 756e6736 Kevin Wolf
734 756e6736 Kevin Wolf
    size_t header_size = sizeof(QCowHeader) + backing_file_len
735 756e6736 Kevin Wolf
        + backing_fmt_len;
736 756e6736 Kevin Wolf
737 756e6736 Kevin Wolf
    if (header_size > s->cluster_size) {
738 756e6736 Kevin Wolf
        return -ENOSPC;
739 756e6736 Kevin Wolf
    }
740 756e6736 Kevin Wolf
741 756e6736 Kevin Wolf
    /* Rewrite backing file name and qcow2 extensions */
742 756e6736 Kevin Wolf
    size_t ext_size = header_size - sizeof(QCowHeader);
743 756e6736 Kevin Wolf
    uint8_t buf[ext_size];
744 756e6736 Kevin Wolf
    size_t offset = 0;
745 756e6736 Kevin Wolf
    size_t backing_file_offset = 0;
746 756e6736 Kevin Wolf
747 756e6736 Kevin Wolf
    if (backing_file) {
748 756e6736 Kevin Wolf
        if (backing_fmt) {
749 756e6736 Kevin Wolf
            int padding = backing_fmt_len -
750 756e6736 Kevin Wolf
                (sizeof(ext_backing_fmt) + strlen(backing_fmt));
751 756e6736 Kevin Wolf
752 756e6736 Kevin Wolf
            memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
753 756e6736 Kevin Wolf
            offset += sizeof(ext_backing_fmt);
754 756e6736 Kevin Wolf
755 756e6736 Kevin Wolf
            memcpy(buf + offset, backing_fmt, strlen(backing_fmt));
756 756e6736 Kevin Wolf
            offset += strlen(backing_fmt);
757 756e6736 Kevin Wolf
758 756e6736 Kevin Wolf
            memset(buf + offset, 0, padding);
759 756e6736 Kevin Wolf
            offset += padding;
760 756e6736 Kevin Wolf
        }
761 756e6736 Kevin Wolf
762 756e6736 Kevin Wolf
        memcpy(buf + offset, backing_file, backing_file_len);
763 756e6736 Kevin Wolf
        backing_file_offset = sizeof(QCowHeader) + offset;
764 756e6736 Kevin Wolf
    }
765 756e6736 Kevin Wolf
766 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size);
767 756e6736 Kevin Wolf
    if (ret < 0) {
768 756e6736 Kevin Wolf
        goto fail;
769 756e6736 Kevin Wolf
    }
770 756e6736 Kevin Wolf
771 756e6736 Kevin Wolf
    /* Update header fields */
772 756e6736 Kevin Wolf
    uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset);
773 756e6736 Kevin Wolf
    uint32_t be_backing_file_size = cpu_to_be32(backing_file_len);
774 756e6736 Kevin Wolf
775 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset),
776 756e6736 Kevin Wolf
        &be_backing_file_offset, sizeof(uint64_t));
777 756e6736 Kevin Wolf
    if (ret < 0) {
778 756e6736 Kevin Wolf
        goto fail;
779 756e6736 Kevin Wolf
    }
780 756e6736 Kevin Wolf
781 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size),
782 756e6736 Kevin Wolf
        &be_backing_file_size, sizeof(uint32_t));
783 756e6736 Kevin Wolf
    if (ret < 0) {
784 756e6736 Kevin Wolf
        goto fail;
785 756e6736 Kevin Wolf
    }
786 756e6736 Kevin Wolf
787 756e6736 Kevin Wolf
    ret = 0;
788 756e6736 Kevin Wolf
fail:
789 756e6736 Kevin Wolf
    return ret;
790 756e6736 Kevin Wolf
}
791 756e6736 Kevin Wolf
792 756e6736 Kevin Wolf
static int qcow2_change_backing_file(BlockDriverState *bs,
793 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
794 756e6736 Kevin Wolf
{
795 756e6736 Kevin Wolf
    return qcow2_update_ext_header(bs, backing_file, backing_fmt);
796 756e6736 Kevin Wolf
}
797 756e6736 Kevin Wolf
798 a9420734 Kevin Wolf
#if 0
799 73c632ed Kevin Wolf
static int get_bits_from_size(size_t size)
800 73c632ed Kevin Wolf
{
801 73c632ed Kevin Wolf
    int res = 0;
802 73c632ed Kevin Wolf

803 73c632ed Kevin Wolf
    if (size == 0) {
804 73c632ed Kevin Wolf
        return -1;
805 73c632ed Kevin Wolf
    }
806 73c632ed Kevin Wolf

807 73c632ed Kevin Wolf
    while (size != 1) {
808 73c632ed Kevin Wolf
        /* Not a power of two */
809 73c632ed Kevin Wolf
        if (size & 1) {
810 73c632ed Kevin Wolf
            return -1;
811 73c632ed Kevin Wolf
        }
812 73c632ed Kevin Wolf

813 73c632ed Kevin Wolf
        size >>= 1;
814 73c632ed Kevin Wolf
        res++;
815 73c632ed Kevin Wolf
    }
816 73c632ed Kevin Wolf

817 73c632ed Kevin Wolf
    return res;
818 73c632ed Kevin Wolf
}
819 a9420734 Kevin Wolf
#endif
820 73c632ed Kevin Wolf
821 a35e1c17 Kevin Wolf
822 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
823 a35e1c17 Kevin Wolf
{
824 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
825 a35e1c17 Kevin Wolf
    uint64_t offset;
826 a35e1c17 Kevin Wolf
    int num;
827 148da7ea Kevin Wolf
    int ret;
828 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
829 a35e1c17 Kevin Wolf
830 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
831 a35e1c17 Kevin Wolf
    offset = 0;
832 72cf2d4f Blue Swirl
    QLIST_INIT(&meta.dependent_requests);
833 148da7ea Kevin Wolf
    meta.cluster_offset = 0;
834 a35e1c17 Kevin Wolf
835 a35e1c17 Kevin Wolf
    while (nb_sectors) {
836 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
837 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
838 148da7ea Kevin Wolf
        if (ret < 0) {
839 19dbcbf7 Kevin Wolf
            return ret;
840 a35e1c17 Kevin Wolf
        }
841 a35e1c17 Kevin Wolf
842 19dbcbf7 Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &meta);
843 19dbcbf7 Kevin Wolf
        if (ret < 0) {
844 148da7ea Kevin Wolf
            qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
845 19dbcbf7 Kevin Wolf
            return ret;
846 a35e1c17 Kevin Wolf
        }
847 a35e1c17 Kevin Wolf
848 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
849 f214978a Kevin Wolf
         * from the list of in-flight requests */
850 f214978a Kevin Wolf
        run_dependent_requests(&meta);
851 f214978a Kevin Wolf
852 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
853 a35e1c17 Kevin Wolf
854 a35e1c17 Kevin Wolf
        nb_sectors -= num;
855 a35e1c17 Kevin Wolf
        offset += num << 9;
856 a35e1c17 Kevin Wolf
    }
857 a35e1c17 Kevin Wolf
858 a35e1c17 Kevin Wolf
    /*
859 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
860 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
861 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
862 a35e1c17 Kevin Wolf
     */
863 148da7ea Kevin Wolf
    if (meta.cluster_offset != 0) {
864 ea80b906 Kevin Wolf
        uint8_t buf[512];
865 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
866 19dbcbf7 Kevin Wolf
        ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
867 19dbcbf7 Kevin Wolf
        if (ret < 0) {
868 19dbcbf7 Kevin Wolf
            return ret;
869 19dbcbf7 Kevin Wolf
        }
870 a35e1c17 Kevin Wolf
    }
871 a35e1c17 Kevin Wolf
872 a35e1c17 Kevin Wolf
    return 0;
873 a35e1c17 Kevin Wolf
}
874 a35e1c17 Kevin Wolf
875 a9420734 Kevin Wolf
#if 0
876 de5f3f40 Kevin Wolf
static int qcow_create2(const char *filename, int64_t total_size,
877 de5f3f40 Kevin Wolf
                        const char *backing_file, const char *backing_format,
878 de5f3f40 Kevin Wolf
                        int flags, size_t cluster_size, int prealloc)
879 de5f3f40 Kevin Wolf
{
880 de5f3f40 Kevin Wolf

881 de5f3f40 Kevin Wolf
    int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
882 de5f3f40 Kevin Wolf
    int ref_clusters, reftable_clusters, backing_format_len = 0;
883 de5f3f40 Kevin Wolf
    int rounded_ext_bf_len = 0;
884 de5f3f40 Kevin Wolf
    QCowHeader header;
885 de5f3f40 Kevin Wolf
    uint64_t tmp, offset;
886 de5f3f40 Kevin Wolf
    uint64_t old_ref_clusters;
887 de5f3f40 Kevin Wolf
    QCowCreateState s1, *s = &s1;
888 de5f3f40 Kevin Wolf
    QCowExtension ext_bf = {0, 0};
889 de5f3f40 Kevin Wolf
    int ret;
890 de5f3f40 Kevin Wolf

891 de5f3f40 Kevin Wolf
    memset(s, 0, sizeof(*s));
892 de5f3f40 Kevin Wolf

893 de5f3f40 Kevin Wolf
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
894 de5f3f40 Kevin Wolf
    if (fd < 0)
895 de5f3f40 Kevin Wolf
        return -errno;
896 de5f3f40 Kevin Wolf
    memset(&header, 0, sizeof(header));
897 de5f3f40 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
898 de5f3f40 Kevin Wolf
    header.version = cpu_to_be32(QCOW_VERSION);
899 de5f3f40 Kevin Wolf
    header.size = cpu_to_be64(total_size * 512);
900 de5f3f40 Kevin Wolf
    header_size = sizeof(header);
901 de5f3f40 Kevin Wolf
    backing_filename_len = 0;
902 de5f3f40 Kevin Wolf
    if (backing_file) {
903 de5f3f40 Kevin Wolf
        if (backing_format) {
904 de5f3f40 Kevin Wolf
            ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT;
905 de5f3f40 Kevin Wolf
            backing_format_len = strlen(backing_format);
906 de5f3f40 Kevin Wolf
            ext_bf.len = backing_format_len;
907 de5f3f40 Kevin Wolf
            rounded_ext_bf_len = (sizeof(ext_bf) + ext_bf.len + 7) & ~7;
908 de5f3f40 Kevin Wolf
            header_size += rounded_ext_bf_len;
909 de5f3f40 Kevin Wolf
        }
910 de5f3f40 Kevin Wolf
        header.backing_file_offset = cpu_to_be64(header_size);
911 de5f3f40 Kevin Wolf
        backing_filename_len = strlen(backing_file);
912 de5f3f40 Kevin Wolf
        header.backing_file_size = cpu_to_be32(backing_filename_len);
913 de5f3f40 Kevin Wolf
        header_size += backing_filename_len;
914 de5f3f40 Kevin Wolf
    }
915 de5f3f40 Kevin Wolf

916 de5f3f40 Kevin Wolf
    /* Cluster size */
917 de5f3f40 Kevin Wolf
    s->cluster_bits = get_bits_from_size(cluster_size);
918 de5f3f40 Kevin Wolf
    if (s->cluster_bits < MIN_CLUSTER_BITS ||
919 de5f3f40 Kevin Wolf
        s->cluster_bits > MAX_CLUSTER_BITS)
920 de5f3f40 Kevin Wolf
    {
921 de5f3f40 Kevin Wolf
        fprintf(stderr, "Cluster size must be a power of two between "
922 de5f3f40 Kevin Wolf
            "%d and %dk\n",
923 de5f3f40 Kevin Wolf
            1 << MIN_CLUSTER_BITS,
924 de5f3f40 Kevin Wolf
            1 << (MAX_CLUSTER_BITS - 10));
925 de5f3f40 Kevin Wolf
        return -EINVAL;
926 de5f3f40 Kevin Wolf
    }
927 de5f3f40 Kevin Wolf
    s->cluster_size = 1 << s->cluster_bits;
928 de5f3f40 Kevin Wolf

929 de5f3f40 Kevin Wolf
    header.cluster_bits = cpu_to_be32(s->cluster_bits);
930 de5f3f40 Kevin Wolf
    header_size = (header_size + 7) & ~7;
931 de5f3f40 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
932 de5f3f40 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
933 de5f3f40 Kevin Wolf
    } else {
934 de5f3f40 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
935 de5f3f40 Kevin Wolf
    }
936 de5f3f40 Kevin Wolf
    l2_bits = s->cluster_bits - 3;
937 de5f3f40 Kevin Wolf
    shift = s->cluster_bits + l2_bits;
938 de5f3f40 Kevin Wolf
    l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
939 de5f3f40 Kevin Wolf
    offset = align_offset(header_size, s->cluster_size);
940 de5f3f40 Kevin Wolf
    s->l1_table_offset = offset;
941 de5f3f40 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
942 de5f3f40 Kevin Wolf
    header.l1_size = cpu_to_be32(l1_size);
943 de5f3f40 Kevin Wolf
    offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
944 de5f3f40 Kevin Wolf

945 de5f3f40 Kevin Wolf
    /* count how many refcount blocks needed */
946 de5f3f40 Kevin Wolf

947 de5f3f40 Kevin Wolf
#define NUM_CLUSTERS(bytes) \
948 de5f3f40 Kevin Wolf
    (((bytes) + (s->cluster_size) - 1) / (s->cluster_size))
949 de5f3f40 Kevin Wolf

950 de5f3f40 Kevin Wolf
    ref_clusters = NUM_CLUSTERS(NUM_CLUSTERS(offset) * sizeof(uint16_t));
951 de5f3f40 Kevin Wolf

952 de5f3f40 Kevin Wolf
    do {
953 de5f3f40 Kevin Wolf
        uint64_t image_clusters;
954 de5f3f40 Kevin Wolf
        old_ref_clusters = ref_clusters;
955 de5f3f40 Kevin Wolf

956 de5f3f40 Kevin Wolf
        /* Number of clusters used for the refcount table */
957 de5f3f40 Kevin Wolf
        reftable_clusters = NUM_CLUSTERS(ref_clusters * sizeof(uint64_t));
958 de5f3f40 Kevin Wolf

959 de5f3f40 Kevin Wolf
        /* Number of clusters that the whole image will have */
960 de5f3f40 Kevin Wolf
        image_clusters = NUM_CLUSTERS(offset) + ref_clusters
961 de5f3f40 Kevin Wolf
            + reftable_clusters;
962 de5f3f40 Kevin Wolf

963 de5f3f40 Kevin Wolf
        /* Number of refcount blocks needed for the image */
964 de5f3f40 Kevin Wolf
        ref_clusters = NUM_CLUSTERS(image_clusters * sizeof(uint16_t));
965 de5f3f40 Kevin Wolf

966 de5f3f40 Kevin Wolf
    } while (ref_clusters != old_ref_clusters);
967 de5f3f40 Kevin Wolf

968 de5f3f40 Kevin Wolf
    s->refcount_table = qemu_mallocz(reftable_clusters * s->cluster_size);
969 de5f3f40 Kevin Wolf

970 de5f3f40 Kevin Wolf
    s->refcount_table_offset = offset;
971 de5f3f40 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(offset);
972 de5f3f40 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(reftable_clusters);
973 de5f3f40 Kevin Wolf
    offset += (reftable_clusters * s->cluster_size);
974 de5f3f40 Kevin Wolf
    s->refcount_block_offset = offset;
975 de5f3f40 Kevin Wolf

976 de5f3f40 Kevin Wolf
    for (i=0; i < ref_clusters; i++) {
977 de5f3f40 Kevin Wolf
        s->refcount_table[i] = cpu_to_be64(offset);
978 de5f3f40 Kevin Wolf
        offset += s->cluster_size;
979 de5f3f40 Kevin Wolf
    }
980 de5f3f40 Kevin Wolf

981 de5f3f40 Kevin Wolf
    s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size);
982 de5f3f40 Kevin Wolf

983 de5f3f40 Kevin Wolf
    /* update refcounts */
984 de5f3f40 Kevin Wolf
    qcow2_create_refcount_update(s, 0, header_size);
985 de5f3f40 Kevin Wolf
    qcow2_create_refcount_update(s, s->l1_table_offset,
986 de5f3f40 Kevin Wolf
        l1_size * sizeof(uint64_t));
987 de5f3f40 Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_table_offset,
988 de5f3f40 Kevin Wolf
        reftable_clusters * s->cluster_size);
989 de5f3f40 Kevin Wolf
    qcow2_create_refcount_update(s, s->refcount_block_offset,
990 de5f3f40 Kevin Wolf
        ref_clusters * s->cluster_size);
991 de5f3f40 Kevin Wolf

992 de5f3f40 Kevin Wolf
    /* write all the data */
993 de5f3f40 Kevin Wolf
    ret = qemu_write_full(fd, &header, sizeof(header));
994 de5f3f40 Kevin Wolf
    if (ret != sizeof(header)) {
995 de5f3f40 Kevin Wolf
        ret = -errno;
996 de5f3f40 Kevin Wolf
        goto exit;
997 de5f3f40 Kevin Wolf
    }
998 de5f3f40 Kevin Wolf
    if (backing_file) {
999 de5f3f40 Kevin Wolf
        if (backing_format_len) {
1000 de5f3f40 Kevin Wolf
            char zero[16];
1001 de5f3f40 Kevin Wolf
            int padding = rounded_ext_bf_len - (ext_bf.len + sizeof(ext_bf));
1002 de5f3f40 Kevin Wolf

1003 de5f3f40 Kevin Wolf
            memset(zero, 0, sizeof(zero));
1004 de5f3f40 Kevin Wolf
            cpu_to_be32s(&ext_bf.magic);
1005 de5f3f40 Kevin Wolf
            cpu_to_be32s(&ext_bf.len);
1006 de5f3f40 Kevin Wolf
            ret = qemu_write_full(fd, &ext_bf, sizeof(ext_bf));
1007 de5f3f40 Kevin Wolf
            if (ret != sizeof(ext_bf)) {
1008 de5f3f40 Kevin Wolf
                ret = -errno;
1009 de5f3f40 Kevin Wolf
                goto exit;
1010 de5f3f40 Kevin Wolf
            }
1011 de5f3f40 Kevin Wolf
            ret = qemu_write_full(fd, backing_format, backing_format_len);
1012 de5f3f40 Kevin Wolf
            if (ret != backing_format_len) {
1013 de5f3f40 Kevin Wolf
                ret = -errno;
1014 de5f3f40 Kevin Wolf
                goto exit;
1015 de5f3f40 Kevin Wolf
            }
1016 de5f3f40 Kevin Wolf
            if (padding > 0) {
1017 de5f3f40 Kevin Wolf
                ret = qemu_write_full(fd, zero, padding);
1018 de5f3f40 Kevin Wolf
                if (ret != padding) {
1019 de5f3f40 Kevin Wolf
                    ret = -errno;
1020 de5f3f40 Kevin Wolf
                    goto exit;
1021 de5f3f40 Kevin Wolf
                }
1022 de5f3f40 Kevin Wolf
            }
1023 de5f3f40 Kevin Wolf
        }
1024 de5f3f40 Kevin Wolf
        ret = qemu_write_full(fd, backing_file, backing_filename_len);
1025 de5f3f40 Kevin Wolf
        if (ret != backing_filename_len) {
1026 de5f3f40 Kevin Wolf
            ret = -errno;
1027 de5f3f40 Kevin Wolf
            goto exit;
1028 de5f3f40 Kevin Wolf
        }
1029 de5f3f40 Kevin Wolf
    }
1030 de5f3f40 Kevin Wolf
    lseek(fd, s->l1_table_offset, SEEK_SET);
1031 de5f3f40 Kevin Wolf
    tmp = 0;
1032 de5f3f40 Kevin Wolf
    for(i = 0;i < l1_size; i++) {
1033 de5f3f40 Kevin Wolf
        ret = qemu_write_full(fd, &tmp, sizeof(tmp));
1034 de5f3f40 Kevin Wolf
        if (ret != sizeof(tmp)) {
1035 de5f3f40 Kevin Wolf
            ret = -errno;
1036 de5f3f40 Kevin Wolf
            goto exit;
1037 de5f3f40 Kevin Wolf
        }
1038 de5f3f40 Kevin Wolf
    }
1039 de5f3f40 Kevin Wolf
    lseek(fd, s->refcount_table_offset, SEEK_SET);
1040 de5f3f40 Kevin Wolf
    ret = qemu_write_full(fd, s->refcount_table,
1041 de5f3f40 Kevin Wolf
        reftable_clusters * s->cluster_size);
1042 de5f3f40 Kevin Wolf
    if (ret != reftable_clusters * s->cluster_size) {
1043 de5f3f40 Kevin Wolf
        ret = -errno;
1044 de5f3f40 Kevin Wolf
        goto exit;
1045 de5f3f40 Kevin Wolf
    }
1046 de5f3f40 Kevin Wolf

1047 de5f3f40 Kevin Wolf
    lseek(fd, s->refcount_block_offset, SEEK_SET);
1048 de5f3f40 Kevin Wolf
    ret = qemu_write_full(fd, s->refcount_block,
1049 de5f3f40 Kevin Wolf
                    ref_clusters * s->cluster_size);
1050 de5f3f40 Kevin Wolf
    if (ret != ref_clusters * s->cluster_size) {
1051 de5f3f40 Kevin Wolf
        ret = -errno;
1052 de5f3f40 Kevin Wolf
        goto exit;
1053 de5f3f40 Kevin Wolf
    }
1054 de5f3f40 Kevin Wolf

1055 de5f3f40 Kevin Wolf
    ret = 0;
1056 de5f3f40 Kevin Wolf
exit:
1057 de5f3f40 Kevin Wolf
    qemu_free(s->refcount_table);
1058 de5f3f40 Kevin Wolf
    qemu_free(s->refcount_block);
1059 de5f3f40 Kevin Wolf
    close(fd);
1060 de5f3f40 Kevin Wolf

1061 de5f3f40 Kevin Wolf
    /* Preallocate metadata */
1062 de5f3f40 Kevin Wolf
    if (ret == 0 && prealloc) {
1063 de5f3f40 Kevin Wolf
        BlockDriverState *bs;
1064 92b30744 Kevin Wolf
        BlockDriver *drv = bdrv_find_format("qcow2");
1065 de5f3f40 Kevin Wolf
        bs = bdrv_new("");
1066 92b30744 Kevin Wolf
        bdrv_open(bs, filename, BDRV_O_CACHE_WB | BDRV_O_RDWR, drv);
1067 19dbcbf7 Kevin Wolf
        ret = preallocate(bs);
1068 de5f3f40 Kevin Wolf
        bdrv_close(bs);
1069 de5f3f40 Kevin Wolf
    }
1070 de5f3f40 Kevin Wolf

1071 de5f3f40 Kevin Wolf
    return ret;
1072 de5f3f40 Kevin Wolf
}
1073 a9420734 Kevin Wolf
#else
1074 a9420734 Kevin Wolf
static int qcow_create2(const char *filename, int64_t total_size,
1075 a9420734 Kevin Wolf
                        const char *backing_file, const char *backing_format,
1076 a9420734 Kevin Wolf
                        int flags, size_t cluster_size, int prealloc,
1077 a9420734 Kevin Wolf
                        QEMUOptionParameter *options)
1078 a9420734 Kevin Wolf
{
1079 a9420734 Kevin Wolf
    /* Calulate cluster_bits */
1080 a9420734 Kevin Wolf
    int cluster_bits;
1081 a9420734 Kevin Wolf
    cluster_bits = ffs(cluster_size) - 1;
1082 a9420734 Kevin Wolf
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1083 a9420734 Kevin Wolf
        (1 << cluster_bits) != cluster_size)
1084 a9420734 Kevin Wolf
    {
1085 a9420734 Kevin Wolf
        error_report(
1086 a9420734 Kevin Wolf
            "Cluster size must be a power of two between %d and %dk\n",
1087 a9420734 Kevin Wolf
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1088 a9420734 Kevin Wolf
        return -EINVAL;
1089 a9420734 Kevin Wolf
    }
1090 a9420734 Kevin Wolf
1091 a9420734 Kevin Wolf
    /*
1092 a9420734 Kevin Wolf
     * Open the image file and write a minimal qcow2 header.
1093 a9420734 Kevin Wolf
     *
1094 a9420734 Kevin Wolf
     * We keep things simple and start with a zero-sized image. We also
1095 a9420734 Kevin Wolf
     * do without refcount blocks or a L1 table for now. We'll fix the
1096 a9420734 Kevin Wolf
     * inconsistency later.
1097 a9420734 Kevin Wolf
     *
1098 a9420734 Kevin Wolf
     * We do need a refcount table because growing the refcount table means
1099 a9420734 Kevin Wolf
     * allocating two new refcount blocks - the seconds of which would be at
1100 a9420734 Kevin Wolf
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1101 a9420734 Kevin Wolf
     * size for any qcow2 image.
1102 a9420734 Kevin Wolf
     */
1103 a9420734 Kevin Wolf
    BlockDriverState* bs;
1104 a9420734 Kevin Wolf
    QCowHeader header;
1105 a9420734 Kevin Wolf
    uint8_t* refcount_table;
1106 a9420734 Kevin Wolf
    int ret;
1107 a9420734 Kevin Wolf
1108 a9420734 Kevin Wolf
    ret = bdrv_create_file(filename, options);
1109 a9420734 Kevin Wolf
    if (ret < 0) {
1110 a9420734 Kevin Wolf
        return ret;
1111 a9420734 Kevin Wolf
    }
1112 a9420734 Kevin Wolf
1113 a9420734 Kevin Wolf
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
1114 a9420734 Kevin Wolf
    if (ret < 0) {
1115 a9420734 Kevin Wolf
        return ret;
1116 a9420734 Kevin Wolf
    }
1117 a9420734 Kevin Wolf
1118 a9420734 Kevin Wolf
    /* Write the header */
1119 a9420734 Kevin Wolf
    memset(&header, 0, sizeof(header));
1120 a9420734 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
1121 a9420734 Kevin Wolf
    header.version = cpu_to_be32(QCOW_VERSION);
1122 a9420734 Kevin Wolf
    header.cluster_bits = cpu_to_be32(cluster_bits);
1123 a9420734 Kevin Wolf
    header.size = cpu_to_be64(0);
1124 a9420734 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(0);
1125 a9420734 Kevin Wolf
    header.l1_size = cpu_to_be32(0);
1126 a9420734 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(cluster_size);
1127 a9420734 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(1);
1128 a9420734 Kevin Wolf
1129 a9420734 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
1130 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1131 a9420734 Kevin Wolf
    } else {
1132 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1133 a9420734 Kevin Wolf
    }
1134 a9420734 Kevin Wolf
1135 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1136 a9420734 Kevin Wolf
    if (ret < 0) {
1137 a9420734 Kevin Wolf
        goto out;
1138 a9420734 Kevin Wolf
    }
1139 a9420734 Kevin Wolf
1140 a9420734 Kevin Wolf
    /* Write an empty refcount table */
1141 a9420734 Kevin Wolf
    refcount_table = qemu_mallocz(cluster_size);
1142 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
1143 a9420734 Kevin Wolf
    qemu_free(refcount_table);
1144 a9420734 Kevin Wolf
1145 a9420734 Kevin Wolf
    if (ret < 0) {
1146 a9420734 Kevin Wolf
        goto out;
1147 a9420734 Kevin Wolf
    }
1148 a9420734 Kevin Wolf
1149 a9420734 Kevin Wolf
    bdrv_close(bs);
1150 a9420734 Kevin Wolf
1151 a9420734 Kevin Wolf
    /*
1152 a9420734 Kevin Wolf
     * And now open the image and make it consistent first (i.e. increase the
1153 a9420734 Kevin Wolf
     * refcount of the cluster that is occupied by the header and the refcount
1154 a9420734 Kevin Wolf
     * table)
1155 a9420734 Kevin Wolf
     */
1156 a9420734 Kevin Wolf
    BlockDriver* drv = bdrv_find_format("qcow2");
1157 a9420734 Kevin Wolf
    assert(drv != NULL);
1158 a9420734 Kevin Wolf
    ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv);
1159 a9420734 Kevin Wolf
    if (ret < 0) {
1160 a9420734 Kevin Wolf
        goto out;
1161 a9420734 Kevin Wolf
    }
1162 a9420734 Kevin Wolf
1163 a9420734 Kevin Wolf
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1164 a9420734 Kevin Wolf
    if (ret < 0) {
1165 a9420734 Kevin Wolf
        goto out;
1166 a9420734 Kevin Wolf
1167 a9420734 Kevin Wolf
    } else if (ret != 0) {
1168 a9420734 Kevin Wolf
        error_report("Huh, first cluster in empty image is already in use?");
1169 a9420734 Kevin Wolf
        abort();
1170 a9420734 Kevin Wolf
    }
1171 a9420734 Kevin Wolf
1172 a9420734 Kevin Wolf
    /* Okay, now that we have a valid image, let's give it the right size */
1173 a9420734 Kevin Wolf
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1174 a9420734 Kevin Wolf
    if (ret < 0) {
1175 a9420734 Kevin Wolf
        goto out;
1176 a9420734 Kevin Wolf
    }
1177 a9420734 Kevin Wolf
1178 a9420734 Kevin Wolf
    /* Want a backing file? There you go.*/
1179 a9420734 Kevin Wolf
    if (backing_file) {
1180 a9420734 Kevin Wolf
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1181 a9420734 Kevin Wolf
        if (ret < 0) {
1182 a9420734 Kevin Wolf
            goto out;
1183 a9420734 Kevin Wolf
        }
1184 a9420734 Kevin Wolf
    }
1185 a9420734 Kevin Wolf
1186 a9420734 Kevin Wolf
    /* And if we're supposed to preallocate metadata, do that now */
1187 a9420734 Kevin Wolf
    if (prealloc) {
1188 a9420734 Kevin Wolf
        ret = preallocate(bs);
1189 a9420734 Kevin Wolf
        if (ret < 0) {
1190 a9420734 Kevin Wolf
            goto out;
1191 a9420734 Kevin Wolf
        }
1192 a9420734 Kevin Wolf
    }
1193 a9420734 Kevin Wolf
1194 a9420734 Kevin Wolf
    ret = 0;
1195 a9420734 Kevin Wolf
out:
1196 a9420734 Kevin Wolf
    bdrv_delete(bs);
1197 a9420734 Kevin Wolf
    return ret;
1198 a9420734 Kevin Wolf
}
1199 a9420734 Kevin Wolf
#endif
1200 de5f3f40 Kevin Wolf
1201 de5f3f40 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
1202 de5f3f40 Kevin Wolf
{
1203 de5f3f40 Kevin Wolf
    const char *backing_file = NULL;
1204 de5f3f40 Kevin Wolf
    const char *backing_fmt = NULL;
1205 de5f3f40 Kevin Wolf
    uint64_t sectors = 0;
1206 de5f3f40 Kevin Wolf
    int flags = 0;
1207 de5f3f40 Kevin Wolf
    size_t cluster_size = 65536;
1208 de5f3f40 Kevin Wolf
    int prealloc = 0;
1209 de5f3f40 Kevin Wolf
1210 de5f3f40 Kevin Wolf
    /* Read out options */
1211 de5f3f40 Kevin Wolf
    while (options && options->name) {
1212 de5f3f40 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1213 de5f3f40 Kevin Wolf
            sectors = options->value.n / 512;
1214 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1215 de5f3f40 Kevin Wolf
            backing_file = options->value.s;
1216 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1217 de5f3f40 Kevin Wolf
            backing_fmt = options->value.s;
1218 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1219 de5f3f40 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1220 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1221 de5f3f40 Kevin Wolf
            if (options->value.n) {
1222 de5f3f40 Kevin Wolf
                cluster_size = options->value.n;
1223 de5f3f40 Kevin Wolf
            }
1224 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1225 de5f3f40 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1226 de5f3f40 Kevin Wolf
                prealloc = 0;
1227 de5f3f40 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
1228 de5f3f40 Kevin Wolf
                prealloc = 1;
1229 de5f3f40 Kevin Wolf
            } else {
1230 de5f3f40 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1231 de5f3f40 Kevin Wolf
                    options->value.s);
1232 de5f3f40 Kevin Wolf
                return -EINVAL;
1233 de5f3f40 Kevin Wolf
            }
1234 de5f3f40 Kevin Wolf
        }
1235 de5f3f40 Kevin Wolf
        options++;
1236 de5f3f40 Kevin Wolf
    }
1237 de5f3f40 Kevin Wolf
1238 de5f3f40 Kevin Wolf
    if (backing_file && prealloc) {
1239 de5f3f40 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1240 de5f3f40 Kevin Wolf
            "the same time\n");
1241 de5f3f40 Kevin Wolf
        return -EINVAL;
1242 de5f3f40 Kevin Wolf
    }
1243 de5f3f40 Kevin Wolf
1244 de5f3f40 Kevin Wolf
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
1245 a9420734 Kevin Wolf
        cluster_size, prealloc, options);
1246 de5f3f40 Kevin Wolf
}
1247 de5f3f40 Kevin Wolf
1248 20d97356 Blue Swirl
static int qcow_make_empty(BlockDriverState *bs)
1249 20d97356 Blue Swirl
{
1250 20d97356 Blue Swirl
#if 0
1251 20d97356 Blue Swirl
    /* XXX: not correct */
1252 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1253 20d97356 Blue Swirl
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1254 20d97356 Blue Swirl
    int ret;
1255 20d97356 Blue Swirl

1256 20d97356 Blue Swirl
    memset(s->l1_table, 0, l1_length);
1257 66f82cee Kevin Wolf
    if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1258 20d97356 Blue Swirl
        return -1;
1259 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1260 20d97356 Blue Swirl
    if (ret < 0)
1261 20d97356 Blue Swirl
        return ret;
1262 20d97356 Blue Swirl

1263 20d97356 Blue Swirl
    l2_cache_reset(bs);
1264 20d97356 Blue Swirl
#endif
1265 20d97356 Blue Swirl
    return 0;
1266 20d97356 Blue Swirl
}
1267 20d97356 Blue Swirl
1268 419b19d9 Stefan Hajnoczi
static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1269 419b19d9 Stefan Hajnoczi
{
1270 419b19d9 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
1271 419b19d9 Stefan Hajnoczi
    int ret, new_l1_size;
1272 419b19d9 Stefan Hajnoczi
1273 419b19d9 Stefan Hajnoczi
    if (offset & 511) {
1274 419b19d9 Stefan Hajnoczi
        return -EINVAL;
1275 419b19d9 Stefan Hajnoczi
    }
1276 419b19d9 Stefan Hajnoczi
1277 419b19d9 Stefan Hajnoczi
    /* cannot proceed if image has snapshots */
1278 419b19d9 Stefan Hajnoczi
    if (s->nb_snapshots) {
1279 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1280 419b19d9 Stefan Hajnoczi
    }
1281 419b19d9 Stefan Hajnoczi
1282 419b19d9 Stefan Hajnoczi
    /* shrinking is currently not supported */
1283 419b19d9 Stefan Hajnoczi
    if (offset < bs->total_sectors * 512) {
1284 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1285 419b19d9 Stefan Hajnoczi
    }
1286 419b19d9 Stefan Hajnoczi
1287 419b19d9 Stefan Hajnoczi
    new_l1_size = size_to_l1(s, offset);
1288 72893756 Stefan Hajnoczi
    ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1289 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1290 419b19d9 Stefan Hajnoczi
        return ret;
1291 419b19d9 Stefan Hajnoczi
    }
1292 419b19d9 Stefan Hajnoczi
1293 419b19d9 Stefan Hajnoczi
    /* write updated header.size */
1294 419b19d9 Stefan Hajnoczi
    offset = cpu_to_be64(offset);
1295 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1296 8b3b7206 Kevin Wolf
                           &offset, sizeof(uint64_t));
1297 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1298 419b19d9 Stefan Hajnoczi
        return ret;
1299 419b19d9 Stefan Hajnoczi
    }
1300 419b19d9 Stefan Hajnoczi
1301 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = new_l1_size;
1302 419b19d9 Stefan Hajnoczi
    return 0;
1303 419b19d9 Stefan Hajnoczi
}
1304 419b19d9 Stefan Hajnoczi
1305 20d97356 Blue Swirl
/* XXX: put compressed sectors first, then all the cluster aligned
1306 20d97356 Blue Swirl
   tables to avoid losing bytes in alignment */
1307 20d97356 Blue Swirl
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1308 20d97356 Blue Swirl
                                 const uint8_t *buf, int nb_sectors)
1309 20d97356 Blue Swirl
{
1310 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1311 20d97356 Blue Swirl
    z_stream strm;
1312 20d97356 Blue Swirl
    int ret, out_len;
1313 20d97356 Blue Swirl
    uint8_t *out_buf;
1314 20d97356 Blue Swirl
    uint64_t cluster_offset;
1315 20d97356 Blue Swirl
1316 20d97356 Blue Swirl
    if (nb_sectors == 0) {
1317 20d97356 Blue Swirl
        /* align end of file to a sector boundary to ease reading with
1318 20d97356 Blue Swirl
           sector based I/Os */
1319 66f82cee Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
1320 20d97356 Blue Swirl
        cluster_offset = (cluster_offset + 511) & ~511;
1321 66f82cee Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset);
1322 20d97356 Blue Swirl
        return 0;
1323 20d97356 Blue Swirl
    }
1324 20d97356 Blue Swirl
1325 20d97356 Blue Swirl
    if (nb_sectors != s->cluster_sectors)
1326 20d97356 Blue Swirl
        return -EINVAL;
1327 20d97356 Blue Swirl
1328 20d97356 Blue Swirl
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1329 20d97356 Blue Swirl
1330 20d97356 Blue Swirl
    /* best compression, small window, no zlib header */
1331 20d97356 Blue Swirl
    memset(&strm, 0, sizeof(strm));
1332 20d97356 Blue Swirl
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1333 20d97356 Blue Swirl
                       Z_DEFLATED, -12,
1334 20d97356 Blue Swirl
                       9, Z_DEFAULT_STRATEGY);
1335 20d97356 Blue Swirl
    if (ret != 0) {
1336 20d97356 Blue Swirl
        qemu_free(out_buf);
1337 20d97356 Blue Swirl
        return -1;
1338 20d97356 Blue Swirl
    }
1339 20d97356 Blue Swirl
1340 20d97356 Blue Swirl
    strm.avail_in = s->cluster_size;
1341 20d97356 Blue Swirl
    strm.next_in = (uint8_t *)buf;
1342 20d97356 Blue Swirl
    strm.avail_out = s->cluster_size;
1343 20d97356 Blue Swirl
    strm.next_out = out_buf;
1344 20d97356 Blue Swirl
1345 20d97356 Blue Swirl
    ret = deflate(&strm, Z_FINISH);
1346 20d97356 Blue Swirl
    if (ret != Z_STREAM_END && ret != Z_OK) {
1347 20d97356 Blue Swirl
        qemu_free(out_buf);
1348 20d97356 Blue Swirl
        deflateEnd(&strm);
1349 20d97356 Blue Swirl
        return -1;
1350 20d97356 Blue Swirl
    }
1351 20d97356 Blue Swirl
    out_len = strm.next_out - out_buf;
1352 20d97356 Blue Swirl
1353 20d97356 Blue Swirl
    deflateEnd(&strm);
1354 20d97356 Blue Swirl
1355 20d97356 Blue Swirl
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1356 20d97356 Blue Swirl
        /* could not compress: write normal cluster */
1357 20d97356 Blue Swirl
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1358 20d97356 Blue Swirl
    } else {
1359 20d97356 Blue Swirl
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1360 20d97356 Blue Swirl
            sector_num << 9, out_len);
1361 20d97356 Blue Swirl
        if (!cluster_offset)
1362 20d97356 Blue Swirl
            return -1;
1363 20d97356 Blue Swirl
        cluster_offset &= s->cluster_offset_mask;
1364 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1365 66f82cee Kevin Wolf
        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
1366 20d97356 Blue Swirl
            qemu_free(out_buf);
1367 20d97356 Blue Swirl
            return -1;
1368 20d97356 Blue Swirl
        }
1369 20d97356 Blue Swirl
    }
1370 20d97356 Blue Swirl
1371 20d97356 Blue Swirl
    qemu_free(out_buf);
1372 20d97356 Blue Swirl
    return 0;
1373 20d97356 Blue Swirl
}
1374 20d97356 Blue Swirl
1375 20d97356 Blue Swirl
static void qcow_flush(BlockDriverState *bs)
1376 20d97356 Blue Swirl
{
1377 66f82cee Kevin Wolf
    bdrv_flush(bs->file);
1378 20d97356 Blue Swirl
}
1379 20d97356 Blue Swirl
1380 20d97356 Blue Swirl
static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
1381 20d97356 Blue Swirl
         BlockDriverCompletionFunc *cb, void *opaque)
1382 20d97356 Blue Swirl
{
1383 66f82cee Kevin Wolf
    return bdrv_aio_flush(bs->file, cb, opaque);
1384 20d97356 Blue Swirl
}
1385 20d97356 Blue Swirl
1386 20d97356 Blue Swirl
static int64_t qcow_vm_state_offset(BDRVQcowState *s)
1387 20d97356 Blue Swirl
{
1388 20d97356 Blue Swirl
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1389 20d97356 Blue Swirl
}
1390 20d97356 Blue Swirl
1391 20d97356 Blue Swirl
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1392 20d97356 Blue Swirl
{
1393 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1394 20d97356 Blue Swirl
    bdi->cluster_size = s->cluster_size;
1395 20d97356 Blue Swirl
    bdi->vm_state_offset = qcow_vm_state_offset(s);
1396 20d97356 Blue Swirl
    return 0;
1397 20d97356 Blue Swirl
}
1398 20d97356 Blue Swirl
1399 20d97356 Blue Swirl
1400 9ac228e0 Kevin Wolf
static int qcow_check(BlockDriverState *bs, BdrvCheckResult *result)
1401 20d97356 Blue Swirl
{
1402 9ac228e0 Kevin Wolf
    return qcow2_check_refcounts(bs, result);
1403 20d97356 Blue Swirl
}
1404 20d97356 Blue Swirl
1405 20d97356 Blue Swirl
#if 0
1406 20d97356 Blue Swirl
static void dump_refcounts(BlockDriverState *bs)
1407 20d97356 Blue Swirl
{
1408 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1409 20d97356 Blue Swirl
    int64_t nb_clusters, k, k1, size;
1410 20d97356 Blue Swirl
    int refcount;
1411 20d97356 Blue Swirl

1412 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1413 20d97356 Blue Swirl
    nb_clusters = size_to_clusters(s, size);
1414 20d97356 Blue Swirl
    for(k = 0; k < nb_clusters;) {
1415 20d97356 Blue Swirl
        k1 = k;
1416 20d97356 Blue Swirl
        refcount = get_refcount(bs, k);
1417 20d97356 Blue Swirl
        k++;
1418 20d97356 Blue Swirl
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1419 20d97356 Blue Swirl
            k++;
1420 0bfcd599 Blue Swirl
        printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1421 0bfcd599 Blue Swirl
               k - k1);
1422 20d97356 Blue Swirl
    }
1423 20d97356 Blue Swirl
}
1424 20d97356 Blue Swirl
#endif
1425 20d97356 Blue Swirl
1426 20d97356 Blue Swirl
static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1427 20d97356 Blue Swirl
                           int64_t pos, int size)
1428 20d97356 Blue Swirl
{
1429 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1430 20d97356 Blue Swirl
    int growable = bs->growable;
1431 20d97356 Blue Swirl
    int ret;
1432 20d97356 Blue Swirl
1433 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1434 20d97356 Blue Swirl
    bs->growable = 1;
1435 20d97356 Blue Swirl
    ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
1436 20d97356 Blue Swirl
    bs->growable = growable;
1437 20d97356 Blue Swirl
1438 20d97356 Blue Swirl
    return ret;
1439 20d97356 Blue Swirl
}
1440 20d97356 Blue Swirl
1441 20d97356 Blue Swirl
static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1442 20d97356 Blue Swirl
                           int64_t pos, int size)
1443 20d97356 Blue Swirl
{
1444 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1445 20d97356 Blue Swirl
    int growable = bs->growable;
1446 20d97356 Blue Swirl
    int ret;
1447 20d97356 Blue Swirl
1448 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1449 20d97356 Blue Swirl
    bs->growable = 1;
1450 20d97356 Blue Swirl
    ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
1451 20d97356 Blue Swirl
    bs->growable = growable;
1452 20d97356 Blue Swirl
1453 20d97356 Blue Swirl
    return ret;
1454 20d97356 Blue Swirl
}
1455 20d97356 Blue Swirl
1456 20d97356 Blue Swirl
static QEMUOptionParameter qcow_create_options[] = {
1457 20d97356 Blue Swirl
    {
1458 20d97356 Blue Swirl
        .name = BLOCK_OPT_SIZE,
1459 20d97356 Blue Swirl
        .type = OPT_SIZE,
1460 20d97356 Blue Swirl
        .help = "Virtual disk size"
1461 20d97356 Blue Swirl
    },
1462 20d97356 Blue Swirl
    {
1463 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FILE,
1464 20d97356 Blue Swirl
        .type = OPT_STRING,
1465 20d97356 Blue Swirl
        .help = "File name of a base image"
1466 20d97356 Blue Swirl
    },
1467 20d97356 Blue Swirl
    {
1468 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FMT,
1469 20d97356 Blue Swirl
        .type = OPT_STRING,
1470 20d97356 Blue Swirl
        .help = "Image format of the base image"
1471 20d97356 Blue Swirl
    },
1472 20d97356 Blue Swirl
    {
1473 20d97356 Blue Swirl
        .name = BLOCK_OPT_ENCRYPT,
1474 20d97356 Blue Swirl
        .type = OPT_FLAG,
1475 20d97356 Blue Swirl
        .help = "Encrypt the image"
1476 20d97356 Blue Swirl
    },
1477 20d97356 Blue Swirl
    {
1478 20d97356 Blue Swirl
        .name = BLOCK_OPT_CLUSTER_SIZE,
1479 20d97356 Blue Swirl
        .type = OPT_SIZE,
1480 20d97356 Blue Swirl
        .help = "qcow2 cluster size"
1481 20d97356 Blue Swirl
    },
1482 20d97356 Blue Swirl
    {
1483 20d97356 Blue Swirl
        .name = BLOCK_OPT_PREALLOC,
1484 20d97356 Blue Swirl
        .type = OPT_STRING,
1485 20d97356 Blue Swirl
        .help = "Preallocation mode (allowed values: off, metadata)"
1486 20d97356 Blue Swirl
    },
1487 20d97356 Blue Swirl
    { NULL }
1488 20d97356 Blue Swirl
};
1489 20d97356 Blue Swirl
1490 20d97356 Blue Swirl
static BlockDriver bdrv_qcow2 = {
1491 20d97356 Blue Swirl
    .format_name        = "qcow2",
1492 20d97356 Blue Swirl
    .instance_size        = sizeof(BDRVQcowState),
1493 20d97356 Blue Swirl
    .bdrv_probe                = qcow_probe,
1494 20d97356 Blue Swirl
    .bdrv_open                = qcow_open,
1495 20d97356 Blue Swirl
    .bdrv_close                = qcow_close,
1496 20d97356 Blue Swirl
    .bdrv_create        = qcow_create,
1497 20d97356 Blue Swirl
    .bdrv_flush                = qcow_flush,
1498 20d97356 Blue Swirl
    .bdrv_is_allocated        = qcow_is_allocated,
1499 20d97356 Blue Swirl
    .bdrv_set_key        = qcow_set_key,
1500 20d97356 Blue Swirl
    .bdrv_make_empty        = qcow_make_empty,
1501 20d97356 Blue Swirl
1502 20d97356 Blue Swirl
    .bdrv_aio_readv        = qcow_aio_readv,
1503 20d97356 Blue Swirl
    .bdrv_aio_writev        = qcow_aio_writev,
1504 20d97356 Blue Swirl
    .bdrv_aio_flush        = qcow_aio_flush,
1505 419b19d9 Stefan Hajnoczi
1506 419b19d9 Stefan Hajnoczi
    .bdrv_truncate          = qcow2_truncate,
1507 419b19d9 Stefan Hajnoczi
    .bdrv_write_compressed  = qcow_write_compressed,
1508 20d97356 Blue Swirl
1509 20d97356 Blue Swirl
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1510 20d97356 Blue Swirl
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1511 20d97356 Blue Swirl
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1512 20d97356 Blue Swirl
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1513 20d97356 Blue Swirl
    .bdrv_get_info        = qcow_get_info,
1514 20d97356 Blue Swirl
1515 20d97356 Blue Swirl
    .bdrv_save_vmstate    = qcow_save_vmstate,
1516 20d97356 Blue Swirl
    .bdrv_load_vmstate    = qcow_load_vmstate,
1517 20d97356 Blue Swirl
1518 20d97356 Blue Swirl
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1519 20d97356 Blue Swirl
1520 20d97356 Blue Swirl
    .create_options = qcow_create_options,
1521 20d97356 Blue Swirl
    .bdrv_check = qcow_check,
1522 20d97356 Blue Swirl
};
1523 20d97356 Blue Swirl
1524 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1525 5efa9d5a Anthony Liguori
{
1526 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1527 5efa9d5a Anthony Liguori
}
1528 5efa9d5a Anthony Liguori
1529 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);