xref: /qemu/block/parallels.c (revision b92902df)
1019d6b8fSAnthony Liguori /*
2019d6b8fSAnthony Liguori  * Block driver for Parallels disk image format
3019d6b8fSAnthony Liguori  *
4019d6b8fSAnthony Liguori  * Copyright (c) 2007 Alex Beregszaszi
5cc5690f2SDenis V. Lunev  * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
6019d6b8fSAnthony Liguori  *
7cc5690f2SDenis V. Lunev  * This code was originally based on comparing different disk images created
8cc5690f2SDenis V. Lunev  * by Parallels. Currently it is based on opened OpenVZ sources
9cc5690f2SDenis V. Lunev  * available at
10cc5690f2SDenis V. Lunev  *     http://git.openvz.org/?p=ploop;a=summary
11019d6b8fSAnthony Liguori  *
12019d6b8fSAnthony Liguori  * Permission is hereby granted, free of charge, to any person obtaining a copy
13019d6b8fSAnthony Liguori  * of this software and associated documentation files (the "Software"), to deal
14019d6b8fSAnthony Liguori  * in the Software without restriction, including without limitation the rights
15019d6b8fSAnthony Liguori  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16019d6b8fSAnthony Liguori  * copies of the Software, and to permit persons to whom the Software is
17019d6b8fSAnthony Liguori  * furnished to do so, subject to the following conditions:
18019d6b8fSAnthony Liguori  *
19019d6b8fSAnthony Liguori  * The above copyright notice and this permission notice shall be included in
20019d6b8fSAnthony Liguori  * all copies or substantial portions of the Software.
21019d6b8fSAnthony Liguori  *
22019d6b8fSAnthony Liguori  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23019d6b8fSAnthony Liguori  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24019d6b8fSAnthony Liguori  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25019d6b8fSAnthony Liguori  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26019d6b8fSAnthony Liguori  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27019d6b8fSAnthony Liguori  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28019d6b8fSAnthony Liguori  * THE SOFTWARE.
29019d6b8fSAnthony Liguori  */
30922a01a0SMarkus Armbruster 
3180c71a24SPeter Maydell #include "qemu/osdep.h"
32da34e65cSMarkus Armbruster #include "qapi/error.h"
33737e150eSPaolo Bonzini #include "block/block_int.h"
34609f45eaSMax Reitz #include "block/qdict.h"
358942764fSKevin Wolf #include "sysemu/block-backend.h"
361de7afc9SPaolo Bonzini #include "qemu/module.h"
37922a01a0SMarkus Armbruster #include "qemu/option.h"
381511b490SKevin Wolf #include "qapi/qmp/qdict.h"
391511b490SKevin Wolf #include "qapi/qobject-input-visitor.h"
401511b490SKevin Wolf #include "qapi/qapi-visit-block-core.h"
4158369e22SPaolo Bonzini #include "qemu/bswap.h"
420d31c7c2SDenis V. Lunev #include "qemu/bitmap.h"
431d0f37cfSJeff Cody #include "migration/blocker.h"
4490fe66f0SKlim Kireev #include "parallels.h"
45019d6b8fSAnthony Liguori 
46019d6b8fSAnthony Liguori /**************************************************************/
47019d6b8fSAnthony Liguori 
48019d6b8fSAnthony Liguori #define HEADER_MAGIC "WithoutFreeSpace"
49d25d5980SDenis V. Lunev #define HEADER_MAGIC2 "WithouFreSpacExt"
50019d6b8fSAnthony Liguori #define HEADER_VERSION 2
516dd6b9f1SDenis V. Lunev #define HEADER_INUSE_MAGIC  (0x746F6E59)
52555a608cSKlim Kireev #define MAX_PARALLELS_IMAGE_FACTOR (1ull << 32)
53019d6b8fSAnthony Liguori 
54f7abe0ecSMarc-André Lureau static QEnumLookup prealloc_mode_lookup = {
55f7abe0ecSMarc-André Lureau     .array = (const char *const[]) {
56d6179011SDenis V. Lunev         "falloc",
57d6179011SDenis V. Lunev         "truncate",
58f7abe0ecSMarc-André Lureau     },
59f7abe0ecSMarc-André Lureau     .size = PRL_PREALLOC_MODE__MAX
60d6179011SDenis V. Lunev };
61d6179011SDenis V. Lunev 
62d6179011SDenis V. Lunev #define PARALLELS_OPT_PREALLOC_MODE     "prealloc-mode"
63d6179011SDenis V. Lunev #define PARALLELS_OPT_PREALLOC_SIZE     "prealloc-size"
64d6179011SDenis V. Lunev 
65d6179011SDenis V. Lunev static QemuOptsList parallels_runtime_opts = {
66d6179011SDenis V. Lunev     .name = "parallels",
67d6179011SDenis V. Lunev     .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
68d6179011SDenis V. Lunev     .desc = {
69d6179011SDenis V. Lunev         {
70d6179011SDenis V. Lunev             .name = PARALLELS_OPT_PREALLOC_SIZE,
71d6179011SDenis V. Lunev             .type = QEMU_OPT_SIZE,
72d6179011SDenis V. Lunev             .help = "Preallocation size on image expansion",
73ff5bbe56SEdgar Kaziahmedov             .def_value_str = "128M",
74d6179011SDenis V. Lunev         },
75d6179011SDenis V. Lunev         {
76d6179011SDenis V. Lunev             .name = PARALLELS_OPT_PREALLOC_MODE,
77d6179011SDenis V. Lunev             .type = QEMU_OPT_STRING,
78d6179011SDenis V. Lunev             .help = "Preallocation mode on image expansion "
79d6179011SDenis V. Lunev                     "(allowed values: falloc, truncate)",
80d6179011SDenis V. Lunev             .def_value_str = "falloc",
81d6179011SDenis V. Lunev         },
82d6179011SDenis V. Lunev         { /* end of list */ },
83d6179011SDenis V. Lunev     },
84d6179011SDenis V. Lunev };
85d6179011SDenis V. Lunev 
861511b490SKevin Wolf static QemuOptsList parallels_create_opts = {
871511b490SKevin Wolf     .name = "parallels-create-opts",
881511b490SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
891511b490SKevin Wolf     .desc = {
901511b490SKevin Wolf         {
911511b490SKevin Wolf             .name = BLOCK_OPT_SIZE,
921511b490SKevin Wolf             .type = QEMU_OPT_SIZE,
931511b490SKevin Wolf             .help = "Virtual disk size",
941511b490SKevin Wolf         },
951511b490SKevin Wolf         {
961511b490SKevin Wolf             .name = BLOCK_OPT_CLUSTER_SIZE,
971511b490SKevin Wolf             .type = QEMU_OPT_SIZE,
981511b490SKevin Wolf             .help = "Parallels image cluster size",
991511b490SKevin Wolf             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
1001511b490SKevin Wolf         },
1011511b490SKevin Wolf         { /* end of list */ }
1021511b490SKevin Wolf     }
1031511b490SKevin Wolf };
1041511b490SKevin Wolf 
105d6179011SDenis V. Lunev 
106555cc9d9SDenis V. Lunev static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
107555cc9d9SDenis V. Lunev {
108dd97cdc0SDenis V. Lunev     return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
109555cc9d9SDenis V. Lunev }
110555cc9d9SDenis V. Lunev 
1112d68e22eSDenis V. Lunev static uint32_t bat_entry_off(uint32_t idx)
1122d68e22eSDenis V. Lunev {
1132d68e22eSDenis V. Lunev     return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
1142d68e22eSDenis V. Lunev }
1152d68e22eSDenis V. Lunev 
11629442569SRoman Kagan static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
117019d6b8fSAnthony Liguori {
118c34d2451SDavid Woodhouse     uint32_t index, offset;
119019d6b8fSAnthony Liguori 
120019d6b8fSAnthony Liguori     index = sector_num / s->tracks;
121019d6b8fSAnthony Liguori     offset = sector_num % s->tracks;
122019d6b8fSAnthony Liguori 
1239d8b88f6SChristoph Hellwig     /* not allocated */
124369f7de9SDenis V. Lunev     if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
125019d6b8fSAnthony Liguori         return -1;
126369f7de9SDenis V. Lunev     }
127555cc9d9SDenis V. Lunev     return bat2sect(s, index) + offset;
128019d6b8fSAnthony Liguori }
129019d6b8fSAnthony Liguori 
1309de9da17SRoman Kagan static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
1319de9da17SRoman Kagan         int nb_sectors)
1329de9da17SRoman Kagan {
1339de9da17SRoman Kagan     int ret = s->tracks - sector_num % s->tracks;
1349de9da17SRoman Kagan     return MIN(nb_sectors, ret);
1359de9da17SRoman Kagan }
1369de9da17SRoman Kagan 
1376953d920SDenis V. Lunev static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
1386953d920SDenis V. Lunev                             int nb_sectors, int *pnum)
1396953d920SDenis V. Lunev {
1406953d920SDenis V. Lunev     int64_t start_off = -2, prev_end_off = -2;
1416953d920SDenis V. Lunev 
1426953d920SDenis V. Lunev     *pnum = 0;
1436953d920SDenis V. Lunev     while (nb_sectors > 0 || start_off == -2) {
1446953d920SDenis V. Lunev         int64_t offset = seek_to_sector(s, sector_num);
1456953d920SDenis V. Lunev         int to_end;
1466953d920SDenis V. Lunev 
1476953d920SDenis V. Lunev         if (start_off == -2) {
1486953d920SDenis V. Lunev             start_off = offset;
1496953d920SDenis V. Lunev             prev_end_off = offset;
1506953d920SDenis V. Lunev         } else if (offset != prev_end_off) {
1516953d920SDenis V. Lunev             break;
1526953d920SDenis V. Lunev         }
1536953d920SDenis V. Lunev 
1546953d920SDenis V. Lunev         to_end = cluster_remainder(s, sector_num, nb_sectors);
1556953d920SDenis V. Lunev         nb_sectors -= to_end;
1566953d920SDenis V. Lunev         sector_num += to_end;
1576953d920SDenis V. Lunev         *pnum += to_end;
1586953d920SDenis V. Lunev 
1596953d920SDenis V. Lunev         if (offset > 0) {
1606953d920SDenis V. Lunev             prev_end_off += to_end;
1616953d920SDenis V. Lunev         }
1626953d920SDenis V. Lunev     }
1636953d920SDenis V. Lunev     return start_off;
1646953d920SDenis V. Lunev }
1656953d920SDenis V. Lunev 
166ddd2ef2cSDenis V. Lunev static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
167ddd2ef2cSDenis V. Lunev                                  int nb_sectors, int *pnum)
1685a41e1faSDenis V. Lunev {
169bcbb3866SEdgar Kaziakhmedov     int ret;
1705a41e1faSDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
171d8b83e37SDenis V. Lunev     int64_t pos, space, idx, to_allocate, i, len;
172ddd2ef2cSDenis V. Lunev 
173ddd2ef2cSDenis V. Lunev     pos = block_status(s, sector_num, nb_sectors, pnum);
174ddd2ef2cSDenis V. Lunev     if (pos > 0) {
175ddd2ef2cSDenis V. Lunev         return pos;
176ddd2ef2cSDenis V. Lunev     }
1775a41e1faSDenis V. Lunev 
1785a41e1faSDenis V. Lunev     idx = sector_num / s->tracks;
179969401feSLaurent Vivier     to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
18086d1bd70SMax Reitz 
18186d1bd70SMax Reitz     /* This function is called only by parallels_co_writev(), which will never
18286d1bd70SMax Reitz      * pass a sector_num at or beyond the end of the image (because the block
18386d1bd70SMax Reitz      * layer never passes such a sector_num to that function). Therefore, idx
18486d1bd70SMax Reitz      * is always below s->bat_size.
18586d1bd70SMax Reitz      * block_status() will limit *pnum so that sector_num + *pnum will not
18686d1bd70SMax Reitz      * exceed the image end. Therefore, idx + to_allocate cannot exceed
18786d1bd70SMax Reitz      * s->bat_size.
18886d1bd70SMax Reitz      * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
18986d1bd70SMax Reitz      * will always fit into a uint32_t. */
19086d1bd70SMax Reitz     assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
19186d1bd70SMax Reitz 
192ddd2ef2cSDenis V. Lunev     space = to_allocate * s->tracks;
193d8b83e37SDenis V. Lunev     len = bdrv_getlength(bs->file->bs);
194d8b83e37SDenis V. Lunev     if (len < 0) {
195d8b83e37SDenis V. Lunev         return len;
196d8b83e37SDenis V. Lunev     }
197d8b83e37SDenis V. Lunev     if (s->data_end + space > (len >> BDRV_SECTOR_BITS)) {
198ddd2ef2cSDenis V. Lunev         space += s->prealloc_size;
19919f5dc15SDenis V. Lunev         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
200720ff280SKevin Wolf             ret = bdrv_pwrite_zeroes(bs->file,
20174021bc4SEric Blake                                      s->data_end << BDRV_SECTOR_BITS,
20274021bc4SEric Blake                                      space << BDRV_SECTOR_BITS, 0);
2035a41e1faSDenis V. Lunev         } else {
20452cdbc58SKevin Wolf             ret = bdrv_truncate(bs->file,
205ed3d2ec9SMax Reitz                                 (s->data_end + space) << BDRV_SECTOR_BITS,
206c80d8b06SMax Reitz                                 false, PREALLOC_MODE_OFF, NULL);
2075a41e1faSDenis V. Lunev         }
2085a41e1faSDenis V. Lunev         if (ret < 0) {
2095a41e1faSDenis V. Lunev             return ret;
2105a41e1faSDenis V. Lunev         }
21119f5dc15SDenis V. Lunev     }
212ddd2ef2cSDenis V. Lunev 
213bcbb3866SEdgar Kaziakhmedov     /* Try to read from backing to fill empty clusters
214bcbb3866SEdgar Kaziakhmedov      * FIXME: 1. previous write_zeroes may be redundant
215bcbb3866SEdgar Kaziakhmedov      *        2. most of data we read from backing will be rewritten by
216bcbb3866SEdgar Kaziakhmedov      *           parallels_co_writev. On aligned-to-cluster write we do not need
217bcbb3866SEdgar Kaziakhmedov      *           this read at all.
218bcbb3866SEdgar Kaziakhmedov      *        3. it would be good to combine write of data from backing and new
219bcbb3866SEdgar Kaziakhmedov      *           data into one write call */
220bcbb3866SEdgar Kaziakhmedov     if (bs->backing) {
221bcbb3866SEdgar Kaziakhmedov         int64_t nb_cow_sectors = to_allocate * s->tracks;
222bcbb3866SEdgar Kaziakhmedov         int64_t nb_cow_bytes = nb_cow_sectors << BDRV_SECTOR_BITS;
223a4072543SVladimir Sementsov-Ogievskiy         void *buf = qemu_blockalign(bs, nb_cow_bytes);
224bcbb3866SEdgar Kaziakhmedov 
225a4072543SVladimir Sementsov-Ogievskiy         ret = bdrv_co_pread(bs->backing, idx * s->tracks * BDRV_SECTOR_SIZE,
226a4072543SVladimir Sementsov-Ogievskiy                             nb_cow_bytes, buf, 0);
227bcbb3866SEdgar Kaziakhmedov         if (ret < 0) {
228a4072543SVladimir Sementsov-Ogievskiy             qemu_vfree(buf);
229bcbb3866SEdgar Kaziakhmedov             return ret;
230bcbb3866SEdgar Kaziakhmedov         }
231bcbb3866SEdgar Kaziakhmedov 
232d08c2a24SEric Blake         ret = bdrv_co_pwritev(bs->file, s->data_end * BDRV_SECTOR_SIZE,
233a4072543SVladimir Sementsov-Ogievskiy                               nb_cow_bytes, buf, 0);
234a4072543SVladimir Sementsov-Ogievskiy         qemu_vfree(buf);
235bcbb3866SEdgar Kaziakhmedov         if (ret < 0) {
236bcbb3866SEdgar Kaziakhmedov             return ret;
237bcbb3866SEdgar Kaziakhmedov         }
238bcbb3866SEdgar Kaziakhmedov     }
239bcbb3866SEdgar Kaziakhmedov 
240ddd2ef2cSDenis V. Lunev     for (i = 0; i < to_allocate; i++) {
241ddd2ef2cSDenis V. Lunev         s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
24219f5dc15SDenis V. Lunev         s->data_end += s->tracks;
243ddd2ef2cSDenis V. Lunev         bitmap_set(s->bat_dirty_bmap,
244c9f6856dSVladimir Sementsov-Ogievskiy                    bat_entry_off(idx + i) / s->bat_dirty_block, 1);
245ddd2ef2cSDenis V. Lunev     }
2465a41e1faSDenis V. Lunev 
247ddd2ef2cSDenis V. Lunev     return bat2sect(s, idx) + sector_num % s->tracks;
2485a41e1faSDenis V. Lunev }
2495a41e1faSDenis V. Lunev 
2500d31c7c2SDenis V. Lunev 
2510d31c7c2SDenis V. Lunev static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
2520d31c7c2SDenis V. Lunev {
2530d31c7c2SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
2540d31c7c2SDenis V. Lunev     unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
2550d31c7c2SDenis V. Lunev     unsigned long bit;
2560d31c7c2SDenis V. Lunev 
2570d31c7c2SDenis V. Lunev     qemu_co_mutex_lock(&s->lock);
2580d31c7c2SDenis V. Lunev 
2590d31c7c2SDenis V. Lunev     bit = find_first_bit(s->bat_dirty_bmap, size);
2600d31c7c2SDenis V. Lunev     while (bit < size) {
2610d31c7c2SDenis V. Lunev         uint32_t off = bit * s->bat_dirty_block;
2620d31c7c2SDenis V. Lunev         uint32_t to_write = s->bat_dirty_block;
2630d31c7c2SDenis V. Lunev         int ret;
2640d31c7c2SDenis V. Lunev 
2650d31c7c2SDenis V. Lunev         if (off + to_write > s->header_size) {
2660d31c7c2SDenis V. Lunev             to_write = s->header_size - off;
2670d31c7c2SDenis V. Lunev         }
268d9ca2ea2SKevin Wolf         ret = bdrv_pwrite(bs->file, off, (uint8_t *)s->header + off,
2699a4f4c31SKevin Wolf                           to_write);
2700d31c7c2SDenis V. Lunev         if (ret < 0) {
2710d31c7c2SDenis V. Lunev             qemu_co_mutex_unlock(&s->lock);
2720d31c7c2SDenis V. Lunev             return ret;
2730d31c7c2SDenis V. Lunev         }
2740d31c7c2SDenis V. Lunev         bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
2750d31c7c2SDenis V. Lunev     }
2760d31c7c2SDenis V. Lunev     bitmap_zero(s->bat_dirty_bmap, size);
2770d31c7c2SDenis V. Lunev 
2780d31c7c2SDenis V. Lunev     qemu_co_mutex_unlock(&s->lock);
2790d31c7c2SDenis V. Lunev     return 0;
2800d31c7c2SDenis V. Lunev }
2810d31c7c2SDenis V. Lunev 
2820d31c7c2SDenis V. Lunev 
2838e0cf59dSEric Blake static int coroutine_fn parallels_co_block_status(BlockDriverState *bs,
2848e0cf59dSEric Blake                                                   bool want_zero,
2858e0cf59dSEric Blake                                                   int64_t offset,
2868e0cf59dSEric Blake                                                   int64_t bytes,
2878e0cf59dSEric Blake                                                   int64_t *pnum,
2888e0cf59dSEric Blake                                                   int64_t *map,
2898e0cf59dSEric Blake                                                   BlockDriverState **file)
290dd3bed16SRoman Kagan {
291dd3bed16SRoman Kagan     BDRVParallelsState *s = bs->opaque;
2928e0cf59dSEric Blake     int count;
293dd3bed16SRoman Kagan 
2948e0cf59dSEric Blake     assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE));
295dd3bed16SRoman Kagan     qemu_co_mutex_lock(&s->lock);
2968e0cf59dSEric Blake     offset = block_status(s, offset >> BDRV_SECTOR_BITS,
2978e0cf59dSEric Blake                           bytes >> BDRV_SECTOR_BITS, &count);
298dd3bed16SRoman Kagan     qemu_co_mutex_unlock(&s->lock);
299dd3bed16SRoman Kagan 
3008e0cf59dSEric Blake     *pnum = count * BDRV_SECTOR_SIZE;
301dd3bed16SRoman Kagan     if (offset < 0) {
302dd3bed16SRoman Kagan         return 0;
303dd3bed16SRoman Kagan     }
304dd3bed16SRoman Kagan 
3058e0cf59dSEric Blake     *map = offset * BDRV_SECTOR_SIZE;
306ddf4987dSFam Zheng     *file = bs->file->bs;
3078e0cf59dSEric Blake     return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
308dd3bed16SRoman Kagan }
309dd3bed16SRoman Kagan 
3105a41e1faSDenis V. Lunev static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
311e18a58b4SEric Blake                                             int64_t sector_num, int nb_sectors,
312e18a58b4SEric Blake                                             QEMUIOVector *qiov, int flags)
3135a41e1faSDenis V. Lunev {
3145a41e1faSDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
3155a41e1faSDenis V. Lunev     uint64_t bytes_done = 0;
3165a41e1faSDenis V. Lunev     QEMUIOVector hd_qiov;
3175a41e1faSDenis V. Lunev     int ret = 0;
3185a41e1faSDenis V. Lunev 
319e18a58b4SEric Blake     assert(!flags);
3205a41e1faSDenis V. Lunev     qemu_iovec_init(&hd_qiov, qiov->niov);
3215a41e1faSDenis V. Lunev 
3225a41e1faSDenis V. Lunev     while (nb_sectors > 0) {
3235a41e1faSDenis V. Lunev         int64_t position;
3245a41e1faSDenis V. Lunev         int n, nbytes;
3255a41e1faSDenis V. Lunev 
3265a41e1faSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
327ddd2ef2cSDenis V. Lunev         position = allocate_clusters(bs, sector_num, nb_sectors, &n);
3285a41e1faSDenis V. Lunev         qemu_co_mutex_unlock(&s->lock);
3295a41e1faSDenis V. Lunev         if (position < 0) {
3305a41e1faSDenis V. Lunev             ret = (int)position;
3315a41e1faSDenis V. Lunev             break;
3325a41e1faSDenis V. Lunev         }
3335a41e1faSDenis V. Lunev 
3345a41e1faSDenis V. Lunev         nbytes = n << BDRV_SECTOR_BITS;
3355a41e1faSDenis V. Lunev 
3365a41e1faSDenis V. Lunev         qemu_iovec_reset(&hd_qiov);
3375a41e1faSDenis V. Lunev         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
3385a41e1faSDenis V. Lunev 
339d08c2a24SEric Blake         ret = bdrv_co_pwritev(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
340d08c2a24SEric Blake                               &hd_qiov, 0);
3415a41e1faSDenis V. Lunev         if (ret < 0) {
3425a41e1faSDenis V. Lunev             break;
3435a41e1faSDenis V. Lunev         }
3445a41e1faSDenis V. Lunev 
3455a41e1faSDenis V. Lunev         nb_sectors -= n;
3465a41e1faSDenis V. Lunev         sector_num += n;
3475a41e1faSDenis V. Lunev         bytes_done += nbytes;
3485a41e1faSDenis V. Lunev     }
3495a41e1faSDenis V. Lunev 
3505a41e1faSDenis V. Lunev     qemu_iovec_destroy(&hd_qiov);
3515a41e1faSDenis V. Lunev     return ret;
3525a41e1faSDenis V. Lunev }
3535a41e1faSDenis V. Lunev 
354481fb9cfSDenis V. Lunev static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
355481fb9cfSDenis V. Lunev         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
356019d6b8fSAnthony Liguori {
35729442569SRoman Kagan     BDRVParallelsState *s = bs->opaque;
358481fb9cfSDenis V. Lunev     uint64_t bytes_done = 0;
359481fb9cfSDenis V. Lunev     QEMUIOVector hd_qiov;
360481fb9cfSDenis V. Lunev     int ret = 0;
361481fb9cfSDenis V. Lunev 
362481fb9cfSDenis V. Lunev     qemu_iovec_init(&hd_qiov, qiov->niov);
36329442569SRoman Kagan 
364019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
365481fb9cfSDenis V. Lunev         int64_t position;
366481fb9cfSDenis V. Lunev         int n, nbytes;
367481fb9cfSDenis V. Lunev 
368481fb9cfSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
3696953d920SDenis V. Lunev         position = block_status(s, sector_num, nb_sectors, &n);
370481fb9cfSDenis V. Lunev         qemu_co_mutex_unlock(&s->lock);
371481fb9cfSDenis V. Lunev 
372481fb9cfSDenis V. Lunev         nbytes = n << BDRV_SECTOR_BITS;
373481fb9cfSDenis V. Lunev 
374481fb9cfSDenis V. Lunev         qemu_iovec_reset(&hd_qiov);
375481fb9cfSDenis V. Lunev         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
376481fb9cfSDenis V. Lunev 
377bcbb3866SEdgar Kaziakhmedov         if (position < 0) {
378bcbb3866SEdgar Kaziakhmedov             if (bs->backing) {
379d08c2a24SEric Blake                 ret = bdrv_co_preadv(bs->backing, sector_num * BDRV_SECTOR_SIZE,
380d08c2a24SEric Blake                                      nbytes, &hd_qiov, 0);
381bcbb3866SEdgar Kaziakhmedov                 if (ret < 0) {
382bcbb3866SEdgar Kaziakhmedov                     break;
383bcbb3866SEdgar Kaziakhmedov                 }
384bcbb3866SEdgar Kaziakhmedov             } else {
385bcbb3866SEdgar Kaziakhmedov                 qemu_iovec_memset(&hd_qiov, 0, 0, nbytes);
386bcbb3866SEdgar Kaziakhmedov             }
387bcbb3866SEdgar Kaziakhmedov         } else {
388d08c2a24SEric Blake             ret = bdrv_co_preadv(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
389d08c2a24SEric Blake                                  &hd_qiov, 0);
390481fb9cfSDenis V. Lunev             if (ret < 0) {
391481fb9cfSDenis V. Lunev                 break;
3929d8b88f6SChristoph Hellwig             }
393019d6b8fSAnthony Liguori         }
394019d6b8fSAnthony Liguori 
395481fb9cfSDenis V. Lunev         nb_sectors -= n;
396481fb9cfSDenis V. Lunev         sector_num += n;
397481fb9cfSDenis V. Lunev         bytes_done += nbytes;
398481fb9cfSDenis V. Lunev     }
399481fb9cfSDenis V. Lunev 
400481fb9cfSDenis V. Lunev     qemu_iovec_destroy(&hd_qiov);
4012914caa0SPaolo Bonzini     return ret;
4022914caa0SPaolo Bonzini }
4032914caa0SPaolo Bonzini 
40449ad6467SDenis V. Lunev 
4052fd61638SPaolo Bonzini static int coroutine_fn parallels_co_check(BlockDriverState *bs,
4062fd61638SPaolo Bonzini                                            BdrvCheckResult *res,
40749ad6467SDenis V. Lunev                                            BdrvCheckMode fix)
40849ad6467SDenis V. Lunev {
40949ad6467SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
41049ad6467SDenis V. Lunev     int64_t size, prev_off, high_off;
41149ad6467SDenis V. Lunev     int ret;
41249ad6467SDenis V. Lunev     uint32_t i;
41349ad6467SDenis V. Lunev     bool flush_bat = false;
41449ad6467SDenis V. Lunev     int cluster_size = s->tracks << BDRV_SECTOR_BITS;
41549ad6467SDenis V. Lunev 
4169a4f4c31SKevin Wolf     size = bdrv_getlength(bs->file->bs);
41749ad6467SDenis V. Lunev     if (size < 0) {
41849ad6467SDenis V. Lunev         res->check_errors++;
41949ad6467SDenis V. Lunev         return size;
42049ad6467SDenis V. Lunev     }
42149ad6467SDenis V. Lunev 
4222fd61638SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
4236dd6b9f1SDenis V. Lunev     if (s->header_unclean) {
4246dd6b9f1SDenis V. Lunev         fprintf(stderr, "%s image was not closed correctly\n",
4256dd6b9f1SDenis V. Lunev                 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
4266dd6b9f1SDenis V. Lunev         res->corruptions++;
4276dd6b9f1SDenis V. Lunev         if (fix & BDRV_FIX_ERRORS) {
4286dd6b9f1SDenis V. Lunev             /* parallels_close will do the job right */
4296dd6b9f1SDenis V. Lunev             res->corruptions_fixed++;
4306dd6b9f1SDenis V. Lunev             s->header_unclean = false;
4316dd6b9f1SDenis V. Lunev         }
4326dd6b9f1SDenis V. Lunev     }
4336dd6b9f1SDenis V. Lunev 
43449ad6467SDenis V. Lunev     res->bfi.total_clusters = s->bat_size;
43549ad6467SDenis V. Lunev     res->bfi.compressed_clusters = 0; /* compression is not supported */
43649ad6467SDenis V. Lunev 
43749ad6467SDenis V. Lunev     high_off = 0;
43849ad6467SDenis V. Lunev     prev_off = 0;
43949ad6467SDenis V. Lunev     for (i = 0; i < s->bat_size; i++) {
44049ad6467SDenis V. Lunev         int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
44149ad6467SDenis V. Lunev         if (off == 0) {
44249ad6467SDenis V. Lunev             prev_off = 0;
44349ad6467SDenis V. Lunev             continue;
44449ad6467SDenis V. Lunev         }
44549ad6467SDenis V. Lunev 
44649ad6467SDenis V. Lunev         /* cluster outside the image */
44749ad6467SDenis V. Lunev         if (off > size) {
44849ad6467SDenis V. Lunev             fprintf(stderr, "%s cluster %u is outside image\n",
44949ad6467SDenis V. Lunev                     fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
45049ad6467SDenis V. Lunev             res->corruptions++;
45149ad6467SDenis V. Lunev             if (fix & BDRV_FIX_ERRORS) {
45249ad6467SDenis V. Lunev                 prev_off = 0;
45349ad6467SDenis V. Lunev                 s->bat_bitmap[i] = 0;
45449ad6467SDenis V. Lunev                 res->corruptions_fixed++;
45549ad6467SDenis V. Lunev                 flush_bat = true;
45649ad6467SDenis V. Lunev                 continue;
45749ad6467SDenis V. Lunev             }
45849ad6467SDenis V. Lunev         }
45949ad6467SDenis V. Lunev 
46049ad6467SDenis V. Lunev         res->bfi.allocated_clusters++;
46149ad6467SDenis V. Lunev         if (off > high_off) {
46249ad6467SDenis V. Lunev             high_off = off;
46349ad6467SDenis V. Lunev         }
46449ad6467SDenis V. Lunev 
46549ad6467SDenis V. Lunev         if (prev_off != 0 && (prev_off + cluster_size) != off) {
46649ad6467SDenis V. Lunev             res->bfi.fragmented_clusters++;
46749ad6467SDenis V. Lunev         }
46849ad6467SDenis V. Lunev         prev_off = off;
46949ad6467SDenis V. Lunev     }
47049ad6467SDenis V. Lunev 
4712fd61638SPaolo Bonzini     ret = 0;
47249ad6467SDenis V. Lunev     if (flush_bat) {
473d9ca2ea2SKevin Wolf         ret = bdrv_pwrite_sync(bs->file, 0, s->header, s->header_size);
47449ad6467SDenis V. Lunev         if (ret < 0) {
47549ad6467SDenis V. Lunev             res->check_errors++;
4762fd61638SPaolo Bonzini             goto out;
47749ad6467SDenis V. Lunev         }
47849ad6467SDenis V. Lunev     }
47949ad6467SDenis V. Lunev 
48049ad6467SDenis V. Lunev     res->image_end_offset = high_off + cluster_size;
48149ad6467SDenis V. Lunev     if (size > res->image_end_offset) {
48249ad6467SDenis V. Lunev         int64_t count;
48349ad6467SDenis V. Lunev         count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
48449ad6467SDenis V. Lunev         fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
48549ad6467SDenis V. Lunev                 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
48649ad6467SDenis V. Lunev                 size - res->image_end_offset);
48749ad6467SDenis V. Lunev         res->leaks += count;
48849ad6467SDenis V. Lunev         if (fix & BDRV_FIX_LEAKS) {
489ed3d2ec9SMax Reitz             Error *local_err = NULL;
490e8d04f92SMax Reitz 
491e8d04f92SMax Reitz             /*
492e8d04f92SMax Reitz              * In order to really repair the image, we must shrink it.
493e8d04f92SMax Reitz              * That means we have to pass exact=true.
494e8d04f92SMax Reitz              */
495e8d04f92SMax Reitz             ret = bdrv_truncate(bs->file, res->image_end_offset, true,
4967ea37c30SMax Reitz                                 PREALLOC_MODE_OFF, &local_err);
49749ad6467SDenis V. Lunev             if (ret < 0) {
498ed3d2ec9SMax Reitz                 error_report_err(local_err);
49949ad6467SDenis V. Lunev                 res->check_errors++;
5002fd61638SPaolo Bonzini                 goto out;
50149ad6467SDenis V. Lunev             }
50249ad6467SDenis V. Lunev             res->leaks_fixed += count;
50349ad6467SDenis V. Lunev         }
50449ad6467SDenis V. Lunev     }
50549ad6467SDenis V. Lunev 
5062fd61638SPaolo Bonzini out:
5072fd61638SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
5082fd61638SPaolo Bonzini     return ret;
50949ad6467SDenis V. Lunev }
51049ad6467SDenis V. Lunev 
51149ad6467SDenis V. Lunev 
5121511b490SKevin Wolf static int coroutine_fn parallels_co_create(BlockdevCreateOptions* opts,
513efc75e2aSStefan Hajnoczi                                             Error **errp)
51474cf6c50SDenis V. Lunev {
5151511b490SKevin Wolf     BlockdevCreateOptionsParallels *parallels_opts;
5161511b490SKevin Wolf     BlockDriverState *bs;
5171511b490SKevin Wolf     BlockBackend *blk;
51874cf6c50SDenis V. Lunev     int64_t total_size, cl_size;
519369f7de9SDenis V. Lunev     uint32_t bat_entries, bat_sectors;
52074cf6c50SDenis V. Lunev     ParallelsHeader header;
5211511b490SKevin Wolf     uint8_t tmp[BDRV_SECTOR_SIZE];
52274cf6c50SDenis V. Lunev     int ret;
52374cf6c50SDenis V. Lunev 
5241511b490SKevin Wolf     assert(opts->driver == BLOCKDEV_DRIVER_PARALLELS);
5251511b490SKevin Wolf     parallels_opts = &opts->u.parallels;
5261511b490SKevin Wolf 
5271511b490SKevin Wolf     /* Sanity checks */
5281511b490SKevin Wolf     total_size = parallels_opts->size;
5291511b490SKevin Wolf 
5301511b490SKevin Wolf     if (parallels_opts->has_cluster_size) {
5311511b490SKevin Wolf         cl_size = parallels_opts->cluster_size;
5321511b490SKevin Wolf     } else {
5331511b490SKevin Wolf         cl_size = DEFAULT_CLUSTER_SIZE;
5341511b490SKevin Wolf     }
5351511b490SKevin Wolf 
5362332d825SKevin Wolf     /* XXX What is the real limit here? This is an insanely large maximum. */
5372332d825SKevin Wolf     if (cl_size >= INT64_MAX / MAX_PARALLELS_IMAGE_FACTOR) {
5382332d825SKevin Wolf         error_setg(errp, "Cluster size is too large");
5392332d825SKevin Wolf         return -EINVAL;
5402332d825SKevin Wolf     }
541555a608cSKlim Kireev     if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) {
5421511b490SKevin Wolf         error_setg(errp, "Image size is too large for this cluster size");
543555a608cSKlim Kireev         return -E2BIG;
544555a608cSKlim Kireev     }
54574cf6c50SDenis V. Lunev 
5461511b490SKevin Wolf     if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) {
5471511b490SKevin Wolf         error_setg(errp, "Image size must be a multiple of 512 bytes");
5481511b490SKevin Wolf         return -EINVAL;
54974cf6c50SDenis V. Lunev     }
55074cf6c50SDenis V. Lunev 
5511511b490SKevin Wolf     if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) {
5521511b490SKevin Wolf         error_setg(errp, "Cluster size must be a multiple of 512 bytes");
5531511b490SKevin Wolf         return -EINVAL;
5541511b490SKevin Wolf     }
5551511b490SKevin Wolf 
5561511b490SKevin Wolf     /* Create BlockBackend to write to the image */
5571511b490SKevin Wolf     bs = bdrv_open_blockdev_ref(parallels_opts->file, errp);
5581511b490SKevin Wolf     if (bs == NULL) {
5598942764fSKevin Wolf         return -EIO;
56074cf6c50SDenis V. Lunev     }
5618942764fSKevin Wolf 
562d861ab3aSKevin Wolf     blk = blk_new(bdrv_get_aio_context(bs),
563d861ab3aSKevin Wolf                   BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
5641511b490SKevin Wolf     ret = blk_insert_bs(blk, bs, errp);
56574cf6c50SDenis V. Lunev     if (ret < 0) {
5661511b490SKevin Wolf         goto out;
5671511b490SKevin Wolf     }
5681511b490SKevin Wolf     blk_set_allow_write_beyond_eof(blk, true);
5691511b490SKevin Wolf 
5701511b490SKevin Wolf     /* Create image format */
571369f7de9SDenis V. Lunev     bat_entries = DIV_ROUND_UP(total_size, cl_size);
5722d68e22eSDenis V. Lunev     bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
573369f7de9SDenis V. Lunev     bat_sectors = (bat_sectors *  cl_size) >> BDRV_SECTOR_BITS;
57474cf6c50SDenis V. Lunev 
57574cf6c50SDenis V. Lunev     memset(&header, 0, sizeof(header));
57674cf6c50SDenis V. Lunev     memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
57774cf6c50SDenis V. Lunev     header.version = cpu_to_le32(HEADER_VERSION);
57874cf6c50SDenis V. Lunev     /* don't care much about geometry, it is not used on image level */
579908b1c84SKlim Kireev     header.heads = cpu_to_le32(HEADS_NUMBER);
580908b1c84SKlim Kireev     header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE
581908b1c84SKlim Kireev                                    / HEADS_NUMBER / SEC_IN_CYL);
58274cf6c50SDenis V. Lunev     header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
583369f7de9SDenis V. Lunev     header.bat_entries = cpu_to_le32(bat_entries);
58474cf6c50SDenis V. Lunev     header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
585369f7de9SDenis V. Lunev     header.data_off = cpu_to_le32(bat_sectors);
58674cf6c50SDenis V. Lunev 
58774cf6c50SDenis V. Lunev     /* write all the data */
58874cf6c50SDenis V. Lunev     memset(tmp, 0, sizeof(tmp));
58974cf6c50SDenis V. Lunev     memcpy(tmp, &header, sizeof(header));
59074cf6c50SDenis V. Lunev 
5911511b490SKevin Wolf     ret = blk_pwrite(blk, 0, tmp, BDRV_SECTOR_SIZE, 0);
59274cf6c50SDenis V. Lunev     if (ret < 0) {
59374cf6c50SDenis V. Lunev         goto exit;
59474cf6c50SDenis V. Lunev     }
5951511b490SKevin Wolf     ret = blk_pwrite_zeroes(blk, BDRV_SECTOR_SIZE,
596983a1600SEric Blake                             (bat_sectors - 1) << BDRV_SECTOR_BITS, 0);
59774cf6c50SDenis V. Lunev     if (ret < 0) {
59874cf6c50SDenis V. Lunev         goto exit;
59974cf6c50SDenis V. Lunev     }
60074cf6c50SDenis V. Lunev 
6011511b490SKevin Wolf     ret = 0;
6021511b490SKevin Wolf out:
6031511b490SKevin Wolf     blk_unref(blk);
6041511b490SKevin Wolf     bdrv_unref(bs);
60574cf6c50SDenis V. Lunev     return ret;
60674cf6c50SDenis V. Lunev 
60774cf6c50SDenis V. Lunev exit:
60874cf6c50SDenis V. Lunev     error_setg_errno(errp, -ret, "Failed to create Parallels image");
6091511b490SKevin Wolf     goto out;
6101511b490SKevin Wolf }
6111511b490SKevin Wolf 
612*b92902dfSMaxim Levitsky static int coroutine_fn parallels_co_create_opts(BlockDriver *drv,
613*b92902dfSMaxim Levitsky                                                  const char *filename,
6141511b490SKevin Wolf                                                  QemuOpts *opts,
6151511b490SKevin Wolf                                                  Error **errp)
6161511b490SKevin Wolf {
6171511b490SKevin Wolf     BlockdevCreateOptions *create_options = NULL;
6181511b490SKevin Wolf     Error *local_err = NULL;
6191511b490SKevin Wolf     BlockDriverState *bs = NULL;
62092adf9dbSMarkus Armbruster     QDict *qdict;
6211511b490SKevin Wolf     Visitor *v;
6221511b490SKevin Wolf     int ret;
6231511b490SKevin Wolf 
6241511b490SKevin Wolf     static const QDictRenames opt_renames[] = {
6251511b490SKevin Wolf         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
6261511b490SKevin Wolf         { NULL, NULL },
6271511b490SKevin Wolf     };
6281511b490SKevin Wolf 
6291511b490SKevin Wolf     /* Parse options and convert legacy syntax */
6301511b490SKevin Wolf     qdict = qemu_opts_to_qdict_filtered(opts, NULL, &parallels_create_opts,
6311511b490SKevin Wolf                                         true);
6321511b490SKevin Wolf 
6331511b490SKevin Wolf     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
6341511b490SKevin Wolf         ret = -EINVAL;
63574cf6c50SDenis V. Lunev         goto done;
63674cf6c50SDenis V. Lunev     }
63774cf6c50SDenis V. Lunev 
6381511b490SKevin Wolf     /* Create and open the file (protocol layer) */
6391511b490SKevin Wolf     ret = bdrv_create_file(filename, opts, &local_err);
6401511b490SKevin Wolf     if (ret < 0) {
6411511b490SKevin Wolf         error_propagate(errp, local_err);
6421511b490SKevin Wolf         goto done;
6431511b490SKevin Wolf     }
6441511b490SKevin Wolf 
6451511b490SKevin Wolf     bs = bdrv_open(filename, NULL, NULL,
6461511b490SKevin Wolf                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
6471511b490SKevin Wolf     if (bs == NULL) {
6481511b490SKevin Wolf         ret = -EIO;
6491511b490SKevin Wolf         goto done;
6501511b490SKevin Wolf     }
6511511b490SKevin Wolf 
6521511b490SKevin Wolf     /* Now get the QAPI type BlockdevCreateOptions */
6531511b490SKevin Wolf     qdict_put_str(qdict, "driver", "parallels");
6541511b490SKevin Wolf     qdict_put_str(qdict, "file", bs->node_name);
6551511b490SKevin Wolf 
656af91062eSMarkus Armbruster     v = qobject_input_visitor_new_flat_confused(qdict, errp);
657af91062eSMarkus Armbruster     if (!v) {
6581511b490SKevin Wolf         ret = -EINVAL;
6591511b490SKevin Wolf         goto done;
6601511b490SKevin Wolf     }
6611511b490SKevin Wolf 
6621511b490SKevin Wolf     visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
6631511b490SKevin Wolf     visit_free(v);
6641511b490SKevin Wolf 
6651511b490SKevin Wolf     if (local_err) {
6661511b490SKevin Wolf         error_propagate(errp, local_err);
6671511b490SKevin Wolf         ret = -EINVAL;
6681511b490SKevin Wolf         goto done;
6691511b490SKevin Wolf     }
6701511b490SKevin Wolf 
6711511b490SKevin Wolf     /* Silently round up sizes */
6721511b490SKevin Wolf     create_options->u.parallels.size =
6731511b490SKevin Wolf         ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE);
6741511b490SKevin Wolf     create_options->u.parallels.cluster_size =
6751511b490SKevin Wolf         ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZE);
6761511b490SKevin Wolf 
6771511b490SKevin Wolf     /* Create the Parallels image (format layer) */
6781511b490SKevin Wolf     ret = parallels_co_create(create_options, errp);
6791511b490SKevin Wolf     if (ret < 0) {
6801511b490SKevin Wolf         goto done;
6811511b490SKevin Wolf     }
6821511b490SKevin Wolf     ret = 0;
6831511b490SKevin Wolf 
6841511b490SKevin Wolf done:
685cb3e7f08SMarc-André Lureau     qobject_unref(qdict);
6861511b490SKevin Wolf     bdrv_unref(bs);
6871511b490SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
6881511b490SKevin Wolf     return ret;
6891511b490SKevin Wolf }
6901511b490SKevin Wolf 
69123d6bd3bSDenis V. Lunev 
69223d6bd3bSDenis V. Lunev static int parallels_probe(const uint8_t *buf, int buf_size,
69323d6bd3bSDenis V. Lunev                            const char *filename)
69423d6bd3bSDenis V. Lunev {
69523d6bd3bSDenis V. Lunev     const ParallelsHeader *ph = (const void *)buf;
69623d6bd3bSDenis V. Lunev 
69723d6bd3bSDenis V. Lunev     if (buf_size < sizeof(ParallelsHeader)) {
69823d6bd3bSDenis V. Lunev         return 0;
69923d6bd3bSDenis V. Lunev     }
70023d6bd3bSDenis V. Lunev 
70123d6bd3bSDenis V. Lunev     if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
70223d6bd3bSDenis V. Lunev            !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
70323d6bd3bSDenis V. Lunev            (le32_to_cpu(ph->version) == HEADER_VERSION)) {
70423d6bd3bSDenis V. Lunev         return 100;
70523d6bd3bSDenis V. Lunev     }
70623d6bd3bSDenis V. Lunev 
70723d6bd3bSDenis V. Lunev     return 0;
70823d6bd3bSDenis V. Lunev }
70923d6bd3bSDenis V. Lunev 
7106dd6b9f1SDenis V. Lunev static int parallels_update_header(BlockDriverState *bs)
7116dd6b9f1SDenis V. Lunev {
7126dd6b9f1SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
7139a4f4c31SKevin Wolf     unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
7149a4f4c31SKevin Wolf                         sizeof(ParallelsHeader));
7156dd6b9f1SDenis V. Lunev 
7166dd6b9f1SDenis V. Lunev     if (size > s->header_size) {
7176dd6b9f1SDenis V. Lunev         size = s->header_size;
7186dd6b9f1SDenis V. Lunev     }
719d9ca2ea2SKevin Wolf     return bdrv_pwrite_sync(bs->file, 0, s->header, size);
7206dd6b9f1SDenis V. Lunev }
7216dd6b9f1SDenis V. Lunev 
72223d6bd3bSDenis V. Lunev static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
72323d6bd3bSDenis V. Lunev                           Error **errp)
72423d6bd3bSDenis V. Lunev {
72523d6bd3bSDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
72623d6bd3bSDenis V. Lunev     ParallelsHeader ph;
72719f5dc15SDenis V. Lunev     int ret, size, i;
728d6179011SDenis V. Lunev     QemuOpts *opts = NULL;
729d6179011SDenis V. Lunev     Error *local_err = NULL;
730d6179011SDenis V. Lunev     char *buf;
73123d6bd3bSDenis V. Lunev 
7324e4bf5c4SKevin Wolf     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
7334e4bf5c4SKevin Wolf                                false, errp);
7344e4bf5c4SKevin Wolf     if (!bs->file) {
7354e4bf5c4SKevin Wolf         return -EINVAL;
7364e4bf5c4SKevin Wolf     }
7374e4bf5c4SKevin Wolf 
738cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
73923d6bd3bSDenis V. Lunev     if (ret < 0) {
74023d6bd3bSDenis V. Lunev         goto fail;
74123d6bd3bSDenis V. Lunev     }
74223d6bd3bSDenis V. Lunev 
74323d6bd3bSDenis V. Lunev     bs->total_sectors = le64_to_cpu(ph.nb_sectors);
74423d6bd3bSDenis V. Lunev 
74523d6bd3bSDenis V. Lunev     if (le32_to_cpu(ph.version) != HEADER_VERSION) {
74623d6bd3bSDenis V. Lunev         goto fail_format;
74723d6bd3bSDenis V. Lunev     }
74823d6bd3bSDenis V. Lunev     if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
74923d6bd3bSDenis V. Lunev         s->off_multiplier = 1;
75023d6bd3bSDenis V. Lunev         bs->total_sectors = 0xffffffff & bs->total_sectors;
75123d6bd3bSDenis V. Lunev     } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
75223d6bd3bSDenis V. Lunev         s->off_multiplier = le32_to_cpu(ph.tracks);
75323d6bd3bSDenis V. Lunev     } else {
75423d6bd3bSDenis V. Lunev         goto fail_format;
75523d6bd3bSDenis V. Lunev     }
75623d6bd3bSDenis V. Lunev 
75723d6bd3bSDenis V. Lunev     s->tracks = le32_to_cpu(ph.tracks);
75823d6bd3bSDenis V. Lunev     if (s->tracks == 0) {
75923d6bd3bSDenis V. Lunev         error_setg(errp, "Invalid image: Zero sectors per track");
76023d6bd3bSDenis V. Lunev         ret = -EINVAL;
76123d6bd3bSDenis V. Lunev         goto fail;
76223d6bd3bSDenis V. Lunev     }
76323d6bd3bSDenis V. Lunev     if (s->tracks > INT32_MAX/513) {
76423d6bd3bSDenis V. Lunev         error_setg(errp, "Invalid image: Too big cluster");
76523d6bd3bSDenis V. Lunev         ret = -EFBIG;
76623d6bd3bSDenis V. Lunev         goto fail;
76723d6bd3bSDenis V. Lunev     }
76823d6bd3bSDenis V. Lunev 
76923d6bd3bSDenis V. Lunev     s->bat_size = le32_to_cpu(ph.bat_entries);
77023d6bd3bSDenis V. Lunev     if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
77123d6bd3bSDenis V. Lunev         error_setg(errp, "Catalog too large");
77223d6bd3bSDenis V. Lunev         ret = -EFBIG;
77323d6bd3bSDenis V. Lunev         goto fail;
77423d6bd3bSDenis V. Lunev     }
77523d6bd3bSDenis V. Lunev 
7762d68e22eSDenis V. Lunev     size = bat_entry_off(s->bat_size);
7779a4f4c31SKevin Wolf     s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
7789a4f4c31SKevin Wolf     s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
77923d6bd3bSDenis V. Lunev     if (s->header == NULL) {
78023d6bd3bSDenis V. Lunev         ret = -ENOMEM;
78123d6bd3bSDenis V. Lunev         goto fail;
78223d6bd3bSDenis V. Lunev     }
78319f5dc15SDenis V. Lunev     s->data_end = le32_to_cpu(ph.data_off);
78419f5dc15SDenis V. Lunev     if (s->data_end == 0) {
78519f5dc15SDenis V. Lunev         s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
78619f5dc15SDenis V. Lunev     }
78719f5dc15SDenis V. Lunev     if (s->data_end < s->header_size) {
78823d6bd3bSDenis V. Lunev         /* there is not enough unused space to fit to block align between BAT
78923d6bd3bSDenis V. Lunev            and actual data. We can't avoid read-modify-write... */
79023d6bd3bSDenis V. Lunev         s->header_size = size;
79123d6bd3bSDenis V. Lunev     }
79223d6bd3bSDenis V. Lunev 
793cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, s->header, s->header_size);
79423d6bd3bSDenis V. Lunev     if (ret < 0) {
79523d6bd3bSDenis V. Lunev         goto fail;
79623d6bd3bSDenis V. Lunev     }
79723d6bd3bSDenis V. Lunev     s->bat_bitmap = (uint32_t *)(s->header + 1);
79823d6bd3bSDenis V. Lunev 
79919f5dc15SDenis V. Lunev     for (i = 0; i < s->bat_size; i++) {
80019f5dc15SDenis V. Lunev         int64_t off = bat2sect(s, i);
80119f5dc15SDenis V. Lunev         if (off >= s->data_end) {
80219f5dc15SDenis V. Lunev             s->data_end = off + s->tracks;
80319f5dc15SDenis V. Lunev         }
80419f5dc15SDenis V. Lunev     }
80519f5dc15SDenis V. Lunev 
8066dd6b9f1SDenis V. Lunev     if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
8076dd6b9f1SDenis V. Lunev         /* Image was not closed correctly. The check is mandatory */
8086dd6b9f1SDenis V. Lunev         s->header_unclean = true;
8096dd6b9f1SDenis V. Lunev         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
8106dd6b9f1SDenis V. Lunev             error_setg(errp, "parallels: Image was not closed correctly; "
8116dd6b9f1SDenis V. Lunev                        "cannot be opened read/write");
8126dd6b9f1SDenis V. Lunev             ret = -EACCES;
8136dd6b9f1SDenis V. Lunev             goto fail;
8146dd6b9f1SDenis V. Lunev         }
8156dd6b9f1SDenis V. Lunev     }
8166dd6b9f1SDenis V. Lunev 
817d6179011SDenis V. Lunev     opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, &local_err);
818d6179011SDenis V. Lunev     if (local_err != NULL) {
819d6179011SDenis V. Lunev         goto fail_options;
820d6179011SDenis V. Lunev     }
821d6179011SDenis V. Lunev 
822d6179011SDenis V. Lunev     qemu_opts_absorb_qdict(opts, options, &local_err);
823d6179011SDenis V. Lunev     if (local_err != NULL) {
824d6179011SDenis V. Lunev         goto fail_options;
825d6179011SDenis V. Lunev     }
826d6179011SDenis V. Lunev 
827d6179011SDenis V. Lunev     s->prealloc_size =
828d6179011SDenis V. Lunev         qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
829d6179011SDenis V. Lunev     s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
830d6179011SDenis V. Lunev     buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
831f7abe0ecSMarc-André Lureau     s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
83206c60b6cSMarkus Armbruster                                        PRL_PREALLOC_MODE_FALLOCATE,
83306c60b6cSMarkus Armbruster                                        &local_err);
834d6179011SDenis V. Lunev     g_free(buf);
835d6179011SDenis V. Lunev     if (local_err != NULL) {
836d6179011SDenis V. Lunev         goto fail_options;
837d6179011SDenis V. Lunev     }
838dc62da88SDenis V. Lunev 
839b647d69aSMax Reitz     if (!bdrv_has_zero_init_truncate(bs->file->bs)) {
840d6179011SDenis V. Lunev         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
841d6179011SDenis V. Lunev     }
842d6179011SDenis V. Lunev 
8436c7d390bSJeff Cody     if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
8446dd6b9f1SDenis V. Lunev         s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
8456dd6b9f1SDenis V. Lunev         ret = parallels_update_header(bs);
8466dd6b9f1SDenis V. Lunev         if (ret < 0) {
8476dd6b9f1SDenis V. Lunev             goto fail;
8486dd6b9f1SDenis V. Lunev         }
8496dd6b9f1SDenis V. Lunev     }
8506dd6b9f1SDenis V. Lunev 
851038adc2fSWei Yang     s->bat_dirty_block = 4 * qemu_real_host_page_size;
8520d31c7c2SDenis V. Lunev     s->bat_dirty_bmap =
8530d31c7c2SDenis V. Lunev         bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
8540d31c7c2SDenis V. Lunev 
8551d0f37cfSJeff Cody     /* Disable migration until bdrv_invalidate_cache method is added */
8561d0f37cfSJeff Cody     error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
8571d0f37cfSJeff Cody                "does not support live migration",
8581d0f37cfSJeff Cody                bdrv_get_device_or_node_name(bs));
8591d0f37cfSJeff Cody     ret = migrate_add_blocker(s->migration_blocker, &local_err);
8601d0f37cfSJeff Cody     if (local_err) {
8611d0f37cfSJeff Cody         error_propagate(errp, local_err);
8621d0f37cfSJeff Cody         error_free(s->migration_blocker);
8631d0f37cfSJeff Cody         goto fail;
8641d0f37cfSJeff Cody     }
86523d6bd3bSDenis V. Lunev     qemu_co_mutex_init(&s->lock);
86623d6bd3bSDenis V. Lunev     return 0;
86723d6bd3bSDenis V. Lunev 
86823d6bd3bSDenis V. Lunev fail_format:
86923d6bd3bSDenis V. Lunev     error_setg(errp, "Image not in Parallels format");
87023d6bd3bSDenis V. Lunev     ret = -EINVAL;
87123d6bd3bSDenis V. Lunev fail:
87223d6bd3bSDenis V. Lunev     qemu_vfree(s->header);
87323d6bd3bSDenis V. Lunev     return ret;
874d6179011SDenis V. Lunev 
875d6179011SDenis V. Lunev fail_options:
876d6179011SDenis V. Lunev     error_propagate(errp, local_err);
877d6179011SDenis V. Lunev     ret = -EINVAL;
878d6179011SDenis V. Lunev     goto fail;
87923d6bd3bSDenis V. Lunev }
88023d6bd3bSDenis V. Lunev 
88123d6bd3bSDenis V. Lunev 
882019d6b8fSAnthony Liguori static void parallels_close(BlockDriverState *bs)
883019d6b8fSAnthony Liguori {
884019d6b8fSAnthony Liguori     BDRVParallelsState *s = bs->opaque;
8856dd6b9f1SDenis V. Lunev 
8866c7d390bSJeff Cody     if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) {
8876dd6b9f1SDenis V. Lunev         s->header->inuse = 0;
8886dd6b9f1SDenis V. Lunev         parallels_update_header(bs);
889e8d04f92SMax Reitz 
890e8d04f92SMax Reitz         /* errors are ignored, so we might as well pass exact=true */
891e8d04f92SMax Reitz         bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
8927ea37c30SMax Reitz                       PREALLOC_MODE_OFF, NULL);
89319f5dc15SDenis V. Lunev     }
89419f5dc15SDenis V. Lunev 
8950d31c7c2SDenis V. Lunev     g_free(s->bat_dirty_bmap);
8969eae9ccaSDenis V. Lunev     qemu_vfree(s->header);
8971d0f37cfSJeff Cody 
8981d0f37cfSJeff Cody     migrate_del_blocker(s->migration_blocker);
8991d0f37cfSJeff Cody     error_free(s->migration_blocker);
900019d6b8fSAnthony Liguori }
901019d6b8fSAnthony Liguori 
902019d6b8fSAnthony Liguori static BlockDriver bdrv_parallels = {
903019d6b8fSAnthony Liguori     .format_name	= "parallels",
904019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVParallelsState),
905019d6b8fSAnthony Liguori     .bdrv_probe		= parallels_probe,
9061dec5a70SChristoph Hellwig     .bdrv_open		= parallels_open,
907019d6b8fSAnthony Liguori     .bdrv_close		= parallels_close,
908862f215fSKevin Wolf     .bdrv_child_perm          = bdrv_format_default_perms,
9098e0cf59dSEric Blake     .bdrv_co_block_status     = parallels_co_block_status,
910d0e61ce5SDenis V. Lunev     .bdrv_has_zero_init       = bdrv_has_zero_init_1,
9110d31c7c2SDenis V. Lunev     .bdrv_co_flush_to_os      = parallels_co_flush_to_os,
912481fb9cfSDenis V. Lunev     .bdrv_co_readv  = parallels_co_readv,
9135a41e1faSDenis V. Lunev     .bdrv_co_writev = parallels_co_writev,
914bcbb3866SEdgar Kaziakhmedov     .supports_backing = true,
9151511b490SKevin Wolf     .bdrv_co_create      = parallels_co_create,
916efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts = parallels_co_create_opts,
9172fd61638SPaolo Bonzini     .bdrv_co_check  = parallels_co_check,
91874cf6c50SDenis V. Lunev     .create_opts    = &parallels_create_opts,
919019d6b8fSAnthony Liguori };
920019d6b8fSAnthony Liguori 
921019d6b8fSAnthony Liguori static void bdrv_parallels_init(void)
922019d6b8fSAnthony Liguori {
923019d6b8fSAnthony Liguori     bdrv_register(&bdrv_parallels);
924019d6b8fSAnthony Liguori }
925019d6b8fSAnthony Liguori 
926019d6b8fSAnthony Liguori block_init(bdrv_parallels_init);
927