Statistics
| Branch: | Revision:

root / block / qcow2.c @ edcdd562

History | View | Annotate | Download (38 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 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
799 a35e1c17 Kevin Wolf
{
800 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
801 a35e1c17 Kevin Wolf
    uint64_t offset;
802 a35e1c17 Kevin Wolf
    int num;
803 148da7ea Kevin Wolf
    int ret;
804 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
805 a35e1c17 Kevin Wolf
806 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
807 a35e1c17 Kevin Wolf
    offset = 0;
808 72cf2d4f Blue Swirl
    QLIST_INIT(&meta.dependent_requests);
809 148da7ea Kevin Wolf
    meta.cluster_offset = 0;
810 a35e1c17 Kevin Wolf
811 a35e1c17 Kevin Wolf
    while (nb_sectors) {
812 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
813 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
814 148da7ea Kevin Wolf
        if (ret < 0) {
815 19dbcbf7 Kevin Wolf
            return ret;
816 a35e1c17 Kevin Wolf
        }
817 a35e1c17 Kevin Wolf
818 19dbcbf7 Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &meta);
819 19dbcbf7 Kevin Wolf
        if (ret < 0) {
820 148da7ea Kevin Wolf
            qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
821 19dbcbf7 Kevin Wolf
            return ret;
822 a35e1c17 Kevin Wolf
        }
823 a35e1c17 Kevin Wolf
824 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
825 f214978a Kevin Wolf
         * from the list of in-flight requests */
826 f214978a Kevin Wolf
        run_dependent_requests(&meta);
827 f214978a Kevin Wolf
828 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
829 a35e1c17 Kevin Wolf
830 a35e1c17 Kevin Wolf
        nb_sectors -= num;
831 a35e1c17 Kevin Wolf
        offset += num << 9;
832 a35e1c17 Kevin Wolf
    }
833 a35e1c17 Kevin Wolf
834 a35e1c17 Kevin Wolf
    /*
835 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
836 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
837 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
838 a35e1c17 Kevin Wolf
     */
839 148da7ea Kevin Wolf
    if (meta.cluster_offset != 0) {
840 ea80b906 Kevin Wolf
        uint8_t buf[512];
841 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
842 19dbcbf7 Kevin Wolf
        ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
843 19dbcbf7 Kevin Wolf
        if (ret < 0) {
844 19dbcbf7 Kevin Wolf
            return ret;
845 19dbcbf7 Kevin Wolf
        }
846 a35e1c17 Kevin Wolf
    }
847 a35e1c17 Kevin Wolf
848 a35e1c17 Kevin Wolf
    return 0;
849 a35e1c17 Kevin Wolf
}
850 a35e1c17 Kevin Wolf
851 a9420734 Kevin Wolf
static int qcow_create2(const char *filename, int64_t total_size,
852 a9420734 Kevin Wolf
                        const char *backing_file, const char *backing_format,
853 a9420734 Kevin Wolf
                        int flags, size_t cluster_size, int prealloc,
854 a9420734 Kevin Wolf
                        QEMUOptionParameter *options)
855 a9420734 Kevin Wolf
{
856 a9420734 Kevin Wolf
    /* Calulate cluster_bits */
857 a9420734 Kevin Wolf
    int cluster_bits;
858 a9420734 Kevin Wolf
    cluster_bits = ffs(cluster_size) - 1;
859 a9420734 Kevin Wolf
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
860 a9420734 Kevin Wolf
        (1 << cluster_bits) != cluster_size)
