xref: /openbmc/qemu/block/parallels-ext.c (revision 84369d76)
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/uuid.h"
34 #include "qemu/memalign.h"
35 
36 #define PARALLELS_FORMAT_EXTENSION_MAGIC 0xAB234CEF23DCEA87ULL
37 
38 #define PARALLELS_END_OF_FEATURES_MAGIC 0x0ULL
39 #define PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC 0x20385FAE252CB34AULL
40 
41 typedef struct ParallelsFormatExtensionHeader {
42     uint64_t magic; /* PARALLELS_FORMAT_EXTENSION_MAGIC */
43     uint8_t check_sum[16];
44 } QEMU_PACKED ParallelsFormatExtensionHeader;
45 
46 typedef struct ParallelsFeatureHeader {
47     uint64_t magic;
48     uint64_t flags;
49     uint32_t data_size;
50     uint32_t _unused;
51 } QEMU_PACKED ParallelsFeatureHeader;
52 
53 typedef struct ParallelsDirtyBitmapFeature {
54     uint64_t size;
55     uint8_t id[16];
56     uint32_t granularity;
57     uint32_t l1_size;
58     /* L1 table follows */
59 } QEMU_PACKED ParallelsDirtyBitmapFeature;
60 
61 /* Given L1 table read bitmap data from the image and populate @bitmap */
62 static int GRAPH_RDLOCK
63 parallels_load_bitmap_data(BlockDriverState *bs, const uint64_t *l1_table,
64                            uint32_t l1_size, BdrvDirtyBitmap *bitmap,
65                            Error **errp)
66 {
67     BDRVParallelsState *s = bs->opaque;
68     int ret = 0;
69     uint64_t offset, limit;
70     uint64_t bm_size = bdrv_dirty_bitmap_size(bitmap);
71     uint8_t *buf = NULL;
72     uint64_t i, tab_size =
73         DIV_ROUND_UP(bdrv_dirty_bitmap_serialization_size(bitmap, 0, bm_size),
74                      s->cluster_size);
75 
76     if (tab_size != l1_size) {
77         error_setg(errp, "Bitmap table size %" PRIu32 " does not correspond "
78                    "to bitmap size and cluster size. Expected %" PRIu64,
79                    l1_size, tab_size);
80         return -EINVAL;
81     }
82 
83     buf = qemu_blockalign(bs, s->cluster_size);
84     limit = bdrv_dirty_bitmap_serialization_coverage(s->cluster_size, bitmap);
85     for (i = 0, offset = 0; i < tab_size; ++i, offset += limit) {
86         uint64_t count = MIN(bm_size - offset, limit);
87         uint64_t entry = l1_table[i];
88 
89         if (entry == 0) {
90             /* No need to deserialize zeros because @bitmap is cleared. */
91             continue;
92         }
93 
94         if (entry == 1) {
95             bdrv_dirty_bitmap_deserialize_ones(bitmap, offset, count, false);
96         } else {
97             ret = bdrv_pread(bs->file, entry << BDRV_SECTOR_BITS,
98                              s->cluster_size, buf, 0);
99             if (ret < 0) {
100                 error_setg_errno(errp, -ret,
101                                  "Failed to read bitmap data cluster");
102                 goto finish;
103             }
104             bdrv_dirty_bitmap_deserialize_part(bitmap, buf, offset, count,
105                                                false);
106         }
107     }
108     ret = 0;
109 
110     bdrv_dirty_bitmap_deserialize_finish(bitmap);
111 
112 finish:
113     qemu_vfree(buf);
114 
115     return ret;
116 }
117 
118 /*
119  * @data buffer (of @data_size size) is the Dirty bitmaps feature which
120  * consists of ParallelsDirtyBitmapFeature followed by L1 table.
121  */
122 static BdrvDirtyBitmap * GRAPH_RDLOCK
123 parallels_load_bitmap(BlockDriverState *bs, uint8_t *data, size_t data_size,
124                       Error **errp)
125 {
126     int ret;
127     ParallelsDirtyBitmapFeature bf;
128     g_autofree uint64_t *l1_table = NULL;
129     BdrvDirtyBitmap *bitmap;
130     QemuUUID uuid;
131     char uuidstr[UUID_STR_LEN];
132     int i;
133 
134     if (data_size < sizeof(bf)) {
135         error_setg(errp, "Too small Bitmap Feature area in Parallels Format "
136                    "Extension: %zu bytes, expected at least %zu bytes",
137                    data_size, sizeof(bf));
138         return NULL;
139     }
140     memcpy(&bf, data, sizeof(bf));
141     bf.size = le64_to_cpu(bf.size);
142     bf.granularity = le32_to_cpu(bf.granularity) << BDRV_SECTOR_BITS;
143     bf.l1_size = le32_to_cpu(bf.l1_size);
144     data += sizeof(bf);
145     data_size -= sizeof(bf);
146 
147     if (bf.size != bs->total_sectors) {
148         error_setg(errp, "Bitmap size (in sectors) %" PRId64 " differs from "
149                    "disk size in sectors %" PRId64, bf.size, bs->total_sectors);
150         return NULL;
151     }
152 
153     if (bf.l1_size * sizeof(uint64_t) > data_size) {
154         error_setg(errp, "Bitmaps feature corrupted: l1 table exceeds "
155                    "extension data_size");
156         return NULL;
157     }
158 
159     memcpy(&uuid, bf.id, sizeof(uuid));
160     qemu_uuid_unparse(&uuid, uuidstr);
161     bitmap = bdrv_create_dirty_bitmap(bs, bf.granularity, uuidstr, errp);
162     if (!bitmap) {
163         return NULL;
164     }
165 
166     l1_table = g_new(uint64_t, bf.l1_size);
167     for (i = 0; i < bf.l1_size; i++, data += sizeof(uint64_t)) {
168         l1_table[i] = ldq_le_p(data);
169     }
170 
171     ret = parallels_load_bitmap_data(bs, l1_table, bf.l1_size, bitmap, errp);
172     if (ret < 0) {
173         bdrv_release_dirty_bitmap(bitmap);
174         return NULL;
175     }
176 
177     /* We support format extension only for RO parallels images. */
178     assert(!(bs->open_flags & BDRV_O_RDWR));
179     bdrv_dirty_bitmap_set_readonly(bitmap, true);
180 
181     return bitmap;
182 }
183 
184 static int GRAPH_RDLOCK
185 parallels_parse_format_extension(BlockDriverState *bs, uint8_t *ext_cluster,
186                                  Error **errp)
187 {
188     BDRVParallelsState *s = bs->opaque;
189     int ret;
190     int remaining = s->cluster_size;
191     uint8_t *pos = ext_cluster;
192     ParallelsFormatExtensionHeader eh;
193     g_autofree uint8_t *hash = NULL;
194     size_t hash_len = 0;
195     GSList *bitmaps = NULL, *el;
196 
197     memcpy(&eh, pos, sizeof(eh));
198     eh.magic = le64_to_cpu(eh.magic);
199     pos += sizeof(eh);
200     remaining -= sizeof(eh);
201 
202     if (eh.magic != PARALLELS_FORMAT_EXTENSION_MAGIC) {
203         error_setg(errp, "Wrong parallels Format Extension magic: 0x%" PRIx64
204                    ", expected: 0x%llx", eh.magic,
205                    PARALLELS_FORMAT_EXTENSION_MAGIC);
206         goto fail;
207     }
208 
209     ret = qcrypto_hash_bytes(QCRYPTO_HASH_ALG_MD5, (char *)pos, remaining,
210                              &hash, &hash_len, errp);
211     if (ret < 0) {
212         goto fail;
213     }
214 
215     if (hash_len != sizeof(eh.check_sum) ||
216         memcmp(hash, eh.check_sum, sizeof(eh.check_sum)) != 0) {
217         error_setg(errp, "Wrong checksum in Format Extension header. Format "
218                    "extension is corrupted.");
219         goto fail;
220     }
221 
222     while (true) {
223         ParallelsFeatureHeader fh;
224         BdrvDirtyBitmap *bitmap;
225 
226         if (remaining < sizeof(fh)) {
227             error_setg(errp, "Can not read feature header, as remaining bytes "
228                        "(%d) in Format Extension is less than Feature header "
229                        "size (%zu)", remaining, sizeof(fh));
230             goto fail;
231         }
232 
233         memcpy(&fh, pos, sizeof(fh));
234         pos += sizeof(fh);
235         remaining -= sizeof(fh);
236 
237         fh.magic = le64_to_cpu(fh.magic);
238         fh.flags = le64_to_cpu(fh.flags);
239         fh.data_size = le32_to_cpu(fh.data_size);
240 
241         if (fh.flags) {
242             error_setg(errp, "Flags for extension feature are unsupported");
243             goto fail;
244         }
245 
246         if (fh.data_size > remaining) {
247             error_setg(errp, "Feature data_size exceedes Format Extension "
248                        "cluster");
249             goto fail;
250         }
251 
252         switch (fh.magic) {
253         case PARALLELS_END_OF_FEATURES_MAGIC:
254             return 0;
255 
256         case PARALLELS_DIRTY_BITMAP_FEATURE_MAGIC:
257             bitmap = parallels_load_bitmap(bs, pos, fh.data_size, errp);
258             if (!bitmap) {
259                 goto fail;
260             }
261             bitmaps = g_slist_append(bitmaps, bitmap);
262             break;
263 
264         default:
265             error_setg(errp, "Unknown feature: 0x%" PRIx64, fh.magic);
266             goto fail;
267         }
268 
269         pos = ext_cluster + QEMU_ALIGN_UP(pos + fh.data_size - ext_cluster, 8);
270     }
271 
272 fail:
273     for (el = bitmaps; el; el = el->next) {
274         bdrv_release_dirty_bitmap(el->data);
275     }
276     g_slist_free(bitmaps);
277 
278     return -EINVAL;
279 }
280 
281 int parallels_read_format_extension(BlockDriverState *bs,
282                                     int64_t ext_off, Error **errp)
283 {
284     BDRVParallelsState *s = bs->opaque;
285     int ret;
286     uint8_t *ext_cluster = qemu_blockalign(bs, s->cluster_size);
287 
288     assert(ext_off > 0);
289 
290     ret = bdrv_pread(bs->file, ext_off, s->cluster_size, ext_cluster, 0);
291     if (ret < 0) {
292         error_setg_errno(errp, -ret, "Failed to read Format Extension cluster");
293         goto out;
294     }
295 
296     ret = parallels_parse_format_extension(bs, ext_cluster, errp);
297 
298 out:
299     qemu_vfree(ext_cluster);
300 
301     return ret;
302 }
303