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