Statistics
| Branch: | Revision:

root / block / qcow2.c @ bfe8043e

History | View | Annotate | Download (50.2 kB)

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

37 585f8587 bellard
  - Support for multiple incremental snapshots.
38 585f8587 bellard
  - Memory management by reference counts.
39 585f8587 bellard
  - Clusters which have a reference count of one have the bit
40 585f8587 bellard
    QCOW_OFLAG_COPIED to optimize write performance.
41 5fafdf24 ths
  - Size of compressed clusters is stored in sectors to reduce bit usage
42 585f8587 bellard
    in the cluster offsets.
43 585f8587 bellard
  - Support for storing additional data (such as the VM state) in the
44 3b46e624 ths
    snapshots.
45 585f8587 bellard
  - If a backing store is used, the cluster size is not constrained
46 585f8587 bellard
    (could be backported to QCOW).
47 585f8587 bellard
  - L2 tables have always a size of one cluster.
48 585f8587 bellard
*/
49 585f8587 bellard
50 9b80ddf3 aliguori
51 9b80ddf3 aliguori
typedef struct {
52 9b80ddf3 aliguori
    uint32_t magic;
53 9b80ddf3 aliguori
    uint32_t len;
54 9b80ddf3 aliguori
} QCowExtension;
55 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_END 0
56 7c80ab3f Jes Sorensen
#define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
57 cfcc4c62 Kevin Wolf
#define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
58 9b80ddf3 aliguori
59 7c80ab3f Jes Sorensen
static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
60 585f8587 bellard
{
61 585f8587 bellard
    const QCowHeader *cow_header = (const void *)buf;
62 3b46e624 ths
63 585f8587 bellard
    if (buf_size >= sizeof(QCowHeader) &&
64 585f8587 bellard
        be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
65 6744cbab Kevin Wolf
        be32_to_cpu(cow_header->version) >= 2)
66 585f8587 bellard
        return 100;
67 585f8587 bellard
    else
68 585f8587 bellard
        return 0;
69 585f8587 bellard
}
70 585f8587 bellard
71 9b80ddf3 aliguori
72 9b80ddf3 aliguori
/* 
73 9b80ddf3 aliguori
 * read qcow2 extension and fill bs
74 9b80ddf3 aliguori
 * start reading from start_offset
75 9b80ddf3 aliguori
 * finish reading upon magic of value 0 or when end_offset reached
76 9b80ddf3 aliguori
 * unknown magic is skipped (future extension this version knows nothing about)
77 9b80ddf3 aliguori
 * return 0 upon success, non-0 otherwise
78 9b80ddf3 aliguori
 */
79 7c80ab3f Jes Sorensen
static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
80 cfcc4c62 Kevin Wolf
                                 uint64_t end_offset, void **p_feature_table)
81 9b80ddf3 aliguori
{
82 75bab85c Kevin Wolf
    BDRVQcowState *s = bs->opaque;
83 9b80ddf3 aliguori
    QCowExtension ext;
84 9b80ddf3 aliguori
    uint64_t offset;
85 75bab85c Kevin Wolf
    int ret;
86 9b80ddf3 aliguori
87 9b80ddf3 aliguori
#ifdef DEBUG_EXT
88 7c80ab3f Jes Sorensen
    printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
89 9b80ddf3 aliguori
#endif
90 9b80ddf3 aliguori
    offset = start_offset;
91 9b80ddf3 aliguori
    while (offset < end_offset) {
92 9b80ddf3 aliguori
93 9b80ddf3 aliguori
#ifdef DEBUG_EXT
94 9b80ddf3 aliguori
        /* Sanity check */
95 9b80ddf3 aliguori
        if (offset > s->cluster_size)
96 7c80ab3f Jes Sorensen
            printf("qcow2_read_extension: suspicious offset %lu\n", offset);
97 9b80ddf3 aliguori
98 9b2260cb Dong Xu Wang
        printf("attempting to read extended header in offset %lu\n", offset);
99 9b80ddf3 aliguori
#endif
100 9b80ddf3 aliguori
101 66f82cee Kevin Wolf
        if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) {
102 7c80ab3f Jes Sorensen
            fprintf(stderr, "qcow2_read_extension: ERROR: "
103 0bfcd599 Blue Swirl
                    "pread fail from offset %" PRIu64 "\n",
104 0bfcd599 Blue Swirl
                    offset);
105 9b80ddf3 aliguori
            return 1;
106 9b80ddf3 aliguori
        }
107 9b80ddf3 aliguori
        be32_to_cpus(&ext.magic);
108 9b80ddf3 aliguori
        be32_to_cpus(&ext.len);
109 9b80ddf3 aliguori
        offset += sizeof(ext);
110 9b80ddf3 aliguori
#ifdef DEBUG_EXT
111 9b80ddf3 aliguori
        printf("ext.magic = 0x%x\n", ext.magic);
112 9b80ddf3 aliguori
#endif
113 64ca6aee Kevin Wolf
        if (ext.len > end_offset - offset) {
114 64ca6aee Kevin Wolf
            error_report("Header extension too large");
115 64ca6aee Kevin Wolf
            return -EINVAL;
116 64ca6aee Kevin Wolf
        }
117 64ca6aee Kevin Wolf
118 9b80ddf3 aliguori
        switch (ext.magic) {
119 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_END:
120 9b80ddf3 aliguori
            return 0;
121 f965509c aliguori
122 7c80ab3f Jes Sorensen
        case QCOW2_EXT_MAGIC_BACKING_FORMAT:
123 f965509c aliguori
            if (ext.len >= sizeof(bs->backing_format)) {
124 f965509c aliguori
                fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
125 4c978075 aliguori
                        " (>=%zu)\n",
126 f965509c aliguori
                        ext.len, sizeof(bs->backing_format));
127 f965509c aliguori
                return 2;
128 f965509c aliguori
            }
129 66f82cee Kevin Wolf
            if (bdrv_pread(bs->file, offset , bs->backing_format,
130 f965509c aliguori
                           ext.len) != ext.len)
131 f965509c aliguori
                return 3;
132 f965509c aliguori
            bs->backing_format[ext.len] = '\0';
133 f965509c aliguori
#ifdef DEBUG_EXT
134 f965509c aliguori
            printf("Qcow2: Got format extension %s\n", bs->backing_format);
135 f965509c aliguori
#endif
136 f965509c aliguori
            break;
137 f965509c aliguori
138 cfcc4c62 Kevin Wolf
        case QCOW2_EXT_MAGIC_FEATURE_TABLE:
139 cfcc4c62 Kevin Wolf
            if (p_feature_table != NULL) {
140 cfcc4c62 Kevin Wolf
                void* feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
141 cfcc4c62 Kevin Wolf
                ret = bdrv_pread(bs->file, offset , feature_table, ext.len);
142 cfcc4c62 Kevin Wolf
                if (ret < 0) {
143 cfcc4c62 Kevin Wolf
                    return ret;
144 cfcc4c62 Kevin Wolf
                }
145 cfcc4c62 Kevin Wolf
146 cfcc4c62 Kevin Wolf
                *p_feature_table = feature_table;
147 cfcc4c62 Kevin Wolf
            }
148 cfcc4c62 Kevin Wolf
            break;
149 cfcc4c62 Kevin Wolf
150 9b80ddf3 aliguori
        default:
151 75bab85c Kevin Wolf
            /* unknown magic - save it in case we need to rewrite the header */
152 75bab85c Kevin Wolf
            {
153 75bab85c Kevin Wolf
                Qcow2UnknownHeaderExtension *uext;
154 75bab85c Kevin Wolf
155 75bab85c Kevin Wolf
                uext = g_malloc0(sizeof(*uext)  + ext.len);
156 75bab85c Kevin Wolf
                uext->magic = ext.magic;
157 75bab85c Kevin Wolf
                uext->len = ext.len;
158 75bab85c Kevin Wolf
                QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
159 75bab85c Kevin Wolf
160 75bab85c Kevin Wolf
                ret = bdrv_pread(bs->file, offset , uext->data, uext->len);
161 75bab85c Kevin Wolf
                if (ret < 0) {
162 75bab85c Kevin Wolf
                    return ret;
163 75bab85c Kevin Wolf
                }
164 75bab85c Kevin Wolf
            }
165 9b80ddf3 aliguori
            break;
166 9b80ddf3 aliguori
        }
167 fd29b4bb Kevin Wolf
168 fd29b4bb Kevin Wolf
        offset += ((ext.len + 7) & ~7);
169 9b80ddf3 aliguori
    }
170 9b80ddf3 aliguori
171 9b80ddf3 aliguori
    return 0;
172 9b80ddf3 aliguori
}
173 9b80ddf3 aliguori
174 75bab85c Kevin Wolf
static void cleanup_unknown_header_ext(BlockDriverState *bs)
175 75bab85c Kevin Wolf
{
176 75bab85c Kevin Wolf
    BDRVQcowState *s = bs->opaque;
177 75bab85c Kevin Wolf
    Qcow2UnknownHeaderExtension *uext, *next;
178 75bab85c Kevin Wolf
179 75bab85c Kevin Wolf
    QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
180 75bab85c Kevin Wolf
        QLIST_REMOVE(uext, next);
181 75bab85c Kevin Wolf
        g_free(uext);
182 75bab85c Kevin Wolf
    }
183 75bab85c Kevin Wolf
}
184 9b80ddf3 aliguori
185 b9531b6e Stefan Weil
static void GCC_FMT_ATTR(2, 3) report_unsupported(BlockDriverState *bs,
186 b9531b6e Stefan Weil
    const char *fmt, ...)
187 6744cbab Kevin Wolf
{
188 6744cbab Kevin Wolf
    char msg[64];
189 6744cbab Kevin Wolf
    va_list ap;
190 6744cbab Kevin Wolf
191 6744cbab Kevin Wolf
    va_start(ap, fmt);
192 6744cbab Kevin Wolf
    vsnprintf(msg, sizeof(msg), fmt, ap);
193 6744cbab Kevin Wolf
    va_end(ap);
194 6744cbab Kevin Wolf
195 6744cbab Kevin Wolf
    qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
196 6744cbab Kevin Wolf
        bs->device_name, "qcow2", msg);
197 6744cbab Kevin Wolf
}
198 6744cbab Kevin Wolf
199 cfcc4c62 Kevin Wolf
static void report_unsupported_feature(BlockDriverState *bs,
200 cfcc4c62 Kevin Wolf
    Qcow2Feature *table, uint64_t mask)
201 cfcc4c62 Kevin Wolf
{
202 cfcc4c62 Kevin Wolf
    while (table && table->name[0] != '\0') {
203 cfcc4c62 Kevin Wolf
        if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
204 cfcc4c62 Kevin Wolf
            if (mask & (1 << table->bit)) {
205 cfcc4c62 Kevin Wolf
                report_unsupported(bs, "%.46s",table->name);
206 cfcc4c62 Kevin Wolf
                mask &= ~(1 << table->bit);
207 cfcc4c62 Kevin Wolf
            }
208 cfcc4c62 Kevin Wolf
        }
209 cfcc4c62 Kevin Wolf
        table++;
210 cfcc4c62 Kevin Wolf
    }
211 cfcc4c62 Kevin Wolf
212 cfcc4c62 Kevin Wolf
    if (mask) {
213 cfcc4c62 Kevin Wolf
        report_unsupported(bs, "Unknown incompatible feature: %" PRIx64, mask);
214 cfcc4c62 Kevin Wolf
    }
215 cfcc4c62 Kevin Wolf
}
216 cfcc4c62 Kevin Wolf
217 c61d0004 Stefan Hajnoczi
/*
218 bfe8043e Stefan Hajnoczi
 * Sets the dirty bit and flushes afterwards if necessary.
219 bfe8043e Stefan Hajnoczi
 *
220 bfe8043e Stefan Hajnoczi
 * The incompatible_features bit is only set if the image file header was
221 bfe8043e Stefan Hajnoczi
 * updated successfully.  Therefore it is not required to check the return
222 bfe8043e Stefan Hajnoczi
 * value of this function.
223 bfe8043e Stefan Hajnoczi
 */
224 bfe8043e Stefan Hajnoczi
static int qcow2_mark_dirty(BlockDriverState *bs)
225 bfe8043e Stefan Hajnoczi
{
226 bfe8043e Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
227 bfe8043e Stefan Hajnoczi
    uint64_t val;
228 bfe8043e Stefan Hajnoczi
    int ret;
229 bfe8043e Stefan Hajnoczi
230 bfe8043e Stefan Hajnoczi
    assert(s->qcow_version >= 3);
231 bfe8043e Stefan Hajnoczi
232 bfe8043e Stefan Hajnoczi
    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
233 bfe8043e Stefan Hajnoczi
        return 0; /* already dirty */
234 bfe8043e Stefan Hajnoczi
    }
235 bfe8043e Stefan Hajnoczi
236 bfe8043e Stefan Hajnoczi
    val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
237 bfe8043e Stefan Hajnoczi
    ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, incompatible_features),
238 bfe8043e Stefan Hajnoczi
                      &val, sizeof(val));
239 bfe8043e Stefan Hajnoczi
    if (ret < 0) {
240 bfe8043e Stefan Hajnoczi
        return ret;
241 bfe8043e Stefan Hajnoczi
    }
242 bfe8043e Stefan Hajnoczi
    ret = bdrv_flush(bs->file);
243 bfe8043e Stefan Hajnoczi
    if (ret < 0) {
244 bfe8043e Stefan Hajnoczi
        return ret;
245 bfe8043e Stefan Hajnoczi
    }
246 bfe8043e Stefan Hajnoczi
247 bfe8043e Stefan Hajnoczi
    /* Only treat image as dirty if the header was updated successfully */
248 bfe8043e Stefan Hajnoczi
    s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
249 bfe8043e Stefan Hajnoczi
    return 0;