861 a9420734 Kevin Wolf
    {
862 a9420734 Kevin Wolf
        error_report(
863 a9420734 Kevin Wolf
            "Cluster size must be a power of two between %d and %dk\n",
864 a9420734 Kevin Wolf
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
865 a9420734 Kevin Wolf
        return -EINVAL;
866 a9420734 Kevin Wolf
    }
867 a9420734 Kevin Wolf
868 a9420734 Kevin Wolf
    /*
869 a9420734 Kevin Wolf
     * Open the image file and write a minimal qcow2 header.
870 a9420734 Kevin Wolf
     *
871 a9420734 Kevin Wolf
     * We keep things simple and start with a zero-sized image. We also
872 a9420734 Kevin Wolf
     * do without refcount blocks or a L1 table for now. We'll fix the
873 a9420734 Kevin Wolf
     * inconsistency later.
874 a9420734 Kevin Wolf
     *
875 a9420734 Kevin Wolf
     * We do need a refcount table because growing the refcount table means
876 a9420734 Kevin Wolf
     * allocating two new refcount blocks - the seconds of which would be at
877 a9420734 Kevin Wolf
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
878 a9420734 Kevin Wolf
     * size for any qcow2 image.
879 a9420734 Kevin Wolf
     */
880 a9420734 Kevin Wolf
    BlockDriverState* bs;
881 a9420734 Kevin Wolf
    QCowHeader header;
882 a9420734 Kevin Wolf
    uint8_t* refcount_table;
883 a9420734 Kevin Wolf
    int ret;
884 a9420734 Kevin Wolf
885 a9420734 Kevin Wolf
    ret = bdrv_create_file(filename, options);
886 a9420734 Kevin Wolf
    if (ret < 0) {
887 a9420734 Kevin Wolf
        return ret;
888 a9420734 Kevin Wolf
    }
889 a9420734 Kevin Wolf
890 a9420734 Kevin Wolf
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
891 a9420734 Kevin Wolf
    if (ret < 0) {
892 a9420734 Kevin Wolf
        return ret;
893 a9420734 Kevin Wolf
    }
894 a9420734 Kevin Wolf
895 a9420734 Kevin Wolf
    /* Write the header */
896 a9420734 Kevin Wolf
    memset(&header, 0, sizeof(header));
897 a9420734 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
898 a9420734 Kevin Wolf
    header.version = cpu_to_be32(QCOW_VERSION);
899 a9420734 Kevin Wolf
    header.cluster_bits = cpu_to_be32(cluster_bits);
900 a9420734 Kevin Wolf
    header.size = cpu_to_be64(0);
901 a9420734 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(0);
902 a9420734 Kevin Wolf
    header.l1_size = cpu_to_be32(0);
903 a9420734 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(cluster_size);
904 a9420734 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(1);
905 a9420734 Kevin Wolf
906 a9420734 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
907 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
908 a9420734 Kevin Wolf
    } else {
909 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
910 a9420734 Kevin Wolf
    }
911 a9420734 Kevin Wolf
912 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
913 a9420734 Kevin Wolf
    if (ret < 0) {
914 a9420734 Kevin Wolf
        goto out;
915 a9420734 Kevin Wolf
    }
916 a9420734 Kevin Wolf
917 a9420734 Kevin Wolf
    /* Write an empty refcount table */
918 a9420734 Kevin Wolf
    refcount_table = qemu_mallocz(cluster_size);
919 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
920 a9420734 Kevin Wolf
    qemu_free(refcount_table);
921 a9420734 Kevin Wolf
922 a9420734 Kevin Wolf
    if (ret < 0) {
923 a9420734 Kevin Wolf
        goto out;
924 a9420734 Kevin Wolf
    }
925 a9420734 Kevin Wolf
926 a9420734 Kevin Wolf
    bdrv_close(bs);
927 a9420734 Kevin Wolf
928 a9420734 Kevin Wolf
    /*
929 a9420734 Kevin Wolf
     * And now open the image and make it consistent first (i.e. increase the
930 a9420734 Kevin Wolf
     * refcount of the cluster that is occupied by the header and the refcount
931 a9420734 Kevin Wolf
     * table)
932 a9420734 Kevin Wolf
     */
933 a9420734 Kevin Wolf
    BlockDriver* drv = bdrv_find_format("qcow2");
934 a9420734 Kevin Wolf
    assert(drv != NULL);
935 a9420734 Kevin Wolf
    ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv);
936 a9420734 Kevin Wolf
    if (ret < 0) {
937 a9420734 Kevin Wolf
        goto out;
938 a9420734 Kevin Wolf
    }
939 a9420734 Kevin Wolf
940 a9420734 Kevin Wolf
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
941 a9420734 Kevin Wolf
    if (ret < 0) {
942 a9420734 Kevin Wolf
        goto out;
943 a9420734 Kevin Wolf
944 a9420734 Kevin Wolf
    } else if (ret != 0) {
945 a9420734 Kevin Wolf
        error_report("Huh, first cluster in empty image is already in use?");
946 a9420734 Kevin Wolf
        abort();
947 a9420734 Kevin Wolf
    }
