1baefd977SVladimir Sementsov-Ogievskiy /*
2baefd977SVladimir Sementsov-Ogievskiy * Support of Parallels Format Extension. It's a part of Parallels format
3baefd977SVladimir Sementsov-Ogievskiy * driver.
4baefd977SVladimir Sementsov-Ogievskiy *
5baefd977SVladimir Sementsov-Ogievskiy * Copyright (c) 2021 Virtuozzo International GmbH
6baefd977SVladimir Sementsov-Ogievskiy *
7baefd977SVladimir Sementsov-Ogievskiy * Permission is hereby granted, free of charge, to any person obtaining a copy
8baefd977SVladimir Sementsov-Ogievskiy * of this software and associated documentation files (the "Software"), to deal
9baefd977SVladimir Sementsov-Ogievskiy * in the Software without restriction, including without limitation the rights
10baefd977SVladimir Sementsov-Ogievskiy * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11baefd977SVladimir Sementsov-Ogievskiy * copies of the Software, and to permit persons to whom the Software is
12baefd977SVladimir Sementsov-Ogievskiy * furnished to do so, subject to the following conditions:
13baefd977SVladimir Sementsov-Ogievskiy *
14baefd977SVladimir Sementsov-Ogievskiy * The above copyright notice and this permission notice shall be included in
15baefd977SVladimir Sementsov-Ogievskiy * all copies or substantial portions of the Software.
16baefd977SVladimir Sementsov-Ogievskiy *
17baefd977SVladimir Sementsov-Ogievskiy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18baefd977SVladimir Sementsov-Ogievskiy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19baefd977SVladimir Sementsov-Ogievskiy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20baefd977SVladimir Sementsov-Ogievskiy * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21baefd977SVladimir Sementsov-Ogievskiy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22baefd977SVladimir Sementsov-Ogievskiy * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23baefd977SVladimir Sementsov-Ogievskiy * THE SOFTWARE.
24baefd977SVladimir Sementsov-Ogievskiy */
25baefd977SVladimir Sementsov-Ogievskiy
26baefd977SVladimir Sementsov-Ogievskiy #include "qemu/osdep.h"
27baefd977SVladimir Sementsov-Ogievskiy #include "qapi/error.h"
28e2c1c34fSMarkus Armbruster #include "block/block-io.h"
29baefd977SVladimir Sementsov-Ogievskiy #include "block/block_int.h"
30e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h"
31baefd977SVladimir Sementsov-Ogievskiy #include "parallels.h"
32baefd977SVladimir Sementsov-Ogievskiy #include "crypto/hash.h"
33baefd977SVladimir Sementsov-Ogievskiy #include "qemu/uuid.h"
345df022cfSPeter Maydell #include "qemu/memalign.h"
35baefd977SVladimir Sementsov-Ogievskiy
36baefd977SVladimir Sementsov-Ogievskiy #define PARALLELS_FORMAT_EXTENSION_MAGIC 0xAB234CEF23DCEA87ULL
37baefd977SVladimir Sementsov-Ogievskiy
38baefd977SVladimir Sementsov-Ogievskiy #define PARALLELS_END_OF_FEATURES_MAGIC 0x0ULL
39baefd977SVladimir Sementsov-Ogievskiy #define PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC 0x20385FAE252CB34AULL
40baefd977SVladimir Sementsov-Ogievskiy
41baefd977SVladimir Sementsov-Ogievskiy typedef struct ParallelsFormatExtensionHeader {
42baefd977SVladimir Sementsov-Ogievskiy uint64_t magic; /* PARALLELS_FORMAT_EXTENSION_MAGIC */
43baefd977SVladimir Sementsov-Ogievskiy uint8_t check_sum[16];
44baefd977SVladimir Sementsov-Ogievskiy } QEMU_PACKED ParallelsFormatExtensionHeader;
45baefd977SVladimir Sementsov-Ogievskiy
46baefd977SVladimir Sementsov-Ogievskiy typedef struct ParallelsFeatureHeader {
47baefd977SVladimir Sementsov-Ogievskiy uint64_t magic;
48baefd977SVladimir Sementsov-Ogievskiy uint64_t flags;
49baefd977SVladimir Sementsov-Ogievskiy uint32_t data_size;
50baefd977SVladimir Sementsov-Ogievskiy uint32_t _unused;
51baefd977SVladimir Sementsov-Ogievskiy } QEMU_PACKED ParallelsFeatureHeader;
52baefd977SVladimir Sementsov-Ogievskiy
53baefd977SVladimir Sementsov-Ogievskiy typedef struct ParallelsDirtyBitmapFeature {
54baefd977SVladimir Sementsov-Ogievskiy uint64_t size;
55baefd977SVladimir Sementsov-Ogievskiy uint8_t id[16];
56baefd977SVladimir Sementsov-Ogievskiy uint32_t granularity;
57baefd977SVladimir Sementsov-Ogievskiy uint32_t l1_size;
58baefd977SVladimir Sementsov-Ogievskiy /* L1 table follows */
59baefd977SVladimir Sementsov-Ogievskiy } QEMU_PACKED ParallelsDirtyBitmapFeature;
60baefd977SVladimir Sementsov-Ogievskiy
61baefd977SVladimir Sementsov-Ogievskiy /* Given L1 table read bitmap data from the image and populate @bitmap */
621f051dcbSKevin Wolf static int GRAPH_RDLOCK
parallels_load_bitmap_data(BlockDriverState * bs,const uint64_t * l1_table,uint32_t l1_size,BdrvDirtyBitmap * bitmap,Error ** errp)631f051dcbSKevin Wolf parallels_load_bitmap_data(BlockDriverState *bs, const uint64_t *l1_table,
641f051dcbSKevin Wolf uint32_t l1_size, BdrvDirtyBitmap *bitmap,
65baefd977SVladimir Sementsov-Ogievskiy Error **errp)
66baefd977SVladimir Sementsov-Ogievskiy {
67baefd977SVladimir Sementsov-Ogievskiy BDRVParallelsState *s = bs->opaque;
68baefd977SVladimir Sementsov-Ogievskiy int ret = 0;
69baefd977SVladimir Sementsov-Ogievskiy uint64_t offset, limit;
70baefd977SVladimir Sementsov-Ogievskiy uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
71baefd977SVladimir Sementsov-Ogievskiy uint8_t *buf = NULL;
72baefd977SVladimir Sementsov-Ogievskiy uint64_t i, tab_size =
73baefd977SVladimir Sementsov-Ogievskiy DIV_ROUND_UP(bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size),
74baefd977SVladimir Sementsov-Ogievskiy s->cluster_size);
75baefd977SVladimir Sementsov-Ogievskiy
76baefd977SVladimir Sementsov-Ogievskiy if (tab_size != l1_size) {
77baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap table size %" PRIu32 " does not correspond "
78baefd977SVladimir Sementsov-Ogievskiy "to bitmap size and cluster size. Expected %" PRIu64,
79baefd977SVladimir Sementsov-Ogievskiy l1_size, tab_size);
80baefd977SVladimir Sementsov-Ogievskiy return -EINVAL;
81baefd977SVladimir Sementsov-Ogievskiy }
82baefd977SVladimir Sementsov-Ogievskiy
83baefd977SVladimir Sementsov-Ogievskiy buf = qemu_blockalign(bs, s->cluster_size);
84baefd977SVladimir Sementsov-Ogievskiy limit = bdrv_dirty_bitmap_serialization_coverage(s->cluster_size, bitmap);
85baefd977SVladimir Sementsov-Ogievskiy for (i = 0, offset = 0; i < tab_size; ++i, offset += limit) {
86baefd977SVladimir Sementsov-Ogievskiy uint64_t count = MIN(bm_size - offset, limit);
87baefd977SVladimir Sementsov-Ogievskiy uint64_t entry = l1_table[i];
88baefd977SVladimir Sementsov-Ogievskiy
89baefd977SVladimir Sementsov-Ogievskiy if (entry == 0) {
90baefd977SVladimir Sementsov-Ogievskiy /* No need to deserialize zeros because @bitmap is cleared. */
91baefd977SVladimir Sementsov-Ogievskiy continue;
92baefd977SVladimir Sementsov-Ogievskiy }
93baefd977SVladimir Sementsov-Ogievskiy
94baefd977SVladimir Sementsov-Ogievskiy if (entry == 1) {
95baefd977SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_deserialize_ones(bitmap, offset, count, false);
96baefd977SVladimir Sementsov-Ogievskiy } else {
9732cc71deSAlberto Faria ret = bdrv_pread(bs->file, entry << BDRV_SECTOR_BITS,
9832cc71deSAlberto Faria s->cluster_size, buf, 0);
99baefd977SVladimir Sementsov-Ogievskiy if (ret < 0) {
100baefd977SVladimir Sementsov-Ogievskiy error_setg_errno(errp, -ret,
101baefd977SVladimir Sementsov-Ogievskiy "Failed to read bitmap data cluster");
102baefd977SVladimir Sementsov-Ogievskiy goto finish;
103baefd977SVladimir Sementsov-Ogievskiy }
104baefd977SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_deserialize_part(bitmap, buf, offset, count,
105baefd977SVladimir Sementsov-Ogievskiy false);
106baefd977SVladimir Sementsov-Ogievskiy }
107baefd977SVladimir Sementsov-Ogievskiy }
108baefd977SVladimir Sementsov-Ogievskiy ret = 0;
109baefd977SVladimir Sementsov-Ogievskiy
110baefd977SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_deserialize_finish(bitmap);
111baefd977SVladimir Sementsov-Ogievskiy
112baefd977SVladimir Sementsov-Ogievskiy finish:
113baefd977SVladimir Sementsov-Ogievskiy qemu_vfree(buf);
114baefd977SVladimir Sementsov-Ogievskiy
115baefd977SVladimir Sementsov-Ogievskiy return ret;
116baefd977SVladimir Sementsov-Ogievskiy }
117baefd977SVladimir Sementsov-Ogievskiy
118baefd977SVladimir Sementsov-Ogievskiy /*
119baefd977SVladimir Sementsov-Ogievskiy * @data buffer (of @data_size size) is the Dirty bitmaps feature which
120baefd977SVladimir Sementsov-Ogievskiy * consists of ParallelsDirtyBitmapFeature followed by L1 table.
121baefd977SVladimir Sementsov-Ogievskiy */
1221f051dcbSKevin Wolf static BdrvDirtyBitmap * GRAPH_RDLOCK
parallels_load_bitmap(BlockDriverState * bs,uint8_t * data,size_t data_size,Error ** errp)1231f051dcbSKevin Wolf parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size,
124baefd977SVladimir Sementsov-Ogievskiy Error **errp)
125baefd977SVladimir Sementsov-Ogievskiy {
126baefd977SVladimir Sementsov-Ogievskiy int ret;
127baefd977SVladimir Sementsov-Ogievskiy ParallelsDirtyBitmapFeature bf;
128baefd977SVladimir Sementsov-Ogievskiy g_autofree uint64_t *l1_table = NULL;
129baefd977SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap;
130baefd977SVladimir Sementsov-Ogievskiy QemuUUID uuid;
131721da039SCédric Le Goater char uuidstr[UUID_STR_LEN];
132baefd977SVladimir Sementsov-Ogievskiy int i;
133baefd977SVladimir Sementsov-Ogievskiy
134baefd977SVladimir Sementsov-Ogievskiy if (data_size < sizeof(bf)) {
135baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Too small Bitmap Feature area in Parallels Format "
136baefd977SVladimir Sementsov-Ogievskiy "Extension: %zu bytes, expected at least %zu bytes",
137baefd977SVladimir Sementsov-Ogievskiy data_size, sizeof(bf));
138baefd977SVladimir Sementsov-Ogievskiy return NULL;
139baefd977SVladimir Sementsov-Ogievskiy }
140baefd977SVladimir Sementsov-Ogievskiy memcpy(&bf, data, sizeof(bf));
141baefd977SVladimir Sementsov-Ogievskiy bf.size = le64_to_cpu(bf.size);
142baefd977SVladimir Sementsov-Ogievskiy bf.granularity = le32_to_cpu(bf.granularity) << BDRV_SECTOR_BITS;
143baefd977SVladimir Sementsov-Ogievskiy bf.l1_size = le32_to_cpu(bf.l1_size);
144baefd977SVladimir Sementsov-Ogievskiy data += sizeof(bf);
145baefd977SVladimir Sementsov-Ogievskiy data_size -= sizeof(bf);
146baefd977SVladimir Sementsov-Ogievskiy
147baefd977SVladimir Sementsov-Ogievskiy if (bf.size != bs->total_sectors) {
148baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmap size (in sectors) %" PRId64 " differs from "
149baefd977SVladimir Sementsov-Ogievskiy "disk size in sectors %" PRId64, bf.size, bs->total_sectors);
150baefd977SVladimir Sementsov-Ogievskiy return NULL;
151baefd977SVladimir Sementsov-Ogievskiy }
152baefd977SVladimir Sementsov-Ogievskiy
153baefd977SVladimir Sementsov-Ogievskiy if (bf.l1_size * sizeof(uint64_t) > data_size) {
154baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Bitmaps feature corrupted: l1 table exceeds "
155baefd977SVladimir Sementsov-Ogievskiy "extension data_size");
156baefd977SVladimir Sementsov-Ogievskiy return NULL;
157baefd977SVladimir Sementsov-Ogievskiy }
158baefd977SVladimir Sementsov-Ogievskiy
159baefd977SVladimir Sementsov-Ogievskiy memcpy(&uuid, bf.id, sizeof(uuid));
160baefd977SVladimir Sementsov-Ogievskiy qemu_uuid_unparse(&uuid, uuidstr);
161baefd977SVladimir Sementsov-Ogievskiy bitmap = bdrv_create_dirty_bitmap(bs, bf.granularity, uuidstr, errp);
162baefd977SVladimir Sementsov-Ogievskiy if (!bitmap) {
163baefd977SVladimir Sementsov-Ogievskiy return NULL;
164baefd977SVladimir Sementsov-Ogievskiy }
165baefd977SVladimir Sementsov-Ogievskiy
166baefd977SVladimir Sementsov-Ogievskiy l1_table = g_new(uint64_t, bf.l1_size);
167baefd977SVladimir Sementsov-Ogievskiy for (i = 0; i < bf.l1_size; i++, data += sizeof(uint64_t)) {
168baefd977SVladimir Sementsov-Ogievskiy l1_table[i] = ldq_le_p(data);
169baefd977SVladimir Sementsov-Ogievskiy }
170baefd977SVladimir Sementsov-Ogievskiy
171baefd977SVladimir Sementsov-Ogievskiy ret = parallels_load_bitmap_data(bs, l1_table, bf.l1_size, bitmap, errp);
172baefd977SVladimir Sementsov-Ogievskiy if (ret < 0) {
173baefd977SVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(bitmap);
174baefd977SVladimir Sementsov-Ogievskiy return NULL;
175baefd977SVladimir Sementsov-Ogievskiy }
176baefd977SVladimir Sementsov-Ogievskiy
177baefd977SVladimir Sementsov-Ogievskiy /* We support format extension only for RO parallels images. */
178baefd977SVladimir Sementsov-Ogievskiy assert(!(bs->open_flags & BDRV_O_RDWR));
179baefd977SVladimir Sementsov-Ogievskiy bdrv_dirty_bitmap_set_readonly(bitmap, true);
180baefd977SVladimir Sementsov-Ogievskiy
181baefd977SVladimir Sementsov-Ogievskiy return bitmap;
182baefd977SVladimir Sementsov-Ogievskiy }
183baefd977SVladimir Sementsov-Ogievskiy
1841f051dcbSKevin Wolf static int GRAPH_RDLOCK
parallels_parse_format_extension(BlockDriverState * bs,uint8_t * ext_cluster,Error ** errp)1851f051dcbSKevin Wolf parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster,
1861f051dcbSKevin Wolf Error **errp)
187baefd977SVladimir Sementsov-Ogievskiy {
188baefd977SVladimir Sementsov-Ogievskiy BDRVParallelsState *s = bs->opaque;
189baefd977SVladimir Sementsov-Ogievskiy int ret;
190baefd977SVladimir Sementsov-Ogievskiy int remaining = s->cluster_size;
191baefd977SVladimir Sementsov-Ogievskiy uint8_t *pos = ext_cluster;
192baefd977SVladimir Sementsov-Ogievskiy ParallelsFormatExtensionHeader eh;
193baefd977SVladimir Sementsov-Ogievskiy g_autofree uint8_t *hash = NULL;
194baefd977SVladimir Sementsov-Ogievskiy size_t hash_len = 0;
195baefd977SVladimir Sementsov-Ogievskiy GSList *bitmaps = NULL, *el;
196baefd977SVladimir Sementsov-Ogievskiy
197baefd977SVladimir Sementsov-Ogievskiy memcpy(&eh, pos, sizeof(eh));
198baefd977SVladimir Sementsov-Ogievskiy eh.magic = le64_to_cpu(eh.magic);
199baefd977SVladimir Sementsov-Ogievskiy pos += sizeof(eh);
200baefd977SVladimir Sementsov-Ogievskiy remaining -= sizeof(eh);
201baefd977SVladimir Sementsov-Ogievskiy
202baefd977SVladimir Sementsov-Ogievskiy if (eh.magic != PARALLELS_FORMAT_EXTENSION_MAGIC) {
203baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Wrong parallels Format Extension magic: 0x%" PRIx64
204baefd977SVladimir Sementsov-Ogievskiy ", expected: 0x%llx", eh.magic,
205baefd977SVladimir Sementsov-Ogievskiy PARALLELS_FORMAT_EXTENSION_MAGIC);
206baefd977SVladimir Sementsov-Ogievskiy goto fail;
207baefd977SVladimir Sementsov-Ogievskiy }
208baefd977SVladimir Sementsov-Ogievskiy
209*ef834aa2SMarkus Armbruster ret = qcrypto_hash_bytes(QCRYPTO_HASH_ALGO_MD5, (char *)pos, remaining,
210baefd977SVladimir Sementsov-Ogievskiy &hash, &hash_len, errp);
211baefd977SVladimir Sementsov-Ogievskiy if (ret < 0) {
212baefd977SVladimir Sementsov-Ogievskiy goto fail;
213baefd977SVladimir Sementsov-Ogievskiy }
214baefd977SVladimir Sementsov-Ogievskiy
215baefd977SVladimir Sementsov-Ogievskiy if (hash_len != sizeof(eh.check_sum) ||
216baefd977SVladimir Sementsov-Ogievskiy memcmp(hash, eh.check_sum, sizeof(eh.check_sum)) != 0) {
217baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Wrong checksum in Format Extension header. Format "
218baefd977SVladimir Sementsov-Ogievskiy "extension is corrupted.");
219baefd977SVladimir Sementsov-Ogievskiy goto fail;
220baefd977SVladimir Sementsov-Ogievskiy }
221baefd977SVladimir Sementsov-Ogievskiy
222baefd977SVladimir Sementsov-Ogievskiy while (true) {
223baefd977SVladimir Sementsov-Ogievskiy ParallelsFeatureHeader fh;
224baefd977SVladimir Sementsov-Ogievskiy BdrvDirtyBitmap *bitmap;
225baefd977SVladimir Sementsov-Ogievskiy
226baefd977SVladimir Sementsov-Ogievskiy if (remaining < sizeof(fh)) {
227baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Can not read feature header, as remaining bytes "
228baefd977SVladimir Sementsov-Ogievskiy "(%d) in Format Extension is less than Feature header "
229baefd977SVladimir Sementsov-Ogievskiy "size (%zu)", remaining, sizeof(fh));
230baefd977SVladimir Sementsov-Ogievskiy goto fail;
231baefd977SVladimir Sementsov-Ogievskiy }
232baefd977SVladimir Sementsov-Ogievskiy
233baefd977SVladimir Sementsov-Ogievskiy memcpy(&fh, pos, sizeof(fh));
234baefd977SVladimir Sementsov-Ogievskiy pos += sizeof(fh);
235baefd977SVladimir Sementsov-Ogievskiy remaining -= sizeof(fh);
236baefd977SVladimir Sementsov-Ogievskiy
237baefd977SVladimir Sementsov-Ogievskiy fh.magic = le64_to_cpu(fh.magic);
238baefd977SVladimir Sementsov-Ogievskiy fh.flags = le64_to_cpu(fh.flags);
239baefd977SVladimir Sementsov-Ogievskiy fh.data_size = le32_to_cpu(fh.data_size);
240baefd977SVladimir Sementsov-Ogievskiy
241baefd977SVladimir Sementsov-Ogievskiy if (fh.flags) {
242baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Flags for extension feature are unsupported");
243baefd977SVladimir Sementsov-Ogievskiy goto fail;
244baefd977SVladimir Sementsov-Ogievskiy }
245baefd977SVladimir Sementsov-Ogievskiy
246baefd977SVladimir Sementsov-Ogievskiy if (fh.data_size > remaining) {
247baefd977SVladimir Sementsov-Ogievskiy error_setg(errp, "Feature data_size exceedes Format Extension "
248baefd977SVladimir Sementsov-Ogievskiy "cluster");
249baefd977SVladimir Sementsov-Ogievskiy goto fail;
250baefd977SVladimir Sementsov-Ogievskiy }
251baefd977SVladimir Sementsov-Ogievskiy
252baefd977SVladimir Sementsov-Ogievskiy switch (fh.magic) {
253baefd977SVladimir Sementsov-Ogievskiy case PARALLELS_END_OF_FEATURES_MAGIC:
254baefd977SVladimir Sementsov-Ogievskiy return 0;
255baefd977SVladimir Sementsov-Ogievskiy
256baefd977SVladimir Sementsov-Ogievskiy case PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC:
257baefd977SVladimir Sementsov-Ogievskiy bitmap = parallels_load_bitmap(bs, pos, fh.data_size, errp);
258baefd977SVladimir Sementsov-Ogievskiy if (!bitmap) {
259baefd977SVladimir Sementsov-Ogievskiy goto fail;
260baefd977SVladimir Sementsov-Ogievskiy }
261baefd977SVladimir Sementsov-Ogievskiy bitmaps = g_slist_append(bitmaps, bitmap);
262baefd977SVladimir Sementsov-Ogievskiy break;
263baefd977SVladimir Sementsov-Ogievskiy
264baefd977SVladimir Sementsov-Ogievskiy default:
2653f1db959SPhilippe Mathieu-Daudé error_setg(errp, "Unknown feature: 0x%" PRIx64, fh.magic);
266baefd977SVladimir Sementsov-Ogievskiy goto fail;
267baefd977SVladimir Sementsov-Ogievskiy }
268baefd977SVladimir Sementsov-Ogievskiy
269baefd977SVladimir Sementsov-Ogievskiy pos = ext_cluster + QEMU_ALIGN_UP(pos + fh.data_size - ext_cluster, 8);
270baefd977SVladimir Sementsov-Ogievskiy }
271baefd977SVladimir Sementsov-Ogievskiy
272baefd977SVladimir Sementsov-Ogievskiy fail:
273baefd977SVladimir Sementsov-Ogievskiy for (el = bitmaps; el; el = el->next) {
274baefd977SVladimir Sementsov-Ogievskiy bdrv_release_dirty_bitmap(el->data);
275baefd977SVladimir Sementsov-Ogievskiy }
276baefd977SVladimir Sementsov-Ogievskiy g_slist_free(bitmaps);
277baefd977SVladimir Sementsov-Ogievskiy
278baefd977SVladimir Sementsov-Ogievskiy return -EINVAL;
279baefd977SVladimir Sementsov-Ogievskiy }
280baefd977SVladimir Sementsov-Ogievskiy
parallels_read_format_extension(BlockDriverState * bs,int64_t ext_off,Error ** errp)281baefd977SVladimir Sementsov-Ogievskiy int parallels_read_format_extension(BlockDriverState *bs,
282baefd977SVladimir Sementsov-Ogievskiy int64_t ext_off, Error **errp)
283baefd977SVladimir Sementsov-Ogievskiy {
284baefd977SVladimir Sementsov-Ogievskiy BDRVParallelsState *s = bs->opaque;
285baefd977SVladimir Sementsov-Ogievskiy int ret;
286baefd977SVladimir Sementsov-Ogievskiy uint8_t *ext_cluster = qemu_blockalign(bs, s->cluster_size);
287baefd977SVladimir Sementsov-Ogievskiy
288baefd977SVladimir Sementsov-Ogievskiy assert(ext_off > 0);
289baefd977SVladimir Sementsov-Ogievskiy
29032cc71deSAlberto Faria ret = bdrv_pread(bs->file, ext_off, s->cluster_size, ext_cluster, 0);
291baefd977SVladimir Sementsov-Ogievskiy if (ret < 0) {
292baefd977SVladimir Sementsov-Ogievskiy error_setg_errno(errp, -ret, "Failed to read Format Extension cluster");
293baefd977SVladimir Sementsov-Ogievskiy goto out;
294baefd977SVladimir Sementsov-Ogievskiy }
295baefd977SVladimir Sementsov-Ogievskiy
296baefd977SVladimir Sementsov-Ogievskiy ret = parallels_parse_format_extension(bs, ext_cluster, errp);
297baefd977SVladimir Sementsov-Ogievskiy
298baefd977SVladimir Sementsov-Ogievskiy out:
299baefd977SVladimir Sementsov-Ogievskiy qemu_vfree(ext_cluster);
300baefd977SVladimir Sementsov-Ogievskiy
301baefd977SVladimir Sementsov-Ogievskiy return ret;
302baefd977SVladimir Sementsov-Ogievskiy }
303