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 */
24452fcdbcSMarkus Armbruster
2580c71a24SPeter Maydell #include "qemu/osdep.h"
26da34e65cSMarkus Armbruster #include "qapi/error.h"
27e6ff69bfSDaniel P. Berrange #include "qemu/error-report.h"
28737e150eSPaolo Bonzini #include "block/block_int.h"
29609f45eaSMax Reitz #include "block/qdict.h"
306af40160SKevin Wolf #include "sysemu/block-backend.h"
311de7afc9SPaolo Bonzini #include "qemu/module.h"
32922a01a0SMarkus Armbruster #include "qemu/option.h"
3358369e22SPaolo Bonzini #include "qemu/bswap.h"
34998c2019SMax Reitz #include "qemu/cutils.h"
355df022cfSPeter Maydell #include "qemu/memalign.h"
36019d6b8fSAnthony Liguori #include <zlib.h>
37452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
38d85f4222SDaniel P. Berrange #include "qapi/qmp/qstring.h"
3942a3e1abSKevin Wolf #include "qapi/qobject-input-visitor.h"
4042a3e1abSKevin Wolf #include "qapi/qapi-visit-block-core.h"
41d85f4222SDaniel P. Berrange #include "crypto/block.h"
42795c40b8SJuan Quintela #include "migration/blocker.h"
430d8c41daSMichael S. Tsirkin #include "crypto.h"
44019d6b8fSAnthony Liguori
45019d6b8fSAnthony Liguori /**************************************************************/
46019d6b8fSAnthony Liguori /* QEMU COW block driver with compression and encryption support */
47019d6b8fSAnthony Liguori
48019d6b8fSAnthony Liguori #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
49019d6b8fSAnthony Liguori #define QCOW_VERSION 1
50019d6b8fSAnthony Liguori
51019d6b8fSAnthony Liguori #define QCOW_CRYPT_NONE 0
52019d6b8fSAnthony Liguori #define QCOW_CRYPT_AES 1
53019d6b8fSAnthony Liguori
54019d6b8fSAnthony Liguori #define QCOW_OFLAG_COMPRESSED (1LL << 63)
55019d6b8fSAnthony Liguori
56019d6b8fSAnthony Liguori typedef struct QCowHeader {
57019d6b8fSAnthony Liguori uint32_t magic;
58019d6b8fSAnthony Liguori uint32_t version;
59019d6b8fSAnthony Liguori uint64_t backing_file_offset;
60019d6b8fSAnthony Liguori uint32_t backing_file_size;
61019d6b8fSAnthony Liguori uint32_t mtime;
62019d6b8fSAnthony Liguori uint64_t size; /* in bytes */
63019d6b8fSAnthony Liguori uint8_t cluster_bits;
64019d6b8fSAnthony Liguori uint8_t l2_bits;
65ea54feffSKevin Wolf uint16_t padding;
66019d6b8fSAnthony Liguori uint32_t crypt_method;
67019d6b8fSAnthony Liguori uint64_t l1_table_offset;
68ea54feffSKevin Wolf } QEMU_PACKED QCowHeader;
69019d6b8fSAnthony Liguori
70019d6b8fSAnthony Liguori #define L2_CACHE_SIZE 16
71019d6b8fSAnthony Liguori
72019d6b8fSAnthony Liguori typedef struct BDRVQcowState {
73019d6b8fSAnthony Liguori int cluster_bits;
74019d6b8fSAnthony Liguori int cluster_size;
75019d6b8fSAnthony Liguori int l2_bits;
76019d6b8fSAnthony Liguori int l2_size;
7746485de0SKevin Wolf unsigned int l1_size;
78019d6b8fSAnthony Liguori uint64_t cluster_offset_mask;
79019d6b8fSAnthony Liguori uint64_t l1_table_offset;
80019d6b8fSAnthony Liguori uint64_t *l1_table;
81019d6b8fSAnthony Liguori uint64_t *l2_cache;
82019d6b8fSAnthony Liguori uint64_t l2_cache_offsets[L2_CACHE_SIZE];
83019d6b8fSAnthony Liguori uint32_t l2_cache_counts[L2_CACHE_SIZE];
84019d6b8fSAnthony Liguori uint8_t *cluster_cache;
85019d6b8fSAnthony Liguori uint8_t *cluster_data;
86019d6b8fSAnthony Liguori uint64_t cluster_cache_offset;
87d85f4222SDaniel P. Berrange QCryptoBlock *crypto; /* Disk encryption format driver */
88019d6b8fSAnthony Liguori uint32_t crypt_method_header;
8952b8eb60SKevin Wolf CoMutex lock;
90fd9f102cSKevin Wolf Error *migration_blocker;
91019d6b8fSAnthony Liguori } BDRVQcowState;
92019d6b8fSAnthony Liguori
9342a3e1abSKevin Wolf static QemuOptsList qcow_create_opts;
9442a3e1abSKevin Wolf
95b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
96b9b10c35SKevin Wolf decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
97019d6b8fSAnthony Liguori
qcow_probe(const uint8_t * buf,int buf_size,const char * filename)98019d6b8fSAnthony Liguori static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
99019d6b8fSAnthony Liguori {
100019d6b8fSAnthony Liguori const QCowHeader *cow_header = (const void *)buf;
101019d6b8fSAnthony Liguori
102019d6b8fSAnthony Liguori if (buf_size >= sizeof(QCowHeader) &&
103019d6b8fSAnthony Liguori be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
104019d6b8fSAnthony Liguori be32_to_cpu(cow_header->version) == QCOW_VERSION)
105019d6b8fSAnthony Liguori return 100;
106019d6b8fSAnthony Liguori else
107019d6b8fSAnthony Liguori return 0;
108019d6b8fSAnthony Liguori }
109019d6b8fSAnthony Liguori
qcow_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)110015a1036SMax Reitz static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
111015a1036SMax Reitz Error **errp)
112019d6b8fSAnthony Liguori {
113019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
114d66e5ceeSKevin Wolf unsigned int len, i, shift;
115d66e5ceeSKevin Wolf int ret;
116019d6b8fSAnthony Liguori QCowHeader header;
117d85f4222SDaniel P. Berrange QCryptoBlockOpenOptions *crypto_opts = NULL;
118d85f4222SDaniel P. Berrange unsigned int cflags = 0;
119d85f4222SDaniel P. Berrange QDict *encryptopts = NULL;
120d85f4222SDaniel P. Berrange const char *encryptfmt;
121d85f4222SDaniel P. Berrange
122d85f4222SDaniel P. Berrange qdict_extract_subqdict(options, &encryptopts, "encrypt.");
123d85f4222SDaniel P. Berrange encryptfmt = qdict_get_try_str(encryptopts, "format");
124019d6b8fSAnthony Liguori
12583930780SVladimir Sementsov-Ogievskiy ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
12683930780SVladimir Sementsov-Ogievskiy if (ret < 0) {
127a4b740dbSKevin Wolf goto fail_unlocked;
1284e4bf5c4SKevin Wolf }
1294e4bf5c4SKevin Wolf
130a4b740dbSKevin Wolf bdrv_graph_rdlock_main_loop();
131a4b740dbSKevin Wolf
13232cc71deSAlberto Faria ret = bdrv_pread(bs->file, 0, sizeof(header), &header, 0);
13384b0ec02SLi Zhi Hui if (ret < 0) {
134019d6b8fSAnthony Liguori goto fail;
13584b0ec02SLi Zhi Hui }
136a5fdff18SPeter Maydell header.magic = be32_to_cpu(header.magic);
137a5fdff18SPeter Maydell header.version = be32_to_cpu(header.version);
138a5fdff18SPeter Maydell header.backing_file_offset = be64_to_cpu(header.backing_file_offset);
139a5fdff18SPeter Maydell header.backing_file_size = be32_to_cpu(header.backing_file_size);
140a5fdff18SPeter Maydell header.mtime = be32_to_cpu(header.mtime);
141a5fdff18SPeter Maydell header.size = be64_to_cpu(header.size);
142a5fdff18SPeter Maydell header.crypt_method = be32_to_cpu(header.crypt_method);
143a5fdff18SPeter Maydell header.l1_table_offset = be64_to_cpu(header.l1_table_offset);
144019d6b8fSAnthony Liguori
14584b0ec02SLi Zhi Hui if (header.magic != QCOW_MAGIC) {
14676abe407SPaolo Bonzini error_setg(errp, "Image not in qcow format");
14776abe407SPaolo Bonzini ret = -EINVAL;
148019d6b8fSAnthony Liguori goto fail;
14984b0ec02SLi Zhi Hui }
15084b0ec02SLi Zhi Hui if (header.version != QCOW_VERSION) {
151197bfa7dSJohn Snow error_setg(errp, "qcow (v%d) does not support qcow version %" PRIu32,
152197bfa7dSJohn Snow QCOW_VERSION, header.version);
153197bfa7dSJohn Snow if (header.version == 2 || header.version == 3) {
154197bfa7dSJohn Snow error_append_hint(errp, "Try the 'qcow2' driver instead.\n");
155197bfa7dSJohn Snow }
156197bfa7dSJohn Snow
15784b0ec02SLi Zhi Hui ret = -ENOTSUP;
158019d6b8fSAnthony Liguori goto fail;
15984b0ec02SLi Zhi Hui }
16084b0ec02SLi Zhi Hui
1617159a45bSKevin Wolf if (header.size <= 1) {
1627159a45bSKevin Wolf error_setg(errp, "Image size is too small (must be at least 2 bytes)");
16384b0ec02SLi Zhi Hui ret = -EINVAL;
164019d6b8fSAnthony Liguori goto fail;
16584b0ec02SLi Zhi Hui }
1667159a45bSKevin Wolf if (header.cluster_bits < 9 || header.cluster_bits > 16) {
1677159a45bSKevin Wolf error_setg(errp, "Cluster size must be between 512 and 64k");
1687159a45bSKevin Wolf ret = -EINVAL;
1697159a45bSKevin Wolf goto fail;
1707159a45bSKevin Wolf }
1717159a45bSKevin Wolf
17242eb5817SKevin Wolf /* l2_bits specifies number of entries; storing a uint64_t in each entry,
17342eb5817SKevin Wolf * so bytes = num_entries << 3. */
17442eb5817SKevin Wolf if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
17542eb5817SKevin Wolf error_setg(errp, "L2 table size must be between 512 and 64k");
17642eb5817SKevin Wolf ret = -EINVAL;
17742eb5817SKevin Wolf goto fail;
17842eb5817SKevin Wolf }
17942eb5817SKevin Wolf
180019d6b8fSAnthony Liguori s->crypt_method_header = header.crypt_method;
18184b0ec02SLi Zhi Hui if (s->crypt_method_header) {
182e6ff69bfSDaniel P. Berrange if (bdrv_uses_whitelist() &&
183e6ff69bfSDaniel P. Berrange s->crypt_method_header == QCOW_CRYPT_AES) {
1848c0dcbc4SDaniel P. Berrange error_setg(errp,
1858c0dcbc4SDaniel P. Berrange "Use of AES-CBC encrypted qcow images is no longer "
1868c0dcbc4SDaniel P. Berrange "supported in system emulators");
1878c0dcbc4SDaniel P. Berrange error_append_hint(errp,
1888c0dcbc4SDaniel P. Berrange "You can use 'qemu-img convert' to convert your "
1898c0dcbc4SDaniel P. Berrange "image to an alternative supported format, such "
1908c0dcbc4SDaniel P. Berrange "as unencrypted qcow, or raw with the LUKS "
1918c0dcbc4SDaniel P. Berrange "format instead.\n");
1928c0dcbc4SDaniel P. Berrange ret = -ENOSYS;
1938c0dcbc4SDaniel P. Berrange goto fail;
194e6ff69bfSDaniel P. Berrange }
195d85f4222SDaniel P. Berrange if (s->crypt_method_header == QCOW_CRYPT_AES) {
196d85f4222SDaniel P. Berrange if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
197d85f4222SDaniel P. Berrange error_setg(errp,
198d85f4222SDaniel P. Berrange "Header reported 'aes' encryption format but "
199d85f4222SDaniel P. Berrange "options specify '%s'", encryptfmt);
200d85f4222SDaniel P. Berrange ret = -EINVAL;
201d85f4222SDaniel P. Berrange goto fail;
202d85f4222SDaniel P. Berrange }
203796d3239SMarkus Armbruster qdict_put_str(encryptopts, "format", "qcow");
204796d3239SMarkus Armbruster crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
205d85f4222SDaniel P. Berrange if (!crypto_opts) {
206d85f4222SDaniel P. Berrange ret = -EINVAL;
207d85f4222SDaniel P. Berrange goto fail;
208d85f4222SDaniel P. Berrange }
209e6ff69bfSDaniel P. Berrange
210d85f4222SDaniel P. Berrange if (flags & BDRV_O_NO_IO) {
211d85f4222SDaniel P. Berrange cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
212d85f4222SDaniel P. Berrange }
2131cd9a787SDaniel P. Berrange s->crypto = qcrypto_block_open(crypto_opts, "encrypt.",
2143ab0f063SStefan Hajnoczi NULL, NULL, cflags, errp);
215d85f4222SDaniel P. Berrange if (!s->crypto) {
216d85f4222SDaniel P. Berrange ret = -EINVAL;
217d85f4222SDaniel P. Berrange goto fail;
218d85f4222SDaniel P. Berrange }
219d85f4222SDaniel P. Berrange } else {
220d85f4222SDaniel P. Berrange error_setg(errp, "invalid encryption method in qcow header");
221d85f4222SDaniel P. Berrange ret = -EINVAL;
222d85f4222SDaniel P. Berrange goto fail;
223d85f4222SDaniel P. Berrange }
22454115412SEric Blake bs->encrypted = true;
225c01c214bSDaniel P. Berrange } else {
226c01c214bSDaniel P. Berrange if (encryptfmt) {
227c01c214bSDaniel P. Berrange error_setg(errp, "No encryption in image header, but options "
228c01c214bSDaniel P. Berrange "specified format '%s'", encryptfmt);
229c01c214bSDaniel P. Berrange ret = -EINVAL;
230c01c214bSDaniel P. Berrange goto fail;
231c01c214bSDaniel P. Berrange }
23284b0ec02SLi Zhi Hui }
233019d6b8fSAnthony Liguori s->cluster_bits = header.cluster_bits;
234019d6b8fSAnthony Liguori s->cluster_size = 1 << s->cluster_bits;
235019d6b8fSAnthony Liguori s->l2_bits = header.l2_bits;
236019d6b8fSAnthony Liguori s->l2_size = 1 << s->l2_bits;
237019d6b8fSAnthony Liguori bs->total_sectors = header.size / 512;
238019d6b8fSAnthony Liguori s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
239019d6b8fSAnthony Liguori
240019d6b8fSAnthony Liguori /* read the level 1 table */
241019d6b8fSAnthony Liguori shift = s->cluster_bits + s->l2_bits;
24246485de0SKevin Wolf if (header.size > UINT64_MAX - (1LL << shift)) {
24346485de0SKevin Wolf error_setg(errp, "Image too large");
24446485de0SKevin Wolf ret = -EINVAL;
24546485de0SKevin Wolf goto fail;
24646485de0SKevin Wolf } else {
24746485de0SKevin Wolf uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
24846485de0SKevin Wolf if (l1_size > INT_MAX / sizeof(uint64_t)) {
24946485de0SKevin Wolf error_setg(errp, "Image too large");
25046485de0SKevin Wolf ret = -EINVAL;
25146485de0SKevin Wolf goto fail;
25246485de0SKevin Wolf }
25346485de0SKevin Wolf s->l1_size = l1_size;
25446485de0SKevin Wolf }
255019d6b8fSAnthony Liguori
256019d6b8fSAnthony Liguori s->l1_table_offset = header.l1_table_offset;
2575839e53bSMarkus Armbruster s->l1_table = g_try_new(uint64_t, s->l1_size);
2580df93305SKevin Wolf if (s->l1_table == NULL) {
2590df93305SKevin Wolf error_setg(errp, "Could not allocate memory for L1 table");
2600df93305SKevin Wolf ret = -ENOMEM;
2610df93305SKevin Wolf goto fail;
2620df93305SKevin Wolf }
26384b0ec02SLi Zhi Hui
26432cc71deSAlberto Faria ret = bdrv_pread(bs->file, s->l1_table_offset,
26532cc71deSAlberto Faria s->l1_size * sizeof(uint64_t), s->l1_table, 0);
26684b0ec02SLi Zhi Hui if (ret < 0) {
267019d6b8fSAnthony Liguori goto fail;
26884b0ec02SLi Zhi Hui }
26984b0ec02SLi Zhi Hui
270019d6b8fSAnthony Liguori for(i = 0;i < s->l1_size; i++) {
271a5fdff18SPeter Maydell s->l1_table[i] = be64_to_cpu(s->l1_table[i]);
272019d6b8fSAnthony Liguori }
2730df93305SKevin Wolf
2740df93305SKevin Wolf /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
2750df93305SKevin Wolf s->l2_cache =
2769a4f4c31SKevin Wolf qemu_try_blockalign(bs->file->bs,
2770df93305SKevin Wolf s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
2780df93305SKevin Wolf if (s->l2_cache == NULL) {
2790df93305SKevin Wolf error_setg(errp, "Could not allocate L2 table cache");
2800df93305SKevin Wolf ret = -ENOMEM;
2810df93305SKevin Wolf goto fail;
2820df93305SKevin Wolf }
2837267c094SAnthony Liguori s->cluster_cache = g_malloc(s->cluster_size);
2847267c094SAnthony Liguori s->cluster_data = g_malloc(s->cluster_size);
285019d6b8fSAnthony Liguori s->cluster_cache_offset = -1;
286019d6b8fSAnthony Liguori
287019d6b8fSAnthony Liguori /* read the backing file name */
288019d6b8fSAnthony Liguori if (header.backing_file_offset != 0) {
289019d6b8fSAnthony Liguori len = header.backing_file_size;
290e729fa6aSJeff Cody if (len > 1023 || len >= sizeof(bs->backing_file)) {
291d66e5ceeSKevin Wolf error_setg(errp, "Backing file name too long");
292d66e5ceeSKevin Wolf ret = -EINVAL;
293d66e5ceeSKevin Wolf goto fail;
29484b0ec02SLi Zhi Hui }
29532cc71deSAlberto Faria ret = bdrv_pread(bs->file, header.backing_file_offset, len,
29632cc71deSAlberto Faria bs->auto_backing_file, 0);
29784b0ec02SLi Zhi Hui if (ret < 0) {
298019d6b8fSAnthony Liguori goto fail;
29984b0ec02SLi Zhi Hui }
300998c2019SMax Reitz bs->auto_backing_file[len] = '\0';
301998c2019SMax Reitz pstrcpy(bs->backing_file, sizeof(bs->backing_file),
302998c2019SMax Reitz bs->auto_backing_file);
303019d6b8fSAnthony Liguori }
304de33b1f3SScott Wood
305fd9f102cSKevin Wolf /* Disable migration when qcow images are used */
30681e5f78aSAlberto Garcia error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
30781e5f78aSAlberto Garcia "does not support live migration",
30881e5f78aSAlberto Garcia bdrv_get_device_or_node_name(bs));
3094026f1c4SKevin Wolf
310e0ee3a8fSSteve Sistare ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
311386f6c07SMarkus Armbruster if (ret < 0) {
312fe44dc91SAshijeet Acharya goto fail;
313fe44dc91SAshijeet Acharya }
314fd9f102cSKevin Wolf
315cb3e7f08SMarc-André Lureau qobject_unref(encryptopts);
316d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockOpenOptions(crypto_opts);
317de33b1f3SScott Wood qemu_co_mutex_init(&s->lock);
318a4b740dbSKevin Wolf bdrv_graph_rdunlock_main_loop();
319019d6b8fSAnthony Liguori return 0;
320019d6b8fSAnthony Liguori
321019d6b8fSAnthony Liguori fail:
322a4b740dbSKevin Wolf bdrv_graph_rdunlock_main_loop();
323a4b740dbSKevin Wolf fail_unlocked:
3247267c094SAnthony Liguori g_free(s->l1_table);
3250df93305SKevin Wolf qemu_vfree(s->l2_cache);
3267267c094SAnthony Liguori g_free(s->cluster_cache);
3277267c094SAnthony Liguori g_free(s->cluster_data);
328d85f4222SDaniel P. Berrange qcrypto_block_free(s->crypto);
329cb3e7f08SMarc-André Lureau qobject_unref(encryptopts);
330d85f4222SDaniel P. Berrange qapi_free_QCryptoBlockOpenOptions(crypto_opts);
33184b0ec02SLi Zhi Hui return ret;
332019d6b8fSAnthony Liguori }
333019d6b8fSAnthony Liguori
334d177692eSJeff Cody
335d177692eSJeff Cody /* We have nothing to do for QCOW reopen, stubs just return
336d177692eSJeff Cody * success */
qcow_reopen_prepare(BDRVReopenState * state,BlockReopenQueue * queue,Error ** errp)337d177692eSJeff Cody static int qcow_reopen_prepare(BDRVReopenState *state,
338d177692eSJeff Cody BlockReopenQueue *queue, Error **errp)
339d177692eSJeff Cody {
340d177692eSJeff Cody return 0;
341d177692eSJeff Cody }
342d177692eSJeff Cody
343019d6b8fSAnthony Liguori
344019d6b8fSAnthony Liguori /* 'allocate' is:
345019d6b8fSAnthony Liguori *
346019d6b8fSAnthony Liguori * 0 to not allocate.
347019d6b8fSAnthony Liguori *
348787993a5SEric Blake * 1 to allocate a normal cluster (for sector-aligned byte offsets 'n_start'
349787993a5SEric Blake * to 'n_end' within the cluster)
350019d6b8fSAnthony Liguori *
351019d6b8fSAnthony Liguori * 2 to allocate a compressed cluster of size
352019d6b8fSAnthony Liguori * 'compressed_size'. 'compressed_size' must be > 0 and <
353019d6b8fSAnthony Liguori * cluster_size
354019d6b8fSAnthony Liguori *
35556439e9dSEric Blake * return 0 if not allocated, 1 if *result is assigned, and negative
35656439e9dSEric Blake * errno on failure.
357019d6b8fSAnthony Liguori */
358c2b8e315SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
get_cluster_offset(BlockDriverState * bs,uint64_t offset,int allocate,int compressed_size,int n_start,int n_end,uint64_t * result)359c2b8e315SKevin Wolf get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate,
360c2b8e315SKevin Wolf int compressed_size, int n_start, int n_end,
361ea4b8014SPaolo Bonzini uint64_t *result)
362019d6b8fSAnthony Liguori {
363019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
36456439e9dSEric Blake int min_index, i, j, l1_index, l2_index, ret;
365d7a753a1SEric Blake int64_t l2_offset;
366d7a753a1SEric Blake uint64_t *l2_table, cluster_offset, tmp;
367019d6b8fSAnthony Liguori uint32_t min_count;
368019d6b8fSAnthony Liguori int new_l2_table;
369019d6b8fSAnthony Liguori
37056439e9dSEric Blake *result = 0;
371019d6b8fSAnthony Liguori l1_index = offset >> (s->l2_bits + s->cluster_bits);
372019d6b8fSAnthony Liguori l2_offset = s->l1_table[l1_index];
373019d6b8fSAnthony Liguori new_l2_table = 0;
374019d6b8fSAnthony Liguori if (!l2_offset) {
375019d6b8fSAnthony Liguori if (!allocate)
376019d6b8fSAnthony Liguori return 0;
377019d6b8fSAnthony Liguori /* allocate a new l2 entry */
3780af02bd1SPaolo Bonzini l2_offset = bdrv_co_getlength(bs->file->bs);
379d7a753a1SEric Blake if (l2_offset < 0) {
380d7a753a1SEric Blake return l2_offset;
381d7a753a1SEric Blake }
382019d6b8fSAnthony Liguori /* round to cluster size */
383d7a753a1SEric Blake l2_offset = QEMU_ALIGN_UP(l2_offset, s->cluster_size);
384019d6b8fSAnthony Liguori /* update the L1 entry */
385019d6b8fSAnthony Liguori s->l1_table[l1_index] = l2_offset;
386019d6b8fSAnthony Liguori tmp = cpu_to_be64(l2_offset);
38717362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_L1_UPDATE);
38858684155SAlberto Faria ret = bdrv_co_pwrite_sync(bs->file,
3895e5557d9SKevin Wolf s->l1_table_offset + l1_index * sizeof(tmp),
39032cc71deSAlberto Faria sizeof(tmp), &tmp, 0);
39156439e9dSEric Blake if (ret < 0) {
39256439e9dSEric Blake return ret;
39356439e9dSEric Blake }
394019d6b8fSAnthony Liguori new_l2_table = 1;
395019d6b8fSAnthony Liguori }
396019d6b8fSAnthony Liguori for(i = 0; i < L2_CACHE_SIZE; i++) {
397019d6b8fSAnthony Liguori if (l2_offset == s->l2_cache_offsets[i]) {
398019d6b8fSAnthony Liguori /* increment the hit count */
399019d6b8fSAnthony Liguori if (++s->l2_cache_counts[i] == 0xffffffff) {
400019d6b8fSAnthony Liguori for(j = 0; j < L2_CACHE_SIZE; j++) {
401019d6b8fSAnthony Liguori s->l2_cache_counts[j] >>= 1;
402019d6b8fSAnthony Liguori }
403019d6b8fSAnthony Liguori }
404019d6b8fSAnthony Liguori l2_table = s->l2_cache + (i << s->l2_bits);
405019d6b8fSAnthony Liguori goto found;
406019d6b8fSAnthony Liguori }
407019d6b8fSAnthony Liguori }
408019d6b8fSAnthony Liguori /* not found: load a new entry in the least used one */
409019d6b8fSAnthony Liguori min_index = 0;
410019d6b8fSAnthony Liguori min_count = 0xffffffff;
411019d6b8fSAnthony Liguori for(i = 0; i < L2_CACHE_SIZE; i++) {
412019d6b8fSAnthony Liguori if (s->l2_cache_counts[i] < min_count) {
413019d6b8fSAnthony Liguori min_count = s->l2_cache_counts[i];
414019d6b8fSAnthony Liguori min_index = i;
415019d6b8fSAnthony Liguori }
416019d6b8fSAnthony Liguori }
417019d6b8fSAnthony Liguori l2_table = s->l2_cache + (min_index << s->l2_bits);
41817362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_LOAD);
419019d6b8fSAnthony Liguori if (new_l2_table) {
420019d6b8fSAnthony Liguori memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
42158684155SAlberto Faria ret = bdrv_co_pwrite_sync(bs->file, l2_offset,
42232cc71deSAlberto Faria s->l2_size * sizeof(uint64_t), l2_table, 0);
42356439e9dSEric Blake if (ret < 0) {
42456439e9dSEric Blake return ret;
42556439e9dSEric Blake }
426019d6b8fSAnthony Liguori } else {
42758684155SAlberto Faria ret = bdrv_co_pread(bs->file, l2_offset,
42858684155SAlberto Faria s->l2_size * sizeof(uint64_t), l2_table, 0);
42956439e9dSEric Blake if (ret < 0) {
43056439e9dSEric Blake return ret;
43156439e9dSEric Blake }
432019d6b8fSAnthony Liguori }
433019d6b8fSAnthony Liguori s->l2_cache_offsets[min_index] = l2_offset;
434019d6b8fSAnthony Liguori s->l2_cache_counts[min_index] = 1;
435019d6b8fSAnthony Liguori found:
436019d6b8fSAnthony Liguori l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
437019d6b8fSAnthony Liguori cluster_offset = be64_to_cpu(l2_table[l2_index]);
438019d6b8fSAnthony Liguori if (!cluster_offset ||
439019d6b8fSAnthony Liguori ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
440019d6b8fSAnthony Liguori if (!allocate)
441019d6b8fSAnthony Liguori return 0;
44217362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
443787993a5SEric Blake assert(QEMU_IS_ALIGNED(n_start | n_end, BDRV_SECTOR_SIZE));
444019d6b8fSAnthony Liguori /* allocate a new cluster */
445019d6b8fSAnthony Liguori if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
446787993a5SEric Blake (n_end - n_start) < s->cluster_size) {
447019d6b8fSAnthony Liguori /* if the cluster is already compressed, we must
448019d6b8fSAnthony Liguori decompress it in the case it is not completely
449019d6b8fSAnthony Liguori overwritten */
45056439e9dSEric Blake if (decompress_cluster(bs, cluster_offset) < 0) {
45156439e9dSEric Blake return -EIO;
45256439e9dSEric Blake }
4530af02bd1SPaolo Bonzini cluster_offset = bdrv_co_getlength(bs->file->bs);
454d7a753a1SEric Blake if ((int64_t) cluster_offset < 0) {
455d7a753a1SEric Blake return cluster_offset;
456d7a753a1SEric Blake }
457d7a753a1SEric Blake cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size);
458019d6b8fSAnthony Liguori /* write the cluster content */
45917362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
46058684155SAlberto Faria ret = bdrv_co_pwrite(bs->file, cluster_offset, s->cluster_size,
46132cc71deSAlberto Faria s->cluster_cache, 0);
46256439e9dSEric Blake if (ret < 0) {
46356439e9dSEric Blake return ret;
46456439e9dSEric Blake }
465019d6b8fSAnthony Liguori } else {
4660af02bd1SPaolo Bonzini cluster_offset = bdrv_co_getlength(bs->file->bs);
467d7a753a1SEric Blake if ((int64_t) cluster_offset < 0) {
468d7a753a1SEric Blake return cluster_offset;
469d7a753a1SEric Blake }
470019d6b8fSAnthony Liguori if (allocate == 1) {
471019d6b8fSAnthony Liguori /* round to cluster size */
472d7a753a1SEric Blake cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size);
473d7a753a1SEric Blake if (cluster_offset + s->cluster_size > INT64_MAX) {
474d7a753a1SEric Blake return -E2BIG;
475d7a753a1SEric Blake }
47658684155SAlberto Faria ret = bdrv_co_truncate(bs->file,
47758684155SAlberto Faria cluster_offset + s->cluster_size,
4787b8e4857SKevin Wolf false, PREALLOC_MODE_OFF, 0, NULL);
479d7a753a1SEric Blake if (ret < 0) {
480d7a753a1SEric Blake return ret;
481d7a753a1SEric Blake }
482019d6b8fSAnthony Liguori /* if encrypted, we must initialize the cluster
483019d6b8fSAnthony Liguori content which won't be written */
4848336aafaSDaniel P. Berrange if (bs->encrypted &&
485787993a5SEric Blake (n_end - n_start) < s->cluster_size) {
486787993a5SEric Blake uint64_t start_offset;
487d85f4222SDaniel P. Berrange assert(s->crypto);
488787993a5SEric Blake start_offset = offset & ~(s->cluster_size - 1);
489787993a5SEric Blake for (i = 0; i < s->cluster_size; i += BDRV_SECTOR_SIZE) {
490019d6b8fSAnthony Liguori if (i < n_start || i >= n_end) {
491787993a5SEric Blake memset(s->cluster_data, 0x00, BDRV_SECTOR_SIZE);
4924609742aSDaniel P. Berrange if (qcrypto_block_encrypt(s->crypto,
493787993a5SEric Blake start_offset + i,
494d85f4222SDaniel P. Berrange s->cluster_data,
495d85f4222SDaniel P. Berrange BDRV_SECTOR_SIZE,
496c3a8fe33SAlberto Garcia NULL) < 0) {
49756439e9dSEric Blake return -EIO;
498f6fa64f6SDaniel P. Berrange }
49917362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
50058684155SAlberto Faria ret = bdrv_co_pwrite(bs->file, cluster_offset + i,
50132cc71deSAlberto Faria BDRV_SECTOR_SIZE,
50232cc71deSAlberto Faria s->cluster_data, 0);
50356439e9dSEric Blake if (ret < 0) {
50456439e9dSEric Blake return ret;
50556439e9dSEric Blake }
506019d6b8fSAnthony Liguori }
507019d6b8fSAnthony Liguori }
508019d6b8fSAnthony Liguori }
509019d6b8fSAnthony Liguori } else if (allocate == 2) {
510019d6b8fSAnthony Liguori cluster_offset |= QCOW_OFLAG_COMPRESSED |
511019d6b8fSAnthony Liguori (uint64_t)compressed_size << (63 - s->cluster_bits);
512019d6b8fSAnthony Liguori }
513019d6b8fSAnthony Liguori }
514019d6b8fSAnthony Liguori /* update L2 table */
515019d6b8fSAnthony Liguori tmp = cpu_to_be64(cluster_offset);
516019d6b8fSAnthony Liguori l2_table[l2_index] = tmp;
5170abb1475SMax Reitz if (allocate == 2) {
51817362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
5190abb1475SMax Reitz } else {
52017362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_UPDATE);
5210abb1475SMax Reitz }
52258684155SAlberto Faria ret = bdrv_co_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
52332cc71deSAlberto Faria sizeof(tmp), &tmp, 0);
52456439e9dSEric Blake if (ret < 0) {
52556439e9dSEric Blake return ret;
526019d6b8fSAnthony Liguori }
52756439e9dSEric Blake }
52856439e9dSEric Blake *result = cluster_offset;
52956439e9dSEric Blake return 1;
530019d6b8fSAnthony Liguori }
531019d6b8fSAnthony Liguori
5327ff9579eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qcow_co_block_status(BlockDriverState * bs,bool want_zero,int64_t offset,int64_t bytes,int64_t * pnum,int64_t * map,BlockDriverState ** file)5337ff9579eSKevin Wolf qcow_co_block_status(BlockDriverState *bs, bool want_zero,
5347ff9579eSKevin Wolf int64_t offset, int64_t bytes, int64_t *pnum,
5357ff9579eSKevin Wolf int64_t *map, BlockDriverState **file)
536019d6b8fSAnthony Liguori {
537019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
538d63b4c93SEric Blake int index_in_cluster, ret;
539d63b4c93SEric Blake int64_t n;
540019d6b8fSAnthony Liguori uint64_t cluster_offset;
541019d6b8fSAnthony Liguori
542f8a2e5e3SStefan Hajnoczi qemu_co_mutex_lock(&s->lock);
543d63b4c93SEric Blake ret = get_cluster_offset(bs, offset, 0, 0, 0, 0, &cluster_offset);
544f8a2e5e3SStefan Hajnoczi qemu_co_mutex_unlock(&s->lock);
54556439e9dSEric Blake if (ret < 0) {
54656439e9dSEric Blake return ret;
54756439e9dSEric Blake }
548d63b4c93SEric Blake index_in_cluster = offset & (s->cluster_size - 1);
549d63b4c93SEric Blake n = s->cluster_size - index_in_cluster;
550d63b4c93SEric Blake if (n > bytes) {
551d63b4c93SEric Blake n = bytes;
552d63b4c93SEric Blake }
553019d6b8fSAnthony Liguori *pnum = n;
5544bc74be9SPaolo Bonzini if (!cluster_offset) {
5554bc74be9SPaolo Bonzini return 0;
5564bc74be9SPaolo Bonzini }
55728482891SAndrey Drobyshev via if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
55828482891SAndrey Drobyshev via return BDRV_BLOCK_DATA | BDRV_BLOCK_COMPRESSED;
55928482891SAndrey Drobyshev via }
56028482891SAndrey Drobyshev via if (s->crypto) {
5614bc74be9SPaolo Bonzini return BDRV_BLOCK_DATA;
5624bc74be9SPaolo Bonzini }
563d63b4c93SEric Blake *map = cluster_offset | index_in_cluster;
5643064bf6fSFam Zheng *file = bs->file->bs;
565d63b4c93SEric Blake return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
566019d6b8fSAnthony Liguori }
567019d6b8fSAnthony Liguori
decompress_buffer(uint8_t * out_buf,int out_buf_size,const uint8_t * buf,int buf_size)568019d6b8fSAnthony Liguori static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
569019d6b8fSAnthony Liguori const uint8_t *buf, int buf_size)
570019d6b8fSAnthony Liguori {
571019d6b8fSAnthony Liguori z_stream strm1, *strm = &strm1;
572019d6b8fSAnthony Liguori int ret, out_len;
573019d6b8fSAnthony Liguori
574019d6b8fSAnthony Liguori memset(strm, 0, sizeof(*strm));
575019d6b8fSAnthony Liguori
576019d6b8fSAnthony Liguori strm->next_in = (uint8_t *)buf;
577019d6b8fSAnthony Liguori strm->avail_in = buf_size;
578019d6b8fSAnthony Liguori strm->next_out = out_buf;
579019d6b8fSAnthony Liguori strm->avail_out = out_buf_size;
580019d6b8fSAnthony Liguori
581019d6b8fSAnthony Liguori ret = inflateInit2(strm, -12);
582019d6b8fSAnthony Liguori if (ret != Z_OK)
583019d6b8fSAnthony Liguori return -1;
584019d6b8fSAnthony Liguori ret = inflate(strm, Z_FINISH);
585019d6b8fSAnthony Liguori out_len = strm->next_out - out_buf;
586019d6b8fSAnthony Liguori if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
587019d6b8fSAnthony Liguori out_len != out_buf_size) {
588019d6b8fSAnthony Liguori inflateEnd(strm);
589019d6b8fSAnthony Liguori return -1;
590019d6b8fSAnthony Liguori }
591019d6b8fSAnthony Liguori inflateEnd(strm);
592019d6b8fSAnthony Liguori return 0;
593019d6b8fSAnthony Liguori }
594019d6b8fSAnthony Liguori
595b9b10c35SKevin Wolf static int coroutine_fn GRAPH_RDLOCK
decompress_cluster(BlockDriverState * bs,uint64_t cluster_offset)596b9b10c35SKevin Wolf decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
597019d6b8fSAnthony Liguori {
59866f82ceeSKevin Wolf BDRVQcowState *s = bs->opaque;
599019d6b8fSAnthony Liguori int ret, csize;
600019d6b8fSAnthony Liguori uint64_t coffset;
601019d6b8fSAnthony Liguori
602019d6b8fSAnthony Liguori coffset = cluster_offset & s->cluster_offset_mask;
603019d6b8fSAnthony Liguori if (s->cluster_cache_offset != coffset) {
604019d6b8fSAnthony Liguori csize = cluster_offset >> (63 - s->cluster_bits);
605019d6b8fSAnthony Liguori csize &= (s->cluster_size - 1);
60617362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
60758684155SAlberto Faria ret = bdrv_co_pread(bs->file, coffset, csize, s->cluster_data, 0);
608353a5d84SAlberto Faria if (ret < 0)
609019d6b8fSAnthony Liguori return -1;
610019d6b8fSAnthony Liguori if (decompress_buffer(s->cluster_cache, s->cluster_size,
611019d6b8fSAnthony Liguori s->cluster_data, csize) < 0) {
612019d6b8fSAnthony Liguori return -1;
613019d6b8fSAnthony Liguori }
614019d6b8fSAnthony Liguori s->cluster_cache_offset = coffset;
615019d6b8fSAnthony Liguori }
616019d6b8fSAnthony Liguori return 0;
617019d6b8fSAnthony Liguori }
618019d6b8fSAnthony Liguori
qcow_refresh_limits(BlockDriverState * bs,Error ** errp)619609841a3SEric Blake static void qcow_refresh_limits(BlockDriverState *bs, Error **errp)
620609841a3SEric Blake {
621609841a3SEric Blake /* At least encrypted images require 512-byte alignment. Apply the
622609841a3SEric Blake * limit universally, rather than just on encrypted images, as
623609841a3SEric Blake * it's easier to let the block layer handle rounding than to
624609841a3SEric Blake * audit this code further. */
625609841a3SEric Blake bs->bl.request_alignment = BDRV_SECTOR_SIZE;
626609841a3SEric Blake }
627609841a3SEric Blake
6287b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qcow_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)6297b1fb72eSKevin Wolf qcow_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
6307b1fb72eSKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
631ad53089bSChristoph Hellwig {
632019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
633a15312b0SEric Blake int offset_in_cluster;
63427deebe8SFrediano Ziglio int ret = 0, n;
63543ca85b5SFrediano Ziglio uint64_t cluster_offset;
63627deebe8SFrediano Ziglio uint8_t *buf;
63727deebe8SFrediano Ziglio void *orig_buf;
638019d6b8fSAnthony Liguori
63927deebe8SFrediano Ziglio if (qiov->niov > 1) {
6400df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
6410df93305SKevin Wolf if (buf == NULL) {
6420df93305SKevin Wolf return -ENOMEM;
6430df93305SKevin Wolf }
64427deebe8SFrediano Ziglio } else {
64527deebe8SFrediano Ziglio orig_buf = NULL;
64627deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base;
647019d6b8fSAnthony Liguori }
648019d6b8fSAnthony Liguori
64927deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock);
65027deebe8SFrediano Ziglio
651a15312b0SEric Blake while (bytes != 0) {
65243ca85b5SFrediano Ziglio /* prepare next request */
653a15312b0SEric Blake ret = get_cluster_offset(bs, offset, 0, 0, 0, 0, &cluster_offset);
65456439e9dSEric Blake if (ret < 0) {
65556439e9dSEric Blake break;
65656439e9dSEric Blake }
657a15312b0SEric Blake offset_in_cluster = offset & (s->cluster_size - 1);
658a15312b0SEric Blake n = s->cluster_size - offset_in_cluster;
659a15312b0SEric Blake if (n > bytes) {
660a15312b0SEric Blake n = bytes;
661430bbaaaSFrediano Ziglio }
662019d6b8fSAnthony Liguori
663430bbaaaSFrediano Ziglio if (!cluster_offset) {
664760e0063SKevin Wolf if (bs->backing) {
665019d6b8fSAnthony Liguori /* read from the base image */
66652b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock);
6670abb1475SMax Reitz /* qcow2 emits this on bs->file instead of bs->backing */
66817362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
6694ed3e0c4SVladimir Sementsov-Ogievskiy ret = bdrv_co_pread(bs->backing, offset, n, buf, 0);
67052b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock);
67152b8eb60SKevin Wolf if (ret < 0) {
67256439e9dSEric Blake break;
6735614c188SStefan Weil }
674019d6b8fSAnthony Liguori } else {
675019d6b8fSAnthony Liguori /* Note: in this case, no need to wait */
676a15312b0SEric Blake memset(buf, 0, n);
677019d6b8fSAnthony Liguori }
678430bbaaaSFrediano Ziglio } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
679019d6b8fSAnthony Liguori /* add AIO support for compressed blocks ? */
680430bbaaaSFrediano Ziglio if (decompress_cluster(bs, cluster_offset) < 0) {
68156439e9dSEric Blake ret = -EIO;
68256439e9dSEric Blake break;
6835614c188SStefan Weil }
684a15312b0SEric Blake memcpy(buf, s->cluster_cache + offset_in_cluster, n);
685019d6b8fSAnthony Liguori } else {
686430bbaaaSFrediano Ziglio if ((cluster_offset & 511) != 0) {
68756439e9dSEric Blake ret = -EIO;
68856439e9dSEric Blake break;
689019d6b8fSAnthony Liguori }
69052b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock);
69117362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO);
6924ed3e0c4SVladimir Sementsov-Ogievskiy ret = bdrv_co_pread(bs->file, cluster_offset + offset_in_cluster,
6934ed3e0c4SVladimir Sementsov-Ogievskiy n, buf, 0);
69452b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock);
69552b8eb60SKevin Wolf if (ret < 0) {
69627deebe8SFrediano Ziglio break;
697019d6b8fSAnthony Liguori }
6988336aafaSDaniel P. Berrange if (bs->encrypted) {
699d85f4222SDaniel P. Berrange assert(s->crypto);
7004609742aSDaniel P. Berrange if (qcrypto_block_decrypt(s->crypto,
701a15312b0SEric Blake offset, buf, n, NULL) < 0) {
70256439e9dSEric Blake ret = -EIO;
70356439e9dSEric Blake break;
704f6fa64f6SDaniel P. Berrange }
70543ca85b5SFrediano Ziglio }
70643ca85b5SFrediano Ziglio }
70727deebe8SFrediano Ziglio ret = 0;
70843ca85b5SFrediano Ziglio
709a15312b0SEric Blake bytes -= n;
710a15312b0SEric Blake offset += n;
711a15312b0SEric Blake buf += n;
71252b8eb60SKevin Wolf }
713019d6b8fSAnthony Liguori
71452b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock);
71552b8eb60SKevin Wolf
71627deebe8SFrediano Ziglio if (qiov->niov > 1) {
71703396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size);
71827deebe8SFrediano Ziglio qemu_vfree(orig_buf);
719019d6b8fSAnthony Liguori }
72052b8eb60SKevin Wolf
72152b8eb60SKevin Wolf return ret;
722019d6b8fSAnthony Liguori }
723019d6b8fSAnthony Liguori
7247b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qcow_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)7257b1fb72eSKevin Wolf qcow_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
7267b1fb72eSKevin Wolf QEMUIOVector *qiov, BdrvRequestFlags flags)
727019d6b8fSAnthony Liguori {
728019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
729d1326a78SEric Blake int offset_in_cluster;
730019d6b8fSAnthony Liguori uint64_t cluster_offset;
73127deebe8SFrediano Ziglio int ret = 0, n;
73227deebe8SFrediano Ziglio uint8_t *buf;
73327deebe8SFrediano Ziglio void *orig_buf;
734019d6b8fSAnthony Liguori
73527deebe8SFrediano Ziglio s->cluster_cache_offset = -1; /* disable compressed cache */
73627deebe8SFrediano Ziglio
7371fad1f94SDaniel P. Berrange /* We must always copy the iov when encrypting, so we
7381fad1f94SDaniel P. Berrange * don't modify the original data buffer during encryption */
7391fad1f94SDaniel P. Berrange if (bs->encrypted || qiov->niov > 1) {
7400df93305SKevin Wolf buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
7410df93305SKevin Wolf if (buf == NULL) {
7420df93305SKevin Wolf return -ENOMEM;
7430df93305SKevin Wolf }
744d5e6b161SMichael Tokarev qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
74527deebe8SFrediano Ziglio } else {
74627deebe8SFrediano Ziglio orig_buf = NULL;
74727deebe8SFrediano Ziglio buf = (uint8_t *)qiov->iov->iov_base;
748019d6b8fSAnthony Liguori }
749019d6b8fSAnthony Liguori
75027deebe8SFrediano Ziglio qemu_co_mutex_lock(&s->lock);
75127deebe8SFrediano Ziglio
752d1326a78SEric Blake while (bytes != 0) {
753d1326a78SEric Blake offset_in_cluster = offset & (s->cluster_size - 1);
754d1326a78SEric Blake n = s->cluster_size - offset_in_cluster;
755d1326a78SEric Blake if (n > bytes) {
756d1326a78SEric Blake n = bytes;
757430bbaaaSFrediano Ziglio }
758d1326a78SEric Blake ret = get_cluster_offset(bs, offset, 1, 0, offset_in_cluster,
759d1326a78SEric Blake offset_in_cluster + n, &cluster_offset);
76056439e9dSEric Blake if (ret < 0) {
76156439e9dSEric Blake break;
76256439e9dSEric Blake }
763019d6b8fSAnthony Liguori if (!cluster_offset || (cluster_offset & 511) != 0) {
76427deebe8SFrediano Ziglio ret = -EIO;
76527deebe8SFrediano Ziglio break;
766019d6b8fSAnthony Liguori }
7678336aafaSDaniel P. Berrange if (bs->encrypted) {
768d85f4222SDaniel P. Berrange assert(s->crypto);
769d1326a78SEric Blake if (qcrypto_block_encrypt(s->crypto, offset, buf, n, NULL) < 0) {
770f6fa64f6SDaniel P. Berrange ret = -EIO;
771f6fa64f6SDaniel P. Berrange break;
772f6fa64f6SDaniel P. Berrange }
773019d6b8fSAnthony Liguori }
774019d6b8fSAnthony Liguori
77552b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock);
77617362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
7774ed3e0c4SVladimir Sementsov-Ogievskiy ret = bdrv_co_pwrite(bs->file, cluster_offset + offset_in_cluster,
7784ed3e0c4SVladimir Sementsov-Ogievskiy n, buf, 0);
77952b8eb60SKevin Wolf qemu_co_mutex_lock(&s->lock);
78052b8eb60SKevin Wolf if (ret < 0) {
78127deebe8SFrediano Ziglio break;
7825614c188SStefan Weil }
78327deebe8SFrediano Ziglio ret = 0;
78443ca85b5SFrediano Ziglio
785d1326a78SEric Blake bytes -= n;
786d1326a78SEric Blake offset += n;
787d1326a78SEric Blake buf += n;
788019d6b8fSAnthony Liguori }
78952b8eb60SKevin Wolf qemu_co_mutex_unlock(&s->lock);
790019d6b8fSAnthony Liguori
79127deebe8SFrediano Ziglio qemu_vfree(orig_buf);
792b11a24deSKevin Wolf
79352b8eb60SKevin Wolf return ret;
794019d6b8fSAnthony Liguori }
795019d6b8fSAnthony Liguori
qcow_close(BlockDriverState * bs)796019d6b8fSAnthony Liguori static void qcow_close(BlockDriverState *bs)
797019d6b8fSAnthony Liguori {
798019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
799fd9f102cSKevin Wolf
800d85f4222SDaniel P. Berrange qcrypto_block_free(s->crypto);
801d85f4222SDaniel P. Berrange s->crypto = NULL;
8027267c094SAnthony Liguori g_free(s->l1_table);
8030df93305SKevin Wolf qemu_vfree(s->l2_cache);
8047267c094SAnthony Liguori g_free(s->cluster_cache);
8057267c094SAnthony Liguori g_free(s->cluster_data);
806fd9f102cSKevin Wolf
807c8a7fc51SSteve Sistare migrate_del_blocker(&s->migration_blocker);
808019d6b8fSAnthony Liguori }
809019d6b8fSAnthony Liguori
8104db7ba3bSKevin Wolf static int coroutine_fn GRAPH_UNLOCKED
qcow_co_create(BlockdevCreateOptions * opts,Error ** errp)8114db7ba3bSKevin Wolf qcow_co_create(BlockdevCreateOptions *opts, Error **errp)
812019d6b8fSAnthony Liguori {
81342a3e1abSKevin Wolf BlockdevCreateOptionsQcow *qcow_opts;
8142b16c9ffSLi Zhi Hui int header_size, backing_filename_len, l1_size, shift, i;
815019d6b8fSAnthony Liguori QCowHeader header;
8162b16c9ffSLi Zhi Hui uint8_t *tmp;
8170e7e1989SKevin Wolf int64_t total_size = 0;
8183e1a8134SKirill A. Shutemov int ret;
81942a3e1abSKevin Wolf BlockDriverState *bs;
8206af40160SKevin Wolf BlockBackend *qcow_blk;
821d85f4222SDaniel P. Berrange QCryptoBlock *crypto = NULL;
8220e7e1989SKevin Wolf
82342a3e1abSKevin Wolf assert(opts->driver == BLOCKDEV_DRIVER_QCOW);
82442a3e1abSKevin Wolf qcow_opts = &opts->u.qcow;
82542a3e1abSKevin Wolf
82642a3e1abSKevin Wolf /* Sanity checks */
82742a3e1abSKevin Wolf total_size = qcow_opts->size;
8286aa837f7SDaniel P. Berrange if (total_size == 0) {
8296aa837f7SDaniel P. Berrange error_setg(errp, "Image size is too small, cannot be zero length");
83042a3e1abSKevin Wolf return -EINVAL;
8316aa837f7SDaniel P. Berrange }
8326aa837f7SDaniel P. Berrange
83354fde4ffSMarkus Armbruster if (qcow_opts->encrypt &&
834*d23d2ef3SMarkus Armbruster qcow_opts->encrypt->format != QCRYPTO_BLOCK_FORMAT_QCOW)
83542a3e1abSKevin Wolf {
83642a3e1abSKevin Wolf error_setg(errp, "Unsupported encryption format");
83742a3e1abSKevin Wolf return -EINVAL;
8380e7e1989SKevin Wolf }
839019d6b8fSAnthony Liguori
84042a3e1abSKevin Wolf /* Create BlockBackend to write to the image */
8415b9d79b6SKevin Wolf bs = bdrv_co_open_blockdev_ref(qcow_opts->file, errp);
84242a3e1abSKevin Wolf if (bs == NULL) {
84342a3e1abSKevin Wolf return -EIO;
84442a3e1abSKevin Wolf }
84542a3e1abSKevin Wolf
8465b9d79b6SKevin Wolf qcow_blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE,
847a3aeeab5SEric Blake BLK_PERM_ALL, errp);
848a3aeeab5SEric Blake if (!qcow_blk) {
849a3aeeab5SEric Blake ret = -EPERM;
85042a3e1abSKevin Wolf goto exit;
8512b16c9ffSLi Zhi Hui }
8526af40160SKevin Wolf blk_set_allow_write_beyond_eof(qcow_blk, true);
8536af40160SKevin Wolf
85442a3e1abSKevin Wolf /* Create image format */
855019d6b8fSAnthony Liguori memset(&header, 0, sizeof(header));
856019d6b8fSAnthony Liguori header.magic = cpu_to_be32(QCOW_MAGIC);
857019d6b8fSAnthony Liguori header.version = cpu_to_be32(QCOW_VERSION);
858180e9526SHu Tao header.size = cpu_to_be64(total_size);
859019d6b8fSAnthony Liguori header_size = sizeof(header);
860019d6b8fSAnthony Liguori backing_filename_len = 0;
86154fde4ffSMarkus Armbruster if (qcow_opts->backing_file) {
86242a3e1abSKevin Wolf if (strcmp(qcow_opts->backing_file, "fat:")) {
863019d6b8fSAnthony Liguori header.backing_file_offset = cpu_to_be64(header_size);
86442a3e1abSKevin Wolf backing_filename_len = strlen(qcow_opts->backing_file);
865019d6b8fSAnthony Liguori header.backing_file_size = cpu_to_be32(backing_filename_len);
866019d6b8fSAnthony Liguori header_size += backing_filename_len;
867019d6b8fSAnthony Liguori } else {
868019d6b8fSAnthony Liguori /* special backing file for vvfat */
86954fde4ffSMarkus Armbruster qcow_opts->backing_file = NULL;
870019d6b8fSAnthony Liguori }
871019d6b8fSAnthony Liguori header.cluster_bits = 9; /* 512 byte cluster to avoid copying
872dc6fb73dSDeepak Kathayat unmodified sectors */
873019d6b8fSAnthony Liguori header.l2_bits = 12; /* 32 KB L2 tables */
874019d6b8fSAnthony Liguori } else {
875019d6b8fSAnthony Liguori header.cluster_bits = 12; /* 4 KB clusters */
876019d6b8fSAnthony Liguori header.l2_bits = 9; /* 4 KB L2 tables */
877019d6b8fSAnthony Liguori }
878019d6b8fSAnthony Liguori header_size = (header_size + 7) & ~7;
879019d6b8fSAnthony Liguori shift = header.cluster_bits + header.l2_bits;
880180e9526SHu Tao l1_size = (total_size + (1LL << shift) - 1) >> shift;
881019d6b8fSAnthony Liguori
882019d6b8fSAnthony Liguori header.l1_table_offset = cpu_to_be64(header_size);
883d85f4222SDaniel P. Berrange
88454fde4ffSMarkus Armbruster if (qcow_opts->encrypt) {
885019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
886d85f4222SDaniel P. Berrange
88742a3e1abSKevin Wolf crypto = qcrypto_block_create(qcow_opts->encrypt, "encrypt.",
888d74523a3SHyman Huang NULL, NULL, NULL, 0, errp);
889d85f4222SDaniel P. Berrange if (!crypto) {
890d85f4222SDaniel P. Berrange ret = -EINVAL;
891d85f4222SDaniel P. Berrange goto exit;
892d85f4222SDaniel P. Berrange }
893019d6b8fSAnthony Liguori } else {
894019d6b8fSAnthony Liguori header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
895019d6b8fSAnthony Liguori }
896019d6b8fSAnthony Liguori
897019d6b8fSAnthony Liguori /* write all the data */
89858684155SAlberto Faria ret = blk_co_pwrite(qcow_blk, 0, sizeof(header), &header, 0);
899bf5b16faSAlberto Faria if (ret < 0) {
9003e1a8134SKirill A. Shutemov goto exit;
9013e1a8134SKirill A. Shutemov }
9023e1a8134SKirill A. Shutemov
90354fde4ffSMarkus Armbruster if (qcow_opts->backing_file) {
90458684155SAlberto Faria ret = blk_co_pwrite(qcow_blk, sizeof(header), backing_filename_len,
905a9262f55SAlberto Faria qcow_opts->backing_file, 0);
906bf5b16faSAlberto Faria if (ret < 0) {
9073e1a8134SKirill A. Shutemov goto exit;
908019d6b8fSAnthony Liguori }
9093e1a8134SKirill A. Shutemov }
9103e1a8134SKirill A. Shutemov
9112b16c9ffSLi Zhi Hui tmp = g_malloc0(BDRV_SECTOR_SIZE);
912d737b78cSLaurent Vivier for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE);
913d737b78cSLaurent Vivier i++) {
91458684155SAlberto Faria ret = blk_co_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i,
915a9262f55SAlberto Faria BDRV_SECTOR_SIZE, tmp, 0);
916bf5b16faSAlberto Faria if (ret < 0) {
9172b16c9ffSLi Zhi Hui g_free(tmp);
9182b16c9ffSLi Zhi Hui goto exit;
9192b16c9ffSLi Zhi Hui }
9202b16c9ffSLi Zhi Hui }
9212b16c9ffSLi Zhi Hui
9222b16c9ffSLi Zhi Hui g_free(tmp);
9233e1a8134SKirill A. Shutemov ret = 0;
9243e1a8134SKirill A. Shutemov exit:
925b2ab5f54SKevin Wolf blk_co_unref(qcow_blk);
926b2ab5f54SKevin Wolf bdrv_co_unref(bs);
927d85f4222SDaniel P. Berrange qcrypto_block_free(crypto);
92842a3e1abSKevin Wolf return ret;
92942a3e1abSKevin Wolf }
93042a3e1abSKevin Wolf
9314db7ba3bSKevin Wolf static int coroutine_fn GRAPH_UNLOCKED
qcow_co_create_opts(BlockDriver * drv,const char * filename,QemuOpts * opts,Error ** errp)9324ec8df01SKevin Wolf qcow_co_create_opts(BlockDriver *drv, const char *filename,
93342a3e1abSKevin Wolf QemuOpts *opts, Error **errp)
93442a3e1abSKevin Wolf {
93542a3e1abSKevin Wolf BlockdevCreateOptions *create_options = NULL;
93642a3e1abSKevin Wolf BlockDriverState *bs = NULL;
937344acbd6SEric Blake QDict *qdict = NULL;
93842a3e1abSKevin Wolf Visitor *v;
93942a3e1abSKevin Wolf const char *val;
94042a3e1abSKevin Wolf int ret;
941344acbd6SEric Blake char *backing_fmt;
94242a3e1abSKevin Wolf
94342a3e1abSKevin Wolf static const QDictRenames opt_renames[] = {
94442a3e1abSKevin Wolf { BLOCK_OPT_BACKING_FILE, "backing-file" },
94542a3e1abSKevin Wolf { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT },
94642a3e1abSKevin Wolf { NULL, NULL },
94742a3e1abSKevin Wolf };
94842a3e1abSKevin Wolf
949344acbd6SEric Blake /*
950344acbd6SEric Blake * We can't actually store a backing format, but can check that
951344acbd6SEric Blake * the user's request made sense.
952344acbd6SEric Blake */
953344acbd6SEric Blake backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
954344acbd6SEric Blake if (backing_fmt && !bdrv_find_format(backing_fmt)) {
955344acbd6SEric Blake error_setg(errp, "unrecognized backing format '%s'", backing_fmt);
956344acbd6SEric Blake ret = -EINVAL;
957344acbd6SEric Blake goto fail;
958344acbd6SEric Blake }
959344acbd6SEric Blake
96042a3e1abSKevin Wolf /* Parse options and convert legacy syntax */
96142a3e1abSKevin Wolf qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qcow_create_opts, true);
96242a3e1abSKevin Wolf
96342a3e1abSKevin Wolf val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
96442a3e1abSKevin Wolf if (val && !strcmp(val, "on")) {
96542a3e1abSKevin Wolf qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
96642a3e1abSKevin Wolf } else if (val && !strcmp(val, "off")) {
96742a3e1abSKevin Wolf qdict_del(qdict, BLOCK_OPT_ENCRYPT);
96842a3e1abSKevin Wolf }
96942a3e1abSKevin Wolf
97042a3e1abSKevin Wolf val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
97142a3e1abSKevin Wolf if (val && !strcmp(val, "aes")) {
97242a3e1abSKevin Wolf qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
97342a3e1abSKevin Wolf }
97442a3e1abSKevin Wolf
97542a3e1abSKevin Wolf if (!qdict_rename_keys(qdict, opt_renames, errp)) {
97642a3e1abSKevin Wolf ret = -EINVAL;
97742a3e1abSKevin Wolf goto fail;
97842a3e1abSKevin Wolf }
97942a3e1abSKevin Wolf
98042a3e1abSKevin Wolf /* Create and open the file (protocol layer) */
9812475a0d0SEmanuele Giuseppe Esposito ret = bdrv_co_create_file(filename, opts, errp);
98242a3e1abSKevin Wolf if (ret < 0) {
98342a3e1abSKevin Wolf goto fail;
98442a3e1abSKevin Wolf }
98542a3e1abSKevin Wolf
9865b9d79b6SKevin Wolf bs = bdrv_co_open(filename, NULL, NULL,
98742a3e1abSKevin Wolf BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
98842a3e1abSKevin Wolf if (bs == NULL) {
98942a3e1abSKevin Wolf ret = -EIO;
99042a3e1abSKevin Wolf goto fail;
99142a3e1abSKevin Wolf }
99242a3e1abSKevin Wolf
99342a3e1abSKevin Wolf /* Now get the QAPI type BlockdevCreateOptions */
99442a3e1abSKevin Wolf qdict_put_str(qdict, "driver", "qcow");
99542a3e1abSKevin Wolf qdict_put_str(qdict, "file", bs->node_name);
99642a3e1abSKevin Wolf
997af91062eSMarkus Armbruster v = qobject_input_visitor_new_flat_confused(qdict, errp);
998af91062eSMarkus Armbruster if (!v) {
99942a3e1abSKevin Wolf ret = -EINVAL;
100042a3e1abSKevin Wolf goto fail;
100142a3e1abSKevin Wolf }
100242a3e1abSKevin Wolf
1003b11a093cSMarkus Armbruster visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
100442a3e1abSKevin Wolf visit_free(v);
1005b11a093cSMarkus Armbruster if (!create_options) {
100642a3e1abSKevin Wolf ret = -EINVAL;
100742a3e1abSKevin Wolf goto fail;
100842a3e1abSKevin Wolf }
100942a3e1abSKevin Wolf
101042a3e1abSKevin Wolf /* Silently round up size */
101142a3e1abSKevin Wolf assert(create_options->driver == BLOCKDEV_DRIVER_QCOW);
101242a3e1abSKevin Wolf create_options->u.qcow.size =
101342a3e1abSKevin Wolf ROUND_UP(create_options->u.qcow.size, BDRV_SECTOR_SIZE);
101442a3e1abSKevin Wolf
101542a3e1abSKevin Wolf /* Create the qcow image (format layer) */
101642a3e1abSKevin Wolf ret = qcow_co_create(create_options, errp);
101742a3e1abSKevin Wolf if (ret < 0) {
101842a3e1abSKevin Wolf goto fail;
101942a3e1abSKevin Wolf }
102042a3e1abSKevin Wolf
102142a3e1abSKevin Wolf ret = 0;
102242a3e1abSKevin Wolf fail:
1023344acbd6SEric Blake g_free(backing_fmt);
1024cb3e7f08SMarc-André Lureau qobject_unref(qdict);
1025b2ab5f54SKevin Wolf bdrv_co_unref(bs);
102642a3e1abSKevin Wolf qapi_free_BlockdevCreateOptions(create_options);
10273e1a8134SKirill A. Shutemov return ret;
1028019d6b8fSAnthony Liguori }
1029019d6b8fSAnthony Liguori
qcow_make_empty(BlockDriverState * bs)103079a55866SKevin Wolf static int GRAPH_RDLOCK qcow_make_empty(BlockDriverState *bs)
1031019d6b8fSAnthony Liguori {
1032019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
1033019d6b8fSAnthony Liguori uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1034019d6b8fSAnthony Liguori int ret;
1035019d6b8fSAnthony Liguori
1036019d6b8fSAnthony Liguori memset(s->l1_table, 0, l1_length);
103732cc71deSAlberto Faria if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, l1_length, s->l1_table,
103853fb7844SAlberto Faria 0) < 0)
1039019d6b8fSAnthony Liguori return -1;
1040c80d8b06SMax Reitz ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, false,
10417b8e4857SKevin Wolf PREALLOC_MODE_OFF, 0, NULL);
1042019d6b8fSAnthony Liguori if (ret < 0)
1043019d6b8fSAnthony Liguori return ret;
1044019d6b8fSAnthony Liguori
1045019d6b8fSAnthony Liguori memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
1046019d6b8fSAnthony Liguori memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
1047019d6b8fSAnthony Liguori memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
1048019d6b8fSAnthony Liguori
1049019d6b8fSAnthony Liguori return 0;
1050019d6b8fSAnthony Liguori }
1051019d6b8fSAnthony Liguori
1052019d6b8fSAnthony Liguori /* XXX: put compressed sectors first, then all the cluster aligned
1053019d6b8fSAnthony Liguori tables to avoid losing bytes in alignment */
10547b1fb72eSKevin Wolf static int coroutine_fn GRAPH_RDLOCK
qcow_co_pwritev_compressed(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov)1055e75abedaSVladimir Sementsov-Ogievskiy qcow_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
1056e75abedaSVladimir Sementsov-Ogievskiy QEMUIOVector *qiov)
1057019d6b8fSAnthony Liguori {
1058019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
1059019d6b8fSAnthony Liguori z_stream strm;
1060019d6b8fSAnthony Liguori int ret, out_len;
1061f2b95a12SPavel Butsykin uint8_t *buf, *out_buf;
1062019d6b8fSAnthony Liguori uint64_t cluster_offset;
1063019d6b8fSAnthony Liguori
1064f2b95a12SPavel Butsykin buf = qemu_blockalign(bs, s->cluster_size);
1065655923dfSPavel Butsykin if (bytes != s->cluster_size) {
1066655923dfSPavel Butsykin if (bytes > s->cluster_size ||
1067655923dfSPavel Butsykin offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
1068655923dfSPavel Butsykin {
1069655923dfSPavel Butsykin qemu_vfree(buf);
1070655923dfSPavel Butsykin return -EINVAL;
1071655923dfSPavel Butsykin }
1072655923dfSPavel Butsykin /* Zero-pad last write if image size is not cluster aligned */
1073655923dfSPavel Butsykin memset(buf + bytes, 0, s->cluster_size - bytes);
1074655923dfSPavel Butsykin }
1075f2b95a12SPavel Butsykin qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
1076019d6b8fSAnthony Liguori
1077ebf7bba0SVladimir Sementsov-Ogievskiy out_buf = g_malloc(s->cluster_size);
1078019d6b8fSAnthony Liguori
1079019d6b8fSAnthony Liguori /* best compression, small window, no zlib header */
1080019d6b8fSAnthony Liguori memset(&strm, 0, sizeof(strm));
1081019d6b8fSAnthony Liguori ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
1082019d6b8fSAnthony Liguori Z_DEFLATED, -12,
1083019d6b8fSAnthony Liguori 9, Z_DEFAULT_STRATEGY);
1084019d6b8fSAnthony Liguori if (ret != 0) {
108564ebe71aSKevin Wolf ret = -EINVAL;
108664ebe71aSKevin Wolf goto fail;
1087019d6b8fSAnthony Liguori }
1088019d6b8fSAnthony Liguori
1089019d6b8fSAnthony Liguori strm.avail_in = s->cluster_size;
1090019d6b8fSAnthony Liguori strm.next_in = (uint8_t *)buf;
1091019d6b8fSAnthony Liguori strm.avail_out = s->cluster_size;
1092019d6b8fSAnthony Liguori strm.next_out = out_buf;
1093019d6b8fSAnthony Liguori
1094019d6b8fSAnthony Liguori ret = deflate(&strm, Z_FINISH);
1095019d6b8fSAnthony Liguori if (ret != Z_STREAM_END && ret != Z_OK) {
1096019d6b8fSAnthony Liguori deflateEnd(&strm);
109764ebe71aSKevin Wolf ret = -EINVAL;
109864ebe71aSKevin Wolf goto fail;
1099019d6b8fSAnthony Liguori }
1100019d6b8fSAnthony Liguori out_len = strm.next_out - out_buf;
1101019d6b8fSAnthony Liguori
1102019d6b8fSAnthony Liguori deflateEnd(&strm);
1103019d6b8fSAnthony Liguori
1104019d6b8fSAnthony Liguori if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1105019d6b8fSAnthony Liguori /* could not compress: write normal cluster */
1106609841a3SEric Blake ret = qcow_co_pwritev(bs, offset, bytes, qiov, 0);
110764ebe71aSKevin Wolf if (ret < 0) {
110864ebe71aSKevin Wolf goto fail;
110964ebe71aSKevin Wolf }
1110f2b95a12SPavel Butsykin goto success;
1111f2b95a12SPavel Butsykin }
1112f2b95a12SPavel Butsykin qemu_co_mutex_lock(&s->lock);
111356439e9dSEric Blake ret = get_cluster_offset(bs, offset, 2, out_len, 0, 0, &cluster_offset);
1114f2b95a12SPavel Butsykin qemu_co_mutex_unlock(&s->lock);
111556439e9dSEric Blake if (ret < 0) {
111656439e9dSEric Blake goto fail;
111756439e9dSEric Blake }
111864ebe71aSKevin Wolf if (cluster_offset == 0) {
111964ebe71aSKevin Wolf ret = -EIO;
112064ebe71aSKevin Wolf goto fail;
112164ebe71aSKevin Wolf }
1122019d6b8fSAnthony Liguori cluster_offset &= s->cluster_offset_mask;
1123f2b95a12SPavel Butsykin
112417362398SPaolo Bonzini BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
11254ed3e0c4SVladimir Sementsov-Ogievskiy ret = bdrv_co_pwrite(bs->file, cluster_offset, out_len, out_buf, 0);
112664ebe71aSKevin Wolf if (ret < 0) {
112764ebe71aSKevin Wolf goto fail;
1128019d6b8fSAnthony Liguori }
1129f2b95a12SPavel Butsykin success:
113064ebe71aSKevin Wolf ret = 0;
113164ebe71aSKevin Wolf fail:
1132f2b95a12SPavel Butsykin qemu_vfree(buf);
11337267c094SAnthony Liguori g_free(out_buf);
113464ebe71aSKevin Wolf return ret;
1135019d6b8fSAnthony Liguori }
1136019d6b8fSAnthony Liguori
11373d47eb0aSEmanuele Giuseppe Esposito static int coroutine_fn
qcow_co_get_info(BlockDriverState * bs,BlockDriverInfo * bdi)11383d47eb0aSEmanuele Giuseppe Esposito qcow_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1139019d6b8fSAnthony Liguori {
1140019d6b8fSAnthony Liguori BDRVQcowState *s = bs->opaque;
1141019d6b8fSAnthony Liguori bdi->cluster_size = s->cluster_size;
1142019d6b8fSAnthony Liguori return 0;
1143019d6b8fSAnthony Liguori }
1144019d6b8fSAnthony Liguori
114516d12159SChunyan Liu static QemuOptsList qcow_create_opts = {
114616d12159SChunyan Liu .name = "qcow-create-opts",
114716d12159SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
114816d12159SChunyan Liu .desc = {
1149db08adf5SKevin Wolf {
1150db08adf5SKevin Wolf .name = BLOCK_OPT_SIZE,
115116d12159SChunyan Liu .type = QEMU_OPT_SIZE,
1152db08adf5SKevin Wolf .help = "Virtual disk size"
1153db08adf5SKevin Wolf },
1154db08adf5SKevin Wolf {
1155db08adf5SKevin Wolf .name = BLOCK_OPT_BACKING_FILE,
115616d12159SChunyan Liu .type = QEMU_OPT_STRING,
1157db08adf5SKevin Wolf .help = "File name of a base image"
1158db08adf5SKevin Wolf },
1159db08adf5SKevin Wolf {
1160344acbd6SEric Blake .name = BLOCK_OPT_BACKING_FMT,
1161344acbd6SEric Blake .type = QEMU_OPT_STRING,
1162344acbd6SEric Blake .help = "Format of the backing image",
1163344acbd6SEric Blake },
1164344acbd6SEric Blake {
1165db08adf5SKevin Wolf .name = BLOCK_OPT_ENCRYPT,
116616d12159SChunyan Liu .type = QEMU_OPT_BOOL,
11670cb8d47bSDaniel P. Berrange .help = "Encrypt the image with format 'aes'. (Deprecated "
11680cb8d47bSDaniel P. Berrange "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
11690cb8d47bSDaniel P. Berrange },
11700cb8d47bSDaniel P. Berrange {
11710cb8d47bSDaniel P. Berrange .name = BLOCK_OPT_ENCRYPT_FORMAT,
11720cb8d47bSDaniel P. Berrange .type = QEMU_OPT_STRING,
11730cb8d47bSDaniel P. Berrange .help = "Encrypt the image, format choices: 'aes'",
1174db08adf5SKevin Wolf },
1175d85f4222SDaniel P. Berrange BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."),
117616d12159SChunyan Liu { /* end of list */ }
117716d12159SChunyan Liu }
11780e7e1989SKevin Wolf };
11790e7e1989SKevin Wolf
11802654267cSMax Reitz static const char *const qcow_strong_runtime_opts[] = {
11812654267cSMax Reitz "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET,
11822654267cSMax Reitz
11832654267cSMax Reitz NULL
11842654267cSMax Reitz };
11852654267cSMax Reitz
1186019d6b8fSAnthony Liguori static BlockDriver bdrv_qcow = {
1187019d6b8fSAnthony Liguori .format_name = "qcow",
1188019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVQcowState),
1189019d6b8fSAnthony Liguori .bdrv_probe = qcow_probe,
1190019d6b8fSAnthony Liguori .bdrv_open = qcow_open,
1191019d6b8fSAnthony Liguori .bdrv_close = qcow_close,
119269dca43dSMax Reitz .bdrv_child_perm = bdrv_default_perms,
1193d177692eSJeff Cody .bdrv_reopen_prepare = qcow_reopen_prepare,
119442a3e1abSKevin Wolf .bdrv_co_create = qcow_co_create,
1195efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qcow_co_create_opts,
11963ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1,
1197d67066d8SMax Reitz .is_format = true,
11988ee79e70SKevin Wolf .supports_backing = true,
1199609841a3SEric Blake .bdrv_refresh_limits = qcow_refresh_limits,
1200c68b89acSKevin Wolf
1201609841a3SEric Blake .bdrv_co_preadv = qcow_co_preadv,
1202609841a3SEric Blake .bdrv_co_pwritev = qcow_co_pwritev,
1203d63b4c93SEric Blake .bdrv_co_block_status = qcow_co_block_status,
1204c68b89acSKevin Wolf
1205c68b89acSKevin Wolf .bdrv_make_empty = qcow_make_empty,
1206f2b95a12SPavel Butsykin .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed,
12073d47eb0aSEmanuele Giuseppe Esposito .bdrv_co_get_info = qcow_co_get_info,
12080e7e1989SKevin Wolf
120916d12159SChunyan Liu .create_opts = &qcow_create_opts,
12102654267cSMax Reitz .strong_runtime_opts = qcow_strong_runtime_opts,
1211019d6b8fSAnthony Liguori };
1212019d6b8fSAnthony Liguori
bdrv_qcow_init(void)1213019d6b8fSAnthony Liguori static void bdrv_qcow_init(void)
1214019d6b8fSAnthony Liguori {
1215019d6b8fSAnthony Liguori bdrv_register(&bdrv_qcow);
1216019d6b8fSAnthony Liguori }
1217019d6b8fSAnthony Liguori
1218019d6b8fSAnthony Liguori block_init(bdrv_qcow_init);
1219