OSDN Git Service

parallels: update used bitmap in allocate_cluster
[qmiga/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
31 #include "qemu/osdep.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "block/block_int.h"
35 #include "block/qdict.h"
36 #include "sysemu/block-backend.h"
37 #include "qemu/module.h"
38 #include "qemu/option.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qobject-input-visitor.h"
41 #include "qapi/qapi-visit-block-core.h"
42 #include "qemu/bswap.h"
43 #include "qemu/bitmap.h"
44 #include "qemu/memalign.h"
45 #include "migration/blocker.h"
46 #include "parallels.h"
47
48 /**************************************************************/
49
50 #define HEADER_MAGIC "WithoutFreeSpace"
51 #define HEADER_MAGIC2 "WithouFreSpacExt"
52 #define HEADER_VERSION 2
53 #define HEADER_INUSE_MAGIC  (0x746F6E59)
54 #define MAX_PARALLELS_IMAGE_FACTOR (1ull << 32)
55
56 static QEnumLookup prealloc_mode_lookup = {
57     .array = (const char *const[]) {
58         "falloc",
59         "truncate",
60     },
61     .size = PRL_PREALLOC_MODE__MAX
62 };
63
64 #define PARALLELS_OPT_PREALLOC_MODE     "prealloc-mode"
65 #define PARALLELS_OPT_PREALLOC_SIZE     "prealloc-size"
66
67 static QemuOptsList parallels_runtime_opts = {
68     .name = "parallels",
69     .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
70     .desc = {
71         {
72             .name = PARALLELS_OPT_PREALLOC_SIZE,
73             .type = QEMU_OPT_SIZE,
74             .help = "Preallocation size on image expansion",
75             .def_value_str = "128M",
76         },
77         {
78             .name = PARALLELS_OPT_PREALLOC_MODE,
79             .type = QEMU_OPT_STRING,
80             .help = "Preallocation mode on image expansion "
81                     "(allowed values: falloc, truncate)",
82             .def_value_str = "falloc",
83         },
84         { /* end of list */ },
85     },
86 };
87
88 static QemuOptsList parallels_create_opts = {
89     .name = "parallels-create-opts",
90     .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
91     .desc = {
92         {
93             .name = BLOCK_OPT_SIZE,
94             .type = QEMU_OPT_SIZE,
95             .help = "Virtual disk size",
96         },
97         {
98             .name = BLOCK_OPT_CLUSTER_SIZE,
99             .type = QEMU_OPT_SIZE,
100             .help = "Parallels image cluster size",
101             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
102         },
103         { /* end of list */ }
104     }
105 };
106
107
108 static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
109 {
110     return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
111 }
112
113 static uint32_t bat_entry_off(uint32_t idx)
114 {
115     return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
116 }
117
118 static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
119 {
120     uint32_t index, offset;
121
122     index = sector_num / s->tracks;
123     offset = sector_num % s->tracks;
124
125     /* not allocated */
126     if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
127         return -1;
128     }
129     return bat2sect(s, index) + offset;
130 }
131
132 static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
133         int nb_sectors)
134 {
135     int ret = s->tracks - sector_num % s->tracks;
136     return MIN(nb_sectors, ret);
137 }
138
139 static uint32_t host_cluster_index(BDRVParallelsState *s, int64_t off)
140 {
141     off -= s->data_start << BDRV_SECTOR_BITS;
142     return off / s->cluster_size;
143 }
144
145 static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
146                             int nb_sectors, int *pnum)
147 {
148     int64_t start_off = -2, prev_end_off = -2;
149
150     *pnum = 0;
151     while (nb_sectors > 0 || start_off == -2) {
152         int64_t offset = seek_to_sector(s, sector_num);
153         int to_end;
154
155         if (start_off == -2) {
156             start_off = offset;
157             prev_end_off = offset;
158         } else if (offset != prev_end_off) {
159             break;
160         }
161
162         to_end = cluster_remainder(s, sector_num, nb_sectors);
163         nb_sectors -= to_end;
164         sector_num += to_end;
165         *pnum += to_end;
166
167         if (offset > 0) {
168             prev_end_off += to_end;
169         }
170     }
171     return start_off;
172 }
173
174 static void parallels_set_bat_entry(BDRVParallelsState *s,
175                                     uint32_t index, uint32_t offset)
176 {
177     s->bat_bitmap[index] = cpu_to_le32(offset);
178     bitmap_set(s->bat_dirty_bmap, bat_entry_off(index) / s->bat_dirty_block, 1);
179 }
180
181 static int mark_used(BlockDriverState *bs, unsigned long *bitmap,
182                      uint32_t bitmap_size, int64_t off, uint32_t count)
183 {
184     BDRVParallelsState *s = bs->opaque;
185     uint32_t cluster_index = host_cluster_index(s, off);
186     unsigned long next_used;
187     if (cluster_index + count > bitmap_size) {
188         return -E2BIG;
189     }
190     next_used = find_next_bit(bitmap, bitmap_size, cluster_index);
191     if (next_used < cluster_index + count) {
192         return -EBUSY;
193     }
194     bitmap_set(bitmap, cluster_index, count);
195     return 0;
196 }
197
198 /*
199  * Collect used bitmap. The image can contain errors, we should fill the
200  * bitmap anyway, as much as we can. This information will be used for
201  * error resolution.
202  */
203 static int parallels_fill_used_bitmap(BlockDriverState *bs)
204 {
205     BDRVParallelsState *s = bs->opaque;
206     int64_t payload_bytes;
207     uint32_t i;
208     int err = 0;
209
210     payload_bytes = bdrv_getlength(bs->file->bs);
211     if (payload_bytes < 0) {
212         return payload_bytes;
213     }
214     payload_bytes -= s->data_start * BDRV_SECTOR_SIZE;
215     if (payload_bytes < 0) {
216         return -EINVAL;
217     }
218
219     s->used_bmap_size = DIV_ROUND_UP(payload_bytes, s->cluster_size);
220     if (s->used_bmap_size == 0) {
221         return 0;
222     }
223     s->used_bmap = bitmap_try_new(s->used_bmap_size);
224     if (s->used_bmap == NULL) {
225         return -ENOMEM;
226     }
227
228     for (i = 0; i < s->bat_size; i++) {
229         int err2;
230         int64_t host_off = bat2sect(s, i) << BDRV_SECTOR_BITS;
231         if (host_off == 0) {
232             continue;
233         }
234
235         err2 = mark_used(bs, s->used_bmap, s->used_bmap_size, host_off, 1);
236         if (err2 < 0 && err == 0) {
237             err = err2;
238         }
239     }
240     return err;
241 }
242
243 static void parallels_free_used_bitmap(BlockDriverState *bs)
244 {
245     BDRVParallelsState *s = bs->opaque;
246     s->used_bmap_size = 0;
247     g_free(s->used_bmap);
248 }
249
250 static int64_t coroutine_fn GRAPH_RDLOCK
251 allocate_clusters(BlockDriverState *bs, int64_t sector_num,
252                   int nb_sectors, int *pnum)
253 {
254     int ret = 0;
255     BDRVParallelsState *s = bs->opaque;
256     int64_t pos, space, idx, to_allocate, i, len;
257
258     pos = block_status(s, sector_num, nb_sectors, pnum);
259     if (pos > 0) {
260         return pos;
261     }
262
263     idx = sector_num / s->tracks;
264     to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
265
266     /*
267      * This function is called only by parallels_co_writev(), which will never
268      * pass a sector_num at or beyond the end of the image (because the block
269      * layer never passes such a sector_num to that function). Therefore, idx
270      * is always below s->bat_size.
271      * block_status() will limit *pnum so that sector_num + *pnum will not
272      * exceed the image end. Therefore, idx + to_allocate cannot exceed
273      * s->bat_size.
274      * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
275      * will always fit into a uint32_t.
276      */
277     assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
278
279     space = to_allocate * s->tracks;
280     len = bdrv_co_getlength(bs->file->bs);
281     if (len < 0) {
282         return len;
283     }
284     if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
285         uint32_t new_usedsize;
286
287         space += s->prealloc_size;
288         /*
289          * We require the expanded size to read back as zero. If the
290          * user permitted truncation, we try that; but if it fails, we
291          * force the safer-but-slower fallocate.
292          */
293         if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) {
294             ret = bdrv_co_truncate(bs->file,
295                                    (s->data_end + space) << BDRV_SECTOR_BITS,
296                                    false, PREALLOC_MODE_OFF,
297                                    BDRV_REQ_ZERO_WRITE, NULL);
298             if (ret == -ENOTSUP) {
299                 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
300             }
301         }
302         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
303             ret = bdrv_co_pwrite_zeroes(bs->file,
304                                         s->data_end << BDRV_SECTOR_BITS,
305                                         space << BDRV_SECTOR_BITS, 0);
306         }
307         if (ret < 0) {
308             return ret;
309         }
310
311         new_usedsize = s->used_bmap_size +
312                        (space << BDRV_SECTOR_BITS) / s->cluster_size;
313         s->used_bmap = bitmap_zero_extend(s->used_bmap, s->used_bmap_size,
314                                           new_usedsize);
315         s->used_bmap_size = new_usedsize;
316     }
317
318     /*
319      * Try to read from backing to fill empty clusters
320      * FIXME: 1. previous write_zeroes may be redundant
321      *        2. most of data we read from backing will be rewritten by
322      *           parallels_co_writev. On aligned-to-cluster write we do not need
323      *           this read at all.
324      *        3. it would be good to combine write of data from backing and new
325      *           data into one write call.
326      */
327     if (bs->backing) {
328         int64_t nb_cow_sectors = to_allocate * s->tracks;
329         int64_t nb_cow_bytes = nb_cow_sectors << BDRV_SECTOR_BITS;
330         void *buf = qemu_blockalign(bs, nb_cow_bytes);
331
332         ret = bdrv_co_pread(bs->backing, idx * s->tracks * BDRV_SECTOR_SIZE,
333                             nb_cow_bytes, buf, 0);
334         if (ret < 0) {
335             qemu_vfree(buf);
336             return ret;
337         }
338
339         ret = bdrv_co_pwrite(bs->file, s->data_end * BDRV_SECTOR_SIZE,
340                              nb_cow_bytes, buf, 0);
341         qemu_vfree(buf);
342         if (ret < 0) {
343             return ret;
344         }
345     }
346
347     ret = mark_used(bs, s->used_bmap, s->used_bmap_size,
348                     s->data_end << BDRV_SECTOR_BITS, to_allocate);
349     if (ret < 0) {
350         /* Image consistency is broken. Alarm! */
351         return ret;
352     }
353     for (i = 0; i < to_allocate; i++) {
354         parallels_set_bat_entry(s, idx + i, s->data_end / s->off_multiplier);
355         s->data_end += s->tracks;
356     }
357
358     return bat2sect(s, idx) + sector_num % s->tracks;
359 }
360
361
362 static int coroutine_fn GRAPH_RDLOCK
363 parallels_co_flush_to_os(BlockDriverState *bs)
364 {
365     BDRVParallelsState *s = bs->opaque;
366     unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
367     unsigned long bit;
368
369     qemu_co_mutex_lock(&s->lock);
370
371     bit = find_first_bit(s->bat_dirty_bmap, size);
372     while (bit < size) {
373         uint32_t off = bit * s->bat_dirty_block;
374         uint32_t to_write = s->bat_dirty_block;
375         int ret;
376
377         if (off + to_write > s->header_size) {
378             to_write = s->header_size - off;
379         }
380         ret = bdrv_co_pwrite(bs->file, off, to_write,
381                              (uint8_t *)s->header + off, 0);
382         if (ret < 0) {
383             qemu_co_mutex_unlock(&s->lock);
384             return ret;
385         }
386         bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
387     }
388     bitmap_zero(s->bat_dirty_bmap, size);
389
390     qemu_co_mutex_unlock(&s->lock);
391     return 0;
392 }
393
394
395 static int coroutine_fn parallels_co_block_status(BlockDriverState *bs,
396                                                   bool want_zero,
397                                                   int64_t offset,
398                                                   int64_t bytes,
399                                                   int64_t *pnum,
400                                                   int64_t *map,
401                                                   BlockDriverState **file)
402 {
403     BDRVParallelsState *s = bs->opaque;
404     int count;
405
406     assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE));
407     qemu_co_mutex_lock(&s->lock);
408     offset = block_status(s, offset >> BDRV_SECTOR_BITS,
409                           bytes >> BDRV_SECTOR_BITS, &count);
410     qemu_co_mutex_unlock(&s->lock);
411
412     *pnum = count * BDRV_SECTOR_SIZE;
413     if (offset < 0) {
414         return 0;
415     }
416
417     *map = offset * BDRV_SECTOR_SIZE;
418     *file = bs->file->bs;
419     return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
420 }
421
422 static int coroutine_fn GRAPH_RDLOCK
423 parallels_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
424                     QEMUIOVector *qiov, int flags)
425 {
426     BDRVParallelsState *s = bs->opaque;
427     uint64_t bytes_done = 0;
428     QEMUIOVector hd_qiov;
429     int ret = 0;
430
431     qemu_iovec_init(&hd_qiov, qiov->niov);
432
433     while (nb_sectors > 0) {
434         int64_t position;
435         int n, nbytes;
436
437         qemu_co_mutex_lock(&s->lock);
438         position = allocate_clusters(bs, sector_num, nb_sectors, &n);
439         qemu_co_mutex_unlock(&s->lock);
440         if (position < 0) {
441             ret = (int)position;
442             break;
443         }
444
445         nbytes = n << BDRV_SECTOR_BITS;
446
447         qemu_iovec_reset(&hd_qiov);
448         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
449
450         ret = bdrv_co_pwritev(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
451                               &hd_qiov, 0);
452         if (ret < 0) {
453             break;
454         }
455
456         nb_sectors -= n;
457         sector_num += n;
458         bytes_done += nbytes;
459     }
460
461     qemu_iovec_destroy(&hd_qiov);
462     return ret;
463 }
464
465 static int coroutine_fn GRAPH_RDLOCK
466 parallels_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
467                    QEMUIOVector *qiov)
468 {
469     BDRVParallelsState *s = bs->opaque;
470     uint64_t bytes_done = 0;
471     QEMUIOVector hd_qiov;
472     int ret = 0;
473
474     qemu_iovec_init(&hd_qiov, qiov->niov);
475
476     while (nb_sectors > 0) {
477         int64_t position;
478         int n, nbytes;
479
480         qemu_co_mutex_lock(&s->lock);
481         position = block_status(s, sector_num, nb_sectors, &n);
482         qemu_co_mutex_unlock(&s->lock);
483
484         nbytes = n << BDRV_SECTOR_BITS;
485
486         qemu_iovec_reset(&hd_qiov);
487         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
488
489         if (position < 0) {
490             if (bs->backing) {
491                 ret = bdrv_co_preadv(bs->backing, sector_num * BDRV_SECTOR_SIZE,
492                                      nbytes, &hd_qiov, 0);
493                 if (ret < 0) {
494                     break;
495                 }
496             } else {
497                 qemu_iovec_memset(&hd_qiov, 0, 0, nbytes);
498             }
499         } else {
500             ret = bdrv_co_preadv(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
501                                  &hd_qiov, 0);
502             if (ret < 0) {
503                 break;
504             }
505         }
506
507         nb_sectors -= n;
508         sector_num += n;
509         bytes_done += nbytes;
510     }
511
512     qemu_iovec_destroy(&hd_qiov);
513     return ret;
514 }
515
516 static void parallels_check_unclean(BlockDriverState *bs,
517                                     BdrvCheckResult *res,
518                                     BdrvCheckMode fix)
519 {
520     BDRVParallelsState *s = bs->opaque;
521
522     if (!s->header_unclean) {
523         return;
524     }
525
526     fprintf(stderr, "%s image was not closed correctly\n",
527             fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
528     res->corruptions++;
529     if (fix & BDRV_FIX_ERRORS) {
530         /* parallels_close will do the job right */
531         res->corruptions_fixed++;
532         s->header_unclean = false;
533     }
534 }
535
536 /*
537  * Returns true if data_off is correct, otherwise false. In both cases
538  * correct_offset is set to the proper value.
539  */
540 static bool parallels_test_data_off(BDRVParallelsState *s,
541                                     int64_t file_nb_sectors,
542                                     uint32_t *correct_offset)
543 {
544     uint32_t data_off, min_off;
545     bool old_magic;
546
547     /*
548      * There are two slightly different image formats: with "WithoutFreeSpace"
549      * or "WithouFreSpacExt" magic words. Call the first one as "old magic".
550      * In such images data_off field can be zero. In this case the offset is
551      * calculated as the end of BAT table plus some padding to ensure sector
552      * size alignment.
553      */
554     old_magic = !memcmp(s->header->magic, HEADER_MAGIC, 16);
555
556     min_off = DIV_ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
557     if (!old_magic) {
558         min_off = ROUND_UP(min_off, s->cluster_size / BDRV_SECTOR_SIZE);
559     }
560
561     if (correct_offset) {
562         *correct_offset = min_off;
563     }
564
565     data_off = le32_to_cpu(s->header->data_off);
566     if (data_off == 0 && old_magic) {
567         return true;
568     }
569
570     if (data_off < min_off || data_off > file_nb_sectors) {
571         return false;
572     }
573
574     if (correct_offset) {
575         *correct_offset = data_off;
576     }
577
578     return true;
579 }
580
581 static int coroutine_fn GRAPH_RDLOCK
582 parallels_check_data_off(BlockDriverState *bs, BdrvCheckResult *res,
583                          BdrvCheckMode fix)
584 {
585     BDRVParallelsState *s = bs->opaque;
586     int64_t file_size;
587     uint32_t data_off;
588
589     file_size = bdrv_co_nb_sectors(bs->file->bs);
590     if (file_size < 0) {
591         res->check_errors++;
592         return file_size;
593     }
594
595     if (parallels_test_data_off(s, file_size, &data_off)) {
596         return 0;
597     }
598
599     res->corruptions++;
600     if (fix & BDRV_FIX_ERRORS) {
601         int err;
602         s->header->data_off = cpu_to_le32(data_off);
603         s->data_start = data_off;
604
605         parallels_free_used_bitmap(bs);
606         err = parallels_fill_used_bitmap(bs);
607         if (err == -ENOMEM) {
608             res->check_errors++;
609             return err;
610         }
611
612         res->corruptions_fixed++;
613     }
614
615     fprintf(stderr, "%s data_off field has incorrect value\n",
616             fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
617
618     return 0;
619 }
620
621 static int coroutine_fn GRAPH_RDLOCK
622 parallels_check_outside_image(BlockDriverState *bs, BdrvCheckResult *res,
623                               BdrvCheckMode fix)
624 {
625     BDRVParallelsState *s = bs->opaque;
626     uint32_t i;
627     int64_t off, high_off, size;
628
629     size = bdrv_co_getlength(bs->file->bs);
630     if (size < 0) {
631         res->check_errors++;
632         return size;
633     }
634
635     high_off = 0;
636     for (i = 0; i < s->bat_size; i++) {
637         off = bat2sect(s, i) << BDRV_SECTOR_BITS;
638         if (off + s->cluster_size > size) {
639             fprintf(stderr, "%s cluster %u is outside image\n",
640                     fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
641             res->corruptions++;
642             if (fix & BDRV_FIX_ERRORS) {
643                 parallels_set_bat_entry(s, i, 0);
644                 res->corruptions_fixed++;
645             }
646             continue;
647         }
648         if (high_off < off) {
649             high_off = off;
650         }
651     }
652
653     if (high_off == 0) {
654         res->image_end_offset = s->data_end << BDRV_SECTOR_BITS;
655     } else {
656         res->image_end_offset = high_off + s->cluster_size;
657         s->data_end = res->image_end_offset >> BDRV_SECTOR_BITS;
658     }
659
660     return 0;
661 }
662
663 static int coroutine_fn GRAPH_RDLOCK
664 parallels_check_leak(BlockDriverState *bs, BdrvCheckResult *res,
665                      BdrvCheckMode fix, bool explicit)
666 {
667     BDRVParallelsState *s = bs->opaque;
668     int64_t size;
669     int ret;
670
671     size = bdrv_co_getlength(bs->file->bs);
672     if (size < 0) {
673         res->check_errors++;
674         return size;
675     }
676
677     if (size > res->image_end_offset) {
678         int64_t count;
679         count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size);
680         if (explicit) {
681             fprintf(stderr,
682                     "%s space leaked at the end of the image %" PRId64 "\n",
683                     fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
684                     size - res->image_end_offset);
685             res->leaks += count;
686         }
687         if (fix & BDRV_FIX_LEAKS) {
688             Error *local_err = NULL;
689
690             /*
691              * In order to really repair the image, we must shrink it.
692              * That means we have to pass exact=true.
693              */
694             ret = bdrv_co_truncate(bs->file, res->image_end_offset, true,
695                                    PREALLOC_MODE_OFF, 0, &local_err);
696             if (ret < 0) {
697                 error_report_err(local_err);
698                 res->check_errors++;
699                 return ret;
700             }
701             if (explicit) {
702                 res->leaks_fixed += count;
703             }
704         }
705     }
706
707     return 0;
708 }
709
710 static int coroutine_fn GRAPH_RDLOCK
711 parallels_check_duplicate(BlockDriverState *bs, BdrvCheckResult *res,
712                           BdrvCheckMode fix)
713 {
714     BDRVParallelsState *s = bs->opaque;
715     int64_t host_off, host_sector, guest_sector;
716     unsigned long *bitmap;
717     uint32_t i, bitmap_size, bat_entry;
718     int n, ret = 0;
719     uint64_t *buf = NULL;
720     bool fixed = false;
721
722     /*
723      * Create a bitmap of used clusters.
724      * If a bit is set, there is a BAT entry pointing to this cluster.
725      * Loop through the BAT entries, check bits relevant to an entry offset.
726      * If bit is set, this entry is duplicated. Otherwise set the bit.
727      *
728      * We shouldn't worry about newly allocated clusters outside the image
729      * because they are created higher then any existing cluster pointed by
730      * a BAT entry.
731      */
732     bitmap_size = host_cluster_index(s, res->image_end_offset);
733     if (bitmap_size == 0) {
734         return 0;
735     }
736     if (res->image_end_offset % s->cluster_size) {
737         /* A not aligned image end leads to a bitmap shorter by 1 */
738         bitmap_size++;
739     }
740
741     bitmap = bitmap_new(bitmap_size);
742
743     buf = qemu_blockalign(bs, s->cluster_size);
744
745     for (i = 0; i < s->bat_size; i++) {
746         host_off = bat2sect(s, i) << BDRV_SECTOR_BITS;
747         if (host_off == 0) {
748             continue;
749         }
750
751         ret = mark_used(bs, bitmap, bitmap_size, host_off, 1);
752         assert(ret != -E2BIG);
753         if (ret == 0) {
754             continue;
755         }
756
757         /* this cluster duplicates another one */
758         fprintf(stderr, "%s duplicate offset in BAT entry %u\n",
759                 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
760
761         res->corruptions++;
762
763         if (!(fix & BDRV_FIX_ERRORS)) {
764             continue;
765         }
766
767         /*
768          * Reset the entry and allocate a new cluster
769          * for the relevant guest offset. In this way we let
770          * the lower layer to place the new cluster properly.
771          * Copy the original cluster to the allocated one.
772          * But before save the old offset value for repairing
773          * if we have an error.
774          */
775         bat_entry = s->bat_bitmap[i];
776         parallels_set_bat_entry(s, i, 0);
777
778         ret = bdrv_co_pread(bs->file, host_off, s->cluster_size, buf, 0);
779         if (ret < 0) {
780             res->check_errors++;
781             goto out_repair_bat;
782         }
783
784         guest_sector = (i * (int64_t)s->cluster_size) >> BDRV_SECTOR_BITS;
785         host_sector = allocate_clusters(bs, guest_sector, s->tracks, &n);
786         if (host_sector < 0) {
787             res->check_errors++;
788             goto out_repair_bat;
789         }
790         host_off = host_sector << BDRV_SECTOR_BITS;
791
792         ret = bdrv_co_pwrite(bs->file, host_off, s->cluster_size, buf, 0);
793         if (ret < 0) {
794             res->check_errors++;
795             goto out_repair_bat;
796         }
797
798         if (host_off + s->cluster_size > res->image_end_offset) {
799             res->image_end_offset = host_off + s->cluster_size;
800         }
801
802         /*
803          * In the future allocate_cluster() will reuse holed offsets
804          * inside the image. Keep the used clusters bitmap content
805          * consistent for the new allocated clusters too.
806          *
807          * Note, clusters allocated outside the current image are not
808          * considered, and the bitmap size doesn't change. This specifically
809          * means that -E2BIG is OK.
810          */
811         ret = mark_used(bs, bitmap, bitmap_size, host_off, 1);
812         if (ret == -EBUSY) {
813             res->check_errors++;
814             goto out_repair_bat;
815         }
816
817         fixed = true;
818         res->corruptions_fixed++;
819
820     }
821
822     if (fixed) {
823         /*
824          * When new clusters are allocated, the file size increases by
825          * 128 Mb. We need to truncate the file to the right size. Let
826          * the leak fix code make its job without res changing.
827          */
828         ret = parallels_check_leak(bs, res, fix, false);
829     }
830
831 out_free:
832     g_free(buf);
833     g_free(bitmap);
834     return ret;
835 /*
836  * We can get here only from places where index and old_offset have
837  * meaningful values.
838  */
839 out_repair_bat:
840     s->bat_bitmap[i] = bat_entry;
841     goto out_free;
842 }
843
844 static void parallels_collect_statistics(BlockDriverState *bs,
845                                          BdrvCheckResult *res,
846                                          BdrvCheckMode fix)
847 {
848     BDRVParallelsState *s = bs->opaque;
849     int64_t off, prev_off;
850     uint32_t i;
851
852     res->bfi.total_clusters = s->bat_size;
853     res->bfi.compressed_clusters = 0; /* compression is not supported */
854
855     prev_off = 0;
856     for (i = 0; i < s->bat_size; i++) {
857         off = bat2sect(s, i) << BDRV_SECTOR_BITS;
858         /*
859          * If BDRV_FIX_ERRORS is not set, out-of-image BAT entries were not
860          * fixed. Skip not allocated and out-of-image BAT entries.
861          */
862         if (off == 0 || off + s->cluster_size > res->image_end_offset) {
863             prev_off = 0;
864             continue;
865         }
866
867         if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
868             res->bfi.fragmented_clusters++;
869         }
870         prev_off = off;
871         res->bfi.allocated_clusters++;
872     }
873 }
874
875 static int coroutine_fn GRAPH_RDLOCK
876 parallels_co_check(BlockDriverState *bs, BdrvCheckResult *res,
877                    BdrvCheckMode fix)
878 {
879     BDRVParallelsState *s = bs->opaque;
880     int ret;
881
882     WITH_QEMU_LOCK_GUARD(&s->lock) {
883         parallels_check_unclean(bs, res, fix);
884
885         ret = parallels_check_data_off(bs, res, fix);
886         if (ret < 0) {
887             return ret;
888         }
889
890         ret = parallels_check_outside_image(bs, res, fix);
891         if (ret < 0) {
892             return ret;
893         }
894
895         ret = parallels_check_leak(bs, res, fix, true);
896         if (ret < 0) {
897             return ret;
898         }
899
900         ret = parallels_check_duplicate(bs, res, fix);
901         if (ret < 0) {
902             return ret;
903         }
904
905         parallels_collect_statistics(bs, res, fix);
906     }
907
908     ret = bdrv_co_flush(bs);
909     if (ret < 0) {
910         res->check_errors++;
911     }
912
913     return ret;
914 }
915
916
917 static int coroutine_fn GRAPH_UNLOCKED
918 parallels_co_create(BlockdevCreateOptions* opts, Error **errp)
919 {
920     BlockdevCreateOptionsParallels *parallels_opts;
921     BlockDriverState *bs;
922     BlockBackend *blk;
923     int64_t total_size, cl_size;
924     uint32_t bat_entries, bat_sectors;
925     ParallelsHeader header;
926     uint8_t tmp[BDRV_SECTOR_SIZE];
927     int ret;
928
929     assert(opts->driver == BLOCKDEV_DRIVER_PARALLELS);
930     parallels_opts = &opts->u.parallels;
931
932     /* Sanity checks */
933     total_size = parallels_opts->size;
934
935     if (parallels_opts->has_cluster_size) {
936         cl_size = parallels_opts->cluster_size;
937     } else {
938         cl_size = DEFAULT_CLUSTER_SIZE;
939     }
940
941     /* XXX What is the real limit here? This is an insanely large maximum. */
942     if (cl_size >= INT64_MAX / MAX_PARALLELS_IMAGE_FACTOR) {
943         error_setg(errp, "Cluster size is too large");
944         return -EINVAL;
945     }
946     if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) {
947         error_setg(errp, "Image size is too large for this cluster size");
948         return -E2BIG;
949     }
950
951     if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) {
952         error_setg(errp, "Image size must be a multiple of 512 bytes");
953         return -EINVAL;
954     }
955
956     if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) {
957         error_setg(errp, "Cluster size must be a multiple of 512 bytes");
958         return -EINVAL;
959     }
960
961     /* Create BlockBackend to write to the image */
962     bs = bdrv_co_open_blockdev_ref(parallels_opts->file, errp);
963     if (bs == NULL) {
964         return -EIO;
965     }
966
967     blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
968                              errp);
969     if (!blk) {
970         ret = -EPERM;
971         goto out;
972     }
973     blk_set_allow_write_beyond_eof(blk, true);
974
975     /* Create image format */
976     bat_entries = DIV_ROUND_UP(total_size, cl_size);
977     bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
978     bat_sectors = (bat_sectors *  cl_size) >> BDRV_SECTOR_BITS;
979
980     memset(&header, 0, sizeof(header));
981     memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
982     header.version = cpu_to_le32(HEADER_VERSION);
983     /* don't care much about geometry, it is not used on image level */
984     header.heads = cpu_to_le32(HEADS_NUMBER);
985     header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE
986                                    / HEADS_NUMBER / SEC_IN_CYL);
987     header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
988     header.bat_entries = cpu_to_le32(bat_entries);
989     header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
990     header.data_off = cpu_to_le32(bat_sectors);
991
992     /* write all the data */
993     memset(tmp, 0, sizeof(tmp));
994     memcpy(tmp, &header, sizeof(header));
995
996     ret = blk_co_pwrite(blk, 0, BDRV_SECTOR_SIZE, tmp, 0);
997     if (ret < 0) {
998         goto exit;
999     }
1000     ret = blk_co_pwrite_zeroes(blk, BDRV_SECTOR_SIZE,
1001                                (bat_sectors - 1) << BDRV_SECTOR_BITS, 0);
1002     if (ret < 0) {
1003         goto exit;
1004     }
1005
1006     ret = 0;
1007 out:
1008     blk_co_unref(blk);
1009     bdrv_co_unref(bs);
1010     return ret;
1011
1012 exit:
1013     error_setg_errno(errp, -ret, "Failed to create Parallels image");
1014     goto out;
1015 }
1016
1017 static int coroutine_fn GRAPH_UNLOCKED
1018 parallels_co_create_opts(BlockDriver *drv, const char *filename,
1019                          QemuOpts *opts, Error **errp)
1020 {
1021     BlockdevCreateOptions *create_options = NULL;
1022     BlockDriverState *bs = NULL;
1023     QDict *qdict;
1024     Visitor *v;
1025     int ret;
1026
1027     static const QDictRenames opt_renames[] = {
1028         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
1029         { NULL, NULL },
1030     };
1031
1032     /* Parse options and convert legacy syntax */
1033     qdict = qemu_opts_to_qdict_filtered(opts, NULL, &parallels_create_opts,
1034                                         true);
1035
1036     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
1037         ret = -EINVAL;
1038         goto done;
1039     }
1040
1041     /* Create and open the file (protocol layer) */
1042     ret = bdrv_co_create_file(filename, opts, errp);
1043     if (ret < 0) {
1044         goto done;
1045     }
1046
1047     bs = bdrv_co_open(filename, NULL, NULL,
1048                       BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
1049     if (bs == NULL) {
1050         ret = -EIO;
1051         goto done;
1052     }
1053
1054     /* Now get the QAPI type BlockdevCreateOptions */
1055     qdict_put_str(qdict, "driver", "parallels");
1056     qdict_put_str(qdict, "file", bs->node_name);
1057
1058     v = qobject_input_visitor_new_flat_confused(qdict, errp);
1059     if (!v) {
1060         ret = -EINVAL;
1061         goto done;
1062     }
1063
1064     visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
1065     visit_free(v);
1066     if (!create_options) {
1067         ret = -EINVAL;
1068         goto done;
1069     }
1070
1071     /* Silently round up sizes */
1072     create_options->u.parallels.size =
1073         ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE);
1074     create_options->u.parallels.cluster_size =
1075         ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZE);
1076
1077     /* Create the Parallels image (format layer) */
1078     ret = parallels_co_create(create_options, errp);
1079     if (ret < 0) {
1080         goto done;
1081     }
1082     ret = 0;
1083
1084 done:
1085     qobject_unref(qdict);
1086     bdrv_co_unref(bs);
1087     qapi_free_BlockdevCreateOptions(create_options);
1088     return ret;
1089 }
1090
1091
1092 static int parallels_probe(const uint8_t *buf, int buf_size,
1093                            const char *filename)
1094 {
1095     const ParallelsHeader *ph = (const void *)buf;
1096
1097     if (buf_size < sizeof(ParallelsHeader)) {
1098         return 0;
1099     }
1100
1101     if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
1102            !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
1103            (le32_to_cpu(ph->version) == HEADER_VERSION)) {
1104         return 100;
1105     }
1106
1107     return 0;
1108 }
1109
1110 static int parallels_update_header(BlockDriverState *bs)
1111 {
1112     BDRVParallelsState *s = bs->opaque;
1113     unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
1114                         sizeof(ParallelsHeader));
1115
1116     if (size > s->header_size) {
1117         size = s->header_size;
1118     }
1119     return bdrv_pwrite_sync(bs->file, 0, size, s->header, 0);
1120 }
1121
1122
1123 static int parallels_opts_prealloc(BlockDriverState *bs, QDict *options,
1124                                    Error **errp)
1125 {
1126     int err;
1127     char *buf;
1128     int64_t bytes;
1129     BDRVParallelsState *s = bs->opaque;
1130     Error *local_err = NULL;
1131     QemuOpts *opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, errp);
1132     if (!opts) {
1133         return -ENOMEM;
1134     }
1135
1136     err = -EINVAL;
1137     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1138         goto done;
1139     }
1140
1141     bytes = qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
1142     s->prealloc_size = bytes >> BDRV_SECTOR_BITS;
1143     buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
1144     /* prealloc_mode can be downgraded later during allocate_clusters */
1145     s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
1146                                        PRL_PREALLOC_MODE_FALLOCATE,
1147                                        &local_err);
1148     g_free(buf);
1149     if (local_err != NULL) {
1150         error_propagate(errp, local_err);
1151         goto done;
1152     }
1153     err = 0;
1154
1155 done:
1156     qemu_opts_del(opts);
1157     return err;
1158 }
1159
1160 static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
1161                           Error **errp)
1162 {
1163     BDRVParallelsState *s = bs->opaque;
1164     ParallelsHeader ph;
1165     int ret, size, i;
1166     int64_t file_nb_sectors, sector;
1167     uint32_t data_start;
1168     bool need_check = false;
1169
1170     ret = parallels_opts_prealloc(bs, options, errp);
1171     if (ret < 0) {
1172         return ret;
1173     }
1174
1175     ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
1176     if (ret < 0) {
1177         return ret;
1178     }
1179
1180     file_nb_sectors = bdrv_nb_sectors(bs->file->bs);
1181     if (file_nb_sectors < 0) {
1182         return -EINVAL;
1183     }
1184
1185     ret = bdrv_pread(bs->file, 0, sizeof(ph), &ph, 0);
1186     if (ret < 0) {
1187         return ret;
1188     }
1189
1190     bs->total_sectors = le64_to_cpu(ph.nb_sectors);
1191
1192     if (le32_to_cpu(ph.version) != HEADER_VERSION) {
1193         goto fail_format;
1194     }
1195     if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
1196         s->off_multiplier = 1;
1197         bs->total_sectors = 0xffffffff & bs->total_sectors;
1198     } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
1199         s->off_multiplier = le32_to_cpu(ph.tracks);
1200     } else {
1201         goto fail_format;
1202     }
1203
1204     s->tracks = le32_to_cpu(ph.tracks);
1205     if (s->tracks == 0) {
1206         error_setg(errp, "Invalid image: Zero sectors per track");
1207         return -EINVAL;
1208     }
1209     if (s->tracks > INT32_MAX/513) {
1210         error_setg(errp, "Invalid image: Too big cluster");
1211         return -EFBIG;
1212     }
1213     s->prealloc_size = MAX(s->tracks, s->prealloc_size);
1214     s->cluster_size = s->tracks << BDRV_SECTOR_BITS;
1215
1216     s->bat_size = le32_to_cpu(ph.bat_entries);
1217     if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
1218         error_setg(errp, "Catalog too large");
1219         return -EFBIG;
1220     }
1221
1222     size = bat_entry_off(s->bat_size);
1223     s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
1224     s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
1225     if (s->header == NULL) {
1226         return -ENOMEM;
1227     }
1228
1229     ret = bdrv_pread(bs->file, 0, s->header_size, s->header, 0);
1230     if (ret < 0) {
1231         goto fail;
1232     }
1233     s->bat_bitmap = (uint32_t *)(s->header + 1);
1234
1235     if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
1236         need_check = s->header_unclean = true;
1237     }
1238
1239     {
1240         bool ok = parallels_test_data_off(s, file_nb_sectors, &data_start);
1241         need_check = need_check || !ok;
1242     }
1243
1244     s->data_start = data_start;
1245     s->data_end = s->data_start;
1246     if (s->data_end < (s->header_size >> BDRV_SECTOR_BITS)) {
1247         /*
1248          * There is not enough unused space to fit to block align between BAT
1249          * and actual data. We can't avoid read-modify-write...
1250          */
1251         s->header_size = size;
1252     }
1253
1254     if (ph.ext_off) {
1255         if (flags & BDRV_O_RDWR) {
1256             /*
1257              * It's unsafe to open image RW if there is an extension (as we
1258              * don't support it). But parallels driver in QEMU historically
1259              * ignores the extension, so print warning and don't care.
1260              */
1261             warn_report("Format Extension ignored in RW mode");
1262         } else {
1263             ret = parallels_read_format_extension(
1264                     bs, le64_to_cpu(ph.ext_off) << BDRV_SECTOR_BITS, errp);
1265             if (ret < 0) {
1266                 goto fail;
1267             }
1268         }
1269     }
1270
1271     if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
1272         s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
1273         ret = parallels_update_header(bs);
1274         if (ret < 0) {
1275             goto fail;
1276         }
1277     }
1278
1279     s->bat_dirty_block = 4 * qemu_real_host_page_size();
1280     s->bat_dirty_bmap =
1281         bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
1282
1283     /* Disable migration until bdrv_activate method is added */
1284     error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
1285                "does not support live migration",
1286                bdrv_get_device_or_node_name(bs));
1287     ret = migrate_add_blocker(s->migration_blocker, errp);
1288     if (ret < 0) {
1289         error_setg(errp, "Migration blocker error");
1290         goto fail;
1291     }
1292     qemu_co_mutex_init(&s->lock);
1293
1294     for (i = 0; i < s->bat_size; i++) {
1295         sector = bat2sect(s, i);
1296         if (sector + s->tracks > s->data_end) {
1297             s->data_end = sector + s->tracks;
1298         }
1299     }
1300     need_check = need_check || s->data_end > file_nb_sectors;
1301
1302     if (!need_check) {
1303         ret = parallels_fill_used_bitmap(bs);
1304         if (ret == -ENOMEM) {
1305             goto fail;
1306         }
1307         need_check = need_check || ret < 0; /* These are correctable errors */
1308     }
1309
1310     /*
1311      * We don't repair the image here if it's opened for checks. Also we don't
1312      * want to change inactive images and can't change readonly images.
1313      */
1314     if ((flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) || !(flags & BDRV_O_RDWR)) {
1315         return 0;
1316     }
1317
1318     /* Repair the image if corruption was detected. */
1319     if (need_check) {
1320         BdrvCheckResult res;
1321         ret = bdrv_check(bs, &res, BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1322         if (ret < 0) {
1323             error_setg_errno(errp, -ret, "Could not repair corrupted image");
1324             migrate_del_blocker(s->migration_blocker);
1325             goto fail;
1326         }
1327     }
1328     return 0;
1329
1330 fail_format:
1331     error_setg(errp, "Image not in Parallels format");
1332     return -EINVAL;
1333
1334 fail:
1335     /*
1336      * "s" object was allocated by g_malloc0 so we can safely
1337      * try to free its fields even they were not allocated.
1338      */
1339     parallels_free_used_bitmap(bs);
1340
1341     error_free(s->migration_blocker);
1342     g_free(s->bat_dirty_bmap);
1343     qemu_vfree(s->header);
1344     return ret;
1345 }
1346
1347
1348 static void parallels_close(BlockDriverState *bs)
1349 {
1350     BDRVParallelsState *s = bs->opaque;
1351
1352     if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) {
1353         s->header->inuse = 0;
1354         parallels_update_header(bs);
1355
1356         /* errors are ignored, so we might as well pass exact=true */
1357         bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
1358                       PREALLOC_MODE_OFF, 0, NULL);
1359     }
1360
1361     parallels_free_used_bitmap(bs);
1362
1363     g_free(s->bat_dirty_bmap);
1364     qemu_vfree(s->header);
1365
1366     migrate_del_blocker(s->migration_blocker);
1367     error_free(s->migration_blocker);
1368 }
1369
1370 static bool parallels_is_support_dirty_bitmaps(BlockDriverState *bs)
1371 {
1372     return 1;
1373 }
1374
1375 static BlockDriver bdrv_parallels = {
1376     .format_name                = "parallels",
1377     .instance_size              = sizeof(BDRVParallelsState),
1378     .create_opts                = &parallels_create_opts,
1379     .is_format                  = true,
1380     .supports_backing           = true,
1381
1382     .bdrv_has_zero_init         = bdrv_has_zero_init_1,
1383     .bdrv_supports_persistent_dirty_bitmap = parallels_is_support_dirty_bitmaps,
1384
1385     .bdrv_probe                 = parallels_probe,
1386     .bdrv_open                  = parallels_open,
1387     .bdrv_close                 = parallels_close,
1388     .bdrv_child_perm            = bdrv_default_perms,
1389     .bdrv_co_block_status       = parallels_co_block_status,
1390     .bdrv_co_flush_to_os        = parallels_co_flush_to_os,
1391     .bdrv_co_readv              = parallels_co_readv,
1392     .bdrv_co_writev             = parallels_co_writev,
1393     .bdrv_co_create             = parallels_co_create,
1394     .bdrv_co_create_opts        = parallels_co_create_opts,
1395     .bdrv_co_check              = parallels_co_check,
1396 };
1397
1398 static void bdrv_parallels_init(void)
1399 {
1400     bdrv_register(&bdrv_parallels);
1401 }
1402
1403 block_init(bdrv_parallels_init);