250 bfe8043e Stefan Hajnoczi
}
251 bfe8043e Stefan Hajnoczi
252 bfe8043e Stefan Hajnoczi
/*
253 c61d0004 Stefan Hajnoczi
 * Clears the dirty bit and flushes before if necessary.  Only call this
254 c61d0004 Stefan Hajnoczi
 * function when there are no pending requests, it does not guard against
255 c61d0004 Stefan Hajnoczi
 * concurrent requests dirtying the image.
256 c61d0004 Stefan Hajnoczi
 */
257 c61d0004 Stefan Hajnoczi
static int qcow2_mark_clean(BlockDriverState *bs)
258 c61d0004 Stefan Hajnoczi
{
259 c61d0004 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
260 c61d0004 Stefan Hajnoczi
261 c61d0004 Stefan Hajnoczi
    if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
262 c61d0004 Stefan Hajnoczi
        int ret = bdrv_flush(bs);
263 c61d0004 Stefan Hajnoczi
        if (ret < 0) {
264 c61d0004 Stefan Hajnoczi
            return ret;
265 c61d0004 Stefan Hajnoczi
        }
266 c61d0004 Stefan Hajnoczi
267 c61d0004 Stefan Hajnoczi
        s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
268 c61d0004 Stefan Hajnoczi
        return qcow2_update_header(bs);
269 c61d0004 Stefan Hajnoczi
    }
270 c61d0004 Stefan Hajnoczi
    return 0;
271 c61d0004 Stefan Hajnoczi
}
272 c61d0004 Stefan Hajnoczi
273 7c80ab3f Jes Sorensen
static int qcow2_open(BlockDriverState *bs, int flags)
274 585f8587 bellard
{
275 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
276 6d85a57e Jes Sorensen
    int len, i, ret = 0;
277 585f8587 bellard
    QCowHeader header;
278 9b80ddf3 aliguori
    uint64_t ext_end;
279 585f8587 bellard
280 6d85a57e Jes Sorensen
    ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
281 6d85a57e Jes Sorensen
    if (ret < 0) {
282 585f8587 bellard
        goto fail;
283 6d85a57e Jes Sorensen
    }
284 585f8587 bellard
    be32_to_cpus(&header.magic);
285 585f8587 bellard
    be32_to_cpus(&header.version);
286 585f8587 bellard
    be64_to_cpus(&header.backing_file_offset);
287 585f8587 bellard
    be32_to_cpus(&header.backing_file_size);
288 585f8587 bellard
    be64_to_cpus(&header.size);
289 585f8587 bellard
    be32_to_cpus(&header.cluster_bits);
290 585f8587 bellard
    be32_to_cpus(&header.crypt_method);
291 585f8587 bellard
    be64_to_cpus(&header.l1_table_offset);
292 585f8587 bellard
    be32_to_cpus(&header.l1_size);
293 585f8587 bellard
    be64_to_cpus(&header.refcount_table_offset);
294 585f8587 bellard
    be32_to_cpus(&header.refcount_table_clusters);
295 585f8587 bellard
    be64_to_cpus(&header.snapshots_offset);
296 585f8587 bellard
    be32_to_cpus(&header.nb_snapshots);
297 3b46e624 ths
298 e8cdcec1 Kevin Wolf
    if (header.magic != QCOW_MAGIC) {
299 6d85a57e Jes Sorensen
        ret = -EINVAL;
300 585f8587 bellard
        goto fail;
301 6d85a57e Jes Sorensen
    }
302 6744cbab Kevin Wolf
    if (header.version < 2 || header.version > 3) {
303 6744cbab Kevin Wolf
        report_unsupported(bs, "QCOW version %d", header.version);
304 6744cbab Kevin Wolf
        ret = -ENOTSUP;
305 6744cbab Kevin Wolf
        goto fail;
306 6744cbab Kevin Wolf
    }
307 6744cbab Kevin Wolf
308 6744cbab Kevin Wolf
    s->qcow_version = header.version;
309 6744cbab Kevin Wolf
310 6744cbab Kevin Wolf
    /* Initialise version 3 header fields */
311 6744cbab Kevin Wolf
    if (header.version == 2) {
312 6744cbab Kevin Wolf
        header.incompatible_features    = 0;
313 6744cbab Kevin Wolf
        header.compatible_features      = 0;
314 6744cbab Kevin Wolf
        header.autoclear_features       = 0;
315 6744cbab Kevin Wolf
        header.refcount_order           = 4;
316 6744cbab Kevin Wolf
        header.header_length            = 72;
317 6744cbab Kevin Wolf
    } else {
318 6744cbab Kevin Wolf
        be64_to_cpus(&header.incompatible_features);
319 6744cbab Kevin Wolf
        be64_to_cpus(&header.compatible_features);
320 6744cbab Kevin Wolf
        be64_to_cpus(&header.autoclear_features);
321 6744cbab Kevin Wolf
        be32_to_cpus(&header.refcount_order);
322 6744cbab Kevin Wolf
        be32_to_cpus(&header.header_length);
323 6744cbab Kevin Wolf
    }
324 6744cbab Kevin Wolf
325 6744cbab Kevin Wolf
    if (header.header_length > sizeof(header)) {
326 6744cbab Kevin Wolf
        s->unknown_header_fields_size = header.header_length - sizeof(header);
327 6744cbab Kevin Wolf
        s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
328 6744cbab Kevin Wolf
        ret = bdrv_pread(bs->file, sizeof(header), s->unknown_header_fields,
329 6744cbab Kevin Wolf
                         s->unknown_header_fields_size);
330 6744cbab Kevin Wolf
        if (ret < 0) {
331 6744cbab Kevin Wolf
            goto fail;
332 6744cbab Kevin Wolf
        }
333 6744cbab Kevin Wolf
    }
334 6744cbab Kevin Wolf
335 cfcc4c62 Kevin Wolf
    if (header.backing_file_offset) {
336 cfcc4c62 Kevin Wolf
        ext_end = header.backing_file_offset;
337 cfcc4c62 Kevin Wolf
    } else {
338 cfcc4c62 Kevin Wolf
        ext_end = 1 << header.cluster_bits;
339 cfcc4c62 Kevin Wolf
    }
340 cfcc4c62 Kevin Wolf
341 6744cbab Kevin Wolf
    /* Handle feature bits */
342 6744cbab Kevin Wolf
    s->incompatible_features    = header.incompatible_features;
343 6744cbab Kevin Wolf
    s->compatible_features      = header.compatible_features;
344 6744cbab Kevin Wolf
    s->autoclear_features       = header.autoclear_features;
345 6744cbab Kevin Wolf
346 c61d0004 Stefan Hajnoczi
    if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
347 cfcc4c62 Kevin Wolf
        void *feature_table = NULL;
348 cfcc4c62 Kevin Wolf
        qcow2_read_extensions(bs, header.header_length, ext_end,
349 cfcc4c62 Kevin Wolf
                              &feature_table);
350 cfcc4c62 Kevin Wolf
        report_unsupported_feature(bs, feature_table,
351 c61d0004 Stefan Hajnoczi
                                   s->incompatible_features &
352 c61d0004 Stefan Hajnoczi
                                   ~QCOW2_INCOMPAT_MASK);
353 6744cbab Kevin Wolf
        ret = -ENOTSUP;
354 6744cbab Kevin Wolf
        goto fail;
355 6744cbab Kevin Wolf
    }
356 6744cbab Kevin Wolf
357 6744cbab Kevin Wolf
    /* Check support for various header values */
358 6744cbab Kevin Wolf
    if (header.refcount_order != 4) {
359 6744cbab Kevin Wolf
        report_unsupported(bs, "%d bit reference counts",
360 6744cbab Kevin Wolf
                           1 << header.refcount_order);
361 e8cdcec1 Kevin Wolf
        ret = -ENOTSUP;
362 e8cdcec1 Kevin Wolf
        goto fail;
363 e8cdcec1 Kevin Wolf
    }
364 6744cbab Kevin Wolf
365 d191d12d Stefan Weil
    if (header.cluster_bits < MIN_CLUSTER_BITS ||
366 6d85a57e Jes Sorensen
        header.cluster_bits > MAX_CLUSTER_BITS) {
367 6d85a57e Jes Sorensen
        ret = -EINVAL;
368 585f8587 bellard
        goto fail;
369 6d85a57e Jes Sorensen
    }
370 6d85a57e Jes Sorensen
    if (header.crypt_method > QCOW_CRYPT_AES) {
371 6d85a57e Jes Sorensen
        ret = -EINVAL;
372 585f8587 bellard
        goto fail;
373 6d85a57e Jes Sorensen
    }
374 585f8587 bellard
    s->crypt_method_header = header.crypt_method;
375 6d85a57e Jes Sorensen
    if (s->crypt_method_header) {
376 585f8587 bellard
        bs->encrypted = 1;
377 6d85a57e Jes Sorensen
    }
378 585f8587 bellard
    s->cluster_bits = header.cluster_bits;
379 585f8587 bellard
    s->cluster_size = 1 << s->cluster_bits;
380 585f8587 bellard
    s->cluster_sectors = 1 << (s->cluster_bits - 9);
381 585f8587 bellard
    s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
382 585f8587 bellard
    s->l2_size = 1 << s->l2_bits;
383 585f8587 bellard
    bs->total_sectors = header.size / 512;
384 585f8587 bellard
    s->csize_shift = (62 - (s->cluster_bits - 8));
385 585f8587 bellard
    s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
386 585f8587 bellard
    s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
387 585f8587 bellard
    s->refcount_table_offset = header.refcount_table_offset;
388 5fafdf24 ths
    s->refcount_table_size =
389 585f8587 bellard
        header.refcount_table_clusters << (s->cluster_bits - 3);
390 585f8587 bellard
391 585f8587 bellard
    s->snapshots_offset = header.snapshots_offset;
392 585f8587 bellard
    s->nb_snapshots = header.nb_snapshots;
393 585f8587 bellard
394 585f8587 bellard
    /* read the level 1 table */
395 585f8587 bellard
    s->l1_size = header.l1_size;
396 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = size_to_l1(s, header.size);
397 585f8587 bellard
    /* the L1 table must contain at least enough entries to put
398 585f8587 bellard
       header.size bytes */
399 6d85a57e Jes Sorensen
    if (s->l1_size < s->l1_vm_state_index) {
400 6d85a57e Jes Sorensen
        ret = -EINVAL;
401 585f8587 bellard
        goto fail;
402 6d85a57e Jes Sorensen
    }
403 585f8587 bellard
    s->l1_table_offset = header.l1_table_offset;
404 d191d12d Stefan Weil
    if (s->l1_size > 0) {
405 7267c094 Anthony Liguori
        s->l1_table = g_malloc0(
406 d191d12d Stefan Weil
            align_offset(s->l1_size * sizeof(uint64_t), 512));
407 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
408 6d85a57e Jes Sorensen
                         s->l1_size * sizeof(uint64_t));
409 6d85a57e Jes Sorensen
        if (ret < 0) {
410 d191d12d Stefan Weil
            goto fail;
411 6d85a57e Jes Sorensen
        }
412 d191d12d Stefan Weil
        for(i = 0;i < s->l1_size; i++) {
413 d191d12d Stefan Weil
            be64_to_cpus(&s->l1_table[i]);
414 d191d12d Stefan Weil
        }
415 585f8587 bellard
    }
416 29c1a730 Kevin Wolf
417 29c1a730 Kevin Wolf
    /* alloc L2 table/refcount block cache */
418 6af4e9ea Paolo Bonzini
    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
419 6af4e9ea Paolo Bonzini
    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
420 29c1a730 Kevin Wolf
421 7267c094 Anthony Liguori
    s->cluster_cache = g_malloc(s->cluster_size);
422 585f8587 bellard
    /* one more sector for decompressed data alignment */
423 dea43a65 Frediano Ziglio
    s->cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
424 095a9c58 aliguori
                                  + 512);
425 585f8587 bellard
    s->cluster_cache_offset = -1;
426 06d9260f Anthony Liguori
    s->flags = flags;
427 3b46e624 ths
428 6d85a57e Jes Sorensen
    ret = qcow2_refcount_init(bs);
429 6d85a57e Jes Sorensen
    if (ret != 0) {
430 585f8587 bellard
        goto fail;
431 6d85a57e Jes Sorensen
    }
432 585f8587 bellard
433 72cf2d4f Blue Swirl
    QLIST_INIT(&s->cluster_allocs);
434 f214978a Kevin Wolf
435 9b80ddf3 aliguori
    /* read qcow2 extensions */
436 cfcc4c62 Kevin Wolf
    if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL)) {
437 6d85a57e Jes Sorensen
        ret = -EINVAL;
438 9b80ddf3 aliguori
        goto fail;
439 6d85a57e Jes Sorensen
    }
440 9b80ddf3 aliguori
441 585f8587 bellard
    /* read the backing file name */
442 585f8587 bellard
    if (header.backing_file_offset != 0) {
443 585f8587 bellard
        len = header.backing_file_size;
444 6d85a57e Jes Sorensen
        if (len > 1023) {
445 585f8587 bellard
            len = 1023;
446 6d85a57e Jes Sorensen
        }
447 6d85a57e Jes Sorensen
        ret = bdrv_pread(bs->file, header.backing_file_offset,
448 6d85a57e Jes Sorensen
                         bs->backing_file, len);
449 6d85a57e Jes Sorensen
        if (ret < 0) {
450 585f8587 bellard
            goto fail;
451 6d85a57e Jes Sorensen
        }
452 585f8587 bellard
        bs->backing_file[len] = '\0';
453 585f8587 bellard
    }
454 42deb29f Kevin Wolf
455 42deb29f Kevin Wolf
    ret = qcow2_read_snapshots(bs);
456 42deb29f Kevin Wolf
    if (ret < 0) {
457 585f8587 bellard
        goto fail;
458 6d85a57e Jes Sorensen
    }
459 585f8587 bellard
460 af7b708d Stefan Hajnoczi
    /* Clear unknown autoclear feature bits */