948 a9420734 Kevin Wolf
949 a9420734 Kevin Wolf
    /* Okay, now that we have a valid image, let's give it the right size */
950 a9420734 Kevin Wolf
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
951 a9420734 Kevin Wolf
    if (ret < 0) {
952 a9420734 Kevin Wolf
        goto out;
953 a9420734 Kevin Wolf
    }
954 a9420734 Kevin Wolf
955 a9420734 Kevin Wolf
    /* Want a backing file? There you go.*/
956 a9420734 Kevin Wolf
    if (backing_file) {
957 a9420734 Kevin Wolf
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
958 a9420734 Kevin Wolf
        if (ret < 0) {
959 a9420734 Kevin Wolf
            goto out;
960 a9420734 Kevin Wolf
        }
961 a9420734 Kevin Wolf
    }
962 a9420734 Kevin Wolf
963 a9420734 Kevin Wolf
    /* And if we're supposed to preallocate metadata, do that now */
964 a9420734 Kevin Wolf
    if (prealloc) {
965 a9420734 Kevin Wolf
        ret = preallocate(bs);
966 a9420734 Kevin Wolf
        if (ret < 0) {
967 a9420734 Kevin Wolf
            goto out;
968 a9420734 Kevin Wolf
        }
969 a9420734 Kevin Wolf
    }
970 a9420734 Kevin Wolf
971 a9420734 Kevin Wolf
    ret = 0;
972 a9420734 Kevin Wolf
out:
973 a9420734 Kevin Wolf
    bdrv_delete(bs);
974 a9420734 Kevin Wolf
    return ret;
975 a9420734 Kevin Wolf
}
976 de5f3f40 Kevin Wolf
977 de5f3f40 Kevin Wolf
static int qcow_create(const char *filename, QEMUOptionParameter *options)
978 de5f3f40 Kevin Wolf
{
979 de5f3f40 Kevin Wolf
    const char *backing_file = NULL;
980 de5f3f40 Kevin Wolf
    const char *backing_fmt = NULL;
981 de5f3f40 Kevin Wolf
    uint64_t sectors = 0;
982 de5f3f40 Kevin Wolf
    int flags = 0;
983 de5f3f40 Kevin Wolf
    size_t cluster_size = 65536;
984 de5f3f40 Kevin Wolf
    int prealloc = 0;
985 de5f3f40 Kevin Wolf
986 de5f3f40 Kevin Wolf
    /* Read out options */
987 de5f3f40 Kevin Wolf
    while (options && options->name) {
988 de5f3f40 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
989 de5f3f40 Kevin Wolf
            sectors = options->value.n / 512;
990 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
991 de5f3f40 Kevin Wolf
            backing_file = options->value.s;
992 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
993 de5f3f40 Kevin Wolf
            backing_fmt = options->value.s;
994 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
995 de5f3f40 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
996 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
997 de5f3f40 Kevin Wolf
            if (options->value.n) {
998 de5f3f40 Kevin Wolf
                cluster_size = options->value.n;
999 de5f3f40 Kevin Wolf
            }
1000 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1001 de5f3f40 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1002 de5f3f40 Kevin Wolf
                prealloc = 0;
1003 de5f3f40 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
1004 de5f3f40 Kevin Wolf
                prealloc = 1;
1005 de5f3f40 Kevin Wolf
            } else {
1006 de5f3f40 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1007 de5f3f40 Kevin Wolf
                    options->value.s);
1008 de5f3f40 Kevin Wolf
                return -EINVAL;
1009 de5f3f40 Kevin Wolf
            }
1010 de5f3f40 Kevin Wolf
        }
1011 de5f3f40 Kevin Wolf
        options++;
1012 de5f3f40 Kevin Wolf
    }
1013 de5f3f40 Kevin Wolf
1014 de5f3f40 Kevin Wolf
    if (backing_file && prealloc) {
1015 de5f3f40 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1016 de5f3f40 Kevin Wolf
            "the same time\n");
1017 de5f3f40 Kevin Wolf
        return -EINVAL;
1018 de5f3f40 Kevin Wolf
    }
1019 de5f3f40 Kevin Wolf
1020 de5f3f40 Kevin Wolf
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
1021 a9420734 Kevin Wolf
        cluster_size, prealloc, options);
