]>
Commit | Line | Data |
---|---|---|
6ada7453 TS |
1 | /* |
2 | * Block driver for Parallels disk image format | |
3 | * | |
4 | * Copyright (c) 2007 Alex Beregszaszi | |
cc5690f2 | 5 | * Copyright (c) 2015 Denis V. Lunev <den@openvz.org> |
6ada7453 | 6 | * |
cc5690f2 DL |
7 | * This code was originally based on comparing different disk images created |
8 | * by Parallels. Currently it is based on opened OpenVZ sources | |
9 | * available at | |
10 | * http://git.openvz.org/?p=ploop;a=summary | |
6ada7453 TS |
11 | * |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
13 | * of this software and associated documentation files (the "Software"), to deal | |
14 | * in the Software without restriction, including without limitation the rights | |
15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
16 | * copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
18 | * | |
19 | * The above copyright notice and this permission notice shall be included in | |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
25 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
28 | * THE SOFTWARE. | |
29 | */ | |
80c71a24 | 30 | #include "qemu/osdep.h" |
da34e65c | 31 | #include "qapi/error.h" |
faf07963 | 32 | #include "qemu-common.h" |
737e150e | 33 | #include "block/block_int.h" |
8942764f | 34 | #include "sysemu/block-backend.h" |
1de7afc9 | 35 | #include "qemu/module.h" |
58369e22 | 36 | #include "qemu/bswap.h" |
0d31c7c2 | 37 | #include "qemu/bitmap.h" |
d6179011 | 38 | #include "qapi/util.h" |
6ada7453 TS |
39 | |
40 | /**************************************************************/ | |
41 | ||
42 | #define HEADER_MAGIC "WithoutFreeSpace" | |
d25d5980 | 43 | #define HEADER_MAGIC2 "WithouFreSpacExt" |
6ada7453 | 44 | #define HEADER_VERSION 2 |
6dd6b9f1 | 45 | #define HEADER_INUSE_MAGIC (0x746F6E59) |
6ada7453 | 46 | |
74cf6c50 DL |
47 | #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */ |
48 | ||
49 | ||
6ada7453 | 50 | // always little-endian |
07898904 | 51 | typedef struct ParallelsHeader { |
6ada7453 TS |
52 | char magic[16]; // "WithoutFreeSpace" |
53 | uint32_t version; | |
54 | uint32_t heads; | |
55 | uint32_t cylinders; | |
56 | uint32_t tracks; | |
369f7de9 | 57 | uint32_t bat_entries; |
8c27d54f DL |
58 | uint64_t nb_sectors; |
59 | uint32_t inuse; | |
60 | uint32_t data_off; | |
61 | char padding[12]; | |
07898904 | 62 | } QEMU_PACKED ParallelsHeader; |
6ada7453 | 63 | |
d6179011 DL |
64 | |
65 | typedef enum ParallelsPreallocMode { | |
66 | PRL_PREALLOC_MODE_FALLOCATE = 0, | |
67 | PRL_PREALLOC_MODE_TRUNCATE = 1, | |
7fb1cf16 | 68 | PRL_PREALLOC_MODE__MAX = 2, |
d6179011 DL |
69 | } ParallelsPreallocMode; |
70 | ||
71 | static const char *prealloc_mode_lookup[] = { | |
72 | "falloc", | |
73 | "truncate", | |
74 | NULL, | |
75 | }; | |
76 | ||
77 | ||
6ada7453 | 78 | typedef struct BDRVParallelsState { |
481fb9cf DL |
79 | /** Locking is conservative, the lock protects |
80 | * - image file extending (truncate, fallocate) | |
81 | * - any access to block allocation table | |
82 | */ | |
848c66e8 | 83 | CoMutex lock; |
6ada7453 | 84 | |
9eae9cca DL |
85 | ParallelsHeader *header; |
86 | uint32_t header_size; | |
6dd6b9f1 DL |
87 | bool header_unclean; |
88 | ||
0d31c7c2 DL |
89 | unsigned long *bat_dirty_bmap; |
90 | unsigned int bat_dirty_block; | |
91 | ||
369f7de9 DL |
92 | uint32_t *bat_bitmap; |
93 | unsigned int bat_size; | |
6ada7453 | 94 | |
19f5dc15 | 95 | int64_t data_end; |
d6179011 DL |
96 | uint64_t prealloc_size; |
97 | ParallelsPreallocMode prealloc_mode; | |
98 | ||
9302e863 | 99 | unsigned int tracks; |
d25d5980 DL |
100 | |
101 | unsigned int off_multiplier; | |
6ada7453 TS |
102 | } BDRVParallelsState; |
103 | ||
555cc9d9 | 104 | |
d6179011 DL |
105 | #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode" |
106 | #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size" | |
107 | ||
108 | static QemuOptsList parallels_runtime_opts = { | |
109 | .name = "parallels", | |
110 | .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head), | |
111 | .desc = { | |
112 | { | |
113 | .name = PARALLELS_OPT_PREALLOC_SIZE, | |
114 | .type = QEMU_OPT_SIZE, | |
115 | .help = "Preallocation size on image expansion", | |
116 | .def_value_str = "128MiB", | |
117 | }, | |
118 | { | |
119 | .name = PARALLELS_OPT_PREALLOC_MODE, | |
120 | .type = QEMU_OPT_STRING, | |
121 | .help = "Preallocation mode on image expansion " | |
122 | "(allowed values: falloc, truncate)", | |
123 | .def_value_str = "falloc", | |
124 | }, | |
125 | { /* end of list */ }, | |
126 | }, | |
127 | }; | |
128 | ||
129 | ||
555cc9d9 DL |
130 | static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx) |
131 | { | |
dd97cdc0 | 132 | return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier; |
555cc9d9 DL |
133 | } |
134 | ||
2d68e22e DL |
135 | static uint32_t bat_entry_off(uint32_t idx) |
136 | { | |
137 | return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx; | |
138 | } | |
139 | ||
29442569 | 140 | static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num) |
6ada7453 | 141 | { |
c34d2451 | 142 | uint32_t index, offset; |
6ada7453 TS |
143 | |
144 | index = sector_num / s->tracks; | |
145 | offset = sector_num % s->tracks; | |
146 | ||
9d8b88f6 | 147 | /* not allocated */ |
369f7de9 | 148 | if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) { |
f08e2f84 | 149 | return -1; |
369f7de9 | 150 | } |
555cc9d9 | 151 | return bat2sect(s, index) + offset; |
6ada7453 TS |
152 | } |
153 | ||
9de9da17 RK |
154 | static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, |
155 | int nb_sectors) | |
156 | { | |
157 | int ret = s->tracks - sector_num % s->tracks; | |
158 | return MIN(nb_sectors, ret); | |
159 | } | |
160 | ||
6953d920 DL |
161 | static int64_t block_status(BDRVParallelsState *s, int64_t sector_num, |
162 | int nb_sectors, int *pnum) | |
163 | { | |
164 | int64_t start_off = -2, prev_end_off = -2; | |
165 | ||
166 | *pnum = 0; | |
167 | while (nb_sectors > 0 || start_off == -2) { | |
168 | int64_t offset = seek_to_sector(s, sector_num); | |
169 | int to_end; | |
170 | ||
171 | if (start_off == -2) { | |
172 | start_off = offset; | |
173 | prev_end_off = offset; | |
174 | } else if (offset != prev_end_off) { | |
175 | break; | |
176 | } | |
177 | ||
178 | to_end = cluster_remainder(s, sector_num, nb_sectors); | |
179 | nb_sectors -= to_end; | |
180 | sector_num += to_end; | |
181 | *pnum += to_end; | |
182 | ||
183 | if (offset > 0) { | |
184 | prev_end_off += to_end; | |
185 | } | |
186 | } | |
187 | return start_off; | |
188 | } | |
189 | ||
ddd2ef2c DL |
190 | static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num, |
191 | int nb_sectors, int *pnum) | |
5a41e1fa DL |
192 | { |
193 | BDRVParallelsState *s = bs->opaque; | |
ddd2ef2c DL |
194 | uint32_t idx, to_allocate, i; |
195 | int64_t pos, space; | |
5a41e1fa | 196 | |
ddd2ef2c DL |
197 | pos = block_status(s, sector_num, nb_sectors, pnum); |
198 | if (pos > 0) { | |
199 | return pos; | |
200 | } | |
5a41e1fa | 201 | |
ddd2ef2c | 202 | idx = sector_num / s->tracks; |
369f7de9 | 203 | if (idx >= s->bat_size) { |
5a41e1fa DL |
204 | return -EINVAL; |
205 | } | |
5a41e1fa | 206 | |
969401fe | 207 | to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx; |
ddd2ef2c | 208 | space = to_allocate * s->tracks; |
9a4f4c31 | 209 | if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) { |
19f5dc15 | 210 | int ret; |
ddd2ef2c | 211 | space += s->prealloc_size; |
19f5dc15 | 212 | if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) { |
74021bc4 EB |
213 | ret = bdrv_pwrite_zeroes(bs->file->bs, |
214 | s->data_end << BDRV_SECTOR_BITS, | |
215 | space << BDRV_SECTOR_BITS, 0); | |
19f5dc15 | 216 | } else { |
9a4f4c31 | 217 | ret = bdrv_truncate(bs->file->bs, |
ddd2ef2c | 218 | (s->data_end + space) << BDRV_SECTOR_BITS); |
19f5dc15 DL |
219 | } |
220 | if (ret < 0) { | |
221 | return ret; | |
222 | } | |
5a41e1fa DL |
223 | } |
224 | ||
ddd2ef2c DL |
225 | for (i = 0; i < to_allocate; i++) { |
226 | s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier); | |
227 | s->data_end += s->tracks; | |
228 | bitmap_set(s->bat_dirty_bmap, | |
c9f6856d | 229 | bat_entry_off(idx + i) / s->bat_dirty_block, 1); |
ddd2ef2c | 230 | } |
0d31c7c2 | 231 | |
ddd2ef2c | 232 | return bat2sect(s, idx) + sector_num % s->tracks; |
5a41e1fa DL |
233 | } |
234 | ||
0d31c7c2 DL |
235 | |
236 | static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs) | |
237 | { | |
238 | BDRVParallelsState *s = bs->opaque; | |
239 | unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block); | |
240 | unsigned long bit; | |
241 | ||
242 | qemu_co_mutex_lock(&s->lock); | |
243 | ||
244 | bit = find_first_bit(s->bat_dirty_bmap, size); | |
245 | while (bit < size) { | |
246 | uint32_t off = bit * s->bat_dirty_block; | |
247 | uint32_t to_write = s->bat_dirty_block; | |
248 | int ret; | |
249 | ||
250 | if (off + to_write > s->header_size) { | |
251 | to_write = s->header_size - off; | |
252 | } | |
9a4f4c31 KW |
253 | ret = bdrv_pwrite(bs->file->bs, off, (uint8_t *)s->header + off, |
254 | to_write); | |
0d31c7c2 DL |
255 | if (ret < 0) { |
256 | qemu_co_mutex_unlock(&s->lock); | |
257 | return ret; | |
258 | } | |
259 | bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1); | |
260 | } | |
261 | bitmap_zero(s->bat_dirty_bmap, size); | |
262 | ||
263 | qemu_co_mutex_unlock(&s->lock); | |
264 | return 0; | |
265 | } | |
266 | ||
267 | ||
dd3bed16 | 268 | static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs, |
67a0fd2a | 269 | int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file) |
dd3bed16 RK |
270 | { |
271 | BDRVParallelsState *s = bs->opaque; | |
272 | int64_t offset; | |
273 | ||
274 | qemu_co_mutex_lock(&s->lock); | |
6953d920 | 275 | offset = block_status(s, sector_num, nb_sectors, pnum); |
dd3bed16 RK |
276 | qemu_co_mutex_unlock(&s->lock); |
277 | ||
dd3bed16 RK |
278 | if (offset < 0) { |
279 | return 0; | |
280 | } | |
281 | ||
ddf4987d | 282 | *file = bs->file->bs; |
dd3bed16 RK |
283 | return (offset << BDRV_SECTOR_BITS) | |
284 | BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
285 | } | |
286 | ||
5a41e1fa DL |
287 | static coroutine_fn int parallels_co_writev(BlockDriverState *bs, |
288 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) | |
289 | { | |
290 | BDRVParallelsState *s = bs->opaque; | |
291 | uint64_t bytes_done = 0; | |
292 | QEMUIOVector hd_qiov; | |
293 | int ret = 0; | |
294 | ||
295 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
296 | ||
297 | while (nb_sectors > 0) { | |
298 | int64_t position; | |
299 | int n, nbytes; | |
300 | ||
301 | qemu_co_mutex_lock(&s->lock); | |
ddd2ef2c | 302 | position = allocate_clusters(bs, sector_num, nb_sectors, &n); |
5a41e1fa DL |
303 | qemu_co_mutex_unlock(&s->lock); |
304 | if (position < 0) { | |
305 | ret = (int)position; | |
306 | break; | |
307 | } | |
308 | ||
5a41e1fa DL |
309 | nbytes = n << BDRV_SECTOR_BITS; |
310 | ||
311 | qemu_iovec_reset(&hd_qiov); | |
312 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); | |
313 | ||
9a4f4c31 | 314 | ret = bdrv_co_writev(bs->file->bs, position, n, &hd_qiov); |
5a41e1fa DL |
315 | if (ret < 0) { |
316 | break; | |
317 | } | |
318 | ||
319 | nb_sectors -= n; | |
320 | sector_num += n; | |
321 | bytes_done += nbytes; | |
322 | } | |
323 | ||
324 | qemu_iovec_destroy(&hd_qiov); | |
325 | return ret; | |
326 | } | |
327 | ||
481fb9cf DL |
328 | static coroutine_fn int parallels_co_readv(BlockDriverState *bs, |
329 | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) | |
6ada7453 | 330 | { |
29442569 | 331 | BDRVParallelsState *s = bs->opaque; |
481fb9cf DL |
332 | uint64_t bytes_done = 0; |
333 | QEMUIOVector hd_qiov; | |
334 | int ret = 0; | |
335 | ||
336 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
29442569 | 337 | |
6ada7453 | 338 | while (nb_sectors > 0) { |
481fb9cf DL |
339 | int64_t position; |
340 | int n, nbytes; | |
341 | ||
342 | qemu_co_mutex_lock(&s->lock); | |
6953d920 | 343 | position = block_status(s, sector_num, nb_sectors, &n); |
481fb9cf DL |
344 | qemu_co_mutex_unlock(&s->lock); |
345 | ||
481fb9cf DL |
346 | nbytes = n << BDRV_SECTOR_BITS; |
347 | ||
348 | if (position < 0) { | |
349 | qemu_iovec_memset(qiov, bytes_done, 0, nbytes); | |
350 | } else { | |
351 | qemu_iovec_reset(&hd_qiov); | |
352 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); | |
353 | ||
9a4f4c31 | 354 | ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov); |
29442569 | 355 | if (ret < 0) { |
481fb9cf | 356 | break; |
29442569 | 357 | } |
9d8b88f6 | 358 | } |
481fb9cf | 359 | |
9de9da17 RK |
360 | nb_sectors -= n; |
361 | sector_num += n; | |
481fb9cf | 362 | bytes_done += nbytes; |
6ada7453 | 363 | } |
6ada7453 | 364 | |
481fb9cf | 365 | qemu_iovec_destroy(&hd_qiov); |
2914caa0 PB |
366 | return ret; |
367 | } | |
368 | ||
49ad6467 DL |
369 | |
370 | static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res, | |
371 | BdrvCheckMode fix) | |
372 | { | |
373 | BDRVParallelsState *s = bs->opaque; | |
374 | int64_t size, prev_off, high_off; | |
375 | int ret; | |
376 | uint32_t i; | |
377 | bool flush_bat = false; | |
378 | int cluster_size = s->tracks << BDRV_SECTOR_BITS; | |
379 | ||
9a4f4c31 | 380 | size = bdrv_getlength(bs->file->bs); |
49ad6467 DL |
381 | if (size < 0) { |
382 | res->check_errors++; | |
383 | return size; | |
384 | } | |
385 | ||
6dd6b9f1 DL |
386 | if (s->header_unclean) { |
387 | fprintf(stderr, "%s image was not closed correctly\n", | |
388 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR"); | |
389 | res->corruptions++; | |
390 | if (fix & BDRV_FIX_ERRORS) { | |
391 | /* parallels_close will do the job right */ | |
392 | res->corruptions_fixed++; | |
393 | s->header_unclean = false; | |
394 | } | |
395 | } | |
396 | ||
49ad6467 DL |
397 | res->bfi.total_clusters = s->bat_size; |
398 | res->bfi.compressed_clusters = 0; /* compression is not supported */ | |
399 | ||
400 | high_off = 0; | |
401 | prev_off = 0; | |
402 | for (i = 0; i < s->bat_size; i++) { | |
403 | int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS; | |
404 | if (off == 0) { | |
405 | prev_off = 0; | |
406 | continue; | |
407 | } | |
408 | ||
409 | /* cluster outside the image */ | |
410 | if (off > size) { | |
411 | fprintf(stderr, "%s cluster %u is outside image\n", | |
412 | fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i); | |
413 | res->corruptions++; | |
414 | if (fix & BDRV_FIX_ERRORS) { | |
415 | prev_off = 0; | |
416 | s->bat_bitmap[i] = 0; | |
417 | res->corruptions_fixed++; | |
418 | flush_bat = true; | |
419 | continue; | |
420 | } | |
421 | } | |
422 | ||
423 | res->bfi.allocated_clusters++; | |
424 | if (off > high_off) { | |
425 | high_off = off; | |
426 | } | |
427 | ||
428 | if (prev_off != 0 && (prev_off + cluster_size) != off) { | |
429 | res->bfi.fragmented_clusters++; | |
430 | } | |
431 | prev_off = off; | |
432 | } | |
433 | ||
434 | if (flush_bat) { | |
9a4f4c31 | 435 | ret = bdrv_pwrite_sync(bs->file->bs, 0, s->header, s->header_size); |
49ad6467 DL |
436 | if (ret < 0) { |
437 | res->check_errors++; | |
438 | return ret; | |
439 | } | |
440 | } | |
441 | ||
442 | res->image_end_offset = high_off + cluster_size; | |
443 | if (size > res->image_end_offset) { | |
444 | int64_t count; | |
445 | count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size); | |
446 | fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n", | |
447 | fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR", | |
448 | size - res->image_end_offset); | |
449 | res->leaks += count; | |
450 | if (fix & BDRV_FIX_LEAKS) { | |
9a4f4c31 | 451 | ret = bdrv_truncate(bs->file->bs, res->image_end_offset); |
49ad6467 DL |
452 | if (ret < 0) { |
453 | res->check_errors++; | |
454 | return ret; | |
455 | } | |
456 | res->leaks_fixed += count; | |
457 | } | |
458 | } | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | ||
74cf6c50 DL |
464 | static int parallels_create(const char *filename, QemuOpts *opts, Error **errp) |
465 | { | |
466 | int64_t total_size, cl_size; | |
467 | uint8_t tmp[BDRV_SECTOR_SIZE]; | |
468 | Error *local_err = NULL; | |
8942764f | 469 | BlockBackend *file; |
369f7de9 | 470 | uint32_t bat_entries, bat_sectors; |
74cf6c50 DL |
471 | ParallelsHeader header; |
472 | int ret; | |
473 | ||
474 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), | |
475 | BDRV_SECTOR_SIZE); | |
476 | cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, | |
477 | DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE); | |
478 | ||
479 | ret = bdrv_create_file(filename, opts, &local_err); | |
480 | if (ret < 0) { | |
481 | error_propagate(errp, local_err); | |
482 | return ret; | |
483 | } | |
484 | ||
efaa7c4e | 485 | file = blk_new_open(filename, NULL, NULL, |
72e775c7 | 486 | BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err); |
8942764f | 487 | if (file == NULL) { |
74cf6c50 | 488 | error_propagate(errp, local_err); |
8942764f | 489 | return -EIO; |
74cf6c50 | 490 | } |
8942764f KW |
491 | |
492 | blk_set_allow_write_beyond_eof(file, true); | |
493 | ||
494 | ret = blk_truncate(file, 0); | |
74cf6c50 DL |
495 | if (ret < 0) { |
496 | goto exit; | |
497 | } | |
498 | ||
369f7de9 | 499 | bat_entries = DIV_ROUND_UP(total_size, cl_size); |
2d68e22e | 500 | bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size); |
369f7de9 | 501 | bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS; |
74cf6c50 DL |
502 | |
503 | memset(&header, 0, sizeof(header)); | |
504 | memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic)); | |
505 | header.version = cpu_to_le32(HEADER_VERSION); | |
506 | /* don't care much about geometry, it is not used on image level */ | |
507 | header.heads = cpu_to_le32(16); | |
508 | header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32); | |
509 | header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS); | |
369f7de9 | 510 | header.bat_entries = cpu_to_le32(bat_entries); |
74cf6c50 | 511 | header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE)); |
369f7de9 | 512 | header.data_off = cpu_to_le32(bat_sectors); |
74cf6c50 DL |
513 | |
514 | /* write all the data */ | |
515 | memset(tmp, 0, sizeof(tmp)); | |
516 | memcpy(tmp, &header, sizeof(header)); | |
517 | ||
8341f00d | 518 | ret = blk_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE, 0); |
74cf6c50 DL |
519 | if (ret < 0) { |
520 | goto exit; | |
521 | } | |
d004bd52 EB |
522 | ret = blk_pwrite_zeroes(file, BDRV_SECTOR_SIZE, |
523 | (bat_sectors - 1) << BDRV_SECTOR_BITS, 0); | |
74cf6c50 DL |
524 | if (ret < 0) { |
525 | goto exit; | |
526 | } | |
527 | ret = 0; | |
528 | ||
529 | done: | |
8942764f | 530 | blk_unref(file); |
74cf6c50 DL |
531 | return ret; |
532 | ||
533 | exit: | |
534 | error_setg_errno(errp, -ret, "Failed to create Parallels image"); | |
535 | goto done; | |
536 | } | |
537 | ||
23d6bd3b DL |
538 | |
539 | static int parallels_probe(const uint8_t *buf, int buf_size, | |
540 | const char *filename) | |
541 | { | |
542 | const ParallelsHeader *ph = (const void *)buf; | |
543 | ||
544 | if (buf_size < sizeof(ParallelsHeader)) { | |
545 | return 0; | |
546 | } | |
547 | ||
548 | if ((!memcmp(ph->magic, HEADER_MAGIC, 16) || | |
549 | !memcmp(ph->magic, HEADER_MAGIC2, 16)) && | |
550 | (le32_to_cpu(ph->version) == HEADER_VERSION)) { | |
551 | return 100; | |
552 | } | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
6dd6b9f1 DL |
557 | static int parallels_update_header(BlockDriverState *bs) |
558 | { | |
559 | BDRVParallelsState *s = bs->opaque; | |
9a4f4c31 KW |
560 | unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs), |
561 | sizeof(ParallelsHeader)); | |
6dd6b9f1 DL |
562 | |
563 | if (size > s->header_size) { | |
564 | size = s->header_size; | |
565 | } | |
9a4f4c31 | 566 | return bdrv_pwrite_sync(bs->file->bs, 0, s->header, size); |
6dd6b9f1 DL |
567 | } |
568 | ||
23d6bd3b DL |
569 | static int parallels_open(BlockDriverState *bs, QDict *options, int flags, |
570 | Error **errp) | |
571 | { | |
572 | BDRVParallelsState *s = bs->opaque; | |
573 | ParallelsHeader ph; | |
19f5dc15 | 574 | int ret, size, i; |
d6179011 DL |
575 | QemuOpts *opts = NULL; |
576 | Error *local_err = NULL; | |
577 | char *buf; | |
23d6bd3b | 578 | |
9a4f4c31 | 579 | ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph)); |
23d6bd3b DL |
580 | if (ret < 0) { |
581 | goto fail; | |
582 | } | |
583 | ||
584 | bs->total_sectors = le64_to_cpu(ph.nb_sectors); | |
585 | ||
586 | if (le32_to_cpu(ph.version) != HEADER_VERSION) { | |
587 | goto fail_format; | |
588 | } | |
589 | if (!memcmp(ph.magic, HEADER_MAGIC, 16)) { | |
590 | s->off_multiplier = 1; | |
591 | bs->total_sectors = 0xffffffff & bs->total_sectors; | |
592 | } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) { | |
593 | s->off_multiplier = le32_to_cpu(ph.tracks); | |
594 | } else { | |
595 | goto fail_format; | |
596 | } | |
597 | ||
598 | s->tracks = le32_to_cpu(ph.tracks); | |
599 | if (s->tracks == 0) { | |
600 | error_setg(errp, "Invalid image: Zero sectors per track"); | |
601 | ret = -EINVAL; | |
602 | goto fail; | |
603 | } | |
604 | if (s->tracks > INT32_MAX/513) { | |
605 | error_setg(errp, "Invalid image: Too big cluster"); | |
606 | ret = -EFBIG; | |
607 | goto fail; | |
608 | } | |
609 | ||
610 | s->bat_size = le32_to_cpu(ph.bat_entries); | |
611 | if (s->bat_size > INT_MAX / sizeof(uint32_t)) { | |
612 | error_setg(errp, "Catalog too large"); | |
613 | ret = -EFBIG; | |
614 | goto fail; | |
615 | } | |
616 | ||
2d68e22e | 617 | size = bat_entry_off(s->bat_size); |
9a4f4c31 KW |
618 | s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs)); |
619 | s->header = qemu_try_blockalign(bs->file->bs, s->header_size); | |
23d6bd3b DL |
620 | if (s->header == NULL) { |
621 | ret = -ENOMEM; | |
622 | goto fail; | |
623 | } | |
19f5dc15 DL |
624 | s->data_end = le32_to_cpu(ph.data_off); |
625 | if (s->data_end == 0) { | |
626 | s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); | |
627 | } | |
628 | if (s->data_end < s->header_size) { | |
23d6bd3b DL |
629 | /* there is not enough unused space to fit to block align between BAT |
630 | and actual data. We can't avoid read-modify-write... */ | |
631 | s->header_size = size; | |
632 | } | |
633 | ||
9a4f4c31 | 634 | ret = bdrv_pread(bs->file->bs, 0, s->header, s->header_size); |
23d6bd3b DL |
635 | if (ret < 0) { |
636 | goto fail; | |
637 | } | |
638 | s->bat_bitmap = (uint32_t *)(s->header + 1); | |
639 | ||
19f5dc15 DL |
640 | for (i = 0; i < s->bat_size; i++) { |
641 | int64_t off = bat2sect(s, i); | |
642 | if (off >= s->data_end) { | |
643 | s->data_end = off + s->tracks; | |
644 | } | |
645 | } | |
646 | ||
6dd6b9f1 DL |
647 | if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) { |
648 | /* Image was not closed correctly. The check is mandatory */ | |
649 | s->header_unclean = true; | |
650 | if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { | |
651 | error_setg(errp, "parallels: Image was not closed correctly; " | |
652 | "cannot be opened read/write"); | |
653 | ret = -EACCES; | |
654 | goto fail; | |
655 | } | |
656 | } | |
657 | ||
d6179011 DL |
658 | opts = qemu_opts_create(¶llels_runtime_opts, NULL, 0, &local_err); |
659 | if (local_err != NULL) { | |
660 | goto fail_options; | |
661 | } | |
662 | ||
663 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
664 | if (local_err != NULL) { | |
665 | goto fail_options; | |
666 | } | |
667 | ||
668 | s->prealloc_size = | |
669 | qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0); | |
670 | s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS); | |
671 | buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE); | |
672 | s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf, | |
7fb1cf16 | 673 | PRL_PREALLOC_MODE__MAX, PRL_PREALLOC_MODE_FALLOCATE, &local_err); |
d6179011 DL |
674 | g_free(buf); |
675 | if (local_err != NULL) { | |
676 | goto fail_options; | |
677 | } | |
9a4f4c31 KW |
678 | if (!bdrv_has_zero_init(bs->file->bs) || |
679 | bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 0) { | |
d6179011 DL |
680 | s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE; |
681 | } | |
682 | ||
6dd6b9f1 DL |
683 | if (flags & BDRV_O_RDWR) { |
684 | s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC); | |
685 | ret = parallels_update_header(bs); | |
686 | if (ret < 0) { | |
687 | goto fail; | |
688 | } | |
689 | } | |
690 | ||
0d31c7c2 DL |
691 | s->bat_dirty_block = 4 * getpagesize(); |
692 | s->bat_dirty_bmap = | |
693 | bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block)); | |
694 | ||
23d6bd3b DL |
695 | qemu_co_mutex_init(&s->lock); |
696 | return 0; | |
697 | ||
698 | fail_format: | |
699 | error_setg(errp, "Image not in Parallels format"); | |
700 | ret = -EINVAL; | |
701 | fail: | |
702 | qemu_vfree(s->header); | |
703 | return ret; | |
d6179011 DL |
704 | |
705 | fail_options: | |
706 | error_propagate(errp, local_err); | |
707 | ret = -EINVAL; | |
708 | goto fail; | |
23d6bd3b DL |
709 | } |
710 | ||
711 | ||
6ada7453 TS |
712 | static void parallels_close(BlockDriverState *bs) |
713 | { | |
714 | BDRVParallelsState *s = bs->opaque; | |
6dd6b9f1 DL |
715 | |
716 | if (bs->open_flags & BDRV_O_RDWR) { | |
717 | s->header->inuse = 0; | |
718 | parallels_update_header(bs); | |
719 | } | |
720 | ||
19f5dc15 | 721 | if (bs->open_flags & BDRV_O_RDWR) { |
9a4f4c31 | 722 | bdrv_truncate(bs->file->bs, s->data_end << BDRV_SECTOR_BITS); |
19f5dc15 DL |
723 | } |
724 | ||
0d31c7c2 | 725 | g_free(s->bat_dirty_bmap); |
9eae9cca | 726 | qemu_vfree(s->header); |
6ada7453 TS |
727 | } |
728 | ||
74cf6c50 DL |
729 | static QemuOptsList parallels_create_opts = { |
730 | .name = "parallels-create-opts", | |
731 | .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head), | |
732 | .desc = { | |
733 | { | |
734 | .name = BLOCK_OPT_SIZE, | |
735 | .type = QEMU_OPT_SIZE, | |
736 | .help = "Virtual disk size", | |
737 | }, | |
738 | { | |
739 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
740 | .type = QEMU_OPT_SIZE, | |
741 | .help = "Parallels image cluster size", | |
742 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE), | |
743 | }, | |
744 | { /* end of list */ } | |
745 | } | |
746 | }; | |
747 | ||
5efa9d5a | 748 | static BlockDriver bdrv_parallels = { |
e60f469c AJ |
749 | .format_name = "parallels", |
750 | .instance_size = sizeof(BDRVParallelsState), | |
751 | .bdrv_probe = parallels_probe, | |
1dec5a70 | 752 | .bdrv_open = parallels_open, |
e60f469c | 753 | .bdrv_close = parallels_close, |
dd3bed16 | 754 | .bdrv_co_get_block_status = parallels_co_get_block_status, |
d0e61ce5 | 755 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
0d31c7c2 | 756 | .bdrv_co_flush_to_os = parallels_co_flush_to_os, |
481fb9cf | 757 | .bdrv_co_readv = parallels_co_readv, |
5a41e1fa | 758 | .bdrv_co_writev = parallels_co_writev, |
74cf6c50 DL |
759 | |
760 | .bdrv_create = parallels_create, | |
49ad6467 | 761 | .bdrv_check = parallels_check, |
74cf6c50 | 762 | .create_opts = ¶llels_create_opts, |
6ada7453 | 763 | }; |
5efa9d5a AL |
764 | |
765 | static void bdrv_parallels_init(void) | |
766 | { | |
767 | bdrv_register(&bdrv_parallels); | |
768 | } | |
769 | ||
770 | block_init(bdrv_parallels_init); |