]>
Commit | Line | Data |
---|---|---|
6ada7453 TS |
1 | /* |
2 | * Block driver for Parallels disk image format | |
3 | * | |
4 | * Copyright (c) 2007 Alex Beregszaszi | |
5 | * | |
6 | * This code is based on comparing different disk images created by Parallels. | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 | * THE SOFTWARE. | |
25 | */ | |
faf07963 | 26 | #include "qemu-common.h" |
737e150e | 27 | #include "block/block_int.h" |
1de7afc9 | 28 | #include "qemu/module.h" |
6ada7453 TS |
29 | |
30 | /**************************************************************/ | |
31 | ||
32 | #define HEADER_MAGIC "WithoutFreeSpace" | |
d25d5980 | 33 | #define HEADER_MAGIC2 "WithouFreSpacExt" |
6ada7453 TS |
34 | #define HEADER_VERSION 2 |
35 | #define HEADER_SIZE 64 | |
36 | ||
37 | // always little-endian | |
07898904 | 38 | typedef struct ParallelsHeader { |
6ada7453 TS |
39 | char magic[16]; // "WithoutFreeSpace" |
40 | uint32_t version; | |
41 | uint32_t heads; | |
42 | uint32_t cylinders; | |
43 | uint32_t tracks; | |
44 | uint32_t catalog_entries; | |
8c27d54f DL |
45 | uint64_t nb_sectors; |
46 | uint32_t inuse; | |
47 | uint32_t data_off; | |
48 | char padding[12]; | |
07898904 | 49 | } QEMU_PACKED ParallelsHeader; |
6ada7453 TS |
50 | |
51 | typedef struct BDRVParallelsState { | |
481fb9cf DL |
52 | /** Locking is conservative, the lock protects |
53 | * - image file extending (truncate, fallocate) | |
54 | * - any access to block allocation table | |
55 | */ | |
848c66e8 | 56 | CoMutex lock; |
6ada7453 TS |
57 | |
58 | uint32_t *catalog_bitmap; | |
afbcc40b | 59 | unsigned int catalog_size; |
6ada7453 | 60 | |
9302e863 | 61 | unsigned int tracks; |
d25d5980 DL |
62 | |
63 | unsigned int off_multiplier; | |
6ada7453 TS |
64 | } BDRVParallelsState; |
65 | ||
66 | static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename) | |
67 | { | |
07898904 | 68 | const ParallelsHeader *ph = (const void *)buf; |
6ada7453 TS |
69 | |
70 | if (buf_size < HEADER_SIZE) | |
f08e2f84 | 71 | return 0; |
6ada7453 | 72 | |
d25d5980 DL |
73 | if ((!memcmp(ph->magic, HEADER_MAGIC, 16) || |
74 | !memcmp(ph->magic, HEADER_MAGIC2, 16)) && | |
f08e2f84 DL |
75 | (le32_to_cpu(ph->version) == HEADER_VERSION)) |
76 | return 100; | |
6ada7453 TS |
77 | |
78 | return 0; | |
79 | } | |
80 | ||
015a1036 HR |
81 | static int parallels_open(BlockDriverState *bs, QDict *options, int flags, |
82 | Error **errp) | |
6ada7453 TS |
83 | { |
84 | BDRVParallelsState *s = bs->opaque; | |
1dec5a70 | 85 | int i; |
07898904 | 86 | ParallelsHeader ph; |
46536235 | 87 | int ret; |
6ada7453 | 88 | |
6ada7453 TS |
89 | bs->read_only = 1; // no write support yet |
90 | ||
46536235 KW |
91 | ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph)); |
92 | if (ret < 0) { | |
6ada7453 | 93 | goto fail; |
46536235 | 94 | } |
6ada7453 | 95 | |
d25d5980 DL |
96 | bs->total_sectors = le64_to_cpu(ph.nb_sectors); |
97 | ||
418a7adb DL |
98 | if (le32_to_cpu(ph.version) != HEADER_VERSION) { |
99 | goto fail_format; | |
100 | } | |
d25d5980 DL |
101 | if (!memcmp(ph.magic, HEADER_MAGIC, 16)) { |
102 | s->off_multiplier = 1; | |
103 | bs->total_sectors = 0xffffffff & bs->total_sectors; | |
104 | } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) { | |
105 | s->off_multiplier = le32_to_cpu(ph.tracks); | |
106 | } else { | |
418a7adb | 107 | goto fail_format; |
6ada7453 TS |
108 | } |
109 | ||
6ada7453 | 110 | s->tracks = le32_to_cpu(ph.tracks); |
9302e863 KW |
111 | if (s->tracks == 0) { |
112 | error_setg(errp, "Invalid image: Zero sectors per track"); | |
113 | ret = -EINVAL; | |
114 | goto fail; | |
115 | } | |
d25d5980 DL |
116 | if (s->tracks > INT32_MAX/513) { |
117 | error_setg(errp, "Invalid image: Too big cluster"); | |
118 | ret = -EFBIG; | |
119 | goto fail; | |
120 | } | |
6ada7453 TS |
121 | |
122 | s->catalog_size = le32_to_cpu(ph.catalog_entries); | |
afbcc40b KW |
123 | if (s->catalog_size > INT_MAX / 4) { |
124 | error_setg(errp, "Catalog too large"); | |
125 | ret = -EFBIG; | |
126 | goto fail; | |
127 | } | |
02c4f26b | 128 | s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size); |
f7b593d9 KW |
129 | if (s->catalog_size && s->catalog_bitmap == NULL) { |
130 | ret = -ENOMEM; | |
131 | goto fail; | |
132 | } | |
46536235 KW |
133 | |
134 | ret = bdrv_pread(bs->file, 64, s->catalog_bitmap, s->catalog_size * 4); | |
135 | if (ret < 0) { | |
136 | goto fail; | |
137 | } | |
138 | ||
6ada7453 | 139 | for (i = 0; i < s->catalog_size; i++) |
f08e2f84 | 140 | le32_to_cpus(&s->catalog_bitmap[i]); |
6ada7453 | 141 | |
848c66e8 | 142 | qemu_co_mutex_init(&s->lock); |
6ada7453 | 143 | return 0; |
46536235 | 144 | |
418a7adb DL |
145 | fail_format: |
146 | error_setg(errp, "Image not in Parallels format"); | |
147 | ret = -EINVAL; | |
6ada7453 | 148 | fail: |
46536235 KW |
149 | g_free(s->catalog_bitmap); |
150 | return ret; | |
6ada7453 TS |
151 | } |
152 | ||
29442569 | 153 | static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num) |
6ada7453 | 154 | { |
c34d2451 | 155 | uint32_t index, offset; |
6ada7453 TS |
156 | |
157 | index = sector_num / s->tracks; | |
158 | offset = sector_num % s->tracks; | |
159 | ||
9d8b88f6 | 160 | /* not allocated */ |
da725d0b | 161 | if ((index >= s->catalog_size) || (s->catalog_bitmap[index] == 0)) |
f08e2f84 | 162 | return -1; |
29442569 | 163 | return (uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset; |
6ada7453 TS |
164 | } |
165 | ||
9de9da17 RK |
166 | static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, |
167 | int nb_sectors) | |
168 | { | |
169 | int ret = s->tracks - sector_num % s->tracks; | |
170 | return MIN(nb_sectors, ret); | |
171 | } | |
172 | ||
dd3bed16 RK |
173 | static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs, |
174 | int64_t sector_num, int nb_sectors, int *pnum) | |
175 | { | |
176 | BDRVParallelsState *s = bs->opaque; | |
177 | int64_t offset; | |
178 | ||
179 | qemu_co_mutex_lock(&s->lock); | |
180 | offset = seek_to_sector(s, sector_num); | |
181 | qemu_co_mutex_unlock(&s->lock); | |
182 | ||
183 | *pnum = cluster_remainder(s, sector_num, nb_sectors); | |
184 | ||
185 | if (offset < 0) { | |
186 | return 0; | |
187 | } | |
188 | ||
189 | return (offset << BDRV_SECTOR_BITS) | | |
190 | BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
191 | } | |
192 | ||
481fb9cf DL |
193 | static coroutine_fn int parallels_co_readv(BlockDriverState *bs, |
194 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) | |
6ada7453 | 195 | { |
29442569 | 196 | BDRVParallelsState *s = bs->opaque; |
481fb9cf DL |
197 | uint64_t bytes_done = 0; |
198 | QEMUIOVector hd_qiov; | |
199 | int ret = 0; | |
200 | ||
201 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
29442569 | 202 | |
6ada7453 | 203 | while (nb_sectors > 0) { |
481fb9cf DL |
204 | int64_t position; |
205 | int n, nbytes; | |
206 | ||
207 | qemu_co_mutex_lock(&s->lock); | |
208 | position = seek_to_sector(s, sector_num); | |
209 | qemu_co_mutex_unlock(&s->lock); | |
210 | ||
211 | n = cluster_remainder(s, sector_num, nb_sectors); | |
212 | nbytes = n << BDRV_SECTOR_BITS; | |
213 | ||
214 | if (position < 0) { | |
215 | qemu_iovec_memset(qiov, bytes_done, 0, nbytes); | |
216 | } else { | |
217 | qemu_iovec_reset(&hd_qiov); | |
218 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); | |
219 | ||
220 | ret = bdrv_co_readv(bs->file, position, n, &hd_qiov); | |
29442569 | 221 | if (ret < 0) { |
481fb9cf | 222 | break; |
29442569 | 223 | } |
9d8b88f6 | 224 | } |
481fb9cf | 225 | |
9de9da17 RK |
226 | nb_sectors -= n; |
227 | sector_num += n; | |
481fb9cf | 228 | bytes_done += nbytes; |
6ada7453 | 229 | } |
6ada7453 | 230 | |
481fb9cf | 231 | qemu_iovec_destroy(&hd_qiov); |
2914caa0 PB |
232 | return ret; |
233 | } | |
234 | ||
6ada7453 TS |
235 | static void parallels_close(BlockDriverState *bs) |
236 | { | |
237 | BDRVParallelsState *s = bs->opaque; | |
7267c094 | 238 | g_free(s->catalog_bitmap); |
6ada7453 TS |
239 | } |
240 | ||
5efa9d5a | 241 | static BlockDriver bdrv_parallels = { |
e60f469c AJ |
242 | .format_name = "parallels", |
243 | .instance_size = sizeof(BDRVParallelsState), | |
244 | .bdrv_probe = parallels_probe, | |
1dec5a70 | 245 | .bdrv_open = parallels_open, |
e60f469c | 246 | .bdrv_close = parallels_close, |
dd3bed16 | 247 | .bdrv_co_get_block_status = parallels_co_get_block_status, |
481fb9cf | 248 | .bdrv_co_readv = parallels_co_readv, |
6ada7453 | 249 | }; |
5efa9d5a AL |
250 | |
251 | static void bdrv_parallels_init(void) | |
252 | { | |
253 | bdrv_register(&bdrv_parallels); | |
254 | } | |
255 | ||
256 | block_init(bdrv_parallels_init); |