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