1022 de5f3f40 Kevin Wolf
}
1023 de5f3f40 Kevin Wolf
1024 20d97356 Blue Swirl
static int qcow_make_empty(BlockDriverState *bs)
1025 20d97356 Blue Swirl
{
1026 20d97356 Blue Swirl
#if 0
1027 20d97356 Blue Swirl
    /* XXX: not correct */
1028 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1029 20d97356 Blue Swirl
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1030 20d97356 Blue Swirl
    int ret;
1031 20d97356 Blue Swirl

1032 20d97356 Blue Swirl
    memset(s->l1_table, 0, l1_length);
1033 66f82cee Kevin Wolf
    if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1034 20d97356 Blue Swirl
        return -1;
1035 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1036 20d97356 Blue Swirl
    if (ret < 0)
1037 20d97356 Blue Swirl
        return ret;
1038 20d97356 Blue Swirl

1039 20d97356 Blue Swirl
    l2_cache_reset(bs);
1040 20d97356 Blue Swirl
#endif
1041 20d97356 Blue Swirl
    return 0;
1042 20d97356 Blue Swirl
}
1043 20d97356 Blue Swirl
1044 419b19d9 Stefan Hajnoczi
static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1045 419b19d9 Stefan Hajnoczi
{
1046 419b19d9 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
1047 419b19d9 Stefan Hajnoczi
    int ret, new_l1_size;
1048 419b19d9 Stefan Hajnoczi
1049 419b19d9 Stefan Hajnoczi
    if (offset & 511) {
1050 419b19d9 Stefan Hajnoczi
        return -EINVAL;
1051 419b19d9 Stefan Hajnoczi
    }
1052 419b19d9 Stefan Hajnoczi
1053 419b19d9 Stefan Hajnoczi
    /* cannot proceed if image has snapshots */
1054 419b19d9 Stefan Hajnoczi
    if (s->nb_snapshots) {
1055 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1056 419b19d9 Stefan Hajnoczi
    }
1057 419b19d9 Stefan Hajnoczi
1058 419b19d9 Stefan Hajnoczi
    /* shrinking is currently not supported */
1059 419b19d9 Stefan Hajnoczi
    if (offset < bs->total_sectors * 512) {
1060 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1061 419b19d9 Stefan Hajnoczi
    }
1062 419b19d9 Stefan Hajnoczi
1063 419b19d9 Stefan Hajnoczi
    new_l1_size = size_to_l1(s, offset);
1064 72893756 Stefan Hajnoczi
    ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1065 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1066 419b19d9 Stefan Hajnoczi
        return ret;
1067 419b19d9 Stefan Hajnoczi
    }
1068 419b19d9 Stefan Hajnoczi
1069 419b19d9 Stefan Hajnoczi
    /* write updated header.size */
1070 419b19d9 Stefan Hajnoczi
    offset = cpu_to_be64(offset);
1071 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1072 8b3b7206 Kevin Wolf
                           &offset, sizeof(uint64_t));
1073 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1074 419b19d9 Stefan Hajnoczi
        return ret;
1075 419b19d9 Stefan Hajnoczi
    }
1076 419b19d9 Stefan Hajnoczi
1077 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = new_l1_size;
1078 419b19d9 Stefan Hajnoczi
    return 0;
1079 419b19d9 Stefan Hajnoczi
}
1080 419b19d9 Stefan Hajnoczi
1081 20d97356 Blue Swirl
/* XXX: put compressed sectors first, then all the cluster aligned
1082 20d97356 Blue Swirl
   tables to avoid losing bytes in alignment */
1083 20d97356 Blue Swirl
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
1084 20d97356 Blue Swirl
                                 const uint8_t *buf, int nb_sectors)
1085 20d97356 Blue Swirl
{
1086 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1087 20d97356 Blue Swirl
    z_stream strm;
1088 20d97356 Blue Swirl
    int ret, out_len;
1089 20d97356 Blue Swirl
    uint8_t *out_buf;
1090 20d97356 Blue Swirl
    uint64_t cluster_offset;
1091 20d97356 Blue Swirl
1092 20d97356 Blue Swirl
    if (nb_sectors == 0) {
1093 20d97356 Blue Swirl
        /* align end of file to a sector boundary to ease reading with
1094 20d97356 Blue Swirl
           sector based I/Os */
1095 66f82cee Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
1096 20d97356 Blue Swirl
        cluster_offset = (cluster_offset + 511) & ~511;
1097 66f82cee Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset);
1098 20d97356 Blue Swirl
        return 0;
1099 20d97356 Blue Swirl
    }
