]> git.proxmox.com Git - mirror_qemu.git/blob - block/parallels.c
block/parallels: improve image reading performance
[mirror_qemu.git] / block / parallels.c
1 /*
2 * Block driver for Parallels disk image format
3 *
4 * Copyright (c) 2007 Alex Beregszaszi
5 * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
6 *
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
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 */
30 #include "qemu-common.h"
31 #include "block/block_int.h"
32 #include "qemu/module.h"
33
34 /**************************************************************/
35
36 #define HEADER_MAGIC "WithoutFreeSpace"
37 #define HEADER_MAGIC2 "WithouFreSpacExt"
38 #define HEADER_VERSION 2
39 #define HEADER_INUSE_MAGIC (0x746F6E59)
40
41 #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
42
43
44 // always little-endian
45 typedef struct ParallelsHeader {
46 char magic[16]; // "WithoutFreeSpace"
47 uint32_t version;
48 uint32_t heads;
49 uint32_t cylinders;
50 uint32_t tracks;
51 uint32_t bat_entries;
52 uint64_t nb_sectors;
53 uint32_t inuse;
54 uint32_t data_off;
55 char padding[12];
56 } QEMU_PACKED ParallelsHeader;
57
58 typedef struct BDRVParallelsState {
59 /** Locking is conservative, the lock protects
60 * - image file extending (truncate, fallocate)
61 * - any access to block allocation table
62 */
63 CoMutex lock;
64
65 ParallelsHeader *header;
66 uint32_t header_size;
67 bool header_unclean;
68
69 uint32_t *bat_bitmap;
70 unsigned int bat_size;
71
72 unsigned int tracks;
73
74 unsigned int off_multiplier;
75
76 bool has_truncate;
77 } BDRVParallelsState;
78
79
80 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
81 {
82 return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
83 }
84
85 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
86 {
87 uint32_t index, offset;
88
89 index = sector_num / s->tracks;
90 offset = sector_num % s->tracks;
91
92 /* not allocated */
93 if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
94 return -1;
95 }
96 return bat2sect(s, index) + offset;
97 }
98
99 static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
100 int nb_sectors)
101 {
102 int ret = s->tracks - sector_num % s->tracks;
103 return MIN(nb_sectors, ret);
104 }
105
106 static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
107 int nb_sectors, int *pnum)
108 {
109 int64_t start_off = -2, prev_end_off = -2;
110
111 *pnum = 0;
112 while (nb_sectors > 0 || start_off == -2) {
113 int64_t offset = seek_to_sector(s, sector_num);
114 int to_end;
115
116 if (start_off == -2) {
117 start_off = offset;
118 prev_end_off = offset;
119 } else if (offset != prev_end_off) {
120 break;
121 }
122
123 to_end = cluster_remainder(s, sector_num, nb_sectors);
124 nb_sectors -= to_end;
125 sector_num += to_end;
126 *pnum += to_end;
127
128 if (offset > 0) {
129 prev_end_off += to_end;
130 }
131 }
132 return start_off;
133 }
134
135 static int64_t allocate_cluster(BlockDriverState *bs, int64_t sector_num)
136 {
137 BDRVParallelsState *s = bs->opaque;
138 uint32_t idx, offset;
139 int64_t pos;
140 int ret;
141
142 idx = sector_num / s->tracks;
143 offset = sector_num % s->tracks;
144
145 if (idx >= s->bat_size) {
146 return -EINVAL;
147 }
148 if (s->bat_bitmap[idx] != 0) {
149 return bat2sect(s, idx) + offset;
150 }
151
152 pos = bdrv_getlength(bs->file) >> BDRV_SECTOR_BITS;
153 if (s->has_truncate) {
154 ret = bdrv_truncate(bs->file, (pos + s->tracks) << BDRV_SECTOR_BITS);
155 } else {
156 ret = bdrv_write_zeroes(bs->file, pos, s->tracks, 0);
157 }
158 if (ret < 0) {
159 return ret;
160 }
161
162 s->bat_bitmap[idx] = cpu_to_le32(pos / s->off_multiplier);
163 ret = bdrv_pwrite(bs->file,
164 sizeof(ParallelsHeader) + idx * sizeof(s->bat_bitmap[idx]),
165 s->bat_bitmap + idx, sizeof(s->bat_bitmap[idx]));
166 if (ret < 0) {
167 s->bat_bitmap[idx] = 0;
168 return ret;
169 }
170 return bat2sect(s, idx) + offset;
171 }
172
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 = block_status(s, sector_num, nb_sectors, pnum);
181 qemu_co_mutex_unlock(&s->lock);
182
183 if (offset < 0) {
184 return 0;
185 }
186
187 return (offset << BDRV_SECTOR_BITS) |
188 BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
189 }
190
191 static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
192 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
193 {
194 BDRVParallelsState *s = bs->opaque;
195 uint64_t bytes_done = 0;
196 QEMUIOVector hd_qiov;
197 int ret = 0;
198
199 qemu_iovec_init(&hd_qiov, qiov->niov);
200
201 while (nb_sectors > 0) {
202 int64_t position;
203 int n, nbytes;
204
205 qemu_co_mutex_lock(&s->lock);
206 position = allocate_cluster(bs, sector_num);
207 qemu_co_mutex_unlock(&s->lock);
208 if (position < 0) {
209 ret = (int)position;
210 break;
211 }
212
213 n = cluster_remainder(s, sector_num, nb_sectors);
214 nbytes = n << BDRV_SECTOR_BITS;
215
216 qemu_iovec_reset(&hd_qiov);
217 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
218
219 ret = bdrv_co_writev(bs->file, position, n, &hd_qiov);
220 if (ret < 0) {
221 break;
222 }
223
224 nb_sectors -= n;
225 sector_num += n;
226 bytes_done += nbytes;
227 }
228
229 qemu_iovec_destroy(&hd_qiov);
230 return ret;
231 }
232
233 static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
234 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
235 {
236 BDRVParallelsState *s = bs->opaque;
237 uint64_t bytes_done = 0;
238 QEMUIOVector hd_qiov;
239 int ret = 0;
240
241 qemu_iovec_init(&hd_qiov, qiov->niov);
242
243 while (nb_sectors > 0) {
244 int64_t position;
245 int n, nbytes;
246
247 qemu_co_mutex_lock(&s->lock);
248 position = block_status(s, sector_num, nb_sectors, &n);
249 qemu_co_mutex_unlock(&s->lock);
250
251 nbytes = n << BDRV_SECTOR_BITS;
252
253 if (position < 0) {
254 qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
255 } else {
256 qemu_iovec_reset(&hd_qiov);
257 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
258
259 ret = bdrv_co_readv(bs->file, position, n, &hd_qiov);
260 if (ret < 0) {
261 break;
262 }
263 }
264
265 nb_sectors -= n;
266 sector_num += n;
267 bytes_done += nbytes;
268 }
269
270 qemu_iovec_destroy(&hd_qiov);
271 return ret;
272 }
273
274
275 static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
276 BdrvCheckMode fix)
277 {
278 BDRVParallelsState *s = bs->opaque;
279 int64_t size, prev_off, high_off;
280 int ret;
281 uint32_t i;
282 bool flush_bat = false;
283 int cluster_size = s->tracks << BDRV_SECTOR_BITS;
284
285 size = bdrv_getlength(bs->file);
286 if (size < 0) {
287 res->check_errors++;
288 return size;
289 }
290
291 if (s->header_unclean) {
292 fprintf(stderr, "%s image was not closed correctly\n",
293 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
294 res->corruptions++;
295 if (fix & BDRV_FIX_ERRORS) {
296 /* parallels_close will do the job right */
297 res->corruptions_fixed++;
298 s->header_unclean = false;
299 }
300 }
301
302 res->bfi.total_clusters = s->bat_size;
303 res->bfi.compressed_clusters = 0; /* compression is not supported */
304
305 high_off = 0;
306 prev_off = 0;
307 for (i = 0; i < s->bat_size; i++) {
308 int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
309 if (off == 0) {
310 prev_off = 0;
311 continue;
312 }
313
314 /* cluster outside the image */
315 if (off > size) {
316 fprintf(stderr, "%s cluster %u is outside image\n",
317 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
318 res->corruptions++;
319 if (fix & BDRV_FIX_ERRORS) {
320 prev_off = 0;
321 s->bat_bitmap[i] = 0;
322 res->corruptions_fixed++;
323 flush_bat = true;
324 continue;
325 }
326 }
327
328 res->bfi.allocated_clusters++;
329 if (off > high_off) {
330 high_off = off;
331 }
332
333 if (prev_off != 0 && (prev_off + cluster_size) != off) {
334 res->bfi.fragmented_clusters++;
335 }
336 prev_off = off;
337 }
338
339 if (flush_bat) {
340 ret = bdrv_pwrite_sync(bs->file, 0, s->header, s->header_size);
341 if (ret < 0) {
342 res->check_errors++;
343 return ret;
344 }
345 }
346
347 res->image_end_offset = high_off + cluster_size;
348 if (size > res->image_end_offset) {
349 int64_t count;
350 count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
351 fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
352 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
353 size - res->image_end_offset);
354 res->leaks += count;
355 if (fix & BDRV_FIX_LEAKS) {
356 ret = bdrv_truncate(bs->file, res->image_end_offset);
357 if (ret < 0) {
358 res->check_errors++;
359 return ret;
360 }
361 res->leaks_fixed += count;
362 }
363 }
364
365 return 0;
366 }
367
368
369 static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
370 {
371 int64_t total_size, cl_size;
372 uint8_t tmp[BDRV_SECTOR_SIZE];
373 Error *local_err = NULL;
374 BlockDriverState *file;
375 uint32_t bat_entries, bat_sectors;
376 ParallelsHeader header;
377 int ret;
378
379 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
380 BDRV_SECTOR_SIZE);
381 cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
382 DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
383
384 ret = bdrv_create_file(filename, opts, &local_err);
385 if (ret < 0) {
386 error_propagate(errp, local_err);
387 return ret;
388 }
389
390 file = NULL;
391 ret = bdrv_open(&file, filename, NULL, NULL,
392 BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL, &local_err);
393 if (ret < 0) {
394 error_propagate(errp, local_err);
395 return ret;
396 }
397 ret = bdrv_truncate(file, 0);
398 if (ret < 0) {
399 goto exit;
400 }
401
402 bat_entries = DIV_ROUND_UP(total_size, cl_size);
403 bat_sectors = DIV_ROUND_UP(bat_entries * sizeof(uint32_t) +
404 sizeof(ParallelsHeader), cl_size);
405 bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS;
406
407 memset(&header, 0, sizeof(header));
408 memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
409 header.version = cpu_to_le32(HEADER_VERSION);
410 /* don't care much about geometry, it is not used on image level */
411 header.heads = cpu_to_le32(16);
412 header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32);
413 header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
414 header.bat_entries = cpu_to_le32(bat_entries);
415 header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
416 header.data_off = cpu_to_le32(bat_sectors);
417
418 /* write all the data */
419 memset(tmp, 0, sizeof(tmp));
420 memcpy(tmp, &header, sizeof(header));
421
422 ret = bdrv_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE);
423 if (ret < 0) {
424 goto exit;
425 }
426 ret = bdrv_write_zeroes(file, 1, bat_sectors - 1, 0);
427 if (ret < 0) {
428 goto exit;
429 }
430 ret = 0;
431
432 done:
433 bdrv_unref(file);
434 return ret;
435
436 exit:
437 error_setg_errno(errp, -ret, "Failed to create Parallels image");
438 goto done;
439 }
440
441
442 static int parallels_probe(const uint8_t *buf, int buf_size,
443 const char *filename)
444 {
445 const ParallelsHeader *ph = (const void *)buf;
446
447 if (buf_size < sizeof(ParallelsHeader)) {
448 return 0;
449 }
450
451 if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
452 !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
453 (le32_to_cpu(ph->version) == HEADER_VERSION)) {
454 return 100;
455 }
456
457 return 0;
458 }
459
460 static int parallels_update_header(BlockDriverState *bs)
461 {
462 BDRVParallelsState *s = bs->opaque;
463 unsigned size = MAX(bdrv_opt_mem_align(bs->file), sizeof(ParallelsHeader));
464
465 if (size > s->header_size) {
466 size = s->header_size;
467 }
468 return bdrv_pwrite_sync(bs->file, 0, s->header, size);
469 }
470
471 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
472 Error **errp)
473 {
474 BDRVParallelsState *s = bs->opaque;
475 ParallelsHeader ph;
476 int ret, size;
477
478 ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
479 if (ret < 0) {
480 goto fail;
481 }
482
483 bs->total_sectors = le64_to_cpu(ph.nb_sectors);
484
485 if (le32_to_cpu(ph.version) != HEADER_VERSION) {
486 goto fail_format;
487 }
488 if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
489 s->off_multiplier = 1;
490 bs->total_sectors = 0xffffffff & bs->total_sectors;
491 } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
492 s->off_multiplier = le32_to_cpu(ph.tracks);
493 } else {
494 goto fail_format;
495 }
496
497 s->tracks = le32_to_cpu(ph.tracks);
498 if (s->tracks == 0) {
499 error_setg(errp, "Invalid image: Zero sectors per track");
500 ret = -EINVAL;
501 goto fail;
502 }
503 if (s->tracks > INT32_MAX/513) {
504 error_setg(errp, "Invalid image: Too big cluster");
505 ret = -EFBIG;
506 goto fail;
507 }
508
509 s->bat_size = le32_to_cpu(ph.bat_entries);
510 if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
511 error_setg(errp, "Catalog too large");
512 ret = -EFBIG;
513 goto fail;
514 }
515
516 size = sizeof(ParallelsHeader) + sizeof(uint32_t) * s->bat_size;
517 s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file));
518 s->header = qemu_try_blockalign(bs->file, s->header_size);
519 if (s->header == NULL) {
520 ret = -ENOMEM;
521 goto fail;
522 }
523 if (le32_to_cpu(ph.data_off) < s->header_size) {
524 /* there is not enough unused space to fit to block align between BAT
525 and actual data. We can't avoid read-modify-write... */
526 s->header_size = size;
527 }
528
529 ret = bdrv_pread(bs->file, 0, s->header, s->header_size);
530 if (ret < 0) {
531 goto fail;
532 }
533 s->bat_bitmap = (uint32_t *)(s->header + 1);
534
535 s->has_truncate = bdrv_has_zero_init(bs->file) &&
536 bdrv_truncate(bs->file, bdrv_getlength(bs->file)) == 0;
537
538 if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
539 /* Image was not closed correctly. The check is mandatory */
540 s->header_unclean = true;
541 if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
542 error_setg(errp, "parallels: Image was not closed correctly; "
543 "cannot be opened read/write");
544 ret = -EACCES;
545 goto fail;
546 }
547 }
548
549 if (flags & BDRV_O_RDWR) {
550 s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
551 ret = parallels_update_header(bs);
552 if (ret < 0) {
553 goto fail;
554 }
555 }
556
557 qemu_co_mutex_init(&s->lock);
558 return 0;
559
560 fail_format:
561 error_setg(errp, "Image not in Parallels format");
562 ret = -EINVAL;
563 fail:
564 qemu_vfree(s->header);
565 return ret;
566 }
567
568
569 static void parallels_close(BlockDriverState *bs)
570 {
571 BDRVParallelsState *s = bs->opaque;
572
573 if (bs->open_flags & BDRV_O_RDWR) {
574 s->header->inuse = 0;
575 parallels_update_header(bs);
576 }
577
578 qemu_vfree(s->header);
579 }
580
581 static QemuOptsList parallels_create_opts = {
582 .name = "parallels-create-opts",
583 .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
584 .desc = {
585 {
586 .name = BLOCK_OPT_SIZE,
587 .type = QEMU_OPT_SIZE,
588 .help = "Virtual disk size",
589 },
590 {
591 .name = BLOCK_OPT_CLUSTER_SIZE,
592 .type = QEMU_OPT_SIZE,
593 .help = "Parallels image cluster size",
594 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
595 },
596 { /* end of list */ }
597 }
598 };
599
600 static BlockDriver bdrv_parallels = {
601 .format_name = "parallels",
602 .instance_size = sizeof(BDRVParallelsState),
603 .bdrv_probe = parallels_probe,
604 .bdrv_open = parallels_open,
605 .bdrv_close = parallels_close,
606 .bdrv_co_get_block_status = parallels_co_get_block_status,
607 .bdrv_has_zero_init = bdrv_has_zero_init_1,
608 .bdrv_co_readv = parallels_co_readv,
609 .bdrv_co_writev = parallels_co_writev,
610
611 .bdrv_create = parallels_create,
612 .bdrv_check = parallels_check,
613 .create_opts = &parallels_create_opts,
614 };
615
616 static void bdrv_parallels_init(void)
617 {
618 bdrv_register(&bdrv_parallels);
619 }
620
621 block_init(bdrv_parallels_init);