461 af7b708d Stefan Hajnoczi
    if (!bs->read_only && s->autoclear_features != 0) {
462 af7b708d Stefan Hajnoczi
        s->autoclear_features = 0;
463 af7b708d Stefan Hajnoczi
        ret = qcow2_update_header(bs);
464 af7b708d Stefan Hajnoczi
        if (ret < 0) {
465 af7b708d Stefan Hajnoczi
            goto fail;
466 af7b708d Stefan Hajnoczi
        }
467 af7b708d Stefan Hajnoczi
    }
468 af7b708d Stefan Hajnoczi
469 68d100e9 Kevin Wolf
    /* Initialise locks */
470 68d100e9 Kevin Wolf
    qemu_co_mutex_init(&s->lock);
471 68d100e9 Kevin Wolf
472 c61d0004 Stefan Hajnoczi
    /* Repair image if dirty */
473 c61d0004 Stefan Hajnoczi
    if ((s->incompatible_features & QCOW2_INCOMPAT_DIRTY) &&
474 c61d0004 Stefan Hajnoczi
        !bs->read_only) {
475 c61d0004 Stefan Hajnoczi
        BdrvCheckResult result = {0};
476 c61d0004 Stefan Hajnoczi
477 c61d0004 Stefan Hajnoczi
        ret = qcow2_check_refcounts(bs, &result, BDRV_FIX_ERRORS);
478 c61d0004 Stefan Hajnoczi
        if (ret < 0) {
479 c61d0004 Stefan Hajnoczi
            goto fail;
480 c61d0004 Stefan Hajnoczi
        }
481 c61d0004 Stefan Hajnoczi
482 c61d0004 Stefan Hajnoczi
        ret = qcow2_mark_clean(bs);
483 c61d0004 Stefan Hajnoczi
        if (ret < 0) {
484 c61d0004 Stefan Hajnoczi
            goto fail;
485 c61d0004 Stefan Hajnoczi
        }
486 c61d0004 Stefan Hajnoczi
    }
487 c61d0004 Stefan Hajnoczi
488 585f8587 bellard
#ifdef DEBUG_ALLOC
489 6cbc3031 Philipp Hahn
    {
490 6cbc3031 Philipp Hahn
        BdrvCheckResult result = {0};
491 b35278f7 Stefan Hajnoczi
        qcow2_check_refcounts(bs, &result, 0);
492 6cbc3031 Philipp Hahn
    }
493 585f8587 bellard
#endif
494 6d85a57e Jes Sorensen
    return ret;
495 585f8587 bellard
496 585f8587 bellard
 fail:
497 6744cbab Kevin Wolf
    g_free(s->unknown_header_fields);
498 75bab85c Kevin Wolf
    cleanup_unknown_header_ext(bs);
499 ed6ccf0f Kevin Wolf
    qcow2_free_snapshots(bs);
500 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
501 7267c094 Anthony Liguori
    g_free(s->l1_table);
502 29c1a730 Kevin Wolf
    if (s->l2_table_cache) {
503 29c1a730 Kevin Wolf
        qcow2_cache_destroy(bs, s->l2_table_cache);
504 29c1a730 Kevin Wolf
    }
505 7267c094 Anthony Liguori
    g_free(s->cluster_cache);
506 dea43a65 Frediano Ziglio
    qemu_vfree(s->cluster_data);
507 6d85a57e Jes Sorensen
    return ret;
508 585f8587 bellard
}
509 585f8587 bellard
510 7c80ab3f Jes Sorensen
static int qcow2_set_key(BlockDriverState *bs, const char *key)
511 585f8587 bellard
{
512 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
513 585f8587 bellard
    uint8_t keybuf[16];
514 585f8587 bellard
    int len, i;
515 3b46e624 ths
516 585f8587 bellard
    memset(keybuf, 0, 16);
517 585f8587 bellard
    len = strlen(key);
518 585f8587 bellard
    if (len > 16)
519 585f8587 bellard
        len = 16;
520 585f8587 bellard
    /* XXX: we could compress the chars to 7 bits to increase
521 585f8587 bellard
       entropy */
522 585f8587 bellard
    for(i = 0;i < len;i++) {
523 585f8587 bellard
        keybuf[i] = key[i];
524 585f8587 bellard
    }
525 585f8587 bellard
    s->crypt_method = s->crypt_method_header;
526 585f8587 bellard
527 585f8587 bellard
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
528 585f8587 bellard
        return -1;
529 585f8587 bellard
    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
530 585f8587 bellard
        return -1;
531 585f8587 bellard
#if 0
532 585f8587 bellard
    /* test */
533 585f8587 bellard
    {
534 585f8587 bellard
        uint8_t in[16];
535 585f8587 bellard
        uint8_t out[16];
536 585f8587 bellard
        uint8_t tmp[16];
537 585f8587 bellard
        for(i=0;i<16;i++)
538 585f8587 bellard
            in[i] = i;
539 585f8587 bellard
        AES_encrypt(in, tmp, &s->aes_encrypt_key);
540 585f8587 bellard
        AES_decrypt(tmp, out, &s->aes_decrypt_key);
541 585f8587 bellard
        for(i = 0; i < 16; i++)
542 585f8587 bellard
            printf(" %02x", tmp[i]);
543 585f8587 bellard
        printf("\n");
544 585f8587 bellard
        for(i = 0; i < 16; i++)
545 585f8587 bellard
            printf(" %02x", out[i]);
546 585f8587 bellard
        printf("\n");
547 585f8587 bellard
    }
548 585f8587 bellard
#endif
549 585f8587 bellard
    return 0;
550 585f8587 bellard
}
551 585f8587 bellard
552 f8a2e5e3 Stefan Hajnoczi
static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs,
553 f8a2e5e3 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, int *pnum)
554 585f8587 bellard
{
555 f8a2e5e3 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
556 585f8587 bellard
    uint64_t cluster_offset;
557 1c46efaa Kevin Wolf
    int ret;
558 585f8587 bellard
559 095a9c58 aliguori
    *pnum = nb_sectors;
560 f8a2e5e3 Stefan Hajnoczi
    /* FIXME We can get errors here, but the bdrv_co_is_allocated interface
561 f8a2e5e3 Stefan Hajnoczi
     * can't pass them on today */
562 f8a2e5e3 Stefan Hajnoczi
    qemu_co_mutex_lock(&s->lock);
563 1c46efaa Kevin Wolf
    ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
564 f8a2e5e3 Stefan Hajnoczi
    qemu_co_mutex_unlock(&s->lock);
565 1c46efaa Kevin Wolf
    if (ret < 0) {
566 1c46efaa Kevin Wolf
        *pnum = 0;
567 1c46efaa Kevin Wolf
    }
568 095a9c58 aliguori
569 585f8587 bellard
    return (cluster_offset != 0);
570 585f8587 bellard
}
571 585f8587 bellard
572 a9465922 bellard
/* handle reading after the end of the backing file */
573 bd28f835 Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
574 bd28f835 Kevin Wolf
                  int64_t sector_num, int nb_sectors)
575 a9465922 bellard
{
576 a9465922 bellard
    int n1;
577 a9465922 bellard
    if ((sector_num + nb_sectors) <= bs->total_sectors)
578 a9465922 bellard
        return nb_sectors;
579 a9465922 bellard
    if (sector_num >= bs->total_sectors)
580 a9465922 bellard
        n1 = 0;
581 a9465922 bellard
    else
582 a9465922 bellard
        n1 = bs->total_sectors - sector_num;
583 bd28f835 Kevin Wolf
584 3d9b4925 Michael Tokarev
    qemu_iovec_memset(qiov, 512 * n1, 0, 512 * (nb_sectors - n1));
585 bd28f835 Kevin Wolf
586 a9465922 bellard
    return n1;
587 a9465922 bellard
}
588 a9465922 bellard
589 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
590 3fc48d09 Frediano Ziglio
                          int remaining_sectors, QEMUIOVector *qiov)
591 585f8587 bellard
{
592 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
593 a9465922 bellard
    int index_in_cluster, n1;
594 68d100e9 Kevin Wolf
    int ret;
595 faf575c1 Frediano Ziglio
    int cur_nr_sectors; /* number of sectors in current iteration */
596 c2bdd990 Frediano Ziglio
    uint64_t cluster_offset = 0;
597 3fc48d09 Frediano Ziglio
    uint64_t bytes_done = 0;
598 3fc48d09 Frediano Ziglio
    QEMUIOVector hd_qiov;
599 3fc48d09 Frediano Ziglio
    uint8_t *cluster_data = NULL;
600 585f8587 bellard
601 3fc48d09 Frediano Ziglio
    qemu_iovec_init(&hd_qiov, qiov->niov);
602 3fc48d09 Frediano Ziglio
603 3fc48d09 Frediano Ziglio
    qemu_co_mutex_lock(&s->lock);
604 3fc48d09 Frediano Ziglio
605 3fc48d09 Frediano Ziglio
    while (remaining_sectors != 0) {
606 bd28f835 Kevin Wolf
607 5ebaa27e Frediano Ziglio
        /* prepare next request */
608 3fc48d09 Frediano Ziglio
        cur_nr_sectors = remaining_sectors;
609 5ebaa27e Frediano Ziglio
        if (s->crypt_method) {
610 5ebaa27e Frediano Ziglio
            cur_nr_sectors = MIN(cur_nr_sectors,
611 5ebaa27e Frediano Ziglio
                QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
612 585f8587 bellard
        }
613 5ebaa27e Frediano Ziglio
614 3fc48d09 Frediano Ziglio
        ret = qcow2_get_cluster_offset(bs, sector_num << 9,
615 5ebaa27e Frediano Ziglio
            &cur_nr_sectors, &cluster_offset);
616 8af36488 Kevin Wolf
        if (ret < 0) {
617 3fc48d09 Frediano Ziglio
            goto fail;
618 8af36488 Kevin Wolf
        }
619 bd28f835 Kevin Wolf
620 3fc48d09 Frediano Ziglio
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
621 c87c0672 aliguori
622 3fc48d09 Frediano Ziglio
        qemu_iovec_reset(&hd_qiov);
623 1b093c48 Michael Tokarev
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
624 5ebaa27e Frediano Ziglio
            cur_nr_sectors * 512);
625 5ebaa27e Frediano Ziglio
626 68d000a3 Kevin Wolf
        switch (ret) {
627 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_UNALLOCATED:
628 5ebaa27e Frediano Ziglio
629 5ebaa27e Frediano Ziglio
            if (bs->backing_hd) {
630 5ebaa27e Frediano Ziglio
                /* read from the base image */
631 3fc48d09 Frediano Ziglio
                n1 = qcow2_backing_read1(bs->backing_hd, &hd_qiov,
632 3fc48d09 Frediano Ziglio
                    sector_num, cur_nr_sectors);
633 5ebaa27e Frediano Ziglio
                if (n1 > 0) {
634 5ebaa27e Frediano Ziglio
                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
635 5ebaa27e Frediano Ziglio
                    qemu_co_mutex_unlock(&s->lock);
636 3fc48d09 Frediano Ziglio
                    ret = bdrv_co_readv(bs->backing_hd, sector_num,
637 3fc48d09 Frediano Ziglio
                                        n1, &hd_qiov);
638 5ebaa27e Frediano Ziglio
                    qemu_co_mutex_lock(&s->lock);
639 5ebaa27e Frediano Ziglio
                    if (ret < 0) {
640 3fc48d09 Frediano Ziglio
                        goto fail;
641 5ebaa27e Frediano Ziglio
                    }
642 5ebaa27e Frediano Ziglio
                }
643 5ebaa27e Frediano Ziglio
            } else {
644 5ebaa27e Frediano Ziglio
                /* Note: in this case, no need to wait */
645 3d9b4925 Michael Tokarev
                qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
646 5ebaa27e Frediano Ziglio
            }
647 68d000a3 Kevin Wolf
            break;
648 68d000a3 Kevin Wolf
649 6377af48 Kevin Wolf
        case QCOW2_CLUSTER_ZERO:
650 6377af48 Kevin Wolf
            if (s->qcow_version < 3) {
651 6377af48 Kevin Wolf
                ret = -EIO;
652 6377af48 Kevin Wolf
                goto fail;
653 6377af48 Kevin Wolf
            }
654 3d9b4925 Michael Tokarev
            qemu_iovec_memset(&hd_qiov, 0, 0, 512 * cur_nr_sectors);
655 6377af48 Kevin Wolf
            break;
656 6377af48 Kevin Wolf
657 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_COMPRESSED:
658 5ebaa27e Frediano Ziglio
            /* add AIO support for compressed blocks ? */
659 5ebaa27e Frediano Ziglio
            ret = qcow2_decompress_cluster(bs, cluster_offset);
660 5ebaa27e Frediano Ziglio
            if (ret < 0) {
661 3fc48d09 Frediano Ziglio
                goto fail;
662 bd28f835 Kevin Wolf
            }
663 bd28f835 Kevin Wolf
664 03396148 Michael Tokarev
            qemu_iovec_from_buf(&hd_qiov, 0,
665 5ebaa27e Frediano Ziglio
                s->cluster_cache + index_in_cluster * 512,
666 faf575c1 Frediano Ziglio
                512 * cur_nr_sectors);
667 68d000a3 Kevin Wolf
            break;
668 68d000a3 Kevin Wolf
669 68d000a3 Kevin Wolf
        case QCOW2_CLUSTER_NORMAL:
670 5ebaa27e Frediano Ziglio
            if ((cluster_offset & 511) != 0) {
671 3fc48d09 Frediano Ziglio
                ret = -EIO;
672 3fc48d09 Frediano Ziglio
                goto fail;
673 5ebaa27e Frediano Ziglio
            }
674 bd28f835 Kevin Wolf
675 5ebaa27e Frediano Ziglio
            if (s->crypt_method) {
676 5ebaa27e Frediano Ziglio
                /*
677 5ebaa27e Frediano Ziglio
                 * For encrypted images, read everything into a temporary
678 5ebaa27e Frediano Ziglio
                 * contiguous buffer on which the AES functions can work.
679 5ebaa27e Frediano Ziglio
                 */
680 3fc48d09 Frediano Ziglio
                if (!cluster_data) {
681 3fc48d09 Frediano Ziglio
                    cluster_data =
682 dea43a65 Frediano Ziglio
                        qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
683 5ebaa27e Frediano Ziglio
                }
684 5ebaa27e Frediano Ziglio
685 5ebaa27e Frediano Ziglio
                assert(cur_nr_sectors <=
686 5ebaa27e Frediano Ziglio
                    QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors);
687 3fc48d09 Frediano Ziglio
                qemu_iovec_reset(&hd_qiov);
688 3fc48d09 Frediano Ziglio
                qemu_iovec_add(&hd_qiov, cluster_data,
689 5ebaa27e Frediano Ziglio
                    512 * cur_nr_sectors);
690 5ebaa27e Frediano Ziglio
            }
691 5ebaa27e Frediano Ziglio
692 5ebaa27e Frediano Ziglio
            BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
693 5ebaa27e Frediano Ziglio
            qemu_co_mutex_unlock(&s->lock);
694 5ebaa27e Frediano Ziglio
            ret = bdrv_co_readv(bs->file,
695 5ebaa27e Frediano Ziglio
                                (cluster_offset >> 9) + index_in_cluster,
696 3fc48d09 Frediano Ziglio
                                cur_nr_sectors, &hd_qiov);
697 5ebaa27e Frediano Ziglio
            qemu_co_mutex_lock(&s->lock);
698 5ebaa27e Frediano Ziglio
            if (ret < 0) {
699 3fc48d09 Frediano Ziglio
                goto fail;
700 5ebaa27e Frediano Ziglio
            }
701 5ebaa27e Frediano Ziglio
            if (s->crypt_method) {
702 3fc48d09 Frediano Ziglio
                qcow2_encrypt_sectors(s, sector_num,  cluster_data,
703 3fc48d09 Frediano Ziglio
                    cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
704 03396148 Michael Tokarev
                qemu_iovec_from_buf(qiov, bytes_done,
705 03396148 Michael Tokarev
                    cluster_data, 512 * cur_nr_sectors);
706 5ebaa27e Frediano Ziglio
            }
707 68d000a3 Kevin Wolf
            break;
708 68d000a3 Kevin Wolf
709 68d000a3 Kevin Wolf
        default:
710 68d000a3 Kevin Wolf
            g_assert_not_reached();
711 68d000a3 Kevin Wolf
            ret = -EIO;
712 68d000a3 Kevin Wolf
            goto fail;
713 faf575c1 Frediano Ziglio
        }
714 f141eafe aliguori
715 3fc48d09 Frediano Ziglio
        remaining_sectors -= cur_nr_sectors;
716 3fc48d09 Frediano Ziglio
        sector_num += cur_nr_sectors;
717 3fc48d09 Frediano Ziglio
        bytes_done += cur_nr_sectors * 512;
718 5ebaa27e Frediano Ziglio
    }
719 3fc48d09 Frediano Ziglio
    ret = 0;
720 faf575c1 Frediano Ziglio
721 3fc48d09 Frediano Ziglio
fail:
722 68d100e9 Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
723 42496d62 Kevin Wolf
724 3fc48d09 Frediano Ziglio
    qemu_iovec_destroy(&hd_qiov);
725 dea43a65 Frediano Ziglio
    qemu_vfree(cluster_data);
726 68d100e9 Kevin Wolf
727 68d100e9 Kevin Wolf
    return ret;
728 585f8587 bellard
}
729 585f8587 bellard
730 68d100e9 Kevin Wolf
static void run_dependent_requests(BDRVQcowState *s, QCowL2Meta *m)
731 f214978a Kevin Wolf
{
732 f214978a Kevin Wolf
    /* Take the request off the list of running requests */
733 f214978a Kevin Wolf
    if (m->nb_clusters != 0) {
734 72cf2d4f Blue Swirl
        QLIST_REMOVE(m, next_in_flight);
735 f214978a Kevin Wolf
    }
736 f214978a Kevin Wolf
737 d4c146f0 Stefan Hajnoczi
    /* Restart all dependent requests */
738 68d100e9 Kevin Wolf
    if (!qemu_co_queue_empty(&m->dependent_requests)) {
739 68d100e9 Kevin Wolf
        qemu_co_mutex_unlock(&s->lock);
740 e8ee5e4c Stefan Hajnoczi
        qemu_co_queue_restart_all(&m->dependent_requests);
741 68d100e9 Kevin Wolf
        qemu_co_mutex_lock(&s->lock);
742 f214978a Kevin Wolf
    }
743 f214978a Kevin Wolf
}
744 f214978a Kevin Wolf
745 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
746 3fc48d09 Frediano Ziglio
                           int64_t sector_num,
747 3fc48d09 Frediano Ziglio
                           int remaining_sectors,
748 3fc48d09 Frediano Ziglio
                           QEMUIOVector *qiov)
