xref: /qemu/block/parallels.c (revision d67066d8)
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 {
169bda4cdcbSEric Blake     int ret = 0;
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;
199bda4cdcbSEric Blake         /*
200bda4cdcbSEric Blake          * We require the expanded size to read back as zero. If the
201bda4cdcbSEric Blake          * user permitted truncation, we try that; but if it fails, we
202bda4cdcbSEric Blake          * force the safer-but-slower fallocate.
203bda4cdcbSEric Blake          */
204bda4cdcbSEric Blake         if (s->prealloc_mode == PRL_PREALLOC_MODE_TRUNCATE) {
205bda4cdcbSEric Blake             ret = bdrv_truncate(bs->file,
206bda4cdcbSEric Blake                                 (s->data_end + space) << BDRV_SECTOR_BITS,
207bda4cdcbSEric Blake                                 false, PREALLOC_MODE_OFF, BDRV_REQ_ZERO_WRITE,
208bda4cdcbSEric Blake                                 NULL);
209bda4cdcbSEric Blake             if (ret == -ENOTSUP) {
210bda4cdcbSEric Blake                 s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
211bda4cdcbSEric Blake             }
212bda4cdcbSEric Blake         }
21319f5dc15SDenis V. Lunev         if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
214720ff280SKevin Wolf             ret = bdrv_pwrite_zeroes(bs->file,
21574021bc4SEric Blake                                      s->data_end << BDRV_SECTOR_BITS,
21674021bc4SEric Blake                                      space << BDRV_SECTOR_BITS, 0);
2175a41e1faSDenis V. Lunev         }
2185a41e1faSDenis V. Lunev         if (ret < 0) {
2195a41e1faSDenis V. Lunev             return ret;
2205a41e1faSDenis V. Lunev         }
22119f5dc15SDenis V. Lunev     }
222ddd2ef2cSDenis V. Lunev 
223bcbb3866SEdgar Kaziakhmedov     /* Try to read from backing to fill empty clusters
224bcbb3866SEdgar Kaziakhmedov      * FIXME: 1. previous write_zeroes may be redundant
225bcbb3866SEdgar Kaziakhmedov      *        2. most of data we read from backing will be rewritten by
226bcbb3866SEdgar Kaziakhmedov      *           parallels_co_writev. On aligned-to-cluster write we do not need
227bcbb3866SEdgar Kaziakhmedov      *           this read at all.
228bcbb3866SEdgar Kaziakhmedov      *        3. it would be good to combine write of data from backing and new
229bcbb3866SEdgar Kaziakhmedov      *           data into one write call */
230bcbb3866SEdgar Kaziakhmedov     if (bs->backing) {
231bcbb3866SEdgar Kaziakhmedov         int64_t nb_cow_sectors = to_allocate * s->tracks;
232bcbb3866SEdgar Kaziakhmedov         int64_t nb_cow_bytes = nb_cow_sectors << BDRV_SECTOR_BITS;
233a4072543SVladimir Sementsov-Ogievskiy         void *buf = qemu_blockalign(bs, nb_cow_bytes);
234bcbb3866SEdgar Kaziakhmedov 
235a4072543SVladimir Sementsov-Ogievskiy         ret = bdrv_co_pread(bs->backing, idx * s->tracks * BDRV_SECTOR_SIZE,
236a4072543SVladimir Sementsov-Ogievskiy                             nb_cow_bytes, buf, 0);
237bcbb3866SEdgar Kaziakhmedov         if (ret < 0) {
238a4072543SVladimir Sementsov-Ogievskiy             qemu_vfree(buf);
239bcbb3866SEdgar Kaziakhmedov             return ret;
240bcbb3866SEdgar Kaziakhmedov         }
241bcbb3866SEdgar Kaziakhmedov 
242d08c2a24SEric Blake         ret = bdrv_co_pwritev(bs->file, s->data_end * BDRV_SECTOR_SIZE,
243a4072543SVladimir Sementsov-Ogievskiy                               nb_cow_bytes, buf, 0);
244a4072543SVladimir Sementsov-Ogievskiy         qemu_vfree(buf);
245bcbb3866SEdgar Kaziakhmedov         if (ret < 0) {
246bcbb3866SEdgar Kaziakhmedov             return ret;
247bcbb3866SEdgar Kaziakhmedov         }
248bcbb3866SEdgar Kaziakhmedov     }
249bcbb3866SEdgar Kaziakhmedov 
250ddd2ef2cSDenis V. Lunev     for (i = 0; i < to_allocate; i++) {
251ddd2ef2cSDenis V. Lunev         s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
25219f5dc15SDenis V. Lunev         s->data_end += s->tracks;
253ddd2ef2cSDenis V. Lunev         bitmap_set(s->bat_dirty_bmap,
254c9f6856dSVladimir Sementsov-Ogievskiy                    bat_entry_off(idx + i) / s->bat_dirty_block, 1);
255ddd2ef2cSDenis V. Lunev     }
2565a41e1faSDenis V. Lunev 
257ddd2ef2cSDenis V. Lunev     return bat2sect(s, idx) + sector_num % s->tracks;
2585a41e1faSDenis V. Lunev }
2595a41e1faSDenis V. Lunev 
2600d31c7c2SDenis V. Lunev 
2610d31c7c2SDenis V. Lunev static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
2620d31c7c2SDenis V. Lunev {
2630d31c7c2SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
2640d31c7c2SDenis V. Lunev     unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
2650d31c7c2SDenis V. Lunev     unsigned long bit;
2660d31c7c2SDenis V. Lunev 
2670d31c7c2SDenis V. Lunev     qemu_co_mutex_lock(&s->lock);
2680d31c7c2SDenis V. Lunev 
2690d31c7c2SDenis V. Lunev     bit = find_first_bit(s->bat_dirty_bmap, size);
2700d31c7c2SDenis V. Lunev     while (bit < size) {
2710d31c7c2SDenis V. Lunev         uint32_t off = bit * s->bat_dirty_block;
2720d31c7c2SDenis V. Lunev         uint32_t to_write = s->bat_dirty_block;
2730d31c7c2SDenis V. Lunev         int ret;
2740d31c7c2SDenis V. Lunev 
2750d31c7c2SDenis V. Lunev         if (off + to_write > s->header_size) {
2760d31c7c2SDenis V. Lunev             to_write = s->header_size - off;
2770d31c7c2SDenis V. Lunev         }
278d9ca2ea2SKevin Wolf         ret = bdrv_pwrite(bs->file, off, (uint8_t *)s->header + off,
2799a4f4c31SKevin Wolf                           to_write);
2800d31c7c2SDenis V. Lunev         if (ret < 0) {
2810d31c7c2SDenis V. Lunev             qemu_co_mutex_unlock(&s->lock);
2820d31c7c2SDenis V. Lunev             return ret;
2830d31c7c2SDenis V. Lunev         }
2840d31c7c2SDenis V. Lunev         bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
2850d31c7c2SDenis V. Lunev     }
2860d31c7c2SDenis V. Lunev     bitmap_zero(s->bat_dirty_bmap, size);
2870d31c7c2SDenis V. Lunev 
2880d31c7c2SDenis V. Lunev     qemu_co_mutex_unlock(&s->lock);
2890d31c7c2SDenis V. Lunev     return 0;
2900d31c7c2SDenis V. Lunev }
2910d31c7c2SDenis V. Lunev 
2920d31c7c2SDenis V. Lunev 
2938e0cf59dSEric Blake static int coroutine_fn parallels_co_block_status(BlockDriverState *bs,
2948e0cf59dSEric Blake                                                   bool want_zero,
2958e0cf59dSEric Blake                                                   int64_t offset,
2968e0cf59dSEric Blake                                                   int64_t bytes,
2978e0cf59dSEric Blake                                                   int64_t *pnum,
2988e0cf59dSEric Blake                                                   int64_t *map,
2998e0cf59dSEric Blake                                                   BlockDriverState **file)
300dd3bed16SRoman Kagan {
301dd3bed16SRoman Kagan     BDRVParallelsState *s = bs->opaque;
3028e0cf59dSEric Blake     int count;
303dd3bed16SRoman Kagan 
3048e0cf59dSEric Blake     assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE));
305dd3bed16SRoman Kagan     qemu_co_mutex_lock(&s->lock);
3068e0cf59dSEric Blake     offset = block_status(s, offset >> BDRV_SECTOR_BITS,
3078e0cf59dSEric Blake                           bytes >> BDRV_SECTOR_BITS, &count);
308dd3bed16SRoman Kagan     qemu_co_mutex_unlock(&s->lock);
309dd3bed16SRoman Kagan 
3108e0cf59dSEric Blake     *pnum = count * BDRV_SECTOR_SIZE;
311dd3bed16SRoman Kagan     if (offset < 0) {
312dd3bed16SRoman Kagan         return 0;
313dd3bed16SRoman Kagan     }
314dd3bed16SRoman Kagan 
3158e0cf59dSEric Blake     *map = offset * BDRV_SECTOR_SIZE;
316ddf4987dSFam Zheng     *file = bs->file->bs;
3178e0cf59dSEric Blake     return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
318dd3bed16SRoman Kagan }
319dd3bed16SRoman Kagan 
3205a41e1faSDenis V. Lunev static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
321e18a58b4SEric Blake                                             int64_t sector_num, int nb_sectors,
322e18a58b4SEric Blake                                             QEMUIOVector *qiov, int flags)
3235a41e1faSDenis V. Lunev {
3245a41e1faSDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
3255a41e1faSDenis V. Lunev     uint64_t bytes_done = 0;
3265a41e1faSDenis V. Lunev     QEMUIOVector hd_qiov;
3275a41e1faSDenis V. Lunev     int ret = 0;
3285a41e1faSDenis V. Lunev 
329e18a58b4SEric Blake     assert(!flags);
3305a41e1faSDenis V. Lunev     qemu_iovec_init(&hd_qiov, qiov->niov);
3315a41e1faSDenis V. Lunev 
3325a41e1faSDenis V. Lunev     while (nb_sectors > 0) {
3335a41e1faSDenis V. Lunev         int64_t position;
3345a41e1faSDenis V. Lunev         int n, nbytes;
3355a41e1faSDenis V. Lunev 
3365a41e1faSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
337ddd2ef2cSDenis V. Lunev         position = allocate_clusters(bs, sector_num, nb_sectors, &n);
3385a41e1faSDenis V. Lunev         qemu_co_mutex_unlock(&s->lock);
3395a41e1faSDenis V. Lunev         if (position < 0) {
3405a41e1faSDenis V. Lunev             ret = (int)position;
3415a41e1faSDenis V. Lunev             break;
3425a41e1faSDenis V. Lunev         }
3435a41e1faSDenis V. Lunev 
3445a41e1faSDenis V. Lunev         nbytes = n << BDRV_SECTOR_BITS;
3455a41e1faSDenis V. Lunev 
3465a41e1faSDenis V. Lunev         qemu_iovec_reset(&hd_qiov);
3475a41e1faSDenis V. Lunev         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
3485a41e1faSDenis V. Lunev 
349d08c2a24SEric Blake         ret = bdrv_co_pwritev(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
350d08c2a24SEric Blake                               &hd_qiov, 0);
3515a41e1faSDenis V. Lunev         if (ret < 0) {
3525a41e1faSDenis V. Lunev             break;
3535a41e1faSDenis V. Lunev         }
3545a41e1faSDenis V. Lunev 
3555a41e1faSDenis V. Lunev         nb_sectors -= n;
3565a41e1faSDenis V. Lunev         sector_num += n;
3575a41e1faSDenis V. Lunev         bytes_done += nbytes;
3585a41e1faSDenis V. Lunev     }
3595a41e1faSDenis V. Lunev 
3605a41e1faSDenis V. Lunev     qemu_iovec_destroy(&hd_qiov);
3615a41e1faSDenis V. Lunev     return ret;
3625a41e1faSDenis V. Lunev }
3635a41e1faSDenis V. Lunev 
364481fb9cfSDenis V. Lunev static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
365481fb9cfSDenis V. Lunev         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
366019d6b8fSAnthony Liguori {
36729442569SRoman Kagan     BDRVParallelsState *s = bs->opaque;
368481fb9cfSDenis V. Lunev     uint64_t bytes_done = 0;
369481fb9cfSDenis V. Lunev     QEMUIOVector hd_qiov;
370481fb9cfSDenis V. Lunev     int ret = 0;
371481fb9cfSDenis V. Lunev 
372481fb9cfSDenis V. Lunev     qemu_iovec_init(&hd_qiov, qiov->niov);
37329442569SRoman Kagan 
374019d6b8fSAnthony Liguori     while (nb_sectors > 0) {
375481fb9cfSDenis V. Lunev         int64_t position;
376481fb9cfSDenis V. Lunev         int n, nbytes;
377481fb9cfSDenis V. Lunev 
378481fb9cfSDenis V. Lunev         qemu_co_mutex_lock(&s->lock);
3796953d920SDenis V. Lunev         position = block_status(s, sector_num, nb_sectors, &n);
380481fb9cfSDenis V. Lunev         qemu_co_mutex_unlock(&s->lock);
381481fb9cfSDenis V. Lunev 
382481fb9cfSDenis V. Lunev         nbytes = n << BDRV_SECTOR_BITS;
383481fb9cfSDenis V. Lunev 
384481fb9cfSDenis V. Lunev         qemu_iovec_reset(&hd_qiov);
385481fb9cfSDenis V. Lunev         qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
386481fb9cfSDenis V. Lunev 
387bcbb3866SEdgar Kaziakhmedov         if (position < 0) {
388bcbb3866SEdgar Kaziakhmedov             if (bs->backing) {
389d08c2a24SEric Blake                 ret = bdrv_co_preadv(bs->backing, sector_num * BDRV_SECTOR_SIZE,
390d08c2a24SEric Blake                                      nbytes, &hd_qiov, 0);
391bcbb3866SEdgar Kaziakhmedov                 if (ret < 0) {
392bcbb3866SEdgar Kaziakhmedov                     break;
393bcbb3866SEdgar Kaziakhmedov                 }
394bcbb3866SEdgar Kaziakhmedov             } else {
395bcbb3866SEdgar Kaziakhmedov                 qemu_iovec_memset(&hd_qiov, 0, 0, nbytes);
396bcbb3866SEdgar Kaziakhmedov             }
397bcbb3866SEdgar Kaziakhmedov         } else {
398d08c2a24SEric Blake             ret = bdrv_co_preadv(bs->file, position * BDRV_SECTOR_SIZE, nbytes,
399d08c2a24SEric Blake                                  &hd_qiov, 0);
400481fb9cfSDenis V. Lunev             if (ret < 0) {
401481fb9cfSDenis V. Lunev                 break;
4029d8b88f6SChristoph Hellwig             }
403019d6b8fSAnthony Liguori         }
404019d6b8fSAnthony Liguori 
405481fb9cfSDenis V. Lunev         nb_sectors -= n;
406481fb9cfSDenis V. Lunev         sector_num += n;
407481fb9cfSDenis V. Lunev         bytes_done += nbytes;
408481fb9cfSDenis V. Lunev     }
409481fb9cfSDenis V. Lunev 
410481fb9cfSDenis V. Lunev     qemu_iovec_destroy(&hd_qiov);
4112914caa0SPaolo Bonzini     return ret;
4122914caa0SPaolo Bonzini }
4132914caa0SPaolo Bonzini 
41449ad6467SDenis V. Lunev 
4152fd61638SPaolo Bonzini static int coroutine_fn parallels_co_check(BlockDriverState *bs,
4162fd61638SPaolo Bonzini                                            BdrvCheckResult *res,
41749ad6467SDenis V. Lunev                                            BdrvCheckMode fix)
41849ad6467SDenis V. Lunev {
41949ad6467SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
42049ad6467SDenis V. Lunev     int64_t size, prev_off, high_off;
42149ad6467SDenis V. Lunev     int ret;
42249ad6467SDenis V. Lunev     uint32_t i;
42349ad6467SDenis V. Lunev     bool flush_bat = false;
42449ad6467SDenis V. Lunev     int cluster_size = s->tracks << BDRV_SECTOR_BITS;
42549ad6467SDenis V. Lunev 
4269a4f4c31SKevin Wolf     size = bdrv_getlength(bs->file->bs);
42749ad6467SDenis V. Lunev     if (size < 0) {
42849ad6467SDenis V. Lunev         res->check_errors++;
42949ad6467SDenis V. Lunev         return size;
43049ad6467SDenis V. Lunev     }
43149ad6467SDenis V. Lunev 
4322fd61638SPaolo Bonzini     qemu_co_mutex_lock(&s->lock);
4336dd6b9f1SDenis V. Lunev     if (s->header_unclean) {
4346dd6b9f1SDenis V. Lunev         fprintf(stderr, "%s image was not closed correctly\n",
4356dd6b9f1SDenis V. Lunev                 fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
4366dd6b9f1SDenis V. Lunev         res->corruptions++;
4376dd6b9f1SDenis V. Lunev         if (fix & BDRV_FIX_ERRORS) {
4386dd6b9f1SDenis V. Lunev             /* parallels_close will do the job right */
4396dd6b9f1SDenis V. Lunev             res->corruptions_fixed++;
4406dd6b9f1SDenis V. Lunev             s->header_unclean = false;
4416dd6b9f1SDenis V. Lunev         }
4426dd6b9f1SDenis V. Lunev     }
4436dd6b9f1SDenis V. Lunev 
44449ad6467SDenis V. Lunev     res->bfi.total_clusters = s->bat_size;
44549ad6467SDenis V. Lunev     res->bfi.compressed_clusters = 0; /* compression is not supported */
44649ad6467SDenis V. Lunev 
44749ad6467SDenis V. Lunev     high_off = 0;
44849ad6467SDenis V. Lunev     prev_off = 0;
44949ad6467SDenis V. Lunev     for (i = 0; i < s->bat_size; i++) {
45049ad6467SDenis V. Lunev         int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
45149ad6467SDenis V. Lunev         if (off == 0) {
45249ad6467SDenis V. Lunev             prev_off = 0;
45349ad6467SDenis V. Lunev             continue;
45449ad6467SDenis V. Lunev         }
45549ad6467SDenis V. Lunev 
45649ad6467SDenis V. Lunev         /* cluster outside the image */
45749ad6467SDenis V. Lunev         if (off > size) {
45849ad6467SDenis V. Lunev             fprintf(stderr, "%s cluster %u is outside image\n",
45949ad6467SDenis V. Lunev                     fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
46049ad6467SDenis V. Lunev             res->corruptions++;
46149ad6467SDenis V. Lunev             if (fix & BDRV_FIX_ERRORS) {
46249ad6467SDenis V. Lunev                 prev_off = 0;
46349ad6467SDenis V. Lunev                 s->bat_bitmap[i] = 0;
46449ad6467SDenis V. Lunev                 res->corruptions_fixed++;
46549ad6467SDenis V. Lunev                 flush_bat = true;
46649ad6467SDenis V. Lunev                 continue;
46749ad6467SDenis V. Lunev             }
46849ad6467SDenis V. Lunev         }
46949ad6467SDenis V. Lunev 
47049ad6467SDenis V. Lunev         res->bfi.allocated_clusters++;
47149ad6467SDenis V. Lunev         if (off > high_off) {
47249ad6467SDenis V. Lunev             high_off = off;
47349ad6467SDenis V. Lunev         }
47449ad6467SDenis V. Lunev 
47549ad6467SDenis V. Lunev         if (prev_off != 0 && (prev_off + cluster_size) != off) {
47649ad6467SDenis V. Lunev             res->bfi.fragmented_clusters++;
47749ad6467SDenis V. Lunev         }
47849ad6467SDenis V. Lunev         prev_off = off;
47949ad6467SDenis V. Lunev     }
48049ad6467SDenis V. Lunev 
4812fd61638SPaolo Bonzini     ret = 0;
48249ad6467SDenis V. Lunev     if (flush_bat) {
483d9ca2ea2SKevin Wolf         ret = bdrv_pwrite_sync(bs->file, 0, s->header, s->header_size);
48449ad6467SDenis V. Lunev         if (ret < 0) {
48549ad6467SDenis V. Lunev             res->check_errors++;
4862fd61638SPaolo Bonzini             goto out;
48749ad6467SDenis V. Lunev         }
48849ad6467SDenis V. Lunev     }
48949ad6467SDenis V. Lunev 
49049ad6467SDenis V. Lunev     res->image_end_offset = high_off + cluster_size;
49149ad6467SDenis V. Lunev     if (size > res->image_end_offset) {
49249ad6467SDenis V. Lunev         int64_t count;
49349ad6467SDenis V. Lunev         count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
49449ad6467SDenis V. Lunev         fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
49549ad6467SDenis V. Lunev                 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
49649ad6467SDenis V. Lunev                 size - res->image_end_offset);
49749ad6467SDenis V. Lunev         res->leaks += count;
49849ad6467SDenis V. Lunev         if (fix & BDRV_FIX_LEAKS) {
499ed3d2ec9SMax Reitz             Error *local_err = NULL;
500e8d04f92SMax Reitz 
501e8d04f92SMax Reitz             /*
502e8d04f92SMax Reitz              * In order to really repair the image, we must shrink it.
503e8d04f92SMax Reitz              * That means we have to pass exact=true.
504e8d04f92SMax Reitz              */
505e8d04f92SMax Reitz             ret = bdrv_truncate(bs->file, res->image_end_offset, true,
5067b8e4857SKevin Wolf                                 PREALLOC_MODE_OFF, 0, &local_err);
50749ad6467SDenis V. Lunev             if (ret < 0) {
508ed3d2ec9SMax Reitz                 error_report_err(local_err);
50949ad6467SDenis V. Lunev                 res->check_errors++;
5102fd61638SPaolo Bonzini                 goto out;
51149ad6467SDenis V. Lunev             }
51249ad6467SDenis V. Lunev             res->leaks_fixed += count;
51349ad6467SDenis V. Lunev         }
51449ad6467SDenis V. Lunev     }
51549ad6467SDenis V. Lunev 
5162fd61638SPaolo Bonzini out:
5172fd61638SPaolo Bonzini     qemu_co_mutex_unlock(&s->lock);
5182fd61638SPaolo Bonzini     return ret;
51949ad6467SDenis V. Lunev }
52049ad6467SDenis V. Lunev 
52149ad6467SDenis V. Lunev 
5221511b490SKevin Wolf static int coroutine_fn parallels_co_create(BlockdevCreateOptions* opts,
523efc75e2aSStefan Hajnoczi                                             Error **errp)
52474cf6c50SDenis V. Lunev {
5251511b490SKevin Wolf     BlockdevCreateOptionsParallels *parallels_opts;
5261511b490SKevin Wolf     BlockDriverState *bs;
5271511b490SKevin Wolf     BlockBackend *blk;
52874cf6c50SDenis V. Lunev     int64_t total_size, cl_size;
529369f7de9SDenis V. Lunev     uint32_t bat_entries, bat_sectors;
53074cf6c50SDenis V. Lunev     ParallelsHeader header;
5311511b490SKevin Wolf     uint8_t tmp[BDRV_SECTOR_SIZE];
53274cf6c50SDenis V. Lunev     int ret;
53374cf6c50SDenis V. Lunev 
5341511b490SKevin Wolf     assert(opts->driver == BLOCKDEV_DRIVER_PARALLELS);
5351511b490SKevin Wolf     parallels_opts = &opts->u.parallels;
5361511b490SKevin Wolf 
5371511b490SKevin Wolf     /* Sanity checks */
5381511b490SKevin Wolf     total_size = parallels_opts->size;
5391511b490SKevin Wolf 
5401511b490SKevin Wolf     if (parallels_opts->has_cluster_size) {
5411511b490SKevin Wolf         cl_size = parallels_opts->cluster_size;
5421511b490SKevin Wolf     } else {
5431511b490SKevin Wolf         cl_size = DEFAULT_CLUSTER_SIZE;
5441511b490SKevin Wolf     }
5451511b490SKevin Wolf 
5462332d825SKevin Wolf     /* XXX What is the real limit here? This is an insanely large maximum. */
5472332d825SKevin Wolf     if (cl_size >= INT64_MAX / MAX_PARALLELS_IMAGE_FACTOR) {
5482332d825SKevin Wolf         error_setg(errp, "Cluster size is too large");
5492332d825SKevin Wolf         return -EINVAL;
5502332d825SKevin Wolf     }
551555a608cSKlim Kireev     if (total_size >= MAX_PARALLELS_IMAGE_FACTOR * cl_size) {
5521511b490SKevin Wolf         error_setg(errp, "Image size is too large for this cluster size");
553555a608cSKlim Kireev         return -E2BIG;
554555a608cSKlim Kireev     }
55574cf6c50SDenis V. Lunev 
5561511b490SKevin Wolf     if (!QEMU_IS_ALIGNED(total_size, BDRV_SECTOR_SIZE)) {
5571511b490SKevin Wolf         error_setg(errp, "Image size must be a multiple of 512 bytes");
5581511b490SKevin Wolf         return -EINVAL;
55974cf6c50SDenis V. Lunev     }
56074cf6c50SDenis V. Lunev 
5611511b490SKevin Wolf     if (!QEMU_IS_ALIGNED(cl_size, BDRV_SECTOR_SIZE)) {
5621511b490SKevin Wolf         error_setg(errp, "Cluster size must be a multiple of 512 bytes");
5631511b490SKevin Wolf         return -EINVAL;
5641511b490SKevin Wolf     }
5651511b490SKevin Wolf 
5661511b490SKevin Wolf     /* Create BlockBackend to write to the image */
5671511b490SKevin Wolf     bs = bdrv_open_blockdev_ref(parallels_opts->file, errp);
5681511b490SKevin Wolf     if (bs == NULL) {
5698942764fSKevin Wolf         return -EIO;
57074cf6c50SDenis V. Lunev     }
5718942764fSKevin Wolf 
572a3aeeab5SEric Blake     blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
573a3aeeab5SEric Blake                           errp);
574a3aeeab5SEric Blake     if (!blk) {
575a3aeeab5SEric Blake         ret = -EPERM;
5761511b490SKevin Wolf         goto out;
5771511b490SKevin Wolf     }
5781511b490SKevin Wolf     blk_set_allow_write_beyond_eof(blk, true);
5791511b490SKevin Wolf 
5801511b490SKevin Wolf     /* Create image format */
581369f7de9SDenis V. Lunev     bat_entries = DIV_ROUND_UP(total_size, cl_size);
5822d68e22eSDenis V. Lunev     bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
583369f7de9SDenis V. Lunev     bat_sectors = (bat_sectors *  cl_size) >> BDRV_SECTOR_BITS;
58474cf6c50SDenis V. Lunev 
58574cf6c50SDenis V. Lunev     memset(&header, 0, sizeof(header));
58674cf6c50SDenis V. Lunev     memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
58774cf6c50SDenis V. Lunev     header.version = cpu_to_le32(HEADER_VERSION);
58874cf6c50SDenis V. Lunev     /* don't care much about geometry, it is not used on image level */
589908b1c84SKlim Kireev     header.heads = cpu_to_le32(HEADS_NUMBER);
590908b1c84SKlim Kireev     header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE
591908b1c84SKlim Kireev                                    / HEADS_NUMBER / SEC_IN_CYL);
59274cf6c50SDenis V. Lunev     header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
593369f7de9SDenis V. Lunev     header.bat_entries = cpu_to_le32(bat_entries);
59474cf6c50SDenis V. Lunev     header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
595369f7de9SDenis V. Lunev     header.data_off = cpu_to_le32(bat_sectors);
59674cf6c50SDenis V. Lunev 
59774cf6c50SDenis V. Lunev     /* write all the data */
59874cf6c50SDenis V. Lunev     memset(tmp, 0, sizeof(tmp));
59974cf6c50SDenis V. Lunev     memcpy(tmp, &header, sizeof(header));
60074cf6c50SDenis V. Lunev 
6011511b490SKevin Wolf     ret = blk_pwrite(blk, 0, tmp, BDRV_SECTOR_SIZE, 0);
60274cf6c50SDenis V. Lunev     if (ret < 0) {
60374cf6c50SDenis V. Lunev         goto exit;
60474cf6c50SDenis V. Lunev     }
6051511b490SKevin Wolf     ret = blk_pwrite_zeroes(blk, BDRV_SECTOR_SIZE,
606983a1600SEric Blake                             (bat_sectors - 1) << BDRV_SECTOR_BITS, 0);
60774cf6c50SDenis V. Lunev     if (ret < 0) {
60874cf6c50SDenis V. Lunev         goto exit;
60974cf6c50SDenis V. Lunev     }
61074cf6c50SDenis V. Lunev 
6111511b490SKevin Wolf     ret = 0;
6121511b490SKevin Wolf out:
6131511b490SKevin Wolf     blk_unref(blk);
6141511b490SKevin Wolf     bdrv_unref(bs);
61574cf6c50SDenis V. Lunev     return ret;
61674cf6c50SDenis V. Lunev 
61774cf6c50SDenis V. Lunev exit:
61874cf6c50SDenis V. Lunev     error_setg_errno(errp, -ret, "Failed to create Parallels image");
6191511b490SKevin Wolf     goto out;
6201511b490SKevin Wolf }
6211511b490SKevin Wolf 
622b92902dfSMaxim Levitsky static int coroutine_fn parallels_co_create_opts(BlockDriver *drv,
623b92902dfSMaxim Levitsky                                                  const char *filename,
6241511b490SKevin Wolf                                                  QemuOpts *opts,
6251511b490SKevin Wolf                                                  Error **errp)
6261511b490SKevin Wolf {
6271511b490SKevin Wolf     BlockdevCreateOptions *create_options = NULL;
6281511b490SKevin Wolf     Error *local_err = NULL;
6291511b490SKevin Wolf     BlockDriverState *bs = NULL;
63092adf9dbSMarkus Armbruster     QDict *qdict;
6311511b490SKevin Wolf     Visitor *v;
6321511b490SKevin Wolf     int ret;
6331511b490SKevin Wolf 
6341511b490SKevin Wolf     static const QDictRenames opt_renames[] = {
6351511b490SKevin Wolf         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
6361511b490SKevin Wolf         { NULL, NULL },
6371511b490SKevin Wolf     };
6381511b490SKevin Wolf 
6391511b490SKevin Wolf     /* Parse options and convert legacy syntax */
6401511b490SKevin Wolf     qdict = qemu_opts_to_qdict_filtered(opts, NULL, &parallels_create_opts,
6411511b490SKevin Wolf                                         true);
6421511b490SKevin Wolf 
6431511b490SKevin Wolf     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
6441511b490SKevin Wolf         ret = -EINVAL;
64574cf6c50SDenis V. Lunev         goto done;
64674cf6c50SDenis V. Lunev     }
64774cf6c50SDenis V. Lunev 
6481511b490SKevin Wolf     /* Create and open the file (protocol layer) */
6491511b490SKevin Wolf     ret = bdrv_create_file(filename, opts, &local_err);
6501511b490SKevin Wolf     if (ret < 0) {
6511511b490SKevin Wolf         error_propagate(errp, local_err);
6521511b490SKevin Wolf         goto done;
6531511b490SKevin Wolf     }
6541511b490SKevin Wolf 
6551511b490SKevin Wolf     bs = bdrv_open(filename, NULL, NULL,
6561511b490SKevin Wolf                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
6571511b490SKevin Wolf     if (bs == NULL) {
6581511b490SKevin Wolf         ret = -EIO;
6591511b490SKevin Wolf         goto done;
6601511b490SKevin Wolf     }
6611511b490SKevin Wolf 
6621511b490SKevin Wolf     /* Now get the QAPI type BlockdevCreateOptions */
6631511b490SKevin Wolf     qdict_put_str(qdict, "driver", "parallels");
6641511b490SKevin Wolf     qdict_put_str(qdict, "file", bs->node_name);
6651511b490SKevin Wolf 
666af91062eSMarkus Armbruster     v = qobject_input_visitor_new_flat_confused(qdict, errp);
667af91062eSMarkus Armbruster     if (!v) {
6681511b490SKevin Wolf         ret = -EINVAL;
6691511b490SKevin Wolf         goto done;
6701511b490SKevin Wolf     }
6711511b490SKevin Wolf 
6721511b490SKevin Wolf     visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
6731511b490SKevin Wolf     visit_free(v);
6741511b490SKevin Wolf 
6751511b490SKevin Wolf     if (local_err) {
6761511b490SKevin Wolf         error_propagate(errp, local_err);
6771511b490SKevin Wolf         ret = -EINVAL;
6781511b490SKevin Wolf         goto done;
6791511b490SKevin Wolf     }
6801511b490SKevin Wolf 
6811511b490SKevin Wolf     /* Silently round up sizes */
6821511b490SKevin Wolf     create_options->u.parallels.size =
6831511b490SKevin Wolf         ROUND_UP(create_options->u.parallels.size, BDRV_SECTOR_SIZE);
6841511b490SKevin Wolf     create_options->u.parallels.cluster_size =
6851511b490SKevin Wolf         ROUND_UP(create_options->u.parallels.cluster_size, BDRV_SECTOR_SIZE);
6861511b490SKevin Wolf 
6871511b490SKevin Wolf     /* Create the Parallels image (format layer) */
6881511b490SKevin Wolf     ret = parallels_co_create(create_options, errp);
6891511b490SKevin Wolf     if (ret < 0) {
6901511b490SKevin Wolf         goto done;
6911511b490SKevin Wolf     }
6921511b490SKevin Wolf     ret = 0;
6931511b490SKevin Wolf 
6941511b490SKevin Wolf done:
695cb3e7f08SMarc-André Lureau     qobject_unref(qdict);
6961511b490SKevin Wolf     bdrv_unref(bs);
6971511b490SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
6981511b490SKevin Wolf     return ret;
6991511b490SKevin Wolf }
7001511b490SKevin Wolf 
70123d6bd3bSDenis V. Lunev 
70223d6bd3bSDenis V. Lunev static int parallels_probe(const uint8_t *buf, int buf_size,
70323d6bd3bSDenis V. Lunev                            const char *filename)
70423d6bd3bSDenis V. Lunev {
70523d6bd3bSDenis V. Lunev     const ParallelsHeader *ph = (const void *)buf;
70623d6bd3bSDenis V. Lunev 
70723d6bd3bSDenis V. Lunev     if (buf_size < sizeof(ParallelsHeader)) {
70823d6bd3bSDenis V. Lunev         return 0;
70923d6bd3bSDenis V. Lunev     }
71023d6bd3bSDenis V. Lunev 
71123d6bd3bSDenis V. Lunev     if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
71223d6bd3bSDenis V. Lunev            !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
71323d6bd3bSDenis V. Lunev            (le32_to_cpu(ph->version) == HEADER_VERSION)) {
71423d6bd3bSDenis V. Lunev         return 100;
71523d6bd3bSDenis V. Lunev     }
71623d6bd3bSDenis V. Lunev 
71723d6bd3bSDenis V. Lunev     return 0;
71823d6bd3bSDenis V. Lunev }
71923d6bd3bSDenis V. Lunev 
7206dd6b9f1SDenis V. Lunev static int parallels_update_header(BlockDriverState *bs)
7216dd6b9f1SDenis V. Lunev {
7226dd6b9f1SDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
7239a4f4c31SKevin Wolf     unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
7249a4f4c31SKevin Wolf                         sizeof(ParallelsHeader));
7256dd6b9f1SDenis V. Lunev 
7266dd6b9f1SDenis V. Lunev     if (size > s->header_size) {
7276dd6b9f1SDenis V. Lunev         size = s->header_size;
7286dd6b9f1SDenis V. Lunev     }
729d9ca2ea2SKevin Wolf     return bdrv_pwrite_sync(bs->file, 0, s->header, size);
7306dd6b9f1SDenis V. Lunev }
7316dd6b9f1SDenis V. Lunev 
73223d6bd3bSDenis V. Lunev static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
73323d6bd3bSDenis V. Lunev                           Error **errp)
73423d6bd3bSDenis V. Lunev {
73523d6bd3bSDenis V. Lunev     BDRVParallelsState *s = bs->opaque;
73623d6bd3bSDenis V. Lunev     ParallelsHeader ph;
73719f5dc15SDenis V. Lunev     int ret, size, i;
738d6179011SDenis V. Lunev     QemuOpts *opts = NULL;
739d6179011SDenis V. Lunev     Error *local_err = NULL;
740d6179011SDenis V. Lunev     char *buf;
74123d6bd3bSDenis V. Lunev 
7424e4bf5c4SKevin Wolf     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
7434e4bf5c4SKevin Wolf                                false, errp);
7444e4bf5c4SKevin Wolf     if (!bs->file) {
7454e4bf5c4SKevin Wolf         return -EINVAL;
7464e4bf5c4SKevin Wolf     }
7474e4bf5c4SKevin Wolf 
748cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
74923d6bd3bSDenis V. Lunev     if (ret < 0) {
75023d6bd3bSDenis V. Lunev         goto fail;
75123d6bd3bSDenis V. Lunev     }
75223d6bd3bSDenis V. Lunev 
75323d6bd3bSDenis V. Lunev     bs->total_sectors = le64_to_cpu(ph.nb_sectors);
75423d6bd3bSDenis V. Lunev 
75523d6bd3bSDenis V. Lunev     if (le32_to_cpu(ph.version) != HEADER_VERSION) {
75623d6bd3bSDenis V. Lunev         goto fail_format;
75723d6bd3bSDenis V. Lunev     }
75823d6bd3bSDenis V. Lunev     if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
75923d6bd3bSDenis V. Lunev         s->off_multiplier = 1;
76023d6bd3bSDenis V. Lunev         bs->total_sectors = 0xffffffff & bs->total_sectors;
76123d6bd3bSDenis V. Lunev     } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
76223d6bd3bSDenis V. Lunev         s->off_multiplier = le32_to_cpu(ph.tracks);
76323d6bd3bSDenis V. Lunev     } else {
76423d6bd3bSDenis V. Lunev         goto fail_format;
76523d6bd3bSDenis V. Lunev     }
76623d6bd3bSDenis V. Lunev 
76723d6bd3bSDenis V. Lunev     s->tracks = le32_to_cpu(ph.tracks);
76823d6bd3bSDenis V. Lunev     if (s->tracks == 0) {
76923d6bd3bSDenis V. Lunev         error_setg(errp, "Invalid image: Zero sectors per track");
77023d6bd3bSDenis V. Lunev         ret = -EINVAL;
77123d6bd3bSDenis V. Lunev         goto fail;
77223d6bd3bSDenis V. Lunev     }
77323d6bd3bSDenis V. Lunev     if (s->tracks > INT32_MAX/513) {
77423d6bd3bSDenis V. Lunev         error_setg(errp, "Invalid image: Too big cluster");
77523d6bd3bSDenis V. Lunev         ret = -EFBIG;
77623d6bd3bSDenis V. Lunev         goto fail;
77723d6bd3bSDenis V. Lunev     }
77823d6bd3bSDenis V. Lunev 
77923d6bd3bSDenis V. Lunev     s->bat_size = le32_to_cpu(ph.bat_entries);
78023d6bd3bSDenis V. Lunev     if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
78123d6bd3bSDenis V. Lunev         error_setg(errp, "Catalog too large");
78223d6bd3bSDenis V. Lunev         ret = -EFBIG;
78323d6bd3bSDenis V. Lunev         goto fail;
78423d6bd3bSDenis V. Lunev     }
78523d6bd3bSDenis V. Lunev 
7862d68e22eSDenis V. Lunev     size = bat_entry_off(s->bat_size);
7879a4f4c31SKevin Wolf     s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs));
7889a4f4c31SKevin Wolf     s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
78923d6bd3bSDenis V. Lunev     if (s->header == NULL) {
79023d6bd3bSDenis V. Lunev         ret = -ENOMEM;
79123d6bd3bSDenis V. Lunev         goto fail;
79223d6bd3bSDenis V. Lunev     }
79319f5dc15SDenis V. Lunev     s->data_end = le32_to_cpu(ph.data_off);
79419f5dc15SDenis V. Lunev     if (s->data_end == 0) {
79519f5dc15SDenis V. Lunev         s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
79619f5dc15SDenis V. Lunev     }
79719f5dc15SDenis V. Lunev     if (s->data_end < s->header_size) {
79823d6bd3bSDenis V. Lunev         /* there is not enough unused space to fit to block align between BAT
79923d6bd3bSDenis V. Lunev            and actual data. We can't avoid read-modify-write... */
80023d6bd3bSDenis V. Lunev         s->header_size = size;
80123d6bd3bSDenis V. Lunev     }
80223d6bd3bSDenis V. Lunev 
803cf2ab8fcSKevin Wolf     ret = bdrv_pread(bs->file, 0, s->header, s->header_size);
80423d6bd3bSDenis V. Lunev     if (ret < 0) {
80523d6bd3bSDenis V. Lunev         goto fail;
80623d6bd3bSDenis V. Lunev     }
80723d6bd3bSDenis V. Lunev     s->bat_bitmap = (uint32_t *)(s->header + 1);
80823d6bd3bSDenis V. Lunev 
80919f5dc15SDenis V. Lunev     for (i = 0; i < s->bat_size; i++) {
81019f5dc15SDenis V. Lunev         int64_t off = bat2sect(s, i);
81119f5dc15SDenis V. Lunev         if (off >= s->data_end) {
81219f5dc15SDenis V. Lunev             s->data_end = off + s->tracks;
81319f5dc15SDenis V. Lunev         }
81419f5dc15SDenis V. Lunev     }
81519f5dc15SDenis V. Lunev 
8166dd6b9f1SDenis V. Lunev     if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
8176dd6b9f1SDenis V. Lunev         /* Image was not closed correctly. The check is mandatory */
8186dd6b9f1SDenis V. Lunev         s->header_unclean = true;
8196dd6b9f1SDenis V. Lunev         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
8206dd6b9f1SDenis V. Lunev             error_setg(errp, "parallels: Image was not closed correctly; "
8216dd6b9f1SDenis V. Lunev                        "cannot be opened read/write");
8226dd6b9f1SDenis V. Lunev             ret = -EACCES;
8236dd6b9f1SDenis V. Lunev             goto fail;
8246dd6b9f1SDenis V. Lunev         }
8256dd6b9f1SDenis V. Lunev     }
8266dd6b9f1SDenis V. Lunev 
827d6179011SDenis V. Lunev     opts = qemu_opts_create(&parallels_runtime_opts, NULL, 0, &local_err);
828d6179011SDenis V. Lunev     if (local_err != NULL) {
829d6179011SDenis V. Lunev         goto fail_options;
830d6179011SDenis V. Lunev     }
831d6179011SDenis V. Lunev 
832d6179011SDenis V. Lunev     qemu_opts_absorb_qdict(opts, options, &local_err);
833d6179011SDenis V. Lunev     if (local_err != NULL) {
834d6179011SDenis V. Lunev         goto fail_options;
835d6179011SDenis V. Lunev     }
836d6179011SDenis V. Lunev 
837d6179011SDenis V. Lunev     s->prealloc_size =
838d6179011SDenis V. Lunev         qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
839d6179011SDenis V. Lunev     s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
840d6179011SDenis V. Lunev     buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
841bda4cdcbSEric Blake     /* prealloc_mode can be downgraded later during allocate_clusters */
842f7abe0ecSMarc-André Lureau     s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
84306c60b6cSMarkus Armbruster                                        PRL_PREALLOC_MODE_FALLOCATE,
84406c60b6cSMarkus Armbruster                                        &local_err);
845d6179011SDenis V. Lunev     g_free(buf);
846d6179011SDenis V. Lunev     if (local_err != NULL) {
847d6179011SDenis V. Lunev         goto fail_options;
848d6179011SDenis V. Lunev     }
849dc62da88SDenis V. Lunev 
8506c7d390bSJeff Cody     if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_INACTIVE)) {
8516dd6b9f1SDenis V. Lunev         s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
8526dd6b9f1SDenis V. Lunev         ret = parallels_update_header(bs);
8536dd6b9f1SDenis V. Lunev         if (ret < 0) {
8546dd6b9f1SDenis V. Lunev             goto fail;
8556dd6b9f1SDenis V. Lunev         }
8566dd6b9f1SDenis V. Lunev     }
8576dd6b9f1SDenis V. Lunev 
858038adc2fSWei Yang     s->bat_dirty_block = 4 * qemu_real_host_page_size;
8590d31c7c2SDenis V. Lunev     s->bat_dirty_bmap =
8600d31c7c2SDenis V. Lunev         bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
8610d31c7c2SDenis V. Lunev 
8621d0f37cfSJeff Cody     /* Disable migration until bdrv_invalidate_cache method is added */
8631d0f37cfSJeff Cody     error_setg(&s->migration_blocker, "The Parallels format used by node '%s' "
8641d0f37cfSJeff Cody                "does not support live migration",
8651d0f37cfSJeff Cody                bdrv_get_device_or_node_name(bs));
8661d0f37cfSJeff Cody     ret = migrate_add_blocker(s->migration_blocker, &local_err);
8671d0f37cfSJeff Cody     if (local_err) {
8681d0f37cfSJeff Cody         error_propagate(errp, local_err);
8691d0f37cfSJeff Cody         error_free(s->migration_blocker);
8701d0f37cfSJeff Cody         goto fail;
8711d0f37cfSJeff Cody     }
87223d6bd3bSDenis V. Lunev     qemu_co_mutex_init(&s->lock);
87323d6bd3bSDenis V. Lunev     return 0;
87423d6bd3bSDenis V. Lunev 
87523d6bd3bSDenis V. Lunev fail_format:
87623d6bd3bSDenis V. Lunev     error_setg(errp, "Image not in Parallels format");
87723d6bd3bSDenis V. Lunev     ret = -EINVAL;
87823d6bd3bSDenis V. Lunev fail:
87923d6bd3bSDenis V. Lunev     qemu_vfree(s->header);
88023d6bd3bSDenis V. Lunev     return ret;
881d6179011SDenis V. Lunev 
882d6179011SDenis V. Lunev fail_options:
883d6179011SDenis V. Lunev     error_propagate(errp, local_err);
884d6179011SDenis V. Lunev     ret = -EINVAL;
885d6179011SDenis V. Lunev     goto fail;
88623d6bd3bSDenis V. Lunev }
88723d6bd3bSDenis V. Lunev 
88823d6bd3bSDenis V. Lunev 
889019d6b8fSAnthony Liguori static void parallels_close(BlockDriverState *bs)
890019d6b8fSAnthony Liguori {
891019d6b8fSAnthony Liguori     BDRVParallelsState *s = bs->opaque;
8926dd6b9f1SDenis V. Lunev 
8936c7d390bSJeff Cody     if ((bs->open_flags & BDRV_O_RDWR) && !(bs->open_flags & BDRV_O_INACTIVE)) {
8946dd6b9f1SDenis V. Lunev         s->header->inuse = 0;
8956dd6b9f1SDenis V. Lunev         parallels_update_header(bs);
896e8d04f92SMax Reitz 
897e8d04f92SMax Reitz         /* errors are ignored, so we might as well pass exact=true */
898e8d04f92SMax Reitz         bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, true,
8997b8e4857SKevin Wolf                       PREALLOC_MODE_OFF, 0, NULL);
90019f5dc15SDenis V. Lunev     }
90119f5dc15SDenis V. Lunev 
9020d31c7c2SDenis V. Lunev     g_free(s->bat_dirty_bmap);
9039eae9ccaSDenis V. Lunev     qemu_vfree(s->header);
9041d0f37cfSJeff Cody 
9051d0f37cfSJeff Cody     migrate_del_blocker(s->migration_blocker);
9061d0f37cfSJeff Cody     error_free(s->migration_blocker);
907019d6b8fSAnthony Liguori }
908019d6b8fSAnthony Liguori 
909019d6b8fSAnthony Liguori static BlockDriver bdrv_parallels = {
910019d6b8fSAnthony Liguori     .format_name	= "parallels",
911019d6b8fSAnthony Liguori     .instance_size	= sizeof(BDRVParallelsState),
912019d6b8fSAnthony Liguori     .bdrv_probe		= parallels_probe,
9131dec5a70SChristoph Hellwig     .bdrv_open		= parallels_open,
914019d6b8fSAnthony Liguori     .bdrv_close		= parallels_close,
915862f215fSKevin Wolf     .bdrv_child_perm          = bdrv_format_default_perms,
9168e0cf59dSEric Blake     .bdrv_co_block_status     = parallels_co_block_status,
917d0e61ce5SDenis V. Lunev     .bdrv_has_zero_init       = bdrv_has_zero_init_1,
9180d31c7c2SDenis V. Lunev     .bdrv_co_flush_to_os      = parallels_co_flush_to_os,
919481fb9cfSDenis V. Lunev     .bdrv_co_readv  = parallels_co_readv,
9205a41e1faSDenis V. Lunev     .bdrv_co_writev = parallels_co_writev,
921*d67066d8SMax Reitz     .is_format      = true,
922bcbb3866SEdgar Kaziakhmedov     .supports_backing = true,
9231511b490SKevin Wolf     .bdrv_co_create      = parallels_co_create,
924efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts = parallels_co_create_opts,
9252fd61638SPaolo Bonzini     .bdrv_co_check  = parallels_co_check,
92674cf6c50SDenis V. Lunev     .create_opts    = &parallels_create_opts,
927019d6b8fSAnthony Liguori };
928019d6b8fSAnthony Liguori 
929019d6b8fSAnthony Liguori static void bdrv_parallels_init(void)
930019d6b8fSAnthony Liguori {
931019d6b8fSAnthony Liguori     bdrv_register(&bdrv_parallels);
932019d6b8fSAnthony Liguori }
933019d6b8fSAnthony Liguori 
934019d6b8fSAnthony Liguori block_init(bdrv_parallels_init);
935