1100 20d97356 Blue Swirl
1101 20d97356 Blue Swirl
    if (nb_sectors != s->cluster_sectors)
1102 20d97356 Blue Swirl
        return -EINVAL;
1103 20d97356 Blue Swirl
1104 20d97356 Blue Swirl
    out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1105 20d97356 Blue Swirl
1106 20d97356 Blue Swirl
    /* best compression, small window, no zlib header */
1107 20d97356 Blue Swirl
    memset(&strm, 0, sizeof(strm));
1108 20d97356 Blue Swirl
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1109 20d97356 Blue Swirl
                       Z_DEFLATED, -12,
1110 20d97356 Blue Swirl
                       9, Z_DEFAULT_STRATEGY);
1111 20d97356 Blue Swirl
    if (ret != 0) {
1112 20d97356 Blue Swirl
        qemu_free(out_buf);
1113 20d97356 Blue Swirl
        return -1;
1114 20d97356 Blue Swirl
    }
1115 20d97356 Blue Swirl
1116 20d97356 Blue Swirl
    strm.avail_in = s->cluster_size;
1117 20d97356 Blue Swirl
    strm.next_in = (uint8_t *)buf;
1118 20d97356 Blue Swirl
    strm.avail_out = s->cluster_size;
1119 20d97356 Blue Swirl
    strm.next_out = out_buf;
1120 20d97356 Blue Swirl
1121 20d97356 Blue Swirl
    ret = deflate(&strm, Z_FINISH);
1122 20d97356 Blue Swirl
    if (ret != Z_STREAM_END && ret != Z_OK) {
1123 20d97356 Blue Swirl
        qemu_free(out_buf);
1124 20d97356 Blue Swirl
        deflateEnd(&strm);
1125 20d97356 Blue Swirl
        return -1;
1126 20d97356 Blue Swirl
    }
1127 20d97356 Blue Swirl
    out_len = strm.next_out - out_buf;
1128 20d97356 Blue Swirl
1129 20d97356 Blue Swirl
    deflateEnd(&strm);
1130 20d97356 Blue Swirl
1131 20d97356 Blue Swirl
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1132 20d97356 Blue Swirl
        /* could not compress: write normal cluster */
1133 20d97356 Blue Swirl
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1134 20d97356 Blue Swirl
    } else {
1135 20d97356 Blue Swirl
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1136 20d97356 Blue Swirl
            sector_num << 9, out_len);
1137 20d97356 Blue Swirl
        if (!cluster_offset)
1138 20d97356 Blue Swirl
            return -1;
1139 20d97356 Blue Swirl
        cluster_offset &= s->cluster_offset_mask;
1140 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1141 66f82cee Kevin Wolf
        if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
1142 20d97356 Blue Swirl
            qemu_free(out_buf);
1143 20d97356 Blue Swirl
            return -1;
1144 20d97356 Blue Swirl
        }
1145 20d97356 Blue Swirl
    }
1146 20d97356 Blue Swirl
1147 20d97356 Blue Swirl
    qemu_free(out_buf);
1148 20d97356 Blue Swirl
    return 0;
1149 20d97356 Blue Swirl
}
1150 20d97356 Blue Swirl
1151 205ef796 Kevin Wolf
static int qcow_flush(BlockDriverState *bs)
1152 20d97356 Blue Swirl
{
1153 205ef796 Kevin Wolf
    return bdrv_flush(bs->file);
1154 20d97356 Blue Swirl
}
1155 20d97356 Blue Swirl
1156 20d97356 Blue Swirl
static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
1157 20d97356 Blue Swirl
         BlockDriverCompletionFunc *cb, void *opaque)