749 585f8587 bellard
{
750 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
751 585f8587 bellard
    int index_in_cluster;
752 095a9c58 aliguori
    int n_end;
753 68d100e9 Kevin Wolf
    int ret;
754 faf575c1 Frediano Ziglio
    int cur_nr_sectors; /* number of sectors in current iteration */
755 c2bdd990 Frediano Ziglio
    uint64_t cluster_offset;
756 3fc48d09 Frediano Ziglio
    QEMUIOVector hd_qiov;
757 3fc48d09 Frediano Ziglio
    uint64_t bytes_done = 0;
758 3fc48d09 Frediano Ziglio
    uint8_t *cluster_data = NULL;
759 8e217d53 Kevin Wolf
    QCowL2Meta l2meta = {
760 8e217d53 Kevin Wolf
        .nb_clusters = 0,
761 8e217d53 Kevin Wolf
    };
762 c2271403 Frediano Ziglio
763 3cce16f4 Kevin Wolf
    trace_qcow2_writev_start_req(qemu_coroutine_self(), sector_num,
764 3cce16f4 Kevin Wolf
                                 remaining_sectors);
765 3cce16f4 Kevin Wolf
766 c2271403 Frediano Ziglio
    qemu_co_queue_init(&l2meta.dependent_requests);
767 585f8587 bellard
768 3fc48d09 Frediano Ziglio
    qemu_iovec_init(&hd_qiov, qiov->niov);
769 3fc48d09 Frediano Ziglio
770 3fc48d09 Frediano Ziglio
    s->cluster_cache_offset = -1; /* disable compressed cache */
771 3b46e624 ths
772 3fc48d09 Frediano Ziglio
    qemu_co_mutex_lock(&s->lock);
773 3fc48d09 Frediano Ziglio
774 3fc48d09 Frediano Ziglio
    while (remaining_sectors != 0) {
775 3fc48d09 Frediano Ziglio
776 3cce16f4 Kevin Wolf
        trace_qcow2_writev_start_part(qemu_coroutine_self());
777 3fc48d09 Frediano Ziglio
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
778 3fc48d09 Frediano Ziglio
        n_end = index_in_cluster + remaining_sectors;
779 5ebaa27e Frediano Ziglio
        if (s->crypt_method &&
780 5ebaa27e Frediano Ziglio
            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) {
781 5ebaa27e Frediano Ziglio
            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
782 5ebaa27e Frediano Ziglio
        }
783 095a9c58 aliguori
784 3fc48d09 Frediano Ziglio
        ret = qcow2_alloc_cluster_offset(bs, sector_num << 9,
785 5ebaa27e Frediano Ziglio
            index_in_cluster, n_end, &cur_nr_sectors, &l2meta);
786 5ebaa27e Frediano Ziglio
        if (ret < 0) {
787 3fc48d09 Frediano Ziglio
            goto fail;
788 5ebaa27e Frediano Ziglio
        }
789 148da7ea Kevin Wolf
790 bfe8043e Stefan Hajnoczi
        if (l2meta.nb_clusters > 0 &&
791 bfe8043e Stefan Hajnoczi
            (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS)) {
792 bfe8043e Stefan Hajnoczi
            qcow2_mark_dirty(bs);
793 bfe8043e Stefan Hajnoczi
        }
794 bfe8043e Stefan Hajnoczi
795 5ebaa27e Frediano Ziglio
        cluster_offset = l2meta.cluster_offset;
796 5ebaa27e Frediano Ziglio
        assert((cluster_offset & 511) == 0);
797 148da7ea Kevin Wolf
798 3fc48d09 Frediano Ziglio
        qemu_iovec_reset(&hd_qiov);
799 1b093c48 Michael Tokarev
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
800 5ebaa27e Frediano Ziglio
            cur_nr_sectors * 512);
801 6f5f060b Kevin Wolf
802 5ebaa27e Frediano Ziglio
        if (s->crypt_method) {
803 3fc48d09 Frediano Ziglio
            if (!cluster_data) {
804 dea43a65 Frediano Ziglio
                cluster_data = qemu_blockalign(bs, QCOW_MAX_CRYPT_CLUSTERS *
805 5ebaa27e Frediano Ziglio
                                                 s->cluster_size);
806 5ebaa27e Frediano Ziglio
            }
807 6f5f060b Kevin Wolf
808 3fc48d09 Frediano Ziglio
            assert(hd_qiov.size <=
809 5ebaa27e Frediano Ziglio
                   QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
810 d5e6b161 Michael Tokarev
            qemu_iovec_to_buf(&hd_qiov, 0, cluster_data, hd_qiov.size);
811 6f5f060b Kevin Wolf
812 3fc48d09 Frediano Ziglio
            qcow2_encrypt_sectors(s, sector_num, cluster_data,
813 3fc48d09 Frediano Ziglio
                cluster_data, cur_nr_sectors, 1, &s->aes_encrypt_key);
814 6f5f060b Kevin Wolf
815 3fc48d09 Frediano Ziglio
            qemu_iovec_reset(&hd_qiov);
816 3fc48d09 Frediano Ziglio
            qemu_iovec_add(&hd_qiov, cluster_data,
817 5ebaa27e Frediano Ziglio
                cur_nr_sectors * 512);
818 5ebaa27e Frediano Ziglio
        }
819 6f5f060b Kevin Wolf
820 5ebaa27e Frediano Ziglio
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
821 5ebaa27e Frediano Ziglio
        qemu_co_mutex_unlock(&s->lock);
822 3cce16f4 Kevin Wolf
        trace_qcow2_writev_data(qemu_coroutine_self(),
823 3cce16f4 Kevin Wolf
                                (cluster_offset >> 9) + index_in_cluster);
824 5ebaa27e Frediano Ziglio
        ret = bdrv_co_writev(bs->file,
825 5ebaa27e Frediano Ziglio
                             (cluster_offset >> 9) + index_in_cluster,
826 3fc48d09 Frediano Ziglio
                             cur_nr_sectors, &hd_qiov);
827 5ebaa27e Frediano Ziglio
        qemu_co_mutex_lock(&s->lock);
828 5ebaa27e Frediano Ziglio
        if (ret < 0) {
829 3fc48d09 Frediano Ziglio
            goto fail;
830 5ebaa27e Frediano Ziglio
        }
831 f141eafe aliguori
832 5ebaa27e Frediano Ziglio
        ret = qcow2_alloc_cluster_link_l2(bs, &l2meta);
833 5ebaa27e Frediano Ziglio
        if (ret < 0) {
834 3fc48d09 Frediano Ziglio
            goto fail;
835 5ebaa27e Frediano Ziglio
        }
836 faf575c1 Frediano Ziglio
837 0fa9131a Kevin Wolf
        run_dependent_requests(s, &l2meta);
838 0fa9131a Kevin Wolf
839 3fc48d09 Frediano Ziglio
        remaining_sectors -= cur_nr_sectors;
840 3fc48d09 Frediano Ziglio
        sector_num += cur_nr_sectors;
841 3fc48d09 Frediano Ziglio
        bytes_done += cur_nr_sectors * 512;
842 3cce16f4 Kevin Wolf
        trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_nr_sectors);
843 5ebaa27e Frediano Ziglio
    }
844 3fc48d09 Frediano Ziglio
    ret = 0;
845 faf575c1 Frediano Ziglio
846 3fc48d09 Frediano Ziglio
fail:
847 0fa9131a Kevin Wolf
    run_dependent_requests(s, &l2meta);
848 0fa9131a Kevin Wolf
849 68d100e9 Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
850 3b46e624 ths
851 3fc48d09 Frediano Ziglio
    qemu_iovec_destroy(&hd_qiov);
852 dea43a65 Frediano Ziglio
    qemu_vfree(cluster_data);
853 3cce16f4 Kevin Wolf
    trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
854 42496d62 Kevin Wolf
855 68d100e9 Kevin Wolf
    return ret;
856 585f8587 bellard
}
857 585f8587 bellard
858 7c80ab3f Jes Sorensen
static void qcow2_close(BlockDriverState *bs)
859 585f8587 bellard
{
860 585f8587 bellard
    BDRVQcowState *s = bs->opaque;
861 7267c094 Anthony Liguori
    g_free(s->l1_table);
862 29c1a730 Kevin Wolf
863 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->l2_table_cache);
864 29c1a730 Kevin Wolf
    qcow2_cache_flush(bs, s->refcount_block_cache);
865 29c1a730 Kevin Wolf
866 c61d0004 Stefan Hajnoczi
    qcow2_mark_clean(bs);
867 c61d0004 Stefan Hajnoczi
868 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->l2_table_cache);
869 29c1a730 Kevin Wolf
    qcow2_cache_destroy(bs, s->refcount_block_cache);
870 29c1a730 Kevin Wolf
871 6744cbab Kevin Wolf
    g_free(s->unknown_header_fields);
872 75bab85c Kevin Wolf
    cleanup_unknown_header_ext(bs);
873 6744cbab Kevin Wolf
874 7267c094 Anthony Liguori
    g_free(s->cluster_cache);
875 dea43a65 Frediano Ziglio
    qemu_vfree(s->cluster_data);
876 ed6ccf0f Kevin Wolf
    qcow2_refcount_close(bs);
877 28c1202b Li Zhi Hui
    qcow2_free_snapshots(bs);
