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 */ 3080c71a24SPeter Maydell #include "qemu/osdep.h" 31019d6b8fSAnthony Liguori #include "qemu-common.h" 32737e150eSPaolo Bonzini #include "block/block_int.h" 33*8942764fSKevin Wolf #include "sysemu/block-backend.h" 341de7afc9SPaolo Bonzini #include "qemu/module.h" 350d31c7c2SDenis V. Lunev #include "qemu/bitmap.h" 36d6179011SDenis V. Lunev #include "qapi/util.h" 37019d6b8fSAnthony Liguori 38019d6b8fSAnthony Liguori /**************************************************************/ 39019d6b8fSAnthony Liguori 40019d6b8fSAnthony Liguori #define HEADER_MAGIC "WithoutFreeSpace" 41d25d5980SDenis V. Lunev #define HEADER_MAGIC2 "WithouFreSpacExt" 42019d6b8fSAnthony Liguori #define HEADER_VERSION 2 436dd6b9f1SDenis V. Lunev #define HEADER_INUSE_MAGIC (0x746F6E59) 44019d6b8fSAnthony Liguori 4574cf6c50SDenis V. Lunev #define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */ 4674cf6c50SDenis V. Lunev 4774cf6c50SDenis V. Lunev 48019d6b8fSAnthony Liguori // always little-endian 4907898904SDenis V. Lunev typedef struct ParallelsHeader { 50019d6b8fSAnthony Liguori char magic[16]; // "WithoutFreeSpace" 51019d6b8fSAnthony Liguori uint32_t version; 52019d6b8fSAnthony Liguori uint32_t heads; 53019d6b8fSAnthony Liguori uint32_t cylinders; 54019d6b8fSAnthony Liguori uint32_t tracks; 55369f7de9SDenis V. Lunev uint32_t bat_entries; 568c27d54fSDenis V. Lunev uint64_t nb_sectors; 578c27d54fSDenis V. Lunev uint32_t inuse; 588c27d54fSDenis V. Lunev uint32_t data_off; 598c27d54fSDenis V. Lunev char padding[12]; 6007898904SDenis V. Lunev } QEMU_PACKED ParallelsHeader; 61019d6b8fSAnthony Liguori 62d6179011SDenis V. Lunev 63d6179011SDenis V. Lunev typedef enum ParallelsPreallocMode { 64d6179011SDenis V. Lunev PRL_PREALLOC_MODE_FALLOCATE = 0, 65d6179011SDenis V. Lunev PRL_PREALLOC_MODE_TRUNCATE = 1, 667fb1cf16SEric Blake PRL_PREALLOC_MODE__MAX = 2, 67d6179011SDenis V. Lunev } ParallelsPreallocMode; 68d6179011SDenis V. Lunev 69d6179011SDenis V. Lunev static const char *prealloc_mode_lookup[] = { 70d6179011SDenis V. Lunev "falloc", 71d6179011SDenis V. Lunev "truncate", 72d6179011SDenis V. Lunev NULL, 73d6179011SDenis V. Lunev }; 74d6179011SDenis V. Lunev 75d6179011SDenis V. Lunev 76019d6b8fSAnthony Liguori typedef struct BDRVParallelsState { 77481fb9cfSDenis V. Lunev /** Locking is conservative, the lock protects 78481fb9cfSDenis V. Lunev * - image file extending (truncate, fallocate) 79481fb9cfSDenis V. Lunev * - any access to block allocation table 80481fb9cfSDenis V. Lunev */ 81848c66e8SPaolo Bonzini CoMutex lock; 82019d6b8fSAnthony Liguori 839eae9ccaSDenis V. Lunev ParallelsHeader *header; 849eae9ccaSDenis V. Lunev uint32_t header_size; 856dd6b9f1SDenis V. Lunev bool header_unclean; 866dd6b9f1SDenis V. Lunev 870d31c7c2SDenis V. Lunev unsigned long *bat_dirty_bmap; 880d31c7c2SDenis V. Lunev unsigned int bat_dirty_block; 890d31c7c2SDenis V. Lunev 90369f7de9SDenis V. Lunev uint32_t *bat_bitmap; 91369f7de9SDenis V. Lunev unsigned int bat_size; 92019d6b8fSAnthony Liguori 9319f5dc15SDenis V. Lunev int64_t data_end; 94d6179011SDenis V. Lunev uint64_t prealloc_size; 95d6179011SDenis V. Lunev ParallelsPreallocMode prealloc_mode; 96d6179011SDenis V. Lunev 979302e863SKevin Wolf unsigned int tracks; 98d25d5980SDenis V. Lunev 99d25d5980SDenis V. Lunev unsigned int off_multiplier; 100019d6b8fSAnthony Liguori } BDRVParallelsState; 101019d6b8fSAnthony Liguori 102555cc9d9SDenis V. Lunev 103d6179011SDenis V. Lunev #define PARALLELS_OPT_PREALLOC_MODE "prealloc-mode" 104d6179011SDenis V. Lunev #define PARALLELS_OPT_PREALLOC_SIZE "prealloc-size" 105d6179011SDenis V. Lunev 106d6179011SDenis V. Lunev static QemuOptsList parallels_runtime_opts = { 107d6179011SDenis V. Lunev .name = "parallels", 108d6179011SDenis V. Lunev .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head), 109d6179011SDenis V. Lunev .desc = { 110d6179011SDenis V. Lunev { 111d6179011SDenis V. Lunev .name = PARALLELS_OPT_PREALLOC_SIZE, 112d6179011SDenis V. Lunev .type = QEMU_OPT_SIZE, 113d6179011SDenis V. Lunev .help = "Preallocation size on image expansion", 114d6179011SDenis V. Lunev .def_value_str = "128MiB", 115d6179011SDenis V. Lunev }, 116d6179011SDenis V. Lunev { 117d6179011SDenis V. Lunev .name = PARALLELS_OPT_PREALLOC_MODE, 118d6179011SDenis V. Lunev .type = QEMU_OPT_STRING, 119d6179011SDenis V. Lunev .help = "Preallocation mode on image expansion " 120d6179011SDenis V. Lunev "(allowed values: falloc, truncate)", 121d6179011SDenis V. Lunev .def_value_str = "falloc", 122d6179011SDenis V. Lunev }, 123d6179011SDenis V. Lunev { /* end of list */ }, 124d6179011SDenis V. Lunev }, 125d6179011SDenis V. Lunev }; 126d6179011SDenis V. Lunev 127d6179011SDenis V. Lunev 128555cc9d9SDenis V. Lunev static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx) 129555cc9d9SDenis V. Lunev { 130dd97cdc0SDenis V. Lunev return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier; 131555cc9d9SDenis V. Lunev } 132555cc9d9SDenis V. Lunev 1332d68e22eSDenis V. Lunev static uint32_t bat_entry_off(uint32_t idx) 1342d68e22eSDenis V. Lunev { 1352d68e22eSDenis V. Lunev return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx; 1362d68e22eSDenis V. Lunev } 1372d68e22eSDenis V. Lunev 13829442569SRoman Kagan static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num) 139019d6b8fSAnthony Liguori { 140c34d2451SDavid Woodhouse uint32_t index, offset; 141019d6b8fSAnthony Liguori 142019d6b8fSAnthony Liguori index = sector_num / s->tracks; 143019d6b8fSAnthony Liguori offset = sector_num % s->tracks; 144019d6b8fSAnthony Liguori 1459d8b88f6SChristoph Hellwig /* not allocated */ 146369f7de9SDenis V. Lunev if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) { 147019d6b8fSAnthony Liguori return -1; 148369f7de9SDenis V. Lunev } 149555cc9d9SDenis V. Lunev return bat2sect(s, index) + offset; 150019d6b8fSAnthony Liguori } 151019d6b8fSAnthony Liguori 1529de9da17SRoman Kagan static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num, 1539de9da17SRoman Kagan int nb_sectors) 1549de9da17SRoman Kagan { 1559de9da17SRoman Kagan int ret = s->tracks - sector_num % s->tracks; 1569de9da17SRoman Kagan return MIN(nb_sectors, ret); 1579de9da17SRoman Kagan } 1589de9da17SRoman Kagan 1596953d920SDenis V. Lunev static int64_t block_status(BDRVParallelsState *s, int64_t sector_num, 1606953d920SDenis V. Lunev int nb_sectors, int *pnum) 1616953d920SDenis V. Lunev { 1626953d920SDenis V. Lunev int64_t start_off = -2, prev_end_off = -2; 1636953d920SDenis V. Lunev 1646953d920SDenis V. Lunev *pnum = 0; 1656953d920SDenis V. Lunev while (nb_sectors > 0 || start_off == -2) { 1666953d920SDenis V. Lunev int64_t offset = seek_to_sector(s, sector_num); 1676953d920SDenis V. Lunev int to_end; 1686953d920SDenis V. Lunev 1696953d920SDenis V. Lunev if (start_off == -2) { 1706953d920SDenis V. Lunev start_off = offset; 1716953d920SDenis V. Lunev prev_end_off = offset; 1726953d920SDenis V. Lunev } else if (offset != prev_end_off) { 1736953d920SDenis V. Lunev break; 1746953d920SDenis V. Lunev } 1756953d920SDenis V. Lunev 1766953d920SDenis V. Lunev to_end = cluster_remainder(s, sector_num, nb_sectors); 1776953d920SDenis V. Lunev nb_sectors -= to_end; 1786953d920SDenis V. Lunev sector_num += to_end; 1796953d920SDenis V. Lunev *pnum += to_end; 1806953d920SDenis V. Lunev 1816953d920SDenis V. Lunev if (offset > 0) { 1826953d920SDenis V. Lunev prev_end_off += to_end; 1836953d920SDenis V. Lunev } 1846953d920SDenis V. Lunev } 1856953d920SDenis V. Lunev return start_off; 1866953d920SDenis V. Lunev } 1876953d920SDenis V. Lunev 188ddd2ef2cSDenis V. Lunev static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num, 189ddd2ef2cSDenis V. Lunev int nb_sectors, int *pnum) 1905a41e1faSDenis V. Lunev { 1915a41e1faSDenis V. Lunev BDRVParallelsState *s = bs->opaque; 192ddd2ef2cSDenis V. Lunev uint32_t idx, to_allocate, i; 193ddd2ef2cSDenis V. Lunev int64_t pos, space; 194ddd2ef2cSDenis V. Lunev 195ddd2ef2cSDenis V. Lunev pos = block_status(s, sector_num, nb_sectors, pnum); 196ddd2ef2cSDenis V. Lunev if (pos > 0) { 197ddd2ef2cSDenis V. Lunev return pos; 198ddd2ef2cSDenis V. Lunev } 1995a41e1faSDenis V. Lunev 2005a41e1faSDenis V. Lunev idx = sector_num / s->tracks; 201369f7de9SDenis V. Lunev if (idx >= s->bat_size) { 2025a41e1faSDenis V. Lunev return -EINVAL; 2035a41e1faSDenis V. Lunev } 2045a41e1faSDenis V. Lunev 205ddd2ef2cSDenis V. Lunev to_allocate = (sector_num + *pnum + s->tracks - 1) / s->tracks - idx; 206ddd2ef2cSDenis V. Lunev space = to_allocate * s->tracks; 2079a4f4c31SKevin Wolf if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) { 20819f5dc15SDenis V. Lunev int ret; 209ddd2ef2cSDenis V. Lunev space += s->prealloc_size; 21019f5dc15SDenis V. Lunev if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) { 2119a4f4c31SKevin Wolf ret = bdrv_write_zeroes(bs->file->bs, s->data_end, space, 0); 2125a41e1faSDenis V. Lunev } else { 2139a4f4c31SKevin Wolf ret = bdrv_truncate(bs->file->bs, 214ddd2ef2cSDenis V. Lunev (s->data_end + space) << BDRV_SECTOR_BITS); 2155a41e1faSDenis V. Lunev } 2165a41e1faSDenis V. Lunev if (ret < 0) { 2175a41e1faSDenis V. Lunev return ret; 2185a41e1faSDenis V. Lunev } 21919f5dc15SDenis V. Lunev } 220ddd2ef2cSDenis V. Lunev 221ddd2ef2cSDenis V. Lunev for (i = 0; i < to_allocate; i++) { 222ddd2ef2cSDenis V. Lunev s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier); 22319f5dc15SDenis V. Lunev s->data_end += s->tracks; 224ddd2ef2cSDenis V. Lunev bitmap_set(s->bat_dirty_bmap, 225c9f6856dSVladimir Sementsov-Ogievskiy bat_entry_off(idx + i) / s->bat_dirty_block, 1); 226ddd2ef2cSDenis V. Lunev } 2275a41e1faSDenis V. Lunev 228ddd2ef2cSDenis V. Lunev return bat2sect(s, idx) + sector_num % s->tracks; 2295a41e1faSDenis V. Lunev } 2305a41e1faSDenis V. Lunev 2310d31c7c2SDenis V. Lunev 2320d31c7c2SDenis V. Lunev static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs) 2330d31c7c2SDenis V. Lunev { 2340d31c7c2SDenis V. Lunev BDRVParallelsState *s = bs->opaque; 2350d31c7c2SDenis V. Lunev unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block); 2360d31c7c2SDenis V. Lunev unsigned long bit; 2370d31c7c2SDenis V. Lunev 2380d31c7c2SDenis V. Lunev qemu_co_mutex_lock(&s->lock); 2390d31c7c2SDenis V. Lunev 2400d31c7c2SDenis V. Lunev bit = find_first_bit(s->bat_dirty_bmap, size); 2410d31c7c2SDenis V. Lunev while (bit < size) { 2420d31c7c2SDenis V. Lunev uint32_t off = bit * s->bat_dirty_block; 2430d31c7c2SDenis V. Lunev uint32_t to_write = s->bat_dirty_block; 2440d31c7c2SDenis V. Lunev int ret; 2450d31c7c2SDenis V. Lunev 2460d31c7c2SDenis V. Lunev if (off + to_write > s->header_size) { 2470d31c7c2SDenis V. Lunev to_write = s->header_size - off; 2480d31c7c2SDenis V. Lunev } 2499a4f4c31SKevin Wolf ret = bdrv_pwrite(bs->file->bs, off, (uint8_t *)s->header + off, 2509a4f4c31SKevin Wolf to_write); 2510d31c7c2SDenis V. Lunev if (ret < 0) { 2520d31c7c2SDenis V. Lunev qemu_co_mutex_unlock(&s->lock); 2530d31c7c2SDenis V. Lunev return ret; 2540d31c7c2SDenis V. Lunev } 2550d31c7c2SDenis V. Lunev bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1); 2560d31c7c2SDenis V. Lunev } 2570d31c7c2SDenis V. Lunev bitmap_zero(s->bat_dirty_bmap, size); 2580d31c7c2SDenis V. Lunev 2590d31c7c2SDenis V. Lunev qemu_co_mutex_unlock(&s->lock); 2600d31c7c2SDenis V. Lunev return 0; 2610d31c7c2SDenis V. Lunev } 2620d31c7c2SDenis V. Lunev 2630d31c7c2SDenis V. Lunev 264dd3bed16SRoman Kagan static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs, 26567a0fd2aSFam Zheng int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file) 266dd3bed16SRoman Kagan { 267dd3bed16SRoman Kagan BDRVParallelsState *s = bs->opaque; 268dd3bed16SRoman Kagan int64_t offset; 269dd3bed16SRoman Kagan 270dd3bed16SRoman Kagan qemu_co_mutex_lock(&s->lock); 2716953d920SDenis V. Lunev offset = block_status(s, sector_num, nb_sectors, pnum); 272dd3bed16SRoman Kagan qemu_co_mutex_unlock(&s->lock); 273dd3bed16SRoman Kagan 274dd3bed16SRoman Kagan if (offset < 0) { 275dd3bed16SRoman Kagan return 0; 276dd3bed16SRoman Kagan } 277dd3bed16SRoman Kagan 278ddf4987dSFam Zheng *file = bs->file->bs; 279dd3bed16SRoman Kagan return (offset << BDRV_SECTOR_BITS) | 280dd3bed16SRoman Kagan BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 281dd3bed16SRoman Kagan } 282dd3bed16SRoman Kagan 2835a41e1faSDenis V. Lunev static coroutine_fn int parallels_co_writev(BlockDriverState *bs, 2845a41e1faSDenis V. Lunev int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2855a41e1faSDenis V. Lunev { 2865a41e1faSDenis V. Lunev BDRVParallelsState *s = bs->opaque; 2875a41e1faSDenis V. Lunev uint64_t bytes_done = 0; 2885a41e1faSDenis V. Lunev QEMUIOVector hd_qiov; 2895a41e1faSDenis V. Lunev int ret = 0; 2905a41e1faSDenis V. Lunev 2915a41e1faSDenis V. Lunev qemu_iovec_init(&hd_qiov, qiov->niov); 2925a41e1faSDenis V. Lunev 2935a41e1faSDenis V. Lunev while (nb_sectors > 0) { 2945a41e1faSDenis V. Lunev int64_t position; 2955a41e1faSDenis V. Lunev int n, nbytes; 2965a41e1faSDenis V. Lunev 2975a41e1faSDenis V. Lunev qemu_co_mutex_lock(&s->lock); 298ddd2ef2cSDenis V. Lunev position = allocate_clusters(bs, sector_num, nb_sectors, &n); 2995a41e1faSDenis V. Lunev qemu_co_mutex_unlock(&s->lock); 3005a41e1faSDenis V. Lunev if (position < 0) { 3015a41e1faSDenis V. Lunev ret = (int)position; 3025a41e1faSDenis V. Lunev break; 3035a41e1faSDenis V. Lunev } 3045a41e1faSDenis V. Lunev 3055a41e1faSDenis V. Lunev nbytes = n << BDRV_SECTOR_BITS; 3065a41e1faSDenis V. Lunev 3075a41e1faSDenis V. Lunev qemu_iovec_reset(&hd_qiov); 3085a41e1faSDenis V. Lunev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); 3095a41e1faSDenis V. Lunev 3109a4f4c31SKevin Wolf ret = bdrv_co_writev(bs->file->bs, position, n, &hd_qiov); 3115a41e1faSDenis V. Lunev if (ret < 0) { 3125a41e1faSDenis V. Lunev break; 3135a41e1faSDenis V. Lunev } 3145a41e1faSDenis V. Lunev 3155a41e1faSDenis V. Lunev nb_sectors -= n; 3165a41e1faSDenis V. Lunev sector_num += n; 3175a41e1faSDenis V. Lunev bytes_done += nbytes; 3185a41e1faSDenis V. Lunev } 3195a41e1faSDenis V. Lunev 3205a41e1faSDenis V. Lunev qemu_iovec_destroy(&hd_qiov); 3215a41e1faSDenis V. Lunev return ret; 3225a41e1faSDenis V. Lunev } 3235a41e1faSDenis V. Lunev 324481fb9cfSDenis V. Lunev static coroutine_fn int parallels_co_readv(BlockDriverState *bs, 325481fb9cfSDenis V. Lunev int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 326019d6b8fSAnthony Liguori { 32729442569SRoman Kagan BDRVParallelsState *s = bs->opaque; 328481fb9cfSDenis V. Lunev uint64_t bytes_done = 0; 329481fb9cfSDenis V. Lunev QEMUIOVector hd_qiov; 330481fb9cfSDenis V. Lunev int ret = 0; 331481fb9cfSDenis V. Lunev 332481fb9cfSDenis V. Lunev qemu_iovec_init(&hd_qiov, qiov->niov); 33329442569SRoman Kagan 334019d6b8fSAnthony Liguori while (nb_sectors > 0) { 335481fb9cfSDenis V. Lunev int64_t position; 336481fb9cfSDenis V. Lunev int n, nbytes; 337481fb9cfSDenis V. Lunev 338481fb9cfSDenis V. Lunev qemu_co_mutex_lock(&s->lock); 3396953d920SDenis V. Lunev position = block_status(s, sector_num, nb_sectors, &n); 340481fb9cfSDenis V. Lunev qemu_co_mutex_unlock(&s->lock); 341481fb9cfSDenis V. Lunev 342481fb9cfSDenis V. Lunev nbytes = n << BDRV_SECTOR_BITS; 343481fb9cfSDenis V. Lunev 344481fb9cfSDenis V. Lunev if (position < 0) { 345481fb9cfSDenis V. Lunev qemu_iovec_memset(qiov, bytes_done, 0, nbytes); 3469d8b88f6SChristoph Hellwig } else { 347481fb9cfSDenis V. Lunev qemu_iovec_reset(&hd_qiov); 348481fb9cfSDenis V. Lunev qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes); 349481fb9cfSDenis V. Lunev 3509a4f4c31SKevin Wolf ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov); 351481fb9cfSDenis V. Lunev if (ret < 0) { 352481fb9cfSDenis V. Lunev break; 3539d8b88f6SChristoph Hellwig } 354019d6b8fSAnthony Liguori } 355019d6b8fSAnthony Liguori 356481fb9cfSDenis V. Lunev nb_sectors -= n; 357481fb9cfSDenis V. Lunev sector_num += n; 358481fb9cfSDenis V. Lunev bytes_done += nbytes; 359481fb9cfSDenis V. Lunev } 360481fb9cfSDenis V. Lunev 361481fb9cfSDenis V. Lunev qemu_iovec_destroy(&hd_qiov); 3622914caa0SPaolo Bonzini return ret; 3632914caa0SPaolo Bonzini } 3642914caa0SPaolo Bonzini 36549ad6467SDenis V. Lunev 36649ad6467SDenis V. Lunev static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res, 36749ad6467SDenis V. Lunev BdrvCheckMode fix) 36849ad6467SDenis V. Lunev { 36949ad6467SDenis V. Lunev BDRVParallelsState *s = bs->opaque; 37049ad6467SDenis V. Lunev int64_t size, prev_off, high_off; 37149ad6467SDenis V. Lunev int ret; 37249ad6467SDenis V. Lunev uint32_t i; 37349ad6467SDenis V. Lunev bool flush_bat = false; 37449ad6467SDenis V. Lunev int cluster_size = s->tracks << BDRV_SECTOR_BITS; 37549ad6467SDenis V. Lunev 3769a4f4c31SKevin Wolf size = bdrv_getlength(bs->file->bs); 37749ad6467SDenis V. Lunev if (size < 0) { 37849ad6467SDenis V. Lunev res->check_errors++; 37949ad6467SDenis V. Lunev return size; 38049ad6467SDenis V. Lunev } 38149ad6467SDenis V. Lunev 3826dd6b9f1SDenis V. Lunev if (s->header_unclean) { 3836dd6b9f1SDenis V. Lunev fprintf(stderr, "%s image was not closed correctly\n", 3846dd6b9f1SDenis V. Lunev fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR"); 3856dd6b9f1SDenis V. Lunev res->corruptions++; 3866dd6b9f1SDenis V. Lunev if (fix & BDRV_FIX_ERRORS) { 3876dd6b9f1SDenis V. Lunev /* parallels_close will do the job right */ 3886dd6b9f1SDenis V. Lunev res->corruptions_fixed++; 3896dd6b9f1SDenis V. Lunev s->header_unclean = false; 3906dd6b9f1SDenis V. Lunev } 3916dd6b9f1SDenis V. Lunev } 3926dd6b9f1SDenis V. Lunev 39349ad6467SDenis V. Lunev res->bfi.total_clusters = s->bat_size; 39449ad6467SDenis V. Lunev res->bfi.compressed_clusters = 0; /* compression is not supported */ 39549ad6467SDenis V. Lunev 39649ad6467SDenis V. Lunev high_off = 0; 39749ad6467SDenis V. Lunev prev_off = 0; 39849ad6467SDenis V. Lunev for (i = 0; i < s->bat_size; i++) { 39949ad6467SDenis V. Lunev int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS; 40049ad6467SDenis V. Lunev if (off == 0) { 40149ad6467SDenis V. Lunev prev_off = 0; 40249ad6467SDenis V. Lunev continue; 40349ad6467SDenis V. Lunev } 40449ad6467SDenis V. Lunev 40549ad6467SDenis V. Lunev /* cluster outside the image */ 40649ad6467SDenis V. Lunev if (off > size) { 40749ad6467SDenis V. Lunev fprintf(stderr, "%s cluster %u is outside image\n", 40849ad6467SDenis V. Lunev fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i); 40949ad6467SDenis V. Lunev res->corruptions++; 41049ad6467SDenis V. Lunev if (fix & BDRV_FIX_ERRORS) { 41149ad6467SDenis V. Lunev prev_off = 0; 41249ad6467SDenis V. Lunev s->bat_bitmap[i] = 0; 41349ad6467SDenis V. Lunev res->corruptions_fixed++; 41449ad6467SDenis V. Lunev flush_bat = true; 41549ad6467SDenis V. Lunev continue; 41649ad6467SDenis V. Lunev } 41749ad6467SDenis V. Lunev } 41849ad6467SDenis V. Lunev 41949ad6467SDenis V. Lunev res->bfi.allocated_clusters++; 42049ad6467SDenis V. Lunev if (off > high_off) { 42149ad6467SDenis V. Lunev high_off = off; 42249ad6467SDenis V. Lunev } 42349ad6467SDenis V. Lunev 42449ad6467SDenis V. Lunev if (prev_off != 0 && (prev_off + cluster_size) != off) { 42549ad6467SDenis V. Lunev res->bfi.fragmented_clusters++; 42649ad6467SDenis V. Lunev } 42749ad6467SDenis V. Lunev prev_off = off; 42849ad6467SDenis V. Lunev } 42949ad6467SDenis V. Lunev 43049ad6467SDenis V. Lunev if (flush_bat) { 4319a4f4c31SKevin Wolf ret = bdrv_pwrite_sync(bs->file->bs, 0, s->header, s->header_size); 43249ad6467SDenis V. Lunev if (ret < 0) { 43349ad6467SDenis V. Lunev res->check_errors++; 43449ad6467SDenis V. Lunev return ret; 43549ad6467SDenis V. Lunev } 43649ad6467SDenis V. Lunev } 43749ad6467SDenis V. Lunev 43849ad6467SDenis V. Lunev res->image_end_offset = high_off + cluster_size; 43949ad6467SDenis V. Lunev if (size > res->image_end_offset) { 44049ad6467SDenis V. Lunev int64_t count; 44149ad6467SDenis V. Lunev count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size); 44249ad6467SDenis V. Lunev fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n", 44349ad6467SDenis V. Lunev fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR", 44449ad6467SDenis V. Lunev size - res->image_end_offset); 44549ad6467SDenis V. Lunev res->leaks += count; 44649ad6467SDenis V. Lunev if (fix & BDRV_FIX_LEAKS) { 4479a4f4c31SKevin Wolf ret = bdrv_truncate(bs->file->bs, res->image_end_offset); 44849ad6467SDenis V. Lunev if (ret < 0) { 44949ad6467SDenis V. Lunev res->check_errors++; 45049ad6467SDenis V. Lunev return ret; 45149ad6467SDenis V. Lunev } 45249ad6467SDenis V. Lunev res->leaks_fixed += count; 45349ad6467SDenis V. Lunev } 45449ad6467SDenis V. Lunev } 45549ad6467SDenis V. Lunev 45649ad6467SDenis V. Lunev return 0; 45749ad6467SDenis V. Lunev } 45849ad6467SDenis V. Lunev 45949ad6467SDenis V. Lunev 46074cf6c50SDenis V. Lunev static int parallels_create(const char *filename, QemuOpts *opts, Error **errp) 46174cf6c50SDenis V. Lunev { 46274cf6c50SDenis V. Lunev int64_t total_size, cl_size; 46374cf6c50SDenis V. Lunev uint8_t tmp[BDRV_SECTOR_SIZE]; 46474cf6c50SDenis V. Lunev Error *local_err = NULL; 465*8942764fSKevin Wolf BlockBackend *file; 466369f7de9SDenis V. Lunev uint32_t bat_entries, bat_sectors; 46774cf6c50SDenis V. Lunev ParallelsHeader header; 46874cf6c50SDenis V. Lunev int ret; 46974cf6c50SDenis V. Lunev 47074cf6c50SDenis V. Lunev total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 47174cf6c50SDenis V. Lunev BDRV_SECTOR_SIZE); 47274cf6c50SDenis V. Lunev cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 47374cf6c50SDenis V. Lunev DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE); 47474cf6c50SDenis V. Lunev 47574cf6c50SDenis V. Lunev ret = bdrv_create_file(filename, opts, &local_err); 47674cf6c50SDenis V. Lunev if (ret < 0) { 47774cf6c50SDenis V. Lunev error_propagate(errp, local_err); 47874cf6c50SDenis V. Lunev return ret; 47974cf6c50SDenis V. Lunev } 48074cf6c50SDenis V. Lunev 481*8942764fSKevin Wolf file = blk_new_open("image", filename, NULL, NULL, 4826340472cSKevin Wolf BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL, 4836340472cSKevin Wolf &local_err); 484*8942764fSKevin Wolf if (file == NULL) { 48574cf6c50SDenis V. Lunev error_propagate(errp, local_err); 486*8942764fSKevin Wolf return -EIO; 48774cf6c50SDenis V. Lunev } 488*8942764fSKevin Wolf 489*8942764fSKevin Wolf blk_set_allow_write_beyond_eof(file, true); 490*8942764fSKevin Wolf 491*8942764fSKevin Wolf ret = blk_truncate(file, 0); 49274cf6c50SDenis V. Lunev if (ret < 0) { 49374cf6c50SDenis V. Lunev goto exit; 49474cf6c50SDenis V. Lunev } 49574cf6c50SDenis V. Lunev 496369f7de9SDenis V. Lunev bat_entries = DIV_ROUND_UP(total_size, cl_size); 4972d68e22eSDenis V. Lunev bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size); 498369f7de9SDenis V. Lunev bat_sectors = (bat_sectors * cl_size) >> BDRV_SECTOR_BITS; 49974cf6c50SDenis V. Lunev 50074cf6c50SDenis V. Lunev memset(&header, 0, sizeof(header)); 50174cf6c50SDenis V. Lunev memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic)); 50274cf6c50SDenis V. Lunev header.version = cpu_to_le32(HEADER_VERSION); 50374cf6c50SDenis V. Lunev /* don't care much about geometry, it is not used on image level */ 50474cf6c50SDenis V. Lunev header.heads = cpu_to_le32(16); 50574cf6c50SDenis V. Lunev header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32); 50674cf6c50SDenis V. Lunev header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS); 507369f7de9SDenis V. Lunev header.bat_entries = cpu_to_le32(bat_entries); 50874cf6c50SDenis V. Lunev header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE)); 509369f7de9SDenis V. Lunev header.data_off = cpu_to_le32(bat_sectors); 51074cf6c50SDenis V. Lunev 51174cf6c50SDenis V. Lunev /* write all the data */ 51274cf6c50SDenis V. Lunev memset(tmp, 0, sizeof(tmp)); 51374cf6c50SDenis V. Lunev memcpy(tmp, &header, sizeof(header)); 51474cf6c50SDenis V. Lunev 515*8942764fSKevin Wolf ret = blk_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE); 51674cf6c50SDenis V. Lunev if (ret < 0) { 51774cf6c50SDenis V. Lunev goto exit; 51874cf6c50SDenis V. Lunev } 519*8942764fSKevin Wolf ret = blk_write_zeroes(file, 1, bat_sectors - 1, 0); 52074cf6c50SDenis V. Lunev if (ret < 0) { 52174cf6c50SDenis V. Lunev goto exit; 52274cf6c50SDenis V. Lunev } 52374cf6c50SDenis V. Lunev ret = 0; 52474cf6c50SDenis V. Lunev 52574cf6c50SDenis V. Lunev done: 526*8942764fSKevin Wolf blk_unref(file); 52774cf6c50SDenis V. Lunev return ret; 52874cf6c50SDenis V. Lunev 52974cf6c50SDenis V. Lunev exit: 53074cf6c50SDenis V. Lunev error_setg_errno(errp, -ret, "Failed to create Parallels image"); 53174cf6c50SDenis V. Lunev goto done; 53274cf6c50SDenis V. Lunev } 53374cf6c50SDenis V. Lunev 53423d6bd3bSDenis V. Lunev 53523d6bd3bSDenis V. Lunev static int parallels_probe(const uint8_t *buf, int buf_size, 53623d6bd3bSDenis V. Lunev const char *filename) 53723d6bd3bSDenis V. Lunev { 53823d6bd3bSDenis V. Lunev const ParallelsHeader *ph = (const void *)buf; 53923d6bd3bSDenis V. Lunev 54023d6bd3bSDenis V. Lunev if (buf_size < sizeof(ParallelsHeader)) { 54123d6bd3bSDenis V. Lunev return 0; 54223d6bd3bSDenis V. Lunev } 54323d6bd3bSDenis V. Lunev 54423d6bd3bSDenis V. Lunev if ((!memcmp(ph->magic, HEADER_MAGIC, 16) || 54523d6bd3bSDenis V. Lunev !memcmp(ph->magic, HEADER_MAGIC2, 16)) && 54623d6bd3bSDenis V. Lunev (le32_to_cpu(ph->version) == HEADER_VERSION)) { 54723d6bd3bSDenis V. Lunev return 100; 54823d6bd3bSDenis V. Lunev } 54923d6bd3bSDenis V. Lunev 55023d6bd3bSDenis V. Lunev return 0; 55123d6bd3bSDenis V. Lunev } 55223d6bd3bSDenis V. Lunev 5536dd6b9f1SDenis V. Lunev static int parallels_update_header(BlockDriverState *bs) 5546dd6b9f1SDenis V. Lunev { 5556dd6b9f1SDenis V. Lunev BDRVParallelsState *s = bs->opaque; 5569a4f4c31SKevin Wolf unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs), 5579a4f4c31SKevin Wolf sizeof(ParallelsHeader)); 5586dd6b9f1SDenis V. Lunev 5596dd6b9f1SDenis V. Lunev if (size > s->header_size) { 5606dd6b9f1SDenis V. Lunev size = s->header_size; 5616dd6b9f1SDenis V. Lunev } 5629a4f4c31SKevin Wolf return bdrv_pwrite_sync(bs->file->bs, 0, s->header, size); 5636dd6b9f1SDenis V. Lunev } 5646dd6b9f1SDenis V. Lunev 56523d6bd3bSDenis V. Lunev static int parallels_open(BlockDriverState *bs, QDict *options, int flags, 56623d6bd3bSDenis V. Lunev Error **errp) 56723d6bd3bSDenis V. Lunev { 56823d6bd3bSDenis V. Lunev BDRVParallelsState *s = bs->opaque; 56923d6bd3bSDenis V. Lunev ParallelsHeader ph; 57019f5dc15SDenis V. Lunev int ret, size, i; 571d6179011SDenis V. Lunev QemuOpts *opts = NULL; 572d6179011SDenis V. Lunev Error *local_err = NULL; 573d6179011SDenis V. Lunev char *buf; 57423d6bd3bSDenis V. Lunev 5759a4f4c31SKevin Wolf ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph)); 57623d6bd3bSDenis V. Lunev if (ret < 0) { 57723d6bd3bSDenis V. Lunev goto fail; 57823d6bd3bSDenis V. Lunev } 57923d6bd3bSDenis V. Lunev 58023d6bd3bSDenis V. Lunev bs->total_sectors = le64_to_cpu(ph.nb_sectors); 58123d6bd3bSDenis V. Lunev 58223d6bd3bSDenis V. Lunev if (le32_to_cpu(ph.version) != HEADER_VERSION) { 58323d6bd3bSDenis V. Lunev goto fail_format; 58423d6bd3bSDenis V. Lunev } 58523d6bd3bSDenis V. Lunev if (!memcmp(ph.magic, HEADER_MAGIC, 16)) { 58623d6bd3bSDenis V. Lunev s->off_multiplier = 1; 58723d6bd3bSDenis V. Lunev bs->total_sectors = 0xffffffff & bs->total_sectors; 58823d6bd3bSDenis V. Lunev } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) { 58923d6bd3bSDenis V. Lunev s->off_multiplier = le32_to_cpu(ph.tracks); 59023d6bd3bSDenis V. Lunev } else { 59123d6bd3bSDenis V. Lunev goto fail_format; 59223d6bd3bSDenis V. Lunev } 59323d6bd3bSDenis V. Lunev 59423d6bd3bSDenis V. Lunev s->tracks = le32_to_cpu(ph.tracks); 59523d6bd3bSDenis V. Lunev if (s->tracks == 0) { 59623d6bd3bSDenis V. Lunev error_setg(errp, "Invalid image: Zero sectors per track"); 59723d6bd3bSDenis V. Lunev ret = -EINVAL; 59823d6bd3bSDenis V. Lunev goto fail; 59923d6bd3bSDenis V. Lunev } 60023d6bd3bSDenis V. Lunev if (s->tracks > INT32_MAX/513) { 60123d6bd3bSDenis V. Lunev error_setg(errp, "Invalid image: Too big cluster"); 60223d6bd3bSDenis V. Lunev ret = -EFBIG; 60323d6bd3bSDenis V. Lunev goto fail; 60423d6bd3bSDenis V. Lunev } 60523d6bd3bSDenis V. Lunev 60623d6bd3bSDenis V. Lunev s->bat_size = le32_to_cpu(ph.bat_entries); 60723d6bd3bSDenis V. Lunev if (s->bat_size > INT_MAX / sizeof(uint32_t)) { 60823d6bd3bSDenis V. Lunev error_setg(errp, "Catalog too large"); 60923d6bd3bSDenis V. Lunev ret = -EFBIG; 61023d6bd3bSDenis V. Lunev goto fail; 61123d6bd3bSDenis V. Lunev } 61223d6bd3bSDenis V. Lunev 6132d68e22eSDenis V. Lunev size = bat_entry_off(s->bat_size); 6149a4f4c31SKevin Wolf s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file->bs)); 6159a4f4c31SKevin Wolf s->header = qemu_try_blockalign(bs->file->bs, s->header_size); 61623d6bd3bSDenis V. Lunev if (s->header == NULL) { 61723d6bd3bSDenis V. Lunev ret = -ENOMEM; 61823d6bd3bSDenis V. Lunev goto fail; 61923d6bd3bSDenis V. Lunev } 62019f5dc15SDenis V. Lunev s->data_end = le32_to_cpu(ph.data_off); 62119f5dc15SDenis V. Lunev if (s->data_end == 0) { 62219f5dc15SDenis V. Lunev s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); 62319f5dc15SDenis V. Lunev } 62419f5dc15SDenis V. Lunev if (s->data_end < s->header_size) { 62523d6bd3bSDenis V. Lunev /* there is not enough unused space to fit to block align between BAT 62623d6bd3bSDenis V. Lunev and actual data. We can't avoid read-modify-write... */ 62723d6bd3bSDenis V. Lunev s->header_size = size; 62823d6bd3bSDenis V. Lunev } 62923d6bd3bSDenis V. Lunev 6309a4f4c31SKevin Wolf ret = bdrv_pread(bs->file->bs, 0, s->header, s->header_size); 63123d6bd3bSDenis V. Lunev if (ret < 0) { 63223d6bd3bSDenis V. Lunev goto fail; 63323d6bd3bSDenis V. Lunev } 63423d6bd3bSDenis V. Lunev s->bat_bitmap = (uint32_t *)(s->header + 1); 63523d6bd3bSDenis V. Lunev 63619f5dc15SDenis V. Lunev for (i = 0; i < s->bat_size; i++) { 63719f5dc15SDenis V. Lunev int64_t off = bat2sect(s, i); 63819f5dc15SDenis V. Lunev if (off >= s->data_end) { 63919f5dc15SDenis V. Lunev s->data_end = off + s->tracks; 64019f5dc15SDenis V. Lunev } 64119f5dc15SDenis V. Lunev } 64219f5dc15SDenis V. Lunev 6436dd6b9f1SDenis V. Lunev if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) { 6446dd6b9f1SDenis V. Lunev /* Image was not closed correctly. The check is mandatory */ 6456dd6b9f1SDenis V. Lunev s->header_unclean = true; 6466dd6b9f1SDenis V. Lunev if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) { 6476dd6b9f1SDenis V. Lunev error_setg(errp, "parallels: Image was not closed correctly; " 6486dd6b9f1SDenis V. Lunev "cannot be opened read/write"); 6496dd6b9f1SDenis V. Lunev ret = -EACCES; 6506dd6b9f1SDenis V. Lunev goto fail; 6516dd6b9f1SDenis V. Lunev } 6526dd6b9f1SDenis V. Lunev } 6536dd6b9f1SDenis V. Lunev 654d6179011SDenis V. Lunev opts = qemu_opts_create(¶llels_runtime_opts, NULL, 0, &local_err); 655d6179011SDenis V. Lunev if (local_err != NULL) { 656d6179011SDenis V. Lunev goto fail_options; 657d6179011SDenis V. Lunev } 658d6179011SDenis V. Lunev 659d6179011SDenis V. Lunev qemu_opts_absorb_qdict(opts, options, &local_err); 660d6179011SDenis V. Lunev if (local_err != NULL) { 661d6179011SDenis V. Lunev goto fail_options; 662d6179011SDenis V. Lunev } 663d6179011SDenis V. Lunev 664d6179011SDenis V. Lunev s->prealloc_size = 665d6179011SDenis V. Lunev qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0); 666d6179011SDenis V. Lunev s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS); 667d6179011SDenis V. Lunev buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE); 668d6179011SDenis V. Lunev s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf, 6697fb1cf16SEric Blake PRL_PREALLOC_MODE__MAX, PRL_PREALLOC_MODE_FALLOCATE, &local_err); 670d6179011SDenis V. Lunev g_free(buf); 671d6179011SDenis V. Lunev if (local_err != NULL) { 672d6179011SDenis V. Lunev goto fail_options; 673d6179011SDenis V. Lunev } 6749a4f4c31SKevin Wolf if (!bdrv_has_zero_init(bs->file->bs) || 6759a4f4c31SKevin Wolf bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 0) { 676d6179011SDenis V. Lunev s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE; 677d6179011SDenis V. Lunev } 678d6179011SDenis V. Lunev 6796dd6b9f1SDenis V. Lunev if (flags & BDRV_O_RDWR) { 6806dd6b9f1SDenis V. Lunev s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC); 6816dd6b9f1SDenis V. Lunev ret = parallels_update_header(bs); 6826dd6b9f1SDenis V. Lunev if (ret < 0) { 6836dd6b9f1SDenis V. Lunev goto fail; 6846dd6b9f1SDenis V. Lunev } 6856dd6b9f1SDenis V. Lunev } 6866dd6b9f1SDenis V. Lunev 6870d31c7c2SDenis V. Lunev s->bat_dirty_block = 4 * getpagesize(); 6880d31c7c2SDenis V. Lunev s->bat_dirty_bmap = 6890d31c7c2SDenis V. Lunev bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block)); 6900d31c7c2SDenis V. Lunev 69123d6bd3bSDenis V. Lunev qemu_co_mutex_init(&s->lock); 69223d6bd3bSDenis V. Lunev return 0; 69323d6bd3bSDenis V. Lunev 69423d6bd3bSDenis V. Lunev fail_format: 69523d6bd3bSDenis V. Lunev error_setg(errp, "Image not in Parallels format"); 69623d6bd3bSDenis V. Lunev ret = -EINVAL; 69723d6bd3bSDenis V. Lunev fail: 69823d6bd3bSDenis V. Lunev qemu_vfree(s->header); 69923d6bd3bSDenis V. Lunev return ret; 700d6179011SDenis V. Lunev 701d6179011SDenis V. Lunev fail_options: 702d6179011SDenis V. Lunev error_propagate(errp, local_err); 703d6179011SDenis V. Lunev ret = -EINVAL; 704d6179011SDenis V. Lunev goto fail; 70523d6bd3bSDenis V. Lunev } 70623d6bd3bSDenis V. Lunev 70723d6bd3bSDenis V. Lunev 708019d6b8fSAnthony Liguori static void parallels_close(BlockDriverState *bs) 709019d6b8fSAnthony Liguori { 710019d6b8fSAnthony Liguori BDRVParallelsState *s = bs->opaque; 7116dd6b9f1SDenis V. Lunev 7126dd6b9f1SDenis V. Lunev if (bs->open_flags & BDRV_O_RDWR) { 7136dd6b9f1SDenis V. Lunev s->header->inuse = 0; 7146dd6b9f1SDenis V. Lunev parallels_update_header(bs); 7156dd6b9f1SDenis V. Lunev } 7166dd6b9f1SDenis V. Lunev 71719f5dc15SDenis V. Lunev if (bs->open_flags & BDRV_O_RDWR) { 7189a4f4c31SKevin Wolf bdrv_truncate(bs->file->bs, s->data_end << BDRV_SECTOR_BITS); 71919f5dc15SDenis V. Lunev } 72019f5dc15SDenis V. Lunev 7210d31c7c2SDenis V. Lunev g_free(s->bat_dirty_bmap); 7229eae9ccaSDenis V. Lunev qemu_vfree(s->header); 723019d6b8fSAnthony Liguori } 724019d6b8fSAnthony Liguori 72574cf6c50SDenis V. Lunev static QemuOptsList parallels_create_opts = { 72674cf6c50SDenis V. Lunev .name = "parallels-create-opts", 72774cf6c50SDenis V. Lunev .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head), 72874cf6c50SDenis V. Lunev .desc = { 72974cf6c50SDenis V. Lunev { 73074cf6c50SDenis V. Lunev .name = BLOCK_OPT_SIZE, 73174cf6c50SDenis V. Lunev .type = QEMU_OPT_SIZE, 73274cf6c50SDenis V. Lunev .help = "Virtual disk size", 73374cf6c50SDenis V. Lunev }, 73474cf6c50SDenis V. Lunev { 73574cf6c50SDenis V. Lunev .name = BLOCK_OPT_CLUSTER_SIZE, 73674cf6c50SDenis V. Lunev .type = QEMU_OPT_SIZE, 73774cf6c50SDenis V. Lunev .help = "Parallels image cluster size", 73874cf6c50SDenis V. Lunev .def_value_str = stringify(DEFAULT_CLUSTER_SIZE), 73974cf6c50SDenis V. Lunev }, 74074cf6c50SDenis V. Lunev { /* end of list */ } 74174cf6c50SDenis V. Lunev } 74274cf6c50SDenis V. Lunev }; 74374cf6c50SDenis V. Lunev 744019d6b8fSAnthony Liguori static BlockDriver bdrv_parallels = { 745019d6b8fSAnthony Liguori .format_name = "parallels", 746019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVParallelsState), 747019d6b8fSAnthony Liguori .bdrv_probe = parallels_probe, 7481dec5a70SChristoph Hellwig .bdrv_open = parallels_open, 749019d6b8fSAnthony Liguori .bdrv_close = parallels_close, 750dd3bed16SRoman Kagan .bdrv_co_get_block_status = parallels_co_get_block_status, 751d0e61ce5SDenis V. Lunev .bdrv_has_zero_init = bdrv_has_zero_init_1, 7520d31c7c2SDenis V. Lunev .bdrv_co_flush_to_os = parallels_co_flush_to_os, 753481fb9cfSDenis V. Lunev .bdrv_co_readv = parallels_co_readv, 7545a41e1faSDenis V. Lunev .bdrv_co_writev = parallels_co_writev, 75574cf6c50SDenis V. Lunev 75674cf6c50SDenis V. Lunev .bdrv_create = parallels_create, 75749ad6467SDenis V. Lunev .bdrv_check = parallels_check, 75874cf6c50SDenis V. Lunev .create_opts = ¶llels_create_opts, 759019d6b8fSAnthony Liguori }; 760019d6b8fSAnthony Liguori 761019d6b8fSAnthony Liguori static void bdrv_parallels_init(void) 762019d6b8fSAnthony Liguori { 763019d6b8fSAnthony Liguori bdrv_register(&bdrv_parallels); 764019d6b8fSAnthony Liguori } 765019d6b8fSAnthony Liguori 766019d6b8fSAnthony Liguori block_init(bdrv_parallels_init); 767