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