878 585f8587 bellard
}
879 585f8587 bellard
880 06d9260f Anthony Liguori
static void qcow2_invalidate_cache(BlockDriverState *bs)
881 06d9260f Anthony Liguori
{
882 06d9260f Anthony Liguori
    BDRVQcowState *s = bs->opaque;
883 06d9260f Anthony Liguori
    int flags = s->flags;
884 06d9260f Anthony Liguori
    AES_KEY aes_encrypt_key;
885 06d9260f Anthony Liguori
    AES_KEY aes_decrypt_key;
886 06d9260f Anthony Liguori
    uint32_t crypt_method = 0;
887 06d9260f Anthony Liguori
888 06d9260f Anthony Liguori
    /*
889 06d9260f Anthony Liguori
     * Backing files are read-only which makes all of their metadata immutable,
890 06d9260f Anthony Liguori
     * that means we don't have to worry about reopening them here.
891 06d9260f Anthony Liguori
     */
892 06d9260f Anthony Liguori
893 06d9260f Anthony Liguori
    if (s->crypt_method) {
894 06d9260f Anthony Liguori
        crypt_method = s->crypt_method;
895 06d9260f Anthony Liguori
        memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
896 06d9260f Anthony Liguori
        memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
897 06d9260f Anthony Liguori
    }
898 06d9260f Anthony Liguori
899 06d9260f Anthony Liguori
    qcow2_close(bs);
900 06d9260f Anthony Liguori
901 06d9260f Anthony Liguori
    memset(s, 0, sizeof(BDRVQcowState));
902 06d9260f Anthony Liguori
    qcow2_open(bs, flags);
903 06d9260f Anthony Liguori
904 06d9260f Anthony Liguori
    if (crypt_method) {
905 06d9260f Anthony Liguori
        s->crypt_method = crypt_method;
906 06d9260f Anthony Liguori
        memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
907 06d9260f Anthony Liguori
        memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
908 06d9260f Anthony Liguori
    }
909 06d9260f Anthony Liguori
}
910 06d9260f Anthony Liguori
911 e24e49e6 Kevin Wolf
static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
912 e24e49e6 Kevin Wolf
    size_t len, size_t buflen)
913 e24e49e6 Kevin Wolf
{
914 e24e49e6 Kevin Wolf
    QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
915 e24e49e6 Kevin Wolf
    size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
916 e24e49e6 Kevin Wolf
917 e24e49e6 Kevin Wolf
    if (buflen < ext_len) {
918 e24e49e6 Kevin Wolf
        return -ENOSPC;
919 e24e49e6 Kevin Wolf
    }
920 e24e49e6 Kevin Wolf
921 e24e49e6 Kevin Wolf
    *ext_backing_fmt = (QCowExtension) {
922 e24e49e6 Kevin Wolf
        .magic  = cpu_to_be32(magic),
923 e24e49e6 Kevin Wolf
        .len    = cpu_to_be32(len),
924 e24e49e6 Kevin Wolf
    };
925 e24e49e6 Kevin Wolf
    memcpy(buf + sizeof(QCowExtension), s, len);
926 e24e49e6 Kevin Wolf
927 e24e49e6 Kevin Wolf
    return ext_len;
928 e24e49e6 Kevin Wolf
}
929 e24e49e6 Kevin Wolf
930 756e6736 Kevin Wolf
/*
931 e24e49e6 Kevin Wolf
 * Updates the qcow2 header, including the variable length parts of it, i.e.
932 e24e49e6 Kevin Wolf
 * the backing file name and all extensions. qcow2 was not designed to allow
933 e24e49e6 Kevin Wolf
 * such changes, so if we run out of space (we can only use the first cluster)
934 e24e49e6 Kevin Wolf
 * this function may fail.
935 756e6736 Kevin Wolf
 *
936 756e6736 Kevin Wolf
 * Returns 0 on success, -errno in error cases.
937 756e6736 Kevin Wolf
 */
938 e24e49e6 Kevin Wolf
int qcow2_update_header(BlockDriverState *bs)
939 756e6736 Kevin Wolf
{
940 756e6736 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
941 e24e49e6 Kevin Wolf
    QCowHeader *header;
942 e24e49e6 Kevin Wolf
    char *buf;
943 e24e49e6 Kevin Wolf
    size_t buflen = s->cluster_size;
944 756e6736 Kevin Wolf
    int ret;
945 e24e49e6 Kevin Wolf
    uint64_t total_size;
946 e24e49e6 Kevin Wolf
    uint32_t refcount_table_clusters;
947 6744cbab Kevin Wolf
    size_t header_length;
948 75bab85c Kevin Wolf
    Qcow2UnknownHeaderExtension *uext;
949 756e6736 Kevin Wolf
950 e24e49e6 Kevin Wolf
    buf = qemu_blockalign(bs, buflen);
951 756e6736 Kevin Wolf
952 e24e49e6 Kevin Wolf
    /* Header structure */
953 e24e49e6 Kevin Wolf
    header = (QCowHeader*) buf;
954 756e6736 Kevin Wolf
955 e24e49e6 Kevin Wolf
    if (buflen < sizeof(*header)) {
956 e24e49e6 Kevin Wolf
        ret = -ENOSPC;
957 e24e49e6 Kevin Wolf
        goto fail;
958 756e6736 Kevin Wolf
    }
959 756e6736 Kevin Wolf
960 6744cbab Kevin Wolf
    header_length = sizeof(*header) + s->unknown_header_fields_size;
961 e24e49e6 Kevin Wolf
    total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
962 e24e49e6 Kevin Wolf
    refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
963 e24e49e6 Kevin Wolf
964 e24e49e6 Kevin Wolf
    *header = (QCowHeader) {
965 6744cbab Kevin Wolf
        /* Version 2 fields */
966 e24e49e6 Kevin Wolf
        .magic                  = cpu_to_be32(QCOW_MAGIC),
967 6744cbab Kevin Wolf
        .version                = cpu_to_be32(s->qcow_version),
968 e24e49e6 Kevin Wolf
        .backing_file_offset    = 0,
969 e24e49e6 Kevin Wolf
        .backing_file_size      = 0,
970 e24e49e6 Kevin Wolf
        .cluster_bits           = cpu_to_be32(s->cluster_bits),
971 e24e49e6 Kevin Wolf
        .size                   = cpu_to_be64(total_size),
972 e24e49e6 Kevin Wolf
        .crypt_method           = cpu_to_be32(s->crypt_method_header),
973 e24e49e6 Kevin Wolf
        .l1_size                = cpu_to_be32(s->l1_size),
974 e24e49e6 Kevin Wolf
        .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
975 e24e49e6 Kevin Wolf
        .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
976 e24e49e6 Kevin Wolf
        .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
977 e24e49e6 Kevin Wolf
        .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
978 e24e49e6 Kevin Wolf
        .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
979 6744cbab Kevin Wolf
980 6744cbab Kevin Wolf
        /* Version 3 fields */
981 6744cbab Kevin Wolf
        .incompatible_features  = cpu_to_be64(s->incompatible_features),
982 6744cbab Kevin Wolf
        .compatible_features    = cpu_to_be64(s->compatible_features),
983 6744cbab Kevin Wolf
        .autoclear_features     = cpu_to_be64(s->autoclear_features),
984 6744cbab Kevin Wolf
        .refcount_order         = cpu_to_be32(3 + REFCOUNT_SHIFT),
985 6744cbab Kevin Wolf
        .header_length          = cpu_to_be32(header_length),
986 e24e49e6 Kevin Wolf
    };
987 756e6736 Kevin Wolf
988 6744cbab Kevin Wolf
    /* For older versions, write a shorter header */
989 6744cbab Kevin Wolf
    switch (s->qcow_version) {
990 6744cbab Kevin Wolf
    case 2:
991 6744cbab Kevin Wolf
        ret = offsetof(QCowHeader, incompatible_features);
992 6744cbab Kevin Wolf
        break;
993 6744cbab Kevin Wolf
    case 3:
994 6744cbab Kevin Wolf
        ret = sizeof(*header);
995 6744cbab Kevin Wolf
        break;
996 6744cbab Kevin Wolf
    default:
997 b6c14762 Jim Meyering
        ret = -EINVAL;
998 b6c14762 Jim Meyering
        goto fail;
999 6744cbab Kevin Wolf
    }
1000 6744cbab Kevin Wolf
1001 6744cbab Kevin Wolf
    buf += ret;
1002 6744cbab Kevin Wolf
    buflen -= ret;
1003 6744cbab Kevin Wolf
    memset(buf, 0, buflen);
1004 6744cbab Kevin Wolf
1005 6744cbab Kevin Wolf
    /* Preserve any unknown field in the header */
1006 6744cbab Kevin Wolf
    if (s->unknown_header_fields_size) {
1007 6744cbab Kevin Wolf
        if (buflen < s->unknown_header_fields_size) {
1008 6744cbab Kevin Wolf
            ret = -ENOSPC;
1009 6744cbab Kevin Wolf
            goto fail;
1010 6744cbab Kevin Wolf
        }
1011 6744cbab Kevin Wolf
1012 6744cbab Kevin Wolf
        memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
1013 6744cbab Kevin Wolf
        buf += s->unknown_header_fields_size;
1014 6744cbab Kevin Wolf
        buflen -= s->unknown_header_fields_size;
1015 6744cbab Kevin Wolf
    }
1016 756e6736 Kevin Wolf
1017 e24e49e6 Kevin Wolf
    /* Backing file format header extension */
1018 e24e49e6 Kevin Wolf
    if (*bs->backing_format) {
1019 e24e49e6 Kevin Wolf
        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
1020 e24e49e6 Kevin Wolf
                             bs->backing_format, strlen(bs->backing_format),
1021 e24e49e6 Kevin Wolf
                             buflen);
1022 e24e49e6 Kevin Wolf
        if (ret < 0) {
1023 e24e49e6 Kevin Wolf
            goto fail;
1024 756e6736 Kevin Wolf
        }
1025 756e6736 Kevin Wolf
1026 e24e49e6 Kevin Wolf
        buf += ret;
1027 e24e49e6 Kevin Wolf
        buflen -= ret;
1028 756e6736 Kevin Wolf
    }
1029 756e6736 Kevin Wolf
1030 cfcc4c62 Kevin Wolf
    /* Feature table */
1031 cfcc4c62 Kevin Wolf
    Qcow2Feature features[] = {
1032 c61d0004 Stefan Hajnoczi
        {
1033 c61d0004 Stefan Hajnoczi
            .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
1034 c61d0004 Stefan Hajnoczi
            .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
1035 c61d0004 Stefan Hajnoczi
            .name = "dirty bit",
1036 c61d0004 Stefan Hajnoczi
        },
1037 bfe8043e Stefan Hajnoczi
        {
1038 bfe8043e Stefan Hajnoczi
            .type = QCOW2_FEAT_TYPE_COMPATIBLE,
1039 bfe8043e Stefan Hajnoczi
            .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
1040 bfe8043e Stefan Hajnoczi
            .name = "lazy refcounts",
1041 bfe8043e Stefan Hajnoczi
        },
1042 cfcc4c62 Kevin Wolf
    };
1043 cfcc4c62 Kevin Wolf
1044 cfcc4c62 Kevin Wolf
    ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
1045 cfcc4c62 Kevin Wolf
                         features, sizeof(features), buflen);
1046 cfcc4c62 Kevin Wolf
    if (ret < 0) {
1047 cfcc4c62 Kevin Wolf
        goto fail;
1048 cfcc4c62 Kevin Wolf
    }
1049 cfcc4c62 Kevin Wolf
    buf += ret;
1050 cfcc4c62 Kevin Wolf
    buflen -= ret;
1051 cfcc4c62 Kevin Wolf
1052 75bab85c Kevin Wolf
    /* Keep unknown header extensions */
1053 75bab85c Kevin Wolf
    QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
1054 75bab85c Kevin Wolf
        ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
1055 75bab85c Kevin Wolf
        if (ret < 0) {
1056 75bab85c Kevin Wolf
            goto fail;
1057 75bab85c Kevin Wolf
        }
1058 75bab85c Kevin Wolf
1059 75bab85c Kevin Wolf
        buf += ret;
1060 75bab85c Kevin Wolf
        buflen -= ret;
1061 75bab85c Kevin Wolf
    }
1062 75bab85c Kevin Wolf
1063 e24e49e6 Kevin Wolf
    /* End of header extensions */
1064 e24e49e6 Kevin Wolf
    ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
1065 756e6736 Kevin Wolf
    if (ret < 0) {
1066 756e6736 Kevin Wolf
        goto fail;
1067 756e6736 Kevin Wolf
    }
1068 756e6736 Kevin Wolf
1069 e24e49e6 Kevin Wolf
    buf += ret;
1070 e24e49e6 Kevin Wolf
    buflen -= ret;
1071 756e6736 Kevin Wolf
1072 e24e49e6 Kevin Wolf
    /* Backing file name */
1073 e24e49e6 Kevin Wolf
    if (*bs->backing_file) {
1074 e24e49e6 Kevin Wolf
        size_t backing_file_len = strlen(bs->backing_file);
1075 e24e49e6 Kevin Wolf
1076 e24e49e6 Kevin Wolf
        if (buflen < backing_file_len) {
1077 e24e49e6 Kevin Wolf
            ret = -ENOSPC;
1078 e24e49e6 Kevin Wolf
            goto fail;
1079 e24e49e6 Kevin Wolf
        }
1080 e24e49e6 Kevin Wolf
1081 e24e49e6 Kevin Wolf
        strncpy(buf, bs->backing_file, buflen);
1082 e24e49e6 Kevin Wolf
1083 e24e49e6 Kevin Wolf
        header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
1084 e24e49e6 Kevin Wolf
        header->backing_file_size   = cpu_to_be32(backing_file_len);
1085 756e6736 Kevin Wolf
    }
1086 756e6736 Kevin Wolf
1087 e24e49e6 Kevin Wolf
    /* Write the new header */
1088 e24e49e6 Kevin Wolf
    ret = bdrv_pwrite(bs->file, 0, header, s->cluster_size);
