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