1158 20d97356 Blue Swirl
{
1159 66f82cee Kevin Wolf
    return bdrv_aio_flush(bs->file, cb, opaque);
1160 20d97356 Blue Swirl
}
1161 20d97356 Blue Swirl
1162 20d97356 Blue Swirl
static int64_t qcow_vm_state_offset(BDRVQcowState *s)
1163 20d97356 Blue Swirl
{
1164 20d97356 Blue Swirl
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1165 20d97356 Blue Swirl
}
1166 20d97356 Blue Swirl
1167 20d97356 Blue Swirl
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1168 20d97356 Blue Swirl
{
1169 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1170 20d97356 Blue Swirl
    bdi->cluster_size = s->cluster_size;
1171 20d97356 Blue Swirl
    bdi->vm_state_offset = qcow_vm_state_offset(s);
1172 20d97356 Blue Swirl
    return 0;
1173 20d97356 Blue Swirl
}
1174 20d97356 Blue Swirl
1175 20d97356 Blue Swirl
1176 9ac228e0 Kevin Wolf
static int qcow_check(BlockDriverState *bs, BdrvCheckResult *result)
1177 20d97356 Blue Swirl
{
1178 9ac228e0 Kevin Wolf
    return qcow2_check_refcounts(bs, result);
1179 20d97356 Blue Swirl
}
1180 20d97356 Blue Swirl
1181 20d97356 Blue Swirl
#if 0
1182 20d97356 Blue Swirl
static void dump_refcounts(BlockDriverState *bs)
1183 20d97356 Blue Swirl
{
1184 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1185 20d97356 Blue Swirl
    int64_t nb_clusters, k, k1, size;
1186 20d97356 Blue Swirl
    int refcount;
1187 20d97356 Blue Swirl

1188 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1189 20d97356 Blue Swirl
    nb_clusters = size_to_clusters(s, size);
1190 20d97356 Blue Swirl
    for(k = 0; k < nb_clusters;) {
1191 20d97356 Blue Swirl
        k1 = k;
1192 20d97356 Blue Swirl
        refcount = get_refcount(bs, k);
1193 20d97356 Blue Swirl
        k++;
1194 20d97356 Blue Swirl
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1195 20d97356 Blue Swirl
            k++;
1196 0bfcd599 Blue Swirl
        printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1197 0bfcd599 Blue Swirl
               k - k1);
1198 20d97356 Blue Swirl
    }
1199 20d97356 Blue Swirl
}
1200 20d97356 Blue Swirl
#endif
1201 20d97356 Blue Swirl
1202 20d97356 Blue Swirl
static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1203 20d97356 Blue Swirl
                           int64_t pos, int size)
1204 20d97356 Blue Swirl
{
1205 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1206 20d97356 Blue Swirl
    int growable = bs->growable;
1207 20d97356 Blue Swirl
    int ret;
1208 20d97356 Blue Swirl
1209 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1210 20d97356 Blue Swirl
    bs->growable = 1;
1211 20d97356 Blue Swirl
    ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
1212 20d97356 Blue Swirl
    bs->growable = growable;
1213 20d97356 Blue Swirl
1214 20d97356 Blue Swirl
    return ret;
1215 20d97356 Blue Swirl
}
1216 20d97356 Blue Swirl
1217 20d97356 Blue Swirl
static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1218 20d97356 Blue Swirl
                           int64_t pos, int size)