1089 756e6736 Kevin Wolf
    if (ret < 0) {
1090 756e6736 Kevin Wolf
        goto fail;
1091 756e6736 Kevin Wolf
    }
1092 756e6736 Kevin Wolf
1093 756e6736 Kevin Wolf
    ret = 0;
1094 756e6736 Kevin Wolf
fail:
1095 e24e49e6 Kevin Wolf
    qemu_vfree(header);
1096 756e6736 Kevin Wolf
    return ret;
1097 756e6736 Kevin Wolf
}
1098 756e6736 Kevin Wolf
1099 756e6736 Kevin Wolf
static int qcow2_change_backing_file(BlockDriverState *bs,
1100 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
1101 756e6736 Kevin Wolf
{
1102 e24e49e6 Kevin Wolf
    pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1103 e24e49e6 Kevin Wolf
    pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1104 e24e49e6 Kevin Wolf
1105 e24e49e6 Kevin Wolf
    return qcow2_update_header(bs);
1106 756e6736 Kevin Wolf
}
1107 756e6736 Kevin Wolf
1108 a35e1c17 Kevin Wolf
static int preallocate(BlockDriverState *bs)
1109 a35e1c17 Kevin Wolf
{
1110 a35e1c17 Kevin Wolf
    uint64_t nb_sectors;
1111 a35e1c17 Kevin Wolf
    uint64_t offset;
1112 a35e1c17 Kevin Wolf
    int num;
1113 148da7ea Kevin Wolf
    int ret;
1114 a35e1c17 Kevin Wolf
    QCowL2Meta meta;
1115 a35e1c17 Kevin Wolf
1116 a35e1c17 Kevin Wolf
    nb_sectors = bdrv_getlength(bs) >> 9;
1117 a35e1c17 Kevin Wolf
    offset = 0;
1118 68d100e9 Kevin Wolf
    qemu_co_queue_init(&meta.dependent_requests);
1119 148da7ea Kevin Wolf
    meta.cluster_offset = 0;
1120 a35e1c17 Kevin Wolf
1121 a35e1c17 Kevin Wolf
    while (nb_sectors) {
1122 a35e1c17 Kevin Wolf
        num = MIN(nb_sectors, INT_MAX >> 9);
1123 148da7ea Kevin Wolf
        ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
1124 148da7ea Kevin Wolf
        if (ret < 0) {
1125 19dbcbf7 Kevin Wolf
            return ret;
1126 a35e1c17 Kevin Wolf
        }
1127 a35e1c17 Kevin Wolf
1128 19dbcbf7 Kevin Wolf
        ret = qcow2_alloc_cluster_link_l2(bs, &meta);
1129 19dbcbf7 Kevin Wolf
        if (ret < 0) {
1130 148da7ea Kevin Wolf
            qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters);
1131 19dbcbf7 Kevin Wolf
            return ret;
1132 a35e1c17 Kevin Wolf
        }
1133 a35e1c17 Kevin Wolf
1134 f214978a Kevin Wolf
        /* There are no dependent requests, but we need to remove our request
1135 f214978a Kevin Wolf
         * from the list of in-flight requests */
1136 68d100e9 Kevin Wolf
        run_dependent_requests(bs->opaque, &meta);
1137 f214978a Kevin Wolf
1138 a35e1c17 Kevin Wolf
        /* TODO Preallocate data if requested */
1139 a35e1c17 Kevin Wolf
1140 a35e1c17 Kevin Wolf
        nb_sectors -= num;
1141 a35e1c17 Kevin Wolf
        offset += num << 9;
1142 a35e1c17 Kevin Wolf
    }
1143 a35e1c17 Kevin Wolf
1144 a35e1c17 Kevin Wolf
    /*
1145 a35e1c17 Kevin Wolf
     * It is expected that the image file is large enough to actually contain
1146 a35e1c17 Kevin Wolf
     * all of the allocated clusters (otherwise we get failing reads after
1147 a35e1c17 Kevin Wolf
     * EOF). Extend the image to the last allocated sector.
1148 a35e1c17 Kevin Wolf
     */
1149 148da7ea Kevin Wolf
    if (meta.cluster_offset != 0) {
1150 ea80b906 Kevin Wolf
        uint8_t buf[512];
1151 ea80b906 Kevin Wolf
        memset(buf, 0, 512);
1152 19dbcbf7 Kevin Wolf
        ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1);
1153 19dbcbf7 Kevin Wolf
        if (ret < 0) {
1154 19dbcbf7 Kevin Wolf
            return ret;
1155 19dbcbf7 Kevin Wolf
        }
1156 a35e1c17 Kevin Wolf
    }
1157 a35e1c17 Kevin Wolf
1158 a35e1c17 Kevin Wolf
    return 0;
1159 a35e1c17 Kevin Wolf
}
1160 a35e1c17 Kevin Wolf
1161 7c80ab3f Jes Sorensen
static int qcow2_create2(const char *filename, int64_t total_size,
1162 7c80ab3f Jes Sorensen
                         const char *backing_file, const char *backing_format,
1163 7c80ab3f Jes Sorensen
                         int flags, size_t cluster_size, int prealloc,
1164 6744cbab Kevin Wolf
                         QEMUOptionParameter *options, int version)
1165 a9420734 Kevin Wolf
{
1166 9b2260cb Dong Xu Wang
    /* Calculate cluster_bits */
1167 a9420734 Kevin Wolf
    int cluster_bits;
1168 a9420734 Kevin Wolf
    cluster_bits = ffs(cluster_size) - 1;
1169 a9420734 Kevin Wolf
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1170 a9420734 Kevin Wolf
        (1 << cluster_bits) != cluster_size)
1171 a9420734 Kevin Wolf
    {
1172 a9420734 Kevin Wolf
        error_report(
1173 6daf194d Markus Armbruster
            "Cluster size must be a power of two between %d and %dk",
1174 a9420734 Kevin Wolf
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1175 a9420734 Kevin Wolf
        return -EINVAL;
1176 a9420734 Kevin Wolf
    }
1177 a9420734 Kevin Wolf
1178 a9420734 Kevin Wolf
    /*
1179 a9420734 Kevin Wolf
     * Open the image file and write a minimal qcow2 header.
1180 a9420734 Kevin Wolf
     *
1181 a9420734 Kevin Wolf
     * We keep things simple and start with a zero-sized image. We also
1182 a9420734 Kevin Wolf
     * do without refcount blocks or a L1 table for now. We'll fix the
1183 a9420734 Kevin Wolf
     * inconsistency later.
1184 a9420734 Kevin Wolf
     *
1185 a9420734 Kevin Wolf
     * We do need a refcount table because growing the refcount table means
1186 a9420734 Kevin Wolf
     * allocating two new refcount blocks - the seconds of which would be at
1187 a9420734 Kevin Wolf
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1188 a9420734 Kevin Wolf
     * size for any qcow2 image.
1189 a9420734 Kevin Wolf
     */
1190 a9420734 Kevin Wolf
    BlockDriverState* bs;
1191 a9420734 Kevin Wolf
    QCowHeader header;
1192 a9420734 Kevin Wolf
    uint8_t* refcount_table;
1193 a9420734 Kevin Wolf
    int ret;
1194 a9420734 Kevin Wolf
1195 a9420734 Kevin Wolf
    ret = bdrv_create_file(filename, options);
1196 a9420734 Kevin Wolf
    if (ret < 0) {
1197 a9420734 Kevin Wolf
        return ret;
1198 a9420734 Kevin Wolf
    }
1199 a9420734 Kevin Wolf
1200 a9420734 Kevin Wolf
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
1201 a9420734 Kevin Wolf
    if (ret < 0) {
1202 a9420734 Kevin Wolf
        return ret;
1203 a9420734 Kevin Wolf
    }
1204 a9420734 Kevin Wolf
1205 a9420734 Kevin Wolf
    /* Write the header */
1206 a9420734 Kevin Wolf
    memset(&header, 0, sizeof(header));
1207 a9420734 Kevin Wolf
    header.magic = cpu_to_be32(QCOW_MAGIC);
1208 6744cbab Kevin Wolf
    header.version = cpu_to_be32(version);
1209 a9420734 Kevin Wolf
    header.cluster_bits = cpu_to_be32(cluster_bits);
1210 a9420734 Kevin Wolf
    header.size = cpu_to_be64(0);
1211 a9420734 Kevin Wolf
    header.l1_table_offset = cpu_to_be64(0);
1212 a9420734 Kevin Wolf
    header.l1_size = cpu_to_be32(0);
1213 a9420734 Kevin Wolf
    header.refcount_table_offset = cpu_to_be64(cluster_size);
1214 a9420734 Kevin Wolf
    header.refcount_table_clusters = cpu_to_be32(1);
1215 6744cbab Kevin Wolf
    header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
1216 6744cbab Kevin Wolf
    header.header_length = cpu_to_be32(sizeof(header));
1217 a9420734 Kevin Wolf
1218 a9420734 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
1219 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1220 a9420734 Kevin Wolf
    } else {
1221 a9420734 Kevin Wolf
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1222 a9420734 Kevin Wolf
    }
1223 a9420734 Kevin Wolf
1224 bfe8043e Stefan Hajnoczi
    if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
1225 bfe8043e Stefan Hajnoczi
        header.compatible_features |=
1226 bfe8043e Stefan Hajnoczi
            cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
1227 bfe8043e Stefan Hajnoczi
    }
1228 bfe8043e Stefan Hajnoczi
1229 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1230 a9420734 Kevin Wolf
    if (ret < 0) {
1231 a9420734 Kevin Wolf
        goto out;
1232 a9420734 Kevin Wolf
    }
1233 a9420734 Kevin Wolf
1234 a9420734 Kevin Wolf
    /* Write an empty refcount table */
1235 7267c094 Anthony Liguori
    refcount_table = g_malloc0(cluster_size);
1236 a9420734 Kevin Wolf
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
1237 7267c094 Anthony Liguori
    g_free(refcount_table);
1238 a9420734 Kevin Wolf
1239 a9420734 Kevin Wolf
    if (ret < 0) {
1240 a9420734 Kevin Wolf
        goto out;
1241 a9420734 Kevin Wolf
    }
1242 a9420734 Kevin Wolf
1243 a9420734 Kevin Wolf
    bdrv_close(bs);
1244 a9420734 Kevin Wolf
1245 a9420734 Kevin Wolf
    /*
1246 a9420734 Kevin Wolf
     * And now open the image and make it consistent first (i.e. increase the
1247 a9420734 Kevin Wolf
     * refcount of the cluster that is occupied by the header and the refcount
1248 a9420734 Kevin Wolf
     * table)
1249 a9420734 Kevin Wolf
     */
1250 a9420734 Kevin Wolf
    BlockDriver* drv = bdrv_find_format("qcow2");
1251 a9420734 Kevin Wolf
    assert(drv != NULL);
1252 e1a7107f Kevin Wolf
    ret = bdrv_open(bs, filename,
1253 e1a7107f Kevin Wolf
        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
1254 a9420734 Kevin Wolf
    if (ret < 0) {
1255 a9420734 Kevin Wolf
        goto out;
1256 a9420734 Kevin Wolf
    }
1257 a9420734 Kevin Wolf
1258 a9420734 Kevin Wolf
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1259 a9420734 Kevin Wolf
    if (ret < 0) {
1260 a9420734 Kevin Wolf
        goto out;
1261 a9420734 Kevin Wolf
1262 a9420734 Kevin Wolf
    } else if (ret != 0) {
1263 a9420734 Kevin Wolf
        error_report("Huh, first cluster in empty image is already in use?");
1264 a9420734 Kevin Wolf
        abort();
1265 a9420734 Kevin Wolf
    }
1266 a9420734 Kevin Wolf
1267 a9420734 Kevin Wolf
    /* Okay, now that we have a valid image, let's give it the right size */
1268 a9420734 Kevin Wolf
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1269 a9420734 Kevin Wolf
    if (ret < 0) {
1270 a9420734 Kevin Wolf
        goto out;
1271 a9420734 Kevin Wolf
    }
1272 a9420734 Kevin Wolf
1273 a9420734 Kevin Wolf
    /* Want a backing file? There you go.*/
1274 a9420734 Kevin Wolf
    if (backing_file) {
1275 a9420734 Kevin Wolf
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1276 a9420734 Kevin Wolf
        if (ret < 0) {
1277 a9420734 Kevin Wolf
            goto out;
1278 a9420734 Kevin Wolf
        }
1279 a9420734 Kevin Wolf
    }
1280 a9420734 Kevin Wolf
1281 a9420734 Kevin Wolf
    /* And if we're supposed to preallocate metadata, do that now */
1282 a9420734 Kevin Wolf
    if (prealloc) {
1283 15552c4a Zhi Yong Wu
        BDRVQcowState *s = bs->opaque;
1284 15552c4a Zhi Yong Wu
        qemu_co_mutex_lock(&s->lock);
1285 a9420734 Kevin Wolf
        ret = preallocate(bs);
1286 15552c4a Zhi Yong Wu
        qemu_co_mutex_unlock(&s->lock);
1287 a9420734 Kevin Wolf
        if (ret < 0) {
1288 a9420734 Kevin Wolf
            goto out;
1289 a9420734 Kevin Wolf
        }
1290 a9420734 Kevin Wolf
    }
1291 a9420734 Kevin Wolf
1292 a9420734 Kevin Wolf
    ret = 0;
1293 a9420734 Kevin Wolf
out:
1294 a9420734 Kevin Wolf
    bdrv_delete(bs);
1295 a9420734 Kevin Wolf
    return ret;
1296 a9420734 Kevin Wolf
}
1297 de5f3f40 Kevin Wolf
1298 7c80ab3f Jes Sorensen
static int qcow2_create(const char *filename, QEMUOptionParameter *options)
1299 de5f3f40 Kevin Wolf
{
1300 de5f3f40 Kevin Wolf
    const char *backing_file = NULL;
1301 de5f3f40 Kevin Wolf
    const char *backing_fmt = NULL;
1302 de5f3f40 Kevin Wolf
    uint64_t sectors = 0;
1303 de5f3f40 Kevin Wolf
    int flags = 0;
1304 99cce9fa Kevin Wolf
    size_t cluster_size = DEFAULT_CLUSTER_SIZE;
1305 de5f3f40 Kevin Wolf
    int prealloc = 0;
1306 6744cbab Kevin Wolf
    int version = 2;
1307 de5f3f40 Kevin Wolf
1308 de5f3f40 Kevin Wolf
    /* Read out options */
1309 de5f3f40 Kevin Wolf
    while (options && options->name) {
1310 de5f3f40 Kevin Wolf
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1311 de5f3f40 Kevin Wolf
            sectors = options->value.n / 512;
1312 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1313 de5f3f40 Kevin Wolf
            backing_file = options->value.s;
1314 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) {
1315 de5f3f40 Kevin Wolf
            backing_fmt = options->value.s;
1316 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
1317 de5f3f40 Kevin Wolf
            flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
1318 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
1319 de5f3f40 Kevin Wolf
            if (options->value.n) {
1320 de5f3f40 Kevin Wolf
                cluster_size = options->value.n;
1321 de5f3f40 Kevin Wolf
            }
1322 de5f3f40 Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1323 de5f3f40 Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "off")) {
1324 de5f3f40 Kevin Wolf
                prealloc = 0;
1325 de5f3f40 Kevin Wolf
            } else if (!strcmp(options->value.s, "metadata")) {
1326 de5f3f40 Kevin Wolf
                prealloc = 1;
1327 de5f3f40 Kevin Wolf
            } else {
1328 de5f3f40 Kevin Wolf
                fprintf(stderr, "Invalid preallocation mode: '%s'\n",
1329 de5f3f40 Kevin Wolf
                    options->value.s);
1330 de5f3f40 Kevin Wolf
                return -EINVAL;
1331 de5f3f40 Kevin Wolf
            }
