xref: /qemu/block/qcow.c (revision c01c214b)
1019d6b8fSAnthony Liguori /*
2019d6b8fSAnthony Liguori  * Block driver for the QCOW format
3019d6b8fSAnthony Liguori  *
4019d6b8fSAnthony Liguori  * Copyright (c) 2004-2006 Fabrice Bellard
5019d6b8fSAnthony Liguori  *
6019d6b8fSAnthony Liguori  * Permission is hereby granted, free of charge, to any person obtaining a copy
7019d6b8fSAnthony Liguori  * of this software and associated documentation files (the "Software"), to deal
8019d6b8fSAnthony Liguori  * in the Software without restriction, including without limitation the rights
9019d6b8fSAnthony Liguori  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10019d6b8fSAnthony Liguori  * copies of the Software, and to permit persons to whom the Software is
11019d6b8fSAnthony Liguori  * furnished to do so, subject to the following conditions:
12019d6b8fSAnthony Liguori  *
13019d6b8fSAnthony Liguori  * The above copyright notice and this permission notice shall be included in
14019d6b8fSAnthony Liguori  * all copies or substantial portions of the Software.
15019d6b8fSAnthony Liguori  *
16019d6b8fSAnthony Liguori  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17019d6b8fSAnthony Liguori  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18019d6b8fSAnthony Liguori  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19019d6b8fSAnthony Liguori  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20019d6b8fSAnthony Liguori  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21019d6b8fSAnthony Liguori  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22019d6b8fSAnthony Liguori  * THE SOFTWARE.
23019d6b8fSAnthony Liguori  */
2480c71a24SPeter Maydell #include "qemu/osdep.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
26019d6b8fSAnthony Liguori #include "qemu-common.h"
27e6ff69bfSDaniel P. Berrange #include "qemu/error-report.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
296af40160SKevin Wolf #include "sysemu/block-backend.h"
301de7afc9SPaolo Bonzini #include "qemu/module.h"
3158369e22SPaolo Bonzini #include "qemu/bswap.h"
32019d6b8fSAnthony Liguori #include <zlib.h>
33cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
34d85f4222SDaniel P. Berrange #include "qapi/qmp/qstring.h"
35d85f4222SDaniel P. Berrange #include "crypto/block.h"
36795c40b8SJuan Quintela #include "migration/blocker.h"
37d85f4222SDaniel P. Berrange #include "block/crypto.h"
38019d6b8fSAnthony Liguori 
39019d6b8fSAnthony Liguori /**************************************************************/
40019d6b8fSAnthony Liguori /* QEMU COW block driver with compression and encryption support */
41019d6b8fSAnthony Liguori 
42019d6b8fSAnthony Liguori #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
43019d6b8fSAnthony Liguori #define QCOW_VERSION 1
44019d6b8fSAnthony Liguori 
45019d6b8fSAnthony Liguori #define QCOW_CRYPT_NONE 0
46019d6b8fSAnthony Liguori #define QCOW_CRYPT_AES  1
47019d6b8fSAnthony Liguori 
48019d6b8fSAnthony Liguori #define QCOW_OFLAG_COMPRESSED (1LL << 63)
49019d6b8fSAnthony Liguori 
50019d6b8fSAnthony Liguori typedef struct QCowHeader {
51019d6b8fSAnthony Liguori     uint32_t magic;
52019d6b8fSAnthony Liguori     uint32_t version;
53019d6b8fSAnthony Liguori     uint64_t backing_file_offset;
54019d6b8fSAnthony Liguori     uint32_t backing_file_size;
55019d6b8fSAnthony Liguori     uint32_t mtime;
56019d6b8fSAnthony Liguori     uint64_t size; /* in bytes */
57019d6b8fSAnthony Liguori     uint8_t cluster_bits;
58019d6b8fSAnthony Liguori     uint8_t l2_bits;
59ea54feffSKevin Wolf     uint16_t padding;
60019d6b8fSAnthony Liguori     uint32_t crypt_method;
61019d6b8fSAnthony Liguori     uint64_t l1_table_offset;
62ea54feffSKevin Wolf } QEMU_PACKED QCowHeader;
63019d6b8fSAnthony Liguori 
64019d6b8fSAnthony Liguori #define L2_CACHE_SIZE 16
65019d6b8fSAnthony Liguori 
66019d6b8fSAnthony Liguori typedef struct BDRVQcowState {
67019d6b8fSAnthony Liguori     int cluster_bits;
68019d6b8fSAnthony Liguori     int cluster_size;
69019d6b8fSAnthony Liguori     int cluster_sectors;
70019d6b8fSAnthony Liguori     int l2_bits;
71019d6b8fSAnthony Liguori     int l2_size;
7246485de0SKevin Wolf     unsigned int l1_size;
73019d6b8fSAnthony Liguori     uint64_t cluster_offset_mask;
74019d6b8fSAnthony Liguori     uint64_t l1_table_offset;
75019d6b8fSAnthony Liguori     uint64_t *l1_table;
76019d6b8fSAnthony Liguori     uint64_t *l2_cache;
77019d6b8fSAnthony Liguori     uint64_t l2_cache_offsets[L2_CACHE_SIZE];
78019d6b8fSAnthony Liguori     uint32_t l2_cache_counts[L2_CACHE_SIZE];
79019d6b8fSAnthony Liguori     uint8_t *cluster_cache;
80019d6b8fSAnthony Liguori     uint8_t *cluster_data;
81019d6b8fSAnthony Liguori     uint64_t cluster_cache_offset;
82d85f4222SDaniel P. Berrange     QCryptoBlock *crypto; /* Disk encryption format driver */
83019d6b8fSAnthony Liguori     uint32_t crypt_method_header;
8452b8eb60SKevin Wolf     CoMutex lock;
85fd9f102cSKevin Wolf     Error *migration_blocker;
86019d6b8fSAnthony Liguori } BDRVQcowState;
87019d6b8fSAnthony Liguori 
8866f82ceeSKevin Wolf static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
89019d6b8fSAnthony Liguori 
90019d6b8fSAnthony Liguori static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
91019d6b8fSAnthony Liguori {
92019d6b8fSAnthony Liguori     const QCowHeader *cow_header = (const void *)buf;
93019d6b8fSAnthony Liguori 
94019d6b8fSAnthony Liguori     if (buf_size >= sizeof(QCowHeader) &&
95019d6b8fSAnthony Liguori         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
96019d6b8fSAnthony Liguori         be32_to_cpu(cow_header->version) == QCOW_VERSION)
97019d6b8fSAnthony Liguori         return 100;
98019d6b8fSAnthony Liguori     else
99019d6b8fSAnthony Liguori         return 0;
100019d6b8fSAnthony Liguori }
101019d6b8fSAnthony Liguori 
102d85f4222SDaniel P. Berrange static QemuOptsList qcow_runtime_opts = {
103d85f4222SDaniel P. Berrange     .name = "qcow",
104d85f4222SDaniel P. Berrange     .head = QTAILQ_HEAD_INITIALIZER(qcow_runtime_opts.head),
105d85f4222SDaniel P. Berrange     .desc = {
106d85f4222SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."),
107d85f4222SDaniel P. Berrange         { /* end of list */ }
108d85f4222SDaniel P. Berrange     },
109d85f4222SDaniel P. Berrange };
110d85f4222SDaniel P. Berrange 
111015a1036SMax Reitz static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
112015a1036SMax Reitz                      Error **errp)
113019d6b8fSAnthony Liguori {
114019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
115d66e5ceeSKevin Wolf     unsigned int len, i, shift;
116d66e5ceeSKevin Wolf     int ret;
117019d6b8fSAnthony Liguori     QCowHeader header;
118fe44dc91SAshijeet Acharya     Error *local_err = NULL;
119d85f4222SDaniel P. Berrange     QCryptoBlockOpenOptions *crypto_opts = NULL;
120d85f4222SDaniel P. Berrange     unsigned int cflags = 0;
121d85f4222SDaniel P. Berrange     QDict *encryptopts = NULL;
122d85f4222SDaniel P. Berrange     const char *encryptfmt;
123d85f4222SDaniel P. Berrange 
124d85f4222SDaniel P. Berrange     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
125d85f4222SDaniel P. Berrange     encryptfmt = qdict_get_try_str(encryptopts, "format");
126019d6b8fSAnthony Liguori 
1274e4bf5c4SKevin Wolf     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
1284e4bf5c4SKevin Wolf                                false, errp);
1294e4bf5c4SKevin Wolf     if (!bs->file) {
130d85f4222SDaniel P. Berrange         ret = -EINVAL;
131d85f4222SDaniel P. Berrange         goto fail;
1324e4bf5c4SKevin Wolf     }
1334e4bf5c4SKevin Wolf 
134cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
13584b0ec02SLi Zhi Hui     if (ret < 0) {
136019d6b8fSAnthony Liguori         goto fail;
13784b0ec02SLi Zhi Hui     }
138019d6b8fSAnthony Liguori     be32_to_cpus(&header.magic);
139019d6b8fSAnthony Liguori     be32_to_cpus(&header.version);
140019d6b8fSAnthony Liguori     be64_to_cpus(&header.backing_file_offset);
141019d6b8fSAnthony Liguori     be32_to_cpus(&header.backing_file_size);
142019d6b8fSAnthony Liguori     be32_to_cpus(&header.mtime);
143019d6b8fSAnthony Liguori     be64_to_cpus(&header.size);
144019d6b8fSAnthony Liguori     be32_to_cpus(&header.crypt_method);
145019d6b8fSAnthony Liguori     be64_to_cpus(&header.l1_table_offset);
146019d6b8fSAnthony Liguori 
14784b0ec02SLi Zhi Hui     if (header.magic != QCOW_MAGIC) {
14876abe407SPaolo Bonzini         error_setg(errp, "Image not in qcow format");
14976abe407SPaolo Bonzini         ret = -EINVAL;
150019d6b8fSAnthony Liguori         goto fail;
15184b0ec02SLi Zhi Hui     }
15284b0ec02SLi Zhi Hui     if (header.version != QCOW_VERSION) {
153a55448b3SMax Reitz         error_setg(errp, "Unsupported qcow version %" PRIu32, header.version);
15484b0ec02SLi Zhi Hui         ret = -ENOTSUP;
155019d6b8fSAnthony Liguori         goto fail;
15684b0ec02SLi Zhi Hui     }
15784b0ec02SLi Zhi Hui 
1587159a45bSKevin Wolf     if (header.size <= 1) {
1597159a45bSKevin Wolf         error_setg(errp, "Image size is too small (must be at least 2 bytes)");
16084b0ec02SLi Zhi Hui         ret = -EINVAL;
161019d6b8fSAnthony Liguori         goto fail;
16284b0ec02SLi Zhi Hui     }
1637159a45bSKevin Wolf     if (header.cluster_bits < 9 || header.cluster_bits > 16) {
1647159a45bSKevin Wolf         error_setg(errp, "Cluster size must be between 512 and 64k");
1657159a45bSKevin Wolf         ret = -EINVAL;
1667159a45bSKevin Wolf         goto fail;
1677159a45bSKevin Wolf     }
1687159a45bSKevin Wolf 
16942eb5817SKevin Wolf     /* l2_bits specifies number of entries; storing a uint64_t in each entry,
17042eb5817SKevin Wolf      * so bytes = num_entries << 3. */
17142eb5817SKevin Wolf     if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
17242eb5817SKevin Wolf         error_setg(errp, "L2 table size must be between 512 and 64k");
17342eb5817SKevin Wolf         ret = -EINVAL;
17442eb5817SKevin Wolf         goto fail;
17542eb5817SKevin Wolf     }
17642eb5817SKevin Wolf 
177019d6b8fSAnthony Liguori     s->crypt_method_header = header.crypt_method;
17884b0ec02SLi Zhi Hui     if (s->crypt_method_header) {
179e6ff69bfSDaniel P. Berrange         if (bdrv_uses_whitelist() &&
180e6ff69bfSDaniel P. Berrange             s->crypt_method_header == QCOW_CRYPT_AES) {
1818c0dcbc4SDaniel P. Berrange             error_setg(errp,
1828c0dcbc4SDaniel P. Berrange                        "Use of AES-CBC encrypted qcow images is no longer "
1838c0dcbc4SDaniel P. Berrange                        "supported in system emulators");
1848c0dcbc4SDaniel P. Berrange             error_append_hint(errp,
1858c0dcbc4SDaniel P. Berrange                               "You can use 'qemu-img convert' to convert your "
1868c0dcbc4SDaniel P. Berrange                               "image to an alternative supported format, such "
1878c0dcbc4SDaniel P. Berrange                               "as unencrypted qcow, or raw with the LUKS "
1888c0dcbc4SDaniel P. Berrange                               "format instead.\n");
1898c0dcbc4SDaniel P. Berrange             ret = -ENOSYS;
1908c0dcbc4SDaniel P. Berrange             goto fail;
191e6ff69bfSDaniel P. Berrange         }
192d85f4222SDaniel P. Berrange         if (s->crypt_method_header == QCOW_CRYPT_AES) {
193d85f4222SDaniel P. Berrange             if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
194d85f4222SDaniel P. Berrange                 error_setg(errp,
195d85f4222SDaniel P. Berrange                            "Header reported 'aes' encryption format but "
196d85f4222SDaniel P. Berrange                            "options specify '%s'", encryptfmt);
197d85f4222SDaniel P. Berrange                 ret = -EINVAL;
198d85f4222SDaniel P. Berrange                 goto fail;
199d85f4222SDaniel P. Berrange             }
200d85f4222SDaniel P. Berrange             qdict_del(encryptopts, "format");
201d85f4222SDaniel P. Berrange             crypto_opts = block_crypto_open_opts_init(
202d85f4222SDaniel P. Berrange                 Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
203d85f4222SDaniel P. Berrange             if (!crypto_opts) {
204d85f4222SDaniel P. Berrange                 ret = -EINVAL;
205d85f4222SDaniel P. Berrange                 goto fail;
206d85f4222SDaniel P. Berrange             }
207e6ff69bfSDaniel P. Berrange 
208d85f4222SDaniel P. Berrange             if (flags & BDRV_O_NO_IO) {
209d85f4222SDaniel P. Berrange                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
210d85f4222SDaniel P. Berrange             }
211d85f4222SDaniel P. Berrange             s->crypto = qcrypto_block_open(crypto_opts, NULL, NULL,
212d85f4222SDaniel P. Berrange                                            cflags, errp);
213d85f4222SDaniel P. Berrange             if (!s->crypto) {
214d85f4222SDaniel P. Berrange                 ret = -EINVAL;
215d85f4222SDaniel P. Berrange                 goto fail;
216d85f4222SDaniel P. Berrange             }
217d85f4222SDaniel P. Berrange         } else {
218d85f4222SDaniel P. Berrange             error_setg(errp, "invalid encryption method in qcow header");
219d85f4222SDaniel P. Berrange             ret = -EINVAL;
220d85f4222SDaniel P. Berrange             goto fail;
221d85f4222SDaniel P. Berrange         }
22254115412SEric Blake         bs->encrypted = true;
223*c01c214bSDaniel P. Berrange     } else {
224*c01c214bSDaniel P. Berrange         if (encryptfmt) {
225*c01c214bSDaniel P. Berrange             error_setg(errp, "No encryption in image header, but options "
226*c01c214bSDaniel P. Berrange                        "specified format '%s'", encryptfmt);
227*c01c214bSDaniel P. Berrange             ret = -EINVAL;
228*c01c214bSDaniel P. Berrange             goto fail;
229*c01c214bSDaniel P. Berrange         }
23084b0ec02SLi Zhi Hui     }
231019d6b8fSAnthony Liguori     s->cluster_bits = header.cluster_bits;
232019d6b8fSAnthony Liguori     s->cluster_size = 1 << s->cluster_bits;
233019d6b8fSAnthony Liguori     s->cluster_sectors = 1 << (s->cluster_bits - 9);
234019d6b8fSAnthony Liguori     s->l2_bits = header.l2_bits;
235019d6b8fSAnthony Liguori     s->l2_size = 1 << s->l2_bits;
236019d6b8fSAnthony Liguori     bs->total_sectors = header.size / 512;
237019d6b8fSAnthony Liguori     s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
238019d6b8fSAnthony Liguori 
239019d6b8fSAnthony Liguori     /* read the level 1 table */
240019d6b8fSAnthony Liguori     shift = s->cluster_bits + s->l2_bits;
24146485de0SKevin Wolf     if (header.size > UINT64_MAX - (1LL << shift)) {
24246485de0SKevin Wolf         error_setg(errp, "Image too large");
24346485de0SKevin Wolf         ret = -EINVAL;
24446485de0SKevin Wolf         goto fail;
24546485de0SKevin Wolf     } else {
24646485de0SKevin Wolf         uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
24746485de0SKevin Wolf         if (l1_size > INT_MAX / sizeof(uint64_t)) {
24846485de0SKevin Wolf             error_setg(errp, "Image too large");
24946485de0SKevin Wolf             ret = -EINVAL;
25046485de0SKevin Wolf             goto fail;
25146485de0SKevin Wolf         }
25246485de0SKevin Wolf         s->l1_size = l1_size;
25346485de0SKevin Wolf     }
254019d6b8fSAnthony Liguori 
255019d6b8fSAnthony Liguori     s->l1_table_offset = header.l1_table_offset;
2565839e53bSMarkus Armbruster     s->l1_table = g_try_new(uint64_t, s->l1_size);
2570df93305SKevin Wolf     if (s->l1_table == NULL) {
2580df93305SKevin Wolf         error_setg(errp, "Could not allocate memory for L1 table");
2590df93305SKevin Wolf         ret = -ENOMEM;
2600df93305SKevin Wolf         goto fail;
2610df93305SKevin Wolf     }
26284b0ec02SLi Zhi Hui 
263cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
26484b0ec02SLi Zhi Hui                s->l1_size * sizeof(uint64_t));
26584b0ec02SLi Zhi Hui     if (ret < 0) {
266019d6b8fSAnthony Liguori         goto fail;
26784b0ec02SLi Zhi Hui     }
26884b0ec02SLi Zhi Hui 
269019d6b8fSAnthony Liguori     for(i = 0;i < s->l1_size; i++) {
270019d6b8fSAnthony Liguori         be64_to_cpus(&s->l1_table[i]);
271019d6b8fSAnthony Liguori     }
2720df93305SKevin Wolf 
2730df93305SKevin Wolf     /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
2740df93305SKevin Wolf     s->l2_cache =
2759a4f4c31SKevin Wolf         qemu_try_blockalign(bs->file->bs,
2760df93305SKevin Wolf                             s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
2770df93305SKevin Wolf     if (s->l2_cache == NULL) {
2780df93305SKevin Wolf         error_setg(errp, "Could not allocate L2 table cache");
2790df93305SKevin Wolf         ret = -ENOMEM;
2800df93305SKevin Wolf         goto fail;
2810df93305SKevin Wolf     }
2827267c094SAnthony Liguori     s->cluster_cache = g_malloc(s->cluster_size);
2837267c094SAnthony Liguori     s->cluster_data = g_malloc(s->cluster_size);
284019d6b8fSAnthony Liguori     s->cluster_cache_offset = -1;
285019d6b8fSAnthony Liguori 
286019d6b8fSAnthony Liguori     /* read the backing file name */
287019d6b8fSAnthony Liguori     if (header.backing_file_offset != 0) {
288019d6b8fSAnthony Liguori         len = header.backing_file_size;
289e729fa6aSJeff Cody         if (len > 1023 || len >= sizeof(bs->backing_file)) {
290d66e5ceeSKevin Wolf             error_setg(errp, "Backing file name too long");
291d66e5ceeSKevin Wolf             ret = -EINVAL;
292d66e5ceeSKevin Wolf             goto fail;
29384b0ec02SLi Zhi Hui         }
294cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, header.backing_file_offset,
29584b0ec02SLi Zhi Hui                    bs->backing_file, len);
29684b0ec02SLi Zhi Hui         if (ret < 0) {
297019d6b8fSAnthony Liguori             goto fail;
29884b0ec02SLi Zhi Hui         }
299019d6b8fSAnthony Liguori         bs->backing_file[len] = '\0';
300019d6b8fSAnthony Liguori     }
301de33b1f3SScott Wood 
302fd9f102cSKevin Wolf     /* Disable migration when qcow images are used */
30381e5f78aSAlberto Garcia     error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
30481e5f78aSAlberto Garcia                "does not support live migration",
30581e5f78aSAlberto Garcia                bdrv_get_device_or_node_name(bs));
306fe44dc91SAshijeet Acharya     ret = migrate_add_blocker(s->migration_blocker, &local_err);
307fe44dc91SAshijeet Acharya     if (local_err) {
308fe44dc91SAshijeet Acharya         error_propagate(errp, local_err);
309fe44dc91SAshijeet Acharya         error_free(s->migration_blocker);
310fe44dc91SAshijeet Acharya         goto fail;
311fe44dc91SAshijeet Acharya     }
312fd9f102cSKevin Wolf 
313d85f4222SDaniel P. Berrange     QDECREF(encryptopts);
314d85f4222SDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(crypto_opts);
315de33b1f3SScott Wood     qemu_co_mutex_init(&s->lock);
316019d6b8fSAnthony Liguori     return 0;
317019d6b8fSAnthony Liguori 
318019d6b8fSAnthony Liguori  fail:
3197267c094SAnthony Liguori     g_free(s->l1_table);
3200df93305SKevin Wolf     qemu_vfree(s->l2_cache);
3217267c094SAnthony Liguori     g_free(s->cluster_cache);
3227267c094SAnthony Liguori     g_free(s->cluster_data);
323d85f4222SDaniel P. Berrange     qcrypto_block_free(s->crypto);
324d85f4222SDaniel P. Berrange     QDECREF(encryptopts);
325d85f4222SDaniel P. Berrange     qapi_free_QCryptoBlockOpenOptions(crypto_opts);
32684b0ec02SLi Zhi Hui     return ret;
327019d6b8fSAnthony Liguori }
328019d6b8fSAnthony Liguori 
329d177692eSJeff Cody 
330d177692eSJeff Cody /* We have nothing to do for QCOW reopen, stubs just return
331d177692eSJeff Cody  * success */
332d177692eSJeff Cody static int qcow_reopen_prepare(BDRVReopenState *state,
333d177692eSJeff Cody                                BlockReopenQueue *queue, Error **errp)
334d177692eSJeff Cody {
335d177692eSJeff Cody     return 0;
336d177692eSJeff Cody }
337d177692eSJeff Cody 
338019d6b8fSAnthony Liguori 
339019d6b8fSAnthony Liguori /* 'allocate' is:
340019d6b8fSAnthony Liguori  *
341019d6b8fSAnthony Liguori  * 0 to not allocate.
342019d6b8fSAnthony Liguori  *
343019d6b8fSAnthony Liguori  * 1 to allocate a normal cluster (for sector indexes 'n_start' to
344019d6b8fSAnthony Liguori  * 'n_end')
345019d6b8fSAnthony Liguori  *
346019d6b8fSAnthony Liguori  * 2 to allocate a compressed cluster of size
347019d6b8fSAnthony Liguori  * 'compressed_size'. 'compressed_size' must be > 0 and <
348019d6b8fSAnthony Liguori  * cluster_size
349019d6b8fSAnthony Liguori  *
350019d6b8fSAnthony Liguori  * return 0 if not allocated.
351019d6b8fSAnthony Liguori  */
352019d6b8fSAnthony Liguori static uint64_t get_cluster_offset(BlockDriverState *bs,
353019d6b8fSAnthony Liguori                                    uint64_t offset, int allocate,
354019d6b8fSAnthony Liguori                                    int compressed_size,
355019d6b8fSAnthony Liguori                                    int n_start, int n_end)
356019d6b8fSAnthony Liguori {
357019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
358019d6b8fSAnthony Liguori     int min_index, i, j, l1_index, l2_index;
359019d6b8fSAnthony Liguori     uint64_t l2_offset, *l2_table, cluster_offset, tmp;
360019d6b8fSAnthony Liguori     uint32_t min_count;
361019d6b8fSAnthony Liguori     int new_l2_table;
362019d6b8fSAnthony Liguori 
363019d6b8fSAnthony Liguori     l1_index = offset >> (s->l2_bits + s->cluster_bits);
364019d6b8fSAnthony Liguori     l2_offset = s->l1_table[l1_index];
365019d6b8fSAnthony Liguori     new_l2_table = 0;
366019d6b8fSAnthony Liguori     if (!l2_offset) {
367019d6b8fSAnthony Liguori         if (!allocate)
368019d6b8fSAnthony Liguori             return 0;
369019d6b8fSAnthony Liguori         /* allocate a new l2 entry */
3709a4f4c31SKevin Wolf         l2_offset = bdrv_getlength(bs->file->bs);
371019d6b8fSAnthony Liguori         /* round to cluster size */
372019d6b8fSAnthony Liguori         l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
373019d6b8fSAnthony Liguori         /* update the L1 entry */
374019d6b8fSAnthony Liguori         s->l1_table[l1_index] = l2_offset;
375019d6b8fSAnthony Liguori         tmp = cpu_to_be64(l2_offset);
376d9ca2ea2SKevin Wolf         if (bdrv_pwrite_sync(bs->file,
3775e5557d9SKevin Wolf                 s->l1_table_offset + l1_index * sizeof(tmp),
3785e5557d9SKevin Wolf                 &tmp, sizeof(tmp)) < 0)
379019d6b8fSAnthony Liguori             return 0;
380019d6b8fSAnthony Liguori         new_l2_table = 1;
381019d6b8fSAnthony Liguori     }
382019d6b8fSAnthony Liguori     for(i = 0; i < L2_CACHE_SIZE; i++) {
383019d6b8fSAnthony Liguori         if (l2_offset == s->l2_cache_offsets[i]) {
384019d6b8fSAnthony Liguori             /* increment the hit count */
385019d6b8fSAnthony Liguori             if (++s->l2_cache_counts[i] == 0xffffffff) {
386019d6b8fSAnthony Liguori                 for(j = 0; j < L2_CACHE_SIZE; j++) {
387019d6b8fSAnthony Liguori                     s->l2_cache_counts[j] >>= 1;
388019d6b8fSAnthony Liguori                 }
389019d6b8fSAnthony Liguori             }
390019d6b8fSAnthony Liguori             l2_table = s->l2_cache + (i << s->l2_bits);
391019d6b8fSAnthony Liguori             goto found;
392019d6b8fSAnthony Liguori         }
393019d6b8fSAnthony Liguori     }
394019d6b8fSAnthony Liguori     /* not found: load a new entry in the least used one */
395019d6b8fSAnthony Liguori     min_index = 0;
396019d6b8fSAnthony Liguori     min_count = 0xffffffff;
397019d6b8fSAnthony Liguori     for(i = 0; i < L2_CACHE_SIZE; i++) {
398019d6b8fSAnthony Liguori         if (s->l2_cache_counts[i] < min_count) {
399019d6b8fSAnthony Liguori             min_count = s->l2_cache_counts[i];
400019d6b8fSAnthony Liguori             min_index = i;
401019d6b8fSAnthony Liguori         }
402019d6b8fSAnthony Liguori     }
403019d6b8fSAnthony Liguori     l2_table = s->l2_cache + (min_index << s->l2_bits);
404019d6b8fSAnthony Liguori     if (new_l2_table) {
405019d6b8fSAnthony Liguori         memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
406d9ca2ea2SKevin Wolf         if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
4075e5557d9SKevin Wolf                 s->l2_size * sizeof(uint64_t)) < 0)
408019d6b8fSAnthony Liguori             return 0;
409019d6b8fSAnthony Liguori     } else {
410cf2ab8fcSKevin Wolf         if (bdrv_pread(bs->file, l2_offset, l2_table,
4119a4f4c31SKevin Wolf                        s->l2_size * sizeof(uint64_t)) !=
412019d6b8fSAnthony Liguori             s->l2_size * sizeof(uint64_t))
413019d6b8fSAnthony Liguori             return 0;
414019d6b8fSAnthony Liguori     }
415019d6b8fSAnthony Liguori     s->l2_cache_offsets[min_index] = l2_offset;
416019d6b8fSAnthony Liguori     s->l2_cache_counts[min_index] = 1;
417019d6b8fSAnthony Liguori  found:
418019d6b8fSAnthony Liguori     l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
419019d6b8fSAnthony Liguori     cluster_offset = be64_to_cpu(l2_table[l2_index]);
420019d6b8fSAnthony Liguori     if (!cluster_offset ||
421019d6b8fSAnthony Liguori         ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
422019d6b8fSAnthony Liguori         if (!allocate)
423019d6b8fSAnthony Liguori             return 0;
424019d6b8fSAnthony Liguori         /* allocate a new cluster */
425019d6b8fSAnthony Liguori         if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
426019d6b8fSAnthony Liguori             (n_end - n_start) < s->cluster_sectors) {
427019d6b8fSAnthony Liguori             /* if the cluster is already compressed, we must
428019d6b8fSAnthony Liguori                decompress it in the case it is not completely
429019d6b8fSAnthony Liguori                overwritten */
43066f82ceeSKevin Wolf             if (decompress_cluster(bs, cluster_offset) < 0)
431019d6b8fSAnthony Liguori                 return 0;
4329a4f4c31SKevin Wolf             cluster_offset = bdrv_getlength(bs->file->bs);
433019d6b8fSAnthony Liguori             cluster_offset = (cluster_offset + s->cluster_size - 1) &
434019d6b8fSAnthony Liguori                 ~(s->cluster_size - 1);
435019d6b8fSAnthony Liguori             /* write the cluster content */
436d9ca2ea2SKevin Wolf             if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache,
4379a4f4c31SKevin Wolf                             s->cluster_size) !=
438019d6b8fSAnthony Liguori                 s->cluster_size)
439019d6b8fSAnthony Liguori                 return -1;
440019d6b8fSAnthony Liguori         } else {
4419a4f4c31SKevin Wolf             cluster_offset = bdrv_getlength(bs->file->bs);
442019d6b8fSAnthony Liguori             if (allocate == 1) {
443019d6b8fSAnthony Liguori                 /* round to cluster size */
444019d6b8fSAnthony Liguori                 cluster_offset = (cluster_offset + s->cluster_size - 1) &
445019d6b8fSAnthony Liguori                     ~(s->cluster_size - 1);
446ed3d2ec9SMax Reitz                 bdrv_truncate(bs->file, cluster_offset + s->cluster_size, NULL);
447019d6b8fSAnthony Liguori                 /* if encrypted, we must initialize the cluster
448019d6b8fSAnthony Liguori                    content which won't be written */
4498336aafaSDaniel P. Berrange                 if (bs->encrypted &&
450019d6b8fSAnthony Liguori                     (n_end - n_start) < s->cluster_sectors) {
451019d6b8fSAnthony Liguori                     uint64_t start_sect;
452d85f4222SDaniel P. Berrange                     assert(s->crypto);
453019d6b8fSAnthony Liguori                     start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
454019d6b8fSAnthony Liguori                     for(i = 0; i < s->cluster_sectors; i++) {
455019d6b8fSAnthony Liguori                         if (i < n_start || i >= n_end) {
456f6fa64f6SDaniel P. Berrange                             Error *err = NULL;
4571fad1f94SDaniel P. Berrange                             memset(s->cluster_data, 0x00, 512);
458d85f4222SDaniel P. Berrange                             if (qcrypto_block_encrypt(s->crypto, start_sect + i,
459d85f4222SDaniel P. Berrange                                                       s->cluster_data,
460d85f4222SDaniel P. Berrange                                                       BDRV_SECTOR_SIZE,
461d85f4222SDaniel P. Berrange                                                       &err) < 0) {
462f6fa64f6SDaniel P. Berrange                                 error_free(err);
463f6fa64f6SDaniel P. Berrange                                 errno = EIO;
464f6fa64f6SDaniel P. Berrange                                 return -1;
465f6fa64f6SDaniel P. Berrange                             }
466d9ca2ea2SKevin Wolf                             if (bdrv_pwrite(bs->file,
4679a4f4c31SKevin Wolf                                             cluster_offset + i * 512,
468019d6b8fSAnthony Liguori                                             s->cluster_data, 512) != 512)
469019d6b8fSAnthony Liguori                                 return -1;
470019d6b8fSAnthony Liguori                         }
471019d6b8fSAnthony Liguori                     }
472019d6b8fSAnthony Liguori                 }
473019d6b8fSAnthony Liguori             } else if (allocate == 2) {
474019d6b8fSAnthony Liguori                 cluster_offset |= QCOW_OFLAG_COMPRESSED |
475019d6b8fSAnthony Liguori                     (uint64_t)compressed_size << (63 - s->cluster_bits);
476019d6b8fSAnthony Liguori             }
477019d6b8fSAnthony Liguori         }
478019d6b8fSAnthony Liguori         /* update L2 table */
479019d6b8fSAnthony Liguori         tmp = cpu_to_be64(cluster_offset);
480019d6b8fSAnthony Liguori         l2_table[l2_index] = tmp;
481d9ca2ea2SKevin Wolf         if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
4825e5557d9SKevin Wolf                 &tmp, sizeof(tmp)) < 0)
483019d6b8fSAnthony Liguori             return 0;
484019d6b8fSAnthony Liguori     }
485019d6b8fSAnthony Liguori     return cluster_offset;
486019d6b8fSAnthony Liguori }
487019d6b8fSAnthony Liguori 
488b6b8a333SPaolo Bonzini static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
48967a0fd2aSFam Zheng         int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
490019d6b8fSAnthony Liguori {
491019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
492019d6b8fSAnthony Liguori     int index_in_cluster, n;
493019d6b8fSAnthony Liguori     uint64_t cluster_offset;
494019d6b8fSAnthony Liguori 
495f8a2e5e3SStefan Hajnoczi     qemu_co_mutex_lock(&s->lock);
496019d6b8fSAnthony Liguori     cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
497f8a2e5e3SStefan Hajnoczi     qemu_co_mutex_unlock(&s->lock);
498019d6b8fSAnthony Liguori     index_in_cluster = sector_num & (s->cluster_sectors - 1);
499019d6b8fSAnthony Liguori     n = s->cluster_sectors - index_in_cluster;
500019d6b8fSAnthony Liguori     if (n > nb_sectors)
501019d6b8fSAnthony Liguori         n = nb_sectors;
502019d6b8fSAnthony Liguori     *pnum = n;
5034bc74be9SPaolo Bonzini     if (!cluster_offset) {
5044bc74be9SPaolo Bonzini         return 0;
5054bc74be9SPaolo Bonzini     }
506d85f4222SDaniel P. Berrange     if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->crypto) {
5074bc74be9SPaolo Bonzini         return BDRV_BLOCK_DATA;
5084bc74be9SPaolo Bonzini     }
5094bc74be9SPaolo Bonzini     cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
5103064bf6fSFam Zheng     *file = bs->file->bs;
5114bc74be9SPaolo Bonzini     return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset;
512019d6b8fSAnthony Liguori }
513019d6b8fSAnthony Liguori 
514019d6b8fSAnthony Liguori static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
515019d6b8fSAnthony Liguori                              const uint8_t *buf, int buf_size)
516019d6b8fSAnthony Liguori {
517019d6b8fSAnthony Liguori     z_stream strm1, *strm = &strm1;
518019d6b8fSAnthony Liguori     int ret, out_len;
519019d6b8fSAnthony Liguori 
520019d6b8fSAnthony Liguori     memset(strm, 0, sizeof(*strm));
521019d6b8fSAnthony Liguori 
522019d6b8fSAnthony Liguori     strm->next_in = (uint8_t *)buf;
523019d6b8fSAnthony Liguori     strm->avail_in = buf_size;
524019d6b8fSAnthony Liguori     strm->next_out = out_buf;
525019d6b8fSAnthony Liguori     strm->avail_out = out_buf_size;
526019d6b8fSAnthony Liguori 
527019d6b8fSAnthony Liguori     ret = inflateInit2(strm, -12);
528019d6b8fSAnthony Liguori     if (ret != Z_OK)
529019d6b8fSAnthony Liguori         return -1;
530019d6b8fSAnthony Liguori     ret = inflate(strm, Z_FINISH);
531019d6b8fSAnthony Liguori     out_len = strm->next_out - out_buf;
532019d6b8fSAnthony Liguori     if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
533019d6b8fSAnthony Liguori         out_len != out_buf_size) {
534019d6b8fSAnthony Liguori         inflateEnd(strm);
535019d6b8fSAnthony Liguori         return -1;
536019d6b8fSAnthony Liguori     }
537019d6b8fSAnthony Liguori     inflateEnd(strm);
538019d6b8fSAnthony Liguori     return 0;
539019d6b8fSAnthony Liguori }
540019d6b8fSAnthony Liguori 
54166f82ceeSKevin Wolf static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
542019d6b8fSAnthony Liguori {
54366f82ceeSKevin Wolf     BDRVQcowState *s = bs->opaque;
544019d6b8fSAnthony Liguori     int ret, csize;
545019d6b8fSAnthony Liguori     uint64_t coffset;
546019d6b8fSAnthony Liguori 
547019d6b8fSAnthony Liguori     coffset = cluster_offset & s->cluster_offset_mask;
548019d6b8fSAnthony Liguori     if (s->cluster_cache_offset != coffset) {
549019d6b8fSAnthony Liguori         csize = cluster_offset >> (63 - s->cluster_bits);
550019d6b8fSAnthony Liguori         csize &= (s->cluster_size - 1);
551cf2ab8fcSKevin Wolf         ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
552019d6b8fSAnthony Liguori         if (ret != csize)
553019d6b8fSAnthony Liguori             return -1;
554019d6b8fSAnthony Liguori         if (decompress_buffer(s->cluster_cache, s->cluster_size,
555019d6b8fSAnthony Liguori                               s->cluster_data, csize) < 0) {
556019d6b8fSAnthony Liguori             return -1;
557019d6b8fSAnthony Liguori         }
558019d6b8fSAnthony Liguori         s->cluster_cache_offset = coffset;
559019d6b8fSAnthony Liguori     }
560019d6b8fSAnthony Liguori     return 0;
561019d6b8fSAnthony Liguori }
562019d6b8fSAnthony Liguori 
563a968168cSDong Xu Wang static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
56427deebe8SFrediano Ziglio                          int nb_sectors, QEMUIOVector *qiov)
565ad53089bSChristoph Hellwig {
566019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
567019d6b8fSAnthony Liguori     int index_in_cluster;
56827deebe8SFrediano Ziglio     int ret = 0, n;
56943ca85b5SFrediano Ziglio     uint64_t cluster_offset;
570430bbaaaSFrediano Ziglio     struct iovec hd_iov;
571430bbaaaSFrediano Ziglio     QEMUIOVector hd_qiov;
57227deebe8SFrediano Ziglio     uint8_t *buf;
57327deebe8SFrediano Ziglio     void *orig_buf;
574f6fa64f6SDaniel P. Berrange     Error *err = NULL;
575019d6b8fSAnthony Liguori 
57627deebe8SFrediano Ziglio     if (qiov->niov > 1) {
5770df93305SKevin Wolf         buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
5780df93305SKevin Wolf         if (buf == NULL) {
5790df93305SKevin Wolf             return -ENOMEM;
5800df93305SKevin Wolf         }
58127deebe8SFrediano Ziglio     } else {
58227deebe8SFrediano Ziglio         orig_buf = NULL;
58327deebe8SFrediano Ziglio         buf = (uint8_t *)qiov->iov->iov_base;
584019d6b8fSAnthony Liguori     }
585019d6b8fSAnthony Liguori 
58627deebe8SFrediano Ziglio     qemu_co_mutex_lock(&s->lock);
58727deebe8SFrediano Ziglio 
58827deebe8SFrediano Ziglio     while (nb_sectors != 0) {
58943ca85b5SFrediano Ziglio         /* prepare next request */
59027deebe8SFrediano Ziglio         cluster_offset = get_cluster_offset(bs, sector_num << 9,
591019d6b8fSAnthony Liguori                                                  0, 0, 0, 0);
59227deebe8SFrediano Ziglio         index_in_cluster = sector_num & (s->cluster_sectors - 1);
593430bbaaaSFrediano Ziglio         n = s->cluster_sectors - index_in_cluster;
59427deebe8SFrediano Ziglio         if (n > nb_sectors) {
59527deebe8SFrediano Ziglio             n = nb_sectors;
596430bbaaaSFrediano Ziglio         }
597019d6b8fSAnthony Liguori 
598430bbaaaSFrediano Ziglio         if (!cluster_offset) {
599760e0063SKevin Wolf             if (bs->backing) {
600019d6b8fSAnthony Liguori                 /* read from the base image */
60127deebe8SFrediano Ziglio                 hd_iov.iov_base = (void *)buf;
602430bbaaaSFrediano Ziglio                 hd_iov.iov_len = n * 512;
603430bbaaaSFrediano Ziglio                 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
60452b8eb60SKevin Wolf                 qemu_co_mutex_unlock(&s->lock);
60528b04a8fSKevin Wolf                 ret = bdrv_co_readv(bs->backing, sector_num, n, &hd_qiov);
60652b8eb60SKevin Wolf                 qemu_co_mutex_lock(&s->lock);
60752b8eb60SKevin Wolf                 if (ret < 0) {
60827deebe8SFrediano Ziglio                     goto fail;
6095614c188SStefan Weil                 }
610019d6b8fSAnthony Liguori             } else {
611019d6b8fSAnthony Liguori                 /* Note: in this case, no need to wait */
61227deebe8SFrediano Ziglio                 memset(buf, 0, 512 * n);
613019d6b8fSAnthony Liguori             }
614430bbaaaSFrediano Ziglio         } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
615019d6b8fSAnthony Liguori             /* add AIO support for compressed blocks ? */
616430bbaaaSFrediano Ziglio             if (decompress_cluster(bs, cluster_offset) < 0) {
61727deebe8SFrediano Ziglio                 goto fail;
6185614c188SStefan Weil             }
61927deebe8SFrediano Ziglio             memcpy(buf,
620430bbaaaSFrediano Ziglio                    s->cluster_cache + index_in_cluster * 512, 512 * n);
621019d6b8fSAnthony Liguori         } else {
622430bbaaaSFrediano Ziglio             if ((cluster_offset & 511) != 0) {
62327deebe8SFrediano Ziglio                 goto fail;
624019d6b8fSAnthony Liguori             }
62527deebe8SFrediano Ziglio             hd_iov.iov_base = (void *)buf;
626430bbaaaSFrediano Ziglio             hd_iov.iov_len = n * 512;
627430bbaaaSFrediano Ziglio             qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
62852b8eb60SKevin Wolf             qemu_co_mutex_unlock(&s->lock);
62928b04a8fSKevin Wolf             ret = bdrv_co_readv(bs->file,
630430bbaaaSFrediano Ziglio                                 (cluster_offset >> 9) + index_in_cluster,
631430bbaaaSFrediano Ziglio                                 n, &hd_qiov);
63252b8eb60SKevin Wolf             qemu_co_mutex_lock(&s->lock);
63352b8eb60SKevin Wolf             if (ret < 0) {
63427deebe8SFrediano Ziglio                 break;
635019d6b8fSAnthony Liguori             }
6368336aafaSDaniel P. Berrange             if (bs->encrypted) {
637d85f4222SDaniel P. Berrange                 assert(s->crypto);
638d85f4222SDaniel P. Berrange                 if (qcrypto_block_decrypt(s->crypto, sector_num, buf,
639d85f4222SDaniel P. Berrange                                           n * BDRV_SECTOR_SIZE, &err) < 0) {
640f6fa64f6SDaniel P. Berrange                     goto fail;
641f6fa64f6SDaniel P. Berrange                 }
64243ca85b5SFrediano Ziglio             }
64343ca85b5SFrediano Ziglio         }
64427deebe8SFrediano Ziglio         ret = 0;
64543ca85b5SFrediano Ziglio 
64627deebe8SFrediano Ziglio         nb_sectors -= n;
64727deebe8SFrediano Ziglio         sector_num += n;
64827deebe8SFrediano Ziglio         buf += n * 512;
64952b8eb60SKevin Wolf     }
650019d6b8fSAnthony Liguori 
65127deebe8SFrediano Ziglio done:
65252b8eb60SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
65352b8eb60SKevin Wolf 
65427deebe8SFrediano Ziglio     if (qiov->niov > 1) {
65503396148SMichael Tokarev         qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size);
65627deebe8SFrediano Ziglio         qemu_vfree(orig_buf);
657019d6b8fSAnthony Liguori     }
65852b8eb60SKevin Wolf 
65952b8eb60SKevin Wolf     return ret;
66027deebe8SFrediano Ziglio 
66127deebe8SFrediano Ziglio fail:
662f6fa64f6SDaniel P. Berrange     error_free(err);
66327deebe8SFrediano Ziglio     ret = -EIO;
66427deebe8SFrediano Ziglio     goto done;
665019d6b8fSAnthony Liguori }
666019d6b8fSAnthony Liguori 
667a968168cSDong Xu Wang static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
66827deebe8SFrediano Ziglio                           int nb_sectors, QEMUIOVector *qiov)
669019d6b8fSAnthony Liguori {
670019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
671019d6b8fSAnthony Liguori     int index_in_cluster;
672019d6b8fSAnthony Liguori     uint64_t cluster_offset;
67327deebe8SFrediano Ziglio     int ret = 0, n;
674430bbaaaSFrediano Ziglio     struct iovec hd_iov;
675430bbaaaSFrediano Ziglio     QEMUIOVector hd_qiov;
67627deebe8SFrediano Ziglio     uint8_t *buf;
67727deebe8SFrediano Ziglio     void *orig_buf;
678019d6b8fSAnthony Liguori 
67927deebe8SFrediano Ziglio     s->cluster_cache_offset = -1; /* disable compressed cache */
68027deebe8SFrediano Ziglio 
6811fad1f94SDaniel P. Berrange     /* We must always copy the iov when encrypting, so we
6821fad1f94SDaniel P. Berrange      * don't modify the original data buffer during encryption */
6831fad1f94SDaniel P. Berrange     if (bs->encrypted || qiov->niov > 1) {
6840df93305SKevin Wolf         buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
6850df93305SKevin Wolf         if (buf == NULL) {
6860df93305SKevin Wolf             return -ENOMEM;
6870df93305SKevin Wolf         }
688d5e6b161SMichael Tokarev         qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
68927deebe8SFrediano Ziglio     } else {
69027deebe8SFrediano Ziglio         orig_buf = NULL;
69127deebe8SFrediano Ziglio         buf = (uint8_t *)qiov->iov->iov_base;
692019d6b8fSAnthony Liguori     }
693019d6b8fSAnthony Liguori 
69427deebe8SFrediano Ziglio     qemu_co_mutex_lock(&s->lock);
69527deebe8SFrediano Ziglio 
69627deebe8SFrediano Ziglio     while (nb_sectors != 0) {
69727deebe8SFrediano Ziglio 
69827deebe8SFrediano Ziglio         index_in_cluster = sector_num & (s->cluster_sectors - 1);
699430bbaaaSFrediano Ziglio         n = s->cluster_sectors - index_in_cluster;
70027deebe8SFrediano Ziglio         if (n > nb_sectors) {
70127deebe8SFrediano Ziglio             n = nb_sectors;
702430bbaaaSFrediano Ziglio         }
70327deebe8SFrediano Ziglio         cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
704019d6b8fSAnthony Liguori                                             index_in_cluster,
705430bbaaaSFrediano Ziglio                                             index_in_cluster + n);
706019d6b8fSAnthony Liguori         if (!cluster_offset || (cluster_offset & 511) != 0) {
70727deebe8SFrediano Ziglio             ret = -EIO;
70827deebe8SFrediano Ziglio             break;
709019d6b8fSAnthony Liguori         }
7108336aafaSDaniel P. Berrange         if (bs->encrypted) {
711f6fa64f6SDaniel P. Berrange             Error *err = NULL;
712d85f4222SDaniel P. Berrange             assert(s->crypto);
713d85f4222SDaniel P. Berrange             if (qcrypto_block_encrypt(s->crypto, sector_num, buf,
714d85f4222SDaniel P. Berrange                                       n * BDRV_SECTOR_SIZE, &err) < 0) {
715f6fa64f6SDaniel P. Berrange                 error_free(err);
716f6fa64f6SDaniel P. Berrange                 ret = -EIO;
717f6fa64f6SDaniel P. Berrange                 break;
718f6fa64f6SDaniel P. Berrange             }
719019d6b8fSAnthony Liguori         }
720019d6b8fSAnthony Liguori 
7211fad1f94SDaniel P. Berrange         hd_iov.iov_base = (void *)buf;
722430bbaaaSFrediano Ziglio         hd_iov.iov_len = n * 512;
723430bbaaaSFrediano Ziglio         qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
72452b8eb60SKevin Wolf         qemu_co_mutex_unlock(&s->lock);
72525ec177dSKevin Wolf         ret = bdrv_co_writev(bs->file,
726019d6b8fSAnthony Liguori                              (cluster_offset >> 9) + index_in_cluster,
727430bbaaaSFrediano Ziglio                              n, &hd_qiov);
72852b8eb60SKevin Wolf         qemu_co_mutex_lock(&s->lock);
72952b8eb60SKevin Wolf         if (ret < 0) {
73027deebe8SFrediano Ziglio             break;
7315614c188SStefan Weil         }
73227deebe8SFrediano Ziglio         ret = 0;
73343ca85b5SFrediano Ziglio 
73427deebe8SFrediano Ziglio         nb_sectors -= n;
73527deebe8SFrediano Ziglio         sector_num += n;
73627deebe8SFrediano Ziglio         buf += n * 512;
737019d6b8fSAnthony Liguori     }
73852b8eb60SKevin Wolf     qemu_co_mutex_unlock(&s->lock);
739019d6b8fSAnthony Liguori 
74027deebe8SFrediano Ziglio     qemu_vfree(orig_buf);
741b11a24deSKevin Wolf 
74252b8eb60SKevin Wolf     return ret;
743019d6b8fSAnthony Liguori }
744019d6b8fSAnthony Liguori 
745019d6b8fSAnthony Liguori static void qcow_close(BlockDriverState *bs)
746019d6b8fSAnthony Liguori {
747019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
748fd9f102cSKevin Wolf 
749d85f4222SDaniel P. Berrange     qcrypto_block_free(s->crypto);
750d85f4222SDaniel P. Berrange     s->crypto = NULL;
7517267c094SAnthony Liguori     g_free(s->l1_table);
7520df93305SKevin Wolf     qemu_vfree(s->l2_cache);
7537267c094SAnthony Liguori     g_free(s->cluster_cache);
7547267c094SAnthony Liguori     g_free(s->cluster_data);
755fd9f102cSKevin Wolf 
756fd9f102cSKevin Wolf     migrate_del_blocker(s->migration_blocker);
757fd9f102cSKevin Wolf     error_free(s->migration_blocker);
758019d6b8fSAnthony Liguori }
759019d6b8fSAnthony Liguori 
76016d12159SChunyan Liu static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
761019d6b8fSAnthony Liguori {
7622b16c9ffSLi Zhi Hui     int header_size, backing_filename_len, l1_size, shift, i;
763019d6b8fSAnthony Liguori     QCowHeader header;
7642b16c9ffSLi Zhi Hui     uint8_t *tmp;
7650e7e1989SKevin Wolf     int64_t total_size = 0;
76616d12159SChunyan Liu     char *backing_file = NULL;
76734b5d2c6SMax Reitz     Error *local_err = NULL;
7683e1a8134SKirill A. Shutemov     int ret;
7696af40160SKevin Wolf     BlockBackend *qcow_blk;
7700cb8d47bSDaniel P. Berrange     const char *encryptfmt = NULL;
771d85f4222SDaniel P. Berrange     QDict *options;
772d85f4222SDaniel P. Berrange     QDict *encryptopts = NULL;
773d85f4222SDaniel P. Berrange     QCryptoBlockCreateOptions *crypto_opts = NULL;
774d85f4222SDaniel P. Berrange     QCryptoBlock *crypto = NULL;
7750e7e1989SKevin Wolf 
7760e7e1989SKevin Wolf     /* Read out options */
777180e9526SHu Tao     total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
778c2eb918eSHu Tao                           BDRV_SECTOR_SIZE);
7796aa837f7SDaniel P. Berrange     if (total_size == 0) {
7806aa837f7SDaniel P. Berrange         error_setg(errp, "Image size is too small, cannot be zero length");
7816aa837f7SDaniel P. Berrange         ret = -EINVAL;
7826aa837f7SDaniel P. Berrange         goto cleanup;
7836aa837f7SDaniel P. Berrange     }
7846aa837f7SDaniel P. Berrange 
78516d12159SChunyan Liu     backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
7860cb8d47bSDaniel P. Berrange     encryptfmt = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
7870cb8d47bSDaniel P. Berrange     if (encryptfmt) {
7880cb8d47bSDaniel P. Berrange         if (qemu_opt_get(opts, BLOCK_OPT_ENCRYPT)) {
7890cb8d47bSDaniel P. Berrange             error_setg(errp, "Options " BLOCK_OPT_ENCRYPT " and "
7900cb8d47bSDaniel P. Berrange                        BLOCK_OPT_ENCRYPT_FORMAT " are mutually exclusive");
7910cb8d47bSDaniel P. Berrange             ret = -EINVAL;
7920cb8d47bSDaniel P. Berrange             goto cleanup;
7930cb8d47bSDaniel P. Berrange         }
7940cb8d47bSDaniel P. Berrange     } else if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
7950cb8d47bSDaniel P. Berrange         encryptfmt = "aes";
7960e7e1989SKevin Wolf     }
797019d6b8fSAnthony Liguori 
798c282e1fdSChunyan Liu     ret = bdrv_create_file(filename, opts, &local_err);
7992b16c9ffSLi Zhi Hui     if (ret < 0) {
800b6d5066dSPaolo Bonzini         error_propagate(errp, local_err);
80116d12159SChunyan Liu         goto cleanup;
8022b16c9ffSLi Zhi Hui     }
8032b16c9ffSLi Zhi Hui 
804efaa7c4eSMax Reitz     qcow_blk = blk_new_open(filename, NULL, NULL,
80555880601SKevin Wolf                             BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
80655880601SKevin Wolf                             &local_err);
8076af40160SKevin Wolf     if (qcow_blk == NULL) {
808b6d5066dSPaolo Bonzini         error_propagate(errp, local_err);
8096af40160SKevin Wolf         ret = -EIO;
81016d12159SChunyan Liu         goto cleanup;
8112b16c9ffSLi Zhi Hui     }
8122b16c9ffSLi Zhi Hui 
8136af40160SKevin Wolf     blk_set_allow_write_beyond_eof(qcow_blk, true);
8146af40160SKevin Wolf 
815ed3d2ec9SMax Reitz     ret = blk_truncate(qcow_blk, 0, errp);
8162b16c9ffSLi Zhi Hui     if (ret < 0) {
8172b16c9ffSLi Zhi Hui         goto exit;
8182b16c9ffSLi Zhi Hui     }
8192b16c9ffSLi Zhi Hui 
820019d6b8fSAnthony Liguori     memset(&header, 0, sizeof(header));
821019d6b8fSAnthony Liguori     header.magic = cpu_to_be32(QCOW_MAGIC);
822019d6b8fSAnthony Liguori     header.version = cpu_to_be32(QCOW_VERSION);
823180e9526SHu Tao     header.size = cpu_to_be64(total_size);
824019d6b8fSAnthony Liguori     header_size = sizeof(header);
825019d6b8fSAnthony Liguori     backing_filename_len = 0;
826019d6b8fSAnthony Liguori     if (backing_file) {
827019d6b8fSAnthony Liguori         if (strcmp(backing_file, "fat:")) {
828019d6b8fSAnthony Liguori             header.backing_file_offset = cpu_to_be64(header_size);
829019d6b8fSAnthony Liguori             backing_filename_len = strlen(backing_file);
830019d6b8fSAnthony Liguori             header.backing_file_size = cpu_to_be32(backing_filename_len);
831019d6b8fSAnthony Liguori             header_size += backing_filename_len;
832019d6b8fSAnthony Liguori         } else {
833019d6b8fSAnthony Liguori             /* special backing file for vvfat */
834272545cfSPeter Maydell             g_free(backing_file);
835019d6b8fSAnthony Liguori             backing_file = NULL;
836019d6b8fSAnthony Liguori         }
837019d6b8fSAnthony Liguori         header.cluster_bits = 9; /* 512 byte cluster to avoid copying
838dc6fb73dSDeepak Kathayat                                     unmodified sectors */
839019d6b8fSAnthony Liguori         header.l2_bits = 12; /* 32 KB L2 tables */
840019d6b8fSAnthony Liguori     } else {
841019d6b8fSAnthony Liguori         header.cluster_bits = 12; /* 4 KB clusters */
842019d6b8fSAnthony Liguori         header.l2_bits = 9; /* 4 KB L2 tables */
843019d6b8fSAnthony Liguori     }
844019d6b8fSAnthony Liguori     header_size = (header_size + 7) & ~7;
845019d6b8fSAnthony Liguori     shift = header.cluster_bits + header.l2_bits;
846180e9526SHu Tao     l1_size = (total_size + (1LL << shift) - 1) >> shift;
847019d6b8fSAnthony Liguori 
848019d6b8fSAnthony Liguori     header.l1_table_offset = cpu_to_be64(header_size);
849d85f4222SDaniel P. Berrange 
850d85f4222SDaniel P. Berrange     options = qemu_opts_to_qdict(opts, NULL);
851d85f4222SDaniel P. Berrange     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
852d85f4222SDaniel P. Berrange     QDECREF(options);
8530cb8d47bSDaniel P. Berrange     if (encryptfmt) {
8540cb8d47bSDaniel P. Berrange         if (!g_str_equal(encryptfmt, "aes")) {
8550cb8d47bSDaniel P. Berrange             error_setg(errp, "Unknown encryption format '%s', expected 'aes'",
8560cb8d47bSDaniel P. Berrange                        encryptfmt);
8570cb8d47bSDaniel P. Berrange             ret = -EINVAL;
8580cb8d47bSDaniel P. Berrange             goto exit;
8590cb8d47bSDaniel P. Berrange         }
860019d6b8fSAnthony Liguori         header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
861d85f4222SDaniel P. Berrange 
862d85f4222SDaniel P. Berrange         crypto_opts = block_crypto_create_opts_init(
863d85f4222SDaniel P. Berrange             Q_CRYPTO_BLOCK_FORMAT_QCOW, encryptopts, errp);
864d85f4222SDaniel P. Berrange         if (!crypto_opts) {
865d85f4222SDaniel P. Berrange             ret = -EINVAL;
866d85f4222SDaniel P. Berrange             goto exit;
867d85f4222SDaniel P. Berrange         }
868d85f4222SDaniel P. Berrange 
869d85f4222SDaniel P. Berrange         crypto = qcrypto_block_create(crypto_opts, NULL, NULL, NULL, errp);
870d85f4222SDaniel P. Berrange         if (!crypto) {
871d85f4222SDaniel P. Berrange             ret = -EINVAL;
872d85f4222SDaniel P. Berrange             goto exit;
873d85f4222SDaniel P. Berrange         }
874019d6b8fSAnthony Liguori     } else {
875019d6b8fSAnthony Liguori         header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
876019d6b8fSAnthony Liguori     }
877019d6b8fSAnthony Liguori 
878019d6b8fSAnthony Liguori     /* write all the data */
8798341f00dSEric Blake     ret = blk_pwrite(qcow_blk, 0, &header, sizeof(header), 0);
8803e1a8134SKirill A. Shutemov     if (ret != sizeof(header)) {
8813e1a8134SKirill A. Shutemov         goto exit;
8823e1a8134SKirill A. Shutemov     }
8833e1a8134SKirill A. Shutemov 
884019d6b8fSAnthony Liguori     if (backing_file) {
8856af40160SKevin Wolf         ret = blk_pwrite(qcow_blk, sizeof(header),
8868341f00dSEric Blake                          backing_file, backing_filename_len, 0);
8873e1a8134SKirill A. Shutemov         if (ret != backing_filename_len) {
8883e1a8134SKirill A. Shutemov             goto exit;
889019d6b8fSAnthony Liguori         }
8903e1a8134SKirill A. Shutemov     }
8913e1a8134SKirill A. Shutemov 
8922b16c9ffSLi Zhi Hui     tmp = g_malloc0(BDRV_SECTOR_SIZE);
893d737b78cSLaurent Vivier     for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE);
894d737b78cSLaurent Vivier          i++) {
8958341f00dSEric Blake         ret = blk_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i,
8968341f00dSEric Blake                          tmp, BDRV_SECTOR_SIZE, 0);
8972b16c9ffSLi Zhi Hui         if (ret != BDRV_SECTOR_SIZE) {
8982b16c9ffSLi Zhi Hui             g_free(tmp);
8992b16c9ffSLi Zhi Hui             goto exit;
9002b16c9ffSLi Zhi Hui         }
9012b16c9ffSLi Zhi Hui     }
9022b16c9ffSLi Zhi Hui 
9032b16c9ffSLi Zhi Hui     g_free(tmp);
9043e1a8134SKirill A. Shutemov     ret = 0;
9053e1a8134SKirill A. Shutemov exit:
9066af40160SKevin Wolf     blk_unref(qcow_blk);
90716d12159SChunyan Liu cleanup:
908d85f4222SDaniel P. Berrange     QDECREF(encryptopts);
909d85f4222SDaniel P. Berrange     qcrypto_block_free(crypto);
910d85f4222SDaniel P. Berrange     qapi_free_QCryptoBlockCreateOptions(crypto_opts);
91116d12159SChunyan Liu     g_free(backing_file);
9123e1a8134SKirill A. Shutemov     return ret;
913019d6b8fSAnthony Liguori }
914019d6b8fSAnthony Liguori 
915019d6b8fSAnthony Liguori static int qcow_make_empty(BlockDriverState *bs)
916019d6b8fSAnthony Liguori {
917019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
918019d6b8fSAnthony Liguori     uint32_t l1_length = s->l1_size * sizeof(uint64_t);
919019d6b8fSAnthony Liguori     int ret;
920019d6b8fSAnthony Liguori 
921019d6b8fSAnthony Liguori     memset(s->l1_table, 0, l1_length);
922d9ca2ea2SKevin Wolf     if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
9235e5557d9SKevin Wolf             l1_length) < 0)
924019d6b8fSAnthony Liguori         return -1;
925ed3d2ec9SMax Reitz     ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, NULL);
926019d6b8fSAnthony Liguori     if (ret < 0)
927019d6b8fSAnthony Liguori         return ret;
928019d6b8fSAnthony Liguori 
929019d6b8fSAnthony Liguori     memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
930019d6b8fSAnthony Liguori     memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
931019d6b8fSAnthony Liguori     memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
932019d6b8fSAnthony Liguori 
933019d6b8fSAnthony Liguori     return 0;
934019d6b8fSAnthony Liguori }
935019d6b8fSAnthony Liguori 
936019d6b8fSAnthony Liguori /* XXX: put compressed sectors first, then all the cluster aligned
937019d6b8fSAnthony Liguori    tables to avoid losing bytes in alignment */
938f2b95a12SPavel Butsykin static coroutine_fn int
939f2b95a12SPavel Butsykin qcow_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,
940f2b95a12SPavel Butsykin                            uint64_t bytes, QEMUIOVector *qiov)
941019d6b8fSAnthony Liguori {
942019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
943f2b95a12SPavel Butsykin     QEMUIOVector hd_qiov;
944f2b95a12SPavel Butsykin     struct iovec iov;
945019d6b8fSAnthony Liguori     z_stream strm;
946019d6b8fSAnthony Liguori     int ret, out_len;
947f2b95a12SPavel Butsykin     uint8_t *buf, *out_buf;
948019d6b8fSAnthony Liguori     uint64_t cluster_offset;
949019d6b8fSAnthony Liguori 
950f2b95a12SPavel Butsykin     buf = qemu_blockalign(bs, s->cluster_size);
951655923dfSPavel Butsykin     if (bytes != s->cluster_size) {
952655923dfSPavel Butsykin         if (bytes > s->cluster_size ||
953655923dfSPavel Butsykin             offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
954655923dfSPavel Butsykin         {
955655923dfSPavel Butsykin             qemu_vfree(buf);
956655923dfSPavel Butsykin             return -EINVAL;
957655923dfSPavel Butsykin         }
958655923dfSPavel Butsykin         /* Zero-pad last write if image size is not cluster aligned */
959655923dfSPavel Butsykin         memset(buf + bytes, 0, s->cluster_size - bytes);
960655923dfSPavel Butsykin     }
961f2b95a12SPavel Butsykin     qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
962019d6b8fSAnthony Liguori 
963ebf7bba0SVladimir Sementsov-Ogievskiy     out_buf = g_malloc(s->cluster_size);
964019d6b8fSAnthony Liguori 
965019d6b8fSAnthony Liguori     /* best compression, small window, no zlib header */
966019d6b8fSAnthony Liguori     memset(&strm, 0, sizeof(strm));
967019d6b8fSAnthony Liguori     ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
968019d6b8fSAnthony Liguori                        Z_DEFLATED, -12,
969019d6b8fSAnthony Liguori                        9, Z_DEFAULT_STRATEGY);
970019d6b8fSAnthony Liguori     if (ret != 0) {
97164ebe71aSKevin Wolf         ret = -EINVAL;
97264ebe71aSKevin Wolf         goto fail;
973019d6b8fSAnthony Liguori     }
974019d6b8fSAnthony Liguori 
975019d6b8fSAnthony Liguori     strm.avail_in = s->cluster_size;
976019d6b8fSAnthony Liguori     strm.next_in = (uint8_t *)buf;
977019d6b8fSAnthony Liguori     strm.avail_out = s->cluster_size;
978019d6b8fSAnthony Liguori     strm.next_out = out_buf;
979019d6b8fSAnthony Liguori 
980019d6b8fSAnthony Liguori     ret = deflate(&strm, Z_FINISH);
981019d6b8fSAnthony Liguori     if (ret != Z_STREAM_END && ret != Z_OK) {
982019d6b8fSAnthony Liguori         deflateEnd(&strm);
98364ebe71aSKevin Wolf         ret = -EINVAL;
98464ebe71aSKevin Wolf         goto fail;
985019d6b8fSAnthony Liguori     }
986019d6b8fSAnthony Liguori     out_len = strm.next_out - out_buf;
987019d6b8fSAnthony Liguori 
988019d6b8fSAnthony Liguori     deflateEnd(&strm);
989019d6b8fSAnthony Liguori 
990019d6b8fSAnthony Liguori     if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
991019d6b8fSAnthony Liguori         /* could not compress: write normal cluster */
992f2b95a12SPavel Butsykin         ret = qcow_co_writev(bs, offset >> BDRV_SECTOR_BITS,
993f2b95a12SPavel Butsykin                              bytes >> BDRV_SECTOR_BITS, qiov);
99464ebe71aSKevin Wolf         if (ret < 0) {
99564ebe71aSKevin Wolf             goto fail;
99664ebe71aSKevin Wolf         }
997f2b95a12SPavel Butsykin         goto success;
998f2b95a12SPavel Butsykin     }
999f2b95a12SPavel Butsykin     qemu_co_mutex_lock(&s->lock);
1000f2b95a12SPavel Butsykin     cluster_offset = get_cluster_offset(bs, offset, 2, out_len, 0, 0);
1001f2b95a12SPavel Butsykin     qemu_co_mutex_unlock(&s->lock);
100264ebe71aSKevin Wolf     if (cluster_offset == 0) {
100364ebe71aSKevin Wolf         ret = -EIO;
100464ebe71aSKevin Wolf         goto fail;
100564ebe71aSKevin Wolf     }
1006019d6b8fSAnthony Liguori     cluster_offset &= s->cluster_offset_mask;
1007f2b95a12SPavel Butsykin 
1008f2b95a12SPavel Butsykin     iov = (struct iovec) {
1009f2b95a12SPavel Butsykin         .iov_base   = out_buf,
1010f2b95a12SPavel Butsykin         .iov_len    = out_len,
1011f2b95a12SPavel Butsykin     };
1012f2b95a12SPavel Butsykin     qemu_iovec_init_external(&hd_qiov, &iov, 1);
1013f2b95a12SPavel Butsykin     ret = bdrv_co_pwritev(bs->file, cluster_offset, out_len, &hd_qiov, 0);
101464ebe71aSKevin Wolf     if (ret < 0) {
101564ebe71aSKevin Wolf         goto fail;
1016019d6b8fSAnthony Liguori     }
1017f2b95a12SPavel Butsykin success:
101864ebe71aSKevin Wolf     ret = 0;
101964ebe71aSKevin Wolf fail:
1020f2b95a12SPavel Butsykin     qemu_vfree(buf);
10217267c094SAnthony Liguori     g_free(out_buf);
102264ebe71aSKevin Wolf     return ret;
1023019d6b8fSAnthony Liguori }
1024019d6b8fSAnthony Liguori 
1025019d6b8fSAnthony Liguori static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1026019d6b8fSAnthony Liguori {
1027019d6b8fSAnthony Liguori     BDRVQcowState *s = bs->opaque;
1028019d6b8fSAnthony Liguori     bdi->cluster_size = s->cluster_size;
1029019d6b8fSAnthony Liguori     return 0;
1030019d6b8fSAnthony Liguori }
1031019d6b8fSAnthony Liguori 
103216d12159SChunyan Liu static QemuOptsList qcow_create_opts = {
103316d12159SChunyan Liu     .name = "qcow-create-opts",
103416d12159SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
103516d12159SChunyan Liu     .desc = {
1036db08adf5SKevin Wolf         {
1037db08adf5SKevin Wolf             .name = BLOCK_OPT_SIZE,
103816d12159SChunyan Liu             .type = QEMU_OPT_SIZE,
1039db08adf5SKevin Wolf             .help = "Virtual disk size"
1040db08adf5SKevin Wolf         },
1041db08adf5SKevin Wolf         {
1042db08adf5SKevin Wolf             .name = BLOCK_OPT_BACKING_FILE,
104316d12159SChunyan Liu             .type = QEMU_OPT_STRING,
1044db08adf5SKevin Wolf             .help = "File name of a base image"
1045db08adf5SKevin Wolf         },
1046db08adf5SKevin Wolf         {
1047db08adf5SKevin Wolf             .name = BLOCK_OPT_ENCRYPT,
104816d12159SChunyan Liu             .type = QEMU_OPT_BOOL,
10490cb8d47bSDaniel P. Berrange             .help = "Encrypt the image with format 'aes'. (Deprecated "
10500cb8d47bSDaniel P. Berrange                     "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
10510cb8d47bSDaniel P. Berrange         },
10520cb8d47bSDaniel P. Berrange         {
10530cb8d47bSDaniel P. Berrange             .name = BLOCK_OPT_ENCRYPT_FORMAT,
10540cb8d47bSDaniel P. Berrange             .type = QEMU_OPT_STRING,
10550cb8d47bSDaniel P. Berrange             .help = "Encrypt the image, format choices: 'aes'",
1056db08adf5SKevin Wolf         },
1057d85f4222SDaniel P. Berrange         BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."),
105816d12159SChunyan Liu         { /* end of list */ }
105916d12159SChunyan Liu     }
10600e7e1989SKevin Wolf };
10610e7e1989SKevin Wolf 
1062019d6b8fSAnthony Liguori static BlockDriver bdrv_qcow = {
1063019d6b8fSAnthony Liguori     .format_name	= "qcow",
1064019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVQcowState),
1065019d6b8fSAnthony Liguori     .bdrv_probe		= qcow_probe,
1066019d6b8fSAnthony Liguori     .bdrv_open		= qcow_open,
1067019d6b8fSAnthony Liguori     .bdrv_close		= qcow_close,
1068862f215fSKevin Wolf     .bdrv_child_perm        = bdrv_format_default_perms,
1069d177692eSJeff Cody     .bdrv_reopen_prepare    = qcow_reopen_prepare,
1070c282e1fdSChunyan Liu     .bdrv_create            = qcow_create,
10713ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
10728ee79e70SKevin Wolf     .supports_backing       = true,
1073c68b89acSKevin Wolf 
107452b8eb60SKevin Wolf     .bdrv_co_readv          = qcow_co_readv,
107552b8eb60SKevin Wolf     .bdrv_co_writev         = qcow_co_writev,
1076b6b8a333SPaolo Bonzini     .bdrv_co_get_block_status   = qcow_co_get_block_status,
1077c68b89acSKevin Wolf 
1078c68b89acSKevin Wolf     .bdrv_make_empty        = qcow_make_empty,
1079f2b95a12SPavel Butsykin     .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed,
1080019d6b8fSAnthony Liguori     .bdrv_get_info          = qcow_get_info,
10810e7e1989SKevin Wolf 
108216d12159SChunyan Liu     .create_opts            = &qcow_create_opts,
1083019d6b8fSAnthony Liguori };
1084019d6b8fSAnthony Liguori 
1085019d6b8fSAnthony Liguori static void bdrv_qcow_init(void)
1086019d6b8fSAnthony Liguori {
1087019d6b8fSAnthony Liguori     bdrv_register(&bdrv_qcow);
1088019d6b8fSAnthony Liguori }
1089019d6b8fSAnthony Liguori 
1090019d6b8fSAnthony Liguori block_init(bdrv_qcow_init);
1091