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