1332 6744cbab Kevin Wolf
        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
1333 6744cbab Kevin Wolf
            if (!options->value.s || !strcmp(options->value.s, "0.10")) {
1334 6744cbab Kevin Wolf
                version = 2;
1335 6744cbab Kevin Wolf
            } else if (!strcmp(options->value.s, "1.1")) {
1336 6744cbab Kevin Wolf
                version = 3;
1337 6744cbab Kevin Wolf
            } else {
1338 6744cbab Kevin Wolf
                fprintf(stderr, "Invalid compatibility level: '%s'\n",
1339 6744cbab Kevin Wolf
                    options->value.s);
1340 6744cbab Kevin Wolf
                return -EINVAL;
1341 6744cbab Kevin Wolf
            }
1342 bfe8043e Stefan Hajnoczi
        } else if (!strcmp(options->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
1343 bfe8043e Stefan Hajnoczi
            flags |= options->value.n ? BLOCK_FLAG_LAZY_REFCOUNTS : 0;
1344 de5f3f40 Kevin Wolf
        }
1345 de5f3f40 Kevin Wolf
        options++;
1346 de5f3f40 Kevin Wolf
    }
1347 de5f3f40 Kevin Wolf
1348 de5f3f40 Kevin Wolf
    if (backing_file && prealloc) {
1349 de5f3f40 Kevin Wolf
        fprintf(stderr, "Backing file and preallocation cannot be used at "
1350 de5f3f40 Kevin Wolf
            "the same time\n");
1351 de5f3f40 Kevin Wolf
        return -EINVAL;
1352 de5f3f40 Kevin Wolf
    }
1353 de5f3f40 Kevin Wolf
1354 bfe8043e Stefan Hajnoczi
    if (version < 3 && (flags & BLOCK_FLAG_LAZY_REFCOUNTS)) {
1355 bfe8043e Stefan Hajnoczi
        fprintf(stderr, "Lazy refcounts only supported with compatibility "
1356 bfe8043e Stefan Hajnoczi
                "level 1.1 and above (use compat=1.1 or greater)\n");
1357 bfe8043e Stefan Hajnoczi
        return -EINVAL;
1358 bfe8043e Stefan Hajnoczi
    }
1359 bfe8043e Stefan Hajnoczi
1360 7c80ab3f Jes Sorensen
    return qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
1361 6744cbab Kevin Wolf
                         cluster_size, prealloc, options, version);
1362 de5f3f40 Kevin Wolf
}
1363 de5f3f40 Kevin Wolf
1364 7c80ab3f Jes Sorensen
static int qcow2_make_empty(BlockDriverState *bs)
1365 20d97356 Blue Swirl
{
1366 20d97356 Blue Swirl
#if 0
1367 20d97356 Blue Swirl
    /* XXX: not correct */
1368 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1369 20d97356 Blue Swirl
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1370 20d97356 Blue Swirl
    int ret;
1371 20d97356 Blue Swirl

1372 20d97356 Blue Swirl
    memset(s->l1_table, 0, l1_length);
1373 66f82cee Kevin Wolf
    if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
1374 20d97356 Blue Swirl
        return -1;
1375 66f82cee Kevin Wolf
    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
1376 20d97356 Blue Swirl
    if (ret < 0)
1377 20d97356 Blue Swirl
        return ret;
1378 20d97356 Blue Swirl

1379 20d97356 Blue Swirl
    l2_cache_reset(bs);
1380 20d97356 Blue Swirl
#endif
1381 20d97356 Blue Swirl
    return 0;
1382 20d97356 Blue Swirl
}
1383 20d97356 Blue Swirl
1384 621f0589 Kevin Wolf
static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
1385 621f0589 Kevin Wolf
    int64_t sector_num, int nb_sectors)
1386 621f0589 Kevin Wolf
{
1387 621f0589 Kevin Wolf
    int ret;
1388 621f0589 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1389 621f0589 Kevin Wolf
1390 621f0589 Kevin Wolf
    /* Emulate misaligned zero writes */
1391 621f0589 Kevin Wolf
    if (sector_num % s->cluster_sectors || nb_sectors % s->cluster_sectors) {
1392 621f0589 Kevin Wolf
        return -ENOTSUP;
1393 621f0589 Kevin Wolf
    }
1394 621f0589 Kevin Wolf
1395 621f0589 Kevin Wolf
    /* Whatever is left can use real zero clusters */
1396 621f0589 Kevin Wolf
    qemu_co_mutex_lock(&s->lock);
1397 621f0589 Kevin Wolf
    ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1398 621f0589 Kevin Wolf
        nb_sectors);
1399 621f0589 Kevin Wolf
    qemu_co_mutex_unlock(&s->lock);
1400 621f0589 Kevin Wolf
1401 621f0589 Kevin Wolf
    return ret;
1402 621f0589 Kevin Wolf
}
1403 621f0589 Kevin Wolf
1404 6db39ae2 Paolo Bonzini
static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
1405 6db39ae2 Paolo Bonzini
    int64_t sector_num, int nb_sectors)
1406 5ea929e3 Kevin Wolf
{
1407 6db39ae2 Paolo Bonzini
    int ret;
1408 6db39ae2 Paolo Bonzini
    BDRVQcowState *s = bs->opaque;
1409 6db39ae2 Paolo Bonzini
1410 6db39ae2 Paolo Bonzini
    qemu_co_mutex_lock(&s->lock);
1411 6db39ae2 Paolo Bonzini
    ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
1412 5ea929e3 Kevin Wolf
        nb_sectors);
1413 6db39ae2 Paolo Bonzini
    qemu_co_mutex_unlock(&s->lock);
1414 6db39ae2 Paolo Bonzini
    return ret;
1415 5ea929e3 Kevin Wolf
}
1416 5ea929e3 Kevin Wolf
1417 419b19d9 Stefan Hajnoczi
static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
1418 419b19d9 Stefan Hajnoczi
{
1419 419b19d9 Stefan Hajnoczi
    BDRVQcowState *s = bs->opaque;
1420 419b19d9 Stefan Hajnoczi
    int ret, new_l1_size;
1421 419b19d9 Stefan Hajnoczi
1422 419b19d9 Stefan Hajnoczi
    if (offset & 511) {
1423 259b2173 Kevin Wolf
        error_report("The new size must be a multiple of 512");
1424 419b19d9 Stefan Hajnoczi
        return -EINVAL;
1425 419b19d9 Stefan Hajnoczi
    }
1426 419b19d9 Stefan Hajnoczi
1427 419b19d9 Stefan Hajnoczi
    /* cannot proceed if image has snapshots */
1428 419b19d9 Stefan Hajnoczi
    if (s->nb_snapshots) {
1429 259b2173 Kevin Wolf
        error_report("Can't resize an image which has snapshots");
1430 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1431 419b19d9 Stefan Hajnoczi
    }
1432 419b19d9 Stefan Hajnoczi
1433 419b19d9 Stefan Hajnoczi
    /* shrinking is currently not supported */
1434 419b19d9 Stefan Hajnoczi
    if (offset < bs->total_sectors * 512) {
1435 259b2173 Kevin Wolf
        error_report("qcow2 doesn't support shrinking images yet");
1436 419b19d9 Stefan Hajnoczi
        return -ENOTSUP;
1437 419b19d9 Stefan Hajnoczi
    }
1438 419b19d9 Stefan Hajnoczi
1439 419b19d9 Stefan Hajnoczi
    new_l1_size = size_to_l1(s, offset);
1440 72893756 Stefan Hajnoczi
    ret = qcow2_grow_l1_table(bs, new_l1_size, true);
1441 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1442 419b19d9 Stefan Hajnoczi
        return ret;
1443 419b19d9 Stefan Hajnoczi
    }
1444 419b19d9 Stefan Hajnoczi
1445 419b19d9 Stefan Hajnoczi
    /* write updated header.size */
1446 419b19d9 Stefan Hajnoczi
    offset = cpu_to_be64(offset);
1447 8b3b7206 Kevin Wolf
    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size),
1448 8b3b7206 Kevin Wolf
                           &offset, sizeof(uint64_t));
1449 419b19d9 Stefan Hajnoczi
    if (ret < 0) {
1450 419b19d9 Stefan Hajnoczi
        return ret;
1451 419b19d9 Stefan Hajnoczi
    }
1452 419b19d9 Stefan Hajnoczi
1453 419b19d9 Stefan Hajnoczi
    s->l1_vm_state_index = new_l1_size;
1454 419b19d9 Stefan Hajnoczi
    return 0;
1455 419b19d9 Stefan Hajnoczi
}
1456 419b19d9 Stefan Hajnoczi
1457 20d97356 Blue Swirl
/* XXX: put compressed sectors first, then all the cluster aligned
1458 20d97356 Blue Swirl
   tables to avoid losing bytes in alignment */
1459 7c80ab3f Jes Sorensen
static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
1460 7c80ab3f Jes Sorensen
                                  const uint8_t *buf, int nb_sectors)
1461 20d97356 Blue Swirl
{
1462 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1463 20d97356 Blue Swirl
    z_stream strm;
1464 20d97356 Blue Swirl
    int ret, out_len;
1465 20d97356 Blue Swirl
    uint8_t *out_buf;
1466 20d97356 Blue Swirl
    uint64_t cluster_offset;
1467 20d97356 Blue Swirl
1468 20d97356 Blue Swirl
    if (nb_sectors == 0) {
1469 20d97356 Blue Swirl
        /* align end of file to a sector boundary to ease reading with
1470 20d97356 Blue Swirl
           sector based I/Os */
1471 66f82cee Kevin Wolf
        cluster_offset = bdrv_getlength(bs->file);
1472 20d97356 Blue Swirl
        cluster_offset = (cluster_offset + 511) & ~511;
1473 66f82cee Kevin Wolf
        bdrv_truncate(bs->file, cluster_offset);
1474 20d97356 Blue Swirl
        return 0;
1475 20d97356 Blue Swirl
    }
1476 20d97356 Blue Swirl
1477 20d97356 Blue Swirl
    if (nb_sectors != s->cluster_sectors)
1478 20d97356 Blue Swirl
        return -EINVAL;
1479 20d97356 Blue Swirl
1480 7267c094 Anthony Liguori
    out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1481 20d97356 Blue Swirl
1482 20d97356 Blue Swirl
    /* best compression, small window, no zlib header */
1483 20d97356 Blue Swirl
    memset(&strm, 0, sizeof(strm));
1484 20d97356 Blue Swirl
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1485 20d97356 Blue Swirl
                       Z_DEFLATED, -12,
1486 20d97356 Blue Swirl
                       9, Z_DEFAULT_STRATEGY);
1487 20d97356 Blue Swirl
    if (ret != 0) {
1488 8f1efd00 Kevin Wolf
        ret = -EINVAL;
1489 8f1efd00 Kevin Wolf
        goto fail;
1490 20d97356 Blue Swirl
    }
1491 20d97356 Blue Swirl
1492 20d97356 Blue Swirl
    strm.avail_in = s->cluster_size;
1493 20d97356 Blue Swirl
    strm.next_in = (uint8_t *)buf;
1494 20d97356 Blue Swirl
    strm.avail_out = s->cluster_size;
1495 20d97356 Blue Swirl
    strm.next_out = out_buf;
1496 20d97356 Blue Swirl
1497 20d97356 Blue Swirl
    ret = deflate(&strm, Z_FINISH);
1498 20d97356 Blue Swirl
    if (ret != Z_STREAM_END && ret != Z_OK) {
1499 20d97356 Blue Swirl
        deflateEnd(&strm);
1500 8f1efd00 Kevin Wolf
        ret = -EINVAL;
1501 8f1efd00 Kevin Wolf
        goto fail;
1502 20d97356 Blue Swirl
    }
1503 20d97356 Blue Swirl
    out_len = strm.next_out - out_buf;
1504 20d97356 Blue Swirl
1505 20d97356 Blue Swirl
    deflateEnd(&strm);
1506 20d97356 Blue Swirl
1507 20d97356 Blue Swirl
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1508 20d97356 Blue Swirl
        /* could not compress: write normal cluster */
1509 8f1efd00 Kevin Wolf
        ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1510 8f1efd00 Kevin Wolf
        if (ret < 0) {
1511 8f1efd00 Kevin Wolf
            goto fail;
1512 8f1efd00 Kevin Wolf
        }