1219 20d97356 Blue Swirl
{
1220 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1221 20d97356 Blue Swirl
    int growable = bs->growable;
1222 20d97356 Blue Swirl
    int ret;
1223 20d97356 Blue Swirl
1224 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1225 20d97356 Blue Swirl
    bs->growable = 1;
1226 20d97356 Blue Swirl
    ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
1227 20d97356 Blue Swirl
    bs->growable = growable;
1228 20d97356 Blue Swirl
1229 20d97356 Blue Swirl
    return ret;
1230 20d97356 Blue Swirl
}
1231 20d97356 Blue Swirl
1232 20d97356 Blue Swirl
static QEMUOptionParameter qcow_create_options[] = {
1233 20d97356 Blue Swirl
    {
1234 20d97356 Blue Swirl
        .name = BLOCK_OPT_SIZE,
1235 20d97356 Blue Swirl
        .type = OPT_SIZE,
1236 20d97356 Blue Swirl
        .help = "Virtual disk size"
1237 20d97356 Blue Swirl
    },
1238 20d97356 Blue Swirl
    {
1239 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FILE,
1240 20d97356 Blue Swirl
        .type = OPT_STRING,
1241 20d97356 Blue Swirl
        .help = "File name of a base image"
1242 20d97356 Blue Swirl
    },
1243 20d97356 Blue Swirl
    {
1244 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FMT,
1245 20d97356 Blue Swirl
        .type = OPT_STRING,
1246 20d97356 Blue Swirl
        .help = "Image format of the base image"
1247 20d97356 Blue Swirl
    },
1248 20d97356 Blue Swirl
    {
1249 20d97356 Blue Swirl
        .name = BLOCK_OPT_ENCRYPT,
1250 20d97356 Blue Swirl
        .type = OPT_FLAG,
1251 20d97356 Blue Swirl
        .help = "Encrypt the image"
1252 20d97356 Blue Swirl
    },
1253 20d97356 Blue Swirl
    {
1254 20d97356 Blue Swirl
        .name = BLOCK_OPT_CLUSTER_SIZE,
1255 20d97356 Blue Swirl
        .type = OPT_SIZE,
1256 20d97356 Blue Swirl
        .help = "qcow2 cluster size"
1257 20d97356 Blue Swirl
    },
1258 20d97356 Blue Swirl
    {
1259 20d97356 Blue Swirl
        .name = BLOCK_OPT_PREALLOC,
1260 20d97356 Blue Swirl
        .type = OPT_STRING,
1261 20d97356 Blue Swirl
        .help = "Preallocation mode (allowed values: off, metadata)"
1262 20d97356 Blue Swirl
    },
1263 20d97356 Blue Swirl
    { NULL }
1264 20d97356 Blue Swirl
};
1265 20d97356 Blue Swirl
1266 20d97356 Blue Swirl
static BlockDriver bdrv_qcow2 = {
1267 20d97356 Blue Swirl
    .format_name        = "qcow2",
1268 20d97356 Blue Swirl
    .instance_size        = sizeof(BDRVQcowState),
1269 20d97356 Blue Swirl
    .bdrv_probe                = qcow_probe,
1270 20d97356 Blue Swirl
    .bdrv_open                = qcow_open,
1271 20d97356 Blue Swirl
    .bdrv_close                = qcow_close,
1272 20d97356 Blue Swirl
    .bdrv_create        = qcow_create,
1273 20d97356 Blue Swirl
    .bdrv_flush                = qcow_flush,
1274 20d97356 Blue Swirl
    .bdrv_is_allocated        = qcow_is_allocated,
1275 20d97356 Blue Swirl
    .bdrv_set_key        = qcow_set_key,
1276 20d97356 Blue Swirl
    .bdrv_make_empty        = qcow_make_empty,
1277 20d97356 Blue Swirl
1278 20d97356 Blue Swirl
    .bdrv_aio_readv        = qcow_aio_readv,
1279 20d97356 Blue Swirl
    .bdrv_aio_writev        = qcow_aio_writev,
1280 20d97356 Blue Swirl
    .bdrv_aio_flush        = qcow_aio_flush,
1281 419b19d9 Stefan Hajnoczi
1282 419b19d9 Stefan Hajnoczi
    .bdrv_truncate          = qcow2_truncate,
1283 419b19d9 Stefan Hajnoczi
    .bdrv_write_compressed  = qcow_write_compressed,
1284 20d97356 Blue Swirl
1285 20d97356 Blue Swirl
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1286 20d97356 Blue Swirl
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1287 20d97356 Blue Swirl
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1288 20d97356 Blue Swirl
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1289 51ef6727 edison
    .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
1290 20d97356 Blue Swirl
    .bdrv_get_info        = qcow_get_info,
1291 20d97356 Blue Swirl
1292 20d97356 Blue Swirl
    .bdrv_save_vmstate    = qcow_save_vmstate,
1293 20d97356 Blue Swirl
    .bdrv_load_vmstate    = qcow_load_vmstate,
1294 20d97356 Blue Swirl
1295 20d97356 Blue Swirl
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1296 20d97356 Blue Swirl
1297 20d97356 Blue Swirl
    .create_options = qcow_create_options,
1298 20d97356 Blue Swirl
    .bdrv_check = qcow_check,
1299 20d97356 Blue Swirl
};
1300 20d97356 Blue Swirl
1301 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1302 5efa9d5a Anthony Liguori
{
1303 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1304 5efa9d5a Anthony Liguori
}
1305 5efa9d5a Anthony Liguori
1306 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);