1513 20d97356 Blue Swirl
    } else {
1514 20d97356 Blue Swirl
        cluster_offset = qcow2_alloc_compressed_cluster_offset(bs,
1515 20d97356 Blue Swirl
            sector_num << 9, out_len);
1516 8f1efd00 Kevin Wolf
        if (!cluster_offset) {
1517 8f1efd00 Kevin Wolf
            ret = -EIO;
1518 8f1efd00 Kevin Wolf
            goto fail;
1519 8f1efd00 Kevin Wolf
        }
1520 20d97356 Blue Swirl
        cluster_offset &= s->cluster_offset_mask;
1521 66f82cee Kevin Wolf
        BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
1522 8f1efd00 Kevin Wolf
        ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
1523 8f1efd00 Kevin Wolf
        if (ret < 0) {
1524 8f1efd00 Kevin Wolf
            goto fail;
1525 20d97356 Blue Swirl
        }
1526 20d97356 Blue Swirl
    }
1527 20d97356 Blue Swirl
1528 8f1efd00 Kevin Wolf
    ret = 0;
1529 8f1efd00 Kevin Wolf
fail:
1530 7267c094 Anthony Liguori
    g_free(out_buf);
1531 8f1efd00 Kevin Wolf
    return ret;
1532 20d97356 Blue Swirl
}
1533 20d97356 Blue Swirl
1534 a968168c Dong Xu Wang
static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
1535 20d97356 Blue Swirl
{
1536 29c1a730 Kevin Wolf
    BDRVQcowState *s = bs->opaque;
1537 29c1a730 Kevin Wolf
    int ret;
1538 29c1a730 Kevin Wolf
1539 8b94ff85 Paolo Bonzini
    qemu_co_mutex_lock(&s->lock);
1540 29c1a730 Kevin Wolf
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
1541 29c1a730 Kevin Wolf
    if (ret < 0) {
1542 c95de7e2 Dong Xu Wang
        qemu_co_mutex_unlock(&s->lock);
1543 8b94ff85 Paolo Bonzini
        return ret;
1544 29c1a730 Kevin Wolf
    }
1545 29c1a730 Kevin Wolf
1546 bfe8043e Stefan Hajnoczi
    if (qcow2_need_accurate_refcounts(s)) {
1547 bfe8043e Stefan Hajnoczi
        ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1548 bfe8043e Stefan Hajnoczi
        if (ret < 0) {
1549 bfe8043e Stefan Hajnoczi
            qemu_co_mutex_unlock(&s->lock);
1550 bfe8043e Stefan Hajnoczi
            return ret;
1551 bfe8043e Stefan Hajnoczi
        }
1552 29c1a730 Kevin Wolf
    }
1553 8b94ff85 Paolo Bonzini
    qemu_co_mutex_unlock(&s->lock);
1554 29c1a730 Kevin Wolf
1555 eb489bb1 Kevin Wolf
    return 0;
1556 eb489bb1 Kevin Wolf
}
1557 eb489bb1 Kevin Wolf
1558 7c80ab3f Jes Sorensen
static int64_t qcow2_vm_state_offset(BDRVQcowState *s)
1559 20d97356 Blue Swirl
{
1560 20d97356 Blue Swirl
        return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
1561 20d97356 Blue Swirl
}
1562 20d97356 Blue Swirl
1563 7c80ab3f Jes Sorensen
static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1564 20d97356 Blue Swirl
{
1565 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1566 20d97356 Blue Swirl
    bdi->cluster_size = s->cluster_size;
1567 7c80ab3f Jes Sorensen
    bdi->vm_state_offset = qcow2_vm_state_offset(s);
1568 20d97356 Blue Swirl
    return 0;
1569 20d97356 Blue Swirl
}
1570 20d97356 Blue Swirl
1571 20d97356 Blue Swirl
1572 4534ff54 Kevin Wolf
static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
1573 4534ff54 Kevin Wolf
                       BdrvCheckMode fix)
1574 20d97356 Blue Swirl
{
1575 166acf54 Kevin Wolf
    return qcow2_check_refcounts(bs, result, fix);
1576 20d97356 Blue Swirl
}
1577 20d97356 Blue Swirl
1578 20d97356 Blue Swirl
#if 0
1579 20d97356 Blue Swirl
static void dump_refcounts(BlockDriverState *bs)
1580 20d97356 Blue Swirl
{
1581 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1582 20d97356 Blue Swirl
    int64_t nb_clusters, k, k1, size;
1583 20d97356 Blue Swirl
    int refcount;
1584 20d97356 Blue Swirl

1585 66f82cee Kevin Wolf
    size = bdrv_getlength(bs->file);
1586 20d97356 Blue Swirl
    nb_clusters = size_to_clusters(s, size);
1587 20d97356 Blue Swirl
    for(k = 0; k < nb_clusters;) {
1588 20d97356 Blue Swirl
        k1 = k;
1589 20d97356 Blue Swirl
        refcount = get_refcount(bs, k);
1590 20d97356 Blue Swirl
        k++;
1591 20d97356 Blue Swirl
        while (k < nb_clusters && get_refcount(bs, k) == refcount)
1592 20d97356 Blue Swirl
            k++;
1593 0bfcd599 Blue Swirl
        printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
1594 0bfcd599 Blue Swirl
               k - k1);
1595 20d97356 Blue Swirl
    }
1596 20d97356 Blue Swirl
}
1597 20d97356 Blue Swirl
#endif
1598 20d97356 Blue Swirl
1599 7c80ab3f Jes Sorensen
static int qcow2_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1600 7c80ab3f Jes Sorensen
                              int64_t pos, int size)
1601 20d97356 Blue Swirl
{
1602 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1603 20d97356 Blue Swirl
    int growable = bs->growable;
1604 20d97356 Blue Swirl
    int ret;
1605 20d97356 Blue Swirl
1606 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
1607 20d97356 Blue Swirl
    bs->growable = 1;
1608 7c80ab3f Jes Sorensen
    ret = bdrv_pwrite(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1609 20d97356 Blue Swirl
    bs->growable = growable;
1610 20d97356 Blue Swirl
1611 20d97356 Blue Swirl
    return ret;
1612 20d97356 Blue Swirl
}
1613 20d97356 Blue Swirl
1614 7c80ab3f Jes Sorensen
static int qcow2_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1615 7c80ab3f Jes Sorensen
                              int64_t pos, int size)
1616 20d97356 Blue Swirl
{
1617 20d97356 Blue Swirl
    BDRVQcowState *s = bs->opaque;
1618 20d97356 Blue Swirl
    int growable = bs->growable;
1619 20d97356 Blue Swirl
    int ret;
1620 20d97356 Blue Swirl
1621 66f82cee Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
1622 20d97356 Blue Swirl
    bs->growable = 1;
1623 7c80ab3f Jes Sorensen
    ret = bdrv_pread(bs, qcow2_vm_state_offset(s) + pos, buf, size);
1624 20d97356 Blue Swirl
    bs->growable = growable;
1625 20d97356 Blue Swirl
1626 20d97356 Blue Swirl
    return ret;
1627 20d97356 Blue Swirl
}
1628 20d97356 Blue Swirl
1629 7c80ab3f Jes Sorensen
static QEMUOptionParameter qcow2_create_options[] = {
1630 20d97356 Blue Swirl
    {
1631 20d97356 Blue Swirl
        .name = BLOCK_OPT_SIZE,
1632 20d97356 Blue Swirl
        .type = OPT_SIZE,
1633 20d97356 Blue Swirl
        .help = "Virtual disk size"
1634 20d97356 Blue Swirl
    },
1635 20d97356 Blue Swirl
    {
1636 6744cbab Kevin Wolf
        .name = BLOCK_OPT_COMPAT_LEVEL,
1637 6744cbab Kevin Wolf
        .type = OPT_STRING,
1638 6744cbab Kevin Wolf
        .help = "Compatibility level (0.10 or 1.1)"
1639 6744cbab Kevin Wolf
    },
1640 6744cbab Kevin Wolf
    {
1641 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FILE,
1642 20d97356 Blue Swirl
        .type = OPT_STRING,
1643 20d97356 Blue Swirl
        .help = "File name of a base image"
1644 20d97356 Blue Swirl
    },
1645 20d97356 Blue Swirl
    {
1646 20d97356 Blue Swirl
        .name = BLOCK_OPT_BACKING_FMT,
1647 20d97356 Blue Swirl
        .type = OPT_STRING,
1648 20d97356 Blue Swirl
        .help = "Image format of the base image"
1649 20d97356 Blue Swirl
    },
1650 20d97356 Blue Swirl
    {
1651 20d97356 Blue Swirl
        .name = BLOCK_OPT_ENCRYPT,
1652 20d97356 Blue Swirl
        .type = OPT_FLAG,
1653 20d97356 Blue Swirl
        .help = "Encrypt the image"
1654 20d97356 Blue Swirl
    },
1655 20d97356 Blue Swirl
    {
1656 20d97356 Blue Swirl
        .name = BLOCK_OPT_CLUSTER_SIZE,
1657 20d97356 Blue Swirl
        .type = OPT_SIZE,
1658 99cce9fa Kevin Wolf
        .help = "qcow2 cluster size",
1659 99cce9fa Kevin Wolf
        .value = { .n = DEFAULT_CLUSTER_SIZE },
1660 20d97356 Blue Swirl
    },
1661 20d97356 Blue Swirl
    {
1662 20d97356 Blue Swirl
        .name = BLOCK_OPT_PREALLOC,
1663 20d97356 Blue Swirl
        .type = OPT_STRING,
1664 20d97356 Blue Swirl
        .help = "Preallocation mode (allowed values: off, metadata)"
1665 20d97356 Blue Swirl
    },
1666 bfe8043e Stefan Hajnoczi
    {
1667 bfe8043e Stefan Hajnoczi
        .name = BLOCK_OPT_LAZY_REFCOUNTS,
1668 bfe8043e Stefan Hajnoczi
        .type = OPT_FLAG,
1669 bfe8043e Stefan Hajnoczi
        .help = "Postpone refcount updates",
1670 bfe8043e Stefan Hajnoczi
    },
1671 20d97356 Blue Swirl
    { NULL }
1672 20d97356 Blue Swirl
};
1673 20d97356 Blue Swirl
1674 20d97356 Blue Swirl
static BlockDriver bdrv_qcow2 = {
1675 7c80ab3f Jes Sorensen
    .format_name        = "qcow2",
1676 7c80ab3f Jes Sorensen
    .instance_size      = sizeof(BDRVQcowState),
1677 7c80ab3f Jes Sorensen
    .bdrv_probe         = qcow2_probe,
1678 7c80ab3f Jes Sorensen
    .bdrv_open          = qcow2_open,
1679 7c80ab3f Jes Sorensen
    .bdrv_close         = qcow2_close,
1680 7c80ab3f Jes Sorensen
    .bdrv_create        = qcow2_create,
1681 f8a2e5e3 Stefan Hajnoczi
    .bdrv_co_is_allocated = qcow2_co_is_allocated,
1682 7c80ab3f Jes Sorensen
    .bdrv_set_key       = qcow2_set_key,
1683 7c80ab3f Jes Sorensen
    .bdrv_make_empty    = qcow2_make_empty,
1684 7c80ab3f Jes Sorensen
1685 c68b89ac Kevin Wolf
    .bdrv_co_readv          = qcow2_co_readv,
1686 c68b89ac Kevin Wolf
    .bdrv_co_writev         = qcow2_co_writev,
1687 eb489bb1 Kevin Wolf
    .bdrv_co_flush_to_os    = qcow2_co_flush_to_os,
1688 419b19d9 Stefan Hajnoczi
1689 621f0589 Kevin Wolf
    .bdrv_co_write_zeroes   = qcow2_co_write_zeroes,
1690 6db39ae2 Paolo Bonzini
    .bdrv_co_discard        = qcow2_co_discard,
1691 419b19d9 Stefan Hajnoczi
    .bdrv_truncate          = qcow2_truncate,
1692 7c80ab3f Jes Sorensen
    .bdrv_write_compressed  = qcow2_write_compressed,
1693 20d97356 Blue Swirl
1694 20d97356 Blue Swirl
    .bdrv_snapshot_create   = qcow2_snapshot_create,
1695 20d97356 Blue Swirl
    .bdrv_snapshot_goto     = qcow2_snapshot_goto,
1696 20d97356 Blue Swirl
    .bdrv_snapshot_delete   = qcow2_snapshot_delete,
1697 20d97356 Blue Swirl
    .bdrv_snapshot_list     = qcow2_snapshot_list,
1698 51ef6727 edison
    .bdrv_snapshot_load_tmp     = qcow2_snapshot_load_tmp,
1699 7c80ab3f Jes Sorensen
    .bdrv_get_info      = qcow2_get_info,
1700 20d97356 Blue Swirl
1701 7c80ab3f Jes Sorensen
    .bdrv_save_vmstate    = qcow2_save_vmstate,
1702 7c80ab3f Jes Sorensen
    .bdrv_load_vmstate    = qcow2_load_vmstate,
1703 20d97356 Blue Swirl
1704 20d97356 Blue Swirl
    .bdrv_change_backing_file   = qcow2_change_backing_file,
1705 20d97356 Blue Swirl
1706 06d9260f Anthony Liguori
    .bdrv_invalidate_cache      = qcow2_invalidate_cache,
1707 06d9260f Anthony Liguori
1708 7c80ab3f Jes Sorensen
    .create_options = qcow2_create_options,
1709 7c80ab3f Jes Sorensen
    .bdrv_check = qcow2_check,
1710 20d97356 Blue Swirl
};
1711 20d97356 Blue Swirl
1712 5efa9d5a Anthony Liguori
static void bdrv_qcow2_init(void)
1713 5efa9d5a Anthony Liguori
{
1714 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_qcow2);
1715 5efa9d5a Anthony Liguori
}
1716 5efa9d5a Anthony Liguori
1717 5efa9d5a Anthony Liguori
block_init(bdrv_qcow2_init);